From c727045ea53ff6911a058cbc3b3b9b25c0854994 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Fri, 2 Aug 2024 19:21:35 +0530 Subject: [PATCH] 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