예제 #1
0
 public CilCompilationContext(MethodDefinition method, AstMethodDefinitionBase methodAst, Action<IAstElement, CilCompilationContext> compile, IReferenceContext referenceContext)
 {
     this.compile = compile;
     this.referenceContext = referenceContext;
     this.Method = method;
     this.MethodAst = methodAst;
 }
예제 #2
0
        private void CompileMethod(MethodDefinition method, AstMethodDefinitionBase methodAst, DefinitionBuildingContext parentContext)
        {
            var body = method.Body.GetILProcessor();
            var context = new CilCompilationContext(method, methodAst, (e, c) => CompileCil(body, e, c), parentContext);

            foreach (var element in methodAst.Body) {
                CompileCil(body, element, context);
            }
        }
예제 #3
0
 public void MapDefinition(AstMethodDefinitionBase methodAst, MethodDefinition method)
 {
     references.Add(methodAst, method);
 }
예제 #4
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);
        }
예제 #5
0
 private void DefineParameters(MethodDefinition method, AstMethodDefinitionBase methodAst, DefinitionBuildingContext context)
 {
     foreach (var parameter in methodAst.Parameters) {
         method.Parameters.Add(new ParameterDefinition(parameter.Name, ParameterAttributes.None, context.ConvertReference(parameter.Type)));
     }
 }
예제 #6
0
 protected virtual void BuildParameters(MethodDefinition method, AstMethodDefinitionBase ast, DefinitionBuildingContext context)
 {
     foreach (var parameter in ast.Parameters) {
         method.Parameters.Add(new ParameterDefinition(parameter.Name, ParameterAttributes.None, context.ConvertReference(parameter.Type)));
     }
 }
예제 #7
0
 public virtual void FinalizeBuild(MethodDefinition method, AstMethodDefinitionBase ast, TypeDefinition type, DefinitionBuildingContext context)
 {
     BuildParameters(method, ast, context);
     type.Methods.Add(method);
     context.MapDefinition(ast, method);
 }