diff options
author | rtk0c <[email protected]> | 2022-07-07 19:35:40 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-07-07 19:35:40 -0700 |
commit | 8776b737483f579d2d2034bb91efe0794bf67d92 (patch) | |
tree | dfefd7f9aa2a5fe00b3089d2435f5e1bc6b9c91e | |
parent | eeed6d84f4324387597be05e6ed1df4b3adfca9c (diff) |
Changeset: 81 Replace DeclEnum::underlyingTypeStr with a lookup function from the enumeration value DeclEnum::underlyingType
-rw-r--r-- | source/20-codegen-compiler/CodegenDecl.hpp | 3 | ||||
-rw-r--r-- | source/20-codegen-compiler/CodegenModel.cpp | 3 | ||||
-rw-r--r-- | source/20-codegen-compiler/main.cpp | 13 |
3 files changed, 10 insertions, 9 deletions
diff --git a/source/20-codegen-compiler/CodegenDecl.hpp b/source/20-codegen-compiler/CodegenDecl.hpp index 00fc18e..d99f79c 100644 --- a/source/20-codegen-compiler/CodegenDecl.hpp +++ b/source/20-codegen-compiler/CodegenDecl.hpp @@ -88,7 +88,6 @@ struct DeclEnum { std::string_view fullname; std::vector<DeclEnumElement> elements; EnumUnderlyingType underlyingType; - std::string underlyingTypeStr; // Start with invalid value, calculate on demand mutable EnumValuePattern pattern = EVP_COUNT; @@ -102,6 +101,8 @@ struct DeclEnum { // NOTE: see GenerateForEnum() for the exact heuristics bool generateExcludeUseHeuristics : 1 = false; + std::string_view GetUnderlyingTypeName() const; + EnumValuePattern CalcPattern() const; EnumValuePattern GetPattern() const; }; diff --git a/source/20-codegen-compiler/CodegenModel.cpp b/source/20-codegen-compiler/CodegenModel.cpp index 943d72c..b466d6e 100644 --- a/source/20-codegen-compiler/CodegenModel.cpp +++ b/source/20-codegen-compiler/CodegenModel.cpp @@ -636,10 +636,11 @@ ON CONFLICT DO UPDATE SET Value=?3 )"""sv); int result; + auto eutName = decl.GetUnderlyingTypeName(); sqlite3_bind_int(m->storeEnumStmt, 1, m->FindNamespace(decl.container)); sqlite3_bind_text(m->storeEnumStmt, 2, decl.name.c_str(), decl.name.size(), nullptr); - sqlite3_bind_text(m->storeEnumStmt, 3, decl.underlyingTypeStr.c_str(), decl.underlyingTypeStr.size(), nullptr); + sqlite3_bind_text(m->storeEnumStmt, 3, eutName.data(), eutName.size(), nullptr); if (decl.sourceFile) { sqlite3_bind_text(m->storeEnumStmt, 4, decl.sourceFile->filename.data(), decl.sourceFile->filename.size(), nullptr); } else { diff --git a/source/20-codegen-compiler/main.cpp b/source/20-codegen-compiler/main.cpp index e9a449c..348d60e 100644 --- a/source/20-codegen-compiler/main.cpp +++ b/source/20-codegen-compiler/main.cpp @@ -474,7 +474,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D { auto& o = lookupTable.text; // TODO use correct underlying type - APPEND_FMT_LN(o, "constinit frozen::unordered_map<frozen::string, %s, %" PRId64 "> %s = {", decl.underlyingTypeStr.c_str(), filteredElements.size(), str2ValName); + APPEND_FMT_LN(o, "constinit frozen::unordered_map<frozen::string, %s, %" PRId64 "> %s = {", FSTR_LUT_LOOKUP(EnumUnderlyingType, decl.underlyingType), filteredElements.size(), str2ValName); for (auto& elm : filteredElements) { APPEND_FMT_LN(o, "{\"%s\", %" PRId64 "},", elm.name.c_str(), elm.value); } @@ -878,18 +878,12 @@ void ParseInputFileAndGenerate(AppState& as, CodegenLexer& /*lexingState*/ ls, s ++idx; // Setting underlying type - // NOTE: these two variables are only valid if we enter the EUT found branch - size_t eutClauseBegin = idx + /* The ':' token */ 1; - size_t eutClauseEnd; if (auto eut = TryConsumeEnumUnderlyingTypeClause(ls); eut != EUT_COUNT) { - eutClauseEnd = idx; enumDecl.underlyingType = eut; - enumDecl.underlyingTypeStr = CombineTokens(std::span(tokens.begin() + eutClauseBegin, eutClauseEnd - eutClauseBegin), " "sv); } else { enumDecl.underlyingType = EUT_Int32; - enumDecl.underlyingTypeStr = "int"; } int enumClosingBraceCount = 0; @@ -1372,3 +1366,8 @@ where --output-dir=<path>: the *directory* to write generated contents to. Thi return 0; } + +// TODO move this function to CodegenDecl.cpp, after making LUT able to cross TUs +std::string_view DeclEnum::GetUnderlyingTypeName() const { + return FSTR_LUT_LOOKUP(EnumUnderlyingType, underlyingType); +} |