Skip to content

Commit dbb376a

Browse files
committed
Frontend support for restriction
1 parent 61cace0 commit dbb376a

File tree

2 files changed

+188
-30
lines changed

2 files changed

+188
-30
lines changed

src/libasr/pass/instantiate_template.cpp

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
118118
return result;
119119
}
120120

121+
122+
121123
ASR::asr_t* duplicate_Var(ASR::Var_t *x) {
122124
std::string sym_name = ASRUtils::symbol_name(x->m_v);
123125
ASR::symbol_t *sym = current_scope->get_symbol(sym_name);
@@ -139,6 +141,16 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
139141
return ASR::make_ArrayItem_t(al, x->base.base.loc, m_v, args.p, x->n_args, type, m_value);
140142
}
141143

144+
ASR::asr_t* duplicate_ListItem(ASR::ListItem_t *x) {
145+
ASR::expr_t *m_a = duplicate_expr(x->m_a);
146+
ASR::expr_t *m_pos = duplicate_expr(x->m_pos);
147+
ASR::ttype_t *type = substitute_type(x->m_type);
148+
ASR::expr_t *m_value = duplicate_expr(x->m_value);
149+
150+
return ASR::make_ListItem_t(al, x->base.base.loc,
151+
m_a, m_pos, type, m_value);
152+
}
153+
142154
ASR::array_index_t duplicate_array_index(ASR::array_index_t x) {
143155
ASR::expr_t *left = duplicate_expr(x.m_left);
144156
ASR::expr_t *right = duplicate_expr(x.m_right);
@@ -150,6 +162,21 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
150162
return result;
151163
}
152164

165+
ASR::asr_t* duplicate_Assignment(ASR::Assignment_t *x) {
166+
ASR::expr_t *target = duplicate_expr(x->m_target);
167+
ASR::ttype_t *target_type = substitute_type(ASRUtils::expr_type(x->m_target));
168+
ASR::expr_t *value = duplicate_expr(x->m_value);
169+
if (ASRUtils::is_real(*target_type) && ASR::is_a<ASR::IntegerConstant_t>(*x->m_value)) {
170+
ASR::IntegerConstant_t *int_value = ASR::down_cast<ASR::IntegerConstant_t>(x->m_value);
171+
if (int_value->m_n == 0) {
172+
value = ASRUtils::EXPR(ASR::make_RealConstant_t(al, value->base.loc, 0,
173+
ASRUtils::duplicate_type(al, target_type)));
174+
}
175+
}
176+
ASR::stmt_t *overloaded = duplicate_stmt(x->m_overloaded);
177+
return ASR::make_Assignment_t(al, x->base.base.loc, target, value, overloaded);
178+
}
179+
153180
ASR::asr_t* duplicate_TemplateBinOp(ASR::TemplateBinOp_t *x) {
154181
ASR::expr_t *left = duplicate_expr(x->m_left);
155182
ASR::expr_t *right = duplicate_expr(x->m_right);
@@ -172,11 +199,20 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
172199
return ASR::make_DoLoop_t(al, x->base.base.loc, head, m_body.p, x->n_body);
173200
}
174201

202+
ASR::asr_t* duplicate_Cast(ASR::Cast_t *x) {
203+
ASR::expr_t *arg = duplicate_expr(x->m_arg);
204+
ASR::ttype_t *type = substitute_type(ASRUtils::expr_type(x->m_arg));
205+
if (ASRUtils::is_real(*type)) {
206+
return (ASR::asr_t*) arg;
207+
}
208+
return ASRUtils::make_Cast_t_value(al, x->base.base.loc, arg, ASR::cast_kindType::IntegerToReal, x->m_type);
209+
}
210+
175211
ASR::ttype_t* substitute_type(ASR::ttype_t *param_type) {
176212
if (ASR::is_a<ASR::List_t>(*param_type)) {
177-
ASR::List_t *list_type = ASR::down_cast<ASR::List_t>(param_type);
178-
ASR::ttype_t *elem_type = substitute_type(list_type->m_type);
179-
return ASRUtils::TYPE(ASR::make_List_t(al, param_type->base.loc, elem_type));
213+
ASR::List_t *tlist = ASR::down_cast<ASR::List_t>(param_type);
214+
return ASRUtils::TYPE(ASR::make_List_t(al, param_type->base.loc,
215+
substitute_type(tlist->m_type)));
180216
}
181217
if (ASR::is_a<ASR::TypeParameter_t>(*param_type)) {
182218
ASR::TypeParameter_t *param = ASR::down_cast<ASR::TypeParameter_t>(param_type);
@@ -191,28 +227,26 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
191227
ASR::Real_t* tnew = ASR::down_cast<ASR::Real_t>(t);
192228
return ASRUtils::TYPE(ASR::make_Real_t(al, t->base.loc,
193229
tnew->m_kind, param->m_dims, param->n_dims));
194-
}
230+
}
195231
case ASR::ttypeType::Character: {
196232
ASR::Character_t* tnew = ASR::down_cast<ASR::Character_t>(t);
197233
return ASRUtils::TYPE(ASR::make_Character_t(al, t->base.loc,
198234
tnew->m_kind, tnew->m_len, tnew->m_len_expr,
199235
param->m_dims, param->n_dims));
200-
}
236+
}
201237
default: return subs[param->m_param];
202238
}
203239
}
204240
return param_type;
205241
}
206242

