Skip to content

Commit e530245

Browse files
Replace Var_t's m_v with the symbol in the current scope
1 parent 2a14ce7 commit e530245

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
147147
} case (ASR::symbolType::Function) : {
148148
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(a.second);
149149

150-
// Replace Var_t's m_v with the symbols in the current scope
150+
// Replace Var_t's m_v with the symbol in the current scope
151151
ASRUtils::VarExprVisitor replace_symbols(al, fn->m_symtab);
152152
replace_symbols.visit_Function(*fn);
153153

src/libasr/asr_utils.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,24 +2103,27 @@ class VarExprReplacer: public ASR::BaseExprReplacer<VarExprReplacer>
21032103

21042104
void replace_Var(ASR::Var_t *x) {
21052105
std::string sym_name = ASRUtils::symbol_name(x->m_v);
2106+
ASR::symbol_t *var_s = current_scope->get_symbol(sym_name);
21062107
// Check for the symbol in the current scope; if available, return
2107-
if (current_scope->get_symbol(sym_name)) {
2108-
return;
2109-
}
2110-
// otherwise, get the symbol from the module scope.
2111-
// Create an ExternalSymbol and add it to the current scope.
2112-
ASR::symbol_t *var_s = current_scope->resolve_symbol(sym_name);
2113-
LCOMPILERS_ASSERT(var_s != nullptr)
2114-
LCOMPILERS_ASSERT(ASR::is_a<ASR::Variable_t>(*var_s))
2115-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_s);
2116-
var_s = (ASR::symbol_t *) ASR::make_ExternalSymbol_t(al,
2117-
v->base.base.loc, current_scope, v->m_name, var_s,
2118-
s2c(al, "_global_symbols"), nullptr, 0, v->m_name,
2119-
ASR::accessType::Public);
2120-
current_scope->add_symbol(v->m_name, var_s);
2121-
// Replace the symbol with an ExternalSymbol
2122-
*current_expr = ASRUtils::EXPR(ASR::make_Var_t(al,
2123-
x->base.base.loc, var_s));
2108+
if (var_s != nullptr) {
2109+
*current_expr = ASRUtils::EXPR(ASR::make_Var_t(al,
2110+
x->base.base.loc, var_s));
2111+
} else {
2112+
// otherwise, get the symbol from the module scope.
2113+
// Create an ExternalSymbol and add it to the current scope.
2114+
var_s = current_scope->resolve_symbol(sym_name);
2115+
LCOMPILERS_ASSERT(var_s != nullptr)
2116+
LCOMPILERS_ASSERT(ASR::is_a<ASR::Variable_t>(*var_s))
2117+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_s);
2118+
var_s = (ASR::symbol_t *) ASR::make_ExternalSymbol_t(al,
2119+
v->base.base.loc, current_scope, v->m_name, var_s,
2120+
s2c(al, "_global_symbols"), nullptr, 0, v->m_name,
2121+
ASR::accessType::Public);
2122+
current_scope->add_symbol(v->m_name, var_s);
2123+
// Replace the symbol with an ExternalSymbol
2124+
*current_expr = ASRUtils::EXPR(ASR::make_Var_t(al,
2125+
x->base.base.loc, var_s));
2126+
}
21242127
}
21252128
};
21262129

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
21972197
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
21982198
// Ignore type variables
21992199
} else if (x.m_type->type == ASR::ttypeType::List) {
2200-
// Handled in ``_lpython_main_program``function_scope
2200+
// Handled in `_lpython_main_program` function_scope
22012201
} else {
22022202
throw CodeGenError("Variable type not supported", x.base.base.loc);
22032203
}
@@ -5373,6 +5373,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
53735373
}
53745374
case ASR::ttypeType::List: {
53755375
if (is_external) {
5376+
std::cout << "List initilize\n";
53765377
uint32_t h = get_hash((ASR::asr_t*)x);
53775378
llvm::Type *type;
53785379
int n_dims = 0, a_kind = 4;

src/libasr/pass/global_stmts.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ void pass_wrap_global_stmts_into_function(Allocator &al,
130130
throw LCompilersException("Function already defined");
131131
}
132132
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
133+
134+
// Replace Var_t's m_v with the symbol in the current scope
135+
ASR::Function_t *fn_ = ASR::down_cast2<ASR::Function_t>(fn);
136+
ASRUtils::VarExprVisitor replace_symbols(al, fn_->m_symtab);
137+
replace_symbols.visit_Function(*fn_);
133138
} else {
134139
// The last item was a statement, create a subroutine (returning
135140
// nothing)
@@ -154,6 +159,11 @@ void pass_wrap_global_stmts_into_function(Allocator &al,
154159
throw LCompilersException("Function already defined");
155160
}
156161
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
162+
163+
// Replace Var_t's m_v with the symbol in the current scope
164+
ASR::Function_t *fn_ = ASR::down_cast2<ASR::Function_t>(fn);
165+
ASRUtils::VarExprVisitor replace_symbols(al, fn_->m_symtab);
166+
replace_symbols.visit_Function(*fn_);
157167
}
158168
unit.m_items = nullptr;
159169
unit.n_items = 0;

0 commit comments

Comments
 (0)