diff --git a/Compiler/BackEnd/BackendDAECreate.mo b/Compiler/BackEnd/BackendDAECreate.mo index c6118b4cdb..ef2d2d9d58 100644 --- a/Compiler/BackEnd/BackendDAECreate.mo +++ b/Compiler/BackEnd/BackendDAECreate.mo @@ -1447,6 +1447,7 @@ algorithm beqnslst = lowerEqnsLst(theneqns1,functionTree,{},false); (beqns,breqns,bieqns) = lowerEqns(elseenqs,functionTree,{},{},{},false); beqns = List.flatten({beqns,breqns,bieqns}); + countEquationsInBranches(beqnslst, beqns, inSource); then BackendDAE.IF_EQUATION(explst, beqnslst, beqns, inSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN)::inEquations; @@ -1459,6 +1460,55 @@ algorithm end matchcontinue; end lowerIfEquation; +protected function countEquationsInBranches " +Checks that the number of equations is the same in all branches +of an if-equation" + input list> trueBranches; + input list falseBranch; + input DAE.ElementSource source; + output Integer nrOfEquations; +algorithm + nrOfEquations := matchcontinue(trueBranches,falseBranch,source) + local + list b; + list strs; + String str,eqstr; + list nrOfEquationsBranches; + + case (_, _, _) + equation + nrOfEquations = BackendEquation.equationLstSize(falseBranch); + nrOfEquationsBranches = List.map(trueBranches, BackendEquation.equationLstSize); + b = List.map1(nrOfEquationsBranches, intEq, nrOfEquations); + true = List.reduce(b,boolAnd); + then + nrOfEquations; + + // An if-equation with non-parameter conditions must have an else-clause. + case (_, {}, _) + equation + Error.addSourceMessage(Error.IF_EQUATION_MISSING_ELSE, {}, + ElementSource.getElementSourceFileInfo(source)); + then + fail(); + + // If if-equation with non-parameter conditions must have the same number of + // equations in each branch. + case (_, _ :: _, _) + equation + nrOfEquations = BackendEquation.equationLstSize(falseBranch); + nrOfEquationsBranches = List.map(trueBranches, BackendEquation.equationLstSize); + eqstr = stringDelimitList(List.map(listAppend(trueBranches,{falseBranch}),BackendDump.dumpEqnsStr),"\n"); + strs = List.map(nrOfEquationsBranches, intString); + str = stringDelimitList(strs,","); + str = "{" + str + "," + intString(nrOfEquations) + "}"; + Error.addSourceMessage(Error.IF_EQUATION_UNBALANCED_2,{str,eqstr},ElementSource.getElementSourceFileInfo(source)); + then + fail(); + + end matchcontinue; +end countEquationsInBranches; + protected function lowerIfEquation1 input DAE.Exp cond; input list conditions; diff --git a/Compiler/BackEnd/BackendDAEOptimize.mo b/Compiler/BackEnd/BackendDAEOptimize.mo index 3ff9deddf5..b357b289ed 100644 --- a/Compiler/BackEnd/BackendDAEOptimize.mo +++ b/Compiler/BackEnd/BackendDAEOptimize.mo @@ -2300,7 +2300,6 @@ algorithm // true case left with condition<>false case (_,_,_,_,_,_,_) equation - _ = countEquationsInBranches(theneqns,elseenqs,source); // simplify if eqution // if .. then a=.. elseif .. then a=... else a=.. end if; // to @@ -2315,7 +2314,6 @@ algorithm eqns; case (_,_,_,_,_,_,_) equation - _ = countEquationsInBranches(theneqns,elseenqs,source); fbsExp = makeEquationLstToResidualExpLst(elseenqs); tbsExp = List.map(theneqns, makeEquationLstToResidualExpLst); eqns = makeEquationsFromResiduals(conditions, tbsExp, fbsExp, source, inEqAttr); @@ -2552,55 +2550,6 @@ algorithm oExp := DAE.IFEXP(cond,DAE.BCONST(true),else_); end makeIfExp; -protected function countEquationsInBranches " -Checks that the number of equations is the same in all branches -of an if-equation" - input list> trueBranches; - input list falseBranch; - input DAE.ElementSource source; - output Integer nrOfEquations; -algorithm - nrOfEquations := matchcontinue(trueBranches,falseBranch,source) - local - list b; - list strs; - String str,eqstr; - list nrOfEquationsBranches; - - case (_, _, _) - equation - nrOfEquations = BackendEquation.equationLstSize(falseBranch); - nrOfEquationsBranches = List.map(trueBranches, BackendEquation.equationLstSize); - b = List.map1(nrOfEquationsBranches, intEq, nrOfEquations); - true = List.reduce(b,boolAnd); - then - nrOfEquations; - - // An if-equation with non-parameter conditions must have an else-clause. - case (_, {}, _) - equation - Error.addSourceMessage(Error.IF_EQUATION_MISSING_ELSE, {}, - ElementSource.getElementSourceFileInfo(source)); - then - fail(); - - // If if-equation with non-parameter conditions must have the same number of - // equations in each branch. - case (_, _ :: _, _) - equation - nrOfEquations = BackendEquation.equationLstSize(falseBranch); - nrOfEquationsBranches = List.map(trueBranches, BackendEquation.equationLstSize); - eqstr = stringDelimitList(List.map(listAppend(trueBranches,{falseBranch}),BackendDump.dumpEqnsStr),"\n"); - strs = List.map(nrOfEquationsBranches, intString); - str = stringDelimitList(strs,","); - str = "{" + str + "," + intString(nrOfEquations) + "}"; - Error.addSourceMessage(Error.IF_EQUATION_UNBALANCED_2,{str,eqstr},ElementSource.getElementSourceFileInfo(source)); - then - fail(); - - end matchcontinue; -end countEquationsInBranches; - protected function makeEquationLstToResidualExpLst input list eqLst; output list oExpLst;