DefineNestedType() public method

public DefineNestedType ( string name ) : System.Reflection.Emit.TypeBuilder
name string
return System.Reflection.Emit.TypeBuilder
Esempio n. 1
0
 public System.Type DeclareEnum(TypeBuilder module, string name, IEnumerable<string> values)
 {
     var enumType = module.DefineNestedType(name, TypeAttributes.NestedPublic | TypeAttributes.Sealed, typeof(Enum));
     enumType.DefineField("value__", typeof(int), FieldAttributes.Private | FieldAttributes.SpecialName);
     var i = 0;
     foreach (var value in values)
     {
         FieldBuilder field = enumType.DefineField(value.ToString(), enumType, FieldAttributes.Public | FieldAttributes.Literal | FieldAttributes.Static);
         field.SetConstant(i++);
     }
     return enumType.CreateType();
 }
Esempio n. 2
0
        private TypeBuilder DefineType(Cci.INamedTypeDefinition typeDef, TypeBuilder containingTypeBuilder)
        {
            Debug.Assert(!_typeBuilders.ContainsKey(typeDef));
            TypeBuilder typeBuilder;

            string mangledName = Cci.MetadataWriter.GetMangledName(typeDef);
            TypeAttributes attrs = (TypeAttributes)Cci.MetadataWriter.GetTypeDefFlags(typeDef, _context);

            if (containingTypeBuilder != null)
            {
                typeBuilder = containingTypeBuilder.DefineNestedType(mangledName, attrs, null, (PackingSize)typeDef.Alignment, (int)typeDef.SizeOf);
            }
            else
            {
                var namespaceType = (Cci.INamespaceTypeDefinition)typeDef;

                typeBuilder = _builder.DefineType(
                    MetadataHelpers.BuildQualifiedName(namespaceType.NamespaceName, mangledName),
                    attrs,
                    null, // parent set later
                    (PackingSize)typeDef.Alignment,
                    (int)typeDef.SizeOf
                );
            }

            // generic parameters:
            // We need to define generic parameters so that type references that target them can be resolved later on:
            var typeParameters = GetConsolidatedTypeParameters(typeDef);
            if (typeParameters != null)
            {
                DefineGenericParameters(typeBuilder, null, typeParameters);
            }

            _typeBuilders.Add(typeDef, typeBuilder);
            return typeBuilder;
        }
        /// <summary>
        /// Crea una clase que representara a esta funcion
        /// Este metodo debe ser llamado antes de llamar al GenerateCode
        /// </summary>
        /// <param name="typeBuilder"></param>
        public void CreateCallableClass(TypeBuilder typeBuilder)
        {
            //Las funciones debemos declararlas en un scope anidado, es decir, que sea una clase anidada
            //Guardamos la funcion que definimos en la informacion de este callable
            Callable.ILCallable = typeBuilder.DefineNestedType(Callable.GetName(), TypeAttributes.NestedPublic | TypeAttributes.Class);

            //Como estamos definiendo un nuevo scope, y a veces sera necesario subir hacia los scopes padres, es necesario recibir una instancia del scope anterior
            ParentInstance = Callable.ILCallable.DefineField(NamesGenerator.GenerateNewName(), typeBuilder,
                                                             FieldAttributes.Public);

            //El constructor recibira tantos parametros como reciba la funcion
            //Para poder generar el constructor, definimos primeramente los campos que almacenara esta clase (los parametros)
            Callable.FieldsInClass = new Dictionary<string, FieldBuilder>(); //Donde guardaremos los campos definidos en esta clase asociados a esta funcion
            //Lista de tipos que recibira el constructor
            IList<Type> realTypesOfParameters = new List<Type>();
            //Lo primero que recibe es la instancia del padre
            realTypesOfParameters.Add(typeBuilder);

            foreach (var field in Callable.Fields) {
                //Cogemos el tipo del parametro actual. No era necesario hacerlo desde el chequeo semantico pues aqui tenemos el Scope por ser un IScopeDefiner
                var type = Scope.GetType(field.TypeId).GetILType();
                realTypesOfParameters.Add(type); //Anadimos el tipo (en C#) de este parametro para pasarselo al constructor (el Type[] que recibe)
                //Creamos el campo en la clase
                string name = NamesGenerator.GenerateNewName(); //Nombre del campo
                var fieldBuilder = Callable.ILCallable.DefineField(name, type, FieldAttributes.Public); //Tipo del campo
                Callable.FieldsInClass.Add(name, fieldBuilder);
            }

            //Ahora creamos el constructor, de forma similar a como hicimos con el RecordDeclarationNode
            ConstructorBuilder constructor = Callable.ILCallable.DefineConstructor(MethodAttributes.Public,
                                                                                   CallingConventions.Standard,
                                                                                   realTypesOfParameters.ToArray());
            //Guardamos el constructor en la informacion de este callable para cuando se vaya a instanciar
            //Y ademas para cuando se ejecute el metodo CheckSemantic de este mismo nodo
            Callable.Constructor = constructor;

            //Creamos el cuerpo de la funcion (un metodo en esta clase)
            //El metodo sera publico y no estatico
            //Le asignamos el tipo de retorno que retorna esta funcion en caso de que no sea un procedimiento
            var returnType = Callable.Type == null ? typeof (void) : Scope.GetType(Callable.Type).GetILType();
            var methodBuilder = Callable.ILCallable.DefineMethod("Run", MethodAttributes.Public, //TODO: HideBySig?
                returnType, new Type[0]);
            //Asignamos el metodo principal de esta funcion a la informacion de la funcion
            Callable.Method = methodBuilder;
        }
