Skip to content

Commit

Permalink
Improve Ceval.EvalTarget
Browse files Browse the repository at this point in the history
- Simplify EvalTarget by changing it from multiple records that indicate
  the context to a single record with an InstContext field like the rest
  of the frontend uses, to make it possible to better determine the
  context of the expression being evaluated.
  • Loading branch information
perost committed Jun 19, 2024
1 parent 261d3c8 commit 14b6f50
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 144 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected

case Equation.FOR(range = SOME(range))
algorithm
range := Ceval.evalExp(range, Ceval.EvalTarget.RANGE(Equation.info(eq)));
range := Ceval.evalExp(range, Ceval.EvalTarget.new(Equation.info(eq), NFInstContext.ITERATION_RANGE));
body := Equation.replaceIteratorList(eq.body, eq.iterator, range);
addConnectionsToGraph(body, graph, vCount, eCount, nmvTable);
then
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ protected
Expression.toString(n_arg), Prefixes.variabilityString(n_var)}, info);
end if;

n_arg := Ceval.evalExp(n_arg, Ceval.EvalTarget.GENERIC(info));
n_arg := Ceval.evalExp(n_arg, Ceval.EvalTarget.new(info, context));
n := Expression.integerValue(n_arg);

if n < Type.dimensionCount(exp_ty) then
Expand Down Expand Up @@ -1218,7 +1218,7 @@ protected
if variability > Variability.PARAMETER or purity <> Purity.PURE then
Error.addSourceMessageAndFail(Error.NF_CAT_FIRST_ARG_EVAL, {Expression.toString(arg), Prefixes.variabilityString(variability)}, info);
end if;
Expression.INTEGER(n) := Ceval.evalExp(arg, Ceval.EvalTarget.GENERIC(info));
Expression.INTEGER(n) := Ceval.evalExp(arg, Ceval.EvalTarget.new(info, context));

res := {};
tys := {};
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,7 @@ protected
if InstContext.inRelaxed(context) then
range := Ceval.tryEvalExp(range);
else
range := Ceval.evalExp(range, Ceval.EvalTarget.RANGE(info));
range := Ceval.evalExp(range, Ceval.EvalTarget.new(info, NFInstContext.ITERATION_RANGE));
end if;
iter_ty := Expression.typeOf(range);
end if;
Expand Down Expand Up @@ -2842,7 +2842,7 @@ protected

ErrorExt.setCheckpoint(getInstanceName());
try
exp := Ceval.evalExp(exp, Ceval.EvalTarget.IGNORE_ERRORS());
exp := Ceval.evalExp(exp);
else
end try;
ErrorExt.rollBack(getInstanceName());
Expand Down
140 changes: 42 additions & 98 deletions OMCompiler/Compiler/NFFrontEnd/NFCeval.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import NFClassTree.ClassTree;
import ComplexType = NFComplexType;
import Subscript = NFSubscript;
import NFTyping.TypingError;
import DAE;
import Record = NFRecord;
import InstContext = NFInstContext;

Expand All @@ -63,7 +62,6 @@ import MetaModelica.Dangerous.*;
import Class = NFClass;
import TypeCheck = NFTypeCheck;
import ExpandExp = NFExpandExp;
import ElementSource;
import Prefixes = NFPrefixes;
import UnorderedMap;
import ErrorExt;
Expand All @@ -72,99 +70,40 @@ import Vector;

public
uniontype EvalTarget
record DIMENSION
InstNode component;
Integer index;
Expression exp;
SourceInfo info;
end DIMENSION;

record ATTRIBUTE
Binding binding;
end ATTRIBUTE;

record RANGE
SourceInfo info;
end RANGE;

record CONDITION
record EVAL_TARGET
SourceInfo info;
end CONDITION;

record GENERIC
SourceInfo info;
end GENERIC;

record STATEMENT
DAE.ElementSource source;
end STATEMENT;

record INSTANCE_API
end INSTANCE_API;

record IGNORE_ERRORS end IGNORE_ERRORS;

