diff options
author | rtk0c <[email protected]> | 2022-07-07 19:30:47 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-07-07 19:30:47 -0700 |
commit | a98b0495a957ed15900c052bcad00b3c48367546 (patch) | |
tree | 10945d897ba6671c707e1c9866f235c0e630c08f /source/20-codegen-compiler/CodegenModel.cpp | |
parent | 6839736b8283a59eb743e1f6058c7d266a3e7f36 (diff) |
Changeset: 79 Initial work on fixing codegen for the main project, add enum underlying type (EUT) scanning capability
Diffstat (limited to 'source/20-codegen-compiler/CodegenModel.cpp')
-rw-r--r-- | source/20-codegen-compiler/CodegenModel.cpp | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/source/20-codegen-compiler/CodegenModel.cpp b/source/20-codegen-compiler/CodegenModel.cpp index 97a53b8..943d72c 100644 --- a/source/20-codegen-compiler/CodegenModel.cpp +++ b/source/20-codegen-compiler/CodegenModel.cpp @@ -110,19 +110,26 @@ public: SQLiteStatement deleteStructDeclByFilenameStmt; SQLiteStatement deleteEnumDeclByFilenameStmt; - void InitializeCoreStatements() { + void InitializeDatabase() { char* errMsg = nullptr; int result = sqlite3_exec(database, "PRAGMA user_version = " STRINGIFY(CURRENT_DATABASE_VERSION), nullptr, nullptr, &errMsg); PrintErrMsgIfPresent(errMsg); assert(result == SQLITE_OK); + // TODO create a table of file names // TODO unique with overloading, and container structs result = sqlite3_exec(database, R"""( BEGIN TRANSACTION; -CREATE TABLE Namespaces( +CREATE TABLE Files( -- NOTE: SQLite forbids foreign keys referencing the implicit `rowid` column, we have to create an alias for it Id INTEGER PRIMARY KEY, + FileName TEXT, + UNIQUE (FileName) +); + +CREATE TABLE Namespaces( + Id INTEGER PRIMARY KEY, ParentNamespaceId INTEGER REFERENCES Namespaces(Id), Name TEXT, UNIQUE (ParentNamespaceId, Name) @@ -132,7 +139,7 @@ CREATE TABLE DeclFunctions( Id INTEGER PRIMARY KEY, NamespaceId INTEGER REFERENCES Namespaces(Id), Name TEXT, - FileName TEXT + FileId INTEGER REFERENCES Files(Id) ); CREATE TABLE DeclFunctionParameters( FunctionId INTEGER REFERENCES DeclFunctions(Id) ON DELETE CASCADE, @@ -145,7 +152,7 @@ CREATE TABLE DeclStructs( Id INTEGER PRIMARY KEY, NamespaceId INTEGER REFERENCES Namespaces(Id), Name TEXT, - FileName TEXT, + FileId INTEGER REFERENCES Files(Id), IsMetadataMarked INTEGER ); CREATE TABLE DeclStructBaseClassRelations( @@ -186,7 +193,7 @@ CREATE TABLE DeclEnums( NamespaceId INTEGER REFERENCES Namespaces(Id), Name TEXT, UnderlyingType TEXT, - FileName TEXT + FileId INTEGER REFERENCES Files(Id) ); CREATE TABLE DeclEnumElements( EnumId INTEGER REFERENCES DeclEnums(Id) ON DELETE CASCADE, @@ -195,9 +202,9 @@ CREATE TABLE DeclEnumElements( UNIQUE (EnumId, Name) ); -CREATE INDEX Index_DeclFunctions_FileName ON DeclFunctions(FileName); -CREATE INDEX Index_DeclStructs_FileName ON DeclStructs(FileName); -CREATE INDEX Index_DeclEnums_FileName ON DeclEnums(FileName); +CREATE INDEX Index_DeclFunctions_FileId ON DeclFunctions(FileId); +CREATE INDEX Index_DeclStructs_FileId ON DeclStructs(FileId); +CREATE INDEX Index_DeclEnums_FileId ON DeclEnums(FileId); CREATE UNIQUE INDEX Index_DeclFunctions_Identity ON DeclFunctions(NamespaceId, Name); @@ -439,7 +446,7 @@ CodegenModelArchive::CodegenModelArchive(std::string_view dbPath) if (currentDatabaseVersion == 0) { // Newly created database, initialize it - m->InitializeCoreStatements(); + m->InitializeDatabase(); } else if (currentDatabaseVersion == CURRENT_DATABASE_VERSION) { // Same version, no need to do anything } else { @@ -448,6 +455,7 @@ CodegenModelArchive::CodegenModelArchive(std::string_view dbPath) } } + // Initialize core statements m->beginTransactionStmt.InitializeLazily(m->database, "BEGIN TRANSACTION"); m->commitTransactionStmt.InitializeLazily(m->database, "COMMIT TRANSACTION"); m->rollbackTransactionStmt.InitializeLazily(m->database, "ROLLBACK TRANSACTION"); @@ -460,9 +468,9 @@ CodegenModelArchive::~CodegenModelArchive() { void CodegenModelArchive::DeleteDeclsRelatedToFile(std::string_view filename) { // -Argument- -Description- // ?1 The filename to delete - m->deleteFunctionDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclFunctions WHERE FileName = ?1"sv); - m->deleteStructDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclStructs WHERE FileName = ?1;"sv); - m->deleteEnumDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclEnums WHERE FileName = ?1;"sv); + m->deleteFunctionDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclFunctions WHERE FileId = (SELECT Id FROM Files WHERE FileName = ?1)"sv); + m->deleteStructDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclStructs WHERE FileId = (SELECT Id FROM Files WHERE FileName = ?1);"sv); + m->deleteEnumDeclByFilenameStmt.InitializeLazily(m->database, "DELETE FROM DeclEnums WHERE FileId = (SELECT Id FROM Files WHERE FileName = ?1);"sv); m->BeginTransaction(); auto stmtList = { @@ -517,11 +525,16 @@ void CodegenModelArchive::StoreStruct(const DeclStruct& decl) { // ?2 Struct name // ?3 File containing the struct m->storeStructStmt.InitializeLazily(m->database, R"""( -INSERT INTO DeclStructs(NamespaceId, Name, FileName, IsMetadataMarked) - VALUES (?1, ?2, ?3, TRUE) - ON CONFLICT DO UPDATE SET - FileName = ?3 - RETURNING Id +INSERT OR IGNORE INTO Files(FileName) +VALUES (?3); + +WITH + const AS (SELECT Id AS fileId FROM Files WHERE FileName = ?3) +INSERT INTO DeclStructs(NamespaceId, Name, FileId, IsMetadataMarked) +VALUES (?1, ?2, const.fileId, TRUE) +ON CONFLICT DO UPDATE SET + FileId = const.fileId +RETURNING Id )"""sv); // -Argument- -Description- @@ -530,7 +543,7 @@ INSERT INTO DeclStructs(NamespaceId, Name, FileName, IsMetadataMarked) // ?3 Parent struct's name m->storeStructBaseClassStmt.InitializeLazily(m->database, R"""( INSERT INTO DeclStructBaseClassRelations(StructId, ParentStructNamespaceId, ParentStructName) - VALUES (?1, ?2, ?3) +VALUES (?1, ?2, ?3) )"""sv); // -Argument- -Description- @@ -541,9 +554,11 @@ INSERT INTO DeclStructBaseClassRelations(StructId, ParentStructNamespaceId, Pare // ?5 Setter name (optional) m->storeStructPropertyStmt.InitializeLazily(m->database, R"""( INSERT INTO DeclStructProperties(StructId, Name, Type, GetterName, SetterName, IsMetadataMarked) - VALUES (?1, ?2, ?3, ?4, ?5, TRUE) +VALUES (?1, ?2, ?3, ?4, ?5, TRUE) )"""sv); + int result; + sqlite3_bind_int64(m->storeStructStmt, 1, m->FindNamespace(decl.container)); sqlite3_bind_text(m->storeStructStmt, 2, decl.name.c_str(), decl.name.size(), nullptr); if (decl.sourceFile) { @@ -551,7 +566,8 @@ INSERT INTO DeclStructProperties(StructId, Name, Type, GetterName, SetterName, I } else { sqlite3_bind_text(m->storeStructStmt, 3, "", 0, nullptr); } - int result = sqlite3_step(m->storeStructStmt); + result = sqlite3_step(m->storeStructStmt); + result = sqlite3_step(m->storeStructStmt); assert(result == SQLITE_ROW); int64_t structId = sqlite3_column_int64(m->storeStructStmt, 0); sqlite3_reset(m->storeStructStmt); @@ -594,13 +610,19 @@ void CodegenModelArchive::StoreEnum(const DeclEnum& decl) { // ?2 Enum name // ?3 Enum underlying type // ?4 File containing the enum + // TODO this is breaking sqlite: cannot find const.fileId m->storeEnumStmt.InitializeLazily(m->database, R"""( -INSERT INTO DeclEnums(NamespaceId, Name, UnderlyingType, FileName) - VALUES (?1, ?2, ?3, ?4) - ON CONFLICT DO UPDATE SET - UnderlyingType = ?3, - FileName = ?4 - RETURNING Id +INSERT OR IGNORE INTO Files(FileName) +VALUES (?4); + +WITH + const AS (SELECT Id AS fileId FROM Files WHERE FileName = ?4) +INSERT INTO DeclEnums(NamespaceId, Name, UnderlyingType, FileId) +VALUES (?1, ?2, ?3, const.fileId) +ON CONFLICT DO UPDATE SET + UnderlyingType = ?3, + FileId = const.fileId +RETURNING Id )"""sv); // -Argument- -Description- @@ -609,10 +631,12 @@ INSERT INTO DeclEnums(NamespaceId, Name, UnderlyingType, FileName) // ?3 Enum element value m->storeEnumElmStmt.InitializeLazily(m->database, R"""( INSERT INTO DeclEnumElements(EnumId, Name, Value) - VALUES (?1, ?2, ?3) - ON CONFLICT DO UPDATE SET Value=?3 +VALUES (?1, ?2, ?3) +ON CONFLICT DO UPDATE SET Value=?3 )"""sv); + int result; + sqlite3_bind_int(m->storeEnumStmt, 1, m->FindNamespace(decl.container)); sqlite3_bind_text(m->storeEnumStmt, 2, decl.name.c_str(), decl.name.size(), nullptr); sqlite3_bind_text(m->storeEnumStmt, 3, decl.underlyingTypeStr.c_str(), decl.underlyingTypeStr.size(), nullptr); @@ -621,7 +645,8 @@ INSERT INTO DeclEnumElements(EnumId, Name, Value) } else { sqlite3_bind_text(m->storeEnumStmt, 4, "", 0, nullptr); } - int result = sqlite3_step(m->storeEnumStmt); + result = sqlite3_step(m->storeEnumStmt); + result = sqlite3_step(m->storeEnumStmt); assert(result == SQLITE_ROW); auto enumId = sqlite3_column_int64(m->storeEnumStmt, 0); sqlite3_reset(m->storeEnumStmt); |