Skip to content

Commit

Permalink
Merge pull request #103 from czgdp1807/union_02
Browse files Browse the repository at this point in the history
Ported ``integration_tests/union_02.py`` from LPython and improve LC to compile it
  • Loading branch information
czgdp1807 authored Mar 5, 2024
2 parents 0584a31 + 66107f6 commit 2a6384a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,4 @@ RUN(NAME enum_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_04.cpp LABELS gcc llvm NOFAST)

RUN(NAME union_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME union_02.cpp LABELS gcc llvm NOFAST)
77 changes: 77 additions & 0 deletions integration_tests/union_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>

struct A_t {
int32_t ax;
double ay;
};

void init_A_t(struct A_t& a, int32_t ax_, double ay_) {
a.ax = ax_;
a.ay = ay_;
}

struct B_t {
int64_t bx;
double by;
};

void init_B_t(struct B_t& b, int64_t bx_, double by_) {
b.bx = bx_;
b.by = by_;
}

struct C_t {
int64_t cx;
double cy;
double cz;
};

void init_C_t(struct C_t& c, int64_t cx_, double cy_, double cz_) {
c.cx = cx_;
c.cy = cy_;
c.cz = cz_;
}

union D_t {
struct A_t a;
struct B_t b;
struct C_t c;
};

#define assert(cond) if( !(cond) ) { \
exit(2); \
} \

void test_struct_union() {
union D_t d;

struct A_t aobj;
struct B_t bobj;
struct C_t cobj;

init_A_t(aobj, 0, 1.0);
init_B_t(bobj, int(2), 7.0);
init_C_t(cobj, int(5), 13.0, 8.0);

d.a = aobj;
std::cout << d.a.ax << " " << d.a.ay << std::endl;
assert( d.a.ax == 0 );
assert( abs(d.a.ay - 1.0) <= 1e-12 );

d.b = bobj;
std::cout << d.b.bx << " " << d.b.by << std::endl;
assert( d.b.bx == int(2) );
assert( abs(d.b.by - 7.0) <= 1e-12 );

d.c = cobj;
std::cout << d.c.cx << " " << d.c.cy << " " << d.c.cz << std::endl;
assert( d.c.cx == 5 );
assert( abs(d.c.cy - 13.0) <= 1e-12 );
assert( abs(d.c.cz - 8.0) <= 1e-12 );
}

int main() {

test_struct_union();

}
13 changes: 11 additions & 2 deletions src/libasr/pass/pass_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,9 @@ namespace LCompilers {
current_scope = parent_symtab;
}

void visit_StructType(const ASR::StructType_t& x) {
ASR::StructType_t& xx = const_cast<ASR::StructType_t&>(x);
template <typename T>
void visit_UserDefinedType(const T& x) {
T& xx = const_cast<T&>(x);
SetChar vec; vec.reserve(al, 1);
for( auto itr: x.m_symtab->get_scope() ) {
ASR::ttype_t* type = ASRUtils::extract_type(
Expand All @@ -448,6 +449,14 @@ namespace LCompilers {
xx.m_dependencies = vec.p;
xx.n_dependencies = vec.size();
}

void visit_StructType(const ASR::StructType_t& x) {
visit_UserDefinedType(x);
}

void visit_UnionType(const ASR::UnionType_t& x) {
visit_UserDefinedType(x);
}
};

namespace ReplacerUtils {
Expand Down

0 comments on commit 2a6384a

Please sign in to comment.