/
ComponentReference.mo
2627 lines (2335 loc) · 79.6 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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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
RCS: $Id$
This file contains the module ComponentReference,
which contains functions for ComponentRef."
// public imports
public import Absyn;
public import DAE;
// 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 Global;
protected import List;
protected import Print;
protected import System;
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 createEmptyCrefMemory
"@author: adrpo
creates an array, with one element for each record in DAE.ComponentRef!"
output array<list<DAE.ComponentRef>> crefMemory;
algorithm
crefMemory := arrayCreate(3, {});
end createEmptyCrefMemory;
protected function searchInMememoryLst
"@author: adrpo
This function searches in memory for already existing DAE.ComponentRef"
input DAE.ComponentRef inCref;
input list<DAE.ComponentRef> inMem;
output DAE.ComponentRef outCref;
algorithm
outCref := matchcontinue (inCref, inMem)
local
list<DAE.ComponentRef> rest;
DAE.ComponentRef cref;
// fail if we couldn't find it
case (inCref, {}) then fail();
// see if we have it in memory, return the already existing!
case (inCref, cref::rest)
equation
equality(inCref = cref);
then
cref;
// try the next
case (inCref, cref::rest)
equation
failure(equality(inCref = cref));
cref = searchInMememoryLst(inCref, rest);
then
cref;
end matchcontinue;
end searchInMememoryLst;
protected function shareCref
"@author: adrpo
searches in the global cache for the given cref and if
there is one, returns that pointer, otherwise adds it"
input DAE.ComponentRef inCref;
output DAE.ComponentRef outCref;
algorithm
outCref := matchcontinue (inCref)
local
array<list<DAE.ComponentRef>> crefMem;
list<DAE.ComponentRef> crefLst;
DAE.ComponentRef cref;
Integer indexBasedOnValueConstructor;
// see if we have it in memory
case (inCref) then inCref;
// see if we have it in memory
case (inCref)
equation
// oh the horror. if you don't understand this, contact adrpo
// get from global roots
crefMem = getGlobalRoot(Global.crefIndex);
// select a list based on the constructor of DAE.ComponentRef value
indexBasedOnValueConstructor = valueConstructor(inCref);
crefLst = arrayGet(crefMem, indexBasedOnValueConstructor + 1);
// search in the list for already existing one
cref = searchInMememoryLst(inCref, crefLst);
// print("Lst: " +& intString(listLength(crefLst)) +&
// " Shared: " +& printComponentRefStr(cref) +& "\n");
then
cref;
// we didn't find it, add it
case (inCref)
equation
// oh the horror. if you don't understand this, contact adrpo
// get from global roots
crefMem = getGlobalRoot(Global.crefIndex);
// select a list based on the constructor of DAE.ComponentRef value
indexBasedOnValueConstructor = valueConstructor(inCref);
crefLst = arrayGet(crefMem, indexBasedOnValueConstructor + 1);
// add the translation to the list and set the array
crefMem = arrayUpdate(crefMem, indexBasedOnValueConstructor + 1, inCref::crefLst);
// set the global cache with the new value
setGlobalRoot(Global.crefIndex, crefMem);
then
inCref;
end matchcontinue;
end shareCref;
/***************************************************/
/* 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; // shareCref(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); // shareCref(DAE.CREF_IDENT(ident, identType, subscriptLst));
end makeCrefIdent;
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
"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
"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
" 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
"function: unelabCref
Transform an DAE.ComponentRef into Absyn.ComponentRef."
input DAE.ComponentRef inComponentRef;
output Absyn.ComponentRef outComponentRef;
algorithm
outComponentRef := match (inComponentRef)
local
list<Absyn.Subscript> subs_1;
DAE.Ident id;
list<DAE.Subscript> subs;
Absyn.ComponentRef cr_1;
DAE.ComponentRef cr;
// 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);
end match;
end unelabCref;
protected function unelabSubscripts
"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);
end match;
end unelabSubscripts;
public function toExpCref
"function: toExpCref
Translate an Absyn.ComponentRef into a ComponentRef.
Note: Only support for indexed subscripts of integers"
input Absyn.ComponentRef inComponentRef;
output DAE.ComponentRef outComponentRef;
algorithm
outComponentRef := match (inComponentRef)
local
list<DAE.Subscript> subs_1;
DAE.Ident id;
list<Absyn.Subscript> subs;
DAE.ComponentRef cr_1;
Absyn.ComponentRef cr;
// ids
case (Absyn.CREF_IDENT(name = id,subscripts = subs))
equation
subs_1 = toExpCrefSubs(subs);
then
makeCrefIdent(id,DAE.T_UNKNOWN_DEFAULT,subs_1);
// qualified
case (Absyn.CREF_QUAL(name = id,subscripts = subs,componentRef = cr))
equation
cr_1 = toExpCref(cr);
subs_1 = toExpCrefSubs(subs);
then
makeCrefQual(id,DAE.T_UNKNOWN_DEFAULT,subs_1,cr_1);
// fully qualified
case (Absyn.CREF_FULLYQUALIFIED(componentRef = cr))
equation
cr_1 = toExpCref(cr);
then
cr_1; // There is no fullyqualified cref, translate to qualified cref
end match;
end toExpCref;
protected function toExpCrefSubs
"function: toExpCrefSubs
Helper function to toExpCref."
input list<Absyn.Subscript> inAbsynSubscriptLst;
output list<DAE.Subscript> outSubscriptLst;
algorithm
outSubscriptLst := matchcontinue (inAbsynSubscriptLst)
local
list<DAE.Subscript> xs_1;
Integer i;
list<Absyn.Subscript> xs;
DAE.ComponentRef cr_1;
Absyn.ComponentRef cr;
DAE.Ident s,str;
Absyn.Subscript e;
DAE.Exp exp;
// empty list
case ({}) then {};
// integer subscripts become indexes of integers
case ((Absyn.SUBSCRIPT(subscript = Absyn.INTEGER(value = i)) :: xs))
equation
xs_1 = toExpCrefSubs(xs);
then
(DAE.INDEX(DAE.ICONST(i)) :: xs_1);
// cref subscripts become indexes of crefs
// => Assumes index is INTEGER. FIXME! TODO!: what about if index is an array?
case ((Absyn.SUBSCRIPT(subscript = Absyn.CREF(componentRef = cr)) :: xs))
equation
cr_1 = toExpCref(cr);
xs_1 = toExpCrefSubs(xs);
exp = Expression.makeCrefExp(cr_1,DAE.T_INTEGER_DEFAULT);
then
(DAE.INDEX(exp) :: xs_1);
// when there is an error, move to next TODO! FIXME! report an error!
case ((e :: xs))
equation
s = Dump.printSubscriptsStr({e});
str = stringAppendList({"#Error converting subscript: ",s," to Expression.\n"});
//print("#Error converting subscript: " +& s +& " to Expression.\n");
//Print.printErrorBuf(str);
xs_1 = toExpCrefSubs(xs);
then
xs_1;
end matchcontinue;
end toExpCrefSubs;
public function crefToStr
"function: crefStr
This function converts a ComponentRef to a String.
It is a tail recursive implementation, because of that it
neads inPreString. Use inNameSeperator to define the
Separator inbetween and between the namespace names and the name"
input String inPreString;
input DAE.ComponentRef inComponentRef "The ComponentReference";
input String inNameSeparator "The Separator between the Names";
output String outString;
algorithm
outString:=
match (inPreString,inComponentRef,inNameSeparator)
local
DAE.Ident s,ns,ss;
DAE.ComponentRef n;
case (inPreString,DAE.CREF_IDENT(ident = s),_)
equation
ss = stringAppend(inPreString, s);
then ss;
case (inPreString,DAE.CREF_QUAL(ident = s,componentRef = n),inNameSeparator)
equation
ns = stringAppendList({inPreString, s, inNameSeparator});
ss = crefToStr(ns,n,inNameSeparator);
then
ss;
end match;
end crefToStr;
public function crefStr
"function: crefStr
This function simply converts a ComponentRef to a String."
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString:= crefToStr("",inComponentRef,".");
end crefStr;
public function crefModelicaStr
"function: crefModelicaStr
Same as crefStr, but uses _ instead of . "
input DAE.ComponentRef inComponentRef;
output String outString;
algorithm
outString:= crefToStr("",inComponentRef,"_");
end crefModelicaStr;
public function printComponentRefOptStr
"@autor: adrpo
Print a cref or none"
input Option<DAE.ComponentRef> inComponentRefOpt;
output String outString;
algorithm
outString := matchcontinue(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 matchcontinue;
end printComponentRefOptStr;
public function printComponentRefStr
"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 := matchcontinue (inComponentRef)
local
DAE.Ident s,str,strrest;
list<DAE.Subscript> subs;
DAE.ComponentRef cr;
DAE.Type ty;
// Optimize -- a function call less
case (DAE.CREF_IDENT(ident = s,identType = ty,subscriptLst = {}))
then s;
// idents with subscripts
case DAE.CREF_IDENT(ident = s,identType = ty, subscriptLst = subs)
equation
str = printComponentRef2Str(s, subs);
then
str;
// Qualified - Modelica output - does not handle names with underscores
case DAE.CREF_QUAL(ident = s,subscriptLst = subs,componentRef = cr)
equation
true = Config.modelicaOutput();
str = printComponentRef2Str(s, subs);
strrest = printComponentRefStr(cr);
str = stringAppendList({str, "__", strrest});
then
str;
// Qualified - non Modelica output
case DAE.CREF_QUAL(ident = s,subscriptLst = subs,componentRef = cr)
equation
false = Config.modelicaOutput();
str = printComponentRef2Str(s, subs);
strrest = printComponentRefStr(cr);
str = stringAppendList({str, ".", strrest});
then
str;
// Wild
case DAE.WILD() then "_";
end matchcontinue;
end printComponentRefStr;
public function printComponentRef2Str
"function: printComponentRef2Str
Helper function to printComponentRefStr."
input DAE.Ident inIdent;
input list<DAE.Subscript> inSubscriptLst;
output String outString;
algorithm
outString := matchcontinue (inIdent,inSubscriptLst)
local
DAE.Ident s,str;
list<DAE.Subscript> l;
// no subscripts
case (s,{}) then s;
// some subscripts, Modelica output
case (s,l)
equation
true = Config.modelicaOutput();
str = ExpressionDump.printListStr(l, ExpressionDump.printSubscriptStr, ",");
str = stringAppendList({s, "_L", str, "_R"});
then
str;
// some subscripts, non Modelica output
case (s,l)
equation
false = Config.modelicaOutput();
str = ExpressionDump.printListStr(l, ExpressionDump.printSubscriptStr, ",");
str = stringAppendList({s, "[", str, "]"});
then
str;
end matchcontinue;
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 := matchcontinue (inComponentRef)
local
DAE.Ident s,str,str2,strrest,str_1;
list<DAE.Subscript> subs;
DAE.ComponentRef cr;
DAE.Type ty;
case DAE.CREF_IDENT(ident = s,identType=ty,subscriptLst = subs)
equation
str_1 = ExpressionDump.printListStr(subs, ExpressionDump.debugPrintSubscriptStr, ",");
str = s +& Util.if_(stringLength(str_1) > 0, "["+& str_1 +& "}" , "");
// this printing way will be useful when adressin the 'crefEqual' bug.
// str = ComponentReference.printComponentRef2Str(s, subs);
str2 = ExpressionDump.typeString(ty);
str = stringAppendList({str," [",str2,"]"});
then
str;
case DAE.CREF_QUAL(ident = s,identType=ty,subscriptLst = subs,componentRef = cr) /* Does not handle names with underscores */
equation
true = Config.modelicaOutput();
str = printComponentRef2Str(s, subs);
str2 = ExpressionDump.typeString(ty);
strrest = debugPrintComponentRefTypeStr(cr);
str = stringAppendList({str," [",str2,"] ", "__", strrest});
then
str;
case DAE.CREF_QUAL(ident = s,identType=ty,subscriptLst = subs,componentRef = cr)
equation
false = Config.modelicaOutput();
str = printComponentRef2Str(s, subs);
str2 = ExpressionDump.typeString(ty);
strrest = debugPrintComponentRefTypeStr(cr);
str = stringAppendList({str," [",str2,"] ", ".", strrest});
then
str;
case DAE.WILD() then "_";
end matchcontinue;
end debugPrintComponentRefTypeStr;
/***************************************************/
/* Compare */
/***************************************************/
public function crefLastIdentEqual
"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
"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
"function: crefFirstCrefEqual
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 crefSortFunc "A sorting function (greatherThan) for crefs"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean greaterThan;
algorithm
greaterThan := stringCompare(printComponentRefStr(cr1),printComponentRefStr(cr2)) > 0;
end crefSortFunc;
public function crefContainedIn
"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(ident = _), DAE.CREF_QUAL(componentRef = _)) 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
case (_,_) then false;
end matchcontinue;
end crefContainedIn;
public function crefPrefixOf
"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 outBoolean;
algorithm
outBoolean := match (prefixCref,fullCref)
local
DAE.ComponentRef cr1,cr2;
Boolean res;
DAE.Ident id1,id2;
list<DAE.Subscript> ss1,ss2;
// first is qualified, second is an unqualified ident, return false!
case (DAE.CREF_QUAL(ident = _), DAE.CREF_IDENT(ident = _)) then false;
// both are qualified, dive into
case (DAE.CREF_QUAL(ident = id1, subscriptLst = ss1,componentRef = cr1),
DAE.CREF_QUAL(ident = id2, subscriptLst = ss2,componentRef = cr2))
equation
res = stringEq(id1, id2);
res = Debug.bcallret2(res, Expression.subscriptEqual, ss1, ss2, false);
res = Debug.bcallret2(res, crefPrefixOf, cr1, cr2, false);
then
res;
// 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(ident = id1,subscriptLst = {}),
DAE.CREF_QUAL(ident = id2,subscriptLst = ss2))
then stringEq(id1, id2);
// first is an ID, second is qualified, see if one is prefix of the other
case (DAE.CREF_IDENT(ident = id1,subscriptLst = ss1),
DAE.CREF_QUAL(ident = id2,subscriptLst = ss2))
equation
res = Debug.bcallret2(stringEq(id1, id2), Expression.subscriptEqual, ss1, ss2, false);
then
res;
// 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(ident = id1,subscriptLst = {}),
DAE.CREF_IDENT(ident = id2,subscriptLst = ss2))
then stringEq(id1, id2);
case (DAE.CREF_IDENT(ident = id1,subscriptLst = ss1),
DAE.CREF_IDENT(ident = id2,subscriptLst = ss2))
equation
res = Debug.bcallret2(stringEq(id1, id2), Expression.subscriptEqual, ss1, ss2, false);
then
res;
/* adrpo: 2010-10-07. already handled by the cases above!
they might be equal, a.b.c is a prefix of a.b.c
case (cr1,cr2)
equation
true = crefEqualNoStringCompare(cr1, cr2);
then
true;*/
// they are not a prefix of one-another
case (cr1,cr2)
equation
// print("Expression.crefPrefixOf: " +& printComponentRefStr(cr1) +& " NOT PREFIX OF " +& printComponentRefStr(cr2) +& "\n");
then false;
end match;
end crefPrefixOf;
public function crefNotPrefixOf "negation of crefPrefixOf"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue(cr1, cr2)
// first is qualified, second is an unqualified ident, return false!
case (DAE.CREF_QUAL(ident = _), DAE.CREF_IDENT(ident = _)) then true;
case (cr1, cr2) then (not crefPrefixOf(cr1,cr2));
end matchcontinue;
end crefNotPrefixOf;
public function crefEqual
"function: crefEqual
Returns true if two component references are equal.
No string comparison of unparsed crefs is performed!"
input DAE.ComponentRef inComponentRef1;
input DAE.ComponentRef inComponentRef2;
output Boolean outBoolean;
algorithm
outBoolean := crefEqualStringCompare(inComponentRef1,inComponentRef2);
end crefEqual;
public function crefEqualStringCompare
"function: crefEqualStringCompare
Returns true if two component references are equal,
comparing strings in no other solution is found"
input DAE.ComponentRef inComponentRef1;
input DAE.ComponentRef inComponentRef2;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inComponentRef1,inComponentRef2)
local
DAE.Ident n1,n2,s1,s2;
list<DAE.Subscript> idx1,idx2;
DAE.ComponentRef cr1,cr2;
// check for pointer equality first, if they point to the same thing, they are equal
case (inComponentRef1,inComponentRef2)
equation
true = referenceEq(inComponentRef1,inComponentRef2);
then
true;
// simple identifiers
case (DAE.CREF_IDENT(ident = n1,subscriptLst = {}),DAE.CREF_IDENT(ident = n2,subscriptLst = {}))
equation
true = stringEq(n1, n2);
then
true;
case (DAE.CREF_IDENT(ident = n1,subscriptLst = (idx1 as _::_)),DAE.CREF_IDENT(ident = n2,subscriptLst = (idx2 as _::_)))
equation
true = stringEq(n1, n2);
true = Expression.subscriptEqual(idx1, idx2);
then
true;
// BZ 2009-12
// For some reason in some examples we get crefs on different forms.
// the compare can be crefEqual(CREF_IDENT("mycref",_,{1,2,3}),CREF_IDENT("mycref[1,2,3]",_,{}))
// I do belive this has something to do with variable replacement and BackendDAE.
// TODO: investigate reason, until then keep as is.
// I do believe that this is the same bug as adrians qual-ident bug below.
case (DAE.CREF_IDENT(ident = n1,subscriptLst = {}),DAE.CREF_IDENT(ident = n2,subscriptLst = (idx2 as _::_)))
equation
0 = System.stringFind(n1, n2); // n2 should be first in n1!
s1 = n2 +& "[" +& ExpressionDump.printListStr(idx2, ExpressionDump.printSubscriptStr, ",") +& "]";
true = stringEq(s1,n1);
then
true;
case (DAE.CREF_IDENT(ident = n1,subscriptLst = (idx2 as _::_)),DAE.CREF_IDENT(ident = n2,subscriptLst = {}))
equation
0 = System.stringFind(n2, n1); // n1 should be first in n2!
s1 = n1 +& "[" +& ExpressionDump.printListStr(idx2, ExpressionDump.printSubscriptStr, ",") +& "]";
true = stringEq(s1,n2);
then
true;
// qualified crefs
case (DAE.CREF_QUAL(ident = n1,subscriptLst = idx1,componentRef = cr1),DAE.CREF_QUAL(ident = n2,subscriptLst = idx2,componentRef = cr2))
equation
true = stringEq(n1, n2);
true = crefEqualStringCompare(cr1, cr2);
true = Expression.subscriptEqual(idx1, idx2);
then
true;
// this is a VERY expensive case! Do we NEED IT??!!
// There is a bug here somewhere or in MetaModelica Compiler (MMC).
// Therefore as a last resort, print the strings and compare.
// adrpo: this is really not needed BUT unfortunately IT IS as
// QUAL(x, IDENT(y)) == IDENT(x.y)
// somewhere in the compiler the lhs is replaced by the rhs
// and makes this case needed! THIS SHOULD BE FIXED!! TODO! FIXME!
// NOTE: THIS IS NOT A BUG IN MMC!
/* adrpo: comment this and try to make it work faster with the two cases below!
case (cr1 as DAE.CREF_QUAL(ident = n1),cr2 as DAE.CREF_IDENT)
equation
s1 = printComponentRefStr(cr1);
s2 = printComponentRefStr(cr2);
true = stringEq(s1, s2);
// debug_print("cr1", cr1);
// debug_print("cr2", cr2);
// enableTrace();
then
true;
*/
// the following two cases replaces the one below
// right cref is stringified!
case (cr1 as DAE.CREF_QUAL(ident = n1),cr2 as DAE.CREF_IDENT(ident = n2))
equation
0 = System.stringFind(n2, n1); // n1 should be first in n2!
s1 = printComponentRefStr(cr1);
s2 = printComponentRefStr(cr2);
true = stringEq(s1, s2);
then
true;
// left cref is stringified!
case (cr1 as DAE.CREF_IDENT(ident = n1),cr2 as DAE.CREF_QUAL(ident = n2))
equation
0 = System.stringFind(n1, n2); // n2 should be first in n1!
s1 = printComponentRefStr(cr1);
s2 = printComponentRefStr(cr2);
true = stringEq(s1, s2);
then
true;
// the crefs are not equal!
case (_,_) then false;
end matchcontinue;
end crefEqualStringCompare;
public function crefEqualNoStringCompare
"function: crefEqualNoStringCompare
Returns true if two component references are equal!
IMPORTANT! do not use this function if you have
stringified components, meaning this function will
return false for: cref1: QUAL(x, IDENT(x)) != cref2: IDENT(x.y)"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean res;
algorithm
res := crefEqualNoStringCompare2(referenceEq(cr1,cr2),cr1,cr2);
end crefEqualNoStringCompare;
protected function crefEqualNoStringCompare2
"function: crefEqualNoStringCompare
Returns true if two component references are equal!
IMPORTANT! do not use this function if you have
stringified components, meaning this function will
return false for: cref1: QUAL(x, IDENT(x)) != cref2: IDENT(x.y)"
input Boolean refEq;
input DAE.ComponentRef inComponentRef1;
input DAE.ComponentRef inComponentRef2;
output Boolean res;
algorithm
res := match (refEq,inComponentRef1,inComponentRef2)
local
DAE.Ident n1,n2;
list<DAE.Subscript> idx1,idx2;
DAE.ComponentRef cr1,cr2;
// check for pointer equality first, if they point to the same thing, they are equal
case (true,_,_) then true;
// simple identifiers
case (_,DAE.CREF_IDENT(ident = n1,subscriptLst = idx1),DAE.CREF_IDENT(ident = n2,subscriptLst = idx2))
equation
res = stringEq(n1, n2);
res = Debug.bcallret2(res, Expression.subscriptEqual, idx1, idx2, false);
then res;
// qualified crefs
case (_,DAE.CREF_QUAL(ident = n1,subscriptLst = idx1,componentRef = cr1),DAE.CREF_QUAL(ident = n2,subscriptLst = idx2,componentRef = cr2))
equation
res = stringEq(n1, n2);
res = Debug.bcallret3(res, crefEqualNoStringCompare2, referenceEq(cr1,cr2), cr1, cr2, false);
res = Debug.bcallret2(res, Expression.subscriptEqual, idx1, idx2, false);