Esempio n. 4
0
        public override void GenerateCode(ILGenerator generator, TypeBuilder typeBuilder)
        {
            //Creamos la clase correspondiente a este Let, pues la expresion Let-In-End define un nuevo scope
            var letTypeBuilder = typeBuilder.DefineNestedType(NamesGenerator.GenerateNewName(), TypeAttributes.NestedPublic | TypeAttributes.Class);

            #region Constructor

            //Creamos el constructor de la clase, que recibe un parametro del padre
            var constructor = letTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,
                                             new Type[] {typeBuilder});

            //Definimos los dos campos iniciales que tendra la clase
            //Break (Int32) -> Flag que indica si se tiene que romper romper el flujo de la ejecucion o no. Se le da valor desde un hijo (Break u otro Let-In-End)
            BreakField = letTypeBuilder.DefineField("Break", typeof (int), FieldAttributes.Public);
            //ParentInstance (Tipo del padre) -> Referencia a la clase padre
            ParentInstance = letTypeBuilder.DefineField("Parent", typeBuilder, FieldAttributes.Public);

            //Anadimos el codigo necesario en el constructor
            var genConstructor = constructor.GetILGenerator();

            //Le asignamos a ParentInstance la instancia de mi padre que me pasaron en el constructor
            genConstructor.Emit(OpCodes.Ldarg_0); //Cargamos 'this'
            genConstructor.Emit(OpCodes.Ldarg_1); //Cargamos el parametro del constructor (mi padre)
            genConstructor.Emit(OpCodes.Stfld, ParentInstance); //Cargamos el parametro del constructor (mi padre)

            //Le asignamos el valor '0' al campo 'Break'
            genConstructor.Emit(OpCodes.Ldarg_0); //Cargamos 'this'
            genConstructor.Emit(OpCodes.Ldc_I4, 0); //Metemos en la pila un '0'
            genConstructor.Emit(OpCodes.Stfld, BreakField); //Le asignamos el '0' al campo 'Break' de esta clase

            //Generamos todas las declaraciones ya aqui en el constructor (para que los campos sean asignados a la clase antes de ejecutar el cuerpo del Let)
            foreach (var group in GroupedDeclarations) {
                //Generamos las declaraciones usando el generador del constructor, y se guardaran en la clase ya creada para este Let
                GenerateCodeOfDeclarations(group, genConstructor, letTypeBuilder);
            }

            //Terminamos el constructor
            genConstructor.Emit(OpCodes.Ret);

            #endregion

            //El tipo de retorno del metodo que crearemos depende del tipo de retorno de esta expresion Let-In-End
            //Si no retorna nada, el metodo es Void. Sino, el metodo retorna el tipo de retorno de la expresion
            var methodReturnType = ReturnType == null ? typeof(void) : ReturnType.GetILType();
            //Creamos el metodo principal donde se ejecutara el cuerpo de este Let. No recibe parametros
            var runMethodBuilder = letTypeBuilder.DefineMethod("Run", MethodAttributes.Public, CallingConventions.HasThis,
                                                            methodReturnType, new Type[0]);

            var runGenerator = runMethodBuilder.GetILGenerator();

            //Etiqueta para donde se saltara en caso de que se interrumpa el flujo de ejecucion
            LabelEnd = runGenerator.DefineLabel();

            //Generamos el codigo de la secuencia de expresiones dentro del metodo, y dentro de la clase que creamos para el Let (logico)

            Expressions.GenerateCode(runGenerator, letTypeBuilder);

            //Aqui almacenaremos el valor de retorno del Let (si retorna)
            //            LocalBuilder resultLocal = null;
            //Si la secuencia de expresiones retorna algun valor
            //            if (ReturnType != null) {
                //Creamos la variable donde guardaremos el valor
            //                resultLocal = runGenerator.DeclareLocal(ReturnType.GetILType());
                //Asignamos el valor
            //                runGenerator.Emit(OpCodes.Stloc, resultLocal);
            //            }

            //Ponemos aqui la etiqueta 'END'
            runGenerator.MarkLabel(LabelEnd);

            //Aqui comprobamos si me mandaron a interrumpir el flujo de ejecucion o no...

            //Etiqueta a donde se saltara en caso de que no haga falta interrumpir la ejecucion
            Label lblNoBreak = runGenerator.DefineLabel();

            //Lo siguiente lo tenemos que hacer si alguna expresion de las que se ejecuto anteriormente tenia un 'Break'
            if (Common.Common.BreakFound) {
                //Comprobamos si 'Break == 0'
                // Ver si hace falta terminar la ejecucion del padre.
                runGenerator.Emit(OpCodes.Ldarg_0); //Cargamos 'this'
                runGenerator.Emit(OpCodes.Ldfld, BreakField); //Cargamos el campo 'Break' de la clase del Let
                runGenerator.Emit(OpCodes.Ldc_I4_0); //Anadimos el 0
                runGenerator.Emit(OpCodes.Beq, lblNoBreak);
                    //Si Break == 0, entonces no hace falta terminar. Saltar para NO_BREAK

                //Si hace falta terminar la ejecicion
                //Le asignamos el valor '0' al campo Break de esta clase
                runGenerator.Emit(OpCodes.Ldarg_0);
                runGenerator.Emit(OpCodes.Ldc_I4_0);
                runGenerator.Emit(OpCodes.Stfld, BreakField);

                //Ahora, vamos subiendo por los ParentInstance hasta llegar a uno que sea IBreakable (For, While o Let)
                //Garantizamos que siempre lleguemos, pues tambien lo chequeamos en la semantica del nodo Break.
                var expression = Parent;
                //Mientras no lleguemos a un nodo IBreakable
                while (!(expression is IBreakable)) {
                    expression = expression.Parent;
                }

                //Ya tenemos en 'expression' al IScopeDefiner padre. Lo que tenemos que hacer es poner su campo 'Break' en 1
                runGenerator.Emit(OpCodes.Ldarg_0); //Cargamos 'this'
                runGenerator.Emit(OpCodes.Ldfld, ParentInstance); //Metemos en la pila la referencia a ParentInstance
                runGenerator.Emit(OpCodes.Ldc_I4, 0); //Metemos en la pila el valor '1' que le asignaremos
                runGenerator.Emit(OpCodes.Stfld, ((IBreakable) expression).BreakField); //Hacemos la asignacion
            }

            //Terminamos el metodo principal
            runGenerator.Emit(OpCodes.Ret);
            //Creamos la clase que definimos para que pueda ser usada
            letTypeBuilder.CreateType();

            //Ahora, FUERA DE LA CLASE 'LET', instanciamos la clase Let que creamos, y llamamos al metodo principal!
            generator.Emit(OpCodes.Ldarg_0); //El constructor recibe como 1er argumento la instancia 'this'
            generator.Emit(OpCodes.Newobj, constructor); //Llamamos al constructor (y pone la referencia al nuevo tipo creado en la pila)
            generator.Emit(OpCodes.Callvirt, runMethodBuilder); //Llamamos al metodo principal

            //De la forma que lo diseñe, el valor de retorno (si hay) quedara en el tope de la pila
        }
