@@ -158,7 +158,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
158
158
counter += 1 ;
159
159
ASR::dimension_t * m_dims = nullptr ;
160
160
size_t n_dims = ASRUtils::extract_dimensions_from_ttype (mem_type, m_dims);
161
- sub += indent + convert_variable_decl (*mem_var, true , true , true , true , mem_var_name) + " ;\n " ;
161
+ CDeclarationOptions c_decl_options_;
162
+ c_decl_options_.pre_initialise_derived_type = true ;
163
+ c_decl_options_.use_ptr_for_derived_type = true ;
164
+ c_decl_options_.use_static = true ;
165
+ c_decl_options_.force_declare = true ;
166
+ c_decl_options_.force_declare_name = mem_var_name;
167
+ sub += indent + convert_variable_decl (*mem_var, &c_decl_options_) + " ;\n " ;
162
168
if ( !ASRUtils::is_fixed_size_array (m_dims, n_dims) ) {
163
169
sub += indent + name + " ->" + itr.first + " = " + mem_var_name + " ;\n " ;
164
170
}
@@ -172,12 +178,34 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
172
178
}
173
179
174
180
std::string convert_variable_decl (const ASR::Variable_t &v,
175
- bool pre_initialise_derived_type=true ,
176
- bool use_ptr_for_derived_type=true ,
177
- bool use_static=true ,
178
- bool force_declare=false ,
179
- std::string force_declare_name=" " )
181
+ DeclarationOptions* decl_options=nullptr )
180
182
{
183
+ bool pre_initialise_derived_type;
184
+ bool use_ptr_for_derived_type;
185
+ bool use_static;
186
+ bool force_declare;
187
+ std::string force_declare_name;
188
+ bool declare_as_constant;
189
+ std::string const_name;
190
+
191
+ if ( decl_options ) {
192
+ CDeclarationOptions* c_decl_options = reinterpret_cast <CDeclarationOptions*>(decl_options);
193
+ pre_initialise_derived_type = c_decl_options->pre_initialise_derived_type ;
194
+ use_ptr_for_derived_type = c_decl_options->use_ptr_for_derived_type ;
195
+ use_static = c_decl_options->use_static ;
196
+ force_declare = c_decl_options->force_declare ;
197
+ force_declare_name = c_decl_options->force_declare_name ;
198
+ declare_as_constant = c_decl_options->declare_as_constant ;
199
+ const_name = c_decl_options->const_name ;
200
+ } else {
201
+ pre_initialise_derived_type = true ;
202
+ use_ptr_for_derived_type = true ;
203
+ use_static = true ;
204
+ force_declare = false ;
205
+ force_declare_name = " " ;
206
+ declare_as_constant = false ;
207
+ const_name = " " ;
208
+ }
181
209
std::string sub;
182
210
bool use_ref = (v.m_intent == LFortran::ASRUtils::intent_out ||
183
211
v.m_intent == LFortran::ASRUtils::intent_inout);
@@ -275,8 +303,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
275
303
}
276
304
} else {
277
305
bool is_fixed_size = true ;
306
+ std::string v_m_name = v.m_name ;
307
+ if ( declare_as_constant ) {
308
+ type_name = " const " + type_name;
309
+ v_m_name = const_name;
310
+ }
278
311
dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type, is_fixed_size);
279
- sub = format_type_c (dims, type_name, v. m_name , use_ref, dummy);
312
+ sub = format_type_c (dims, type_name, v_m_name , use_ref, dummy);
280
313
}
281
314
} else if (ASRUtils::is_real (*v_m_type)) {
282
315
ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(v_m_type);
@@ -307,8 +340,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
307
340
}
308
341
} else {
309
342
bool is_fixed_size = true ;
343
+ std::string v_m_name = v.m_name ;
344
+ if ( declare_as_constant ) {
345
+ type_name = " const " + type_name;
346
+ v_m_name = const_name;
347
+ }
310
348
dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type, is_fixed_size);
311
- sub = format_type_c (dims, type_name, v. m_name , use_ref, dummy);
349
+ sub = format_type_c (dims, type_name, v_m_name , use_ref, dummy);
312
350
}
313
351
} else if (ASRUtils::is_complex (*v_m_type)) {
314
352
headers.insert (" complex" );
@@ -340,8 +378,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
340
378
}
341
379
} else {
342
380
bool is_fixed_size = true ;
381
+ std::string v_m_name = v.m_name ;
382
+ if ( declare_as_constant ) {
383
+ type_name = " const " + type_name;
384
+ v_m_name = const_name;
385
+ }
343
386
dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type, is_fixed_size);
344
- sub = format_type_c (dims, type_name, v. m_name , use_ref, dummy);
387
+ sub = format_type_c (dims, type_name, v_m_name , use_ref, dummy);
345
388
}
346
389
} else if (ASRUtils::is_logical (*v_m_type)) {
347
390
ASR::Logical_t *t = ASR::down_cast<ASR::Logical_t>(v_m_type);
@@ -731,12 +774,15 @@ R"(
731
774
indentation_level += 1 ;
732
775
std::string open_struct = indent + c_type_name + " " + std::string (x.m_name ) + " {\n " ;
733
776
indent.push_back (' ' );
777
+ CDeclarationOptions c_decl_options_;
778
+ c_decl_options_.pre_initialise_derived_type = false ;
779
+ c_decl_options_.use_ptr_for_derived_type = false ;
734
780
for ( size_t i = 0 ; i < x.n_members ; i++ ) {
735
781
ASR::symbol_t * member = x.m_symtab ->get_symbol (x.m_members [i]);
736
782
LFORTRAN_ASSERT (ASR::is_a<ASR::Variable_t>(*member));
737
783
body += indent + convert_variable_decl (
738
784
*ASR::down_cast<ASR::Variable_t>(member),
739
- false , false );
785
+ &c_decl_options_ );
740
786
if ( !ASR::is_a<ASR::Const_t>(*ASRUtils::symbol_type (member)) ||
741
787
ASR::down_cast<ASR::Variable_t>(member)->m_intent == ASRUtils::intent_return_var ) {
742
788
body += " ;\n " ;
0 commit comments