From 1f66f14b1e6adc6d39c9525282ea149365f931e2 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 7 Jul 2024 17:01:29 +0530 Subject: [PATCH 1/5] Enabled init fn generation --- src/lpython/semantics/python_ast_to_asr.cpp | 263 +++++++++++++------- 1 file changed, 175 insertions(+), 88 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 0adf860653..fc22eb85d5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2930,86 +2930,8 @@ class CommonVisitor : public AST::BaseVisitor { assign_asr_target = assign_asr_target_copy; } - void handle_init_method(const AST::FunctionDef_t &x, - Vec& member_names, Vec &member_init){ - if(x.n_decorator_list > 0) { - throw SemanticError("Decorators for __init__ not implemented", - x.base.base.loc); - } - if( x.m_args.n_args > 1 ) { - throw SemanticError("Only default constructors implemented ", - x.base.base.loc); - } - // TODO: the obj_name can be anything - std::string obj_name = "self"; - if ( std::string(x.m_args.m_args[0].m_arg) != obj_name) { - throw SemanticError("Only `self` can be used as object name for now", - x.base.base.loc); - } - for(size_t i = 0; i < x.n_body; i++) { - std::string var_name; - if (! AST::is_a(*x.m_body[i]) ){ - throw SemanticError("Only AnnAssign implemented in __init__ ", - x.m_body[i]->base.loc); - } - AST::AnnAssign_t ann_assign = *AST::down_cast(x.m_body[i]); - if(AST::is_a(*ann_assign.m_target)){ - AST::Attribute_t* a = AST::down_cast(ann_assign.m_target); - if(AST::is_a(*a->m_value)) { - AST::Name_t* n = AST::down_cast(a->m_value); - if(std::string(n->m_id) != obj_name) { - throw SemanticError("Object name doesn't matach", - x.m_body[i]->base.loc); - } - } - var_name = a->m_attr; - member_names.push_back(al, s2c(al, var_name)); - } else { - throw SemanticError("Only Attribute supported as target in " - "AnnAssign inside Class", x.m_body[i]->base.loc); - } - ASR::expr_t* init_expr = nullptr; - ASR::abiType abi = ASR::abiType::Source; - bool is_allocatable = false, is_const = false; - ASR::ttype_t *type = ast_expr_to_asr_type(ann_assign.m_annotation->base.loc, - *ann_assign.m_annotation, is_allocatable, is_const, true); - - ASR::storage_typeType storage_type = ASR::storage_typeType::Default; - create_add_variable_to_scope(var_name, type, - ann_assign.base.base.loc, abi, storage_type); - - if (ann_assign.m_value == nullptr) { - throw SemanticError("Missing an initialiser for the data member", - x.m_body[i]->base.loc); - } - this->visit_expr(*ann_assign.m_value); - if (tmp && ASR::is_a(*tmp)) { - ASR::expr_t* value = ASRUtils::EXPR(tmp); - ASR::ttype_t* underlying_type = type; - cast_helper(underlying_type, value, value->base.loc); - if (!ASRUtils::check_equal_type(underlying_type, ASRUtils::expr_type(value), true)) { - std::string ltype = ASRUtils::type_to_str_python(underlying_type); - std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(value)); - diag.add(diag::Diagnostic( - "Type mismatch in annotation-assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {ann_assign.m_target->base.loc, value->base.loc}) - }) - ); - throw SemanticAbort(); - } - init_expr = value; - } - ASR::symbol_t* var_sym = current_scope->resolve_symbol(var_name); - ASR::call_arg_t c_arg; - c_arg.loc = var_sym->base.loc; - c_arg.m_value = init_expr; - member_init.push_back(al, c_arg); - } - - } - + virtual void handle_init_method(const AST::FunctionDef_t &/*x*/, + Vec& /*member_names*/, Vec &/*member_init*/) = 0; void visit_ClassMembers(const AST::ClassDef_t& x, Vec& member_names, Vec& member_fn_names, SetChar& struct_dependencies, Vec &member_init, @@ -3038,12 +2960,14 @@ class CommonVisitor : public AST::BaseVisitor { std::string f_name = f->m_name; if (f_name == "__init__") { this->handle_init_method(*f, member_names, member_init); + this->handle_class_method(*f); // This seems hackish, as struct depends on itself // We need to handle this later. // Removing this throws a ASR verify error + member_fn_names.push_back(al, f->m_name); struct_dependencies.push_back(al, x.m_name); } else { - this->visit_stmt(*x.m_body[i]); + this->handle_class_method(*f); member_fn_names.push_back(al, f->m_name); } continue; @@ -3118,6 +3042,8 @@ class CommonVisitor : public AST::BaseVisitor { } } + virtual void handle_class_method(AST::FunctionDef_t& x) = 0; + ASR::abiType get_abi_from_decorators(AST::expr_t** decorators, size_t n) { for( size_t i = 0; i < n; i++ ) { AST::expr_t* decorator = decorators[i]; @@ -3133,6 +3059,37 @@ class CommonVisitor : public AST::BaseVisitor { return ASR::abiType::Source; } + void replace_add_self_instance(ASR::symbol_t *x, const LCompilers::Location& loc){ + if ( x==nullptr || !ASR::is_a(*x) ){ + throw LCompilersException("x doesn't exist or isn't a Struct_t"); + } + SymbolTable *parent_scope = current_scope; + ASR::Struct_t* s = ASR::down_cast(x); + ASR::ttype_t* st = ASRUtils::TYPE( + ASRUtils::make_StructType_t_util(al, loc, + x)); + current_scope = s->m_symtab; + std::string self_name = "self"; + for(size_t i = 0; in_member_functions;i++){ + std::string name = std::string(s->m_member_functions[i]); + ASR::symbol_t* f_sym = current_scope->get_symbol(name); + LCOMPILERS_ASSERT(ASR::is_a(*f_sym)); + ASR::Function_t* f = ASR::down_cast(f_sym); + SymbolTable *struct_scope = current_scope; + current_scope = f->m_symtab; + if ( current_scope->get_symbol(self_name) ) { + current_scope->erase_symbol(self_name); + create_add_variable_to_scope(self_name, st, + loc, ASR::abiType::Source); + } else { + create_add_variable_to_scope(self_name, st, + loc, ASR::abiType::Source); + } + current_scope = struct_scope; + } + current_scope = parent_scope; + } + void visit_ClassDef(const AST::ClassDef_t& x) { std::string x_m_name = x.m_name; if( is_enum(x.m_bases, x.n_bases) ) { @@ -3306,15 +3263,19 @@ class CommonVisitor : public AST::BaseVisitor { SymbolTable *parent_scope = current_scope; if( ASR::symbol_t* sym = current_scope->resolve_symbol(x_m_name) ) { LCOMPILERS_ASSERT(ASR::is_a(*sym)); + replace_add_self_instance(sym,x.base.base.loc); ASR::Struct_t *st = ASR::down_cast(sym); current_scope = st->m_symtab; for( size_t i = 0; i < x.n_body; i++ ) { if ( AST::is_a(*x.m_body[i]) ) { AST::FunctionDef_t* f = AST::down_cast(x.m_body[i]); - if ( std::string(f->m_name) != std::string("__init__") ) { - this->visit_stmt(*x.m_body[i]); + Vec member_names; + Vec member_init; + if ( std::string(f->m_name) == std::string("__init__") ) { + this->handle_init_method(*f, member_names, member_init); } + this->visit_stmt(*x.m_body[i]); } } } else { @@ -3343,16 +3304,11 @@ class CommonVisitor : public AST::BaseVisitor { member_fn_names.size(), class_abi, ASR::accessType::Public, false, false, member_init.p, member_init.size(), nullptr, nullptr)); - ASR::ttype_t* class_type = ASRUtils::TYPE( - ASRUtils::make_StructType_t_util(al, x.base.base.loc, - class_sym)); std::string self_name = "self"; if ( current_scope->get_symbol(self_name) ) { throw SemanticError("`self` cannot be used as a data member " "for now", x.base.base.loc); } - create_add_variable_to_scope(self_name, class_type, - x.base.base.loc, class_abi); parent_scope->add_symbol(x.m_name, class_sym); } current_scope = parent_scope; @@ -4295,6 +4251,117 @@ class SymbolTableVisitor : public CommonVisitor { tmp = tmp0; } + void handle_class_method(AST::FunctionDef_t& x){ + AST::arguments_t old_args = x.m_args; + AST::arguments_t* new_args = al.make_new(); + if ( old_args.n_defaults || old_args.n_kw_defaults || + old_args.n_kwarg || old_args.n_kwonlyargs || + old_args.n_posonlyargs || + old_args.n_vararg ) { + throw LCompilersException("Only args implemented yet"); + } + new_args->n_args = old_args.n_args - 1; + Vec args_vec; + args_vec.reserve(al, new_args->n_args); + for (size_t i = 1; i < old_args.n_args ; i++) { + AST::arg_t* arg = al.make_new(); + arg->loc = old_args.loc; + arg->m_annotation = old_args.m_args[i].m_annotation; + if ( old_args.m_args[i].m_arg ==nullptr ){ + arg->m_arg = nullptr; + } else { + arg->m_arg = s2c(al,std::string(old_args.m_args[i].m_arg)); + } + if ( old_args.m_args[i].m_type_comment ==nullptr ){ + arg->m_type_comment = nullptr; + } else { + arg->m_type_comment = s2c(al,std::string(old_args.m_args[i].m_type_comment)); + } + args_vec.push_back(al,*arg); + } + new_args->m_args = args_vec.p; + x = *AST::down_cast(AST::make_FunctionDef_t(al,x.base.base.loc, + x.m_name,*new_args,x.m_body,x.n_body,x.m_decorator_list,x.n_decorator_list, + x.m_returns,x.m_type_comment)); + visit_FunctionDef(x); + } + + void handle_init_method(const AST::FunctionDef_t &x, + Vec& member_names, Vec &member_init){ + if(x.n_decorator_list > 0) { + throw SemanticError("Decorators for __init__ not implemented", + x.base.base.loc); + } + std::string obj_name = "self"; + if ( std::string(x.m_args.m_args[0].m_arg) != obj_name) { + throw SemanticError("Only `self` can be used as object name for now", + x.base.base.loc); + } + for(size_t i = 0; i < x.n_body; i++) { + std::string var_name; + if (! AST::is_a(*x.m_body[i]) ){ + throw SemanticError("Only AnnAssign implemented in __init__ ", + x.m_body[i]->base.loc); + } + AST::AnnAssign_t ann_assign = *AST::down_cast(x.m_body[i]); + if(AST::is_a(*ann_assign.m_target)){ + AST::Attribute_t* a = AST::down_cast(ann_assign.m_target); + if(AST::is_a(*a->m_value)) { + AST::Name_t* n = AST::down_cast(a->m_value); + if(std::string(n->m_id) != obj_name) { + throw SemanticError("Object name doesn't matach", + x.m_body[i]->base.loc); + } + } + var_name = a->m_attr; + member_names.push_back(al, s2c(al, var_name)); + } else { + throw SemanticError("Only Attribute supported as target in " + "AnnAssign inside Class", x.m_body[i]->base.loc); + } + ASR::expr_t* init_expr = nullptr; + ASR::abiType abi = ASR::abiType::Source; + bool is_allocatable = false, is_const = false; + ASR::ttype_t *type = ast_expr_to_asr_type(ann_assign.m_annotation->base.loc, + *ann_assign.m_annotation, is_allocatable, is_const, true); + + ASR::storage_typeType storage_type = ASR::storage_typeType::Default; + create_add_variable_to_scope(var_name, type, + ann_assign.base.base.loc, abi, storage_type); + + if (ann_assign.m_value == nullptr) { + throw SemanticError("Missing an initialiser for the data member", + x.m_body[i]->base.loc); + } + this->visit_expr(*ann_assign.m_value); + if (tmp && ASR::is_a(*tmp)) { + ASR::expr_t* value = ASRUtils::EXPR(tmp); + ASR::ttype_t* underlying_type = type; + cast_helper(underlying_type, value, value->base.loc); + if (!ASRUtils::check_equal_type(underlying_type, ASRUtils::expr_type(value), true)) { + std::string ltype = ASRUtils::type_to_str_python(underlying_type); + std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(value)); + diag.add(diag::Diagnostic( + "Type mismatch in annotation-assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {ann_assign.m_target->base.loc, value->base.loc}) + }) + ); + throw SemanticAbort(); + } + init_expr = value; + } + ASR::symbol_t* var_sym = current_scope->resolve_symbol(var_name); + ASR::call_arg_t c_arg; + c_arg.loc = var_sym->base.loc; + c_arg.m_value = init_expr; + member_init.push_back(al, c_arg); + } + + } + + ASR::symbol_t* create_implicit_interface_function(Location &loc, ASR::FunctionType_t *func, std::string func_name) { SymbolTable *parent_scope = current_scope; current_scope = al.make_new(parent_scope); @@ -5128,6 +5195,26 @@ class BodyVisitor : public CommonVisitor { rt_vec.clear(); } + void handle_class_method(AST::FunctionDef_t& /*x*/){ + + } + + void handle_init_method(const AST::FunctionDef_t &x, + Vec& /*member_names*/ , Vec &/*member_init*/){ + for (size_t i = 0 ; i < x.n_body; i++){ + if (! AST::is_a(*x.m_body[i]) ){ + throw SemanticError("Only AnnAssign implemented in __init__ ", + x.m_body[i]->base.loc); + } + AST::AnnAssign_t ann_assign = *AST::down_cast(x.m_body[i]); + AST::ast_t* assgn_ast = AST::make_Assign_t(al,ann_assign.base.base.loc, + &ann_assign.m_target,1,ann_assign.m_value,nullptr); + AST::stmt_t* assgn = AST::down_cast(assgn_ast); + x.m_body[i] = assgn; + } + } + + void visit_FunctionDef(const AST::FunctionDef_t &x) { SymbolTable *old_scope = current_scope; ASR::symbol_t *t = current_scope->get_symbol(x.m_name); From 92e75ab5cc56e87e876950035e7df06587da7a64 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 7 Jul 2024 18:14:45 +0530 Subject: [PATCH 2/5] Removed unnecessary new FunctionDef --- src/lpython/semantics/python_ast_to_asr.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index fc22eb85d5..ee0ddc215c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4280,9 +4280,7 @@ class SymbolTableVisitor : public CommonVisitor { args_vec.push_back(al,*arg); } new_args->m_args = args_vec.p; - x = *AST::down_cast(AST::make_FunctionDef_t(al,x.base.base.loc, - x.m_name,*new_args,x.m_body,x.n_body,x.m_decorator_list,x.n_decorator_list, - x.m_returns,x.m_type_comment)); + x.m_args = *new_args; visit_FunctionDef(x); } From a311e1d881de108bf34e80e86f4700f3446283b3 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 7 Jul 2024 18:26:19 +0530 Subject: [PATCH 3/5] Removed class dependency on itself --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index ee0ddc215c..b025d10916 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2961,11 +2961,7 @@ class CommonVisitor : public AST::BaseVisitor { if (f_name == "__init__") { this->handle_init_method(*f, member_names, member_init); this->handle_class_method(*f); - // This seems hackish, as struct depends on itself - // We need to handle this later. - // Removing this throws a ASR verify error member_fn_names.push_back(al, f->m_name); - struct_dependencies.push_back(al, x.m_name); } else { this->handle_class_method(*f); member_fn_names.push_back(al, f->m_name); From d6f6b8fa3f786a9d09e41200fa73fb8c9fac36de Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Mon, 8 Jul 2024 18:05:52 +0530 Subject: [PATCH 4/5] Enabled function generation and parameterised init --- src/libasr/codegen/asr_to_llvm.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 0d92f7377b..e6574bdfc1 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -3054,6 +3054,30 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } } + void instantiate_methods(const ASR::Struct_t &x) { + SymbolTable *current_scope_copy = current_scope; + current_scope = x.m_symtab; + for ( auto &item : x.m_symtab->get_scope() ) { + if ( is_a(*item.second) ) { + ASR::Function_t *v = down_cast(item.second); + instantiate_function(*v); + } + } + current_scope = current_scope_copy; + } + + void visit_methods (const ASR::Struct_t &x) { + SymbolTable *current_scope_copy = current_scope; + current_scope = x.m_symtab; + for ( auto &item : x.m_symtab->get_scope() ) { + if ( is_a(*item.second) ) { + ASR::Function_t *v = down_cast(item.second); + visit_Function(*v); + } + } + current_scope = current_scope_copy; + } + void start_module_init_function_prototype(const ASR::Module_t &x) { uint32_t h = get_hash((ASR::asr_t*)&x); llvm::FunctionType *function_type = llvm::FunctionType::get( @@ -3095,6 +3119,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } else if (is_a(*item.second)) { ASR::EnumType_t *et = down_cast(item.second); visit_EnumType(*et); + } else if (is_a(*item.second)) { + ASR::Struct_t *st = down_cast(item.second); + instantiate_methods(*st); } } finish_module_init_function_prototype(x); @@ -4145,6 +4172,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor if (is_a(*item.second)) { ASR::Function_t *s = ASR::down_cast(item.second); visit_Function(*s); + } else if ( is_a(*item.second) ) { + ASR::Struct_t *st = down_cast(item.second); + visit_methods(*st); } } } From 1107001f482bdf6e15e532f22c256a60ef17bf19 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Wed, 10 Jul 2024 00:46:02 +0530 Subject: [PATCH 5/5] Reference --- src/lpython/semantics/python_ast_to_asr.cpp | 126 +++++++++++--------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b025d10916..06c833fd97 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1913,6 +1913,8 @@ class CommonVisitor : public AST::BaseVisitor { current_scope->add_symbol(import_name, import_struct_member); } return ASRUtils::TYPE(ASR::make_Union_t(al, attr_annotation->base.base.loc, import_struct_member)); + } else if () { + //intent inout } throw SemanticError("Only Name, Subscript, and Call supported for now in annotation of annotated assignment.", loc); @@ -2930,7 +2932,7 @@ class CommonVisitor : public AST::BaseVisitor { assign_asr_target = assign_asr_target_copy; } - virtual void handle_init_method(const AST::FunctionDef_t &/*x*/, + virtual void handle_init_method(AST::FunctionDef_t &/*x*/, Vec& /*member_names*/, Vec &/*member_init*/) = 0; void visit_ClassMembers(const AST::ClassDef_t& x, Vec& member_names, Vec& member_fn_names, @@ -2959,8 +2961,8 @@ class CommonVisitor : public AST::BaseVisitor { *f = AST::down_cast(x.m_body[i]); std::string f_name = f->m_name; if (f_name == "__init__") { - this->handle_init_method(*f, member_names, member_init); - this->handle_class_method(*f); + // this->handle_init_method(*f, member_names, member_init);//members + this->handle_class_method(*f);//Handle both in single fn member_fn_names.push_back(al, f->m_name); } else { this->handle_class_method(*f); @@ -3275,6 +3277,7 @@ class CommonVisitor : public AST::BaseVisitor { } } } else { + //SymTab self current_scope = al.make_new(parent_scope); Vec member_names; Vec member_fn_names; @@ -3290,9 +3293,6 @@ class CommonVisitor : public AST::BaseVisitor { "instead use the dataclass decorator ", x.base.base.loc); } - visit_ClassMembers(x, member_names, member_fn_names, - struct_dependencies, member_init, false, class_abi, true); - LCOMPILERS_ASSERT(member_init.size() == member_names.size()); ASR::symbol_t* class_sym = ASR::down_cast( ASR::make_Struct_t(al, x.base.base.loc, current_scope, x.m_name, struct_dependencies.p, struct_dependencies.size(), @@ -3300,12 +3300,19 @@ class CommonVisitor : public AST::BaseVisitor { member_fn_names.size(), class_abi, ASR::accessType::Public, false, false, member_init.p, member_init.size(), nullptr, nullptr)); - std::string self_name = "self"; - if ( current_scope->get_symbol(self_name) ) { - throw SemanticError("`self` cannot be used as a data member " - "for now", x.base.base.loc); - } parent_scope->add_symbol(x.m_name, class_sym); + visit_ClassMembers(x, member_names, member_fn_names, + struct_dependencies, member_init, false, class_abi, true); + LCOMPILERS_ASSERT(member_init.size() == member_names.size()); + ASR::Struct_t* struct_ = ASR::down_cast(class_sym); + struct_->m_dependencies; + struct_->m_members; + struct_->m_member_functions; + // std::string self_name = "self"; + // if ( current_scope->get_symbol(self_name) ) { + // throw SemanticError("`self` cannot be used as a data member " + // "for now", x.base.base.loc); + // } } current_scope = parent_scope; } @@ -4246,7 +4253,7 @@ class SymbolTableVisitor : public CommonVisitor { global_scope = nullptr; tmp = tmp0; } - + self:"coord" void handle_class_method(AST::FunctionDef_t& x){ AST::arguments_t old_args = x.m_args; AST::arguments_t* new_args = al.make_new(); @@ -4280,17 +4287,17 @@ class SymbolTableVisitor : public CommonVisitor { visit_FunctionDef(x); } - void handle_init_method(const AST::FunctionDef_t &x, + void handle_init_method(AST::FunctionDef_t &x, Vec& member_names, Vec &member_init){ if(x.n_decorator_list > 0) { throw SemanticError("Decorators for __init__ not implemented", x.base.base.loc); } std::string obj_name = "self"; - if ( std::string(x.m_args.m_args[0].m_arg) != obj_name) { - throw SemanticError("Only `self` can be used as object name for now", - x.base.base.loc); - } + // if ( std::string(x.m_args.m_args[0].m_arg) != obj_name) { + // throw SemanticError("Only `self` can be used as object name for now", + // x.base.base.loc); + // } for(size_t i = 0; i < x.n_body; i++) { std::string var_name; if (! AST::is_a(*x.m_body[i]) ){ @@ -4323,34 +4330,32 @@ class SymbolTableVisitor : public CommonVisitor { create_add_variable_to_scope(var_name, type, ann_assign.base.base.loc, abi, storage_type); - if (ann_assign.m_value == nullptr) { - throw SemanticError("Missing an initialiser for the data member", - x.m_body[i]->base.loc); - } - this->visit_expr(*ann_assign.m_value); - if (tmp && ASR::is_a(*tmp)) { - ASR::expr_t* value = ASRUtils::EXPR(tmp); - ASR::ttype_t* underlying_type = type; - cast_helper(underlying_type, value, value->base.loc); - if (!ASRUtils::check_equal_type(underlying_type, ASRUtils::expr_type(value), true)) { - std::string ltype = ASRUtils::type_to_str_python(underlying_type); - std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(value)); - diag.add(diag::Diagnostic( - "Type mismatch in annotation-assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {ann_assign.m_target->base.loc, value->base.loc}) - }) - ); - throw SemanticAbort(); + if (ann_assign.m_value != nullptr) { + this->visit_expr(*ann_assign.m_value); + if (tmp && ASR::is_a(*tmp)) { + ASR::expr_t* value = ASRUtils::EXPR(tmp); + ASR::ttype_t* underlying_type = type; + cast_helper(underlying_type, value, value->base.loc); + if (!ASRUtils::check_equal_type(underlying_type, ASRUtils::expr_type(value), true)) { + std::string ltype = ASRUtils::type_to_str_python(underlying_type); + std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(value)); + diag.add(diag::Diagnostic( + "Type mismatch in annotation-assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {ann_assign.m_target->base.loc, value->base.loc}) + }) + ); + throw SemanticAbort(); + } + init_expr = value; } - init_expr = value; } - ASR::symbol_t* var_sym = current_scope->resolve_symbol(var_name); - ASR::call_arg_t c_arg; - c_arg.loc = var_sym->base.loc; - c_arg.m_value = init_expr; - member_init.push_back(al, c_arg); + // ASR::symbol_t* var_sym = current_scope->resolve_symbol(var_name); + // ASR::call_arg_t c_arg; + // c_arg.loc = var_sym->base.loc; + // c_arg.m_value = init_expr; + // member_init.push_back(al, c_arg); } } @@ -4547,6 +4552,7 @@ class SymbolTableVisitor : public CommonVisitor { bool is_allocatable = false, is_const = false; size_t default_arg_index_start = x.m_args.n_args - x.m_args.n_defaults; for (size_t i=0; i { } - void handle_init_method(const AST::FunctionDef_t &x, - Vec& /*member_names*/ , Vec &/*member_init*/){ - for (size_t i = 0 ; i < x.n_body; i++){ - if (! AST::is_a(*x.m_body[i]) ){ - throw SemanticError("Only AnnAssign implemented in __init__ ", - x.m_body[i]->base.loc); - } - AST::AnnAssign_t ann_assign = *AST::down_cast(x.m_body[i]); - AST::ast_t* assgn_ast = AST::make_Assign_t(al,ann_assign.base.base.loc, - &ann_assign.m_target,1,ann_assign.m_value,nullptr); - AST::stmt_t* assgn = AST::down_cast(assgn_ast); - x.m_body[i] = assgn; - } - } + // void handle_init_method(AST::FunctionDef_t &x, + // Vec& /*member_names*/ , Vec &/*member_init*/){ + // Vec new_m_body; + // new_m_body.reserve(al, 1); + // for (size_t i = 0 ; i < x.n_body; i++){ + // if ( !AST::is_a(*x.m_body[i]) ){ + // throw SemanticError("Only AnnAssign implemented in __init__ ", + // x.m_body[i]->base.loc); + // } + // AST::AnnAssign_t ann_assign = *AST::down_cast(x.m_body[i]); + // if ( ann_assign.m_value != nullptr ) { + // AST::ast_t* assgn_ast = AST::make_Assign_t(al,ann_assign.base.base.loc, + // &ann_assign.m_target,1,ann_assign.m_value,nullptr); + // AST::stmt_t* assgn = AST::down_cast(assgn_ast); + // new_m_body.push_back(al, assgn); + // } + // } + // x.n_body = new_m_body.n; + // x.m_body = new_m_body.p; + // } void visit_FunctionDef(const AST::FunctionDef_t &x) {