@@ -420,133 +420,6 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
420420 return mod1_mod;
421421}
422422
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,
425- const Location &loc, bool skip_current_scope_check=false ) {
426- new_sym_name = ASRUtils::get_mangled_name (m, new_sym_name);
427- ASR::symbol_t *t = m->m_symtab ->resolve_symbol (cur_sym_name);
428- if (!t) {
429- throw SemanticError (" The symbol '" + cur_sym_name + " ' not found in the module '" + std::string (m->m_name ) + " '" ,
430- loc);
431- }
432- if (!skip_current_scope_check &&
433- current_scope->get_scope ().find (new_sym_name) != current_scope->get_scope ().end ()) {
434- throw SemanticError (new_sym_name + " already defined" , loc);
435- }
436- if (ASR::is_a<ASR::Function_t>(*t)) {
437- ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
438- // `mfn` is the Function in a module. Now we construct
439- // an ExternalSymbol that points to it.
440- Str name;
441- name.from_str (al, new_sym_name);
442- char *cname = name.c_str (al);
443- ASR::asr_t *fn = ASR::make_ExternalSymbol_t (
444- al, loc,
445- /* a_symtab */ current_scope,
446- /* a_name */ cname,
447- (ASR::symbol_t *)mfn,
448- m->m_name , nullptr , 0 , mfn->m_name ,
449- ASR::accessType::Public
450- );
451- return ASR::down_cast<ASR::symbol_t >(fn);
452- } else if (ASR::is_a<ASR::StructType_t>(*t)) {
453- ASR::StructType_t *st = ASR::down_cast<ASR::StructType_t>(t);
454- // `st` is the StructType in a module. Now we construct
455- // an ExternalSymbol that points to it.
456- Str name;
457- name.from_str (al, new_sym_name);
458- char *cname = name.c_str (al);
459- ASR::asr_t *est = ASR::make_ExternalSymbol_t (
460- al, loc,
461- /* a_symtab */ current_scope,
462- /* a_name */ cname,
463- (ASR::symbol_t *)st,
464- m->m_name , nullptr , 0 , st->m_name ,
465- ASR::accessType::Public
466- );
467- return ASR::down_cast<ASR::symbol_t >(est);
468- } else if (ASR::is_a<ASR::EnumType_t>(*t)) {
469- ASR::EnumType_t *et = ASR::down_cast<ASR::EnumType_t>(t);
470- Str name;
471- name.from_str (al, new_sym_name);
472- char *cname = name.c_str (al);
473- ASR::asr_t *est = ASR::make_ExternalSymbol_t (
474- al, loc,
475- /* a_symtab */ current_scope,
476- /* a_name */ cname,
477- (ASR::symbol_t *)et,
478- m->m_name , nullptr , 0 , et->m_name ,
479- ASR::accessType::Public
480- );
481- return ASR::down_cast<ASR::symbol_t >(est);
482- } else if (ASR::is_a<ASR::UnionType_t>(*t)) {
483- ASR::UnionType_t *ut = ASR::down_cast<ASR::UnionType_t>(t);
484- Str name;
485- name.from_str (al, new_sym_name);
486- char *cname = name.c_str (al);
487- ASR::asr_t *est = ASR::make_ExternalSymbol_t (
488- al, loc,
489- /* a_symtab */ current_scope,
490- /* a_name */ cname,
491- (ASR::symbol_t *)ut,
492- m->m_name , nullptr , 0 , ut->m_name ,
493- ASR::accessType::Public
494- );
495- return ASR::down_cast<ASR::symbol_t >(est);
496- } else if (ASR::is_a<ASR::Variable_t>(*t)) {
497- ASR::Variable_t *mv = ASR::down_cast<ASR::Variable_t>(t);
498- // `mv` is the Variable in a module. Now we construct
499- // an ExternalSymbol that points to it.
500- Str name;
501- name.from_str (al, new_sym_name);
502- char *cname = name.c_str (al);
503- ASR::asr_t *v = ASR::make_ExternalSymbol_t (
504- al, loc,
505- /* a_symtab */ current_scope,
506- /* a_name */ cname,
507- (ASR::symbol_t *)mv,
508- m->m_name , nullptr , 0 , mv->m_name ,
509- ASR::accessType::Public
510- );
511- return ASR::down_cast<ASR::symbol_t >(v);
512- } else if (ASR::is_a<ASR::GenericProcedure_t>(*t)) {
513- ASR::GenericProcedure_t *gt = ASR::down_cast<ASR::GenericProcedure_t>(t);
514- Str name;
515- name.from_str (al, new_sym_name);
516- char *cname = name.c_str (al);
517- ASR::asr_t *v = ASR::make_ExternalSymbol_t (
518- al, loc,
519- /* a_symtab */ current_scope,
520- /* a_name */ cname,
521- (ASR::symbol_t *)gt,
522- m->m_name , nullptr , 0 , gt->m_name ,
523- ASR::accessType::Public
524- );
525- return ASR::down_cast<ASR::symbol_t >(v);
526- } else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
527- ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
528- SymbolTable *symtab = current_scope;
529- // while (symtab->parent != nullptr) symtab = symtab->parent;
530- ASR::symbol_t *sym = symtab->resolve_symbol (es->m_module_name );
531- ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
532- return import_from_module (al, m, symtab, es->m_module_name ,
533- cur_sym_name, new_sym_name, loc);
534- } else if (ASR::is_a<ASR::Module_t>(*t)) {
535- ASR::Module_t* mt = ASR::down_cast<ASR::Module_t>(t);
536- std::string mangled_cur_sym_name = ASRUtils::get_mangled_name (mt, new_sym_name);
537- if ( cur_sym_name == mangled_cur_sym_name ) {
538- throw SemanticError (" Importing modules isn't supported yet." , loc);
539- }
540- return import_from_module (al, mt, current_scope, std::string (mt->m_name ),
541- cur_sym_name, new_sym_name, loc);
542- } else {
543- throw SemanticError (" Only Subroutines, Functions, StructType, Variables and "
544- " ExternalSymbol are currently supported in 'import'" , loc);
545- }
546- LCOMPILERS_ASSERT (false );
547- return nullptr ;
548- }
549-
550423// Here, we call the global_initializer & global_statements to
551424// initialize and execute the global symbols
552425void get_calls_to_global_init_and_stmts (Allocator &al, const Location &loc, SymbolTable* scope,
@@ -1117,6 +990,134 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
1117990 return type;
1118991 }
1119992
993+ ASR::symbol_t * import_from_module (Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
994+ std::string /* mname*/ , std::string cur_sym_name, std::string& new_sym_name,
995+ const Location &loc, bool skip_current_scope_check=false ) {
996+ new_sym_name = ASRUtils::get_mangled_name (m, new_sym_name);
997+ ASR::symbol_t *t = m->m_symtab ->resolve_symbol (cur_sym_name);
998+ if (!t) {
999+ throw SemanticError (" The symbol '" + cur_sym_name + " ' not found in the module '" + std::string (m->m_name ) + " '" ,
1000+ loc);
1001+ }
1002+ if (!skip_current_scope_check &&
1003+ current_scope->get_scope ().find (new_sym_name) != current_scope->get_scope ().end ()) {
1004+ throw SemanticError (new_sym_name + " already defined" , loc);
1005+ }
1006+ if (ASR::is_a<ASR::Function_t>(*t)) {
1007+ ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
1008+ // `mfn` is the Function in a module. Now we construct
1009+ // an ExternalSymbol that points to it.
1010+ Str name;
1011+ name.from_str (al, new_sym_name);
1012+ char *cname = name.c_str (al);
1013+ ASR::asr_t *fn = ASR::make_ExternalSymbol_t (
1014+ al, loc,
1015+ /* a_symtab */ current_scope,
1016+ /* a_name */ cname,
1017+ (ASR::symbol_t *)mfn,
1018+ m->m_name , nullptr , 0 , mfn->m_name ,
1019+ ASR::accessType::Public
1020+ );
1021+ return ASR::down_cast<ASR::symbol_t >(fn);
1022+ } else if (ASR::is_a<ASR::StructType_t>(*t)) {
1023+ ASR::StructType_t *st = ASR::down_cast<ASR::StructType_t>(t);
1024+ // `st` is the StructType in a module. Now we construct
1025+ // an ExternalSymbol that points to it.
1026+ Str name;
1027+ name.from_str (al, new_sym_name);
1028+ char *cname = name.c_str (al);
1029+ ASR::asr_t *est = ASR::make_ExternalSymbol_t (
1030+ al, loc,
1031+ /* a_symtab */ current_scope,
1032+ /* a_name */ cname,
1033+ (ASR::symbol_t *)st,
1034+ m->m_name , nullptr , 0 , st->m_name ,
1035+ ASR::accessType::Public
1036+ );
1037+ return ASR::down_cast<ASR::symbol_t >(est);
1038+ } else if (ASR::is_a<ASR::EnumType_t>(*t)) {
1039+ ASR::EnumType_t *et = ASR::down_cast<ASR::EnumType_t>(t);
1040+ Str name;
1041+ name.from_str (al, new_sym_name);
1042+ char *cname = name.c_str (al);
1043+ ASR::asr_t *est = ASR::make_ExternalSymbol_t (
1044+ al, loc,
1045+ /* a_symtab */ current_scope,
1046+ /* a_name */ cname,
1047+ (ASR::symbol_t *)et,
1048+ m->m_name , nullptr , 0 , et->m_name ,
1049+ ASR::accessType::Public
1050+ );
1051+ return ASR::down_cast<ASR::symbol_t >(est);
1052+ } else if (ASR::is_a<ASR::UnionType_t>(*t)) {
1053+ ASR::UnionType_t *ut = ASR::down_cast<ASR::UnionType_t>(t);
1054+ Str name;
1055+ name.from_str (al, new_sym_name);
1056+ char *cname = name.c_str (al);
1057+ ASR::asr_t *est = ASR::make_ExternalSymbol_t (
1058+ al, loc,
1059+ /* a_symtab */ current_scope,
1060+ /* a_name */ cname,
1061+ (ASR::symbol_t *)ut,
1062+ m->m_name , nullptr , 0 , ut->m_name ,
1063+ ASR::accessType::Public
1064+ );
1065+ return ASR::down_cast<ASR::symbol_t >(est);
1066+ } else if (ASR::is_a<ASR::Variable_t>(*t)) {
1067+ ASR::Variable_t *mv = ASR::down_cast<ASR::Variable_t>(t);
1068+ // `mv` is the Variable in a module. Now we construct
1069+ // an ExternalSymbol that points to it.
1070+ Str name;
1071+ name.from_str (al, new_sym_name);
1072+ char *cname = name.c_str (al);
1073+ ASR::asr_t *v = ASR::make_ExternalSymbol_t (
1074+ al, loc,
1075+ /* a_symtab */ current_scope,
1076+ /* a_name */ cname,
1077+ (ASR::symbol_t *)mv,
1078+ m->m_name , nullptr , 0 , mv->m_name ,
1079+ ASR::accessType::Public
1080+ );
1081+ return ASR::down_cast<ASR::symbol_t >(v);
1082+ } else if (ASR::is_a<ASR::GenericProcedure_t>(*t)) {
1083+ ASR::GenericProcedure_t *gt = ASR::down_cast<ASR::GenericProcedure_t>(t);
1084+ Str name;
1085+ name.from_str (al, new_sym_name);
1086+ char *cname = name.c_str (al);
1087+ ASR::asr_t *v = ASR::make_ExternalSymbol_t (
1088+ al, loc,
1089+ /* a_symtab */ current_scope,
1090+ /* a_name */ cname,
1091+ (ASR::symbol_t *)gt,
1092+ m->m_name , nullptr , 0 , gt->m_name ,
1093+ ASR::accessType::Public
1094+ );
1095+ return ASR::down_cast<ASR::symbol_t >(v);
1096+ } else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
1097+ ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
1098+ SymbolTable *symtab = current_scope;
1099+ // while (symtab->parent != nullptr) symtab = symtab->parent;
1100+ ASR::symbol_t *sym = symtab->resolve_symbol (es->m_module_name );
1101+ ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
1102+ return import_from_module (al, m, symtab, es->m_module_name ,
1103+ cur_sym_name, new_sym_name, loc);
1104+ } else if (ASR::is_a<ASR::Module_t>(*t)) {
1105+ ASR::Module_t* mt = ASR::down_cast<ASR::Module_t>(t);
1106+ std::string mangled_cur_sym_name = ASRUtils::get_mangled_name (mt, new_sym_name);
1107+ if ( cur_sym_name == mangled_cur_sym_name ) {
1108+ throw SemanticError (" Importing modules isn't supported yet." , loc);
1109+ }
1110+ return import_from_module (al, mt, current_scope, std::string (mt->m_name ),
1111+ cur_sym_name, new_sym_name, loc);
1112+ } else {
1113+ throw SemanticError (" Only Subroutines, Functions, StructType, Variables and "
1114+ " ExternalSymbol are currently supported in 'import'" , loc);
1115+ }
1116+ LCOMPILERS_ASSERT (false );
1117+ return nullptr ;
1118+ }
1119+
1120+
11201121 // Function to create appropriate call based on symbol type. If it is external
11211122 // generic symbol then it changes the name accordingly.
11221123 ASR::asr_t * make_call_helper (Allocator &al, ASR::symbol_t * s, SymbolTable *current_scope,
0 commit comments