Exemplo n.º 1
0
        public static void WriteHeaders(
            CodeTextStorage storage,
            TranslateContext translateContext,
            PreparedInformations prepared)
        {
            IExtractContext extractContext      = translateContext;
            var             assemblyName        = translateContext.Assembly.Name;
            var             assemblyMangledName = translateContext.Assembly.MangledName;

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

            foreach (var g in prepared.Types.
                     Where(type => type.DeclaringType == null).
                     GroupBy(type => type.ScopeName))
            {
                using (var _ = storage.EnterScope(g.Key))
                {
                    foreach (var type in g)
                    {
                        using (var twHeader = storage.CreateHeaderWriter(type.Name))
                        {
                            var scopeName = Utilities.GetMangledName(type.ScopeName);

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

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

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

                            twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                            twHeader.WriteLine("// [14-2] Type pre definitions:");
                            twHeader.SplitLine();

                            // All types exclude privates
                            WriteTypePreDefinitions(
                                twHeader,
                                type,
                                typesByDeclaring);

                            twHeader.WriteLine("///////////////////////////////////////////////////////////////////////////");
                            twHeader.WriteLine("// [14-3] Type body definitions:");
                            twHeader.SplitLine();

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

                            InternalWriteHeader(
                                twHeader,
                                prepared,
                                type,
                                MemberScopes.Public,
                                typesByDeclaring);

                            twHeader.WriteLine("#endif");
                            twHeader.SplitLine();

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

                            twHeader.Flush();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
0
        public static void WriteCommonInternalHeader(
            CodeTextStorage storage,
            TranslateContext translateContext,
            PreparedInformations prepared,
            string assemblyName)
        {
            IExtractContext extractContext               = translateContext;
            var             annotatedAssemblyName        = assemblyName + "_internal";
            var             annotatedAssemblyMangledName = Utilities.GetMangledName(annotatedAssemblyName);

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

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

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

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

                    foreach (var(symbolName, _) in constStrings)
                    {
                        twHeader.WriteLine(
                            "extern System_String* const {0};",
                            symbolName);
                    }
                    twHeader.SplitLine();
                }

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

                    foreach (var information in declaredValues)
                    {
                        foreach (var declaredFields in information.DeclaredFields)
                        {
                            twHeader.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);
                        twHeader.WriteLine(
                            "extern const {0};",
                            lhs);
                    }
                    twHeader.SplitLine();
                }

                twHeader.WriteLine("#endif");
                twHeader.Flush();
            }
        }
Exemplo n.º 4
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());
        }