Esempio n. 5
0
 /// <summary>
 /// Defines a nested type given its name..
 /// </summary>
 /// <param name="name">The short name of the type.</param>
 /// <returns>Returns the created <see cref="TypeBuilderHelper"/>.</returns>
 /// <seealso cref="System.Reflection.Emit.TypeBuilder.DefineNestedType(string)">
 /// TypeBuilder.DefineNestedType Method</seealso>
 public TypeBuilderHelper DefineNestedType(string name)
 {
     return(new TypeBuilderHelper(_assembly, _typeBuilder.DefineNestedType(name)));
 }
Esempio n. 6
0
        protected Type GrabType(Type Copy, TypeBuilder On)
        {
            if (Copy == null) return null;

            if (TypesDone.ContainsKey(Copy))
                return TypesDone[Copy];

            if (!Sources.Contains(Copy.Module))
                return TypeReplaceGenerics(Copy);

            if (Copy.IsByRef)
                return GrabType(Copy.GetElementType()).MakeByRefType();

            if (Copy.IsArray)
                return GrabType(Copy.GetElementType()).MakeArrayType();

            if (On == null)
                On = GrabType(Copy.DeclaringType) as TypeBuilder;

            var OurInterfaces = new List<Type>();
            Type[] Interfaces;

            if (!Copy.IsEnum)
            {
                // To not specify an interface implementation if one of the parent types
                // already implements one of the interfaces returned by GetInterfaces.
                foreach (Type T in Copy.GetInterfaces())
                {
                    if (!T.IsAssignableFrom(Copy.BaseType))
                        OurInterfaces.Add(T);
                }
                Interfaces = OurInterfaces.ToArray();
            }
            else Interfaces = null;

            TypeBuilder Ret;
            if (On == null) Ret = Module.DefineType(Copy.Name, Copy.Attributes, GrabType(Copy.BaseType), Interfaces);
            else Ret = On.DefineNestedType(Copy.Name, Copy.Attributes, GrabType(Copy.BaseType), Interfaces);

            TypesDone.Add(Copy, Ret);

            // We need to copy over the static constructor explicitly, because it is never called in the IL
            ConstructorInfo StaticConstr = FindStaticConstructor(Copy);
            if (StaticConstr != null)
                GrabConstructor(StaticConstr, Ret);

            // Enum fields need to be copied over on .NET to avoid a TypeLoadException
            // Interestingly, enum types without fields are perfectly fine with mono.
            if (Copy.IsEnum)
            {
                GrabField(Copy.GetField("value__"), Ret);

                foreach (FieldInfo Field in Copy.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    if (Field.DeclaringType != Copy) continue;
                    GrabField(Field);
                }
            }

            // - If we are copying over a delegate, we need to guarantee that all members are copied over,
            //   if not we'll cause a runtime error somewhere along the pipeline (for example: mono fails
            //   on an assertion).
            // - If we are copying over a class with an abstract parent, we need to copy over all methods
            //   to prevent a TypeLoadException at runtime (non-abstract types containing methods without
            //   a body cause this)
            // Delegates have a native-code constructor that is needed, too
            if (Copy.BaseType == typeof(MulticastDelegate))
            {
                ConstructorInfo NativeCtor = Copy.GetConstructors()[0];
                GrabConstructor(NativeCtor, Ret);

                GrabMethod(Copy.GetMethod("Invoke"), Ret);
                GrabMethod(Copy.GetMethod("BeginInvoke"), Ret);
                GrabMethod(Copy.GetMethod("EndInvoke"), Ret);
            }
            else if (Copy.IsExplicitLayout || Copy.BaseType.IsAbstract || OurInterfaces.Count > 0)
            {
                foreach (MethodInfo Method in Copy.GetMethods())
                {
                    if (Method.DeclaringType != Copy) continue;
                    GrabMethod(Method, Ret);
                }

                foreach (MethodInfo Method in Copy.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    if (Method.DeclaringType != Copy) continue;
                    GrabMethod(Method, Ret);
                }

                foreach (FieldInfo Field in Copy.GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
                {
                    if (Field.DeclaringType != Copy) continue;
                    GrabField(Field, Ret);
                }
            }

            if (Copy.IsEnum || Copy.BaseType == typeof(MulticastDelegate))
                Ret.CreateType();

            return Ret;
        }
        private void DeserializeNested(TypeBuilder tb)
        {
            PushState();

            DeserializeCore(
                tb.DefineNestedType(
                    Name(m_br.ReadString()),
                        (TypeAttributes)m_br.ReadUInt32(),
                        ReadType()));

            PopState();
        }
Esempio n. 8
0
		public static TypeBuilder DefineNestedDelegate (TypeBuilder typeBuilder, String delegateName, Type returnType, Type[] parameterTypes, String[] parameterNames, out ConstructorBuilder constructorBuilder)
		{
			// Parameter types for BeginInvoker are: parameterTypes + AsyncCallback + Object
			Type[] beginInvokeParameterTypes = ArrayHelper.Append (parameterTypes, typeof(AsyncCallback), typeof(Object));
			
			// Put names on parameters to ease debugging
			String[] beginInvokeParameterNames = new String[beginInvokeParameterTypes.Length];
			Array.Copy (parameterNames, beginInvokeParameterNames, parameterNames.Length);
			beginInvokeParameterNames [parameterTypes.Length] = "callback";
			beginInvokeParameterNames [parameterTypes.Length + 1] = "object";
			
			// Create the nested type
			TypeBuilder delegateBuilder = typeBuilder.DefineNestedType (delegateName, EmitConstants.PUBLIC_NESTED_TYPE, typeof(MulticastDelegate));
			
			constructorBuilder = DefineConstructor (delegateBuilder, MethodImplAttributes.Runtime, new[] {
				typeof(Object),
				typeof(IntPtr)
			}, new[] {
				"object",
				"method"
			});
			DefineOverrideMethod (delegateBuilder, MethodImplAttributes.Runtime, "Invoke", returnType, parameterTypes, parameterNames);
			DefineOverrideMethod (delegateBuilder, MethodImplAttributes.Runtime, "BeginInvoke", typeof(IAsyncResult), beginInvokeParameterTypes, beginInvokeParameterNames);
			DefineOverrideMethod (delegateBuilder, MethodImplAttributes.Runtime, "EndInvoke", returnType, new[] {typeof(IAsyncResult)}, new[] {"result"});
			
			return delegateBuilder;
		}
Esempio n. 9
0
        /*private class RefsAndArraysBuilderEx : RefsAndArraysBuilder
        {
            private MetaDataMapper mapper;

            public RefsAndArraysBuilderEx(MetaDataMapper mapper)
            {
                this.mapper = mapper;
            }

            public override Type BuildRefType(Type type)
            {
                return( mapper.Map(TypeEx.BuildRefType(type)) );
            }

            public override Type BuildArrayType(Type type)
            {
                return( mapper.Map(TypeEx.BuildArrayType(type)) );
            }
        }*/
        private static Type GetOrBuild(TypeBuilder cilpe, ArrayList list, int i)
        {
            if(list.Count > i)
                return(list[i] as Type);
            if(list.Count == i)
            {
                string name = "$CILPE$PseudoParameter_" + i;
                TypeBuilder type = cilpe.DefineNestedType(name, TypeAttributes.NestedAssembly | TypeAttributes.Class);
                list.Add(type);
                return(type);
            }
            throw new ExportException();
        }
Esempio n. 10
0
            public ASTNodeVisitor_JITCompiler(TypeBuilder parentTypeBuilder, ASTNode_Lambda node)
            {
                mLambdaNode = node;
                TypeBuilder = parentTypeBuilder.DefineNestedType(JITInterpreter_DS2.Instance().GenernateUniqueString("closure"), TypeAttributes.NestedPublic);

                ConstructorBuilder = TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

                FieldBuilders = new Dictionary<FreeAddress, FieldBuilder>();
                foreach (var address in mLambdaNode.GetFreeAddresses()) {
                    FieldBuilders[address] = TypeBuilder.DefineField(address.ToString(), typeof(SharedValue), FieldAttributes.Public);
                }

                if (HasThisArgument()) {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                } else {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }

                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareLocals();

                mTaillCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTaillCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);

                TypeBuilder.CreateType();
            }
