Пример #1
0
	void PType(out TypeNode c) {
		c = null; TypeNode modifier=null, modified=null; 
		switch (la.kind) {
		case 6: {
			Get();
			c = SystemTypes.Object; 
			break;
		}
		case 7: {
			Get();
			c = SystemTypes.String; 
			break;
		}
		case 8: {
			Get();
			c = SystemTypes.Char; 
			break;
		}
		case 9: {
			Get();
			c = SystemTypes.Void; 
			break;
		}
		case 10: {
			Get();
			c = SystemTypes.Boolean; 
			break;
		}
		case 11: {
			Get();
			c = SystemTypes.Int8; 
			break;
		}
		case 12: {
			Get();
			c = SystemTypes.Int16; 
			break;
		}
		case 13: {
			Get();
			c = SystemTypes.Int32; 
			break;
		}
		case 14: {
			Get();
			c = SystemTypes.Int64; 
			break;
		}
		case 15: {
			Get();
			c = SystemTypes.UInt8; 
			break;
		}
		case 16: {
			Get();
			c = SystemTypes.UInt16; 
			break;
		}
		case 17: {
			Get();
			c = SystemTypes.UInt32; 
			break;
		}
		case 18: {
			Get();
			c = SystemTypes.UInt64; 
			break;
		}
		case 19: {
			Get();
			c = SystemTypes.Single; 
			break;
		}
		case 20: {
			Get();
			c = SystemTypes.Double; 
			break;
		}
		case 21: {
			Get();
			Expect(22);
			PType(out modifier);
			Expect(23);
			PType(out modified);
			Expect(24);
			c = OptionalModifier.For(modifier,modified); 
			break;
		}
		case 25: {
			Get();
			Expect(22);
			string id; 
			Ident(out id);
			Expect(24);
			c = LookupTypeParameter(id); 
			break;
		}
		case 26: {
			Get();
			Expect(22);
			string id; 
			Ident(out id);
			Expect(24);
			c = LookupMethodTypeParameter(id); 
			break;
		}
		case 1: case 28: case 35: {
			PTypeRef(out c);
			break;
		}
		default: SynErr(109); break;
		}
		if (StartOf(1)) {
			if (la.kind == 27) {
				Get();
				c = c.GetReferenceType(); 
			} else if (la.kind == 28) {
				Get();
				Expect(29);
				c = c.GetArrayType(1); 
				while (la.kind == 28) {
					Get();
					Expect(29);
					c = c.GetArrayType(1); 
				}
			} else if (la.kind == 22) {
				Get();
				PType(out c);
				while (la.kind == 23) {
					Get();
					PType(out c);
				}
				Expect(24);
			} else {
				Get();
				c = c.GetPointerType(); 
			}
		}
	}
