Skip to content

Commit

Permalink
C: Use const in place of define for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikh-Ubaid committed Jun 20, 2023
1 parent a9c75de commit 2b3ba5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
30 changes: 8 additions & 22 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,10 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type);
sub = format_type_c("", "enum " + std::string(enum_type->m_name), v.m_name, false, false);
} else if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
if( v.m_intent == ASRUtils::intent_local ) {
LCOMPILERS_ASSERT(v.m_symbolic_value);
visit_expr(*v.m_symbolic_value);
sub = "#define " + std::string(v.m_name) + " " + src + "\n";
return sub;
} else {
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
ASR::down_cast<ASR::Const_t>(v_m_type)->m_type);
sub = format_type_c("", "const " + const_underlying_type + " ",
v.m_name, false, false);
}
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
ASRUtils::type_get_past_const(v_m_type));
sub = format_type_c("", "const " + const_underlying_type,
v.m_name, false, false);
} else if (ASR::is_a<ASR::TypeParameter_t>(*v_m_type)) {
// Ignore type variables
return "";
Expand Down Expand Up @@ -702,8 +695,7 @@ R"(
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
unit_src_tmp = convert_variable_decl(*v);
unit_src += unit_src_tmp;
if(unit_src_tmp.size() > 0 && (!ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var )) {
if(unit_src_tmp.size() > 0) {
unit_src += ";\n";
}
}
Expand Down Expand Up @@ -852,9 +844,7 @@ R"(
item.second);
unit_src_tmp = convert_variable_decl(*v);
unit_src += unit_src_tmp;
if(unit_src_tmp.size() > 0 &&
(!ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var )) {
if(unit_src_tmp.size() > 0) {
unit_src += ";\n";
}
}
Expand Down Expand Up @@ -920,8 +910,7 @@ R"(
decl += indent1;
decl_tmp = convert_variable_decl(*v);
decl += decl_tmp;
if(decl_tmp.size() > 0 && (!ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var )) {
if(decl_tmp.size() > 0) {
decl += ";\n";
}
}
Expand Down Expand Up @@ -1007,10 +996,7 @@ R"( // Initialise Numpy
body += indent + convert_variable_decl(
*ASR::down_cast<ASR::Variable_t>(member),
&c_decl_options_);
if( !ASR::is_a<ASR::Const_t>(*ASRUtils::symbol_type(member)) ||
ASR::down_cast<ASR::Variable_t>(member)->m_intent == ASRUtils::intent_return_var ) {
body += ";\n";
}
body += ";\n";
}
indentation_level -= 1;
std::string end_struct = "};\n\n";
Expand Down
18 changes: 3 additions & 15 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,7 @@ R"(#include <stdio.h>
ASR::symbol_t* var_sym = x.m_symtab->get_symbol(item);
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
std::string d = self().convert_variable_decl(*v);
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var ) {
d += ";\n";
}
std::string d = self().convert_variable_decl(*v) + ";\n";
decl += check_tmp_buffer() + d;
}
}
Expand Down Expand Up @@ -354,11 +350,7 @@ R"(#include <stdio.h>
ASR::symbol_t* var_sym = block->m_symtab->get_symbol(item);
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
std::string d = indent + self().convert_variable_decl(*v);
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var ) {
d += ";\n";
}
std::string d = indent + self().convert_variable_decl(*v) + ";\n";
decl += check_tmp_buffer() + d;
}
}
Expand Down Expand Up @@ -673,11 +665,7 @@ R"(#include <stdio.h>
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
if (v->m_intent == ASRUtils::intent_local ||
v->m_intent == ASRUtils::intent_return_var) {
std::string d = indent + self().convert_variable_decl(*v);
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var ) {
d += ";\n";
}
std::string d = indent + self().convert_variable_decl(*v) + ";\n";
if (ASR::is_a<ASR::SymbolicExpression_t>(*v->m_type)) {
std::string v_m_name = v->m_name;
d += indent + "basic_new_stack(" + v_m_name + ");\n";
Expand Down

0 comments on commit 2b3ba5b

Please sign in to comment.