@@ -940,41 +940,60 @@ function flattenIfEquation
940940 input ComponentRef prefix;
941941 input output list< Equation > equations;
942942protected
943+ Equation . Branch branch;
943944 list< Equation . Branch > branches, bl = {};
944945 Expression cond;
945946 list< Equation > eql;
946947 Variability var ;
947948 Boolean has_connect;
948949 DAE . ElementSource src;
950+ SourceInfo info;
951+ Ceval . EvalTarget target;
949952algorithm
950953 Equation . IF (branches = branches, source = src) := eq;
951954 has_connect := Equation . contains(eq, isConnectEq);
952955
953- for b in branches loop
954- bl := match b
956+ // Print errors for unbound constants/parameters if the if-equation contains
957+ // connects, since we must select a branch in that case.
958+ target := if has_connect then
959+ Ceval . EvalTarget . GENERIC (Equation . info(eq)) else
960+ Ceval . EvalTarget . IGNORE_ERRORS ();
961+
962+ while not listEmpty(branches) loop
963+ branch :: branches := branches;
964+
965+ bl := match branch
955966 case Equation . Branch . BRANCH (cond, var , eql)
956967 algorithm
957968 // Flatten the condition and body of the branch.
958969 cond := flattenExp(cond, prefix);
959970 eql := flattenEquations(eql, prefix);
960971
961- // Force branch selection if the if-equation contains connects .
962- if has_connect then
963- cond := Ceval . evalExp(cond);
972+ // Evaluate structural conditions .
973+ if var <= Variability . STRUCTURAL_PARAMETER then
974+ cond := Ceval . evalExp(cond, target );
964975
965- if not Expression . isBoolean(cond) then
976+ // Conditions in an if-equation that contains connects must be possible to evaluate.
977+ if not Expression . isBoolean(cond) and has_connect then
966978 Error . addInternalError(
967979 "Failed to evaluate branch condition in if equation containing connect equations: `" +
968980 Expression . toString(cond) + "`" , Equation . info(eq));
969981 fail();
970982 end if ;
971983 end if ;
972984
973- if Expression . isTrue(cond) and listEmpty(bl) then
974- // If the condition is literal true and we haven't collected any other
975- // branches yet, replace the if equation with this branch.
976- equations := listAppend(eql, equations);
977- return ;
985+ if Expression . isTrue(cond) then
986+ // The condition is true and the branch will thus always be selected
987+ // if reached, so we can discard the remaining branches.
988+ branches := {};
989+
990+ if listEmpty(bl) then
991+ // If we haven't collected any other branches yet, replace the if-equation with this branch.
992+ equations := listAppend(eql, equations);
993+ else
994+ // Otherwise, append this branch.
995+ bl := Equation . makeBranch(cond, eql, var ) :: bl;
996+ end if ;
978997 elseif not Expression . isFalse(cond) then
979998 // Only add the branch to the list of branches if the condition is not
980999 // literal false, otherwise just drop it since it will never trigger.
@@ -983,25 +1002,27 @@ algorithm
9831002 then
9841003 bl;
9851004
986- // An invalid branch must have a false condition if the if-equation
987- // contains connects, anything else is an error.
988- case Equation . Branch . INVALID_BRANCH (branch = Equation . Branch . BRANCH (condition = cond))
1005+ // An invalid branch must have a false condition, anything else is an error.
1006+ case Equation . Branch . INVALID_BRANCH (branch =
1007+ Equation . Branch . BRANCH (condition = cond, conditionVar = var ))
9891008 guard has_connect
9901009 algorithm
991- cond := Ceval . evalExp(cond);
1010+ if var <= Variability . STRUCTURAL_PARAMETER then
1011+ cond := Ceval . evalExp(cond, target);
1012+ end if ;
9921013
9931014 if not Expression . isFalse(cond) then
994- Equation . Branch . triggerErrors(b );
1015+ Equation . Branch . triggerErrors(branch );
9951016 end if ;
9961017 then
9971018 bl;
9981019
999- else b :: bl;
1020+ else branch :: bl;
10001021 end match;
1001- end for ;
1022+ end while ;
10021023
1003- // Add the flattened if equation to the list of equations if we got this far,
1004- // and there are any branches still remaining.
1024+ // Add the flattened if- equation to the list of equations if there are any
1025+ // branches still remaining.
10051026 if not listEmpty(bl) then
10061027 equations := Equation . IF (listReverseInPlace(bl), src) :: equations;
10071028 end if ;
0 commit comments