5
5
#include < libasr/containers.h>
6
6
#include < libasr/codegen/asr_to_c.h>
7
7
#include < libasr/codegen/asr_to_c_cpp.h>
8
+ #include < libasr/codegen/c_utils.h>
8
9
#include < libasr/exception.h>
9
10
#include < libasr/asr_utils.h>
10
11
#include < libasr/string_utils.h>
@@ -227,7 +228,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
227
228
}
228
229
}
229
230
if ( size == 0 ) {
230
- std::string element_type_str = get_c_type_from_ttype_t (element_type);
231
+ std::string element_type_str = CUtils:: get_c_type_from_ttype_t (element_type);
231
232
dims = " (" + element_type_str + " *)" + " malloc(sizeof(" + element_type_str + " )" + array_size + " )" ;
232
233
is_fixed_size = false ;
233
234
return dims;
@@ -522,10 +523,15 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
522
523
}
523
524
} else if (ASR::is_a<ASR::List_t>(*v.m_type )) {
524
525
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v.m_type );
525
- std::string list_element_type = get_c_type_from_ttype_t (t->m_type );
526
+ std::string list_element_type = CUtils:: get_c_type_from_ttype_t (t->m_type );
526
527
std::string list_type_c = list_api->get_list_type (t, list_element_type);
527
528
sub = format_type_c (" " , list_type_c, v.m_name ,
528
529
false , false );
530
+ } else if (ASR::is_a<ASR::Tuple_t>(*v.m_type )) {
531
+ ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v.m_type );
532
+ std::string tuple_type_c = tuple_api->get_tuple_type (t);
533
+ sub = format_type_c (" " , tuple_type_c, v.m_name ,
534
+ false , false );
529
535
} else if (ASR::is_a<ASR::CPtr_t>(*v.m_type )) {
530
536
sub = format_type_c (" " , " void*" , v.m_name , false , false );
531
537
} else if (ASR::is_a<ASR::Const_t>(*v.m_type )) {
@@ -535,7 +541,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
535
541
sub = " #define " + std::string (v.m_name ) + " " + src + " \n " ;
536
542
return sub;
537
543
} else {
538
- std::string const_underlying_type = get_c_type_from_ttype_t (
544
+ std::string const_underlying_type = CUtils:: get_c_type_from_ttype_t (
539
545
ASR::down_cast<ASR::Const_t>(v.m_type )->m_type );
540
546
sub = format_type_c (" " , " const " + const_underlying_type + " " ,
541
547
v.m_name , false , false );
@@ -576,6 +582,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
576
582
list_api->set_global_scope (global_scope);
577
583
c_utils_functions->set_indentation (indentation_level, indentation_spaces);
578
584
c_utils_functions->set_global_scope (global_scope);
585
+ tuple_api->set_indentation (indentation_level, indentation_spaces);
586
+ tuple_api->set_global_scope (global_scope);
579
587
580
588
std::string head =
581
589
R"(
@@ -715,10 +723,16 @@ R"(
715
723
if ( c_utils_functions->get_util_func_decls ().size () > 0 ) {
716
724
array_types_decls += " \n " + c_utils_functions->get_util_func_decls () + " \n " ;
717
725
}
726
+ if ( tuple_api->get_tuple_func_decls ().size () > 0 ) {
727
+ array_types_decls += " \n " + tuple_api->get_tuple_func_decls () + " \n " ;
728
+ }
718
729
std::string list_funcs_defined = " " ;
719
730
if ( list_api->get_generated_code ().size () > 0 ) {
720
731
list_funcs_defined = " \n " + list_api->get_generated_code () + " \n " ;
721
732
}
733
+ if ( tuple_api->get_generated_code ().size () > 0 ) {
734
+ list_funcs_defined = " \n " + tuple_api->get_generated_code () + " \n " ;
735
+ }
722
736
std::string util_funcs_defined = " " ;
723
737
if ( c_utils_functions->get_generated_code ().size () > 0 ) {
724
738
util_funcs_defined = " \n " + c_utils_functions->get_generated_code () + " \n " ;
988
1002
case ASR::ttypeType::CPtr: {
989
1003
return " %p" ;
990
1004
}
1005
+ case ASR::ttypeType::Complex: {
1006
+ return " (%f, %f)" ;
1007
+ }
991
1008
case ASR::ttypeType::Pointer: {
992
1009
if ( !deref_ptr ) {
993
1010
return " %p" ;
@@ -1027,6 +1044,11 @@ R"(
1027
1044
ASR::ttype_t * value_type = ASRUtils::expr_type (x.m_values [i]);
1028
1045
out += get_print_type (value_type, ASR::is_a<ASR::ArrayItem_t>(*x.m_values [i]));
1029
1046
v.push_back (src);
1047
+ if (value_type->type == ASR::ttypeType::Complex) {
1048
+ v.pop_back ();
1049
+ v.push_back (" creal(" + src + " )" );
1050
+ v.push_back (" cimag(" + src + " )" );
1051
+ }
1030
1052
if (i+1 !=x.n_values ) {
1031
1053
out += " \%s" ;
1032
1054
v.push_back (separator);
@@ -1052,7 +1074,7 @@ R"(
1052
1074
visit_expr (*x.m_v );
1053
1075
std::string var_name = src;
1054
1076
std::string args = " " ;
1055
- std::string result_type = get_c_type_from_ttype_t (x.m_type );
1077
+ std::string result_type = CUtils:: get_c_type_from_ttype_t (x.m_type );
1056
1078
if (x.m_dim == nullptr ) {
1057
1079
std::string array_size_func = c_utils_functions->get_array_size ();
1058
1080
ASR::dimension_t * m_dims = nullptr ;
@@ -1072,13 +1094,13 @@ R"(
1072
1094
std::string shape = src;
1073
1095
1074
1096
ASR::ttype_t * array_type_asr = ASRUtils::expr_type (x.m_array );
1075
- std::string array_type_name = get_c_type_from_ttype_t (array_type_asr);
1097
+ std::string array_type_name = CUtils:: get_c_type_from_ttype_t (array_type_asr);
1076
1098
std::string array_encoded_type_name = ASRUtils::get_type_code (array_type_asr, true , false );
1077
1099
std::string array_type = get_array_type (array_type_name, array_encoded_type_name, true );
1078
1100
std::string return_type = get_array_type (array_type_name, array_encoded_type_name, false );
1079
1101
1080
1102
ASR::ttype_t * shape_type_asr = ASRUtils::expr_type (x.m_shape );
1081
- std::string shape_type_name = get_c_type_from_ttype_t (shape_type_asr);
1103
+ std::string shape_type_name = CUtils:: get_c_type_from_ttype_t (shape_type_asr);
1082
1104
std::string shape_encoded_type_name = ASRUtils::get_type_code (shape_type_asr, true , false );
1083
1105
std::string shape_type = get_array_type (shape_type_name, shape_encoded_type_name, true );
1084
1106
@@ -1091,7 +1113,7 @@ R"(
1091
1113
visit_expr (*x.m_v );
1092
1114
std::string var_name = src;
1093
1115
std::string args = " " ;
1094
- std::string result_type = get_c_type_from_ttype_t (x.m_type );
1116
+ std::string result_type = CUtils:: get_c_type_from_ttype_t (x.m_type );
1095
1117
visit_expr (*x.m_dim );
1096
1118
std::string idx = src;
1097
1119
if ( x.m_bound == ASR::arrayboundType::LBound ) {
@@ -1116,7 +1138,7 @@ R"(
1116
1138
array_const.pop_back ();
1117
1139
1118
1140
ASR::ttype_t * array_type_asr = x.m_type ;
1119
- std::string array_type_name = get_c_type_from_ttype_t (array_type_asr);
1141
+ std::string array_type_name = CUtils:: get_c_type_from_ttype_t (array_type_asr);
1120
1142
std::string array_encoded_type_name = ASRUtils::get_type_code (array_type_asr, true , false );
1121
1143
std::string return_type = get_array_type (array_type_name, array_encoded_type_name, false );
1122
1144
0 commit comments