예제 #1
0
        public static void WriteCommonHeader(
            CodeTextStorage storage,
            TranslateContext translateContext,
            PreparedInformations prepared,
            string assemblyName)
        {
            IExtractContext extractContext      = translateContext;
            var             assemblyMangledName = Utilities.GetMangledName(assemblyName);

            using (var twHeader = storage.CreateHeaderWriter(assemblyName))
            {
                twHeader.WriteLine(
                    "// [13-1] This is {0} native code translated by IL2C, do not edit.",
                    assemblyName);
                twHeader.SplitLine();

                twHeader.WriteLine(
                    "#ifndef __{0}_H__",
                    assemblyMangledName);
                twHeader.WriteLine(
                    "#define __{0}_H__",
                    assemblyMangledName);
                twHeader.SplitLine();
                twHeader.WriteLine("#pragma once");
                twHeader.SplitLine();

                // Write assembly references.
                var assemblies = extractContext.EnumerateRegisteredTypes().
                                 SelectMany(entry => entry.Value).
                                 Distinct().
                                 OrderByDependant(translateContext.Assembly).
                                 Select(type => type.DeclaringModule.DeclaringAssembly).
                                 Where(assembly => !assembly.Equals(translateContext.Assembly)).
                                 Distinct().
                                 ToArray();
                if (assemblies.Length >= 1)
                {
                    twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                    twHeader.WriteLine("// [13-2] Assembly references:");
                    twHeader.SplitLine();

                    foreach (var assembly in assemblies)
                    {
                        twHeader.WriteLine("#include <{0}.h>", assembly.Name);
                    }
                    twHeader.SplitLine();
                }

                // Write native headers from the NativeType/NativeMethod/NativeValue attributes.
                var importFileNames = extractContext.EnumerateRequiredImportIncludeFileNames().ToArray();
                if (importFileNames.Length >= 1)
                {
                    twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                    twHeader.WriteLine("// [13-3] Import native headers:");
                    twHeader.SplitLine();

                    foreach (var fileName in importFileNames)
                    {
                        twHeader.WriteLine("#include <{0}>", fileName);
                    }
                    twHeader.SplitLine();
                }

                var types = prepared.Types.
                            Where(type => type.DeclaringType == null).
                            OrderByDependant(translateContext.Assembly).
                            ToArray();
                if (types.Length >= 1)
                {
                    // Write pre definitions of type.
                    twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                    twHeader.WriteLine("// [13-4] Type pre definitions:");
                    twHeader.SplitLine();

                    foreach (var type in types)
                    {
                        twHeader.WriteLine(
                            "#include \"{0}/{1}/{2}.h\"",
                            assemblyName,
                            Utilities.GetCLanguageScopedPath(type.ScopeName),
                            type.Name);
                    }
                    twHeader.SplitLine();

                    // Write body definitions of type.
                    twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                    twHeader.WriteLine("// [13-5] Type body definitions:");
                    twHeader.SplitLine();

                    twHeader.WriteLine(
                        "#define {0}_DECL_TYPE_BODY__ 1",
                        assemblyMangledName);
                    twHeader.SplitLine();

                    foreach (var type in types)
                    {
                        twHeader.WriteLine(
                            "#include \"{0}/{1}/{2}.h\"",
                            assemblyName,
                            Utilities.GetCLanguageScopedPath(type.ScopeName),
                            type.Name);
                    }
                    twHeader.SplitLine();
                }

                twHeader.WriteLine("#endif");
                twHeader.Flush();
            }
        }
