@@ -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;
11771180algorithm
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 ;
12051210end typeExpDim;
12061211
12071212function typeArrayDim
0 commit comments