Skip to content

Commit 126a3b5

Browse files
authored
Merge pull request #2180 from Shaikh-Ubaid/fix_inner_struct_pass_to_func
Fix inner struct passed as an argument to a function
2 parents ea9715b + f89861b commit 126a3b5

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ RUN(NAME structs_30 LABELS cpython llvm c)
615615
RUN(NAME structs_31 LABELS cpython llvm c)
616616
RUN(NAME structs_32 LABELS cpython llvm c)
617617
RUN(NAME structs_33 LABELS cpython llvm c)
618+
RUN(NAME structs_34 LABELS cpython llvm c)
618619

619620
RUN(NAME symbolics_01 LABELS cpython_sym c_sym)
620621
RUN(NAME symbolics_02 LABELS cpython_sym c_sym)

integration_tests/structs_34.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from lpython import packed, dataclass, ccallable, i32, ccallback
2+
3+
@ccallable
4+
@packed
5+
@dataclass
6+
class struct_0:
7+
val_0 : i32 = 613
8+
9+
@ccallable
10+
@packed
11+
@dataclass
12+
class struct_1:
13+
val_1 : struct_0 = struct_0()
14+
15+
def print_val_0_in_struct_0(struct_0_instance : struct_0) -> i32:
16+
print(struct_0_instance.val_0)
17+
return 0
18+
19+
@ccallback
20+
def entry_point() -> i32:
21+
struct_1_instance : struct_1 = struct_1()
22+
return print_val_0_in_struct_0(struct_1_instance.val_1)
23+
24+
assert entry_point() == 0

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,33 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
10121012
this->visit_expr(*x.m_arg);
10131013
}
10141014

1015+
std::string construct_call_args(size_t n_args, ASR::call_arg_t* m_args) {
1016+
bracket_open++;
1017+
std::string args = "";
1018+
for (size_t i=0; i<n_args; i++) {
1019+
if (ASR::is_a<ASR::Var_t>(*m_args[i].m_value)) {
1020+
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(m_args[i].m_value);
1021+
std::string arg_name = arg->m_name;
1022+
if( ASRUtils::is_array(arg->m_type) &&
1023+
ASRUtils::is_pointer(arg->m_type) ) {
1024+
args += "&" + arg_name;
1025+
} else {
1026+
args += arg_name;
1027+
}
1028+
} else {
1029+
self().visit_expr(*m_args[i].m_value);
1030+
if( ASR::is_a<ASR::Struct_t>(*ASRUtils::expr_type(m_args[i].m_value)) ) {
1031+
args += "&" + src;
1032+
} else {
1033+
args += src;
1034+
}
1035+
}
1036+
if (i < n_args-1) args += ", ";
1037+
}
1038+
bracket_open--;
1039+
return args;
1040+
}
1041+
10151042
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
10161043
CHECK_FAST_C_CPP(compiler_options, x)
10171044
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(
@@ -1052,15 +1079,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
10521079
+ "' not implemented");
10531080
}
10541081
} else {
1055-
std::string args;
1056-
bracket_open++;
1057-
for (size_t i=0; i<x.n_args; i++) {
1058-
self().visit_expr(*x.m_args[i].m_value);
1059-
args += src;
1060-
if (i < x.n_args-1) args += ", ";
1061-
}
1062-
bracket_open--;
1063-
src = fn_name + "(" + args + ")";
1082+
src = fn_name + "(" + construct_call_args(x.n_args, x.m_args) + ")";
10641083
}
10651084
last_expr_precedence = 2;
10661085
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
@@ -2739,30 +2758,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
27392758
if (sym_name == "main") {
27402759
sym_name = "_xx_lcompilers_changed_main_xx";
27412760
}
2742-
std::string out = indent + sym_name + "(";
2743-
for (size_t i=0; i<x.n_args; i++) {
2744-
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value)) {
2745-
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i].m_value);
2746-
std::string arg_name = arg->m_name;
2747-
if( ASRUtils::is_array(arg->m_type) &&
2748-
ASRUtils::is_pointer(arg->m_type) ) {
2749-
out += "&" + arg_name;
2750-
} else {
2751-
out += arg_name;
2752-
}
2753-
} else {
2754-
self().visit_expr(*x.m_args[i].m_value);
2755-
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_args[i].m_value) ||
2756-
ASR::is_a<ASR::Struct_t>(*ASRUtils::expr_type(x.m_args[i].m_value)) ) {
2757-
out += "&" + src;
2758-
} else {
2759-
out += src;
2760-
}
2761-
}
2762-
if (i < x.n_args-1) out += ", ";
2763-
}
2764-
out += ");\n";
2765-
src = out;
2761+
src = indent + sym_name + "(" + construct_call_args(x.n_args, x.m_args) + ");\n";
27662762
}
27672763

27682764
#define SET_INTRINSIC_NAME(X, func_name) \

0 commit comments

Comments
 (0)