Skip to content

Commit

Permalink
[NF] Add type checking for reductions.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 18, 2019
1 parent bbc0127 commit ea40561
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -1053,6 +1053,7 @@ protected
next_origin := intBitOr(origin, ExpOrigin.FOR);
(arg, ty) := Typing.typeExp(call.exp, next_origin, info);
{fn} := Function.typeRefCache(call.ref);
TypeCheck.checkReductionType(ty, Function.name(fn), call.exp, info);
then
(TYPED_REDUCTION(fn, ty, variability, arg, iters), ty, variability);

Expand Down
57 changes: 57 additions & 0 deletions Compiler/NFFrontEnd/NFTypeCheck.mo
Expand Up @@ -78,6 +78,8 @@ import MetaModelica.Dangerous.*;
import OperatorOverloading = NFOperatorOverloading;
import ExpandExp = NFExpandExp;
import NFFunction.Slot;
import Util;
import System;

public
type MatchKind = enumeration(
Expand Down Expand Up @@ -3015,5 +3017,60 @@ algorithm
end if;
end checkDimensionType;

function checkReductionType
input Type ty;
input Absyn.Path name;
input Expression exp;
input SourceInfo info;
protected
Type ety;
String err;
algorithm
err := match name
case Absyn.Path.IDENT("sum")
then
match Type.arrayElementType(ty)
case Type.INTEGER() then "";
case Type.REAL() then "";
else "Integer or Real";
end match;

case Absyn.Path.IDENT("product")
then
match ty
case Type.INTEGER() then "";
case Type.REAL() then "";
else "scalar Integer or Real";
end match;

case Absyn.Path.IDENT("min")
then
match ty
case Type.INTEGER() then "";
case Type.REAL() then "";
case Type.BOOLEAN() then "";
case Type.ENUMERATION() then "";
else "scalar enumeration, Boolean, Integer or Real";
end match;

case Absyn.Path.IDENT("max")
then
match ty
case Type.INTEGER() then "";
case Type.REAL() then "";
case Type.BOOLEAN() then "";
case Type.ENUMERATION() then "";
else "scalar enumeration, Boolean, Integer or Real";
end match;

else "";
end match;

if not stringEmpty(err) then
Error.addSourceMessageAndFail(Error.INVALID_REDUCTION_TYPE,
{Expression.toString(exp), Type.toString(ty), Absyn.pathString(name), err}, info);
end if;
end checkReductionType;

annotation(__OpenModelica_Interface="frontend");
end NFTypeCheck;
2 changes: 2 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -814,6 +814,8 @@ public constant Message OPERATOR_NOT_ENCAPSULATED = MESSAGE(342, TRANSLATION(),
Util.gettext("Operator %s is not encapsulated."));
public constant Message NO_SUCH_INPUT_PARAMETER = MESSAGE(343, TRANSLATION(), ERROR(),
Util.gettext("Function %s has no input parameter named %s."));
public constant Message INVALID_REDUCTION_TYPE = MESSAGE(344, TRANSLATION(), ERROR(),
Util.gettext("Invalid expression ‘%s‘ of type %s in %s reduction, expected %s."));
public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSLATION(), WARNING(),
Util.gettext("The initial conditions are not fully specified. %s."));
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),
Expand Down

0 comments on commit ea40561

Please sign in to comment.