public void WriteHeader(CodeTextStorage storage) { using (var _ = storage.EnterScope(translateContext.FocusedAssemblyInformation.IdentName)) { foreach (var module in translateContext.FocusedAssemblyInformation.Modules.Values) { var totalHeaderWriter = storage.Wirter(module.CXXHeaderName + ".h"); totalHeaderWriter.WriteLine("#pragma once"); totalHeaderWriter.WriteLine(EnvIncludes); foreach (var type in module.Types.Values) { var codeWriter = storage.Wirter(type.TypeName + ".h"); typeHeaderWriters[type.FullName] = codeWriter; codeWriter.WriteLine("#pragma once"); codeWriter.WriteLine(EnvIncludes); using (var ___ = new CXXScopeDisposer(codeWriter, "\nnamespace " + type.CXXNamespace)) { WriteTypeRecursively(codeWriter, type); } typeHeaderWriters[type.FullName].Flush(); totalHeaderWriter.WriteLine($"#include \"{type.TypeName}.h\""); } totalHeaderWriter.Flush(); } }//End Dispose EnterScope }
public void WriteSource(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 + ".cpp"); typeSourceWriters[type.FullName] = codeWriter; codeWriter.WriteLine(EnvIncludes); codeWriter.WriteLine($"#include <{type.TypeName}.h>"); foreach (var method in type.Methods) { CXXMethodTranslateContext methodContext = new CXXMethodTranslateContext(translateContext, method); // [2-1] Stack Code codeWriter.WriteLine($"\n//{method.CXXMethodName}\n//[2-1] Here Begins Stack Declaration"); codeWriter.WriteLine($"struct {method.CXXStackName}"); codeWriter.WriteLine("{"); codeWriter.indent(); foreach (var localVar in method.LocalVariables) { codeWriter.WriteLine($"{localVar.CXXTypeName} v{localVar.Index};"); } codeWriter.WriteLine("template<bool InitLocals> static void Init(){};//Active with MethodBody.InitLocals Property."); codeWriter.unindent(); codeWriter.WriteLine("};\n"); // [2-2-1] Method Code codeWriter.WriteLine("//[2-2] Here Begins Method Body"); codeWriter.WriteLine( method.CXXRetType + " " + method.CXXMethodName + method.CXXParamSequence); codeWriter.WriteLine("{"); // [2-2-2] Code Body codeWriter.indent(); codeWriter.WriteLine($"{method.CXXStackName} stack;"); codeWriter.WriteLine($"stack.Init<{method.InitLocals.ToString().ToLower()}>();"); foreach (var instruction in method.Body.Instructions) { codeWriter.WriteLine(NoteILInstruction(instruction, methodContext)); codeWriter.WriteLine(TranslateILInstruction(instruction, methodContext)); } codeWriter.unindent(); codeWriter.WriteLine("}"); } typeSourceWriters[type.FullName].Flush(); } } }//End Dispose EnterScope }
public void WriteSource(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 + ".cpp"); typeSourceWriters[type.FullName] = codeWriter; codeWriter.WriteLine(EnvIncludes); codeWriter.WriteLine($"#include <{translateContext.FocusedAssemblyInformation.IdentName}/{type.TypeName}.h>"); codeWriter.WriteLine($"#if defined(RTCLI_COMPILER_CLANG)"); codeWriter.WriteLine($"#pragma clang diagnostic push"); codeWriter.WriteLine($"#pragma clang diagnostic ignored \"-Wunused-label\""); codeWriter.WriteLine($"#elif defined(RTCLI_COMPILER_MSVC)"); codeWriter.WriteLine($"#pragma warning(push)"); codeWriter.WriteLine($"#pragma warning(disable: 4102)"); codeWriter.WriteLine($"#endif"); WriteMethodRecursive(codeWriter, type); codeWriter.WriteLine($"#if defined(RTCLI_COMPILER_CLANG)"); codeWriter.WriteLine($"#pragma clang diagnostic pop"); codeWriter.WriteLine($"#elif defined(RTCLI_COMPILER_MSVC)"); codeWriter.WriteLine($"#pragma warning(pop)"); codeWriter.WriteLine($"#endif"); codeWriter.WriteLine($"#ifdef RTCLI_TEST_POINT"); codeWriter.WriteLine("int main(void){"); codeWriter.WriteLine($"\t{type.CXXTypeName}::Test();"); codeWriter.WriteLine("\treturn 0;"); codeWriter.WriteLine("}"); codeWriter.WriteLine($"#endif"); typeSourceWriters[type.FullName].Flush(); } } }//End Dispose EnterScope }
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(); } } } } }
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)) { 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); } } } return(sourceFiles.ToArray()); }
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()); }
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 }