-
Notifications
You must be signed in to change notification settings - Fork 298
/
AbsynUtil.mo
6591 lines (5899 loc) · 201 KB
/
AbsynUtil.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 AbsynUtil
protected import Absyn;
protected import Dump;
protected import Error;
protected import List;
protected import System;
protected import Util;
public constant Absyn.ClassDef dummyParts = Absyn.PARTS({},{},{},{},NONE());
public constant Absyn.Info dummyInfo = SOURCEINFO("",false,0,0,0,0,0.0);
public constant Absyn.Program dummyProgram = Absyn.PROGRAM({},Absyn.TOP());
replaceable type TypeA subtypeof Any;
replaceable type Type_a subtypeof Any;
replaceable type Argument subtypeof Any;
replaceable type Arg subtypeof Any;
// stefan
public function traverseEquation
"Traverses all subequations of an equation.
Takes a function and an extra argument passed through the traversal"
input Absyn.Equation inEquation;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<Absyn.Equation, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Equation, TypeA> inTpl;
output tuple<Absyn.Equation, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := matchcontinue (inEquation,inFunc,inTypeA)
local
TypeA arg,arg_1,arg_2,arg_3,arg_4;
Absyn.Equation eq,eq_1;
FuncTplToTpl rel;
Absyn.Exp e,e_1;
list<Absyn.EquationItem> eqilst,eqilst1,eqilst2,eqilst_1,eqilst1_1,eqilst2_1;
list<tuple<Absyn.Exp, list<Absyn.EquationItem>>> eeqitlst,eeqitlst_1;
Absyn.ForIterators fis,fis_1;
Absyn.EquationItem ei,ei_1;
case(eq as Absyn.EQ_IF(e,eqilst1,eeqitlst,eqilst2),rel,arg)
equation
((eqilst1_1,arg_1)) = traverseEquationItemList(eqilst1,rel,arg);
((eeqitlst_1,arg_2)) = traverseExpEqItemTupleList(eeqitlst,rel,arg_1);
((eqilst2_1,arg_3)) = traverseEquationItemList(eqilst2,rel,arg_2);
((Absyn.EQ_IF(),arg_4)) = rel((eq,arg_3));
then
((Absyn.EQ_IF(e,eqilst1_1,eeqitlst_1,eqilst2_1),arg_4));
case(eq as Absyn.EQ_FOR(_,eqilst),rel,arg)
equation
((eqilst_1,arg_1)) = traverseEquationItemList(eqilst,rel,arg);
((Absyn.EQ_FOR(fis_1,_),arg_2)) = rel((eq,arg_1));
then
((Absyn.EQ_FOR(fis_1,eqilst_1),arg_2));
case(eq as Absyn.EQ_WHEN_E(_,eqilst,eeqitlst),rel,arg)
equation
((eqilst_1,arg_1)) = traverseEquationItemList(eqilst,rel,arg);
((eeqitlst_1,arg_2)) = traverseExpEqItemTupleList(eeqitlst,rel,arg_1);
((Absyn.EQ_WHEN_E(e_1,_,_),arg_3)) = rel((eq,arg_2));
then
((Absyn.EQ_WHEN_E(e_1,eqilst_1,eeqitlst_1),arg_3));
case(eq as Absyn.EQ_FAILURE(ei),rel,arg)
equation
((ei_1,arg_1)) = traverseEquationItem(ei,rel,arg);
((Absyn.EQ_FAILURE(),arg_2)) = rel((eq,arg_1));
then
((Absyn.EQ_FAILURE(ei_1),arg_2));
case(eq,rel,arg)
equation
((eq_1,arg_1)) = rel((eq,arg));
then
((eq_1,arg_1));
end matchcontinue;
end traverseEquation;
// stefan
protected function traverseEquationItem
"Traverses the equation inside an equationitem"
input Absyn.EquationItem inEquationItem;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<Absyn.EquationItem, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Equation, TypeA> inTpl;
output tuple<Absyn.Equation, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := matchcontinue (inEquationItem,inFunc,inTypeA)
local
Absyn.EquationItem ei;
FuncTplToTpl rel;
TypeA arg,arg_1;
Absyn.Equation eq,eq_1;
Option<Absyn.Comment> oc;
Absyn.Info info;
case(Absyn.EQUATIONITEM(eq,oc,info),rel,arg)
equation
((eq_1,arg_1)) = traverseEquation(eq,rel,arg);
then
((Absyn.EQUATIONITEM(eq_1,oc,info),arg_1));
case(ei,_,arg) then ((ei,arg));
end matchcontinue;
end traverseEquationItem;
// stefan
public function traverseEquationItemList
"calls traverseEquationItem on every element of the given list"
input list<Absyn.EquationItem> inEquationItemList;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<list<Absyn.EquationItem>, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Equation, TypeA> inTpl;
output tuple<Absyn.Equation, TypeA> outTpl;
end FuncTplToTpl;
protected
TypeA arg2 = inTypeA;
algorithm
outTpl := (list(match el
local
Absyn.EquationItem ei,ei_1;
case (ei) equation
((ei_1,arg2)) = traverseEquationItem(ei,inFunc,arg2);
then ei_1;
end match for el in inEquationItemList), arg2);
end traverseEquationItemList;
// stefan
public function traverseExpEqItemTupleList
"traverses a list of Absyn.Exp * Absyn.EquationItem list tuples
mostly used for else-if blocks"
input list<tuple<Absyn.Exp, list<Absyn.EquationItem>>> inList;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<list<tuple<Absyn.Exp, list<Absyn.EquationItem>>>, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Equation, TypeA> inTpl;
output tuple<Absyn.Equation, TypeA> outTpl;
end FuncTplToTpl;
protected
TypeA arg2 = inTypeA;
algorithm
outTpl := (list(match el
local
Absyn.Exp e;
list<Absyn.EquationItem> eilst,eilst_1;
case (e,eilst) equation
((eilst_1,arg2)) = traverseEquationItemList(eilst,inFunc,arg2);
then (e,eilst_1);
end match for el in inList), arg2);
end traverseExpEqItemTupleList;
// stefan
public function traverseAlgorithm
"Traverses all subalgorithms of an algorithm
Takes a function and an extra argument passed through the traversal"
input Absyn.Algorithm inAlgorithm;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<Absyn.Algorithm, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Algorithm, TypeA> inTpl;
output tuple<Absyn.Algorithm, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := matchcontinue (inAlgorithm,inFunc,inTypeA)
local
TypeA arg,arg_1,arg1_1,arg2_1,arg3_1;
Absyn.Algorithm alg,alg_1,alg1_1,alg2_1,alg3_1;
list<Absyn.AlgorithmItem> ailst,ailst1,ailst2,ailst_1,ailst1_1,ailst2_1;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> eaitlst,eaitlst_1;
FuncTplToTpl rel;
Absyn.AlgorithmItem ai,ai_1;
Absyn.Exp e,e_1;
Absyn.ForIterators fis,fis_1;
case(alg as Absyn.ALG_IF(_,ailst1,eaitlst,ailst2),rel,arg)
equation
((ailst1_1,arg1_1)) = traverseAlgorithmItemList(ailst1,rel,arg);
((eaitlst_1,arg2_1)) = traverseExpAlgItemTupleList(eaitlst,rel,arg1_1);
((ailst2_1,arg3_1)) = traverseAlgorithmItemList(ailst2,rel,arg2_1);
((Absyn.ALG_IF(e_1,_,_,_),arg_1)) = rel((alg,arg3_1));
then
((Absyn.ALG_IF(e_1,ailst1_1,eaitlst_1,ailst2_1),arg_1));
case(alg as Absyn.ALG_FOR(_,ailst),rel,arg)
equation
((ailst_1,arg1_1)) = traverseAlgorithmItemList(ailst,rel,arg);
((Absyn.ALG_FOR(fis_1,_),arg_1)) = rel((alg,arg1_1));
then
((Absyn.ALG_FOR(fis_1,ailst_1),arg_1));
case(alg as Absyn.ALG_PARFOR(_,ailst),rel,arg)
equation
((ailst_1,arg1_1)) = traverseAlgorithmItemList(ailst,rel,arg);
((Absyn.ALG_PARFOR(fis_1,_),arg_1)) = rel((alg,arg1_1));
then
((Absyn.ALG_PARFOR(fis_1,ailst_1),arg_1));
case(alg as Absyn.ALG_WHILE(_,ailst),rel,arg)
equation
((ailst_1,arg1_1)) = traverseAlgorithmItemList(ailst,rel,arg);
((Absyn.ALG_WHILE(e_1,_),arg_1)) = rel((alg,arg1_1));
then
((Absyn.ALG_WHILE(e_1,ailst_1),arg_1));
case(alg as Absyn.ALG_WHEN_A(_,ailst,eaitlst),rel,arg)
equation
((ailst_1,arg1_1)) = traverseAlgorithmItemList(ailst,rel,arg);
((eaitlst_1,arg2_1)) = traverseExpAlgItemTupleList(eaitlst,rel,arg1_1);
((Absyn.ALG_WHEN_A(e_1,_,_),arg_1)) = rel((alg,arg2_1));
then
((Absyn.ALG_WHEN_A(e_1,ailst_1,eaitlst_1),arg_1));
case(alg,rel,arg)
equation
((alg_1,arg_1)) = rel((alg,arg));
then
((alg_1,arg_1));
end matchcontinue;
end traverseAlgorithm;
// stefan
public function traverseAlgorithmItem
"traverses the Absyn.Algorithm contained in an Absyn.AlgorithmItem, if any
see traverseAlgorithm"
input Absyn.AlgorithmItem inAlgorithmItem;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<Absyn.AlgorithmItem, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Algorithm, TypeA> inTpl;
output tuple<Absyn.Algorithm, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := matchcontinue (inAlgorithmItem,inFunc,inTypeA)
local
FuncTplToTpl rel;
TypeA arg,arg_1;
Absyn.Algorithm alg,alg_1;
Option<Absyn.Comment> oc;
Absyn.AlgorithmItem ai;
Absyn.Info info;
case(Absyn.ALGORITHMITEM(alg,oc,info),rel,arg)
equation
((alg_1,arg_1)) = traverseAlgorithm(alg,rel,arg);
then
((Absyn.ALGORITHMITEM(alg_1,oc,info),arg_1));
case(ai,_,arg) then ((ai,arg));
end matchcontinue;
end traverseAlgorithmItem;
// stefan
public function traverseAlgorithmItemList
"calls traverseAlgorithmItem on each item in a list of AlgorithmItems"
input list<Absyn.AlgorithmItem> inAlgorithmItemList;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<list<Absyn.AlgorithmItem>, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Algorithm, TypeA> inTpl;
output tuple<Absyn.Algorithm, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := match (inAlgorithmItemList,inFunc,inTypeA)
local
FuncTplToTpl rel;
TypeA arg,arg_1,arg_2;
Absyn.AlgorithmItem ai,ai_1;
list<Absyn.AlgorithmItem> cdr,cdr_1;
case({},_,arg) then (({},arg));
case(ai :: cdr,rel,arg)
equation
((ai_1,arg_1)) = traverseAlgorithmItem(ai,rel,arg);
((cdr_1,arg_2)) = traverseAlgorithmItemList(cdr,rel,arg_1);
then
((ai_1 :: cdr_1,arg_2));
end match;
end traverseAlgorithmItemList;
// stefan
public function traverseExpAlgItemTupleList
"traverses a list of Absyn.Exp * Absyn.AlgorithmItem list tuples
mostly used for else-if blocks"
input list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> inList;
input FuncTplToTpl inFunc;
input TypeA inTypeA;
output tuple<list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>>, TypeA> outTpl;
partial function FuncTplToTpl
input tuple<Absyn.Algorithm, TypeA> inTpl;
output tuple<Absyn.Algorithm, TypeA> outTpl;
end FuncTplToTpl;
algorithm
outTpl := match (inList,inFunc,inTypeA)
local
FuncTplToTpl rel;
TypeA arg,arg_1,arg_2;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> cdr,cdr_1;
Absyn.Exp e;
list<Absyn.AlgorithmItem> ailst,ailst_1;
case({},_,arg) then (({},arg));
case((e,ailst) :: cdr,rel,arg)
equation
((ailst_1,arg_1)) = traverseAlgorithmItemList(ailst,rel,arg);
((cdr_1,arg_2)) = traverseExpAlgItemTupleList(cdr,rel,arg_1);
then
(((e,ailst_1) :: cdr_1,arg_2));
end match;
end traverseExpAlgItemTupleList;
public function traverseExp
" Traverses all subexpressions of an Absyn.Exp expression.
Takes a function and an extra argument passed through the traversal.
NOTE:This function was copied from Expression.traverseExpression."
input Absyn.Exp inExp;
input FuncType inFunc;
input Type_a inArg;
output Absyn.Exp outExp;
output Type_a outArg;
partial function FuncType
input Absyn.Exp inExp;
input Type_a inArg;
output Absyn.Exp outExp;
output Type_a outArg;
end FuncType;
algorithm
(outExp,outArg) := traverseExpBidir(inExp,dummyTraverseExp,inFunc,inArg);
end traverseExp;
public function traverseExpTopDown
" Traverses all subexpressions of an Absyn.Exp expression.
Takes a function and an extra argument passed through the traversal."
input Absyn.Exp inExp;
input FuncType inFunc;
input Type_a inArg;
output Absyn.Exp outExp;
output Type_a outArg;
partial function FuncType
input Absyn.Exp inExp;
input Type_a inArg;
output Absyn.Exp outExp;
output Type_a outArg;
end FuncType;
algorithm
(outExp,outArg) := traverseExpBidir(inExp,inFunc,dummyTraverseExp,inArg);
end traverseExpTopDown;
public function traverseExpList
"calls traverseExp on each element in the given list"
input list<Absyn.Exp> inExpList;
input FuncTplToTpl inFunc;
input Type_a inArg;
output list<Absyn.Exp> outExpList;
output Type_a outArg;
partial function FuncTplToTpl
input Absyn.Exp inExp;
input Type_a inArg;
output Absyn.Exp outExp;
output Type_a outArg;
end FuncTplToTpl;
algorithm
(outExpList,outArg) := traverseExpListBidir(inExpList,dummyTraverseExp,inFunc,inArg);
end traverseExpList;
public function traverseExpListBidir
"Traverses a list of expressions, calling traverseExpBidir on each
expression."
input list<Absyn.Exp> inExpl;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output list<Absyn.Exp> outExpl;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outExpl, outArg) := List.map2FoldCheckReferenceEq(inExpl, traverseExpBidir, enterFunc, exitFunc, inArg);
end traverseExpListBidir;
public function traverseExpBidir
"This function takes an expression and a tuple with an enter function, an exit
function, and an extra argument. For each expression it encounters it calls
the enter function with the expression and the extra argument. It then
traverses all subexpressions in the expression and calls traverseExpBidir on
them with the updated argument. Finally it calls the exit function, again with
the updated argument. This means that this function is bidirectional, and can
be used to emulate both top-down and bottom-up traversal."
input Absyn.Exp inExp;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.Exp e;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(e, arg) := enterFunc(inExp, inArg);
(e, arg) := traverseExpBidirSubExps(e, enterFunc, exitFunc, arg);
(e, arg) := exitFunc(e, arg);
end traverseExpBidir;
public function traverseExpOptBidir
"Same as traverseExpBidir, but with an optional expression. Calls
traverseExpBidir if the option is SOME(), or just returns the input if it's
NONE()"
input Option<Absyn.Exp> inExp;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Option<Absyn.Exp> outExp;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outExp, arg) := match(inExp, enterFunc, exitFunc, inArg)
local
Absyn.Exp e1,e2;
tuple<FuncType, FuncType, Argument> tup;
case (SOME(e1), _, _, _)
equation
(e2, arg) = traverseExpBidir(e1, enterFunc, exitFunc, inArg);
then
(if referenceEq(e1,e2) then inExp else SOME(e2), arg);
else (inExp, inArg);
end match;
end traverseExpOptBidir;
protected function traverseExpBidirSubExps
"Helper function to traverseExpBidir. Traverses the subexpressions of an
expression and calls traverseExpBidir on them."
input Absyn.Exp inExp;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.Exp e;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(e, arg) := match (inExp, enterFunc, exitFunc, inArg)
local
Absyn.Exp e1, e1m, e2, e2m, e3, e3m;
Option<Absyn.Exp> oe1, oe1m;
tuple<FuncType, FuncType, Argument> tup;
Absyn.Operator op;
Absyn.ComponentRef cref, crefm;
list<tuple<Absyn.Exp, Absyn.Exp>> else_ifs1,else_ifs2;
list<Absyn.Exp> expl1,expl2;
list<list<Absyn.Exp>> mat_expl;
Absyn.FunctionArgs fargs1,fargs2;
String error_msg;
Absyn.Ident id, enterName, exitName;
Absyn.MatchType match_ty;
list<Absyn.ElementItem> match_decls;
list<Absyn.Case> match_cases;
Option<String> cmt;
case (Absyn.INTEGER(), _, _, _) then (inExp, inArg);
case (Absyn.REAL(), _, _, _) then (inExp, inArg);
case (Absyn.STRING(), _, _, _) then (inExp, inArg);
case (Absyn.BOOL(), _, _, _) then (inExp, inArg);
case (Absyn.CREF(componentRef = cref), _, _, arg)
equation
(crefm, arg) = traverseExpBidirCref(cref, enterFunc, exitFunc, arg);
then
(if referenceEq(cref,crefm) then inExp else Absyn.CREF(crefm), arg);
case (Absyn.BINARY(exp1 = e1, op = op, exp2 = e2), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) then inExp else Absyn.BINARY(e1m, op, e2m), arg);
case (Absyn.UNARY(op = op, exp = e1), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then inExp else Absyn.UNARY(op, e1m), arg);
case (Absyn.LBINARY(exp1 = e1, op = op, exp2 = e2), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) then inExp else Absyn.LBINARY(e1m, op, e2m), arg);
case (Absyn.LUNARY(op = op, exp = e1), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then inExp else Absyn.LUNARY(op, e1m), arg);
case (Absyn.RELATION(exp1 = e1, op = op, exp2 = e2), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) then inExp else Absyn.RELATION(e1m, op, e2m), arg);
case (Absyn.IFEXP(ifExp = e1, trueBranch = e2, elseBranch = e3,
elseIfBranch = else_ifs1), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
(e3m, arg) = traverseExpBidir(e3, enterFunc, exitFunc, arg);
(else_ifs2, arg) = List.map2FoldCheckReferenceEq(else_ifs1, traverseExpBidirElseIf, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) and referenceEq(e3,e3m) and referenceEq(else_ifs1,else_ifs2) then inExp else Absyn.IFEXP(e1m, e2m, e3m, else_ifs2), arg);
case (Absyn.CALL(function_ = cref, functionArgs = fargs1), _, _, arg)
equation
(fargs2, arg) = traverseExpBidirFunctionArgs(fargs1, enterFunc, exitFunc, arg);
then
(if referenceEq(fargs1,fargs2) then inExp else Absyn.CALL(cref, fargs2), arg);
case (Absyn.PARTEVALFUNCTION(function_ = cref, functionArgs = fargs1), _, _, arg)
equation
(fargs2, arg) = traverseExpBidirFunctionArgs(fargs1, enterFunc, exitFunc, arg);
then
(if referenceEq(fargs1,fargs2) then inExp else Absyn.PARTEVALFUNCTION(cref, fargs2), arg);
case (Absyn.ARRAY(arrayExp = expl1), _, _, arg)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then inExp else Absyn.ARRAY(expl2), arg);
case (Absyn.MATRIX(matrix = mat_expl), _, _, arg)
equation
(mat_expl, arg) = List.map2FoldCheckReferenceEq(mat_expl, traverseExpListBidir, enterFunc, exitFunc, arg);
then
(Absyn.MATRIX(mat_expl), arg);
case (Absyn.RANGE(start = e1, step = oe1, stop = e2), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(oe1m, arg) = traverseExpOptBidir(oe1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) and referenceEq(oe1,oe1m) then inExp else Absyn.RANGE(e1m, oe1m, e2m), arg);
case (Absyn.END(), _, _, _) then (inExp, inArg);
case (Absyn.TUPLE(expressions = expl1), _, _, arg)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then inExp else Absyn.TUPLE(expl2), arg);
case (Absyn.AS(id = id, exp = e1), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then inExp else Absyn.AS(id, e1m), arg);
case (Absyn.CONS(head = e1, rest = e2), _, _, arg)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2m, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) and referenceEq(e2,e2m) then inExp else Absyn.CONS(e1m, e2m), arg);
case (Absyn.MATCHEXP(matchTy = match_ty, inputExp = e1, localDecls = match_decls,
cases = match_cases, comment = cmt), _, _, arg)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(match_cases, arg) = List.map2FoldCheckReferenceEq(match_cases, traverseMatchCase, enterFunc, exitFunc, arg);
then
(Absyn.MATCHEXP(match_ty, e1, match_decls, match_cases, cmt), arg);
case (Absyn.LIST(exps = expl1), _, _, arg)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then inExp else Absyn.LIST(expl2), arg);
case (Absyn.CODE(), _, _, _)
then (inExp, inArg);
case (Absyn.DOT(), _, _, arg)
equation
(e1, arg) = traverseExpBidir(inExp.exp, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(inExp.index, enterFunc, exitFunc, arg);
then
(if referenceEq(inExp.exp,e1) and referenceEq(inExp.index,e2) then inExp else Absyn.DOT(e1, e2), arg);
else
algorithm
(,,enterName) := System.dladdr(enterFunc);
(,,exitName) := System.dladdr(exitFunc);
error_msg := "in traverseExpBidirSubExps(" + enterName + ", " + exitName + ") - Unknown expression: ";
error_msg := error_msg + Dump.printExpStr(inExp);
Error.addMessage(Error.INTERNAL_ERROR, {error_msg});
then
fail();
end match;
end traverseExpBidirSubExps;
public function traverseExpBidirCref
"Helper function to traverseExpBidirSubExps. Traverses any expressions in a
component reference (i.e. in it's subscripts)."
input Absyn.ComponentRef inCref;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.ComponentRef outCref;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outCref, arg) := match(inCref, enterFunc, exitFunc, inArg)
local
Absyn.Ident name;
Absyn.ComponentRef cr1,cr2;
list<Absyn.Subscript> subs1,subs2;
tuple<FuncType, FuncType, Argument> tup;
case (Absyn.CREF_FULLYQUALIFIED(componentRef = cr1), _, _, arg)
equation
(cr2, arg) = traverseExpBidirCref(cr1, enterFunc, exitFunc, arg);
then
(if referenceEq(cr1,cr2) then inCref else crefMakeFullyQualified(cr2), arg);
case (Absyn.CREF_QUAL(name = name, subscripts = subs1, componentRef = cr1), _, _, arg)
equation
(subs2, arg) = List.map2FoldCheckReferenceEq(subs1, traverseExpBidirSubs, enterFunc, exitFunc, arg);
(cr2, arg) = traverseExpBidirCref(cr1, enterFunc, exitFunc, arg);
then
(if referenceEq(cr1,cr2) and referenceEq(subs1,subs2) then inCref else Absyn.CREF_QUAL(name, subs2, cr2), arg);
case (Absyn.CREF_IDENT(name = name, subscripts = subs1), _, _, arg)
equation
(subs2, arg) = List.map2FoldCheckReferenceEq(subs1, traverseExpBidirSubs, enterFunc, exitFunc, arg);
then
(if referenceEq(subs1,subs2) then inCref else Absyn.CREF_IDENT(name, subs2), arg);
case (Absyn.ALLWILD(), _, _, _) then (inCref, inArg);
case (Absyn.WILD(), _, _, _) then (inCref, inArg);
end match;
end traverseExpBidirCref;
public function traverseExpBidirSubs
"Helper function to traverseExpBidirCref. Traverses expressions in a
subscript."
input Absyn.Subscript inSubscript;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.Subscript outSubscript;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outSubscript, arg) := match(inSubscript, enterFunc, exitFunc, inArg)
local
Absyn.Exp e1,e2;
case (Absyn.SUBSCRIPT(subscript = e1), _, _, arg)
equation
(e2, arg) = traverseExpBidir(e1, enterFunc, exitFunc, inArg);
then
(if referenceEq(e1,e2) then inSubscript else Absyn.SUBSCRIPT(e2), arg);
case (Absyn.NOSUB(), _, _, _) then (inSubscript, inArg);
end match;
end traverseExpBidirSubs;
public function traverseExpBidirElseIf
"Helper function to traverseExpBidirSubExps. Traverses the expressions in an
elseif branch."
input tuple<Absyn.Exp, Absyn.Exp> inElseIf;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output tuple<Absyn.Exp, Absyn.Exp> outElseIf;
output Argument arg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
protected
Absyn.Exp e1, e2;
tuple<FuncType, FuncType, Argument> tup;
algorithm
(e1, e2) := inElseIf;
(e1, arg) := traverseExpBidir(e1, enterFunc, exitFunc, inArg);
(e2, arg) := traverseExpBidir(e2, enterFunc, exitFunc, arg);
outElseIf := (e1, e2);
end traverseExpBidirElseIf;
public function traverseExpBidirFunctionArgs
"Helper function to traverseExpBidirSubExps. Traverses the expressions in a
list of function argument."
input Absyn.FunctionArgs inArgs;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.FunctionArgs outArgs;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outArgs, outArg) := match(inArgs, enterFunc, exitFunc, inArg)
local
Absyn.Exp e1,e2;
list<Absyn.Exp> expl1,expl2;
list<Absyn.NamedArg> named_args1,named_args2;
Absyn.ForIterators iters1,iters2;
Argument arg;
Absyn.ReductionIterType iterType;
case (Absyn.FUNCTIONARGS(args = expl1, argNames = named_args1), _, _, arg)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
(named_args2, arg) = List.map2FoldCheckReferenceEq(named_args1, traverseExpBidirNamedArg, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) and referenceEq(named_args1,named_args2) then inArgs else Absyn.FUNCTIONARGS(expl2, named_args2), arg);
case (Absyn.FOR_ITER_FARG(e1, iterType, iters1), _, _, arg)
equation
(e2, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(iters2, arg) = List.map2FoldCheckReferenceEq(iters1, traverseExpBidirIterator, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e2) and referenceEq(iters1,iters2) then inArgs else Absyn.FOR_ITER_FARG(e2, iterType, iters2), arg);
end match;
end traverseExpBidirFunctionArgs;
public function traverseExpBidirNamedArg
"Helper function to traverseExpBidirFunctionArgs. Traverses the expressions in
a named function argument."
input Absyn.NamedArg inArg;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inExtra;
output Absyn.NamedArg outArg;
output Argument outExtra;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
protected
Absyn.Ident name;
Absyn.Exp value1,value2;
algorithm
Absyn.NAMEDARG(name, value1) := inArg;
(value2, outExtra) := traverseExpBidir(value1, enterFunc, exitFunc, inExtra);
outArg := if referenceEq(value1,value2) then inArg else Absyn.NAMEDARG(name, value2);
end traverseExpBidirNamedArg;
public function traverseExpBidirIterator
"Helper function to traverseExpBidirFunctionArgs. Traverses the expressions in
an iterator."
input Absyn.ForIterator inIterator;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.ForIterator outIterator;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
protected
Absyn.Ident name;
Option<Absyn.Exp> guardExp1,guardExp2,range1,range2;
algorithm
Absyn.ITERATOR(name=name, guardExp=guardExp1, range=range1) := inIterator;
(guardExp2, outArg) := traverseExpOptBidir(guardExp1, enterFunc, exitFunc, inArg);
(range2, outArg) := traverseExpOptBidir(range1, enterFunc, exitFunc, outArg);
outIterator := if referenceEq(guardExp1,guardExp2) and referenceEq(range1,range2) then inIterator else Absyn.ITERATOR(name, guardExp2, range2);
end traverseExpBidirIterator;
public function traverseMatchCase
input Absyn.Case inMatchCase;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.Case outMatchCase;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outMatchCase, outArg) := match(inMatchCase, enterFunc, exitFunc, inArg)
local
Argument arg;
Absyn.Exp pattern, result;
Absyn.Info info, resultInfo, pinfo;
list<Absyn.ElementItem> ldecls;
Absyn.ClassPart cp;
Option<String> cmt;
Option<Absyn.Exp> patternGuard;
case (Absyn.CASE(pattern, patternGuard, pinfo, ldecls, cp, result, resultInfo, cmt, info), _, _, arg)
equation
(pattern, arg) = traverseExpBidir(pattern, enterFunc, exitFunc, arg);
(patternGuard, arg) = traverseExpOptBidir(patternGuard, enterFunc, exitFunc, arg);
(cp, arg) = traverseClassPartBidir(cp, enterFunc, exitFunc, arg);
(result, arg) = traverseExpBidir(result, enterFunc, exitFunc, arg);
then
(Absyn.CASE(pattern, patternGuard, pinfo, ldecls, cp, result, resultInfo, cmt, info), arg);
case (Absyn.ELSE(localDecls = ldecls, classPart = cp, result = result, resultInfo = resultInfo,
comment = cmt, info = info), _, _, arg)
equation
(cp, arg) = traverseClassPartBidir(cp, enterFunc, exitFunc, arg);
(result, arg) = traverseExpBidir(result, enterFunc, exitFunc, arg);
then
(Absyn.ELSE(ldecls, cp, result, resultInfo, cmt, info), arg);
end match;
end traverseMatchCase;
protected function traverseClassPartBidir
input Absyn.ClassPart cp;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.ClassPart outCp;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outCp, outArg) := match (cp,enterFunc,exitFunc,inArg)
local
list<Absyn.AlgorithmItem> algs;
list<Absyn.EquationItem> eqs;
Argument arg;
case (Absyn.ALGORITHMS(algs),_,_,arg)
equation
(algs, arg) = List.map2FoldCheckReferenceEq(algs, traverseAlgorithmItemBidir, enterFunc, exitFunc, arg);
then (Absyn.ALGORITHMS(algs),arg);
case (Absyn.EQUATIONS(eqs),_,_,arg)
equation
(eqs, arg) = List.map2FoldCheckReferenceEq(eqs, traverseEquationItemBidir, enterFunc, exitFunc, arg);
then (Absyn.EQUATIONS(eqs),arg);
end match;
end traverseClassPartBidir;
protected function traverseEquationItemListBidir
input list<Absyn.EquationItem> inEquationItems;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output list<Absyn.EquationItem> outEquationItems;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outEquationItems, outArg) := List.map2FoldCheckReferenceEq(inEquationItems, traverseEquationItemBidir, enterFunc, exitFunc, inArg);
end traverseEquationItemListBidir;
protected function traverseAlgorithmItemListBidir
input list<Absyn.AlgorithmItem> inAlgs;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output list<Absyn.AlgorithmItem> outAlgs;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outAlgs, outArg) := List.map2FoldCheckReferenceEq(inAlgs, traverseAlgorithmItemBidir, enterFunc, exitFunc, inArg);
end traverseAlgorithmItemListBidir;
protected function traverseAlgorithmItemBidir
input Absyn.AlgorithmItem inAlgorithmItem;
input FuncType enterFunc;
input FuncType exitFunc;
input Argument inArg;
output Absyn.AlgorithmItem outAlgorithmItem;
output Argument outArg;
partial function FuncType
input Absyn.Exp inExp;
input Argument inArg;
output Absyn.Exp outExp;
output Argument outArg;
end FuncType;
algorithm
(outAlgorithmItem, outArg) := match(inAlgorithmItem, enterFunc, exitFunc, inArg)
local
Argument arg;
Absyn.Algorithm alg;
Option<Absyn.Comment> cmt;
Absyn.Info info;
case (Absyn.ALGORITHMITEM(algorithm_ = alg, comment = cmt, info = info), _, _, arg)
equation
(alg, arg) = traverseAlgorithmBidir(alg, enterFunc, exitFunc, arg);
then