diff --git a/Compiler/Algorithm.mo b/Compiler/Algorithm.mo index 02a04835f8c..362a32607e1 100644 --- a/Compiler/Algorithm.mo +++ b/Compiler/Algorithm.mo @@ -693,133 +693,19 @@ public function getAllExpsStmts "function: getAllExpsStmts input list stmts; output list exps; list> expslist; + list stms; algorithm - expslist := Util.listMap(stmts, getAllExpsStmt); - exps := Util.listFlatten(expslist); + (stms,exps) := DAEUtil.traverseDAEEquationsStmts(stmts,getAllExpsStmtsCollecter,{}); end getAllExpsStmts; -protected function getAllExpsStmt "function: getAllExpsStmt - Returns all expressions in a statement." - input Statement inStatement; - output list outExpExpLst; -algorithm - outExpExpLst:= - matchcontinue (inStatement) - local - DAE.Exp crexp,exp,e1,e2; - DAE.ExpType expty; - DAE.ComponentRef cr; - list exps,explist,exps1,elseexps,fargs; - list stmts; - Else else_; - Boolean flag; - Ident id; - Statement elsew; - Absyn.Path fname; - case DAE.STMT_ASSIGN(type_ = expty,exp1 = (e2 as DAE.CREF(cr,_)),exp = exp) - equation - crexp = Expression.makeCrefExp(cr,expty); - then - {crexp,exp}; - case DAE.STMT_ASSIGN(type_ = expty,exp1 = (e2 as DAE.ASUB(e1,ea2)),exp = exp) - local list ea2; - equation - then - {e2,exp}; - case DAE.STMT_TUPLE_ASSIGN(type_ = expty,expExpLst = explist,exp = exp) - equation - exps = listAppend(explist, {exp}); - then - exps; - case DAE.STMT_ASSIGN_ARR(type_ = expty,componentRef = cr,exp = exp) - equation - crexp = Expression.makeCrefExp(cr,expty); - then - {crexp,exp}; - case DAE.STMT_IF(exp = exp,statementLst = stmts,else_ = else_) - equation - exps1 = getAllExpsStmts(stmts); - elseexps = getAllExpsElse(else_); - exps = listAppend(exps1, elseexps); - then - (exp :: exps); - case DAE.STMT_FOR(type_ = expty,iterIsArray = flag,ident = id,exp = exp,statementLst = stmts) - equation - exps = getAllExpsStmts(stmts); - then - (exp :: exps); - case DAE.STMT_WHILE(exp = exp,statementLst = stmts) - equation - exps = getAllExpsStmts(stmts); - then - (exp :: exps); - case DAE.STMT_WHEN(exp = exp,statementLst = stmts, elseWhen=SOME(elsew)) - equation - exps1 = getAllExpsStmt(elsew); - exps = list_append(getAllExpsStmts(stmts),exps1); - then - (exp :: exps); - case DAE.STMT_WHEN(exp = exp,statementLst = stmts) - equation - exps = getAllExpsStmts(stmts); - then - (exp :: exps); - case DAE.STMT_ASSERT(cond = e1,msg= e2) then {e1,e2}; - case DAE.STMT_BREAK(source = _) then {}; - case DAE.STMT_RETURN(source = _) then {}; - case DAE.STMT_FAILURE(body = stmts) then getAllExpsStmts(stmts); - case DAE.STMT_THROW(source = _) then {}; - case DAE.STMT_TRY(tryBody = stmts) - equation - exps = getAllExpsStmts(stmts); - then - exps; - case DAE.STMT_CATCH(catchBody = stmts) - equation - exps = getAllExpsStmts(stmts); - then - exps; - - case DAE.STMT_NORETCALL(exp = e1) then {e1}; - - case(DAE.STMT_REINIT(var = e1, value = e2)) then {e1,e2}; - - case(DAE.STMT_MATCHCASES(caseStmt = exps)) then exps; - - case _ - equation - Debug.fprintln("failtrace", "- Algorithm.getAllExpsStmt failed"); - then - fail(); - end matchcontinue; -end getAllExpsStmt; - -protected function getAllExpsElse "function: getAllExpsElse - Helper function to getAllExpsStmt." - input Else inElse; - output list outExpExpLst; +function getAllExpsStmtsCollecter + input DAE.Exp exp; + input list inExps; + output DAE.Exp oexp; + output list outExps; algorithm - outExpExpLst:= - matchcontinue (inElse) - local - list exps1,elseexps,exps; - DAE.Exp exp; - list stmts; - Else else_; - case DAE.NOELSE() then {}; - case DAE.ELSEIF(exp = exp,statementLst = stmts,else_ = else_) - equation - exps1 = getAllExpsStmts(stmts); - elseexps = getAllExpsElse(else_); - exps = listAppend(exps1, elseexps); - then - (exp :: exps); - case DAE.ELSE(statementLst = stmts) - equation - exps = getAllExpsStmts(stmts); - then - exps; - end matchcontinue; -end getAllExpsElse; + oexp:=exp; + outExps:=listAppend(inExps,{exp}); +end getAllExpsStmtsCollecter; end Algorithm; diff --git a/Compiler/BackendDAECreate.mo b/Compiler/BackendDAECreate.mo index ea225c7ecf5..986eaedadd1 100644 --- a/Compiler/BackendDAECreate.mo +++ b/Compiler/BackendDAECreate.mo @@ -1721,12 +1721,7 @@ algorithm inputs2 = BackendDAEUtil.statesAndVarsExp(e1,vars); inputs = Util.listListUnionOnTrue({inputs1, inputs2}, Expression.expEqual); then (inputs,{}); - case(vars, DAE.STMT_TERMINATE(msg = e2)) - equation - inputs = BackendDAEUtil.statesAndVarsExp(e2, vars); - then - (inputs, {}); - // TODO: is the reinit var a input or a output + case(vars, DAE.STMT_TERMINATE(msg = _)) then ({}, {}); case(vars, DAE.STMT_REINIT(var = e as DAE.CREF(componentRef = _), value = e2)) equation inputs = BackendDAEUtil.statesAndVarsExp(e2, vars); diff --git a/Compiler/DAEUtil.mo b/Compiler/DAEUtil.mo index 3bfed403025..03dcc812bb3 100644 --- a/Compiler/DAEUtil.mo +++ b/Compiler/DAEUtil.mo @@ -3750,6 +3750,24 @@ algorithm (e_2 as DAE.CREF(cr_1,_), extraArg) = func(DAE.CREF(cr,tp), extraArg); (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); then (DAE.STMT_ASSIGN_ARR(tp,cr_1,e_1,source) :: xs_1,extraArg); + case (((x as DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source)) :: xs),func,extraArg) + equation + (e_1, extraArg) = func(e, extraArg); + failure((DAE.CREF(_,_), _) = func(DAE.CREF(cr,tp), extraArg)); + true = RTOpts.debugFlag("failtrace"); + print(DAEDump.ppStatementStr(x)); + print("Warning, not allowed to set the componentRef to a expression in DAEUtil.traverseDAEEquationsStmts\n"); + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_ASSIGN_ARR(tp,cr,e_1,source) :: xs_1,extraArg); + + case (((x as DAE.STMT_IF(exp=e,statementLst=stmts,else_ = el, source = source)) :: xs),func,extraArg) + local Algorithm.Else el,el_1; + equation + (el_1,extraArg) = traverseDAEEquationsStmtsElse(el,func,extraArg); + (stmts2,extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg); + (e_1,extraArg) = func(e, extraArg); + (xs_1,extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_IF(e_1,stmts2,el_1,source) :: xs_1,extraArg); case (((x as DAE.STMT_FOR(type_=tp,iterIsArray=b1,ident=id1,exp=e,statementLst=stmts, source = source)) :: xs),func,extraArg) equation @@ -3816,21 +3834,45 @@ algorithm equation (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); then (x :: xs_1,extraArg); - - case (((x as DAE.STMT_IF(exp=e,statementLst=stmts,else_ = el, source = source)) :: xs),func,extraArg) - local Algorithm.Else el,el_1; + // MetaModelica extension. KS + case (((x as DAE.STMT_FAILURE(body=stmts, source = source)) :: xs),func,extraArg) equation - (el_1,extraArg) = traverseDAEEquationsStmtsElse(el,func,extraArg); - (stmts2,extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg); - (e_1,extraArg) = func(e, extraArg); - (xs_1,extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); - then (DAE.STMT_IF(e_1,stmts2,el_1,source) :: xs_1,extraArg); - + (stmts2, extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg); + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_FAILURE(stmts2,source) :: xs_1,extraArg); + case (((x as DAE.STMT_TRY(tryBody=stmts, source = source)) :: xs),func,extraArg) + equation + (stmts2, extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg); + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_TRY(stmts2,source) :: xs_1,extraArg); + case (((x as DAE.STMT_CATCH(catchBody=stmts, source = source)) :: xs),func,extraArg) + equation + (stmts2, extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg); + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_CATCH(stmts2,source) :: xs_1,extraArg); + case (((x as DAE.STMT_THROW(source = source)) :: xs),func,extraArg) + equation + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (x :: xs_1,extraArg); + case (((x as DAE.STMT_GOTO(source = source)) :: xs),func,extraArg) + equation + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (x :: xs_1,extraArg); + case (((x as DAE.STMT_LABEL(source = source)) :: xs),func,extraArg) + equation + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (x :: xs_1,extraArg); + case (((x as DAE.STMT_MATCHCASES(matchType = matchType, inputExps = expl1,caseStmt=expl2, source = source)) :: xs),func,extraArg) + local Absyn.MatchType matchType; + equation + (expl1, extraArg) = traverseDAEExpList(expl1,func,extraArg); + (expl1, extraArg) = traverseDAEExpList(expl1,func,extraArg); + (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); + then (DAE.STMT_MATCHCASES(matchType,expl1,expl2,source) :: xs_1,extraArg); case ((x :: xs),func,extraArg) equation - print("Warning, not implemented in replace_equations_stmts\n"); (xs_1,extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); - then (x :: xs_1,extraArg); + then (x :: xs_1,extraArg); end matchcontinue; end traverseDAEEquationsStmts;