From 8c2b1bd5bd85667a2ea24ec3aa85cbdd97f9ea1c Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 17 Jul 2022 23:09:00 -0700 Subject: Changeset: 85 Work on codegen (a big blob of changes about various things, giving up on writing a clear commit message) - stuff along the lines of cleaning up store process - remove completed TODOs - move code generation out of parser loop - ^^^ also introduce some weird bugs of DeclXxx::name field disappearing -- maybe fixed, maybe didn't, can't reliably reproduce - add infra to mangle (not included in codegen yet, also not tested) - convert SourceFile storage map to node map, ensuring pointer stability (was broken before) - buildsystem asan and UBsan applying to all targest --- source/20-codegen-compiler/CodegenUtils.cpp | 80 ++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 25 deletions(-) (limited to 'source/20-codegen-compiler/CodegenUtils.cpp') 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 #include +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 components; - if (!name.empty()) { - components.push_back(name); - length += name.length(); + if (!prefix.empty()) { + length += prefix.length() + delimiter.length(); } - - DeclNamespace* currentNamespace = ns; - while (currentNamespace) { - components.push_back(currentNamespace->name); - length += currentNamespace->name.size() + /*::*/ 2; - currentNamespace = currentNamespace->container; + if (!suffix.empty()) { + length += suffix.length() + delimiter.length(); } + size_t nsCount = 0; + { + DeclNamespace* curr = ns; + while (curr) { + length += curr->name.length() + delimiter.length(); + + curr = curr->container; + ++nsCount; + } + } + length -= delimiter.length(); + + std::string joined; + joined.reserve(length); - std::string fullname; - fullname.reserve(length); - for (auto it = components.rbegin(); it != components.rend(); ++it) { - fullname += *it; - fullname += "::"; + if (!prefix.empty()) { + joined += prefix; + joined += delimiter; + } + { + DeclNamespace* curr = ns; + size_t i = 0; + while (curr) { + joined += curr->name; + if (!suffix.empty() || i != (nsCount - 1)) { + joined += delimiter; + } + + curr = curr->container; + ++i; + } } - // Get rid of the last "::" - fullname.pop_back(); - fullname.pop_back(); + if (!suffix.empty()) { + joined += suffix; + } + + return joined; +} + +std::string Utils::MakeFullName(std::string_view name, DeclNamespace* ns) { + return JoinNames(ns, ""sv, name, "::"sv); +} - return fullname; +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 #include #include @@ -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 #include #include +#include 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); } -- cgit v1.2.3-70-g09d2