Skip to content

Commit a3ddfaf

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Function evaluation improvements.
- Handle some subscripted expressions in EvalFunction. - Fixed type of cat in Ceval.evalBuiltinCat. Belonging to [master]: - OpenModelica/OMCompiler#2421 - OpenModelica/OpenModelica-testsuite#942
1 parent 8b4871a commit a3ddfaf

File tree

6 files changed

+60
-20
lines changed

6 files changed

+60
-20
lines changed

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ algorithm
13941394
fail();
13951395
end if;
13961396
(es,dims) := ExpressionSimplify.evalCat(n, args, getArrayContents=Expression.arrayElements, toString=Expression.toString);
1397-
result := Expression.arrayFromList(es, Type.arrayElementType(ty), list(Dimension.fromInteger(d) for d in dims));
1397+
result := Expression.arrayFromList(es, Expression.typeOf(listHead(es)), list(Dimension.fromInteger(d) for d in dims));
13981398
end evalBuiltinCat;
13991399

14001400
function evalBuiltinCeil

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ public
357357
end match;
358358
end applySubscript2;
359359

360+
function getSubscripts
361+
input ComponentRef cref;
362+
output list<Subscript> subscripts;
363+
algorithm
364+
subscripts := match cref
365+
case CREF() then cref.subscripts;
366+
else {};
367+
end match;
368+
end getSubscripts;
369+
360370
function setSubscripts
361371
"Sets the subscripts of the first part of a cref."
362372
input list<Subscript> subscripts;

Compiler/NFFrontEnd/NFEvalFunction.mo

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,32 @@ algorithm
210210
if Binding.isBound(binding) then
211211
bindingExp := Binding.getExp(binding);
212212
else
213-
bindingExp := buildRecordBinding(node);
213+
bindingExp := buildBinding(node);
214214
end if;
215215
end getBindingExp;
216216

217-
function buildRecordBinding
217+
function buildBinding
218218
input InstNode node;
219219
output Expression result;
220220
protected
221-
InstNode cls_node = InstNode.classScope(node);
222-
Class cls = InstNode.getClass(cls_node);
221+
Type ty;
222+
algorithm
223+
ty := InstNode.getType(node);
224+
225+
result := match ty
226+
case Type.ARRAY() guard Type.hasKnownSize(ty) then Expression.fillType(ty, Expression.EMPTY());
227+
case Type.COMPLEX() then buildRecordBinding(ty.cls);
228+
else Expression.EMPTY();
229+
end match;
230+
end buildBinding;
231+
232+
function buildRecordBinding
233+
input InstNode recordNode;
234+
output Expression result;
235+
protected
236+
Class cls = InstNode.getClass(recordNode);
223237
array<InstNode> comps;
224238
list<Expression> bindings;
225-
Component comp;
226239
Expression exp;
227240
algorithm
228241
result := match cls
@@ -231,13 +244,12 @@ algorithm
231244
bindings := {};
232245

233246
for i in arrayLength(comps):-1:1 loop
234-
bindings := getBindingExp(comps[i]) :: bindings;
247+
bindings := Expression.makeMutable(getBindingExp(comps[i])) :: bindings;
235248
end for;
236249
then
237-
Expression.RECORD(InstNode.scopePath(cls_node), cls.ty, bindings);
250+
Expression.RECORD(InstNode.scopePath(recordNode), cls.ty, bindings);
238251

239252
case Class.TYPED_DERIVED() then buildRecordBinding(cls.baseClass);
240-
else Expression.makeMutable(Expression.EMPTY());
241253
end match;
242254
end buildRecordBinding;
243255

@@ -266,12 +278,6 @@ algorithm
266278
local
267279
Option<Expression> repl_exp;
268280

269-
case Expression.CREF(cref = ComponentRef.CREF(subscripts = _ :: _))
270-
algorithm
271-
Error.assertion(false, getInstanceName() + ": missing handling of subscripts", sourceInfo());
272-
then
273-
fail();
274-
275281
case Expression.CREF() then applyReplacementCref(repl, exp.cref, exp);
276282
else exp;
277283
end match;
@@ -305,14 +311,18 @@ algorithm
305311
return;
306312
end if;
307313

314+
outExp := Expression.applySubscripts(ComponentRef.getSubscripts(listHead(cref_parts)), outExp);
315+
cref_parts := listRest(cref_parts);
316+
308317
if not listEmpty(cref_parts) then
309318
try
310319
// If the cref consists of more than one identifier we need to look up
311320
// the corresponding record field in the expression.
312-
for cr in listRest(cref_parts) loop
321+
for cr in cref_parts loop
313322
node := ComponentRef.node(cr);
314323
outExp := Expression.makeImmutable(outExp);
315324
outExp := Expression.lookupRecordField(InstNode.name(node), outExp);
325+
outExp := Expression.applySubscripts(ComponentRef.getSubscripts(cr), outExp);
316326
end for;
317327
else
318328
Error.assertion(false, getInstanceName() + " could not find replacement for " +
@@ -420,12 +430,13 @@ function assignVariable
420430
algorithm
421431
() := match (variable, value)
422432
local
423-
Expression val;
433+
Expression var, val;
424434
list<Expression> vals;
435+
Mutable<Expression> var_ptr;
425436

426-
case (Expression.MUTABLE(), _)
437+
case (Expression.MUTABLE(exp = var_ptr), _)
427438
algorithm
428-
Mutable.update(variable.exp, assignExp(Mutable.access(variable.exp), value));
439+
Mutable.update(var_ptr, assignExp(Mutable.access(var_ptr), value));
429440
then
430441
();
431442

@@ -438,6 +449,13 @@ algorithm
438449
then
439450
();
440451

452+
case (Expression.SUBSCRIPTED_EXP(exp = Expression.MUTABLE(exp = var_ptr)), _)
453+
algorithm
454+
Error.assertion(false, getInstanceName() +
455+
": missing handling of subscripted assignment", sourceInfo());
456+
then
457+
();
458+
441459
else
442460
algorithm
443461
Error.assertion(false, getInstanceName() + " failed on " +

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ public
553553
case SUBSCRIPTED_EXP() then exp.ty;
554554
case TUPLE_ELEMENT() then exp.ty;
555555
case BOX() then Type.METABOXED(typeOf(exp.exp));
556+
case MUTABLE() then typeOf(Mutable.access(exp.exp));
556557
else Type.UNKNOWN();
557558
end match;
558559
end typeOf;

Compiler/NFFrontEnd/NFFunction.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ uniontype Function
208208
Type returnType;
209209
DAE.FunctionAttributes attributes;
210210
Pointer<Boolean> collected "Whether this function has already been added to the function tree or not.";
211-
Pointer<Integer> callCounter "Used during function evaluation to avoid infinite loops.";
211+
Pointer<Integer> callCounter "Used during function evaluation to limit recursion.";
212212
end FUNCTION;
213213

214214
function new

Compiler/NFFrontEnd/NFType.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,17 @@ public
517517
end match;
518518
end dimensionCount;
519519

520+
function hasKnownSize
521+
input Type ty;
522+
output Boolean isKnown;
523+
algorithm
524+
isKnown := match ty
525+
case ARRAY() then List.all(ty.dimensions, function Dimension.isKnown(allowExp = false));
526+
case FUNCTION() then hasKnownSize(ty.resultType);
527+
else true;
528+
end match;
529+
end hasKnownSize;
530+
520531
function mapDims
521532
input output Type ty;
522533
input FuncT func;

0 commit comments

Comments
 (0)