Esempio n. 11
0
 public Class(Assembly asm, string ns, string name, bool pub, TypeBuilder parent)
 {
     Name = name;
     if (parent != null)
     {
         FullName = name;
         Access = pub ? TypeAttributes.NestedPublic : TypeAttributes.NestedPrivate;
         TypeBuilder = parent.DefineNestedType(FullName, Access);
     }
     else
     {
         FullName = ns + "." + name;
         Access = pub ? TypeAttributes.Public : TypeAttributes.NotPublic;
         TypeBuilder = asm.ModuleBuilder.DefineType(FullName, Access);
     }
 }
Esempio n. 12
0
        protected static void ParseClass(XElement el, TypeBuilder parentType = null)
        {
            string ClassName = el.Attribute("Name").Value;
            string Namespace = "";
            if (el.Attribute("Namespace") != null)
                Namespace = el.Attribute("Namespace").Value;

            if (Namespace.Length > 0 && !Namespace.EndsWith("."))
                Namespace += ".";

            TypeBuilder newType = null;
            if (parentType != null)
            {
                try
                {
                    newType = parentType.DefineNestedType(ClassName, TypeAttributes.NestedPublic | TypeAttributes.Class);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.ToString());
                }
            }
            else
            {
                newType = moduleBuilder.DefineType(Namespace + ClassName, TypeAttributes.Public | TypeAttributes.Class);
            }

            Type baseType = null;
            if (el.Attribute("Base") != null)
            {
                baseType = GetType(el.Attribute("Base").Value);
            }
            if (baseType == null)
                baseType = typeof(ModAPI.Data.Models.BaseXMLProvider);
            newType.SetParent(baseType);
            Debug.Log("DynamicTypes", "Parsing dynamic type \"" + newType.FullName + "\".");
            BuildingTypes.Add(newType.FullName, newType);
            foreach (XElement subClass in el.Elements("Class"))
            {
                ParseClass(subClass, newType);
            }

            foreach (XElement propertyEl in el.Elements("Property"))
            {
                string propertyName = propertyEl.Attribute("Name").Value;
                string typeName = propertyEl.Attribute("Type").Value;
                Type propertyType = GetType(typeName);
                if (!ParseProperty(newType, propertyType, typeName, propertyName))
                {
                    if (!typeWaiting.ContainsKey(newType.FullName))
                        typeWaiting.Add(newType.FullName, 0);
                    typeWaiting[newType.FullName]++;
                }
            }

            foreach (XElement fieldEl in el.Elements("Field"))
            {
                string fieldName = fieldEl.Attribute("Name").Value;
                string typeName = fieldEl.Attribute("Type").Value;
                Type fieldType = GetType(typeName);
                if (!ParseField(newType, fieldType, typeName, fieldName))
                {
                    if (!typeWaiting.ContainsKey(newType.FullName))
                        typeWaiting.Add(newType.FullName, 0);
                    typeWaiting[newType.FullName]++;
                }
            }

            if (!typeWaiting.ContainsKey(newType.FullName))
            {
                TypeComplete(newType);
            }
            else
            {
                Debug.Log("DynamicTypes", "Dynamic type \"" + newType.FullName + "\" is waiting for completion.");
            }
        }
