Skip to content

Commit 68024a0

Browse files
authored
Merge pull request #1811 from Shaikh-Ubaid/arr_of_struct
Support array constant initialization for array of structs
2 parents 7727de2 + f90ee30 commit 68024a0

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ RUN(NAME structs_19 LABELS cpython llvm c
477477
RUN(NAME structs_20 LABELS cpython llvm c
478478
EXTRAFILES structs_20b.c)
479479
RUN(NAME structs_21 LABELS cpython llvm c)
480+
RUN(NAME structs_22 LABELS cpython llvm c)
480481
RUN(NAME sizeof_01 LABELS llvm c
481482
EXTRAFILES sizeof_01b.c)
482483
RUN(NAME sizeof_02 LABELS cpython llvm c)

integration_tests/structs_22.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from lpython import dataclass, i32, f64, u64
2+
from numpy import array
3+
4+
@dataclass
5+
class Foo:
6+
x: i32
7+
y: i32
8+
9+
@dataclass
10+
class Foo2:
11+
p: f64
12+
q: i32
13+
r: u64
14+
15+
def main0() -> None:
16+
foos: Foo[2] = array([Foo(1, 2), Foo(3, 4)])
17+
print(foos[0].x, foos[0].y, foos[1].x, foos[1].y)
18+
19+
assert foos[0].x == 1
20+
assert foos[0].y == 2
21+
assert foos[1].x == 3
22+
assert foos[1].y == 4
23+
24+
def main1() -> None:
25+
foos2: Foo2[3] = array([Foo2(-2.3, 42, u64(3)), Foo2(45.5, -3, u64(10001)), Foo2(1.0, -101, u64(100))])
26+
i: i32
27+
for i in range(3):
28+
print(foos2[i].p, foos2[i].q, foos2[i].r)
29+
30+
eps: f64
31+
eps = 1e-12
32+
assert abs(foos2[0].p - (-2.3)) <= eps
33+
assert foos2[0].q == 42
34+
assert foos2[0].r == u64(3)
35+
assert abs(foos2[1].p - (45.5)) <= eps
36+
assert foos2[1].q == -3
37+
assert foos2[1].r == u64(10001)
38+
assert abs(foos2[2].p - (1.0)) <= eps
39+
assert foos2[2].q == -101
40+
assert foos2[2].r == u64(100)
41+
42+
main0()
43+
main1()

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,10 +1133,10 @@ R"(
11331133
}
11341134
for (size_t i=0; i<x.n_values; i++) {
11351135
this->visit_expr(*x.m_values[i]);
1136-
if( ASRUtils::is_array(ASRUtils::expr_type(x.m_values[i])) ) {
1136+
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
1137+
if( ASRUtils::is_array(value_type) ) {
11371138
src += "->data";
11381139
}
1139-
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
11401140
if (value_type->type == ASR::ttypeType::List ||
11411141
value_type->type == ASR::ttypeType::Tuple) {
11421142
tmp_gen += "\"";

src/libasr/codegen/c_utils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ class CCPPDSUtils {
471471
default: { throw LCompilersException("Integer kind not supported"); }
472472
}
473473
}
474+
case ASR::ttypeType::UnsignedInteger: {
475+
ASR::UnsignedInteger_t *ui = (ASR::UnsignedInteger_t*)t;
476+
switch (ui->m_kind) {
477+
case 1: { return "%u"; }
478+
case 2: { return "%u"; }
479+
case 4: { return "%u"; }
480+
case 8: {
481+
if (platform == Platform::Linux) {
482+
return "%lu";
483+
} else {
484+
return "%llu";
485+
}
486+
}
487+
default: { throw LCompilersException("Unsigned Integer kind not supported"); }
488+
}
489+
}
474490
case ASR::ttypeType::Real: {
475491
ASR::Real_t *r = (ASR::Real_t*)t;
476492
switch (r->m_kind) {

src/libasr/pass/pass_manager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ namespace LCompilers {
189189
"nested_vars",
190190
"global_stmts",
191191
"init_expr",
192-
"class_constructor",
193192
"implied_do_loops",
193+
"class_constructor",
194194
"pass_list_expr",
195195
"arr_slice",
196196
"subroutine_from_function",
@@ -212,8 +212,8 @@ namespace LCompilers {
212212
_with_optimization_passes = {
213213
"global_stmts",
214214
"init_expr",
215-
"class_constructor",
216215
"implied_do_loops",
216+
"class_constructor",
217217
"pass_array_by_data",
218218
"arr_slice",
219219
"subroutine_from_function",

0 commit comments

Comments
 (0)