diff options
Diffstat (limited to 'source/20-codegen-compiler/CodegenUtils.cpp')
-rw-r--r-- | source/20-codegen-compiler/CodegenUtils.cpp | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/source/20-codegen-compiler/CodegenUtils.cpp b/source/20-codegen-compiler/CodegenUtils.cpp index 415a183..5bc5d79 100644 --- a/source/20-codegen-compiler/CodegenUtils.cpp +++ b/source/20-codegen-compiler/CodegenUtils.cpp @@ -7,13 +7,17 @@ #include <cstdio> #include <cstdlib> +using namespace std::literals; + bool Utils::WriteOutputFile(const CodegenOutput& output, const char* path) { auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); if (!outputFile) { printf("[ERROR] unable to open output file %s\n", path); return false; } - DEFER { fclose(outputFile); }; + DEFER { + fclose(outputFile); + }; DEBUG_PRINTF("Writing output %s\n", path); output.Write(outputFile); @@ -21,32 +25,59 @@ bool Utils::WriteOutputFile(const CodegenOutput& output, const char* path) { return true; } -std::string Utils::MakeFullName(std::string_view name, DeclNamespace* ns) { +std::string Utils::JoinNames(DeclNamespace* ns, std::string_view prefix, std::string_view suffix, std::string_view delimiter) { size_t length = 0; - std::vector<std::string_view> components; - if (!name.empty()) { - components.push_back(name); - length += name.length(); + if (!prefix.empty()) { + length += prefix.length() + delimiter.length(); } + if (!suffix.empty()) { + length += suffix.length() + delimiter.length(); + } + size_t nsCount = 0; + { + DeclNamespace* curr = ns; + while (curr) { + length += curr->name.length() + delimiter.length(); - DeclNamespace* currentNamespace = ns; - while (currentNamespace) { - components.push_back(currentNamespace->name); - length += currentNamespace->name.size() + /*::*/ 2; - currentNamespace = currentNamespace->container; + curr = curr->container; + ++nsCount; + } } + length -= delimiter.length(); - std::string fullname; - fullname.reserve(length); - for (auto it = components.rbegin(); it != components.rend(); ++it) { - fullname += *it; - fullname += "::"; + std::string joined; + joined.reserve(length); + + if (!prefix.empty()) { + joined += prefix; + joined += delimiter; } - // Get rid of the last "::" - fullname.pop_back(); - fullname.pop_back(); + { + DeclNamespace* curr = ns; + size_t i = 0; + while (curr) { + joined += curr->name; + if (!suffix.empty() || i != (nsCount - 1)) { + joined += delimiter; + } - return fullname; + curr = curr->container; + ++i; + } + } + if (!suffix.empty()) { + joined += suffix; + } + + return joined; +} + +std::string Utils::MakeFullName(std::string_view name, DeclNamespace* ns) { + return JoinNames(ns, ""sv, name, "::"sv); +} + +std::string Utils::MakeMangledName(std::string_view name, DeclNamespace* ns) { + return JoinNames(ns, ""sv, name, "_"sv); } // NOTE: assuming we are only dealing with ASCII characters @@ -119,7 +150,6 @@ void Utils::ProduceGeneratedHeader(const char* headerFilename, CodegenOutput& he CodegenOutputThing headerOut; headerOut.text += &R"""( // This file is generated. Any changes will be overidden when building. -#pragma once #include <MetadataBase.hpp> #include <cstddef> #include <cstdint> @@ -129,13 +159,13 @@ 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 <MetadataDetails.hpp> #include <frozen/string.h> #include <frozen/unordered_map.h> +#include <MetadataDetails.hpp> using namespace std::literals; using namespace Metadata; )"""[1]; - header.AddOutputThing(std::move(headerOut)); - source.AddOutputThing(std::move(sourceOut)); + header.AddOutputThing(std::move(headerOut), 0); + source.AddOutputThing(std::move(sourceOut), 0); } |