@@ -290,11 +290,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
290
290
291
291
// Here, we call the global_initializer & global_statements to
292
292
// initialize and execute the global symbols
293
- void get_calls_to_global_init_and_stmts (Allocator &al, const Location &loc, SymbolTable* scope,
293
+ void get_calls_to_global_stmts (Allocator &al, const Location &loc, SymbolTable* scope,
294
294
ASR::Module_t* mod, std::vector<ASR::asr_t *> &tmp_vec) {
295
295
296
296
std::string mod_name = mod->m_name ;
297
- std::string g_func_name = mod_name + " global_init " ;
297
+ std::string g_func_name = mod_name + " global_stmts " ;
298
298
ASR::symbol_t *g_func = mod->m_symtab ->get_symbol (g_func_name);
299
299
if (g_func && !scope->get_symbol (g_func_name)) {
300
300
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t >(
@@ -306,19 +306,6 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
306
306
tmp_vec.push_back (ASRUtils::make_SubroutineCall_t_util (al, loc,
307
307
es, g_func, nullptr , 0 , nullptr , nullptr , false , false ));
308
308
}
309
-
310
- g_func_name = mod_name + " global_stmts" ;
311
- g_func = mod->m_symtab ->get_symbol (g_func_name);
312
- if (g_func && !scope->get_symbol (g_func_name)) {
313
- ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t >(
314
- ASR::make_ExternalSymbol_t (al, mod->base .base .loc ,
315
- scope, s2c (al, g_func_name), g_func,
316
- s2c (al, mod_name), nullptr , 0 , s2c (al, g_func_name),
317
- ASR::accessType::Public));
318
- scope->add_symbol (g_func_name, es);
319
- tmp_vec.push_back (ASRUtils::make_SubroutineCall_t_util (al, loc,
320
- es, g_func, nullptr , 0 , nullptr , nullptr , false , false ));
321
- }
322
309
}
323
310
324
311
template <class Struct >
@@ -4888,6 +4875,14 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4888
4875
tmp = nullptr ;
4889
4876
tmp_vec.clear ();
4890
4877
visit_stmt (*x.m_body [i]);
4878
+ if (!global_init.empty ()) {
4879
+ for (auto t: global_init) {
4880
+ if (t) {
4881
+ items.push_back (al, t);
4882
+ global_init.erase (t);
4883
+ }
4884
+ }
4885
+ }
4891
4886
if (tmp) {
4892
4887
items.push_back (al, tmp);
4893
4888
} else if (!tmp_vec.empty ()) {
@@ -4905,30 +4900,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4905
4900
mod->m_dependencies = current_module_dependencies.p ;
4906
4901
mod->n_dependencies = current_module_dependencies.n ;
4907
4902
4908
- if (global_init.n > 0 ) {
4909
- // unit->m_items is used and set to nullptr in the
4910
- // `pass_wrap_global_stmts_into_function` pass
4911
- unit->m_items = global_init.p ;
4912
- unit->n_items = global_init.size ();
4913
- std::string func_name = module_name + " global_init" ;
4914
- LCompilers::PassOptions pass_options;
4915
- pass_options.run_fun = func_name;
4916
- pass_wrap_global_stmts (al, *unit, pass_options);
4917
-
4918
- ASR::symbol_t *f_sym = unit->m_symtab ->get_symbol (func_name);
4919
- if (f_sym) {
4920
- // Add the `global_initilaizer` function into the
4921
- // module and later call this function to initialize the
4922
- // global variables like list, ...
4923
- ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4924
- f->m_symtab ->parent = mod->m_symtab ;
4925
- mod->m_symtab ->add_symbol (func_name, (ASR::symbol_t *) f);
4926
- // Erase the function in TranslationUnit
4927
- unit->m_symtab ->erase_symbol (func_name);
4928
- }
4929
- global_init.p = nullptr ;
4930
- global_init.n = 0 ;
4931
- }
4903
+ LCOMPILERS_ASSERT (global_init.empty ())
4932
4904
4933
4905
if (items.n > 0 ) {
4934
4906
unit->m_items = items.p ;
@@ -5012,7 +4984,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5012
4984
ASR::symbol_t *mod_sym = current_scope->resolve_symbol (mod_name);
5013
4985
if (mod_sym) {
5014
4986
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5015
- get_calls_to_global_init_and_stmts (al, x.base .base .loc , current_scope, mod, tmp_vec);
4987
+ get_calls_to_global_stmts (al, x.base .base .loc , current_scope, mod, tmp_vec);
5016
4988
}
5017
4989
}
5018
4990
}
@@ -5031,7 +5003,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5031
5003
ASR::symbol_t *mod_sym = current_scope->resolve_symbol (mod_name);
5032
5004
if (mod_sym) {
5033
5005
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5034
- get_calls_to_global_init_and_stmts (al, x.base .base .loc , current_scope, mod, tmp_vec);
5006
+ get_calls_to_global_stmts (al, x.base .base .loc , current_scope, mod, tmp_vec);
5035
5007
}
5036
5008
tmp = nullptr ;
5037
5009
}
@@ -8444,7 +8416,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
8444
8416
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
8445
8417
LCOMPILERS_ASSERT (mod);
8446
8418
std::vector<ASR::asr_t *> tmp_vec;
8447
- get_calls_to_global_init_and_stmts (al, tu->base .base .loc , program_scope, mod, tmp_vec);
8419
+ get_calls_to_global_stmts (al, tu->base .base .loc , program_scope, mod, tmp_vec);
8448
8420
8449
8421
for (auto i:tmp_vec) {
8450
8422
prog_body.push_back (al, ASRUtils::STMT (i));
0 commit comments