Skip to content

Commit 245eb5c

Browse files
authored
Remove ttype enum_type from EnumName and EnumValue (#1133)
1 parent 9646432 commit 245eb5c

File tree

5 files changed

+35
-40
lines changed

5 files changed

+35
-40
lines changed

src/libasr/ASR.asdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ expr
281281

282282
| BitCast(expr source, expr mold, expr? size, ttype type, expr? value)
283283
| DerivedRef(expr v, symbol m, ttype type, expr? value)
284-
| EnumName(symbol v, ttype enum_type, ttype type, expr? value)
285-
| EnumValue(symbol v, ttype enum_type, ttype type, expr? value)
284+
| EnumName(symbol v, ttype type, expr? value)
285+
| EnumValue(symbol v, ttype type, expr? value)
286286
| OverloadedCompare(expr left, cmpop op, expr right, ttype type, expr? value, expr overloaded)
287287
| OverloadedBinOp(expr left, binop op, expr right, ttype type, expr? value, expr overloaded)
288288
| Cast(expr arg, cast_kind kind, ttype type, expr? value)

src/libasr/asr_utils.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ static inline ASR::ttype_t* duplicate_type(Allocator& al, const ASR::ttype_t* t,
12481248
//return ASRUtils::TYPE(ASR::make_TypeParameter_t(al, t->base.loc,
12491249
// tp->m_param, dimsp, dimsn, tp->m_rt, tp->n_rt));
12501250
return ASRUtils::TYPE(ASR::make_TypeParameter_t(al, t->base.loc,
1251-
tp->m_param, dimsp, dimsn));
1251+
tp->m_param, dimsp, dimsn));
12521252
}
12531253
default : throw LCompilersException("Not implemented " + std::to_string(t->type));
12541254
}
@@ -1278,7 +1278,7 @@ static inline ASR::ttype_t* duplicate_type_without_dims(Allocator& al, const ASR
12781278
// tp->m_param, nullptr, 0, tp->m_rt, tp->n_rt));
12791279
return ASRUtils::TYPE(ASR::make_TypeParameter_t(al, t->base.loc,
12801280
tp->m_param, nullptr, 0));
1281-
}
1281+
}
12821282
default : throw LCompilersException("Not implemented " + std::to_string(t->type));
12831283
}
12841284
}
@@ -1912,6 +1912,17 @@ static inline void get_dimensions(ASR::expr_t* array, Vec<ASR::expr_t*>& dims,
19121912
}
19131913
}
19141914

1915+
static inline ASR::EnumType_t* get_EnumType_from_symbol(ASR::symbol_t* s) {
1916+
ASR::Variable_t* s_var = ASR::down_cast<ASR::Variable_t>(s);
1917+
if( ASR::is_a<ASR::Enum_t>(*s_var->m_type) ) {
1918+
ASR::Enum_t* enum_ = ASR::down_cast<ASR::Enum_t>(s_var->m_type);
1919+
return ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type);
1920+
}
1921+
ASR::symbol_t* enum_type_cand = ASR::down_cast<ASR::symbol_t>(s_var->m_parent_symtab->asr_owner);
1922+
LFORTRAN_ASSERT(ASR::is_a<ASR::EnumType_t>(*enum_type_cand));
1923+
return ASR::down_cast<ASR::EnumType_t>(enum_type_cand);
1924+
}
1925+
19151926
} // namespace ASRUtils
19161927

19171928
} // namespace LFortran

