From 23c6d10b73767879ed1b0678d675e1cadea92ac9 Mon Sep 17 00:00:00 2001 From: LeDron12 Date: Sun, 7 Aug 2022 19:38:41 +0300 Subject: [PATCH 1/3] enum typedef + printf --- project/src/transpiler/CMakeLists.txt | 84 ++++++++++---------- project/src/transpiler/enum_manager.cpp | 14 ++++ project/src/transpiler/enum_manager.h | 4 +- project/src/transpiler/enumdecl.cpp | 14 +++- project/src/transpiler/transpile_helper.cpp | 14 ++++ project/tests/main/enum/{enum.c => enum01.c} | 32 +++----- project/tests/main/enum/enum02.c | 24 ++++++ 7 files changed, 119 insertions(+), 67 deletions(-) rename project/tests/main/enum/{enum.c => enum01.c} (79%) create mode 100644 project/tests/main/enum/enum02.c diff --git a/project/src/transpiler/CMakeLists.txt b/project/src/transpiler/CMakeLists.txt index 6e2d5eb2..e08dbc1a 100755 --- a/project/src/transpiler/CMakeLists.txt +++ b/project/src/transpiler/CMakeLists.txt @@ -17,55 +17,55 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") # Source files list set(SOURCE_FILES - # Headers - vardecl.h - analyzers.h - matchers.h - util.h - memory_manager.h - unit_transpiler.h - eo_object.h - function_manager.h - transpile_helper.h - recorddecl.h - record_manager.h - aliases.h - tracer.h - process_variables.h - enumdecl.h - enum_manager.h + # Headers + vardecl.h + analyzers.h + matchers.h + util.h + memory_manager.h + unit_transpiler.h + eo_object.h + function_manager.h + transpile_helper.h + recorddecl.h + record_manager.h + aliases.h + tracer.h + process_variables.h + enumdecl.h + enum_manager.h - # Sources - main.cpp - vardecl.cpp - analyzers.cpp - matchers.cpp - util.cpp - memory_manager.cpp - unit_transpiler.cpp - eo_object.cpp - function_manager.cpp - transpile_helper.cpp - recorddecl.cpp - record_manager.cpp - tracer.cpp - process_variables.cpp - enumdecl.cpp - enum_manager.cpp) + # Sources + main.cpp + vardecl.cpp + analyzers.cpp + matchers.cpp + util.cpp + memory_manager.cpp + unit_transpiler.cpp + eo_object.cpp + function_manager.cpp + transpile_helper.cpp + recorddecl.cpp + record_manager.cpp + tracer.cpp + process_variables.cpp + enumdecl.cpp + enum_manager.cpp) # add_executable(recvisitor main.cpp) add_executable(c2eo ${SOURCE_FILES}) # llvm libraries target_link_libraries(c2eo - clangAST - clangASTMatchers - clangBasic - clangFrontend - clangSerialization - clangTooling - LLVMSupport - LLVMFrontendOpenMP) + clangAST + clangASTMatchers + clangBasic + clangFrontend + clangSerialization + clangTooling + LLVMSupport + LLVMFrontendOpenMP) set(LLVM_LINK_COMPONENTS Support) diff --git a/project/src/transpiler/enum_manager.cpp b/project/src/transpiler/enum_manager.cpp index 00735a55..c1342398 100644 --- a/project/src/transpiler/enum_manager.cpp +++ b/project/src/transpiler/enum_manager.cpp @@ -27,6 +27,11 @@ EnumType EnumManager::Add(const clang::EnumDecl *id, std::string name, size_t size, const std::vector &values) { + // llvm::outs() << "enum " << name << "\n"; + // for (auto val : values) { + // llvm::outs() << "enum const " << val.name << " " << val.value << + // "\n"; + // } EnumType enumType = {id, std::move(name), size, values}; enum_types.push_back(enumType); return enumType; @@ -44,3 +49,12 @@ EnumConstantType *EnumManager::GetConstantById( } return nullptr; } + +EnumType *EnumManager::GetById(const clang::EnumDecl *id) { + for (auto type = enum_types.begin(); type != enum_types.end(); type++) { + if (type->id == id) { + return type.base(); + } + } + return nullptr; +} \ No newline at end of file diff --git a/project/src/transpiler/enum_manager.h b/project/src/transpiler/enum_manager.h index cfef6642..11fb50fa 100644 --- a/project/src/transpiler/enum_manager.h +++ b/project/src/transpiler/enum_manager.h @@ -56,7 +56,9 @@ class EnumManager { EnumConstantType *GetConstantById(const clang::EnumConstantDecl *id); + EnumType *GetById(const clang::EnumDecl *id); + std::vector enum_types; }; -#endif // PROJECT_SRC_TRANSPILER_ENUM_MANAGER_H_ +#endif // PROJECT_SRC_TRANSPILER_ENUM_MANAGER_H_ \ No newline at end of file diff --git a/project/src/transpiler/enumdecl.cpp b/project/src/transpiler/enumdecl.cpp index 7230227c..4b4d2531 100644 --- a/project/src/transpiler/enumdecl.cpp +++ b/project/src/transpiler/enumdecl.cpp @@ -38,12 +38,18 @@ EnumType ProcessEnumDecl(const clang::EnumDecl *ED) { } extern UnitTranspiler transpiler; - // if (transpiler.enum_manager_.GetById(ED)) { - // return {}; - // } + if (transpiler.enum_manager_.GetById(ED) != nullptr) { + return {}; + } std::vector constants; - std::string enum_name = "en-" + ED->getNameAsString(); + std::string enum_name = "en-"; + try { + enum_name += ED->getNameAsString(); + } catch (std::exception &) { + enum_name += "noname"; + } + uint64_t size = 0; for (auto decl = ED->decls_begin(); decl != ED->decls_end(); decl++) { diff --git a/project/src/transpiler/transpile_helper.cpp b/project/src/transpiler/transpile_helper.cpp index edd4cfed..3cc2d050 100644 --- a/project/src/transpiler/transpile_helper.cpp +++ b/project/src/transpiler/transpile_helper.cpp @@ -31,6 +31,7 @@ #include #include +#include "src/transpiler/enumdecl.h" #include "src/transpiler/memory_manager.h" #include "src/transpiler/process_variables.h" #include "src/transpiler/recorddecl.h" @@ -139,6 +140,8 @@ EOObject GetSwitchEOObject(const SwitchStmt *p_stmt); EOObject GetCaseCondEOObject(const vector &all_cases, const EOObject &switch_exp, size_t i); +void AppendDeclStmt(const DeclStmt *stmt); + extern UnitTranspiler transpiler; extern ASTContext *context; @@ -366,6 +369,8 @@ EOObject GetStmtEOObject(const Stmt *stmt) { return GetFloatingLiteralEOObject(op); } if (stmt_class == Stmt::DeclStmtClass) { + const auto *op = dyn_cast(stmt); + AppendDeclStmt(op); return EOObject(EOObjectType::EO_EMPTY); } if (stmt_class == Stmt::CallExprClass) { @@ -602,6 +607,15 @@ EOObject GetSwitchEOObject(const SwitchStmt *p_stmt) { return goto_object; } +void AppendDeclStmt(const DeclStmt *stmt) { + for (auto decl : stmt->decls()) { + if (decl->getKind() == Decl::Kind::Enum) { + auto *enum_decl = dyn_cast(decl); + ProcessEnumDecl(enum_decl); + } + } +} + EOObject GetCaseCondEOObject(const vector &all_cases, const EOObject &switch_exp, size_t i) { EOObject eq_object{"eq"}; diff --git a/project/tests/main/enum/enum.c b/project/tests/main/enum/enum01.c similarity index 79% rename from project/tests/main/enum/enum.c rename to project/tests/main/enum/enum01.c index 0b51b27f..1bff9e08 100644 --- a/project/tests/main/enum/enum.c +++ b/project/tests/main/enum/enum01.c @@ -1,28 +1,20 @@ #include "stdio.h" -enum numbers_1 { - ZERO, - ONE, - TWO -}; +enum numbers_1 { ZERO, ONE, TWO }; -enum numbers_2 { - FIVE = 5, - TEN = 10, - FIFTEEN = 15 -}; +enum numbers_2 { FIVE = 5, TEN = 10, FIFTEEN = 15 }; enum numbers_3 { - TWENTY_FIVE = 25, - TWENTY_SIX, - TWENTY_SEVEN, - TWENTY_FIVE_2 = TWENTY_FIVE, - TWENTY_SIX_2, - TWENTY_SEVEN_2, - TWENTY_FOUR = 24, - MINUS_FOUR = -4, - MINUS_THREE, - MINUS_TWO + TWENTY_FIVE = 25, + TWENTY_SIX, + TWENTY_SEVEN, + TWENTY_FIVE_2 = TWENTY_FIVE, + TWENTY_SIX_2, + TWENTY_SEVEN_2, + TWENTY_FOUR = 24, + MINUS_FOUR = -4, + MINUS_THREE, + MINUS_TWO }; int main() { diff --git a/project/tests/main/enum/enum02.c b/project/tests/main/enum/enum02.c new file mode 100644 index 00000000..26d118c4 --- /dev/null +++ b/project/tests/main/enum/enum02.c @@ -0,0 +1,24 @@ +#include + +enum fred { a, b, c }; + +int main() { + printf("%d\n", a); + printf("%d\n", b); + printf("%d\n", c); + + enum fred d; + + typedef enum { e, f, g } h; + printf("%d\n", e); + printf("%d\n", f); + printf("%d\n", g); + + typedef enum { i, j, k } m; + + printf("%d\n", i); + printf("%d\n", j); + printf("%d\n", k); + + return 0; +} \ No newline at end of file From 6f2cf3c424a4316254a412e81662a74ab0c19067 Mon Sep 17 00:00:00 2001 From: LeDron12 Date: Sun, 7 Aug 2022 19:41:28 +0300 Subject: [PATCH 2/3] enum typedef + printf --- project/src/transpiler/enum_manager.cpp | 2 +- project/src/transpiler/enum_manager.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/src/transpiler/enum_manager.cpp b/project/src/transpiler/enum_manager.cpp index c1342398..6aa11e0f 100644 --- a/project/src/transpiler/enum_manager.cpp +++ b/project/src/transpiler/enum_manager.cpp @@ -57,4 +57,4 @@ EnumType *EnumManager::GetById(const clang::EnumDecl *id) { } } return nullptr; -} \ No newline at end of file +} diff --git a/project/src/transpiler/enum_manager.h b/project/src/transpiler/enum_manager.h index 11fb50fa..71d3a77c 100644 --- a/project/src/transpiler/enum_manager.h +++ b/project/src/transpiler/enum_manager.h @@ -61,4 +61,4 @@ class EnumManager { std::vector enum_types; }; -#endif // PROJECT_SRC_TRANSPILER_ENUM_MANAGER_H_ \ No newline at end of file +#endif // PROJECT_SRC_TRANSPILER_ENUM_MANAGER_H_ From be6f4b68b50a907d775ba0371abf037dd978ffe4 Mon Sep 17 00:00:00 2001 From: LeDron12 Date: Sun, 7 Aug 2022 20:12:03 +0300 Subject: [PATCH 3/3] enum typedef + printf --- project/src/transpiler/transpile_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/src/transpiler/transpile_helper.cpp b/project/src/transpiler/transpile_helper.cpp index 3cc2d050..6328a985 100644 --- a/project/src/transpiler/transpile_helper.cpp +++ b/project/src/transpiler/transpile_helper.cpp @@ -608,7 +608,7 @@ EOObject GetSwitchEOObject(const SwitchStmt *p_stmt) { } void AppendDeclStmt(const DeclStmt *stmt) { - for (auto decl : stmt->decls()) { + for (auto *decl : stmt->decls()) { if (decl->getKind() == Decl::Kind::Enum) { auto *enum_decl = dyn_cast(decl); ProcessEnumDecl(enum_decl);