@@ -62,6 +62,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
62
62
bool intrinsic_module = false ;
63
63
const ASR::Function_t *current_function = nullptr ;
64
64
std::map<uint64_t , SymbolInfo> sym_info;
65
+ std::map<uint64_t , std::string> const_var_names;
65
66
66
67
// Output configuration:
67
68
// Use std::string or char*
@@ -80,7 +81,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
80
81
81
82
std::unique_ptr<CCPPDSUtils> c_ds_api;
82
83
std::string const_name;
83
- size_t const_list_count ;
84
+ size_t const_vars_count ;
84
85
85
86
SymbolTable* current_scope;
86
87
bool is_string_concat_present;
@@ -93,7 +94,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
93
94
is_c{is_c}, global_scope{nullptr }, lower_bound{default_lower_bound},
94
95
template_number{0 }, c_ds_api{std::make_unique<CCPPDSUtils>(is_c)},
95
96
const_name{" constname" },
96
- const_list_count {0 }, is_string_concat_present{false } {
97
+ const_vars_count {0 }, is_string_concat_present{false } {
97
98
}
98
99
99
100
void visit_TranslationUnit (const ASR::TranslationUnit_t &x) {
@@ -518,8 +519,8 @@ R"(#include <stdio.h>
518
519
last_expr_precedence = 2 ;
519
520
if ( ASR::is_a<ASR::List_t>(*x.m_type ) ) {
520
521
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(x.m_type );
521
- const_name += std::to_string (const_list_count );
522
- const_list_count += 1 ;
522
+ const_name += std::to_string (const_vars_count );
523
+ const_vars_count += 1 ;
523
524
const_name = current_scope->get_unique_name (const_name);
524
525
std::string indent (indentation_level*indentation_spaces, ' ' );
525
526
current_body += indent + c_ds_api->get_list_type (list_type) + " " +
@@ -628,15 +629,16 @@ R"(#include <stdio.h>
628
629
ASR::TupleConstant_t *tup_c = ASR::down_cast<ASR::TupleConstant_t>(x.m_target );
629
630
std::string src_tmp = " " , val_name = " " ;
630
631
if (ASR::is_a<ASR::TupleConstant_t>(*x.m_value )) {
631
- val_name = const_name + std::to_string (const_list_count);
632
- self ().visit_TupleConstant (*ASR::down_cast<ASR::TupleConstant_t>(x.m_value ));
632
+ ASR::TupleConstant_t *tup_const = ASR::down_cast<ASR::TupleConstant_t>(x.m_value );
633
+ self ().visit_TupleConstant (*tup_const);
634
+ val_name = const_var_names[get_hash ((ASR::asr_t *)tup_const)];
633
635
src_tmp += src;
634
636
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value )) {
635
637
self ().visit_FunctionCall (*ASR::down_cast<ASR::FunctionCall_t>(x.m_value ));
636
638
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(tup_c->m_type );
637
639
std::string tuple_type_c = c_ds_api->get_tuple_type (t);
638
- const_name += std::to_string (const_list_count );
639
- const_list_count += 1 ;
640
+ const_name += std::to_string (const_vars_count );
641
+ const_vars_count += 1 ;
640
642
const_name = current_scope->get_unique_name (const_name);
641
643
src_tmp += indent + tuple_type_c + " " + const_name + " = " + src + " ;\n " ;
642
644
val_name = const_name;
@@ -687,7 +689,9 @@ R"(#include <stdio.h>
687
689
std::string list_dc_func = c_ds_api->get_list_deepcopy_func (list_target);
688
690
if ( ASR::is_a<ASR::ListConstant_t>(*x.m_value ) ) {
689
691
src += value;
690
- src += indent + list_dc_func + " (&" + const_name + " , &" + target + " );\n\n " ;
692
+ ASR::ListConstant_t *l_const = ASR::down_cast<ASR::ListConstant_t>(x.m_value );
693
+ std::string var_name = const_var_names[get_hash ((ASR::asr_t *)l_const)];
694
+ src += indent + list_dc_func + " (&" + var_name + " , &" + target + " );\n\n " ;
691
695
} else if (ASR::is_a<ASR::ListConcat_t>(*x.m_value )) {
692
696
src += indent + list_dc_func + " (" + value + " , &" + target + " );\n\n " ;
693
697
} else {
@@ -768,39 +772,42 @@ R"(#include <stdio.h>
768
772
void visit_ListConstant (const ASR::ListConstant_t& x) {
769
773
std::string indent (indentation_level * indentation_spaces, ' ' );
770
774
std::string tab (indentation_spaces, ' ' );
771
- const_name += std::to_string (const_list_count );
772
- const_list_count += 1 ;
775
+ const_name += std::to_string (const_vars_count );
776
+ const_vars_count += 1 ;
773
777
const_name = current_scope->get_unique_name (const_name);
778
+ std::string var_name = const_name;
779
+ const_var_names[get_hash ((ASR::asr_t *)&x)] = var_name;
774
780
ASR::List_t* t = ASR::down_cast<ASR::List_t>(x.m_type );
775
781
std::string list_type_c = c_ds_api->get_list_type (t);
776
782
std::string src_tmp = " " ;
777
- src_tmp += indent + list_type_c + " " + const_name + " ;\n " ;
783
+ src_tmp += indent + list_type_c + " " + var_name + " ;\n " ;
778
784
std::string list_init_func = c_ds_api->get_list_init_func (t);
779
- src_tmp += indent + list_init_func + " (&" + const_name + " , " +
785
+ src_tmp += indent + list_init_func + " (&" + var_name + " , " +
780
786
std::to_string (x.n_args ) + " );\n " ;
781
787
for ( size_t i = 0 ; i < x.n_args ; i++ ) {
782
788
self ().visit_expr (*x.m_args [i]);
783
789
if ( ASR::is_a<ASR::Character_t>(*t->m_type ) ) {
784
- src_tmp += const_name + " .data[" + std::to_string (i) +" ] = (char*) malloc(40 * sizeof(char));\n " ;
790
+ src_tmp += var_name + " .data[" + std::to_string (i) +" ] = (char*) malloc(40 * sizeof(char));\n " ;
785
791
}
786
792
src_tmp += indent + c_ds_api->get_deepcopy (t->m_type , src,
787
- const_name + " .data[" + std::to_string (i) +" ]" ) + " \n " ;
793
+ var_name + " .data[" + std::to_string (i) +" ]" ) + " \n " ;
788
794
}
789
- src_tmp += indent + const_name + " .current_end_point = " + std::to_string (x.n_args ) + " ;\n " ;
795
+ src_tmp += indent + var_name + " .current_end_point = " + std::to_string (x.n_args ) + " ;\n " ;
790
796
src = src_tmp;
791
797
}
792
798
793
799
void visit_TupleConstant (const ASR::TupleConstant_t& x) {
794
800
std::string indent (indentation_level * indentation_spaces, ' ' );
795
801
std::string tab (indentation_spaces, ' ' );
796
- const_name += std::to_string (const_list_count );
797
- const_list_count += 1 ;
802
+ const_name += std::to_string (const_vars_count );
803
+ const_vars_count += 1 ;
798
804
const_name = current_scope->get_unique_name (const_name);
799
805
std::string var_name = const_name;
806
+ const_var_names[get_hash ((ASR::asr_t *)&x)] = var_name;
800
807
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(x.m_type );
801
808
std::string tuple_type_c = c_ds_api->get_tuple_type (t);
802
809
std::string src_tmp = " " ;
803
- src_tmp += indent + tuple_type_c + " " + const_name + " ;\n " ;
810
+ src_tmp += indent + tuple_type_c + " " + var_name + " ;\n " ;
804
811
for (size_t i = 0 ; i < x.n_elements ; i++) {
805
812
self ().visit_expr (*x.m_elements [i]);
806
813
std::string ele = " .element_" + std::to_string (i);
0 commit comments