Skip to content

Commit

Permalink
Merge pull request lcompilers#2636 from Shaikh-Ubaid/remove_const
Browse files Browse the repository at this point in the history
Remove `const` ttype
  • Loading branch information
Shaikh-Ubaid committed Mar 29, 2024
2 parents c0446d6 + e88ce35 commit 8820e02
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 267 deletions.
1 change: 0 additions & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ ttype
| Dict(ttype key_type, ttype value_type)
| Pointer(ttype type)
| Allocatable(ttype type)
| Const(ttype type)
| CPtr()
| SymbolicExpression()
| TypeParameter(identifier param)
Expand Down
82 changes: 16 additions & 66 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,6 @@ static inline const ASR::symbol_t *symbol_get_past_external(const ASR::symbol_t
}
}

static inline ASR::ttype_t *type_get_past_const(ASR::ttype_t *f)
{
if (ASR::is_a<ASR::Const_t>(*f)) {
ASR::Const_t *e = ASR::down_cast<ASR::Const_t>(f);
LCOMPILERS_ASSERT(!ASR::is_a<ASR::Const_t>(*e->m_type));
return e->m_type;
} else {
return f;
}
}

static inline ASR::ttype_t *type_get_past_pointer(ASR::ttype_t *f)
{
if (ASR::is_a<ASR::Pointer_t>(*f)) {
Expand Down Expand Up @@ -227,9 +216,6 @@ static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
case ASR::ttypeType::Allocatable: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type);
}
case ASR::ttypeType::Const: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
}
default : {
return -1;
}
Expand Down Expand Up @@ -277,10 +263,6 @@ static inline void set_kind_to_ttype_t(ASR::ttype_t* type, int kind) {
set_kind_to_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type, kind);
break;
}
case ASR::ttypeType::Const: {
set_kind_to_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type, kind);
break;
}
default : {
return;
}
Expand Down Expand Up @@ -392,10 +374,6 @@ static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type, int overl
ASR::Allocatable_t* pointer_asr = ASR::down_cast<ASR::Allocatable_t>(asr_type);
return pointer_asr->m_type;
}
case ASR::ttypeType::Const: {
ASR::Const_t* const_asr = ASR::down_cast<ASR::Const_t>(asr_type);
return const_asr->m_type;
}
default: {
return asr_type;
}
Expand Down Expand Up @@ -602,10 +580,6 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
encode_dimensions(array_t->n_dims, res, false);
return res;
}
case ASR::ttypeType::Const: {
return type_to_str(ASRUtils::get_contained_type(
const_cast<ASR::ttype_t*>(t))) + " const";
}
case ASR::ttypeType::TypeParameter: {
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(t);
return tp->m_param;
Expand Down Expand Up @@ -660,10 +634,6 @@ static inline std::string type_to_str_with_substitution(const ASR::ttype_t *t,
encode_dimensions(array_t->n_dims, res, false);
return res;
}
case ASR::ttypeType::Const: {
return type_to_str_with_substitution(ASRUtils::get_contained_type(
const_cast<ASR::ttype_t*>(t)), subs) + " const";
}
case ASR::ttypeType::FunctionType: {
ASR::FunctionType_t* ftp = ASR::down_cast<ASR::FunctionType_t>(t);
std::string result = "(";
Expand Down Expand Up @@ -1552,15 +1522,6 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
return "Allocatable[" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "]";
}
case ASR::ttypeType::Const: {
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
if( use_underscore_sep ) {
return "Const_" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "_";
}
return "Const[" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "]";
}
case ASR::ttypeType::SymbolicExpression: {
return "S";
}
Expand Down Expand Up @@ -1694,10 +1655,6 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
ASR::Allocatable_t* p = ASR::down_cast<ASR::Allocatable_t>(t);
return "Allocatable[" + type_to_str_python(p->m_type) + "]";
}
case ASR::ttypeType::Const: {
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
return "Const[" + type_to_str_python(p->m_type) + "]";
}
case ASR::ttypeType::TypeParameter: {
ASR::TypeParameter_t *p = ASR::down_cast<ASR::TypeParameter_t>(t);
return p->m_param;
Expand Down Expand Up @@ -2008,6 +1965,18 @@ bool use_overloaded_file_read_write(std::string &read_write, Vec<ASR::expr_t*> a

void set_intrinsic(ASR::symbol_t* sym);

static inline bool is_const(ASR::expr_t *x) {
if (ASR::is_a<ASR::Var_t>(*x)) {
ASR::Var_t* v = ASR::down_cast<ASR::Var_t>(x);
ASR::symbol_t* sym = ASRUtils::symbol_get_past_external(v->m_v);
if (sym && ASR::is_a<ASR::Variable_t>(*sym)) {
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(sym);
return var->m_storage == ASR::storage_typeType::Parameter;
}
}
return false;
}

static inline bool is_pointer(ASR::ttype_t *x) {
return ASR::is_a<ASR::Pointer_t>(*x);
}
Expand All @@ -2020,10 +1989,9 @@ static inline bool is_integer(ASR::ttype_t &x) {
// type_get_past_pointer(
// type_get_past_const(&x))))));
return ASR::is_a<ASR::Integer_t>(
*type_get_past_const(
type_get_past_array(
*type_get_past_array(
type_get_past_allocatable(
type_get_past_pointer(&x)))));
type_get_past_pointer(&x))));
}