Esempio n. 13
0
 public override void Gen( TypeBuilder program, MethodBuilder method )
 {
     AstNode typeclass = this.child;
     var tc = program.DefineNestedType( typeclass.symbol, TypeAttributes.NestedPublic );
     EnviromentList.Instance.LoopupSymbol( typeclass.symbol ).typeBuilder = tc;
     AstNode sibling = typeclass.sibling;
     while (sibling != null) {
         var subType = program.DefineNestedType( sibling.symbol, TypeAttributes.NestedPublic, tc );
         EnviromentList.Instance.LoopupSymbol( sibling.symbol ).typeBuilder = subType;
         var cc  = subType.DefineConstructor( MethodAttributes.Public,
              CallingConventions.Standard, new Type[0] );
         EnviromentList.Instance.LoopupSymbol( sibling.symbol ).Constructor = cc;
         cc.GetILGenerator().Emit( OpCodes.Ret );
         sibling = sibling.sibling;
     }
 }
Esempio n. 14
0
            public ASTNodeVisitor_JITCompiler(ASTNodeVisitor_JITCompiler parent, TypeBuilder envTypeBuilder, ASTNode_Lambda node)
            {
                mParent = parent;
                mEnvTypeBuilder = envTypeBuilder;
                mLambdaNode = node;

                mHeapEnvTypeBuilder = envTypeBuilder.DefineNestedType(JITInterpreter_DS.Instance().GenernateUniqueString("nested_class"));

                if (HasThisArgument()) {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("method"),
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                } else {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("static_method"),
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }

                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareArguments();
                DeclareLocals();
                EmitInitHeapEnv();

                mTailCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTailCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);
                mHeapEnvTypeBuilder.CreateType();
            }
