GetILGenerator() public method

public GetILGenerator ( ) : System.Reflection.Emit.ILGenerator
return System.Reflection.Emit.ILGenerator
Esempio n. 1
0
        public void EmitConstrucorIL(ConstructorBuilder builder)
        {
            ConstructorInfo conObj = typeof(object).GetConstructor(new Type[0]);

            //call constructor of base object
            ILGenerator il = builder.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Call, conObj);
            il.Emit(OpCodes.Ret);
        }
 protected ILGenerator GetInitConstructorIL(ConstructorBuilder constructorBuilder, Dictionary<TypeFactoryMap, FieldBuilder> singletonFields)
 {
     var constrIl = constructorBuilder.GetILGenerator();
     constrIl.Emit(OpCodes.Ldarg_0);
     constrIl.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
     foreach (var fieldPair in singletonFields)
     {
         var injectableGetSingletonMethod = typeof(IInjectableModule).GetMethod("GetSingleton").MakeGenericMethod(fieldPair.Key.BaseType);
         constrIl.Emit(OpCodes.Ldarg_0);
         constrIl.Emit(OpCodes.Ldarg_1);
         constrIl.Emit(OpCodes.Ldstr, fieldPair.Key.Name);
         constrIl.Emit(OpCodes.Callvirt, injectableGetSingletonMethod);
         constrIl.Emit(OpCodes.Stfld, fieldPair.Value);
     }
     return constrIl;
 }
        public void Execute()
        {
            constructorBuilder = builder().TypeBuilder.DefineConstructor(
                MethodAttributes.Public |
                MethodAttributes.SpecialName |
                MethodAttributes.RTSpecialName,
                CallingConventions.Standard,
                new Type[0]);

            this.constructor = typeof(object).GetConstructor(new Type[0]);

            ILGenerator il = constructorBuilder.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Call, constructor);
            il.Emit(OpCodes.Ret);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a ConstructorBuilder for current constructor entity.
        /// </summary>
        public override void PrepareSelf()
        {
            // todo: remove when we support static ctors
            if(IsStatic)
                throw new LensCompilerException(CompilerMessages.ConstructorStatic);

            if (ConstructorBuilder != null || IsImported)
                return;

            var ctx = ContainerType.Context;

            if (ArgumentTypes == null)
                ArgumentTypes = Arguments == null
                    ? new Type[0]
                    : Arguments.Values.Select(fa => fa.GetArgumentType(ctx)).ToArray();

            ConstructorBuilder = ContainerType.TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, ArgumentTypes);
            Generator = ConstructorBuilder.GetILGenerator(Context.ILStreamSize);
        }
        protected internal override void OnCompile()
        {
            Builder = this.TargetClass.Builder.DefineConstructor(this.Attributes
                , this.CallingConvention, this.ParameterTypes
                );
            ILGenerator il = SetILGenerator(Builder.GetILGenerator());
            try
            {
                CompileParameters(Builder);
                CompileLocals(il);

                ILHelper msil = new ILHelper(il);
                base.EmitInstructions(msil);
                msil.Return();
            }
            finally
            {
                SetILGenerator(il);
            }
        }
 private void EmitCtor(PropertyAndField[] properties, ConstructorBuilder method)
 {
     ILGenerator iLGenerator = method.GetILGenerator();
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Call, ObjectType.GetConstructor(Type.EmptyTypes));
     int index = 0;
     if (0 < properties.Length)
     {
         do
         {
             iLGenerator.Emit(OpCodes.Ldarg_0);
             int arg = index + 1;
             iLGenerator.Emit(OpCodes.Ldarg, arg);
             iLGenerator.Emit(OpCodes.Stfld, properties[index].Field);
             index = arg;
         }
         while (index < properties.Length);
     }
     iLGenerator.Emit(OpCodes.Ret);
 }
		void SetUp (AssemblyBuilderAccess access)
		{
			AssemblyName assemblyName = new AssemblyName ();
			assemblyName.Name = ASSEMBLY_NAME;

			assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
				assemblyName, access,
				Path.GetTempPath ());

			module = assembly.DefineDynamicModule ("module1");

			tb = module.DefineType ("Bar");
			GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");

			cb = tb.DefineConstructor (MethodAttributes.Public,
				CallingConventions.Standard,
				new Type [] { typeof (string), typeof (int) });
			ILGenerator ig = cb.GetILGenerator ();
			ig.Emit (OpCodes.Ret);

			typeBarOfInt32 = tb.MakeGenericType (typeof (int));
			ci = TypeBuilder.GetConstructor (typeBarOfInt32, cb);
		}
