77#include < libasr/pass/pass_utils.h>
88#include < unordered_map>
99#include < set>
10-
10+ # include < unordered_set >
1111
1212extern std::string lcompilers_unique_ID;
1313
@@ -46,15 +46,28 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
4646 bool all_symbols_mangling;
4747 bool bindc_mangling = false ;
4848 bool fortran_mangling;
49+ bool c_mangling;
4950 bool should_mangle = false ;
5051 std::vector<std::string> parent_function_name;
5152 std::string module_name = " " ;
5253 SymbolTable* current_scope = nullptr ;
5354
54- SymbolRenameVisitor (bool mm, bool gm, bool im, bool am, bool bcm, bool fm) :
55+ SymbolRenameVisitor (bool mm, bool gm, bool im, bool am, bool bcm, bool fm, bool cm ) :
5556 module_name_mangling (mm), global_symbols_mangling(gm), intrinsic_symbols_mangling(im),
56- all_symbols_mangling (am), bindc_mangling(bcm), fortran_mangling(fm) {}
57+ all_symbols_mangling (am), bindc_mangling(bcm), fortran_mangling(fm) , c_mangling(cm){}
58+
59+
60+ const std::unordered_set<std::string> reserved_keywords_c = {
61+ " _Alignas" , " _Alignof" , " _Atomic" , " _Bool" , " _Complex" , " _Generic" , " _Imaginary" , " _Noreturn" , " _Static_assert" , " _Thread_local" , " auto" , " break" , " case" , " char" , " _Bool" , " const" , " continue" , " default" , " do" , " double" , " else" , " enum" , " extern" , " float" , " for" , " goto" , " if" , " int" , " long" , " register" , " return" , " short" , " signed" , " sizeof" , " static" , " struct" , " switch" , " typedef" , " union" , " unsigned" , " void" , " volatile" , " while"
62+ };
5763
64+ // TODO: Implement other backends mangling when refactoring the pass infrastructure
65+ void mangle_c (ASR::symbol_t * sym, const std::string& name){
66+ if (reserved_keywords_c.find (name) != reserved_keywords_c.end ()) {
67+ sym_to_renamed[sym] = " _xx_" +std::string (name)+" _xx_" ;
68+ }
69+ return ;
70+ }
5871
5972 std::string update_name (std::string curr_name) {
6073 if (startswith (curr_name, " _lpython" ) || startswith (curr_name, " _lfortran" ) ) {
@@ -147,7 +160,11 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
147160 sym_to_renamed[sym] = current_scope->parent ->get_unique_name (
148161 " f" + std::string (x.m_name ));
149162 }
150- }
163+ }
164+ }
165+ if ( c_mangling ) {
166+ ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
167+ mangle_c (sym , std::string (x.m_name ));
151168 }
152169 for (auto &a : x.m_symtab ->get_scope ()) {
153170 bool nested_function = is_nested_function (a.second );
@@ -178,6 +195,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
178195 std::string (x.m_name ));
179196 }
180197 }
198+
199+ if ( c_mangling ) {
200+ mangle_c (sym , std::string (x.m_name ));
201+ }
181202 }
182203
183204 void visit_GenericProcedure (const ASR::GenericProcedure_t &x) {
@@ -204,6 +225,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
204225 sym_to_renamed[sym] = update_name (x.m_name );
205226 }
206227 }
228+ if ( c_mangling ) {
229+ ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
230+ mangle_c (sym , std::string (x.m_name ));
231+ }
207232 for (auto &a : x.m_symtab ->get_scope ()) {
208233 this ->visit_symbol (*a.second );
209234 }
@@ -232,6 +257,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
232257 sym_to_renamed[sym] = update_name (x.m_name );
233258 }
234259 }
260+ if (c_mangling ) {
261+ ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
262+ mangle_c (sym , std::string (x.m_name ));
263+ }
235264 }
236265
237266 template <typename T>
@@ -240,6 +269,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
240269 ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
241270 sym_to_renamed[sym] = update_name (x.m_name );
242271 }
272+ if ( c_mangling ) {
273+ ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
274+ mangle_c (sym , std::string (x.m_name ));
275+ }
243276 for (auto &a : x.m_symtab ->get_scope ()) {
244277 this ->visit_symbol (*a.second );
245278 }
@@ -521,8 +554,9 @@ void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
521554 if (pass_options.mangle_underscore ) {
522555 lcompilers_unique_ID = " " ;
523556 }
524- if (!any_present || (!(pass_options.mangle_underscore ||
525- pass_options.fortran_mangling ) && lcompilers_unique_ID.empty ())) {
557+ if ((!any_present || (!(pass_options.mangle_underscore ||
558+ pass_options.fortran_mangling ) && lcompilers_unique_ID.empty ())) &&
559+ !pass_options.c_mangling ) {
526560 // `--mangle-underscore` doesn't require `lcompilers_unique_ID`
527561 // `lcompilers_unique_ID` is not mandatory for `--apply-fortran-mangling`
528562 return ;
@@ -532,7 +566,8 @@ void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
532566 pass_options.intrinsic_symbols_mangling ,
533567 pass_options.all_symbols_mangling ,
534568 pass_options.bindc_mangling ,
535- pass_options.fortran_mangling );
569+ pass_options.fortran_mangling ,
570+ pass_options.c_mangling );
536571 v.visit_TranslationUnit (unit);
537572 UniqueSymbolVisitor u (al, v.sym_to_renamed );
538573 u.visit_TranslationUnit (unit);
0 commit comments