Skip to content

Commit f8f58d0

Browse files
authored
Merge pull request #2161 from Shaikh-Ubaid/fix_scoping2
Fix scoping issue
2 parents a14d2c8 + a43632e commit f8f58d0

File tree

266 files changed

+6541
-6775
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+6541
-6775
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ RUN(NAME expr_16 LABELS cpython c)
423423
RUN(NAME expr_17 LABELS cpython llvm c)
424424
RUN(NAME expr_18 FAIL LABELS cpython llvm c)
425425
RUN(NAME expr_19 LABELS cpython llvm c)
426+
RUN(NAME expr_20 LABELS cpython llvm c)
426427

427428
RUN(NAME expr_01u LABELS cpython llvm c NOFAST)
428429
RUN(NAME expr_02u LABELS cpython llvm c NOFAST)

integration_tests/array_expr_02.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from lpython import i32, f32, TypeVar
22
from numpy import empty, sqrt, float32
33

4-
n: i32
54
n = TypeVar("n")
65

76
def modify(array_a: f32[:], n: i32) -> f32[n]:

integration_tests/expr_20.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from lpython import i16, i32
2+
3+
def f():
4+
i: i32 = 5
5+
print(i16(i % 1023))
6+
7+
def u16(x: i16) -> i32:
8+
if x >= i16(0):
9+
return i32(x)
10+
else:
11+
return i32(x) + 65536
12+
13+
f()
14+
print(u16(i16(10)), u16(i16(-10)))
15+
assert(u16(i16(10)) == 10)
16+
assert(u16(i16(-10)) == 65526)

integration_tests/generics_array_02.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from lpython import TypeVar, restriction, i32, f32
22
from numpy import empty
33

4-
n: i32
54
n = TypeVar("n")
65
T = TypeVar('T')
76

integration_tests/generics_array_03.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from lpython import TypeVar, restriction, i32, f32
22
from numpy import empty
33

4-
n: i32
54
n = TypeVar("n")
6-
m: i32
75
m = TypeVar("m")
86
T = TypeVar('T')
97

integration_tests/test_numpy_02.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
eps: f64
1313
eps = 1e-12
1414

15-
n: i32
1615
n = TypeVar("n")
1716

1817
def zeros(n: i32) -> f64[n]:
@@ -126,7 +125,6 @@ def fabs(f: f64) -> f64:
126125
def fabs(b: bool) -> f64:
127126
return sqrt(b)
128127

129-
num: i32
130128
num = TypeVar("num")
131129
def linspace(start: f64, stop: f64, num: i32) -> f64[num]:
132130
A: f64[num]