Пример #2
0
        public override TypeNode VisitTypeReference(TypeNode type)
        { //TODO: break up this method
            if (type == null)
            {
                return(null);
            }
            TypeNodeList pars = this.pars;
            TypeNodeList args = this.args;

            switch (type.NodeType)
            {
            case NodeType.ArrayType:
                ArrayType arrType  = (ArrayType)type;
                TypeNode  elemType = this.VisitTypeReference(arrType.ElementType);
                if (elemType == arrType.ElementType || elemType == null)
                {
                    return(arrType);
                }
                if (arrType.IsSzArray())
                {
                    return(elemType.GetArrayType(1));
                }
                return(elemType.GetArrayType(arrType.Rank, arrType.Sizes, arrType.LowerBounds));

            case NodeType.DelegateNode:
            {
                FunctionType ftype = type as FunctionType;
                if (ftype == null)
                {
                    goto default;
                }
                TypeNode referringType = ftype.DeclaringType == null ? this.CurrentType : this.VisitTypeReference(ftype.DeclaringType);
                return(FunctionType.For(this.VisitTypeReference(ftype.ReturnType), this.VisitParameterList(ftype.Parameters), referringType));
            }

            case NodeType.Pointer:
                Pointer pType = (Pointer)type;
                elemType = this.VisitTypeReference(pType.ElementType);
                if (elemType == pType.ElementType || elemType == null)
                {
                    return(pType);
                }
                return(elemType.GetPointerType());

            case NodeType.Reference:
                Reference rType = (Reference)type;
                elemType = this.VisitTypeReference(rType.ElementType);
                if (elemType == rType.ElementType || elemType == null)
                {
                    return(rType);
                }
                return(elemType.GetReferenceType());

            case NodeType.ArrayTypeExpression:
                ArrayTypeExpression aExpr = (ArrayTypeExpression)type;
                aExpr.ElementType = this.VisitTypeReference(aExpr.ElementType);
                return(aExpr);

            case NodeType.BoxedTypeExpression:
                BoxedTypeExpression bExpr = (BoxedTypeExpression)type;
                bExpr.ElementType = this.VisitTypeReference(bExpr.ElementType);
                return(bExpr);

            case NodeType.ClassExpression:
            {
                ClassExpression cExpr = (ClassExpression)type;
                cExpr.Expression = this.VisitTypeExpression(cExpr.Expression);

                //Could happen if the expression is a template parameter
                if (cExpr.Expression is Literal lit)
                {
                    return(lit.Value as TypeNode);
                }

                cExpr.TemplateArguments = this.VisitTypeReferenceList(cExpr.TemplateArguments);
                return(cExpr);
            }

            case NodeType.ClassParameter:
            case NodeType.TypeParameter:
                int key = type.UniqueKey;
                for (int i = 0, n = pars == null ? 0 : pars.Count, m = args == null ? 0 : args.Count; i < n && i < m; i++)
                {
                    //^ assert pars != null && args != null;
                    TypeNode tp = pars[i];
                    if (tp == null)
                    {
                        continue;
                    }
                    if (tp.UniqueKey == key)
                    {
                        return(args[i]);
                    }
                    if (tp.Name.UniqueIdKey == type.Name.UniqueIdKey && (tp is ClassParameter && type is TypeParameter))
                    {
                        //This shouldn't really happen, but in practice it does. Hack past it.
                        return(args[i]);
                    }
                }
                return(type);

            case NodeType.FlexArrayTypeExpression:
                FlexArrayTypeExpression flExpr = (FlexArrayTypeExpression)type;
                flExpr.ElementType = this.VisitTypeReference(flExpr.ElementType);
                return(flExpr);

            case NodeType.FunctionTypeExpression:
                FunctionTypeExpression ftExpr = (FunctionTypeExpression)type;
                ftExpr.Parameters = this.VisitParameterList(ftExpr.Parameters);
                ftExpr.ReturnType = this.VisitTypeReference(ftExpr.ReturnType);
                return(ftExpr);

            case NodeType.InvariantTypeExpression:
                InvariantTypeExpression invExpr = (InvariantTypeExpression)type;
                invExpr.ElementType = this.VisitTypeReference(invExpr.ElementType);
                return(invExpr);

            case NodeType.InterfaceExpression:
                InterfaceExpression iExpr = (InterfaceExpression)type;
                if (iExpr.Expression == null)
                {
                    goto default;
                }
                iExpr.Expression        = this.VisitTypeExpression(iExpr.Expression);
                iExpr.TemplateArguments = this.VisitTypeReferenceList(iExpr.TemplateArguments);
                return(iExpr);

            case NodeType.NonEmptyStreamTypeExpression:
                NonEmptyStreamTypeExpression neExpr = (NonEmptyStreamTypeExpression)type;
                neExpr.ElementType = this.VisitTypeReference(neExpr.ElementType);
                return(neExpr);

            case NodeType.NonNullTypeExpression:
                NonNullTypeExpression nnExpr = (NonNullTypeExpression)type;
                nnExpr.ElementType = this.VisitTypeReference(nnExpr.ElementType);
                return(nnExpr);

            case NodeType.NonNullableTypeExpression:
                NonNullableTypeExpression nbExpr = (NonNullableTypeExpression)type;
                nbExpr.ElementType = this.VisitTypeReference(nbExpr.ElementType);
                return(nbExpr);

            case NodeType.NullableTypeExpression:
                NullableTypeExpression nuExpr = (NullableTypeExpression)type;
                nuExpr.ElementType = this.VisitTypeReference(nuExpr.ElementType);
                return(nuExpr);

            case NodeType.OptionalModifier:
            {
                TypeModifier modType      = (TypeModifier)type;
                TypeNode     modifiedType = this.VisitTypeReference(modType.ModifiedType);
                TypeNode     modifierType = this.VisitTypeReference(modType.Modifier);
                if (modifiedType == null || modifierType == null)
                {
                    return(type);
                }

                return(OptionalModifier.For(modifierType, modifiedType));
            }

            case NodeType.RequiredModifier:
            {
                TypeModifier modType      = (TypeModifier)type;
                TypeNode     modifiedType = this.VisitTypeReference(modType.ModifiedType);
                TypeNode     modifierType = this.VisitTypeReference(modType.Modifier);
                if (modifiedType == null || modifierType == null)
                {
                    Debug.Fail(""); return(type);
                }
                return(RequiredModifier.For(modifierType, modifiedType));
            }

            case NodeType.OptionalModifierTypeExpression:
                OptionalModifierTypeExpression optmodType = (OptionalModifierTypeExpression)type;
                optmodType.ModifiedType = this.VisitTypeReference(optmodType.ModifiedType);
                optmodType.Modifier     = this.VisitTypeReference(optmodType.Modifier);
                return(optmodType);

            case NodeType.RequiredModifierTypeExpression:
                RequiredModifierTypeExpression reqmodType = (RequiredModifierTypeExpression)type;
                reqmodType.ModifiedType = this.VisitTypeReference(reqmodType.ModifiedType);
                reqmodType.Modifier     = this.VisitTypeReference(reqmodType.Modifier);
                return(reqmodType);

            case NodeType.PointerTypeExpression:
                PointerTypeExpression pExpr = (PointerTypeExpression)type;
                pExpr.ElementType = this.VisitTypeReference(pExpr.ElementType);
                return(pExpr);

            case NodeType.ReferenceTypeExpression:
                ReferenceTypeExpression rExpr = (ReferenceTypeExpression)type;
                rExpr.ElementType = this.VisitTypeReference(rExpr.ElementType);
                return(rExpr);

            case NodeType.StreamTypeExpression:
                StreamTypeExpression sExpr = (StreamTypeExpression)type;
                sExpr.ElementType = this.VisitTypeReference(sExpr.ElementType);
                return(sExpr);

            case NodeType.TupleTypeExpression:
                TupleTypeExpression tuExpr = (TupleTypeExpression)type;
                tuExpr.Domains = this.VisitFieldList(tuExpr.Domains);
                return(tuExpr);

            case NodeType.TypeExpression:
            {
                TypeExpression tExpr = (TypeExpression)type;
                tExpr.Expression = this.VisitTypeExpression(tExpr.Expression);
                if (tExpr.Expression is Literal)
                {
                    return(type);
                }
                tExpr.TemplateArguments = this.VisitTypeReferenceList(tExpr.TemplateArguments);
                return(tExpr);
            }

            case NodeType.TypeIntersectionExpression:
                TypeIntersectionExpression tiExpr = (TypeIntersectionExpression)type;
                tiExpr.Types = this.VisitTypeReferenceList(tiExpr.Types);
                return(tiExpr);

            case NodeType.TypeUnionExpression:
                TypeUnionExpression tyuExpr = (TypeUnionExpression)type;
                tyuExpr.Types = this.VisitTypeReferenceList(tyuExpr.Types);
                return(tyuExpr);

            default:
                TypeNode declaringType = this.VisitTypeReference(type.DeclaringType);
                if (declaringType != null)
                {
                    Identifier tname = type.Name;
                    if (type.Template != null && type.IsGeneric)
                    {
                        tname = type.Template.Name;
                    }
                    TypeNode nt = declaringType.GetNestedType(tname);
                    if (nt != null)
                    {
                        TypeNodeList arguments = type.TemplateArguments;
                        type = nt;
                        if (TargetPlatform.UseGenerics)
                        {
                            if (arguments != null && arguments.Count > 0 && nt.ConsolidatedTemplateParameters != null && nt.ConsolidatedTemplateParameters.Count > 0)
                            {
                                type = nt.GetTemplateInstance(this.TargetModule, this.CurrentType, declaringType, arguments);
                            }
                        }
                    }
                }

                if (type.Template != null && (type.ConsolidatedTemplateParameters == null || type.ConsolidatedTemplateParameters.Count == 0))
                {
                    if (!type.IsNotFullySpecialized && (!type.IsNormalized ||
                                                        (this.CurrentType != null && type.DeclaringModule == this.CurrentType.DeclaringModule)))
                    {
                        return(type);
                    }

                    // Type is a template instance, but some of its arguments were themselves parameters.
                    // See if any of these parameters are to be specialized by this specializer.
                    bool         mustSpecializeFurther = false;
                    TypeNodeList targs   = type.TemplateArguments;
                    int          numArgs = targs == null ? 0 : targs.Count;

                    if (targs != null)
                    {
                        targs = new TypeNodeList(targs);

                        for (int i = 0; i < numArgs; i++)
                        {
                            TypeNode targ = targs[i];

                            if (targ is ITypeParameter tparg)
                            {
                                for (int j = 0, np = pars == null ? 0 : pars.Count, m = args == null ? 0 : args.Count; j < np && j < m; j++)
                                {
                                    //^ assert pars != null && args != null;
                                    if (TargetPlatform.UseGenerics)
                                    {
                                        if (!(pars[j] is ITypeParameter par))
                                        {
                                            continue;
                                        }

                                        if (tparg == par || (tparg.ParameterListIndex == par.ParameterListIndex && tparg.DeclaringMember == par.DeclaringMember))
                                        {
                                            targ = this.args[j];
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (targ == pars[j])
                                        {
                                            targ = this.args[j];
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (targ != type)
                                {
                                    targ = this.VisitTypeReference(targ);
                                }

                                if (targ == type)
                                {
                                    continue;
                                }
                            }

                            mustSpecializeFurther |= targs[i] != targ;
                            targs[i] = targ;
                        }
                    }

                    if (targs == null || !mustSpecializeFurther)
                    {
                        return(type);
                    }

                    return(type.Template.GetTemplateInstance(this.TargetModule, this.CurrentType, declaringType, targs));
                }

                TypeNodeList tPars = type.TemplateParameters;
                if (tPars == null || tPars.Count == 0)
                {
                    return(type);                                       //Not a parameterized type. No need to get an instance.
                }
                TypeNodeList tArgs = new TypeNodeList();
                for (int i = 0, n = tPars.Count; i < n; i++)
                {
                    TypeNode tPar = tPars[i];
                    tArgs.Add(tPar);     //Leave parameter in place if there is no match
                    if (tPar == null || tPar.Name == null)
                    {
                        continue;
                    }
                    int idKey = tPar.Name.UniqueIdKey;
                    for (int j = 0, m = pars == null ? 0 : pars.Count, k = args == null ? 0 : args.Count; j < m && j < k; j++)
                    {
                        //^ assert pars != null && args != null;
                        TypeNode par = pars[j];
                        if (par == null || par.Name == null)
                        {
                            continue;
                        }
                        if (par.Name.UniqueIdKey == idKey)
                        {
                            tArgs[i] = args[j];
                            break;
                        }
                    }
                }
                return(type.GetTemplateInstance(this.TargetModule, this.CurrentType, this.VisitTypeReference(type.DeclaringType), tArgs));
            }
        }
        public void Parse_UnsafeCallWithSingleVariableSetSafeByIndexer_ReturnsProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample         = TestHelper.GetSample <IndexerSample>("UnsafeCallWithSingleVariableSetSafeByIndexer", stringTypeNode.GetArrayType(1));

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
Пример #4
0
        public void Setup(AssemblyNode mscorlib, AssemblyNode jsTypes, AssemblyNode rewriteAssembly)
        {
            NumWarnings = 0;
            NumErrors = 0;

            MsCorLib = mscorlib;

            AssemblyDelaySignAttributeType = GetType(mscorlib, "System.Reflection", "AssemblyDelaySignAttribute");

            VoidType = GetType(mscorlib, "System", "Void");
            ObjectType = GetType(mscorlib, "System", "Object");
            StringType = GetType(mscorlib, "System", "String");
            IntType = GetType(mscorlib, "System", "Int32");
            BooleanType = GetType(mscorlib, "System", "Boolean");
            MethodBaseType = GetType(mscorlib, "System.Reflection", "MethodBase");
            RuntimeTypeHandleType = GetType(mscorlib, "System", "RuntimeTypeHandle");
            NullableTypeConstructor = GetType(mscorlib, "System", "Nullable`1");
            CompilerGeneratedAttributeType = GetType
                (mscorlib, "System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
            DllImportAttributeType = GetType(mscorlib, "System.Runtime.InteropServices", "DllImportAttribute");

            TypeType = GetType(mscorlib, "System", "Type");
            Type_GetMethodMethod = GetMethod(TypeType, "GetMethod", StringType, TypeType.GetArrayType(1));
            Type_GetMemberMethod = GetMethod(TypeType, "GetMember", StringType);
            Type_GetConstructorMethod = GetMethod(TypeType, "GetConstructor", TypeType.GetArrayType(1));
            Type_GetTypeFromHandleMethod = GetMethod(TypeType, "GetTypeFromHandle", RuntimeTypeHandleType);
            Type_GetGenericArgumentsMethod = GetMethod(TypeType, "GetGenericArguments");
            Type_MakeArrayTypeMethod = GetMethod(TypeType, "MakeArrayType");
            Type_MakeGenericTypeMethod = GetMethod(TypeType, "MakeGenericType", TypeType.GetArrayType(1));

            InteropTypes = new InteropTypes(this);
            InteropManager = new InteropManager(this);

            IgnoreAttributeType = GetType(jsTypes, Constants.JSTypesIL2JSNS, "IgnoreAttribute");
            InteropAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "InteropAttribute");
            NamingAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "NamingAttribute");
            ImportAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "ImportAttribute");
            ImportKeyAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "ImportKeyAttribute");
            ExportAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "ExportAttribute");
            NotExportedAttributeType = GetType(jsTypes, Constants.JSTypesInteropNS, "NotExportedAttribute");
            InteropGeneratedAttributeType = GetType(jsTypes, Constants.JSTypesIL2JSNS, "InteropGeneratedAttribute");

            JSTypes = jsTypes;

            JSContextType = GetType(jsTypes, Constants.JSTypesInteropNS, "JSContext");

            InteropContextManagerType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "InteropContextManager");
            InteropContextManager_GetDatabaseMethod = GetMethod(InteropContextManagerType, "get_Database");
            InteropContextManager_GetCurrentRuntimeMethod = GetMethod(InteropContextManagerType, "get_CurrentRuntime");
            InteropContextManager_GetRuntimeForObjectMethod = GetMethod
                (InteropContextManagerType, "GetRuntimeForObject", ObjectType);

            InteropDatabaseType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "InteropDatabase");
            InteropDatabase_RegisterRootExpression = GetMethod(InteropDatabaseType, "RegisterRootExpression", StringType);
            InteropDatabase_RegisterDelegateShimMethod = GetMethod
                (InteropDatabaseType, "RegisterDelegateShim", TypeType);
            InteropDatabase_RegisterTypeMethod = GetMethod
                (InteropDatabaseType,
                 "RegisterType",
                 TypeType,
                 IntType,
                 StringType,
                 StringType,
                 IntType,
                 BooleanType,
                 BooleanType,
                 BooleanType);
            InteropDatabase_RegisterExportMethod = GetMethod
                (InteropDatabaseType, "RegisterExport", MethodBaseType, BooleanType, StringType);

            SimpleMethodBaseType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleMethodBase");

            SimpleMethodInfoType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleMethodInfo");
            SimpleMethodInfo_Ctor = GetConstructor
                (SimpleMethodInfoType, BooleanType, StringType, TypeType, TypeType.GetArrayType(1), TypeType);

            SimpleConstructorInfoType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleConstructorInfo");
            SimpleConstructorInfo_Ctor = GetConstructor(SimpleConstructorInfoType, TypeType, TypeType.GetArrayType(1));

            RuntimeType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "Runtime");
            Runtime_CompleteConstructionMethod = GetMethod
                (RuntimeType, "CompleteConstruction", SimpleMethodBaseType, ObjectType, JSContextType);
            Runtime_CallImportedMethodMethod = GetMethod
                (RuntimeType, "CallImportedMethod", SimpleMethodBaseType, StringType, ObjectType.GetArrayType(1));

            UniversalDelegateType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "UniversalDelegate");
            UniversalDelegate_InvokeMethod = GetUniqueMethod(UniversalDelegateType, "Invoke");

            MethodBase_GetGenericArgumentsMethod = GetMethod(MethodBaseType, "GetGenericArguments");

            ModuleType = GetType(rewriteAssembly, "", "<Module>");
            foreach (var member in ModuleType.Members)
            {
                var cctor = member as StaticInitializer;
                if (cctor != null)
                    ModuleCCtorMethod = cctor;
            }
        }
        public void Parse_SafeCallUsingIndexer_NoProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample         = TestHelper.GetSample <IndexerSample>("SafeCallUsingIndexer", stringTypeNode.GetArrayType(1));

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
Пример #6
0
 public TypeNode ArrayType(TypeNode type, int rank)
 {
     return(type.GetArrayType(rank));
 }
