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

Commit ea40561

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Add type checking for reductions.
Belonging to [master]: - #2881 - OpenModelica/OpenModelica-testsuite#1107
1 parent bbc0127 commit ea40561

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ protected
10531053
next_origin := intBitOr(origin, ExpOrigin.FOR);
10541054
(arg, ty) := Typing.typeExp(call.exp, next_origin, info);
10551055
{fn} := Function.typeRefCache(call.ref);
1056+
TypeCheck.checkReductionType(ty, Function.name(fn), call.exp, info);
10561057
then
10571058
(TYPED_REDUCTION(fn, ty, variability, arg, iters), ty, variability);
10581059

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ import MetaModelica.Dangerous.*;
7878
import OperatorOverloading = NFOperatorOverloading;
7979
import ExpandExp = NFExpandExp;
8080
import NFFunction.Slot;
81+
import Util;
82+
import System;
8183

8284
public
8385
type MatchKind = enumeration(
@@ -3015,5 +3017,60 @@ algorithm
30153017
end if;
30163018
end checkDimensionType;
30173019

3020+
function checkReductionType
3021+
input Type ty;
3022+
input Absyn.Path name;
3023+
input Expression exp;
3024+
input SourceInfo info;
3025+
protected
3026+
Type ety;
3027+
String err;
3028+
algorithm
3029+
err := match name
3030+
case Absyn.Path.IDENT("sum")
3031+
then
3032+
match Type.arrayElementType(ty)
3033+
case Type.INTEGER() then "";
3034+
case Type.REAL() then "";
3035+
else "Integer or Real";
3036+
end match;
3037+
3038+
case Absyn.Path.IDENT("product")
3039+
then
3040+
match ty
3041+
case Type.INTEGER() then "";
3042+
case Type.REAL() then "";
3043+
else "scalar Integer or Real";
3044+
end match;
3045+
3046+
case Absyn.Path.IDENT("min")
3047+
then
3048+
match ty
3049+
case Type.INTEGER() then "";
3050+
case Type.REAL() then "";
3051+
case Type.BOOLEAN() then "";
3052+
case Type.ENUMERATION() then "";
3053+
else "scalar enumeration, Boolean, Integer or Real";
3054+
end match;
3055+
3056+
case Absyn.Path.IDENT("max")
3057+
then
3058+
match ty
3059+
case Type.INTEGER() then "";
3060+
case Type.REAL() then "";
3061+
case Type.BOOLEAN() then "";
3062+
case Type.ENUMERATION() then "";
3063+
else "scalar enumeration, Boolean, Integer or Real";
3064+
end match;
3065+
3066+
else "";
3067+
end match;
3068+
3069+
if not stringEmpty(err) then
3070+
Error.addSourceMessageAndFail(Error.INVALID_REDUCTION_TYPE,
3071+
{Expression.toString(exp), Type.toString(ty), Absyn.pathString(name), err}, info);
3072+
end if;
3073+
end checkReductionType;
3074+
30183075
annotation(__OpenModelica_Interface="frontend");
30193076
end NFTypeCheck;

Compiler/Util/Error.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ public constant Message OPERATOR_NOT_ENCAPSULATED = MESSAGE(342, TRANSLATION(),
814814
Util.gettext("Operator %s is not encapsulated."));
815815
public constant Message NO_SUCH_INPUT_PARAMETER = MESSAGE(343, TRANSLATION(), ERROR(),
816816
Util.gettext("Function %s has no input parameter named %s."));
817+
public constant Message INVALID_REDUCTION_TYPE = MESSAGE(344, TRANSLATION(), ERROR(),
818+
Util.gettext("Invalid expression ‘%s‘ of type %s in %s reduction, expected %s."));
817819
public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSLATION(), WARNING(),
818820
Util.gettext("The initial conditions are not fully specified. %s."));
819821
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),

0 commit comments

Comments
 (0)