Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 77.7% 496 / 0 / 638
Functions: -% 0 / 1 / 1
Branches: 72.5% 358 / 0 / 494

OMCompiler/Compiler/BackEnd/BackendVarTransform.mo
Line Branch Exec Source
1 /*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36 encapsulated package BackendVarTransform
37 " file: BackendVarTransform.mo
38 package: BackendVarTransform
39 description: BackendVarTransform contains a Binary Tree representation of variable replacements.
40
41
42 This module contain a Binary tree representation of variable replacements
43 along with some functions for performing replacements of variables in equations"
44
45 public import BackendDAE;
46 public import DAE;
47 public import UnorderedMap;
48 public import UnorderedSet;
49
50 protected import Absyn;
51 protected import AbsynUtil;
52 protected import BackendDAETransform;
53 protected import BaseHashSet;
54 protected import BackendEquation;
55 protected import BackendVariable;
56 protected import ClassInf;
57 protected import ComponentReference;
58 protected import ComponentReferenceBasics;
59 protected import DAEUtil;
60 protected import Debug;
61 protected import DoubleEnded;
62 protected import ElementSource;
63 protected import Expression;
64 protected import ExpressionBasics;
65 protected import ExpressionDump;
66 protected import ExpressionSimplify;
67 protected import Flags;
68 protected import HashSet;
69 protected import List;
70 protected import Types;
71 protected import Util;
72 protected import MetaModelica.Dangerous.listReverseInPlace;
73
74 public
75 type ExpOpt = Option<DAE.Exp>;
76 type CrefExpTable = UnorderedMap<DAE.ComponentRef, ExpOpt>;
77 type CrefList = list<DAE.ComponentRef>;
78 type CrefListOpt = Option<CrefList>;
79 type CrefCrefListTable = UnorderedMap<DAE.ComponentRef, CrefListOpt>;
80 type CrefSet = UnorderedSet<DAE.ComponentRef>;
81
82 uniontype VariableReplacements
83 "VariableReplacements consists of a mapping between variables and expressions, the first binary tree of this type.
84 To eliminate a variable from an equation system a replacement rule varname->expression is added to this
85 datatype.
86 To be able to update these replacement rules incrementally a backward lookup mechanism is also required.
87 For instance, having a rule a->b and adding a rule b->c requires to find the first rule a->b and update it to
88 a->c. This is what the second binary tree is used for."
89 record REPLACEMENTS
90 CrefExpTable hashTable "src -> dst, used for replacing. src is variable, dst is expression.";
91 CrefCrefListTable invHashTable "dst -> list of sources. dst is a variable, sources are variables.";
92 CrefSet extendhashTable "src -> nothing, used for extend arrays and records.";
93 list<DAE.Ident> iterationVars "this are the implicit declerate iteration variables for for and range expressions";
94 Option<CrefExpTable> derConst "this is used if states are constant to replace der(state) with 0.0";
95 end REPLACEMENTS;
96
97 end VariableReplacements;
98
99 partial function FuncTypeExp_ExpToBoolean
100 input DAE.Exp inExp;
101 output Boolean outBoolean;
102 end FuncTypeExp_ExpToBoolean;
103
104 function newCrefExpTable
105 output CrefExpTable table = UnorderedMap.new<ExpOpt>(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual);
106 end newCrefExpTable;
107
108 function newCrefExpTableSized
109 input Integer size;
110 output CrefExpTable table = UnorderedMap.new<ExpOpt>(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual, size);
111 end newCrefExpTableSized;
112
113 function newCrefCrefListTable
114 output CrefCrefListTable table = UnorderedMap.new<CrefListOpt>(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual);
115 end newCrefCrefListTable;
116
117 function newCrefCrefListTableSized
118 input Integer size;
119 output CrefCrefListTable table = UnorderedMap.new<CrefListOpt>(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual, size);
120 end newCrefCrefListTableSized;
121
122 function newCrefSet
123 output CrefSet set = UnorderedSet.new(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual);
124 end newCrefSet;
125
126 function newCrefSetSized
127 input Integer size;
128 output CrefSet set = UnorderedSet.new(ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual, size);
129 end newCrefSetSized;
130
131 public function emptyReplacements "
132 Returns an empty set of replacement rules
133 "
134 output VariableReplacements outVariableReplacements =
135 REPLACEMENTS(newCrefExpTable(), newCrefCrefListTable(), newCrefSet(), {}, NONE());
136 end emptyReplacements;
137
138 public function emptyReplacementsSized "Returns an empty set of replacement rules, giving a size of hashtables to allocate"
139 input Integer size;
140 output VariableReplacements outVariableReplacements =
141 REPLACEMENTS(newCrefExpTableSized(size), newCrefCrefListTableSized(size), newCrefSetSized(size), {}, NONE());
142 end emptyReplacementsSized;
143
144 public function removeReplacement
145 "Removes the replacement for a given key. The extendhashtable is not updated."
146 input VariableReplacements repl;
147 input DAE.ComponentRef inSrc;
148 protected
149 Option<DAE.Exp> dst_opt;
150 algorithm
151 465041 dst_opt := UnorderedMap.getOrDefault(inSrc, repl.hashTable, NONE());
152
153
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 465041 times.
✓ Branch 2 taken 462175 times.
✓ Branch 3 taken 2866 times.
465041 if isNone(dst_opt) then
154 462175 return;
155 end if;
156
157 try
158 // Replace the value with NONE() to mark it as deleted.
159 2866 UnorderedMap.add(inSrc, NONE(), repl.hashTable);
160 2866 removeReplacementInv(repl.invHashTable, Util.getOption(dst_opt));
161 else
162 ✗ Error.addInternalError("-BackendVarTransform.removeReplacement failed for " + ComponentReferenceBasics.printComponentRefStr(inSrc) +"\n", sourceInfo());
163 end try;
164 end removeReplacement;
165
166 public function removeReplacements
167 input VariableReplacements iRepl;
168 input list<DAE.ComponentRef> inSrcs;
169 algorithm
170
2/2
✓ Branch 0 taken 155337 times.
✓ Branch 1 taken 391323 times.
546660 for cr in inSrcs loop
171 155337 removeReplacement(iRepl,cr);
172 end for;
173 end removeReplacements;
174
175 public function addReplacements
176 input VariableReplacements iRepl;
177 input list<DAE.ComponentRef> inSrcs;
178 input list<DAE.Exp> inDsts;
179 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
180 output VariableReplacements outRepl;
181 algorithm
182 outRepl := match(inSrcs, inDsts)
183 local
184 DAE.ComponentRef cr;
185 list<DAE.ComponentRef> crlst;
186 DAE.Exp exp;
187 VariableReplacements repl;
188 list<DAE.Exp> explst;
189 case ({}, {}) then iRepl;
190 case (cr::crlst, exp::explst)
191 algorithm
192 159419 repl := addReplacement(iRepl,cr,exp,inFuncTypeExpExpToBooleanOption);
193 159419 then
194 addReplacements(repl,crlst,explst,inFuncTypeExpExpToBooleanOption);
195 end match;
196 end addReplacements;
197
198 public function addReplacement "
199 Adds a replacement rule to the set of replacement rules given as argument.
200 If a replacement rule a->b already exists and we add a new rule b->c then
201 the rule a->b is updated to a->c. This is done using the make_transitive
202 function.
203 "
204 input VariableReplacements repl;
205 input DAE.ComponentRef inSrc;
206 input DAE.Exp inDst;
207 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
208 output VariableReplacements outRepl;
209 algorithm
210 outRepl:=
211 matchcontinue (inSrc, inDst)
212 local
213 DAE.ComponentRef src;
214 DAE.Exp dst;
215 CrefExpTable ht;
216 CrefSet eht;
217 CrefCrefListTable invHt;
218 list<DAE.Ident> iv;
219 String s;
220 Option<CrefExpTable> derConst;
221 // PA: Commented out this, since it will only slow things down without adding any functionality.
222 // Once match is available as a complement to matchcontinue, this case could be useful again.
223 //case ((repl as REPLACEMENTS(ht,invHt)),src,dst) /* source dest */
224 // equation
225 // olddst = BaseHashTable.get(src, ht) "if rule a->b exists, fail";
226 // then
227 // fail();
228
229 case (src, dst)
230 algorithm
231 578999 (REPLACEMENTS(ht,invHt,eht,iv,derConst),src,dst) := makeTransitive(repl, src, dst, inFuncTypeExpExpToBooleanOption);
232 /*s1 = ComponentReferenceBasics.printComponentRefStr(src);
233 s2 = ExpressionBasics.printExpStr(dst);
234 s3 = ComponentReferenceBasics.printComponentRefStr(src_1);
235 s4 = ExpressionBasics.printExpStr(dst_1);
236 s = stringAppendList(
237 {"add_replacement(",s1,", ",s2,") -> add_replacement(",s3,
238 ", ",s4,")\n"});
239 print(s);
240 fprint(Flags.ADD_REPL, s);*/
241
242 578999 UnorderedMap.add(src, SOME(dst), ht);
243 578999 invHt := addReplacementInv(invHt, src, dst);
244 578999 eht := addExtendReplacement(eht,src,NONE());
245 578999 then
246 REPLACEMENTS(ht,invHt,eht,iv,derConst);
247 case (_, _)
248 algorithm
249 ✗ s := ComponentReferenceBasics.printComponentRefStr(inSrc);
250 ✗ print("-BackendVarTransform.addReplacement failed for " + s);
251 ✗ then
252 fail();
253 end matchcontinue;
254 end addReplacement;
255
256 public function performReplacementsEqSystem
257 input BackendDAE.EqSystem inEqs;
258 input VariableReplacements inRepl;
259 output BackendDAE.EqSystem outEqs = inEqs;
260 protected
261 BackendDAE.EquationArray eqArr;
262 algorithm
263 30 eqArr := inEqs.orderedEqs;
264 30 BackendVariable.traverseBackendDAEVarsWithUpdate(inEqs.orderedVars, replaceVarTraverser, inRepl);
265 30 (eqArr, _) := replaceEquationsArr(eqArr, inRepl, NONE());
266 30 outEqs.orderedEqs := eqArr;
267 end performReplacementsEqSystem;
268
269 protected function addReplacementNoTransitive "Similar to addReplacement but
270 does not make transitive replacement rules.
271 "
272 input VariableReplacements repl;
273 input DAE.ComponentRef inSrc;
274 input DAE.Exp inDst;
275 output VariableReplacements outRepl;
276 algorithm
277 outRepl:=
278 matchcontinue (repl,inSrc,inDst)
279 local
280 DAE.ComponentRef src;
281 DAE.Exp dst;
282 CrefExpTable ht;
283 CrefSet eht;
284 CrefCrefListTable invHt;
285 list<DAE.Ident> iv;
286 Option<CrefExpTable> derConst;
287 case (REPLACEMENTS(hashTable=ht),src,_) /* source dest */
288 guard
289 isSome(UnorderedMap.getOrDefault(src, ht, NONE())) // if rule a->b exists, fail
290 then
291 fail();
292 case (REPLACEMENTS(ht,invHt,eht,iv,derConst),src,dst)
293 algorithm
294 16066 UnorderedMap.add(src, SOME(dst), ht);
295 16066 invHt := addReplacementInv(invHt, src, dst);
296 16066 eht := addExtendReplacement(eht,src,NONE());
297 16066 then
298 REPLACEMENTS(ht,invHt,eht,iv,derConst);
299 else
300 algorithm
301 ✗ print("-add_replacement failed for " + ComponentReferenceBasics.printComponentRefStr(inSrc) + " = " + ExpressionBasics.printExpStr(inDst) + "\n");
302 ✗ then
303 fail();
304 end matchcontinue;
305 end addReplacementNoTransitive;
306
307 protected function removeReplacementInv "
308 Helper function to removeReplacement
309 removes the inverse rule of a replacement in the second binary tree
310 of VariableReplacements.
311 "
312 input CrefCrefListTable invHt;
313 input DAE.Exp dst;
314 algorithm
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2866 times.
2866 for d in Expression.extractCrefsFromExp(dst) loop
316 ✗ UnorderedMap.tryUpdate(d, NONE(), invHt);
317 end for;
318 end removeReplacementInv;
319
320 protected function addReplacementInv "
321 Helper function to addReplacement
322 Adds the inverse rule of a replacement to the second binary tree
323 of VariableReplacements.
324 "
325 input output CrefCrefListTable invHt;
326 input DAE.ComponentRef src;
327 input DAE.Exp dst;
328 algorithm
329
2/2
✓ Branch 1 taken 113623 times.
✓ Branch 2 taken 595065 times.
708688 for d in Expression.extractCrefsFromExp(dst) loop
330 113623 invHt := addReplacementInv2(invHt, d, src);
331 end for;
332 end addReplacementInv;
333
334 protected function addReplacementInv2 "
335 Helper function to addReplacementInv
336 Adds the inverse rule for one of the variables of a replacement to the second binary tree
337 of VariableReplacements.
338 Since a replacement is on the form var -> expression of vars(v1,v2,...,vn) the inverse binary tree
339 contains rules for v1 -> var, v2 -> var, ...., vn -> var so that any of the variables of the expression
340 will update the rule.
341 "
342 input output CrefCrefListTable invHt;
343 input DAE.ComponentRef dst;
344 input DAE.ComponentRef src;
345 protected
346 Option<list<DAE.ComponentRef>> srcs_opt;
347 list<DAE.ComponentRef> srcs;
348 algorithm
349 113623 srcs_opt := UnorderedMap.getOrDefault(dst, invHt, NONE());
350
351
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 113623 times.
✓ Branch 2 taken 37257 times.
✓ Branch 3 taken 76366 times.
113623 if isSome(srcs_opt) then
352 // previous elt for dst -> src, append..
353 76366 SOME(srcs) := srcs_opt;
354 srcs := src::srcs;
355 76366 UnorderedMap.add(dst, SOME(srcs), invHt);
356 else
357 // No previous elt for dst -> src
358 37257 UnorderedMap.add(dst, SOME({src}), invHt);
359 end if;
360 end addReplacementInv2;
361
362 protected function makeTransitive "
363 This function takes a set of replacement rules and a new replacement rule
364 in the form of two ComponentRef:s and makes sure the new replacement rule
365 is replaced with the transitive value.
366 For example, if we have the rule a->b and a new rule c->a it is changed to c->b.
367 Also, if we have a rule a->b and a new rule b->c then the -old- rule a->b is changed
368 to a->c.
369 For arbitrary expressions: if we have a rule ax-> expr(b1,..,bn) and a new rule c->expr(a1,ax,..,an)
370 it is changed to c-> expr(a1,expr(b1,...,bn),..,an).
371 And similary for a rule ax -> expr(b1,bx,..,bn) and a new rule bx->expr(c1,..,cn) then old rule is changed to
372 ax -> expr(b1,expr(c1,..,cn),..,bn).
373 "
374 input VariableReplacements repl;
375 input DAE.ComponentRef src;
376 input DAE.Exp dst;
377 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
378 output VariableReplacements outRepl;
379 output DAE.ComponentRef outSrc;
380 output DAE.Exp outDst;
381 algorithm
382 (outRepl,outSrc,outDst):=
383 match inFuncTypeExpExpToBooleanOption
384 local
385 VariableReplacements repl_1,repl_2;
386 DAE.ComponentRef src_1,src_2;
387 DAE.Exp dst_1,dst_2,dst_3;
388
389 case _
390 algorithm
391 578999 (repl_1,src_1,dst_1) := makeTransitive1(repl, src, dst,inFuncTypeExpExpToBooleanOption);
392 578999 (repl_2,src_2,dst_2) := makeTransitive2(repl_1, src_1, dst_1,inFuncTypeExpExpToBooleanOption);
393 578999 (dst_3,_) := ExpressionSimplify.simplify1(dst_2) "to remove e.g. --a";
394
1/2
✓ Branch 0 taken 578999 times.
✗ Branch 1 not taken.
578999 then
395 (repl_2,src_2,dst_3);
396 end match;
397 end makeTransitive;
398
399 protected function makeTransitive1 "
400 helper function to makeTransitive
401 "
402 input VariableReplacements repl;
403 input DAE.ComponentRef src;
404 input DAE.Exp dst;
405 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
406 output VariableReplacements outRepl;
407 output DAE.ComponentRef outSrc;
408 output DAE.Exp outDst;
409 algorithm
410 (outRepl,outSrc,outDst):=
411 matchcontinue repl
412 local
413 list<DAE.ComponentRef> lst;
414 VariableReplacements repl_1,singleRepl;
415 CrefCrefListTable invHt;
416 // old rule a->expr(b1,..,bn) must be updated to a->expr(c_exp,...,bn) when new rule b1->c_exp
417 // is introduced
418 case REPLACEMENTS(invHashTable=invHt)
419 algorithm
420
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3371 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3371 times.
578999 SOME(lst) := UnorderedMap.getOrFail(src, invHt);
421 3371 singleRepl := addReplacementNoTransitive(emptyReplacementsSized(53),src,dst);
422 3371 repl_1 := makeTransitive12(lst,repl,singleRepl,inFuncTypeExpExpToBooleanOption,HashSet.emptyHashSet());
423 then
424 (repl_1,src,dst);
425 else (repl,src,dst);
426 end matchcontinue;
427 end makeTransitive1;
428
429 protected function makeTransitive12 "Helper function to makeTransitive1
430 For each old rule a->expr(b1,..,bn) update dest by applying the new rule passed as argument
431 in singleRepl."
432 input list<DAE.ComponentRef> lst;
433 input VariableReplacements repl;
434 input VariableReplacements singleRepl "contain one replacement rule: the rule to be added";
435 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
436 input HashSet.HashSet inSet "to avoid double work";
437 output VariableReplacements outRepl = repl;
438 protected
439 HashSet.HashSet set = inSet;
440 DAE.Exp crDst;
441 algorithm
442
2/2
✓ Branch 0 taken 12695 times.
✓ Branch 1 taken 3371 times.
16066 for cr in lst loop
443
1/2
✓ Branch 1 taken 12695 times.
✗ Branch 2 not taken.
12695 if not BaseHashSet.has(cr,set) then
444 try
445 12695 set := BaseHashSet.add(cr,set);
446
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 12695 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12695 times.
12695 SOME(crDst) := UnorderedMap.getOrFail(cr,outRepl.hashTable);
447 12695 (crDst,_) := replaceExp(crDst,singleRepl,inFuncTypeExpExpToBooleanOption);
448 12695 outRepl := addReplacementNoTransitive(outRepl,cr,crDst) "add updated old rule";
449 else
450 end try;
451 end if;
452 end for;
453 end makeTransitive12;
454
455 protected function makeTransitive2 "
456 Helper function to makeTransitive
457 "
458 input VariableReplacements repl;
459 input DAE.ComponentRef src;
460 input DAE.Exp dst;
461 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
462 output VariableReplacements outRepl;
463 output DAE.ComponentRef outSrc;
464 output DAE.Exp outDst;
465 algorithm
466 (outRepl,outSrc,outDst):=
467 match inFuncTypeExpExpToBooleanOption
468 local
469 DAE.Exp dst_1;
470 // for rule a->b1+..+bn, replace all b1 to bn's in the expression;
471 case _
472 algorithm
473 578999 (dst_1,_) := replaceExp(dst,repl,inFuncTypeExpExpToBooleanOption);
474 then
475 (repl,src,dst_1);
476 // replace Exp failed, keep old rule.
477 case _ then (repl,src,dst); /* dst has no own replacement, return */
478 end match;
479 end makeTransitive2;
480
481 protected function addExtendReplacement
482 "author: Frenkel TUD 2011-04
483 checks if the parents of cref from type array or record
484 and add a rule to extend them."
485 input CrefSet extendrepl;
486 input DAE.ComponentRef cr;
487 input Option<DAE.ComponentRef> preCr;
488 output CrefSet outExtendrepl = extendrepl;
489 protected
490 list<tuple<DAE.ComponentRef, Option<DAE.ComponentRef>>> worklist = {(cr, preCr)};
491 DAE.ComponentRef wcr, pk;
492 Option<DAE.ComponentRef> wpre;
493 algorithm
494 // A walk adds the prefixes top-down, so when the deepest one is present
495 // already only the last identifier is left to handle.
496
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 595065 times.
✓ Branch 2 taken 595065 times.
✗ Branch 3 not taken.
595065 if isNone(preCr) then
497 try
498
2/2
✓ Branch 0 taken 82310 times.
✓ Branch 1 taken 512755 times.
595065 DAE.CREF_QUAL() := cr;
499 512755 pk := ComponentReference.crefStripLastIdent(cr);
500
2/2
✓ Branch 2 taken 440774 times.
✓ Branch 3 taken 71981 times.
512755 if UnorderedSet.contains(ComponentReferenceBasics.crefStripLastSubs(pk), extendrepl) then
501 440774 worklist := {(ComponentReferenceBasics.crefLastCref(cr), SOME(pk))};
502 end if;
503 else
504 end try;
505 end if;
506 // The replacement set is mutated in-place, so visiting order does not matter
507
2/2
✓ Branch 0 taken 781654 times.
✓ Branch 1 taken 595065 times.
1376719 while not listEmpty(worklist) loop
508 781654 (wcr, wpre) := listHead(worklist);
509 781654 worklist := listRest(worklist);
510 _ := match (wcr, wpre)
511 local
512 DAE.ComponentRef subcr,precr,precr1,pcr,precrn,precrn1;
513 DAE.Ident ident;
514 DAE.Type ty;
515 list<DAE.Subscript> subscriptLst;
516 list<DAE.Var> varLst;
517 list<DAE.ComponentRef> crefs;
518 String s;
519 case (DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()), NONE())
520 algorithm
521 10391 precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
522 10391 UnorderedSet.add(precr, extendrepl);
523 then ();
524 case (DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()), SOME(pcr))
525 algorithm
526 176125 precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
527 176125 precr1 := ComponentReference.joinCrefs(pcr,precr);
528 176125 UnorderedSet.add(precr1, extendrepl);
529 then ();
530 case (DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)), NONE())
531 algorithm
532 ✗ precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
533 ✗ if not UnorderedSet.contains(precr, extendrepl) then
534 ✗ UnorderedSet.add(precr, extendrepl);
535 // queue the record members, parented by precr
536 ✗ crefs := List.map(varLst,ComponentReference.creffromVar);
537 ✗ for c in crefs loop
538 ✗ worklist := (c, SOME(precr)) :: worklist;
539 end for;
540 end if;
541 then ();
542 case (DAE.CREF_IDENT(identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)), SOME(pcr))
543 algorithm
544 ✗ precr1 := ComponentReference.joinCrefs(pcr,wcr);
545 ✗ if not UnorderedSet.contains(precr1, extendrepl) then
546 ✗ UnorderedSet.add(precr1, extendrepl);
547 // queue the record members, parented by precr1
548 ✗ crefs := List.map(varLst,ComponentReference.creffromVar);
549 ✗ for c in crefs loop
550 ✗ worklist := (c, SOME(precr1)) :: worklist;
551 end for;
552 end if;
553 then ();
554 case (DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_), NONE())
555 algorithm
556 ✗ precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
557 ✗ UnorderedSet.add(precr, extendrepl);
558 then ();
559 case (DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_), SOME(pcr))
560 algorithm
561 ✗ precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
562 ✗ precr1 := ComponentReference.joinCrefs(pcr,precr);
563 ✗ UnorderedSet.add(precr1, extendrepl);
564 then ();
565 case (DAE.CREF_IDENT(), _)
566 then ();
567 case (DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr), NONE())
568 algorithm
569 71981 precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
570 71981 UnorderedSet.add(precr, extendrepl);
571 71981 precrn := ComponentReferenceBasics.makeCrefIdent(ident,ty,subscriptLst);
572 71981 worklist := (subcr, SOME(precrn)) :: worklist;
573 then ();
574 case (DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr), SOME(pcr))
575 algorithm
576 114608 precr := ComponentReferenceBasics.makeCrefIdent(ident,ty,{});
577 114608 precr1 := ComponentReference.joinCrefs(pcr,precr);
578 114608 UnorderedSet.add(precr1, extendrepl);
579 114608 precrn := ComponentReferenceBasics.makeCrefIdent(ident,ty,subscriptLst);
580 114608 precrn1 := ComponentReference.joinCrefs(pcr,precrn);
581 114608 worklist := (subcr, SOME(precrn1)) :: worklist;
582 then ();
583 end match;
584 end while;
585 end addExtendReplacement;
586
587 protected function addIterationVar
588 "add a var to the iterationVars"
589 input output VariableReplacements repl;
590 input DAE.Ident inVar;
591 algorithm
592 451 repl.iterationVars := inVar :: repl.iterationVars;
593 end addIterationVar;
594
595 protected function removeIterationVar
596 "remove the first equal var from the iterationVars"
597 input output VariableReplacements repl;
598 input DAE.Ident inVar;
599 algorithm
600 91 repl.iterationVars := List.deleteMemberOnTrue(inVar, repl.iterationVars, stringEq);
601 end removeIterationVar;
602
603 protected function isIterationVar
604 "remove true if it is an iteration var"
605 input VariableReplacements repl;
606 input DAE.Ident inVar;
607 output Boolean is;
608 algorithm
609 is:=
610 match repl
611 local
612 list<DAE.Ident> iv;
613 case REPLACEMENTS(iterationVars=iv)
614 1532166 then
615 listMember(inVar, iv);
616 end match;
617 end isIterationVar;
618
619 public function addDerConstRepl
620 "add a var to the derConst replacements, replace der(const) with 0.0"
621 input DAE.ComponentRef inComponentRef;
622 input DAE.Exp inExp;
623 input output VariableReplacements repl;
624 protected
625 CrefExpTable derConst;
626 algorithm
627
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2058 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 1930 times.
2058 if isSome(repl.derConst) then
628 3860 UnorderedMap.add(inComponentRef, SOME(inExp), Util.getOption(repl.derConst));
629 else
630 128 derConst := newCrefExpTable();
631 128 UnorderedMap.add(inComponentRef, SOME(inExp), derConst);
632 128 repl.derConst := SOME(derConst);
633 end if;
634 end addDerConstRepl;
635
636 public function getReplacement "
637 Retrives a replacement variable given a set of replacement rules and a
638 source variable.
639 "
640 input VariableReplacements inVariableReplacements;
641 input DAE.ComponentRef inComponentRef;
642 output DAE.Exp outExp;
643 algorithm
644
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 470167 times.
✓ Branch 3 taken 2232 times.
✓ Branch 4 taken 467935 times.
4932837 SOME(outExp) := UnorderedMap.getOrFail(inComponentRef, inVariableReplacements.hashTable);
645 end getReplacement;
646
647 public function hasReplacement "
648 Outputs true if the replacements contain a rule for the cref
649 "
650 input VariableReplacements repl;
651 input DAE.ComponentRef inComponentRef;
652 output Boolean bOut;
653 algorithm
654
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 297806 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 297805 times.
297806 bOut := isSome(UnorderedMap.getOrDefault(inComponentRef, repl.hashTable, NONE()));
655 end hasReplacement;
656
657 public function hasNoReplacement "
658 Outputs false if the replacements contain a rule for the cref
659 "
660 input DAE.ComponentRef inComponentRef;
661 input VariableReplacements repl;
662 output Boolean bOut;
663 algorithm
664
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 303 times.
303 bOut := isNone(UnorderedMap.getOrDefault(inComponentRef, repl.hashTable, NONE()));
665 end hasNoReplacement;
666
667 public function getAllReplacements
668 input VariableReplacements repl;
669 output list<DAE.ComponentRef> crefs;
670 output list<DAE.Exp> dsts;
671 algorithm
672 35320 (crefs, dsts) := getCrefExpTableEntries(repl.hashTable);
673 end getAllReplacements;
674
675 protected function getCrefExpTableEntries
676 input CrefExpTable table;
677 output list<DAE.ComponentRef> crefs;
678 output list<DAE.Exp> dsts;
679 protected
680 list<Option<DAE.Exp>> opt_dsts;
681 algorithm
682 35321 crefs := UnorderedMap.keyList(table);
683 35321 opt_dsts := UnorderedMap.valueList(table);
684 // Remove deleted replacements, i.e. replacements set to NONE().
685 35321 (opt_dsts, crefs) := List.filterOnTrueSync(opt_dsts, isSome, crefs);
686
5/6
✓ Branch 0 taken 174589 times.
✓ Branch 1 taken 35321 times.
✓ Branch 2 taken 174589 times.
✓ Branch 3 taken 35321 times.
✓ Branch 5 taken 35321 times.
✗ Branch 6 not taken.
209910 dsts := list(Util.getOption(d) for d in opt_dsts);
687 end getCrefExpTableEntries;
688
689 public function hasExtendReplacement "
690 Returns whether an extend replacement exists or not.
691 "
692 input VariableReplacements repl;
693 input DAE.ComponentRef src;
694 output Boolean exists;
695 algorithm
696 4460974 exists := UnorderedSet.contains(ComponentReferenceBasics.crefStripLastSubs(src), repl.extendhashTable);
697 end hasExtendReplacement;
698
699 protected function avoidDoubleHashLookup "
700 Author BZ 200X-XX modified 2008-06
701 When adding replacement rules, we might not have the correct type availible at the moment.
702 Then DAE.T_UNKNOWN_DEFAULT is used, so when replacing exp and finding DAE.T_UNKNOWN_DEFAULT, we use the
703 type of the expression to be replaced instead.
704 TODO: find out why array residual functions containing arrays as xloc[] does not work,
705 doing that will allow us to use this function for all crefs."
706 input DAE.Exp inExp;
707 input DAE.Type inType;
708 output DAE.Exp outExp;
709 algorithm outExp := matchcontinue inExp
710 local DAE.ComponentRef cr;
711 ✗ case DAE.CREF(cr,DAE.T_UNKNOWN()) then Expression.makeCrefExp(cr,inType);
712 else inExp;
713 end matchcontinue;
714 end avoidDoubleHashLookup;
715
716
717 public function isReplacementEmpty
718 input VariableReplacements repl;
719 output Boolean empty = UnorderedMap.none(repl.hashTable, isSome) and isNone(repl.derConst);
720 end isReplacementEmpty;
721
722 /*********************************************************/
723 /* replace Expression with condition function */
724 /*********************************************************/
725
726 public function replaceExp "Takes a set of replacement rules and an expression and a function
727 giving a boolean value for an expression.
728 The function replaces all variables in the expression using
729 the replacement rules, if the boolean value is true children of the
730 expression is visited (including the expression itself). If it is false,
731 no replacemet is performed."
732 input DAE.Exp inExp;
733 input VariableReplacements inVariableReplacements;
734 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
735 output DAE.Exp outExp;
736 output Boolean replacementPerformed;
737 algorithm
738 (outExp,replacementPerformed) :=
739 match (inExp,inFuncTypeExpExpToBooleanOption)
740 local
741 DAE.ComponentRef cr;
742 DAE.Exp e,e1_1,e2_1,e1,e2,e3_1,e3, solverMethod, resolution, startInterval;
743 DAE.Type t,tp;
744 VariableReplacements repl;
745 Option<FuncTypeExp_ExpToBoolean> cond;
746 DAE.Operator op;
747 list<DAE.Exp> expl_1,expl;
748 list<DAE.Subscript> subs;
749 Absyn.Path path;
750 Boolean c,c1,c2,c3;
751 Integer b,i;
752 Absyn.CodeNode a;
753 list<list<DAE.Exp>> bexpl_1,bexpl;
754 Integer index_;
755 Option<tuple<DAE.Exp,Integer,Integer>> isExpisASUB;
756 DAE.ReductionInfo reductionInfo;
757 DAE.ReductionIterators iters;
758 DAE.CallAttributes attr;
759 DAE.Ident ident;
760 CrefExpTable derConst;
761 list<String> fields;
762 list<DAE.Dimension> dims;
763
764 // Note: Most of these functions check if a subexpression did a replacement.
765 // If it did not, we do not create a new copy of the expression (to save some memory).
766 case (DAE.CREF(componentRef = DAE.CREF_IDENT(ident=ident)),_)
767 guard
768 isIterationVar(inVariableReplacements, ident)
769 1998 then
770 (inExp,false);
771 case (DAE.CREF(componentRef = cr,ty = t),cond)
772 guard replaceExpCond(cond, inExp)
773 algorithm
774
2/2
✓ Branch 1 taken 2105909 times.
✓ Branch 2 taken 2821493 times.
4927402 if ComponentReferenceBasics.crefHasNoSubscripts(cr) then
775 2105909 c := false;
776 else
777 2821493 (cr,c) := replaceCrefSubs(cr,inVariableReplacements,cond);
778 end if;
779 try
780 4927402 e1 := getReplacement(inVariableReplacements, cr);
781 466246 e := avoidDoubleHashLookup(e1,t);
782 466246 c := true;
783 else
784 try
785 // only expand cref if dimensions are fixed
786 4461156 (_, dims) := TypesDump.flattenArrayType(t);
787
8/8
✓ Branch 0 taken 29569 times.
✓ Branch 1 taken 4461156 times.
✓ Branch 2 taken 29569 times.
✓ Branch 3 taken 4461156 times.
✓ Branch 5 taken 29387 times.
✓ Branch 6 taken 182 times.
✓ Branch 8 taken 182 times.
✓ Branch 9 taken 4460974 times.
4490725 true := List.none(list(Types.dimNotFixed(dim) for dim in dims), Util.id);
788
789
2/2
✓ Branch 1 taken 4312710 times.
✓ Branch 2 taken 148216 times.
4460974 true := hasExtendReplacement(inVariableReplacements, cr);
790
2/2
✓ Branch 1 taken 139936 times.
✓ Branch 2 taken 8280 times.
148216 (e2,true) := Expression.extendArrExp(inExp,false);
791 8280 (e,_) := replaceExp(e2,inVariableReplacements,cond);
792 8280 c := true;
793 else
794
2/2
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 4451958 times.
4452876 e := if c then DAE.CREF(cr,t) else inExp;
795 end try;
796 end try;
797 4927402 then
798 (e,c);
799 case (DAE.BINARY(exp1 = e1,operator = op,exp2 = e2),cond)
800 guard replaceExpCond(cond, inExp)
801 algorithm
802 3596940 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
803 3596940 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
804
8/8
✓ Branch 0 taken 3348393 times.
✓ Branch 1 taken 248547 times.
✓ Branch 2 taken 125760 times.
✓ Branch 3 taken 3222633 times.
✓ Branch 5 taken 3348393 times.
✓ Branch 6 taken 248547 times.
✓ Branch 7 taken 3222633 times.
✓ Branch 8 taken 125760 times.
6819573 then
805 (if c1 or c2 then DAE.BINARY(e1_1,op,e2_1) else inExp, c1 or c2);
806 case (DAE.LBINARY(exp1 = e1,operator = op,exp2 = e2),cond)
807 guard replaceExpCond(cond, inExp)
808 algorithm
809 33934 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
810 33934 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
811
8/8
✓ Branch 0 taken 29274 times.
✓ Branch 1 taken 4660 times.
✓ Branch 2 taken 415 times.
✓ Branch 3 taken 28859 times.
✓ Branch 5 taken 29274 times.
✓ Branch 6 taken 4660 times.
✓ Branch 7 taken 28859 times.
✓ Branch 8 taken 415 times.
62793 then
812 (if c1 or c2 then DAE.LBINARY(e1_1,op,e2_1) else inExp, c1 or c2);
813 case (DAE.UNARY(operator = op,exp = e1),cond)
814 guard replaceExpCond(cond, inExp)
815 algorithm
816 198687 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
817
2/2
✓ Branch 0 taken 15450 times.
✓ Branch 1 taken 183237 times.
198687 then
818 (if c1 then DAE.UNARY(op,e1_1) else inExp, c1);
819 case (DAE.LUNARY(operator = op,exp = e1),cond)
820 guard replaceExpCond(cond, inExp)
821 algorithm
822 7000 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
823
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 6811 times.
7000 then
824 (if c1 then DAE.LUNARY(op,e1_1) else inExp, c1);
825 case (DAE.RELATION(exp1 = e1,operator = op,exp2 = e2, index=index_, optionExpisASUB= isExpisASUB),cond)
826 algorithm
827 99296 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
828 99296 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
829
8/8
✓ Branch 0 taken 87492 times.
✓ Branch 1 taken 11804 times.
✓ Branch 2 taken 1539 times.
✓ Branch 3 taken 85953 times.
✓ Branch 5 taken 87492 times.
✓ Branch 6 taken 11804 times.
✓ Branch 7 taken 85953 times.
✓ Branch 8 taken 1539 times.
185249 then
830 (if c1 or c2 then DAE.RELATION(e1_1,op,e2_1,index_,isExpisASUB) else inExp, c1 or c2);
831 case (DAE.IFEXP(expCond = e1,expThen = e2,expElse = e3),cond)
832 guard replaceExpCond(cond, inExp)
833 algorithm
834 41926 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
835 41926 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
836 41926 (e3_1,c3) := replaceExp(e3, inVariableReplacements, cond);
837
12/12
✓ Branch 0 taken 38087 times.
✓ Branch 1 taken 3839 times.
✓ Branch 2 taken 37313 times.
✓ Branch 3 taken 774 times.
✓ Branch 4 taken 1353 times.
✓ Branch 5 taken 35960 times.
✓ Branch 7 taken 38087 times.
✓ Branch 8 taken 3839 times.
✓ Branch 9 taken 37313 times.
✓ Branch 10 taken 774 times.
✓ Branch 11 taken 35960 times.
✓ Branch 12 taken 1353 times.
77886 then
838 (if c1 or c2 or c3 then DAE.IFEXP(e1_1,e2_1,e3_1) else inExp, c1 or c2 or c3);
839 case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef = cr)}),cond)
840 algorithm
841 try
842
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 62237 times.
✓ Branch 2 taken 38443 times.
✓ Branch 3 taken 23794 times.
62237 SOME(derConst) := inVariableReplacements.derConst;
843
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2077 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2077 times.
23794 SOME(e) := UnorderedMap.getOrFail(cr, derConst);
844 2077 (e,_) := replaceExp(e, inVariableReplacements, cond);
845 2077 c := true;
846 else
847
1/2
✓ Branch 1 taken 60160 times.
✗ Branch 2 not taken.
60160 if replaceExpCond(cond, inExp) then
848 60160 (e,c) := replaceCallExp(inExp, inVariableReplacements, cond);
849 else
850 ✗ (e,c) := (inExp,false);
851 end if;
852 end try;
853 62237 then
854 (e,c);
855 case (DAE.CALL(),cond)
856 guard replaceExpCond(cond, inExp)
857 algorithm
858 237646 (e,c) := replaceCallExp(inExp, inVariableReplacements, cond);
859 then
860 (e,c);
861 case (DAE.RECORD(path, expl, fields, t), cond)
862 algorithm
863 try
864 // add all constant attribute bindings to the replacements
865 // partially fixes ticket #9036
866 41130 repl := addConstantRecordReplacements(t, expl, inVariableReplacements, inFuncTypeExpExpToBooleanOption);
867 41130 (expl, c) := replaceExpList(expl, repl, cond);
868 else
869 ✗ c := false;
870 end try;
871
2/2
✓ Branch 0 taken 8657 times.
✓ Branch 1 taken 32473 times.
41130 then
872 (if c then DAE.RECORD(path, expl, fields, t) else inExp, c);
873 // RATIONAL_CLOCK
874 case (DAE.CLKCONST(DAE.RATIONAL_CLOCK(intervalCounter=e, resolution=resolution)), cond)
875 algorithm
876 10 (e, c1) := replaceExp(e, inVariableReplacements, cond);
877 10 (resolution, c2) := replaceExp(resolution, inVariableReplacements, cond);
878
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10 c3 := c1 or c2;
879
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 then
880 (if c3 then DAE.CLKCONST(DAE.RATIONAL_CLOCK(e, resolution)) else inExp, c3);
881 // REAL_CLOCK
882 case (DAE.CLKCONST(DAE.REAL_CLOCK(interval=e)), cond)
883 algorithm
884 4 (e, c1) := replaceExp(e, inVariableReplacements, cond);
885
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 then
886 (if c1 then DAE.CLKCONST(DAE.REAL_CLOCK(e)) else inExp, c1);
887 // EVENT_CLOCK
888 case (DAE.CLKCONST(DAE.EVENT_CLOCK(condition=e, startInterval=startInterval)), cond)
889 algorithm
890 ✗ (e, c1) := replaceExp(e, inVariableReplacements, cond);
891 ✗ (startInterval, c2) := replaceExp(startInterval, inVariableReplacements, cond);
892 ✗ c3 := c1 or c2;
893 ✗ then
894 (if c3 then DAE.CLKCONST(DAE.EVENT_CLOCK(e, startInterval)) else inExp, c3);
895 // SOLVER_CLOCK
896 case (DAE.CLKCONST(DAE.SOLVER_CLOCK(c=e, solverMethod=solverMethod)), cond)
897 algorithm
898 ✗ (e, c1) := replaceExp(e, inVariableReplacements, cond);
899 ✗ (solverMethod, c2) := replaceExp(solverMethod, inVariableReplacements, cond);
900 ✗ c3 := c1 or c2;
901 ✗ then
902 (if c3 then DAE.CLKCONST(DAE.SOLVER_CLOCK(e, solverMethod)) else inExp, c3);
903
904 case (DAE.PARTEVALFUNCTION(path,expl,tp,t),cond)
905 guard replaceExpCond(cond, inExp)
906 algorithm
907 355 (expl_1,c1) := replaceExpList(expl, inVariableReplacements, cond);
908
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 317 times.
355 then
909 (if c1 then DAE.PARTEVALFUNCTION(path,expl_1,tp,t) else inExp, c1);
910 case (DAE.ARRAY(ty = tp,scalar = c,array = expl),cond)
911 guard replaceExpCond(cond, inExp)
912 algorithm
913 117422 (expl_1,c1) := replaceExpList(expl, inVariableReplacements, cond);
914
4/4
✓ Branch 0 taken 12044 times.
✓ Branch 1 taken 105378 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 11977 times.
117489 then
915 (if c1 then DAE.ARRAY(tp,c,expl_1) else inExp, c1);
916 case (DAE.MATRIX(ty = t,integer = b,matrix = bexpl),cond)
917 guard replaceExpCond(cond, inExp)
918 algorithm
919 25071 (bexpl_1,c1) := replaceExpMatrix(bexpl, inVariableReplacements, cond);
920
2/2
✓ Branch 0 taken 6094 times.
✓ Branch 1 taken 18977 times.
25071 then
921 (if c1 then DAE.MATRIX(t,b,bexpl_1) else inExp, c1);
922 case (DAE.RANGE(ty = tp,start = e1,step = NONE(),stop = e2),cond)
923 guard replaceExpCond(cond, inExp)
924 algorithm
925 854 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
926 854 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
927
6/8
✓ Branch 0 taken 854 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 735 times.
✓ Branch 5 taken 854 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 735 times.
✓ Branch 8 taken 119 times.
1589 then
928 (if c1 or c2 then DAE.RANGE(tp,e1_1,NONE(),e2_1) else inExp, c1 or c2);
929 case (DAE.RANGE(ty = tp,start = e1,step = SOME(e3),stop = e2),cond)
930 guard replaceExpCond(cond, inExp)
931 algorithm
932 6 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
933 6 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
934 6 (e3_1,c3) := replaceExp(e3, inVariableReplacements, cond);
935
6/12
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 6 times.
✗ Branch 12 not taken.
12 then
936 (if c1 or c2 or c3 then DAE.RANGE(tp,e1_1,SOME(e3_1),e2_1) else inExp, c1 or c2 or c3);
937 case (DAE.TUPLE(PR = expl),cond)
938 guard replaceExpCond(cond, inExp)
939 algorithm
940 2123 (expl_1,c1) := replaceExpList(expl, inVariableReplacements, cond);
941
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2115 times.
2123 then
942 (if c1 then DAE.TUPLE(expl_1) else inExp, c1);
943 case (DAE.CAST(ty = tp,exp = e1),cond)
944 guard replaceExpCond(cond, inExp)
945 algorithm
946 13572 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
947
2/2
✓ Branch 0 taken 4391 times.
✓ Branch 1 taken 9181 times.
13572 then
948 (if c1 then DAE.CAST(tp,e1_1) else inExp, c1);
949 case (DAE.ASUB(exp = e1,sub = subs),cond)
950 guard replaceExpCond(cond, inExp)
951 algorithm
952 try
953 29056 expl := List.map(subs, Expression.getSubscriptExp);
954 29056 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
955 29056 (expl,c2) := replaceExpList(expl, inVariableReplacements, cond);
956
4/4
✓ Branch 0 taken 22181 times.
✓ Branch 1 taken 6875 times.
✓ Branch 2 taken 21866 times.
✓ Branch 3 taken 315 times.
29056 true := c1 or c2;
957 7190 e := Expression.makeASUB(e1_1,expl);
958 7190 c := true;
959 else
960 21866 (e,c) := (inExp,false);
961 end try;
962 29056 then
963 (e,c);
964 case ((DAE.TSUB(exp = e1,ix = i, ty = tp)),cond)
965 guard replaceExpCond(cond, e1)
966 algorithm
967 97 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
968
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 80 times.
97 then
969 (if c1 then DAE.TSUB(e1_1,i,tp) else inExp, c1);
970 case ((DAE.RSUB(exp = e1,ix = i, fieldName = ident, ty = tp)),cond)
971 guard replaceExpCond(cond, e1)
972 algorithm
973 6373 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6373 times.
6373 then
975 (if c1 then DAE.RSUB(e1_1,i,ident,tp) else inExp, c1);
976 case (DAE.SIZE(exp = e1,sz = SOME(e2)),cond)
977 guard replaceExpCond(cond, inExp)
978 algorithm
979 81 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
980 81 (e2_1,c2) := replaceExp(e2, inVariableReplacements, cond);
981
6/8
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47 times.
✓ Branch 5 taken 47 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 47 times.
✗ Branch 8 not taken.
162 then
982 (if c1 or c2 then DAE.SIZE(e1_1,SOME(e2_1)) else inExp, c1 or c2);
983 case (DAE.CODE(code = a,ty = tp),_)
984 algorithm
985 ✗ print("replace_exp on CODE not impl.\n");
986 ✗ then
987 (DAE.CODE(a,tp),false);
988 case (DAE.REDUCTION(reductionInfo = reductionInfo,expr = e1,iterators = iters),cond)
989 guard replaceExpCond(cond, inExp)
990 algorithm
991 8 (e1_1,_) := replaceExp(e1, inVariableReplacements, cond);
992 8 (iters,c1) := replaceExpIters(iters, inVariableReplacements, cond);
993
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 then (if c1 then DAE.REDUCTION(reductionInfo,e1_1,iters) else inExp, c1);
994 case (DAE.BOX(exp = e1),cond)
995 guard replaceExpCond(cond, inExp)
996 algorithm
997 1021 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
998
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 862 times.
1021 then
999 (if c1 then DAE.BOX(e1_1) else inExp, c1);
1000 case (DAE.UNBOX(ty=tp, exp = e1),cond)
1001 guard replaceExpCond(cond, inExp)
1002 algorithm
1003 77 (e1_1,c1) := replaceExp(e1, inVariableReplacements, cond);
1004
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 8 times.
77 then
1005 (if c1 then DAE.UNBOX(e1_1,tp) else inExp, c1);
1006 else
1007 7051108 then (inExp,false);
1008 end match;
1009 end replaceExp;
1010
1011 protected function replaceCallExp
1012 "The call case of replaceExp: a partially applied function replaced for
1013 the called name contributes its bound arguments."
1014 input DAE.Exp inExp;
1015 input VariableReplacements inVariableReplacements;
1016 input Option<FuncTypeExp_ExpToBoolean> cond;
1017 output DAE.Exp outExp;
1018 output Boolean replacementPerformed;
1019 protected
1020 DAE.ComponentRef cr;
1021 DAE.Exp e1;
1022 Absyn.Path path;
1023 list<DAE.Exp> expl, expl_1;
1024 DAE.CallAttributes attr;
1025 algorithm
1026 try
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297806 times.
563145 DAE.CALL(path = path, expLst = expl, attr = attr) := inExp;
1028 297806 cr := ComponentReference.toExpCref(AbsynUtil.pathToCref(path));
1029
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 297805 times.
297806 if hasReplacement(inVariableReplacements,cr) then
1030 1 e1 := getReplacement(inVariableReplacements,cr);
1031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 DAE.PARTEVALFUNCTION(path=path,expList = expl_1) := e1;
1032 1 expl := listAppend(expl_1,expl);
1033 end if;
1034
2/2
✓ Branch 1 taken 265339 times.
✓ Branch 2 taken 32467 times.
297806 (expl_1,true) := replaceExpList(expl, inVariableReplacements, cond);
1035 32467 outExp := DAE.CALL(path,expl_1,attr);
1036 replacementPerformed := true;
1037 else
1038 outExp := inExp;
1039 replacementPerformed := false;
1040 end try;
1041 end replaceCallExp;
1042
1043 function addConstantRecordReplacements
1044 "adds replacements regarding constant elements of records.
1045 needs to always be done when other elements of records are replaced."
1046 input DAE.Type ty;
1047 input list<DAE.Exp> expl;
1048 input output VariableReplacements repl;
1049 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1050 protected
1051 DAE.ComponentRef cref;
1052 algorithm
1053 repl := match ty
1054 local
1055 DAE.Exp bind;
1056 Absyn.Path path;
1057
1058 case DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(path = path)) algorithm
1059
2/2
✓ Branch 0 taken 353007 times.
✓ Branch 1 taken 41130 times.
394137 for var in ty.varLst loop
1060 // only do something if there is a binding
1061
2/2
✓ Branch 1 taken 32437 times.
✓ Branch 2 taken 320570 times.
353007 if DAEUtil.isBound(var.binding) then
1062
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 32437 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32437 times.
32437 SOME(bind) := DAEUtil.bindingExp(var.binding);
1063 32437 cref := getRecordElement(var.name, expl, path);
1064 // only replace if the expression is const and the name was found
1065 // if replacement already happened the name might not be found -> no error!
1066
4/4
✓ Branch 1 taken 32397 times.
✓ Branch 2 taken 40 times.
✓ Branch 4 taken 407 times.
✓ Branch 5 taken 31990 times.
32437 if Expression.isConst(bind) and not ComponentReference.isWild(cref) then
1067 407 repl := addReplacement(repl, cref, bind, inFuncTypeExpExpToBooleanOption);
1068 end if;
1069 end if;
1070 end for;
1071 then repl;
1072 else repl;
1073 end match;
1074 end addConstantRecordReplacements;
1075
1076 function getRecordElement
1077 "takes an attribute name and the list of full attribute names.
1078 returns the cref of which the last ident matches the required name, and which
1079 is an element of a record of the given type. A record expression may also be
1080 built from the elements of a different record, and those are unrelated to this
1081 record's bindings even when a name matches."
1082 input DAE.Ident name;
1083 input list<DAE.Exp> expl;
1084 input Absyn.Path recordPath;
1085 output DAE.ComponentRef cref = DAE.WILD();
1086 algorithm
1087
2/2
✓ Branch 0 taken 1561146 times.
✓ Branch 1 taken 31997 times.
1593143 for e in expl loop
1088 () := match e
1089 case DAE.CREF()
1090 guard ComponentReferenceBasics.crefLastIdent(e.componentRef) == name and
1091 isElementOfRecord(e.componentRef, recordPath)
1092 algorithm
1093 440 cref := e.componentRef;
1094 440 return;
1095 then ();
1096 else ();
1097 end match;
1098 end for;
1099 end getRecordElement;
1100
1101 function isElementOfRecord
1102 "Whether the cref names an element of a record of the given type."
1103 input DAE.ComponentRef cref;
1104 input Absyn.Path recordPath;
1105 output Boolean res;
1106 protected
1107 Absyn.Path path;
1108 algorithm
1109 res := match cref
1110 case DAE.CREF_QUAL(componentRef = DAE.CREF_IDENT())
1111 algorithm
1112 res := match Types.arrayElementType(cref.identType)
1113 case DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(path = path))
1114 440 then AbsynUtil.pathEqual(path, recordPath);
1115 else false;
1116 end match;
1117 then res;
1118
1119 98 case DAE.CREF_QUAL() then isElementOfRecord(cref.componentRef, recordPath);
1120 else false;
1121 end match;
1122 end isElementOfRecord;
1123
1124 public function replaceCref"replaces a cref.
1125 author: Waurich TUD 2014-06"
1126 input DAE.ComponentRef crefIn;
1127 input VariableReplacements replIn;
1128 output DAE.Exp expOut;
1129 output Boolean changedOut;
1130 algorithm
1131 (expOut,changedOut) := match replIn
1132 local
1133 case _ guard hasReplacement(replIn,crefIn)
1134 algorithm
1135 ✗ expOut := getReplacement(replIn,crefIn);
1136 then (expOut,true);
1137 else
1138 algorithm
1139 ✗ expOut := DAE.CREF(crefIn,ComponentReference.crefType(crefIn));
1140 then (expOut,false);
1141 end match;
1142 end replaceCref;
1143
1144 protected function replaceCrefSubs
1145 input DAE.ComponentRef inCref;
1146 input VariableReplacements repl;
1147 input Option<FuncTypeExp_ExpToBoolean> cond;
1148 output DAE.ComponentRef outCr;
1149 output Boolean replacementPerformed;
1150 algorithm
1151 (outCr,replacementPerformed) := match inCref
1152 local
1153 String name;
1154 DAE.ComponentRef cr,cr_1;
1155 DAE.Type ty;
1156 list<DAE.Subscript> subs,subs_1;
1157 Boolean c1,c2;
1158
1159 case DAE.CREF_QUAL(ident = name, identType = ty, subscriptLst = subs, componentRef = cr)
1160 algorithm
1161 4630453 (subs_1, c1) := replaceCrefSubs2(subs, repl, cond);
1162 4630453 (cr_1, c2) := replaceCrefSubs(cr, repl, cond);
1163
1/2
✓ Branch 0 taken 4630453 times.
✗ Branch 1 not taken.
4630453 subs := if c1 then subs_1 else subs;
1164
2/2
✓ Branch 0 taken 4630449 times.
✓ Branch 1 taken 4 times.
4630453 cr := if c2 then cr_1 else cr;
1165
3/4
✓ Branch 0 taken 4630453 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4630449 times.
4630453 cr := if c1 or c2 then DAE.CREF_QUAL(name, ty, subs, cr) else inCref;
1166
3/4
✓ Branch 0 taken 4630453 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4630449 times.
✓ Branch 3 taken 4 times.
4630453 then
1167 (cr, c1 or c2);
1168
1169 case DAE.CREF_IDENT(ident = name, identType = ty, subscriptLst = subs)
1170 algorithm
1171 2821445 (subs, c1) := replaceCrefSubs2(subs, repl, cond);
1172
2/2
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 2820527 times.
2821445 cr := if c1 then DAE.CREF_IDENT(name, ty, subs) else inCref;
1173 2821445 then
1174 (cr, c1);
1175
1176 else (inCref,false);
1177 end match;
1178 end replaceCrefSubs;
1179
1180 protected function replaceCrefSubs2
1181 input list<DAE.Subscript> isubs;
1182 input VariableReplacements repl;
1183 input Option<FuncTypeExp_ExpToBoolean> cond;
1184 output list<DAE.Subscript> outSubs;
1185 output Boolean replacementPerformed = false;
1186 algorithm
1187
4/4
✓ Branch 0 taken 3476878 times.
✓ Branch 1 taken 7451898 times.
✓ Branch 2 taken 3476878 times.
✓ Branch 3 taken 7451898 times.
10928776 outSubs := list(match sub
1188 local
1189 DAE.Exp exp;
1190 Boolean c1;
1191 case DAE.WHOLEDIM()
1192 then sub;
1193 case DAE.SLICE(exp = exp)
1194 algorithm
1195 34 (exp,c1) := replaceExp(exp, repl, cond);
1196
3/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 2 times.
34 replacementPerformed := replacementPerformed or c1;
1197
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 32 times.
34 then if c1 then DAE.SLICE(exp) else sub;
1198 case DAE.INDEX(exp = exp)
1199 algorithm
1200 3476761 (exp,c1) := replaceExp(exp, repl, cond);
1201
4/4
✓ Branch 0 taken 3476760 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3475844 times.
✓ Branch 3 taken 916 times.
3476761 replacementPerformed := replacementPerformed or c1;
1202
2/2
✓ Branch 0 taken 917 times.
✓ Branch 1 taken 3475844 times.
3476761 then if c1 then DAE.INDEX(exp) else sub;
1203 case DAE.WHOLE_NONEXP(exp = exp)
1204 algorithm
1205 ✗ (exp,c1) := replaceExp(exp, repl, cond);
1206 ✗ replacementPerformed := replacementPerformed or c1;
1207 ✗ then if c1 then DAE.WHOLE_NONEXP(exp) else sub;
1208 end match for sub in isubs);
1209 end replaceCrefSubs2;
1210
1211 public function replaceExpList
1212 input list<DAE.Exp> iexpl;
1213 input VariableReplacements repl;
1214 input Option<FuncTypeExp_ExpToBoolean> cond;
1215 output list<DAE.Exp> outExpl;
1216 output Boolean replacementPerformed = false;
1217 protected
1218 DAE.Exp exp_;
1219 Boolean c;
1220 algorithm
1221
4/4
✓ Branch 0 taken 1617643 times.
✓ Branch 1 taken 563057 times.
✓ Branch 2 taken 1617643 times.
✓ Branch 3 taken 563057 times.
2180700 outExpl := list(match exp
1222 case _ algorithm
1223 1617643 (exp_,c) := replaceExp(exp,repl,cond);
1224
2/2
✓ Branch 0 taken 1466595 times.
✓ Branch 1 taken 151048 times.
1617643 if c then
1225 replacementPerformed := true;
1226 else
1227 exp_ := exp;
1228 end if;
1229 then exp_;
1230 end match for exp in iexpl);
1231 end replaceExpList;
1232
1233 public function replaceExpList1
1234 input list<DAE.Exp> iexpl;
1235 input VariableReplacements repl;
1236 input Option<FuncTypeExp_ExpToBoolean> cond;
1237 output list<DAE.Exp> outExpl;
1238 output list<Boolean> replacementPerformed;
1239 protected
1240 list<DAE.Exp> acc1 = {};
1241 list<Boolean> acc2 = {};
1242 Boolean c;
1243 algorithm
1244
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 173 times.
638 for exp in iexpl loop
1245 465 (exp,c) := replaceExp(exp,repl,cond);
1246
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 37 times.
465 acc2 := c::acc2;
1247 acc1 := exp::acc1;
1248 end for;
1249 173 outExpl := listReverseInPlace(acc1);
1250 173 replacementPerformed := listReverseInPlace(acc2);
1251 end replaceExpList1;
1252
1253
1254 protected function replaceExpIters
1255 input list<DAE.ReductionIterator> inIters;
1256 input VariableReplacements repl;
1257 input Option<FuncTypeExp_ExpToBoolean> cond;
1258 output list<DAE.ReductionIterator> outIter;
1259 output Boolean replacementPerformed = false;
1260 protected
1261 DAE.ReductionIterator it;
1262 algorithm
1263
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
16 outIter := list(match iter
1264 local
1265 String id;
1266 DAE.Exp exp,gexp;
1267 DAE.Type ty;
1268 Boolean b1,b2;
1269 case DAE.REDUCTIONITER(id,exp,NONE(),ty)
1270 algorithm
1271 8 (exp,b1) := replaceExp(exp, repl, cond);
1272
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if b1 then
1273 4 it := DAE.REDUCTIONITER(id,exp,NONE(),ty);
1274 replacementPerformed := true;
1275 else
1276 it := iter;
1277 end if;
1278 then it;
1279 case DAE.REDUCTIONITER(id,exp,SOME(gexp),ty)
1280 algorithm
1281 ✗ (exp,b1) := replaceExp(exp, repl, cond);
1282 ✗ (gexp,b2) := replaceExp(gexp, repl, cond);
1283 ✗ if b1 or b2 then
1284 ✗ it := DAE.REDUCTIONITER(id,exp,SOME(gexp),ty);
1285 ✗ replacementPerformed := true;
1286 else
1287 it := iter;
1288 end if;
1289 then it;
1290 else iter;
1291 end match for iter in inIters);
1292 end replaceExpIters;
1293
1294 protected function replaceExpCond "function replaceExpCond(cond,e) => true &
1295 Helper function to replace_Expression. Evaluates a condition function if
1296 SOME otherwise returns true."
1297 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1298 input DAE.Exp inExp;
1299 output Boolean outBoolean;
1300 algorithm
1301 outBoolean:=
1302 match (inFuncTypeExpExpToBooleanOption,inExp)
1303 local
1304 Boolean res;
1305 FuncTypeExp_ExpToBoolean cond;
1306 DAE.Exp e;
1307 case (SOME(cond),e) /* cond e */
1308 algorithm
1309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3169389 times.
3169389 res := cond(e);
1310 then
1311 res;
1312 else true;
1313 end match;
1314 end replaceExpCond;
1315
1316 protected function replaceExpMatrix "author: PA
1317 Helper function to replaceExp, traverses Matrix expression list."
1318 input list<list<DAE.Exp>> inTplExpExpBooleanLstLst;
1319 input VariableReplacements inVariableReplacements;
1320 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1321 output list<list<DAE.Exp>> outTplExpExpBooleanLstLst;
1322 output Boolean replacementPerformed = false;
1323
1324 protected
1325 list<DAE.Exp> exp_;
1326 Boolean c;
1327 algorithm
1328
4/4
✓ Branch 0 taken 75112 times.
✓ Branch 1 taken 25071 times.
✓ Branch 2 taken 75112 times.
✓ Branch 3 taken 25071 times.
100183 outTplExpExpBooleanLstLst := list(match exp
1329 case _ algorithm
1330 75112 (exp_,c) := replaceExpList(exp, inVariableReplacements, inFuncTypeExpExpToBooleanOption);
1331
2/2
✓ Branch 0 taken 57895 times.
✓ Branch 1 taken 17217 times.
75112 if c then
1332 replacementPerformed := true;
1333 else
1334 exp_ := exp;
1335 end if;
1336 then exp_;
1337 end match for exp in inTplExpExpBooleanLstLst);
1338 end replaceExpMatrix;
1339
1340 /*********************************************************/
1341 /* condition function for replace Expression */
1342 /*********************************************************/
1343
1344 public function skipPreOperator "The variable/exp in the pre operator should not be replaced.
1345 This function is passed to replace_exp to ensure this."
1346 input DAE.Exp inExp;
1347 output Boolean outBoolean;
1348 algorithm
1349 outBoolean := match inExp
1350 local String idn;
1351 case DAE.CALL(path = Absyn.IDENT(name = idn))
1352 guard idn=="pre" or idn=="previous" then false;
1353 else true;
1354 end match;
1355 end skipPreOperator;
1356
1357 public function skipPreChangeEdgeOperator "The variable/exp in the pre/change/edge operator should not be replaced.
1358 This function is passed to replace_exp to ensure this."
1359 input DAE.Exp inExp;
1360 output Boolean outBoolean;
1361 algorithm
1362 outBoolean := match inExp
1363 local
1364 DAE.ComponentRef cr;
1365 String idn;
1366 case DAE.CALL(path = Absyn.IDENT(name = idn),expLst = {DAE.CREF(componentRef=cr)})
1367 4781 guard idn=="pre" or idn=="previous" or idn=="change" or idn=="edge" then selfGeneratedVar(cr);
1368 case DAE.CALL(path = Absyn.IDENT(name = idn))
1369 guard idn=="pre" or idn=="previous" or idn=="change" or idn=="edge" then false;
1370 else true;
1371 end match;
1372 end skipPreChangeEdgeOperator;
1373
1374 protected function selfGeneratedVar
1375 input DAE.ComponentRef inCref;
1376 output Boolean b;
1377 algorithm
1378 b := match inCref
1379 local String idn;
1380 case DAE.CREF_QUAL(ident = idn) guard idn=="$ZERO" or idn=="$_DER" or idn=="$pDER" then true;
1381 // keep same a while untill we know which are needed
1382 //case DAE.CREF_QUAL(ident = "$DER") then true;
1383 else false;
1384 end match;
1385 end selfGeneratedVar;
1386
1387 /*********************************************************/
1388 /* replace Equations */
1389 /*********************************************************/
1390
1391 public function replaceEquationsArr
1392 "This function takes a list of equations ana a set of variable
1393 replacements and applies the replacements on all equations.
1394 The function returns the updated list of equations"
1395 input BackendDAE.EquationArray inEqns;
1396 input VariableReplacements repl;
1397 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1398 output BackendDAE.EquationArray outEqns;
1399 output Boolean replacementPerformed;
1400 algorithm
1401 (outEqns,replacementPerformed) := matchcontinue inFuncTypeExpExpToBooleanOption
1402 local
1403 list<BackendDAE.Equation> eqns;
1404 case _
1405 algorithm
1406 // Do not do empty replacements; it just takes time ;)
1407
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 37 times.
44 false := isReplacementEmpty(repl);
1408 37 (_,_,eqns,replacementPerformed) := BackendEquation.traverseEquationArray(inEqns,replaceEquationTraverser,(repl,inFuncTypeExpExpToBooleanOption,{},false));
1409
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 outEqns := if replacementPerformed then BackendEquation.listEquation(eqns) else inEqns;
1410 then
1411 (outEqns,replacementPerformed);
1412 else
1413 then
1414 (inEqns,false);
1415 end matchcontinue;
1416 end replaceEquationsArr;
1417
1418 protected function replaceEquationTraverser
1419 "Help function to e.g. removeSimpleEquations"
1420 input BackendDAE.Equation inEq;
1421 input tuple<VariableReplacements,Option<FuncTypeExp_ExpToBoolean>,list<BackendDAE.Equation>,Boolean> inTpl;
1422 output BackendDAE.Equation e;
1423 output tuple<VariableReplacements,Option<FuncTypeExp_ExpToBoolean>,list<BackendDAE.Equation>,Boolean> outTpl;
1424 protected
1425 VariableReplacements repl;
1426 Option<FuncTypeExp_ExpToBoolean> optfunc;
1427 list<BackendDAE.Equation> eqns;
1428 Boolean b;
1429 algorithm
1430 e := inEq;
1431 328 (repl,optfunc,eqns,b) := inTpl;
1432 328 (eqns,b) := replaceEquation(e,repl,optfunc,eqns,b);
1433
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 295 times.
361 outTpl := (repl,optfunc,eqns,b);
1434 end replaceEquationTraverser;
1435
1436 public function replaceEquations
1437 "This function takes a list of equations ana a set of variable
1438 replacements and applies the replacements on all equations.
1439 The function returns the updated list of equations"
1440 input list<BackendDAE.Equation> inEqns;
1441 input VariableReplacements repl;
1442 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1443 output list<BackendDAE.Equation> outEqns;
1444 output Boolean replacementPerformed;
1445 algorithm
1446
2/2
✓ Branch 1 taken 32754 times.
✓ Branch 2 taken 169175 times.
201929 if isReplacementEmpty(repl) then
1447 outEqns := inEqns;
1448 32754 replacementPerformed := false;
1449 else
1450 169175 (outEqns,replacementPerformed) := replaceEquations2(inEqns,repl,inFuncTypeExpExpToBooleanOption,{},false);
1451 if replacementPerformed and false /* Seems to break some modules */ then
1452 (outEqns,_) := BackendDAETransform.traverseBackendDAEExpsEqnLstWithSymbolicOperation(outEqns,BackendDAETransform.collapseArrayCrefExp,0/*dummy*/);
1453 end if;
1454 end if;
1455 end replaceEquations;
1456
1457 protected function replaceEquations2
1458 input list<BackendDAE.Equation> inBackendDAEEquationLst;
1459 input VariableReplacements inVariableReplacements;
1460 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1461 input list<BackendDAE.Equation> inAcc;
1462 input Boolean iReplacementPerformed;
1463 output list<BackendDAE.Equation> outBackendDAEEquationLst;
1464 output Boolean replacementPerformed;
1465 algorithm
1466 (outBackendDAEEquationLst,replacementPerformed) :=
1467 match inBackendDAEEquationLst
1468 local
1469 BackendDAE.Equation a;
1470 list<BackendDAE.Equation> es,acc;
1471 Boolean b;
1472 169759 case {} then (listReverse(inAcc),iReplacementPerformed);
1473 case a::es
1474 algorithm
1475 512078 (acc,b) := replaceEquation(a,inVariableReplacements,inFuncTypeExpExpToBooleanOption,inAcc,iReplacementPerformed);
1476 512078 (es,b) := replaceEquations2(es, inVariableReplacements,inFuncTypeExpExpToBooleanOption,acc,b);
1477 then
1478 (es,b);
1479 end match;
1480 end replaceEquations2;
1481
1482 protected function replaceEquation
1483 input BackendDAE.Equation inBackendDAEEquation;
1484 input VariableReplacements inVariableReplacements;
1485 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1486 input list<BackendDAE.Equation> inAcc;
1487 input Boolean iReplacementPerformed;
1488 output list<BackendDAE.Equation> outBackendDAEEquationLst;
1489 output Boolean replacementPerformed;
1490 algorithm
1491 (outBackendDAEEquationLst,replacementPerformed) :=
1492 matchcontinue (inBackendDAEEquation, inVariableReplacements)
1493 local
1494 DAE.Exp e1_1,e2_1,e1_2,e2_2,e1,e2,e_1,e_2,e;
1495 VariableReplacements repl;
1496 BackendDAE.Equation a;
1497 DAE.ComponentRef cr;
1498 Integer size;
1499 Option<Integer> recordSize;
1500 list<DAE.Exp> expl,expl1,expl2;
1501 BackendDAE.WhenEquation whenEqn,whenEqn1;
1502 DAE.ElementSource source;
1503 Boolean b1,b2,b3, hasArrayCref;
1504 list<Integer> dimSize;
1505 DAE.Algorithm alg;
1506 list<DAE.ComponentRef> crefs;
1507 list<DAE.Statement> stmts,stmts1;
1508 list<Boolean> blst;
1509 list<BackendDAE.Equation> eqns;
1510 list<list<BackendDAE.Equation>> eqnslst;
1511 DAE.Expand crefExpand;
1512 BackendDAE.EquationAttributes eqAttr;
1513
1514 case (BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=e1, right=e2, source=source, attr=eqAttr, recordSize=recordSize), repl)
1515 algorithm
1516 15267 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1517 15267 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1518
4/4
✓ Branch 0 taken 15127 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 13937 times.
✓ Branch 3 taken 1190 times.
15267 true := b1 or b2;
1519 1330 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_1);
1520 1330 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
1521
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1330 times.
1330 (DAE.EQUALITY_EXPS(e1_2,e2_2),source) := ExpressionSimplify.simplifyAddSymbolicOperation(DAE.EQUALITY_EXPS(e1_1,e2_1),source);
1522 1330 then
1523 (BackendDAE.ARRAY_EQUATION(dimSize,e1_2,e2_2,source,eqAttr,recordSize)::inAcc,true);
1524
1525 case (BackendDAE.COMPLEX_EQUATION(size=size, left=e1, right=e2, source=source, attr=eqAttr), repl)
1526 algorithm
1527 4938 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1528 4938 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1529
4/4
✓ Branch 0 taken 4748 times.
✓ Branch 1 taken 190 times.
✓ Branch 2 taken 4000 times.
✓ Branch 3 taken 748 times.
4938 true := b1 or b2;
1530 938 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_1);
1531 938 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
1532
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 938 times.
938 (DAE.EQUALITY_EXPS(e1_2,e2_2),source) := ExpressionSimplify.simplifyAddSymbolicOperation(DAE.EQUALITY_EXPS(e1_1,e2_1),source);
1533 938 then
1534 (BackendDAE.COMPLEX_EQUATION(size,e1_2,e2_2,source,eqAttr)::inAcc,true);
1535
1536 case (BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, attr=eqAttr), repl)
1537 algorithm
1538 482352 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1539 482352 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1540
4/4
✓ Branch 0 taken 462651 times.
✓ Branch 1 taken 19701 times.
✓ Branch 2 taken 419638 times.
✓ Branch 3 taken 43013 times.
482352 true := b1 or b2;
1541 62714 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_1);
1542 62714 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
1543
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 62714 times.
62714 (DAE.EQUALITY_EXPS(e1_2,e2_2),source) := ExpressionSimplify.simplifyAddSymbolicOperation(DAE.EQUALITY_EXPS(e1_1,e2_1),source);
1544 62714 then
1545 (BackendDAE.EQUATION(e1_2,e2_2,source,eqAttr)::inAcc,true);
1546
1547 case (BackendDAE.ALGORITHM(size=size, alg=DAE.ALGORITHM_STMTS(statementLst=stmts), source=source, expand=crefExpand, attr=eqAttr), repl)
1548 algorithm
1549 7532 crefs := Expression.getLhsCrefsFromStatements(stmts);
1550 // if there is no need for expanding the original equation, the replaced one shouldn't either
1551 7532 hasArrayCref := List.any(crefs,ComponentReference.isArrayElement);
1552
2/2
✓ Branch 0 taken 7354 times.
✓ Branch 1 taken 178 times.
7532 crefExpand := if hasArrayCref then crefExpand else DAE.NOT_EXPAND();
1553
1554
2/2
✓ Branch 1 taken 6265 times.
✓ Branch 2 taken 1267 times.
7532 (stmts1,true) := replaceStatementLst(stmts,repl,inFuncTypeExpExpToBooleanOption,{},false);
1555 1267 alg := DAE.ALGORITHM_STMTS(stmts1);
1556 // if all statements are removed, remove the whole algorithm
1557
1/2
✓ Branch 0 taken 1267 times.
✗ Branch 1 not taken.
1267 eqns := if not listEmpty(stmts1) then BackendDAE.ALGORITHM(size,alg,source,crefExpand,eqAttr)::inAcc else inAcc;
1558 then
1559 (eqns,true);
1560
1561 case (BackendDAE.SOLVED_EQUATION(componentRef=cr, exp=e, source=source, attr=eqAttr), repl)
1562 algorithm
1563
2/2
✓ Branch 1 taken 227 times.
✓ Branch 2 taken 69 times.
296 (e_1,true) := replaceExp(e, repl,inFuncTypeExpExpToBooleanOption);
1564 69 (e_2,_) := ExpressionSimplify.simplify(e_1);
1565 69 source := ElementSource.addSymbolicTransformationSubstitution(true,source,e,e_2);
1566 69 then
1567 (BackendDAE.SOLVED_EQUATION(cr,e_2,source,eqAttr)::inAcc,true);
1568
1569 case (BackendDAE.RESIDUAL_EQUATION(exp=e, source=source, attr=eqAttr), repl)
1570 algorithm
1571 ✗ (e_1,true) := replaceExp(e, repl,inFuncTypeExpExpToBooleanOption);
1572 ✗ (e_2,_) := ExpressionSimplify.simplify(e_1);
1573 ✗ source := ElementSource.addSymbolicTransformationSubstitution(true,source,e,e_2);
1574 ✗ then
1575 (BackendDAE.RESIDUAL_EQUATION(e_2,source,eqAttr)::inAcc,true);
1576
1577 case (BackendDAE.WHEN_EQUATION(size,whenEqn,source,eqAttr), repl)
1578 algorithm
1579
2/2
✓ Branch 1 taken 1778 times.
✓ Branch 2 taken 78 times.
1856 (whenEqn1,source,true) := replaceWhenEquation(whenEqn,repl,inFuncTypeExpExpToBooleanOption,source);
1580 78 then
1581 (BackendDAE.WHEN_EQUATION(size,whenEqn1,source,eqAttr)::inAcc,true);
1582
1583 case (BackendDAE.IF_EQUATION(conditions=expl, eqnstrue=eqnslst, eqnsfalse=eqns, source=source, attr=eqAttr), repl)
1584 algorithm
1585 155 (expl1,blst) := replaceExpList1(expl, repl, inFuncTypeExpExpToBooleanOption);
1586 155 b1 := List.any(blst, Util.id);
1587 155 source := ElementSource.addSymbolicTransformationSubstitutionLst(blst,source,expl,expl1);
1588 155 (expl2,blst) := ExpressionSimplify.condsimplifyList1(blst,expl1);
1589 155 source := ElementSource.addSymbolicTransformationSimplifyLst(blst,source,expl1,expl2);
1590 155 (eqnslst,b2) := List.map3Fold(eqnslst,replaceEquations2,repl,inFuncTypeExpExpToBooleanOption,{},false);
1591 155 (eqns,b3) := replaceEquations2(eqns,repl,inFuncTypeExpExpToBooleanOption,{},false);
1592
4/6
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 154 times.
✗ Branch 5 not taken.
155 true := b1 or b2 or b3;
1593 1 eqns := optimizeIfEquation(expl2,eqnslst,eqns,{},{},source,eqAttr,inAcc);
1594 then
1595 (eqns,true);
1596
1597 case (a, _) then (a::inAcc,iReplacementPerformed);
1598
1599 end matchcontinue;
1600 end replaceEquation;
1601
1602 protected function optimizeIfEquation
1603 input list<DAE.Exp> conditions;
1604 input list<list<BackendDAE.Equation>> theneqns;
1605 input list<BackendDAE.Equation> elseenqs;
1606 input list<DAE.Exp> conditions1;
1607 input list<list<BackendDAE.Equation>> theneqns1;
1608 input DAE.ElementSource source;
1609 input BackendDAE.EquationAttributes inEqAttr;
1610 input list<BackendDAE.Equation> inEqns;
1611 output list<BackendDAE.Equation> outEqns;
1612 algorithm
1613 outEqns := matchcontinue(conditions, theneqns, conditions1, theneqns1)
1614 local
1615 DAE.Exp e;
1616 list<DAE.Exp> explst;
1617 list<list<BackendDAE.Equation>> eqnslst;
1618 list<BackendDAE.Equation> eqns;
1619
1620 // no true case left with condition<>false
1621 case ({}, {}, {}, {})
1622 ✗ then
1623 listAppend(elseenqs,inEqns);
1624 // true case left with condition<>false
1625 case ({}, {}, _, _)
1626 algorithm
1627 ✗ explst := listReverse(conditions1);
1628 ✗ eqnslst := listReverse(theneqns1);
1629 ✗ then
1630 BackendDAE.IF_EQUATION(explst,eqnslst,elseenqs,source,inEqAttr)::inEqns;
1631 // if true use it if it is the first one
1632 case(DAE.BCONST(true)::_, eqns::_, {}, {})
1633 1 then
1634 listAppend(eqns,inEqns);
1635 // if true use it as new else if it is not the first one
1636 case(DAE.BCONST(true)::_, eqns::_, {}, {})
1637 algorithm
1638 ✗ explst := listReverse(conditions1);
1639 ✗ eqnslst := listReverse(theneqns1);
1640 ✗ then
1641 BackendDAE.IF_EQUATION(explst,eqnslst,eqns,source,inEqAttr)::inEqns;
1642 // if false skip it
1643 case(DAE.BCONST(false)::explst, _::eqnslst, _, _)
1644 ✗ then
1645 optimizeIfEquation(explst,eqnslst,elseenqs,conditions1,theneqns1,source,inEqAttr,inEqns);
1646 // all other cases
1647 case(e::explst, eqns::eqnslst, _, _)
1648 ✗ then
1649 optimizeIfEquation(explst,eqnslst,elseenqs,e::conditions1,eqns::theneqns1,source,inEqAttr,inEqns);
1650 end matchcontinue;
1651 end optimizeIfEquation;
1652
1653 protected function validWhenLeftHandSide
1654 input DAE.Exp inLhs;
1655 input DAE.Exp inRhs;
1656 input DAE.ComponentRef oldCr;
1657 output DAE.ComponentRef outCr;
1658 output DAE.Exp oRhs;
1659 algorithm
1660 (outCr,oRhs) := match inLhs
1661 local
1662 DAE.ComponentRef cr;
1663 DAE.Operator op;
1664 String msg;
1665 case DAE.CREF(componentRef=cr) then (cr,inRhs);
1666 ✗ case DAE.UNARY(operator=op,exp=DAE.CREF(componentRef=cr)) then (cr,DAE.UNARY(op,inRhs));
1667 ✗ case DAE.LUNARY(operator=op,exp=DAE.CREF(componentRef=cr)) then (cr,DAE.LUNARY(op,inRhs));
1668 else
1669 algorithm
1670 ✗ true := Flags.isSet(Flags.FAILTRACE);
1671 ✗ msg := "BackendVarTransform: failed to replace left hand side of when equation " +
1672 ComponentReferenceBasics.printComponentRefStr(oldCr) + " with " + ExpressionBasics.printExpStr(inLhs) + "\n";
1673 // print(msg + "\n");
1674 ✗ Debug.trace(msg);
1675 ✗ then
1676 fail();
1677 end match;
1678 end validWhenLeftHandSide;
1679
1680 protected function replaceWhenEquation "Replaces variables in a when equation"
1681 input BackendDAE.WhenEquation whenEqn;
1682 input VariableReplacements repl;
1683 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1684 input DAE.ElementSource isource;
1685 output BackendDAE.WhenEquation outWhenEqn;
1686 output DAE.ElementSource osource;
1687 output Boolean replacementPerformed;
1688
1689 algorithm
1690 (outWhenEqn,osource,replacementPerformed) :=
1691 match whenEqn
1692 local
1693 DAE.Exp cond,cond1,cond2;
1694 BackendDAE.WhenEquation weqn;
1695 Boolean b1,b2,b3,b4;
1696 DAE.ElementSource source;
1697 list<BackendDAE.WhenOperator> whenStmtLst;
1698 Option<BackendDAE.WhenEquation> oelsewhenPart;
1699 BackendDAE.WhenEquation elsewhenPart;
1700
1701 case BackendDAE.WHEN_STMTS(condition=cond,whenStmtLst=whenStmtLst,elsewhenPart=oelsewhenPart)
1702 algorithm
1703 1881 (cond1, b1) := replaceExp(cond, repl, inFuncTypeExpExpToBooleanOption);
1704 1881 (cond2, _) := ExpressionSimplify.condsimplify(b1, cond1);
1705 1881 source := ElementSource.addSymbolicTransformationSubstitution(b1, isource, cond, cond2);
1706 1881 (whenStmtLst,b2) := replaceWhenOperator(whenStmtLst, repl, inFuncTypeExpExpToBooleanOption, false, {});
1707
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1881 times.
✓ Branch 2 taken 1856 times.
✓ Branch 3 taken 25 times.
1881 if isSome(oelsewhenPart) then
1708 25 SOME(elsewhenPart) := oelsewhenPart;
1709 25 (elsewhenPart,source,b3) := replaceWhenEquation(elsewhenPart,repl,inFuncTypeExpExpToBooleanOption,source);
1710 oelsewhenPart := SOME(elsewhenPart);
1711 else
1712 oelsewhenPart := NONE();
1713 1856 b3 := false;
1714 end if;
1715
5/6
✓ Branch 0 taken 1831 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 1803 times.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1803 times.
1881 b4 := b1 or b2 or b3;
1716 78 weqn := if b4 then BackendDAE.WHEN_STMTS(cond2,whenStmtLst,oelsewhenPart) else whenEqn;
1717
1/2
✓ Branch 0 taken 1881 times.
✗ Branch 1 not taken.
1881 then (weqn,source,b4);
1718
1719 end match;
1720 end replaceWhenEquation;
1721
1722 protected function replaceWhenOperator
1723 "author: Frenkel TUD 2012-09"
1724 input list<BackendDAE.WhenOperator> inReinitStmtLst;
1725 input VariableReplacements repl;
1726 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1727 input Boolean replacementPerformed;
1728 input list<BackendDAE.WhenOperator> iAcc;
1729 output list<BackendDAE.WhenOperator> oReinitStmtLst;
1730 output Boolean oReplacementPerformed;
1731
1732 algorithm
1733 (oReinitStmtLst,oReplacementPerformed) :=
1734 match inReinitStmtLst
1735 local
1736 list<BackendDAE.WhenOperator> res,res1;
1737 BackendDAE.WhenOperator wop,wop1;
1738 DAE.Exp cond,cond1,level,cre,cre1,exp,exp1;
1739 DAE.ComponentRef cr,cr1;
1740 DAE.ElementSource source;
1741 Boolean b,b1,b2;
1742
1743 1881 case {} then (listReverse(iAcc),replacementPerformed);
1744
1745 case (wop as BackendDAE.ASSIGN(left=cre as DAE.CREF(componentRef = cr),right=exp,source=source))::res
1746 algorithm
1747 //cre = Expression.crefExp(cr);
1748 1790 (cre1,b1) := replaceExp(cre,repl,inFuncTypeExpExpToBooleanOption);
1749 1790 validWhenLeftHandSide(cre1,cre,cr);
1750 1790 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,cre,cre1);
1751 1790 (exp1,b2) := replaceExp(exp,repl,inFuncTypeExpExpToBooleanOption);
1752 1790 (exp1,_) := ExpressionSimplify.condsimplify(b2,exp1);
1753 1790 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,exp,exp1);
1754
3/4
✓ Branch 0 taken 1790 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1756 times.
✓ Branch 3 taken 34 times.
1790 b := b1 or b2;
1755
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 1756 times.
1790 wop1 := if b then BackendDAE.ASSIGN(cre1,exp1,source) else wop;
1756 1881 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1757 then
1758 (res1,b);
1759
1760 case (wop as BackendDAE.ASSIGN(left=cre, right=exp, source=source))::res
1761 algorithm
1762 //cre = Expression.crefExp(cr);
1763 22 (cre1,b1) := replaceExp(cre,repl,inFuncTypeExpExpToBooleanOption);
1764 22 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,cre,cre1);
1765 22 (exp1,b2) := replaceExp(exp,repl,inFuncTypeExpExpToBooleanOption);
1766 22 (exp1,_) := ExpressionSimplify.condsimplify(b2,exp1);
1767 22 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,exp,exp1);
1768
3/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 6 times.
22 b := b1 or b2;
1769
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
22 wop1 := if b then BackendDAE.ASSIGN(cre1,exp1,source) else wop;
1770 22 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1771 then
1772 (res1,b);
1773
1774 case (wop as BackendDAE.REINIT(stateVar=cr,value=cond,source=source))::res
1775 algorithm
1776 24 cre := Expression.crefExp(cr);
1777 24 (cre1,b1) := replaceExp(cre,repl,inFuncTypeExpExpToBooleanOption);
1778 24 (cr1,_) := validWhenLeftHandSide(cre1,cre,cr);
1779 24 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,cre,cre1);
1780 24 (cond1,b2) := replaceExp(cond,repl,inFuncTypeExpExpToBooleanOption);
1781 24 (cond1,_) := ExpressionSimplify.condsimplify(b2,cond1);
1782 24 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,cond,cond1);
1783
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 b := b1 or b2;
1784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 wop1 := if b then BackendDAE.REINIT(cr1,cond1,source) else wop;
1785 24 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1786 then
1787 (res1,b);
1788 case (wop as BackendDAE.ASSERT(condition=cond,message=exp,level=level,source=source))::res
1789 algorithm
1790 20 (cond1,b1) := replaceExp(cond,repl,inFuncTypeExpExpToBooleanOption);
1791 20 (cond1,_) := ExpressionSimplify.condsimplify(b1,cond1);
1792 20 (exp1,b2) := replaceExp(exp,repl,inFuncTypeExpExpToBooleanOption);
1793
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
20 b := b1 or b2;
1794 20 source := ElementSource.addSymbolicTransformationSubstitution(b,source,cond,cond1);
1795
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 19 times.
20 wop1 := if b then BackendDAE.ASSERT(cond1,exp1,level,source) else wop;
1796 20 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1797 then
1798 (res1,b);
1799 case (wop as BackendDAE.TERMINATE(message=exp,source=source))::res
1800 algorithm
1801 10 (exp1,b) := replaceExp(exp,repl,inFuncTypeExpExpToBooleanOption);
1802 10 source := ElementSource.addSymbolicTransformationSubstitution(b,source,exp,exp1);
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 wop1 := if b then BackendDAE.TERMINATE(exp1,source) else wop;
1804
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1805 then
1806 (res1,b);
1807 case (wop as BackendDAE.NORETCALL(exp=exp,source=source))::res
1808 algorithm
1809 15 (exp1,b) := replaceExp(exp,repl,inFuncTypeExpExpToBooleanOption);
1810 15 (exp1,_) := ExpressionSimplify.condsimplify(b,exp1);
1811 15 source := ElementSource.addSymbolicTransformationSubstitution(b,source,exp,exp1);
1812
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14 times.
15 wop1 := if b then BackendDAE.NORETCALL(exp1,source) else wop;
1813
3/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 1 time.
29 (res1,b) := replaceWhenOperator(res,repl,inFuncTypeExpExpToBooleanOption,replacementPerformed or b,wop1::iAcc);
1814 then
1815 (res1,b);
1816 end match;
1817 end replaceWhenOperator;
1818
1819 /*********************************************************/
1820 /* replace statements */
1821 /*********************************************************/
1822
1823 public function replaceStatementLst "
1824 function: replaceStatementLst
1825 perform replacements on statements.
1826 "
1827 input list<DAE.Statement> inStatementLst;
1828 input VariableReplacements inVariableReplacements;
1829 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1830 input list<DAE.Statement> inAcc;
1831 input Boolean inBAcc;
1832 output list<DAE.Statement> outStatementLst = {};
1833 output Boolean replacementPerformed = inBAcc;
1834
1835 protected
1836 VariableReplacements repl = inVariableReplacements;
1837 list<DAE.Statement> statementLst,statementLst_1;
1838 DAE.Statement statement,statement_1;
1839 DAE.Type type_;
1840 DAE.Exp e1_1,e2_1,e1,e2,e1_2,e2_2,e3,e3_1,e3_2;
1841 list<DAE.Exp> expExpLst,expExpLst_1;
1842 DAE.Else else_;
1843 DAE.ElementSource source;
1844 DAE.ComponentRef cr;
1845 Boolean iterIsArray;
1846 DAE.Ident ident;
1847 list<DAE.ComponentRef> conditions;
1848 Boolean initialCall;
1849 Boolean b1,b2,b3;
1850 list<tuple<DAE.ComponentRef,SourceInfo>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";
1851 list<tuple<DAE.ComponentRef, array<DAE.Exp>>> sub_iters;
1852 algorithm
1853
2/2
✓ Branch 0 taken 12587 times.
✓ Branch 1 taken 10140 times.
22727 for stmt in inStatementLst loop
1854 (outStatementLst, replacementPerformed) := matchcontinue stmt
1855 case DAE.STMT_ASSIGN(type_=type_,exp1=e1,exp=e2,source=source)
1856 algorithm
1857 3262 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1858 //isCon = Expression.isConst(e1_1);
1859 //e1_1 = if_(isCon,e1,e1_1);
1860 //cr = Expression.expCref(e1);
1861 //repl = Debug.bcallret3(isCon,removeReplacement,repl,cr,NONE(),repl);
1862 3262 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1863
4/4
✓ Branch 0 taken 3201 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 3166 times.
✓ Branch 3 taken 35 times.
3262 true := b1 or b2;
1864 96 (e1_2,_) := ExpressionSimplify.simplify(e1_1);
1865 96 (e2_2,_) := ExpressionSimplify.simplify(e2_1);
1866 96 (e1_2,e2_2) := moveNegateRhs(e1_2,e2_2);
1867 96 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_2);
1868 96 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_2);
1869 192 then
1870 (DAE.STMT_ASSIGN(type_,e1_2,e2_2,source) :: outStatementLst, true);
1871
1872 case DAE.STMT_TUPLE_ASSIGN(type_=type_,expExpLst=expExpLst,exp=e2,source=source)
1873 algorithm
1874 53 (expExpLst_1,b1) := replaceExpList(expExpLst,repl,inFuncTypeExpExpToBooleanOption);
1875 53 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1876
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 4 times.
53 true := b1 or b2;
1877 5 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
1878 5 (e2_2,b1) := ExpressionSimplify.simplify(e2_1);
1879 5 source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e2_1),DAE.PARTIAL_EQUATION(e2_2));
1880 10 then
1881 (DAE.STMT_TUPLE_ASSIGN(type_,expExpLst_1,e2_2,source) :: outStatementLst, true);
1882
1883 case DAE.STMT_ASSIGN_ARR(type_=type_, lhs = e1 as DAE.CREF(componentRef=cr),exp=e2,source=source)
1884 algorithm
1885 177 (e1_1,b1) := replaceExp(e1,repl,inFuncTypeExpExpToBooleanOption);
1886 177 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1887
3/4
✓ Branch 0 taken 177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 175 times.
✓ Branch 3 taken 2 times.
177 true := b1 or b2;
1888 2 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_1);
1889 2 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
1890
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 (DAE.EQUALITY_EXPS(e1_1,e2_2),source) := ExpressionSimplify.simplifyAddSymbolicOperation(DAE.EQUALITY_EXPS(e1_1,e2_1),source);
1891 2 then
1892 (validLhsArrayAssignSTMT(cr,e1_1,e2_2,type_,source,outStatementLst), true);
1893
1894 case DAE.STMT_IF(exp=e1,statementLst=statementLst,else_=else_,source=source)
1895 algorithm
1896 863 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1897 863 (e1_2,_) := ExpressionSimplify.condsimplify(b1,e1_1);
1898 863 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_2);
1899
4/4
✓ Branch 0 taken 862 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 848 times.
✓ Branch 3 taken 14 times.
1711 then
1900 replaceSTMT_IF(e1_2,statementLst,else_,source,repl,inFuncTypeExpExpToBooleanOption,outStatementLst,replacementPerformed or b1);
1901
1902 case DAE.STMT_FOR(type_=type_,iterIsArray=iterIsArray,iter=ident,range=e1,statementLst=statementLst,source=source,sub_iters=sub_iters)
1903 algorithm
1904 451 repl := addIterationVar(repl,ident);
1905 451 (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
1906 451 (e1_1,b2) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1907
4/4
✓ Branch 0 taken 436 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 360 times.
✓ Branch 3 taken 76 times.
451 true := b1 or b2;
1908 91 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e1,e1_1);
1909 91 (e1_2,b1) := ExpressionSimplify.condsimplify(b2,e1_1);
1910 91 source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1911 91 repl := removeIterationVar(repl,ident);
1912
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
273 then
1913 (DAE.STMT_FOR(type_,iterIsArray,ident,e1_2,statementLst_1,source,sub_iters) :: outStatementLst, true);
1914
1915 case DAE.STMT_PARFOR(type_=type_,iterIsArray=iterIsArray,iter=ident,range=e1,statementLst=statementLst,loopPrlVars=loopPrlVars,source=source)
1916 algorithm
1917 ✗ (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
1918 ✗ (e1_1,b2) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1919 ✗ true := b1 or b2;
1920 ✗ source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e1,e1_1);
1921 ✗ (e1_2,b1) := ExpressionSimplify.condsimplify(b2,e1_1);
1922 ✗ source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1923 ✗ then
1924 (DAE.STMT_PARFOR(type_,iterIsArray,ident,e1_2,statementLst_1,loopPrlVars,source) :: outStatementLst, true);
1925
1926 case DAE.STMT_WHILE(exp=e1,statementLst=statementLst,source=source)
1927 algorithm
1928 ✗ (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
1929 ✗ (e1_1,b2) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1930 ✗ true := b1 or b2;
1931 ✗ source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e1,e1_1);
1932 ✗ (e1_2,b1) := ExpressionSimplify.condsimplify(b2,e1_1);
1933 ✗ source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1934 ✗ then
1935 (DAE.STMT_WHILE(e1_2,statementLst_1,source) :: outStatementLst, true);
1936
1937 case DAE.STMT_WHEN(exp=e1,conditions=conditions,initialCall=initialCall,statementLst=statementLst,elseWhen=NONE(),source=source)
1938 algorithm
1939 370 (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
1940 370 (e1_1,b2) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1941
3/4
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 355 times.
✗ Branch 3 not taken.
370 true := b1 or b2;
1942 15 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e1,e1_1);
1943 15 (e1_2,b1) := ExpressionSimplify.condsimplify(b2,e1_1);
1944 15 source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1945
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
43 then
1946 (DAE.STMT_WHEN(e1_2,conditions,initialCall,statementLst_1,NONE(),source) :: outStatementLst, true);
1947
1948 case DAE.STMT_WHEN(exp=e1,conditions=conditions,initialCall=initialCall,statementLst=statementLst,elseWhen=SOME(statement),source=source)
1949 algorithm
1950 229 (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
1951
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 229 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 229 times.
457 (statement_1::{},b2) := replaceStatementLst({statement}, repl,inFuncTypeExpExpToBooleanOption,{},false);
1952 229 (e1_1,b3) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1953
4/6
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 228 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 228 times.
✗ Branch 5 not taken.
229 true := b1 or b2 or b3;
1954 1 source := ElementSource.addSymbolicTransformationSubstitution(b3,source,e1,e1_1);
1955 1 (e1_2,b1) := ExpressionSimplify.condsimplify(b3,e1_1);
1956 1 source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1957
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
3 then
1958 (DAE.STMT_WHEN(e1_2,conditions,initialCall,statementLst_1,SOME(statement_1),source) :: outStatementLst, true);
1959
1960 case DAE.STMT_ASSERT(cond=e1,msg=e2,level=e3,source=source)
1961 algorithm
1962 6879 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1963 6879 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1964 6879 (e3_1,b3) := replaceExp(e3, repl,inFuncTypeExpExpToBooleanOption);
1965
5/6
✓ Branch 0 taken 5716 times.
✓ Branch 1 taken 1163 times.
✓ Branch 2 taken 5700 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 5700 times.
✗ Branch 5 not taken.
6879 true := b1 or b2 or b3;
1966 1179 (e1_2,_) := ExpressionSimplify.condsimplify(b1,e1_1);
1967 1179 (e2_2,_) := ExpressionSimplify.condsimplify(b2,e2_1);
1968 1179 (e3_2,_) := ExpressionSimplify.condsimplify(b3,e3_1);
1969 1179 source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_2);
1970 1179 source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_2);
1971 1179 source := ElementSource.addSymbolicTransformationSubstitution(b3,source,e3,e3_2);
1972 2358 then
1973 (DAE.STMT_ASSERT(e1_2,e2_2,e3_2,source) :: outStatementLst, true);
1974
1975 case DAE.STMT_TERMINATE(msg=e1,source=source)
1976 algorithm
1977 ✗ (e1_1,true) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1978 ✗ source := ElementSource.addSymbolicTransformationSubstitution(true,source,e1,e1_1);
1979 ✗ (e1_2,b1) := ExpressionSimplify.simplify(e1_1);
1980 ✗ source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
1981 ✗ then
1982 (DAE.STMT_TERMINATE(e1_2,source) :: outStatementLst, true);
1983
1984 case DAE.STMT_REINIT(var=e1,value=e2,source=source)
1985 algorithm
1986 31 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1987 31 (e2_1,b2) := replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
1988
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 true := b1 or b2;
1989 ✗ (e1_2,_) := ExpressionSimplify.condsimplify(b1,e1_1);
1990 ✗ (e2_2,_) := ExpressionSimplify.condsimplify(b2,e2_1);
1991 ✗ source := ElementSource.addSymbolicTransformationSubstitution(b1,source,e1,e1_2);
1992 ✗ source := ElementSource.addSymbolicTransformationSubstitution(b2,source,e2,e2_2);
1993 ✗ then
1994 (DAE.STMT_REINIT(e1_2,e2_2,source) :: outStatementLst, true);
1995
1996 case DAE.STMT_NORETCALL(exp=e1,source=source)
1997 algorithm
1998
2/2
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 41 times.
209 (e1_1,true) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
1999 41 source := ElementSource.addSymbolicTransformationSubstitution(true,source,e1,e1_1);
2000 41 (e1_2,b1) := ExpressionSimplify.simplify(e1_1);
2001 41 source := ElementSource.addSymbolicTransformationSimplify(b1,source,DAE.PARTIAL_EQUATION(e1_1),DAE.PARTIAL_EQUATION(e1_2));
2002 82 then
2003 (DAE.STMT_NORETCALL(e1_2,source) :: outStatementLst, true);
2004
2005 // MetaModelica extension. KS
2006 case DAE.STMT_FAILURE(body=statementLst,source=source)
2007 algorithm
2008 ✗ (statementLst_1,true) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
2009 ✗ then
2010 (DAE.STMT_FAILURE(statementLst_1,source) :: outStatementLst, true);
2011
2012 10294 else (stmt :: outStatementLst, replacementPerformed);
2013 end matchcontinue;
2014 end for;
2015
2016 10140 outStatementLst := listReverseInPlace(outStatementLst);
2017 end replaceStatementLst;
2018
2019 public function replaceStatementLstRHS
2020 input list<DAE.Statement> inStatementLst;
2021 input VariableReplacements inVariableReplacements;
2022 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2023 input list<DAE.Statement> inAcc;
2024 input Boolean inBAcc;
2025 output list<DAE.Statement> outStatementLst;
2026 output Boolean replacementPerformed;
2027 partial function FuncTypeExp_ExpToBoolean
2028 input DAE.Exp inExp;
2029 output Boolean outBoolean;
2030 end FuncTypeExp_ExpToBoolean;
2031 algorithm
2032 ✗ (outStatementLst,(_,_,replacementPerformed)) := DAEUtil.traverseDAEEquationsStmtsRhsOnly(inStatementLst,replaceExpWrapper,(inVariableReplacements,inFuncTypeExpExpToBooleanOption,false));
2033 end replaceStatementLstRHS;
2034
2035 protected function replaceExpWrapper"to use replaceExp in DAEUtil.traverseDAEEquationsStmtsRhsOnly
2036 author: Waurich TUD 2014-4"
2037 input DAE.Exp inExp;
2038 input tuple<VariableReplacements,Option<FuncTypeExp_ExpToBoolean>,Boolean> inTpl;
2039 output DAE.Exp exp;
2040 output tuple<VariableReplacements,Option<FuncTypeExp_ExpToBoolean>,Boolean> tpl;
2041 partial function FuncTypeExp_ExpToBoolean
2042 input DAE.Exp inExp;
2043 output Boolean outBoolean;
2044 end FuncTypeExp_ExpToBoolean;
2045 protected
2046 Boolean b1,b2;
2047 VariableReplacements repl;
2048 Option<FuncTypeExp_ExpToBoolean> opt;
2049 algorithm
2050 exp := inExp;
2051 tpl := inTpl;
2052 ✗ (repl,opt,b1) := tpl;
2053 ✗ (exp,b2) := replaceExp(exp,repl,opt);
2054 ✗ b2 := b1 or b2;
2055 ✗ tpl := (repl,opt,b2);
2056 end replaceExpWrapper;
2057
2058 protected function moveNegateRhs
2059 input DAE.Exp inLhs;
2060 input DAE.Exp inRhs;
2061 output DAE.Exp outLhs;
2062 output DAE.Exp outRhs;
2063 algorithm
2064 (outLhs,outRhs) := match inLhs
2065 local
2066 DAE.Exp e;
2067 DAE.Type ty;
2068 ✗ case DAE.LUNARY(DAE.NOT(ty),e) then (e,DAE.LUNARY(DAE.NOT(ty),inRhs));
2069 1 case DAE.UNARY(DAE.UMINUS(ty),e) then (e,DAE.UNARY(DAE.UMINUS(ty),inRhs));
2070 ✗ case DAE.UNARY(DAE.UMINUS_ARR(ty),e) then (e,DAE.UNARY(DAE.UMINUS_ARR(ty),inRhs));
2071 case _ then (inLhs,inRhs);
2072 end match;
2073 end moveNegateRhs;
2074
2075 protected function validLhsArrayAssignSTMT "
2076 function: validLhsArrayAssignSTMT
2077 author Frenkel TUD 2012-11
2078 checks if the lhs is a variable or an array of variables."
2079 input DAE.ComponentRef oldCr;
2080 input DAE.Exp lhs;
2081 input DAE.Exp rhs;
2082 input DAE.Type type_;
2083 input DAE.ElementSource source;
2084 input list<DAE.Statement> inStatementLst;
2085 output list<DAE.Statement> outStatementLst;
2086 algorithm
2087 outStatementLst :=
2088 matchcontinue lhs
2089 local
2090 list<DAE.Statement> statementLst;
2091 list<DAE.Exp> elst,elst1;
2092 DAE.Type tp;
2093 DAE.Exp e, crefexp;
2094 list<Integer> ds;
2095 list<list<DAE.Subscript>> subslst;
2096 String msg;
2097 2 case crefexp then DAE.STMT_ASSIGN_ARR(type_,crefexp,rhs,source)::inStatementLst;
2098 ✗ case DAE.UNARY(DAE.UMINUS(tp),crefexp) then DAE.STMT_ASSIGN_ARR(type_,crefexp,DAE.UNARY(DAE.UMINUS(tp),rhs),source)::inStatementLst;
2099 ✗ case DAE.UNARY(DAE.UMINUS_ARR(tp),crefexp) then DAE.STMT_ASSIGN_ARR(type_,crefexp,DAE.UNARY(DAE.UMINUS_ARR(tp),rhs),source)::inStatementLst;
2100 ✗ case DAE.LUNARY(DAE.NOT(tp),crefexp) then DAE.STMT_ASSIGN_ARR(type_,crefexp,DAE.LUNARY(DAE.NOT(tp),rhs),source)::inStatementLst;
2101 case DAE.ARRAY(array=elst)
2102 algorithm
2103 ✗ ds := Expression.dimensionsSizes(Expression.arrayDimension(type_));
2104 ✗ subslst := Expression.dimensionSizesSubscripts(ds);
2105 ✗ subslst := Expression.rangesToSubscripts(subslst);
2106 ✗ elst1 := List.map1r(subslst,Expression.applyExpSubscripts,rhs);
2107 ✗ e := listHead(elst1);
2108 ✗ tp := Expression.typeof(e);
2109 ✗ statementLst := List.threadFold2(elst,elst1,validLhsAssignSTMT,tp,source,inStatementLst);
2110 then
2111 statementLst;
2112 else
2113 algorithm
2114 ✗ true := Flags.isSet(Flags.FAILTRACE);
2115 ✗ msg := "BackendVarTransform: failed to replace left hand side of array assign statement " +
2116 ComponentReferenceBasics.printComponentRefStr(oldCr) + " with " + ExpressionBasics.printExpStr(lhs) + "\n";
2117 // print(msg + "\n");
2118 ✗ Debug.trace(msg);
2119 ✗ then
2120 fail();
2121 end matchcontinue;
2122 end validLhsArrayAssignSTMT;
2123
2124 protected function validLhsAssignSTMT "
2125 function: validLhsAssignSTMT
2126 author Frenkel TUD 2012-11
2127 checks if the lhs is a variable or an array of variables."
2128 input DAE.Exp lhs;
2129 input DAE.Exp rhs;
2130 input DAE.Type type_;
2131 input DAE.ElementSource source;
2132 input list<DAE.Statement> inStatementLst;
2133 output list<DAE.Statement> outStatementLst;
2134 algorithm
2135 outStatementLst :=
2136 match lhs
2137 local DAE.Type tp;
2138 ✗ case DAE.CREF() then DAE.STMT_ASSIGN(type_,lhs,rhs,source)::inStatementLst;
2139 ✗ case DAE.UNARY(DAE.UMINUS(tp),DAE.CREF()) then DAE.STMT_ASSIGN(type_,lhs,DAE.UNARY(DAE.UMINUS(tp),rhs),source)::inStatementLst;
2140 ✗ case DAE.LUNARY(DAE.NOT(tp),DAE.CREF()) then DAE.STMT_ASSIGN(type_,lhs,DAE.LUNARY(DAE.NOT(tp),rhs),source)::inStatementLst;
2141 end match;
2142 end validLhsAssignSTMT;
2143
2144
2145 protected function replaceElse "
2146 Helper for replaceStatementLst.
2147 "
2148 input DAE.Else inElse;
2149 input VariableReplacements inVariableReplacements;
2150 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2151 output DAE.Else outElse;
2152 output Boolean replacementPerformed;
2153
2154 algorithm
2155 (outElse,replacementPerformed) := matchcontinue (inElse, inVariableReplacements)
2156 local
2157 VariableReplacements repl;
2158 list<DAE.Statement> statementLst,statementLst_1;
2159 DAE.Exp e1,e1_1,e1_2;
2160 DAE.Else else_,else_1;
2161 Boolean b1,b2;
2162 case (DAE.ELSEIF(exp=e1,statementLst=statementLst,else_=else_), repl)
2163 algorithm
2164 302 (e1_1,b1) := replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
2165 302 (e1_2,_) := ExpressionSimplify.condsimplify(b1,e1_1);
2166 302 (else_1,b2) := replaceElse1(e1_2,statementLst,else_,repl,inFuncTypeExpExpToBooleanOption);
2167
4/4
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 297 times.
✓ Branch 3 taken 1 time.
302 true := b1 or b2;
2168 then
2169 (else_1,true);
2170 case (DAE.ELSE(statementLst=statementLst), repl)
2171 algorithm
2172
2/2
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 1 time.
152 (statementLst_1,true) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
2173 1 then
2174 (DAE.ELSE(statementLst_1),true);
2175 else (inElse,false);
2176 end matchcontinue;
2177 end replaceElse;
2178
2179 protected function replaceElse1 "
2180 Helper for replaceStatementLst.
2181 "
2182 input DAE.Exp inExp;
2183 input list<DAE.Statement> inStatementLst;
2184 input DAE.Else inElse;
2185 input VariableReplacements inVariableReplacements;
2186 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2187 output DAE.Else outElse;
2188 output Boolean replacementPerformed;
2189
2190 algorithm
2191 (outElse,replacementPerformed) := matchcontinue (inExp, inStatementLst, inElse, inVariableReplacements)
2192 local
2193 VariableReplacements repl;
2194 list<DAE.Statement> statementLst,statementLst_1;
2195 DAE.Exp e1;
2196 DAE.Else else_,else_1;
2197 Boolean b1,b2;
2198 case (DAE.BCONST(true), statementLst, _, repl)
2199 algorithm
2200 ✗ (statementLst_1,_) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
2201 ✗ then
2202 (DAE.ELSE(statementLst_1),true);
2203 case (DAE.BCONST(false), _, else_, repl)
2204 algorithm
2205 ✗ (else_1,_) := replaceElse(else_, repl,inFuncTypeExpExpToBooleanOption);
2206 then
2207 (else_1,true);
2208 case (e1, statementLst, else_, repl)
2209 algorithm
2210 302 (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
2211 302 (else_1,b2) := replaceElse(else_,repl,inFuncTypeExpExpToBooleanOption);
2212
4/4
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 299 times.
✓ Branch 3 taken 2 times.
302 true := b1 or b2;
2213 3 then
2214 (DAE.ELSEIF(e1,statementLst_1,else_1),true);
2215 case (e1, statementLst, else_, _)
2216 299 then
2217 (DAE.ELSEIF(e1,statementLst,else_),false);
2218 end matchcontinue;
2219 end replaceElse1;
2220
2221 protected function replaceSTMT_IF
2222 input DAE.Exp inExp;
2223 input list<DAE.Statement> inStatementLst;
2224 input DAE.Else inElse;
2225 input DAE.ElementSource inSource;
2226 input VariableReplacements inVariableReplacements;
2227 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2228 input list<DAE.Statement> inAcc;
2229 input Boolean inBAcc;
2230 output list<DAE.Statement> outStatementLst;
2231 output Boolean replacementPerformed;
2232
2233 algorithm
2234 (outStatementLst,replacementPerformed) :=
2235 matchcontinue (inExp, inStatementLst, inElse, inSource, inVariableReplacements)
2236 local
2237 DAE.Exp exp,exp_e;
2238 list<DAE.Statement> statementLst,statementLst_e,statementLst_1;
2239 DAE.Else else_,else_e,else_1;
2240 DAE.ElementSource source;
2241 VariableReplacements repl;
2242 Boolean b1,b2;
2243 case (DAE.BCONST(true), statementLst, _, _, repl)
2244 ✗ then replaceStatementLst(statementLst, repl, inFuncTypeExpExpToBooleanOption, inAcc, true);
2245 case (DAE.BCONST(false), _, DAE.NOELSE(), _, _)
2246 ✗ then (inAcc, true);
2247 case (DAE.BCONST(false), _, DAE.ELSEIF(exp=exp_e,statementLst=statementLst_e,else_=else_e), source, repl)
2248 ✗ then replaceSTMT_IF(exp_e,statementLst_e,else_e,source,repl,inFuncTypeExpExpToBooleanOption,inAcc,true);
2249 case (DAE.BCONST(false), _, DAE.ELSE(statementLst=statementLst_e), _, repl)
2250 ✗ then replaceStatementLst(statementLst_e, repl, inFuncTypeExpExpToBooleanOption, inAcc, true);
2251 case (exp, statementLst, else_, source, repl)
2252 algorithm
2253 863 (statementLst_1,b1) := replaceStatementLst(statementLst, repl,inFuncTypeExpExpToBooleanOption,{},false);
2254 863 (else_1,b2) := replaceElse(else_,repl,inFuncTypeExpExpToBooleanOption);
2255
4/4
✓ Branch 0 taken 817 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 815 times.
✓ Branch 3 taken 2 times.
863 true := b1 or b2;
2256 96 then
2257 (DAE.STMT_IF(exp,statementLst_1,else_1,source) :: inAcc, true);
2258 case (exp, statementLst, else_, source, _)
2259 1630 then (DAE.STMT_IF(exp,statementLst,else_,source) :: inAcc, inBAcc);
2260 end matchcontinue;
2261 end replaceSTMT_IF;
2262
2263 /*********************************************************/
2264 /* variable replacements */
2265 /*********************************************************/
2266
2267 public function replaceVarTraverser "author: Frenkel TUD 2011-03"
2268 input BackendDAE.Var inVar;
2269 input VariableReplacements inRepl;
2270 output BackendDAE.Var outVar;
2271 output VariableReplacements repl = inRepl;
2272 algorithm
2273 142235 outVar := replaceBindingExp(inVar, inRepl);
2274 142235 outVar := replaceVariableAttributesInVar(outVar, inRepl);
2275 end replaceVarTraverser;
2276
2277 public function replaceBindingExp
2278 input BackendDAE.Var varIn;
2279 input VariableReplacements repl;
2280 output BackendDAE.Var varOut;
2281 algorithm
2282 varOut := match varIn
2283 local
2284 DAE.Exp exp;
2285 case BackendDAE.VAR(bindExp=SOME(exp))
2286 algorithm
2287 141153 exp := replaceExp(exp,repl,NONE());
2288 141153 (exp,_) := ExpressionSimplify.simplify(exp);
2289 141153 then BackendVariable.setBindExp(varIn,SOME(exp));
2290 case BackendDAE.VAR(bindExp=NONE())
2291 then varIn;
2292 end match;
2293 end replaceBindingExp;
2294
2295 public function replaceVariableAttributes
2296 input DAE.VariableAttributes attrIn;
2297 input VariableReplacements repl;
2298 output DAE.VariableAttributes attrOut;
2299 algorithm
2300 attrOut := match attrIn
2301 local
2302 Option<DAE.Exp> quantity "quantity";
2303 Option<DAE.Exp> unit "unit";
2304 Option<DAE.Exp> displayUnit "displayUnit";
2305 Option<DAE.Exp> min;
2306 Option<DAE.Exp> max;
2307 Option<DAE.Exp> start "start value";
2308 Option<DAE.Exp> fixed "fixed - true: default for parameter/constant, false - default for other variables";
2309 Option<DAE.Exp> nominal "nominal";
2310 Option<DAE.StateSelect> stateSelectOption;
2311 Option<DAE.Uncertainty> uncertainOption;
2312 Option<DAE.Distribution> distributionOption;
2313 Option<DAE.Exp> equationBound;
2314 Option<Boolean> isProtected;
2315 Option<Boolean> finalPrefix;
2316 Option<DAE.StartOrigin> startOrigin;
2317 case DAE.VAR_ATTR_REAL(quantity,unit,displayUnit,min,max,start,fixed,nominal,stateSelectOption,uncertainOption,distributionOption,
2318 equationBound,isProtected,finalPrefix,startOrigin)
2319 algorithm
2320 120286 quantity := replaceOptionExp(quantity,repl);
2321 120286 unit := replaceOptionExp(unit,repl);
2322 120286 displayUnit := replaceOptionExp(displayUnit,repl);
2323 120286 min := replaceOptionExp(min,repl);
2324 120286 max := replaceOptionExp(max,repl);
2325 120286 start := replaceOptionExp(start,repl);
2326 120286 fixed := replaceOptionExp(fixed,repl);
2327 120286 nominal := replaceOptionExp(nominal,repl);
2328 120286 equationBound := replaceOptionExp(equationBound,repl);
2329 120286 then DAE.VAR_ATTR_REAL(quantity,unit,displayUnit,min,max,start,fixed,nominal,stateSelectOption,uncertainOption,distributionOption,
2330 equationBound,isProtected,finalPrefix,startOrigin);
2331
2332 case DAE.VAR_ATTR_INT(quantity,min,max,start,fixed,uncertainOption,distributionOption,
2333 equationBound,isProtected,finalPrefix,startOrigin)
2334 algorithm
2335 7350 quantity := replaceOptionExp(quantity,repl);
2336 7350 min := replaceOptionExp(min,repl);
2337 7350 max := replaceOptionExp(max,repl);
2338 7350 start := replaceOptionExp(start,repl);
2339 7350 fixed := replaceOptionExp(fixed,repl);
2340 7350 equationBound := replaceOptionExp(equationBound,repl);
2341 7350 then DAE.VAR_ATTR_INT(quantity,min,max,start,fixed,uncertainOption,distributionOption,
2342 equationBound,isProtected,finalPrefix,startOrigin);
2343
2344 case DAE.VAR_ATTR_BOOL(quantity,start,fixed,equationBound,isProtected,finalPrefix,startOrigin)
2345 algorithm
2346 11467 quantity := replaceOptionExp(quantity,repl);
2347 11467 start := replaceOptionExp(start,repl);
2348 11467 fixed := replaceOptionExp(fixed,repl);
2349 11467 equationBound := replaceOptionExp(equationBound,repl);
2350 11467 then DAE.VAR_ATTR_BOOL(quantity,start,fixed,equationBound,isProtected,finalPrefix,startOrigin);
2351
2352 case DAE.VAR_ATTR_STRING(quantity,start,fixed,equationBound,isProtected,finalPrefix,startOrigin)
2353 algorithm
2354 1363 quantity := replaceOptionExp(quantity,repl);
2355 1363 start := replaceOptionExp(start,repl);
2356 1363 fixed := replaceOptionExp(fixed,repl);
2357 1363 equationBound := replaceOptionExp(equationBound,repl);
2358 1363 then DAE.VAR_ATTR_STRING(quantity,start,fixed,equationBound,isProtected,finalPrefix,startOrigin);
2359
2360 case DAE.VAR_ATTR_ENUMERATION(quantity,min,max,start,fixed,equationBound,isProtected,finalPrefix,startOrigin)
2361 algorithm
2362 1622 quantity := replaceOptionExp(quantity,repl);
2363 1622 min := replaceOptionExp(min,repl);
2364 1622 max := replaceOptionExp(max,repl);
2365 1622 start := replaceOptionExp(start,repl);
2366 1622 fixed := replaceOptionExp(fixed,repl);
2367 1622 equationBound := replaceOptionExp(equationBound,repl);
2368 1622 then DAE.VAR_ATTR_ENUMERATION(quantity,min,max,start,fixed,equationBound,isProtected,finalPrefix,startOrigin);
2369
2370 else
2371 then attrIn;
2372 end match;
2373 end replaceVariableAttributes;
2374
2375 public function replaceOptionExp"replaces the exp inside an option"
2376 input Option<DAE.Exp> optIn;
2377 input VariableReplacements repl;
2378 output Option<DAE.Exp> optOut;
2379 protected
2380 DAE.Exp exp;
2381 algorithm
2382
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1187726 times.
✓ Branch 2 taken 232146 times.
✓ Branch 3 taken 955580 times.
1187726 if isSome(optIn) then
2383 232146 exp := Util.getOption(optIn);
2384 232146 exp := replaceExp(exp,repl,NONE());
2385 optOut := SOME(exp);
2386 else
2387 optOut := NONE();
2388 end if;
2389 end replaceOptionExp;
2390
2391 public function replaceVariableAttributesInVar
2392 input BackendDAE.Var varIn;
2393 input VariableReplacements repl;
2394 output BackendDAE.Var varOut;
2395 algorithm
2396 varOut := match varIn
2397 local
2398 DAE.VariableAttributes values;
2399 case BackendDAE.VAR(values=SOME(values))
2400 algorithm
2401 142088 values := replaceVariableAttributes(values,repl);
2402 142088 then BackendVariable.setVarAttributes(varIn,SOME(values));
2403 else
2404 then varIn;
2405 end match;
2406 end replaceVariableAttributesInVar;
2407
2408 protected function negateOperator
2409 "makes an add out of a sub and a sub out of an add."
2410 input DAE.Operator inOp;
2411 output DAE.Operator outOp;
2412 algorithm
2413 outOp:= match inOp
2414 local
2415 DAE.Type ty;
2416 ✗ case DAE.UMINUS(ty=ty) then DAE.ADD(ty);
2417 ✗ case DAE.SUB(ty=ty) then DAE.ADD(ty);
2418 ✗ case DAE.ADD(ty=ty) then DAE.SUB(ty);
2419 else inOp;
2420 end match;
2421 end negateOperator;
2422
2423 public function replaceEventInfo
2424 input BackendDAE.EventInfo eInfoIn;
2425 input VariableReplacements inVariableReplacements;
2426 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2427 output BackendDAE.EventInfo eInfoOut;
2428
2429 protected
2430 Integer numberMathEvents;
2431 list<BackendDAE.TimeEvent> timeEvents;
2432 BackendDAE.ZeroCrossingSet zeroCrossingLst, sampleLst;
2433 BackendDAE.ZeroCrossingSet relationsLst;
2434 protected partial function Func
2435 input output BackendDAE.ZeroCrossing zc;
2436 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2437 end Func;
2438 Func zc;
2439 algorithm
2440 ✗ BackendDAE.EVENT_INFO(timeEvents, zeroCrossingLst, relationsLst, sampleLst, numberMathEvents) := eInfoIn;
2441 ✗ timeEvents := List.map2(timeEvents, replaceTimeEvents, inVariableReplacements, inFuncTypeExpExpToBooleanOption);
2442 ✗ zc := function replaceZeroCrossing(inVariableReplacements=inVariableReplacements);
2443 ✗ DoubleEnded.mapNoCopy_1(zeroCrossingLst.zc, zc, inFuncTypeExpExpToBooleanOption);
2444 ✗ DoubleEnded.mapNoCopy_1(sampleLst.zc, zc, inFuncTypeExpExpToBooleanOption);
2445 ✗ DoubleEnded.mapNoCopy_1(relationsLst.zc, zc, inFuncTypeExpExpToBooleanOption);
2446 ✗ eInfoOut := BackendDAE.EVENT_INFO(timeEvents,zeroCrossingLst,relationsLst,sampleLst,numberMathEvents);
2447 end replaceEventInfo;
2448
2449 protected function replaceTimeEvents
2450 input BackendDAE.TimeEvent teIn;
2451 input VariableReplacements inVariableReplacements;
2452 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2453 output BackendDAE.TimeEvent teOut;
2454
2455 algorithm
2456 teOut := match teIn
2457 local
2458 Integer index;
2459 DAE.Exp startExp, intervalExp;
2460 case BackendDAE.SAMPLE_TIME_EVENT(index=index, startExp=startExp, intervalExp=intervalExp)
2461 algorithm
2462 ✗ (startExp,_) := replaceExp(startExp,inVariableReplacements,inFuncTypeExpExpToBooleanOption);
2463 ✗ (intervalExp,_) := replaceExp(intervalExp,inVariableReplacements,inFuncTypeExpExpToBooleanOption);
2464 ✗ then BackendDAE.SAMPLE_TIME_EVENT(index, startExp, intervalExp, teIn.iter);
2465 else
2466 then teIn;
2467 end match;
2468 end replaceTimeEvents;
2469
2470 protected function replaceZeroCrossing"replaces the exp in the BackendDAE.ZeroCrossing"
2471 input BackendDAE.ZeroCrossing zcIn;
2472 input VariableReplacements inVariableReplacements;
2473 input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
2474 output BackendDAE.ZeroCrossing zcOut;
2475
2476 algorithm
2477 zcOut := match zcIn
2478 local
2479 DAE.Exp relation_;
2480 case BackendDAE.ZERO_CROSSING(relation_ = relation_)
2481 algorithm
2482 ✗ (relation_,_) := replaceExp(relation_,inVariableReplacements,inFuncTypeExpExpToBooleanOption);
2483 ✗ then BackendDAE.ZERO_CROSSING(zcIn.index, relation_, zcIn.occurEquLst, zcIn.iter);
2484 else
2485 then zcIn;
2486 end match;
2487 end replaceZeroCrossing;
2488
2489
2490 /*********************************************************/
2491 /* dump replacements */
2492 /*********************************************************/
2493
2494 public function dumpReplacements "Prints the variable replacements on form var1 -> var2"
2495 input VariableReplacements repl;
2496 protected
2497 list<DAE.ComponentRef> srcs;
2498 list<DAE.Exp> dsts;
2499 list<tuple<DAE.ComponentRef,DAE.Exp>> tplLst;
2500 algorithm
2501 50 (srcs, dsts) := getAllReplacements(repl);
2502 50 tplLst := List.zip(srcs, dsts);
2503 50 print("\nReplacements: (");
2504 50 print(String(listLength(tplLst)));
2505 50 print(")\n");
2506 50 print("========================================\n");
2507 50 print(stringDelimitList(List.map(tplLst,printReplacementTupleStr), "\n"));
2508 50 print("\n");
2509 end dumpReplacements;
2510
2511 public function dumpExtendReplacements
2512 "Prints the variable extendreplacements on form var1 -> var2.
2513 Sorted by name: the set is a hash set, so its traversal order depends on the
2514 width of Integer and would differ between platforms and compiler builds."
2515 input VariableReplacements repl;
2516 protected
2517 list<String> names;
2518 algorithm
2519
4/4
✓ Branch 1 taken 185 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 185 times.
✓ Branch 4 taken 2 times.
187 names := list(ComponentReferenceBasics.printComponentRefStr(c) for c in UnorderedSet.toList(repl.extendhashTable));
2520 2 names := List.sort(names, Util.strcmpBool);
2521 2 print("\nExtendReplacements: (");
2522 2 print(String(listLength(names)));
2523 2 print(")\n");
2524 2 print("========================================\n");
2525 2 print(stringDelimitList(names, "\n"));
2526 2 print("\n");
2527 end dumpExtendReplacements;
2528
2529 public function dumpDerConstReplacements
2530 "Prints the variable derConst replacements on form var1 -> exp"
2531 input VariableReplacements repl;
2532 protected
2533 list<DAE.ComponentRef> srcs;
2534 list<DAE.Exp> dsts;
2535 list<tuple<DAE.ComponentRef,DAE.Exp>> tplLst;
2536 algorithm
2537
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
2 if isSome(repl.derConst) then
2538 1 (srcs, dsts) := getCrefExpTableEntries(Util.getOption(repl.derConst));
2539 1 tplLst := List.zip(srcs, dsts);
2540 1 print("\nDerConstReplacements: (");
2541 1 print(String(listLength(tplLst)));
2542 1 print(")\n");
2543 1 print("========================================\n");
2544 1 print(stringDelimitList(List.map(tplLst,printReplacementTupleStr), "\n"));
2545 1 print("\n");
2546 end if;
2547 end dumpDerConstReplacements;
2548
2549 protected function printReplacementTupleStr "help function to dumpReplacements"
2550 input tuple<DAE.ComponentRef,DAE.Exp> tpl;
2551 output String str;
2552 algorithm
2553 // optional exteded type debugging
2554 //str := ComponentReference.debugPrintComponentRefTypeStr(Util.tuple21(tpl)) + " -> " + ExpressionDump.debugPrintComponentRefExp(Util.tuple22(tpl));
2555 // Normal debugging, without type&dimension information on crefs.
2556 554 str := ComponentReferenceBasics.printComponentRefStr(Util.tuple21(tpl)) + " -> " + ExpressionBasics.printExpStr(Util.tuple22(tpl));
2557 end printReplacementTupleStr;
2558
2559 public function getConstantReplacements"gets a clean replacement set containing only constant replacement rules"
2560 input VariableReplacements replIn;
2561 output VariableReplacements replOut;
2562 protected
2563 list<DAE.ComponentRef> crefs;
2564 list<DAE.Exp> exps;
2565 algorithm
2566 ✗ (crefs,exps) := getAllReplacements(replIn);
2567 ✗ (exps,crefs):= List.filterOnTrueSync(exps,Expression.isEvaluatedConst,crefs);
2568 ✗ replOut := emptyReplacements();
2569 ✗ replOut := addReplacements(replOut,crefs,exps,NONE());
2570 end getConstantReplacements;
2571
2572 annotation(__OpenModelica_Interface="backend");
2573 end BackendVarTransform;
2574