src/libasr/codegen/asr_to_c.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,7 @@ R"(
576576
void visit_EnumName(const ASR::EnumName_t& x) {
577577
ASR::Variable_t* enum_var = ASR::down_cast<ASR::Variable_t>(x.m_v);
578578
int64_t min_value = INT64_MAX;
579-
ASR::Enum_t* x_enum = ASR::down_cast<ASR::Enum_t>(x.m_enum_type);
580-
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(x_enum->m_enum_type);
581-
579+
ASR::EnumType_t* enum_type = ASRUtils::get_EnumType_from_symbol(x.m_v);
582580
for( auto itr: enum_type->m_symtab->get_scope() ) {
583581
ASR::Variable_t* itr_var = ASR::down_cast<ASR::Variable_t>(itr.second);
584582
ASR::expr_t* value = itr_var->m_symbolic_value;

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,8 +1625,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16251625
}
16261626

16271627
void lookup_EnumValue(const ASR::EnumValue_t& x) {
1628-
ASR::Enum_t* x_enum = ASR::down_cast<ASR::Enum_t>(x.m_enum_type);
1629-
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(x_enum->m_enum_type);
1628+
ASR::EnumType_t* enum_type = ASRUtils::get_EnumType_from_symbol(x.m_v);
16301629
uint32_t h = get_hash((ASR::asr_t*) enum_type);
16311630
llvm::Value* array = llvm_symtab[h];
16321631
tmp = llvm_utils->create_gep(array, tmp);
@@ -1639,8 +1638,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16391638
this->visit_expr(*x.m_value);
16401639
} else {
16411640
ASR::Variable_t* x_mv = ASR::down_cast<ASR::Variable_t>(x.m_v);
1642-
ASR::Enum_t* x_enum = ASR::down_cast<ASR::Enum_t>(x.m_enum_type);
1643-
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(x_enum->m_enum_type);
1641+
ASR::EnumType_t* enum_type = ASRUtils::get_EnumType_from_symbol(x.m_v);
16441642
for( size_t i = 0; i < enum_type->n_members; i++ ) {
16451643
if( std::string(enum_type->m_members[i]) == std::string(x_mv->m_name) ) {
16461644
tmp = llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), llvm::APInt(32, i));
@@ -1669,8 +1667,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16691667

16701668
ASR::Variable_t* x_m_v = ASR::down_cast<ASR::Variable_t>(x.m_v);
16711669
fetch_val(x_m_v);
1672-
ASR::Enum_t* x_enum = ASR::down_cast<ASR::Enum_t>(x.m_enum_type);
1673-
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(x_enum->m_enum_type);
1670+
ASR::EnumType_t* enum_type = ASRUtils::get_EnumType_from_symbol(x.m_v);
16741671
uint32_t h = get_hash((ASR::asr_t*) enum_type);
16751672
llvm::Value* array = llvm_symtab[h];
16761673
if( ASR::is_a<ASR::Integer_t>(*enum_type->m_type) ) {
@@ -5115,15 +5112,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
51155112
args.push_back(tmp);
51165113
} else if (ASRUtils::is_real(*t)) {
51175114
llvm::Value *d;
5118-
// if( ASR::is_a<ASR::EnumValue_t>(*v) ) {
5119-
// ASR::EnumValue_t* enum_value = ASR::down_cast<ASR::EnumValue_t>(v);
5120-
// ASR::Enum_t* x_enum = ASR::down_cast<ASR::Enum_t>(enum_value->m_enum_type);
5121-
// ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(x_enum->m_enum_type);
5122-
// uint32_t h = get_hash((ASR::asr_t*) enum_type);
5123-
// llvm::Value* array = llvm_symtab[h];
5124-
// tmp = llvm_utils->create_gep(array, tmp);
5125-
// tmp = LLVM::CreateLoad(*builder, llvm_utils->create_gep(tmp, 1));
5126-
// }
51275115
switch( a_kind ) {
51285116
case 4 : {
51295117
// Cast float to double as a workaround for the fact that

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
668668
call_args_vec.push_back(al, arg);
669669
}
670670
}
671-
671+
672672
int64_t find_argument_position_from_name(ASR::Function_t* orig_func, std::string arg_name,
673673
const Location& call_loc, bool raise_error) {
674674
int64_t arg_position = -1;
@@ -955,7 +955,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
955955

956956
ASR::symbol_t *t = get_generic_function(subs, rt_subs, *func);
957957
std::string new_call_name = (ASR::down_cast<ASR::Function_t>(t))->m_name;
958-
958+
959959
// Currently ignoring keyword arguments for generic function calls
960960
Vec<ASR::call_arg_t> new_args;
961961
new_args.reserve(al, n_pos_args);
@@ -965,7 +965,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
965965
new_call_arg.loc = args.p[i].loc;
966966
new_args.push_back(al, new_call_arg);
967967
}
968-
return make_call_helper(al, t, current_scope, new_args, new_call_name, loc);
968+
return make_call_helper(al, t, current_scope, new_args, new_call_name, loc);
969969
}
970970
if (args.size() != func->n_args) {
971971
std::string fnd = std::to_string(args.size());
@@ -1099,7 +1099,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
10991099
* the restriction
11001100
*/
11011101
void check_type_restriction(std::map<std::string, ASR::ttype_t*> subs,
1102-
std::map<std::string, ASR::symbol_t*> rt_subs,
1102+
std::map<std::string, ASR::symbol_t*> rt_subs,
11031103
ASR::Function_t* rt, const Location& loc) {
11041104
std::string rt_name = rt->m_name;
11051105
if (rt_subs.find(rt_name) != rt_subs.end()) {
@@ -1110,13 +1110,13 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
11101110
throw SemanticError(msg, loc);
11111111
}
11121112
ASR::Function_t* rt_arg_func = ASR::down_cast<ASR::Function_t>(rt_arg);
1113-
/** @brief different argument number between the function given and the
1113+
/** @brief different argument number between the function given and the
11141114
* restriction result in error **/
11151115
if (rt->n_args != rt_arg_func->n_args) {
1116-
std::string msg = "The function " + std::string(rt_arg_func->m_name)
1116+
std::string msg = "The function " + std::string(rt_arg_func->m_name)
11171117
+ " provided for the restriction "
11181118
+ std::string(rt->m_name) + " have different number of arguments";
1119-
throw SemanticError(msg, rt_arg->base.loc);
1119+
throw SemanticError(msg, rt_arg->base.loc);
11201120
}
11211121
for (size_t j=0; j<rt->n_args; j++) {
11221122
ASR::ttype_t* rt_type = ASRUtils::expr_type(rt->m_args[j]);
@@ -1125,7 +1125,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
11251125
std::string rt_type_param = ASR::down_cast<ASR::TypeParameter_t>(
11261126
ASRUtils::get_type_parameter(rt_type))->m_param;
11271127
/**
1128-
* @brief if the type of the function given for the restriction does not
1128+
* @brief if the type of the function given for the restriction does not
11291129
* satisfy the type substitution from the function argument, it
11301130
* results in error **/
11311131
if (!ASRUtils::check_equal_type(subs[rt_type_param], rt_arg_type)) {
@@ -1186,7 +1186,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
11861186
ASR::ttype_t* right_type = ASRUtils::expr_type(rt->m_args[1]);
11871187
left_type = ASR::is_a<ASR::TypeParameter_t>(*left_type)
11881188
? subs[ASR::down_cast<ASR::TypeParameter_t>(left_type)->m_param] : left_type;
1189-
if ((ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*right_type)) ||
1189+
if ((ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*right_type)) ||
11901190
(ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type))) {
11911191
return;
11921192
}
@@ -1231,7 +1231,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12311231
new_function_num = 0;
12321232
}
12331233
generic_func_nums[func_name] = new_function_num + 1;
1234-
std::string new_func_name = "__lpython_generic_" + func_name + "_"
1234+
std::string new_func_name = "__lpython_generic_" + func_name + "_"
12351235
+ std::to_string(new_function_num);
12361236
generic_func_subs[new_func_name] = subs;
12371237
t = pass_instantiate_generic_function(al, subs, rt_subs, current_scope,
@@ -2981,7 +2981,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
29812981
Vec<ASR::dimension_t> dims;
29822982
dims.reserve(al, 4);
29832983

2984-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, x.base.base.loc,
2984+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, x.base.base.loc,
29852985
s2c(al, tvar_name), dims.p, dims.size()));
29862986

29872987
ASR::expr_t *value = nullptr;
@@ -3705,11 +3705,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37053705
"Only value and name properties are supported for now.", loc);
37063706
}
37073707
if( attr_name == "value" ) {
3708-
tmp = ASR::make_EnumValue_t(al, loc, t, type, enum_type->m_type, nullptr);
3708+
tmp = ASR::make_EnumValue_t(al, loc, t, enum_type->m_type, nullptr);
37093709
} else if( attr_name == "name" ) {
37103710
ASR::ttype_t* char_type = ASRUtils::TYPE(ASR::make_Character_t(al, loc, 1, -2,
37113711
nullptr, nullptr, 0));
3712-
tmp = ASR::make_EnumName_t(al, loc, t, type, char_type, nullptr);
3712+
tmp = ASR::make_EnumName_t(al, loc, t, char_type, nullptr);
37133713
}
37143714
} else if(ASR::is_a<ASR::Pointer_t>(*type)) {
37153715
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(type);
@@ -3740,8 +3740,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37403740
x.base.base.loc);
37413741
}
37423742
ASR::Variable_t* enum_member_variable = ASR::down_cast<ASR::Variable_t>(enum_member);
3743-
ASR::ttype_t* enum_ttype = ASRUtils::TYPE(ASR::make_Enum_t(al, x.base.base.loc, t, nullptr, 0));
3744-
tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_member, enum_ttype,
3743+
tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_member,
37453744
enum_member_variable->m_type, ASRUtils::expr_value(enum_member_variable->m_symbolic_value));
37463745
} else {
37473746
throw SemanticError("Only Variable type is supported for now in Attribute",
@@ -3768,8 +3767,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37683767
enum_ref_type = enum_ref->m_type;
37693768
enum_ref_value = enum_m_var->m_symbolic_value;
37703769
tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_ref->m_v,
3771-
enum_ref->m_enum_type, enum_ref_type,
3772-
ASRUtils::expr_value(enum_ref_value));
3770+
enum_ref_type, ASRUtils::expr_value(enum_ref_value));
37733771
} else if( enum_property == "name" ) {
37743772
char *s = enum_m_var->m_name;
37753773
size_t s_size = std::string(s).size();
@@ -3778,7 +3776,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37783776
enum_ref_value = ASRUtils::EXPR(ASR::make_StringConstant_t(al, x.base.base.loc,
37793777
s, enum_ref_type));
37803778
tmp = ASR::make_EnumName_t(al, x.base.base.loc, enum_ref->m_v,
3781-
enum_ref->m_enum_type, enum_ref_type, enum_ref_value);
3779+
enum_ref_type, enum_ref_value);
37823780
}
37833781
} else {
37843782
visit_AttributeUtil(ASRUtils::expr_type(e), x.m_attr, e, x.base.base.loc);

0 commit comments

Comments
 (0)