public static void WriteBoxedValueType(CodeTextWriter Writer, TypeDefinition Type)
        {
            if (!Type.IsValueType)
            {
                return;
            }
            var    solved     = Type.InterfacesSolved();
            string Interfaces = string.Join(',', solved.Select(a => $"public {a.InterfaceType.CXXTypeName()}"));

            // [H2004] Boxed ValueType
            if (Type.HasGenericParameters)
            {
                Writer.WriteLine($"template<{Type.CXXTemplateParam()}>");
            }
            string classDef = $"struct {Type.CXXShortTypeName()}_V : public RTCLI::System::ValueType{(solved.Count == 0 ? "" : "," + Interfaces)}";

            using (var classScope = new CXXScopeDisposer(Writer, classDef, true,
                                                         $"// [H2004] Boxed ValueType {Type.CXXTypeName()}_V ",
                                                         $"// [H2004] Exit Boxed ValueType {Type.CXXTypeName()}_V"))
            {
                Writer.unindent().WriteLine("public:").indent();
                Writer.WriteLine($"using ValueType = {Type.CXXShortTypeName()};");
                //Writer.WriteLine($"using ValueType = struct {type.CXXShortTypeName()};");
                Writer.WriteLine($"{Type.CXXShortTypeName()} value;");
                WriteMethodSignatures(Writer, Type, false);
            }
        }
Exemple #2
0
 public void Dispose()
 {
     if (parent != null)
     {
         parent.unindent();
         parent.WriteLine("}" + (EndWithSemicolon?";":""));
         parent = null;
     }
 }
        private void WriteTypeRecursively(CodeTextWriter codeWriter, TypeDefinition type)
        {
            if (type.IsEnum)
            {
                CXXHeaderRules.WriteEnumType(codeWriter, type);
                return;
            }

            // [C0004] generic
            if (type.HasGenericParameters)
            {
                codeWriter.WriteLine(CXXHeaderRules.GenericDeclaration(type));
            }

            // [H2000] Type Scope
            using (var typeScope = new CXXTypeScope(codeWriter, type))
            {
                codeWriter.unindent().WriteLine("public:").indent();
                foreach (var nested in type.NestedTypes)
                {
                    // [H2002] Inner Types
                    codeWriter.WriteLine($"// [H2002] Inner Types {nested.CXXShortTypeName()}");
                    WriteTypeRecursively(codeWriter, nested);
                }
                // [H2001] Method Signatures
                CXXHeaderRules.WriteMethodSignatures(codeWriter, type, type.IsValueType);

                // [H2005] Field Declaration
                CXXHeaderRules.WriteFieldDeclaration(codeWriter, type);
            }

            // [H2004] Boxed ValueType
            if (!type.IsValueType)
            {
                return;
            }
            CXXHeaderRules.WriteBoxedValueType(codeWriter, type);
        }
        public static void WriteEnumType(CodeTextWriter Writer, TypeDefinition Type)
        {
            if (!Type.IsEnum)
            {
                return;
            }
            Writer.WriteLine($"using {Type.CXXShortTypeName()} = {Type.Fields.First().FieldType.CXXTypeName()};");
            string classDef = $"struct {Type.CXXShortTypeName()}_V : public RTCLI::System::Enum";

            using (var classScope = new CXXScopeDisposer(Writer, classDef, true,
                                                         $"// [H2004] Boxed ValueType {Type.CXXTypeName()}_V ",
                                                         $"// [H2004] Exit Boxed ValueType {Type.CXXTypeName()}_V"))
            {
                Writer.unindent().WriteLine("public:").indent();
                Writer.WriteLine($"using ValueType = {Type.CXXShortTypeName()};");
                //Writer.WriteLine($"using ValueType = struct {type.CXXShortTypeName()};");
                Writer.WriteLine($"{Type.CXXShortTypeName()} value;");
                foreach (var Field in Type.Fields.Skip(1))
                {
                    Writer.WriteLine($"static constexpr {Type.CXXShortTypeName()} {Field.Name} = {Field.Constant};");
                }
            }
        }
        public static void WriteMethodBody(CodeTextWriter Writer, MethodDefinition Method, bool ValueType)
        {
            MethodTranslateContextCXX methodContext = new MethodTranslateContextCXX(Method);

            Writer.WriteLine("{");
            Writer.indent();
            Writer.WriteLine("// [S2000] Method Body");
            if (Method.IsConstructor && Method.IsStatic)
            {
                Writer.WriteLine("// [S2000-2] Static Constructor Body");
                Writer.WriteLine("static std::once_flag flag;");
                Writer.WriteLine("std::call_once(flag,[&]()");
                Writer.WriteLine("{");
                Writer.indent();
            }
            if (Method.Body.HasVariables)
            {
                Writer.WriteLine("// [S2000-0] Local Varaiables");
                foreach (var localVar in Method.Body.Variables)
                {
                    if (Method.Body.InitLocals)
                    {
                        Writer.WriteLine($"{localVar.CXXVarDeclaration()} v{localVar.Index} = {localVar.CXXVarInitVal()};");
                    }
                    else
                    {
                        Writer.WriteLine($"{localVar.CXXVarDeclaration()} v{localVar.Index};");
                    }
                }
            }

            Writer.WriteLine("// [S2000-1] Code Body");
            foreach (var instruction in Method.Body.Instructions)
            {
                IterateILInstruction(instruction, methodContext);
            }
            foreach (var sr in methodContext.StaticReference)
            {
                var constructor = sr.GetStaticConstructor();
                if (constructor == null)
                {
                    continue;
                }
                Writer.WriteLine($"{constructor.CXXMethodCallName(sr)}();");
            }
            foreach (var instruction in Method.Body.Instructions)
            {
                Writer.WriteLine(NoteILInstruction(instruction, methodContext));
                var Lable = instruction.GetLabel();
                if (methodContext.LableReference.Contains(Lable))
                {
                    Writer.WriteLineRaw(Lable + " :");
                }
                Writer.WriteLine(TranslateILInstruction(instruction, methodContext));
            }
            if (Method.IsConstructor && Method.IsStatic)
            {
                Writer.unindent();
                Writer.WriteLine("});");
                Writer.WriteLine("// [S2000-2] Static Constructor Body End");
            }
            Writer.unindent();
            Writer.WriteLine("}");
        }
