@@ -45,15 +45,15 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
45
45
bool intrinsic_symbols_mangling;
46
46
bool all_symbols_mangling;
47
47
bool bindc_mangling = false ;
48
+ bool fortran_mangling;
48
49
bool should_mangle = false ;
49
50
std::vector<std::string> parent_function_name;
50
51
std::string module_name = " " ;
51
52
SymbolTable* current_scope = nullptr ;
52
53
53
- SymbolRenameVisitor (
54
- bool mm, bool gm, bool im, bool am, bool bcm) : module_name_mangling(mm),
55
- global_symbols_mangling (gm), intrinsic_symbols_mangling(im),
56
- all_symbols_mangling(am), bindc_mangling(bcm){}
54
+ SymbolRenameVisitor (bool mm, bool gm, bool im, bool am, bool bcm, bool fm) :
55
+ module_name_mangling (mm), global_symbols_mangling(gm), intrinsic_symbols_mangling(im),
56
+ all_symbols_mangling (am), bindc_mangling(bcm), fortran_mangling(fm) {}
57
57
58
58
59
59
std::string update_name (std::string curr_name) {
@@ -84,12 +84,17 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
84
84
}
85
85
86
86
void visit_Program (const ASR::Program_t &x) {
87
+ SymbolTable *current_scope_copy = current_scope;
88
+ current_scope = x.m_symtab ;
87
89
for (auto &a : x.m_symtab ->get_scope ()) {
88
90
visit_symbol (*a.second );
89
91
}
92
+ current_scope = current_scope_copy;
90
93
}
91
94
92
95
void visit_Module (const ASR::Module_t &x) {
96
+ SymbolTable *current_scope_copy = current_scope;
97
+ current_scope = x.m_symtab ;
93
98
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
94
99
bool should_mangle_copy = should_mangle;
95
100
std::string mod_name_copy = module_name;
@@ -106,6 +111,7 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
106
111
}
107
112
should_mangle = should_mangle_copy;
108
113
module_name = mod_name_copy;
114
+ current_scope = current_scope_copy;
109
115
}
110
116
111
117
bool is_nested_function (ASR::symbol_t *sym) {
@@ -124,12 +130,24 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
124
130
}
125
131
126
132
void visit_Function (const ASR::Function_t &x) {
133
+ SymbolTable *current_scope_copy = current_scope;
134
+ current_scope = x.m_symtab ;
127
135
ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType (x);
128
136
if (bindc_mangling || f_type->m_abi != ASR::abiType::BindC) {
129
137
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
130
138
if (all_symbols_mangling || should_mangle) {
131
139
sym_to_renamed[sym] = update_name (x.m_name );
132
140
}
141
+ if ( fortran_mangling ) {
142
+ if ( sym_to_renamed.find (sym) != sym_to_renamed.end ()
143
+ && startswith (sym_to_renamed[sym], " _" ) ) {
144
+ sym_to_renamed[sym] = current_scope->parent ->get_unique_name (
145
+ " f" + sym_to_renamed[sym]);
146
+ } else if ( startswith (x.m_name , " _" ) ) {
147
+ sym_to_renamed[sym] = current_scope->parent ->get_unique_name (
148
+ " f" + std::string (x.m_name ));
149
+ }
150
+ }
133
151
}
134
152
for (auto &a : x.m_symtab ->get_scope ()) {
135
153
bool nested_function = is_nested_function (a.second );
@@ -141,14 +159,25 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
141
159
parent_function_name.pop_back ();
142
160
}
143
161
}
162
+ current_scope = current_scope_copy;
144
163
}
145
164
146
165
template <typename T>
147
166
void visit_symbols_1 (T &x) {
167
+ ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
148
168
if (all_symbols_mangling || should_mangle) {
149
- ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&x);
150
169
sym_to_renamed[sym] = update_name (x.m_name );
151
170
}
171
+ if ( fortran_mangling ) {
172
+ if ( sym_to_renamed.find (sym) != sym_to_renamed.end ()
173
+ && startswith (sym_to_renamed[sym], " _" ) ) {
174
+ sym_to_renamed[sym] = current_scope->get_unique_name (" v" +
175
+ sym_to_renamed[sym]);
176
+ } else if ( startswith (x.m_name , " _" ) ) {
177
+ sym_to_renamed[sym] = current_scope->get_unique_name (" v" +
178
+ std::string (x.m_name ));
179
+ }
180
+ }
152
181
}
153
182
154
183
void visit_GenericProcedure (const ASR::GenericProcedure_t &x) {
@@ -472,19 +501,38 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
472
501
473
502
void pass_unique_symbols (Allocator &al, ASR::TranslationUnit_t &unit,
474
503
const LCompilers::PassOptions& pass_options) {
504
+ /*
505
+ * This pass is applied iff the following options are passed; otherwise, return
506
+ * MANGLING_OPTION="--all-mangling"
507
+ * MANGLING_OPTION="--module-mangling"
508
+ * MANGLING_OPTION="--global-mangling"
509
+ * MANGLING_OPTION="--intrinsic-mangling"
510
+ * COMPILER_SPECIFIC_OPTION="--generate-object-code" // LFortran
511
+ * COMPILER_SPECIFIC_OPTION="--separate-compilation" // LPython
512
+ * Usage:
513
+ * `$MANGLING_OPTION $COMPILER_SPECIFIC_OPTION`
514
+ * The following are used by LFortran, Usage:
515
+ * `$MANGLING_OPTIONS --mangle-underscore [$COMPILER_SPECIFIC_OPTION]`
516
+ * * `--apply-fortran-mangling [$MANGLING_OPTION] [$COMPILER_SPECIFIC_OPTION]`
517
+ */
475
518
bool any_present = (pass_options.module_name_mangling || pass_options.global_symbols_mangling ||
476
- pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling || pass_options.bindc_mangling );
519
+ pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling ||
520
+ pass_options.bindc_mangling || pass_options.fortran_mangling );
477
521
if (pass_options.mangle_underscore ) {
478
522
lcompilers_unique_ID = " " ;
479
523
}
480
- if (!any_present || ( !pass_options.mangle_underscore && lcompilers_unique_ID.empty () )) {
524
+ if (!any_present || (!(pass_options.mangle_underscore ||
525
+ pass_options.fortran_mangling ) && lcompilers_unique_ID.empty ())) {
526
+ // `--mangle-underscore` doesn't require `lcompilers_unique_ID`
527
+ // `lcompilers_unique_ID` is not mandatory for `--apply-fortran-mangling`
481
528
return ;
482
529
}
483
530
SymbolRenameVisitor v (pass_options.module_name_mangling ,
484
531
pass_options.global_symbols_mangling ,
485
532
pass_options.intrinsic_symbols_mangling ,
486
533
pass_options.all_symbols_mangling ,
487
- pass_options.bindc_mangling );
534
+ pass_options.bindc_mangling ,
535
+ pass_options.fortran_mangling );
488
536
v.visit_TranslationUnit (unit);
489
537
UniqueSymbolVisitor u (al, v.sym_to_renamed );
490
538
u.visit_TranslationUnit (unit);
0 commit comments