@@ -105,6 +105,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
105
105
std::map<uint64_t , std::string> const_var_names;
106
106
std::map<int32_t , std::string> gotoid2name;
107
107
std::map<std::string, std::string> emit_headers;
108
+ std::string array_types_decls;
108
109
109
110
// Output configuration:
110
111
// Use std::string or char*
@@ -146,7 +147,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
146
147
BaseCCPPVisitor (diag::Diagnostics &diag, Platform &platform,
147
148
CompilerOptions &_compiler_options, bool gen_stdstring, bool gen_stdcomplex, bool is_c,
148
149
int64_t default_lower_bound) : diag{diag},
149
- platform{platform}, compiler_options{_compiler_options},
150
+ platform{platform}, compiler_options{_compiler_options}, array_types_decls{ std::string ( " " )},
150
151
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
151
152
is_c{is_c}, global_scope{nullptr }, lower_bound{default_lower_bound},
152
153
template_number{0 }, c_ds_api{std::make_unique<CCPPDSUtils>(is_c, platform)},
@@ -381,28 +382,32 @@ R"(#include <stdio.h>
381
382
}
382
383
if (x.m_return_var ) {
383
384
ASR::Variable_t *return_var = ASRUtils::EXPR2VAR (x.m_return_var );
385
+ bool is_array = ASRUtils::is_array (return_var->m_type );
384
386
if (ASRUtils::is_integer (*return_var->m_type )) {
385
- int kind = ASR::down_cast<ASR::Integer_t>(return_var->m_type )->m_kind ;
386
- switch (kind) {
387
- case (1 ) : sub = " int8_t " ; break ;
388
- case (2 ) : sub = " int16_t " ; break ;
389
- case (4 ) : sub = " int32_t " ; break ;
390
- case (8 ) : sub = " int64_t " ; break ;
387
+ int kind = ASRUtils::extract_kind_from_ttype_t (return_var->m_type );
388
+ if (is_array) {
389
+ sub = " struct i" + std::to_string (kind * 8 ) + " * " ;
390
+ } else {
391
+ sub = " int" + std::to_string (kind * 8 ) + " _t " ;
391
392
}
392
393
} else if (ASRUtils::is_unsigned_integer (*return_var->m_type )) {
393
- int kind = ASR::down_cast<ASR::UnsignedInteger_t>(return_var->m_type )->m_kind ;
394
- switch (kind) {
395
- case (1 ) : sub = " uint8_t " ; break ;
396
- case (2 ) : sub = " uint16_t " ; break ;
397
- case (4 ) : sub = " uint32_t " ; break ;
398
- case (8 ) : sub = " uint64_t " ; break ;
394
+ int kind = ASRUtils::extract_kind_from_ttype_t (return_var->m_type );
395
+ if (is_array) {
396
+ sub = " struct u" + std::to_string (kind * 8 ) + " * " ;
397
+ } else {
398
+ sub = " uint" + std::to_string (kind * 8 ) + " _t " ;
399
399
}
400
400
} else if (ASRUtils::is_real (*return_var->m_type )) {
401
- bool is_float = ASR::down_cast<ASR::Real_t>(return_var->m_type )->m_kind == 4 ;
402
- if (is_float) {
403
- sub = " float " ;
401
+ int kind = ASRUtils::extract_kind_from_ttype_t (return_var->m_type );
402
+ bool is_float = (kind == 4 );
403
+ if (is_array) {
404
+ sub = " struct r" + std::to_string (kind * 8 ) + " * " ;
404
405
} else {
405
- sub = " double " ;
406
+ if (is_float) {
407
+ sub = " float " ;
408
+ } else {
409
+ sub = " double " ;
410
+ }
406
411
}
407
412
} else if (ASRUtils::is_logical (*return_var->m_type )) {
408
413
sub = " bool " ;
@@ -534,17 +539,30 @@ R"(#include <stdio.h>
534
539
if (!x.m_return_var ) return " " ;
535
540
ASR::Variable_t* r_v = ASRUtils::EXPR2VAR (x.m_return_var );
536
541
std::string indent = " \n " ;
537
- std::string py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t (r_v->m_type ) + " (pValue)" ;
538
- std::string ret_var_decl = indent + CUtils::get_c_type_from_ttype_t (r_v->m_type ) + " " + std::string (r_v->m_name ) + " ;" ;
539
- std::string ret_assign = indent + std::string (r_v->m_name ) + " = " + py_val_cnvrt + " ;" ;
540
- std::string ret_stmt = indent + " return " + std::string (r_v->m_name ) + " ;" ;
541
- std::string clear_pValue = indent + " Py_DECREF(pValue);" ;
542
- std::string copy_result = " " ;
542
+ std::string py_val_cnvrt, ret_var_decl, copy_result;
543
543
if (ASRUtils::is_aggregate_type (r_v->m_type )) {
544
- if (ASRUtils::is_character (*r_v->m_type )) {
545
- copy_result = indent + std::string (r_v->m_name ) + " = _lfortran_str_copy(" + std::string (r_v->m_name ) + " , 1, 0);" ;
544
+ if (ASRUtils::is_array (r_v->m_type )) {
545
+ ASR::ttype_t * array_type_asr = ASRUtils::type_get_past_array (r_v->m_type );
546
+ std::string array_type_name = CUtils::get_c_type_from_ttype_t (array_type_asr);
547
+ std::string array_encoded_type_name = ASRUtils::get_type_code (array_type_asr, true , false );
548
+ std::string return_type = c_ds_api->get_array_type (array_type_name, array_encoded_type_name, array_types_decls, true );
549
+ py_val_cnvrt = bind_py_utils_functions->get_conv_py_arr_to_c (return_type, array_type_name,
550
+ array_encoded_type_name) + " (pValue)" ;
551
+ ret_var_decl = indent + return_type + " _lpython_return_variable;" ;
552
+ } else {
553
+ if (ASRUtils::is_character (*r_v->m_type )) {
554
+ py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t (r_v->m_type ) + " (pValue)" ;
555
+ ret_var_decl = indent + CUtils::get_c_type_from_ttype_t (r_v->m_type ) + " _lpython_return_variable;" ;
556
+ copy_result = indent + " _lpython_return_variable = _lfortran_str_copy(" + std::string (r_v->m_name ) + " , 1, 0);" ;
557
+ }
546
558
}
559
+ } else {
560
+ py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t (r_v->m_type ) + " (pValue)" ;
561
+ ret_var_decl = indent + CUtils::get_c_type_from_ttype_t (r_v->m_type ) + " _lpython_return_variable;" ;
547
562
}
563
+ std::string ret_assign = indent + std::string (r_v->m_name ) + " = " + py_val_cnvrt + " ;" ;
564
+ std::string ret_stmt = indent + " return _lpython_return_variable;" ;
565
+ std::string clear_pValue = indent + " Py_DECREF(pValue);" ;
548
566
return ret_var_decl + ret_assign + copy_result + clear_pValue + ret_stmt + " \n " ;
549
567
}
550
568
0 commit comments