Skip to content

Commit b81d0d7

Browse files
committed
C_CPP: Define and use construct_call_args()
1 parent ea9715b commit b81d0d7

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 28 additions & 24 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>(
@@ -2739,30 +2766,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
27392766
if (sym_name == "main") {
27402767
sym_name = "_xx_lcompilers_changed_main_xx";
27412768
}
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;
2769+
src = indent + sym_name + "(" + construct_call_args(x.n_args, x.m_args) + ");\n";
27662770
}
27672771

27682772
#define SET_INTRINSIC_NAME(X, func_name) \

0 commit comments

Comments
 (0)