Skip to content

Commit 43dd82b

Browse files
authored
Merge pull request #611 from czgdp1807/structs_01
Initial support for structs at ASR, LLVM levels.
2 parents b0f68f7 + 764d290 commit 43dd82b

File tree

9 files changed

+227
-80
lines changed

9 files changed

+227
-80
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ RUN(NAME test_integer_bitnot LABELS cpython llvm)
168168
RUN(NAME test_unary_minus LABELS cpython llvm)
169169
RUN(NAME test_unary_plus LABELS cpython llvm)
170170
RUN(NAME test_issue_518 LABELS cpython llvm)
171+
RUN(NAME structs_01 LABELS cpython llvm)
171172

172173
# Just CPython
173174
RUN(NAME test_builtin_bin LABELS cpython)

integration_tests/structs_01.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from ltypes import i32, f32, dataclass
2+
3+
@dataclass
4+
class A:
5+
x: i32
6+
y: f32
7+
8+
def f(a: A):
9+
print(a.x)
10+
print(a.y)
11+
12+
def g():
13+
x: A
14+
x = A(3, 3.3)
15+
f(x)
16+
# TODO: the above constructor does not initialize `A` in LPython yet, so
17+
# the below does not work:
18+
#assert x.x == 3
19+
#assert x.y == 3.3
20+
21+
x.x = 5
22+
x.y = 5.5
23+
f(x)
24+
assert x.x == 5
25+
assert x.y == 5.5
26+
27+
g()

src/libasr/pass/class_constructor.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
3737
}
3838

3939
void visit_DerivedTypeConstructor(const ASR::DerivedTypeConstructor_t &x) {
40+
if( x.n_args == 0 ) {
41+
remove_original_stmt = true;
42+
}
4043
ASR::Derived_t* dt_der = down_cast<ASR::Derived_t>(x.m_type);
41-
ASR::DerivedType_t* dt_dertype = (ASR::DerivedType_t*)(&(
42-
LFortran::ASRUtils::symbol_get_past_external(dt_der->m_derived_type)->base));
43-
for( size_t i = 0; i < dt_dertype->n_members; i++ ) {
44+
ASR::DerivedType_t* dt_dertype = ASR::down_cast<ASR::DerivedType_t>(
45+
LFortran::ASRUtils::symbol_get_past_external(dt_der->m_derived_type));
46+
for( size_t i = 0; i < std::min(dt_dertype->n_members, x.n_args); i++ ) {
4447
ASR::symbol_t* member = dt_dertype->m_symtab->resolve_symbol(std::string(dt_dertype->m_members[i], strlen(dt_dertype->m_members[i])));
4548
ASR::expr_t* derived_ref = LFortran::ASRUtils::EXPR(ASRUtils::getDerivedRef_t(al, x.base.base.loc, (ASR::asr_t*)result_var, member, current_scope));
4649
ASR::stmt_t* assign = LFortran::ASRUtils::STMT(ASR::make_Assignment_t(al, x.base.base.loc, derived_ref, x.m_args[i], nullptr));

src/libasr/pass/pass_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace LFortran {
6868

6969
public:
7070

71-
bool asr_changed, retain_original_stmt;
71+
bool asr_changed, retain_original_stmt, remove_original_stmt;
7272
Allocator& al;
7373
Vec<ASR::stmt_t*> pass_result;
7474
SymbolTable* current_scope;
@@ -86,6 +86,7 @@ namespace LFortran {
8686
// visitor method:
8787
pass_result.n = 0;
8888
retain_original_stmt = false;
89+
remove_original_stmt = false;
8990
self().visit_stmt(*m_body[i]);
9091
if (pass_result.size() > 0) {
9192
asr_changed = true;
@@ -97,7 +98,7 @@ namespace LFortran {
9798
retain_original_stmt = false;
9899
}
99100
pass_result.n = 0;
100-
} else {
101+
} else if(!remove_original_stmt) {
101102
body.push_back(al, m_body[i]);
102103
}
103104
}

0 commit comments

Comments
 (0)