diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 30578fb605..c6af800a5d 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,40 +101,32 @@ 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_StructType(const ASR::StructType_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); - } + void visit_Variable(const ASR::Variable_t &x) { + visit_symbols_1(x); } - void visit_EnumType(const ASR::EnumType_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); @@ -142,35 +138,20 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } } - 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); - } + void visit_StructType(const ASR::StructType_t &x) { + visit_symbols_2(x); } - 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_EnumType(const ASR::EnumType_t &x) { + visit_symbols_2(x); + } + + void visit_UnionType(const ASR::UnionType_t &x) { + 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) { @@ -182,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); @@ -192,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); } }; @@ -254,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()) { @@ -282,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) { @@ -370,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]); @@ -406,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) { @@ -522,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]); @@ -542,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); } };