@@ -307,6 +307,7 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
307
307
bool compile_module = true ;
308
308
ASR::TranslationUnit_t* mod1 = nullptr ;
309
309
std::string input;
310
+ std::string module_dir_name = " " ;
310
311
bool found = set_module_path (infile0c, rl_path, infile,
311
312
path_used, input, ltypes, enum_py);
312
313
if ( !found ) {
@@ -335,7 +336,15 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
335
336
336
337
// insert into `symtab`
337
338
std::vector<std::pair<std::string, ASR::Module_t*>> children_modules;
338
- ASRUtils::extract_module_python (*mod1, children_modules, module_name);
339
+ if (module_name == " __init__" ) {
340
+ // remove `__init__.py`
341
+ module_dir_name = infile.substr (0 , infile.find_last_of (' /' ));
342
+ // assign module directory name
343
+ module_dir_name = module_dir_name.substr (module_dir_name.find_last_of (' /' ) + 1 );
344
+ ASRUtils::extract_module_python (*mod1, children_modules, module_dir_name);
345
+ } else {
346
+ ASRUtils::extract_module_python (*mod1, children_modules, module_name);
347
+ }
339
348
ASR::Module_t* mod2 = nullptr ;
340
349
for ( auto & a: children_modules ) {
341
350
std::string a_name = a.first ;
@@ -344,6 +353,10 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
344
353
a_mod->m_name = s2c (al, module_name);
345
354
a_mod->m_intrinsic = intrinsic;
346
355
mod2 = a_mod;
356
+ } else if (a_name == module_dir_name) {
357
+ a_mod->m_name = s2c (al, module_dir_name);
358
+ a_mod->m_intrinsic = intrinsic;
359
+ mod2 = a_mod;
347
360
}
348
361
symtab->add_symbol (a_name, (ASR::symbol_t *)a_mod);
349
362
a_mod->m_symtab ->parent = symtab;
@@ -429,9 +442,18 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
429
442
ASR::accessType::Public
430
443
);
431
444
return ASR::down_cast<ASR::symbol_t >(v);
445
+ } else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
446
+ ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
447
+ SymbolTable *symtab = current_scope;
448
+ while (symtab->parent != nullptr ) symtab = symtab->parent ;
449
+ ASR::symbol_t *sym = symtab->get_symbol (es->m_module_name );
450
+ ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
451
+
452
+ return import_from_module (al, m, symtab, es->m_name ,
453
+ cur_sym_name, new_sym_name, loc);
432
454
} else {
433
- throw SemanticError (" Only Subroutines, Functions and Variables are currently supported in 'import' " ,
434
- loc);
455
+ throw SemanticError (" Only Subroutines, Functions, Variables and "
456
+ " ExternalSymbol are currently supported in 'import' " , loc);
435
457
}
436
458
LFORTRAN_ASSERT (false );
437
459
return nullptr ;
@@ -3157,6 +3179,24 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3157
3179
st = st->parent ;
3158
3180
}
3159
3181
bool ltypes, enum_py;
3182
+ if (msym != " ltypes" ) {
3183
+ if (import_path != " " &&
3184
+ !path_exits (paths[0 ] + ' /' + msym + " .py" )) {
3185
+ paths = {import_path};
3186
+ if (parent_dir != " " ) paths[0 ] += ' /' + parent_dir;
3187
+ if (is_directory (paths[0 ])) {
3188
+ paths[0 ] += ' /' + msym;
3189
+ msym = " __init__" ;
3190
+ }
3191
+ } else if (is_directory (msym)) {
3192
+ paths = {rl_path, msym};
3193
+ msym = " __init__" ;
3194
+ } else if (paths[1 ] != " "
3195
+ && is_directory (paths[1 ] + ' /' + msym)) {
3196
+ paths[1 ] += ' /' + msym;
3197
+ msym = " __init__" ;
3198
+ }
3199
+ }
3160
3200
t = (ASR::symbol_t *)(load_module (al, st,
3161
3201
msym, x.base .base .loc , false , paths, ltypes, enum_py,
3162
3202
[&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); }
@@ -3202,6 +3242,24 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3202
3242
}
3203
3243
for (auto &mod_sym : mods) {
3204
3244
bool ltypes, enum_py;
3245
+ if (mod_sym != " ltypes" ) {
3246
+ if (import_path != " " &&
3247
+ !path_exits (paths[0 ] + ' /' + mod_sym + " .py" )) {
3248
+ paths = {import_path};
3249
+ if (parent_dir != " " ) paths[0 ] += ' /' + parent_dir;
3250
+ if (is_directory (paths[0 ])) {
3251
+ paths[0 ] += ' /' + mod_sym;
3252
+ mod_sym = " __init__" ;
3253
+ }
3254
+ } else if (is_directory (mod_sym)) {
3255
+ paths = {rl_path, mod_sym};
3256
+ mod_sym = " __init__" ;
3257
+ } else if (paths[1 ] != " "
3258
+ && is_directory (paths[1 ] + ' /' + mod_sym)) {
3259
+ paths[1 ] += ' /' + mod_sym;
3260
+ mod_sym = " __init__" ;
3261
+ }
3262
+ }
3205
3263
t = (ASR::symbol_t *)(load_module (al, st,
3206
3264
mod_sym, x.base .base .loc , false , paths, ltypes, enum_py,
3207
3265
[&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); }
0 commit comments