예제 #2
0
        public static string[] WriteSourceCodes(
            CodeTextStorage storage,
            TranslateContext translateContext,
            PreparedInformations prepared,
            DebugInformationOptions debugInformationOption)
        {
            IExtractContextHost extractContext = translateContext;
            var assemblyName = extractContext.Assembly.Name;

            var typesByDeclaring = prepared.Types.
                                   GroupBy(type => type.DeclaringType ?? type).
                                   ToDictionary(
                g => g.Key,
                g => g.OrderByDependant(translateContext.Assembly).ToArray());

            var sourceFiles = new List <string>();

            foreach (var targetType in prepared.Types.
                     Where(type => type.DeclaringType == null))
            {
                using (var _ = storage.EnterScope(targetType.ScopeName))
                {
                    using (var twSource = storage.CreateSourceCodeWriter(targetType.Name))
                    {
                        // HACK: Unreal Engine 4 needs include directive with same file name as header extension (ex: foo.c --> foo.h) at first line.
                        if (extractContext.TargetPlatform == TargetPlatforms.UE4)
                        {
                            twSource.WriteLine(
                                "#include \"{0}.h\"   // [16-1] Needs for Unreal Engine 4.",
                                targetType.Name);
                            twSource.SplitLine();
                        }

                        twSource.WriteLine(
                            "// [15-2] This is {0} native code translated by IL2C, do not edit.",
                            assemblyName);
                        twSource.SplitLine();

                        twSource.WriteLine(
                            "#include <{0}.h>",
                            assemblyName);
                        twSource.WriteLine(
                            "#include <{0}_internal.h>",
                            assemblyName);
                        twSource.SplitLine();

                        // Write assembly references at the file scope.
                        InternalWriteAssemblyReferences(
                            twSource,
                            translateContext,
                            extractContext,
                            targetType);

                        twSource.WriteLine("#ifdef __cplusplus");
                        twSource.WriteLine("extern \"C\" {");
                        twSource.WriteLine("#endif");
                        twSource.SplitLine();

                        InternalWriteSourceCode(
                            twSource,
                            extractContext,
                            prepared,
                            targetType,
                            debugInformationOption,
                            typesByDeclaring);

                        twSource.WriteLine("#ifdef __cplusplus");
                        twSource.WriteLine("}");
                        twSource.WriteLine("#endif");
                        twSource.SplitLine();

                        twSource.Flush();

                        sourceFiles.Add(twSource.RelatedPath);
                    }

                    // HACK: Unreal Engine 4 needs include directive with same file name as header extension (ex: foo.c --> foo.h) at first line.
                    if (extractContext.TargetPlatform == TargetPlatforms.UE4)
                    {
                        using (var twUE4Header = storage.CreateHeaderWriter(targetType.Name))
                        {
                            twUE4Header.WriteLine(
                                "// [16-2] This is {0} native code translated by IL2C, do not edit.",
                                assemblyName);
                            twUE4Header.WriteLine(
                                "// It's a dummy header file for helping and using only Unreal Engine 4.",
                                assemblyName);

                            twUE4Header.Flush();
                        }
                    }
                }
            }

            return(sourceFiles.ToArray());
        }
