Skip to content

Commit

Permalink
Merge pull request #101 from czgdp1807/struct_02
Browse files Browse the repository at this point in the history
Ported ``integration_tests/structs_04.py`` from LPython and improve LC to compile it
  • Loading branch information
czgdp1807 authored Feb 29, 2024
2 parents edec429 + 2266865 commit 0d1fb2e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ RUN(NAME struct_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_04.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_05.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_06.cpp LABELS gcc llvm NOFAST)

RUN(NAME pointer_01.cpp LABELS gcc)
RUN(NAME pointer_02.cpp LABELS gcc)
Expand Down
60 changes: 60 additions & 0 deletions integration_tests/struct_06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>

struct A_t {
float y;
int32_t x;
};

struct B_t {
int32_t z;
struct A_t a;
};

void init_A_t(A_t& a, float y_, int32_t x_) {
a.y = y_;
a.x = x_;
}

void init_B_t(B_t& b, int32_t z_, struct A_t a_) {
b.z = z_;
b.a = a_;
}

void assert(bool condition) {
if( !condition ) {
exit(2);
}
}

void f(const B_t& b) {
std::cout << b.z << " " << b.a.x << " " << b.a.y << std::endl;
assert( b.z == 1 );
assert( b.a.x == 2 );
assert( double(b.a.y) == 3.0 );
}

void g() {
struct A_t a1;
struct A_t a2;
struct B_t b;
init_A_t(a1, float(1.0), 1);
init_A_t(a2, float(2.0), 2);
init_B_t(b, 1, a1);
b.a = a2;
b.z = 1;
b.a.x = 2;
b.a.y = float(3.0);
assert( a1.x == 1 );
assert( double(a1.y) == 1.0 );
assert( a2.x == 2 );
assert( double(a2.y) == 2.0 );
f(b);
}

int main() {

g();

return 0;

}
3 changes: 3 additions & 0 deletions src/lc/clang_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
if( init_expr->getStmtClass() == clang::Stmt::StmtClass::InitListExprClass ) {
init_expr = static_cast<clang::InitListExpr*>(init_expr)->getInit(0);
}
if( init_expr->getStmtClass() == clang::Stmt::StmtClass::CXXConstructExprClass ) {
init_expr = static_cast<clang::CXXConstructExpr*>(init_expr)->getArg(0);
}
if( init_expr->getStmtClass() != clang::Stmt::StmtClass::ImplicitCastExprClass ||
static_cast<clang::ImplicitCastExpr*>(init_expr)->getSubExpr()->getStmtClass() !=
clang::Stmt::StmtClass::DeclRefExprClass ) {
Expand Down

0 comments on commit 0d1fb2e

Please sign in to comment.