Skip to content

Commit

Permalink
[NF] Fix variability of size() properly.
Browse files Browse the repository at this point in the history
- Partially revert previous commit that propagated origin to
  Ceval.evalExp, and deduce the origin based on where a component is
  declared instead. Package constants should be typed like they're
  class variables, even when used in a function.

Belonging to [master]:
  - OpenModelica/OMCompiler#2425
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 9, 2018
1 parent 63ed252 commit 0e2c8f5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 81 deletions.
6 changes: 3 additions & 3 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -767,7 +767,7 @@ uniontype Call
algorithm
ptree := buildParameterTree(fnArgs, ptree);
exp := Expression.map(dim.exp, function evaluateCallTypeDimExp(ptree = ptree));
exp := Ceval.evalExp(exp, ExpOrigin.FUNCTION, Ceval.EvalTarget.IGNORE_ERRORS());
exp := Ceval.evalExp(exp, Ceval.EvalTarget.IGNORE_ERRORS());
then
Dimension.fromExp(exp, dim.var);

Expand Down Expand Up @@ -1940,7 +1940,7 @@ protected
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info);

if arg_var <= Variability.STRUCTURAL_PARAMETER then
arg := Ceval.evalExp(arg, origin);
arg := Ceval.evalExp(arg);
arg_ty := Expression.typeOf(arg);
end if;

Expand Down Expand Up @@ -2203,7 +2203,7 @@ protected
if variability > Variability.PARAMETER then
Error.addSourceMessageAndFail(Error.NF_CAT_FIRST_ARG_EVAL, {Expression.toString(arg), Prefixes.variabilityString(variability)}, info);
end if;
Expression.INTEGER(n) := Ceval.evalExp(arg, origin, Ceval.EvalTarget.GENERIC(info));
Expression.INTEGER(n) := Ceval.evalExp(arg, Ceval.EvalTarget.GENERIC(info));

res := {};
tys := {};
Expand Down
90 changes: 42 additions & 48 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -54,6 +54,7 @@ import List;
import System;
import ExpressionIterator = NFExpressionIterator;
import MetaModelica.Dangerous.*;
import NFClass.Class;

public
uniontype EvalTarget
Expand Down Expand Up @@ -120,7 +121,6 @@ end EvalTarget;

function evalExp
input output Expression exp;
input ExpOrigin.Type origin;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
algorithm
exp := match exp
Expand All @@ -134,13 +134,16 @@ algorithm
Option<Expression> oexp;
ComponentRef cref;
Dimension dim;
ExpOrigin.Type exp_origin;

case Expression.CREF(cref = cref as ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE(),
origin = NFComponentRef.Origin.CREF))
algorithm
Typing.typeComponentBinding(c, origin);
exp_origin := if Class.isFunction(InstNode.getClass(InstNode.parent(c)))
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;
Typing.typeComponentBinding(c, exp_origin);
binding := Component.getBinding(InstNode.component(c));
exp1 := evalBinding(binding, exp, origin, target);
exp1 := evalBinding(binding, exp, target);
then
Expression.applySubscripts(cref.subscripts, exp1);

Expand All @@ -149,31 +152,31 @@ algorithm

case Expression.ARRAY()
algorithm
exp.elements := list(evalExp(e, origin, target) for e in exp.elements);
exp.elements := list(evalExp(e, target) for e in exp.elements);
then
exp;

case Expression.RANGE()
algorithm
exp1 := evalExp(exp.start, origin, target);
oexp := evalExpOpt(exp.step, origin, target);
exp3 := evalExp(exp.stop, origin, target);
exp1 := evalExp(exp.start, target);
oexp := evalExpOpt(exp.step, target);
exp3 := evalExp(exp.stop, target);
then
if EvalTarget.isRange(target) then
Expression.RANGE(exp.ty, exp1, oexp, exp3) else evalRange(exp1, oexp, exp3);

case Expression.RECORD()
algorithm
exp.elements := list(evalExp(e, origin, target) for e in exp.elements);
exp.elements := list(evalExp(e, target) for e in exp.elements);
then
exp;

case Expression.CALL()
then evalCall(exp.call, origin, target);
then evalCall(exp.call, target);