function isRange
input EvalTarget target;
output Boolean isRange;
algorithm
isRange := match target
case RANGE() then true;
else false;
end match;
end isRange;
InstContext.Type context;
Option<EvalTargetData> extra;
end EVAL_TARGET;

function isDimension
input EvalTarget target;
output Boolean isDim;
algorithm
isDim := match target
case DIMENSION() then true;
else false;
end match;
end isDimension;

function isInstanceAPI
input EvalTarget target;
output Boolean api;
algorithm
api := match target
case INSTANCE_API() then true;
else false;
end match;
end isInstanceAPI;
function new
input SourceInfo info;
input InstContext.Type context = NFInstContext.NO_CONTEXT;
input Option<EvalTargetData> extra = NONE();
output EvalTarget target = EVAL_TARGET(info, context, extra);
end new;

function hasInfo
input EvalTarget target;
output Boolean hasInfo;
algorithm
hasInfo := match target
case DIMENSION() then true;
case ATTRIBUTE() then true;
case RANGE() then true;
case CONDITION() then true;
case GENERIC() then true;
case STATEMENT() then true;
else false;
end match;
output Boolean res = not stringEmpty(target.info.fileName);
end hasInfo;

function getInfo
input EvalTarget target;
output SourceInfo info;
algorithm
info := match target
case DIMENSION() then target.info;
case ATTRIBUTE() then Binding.getInfo(target.binding);
case RANGE() then target.info;
case CONDITION() then target.info;
case GENERIC() then target.info;
case STATEMENT() then ElementSource.getInfo(target.source);
else AbsynUtil.dummyInfo;
end match;
output SourceInfo info = target.info;
end getInfo;
end EvalTarget;

constant EvalTarget noTarget = EvalTarget.EVAL_TARGET(AbsynUtil.dummyInfo, NFInstContext.NO_CONTEXT, NONE());

uniontype EvalTargetData
record DIMENSION_DATA
InstNode component;
Integer index;
Expression exp;
end DIMENSION_DATA;
end EvalTargetData;

function tryEvalExp
input output Expression exp;
algorithm
Expand All @@ -180,7 +119,7 @@ end tryEvalExp;

function evalExp
input output Expression exp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
algorithm
exp := match exp
local
Expand Down Expand Up @@ -300,7 +239,7 @@ end evalExp;

function evalExpOpt
input output Option<Expression> oexp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
algorithm
oexp := match oexp
local
Expand All @@ -325,7 +264,7 @@ function evalExpPartial
expressions. This can be used to optimize an expression that is expected to
be evaluated many times, for example the expression in an array constructor."
input Expression exp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
input Boolean evaluated = true;
output Expression outExp;
output Boolean outEvaluated "True if the whole expression is evaluated, otherwise false.";
Expand Down Expand Up @@ -823,7 +762,7 @@ function evalTypename
algorithm
// Only expand the typename into an array if it's used as a range, and keep
// them as typenames when used as e.g. dimensions.
exp := if EvalTarget.isRange(target) then ExpandExp.expandTypename(ty) else originExp;
exp := if InstContext.inIterationRange(target.context) then ExpandExp.expandTypename(ty) else originExp;
end evalTypename;

function evalRange
Expand All @@ -842,7 +781,7 @@ algorithm
step_exp := evalExpOpt(step_exp, target);
stop_exp := evalExp(stop_exp, target);

