-
Notifications
You must be signed in to change notification settings - Fork 297
/
Inline.mo
1832 lines (1704 loc) · 64.9 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-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, 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 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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.
*
*/
encapsulated package Inline
" file: Inline.mo
package: Inline
description: inline functions
This module contains data structures and functions for inline functions.
The entry point is the inlineCalls function, or inlineCallsInFunctions
"
import Absyn;
import AbsynUtil;
import AvlSetPath;
import BaseHashTable;
import DAE;
import HashTableCG;
import SCode;
import Util;
type Functiontuple = tuple<Option<DAE.FunctionTree>,list<DAE.InlineType>>;
protected
import Ceval;
import ClassInf;
import ComponentReference;
import Config;
import DAEDump;
import Debug;
import ElementSource;
import Error;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Flags;
import Global;
import List;
import SCodeUtil;
import Types;
import VarTransform;
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,min,max;
Option<DAE.StateSelect> stateSelectOption;
Option<DAE.Uncertainty> uncertainOption;
Option<DAE.Distribution> distributionOption;
Option<DAE.Exp> equationBound;
Option<Boolean> isProtected,finalPrefix;
list<DAE.Statement> assrtLst;
case (NONE(),_,_) then (NONE(),isource,false);
case
(SOME(DAE.VAR_ATTR_REAL(quantity=quantity,unit=unit,displayUnit=displayUnit,min=min,max=max,start = 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,max,SOME(r),fixed,nominal,
stateSelectOption,uncertainOption,distributionOption,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_INT(quantity=quantity,min=min,max=max,start = 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,max,SOME(r),fixed,uncertainOption,distributionOption,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_BOOL(quantity=quantity,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_BOOL(quantity,SOME(r),fixed,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_STRING(quantity=quantity,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_STRING(quantity,SOME(r),fixed,equationBound,isProtected,finalPrefix,so)),source,true);
case (SOME(DAE.VAR_ATTR_ENUMERATION(quantity=quantity,min=min,max=max,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,max,SOME(r),fixed,equationBound,isProtected,finalPrefix,so)),source,true);
else (inVariableAttributesOption,isource,false);
end matchcontinue;
end inlineStartAttribute;
public function inlineCallsInFunctions
"inlines calls in DAEElements"
input list<DAE.Function> inElementList;
input Functiontuple inFunctions;
output list<DAE.Function> outElementList;
protected
list<DAE.Element> body;
DAE.ExternalDecl ext_decl;
DAE.FunctionDefinition fn_def;
list<DAE.FunctionDefinition> fn_defs;
algorithm
outElementList := list(
matchcontinue fn
case DAE.FUNCTION(functions = (fn_def as DAE.FUNCTION_DEF()) :: fn_defs)
algorithm
(body, true) := inlineDAEElements(fn_def.body, inFunctions, {}, false);
fn_def.body := body;
fn.functions := fn_def :: fn_defs;
then
fn;
case DAE.FUNCTION(functions = (fn_def as DAE.FUNCTION_EXT()) :: fn_defs)
algorithm
(body, true) := inlineDAEElements(fn_def.body, inFunctions, {}, false);
fn_def.body := body;
fn.functions := fn_def :: fn_defs;
then
fn;
else fn;
end matchcontinue
for fn in inElementList);
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
"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;
Boolean e;
DAE.Dimensions dimension;
DAE.Algorithm alg,alg_1;
String i;
Absyn.Path p;
list<DAE.Exp> explst,explst_1;
DAE.ElementSource source;
Boolean b1,b2,b3;
list<DAE.Statement> assrtLst;
case (DAE.VAR(componentRef,kind,direction,parallelism,protection,ty,SOME(binding),dims,ct,
source,variableAttributesOption,absynCommentOption,innerOuter,e),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,e),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,_,_) = inlineExp(exp1,fns,source);
(exp2_1,source,_,_) = inlineExp(exp2,fns,source);
then
(DAE.INITIALEQUATION(exp1_1,exp2_1,source),true);
case((DAE.ALGORITHM(alg,source)),fns)
equation
(alg_1,true) = inlineAlgorithm(alg,fns);
then
(DAE.ALGORITHM(alg_1,source),true);
case((DAE.INITIALALGORITHM(alg,source)) ,fns)
equation
(alg_1,true) = inlineAlgorithm(alg,fns);
then
(DAE.INITIALALGORITHM(alg_1,source),true);
case(DAE.COMP(i,elist,source,absynCommentOption),fns)
equation
(elist_1,true) = inlineDAEElements(elist,fns,{},false);
then
(DAE.COMP(i,elist_1,source,absynCommentOption),true);
case(DAE.ASSERT(exp1,exp2,exp3,source) ,fns)
equation
(exp1_1,source,b1,_) = inlineExp(exp1,fns,source);
(exp2_1,source,b2,_) = inlineExp(exp2,fns,source);
(exp3_1,source,b3,_) = inlineExp(exp3,fns,source);
true = b1 or b2 or b3;
then
(DAE.ASSERT(exp1_1,exp2_1,exp3_1,source),true);
case(DAE.INITIAL_ASSERT(exp1,exp2,exp3,source) ,fns)
equation
(exp1_1,source,b1,_) = inlineExp(exp1,fns,source);
(exp2_1,source,b2,_) = inlineExp(exp2,fns,source);
(exp3_1,source,b3,_) = inlineExp(exp3,fns,source);
true = b1 or b2 or b3;
then
(DAE.INITIAL_ASSERT(exp1_1,exp2_1,exp3_1,source),true);
case(DAE.TERMINATE(exp,source),fns)
equation
(exp_1,source,true,_) = inlineExp(exp,fns,source);
then
(DAE.TERMINATE(exp_1,source),true);
case(DAE.INITIAL_TERMINATE(exp,source),fns)
equation
(exp_1,source,true,_) = inlineExp(exp,fns,source);
then
(DAE.INITIAL_TERMINATE(exp_1,source),true);
case(DAE.REINIT(componentRef,exp,source),fns)
equation
(exp_1,source,true,_) = inlineExp(exp,fns,source);
then
(DAE.REINIT(componentRef,exp_1,source),true);
case(DAE.NORETCALL(exp,source),fns)
equation
(exp,source,true,_) = inlineExp(exp,fns,source);
then
(DAE.NORETCALL(exp,source),true);
case(DAE.INITIAL_NORETCALL(exp,source),fns)
equation
(exp,source,true,_) = inlineExp(exp,fns,source);
then
(DAE.INITIAL_NORETCALL(exp,source),true);
case(el,_)
then
(el,false);
end matchcontinue;
end inlineDAEElement;
public function inlineAlgorithm
"inline calls in an DAE.Algorithm"
input DAE.Algorithm inAlgorithm;
input Functiontuple inElementList;
output DAE.Algorithm outAlgorithm;
output Boolean inlined;
algorithm
(outAlgorithm,inlined) := matchcontinue(inAlgorithm,inElementList)
local
list<DAE.Statement> stmts,stmts_1;
Functiontuple fns;
case(DAE.ALGORITHM_STMTS(stmts),fns)
equation
(stmts_1,inlined) = inlineStatements(stmts,fns,{},false);
then
(DAE.ALGORITHM_STMTS(stmts_1),inlined);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("Inline.inlineAlgorithm failed\n");
then
fail();
end matchcontinue;
end inlineAlgorithm;
public function inlineStatements
input list<DAE.Statement> inStatements;
input Functiontuple inElementList;
input list<DAE.Statement> iAcc;
input Boolean iInlined;
output list<DAE.Statement> outStatements;
output Boolean OInlined;
algorithm
(outStatements,OInlined) := match(inStatements,inElementList,iAcc,iInlined)
local
DAE.Statement stmt;
list<DAE.Statement> rest,acc;
Boolean inlined;
case ({},_,_,_) then (listReverse(iAcc),iInlined);
case (stmt::rest,_,_,_)
equation
(stmt,inlined) = inlineStatement(stmt,inElementList);
(acc,inlined) = inlineStatements(rest,inElementList,stmt::iAcc,inlined or iInlined);
then
(acc,inlined);
end match;
end inlineStatements;
protected function inlineStatement
"inlines calls in an DAE.Statement"
input DAE.Statement inStatement;
input Functiontuple inElementList;
output DAE.Statement outStatement;
output Boolean inlined;
algorithm
(outStatement,inlined) := matchcontinue(inStatement,inElementList)
local
Functiontuple fns;
DAE.Statement stmt,stmt_1;
DAE.Type t;
DAE.Exp e,e_1,e1,e1_1,e2,e2_1,e3,e3_1;
list<DAE.Exp> explst,explst_1;
DAE.ComponentRef cref;
DAE.Else a_else,a_else_1;
list<DAE.Statement> stmts,stmts_1;
Boolean b,b1,b2,b3;
String i;
DAE.ElementSource source;
list<DAE.ComponentRef> conditions;
Boolean initialCall;
list<DAE.Statement> assrtLst;
case (DAE.STMT_ASSIGN(t,e1,e2,source),fns)
equation
(e1_1,source,b1,_) = inlineExp(e1,fns,source);
(e2_1,source,b2,_) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(DAE.STMT_ASSIGN(t,e1_1,e2_1,source),true);
case(DAE.STMT_TUPLE_ASSIGN(t,explst,e,source),fns)
equation
(explst_1,source,b1) = inlineExps(explst,fns,source);
(e_1,source,b2,_) = inlineExp(e,fns,source);
true = b1 or b2;
then
(DAE.STMT_TUPLE_ASSIGN(t,explst_1,e_1,source),true);
case(DAE.STMT_ASSIGN_ARR(t,e1,e2,source),fns)
equation
(e1_1,source,b1,_) = inlineExp(e1,fns,source);
(e2_1,source,b2,_) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(DAE.STMT_ASSIGN_ARR(t,e1_1,e2_1,source),true);
case(DAE.STMT_IF(e,stmts,a_else,source),fns)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
(a_else_1,source,b3) = inlineElse(a_else,fns,source);
true = b1 or b2 or b3;
then
(DAE.STMT_IF(e_1,stmts_1,a_else_1,source),true);
case(DAE.STMT_FOR(t,b,i,e,stmts,source),fns)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
true = b1 or b2;
then
(DAE.STMT_FOR(t,b,i,e_1,stmts_1,source),true);
case(DAE.STMT_WHILE(e,stmts,source),fns)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
true = b1 or b2;
then
(DAE.STMT_WHILE(e_1,stmts_1,source),true);
case(DAE.STMT_WHEN(e,conditions,initialCall,stmts,SOME(stmt),source),fns)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
(stmt_1,b3) = inlineStatement(stmt,fns);
true = b1 or b2 or b3;
then
(DAE.STMT_WHEN(e_1,conditions,initialCall,stmts_1,SOME(stmt_1),source),true);
case(DAE.STMT_WHEN(e,conditions,initialCall,stmts,NONE(),source),fns)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
true = b1 or b2;
then
(DAE.STMT_WHEN(e_1,conditions,initialCall,stmts_1,NONE(),source),true);
case(DAE.STMT_ASSERT(e1,e2,e3,source),fns)
equation
(e1_1,source,b1,_) = inlineExp(e1,fns,source);
(e2_1,source,b2,_) = inlineExp(e2,fns,source);
(e3_1,source,b3,_) = inlineExp(e3,fns,source);
true = b1 or b2 or b3;
then
(DAE.STMT_ASSERT(e1_1,e2_1,e3_1,source),true);
case(DAE.STMT_TERMINATE(e,source),fns)
equation
(e_1,source,true,_) = inlineExp(e,fns,source);
then
(DAE.STMT_TERMINATE(e_1,source),true);
case(DAE.STMT_REINIT(e1,e2,source),fns)
equation
(e1_1,source,b1,_) = inlineExp(e1,fns,source);
(e2_1,source,b2,_) = inlineExp(e2,fns,source);
true = b1 or b2;
then
(DAE.STMT_REINIT(e1_1,e2_1,source),true);
case(DAE.STMT_NORETCALL(e,source),fns)
equation
(e_1,source,true,_) = inlineExp(e,fns,source);
then
(DAE.STMT_NORETCALL(e_1,source),true);
case(DAE.STMT_FAILURE(stmts,source),fns)
equation
(stmts_1,true) = inlineStatements(stmts,fns,{},false);
then
(DAE.STMT_FAILURE(stmts_1,source),true);
case(stmt,_) then (stmt,false);
end matchcontinue;
end inlineStatement;
protected function inlineElse
"inlines calls in an DAE.Else"
input DAE.Else inElse;
input Functiontuple inElementList;
input DAE.ElementSource inSource;
output DAE.Else outElse;
output DAE.ElementSource outSource;
output Boolean inlined;
algorithm
(outElse,outSource,inlined) := matchcontinue(inElse,inElementList,inSource)
local
Functiontuple fns;
DAE.Else a_else,a_else_1;
DAE.Exp e,e_1;
list<DAE.Statement> stmts,stmts_1;
DAE.ElementSource source;
Boolean b1,b2,b3;
list<DAE.Statement> assrtLst;
case (DAE.ELSEIF(e,stmts,a_else),fns,source)
equation
(e_1,source,b1,_) = inlineExp(e,fns,source);
(stmts_1,b2) = inlineStatements(stmts,fns,{},false);
(a_else_1,source,b3) = inlineElse(a_else,fns,source);
true = b1 or b2 or b3;
then
(DAE.ELSEIF(e_1,stmts_1,a_else_1),source,true);
case (DAE.ELSE(stmts),fns,source)
equation
(stmts_1,true) = inlineStatements(stmts,fns,{},false);
then
(DAE.ELSE(stmts_1),source,true);
case (a_else,_,source) then (a_else,source,false);
end matchcontinue;
end inlineElse;
public function inlineExpOpt "
function: inlineExpOpt
inlines calls in an DAE.Exp"
input Option<DAE.Exp> inExpOption;
input Functiontuple inElementList;
input DAE.ElementSource inSource;
output Option<DAE.Exp> outExpOption;
output DAE.ElementSource outSource;
output Boolean inlined;
algorithm
(outExpOption,outSource,inlined) := match(inExpOption,inElementList,inSource)
local
DAE.Exp exp;
DAE.ElementSource source;
Boolean b;
list<DAE.Statement> assrtLst;
case(NONE(),_,_) then (NONE(),inSource,false);
case(SOME(exp),_,_)
equation
(exp,source,b,_) = inlineExp(exp,inElementList,inSource);
then
(SOME(exp),source,b);
end match;
end inlineExpOpt;
public function inlineExp "
function: inlineExp
inlines calls in a DAE.Exp"
input DAE.Exp inExp;
input Functiontuple inElementList;
input DAE.ElementSource inSource;
output DAE.Exp outExp;
output DAE.ElementSource outSource;
output Boolean inlined;
output list<DAE.Statement> assrtLstOut;
algorithm
(outExp,outSource,inlined,assrtLstOut) := matchcontinue (inExp,inElementList,inSource)
local
Functiontuple fns;
DAE.Exp e,e_1,e_2;
DAE.ElementSource source;
list<DAE.Statement> assrtLst;
DAE.EquationExp eq;
// never inline WILD!
case (DAE.CREF(componentRef = DAE.WILD()),_,_) then (inExp,inSource,false,{});
case (e,fns,source)
algorithm
(e_1,assrtLst) := Expression.traverseExpBottomUp(e,function inlineCall(fns=fns),{});
false := referenceEq(e, e_1);
if Flags.isSet(Flags.INFO_XML_OPERATIONS) then
source := ElementSource.addSymbolicTransformation(source,DAE.OP_INLINE(DAE.PARTIAL_EQUATION(e),DAE.PARTIAL_EQUATION(e_1)));
(DAE.PARTIAL_EQUATION(e_2),source) := ExpressionSimplify.simplifyAddSymbolicOperation(DAE.PARTIAL_EQUATION(e_1), source);
else
e_2 := ExpressionSimplify.simplify(e_1);
end if;
then
(e_2,source,true,assrtLst);
else (inExp,inSource,false,{});
end matchcontinue;
end inlineExp;
public function forceInlineExp "
function: inlineExp
inlines calls in an DAE.Exp"
input DAE.Exp inExp;
input Functiontuple inElementList;
input DAE.ElementSource inSource;
output DAE.Exp outExp;
output DAE.ElementSource outSource;
output Boolean inlineperformed;
algorithm
(outExp,outSource,inlineperformed) := match (inExp,inElementList,inSource)
local
Functiontuple fns;
DAE.Exp e,e_1,e_2;
DAE.ElementSource source;
list<DAE.Statement> assrtLst;
DAE.FunctionTree functionTree;
Boolean b;
case (e,(SOME(functionTree),_),source)
guard
Expression.isConst(inExp)
equation
e_1 = Ceval.cevalSimpleWithFunctionTreeReturnExp(inExp, functionTree);
source = ElementSource.addSymbolicTransformation(source,DAE.OP_INLINE(DAE.PARTIAL_EQUATION(e),DAE.PARTIAL_EQUATION(e_1)));
then (e_1,source,true);
case (e,fns,source)
equation
(e_1,_) = Expression.traverseExpBottomUp(e,function forceInlineCall(fns=fns, visitedPaths=AvlSetPath.Tree.EMPTY()),{});
b = not referenceEq(e, e_1);
if b then
source = ElementSource.addSymbolicTransformation(source,DAE.OP_INLINE(DAE.PARTIAL_EQUATION(e),DAE.PARTIAL_EQUATION(e_1)));
(DAE.PARTIAL_EQUATION(e_1),source) = ExpressionSimplify.simplifyAddSymbolicOperation(DAE.PARTIAL_EQUATION(e_1), source);
end if;
then
(e_1,source,b);
else (inExp,inSource,false);
end match;
end forceInlineExp;
public function inlineExps "
function: inlineExp
inlines calls in an DAE.Exp"
input list<DAE.Exp> inExps;
input Functiontuple inElementList;
input DAE.ElementSource inSource;
output list<DAE.Exp> outExps;
output DAE.ElementSource outSource;
output Boolean inlined;
algorithm
(outExps,outSource,inlined) := inlineExpsWork(inExps,inElementList,inSource,{},false);
end inlineExps;
protected function inlineExpsWork "
function: inlineExp
inlines calls in an DAE.Exp"
input list<DAE.Exp> inExps;
input Functiontuple fns;
input DAE.ElementSource inSource;
input list<DAE.Exp> iAcc;
input Boolean iInlined;
output list<DAE.Exp> outExps;
output DAE.ElementSource outSource;
output Boolean oInlined;
algorithm
(outExps,outSource,oInlined) := match (inExps,fns,inSource,iAcc,iInlined)
local
DAE.Exp e;
list<DAE.Exp> exps;
DAE.ElementSource source;
Boolean b;
list<DAE.Statement> assrtLst;
case ({},_,_,_,_) then (listReverse(iAcc),inSource,iInlined);
case (e::exps,_,_,_,_)
equation
(e,source,b,_) = inlineExp(e,fns,inSource);
(exps,source,b) = inlineExpsWork(exps,fns,source,e::iAcc,b or iInlined);
then
(exps,source,b);
end match;
end inlineExpsWork;
public function checkExpsTypeEquiv
"@author: adrpo
check two types for equivalence"
input DAE.Exp inExp1;
input DAE.Exp inExp2;
output Boolean bEquiv;
algorithm
bEquiv := match(inExp1, inExp2)
local
DAE.Type ty1,ty2;
Boolean b;
case (_, _)
equation
if Config.acceptMetaModelicaGrammar() then
// adrpo: DO NOT COMPARE TYPES for equivalence for MetaModelica!
b = true;
else // compare
ty1 = Expression.typeof(inExp1);
ty2 = Expression.typeof(inExp2);
ty2 = Types.traverseType(ty2, -1, Types.makeExpDimensionsUnknown);
b = Types.equivtypesOrRecordSubtypeOf(ty1,ty2);
end if;
then b;
end match;
end checkExpsTypeEquiv;
public function inlineCall
"replaces an inline call with the expression from the function"
input output DAE.Exp exp;
input output list<DAE.Statement> assrtLst;
input Functiontuple fns;
algorithm
(exp,assrtLst) := matchcontinue exp
local
list<DAE.Element> fn;
Absyn.Path p;
list<DAE.Exp> args;
list<DAE.ComponentRef> crefs;
list<tuple<DAE.ComponentRef, DAE.Exp>> argmap;
list<DAE.ComponentRef> lst_cr;
DAE.ElementSource source;
DAE.Exp newExp,newExp1, e1, cond, msg, level, newAssrtCond, newAssrtMsg, newAssrtLevel;
DAE.InlineType inlineType;
DAE.Statement assrt;
HashTableCG.HashTable checkcr;
list<DAE.Statement> stmts,assrtStmts;
VarTransform.VariableReplacements repl;
Boolean generateEvents;
Option<SCode.Comment> comment;
DAE.Type ty;
DAE.Function func;
// If we disable inlining by use of flags, we still inline builtin functions
case DAE.CALL(attr=DAE.CALL_ATTR(inlineType=inlineType))
equation
false = Flags.isSet(Flags.INLINE_FUNCTIONS);
false = valueEq(DAE.BUILTIN_EARLY_INLINE(), inlineType);
then (exp,assrtLst);
// remove empty calls entirely if it is not impure
case (e1 as DAE.CALL(p,args,DAE.CALL_ATTR(ty=ty,inlineType=inlineType)))
equation
// is impure?
func = getFunction(p,fns);
false = DAEUtil.getFunctionImpureAttribute(func);
// no return value?
0 = Types.getDimensionProduct(ty);
newExp = Expression.makeArray({}, ty, true);
then (newExp, assrtLst);
case (e1 as DAE.CALL(p,args,DAE.CALL_ATTR(ty=ty,inlineType=inlineType)))
equation
//true = DAEUtil.convertInlineTypeToBool(inlineType);
true = checkInlineType(inlineType,fns);
(fn,comment) = getFunctionBody(p,fns);
(checkcr,repl) = getInlineHashTableVarTransform();
if (Config.acceptMetaModelicaGrammar()) then // MetaModelica
crefs = List.map(fn,getInputCrefs);
crefs = List.select(crefs,removeWilds);
argmap = List.zip(crefs,args);
false = List.exist(fn,DAEUtil.isProtectedVar);
newExp = getRhsExp(fn);
// compare types
true = checkExpsTypeEquiv(e1, newExp);
(argmap,checkcr) = extendCrefRecords(argmap,checkcr);
// add noEvent to avoid events as usually for functions
// MSL 3.2.1 need GenerateEvents to disable this
newExp = Expression.addNoEventToRelationsAndConds(newExp);
(newExp,(_,_,true)) = Expression.traverseExpBottomUp(newExp,replaceArgs,(argmap,checkcr,true));
// for inlinecalls in functions
(newExp1,assrtLst) = Expression.traverseExpBottomUp(newExp,function inlineCall(fns=fns),assrtLst);
else // normal Modelica
// get inputs, body and output
(crefs,lst_cr,stmts,repl) = getFunctionInputsOutputBody(fn, repl);
// merge statements to one line
(repl,assrtStmts) = mergeFunctionBody(stmts,repl,{});
// depend on detection of assert or not
if (listEmpty(assrtStmts)) then // no assert detected
// output
newExp = Expression.makeTuple(list( getReplacementCheckComplex(repl,cr,ty) for cr in lst_cr));
// compare types
true = checkExpsTypeEquiv(e1, newExp);
// input map cref again function args
argmap = List.zip(crefs,args);
(checkcr,_) = getInlineHashTableVarTransform();
(argmap,checkcr) = extendCrefRecords(argmap,checkcr);
// add noEvent to avoid events as usually for functions
// MSL 3.2.1 need GenerateEvents to disable this
generateEvents = hasGenerateEventsAnnotation(comment);
newExp = if not generateEvents then Expression.addNoEventToRelationsAndConds(newExp) else newExp;
(newExp,(_,_,true)) = Expression.traverseExpBottomUp(newExp,replaceArgs,(argmap,checkcr,true));
// for inlinecalls in functions
(newExp1,assrtLst) = Expression.traverseExpBottomUp(newExp,function inlineCall(fns=fns),assrtLst);
else // assert detected
true = listLength(assrtStmts) == 1;
assrt = listHead(assrtStmts);
DAE.STMT_ASSERT() = assrt;
//newExp = getReplacementCheckComplex(repl,cr,ty); // the function that replaces the output variable
newExp = Expression.makeTuple(list( getReplacementCheckComplex(repl,cr,ty) for cr in lst_cr));
// compare types
true = checkExpsTypeEquiv(e1, newExp);
argmap = List.zip(crefs,args);
(argmap,checkcr) = extendCrefRecords(argmap,checkcr);
// add noEvent to avoid events as usually for functions
// MSL 3.2.1 need GenerateEvents to disable this
generateEvents = hasGenerateEventsAnnotation(comment);
newExp = if not generateEvents then Expression.addNoEventToRelationsAndConds(newExp) else newExp;
(newExp,(_,_,true)) = Expression.traverseExpBottomUp(newExp,replaceArgs,(argmap,checkcr,true));
assrt = inlineAssert(assrt,fns,argmap,checkcr);
// for inlinecalls in functions
(newExp1,assrtLst) = Expression.traverseExpBottomUp(newExp,function inlineCall(fns=fns),assrt::assrtLst);
end if;
end if;
then
(newExp1,assrtLst);
else (exp,assrtLst);
end matchcontinue;
end inlineCall;
protected function inlineAssert "inlines an assert.
author:Waurich TUD 2013-10"
input DAE.Statement assrtIn;
input Functiontuple fns;
input list<tuple<DAE.ComponentRef, DAE.Exp>> argmap;
input HashTableCG.HashTable checkcr;
output DAE.Statement assrtOut;
protected
DAE.ElementSource source;
DAE.Exp cond, msg, level;
algorithm
DAE.STMT_ASSERT(cond=cond, msg=msg, level=level, source=source) := assrtIn;
(cond,(_,_,true)) := Expression.traverseExpBottomUp(cond,replaceArgs,(argmap,checkcr,true));
//print("ASSERT inlined: "+ExpressionDump.printExpStr(cond)+"\n");
(msg,(_,_,true)) := Expression.traverseExpBottomUp(msg,replaceArgs,(argmap,checkcr,true));
// These clear checkcr/repl and need to be performed last
// (cond,_,_,_) := inlineExp(cond,fns,source);
// (msg,_,_,_) := inlineExp(msg,fns,source);
assrtOut := DAE.STMT_ASSERT(cond, msg, level, source);
end inlineAssert;
public function hasGenerateEventsAnnotation
input Option<SCode.Comment> comment;
output Boolean b;
algorithm
b := match(comment)
local
SCode.Annotation anno;
list<SCode.Annotation> annos;
case (SOME(SCode.COMMENT(annotation_=SOME(anno))))
then
SCodeUtil.hasBooleanNamedAnnotation(anno,"GenerateEvents");
else false;
end match;
end hasGenerateEventsAnnotation;
protected function dumpArgmap
input tuple<DAE.ComponentRef, DAE.Exp> inTpl;
protected
DAE.ComponentRef cr;
DAE.Exp exp;
algorithm
(cr,exp) := inTpl;
print(ComponentReference.printComponentRefStr(cr) + " -> " + ExpressionDump.printExpStr(exp) + "\n");
end dumpArgmap;
public function forceInlineCall
"replaces an inline call with the expression from the function"
input output DAE.Exp exp;
input output list<DAE.Statement> assrtLst;
input Functiontuple fns;
input AvlSetPath.Tree visitedPaths = AvlSetPath.EMPTY();
algorithm
(exp,assrtLst) := matchcontinue exp
local
list<DAE.Element> fn;
Absyn.Path p;
list<DAE.Exp> args;
list<DAE.ComponentRef> lst_cr;
list<DAE.ComponentRef> crefs;
list<DAE.Statement> assrtStmts;
list<tuple<DAE.ComponentRef, DAE.Exp>> argmap;
DAE.Exp newExp,newExp1, e1;
DAE.InlineType inlineType;
DAE.Statement assrt;
HashTableCG.HashTable checkcr;
list<DAE.Statement> stmts;
VarTransform.VariableReplacements repl;
Boolean generateEvents,b;
Option<SCode.Comment> comment;
case (e1 as DAE.CALL(p,args,DAE.CALL_ATTR(inlineType=inlineType))) guard not AvlSetPath.hasKey(visitedPaths, p)
equation
//print(printInlineTypeStr(inlineType));
false = Config.acceptMetaModelicaGrammar();
true = checkInlineType(inlineType,fns);
(fn,comment) = getFunctionBody(p,fns);
(checkcr,repl) = getInlineHashTableVarTransform();
// get inputs, body and output
(crefs,lst_cr,stmts,repl) = getFunctionInputsOutputBody(fn,repl);
// merge statements to one line
(repl,_) = mergeFunctionBody(stmts,repl,{});
//newExp = VarTransform.getReplacement(repl,cr);
newExp = Expression.makeTuple(list( VarTransform.getReplacement(repl,cr) for cr in lst_cr));
// compare types
true = checkExpsTypeEquiv(e1, newExp);
argmap = List.zip(crefs,args);
(argmap,checkcr) = extendCrefRecords(argmap,checkcr);
// add noEvent to avoid events as usually for functions
// MSL 3.2.1 need GenerateEvents to disable this
generateEvents = hasGenerateEventsAnnotation(comment);
newExp = if not generateEvents then Expression.addNoEventToRelationsAndConds(newExp) else newExp;
(newExp,(_,_,true)) = Expression.traverseExpBottomUp(newExp,replaceArgs,(argmap,checkcr,true));
// for inlinecalls in functions
(newExp1,assrtLst) = Expression.traverseExpBottomUp(newExp,function forceInlineCall(fns=fns,visitedPaths=AvlSetPath.add(visitedPaths, p)),assrtLst);