-
Notifications
You must be signed in to change notification settings - Fork 297
/
AbsynUtil.mo
6041 lines (5370 loc) · 181 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;
import Dump;
import Error;
import List;
import System;
import Util;
import MetaModelica.Dangerous.listReverseInPlace;
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());
public function traverseExp<Arg>
" 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 Arg inArg;
output Absyn.Exp outExp;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outExp,outArg) := traverseExpBidir(inExp,dummyTraverseExp,inFunc,inArg);
end traverseExp;
public function traverseExpTopDown<Arg>
" 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 Arg inArg;
output Absyn.Exp outExp;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outExp,outArg) := traverseExpBidir(inExp,inFunc,dummyTraverseExp,inArg);
end traverseExpTopDown;
public function traverseExpList<Arg>
"calls traverseExp on each element in the given list"
input list<Absyn.Exp> inExpList;
input FuncType inFunc;
input Arg inArg;
output list<Absyn.Exp> outExpList;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outExpList,outArg) := traverseExpListBidir(inExpList,dummyTraverseExp,inFunc,inArg);
end traverseExpList;
public function traverseExpListBidir<Arg>
"Traverses a list of expressions, calling traverseExpBidir on each
expression."
input list<Absyn.Exp> inExpl;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output list<Absyn.Exp> outExpl;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outExpl, outArg) := List.map2FoldCheckReferenceEq(inExpl, traverseExpBidir, enterFunc, exitFunc, inArg);
end traverseExpListBidir;
public function traverseExpBidir<Arg>
"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 Arg inArg;
output Absyn.Exp e;
output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
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<Arg>
"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 Arg inArg;
output Option<Absyn.Exp> outExp;
output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outExp, arg) := match inExp
local
Absyn.Exp e1,e2;
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<Arg>
"Helper function to traverseExpBidir. Traverses the subexpressions of an
expression and calls traverseExpBidir on them."
input output Absyn.Exp exp;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(exp, arg) := match exp
local
Absyn.Exp e1, e1m, e2, e2m, e3, e3m;
Option<Absyn.Exp> oe1, oe1m;
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;
list<Absyn.Case> match_cases;
case Absyn.INTEGER() then (exp, arg);
case Absyn.REAL() then (exp, arg);
case Absyn.STRING() then (exp, arg);
case Absyn.BOOL() then (exp, arg);
case Absyn.CREF(componentRef = cref)
equation
(crefm, arg) = traverseExpBidirCref(cref, enterFunc, exitFunc, arg);
then
(if referenceEq(cref,crefm) then exp else Absyn.CREF(crefm), arg);
case Absyn.BINARY(exp1 = e1, exp2 = e2)
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 exp else Absyn.BINARY(e1m, exp.op, e2m), arg);
case Absyn.UNARY(exp = e1)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then exp else Absyn.UNARY(exp.op, e1m), arg);
case Absyn.LBINARY(exp1 = e1, exp2 = e2)
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 exp else Absyn.LBINARY(e1m, exp.op, e2m), arg);
case Absyn.LUNARY(exp = e1)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then exp else Absyn.LUNARY(exp.op, e1m), arg);
case Absyn.RELATION(exp1 = e1, exp2 = e2)
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 exp else Absyn.RELATION(e1m, exp.op, e2m), arg);
case Absyn.IFEXP(ifExp = e1, trueBranch = e2, elseBranch = e3, elseIfBranch = else_ifs1)
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 exp else Absyn.IFEXP(e1m, e2m, e3m, else_ifs2), arg);
case Absyn.CALL(function_ = cref, functionArgs = fargs1)
equation
(fargs2, arg) = traverseExpBidirFunctionArgs(fargs1, enterFunc, exitFunc, arg);
then
(if referenceEq(fargs1,fargs2) then exp else Absyn.CALL(cref, fargs2, exp.typeVars), arg);
case Absyn.PARTEVALFUNCTION(function_ = cref, functionArgs = fargs1)
equation
(fargs2, arg) = traverseExpBidirFunctionArgs(fargs1, enterFunc, exitFunc, arg);
then
(if referenceEq(fargs1,fargs2) then exp else Absyn.PARTEVALFUNCTION(cref, fargs2), arg);
case Absyn.ARRAY(arrayExp = expl1)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then exp else Absyn.ARRAY(expl2), arg);
case Absyn.MATRIX(matrix = mat_expl)
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)
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 exp else Absyn.RANGE(e1m, oe1m, e2m), arg);
case Absyn.END() then (exp, arg);
case Absyn.TUPLE(expressions = expl1)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then exp else Absyn.TUPLE(expl2), arg);
case Absyn.AS(id = id, exp = e1)
equation
(e1m, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e1m) then exp else Absyn.AS(id, e1m), arg);
case Absyn.CONS(head = e1, rest = e2)
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 exp else Absyn.CONS(e1m, e2m), arg);
case Absyn.MATCHEXP(inputExp = e1, cases = match_cases)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(match_cases, arg) = List.map2FoldCheckReferenceEq(match_cases, traverseMatchCase, enterFunc, exitFunc, arg);
then
(Absyn.MATCHEXP(exp.matchTy, e1, exp.localDecls, match_cases, exp.comment), arg);
case Absyn.LIST(exps = expl1)
equation
(expl2, arg) = traverseExpListBidir(expl1, enterFunc, exitFunc, arg);
then
(if referenceEq(expl1,expl2) then exp else Absyn.LIST(expl2), arg);
case Absyn.CODE()
then (exp, arg);
case Absyn.DOT()
equation
(e1, arg) = traverseExpBidir(exp.exp, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(exp.index, enterFunc, exitFunc, arg);
then
(if referenceEq(exp.exp,e1) and referenceEq(exp.index,e2) then exp else Absyn.DOT(e1, e2), arg);
case Absyn.EXPRESSIONCOMMENT()
equation
(e1, arg) = traverseExpBidir(exp.exp, enterFunc, exitFunc, arg);
then
(if referenceEq(exp.exp,e1) then exp else Absyn.EXPRESSIONCOMMENT(exp.commentsBefore, e1, exp.commentsAfter), 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(exp);
Error.addMessage(Error.INTERNAL_ERROR, {error_msg});
then
fail();
end match;
end traverseExpBidirSubExps;
public function traverseExpBidirCref<Arg>
"Helper function to traverseExpBidirSubExps. Traverses any expressions in a
component reference (i.e. in it's subscripts)."
input output Absyn.ComponentRef cref;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(cref, arg) := match cref
local
Absyn.Ident name;
Absyn.ComponentRef cr1,cr2;
list<Absyn.Subscript> subs1,subs2;
case Absyn.CREF_FULLYQUALIFIED(componentRef = cr1)
equation
(cr2, arg) = traverseExpBidirCref(cr1, enterFunc, exitFunc, arg);
then
(if referenceEq(cr1,cr2) then cref else crefMakeFullyQualified(cr2), arg);
case Absyn.CREF_QUAL(name = name, subscripts = subs1, componentRef = cr1)
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 cref else Absyn.CREF_QUAL(name, subs2, cr2), arg);
case Absyn.CREF_IDENT(name = name, subscripts = subs1)
equation
(subs2, arg) = List.map2FoldCheckReferenceEq(subs1, traverseExpBidirSubs, enterFunc, exitFunc, arg);
then
(if referenceEq(subs1,subs2) then cref else Absyn.CREF_IDENT(name, subs2), arg);
case Absyn.ALLWILD() then (cref, arg);
case Absyn.WILD() then (cref, arg);
end match;
end traverseExpBidirCref;
public function traverseExpBidirSubs<Arg>
"Helper function to traverseExpBidirCref. Traverses expressions in a
subscript."
input output Absyn.Subscript subscript;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(subscript, arg) := match subscript
local
Absyn.Exp e1,e2;
case Absyn.SUBSCRIPT(subscript = e1)
equation
(e2, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
then
(if referenceEq(e1,e2) then subscript else Absyn.SUBSCRIPT(e2), arg);
case Absyn.NOSUB() then (subscript, arg);
end match;
end traverseExpBidirSubs;
public function traverseExpBidirElseIf<Arg>
"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 Arg inArg;
output tuple<Absyn.Exp, Absyn.Exp> outElseIf;
output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
protected
Absyn.Exp e1, e2;
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<Arg>
"Helper function to traverseExpBidirSubExps. Traverses the expressions in a
list of function argument."
input output Absyn.FunctionArgs args;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(args, arg) := match args
local
Absyn.Exp e1,e2;
list<Absyn.Exp> expl1,expl2;
list<Absyn.NamedArg> named_args1,named_args2;
Absyn.ForIterators iters1,iters2;
Absyn.ReductionIterType iterType;
case Absyn.FUNCTIONARGS(args = expl1, argNames = named_args1)
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 args else Absyn.FUNCTIONARGS(expl2, named_args2), arg);
case Absyn.FOR_ITER_FARG(e1, iterType, iters1)
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 args else Absyn.FOR_ITER_FARG(e2, iterType, iters2), arg);
end match;
end traverseExpBidirFunctionArgs;
public function traverseExpBidirNamedArg<Arg>
"Helper function to traverseExpBidirFunctionArgs. Traverses the expressions in
a named function argument."
input Absyn.NamedArg inArg;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inExtra;
output Absyn.NamedArg outArg;
output Arg outExtra;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
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<Arg>
"Helper function to traverseExpBidirFunctionArgs. Traverses the expressions in
an iterator."
input Absyn.ForIterator inIterator;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output Absyn.ForIterator outIterator;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
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<Arg>
input output Absyn.Case matchCase;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(matchCase, arg) := match matchCase
local
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)
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(ldecls, cp, result, resultInfo, cmt, info)
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<Arg>
input output Absyn.ClassPart cp;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(cp, arg) := match cp
local
list<Absyn.AlgorithmItem> algs;
list<Absyn.EquationItem> eqs;
case Absyn.ALGORITHMS(algs)
algorithm
(algs, arg) := List.map2FoldCheckReferenceEq(algs, traverseAlgorithmItemBidir, enterFunc, exitFunc, arg);
then
(Absyn.ALGORITHMS(algs),arg);
case Absyn.EQUATIONS(eqs)
algorithm
(eqs, arg) := List.map2FoldCheckReferenceEq(eqs, traverseEquationItemBidir, enterFunc, exitFunc, arg);
then
(Absyn.EQUATIONS(eqs),arg);
end match;
end traverseClassPartBidir;
protected function traverseEquationItemListBidir<Arg>
input list<Absyn.EquationItem> inEquationItems;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output list<Absyn.EquationItem> outEquationItems;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outEquationItems, outArg) := List.map2FoldCheckReferenceEq(inEquationItems, traverseEquationItemBidir, enterFunc, exitFunc, inArg);
end traverseEquationItemListBidir;
protected function traverseAlgorithmItemListBidir<Arg>
input list<Absyn.AlgorithmItem> inAlgs;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output list<Absyn.AlgorithmItem> outAlgs;
output Arg outArg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
(outAlgs, outArg) := List.map2FoldCheckReferenceEq(inAlgs, traverseAlgorithmItemBidir, enterFunc, exitFunc, inArg);
end traverseAlgorithmItemListBidir;
protected function traverseAlgorithmItemBidir<Arg>
input output Absyn.AlgorithmItem algorithmItem;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
() := match algorithmItem
local
Absyn.Algorithm alg;
case Absyn.ALGORITHMITEM(algorithm_ = alg)
algorithm
(alg, arg) := traverseAlgorithmBidir(alg, enterFunc, exitFunc, arg);
algorithmItem.algorithm_ := alg;
then
();
case Absyn.ALGORITHMITEMCOMMENT() then ();
end match;
end traverseAlgorithmItemBidir;
protected function traverseEquationItemBidir<Arg>
input output Absyn.EquationItem equationItem;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
() := match equationItem
local
Absyn.Equation eq;
case Absyn.EQUATIONITEM(equation_ = eq)
algorithm
(eq, arg) := traverseEquationBidir(eq, enterFunc, exitFunc, arg);
equationItem.equation_ := eq;
then
();
case Absyn.EQUATIONITEMCOMMENT() then ();
end match;
end traverseEquationItemBidir;
public function traverseEquationBidir<Arg>
input output Absyn.Equation eq;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
eq := match eq
local
Absyn.Exp e1, e2;
list<Absyn.EquationItem> eqil1, eqil2;
list<tuple<Absyn.Exp, list<Absyn.EquationItem>>> else_branch;
Absyn.ComponentRef cref1, cref2;
Absyn.ForIterators iters;
Absyn.FunctionArgs func_args;
Absyn.EquationItem eq1;
case Absyn.EQ_IF(ifExp = e1, equationTrueItems = eqil1, elseIfBranches = else_branch, equationElseItems = eqil2)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(eqil1, arg) = traverseEquationItemListBidir(eqil1, enterFunc, exitFunc, arg);
(else_branch,arg) = List.map2FoldCheckReferenceEq(else_branch, traverseEquationBidirElse, enterFunc, exitFunc, arg);
(eqil2,arg) = traverseEquationItemListBidir(eqil2, enterFunc, exitFunc, arg);
then
Absyn.EQ_IF(e1, eqil1, else_branch, eqil2);
case Absyn.EQ_EQUALS(leftSide = e1, rightSide = e2)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
Absyn.EQ_EQUALS(e1, e2);
case Absyn.EQ_PDE(leftSide = e1, rightSide = e2, domain = cref1)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
cref1 = traverseExpBidirCref(cref1, enterFunc, exitFunc, arg);
then
Absyn.EQ_PDE(e1, e2,cref1);
case Absyn.EQ_CONNECT(connector1 = cref1, connector2 = cref2)
equation
(cref1, arg) = traverseExpBidirCref(cref1, enterFunc, exitFunc, arg);
(cref2, arg) = traverseExpBidirCref(cref2, enterFunc, exitFunc, arg);
then
Absyn.EQ_CONNECT(cref1, cref2);
case Absyn.EQ_FOR(iterators = iters, forEquations = eqil1)
equation
(iters, arg) = List.map2FoldCheckReferenceEq(iters, traverseExpBidirIterator, enterFunc, exitFunc, arg);
(eqil1, arg) = traverseEquationItemListBidir(eqil1, enterFunc, exitFunc, arg);
then
Absyn.EQ_FOR(iters, eqil1);
case Absyn.EQ_WHEN_E(whenExp = e1, whenEquations = eqil1, elseWhenEquations = else_branch)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(eqil1, arg) = traverseEquationItemListBidir(eqil1, enterFunc, exitFunc, arg);
(else_branch, arg) = List.map2FoldCheckReferenceEq(else_branch, traverseEquationBidirElse, enterFunc, exitFunc, arg);
then
Absyn.EQ_WHEN_E(e1, eqil1, else_branch);
case Absyn.EQ_NORETCALL(functionName = cref1, functionArgs = func_args)
equation
(cref1, arg) = traverseExpBidirCref(cref1, enterFunc, exitFunc, arg);
(func_args, arg) = traverseExpBidirFunctionArgs(func_args, enterFunc, exitFunc, arg);
then
Absyn.EQ_NORETCALL(cref1, func_args);
case Absyn.EQ_FAILURE(equ = eq1)
equation
(eq1, arg) = traverseEquationItemBidir(eq1, enterFunc, exitFunc, arg);
then
Absyn.EQ_FAILURE(eq1);
end match;
end traverseEquationBidir;
protected function traverseEquationBidirElse<Arg>
input tuple<Absyn.Exp, list<Absyn.EquationItem>> inElse;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output tuple<Absyn.Exp, list<Absyn.EquationItem>> outElse;
output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
protected
Absyn.Exp e;
list<Absyn.EquationItem> eqil;
algorithm
(e, eqil) := inElse;
(e, arg) := traverseExpBidir(e, enterFunc, exitFunc, inArg);
(eqil, arg) := traverseEquationItemListBidir(eqil, enterFunc, exitFunc, arg);
outElse := (e, eqil);
end traverseEquationBidirElse;
protected function traverseAlgorithmBidirElse<Arg>
input tuple<Absyn.Exp, list<Absyn.AlgorithmItem>> inElse;
input FuncType enterFunc;
input FuncType exitFunc;
input Arg inArg;
output tuple<Absyn.Exp, list<Absyn.AlgorithmItem>> outElse;
output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
protected
Absyn.Exp e;
list<Absyn.AlgorithmItem> algs;
algorithm
(e, algs) := inElse;
(e, arg) := traverseExpBidir(e, enterFunc, exitFunc, inArg);
(algs, arg) := traverseAlgorithmItemListBidir(algs, enterFunc, exitFunc, arg);
outElse := (e, algs);
end traverseAlgorithmBidirElse;
protected function traverseAlgorithmBidir<Arg>
input output Absyn.Algorithm alg;
input FuncType enterFunc;
input FuncType exitFunc;
input output Arg arg;
partial function FuncType
input output Absyn.Exp exp;
input output Arg arg;
end FuncType;
algorithm
alg := match alg
local
Absyn.Exp e1, e2;
list<Absyn.AlgorithmItem> algs1, algs2;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> else_branch;
Absyn.ComponentRef cref1, cref2;
Absyn.ForIterators iters;
Absyn.FunctionArgs func_args;
case Absyn.ALG_ASSIGN(e1, e2)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(e2, enterFunc, exitFunc, arg);
then
Absyn.ALG_ASSIGN(e1, e2);
case Absyn.ALG_IF(e1, algs1, else_branch, algs2)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
(else_branch, arg) = List.map2FoldCheckReferenceEq(else_branch, traverseAlgorithmBidirElse, enterFunc, exitFunc, arg);
(algs2, arg) = traverseAlgorithmItemListBidir(algs2, enterFunc, exitFunc, arg);
then
Absyn.ALG_IF(e1, algs1, else_branch, algs2);
case Absyn.ALG_FOR(iters, algs1)
equation
(iters, arg) = List.map2FoldCheckReferenceEq(iters, traverseExpBidirIterator, enterFunc, exitFunc, arg);
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
then
Absyn.ALG_FOR(iters, algs1);
case Absyn.ALG_PARFOR(iters, algs1)
equation
(iters, arg) = List.map2FoldCheckReferenceEq(iters, traverseExpBidirIterator, enterFunc, exitFunc, arg);
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
then
Absyn.ALG_PARFOR(iters, algs1);
case Absyn.ALG_WHILE(e1, algs1)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
then
Absyn.ALG_WHILE(e1, algs1);
case Absyn.ALG_WHEN_A(e1, algs1, else_branch)
equation
(e1, arg) = traverseExpBidir(e1, enterFunc, exitFunc, arg);
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
(else_branch, arg) = List.map2FoldCheckReferenceEq(else_branch, traverseAlgorithmBidirElse, enterFunc, exitFunc, arg);
then
Absyn.ALG_WHEN_A(e1, algs1, else_branch);
case Absyn.ALG_NORETCALL(cref1, func_args)
equation
(cref1, arg) = traverseExpBidirCref(cref1, enterFunc, exitFunc, arg);
(func_args, arg) = traverseExpBidirFunctionArgs(func_args, enterFunc, exitFunc, arg);
then
Absyn.ALG_NORETCALL(cref1, func_args);
case Absyn.ALG_RETURN() then alg;
case Absyn.ALG_BREAK() then alg;
case Absyn.ALG_CONTINUE() then alg;
case Absyn.ALG_FAILURE(algs1)
equation
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
then
Absyn.ALG_FAILURE(algs1);
case Absyn.ALG_TRY(algs1, algs2)
equation
(algs1, arg) = traverseAlgorithmItemListBidir(algs1, enterFunc, exitFunc, arg);
(algs2, arg) = traverseAlgorithmItemListBidir(algs2, enterFunc, exitFunc, arg);
then
Absyn.ALG_TRY(algs1, algs2);
end match;
end traverseAlgorithmBidir;
public function makeIdentPathFromString
input String s;
output Absyn.Path p;
algorithm
p := Absyn.IDENT(s);
annotation(__OpenModelica_EarlyInline = true);
end makeIdentPathFromString;
public function makeQualifiedPathFromStrings
input String s1;
input String s2;
output Absyn.Path p;
algorithm
p := Absyn.QUALIFIED(s1,Absyn.IDENT(s2));
annotation(__OpenModelica_EarlyInline = true);
end makeQualifiedPathFromStrings;
public function className "returns the class name of a Absyn.Class as a Absyn.Path"
input Absyn.Class cl;
output Absyn.Path name;
protected
String id;
algorithm
Absyn.CLASS(name = id) := cl;
name := Absyn.IDENT(id);
end className;
public function isClassNamed
input String inName;
input Absyn.Class inClass;
output Boolean outIsNamed;
algorithm
outIsNamed := match inClass
case Absyn.CLASS() then inName == inClass.name;
else false;
end match;
end isClassNamed;
public function elementSpecName
"The Absyn.ElementSpec type contains the name of the element, and this function
extracts this name."
input Absyn.ElementSpec inElementSpec;
output Absyn.Ident outIdent;
algorithm
outIdent := match (inElementSpec)
local Absyn.Ident n;
case Absyn.CLASSDEF(class_ = Absyn.CLASS(name = n)) then n;
case Absyn.COMPONENTS(components = {Absyn.COMPONENTITEM(component = Absyn.COMPONENT(name = n))}) then n;
end match;
end elementSpecName;
public function isClassdef
input Absyn.Element inElement;
output Boolean b;
algorithm
b := match inElement
case Absyn.ELEMENT(specification=Absyn.CLASSDEF()) then true;
else false;
end match;
end isClassdef;
public function printImportString
"This function takes a Absyn.Import and prints it as a flat-string."
input Absyn.Import imp;
output String ostring;
algorithm
ostring := match(imp)
case Absyn.NAMED_IMPORT() then imp.name;
case Absyn.QUAL_IMPORT() then pathString(imp.path);
case Absyn.UNQUAL_IMPORT() then pathString(imp.path);
end match;
end printImportString;
public function expString "returns the string of an expression if it is a string constant."
input Absyn.Exp exp;
output String str;
algorithm
Absyn.STRING(str) := exp;
end expString;
public function expCref "returns the componentRef of an expression if matches."
input Absyn.Exp exp;
output Absyn.ComponentRef cr;
algorithm
Absyn.CREF(cr) := exp;
end expCref;
public function crefExp "returns the componentRef of an expression if matches."
input Absyn.ComponentRef cr;
output Absyn.Exp exp;
algorithm
exp := Absyn.CREF(cr);
annotation(__OpenModelica_EarlyInline = true);
end crefExp;
public function pathEqual "Returns true if two paths are equal."
input Absyn.Path path1;
input Absyn.Path path2;
output Boolean equal;
algorithm
equal := match (path1, path2)
// fully qual vs. path
case (Absyn.FULLYQUALIFIED(), _) then pathEqual(path1.path, path2);
// path vs. fully qual
case (_, Absyn.FULLYQUALIFIED()) then pathEqual(path1, path2.path);