Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix scoping issue #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ RUN(NAME expr_16 LABELS cpython c)
RUN(NAME expr_17 LABELS cpython llvm c)
RUN(NAME expr_18 FAIL LABELS cpython llvm c)
RUN(NAME expr_19 LABELS cpython llvm c)
RUN(NAME expr_20 LABELS cpython llvm c)

RUN(NAME expr_01u LABELS cpython llvm c NOFAST)
RUN(NAME expr_02u LABELS cpython llvm c NOFAST)
Expand Down
1 change: 0 additions & 1 deletion integration_tests/array_expr_02.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from lpython import i32, f32, TypeVar
from numpy import empty, sqrt, float32

n: i32
n = TypeVar("n")

def modify(array_a: f32[:], n: i32) -> f32[n]:
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/expr_20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from lpython import i16, i32

def f():
i: i32 = 5
print(i16(i % 1023))

def u16(x: i16) -> i32:
if x >= i16(0):
return i32(x)
else:
return i32(x) + 65536

f()
print(u16(i16(10)), u16(i16(-10)))
assert(u16(i16(10)) == 10)
assert(u16(i16(-10)) == 65526)
1 change: 0 additions & 1 deletion integration_tests/generics_array_02.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from lpython import TypeVar, restriction, i32, f32
from numpy import empty

n: i32
n = TypeVar("n")
T = TypeVar('T')

Expand Down
2 changes: 0 additions & 2 deletions integration_tests/generics_array_03.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from lpython import TypeVar, restriction, i32, f32
from numpy import empty

n: i32
n = TypeVar("n")
m: i32
m = TypeVar("m")
T = TypeVar('T')

Expand Down
2 changes: 0 additions & 2 deletions integration_tests/test_numpy_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
eps: f64
eps = 1e-12

n: i32
n = TypeVar("n")

def zeros(n: i32) -> f64[n]:
Expand Down Expand Up @@ -126,7 +125,6 @@ def fabs(f: f64) -> f64:
def fabs(b: bool) -> f64:
return sqrt(b)

