private Emit.MethodBuilder GenerateMethod(Emit.MethodBuilder methb, MethodDeclaration method) { this.il = methb.GetILGenerator(); this.symbolTable = new Collections.Dictionary <string, Emit.LocalBuilder>(); this.parameterTable = new System.Collections.Generic.Dictionary <string, Type>(); //foreach (string arg in method.parameters) //{ // parameterTable[arg] = typeof(string); //} foreach (Collections.KeyValuePair <string, Type> para in method.parameters) { parameterTable[para.Key] = para.Value; } this.GenStmt(method.stmt); Collections.List <Type> args = new System.Collections.Generic.List <Type>(); foreach (Collections.KeyValuePair <string, Type> para in parameterTable) { args.Add(para.Value); } //foreach(string arg in method.parameters) //{ // args.Add(typeof(string)); //} methb.SetParameters(args.ToArray()); return(methb); }
public static void DefineParameters(MethodBuilder method, RppParameterInfo[] rppParams) { Type[] paramTypes = ParametersTypes(rppParams); method.SetParameters(paramTypes); int index = 1; foreach (var param in rppParams) { param.Index = index; // In static args should start from 1 method.DefineParameter(index, ParameterAttributes.None, param.Name); index++; } }
private static void AdjustParameterTypes(ParameterInfo[] parameters, Func <Type, Type> typeReplacer, MethodDefinition method) { var fixedParams = new Type[parameters.Length]; foreach (var parameter in parameters) { var paramType = typeReplacer(parameter.ParameterType); fixedParams[parameter.Position] = paramType; method.DefineParameter(parameter.Position, parameter.Attributes, parameter.Name); } // Set fixed parameters having correct generic parameters. method.SetParameters(fixedParams); // Set fixed return type with correct generic parameter. method.SetReturnType(typeReplacer(method.ReturnType)); }
/// <summary> /// Copies the method signature from one method to another. /// This includes generic parameters, constraints and parameters. /// </summary> /// <param name="sourceMethod">The source method.</param> /// <param name="targetMethod">The target method.</param> internal static void CopyMethodSignature(MethodInfo sourceMethod, MethodBuilder targetMethod) { CopyGenericSignature(sourceMethod, targetMethod); targetMethod.SetReturnType(sourceMethod.ReturnType); // copy the parameters and attributes // it seems that we can use the source parameters directly because the target method is derived // from the source method var parameters = sourceMethod.GetParameters(); targetMethod.SetParameters(parameters.Select(p => p.ParameterType).ToArray()); for (int i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; targetMethod.DefineParameter(i + 1, parameter.Attributes, parameter.Name); } }
/// <summary> /// Initializes a new instance of the <see cref="MethodBuilderHelper"/> class /// with the specified parameters. /// </summary> /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param> /// <param name="methodBuilder">A <see cref="MethodBuilder"/></param> /// <param name="genericArguments">Generic arguments of the method.</param> /// <param name="returnType">The return type of the method.</param> /// <param name="parameterTypes">The types of the parameters of the method.</param> internal MethodBuilderHelper( TypeBuilderHelper typeBuilder, MethodBuilder methodBuilder, Type[] genericArguments, Type returnType, Type[] parameterTypes ) : base(typeBuilder) { if (methodBuilder == null) throw new ArgumentNullException("methodBuilder"); if (genericArguments == null) throw new ArgumentNullException("genericArguments"); _methodBuilder = methodBuilder; var genArgNames = genericArguments.Select(t => t.Name).ToArray(); var genParams = methodBuilder.DefineGenericParameters(genArgNames); // Copy parameter constraints. // List<Type> interfaceConstraints = null; for (var i = 0; i < genParams.Length; i++) { genParams[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes); foreach (var constraint in genericArguments[i].GetGenericParameterConstraints()) { if (constraint.IsClass) genParams[i].SetBaseTypeConstraint(constraint); else { if (interfaceConstraints == null) interfaceConstraints = new List<Type>(); interfaceConstraints.Add(constraint); } } if (interfaceConstraints != null && interfaceConstraints.Count != 0) { genParams[i].SetInterfaceConstraints(interfaceConstraints.ToArray()); interfaceConstraints.Clear(); } } // When a method contains a generic parameter we need to replace all // generic types from methodInfoDeclaration with local ones. // for (var i = 0; i < parameterTypes.Length; i++) parameterTypes[i] = TypeHelper.TranslateGenericParameters(parameterTypes[i], genParams); methodBuilder.SetParameters(parameterTypes); methodBuilder.SetReturnType(TypeHelper.TranslateGenericParameters(returnType, genParams)); // Once all generic stuff is done is it is safe to call SetCustomAttribute // methodBuilder.SetCustomAttribute(Type.Assembly.BLToolkitAttribute); }
public void Compile() { GreatestFixedPointFunction = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name + Constants.FunctionSuffix, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var functionClassGenericParameters = GreatestFixedPointFunction.DefineGenericParameters(new[] { Constants.GFixFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); GreatestFixedPointFunctionApplyMethod = GreatestFixedPointFunction.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var underlyingType = GreatestFixedPointFunctionApplyMethod.DefineGenericParameters(Constants.ApplyMethodGenericParameterName)[0]; var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, underlyingType, functionClassGenericParameters.Skip(1).ToArray(), runtimeContainer); GreatestFixedPointFunctionApplyMethod.SetParameters(underlyingType, typeof(IFunction<,>).MakeGenericType(underlyingType, functorClass)); GreatestFixedPointFunctionApplyMethod.SetReturnType(functionClassGenericParameters[0]); TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var typeGenericParameters = declaration.TypeParameters.Any() ? TypeBuilder.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; GreatestFixedPointApplyMethod = TypeBuilder.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var resultTypeGfix = GreatestFixedPointApplyMethod.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0]; GreatestFixedPointApplyMethod.SetParameters(GreatestFixedPointFunction.MakeGenericType(new[] { resultTypeGfix }.Concat(typeGenericParameters).ToArray())); GreatestFixedPointApplyMethod.SetReturnType(resultTypeGfix); CompileAnaClass(); CompileAna(); CompileAnaFunction(); CompileAnaFunction1(); CompileIn(); CompileInFunction(); CompileOut(); CompileOutFunction(); TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder( typeof(InfiniteAttribute).GetConstructors()[0], new object[] { declaration.ConstructorFunctionName, declaration.DestructorFunctionName, declaration.AnaFunctionName, InFunction })); }
private void CompileOut() { Out = utilityClass.DefineMethod(Constants.OutMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? Out.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var returnType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters) : TypeBuilder; var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, returnType, genericParameters, runtimeContainer); Out.SetParameters(fGreatestFixedPoint); Out.SetReturnType(returnType); Out.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var outBody = Out.GetILGenerator(); outBody.Emit(OpCodes.Ldarg_0); outBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( InFunction.MakeGenericType(genericParameters), InFunctionConstructor) : InFunctionConstructor); outBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new[] { returnType, fGreatestFixedPoint }.Concat(genericParameters).ToArray())); outBody.Emit(OpCodes.Call, Ana.MakeGenericMethod(new[] { fGreatestFixedPoint }.Concat(genericParameters).ToArray())); outBody.Emit(OpCodes.Ret); }
private void CompileIn() { CompileInGeneratingFunction(); In = utilityClass.DefineMethod(Constants.InMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? In.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var resultType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.ToArray()) : TypeBuilder; In.SetReturnType(FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, resultType, genericParameters, runtimeContainer)); In.SetParameters(resultType); In.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, resultType, genericParameters, runtimeContainer); var applyMethodGenericClass = declaration.TypeParameters.Any() ? TypeBuilder.GetMethod( TypeBuilder.MakeGenericType(genericParameters), GreatestFixedPointApplyMethod) : GreatestFixedPointApplyMethod; var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(fGreatestFixedPoint); var inBody = In.GetILGenerator(); inBody.Emit(OpCodes.Ldarg_0); inBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( InClass.MakeGenericType(genericParameters), InGeneratingFunctionConstructor) : InGeneratingFunctionConstructor); inBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod); inBody.Emit(OpCodes.Ret); }
private void CompileAna() { Ana = utilityClass.DefineMethod(Constants.AnaMethodName, MethodAttributes.Public | MethodAttributes.Static); var anaGenericParameters = Ana.DefineGenericParameters(new[] { Constants.AnaMethodGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); Ana.SetReturnType(declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(anaGenericParameters.Skip(1).ToArray()) : TypeBuilder); var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaGenericParameters[0], anaGenericParameters.Skip(1).ToArray(), runtimeContainer); var generatorType = typeof(IFunction<,>).MakeGenericType(anaGenericParameters[0], functorClass); Ana.SetParameters(anaGenericParameters[0], generatorType); var anaBody = Ana.GetILGenerator(); anaBody.Emit(OpCodes.Ldarg_0); anaBody.Emit(OpCodes.Ldarg_1); anaBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(AnaClass.MakeGenericType(anaGenericParameters), AnaClassConstructor)); anaBody.Emit(OpCodes.Ret); }
private static void AdjustParameterTypes(ParameterInfo[] parameters, Func<Type, Type> typeReplacer, MethodDefinition method) { var fixedParams = new Type[parameters.Length]; foreach (var parameter in parameters) { var paramType = typeReplacer(parameter.ParameterType); fixedParams[parameter.Position] = paramType; method.DefineParameter(parameter.Position, parameter.Attributes, parameter.Name); } // Set fixed parameters having correct generic parameters. method.SetParameters(fixedParams); // Set fixed return type with correct generic parameter. method.SetReturnType(typeReplacer(method.ReturnType)); }
public void Compile() { TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var genericParameters = declaration.TypeParameters.Any() ? ((TypeBuilder)TypeBuilder).DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; Cata = ((TypeBuilder)TypeBuilder).DefineMethod(Constants.CataMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var cataReturnType = Cata.DefineGenericParameters(Constants.CataMethodGenericParameterName)[0]; var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, cataReturnType, genericParameters, runtimeContainer); Cata.SetParameters(typeof(IFunction<,>).MakeGenericType(functorClass, cataReturnType)); Cata.SetReturnType(cataReturnType); CompileCataFunction(); CompileCataFunction1(); CompileOutClass(); CompileOut(); CompileOutFunction(); CompileIn(); CompileInFunction(); TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(FiniteAttribute).GetConstructors()[0], new object[] { declaration.ConstructorFunctionName, declaration.DestructorFunctionName, declaration.CataFunctionName, InFunction })); }
private void CompileIn() { In = utilityClass.DefineMethod(Constants.InMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? In.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var inputParameter = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.ToArray()) : TypeBuilder; In.SetReturnType(FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, inputParameter, genericParameters, runtimeContainer)); In.SetParameters(inputParameter); In.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var inBody = In.GetILGenerator(); var fLeastFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, inputParameter, genericParameters, runtimeContainer); inBody.Emit(OpCodes.Ldarg_0); inBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( OutFunction.MakeGenericType(genericParameters), OutFunctionConstructor) : OutFunctionConstructor); inBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { fLeastFixedPoint, inputParameter }.Concat(genericParameters).ToArray())); inBody.Emit(OpCodes.Callvirt, declaration.TypeParameters.Any() ? TypeBuilder.GetMethod(inputParameter, Cata).MakeGenericMethod(fLeastFixedPoint) : Cata.MakeGenericMethod(fLeastFixedPoint)); inBody.Emit(OpCodes.Ret); }
private void EmitILCodeForFunctionParameters(ILGenerator ilgen, MethodBuilder methodBuilder, List<FunctionArgumentInfo> paramTypes, bool funcReturnTypeExistsAsTable) { methodBuilder.SetParameters(paramTypes.Select(x => x.Type??typeof(object)).ToArray()); foreach (var paramType in paramTypes) { var i = paramType.Index; var argName = paramType.Name ?? $"param{i}"; var mappedArgType = paramType.Type; if (mappedArgType == null) { continue; } methodBuilder.DefineParameter(i + 1, ParameterAttributes.In, argName); if (funcReturnTypeExistsAsTable) { ilgen.EmitParameterBodyForGetTable(i, mappedArgType); } else { ilgen.EmitParameterBodyForGetEnumerable(i, argName, mappedArgType); } } }
protected override void PreBuild() { if (Info != null) { return; } var cont = CurrentContainer; Builder = cont.CreateMethod(Name, Attributes); Info = Builder; if (Generics.Count > 0) { var gb = Builder.DefineGenericParameters(Generics.ToNames()); Generics.RegisterBuilders(gb); } if (ReturnType != null) { Builder.SetReturnType(ReturnType.GainType()); //todo ジェネリクスに対応したTypeを生成する。 } Builder.SetParameters(Arguments.ToTypes()); Arguments.RegisterBuilders(Builder, IsInstance); SpreadGenerator(); }