-
Notifications
You must be signed in to change notification settings - Fork 299
/
NFSubscript.mo
1360 lines (1188 loc) · 37.7 KB
/
NFSubscript.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 uniontype NFSubscript
protected
import DAE;
import List;
import SimplifyExp = NFSimplifyExp;
import Type = NFType;
import RangeIterator = NFRangeIterator;
import Dump;
import ExpandExp = NFExpandExp;
import Prefixes = NFPrefixes;
import Ceval = NFCeval;
import MetaModelica.Dangerous.listReverseInPlace;
import Util;
import JSON;
public
import Expression = NFExpression;
import Absyn;
import AbsynUtil;
import Dimension = NFDimension;
import NFPrefixes.{Variability, Purity};
import NFCeval.EvalTarget;
import NFInstNode.InstNode;
import ComponentRef = NFComponentRef;
import Subscript = NFSubscript;
record RAW_SUBSCRIPT
Absyn.Subscript subscript;
end RAW_SUBSCRIPT;
record UNTYPED
Expression exp;
end UNTYPED;
record INDEX
Expression index;
end INDEX;
record SLICE
Expression slice;
end SLICE;
record EXPANDED_SLICE
list<Subscript> indices;
end EXPANDED_SLICE;
record WHOLE end WHOLE;
// Split proxy and index subscripts are added to modifier array expressions to
// indicate where they are split when propagating them down to the array
// elements. Proxies are added during the instantiation and then replaced with
// split indices during typing once the number of dimensions on elements are known.
record SPLIT_PROXY
InstNode origin;
InstNode parent;
end SPLIT_PROXY;
record SPLIT_INDEX
InstNode node;
Integer dimIndex;
end SPLIT_INDEX;
function fromExp
input Expression exp;
output Subscript subscript;
algorithm
subscript := match exp
case Expression.INTEGER() then INDEX(exp);
case Expression.BOOLEAN() then INDEX(exp);
case Expression.ENUM_LITERAL() then INDEX(exp);
else UNTYPED(exp);
end match;
end fromExp;
function fromTypedExp
input Expression exp;
output Subscript subscript;
algorithm
subscript := if Type.isArray(Expression.typeOf(exp)) then SLICE(exp) else INDEX(exp);
end fromTypedExp;
function toExp
input Subscript subscript;
output Expression exp;
algorithm
exp := match subscript
case UNTYPED() then subscript.exp;
case INDEX() then subscript.index;
case SLICE() then subscript.slice;
end match;
end toExp;
function toInteger
input Subscript subscript;
output Integer int;
algorithm
int := match subscript
case INDEX() then Expression.toInteger(subscript.index);
end match;
end toInteger;
function toIndexList
input Subscript subscript;
input Integer length;
input Boolean baseZero = true;
output list<Integer> indices;
protected
Integer shift = if baseZero then 1 else 0;
algorithm
indices := match subscript
local
array<Expression> elems;
Integer start, step, stop;
case INDEX() then {toInteger(subscript)-shift};
case WHOLE() then List.intRange2(1-shift,length-shift);
case SLICE(slice = Expression.ARRAY(elements = elems))
then list(Expression.toInteger(e) for e in elems);
case SLICE(slice = Expression.RANGE(
start = Expression.INTEGER(start),
step = SOME(Expression.INTEGER(step)),
stop = Expression.INTEGER(stop)))
then List.intRange3(start-shift, step, stop-shift);
case SLICE(slice = Expression.RANGE(
start = Expression.INTEGER(start),
step = NONE(),
stop = Expression.INTEGER(stop)))
then List.intRange2(start-shift, stop-shift);
else algorithm
Error.assertion(false, getInstanceName() + " got an incorrect subscript type " + toString(subscript) + ".", sourceInfo());
then fail();
end match;
end toIndexList;
protected function isValidIndexType
input Type ty;
output Boolean b = Type.isInteger(ty) or Type.isBoolean(ty) or Type.isEnumeration(ty);
end isValidIndexType;
public
function makeIndex
input Expression exp;
output Subscript subscript;
protected
Type ty;
algorithm
ty := Expression.typeOf(exp);
if isValidIndexType(ty) then
subscript := INDEX(exp);
else
Error.assertion(false, getInstanceName() + " got a non integer type exp to make an index sub", sourceInfo());
fail();
end if;
end makeIndex;
function makeSplitIndex
input InstNode node;
input Integer dimIndex;
output Subscript subscript = SPLIT_INDEX(node, dimIndex);
algorithm
if dimIndex < 1 then
Error.assertion(false, getInstanceName() + " got invalid index " + String(dimIndex), sourceInfo());
end if;
end makeSplitIndex;
function isIndex
input Subscript sub;
output Boolean isIndex;
algorithm
isIndex := match sub
case INDEX() then true;
else false;
end match;
end isIndex;
function isWhole
input Subscript sub;
output Boolean isWhole;
algorithm
isWhole := match sub
case WHOLE() then true;
else false;
end match;
end isWhole;
function isSimple
"used for determining if its simple enough to use for an array equation
in the case of non scalarization (new backend)"
input Subscript sub;
output Boolean isSimple = isIndex(sub) or isWhole(sub);
end isSimple;
function isSliced
input Subscript sub;
output Boolean sliced;
algorithm
sliced := match sub
case SLICE() then true;
case WHOLE() then true;
else false;
end match;
end isSliced;
function isScalar
input Subscript sub;
output Boolean isScalar;
algorithm
isScalar := match sub
local
Type ty;
case INDEX() algorithm
ty := Expression.typeOf(sub.index);
then
isValidIndexType(ty);
case SPLIT_INDEX() then true;
else false;
end match;
end isScalar;
function isScalarLiteral
input Subscript sub;
output Boolean isScalarLiteral;
algorithm
isScalarLiteral := match sub
case INDEX() then Expression.isScalarLiteral(sub.index);
else false;
end match;
end isScalarLiteral;
function equalsIterator
input Subscript sub;
input InstNode iterator;
output Boolean res;
protected
ComponentRef cref;
algorithm
res := match sub
case UNTYPED(exp = Expression.CREF(cref = cref))
then InstNode.refEqual(iterator, ComponentRef.node(cref));
case INDEX(index = Expression.CREF(cref = cref))
then InstNode.refEqual(iterator, ComponentRef.node(cref));
else false;
end match;
end equalsIterator;
function isIterator
input Subscript sub;
output Boolean res;
protected
ComponentRef cref;
algorithm
res := match sub
case UNTYPED() then Expression.isIterator(sub.exp);
case INDEX() then Expression.isIterator(sub.index);
else false;
end match;
end isIterator;
function toIterator
input Subscript sub;
output InstNode iterator;
protected
ComponentRef cref;
algorithm
iterator := match sub
case UNTYPED(exp = Expression.CREF(cref = cref))
guard ComponentRef.isIterator(cref)
then ComponentRef.node(cref);
case INDEX(index = Expression.CREF(cref = cref))
guard ComponentRef.isIterator(cref)
then ComponentRef.node(cref);
else InstNode.EMPTY_NODE();
end match;
end toIterator;
function isBackendIterator
input Subscript sub;
output Boolean res;
protected
ComponentRef cref;
algorithm
res := match sub
case INDEX(index = Expression.CREF(cref = cref))
then ComponentRef.isIterator(cref);
else false;
end match;
end isBackendIterator;
function isEqual
input Subscript subscript1;
input Subscript subscript2;
output Boolean isEqual;
algorithm
isEqual := match (subscript1, subscript2)
case (RAW_SUBSCRIPT(), RAW_SUBSCRIPT())
then AbsynUtil.subscriptEqual(subscript1.subscript, subscript2.subscript);
case (UNTYPED(), UNTYPED())
then Expression.isEqual(subscript1.exp, subscript2.exp);
case (INDEX(), INDEX())
then Expression.isEqual(subscript1.index, subscript2.index);
case (SLICE(), SLICE())
then Expression.isEqual(subscript1.slice, subscript2.slice);
case (WHOLE(), WHOLE()) then true;
case (SPLIT_INDEX(), SPLIT_INDEX())
then subscript1.dimIndex == subscript2.dimIndex and
InstNode.refEqual(subscript1.node, subscript2.node);
else false;
end match;
end isEqual;
function isEqualList
input list<Subscript> subscripts1;
input list<Subscript> subscripts2;
output Boolean isEqual;
protected
Subscript s2;
list<Subscript> rest = subscripts2;
algorithm
for s1 in subscripts1 loop
if listEmpty(rest) then
isEqual := false;
return;
end if;
s2 :: rest := rest;
if not isEqual(s1, s2) then
isEqual := false;
return;
end if;
end for;
isEqual := listEmpty(rest);
end isEqualList;
function compare
input Subscript subscript1;
input Subscript subscript2;
output Integer comp;
algorithm
if referenceEq(subscript1, subscript2) then
comp := 0;
return;
end if;
comp := Util.intCompare(valueConstructor(subscript1), valueConstructor(subscript2));
if comp <> 0 then
return;
end if;
comp := match subscript1
local
Expression e;
InstNode node;
Integer index;
case UNTYPED()
algorithm
UNTYPED(exp = e) := subscript2;
then
Expression.compare(subscript1.exp, e);
case INDEX()
algorithm
INDEX(index = e) := subscript2;
then
Expression.compare(subscript1.index, e);
case SLICE()
algorithm
SLICE(slice = e) := subscript2;
then
Expression.compare(subscript1.slice, e);
case WHOLE() then 0;
case SPLIT_INDEX()
algorithm
SPLIT_INDEX(node = node, dimIndex = index) := subscript2;
comp := InstNode.refCompare(subscript1.node, node);
then
if comp == 0 then Util.intCompare(subscript1.dimIndex, index) else comp;
end match;
end compare;
function compareList
input list<Subscript> subscripts1;
input list<Subscript> subscripts2;
output Integer comp;
protected
Subscript s2;
list<Subscript> rest_s2 = subscripts2;
algorithm
comp := Util.intCompare(listLength(subscripts1), listLength(subscripts2));
if comp <> 0 then
return;
end if;
for s1 in subscripts1 loop
s2 :: rest_s2 := rest_s2;
comp := compare(s1, s2);
if comp <> 0 then
return;
end if;
end for;
comp := 0;
end compareList;
function containsExp
input Subscript subscript;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
res := match subscript
case UNTYPED() then Expression.contains(subscript.exp, func);
case INDEX() then Expression.contains(subscript.index, func);
case SLICE() then Expression.contains(subscript.slice, func);
else false;
end match;
end containsExp;
function listContainsExp
input list<Subscript> subscripts;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
for s in subscripts loop
if containsExp(s, func) then
res := true;
return;
end if;
end for;
res := false;
end listContainsExp;
function containsExpShallow
input Subscript subscript;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
res := match subscript
case UNTYPED() then func(subscript.exp);
case INDEX() then func(subscript.index);
case SLICE() then func(subscript.slice);
else false;
end match;
end containsExpShallow;
function listContainsExpShallow
input list<Subscript> subscripts;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
for s in subscripts loop
if containsExpShallow(s, func) then
res := true;
return;
end if;
end for;
res := false;
end listContainsExpShallow;
function applyExp
input Subscript subscript;
input ApplyFunc func;
partial function ApplyFunc
input Expression exp;
end ApplyFunc;
algorithm
() := match subscript
case UNTYPED() algorithm Expression.apply(subscript.exp, func); then ();
case INDEX() algorithm Expression.apply(subscript.index, func); then ();
case SLICE() algorithm Expression.apply(subscript.slice, func); then ();
else ();
end match;
end applyExp;
function applyExpShallow
input Subscript subscript;
input ApplyFunc func;
partial function ApplyFunc
input Expression exp;
end ApplyFunc;
algorithm
() := match subscript
case UNTYPED() algorithm func(subscript.exp); then ();
case INDEX() algorithm func(subscript.index); then ();
case SLICE() algorithm func(subscript.slice); then ();
else ();
end match;
end applyExpShallow;
function mapExp
input Subscript subscript;
input MapFunc func;
output Subscript outSubscript;
partial function MapFunc
input output Expression e;
end MapFunc;
algorithm
outSubscript := match subscript
local
Expression e1, e2;
case UNTYPED(exp = e1)
algorithm
e2 := Expression.map(e1, func);
then
if referenceEq(e1, e2) then subscript else UNTYPED(e2);
case INDEX(index = e1)
algorithm
e2 := Expression.map(e1, func);
then
if referenceEq(e1, e2) then subscript else fromTypedExp(e2);
case SLICE(slice = e1)
algorithm
e2 := Expression.map(e1, func);
then
if referenceEq(e1, e2) then subscript else fromTypedExp(e2);
else subscript;
end match;
end mapExp;
function mapShallowExp
input Subscript subscript;
input MapFunc func;
output Subscript outSubscript;
partial function MapFunc
input output Expression e;
end MapFunc;
algorithm
outSubscript := match subscript
local
Expression e1, e2;
case UNTYPED(exp = e1)
algorithm
e2 := func(e1);
then
if referenceEq(e1, e2) then subscript else UNTYPED(e2);
case INDEX(index = e1)
algorithm
e2 := func(e1);
then
if referenceEq(e1, e2) then subscript else fromTypedExp(e2);
case SLICE(slice = e1)
algorithm
e2 := func(e1);
then
if referenceEq(e1, e2) then subscript else fromTypedExp(e2);
else subscript;
end match;
end mapShallowExp;
function foldExp<ArgT>
input Subscript subscript;
input FoldFunc func;
input ArgT arg;
output ArgT result;
partial function FoldFunc
input Expression exp;
input output ArgT arg;
end FoldFunc;
algorithm
result := match subscript
case UNTYPED() then Expression.fold(subscript.exp, func, arg);
case INDEX() then Expression.fold(subscript.index, func, arg);
case SLICE() then Expression.fold(subscript.slice, func, arg);
else arg;
end match;
end foldExp;
function mapFoldExp<ArgT>
input Subscript subscript;
input MapFunc func;
output Subscript outSubscript;
input output ArgT arg;
partial function MapFunc
input output Expression e;
input output ArgT arg;
end MapFunc;
algorithm
outSubscript := match subscript
local
Expression exp;
case UNTYPED()
algorithm
(exp, arg) := Expression.mapFold(subscript.exp, func, arg);
then
if referenceEq(subscript.exp, exp) then subscript else UNTYPED(exp);
case INDEX()
algorithm
(exp, arg) := Expression.mapFold(subscript.index, func, arg);
then
if referenceEq(subscript.index, exp) then subscript else fromTypedExp(exp);
case SLICE()
algorithm
(exp, arg) := Expression.mapFold(subscript.slice, func, arg);
then
if referenceEq(subscript.slice, exp) then subscript else fromTypedExp(exp);
else subscript;
end match;
end mapFoldExp;
function mapFoldExpShallow<ArgT>
input Subscript subscript;
input MapFunc func;
output Subscript outSubscript;
input output ArgT arg;
partial function MapFunc
input output Expression e;
input output ArgT arg;
end MapFunc;
algorithm
outSubscript := match subscript
local
Expression exp;
case UNTYPED()
algorithm
(exp, arg) := func(subscript.exp, arg);
then
if referenceEq(subscript.exp, exp) then subscript else UNTYPED(exp);
case INDEX()
algorithm
(exp, arg) := func(subscript.index, arg);
then
if referenceEq(subscript.index, exp) then subscript else fromTypedExp(exp);
case SLICE()
algorithm
(exp, arg) := func(subscript.slice, arg);
then
if referenceEq(subscript.slice, exp) then subscript else fromTypedExp(exp);
else subscript;
end match;
end mapFoldExpShallow;
function toAbsyn
input Subscript subscript;
output Absyn.Subscript asubscript;
algorithm
asubscript := match subscript
case RAW_SUBSCRIPT() then subscript.subscript;
case UNTYPED() then Absyn.Subscript.SUBSCRIPT(Expression.toAbsyn(subscript.exp));
case INDEX() then Absyn.Subscript.SUBSCRIPT(Expression.toAbsyn(subscript.index));
case SLICE() then Absyn.Subscript.SUBSCRIPT(Expression.toAbsyn(subscript.slice));
case WHOLE() then Absyn.Subscript.NOSUB();
else
algorithm
Error.assertion(false, getInstanceName() + " failed on unknown subscript", sourceInfo());
then
fail();
end match;
end toAbsyn;
function toDAE
input Subscript subscript;
output DAE.Subscript daeSubscript;
algorithm
daeSubscript := match subscript
case INDEX() then DAE.INDEX(Expression.toDAE(subscript.index));
case SLICE() then DAE.SLICE(Expression.toDAE(subscript.slice));
case WHOLE() then DAE.WHOLEDIM();
else
algorithm
Error.assertion(false, getInstanceName() + " failed on unknown subscript " + toString(subscript), sourceInfo());
then
fail();
end match;
end toDAE;
function toDAEExp
input Subscript subscript;
output DAE.Exp daeExp;
algorithm
daeExp := match subscript
case INDEX() then Expression.toDAE(subscript.index);
case SLICE() then Expression.toDAE(subscript.slice);
else
algorithm
Error.assertion(false, getInstanceName() + " failed on unknown subscript '" +
toString(subscript) + "'", sourceInfo());
then
fail();
end match;
end toDAEExp;
function toString
input Subscript subscript;
output String string;
algorithm
string := match subscript
case RAW_SUBSCRIPT() then Dump.printSubscriptStr(subscript.subscript);
case UNTYPED() then Expression.toString(subscript.exp);
case INDEX() then Expression.toString(subscript.index);
case SLICE() then Expression.toString(subscript.slice);
case EXPANDED_SLICE()
then List.toString(subscript.indices, toString, "", "{", ", ", "}", false);
case WHOLE() then ":";
case SPLIT_PROXY()
then "<" + InstNode.name(subscript.origin) + ", " + InstNode.name(subscript.parent) + ">";
case SPLIT_INDEX()
then "<" + InstNode.name(subscript.node) + ", " + String(subscript.dimIndex) + ">";
end match;
end toString;
function toStringList
input list<Subscript> subscripts;
output String string;
algorithm
string := List.toString(subscripts, toString, "", "[", ", ", "]", false);
end toStringList;
function toFlatString
input Subscript subscript;
output String string;
algorithm
string := match subscript
case RAW_SUBSCRIPT() then Dump.printSubscriptStr(subscript.subscript);
case UNTYPED() then Expression.toFlatString(subscript.exp);
case INDEX() then Expression.toFlatString(subscript.index);
case SLICE() then Expression.toFlatString(subscript.slice);
case EXPANDED_SLICE()
then List.toString(subscript.indices, toString, "", "{", ", ", "}", false);
case WHOLE() then ":";
case SPLIT_INDEX()
then "<" + InstNode.name(subscript.node) + ", " + String(subscript.dimIndex) + ">";
end match;
end toFlatString;
function toFlatStringList
input list<Subscript> subscripts;
output String string;
algorithm
string := List.toString(subscripts, toFlatString, "", "[", ",", "]", false);
end toFlatStringList;
function toJSON
input Subscript subscript;
output JSON json;
algorithm
json := match subscript
case UNTYPED() then Expression.toJSON(subscript.exp);
case INDEX() then Expression.toJSON(subscript.index);
case SLICE() then Expression.toJSON(subscript.slice);
else JSON.makeString(toString(subscript));
end match;
end toJSON;
function toJSONList
input list<Subscript> subscripts;
output JSON json = JSON.makeNull();
algorithm
for s in subscripts loop
json := JSON.addElement(toJSON(s), json);
end for;
end toJSONList;
function eval
input Subscript subscript;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
output Subscript outSubscript;
algorithm
outSubscript := match subscript
case INDEX() then INDEX(Ceval.evalExp(subscript.index, target));
case SLICE() then SLICE(Ceval.evalExp(subscript.slice, target));
else subscript;
end match;
end eval;
function simplify
input Subscript subscript;
input Dimension dimension;
output Subscript outSubscript;
algorithm
outSubscript := match subscript
case INDEX() then INDEX(SimplifyExp.simplify(subscript.index));
case SLICE() then simplifySlice(subscript.slice, dimension);
else subscript;
end match;
end simplify;
function simplifySlice
input Expression slice;
input Dimension dimension;
output Subscript outSubscript;
protected
Expression exp;
algorithm
exp := SimplifyExp.simplify(slice);
outSubscript := match exp
// If the slice is equivalent to 1:size(dim), replace it with :
case Expression.RANGE()
guard (isNone(exp.step) or Expression.isOne(Util.getOption(exp.step))) and
Dimension.expIsLowerBound(exp.start) and
Dimension.expIsUpperBound(exp.stop, dimension)
then WHOLE();
// Otherwise return a new slice with the simplified expression.
else SLICE(exp);
end match;
end simplifySlice;
function simplifyList
input list<Subscript> subscripts;
input list<Dimension> dimensions;
input Boolean trim = false;
output list<Subscript> outSubscripts = {};
protected
Dimension d;
list<Dimension> rest_d = dimensions;
algorithm
if listEmpty(dimensions) then
// If the type of the subscript owner isn't known, for example when dealing
// with expandable connector elements, treat the dimensions as unknown.
outSubscripts := list(simplify(s, Dimension.UNKNOWN()) for s in subscripts);
else
rest_d := List.lastN(dimensions, listLength(subscripts));
for s in subscripts loop
d :: rest_d := rest_d;
outSubscripts := simplify(s, d) :: outSubscripts;
end for;
if trim then
outSubscripts := listReverseInPlace(List.trim(outSubscripts, isWhole));
else
outSubscripts := listReverseInPlace(outSubscripts);
end if;
end if;
end simplifyList;
function toDimension
"Returns a dimension representing the size of the given subscript."
input Subscript subscript;
output Dimension dimension;
algorithm
dimension := match subscript
case INDEX() then Dimension.fromInteger(1);
case SLICE() then listHead(Type.arrayDims(Expression.typeOf(subscript.slice)));
case WHOLE() then Dimension.UNKNOWN();
case SPLIT_INDEX() then Dimension.fromInteger(1);
end match;
end toDimension;
function fromDimension
"Returns a slice subscripts that covers the given dimension.
Will fail for untyped or unknown dimensions."
input Dimension dimension;
output Subscript subscript;
algorithm
subscript := match dimension
case Dimension.INTEGER()
then Subscript.SLICE(Expression.makeIntegerRange(1, 1, dimension.size));
case Dimension.BOOLEAN()
then Subscript.SLICE(Expression.makeRange(Expression.BOOLEAN(false), NONE(), Expression.BOOLEAN(true)));
case Dimension.ENUM()
then Subscript.SLICE(Expression.makeRange(
Expression.makeEnumLiteral(dimension.enumType, 1),
NONE(),
Expression.makeEnumLiteral(dimension.enumType, Type.enumSize(dimension.enumType))));
case Dimension.EXP()
then Subscript.SLICE(Expression.makeRange(Expression.INTEGER(1), NONE(), dimension.exp));
end match;
end fromDimension;
function scalarize
input Subscript subscript;
input Dimension dimension;
output list<Subscript> subscripts;
algorithm
subscripts := match subscript
case INDEX() then {subscript};
case SLICE()
then list(INDEX(e) for e in Expression.arrayElements(ExpandExp.expand(subscript.slice)));
case WHOLE()
then RangeIterator.map(RangeIterator.fromDim(dimension), makeIndex);
else {subscript};
end match;
end scalarize;
function scalarizeList
input list<Subscript> subscripts;
input list<Dimension> dimensions;
output list<list<Subscript>> outSubscripts = {};
protected
Dimension dim;
list<Dimension> rest_dims = dimensions;
list<Subscript> subs;
algorithm
for s in subscripts loop
dim :: rest_dims := rest_dims;
subs := scalarize(s, dim);
if listEmpty(subs) then
outSubscripts := {};
return;
else
outSubscripts := subs :: outSubscripts;
end if;
end for;