Esempio n. 15
0
 private Type BuildDelegate(TypeBuilder type, MethodInfo method, Hashtable ctors, Hashtable invocations)
 {
     TypeBuilder d = type.DefineNestedType(method.Name + "Handler",
         TypeAttributes.Class | TypeAttributes.NestedPrivate | TypeAttributes.Sealed,
         typeof(System.MulticastDelegate));
     ParameterInfo[] parameters = method.GetParameters();
     Type[] paramTypes1 = new Type[parameters.Length];
     Type[] paramTypes2 = new Type[parameters.Length + 2];
     for (int i = 0; i < parameters.Length; i++)
     {
         paramTypes1[i] = parameters[i].ParameterType;
         paramTypes2[i] = parameters[i].ParameterType;
     }
     paramTypes2[parameters.Length] = typeof(AsyncCallback);
     paramTypes2[parameters.Length + 1] = typeof(object);
     MethodAttributes theAttr = MethodAttributes.Public | MethodAttributes.HideBySig
         | MethodAttributes.NewSlot | MethodAttributes.Virtual;
     MethodImplAttributes theImplAttr = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
     ConstructorBuilder ctor = d.DefineConstructor(MethodAttributes.Public,
         CallingConventions.Standard, new Type[] { typeof(object), typeof(IntPtr) });
     ctor.SetImplementationFlags(theImplAttr);
     ctors.Add(method.Name, ctor);
     d.DefineMethod("Invoke", theAttr, method.ReturnType, paramTypes1).SetImplementationFlags(theImplAttr);
     MethodBuilder invocation = d.DefineMethod("BeginInvoke", theAttr, typeof(IAsyncResult), paramTypes2);
     invocation.SetImplementationFlags(theImplAttr);
     invocations.Add(method.Name, invocation);
     d.DefineMethod("EndInvoke", theAttr, method.ReturnType,
         new Type[] { typeof(IAsyncResult) }).SetImplementationFlags(theImplAttr);
     return d;
 }