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

Commit e92a0b5

Browse files
perostOpenModelica-Hudson
authored andcommitted
Fix min/max for enumerations.
- Fixed elaboration and constant evaluation of min/max with enumeration arguments.
1 parent c2054ff commit e92a0b5

File tree

4 files changed

+34
-59
lines changed

4 files changed

+34
-59
lines changed

Compiler/FrontEnd/Ceval.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,8 @@ algorithm
36803680
case (Values.INTEGER(i1), Values.INTEGER(i2)) then Values.INTEGER(max(i1, i2));
36813681
case (Values.REAL(r1), Values.REAL(r2)) then Values.REAL(max(r1, r2));
36823682
case (Values.BOOL(b1), Values.BOOL(b2)) then Values.BOOL(b1 or b2);
3683+
case (Values.ENUM_LITERAL(), Values.ENUM_LITERAL())
3684+
then if v1.index > v2.index then v1 else v2;
36833685
else
36843686
algorithm
36853687
true := Flags.isSet(Flags.FAILTRACE);
@@ -3756,6 +3758,8 @@ algorithm
37563758
case (Values.INTEGER(i1), Values.INTEGER(i2)) then Values.INTEGER(min(i1, i2));
37573759
case (Values.REAL(r1), Values.REAL(r2)) then Values.REAL(min(r1, r2));
37583760
case (Values.BOOL(b1), Values.BOOL(b2)) then Values.BOOL(b1 and b2);
3761+
case (Values.ENUM_LITERAL(), Values.ENUM_LITERAL())
3762+
then if v1.index < v2.index then v1 else v2;
37593763
else
37603764
algorithm
37613765
true := Flags.isSet(Flags.FAILTRACE);

Compiler/FrontEnd/Static.mo

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,14 @@ algorithm
18651865
then
18661866
(exp, ty, ty, NONE(), fn);
18671867

1868+
case (Absyn.IDENT("min"), DAE.T_ENUMERATION())
1869+
algorithm
1870+
v := Values.ENUM_LITERAL(Absyn.suffixPath(unboxedType.path,
1871+
List.last(unboxedType.names)), listLength(unboxedType.names));
1872+
(exp, ty) := Types.matchType(inExp, inType, DAE.T_ENUMERATION_DEFAULT, true);
1873+
then
1874+
(exp, ty, ty, SOME(v), fn);
1875+
18681876
case (Absyn.IDENT("max"), DAE.T_REAL())
18691877
algorithm
18701878
r := realNeg(System.realMaxLit());
@@ -1895,6 +1903,13 @@ algorithm
18951903
then
18961904
(exp, ty, ty, SOME(v), fn);
18971905

1906+
case (Absyn.IDENT("max"), DAE.T_ENUMERATION())
1907+
algorithm
1908+
v := Values.ENUM_LITERAL(Absyn.suffixPath(unboxedType.path, listHead(unboxedType.names)), 1);
1909+
(exp, ty) := Types.matchType(inExp, inType, DAE.T_ENUMERATION_DEFAULT, true);
1910+
then
1911+
(exp, ty, ty, SOME(v), fn);
1912+
18981913
case (Absyn.IDENT("sum"), DAE.T_REAL())
18991914
algorithm
19001915
v := Values.REAL(0.0);
@@ -4508,62 +4523,54 @@ end elabBuiltinMin;
45084523
protected function elabBuiltinMinMaxCommon
45094524
"Helper function to elabBuiltinMin and elabBuiltinMax, containing common
45104525
functionality."
4511-
input FCore.Cache inCache;
4512-
input FCore.Graph inEnv;
4526+
input output FCore.Cache cache;
4527+
input FCore.Graph env;
45134528
input String inFnName;
45144529
input list<Absyn.Exp> inFnArgs;
4515-
input Boolean inImpl;
4516-
input Prefix.Prefix inPrefix;
4530+
input Boolean impl;
4531+
input Prefix.Prefix prefix;
45174532
input SourceInfo info;
4518-
output FCore.Cache outCache;
4519-
output DAE.Exp outExp;
4520-
output DAE.Properties outProperties;
4533+
output DAE.Exp outExp;
4534+
output DAE.Properties outProperties;
45214535
algorithm
4522-
(outCache, outExp, outProperties):=
4523-
match (inCache, inEnv, inFnName, inFnArgs, inImpl, inPrefix, info)
4536+
(outExp, outProperties) := match inFnArgs
45244537
local
45254538
DAE.Exp arrexp_1,s1_1,s2_1, call;
45264539
DAE.Type tp;
45274540
DAE.Type ty,ty1,ty2,elt_ty;
45284541
DAE.Const c,c1,c2;
4529-
FCore.Graph env;
45304542
Absyn.Exp arrexp,s1,s2;
4531-
Boolean impl;
4532-
FCore.Cache cache;
4533-
Prefix.Prefix pre;
45344543
DAE.Properties p;
45354544

