From 1a6f1ea3b76c3ed4cad5aba5502af390ce50a2c0 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sat, 28 May 2022 20:52:42 -0700 Subject: Changeset: 42 Change codegen input parsing to lookahead based; lookup table infra; input/output decl infra --- buildtools/codegen/CodegenOutput.inl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 buildtools/codegen/CodegenOutput.inl (limited to 'buildtools/codegen/CodegenOutput.inl') diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl new file mode 100644 index 0000000..6d59301 --- /dev/null +++ b/buildtools/codegen/CodegenOutput.inl @@ -0,0 +1,31 @@ +#pragma once + +#include "CodegenDecl.hpp" + +#include +#include +#include +#include + +// A generic "thing" (could be anything, comments, string-concated functionsm, etc.) to spit into the output file +struct CodegenOutputThing { + std::string text; +}; + +class CodegenOutput { +private: + std::vector mOutThings; + +public: + void AddOutputThing(CodegenOutputThing thing) { + mOutThings.push_back(std::move(thing)); + } + + void MergeContents(CodegenOutput other) { + std::move(other.mOutThings.begin(), other.mOutThings.end(), this->mOutThings.begin()); + } + + void Write(FILE* file) { + // TODO + } +}; -- cgit v1.2.3-70-g09d2 From 66b63ae887f553e1cac813546a6b827a9c85d80c Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 29 May 2022 16:14:26 -0700 Subject: Changeset: 43 Add tostring code gen for enums --- buildtools/codegen/CodegenDecl.cpp | 48 ++++++ buildtools/codegen/CodegenDecl.hpp | 33 ++++ buildtools/codegen/CodegenInput.inl | 4 +- buildtools/codegen/CodegenOutput.inl | 18 ++- buildtools/codegen/main.cpp | 212 +++++++++++++++++++++---- buildtools/codegen/tests/examples/TestEnum.hpp | 17 +- source-common/Enum.hpp | 2 +- 7 files changed, 295 insertions(+), 39 deletions(-) create mode 100644 buildtools/codegen/CodegenDecl.cpp (limited to 'buildtools/codegen/CodegenOutput.inl') diff --git a/buildtools/codegen/CodegenDecl.cpp b/buildtools/codegen/CodegenDecl.cpp new file mode 100644 index 0000000..ac6bb01 --- /dev/null +++ b/buildtools/codegen/CodegenDecl.cpp @@ -0,0 +1,48 @@ +#include "CodegenDecl.hpp" + +#include + +static EnumValuePattern NextPattern(EnumValuePattern val) { + return (EnumValuePattern)(val + 1); +} + +EnumValuePattern DeclEnum::CalcPattern() const { + if (elements.empty()) return EVP_Continuous; + + auto pattern = EVP_Continuous; +restart: + auto lastVal = elements[0].value; + for (size_t i = 1; i < elements.size(); ++i) { + auto currVal = elements[i].value; + switch (pattern) { + case EVP_Continuous: { + bool satisfy = lastVal + 1 == currVal; + if (!satisfy) { + pattern = NextPattern(pattern); + goto restart; + } + } break; + + case EVP_Bits: { + bool satisfy = (lastVal << 1) == currVal; + if (!satisfy) { + pattern = NextPattern(pattern); + goto restart; + } + } break; + + // A random pattern can match anything + case EVP_Random: + case EVP_COUNT: break; + } + lastVal = currVal; + } + + return pattern; +} + +void DeclEnum::CalcPatternIfNecessary() { + if (pattern == EVP_COUNT) { + CalcPattern(); + } +} diff --git a/buildtools/codegen/CodegenDecl.hpp b/buildtools/codegen/CodegenDecl.hpp index 3414d80..c2d8dbb 100644 --- a/buildtools/codegen/CodegenDecl.hpp +++ b/buildtools/codegen/CodegenDecl.hpp @@ -20,8 +20,22 @@ enum EnumUnderlyingType { EUT_COUNT, }; +enum EnumValuePattern { + // The numbers cover n..m with no gaps + EVP_Continuous, + // The numbers cover for i in n..m, 1 << i + // e.g. [0] = 1 << 0, + // [1] = 1 << 1. + // [2] = 1 << 2. etc. + EVP_Bits, + // The numbesr don't have a particular pattern + EVP_Random, + EVP_COUNT, +}; + struct DeclEnumElement { std::string name; + // TODO support int64_t, etc. enum underlying types uint64_t value; }; @@ -29,4 +43,23 @@ struct DeclEnum { std::string name; std::vector elements; EnumUnderlyingType underlyingType; + // Start with invalid value, calculate on demand + EnumValuePattern pattern = EVP_COUNT; + + EnumValuePattern CalcPattern() const; + void CalcPatternIfNecessary(); +}; + +struct DeclFunctionArgument { + std::string type; + std::string name; +}; + +struct DeclFunction { + // Things like extern, static, etc. that gets written before the function return type + std::string prefix; + std::string name; + std::string returnType; + std::vector arguments; + std::string body; }; diff --git a/buildtools/codegen/CodegenInput.inl b/buildtools/codegen/CodegenInput.inl index 9fae43c..80a39d0 100644 --- a/buildtools/codegen/CodegenInput.inl +++ b/buildtools/codegen/CodegenInput.inl @@ -13,7 +13,7 @@ class CodegenInput { private: std::vector mEnums; - robin_hood::unordered_map mDeclByName; + robin_hood::unordered_map mDeclByName; public: void AddEnum(DeclEnum decl) { @@ -24,7 +24,7 @@ public: } #endif - mDeclByName.try_emplace(decl.name, mEnums.size()); + mDeclByName.try_emplace(std::string(decl.name), mEnums.size()); mEnums.push_back(std::move(decl)); } diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index 6d59301..752682c 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -15,6 +15,14 @@ struct CodegenOutputThing { class CodegenOutput { private: std::vector mOutThings; + std::vector mOutStructs; + std::vector mOutEnums; + std::vector mOutFunctions; + +public: + std::string optionOutPrefix; + // Whether to add prefixes mOutPrefix to all global names or not + bool optionAutoAddPrefix : 1 = false; public: void AddOutputThing(CodegenOutputThing thing) { @@ -22,10 +30,16 @@ public: } void MergeContents(CodegenOutput other) { - std::move(other.mOutThings.begin(), other.mOutThings.end(), this->mOutThings.begin()); + std::move(other.mOutThings.begin(), other.mOutThings.end(), std::back_inserter(this->mOutThings)); } void Write(FILE* file) { - // TODO +#define WRITE_LITERAL(str) fwrite(str, sizeof(char), sizeof(str) - 1, file) + for (auto& thing : mOutThings) { + WRITE_LITERAL("// Output thing\n"); + fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); + WRITE_LITERAL("\n"); + } +#undef WRITE_LITERAL } }; diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index 74acd1c..ed8cbbe 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -76,6 +76,7 @@ BSTR_LUT_DECL(CppKeyword, 0, CKw_COUNT) { BSTR_LUT_MAP_FOR(CppKeyword); BSTR_LUT_MAP(CKw_Struct, "struct"); BSTR_LUT_MAP(CKw_Class, "class"); + BSTR_LUT_MAP(CKw_Enum, "enum"); } enum CodegenDirective { @@ -161,6 +162,10 @@ PeekDirectiveArgumentList(const std::vector& tokens, size_t curre } } + if (!currentArg.empty()) { + result.push_back(std::move(currentArg)); + } + return { result, i }; } @@ -223,21 +228,95 @@ BSTR_LUT_DECL(StructMetaGenOptions, 0, SMGO_COUNT) { } enum EnumMetaGenOptions { - EMGO_Basic, + EMGO_ToString, + EMGO_ExcludeUseHeuristics, EMGO_COUNT, }; BSTR_LUT_DECL(EnumMetaGenOptions, 0, EMGO_COUNT) { BSTR_LUT_MAP_FOR(EnumMetaGenOptions); - BSTR_LUT_MAP(EMGO_Basic, "GenBasic"); + BSTR_LUT_MAP(EMGO_ToString, "ToString"); + BSTR_LUT_MAP(EMGO_ExcludeUseHeuristics, "ExcludeHeuristics"); +} + +// I give up, hopefully nothing overflows this buffer +#define APPEND_SPRINTF(out, format, ...) \ + { \ + char buffer[65536]; \ + snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \ + out += buffer; \ + } + +std::string GenerateEnumStringArray(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) { + std::string arrayName; + APPEND_SPRINTF(arrayName, "gCG_%s_StringTable", decl.name.c_str()); + + CodegenOutputThing thing; + APPEND_SPRINTF(thing.text, "const char* %s[] = {\n", arrayName.c_str()); + for (auto& elm : decl.elements) { + if (useHeruistics && elm.name.ends_with("COUNT")) { + continue; + } + + APPEND_SPRINTF(thing.text, "\"%s\",\n", elm.name.c_str()); + } + thing.text += "};\n"; + out.AddOutputThing(std::move(thing)); + + return arrayName; +} + +std::string GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) { + std::string mapName; + // TODO + + return mapName; } void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags options) { + bool useExcludeHeuristics = options.IsSet(EMGO_ExcludeUseHeuristics); + + if (options.IsSet(EMGO_ToString)) { + // Generate name lookup table + + switch (decl.CalcPattern()) { + case EVP_Continuous: { + auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); + int minVal = decl.elements.front().value; + int maxVal = decl.elements.back().value; + if (useExcludeHeuristics && + decl.elements.back().name.ends_with("COUNT") && + decl.elements.size() >= 2) + { + // Skip the last *_COUNT element if instructed to use heuristics + maxVal = decl.elements[decl.elements.size() - 2].value; + } + + CodegenOutputThing lookupFunction; + APPEND_SPRINTF(lookupFunction.text, "std::string_view Stringify%s(%s value) {\n", decl.name.c_str(), decl.name.c_str()); + APPEND_SPRINTF(lookupFunction.text, " if (value < 0 || value >= %d) return {};\n", maxVal); + APPEND_SPRINTF(lookupFunction.text, " return %s[value - %d];\n", arrayName.c_str(), minVal); + lookupFunction.text += "}\n"; + + out.AddOutputThing(std::move(lookupFunction)); + } break; + + case EVP_Bits: { + auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); + } break; + + case EVP_Random: { + auto mapName = GenerateEnumStringMap(out, decl, useExcludeHeuristics); + } break; + + case EVP_COUNT: break; + } + } } void HandleInputFile(AppState& state, std::string_view source) { auto tokens = RecordTokens(source); - size_t tokenIdx = 0; + size_t idx = 0; #if CODEGEN_DEBUG_PRINT printf("BEGIN tokens\n"); @@ -247,12 +326,12 @@ void HandleInputFile(AppState& state, std::string_view source) { printf("END tokens\n"); #endif - CodegenInput input; - CodegenOutput output; + CodegenInput fileInput; + CodegenOutput fileOutput; int bracePairDepth = 0; - while (tokenIdx < tokens.size()) { - auto& token = tokens[tokenIdx]; + while (idx < tokens.size()) { + auto& token = tokens[idx]; bool incrementTokenIdx = true; @@ -265,25 +344,77 @@ void HandleInputFile(AppState& state, std::string_view source) { if (iter != map.end()) { keyword = iter->second; } else { - break; + keyword = CKw_COUNT; // Skip keyword section } } switch (keyword) { case CKw_Struct: case CKw_Class: { - auto& idenTok = tokens[tokenIdx + 1]; // TODO handle end of list + auto& idenTok = tokens[idx + 1]; // TODO handle end of list DEBUG_PRINTF("[DEBUG] found struct named %s\n", idenTok.text.c_str()); - } break; + goto endIdenCase; + } case CKw_Enum: { - StbLexerToken* idenTok = &token + 1; // TODO handle end of list - if (idenTok->text == "class") { - idenTok += 1; - DEBUG_PRINTF("[DEBUG] found enum class named %s\n", idenTok->text.c_str()); + // Consume the "enum" keyword + ++idx; + incrementTokenIdx = false; + + DeclEnum enumDecl; + enumDecl.underlyingType = EUT_Int32; // TODO + + if (tokens[idx].text == "class") { + // Consume the "class" keyword + ++idx; + DEBUG_PRINTF("[DEBUG] found enum class named %s\n", tokens[idx].text.c_str()); } else { - DEBUG_PRINTF("[DEBUG] found enum named %s\n", idenTok->text.c_str()); + DEBUG_PRINTF("[DEBUG] found enum named %s\n", tokens[idx].text.c_str()); } - } break; + + // Consume the enum name identifier + enumDecl.name = tokens[idx].text; + ++idx; + + int enumClosingBraceCount = 0; + int enumBraceDepth = 0; + while (enumClosingBraceCount == 0 && idx < tokens.size()) { + auto& token = tokens[idx]; + switch (token.type) { + case CLEX_id: { + auto& vec = enumDecl.elements; + // Set to the previous enum element's value + 1, or starting from 0 if this is the first + // Also overridden in the CLEX_intlit branch + auto value = vec.empty() ? 0 : vec.back().value + 1; + vec.push_back(DeclEnumElement{ + .name = token.text, + .value = value, + }); + } break; + + case CLEX_intlit: { + + } break; + + case CLEX_ext_single_char: { + switch (token.text[0]) { + case '{': { + ++enumBraceDepth; + } break; + + case '}': { + --enumBraceDepth; + ++enumClosingBraceCount; + } break; + } + } break; + } + + ++idx; + } + + fileInput.AddEnum(std::move(enumDecl)); + goto endIdenCase; + } case CKw_COUNT: break; } @@ -295,24 +426,33 @@ void HandleInputFile(AppState& state, std::string_view source) { if (iter != map.end()) { directive = iter->second; } else { - break; + directive = CD_COUNT; // Skip directive section } } switch (directive) { case CD_ClassInfo: { // TODO - } break; + goto endIdenCase; + } case CD_EnumInfo: { + // Consume the directive + ++idx; + incrementTokenIdx = false; + auto& optionsStrMap = BSTR_LUT_S2V(EnumMetaGenOptions); - auto [argList, newIdx] = PeekDirectiveArgumentList(tokens, tokenIdx); + auto [argList, newIdx] = PeekDirectiveArgumentList(tokens, idx); if (argList.size() < 1) { printf("[ERROR] invalid syntax for BRUSSEL_ENUM\n"); - break; + break; // TODO handle this error case gracefully (advance to semicolon?) } auto& enumName = argList[0][0]->text; - auto enumDecl = input.FindEnumByName(enumName); + auto enumDecl = fileInput.FindEnumByName(enumName); + if (!enumDecl) { + printf("[ERROR] BRUSSEL_ENUM: referring to non-existent enum '%s'\n", enumName.c_str()); + break; + } auto& directiveOptions = argList[1]; EnumFlags options; @@ -321,19 +461,23 @@ void HandleInputFile(AppState& state, std::string_view source) { if (iter != optionsStrMap.end()) { options |= iter->second; } else { - printf("[ERROR] invalid option '%s' for BRUSSEL_ENUM", optionTok->text.c_str()); + printf("[ERROR] BRUSSEL_ENUM: invalid option '%s'\n", optionTok->text.c_str()); } } - GenerateForEnum(output, *enumDecl, options); + GenerateForEnum(fileOutput, *enumDecl, options); - tokenIdx = newIdx; + idx = newIdx; incrementTokenIdx = false; - } break; + goto endIdenCase; + } case CD_COUNT: break; } - } break; + + endIdenCase: + break; + } case '{': { bracePairDepth++; @@ -347,13 +491,15 @@ void HandleInputFile(AppState& state, std::string_view source) { } if (incrementTokenIdx) { - ++tokenIdx; + ++idx; } } if (bracePairDepth != 0) { printf("[WARNING] unbalanced brace at end of file."); } + + state.mainOutput.MergeContents(std::move(fileOutput)); } std::string ReadFileAtOnce(const fs::path& path) { @@ -436,12 +582,12 @@ int main(int argc, char* argv[]) { if (argc < 2) { // NOTE: keep in sync with various enum options and parser code printf(&R"""( -USAGE: codegen.exe [:]... -where : the _file_ to write generated contents to - is one of: - "single" process this file only - "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files -)"""[1]); + USAGE: codegen.exe [:]... + where : the _file_ to write generated contents to + is one of: + "single" process this file only + "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files + )"""[1]); return -1; } diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp b/buildtools/codegen/tests/examples/TestEnum.hpp index 2a93c01..c498cd9 100644 --- a/buildtools/codegen/tests/examples/TestEnum.hpp +++ b/buildtools/codegen/tests/examples/TestEnum.hpp @@ -5,4 +5,19 @@ enum MyEnum { EnumElement2, EnumElement3, }; -BRUSSEL_ENUM(MyEnum, GenBasic); +BRUSSEL_ENUM(MyEnum, ToString); + +enum CountedEnumAll { + CEA_Foo, + CEA_Bar, + CEA_COUNT, +}; +BRUSSEL_ENUM(CountedEnumAll, ToString); + +enum CountedEnum { + CE_Foo, + CE_Bar, + CE_FooBar, + CE_COUNT, +}; +BRUSSEL_ENUM(CountedEnum, ToString ExcludeHeuristics); diff --git a/source-common/Enum.hpp b/source-common/Enum.hpp index e8750f2..8ad75ba 100644 --- a/source-common/Enum.hpp +++ b/source-common/Enum.hpp @@ -18,7 +18,7 @@ public: } EnumFlags(TEnum e) - : mValue{ 1 << static_cast(e) } { + : mValue{ static_cast(1) << static_cast(e) } { } bool IsSet(EnumFlags mask) const { -- cgit v1.2.3-70-g09d2 From 5112858f9a4adcf76240bcddad19179a5c56d4f3 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 29 May 2022 18:30:03 -0700 Subject: Changeset: 47 Add fromstring codegen for enums --- buildtools/codegen/CodegenDecl.cpp | 5 +- buildtools/codegen/CodegenDecl.hpp | 4 +- buildtools/codegen/CodegenMacros.hpp | 30 ++++++++ buildtools/codegen/CodegenOutput.inl | 25 +++++-- buildtools/codegen/main.cpp | 98 +++++++++++++++++++------- buildtools/codegen/tests/examples/TestEnum.hpp | 6 +- 6 files changed, 130 insertions(+), 38 deletions(-) create mode 100644 buildtools/codegen/CodegenMacros.hpp (limited to 'buildtools/codegen/CodegenOutput.inl') diff --git a/buildtools/codegen/CodegenDecl.cpp b/buildtools/codegen/CodegenDecl.cpp index ac6bb01..7cf21ce 100644 --- a/buildtools/codegen/CodegenDecl.cpp +++ b/buildtools/codegen/CodegenDecl.cpp @@ -41,8 +41,9 @@ restart: return pattern; } -void DeclEnum::CalcPatternIfNecessary() { +EnumValuePattern DeclEnum::GetPattern() const { if (pattern == EVP_COUNT) { - CalcPattern(); + pattern = CalcPattern(); } + return pattern; } diff --git a/buildtools/codegen/CodegenDecl.hpp b/buildtools/codegen/CodegenDecl.hpp index c2d8dbb..b7c1ee4 100644 --- a/buildtools/codegen/CodegenDecl.hpp +++ b/buildtools/codegen/CodegenDecl.hpp @@ -44,10 +44,10 @@ struct DeclEnum { std::vector elements; EnumUnderlyingType underlyingType; // Start with invalid value, calculate on demand - EnumValuePattern pattern = EVP_COUNT; + mutable EnumValuePattern pattern = EVP_COUNT; EnumValuePattern CalcPattern() const; - void CalcPatternIfNecessary(); + EnumValuePattern GetPattern() const; }; struct DeclFunctionArgument { diff --git a/buildtools/codegen/CodegenMacros.hpp b/buildtools/codegen/CodegenMacros.hpp new file mode 100644 index 0000000..84c9d09 --- /dev/null +++ b/buildtools/codegen/CodegenMacros.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + +// I give up, hopefully nothing overflows this buffer +// TODO handle buffer sizing properly + +#define APPEND_LIT(out, str) out += str + +#define APPEND_FMT(out, format, ...) \ + { \ + char buffer[65536]; \ + snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \ + out += buffer; \ + } + +#define WRITE_LIT(file, str) fwrite(str, sizeof(char), sizeof(str) - 1, file) + +// NOTE: snprintf() returns the size written (given an infinite buffer) not including \0 +#define WRITE_FMT(file, format, ...) \ + { \ + char buffer[65536]; \ + int size = snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \ + fwrite(buffer, sizeof(char), std::min(size, sizeof(buffer)), file); \ + } + +#define APPEND_LIT_LN(out, str) APPEND_LIT(out, (str "\n")) +#define APPEND_FMT_LN(out, format, ...) APPEND_FMT(out, (format "\n"), __VA_ARGS__) +#define WRITE_LIT_LN(out, str) WRITE_LIT(out, (str "\n")) +#define WRITE_FMT_LN(out, format, ...) WRITE_FMT(out, (format "\n"), __VA_ARGS__) diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index 752682c..d1f2e50 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -1,7 +1,9 @@ #pragma once #include "CodegenDecl.hpp" +#include "CodegenMacros.hpp" +#include #include #include #include @@ -31,15 +33,30 @@ public: void MergeContents(CodegenOutput other) { std::move(other.mOutThings.begin(), other.mOutThings.end(), std::back_inserter(this->mOutThings)); + std::move(other.mOutStructs.begin(), other.mOutStructs.end(), std::back_inserter(this->mOutStructs)); + std::move(other.mOutEnums.begin(), other.mOutEnums.end(), std::back_inserter(this->mOutEnums)); + std::move(other.mOutFunctions.begin(), other.mOutFunctions.end(), std::back_inserter(this->mOutFunctions)); } void Write(FILE* file) { -#define WRITE_LITERAL(str) fwrite(str, sizeof(char), sizeof(str) - 1, file) for (auto& thing : mOutThings) { - WRITE_LITERAL("// Output thing\n"); + WRITE_LIT(file, "// Output thing\n"); fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); - WRITE_LITERAL("\n"); + WRITE_LIT(file, "\n"); + } + + for (auto& declStruct : mOutStructs) { + WRITE_FMT(file, "struct %s {\n", declStruct.name.c_str()); + // TODO + WRITE_LIT(file, "}\n"); + } + + for (auto& declEnum : mOutEnums) { + // TODO + } + + for (auto& declFunc : mOutFunctions) { + // TODO } -#undef WRITE_LITERAL } }; diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index 334b195..df52b66 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -1,5 +1,6 @@ #include "CodegenConfig.hpp" #include "CodegenDecl.hpp" +#include "CodegenMacros.hpp" #include "CodegenInput.inl" #include "CodegenOutput.inl" @@ -10,6 +11,8 @@ #include #include +#include +#include #include #include #include @@ -23,8 +26,11 @@ using namespace std::literals; namespace fs = std::filesystem; +// TODO handle namespace + struct AppState { - CodegenOutput mainOutput; + CodegenOutput headerOutput; + CodegenOutput sourceOutput; }; enum { @@ -229,6 +235,7 @@ BSTR_LUT_DECL(StructMetaGenOptions, 0, SMGO_COUNT) { enum EnumMetaGenOptions { EMGO_ToString, + EMGO_FromString, EMGO_ExcludeUseHeuristics, EMGO_COUNT, }; @@ -236,31 +243,24 @@ enum EnumMetaGenOptions { BSTR_LUT_DECL(EnumMetaGenOptions, 0, EMGO_COUNT) { BSTR_LUT_MAP_FOR(EnumMetaGenOptions); BSTR_LUT_MAP(EMGO_ToString, "ToString"); + BSTR_LUT_MAP(EMGO_FromString, "FromString"); BSTR_LUT_MAP(EMGO_ExcludeUseHeuristics, "ExcludeHeuristics"); } -// I give up, hopefully nothing overflows this buffer -#define APPEND_SPRINTF(out, format, ...) \ - { \ - char buffer[65536]; \ - snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \ - out += buffer; \ - } - std::string GenerateEnumStringArray(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) { std::string arrayName; - APPEND_SPRINTF(arrayName, "gCG_%s_StringTable", decl.name.c_str()); + APPEND_FMT(arrayName, "gCG_%s_Val2Str", decl.name.c_str()); CodegenOutputThing thing; - APPEND_SPRINTF(thing.text, "const char* %s[] = {\n", arrayName.c_str()); + 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_SPRINTF(thing.text, "\"%s\",\n", elm.name.c_str()); + APPEND_FMT_LN(thing.text, "\"%s\",", elm.name.c_str()); } - thing.text += "};\n"; + APPEND_LIT_LN(thing.text, "};"); out.AddOutputThing(std::move(thing)); return arrayName; @@ -277,9 +277,9 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags string lookup table and function - switch (decl.CalcPattern()) { + switch (decl.GetPattern()) { case EVP_Continuous: { auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); int minVal = decl.elements.front().value; @@ -293,25 +293,57 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags= %d) return {};\n", maxVal); - APPEND_SPRINTF(lookupFunction.text, " return %s[value - %d];\n", arrayName.c_str(), minVal); - lookupFunction.text += "}\n"; + auto& o = lookupFunction.text; + APPEND_FMT_LN(o, "std::string_view EnumToString_%s(%s value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o, " if (value < 0 || value >= %d) return {};", maxVal); + APPEND_FMT_LN(o, " return %s[value - %d];", arrayName.c_str(), minVal); + APPEND_LIT_LN(o, "}"); out.AddOutputThing(std::move(lookupFunction)); } break; case EVP_Bits: { auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); + // TODO } break; case EVP_Random: { auto mapName = GenerateEnumStringMap(out, decl, useExcludeHeuristics); + // TODO } break; case EVP_COUNT: break; } } + + if (options.IsSet(EMGO_FromString)) { + // Generate string -> value lookup table + char mapName[1024]; + snprintf(mapName, sizeof(mapName), "gCG_%s_Str2Val", decl.name.c_str()); + + CodegenOutputThing lookupTable; + auto& o1 = lookupTable.text; + APPEND_FMT_LN(o1, "constinit frozen::unordered_map %s = {", decl.name.c_str(), decl.elements.size(), mapName); + for (auto& elm : decl.elements) { + APPEND_FMT_LN(o1, "{\"%s\", %s::%s},", elm.name.c_str(), decl.name.c_str(), elm.name.c_str()); + } + APPEND_LIT_LN(o1, "};"); + + // Generate lookup function + CodegenOutputThing lookupFunction; + auto& o2 = lookupFunction.text; + APPEND_FMT_LN(o2, "std::optional<%s> EnumFromString_%s(std::string_view value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o2, " auto iter = %s.find(value);", mapName); + APPEND_FMT_LN(o2, " if (iter != %s.end()) {", mapName); + APPEND_LIT_LN(o2, " return iter->second;"); + APPEND_LIT_LN(o2, " } else {"); + APPEND_LIT_LN(o2, " return {};"); + APPEND_LIT_LN(o2, " }"); + APPEND_LIT_LN(o2, "}"); + + out.AddOutputThing(std::move(lookupTable)); + out.AddOutputThing(std::move(lookupFunction)); + } } void HandleInputFile(AppState& state, std::string_view source) { @@ -499,7 +531,7 @@ void HandleInputFile(AppState& state, std::string_view source) { printf("[WARNING] unbalanced brace at end of file."); } - state.mainOutput.MergeContents(std::move(fileOutput)); + state.sourceOutput.MergeContents(std::move(fileOutput)); } std::string ReadFileAtOnce(const fs::path& path) { @@ -577,17 +609,29 @@ int main(int argc, char* argv[]) { AppState state; + state.sourceOutput.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. + +#include +#include +#include +#include +using namespace std::literals; + )"""[1], + }); + // 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) { // NOTE: keep in sync with various enum options and parser code printf(&R"""( - USAGE: codegen.exe [:]... - where : the _file_ to write generated contents to - is one of: - "single" process this file only - "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files - )"""[1]); +USAGE: codegen.exe [:]... +where : the _file_ to write generated contents to + is one of: + "single" process this file only + "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files +)"""[1]); return -1; } @@ -615,7 +659,7 @@ int main(int argc, char* argv[]) { return -1; } DEFER { fclose(outputFile); }; - state.mainOutput.Write(outputFile); + state.sourceOutput.Write(outputFile); } return 0; diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp b/buildtools/codegen/tests/examples/TestEnum.hpp index c498cd9..6b4ab33 100644 --- a/buildtools/codegen/tests/examples/TestEnum.hpp +++ b/buildtools/codegen/tests/examples/TestEnum.hpp @@ -5,14 +5,14 @@ enum MyEnum { EnumElement2, EnumElement3, }; -BRUSSEL_ENUM(MyEnum, ToString); +BRUSSEL_ENUM(MyEnum, ToString FromString); enum CountedEnumAll { CEA_Foo, CEA_Bar, CEA_COUNT, }; -BRUSSEL_ENUM(CountedEnumAll, ToString); +BRUSSEL_ENUM(CountedEnumAll, ToString FromString); enum CountedEnum { CE_Foo, @@ -20,4 +20,4 @@ enum CountedEnum { CE_FooBar, CE_COUNT, }; -BRUSSEL_ENUM(CountedEnum, ToString ExcludeHeuristics); +BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); -- cgit v1.2.3-70-g09d2 From 6f29abe5571eb68207986bdadb97b207264ac958 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 29 May 2022 21:57:53 -0700 Subject: Changeset: 49 Convert codegen output to use template specialization --- CMakeLists.txt | 45 +++++-- README.md | 9 ++ buildtools/codegen/CodegenOutput.inl | 3 +- buildtools/codegen/main.cpp | 148 ++++++++++++++------- buildtools/codegen/tests/examples/TestEnum.hpp | 23 ---- buildtools/codegen/tests/examples/TestEnum.hpp.txt | 21 +++ source-codegen-base/MetadataBase.hpp | 14 ++ 7 files changed, 180 insertions(+), 83 deletions(-) delete mode 100644 buildtools/codegen/tests/examples/TestEnum.hpp create mode 100644 buildtools/codegen/tests/examples/TestEnum.hpp.txt create mode 100644 source-codegen-base/MetadataBase.hpp (limited to 'buildtools/codegen/CodegenOutput.inl') diff --git a/CMakeLists.txt b/CMakeLists.txt index 19518bf..0e8dc26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,10 +15,10 @@ add_subdirectory(3rdparty/tracy) # ============================================================================== -file(GLOB_RECURSE commonthings_SOURCES source-common/*.c source-common/*.cpp) -add_library(commonthings OBJECT ${commonthings_SOURCES}) +file(GLOB_RECURSE things_common_SOURCES source-common/*.c source-common/*.cpp) +add_library(things_common OBJECT ${things_common_SOURCES}) -set_target_properties(commonthings PROPERTIES +set_target_properties(things_common PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF @@ -27,8 +27,8 @@ set_target_properties(commonthings PROPERTIES UNITY_BUILD OFF ) -target_include_directories(commonthings PUBLIC source-common) -target_link_libraries(commonthings PUBLIC +target_include_directories(things_common PUBLIC source-common) +target_link_libraries(things_common PUBLIC # External dependencies ${CONAN_LIBS} glm::glm @@ -37,25 +37,45 @@ target_link_libraries(commonthings PUBLIC # ============================================================================== # NOTE: delibrately not recursive, see README.md in the folder for details -file(GLOB meta_codegen_SOURCES buildtools/codegen/*.c buildtools/codegen/*.cpp) -add_executable(meta_codegen ${meta_codegen_SOURCES}) +file(GLOB codegen_SOURCES buildtools/codegen/*.c buildtools/codegen/*.cpp) +add_executable(codegen ${codegen_SOURCES}) -set_target_properties(meta_codegen PROPERTIES +set_target_properties(codegen PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF UNITY_BUILD OFF ) -target_link_libraries(meta_codegen PRIVATE +target_link_libraries(codegen PRIVATE # External dependencies ${CONAN_LIBS} # Project internal components - commonthings + things_common ) -target_flag_rtti(meta_codegen OFF) +target_flag_rtti(codegen OFF) + +# ============================================================================== + +file(GLOB_RECURSE things_codegen_base_SOURCES source-common/*.c source-common/*.cpp) +add_library(things_codegen_base OBJECT ${things_codegen_base_SOURCES}) + +set_target_properties(things_common PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF +) + +target_include_directories(things_codegen_base PUBLIC source-codegen-base) +target_link_libraries(things_codegen_base PUBLIC + # External dependencies + ${CONAN_LIBS} + + # Project internal components + things_common +) # ============================================================================== @@ -95,7 +115,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE tracy # Project internal components - commonthings + things_common + things_metadata ) option(BRUSSEL_ENABLE_PROFILING "Whether profiling support is enabled or not." OFF) diff --git a/README.md b/README.md index dc0b4c1..5caf9f7 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,12 @@ + [Urho3D](https://urho3d.io/) + [godot](https://godotengine.org/) + Everything else in `3rdparty/` + +## Project Structure +- `buildtools` + - `cmake`: CMake scripts consumed by the root `CMakeLists.txt`. + - `codegen`: Code generator similar to Qt MOC. + Currently runs on: `source`. +- `source-common`: Code that's compiled as a part of all targets. Check each target's build script for details. +- `source-codegen-base`: Code that's consumed along with output of `buildtools/codegen`. +- `source`: The main game. diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index d1f2e50..edd9abc 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -38,9 +38,8 @@ public: std::move(other.mOutFunctions.begin(), other.mOutFunctions.end(), std::back_inserter(this->mOutFunctions)); } - void Write(FILE* file) { + void Write(FILE* file) const { for (auto& thing : mOutThings) { - WRITE_LIT(file, "// Output thing\n"); fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); WRITE_LIT(file, "\n"); } diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index df52b66..e759c31 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -273,7 +273,7 @@ std::string GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, bool return mapName; } -void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags options) { +void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const DeclEnum& decl, EnumFlags options) { bool useExcludeHeuristics = options.IsSet(EMGO_ExcludeUseHeuristics); if (options.IsSet(EMGO_ToString)) { @@ -281,7 +281,7 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags= %d) return {};", maxVal); - APPEND_FMT_LN(o, " return %s[value - %d];", arrayName.c_str(), minVal); - APPEND_LIT_LN(o, "}"); - - out.AddOutputThing(std::move(lookupFunction)); + CodegenOutputThing lookupFunctionDecl; + auto& o1 = lookupFunctionDecl.text; + APPEND_LIT_LN(o1, "template <>"); + APPEND_FMT_LN(o1, "std::string_view Metadata::EnumToString<%s>(%s);", decl.name.c_str(), decl.name.c_str()); + + CodegenOutputThing lookupFunctionDef; + auto& o2 = lookupFunctionDef.text; + APPEND_LIT_LN(o2, "template <>"); + APPEND_FMT_LN(o2, "std::string_view Metadata::EnumToString<%s>(%s value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o2, " if (value < 0 || value >= %d) return {};", maxVal); + APPEND_FMT_LN(o2, " return %s[value - %d];", arrayName.c_str(), minVal); + APPEND_LIT_LN(o2, "}"); + + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); + sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } break; case EVP_Bits: { - auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); + auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics); // TODO } break; case EVP_Random: { - auto mapName = GenerateEnumStringMap(out, decl, useExcludeHeuristics); + auto mapName = GenerateEnumStringMap(sourceOut, decl, useExcludeHeuristics); // TODO } break; @@ -330,19 +337,26 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags EnumFromString_%s(std::string_view value) {", decl.name.c_str(), decl.name.c_str()); - APPEND_FMT_LN(o2, " auto iter = %s.find(value);", mapName); - APPEND_FMT_LN(o2, " if (iter != %s.end()) {", mapName); - APPEND_LIT_LN(o2, " return iter->second;"); - APPEND_LIT_LN(o2, " } else {"); - APPEND_LIT_LN(o2, " return {};"); - APPEND_LIT_LN(o2, " }"); - APPEND_LIT_LN(o2, "}"); - - out.AddOutputThing(std::move(lookupTable)); - out.AddOutputThing(std::move(lookupFunction)); + CodegenOutputThing lookupFunctionDecl; + auto& o2 = lookupFunctionDecl.text; + APPEND_LIT_LN(o2, "template <>"); + APPEND_FMT_LN(o2, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view);", decl.name.c_str(), decl.name.c_str()); + + CodegenOutputThing lookupFunctionDef; + auto& o3 = lookupFunctionDef.text; + APPEND_LIT_LN(o3, "template <>"); + APPEND_FMT_LN(o3, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o3, " auto iter = %s.find(value);", mapName); + APPEND_FMT_LN(o3, " if (iter != %s.end()) {", mapName); + APPEND_LIT_LN(o3, " return iter->second;"); + APPEND_LIT_LN(o3, " } else {"); + APPEND_LIT_LN(o3, " return {};"); + APPEND_LIT_LN(o3, " }"); + APPEND_LIT_LN(o3, "}"); + + sourceOut.AddOutputThing(std::move(lookupTable)); + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); + sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } } @@ -358,8 +372,9 @@ void HandleInputFile(AppState& state, std::string_view source) { printf("END tokens\n"); #endif - CodegenInput fileInput; - CodegenOutput fileOutput; + CodegenInput cgInput; + auto& cgHeaderOutput = state.headerOutput; + auto& cgSourceOutput = state.sourceOutput; int bracePairDepth = 0; while (idx < tokens.size()) { @@ -444,7 +459,7 @@ void HandleInputFile(AppState& state, std::string_view source) { ++idx; } - fileInput.AddEnum(std::move(enumDecl)); + cgInput.AddEnum(std::move(enumDecl)); goto endIdenCase; } @@ -480,7 +495,7 @@ void HandleInputFile(AppState& state, std::string_view source) { } auto& enumName = argList[0][0]->text; - auto enumDecl = fileInput.FindEnumByName(enumName); + auto enumDecl = cgInput.FindEnumByName(enumName); if (!enumDecl) { printf("[ERROR] BRUSSEL_ENUM: referring to non-existent enum '%s'\n", enumName.c_str()); break; @@ -497,7 +512,7 @@ void HandleInputFile(AppState& state, std::string_view source) { } } - GenerateForEnum(fileOutput, *enumDecl, options); + GenerateForEnum(cgHeaderOutput, cgSourceOutput, *enumDecl, options); idx = newIdx; incrementTokenIdx = false; @@ -530,8 +545,6 @@ void HandleInputFile(AppState& state, std::string_view source) { if (bracePairDepth != 0) { printf("[WARNING] unbalanced brace at end of file."); } - - state.sourceOutput.MergeContents(std::move(fileOutput)); } std::string ReadFileAtOnce(const fs::path& path) { @@ -596,6 +609,23 @@ InputOpcode ParseInputOpcode(std::string_view text) { return IOP_COUNT; } +bool WriteOutputFile(const CodegenOutput& output, const char* dirPath, const char* fileName) { + char path[2048]; + snprintf(path, sizeof(path), "%s/%s", dirPath, fileName); + + auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!outputFile) { + printf("[ERROR] unable to open output file %s", path); + return false; + } + DEFER { fclose(outputFile); }; + + DEBUG_PRINTF("Writing output %s", path); + output.Write(outputFile); + + return true; +} + int main(int argc, char* argv[]) { STR_LUT_INIT(ClexNames); BSTR_LUT_INIT(CppKeyword); @@ -609,14 +639,22 @@ int main(int argc, char* argv[]) { AppState state; + state.headerOutput.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#pragma once + +#include +)"""[1], + }); + state.sourceOutput.AddOutputThing(CodegenOutputThing{ .text = &R"""( // This file is generated. Any changes will be overidden when building. +#include "MetadataImpl.hpp" #include #include -#include -#include using namespace std::literals; )"""[1], }); @@ -627,7 +665,7 @@ using namespace std::literals; // NOTE: keep in sync with various enum options and parser code printf(&R"""( USAGE: codegen.exe [:]... -where : the _file_ to write generated contents to +where : the directory to write generated contents to. This will NOT automatically create the directory. is one of: "single" process this file only "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files @@ -635,8 +673,33 @@ where : the _file_ to write generated contents to return -1; } - const char* outputFilePath = argv[1]; - DEBUG_PRINTF("Outputting to file %s.\n", outputFilePath); + const char* outputDir = argv[1]; + DEBUG_PRINTF("Outputting to directory %s.\n", outputDirPath); + + { + char path[2048]; + snprintf(path, sizeof(path), "%s/%s", outputDir, "Metadata.hpp"); + + auto fileContent = R"""( +// This file is generated. Any changes will be overidden when building. +// This file is an umbrella header for all generated metadata. End users simply include this file to get access to everything. +#pragma once + +#include + +#include +)"""sv.substr(1); // Get rid of the \n at the beginning + + auto file = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!file) { + printf("[ERROR] unable to open output file %s", path); + return -1; + } + DEFER { fclose(file); }; + + DEBUG_PRINTF("Writing output file %s", path); + fwrite(fileContent.data(), sizeof(decltype(fileContent)::value_type), fileContent.size(), file); + } for (int i = 2; i < argc; ++i) { std::string_view arg(argv[i]); @@ -652,15 +715,8 @@ where : the _file_ to write generated contents to } } - { - auto outputFile = Utils::OpenCstdioFile(outputFilePath, Utils::WriteTruncate); - if (!outputFile) { - printf("[ERROR] unable to open output file %s", outputFilePath); - return -1; - } - DEFER { fclose(outputFile); }; - state.sourceOutput.Write(outputFile); - } + WriteOutputFile(state.headerOutput, outputDir, "MetadataImpl.hpp"); + WriteOutputFile(state.sourceOutput, outputDir, "MetadataImpl.cpp"); return 0; } diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp b/buildtools/codegen/tests/examples/TestEnum.hpp deleted file mode 100644 index 6b4ab33..0000000 --- a/buildtools/codegen/tests/examples/TestEnum.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "MacrosCodegen.hpp" - -enum MyEnum { - EnumElement1, - EnumElement2, - EnumElement3, -}; -BRUSSEL_ENUM(MyEnum, ToString FromString); - -enum CountedEnumAll { - CEA_Foo, - CEA_Bar, - CEA_COUNT, -}; -BRUSSEL_ENUM(CountedEnumAll, ToString FromString); - -enum CountedEnum { - CE_Foo, - CE_Bar, - CE_FooBar, - CE_COUNT, -}; -BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp.txt b/buildtools/codegen/tests/examples/TestEnum.hpp.txt new file mode 100644 index 0000000..e596c5e --- /dev/null +++ b/buildtools/codegen/tests/examples/TestEnum.hpp.txt @@ -0,0 +1,21 @@ +enum MyEnum { + EnumElement1, + EnumElement2, + EnumElement3, +}; +BRUSSEL_ENUM(MyEnum, ToString FromString); + +enum CountedEnumAll { + CEA_Foo, + CEA_Bar, + CEA_COUNT, +}; +BRUSSEL_ENUM(CountedEnumAll, ToString FromString); + +enum CountedEnum { + CE_Foo, + CE_Bar, + CE_FooBar, + CE_COUNT, +}; +BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); diff --git a/source-codegen-base/MetadataBase.hpp b/source-codegen-base/MetadataBase.hpp new file mode 100644 index 0000000..59c61da --- /dev/null +++ b/source-codegen-base/MetadataBase.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace Metadata { + +template +std::string_view EnumToString(TEnum value) = delete; + +template +std::optional EnumFromString(std::string_view str) = delete; + +} // namespace Metadata -- cgit v1.2.3-70-g09d2 From ce9559e8c2b69d46cff064241bd9a04c014af44f Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 30 May 2022 13:09:26 -0700 Subject: Changeset: 51 Add integration into the main game --- CMakeLists.txt | 24 +++++- buildtools/codegen/CodegenOutput.inl | 19 ++++- buildtools/codegen/CodegenUtils.inl | 73 ++++++++++++++++ buildtools/codegen/main.cpp | 154 ++++++++++------------------------ source-codegen-base/MacrosCodegen.hpp | 7 ++ source-codegen-base/Metadata.cpp | 1 + source-codegen-base/Metadata.hpp | 4 + source-codegen-base/MetadataBase.hpp | 4 +- source-common/Macros.hpp | 2 + source-common/MacrosCodegen.hpp | 7 -- source/GraphicsTags.cpp | 2 + source/GraphicsTags.hpp | 12 +++ source/main.cpp | 5 ++ 13 files changed, 191 insertions(+), 123 deletions(-) create mode 100644 buildtools/codegen/CodegenUtils.inl create mode 100644 source-codegen-base/MacrosCodegen.hpp create mode 100644 source-codegen-base/Metadata.cpp create mode 100644 source-codegen-base/Metadata.hpp delete mode 100644 source-common/MacrosCodegen.hpp (limited to 'buildtools/codegen/CodegenOutput.inl') diff --git a/CMakeLists.txt b/CMakeLists.txt index 55db14b..4800935 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,6 @@ if(BRUSSEL_CODEGEN_DEBUG_PRINT) target_compile_definitions(codegen PRIVATE CODEGEN_DEBUG_PRINT=1) endif() -# ============================================================================== - file(GLOB_RECURSE things_codegen_base_SOURCES source-codegen-base/*.c source-codegen-base/*.cpp) add_library(things_codegen_base OBJECT ${things_codegen_base_SOURCES}) @@ -82,6 +80,25 @@ target_link_libraries(things_codegen_base PUBLIC things_common ) +# TODO support reading all files from the target, instead of manually supplying a search dir +function(target_gen_metadata TARGET_NAME SEARCH_DIR) + get_target_property(TARGET_SOURCES ${TARGET_NAME} SOURCES) + + set(OUTPUT_DIR ${CMAKE_BINARY_DIR}/generated/${TARGET_NAME}) + set(OUTPUT_FILES + ${OUTPUT_DIR}/generated/GeneratedCode.hpp + ${OUTPUT_DIR}/generated/GeneratedCode.cpp + ) + add_custom_command( + OUTPUT ${OUTPUT_FILES} + COMMAND codegen ${OUTPUT_DIR}/generated rec:${SEARCH_DIR} + DEPENDS ${TARGET_SOURCES} + ) + + target_include_directories(${TARGET_NAME} PRIVATE ${OUTPUT_DIR}) + target_sources(${TARGET_NAME} PRIVATE ${OUTPUT_FILES}) +endfunction() + # ============================================================================== # add_executable requires at least one source file @@ -153,3 +170,6 @@ if(BRUSSEL_ENABLE_ASAN) -fno-omit-frame-pointer ) endif() + +get_filename_component(METADATA_INP_DIR "source" ABSOLUTE) +target_gen_metadata(${PROJECT_NAME} ${METADATA_INP_DIR}) diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index edd9abc..ff7b912 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -3,6 +3,9 @@ #include "CodegenDecl.hpp" #include "CodegenMacros.hpp" +#include + +#include #include #include #include @@ -16,6 +19,7 @@ struct CodegenOutputThing { class CodegenOutput { private: + robin_hood::unordered_set mRequestIncludes; std::vector mOutThings; std::vector mOutStructs; std::vector mOutEnums; @@ -27,6 +31,12 @@ public: bool optionAutoAddPrefix : 1 = false; public: + void AddRequestInclude(std::string_view include) { + if (!mRequestIncludes.contains(include)) { + mRequestIncludes.insert(std::string(include)); + } + } + void AddOutputThing(CodegenOutputThing thing) { mOutThings.push_back(std::move(thing)); } @@ -39,15 +49,20 @@ public: } void Write(FILE* file) const { + for (auto& include : mRequestIncludes) { + // TODO how to resolve to the correct include paths? + WRITE_FMT_LN(file, "#include <%s>", include.c_str()); + } + for (auto& thing : mOutThings) { fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); WRITE_LIT(file, "\n"); } for (auto& declStruct : mOutStructs) { - WRITE_FMT(file, "struct %s {\n", declStruct.name.c_str()); + WRITE_FMT_LN(file, "struct %s {", declStruct.name.c_str()); // TODO - WRITE_LIT(file, "}\n"); + WRITE_LIT_LN(file, "};"); } for (auto& declEnum : mOutEnums) { diff --git a/buildtools/codegen/CodegenUtils.inl b/buildtools/codegen/CodegenUtils.inl new file mode 100644 index 0000000..ea46ac1 --- /dev/null +++ b/buildtools/codegen/CodegenUtils.inl @@ -0,0 +1,73 @@ +#pragma once + +#include "CodegenConfig.hpp" +#include "CodegenMacros.hpp" + +#include "CodegenOutput.inl" + +#include +#include + +#include +#include +#include + +namespace Utils { + +std::string ReadFileAsString(const std::filesystem::path& path) { + auto file = Utils::OpenCstdioFile(path, Utils::Read); + if (!file) throw std::runtime_error("Failed to open source file."); + DEFER { fclose(file); }; + + fseek(file, 0, SEEK_END); + auto fileSize = ftell(file); + rewind(file); + + std::string result(fileSize, '\0'); + fread(result.data(), fileSize, 1, file); + + return result; +} + +bool WriteOutputFile(const CodegenOutput& output, std::string_view dir, std::string_view filename, std::string_view additionalSuffix = {}) { + char path[2048]; + snprintf(path, sizeof(path), "%.*s/%.*s%.*s", PRINTF_STRING_VIEW(dir), PRINTF_STRING_VIEW(filename), PRINTF_STRING_VIEW(additionalSuffix)); + + auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!outputFile) { + printf("[ERROR] unable to open output file %s\n", path); + return false; + } + DEFER { fclose(outputFile); }; + + DEBUG_PRINTF("Writing output %s\n", path); + output.Write(outputFile); + + return true; +} + +void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { + output.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#pragma once + +#include +)"""[1], + }); +} + +void ProduceGeneratedSourceFileHeader(CodegenOutput& output) { + output.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#include "GeneratedCode.hpp" + +#include +#include +using namespace std::literals; + )"""[1], + }); +} + +} // namespace Utils diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index 298f19e..3a3e443 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -4,6 +4,7 @@ #include "CodegenInput.inl" #include "CodegenOutput.inl" +#include "CodegenUtils.inl" #include #include @@ -27,10 +28,12 @@ using namespace std::literals; namespace fs = std::filesystem; // TODO handle namespace +// TODO support codegen target in .cpp files struct AppState { - CodegenOutput headerOutput; - CodegenOutput sourceOutput; + std::string_view outputDir; + CodegenOutput mainHeaderOutput; + CodegenOutput mainSourceOutput; }; enum { @@ -113,7 +116,7 @@ bool StbTokenIsMultiChar(int lexerToken) { void CheckBraceDepth(int braceDpeth) { if (braceDpeth < 0) { - printf("[WARNING] unbalanced brace"); + printf("[WARNING] unbalanced brace\n"); } } @@ -197,7 +200,7 @@ std::vector RecordTokens(std::string_view source) { } if (lexer.token == CLEX_parse_error) { - printf("[ERROR] stb_c_lexer countered a parse error."); + printf("[ERROR] stb_c_lexer countered a parse error.\n"); // TODO how to handle? continue; } @@ -292,11 +295,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D maxVal = decl.elements[decl.elements.size() - 2].value; } - CodegenOutputThing lookupFunctionDecl; - auto& o1 = lookupFunctionDecl.text; - APPEND_LIT_LN(o1, "template <>"); - APPEND_FMT_LN(o1, "std::string_view Metadata::EnumToString<%s>(%s);", decl.name.c_str(), decl.name.c_str()); - CodegenOutputThing lookupFunctionDef; auto& o2 = lookupFunctionDef.text; APPEND_LIT_LN(o2, "template <>"); @@ -305,7 +303,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_FMT_LN(o2, " return %s[value - %d];", arrayName.c_str(), minVal); APPEND_LIT_LN(o2, "}"); - headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } break; @@ -337,11 +334,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_LIT_LN(o1, "};"); // Generate lookup function - CodegenOutputThing lookupFunctionDecl; - auto& o2 = lookupFunctionDecl.text; - APPEND_LIT_LN(o2, "template <>"); - APPEND_FMT_LN(o2, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view);", decl.name.c_str(), decl.name.c_str()); - CodegenOutputThing lookupFunctionDef; auto& o3 = lookupFunctionDef.text; APPEND_LIT_LN(o3, "template <>"); @@ -355,12 +347,11 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_LIT_LN(o3, "}"); sourceOut.AddOutputThing(std::move(lookupTable)); - headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } } -void HandleInputFile(AppState& state, std::string_view source) { +void HandleInputFile(AppState& state, std::string_view filenameStem, std::string_view source) { auto tokens = RecordTokens(source); size_t idx = 0; @@ -373,8 +364,10 @@ void HandleInputFile(AppState& state, std::string_view source) { #endif CodegenInput cgInput; - auto& cgHeaderOutput = state.headerOutput; - auto& cgSourceOutput = state.sourceOutput; + CodegenOutput cgHeaderOutput; + Utils::ProduceGeneratedHeaderFileHeader(cgHeaderOutput); + CodegenOutput cgSourceOutput; + Utils::ProduceGeneratedSourceFileHeader(cgSourceOutput); int bracePairDepth = 0; while (idx < tokens.size()) { @@ -545,21 +538,9 @@ void HandleInputFile(AppState& state, std::string_view source) { if (bracePairDepth != 0) { printf("[WARNING] unbalanced brace at end of file."); } -} - -std::string ReadFileAtOnce(const fs::path& path) { - auto file = Utils::OpenCstdioFile(path, Utils::Read); - if (!file) throw std::runtime_error("Failed to open source file."); - DEFER { fclose(file); }; - fseek(file, 0, SEEK_END); - auto fileSize = ftell(file); - rewind(file); - - std::string result(fileSize, '\0'); - fread(result.data(), fileSize, 1, file); - - return result; + Utils::WriteOutputFile(cgHeaderOutput, state.outputDir, filenameStem, ".gh.inl"sv); + Utils::WriteOutputFile(cgSourceOutput, state.outputDir, filenameStem, ".gs.inl"sv); } enum InputOpcode { @@ -571,12 +552,17 @@ enum InputOpcode { void HandleArgument(AppState& state, InputOpcode opcode, std::string_view operand) { switch (opcode) { case IOP_ProcessSingleFile: { - fs::path filePath(operand); - auto source = ReadFileAtOnce(filePath); - HandleInputFile(state, source); + DEBUG_PRINTF("Processing single file %.*s\n", PRINTF_STRING_VIEW(operand)); + + fs::path path(operand); + auto filenameStem = path.stem().string(); + auto source = Utils::ReadFileAsString(path); + HandleInputFile(state, filenameStem, source); } break; case IOP_ProcessRecursively: { + DEBUG_PRINTF("Recursively processing folder %.*s\n", PRINTF_STRING_VIEW(operand)); + fs::path startPath(operand); for (auto& item : fs::recursive_directory_iterator(startPath)) { if (!item.is_regular_file()) { @@ -584,15 +570,19 @@ void HandleArgument(AppState& state, InputOpcode opcode, std::string_view operan } auto& path = item.path(); - auto filename = path.filename().string(); - if (filename != ".c" || - filename != ".cpp") + auto pathExt = path.extension(); + auto pathStem = path.stem(); + if (pathExt != ".h" && + pathExt != ".hpp") { continue; } - auto source = ReadFileAtOnce(path); - HandleInputFile(state, source); + DEBUG_PRINTF("Processing subfile %s\n", path.string().c_str()); + + auto filenameStem = pathStem.string(); + auto source = Utils::ReadFileAsString(path); + HandleInputFile(state, filenameStem, source); } } break; @@ -605,25 +595,10 @@ InputOpcode ParseInputOpcode(std::string_view text) { return IOP_ProcessSingleFile; } else if (text == "rec"sv) { return IOP_ProcessRecursively; + } else { + DEBUG_PRINTF("Unknown input opcode %s\n", text.data()); + throw std::runtime_error("Unknown input opcode"); } - return IOP_COUNT; -} - -bool WriteOutputFile(const CodegenOutput& output, const char* dirPath, const char* fileName) { - char path[2048]; - snprintf(path, sizeof(path), "%s/%s", dirPath, fileName); - - auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); - if (!outputFile) { - printf("[ERROR] unable to open output file %s", path); - return false; - } - DEFER { fclose(outputFile); }; - - DEBUG_PRINTF("Writing output %s", path); - output.Write(outputFile); - - return true; } int main(int argc, char* argv[]) { @@ -639,25 +614,8 @@ int main(int argc, char* argv[]) { AppState state; - state.headerOutput.AddOutputThing(CodegenOutputThing{ - .text = &R"""( -// This file is generated. Any changes will be overidden when building. -#pragma once - -#include -)"""[1], - }); - - state.sourceOutput.AddOutputThing(CodegenOutputThing{ - .text = &R"""( -// This file is generated. Any changes will be overidden when building. -#include "MetadataImpl.hpp" - -#include -#include -using namespace std::literals; - )"""[1], - }); + 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 @@ -668,55 +626,31 @@ USAGE: codegen.exe [:]... where : the directory to write generated contents to. This will NOT automatically create the directory. is one of: "single" process this file only - "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files + "rec" starting at the given directory , recursively process all .h .hpp files )"""[1]); return -1; } - const char* outputDir = argv[1]; + state.outputDir = std::string_view(argv[1]); DEBUG_PRINTF("Outputting to directory %s.\n", outputDir); - { - char path[2048]; - snprintf(path, sizeof(path), "%s/%s", outputDir, "Metadata.hpp"); - - auto fileContent = R"""( -// This file is generated. Any changes will be overidden when building. -// This file is an umbrella header for all generated metadata. End users simply include this file to get access to everything. -#pragma once - -#include - -#include -)"""sv.substr(1); // Get rid of the \n at the beginning - - auto file = Utils::OpenCstdioFile(path, Utils::WriteTruncate); - if (!file) { - printf("[ERROR] unable to open output file %s", path); - return -1; - } - DEFER { fclose(file); }; - - DEBUG_PRINTF("Writing output file %s", path); - fwrite(fileContent.data(), sizeof(decltype(fileContent)::value_type), fileContent.size(), file); - } - for (int i = 2; i < argc; ++i) { - std::string_view arg(argv[i]); + const char* argRaw = argv[i]; + std::string_view arg(argRaw); + DEBUG_PRINTF("Processing input command %s\n", argRaw); + auto separatorLoc = arg.find(':'); if (separatorLoc != std::string_view::npos) { auto opcodeString = arg.substr(0, separatorLoc); auto opcode = ParseInputOpcode(opcodeString); auto operand = arg.substr(separatorLoc + 1); - DEBUG_PRINTF("Processing input command %.*s at path %.*s\n", (int)opcodeString.size(), opcodeString.data(), (int)operand.size(), operand.data()); - HandleArgument(state, opcode, operand); } } - WriteOutputFile(state.headerOutput, outputDir, "MetadataImpl.hpp"); - WriteOutputFile(state.sourceOutput, outputDir, "MetadataImpl.cpp"); + Utils::WriteOutputFile(state.mainHeaderOutput, state.outputDir, "GeneratedCode.hpp"sv); + Utils::WriteOutputFile(state.mainSourceOutput, state.outputDir, "GeneratedCode.cpp"sv); return 0; } diff --git a/source-codegen-base/MacrosCodegen.hpp b/source-codegen-base/MacrosCodegen.hpp new file mode 100644 index 0000000..6803023 --- /dev/null +++ b/source-codegen-base/MacrosCodegen.hpp @@ -0,0 +1,7 @@ +// NOTE: contents of this file is coupled with buildtools/codegen/ +// when updating, change both sides at the same time + +#pragma once + +#define BRUSSEL_CLASS(name, options) +#define BRUSSEL_ENUM(name, options) diff --git a/source-codegen-base/Metadata.cpp b/source-codegen-base/Metadata.cpp new file mode 100644 index 0000000..ee32054 --- /dev/null +++ b/source-codegen-base/Metadata.cpp @@ -0,0 +1 @@ +#include "Metadata.hpp" diff --git a/source-codegen-base/Metadata.hpp b/source-codegen-base/Metadata.hpp new file mode 100644 index 0000000..a038c15 --- /dev/null +++ b/source-codegen-base/Metadata.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "MacrosCodegen.hpp" +#include "MetadataBase.hpp" diff --git a/source-codegen-base/MetadataBase.hpp b/source-codegen-base/MetadataBase.hpp index 59c61da..8be668d 100644 --- a/source-codegen-base/MetadataBase.hpp +++ b/source-codegen-base/MetadataBase.hpp @@ -6,9 +6,9 @@ namespace Metadata { template -std::string_view EnumToString(TEnum value) = delete; +std::string_view EnumToString(TEnum value); template -std::optional EnumFromString(std::string_view str) = delete; +std::optional EnumFromString(std::string_view str); } // namespace Metadata diff --git a/source-common/Macros.hpp b/source-common/Macros.hpp index b5d05fa..a255ada 100644 --- a/source-common/Macros.hpp +++ b/source-common/Macros.hpp @@ -14,6 +14,8 @@ #define UNUSED(x) (void)x; +#define PRINTF_STRING_VIEW(s) (int)s.size(), s.data() + #if defined(_MSC_VER) # define UNREACHABLE __assume(0) #elif defined(__GNUC__) || defined(__clang__) diff --git a/source-common/MacrosCodegen.hpp b/source-common/MacrosCodegen.hpp deleted file mode 100644 index 6803023..0000000 --- a/source-common/MacrosCodegen.hpp +++ /dev/null @@ -1,7 +0,0 @@ -// NOTE: contents of this file is coupled with buildtools/codegen/ -// when updating, change both sides at the same time - -#pragma once - -#define BRUSSEL_CLASS(name, options) -#define BRUSSEL_ENUM(name, options) diff --git a/source/GraphicsTags.cpp b/source/GraphicsTags.cpp index b389acf..522a58f 100644 --- a/source/GraphicsTags.cpp +++ b/source/GraphicsTags.cpp @@ -306,3 +306,5 @@ GLenum Tags::FindGLType(std::string_view name) { return GL_NONE; } } + +#include diff --git a/source/GraphicsTags.hpp b/source/GraphicsTags.hpp index 34c0885..09e62bf 100644 --- a/source/GraphicsTags.hpp +++ b/source/GraphicsTags.hpp @@ -5,6 +5,16 @@ #include #include +#include + +enum TestEnum { + TE_Position, + TE_Color, + TE_TexCoord, + TE_COUNT, +}; +BRUSSEL_ENUM(TestEnum, ToString FromString ExcludeHeuristics); + namespace Tags { /// Vertex element semantics, used to identify the meaning of vertex buffer contents enum VertexElementSemantic { @@ -96,3 +106,5 @@ GLenum FindGLType(std::string_view name); constexpr GLuint kInvalidLocation = std::numeric_limits::max(); } // namespace Tags + +#include diff --git a/source/main.cpp b/source/main.cpp index c49fc0b..353746b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,9 +3,11 @@ #include "AppConfig.hpp" #include "CommonVertexIndex.hpp" #include "EditorGuizmo.hpp" +#include "GraphicsTags.hpp" #include "Ires.hpp" #include "Level.hpp" #include "Material.hpp" +#include "Metadata.hpp" #include "Shader.hpp" #define GLFW_INCLUDE_NONE @@ -102,6 +104,9 @@ fs::path GetEnvVar(const char* name, const char* backup) { int main(int argc, char* argv[]) { using namespace Tags; + auto str = Metadata::EnumToString(TE_Color); + printf("%.*s", PRINTF_STRING_VIEW(str)); + constexpr auto kImGuiBackend = "imgui-backend"; constexpr auto kGameDataDir = "game-data-directory"; constexpr auto kGameAssetDir = "game-asset-directory"; -- cgit v1.2.3-70-g09d2