aboutsummaryrefslogtreecommitdiff
path: root/source/20-codegen-compiler/CodegenModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/20-codegen-compiler/CodegenModel.cpp')
-rw-r--r--source/20-codegen-compiler/CodegenModel.cpp61
1 files changed, 28 insertions, 33 deletions
diff --git a/source/20-codegen-compiler/CodegenModel.cpp b/source/20-codegen-compiler/CodegenModel.cpp
index e3fc810..be41e68 100644
--- a/source/20-codegen-compiler/CodegenModel.cpp
+++ b/source/20-codegen-compiler/CodegenModel.cpp
@@ -1,5 +1,4 @@
#include "CodegenModel.hpp"
-#include "CodegenModelArchive.hpp"
#include "CodegenUtils.hpp"
@@ -22,8 +21,8 @@ struct SomeDecl {
std::variant<DeclStruct, DeclFunction, DeclEnum> v;
};
-class CodegenModel::Private {
- friend class CodegenModelArchive;
+class CodegenRuntimeModel::Private {
+ friend class CodegenArchiveModel;
public:
// We want address stability for everything
@@ -34,7 +33,6 @@ public:
// A number for `PRAGMA user_vesrion`, representing the current database version. Increment when the table format changes.
#define CURRENT_DATABASE_VERSION 1
constexpr int64_t kGlobalNamespaceId = 1;
-constexpr int64_t kNoFileId = 1;
struct SQLiteDatabase {
sqlite3* database = nullptr;
@@ -94,8 +92,8 @@ void PrintErrMsgIfPresent(char*& errMsg) {
}
} // namespace
-class CodegenModelArchive::Private {
- friend class CodegenModel;
+class CodegenArchiveModel::Private {
+ friend class CodegenRuntimeModel;
public:
// NOTE: this must be the first field, because we want it to destruct after all other statement fields
@@ -225,9 +223,6 @@ CREATE UNIQUE INDEX Index_DeclStructMethods_Identity ON DeclStructMethods(Struct
CREATE UNIQUE INDEX Index_DeclEnums_Identity ON DeclEnums(NamespaceId, Name);
-INSERT INTO Files(Id, FileName)
-VALUES (1, '');
-
-- Special global namespace that has no parent, and Id should always be 1
INSERT INTO Namespaces(Id, ParentNamespaceId, Name)
VALUES (1, NULL, '<global namespace>');
@@ -341,10 +336,10 @@ COMMIT TRANSACTION;
return fileId;
}
- /// \return Row ID of the file
+ /// \return Row ID of the file, or 0 if not found.
int64_t FindOrStoreFile(/*nullable*/ const SourceFile* file) {
if (!file) {
- return kNoFileId;
+ return 0;
}
return FindOrStoreFile(file->filename);
}
@@ -392,7 +387,7 @@ private:
for (auto it = namespaceNames.rbegin(); it != namespaceNames.rend(); ++it) {
fullname.append(*it);
- if (!append && std::next(it) != namespaceNames.rend()) {
+ if (append || std::next(it) != namespaceNames.rend()) {
fullname.append("::");
}
}
@@ -438,12 +433,12 @@ private:
}
};
-CodegenModel::CodegenModel()
+CodegenRuntimeModel::CodegenRuntimeModel()
: m{ new Private() } //
{
}
-CodegenModel::~CodegenModel() {
+CodegenRuntimeModel::~CodegenRuntimeModel() {
delete m;
}
@@ -455,7 +450,7 @@ CodegenModel::~CodegenModel() {
declRef.fullname = key; \
return &declRef
-DeclEnum* CodegenModel::AddEnum(std::string fullname, DeclEnum decl) {
+DeclEnum* CodegenRuntimeModel::AddEnum(std::string fullname, DeclEnum decl) {
#if CODEGEN_DEBUG_PRINT
printf("Committed enum '%s'\n", decl.name.c_str());
for (auto& elm : decl.elements) {
@@ -466,7 +461,7 @@ DeclEnum* CodegenModel::AddEnum(std::string fullname, DeclEnum decl) {
STORE_DECL_OF_TYPE(DeclEnum, fullname, decl);
}
-DeclStruct* CodegenModel::AddStruct(std::string fullname, DeclStruct decl) {
+DeclStruct* CodegenRuntimeModel::AddStruct(std::string fullname, DeclStruct decl) {
#if CODEGEN_DEBUG_PRINT
printf("Committed struct '%s'\n", decl.name.c_str());
printf(" Base classes:\n");
@@ -488,15 +483,15 @@ DeclStruct* CodegenModel::AddStruct(std::string fullname, DeclStruct decl) {
} \
return nullptr
-const DeclEnum* CodegenModel::FindEnum(std::string_view name) const {
+const DeclEnum* CodegenRuntimeModel::FindEnum(std::string_view name) const {
FIND_DECL_OF_TYPE(DeclEnum);
}
-const DeclStruct* CodegenModel::FindStruct(std::string_view name) const {
+const DeclStruct* CodegenRuntimeModel::FindStruct(std::string_view name) const {
FIND_DECL_OF_TYPE(DeclStruct);
}
-DeclNamespace* CodegenModel::AddNamespace(DeclNamespace ns) {
+DeclNamespace* CodegenRuntimeModel::AddNamespace(DeclNamespace ns) {
auto path = Utils::MakeFullName(""sv, &ns);
auto [iter, success] = m->namespaces.try_emplace(std::move(path), std::move(ns));
auto& nsRef = iter->second;
@@ -506,7 +501,7 @@ DeclNamespace* CodegenModel::AddNamespace(DeclNamespace ns) {
return &nsRef;
}
-const DeclNamespace* CodegenModel::FindNamespace(std::string_view fullname) const {
+const DeclNamespace* CodegenRuntimeModel::FindNamespace(std::string_view fullname) const {
auto iter = m->namespaces.find(fullname);
if (iter != m->namespaces.end()) {
return &iter->second;
@@ -515,11 +510,11 @@ const DeclNamespace* CodegenModel::FindNamespace(std::string_view fullname) cons
}
}
-DeclNamespace* CodegenModel::FindNamespace(std::string_view name) {
- return const_cast<DeclNamespace*>(const_cast<const CodegenModel*>(this)->FindNamespace(name));
+DeclNamespace* CodegenRuntimeModel::FindNamespace(std::string_view name) {
+ return const_cast<DeclNamespace*>(const_cast<const CodegenRuntimeModel*>(this)->FindNamespace(name));
}
-CodegenModelArchive::CodegenModelArchive(std::string_view dbPath)
+CodegenArchiveModel::CodegenArchiveModel(std::string_view dbPath)
: m{ new Private() } //
{
std::string zstrPath(dbPath);
@@ -571,11 +566,11 @@ CodegenModelArchive::CodegenModelArchive(std::string_view dbPath)
m->storeNamespaceStmt.Initialize(m->database, "INSERT INTO Namespaces(ParentNamespaceId, Name) VALUES (?1, ?2) RETURNING Id");
}
-CodegenModelArchive::~CodegenModelArchive() {
+CodegenArchiveModel::~CodegenArchiveModel() {
delete m;
}
-void CodegenModelArchive::DeleteDeclsRelatedToFile(std::string_view filename) {
+void CodegenArchiveModel::DeleteDeclsRelatedToFile(std::string_view filename) {
// -Argument- -Description-
// ?1 The filename to delete
m->deleteFunctionDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclFunctions WHERE FileId = (SELECT Id FROM Files WHERE FileName = ?1)"sv);
@@ -598,11 +593,11 @@ void CodegenModelArchive::DeleteDeclsRelatedToFile(std::string_view filename) {
m->CommitTransaction();
}
-void CodegenModelArchive::Store(const CodegenModel& cgModel) {
+void CodegenArchiveModel::Store(const CodegenRuntimeModel& cgModel) {
auto& cgm = cgModel.GetPimpl();
struct Visiter {
- CodegenModelArchive* self;
+ CodegenArchiveModel* self;
void operator()(const DeclStruct& decl) const {
self->StoreStruct(decl);
@@ -629,12 +624,12 @@ void CodegenModelArchive::Store(const CodegenModel& cgModel) {
m->CommitTransaction();
}
-void CodegenModelArchive::LoadInto(CodegenModel& model) const {
+void CodegenArchiveModel::LoadInto(CodegenRuntimeModel& model) const {
// TODO
}
-CodegenModel CodegenModelArchive::Load() const {
- CodegenModel cgModel;
+CodegenRuntimeModel CodegenArchiveModel::Load() const {
+ CodegenRuntimeModel cgModel;
// TODO files
// TODO namespaces
@@ -733,7 +728,7 @@ CodegenModel CodegenModelArchive::Load() const {
return cgModel;
}
-void CodegenModelArchive::StoreStruct(const DeclStruct& decl) {
+void CodegenArchiveModel::StoreStruct(const DeclStruct& decl) {
// -Argument- -Description-
// ?1 Namespace ID
// ?2 Struct name
@@ -810,11 +805,11 @@ VALUES (?1, ?2, ?3, ?4, ?5, ?6)
}
}
-void CodegenModelArchive::StoreFunction(const DeclFunction& decl) {
+void CodegenArchiveModel::StoreFunction(const DeclFunction& decl) {
// TODO
}
-void CodegenModelArchive::StoreEnum(const DeclEnum& decl) {
+void CodegenArchiveModel::StoreEnum(const DeclEnum& decl) {
// -Argument- -Description-
// ?1 Namespace ID
// ?2 Enum name