case Expression.SIZE(dimIndex = SOME(exp1))
algorithm
dim := listGet(Type.arrayDims(Expression.typeOf(exp.exp)), Expression.toInteger(evalExp(exp1, origin, target)));
dim := listGet(Type.arrayDims(Expression.typeOf(exp.exp)), Expression.toInteger(evalExp(exp1, target)));
then
if Dimension.isKnown(dim) then Expression.INTEGER(Dimension.size(dim)) else exp;

Expand All @@ -186,58 +189,58 @@ algorithm

case Expression.BINARY()
algorithm
exp1 := evalExp(exp.exp1, origin, target);
exp2 := evalExp(exp.exp2, origin, target);
exp1 := evalExp(exp.exp1, target);
exp2 := evalExp(exp.exp2, target);
then
evalBinaryOp(exp1, exp.operator, exp2);

case Expression.UNARY()
algorithm
exp1 := evalExp(exp.exp, origin, target);
exp1 := evalExp(exp.exp, target);
then
evalUnaryOp(exp1, exp.operator);

case Expression.LBINARY()
then evalLogicBinaryOp(exp.exp1, exp.operator, exp.exp2, origin, target);
then evalLogicBinaryOp(exp.exp1, exp.operator, exp.exp2, target);

case Expression.LUNARY()
algorithm
exp1 := evalExp(exp.exp, origin, target);
exp1 := evalExp(exp.exp, target);
then
evalLogicUnaryOp(exp1, exp.operator);

case Expression.RELATION()
algorithm
exp1 := evalExp(exp.exp1, origin, target);
exp2 := evalExp(exp.exp2, origin, target);
exp1 := evalExp(exp.exp1, target);
exp2 := evalExp(exp.exp2, target);
then
evalRelationOp(exp1, exp.operator, exp2);

case Expression.IF() then evalIfExp(exp.condition, exp.trueBranch, exp.falseBranch, origin, target);
case Expression.IF() then evalIfExp(exp.condition, exp.trueBranch, exp.falseBranch, target);

case Expression.CAST()
algorithm
exp1 := evalExp(exp.exp, origin, target);
exp1 := evalExp(exp.exp, target);
then
evalCast(exp1, exp.ty);

case Expression.UNBOX()
algorithm
exp1 := evalExp(exp.exp, origin, target);
exp1 := evalExp(exp.exp, target);
then Expression.UNBOX(exp1, exp.ty);

case Expression.MUTABLE()
algorithm
exp1 := evalExp(Mutable.access(exp.exp), origin, target);
exp1 := evalExp(Mutable.access(exp.exp), target);
then
exp1;

case Expression.SUBSCRIPTED_EXP()
algorithm
exp1 := evalExp(exp.exp, origin, target);
exp1 := evalExp(exp.exp, target);

for s in exp.subscripts loop
exp2 := evalExp(s, origin, target);
exp2 := evalExp(s, target);
exp1 := Expression.applyIndexSubscript(exp2, exp1);
end for;
then
Expand All @@ -249,27 +252,25 @@ end evalExp;

function evalExpOpt
input output Option<Expression> oexp;
input ExpOrigin.Type origin;
input EvalTarget target;
algorithm
oexp := match oexp
local
Expression e;

case SOME(e) then SOME(evalExp(e, origin, target));
case SOME(e) then SOME(evalExp(e, target));
else oexp;
end match;
end evalExpOpt;

function evalBinding
input Binding binding;
input Expression originExp "The expression the binding came from, e.g. a cref.";
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
algorithm
exp := match binding
case Binding.TYPED_BINDING() then evalExp(binding.bindingExp, origin, target);
case Binding.TYPED_BINDING() then evalExp(binding.bindingExp, target);
case Binding.UNBOUND()
algorithm
printUnboundError(target, originExp);
Expand Down Expand Up @@ -818,13 +819,12 @@ function evalLogicBinaryOp
input Expression exp1;
input Operator op;
input Expression exp2;
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
algorithm
exp := match op.op
case Op.AND then evalLogicBinaryAnd(exp1, exp2, origin, target);
case Op.OR then evalLogicBinaryOr(exp1, exp2, origin, target);
case Op.AND then evalLogicBinaryAnd(exp1, exp2, target);
case Op.OR then evalLogicBinaryOr(exp1, exp2, target);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expand All @@ -837,17 +837,16 @@ end evalLogicBinaryOp;
function evalLogicBinaryAnd
input Expression exp1;
input Expression exp2;
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
protected
Expression e;
algorithm
e := evalExp(exp1, origin, target);
e := evalExp(exp1, target);

