Skip to content

Commit f1b31d8

Browse files
committed
Use constant variables to declare import constants inside a Function
1 parent 45b1b3b commit f1b31d8

File tree

3 files changed

+117
-17
lines changed

3 files changed

+117
-17
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
158158
counter += 1;
159159
ASR::dimension_t* m_dims = nullptr;
160160
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";
162168
if( !ASRUtils::is_fixed_size_array(m_dims, n_dims) ) {
163169
sub += indent + name + "->" + itr.first + " = " + mem_var_name + ";\n";
164170
}
@@ -172,12 +178,34 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
172178
}
173179

174180
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)
180182
{
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+
}
181209
std::string sub;
182210
bool use_ref = (v.m_intent == LFortran::ASRUtils::intent_out ||
183211
v.m_intent == LFortran::ASRUtils::intent_inout);
@@ -275,8 +303,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
275303
}
276304
} else {
277305
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+
}
278311
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);
280313
}
281314
} else if (ASRUtils::is_real(*v_m_type)) {
282315
ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(v_m_type);
@@ -307,8 +340,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
307340
}
308341
} else {
309342
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+
}
310348
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);
312350
}
313351
} else if (ASRUtils::is_complex(*v_m_type)) {
314352
headers.insert("complex");
@@ -340,8 +378,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
340378
}
341379
} else {
342380
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+
}
343386
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);
345388
}
346389
} else if (ASRUtils::is_logical(*v_m_type)) {
347390
ASR::Logical_t *t = ASR::down_cast<ASR::Logical_t>(v_m_type);
@@ -731,12 +774,15 @@ R"(
731774
indentation_level += 1;
732775
std::string open_struct = indent + c_type_name + " " + std::string(x.m_name) + " {\n";
733776
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;
734780
for( size_t i = 0; i < x.n_members; i++ ) {
735781
ASR::symbol_t* member = x.m_symtab->get_symbol(x.m_members[i]);
736782
LFORTRAN_ASSERT(ASR::is_a<ASR::Variable_t>(*member));
737783
body += indent + convert_variable_decl(
738784
*ASR::down_cast<ASR::Variable_t>(member),
739-
false, false);
785+
&c_decl_options_);
740786
if( !ASR::is_a<ASR::Const_t>(*ASRUtils::symbol_type(member)) ||
741787
ASR::down_cast<ASR::Variable_t>(member)->m_intent == ASRUtils::intent_return_var ) {
742788
body += ";\n";

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
namespace LFortran {
3232

33-
3433
// Platform dependent fast unique hash:
3534
static inline uint64_t get_hash(ASR::asr_t *node)
3635
{
@@ -43,6 +42,38 @@ struct SymbolInfo
4342
bool intrinsic_function = false;
4443
};
4544

45+
struct DeclarationOptions {
46+
};
47+
48+
struct CDeclarationOptions: public DeclarationOptions {
49+
bool pre_initialise_derived_type;
50+
bool use_ptr_for_derived_type;
51+
bool use_static;
52+
bool force_declare;
53+
std::string force_declare_name;
54+
bool declare_as_constant;
55+
std::string const_name;
56+
57+
CDeclarationOptions() :
58+
pre_initialise_derived_type{true},
59+
use_ptr_for_derived_type{true},
60+
use_static{true},
61+
force_declare{false},
62+
force_declare_name{""},
63+
declare_as_constant{false},
64+
const_name{""} {
65+
}
66+
};
67+
68+
struct CPPDeclarationOptions: public DeclarationOptions {
69+
bool use_static;
70+
bool use_templates_for_arrays;
71+
72+
CPPDeclarationOptions() :
73+
use_static{true},
74+
use_templates_for_arrays{false} {
75+
}
76+
};
4677

4778
template <class Struct>
4879
class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
@@ -376,9 +407,14 @@ R"(#include <stdio.h>
376407
ASR::Variable_t *arg = LFortran::ASRUtils::EXPR2VAR(x.m_args[i]);
377408
LFORTRAN_ASSERT(LFortran::ASRUtils::is_arg_dummy(arg->m_intent));
378409
if( is_c ) {
379-
func += self().convert_variable_decl(*arg, false);
410+
CDeclarationOptions c_decl_options;
411+
c_decl_options.pre_initialise_derived_type = false;
412+
func += self().convert_variable_decl(*arg, &c_decl_options);
380413
} else {
381-
func += self().convert_variable_decl(*arg, false, true);
414+
CPPDeclarationOptions cpp_decl_options;
415+
cpp_decl_options.use_static = false;
416+
cpp_decl_options.use_templates_for_arrays = true;
417+
func += self().convert_variable_decl(*arg, &cpp_decl_options);
382418
}
383419
if (i < x.n_args-1) func += ", ";
384420
}
@@ -466,8 +502,15 @@ R"(#include <stdio.h>
466502
if (ASR::is_a<ASR::Variable_t>(*v_sym)) {
467503
ASR::Variable_t* v = ASR::down_cast<ASR::Variable_t>(v_sym);
468504
if( v->m_symbolic_value ) {
469-
this->visit_expr(*v->m_symbolic_value);
470-
decl += indent + "#define " + std::string(v_ext->m_name) + " " + src + "\n";
505+
if( is_c ) {
506+
CDeclarationOptions c_decl_options;
507+
c_decl_options.declare_as_constant = true;
508+
c_decl_options.const_name = v_ext->m_name;
509+
decl += indent + self().convert_variable_decl(*v, &c_decl_options) + ";\n";
510+
} else {
511+
// TODO: Do for CPP when the use case shows up
512+
decl += indent + self().convert_variable_decl(*v) + ";\n";
513+
}
471514
src.clear();
472515
}
473516
}

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,20 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
209209
return typename_T + "* " + v_name;
210210
}
211211

212-
std::string convert_variable_decl(const ASR::Variable_t &v, bool use_static=true,
213-
bool use_templates_for_arrays=false)
212+
std::string convert_variable_decl(const ASR::Variable_t &v, DeclarationOptions* decl_options=nullptr)
214213
{
214+
bool use_static;
215+
bool use_templates_for_arrays;
216+
217+
if( decl_options ) {
218+
CPPDeclarationOptions* cpp_decl_options = reinterpret_cast<CPPDeclarationOptions*>(decl_options);
219+
use_static = cpp_decl_options->use_static;
220+
use_templates_for_arrays = cpp_decl_options->use_templates_for_arrays;
221+
} else {
222+
use_static = true;
223+
use_templates_for_arrays = false;
224+
}
225+
215226
std::string sub;
216227
bool use_ref = (v.m_intent == LFortran::ASRUtils::intent_out ||
217228
v.m_intent == LFortran::ASRUtils::intent_inout ||

0 commit comments

Comments
 (0)