From 2270fbb08552da139c35e6a6e314c671ec0bd702 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Thu, 1 Aug 2024 21:00:11 +0530 Subject: [PATCH 01/10] Temp --- src/libasr/pass/pass_utils.h | 123 +++++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 141 ++++++++++---------- 2 files changed, 192 insertions(+), 72 deletions(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index e88563f72e..98346fa672 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -570,6 +570,105 @@ namespace LCompilers { */ }; + ASR::symbol_t* get_struct_member(Allocator& al, ASR::symbol_t* struct_type_sym, std::string &call_name, + const Location &loc, SymbolTable* current_scope) { + ASR::Struct_t* struct_type = ASR::down_cast(struct_type_sym); + std::string struct_var_name = struct_type->m_name; + std::string struct_member_name = call_name; + ASR::symbol_t* struct_member = struct_type->m_symtab->resolve_symbol(struct_member_name); + ASR::symbol_t* struct_mem_asr_owner = ASRUtils::get_asr_owner(struct_member); + if( !struct_member || !struct_mem_asr_owner || + !ASR::is_a(*struct_mem_asr_owner) ) { + throw LCompilersException(struct_member_name + " not present in " + + struct_var_name + " dataclass"); + } + std::string import_name = struct_var_name + "_" + struct_member_name; + ASR::symbol_t* import_struct_member = current_scope->resolve_symbol(import_name); + bool import_from_struct = true; + if( import_struct_member ) { + if( ASR::is_a(*import_struct_member) ) { + ASR::ExternalSymbol_t* ext_sym = ASR::down_cast(import_struct_member); + if( ext_sym->m_external == struct_member && + std::string(ext_sym->m_module_name) == struct_var_name ) { + import_from_struct = false; + } + } + } + if( import_from_struct ) { + import_name = current_scope->get_unique_name(import_name, false); + import_struct_member = ASR::down_cast(ASR::make_ExternalSymbol_t(al, + loc, current_scope, s2c(al, import_name), + struct_member, s2c(al, struct_var_name), nullptr, 0, + s2c(al, struct_member_name), ASR::accessType::Public)); + current_scope->add_symbol(import_name, import_struct_member); + } + return import_struct_member; + } + + ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, Vec args, + std::string call_name, const Location &loc) { + + ASR::symbol_t *s_generic = nullptr, *stemp = s; + // Type map for generic functions + std::map subs; + std::map rt_subs; + // handling ExternalSymbol + s = ASRUtils::symbol_get_past_external(s); + + if (ASR::is_a(*s)) { + ASR::Function_t *func = ASR::down_cast(s); + if (args.size() < func->n_args) { + std::string missed_args_names =" "; + size_t missed_args_count =0; + for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ + ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); + if(var->m_symbolic_value == nullptr) { + missed_args_names+= "'" + std::string(var->m_name) + "' and "; + missed_args_count++; + } else { + ASR::call_arg_t call_arg; + call_arg.m_value = var->m_symbolic_value; + call_arg.loc = (var->m_symbolic_value->base).loc; + args.push_back(al,call_arg); + } + } + if(missed_args_count > 0){ + missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); + LCompilersException("Number of arguments does not match in the function call"); + + } + } + // Vec args_new; + // args_new.reserve(al, func->n_args); + // cast_args(al,func->m_args, func->n_args, args_new, args, true); + return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp, + s_generic, args.p, args.size(), nullptr, nullptr, false, false); + } else { + throw LCompilersException("Unsupported call type for " + call_name); + } + } + + void cast_args(Allocator& al, ASR::expr_t** m_args, size_t n_args, + Vec& call_args_vec, + Vec& args, + bool check_type_equality=true) { + LCOMPILERS_ASSERT(call_args_vec.reserve_called); + for (size_t i = 0; i < n_args; i++) { + ASR::call_arg_t c_arg; + c_arg.loc = args[i].loc; + c_arg.m_value = args[i].m_value; + // cast_helper(m_args[i], c_arg.m_value, true); + ASR::ttype_t* left_type = ASRUtils::expr_type(m_args[i]); + ASR::ttype_t* right_type = ASRUtils::expr_type(c_arg.m_value); + if( check_type_equality && !ASRUtils::check_equal_type(left_type, right_type) ) { + std::string ltype = ASRUtils::type_to_str_python(left_type); + std::string rtype = ASRUtils::type_to_str_python(right_type); + LCompilersException("Type mismatch in procedure call; the types must be compatible"); + } + call_args_vec.push_back(al, c_arg); + } + } + namespace ReplacerUtils { template void replace_StructConstructor(ASR::StructConstructor_t* x, @@ -578,6 +677,30 @@ namespace LCompilers { bool perform_cast=false, ASR::cast_kindType cast_kind=ASR::cast_kindType::IntegerToInteger, ASR::ttype_t* casted_type=nullptr) { + ASR::Struct_t* st = ASR::down_cast(x->m_dt_sym); + if ( st->n_member_functions > 0 ) { + remove_original_statement = true; + if ( !ASR::is_a(*(replacer->result_var)) ) { + throw LCompilersException("Expected a var here"); + } + ASR::Var_t* target = ASR::down_cast(replacer->result_var); + ASR::call_arg_t first_arg; + first_arg.loc = x->base.base.loc; first_arg.m_value = replacer->result_var; + Vec new_args; new_args.reserve(replacer->al,1); + new_args.push_back(replacer->al, first_arg); + for( size_t i = 0; i < x->n_args; i++ ) { + new_args.push_back(replacer->al, x->m_args[i]); + } + ASR::StructType_t* type = ASR::down_cast( + (ASR::down_cast(target->m_v))->m_type); + std::string call_name = "__init__"; + ASR::symbol_t* call_sym = get_struct_member(replacer->al,type->m_derived_type, call_name, + x->base.base.loc, replacer->current_scope); + result_vec->push_back(replacer->al, + ASRUtils::STMT(make_call_helper(replacer->al,call_sym, + new_args,call_name,x->base.base.loc))); + return; + } if( x->n_args == 0 ) { if( !inside_symtab ) { remove_original_statement = true; diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 24daf36684..8ecca75442 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1282,16 +1282,13 @@ class CommonVisitor : public AST::BaseVisitor { args, st, loc); } if ( st->n_member_functions > 0 ) { - // Empty struct constructor // Initializers handled in init proc call - Vecempty_args; - empty_args.reserve(al, 1); - for (size_t i = 0; i < st->n_members; i++) { - empty_args.push_back(al, st->m_initializers[i]); + if ( n_kwargs>0 ) { + throw SemanticError("Keyword args are not supported", loc); } ASR::ttype_t* der_type = ASRUtils::TYPE(ASRUtils::make_StructType_t_util(al, loc, stemp)); - return ASR::make_StructConstructor_t(al, loc, stemp, empty_args.p, - empty_args.size(), der_type, nullptr); + return ASR::make_StructConstructor_t(al, loc, stemp, args.p, + args.size(), der_type, nullptr); } if ( args.size() > 0 && args.size() > st->n_members ) { @@ -5609,26 +5606,26 @@ class BodyVisitor : public CommonVisitor { } tmp_vec.push_back(ASR::make_Assignment_t(al, x.base.base.loc, target, tmp_value, overloaded)); - if ( target->type == ASR::exprType::Var && - tmp_value->type == ASR::exprType::StructConstructor ) { - Vec new_args; new_args.reserve(al, 1); - ASR::call_arg_t self_arg; - self_arg.loc = x.base.base.loc; - ASR::symbol_t* st = ASR::down_cast(target)->m_v; - self_arg.m_value = target; - new_args.push_back(al,self_arg); - AST::Call_t* call = AST::down_cast(x.m_value); - if ( call->n_keywords>0 ) { - throw SemanticError("Kwargs not implemented yet", x.base.base.loc); - } - visit_expr_list(call->m_args, call->n_args, new_args); - ASR::symbol_t* der = ASR::down_cast( - ASR::down_cast(st)->m_type)->m_derived_type; - std::string call_name = "__init__"; - ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - tmp_vec.push_back(make_call_helper(al, call_sym, - current_scope, new_args, call_name, x.base.base.loc)); - } + // if ( target->type == ASR::exprType::Var && + // tmp_value->type == ASR::exprType::StructConstructor ) { + // Vec new_args; new_args.reserve(al, 1); + // ASR::call_arg_t self_arg; + // self_arg.loc = x.base.base.loc; + // ASR::symbol_t* st = ASR::down_cast(target)->m_v; + // self_arg.m_value = target; + // new_args.push_back(al,self_arg); + // AST::Call_t* call = AST::down_cast(x.m_value); + // if ( call->n_keywords>0 ) { + // throw SemanticError("Kwargs not implemented yet", x.base.base.loc); + // } + // visit_expr_list(call->m_args, call->n_args, new_args); + // ASR::symbol_t* der = ASR::down_cast( + // ASR::down_cast(st)->m_type)->m_derived_type; + // std::string call_name = "__init__"; + // ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); + // tmp_vec.push_back(make_call_helper(al, call_sym, + // current_scope, new_args, call_name, x.base.base.loc)); + // } } // to make sure that we add only those statements in tmp_vec tmp = nullptr; @@ -8085,25 +8082,25 @@ we will have to use something else. } else if( ASR::is_a(*st) ) { st = get_struct_member(st, call_name, loc); } else if ( ASR::is_a(*st)) { - ASR::Variable_t* var = ASR::down_cast(st); - if (ASR::is_a(*var->m_type) || - ASR::is_a(*var->m_type) ) { - //TODO: Correct Class and ClassType - // call to struct member function - // modifying args to pass the object as self - ASR::symbol_t* der = ASR::down_cast(var->m_type)->m_derived_type; - Vec new_args; new_args.reserve(al, args.n + 1); - ASR::call_arg_t self_arg; - self_arg.loc = args[0].loc; - self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, loc, st)); - new_args.push_back(al, self_arg); - for (size_t i=0; i(st); + // if (ASR::is_a(*var->m_type) || + // ASR::is_a(*var->m_type) ) { + // //TODO: Correct Class and ClassType + // // call to struct member function + // // modifying args to pass the object as self + // ASR::symbol_t* der = ASR::down_cast(var->m_type)->m_derived_type; + // Vec new_args; new_args.reserve(al, args.n + 1); + // ASR::call_arg_t self_arg; + // self_arg.loc = args[0].loc; + // self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, loc, st)); + // new_args.push_back(al, self_arg); + // for (size_t i=0; iresolve_symbol(mod_name); Vec eles; @@ -8124,7 +8121,7 @@ we will have to use something else. } handle_builtin_attribute(se, at->m_attr, loc, eles); return; - } + // } } } tmp = make_call_helper(al, st, current_scope, args, call_name, loc); @@ -8261,31 +8258,31 @@ we will have to use something else. } handle_builtin_attribute(subscript_expr, at->m_attr, loc, eles); return; - } else if ( AST::is_a(*at->m_value) ) { - AST::Attribute_t* at_m_value = AST::down_cast(at->m_value); - visit_Attribute(*at_m_value); - ASR::expr_t* e = ASRUtils::EXPR(tmp); - if ( !ASR::is_a(*e) ) { - throw SemanticError("Expected a class variable here", loc); - } - if ( !ASR::is_a(*ASRUtils::expr_type(e)) ) { - throw SemanticError("Only Classes supported in nested attribute call", loc); - } - ASR::StructType_t* der = ASR::down_cast(ASRUtils::expr_type(e)); - ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type); - std::string call_name = at->m_attr; - - Vec new_args; new_args.reserve(al, args.n + 1); - ASR::call_arg_t self_arg; - self_arg.loc = args[0].loc; - self_arg.m_value = e; - new_args.push_back(al, self_arg); - for (size_t i=0; i(*at->m_value) ) { + // AST::Attribute_t* at_m_value = AST::down_cast(at->m_value); + // visit_Attribute(*at_m_value); + // ASR::expr_t* e = ASRUtils::EXPR(tmp); + // if ( !ASR::is_a(*e) ) { + // throw SemanticError("Expected a class variable here", loc); + // } + // if ( !ASR::is_a(*ASRUtils::expr_type(e)) ) { + // throw SemanticError("Only Classes supported in nested attribute call", loc); + // } + // ASR::StructType_t* der = ASR::down_cast(ASRUtils::expr_type(e)); + // ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type); + // std::string call_name = at->m_attr; + + // Vec new_args; new_args.reserve(al, args.n + 1); + // ASR::call_arg_t self_arg; + // self_arg.loc = args[0].loc; + // self_arg.m_value = e; + // new_args.push_back(al, self_arg); + // for (size_t i=0; i Date: Fri, 2 Aug 2024 18:20:39 +0530 Subject: [PATCH 02/10] Refactor --- src/libasr/pass/pass_utils.cpp | 97 ++++++++++++++ src/libasr/pass/pass_utils.h | 97 +------------- src/lpython/semantics/python_ast_to_asr.cpp | 140 ++++++++++---------- 3 files changed, 169 insertions(+), 165 deletions(-) diff --git a/src/libasr/pass/pass_utils.cpp b/src/libasr/pass/pass_utils.cpp index 35b99f5c58..9b08e7566e 100644 --- a/src/libasr/pass/pass_utils.cpp +++ b/src/libasr/pass/pass_utils.cpp @@ -1247,7 +1247,104 @@ namespace LCompilers { } } } + ASR::symbol_t* get_struct_member(Allocator& al, ASR::symbol_t* struct_type_sym, std::string &call_name, + const Location &loc, SymbolTable* current_scope) { + ASR::Struct_t* struct_type = ASR::down_cast(struct_type_sym); + std::string struct_var_name = struct_type->m_name; + std::string struct_member_name = call_name; + ASR::symbol_t* struct_member = struct_type->m_symtab->resolve_symbol(struct_member_name); + ASR::symbol_t* struct_mem_asr_owner = ASRUtils::get_asr_owner(struct_member); + if( !struct_member || !struct_mem_asr_owner || + !ASR::is_a(*struct_mem_asr_owner) ) { + throw LCompilersException(struct_member_name + " not present in " + + struct_var_name + " dataclass"); + } + std::string import_name = struct_var_name + "_" + struct_member_name; + ASR::symbol_t* import_struct_member = current_scope->resolve_symbol(import_name); + bool import_from_struct = true; + if( import_struct_member ) { + if( ASR::is_a(*import_struct_member) ) { + ASR::ExternalSymbol_t* ext_sym = ASR::down_cast(import_struct_member); + if( ext_sym->m_external == struct_member && + std::string(ext_sym->m_module_name) == struct_var_name ) { + import_from_struct = false; + } + } + } + if( import_from_struct ) { + import_name = current_scope->get_unique_name(import_name, false); + import_struct_member = ASR::down_cast(ASR::make_ExternalSymbol_t(al, + loc, current_scope, s2c(al, import_name), + struct_member, s2c(al, struct_var_name), nullptr, 0, + s2c(al, struct_member_name), ASR::accessType::Public)); + current_scope->add_symbol(import_name, import_struct_member); + } + return import_struct_member; + } + + ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, Vec args, + std::string call_name, const Location &loc) { + + ASR::symbol_t *s_generic = nullptr, *stemp = s; + // Type map for generic functions + std::map subs; + std::map rt_subs; + // handling ExternalSymbol + s = ASRUtils::symbol_get_past_external(s); + + if (ASR::is_a(*s)) { + ASR::Function_t *func = ASR::down_cast(s); + if (args.size() < func->n_args) { + std::string missed_args_names =" "; + size_t missed_args_count =0; + for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ + ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); + if(var->m_symbolic_value == nullptr) { + missed_args_names+= "'" + std::string(var->m_name) + "' and "; + missed_args_count++; + } else { + ASR::call_arg_t call_arg; + call_arg.m_value = var->m_symbolic_value; + call_arg.loc = (var->m_symbolic_value->base).loc; + args.push_back(al,call_arg); + } + } + if(missed_args_count > 0){ + missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); + LCompilersException("Number of arguments does not match in the function call"); + + } + } + // Vec args_new; + // args_new.reserve(al, func->n_args); + // cast_args(al,func->m_args, func->n_args, args_new, args, true); + return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp, + s_generic, args.p, args.size(), nullptr, nullptr, false, false); + } else { + throw LCompilersException("Unsupported call type for " + call_name); + } + } + // void cast_args(Allocator& al, ASR::expr_t** m_args, size_t n_args, + // Vec& call_args_vec, + // Vec& args, + // bool check_type_equality=true) { + // LCOMPILERS_ASSERT(call_args_vec.reserve_called); + // for (size_t i = 0; i < n_args; i++) { + // ASR::call_arg_t c_arg; + // c_arg.loc = args[i].loc; + // c_arg.m_value = args[i].m_value; + // // cast_helper(m_args[i], c_arg.m_value, true); + // ASR::ttype_t* left_type = ASRUtils::expr_type(m_args[i]); + // ASR::ttype_t* right_type = ASRUtils::expr_type(c_arg.m_value); + // if( check_type_equality && !ASRUtils::check_equal_type(left_type, right_type) ) { + // std::string ltype = ASRUtils::type_to_str_python(left_type); + // std::string rtype = ASRUtils::type_to_str_python(right_type); + // LCompilersException("Type mismatch in procedure call; the types must be compatible"); + // } + // call_args_vec.push_back(al, c_arg); + // } + // } } // namespace PassUtils } // namespace LCompilers diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 98346fa672..2dea9219ce 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -571,103 +571,10 @@ namespace LCompilers { }; ASR::symbol_t* get_struct_member(Allocator& al, ASR::symbol_t* struct_type_sym, std::string &call_name, - const Location &loc, SymbolTable* current_scope) { - ASR::Struct_t* struct_type = ASR::down_cast(struct_type_sym); - std::string struct_var_name = struct_type->m_name; - std::string struct_member_name = call_name; - ASR::symbol_t* struct_member = struct_type->m_symtab->resolve_symbol(struct_member_name); - ASR::symbol_t* struct_mem_asr_owner = ASRUtils::get_asr_owner(struct_member); - if( !struct_member || !struct_mem_asr_owner || - !ASR::is_a(*struct_mem_asr_owner) ) { - throw LCompilersException(struct_member_name + " not present in " + - struct_var_name + " dataclass"); - } - std::string import_name = struct_var_name + "_" + struct_member_name; - ASR::symbol_t* import_struct_member = current_scope->resolve_symbol(import_name); - bool import_from_struct = true; - if( import_struct_member ) { - if( ASR::is_a(*import_struct_member) ) { - ASR::ExternalSymbol_t* ext_sym = ASR::down_cast(import_struct_member); - if( ext_sym->m_external == struct_member && - std::string(ext_sym->m_module_name) == struct_var_name ) { - import_from_struct = false; - } - } - } - if( import_from_struct ) { - import_name = current_scope->get_unique_name(import_name, false); - import_struct_member = ASR::down_cast(ASR::make_ExternalSymbol_t(al, - loc, current_scope, s2c(al, import_name), - struct_member, s2c(al, struct_var_name), nullptr, 0, - s2c(al, struct_member_name), ASR::accessType::Public)); - current_scope->add_symbol(import_name, import_struct_member); - } - return import_struct_member; - } + const Location &loc, SymbolTable* current_scope); ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, Vec args, - std::string call_name, const Location &loc) { - - ASR::symbol_t *s_generic = nullptr, *stemp = s; - // Type map for generic functions - std::map subs; - std::map rt_subs; - // handling ExternalSymbol - s = ASRUtils::symbol_get_past_external(s); - - if (ASR::is_a(*s)) { - ASR::Function_t *func = ASR::down_cast(s); - if (args.size() < func->n_args) { - std::string missed_args_names =" "; - size_t missed_args_count =0; - for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ - ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); - if(var->m_symbolic_value == nullptr) { - missed_args_names+= "'" + std::string(var->m_name) + "' and "; - missed_args_count++; - } else { - ASR::call_arg_t call_arg; - call_arg.m_value = var->m_symbolic_value; - call_arg.loc = (var->m_symbolic_value->base).loc; - args.push_back(al,call_arg); - } - } - if(missed_args_count > 0){ - missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); - LCompilersException("Number of arguments does not match in the function call"); - - } - } - // Vec args_new; - // args_new.reserve(al, func->n_args); - // cast_args(al,func->m_args, func->n_args, args_new, args, true); - return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp, - s_generic, args.p, args.size(), nullptr, nullptr, false, false); - } else { - throw LCompilersException("Unsupported call type for " + call_name); - } - } - - void cast_args(Allocator& al, ASR::expr_t** m_args, size_t n_args, - Vec& call_args_vec, - Vec& args, - bool check_type_equality=true) { - LCOMPILERS_ASSERT(call_args_vec.reserve_called); - for (size_t i = 0; i < n_args; i++) { - ASR::call_arg_t c_arg; - c_arg.loc = args[i].loc; - c_arg.m_value = args[i].m_value; - // cast_helper(m_args[i], c_arg.m_value, true); - ASR::ttype_t* left_type = ASRUtils::expr_type(m_args[i]); - ASR::ttype_t* right_type = ASRUtils::expr_type(c_arg.m_value); - if( check_type_equality && !ASRUtils::check_equal_type(left_type, right_type) ) { - std::string ltype = ASRUtils::type_to_str_python(left_type); - std::string rtype = ASRUtils::type_to_str_python(right_type); - LCompilersException("Type mismatch in procedure call; the types must be compatible"); - } - call_args_vec.push_back(al, c_arg); - } - } + std::string call_name, const Location &loc); namespace ReplacerUtils { template diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 8ecca75442..8f9bb9b1ab 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5301,31 +5301,31 @@ class BodyVisitor : public CommonVisitor { } ASR::expr_t *init_expr = nullptr; visit_AnnAssignUtil(x, var_name, init_expr); - ASR::symbol_t* sym = current_scope->get_symbol(var_name); - if ( sym && ASR::is_a(*sym) ) { - ASR::Variable_t* var = ASR::down_cast(sym); - if ( ASR::is_a(*(var->m_type)) && - !ASR::down_cast((var->m_type))->m_is_cstruct ) { - if ( !ASR::is_a(*init_expr) ) { - throw SemanticError("Only Class constructor is allowed in the object assignment for now", x.base.base.loc); - } - AST::Call_t* call = AST::down_cast(x.m_value); - if ( call->n_keywords>0 ) { - throw SemanticError("Kwargs not implemented yet", x.base.base.loc); - } - Vec args; - args.reserve(al, call->n_args + 1); - ASR::call_arg_t self_arg; - self_arg.loc = x.base.base.loc; - self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, sym)); - args.push_back(al, self_arg); - visit_expr_list(call->m_args, call->n_args, args); - ASR::symbol_t* der = ASR::down_cast((var->m_type))->m_derived_type; - std::string call_name = "__init__"; - ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - tmp = make_call_helper(al, call_sym, current_scope, args, call_name, x.base.base.loc); - } - } + // ASR::symbol_t* sym = current_scope->get_symbol(var_name); + // if ( sym && ASR::is_a(*sym) ) { + // ASR::Variable_t* var = ASR::down_cast(sym); + // if ( ASR::is_a(*(var->m_type)) && + // !ASR::down_cast((var->m_type))->m_is_cstruct ) { + // if ( !ASR::is_a(*init_expr) ) { + // throw SemanticError("Only Class constructor is allowed in the object assignment for now", x.base.base.loc); + // } + // AST::Call_t* call = AST::down_cast(x.m_value); + // if ( call->n_keywords>0 ) { + // throw SemanticError("Kwargs not implemented yet", x.base.base.loc); + // } + // Vec args; + // args.reserve(al, call->n_args + 1); + // ASR::call_arg_t self_arg; + // self_arg.loc = x.base.base.loc; + // self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, sym)); + // args.push_back(al, self_arg); + // visit_expr_list(call->m_args, call->n_args, args); + // ASR::symbol_t* der = ASR::down_cast((var->m_type))->m_derived_type; + // std::string call_name = "__init__"; + // ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); + // tmp = make_call_helper(al, call_sym, current_scope, args, call_name, x.base.base.loc); + // } + // } } void visit_Delete(const AST::Delete_t &x) { @@ -8082,25 +8082,25 @@ we will have to use something else. } else if( ASR::is_a(*st) ) { st = get_struct_member(st, call_name, loc); } else if ( ASR::is_a(*st)) { - // ASR::Variable_t* var = ASR::down_cast(st); - // if (ASR::is_a(*var->m_type) || - // ASR::is_a(*var->m_type) ) { - // //TODO: Correct Class and ClassType - // // call to struct member function - // // modifying args to pass the object as self - // ASR::symbol_t* der = ASR::down_cast(var->m_type)->m_derived_type; - // Vec new_args; new_args.reserve(al, args.n + 1); - // ASR::call_arg_t self_arg; - // self_arg.loc = args[0].loc; - // self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, loc, st)); - // new_args.push_back(al, self_arg); - // for (size_t i=0; i(st); + if (ASR::is_a(*var->m_type) || + ASR::is_a(*var->m_type) ) { + //TODO: Correct Class and ClassType + // call to struct member function + // modifying args to pass the object as self + ASR::symbol_t* der = ASR::down_cast(var->m_type)->m_derived_type; + Vec new_args; new_args.reserve(al, args.n + 1); + ASR::call_arg_t self_arg; + self_arg.loc = args[0].loc; + self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, loc, st)); + new_args.push_back(al, self_arg); + for (size_t i=0; iresolve_symbol(mod_name); Vec eles; @@ -8121,7 +8121,7 @@ we will have to use something else. } handle_builtin_attribute(se, at->m_attr, loc, eles); return; - // } + } } } tmp = make_call_helper(al, st, current_scope, args, call_name, loc); @@ -8258,31 +8258,31 @@ we will have to use something else. } handle_builtin_attribute(subscript_expr, at->m_attr, loc, eles); return; - // } else if ( AST::is_a(*at->m_value) ) { - // AST::Attribute_t* at_m_value = AST::down_cast(at->m_value); - // visit_Attribute(*at_m_value); - // ASR::expr_t* e = ASRUtils::EXPR(tmp); - // if ( !ASR::is_a(*e) ) { - // throw SemanticError("Expected a class variable here", loc); - // } - // if ( !ASR::is_a(*ASRUtils::expr_type(e)) ) { - // throw SemanticError("Only Classes supported in nested attribute call", loc); - // } - // ASR::StructType_t* der = ASR::down_cast(ASRUtils::expr_type(e)); - // ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type); - // std::string call_name = at->m_attr; - - // Vec new_args; new_args.reserve(al, args.n + 1); - // ASR::call_arg_t self_arg; - // self_arg.loc = args[0].loc; - // self_arg.m_value = e; - // new_args.push_back(al, self_arg); - // for (size_t i=0; i(*at->m_value) ) { + AST::Attribute_t* at_m_value = AST::down_cast(at->m_value); + visit_Attribute(*at_m_value); + ASR::expr_t* e = ASRUtils::EXPR(tmp); + if ( !ASR::is_a(*e) ) { + throw SemanticError("Expected a class variable here", loc); + } + if ( !ASR::is_a(*ASRUtils::expr_type(e)) ) { + throw SemanticError("Only Classes supported in nested attribute call", loc); + } + ASR::StructType_t* der = ASR::down_cast(ASRUtils::expr_type(e)); + ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type); + std::string call_name = at->m_attr; + + Vec new_args; new_args.reserve(al, args.n + 1); + ASR::call_arg_t self_arg; + self_arg.loc = args[0].loc; + self_arg.m_value = e; + new_args.push_back(al, self_arg); + for (size_t i=0; i Date: Fri, 2 Aug 2024 19:06:27 +0530 Subject: [PATCH 03/10] Completed the refactor --- src/libasr/pass/pass_utils.cpp | 24 ------ src/libasr/pass/pass_utils.h | 46 +++-------- src/lpython/semantics/python_ast_to_asr.cpp | 90 ++++++++++----------- 3 files changed, 56 insertions(+), 104 deletions(-) diff --git a/src/libasr/pass/pass_utils.cpp b/src/libasr/pass/pass_utils.cpp index 9b08e7566e..0451fa12fe 100644 --- a/src/libasr/pass/pass_utils.cpp +++ b/src/libasr/pass/pass_utils.cpp @@ -1315,36 +1315,12 @@ namespace LCompilers { } } - // Vec args_new; - // args_new.reserve(al, func->n_args); - // cast_args(al,func->m_args, func->n_args, args_new, args, true); return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp, s_generic, args.p, args.size(), nullptr, nullptr, false, false); } else { throw LCompilersException("Unsupported call type for " + call_name); } } - - // void cast_args(Allocator& al, ASR::expr_t** m_args, size_t n_args, - // Vec& call_args_vec, - // Vec& args, - // bool check_type_equality=true) { - // LCOMPILERS_ASSERT(call_args_vec.reserve_called); - // for (size_t i = 0; i < n_args; i++) { - // ASR::call_arg_t c_arg; - // c_arg.loc = args[i].loc; - // c_arg.m_value = args[i].m_value; - // // cast_helper(m_args[i], c_arg.m_value, true); - // ASR::ttype_t* left_type = ASRUtils::expr_type(m_args[i]); - // ASR::ttype_t* right_type = ASRUtils::expr_type(c_arg.m_value); - // if( check_type_equality && !ASRUtils::check_equal_type(left_type, right_type) ) { - // std::string ltype = ASRUtils::type_to_str_python(left_type); - // std::string rtype = ASRUtils::type_to_str_python(right_type); - // LCompilersException("Type mismatch in procedure call; the types must be compatible"); - // } - // call_args_vec.push_back(al, c_arg); - // } - // } } // namespace PassUtils } // namespace LCompilers diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 2dea9219ce..5d83ca9dd9 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -584,30 +584,6 @@ namespace LCompilers { bool perform_cast=false, ASR::cast_kindType cast_kind=ASR::cast_kindType::IntegerToInteger, ASR::ttype_t* casted_type=nullptr) { - ASR::Struct_t* st = ASR::down_cast(x->m_dt_sym); - if ( st->n_member_functions > 0 ) { - remove_original_statement = true; - if ( !ASR::is_a(*(replacer->result_var)) ) { - throw LCompilersException("Expected a var here"); - } - ASR::Var_t* target = ASR::down_cast(replacer->result_var); - ASR::call_arg_t first_arg; - first_arg.loc = x->base.base.loc; first_arg.m_value = replacer->result_var; - Vec new_args; new_args.reserve(replacer->al,1); - new_args.push_back(replacer->al, first_arg); - for( size_t i = 0; i < x->n_args; i++ ) { - new_args.push_back(replacer->al, x->m_args[i]); - } - ASR::StructType_t* type = ASR::down_cast( - (ASR::down_cast(target->m_v))->m_type); - std::string call_name = "__init__"; - ASR::symbol_t* call_sym = get_struct_member(replacer->al,type->m_derived_type, call_name, - x->base.base.loc, replacer->current_scope); - result_vec->push_back(replacer->al, - ASRUtils::STMT(make_call_helper(replacer->al,call_sym, - new_args,call_name,x->base.base.loc))); - return; - } if( x->n_args == 0 ) { if( !inside_symtab ) { remove_original_statement = true; @@ -628,22 +604,22 @@ namespace LCompilers { } std::deque constructor_arg_syms; - ASR::StructType_t* dt_der = ASR::down_cast(x->m_type); - ASR::Struct_t* dt_dertype = ASR::down_cast( - ASRUtils::symbol_get_past_external(dt_der->m_derived_type)); - while( dt_dertype ) { - for( int i = (int) dt_dertype->n_members - 1; i >= 0; i-- ) { + ASR::StructType_t* dt_dertype = ASR::down_cast(x->m_type); + ASR::Struct_t* dt_der = ASR::down_cast( + ASRUtils::symbol_get_past_external(dt_dertype->m_derived_type)); + while( dt_der ) { + for( int i = (int) dt_der->n_members - 1; i >= 0; i-- ) { constructor_arg_syms.push_front( - dt_dertype->m_symtab->get_symbol( - dt_dertype->m_members[i])); + dt_der->m_symtab->get_symbol( + dt_der->m_members[i])); } - if( dt_dertype->m_parent != nullptr ) { + if( dt_der->m_parent != nullptr ) { ASR::symbol_t* dt_der_sym = ASRUtils::symbol_get_past_external( - dt_dertype->m_parent); + dt_der->m_parent); LCOMPILERS_ASSERT(ASR::is_a(*dt_der_sym)); - dt_dertype = ASR::down_cast(dt_der_sym); + dt_der = ASR::down_cast(dt_der_sym); } else { - dt_dertype = nullptr; + dt_der = nullptr; } } LCOMPILERS_ASSERT(constructor_arg_syms.size() == x->n_args); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 8f9bb9b1ab..07e51a924a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5301,31 +5301,31 @@ class BodyVisitor : public CommonVisitor { } ASR::expr_t *init_expr = nullptr; visit_AnnAssignUtil(x, var_name, init_expr); - // ASR::symbol_t* sym = current_scope->get_symbol(var_name); - // if ( sym && ASR::is_a(*sym) ) { - // ASR::Variable_t* var = ASR::down_cast(sym); - // if ( ASR::is_a(*(var->m_type)) && - // !ASR::down_cast((var->m_type))->m_is_cstruct ) { - // if ( !ASR::is_a(*init_expr) ) { - // throw SemanticError("Only Class constructor is allowed in the object assignment for now", x.base.base.loc); - // } - // AST::Call_t* call = AST::down_cast(x.m_value); - // if ( call->n_keywords>0 ) { - // throw SemanticError("Kwargs not implemented yet", x.base.base.loc); - // } - // Vec args; - // args.reserve(al, call->n_args + 1); - // ASR::call_arg_t self_arg; - // self_arg.loc = x.base.base.loc; - // self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, sym)); - // args.push_back(al, self_arg); - // visit_expr_list(call->m_args, call->n_args, args); - // ASR::symbol_t* der = ASR::down_cast((var->m_type))->m_derived_type; - // std::string call_name = "__init__"; - // ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - // tmp = make_call_helper(al, call_sym, current_scope, args, call_name, x.base.base.loc); - // } - // } + ASR::symbol_t* sym = current_scope->get_symbol(var_name); + if ( sym && ASR::is_a(*sym) ) { + ASR::Variable_t* var = ASR::down_cast(sym); + if ( ASR::is_a(*(var->m_type)) && + !ASR::down_cast((var->m_type))->m_is_cstruct ) { + if ( !ASR::is_a(*init_expr) ) { + throw SemanticError("Only Class constructor is allowed in the object assignment for now", x.base.base.loc); + } + AST::Call_t* call = AST::down_cast(x.m_value); + if ( call->n_keywords>0 ) { + throw SemanticError("Kwargs not implemented yet", x.base.base.loc); + } + Vec args; + args.reserve(al, call->n_args + 1); + ASR::call_arg_t self_arg; + self_arg.loc = x.base.base.loc; + self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, sym)); + args.push_back(al, self_arg); + visit_expr_list(call->m_args, call->n_args, args); + ASR::symbol_t* der = ASR::down_cast((var->m_type))->m_derived_type; + std::string call_name = "__init__"; + ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); + tmp = make_call_helper(al, call_sym, current_scope, args, call_name, x.base.base.loc); + } + } } void visit_Delete(const AST::Delete_t &x) { @@ -5606,26 +5606,26 @@ class BodyVisitor : public CommonVisitor { } tmp_vec.push_back(ASR::make_Assignment_t(al, x.base.base.loc, target, tmp_value, overloaded)); - // if ( target->type == ASR::exprType::Var && - // tmp_value->type == ASR::exprType::StructConstructor ) { - // Vec new_args; new_args.reserve(al, 1); - // ASR::call_arg_t self_arg; - // self_arg.loc = x.base.base.loc; - // ASR::symbol_t* st = ASR::down_cast(target)->m_v; - // self_arg.m_value = target; - // new_args.push_back(al,self_arg); - // AST::Call_t* call = AST::down_cast(x.m_value); - // if ( call->n_keywords>0 ) { - // throw SemanticError("Kwargs not implemented yet", x.base.base.loc); - // } - // visit_expr_list(call->m_args, call->n_args, new_args); - // ASR::symbol_t* der = ASR::down_cast( - // ASR::down_cast(st)->m_type)->m_derived_type; - // std::string call_name = "__init__"; - // ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - // tmp_vec.push_back(make_call_helper(al, call_sym, - // current_scope, new_args, call_name, x.base.base.loc)); - // } + if ( target->type == ASR::exprType::Var && + tmp_value->type == ASR::exprType::StructConstructor ) { + Vec new_args; new_args.reserve(al, 1); + ASR::call_arg_t self_arg; + self_arg.loc = x.base.base.loc; + ASR::symbol_t* st = ASR::down_cast(target)->m_v; + self_arg.m_value = target; + new_args.push_back(al,self_arg); + AST::Call_t* call = AST::down_cast(x.m_value); + if ( call->n_keywords>0 ) { + throw SemanticError("Kwargs not implemented yet", x.base.base.loc); + } + visit_expr_list(call->m_args, call->n_args, new_args); + ASR::symbol_t* der = ASR::down_cast( + ASR::down_cast(st)->m_type)->m_derived_type; + std::string call_name = "__init__"; + ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); + tmp_vec.push_back(make_call_helper(al, call_sym, + current_scope, new_args, call_name, x.base.base.loc)); + } } // to make sure that we add only those statements in tmp_vec tmp = nullptr; From c727045ea53ff6911a058cbc3b3b9b25c0854994 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Fri, 2 Aug 2024 19:21:35 +0530 Subject: [PATCH 04/10] Rebased --- src/libasr/pass/pass_utils.h | 26 +++++++++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 18 -------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 5d83ca9dd9..85e8fc5446 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -584,6 +584,32 @@ namespace LCompilers { bool perform_cast=false, ASR::cast_kindType cast_kind=ASR::cast_kindType::IntegerToInteger, ASR::ttype_t* casted_type=nullptr) { + if ( x->m_dt_sym && ASR::is_a(*(x->m_dt_sym)) ) { + ASR::Struct_t* st = ASR::down_cast(x->m_dt_sym); + if ( st->n_member_functions > 0 ) { + remove_original_statement = true; + if ( !ASR::is_a(*(replacer->result_var)) ) { + throw LCompilersException("Expected a var here"); + } + ASR::Var_t* target = ASR::down_cast(replacer->result_var); + ASR::call_arg_t first_arg; + first_arg.loc = x->base.base.loc; first_arg.m_value = replacer->result_var; + Vec new_args; new_args.reserve(replacer->al,1); + new_args.push_back(replacer->al, first_arg); + for( size_t i = 0; i < x->n_args; i++ ) { + new_args.push_back(replacer->al, x->m_args[i]); + } + ASR::StructType_t* type = ASR::down_cast( + (ASR::down_cast(target->m_v))->m_type); + std::string call_name = "__init__"; + ASR::symbol_t* call_sym = get_struct_member(replacer->al,type->m_derived_type, call_name, + x->base.base.loc, replacer->current_scope); + result_vec->push_back(replacer->al, + ASRUtils::STMT(make_call_helper(replacer->al,call_sym, + new_args,call_name,x->base.base.loc))); + return; + } + } if( x->n_args == 0 ) { if( !inside_symtab ) { remove_original_statement = true; diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 07e51a924a..1621ff8a29 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5313,17 +5313,6 @@ class BodyVisitor : public CommonVisitor { if ( call->n_keywords>0 ) { throw SemanticError("Kwargs not implemented yet", x.base.base.loc); } - Vec args; - args.reserve(al, call->n_args + 1); - ASR::call_arg_t self_arg; - self_arg.loc = x.base.base.loc; - self_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, sym)); - args.push_back(al, self_arg); - visit_expr_list(call->m_args, call->n_args, args); - ASR::symbol_t* der = ASR::down_cast((var->m_type))->m_derived_type; - std::string call_name = "__init__"; - ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - tmp = make_call_helper(al, call_sym, current_scope, args, call_name, x.base.base.loc); } } } @@ -5618,13 +5607,6 @@ class BodyVisitor : public CommonVisitor { if ( call->n_keywords>0 ) { throw SemanticError("Kwargs not implemented yet", x.base.base.loc); } - visit_expr_list(call->m_args, call->n_args, new_args); - ASR::symbol_t* der = ASR::down_cast( - ASR::down_cast(st)->m_type)->m_derived_type; - std::string call_name = "__init__"; - ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc); - tmp_vec.push_back(make_call_helper(al, call_sym, - current_scope, new_args, call_name, x.base.base.loc)); } } // to make sure that we add only those statements in tmp_vec From f5e15548497ea99561704ddcb68f18fc2a268a4a Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Fri, 2 Aug 2024 19:25:31 +0530 Subject: [PATCH 05/10] refactor --- src/lpython/semantics/python_ast_to_asr.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1621ff8a29..fad8029a00 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5597,12 +5597,6 @@ class BodyVisitor : public CommonVisitor { overloaded)); if ( target->type == ASR::exprType::Var && tmp_value->type == ASR::exprType::StructConstructor ) { - Vec new_args; new_args.reserve(al, 1); - ASR::call_arg_t self_arg; - self_arg.loc = x.base.base.loc; - ASR::symbol_t* st = ASR::down_cast(target)->m_v; - self_arg.m_value = target; - new_args.push_back(al,self_arg); AST::Call_t* call = AST::down_cast(x.m_value); if ( call->n_keywords>0 ) { throw SemanticError("Kwargs not implemented yet", x.base.base.loc); From 459a75cdeb61d684dd5319c6bc42bc2fb2776384 Mon Sep 17 00:00:00 2001 From: tanay-man <93091118+tanay-man@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:19:46 +0530 Subject: [PATCH 06/10] Update src/libasr/pass/pass_utils.h Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> --- src/libasr/pass/pass_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 85e8fc5446..a7dd5a5a1b 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -594,7 +594,7 @@ namespace LCompilers { ASR::Var_t* target = ASR::down_cast(replacer->result_var); ASR::call_arg_t first_arg; first_arg.loc = x->base.base.loc; first_arg.m_value = replacer->result_var; - Vec new_args; new_args.reserve(replacer->al,1); + Vec new_args; new_args.reserve(replacer->al,x->n_args+1); new_args.push_back(replacer->al, first_arg); for( size_t i = 0; i < x->n_args; i++ ) { new_args.push_back(replacer->al, x->m_args[i]); From 316166e2e8c97066c62d4a3ee2fc35a35d98c7d0 Mon Sep 17 00:00:00 2001 From: tanay-man <93091118+tanay-man@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:19:54 +0530 Subject: [PATCH 07/10] Update src/libasr/pass/pass_utils.h Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> --- src/libasr/pass/pass_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index a7dd5a5a1b..10f0edce0c 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -584,7 +584,7 @@ namespace LCompilers { bool perform_cast=false, ASR::cast_kindType cast_kind=ASR::cast_kindType::IntegerToInteger, ASR::ttype_t* casted_type=nullptr) { - if ( x->m_dt_sym && ASR::is_a(*(x->m_dt_sym)) ) { + if ( ASR::is_a(*(x->m_dt_sym)) ) { ASR::Struct_t* st = ASR::down_cast(x->m_dt_sym); if ( st->n_member_functions > 0 ) { remove_original_statement = true; From 1fe5ace23a721bd6d15990ca0c6a7f4c24353bfe Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:24:59 +0530 Subject: [PATCH 08/10] Update src/libasr/pass/pass_utils.h --- src/libasr/pass/pass_utils.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 10f0edce0c..319f968459 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -604,9 +604,10 @@ namespace LCompilers { std::string call_name = "__init__"; ASR::symbol_t* call_sym = get_struct_member(replacer->al,type->m_derived_type, call_name, x->base.base.loc, replacer->current_scope); - result_vec->push_back(replacer->al, - ASRUtils::STMT(make_call_helper(replacer->al,call_sym, - new_args,call_name,x->base.base.loc))); + result_vec->push_back(replacer->al, ASRUtils::STMT( + ASRUtils::make_SubroutineCall_t_util(replacer->al, + x->base.base.loc, call_sym, nullptr, new_args.p, new_args.size(), + nullptr, nullptr, false, false))); return; } } From 47430c93c49765444b1ece2400e2dcd196b0e18b Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:25:06 +0530 Subject: [PATCH 09/10] Update src/libasr/pass/pass_utils.cpp --- src/libasr/pass/pass_utils.cpp | 39 ---------------------------------- 1 file changed, 39 deletions(-) diff --git a/src/libasr/pass/pass_utils.cpp b/src/libasr/pass/pass_utils.cpp index 0451fa12fe..e15eb84477 100644 --- a/src/libasr/pass/pass_utils.cpp +++ b/src/libasr/pass/pass_utils.cpp @@ -1282,45 +1282,6 @@ namespace LCompilers { return import_struct_member; } - ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, Vec args, - std::string call_name, const Location &loc) { - - ASR::symbol_t *s_generic = nullptr, *stemp = s; - // Type map for generic functions - std::map subs; - std::map rt_subs; - // handling ExternalSymbol - s = ASRUtils::symbol_get_past_external(s); - - if (ASR::is_a(*s)) { - ASR::Function_t *func = ASR::down_cast(s); - if (args.size() < func->n_args) { - std::string missed_args_names =" "; - size_t missed_args_count =0; - for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ - ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); - if(var->m_symbolic_value == nullptr) { - missed_args_names+= "'" + std::string(var->m_name) + "' and "; - missed_args_count++; - } else { - ASR::call_arg_t call_arg; - call_arg.m_value = var->m_symbolic_value; - call_arg.loc = (var->m_symbolic_value->base).loc; - args.push_back(al,call_arg); - } - } - if(missed_args_count > 0){ - missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); - LCompilersException("Number of arguments does not match in the function call"); - - } - } - return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp, - s_generic, args.p, args.size(), nullptr, nullptr, false, false); - } else { - throw LCompilersException("Unsupported call type for " + call_name); - } - } } // namespace PassUtils } // namespace LCompilers From ff8cebc2e020810a54f5fb7777d6d46bcccb1760 Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:25:14 +0530 Subject: [PATCH 10/10] Update src/libasr/pass/pass_utils.h --- src/libasr/pass/pass_utils.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index 319f968459..2025e78113 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -573,9 +573,6 @@ namespace LCompilers { ASR::symbol_t* get_struct_member(Allocator& al, ASR::symbol_t* struct_type_sym, std::string &call_name, const Location &loc, SymbolTable* current_scope); - ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, Vec args, - std::string call_name, const Location &loc); - namespace ReplacerUtils { template void replace_StructConstructor(ASR::StructConstructor_t* x,