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

Commit de9ded9

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improve typing of size expression.
- Handle size expressions such as x[:, size(x, 1)] in functions. Belonging to [master]: - #2318 - OpenModelica/OpenModelica-testsuite#899
1 parent 75bf644 commit de9ded9

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,21 @@ algorithm
439439
// give different results depending on the declaration order of components.
440440
case Dimension.UNTYPED(isProcessing = true)
441441
algorithm
442-
// TODO: Tell the user which variables are involved in the loop (can be
443-
// found with DFS on the dimension expression. Maybe have a limit
444-
// on the output in case there's a lot of dimensions involved.
445-
Error.addSourceMessage(Error.CYCLIC_DIMENSIONS,
446-
{String(index), InstNode.name(component), Expression.toString(dimension.dimension)}, info);
442+
// Only give an error if we're not in a function.
443+
if intBitAnd(origin, ExpOrigin.FUNCTION) == 0 then
444+
// TODO: Tell the user which variables are involved in the loop (can be
445+
// found with DFS on the dimension expression. Maybe have a limit
446+
// on the output in case there's a lot of dimensions involved.
447+
Error.addSourceMessage(Error.CYCLIC_DIMENSIONS,
448+
{String(index), InstNode.name(component), Expression.toString(dimension.dimension)}, info);
449+
fail();
450+
end if;
451+
452+
// If we are in a functions we allow e.g. size expression of unknown dimensions.
453+
dim := Dimension.UNKNOWN();
454+
arrayUpdate(dimensions, index, dim);
447455
then
448-
fail();
456+
dim;
449457

450458
// If the dimension is not typed, type it.
451459
case Dimension.UNTYPED()
@@ -1163,12 +1171,14 @@ function typeExpDim
11631171
input Integer dimIndex;
11641172
input ExpOrigin.Type origin;
11651173
input SourceInfo info;
1166-
output Dimension dim;
1167-
output TypingError error;
1174+
output Dimension dim;
1175+
output Option<Expression> typedExp = NONE();
1176+
output TypingError error;
11681177
algorithm
11691178
(dim, error) := match exp
11701179
local
11711180
Type ty;
1181+
Expression e;
11721182

11731183
// An untyped array, use typeArrayDim to get the dimension.
11741184
case Expression.ARRAY(ty = Type.UNKNOWN())
@@ -1186,7 +1196,8 @@ algorithm
11861196
// from the type.
11871197
else
11881198
algorithm
1189-
(_, ty, _) := typeExp(exp, origin, info);
1199+
(e, ty, _) := typeExp(exp, origin, info);
1200+
typedExp := SOME(e);
11901201
then
11911202
nthDimensionBoundsChecked(ty, dimIndex);
11921203

@@ -1726,6 +1737,7 @@ protected
17261737
Integer iindex, dim_size;
17271738
Dimension dim;
17281739
TypingError ty_err;
1740+
Option<Expression> oexp;
17291741
algorithm
17301742
(sizeExp, sizeType, variability) := match sizeExp
17311743
case Expression.SIZE(exp = exp, dimIndex = SOME(index))
@@ -1751,7 +1763,7 @@ algorithm
17511763
Expression.INTEGER(iindex) := index;
17521764

17531765
// Get the iindex'd dimension of the expression.
1754-
(dim, ty_err) := typeExpDim(exp, iindex, origin, info);
1766+
(dim, oexp, ty_err) := typeExpDim(exp, iindex, origin, info);
17551767
checkSizeTypingError(ty_err, exp, iindex, info);
17561768

17571769
if Dimension.isKnown(dim) and evaluate then
@@ -1760,7 +1772,12 @@ algorithm
17601772
else
17611773
// If the dimension size is unknown (e.g. in a function) or
17621774
// evaluation is disabled, return a size expression instead.
1763-
exp := typeExp(sizeExp.exp, origin, info);
1775+
if isSome(oexp) then
1776+
SOME(exp) := oexp;
1777+
else
1778+
exp := typeExp(exp, origin, info);
1779+
end if;
1780+
17641781
exp := Expression.SIZE(exp, SOME(index));
17651782
end if;
17661783

0 commit comments

Comments
 (0)