diff options
author | rtk0c <[email protected]> | 2022-07-18 10:24:01 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-07-18 10:24:01 -0700 |
commit | 4067db383fb17d0580ebdc41e6354a84898e8343 (patch) | |
tree | e7f36bbd1992ea14916e7387dfcdd050807e23e1 /source/20-codegen-compiler/main.cpp | |
parent | a78bffc3a8ca4dfe6ca985223e38d28608184c52 (diff) |
Changeset: 87 Make codegen use mangled names, convert `std::string_view fullname` to `const std::string* fullname` to indicate the reference nature better
Diffstat (limited to 'source/20-codegen-compiler/main.cpp')
-rw-r--r-- | source/20-codegen-compiler/main.cpp | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/source/20-codegen-compiler/main.cpp b/source/20-codegen-compiler/main.cpp index 782996b..5b82d02 100644 --- a/source/20-codegen-compiler/main.cpp +++ b/source/20-codegen-compiler/main.cpp @@ -389,15 +389,8 @@ void GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, const char* } void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const DeclEnum& decl) { - char enumName[2048]; - if (decl.container) { - snprintf(enumName, sizeof(enumName), "%.*s::%s", PRINTF_STRING_VIEW(decl.container->fullname), decl.name.c_str()); - } else { - strncpy(enumName, decl.name.c_str(), sizeof(enumName)); - } - - // TODO mangle to prevent name conflicts of enum in different namespaces - auto& declIdName = decl.name; + auto& enumName = decl.GetFullName(); + auto& mangledName = decl.GetMangledName(); auto useExcludeHeuristics = decl.generateExcludeUseHeuristics; auto filteredElements = [&]() { @@ -426,7 +419,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D if (decl.generateToString) { // Generate value -> string lookup table and function - INPLACE_FMT(val2StrName, "gCG_%s_Val2Str", declIdName.c_str()); + INPLACE_FMT(val2StrName, "gCG_%s_Val2Str", mangledName.c_str()); switch (decl.GetPattern()) { case EVP_Continuous: { @@ -438,14 +431,14 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D { auto& o = lookupFunctionDecl.text; APPEND_LIT_LN(o, "template <>"); - APPEND_FMT_LN(o, "std::string_view Metadata::EnumToString<%s>(%s value);", enumName, enumName); + APPEND_FMT_LN(o, "std::string_view Metadata::EnumToString<%s>(%s value);", enumName.c_str(), enumName.c_str()); } 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, "std::string_view Metadata::EnumToString<%s>(%s value) {", enumName.c_str(), enumName.c_str()); 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); @@ -472,7 +465,7 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D if (decl.generateFromString) { // Generate string -> value lookup table - INPLACE_FMT(str2ValName, "gCG_%s_Str2Val", declIdName.c_str()); + INPLACE_FMT(str2ValName, "gCG_%s_Str2Val", mangledName.c_str()); CodegenOutputThing lookupTable; { @@ -490,17 +483,17 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D { 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); + APPEND_FMT_LN(o, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value);", enumName.c_str(), enumName.c_str()); } CodegenOutputThing lookupFunctionDef; { auto& o = lookupFunctionDef.text; APPEND_LIT_LN(o, "template <>"); - APPEND_FMT_LN(o, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value) {", enumName, enumName); + APPEND_FMT_LN(o, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value) {", enumName.c_str(), enumName.c_str()); APPEND_FMT_LN(o, " auto iter = %s.find(value);", str2ValName); APPEND_FMT_LN(o, " if (iter != %s.end()) {", str2ValName); - APPEND_FMT_LN(o, " return (%s)iter->second;", enumName); + APPEND_FMT_LN(o, " return (%s)iter->second;", enumName.c_str()); APPEND_LIT_LN(o, " } else {"); APPEND_LIT_LN(o, " return {};"); APPEND_LIT_LN(o, " }"); @@ -530,8 +523,8 @@ void GenerateForClassMetadata( CodegenOutput& sourceOutput, const DeclStruct& decl) // { - // TODO mangle - auto declIdName = decl.name.c_str(); + auto& mangedName = decl.GetMangledName(); + auto mangedNameCstr = mangedName.c_str(); CodegenOutputThing data; // TODO generate type id, this needs global scanning @@ -542,7 +535,7 @@ void GenerateForClassMetadata( auto baseClassIdName = baseClass->name.c_str(); APPEND_FMT_LN(data.text, "extern const TypeInfo gCGtype_%s_TypeInfo;", baseClassIdName); } - APPEND_FMT_LN(data.text, "const TypeInfo* const gCGtype_%s_BaseClasses[] = {", declIdName); + APPEND_FMT_LN(data.text, "const TypeInfo* const gCGtype_%s_BaseClasses[] = {", mangedNameCstr); for (auto& baseClass : decl.baseClasses) { auto baseClassIdName = baseClass->name.c_str(); APPEND_FMT_LN(data.text, "gCGtype_%s_TypeInfo,", baseClassIdName); @@ -551,7 +544,7 @@ void GenerateForClassMetadata( } if (!decl.memberVariables.empty()) { - APPEND_FMT_LN(data.text, "const TypePropertyInfo gCGtype_%s_Properties[] = {", declIdName); + APPEND_FMT_LN(data.text, "const TypePropertyInfo gCGtype_%s_Properties[] = {", mangedNameCstr); for (auto& property : decl.memberVariables) { APPEND_FMT_LN(data.text, "{.name=\"%s\"sv, .getterName=\"%s\"sv, .setterName=\"%s\"sv},", property.name.c_str(), property.getterName.c_str(), property.setterName.c_str()); } @@ -559,28 +552,28 @@ void GenerateForClassMetadata( } if (!decl.memberFunctions.empty()) { - APPEND_FMT_LN(data.text, "const TypeMethodInfo gCGtype_%s_Methods[] = {", declIdName); + APPEND_FMT_LN(data.text, "const TypeMethodInfo gCGtype_%s_Methods[] = {", mangedNameCstr); for (auto& method : decl.memberFunctions) { // TODO } APPEND_LIT_LN(data.text, "};"); } - APPEND_FMT_LN(data.text, "const TypeInfo gCGtype_%s_TypeInfo = {", declIdName); - APPEND_FMT_LN(data.text, ".name = \"%s\"sv,", declIdName); - if (!decl.baseClasses.empty()) APPEND_FMT_LN(data.text, ".parents = gCGtype_%s_BaseClasses,", declIdName); - if (!decl.memberVariables.empty()) APPEND_FMT_LN(data.text, ".properties = gCGtype_%s_Properties,", declIdName); - if (!decl.memberFunctions.empty()) APPEND_FMT_LN(data.text, ".methods = gCGtype_%s_Methods,", declIdName); + APPEND_FMT_LN(data.text, "const TypeInfo gCGtype_%s_TypeInfo = {", mangedNameCstr); + APPEND_FMT_LN(data.text, ".name = \"%s\"sv,", mangedNameCstr); + if (!decl.baseClasses.empty()) APPEND_FMT_LN(data.text, ".parents = gCGtype_%s_BaseClasses,", mangedNameCstr); + if (!decl.memberVariables.empty()) APPEND_FMT_LN(data.text, ".properties = gCGtype_%s_Properties,", mangedNameCstr); + if (!decl.memberFunctions.empty()) APPEND_FMT_LN(data.text, ".methods = gCGtype_%s_Methods,", mangedNameCstr); APPEND_LIT_LN(data.text, "};"); CodegenOutputThing queryFunc; APPEND_FMT(queryFunc.text, "template <>\n" - "const TypeInfo* Metadata::GetTypeInfo<%.*s>() {\n" + "const TypeInfo* Metadata::GetTypeInfo<%s>() {\n" " return &gCGtype_%s_TypeInfo;\n" "}\n", - PRINTF_STRING_VIEW(decl.fullname), - declIdName); + decl.fullname->c_str(), + mangedNameCstr); sourceOutput.AddOutputThing(std::move(data)); sourceOutput.AddOutputThing(std::move(queryFunc)); @@ -1387,7 +1380,7 @@ where --output-dir=<path>: the *directory* to write generated contents to. Thi // TODO work with pass-by-value vs pass-by-reference // this probably needs libclang to detect the size and existance of trivial copy-ctors CodegenOutputThing data; - APPEND_FMT_LN(data.text, "const %s& %.*s::%s() const {", property.type.c_str(), PRINTF_STRING_VIEW(property.containerStruct->fullname), property.getterName.c_str()); + APPEND_FMT_LN(data.text, "const %s& %s::%s() const {", property.type.c_str(), property.containerStruct->fullname->c_str(), property.getterName.c_str()); APPEND_FMT_LN(data.text, " return %s;", property.name.c_str()); APPEND_LIT_LN(data.text, "}"); @@ -1395,7 +1388,7 @@ where --output-dir=<path>: the *directory* to write generated contents to. Thi } if (property.isSetterGenerated) { CodegenOutputThing data; - APPEND_FMT_LN(data.text, "void %.*s::%s(const %s& value) const {", PRINTF_STRING_VIEW(property.containerStruct->fullname), property.setterName.c_str(), property.type.c_str()); + APPEND_FMT_LN(data.text, "void %s::%s(const %s& value) const {", property.containerStruct->fullname->c_str(), property.setterName.c_str(), property.type.c_str()); APPEND_FMT_LN(data.text, " this->%s = value;", property.name.c_str()); APPEND_LIT_LN(data.text, "}"); |