From 1af2280572487f0c7fa4b7686a1a71d24839a32b Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:24:59 +0530 Subject: [PATCH 1/6] Refactor symbols type 1 --- src/libasr/pass/unique_symbols.cpp | 33 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 30578fb605..d2ecc83f1f 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -40,6 +40,7 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { bool intrinsic_symbols_mangling; bool all_symbols_mangling; bool should_mangle = false; + std::string module_name = ""; SymbolRenameVisitor( bool mm, bool gm, bool im, bool am) : module_name_mangling(mm), @@ -51,7 +52,7 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { return curr_name; } - return curr_name + "_" + lcompilers_unique_ID; + return module_name + curr_name + "_" + lcompilers_unique_ID; } void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { @@ -71,6 +72,8 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { void visit_Module(const ASR::Module_t &x) { ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); bool should_mangle_copy = should_mangle; + std::string mod_name_copy = module_name; + module_name = std::string(x.m_name) + "_"; if (all_symbols_mangling || module_name_mangling || should_mangle) { sym_to_renamed[sym] = update_name(x.m_name); } @@ -82,6 +85,7 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { visit_symbol(*a.second); } should_mangle = should_mangle_copy; + module_name = mod_name_copy; } void visit_Function(const ASR::Function_t &x) { @@ -97,25 +101,28 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } - void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { + template + void visit_symbols_1(T &x) { if (all_symbols_mangling || should_mangle) { ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); sym_to_renamed[sym] = update_name(x.m_name); } } + void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { + visit_symbols_1(x); + } + void visit_CustomOperator(const ASR::CustomOperator_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } + visit_symbols_1(x); } void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } + visit_symbols_1(x); + } + + void visit_Variable(const ASR::Variable_t &x) { + visit_symbols_1(x); } void visit_StructType(const ASR::StructType_t &x) { @@ -154,12 +161,6 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } - void visit_Variable(const ASR::Variable_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - } void visit_ClassType(const ASR::ClassType_t &x) { if (x.m_abi != ASR::abiType::BindC) { From da80ea67de558a4c0c3802ba880098cb170cfca2 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:26:26 +0530 Subject: [PATCH 2/6] Refactor symbols type 2 --- src/libasr/pass/unique_symbols.cpp | 38 +++++++----------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index d2ecc83f1f..40d9938e59 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -125,7 +125,8 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { visit_symbols_1(x); } - void visit_StructType(const ASR::StructType_t &x) { + template + void visit_symbols_2(T &x) { if (x.m_abi != ASR::abiType::BindC) { if (all_symbols_mangling || should_mangle) { ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); @@ -137,41 +138,20 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } + void visit_StructType(const ASR::StructType_t &x) { + visit_symbols_2(x); + } + void visit_EnumType(const ASR::EnumType_t &x) { - if (x.m_abi != ASR::abiType::BindC) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_2(x); } void visit_UnionType(const ASR::UnionType_t &x) { - if (x.m_abi != ASR::abiType::BindC) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_2(x); } - void visit_ClassType(const ASR::ClassType_t &x) { - if (x.m_abi != ASR::abiType::BindC) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_2(x); } void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { From 4b7403b467b207ee413963faaa6d8945b6e1260d Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:27:29 +0530 Subject: [PATCH 3/6] Refactor symbols type 3 --- src/libasr/pass/unique_symbols.cpp | 31 +++++++++--------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 40d9938e59..931ef9b32d 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -163,7 +163,8 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } - void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { + template + void visit_symbols_3(T &x) { if (all_symbols_mangling || should_mangle) { ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); sym_to_renamed[sym] = update_name(x.m_name); @@ -173,34 +174,20 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } + void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { + visit_symbols_3(x); + } + void visit_Block(const ASR::Block_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_3(x); } void visit_Requirement(const ASR::Requirement_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_3(x); } void visit_Template(const ASR::Template_t &x) { - if (all_symbols_mangling || should_mangle) { - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = update_name(x.m_name); - } - for (auto &a : x.m_symtab->get_scope()) { - this->visit_symbol(*a.second); - } + visit_symbols_3(x); } }; From c5b91ced9b431a931f5e3b173e1ffeaf29f5e40e Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:31:45 +0530 Subject: [PATCH 4/6] Refactor symbol updater with type 1 --- src/libasr/pass/unique_symbols.cpp | 61 +++++------------------------- 1 file changed, 9 insertions(+), 52 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 931ef9b32d..b96ec9db25 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -222,8 +222,9 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { current_scope = current_scope_copy; } - void visit_Program(const ASR::Program_t &x) { - ASR::Program_t& xx = const_cast(x); + template + void update_symbols_1(const T &x) { + T& xx = const_cast(x); std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { @@ -250,60 +251,16 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { current_scope = current_scope_copy; } + void visit_Program(const ASR::Program_t &x) { + update_symbols_1(x); + } + void visit_Module(const ASR::Module_t &x) { - ASR::Module_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - for (size_t i=0; iget_scope(); - for (auto &a : x.m_symtab->get_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_1(x); } void visit_Function(const ASR::Function_t &x) { - ASR::Function_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - for (size_t i=0; iget_scope(); - for (auto &a : x.m_symtab->get_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_1(x); } void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { From 03ef3bdba7e89545c093d23f05b0373f7eb0fa90 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:33:14 +0530 Subject: [PATCH 5/6] Refactor symbol updater with type 2 --- src/libasr/pass/unique_symbols.cpp | 77 ++++-------------------------- 1 file changed, 9 insertions(+), 68 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index b96ec9db25..f00e9fc2dc 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -295,8 +295,9 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { } } - void visit_StructType(const ASR::StructType_t &x) { - ASR::StructType_t& xx = const_cast(x); + template + void update_symbols_2(const T &x) { + T& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -331,76 +332,16 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { current_scope = current_scope_copy; } + void visit_StructType(const ASR::StructType_t &x) { + update_symbols_2(x); + } + void visit_EnumType(const ASR::EnumType_t &x) { - ASR::EnumType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - std::map current_scope_copy = current_scope; - for (size_t i=0; iget_scope(); - for (size_t i=0; iget_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_2(x); } void visit_UnionType(const ASR::UnionType_t &x) { - ASR::UnionType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - std::map current_scope_copy = current_scope; - for (size_t i=0; iget_scope(); - for (size_t i=0; iget_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_2(x); } void visit_Variable(const ASR::Variable_t &x) { From 12981c1fcb71f690b9dbf35ee55c78440104d730 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 3 Aug 2023 18:34:59 +0530 Subject: [PATCH 6/6] Refactor symbol update with type 3 --- src/libasr/pass/unique_symbols.cpp | 63 +++++------------------------- 1 file changed, 10 insertions(+), 53 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index f00e9fc2dc..c6af800a5d 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -388,8 +388,9 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { } } - void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { - ASR::AssociateBlock_t& xx = const_cast(x); + template + void update_symbols_3(const T &x) { + T& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -408,64 +409,20 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { current_scope = current_scope_copy; } + void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { + update_symbols_3(x); + } + void visit_Block(const ASR::Block_t &x) { - ASR::Block_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - std::map current_scope_copy = current_scope; - current_scope = x.m_symtab->get_scope(); - for (auto &a : x.m_symtab->get_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_3(x); } void visit_Requirement(const ASR::Requirement_t &x) { - ASR::Requirement_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - std::map current_scope_copy = current_scope; - current_scope = x.m_symtab->get_scope(); - for (auto &a : x.m_symtab->get_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_3(x); } void visit_Template(const ASR::Template_t &x) { - ASR::Template_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - xx.m_name = s2c(al, sym_to_new_name[sym]); - } - std::map current_scope_copy = current_scope; - current_scope = x.m_symtab->get_scope(); - for (auto &a : x.m_symtab->get_scope()) { - visit_symbol(*a.second); - } - for (auto &a: current_scope) { - if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { - xx.m_symtab->erase_symbol(a.first); - xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); - } - } - current_scope = current_scope_copy; + update_symbols_3(x); } };