7
7
#include < libasr/pass/pass_utils.h>
8
8
#include < unordered_map>
9
9
#include < set>
10
-
10
+ # include < unordered_set >
11
11
12
12
extern std::string lcompilers_unique_ID;
13
13
@@ -46,15 +46,28 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
46
46
bool all_symbols_mangling;
47
47
bool bindc_mangling = false ;
48
48
bool fortran_mangling;
49
+ bool c_mangling;
49
50
bool should_mangle = false ;
50
51
std::vector<std::string> parent_function_name;
51
52
std::string module_name = " " ;
52
53
SymbolTable* current_scope = nullptr ;
53
54
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 ) :
55
56
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
+ };
57
63
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
+ }
58
71
59
72
std::string update_name (std::string curr_name) {
60
73
if (startswith (curr_name, " _lpython" ) || startswith (curr_name, " _lfortran" ) ) {
@@ -147,7 +160,11 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
147
160
sym_to_renamed[sym] = current_scope->parent ->get_unique_name (
148
161
" f" + std::string (x.m_name ));
149
162
}
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 ));
151
168
}
152
169
for (auto &a : x.m_symtab ->get_scope ()) {
153
170
bool nested_function = is_nested_function (a.second );
@@ -178,6 +195,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
178
195
std::string (x.m_name ));
179
196
}
180
197
}
198
+
199
+ if ( c_mangling ) {
200
+ mangle_c (sym , std::string (x.m_name ));
201
+ }
181
202
}
182
203
183
204
void visit_GenericProcedure (const ASR::GenericProcedure_t &x) {
@@ -204,6 +225,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
204
225
sym_to_renamed[sym] = update_name (x.m_name );
205
226
}
206
227
}
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
+ }
207
232
for (auto &a : x.m_symtab ->get_scope ()) {
208
233
this ->visit_symbol (*a.second );
209
234
}
@@ -232,6 +257,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
232
257
sym_to_renamed[sym] = update_name (x.m_name );
233
258
}
234
259
}
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
+ }
235
264
}
236
265
237
266
template <typename T>
@@ -240,6 +269,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
240
269
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
241
270
sym_to_renamed[sym] = update_name (x.m_name );
242
271
}
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
+ }
243
276
for (auto &a : x.m_symtab ->get_scope ()) {
244
277
this ->visit_symbol (*a.second );
245
278
}
@@ -521,8 +554,9 @@ void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
521
554
if (pass_options.mangle_underscore ) {
522
555
lcompilers_unique_ID = " " ;
523
556
}
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 ) {
526
560
// `--mangle-underscore` doesn't require `lcompilers_unique_ID`
527
561
// `lcompilers_unique_ID` is not mandatory for `--apply-fortran-mangling`
528
562
return ;
@@ -532,7 +566,8 @@ void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
532
566
pass_options.intrinsic_symbols_mangling ,
533
567
pass_options.all_symbols_mangling ,
534
568
pass_options.bindc_mangling ,
535
- pass_options.fortran_mangling );
569
+ pass_options.fortran_mangling ,
570
+ pass_options.c_mangling );
536
571
v.visit_TranslationUnit (unit);
537
572
UniqueSymbolVisitor u (al, v.sym_to_renamed );
538
573
u.visit_TranslationUnit (unit);
0 commit comments