From 8510a85f79f706b93982b4e398b187b5f77081dd Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 3 Jun 2022 22:58:28 -0700 Subject: Changeset: 61 [BUGGED] Move object kind enums to use generated ToString and FromStrong The old mechanism rely on a specific prefix to Ires and GameObject string representations, but the generator currently leaves the enum value. Therefore all editor (e.g. drag & drop) and IO (e.g. ires loading) mechanisms are broken. --- source/20-codegen-compiler/CodegenUtils.cpp | 4 +-- source/20-codegen-compiler/main.cpp | 34 ++++++++++++++++++++-- .../test/examples/TestEnum.hpp.txt | 5 ++-- 3 files changed, 36 insertions(+), 7 deletions(-) (limited to 'source/20-codegen-compiler') diff --git a/source/20-codegen-compiler/CodegenUtils.cpp b/source/20-codegen-compiler/CodegenUtils.cpp index a43b72c..0c70cb6 100644 --- a/source/20-codegen-compiler/CodegenUtils.cpp +++ b/source/20-codegen-compiler/CodegenUtils.cpp @@ -120,10 +120,7 @@ void Utils::ProduceGeneratedHeader(const char* headerFilename, CodegenOutput& he headerOut.text += &R"""( // This file is generated. Any changes will be overidden when building. #pragma once - #include -#include - #include #include )"""[1]; @@ -132,6 +129,7 @@ void Utils::ProduceGeneratedHeader(const char* headerFilename, CodegenOutput& he APPEND_LIT_LN(sourceOut.text, "// This file is generated. Any changes will be overidden when building."); APPEND_FMT_LN(sourceOut.text, "#include \"%s\"", headerFilename); sourceOut.text += &R"""( +#include #include #include using namespace std::literals; diff --git a/source/20-codegen-compiler/main.cpp b/source/20-codegen-compiler/main.cpp index bb7c996..5e052a3 100644 --- a/source/20-codegen-compiler/main.cpp +++ b/source/20-codegen-compiler/main.cpp @@ -66,6 +66,18 @@ FSTR_LUT_DECL(ClexNames, CLEX_eof, CLEX_ext_COUNT) { FSTR_LUT_MAP_ENUM(CLEX_ext_dot_dot_dot); } +FSTR_LUT_DECL(EnumUnderlyingType, 0, EUT_COUNT) { + FSTR_LUT_MAP_FOR(EnumUnderlyingType); + FSTR_LUT_MAP(EUT_Int8, "int8_t"); + FSTR_LUT_MAP(EUT_Int16, "int16_t"); + FSTR_LUT_MAP(EUT_Int32, "int32_t"); + FSTR_LUT_MAP(EUT_Int64, "int64_t"); + FSTR_LUT_MAP(EUT_Uint8, "uint8_t"); + FSTR_LUT_MAP(EUT_Uint16, "uint16_t"); + FSTR_LUT_MAP(EUT_Uint32, "uint32_t"); + FSTR_LUT_MAP(EUT_Uint64, "uint64_t"); +} + RSTR_LUT_DECL(EnumUnderlyingType, 0, EUT_COUNT) { RSTR_LUT_MAP_FOR(EnumUnderlyingType); @@ -352,16 +364,25 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D int minVal = filteredElements.empty() ? 0 : filteredElements.front().value; int maxVal = filteredElements.empty() ? 0 : filteredElements.back().value; + CodegenOutputThing lookupFunctionDecl; + { + auto& o = lookupFunctionDecl.text; + APPEND_LIT_LN(o, "template <>"); + APPEND_FMT_LN(o, "std::string_view Metadata::EnumToString<%s>(%s value);", enumName, enumName); + } + CodegenOutputThing lookupFunctionDef; { auto& o = lookupFunctionDef.text; APPEND_LIT_LN(o, "template <>"); APPEND_FMT_LN(o, "std::string_view Metadata::EnumToString<%s>(%s value) {", enumName, enumName); - APPEND_FMT_LN(o, " if (value < %d || value > %d) return {};", minVal, maxVal); - APPEND_FMT_LN(o, " return %s[value - %d];", val2StrName, minVal); + APPEND_FMT_LN(o, " auto intVal = (%s)value;", FSTR_LUT_LOOKUP(EnumUnderlyingType, decl.underlyingType)); + APPEND_FMT_LN(o, " if (intVal < %d || intVal > %d) return {};", minVal, maxVal); + APPEND_FMT_LN(o, " return %s[intVal - %d];", val2StrName, minVal); APPEND_LIT_LN(o, "}"); } + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } break; @@ -395,6 +416,13 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D } // Generate lookup function + CodegenOutputThing lookupFunctionDecl; + { + auto& o = lookupFunctionDecl.text; + APPEND_LIT_LN(o, "template <>"); + APPEND_FMT_LN(o, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value);", enumName, enumName); + } + CodegenOutputThing lookupFunctionDef; { auto& o = lookupFunctionDef.text; @@ -410,6 +438,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D } sourceOut.AddOutputThing(std::move(lookupTable)); + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } } @@ -1032,6 +1061,7 @@ InputOpcode ParseInputOpcode(std::string_view text) { int main(int argc, char* argv[]) { FSTR_LUT_INIT(ClexNames); + FSTR_LUT_INIT(EnumUnderlyingType); RSTR_LUT_INIT(EnumUnderlyingType); FSTR_LUT_INIT(EnumValuePattern); RSTR_LUT_INIT(CppKeyword); diff --git a/source/20-codegen-compiler/test/examples/TestEnum.hpp.txt b/source/20-codegen-compiler/test/examples/TestEnum.hpp.txt index 441d97c..30c36c0 100644 --- a/source/20-codegen-compiler/test/examples/TestEnum.hpp.txt +++ b/source/20-codegen-compiler/test/examples/TestEnum.hpp.txt @@ -5,7 +5,8 @@ enum MyEnum { }; BRUSSEL_ENUM(MyEnum, ToString FromString); -enum CountedEnumAll { +// Let's also test enum class +enum class CountedEnumAll { CEA_Foo, CEA_Bar, CEA_COUNT, @@ -21,7 +22,7 @@ enum CountedEnum { BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); namespace MyNamespace { -enum MyNamespacedEnum { +enum class MyNamespacedEnum { MNE_Foo, MNE_Bar, }; -- cgit v1.2.3-70-g09d2