-
Notifications
You must be signed in to change notification settings - Fork 298
/
ComponentReference.mo
4058 lines (3608 loc) · 121 KB
/
ComponentReference.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 ComponentReference
" file: ComponentReference.mo
package: ComponentReference
description: All stuff for ComponentRef datatypes
This file contains the module ComponentReference,
which contains functions for ComponentRef."
// public imports
public import Absyn;
public import DAE;
public import File;
// protected imports
protected import ClassInf;
protected import Config;
protected import Debug;
protected import Dump;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import List;
protected import MetaModelica.Dangerous;
protected import Print;
protected import System;
protected import Types;
protected import Util;
// do not make this public. instead use the function below.
protected constant DAE.ComponentRef dummyCref = DAE.CREF_IDENT("dummy", DAE.T_UNKNOWN_DEFAULT, {});
public function hashComponentRefMod "
author: PA
Calculates a hash value for DAE.ComponentRef, by hashing each individual part separately and summing the values, and then apply
intMod to it, to return a value in range [0,mod-1].
Also hashes subscripts in a clever way avoiding [1,2] and [2,1] to hash to the same value. This is done by investigating array type
to find dimension of array.
"
input DAE.ComponentRef cr;
input Integer mod;
output Integer res;
protected
Integer h;
algorithm
// hash might overflow => force positive
h := intAbs(hashComponentRef(cr));
res := intMod(h,mod);
end hashComponentRefMod;
public function hashComponentRef "new hashing that properly deals with subscripts so [1,2] and [2,1] hash to different values"
input DAE.ComponentRef cr;
output Integer hash;
algorithm
hash := match(cr)
local
DAE.Ident id;
DAE.Type tp;
list<DAE.Subscript> subs;
DAE.ComponentRef cr1;
case(DAE.CREF_IDENT(id,tp,subs)) equation
//print("IDENT, "+id+" hashed to "+intString(stringHashDjb2(id))+", subs hashed to "+intString(hashSubscripts(tp,subs))+"\n");
then stringHashDjb2(id) + hashSubscripts(tp,subs);
case(DAE.CREF_QUAL(id,tp,subs,cr1)) equation
//print("QUAL, "+id+" hashed to "+intString(stringHashDjb2(id))+", subs hashed to "+intString(hashSubscripts(tp,subs))+"\n");
then stringHashDjb2(id)+hashSubscripts(tp,subs)+hashComponentRef(cr1);
case(DAE.CREF_ITER(id,_,tp,subs))
then stringHashDjb2(id)+ hashSubscripts(tp,subs);
else 0;
end match;
end hashComponentRef;
protected protected function hashSubscripts "help function, hashing subscripts making sure [1,2] and [2,1] doesn't match to the same number"
input DAE.Type tp;
input list<DAE.Subscript> subs;
output Integer hash;
algorithm
hash := match(tp,subs)
case(_,{}) then 0;
// TODO: Currently, the types of component references are wrong, they consider the subscripts but they should not.
// For example, given Real a[10,10]; the component reference 'a[1,2]' should have type Real[10,10] but it has type Real.
else hashSubscripts2(List.fill(1,listLength(subs)),/*DAEUtil.expTypeArrayDimensions(tp),*/subs,1);
end match;
end hashSubscripts;
protected protected function hashSubscripts2 "help function"
input list<Integer> dims;
input list<DAE.Subscript> subs;
input Integer factor;
output Integer hash;
algorithm
hash := match(dims,subs,factor)
local
Integer i1;
DAE.Subscript s;
list<Integer> rest_dims;
list<DAE.Subscript> rest_subs;
case({},{},_) then 0;
case(_::rest_dims,s::rest_subs,_)
// TODO: change to using dimensions once cref types has been fixed.
then hashSubscript(s)*factor + hashSubscripts2(rest_dims,rest_subs,factor*1000/* *i1 */);
end match;
end hashSubscripts2;
protected function hashSubscript "help function"
input DAE.Subscript sub;
output Integer hash;
algorithm
hash := match(sub)
local
DAE.Exp exp;
Integer i;
case(DAE.WHOLEDIM()) then 0;
case(DAE.INDEX(DAE.ICONST(i))) then i;
case(DAE.SLICE(exp)) then Expression.hashExp(exp);
case(DAE.INDEX(exp)) then Expression.hashExp(exp);
case(DAE.WHOLE_NONEXP(exp)) then Expression.hashExp(exp);
end match;
end hashSubscript;
public function createEmptyCrefMemory
"@author: adrpo
creates an array, with one element for each record in ComponentRef!"
output array<list<DAE.ComponentRef>> crefMemory;
algorithm
crefMemory := arrayCreate(3, {});
end createEmptyCrefMemory;
/***************************************************/
/* generate a ComponentRef */
/***************************************************/
public function makeDummyCref
"@author: adrpo
This function creates a dummy component reference"
output DAE.ComponentRef outCrefIdent;
annotation(__OpenModelica_EarlyInline = true);
algorithm
outCrefIdent := dummyCref;
end makeDummyCref;
public function makeCrefIdent
"@author: adrpo
This function creates a DAE.CREF_IDENT(ident, identType, subscriptLst)"
input DAE.Ident ident;
input DAE.Type identType "type of the identifier, without considering the subscripts";
input list<DAE.Subscript> subscriptLst;
output DAE.ComponentRef outCrefIdent;
annotation(__OpenModelica_EarlyInline = true);
algorithm
outCrefIdent := DAE.CREF_IDENT(ident, identType, subscriptLst);
end makeCrefIdent;
public function makeUntypedCrefIdent
input DAE.Ident ident;
output DAE.ComponentRef outCrefIdent;
annotation(__OpenModelica_EarlyInline = true);
algorithm
outCrefIdent := DAE.CREF_IDENT(ident, DAE.T_UNKNOWN_DEFAULT, {});
end makeUntypedCrefIdent;
public function makeCrefQual
"@author: adrpo
This function creates a DAE.CREF_QUAL(ident, identType, subscriptLst, componentRef)"
input DAE.Ident ident;
input DAE.Type identType "type of the identifier, without considering the subscripts";
input list<DAE.Subscript> subscriptLst;
input DAE.ComponentRef componentRef;
output DAE.ComponentRef outCrefQual;
annotation(__OpenModelica_EarlyInline = true);
protected
DAE.ComponentRef subCref;
algorithm
// subCref := shareCref(componentRef);
// outCrefQual := shareCref(DAE.CREF_QUAL(ident, identType, subscriptLst, subCref));
outCrefQual := DAE.CREF_QUAL(ident, identType, subscriptLst, componentRef);
end makeCrefQual;
/***************************************************/
/* transform to other types */
/***************************************************/
public function crefToPath
"This function converts a ComponentRef to a Path, if possible.
If the component reference contains subscripts, it will silently
fail."
input DAE.ComponentRef inComponentRef;
output Absyn.Path outPath;
algorithm
outPath := match (inComponentRef)
local
DAE.Ident i;
Absyn.Path p;
DAE.ComponentRef c;
case DAE.CREF_IDENT(ident = i,subscriptLst = {}) then Absyn.IDENT(i);
case DAE.CREF_QUAL(ident = i,subscriptLst = {},componentRef = c)
equation
p = crefToPath(c);
then
Absyn.QUALIFIED(i,p);
end match;
end crefToPath;
public function crefToPathIgnoreSubs
input DAE.ComponentRef inComponentRef;
output Absyn.Path outPath;
algorithm
outPath := match(inComponentRef)
local
DAE.Ident i;
Absyn.Path p;
DAE.ComponentRef c;
case DAE.CREF_IDENT(ident = i) then Absyn.IDENT(i);
case DAE.CREF_QUAL(ident = i, componentRef = c)
equation
p = crefToPathIgnoreSubs(c);
then
Absyn.QUALIFIED(i, p);
end match;
end crefToPathIgnoreSubs;
public function pathToCref
"This function converts a Absyn.Path to a ComponentRef."
input Absyn.Path inPath;
output DAE.ComponentRef outComponentRef;
algorithm
outComponentRef := match (inPath)
local
DAE.Ident i;
DAE.ComponentRef c;
Absyn.Path p;
case Absyn.IDENT(name = i) then makeCrefIdent(i,DAE.T_UNKNOWN_DEFAULT,{});
case (Absyn.FULLYQUALIFIED(p)) then pathToCref(p);
case Absyn.QUALIFIED(name = i,path = p)
equation
c = pathToCref(p);
then
makeCrefQual(i,DAE.T_UNKNOWN_DEFAULT,{},c);
end match;
end pathToCref;
public function creffromVar
" author: Frenkel TUD
generates a cref from DAE.Var"
input DAE.Var inVar;
output DAE.ComponentRef outComponentRef;
algorithm
outComponentRef := match (inVar)
local
String name;
DAE.Type ty;
case DAE.TYPES_VAR(name=name,ty=ty)
then
makeCrefIdent(name,ty,{});
end match;
end creffromVar;
public function unelabCref
"Transform an ComponentRef into Absyn.ComponentRef."
input DAE.ComponentRef inComponentRef;
output Absyn.ComponentRef outComponentRef;
algorithm
outComponentRef := matchcontinue (inComponentRef)
local
list<Absyn.Subscript> subs_1;
DAE.Ident id;
list<DAE.Subscript> subs;
Absyn.ComponentRef cr_1;
DAE.ComponentRef cr;
// iterators
case (DAE.CREF_ITER(ident = id, subscriptLst = subs))
equation
subs_1 = unelabSubscripts(subs);
then
Absyn.CREF_IDENT(id ,subs_1);
// identifiers
case (DAE.CREF_IDENT(ident = id, subscriptLst = subs))
equation
subs_1 = unelabSubscripts(subs);
then
Absyn.CREF_IDENT(id, subs_1);
// qualified
case (DAE.CREF_QUAL(ident = id, subscriptLst = subs, componentRef = cr))
equation
cr_1 = unelabCref(cr);
subs_1 = unelabSubscripts(subs);
then
Absyn.CREF_QUAL(id, subs_1, cr_1);
case _
equation
true = Flags.isSet(Flags.FAILTRACE);
print("ComponentReference.unelabCref failed on: " + printComponentRefStr(inComponentRef) + "\n");
then
fail();
end matchcontinue;
end unelabCref;
protected function unelabSubscripts
"Helper function to unelabCref, handles subscripts."
input list<DAE.Subscript> inSubscriptLst;
output list<Absyn.Subscript> outAbsynSubscriptLst;
algorithm
outAbsynSubscriptLst := match (inSubscriptLst)
local
list<Absyn.Subscript> xs_1;
list<DAE.Subscript> xs;
Absyn.Exp e_1;
DAE.Exp e;
// empty list
case ({}) then {};
// whole dimension
case ((DAE.WHOLEDIM() :: xs))
equation
xs_1 = unelabSubscripts(xs);
then
(Absyn.NOSUB() :: xs_1);
// slices
case ((DAE.SLICE(exp = e) :: xs))
equation
xs_1 = unelabSubscripts(xs);
e_1 = Expression.unelabExp(e);
then
(Absyn.SUBSCRIPT(e_1) :: xs_1);
// indexes
case ((DAE.INDEX(exp = e) :: xs))
equation
xs_1 = unelabSubscripts(xs);
e_1 = Expression.unelabExp(e);
then
(Absyn.SUBSCRIPT(e_1) :: xs_1);
case (DAE.WHOLE_NONEXP(exp = e) :: xs)
equation
xs_1 = unelabSubscripts(xs);
e_1 = Expression.unelabExp(e);
then
(Absyn.SUBSCRIPT(e_1) :: xs_1);
end match;
end unelabSubscripts;
public function toExpCref
"Translates an Absyn cref to an untyped DAE cref."
input Absyn.ComponentRef absynCref;
output DAE.ComponentRef daeCref;
algorithm
daeCref := match absynCref
local
case Absyn.CREF_IDENT()
then makeCrefIdent(absynCref.name, DAE.T_UNKNOWN_DEFAULT,
toExpCrefSubs(absynCref.subscripts));
case Absyn.CREF_QUAL()
then makeCrefQual(absynCref.name, DAE.T_UNKNOWN_DEFAULT,
toExpCrefSubs(absynCref.subscripts), toExpCref(absynCref.componentRef));
case Absyn.CREF_FULLYQUALIFIED()
then toExpCref(absynCref.componentRef);
case Absyn.WILD() then DAE.WILD();
case Absyn.ALLWILD() then DAE.WILD();
end match;
end toExpCref;
protected function toExpCrefSubs
"Translates a list of Absyn subscripts to a list of untyped DAE subscripts."
input list<Absyn.Subscript> absynSubs;
output list<DAE.Subscript> daeSubs;
protected
Integer i;
algorithm
daeSubs := list(
match sub
case Absyn.SUBSCRIPT() then DAE.INDEX(Expression.fromAbsynExp(sub.subscript));
case Absyn.NOSUB() then DAE.WHOLEDIM();
end match
for sub in absynSubs);
end toExpCrefSubs;
public function crefStr
"This function simply converts a ComponentRef to a String."
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString := stringDelimitList(toStringList(inComponentRef), if Flags.getConfigBool(Flags.MODELICA_OUTPUT) then "__" else ".");
end crefStr;
public function crefListStr
"This function simply converts a list of ComponentReferences to a String."
input list<DAE.ComponentRef> crList;
output String outString = "";
algorithm
for cr in crList loop
outString := outString + crefStr(cr) + "\n";
end for;
end crefListStr;
public function crefModelicaStr
"Same as crefStr, but uses _ instead of . "
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString := stringDelimitList(toStringList(inComponentRef), "_");
end crefModelicaStr;
public function printComponentRefOptStr
"@autor: adrpo
Print a cref or none"
input Option<DAE.ComponentRef> inComponentRefOpt;
output String outString;
algorithm
outString := match(inComponentRefOpt)
local
String str;
DAE.ComponentRef cref;
// none
case NONE() then "NONE()";
// some
case SOME(cref)
equation
str = printComponentRefStr(cref);
str = "SOME(" + str + ")";
then
str;
end match;
end printComponentRefOptStr;
public function printComponentRefStr
"Print a ComponentRef.
LS: print functions that return a string instead of printing
Had to duplicate the huge printExp2 and modify.
An alternative would be to implement sprint somehow
which would need internal state, with reset and
getString methods.
Once these are tested and ok, the printExp above can
be replaced by a call to these _str functions and
printing the result."
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString := match (inComponentRef)
local
DAE.Ident s,str,strrest,strseb;
list<DAE.Subscript> subs;
DAE.ComponentRef cr;
Boolean b;
Integer ix;
// Optimize -- a function call less
case (DAE.CREF_IDENT(ident = s,subscriptLst = {}))
then s;
// idents with subscripts
case DAE.CREF_IDENT(ident = s,subscriptLst = subs)
equation
str = printComponentRef2Str(s, subs);
then
str;
// Optimize -- a function call less
case (DAE.CREF_ITER(ident = s,index=ix,subscriptLst = {}))
then s + "/* iter index " + intString(ix) + " */";
// idents with subscripts
case DAE.CREF_ITER(ident = s,index=ix,subscriptLst = subs)
equation
str = printComponentRef2Str(s, subs);
then
str + "/* iter index " + intString(ix) + " */";
// Qualified - Modelica output - does not handle names with underscores
// Qualified - non Modelica output
case DAE.CREF_QUAL(ident = s,subscriptLst = subs,componentRef = cr)
equation
b = Config.modelicaOutput();
str = printComponentRef2Str(s, subs);
strrest = printComponentRefStr(cr);
strseb = if b then "__" else ".";
str = stringAppendList({str, strseb, strrest});
then
str;
// Wild
case DAE.WILD() then "_";
end match;
end printComponentRefStr;
public function printComponentRefStrFixDollarDer
"Like printComponentRefStr but also fixes the special dollar-sign variables"
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString := match (inComponentRef)
local
DAE.ComponentRef cr;
case (DAE.CREF_QUAL(ident = "$DER",subscriptLst = {},componentRef=cr))
then "der(" + printComponentRefStr(cr) + ")";
else printComponentRefStr(inComponentRef);
end match;
end printComponentRefStrFixDollarDer;
public function printComponentRef2Str
"Helper function to printComponentRefStr."
input DAE.Ident inIdent;
input list<DAE.Subscript> inSubscriptLst;
output String outString;
algorithm
outString := match (inIdent,inSubscriptLst)
local
DAE.Ident s,str,strseba,strsebb;
list<DAE.Subscript> l;
Boolean b;
// no subscripts
case (s,{}) then s;
// some subscripts, Modelica output
// some subscripts, non Modelica output
case (s,l)
equation
b = Config.modelicaOutput();
str = ExpressionDump.printListStr(l, ExpressionDump.printSubscriptStr, ",");
((strseba,strsebb)) = if b then ("_L","_R") else ("[","]");
str = stringAppendList({s, strseba, str, strsebb});
then
str;
end match;
end printComponentRef2Str;
public function debugPrintComponentRefTypeStr "Function: debugPrintComponentRefTypeStr
This function is equal to debugPrintComponentRefTypeStr with the extra feature that it
prints the base type of each ComponentRef.
NOTE Only used for debugging."
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString := match (inComponentRef)
local
DAE.Ident s,str,str2,strrest,str_1;
list<DAE.Subscript> subs;
DAE.ComponentRef cr;
DAE.Type ty;
case DAE.WILD() then "_";
case DAE.CREF_IDENT(ident = s,identType=ty,subscriptLst = subs)
equation
str_1 = ExpressionDump.printListStr(subs, ExpressionDump.debugPrintSubscriptStr, ", ");
str = s + (if stringLength(str_1) > 0 then "["+ str_1 + "]" else "");
str2 = Types.unparseType(ty);
str = stringAppendList({str," [",str2,"]"});
then
str;
// Does not handle names with underscores
case DAE.CREF_QUAL(ident = s,identType=ty,subscriptLst = subs,componentRef = cr)
equation
if (Config.modelicaOutput())
then
str = printComponentRef2Str(s, subs);
str2 = Types.unparseType(ty);
strrest = debugPrintComponentRefTypeStr(cr);
str = stringAppendList({str," [",str2,"] ", "__", strrest});
else
str_1 = ExpressionDump.printListStr(subs, ExpressionDump.debugPrintSubscriptStr, ", ");
str = s + (if stringLength(str_1) > 0 then "["+ str_1 + "]" else "");
str2 = Types.unparseType(ty);
strrest = debugPrintComponentRefTypeStr(cr);
str = stringAppendList({str," [",str2,"] ", ".", strrest});
end if;
then
str;
end match;
end debugPrintComponentRefTypeStr;
/***************************************************/
/* Compare */
/***************************************************/
public function crefLastIdentEqual
"author: Frenkel TUD
Returns true if the ComponentRefs has the same name (the last identifier)."
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean equal;
protected
DAE.Ident id1,id2;
algorithm
id1 := crefLastIdent(cr1);
id2 := crefLastIdent(cr2);
equal := stringEq(id1, id2);
end crefLastIdentEqual;
public function crefFirstCrefEqual
"author: Frenkel TUD
Returns true if the ComponentRefs have the same first Cref."
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean equal;
protected
DAE.ComponentRef pcr1,pcr2;
algorithm
pcr1 := crefFirstCref(cr1);
pcr2 := crefFirstCref(cr2);
equal := crefEqual(pcr1,pcr2);
end crefFirstCrefEqual;
public function crefFirstCrefLastCrefEqual
"author: Frenkel TUD
Returns true if the ComponentRefs have the same first Cref."
input DAE.ComponentRef cr1 "First Cref";
input DAE.ComponentRef cr2 "Last Cref";
output Boolean equal;
protected
DAE.ComponentRef pcr1,pcr2;
algorithm
pcr1 := crefFirstCref(cr1);
pcr2 := crefLastCref(cr2);
equal := crefEqual(pcr1,pcr2);
end crefFirstCrefLastCrefEqual;
public function crefFirstIdentEqual
"Returns true if the first identifier in both crefs are the same, otherwise false."
input DAE.ComponentRef inCref1;
input DAE.ComponentRef inCref2;
output Boolean outEqual;
protected
DAE.Ident id1, id2;
algorithm
id1 := crefFirstIdent(inCref1);
id2 := crefFirstIdent(inCref2);
outEqual := stringEq(id1, id2);
end crefFirstIdentEqual;
protected
type CompareWithSubsType = enumeration(WithoutSubscripts, WithGenericSubscript, WithGenericSubscriptNotAlphabetic, WithIntSubscript);
package CompareWithGenericSubscript "Package that can be modified to do different kinds of comparisons"
constant CompareWithSubsType compareSubscript=CompareWithSubsType.WithGenericSubscript;
function compare
input DAE.ComponentRef cr1, cr2;
output Integer res;
algorithm
res := match (cr1, cr2)
case (DAE.CREF_IDENT(),DAE.CREF_IDENT())
algorithm
res := stringCompare(cr1.ident, cr2.ident);
if compareSubscript==CompareWithSubsType.WithoutSubscripts or res <> 0 then
return;
end if;
then compareSubs(cr1.subscriptLst, cr2.subscriptLst);
case (DAE.CREF_QUAL(),DAE.CREF_QUAL())
algorithm
res := stringCompare(cr1.ident, cr2.ident);
if res <> 0 then
return;
end if;
if compareSubscript<>CompareWithSubsType.WithoutSubscripts then
res := compareSubs(cr1.subscriptLst, cr2.subscriptLst);
if res <> 0 then
return;
end if;
end if;
then compare(cr1.componentRef, cr2.componentRef);
case (DAE.CREF_QUAL(),DAE.CREF_IDENT())
algorithm
res := stringCompare(cr1.ident, cr2.ident);
if res <> 0 then
return;
end if;
if compareSubscript<>CompareWithSubsType.WithoutSubscripts then
res := compareSubs(cr1.subscriptLst, cr2.subscriptLst);
end if;
if res <> 0 then
return;
end if;
then 1;
case (DAE.CREF_IDENT(),DAE.CREF_QUAL())
algorithm
res := stringCompare(cr1.ident, cr2.ident);
if res <> 0 then
return;
end if;
if compareSubscript<>CompareWithSubsType.WithoutSubscripts then
res := compareSubs(cr1.subscriptLst, cr2.subscriptLst);
end if;
if res <> 0 then
return;
end if;
then -1;
end match;
end compare;
function compareSubs
input list<DAE.Subscript> ss1, ss2;
output Integer res=0;
protected
list<DAE.Subscript> ss=ss2;
DAE.Subscript s2;
Integer i1, i2;
DAE.Exp e1, e2;
algorithm
for s1 in ss1 loop
if listEmpty(ss) then
res := -1;
return;
end if;
s2::ss := ss;
if compareSubscript == CompareWithSubsType.WithGenericSubscript then
res := stringCompare(ExpressionDump.printSubscriptStr(s1), ExpressionDump.printSubscriptStr(s2));
elseif compareSubscript == CompareWithSubsType.WithGenericSubscriptNotAlphabetic then
res := Expression.compareSubscripts(s1, s2);
else
i1 := Expression.subscriptInt(s1);
i2 := Expression.subscriptInt(s2);
res := if i1 < i2 then -1 elseif i1 > i2 then 1 else 0;
end if;
if res <> 0 then
return;
end if;
end for;
if not listEmpty(ss) then
res := 1;
end if;
end compareSubs;
end CompareWithGenericSubscript;
package CompareWithGenericSubscriptNotAlphabetic
extends CompareWithGenericSubscript(compareSubscript=CompareWithSubsType.WithGenericSubscriptNotAlphabetic);
end CompareWithGenericSubscriptNotAlphabetic;
package CompareWithoutSubscripts
extends CompareWithGenericSubscript(compareSubscript=CompareWithSubsType.WithoutSubscripts);
end CompareWithoutSubscripts;
package CompareWithIntSubscript "More efficient than CompareWithGenericSubscript, assuming all subscripts are integers"
extends CompareWithGenericSubscript(compareSubscript=CompareWithSubsType.WithIntSubscript);
end CompareWithIntSubscript;
public function crefSortFunc "A sorting function (greatherThan) for crefs"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean greaterThan;
algorithm
greaterThan := CompareWithGenericSubscript.compare(cr1,cr2) > 0;
end crefSortFunc;
public function crefCompareGeneric "A sorting function for crefs"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Integer comp;
algorithm
comp := CompareWithGenericSubscript.compare(cr1,cr2);
end crefCompareGeneric;
public function crefCompareIntSubscript "A sorting function for crefs"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Integer comp;
algorithm
comp := CompareWithIntSubscript.compare(cr1,cr2);
end crefCompareIntSubscript;
public function crefCompareGenericNotAlphabetic "A sorting function for crefs"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Integer comp;
algorithm
comp := CompareWithGenericSubscriptNotAlphabetic.compare(cr1,cr2);
end crefCompareGenericNotAlphabetic;
public function crefLexicalGreaterSubsAtEnd
"mahge:
Compares two crefs lexically. Subscripts are treated as if they are
they are at the end of the whole component reference.
e.g. r[1].i is greater than r[2].a.
returns true if the first cref is greater than the second"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean isGreater;
algorithm
isGreater := crefLexicalCompareSubsAtEnd(cr1,cr2) > 0;
end crefLexicalGreaterSubsAtEnd;
public function crefLexicalCompareSubsAtEnd
"mahge:
Compares two crefs lexically. Subscripts are treated as if they are
they are at the end of the whole component reference.
e.g. r[1].i is greater than r[2].a.
returns value is same as C strcmp. 0 if equal, 1 if first is greater, -1 otherwise"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Integer res;
protected
list<Integer> subs1;
list<Integer> subs2;
algorithm
res := CompareWithoutSubscripts.compare(cr1, cr2);
if res <> 0 then
return;
end if;
subs1 := Expression.subscriptsInt(crefSubs(cr1));
subs2 := Expression.subscriptsInt(crefSubs(cr2));
res := crefLexicalCompareSubsAtEnd2(subs1, subs2);
end crefLexicalCompareSubsAtEnd;
protected function crefLexicalCompareSubsAtEnd2
"mahge:
Helper function for crefLexicalCompareubsAtEnd
compares subs. However only if the crefs with out subs are equal.
(i.e. identsCompared is 0)
otherwise just returns"
input list<Integer> inSubs1;
input list<Integer> inSubs2;
output Integer res = 0;
protected
list<Integer> rest=inSubs2;
algorithm
for i in inSubs1 loop
res::rest := rest;
res := if i>res then 1 elseif i<res then -1 else 0;
if res <> 0 then
return;
end if;
end for;
end crefLexicalCompareSubsAtEnd2;
public function crefContainedIn
"author: PA
Returns true if second arg is a sub component ref of first arg.
For instance, b.c. is a sub_component of a.b.c."
input DAE.ComponentRef containerCref "the cref that might contain";
input DAE.ComponentRef containedCref "cref that might be contained";
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (containerCref, containedCref)
local
DAE.ComponentRef full,partOf,cr2;
Boolean res;
// a qualified cref cannot be contained in an ident cref.
case (DAE.CREF_IDENT(), DAE.CREF_QUAL()) then false;
// see if they are equal
case (full, partOf)
equation
true = crefEqualNoStringCompare(full, partOf);
then
true;
// dive into
case (full as DAE.CREF_QUAL(componentRef = cr2), partOf)
equation
false = crefEqualNoStringCompare(full, partOf);
res = crefContainedIn(cr2,partOf);
then
res;
// anything else is false
else false;
end matchcontinue;
end crefContainedIn;
public function crefPrefixOf
"author: PA
Returns true if prefixCref is a prefix of fullCref
For example, a.b is a prefix of a.b.c.
adrpo 2010-10-07,
added also that a.b.c is a prefix of a.b.c[1].*!"
input DAE.ComponentRef prefixCref;
input DAE.ComponentRef fullCref;
output Boolean outPrefixOf;
algorithm
outPrefixOf := match (prefixCref,fullCref)
// both are qualified, dive into
case (DAE.CREF_QUAL(), DAE.CREF_QUAL())
then prefixCref.ident == fullCref.ident and
Expression.subscriptEqual(prefixCref.subscriptLst, fullCref.subscriptLst) and
crefPrefixOf(prefixCref.componentRef, fullCref.componentRef);
// adrpo: 2010-10-07: first is an ID, second is qualified, see if one is prefix of the other
// even if the first one DOESN'T HAVE SUBSCRIPTS!
case (DAE.CREF_IDENT(subscriptLst = {}), DAE.CREF_QUAL())
then prefixCref.ident == fullCref.ident;
// first is an ID, second is qualified, see if one is prefix of the other
case (DAE.CREF_IDENT(), DAE.CREF_QUAL())
then prefixCref.ident == fullCref.ident and
Expression.subscriptEqual(prefixCref.subscriptLst, fullCref.subscriptLst);
// adrpo: 2010-10-07: first is an ID, second is an ID, see if one is prefix of the other
// even if the first one DOESN'T HAVE SUBSCRIPTS!
case (DAE.CREF_IDENT(subscriptLst = {}), DAE.CREF_IDENT())
then stringEq(prefixCref.ident, fullCref.ident);
case (DAE.CREF_IDENT(), DAE.CREF_IDENT())
then prefixCref.ident == fullCref.ident and
Expression.subscriptEqual(prefixCref.subscriptLst, fullCref.subscriptLst);
// they are not a prefix of one-another
else false;
end match;
end crefPrefixOf;
public function crefPrefixOfIgnoreSubscripts
"author: PA
Returns true if prefixCref is a prefix of fullCref
For example, a.b is a prefix of a.b.c.
This function ignores the subscripts"
input DAE.ComponentRef prefixCref;
input DAE.ComponentRef fullCref;
output Boolean outPrefixOf;
algorithm
outPrefixOf := match (prefixCref,fullCref)
// both are qualified, dive into
case (DAE.CREF_QUAL(), DAE.CREF_QUAL())
then prefixCref.ident == fullCref.ident and
crefPrefixOfIgnoreSubscripts(prefixCref.componentRef, fullCref.componentRef);
// first is an ID, second is qualified, see if one is prefix of the other
case (DAE.CREF_IDENT(), DAE.CREF_QUAL())
then prefixCref.ident == fullCref.ident;
case (DAE.CREF_IDENT(), DAE.CREF_IDENT())
then prefixCref.ident == fullCref.ident;
// they are not a prefix of one-another
else false;
end match;
end crefPrefixOfIgnoreSubscripts;
public function crefNotPrefixOf "negation of crefPrefixOf"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean outBoolean;
algorithm
outBoolean := match(cr1, cr2)
// first is qualified, second is an unqualified ident, return false!
case (DAE.CREF_QUAL(), DAE.CREF_IDENT()) then true;
else not crefPrefixOf(cr1, cr2);
end match;
end crefNotPrefixOf;
public function crefEqual
"Returns true if two component references are equal.
No string comparison of unparsed crefs is performed!"