diff --git a/codegen.pri b/codegen.pri index 80749f6..7612eb5 100644 --- a/codegen.pri +++ b/codegen.pri @@ -4,7 +4,7 @@ Codegen.name = Codegen Codegen.input = SOURCES Codegen.output = $$CODEGEN_OUT_PATH Codegen.variable_out = SOURCES -Codegen.commands = python $$CODEGEN_DIR/codegen.py -pwd $$CODEGEN_SRC_PATH -cmdinput -include $$INCLUDEPATH -sourcefiles $$SOURCES $$HEADERS -o $$CODEGEN_OUT_PATH +Codegen.commands = python3 $$CODEGEN_DIR/codegen.py -pwd $$CODEGEN_SRC_PATH -cmdinput -include $$INCLUDEPATH -sourcefiles $$SOURCES $$HEADERS -o $$CODEGEN_OUT_PATH Codegen.CONFIG += target_predeps combine QMAKE_EXTRA_COMPILERS += Codegen DEFINES += WITH_CODEGEN diff --git a/codegen.py b/codegen.py index 077b4d2..51c4b90 100644 --- a/codegen.py +++ b/codegen.py @@ -111,17 +111,18 @@ def DebugPrint(self): class ScopedDeclare: """ Recursive class that represents a scoped forward declare in the generated source code """ - def __init__(self, TypeName, Name): + def __init__(self, TypeName, Name, UnderlyingType=None): self.TypeName = TypeName self.Name = Name self.Children = [] - - def AddChild(self, TypeName, Name): + self.UnderlyingType = UnderlyingType + + def AddChild(self, TypeName, Name, UnderlyingType=None): for Child in self.Children: if Child.TypeName == TypeName and Child.Name == Name: return Child - - NewDeclare = ScopedDeclare(TypeName, Name) + + NewDeclare = ScopedDeclare(TypeName, Name, UnderlyingType) self.Children.append(NewDeclare) return self.Children[-1] @@ -131,8 +132,9 @@ def GenerateText(self, Indentation): if self.Name: IndentText = '\t' * Indentation LineEnd = " {" if self.Children else ";" - OutText = "%s%s %s%s\n" % (IndentText, self.TypeName, self.Name, LineEnd) - + UTSuffix = ": " + self.UnderlyingType if self.UnderlyingType else "" + OutText = "%s%s %s%s%s\n" % (IndentText, self.TypeName, self.Name, UTSuffix, LineEnd) + for Child in self.Children: OutText += Child.GenerateText(Indentation + 1) @@ -233,17 +235,16 @@ def CursorRecurse(self, Cursor, Depth): break Parent = Parent.semantic_parent - - if DeclarationCursors: - Declaration = self.RootDeclare - - for i in range(len(DeclarationCursors)-1, -1, -1): - Decl = DeclarationCursors[i] - TypeName = "namespace" if Decl.kind is clang.cindex.CursorKind.NAMESPACE else "struct" - Declaration = Declaration.AddChild(TypeName, Decl.spelling) - - Declaration.AddChild("enum", NewEnum.Name) - + + Declaration = self.RootDeclare + for i in range(len(DeclarationCursors)-1, -1, -1): + Decl = DeclarationCursors[i] + TypeName = "namespace" if Decl.kind is clang.cindex.CursorKind.NAMESPACE else "struct" + Declaration = Declaration.AddChild(TypeName, Decl.spelling) + + UnderlyingType = Child.enum_type.get_canonical().spelling + Declaration.AddChild("enum", NewEnum.Name, UnderlyingType) + # currently we're only testing enums - recurse for non-enum types to look for more enums else: self.CursorRecurse(Child, Depth + 1) @@ -308,7 +309,10 @@ def RunCodegen(): # Clang index #@todo - this definitely isn't portable??? how should I be setting this??? - clang.cindex.Config.set_library_file('C:\\Program Files\\LLVM\\bin\\libclang.dll') + if sys.platform == "win32": + clang.cindex.Config.set_library_file('C:\\Program Files\\LLVM\\bin\\libclang.dll') + elif sys.platform == "darwin": + clang.cindex.Config.set_library_file('/usr/local/opt/llvm@6/lib/libclang.dylib') ClangIndex = clang.cindex.Index.create() # C++ environment @@ -358,4 +362,4 @@ def RunCodegen(): if EnableProfiling: cProfile.run('RunCodegen()') else: - RunCodegen() \ No newline at end of file + RunCodegen() diff --git a/template.mako b/template.mako index 43eff67..d245c2d 100644 --- a/template.mako +++ b/template.mako @@ -7,7 +7,8 @@ ${Decl.GenerateText(0)} % endfor % for Enum in Enums: -const CEnumNameMap TEnumReflection::skNameMap = { +template<> +const CEnumNameMap TEnumReflection<${Enum.FullName}>::skNameMap = { <% ValueSet = set() %> \ % for Constant in Enum.Constants: % if Constant.Value not in ValueSet: @@ -16,6 +17,7 @@ const CEnumNameMap TEnumReflection::skNameMap = { % endif % endfor }; -const int TEnumReflection::skErrorValue = ${Enum.ErrorValue}; +template<> +const int TEnumReflection<${Enum.FullName}>::skErrorValue = ${Enum.ErrorValue}; % endfor