/
Inline.mo
2174 lines (2050 loc) · 74.8 KB
/
Inline.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
// stefan
encapsulated package Inline
" file: Inline.mo
package: Inline
description: inline functions
RCS: $Id$
This module contains data structures and functions for inline functions.
The entry point is the inlineCalls function, or inlineCallsInFunctions
"
public import Absyn;
public import Algorithm;
public import BackendDAE;
public import BaseHashTable;
public import DAE;
public import Env;
public import HashTableCG;
public import SCode;
public import Util;
public import Values;
type Ident = String;
public type Functiontuple = tuple<Option<DAE.FunctionTree>,list<DAE.InlineType>>;
protected import ComponentReference;
protected import Config;
protected import DAEUtil;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import List;
protected import Types;
protected import VarTransform;
public function inlineCalls
"function: inlineCalls
searches for calls where the inline flag is true, and inlines them"
input list<DAE.InlineType> inITLst;
input BackendDAE.BackendDAE inBackendDAE;
output BackendDAE.BackendDAE outBackendDAE;
algorithm
outBackendDAE := matchcontinue(inITLst,inBackendDAE)
local
list<DAE.InlineType> itlst;
BackendDAE.Variables knownVars;
BackendDAE.Variables externalObjects,aliasVars "alias-variables' hashtable";
BackendDAE.EquationArray removedEqs;
BackendDAE.EquationArray initialEqs;
array<DAE.Constraint> constrs;
array<DAE.ClassAttributes> clsAttrs;
BackendDAE.EventInfo eventInfo;
BackendDAE.ExternalObjectClasses extObjClasses;
Functiontuple tpl;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
DAE.FunctionTree functionTree;
Env.Cache cache;
Env.Env env;
case (itlst,BackendDAE.DAE(eqs,BackendDAE.SHARED(knownVars=knownVars,externalObjects=externalObjects,aliasVars=aliasVars,initialEqs=initialEqs,removedEqs=removedEqs,constraints=constrs,classAttrs=clsAttrs,cache=cache,env=env,functionTree=functionTree,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=btp,symjacs=symjacs)))
equation
tpl = (SOME(functionTree),itlst);
eqs = List.map1(eqs,inlineEquationSystem,tpl);
(knownVars,_) = inlineVariables(knownVars,tpl);
(externalObjects,_) = inlineVariables(externalObjects,tpl);
(initialEqs,_) = inlineEquationArray(initialEqs,tpl);
(removedEqs,_) = inlineEquationArray(removedEqs,tpl);
eventInfo = inlineEventInfo(eventInfo,tpl);
then
BackendDAE.DAE(eqs,BackendDAE.SHARED(knownVars,externalObjects,aliasVars,initialEqs,removedEqs,constrs,clsAttrs,cache,env,functionTree,eventInfo,extObjClasses,btp,symjacs));
case(_,_)
equation
Debug.fprintln(Flags.FAILTRACE,"Inline.inlineCalls failed");
then
fail();
end matchcontinue;
end inlineCalls;
protected function inlineEquationSystem
input BackendDAE.EqSystem eqs;
input Functiontuple tpl;
output BackendDAE.EqSystem oeqs;
algorithm
oeqs := match (eqs,tpl)
local
BackendDAE.EqSystem syst;
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
BackendDAE.Matching matching;
Boolean b1,b2;
BackendDAE.StateSets stateSets;
case (syst as BackendDAE.EQSYSTEM(orderedVars=orderedVars,orderedEqs=orderedEqs,matching=matching,stateSets=stateSets),_)
equation
(orderedVars,b1) = inlineVariables(orderedVars,tpl);
(orderedEqs,b2) = inlineEquationArray(orderedEqs,tpl);
syst = Util.if_(b1 or b2,BackendDAE.EQSYSTEM(orderedVars,orderedEqs,NONE(),NONE(),matching,stateSets),syst);
then
syst;
end match;
end inlineEquationSystem;
protected function inlineEquationArray "
function: inlineEquationArray
inlines function calls in an equation array"
input BackendDAE.EquationArray inEquationArray;
input Functiontuple inElementList;
output BackendDAE.EquationArray outEquationArray;
output Boolean oInlined;
algorithm
(outEquationArray,oInlined) := matchcontinue(inEquationArray,inElementList)
local
Functiontuple fns;
Integer i1,i2,size;
array<Option<BackendDAE.Equation>> eqarr;
case(BackendDAE.EQUATION_ARRAY(size,i1,i2,eqarr),fns)
equation
oInlined = inlineEquationOptArray(1,eqarr,i2,fns,false);
then
(BackendDAE.EQUATION_ARRAY(size,i1,i2,eqarr),oInlined);
case(_,_)
equation
Debug.fprintln(Flags.FAILTRACE,"Inline.inlineEquationArray failed");
then
fail();
end matchcontinue;
end inlineEquationArray;
protected function inlineEquationOptArray
"functio: inlineEquationrOptArray
inlines calls in a equation option"
input Integer Index;
input array<Option<BackendDAE.Equation>> inEqnArray;
input Integer arraysize;
input Functiontuple fns;
input Boolean iInlined;
output Boolean oInlined;
algorithm
oInlined := matchcontinue(Index,inEqnArray,arraysize,fns,iInlined)
local
Option<BackendDAE.Equation> eqn;
Boolean b;
case(_,_,_,_,_)
equation
true = intLe(Index,arraysize);
eqn = inEqnArray[Index];
(eqn,b) = inlineEqOpt(eqn,fns);
updateArrayCond(b,inEqnArray,Index,eqn);
then
inlineEquationOptArray(Index+1,inEqnArray,arraysize,fns,b or iInlined);
case(_,_,_,_,_)
equation
false = intLe(Index,arraysize);
then
iInlined;
end matchcontinue;
end inlineEquationOptArray;
protected function inlineEqOpt "
function: inlineEqOpt
inlines function calls in equations"
input Option<BackendDAE.Equation> inEquationOption;
input Functiontuple inElementList;
output Option<BackendDAE.Equation> outEquationOption;
output Boolean inlined;
algorithm
(outEquationOption,inlined) := match(inEquationOption,inElementList)
local
BackendDAE.Equation eqn;
Boolean b;
case(NONE(),_) then (NONE(),false);
case(SOME(eqn),_)
equation
(eqn,b) = inlineEq(eqn,inElementList);
then
(SOME(eqn),b);
end match;
end inlineEqOpt;
public function inlineEq "
function: inlineEq
inlines function calls in equations"
input BackendDAE.Equation inEquation;
input Functiontuple fns;
output BackendDAE.Equation outEquation;
output Boolean inlined;
algorithm
(outEquation,inlined) := matchcontinue(inEquation,fns)
local
DAE.Exp e,e_1,e1,e1_1,e2,e2_1;
Integer size;
list<DAE.Exp> explst;
DAE.ComponentRef cref;
BackendDAE.WhenEquation weq,weq_1;
DAE.ElementSource source;
list<Integer> dimSize;
DAE.Algorithm alg;
list<DAE.Statement> stmts,stmts1;
list<BackendDAE.Equation> eqns;
list<list<BackendDAE.Equation>> eqnslst;
Boolean b1,b2,b3,d;
case(BackendDAE.EQUATION(e1,e2,source,d),_)
equation
(e1_1,source,b1) = inlineExp(e1,fns,source);
(e2_1,source,b2) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(BackendDAE.EQUATION(e1_1,e2_1,source,d),true);
case(BackendDAE.ARRAY_EQUATION(dimSize,e1,e2,source,d),_)
equation
(e1_1,source,b1) = inlineExp(e1,fns,source);
(e2_1,source,b2) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(BackendDAE.ARRAY_EQUATION(dimSize,e1_1,e2_1,source,d),true);
case(BackendDAE.SOLVED_EQUATION(cref,e,source,d),_)
equation
(e_1,source,true) = inlineExp(e,fns,source);
then
(BackendDAE.SOLVED_EQUATION(cref,e_1,source,d),true);
case(BackendDAE.RESIDUAL_EQUATION(e,source,d),_)
equation
(e_1,source,true) = inlineExp(e,fns,source);
then
(BackendDAE.RESIDUAL_EQUATION(e_1,source,d),true);
case(BackendDAE.ALGORITHM(size=size,alg=alg as DAE.ALGORITHM_STMTS(statementLst = stmts),source=source),_)
equation
(stmts1,true) = inlineStatements(stmts,fns,{},false);
alg = DAE.ALGORITHM_STMTS(stmts1);
then
(BackendDAE.ALGORITHM(size,alg,source),true);
case(BackendDAE.WHEN_EQUATION(size,weq,source),_)
equation
(weq_1,source,true) = inlineWhenEq(weq,fns,source);
then
(BackendDAE.WHEN_EQUATION(size,weq_1,source),true);
case(BackendDAE.COMPLEX_EQUATION(size,e1,e2,source,d),_)
equation
(e1_1,source,b1) = inlineExp(e1,fns,source);
(e2_1,source,b2) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(BackendDAE.COMPLEX_EQUATION(size,e1_1,e2_1,source,d),true);
case(BackendDAE.IF_EQUATION(explst,eqnslst,eqns,source),_)
equation
(explst,source,b1) = inlineExps(explst,fns,source);
(eqnslst,b2) = inlineEqsLst(eqnslst,fns,{},false);
(eqns,b3) = inlineEqs(eqns,fns,{},false);
true = b1 or b2 or b3;
then
(BackendDAE.IF_EQUATION(explst,eqnslst,eqns,source),true);
else
then
(inEquation,false);
end matchcontinue;
end inlineEq;
protected function inlineEqsLst
input list<list<BackendDAE.Equation>> inEqnsList;
input Functiontuple inFunctions;
input list<list<BackendDAE.Equation>> iAcc;
input Boolean iInlined;
output list<list<BackendDAE.Equation>> outEqnsList;
output Boolean OInlined;
algorithm
(outEqnsList,OInlined) := match(inEqnsList,inFunctions,iAcc,iInlined)
local
list<BackendDAE.Equation> eqn;
list<list<BackendDAE.Equation>> rest,acc;
Boolean inlined;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (eqn::rest,_,_,_)
equation
(eqn,inlined) = inlineEqs(eqn,inFunctions,{},false);
(acc,inlined) = inlineEqsLst(rest,inFunctions,eqn::iAcc,inlined or iInlined);
then
(acc,inlined);
end match;
end inlineEqsLst;
public function inlineEqs
input list<BackendDAE.Equation> inEqnsList;
input Functiontuple inFunctions;
input list<BackendDAE.Equation> iAcc;
input Boolean iInlined;
output list<BackendDAE.Equation> outEqnsList;
output Boolean OInlined;
algorithm
(outEqnsList,OInlined) := match(inEqnsList,inFunctions,iAcc,iInlined)
local
BackendDAE.Equation eqn;
list<BackendDAE.Equation> rest,acc;
Boolean inlined;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (eqn::rest,_,_,_)
equation
(eqn,inlined) = inlineEq(eqn,inFunctions);
(acc,inlined) = inlineEqs(rest,inFunctions,eqn::iAcc,inlined or iInlined);
then
(acc,inlined);
end match;
end inlineEqs;
protected function inlineWhenEq
"function: inlineWhenEq
inlines function calls in when equations"
input BackendDAE.WhenEquation inWhenEquation;
input Functiontuple fns;
input DAE.ElementSource inSource;
output BackendDAE.WhenEquation outWhenEquation;
output DAE.ElementSource outSource;
output Boolean inlined;
algorithm
(outWhenEquation,outSource,inlined) := matchcontinue(inWhenEquation,fns,inSource)
local
DAE.ComponentRef cref;
DAE.Exp e,e_1,cond;
BackendDAE.WhenEquation weq,weq_1;
DAE.ElementSource source;
Boolean b1,b2,b3;
case (BackendDAE.WHEN_EQ(cond,cref,e,NONE()),_,_)
equation
(e_1,source,b1) = inlineExp(e,fns,inSource);
(cond,source,b2) = inlineExp(cond,fns,source);
true = b1 or b2;
then
(BackendDAE.WHEN_EQ(cond,cref,e_1,NONE()),source,true);
case (BackendDAE.WHEN_EQ(cond,cref,e,SOME(weq)),_,_)
equation
(e_1,source,b1) = inlineExp(e,fns,inSource);
(cond,source,b2) = inlineExp(cond,fns,source);
(weq_1,source,b3) = inlineWhenEq(weq,fns,source);
true = b1 or b2 or b3;
then
(BackendDAE.WHEN_EQ(cond,cref,e_1,SOME(weq_1)),source,true);
else
then
(inWhenEquation,inSource,false);
end matchcontinue;
end inlineWhenEq;
protected function inlineVariables
"function: inlineVariables
inlines function calls in variables"
input BackendDAE.Variables inVariables;
input Functiontuple inElementList;
output BackendDAE.Variables outVariables;
output Boolean inlined;
algorithm
(outVariables,inlined) := matchcontinue(inVariables,inElementList)
local
Functiontuple fns;
array<list<BackendDAE.CrefIndex>> crefind;
Integer i1,i2,i3,i4;
array<Option<BackendDAE.Var>> vararr;
case(BackendDAE.VARIABLES(crefind,BackendDAE.VARIABLE_ARRAY(i3,i4,vararr),i1,i2),fns)
equation
inlined = inlineVarOptArray(1,vararr,i4,fns,false);
then
(BackendDAE.VARIABLES(crefind,BackendDAE.VARIABLE_ARRAY(i3,i4,vararr),i1,i2),inlined);
case(_,_)
equation
Debug.fprintln(Flags.FAILTRACE,"Inline.inlineVariables failed");
then
fail();
end matchcontinue;
end inlineVariables;
protected function updateArrayCond
input Boolean cond;
input array<Type_a> inArr;
input Integer index;
input Type_a value;
replaceable type Type_a subtypeof Any;
algorithm
_ := match(cond,inArr,index,value)
case(false,_,_,_) then ();
case(true,_,_,_)
equation
_ = arrayUpdate(inArr,index,value);
then
();
end match;
end updateArrayCond;
protected function inlineVarOptArray
"functio: inlineVarOptArray
inlines calls in a variable option"
input Integer Index;
input array<Option<BackendDAE.Var>> inVarArray;
input Integer arraysize;
input Functiontuple fns;
input Boolean iInlined;
output Boolean oInlined;
algorithm
oInlined := matchcontinue(Index,inVarArray,arraysize,fns,iInlined)
local
Option<BackendDAE.Var> var;
Boolean b;
case(_,_,_,_,_)
equation
true = intLe(Index,arraysize);
var = inVarArray[Index];
(var,b) = inlineVarOpt(var,fns);
updateArrayCond(b,inVarArray,Index,var);
then
inlineVarOptArray(Index+1,inVarArray,arraysize,fns,b or iInlined);
case(_,_,_,_,_)
equation
false = intLe(Index,arraysize);
then
iInlined;
end matchcontinue;
end inlineVarOptArray;
protected function inlineVarOpt
"functio: inlineVarOpt
inlines calls in a variable option"
input Option<BackendDAE.Var> inVarOption;
input Functiontuple fns;
output Option<BackendDAE.Var> outVarOption;
output Boolean inlined;
algorithm
(outVarOption,inlined) := match(inVarOption,fns)
local
BackendDAE.Var var;
Boolean b;
case(NONE(),_) then (NONE(),false);
case(SOME(var),_)
equation
(var,b) = inlineVar(var,fns);
then
(SOME(var),b);
end match;
end inlineVarOpt;
public function inlineVar
"functio: inlineVar
inlines calls in a variable"
input BackendDAE.Var inVar;
input Functiontuple inElementList;
output BackendDAE.Var outVar;
output Boolean inlined;
algorithm
(outVar,inlined) := match(inVar,inElementList)
local
Functiontuple fns;
DAE.ComponentRef varName;
BackendDAE.VarKind varKind;
DAE.VarDirection varDirection;
DAE.VarParallelism varParallelism;
BackendDAE.Type varType;
Option<Values.Value> bindValue;
DAE.InstDims arrayDim;
Option<DAE.VariableAttributes> values,values1;
Option<SCode.Comment> comment;
DAE.ConnectorType ct;
BackendDAE.Var var;
DAE.ElementSource source;
Option<DAE.Exp> bind;
Boolean b1,b2;
case(BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,bindValue,arrayDim,source,values,comment,ct),fns)
equation
(bind,source,b1) = inlineExpOpt(bind,fns,source);
(values1,source,b2) = inlineStartAttribute(values,source,fns);
then
(BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,bindValue,arrayDim,source,values1,comment,ct),b1 or b2);
case(var,_) then (var,false);
end match;
end inlineVar;
public function inlineStartAttribute
input Option<DAE.VariableAttributes> inVariableAttributesOption;
input DAE.ElementSource isource;
input Functiontuple fns;
output Option<DAE.VariableAttributes> outVariableAttributesOption;
output DAE.ElementSource osource;
output Boolean b;
algorithm
(outVariableAttributesOption,osource,b):=matchcontinue (inVariableAttributesOption,isource,fns)
local
DAE.ElementSource source;
DAE.Exp r;
Option<DAE.Exp> quantity,unit,displayUnit,fixed,nominal,so;
tuple<Option<DAE.Exp>, Option<DAE.Exp>> min;
Option<DAE.StateSelect> stateSelectOption;
Option<DAE.Uncertainty> uncertainOption;
Option<DAE.Distribution> distributionOption;
Option<DAE.Exp> equationBound;
Option<Boolean> isProtected,finalPrefix;
case (NONE(),_,_) then (NONE(),isource,false);
case (SOME(DAE.VAR_ATTR_REAL(quantity=quantity,unit=unit,displayUnit=displayUnit,min=min,initial_ = SOME(r),
fixed=fixed,nominal=nominal,stateSelectOption=stateSelectOption,uncertainOption=uncertainOption,
distributionOption=distributionOption,equationBound=equationBound,isProtected=isProtected,finalPrefix=finalPrefix,
startOrigin=so)),_,_)
equation
(r,source,true) = inlineExp(r,fns,isource);
then (SOME(DAE.VAR_ATTR_REAL(quantity,unit,displayUnit,min,SOME(r),fixed,nominal,
stateSelectOption,uncertainOption,distributionOption,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_INT(quantity=quantity,min=min,initial_ = SOME(r),
fixed=fixed,uncertainOption=uncertainOption,distributionOption=distributionOption,equationBound=equationBound,
isProtected=isProtected,finalPrefix=finalPrefix,startOrigin=so)),_,_)
equation
(r,source,true) = inlineExp(r,fns,isource);
then (SOME(DAE.VAR_ATTR_INT(quantity,min,SOME(r),fixed,uncertainOption,distributionOption,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_BOOL(quantity=quantity,initial_ = SOME(r),
fixed=fixed,equationBound=equationBound,isProtected=isProtected,finalPrefix=finalPrefix,startOrigin=so)),_,_)
equation
(r,source,true) = inlineExp(r,fns,isource);
then (SOME(DAE.VAR_ATTR_BOOL(quantity,SOME(r),fixed,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_STRING(quantity=quantity,initial_ = SOME(r),
equationBound=equationBound,isProtected=isProtected,finalPrefix=finalPrefix,startOrigin=so)),_,_)
equation
(r,source,true) = inlineExp(r,fns,isource);
then (SOME(DAE.VAR_ATTR_STRING(quantity,SOME(r),equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_ENUMERATION(quantity=quantity,min=min,start = SOME(r),
fixed=fixed,equationBound=equationBound,
isProtected=isProtected,finalPrefix=finalPrefix,startOrigin=so)),_,_)
equation
(r,source,true) = inlineExp(r,fns,isource);
then (SOME(DAE.VAR_ATTR_ENUMERATION(quantity,min,SOME(r),fixed,equationBound,isProtected,finalPrefix,so)),source,true);
case (_,_,_) then (inVariableAttributesOption,isource,false);
end matchcontinue;
end inlineStartAttribute;
protected function inlineEventInfo "function inlineEventInfo
inlines function calls in event info"
input BackendDAE.EventInfo inEventInfo;
input Functiontuple inElementList;
output BackendDAE.EventInfo outEventInfo;
algorithm
outEventInfo := matchcontinue(inEventInfo,inElementList)
local
Functiontuple fns;
list<BackendDAE.WhenClause> wclst,wclst_1;
list<BackendDAE.ZeroCrossing> zclst,zclst_1,relations,samples;
Integer numberOfRelations,numberOfMathEvents;
BackendDAE.EventInfo ev;
Boolean b1,b2,b3,b4;
BackendDAE.SampleLookup sampleLookup;
case(BackendDAE.EVENT_INFO(sampleLookup,wclst,zclst,samples,relations,numberOfRelations,numberOfMathEvents),fns)
equation
(wclst_1,b1) = inlineWhenClauses(wclst,fns,{},false);
(zclst_1,b2) = inlineZeroCrossings(zclst,fns,{},false);
(relations,b3) = inlineZeroCrossings(relations,fns,{},false);
(samples,b4) = inlineZeroCrossings(samples,fns,{},false);
ev = Util.if_(b1 or b2,BackendDAE.EVENT_INFO(sampleLookup,wclst_1,zclst_1,samples,relations,numberOfRelations,numberOfMathEvents),inEventInfo);
then
ev;
case(_,_)
equation
Debug.fprintln(Flags.FAILTRACE,"Inline.inlineEventInfo failed");
then
fail();
end matchcontinue;
end inlineEventInfo;
protected function inlineZeroCrossings
"function: inlineZeroCrossings
inlines function calls in reinit statements"
input list<BackendDAE.ZeroCrossing> inStmts;
input Functiontuple fns;
input list<BackendDAE.ZeroCrossing> iAcc;
input Boolean iInlined;
output list<BackendDAE.ZeroCrossing> outStmts;
output Boolean oInlined;
algorithm
(outStmts,oInlined) := match (inStmts,fns,iAcc,iInlined)
local
BackendDAE.ZeroCrossing zc;
list<BackendDAE.ZeroCrossing> rest,stmts;
Boolean b;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (zc::rest,_,_,_)
equation
(zc,b) = inlineZeroCrossing(zc,fns);
(stmts,b) = inlineZeroCrossings(rest,fns,zc::iAcc,b or iInlined);
then
(stmts,b);
end match;
end inlineZeroCrossings;
protected function inlineZeroCrossing
"function: inlineZeroCrossing
inlines function calls in a zero crossing"
input BackendDAE.ZeroCrossing inZeroCrossing;
input Functiontuple inElementList;
output BackendDAE.ZeroCrossing outZeroCrossing;
output Boolean oInlined;
algorithm
(outZeroCrossing,oInlined) := matchcontinue(inZeroCrossing,inElementList)
local
Functiontuple fns;
DAE.Exp e,e_1;
list<Integer> ilst1,ilst2;
case(BackendDAE.ZERO_CROSSING(e,ilst1,ilst2),fns)
equation
(e_1,_,true) = inlineExp(e,fns,DAE.emptyElementSource/*TODO: Propagate operation info*/);
then
(BackendDAE.ZERO_CROSSING(e_1,ilst1,ilst2),true);
case(_,_)
then
(inZeroCrossing,false);
end matchcontinue;
end inlineZeroCrossing;
protected function inlineWhenClauses
"function: inlineWhenClauses
inlines function calls in reinit statements"
input list<BackendDAE.WhenClause> inStmts;
input Functiontuple fns;
input list<BackendDAE.WhenClause> iAcc;
input Boolean iInlined;
output list<BackendDAE.WhenClause> outStmts;
output Boolean oInlined;
algorithm
(outStmts,oInlined) := match (inStmts,fns,iAcc,iInlined)
local
BackendDAE.WhenClause wc;
list<BackendDAE.WhenClause> rest,stmts;
Boolean b;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (wc::rest,_,_,_)
equation
(wc,b) = inlineWhenClause(wc,fns);
(stmts,b) = inlineWhenClauses(rest,fns,wc::iAcc,b or iInlined);
then
(stmts,b);
end match;
end inlineWhenClauses;
protected function inlineWhenClause
"function: inlineWhenClause
inlines function calls in a when clause"
input BackendDAE.WhenClause inWhenClause;
input Functiontuple inElementList;
output BackendDAE.WhenClause outWhenClause;
output Boolean inlined;
algorithm
(outWhenClause,inlined) := matchcontinue(inWhenClause,inElementList)
local
Functiontuple fns;
DAE.Exp e,e_1;
list<BackendDAE.WhenOperator> rslst,rslst_1;
Option<Integer> io;
Boolean b1,b2;
case(BackendDAE.WHEN_CLAUSE(e,rslst,io),fns)
equation
(e_1,_,b1) = inlineExp(e,fns,DAE.emptyElementSource/*TODO: Propagate operation info*/);
(rslst_1,b2) = inlineReinitStmts(rslst,fns,{},false);
true = b1 or b2;
then
(BackendDAE.WHEN_CLAUSE(e_1,rslst_1,io),true);
case(_,_)
then
(inWhenClause,false);
end matchcontinue;
end inlineWhenClause;
protected function inlineReinitStmts
"function: inlineReinitStmts
inlines function calls in reinit statements"
input list<BackendDAE.WhenOperator> inStmts;
input Functiontuple fns;
input list<BackendDAE.WhenOperator> iAcc;
input Boolean iInlined;
output list<BackendDAE.WhenOperator> outStmts;
output Boolean oInlined;
algorithm
(outStmts,oInlined) := match (inStmts,fns,iAcc,iInlined)
local
BackendDAE.WhenOperator re;
list<BackendDAE.WhenOperator> rest,stmts;
Boolean b;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (re::rest,_,_,_)
equation
(re,b) = inlineReinitStmt(re,fns);
(stmts,b) = inlineReinitStmts(rest,fns,re::iAcc,b or iInlined);
then
(stmts,b);
end match;
end inlineReinitStmts;
protected function inlineReinitStmt
"function: inlineReinitStmt
inlines function calls in a reinit statement"
input BackendDAE.WhenOperator inReinitStatement;
input Functiontuple inElementList;
output BackendDAE.WhenOperator outReinitStatement;
output Boolean inlined;
algorithm
(outReinitStatement,inlined) := matchcontinue(inReinitStatement,inElementList)
local
Functiontuple fns;
DAE.ComponentRef cref;
DAE.Exp e,e_1;
BackendDAE.WhenOperator rs;
DAE.ElementSource source;
case (BackendDAE.REINIT(cref,e,source),fns)
equation
(e_1,source,true) = inlineExp(e,fns,source);
then
(BackendDAE.REINIT(cref,e_1,source),true);
case (rs,_) then (rs,false);
end matchcontinue;
end inlineReinitStmt;
public function inlineCallsInFunctions
"function: inlineDAEElements
inlines calls in DAEElements"
input list<DAE.Function> inElementList;
input Functiontuple inFunctions;
input list<DAE.Function> iAcc;
output list<DAE.Function> outElementList;
algorithm
outElementList := matchcontinue(inElementList,inFunctions,iAcc)
local
list<DAE.Function> cdr;
list<DAE.Element> elist,elist_1;
DAE.Function el,res;
DAE.Type t;
Boolean partialPrefix, isImpure;
Absyn.Path p;
DAE.ExternalDecl ext;
DAE.InlineType inlineType;
list<DAE.FunctionDefinition> funcDefs;
DAE.ElementSource source;
Option<SCode.Comment> cmt;
case({},_,_) then listReverse(iAcc);
case(DAE.FUNCTION(p,DAE.FUNCTION_DEF(body = elist)::funcDefs,t,partialPrefix,isImpure,inlineType,source,cmt) :: cdr,_,_)
equation
(elist_1,true)= inlineDAEElements(elist,inFunctions,{},false);
res = DAE.FUNCTION(p,DAE.FUNCTION_DEF(elist_1)::funcDefs,t,partialPrefix,isImpure,inlineType,source,cmt);
then
inlineCallsInFunctions(cdr,inFunctions,res::iAcc);
// external functions
case(DAE.FUNCTION(p,DAE.FUNCTION_EXT(elist,ext)::funcDefs,t,partialPrefix,isImpure,inlineType,source,cmt) :: cdr,_,_)
equation
(elist_1,true)= inlineDAEElements(elist,inFunctions,{},false);
res = DAE.FUNCTION(p,DAE.FUNCTION_EXT(elist_1,ext)::funcDefs,t,partialPrefix,isImpure,inlineType,source,cmt);
then
inlineCallsInFunctions(cdr,inFunctions,res::iAcc);
case(el :: cdr,_,_)
then
inlineCallsInFunctions(cdr,inFunctions,el::iAcc);
else
equation
Error.addMessage(Error.INTERNAL_ERROR,{"Inline.inlineCallsInFunctions failed"});
then fail();
end matchcontinue;
end inlineCallsInFunctions;
protected function inlineDAEElementsLst
input list<list<DAE.Element>> inElementList;
input Functiontuple inFunctions;
input list<list<DAE.Element>> iAcc;
input Boolean iInlined;
output list<list<DAE.Element>> outElementList;
output Boolean OInlined;
algorithm
(outElementList,OInlined) := match(inElementList,inFunctions,iAcc,iInlined)
local
list<DAE.Element> elem;
list<list<DAE.Element>> rest,acc;
Boolean inlined;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (elem::rest,_,_,_)
equation
(elem,inlined) = inlineDAEElements(elem,inFunctions,{},false);
(acc,inlined) = inlineDAEElementsLst(rest,inFunctions,elem::iAcc,inlined or iInlined);
then
(acc,inlined);
end match;
end inlineDAEElementsLst;
protected function inlineDAEElements
input list<DAE.Element> inElementList;
input Functiontuple inFunctions;
input list<DAE.Element> iAcc;
input Boolean iInlined;
output list<DAE.Element> outElementList;
output Boolean OInlined;
algorithm
(outElementList,OInlined) := match(inElementList,inFunctions,iAcc,iInlined)
local
DAE.Element elem;
list<DAE.Element> rest,acc;
Boolean inlined;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (elem::rest,_,_,_)
equation
(elem,inlined) = inlineDAEElement(elem,inFunctions);
(acc,inlined) = inlineDAEElements(rest,inFunctions,elem::iAcc,inlined or iInlined);
then
(acc,inlined);
end match;
end inlineDAEElements;
protected function inlineDAEElement
"function: inlineDAEElement
inlines calls in DAEElements"
input DAE.Element inElement;
input Functiontuple inFunctions;
output DAE.Element outElement;
output Boolean inlined;
algorithm
(outElement,inlined) := matchcontinue(inElement,inFunctions)
local
Functiontuple fns;
list<DAE.Element> elist,elist_1;
list<list<DAE.Element>> dlist,dlist_1;
DAE.Element el,el_1;
DAE.ComponentRef componentRef;
DAE.VarKind kind;
DAE.VarDirection direction;
DAE.VarParallelism parallelism;
DAE.VarVisibility protection;
DAE.Type ty;
DAE.Exp binding,binding_1,exp,exp_1,exp1,exp1_1,exp2,exp2_1,exp3,exp3_1;
DAE.InstDims dims;
DAE.ConnectorType ct;
Option<DAE.VariableAttributes> variableAttributesOption;
Option<SCode.Comment> absynCommentOption;
Absyn.InnerOuter innerOuter;
DAE.Dimensions dimension;
Algorithm.Algorithm alg,alg_1;
Ident i;
Absyn.Path p;
list<DAE.Exp> explst,explst_1;
DAE.ElementSource source;
Boolean b1,b2,b3;
case (DAE.VAR(componentRef,kind,direction,parallelism,protection,ty,SOME(binding),dims,ct,
source,variableAttributesOption,absynCommentOption,innerOuter),fns)
equation
(binding_1,source,true) = inlineExp(binding,fns,source);
then
(DAE.VAR(componentRef,kind,direction,parallelism,protection,ty,SOME(binding_1),dims,ct,
source,variableAttributesOption,absynCommentOption,innerOuter),true);
case (DAE.DEFINE(componentRef,exp,source) ,fns)
equation
(exp_1,source,true) = inlineExp(exp,fns,source);
then
(DAE.DEFINE(componentRef,exp_1,source),true);
case(DAE.INITIALDEFINE(componentRef,exp,source) ,fns)
equation
(exp_1,source,true) = inlineExp(exp,fns,source);
then
(DAE.INITIALDEFINE(componentRef,exp_1,source),true);
case(DAE.EQUATION(exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
then
(DAE.EQUATION(exp1_1,exp2_1,source),true);
case(DAE.ARRAY_EQUATION(dimension,exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
then
(DAE.ARRAY_EQUATION(dimension,exp1_1,exp2_1,source),true);
case(DAE.INITIAL_ARRAY_EQUATION(dimension,exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
then
(DAE.INITIAL_ARRAY_EQUATION(dimension,exp1_1,exp2_1,source),true);
case(DAE.COMPLEX_EQUATION(exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
then
(DAE.COMPLEX_EQUATION(exp1_1,exp2_1,source),true);
case(DAE.INITIAL_COMPLEX_EQUATION(exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
then
(DAE.INITIAL_COMPLEX_EQUATION(exp1_1,exp2_1,source),true);
case(DAE.WHEN_EQUATION(exp,elist,SOME(el),source),fns)
equation
(exp_1,source,b1) = inlineExp(exp,fns,source);
(elist_1,b2) = inlineDAEElements(elist,fns,{},false);
(el_1,b3) = inlineDAEElement(el,fns);
true = b1 or b2 or b3;
then
(DAE.WHEN_EQUATION(exp_1,elist_1,SOME(el_1),source),true);
case(DAE.WHEN_EQUATION(exp,elist,NONE(),source),fns)
equation
(exp_1,source,b1) = inlineExp(exp,fns,source);
(elist_1,b2) = inlineDAEElements(elist,fns,{},false);
true = b1 or b2;
then
(DAE.WHEN_EQUATION(exp_1,elist_1,NONE(),source),true);
case(DAE.IF_EQUATION(explst,dlist,elist,source) ,fns)
equation
(explst_1,source,b1) = inlineExps(explst,fns,source);
(dlist_1,b2) = inlineDAEElementsLst(dlist,fns,{},false);
(elist_1,b3) = inlineDAEElements(elist,fns,{},false);
true = b1 or b2 or b3;
then
(DAE.IF_EQUATION(explst_1,dlist_1,elist_1,source),true);
case(DAE.INITIAL_IF_EQUATION(explst,dlist,elist,source) ,fns)
equation
(explst_1,source,b1) = inlineExps(explst,fns,source);
(dlist_1,b2) = inlineDAEElementsLst(dlist,fns,{},false);
(elist_1,b3) = inlineDAEElements(elist,fns,{},false);
true = b1 or b2 or b3;
then
(DAE.INITIAL_IF_EQUATION(explst_1,dlist_1,elist_1,source),true);
case(DAE.INITIALEQUATION(exp1,exp2,source),fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
then
(DAE.INITIALEQUATION(exp1_1,exp2_1,source),true);
case((el as DAE.ALGORITHM(alg,source)),fns)
equation
(alg_1,true) = inlineAlgorithm(alg,fns);
then
(DAE.ALGORITHM(alg_1,source),true);
case((el as DAE.INITIALALGORITHM(alg,source)) ,fns)
equation
(alg_1,true) = inlineAlgorithm(alg,fns);