aboutsummaryrefslogtreecommitdiff
path: root/source/20-codegen-compiler/CodegenModel.cpp
blob: d075c1df830a7b048c348b11d140b7b0844f4bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "CodegenModel.hpp"
#include "CodegenModelArchive.hpp"

#include "CodegenUtils.hpp"

#include <Macros.hpp>
#include <ScopeGuard.hpp>
#include <Utils.hpp>

#include <robin_hood.h>
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <variant>

using namespace std::literals;

struct SomeDecl {
	std::variant<DeclStruct, DeclFunction, DeclEnum> v;
};

class CodegenModel::Private {
	friend class CodegenModelArchive;

public:
	// We want address stability for everything
	robin_hood::unordered_node_map<std::string, SomeDecl, StringHash, StringEqual> decls;
	robin_hood::unordered_node_map<std::string, DeclNamespace, StringHash, StringEqual> namespaces;
};

class CodegenModelArchive::Private {
	friend class CodegenModel;

public:
	sqlite3* database = nullptr;
	sqlite3_stmt* beginTransactionStmt = nullptr;
	sqlite3_stmt* commitTransactionStmt = nullptr;
	sqlite3_stmt* rollbackTransactionStmt = nullptr;
	sqlite3_stmt* storeEnumStmt = nullptr;
	sqlite3_stmt* storeEnumElmStmt = nullptr;

	// See below for definition
	void InitializeDatabase();

	void BeginTransaction() {
		int result = sqlite3_step(beginTransactionStmt);
		assert(result == SQLITE_DONE);
		sqlite3_reset(beginTransactionStmt);
	}

	void CommitTransaction() {
		int result = sqlite3_step(commitTransactionStmt);
		assert(result == SQLITE_DONE);
		sqlite3_reset(commitTransactionStmt);
	}

	void RollbackTransaction() {
		int result = sqlite3_step(rollbackTransactionStmt);
		assert(result == SQLITE_DONE);
		sqlite3_reset(rollbackTransactionStmt);
	}
};

CodegenModel::CodegenModel()
	: m{ new Private() } //
{
}

CodegenModel::~CodegenModel() {
	delete m;
}

#define STORE_DECL_OF_TYPE(DeclType, fullname, decl)                                                    \
	auto [iter, success] = m->decls.try_emplace(std::move(fullname), SomeDecl{ .v = std::move(decl) }); \
	auto& key = iter->first;                                                                            \
	auto& val = iter->second;                                                                           \
	auto& declRef = std::get<DeclType>(val.v);                                                          \
	declRef.fullname = key;                                                                             \
	return &declRef

DeclEnum* CodegenModel::AddEnum(std::string fullname, DeclEnum decl) {
#if CODEGEN_DEBUG_PRINT
	printf("Committed enum '%s'\n", decl.name.c_str());
	for (auto& elm : decl.elements) {
		printf("  - element %s = %" PRId64 "\n", elm.name.c_str(), elm.value);
	}
#endif

	STORE_DECL_OF_TYPE(DeclEnum, fullname, decl);
}

DeclStruct* CodegenModel::AddStruct(std::string fullname, DeclStruct decl) {
#if CODEGEN_DEBUG_PRINT
	printf("Committed struct '%s'\n", decl.name.c_str());
	printf("  Base classes:\n");
	for (auto& base : decl.baseClasses) {
		printf("  - %.*s\n", PRINTF_STRING_VIEW(base->name));
	}
#endif

	STORE_DECL_OF_TYPE(DeclStruct, fullname, decl);
}

#define FIND_DECL_OF_TYPE(DeclType)                     \
	auto iter = m->decls.find(name);                    \
	if (iter != m->decls.end()) {                       \
		auto& some = iter->second.v;                    \
		if (auto decl = std::get_if<DeclType>(&some)) { \
			return decl;                                \
		}                                               \
	}                                                   \
	return nullptr

const DeclEnum* CodegenModel::FindEnum(std::string_view name) const {
	FIND_DECL_OF_TYPE(DeclEnum);
}

const DeclStruct* CodegenModel::FindStruct(std::string_view name) const {
	FIND_DECL_OF_TYPE(DeclStruct);
}

DeclNamespace* CodegenModel::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;
	if (success) {
		nsRef.fullname = iter->first;
	}
	return &nsRef;
}

const DeclNamespace* CodegenModel::FindNamespace(std::string_view fullname) const {
	auto iter = m->namespaces.find(fullname);
	if (iter != m->namespaces.end()) {
		return &iter->second;
	} else {
		return nullptr;
	}
}

DeclNamespace* CodegenModel::FindNamespace(std::string_view name) {
	return const_cast<DeclNamespace*>(const_cast<const CodegenModel*>(this)->FindNamespace(name));
}

sqlite3_stmt* Utils::NewSqliteStatement(sqlite3* database, std::string_view sql) {
	sqlite3_stmt* stmt;
	int result = sqlite3_prepare(database, sql.data(), sql.size(), &stmt, nullptr);
	if (result != SQLITE_OK) {
		INPLACE_FMT(msg, "Failed to prepare statement, error message: %s", sqlite3_errmsg(database));
		throw std::runtime_error(msg);
	}

	return stmt;
}

