-
Notifications
You must be signed in to change notification settings - Fork 298
/
SCodeUtil.mo
5796 lines (5122 loc) · 170 KB
/
SCodeUtil.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 SCodeUtil
" file: SCodeUtil.mo
package: SCodeUtil
description: Utility functions for the SCodeUtil.intermediate form"
import SCode;
protected
import Absyn;
import AbsynUtil;
import Error;
import List;
import Util;
public
replaceable type Argument subtypeof Any;
constant SourceInfo dummyInfo = SOURCEINFO("", false, 0, 0, 0, 0, 0.0);
function stripSubmod
"Removes all submodifiers from the Mod."
input SCode.Mod inMod;
output SCode.Mod outMod;
algorithm
outMod := match(inMod)
local
SCode.Final fp;
SCode.Each ep;
Option<Absyn.Exp> binding;
SourceInfo info;
case SCode.MOD(fp, ep, _, binding, info)
then SCode.MOD(fp, ep, {}, binding, info);
else inMod;
end match;
end stripSubmod;
function filterSubMods
"Removes submods from a modifier based on a filter function."
input output SCode.Mod mod;
input FilterFunc filter;
partial function FilterFunc
input SCode.SubMod submod;
output Boolean keep;
end FilterFunc;
algorithm
mod := match mod
case SCode.MOD()
algorithm
mod.subModLst := list(m for m guard filter(m) in mod.subModLst);
then
match mod
case SCode.MOD(subModLst = {}, binding = NONE()) then SCode.NOMOD();
else mod;
end match;
else mod;
end match;
end filterSubMods;
function filterGivenSubModNames
input SCode.SubMod submod;
input list<String> namesToKeep;
output Boolean keep;
algorithm
keep := listMember(submod.ident, namesToKeep);
end filterGivenSubModNames;
function removeGivenSubModNames
input SCode.SubMod submod;
input list<String> namesToRemove;
output Boolean keep;
algorithm
keep := not listMember(submod.ident, namesToRemove);
end removeGivenSubModNames;
function getElementNamed
"Return the Element with the name given as first argument from the Class."
input SCode.Ident inIdent;
input SCode.Element inClass;
output SCode.Element outElement;
algorithm
outElement := match (inIdent,inClass)
local
SCode.Element elt;
String id;
list<SCode.Element> elts;
case (id,SCode.CLASS(classDef = SCode.PARTS(elementLst = elts)))
equation
elt = getElementNamedFromElts(id, elts);
then
elt;
/* adrpo: handle also the case model extends X then X; */
case (id,SCode.CLASS(classDef = SCode.CLASS_EXTENDS(composition = SCode.PARTS(elementLst = elts))))
equation
elt = getElementNamedFromElts(id, elts);
then
elt;
end match;
end getElementNamed;
function getElementNamedFromElts
"Helper function to getElementNamed."
input SCode.Ident inIdent;
input list<SCode.Element> inElementLst;
output SCode.Element outElement;
algorithm
outElement := matchcontinue (inIdent,inElementLst)
local
SCode.Element elt,comp,cdef;
String id2,id1;
list<SCode.Element> xs;
case (id2,((comp as SCode.COMPONENT(name = id1)) :: _))
equation
true = stringEq(id1, id2);
then
comp;
case (id2,(SCode.COMPONENT(name = id1) :: xs))
equation
false = stringEq(id1, id2);
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,(SCode.CLASS(name = id1) :: xs))
equation
false = stringEq(id1, id2);
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,(SCode.EXTENDS() :: xs))
equation
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,((cdef as SCode.CLASS(name = id1)) :: _))
equation
true = stringEq(id1, id2);
then
cdef;
// Try next.
case (id2, _:: xs)
equation
elt = getElementNamedFromElts(id2, xs);
then
elt;
end matchcontinue;
end getElementNamedFromElts;
function isElementExtends "
Author BZ, 2009-01
check if an element is of type EXTENDS or not."
input SCode.Element ele;
output Boolean isExtend;
algorithm
isExtend := match(ele)
case SCode.EXTENDS() then true;
else false;
end match;
end isElementExtends;
function isElementExtendsOrClassExtends
"Check if an element extends another class."
input SCode.Element ele;
output Boolean isExtend;
algorithm
isExtend := match(ele)
case SCode.EXTENDS() then true;
else false;
end match;
end isElementExtendsOrClassExtends;
function isNotElementClassExtends "
check if an element is not of type CLASS_EXTENDS."
input SCode.Element ele;
output Boolean isExtend;
algorithm
isExtend := match(ele)
case SCode.CLASS(classDef = SCode.CLASS_EXTENDS()) then false;
else true;
end match;
end isNotElementClassExtends;
function isParameterOrConst
"Returns true if Variability indicates a parameter or constant."
input SCode.Variability inVariability;
output Boolean outBoolean;
algorithm
outBoolean := match (inVariability)
case SCode.PARAM() then true;
case SCode.CONST() then true;
else false;
end match;
end isParameterOrConst;
function isConstant
"Returns true if Variability is constant, otherwise false"
input SCode.Variability inVariability;
output Boolean outBoolean;
algorithm
outBoolean := match (inVariability)
case SCode.CONST() then true;
else false;
end match;
end isConstant;
function countParts
"Counts the number of ClassParts of a Class."
input SCode.Element inClass;
output Integer outInteger;
algorithm
outInteger := matchcontinue (inClass)
local
Integer res;
list<SCode.Element> elts;
case SCode.CLASS(classDef = SCode.PARTS(elementLst = elts))
equation
res = listLength(elts);
then
res;
/* adrpo: handle also model extends X ... parts ... end X; */
case SCode.CLASS(classDef = SCode.CLASS_EXTENDS(composition = SCode.PARTS(elementLst = elts)))
equation
res = listLength(elts);
then
res;
else 0;
end matchcontinue;
end countParts;
function componentNames
"Return a string list of all component names of a class."
input SCode.Element inClass;
output list<String> outStringLst;
algorithm
outStringLst := match (inClass)
local list<String> res; list<SCode.Element> elts;
case (SCode.CLASS(classDef = SCode.PARTS(elementLst = elts)))
equation
res = componentNamesFromElts(elts);
then
res;
/* adrpo: handle also the case model extends X end X;*/
case (SCode.CLASS(classDef = SCode.CLASS_EXTENDS(composition = SCode.PARTS(elementLst = elts))))
equation
res = componentNamesFromElts(elts);
then
res;
else {};
end match;
end componentNames;
function componentNamesFromElts
"Helper function to componentNames."
input list<SCode.Element> inElements;
output list<String> outComponentNames;
algorithm
outComponentNames := List.filterMap(inElements, componentName);
end componentNamesFromElts;
function componentName
input SCode.Element inComponent;
output String outName;
algorithm
SCode.COMPONENT(name = outName) := inComponent;
end componentName;
function elementInfo "retrieves the element info"
input SCode.Element e;
output SourceInfo info;
algorithm
info := match(e)
local
SourceInfo i;
case(SCode.COMPONENT(info = i)) then i;
case(SCode.CLASS(info = i)) then i;
case(SCode.EXTENDS(info = i)) then i;
case(SCode.IMPORT(info = i)) then i;
else AbsynUtil.dummyInfo;
end match;
end elementInfo;
function elementName ""
input SCode.Element e;
output String s;
algorithm
s := match(e)
case (SCode.COMPONENT(name = s)) then s;
case (SCode.CLASS(name = s)) then s;
end match;
end elementName;
function elementNameInfo
input SCode.Element inElement;
output String outName;
output SourceInfo outInfo;
algorithm
(outName, outInfo) := match(inElement)
local
String name;
SourceInfo info;
case SCode.COMPONENT(name = name, info = info) then (name, info);
case SCode.CLASS(name = name, info = info) then (name, info);
end match;
end elementNameInfo;
function elementNames "Gets all elements that have an element name from the list"
input list<SCode.Element> elts;
output list<String> names;
algorithm
names := List.fold(elts,elementNamesWork,{});
end elementNames;
protected function elementNamesWork "Gets all elements that have an element name from the list"
input SCode.Element e;
input list<String> acc;
output list<String> out;
algorithm
out := match(e,acc)
local
String s;
case (SCode.COMPONENT(name = s),_) then s::acc;
case (SCode.CLASS(name = s),_) then s::acc;
else acc;
end match;
end elementNamesWork;
public function renameElement
input SCode.Element inElement;
input String inName;
output SCode.Element outElement;
algorithm
outElement := match(inElement, inName)
local
SCode.Prefixes pf;
SCode.Encapsulated ep;
SCode.Partial pp;
SCode.Restriction res;
SCode.ClassDef cdef;
SourceInfo i;
SCode.Attributes attr;
Absyn.TypeSpec ty;
SCode.Mod mod;
SCode.Comment cmt;
Option<Absyn.Exp> cond;
case (SCode.CLASS(_, pf, ep, pp, res, cdef, cmt, i), _)
then SCode.CLASS(inName, pf, ep, pp, res, cdef, cmt, i);
case (SCode.COMPONENT(_, pf, attr, ty, mod, cmt, cond, i), _)
then SCode.COMPONENT(inName, pf, attr, ty, mod, cmt, cond, i);
end match;
end renameElement;
public function elementNameEqual
input SCode.Element inElement1;
input SCode.Element inElement2;
output Boolean outEqual;
algorithm
outEqual := match (inElement1, inElement2)
case (SCode.CLASS(), SCode.CLASS()) then inElement1.name == inElement2.name;
case (SCode.COMPONENT(), SCode.COMPONENT()) then inElement1.name == inElement2.name;
case (SCode.DEFINEUNIT(), SCode.DEFINEUNIT()) then inElement1.name == inElement2.name;
case (SCode.EXTENDS(), SCode.EXTENDS())
then AbsynUtil.pathEqual(inElement1.baseClassPath, inElement2.baseClassPath);
case (SCode.IMPORT(), SCode.IMPORT())
then AbsynUtil.importEqual(inElement1.imp, inElement2.imp);
else false;
end match;
end elementNameEqual;
public function enumName ""
input SCode.Enum e;
output String s;
algorithm
s := match(e)
case(SCode.ENUM(literal = s)) then s;
end match;
end enumName;
public function isRecord
"Return true if Class is a record."
input SCode.Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := match(inClass)
case SCode.CLASS(restriction = SCode.R_RECORD()) then true;
else false;
end match;
end isRecord;
public function isTypeVar
"Return true if Class is a type"
input SCode.Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := match(inClass)
case SCode.CLASS(restriction = SCode.R_TYPE()) then true;
else false;
end match;
end isTypeVar;
public function isOperatorRecord
"Return true if Class is a operator record."
input SCode.Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := match(inClass)
case SCode.CLASS(restriction = SCode.R_RECORD(true)) then true;
else false;
end match;
end isOperatorRecord;
public function isFunction
"Return true if Class is a function."
input SCode.Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := match(inClass)
case SCode.CLASS(restriction = SCode.R_FUNCTION()) then true;
else false;
end match;
end isFunction;
public function isUniontype
"Return true if Class is a uniontype."
input SCode.Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := match(inClass)
case SCode.CLASS(restriction = SCode.R_UNIONTYPE()) then true;
else false;
end match;
end isUniontype;
public function isFunctionRestriction
"Return true if restriction is a function."
input SCode.Restriction inRestriction;
output Boolean outBoolean;
algorithm
outBoolean := match(inRestriction)
case SCode.R_FUNCTION() then true;
else false;
end match;
end isFunctionRestriction;
public function isFunctionOrExtFunctionRestriction
"restriction is function or external function.
Otherwise false is returned."
input SCode.Restriction r;
output Boolean res;
algorithm
res := match(r)
case (SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION())) then true;
case (SCode.R_FUNCTION(SCode.FR_EXTERNAL_FUNCTION())) then true;
else false;
end match;
end isFunctionOrExtFunctionRestriction;
public function isOperator
"restriction is operator or operator function.
Otherwise false is returned."
input SCode.Element el;
output Boolean res;
algorithm
res := match(el)
case (SCode.CLASS(restriction=SCode.R_OPERATOR())) then true;
case (SCode.CLASS(restriction=SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION()))) then true;
else false;
end match;
end isOperator;
public function className
"Returns the class name of a Class."
input SCode.Element inClass;
output String outName;
algorithm
SCode.CLASS(name = outName) := inClass;
end className;
public function classSetPartial
"author: PA
Sets the partial attribute of a Class"
input SCode.Element inClass;
input SCode.Partial inPartial;
output SCode.Element outClass;
algorithm
outClass := match (inClass,inPartial)
local
String id;
SCode.Encapsulated enc;
SCode.Partial partialPrefix;
SCode.Restriction restr;
SCode.ClassDef def;
SourceInfo info;
SCode.Prefixes prefixes;
SCode.Comment cmt;
case (SCode.CLASS(name = id,
prefixes = prefixes,
encapsulatedPrefix = enc,
restriction = restr,
classDef = def,
cmt = cmt,
info = info),partialPrefix)
then SCode.CLASS(id,prefixes,enc,partialPrefix,restr,def,cmt,info);
end match;
end classSetPartial;
public function elementEqual
"returns true if two elements are equal,
i.e. for a component have the same type,
name, and attributes, etc."
input SCode.Element element1;
input SCode.Element element2;
output Boolean equal;
algorithm
equal := matchcontinue(element1,element2)
local
SCode.Ident name1,name2;
SCode.Prefixes prefixes1, prefixes2;
SCode.Encapsulated en1, en2;
SCode.Partial p1,p2;
SCode.Restriction restr1, restr2;
SCode.Attributes attr1,attr2;
SCode.Mod mod1,mod2;
Absyn.TypeSpec tp1,tp2;
Absyn.Import im1,im2;
Absyn.Path path1,path2;
Option<String> os1,os2;
Option<Real> or1,or2;
Option<Absyn.Exp> cond1, cond2;
SCode.ClassDef cd1,cd2;
case (SCode.CLASS(name1,prefixes1,en1,p1,restr1,cd1,_,_),SCode.CLASS(name2,prefixes2,en2,p2,restr2,cd2,_,_))
equation
true = stringEq(name1,name2);
true = prefixesEqual(prefixes1,prefixes2);
true = valueEq(en1,en2);
true = valueEq(p1,p2);
true = restrictionEqual(restr1,restr2);
true = classDefEqual(cd1,cd2);
then
true;
case (SCode.COMPONENT(name1,prefixes1,attr1,tp1,mod1,_,cond1,_),
SCode.COMPONENT(name2,prefixes2,attr2,tp2,mod2,_,cond2,_))
equation
equality(cond1 = cond2);
true = stringEq(name1,name2);
true = prefixesEqual(prefixes1,prefixes2);
true = attributesEqual(attr1,attr2);
true = modEqual(mod1,mod2);
true = AbsynUtil.typeSpecEqual(tp1,tp2);
then
true;
case (SCode.EXTENDS(path1,_,mod1,_,_), SCode.EXTENDS(path2,_,mod2,_,_))
equation
true = AbsynUtil.pathEqual(path1,path2);
true = modEqual(mod1,mod2);
then
true;
case (SCode.IMPORT(imp = im1), SCode.IMPORT(imp = im2))
equation
true = AbsynUtil.importEqual(im1,im2);
then
true;
case (SCode.DEFINEUNIT(name1,_,os1,or1), SCode.DEFINEUNIT(name2,_,os2,or2))
equation
true = stringEq(name1,name2);
equality(os1 = os2);
equality(or1 = or2);
then
true;
// otherwise false
else false;
end matchcontinue;
end elementEqual;
// stefan
public function annotationEqual
"returns true if 2 annotations are equal"
input SCode.Annotation annotation1;
input SCode.Annotation annotation2;
output Boolean equal;
protected
SCode.Mod mod1, mod2;
algorithm
SCode.ANNOTATION(modification = mod1) := annotation1;
SCode.ANNOTATION(modification = mod2) := annotation2;
equal := modEqual(mod1, mod2);
end annotationEqual;
public function restrictionEqual "Returns true if two Restriction's are equal."
input SCode.Restriction restr1;
input SCode.Restriction restr2;
output Boolean equal;
algorithm
equal := match(restr1,restr2)
local
SCode.FunctionRestriction funcRest1,funcRest2;
case (SCode.R_CLASS(),SCode.R_CLASS()) then true;
case (SCode.R_OPTIMIZATION(),SCode.R_OPTIMIZATION()) then true;
case (SCode.R_MODEL(),SCode.R_MODEL()) then true;
case (SCode.R_RECORD(true),SCode.R_RECORD(true)) then true; // operator record
case (SCode.R_RECORD(false),SCode.R_RECORD(false)) then true;
case (SCode.R_BLOCK(),SCode.R_BLOCK()) then true;
case (SCode.R_CONNECTOR(true),SCode.R_CONNECTOR(true)) then true; // expandable connectors
case (SCode.R_CONNECTOR(false),SCode.R_CONNECTOR(false)) then true; // non expandable connectors
case (SCode.R_OPERATOR(),SCode.R_OPERATOR()) then true; // operator
case (SCode.R_TYPE(),SCode.R_TYPE()) then true;
case (SCode.R_PACKAGE(),SCode.R_PACKAGE()) then true;
case (SCode.R_FUNCTION(funcRest1),SCode.R_FUNCTION(funcRest2)) then funcRestrictionEqual(funcRest1,funcRest2);
case (SCode.R_ENUMERATION(),SCode.R_ENUMERATION()) then true;
case (SCode.R_PREDEFINED_INTEGER(),SCode.R_PREDEFINED_INTEGER()) then true;
case (SCode.R_PREDEFINED_REAL(),SCode.R_PREDEFINED_REAL()) then true;
case (SCode.R_PREDEFINED_STRING(),SCode.R_PREDEFINED_STRING()) then true;
case (SCode.R_PREDEFINED_BOOLEAN(),SCode.R_PREDEFINED_BOOLEAN()) then true;
// BTH
case (SCode.R_PREDEFINED_CLOCK(),SCode.R_PREDEFINED_CLOCK()) then true;
case (SCode.R_PREDEFINED_ENUMERATION(),SCode.R_PREDEFINED_ENUMERATION()) then true;
case (SCode.R_UNIONTYPE(),SCode.R_UNIONTYPE()) then min(t1==t2 threaded for t1 in restr1.typeVars, t2 in restr2.typeVars);
else false;
end match;
end restrictionEqual;
public function funcRestrictionEqual
input SCode.FunctionRestriction funcRestr1;
input SCode.FunctionRestriction funcRestr2;
output Boolean equal;
algorithm
equal := match(funcRestr1,funcRestr2)
local Boolean b1, b2;
case (SCode.FR_NORMAL_FUNCTION(b1),SCode.FR_NORMAL_FUNCTION(b2)) then boolEq(b1, b2);
case (SCode.FR_EXTERNAL_FUNCTION(b1),SCode.FR_EXTERNAL_FUNCTION(b2)) then boolEq(b1, b2);
case (SCode.FR_OPERATOR_FUNCTION(),SCode.FR_OPERATOR_FUNCTION()) then true;
case (SCode.FR_RECORD_CONSTRUCTOR(),SCode.FR_RECORD_CONSTRUCTOR()) then true;
case (SCode.FR_PARALLEL_FUNCTION(),SCode.FR_PARALLEL_FUNCTION()) then true;
case (SCode.FR_KERNEL_FUNCTION(),SCode.FR_KERNEL_FUNCTION()) then true;
else false;
end match;
end funcRestrictionEqual;
function enumEqual
input SCode.Enum e1;
input SCode.Enum e2;
output Boolean isEqual;
algorithm
isEqual := match(e1, e2)
local
String s1, s2;
Boolean b1;
case (SCode.ENUM(s1,_), SCode.ENUM(s2,_))
equation
b1 = stringEq(s1, s2);
// ignore comments here.
then b1;
end match;
end enumEqual;
protected function classDefEqual
"Returns true if Two ClassDef's are equal"
input SCode.ClassDef cdef1;
input SCode.ClassDef cdef2;
output Boolean equal;
algorithm
equal := match(cdef1,cdef2)
local
list<SCode.Element> elts1,elts2;
list<SCode.Equation> eqns1,eqns2;
list<SCode.Equation> ieqns1,ieqns2;
list<SCode.AlgorithmSection> algs1,algs2;
list<SCode.AlgorithmSection> ialgs1,ialgs2;
list<SCode.ConstraintSection> cons1,cons2;
SCode.Attributes attr1,attr2;
Absyn.TypeSpec tySpec1, tySpec2;
Absyn.Path p1, p2;
SCode.Mod mod1,mod2;
list<SCode.Enum> elst1,elst2;
list<SCode.Ident> ilst1,ilst2;
list<Absyn.NamedArg> clsttrs1,clsttrs2;
case(SCode.PARTS(elts1,eqns1,ieqns1,algs1,ialgs1,_,_,_),
SCode.PARTS(elts2,eqns2,ieqns2,algs2,ialgs2,_,_,_))
equation
List.threadMapAllValue(elts1,elts2,elementEqual,true);
List.threadMapAllValue(eqns1,eqns2,equationEqual,true);
List.threadMapAllValue(ieqns1,ieqns2,equationEqual,true);
List.threadMapAllValue(algs1,algs2,algorithmEqual,true);
List.threadMapAllValue(ialgs1,ialgs2,algorithmEqual,true);
then true;
case (SCode.DERIVED(tySpec1,mod1,attr1),
SCode.DERIVED(tySpec2,mod2,attr2))
equation
true = AbsynUtil.typeSpecEqual(tySpec1, tySpec2);
true = modEqual(mod1,mod2);
true = attributesEqual(attr1, attr2);
then
true;
case (SCode.ENUMERATION(elst1),SCode.ENUMERATION(elst2))
equation
List.threadMapAllValue(elst1,elst2,enumEqual,true);
then
true;
case (SCode.CLASS_EXTENDS(mod1,SCode.PARTS(elts1,eqns1,ieqns1,algs1,ialgs1,_,_,_)),
SCode.CLASS_EXTENDS(mod2,SCode.PARTS(elts2,eqns2,ieqns2,algs2,ialgs2,_,_,_)))
equation
List.threadMapAllValue(elts1,elts2,elementEqual,true);
List.threadMapAllValue(eqns1,eqns2,equationEqual,true);
List.threadMapAllValue(ieqns1,ieqns2,equationEqual,true);
List.threadMapAllValue(algs1,algs2,algorithmEqual,true);
List.threadMapAllValue(ialgs1,ialgs2,algorithmEqual,true);
true = modEqual(mod1,mod2);
then
true;
case (SCode.PDER(_,ilst1),SCode.PDER(_,ilst2))
equation
List.threadMapAllValue(ilst1,ilst2,stringEq,true);
then
true;
/* adrpo: TODO! FIXME! are these below really needed??!!
// as far as I can tell we handle all the cases.
case(cdef1, cdef2)
equation
equality(cdef1=cdef2);
then true;
case(cdef1, cdef2)
equation
failure(equality(cdef1=cdef2));
then false;*/
else fail();
end match;
end classDefEqual;
protected function arraydimOptEqual
"Returns true if two Option<ArrayDim> are equal"
input Option<Absyn.ArrayDim> adopt1;
input Option<Absyn.ArrayDim> adopt2;
output Boolean equal;
algorithm
equal := matchcontinue(adopt1,adopt2)
local
list<Absyn.Subscript> lst1,lst2;
list<Boolean> blst;
case(NONE(),NONE()) then true;
case(SOME(lst1),SOME(lst2))
equation
List.threadMapAllValue(lst1,lst2,subscriptEqual,true);
then
true;
// oth. false
case(SOME(_),SOME(_)) then false;
end matchcontinue;
end arraydimOptEqual;
protected function subscriptEqual
"Returns true if two Absyn.Subscript are equal"
input Absyn.Subscript sub1;
input Absyn.Subscript sub2;
output Boolean equal;
algorithm
equal := match(sub1,sub2)
local
Absyn.Exp e1,e2;
case(Absyn.NOSUB(),Absyn.NOSUB()) then true;
case(Absyn.SUBSCRIPT(e1),Absyn.SUBSCRIPT(e2))
then AbsynUtil.expEqual(e1,e2);
end match;
end subscriptEqual;
protected function algorithmEqual
"Returns true if two Algorithm's are equal."
input SCode.AlgorithmSection alg1;
input SCode.AlgorithmSection alg2;
output Boolean equal;
algorithm
equal := matchcontinue(alg1,alg2)
local
list<SCode.Statement> a1,a2;
case(SCode.ALGORITHM(a1),SCode.ALGORITHM(a2))
equation
List.threadMapAllValue(a1,a2,algorithmEqual2,true);
then
true;
// false otherwise!
else false;
end matchcontinue;
end algorithmEqual;
protected function algorithmEqual2
"Returns true if two Absyn.Algorithm are equal."
input SCode.Statement ai1;
input SCode.Statement ai2;
output Boolean equal;
algorithm
equal := matchcontinue(ai1,ai2)
local
Absyn.Algorithm alg1,alg2;
SCode.Statement a1,a2;
Absyn.ComponentRef cr1,cr2;
Absyn.Exp e1,e2,e11,e12,e21,e22;
Boolean b1,b2;
case(SCode.ALG_ASSIGN(assignComponent = Absyn.CREF(cr1), value = e1),
SCode.ALG_ASSIGN(assignComponent = Absyn.CREF(cr2), value = e2))
equation
b1 = AbsynUtil.crefEqual(cr1,cr2);
b2 = AbsynUtil.expEqual(e1,e2);
equal = boolAnd(b1,b2);
then equal;
case(SCode.ALG_ASSIGN(assignComponent = e11 as Absyn.TUPLE(_), value = e12),SCode.ALG_ASSIGN(assignComponent = e21 as Absyn.TUPLE(_), value = e22))
equation
b1 = AbsynUtil.expEqual(e11,e21);
b2 = AbsynUtil.expEqual(e12,e22);
equal = boolAnd(b1,b2);
then equal;
// base it on equality for now as the ones below are not implemented!
case(a1, a2)
equation
Absyn.ALGORITHMITEM(algorithm_ = alg1) = statementToAlgorithmItem(a1);
Absyn.ALGORITHMITEM(algorithm_ = alg2) = statementToAlgorithmItem(a2);
// Don't compare comments and line numbers
equality(alg1 = alg2);
then
true;
// maybe replace failure/equality with these:
//case(Absyn.ALG_IF(_,_,_,_),Absyn.ALG_IF(_,_,_,_)) then false; // TODO: SCode.ALG_IF
//case (Absyn.ALG_FOR(_,_),Absyn.ALG_FOR(_,_)) then false; // TODO: SCode.ALG_FOR
//case (Absyn.ALG_WHILE(_,_),Absyn.ALG_WHILE(_,_)) then false; // TODO: SCode.ALG_WHILE
//case(Absyn.ALG_WHEN_A(_,_,_),Absyn.ALG_WHEN_A(_,_,_)) then false; //TODO: SCode.ALG_WHILE
//case (Absyn.ALG_NORETCALL(_,_),Absyn.ALG_NORETCALL(_,_)) then false; //TODO: SCode.ALG_NORETCALL
else false;
end matchcontinue;
end algorithmEqual2;
public function equationEqual
"Returns true if two equations are equal."
input SCode.Equation eqn1;
input SCode.Equation eqn2;
output Boolean equal;
protected
SCode.EEquation eq1, eq2;
algorithm
SCode.EQUATION(eEquation = eq1) := eqn1;
SCode.EQUATION(eEquation = eq2) := eqn2;
equal := equationEqual2(eq1, eq2);
end equationEqual;
protected function equationEqual2
"Helper function to equationEqual"
input SCode.EEquation eq1;
input SCode.EEquation eq2;
output Boolean equal;
algorithm
equal := matchcontinue(eq1,eq2)
local
list<list<SCode.EEquation>> tb1,tb2;
Absyn.Exp cond1,cond2;
list<Absyn.Exp> ifcond1,ifcond2;
Absyn.Exp e11,e12,e21,e22,exp1,exp2,c1,c2,m1,m2,e1,e2;
Absyn.ComponentRef cr11,cr12,cr21,cr22,cr1,cr2;
Absyn.Ident id1,id2;
list<SCode.EEquation> fb1,fb2,eql1,eql2,elst1,elst2;
case (SCode.EQ_IF(condition = ifcond1, thenBranch = tb1, elseBranch = fb1),SCode.EQ_IF(condition = ifcond2, thenBranch = tb2, elseBranch = fb2))
equation
true = equationEqual22(tb1,tb2);
List.threadMapAllValue(fb1,fb2,equationEqual2,true);
List.threadMapAllValue(ifcond1,ifcond2,AbsynUtil.expEqual,true);
then
true;
case(SCode.EQ_EQUALS(expLeft = e11, expRight = e12),SCode.EQ_EQUALS(expLeft = e21, expRight = e22))
equation
true = AbsynUtil.expEqual(e11,e21);
true = AbsynUtil.expEqual(e12,e22);
then
true;
case(SCode.EQ_PDE(expLeft = e11, expRight = e12, domain = cr1),SCode.EQ_PDE(expLeft = e21, expRight = e22, domain = cr2))
equation
true = AbsynUtil.expEqual(e11,e21);
true = AbsynUtil.expEqual(e12,e22);
true = AbsynUtil.crefEqual(cr1,cr2);
then
true;
case(SCode.EQ_CONNECT(crefLeft = cr11, crefRight = cr12),SCode.EQ_CONNECT(crefLeft = cr21, crefRight = cr22))
equation
true = AbsynUtil.crefEqual(cr11,cr21);
true = AbsynUtil.crefEqual(cr12,cr22);
then
true;
case (SCode.EQ_FOR(index = id1, range = SOME(exp1), eEquationLst = eql1),SCode.EQ_FOR(index = id2, range = SOME(exp2), eEquationLst = eql2))
equation
List.threadMapAllValue(eql1,eql2,equationEqual2,true);
true = AbsynUtil.expEqual(exp1,exp2);
true = stringEq(id1,id2);
then
true;
case (SCode.EQ_FOR(index = id1, range = NONE(), eEquationLst = eql1),SCode.EQ_FOR(index = id2, range = NONE(), eEquationLst = eql2))
equation
List.threadMapAllValue(eql1,eql2,equationEqual2,true);
true = stringEq(id1,id2);
then
true;
case (SCode.EQ_WHEN(condition = cond1, eEquationLst = elst1),SCode.EQ_WHEN(condition = cond2, eEquationLst = elst2)) // TODO: elsewhen not checked yet.
equation
List.threadMapAllValue(elst1,elst2,equationEqual2,true);
true = AbsynUtil.expEqual(cond1,cond2);
then
true;
case (SCode.EQ_ASSERT(condition = c1, message = m1),SCode.EQ_ASSERT(condition = c2, message = m2))
equation
true = AbsynUtil.expEqual(c1,c2);
true = AbsynUtil.expEqual(m1,m2);
then
true;
case (SCode.EQ_REINIT(), SCode.EQ_REINIT())
equation
true = AbsynUtil.expEqual(eq1.cref, eq2.cref);
true = AbsynUtil.expEqual(eq1.expReinit, eq2.expReinit);
then
true;