Skip to content

Support array constant initialization for array of structs #1811

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 4 commits into from
May 15, 2023
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 @@ -477,6 +477,7 @@ RUN(NAME structs_19 LABELS cpython llvm c
RUN(NAME structs_20 LABELS cpython llvm c
EXTRAFILES structs_20b.c)
RUN(NAME structs_21 LABELS cpython llvm c)
RUN(NAME structs_22 LABELS cpython llvm c)
RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
RUN(NAME sizeof_02 LABELS cpython llvm c)
Expand Down
43 changes: 43 additions & 0 deletions integration_tests/structs_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from lpython import dataclass, i32, f64, u64
from numpy import array

@dataclass
class Foo:
x: i32
y: i32

@dataclass
class Foo2:
p: f64
q: i32
r: u64

def main0() -> None:
foos: Foo[2] = array([Foo(1, 2), Foo(3, 4)])
print(foos[0].x, foos[0].y, foos[1].x, foos[1].y)

assert foos[0].x == 1
assert foos[0].y == 2
assert foos[1].x == 3
assert foos[1].y == 4

def main1() -> None:
foos2: Foo2[3] = array([Foo2(-2.3, 42, u64(3)), Foo2(45.5, -3, u64(10001)), Foo2(1.0, -101, u64(100))])
i: i32
for i in range(3):
print(foos2[i].p, foos2[i].q, foos2[i].r)

eps: f64
eps = 1e-12
assert abs(foos2[0].p - (-2.3)) <= eps
assert foos2[0].q == 42
assert foos2[0].r == u64(3)
assert abs(foos2[1].p - (45.5)) <= eps
assert foos2[1].q == -3
assert foos2[1].r == u64(10001)
assert abs(foos2[2].p - (1.0)) <= eps
assert foos2[2].q == -101
assert foos2[2].r == u64(100)

main0()
main1()
4 changes: 2 additions & 2 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,10 +1133,10 @@ R"(
}
for (size_t i=0; i<x.n_values; i++) {
this->visit_expr(*x.m_values[i]);
if( ASRUtils::is_array(ASRUtils::expr_type(x.m_values[i])) ) {
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
if( ASRUtils::is_array(value_type) ) {
src += "->data";
}
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
if (value_type->type == ASR::ttypeType::List ||
value_type->type == ASR::ttypeType::Tuple) {
tmp_gen += "\"";
Expand Down
16 changes: 16 additions & 0 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,22 @@ class CCPPDSUtils {
default: { throw LCompilersException("Integer kind not supported"); }
}
}
case ASR::ttypeType::UnsignedInteger: {
ASR::UnsignedInteger_t *ui = (ASR::UnsignedInteger_t*)t;
switch (ui->m_kind) {
case 1: { return "%u"; }
case 2: { return "%u"; }
case 4: { return "%u"; }
case 8: {
if (platform == Platform::Linux) {
return "%lu";
} else {
return "%llu";
}
}
default: { throw LCompilersException("Unsigned Integer kind not supported"); }
}
}
case ASR::ttypeType::Real: {
ASR::Real_t *r = (ASR::Real_t*)t;
switch (r->m_kind) {
Expand Down
4 changes: 2 additions & 2 deletions src/libasr/pass/pass_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ namespace LCompilers {
"nested_vars",
"global_stmts",
"init_expr",
"class_constructor",
"implied_do_loops",
"class_constructor",
"pass_list_expr",
"arr_slice",
"subroutine_from_function",
Expand All @@ -212,8 +212,8 @@ namespace LCompilers {
_with_optimization_passes = {
"global_stmts",
"init_expr",
"class_constructor",
"implied_do_loops",
"class_constructor",
"pass_array_by_data",
"arr_slice",
"subroutine_from_function",
Expand Down