exp := match e
case Expression.BOOLEAN()
then if e.value then evalExp(exp2, origin, target) else e;
then if e.value then evalExp(exp2, target) else e;

else
algorithm
Expand All @@ -861,17 +860,16 @@ end evalLogicBinaryAnd;
function evalLogicBinaryOr
input Expression exp1;
input Expression exp2;
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
protected
Expression e;
algorithm
e := evalExp(exp1, origin, target);
e := evalExp(exp1, target);

exp := match e
case Expression.BOOLEAN()
then if e.value then e else evalExp(exp2, origin, target);
then if e.value then e else evalExp(exp2, target);

else
algorithm
Expand Down Expand Up @@ -1099,17 +1097,16 @@ function evalIfExp
input Expression condition;
input Expression trueBranch;
input Expression falseBranch;
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
protected
Expression cond;
algorithm
cond := evalExp(condition, origin, target);
cond := evalExp(condition, target);

exp := match cond
case Expression.BOOLEAN()
then evalExp(if cond.value then trueBranch else falseBranch, origin, target);
then evalExp(if cond.value then trueBranch else falseBranch, target);

else
algorithm
Expand Down Expand Up @@ -1143,7 +1140,6 @@ end evalCast;

function evalCall
input Call call;
input ExpOrigin.Type origin;
input EvalTarget target;
output Expression exp;
algorithm
Expand All @@ -1153,15 +1149,15 @@ algorithm

case Call.TYPED_CALL()
algorithm
args := list(evalExp(arg, origin, target) for arg in call.arguments);
args := list(evalExp(arg, target) for arg in call.arguments);
then
if Function.isBuiltin(call.fn) then
evalBuiltinCall(call.fn, args, target)
else
evalNormalCall(call.fn, args, call);

case Call.TYPED_MAP_CALL()
then evalReduction(call.exp, call.ty, call.iters, origin);
then evalReduction(call.exp, call.ty, call.iters);

else
algorithm
Expand Down Expand Up @@ -2127,7 +2123,6 @@ function evalReduction
input Expression exp;
input Type ty;
input list<tuple<InstNode, Expression>> iterators;
input ExpOrigin.Type origin;
output Expression result;
protected
Expression e = exp, range;
Expand All @@ -2141,18 +2136,17 @@ algorithm
iter := Mutable.create(Expression.INTEGER(0));
e := Expression.replaceIterator(e, node, Expression.MUTABLE(iter));
iters := iter :: iters;
ranges := evalExp(range, origin) :: ranges;
ranges := evalExp(range) :: ranges;
end for;

result := evalReduction2(e, ty, ranges, iters, origin);
result := evalReduction2(e, ty, ranges, iters);
end evalReduction;

function evalReduction2
input Expression exp;
input Type ty;
input list<Expression> ranges;
input list<Mutable<Expression>> iterators;
input ExpOrigin.Type origin;
output Expression result;
protected
Expression range;
Expand All @@ -2164,7 +2158,7 @@ protected
Type el_ty;
algorithm
if listEmpty(ranges) then
result := evalExp(exp, origin);
result := evalExp(exp);
else
range :: ranges_rest := ranges;
iter :: iters_rest := iterators;
Expand All @@ -2174,7 +2168,7 @@ algorithm
while ExpressionIterator.hasNext(range_iter) loop
(range_iter, value) := ExpressionIterator.next(range_iter);
Mutable.update(iter, value);
expl := evalReduction2(exp, el_ty, ranges_rest, iters_rest, origin) :: expl;
expl := evalReduction2(exp, el_ty, ranges_rest, iters_rest) :: expl;
end while;

result := Expression.ARRAY(ty, listReverseInPlace(expl));
Expand Down

0 comments on commit 0e2c8f5

Please sign in to comment.