Skip to content

Commit 2a6384a

Browse files
authored
Merge pull request #103 from czgdp1807/union_02
Ported ``integration_tests/union_02.py`` from LPython and improve LC to compile it
2 parents 0584a31 + 66107f6 commit 2a6384a

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

integration_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,4 @@ RUN(NAME enum_03.cpp LABELS gcc llvm NOFAST)
231231
RUN(NAME enum_04.cpp LABELS gcc llvm NOFAST)
232232

233233
RUN(NAME union_01.cpp LABELS gcc llvm NOFAST)
234+
RUN(NAME union_02.cpp LABELS gcc llvm NOFAST)

integration_tests/union_02.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
3+
struct A_t {
4+
int32_t ax;
5+
double ay;
6+
};
7+
8+
void init_A_t(struct A_t& a, int32_t ax_, double ay_) {
9+
a.ax = ax_;
10+
a.ay = ay_;
11+
}
12+
13+
struct B_t {
14+
int64_t bx;
15+
double by;
16+
};
17+
18+
void init_B_t(struct B_t& b, int64_t bx_, double by_) {
19+
b.bx = bx_;
20+
b.by = by_;
21+
}
22+
23+
struct C_t {
24+
int64_t cx;
25+
double cy;
26+
double cz;
27+
};
28+
29+
void init_C_t(struct C_t& c, int64_t cx_, double cy_, double cz_) {
30+
c.cx = cx_;
31+
c.cy = cy_;
32+
c.cz = cz_;
33+
}
34+
35+
union D_t {
36+
struct A_t a;
37+
struct B_t b;
38+
struct C_t c;
39+
};
40+
41+
#define assert(cond) if( !(cond) ) { \
42+
exit(2); \
43+
} \
44+
45+
void test_struct_union() {
46+
union D_t d;
47+
48+
struct A_t aobj;
49+
struct B_t bobj;
50+
struct C_t cobj;
51+
52+
init_A_t(aobj, 0, 1.0);
53+
init_B_t(bobj, int(2), 7.0);
54+
init_C_t(cobj, int(5), 13.0, 8.0);
55+
56+
d.a = aobj;
57+
std::cout << d.a.ax << " " << d.a.ay << std::endl;
58+
assert( d.a.ax == 0 );
59+
assert( abs(d.a.ay - 1.0) <= 1e-12 );
60+
61+
d.b = bobj;
62+
std::cout << d.b.bx << " " << d.b.by << std::endl;
63+
assert( d.b.bx == int(2) );
64+
assert( abs(d.b.by - 7.0) <= 1e-12 );
65+
66+
d.c = cobj;
67+
std::cout << d.c.cx << " " << d.c.cy << " " << d.c.cz << std::endl;
68+
assert( d.c.cx == 5 );
69+
assert( abs(d.c.cy - 13.0) <= 1e-12 );
70+
assert( abs(d.c.cz - 8.0) <= 1e-12 );
71+
}
72+
73+
int main() {
74+
75+
test_struct_union();
76+
77+
}

src/libasr/pass/pass_utils.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,9 @@ namespace LCompilers {
431431
current_scope = parent_symtab;
432432
}
433433

434-
void visit_StructType(const ASR::StructType_t& x) {
435-
ASR::StructType_t& xx = const_cast<ASR::StructType_t&>(x);
434+
template <typename T>
435+
void visit_UserDefinedType(const T& x) {
436+
T& xx = const_cast<T&>(x);
436437
SetChar vec; vec.reserve(al, 1);
437438
for( auto itr: x.m_symtab->get_scope() ) {
438439
ASR::ttype_t* type = ASRUtils::extract_type(
@@ -448,6 +449,14 @@ namespace LCompilers {
448449
xx.m_dependencies = vec.p;
449450
xx.n_dependencies = vec.size();
450451
}
452+
453+
void visit_StructType(const ASR::StructType_t& x) {
454+
visit_UserDefinedType(x);
455+
}
456+
457+
void visit_UnionType(const ASR::UnionType_t& x) {
458+
visit_UserDefinedType(x);
459+
}
451460
};
452461

453462
namespace ReplacerUtils {

0 commit comments

Comments
 (0)