Skip to content

Commit 2eec664

Browse files
Import global statements
1 parent 6ebd4b8 commit 2eec664

File tree

10 files changed

+237
-108
lines changed

10 files changed

+237
-108
lines changed

examples/a.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# x: i32 = 1
2+
print(123)
3+
x: list[i32]
4+
x = [1]
5+
# assert True

examples/expr2.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
def main0():
2-
x: i32
3-
x = (2+3)*5
4-
print(x)
5-
6-
main0()
7-
8-
# Not implemented yet in LPython:
9-
#if __name__ == "__main__":
10-
# main()
1+
# from a import x
2+
import a
3+
print(456)

integration_tests/global_syms_03_a.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from lpython import i32
22

3+
print("Imported from global_syms_03_a")
4+
35
l_1: list[str] = ['Monday', 'Tuesday', 'Wednesday']
46

5-
def populate_lists() -> list[i32]:
6-
return [10, -20]
7-
l_2: list[i32] = populate_lists()
7+
# def populate_lists() -> list[i32]:
8+
# return [10, -20]
9+
# l_2: list[i32] = populate_lists()
10+
11+
# l_1.append('Thursday')

integration_tests/global_syms_03_b.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from global_syms_03_a import l_1, l_2
2-
3-
assert len(l_1) == 3
4-
assert l_1[1] == "Tuesday"
5-
assert l_2[1] == -20
1+
# from global_syms_03_a import l_1, l_2
2+
import global_syms_03_a
3+
# assert len(l_1) == 3
4+
# assert l_1[1] == "Tuesday"
5+
# assert l_1[4] == "Thursday"
6+
# assert l_2[1] == -20

