public ClassCode Generate(XamlDocument document) { var result = new ClassCode { Name = document.Name, CodeBlocks = new List <Code>(), UsingNamespaces = new List <string>(), Namespace = document.Namespace }; IEnumerable <XElement> children = document.Elements; foreach (var child in children) { string name = child.Name.LocalName; switch (name) { case "Members": result.CodeBlocks.AddRange(GetProperties(child)); break; case "TextExpression.NamespacesForImplementation": result.UsingNamespaces.AddRange(GetNamespaces(child)); break; case "Sequence": result.CodeBlocks.Add(methodGenerator.Generate(child)); break; } } customMethodAlocator.Allocate(result.CodeBlocks); return(result); }
public ChannelEvaluator(List <Channel> channels) { this.channelCount = channels.Count; var generator = new MethodGenerator(channels); generator.Generate(); this.eval = generator.Delegate; this.splines = generator.Splines; }
private string BuildLambdaExpression(string delegateType, List <Var> generatedParameters, TypeReference returnType, List <string> statements, SemanticModel semanticModel) { string method = MethodGenerator.Generate("invoke", returnType, new List <string>(), AccessModifier.Public, generatedParameters, statements, false, false, semanticModel).MainPart; string generatedExpression = string.Format(@"new {0}() {{ @Override {1} }}", delegateType, method.AddTab(4)); return(generatedExpression); }
public SourceCode GenerateComplexProperty(ComplexPropertyDescription complexPropertyDescription, SemanticModel semanticModel) { string propertyBackingFieldName = complexPropertyDescription.PropertyName.ToLowerFirstChar(); //string optionalStaticModifier = complexPropertyDescription.IsStatic ? "static " : ""; //string backingField = // "private " + optionalStaticModifier // + complexPropertyDescription.PropertyType.Text + " " + propertyBackingFieldName + ";\n"; var property = new SourceCode { MainPart = "" }; if (complexPropertyDescription.GetAccessModifier.HasValue) { var getterStatements = complexPropertyDescription.GetStatements; string name = GenerateGetterMethodName(complexPropertyDescription.PropertyType, complexPropertyDescription.PropertyName); var getter = MethodGenerator.Generate( name, complexPropertyDescription.PropertyType, new List <string>(), complexPropertyDescription.GetAccessModifier.Value, new List <Var>(), getterStatements, complexPropertyDescription.IsStatic, complexPropertyDescription.IsVirtual, semanticModel); property.MainPart += getter.MainPart + "\n"; } if (complexPropertyDescription.SetAccessModifier.HasValue) { var setterStatements = complexPropertyDescription.SetStatements; var setterArgs = new List <Var> { new Var { Name = "value", Type = complexPropertyDescription.PropertyType } }; var setter = MethodGenerator.Generate( "set" + complexPropertyDescription.PropertyName, JavaTypeReferences.Void, new List <string>(), complexPropertyDescription.SetAccessModifier.Value, setterArgs, setterStatements, complexPropertyDescription.IsStatic, complexPropertyDescription.IsVirtual, semanticModel); property.MainPart += setter.MainPart + "\n"; } return(property); }
public SourceCode GenerateSimpleProperty(SimplePropertyDescription simplePropertyDescription, SemanticModel semanticModel, bool isFromInterface) { string propertyBackingFieldName = simplePropertyDescription.PropertyName.ToLowerFirstChar(); string optionalStaticModifier = simplePropertyDescription.IsStatic ? "static " : ""; string backingField = "private " + optionalStaticModifier + simplePropertyDescription.PropertyType.Text + " " + propertyBackingFieldName + ";\n"; SourceCode property; if (isFromInterface) { property = new SourceCode { MainPart = "" }; } else { property = new SourceCode { MainPart = backingField + "\n" }; } if (simplePropertyDescription.GetAccessModifier.HasValue) { Optional <List <string> > getterStatements; if (isFromInterface) { getterStatements = new Optional <List <string> >(); } else { getterStatements = new List <string> { "return " + propertyBackingFieldName + ";" }; } string name = GenerateGetterMethodName(simplePropertyDescription.PropertyType, simplePropertyDescription.PropertyName); var getter = MethodGenerator.Generate( name, simplePropertyDescription.PropertyType, new List <string>(), simplePropertyDescription.GetAccessModifier.Value, new List <Var>(), getterStatements, simplePropertyDescription.IsStatic, simplePropertyDescription.IsVirtual, semanticModel); property.MainPart += getter.MainPart + "\n"; } if (simplePropertyDescription.SetAccessModifier.HasValue) { Optional <List <string> > setterStatements; if (isFromInterface) { setterStatements = new Optional <List <string> >(); } else { string fieldAccessor = simplePropertyDescription.IsStatic ? "" : "this."; setterStatements = new List <string> { fieldAccessor + propertyBackingFieldName + " = " + "value" + ";" }; } var setterArgs = new List <Var> { new Var { Name = "value", Type = simplePropertyDescription.PropertyType } }; var setter = MethodGenerator.Generate( "set" + simplePropertyDescription.PropertyName, JavaTypeReferences.Void, new List <string>(), simplePropertyDescription.SetAccessModifier.Value, setterArgs, setterStatements, simplePropertyDescription.IsStatic, simplePropertyDescription.IsVirtual, semanticModel); property.MainPart += setter.MainPart + "\n"; } return(property); }
public CompileUnit Generate() { CompileUnit unit = new CompileUnit(); unit.Name = GenContext.GetTypeName(CurrType); // 重排字段 var fields = LayoutFields(out var sfields); // 生成类结构 CodePrinter prtDecl = new CodePrinter(); if (!CurrType.IsEnumType) { prtDecl.AppendFormatLine("// {0}", CurrType.GetNameKey()); var baseType = CurrType.BaseType; // 值类型不继承任何基类 if (CurrType.IsValueType) { baseType = null; } // 接口类型继承 object if (baseType == null && CurrType.Def.IsInterface) { baseType = GenContext.TypeMgr.GetTypeByName("Object"); } if (baseType != null) { string strBaseTypeName = GenContext.GetTypeName(baseType); unit.DeclDepends.Add(strBaseTypeName); prtDecl.AppendFormatLine("struct {0} : {1}", GenContext.GetTypeName(CurrType), strBaseTypeName); } else { prtDecl.AppendFormatLine("struct {0}", GenContext.GetTypeName(CurrType)); } prtDecl.AppendLine("{"); ++prtDecl.Indents; // 生成对象内置字段 if (CurrType.IsArrayType) { var arrayInfo = CurrType.ArrayInfo; if (arrayInfo.IsSZArray) { prtDecl.AppendLine("int32_t Length;"); } else { uint rank = arrayInfo.Rank; for (int i = 0; i < rank; ++i) { prtDecl.AppendFormatLine("int32_t LowerBound{0};\nint32_t Size{0};", i); } } } else { string nameKey = CurrType.GetNameKey(); if (nameKey == "Object") { prtDecl.AppendLine("uint32_t TypeID;"); } else if (nameKey == "System.Array") { prtDecl.AppendLine("int32_t Rank;"); } } // 生成字段 foreach (var fldX in fields) { RefValueTypeDecl(unit, fldX.FieldType); prtDecl.AppendLine("// " + fldX.GetReplacedNameKey()); prtDecl.AppendFormatLine("{0} {1};", GenContext.GetTypeName(fldX.FieldType), GenContext.GetFieldName(fldX)); } --prtDecl.Indents; prtDecl.AppendLine("};"); } CodePrinter prtImpl = new CodePrinter(); // 生成类型判断函数 GenerateIsType(prtDecl, prtImpl); // 生成静态字段 foreach (var sfldX in sfields) { RefValueTypeDecl(unit, sfldX.FieldType); string fldDecl = string.Format("{0} {1};", GenContext.GetTypeName(sfldX.FieldType), GenContext.GetFieldName(sfldX)); prtDecl.AppendFormatLine("// {0} -> {1}", sfldX.DeclType.GetNameKey(), sfldX.GetReplacedNameKey()); prtDecl.AppendLine("extern " + fldDecl); prtImpl.AppendLine(fldDecl); } // 生成方法 foreach (MethodX metX in CurrType.Methods) { var metGen = new MethodGenerator(GenContext, metX); metGen.Generate(); prtDecl.AppendFormatLine("// {0} -> {1}", metX.DeclType.GetNameKey(), metX.GetReplacedNameKey()); prtDecl.Append(metGen.DeclCode); unit.DeclDepends.UnionWith(metGen.DeclDepends); prtImpl.Append(metGen.ImplCode); unit.ImplDepends.UnionWith(metGen.ImplDepends); unit.StringDepends.UnionWith(metGen.StringDepends); } unit.DeclCode = prtDecl.ToString(); unit.ImplCode = prtImpl.ToString(); return(unit); }
public CompileUnit Generate() { CompileUnit unit = new CompileUnit(); unit.Name = GenContext.GetTypeName(CurrType, false); // 重排字段 var fields = LayoutFields(out var sfields); string nameKey = CurrType.GetNameKey(); bool currIsObject = nameKey == "Object"; // 生成类结构 CodePrinter prtDecl = new CodePrinter(); if (!CurrType.IsEnumType) { prtDecl.AppendFormatLine("// {0}", nameKey); var baseType = CurrType.BaseType; // 值类型不继承任何基类 if (CurrType.IsValueType) { baseType = null; } // 接口类型继承 object if (baseType == null && CurrType.Def.IsInterface) { baseType = GenContext.TypeMgr.GetTypeByName("Object"); } if (baseType != null) { string strBaseTypeName = GenContext.GetTypeName(baseType); unit.DeclDepends.Add(strBaseTypeName); prtDecl.AppendFormatLine("struct {0} : {1}", GenContext.GetTypeName(CurrType), strBaseTypeName); } else { prtDecl.AppendFormatLine("struct {0}", GenContext.GetTypeName(CurrType)); } prtDecl.AppendLine("{"); ++prtDecl.Indents; // 生成对象内置字段 if (CurrType.IsArrayType) { var arrayInfo = CurrType.ArrayInfo; if (!arrayInfo.IsSZArray) { uint rank = arrayInfo.Rank; for (int i = 0; i < rank; ++i) { prtDecl.AppendFormatLine("int32_t LowerBound{0};\nuint32_t Size{0};", i); } } } else { if (currIsObject) { prtDecl.AppendLine("uint32_t TypeID;"); prtDecl.AppendLine("uint8_t Flags[4];"); } else if (nameKey == "System.Array") { prtDecl.AppendLine("uintptr_t Length;"); prtDecl.AppendLine("uint32_t Rank;"); prtDecl.AppendLine("uint32_t ElemSize;"); } } bool isExplicitLayout = CurrType.Def.IsExplicitLayout; uint classSize = CurrType.Def.ClassSize; bool hasStructSize = CurrType.Def.HasClassLayout && classSize != 0; if (isExplicitLayout || hasStructSize) { prtDecl.AppendLine("union\n{"); ++prtDecl.Indents; if (!isExplicitLayout && fields.Count != 0) { prtDecl.AppendLine("struct\n{"); ++prtDecl.Indents; } } int fldCounter = 0; // 生成字段 foreach (var fldX in fields) { RefValueTypeDecl(unit, fldX.FieldType); bool isFieldExLayout = isExplicitLayout && fldX.Def.FieldOffset != 0; if (isFieldExLayout) { prtDecl.AppendLine("struct\n{"); ++prtDecl.Indents; prtDecl.AppendFormatLine("uint8_t padding_{0}[{1}];", fldCounter, fldX.Def.FieldOffset); } prtDecl.AppendLine("// " + fldX.GetReplacedNameKey()); prtDecl.AppendFormatLine("{0} {1};", GenContext.GetTypeName(fldX.FieldType), GenContext.GetFieldName(fldX)); if (isFieldExLayout) { --prtDecl.Indents; prtDecl.AppendLine("};"); } ++fldCounter; } if (isExplicitLayout || hasStructSize) { if (!isExplicitLayout && fields.Count != 0) { --prtDecl.Indents; prtDecl.AppendLine("};"); } if (hasStructSize) { prtDecl.AppendFormatLine("uint8_t padding_struct[{0}];", classSize); } --prtDecl.Indents; prtDecl.AppendLine("};"); } --prtDecl.Indents; prtDecl.AppendLine("};"); } CodePrinter prtImpl = new CodePrinter(); // 生成类型判断函数 GenerateIsType(prtDecl, prtImpl, currIsObject); // 生成静态字段 foreach (var sfldX in sfields) { RefValueTypeDecl(unit, sfldX.FieldType); string fldDecl = string.Format("{0} {1};", GenContext.GetTypeName(sfldX.FieldType), GenContext.GetFieldName(sfldX)); prtDecl.AppendFormatLine("// {0} -> {1}", sfldX.DeclType.GetNameKey(), sfldX.GetReplacedNameKey()); prtDecl.AppendLine("extern " + fldDecl); prtImpl.AppendLine(fldDecl); } // 生成方法 foreach (MethodX metX in CurrType.Methods) { var metGen = new MethodGenerator(GenContext, metX); metGen.Generate(); AppendRuntimeFlags(metX, prtDecl); prtDecl.AppendFormatLine("// {0}{1}{2} -> {3}", !metX.Def.HasBody && !metX.Def.IsAbstract ? "extern " : null, metX.Def.IsInternalCall ? "internalcall " : null, metX.DeclType.GetNameKey(), metX.GetReplacedNameKey()); prtDecl.Append(metGen.DeclCode); unit.DeclDepends.UnionWith(metGen.DeclDepends); prtImpl.Append(metGen.ImplCode); unit.ImplDepends.UnionWith(metGen.ImplDepends); unit.StringDepends.UnionWith(metGen.StringDepends); } unit.DeclCode = prtDecl.ToString(); unit.ImplCode = prtImpl.ToString(); return(unit); }
/// <summary> /// Generates code for the specified method information. /// </summary> /// <param name="methodInfo">The method information.</param> /// <returns>System.String.</returns> public string Generate(MethodInfo methodInfo) => _methodGenerator.Generate(methodInfo);
private SourceCode GetMethods(SemanticModel semanticModel, HashSet <string> typeReferences, IEnumerable <MethodDeclarationSyntax> methodDeclarations) { var generatedMethods = new SourceCode { MainPart = "" }; foreach (var methodDeclaration in methodDeclarations) { string identifier = methodDeclaration.Identifier.ValueText; var returnType = methodDeclaration.ReturnType; var returnTypeReference = TypeReferenceGenerator.GenerateTypeReference(returnType, semanticModel); if (!returnTypeReference.IsPredefined) { typeReferences.Add(returnTypeReference.Text); } var typeParameters = new List <string>(); if (methodDeclaration.TypeParameterList != null) { foreach (var typeParameter in methodDeclaration.TypeParameterList.Parameters) { typeParameters.Add(typeParameter.Identifier.ValueText); } } var parameters = new List <Var>(); foreach (var parameter in methodDeclaration.ParameterList.Parameters) { var typeReference = TypeReferenceGenerator.GenerateTypeReference(parameter.Type, semanticModel); parameters.Add(new Var { Name = parameter.Identifier.ValueText, Type = typeReference }); } var accessModifier = GetAccessModifier(methodDeclaration.Modifiers.ToList()); bool isStatic = HasStaticModifier(methodDeclaration.Modifiers.ToList()); Optional <List <string> > body; if (methodDeclaration.Body == null) { body = new Optional <List <string> >(); } else { var statements = new List <string>(); foreach (var statement in methodDeclaration.Body.Statements) { string generatedStatement = StatementGenerator.Generate(statement, semanticModel); statements.Add(generatedStatement); new Optional <List <string> >(); } body = new Optional <List <string> >(statements); } bool isVirtual = !body.HasValue || HasVirtualModifier(methodDeclaration.Modifiers.ToList()); var generatedMethod = MethodGenerator.Generate(identifier, returnTypeReference, typeParameters, accessModifier, parameters, body, isStatic, isVirtual, semanticModel); generatedMethods.MainPart += "\n" + generatedMethod.MainPart; } return(generatedMethods); }
public void TestGenerate() { var php = _generator.Generate(new Method("foo")); Assert.AreEqual("private function foo(){}", php); }
public CompileUnit Generate() { CompileUnit unit = new CompileUnit(); string strTypeName = GenContext.GetTypeName(CurrType, false); unit.Name = strTypeName; // 重排字段 var fields = LayoutFields(out var sfields); string nameKey = CurrType.GetNameKey(); bool currIsObject = nameKey == "Object"; // 生成类结构 CodePrinter prtDecl = new CodePrinter(); if (!CurrType.IsEnumType) { prtDecl.AppendFormatLine("// {0}", Helper.EscapeString(nameKey)); ushort packSize = CurrType.Def.HasClassLayout ? CurrType.Def.PackingSize : (ushort)0; if (packSize > 2 && !Helper.IsPowerOfTwo(packSize)) { throw new TypeLoadException(); } if (packSize != 0) { prtDecl.AppendLine("#if defined(IL2CPP_MSVC_LIKE)"); prtDecl.AppendFormatLine("#pragma pack(push, {0})", packSize); prtDecl.AppendLine("#endif"); } var baseType = CurrType.BaseType; // 值类型不继承任何基类 if (CurrType.IsValueType) { baseType = null; } // 接口类型继承 object if (baseType == null && CurrType.Def.IsInterface) { baseType = GenContext.GetTypeByName("Object"); } if (baseType != null) { string strBaseTypeName = GenContext.GetTypeName(baseType); unit.DeclDepends.Add(strBaseTypeName); prtDecl.AppendFormatLine("struct {0} : {1}", strTypeName, strBaseTypeName); } else { prtDecl.AppendFormatLine("struct {0}", strTypeName); } prtDecl.AppendLine("{"); ++prtDecl.Indents; // 生成对象内置字段 if (CurrType.IsArrayType) { var arrayInfo = CurrType.ArrayInfo; if (!arrayInfo.IsSZArray) { uint rank = arrayInfo.Rank; for (int i = 0; i < rank; ++i) { prtDecl.AppendFormatLine("int32_t LowerBound{0};\nuint32_t Size{0};", i); } } } else { if (currIsObject) { prtDecl.AppendLine("uint32_t TypeID;"); prtDecl.AppendLine("uint8_t Flags[4];"); } else if (nameKey == "System.Array") { prtDecl.AppendLine("uint32_t Length;"); prtDecl.AppendLine("uint32_t ElemSize : 24;"); prtDecl.AppendLine("uint32_t Rank : 8;"); } } bool isExplicitLayout = CurrType.Def.IsExplicitLayout; uint classSize = CurrType.Def.ClassSize; bool hasStructSize = CurrType.Def.HasClassLayout && classSize != 0; if (isExplicitLayout || hasStructSize) { prtDecl.AppendLine("union\n{"); ++prtDecl.Indents; if (!isExplicitLayout && fields.Count != 0) { prtDecl.AppendLine("struct\n{"); ++prtDecl.Indents; } } int fldCounter = 0; // 生成字段 foreach (var fldX in fields) { RefValueTypeDecl(unit, fldX.FieldType); bool isFieldExLayout = isExplicitLayout && fldX.Def.FieldOffset != 0; if (isFieldExLayout) { prtDecl.AppendLine("struct\n{"); ++prtDecl.Indents; prtDecl.AppendFormatLine("uint8_t padding_{0}[{1}];", fldCounter, fldX.Def.FieldOffset); } prtDecl.AppendLine("// " + fldX.GetReplacedNameKey()); prtDecl.AppendFormatLine("{0} {1};", GenContext.GetTypeName(fldX.FieldType), GenContext.GetFieldName(fldX)); if (isFieldExLayout) { --prtDecl.Indents; prtDecl.AppendLine("};"); } ++fldCounter; } if (isExplicitLayout || hasStructSize) { if (!isExplicitLayout && fields.Count != 0) { --prtDecl.Indents; prtDecl.AppendLine("};"); } if (hasStructSize) { prtDecl.AppendFormatLine("uint8_t padding_struct[{0}];", classSize); } --prtDecl.Indents; prtDecl.AppendLine("};"); } --prtDecl.Indents; if (packSize != 0) { prtDecl.AppendFormatLine("}} IL2CPP_PACKED_TAIL({0});", packSize); } else { prtDecl.AppendLine("};"); } if (packSize != 0) { prtDecl.AppendLine("#if defined(IL2CPP_MSVC_LIKE)"); prtDecl.AppendLine("#pragma pack(pop)"); prtDecl.AppendLine("#endif"); } } CodePrinter prtImpl = new CodePrinter(); // 生成静态字段 foreach (var sfldX in sfields) { RefValueTypeDecl(unit, sfldX.FieldType); string sfldName = GenContext.GetFieldName(sfldX); string fldDecl = string.Format("{0} {1};", GenContext.GetTypeName(sfldX.FieldType), sfldName); prtDecl.AppendFormatLine("// {0} -> {1}", Helper.EscapeString(sfldX.DeclType.GetNameKey()), Helper.EscapeString(sfldX.GetReplacedNameKey())); prtDecl.AppendLine("extern " + fldDecl); prtImpl.AppendLine(fldDecl); bool hasRef = GenContext.IsRefOrContainsRef(GenContext.GetTypeBySig(sfldX.FieldType)); GenContext.AddStaticField(strTypeName, sfldName, hasRef); } // 生成类型判断函数 GenIsTypeFunc(prtDecl, prtImpl, currIsObject); // 生成方法 foreach (MethodX metX in CurrType.Methods) { var metGen = new MethodGenerator(GenContext, metX); metGen.Generate(); AppendRuntimeFlags(metX, prtDecl); prtDecl.AppendFormatLine("// {0}{1}{2} -> {3}", Helper.IsExtern(metX.Def) ? "extern " : null, metX.Def.IsInternalCall ? "internalcall " : null, Helper.EscapeString(metX.DeclType.GetNameKey()), Helper.EscapeString(metX.GetReplacedNameKey())); prtDecl.Append(metGen.DeclCode); unit.DeclDepends.UnionWith(metGen.DeclDepends); prtImpl.Append(metGen.ImplCode); unit.ImplDepends.UnionWith(metGen.ImplDepends); unit.StringDepends.UnionWith(metGen.StringDepends); } GenerateMetadata(prtDecl, prtImpl); unit.DeclCode = prtDecl.ToString(); unit.ImplCode = prtImpl.ToString(); return(unit); }