예제 #3
0
        public static string WriteCommonInternalSourceCode(
            CodeTextStorage storage,
            TranslateContext translateContext,
            PreparedInformations prepared,
            string assemblyName)
        {
            IExtractContext extractContext = translateContext;

            using (var twSource = storage.CreateSourceCodeWriter(assemblyName + "_internal"))
            {
                var assemblyMangledName = Utilities.GetMangledName(assemblyName);

                twSource.WriteLine(
                    "// [15-1] This is {0} native code translated by IL2C, do not edit.",
                    assemblyName);
                twSource.SplitLine();

                twSource.WriteLine(
                    "#include <{0}.h>",
                    assemblyName);
                twSource.WriteLine(
                    "#include <{0}_internal.h>",
                    assemblyName);
                twSource.SplitLine();

                var constStrings = extractContext.
                                   ExtractConstStrings().
                                   ToArray();
                if (constStrings.Length >= 1)
                {
                    twSource.WriteLine("//////////////////////////////////////////////////////////////////////////////////");
                    twSource.WriteLine("// [9-1-2] Const strings:");
                    twSource.SplitLine();

                    foreach (var(symbolName, value) in constStrings)
                    {
                        twSource.WriteLine(
                            "IL2C_CONST_STRING({0}, {1});",
                            symbolName,
                            Utilities.GetCLanguageExpression(value));
                    }

                    twSource.SplitLine();
                }

                var declaredValues = extractContext.
                                     ExtractDeclaredValues().
                                     ToArray();
                if (declaredValues.Length >= 1)
                {
                    twSource.WriteLine("//////////////////////////////////////////////////////////////////////////////////");
                    twSource.WriteLine("// [12-1-2] Declared values:");
                    twSource.SplitLine();

                    foreach (var information in declaredValues)
                    {
                        foreach (var declaredFields in information.DeclaredFields)
                        {
                            twSource.WriteLine(
                                "// {0}",
                                declaredFields.FriendlyName);
                        }

                        var targetType = (information.HintTypes.Length == 1) ?
                                         information.HintTypes[0] :
                                         extractContext.MetadataContext.ByteType.MakeArray();
                        Debug.Assert(targetType.IsArray);

                        var elementType = targetType.ElementType.ResolveToRuntimeType();
                        var values      = Utilities.ResourceDataToSpecificArray(information.ResourceData, elementType);

                        var lhs = targetType.GetCLanguageTypeName(information.SymbolName, true);
                        twSource.WriteLine(
                            "const {0} =",
                            lhs);
                        using (var _ = twSource.Shift())
                        {
                            twSource.WriteLine(
                                "{0};",
                                Utilities.GetCLanguageExpression(values));
                        }
                    }

                    twSource.SplitLine();
                }

                twSource.Flush();

                return(twSource.RelatedPath);
            }
        }
예제 #4
0
 public void WriteHeader(CodeTextStorage storage)
 {
     using (var _ = storage.EnterScope(translateContext.FocusedAssemblyInformation.IdentName))
     {
         foreach (var module in translateContext.FocusedAssemblyInformation.Modules.Values)
         {
             foreach (var type in module.Types.Values)
             {
                 var codeWriter = storage.Wirter(type.TypeName + ".h");
                 typeHeaderWriters[type.FullName] = codeWriter;
                 codeWriter.WriteLine(EnvIncludes);
                 using (var ___ = new CXXScopeDisposer(codeWriter, "\nnamespace " + type.CXXNamespace))
                 {
                     using (var classScope = new CXXScopeDisposer(codeWriter, $"RTCLI_API class {type.CXXTypeNameShort} : public RTCLI::System::Object", true))
                     {
                         codeWriter.WriteLine("public:");
                         foreach (var method in type.Methods)
                         {
                             if (method.IsPublic)
                             {
                                 codeWriter.WriteLine(method.CXXMethodSignature);
                             }
                         }
                         foreach (var field in type.Fields)
                         {
                             if (field.IsPublic)
                             {
                                 codeWriter.WriteLine(field.CXXFieldDeclaration);
                             }
                         }
                         codeWriter.WriteLine("private:");
                         foreach (var method in type.Methods)
                         {
                             if (method.IsPrivate)
                             {
                                 codeWriter.WriteLine(method.CXXMethodSignature);
                             }
                         }
                         foreach (var field in type.Fields)
                         {
                             if (field.IsPrivate)
                             {
                                 codeWriter.WriteLine(field.CXXFieldDeclaration);
                             }
                         }
                         codeWriter.WriteLine("protected:");
                         foreach (var method in type.Methods)
                         {
                             if (method.IsFamily)
                             {
                                 codeWriter.WriteLine(method.CXXMethodSignature);
                             }
                         }
                         foreach (var field in type.Fields)
                         {
                             if (field.IsFamily)
                             {
                                 codeWriter.WriteLine(field.CXXFieldDeclaration);
                             }
                         }
                     }
                 }
                 typeHeaderWriters[type.FullName].Flush();
             }
         }
     }//End Dispose EnterScope
 }