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

Commit 4b0bcc2

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improve Typing.typeExpDim.
- Try harder to use the type of the expression if it has one. Belonging to [master]: - #2916
1 parent 68cc738 commit 4b0bcc2

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

Compiler/NFFrontEnd/NFDimension.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public
288288
then Expression.makeEnumLiteral(ty, listLength(ty.literals));
289289
case EXP() then dim.exp;
290290
case UNKNOWN()
291-
then Expression.SIZE(Expression.CREF(Type.INTEGER(), ComponentRef.stripSubscripts(cref)),
291+
then Expression.SIZE(Expression.CREF(Type.UNKNOWN(), ComponentRef.stripSubscripts(cref)),
292292
SOME(Expression.INTEGER(index)));
293293
end match;
294294
end endExp;

Compiler/NFFrontEnd/NFType.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ public
427427
end match;
428428
end isUnknown;
429429

430+
function isKnown
431+
input Type ty;
432+
output Boolean isKnown;
433+
algorithm
434+
isKnown := match ty
435+
case UNKNOWN() then false;
436+
else true;
437+
end match;
438+
end isKnown;
439+
430440
function isPolymorphic
431441
input Type ty;
432442
output Boolean isPolymorphic;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,34 +1174,39 @@ function typeExpDim
11741174
output Dimension dim;
11751175
output Option<Expression> typedExp = NONE();
11761176
output TypingError error;
1177+
protected
1178+
Type ty;
1179+
Expression e;
11771180
algorithm
1178-
(dim, error) := match exp
1179-
local
1180-
Type ty;
1181-
Expression e;
1181+
ty := Expression.typeOf(exp);
11821182

1183-
// An untyped array, use typeArrayDim to get the dimension.
1184-
case Expression.ARRAY(ty = Type.UNKNOWN())
1185-
then typeArrayDim(exp, dimIndex);
1186-
1187-
// A typed array, fetch the dimension from its type.
1188-
case Expression.ARRAY()
1189-
then nthDimensionBoundsChecked(exp.ty, dimIndex);
1190-
1191-
// A cref, use typeCrefDim to get the dimension.
1192-
case Expression.CREF()
1193-
then typeCrefDim(exp.cref, dimIndex, origin, info);
1194-
1195-
// Any other expression, type the whole expression and get the dimension
1196-
// from the type.
1197-
else
1198-
algorithm
1199-
(e, ty, _) := typeExp(exp, origin, info);
1200-
typedExp := SOME(e);
1201-
then
1202-
nthDimensionBoundsChecked(ty, dimIndex);
1183+
if Type.isKnown(ty) then
1184+
// If the expression has already been typed, just get the dimension from the type.
1185+
(dim, error) := nthDimensionBoundsChecked(ty, dimIndex);
1186+
typedExp := SOME(exp);
1187+
else
1188+
// Otherwise we try to type as little as possible of the expression to get
1189+
// the dimension we need, to avoid introducing unnecessary cycles.
1190+
(dim, error) := match exp
1191+
// An untyped array, use typeArrayDim to get the dimension.
1192+
case Expression.ARRAY(ty = Type.UNKNOWN())
1193+
then typeArrayDim(exp, dimIndex);
1194+
1195+
// A cref, use typeCrefDim to get the dimension.
1196+
case Expression.CREF()
1197+
then typeCrefDim(exp.cref, dimIndex, origin, info);
1198+
1199+
// Any other expression, type the whole expression and get the dimension
1200+
// from the type.
1201+
else
1202+
algorithm
1203+
(e, ty, _) := typeExp(exp, origin, info);
1204+
typedExp := SOME(e);
1205+
then
1206+
nthDimensionBoundsChecked(ty, dimIndex);
12031207

1204-
end match;
1208+
end match;
1209+
end if;
12051210
end typeExpDim;
12061211

12071212
function typeArrayDim

0 commit comments

Comments
 (0)