diff options
author | rtk0c <[email protected]> | 2022-05-30 23:42:02 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-30 23:42:02 -0700 |
commit | 8a0f2cd0b398ee0b7740e44a0e5fb2f75d090ccb (patch) | |
tree | 2838fd73313948f4db156b60c079468314eb1564 /source/20-codegen-compiler/main.cpp | |
parent | 3571627772b245e3e1a45b66e9a8fe4d50d06c8c (diff) |
Changeset: 59 Integrate enum codegen into the actual project
Diffstat (limited to 'source/20-codegen-compiler/main.cpp')
-rw-r--r-- | source/20-codegen-compiler/main.cpp | 58 |
1 files changed, 25 insertions, 33 deletions
diff --git a/source/20-codegen-compiler/main.cpp b/source/20-codegen-compiler/main.cpp index b139515..874cacb 100644 --- a/source/20-codegen-compiler/main.cpp +++ b/source/20-codegen-compiler/main.cpp @@ -27,13 +27,10 @@ using namespace std::literals; namespace fs = std::filesystem; -// TODO handle namespace // TODO support codegen target in .cpp files struct AppState { std::string_view outputDir; - CodegenOutput mainHeaderOutput; - CodegenOutput mainSourceOutput; }; enum { @@ -252,30 +249,28 @@ BSTR_LUT_DECL(EnumMetaGenOptions, 0, EMGO_COUNT) { BSTR_LUT_MAP(EMGO_ExcludeUseHeuristics, "ExcludeHeuristics"); } -std::string GenerateEnumStringArray(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) { - std::string arrayName; - APPEND_FMT(arrayName, "gCG_%s_Val2Str", decl.name.c_str()); +std::string GenerateEnumStringArray(CodegenOutput& out, const DeclEnum& decl, const std::vector<DeclEnumElement>& filteredElements, bool useHeruistics) { + INPLACE_FMT(arrayName, "gCG_%s_Val2Str", decl.name.c_str()); CodegenOutputThing thing; - APPEND_FMT_LN(thing.text, "const char* %s[] = {", arrayName.c_str()); - for (auto& elm : decl.elements) { - if (useHeruistics && elm.name.ends_with("COUNT")) { - continue; - } - + APPEND_FMT_LN(thing.text, "const char* %s[] = {", arrayName); + for (auto& elm : filteredElements) { APPEND_FMT_LN(thing.text, "\"%s\",", elm.name.c_str()); } APPEND_LIT_LN(thing.text, "};"); out.AddOutputThing(std::move(thing)); - return arrayName; + return std::string(arrayName); } std::string GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) { - std::string mapName; + INPLACE_FMT(mapName, "gCG_%s_Val2Str", decl.name.c_str()); + + CodegenOutputThing thing; // TODO + out.AddOutputThing(std::move(thing)); - return mapName; + return std::string(mapName); } void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const DeclEnum& decl, EnumFlags<EnumMetaGenOptions> options) { @@ -306,7 +301,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D switch (decl.GetPattern()) { case EVP_Continuous: { - auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics); + auto arrayName = GenerateEnumStringArray(sourceOut, decl, filteredElements, useExcludeHeuristics); int minVal = filteredElements.empty() ? 0 : filteredElements.front().value; int maxVal = filteredElements.empty() ? 0 : filteredElements.back().value; @@ -324,7 +319,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D } break; case EVP_Bits: { - auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics); + auto arrayName = GenerateEnumStringArray(sourceOut, decl, filteredElements, useExcludeHeuristics); // TODO } break; @@ -339,9 +334,8 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D if (options.IsSet(EMGO_FromString)) { // Generate string -> value lookup table - char mapName[1024]; // TODO mangle to prevent name conflicts of enum in different namespaces - snprintf(mapName, sizeof(mapName), "gCG_%s_Str2Val", decl.name.c_str()); + INPLACE_FMT(mapName, "gCG_%s_Str2Val", decl.name.c_str()); CodegenOutputThing lookupTable; { @@ -388,9 +382,13 @@ void HandleInputFile(AppState& state, std::string_view filenameStem, std::string CodegenInput cgInput; CodegenOutput cgHeaderOutput; - Utils::ProduceGeneratedHeaderFileHeader(cgHeaderOutput); CodegenOutput cgSourceOutput; - Utils::ProduceGeneratedSourceFileHeader(cgSourceOutput); + { + INPLACE_FMT(hpp, "%.*s.gh.inl", PRINTF_STRING_VIEW(filenameStem)); + INPLACE_FMT(cpp, "%.*s.gs.inl", PRINTF_STRING_VIEW(filenameStem)); + Utils::ProduceGeneratedHeader(hpp, cgHeaderOutput, cpp, cgSourceOutput); + } + CodegenOutput cgStandaloneSourceOutput; int currentBraceDepth = 0; // The current effective namespace, see example @@ -640,11 +638,12 @@ void HandleInputFile(AppState& state, std::string_view filenameStem, std::string printf("[WARNING] unbalanced brace at end of file."); } - Utils::WriteOutputFile(cgHeaderOutput, state.outputDir, filenameStem, ".gh.inl"sv); - Utils::WriteOutputFile(cgSourceOutput, state.outputDir, filenameStem, ".gs.inl"sv); - - // TODO see CMakeLists.txt for rationale, clean this up to be a proper citizen - Utils::WriteOutputFile(CodegenOutput{}, state.outputDir, filenameStem, ".gs.cpp"sv); + INPLACE_FMT(generatedHeaderInlName, "%.*s/%.*s.gh.inl", PRINTF_STRING_VIEW(state.outputDir), PRINTF_STRING_VIEW(filenameStem)); + Utils::WriteOutputFile(cgHeaderOutput, generatedHeaderInlName); + INPLACE_FMT(generatedSourceInlName, "%.*s/%.*s.gs.inl", PRINTF_STRING_VIEW(state.outputDir), PRINTF_STRING_VIEW(filenameStem)); + Utils::WriteOutputFile(cgSourceOutput, generatedSourceInlName); + INPLACE_FMT(generatedCppName, "%.*s/%.*s.g.cpp", PRINTF_STRING_VIEW(state.outputDir), PRINTF_STRING_VIEW(filenameStem)); + Utils::WriteOutputFile(cgStandaloneSourceOutput, generatedCppName); } enum InputOpcode { @@ -718,9 +717,6 @@ int main(int argc, char* argv[]) { AppState state; - Utils::ProduceGeneratedHeaderFileHeader(state.mainHeaderOutput); - Utils::ProduceGeneratedSourceFileHeader(state.mainSourceOutput); - // If no cli is provided (argv[0] conventionally but not mandatorily the cli), this will do thing // Otherwise, start with the 2nd element in the array, which is the 1st actual argument if (argc < 2) { @@ -753,9 +749,5 @@ where <output path>: the directory to write generated contents to. This will N } } - // TODO do we even need these? - // Utils::WriteOutputFile(state.mainHeaderOutput, state.outputDir, "GeneratedCode.hpp"sv); - // Utils::WriteOutputFile(state.mainSourceOutput, state.outputDir, "GeneratedCode.cpp"sv); - return 0; } |