Esempio n. 8
0
 /// <summary>
 /// Constructs an Emitter using the specified ConstructorBuilder.
 /// </summary>
 /// <param name="cntb">ConstructorBuilder</param>
 public Emitter(ConstructorBuilder cntb)
 {
     if (cntb == null) {
         throw new ArgumentNullException("cntb");
     }
     _il = cntb.GetILGenerator();
     _temp = new Collection<LocalDescriptor>();
     _code = new Collection<Instruction>();
 }
Esempio n. 9
0
        public Type Compile()
        {
            TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name,
                TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed);

            var genericParameters = declaration.TypeParameters.Any() ? TypeBuilder.DefineGenericParameters(declaration.TypeParameters) : Type.EmptyTypes;

            var containedType = new TypeConverter(runtimeContainer, genericParameters).Convert(declaration.Type);

            field = TypeBuilder.DefineField(Constants.BoxedTypeValueFieldName, containedType, FieldAttributes.Public);

            constructor = TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { containedType });

            var cataCtorBody = constructor.GetILGenerator();
            cataCtorBody.Emit(OpCodes.Ldarg_0);
            cataCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            cataCtorBody.Emit(OpCodes.Ldarg_0);
            cataCtorBody.Emit(OpCodes.Ldarg_1);
            cataCtorBody.Emit(OpCodes.Stfld, field);
            cataCtorBody.Emit(OpCodes.Ret);

            MethodsType = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name + Constants.MethodsSuffix,
                TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract);

            CompileBoxFunction();
            CompileUnboxFunction();

            TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(SynonymAttribute).GetConstructors()[0],
                new object[]
                {
                    declaration.ConstructorFunctionName,
                    declaration.DestructorFunctionName,
                    UnboxFunction
                }));

            return TypeBuilder;
        }
Esempio n. 10
0
        private void CompileUnboxFunction()
        {
            UnboxFunction = MethodsType.DefineNestedType(Constants.UnboxFunctionClassName,
               TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.Class,
               null, Type.EmptyTypes);

            var genericParameters = declaration.TypeParameters.Any() ? UnboxFunction.DefineGenericParameters(declaration.TypeParameters) : Type.EmptyTypes;

            var containedType = new TypeConverter(runtimeContainer, genericParameters).Convert(declaration.Type);

            UnboxFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(TypeBuilder, containedType));

            UnboxFunctionConstructor = UnboxFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
            var ctorBody = UnboxFunctionConstructor.GetILGenerator();
            ctorBody.Emit(OpCodes.Ldarg_0);
            ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            ctorBody.Emit(OpCodes.Ret);

            var call = UnboxFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual,
                containedType, new Type[] { TypeBuilder });

            var callBody = call.GetILGenerator();
            callBody.Emit(OpCodes.Ldarg_1);
            callBody.Emit(OpCodes.Ldfld, declaration.TypeParameters.Any()
                ? TypeBuilder.GetField(TypeBuilder.MakeGenericType(genericParameters), field)
                : field);
            callBody.Emit(OpCodes.Ret);
        }
