Skip to content

Commit 264fba3

Browse files
committed
wip
1 parent 800e186 commit 264fba3

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

integration_tests/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ macro(RUN)
6161
if (import_path)
6262
add_custom_command(
6363
OUTPUT ${name}.o
64-
COMMAND lpython -c -I ${import_path} ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
64+
COMMAND lpython -c -I ${CMAKE_CURRENT_SOURCE_DIR}/${import_path} ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
6565
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
6666
VERBATIM)
6767
else ()
@@ -248,9 +248,10 @@ RUN(NAME test_for_loop LABELS cpython llvm c)
248248
RUN(NAME modules_01 LABELS cpython llvm wasm wasm_x86)
249249
RUN(NAME modules_02 LABELS cpython llvm wasm wasm_x86)
250250
RUN(NAME test_import_01 LABELS cpython llvm)
251-
RUN(NAME test_import_02 IMPORT_PATH integration_tests
252-
LABELS cpython llvm)
251+
RUN(NAME test_import_02 LABELS cpython llvm)
253252
RUN(NAME test_import_03 LABELS cpython llvm)
253+
RUN(NAME test_import_04 IMPORT_PATH ..
254+
LABELS cpython llvm)
254255
RUN(NAME test_math LABELS cpython llvm)
255256
RUN(NAME test_numpy_01 LABELS cpython llvm c)
256257
RUN(NAME test_numpy_02 LABELS cpython llvm c)

integration_tests/test_import_04.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import test_modules
2+
3+
def main0():
4+
print(test_modules.sin(0.5))
5+
print(test_modules.cos(0.5))
6+
assert abs(test_modules.sin(0.5) + test_modules.cos(0.5) - 1.0) <= 1e-12
7+
8+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,17 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
421421
}
422422

423423
ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
424-
std::string mname, std::string cur_sym_name, std::string new_sym_name,
424+
std::string mname, std::string cur_sym_name, std::string& new_sym_name,
425425
const Location &loc, bool skip_current_scope_check=false) {
426-
// std::cout<<"module_name: "<<m->m_name<<std::endl;
426+
// std::cout<<"module_name: "<<m->m_name<<" "<<cur_sym_name<<" "<<new_sym_name<<std::endl;
427+
new_sym_name = ASRUtils::get_mangled_name(m, new_sym_name);
427428
// for( auto& itr: m->m_symtab->get_scope() ) {
428429
// std::cout<<"itr.first: "<<itr.first<<std::endl;
429430
// }
430431
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
432+
// std::cout<<"t: "<<t<<std::endl;
431433
if (!t) {
432-
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
434+
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + std::string(m->m_name) + "'",
433435
loc);
434436
}
435437
if (!skip_current_scope_check &&
@@ -487,6 +489,8 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
487489
SymbolTable *symtab = current_scope;
488490
// while (symtab->parent != nullptr) symtab = symtab->parent;
489491
ASR::symbol_t *sym = symtab->resolve_symbol(es->m_module_name);
492+
// std::cout<<"sym: "<<sym<<std::endl;
493+
// std::cout<<"sym.type: "<<ASRUtils::symbol_get_past_external(sym)->type<<" "<<es->m_module_name<<" "<<es->m_name<<std::endl;
490494
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
491495
return import_from_module(al, m, symtab, es->m_module_name,
492496
cur_sym_name, new_sym_name, loc);
@@ -1020,6 +1024,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
10201024
std::string mod_name = ASR::down_cast<ASR::ExternalSymbol_t>(stemp)->m_module_name;
10211025
ASR::symbol_t *mt = symtab->resolve_symbol(mod_name);
10221026
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(mt);
1027+
local_sym = ASRUtils::get_mangled_name(m, local_sym);
10231028
stemp = import_from_module(al, m, symtab, mod_name,
10241029
remote_sym, local_sym, loc);
10251030
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*stemp));
@@ -3547,7 +3552,18 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
35473552
throw SemanticError("The module '" + msym + "' cannot be loaded",
35483553
x.base.base.loc);
35493554
}
3550-
current_module_dependencies.push_back(al, s2c(al, msym));
3555+
if( msym == "__init__" ) {
3556+
for( auto item: ASRUtils::symbol_symtab(t)->get_scope() ) {
3557+
if( ASR::is_a<ASR::ExternalSymbol_t>(*item.second) &&
3558+
!present(current_module_dependencies,
3559+
ASR::down_cast<ASR::ExternalSymbol_t>(item.second)->m_module_name) ) {
3560+
current_module_dependencies.push_back(al,
3561+
ASR::down_cast<ASR::ExternalSymbol_t>(item.second)->m_module_name);
3562+
}
3563+
}
3564+
} else {
3565+
current_module_dependencies.push_back(al, s2c(al, msym));
3566+
}
35513567
}
35523568

35533569
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
@@ -3557,7 +3573,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
35573573
}
35583574
std::string new_sym_name = ASRUtils::get_mangled_name(m, remote_sym);
35593575
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
3560-
remote_sym, new_sym_name, x.base.base.loc, false);
3576+
remote_sym, new_sym_name, x.base.base.loc, true);
35613577
current_scope->add_symbol(new_sym_name, t);
35623578
}
35633579

@@ -4636,7 +4652,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
46364652
ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name);
46374653
if (!sym) {
46384654
sym = import_from_module(al, m, current_scope, value,
4639-
x.m_attr, sym_name, x.base.base.loc, true);
4655+
x.m_attr, sym_name, x.base.base.loc, false);
46404656
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*sym));
46414657
current_scope->add_symbol(sym_name, sym);
46424658
}

test_modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .mathfn import sin, cos

test_modules/mathfn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .sin import sin, cos

test_modules/mathfn/sin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ltypes import f64
2+
3+
def sin(x: f64) -> f64:
4+
return x + 1.0
5+
6+
def cos(x: f64) -> f64:
7+
return x - 1.0

0 commit comments

Comments
 (0)