aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-07-18 10:24:01 -0700
committerrtk0c <[email protected]>2022-07-18 10:24:01 -0700
commit4067db383fb17d0580ebdc41e6354a84898e8343 (patch)
treee7f36bbd1992ea14916e7387dfcdd050807e23e1 /source
parenta78bffc3a8ca4dfe6ca985223e38d28608184c52 (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')
-rw-r--r--source/20-codegen-compiler/CodegenDecl.hpp12
-rw-r--r--source/20-codegen-compiler/CodegenModel.cpp4
-rw-r--r--source/20-codegen-compiler/main.cpp55
3 files changed, 33 insertions, 38 deletions
diff --git a/source/20-codegen-compiler/CodegenDecl.hpp b/source/20-codegen-compiler/CodegenDecl.hpp
index eacd254..e645323 100644
--- a/source/20-codegen-compiler/CodegenDecl.hpp
+++ b/source/20-codegen-compiler/CodegenDecl.hpp
@@ -23,7 +23,7 @@ struct DeclNamespace {
DeclNamespace* container = nullptr;
std::string name;
- std::string_view fullname; // View into storage map key
+ const std::string* fullname = nullptr; // View into storage map key
};
struct DeclStruct;
@@ -52,12 +52,14 @@ struct DeclStruct {
std::vector<DeclMemberFunction> generatedFunctions;
std::string name;
mutable std::string mangledName;
- std::string_view fullname;
+ const std::string* fullname = nullptr; // View into storage map key
// Scanned generation options
bool generating : 1 = false;
bool generatingInheritanceHiearchy : 1 = false;
+ const std::string& GetName() const { return name; }
+ const std::string& GetFullName() const { return *fullname; }
const std::string& GetMangledName() const;
};
@@ -97,7 +99,7 @@ struct DeclEnum {
DeclNamespace* container = nullptr;
std::string name;
mutable std::string mangledName;
- std::string_view fullname;
+ const std::string* fullname = nullptr; // View into storage map key
std::vector<DeclEnumElement> elements;
EnumUnderlyingType underlyingType;
// Start with invalid value, calculate on demand
@@ -114,7 +116,7 @@ struct DeclEnum {
bool generateExcludeUseHeuristics : 1 = false;
const std::string& GetName() const { return name; }
- std::string_view GetFullName() const { return fullname; }
+ const std::string& GetFullName() const { return *fullname; }
const std::string& GetMangledName() const;
std::string_view GetUnderlyingTypeName() const;
@@ -134,7 +136,7 @@ struct DeclFunction {
// Things like extern, static, etc. that gets written before the function return type
std::string prefix;
std::string name;
- std::string_view fullname;
+ const std::string* fullname = nullptr; // View into storage map key
std::string returnType;
std::vector<DeclFunctionArgument> arguments;
std::string body;
diff --git a/source/20-codegen-compiler/CodegenModel.cpp b/source/20-codegen-compiler/CodegenModel.cpp
index be41e68..da7a5cc 100644
--- a/source/20-codegen-compiler/CodegenModel.cpp
+++ b/source/20-codegen-compiler/CodegenModel.cpp
@@ -447,7 +447,7 @@ CodegenRuntimeModel::~CodegenRuntimeModel() {
auto& key = iter->first; \
auto& val = iter->second; \
auto& declRef = std::get<DeclType>(val.v); \
- declRef.fullname = key; \
+ declRef.fullname = &key; \
return &declRef
DeclEnum* CodegenRuntimeModel::AddEnum(std::string fullname, DeclEnum decl) {
@@ -496,7 +496,7 @@ DeclNamespace* CodegenRuntimeModel::AddNamespace(DeclNamespace ns) {
auto [iter, success] = m->namespaces.try_emplace(std::move(path), std::move(ns));
auto& nsRef = iter->second;
if (success) {
- nsRef.fullname = iter->first;
+ nsRef.fullname = &iter->first;
}
return &nsRef;
}
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, "}");