Esempio n. 11
0
        // CodeDom生成 
        //public void CreateClass(Type defineType, string className, Dictionary<string, Type> dicProperty)
        //{
        //    // 命名空间
        //    var nameSpace = new CodeNamespace(defineType.Namespace);
        //    // 定义一个新类
        //    var sortDeleteClass = new CodeTypeDeclaration(className)
        //    {
        //        IsClass = true,
        //        Attributes = MemberAttributes.Public
        //    };
        //    foreach (var keyValue in dicProperty)
        //    {
        //        var field = new CodeMemberField(keyValue.Value, "_" + keyValue.Key) { Attributes = MemberAttributes.Private };
        //        var property = new CodeMemberProperty()
        //        {
        //            Name = keyValue.Key,
        //            Type = new CodeTypeReference(keyValue.Value),
        //            Attributes = MemberAttributes.Private,
        //        };
        //        // 定义Get
        //        property.GetStatements.Add(new CodeMethodReturnStatement(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
        //        // 定义Set
        //        property.SetStatements.Add(new CodeMethodReturnStatement(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
        //        // 添加字段
        //        sortDeleteClass.Members.Add(field);
        //        // 添加属性
        //        sortDeleteClass.Members.Add(property);
        //    }
        //    nameSpace.Types.Add(sortDeleteClass);
        //    var cpr = CodeDomProvider.CreateProvider("C#");
        //    cpr.CompileAssemblyFromDom();
        //}

        /// <summary>
        ///     定义方法体
        /// </summary>
        /// <param name="ctor">构造函数生成器</param>
        private static void IlGenerator(ConstructorBuilder ctor)
        {
            var il = ctor.GetILGenerator();
            il.Emit(OpCodes.Ldarg);
            //il.Emit(OpCodes.Ldarg_0);
            //il.Emit(OpCodes.Ldarg_1);
            //il.Emit(OpCodes.Stfld, myGreetingField);
            il.Emit(OpCodes.Ret);
        }
Esempio n. 12
0
 public CodeGen GetOrMakeInitializer()
 {
     if (initializer == null) {
         initializer = myType.DefineTypeInitializer();
         initGen = new CodeGen(this, initializer, initializer.GetILGenerator(), Type.EmptyTypes);
     }
     return initGen;
 }
Esempio n. 13
0
 public ConstructorBuilder(System.Reflection.Emit.ConstructorBuilder builder)
 {
     _builder = builder;
     IL       = builder.GetILGenerator();
 }
Esempio n. 14
0
        private void CompileOutClass()
        {
            OutClass = utilityClass.DefineNestedType(Constants.OutClassName,
               TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
               null, new[] { TypeBuilder });

            var genericParameters = declaration.TypeParameters.Any() ? OutClass.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes;

            var fLeastFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, TypeBuilder, genericParameters, runtimeContainer);

            var predField = OutClass.DefineField(Constants.OutClassPredFieldName, fLeastFixedPoint, FieldAttributes.Private);

            OutClassConstructor = OutClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { fLeastFixedPoint });

            var outCtorBody = OutClassConstructor.GetILGenerator();
            outCtorBody.Emit(OpCodes.Ldarg_0);
            outCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            outCtorBody.Emit(OpCodes.Ldarg_0);
            outCtorBody.Emit(OpCodes.Ldarg_1);
            outCtorBody.Emit(OpCodes.Stfld, predField);
            outCtorBody.Emit(OpCodes.Ret);

            var cata = OutClass.DefineMethod(Constants.CataMethodName, MethodAttributes.Public | MethodAttributes.Virtual);

            var genericParameter = cata.DefineGenericParameters(Constants.CataMethodGenericParameterName)[0];

            var fGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameter, genericParameters, runtimeContainer);

            cata.SetParameters(typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter));
            cata.SetReturnType(genericParameter);

            var cataBody = cata.GetILGenerator();
            cataBody.Emit(OpCodes.Ldarg_1);
            cataBody.Emit(OpCodes.Ldarg_1);
            cataBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(CataFunction.MakeGenericType(new[] { genericParameter }.Concat(genericParameters).ToArray()),
                CataFunctionConstructor));
            cataBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { TypeBuilder, genericParameter }.Concat(genericParameters).ToArray()));
            cataBody.Emit(OpCodes.Ldarg_0);
            cataBody.Emit(OpCodes.Ldfld, predField);
            cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
                typeof(IFunction<,>).MakeGenericType(fLeastFixedPoint, fGenericParameter),
                typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
            cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
                typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter),
                typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
            cataBody.Emit(OpCodes.Ret);
        }
