Skip to content

Commit 8820e02

Browse files
authored
Merge pull request #2636 from Shaikh-Ubaid/remove_const
Remove `const` ttype
2 parents c0446d6 + e88ce35 commit 8820e02

15 files changed

+98
-267
lines changed

src/libasr/ASR.asdl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ ttype
201201
| Dict(ttype key_type, ttype value_type)
202202
| Pointer(ttype type)
203203
| Allocatable(ttype type)
204-
| Const(ttype type)
205204
| CPtr()
206205
| SymbolicExpression()
207206
| TypeParameter(identifier param)

src/libasr/asr_utils.h

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,6 @@ static inline const ASR::symbol_t *symbol_get_past_external(const ASR::symbol_t
152152
}
153153
}
154154

155-
static inline ASR::ttype_t *type_get_past_const(ASR::ttype_t *f)
156-
{
157-
if (ASR::is_a<ASR::Const_t>(*f)) {
158-
ASR::Const_t *e = ASR::down_cast<ASR::Const_t>(f);
159-
LCOMPILERS_ASSERT(!ASR::is_a<ASR::Const_t>(*e->m_type));
160-
return e->m_type;
161-
} else {
162-
return f;
163-
}
164-
}
165-
166155
static inline ASR::ttype_t *type_get_past_pointer(ASR::ttype_t *f)
167156
{
168157
if (ASR::is_a<ASR::Pointer_t>(*f)) {
@@ -227,9 +216,6 @@ static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
227216
case ASR::ttypeType::Allocatable: {
228217
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type);
229218
}
230-
case ASR::ttypeType::Const: {
231-
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
232-
}
233219
default : {
234220
return -1;
235221
}
@@ -277,10 +263,6 @@ static inline void set_kind_to_ttype_t(ASR::ttype_t* type, int kind) {
277263
set_kind_to_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type, kind);
278264
break;
279265
}
280-
case ASR::ttypeType::Const: {
281-
set_kind_to_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type, kind);
282-
break;
283-
}
284266
default : {
285267
return;
286268
}
@@ -392,10 +374,6 @@ static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type, int overl
392374
ASR::Allocatable_t* pointer_asr = ASR::down_cast<ASR::Allocatable_t>(asr_type);
393375
return pointer_asr->m_type;
394376
}
395-
case ASR::ttypeType::Const: {
396-
ASR::Const_t* const_asr = ASR::down_cast<ASR::Const_t>(asr_type);
397-
return const_asr->m_type;
398-
}
399377
default: {
400378
return asr_type;
401379
}
@@ -602,10 +580,6 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
602580
encode_dimensions(array_t->n_dims, res, false);
603581
return res;
604582
}
605-
case ASR::ttypeType::Const: {
606-
return type_to_str(ASRUtils::get_contained_type(
607-
const_cast<ASR::ttype_t*>(t))) + " const";
608-
}
609583
case ASR::ttypeType::TypeParameter: {
610584
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(t);
611585
return tp->m_param;
@@ -660,10 +634,6 @@ static inline std::string type_to_str_with_substitution(const ASR::ttype_t *t,
660634
encode_dimensions(array_t->n_dims, res, false);
661635
return res;
662636
}
663-
case ASR::ttypeType::Const: {
664-
return type_to_str_with_substitution(ASRUtils::get_contained_type(
665-
const_cast<ASR::ttype_t*>(t)), subs) + " const";
666-
}
667637
case ASR::ttypeType::FunctionType: {
668638
ASR::FunctionType_t* ftp = ASR::down_cast<ASR::FunctionType_t>(t);
669639
std::string result = "(";
@@ -1552,15 +1522,6 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
15521522
return "Allocatable[" + get_type_code(p->m_type, use_underscore_sep,
15531523
encode_dimensions_, set_dimensional_hint) + "]";
15541524
}
1555-
case ASR::ttypeType::Const: {
1556-
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
1557-
if( use_underscore_sep ) {
1558-
return "Const_" + get_type_code(p->m_type, use_underscore_sep,
1559-
encode_dimensions_, set_dimensional_hint) + "_";
1560-
}
1561-
return "Const[" + get_type_code(p->m_type, use_underscore_sep,
1562-
encode_dimensions_, set_dimensional_hint) + "]";
1563-
}
15641525
case ASR::ttypeType::SymbolicExpression: {
15651526
return "S";
15661527
}
@@ -1694,10 +1655,6 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
16941655
ASR::Allocatable_t* p = ASR::down_cast<ASR::Allocatable_t>(t);
16951656
return "Allocatable[" + type_to_str_python(p->m_type) + "]";
16961657
}
1697-
case ASR::ttypeType::Const: {
1698-
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
1699-
return "Const[" + type_to_str_python(p->m_type) + "]";
1700-
}
17011658
case ASR::ttypeType::TypeParameter: {
17021659
ASR::TypeParameter_t *p = ASR::down_cast<ASR::TypeParameter_t>(t);
17031660
return p->m_param;
@@ -2008,6 +1965,18 @@ bool use_overloaded_file_read_write(std::string &read_write, Vec<ASR::expr_t*> a
20081965

20091966
void set_intrinsic(ASR::symbol_t* sym);
20101967

1968+
static inline bool is_const(ASR::expr_t *x) {
1969+
if (ASR::is_a<ASR::Var_t>(*x)) {
1970+
ASR::Var_t* v = ASR::down_cast<ASR::Var_t>(x);
1971+
ASR::symbol_t* sym = ASRUtils::symbol_get_past_external(v->m_v);
1972+
if (sym && ASR::is_a<ASR::Variable_t>(*sym)) {
1973+
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(sym);
1974+
return var->m_storage == ASR::storage_typeType::Parameter;
1975+
}
1976+
}
1977+
return false;
1978+
}
1979+
20111980
static inline bool is_pointer(ASR::ttype_t *x) {
20121981
return ASR::is_a<ASR::Pointer_t>(*x);
20131982
}
@@ -2020,10 +1989,9 @@ static inline bool is_integer(ASR::ttype_t &x) {
20201989
// type_get_past_pointer(
20211990
// type_get_past_const(&x))))));
20221991
return ASR::is_a<ASR::Integer_t>(
2023-
*type_get_past_const(
2024-
type_get_past_array(
1992+
*type_get_past_array(
20251993
type_get_past_allocatable(
2026-
type_get_past_pointer(&x)))));
1994+
type_get_past_pointer(&x))));
20271995
}
20281996

