Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 890b4e2

Browse files
committed
DEV: Update usage of libasr
1 parent bb6e2ee commit 890b4e2

File tree

2 files changed

+89
-121
lines changed

2 files changed

+89
-121
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
304304
ASR::accessType::Public));
305305
scope->add_symbol(g_func_name, es);
306306
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
307-
es, g_func, nullptr, 0, nullptr, nullptr, false));
307+
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
308308
}
309309

310310
g_func_name = mod_name + "global_stmts";
@@ -317,7 +317,7 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
317317
ASR::accessType::Public));
318318
scope->add_symbol(g_func_name, es);
319319
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
320-
es, g_func, nullptr, 0, nullptr, nullptr, false));
320+
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
321321
}
322322
}
323323

@@ -1038,6 +1038,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
10381038

10391039
ASR::asr_t* make_dummy_assignment(ASR::expr_t* expr) {
10401040
ASR::ttype_t* type = ASRUtils::expr_type(expr);
1041+
LCOMPILERS_ASSERT(type != nullptr);
10411042
std::string dummy_ret_name = current_scope->get_unique_name("__lcompilers_dummy", false);
10421043
SetChar variable_dependencies_vec;
10431044
variable_dependencies_vec.reserve(al, 1);
@@ -1233,7 +1234,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
12331234
}
12341235

12351236
return ASRUtils::make_SubroutineCall_t_util(al, loc, stemp,
1236-
s_generic, args_new.p, args_new.size(), nullptr, nullptr, false);
1237+
s_generic, args_new.p, args_new.size(), nullptr, nullptr, false, false);
12371238
}
12381239
} else if(ASR::is_a<ASR::StructType_t>(*s)) {
12391240
ASR::StructType_t* StructType = ASR::down_cast<ASR::StructType_t>(s);
@@ -1903,8 +1904,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
19031904
// See integration_tests/test_bool_binop.py for its significance.
19041905
if(!is_assign && ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type) ) {
19051906
ASR::ttype_t* dest_type = ASRUtils::TYPE(ASR::make_Integer_t(al, left_type->base.loc, 4));
1906-
left = CastingUtil::perform_casting(left, left_type, dest_type, al, left->base.loc);
1907-
right = CastingUtil::perform_casting(right, right_type, dest_type, al, right->base.loc);
1907+
left = CastingUtil::perform_casting(left, dest_type, al, left->base.loc);
1908+
right = CastingUtil::perform_casting(right, dest_type, al, right->base.loc);
19081909
return ;
19091910
}
19101911

@@ -1915,8 +1916,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
19151916
if( casted_expression_signal == 2 ) {
19161917
return ;
19171918
}
1918-
src_expr = CastingUtil::perform_casting(src_expr, src_type,
1919-
dest_type, al, src_expr->base.loc);
1919+
src_expr = CastingUtil::perform_casting(src_expr, dest_type, al, src_expr->base.loc);
19201920
if( casted_expression_signal == 0 ) {
19211921
left = src_expr;
19221922
right = dest_expr;
@@ -1938,8 +1938,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
19381938
if( ASRUtils::check_equal_type(src_type, dest_type) ) {
19391939
return ;
19401940
}
1941-
src_expr = CastingUtil::perform_casting(src_expr, src_type,
1942-
dest_type, al, loc);
1941+
src_expr = CastingUtil::perform_casting(src_expr, dest_type, al, loc);
19431942
}
19441943

