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 6 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 @@ -830,6 +830,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)
Expand Down
27 changes: 27 additions & 0 deletions integration_tests/class_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from lpython import i32,f64
from math import sqrt

class coord:
def __init__(self:"coord"):
self.x: i32 = 3
self.y: i32 = 4

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
4 changes: 3 additions & 1 deletion src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4299,7 +4299,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