static inline bool is_unsigned_integer(ASR::ttype_t &x) {
Expand Down Expand Up @@ -2174,10 +2142,6 @@ inline size_t extract_dimensions_from_ttype(ASR::ttype_t *x,
n_dims = extract_dimensions_from_ttype(ASR::down_cast<ASR::Allocatable_t>(x)->m_type, m_dims);
break;
}
case ASR::ttypeType::Const: {
n_dims = extract_dimensions_from_ttype(ASR::down_cast<ASR::Const_t>(x)->m_type, m_dims);
break;
}
case ASR::ttypeType::SymbolicExpression:
case ASR::ttypeType::Integer:
case ASR::ttypeType::UnsignedInteger:
Expand Down Expand Up @@ -2476,9 +2440,6 @@ inline bool is_array(ASR::ttype_t *x) {
}

static inline bool is_aggregate_type(ASR::ttype_t* asr_type) {
if( ASR::is_a<ASR::Const_t>(*asr_type) ) {
asr_type = ASR::down_cast<ASR::Const_t>(asr_type)->m_type;
}
return ASRUtils::is_array(asr_type) ||
!(ASR::is_a<ASR::Integer_t>(*asr_type) ||
ASR::is_a<ASR::UnsignedInteger_t>(*asr_type) ||
Expand Down Expand Up @@ -2581,12 +2542,6 @@ static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
ASR::CPtr_t* ptr = ASR::down_cast<ASR::CPtr_t>(t);
return ASRUtils::TYPE(ASR::make_CPtr_t(al, ptr->base.base.loc));
}
case ASR::ttypeType::Const: {
ASR::Const_t* c = ASR::down_cast<ASR::Const_t>(t);
ASR::ttype_t* dup_type = duplicate_type(al, c->m_type, dims);
return ASRUtils::TYPE(ASR::make_Const_t(al, c->base.base.loc,
dup_type));
}
case ASR::ttypeType::List: {
ASR::List_t* l = ASR::down_cast<ASR::List_t>(t);
ASR::ttype_t* dup_type = duplicate_type(al, l->m_type);
Expand Down Expand Up @@ -3442,11 +3397,6 @@ inline bool check_equal_type(ASR::ttype_t* x, ASR::ttype_t* y, bool check_for_di
x = ASRUtils::type_get_past_allocatable(x);
y = ASRUtils::type_get_past_allocatable(y);
return check_equal_type(x, y);
} else if(ASR::is_a<ASR::Const_t>(*x) ||
ASR::is_a<ASR::Const_t>(*y)) {
x = ASRUtils::get_contained_type(x);
y = ASRUtils::get_contained_type(y);
return check_equal_type(x, y);
} else if (ASR::is_a<ASR::List_t>(*x) && ASR::is_a<ASR::List_t>(*y)) {
x = ASR::down_cast<ASR::List_t>(x)->m_type;
y = ASR::down_cast<ASR::List_t>(y)->m_type;
Expand Down Expand Up @@ -5189,9 +5139,9 @@ static inline void Call_t_body(Allocator& al, ASR::symbol_t* a_name,
}
if( ASRUtils::is_array(arg_type) && ASRUtils::is_array(orig_arg_type) ) {
ASR::Array_t* arg_array_t = ASR::down_cast<ASR::Array_t>(
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(arg_type)));
ASRUtils::type_get_past_pointer(arg_type));
ASR::Array_t* orig_arg_array_t = ASR::down_cast<ASR::Array_t>(
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(orig_arg_type)));
ASRUtils::type_get_past_pointer(orig_arg_type));
if( (arg_array_t->m_physical_type != orig_arg_array_t->m_physical_type) ||
(arg_array_t->m_physical_type == ASR::array_physical_typeType::DescriptorArray &&
arg_array_t->m_physical_type == orig_arg_array_t->m_physical_type &&
Expand Down
16 changes: 9 additions & 7 deletions src/libasr/asr_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,15 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
ASR::expr_t* target = x.m_target;
if( ASR::is_a<ASR::Var_t>(*target) ) {
ASR::Var_t* target_Var = ASR::down_cast<ASR::Var_t>(target);
bool is_target_const = false;
ASR::ttype_t* target_type = nullptr;
if( ASR::is_a<ASR::Variable_t>(*target_Var->m_v) ||
(ASR::is_a<ASR::ExternalSymbol_t>(*target_Var->m_v) &&
ASR::down_cast<ASR::ExternalSymbol_t>(target_Var->m_v)->m_external) ) {
target_type = ASRUtils::expr_type(target);
ASR::symbol_t* target_sym = ASRUtils::symbol_get_past_external(target_Var->m_v);
if( target_sym && ASR::is_a<ASR::Variable_t>(*target_sym) ) {
ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(target_sym);
target_type = var->m_type;
is_target_const = var->m_storage == ASR::storage_typeType::Parameter;
}
if( target_type && ASR::is_a<ASR::Const_t>(*target_type) ) {
if( is_target_const ) {
std::string variable_name = ASRUtils::symbol_name(target_Var->m_v);
require(const_assigned.find(std::make_pair(current_symtab->counter,
variable_name)) == const_assigned.end(),
Expand Down Expand Up @@ -1163,14 +1165,14 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
void visit_dimension(const dimension_t &x) {
if (x.m_start) {
require_with_loc(ASRUtils::is_integer(
*ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_start))),
*ASRUtils::expr_type(x.m_start)),
"Start dimension must be a signed integer", x.loc);
visit_expr(*x.m_start);
}

if (x.m_length) {
require_with_loc(ASRUtils::is_integer(
*ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_length))),
*ASRUtils::expr_type(x.m_length)),
"Length dimension must be a signed integer", x.loc);
visit_expr(*x.m_length);
}
Expand Down
9 changes: 0 additions & 9 deletions src/libasr/casting_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ namespace LCompilers::CastingUtil {
bool is_assign, bool allow_int_to_float) {
ASR::ttype_t* left_type = ASRUtils::expr_type(left_expr);
ASR::ttype_t* right_type = ASRUtils::expr_type(right_expr);
if( ASR::is_a<ASR::Const_t>(*left_type) ) {
left_type = ASRUtils::get_contained_type(left_type);
}
if( ASR::is_a<ASR::Const_t>(*right_type) ) {
right_type = ASRUtils::get_contained_type(right_type);
}
left_type = ASRUtils::type_get_past_pointer(left_type);
right_type = ASRUtils::type_get_past_pointer(right_type);
if( ASRUtils::check_equal_type(left_type, right_type) ||
Expand Down Expand Up @@ -121,9 +115,6 @@ namespace LCompilers::CastingUtil {
ASR::ttype_t* dest, Allocator& al,
const Location& loc) {
ASR::ttype_t* src = ASRUtils::expr_type(expr);
if( ASR::is_a<ASR::Const_t>(*src) ) {
src = ASRUtils::get_contained_type(src);
}
// TODO: Uncomment the following
// ASR::ttypeType src_type = ASRUtils::extract_type(src)->type;
// ASR::ttypeType dest_type = ASRUtils::extract_type(dest)->type;
Expand Down
22 changes: 9 additions & 13 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
ASR::dimension_t* m_dims = nullptr;
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(v.m_type, m_dims);
ASR::ttype_t* v_m_type = v.m_type;
if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
if( is_array ) {
v_m_type = ASR::down_cast<ASR::Const_t>(v_m_type)->m_type;
}
}
v_m_type = ASRUtils::type_get_past_array(ASRUtils::type_get_past_allocatable(v_m_type));
if (ASRUtils::is_pointer(v_m_type)) {
ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(v_m_type)->m_type;
Expand Down Expand Up @@ -400,7 +395,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
} else {
std::string dims;
use_ref = use_ref && !is_array;
if (ASRUtils::is_integer(*v_m_type)) {
if (v.m_storage == ASR::storage_typeType::Parameter) {
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
if (v.m_intent != ASR::intentType::ReturnVar) {
sub = "const " + sub;
}
} else if (ASRUtils::is_integer(*v_m_type)) {
headers.insert("inttypes.h");
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
Expand Down Expand Up @@ -546,11 +547,6 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
ASR::Enum_t* enum_ = ASR::down_cast<ASR::Enum_t>(v_m_type);
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)) {
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 All @@ -565,7 +561,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
}
if (dims.size() == 0 && v.m_symbolic_value && !do_not_initialize) {
ASR::expr_t* init_expr = v.m_symbolic_value;
if( !ASR::is_a<ASR::Const_t>(*v.m_type) ) {
if( v.m_storage != ASR::storage_typeType::Parameter ) {
for( size_t i = 0; i < v.n_dependencies; i++ ) {
std::string variable_name = v.m_dependencies[i];
ASR::symbol_t* dep_sym = current_scope->resolve_symbol(variable_name);
Expand Down Expand Up @@ -1367,7 +1363,7 @@ R"( // Initialise Numpy
}

ASR::ttype_t* x_mv_type_ = ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(x_mv_type)));
ASRUtils::type_get_past_pointer(x_mv_type));
LCOMPILERS_ASSERT(ASR::is_a<ASR::Array_t>(*x_mv_type_));
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(x_mv_type_);
std::vector<std::string> diminfo;
Expand Down
12 changes: 1 addition & 11 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ R"(#include <stdio.h>
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
std::string decl = self().convert_variable_decl(*v);
decl = check_tmp_buffer() + decl;
bool used_define_for_const = (ASR::is_a<ASR::Const_t>(*v->m_type) &&
bool used_define_for_const = (v->m_storage == ASR::storage_typeType::Parameter &&
v->m_intent == ASRUtils::intent_local);
if (used_define_for_const) {
contains += decl + "\n";
Expand Down Expand Up @@ -496,10 +496,6 @@ R"(#include <stdio.h>
} else if (ASR::is_a<ASR::Tuple_t>(*return_var->m_type)) {
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(return_var->m_type);
sub = c_ds_api->get_tuple_type(tup_type) + " ";
} else if (ASR::is_a<ASR::Const_t>(*return_var->m_type)) {
ASR::Const_t* const_type = ASR::down_cast<ASR::Const_t>(return_var->m_type);
std::string const_type_str = CUtils::get_c_type_from_ttype_t(const_type->m_type);
sub = "const " + const_type_str + " ";
} else if (ASR::is_a<ASR::Pointer_t>(*return_var->m_type)) {
ASR::Pointer_t* ptr_type = ASR::down_cast<ASR::Pointer_t>(return_var->m_type);
std::string pointer_type_str = CUtils::get_c_type_from_ttype_t(ptr_type->m_type);
Expand Down Expand Up @@ -721,8 +717,6 @@ R"(#include <stdio.h>
}
} case ASR::ttypeType::Logical : {
return "p";
} case ASR::ttypeType::Const : {
return get_type_format(ASR::down_cast<ASR::Const_t>(type)->m_type);
} case ASR::ttypeType::Array : {
return "O";
} default: {
Expand Down Expand Up @@ -1252,10 +1246,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
void visit_Assignment(const ASR::Assignment_t &x) {
std::string target;
ASR::ttype_t* m_target_type = ASRUtils::expr_type(x.m_target);
if( ASR::is_a<ASR::Const_t>(*m_target_type) ) {
src = "";
return ;
}
ASR::ttype_t* m_value_type = ASRUtils::expr_type(x.m_value);
bool is_target_list = ASR::is_a<ASR::List_t>(*m_target_type);
bool is_value_list = ASR::is_a<ASR::List_t>(*m_value_type);
Expand Down
Loading

0 comments on commit 8820e02

Please sign in to comment.