void CodegenModelArchive::Private::InitializeDatabase() {
	int result = sqlite3_exec(database, "PRAGMA user_version = 1", nullptr, nullptr, nullptr);
	assert(result == SQLITE_OK);

	result = sqlite3_exec(database, R"""(
BEGIN TRANSACTION;
CREATE TABLE DeclEnums(
    Id INT PRIMARY KEY,
    Name TEXT,
    UnderlyingType TEXT
);
CREATE TABLE DeclEnumElements(
    EnumId INT,
    Name TEXT,
    Value INT,
    FOREIGN KEY (EnumId) REFERENCES DeclEnums(Id)
);
COMMIT TRANSACTION;
)""",
		nullptr,
		nullptr,
		nullptr);
	assert(result == SQLITE_OK);
}

CodegenModelArchive::CodegenModelArchive(std::string_view dbPath)
	: m{ new Private() } //
{
	std::string zstrPath(dbPath);
	int reuslt = sqlite3_open(zstrPath.c_str(), &m->database);
	if (reuslt != SQLITE_OK) {
		std::string msg;
		msg += "Failed to open SQLite3 database, error message:\n";
		msg += sqlite3_errmsg(m->database);
		throw std::runtime_error(msg);
	}

	// Keep the same as `PRAGMA user_version` in the CodegenMetadataArchive::Private::InitializeDatabase()
	constexpr int kDatabaseVersion = 1;
	int currentDatabaseVersion;
	{
		auto readVersionStmt = Utils::NewSqliteStatement(m->database, "PRAGMA user_version");
		DEFER { sqlite3_finalize(readVersionStmt); };

		int result = sqlite3_step(readVersionStmt);
		assert(result == SQLITE_ROW);
		currentDatabaseVersion = sqlite3_column_int(readVersionStmt, 0);

		result = sqlite3_step(readVersionStmt);
		assert(result == SQLITE_DONE);

		if (currentDatabaseVersion == 0) {
			// Newly created database, initialize it
			m->InitializeDatabase();
		} else if (currentDatabaseVersion == kDatabaseVersion) {
			// Same version, no need to do anything
		} else {
			INPLACE_FMT(msg, "Incompatbile database versions %d (in file) vs %d (expected).", currentDatabaseVersion, kDatabaseVersion);
			throw std::runtime_error(msg);
		}
	}

	m->beginTransactionStmt = Utils::NewSqliteStatement(m->database, "BEGIN TRANSACTION");
	m->commitTransactionStmt = Utils::NewSqliteStatement(m->database, "COMMIT TRANSACTION");
	m->rollbackTransactionStmt = Utils::NewSqliteStatement(m->database, "ROLLBACK TRANSACTION");

	m->storeEnumStmt = Utils::NewSqliteStatement(m->database, "INSERT INTO DeclEnums(Name, UnderlyingType) VALUES(?, ?)"sv);
	m->storeEnumElmStmt = Utils::NewSqliteStatement(m->database, "INSERT INTO DeclEnumElements(EnumId, Name, Value) VALUES(?, ?, ?)"sv);
}

CodegenModelArchive::~CodegenModelArchive() {
	sqlite3_close(m->database);
	sqlite3_finalize(m->beginTransactionStmt);
	sqlite3_finalize(m->commitTransactionStmt);
	sqlite3_finalize(m->rollbackTransactionStmt);
	sqlite3_finalize(m->storeEnumStmt);
	sqlite3_finalize(m->storeEnumElmStmt);

	delete m;
}

void CodegenModelArchive::Store(const CodegenModel& cgInput) {
	auto& cgm = cgInput.GetPimpl();

	struct Visiter {
		CodegenModelArchive* self;

		void operator()(const DeclStruct& decl) const {
			self->StoreStruct(decl);
		}
		void operator()(const DeclFunction& decl) const {
			self->StoreFunction(decl);
		}
		void operator()(const DeclEnum& decl) const {
			self->StoreEnum(decl);
		}
	} visiter;
	visiter.self = this;

	m->BeginTransaction();

	for (auto&& [DISCARD, value] : cgm.decls) {
		std::visit(visiter, value.v);
	}

	m->CommitTransaction();
}

void CodegenModelArchive::StoreStruct(const DeclStruct& decl) {
	// TODO
}

void CodegenModelArchive::StoreFunction(const DeclFunction& decl) {
	// TODO
}

void CodegenModelArchive::StoreEnum(const DeclEnum& decl) {
	// TOOD only update
	sqlite3_bind_text(m->storeEnumStmt, 1, decl.name.c_str(), decl.name.size(), nullptr);
	sqlite3_bind_text(m->storeEnumStmt, 2, decl.underlyingTypeStr.c_str(), decl.underlyingTypeStr.size(), nullptr);
	int result = sqlite3_step(m->storeEnumStmt);
	assert(result == SQLITE_DONE);
	sqlite3_reset(m->storeEnumStmt);
	sqlite3_clear_bindings(m->storeEnumStmt);

	auto enumRowId = sqlite3_last_insert_rowid(m->database);

	for (auto& elm : decl.elements) {
		sqlite3_bind_int64(m->storeEnumElmStmt, 1, enumRowId);
		sqlite3_bind_text(m->storeEnumElmStmt, 2, elm.name.c_str(), elm.name.size(), nullptr);
		sqlite3_bind_int64(m->storeEnumElmStmt, 3, elm.value);
		int result = sqlite3_step(m->storeEnumElmStmt);
		assert(result == SQLITE_DONE);
		sqlite3_reset(m->storeEnumElmStmt);
		sqlite3_clear_bindings(m->storeEnumElmStmt);
	}
}