From 3e73086e47c206fc5c40250dcf34f9984bf02e0f Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Mon, 3 Jun 2024 15:28:02 +0530 Subject: [PATCH 01/12] Added function def ASR node to class node --- src/lpython/semantics/python_ast_to_asr.cpp | 165 +++++++++++--------- 1 file changed, 92 insertions(+), 73 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d1c324d237..175831e63d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2926,7 +2926,15 @@ class CommonVisitor : public AST::BaseVisitor { void visit_ClassMembers(const AST::ClassDef_t& x, Vec& member_names, SetChar& struct_dependencies, Vec &member_init, - bool is_enum_scope=false, ASR::abiType abi=ASR::abiType::Source) { + bool is_enum_scope=false, ASR::abiType abi=ASR::abiType::Source, + bool is_generating_body = false) { + if(is_generating_body){ + for( size_t i = 0; i < x.n_body; i++ ){ + if ( AST::is_a(*x.m_body[i]) ) + this->visit_stmt(*x.m_body[i]); + } + return; + } int64_t prev_value = 1; for( size_t i = 0; i < x.n_body; i++ ) { if (AST::is_a(*x.m_body[i])) { @@ -2942,74 +2950,80 @@ class CommonVisitor : public AST::BaseVisitor { visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; } else if ( AST::is_a(*x.m_body[i]) ) { - throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc); + tmp = ASR::make_ClassProcedure_t( + + ); + this->visit_stmt(*x.m_body[i]); + continue; } else if (AST::is_a(*x.m_body[i])) { continue; - } else if (!AST::is_a(*x.m_body[i])) { - throw SemanticError("AnnAssign expected inside struct", 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)) { - throw SemanticError("Only Name supported as target in AnnAssign inside struct", x.m_body[i]->base.loc); - } - AST::Name_t *n = AST::down_cast(ann_assign->m_target); - std::string var_name = n->m_id; - ASR::expr_t* init_expr = nullptr; - if( is_enum_scope ) { - ASR::ttype_t* i64_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8)); - init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, -1, i64_type)); - } - visit_AnnAssignUtil(*ann_assign, var_name, init_expr, false, abi, true); - 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); - if( is_enum_scope ) { - if( AST::is_a(*ann_assign->m_value) ) { - AST::Call_t* auto_call_cand = AST::down_cast(ann_assign->m_value); - if( AST::is_a(*auto_call_cand->m_func) ) { - AST::Name_t* func = AST::down_cast(auto_call_cand->m_func); - std::string func_name = func->m_id; - if( func_name == "auto" ) { - ASR::ttype_t* int_type = ASRUtils::symbol_type(var_sym); - init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, - auto_call_cand->base.base.loc, prev_value, int_type)); - prev_value += 1; + } else if (AST::is_a(*x.m_body[i])) { + AST::AnnAssign_t* ann_assign = AST::down_cast(x.m_body[i]); + if (!AST::is_a(*ann_assign->m_target)) { + throw SemanticError("Only Name supported as target in AnnAssign inside struct", x.m_body[i]->base.loc); + } + AST::Name_t *n = AST::down_cast(ann_assign->m_target); + std::string var_name = n->m_id; + ASR::expr_t* init_expr = nullptr; + if( is_enum_scope ) { + ASR::ttype_t* i64_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8)); + init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, -1, i64_type)); + } + visit_AnnAssignUtil(*ann_assign, var_name, init_expr, false, abi, true); + 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); + if( is_enum_scope ) { + if( AST::is_a(*ann_assign->m_value) ) { + AST::Call_t* auto_call_cand = AST::down_cast(ann_assign->m_value); + if( AST::is_a(*auto_call_cand->m_func) ) { + AST::Name_t* func = AST::down_cast(auto_call_cand->m_func); + std::string func_name = func->m_id; + if( func_name == "auto" ) { + ASR::ttype_t* int_type = ASRUtils::symbol_type(var_sym); + init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, + auto_call_cand->base.base.loc, prev_value, int_type)); + prev_value += 1; + } } + } else { + this->visit_expr(*ann_assign->m_value); + ASR::expr_t* enum_value = ASRUtils::expr_value(ASRUtils::EXPR(tmp)); + LCOMPILERS_ASSERT(ASRUtils::is_value_constant(enum_value)); + ASRUtils::extract_value(enum_value, prev_value); + prev_value += 1; + init_expr = enum_value; } } else { - this->visit_expr(*ann_assign->m_value); - ASR::expr_t* enum_value = ASRUtils::expr_value(ASRUtils::EXPR(tmp)); - LCOMPILERS_ASSERT(ASRUtils::is_value_constant(enum_value)); - ASRUtils::extract_value(enum_value, prev_value); - prev_value += 1; - init_expr = enum_value; - } - } else { - init_expr = nullptr; - } - if( ASR::is_a(*var_sym) ) { - ASR::Variable_t* variable = ASR::down_cast(var_sym); - variable->m_symbolic_value = init_expr; + init_expr = nullptr; + } + if( ASR::is_a(*var_sym) ) { + ASR::Variable_t* variable = ASR::down_cast(var_sym); + variable->m_symbolic_value = init_expr; + } + ASR::ttype_t* var_type = ASRUtils::type_get_past_pointer(ASRUtils::symbol_type(var_sym)); + char* aggregate_type_name = nullptr; + if( ASR::is_a(*var_type) ) { + aggregate_type_name = ASRUtils::symbol_name( + ASR::down_cast(var_type)->m_derived_type); + } else if( ASR::is_a(*var_type) ) { + aggregate_type_name = ASRUtils::symbol_name( + ASR::down_cast(var_type)->m_enum_type); + } else if( ASR::is_a(*var_type) ) { + aggregate_type_name = ASRUtils::symbol_name( + ASR::down_cast(var_type)->m_union_type); + } + if( aggregate_type_name && + !current_scope->get_symbol(std::string(aggregate_type_name)) ) { + struct_dependencies.push_back(al, aggregate_type_name); + } + member_names.push_back(al, n->m_id); } - ASR::ttype_t* var_type = ASRUtils::type_get_past_pointer(ASRUtils::symbol_type(var_sym)); - char* aggregate_type_name = nullptr; - if( ASR::is_a(*var_type) ) { - aggregate_type_name = ASRUtils::symbol_name( - ASR::down_cast(var_type)->m_derived_type); - } else if( ASR::is_a(*var_type) ) { - aggregate_type_name = ASRUtils::symbol_name( - ASR::down_cast(var_type)->m_enum_type); - } else if( ASR::is_a(*var_type) ) { - aggregate_type_name = ASRUtils::symbol_name( - ASR::down_cast(var_type)->m_union_type); - } - if( aggregate_type_name && - !current_scope->get_symbol(std::string(aggregate_type_name)) ) { - struct_dependencies.push_back(al, aggregate_type_name); + else { + throw SemanticError("AnnAssign or Function def expected inside struct", x.m_body[i]->base.loc); } - member_names.push_back(al, n->m_id); } } @@ -3030,6 +3044,7 @@ class CommonVisitor : public AST::BaseVisitor { void visit_ClassDef(const AST::ClassDef_t& x) { std::string x_m_name = x.m_name; + bool is_generating_body = false; if( is_enum(x.m_bases, x.n_bases) ) { if( current_scope->resolve_symbol(x_m_name) ) { return ; @@ -3150,19 +3165,21 @@ class CommonVisitor : public AST::BaseVisitor { } ASR::expr_t* algined_expr = nullptr; bool is_packed = false; - if( !is_dataclass(x.m_decorator_list, x.n_decorator_list, - algined_expr, is_packed) ) { - throw SemanticError("Only dataclass-decorated classes and Enum subclasses are supported.", - x.base.base.loc); - } if( x.n_bases > 0 ) { throw SemanticError("Inheritance in classes isn't supported yet.", x.base.base.loc); } - SymbolTable *parent_scope = current_scope; - current_scope = al.make_new(parent_scope); + ASR::symbol_t* clss_sym = current_scope->get_symbol(x_m_name); + ASR::StructType_t* clss = nullptr; + if(clss_sym != nullptr && !is_enum(x.m_bases, x.n_bases) && !is_union(x.m_bases, x.n_bases)){ + clss = ASR::down_cast(clss_sym); + current_scope = clss->m_symtab; + is_generating_body = true; + }else{ + current_scope = al.make_new(parent_scope); + } Vec member_names; Vec member_init; member_names.reserve(al, x.n_body); @@ -3173,7 +3190,7 @@ class CommonVisitor : public AST::BaseVisitor { if( is_bindc_class(x.m_decorator_list, x.n_decorator_list) ) { class_abi = ASR::abiType::BindC; } - visit_ClassMembers(x, member_names, struct_dependencies, member_init, false, class_abi); + visit_ClassMembers(x, member_names, struct_dependencies, member_init, false, class_abi,is_generating_body); LCOMPILERS_ASSERT(member_init.size() == member_names.size()); ASR::symbol_t* class_type = ASR::down_cast(ASR::make_StructType_t(al, x.base.base.loc, current_scope, x.m_name, @@ -3184,12 +3201,14 @@ class CommonVisitor : public AST::BaseVisitor { nullptr)); current_scope = parent_scope; if (current_scope->resolve_symbol(x_m_name)) { + clss->m_symtab->asr_owner = &clss_sym->base; + return; + } else { + current_scope->add_symbol(x_m_name, class_type); ASR::symbol_t* sym = current_scope->resolve_symbol(x_m_name); ASR::StructType_t *st = ASR::down_cast(sym); st->m_initializers = member_init.p; - st->n_initializers = member_init.size(); - } else { - current_scope->add_symbol(x_m_name, class_type); + st->n_initializers = member_init.size(); } } From aa65b72310638cdda798f7a02412be1bf2e02450 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Mon, 3 Jun 2024 15:45:57 +0530 Subject: [PATCH 02/12] Cleanup --- src/lpython/semantics/python_ast_to_asr.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 175831e63d..ca67a73b4c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2950,9 +2950,6 @@ class CommonVisitor : public AST::BaseVisitor { visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; } else if ( AST::is_a(*x.m_body[i]) ) { - tmp = ASR::make_ClassProcedure_t( - - ); this->visit_stmt(*x.m_body[i]); continue; } else if (AST::is_a(*x.m_body[i])) { From 3580bb6c89d01ac991ad06fc9a4e9f566a69ea76 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Thu, 6 Jun 2024 18:25:32 +0530 Subject: [PATCH 03/12] Added class proc generation --- src/lpython/semantics/python_ast_to_asr.cpp | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index ca67a73b4c..4dca4b91bd 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2947,10 +2947,32 @@ class CommonVisitor : public AST::BaseVisitor { } throw SemanticError("Only doc strings and const ellipsis allowed as expressions inside class", expr->base.base.loc); } else if( AST::is_a(*x.m_body[i]) ) { - visit_ClassDef(*AST::down_cast(x.m_body[i])); + visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; } else if ( AST::is_a(*x.m_body[i]) ) { this->visit_stmt(*x.m_body[i]); + // AST::FunctionDef_t* f_ast = AST::down_cast(x.m_body[i]); + // ASR::symbol_t* f_sym = current_scope->get_symbol(f_ast->m_name); + // ASR::Function_t* f = ASR::down_cast(f_sym); + // std::string class_proc_name = "Xx_Class_Procedure_"+std::string(f->m_name); + // SymbolTable* proc_scope = ASRUtils::symbol_parent_symtab(f_sym); + // Str s; + // s.from_str(al,class_proc_name); + // ASR::abiType abi = ASR::abiType::Source; + // tmp = make_ClassProcedure_t( + // al, + // f_sym->base.loc, + // proc_scope, + // s.p, + // nullptr, + // f->m_name, + // f_sym, + // abi, + // false, + // false + // ); + // ASR::symbol_t *cls_proc_sym = ASR::down_cast(tmp); + // current_scope->add_symbol(class_proc_name, cls_proc_sym); continue; } else if (AST::is_a(*x.m_body[i])) { continue; From f95e2f281c74328bac5d103c6a822271028e7340 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Thu, 6 Jun 2024 19:26:04 +0530 Subject: [PATCH 04/12] Uncomment class_proc genr and correct asr_verify --- src/libasr/asr_verify.cpp | 4 +- src/libasr/stzY3vVD | 1 + src/lpython/semantics/python_ast_to_asr.cpp | 44 ++++++++++----------- 3 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 src/libasr/stzY3vVD diff --git a/src/libasr/asr_verify.cpp b/src/libasr/asr_verify.cpp index 16b255f8cc..592e90d01d 100644 --- a/src/libasr/asr_verify.cpp +++ b/src/libasr/asr_verify.cpp @@ -524,11 +524,13 @@ class VerifyVisitor : public BaseWalkVisitor ASR::is_a(*a.second) || ASR::is_a(*a.second) || ASR::is_a(*a.second) || - ASR::is_a(*a.second) ) { + ASR::is_a(*a.second) || + ASR::is_a(*a.second)) { continue ; } // TODO: Uncomment the following line // ASR::ttype_t* var_type = ASRUtils::extract_type(ASRUtils::symbol_type(a.second)); + ASR::ttype_t* var_type = ASRUtils::type_get_past_pointer(ASRUtils::symbol_type(a.second)); char* aggregate_type_name = nullptr; ASR::symbol_t* sym = nullptr; diff --git a/src/libasr/stzY3vVD b/src/libasr/stzY3vVD new file mode 100644 index 0000000000..8b277f0dd5 --- /dev/null +++ b/src/libasr/stzY3vVD @@ -0,0 +1 @@ +! diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4dca4b91bd..8776f444ad 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2951,28 +2951,28 @@ class CommonVisitor : public AST::BaseVisitor { continue; } else if ( AST::is_a(*x.m_body[i]) ) { this->visit_stmt(*x.m_body[i]); - // AST::FunctionDef_t* f_ast = AST::down_cast(x.m_body[i]); - // ASR::symbol_t* f_sym = current_scope->get_symbol(f_ast->m_name); - // ASR::Function_t* f = ASR::down_cast(f_sym); - // std::string class_proc_name = "Xx_Class_Procedure_"+std::string(f->m_name); - // SymbolTable* proc_scope = ASRUtils::symbol_parent_symtab(f_sym); - // Str s; - // s.from_str(al,class_proc_name); - // ASR::abiType abi = ASR::abiType::Source; - // tmp = make_ClassProcedure_t( - // al, - // f_sym->base.loc, - // proc_scope, - // s.p, - // nullptr, - // f->m_name, - // f_sym, - // abi, - // false, - // false - // ); - // ASR::symbol_t *cls_proc_sym = ASR::down_cast(tmp); - // current_scope->add_symbol(class_proc_name, cls_proc_sym); + AST::FunctionDef_t* f_ast = AST::down_cast(x.m_body[i]); + ASR::symbol_t* f_sym = current_scope->get_symbol(f_ast->m_name); + ASR::Function_t* f = ASR::down_cast(f_sym); + std::string class_proc_name = "Xx_Class_Procedure_"+std::string(f->m_name); + SymbolTable* proc_scope = ASRUtils::symbol_parent_symtab(f_sym); + Str s; + s.from_str(al,class_proc_name); + ASR::abiType abi = ASR::abiType::Source; + tmp = make_ClassProcedure_t( + al, + f_sym->base.loc, + proc_scope, + s.p, + nullptr, + f->m_name, + f_sym, + abi, + false, + false + ); + ASR::symbol_t *cls_proc_sym = ASR::down_cast(tmp); + current_scope->add_symbol(class_proc_name, cls_proc_sym); continue; } else if (AST::is_a(*x.m_body[i])) { continue; From ea4903652064957e345aba95a434aeb410bbbb60 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sat, 8 Jun 2024 16:27:58 +0530 Subject: [PATCH 05/12] generate member func def in LLVM IR --- src/libasr/codegen/asr_to_llvm.cpp | 15 +++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 37 ++++++++++++--------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index bd267d88d5..3ed4327525 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -2969,6 +2969,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } } + void visit_StructType(const ASR::StructType_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( @@ -3010,6 +3022,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::StructType_t *st = down_cast(item.second); + visit_StructType(*st); } } finish_module_init_function_prototype(x); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 8776f444ad..01031d7040 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2950,29 +2950,33 @@ class CommonVisitor : public AST::BaseVisitor { visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; } else if ( AST::is_a(*x.m_body[i]) ) { - this->visit_stmt(*x.m_body[i]); AST::FunctionDef_t* f_ast = AST::down_cast(x.m_body[i]); + std::string f_name_old = std::string(f_ast->m_name); + std::string f_name_new = "Xx_Class_Procedure_" + f_name_old; + Str old_name; + old_name.from_str(al,f_name_old); + Str new_name; + new_name.from_str(al,f_name_new); + f_ast->m_name = new_name.p; + this->visit_stmt(*x.m_body[i]); ASR::symbol_t* f_sym = current_scope->get_symbol(f_ast->m_name); ASR::Function_t* f = ASR::down_cast(f_sym); - std::string class_proc_name = "Xx_Class_Procedure_"+std::string(f->m_name); SymbolTable* proc_scope = ASRUtils::symbol_parent_symtab(f_sym); - Str s; - s.from_str(al,class_proc_name); ASR::abiType abi = ASR::abiType::Source; tmp = make_ClassProcedure_t( - al, - f_sym->base.loc, - proc_scope, - s.p, - nullptr, - f->m_name, - f_sym, - abi, - false, - false - ); + /*&al*/al, + /*&a_loc*/f_sym->base.loc, + /*a_parent_symtab*/proc_scope, + /*a_name*/old_name.p, + /*a_self_argument*/nullptr, + /*a_proc_name*/f->m_name, + /*a_proc*/f_sym, + /*a_abi*/abi, + /*a_is_deferred*/false, + /*a_is_nopass*/false + ); ASR::symbol_t *cls_proc_sym = ASR::down_cast(tmp); - current_scope->add_symbol(class_proc_name, cls_proc_sym); + current_scope->add_symbol(f_name_old, cls_proc_sym); continue; } else if (AST::is_a(*x.m_body[i])) { continue; @@ -7623,6 +7627,7 @@ we will have to use something else. if (ASR::is_a(*var->m_type)) { // call to struct member function ASR::Struct_t* var_struct = ASR::down_cast(var->m_type); + call_name = "Xx_Class_Procedure_" + call_name; st = get_struct_member(var_struct->m_derived_type, call_name, loc); } else { // this case when we have variable and attribute From 35db4bfc00ada08da130c99289f9aaf75a83315e Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Mon, 10 Jun 2024 15:36:27 +0530 Subject: [PATCH 06/12] Fixed an error in the fn def in llvm ir --- src/libasr/codegen/asr_to_llvm.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 3ed4327525..0af6831a2c 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -2970,6 +2970,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_StructType(const ASR::StructType_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 make_struct_f_def(const ASR::StructType_t &x){ SymbolTable *current_scope_copy = current_scope; current_scope = x.m_symtab; for (auto &item : x.m_symtab->get_scope()){ @@ -3023,8 +3035,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASR::EnumType_t *et = down_cast(item.second); visit_EnumType(*et); } else if (is_a(*item.second)) { + mangle_prefix = ""; ASR::StructType_t *st = down_cast(item.second); visit_StructType(*st); + mangle_prefix = "__module_" + std::string(x.m_name) + "_"; } } finish_module_init_function_prototype(x); @@ -4018,6 +4032,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::StructType_t *st = down_cast(item.second); + make_struct_f_def(*st); } } } From 1e18e9b082aa57f47883612ce848eac8008dfa29 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Thu, 13 Jun 2024 12:01:24 +0530 Subject: [PATCH 07/12] Fixed casting in AnnAssign of members --- src/lpython/semantics/python_ast_to_asr.cpp | 59 +++++++++------------ 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 01031d7040..77c949ef74 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2870,9 +2870,10 @@ class CommonVisitor : public AST::BaseVisitor { if (is_const) { storage_type = ASR::storage_typeType::Parameter; } - - create_add_variable_to_scope(var_name, type, - x.base.base.loc, abi, storage_type); + if( !(inside_struct && current_scope->resolve_symbol(var_name)) ){ + create_add_variable_to_scope(var_name, type, + x.base.base.loc, abi, storage_type); + } ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_target); @@ -2928,10 +2929,24 @@ class CommonVisitor : public AST::BaseVisitor { Vec &member_init, bool is_enum_scope=false, ASR::abiType abi=ASR::abiType::Source, bool is_generating_body = false) { - if(is_generating_body){ + if(is_generating_body && !is_enum_scope){ for( size_t i = 0; i < x.n_body; i++ ){ - if ( AST::is_a(*x.m_body[i]) ) + if ( AST::is_a(*x.m_body[i]) ) + //generating function body this->visit_stmt(*x.m_body[i]); + if (AST::is_a(*x.m_body[i])) { + //Add initializers to the AnnAssign + AST::AnnAssign_t* ann_assign = AST::down_cast(x.m_body[i]); + AST::Name_t *n = AST::down_cast(ann_assign->m_target); + std::string var_name = n->m_id; + ASR::expr_t* init_expr = nullptr; + visit_AnnAssignUtil(*ann_assign, var_name, init_expr, false, abi, true); + 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); + } } return; } @@ -2950,33 +2965,7 @@ class CommonVisitor : public AST::BaseVisitor { visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; } else if ( AST::is_a(*x.m_body[i]) ) { - AST::FunctionDef_t* f_ast = AST::down_cast(x.m_body[i]); - std::string f_name_old = std::string(f_ast->m_name); - std::string f_name_new = "Xx_Class_Procedure_" + f_name_old; - Str old_name; - old_name.from_str(al,f_name_old); - Str new_name; - new_name.from_str(al,f_name_new); - f_ast->m_name = new_name.p; this->visit_stmt(*x.m_body[i]); - ASR::symbol_t* f_sym = current_scope->get_symbol(f_ast->m_name); - ASR::Function_t* f = ASR::down_cast(f_sym); - SymbolTable* proc_scope = ASRUtils::symbol_parent_symtab(f_sym); - ASR::abiType abi = ASR::abiType::Source; - tmp = make_ClassProcedure_t( - /*&al*/al, - /*&a_loc*/f_sym->base.loc, - /*a_parent_symtab*/proc_scope, - /*a_name*/old_name.p, - /*a_self_argument*/nullptr, - /*a_proc_name*/f->m_name, - /*a_proc*/f_sym, - /*a_abi*/abi, - /*a_is_deferred*/false, - /*a_is_nopass*/false - ); - ASR::symbol_t *cls_proc_sym = ASR::down_cast(tmp); - current_scope->add_symbol(f_name_old, cls_proc_sym); continue; } else if (AST::is_a(*x.m_body[i])) { continue; @@ -3196,7 +3185,7 @@ class CommonVisitor : public AST::BaseVisitor { SymbolTable *parent_scope = current_scope; ASR::symbol_t* clss_sym = current_scope->get_symbol(x_m_name); ASR::StructType_t* clss = nullptr; - if(clss_sym != nullptr && !is_enum(x.m_bases, x.n_bases) && !is_union(x.m_bases, x.n_bases)){ + if( clss_sym != nullptr ){ clss = ASR::down_cast(clss_sym); current_scope = clss->m_symtab; is_generating_body = true; @@ -3214,7 +3203,6 @@ class CommonVisitor : public AST::BaseVisitor { class_abi = ASR::abiType::BindC; } visit_ClassMembers(x, member_names, struct_dependencies, member_init, false, class_abi,is_generating_body); - LCOMPILERS_ASSERT(member_init.size() == member_names.size()); ASR::symbol_t* class_type = ASR::down_cast(ASR::make_StructType_t(al, x.base.base.loc, current_scope, x.m_name, struct_dependencies.p, struct_dependencies.size(), @@ -3224,6 +3212,10 @@ class CommonVisitor : public AST::BaseVisitor { nullptr)); current_scope = parent_scope; if (current_scope->resolve_symbol(x_m_name)) { + ASR::symbol_t* sym = current_scope->resolve_symbol(x_m_name); + ASR::StructType_t *st = ASR::down_cast(sym); + st->m_initializers = member_init.p; + st->n_initializers = member_init.size(); clss->m_symtab->asr_owner = &clss_sym->base; return; } else { @@ -7627,7 +7619,6 @@ we will have to use something else. if (ASR::is_a(*var->m_type)) { // call to struct member function ASR::Struct_t* var_struct = ASR::down_cast(var->m_type); - call_name = "Xx_Class_Procedure_" + call_name; st = get_struct_member(var_struct->m_derived_type, call_name, loc); } else { // this case when we have variable and attribute From 374e48428d1e590ff4f447a3eef16a878608ea49 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Thu, 13 Jun 2024 12:54:33 +0530 Subject: [PATCH 08/12] Updated tests and removed redundant block --- src/lpython/semantics/python_ast_to_asr.cpp | 4 - tests/reference/asr-intent_01-66824bc.json | 2 +- tests/reference/asr-intent_01-66824bc.stdout | 2 +- tests/reference/asr-structs_01-0893e35.json | 10 +- tests/reference/asr-structs_01-0893e35.stdout | 116 ++++++++++++++++++ tests/reference/asr-structs_01-66dc2c9.json | 2 +- tests/reference/asr-structs_01-66dc2c9.stdout | 8 +- tests/reference/asr-structs_01-be14d49.json | 2 +- tests/reference/asr-structs_01-be14d49.stdout | 8 +- tests/reference/asr-structs_02-2ab459a.json | 2 +- tests/reference/asr-structs_02-2ab459a.stdout | 8 +- tests/reference/asr-structs_03-0cef911.json | 2 +- tests/reference/asr-structs_03-0cef911.stdout | 8 +- tests/reference/asr-structs_04-387747b.json | 2 +- tests/reference/asr-structs_04-387747b.stdout | 8 +- tests/reference/asr-structs_05-fa98307.json | 2 +- tests/reference/asr-structs_05-fa98307.stdout | 8 +- tests/reference/asr-structs_10-cb8a283.json | 2 +- tests/reference/asr-structs_10-cb8a283.stderr | 12 +- tests/reference/asr-structs_16-44de89a.json | 2 +- tests/reference/asr-structs_16-44de89a.stdout | 8 +- ..._class_constructor-structs_16-5e3508f.json | 2 +- ...lass_constructor-structs_16-5e3508f.stdout | 8 +- 23 files changed, 168 insertions(+), 60 deletions(-) create mode 100644 tests/reference/asr-structs_01-0893e35.stdout diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 77c949ef74..918cf686c9 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3220,10 +3220,6 @@ class CommonVisitor : public AST::BaseVisitor { return; } else { current_scope->add_symbol(x_m_name, class_type); - ASR::symbol_t* sym = current_scope->resolve_symbol(x_m_name); - ASR::StructType_t *st = ASR::down_cast(sym); - st->m_initializers = member_init.p; - st->n_initializers = member_init.size(); } } diff --git a/tests/reference/asr-intent_01-66824bc.json b/tests/reference/asr-intent_01-66824bc.json index c981754321..1c060f855d 100644 --- a/tests/reference/asr-intent_01-66824bc.json +++ b/tests/reference/asr-intent_01-66824bc.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-intent_01-66824bc.stdout", - "stdout_hash": "415fb57ee7c986fc49e7c0801edae4e37d6ea06143d27a998c50ab5c", + "stdout_hash": "b4d6158df60118b1a4c88f675262373b18c3ebe073ac2d0761ece543", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-intent_01-66824bc.stdout b/tests/reference/asr-intent_01-66824bc.stdout index d668611200..0cd3dbd294 100644 --- a/tests/reference/asr-intent_01-66824bc.stdout +++ b/tests/reference/asr-intent_01-66824bc.stdout @@ -171,7 +171,7 @@ main_program: (Program (SymbolTable - 6 + 5 { }) diff --git a/tests/reference/asr-structs_01-0893e35.json b/tests/reference/asr-structs_01-0893e35.json index b415cd4e5b..d2b16b3640 100644 --- a/tests/reference/asr-structs_01-0893e35.json +++ b/tests/reference/asr-structs_01-0893e35.json @@ -5,9 +5,9 @@ "infile_hash": "9342dd6345fb71bbbfb1c41e643530c58493758d08774f838b1c85e0", "outfile": null, "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-structs_01-0893e35.stderr", - "stderr_hash": "6f58b337cbc9cb1832e2ecba47fdad4b64e4484b913e29f61c64dbea", - "returncode": 2 + "stdout": "asr-structs_01-0893e35.stdout", + "stdout_hash": "8544977086f85790392cd7aca3915021e9ce236ff83dd681ef0a04e6", + "stderr": null, + "stderr_hash": null, + "returncode": 0 } \ No newline at end of file diff --git a/tests/reference/asr-structs_01-0893e35.stdout b/tests/reference/asr-structs_01-0893e35.stdout new file mode 100644 index 0000000000..fe91a8a77e --- /dev/null +++ b/tests/reference/asr-structs_01-0893e35.stdout @@ -0,0 +1,116 @@ +(TranslationUnit + (SymbolTable + 1 + { + __main__: + (Module + (SymbolTable + 2 + { + PackedStruct: + (StructType + (SymbolTable + 3 + { + Data1: + (Variable + 3 + Data1 + [] + Local + () + () + Default + (Integer 1) + () + Source + Public + Required + .false. + ), + Data2: + (Variable + 3 + Data2 + [] + Local + () + () + Default + (Integer 4) + () + Source + Public + Required + .false. + ), + Data3: + (Variable + 3 + Data3 + [] + Local + () + () + Default + (Integer 1) + () + Source + Public + Required + .false. + ), + Data4: + (Variable + 3 + Data4 + [] + Local + () + () + Default + (Integer 1) + () + Source + Public + Required + .false. + ) + }) + PackedStruct + [] + [Data1 + Data2 + Data3 + Data4] + Source + Public + .false. + .false. + [(()) + (()) + (()) + (())] + () + () + ) + }) + __main__ + [] + .false. + .false. + ), + main_program: + (Program + (SymbolTable + 4 + { + + }) + main_program + [] + [] + ) + }) + [] +) diff --git a/tests/reference/asr-structs_01-66dc2c9.json b/tests/reference/asr-structs_01-66dc2c9.json index ab164948f8..03f10e8011 100644 --- a/tests/reference/asr-structs_01-66dc2c9.json +++ b/tests/reference/asr-structs_01-66dc2c9.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_01-66dc2c9.stdout", - "stdout_hash": "5a32fdd6e6d78976f4d3effbdf4ab79c614eb664a4fd92967ff5d7d7", + "stdout_hash": "3707ab1ee9836ed08cf219d72023f5927694523166b47faf93ed1bc4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_01-66dc2c9.stdout b/tests/reference/asr-structs_01-66dc2c9.stdout index afebbfd171..98f9e178f5 100644 --- a/tests/reference/asr-structs_01-66dc2c9.stdout +++ b/tests/reference/asr-structs_01-66dc2c9.stdout @@ -61,7 +61,7 @@ __main__global_stmts: (Function (SymbolTable - 6 + 5 { }) @@ -163,11 +163,11 @@ main_program: (Program (SymbolTable - 7 + 6 { __main__global_stmts: (ExternalSymbol - 7 + 6 __main__global_stmts 2 __main__global_stmts __main__ @@ -179,7 +179,7 @@ main_program [__main__] [(SubroutineCall - 7 __main__global_stmts + 6 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_01-be14d49.json b/tests/reference/asr-structs_01-be14d49.json index f149ce9e6e..136a469295 100644 --- a/tests/reference/asr-structs_01-be14d49.json +++ b/tests/reference/asr-structs_01-be14d49.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_01-be14d49.stdout", - "stdout_hash": "6ff17e00a05b231e19396a82ff1a25538d74f39f4df7ccc44abf592c", + "stdout_hash": "b90bd9634c5634385cc255dc7d6540875c22968ef123777394d4c26e", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_01-be14d49.stdout b/tests/reference/asr-structs_01-be14d49.stdout index e96c8d99c9..7c3906a437 100644 --- a/tests/reference/asr-structs_01-be14d49.stdout +++ b/tests/reference/asr-structs_01-be14d49.stdout @@ -61,7 +61,7 @@ __main__global_stmts: (Function (SymbolTable - 8 + 7 { }) @@ -506,11 +506,11 @@ main_program: (Program (SymbolTable - 9 + 8 { __main__global_stmts: (ExternalSymbol - 9 + 8 __main__global_stmts 2 __main__global_stmts __main__ @@ -522,7 +522,7 @@ main_program [__main__] [(SubroutineCall - 9 __main__global_stmts + 8 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_02-2ab459a.json b/tests/reference/asr-structs_02-2ab459a.json index 298b5bc8a2..85dd21b6ea 100644 --- a/tests/reference/asr-structs_02-2ab459a.json +++ b/tests/reference/asr-structs_02-2ab459a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_02-2ab459a.stdout", - "stdout_hash": "cc9088a5c112c3dd9820ddfb3695cc301e46d853c4f4634fcdb457b6", + "stdout_hash": "545b53fda59f078d8aeebb4577b6e7dc78699aa576d6ffbc594f5e91", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_02-2ab459a.stdout b/tests/reference/asr-structs_02-2ab459a.stdout index 00933ce63c..36acbf605a 100644 --- a/tests/reference/asr-structs_02-2ab459a.stdout +++ b/tests/reference/asr-structs_02-2ab459a.stdout @@ -61,7 +61,7 @@ __main__global_stmts: (Function (SymbolTable - 7 + 6 { }) @@ -375,11 +375,11 @@ main_program: (Program (SymbolTable - 8 + 7 { __main__global_stmts: (ExternalSymbol - 8 + 7 __main__global_stmts 2 __main__global_stmts __main__ @@ -391,7 +391,7 @@ main_program [__main__] [(SubroutineCall - 8 __main__global_stmts + 7 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_03-0cef911.json b/tests/reference/asr-structs_03-0cef911.json index 4cff33ed98..606010d198 100644 --- a/tests/reference/asr-structs_03-0cef911.json +++ b/tests/reference/asr-structs_03-0cef911.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_03-0cef911.stdout", - "stdout_hash": "86f4e5e4f8a98068919cc24f5e1add31777cbf511dcc6164587c58e3", + "stdout_hash": "6add835ab5c6038f3eea263aab954964c8cc5f8157ff3291551f0e29", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_03-0cef911.stdout b/tests/reference/asr-structs_03-0cef911.stdout index 5f268f4dde..72f1b2e8b4 100644 --- a/tests/reference/asr-structs_03-0cef911.stdout +++ b/tests/reference/asr-structs_03-0cef911.stdout @@ -61,7 +61,7 @@ __main__global_stmts: (Function (SymbolTable - 7 + 6 { }) @@ -355,11 +355,11 @@ main_program: (Program (SymbolTable - 8 + 7 { __main__global_stmts: (ExternalSymbol - 8 + 7 __main__global_stmts 2 __main__global_stmts __main__ @@ -371,7 +371,7 @@ main_program [__main__] [(SubroutineCall - 8 __main__global_stmts + 7 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_04-387747b.json b/tests/reference/asr-structs_04-387747b.json index d0f8cbec18..dce20d6202 100644 --- a/tests/reference/asr-structs_04-387747b.json +++ b/tests/reference/asr-structs_04-387747b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_04-387747b.stdout", - "stdout_hash": "27f6a0c804ed3cea5368c4bec54cb4ea35c60215f354d0d91bc24e89", + "stdout_hash": "5da7fcbfef353f9e0aa676ae59874a5d7ddafd30069c633e3c12db7b", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_04-387747b.stdout b/tests/reference/asr-structs_04-387747b.stdout index 32225f2ccf..14a40bcbaf 100644 --- a/tests/reference/asr-structs_04-387747b.stdout +++ b/tests/reference/asr-structs_04-387747b.stdout @@ -133,7 +133,7 @@ __main__global_stmts: (Function (SymbolTable - 9 + 7 { }) @@ -620,11 +620,11 @@ main_program: (Program (SymbolTable - 10 + 8 { __main__global_stmts: (ExternalSymbol - 10 + 8 __main__global_stmts 2 __main__global_stmts __main__ @@ -636,7 +636,7 @@ main_program [__main__] [(SubroutineCall - 10 __main__global_stmts + 8 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_05-fa98307.json b/tests/reference/asr-structs_05-fa98307.json index cc000e12a0..af0869748b 100644 --- a/tests/reference/asr-structs_05-fa98307.json +++ b/tests/reference/asr-structs_05-fa98307.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_05-fa98307.stdout", - "stdout_hash": "46a6d4fc967a5081b9d2df3936f9a3696cc8383bd140ee0cb37c5e75", + "stdout_hash": "28bf4c643bcd85ad31e0e350e74f557ca56f7d4f69a69b7d9246f9e7", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_05-fa98307.stdout b/tests/reference/asr-structs_05-fa98307.stdout index 1ef54ab37e..f3d1b8675f 100644 --- a/tests/reference/asr-structs_05-fa98307.stdout +++ b/tests/reference/asr-structs_05-fa98307.stdout @@ -151,7 +151,7 @@ __main__global_stmts: (Function (SymbolTable - 232 + 231 { }) @@ -1446,11 +1446,11 @@ main_program: (Program (SymbolTable - 233 + 232 { __main__global_stmts: (ExternalSymbol - 233 + 232 __main__global_stmts 2 __main__global_stmts __main__ @@ -1462,7 +1462,7 @@ main_program [__main__] [(SubroutineCall - 233 __main__global_stmts + 232 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/asr-structs_10-cb8a283.json b/tests/reference/asr-structs_10-cb8a283.json index 6420f7ba94..d59834dcb5 100644 --- a/tests/reference/asr-structs_10-cb8a283.json +++ b/tests/reference/asr-structs_10-cb8a283.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "asr-structs_10-cb8a283.stderr", - "stderr_hash": "2b88127fdbc0c9e3798569cdfa4ef5745d6fce6aeba8fc3f6a1ace9f", + "stderr_hash": "986a1260291bbe43df27d67d801f490853aca9745c2dcf67f7619814", "returncode": 2 } \ No newline at end of file diff --git a/tests/reference/asr-structs_10-cb8a283.stderr b/tests/reference/asr-structs_10-cb8a283.stderr index 757e06a54e..2112ad6a59 100644 --- a/tests/reference/asr-structs_10-cb8a283.stderr +++ b/tests/reference/asr-structs_10-cb8a283.stderr @@ -1,9 +1,5 @@ -semantic error: Struct member functions are not supported - --> tests/errors/structs_10.py:7:5 - 8:24 +semantic error: The type 'StringIO' is undeclared. + --> tests/errors/structs_10.py:7:25 | -7 | def print_len(self: StringIO): - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... -... - | -8 | print(self._len) - | ...^^^^^^^^^^^^^^^^^^^^^^^^ +7 | def print_len(self: StringIO): + | ^^^^^^^^ diff --git a/tests/reference/asr-structs_16-44de89a.json b/tests/reference/asr-structs_16-44de89a.json index 2710b9ad8a..4b1e0512b3 100644 --- a/tests/reference/asr-structs_16-44de89a.json +++ b/tests/reference/asr-structs_16-44de89a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-structs_16-44de89a.stdout", - "stdout_hash": "65cfcaf1a3de5bfe7720be9983c0a9ad22d877701f1375eead4ca4b1", + "stdout_hash": "ca24d8b066ba586afc769b2162a90d810050b487511cc79a9977b8c0", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-structs_16-44de89a.stdout b/tests/reference/asr-structs_16-44de89a.stdout index b98dbfb8e1..e5d6197044 100644 --- a/tests/reference/asr-structs_16-44de89a.stdout +++ b/tests/reference/asr-structs_16-44de89a.stdout @@ -111,7 +111,7 @@ __main__global_stmts: (Function (SymbolTable - 8 + 6 { }) @@ -300,11 +300,11 @@ main_program: (Program (SymbolTable - 9 + 7 { __main__global_stmts: (ExternalSymbol - 9 + 7 __main__global_stmts 2 __main__global_stmts __main__ @@ -316,7 +316,7 @@ main_program [__main__] [(SubroutineCall - 9 __main__global_stmts + 7 __main__global_stmts 2 __main__global_stmts [] () diff --git a/tests/reference/pass_class_constructor-structs_16-5e3508f.json b/tests/reference/pass_class_constructor-structs_16-5e3508f.json index 8204d2a621..73523e1cdc 100644 --- a/tests/reference/pass_class_constructor-structs_16-5e3508f.json +++ b/tests/reference/pass_class_constructor-structs_16-5e3508f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "pass_class_constructor-structs_16-5e3508f.stdout", - "stdout_hash": "b2d0bddf9e8ba1877d428e44cb6bc2a32bb7d2c9db18a20d649dd7cf", + "stdout_hash": "86e4faac26ee8e3fc3fa45581414fc8c6372bd75c0013c2c2d617766", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/pass_class_constructor-structs_16-5e3508f.stdout b/tests/reference/pass_class_constructor-structs_16-5e3508f.stdout index 7ab18fc44d..f64b6f0f9f 100644 --- a/tests/reference/pass_class_constructor-structs_16-5e3508f.stdout +++ b/tests/reference/pass_class_constructor-structs_16-5e3508f.stdout @@ -111,7 +111,7 @@ __main__global_stmts: (Function (SymbolTable - 8 + 6 { }) @@ -329,11 +329,11 @@ main_program: (Program (SymbolTable - 9 + 7 { __main__global_stmts: (ExternalSymbol - 9 + 7 __main__global_stmts 2 __main__global_stmts __main__ @@ -345,7 +345,7 @@ main_program [__main__] [(SubroutineCall - 9 __main__global_stmts + 7 __main__global_stmts 2 __main__global_stmts [] () From 865452712e56183c392cef23d52b0f623978c890 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 16 Jun 2024 21:57:55 +0530 Subject: [PATCH 09/12] Fixed alignment issues --- src/libasr/stzY3vVD | 1 - src/lpython/semantics/python_ast_to_asr.cpp | 8 +++----- tests/reference/asr-structs_01-0893e35.json | 10 +++++----- 3 files changed, 8 insertions(+), 11 deletions(-) delete mode 100644 src/libasr/stzY3vVD diff --git a/src/libasr/stzY3vVD b/src/libasr/stzY3vVD deleted file mode 100644 index 8b277f0dd5..0000000000 --- a/src/libasr/stzY3vVD +++ /dev/null @@ -1 +0,0 @@ -! diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d13400286a..47218dbad7 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2493,13 +2493,11 @@ class CommonVisitor : public AST::BaseVisitor { return false; } - bool is_dataclass(AST::expr_t** decorators, size_t n, + void get_alignment(AST::expr_t** decorators, size_t n, ASR::expr_t*& aligned_expr, bool& is_packed) { - bool is_dataclass_ = false; for( size_t i = 0; i < n; i++ ) { if( AST::is_a(*decorators[i]) ) { AST::Name_t* dc_name = AST::down_cast(decorators[i]); - is_dataclass_ = std::string(dc_name->m_id) == "dataclass"; is_packed = is_packed || std::string(dc_name->m_id) == "packed"; } else if( AST::is_a(*decorators[i]) ) { AST::Call_t* dc_call = AST::down_cast(decorators[i]); @@ -2532,7 +2530,7 @@ class CommonVisitor : public AST::BaseVisitor { } } - return is_dataclass_; + return; } bool is_enum(AST::expr_t** bases, size_t n) { @@ -3177,7 +3175,7 @@ class CommonVisitor : public AST::BaseVisitor { } ASR::expr_t* algined_expr = nullptr; bool is_packed = false; - + get_alignment(x.m_decorator_list, x.n_decorator_list, algined_expr, is_packed); if( x.n_bases > 0 ) { throw SemanticError("Inheritance in classes isn't supported yet.", x.base.base.loc); diff --git a/tests/reference/asr-structs_01-0893e35.json b/tests/reference/asr-structs_01-0893e35.json index d2b16b3640..b415cd4e5b 100644 --- a/tests/reference/asr-structs_01-0893e35.json +++ b/tests/reference/asr-structs_01-0893e35.json @@ -5,9 +5,9 @@ "infile_hash": "9342dd6345fb71bbbfb1c41e643530c58493758d08774f838b1c85e0", "outfile": null, "outfile_hash": null, - "stdout": "asr-structs_01-0893e35.stdout", - "stdout_hash": "8544977086f85790392cd7aca3915021e9ce236ff83dd681ef0a04e6", - "stderr": null, - "stderr_hash": null, - "returncode": 0 + "stdout": null, + "stdout_hash": null, + "stderr": "asr-structs_01-0893e35.stderr", + "stderr_hash": "6f58b337cbc9cb1832e2ecba47fdad4b64e4484b913e29f61c64dbea", + "returncode": 2 } \ No newline at end of file From 969f880211ded9acf339d83f871b443ce534a892 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 16 Jun 2024 22:19:34 +0530 Subject: [PATCH 10/12] Added test for static class methods --- integration_tests/CMakeLists.txt | 1 + integration_tests/class_01.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 integration_tests/class_01.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index ea416e764b..f10bb882fb 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -828,6 +828,7 @@ RUN(NAME callback_03 LABELS cpython llvm llvm_jit c) RUN(NAME lambda_01 LABELS cpython llvm llvm_jit) RUN(NAME c_mangling LABELS cpython llvm llvm_jit c) +RUN(NAME class_01 LABELS cpython llvm llvm_jit c) # callback_04 is to test emulation. So just run with cpython RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython) diff --git a/integration_tests/class_01.py b/integration_tests/class_01.py new file mode 100644 index 0000000000..db229f8b1a --- /dev/null +++ b/integration_tests/class_01.py @@ -0,0 +1,28 @@ +from lpython import i32,i64 + +def fn_2(): + print("Inside fn_2") + return + +class Test: + mem : i64 = i64(5) + s : str = "abc" + def fn_1(): + print("Inside fn_1") + print("fn_2 called") + fn_2() + print("fn_3 called") + fn_3() + return + def fn_3(): + print("Inside fn_3") + +def main(): + t: Test = Test() + print(t.mem) + assert t.mem == i64(5) + print(t.s) + assert t.s == "abc" + Test.fn_1() + +main() \ No newline at end of file From 78cd91a26e4805ab01b6234966a44f534650f7d5 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 16 Jun 2024 22:25:04 +0530 Subject: [PATCH 11/12] Correcting the test for static functions --- integration_tests/class_01.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/class_01.py b/integration_tests/class_01.py index db229f8b1a..f23e1b7076 100644 --- a/integration_tests/class_01.py +++ b/integration_tests/class_01.py @@ -12,7 +12,7 @@ def fn_1(): print("fn_2 called") fn_2() print("fn_3 called") - fn_3() + Test.fn_3() return def fn_3(): print("Inside fn_3") From e3279e6fcca7510c86e2c4fe4034c628bdcc16a1 Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 16 Jun 2024 22:33:22 +0530 Subject: [PATCH 12/12] Removed c backend for the class_01 test --- integration_tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index f10bb882fb..3a325386a8 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -828,7 +828,7 @@ RUN(NAME callback_03 LABELS cpython llvm llvm_jit c) RUN(NAME lambda_01 LABELS cpython llvm llvm_jit) RUN(NAME c_mangling LABELS cpython llvm llvm_jit c) -RUN(NAME class_01 LABELS cpython llvm llvm_jit c) +RUN(NAME class_01 LABELS cpython llvm llvm_jit) # callback_04 is to test emulation. So just run with cpython RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)