if EvalTarget.isRange(target) then
if InstContext.inIterationRange(target.context) then
ty := TypeCheck.getRangeType(start_exp, step_exp, stop_exp,
Type.arrayElementType(ty), EvalTarget.getInfo(target));
result := Expression.RANGE(ty, start_exp, step_exp, stop_exp);
Expand Down Expand Up @@ -963,7 +902,7 @@ function evalBinaryOp
input Expression exp1;
input Operator op;
input Expression exp2;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
output Expression exp;
algorithm
exp := Expression.mapSplitExpressions(Expression.BINARY(exp1, op, exp2),
Expand All @@ -986,7 +925,7 @@ function evalBinaryOp_dispatch
input Expression exp1;
input Operator op;
input Expression exp2;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
output Expression exp;
algorithm
exp := match op.op
Expand Down Expand Up @@ -1464,7 +1403,7 @@ function evalLogicBinaryOp
input Expression exp1;
input Operator op;
input Expression exp2;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = noTarget;
output Expression exp;
algorithm
exp := Expression.mapSplitExpressions(Expression.LBINARY(exp1, op, exp2),
Expand Down Expand Up @@ -3177,7 +3116,7 @@ protected
Type ty;
algorithm
if listEmpty(ranges) then
result := evalExp(exp, EvalTarget.IGNORE_ERRORS());
result := evalExp(exp);
else
range :: ranges_rest := ranges;
range := evalExp(range);
Expand Down Expand Up @@ -3242,7 +3181,7 @@ algorithm
end match;

result := Expression.foldReduction(exp, iters, default_exp,
function evalExp(target = EvalTarget.IGNORE_ERRORS()), red_fn);
function evalExp(target = noTarget), red_fn);
end evalReduction;

function evalSize
Expand Down Expand Up @@ -3341,18 +3280,23 @@ function printUnboundError
input Component component;
input EvalTarget target;
input Expression exp;
protected
EvalTargetData extra;
algorithm
() := match target
case EvalTarget.IGNORE_ERRORS() then ();
if not EvalTarget.hasInfo(target) then
return;
end if;

case EvalTarget.DIMENSION()
() := match target.extra
case SOME(extra as EvalTargetData.DIMENSION_DATA())
algorithm
Error.addSourceMessage(Error.STRUCTURAL_PARAMETER_OR_CONSTANT_WITH_NO_BINDING,
{Expression.toString(exp), InstNode.name(target.component)}, target.info);
{Expression.toString(extra.exp), InstNode.name(extra.component)}, target.info);
then
fail();

case EvalTarget.CONDITION()
case _
guard InstContext.inCondition(target.context)
algorithm
Error.addSourceMessage(Error.CONDITIONAL_EXP_WITHOUT_VALUE,
{Expression.toString(exp)}, target.info);
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFDimension.mo
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public

function eval
input Dimension dim;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input EvalTarget target = NFCeval.noTarget;
output Dimension outDim;
algorithm
outDim := match dim
Expand Down
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ algorithm
if InstContext.inRelaxed(context) then
eexp := Ceval.tryEvalExp(eexp);
else
eexp := Ceval.evalExp(eexp, Ceval.EvalTarget.ATTRIBUTE(binding));
eexp := Ceval.evalExp(eexp, Ceval.EvalTarget.new(info, context));
end if;
end if;

Expand Down Expand Up @@ -204,7 +204,7 @@ algorithm
if ComponentRef.nodeVariability(cref) <= Variability.STRUCTURAL_PARAMETER and
not Type.isExternalObject(ty) then
// Evaluate all constants and structural parameters.
outExp := Ceval.evalCref(cref, outExp, Ceval.EvalTarget.IGNORE_ERRORS(), evalSubscripts = false);
outExp := Ceval.evalCref(cref, outExp, NFCeval.noTarget, evalSubscripts = false);
outExp := Flatten.flattenExp(outExp, Flatten.Prefix.PREFIX(InstNode.EMPTY_NODE(), cref), info);
outChanged := true;
elseif outChanged then
Expand Down Expand Up @@ -607,7 +607,7 @@ algorithm
if evaluateAll or not isLocalFunctionVariable(e.cref, fnNode) then
ErrorExt.setCheckpoint(getInstanceName());
try
outExp := Ceval.evalCref(e.cref, e, Ceval.EvalTarget.IGNORE_ERRORS(), evalSubscripts = false);
outExp := Ceval.evalCref(e.cref, e, NFCeval.noTarget, evalSubscripts = false);
else
outExp := e;
end try;
Expand Down
Loading

0 comments on commit 14b6f50

Please sign in to comment.