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

Commit 1769e70

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix the variability of size.
Belonging to [master]: - #2294 - OpenModelica/OpenModelica-testsuite#886
1 parent 921ea29 commit 1769e70

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ uniontype Call
10641064
dims := Type.arrayDims(resTy);
10651065
resTyToMatch := Type.ARRAY(Type.arrayElementType(resTy), List.set(dims, n, Dimension.UNKNOWN()));
10661066
dims := list(listGet(lst, n) for lst in dimsLst);
1067-
sumDim := Dimension.INTEGER(0);
1067+
sumDim := Dimension.fromInteger(0);
10681068
for d in dims loop
10691069
// Create the concatenated dimension
10701070
sumDim := Dimension.add(sumDim, d);
@@ -2019,7 +2019,7 @@ protected
20192019
dim_size := 1;
20202020
end if;
20212021

2022-
ty := Type.ARRAY(Type.arrayElementType(ty), {Dimension.INTEGER(dim_size)});
2022+
ty := Type.ARRAY(Type.arrayElementType(ty), {Dimension.fromInteger(dim_size)});
20232023
{fn} := typeCachedFunctions(fn_ref);
20242024
callExp := Expression.CALL(makeBuiltinCall2(fn, {arg}, ty, variability));
20252025
end typeVectorCall;
@@ -2058,8 +2058,8 @@ protected
20582058
dims := Type.arrayDims(ty);
20592059

20602060
dims := match listLength(dims)
2061-
case 0 then {Dimension.INTEGER(1), Dimension.INTEGER(1)};
2062-
case 1 then {listHead(dims), Dimension.INTEGER(1)};
2061+
case 0 then {Dimension.fromInteger(1), Dimension.fromInteger(1)};
2062+
case 1 then {listHead(dims), Dimension.fromInteger(1)};
20632063
case 2 then dims;
20642064
else
20652065
algorithm

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Dimension = NFDimension;
4444
import Type = NFType;
4545
import NFTyping.ExpOrigin;
4646
import ExpressionSimplify;
47+
import NFPrefixes.Variability;
4748

4849
protected
4950
import NFFunction.Function;
@@ -177,8 +178,9 @@ algorithm
177178
case Expression.SIZE()
178179
algorithm
179180
expl := list(Expression.INTEGER(Dimension.size(d)) for d in Type.arrayDims(Expression.typeOf(exp.exp)));
181+
dim := Dimension.INTEGER(listLength(expl), Variability.PARAMETER);
180182
then
181-
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.INTEGER(listLength(expl))}), expl);
183+
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {dim}), expl);
182184

183185
case Expression.BINARY()
184186
algorithm
@@ -486,7 +488,7 @@ protected
486488
Type ty;
487489
algorithm
488490
ty := Expression.typeOf(listHead(args));
489-
ty := Type.liftArrayLeft(ty, Dimension.INTEGER(listLength(args)));
491+
ty := Type.liftArrayLeft(ty, Dimension.fromInteger(listLength(args)));
490492
result := Expression.ARRAY(ty, args);
491493
end evalBuiltinArray;
492494

@@ -560,7 +562,7 @@ algorithm
560562
fail();
561563
end if;
562564
(es,dims) := ExpressionSimplify.evalCat(n, args, getArrayContents=Expression.arrayElements, toString=Expression.toString);
563-
result := Expression.arrayFromList(es, Type.arrayElementType(ty), list(Dimension.INTEGER(d) for d in dims));
565+
result := Expression.arrayFromList(es, Type.arrayElementType(ty), list(Dimension.fromInteger(d) for d in dims));
564566
end evalBuiltinCat;
565567

566568
function evalBuiltinCeil
@@ -608,7 +610,7 @@ algorithm
608610
z2 := Expression.REAL(x3 * y1 - x1 * y3);
609611
z3 := Expression.REAL(x1 * y2 - x2 * y1);
610612
then
611-
Expression.ARRAY(Type.ARRAY(Type.REAL(), {Dimension.INTEGER(3)}), {z1, z2, z3});
613+
Expression.ARRAY(Type.ARRAY(Type.REAL(), {Dimension.fromInteger(3)}), {z1, z2, z3});
612614

613615
else algorithm printWrongArgsError(getInstanceName(), args, sourceInfo()); then fail();
614616
end match;
@@ -636,7 +638,7 @@ algorithm
636638
n := listLength(elems);
637639