Пример #7
0
 private TypeNode/*!*/ ParseArrayOrGenericType(string typeName, TypeNode/*!*/ rootType)
 {
     if (typeName == null || rootType == null) { Debug.Assert(false); return rootType; }
     //Get here after "rootType[" has been parsed. What follows is either an array type specifier or some generic type arguments.
     if (typeName.Length == 0)
         throw new InvalidMetadataException(ExceptionStrings.BadSerializedTypeName); //Something ought to follow the [
     if (typeName[0] == ']')
     { //Single dimensional array with zero lower bound
         if (typeName.Length == 1) return rootType.GetArrayType(1);
         if (typeName[1] == '[' && typeName.Length > 2)
             return this.ParseArrayOrGenericType(typeName.Substring(2), rootType.GetArrayType(1));
         throw new InvalidMetadataException(ExceptionStrings.BadSerializedTypeName);
     }
     if (typeName[0] == '*')
     { //Single dimensional array with unknown lower bound
         if (typeName.Length > 1 && typeName[1] == ']')
         {
             if (typeName.Length == 2) return rootType.GetArrayType(1, true);
             if (typeName[2] == '[' && typeName.Length > 3)
                 return this.ParseArrayOrGenericType(typeName.Substring(3), rootType.GetArrayType(1, true));
         }
         throw new InvalidMetadataException(ExceptionStrings.BadSerializedTypeName);
     }
     if (typeName[0] == ',')
     { //Muti dimensional array
         int rank = 1;
         while (rank < typeName.Length && typeName[rank] == ',') rank++;
         if (rank < typeName.Length && typeName[rank] == ']')
         {
             if (typeName.Length == rank + 1) return rootType.GetArrayType(rank + 1);
             if (typeName[rank + 1] == '[' && typeName.Length > rank + 2)
                 return this.ParseArrayOrGenericType(typeName.Substring(rank + 2), rootType.GetArrayType(rank));
         }
         throw new InvalidMetadataException(ExceptionStrings.BadSerializedTypeName);
     }
     //Generic type instance
     int offset = 0;
     if (typeName[0] == '[') offset = 1; //Assembly qualified type name forming part of a generic parameter list        
     TypeNodeList arguments = new TypeNodeList();
     int commaPos = FindFirstCommaOutsideBrackets(typeName);
     while (commaPos > 1)
     {
         arguments.Add(this.GetTypeFromSerializedName(typeName.Substring(offset, commaPos - offset)));
         typeName = typeName.Substring(commaPos + 1);
         offset = typeName[0] == '[' ? 1 : 0;
         commaPos = FindFirstCommaOutsideBrackets(typeName);
     }
     //Find the position of the first unbalanced ].
     int lastCharPos = offset;
     for (int leftBracketCount = 0; lastCharPos < typeName.Length; lastCharPos++)
     {
         char ch = typeName[lastCharPos];
         if (ch == '[') leftBracketCount++;
         else if (ch == ']')
         {
             leftBracketCount--;
             if (leftBracketCount < 0) break;
         }
     }
     arguments.Add(this.GetTypeFromSerializedName(typeName.Substring(offset, lastCharPos - offset)));
     TypeNode retVal = rootType.GetGenericTemplateInstance(this.module, arguments);
     if (lastCharPos + 1 < typeName.Length && typeName[lastCharPos + 1] == ']')
         lastCharPos++;
     if (lastCharPos + 1 < typeName.Length)
     {
         //The generic type is complete, but there is yet more to the type
         char ch = typeName[lastCharPos + 1];
         if (ch == '+') retVal = this.GetTypeFromSerializedName(typeName.Substring(lastCharPos + 2), retVal);
         if (ch == '&') retVal = retVal.GetReferenceType();
         if (ch == '*') retVal = retVal.GetPointerType();
         if (ch == '[') retVal = this.ParseArrayOrGenericType(typeName.Substring(lastCharPos + 2, typeName.Length - 1 - lastCharPos - 1), retVal);
     }
     return retVal;
 }
