@@ -4872,50 +4872,47 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
48724872 }
48734873 }
48744874
4875+ // --- RESTORED visit_ImportFrom ---
48754876 void visit_ImportFrom (const AST::ImportFrom_t &x) {
48764877 if (!x.m_module ) {
48774878 throw SemanticError (" Not implemented: The import statement must currently specify the module name" , x.base .base .loc );
48784879 }
48794880 std::string msym = x.m_module ; // Module name
48804881
48814882 // Get the module, for now assuming it is not loaded, so we load it:
4882- ASR::symbol_t *t = nullptr ; // current_scope->parent->resolve_symbol(msym);
4883- if (!t) {
4884- std::string rl_path = get_runtime_library_dir ();
4885- std::vector<std::string> paths;
4886- for (auto &path:import_paths) {
4887- paths.push_back (path);
4888- }
4889- paths.push_back (rl_path);
4890- paths.push_back (parent_dir);
4883+ ASR::symbol_t *t = nullptr ;
4884+ std::string rl_path = get_runtime_library_dir ();
4885+ std::vector<std::string> paths;
4886+ for (auto &path:import_paths) {
4887+ paths.push_back (path);
4888+ }
4889+ paths.push_back (rl_path);
4890+ paths.push_back (parent_dir);
48914891
4892- bool lpython, enum_py, copy, sympy;
4893- set_module_symbol (msym, paths);
4894- t = (ASR::symbol_t *)(load_module (al, global_scope,
4895- msym, x.base .base .loc , diag, lm, false , paths, lpython, enum_py, copy, sympy,
4896- [&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); },
4897- allow_implicit_casting));
4898- if (lpython || enum_py || copy || sympy) {
4899- // TODO: For now we skip lpython import completely. Later on we should note what symbols
4900- // got imported from it, and give an error message if an annotation is used without
4901- // importing it.
4902- tmp = nullptr ;
4903- return ;
4904- }
4905- if (!t) {
4906- throw SemanticError (" The module '" + msym + " ' cannot be loaded" ,
4907- x.base .base .loc );
4908- }
4909- if ( msym == " __init__" ) {
4910- for ( auto item: ASRUtils::symbol_symtab (t)->get_scope () ) {
4911- if ( ASR::is_a<ASR::ExternalSymbol_t>(*item.second ) ) {
4912- current_module_dependencies.push_back (al,
4913- ASR::down_cast<ASR::ExternalSymbol_t>(item.second )->m_module_name );
4914- }
4892+ bool lpython, enum_py, copy, sympy;
4893+ set_module_symbol (msym, paths);
4894+ t = (ASR::symbol_t *)(load_module (al, global_scope,
4895+ msym, x.base .base .loc , diag, lm, false , paths, lpython, enum_py, copy, sympy,
4896+ [&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); },
4897+ allow_implicit_casting));
4898+
4899+ if (lpython || enum_py || copy || sympy) {
4900+ tmp = nullptr ;
4901+ return ;
4902+ }
4903+ if (!t) {
4904+ throw SemanticError (" The module '" + msym + " ' cannot be loaded" ,
4905+ x.base .base .loc );
4906+ }
4907+ if ( msym == " __init__" ) {
4908+ for ( auto item: ASRUtils::symbol_symtab (t)->get_scope () ) {
4909+ if ( ASR::is_a<ASR::ExternalSymbol_t>(*item.second ) ) {
4910+ current_module_dependencies.push_back (al,
4911+ ASR::down_cast<ASR::ExternalSymbol_t>(item.second )->m_module_name );
49154912 }
4916- } else {
4917- current_module_dependencies.push_back (al, s2c (al, msym));
49184913 }
4914+ } else {
4915+ current_module_dependencies.push_back (al, s2c (al, msym));
49194916 }
49204917
49214918 ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
@@ -4931,7 +4928,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49314928 new_sym_name = ASRUtils::get_mangled_name (m, x.m_names [j].m_asname );
49324929 }
49334930 ASR::symbol_t *t = import_from_module (al, m, current_scope, msym,
4934- remote_sym, new_sym_name, x.m_names [i].loc , true );
4931+ remote_sym, new_sym_name, x.m_names [i].loc , true );
49354932 if (current_scope->get_scope ().find (new_sym_name) != current_scope->get_scope ().end ()) {
49364933 ASR::symbol_t *old_sym = current_scope->get_scope ().find (new_sym_name)->second ;
49374934 diag.add (diag::Diagnostic (
@@ -4946,10 +4943,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49464943 current_scope->add_symbol (new_sym_name, t);
49474944 }
49484945 }
4949-
49504946 tmp = nullptr ;
49514947 }
4952-
4948+ // --- YOUR FIXED visit_Import ---
49534949 void visit_Import (const AST::Import_t &x) {
49544950 ASR::symbol_t *t = nullptr ;
49554951 std::string rl_path = get_runtime_library_dir ();
@@ -4992,116 +4988,16 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49924988 current_module_dependencies.push_back (al, s2c (al, mod_sym));
49934989 }
49944990
4995- // --- ALIAS LOGIC (FIXED) ---
49964991 if (alias) {
49974992 std::string alias_str = std::string (alias);
4998-
49994993 if (current_scope->get_symbol (alias_str) == nullptr ) {
5000-
5001- // The Corrected Constructor Call
50024994 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
4995+ al, x.base .base .loc , current_scope,
4996+ s2c (al, alias_str), t, s2c (al, mod_sym),
4997+ nullptr , 0 , s2c (al, mod_sym), ASR::accessType::Public
50134998 );
5014-
50154999 current_scope->add_symbol (alias_str, ASR::down_cast<ASR::symbol_t >(ext_sym));
50165000 }
5017- } else {
5018- if (current_scope->get_symbol (mod_sym) == nullptr ) {
5019- current_scope->add_symbol (mod_sym, t);
5020- }
5021- }
5022- }
5023- }
5024-
5025- void visit_AugAssign (const AST::AugAssign_t &/* x*/ ) {
5026- // We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
5027- }
5028-
5029- void visit_AnnAssign (const AST::AnnAssign_t &/* x*/ ) {
5030- // We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
5031- }
5032-
5033- void visit_Assign (const AST::Assign_t &x) {
5034- /* *
5035- * Type variables have to be put in the symbol table before checking function definitions.
5036- * This describes a different treatment for Assign in the form of `T = TypeVar('T', ...)`
5037- */
5038- if (x.n_targets == 1 && AST::is_a<AST::Call_t>(*x.m_value )) {
5039- AST::Call_t *rh = AST::down_cast<AST::Call_t>(x.m_value );
5040- if (AST::is_a<AST::Name_t>(*rh->m_func )) {
5041- AST::Name_t *tv = AST::down_cast<AST::Name_t>(rh->m_func );
5042- std::string f_name = tv->m_id ;
5043- if (strcmp (s2c (al, f_name), " TypeVar" ) == 0 &&
5044- rh->n_args > 0 && AST::is_a<AST::ConstantStr_t>(*rh->m_args [0 ])) {
5045- if (AST::is_a<AST::Name_t>(*x.m_targets [0 ])) {
5046- std::string tvar_name = AST::down_cast<AST::Name_t>(x.m_targets [0 ])->m_id ;
5047- // Check if the type variable name is a reserved type keyword
5048- const char * type_list[15 ]
5049- = { " list" , " set" , " dict" , " tuple" ,
5050- " i8" , " i16" , " i32" , " i64" , " f32" ,
5051- " f64" , " c32" , " c64" , " str" , " bool" , " i1" };
5052- for (int i = 0 ; i < 15 ; i++) {
5053- if (strcmp (s2c (al, tvar_name), type_list[i]) == 0 ) {
5054- throw SemanticError (tvar_name + " is a reserved type, consider a different type variable name" ,
5055- x.base .base .loc );
5056- }
5057- }
5058- // Check if the type variable is already defined
5059- if (current_scope->get_scope ().find (tvar_name) !=
5060- current_scope->get_scope ().end ()) {
5061- ASR::symbol_t *orig_decl = current_scope->get_symbol (tvar_name);
5062- throw SemanticError (diag::Diagnostic (
5063- " Variable " + tvar_name + " is already declared in the same scope" ,
5064- diag::Level::Error, diag::Stage::Semantic, {
5065- diag::Label (" original declaration" , {orig_decl->base .loc }, false ),
5066- diag::Label (" redeclaration" , {x.base .base .loc }),
5067- }));
5068- }
5069-
5070- // Build ttype
5071- Vec<ASR::dimension_t > dims;
5072- dims.reserve (al, 4 );
5073-
5074- ASR::ttype_t *type = ASRUtils::TYPE (ASR::make_TypeParameter_t (al, x.base .base .loc ,
5075- s2c (al, tvar_name)));
5076- type = ASRUtils::make_Array_t_util (al, x.base .base .loc , type, dims.p , dims.size ());
5077-
5078- ASR::expr_t *value = nullptr ;
5079- ASR::expr_t *init_expr = nullptr ;
5080- ASR::intentType s_intent = ASRUtils::intent_local;
5081- ASR::storage_typeType storage_type = ASR::storage_typeType::Default;
5082- ASR::abiType current_procedure_abi_type = ASR::abiType::Source;
5083- ASR::accessType s_access = ASR::accessType::Public;
5084- ASR::presenceType s_presence = ASR::presenceType::Required;
5085- bool value_attr = false ;
5086-
5087- SetChar variable_dependencies_vec;
5088- variable_dependencies_vec.reserve (al, 1 );
5089- ASRUtils::collect_variable_dependencies (al, variable_dependencies_vec, type, init_expr, value);
5090- // Build the variable and add it to the scope
5091- ASR::asr_t *v = ASR::make_Variable_t (al, x.base .base .loc , current_scope,
5092- s2c (al, tvar_name), variable_dependencies_vec.p , variable_dependencies_vec.size (),
5093- s_intent, init_expr, value, storage_type, type, nullptr , current_procedure_abi_type,
5094- s_access, s_presence, value_attr, false , false , nullptr , false , false );
5095- current_scope->add_symbol (tvar_name, ASR::down_cast<ASR::symbol_t >(v));
5096-
5097- tmp = nullptr ;
5098-
5099- return ;
5100- } else {
5101- // This error might need to be further elaborated
5102- throw SemanticError (" Type variable must be a variable" , x.base .base .loc );
5103- }
5104- }
51055001 }
51065002 }
51075003 }
0 commit comments