638640
elem_ty := Expression.typeOf(listHead(elems));
639-
row_ty := Type.liftArrayLeft(elem_ty, Dimension.INTEGER(n));
641+
row_ty := Type.liftArrayLeft(elem_ty, Dimension.fromInteger(n));
640642
zero := Expression.makeZero(elem_ty);
641643

642644
for e in listReverse(elems) loop
@@ -656,7 +658,7 @@ algorithm
656658
rows := Expression.ARRAY(row_ty, row) :: rows;
657659
end for;
658660
then
659-
Expression.ARRAY(Type.liftArrayLeft(row_ty, Dimension.INTEGER(n)), rows);
661+
Expression.ARRAY(Type.liftArrayLeft(row_ty, Dimension.fromInteger(n)), rows);
660662

661663
else algorithm printWrongArgsError(getInstanceName(), {arg}, sourceInfo()); then fail();
662664
end match;
@@ -736,7 +738,7 @@ algorithm
736738
end match;
737739

738740
arr := list(result for e in 1:dim_size);
739-
arr_ty := Type.liftArrayLeft(arr_ty, Dimension.INTEGER(dim_size));
741+
arr_ty := Type.liftArrayLeft(arr_ty, Dimension.fromInteger(dim_size));
740742
result := Expression.ARRAY(arr_ty, arr);
741743
end for;
742744
end evalBuiltinFill2;
@@ -981,18 +983,18 @@ protected
981983
algorithm
982984
Error.assertion(n >= 1, "Promote called with n<1", sourceInfo());
983985
if n == 1 then
984-
result := Expression.ARRAY(Type.liftArrayLeft(Expression.typeOf(arg),Dimension.INTEGER(1)), {arg});
986+
result := Expression.ARRAY(Type.liftArrayLeft(Expression.typeOf(arg),Dimension.fromInteger(1)), {arg});
985987
return;
986988
end if;
987989
result := match arg
988990
case Expression.ARRAY()
989991
algorithm
990992
(exps as (Expression.ARRAY(ty=ty)::_)) := list(evalBuiltinPromoteWork(e, n-1) for e in arg.elements);
991-
then Expression.ARRAY(Type.liftArrayLeft(ty,Dimension.INTEGER(listLength(arg.elements))), exps);
993+
then Expression.ARRAY(Type.liftArrayLeft(ty,Dimension.fromInteger(listLength(arg.elements))), exps);
992994
else
993995
algorithm
994996
(exp as Expression.ARRAY(ty=ty)) := evalBuiltinPromoteWork(arg, n-1);
995-
then Expression.ARRAY(Type.liftArrayLeft(ty,Dimension.INTEGER(1)), {exp});
997+
then Expression.ARRAY(Type.liftArrayLeft(ty,Dimension.fromInteger(1)), {exp});
996998
end match;
997999
end evalBuiltinPromoteWork;
9981000

@@ -1096,7 +1098,7 @@ algorithm
10961098
y1 := Expression.ARRAY(ty, {zero, Expression.negate(x3), x2});
10971099
y2 := Expression.ARRAY(ty, {x3, zero, Expression.negate(x1)});
10981100
y3 := Expression.ARRAY(ty, {Expression.negate(x2), x1, zero});
1099-
ty := Type.liftArrayLeft(ty, Dimension.INTEGER(3));
1101+
ty := Type.liftArrayLeft(ty, Dimension.fromInteger(3));
11001102
then
11011103
Expression.ARRAY(ty, {y1, y2, y3});
11021104

@@ -1203,7 +1205,7 @@ protected
12031205
algorithm
12041206
expl := Expression.fold(arg, evalBuiltinVector2, {});
12051207
ty := Type.liftArrayLeft(Type.arrayElementType(Expression.typeOf(arg)),
1206-
Dimension.INTEGER(listLength(expl)));
1208+
Dimension.fromInteger(listLength(expl)));
12071209
result := Expression.ARRAY(ty, listReverse(expl));
12081210
end evalBuiltinVector;
12091211

Compiler/NFFrontEnd/NFDimension.mo

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public
5858

5959
record INTEGER
6060
Integer size;
61+
Variability var;
6162
end INTEGER;
6263

6364
record BOOLEAN
@@ -86,7 +87,7 @@ public
8687
ComponentRef cref;
8788
Type ty;
8889