src/bin/lpython.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int emit_asr(const std::string &infile,
231231
diagnostics.diagnostics.clear();
232232
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
233233
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics,
234-
compiler_options, true, "", infile);
234+
compiler_options, true, "__main__", infile);
235235
std::cerr << diagnostics.render(lm, compiler_options);
236236
if (!r.ok) {
237237
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -291,7 +291,7 @@ int emit_cpp(const std::string &infile,
291291

292292
diagnostics.diagnostics.clear();
293293
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
294-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
294+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
295295
std::cerr << diagnostics.render(lm, compiler_options);
296296
if (!r1.ok) {
297297
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -336,7 +336,7 @@ int emit_c(const std::string &infile,
336336

337337
diagnostics.diagnostics.clear();
338338
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
339-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
339+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
340340
std::cerr << diagnostics.render(lm, compiler_options);
341341
if (!r1.ok) {
342342
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -393,7 +393,7 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
393393

394394
diagnostics.diagnostics.clear();
395395
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
396-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
396+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
397397
std::cerr << diagnostics.render(lm, compiler_options);
398398
if (!r1.ok) {
399399
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -453,7 +453,7 @@ int emit_wat(const std::string &infile,
453453

454454
diagnostics.diagnostics.clear();
455455
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
456-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
456+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
457457
std::cerr << diagnostics.render(lm, compiler_options);
458458
if (!r1.ok) {
459459
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -502,7 +502,7 @@ int get_symbols (const std::string &infile,
502502
if (r1.ok) {
503503
LCompilers::LPython::AST::ast_t* ast = r1.result;
504504
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
505-
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
505+
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
506506
if (!x.ok) {
507507
std::cout << "{}\n";
508508
return 0;
@@ -603,7 +603,7 @@ int get_errors (const std::string &infile,
603603
if (r1.ok) {
604604
LCompilers::LPython::AST::ast_t* ast = r1.result;
605605
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
606-
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
606+
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
607607
}
608608
std::vector<LCompilers::error_highlight> diag_lists;
609609
LCompilers::error_highlight h;
@@ -723,7 +723,7 @@ int emit_llvm(const std::string &infile,
723723
LCompilers::LPython::AST::ast_t* ast = r.result;
724724
diagnostics.diagnostics.clear();
725725
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
726-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
726+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
727727
std::cerr << diagnostics.render(lm, compiler_options);
728728
if (!r1.ok) {
729729
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -799,7 +799,7 @@ int compile_python_to_object_file(
799799
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
800800
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
801801
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
802-
!(arg_c && compiler_options.disable_main), "", infile);
802+
!(arg_c && compiler_options.disable_main), "__main__", infile);
803803

804804
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
805805
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
@@ -916,7 +916,7 @@ int compile_to_binary_wasm(
916916
diagnostics.diagnostics.clear();
917917
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
918918
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
919-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
919+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
920920
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
921921
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
922922
std::cerr << diagnostics.render(lm, compiler_options);
@@ -989,7 +989,7 @@ int compile_to_binary_x86(
989989
diagnostics.diagnostics.clear();
990990
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
991991
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
992-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
992+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
993993
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
994994
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
995995
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1063,7 +1063,7 @@ int compile_to_binary_wasm_to_x86(
10631063
diagnostics.diagnostics.clear();
10641064
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
10651065
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1066-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
1066+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
10671067
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
10681068
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
10691069
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1359,7 +1359,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
13591359
if (ast.ok) {
13601360
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13611361
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1362-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1362+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
13631363
out = diagnostics.render(lm, compiler_options);
13641364
if (asr.ok) {
13651365
out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
@@ -1377,7 +1377,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
13771377
if (ast.ok) {
13781378
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13791379
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1380-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1380+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
13811381
out = diagnostics.render(lm, compiler_options);
13821382
if (asr.ok) {
13831383
LCompilers::Result<LCompilers::Vec<uint8_t>>
@@ -1404,7 +1404,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
14041404
if (ast.ok) {
14051405
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
14061406
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1407-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1407+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
14081408
out = diagnostics.render(lm, compiler_options);
14091409
if (asr.ok) {
14101410
auto res = LCompilers::asr_to_cpp(al, *asr.result, diagnostics,
@@ -1442,7 +1442,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
14421442
if (ast.ok) {
14431443
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
14441444
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1445-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1445+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
14461446
out = diagnostics.render(lm, compiler_options);
14471447
if (asr.ok) {
14481448
LCompilers::Result<LCompilers::Vec<uint8_t>>

src/libasr/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ set(SRC
3333
pass/do_loops.cpp
3434
pass/for_all.cpp
3535
pass/global_stmts.cpp
36-
pass/global_stmts_program.cpp
37-
pass/global_symbols.cpp
3836
pass/select_case.cpp
3937
pass/init_expr.cpp
4038
pass/implied_do_loops.cpp

src/libasr/asr_scopes.cpp

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -96,102 +96,4 @@ std::string SymbolTable::get_unique_name(const std::string &name, bool use_uniqu
9696
return unique_name;
9797
}
9898

99-
void SymbolTable::move_symbols_from_global_scope(Allocator &al,
100-
SymbolTable *module_scope, Vec<char *> &syms,
101-
SetChar &mod_dependencies) {
102-
// TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
103-
syms.reserve(al, 4);
104-
mod_dependencies.reserve(al, 4);
105-
for (auto &a : scope) {
106-
switch (a.second->type) {
107-
case (ASR::symbolType::Module): {
108-
// Pass
109-
break;
110-
} case (ASR::symbolType::Function) : {
111-
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(a.second);
112-
for (size_t i = 0; i < fn->n_dependencies; i++ ) {
113-
ASR::symbol_t *s = fn->m_symtab->get_symbol(
114-
fn->m_dependencies[i]);
115-
if (s == nullptr) {
116-
std::string block_name = "block";
117-
ASR::symbol_t *block_s = fn->m_symtab->get_symbol(block_name);
118-
int32_t j = 1;
119-
while(block_s != nullptr) {
120-
while(block_s != nullptr) {
121-
ASR::Block_t *b = ASR::down_cast<ASR::Block_t>(block_s);
122-
s = b->m_symtab->get_symbol(fn->m_dependencies[i]);
123-
if (s == nullptr) {
124-
block_s = b->m_symtab->get_symbol("block");
125-
} else {
126-
break;
127-
}
128-
}
129-
if (s == nullptr) {
130-
block_s = fn->m_symtab->get_symbol(block_name +
131-
std::to_string(j));
132-
j++;
133-
} else {
134-
break;
135-
}
136-
}
137-
}
138-
if (s == nullptr) {
139-
s = fn->m_symtab->parent->get_symbol(fn->m_dependencies[i]);
140-
}
141-
if (s != nullptr && ASR::is_a<ASR::ExternalSymbol_t>(*s)) {
142-
char *es_name = ASR::down_cast<
143-
ASR::ExternalSymbol_t>(s)->m_module_name;
144-
mod_dependencies.push_back(al, es_name);
145-
}
146-
}
147-
fn->m_symtab->parent = module_scope;
148-
module_scope->add_symbol(a.first, (ASR::symbol_t *) fn);
149-
syms.push_back(al, s2c(al, a.first));
150-
break;
151-
} case (ASR::symbolType::GenericProcedure) : {
152-
ASR::GenericProcedure_t *es = ASR::down_cast<ASR::GenericProcedure_t>(a.second);
153-
es->m_parent_symtab = module_scope;
154-
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
155-
syms.push_back(al, s2c(al, a.first));
156-
break;
157-
} case (ASR::symbolType::ExternalSymbol) : {
158-
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(a.second);
159-
mod_dependencies.push_back(al, es->m_module_name);
160-
es->m_parent_symtab = module_scope;
161-
LCOMPILERS_ASSERT(ASRUtils::symbol_get_past_external(a.second));
162-
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
163-
syms.push_back(al, s2c(al, a.first));
164-
break;
165-
} case (ASR::symbolType::StructType) : {
166-
ASR::StructType_t *st = ASR::down_cast<ASR::StructType_t>(a.second);
167-
st->m_symtab->parent = module_scope;
168-
module_scope->add_symbol(a.first, (ASR::symbol_t *) st);
169-
syms.push_back(al, s2c(al, a.first));
170-
break;
171-
} case (ASR::symbolType::EnumType) : {
172-
ASR::EnumType_t *et = ASR::down_cast<ASR::EnumType_t>(a.second);
173-
et->m_symtab->parent = module_scope;
174-
module_scope->add_symbol(a.first, (ASR::symbol_t *) et);
175-
syms.push_back(al, s2c(al, a.first));
176-
break;
177-
} case (ASR::symbolType::UnionType) : {
178-
ASR::UnionType_t *ut = ASR::down_cast<ASR::UnionType_t>(a.second);
179-
ut->m_symtab->parent = module_scope;
180-
module_scope->add_symbol(a.first, (ASR::symbol_t *) ut);
181-
syms.push_back(al, s2c(al, a.first));
182-
break;
183-
} case (ASR::symbolType::Variable) : {
184-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
185-
v->m_parent_symtab = module_scope;
186-
module_scope->add_symbol(a.first, (ASR::symbol_t *) v);
187-
syms.push_back(al, s2c(al, a.first));
188-
break;
189-
} default : {
190-
throw LCompilersException("Moving the symbol:`" + a.first +
191-
"` from global scope is not implemented yet");
192-
}
193-
}
194-
}
195-
}
196-
19799
} // namespace LCompilers

src/libasr/asr_scopes.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ struct SymbolTable {
9696
size_t n_scope_names, char **m_scope_names);
9797

9898
std::string get_unique_name(const std::string &name, bool use_unique_id=true);
99-
100-
void move_symbols_from_global_scope(Allocator &al,
101-
SymbolTable *module_scope, Vec<char *> &syms,
102-
SetChar &mod_dependencies);
10399
};
104100

105101
} // namespace LCompilers

src/libasr/gen_pass.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"replace_fma",
1010
"replace_for_all",
1111
"wrap_global_stmts",
12-
"wrap_global_stmts_program",
13-
"wrap_global_symbols",
1412
"replace_implied_do_loops",
1513
"replace_init_expr",
1614
"inline_function_calls",

0 commit comments

Comments
 (0)