Skip to content

Commit

Permalink
Merge pull request lcompilers#1378 from Smit-create/vars_fix
Browse files Browse the repository at this point in the history
Order of declaration: Only include symbols if they are in scope
  • Loading branch information
certik committed Jan 6, 2023
2 parents 9066fce + 2ef019a commit 3f2c9f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ RUN(NAME test_cmath LABELS cpython llvm c)
RUN(NAME test_complex LABELS cpython llvm c)
RUN(NAME test_max_min LABELS cpython llvm c)
RUN(NAME test_global LABELS cpython llvm)
RUN(NAME test_global_decl LABELS cpython llvm c)
RUN(NAME test_integer_bitnot LABELS cpython llvm c)
RUN(NAME test_ifexp LABELS cpython llvm c)
RUN(NAME test_unary_minus LABELS cpython llvm c)
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/test_global_decl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ltypes import i32
from numpy import empty, int32

# issue-1368

SIZE: i32 = i32(3)

def main() -> None:
xs: i32[SIZE] = empty(SIZE, dtype=int32)
i: i32
for i in range(SIZE):
xs[i] = i+1

for i in range(SIZE):
assert xs[i] == i+1

main()
14 changes: 12 additions & 2 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ std::vector<std::string> determine_function_definition_order(
ASR::Function_t* func = ASR::down_cast<ASR::Function_t>(itr.second);
for( size_t i = 0; i < func->n_dependencies; i++ ) {
std::string dep = func->m_dependencies[i];
deps.push_back(dep);
// Check if the dependent variable is present in the symtab.
// This will help us to include only local dependencies, and we
// assume that dependencies in the parent symtab are already declared
// earlier.
if (symtab->get_symbol(dep) != nullptr)
deps.push_back(dep);
}
func_dep_graph[itr.first] = deps;
}
Expand All @@ -91,7 +96,12 @@ std::vector<std::string> determine_variable_declaration_order(
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(itr.second);
for( size_t i = 0; i < var->n_dependencies; i++ ) {
std::string dep = var->m_dependencies[i];
deps.push_back(dep);
// Check if the dependent variable is present in the symtab.
// This will help us to include only local dependencies, and we
// assume that dependencies in the parent symtab are already declared
// earlier.
if (symtab->get_symbol(dep) != nullptr)
deps.push_back(dep);
}
var_dep_graph[itr.first] = deps;
}
Expand Down

0 comments on commit 3f2c9f3

Please sign in to comment.