89-
case Expression.INTEGER() then INTEGER(exp.value);
90+
case Expression.INTEGER() then INTEGER(exp.value, var);
9091

9192
case Expression.TYPENAME(ty = Type.ARRAY(elementType = ty))
9293
then
@@ -104,9 +105,14 @@ public
104105
end match;
105106
end fromExp;
106107

108+
function fromInteger
109+
input Integer n;
110+
output Dimension dim = INTEGER(n, Variability.CONSTANT);
111+
end fromInteger;
112+
107113
function fromExpList
108114
input list<Expression> expl;
109-
output Dimension dim = INTEGER(listLength(expl));
115+
output Dimension dim = INTEGER(listLength(expl), Variability.CONSTANT);
110116
end fromExpList;
111117

112118
function toDAE
@@ -133,7 +139,7 @@ public
133139
c := match (a, b)
134140
case (UNKNOWN(),_) then UNKNOWN();
135141
case (_,UNKNOWN()) then UNKNOWN();
136-
case (INTEGER(),INTEGER()) then INTEGER(a.size+b.size);
142+
case (INTEGER(),INTEGER()) then INTEGER(a.size+b.size, Prefixes.variabilityMax(a.var, b.var));
137143
case (INTEGER(),EXP()) then EXP(Expression.BINARY(b.exp, Operator.OPERATOR(Type.INTEGER(), NFOperator.Op.ADD), Expression.INTEGER(a.size)), b.var);
138144
case (EXP(),INTEGER()) then EXP(Expression.BINARY(a.exp, Operator.OPERATOR(Type.INTEGER(), NFOperator.Op.ADD), Expression.INTEGER(b.size)), a.var);
139145
case (EXP(),EXP()) then EXP(Expression.BINARY(a.exp, Operator.OPERATOR(Type.INTEGER(), NFOperator.Op.ADD), b.exp), Prefixes.variabilityMax(a.var, b.var));
@@ -283,5 +289,18 @@ public
283289
end match;
284290
end setScope;
285291

292+
function variability
293+
input Dimension dim;
294+
output Variability var;
295+
algorithm
296+
var := match dim
297+
case INTEGER() then dim.var;
298+
case BOOLEAN() then Variability.CONSTANT;
299+
case ENUM() then Variability.CONSTANT;
300+
case EXP() then dim.var;
301+
case UNKNOWN() then Variability.CONTINUOUS;
302+
end match;
303+
end variability;
304+
286305
annotation(__OpenModelica_Interface="frontend");
287306
end NFDimension;

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ public
20412041
subs := expandCref2(crefExp.cref);
20422042

20432043
if listEmpty(subs) then
2044-
arrayExp := ARRAY(Type.ARRAY(Type.arrayElementType(crefExp.ty), {Dimension.INTEGER(0)}), {});
2044+
arrayExp := ARRAY(Type.ARRAY(Type.arrayElementType(crefExp.ty), {Dimension.fromInteger(0)}), {});
20452045
else
20462046
arrayExp := expandCref3(subs, crefExp.cref, Type.arrayElementType(crefExp.ty));
20472047
end if;
@@ -2696,7 +2696,7 @@ public
26962696
algorithm
26972697
zero := makeZero(elementType);
26982698
one := makeOne(elementType);
2699-
row_ty := Type.ARRAY(elementType, {Dimension.INTEGER(n)});
2699+
row_ty := Type.ARRAY(elementType, {Dimension.fromInteger(n)});
27002700

27012701
for i in 1:n loop
27022702
row := {};
@@ -2714,7 +2714,7 @@ public
27142714
rows := Expression.ARRAY(row_ty, row) :: rows;
27152715
end for;
27162716

2717-
matrix := ARRAY(Type.liftArrayLeft(row_ty, Dimension.INTEGER(n)), rows);
2717+
matrix := ARRAY(Type.liftArrayLeft(row_ty, Dimension.fromInteger(n)), rows);
27182718
end makeIdentityMatrix;
27192719

27202720
function promote
@@ -2724,7 +2724,7 @@ public
27242724
protected
27252725
list<Dimension> dims;
27262726
algorithm
2727-
dims := list(Dimension.INTEGER(1) for i in Type.dimensionCount(ty):n-1);
2727+
dims := list(Dimension.fromInteger(1) for i in Type.dimensionCount(ty):n-1);
27282728
ty := Type.liftArrayRightList(ty, dims);
27292729
e := CALL(Call.makeBuiltinCall2(NFBuiltinFuncs.PROMOTE, {e, INTEGER(n)}, ty));
27302730
end promote;

