public void Compile(AstRoot root, Stream toStream, CompilationArguments arguments) { Argument.RequireNotNull("toStream", toStream); Argument.RequireNotNull("root", root); Argument.RequireNotNull("arguments", arguments); var assembly = AssemblyDefinition.CreateAssembly( new AssemblyNameDefinition(arguments.AssemblyName, arguments.AssemblyVersion), arguments.AssemblyName, arguments.Target == CompilationTarget.Console ? ModuleKind.Console : ModuleKind.Dll ); var module = assembly.MainModule; var context = new DefinitionBuildingContext(module, this.referenceProviders); foreach (var typeAst in root.Descendants <AstTypeDefinition>()) { DefineType(module, typeAst, context); } context.ClearDebt(); foreach (var type in module.Types) { foreach (var method in type.Methods) { CompileMethod(method, context.GetAst(method), context); } } assembly.Write(toStream); }
private void DefineMember(TypeDefinition type, IAstDefinition memberAst, DefinitionBuildingContext context) { var builder = this.builders.SingleOrDefault(c => c.CanBuild(memberAst, type)); if (builder == null) { throw new NotImplementedException("LightCompiler: No DefinitionBuilder for " + memberAst + " under " + type + "."); } builder.Build(memberAst, type, context); }
public override void Build(AstFunctionDefinition ast, TypeDefinition type, DefinitionBuildingContext context) { var attributes = MethodAttributes.Public; if (ast.Compilation.Static) { attributes |= MethodAttributes.Static; } var method = new MethodDefinition(ast.Name, attributes, type.Module.Import(typeof(void))); var astGenericParameterTypes = ast.Parameters.Select(p => p.Type).OfType <AstGenericPlaceholderType>(); foreach (var astPlaceholder in astGenericParameterTypes) { var genericParameter = new GenericParameter(astPlaceholder.Name, method); method.GenericParameters.Add(genericParameter); context.MapDefinition(astPlaceholder, genericParameter); } var returnType = context.ConvertReference(ast.ReturnType, returnNullIfFailed: true); if (returnType != null) { method.ReturnType = returnType; } else { context.AddDebt(() => method.ReturnType = context.ConvertReference(ast.ReturnType)); } if (ast.Compilation.EntryPoint) { type.Module.EntryPoint = method; } this.helper.FinalizeBuild(method, ast, type, context); }
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); } }
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); }
public override void Build(AstConstructorDefinition ast, TypeDefinition type, DefinitionBuildingContext context) { var constructor = new MethodDefinition( ".ctor", MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.HideBySig, type.Module.Import(typeof(void)) ); constructor.Attributes |= MethodAttributes.Public; this.helper.FinalizeBuild(constructor, ast, type, context); }
public virtual void FinalizeBuild(MethodDefinition method, AstMethodDefinitionBase ast, TypeDefinition type, DefinitionBuildingContext context) { BuildParameters(method, ast, context); type.Methods.Add(method); context.MapDefinition(ast, method); }
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))); } }
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))); } }
public virtual void FinalizeBuild(MethodDefinition method, AstMethodDefinitionBase ast, TypeDefinition type, DefinitionBuildingContext context) { BuildParameters(method, ast, context); type.Methods.Add(method); context.MapDefinition(ast, method); }
public override void Build(AstPropertyDefinition propertyOrField, TypeDefinition typeDefinition, DefinitionBuildingContext context) { var type = context.ConvertReference(propertyOrField.Type); var fieldDefinition = new FieldDefinition(propertyOrField.Name, FieldAttributes.Private, type); typeDefinition.Fields.Add(fieldDefinition); context.MapDefinition(propertyOrField, fieldDefinition); }