Skip to content

Commit 12de970

Browse files
XX Wrap all the symbols from global scope into a function
1 parent 6694acd commit 12de970

29 files changed

+77
-36
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,26 @@ std::string SymbolTable::get_unique_name(const std::string &name) {
134134
return unique_name;
135135
}
136136

137+
void SymbolTable::move_symbols_from_global_scope(SymbolTable *fn_scope,
138+
std::vector<std::string> &syms) {
139+
for (auto &a : scope) {
140+
switch (a.second->type) {
141+
case (ASR::symbolType::Variable) : {
142+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
143+
v->m_parent_symtab = fn_scope;
144+
fn_scope->add_symbol(a.first, (ASR::symbol_t *)v);
145+
syms.push_back(a.first);
146+
break;
147+
}
148+
case (ASR::symbolType::Function) : {
149+
// Pass
150+
break;
151+
}
152+
default : {
153+
LCompilersException("Not implemented!");
154+
};
155+
}
156+
}
157+
}
158+
137159
} // namespace LCompilers

src/libasr/asr_scopes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ struct SymbolTable {
8080
size_t n_scope_names, char **m_scope_names);
8181

8282
std::string get_unique_name(const std::string &name);
83+
84+
void move_symbols_from_global_scope(SymbolTable *fn_scope,
85+
std::vector<std::string> &syms);
8386
};
8487

8588
} // namespace LCompilers

src/libasr/pass/global_stmts.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,14 @@ void pass_wrap_global_stmts_into_function(Allocator &al,
162162
}
163163
}
164164

165+
void pass_wrap_global_vars_into_function(ASR::TranslationUnit_t &unit) {
166+
std::vector<std::string> symbols;
167+
ASR::symbol_t *fn = unit.m_global_scope->get_symbol("_lpython_main_program");
168+
unit.m_global_scope->move_symbols_from_global_scope(
169+
ASR::down_cast<ASR::Function_t>(fn)->m_symtab, symbols);
170+
for (auto &a: symbols) {
171+
unit.m_global_scope->erase_symbol(a);
172+
}
173+
}
174+
165175
} // namespace LCompilers

src/libasr/pass/global_stmts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace LCompilers {
88

99
void pass_wrap_global_stmts_into_function(Allocator &al, ASR::TranslationUnit_t &unit,
1010
const LCompilers::PassOptions& pass_options);
11+
void pass_wrap_global_vars_into_function(ASR::TranslationUnit_t &unit);
1112

1213
} // namespace LCompilers
1314

src/libasr/pass/global_stmts_program.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void pass_wrap_global_stmts_into_program(Allocator &al,
2626
prog_body.reserve(al, 1);
2727
if (unit.n_items > 0) {
2828
pass_wrap_global_stmts_into_function(al, unit, pass_options);
29+
pass_wrap_global_vars_into_function(unit);
2930
ASR::symbol_t *fn = unit.m_global_scope->get_symbol(program_fn_name);
3031
if (ASR::is_a<ASR::Function_t>(*fn)
3132
&& ASR::down_cast<ASR::Function_t>(fn)->m_return_var == nullptr) {

tests/reference/asr-bindc_01-6d521a9.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_01-6d521a9.stdout",
9-
"stdout_hash": "35f2201203f83b79c5c537d35037267fd0eea6065948d2cc08631f17",
9+
"stdout_hash": "1040f4d6e093e49f3eaf317428bb60e911fa9a50deae0f22684715fa",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 3 {}) _lpython_main_program [] [] [(CPtrToPointer (Var 1 queries) (Var 1 x) ()) (Print () [(Var 1 queries) (Var 1 x)] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), queries: (Variable 1 queries [] Local () () Default (CPtr) Source Public Required .false.), x: (Variable 1 x [] Local () () Default (Pointer (Integer 2 [])) Source Public Required .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 3 {queries: (Variable 3 queries [] Local () () Default (CPtr) Source Public Required .false.), x: (Variable 3 x [] Local () () Default (Pointer (Integer 2 [])) Source Public Required .false.)}) _lpython_main_program [] [] [(CPtrToPointer (Var 3 queries) (Var 3 x) ()) (Print () [(Var 3 queries) (Var 3 x)] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])

tests/reference/asr-bindc_02-bc1a7ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "340427cb095972a4cbc1f394d08a22161da70f1c14d2652ff69b0658",
9+
"stdout_hash": "725ebaf473e00bdbf1bf80e13c83ba50f60192f22d54b4b880942e17",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_02-bc1a7ea.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-expr_07-7742668.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr_07-7742668.stdout",
9-
"stdout_hash": "387ae27c01005ee7bffb009fe98deacd5d4bdac8bf0710beff8ed697",
9+
"stdout_hash": "42e89818ed3238a32ee4b3910239dad4b8e5232617d0a6cf3b3dd7bc",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)