Skip to content

Commit

Permalink
Merge pull request #2180 from Shaikh-Ubaid/fix_inner_struct_pass_to_func
Browse files Browse the repository at this point in the history
Fix inner struct passed as an argument to a function
  • Loading branch information
certik authored Jul 18, 2023
2 parents ea9715b + f89861b commit 126a3b5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ RUN(NAME structs_30 LABELS cpython llvm c)
RUN(NAME structs_31 LABELS cpython llvm c)
RUN(NAME structs_32 LABELS cpython llvm c)
RUN(NAME structs_33 LABELS cpython llvm c)
RUN(NAME structs_34 LABELS cpython llvm c)

RUN(NAME symbolics_01 LABELS cpython_sym c_sym)
RUN(NAME symbolics_02 LABELS cpython_sym c_sym)
Expand Down
24 changes: 24 additions & 0 deletions integration_tests/structs_34.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from lpython import packed, dataclass, ccallable, i32, ccallback

@ccallable
@packed
@dataclass
class struct_0:
val_0 : i32 = 613

@ccallable
@packed
@dataclass
class struct_1:
val_1 : struct_0 = struct_0()

def print_val_0_in_struct_0(struct_0_instance : struct_0) -> i32:
print(struct_0_instance.val_0)
return 0

@ccallback
def entry_point() -> i32:
struct_1_instance : struct_1 = struct_1()
return print_val_0_in_struct_0(struct_1_instance.val_1)

assert entry_point() == 0
62 changes: 29 additions & 33 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,33 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
this->visit_expr(*x.m_arg);
}

std::string construct_call_args(size_t n_args, ASR::call_arg_t* m_args) {
bracket_open++;
std::string args = "";
for (size_t i=0; i<n_args; i++) {
if (ASR::is_a<ASR::Var_t>(*m_args[i].m_value)) {
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(m_args[i].m_value);
std::string arg_name = arg->m_name;
if( ASRUtils::is_array(arg->m_type) &&
ASRUtils::is_pointer(arg->m_type) ) {
args += "&" + arg_name;
} else {
args += arg_name;
}
} else {
self().visit_expr(*m_args[i].m_value);
if( ASR::is_a<ASR::Struct_t>(*ASRUtils::expr_type(m_args[i].m_value)) ) {
args += "&" + src;
} else {
args += src;
}
}
if (i < n_args-1) args += ", ";
}
bracket_open--;
return args;
}

void visit_FunctionCall(const ASR::FunctionCall_t &x) {
CHECK_FAST_C_CPP(compiler_options, x)
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(
Expand Down Expand Up @@ -1052,15 +1079,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
+ "' not implemented");
}
} else {
std::string args;
bracket_open++;
for (size_t i=0; i<x.n_args; i++) {
self().visit_expr(*x.m_args[i].m_value);
args += src;
if (i < x.n_args-1) args += ", ";
}
bracket_open--;
src = fn_name + "(" + args + ")";
src = fn_name + "(" + construct_call_args(x.n_args, x.m_args) + ")";
}
last_expr_precedence = 2;
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
Expand Down Expand Up @@ -2739,30 +2758,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
if (sym_name == "main") {
sym_name = "_xx_lcompilers_changed_main_xx";
}
std::string out = indent + sym_name + "(";
for (size_t i=0; i<x.n_args; i++) {
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value)) {
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i].m_value);
std::string arg_name = arg->m_name;
if( ASRUtils::is_array(arg->m_type) &&
ASRUtils::is_pointer(arg->m_type) ) {
out += "&" + arg_name;
} else {
out += arg_name;
}
} else {
self().visit_expr(*x.m_args[i].m_value);
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_args[i].m_value) ||
ASR::is_a<ASR::Struct_t>(*ASRUtils::expr_type(x.m_args[i].m_value)) ) {
out += "&" + src;
} else {
out += src;
}
}
if (i < x.n_args-1) out += ", ";
}
out += ");\n";
src = out;
src = indent + sym_name + "(" + construct_call_args(x.n_args, x.m_args) + ");\n";
}

#define SET_INTRINSIC_NAME(X, func_name) \
Expand Down

0 comments on commit 126a3b5

Please sign in to comment.