Пример #8
0
        public void Setup(AssemblyNode mscorlib, AssemblyNode jsTypes, AssemblyNode rewriteAssembly)
        {
            NumWarnings = 0;
            NumErrors   = 0;

            MsCorLib = mscorlib;

            AssemblyDelaySignAttributeType = GetType(mscorlib, "System.Reflection", "AssemblyDelaySignAttribute");

            VoidType                       = GetType(mscorlib, "System", "Void");
            ObjectType                     = GetType(mscorlib, "System", "Object");
            StringType                     = GetType(mscorlib, "System", "String");
            IntType                        = GetType(mscorlib, "System", "Int32");
            BooleanType                    = GetType(mscorlib, "System", "Boolean");
            MethodBaseType                 = GetType(mscorlib, "System.Reflection", "MethodBase");
            RuntimeTypeHandleType          = GetType(mscorlib, "System", "RuntimeTypeHandle");
            NullableTypeConstructor        = GetType(mscorlib, "System", "Nullable`1");
            CompilerGeneratedAttributeType = GetType
                                                 (mscorlib, "System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
            DllImportAttributeType = GetType(mscorlib, "System.Runtime.InteropServices", "DllImportAttribute");

            TypeType                       = GetType(mscorlib, "System", "Type");
            Type_GetMethodMethod           = GetMethod(TypeType, "GetMethod", StringType, TypeType.GetArrayType(1));
            Type_GetMemberMethod           = GetMethod(TypeType, "GetMember", StringType);
            Type_GetConstructorMethod      = GetMethod(TypeType, "GetConstructor", TypeType.GetArrayType(1));
            Type_GetTypeFromHandleMethod   = GetMethod(TypeType, "GetTypeFromHandle", RuntimeTypeHandleType);
            Type_GetGenericArgumentsMethod = GetMethod(TypeType, "GetGenericArguments");
            Type_MakeArrayTypeMethod       = GetMethod(TypeType, "MakeArrayType");
            Type_MakeGenericTypeMethod     = GetMethod(TypeType, "MakeGenericType", TypeType.GetArrayType(1));

            InteropTypes   = new InteropTypes(this);
            InteropManager = new InteropManager(this);

            IgnoreAttributeType           = GetType(jsTypes, Constants.JSTypesIL2JSNS, "IgnoreAttribute");
            InteropAttributeType          = GetType(jsTypes, Constants.JSTypesInteropNS, "InteropAttribute");
            NamingAttributeType           = GetType(jsTypes, Constants.JSTypesInteropNS, "NamingAttribute");
            ImportAttributeType           = GetType(jsTypes, Constants.JSTypesInteropNS, "ImportAttribute");
            ImportKeyAttributeType        = GetType(jsTypes, Constants.JSTypesInteropNS, "ImportKeyAttribute");
            ExportAttributeType           = GetType(jsTypes, Constants.JSTypesInteropNS, "ExportAttribute");
            NotExportedAttributeType      = GetType(jsTypes, Constants.JSTypesInteropNS, "NotExportedAttribute");
            InteropGeneratedAttributeType = GetType(jsTypes, Constants.JSTypesIL2JSNS, "InteropGeneratedAttribute");

            JSTypes = jsTypes;

            JSContextType = GetType(jsTypes, Constants.JSTypesInteropNS, "JSContext");

            InteropContextManagerType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "InteropContextManager");
            InteropContextManager_GetDatabaseMethod         = GetMethod(InteropContextManagerType, "get_Database");
            InteropContextManager_GetCurrentRuntimeMethod   = GetMethod(InteropContextManagerType, "get_CurrentRuntime");
            InteropContextManager_GetRuntimeForObjectMethod = GetMethod
                                                                  (InteropContextManagerType, "GetRuntimeForObject", ObjectType);

            InteropDatabaseType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "InteropDatabase");
            InteropDatabase_RegisterRootExpression     = GetMethod(InteropDatabaseType, "RegisterRootExpression", StringType);
            InteropDatabase_RegisterDelegateShimMethod = GetMethod
                                                             (InteropDatabaseType, "RegisterDelegateShim", TypeType);
            InteropDatabase_RegisterTypeMethod = GetMethod
                                                     (InteropDatabaseType,
                                                     "RegisterType",
                                                     TypeType,
                                                     IntType,
                                                     StringType,
                                                     StringType,
                                                     IntType,
                                                     BooleanType,
                                                     BooleanType,
                                                     BooleanType);
            InteropDatabase_RegisterExportMethod = GetMethod
                                                       (InteropDatabaseType, "RegisterExport", MethodBaseType, BooleanType, StringType);

            SimpleMethodBaseType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleMethodBase");

            SimpleMethodInfoType  = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleMethodInfo");
            SimpleMethodInfo_Ctor = GetConstructor
                                        (SimpleMethodInfoType, BooleanType, StringType, TypeType, TypeType.GetArrayType(1), TypeType);

            SimpleConstructorInfoType  = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "SimpleConstructorInfo");
            SimpleConstructorInfo_Ctor = GetConstructor(SimpleConstructorInfoType, TypeType, TypeType.GetArrayType(1));

            RuntimeType = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "Runtime");
            Runtime_CompleteConstructionMethod = GetMethod
                                                     (RuntimeType, "CompleteConstruction", SimpleMethodBaseType, ObjectType, JSContextType);
            Runtime_CallImportedMethodMethod = GetMethod
                                                   (RuntimeType, "CallImportedMethod", SimpleMethodBaseType, StringType, ObjectType.GetArrayType(1));

            UniversalDelegateType          = GetType(jsTypes, Constants.JSTypesManagedInteropNS, "UniversalDelegate");
            UniversalDelegate_InvokeMethod = GetUniqueMethod(UniversalDelegateType, "Invoke");

            MethodBase_GetGenericArgumentsMethod = GetMethod(MethodBaseType, "GetGenericArguments");

            ModuleType = GetType(rewriteAssembly, "", "<Module>");
            foreach (var member in ModuleType.Members)
            {
                var cctor = member as StaticInitializer;
                if (cctor != null)
                {
                    ModuleCCtorMethod = cctor;
                }
            }
        }
Пример #9
0
        public Expression ArrayExpression(ExpressionList expressions, TypeNode arrayElementType)
        {
            var clrElementType = arrayElementType;
            var enumElementType = clrElementType as EnumNode;
            if (enumElementType != null)
                clrElementType = enumElementType.UnderlyingType;

            if (expressions == null)
                // null;
                return new Literal(null, arrayElementType.GetArrayType(1));
            else
            {
                // newarr <expressions.length>;
                var statements = new StatementList();
                statements.Add
                    (new ExpressionStatement
                         (new ConstructArray
                              (arrayElementType, new ExpressionList(new Literal(expressions.Count)), null)));
                var arrayType = arrayElementType.GetArrayType(1);
                for (var i = 0; i < expressions.Count; i++)
                {
                    // dup;
                    // <i>;
                    // <expressions[i]>;
                    // stelem;
                    statements.Add
                        (new AssignmentStatement
                             (new Indexer
                                  (new Expression(NodeType.Dup, arrayType),
                                   new ExpressionList(new Literal(i)),
                                   clrElementType),
                              expressions[i]));
                }
                // leave array on stack
                return new BlockExpression(new Block(statements));
            }
        }