Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove const ttype #119

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 49 additions & 41 deletions src/lc/clang_ast_to_asr.cpp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,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
95 changes: 14 additions & 81 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 @@ -2014,11 +1971,9 @@ static inline bool is_pointer(ASR::ttype_t *x) {

static inline bool is_integer(ASR::ttype_t &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(
type_get_past_const(&x))))));
type_get_past_pointer(&x))));
}

static inline bool is_unsigned_integer(ASR::ttype_t &x) {
Expand All @@ -2030,10 +1985,9 @@ static inline bool is_unsigned_integer(ASR::ttype_t &x) {

static inline bool is_real(ASR::ttype_t &x) {
return ASR::is_a<ASR::Real_t>(
*type_get_past_const(
type_get_past_array(
type_get_past_allocatable(
type_get_past_pointer(&x)))));
*type_get_past_array(
type_get_past_allocatable(
type_get_past_pointer(&x))));
}

static inline bool is_character(ASR::ttype_t &x) {
Expand Down Expand Up @@ -2165,10 +2119,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 @@ -2198,10 +2148,9 @@ inline size_t extract_dimensions_from_ttype(ASR::ttype_t *x,
}

static inline ASR::ttype_t *extract_type(ASR::ttype_t *type) {
return type_get_past_const(
type_get_past_array(
type_get_past_allocatable(
type_get_past_pointer(type))));
return type_get_past_array(
type_get_past_allocatable(
type_get_past_pointer(type)));
}

static inline bool is_fixed_size_array(ASR::dimension_t* m_dims, size_t n_dims) {
Expand Down Expand Up @@ -2464,9 +2413,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 @@ -2569,12 +2515,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 @@ -3038,12 +2978,10 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b,
if( a == nullptr && b == nullptr ) {
return true;
}
a = ASRUtils::type_get_past_const(
ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(a)));
b = ASRUtils::type_get_past_const(
ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(b)));
a = ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(a));
b = ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(b));
if( !check_for_dimensions ) {
a = ASRUtils::type_get_past_array(a);
b = ASRUtils::type_get_past_array(b);
Expand Down Expand Up @@ -3428,11 +3366,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 @@ -5175,9 +5108,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 @@ -359,13 +359,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 @@ -1159,14 +1161,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 @@ -120,9 +114,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);
}
ASR::ttypeType src_type = ASRUtils::extract_type(src)->type;
ASR::ttypeType dest_type = ASRUtils::extract_type(dest)->type;
ASR::cast_kindType cast_kind;
Expand Down
15 changes: 4 additions & 11 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 @@ -546,10 +541,8 @@ 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,
} else if (v.m_storage == ASR::storage_typeType::Parameter) {
sub = format_type_c("", "const " + CUtils::get_c_type_from_ttype_t(v_m_type),
v.m_name, false, false);
} else if (ASR::is_a<ASR::TypeParameter_t>(*v_m_type)) {
// Ignore type variables
Expand All @@ -565,7 +558,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 +1360,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
14 changes: 3 additions & 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,8 @@ 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 (return_var->m_storage == ASR::storage_typeType::Parameter) {
sub = "const " + CUtils::get_c_type_from_ttype_t(return_var->m_type) + " ";
} 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 +719,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 +1248,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
Loading