@@ -173,6 +173,24 @@ class CCPPDSUtils {
173
173
global_scope = global_scope_;
174
174
}
175
175
176
+ std::string get_deepcopy (ASR::ttype_t *t, std::string value, std::string target) {
177
+ if (ASR::is_a<ASR::List_t>(*t)) {
178
+ ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(t);
179
+ std::string list_type_code = ASRUtils::get_type_code (list_type->m_type , true );
180
+ std::string func = typecodeToDSfuncs[list_type_code][" list_deepcopy" ];
181
+ return func + " (&" + value + " , &" + target + " );" ;
182
+ } else if (ASR::is_a<ASR::Tuple_t>(*t)) {
183
+ ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(t);
184
+ std::string tup_type_code = CUtils::get_tuple_type_code (tup_type);
185
+ std::string func = typecodeToDSfuncs[tup_type_code][" tuple_deepcopy" ];
186
+ return func + " (" + value + " , &" + target + " );" ;
187
+ } else if (ASR::is_a<ASR::Character_t>(*t)) {
188
+ return " strcpy(" + target + " , " + value + " );" ;
189
+ } else {
190
+ return target + " = " + value + " ;" ;
191
+ }
192
+ }
193
+
176
194
bool is_non_primitive_DT (ASR::ttype_t *t) {
177
195
return ASR::is_a<ASR::List_t>(*t) || ASR::is_a<ASR::Tuple_t>(*t);
178
196
}
@@ -469,14 +487,8 @@ class CCPPDSUtils {
469
487
if ( ASR::is_a<ASR::Character_t>(*m_type) ) {
470
488
generated_code += indent + tab + " x->data[x->current_end_point] = (char*) malloc(40 * sizeof(char));\n " ;
471
489
}
472
- if (ASR::is_a<ASR::List_t>(*m_type)) {
473
- ASR::ttype_t *tt = ASR::down_cast<ASR::List_t>(m_type)->m_type ;
474
- std::string deep_copy_func = typecodeToDSfuncs[ASRUtils::get_type_code (tt, true )][" list_deepcopy" ];
475
- LFORTRAN_ASSERT (deep_copy_func.size () > 0 );
476
- generated_code += indent + tab + deep_copy_func + " (&element, &x->data[x->current_end_point]);\n " ;
477
- } else {
478
- generated_code += indent + tab + deepcopy_function (" x->data[x->current_end_point]" , " element" , m_type) + " \n " ;
479
- }
490
+ generated_code += indent + tab + \
491
+ get_deepcopy (m_type, " element" , " x->data[x->current_end_point]" ) + " \n " ;
480
492
generated_code += indent + tab + " x->current_end_point += 1;\n " ;
481
493
generated_code += indent + " }\n\n " ;
482
494
}
@@ -561,6 +573,12 @@ class CCPPDSUtils {
561
573
generated_code += indent + " }\n\n " ;
562
574
}
563
575
576
+ std::string get_tuple_deepcopy_func (ASR::Tuple_t* tup_type) {
577
+ std::string tuple_type_code = CUtils::get_tuple_type_code (tup_type);
578
+ return typecodeToDSfuncs[tuple_type_code][" tuple_deepcopy" ];
579
+ }
580
+
581
+
564
582
std::string get_tuple_type (ASR::Tuple_t* tuple_type) {
565
583
std::string tuple_type_code = CUtils::get_tuple_type_code (tuple_type);
566
584
if (typecodeToDStype.find (tuple_type_code) != typecodeToDStype.end ()) {
@@ -584,9 +602,35 @@ class CCPPDSUtils {
584
602
tmp_gen += indent + " };\n\n " ;
585
603
func_decls += tmp_gen;
586
604
generate_compare_funcs ((ASR::ttype_t *)tuple_type);
605
+ tuple_deepcopy (tuple_type, tuple_type_code);
587
606
return tuple_struct_type;
588
607
}
589
608
609
+ void tuple_deepcopy (ASR::Tuple_t *t, std::string tuple_type_code) {
610
+ std::string indent (indentation_level * indentation_spaces, ' ' );
611
+ std::string tab (indentation_spaces, ' ' );
612
+ std::string tup_dc_func = global_scope->get_unique_name (" tuple_deepcopy_" + tuple_type_code);
613
+ typecodeToDSfuncs[tuple_type_code][" tuple_deepcopy" ] = tup_dc_func;
614
+ std::string tuple_struct_type = typecodeToDStype[tuple_type_code];
615
+ std::string signature = " void " + tup_dc_func + " ("
616
+ + tuple_struct_type + " src, "
617
+ + tuple_struct_type + " * dest)" ;
618
+ std::string tmp_def = " " , tmp_gen = " " ;
619
+ tmp_def += " inline " + signature + " ;\n " ;
620
+ tmp_gen += indent + signature + " {\n " ;
621
+ for (size_t i=0 ; i<t->n_type ; i++) {
622
+ std::string n = std::to_string (i);
623
+ if (ASR::is_a<ASR::Character_t>(*t->m_type [i])) {
624
+ tmp_gen += indent + tab + " dest->element_" + n + " = " + \
625
+ " (char *) malloc(40*sizeof(char));\n " ;
626
+ }
627
+ tmp_gen += indent + tab + get_deepcopy (t->m_type [i], " src.element_" + n,
628
+ " dest->element_" + n) + " \n " ;
629
+ }
630
+ tmp_gen += indent + " }\n\n " ;
631
+ func_decls += tmp_def;
632
+ generated_code += tmp_gen;
633
+ }
590
634
591
635
~CCPPDSUtils () {
592
636
typecodeToDStype.clear ();
0 commit comments