num: i32
num = TypeVar("num")
def linspace(start: f64, stop: f64, num: i32) -> f64[num]:
A: f64[num]
Expand Down
32 changes: 16 additions & 16 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ int emit_asr(const std::string &infile,
diagnostics.diagnostics.clear();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics,
compiler_options, true, "", infile);
compiler_options, true, "__main__", infile);
std::cerr << diagnostics.render(lm, compiler_options);
if (!r.ok) {
LCOMPILERS_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -291,7 +291,7 @@ int emit_cpp(const std::string &infile,

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

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

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

diagnostics.diagnostics.clear();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
std::cerr << diagnostics.render(lm, compiler_options);
if (!r1.ok) {
LCOMPILERS_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -502,7 +502,7 @@ int get_symbols (const std::string &infile,
if (r1.ok) {
LCompilers::LPython::AST::ast_t* ast = r1.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
if (!x.ok) {
std::cout << "{}\n";
return 0;
Expand Down Expand Up @@ -603,7 +603,7 @@ int get_errors (const std::string &infile,
if (r1.ok) {
LCompilers::LPython::AST::ast_t* ast = r1.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
}
std::vector<LCompilers::error_highlight> diag_lists;
LCompilers::error_highlight h;
Expand Down Expand Up @@ -723,7 +723,7 @@ int emit_llvm(const std::string &infile,
LCompilers::LPython::AST::ast_t* ast = r.result;
diagnostics.diagnostics.clear();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
std::cerr << diagnostics.render(lm, compiler_options);
if (!r1.ok) {
LCOMPILERS_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -799,7 +799,7 @@ int compile_python_to_object_file(
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
!(arg_c && compiler_options.disable_main), "", infile);
!(arg_c && compiler_options.disable_main), "__main__", infile);

auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
Expand Down Expand Up @@ -916,7 +916,7 @@ int compile_to_binary_wasm(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(lm, compiler_options);
Expand Down Expand Up @@ -989,7 +989,7 @@ int compile_to_binary_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(lm, compiler_options);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ int compile_to_binary_wasm_to_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(lm, compiler_options);
Expand Down Expand Up @@ -1359,7 +1359,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
out = diagnostics.render(lm, compiler_options);
if (asr.ok) {
out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
Expand All @@ -1377,7 +1377,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
out = diagnostics.render(lm, compiler_options);
if (asr.ok) {
LCompilers::Result<LCompilers::Vec<uint8_t>>
Expand All @@ -1404,7 +1404,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
out = diagnostics.render(lm, compiler_options);
if (asr.ok) {
auto res = LCompilers::asr_to_cpp(al, *asr.result, diagnostics,
Expand Down Expand Up @@ -1442,7 +1442,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
out = diagnostics.render(lm, compiler_options);
if (asr.ok) {
LCompilers::Result<LCompilers::Vec<uint8_t>>
Expand Down
2 changes: 0 additions & 2 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ set(SRC
pass/do_loops.cpp
pass/for_all.cpp
pass/global_stmts.cpp
pass/global_stmts_program.cpp
pass/global_symbols.cpp
pass/select_case.cpp
pass/init_expr.cpp
pass/implied_do_loops.cpp
Expand Down
98 changes: 0 additions & 98 deletions src/libasr/asr_scopes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,102 +96,4 @@ std::string SymbolTable::get_unique_name(const std::string &name, bool use_uniqu
return unique_name;
}

void SymbolTable::move_symbols_from_global_scope(Allocator &al,
SymbolTable *module_scope, Vec<char *> &syms,
SetChar &mod_dependencies) {
// TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
syms.reserve(al, 4);
mod_dependencies.reserve(al, 4);
for (auto &a : scope) {
switch (a.second->type) {
case (ASR::symbolType::Module): {
// Pass
break;
} case (ASR::symbolType::Function) : {
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(a.second);
for (size_t i = 0; i < fn->n_dependencies; i++ ) {
ASR::symbol_t *s = fn->m_symtab->get_symbol(
fn->m_dependencies[i]);
if (s == nullptr) {
std::string block_name = "block";
ASR::symbol_t *block_s = fn->m_symtab->get_symbol(block_name);
int32_t j = 1;
while(block_s != nullptr) {
while(block_s != nullptr) {
ASR::Block_t *b = ASR::down_cast<ASR::Block_t>(block_s);
s = b->m_symtab->get_symbol(fn->m_dependencies[i]);
if (s == nullptr) {
block_s = b->m_symtab->get_symbol("block");
} else {
break;
}
}
if (s == nullptr) {
block_s = fn->m_symtab->get_symbol(block_name +
std::to_string(j));
j++;
} else {
break;
}
}
}
if (s == nullptr) {
s = fn->m_symtab->parent->get_symbol(fn->m_dependencies[i]);
}
if (s != nullptr && ASR::is_a<ASR::ExternalSymbol_t>(*s)) {
char *es_name = ASR::down_cast<
ASR::ExternalSymbol_t>(s)->m_module_name;
mod_dependencies.push_back(al, es_name);
}
}
fn->m_symtab->parent = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) fn);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::GenericProcedure) : {
ASR::GenericProcedure_t *es = ASR::down_cast<ASR::GenericProcedure_t>(a.second);
es->m_parent_symtab = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::ExternalSymbol) : {
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(a.second);
mod_dependencies.push_back(al, es->m_module_name);
es->m_parent_symtab = module_scope;
LCOMPILERS_ASSERT(ASRUtils::symbol_get_past_external(a.second));
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::StructType) : {
ASR::StructType_t *st = ASR::down_cast<ASR::StructType_t>(a.second);
st->m_symtab->parent = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) st);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::EnumType) : {
ASR::EnumType_t *et = ASR::down_cast<ASR::EnumType_t>(a.second);
et->m_symtab->parent = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) et);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::UnionType) : {
ASR::UnionType_t *ut = ASR::down_cast<ASR::UnionType_t>(a.second);
ut->m_symtab->parent = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) ut);
syms.push_back(al, s2c(al, a.first));
break;
} case (ASR::symbolType::Variable) : {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
v->m_parent_symtab = module_scope;
module_scope->add_symbol(a.first, (ASR::symbol_t *) v);
syms.push_back(al, s2c(al, a.first));
break;
} default : {
throw LCompilersException("Moving the symbol:`" + a.first +
"` from global scope is not implemented yet");
}
}
}
}

} // namespace LCompilers
4 changes: 0 additions & 4 deletions src/libasr/asr_scopes.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ struct SymbolTable {
size_t n_scope_names, char **m_scope_names);

std::string get_unique_name(const std::string &name, bool use_unique_id=true);

void move_symbols_from_global_scope(Allocator &al,
SymbolTable *module_scope, Vec<char *> &syms,
SetChar &mod_dependencies);
};

} // namespace LCompilers
Expand Down
2 changes: 0 additions & 2 deletions src/libasr/gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"replace_fma",
"replace_for_all",
"wrap_global_stmts",
"wrap_global_stmts_program",
"wrap_global_symbols",
"replace_implied_do_loops",
"replace_init_expr",
"inline_function_calls",
Expand Down
Loading