20291997
static inline bool is_unsigned_integer(ASR::ttype_t &x) {
@@ -2174,10 +2142,6 @@ inline size_t extract_dimensions_from_ttype(ASR::ttype_t *x,
21742142
n_dims = extract_dimensions_from_ttype(ASR::down_cast<ASR::Allocatable_t>(x)->m_type, m_dims);
21752143
break;
21762144
}
2177-
case ASR::ttypeType::Const: {
2178-
n_dims = extract_dimensions_from_ttype(ASR::down_cast<ASR::Const_t>(x)->m_type, m_dims);
2179-
break;
2180-
}
21812145
case ASR::ttypeType::SymbolicExpression:
21822146
case ASR::ttypeType::Integer:
21832147
case ASR::ttypeType::UnsignedInteger:
@@ -2476,9 +2440,6 @@ inline bool is_array(ASR::ttype_t *x) {
24762440
}
24772441

24782442
static inline bool is_aggregate_type(ASR::ttype_t* asr_type) {
2479-
if( ASR::is_a<ASR::Const_t>(*asr_type) ) {
2480-
asr_type = ASR::down_cast<ASR::Const_t>(asr_type)->m_type;
2481-
}
24822443
return ASRUtils::is_array(asr_type) ||
24832444
!(ASR::is_a<ASR::Integer_t>(*asr_type) ||
24842445
ASR::is_a<ASR::UnsignedInteger_t>(*asr_type) ||
@@ -2581,12 +2542,6 @@ static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
25812542
ASR::CPtr_t* ptr = ASR::down_cast<ASR::CPtr_t>(t);
25822543
return ASRUtils::TYPE(ASR::make_CPtr_t(al, ptr->base.base.loc));
25832544
}
2584-
case ASR::ttypeType::Const: {
2585-
ASR::Const_t* c = ASR::down_cast<ASR::Const_t>(t);
2586-
ASR::ttype_t* dup_type = duplicate_type(al, c->m_type, dims);
2587-
return ASRUtils::TYPE(ASR::make_Const_t(al, c->base.base.loc,
2588-
dup_type));
2589-
}
25902545
case ASR::ttypeType::List: {
25912546
ASR::List_t* l = ASR::down_cast<ASR::List_t>(t);
25922547
ASR::ttype_t* dup_type = duplicate_type(al, l->m_type);
@@ -3442,11 +3397,6 @@ inline bool check_equal_type(ASR::ttype_t* x, ASR::ttype_t* y, bool check_for_di
34423397
x = ASRUtils::type_get_past_allocatable(x);
34433398
y = ASRUtils::type_get_past_allocatable(y);
34443399
return check_equal_type(x, y);
3445-
} else if(ASR::is_a<ASR::Const_t>(*x) ||
3446-
ASR::is_a<ASR::Const_t>(*y)) {
3447-
x = ASRUtils::get_contained_type(x);
3448-
y = ASRUtils::get_contained_type(y);
3449-
return check_equal_type(x, y);
34503400
} else if (ASR::is_a<ASR::List_t>(*x) && ASR::is_a<ASR::List_t>(*y)) {
34513401
x = ASR::down_cast<ASR::List_t>(x)->m_type;
34523402
y = ASR::down_cast<ASR::List_t>(y)->m_type;
@@ -5189,9 +5139,9 @@ static inline void Call_t_body(Allocator& al, ASR::symbol_t* a_name,
51895139
}
51905140
if( ASRUtils::is_array(arg_type) && ASRUtils::is_array(orig_arg_type) ) {
51915141
ASR::Array_t* arg_array_t = ASR::down_cast<ASR::Array_t>(
5192-
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(arg_type)));
5142+
ASRUtils::type_get_past_pointer(arg_type));
51935143
ASR::Array_t* orig_arg_array_t = ASR::down_cast<ASR::Array_t>(
5194-
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(orig_arg_type)));
5144+
ASRUtils::type_get_past_pointer(orig_arg_type));
51955145
if( (arg_array_t->m_physical_type != orig_arg_array_t->m_physical_type) ||
51965146
(arg_array_t->m_physical_type == ASR::array_physical_typeType::DescriptorArray &&
51975147
arg_array_t->m_physical_type == orig_arg_array_t->m_physical_type &&

src/libasr/asr_verify.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,15 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
361361
ASR::expr_t* target = x.m_target;
362362
if( ASR::is_a<ASR::Var_t>(*target) ) {
363363
ASR::Var_t* target_Var = ASR::down_cast<ASR::Var_t>(target);
364+
bool is_target_const = false;
364365
ASR::ttype_t* target_type = nullptr;
365-
if( ASR::is_a<ASR::Variable_t>(*target_Var->m_v) ||
366-
(ASR::is_a<ASR::ExternalSymbol_t>(*target_Var->m_v) &&
367-
ASR::down_cast<ASR::ExternalSymbol_t>(target_Var->m_v)->m_external) ) {
368-
target_type = ASRUtils::expr_type(target);
366+
ASR::symbol_t* target_sym = ASRUtils::symbol_get_past_external(target_Var->m_v);
367+
if( target_sym && ASR::is_a<ASR::Variable_t>(*target_sym) ) {
368+
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(target_sym);
369+
target_type = var->m_type;
370+
is_target_const = var->m_storage == ASR::storage_typeType::Parameter;
369371
}
370-
if( target_type && ASR::is_a<ASR::Const_t>(*target_type) ) {
372+
if( is_target_const ) {
371373
std::string variable_name = ASRUtils::symbol_name(target_Var->m_v);
372374
require(const_assigned.find(std::make_pair(current_symtab->counter,
373375
variable_name)) == const_assigned.end(),
@@ -1163,14 +1165,14 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
11631165
void visit_dimension(const dimension_t &x) {
11641166
if (x.m_start) {
11651167
require_with_loc(ASRUtils::is_integer(
1166-
*ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_start))),
1168+
*ASRUtils::expr_type(x.m_start)),
11671169
"Start dimension must be a signed integer", x.loc);
11681170
visit_expr(*x.m_start);
11691171
}
11701172

11711173
if (x.m_length) {
11721174
require_with_loc(ASRUtils::is_integer(
1173-
*ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_length))),
1175+
*ASRUtils::expr_type(x.m_length)),
11741176
"Length dimension must be a signed integer", x.loc);
11751177
visit_expr(*x.m_length);
11761178
}

src/libasr/casting_utils.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ namespace LCompilers::CastingUtil {
5858
bool is_assign, bool allow_int_to_float) {
5959
ASR::ttype_t* left_type = ASRUtils::expr_type(left_expr);
6060
ASR::ttype_t* right_type = ASRUtils::expr_type(right_expr);
61-
if( ASR::is_a<ASR::Const_t>(*left_type) ) {
62-
left_type = ASRUtils::get_contained_type(left_type);
63-
}
64-
if( ASR::is_a<ASR::Const_t>(*right_type) ) {
65-
right_type = ASRUtils::get_contained_type(right_type);
66-
}
6761
left_type = ASRUtils::type_get_past_pointer(left_type);
6862
right_type = ASRUtils::type_get_past_pointer(right_type);
6963
if( ASRUtils::check_equal_type(left_type, right_type) ||
@@ -121,9 +115,6 @@ namespace LCompilers::CastingUtil {
121115
ASR::ttype_t* dest, Allocator& al,
122116
const Location& loc) {
123117
ASR::ttype_t* src = ASRUtils::expr_type(expr);
124-
if( ASR::is_a<ASR::Const_t>(*src) ) {
125-
src = ASRUtils::get_contained_type(src);
126-
}
127118
// TODO: Uncomment the following
128119
// ASR::ttypeType src_type = ASRUtils::extract_type(src)->type;
129120
// ASR::ttypeType dest_type = ASRUtils::extract_type(dest)->type;

src/libasr/codegen/asr_to_c.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,6 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
275275
ASR::dimension_t* m_dims = nullptr;
276276
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(v.m_type, m_dims);
277277
ASR::ttype_t* v_m_type = v.m_type;
278-
if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
279-
if( is_array ) {
280-
v_m_type = ASR::down_cast<ASR::Const_t>(v_m_type)->m_type;
281-
}
282-
}
283278
v_m_type = ASRUtils::type_get_past_array(ASRUtils::type_get_past_allocatable(v_m_type));
284279
if (ASRUtils::is_pointer(v_m_type)) {
285280
ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(v_m_type)->m_type;
@@ -400,7 +395,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
400395
} else {
401396
std::string dims;
402397
use_ref = use_ref && !is_array;
403-
if (ASRUtils::is_integer(*v_m_type)) {
398+
if (v.m_storage == ASR::storage_typeType::Parameter) {
399+
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
400+
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
401+
if (v.m_intent != ASR::intentType::ReturnVar) {
402+
sub = "const " + sub;
403+
}
404+
} else if (ASRUtils::is_integer(*v_m_type)) {
404405
headers.insert("inttypes.h");
405406
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
406407
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
@@ -546,11 +547,6 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
546547
ASR::Enum_t* enum_ = ASR::down_cast<ASR::Enum_t>(v_m_type);
547548
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type);
548549
sub = format_type_c("", "enum " + std::string(enum_type->m_name), v.m_name, false, false);
549-
} else if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
550-
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
551-
ASRUtils::type_get_past_const(v_m_type));
552-
sub = format_type_c("", "const " + const_underlying_type,
553-
v.m_name, false, false);
554550
} else if (ASR::is_a<ASR::TypeParameter_t>(*v_m_type)) {
555551
// Ignore type variables
556552
return "";
@@ -565,7 +561,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
565561
}
566562
if (dims.size() == 0 && v.m_symbolic_value && !do_not_initialize) {
567563
ASR::expr_t* init_expr = v.m_symbolic_value;
568-
if( !ASR::is_a<ASR::Const_t>(*v.m_type) ) {
564+
if( v.m_storage != ASR::storage_typeType::Parameter ) {
569565
for( size_t i = 0; i < v.n_dependencies; i++ ) {
570566
std::string variable_name = v.m_dependencies[i];
571567
ASR::symbol_t* dep_sym = current_scope->resolve_symbol(variable_name);
@@ -1367,7 +1363,7 @@ R"( // Initialise Numpy
13671363
}
13681364

13691365
ASR::ttype_t* x_mv_type_ = ASRUtils::type_get_past_allocatable(
1370-
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(x_mv_type)));
1366+
ASRUtils::type_get_past_pointer(x_mv_type));
13711367
LCOMPILERS_ASSERT(ASR::is_a<ASR::Array_t>(*x_mv_type_));
13721368
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(x_mv_type_);
13731369
std::vector<std::string> diminfo;

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ R"(#include <stdio.h>
311311
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
312312
std::string decl = self().convert_variable_decl(*v);
313313
decl = check_tmp_buffer() + decl;
314-
bool used_define_for_const = (ASR::is_a<ASR::Const_t>(*v->m_type) &&
314+
bool used_define_for_const = (v->m_storage == ASR::storage_typeType::Parameter &&
315315
v->m_intent == ASRUtils::intent_local);
316316
if (used_define_for_const) {
317317
contains += decl + "\n";
@@ -496,10 +496,6 @@ R"(#include <stdio.h>
496496
} else if (ASR::is_a<ASR::Tuple_t>(*return_var->m_type)) {
497497
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(return_var->m_type);
498498
sub = c_ds_api->get_tuple_type(tup_type) + " ";
499-
} else if (ASR::is_a<ASR::Const_t>(*return_var->m_type)) {
500-
ASR::Const_t* const_type = ASR::down_cast<ASR::Const_t>(return_var->m_type);
501-
std::string const_type_str = CUtils::get_c_type_from_ttype_t(const_type->m_type);
502-
sub = "const " + const_type_str + " ";
503499
} else if (ASR::is_a<ASR::Pointer_t>(*return_var->m_type)) {
504500
ASR::Pointer_t* ptr_type = ASR::down_cast<ASR::Pointer_t>(return_var->m_type);
505501
std::string pointer_type_str = CUtils::get_c_type_from_ttype_t(ptr_type->m_type);
@@ -721,8 +717,6 @@ R"(#include <stdio.h>
721717
}
722718
} case ASR::ttypeType::Logical : {
723719
return "p";
724-
} case ASR::ttypeType::Const : {
725-
return get_type_format(ASR::down_cast<ASR::Const_t>(type)->m_type);
726720
} case ASR::ttypeType::Array : {
727721
return "O";
728722
} default: {
@@ -1252,10 +1246,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
12521246
void visit_Assignment(const ASR::Assignment_t &x) {
12531247
std::string target;
12541248
ASR::ttype_t* m_target_type = ASRUtils::expr_type(x.m_target);
1255-
if( ASR::is_a<ASR::Const_t>(*m_target_type) ) {
1256-
src = "";
1257-
return ;
1258-
}
12591249
ASR::ttype_t* m_value_type = ASRUtils::expr_type(x.m_value);
12601250
bool is_target_list = ASR::is_a<ASR::List_t>(*m_target_type);
12611251
bool is_value_list = ASR::is_a<ASR::List_t>(*m_value_type);

0 commit comments

Comments
 (0)