src/libasr/asr_scopes.cpp

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -206,47 +206,47 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
206206
es->m_parent_symtab = module_scope;
207207
ASR::symbol_t *s = ASRUtils::symbol_get_past_external(a.second);
208208
LCOMPILERS_ASSERT(s);
209-
if (ASR::is_a<ASR::Variable_t>(*s)) {
210-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
211-
if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
212-
&& ASR::is_a<ASR::List_t>(*v->m_type)) {
213-
ASR::expr_t* target = ASRUtils::EXPR(ASR::make_Var_t(
214-
al, v->base.base.loc, (ASR::symbol_t *) es));
215-
ASR::expr_t *value = v->m_symbolic_value;
216-
v->m_symbolic_value = nullptr;
217-
v->m_value = nullptr;
218-
if (ASR::is_a<ASR::FunctionCall_t>(*value)) {
219-
ASR::FunctionCall_t *call =
220-
ASR::down_cast<ASR::FunctionCall_t>(value);
221-
ASR::Module_t *m = ASRUtils::get_sym_module(s);
222-
ASR::symbol_t *func = m->m_symtab->get_symbol(
223-
ASRUtils::symbol_name(call->m_name));
224-
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(func);
225-
std::string func_name = std::string(m->m_name) +
226-
"@" + f->m_name;
227-
ASR::symbol_t *es_func;
228-
if (!module_scope->get_symbol(func_name)) {
229-
es_func = ASR::down_cast<ASR::symbol_t>(
230-
ASR::make_ExternalSymbol_t(al, f->base.base.loc,
231-
module_scope, s2c(al, func_name), func, m->m_name,
232-
nullptr, 0, s2c(al, f->m_name), ASR::accessType::Public));
233-
module_scope->add_symbol(func_name, es_func);
234-
if (!present(func_dependencies, s2c(al, func_name))) {
235-
func_dependencies.push_back(al, s2c(al,func_name));
236-
}
237-
} else {
238-
es_func = module_scope->get_symbol(func_name);
239-
}
240-
value = ASRUtils::EXPR(ASR::make_FunctionCall_t(al,
241-
call->base.base.loc, es_func, call->m_original_name,
242-
call->m_args, call->n_args, call->m_type,
243-
call->m_value, call->m_dt));
244-
}
245-
ASR::asr_t* assign = ASR::make_Assignment_t(al,
246-
v->base.base.loc, target, value, nullptr);
247-
var_init.push_back(al, ASRUtils::STMT(assign));
248-
}
249-
}
209+
// if (ASR::is_a<ASR::Variable_t>(*s)) {
210+
// ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
211+
// if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
212+
// && ASR::is_a<ASR::List_t>(*v->m_type)) {
213+
// ASR::expr_t* target = ASRUtils::EXPR(ASR::make_Var_t(
214+
// al, v->base.base.loc, (ASR::symbol_t *) es));
215+
// ASR::expr_t *value = v->m_symbolic_value;
216+
// v->m_symbolic_value = nullptr;
217+
// v->m_value = nullptr;
218+
// if (ASR::is_a<ASR::FunctionCall_t>(*value)) {
219+
// ASR::FunctionCall_t *call =
220+
// ASR::down_cast<ASR::FunctionCall_t>(value);
221+
// ASR::Module_t *m = ASRUtils::get_sym_module(s);
222+
// ASR::symbol_t *func = m->m_symtab->get_symbol(
223+
// ASRUtils::symbol_name(call->m_name));
224+
// ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(func);
225+
// std::string func_name = std::string(m->m_name) +
226+
// "@" + f->m_name;
227+
// ASR::symbol_t *es_func;
228+
// if (!module_scope->get_symbol(func_name)) {
229+
// es_func = ASR::down_cast<ASR::symbol_t>(
230+
// ASR::make_ExternalSymbol_t(al, f->base.base.loc,
231+
// module_scope, s2c(al, func_name), func, m->m_name,
232+
// nullptr, 0, s2c(al, f->m_name), ASR::accessType::Public));
233+
// module_scope->add_symbol(func_name, es_func);
234+
// if (!present(func_dependencies, s2c(al, func_name))) {
235+
// func_dependencies.push_back(al, s2c(al,func_name));
236+
// }
237+
// } else {
238+
// es_func = module_scope->get_symbol(func_name);
239+
// }
240+
// value = ASRUtils::EXPR(ASR::make_FunctionCall_t(al,
241+
// call->base.base.loc, es_func, call->m_original_name,
242+
// call->m_args, call->n_args, call->m_type,
243+
// call->m_value, call->m_dt));
244+
// }
245+
// ASR::asr_t* assign = ASR::make_Assignment_t(al,
246+
// v->base.base.loc, target, value, nullptr);
247+
// var_init.push_back(al, ASRUtils::STMT(assign));
248+
// }
249+
// }
250250
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
251251
syms.push_back(al, s2c(al, a.first));
252252
break;
@@ -273,22 +273,22 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
273273
v->m_parent_symtab = module_scope;
274274
// Make the Assignment statement only for the data-types (List,
275275
// Dict, ...), that cannot be handled in the LLVM global scope
276-
if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
277-
&& ASR::is_a<ASR::List_t>(*v->m_type)) {
278-
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(
279-
al, v->base.base.loc, (ASR::symbol_t *) v));
280-
ASR::asr_t* assign = ASR::make_Assignment_t(al,
281-
v->base.base.loc, v_expr, v->m_symbolic_value, nullptr);
282-
var_init.push_back(al, ASRUtils::STMT(assign));
283-
v->m_symbolic_value = nullptr;
284-
v->m_value = nullptr;
285-
Vec<char*> v_dependencies;
286-
v_dependencies.reserve(al, 1);
287-
ASRUtils::collect_variable_dependencies(al,
288-
v_dependencies, v->m_type);
289-
v->m_dependencies = v_dependencies.p;
290-
v->n_dependencies = v_dependencies.size();
291-
}
276+
// if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
277+
// && ASR::is_a<ASR::List_t>(*v->m_type)) {
278+
// ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(
279+
// al, v->base.base.loc, (ASR::symbol_t *) v));
280+
// ASR::asr_t* assign = ASR::make_Assignment_t(al,
281+
// v->base.base.loc, v_expr, v->m_symbolic_value, nullptr);
282+
// var_init.push_back(al, ASRUtils::STMT(assign));
283+
// v->m_symbolic_value = nullptr;
284+
// v->m_value = nullptr;
285+
// Vec<char*> v_dependencies;
286+
// v_dependencies.reserve(al, 1);
287+
// ASRUtils::collect_variable_dependencies(al,
288+
// v_dependencies, v->m_type);
289+
// v->m_dependencies = v_dependencies.p;
290+
// v->n_dependencies = v_dependencies.size();
291+
// }
292292
module_scope->add_symbol(a.first, (ASR::symbol_t *) v);
293293
syms.push_back(al, s2c(al, a.first));
294294
break;

src/libasr/asr_utils.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <libasr/utils.h>
99
#include <libasr/modfile.h>
1010
#include <libasr/pass/pass_utils.h>
11+
#include <libasr/pass/global_stmts.h>
12+
#include <libasr/pass/global_symbols.h>
1113

