Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Default constructors #2750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,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)

# callback_04 is to test emulation. So just run with cpython
RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)
Expand Down
28 changes: 28 additions & 0 deletions integration_tests/class_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from lpython import i32,f64
from math import sqrt

class coord:
def __init__(self: "coord"):
self.x: i32 = 3
self.y: i32 = 4
Comment on lines +4 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this is represented as:

class coord:
	self: "coord"
	x: i32 = 3
	y: i32 = 4

And @tanay-man wants this PR to be merged and work on the arguments initialisation in the next PR.


def main():
p1: coord = coord()
sq_dist : i32 = p1.x*p1.x + p1.y*p1.y
dist : f64 = sqrt(f64(sq_dist))
print("Squared Distance from origin = ", sq_dist)
assert sq_dist == 25
print("Distance from origin = ", dist)
assert dist == f64(5)
print("p1.x = 6")
print("p1.y = 8")
p1.x = i32(6)
p1.y = 8
sq_dist = p1.x*p1.x + p1.y*p1.y
dist = sqrt(f64(sq_dist))
print("Squared Distance from origin = ", sq_dist)
assert sq_dist == 100
print("Distance from origin = ", dist)
assert dist == f64(10)

main()
2 changes: 1 addition & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ symbol
| GenericProcedure(symbol_table parent_symtab, identifier name, symbol* procs, access access)
| CustomOperator(symbol_table parent_symtab, identifier name, symbol* procs, access access)
| ExternalSymbol(symbol_table parent_symtab, identifier name, symbol external, identifier module_name, identifier* scope_names, identifier original_name, access access)
| Struct(symbol_table symtab, identifier name, identifier* dependencies, identifier* members, abi abi, access access, bool is_packed, bool is_abstract, call_arg* initializers, expr? alignment, symbol? parent)
| Struct(symbol_table symtab, identifier name, identifier* dependencies, identifier* members, identifier* member_functions, abi abi, access access, bool is_packed, bool is_abstract, call_arg* initializers, expr? alignment, symbol? parent)
| EnumType(symbol_table symtab, identifier name, identifier* dependencies, identifier* members, abi abi, access access, enumtype enum_value_type, ttype type, symbol? parent)
| UnionType(symbol_table symtab, identifier name, identifier* dependencies, identifier* members, abi abi, access access, call_arg* initializers, symbol? parent)
| Variable(symbol_table parent_symtab, identifier name, identifier* dependencies, intent intent, expr? symbolic_value, expr? value, storage_type storage, ttype type, symbol? type_declaration, abi abi, access access, presence presence, bool value_attr)
Expand Down
44 changes: 29 additions & 15 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2453,23 +2453,35 @@ static inline ASR::dimension_t* duplicate_dimensions(Allocator& al, ASR::dimensi
static inline ASR::asr_t* make_StructType_t_util(Allocator& al, Location loc, ASR::symbol_t* der){
ASR::Struct_t* st = ASR::down_cast<ASR::Struct_t>(ASRUtils::symbol_get_past_external(der));
Vec<ASR::ttype_t*> members;
members.reserve(al, st->n_members);
members.reserve(al, 1);
Vec<ASR::ttype_t*> member_functions;
member_functions.reserve(al, 1);
SymbolTable* current_scope = st->m_symtab;
for(size_t i = 0; i < st->n_members; i++){
ASR::symbol_t* temp = current_scope->get_symbol(st->m_members[i]);
if(ASR::is_a<ASR::Variable_t>(*temp)){
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(
ASRUtils::symbol_get_past_external(temp));
members.push_back(al,var->m_type);
members.push_back(al,var->m_type);
}
}
return ASR::make_StructType_t(al,
loc,
members.p,
for(size_t i = 0; i < st->n_member_functions; i++){
ASR::symbol_t* sym = current_scope->get_symbol(st->m_member_functions[i]);
if(sym && ASR::is_a<ASR::Function_t>(*sym)){
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(
ASRUtils::symbol_get_past_external(sym));
ASR::ttype_t* f_type = f->m_function_signature;
member_functions.push_back(al, f_type);
}
}
bool is_cstruct = member_functions.n == 0;
return ASR::make_StructType_t(al,
loc,
members.p,
members.n,
nullptr, //Correct this when mem fn added to Struct_t
0, //Correct this when mem fn added to Struct_t
true, //Correct this when mem fn added to Struct_t
member_functions.p,
member_functions.n,
is_cstruct,
der);

}
Expand Down Expand Up @@ -2530,12 +2542,12 @@ static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
}
case ASR::ttypeType::StructType: {
ASR::StructType_t* tnew = ASR::down_cast<ASR::StructType_t>(t);
t_ = ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tnew->m_data_member_types,
t_ = ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tnew->m_data_member_types,
tnew->n_data_member_types,
tnew->m_member_function_types,
tnew->n_member_function_types,
tnew->m_is_cstruct,
tnew->m_is_cstruct,
tnew->m_derived_type));
break;
}
Expand Down Expand Up @@ -2686,12 +2698,12 @@ static inline ASR::ttype_t* duplicate_type_without_dims(Allocator& al, const ASR
}
case ASR::ttypeType::StructType: {
ASR::StructType_t* tstruct = ASR::down_cast<ASR::StructType_t>(t);
return ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tstruct->m_data_member_types,
return ASRUtils::TYPE(ASR::make_StructType_t(al, t->base.loc,
tstruct->m_data_member_types,
tstruct->n_data_member_types,
tstruct->m_member_function_types,
tstruct->n_member_function_types,
tstruct->m_is_cstruct,
tstruct->m_is_cstruct,
tstruct->m_derived_type));
}
case ASR::ttypeType::Pointer: {
Expand Down Expand Up @@ -4299,7 +4311,9 @@ class SymbolDuplicator {
return ASR::down_cast<ASR::symbol_t>(ASR::make_Struct_t(
al, struct_type_t->base.base.loc, struct_type_symtab,
struct_type_t->m_name, struct_type_t->m_dependencies, struct_type_t->n_dependencies,
struct_type_t->m_members, struct_type_t->n_members, struct_type_t->m_abi,
struct_type_t->m_members, struct_type_t->n_members,
struct_type_t->m_member_functions, struct_type_t->n_member_functions,
struct_type_t->m_abi,
struct_type_t->m_access, struct_type_t->m_is_packed, struct_type_t->m_is_abstract,
struct_type_t->m_initializers, struct_type_t->n_initializers, struct_type_t->m_alignment,
struct_type_t->m_parent));
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/asr_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
ASR::is_a<ASR::Struct_t>(*a.second) ||
ASR::is_a<ASR::UnionType_t>(*a.second) ||
ASR::is_a<ASR::ExternalSymbol_t>(*a.second) ||
ASR::is_a<ASR::CustomOperator_t>(*a.second) ) {
ASR::is_a<ASR::CustomOperator_t>(*a.second) ||
ASR::is_a<ASR::Function_t>(*a.second)) {
continue ;
}
// TODO: Uncomment the following line
Expand Down
17 changes: 16 additions & 1 deletion src/libasr/pass/instantiate_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,19 @@ class SymbolInstantiator : public ASR::BaseExprStmtDuplicator<SymbolInstantiator
data_member_names.push_back(al, x->m_members[i]);
}

