Example #1
0
        private void DefineType(ModuleDefinition module, Ast.Definitions.AstTypeDefinition typeAst, DefinitionBuildingContext context)
        {
            var type = new TypeDefinition("", typeAst.Name, TypeAttributes.Public | ToTypeAttribute(typeAst.DefinitionType)) {
                BaseType = module.Import(typeof(object))
            };
            module.Types.Add(type);

            foreach (var memberAst in typeAst.Members) {
                DefineMember(type, memberAst, context);
            }
            context.MapDefinition(typeAst, type);
        }
Example #2
0
        private void DefineFunction(TypeDefinition type, AstMethodDefinitionBase methodAst, DefinitionBuildingContext context)
        {
            MethodDefinition method;
            if (methodAst is Ast.Definitions.AstFunctionDefinition) {
                var functionAst = methodAst as AstFunctionDefinition;
                var attributes = MethodAttributes.Public;
                if (methodAst.Compilation.Static)
                    attributes |= MethodAttributes.Static;

                var returnType = context.ConvertReference(functionAst.ReturnType, returnNullIfFailed: true);
                method = new MethodDefinition(functionAst.Name, attributes, returnType ?? CecilHelper.GetVoidType(type.Module));
                if (returnType == null)
                    context.AddDebt(() => method.ReturnType = context.ConvertReference(functionAst.ReturnType));

                if (methodAst.Compilation.EntryPoint)
                    type.Module.EntryPoint = method;
            }
            else if (methodAst is Ast.Definitions.AstConstructorDefinition) {
                method = CecilHelper.CreateConstructor(type);
                method.Attributes |= MethodAttributes.Public;
            }
            else {
                throw new NotImplementedException("LightCompiler.CompileFunction: cannot compile " + methodAst + ".");
            }

            type.Methods.Add(method);
            context.MapDefinition(methodAst, method);
            DefineParameters(method, methodAst, context);
        }