Exemple #6
0
        public void WriteTypeRecursively(CodeTextWriter codeWriter, Metadata.TypeInformation type)
        {
            if (type.HasGenericParameters)
            {
                codeWriter.WriteLine($"template<{type.CXXTemplateParam}>");
            }
            string Interfaces = string.Join(',', type.Interfaces.Select(a => $"public {a.CXXTypeName}"));

            if (type.Interfaces.Count > 0)
            {
                Interfaces = "," + Interfaces;
            }
            string BaseType = type.BaseType != null ? type.BaseType.CXXTypeName : "RTCLI::System::Object";

            using (var classScope = new CXXScopeDisposer(codeWriter,
                                                         type.IsStruct ?
                                                         $"struct {type.CXXTypeNameShort}"
                               : $"class {type.CXXTypeNameShort} : public {BaseType}{Interfaces}",

                                                         true))
            {
                codeWriter.unindent().WriteLine("public:").indent();
                foreach (var nested in type.Nested)
                {
                    WriteTypeRecursively(codeWriter, nested);
                }
                foreach (var method in type.Methods)
                {
                    if (method.HasGenericParameters)
                    {
                        codeWriter.WriteLine($"template<{method.CXXTemplateParam}>");
                    }

                    codeWriter.WriteLine($"{(method.IsNewSlot?"virtual ":"")}{method.CXXMethodSignature(true)};");
                }
                foreach (var field in type.Fields)
                {
                    codeWriter.WriteLine(field.CXXFieldDeclaration);
                }
            }

            if (!type.IsStruct)
            {
                return;
            }

            if (type.HasGenericParameters)
            {
                codeWriter.WriteLine($"template<{type.CXXTemplateParam}>");
            }
            string classDef = $"class {type.CXXTypeNameShort}_V : public RTCLI::System::ValueType{Interfaces}";

            using (var classScope = new CXXScopeDisposer(codeWriter, classDef, true))
            {
                codeWriter.unindent().WriteLine("public:").indent();
                codeWriter.WriteLine($"using ValueType = {type.CXXTypeNameShort};");
                //codeWriter.WriteLine($"using ValueType = struct {type.CXXTypeNameShort};");
                codeWriter.WriteLine($"{type.CXXTypeNameShort} value;");
                foreach (var method in type.Methods)
                {
                    if (method.HasGenericParameters)
                    {
                        codeWriter.WriteLine($"template<{method.CXXTemplateParam}>");
                    }
                    codeWriter.WriteLine($"RTCLI_FORCEINLINE {method.CXXMethodSignature(true)} {{ value.{method.CXXMethodNameShort}{method.CXXArgSequence}; }}");
                }
            }
        }
Exemple #7
0
        public void WriteMethodRecursive(CodeTextWriter codeWriter, Metadata.TypeInformation type)
        {
            foreach (var nested in type.Nested)
            {
                WriteMethodRecursive(codeWriter, nested);
            }
            foreach (var method in type.Methods)
            {
                if (method.Body == null)
                {
                    continue;
                }
                CXXMethodTranslateContext methodContext = new CXXMethodTranslateContext(translateContext, method);

                // [2-2-1] Method Code
                if (type.HasGenericParameters)
                {
                    codeWriter.WriteLine($"template<{type.CXXTemplateParam}>");
                }
                if (method.HasGenericParameters)
                {
                    codeWriter.WriteLine($"template<{method.CXXTemplateParam}>");
                }

                codeWriter.WriteLine(
                    method.CXXRetType + " " + method.CXXMethodDeclareName + method.CXXParamSequence(false));

                codeWriter.WriteLine("{");
                // [2-2-2] Code Body
                codeWriter.indent();
                if (method.IsConstructor && method.IsStatic)
                {
                    codeWriter.WriteLine("static std::once_flag flag;");
                    codeWriter.WriteLine("std::call_once(flag,[&]()");
                    codeWriter.WriteLine("{");
                    codeWriter.indent();
                }
                foreach (var localVar in method.LocalVariables)
                {
                    if (method.InitLocals)
                    {
                        codeWriter.WriteLine($"{localVar.CXXVarDeclaration} v{localVar.Index} = {localVar.CXXVarInitVal};");
                    }
                    else
                    {
                        codeWriter.WriteLine($"{localVar.CXXVarDeclaration} v{localVar.Index};");
                    }
                }
                foreach (var instruction in method.Body.Instructions)
                {
                    codeWriter.WriteLine(NoteILInstruction(instruction, methodContext));
                    codeWriter.WriteLine(
                        instruction.GetLabel() + ": " +
                        TranslateILInstruction(instruction, methodContext));
                }
                if (method.IsConstructor && method.IsStatic)
                {
                    codeWriter.unindent();
                    codeWriter.WriteLine("});");
                }
                codeWriter.unindent();
                codeWriter.WriteLine("}");
            }
        }