@@ -894,7 +894,7 @@ algorithm
894894 Equation . CONNECT (e1, e2, eql, eq. source) :: equations;
895895
896896 case Equation . IF ()
897- then flattenIfEquation(eq. branches , prefix, eq . source , equations);
897+ then flattenIfEquation(eq, prefix, equations);
898898
899899 case Equation . WHEN ()
900900 algorithm
@@ -934,25 +934,40 @@ algorithm
934934end flattenEquation;
935935
936936function flattenIfEquation
937- input list < Equation . Branch > branches ;
937+ input Equation eq ;
938938 input ComponentRef prefix;
939- input DAE . ElementSource source;
940939 input output list< Equation > equations;
941940protected
942- list< Equation . Branch > bl = {};
941+ list< Equation . Branch > branches, bl = {};
943942 Expression cond;
944943 list< Equation > eql;
945944 Variability var ;
945+ Boolean has_connect;
946+ DAE . ElementSource src;
946947algorithm
948+ Equation . IF (branches = branches, source = src) := eq;
949+ has_connect := Equation . contains(eq, isConnectEq);
950+
947951 for b in branches loop
948952 bl := match b
949953 case Equation . Branch . BRANCH (cond, var , eql)
950954 algorithm
951- // flatten the condition first
955+ // Flatten the condition and body of the branch.
952956 cond := flattenExp(cond, prefix);
953- // flatten the equations
954957 eql := flattenEquations(eql, prefix);
955958
959+ // Force branch selection if the if-equation contains connects.
960+ if has_connect then
961+ cond := Ceval . evalExp(cond);
962+
963+ if not Expression . isBoolean(cond) then
964+ Error . addInternalError(
965+ "Failed to evaluate branch condition in if equation containing connect equations: `" +
966+ Expression . toString(cond) + "`" , Equation . info(eq));
967+ fail();
968+ end if ;
969+ end if ;
970+
956971 if Expression . isTrue(cond) and listEmpty(bl) then
957972 // If the condition is literal true and we haven't collected any other
958973 // branches yet, replace the if equation with this branch.
@@ -966,17 +981,45 @@ algorithm
966981 then
967982 bl;
968983
984+ // An invalid branch must have a false condition if the if-equation
985+ // contains connects, anything else is an error.
986+ case Equation . Branch . INVALID_BRANCH (branch = Equation . Branch . BRANCH (condition = cond))
987+ guard has_connect
988+ algorithm
989+ cond := Ceval . evalExp(cond);
990+
991+ if not Expression . isFalse(cond) then
992+ Equation . Branch . triggerErrors(b);
993+ end if ;
994+ then
995+ bl;
996+
969997 else b :: bl;
970998 end match;
971999 end for ;
9721000
9731001 // Add the flattened if equation to the list of equations if we got this far,
9741002 // and there are any branches still remaining.
9751003 if not listEmpty(bl) then
976- equations := Equation . IF (listReverseInPlace(bl), source ) :: equations;
1004+ equations := Equation . IF (listReverseInPlace(bl), src ) :: equations;
9771005 end if ;
9781006end flattenIfEquation;
9791007
1008+ function isConnectEq
1009+ input Equation eq;
1010+ output Boolean isConnect;
1011+ algorithm
1012+ isConnect := match eq
1013+ local
1014+ Function fn;
1015+
1016+ case Equation . CONNECT () then true ;
1017+ case Equation . NORETCALL (exp = Expression . CALL (call = Call . TYPED_CALL (fn = fn)))
1018+ then Absyn . pathFirstIdent(Function . name(fn)) == "Connections" ;
1019+ else false ;
1020+ end match;
1021+ end isConnectEq;
1022+
9801023function flattenEqBranch
9811024 input output Equation . Branch branch;
9821025 input ComponentRef prefix;
0 commit comments