Esempio n. 15
0
        private void CompileCataFunction()
        {
            CataFunction = utilityClass.DefineNestedType(Constants.CataFunctionClassName,
                           TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
                           null, TypeBuilder.EmptyTypes);

            var genericParameters = CataFunction.DefineGenericParameters(new[] { Constants.CataFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray());

            var inputParameter = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.Skip(1).ToArray()) : TypeBuilder;

            CataFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(inputParameter, genericParameters[0]));

            var fSeedType = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameters[0], genericParameters, runtimeContainer);

            var algebra = typeof(IFunction<,>).MakeGenericType(fSeedType, genericParameters[0]);

            var seedField = CataFunction.DefineField(Constants.CataFunctionClassSeedFieldName, algebra, FieldAttributes.Private);

            CataFunctionConstructor = CataFunction.DefineConstructor(MethodAttributes.Public,
                CallingConventions.Standard, new[] { algebra });

            var cataCtorBody = CataFunctionConstructor.GetILGenerator();
            cataCtorBody.Emit(OpCodes.Ldarg_0);
            cataCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            cataCtorBody.Emit(OpCodes.Ldarg_0);
            cataCtorBody.Emit(OpCodes.Ldarg_1);
            cataCtorBody.Emit(OpCodes.Stfld, seedField);
            cataCtorBody.Emit(OpCodes.Ret);

            var call = CataFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual,
                genericParameters[0], new Type[] { inputParameter });

            var callBody = call.GetILGenerator();
            callBody.Emit(OpCodes.Ldarg_1);
            callBody.Emit(OpCodes.Ldarg_0);
            callBody.Emit(OpCodes.Ldfld, seedField);
            callBody.Emit(OpCodes.Callvirt, genericParameters.Skip(1).Any()
                ? TypeBuilder.GetMethod(
                    TypeBuilder.MakeGenericType(genericParameters.Skip(1).ToArray()),
                    Cata).MakeGenericMethod(genericParameters[0])
                : Cata.MakeGenericMethod(genericParameters[0]));
            callBody.Emit(OpCodes.Ret);
        }
Esempio n. 16
0
        /// <summary>
        /// Generate the declaration for the IgnoresAccessChecksToAttribute type.
        /// This attribute will be both defined and used in the dynamic assembly.
        /// Each usage identifies the name of the assembly containing non-public
        /// types the dynamic assembly needs to access.  Normally those types
        /// would be inaccessible, but this attribute allows them to be visible.
        /// It works like a reverse InternalsVisibleToAttribute.
        /// This method returns the ConstructorInfo of the generated attribute.
        /// </summary>
        public static ConstructorInfo AddToModule(ModuleBuilder mb)
        {
            TypeBuilder attributeTypeBuilder =
                mb.DefineType("System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute",
                              TypeAttributes.Public | TypeAttributes.Class,
                              typeof(Attribute));

            // Create backing field as:
            // private string assemblyName;
            FieldBuilder assemblyNameField =
                attributeTypeBuilder.DefineField("assemblyName", typeof(string), FieldAttributes.Private);

            // Create ctor as:
            // public IgnoresAccessChecksToAttribute(string)
            ConstructorBuilder constructorBuilder = attributeTypeBuilder.DefineConstructor(MethodAttributes.Public,
                                                                                           CallingConventions.HasThis,
                                                                                           new Type[] { assemblyNameField.FieldType });

            ILGenerator il = constructorBuilder.GetILGenerator();

            // Create ctor body as:
            // this.assemblyName = {ctor parameter 0}
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg, 1);
            il.Emit(OpCodes.Stfld, assemblyNameField);

            // return
            il.Emit(OpCodes.Ret);

            // Define property as:
            // public string AssemblyName {get { return this.assemblyName; } }
            _ = attributeTypeBuilder.DefineProperty(
                "AssemblyName",
                PropertyAttributes.None,
                CallingConventions.HasThis,
                returnType: typeof(string),
                parameterTypes: null);

            MethodBuilder getterMethodBuilder = attributeTypeBuilder.DefineMethod(
                "get_AssemblyName",
                MethodAttributes.Public,
                CallingConventions.HasThis,
                returnType: typeof(string),
                parameterTypes: null);

            // Generate body:
            // return this.assemblyName;
            il = getterMethodBuilder.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, assemblyNameField);
            il.Emit(OpCodes.Ret);

            // Generate the AttributeUsage attribute for this attribute type:
            // [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
            TypeInfo attributeUsageTypeInfo = typeof(AttributeUsageAttribute).GetTypeInfo();

            // Find the ctor that takes only AttributeTargets
            ConstructorInfo attributeUsageConstructorInfo =
                attributeUsageTypeInfo.DeclaredConstructors
                .Single(c => c.GetParameters().Length == 1 &&
                        c.GetParameters()[0].ParameterType == typeof(AttributeTargets));

            // Find the property to set AllowMultiple
            PropertyInfo allowMultipleProperty =
                attributeUsageTypeInfo.DeclaredProperties
                .Single(f => string.Equals(f.Name, "AllowMultiple"));

            // Create a builder to construct the instance via the ctor and property
            CustomAttributeBuilder customAttributeBuilder =
                new CustomAttributeBuilder(attributeUsageConstructorInfo,
                                           new object[] { AttributeTargets.Assembly },
                                           new PropertyInfo[] { allowMultipleProperty },
                                           new object[] { true });

            // Attach this attribute instance to the newly defined attribute type
            attributeTypeBuilder.SetCustomAttribute(customAttributeBuilder);

            // Make the TypeInfo real so the constructor can be used.
            return(attributeTypeBuilder.CreateTypeInfo() !.DeclaredConstructors.Single());
        }
