-
Notifications
You must be signed in to change notification settings - Fork 297
/
NFType.mo
1393 lines (1234 loc) · 39.1 KB
/
NFType.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 NFType
protected
import Type = NFType;
import Array;
import List;
import Class = NFClass;
import IOStream;
import Util;
import NFClassTree.ClassTree;
public
import Dimension = NFDimension;
import NFInstNode.InstNode;
import Subscript = NFSubscript;
import ComplexType = NFComplexType;
import NFFunction.Function;
import Record = NFRecord;
import UnorderedMap;
type FunctionType = enumeration(
FUNCTIONAL_PARAMETER "Function parameter of function type.",
FUNCTION_REFERENCE "Function name used to reference a function.",
FUNCTIONAL_VARIABLE "A variable that contains a function reference."
);
type Branch = enumeration(
NONE,
TRUE,
FALSE
);
record INTEGER
end INTEGER;
record REAL
end REAL;
record STRING
end STRING;
record BOOLEAN
end BOOLEAN;
record CLOCK
end CLOCK;
record ENUMERATION
Absyn.Path typePath;
list<String> literals;
end ENUMERATION;
record ENUMERATION_ANY "enumeration(:)"
end ENUMERATION_ANY;
record ARRAY
Type elementType;
list<Dimension> dimensions;
end ARRAY;
record TUPLE
list<Type> types;
Option<list<String>> names;
end TUPLE;
record NORETCALL
end NORETCALL;
record UNKNOWN
end UNKNOWN;
record COMPLEX
InstNode cls;
ComplexType complexTy;
end COMPLEX;
record FUNCTION
Function fn;
FunctionType fnType;
end FUNCTION;
record METABOXED "Used for MetaModelica generic types"
Type ty;
end METABOXED;
record POLYMORPHIC
String name;
end POLYMORPHIC;
record ANY
end ANY;
record SUBSCRIPTED
String name;
Type ty;
list<Type> subs;
Type subscriptedTy;
end SUBSCRIPTED;
record CONDITIONAL_ARRAY
"A type that might be one of two types depending on a condition.
The two types are assumed to be array types with equal number of dimensions."
Type trueType;
Type falseType;
Branch matchedBranch;
end CONDITIONAL_ARRAY;
record UNTYPED
"Used by untyped components to store type information needed during typing."
InstNode typeNode;
array<Dimension> dimensions;
end UNTYPED;
// TODO: Fix constants in uniontypes and use these wherever applicable to
// speed up comparisons using referenceEq.
//constant Type INTEGER_DEFAULT = NFType.INTEGER();
//constant Type REAL_DEFAULT = NFType.REAL();
//constant Type STRING_DEFAULT = NFType.STRING();
//constant Type BOOLEAN_DEFAULT = NFType.BOOLEAN();
function liftArrayLeft
"Adds an array dimension to a type on the left side, e.g.
listArrayLeft(Real[2, 3], [4]) => Real[4, 2, 3]."
input output Type ty;
input Dimension dim;
algorithm
ty := match ty
case ARRAY() then ARRAY(ty.elementType, dim :: ty.dimensions);
case CONDITIONAL_ARRAY() then CONDITIONAL_ARRAY(liftArrayLeft(ty.trueType, dim),
liftArrayLeft(ty.falseType, dim),
ty.matchedBranch);
else ARRAY(ty, {dim});
end match;
end liftArrayLeft;
function liftArrayLeftList
"Adds array dimensions to a type on the left side, e.g.
listArrayLeft(Real[2, 3], [4, 5]) => Real[4, 5, 2, 3]."
input output Type ty;
input list<Dimension> dims;
algorithm
if listEmpty(dims) then
return;
end if;
ty := match ty
case ARRAY() then ARRAY(ty.elementType, listAppend(dims, ty.dimensions));
case CONDITIONAL_ARRAY() then CONDITIONAL_ARRAY(liftArrayLeftList(ty.trueType, dims),
liftArrayLeftList(ty.falseType, dims),
ty.matchedBranch);
else ARRAY(ty, dims);
end match;
end liftArrayLeftList;
function liftArrayRightList
"Adds array dimensions to a type on the left side, e.g.
listArrayLeft(Real[2, 3], [4, 5]) => Real[2, 3, 4, 5]."
input output Type ty;
input list<Dimension> dims;
algorithm
if listEmpty(dims) then
return;
end if;
ty := match ty
case ARRAY() then ARRAY(ty.elementType, listAppend(ty.dimensions, dims));
case CONDITIONAL_ARRAY() then CONDITIONAL_ARRAY(liftArrayRightList(ty.trueType, dims),
liftArrayRightList(ty.falseType, dims),
ty.matchedBranch);
else ARRAY(ty, dims);
end match;
end liftArrayRightList;
function unliftArray
input output Type ty;
algorithm
ty := match ty
local
list<Dimension> dims;
Type tty, fty;
case ARRAY(dimensions = _ :: dims)
then if listEmpty(dims) then ty.elementType else ARRAY(ty.elementType, dims);
case CONDITIONAL_ARRAY()
algorithm
tty := unliftArray(ty.trueType);
fty := unliftArray(ty.falseType);
then
if isEqual(tty, fty) then tty else CONDITIONAL_ARRAY(tty, fty, ty.matchedBranch);
end match;
end unliftArray;
function unliftArrayN
input Integer N;
input output Type ty;
algorithm
if N == 0 then
return;
end if;
ty := match ty
local
list<Dimension> dims;
Type tty, fty;
case ARRAY(dimensions = dims)
algorithm
for i in 1:N loop
dims := listRest(dims);
end for;
then
if listEmpty(dims) then ty.elementType else ARRAY(ty.elementType, dims);
case CONDITIONAL_ARRAY()
algorithm
tty := unliftArrayN(N, ty.trueType);
fty := unliftArrayN(N, ty.falseType);
then
if isEqual(tty, fty) then tty else CONDITIONAL_ARRAY(tty, fty, ty.matchedBranch);
end match;
end unliftArrayN;
function isInteger
input Type ty;
output Boolean isInteger;
algorithm
isInteger := match ty
case INTEGER() then true;
else false;
end match;
end isInteger;
function isReal
input Type ty;
output Boolean isReal;
algorithm
isReal := match ty
case REAL() then true;
else false;
end match;
end isReal;
function isBoolean
input Type ty;
output Boolean isBool;
algorithm
isBool := match ty
case BOOLEAN() then true;
else false;
end match;
end isBoolean;
function isString
input Type ty;
output Boolean isString;
algorithm
isString := match ty
case STRING() then true;
else false;
end match;
end isString;
function isClock
input Type ty;
output Boolean isClock;
algorithm
isClock := match ty
case CLOCK() then true;
else false;
end match;
end isClock;
function isScalar
input Type ty;
output Boolean isScalar;
algorithm
isScalar := match ty
case ARRAY() then false;
case CONDITIONAL_ARRAY() then false;
else true;
end match;
end isScalar;
function isArray
input Type ty;
output Boolean isArray;
algorithm
isArray := match ty
case ARRAY() then true;
case CONDITIONAL_ARRAY() then true;
else false;
end match;
end isArray;
function isConditionalArray
input Type ty;
output Boolean isConditionalArray;
algorithm
isConditionalArray := match ty
case CONDITIONAL_ARRAY() then true;
else false;
end match;
end isConditionalArray;
function setConditionalArrayTypes
input Type condType;
input Type trueType;
input Type falseType;
output Type outType;
protected
Branch matched_branch;
algorithm
CONDITIONAL_ARRAY(matchedBranch = matched_branch) := condType;
outType := CONDITIONAL_ARRAY(trueType, falseType, matched_branch);
end setConditionalArrayTypes;
function isMatchedBranch
input Boolean condition;
input Type condType;
output Boolean isMatched = true;
protected
Branch matched_branch;
algorithm
CONDITIONAL_ARRAY(matchedBranch = matched_branch) := condType;
if condition and matched_branch == Branch.FALSE or
not condition and matched_branch == Branch.TRUE then
isMatched := false;
end if;
end isMatchedBranch;
function simplifyConditionalArray
input Type ty;
output Type outType;
algorithm
outType := match ty
case CONDITIONAL_ARRAY()
then match ty.matchedBranch
case Branch.TRUE then ty.trueType;
case Branch.FALSE then ty.falseType;
else ty;
end match;
else ty;
end match;
end simplifyConditionalArray;
function isVector
"Return whether the type is a vector type or not, i.e. a 1-dimensional array."
input Type ty;
output Boolean isVector;
algorithm
isVector := match ty
case ARRAY(dimensions = {_}) then true;
case CONDITIONAL_ARRAY() then isVector(ty.trueType);
else false;
end match;
end isVector;
function isMatrix
input Type ty;
output Boolean isMatrix;
algorithm
isMatrix := match ty
case ARRAY(dimensions = {_, _}) then true;
case CONDITIONAL_ARRAY() then isMatrix(ty.trueType);
else false;
end match;
end isMatrix;
function isSquareMatrix
input Type ty;
output Boolean isSquareMatrix;
algorithm
isSquareMatrix := match ty
local
Dimension d1, d2;
case ARRAY(dimensions = {d1, d2}) then Dimension.isEqualKnown(d1, d2);
case CONDITIONAL_ARRAY() then isSquareMatrix(ty.trueType);
else false;
end match;
end isSquareMatrix;
function isEmptyArray
input Type ty;
output Boolean isEmpty;
algorithm
isEmpty := match ty
case ARRAY() then List.exist(ty.dimensions, Dimension.isZero);
case CONDITIONAL_ARRAY() then isEmptyArray(ty.trueType);
else false;
end match;
end isEmptyArray;
function isSingleElementArray
input Type ty;
output Boolean isSingleElement;
algorithm
isSingleElement := match ty
local
Dimension d;
case ARRAY(dimensions = {d})
then Dimension.isKnown(d) and Dimension.size(d) == 1;
else false;
end match;
end isSingleElementArray;
function isEnumeration
input Type ty;
output Boolean isEnum;
algorithm
isEnum := match ty
case ENUMERATION() then true;
case ENUMERATION_ANY() then true;
else false;
end match;
end isEnumeration;
function isComplex
input Type ty;
output Boolean isComplex;
algorithm
isComplex := match ty
case COMPLEX() then true;
else false;
end match;
end isComplex;
function isComplexArray
input Type ty;
output Boolean isComplex;
algorithm
isComplex := match ty
case ARRAY() guard(isComplex(ty.elementType)) then true;
case ARRAY() then isComplexArray(ty.elementType);
else false;
end match;
end isComplexArray;
function complexNode
input Type ty;
output InstNode node;
algorithm
COMPLEX(cls = node) := ty;
end complexNode;
function complexComponents
input Type ty;
output array<InstNode> comps;
algorithm
comps := ClassTree.getComponents(Class.classTree(InstNode.getClass(complexNode(ty))));
end complexComponents;
function isConnector
input Type ty;
output Boolean isConnector;
algorithm
isConnector := match ty
case COMPLEX(complexTy = ComplexType.CONNECTOR()) then true;
else false;
end match;
end isConnector;
function isExpandableConnector
input Type ty;
output Boolean isExpandable;
algorithm
isExpandable := match ty
case COMPLEX(complexTy = ComplexType.EXPANDABLE_CONNECTOR()) then true;
else false;
end match;
end isExpandableConnector;
function isExternalObject
input Type ty;
output Boolean isEO;
algorithm
isEO := match ty
case COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT()) then true;
else false;
end match;
end isExternalObject;
function isRecord
input Type ty;
output Boolean isRecord;
algorithm
isRecord := match ty
case COMPLEX(complexTy = ComplexType.RECORD()) then true;
else false;
end match;
end isRecord;
function isBasic
input Type ty;
output Boolean isNumeric;
algorithm
isNumeric := match ty
case REAL() then true;
case INTEGER() then true;
case BOOLEAN() then true;
case STRING() then true;
case ENUMERATION() then true;
case CLOCK() then true;
case FUNCTION() then isBasic(Function.returnType(ty.fn));
else false;
end match;
end isBasic;
function isBasicNumeric
input Type ty;
output Boolean isNumeric;
algorithm
isNumeric := match ty
case REAL() then true;
case INTEGER() then true;
else false;
end match;
end isBasicNumeric;
function isNumeric
input Type ty;
output Boolean isNumeric;
algorithm
isNumeric := match ty
case ARRAY() then isBasicNumeric(ty.elementType);
case CONDITIONAL_ARRAY() then isNumeric(ty.trueType);
else isBasicNumeric(ty);
end match;
end isNumeric;
function isScalarBuiltin
"Returns true for all the builtin scalar types such as Integer, Real, etc."
input Type ty;
output Boolean isScalarBuiltin;
algorithm
isScalarBuiltin := match ty
case INTEGER() then true;
case REAL() then true;
case STRING() then true;
case BOOLEAN() then true;
case CLOCK() then true;
case ENUMERATION() then true;
case ENUMERATION_ANY() then true;
case FUNCTION() then isScalarBuiltin(Function.returnType(ty.fn));
else false;
end match;
end isScalarBuiltin;
function isTuple
input Type ty;
output Boolean isTuple;
algorithm
isTuple := match ty
case TUPLE() then true;
else false;
end match;
end isTuple;
function isUnknown
input Type ty;
output Boolean isUnknown;
algorithm
isUnknown := match ty
case UNKNOWN() then true;
else false;
end match;
end isUnknown;
function isKnown
input Type ty;
output Boolean isKnown;
algorithm
isKnown := match ty
case UNKNOWN() then false;
case UNTYPED() then false;
else true;
end match;
end isKnown;
function isPolymorphic
input Type ty;
output Boolean isPolymorphic;
algorithm
isPolymorphic := match ty
case POLYMORPHIC() then true;
else false;
end match;
end isPolymorphic;
function isPolymorphicNamed
input Type ty;
input String name;
output Boolean res;
algorithm
res := match ty
case POLYMORPHIC() then name == ty.name;
else false;
end match;
end isPolymorphicNamed;
function firstTupleType
input Type ty;
output Type outTy;
algorithm
outTy := match ty
case TUPLE() then listHead(ty.types);
case ARRAY() then Type.ARRAY(firstTupleType(ty.elementType), ty.dimensions);
else ty;
end match;
end firstTupleType;
function nthTupleType
input Type ty;
input Integer n;
output Type outTy;
algorithm
outTy := match ty
case TUPLE() then listGet(ty.types, n);
case ARRAY() then Type.ARRAY(nthTupleType(ty.elementType, n), ty.dimensions);
else ty;
end match;
end nthTupleType;
function arrayElementType
"Returns the common type of the elements in an array, or just the type
itself if it's not an array type."
input Type ty;
output Type elementTy;
algorithm
elementTy := match ty
case ARRAY() then ty.elementType;
case CONDITIONAL_ARRAY() then arrayElementType(ty.trueType);
else ty;
end match;
end arrayElementType;
function setArrayElementType
"Sets the common type of the elements in an array, if the type is an array
type. Otherwise it just returns the given element type."
input Type arrayTy;
input Type elementTy;
output Type ty;
algorithm
ty := match arrayTy
case ARRAY() then liftArrayLeftList(elementTy, arrayTy.dimensions);
case CONDITIONAL_ARRAY()
then CONDITIONAL_ARRAY(setArrayElementType(arrayTy.trueType, elementTy),
setArrayElementType(arrayTy.falseType, elementTy),
arrayTy.matchedBranch);
else elementTy;
end match;
end setArrayElementType;
function elementType
input Type ty;
output Type elementTy;
algorithm
elementTy := match ty
case ARRAY() then ty.elementType;
case CONDITIONAL_ARRAY() then elementType(ty.trueType);
case FUNCTION() then elementType(Function.returnType(ty.fn));
else ty;
end match;
end elementType;
function copyElementType
"Sets the element type of the destination type to the element type of the
source type."
input Type dstType;
input Type srcType;
output Type ty;
algorithm
ty := setArrayElementType(dstType, arrayElementType(srcType));
end copyElementType;
function arrayDims
input Type ty;
output list<Dimension> dims;
algorithm
dims := match ty
case ARRAY() then ty.dimensions;
case FUNCTION() then arrayDims(Function.returnType(ty.fn));
case METABOXED() then arrayDims(ty.ty);
case CONDITIONAL_ARRAY() then List.fill(Dimension.UNKNOWN(), dimensionCount(ty.trueType));
case UNTYPED() then arrayList(ty.dimensions);
else {};
end match;
end arrayDims;
function copyDims
"Copies array dimensions from one type to another, discarding the existing
dimensions of the destination type but keeping its element type."
input Type srcType;
input Type dstType;
output Type ty;
algorithm
if listEmpty(arrayDims(srcType)) then
ty := arrayElementType(dstType);
else
ty := match dstType
case ARRAY()
then ARRAY(dstType.elementType, arrayDims(srcType));
else ARRAY(dstType, arrayDims(srcType));
end match;
end if;
end copyDims;
function nthDimension
input Type ty;
input Integer index;
output Dimension dim;
algorithm
dim := match ty
case ARRAY() then listGet(ty.dimensions, index);
case FUNCTION() then nthDimension(Function.returnType(ty.fn), index);
case METABOXED() then nthDimension(ty.ty, index);
end match;
end nthDimension;
function dimensionCount
input Type ty;
output Integer dimCount;
algorithm
dimCount := match ty
case ARRAY() then listLength(ty.dimensions);
case CONDITIONAL_ARRAY() then dimensionCount(ty.trueType);
case FUNCTION() then dimensionCount(Function.returnType(ty.fn));
case METABOXED() then dimensionCount(ty.ty);
case UNTYPED() then arrayLength(ty.dimensions);
else 0;
end match;
end dimensionCount;
function dimensionDiff
input Type ty1;
input Type ty2;
output Integer diff = dimensionCount(ty1) - dimensionCount(ty2);
end dimensionDiff;
function hasKnownSize
input Type ty;
output Boolean isKnown;
algorithm
isKnown := match ty
case ARRAY() then List.all(ty.dimensions, function Dimension.isKnown(allowExp = false));
case CONDITIONAL_ARRAY() then false;
case FUNCTION() then hasKnownSize(Function.returnType(ty.fn));
else true;
end match;
end hasKnownSize;
function hasZeroDimension
input Type ty;
output Boolean hasZero;
algorithm
hasZero := match ty
case ARRAY() then List.exist(ty.dimensions, Dimension.isZero);
case CONDITIONAL_ARRAY() then hasZeroDimension(ty.trueType) and hasZeroDimension(ty.falseType);
else false;
end match;
end hasZeroDimension;
function mapDims
input output Type ty;
input FuncT func;
partial function FuncT
input output Dimension dim;
end FuncT;
algorithm
() := match ty
local
Function fn;
case ARRAY()
algorithm
ty.dimensions := list(func(d) for d in ty.dimensions);
then
();
case TUPLE()
algorithm
ty.types := list(mapDims(t, func) for t in ty.types);
then
();
case FUNCTION(fn = fn)
algorithm
ty.fn := Function.setReturnType(mapDims(Function.returnType(fn), func), fn);
then
();
case METABOXED()
algorithm
ty.ty := mapDims(ty.ty, func);
then
();
case CONDITIONAL_ARRAY()
algorithm
ty.trueType := mapDims(ty.trueType, func);
ty.falseType := mapDims(ty.falseType, func);
then
();
else ();
end match;
end mapDims;
function foldDims<ArgT>
input Type ty;
input FuncT func;
input output ArgT arg;
partial function FuncT
input Dimension dim;
input output ArgT arg;
end FuncT;
algorithm
arg := match ty
case ARRAY() then List.fold(ty.dimensions, func, arg);
case TUPLE() then List.fold(ty.types, function foldDims(func = func), arg);
case FUNCTION() then foldDims(Function.returnType(ty.fn), func, arg);
case METABOXED() then foldDims(ty.ty, func, arg);
else arg;
end match;
end foldDims;
function nthEnumLiteral
input Type ty;
input Integer index;
output String literal;
protected
list<String> literals;
algorithm
ENUMERATION(literals = literals) := ty;
literal := listGet(literals, index);
end nthEnumLiteral;
function toString
input Type ty;
output String str;
algorithm
str := match ty
case Type.INTEGER() then "Integer";
case Type.REAL() then "Real";
case Type.STRING() then "String";
case Type.BOOLEAN() then "Boolean";
case Type.CLOCK() then "Clock";
case Type.ENUMERATION() then "enumeration " + AbsynUtil.pathString(ty.typePath) +
"(" + stringDelimitList(ty.literals, ", ") + ")";
case Type.ENUMERATION_ANY() then "enumeration(:)";
case Type.ARRAY() then List.toString(ty.dimensions, Dimension.toString, toString(ty.elementType), "[", ", ", "]", false);
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")";
case Type.NORETCALL() then "()";
case Type.UNKNOWN() then "unknown()";
case Type.COMPLEX() then AbsynUtil.pathString(InstNode.scopePath(ty.cls));
case Type.FUNCTION() then Function.typeString(ty.fn);
case Type.METABOXED() then "#" + toString(ty.ty);
case Type.POLYMORPHIC()
then if Util.stringStartsWith("__", ty.name) then
substring(ty.name, 3, stringLength(ty.name)) else "<" + ty.name + ">";
case Type.ANY() then "$ANY$";
case Type.CONDITIONAL_ARRAY() then toString(ty.trueType) + "|" + toString(ty.falseType);
case Type.UNTYPED() then List.toString(arrayList(ty.dimensions), Dimension.toString, InstNode.name(ty.typeNode), "[", ", ", "]", false);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type: " + anyString(ty), sourceInfo());
then
fail();
end match;
end toString;
function toFlatString
input Type ty;
output String str;
algorithm
str := match ty
case Type.INTEGER() then "Integer";
case Type.REAL() then "Real";
case Type.STRING() then "String";
case Type.BOOLEAN() then "Boolean";
case Type.CLOCK() then "Clock";
case Type.ENUMERATION() then Util.makeQuotedIdentifier(AbsynUtil.pathString(ty.typePath));
case Type.ENUMERATION_ANY() then "enumeration(:)";
case Type.ARRAY() then List.toString(ty.dimensions, Dimension.toFlatString, toFlatString(ty.elementType), "[", ", ", "]", false);
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toFlatString), ", ") + ")";
case Type.NORETCALL() then "()";
case Type.UNKNOWN() then "unknown()";
case Type.COMPLEX() then Util.makeQuotedIdentifier(AbsynUtil.pathString(InstNode.scopePath(ty.cls)));
case Type.FUNCTION() then Function.typeString(ty.fn);
case Type.METABOXED() then "#" + toFlatString(ty.ty);
case Type.POLYMORPHIC() then "<" + ty.name + ">";
case Type.ANY() then "$ANY$";
case Type.CONDITIONAL_ARRAY() then toFlatString(ty.trueType) + "|" + toFlatString(ty.falseType);
case Type.UNTYPED() then List.toString(arrayList(ty.dimensions), Dimension.toFlatString, InstNode.name(ty.typeNode), "[", ", ", "]", false);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type: " + anyString(ty), sourceInfo());
then
fail();
end match;
end toFlatString;
function dimensionsToFlatString
input Type ty;
output String str;
algorithm
str := match ty
case Type.ARRAY() then stringDelimitList(List.map(ty.dimensions, Dimension.toFlatString), ", ");
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown or not array type: " + anyString(ty), sourceInfo());
then
fail();
end match;
end dimensionsToFlatString;
function toFlatDeclarationStream
input Type ty;
input output IOStream.IOStream s;
algorithm
s := match ty
local
Integer index;
String name;
ComplexType complexTy;
Absyn.Path path;
InstNode constructor, destructor;
Function f;
case ENUMERATION()
algorithm
s := IOStream.append(s, "type ");
s := IOStream.append(s, Util.makeQuotedIdentifier(AbsynUtil.pathString(ty.typePath)));
s := IOStream.append(s, " = enumeration(");
if not listEmpty(ty.literals) then
s := IOStream.append(s, listHead(ty.literals));
for l in listRest(ty.literals) loop
s := IOStream.append(s, ", ");
s := IOStream.append(s, l);
end for;
end if;
s := IOStream.append(s, ")");
then
s;
case COMPLEX(complexTy = ComplexType.RECORD())
then InstNode.toFlatStream(ty.cls, s);
case COMPLEX(complexTy = complexTy as ComplexType.EXTERNAL_OBJECT())
algorithm
path := InstNode.scopePath(ty.cls);
name := Util.makeQuotedIdentifier(AbsynUtil.pathString(path));
s := IOStream.append(s, "class ");
s := IOStream.append(s, name);
s := IOStream.append(s, "\n extends ExternalObject;\n\n");
{f} := Function.typeNodeCache(complexTy.constructor);