示例#1
0
        private void GenerateClassMethods(ITypeBuilder type, IReadOnlyList <MethodSyntaxNode> methods, CodeGenerationStore store,
                                          Dictionary <IMethodBuilder, MethodSyntaxNode> methodsDictionary, ref IMethodInfo?entryPoint)
        {
            var definedMethods = new List <IMethodInfo>();

            store.Methods.Add(type, definedMethods);

            foreach (var method in methods)
            {
                var methodAttributes = MethodAttributes.Public;
                if (method.IsStatic)
                {
                    methodAttributes |= MethodAttributes.Static;
                }

                var parameters = method.Parameters.Select(x =>
                {
                    var tpe = store.TypeDefLookup(x.Type);
                    if (x.IsRef)
                    {
                        throw new RefTypeException("Ref types are not supported");
                    }

                    typeChecker !.AssertTypeIsNotVoid(tpe);
                    return(tpe);
                }).ToArray();

                var arrType = typeof(int[]);

                var returnType = store.TypeDefLookup(method.ReturnType);

                var definedMethod = type.DefineMethod(method.Name, methodAttributes, returnType, parameters, method.IsEntryPoint);

                if (method.IsEntryPoint)
                {
                    if (entryPoint != null)
                    {
                        throw new EntryPointException("Can only have 1 entry point");
                    }
                    entryPoint = definedMethod;
                }

                definedMethods.Add(definedMethod);

                store.MethodParameters.Add(definedMethod, parameters);
                methodsDictionary.Add(definedMethod, method);

                int offset = 0;
                //if (!method.IsStatic)
                //{
                //    offset = 1;
                //}

                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    definedMethod.DefineParameter(i + 1 + offset, ParameterAttributes.None, method.Parameters[i].Name);
                }
            }
        }
示例#2
0
        public IMethodBuilder DefineMethod(string name, MethodAttributes attributes)
        {
            ArgumentUtility.CheckNotNullOrEmpty("name", name);

            var methodBuilder = _typeBuilder.DefineMethod(name, attributes);

            return(new MethodBuilderDecorator(methodBuilder, EmittableOperandProvider));
        }