Vec<char*> data_member_fn_names;
data_member_fn_names.reserve(al, x->n_member_functions);
for (size_t i=0; i<x->n_members; i++) {
data_member_fn_names.push_back(al, x->m_member_functions[i]);
}

ASR::expr_t *m_alignment = duplicate_expr(x->m_alignment);

ASR::asr_t *result = ASR::make_Struct_t(al, x->base.base.loc,
current_scope, s2c(al, new_sym_name),
nullptr, 0,
data_member_names.p, data_member_names.size(),
data_member_fn_names.p, data_member_fn_names.size(),
x->m_abi, x->m_access, x->m_is_packed, x->m_is_abstract,
nullptr, 0, m_alignment, nullptr);

Expand Down Expand Up @@ -1255,11 +1262,19 @@ class SymbolInstantiator : public ASR::BaseExprStmtDuplicator<SymbolInstantiator
data_member_names.push_back(al, x->m_members[i]);
}

Vec<char*> data_member_fn_names;
data_member_fn_names.reserve(al, x->n_member_functions);
for (size_t i=0; i<x->n_members; i++) {
data_member_fn_names.push_back(al, x->m_member_functions[i]);
}

ASR::expr_t* m_alignment = duplicate_expr(x->m_alignment);

ASR::asr_t* result = ASR::make_Struct_t(al, x->base.base.loc,
new_scope, s2c(al, new_sym_name), nullptr, 0, data_member_names.p,
data_member_names.size(), x->m_abi, x->m_access, x->m_is_packed,
data_member_names.size(),
data_member_fn_names.p, data_member_fn_names.size(),
x->m_abi, x->m_access, x->m_is_packed,
x->m_is_abstract, nullptr, 0, m_alignment, nullptr);

ASR::symbol_t* s = ASR::down_cast<ASR::symbol_t>(result);
Expand Down
Loading
Loading