243+
// Commented out part is not yet considered for generic functions
207244
ASR::asr_t* make_BinOp_helper(ASR::expr_t *left, ASR::expr_t *right,
208245
ASR::binopType op, const Location &loc) {
209246
ASR::ttype_t *left_type = ASRUtils::expr_type(left);
210247
ASR::ttype_t *right_type = ASRUtils::expr_type(right);
211248
ASR::ttype_t *dest_type = nullptr;
212249
ASR::expr_t *value = nullptr;
213-
214-
// bool right_is_int = ASRUtils::is_character(*left_type) && ASRUtils::is_integer(*right_type);
215-
// bool left_is_int = ASRUtils::is_integer(*left_type) && ASRUtils::is_character(*right_type);
216250

217251
if ((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) ||
218252
ASRUtils::is_complex(*left_type) || ASRUtils::is_logical(*left_type)) &&
@@ -223,7 +257,6 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
223257
dest_type = ASRUtils::expr_type(left);
224258
} else if (ASRUtils::is_character(*left_type) && ASRUtils::is_character(*right_type)
225259
&& op == ASR::binopType::Add) {
226-
// string concat
227260
ASR::Character_t *left_type2 = ASR::down_cast<ASR::Character_t>(left_type);
228261
ASR::Character_t *right_type2 = ASR::down_cast<ASR::Character_t>(right_type);
229262
LFORTRAN_ASSERT(left_type2->n_dims == 0);
@@ -250,9 +283,10 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
250283
int64_t result;
251284
switch (op) {
252285
case (ASR::binopType::Add): { result = left_value + right_value; break; }
253-
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
254-
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
286+
// case (ASR::binopType::Sub): { result = left_value - right_value; break; }
287+
// case (ASR::binopType::Mul): { result = left_value * right_value; break; }
255288
case (ASR::binopType::Div): { result = left_value / right_value; break; }
289+
/*
256290
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
257291
case (ASR::binopType::BitAnd): { result = left_value & right_value; break; }
258292
case (ASR::binopType::BitOr): { result = left_value | right_value; break; }
@@ -271,17 +305,20 @@ class FunctionInstantiator : public ASR::BaseExprStmtDuplicator<FunctionInstanti
271305
result = left_value >> right_value;
272306
break;
273307
}
308+
*/
274309
default: { LFORTRAN_ASSERT(false); } // should never happen
275310
}
276311
value = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, loc, result, dest_type));
277312
}
278313
return ASR::make_IntegerBinOp_t(al, loc, left, op, right, dest_type, value);
279314
} else if (ASRUtils::is_real(*dest_type)) {
315+
/*
280316
if (op == ASR::binopType::BitAnd || op == ASR::binopType::BitOr || op == ASR::binopType::BitXor ||
281317
op == ASR::binopType::BitLShift || op == ASR::binopType::BitRShift) {
282318
throw LCompilersException("ICE: failure in instantiation: Unsupported binary operation on floats: '"
283319
+ ASRUtils::binop_to_str_python(op) + "'");
284320
}
321+
*/
285322
right = cast_helper(left_type, right);
286323
dest_type = ASRUtils::expr_type(right);
287324
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {

0 commit comments

Comments
 (0)