Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit facb5c9

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improve handling of scalar().
- Implement expansion of size(x). - Try to expand the argument to scalar and retrieve the result directly instead of creating a call to scalar, since scalar isn't implemented in the runtime. Belonging to [master]: - #2539 - OpenModelica/OpenModelica-testsuite#985
1 parent 5615485 commit facb5c9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected
5858
import TypeCheck = NFTypeCheck;
5959
import Typing = NFTyping;
6060
import Util;
61+
import ExpandExp = NFExpandExp;
6162

6263
public
6364
function needSpecialHandling
@@ -885,6 +886,7 @@ protected
885886
Expression arg;
886887
Variability var;
887888
Function fn;
889+
Boolean expanded;
888890
algorithm
889891
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
890892
assertNoNamedParams("scalar", named_args, info);
@@ -904,9 +906,22 @@ protected
904906
end if;
905907
end for;
906908

909+
(arg, expanded) := ExpandExp.expand(arg);
907910
ty := Type.arrayElementType(ty);
908-
{fn} := Function.typeRefCache(fn_ref);
909-
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
911+
912+
if expanded then
913+
args := Expression.arrayScalarElements(arg);
914+
915+
if listLength(args) <> 1 then
916+
Error.assertion(false, getInstanceName() + " failed to expand scalar(" +
917+
Expression.toString(arg) + ") correctly", info);
918+
end if;
919+
920+
callExp := listHead(args);
921+
else
922+
{fn} := Function.typeRefCache(fn_ref);
923+
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
924+
end if;
910925
end typeScalarCall;
911926

912927
function typeVectorCall

Compiler/NFFrontEnd/NFExpandExp.mo

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public
6868
case Expression.ARRAY() then (exp, true);
6969
case Expression.RANGE() then expandRange(exp);
7070
case Expression.CALL() then expandCall(exp.call, exp);
71+
case Expression.SIZE() then expandSize(exp);
7172
case Expression.BINARY() then expandBinary(exp, exp.operator);
7273
case Expression.UNARY() then expandUnary(exp.exp, exp.operator);
7374
case Expression.LBINARY() then expandLogicalBinary(exp);
@@ -338,6 +339,31 @@ public
338339
end if;
339340
end expandReduction2;
340341

342+
function expandSize
343+
input Expression exp;
344+
output Expression outExp;
345+
output Boolean expanded = true;
346+
algorithm
347+
outExp := match exp
348+
local
349+
Integer dims;
350+
Expression e;
351+
Type ty;
352+
list<Expression> expl;
353+
354+
case Expression.SIZE(exp = e, dimIndex = NONE())
355+
algorithm
356+
ty := Expression.typeOf(e);
357+
dims := Type.dimensionCount(ty);
358+
expl := list(Expression.SIZE(e, SOME(Expression.INTEGER(i))) for i in 1:dims);
359+
then
360+
Expression.ARRAY(Type.ARRAY(ty, {Dimension.fromInteger(dims)}), expl);
361+
362+
// Size with an index is scalar, and thus already maximally expanded.
363+
else exp;
364+
end match;
365+
end expandSize;
366+
341367
function expandBinary
342368
input Expression exp;
343369
input Operator op;

0 commit comments

Comments
 (0)