1214
namespace LCompilers {
1315

@@ -111,20 +113,38 @@ std::vector<std::string> determine_variable_declaration_order(
111113
return ASRUtils::order_deps(var_dep_graph);
112114
}
113115

114-
void extract_module_python(const ASR::TranslationUnit_t &m,
115-
std::vector<std::pair<std::string, ASR::Module_t*>>& children_modules,
116-
std::string module_name) {
116+
void extract_module_python(Allocator &al, ASR::TranslationUnit_t &m,
117+
std::vector<std::pair<std::string, ASR::symbol_t*>>& children_modules,
118+
std::string module_name, bool import_stmts) {
117119
bool module_found = false;
120+
if (m.n_items > 0 && import_stmts) {
121+
// Wrap all the global statements into a Function: `_global_statements`
122+
LCompilers::PassOptions pass_options;
123+
pass_options.run_fun = "_global_statements";
124+
pass_wrap_global_stmts_into_function(al, m, pass_options);
125+
}
118126
for (auto &a : m.m_global_scope->get_scope()) {
119127
if( ASR::is_a<ASR::Module_t>(*a.second) ) {
120128
if( a.first == "__main__" ) {
121129
module_found = true;
130+
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(a.second);
131+
ASR::symbol_t *f_sym = m.m_global_scope->get_symbol(
132+
"_global_statements");
133+
if (f_sym && import_stmts) {
134+
// Add the `_global_statements` function into the `__main__`
135+
// module and later used by `import x`
136+
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
137+
f->m_symtab->parent = mod->m_symtab;
138+
mod->m_symtab->add_symbol("_global_statements",
139+
(ASR::symbol_t *) f);
140+
}
122141
children_modules.push_back(std::make_pair(module_name,
123-
ASR::down_cast<ASR::Module_t>(a.second)));
142+
(ASR::symbol_t*) mod));
124143
} else {
125-
children_modules.push_back(std::make_pair(a.first,
126-
ASR::down_cast<ASR::Module_t>(a.second)));
144+
children_modules.push_back(std::make_pair(a.first, a.second));
127145
}
146+
} else if ( ASR::is_a<ASR::ExternalSymbol_t>(*a.second) ) {
147+
children_modules.push_back(std::make_pair(a.first, a.second));
128148
}
129149
}
130150
if( !module_found ) {

src/libasr/asr_utils.h

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,9 @@ std::vector<std::string> determine_function_definition_order(
12831283
std::vector<std::string> determine_variable_declaration_order(
12841284
SymbolTable* symtab);
12851285

1286-
void extract_module_python(const ASR::TranslationUnit_t &m,
1287-
std::vector<std::pair<std::string, ASR::Module_t*>>& children_modules,
1288-
std::string module_name);
1286+
void extract_module_python(Allocator &al, ASR::TranslationUnit_t &m,
1287+
std::vector<std::pair<std::string, ASR::symbol_t*>>& children_modules,
1288+
std::string module_name, bool import_stmts);
12891289

12901290
ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m);
12911291

@@ -2572,6 +2572,49 @@ class ReplaceArgVisitor: public ASR::BaseExprReplacer<ReplaceArgVisitor> {
25722572

25732573
};
25742574

2575+
class VarExprReplacer: public ASR::BaseExprReplacer<VarExprReplacer>
2576+
{
2577+
private:
2578+
Allocator &al;
2579+
SymbolTable *current_scope;
2580+
2581+
public:
2582+
VarExprReplacer(Allocator& al_, SymbolTable *current_scope_) :
2583+
al(al_), current_scope(current_scope_)
2584+
{}
2585+
2586+
void replace_Var(ASR::Var_t *x) {
2587+
std::string sym_name = ASRUtils::symbol_name(x->m_v);
2588+
ASR::symbol_t *s = current_scope->resolve_symbol(sym_name);
2589+
// LCOMPILERS_ASSERT(s != nullptr)
2590+
if (s) {
2591+
*current_expr = ASRUtils::EXPR(ASR::make_Var_t(al,
2592+
x->base.base.loc, s));
2593+
}
2594+
}
2595+
};
2596+
2597+
class VarExprVisitor : public ASR::CallReplacerOnExpressionsVisitor
2598+
<VarExprVisitor>
2599+
{
2600+
private:
2601+
2602+
VarExprReplacer replacer;
2603+
2604+
public:
2605+
2606+
VarExprVisitor(Allocator& al_, SymbolTable *current_scope) :
2607+
replacer(al_, current_scope)
2608+
{ }
2609+
2610+
void call_replacer() {
2611+
replacer.current_expr = current_expr;
2612+
replacer.replace_expr(*current_expr);
2613+
}
2614+
2615+
};
2616+
2617+
25752618
class ExprStmtDuplicator: public ASR::BaseExprStmtDuplicator<ExprStmtDuplicator>
25762619
{
25772620
public:

src/libasr/asr_verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
638638
i ++;
639639
}
640640
}
641-
require(symtab_in_scope(current_symtab, x.m_v) || var_present_in_enum,
641+
require((symtab_in_scope(current_symtab, x.m_v) || current_symtab->parent == nullptr) || var_present_in_enum ,
642642
"Var::m_v `" + x_mv_name + "` cannot point outside of its symbol table");
643643
variable_dependencies.push_back(x_mv_name);
644644
}

src/libasr/pass/global_symbols.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ void pass_wrap_global_syms_into_module(Allocator &al,
5454
ASR::symbol_t *module = (ASR::symbol_t *) ASR::make_Module_t(al, loc,
5555
module_scope, module_name, mod_dependencies.p, mod_dependencies.n,
5656
false, false);
57+
// Replace the Var_t with correct symbol_table
58+
ASRUtils::VarExprVisitor replace_sym(al, module_scope);
59+
replace_sym.visit_Module(*ASR::down_cast<ASR::Module_t>(module));
5760
unit.m_global_scope->add_symbol(module_name, module);
5861
}
5962

0 commit comments

Comments
 (0)