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

Commit 2f395d3

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix typing of min/max.
- Do manual typing of min/max instead of relying on polymorphic types that doesn't work correctly. Belonging to [master]: - #2647 - OpenModelica/OpenModelica-testsuite#1030
1 parent 22bbcbb commit 2f395d3

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public
114114
case "initial" then typeDiscreteCall(call, origin, info);
115115
case "isRoot" then typeIsRootCall(call, origin, info);
116116
case "matrix" then typeMatrixCall(call, origin, info);
117-
case "max" then typeMinMaxCall(call, origin, info);
118-
case "min" then typeMinMaxCall(call, origin, info);
117+
case "max" then typeMinMaxCall("max", call, origin, info);
118+
case "min" then typeMinMaxCall("min", call, origin, info);
119119
case "ndims" then typeNdimsCall(call, origin, info);
120120
case "noEvent" then typeNoEventCall(call, origin, info);
121121
case "ones" then typeZerosOnesCall("ones", call, origin, info);
@@ -664,23 +664,75 @@ protected
664664
end typeEdgeCall;
665665

666666
function typeMinMaxCall
667+
input String name;
667668
input Call call;
668669
input ExpOrigin.Type origin;
669670
input SourceInfo info;
670671
output Expression callExp;
671672
output Type ty;
672673
output Variability var;
673674
protected
674-
Call argtycall;
675+
ComponentRef fn_ref;
676+
list<Expression> args;
677+
list<NamedArg> named_args;
678+
Expression arg;
679+
Function fn;
680+
Expression arg1, arg2;
681+
Type ty1, ty2;
682+
Variability var1, var2;
683+
TypeCheck.MatchKind mk;
675684
algorithm
676-
argtycall := Call.typeMatchNormalCall(call, origin, info);
677-
argtycall := Call.unboxArgs(argtycall);
678-
ty := Call.typeOf(argtycall);
679-
var := Call.variability(argtycall);
680-
callExp := Expression.CALL(argtycall);
681-
// TODO: check basic type in two argument overload.
682-
// check arrays of simple types in one argument overload.
683-
// fix return type.
685+
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
686+
assertNoNamedParams(name, named_args, info);
687+
688+
(args, ty, var) := match args
689+
case {arg1}
690+
algorithm
691+
(arg1, ty1, var1) := Typing.typeExp(arg1, origin, info);
692+
ty := Type.arrayElementType(ty1);
693+
694+
if not (Type.isArray(ty1) and Type.isBasic(ty)) then
695+
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
696+
{"1", name, "", Expression.toString(arg1), Type.toString(ty1), "Any[:, ...]"}, info);
697+
end if;
698+
699+
then
700+
({arg1}, ty, var1);
701+
702+
case {arg1, arg2}
703+
algorithm
704+
(arg1, ty1, var1) := Typing.typeExp(arg1, origin, info);
705+
(arg2, ty2, var2) := Typing.typeExp(arg2, origin, info);
706+
707+
if not Type.isBasic(ty1) then
708+
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
709+
{"1", name, "", Expression.toString(arg1), Type.toString(ty1), "Any"}, info);
710+
end if;
711+
712+
if not Type.isBasic(ty2) then
713+
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
714+
{"2", name, "", Expression.toString(arg2), Type.toString(ty2), "Any"}, info);
715+
end if;
716+
717+
(arg1, arg2, ty, mk) := TypeCheck.matchExpressions(arg1, ty1, arg2, ty2);
718+
719+
if not TypeCheck.isValidArgumentMatch(mk) then
720+
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
721+
{Call.toString(call), name + "(Any[:, ...]) => Any\n" + name + "(Any, Any) => Any"}, info);
722+
end if;
723+
then
724+
({arg1, arg2}, ty, Prefixes.variabilityMax(var1, var2));
725+
726+
else
727+
algorithm
728+
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
729+
{Call.toString(call), name + "(Any[:, ...]) => Any\n" + name + "(Any, Any) => Any"}, info);
730+
then
731+
fail();
732+
end match;
733+
734+
fn := listHead(Function.typeRefCache(fn_ref));
735+
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, ty));
684736
end typeMinMaxCall;
685737

686738
function typeSumCall

Compiler/NFFrontEnd/NFModelicaBuiltin.mo

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,24 @@ function delay = $overload(OpenModelica.Internal.delay2,OpenModelica.Internal.de
373373
// function min = $overload(OpenModelica.Internal.realMin, OpenModelica.Internal.intMin
374374
// , OpenModelica.Internal.boolMin, OpenModelica.Internal.enumMin
375375
// , OpenModelica.Internal.arrayMin)
376-
function min = $overload(OpenModelica.Internal.scalarMin, OpenModelica.Internal.arrayMin)
376+
//function min = $overload(OpenModelica.Internal.scalarMin, OpenModelica.Internal.arrayMin)
377+
function min
378+
external "builtin";
377379
annotation(Documentation(info="<html>
378380
See <a href=\"modelica://ModelicaReference.Operators.'min()'\">min()</a>
379381
</html>"));
382+
end min;
380383

381384
// function max = $overload(OpenModelica.Internal.realMax, OpenModelica.Internal.intMax
382385
// , OpenModelica.Internal.boolMax, OpenModelica.Internal.enumMax
383386
// , OpenModelica.Internal.arrayMax)
384-
function max = $overload(OpenModelica.Internal.scalarMax, OpenModelica.Internal.arrayMax)
387+
//function max = $overload(OpenModelica.Internal.scalarMax, OpenModelica.Internal.arrayMax)
388+
function max
389+
external "builtin";
385390
annotation(Documentation(info="<html>
386391
See <a href=\"modelica://ModelicaReference.Operators.'max()'\">max()</a>
387392
</html>"));
393+
end max;
388394

389395
function sum<ArrayType, ScalarBasicType> "Sum of all array elements"
390396
input ArrayType a;

0 commit comments

Comments
 (0)