From c399c46cca5e460eec9a50fa372246dd624b03d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Wed, 19 Jun 2024 14:42:19 +0200 Subject: [PATCH] Improve Ceval.EvalTarget (#12617) - 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. --- .../Compiler/NFFrontEnd/NFArrayConnections.mo | 2 +- .../Compiler/NFFrontEnd/NFBuiltinCall.mo | 4 +- OMCompiler/Compiler/NFFrontEnd/NFCall.mo | 4 +- OMCompiler/Compiler/NFFrontEnd/NFCeval.mo | 140 ++++++------------ OMCompiler/Compiler/NFFrontEnd/NFDimension.mo | 2 +- .../Compiler/NFFrontEnd/NFEvalConstants.mo | 6 +- .../Compiler/NFFrontEnd/NFEvalFunction.mo | 30 ++-- OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo | 10 +- .../NFFrontEnd/NFFunctionDerivative.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFPackage.mo | 3 +- .../Compiler/NFFrontEnd/NFSBGraphUtil.mo | 4 +- .../Compiler/NFFrontEnd/NFSimplifyExp.mo | 4 +- OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFTyping.mo | 26 ++-- OMCompiler/Compiler/Script/NFApi.mo | 2 +- 16 files changed, 99 insertions(+), 144 deletions(-) diff --git a/OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo b/OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo index f29049b2e37..d16cce539fb 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo @@ -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 diff --git a/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo b/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo index 579d245643c..e31a36fd9d9 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo @@ -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 @@ -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 := {}; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFCall.mo b/OMCompiler/Compiler/NFFrontEnd/NFCall.mo index 21ef26e679c..7375a0a8e52 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFCall.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFCall.mo @@ -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; @@ -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()); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo b/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo index 95318707a11..88b7f4e37dd 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo @@ -49,7 +49,6 @@ import NFClassTree.ClassTree; import ComplexType = NFComplexType; import Subscript = NFSubscript; import NFTyping.TypingError; -import DAE; import Record = NFRecord; import InstContext = NFInstContext; @@ -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; @@ -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 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 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 @@ -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 @@ -300,7 +239,7 @@ end evalExp; function evalExpOpt input output Option oexp; - input EvalTarget target = EvalTarget.IGNORE_ERRORS(); + input EvalTarget target = noTarget; algorithm oexp := match oexp local @@ -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."; @@ -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 @@ -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); @@ -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), @@ -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 @@ -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), @@ -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); @@ -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 @@ -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); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFDimension.mo b/OMCompiler/Compiler/NFFrontEnd/NFDimension.mo index 4a848ccbc50..4a3d7af7d0d 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFDimension.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFDimension.mo @@ -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 diff --git a/OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo b/OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo index b33ecaf80ea..c35d45ae3d5 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo @@ -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; @@ -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 @@ -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; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo b/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo index de2df8f8f02..c034a5ba330 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo @@ -60,6 +60,7 @@ import EvalFunctionExt = NFEvalFunctionExt; import FFI; import Flags; import Global; +import InstContext = NFInstContext; import MetaModelica.Dangerous.*; import NFPrefixes.Variability; import RangeIterator = NFRangeIterator; @@ -73,6 +74,9 @@ import UnorderedMap; type FlowControl = enumeration(NEXT, CONTINUE, BREAK, RETURN, ASSERTION); type ArgumentMap = UnorderedMap; +constant InstContext.Type STATEMENT_CONTEXT = intBitOr(NFInstContext.FUNCTION, NFInstContext.ALGORITHM); +constant InstContext.Type IF_COND_CONTEXT = intBitOr(STATEMENT_CONTEXT, intBitOr(NFInstContext.IF, NFInstContext.CONDITION)); + public function evaluate input Function fn; @@ -167,14 +171,14 @@ algorithm result := matchcontinue lang case "builtin" // Functions defined as 'external "builtin"', delegate to Ceval. - then Ceval.evalBuiltinCall(fn, args, EvalTarget.IGNORE_ERRORS()); + then Ceval.evalBuiltinCall(fn, args, NFCeval.noTarget); case "FORTRAN 77" // This had better be a Lapack function. then evaluateExternal2(name, fn, args, ext_args); case _ - guard not EvalTarget.isInstanceAPI(target) + guard not InstContext.inInstanceAPI(target.context) // For anything else, try to call the function via FFI. then callExternalFunction(name, fn, args, ext_args, output_ref, ann); @@ -752,7 +756,8 @@ function evaluateAssignment input DAE.ElementSource source; output FlowControl ctrl = FlowControl.NEXT; algorithm - assignVariable(lhsExp, Ceval.evalExp(rhsExp, EvalTarget.STATEMENT(source))); + assignVariable(lhsExp, + Ceval.evalExp(rhsExp, EvalTarget.new(ElementSource.getInfo(source), STATEMENT_CONTEXT))); end evaluateAssignment; public @@ -965,7 +970,8 @@ protected list body = forBody; Integer i = 0, limit = Flags.getConfigInt(Flags.EVAL_LOOP_LIMIT); algorithm - range_exp := Ceval.evalExp(Util.getOption(range), EvalTarget.STATEMENT(source)); + range_exp := Ceval.evalExp(Util.getOption(range), + EvalTarget.new(ElementSource.getInfo(source), STATEMENT_CONTEXT)); range_iter := RangeIterator.fromExp(range_exp); if RangeIterator.hasNext(range_iter) then @@ -1007,7 +1013,7 @@ algorithm for branch in branches loop (cond, body) := branch; - if Expression.isTrue(Ceval.evalExp(cond, EvalTarget.STATEMENT(source))) then + if Expression.isTrue(Ceval.evalExp(cond, EvalTarget.new(ElementSource.getInfo(source), IF_COND_CONTEXT))) then ctrl := evaluateStatements(body); return; end if; @@ -1022,24 +1028,24 @@ function evaluateAssert output FlowControl ctrl = FlowControl.NEXT; protected Expression cond, msg, lvl; - DAE.ElementSource source; - EvalTarget target = EvalTarget.STATEMENT(Statement.source(assertStmt)); + SourceInfo info = ElementSource.getInfo(Statement.source(assertStmt)); + EvalTarget target = EvalTarget.new(info, STATEMENT_CONTEXT); algorithm if Expression.isFalse(Ceval.evalExp(condition, target)) then - Statement.ASSERT(message = msg, level = lvl, source = source) := assertStmt; + Statement.ASSERT(message = msg, level = lvl) := assertStmt; msg := Ceval.evalExp(msg, target); lvl := Ceval.evalExp(lvl, target); () := match (msg, lvl) case (Expression.STRING(), Expression.ENUM_LITERAL(name = "warning")) algorithm - Error.addSourceMessage(Error.ASSERT_TRIGGERED_WARNING, {msg.value}, ElementSource.getInfo(source)); + Error.addSourceMessage(Error.ASSERT_TRIGGERED_WARNING, {msg.value}, info); then (); case (Expression.STRING(), Expression.ENUM_LITERAL(name = "error")) algorithm - Error.addSourceMessage(Error.ASSERT_TRIGGERED_ERROR, {msg.value}, ElementSource.getInfo(source)); + Error.addSourceMessage(Error.ASSERT_TRIGGERED_ERROR, {msg.value}, info); ctrl := FlowControl.ASSERTION; then (); @@ -1059,7 +1065,7 @@ function evaluateNoRetCall input DAE.ElementSource source; output FlowControl ctrl = FlowControl.NEXT; algorithm - Ceval.evalExp(callExp, EvalTarget.STATEMENT(source)); + Ceval.evalExp(callExp, EvalTarget.new(ElementSource.getInfo(source), STATEMENT_CONTEXT)); end evaluateNoRetCall; function evaluateWhile @@ -1069,7 +1075,7 @@ function evaluateWhile output FlowControl ctrl = FlowControl.NEXT; protected Integer i = 0, limit = Flags.getConfigInt(Flags.EVAL_LOOP_LIMIT); - EvalTarget target = EvalTarget.STATEMENT(source); + EvalTarget target = EvalTarget.new(ElementSource.getInfo(source), STATEMENT_CONTEXT); algorithm while Expression.isTrue(Ceval.evalExp(condition, target)) loop ctrl := evaluateStatements(body); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo b/OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo index 942e1af125f..f200feff81e 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo @@ -343,7 +343,7 @@ public // This relies on the fact that Ceval.evalBuiltinCat doesn't actually do any // actual constant evaluation, and works on non-constant arrays too as long // as they're expanded. - exp := Ceval.evalBuiltinCat(listHead(args), expl, Ceval.EvalTarget.IGNORE_ERRORS()); + exp := Ceval.evalBuiltinCat(listHead(args), expl, NFCeval.noTarget); else exp := expandGeneric(Expression.CALL(call)); end if; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo b/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo index 5f1367f2550..c8d0e44998b 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo @@ -598,7 +598,7 @@ algorithm if Binding.isBound(condition) then cond := flattenBinding(condition, prefix); exp := Binding.getTypedExp(cond); - exp := Ceval.evalExp(exp, Ceval.EvalTarget.CONDITION(Binding.getInfo(cond))); + exp := Ceval.evalExp(exp, Ceval.EvalTarget.new(Binding.getInfo(cond), NFInstContext.CONDITION)); exp := Expression.expandSplitIndices(exp); // Hack to make arrays work when all elements have the same value. @@ -1893,9 +1893,7 @@ algorithm // Print errors for unbound constants/parameters if the if-equation contains // connects, since we must select a branch in that case. - target := if has_connect then - Ceval.EvalTarget.GENERIC(info) else - Ceval.EvalTarget.IGNORE_ERRORS(); + target := if has_connect then Ceval.EvalTarget.new(info) else NFCeval.noTarget; while not listEmpty(branches) loop branch :: branches := branches; @@ -2009,7 +2007,7 @@ algorithm // Unroll the loop by replacing the iterator with each of its values in the for loop body. range := flattenExp(range, prefix, info); - range := Ceval.evalExp(range, Ceval.EvalTarget.RANGE(info)); + range := Ceval.evalExp(range, Ceval.EvalTarget.new(info, NFInstContext.ITERATION_RANGE)); range_iter := RangeIterator.fromExp(range); while RangeIterator.hasNext(range_iter) loop @@ -2038,7 +2036,7 @@ algorithm (connects, non_connects) := splitForLoop2(body); if not listEmpty(connects) then - range := Ceval.evalExpOpt(range, Ceval.EvalTarget.RANGE(Equation.info(forLoop))); + range := Ceval.evalExpOpt(range, Ceval.EvalTarget.new(Equation.info(forLoop), NFInstContext.ITERATION_RANGE)); eq := Equation.FOR(iter, range, connects, scope, src); if settings.arrayConnect then diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo b/OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo index a8e0926874a..e4cb5918a0e 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo @@ -109,7 +109,7 @@ public fail(); end if; - order := Ceval.evalExp(order, EvalTarget.GENERIC(info)); + order := Ceval.evalExp(order, EvalTarget.new(info)); end typeDerivative; function toDAE diff --git a/OMCompiler/Compiler/NFFrontEnd/NFPackage.mo b/OMCompiler/Compiler/NFFrontEnd/NFPackage.mo index 78164fb95cb..0e5b0c3953c 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFPackage.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFPackage.mo @@ -293,8 +293,7 @@ public algorithm exp := match exp case Expression.CREF(cref = cref as ComponentRef.CREF()) - then if ComponentRef.isPackageConstant(cref) then - Ceval.evalExp(exp, Ceval.EvalTarget.IGNORE_ERRORS()) else exp; + then if ComponentRef.isPackageConstant(cref) then Ceval.evalExp(exp) else exp; else exp; end match; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFSBGraphUtil.mo b/OMCompiler/Compiler/NFFrontEnd/NFSBGraphUtil.mo index 83f4aa23efa..b5a15381c66 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFSBGraphUtil.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFSBGraphUtil.mo @@ -169,7 +169,7 @@ public output Expression outExp; algorithm if Expression.isCref(e) then - outExp := Ceval.evalExp(e, Ceval.EvalTarget.RANGE(AbsynUtil.dummyInfo)); + outExp := Ceval.evalExp(e, Ceval.EvalTarget.new(AbsynUtil.dummyInfo, NFInstContext.ITERATION_RANGE)); else outExp := e; end if; @@ -364,4 +364,4 @@ public end linearMapFromIntervals; annotation(__OpenModelica_Interface="frontend"); -end NFSBGraphUtil; \ No newline at end of file +end NFSBGraphUtil; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo b/OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo index c97d0ebf033..44d77240130 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo @@ -188,7 +188,7 @@ algorithm if is_pure and List.all(args, Expression.isLiteral) and (scalarize or Type.isScalar(call.ty)) then try - callExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS()); + callExp := Ceval.evalCall(call, NFCeval.noTarget); else callExp := Expression.CALL(call); end try; @@ -223,7 +223,7 @@ algorithm ErrorExt.setCheckpoint(getInstanceName()); try - outExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS()); + outExp := Ceval.evalCall(call, NFCeval.noTarget); ErrorExt.delCheckpoint(getInstanceName()); else if Flags.isSet(Flags.FAILTRACE) then diff --git a/OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo b/OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo index 16b192587f7..55e58f5bc49 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo @@ -868,7 +868,7 @@ public function eval input Subscript subscript; - input EvalTarget target = EvalTarget.IGNORE_ERRORS(); + input EvalTarget target = NFCeval.noTarget; output Subscript outSubscript; algorithm outSubscript := match subscript diff --git a/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo b/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo index dc095328dd1..88aac94f5d3 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo @@ -606,6 +606,7 @@ algorithm TypingError ty_err; Integer parent_dims, dim_index; Boolean evaluated; + Ceval.EvalTarget target; // A dimension that we're already trying to type. case Dimension.UNTYPED(isProcessing = true) @@ -638,7 +639,9 @@ algorithm exp := Ceval.tryEvalExp(exp); evaluated := Expression.isLiteral(exp); else - exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info)); + target := Ceval.EvalTarget.new(info, context, + SOME(Ceval.EvalTargetData.DIMENSION_DATA(component, index, exp))); + exp := Ceval.evalExp(exp, target); end if; else Error.addSourceMessage(Error.DIMENSION_NOT_KNOWN, {Expression.toString(exp)}, info); @@ -739,7 +742,9 @@ algorithm if InstContext.inRelaxed(context) then exp := Ceval.tryEvalExp(exp); else - exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info)); + target := Ceval.EvalTarget.new(info, context, + SOME(Ceval.EvalTargetData.DIMENSION_DATA(component, index, exp))); + exp := Ceval.evalExp(exp, target); end if; exp := subscriptDimExp(exp, component); @@ -804,7 +809,8 @@ algorithm if InstContext.inRelaxed(context) then e := Ceval.tryEvalExp(e); else - e := Ceval.evalExp(e, Ceval.EvalTarget.DIMENSION(component, index, e, info)); + e := Ceval.evalExp(e, Ceval.EvalTarget.new(info, context, + SOME(Ceval.EvalTargetData.DIMENSION_DATA(component, index, e)))); end if; (dim, error) := nthDimensionBoundsChecked(Expression.typeOf(e), dim_index); @@ -1198,11 +1204,13 @@ algorithm SourceInfo info; MatchKind mk; NFBinding.EvalState eval_state; + InstContext.Type next_context; case Binding.UNTYPED_BINDING(bindingExp = exp) algorithm + next_context := InstContext.set(context, NFInstContext.CONDITION); info := Binding.getInfo(condition); - (exp, ty, var, purity) := typeExp(exp, InstContext.set(context, NFInstContext.CONDITION), info); + (exp, ty, var, purity) := typeExp(exp, next_context, info); (exp, _, mk) := TypeCheck.matchTypes(ty, Type.BOOLEAN(), exp); if TypeCheck.isIncompatibleMatch(mk) then @@ -1222,7 +1230,7 @@ algorithm if evaluate then ErrorExt.setCheckpoint(getInstanceName()); try - exp := Ceval.evalExp(exp, Ceval.EvalTarget.CONDITION(info)); + exp := Ceval.evalExp(exp, Ceval.EvalTarget.new(info, next_context)); exp := simplifyDimExp(exp); eval_state := NFBinding.EvalState.EVALUATED; else @@ -1712,7 +1720,7 @@ algorithm if Type.isConditionalArray(ty) then e := Expression.map(e, - function evaluateArrayIf(target = Ceval.EvalTarget.GENERIC(info))); + function evaluateArrayIf(target = Ceval.EvalTarget.new(info, context))); (e, ty, _) := typeExp(e, context, info); end if; @@ -2505,7 +2513,7 @@ algorithm if variability <= Variability.STRUCTURAL_PARAMETER and purity == Purity.PURE then // Evaluate the index if it's a constant. - index := Ceval.evalExp(index, Ceval.EvalTarget.IGNORE_ERRORS()); + index := Ceval.evalExp(index, NFCeval.noTarget); // TODO: Print an error if the index couldn't be evaluated to an int. Expression.INTEGER(iindex) := index; @@ -2679,7 +2687,7 @@ function evaluateCondition protected Expression cond_exp; algorithm - cond_exp := Ceval.evalExp(condExp, Ceval.EvalTarget.GENERIC(info)); + cond_exp := Ceval.evalExp(condExp, Ceval.EvalTarget.new(info, context)); if Expression.arrayAllEqual(cond_exp) then cond_exp := Expression.arrayFirstScalar(cond_exp); @@ -2861,7 +2869,7 @@ algorithm algorithm // The only other kind of expression that's allowed is scalar constants. if Type.isScalarBuiltin(ty) and var == Variability.CONSTANT then - outArg := Ceval.evalExp(outArg, Ceval.EvalTarget.GENERIC(info)); + outArg := Ceval.evalExp(outArg, Ceval.EvalTarget.new(info, NFInstContext.FUNCTION)); else Error.addSourceMessage(Error.EXTERNAL_ARG_WRONG_EXP, {Expression.toString(outArg)}, info); diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index 7bcf5dc0d48..00aa0a38f40 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -1521,7 +1521,7 @@ algorithm if evaluate and not Expression.isLiteral(exp) then ErrorExt.setCheckpoint(getInstanceName()); try - exp := Ceval.evalExp(exp, NFCeval.EvalTarget.INSTANCE_API()); + exp := Ceval.evalExp(exp, Ceval.EvalTarget.new(AbsynUtil.dummyInfo, NFInstContext.INSTANCE_API)); json := JSON.addPair("value", Expression.toJSON(exp), json); else end try;