19451944
void make_BinOp_helper(ASR::expr_t *left, ASR::expr_t *right,
@@ -2186,33 +2185,31 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
21862185
ASRUtils::create_intrinsic_function create_function;
21872186
switch (op) {
21882187
case (ASR::binopType::Add): {
2189-
create_function = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SymbolicAdd");
2188+
create_function = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("SymbolicAdd");
21902189
break;
21912190
}
21922191
case (ASR::binopType::Sub): {
2193-
create_function = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SymbolicSub");
2192+
create_function = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("SymbolicSub");
21942193
break;
21952194
}
21962195
case (ASR::binopType::Mul): {
2197-
create_function = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SymbolicMul");
2196+
create_function = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("SymbolicMul");
21982197
break;
21992198
}
22002199
case (ASR::binopType::Div): {
2201-
create_function = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SymbolicDiv");
2200+
create_function = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("SymbolicDiv");
22022201
break;
22032202
}
22042203
case (ASR::binopType::Pow): {
2205-
create_function = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SymbolicPow");
2204+
create_function = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("SymbolicPow");
22062205
break;
22072206
}
22082207
default: {
22092208
throw SemanticError("Not implemented: The following symbolic binary operator has not been implemented", loc);
22102209
break;
22112210
}
22122211
}
2213-
tmp = create_function(al, loc, args_with_symbolic, [&](const std::string& msg, const Location& loc) {
2214-
throw SemanticError(msg, loc);
2215-
});
2212+
tmp = create_function(al, loc, args_with_symbolic, diag);
22162213
return;
22172214
} else {
22182215
std::string ltype = ASRUtils::type_to_str_python(ASRUtils::expr_type(left));
@@ -3234,14 +3231,13 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
32343231
ASR::symbol_t *s = current_scope->resolve_symbol(name);
32353232
LCOMPILERS_ASSERT(s);
32363233
tmp = ASR::make_Var_t(al, x.base.base.loc, s);
3237-
} else if (ASRUtils::IntrinsicScalarFunctionRegistry::is_intrinsic_function(name) &&
3234+
} else if (ASRUtils::IntrinsicElementalFunctionRegistry::is_intrinsic_function(name) &&
32383235
(not_cpython_builtin.find(name) == not_cpython_builtin.end() ||
32393236
imported_functions.find(name) != imported_functions.end() )) {
32403237
ASRUtils::create_intrinsic_function create_func =
3241-
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function(name);
3238+
ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function(name);
32423239
Vec<ASR::expr_t*> args_;
3243-
tmp = create_func(al, x.base.base.loc, args_, [&](const std::string &msg, const Location &loc) {
3244-
throw SemanticError(msg, loc); });
3240+
tmp = create_func(al, x.base.base.loc, args_, diag);
32453241
} else {
32463242
throw SemanticError("Variable '" + name + "' not declared",
32473243
x.base.base.loc);
@@ -3333,8 +3329,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
33333329
ASR::ttype_t *dest_type = left_operand_type;
33343330

33353331
if (!ASRUtils::check_equal_type(left_operand_type, right_operand_type)) {
3336-
throw SemanticError("Type mismatch: '" + ASRUtils::type_to_str_python(left_operand_type)
3337-
+ "' and '" + ASRUtils::type_to_str_python(right_operand_type)
3332+
throw SemanticError("Type mismatch: '" + ASRUtils::type_to_str_python(left_operand_type)
3333+
+ "' and '" + ASRUtils::type_to_str_python(right_operand_type)
33383334
+ "'. Both operands must be of the same type.", x.base.base.loc);
33393335
}
33403336
// Reference: https://docs.python.org/3/reference/expressions.html#boolean-operations
@@ -3474,10 +3470,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
34743470
args.push_back(al, left);
34753471
args.push_back(al, right);
34763472
ASRUtils::create_intrinsic_function create_func =
3477-
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function(op_name);
3478-
tmp = create_func(al, x.base.base.loc, args,
3479-
[&](const std::string &msg, const Location &loc) {
3480-
throw SemanticError(msg, loc); });
3473+
ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function(op_name);
3474+
tmp = create_func(al, x.base.base.loc, args, diag);
34813475
return;
34823476
}
34833477

@@ -3911,7 +3905,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
39113905
ASR::expr_t *dim_size = ASR::down_cast<ASR::Array_t>(type)->m_dims[idx].m_length;
39123906
ASR::expr_t *idx_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, idx + 1, int_type));
39133907
ASR::expr_t *size_expr = ASRUtils::EXPR(ASRUtils::make_ArraySize_t_util(al, loc, value, idx_expr, int_type, dim_size, false));
3914-
index = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc,
3908+
index = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc,
39153909
size_expr, ASR::binopType::Add, neg_idx, int_type, nullptr));
39163910
}
39173911
}
@@ -6853,9 +6847,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
68536847
*/
68546848
Vec<ASR::expr_t*> args_; args_.reserve(al, args.n);
68556849
ASRUtils::visit_expr_list(al, args, args_);
6856-
tmp = ASRUtils::Partition::create_partition(al, loc, args_, s_var,
6857-
[&](const std::string &msg, const Location &loc) {
6858-
throw SemanticError(msg, loc); });
6850+
tmp = ASRUtils::Partition::create_partition(al, loc, args_, s_var, diag);
68596851
return;
68606852
} else if(attr_name == "count") {
68616853
if(args.size() != 1) {
@@ -7230,9 +7222,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
72307222
loc, 1, s_var.size(), nullptr));
72317223
ASR::expr_t *str = ASRUtils::EXPR(ASR::make_StringConstant_t(al,
72327224
loc, s2c(al, s_var), char_type));
7233-
tmp = ASRUtils::Partition::create_partition(al, loc, args_, str,
7234-
[&](const std::string &msg, const Location &loc) {
7235-
throw SemanticError(msg, loc); });
7225+
tmp = ASRUtils::Partition::create_partition(al, loc, args_, str, diag);
72367226
return;
72377227
} else if (attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
72387228
/*
@@ -7345,7 +7335,7 @@ we will have to use something else.
73457335
} else if (attr_name == "isalpha") {
73467336
/*
73477337
* Specification -
7348-
Return True if all characters in the string are alphabets,
7338+
Return True if all characters in the string are alphabets,
73497339
and there is at least one character in the string.
73507340
*/
73517341
bool is_alpha = (s_var.size() != 0);
@@ -7361,7 +7351,7 @@ we will have to use something else.
73617351
} else if (attr_name == "isalnum") {
73627352
/*
73637353
* Specification -
7364-
Return True if all characters in the string are alphabets or numbers,
7354+
Return True if all characters in the string are alphabets or numbers,
73657355
and there is at least one character in the string.
73667356
*/
73677357
bool is_alnum = (s_var.size() != 0);
@@ -7377,7 +7367,7 @@ we will have to use something else.
73777367
} else if (attr_name == "isnumeric") {
73787368
/*
73797369
* Specification -
7380-
Return True if all characters in the string are numbers,
7370+
Return True if all characters in the string are numbers,
73817371
and there is at least one character in the string.
73827372
*/
73837373
bool is_numeric = (s_var.size() != 0);
@@ -7393,7 +7383,7 @@ we will have to use something else.
73937383
} else if (attr_name == "istitle") {
73947384
/*
73957385
* Specification -
7396-
Returns True if all words in the string are in title case,
7386+
Returns True if all words in the string are in title case,
73977387
and there is at least one character in the string.
73987388
*/
73997389
bool is_title = (s_var.size() != 0);
@@ -7457,15 +7447,13 @@ we will have to use something else.
74577447
if (symbolic_attributes.find(call_name) != symbolic_attributes.end() &&
74587448
symbolic_constants.find(mod_name) != symbolic_constants.end()){
74597449
ASRUtils::create_intrinsic_function create_func;
7460-
create_func = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function(mod_name);
7450+
create_func = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function(mod_name);
74617451
Vec<ASR::expr_t*> eles; eles.reserve(al, args.size());
74627452
Vec<ASR::expr_t*> args_; args_.reserve(al, 1);
74637453
for (size_t i=0; i<args.size(); i++) {
74647454
eles.push_back(al, args[i].m_value);
74657455
}
7466-
tmp = create_func(al, at->base.base.loc, args_,
7467-
[&](const std::string &msg, const Location &loc) {
7468-
throw SemanticError(msg, loc); });
7456+
tmp = create_func(al, at->base.base.loc, args_, diag);
74697457
handle_symbolic_attribute(ASRUtils::EXPR(tmp), call_name, loc, eles);
74707458
return;
74717459
}
@@ -7687,13 +7675,13 @@ we will have to use something else.
76877675
imported_functions[call_name] == "sympy"){
76887676
intrinsic_name = "Symbolic" + std::string(1, std::toupper(call_name[0])) + call_name.substr(1);
76897677
}
7690-
if ((ASRUtils::IntrinsicScalarFunctionRegistry::is_intrinsic_function(intrinsic_name) ||
7678+
if ((ASRUtils::IntrinsicElementalFunctionRegistry::is_intrinsic_function(intrinsic_name) ||
76917679
ASRUtils::IntrinsicArrayFunctionRegistry::is_intrinsic_function(intrinsic_name)) &&
76927680
(not_cpython_builtin.find(call_name) == not_cpython_builtin.end() ||
76937681
imported_functions.find(call_name) != imported_functions.end() )) {
76947682
ASRUtils::create_intrinsic_function create_func;
7695-
if (ASRUtils::IntrinsicScalarFunctionRegistry::is_intrinsic_function(intrinsic_name)) {
7696-
create_func = ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function(intrinsic_name);
7683+
if (ASRUtils::IntrinsicElementalFunctionRegistry::is_intrinsic_function(intrinsic_name)) {
7684+
create_func = ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function(intrinsic_name);
76977685
} else {
76987686
create_func = ASRUtils::IntrinsicArrayFunctionRegistry::get_create_function(intrinsic_name);
76997687
}
@@ -7704,9 +7692,7 @@ we will have to use something else.
77047692
throw SemanticError("Function '" + call_name + "' does not accept vector values",
77057693
x.base.base.loc);
77067694
}
7707-
tmp = create_func(al, x.base.base.loc, args_,
7708-
[&](const std::string &msg, const Location &loc) {
7709-
throw SemanticError(msg, loc); });
7695+
tmp = create_func(al, x.base.base.loc, args_, diag);
77107696
return ;
77117697
} else if (intrinsic_procedures.is_intrinsic(call_name)) {
77127698
s = resolve_intrinsic_function(x.base.base.loc, call_name);
@@ -7789,11 +7775,9 @@ we will have to use something else.
77897775
} else if( call_name == "reserve" ) {
77907776
parse_args(x, args);
77917777
ASRUtils::create_intrinsic_function create_func =
7792-
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("reserve");
7778+
ASRUtils::IntrinsicElementalFunctionRegistry::get_create_function("list.reserve");
77937779
Vec<ASR::expr_t*> args_exprs = ASRUtils::call_arg2expr(al, args);
7794-
tmp = create_func(al, x.base.base.loc, args_exprs,
7795-
[&](const std::string &msg, const Location &loc) {
7796-
throw SemanticError(msg, loc); });
7780+
tmp = create_func(al, x.base.base.loc, args_exprs, diag);
77977781
return ;
77987782
} else if (call_name == "size") {
77997783
parse_args(x, args);
@@ -7866,7 +7850,7 @@ we will have to use something else.
78667850
} else {
78677851
Vec<ASR::expr_t*> arr_args;
78687852
arr_args.reserve(al, 0);
7869-
tmp = ASRUtils::make_ArrayConstant_t_util(al, x.base.base.loc,
7853+
tmp = ASRUtils::make_ArrayConstructor_t_util(al, x.base.base.loc,
78707854
arr_args.p, arr_args.size(), type, ASR::arraystorageType::RowMajor);
78717855
}
78727856
return;
@@ -8025,10 +8009,10 @@ we will have to use something else.
80258009
type = ASRUtils::make_Array_t_util(al, x.base.base.loc, type, dims.p, dims.size(),
80268010
ASR::abiType::Source, false, ASR::array_physical_typeType::PointerToDataArray, true);
80278011
for( size_t i = 0; i < n_args; i++ ) {
8028-
m_args[i] = CastingUtil::perform_casting(m_args[i], ASRUtils::expr_type(m_args[i]),
8029-
ASRUtils::type_get_past_array(type), al, x.base.base.loc);
8012+
m_args[i] = CastingUtil::perform_casting(m_args[i], ASRUtils::type_get_past_array(type),
8013+
al, x.base.base.loc);
80308014
}
8031-
tmp = ASR::make_ArrayConstant_t(al, x.base.base.loc, m_args, n_args, type, ASR::arraystorageType::RowMajor);
8015+
tmp = ASRUtils::make_ArrayConstructor_t_util(al, x.base.base.loc, m_args, n_args, type, ASR::arraystorageType::RowMajor);
80328016
} else {
80338017
throw SemanticError("array accepts only list for now, got " +
80348018
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);

0 commit comments

Comments
 (0)