Esempio n. 17
0
 void CopyMethodBody(MethodBase Base, ConstructorBuilder Builder)
 {
     if(HasBody(Base))
         CopyMethodBody(Base, Builder.GetILGenerator());
 }
Esempio n. 18
0
        private void CompileAnaClass()
        {
            AnaClass = utilityClass.DefineNestedType(Constants.AnaClassName,
                            TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
                            null, TypeBuilder.EmptyTypes);

            var anaClassGenericParameters = AnaClass.DefineGenericParameters(new[] { Constants.AnaClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray());

            if (declaration.TypeParameters.Any())
            {
                AnaClass.AddInterfaceImplementation(TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray()));
            }
            else
            {
                AnaClass.AddInterfaceImplementation(TypeBuilder);
            }

            var anaClassGeneratorType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0],
                FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer));

            var seedField = AnaClass.DefineField(Constants.AnaClassSeedFieldName, anaClassGenericParameters[0], FieldAttributes.Private);
            var generatorField = AnaClass.DefineField(Constants.AnaClassGeneratorFieldName, anaClassGeneratorType, FieldAttributes.Private);

            AnaClassConstructor = AnaClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { anaClassGenericParameters[0], anaClassGeneratorType });
            var ctorBody = AnaClassConstructor.GetILGenerator();
            ctorBody.Emit(OpCodes.Ldarg_0);
            ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            ctorBody.Emit(OpCodes.Ldarg_0);
            ctorBody.Emit(OpCodes.Ldarg_1);
            ctorBody.Emit(OpCodes.Stfld, seedField);
            ctorBody.Emit(OpCodes.Ldarg_0);
            ctorBody.Emit(OpCodes.Ldarg_2);
            ctorBody.Emit(OpCodes.Stfld, generatorField);
            ctorBody.Emit(OpCodes.Ret);

            var apply = AnaClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual);

            var resultType = apply.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0];

            var applyFunctionGenericParameters = new[] { resultType }.Concat(anaClassGenericParameters.Skip(1)).ToArray();

            apply.SetParameters(GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters));
            apply.SetReturnType(resultType);

            var applyMethodGenericClass = TypeBuilder.GetMethod(
               GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters),
               GreatestFixedPointFunctionApplyMethod);
            var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(anaClassGenericParameters[0]);

            var applyBody = apply.GetILGenerator();
            applyBody.Emit(OpCodes.Ldarg_1);
            applyBody.Emit(OpCodes.Ldarg_0);
            applyBody.Emit(OpCodes.Ldfld, seedField);
            applyBody.Emit(OpCodes.Ldarg_0);
            applyBody.Emit(OpCodes.Ldfld, generatorField);
            applyBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod);
            applyBody.Emit(OpCodes.Ret);
        }
Esempio n. 19
0
		/// <summary>
		///		Initializes a new instance of the <see cref="TracingILGenerator"/> class.
		/// </summary>
		/// <param name="constructorBuilder">The constructor builder.</param>
		/// <param name="traceWriter">The trace writer.</param>
		/// <param name="isDebuggable"><c>true</c> if the underlying builders are debuggable; othersie <c>false</c>.</param>
		public TracingILGenerator( ConstructorBuilder constructorBuilder, TextWriter traceWriter, bool isDebuggable )
			: this( constructorBuilder != null ? constructorBuilder.GetILGenerator() : null, false, traceWriter, isDebuggable )
		{
			Contract.Assert( constructorBuilder != null );
		}
