This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
NFType.mo
656 lines (578 loc) · 16.6 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
/*
* 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 List;
public
import Dimension = NFDimension;
import NFInstNode.InstNode;
import Subscript = NFSubscript;
import ComplexType = NFComplexType;
import ConvertDAE = NFConvertDAE;
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
Type resultType;
DAE.FunctionAttributes attributes;
end FUNCTION;
record METABOXED "Used for MetaModelica generic types"
Type ty;
end METABOXED;
record POLYMORPHIC
String name;
end POLYMORPHIC;
record ANY
end ANY;
// 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);
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));
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));
else ARRAY(ty, dims);
end match;
end liftArrayRightList;
function unliftArray
input output Type ty;
protected
Type el_ty;
list<Dimension> dims;
algorithm
ARRAY(el_ty, _ :: dims) := ty;
if listEmpty(dims) then
ty := el_ty;
else
ty := ARRAY(el_ty, dims);
end if;
end unliftArray;
function unliftArrayN
input Integer N;
input output Type ty;
protected
Type el_ty;
list<Dimension> dims;
algorithm
ARRAY(el_ty, dims) := ty;
for i in 1:N loop
dims := listRest(dims);
end for;
if listEmpty(dims) then
ty := el_ty;
else
ty := ARRAY(el_ty, dims);
end if;
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 isArray
input Type ty;
output Boolean isArray;
algorithm
isArray := match ty
case ARRAY() then true;
else false;
end match;
end isArray;
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;
else false;
end match;
end isVector;
function isMatrix
input Type ty;
output Boolean isMatrix;
algorithm
isMatrix := match ty
case ARRAY(dimensions = {_, _}) then true;
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);
else false;
end match;
end isSquareMatrix;
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 isScalarArray
input Type ty;
output Boolean isScalar;
algorithm
isScalar := match ty
case ARRAY(dimensions = {_}) then true;
else false;
end match;
end isScalarArray;
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(ty.resultType);
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;
case FUNCTION() then isBasicNumeric(ty.resultType);
else false;
end match;
end isBasicNumeric;
function isNumeric
input Type ty;
output Boolean isNumeric;
algorithm
isNumeric := match ty
case ARRAY() then isBasicNumeric(ty.elementType);
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(ty.resultType);
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 isPolymorphic
input Type ty;
output Boolean isPolymorphic;
algorithm
isPolymorphic := match ty
case POLYMORPHIC() then true;
else false;
end match;
end isPolymorphic;
function firstTupleType
input Type ty;
output Type outTy;
algorithm
outTy := match ty
case TUPLE() then listHead(ty.types);
else ty;
end match;
end firstTupleType;
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;
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 ARRAY(elementTy, arrayTy.dimensions);
else elementTy;
end match;
end setArrayElementType;
function elementType
input Type ty;
output Type elementTy;
algorithm
elementTy := match ty
case ARRAY() then ty.elementType;
case FUNCTION() then ty.resultType;
else ty;
end match;
end elementType;
function arrayDims
input Type ty;
output list<Dimension> dims;
algorithm
dims := match ty
case ARRAY() then ty.dimensions;
case FUNCTION() then arrayDims(ty.resultType);
case METABOXED() then arrayDims(ty.ty);
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
ty := match dstType
case ARRAY()
then ARRAY(dstType.elementType, arrayDims(srcType));
else ARRAY(dstType, arrayDims(srcType));
end match;
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(ty.resultType, 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 FUNCTION() then dimensionCount(ty.resultType);
case METABOXED() then dimensionCount(ty.ty);
else 0;
end match;
end dimensionCount;
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 " + Absyn.pathString(ty.typePath) +
"(" + stringDelimitList(ty.literals, ", ") + ")";
case Type.ENUMERATION_ANY() then "enumeration(:)";
case Type.ARRAY() then toString(ty.elementType) + "[" + stringDelimitList(List.map(ty.dimensions, Dimension.toString), ", ") + "]";
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")";
case Type.NORETCALL() then "()";
case Type.UNKNOWN() then "unknown()";
case Type.COMPLEX() then InstNode.name(ty.cls);
case Type.FUNCTION() then "function( output " + toString(ty.resultType) + " )";
case Type.METABOXED() then "#" + toString(ty.ty);
case Type.POLYMORPHIC() then "<" + ty.name + ">";
case Type.ANY() then "$ANY$";
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type: " + anyString(ty), sourceInfo());
then
fail();
end match;
end toString;
function typenameString
input Type ty;
output String str;
algorithm
str := match ty
case Type.ENUMERATION() then Absyn.pathString(ty.typePath);
else toString(ty);
end match;
end typenameString;
function toDAE
input Type ty;
output DAE.Type daeTy;
algorithm
daeTy := match ty
case Type.INTEGER() then DAE.T_INTEGER_DEFAULT;
case Type.REAL() then DAE.T_REAL_DEFAULT;
case Type.STRING() then DAE.T_STRING_DEFAULT;
case Type.BOOLEAN() then DAE.T_BOOL_DEFAULT;
case Type.ENUMERATION() then DAE.T_ENUMERATION(NONE(), ty.typePath, ty.literals, {}, {});
case Type.CLOCK() then DAE.T_CLOCK_DEFAULT;
case Type.ARRAY()
then DAE.T_ARRAY(toDAE(ty.elementType),
list(Dimension.toDAE(d) for d in ty.dimensions));
case Type.TUPLE()
then DAE.T_TUPLE(list(toDAE(t) for t in ty.types), ty.names);
case Type.FUNCTION()
then DAE.T_FUNCTION({} /*TODO:FIXME*/, toDAE(ty.resultType), ty.attributes, Absyn.IDENT("TODO:FIXME"));
case Type.NORETCALL() then DAE.T_NORETCALL_DEFAULT;
case Type.UNKNOWN() then DAE.T_UNKNOWN_DEFAULT;
case Type.COMPLEX() then InstNode.toDAEType(ty.cls);
case Type.POLYMORPHIC() then DAE.T_METAPOLYMORPHIC(ty.name);
case Type.ANY() then DAE.T_ANYTYPE(NONE());
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type: " + anyString(ty), sourceInfo());
then
fail();
end match;
end toDAE;
function subscript
"Reduces a type's dimensions based on the given list of subscripts."
input output Type ty;
input list<Subscript> subs;
protected
Dimension dim;
list<Dimension> dims, subbed_dims = {};
algorithm
if listEmpty(subs) then
return;
end if;
dims := arrayDims(ty);
for sub in subs loop
dim :: dims := dims;
subbed_dims := match sub
case Subscript.INDEX() then subbed_dims;
case Subscript.SLICE() then Subscript.toDimension(sub) :: subbed_dims;
case Subscript.WHOLE() then dim :: subbed_dims;
end match;
end for;
ty := arrayElementType(ty);
if not (listEmpty(subbed_dims) and listEmpty(dims)) then
ty := ARRAY(ty, listAppend(listReverse(subbed_dims), dims));
end if;
end subscript;
function isEqual
input Type ty1;
input Type ty2;
output Boolean equal;
algorithm
if referenceEq(ty1, ty2) then
equal := true;
return;
end if;
if valueConstructor(ty1) <> valueConstructor(ty2) then
equal := false;
return;
end if;
equal := match (ty1, ty2)
local
list<String> names1, names2;
case (ENUMERATION(), ENUMERATION())
then List.isEqualOnTrue(ty1.literals, ty2.literals, stringEq);
case (ARRAY(), ARRAY())
then isEqual(ty1.elementType, ty2.elementType) and
List.isEqualOnTrue(ty1.dimensions, ty2.dimensions, Dimension.isEqualKnown);
case (TUPLE(names = SOME(names1)), TUPLE(names = SOME(names2)))
then List.isEqualOnTrue(names1, names2, stringEq) and
List.isEqualOnTrue(ty1.types, ty2.types, isEqual);
case (TUPLE(names = NONE()), TUPLE(names = NONE()))
then List.isEqualOnTrue(ty1.types, ty2.types, isEqual);
case (TUPLE(), TUPLE()) then false;
else true;
end match;
end isEqual;
function isDiscrete
input Type ty;
output Boolean isDiscrete;
algorithm
isDiscrete := match ty
case INTEGER() then true;
case STRING() then true;
case BOOLEAN() then true;
case ENUMERATION() then true;
case ARRAY() then isDiscrete(ty.elementType);
case FUNCTION() then isDiscrete(ty.resultType);
else false;
end match;
end isDiscrete;
annotation(__OpenModelica_Interface="frontend");
end NFType;