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}", 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}", 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("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 fldDecl = string.Format("{0} {1};", GenContext.GetTypeName(sfldX.FieldType), GenContext.GetFieldName(sfldX)); prtDecl.AppendFormatLine("// {0} -> {1}", Helper.EscapeString(sfldX.DeclType.GetNameKey()), Helper.EscapeString(sfldX.GetReplacedNameKey())); prtDecl.AppendLine("extern " + fldDecl); prtImpl.AppendLine(fldDecl); } // 生成类型判断函数 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}", !metX.Def.HasBody && !metX.Def.IsAbstract ? "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); }