From b81d0d70f5a2b779a44bc3d87ed3d3836183e510 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 18 Jul 2023 15:26:27 +0530 Subject: [PATCH 1/3] C_CPP: Define and use construct_call_args() --- src/libasr/codegen/asr_to_c_cpp.h | 52 +++++++++++++++++-------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index e4b631d7a1..65ea2e8bf3 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -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(*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(*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( @@ -2739,30 +2766,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.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(*x.m_args[i].m_value) || - ASR::is_a(*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) \ From 96433ad459d53caf4abd75a90c90282cc17631e4 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 18 Jul 2023 15:27:33 +0530 Subject: [PATCH 2/3] C_CPP: Fix inner struct pass to func --- src/libasr/codegen/asr_to_c_cpp.h | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index 65ea2e8bf3..0128978218 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -1079,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.m_type) ) { From f89861bc018fb44d0cb8ec8768d19a0807b53393 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 18 Jul 2023 15:30:44 +0530 Subject: [PATCH 3/3] TEST: Add test for inner struct pass to func --- integration_tests/CMakeLists.txt | 1 + integration_tests/structs_34.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 integration_tests/structs_34.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index b854115b4b..3812fb82e7 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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) diff --git a/integration_tests/structs_34.py b/integration_tests/structs_34.py new file mode 100644 index 0000000000..f69d6d5f6a --- /dev/null +++ b/integration_tests/structs_34.py @@ -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