aboutsummaryrefslogtreecommitdiff
path: root/source/20-codegen-compiler/CodegenUtils.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-07-17 23:09:00 -0700
committerrtk0c <[email protected]>2022-07-17 23:09:00 -0700
commit8c2b1bd5bd85667a2ea24ec3aa85cbdd97f9ea1c (patch)
tree80b12277b667747aa4f18ebcc3931c2ea618cb1e /source/20-codegen-compiler/CodegenUtils.cpp
parentc6e57dc94e532442ffa0bd57a16206217adbca92 (diff)
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
Diffstat (limited to 'source/20-codegen-compiler/CodegenUtils.cpp')
-rw-r--r--source/20-codegen-compiler/CodegenUtils.cpp78
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);
}