@@ -421,11 +421,12 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
421
421
}
422
422
423
423
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,
425
425
const Location &loc, bool skip_current_scope_check=false ) {
426
+ new_sym_name = ASRUtils::get_mangled_name (m, new_sym_name);
426
427
ASR::symbol_t *t = m->m_symtab ->resolve_symbol (cur_sym_name);
427
428
if (!t) {
428
- throw SemanticError (" The symbol '" + cur_sym_name + " ' not found in the module '" + mname + " '" ,
429
+ throw SemanticError (" The symbol '" + cur_sym_name + " ' not found in the module '" + std::string (m-> m_name ) + " '" ,
429
430
loc);
430
431
}
431
432
if (!skip_current_scope_check &&
@@ -481,11 +482,19 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
481
482
} else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
482
483
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
483
484
SymbolTable *symtab = current_scope;
484
- while (symtab->parent != nullptr ) symtab = symtab->parent ;
485
- ASR::symbol_t *sym = symtab->get_symbol (es->m_module_name );
485
+ // while (symtab->parent != nullptr) symtab = symtab->parent;
486
+ ASR::symbol_t *sym = symtab->resolve_symbol (es->m_module_name );
486
487
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
487
- return import_from_module (al, m, symtab, es->m_name ,
488
+ return import_from_module (al, m, symtab, es->m_module_name ,
488
489
cur_sym_name, new_sym_name, loc);
490
+ } else if (ASR::is_a<ASR::Module_t>(*t)) {
491
+ ASR::Module_t* mt = ASR::down_cast<ASR::Module_t>(t);
492
+ std::string mangled_cur_sym_name = ASRUtils::get_mangled_name (mt, new_sym_name);
493
+ if ( cur_sym_name == mangled_cur_sym_name ) {
494
+ throw SemanticError (" Importing modules isn't supported yet." , loc);
495
+ }
496
+ return import_from_module (al, mt, current_scope, std::string (mt->m_name ),
497
+ cur_sym_name, new_sym_name, loc);
489
498
} else {
490
499
throw SemanticError (" Only Subroutines, Functions, Variables and "
491
500
" ExternalSymbol are currently supported in 'import'" , loc);
@@ -1007,6 +1016,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
1007
1016
std::string mod_name = ASR::down_cast<ASR::ExternalSymbol_t>(stemp)->m_module_name ;
1008
1017
ASR::symbol_t *mt = symtab->resolve_symbol (mod_name);
1009
1018
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(mt);
1019
+ local_sym = ASRUtils::get_mangled_name (m, local_sym);
1010
1020
stemp = import_from_module (al, m, symtab, mod_name,
1011
1021
remote_sym, local_sym, loc);
1012
1022
LFORTRAN_ASSERT (ASR::is_a<ASR::ExternalSymbol_t>(*stemp));
@@ -3534,7 +3544,18 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3534
3544
throw SemanticError (" The module '" + msym + " ' cannot be loaded" ,
3535
3545
x.base .base .loc );
3536
3546
}
3537
- current_module_dependencies.push_back (al, s2c (al, msym));
3547
+ if ( msym == " __init__" ) {
3548
+ for ( auto item: ASRUtils::symbol_symtab (t)->get_scope () ) {
3549
+ if ( ASR::is_a<ASR::ExternalSymbol_t>(*item.second ) &&
3550
+ !present (current_module_dependencies,
3551
+ ASR::down_cast<ASR::ExternalSymbol_t>(item.second )->m_module_name ) ) {
3552
+ current_module_dependencies.push_back (al,
3553
+ ASR::down_cast<ASR::ExternalSymbol_t>(item.second )->m_module_name );
3554
+ }
3555
+ }
3556
+ } else {
3557
+ current_module_dependencies.push_back (al, s2c (al, msym));
3558
+ }
3538
3559
}
3539
3560
3540
3561
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
@@ -3544,7 +3565,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3544
3565
}
3545
3566
std::string new_sym_name = ASRUtils::get_mangled_name (m, remote_sym);
3546
3567
ASR::symbol_t *t = import_from_module (al, m, current_scope, msym,
3547
- remote_sym, new_sym_name, x.base .base .loc , false );
3568
+ remote_sym, new_sym_name, x.base .base .loc , true );
3548
3569
current_scope->add_symbol (new_sym_name, t);
3549
3570
}
3550
3571
@@ -4614,7 +4635,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4614
4635
ASR::symbol_t *sym = current_scope->resolve_symbol (sym_name);
4615
4636
if (!sym) {
4616
4637
sym = import_from_module (al, m, current_scope, value,
4617
- x.m_attr , sym_name, x.base .base .loc , true );
4638
+ x.m_attr , sym_name, x.base .base .loc , false );
4618
4639
LFORTRAN_ASSERT (ASR::is_a<ASR::ExternalSymbol_t>(*sym));
4619
4640
current_scope->add_symbol (sym_name, sym);
4620
4641
}
@@ -5898,7 +5919,13 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
5898
5919
} else {
5899
5920
return res2.error ;
5900
5921
}
5901
- LFORTRAN_ASSERT (asr_verify (*tu, true , diagnostics));
5922
+ #if defined(WITH_LFORTRAN_ASSERT)
5923
+ diag::Diagnostics diagnostics;
5924
+ if (!asr_verify (*tu, true , diagnostics)) {
5925
+ std::cerr << diagnostics.render2 ();
5926
+ throw LCompilersException (" Verify failed" );
5927
+ };
5928
+ #endif
5902
5929
}
5903
5930
5904
5931
if (main_module) {
0 commit comments