Compiler/NFFrontEnd/NFSubscript.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public
330330
output Dimension dimension;
331331
algorithm
332332
dimension := match subscript
333-
case INDEX() then Dimension.INTEGER(1);
333+
case INDEX() then Dimension.fromInteger(1);
334334
case SLICE() then listHead(Type.arrayDims(Expression.typeOf(subscript.slice)));
335335
case WHOLE() then Dimension.UNKNOWN();
336336
end match;

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ algorithm
19791979
Integer step;
19801980

19811981
case (Expression.INTEGER(), NONE(), Expression.INTEGER())
1982-
then Dimension.INTEGER(max(stopExp.value - startExp.value + 1, 0));
1982+
then Dimension.fromInteger(max(stopExp.value - startExp.value + 1, 0));
19831983

19841984
case (Expression.INTEGER(), SOME(Expression.INTEGER(value = step)), Expression.INTEGER())
19851985
algorithm
@@ -1988,7 +1988,7 @@ algorithm
19881988
Error.addSourceMessageAndFail(Error.RANGE_ZERO_STEP, {}, info);
19891989
end if;
19901990
then
1991-
Dimension.INTEGER(max(intDiv(stopExp.value - startExp.value, step) + 1, 0));
1991+
Dimension.fromInteger(max(intDiv(stopExp.value - startExp.value, step) + 1, 0));
19921992

19931993
else Dimension.UNKNOWN();
19941994
end match;
@@ -2006,7 +2006,7 @@ algorithm
20062006
Real step;
20072007

20082008
case (Expression.REAL(), NONE(), Expression.REAL())
2009-
then Dimension.INTEGER(Util.realRangeSize(startExp.value, 1.0, stopExp.value));
2009+
then Dimension.fromInteger(Util.realRangeSize(startExp.value, 1.0, stopExp.value));
20102010

20112011
case (Expression.REAL(), SOME(Expression.REAL(value = step)), Expression.REAL())
20122012
algorithm
@@ -2017,7 +2017,7 @@ algorithm
20172017
Error.addSourceMessageAndFail(Error.RANGE_ZERO_STEP, {}, info);
20182018
end if;
20192019
then
2020-
Dimension.INTEGER(Util.realRangeSize(startExp.value, step, stopExp.value));
2020+
Dimension.fromInteger(Util.realRangeSize(startExp.value, step, stopExp.value));
20212021

20222022
else Dimension.UNKNOWN();
20232023
end match;
@@ -2038,7 +2038,7 @@ algorithm
20382038
elseif startExp.value < startExp.value then 2
20392039
else 0;
20402040
then
2041-
Dimension.INTEGER(sz);
2041+
Dimension.fromInteger(sz);
20422042

20432043
else Dimension.UNKNOWN();
20442044
end match;
@@ -2051,7 +2051,7 @@ function getRangeTypeEnum
20512051
algorithm
20522052
dim := match (startExp, stopExp)
20532053
case (Expression.ENUM_LITERAL(), Expression.ENUM_LITERAL())
2054-
then Dimension.INTEGER(max(stopExp.index - startExp.index + 1, 0));
2054+
then Dimension.fromInteger(max(stopExp.index - startExp.index + 1, 0));
20552055

20562056
else Dimension.UNKNOWN();
20572057
end match;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,9 @@ algorithm
17781778
exp := Expression.SIZE(exp, SOME(index));
17791779
end if;
17801780

1781-
variability := Variability.PARAMETER;
1781+
// Use the most variable of the index and the dimension as the variability
1782+
// of the size expression.
1783+
variability := Prefixes.variabilityMax(variability, Dimension.variability(dim));
17821784
else
17831785
// If the index is not a constant, type the whole expression.
17841786
(exp, exp_ty) := typeExp(sizeExp.exp, origin, info);
@@ -1806,7 +1808,7 @@ algorithm
18061808
fail();
18071809
end if;
18081810

1809-
sizeType := Type.ARRAY(Type.INTEGER(), {Dimension.INTEGER(Type.dimensionCount(exp_ty))});
1811+
sizeType := Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(Type.dimensionCount(exp_ty))});
18101812
then
18111813
(Expression.SIZE(exp, NONE()), sizeType, Variability.PARAMETER);
18121814

0 commit comments

Comments
 (0)