Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 62a0b9d

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Add support for inline of some if-statements
If-statements that look like if-expressions can now be inlined. This fixes an issue reported in ticket:4423. Belonging to [master]: - #2076 - OpenModelica/OpenModelica-testsuite#810
1 parent 70f05e5 commit 62a0b9d

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

Compiler/FrontEnd/DAEDump.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ public function ppStmtListStr "
17901790
Helper function to pp_stmt_str
17911791
"
17921792
input list<DAE.Statement> inAlgorithmStatementLst;
1793-
input Integer inInteger;
1793+
input Integer inInteger=0;
17941794
output String outString;
17951795
algorithm
17961796
outString:=

Compiler/FrontEnd/Inline.mo

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ algorithm
10161016
local
10171017
list<DAE.Statement> stmts;
10181018
VarTransform.VariableReplacements repl;
1019-
DAE.ComponentRef cr;
1019+
DAE.ComponentRef cr, cr1, cr2;
10201020
DAE.ElementSource source;
10211021
DAE.Exp exp, exp1, exp2;
10221022
DAE.Statement stmt;
@@ -1053,6 +1053,31 @@ algorithm
10531053
(repl,assertStmts) = mergeFunctionBody(stmts,iRepl,stmt::assertStmtsIn);
10541054
then
10551055
(repl,assertStmts);
1056+
// if a then x := b; else x := c; end if; => x := if a then b else c;
1057+
case (DAE.STMT_IF(exp = exp,
1058+
statementLst = {DAE.STMT_ASSIGN(exp1 = DAE.CREF(componentRef = cr1), exp = exp1)},
1059+
else_=DAE.ELSE(statementLst={DAE.STMT_ASSIGN(exp1 = DAE.CREF(componentRef = cr2), exp = exp2)}))::stmts,_,_)
1060+
guard ComponentReference.crefEqual(cr1, cr2)
1061+
equation
1062+
(exp,_) = VarTransform.replaceExp(exp,iRepl,NONE());
1063+
(exp1,_) = VarTransform.replaceExp(exp1,iRepl,NONE());
1064+
(exp2,_) = VarTransform.replaceExp(exp2,iRepl,NONE());
1065+
repl = VarTransform.addReplacementNoTransitive(iRepl,cr1,DAE.IFEXP(exp,exp1,exp2));
1066+
(repl,assertStmts) = mergeFunctionBody(stmts,repl,assertStmtsIn);
1067+
then (repl,assertStmts);
1068+
1069+
case (DAE.STMT_IF(exp = exp,
1070+
statementLst = {DAE.STMT_ASSIGN_ARR(lhs = DAE.CREF(componentRef = cr1), exp = exp1)},
1071+
else_=DAE.ELSE(statementLst={DAE.STMT_ASSIGN_ARR(lhs = DAE.CREF(componentRef = cr2), exp = exp2)}))::stmts,_,_)
1072+
guard ComponentReference.crefEqual(cr1, cr2)
1073+
equation
1074+
(exp,_) = VarTransform.replaceExp(exp,iRepl,NONE());
1075+
(exp1,_) = VarTransform.replaceExp(exp1,iRepl,NONE());
1076+
(exp2,_) = VarTransform.replaceExp(exp2,iRepl,NONE());
1077+
repl = VarTransform.addReplacementNoTransitive(iRepl,cr1,DAE.IFEXP(exp,exp1,exp2));
1078+
(repl,assertStmts) = mergeFunctionBody(stmts,repl,assertStmtsIn);
1079+
then (repl,assertStmts);
1080+
10561081
end match;
10571082
end mergeFunctionBody;
10581083

0 commit comments

Comments
 (0)