Esempio n. 20
0
        private void CompileAnaFunction()
        {
            AnaFunction = utilityClass.DefineNestedType(Constants.AnaFunctionClassName,
                           TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
                           null, TypeBuilder.EmptyTypes);

            var anaClassGenericParameters = AnaFunction.DefineGenericParameters(new[] { Constants.AnaFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray());

            var resultType = declaration.TypeParameters.Any()
                ? TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray())
                : TypeBuilder;

            AnaFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0],
                 resultType));

            var seedType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0],
                FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer));

            var seedField = AnaFunction.DefineField(Constants.AnaFunctionClassSeedFieldName, seedType, FieldAttributes.Private);

            AnaFunctionConstructor = AnaFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { seedType });

            var anaCtorBody = AnaFunctionConstructor.GetILGenerator();
            anaCtorBody.Emit(OpCodes.Ldarg_0);
            anaCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            anaCtorBody.Emit(OpCodes.Ldarg_0);
            anaCtorBody.Emit(OpCodes.Ldarg_1);
            anaCtorBody.Emit(OpCodes.Stfld, seedField);
            anaCtorBody.Emit(OpCodes.Ret);

            var call = AnaFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual,
                resultType, new[] { anaClassGenericParameters[0] });

            var callBody = call.GetILGenerator();
            callBody.Emit(OpCodes.Ldarg_1);
            callBody.Emit(OpCodes.Ldarg_0);
            callBody.Emit(OpCodes.Ldfld, seedField);
            callBody.Emit(OpCodes.Call, Ana.MakeGenericMethod(anaClassGenericParameters));
            callBody.Emit(OpCodes.Ret);
        }
Esempio n. 21
0
		/// <summary>
		/// Creates a new <see cref="ILEmitter"/> for a given <see cref="ConstructorBuilder"/>.
		/// </summary>
		/// <param name="constructorBuilder">The <see cref="ConstructorBuilder"/> to emit to.</param>
		public ILEmitter(ConstructorBuilder/*!*/ constructorBuilder)
			: this(constructorBuilder.GetILGenerator(), Containers.ConstructorBuilder)
		{
			this.method = constructorBuilder;
		}
Esempio n. 22
0
        private void CompileInGeneratingFunction()
        {
            InClass = utilityClass.DefineNestedType(Constants.InGeneratingFunctionClassName,
                TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic);

            var genericParameters = declaration.TypeParameters.Any() ? InClass.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes;

            var greatestFixedPoint = declaration.TypeParameters.Any()
                ? TypeBuilder.MakeGenericType(genericParameters)
                : TypeBuilder;

            var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, greatestFixedPoint, genericParameters, runtimeContainer);

            InClass.AddInterfaceImplementation(GreatestFixedPointFunction.MakeGenericType(new[] { fGreatestFixedPoint }.Concat(genericParameters).ToArray()));

            InGeneratingFunctionConstructor = InClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, TypeBuilder.EmptyTypes);
            var inCtorBody = InGeneratingFunctionConstructor.GetILGenerator();
            inCtorBody.Emit(OpCodes.Ldarg_0);
            inCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
            inCtorBody.Emit(OpCodes.Ret);

            var apply = InClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual,
                fGreatestFixedPoint, TypeBuilder.EmptyTypes);

            var genericParameter = apply.DefineGenericParameters(Constants.ApplyMethodGenericParameterName)[0];

            var fGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameter, genericParameters, runtimeContainer);

            apply.SetParameters(genericParameter, typeof(IFunction<,>).MakeGenericType(genericParameter, fGenericParameter));

            var applyBody = apply.GetILGenerator();
            applyBody.Emit(OpCodes.Ldarg_2);
            applyBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(
               AnaFunction.MakeGenericType(new[] { genericParameter }.Concat(genericParameters).ToArray()),
               AnaFunctionConstructor));
            applyBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { genericParameter, greatestFixedPoint }.Concat(genericParameters).ToArray()));
            applyBody.Emit(OpCodes.Ldarg_2);
            applyBody.Emit(OpCodes.Ldarg_1);
            applyBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
                typeof(IFunction<,>).MakeGenericType(genericParameter, fGenericParameter),
                typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
            applyBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
                typeof(IFunction<,>).MakeGenericType(fGenericParameter, fGreatestFixedPoint),
                typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
            applyBody.Emit(OpCodes.Ret);
        }