45364545
// min|max(vector)
4537-
case (cache, env, _, {arrexp}, impl, pre, _)
4546+
case {arrexp}
45384547
equation
45394548
(cache, arrexp_1, DAE.PROP(ty, c), _) =
4540-
elabExpInExpression(cache, env, arrexp, impl,NONE(), true, pre, info);
4549+
elabExpInExpression(cache, env, arrexp, impl,NONE(), true, prefix, info);
45414550
true = Types.isArray(ty);
45424551
arrexp_1 = Expression.matrixToArray(arrexp_1);
45434552
elt_ty = Types.arrayElementType(ty);
45444553
tp = Types.simplifyType(elt_ty);
45454554
false = Types.isString(tp);
45464555
call = Expression.makePureBuiltinCall(inFnName, {arrexp_1}, tp);
45474556
then
4548-
(cache, call, DAE.PROP(elt_ty,c));
4557+
(call, DAE.PROP(elt_ty,c));
45494558

45504559
// min|max(x,y) where x & y are scalars.
4551-
case (cache, env, _, {s1, s2}, impl, pre, _)
4560+
case {s1, s2}
45524561
equation
45534562
(cache, s1_1, DAE.PROP(ty1, c1), _) =
4554-
elabExpInExpression(cache, env, s1, impl,NONE(), true, pre, info);
4563+
elabExpInExpression(cache, env, s1, impl, NONE(), true, prefix, info);
45554564
(cache, s2_1, DAE.PROP(ty2, c2), _) =
4556-
elabExpInExpression(cache, env, s2, impl,NONE(), true, pre, info);
4565+
elabExpInExpression(cache, env, s2, impl, NONE(), true, prefix, info);
45574566

4558-
ty = Types.scalarSuperType(ty1,ty2);
4559-
(s1_1,_) = Types.matchType(s1_1, ty1, ty, true);
4560-
(s2_1,_) = Types.matchType(s2_1, ty2, ty, true);
4567+
(s1_1, s2_1, ty, true) = Types.checkTypeCompat(s1_1, ty1, s2_1, ty2);
45614568
c = Types.constAnd(c1, c2);
45624569
tp = Types.simplifyType(ty);
45634570
false = Types.isString(tp);
45644571
call = Expression.makePureBuiltinCall(inFnName, {s1_1, s2_1}, tp);
45654572
then
4566-
(cache, call, DAE.PROP(ty,c));
4573+
(call, DAE.PROP(ty,c));
45674574

45684575
end match;
45694576
end elabBuiltinMinMaxCommon;

Compiler/FrontEnd/Types.mo

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7292,27 +7292,6 @@ algorithm
72927292
end match;
72937293
end varHasMetaRecordType;
72947294

7295-
public function scalarSuperType
7296-
"Checks that the givens types are scalar and that one is subtype of the other (in the case of integers)."
7297-
input DAE.Type ity1;
7298-
input DAE.Type ity2;
7299-
output DAE.Type ty;
7300-
algorithm
7301-
ty := match (ity1,ity2)
7302-
local Type ty1, ty2;
7303-
case (DAE.T_INTEGER(),DAE.T_INTEGER()) then DAE.T_INTEGER_DEFAULT;
7304-
case (DAE.T_REAL(),DAE.T_REAL()) then DAE.T_REAL_DEFAULT;
7305-
case (DAE.T_INTEGER(),DAE.T_REAL()) then DAE.T_REAL_DEFAULT;
7306-
case (DAE.T_REAL(),DAE.T_INTEGER()) then DAE.T_REAL_DEFAULT;
7307-
case (DAE.T_SUBTYPE_BASIC(complexType = ty1),ty2) then scalarSuperType(ty1,ty2);
7308-
case (ty1,DAE.T_SUBTYPE_BASIC(complexType = ty2)) then scalarSuperType(ty1,ty2);
7309-
7310-
case (DAE.T_BOOL(),DAE.T_BOOL()) then DAE.T_BOOL_DEFAULT;
7311-
// adrpo: TODO? Why not string here?
7312-
// case (DAE.T_STRING(varLst = _),DAE.T_STRING(varLst = _)) then DAE.T_STRING_DEFAULT;
7313-
end match;
7314-
end scalarSuperType;
7315-
73167295
protected function optInteger
73177296
input Option<Integer> inInt;
73187297
output Integer outInt;

Compiler/NFFrontEnd/NFType.mo

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,6 @@ public
367367
end match;
368368
end dimensionCount;
369369

370-
function scalarSuperType
371-
"Checks that the given types are scalar and that one is a subtype of the other."
372-
input Type ty1;
373-
input Type ty2;
374-
output Type ty;
375-
algorithm
376-
ty := match (ty1, ty2)
377-
case (INTEGER(), INTEGER()) then INTEGER();
378-
case (REAL(), REAL()) then REAL();
379-
case (INTEGER(), REAL()) then REAL();
380-
case (REAL(), INTEGER()) then REAL();
381-
case (BOOLEAN(), BOOLEAN()) then BOOLEAN();
382-
end match;
383-
end scalarSuperType;
384-
385370
function toString
386371
input Type ty;
387372
output String str;

0 commit comments

Comments
 (0)