Skip to content

Commit 63cf4cc

Browse files
Fix: Support 'import module as alias' in semantic analysis
1 parent a9a4698 commit 63cf4cc

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4959,28 +4959,28 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49594959
}
49604960
paths.push_back(rl_path);
49614961
paths.push_back(parent_dir);
4962-
std::vector<std::string> mods;
4962+
49634963
for (size_t i=0; i<x.n_names; i++) {
4964-
mods.push_back(x.m_names[i].m_name);
4965-
}
4966-
for (auto &mod_sym : mods) {
4964+
std::string mod_sym = x.m_names[i].m_name;
4965+
char* alias = x.m_names[i].m_asname;
4966+
49674967
bool lpython, enum_py, copy, sympy;
49684968
set_module_symbol(mod_sym, paths);
4969+
49694970
t = (ASR::symbol_t*)(load_module(al, global_scope,
49704971
mod_sym, x.base.base.loc, diag, lm, false, paths, lpython, enum_py, copy, sympy,
49714972
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); },
49724973
allow_implicit_casting));
4974+
49734975
if (lpython || enum_py || copy || sympy) {
4974-
// TODO: For now we skip lpython import completely. Later on we should note what symbols
4975-
// got imported from it, and give an error message if an annotation is used without
4976-
// importing it.
49774976
tmp = nullptr;
49784977
continue;
49794978
}
49804979
if (!t) {
49814980
throw SemanticError("The module '" + mod_sym + "' cannot be loaded",
49824981
x.base.base.loc);
49834982
}
4983+
49844984
if( mod_sym == "__init__" ) {
49854985
for( auto item: ASRUtils::symbol_symtab(t)->get_scope() ) {
49864986
if( ASR::is_a<ASR::ExternalSymbol_t>(*item.second) ) {
@@ -4991,6 +4991,34 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49914991
} else {
49924992
current_module_dependencies.push_back(al, s2c(al, mod_sym));
49934993
}
4994+
4995+
// --- ALIAS LOGIC (FIXED) ---
4996+
if (alias) {
4997+
std::string alias_str = std::string(alias);
4998+
4999+
if (current_scope->get_symbol(alias_str) == nullptr) {
5000+
5001+
// The Corrected Constructor Call
5002+
ASR::asr_t *ext_sym = ASR::make_ExternalSymbol_t(
5003+
al,
5004+
x.base.base.loc,
5005+
current_scope, // Parent Symbol Table
5006+
s2c(al, alias_str), // Name: "np"
5007+
t, // Symbol: Pointer to numpy module
5008+
s2c(al, mod_sym), // Module Name: "numpy"
5009+
nullptr, // scope_names (dependencies)
5010+
0, // n_scope_names (MUST BE 0, NOT nullptr)
5011+
s2c(al, mod_sym), // original_name: "numpy"
5012+
ASR::accessType::Public // Access
5013+
);
5014+
5015+
current_scope->add_symbol(alias_str, ASR::down_cast<ASR::symbol_t>(ext_sym));
5016+
}
5017+
} else {
5018+
if (current_scope->get_symbol(mod_sym) == nullptr) {
5019+
current_scope->add_symbol(mod_sym, t);
5020+
}
5021+
}
49945022
}
49955023
}
49965024

0 commit comments

Comments
 (0)