-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFExpression.mo
4709 lines (4062 loc) · 137 KB
/
NFExpression.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 NFExpression
protected
import Util;
import Absyn;
import AbsynUtil;
import List;
import System;
import Flags;
import Builtin = NFBuiltin;
import BuiltinCall = NFBuiltinCall;
import Expression = NFExpression;
import Function = NFFunction;
import NFPrefixes.Variability;
import Prefixes = NFPrefixes;
import Ceval = NFCeval;
import ComplexType = NFComplexType;
import ExpandExp = NFExpandExp;
import TypeCheck = NFTypeCheck;
import ValuesUtil;
import MetaModelica.Dangerous.listReverseInPlace;
import RangeIterator = NFRangeIterator;
public
import Absyn.Path;
import DAE;
import NFInstNode.InstNode;
import Operator = NFOperator;
import Subscript = NFSubscript;
import Dimension = NFDimension;
import Type = NFType;
import ComponentRef = NFComponentRef;
import Call = NFCall;
import Binding = NFBinding;
import NFClassTree.ClassTree;
import Class = NFClass;
import NFComponentRef.Origin;
import NFTyping.ExpOrigin;
import Values;
import Record = NFRecord;
import ClockKind = NFClockKind;
record INTEGER
Integer value;
end INTEGER;
record REAL
Real value;
end REAL;
record STRING
String value;
end STRING;
record BOOLEAN
Boolean value;
end BOOLEAN;
record ENUM_LITERAL
Type ty;
String name;
Integer index;
end ENUM_LITERAL;
record CLKCONST "Clock constructors"
ClockKind clk "Clock kinds";
end CLKCONST;
record CREF
Type ty;
ComponentRef cref;
end CREF;
record TYPENAME "Represents a type used as a range, e.g. Boolean."
Type ty;
end TYPENAME;
record ARRAY
Type ty;
list<Expression> elements;
Boolean literal "True if the array is known to only contain literal expressions.";
end ARRAY;
record MATRIX "The array concatentation operator [a,b; c,d]; this should be removed during type-checking"
// Does not have a type since we only keep this operator before type-checking
list<list<Expression>> elements;
end MATRIX;
record RANGE
Type ty;
Expression start;
Option<Expression> step;
Expression stop;
end RANGE;
record TUPLE
Type ty;
list<Expression> elements;
end TUPLE;
record RECORD
Path path; // Maybe not needed since the type contains the name. Prefix?
Type ty;
list<Expression> elements;
end RECORD;
record CALL
Call call;
end CALL;
record SIZE
Expression exp;
Option<Expression> dimIndex;
end SIZE;
record END
end END;
record BINARY "Binary operations, e.g. a+4"
Expression exp1;
Operator operator;
Expression exp2;
end BINARY;
record UNARY "Unary operations, -(4x)"
Operator operator;
Expression exp;
end UNARY;
record LBINARY "Logical binary operations: and, or"
Expression exp1;
Operator operator;
Expression exp2;
end LBINARY;
record LUNARY "Logical unary operations: not"
Operator operator;
Expression exp;
end LUNARY;
record RELATION "Relation, e.g. a <= 0"
Expression exp1;
Operator operator;
Expression exp2;
end RELATION;
record IF
Type ty;
Expression condition;
Expression trueBranch;
Expression falseBranch;
end IF;
record CAST
Type ty;
Expression exp;
end CAST;
record BOX "MetaModelica boxed value"
Expression exp;
end BOX;
record UNBOX "MetaModelica value unboxing (similar to a cast)"
Expression exp;
Type ty;
end UNBOX;
record SUBSCRIPTED_EXP
Expression exp;
list<Subscript> subscripts;
Type ty;
end SUBSCRIPTED_EXP;
record TUPLE_ELEMENT
Expression tupleExp;
Integer index;
Type ty;
end TUPLE_ELEMENT;
record RECORD_ELEMENT
Expression recordExp;
Integer index;
String fieldName;
Type ty;
end RECORD_ELEMENT;
record MUTABLE
Mutable<Expression> exp;
end MUTABLE;
record EMPTY
Type ty;
end EMPTY;
record PARTIAL_FUNCTION_APPLICATION
ComponentRef fn;
list<Expression> args;
list<String> argNames;
Type ty;
end PARTIAL_FUNCTION_APPLICATION;
record BINDING_EXP
"Represents a binding expression, for example:
model A
Real x[2];
end A;
model B
A a[3](x = {{1, 2}, {3, 4}, {5, 6}});
end B;
is represented as:
BINDING_EXP({{1, 2}, {3, 4}, {5, 6}}, Real[3, 2], Real[2], {x, a}, false);
"
Expression exp;
Type expType "The actual type of exp.";
Type bindingType "The type of the propagated binding.";
list<InstNode> parents;
Boolean isEach;
end BINDING_EXP;
function isArray
input Expression exp;
output Boolean isArray;
algorithm
isArray := match exp
case ARRAY() then true;
else false;
end match;
end isArray;
function isEmptyArray
input Expression exp;
output Boolean emptyArray;
algorithm
emptyArray := match exp
case ARRAY(elements = {}) then true;
else false;
end match;
end isEmptyArray;
function isCref
input Expression exp;
output Boolean isCref;
algorithm
isCref := match exp
case CREF() then true;
else false;
end match;
end isCref;
function isWildCref
input Expression exp;
output Boolean wild;
algorithm
wild := match exp
case CREF(cref = ComponentRef.WILD()) then true;
else false;
end match;
end isWildCref;
function isCall
input Expression exp;
output Boolean isCall;
algorithm
isCall := match exp
case CALL() then true;
else false;
end match;
end isCall;
function isImpureCall
input Expression exp;
output Boolean isImpure;
algorithm
isImpure := match exp
case CALL() then Call.isImpure(exp.call);
else false;
end match;
end isImpureCall;
function isTrue
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(true) then true;
else false;
end match;
end isTrue;
function isAllTrue
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(true) then true;
case ARRAY()
algorithm
for e in exp.elements loop
if not isAllTrue(e) then
isTrue := false;
return;
end if;
end for;
then
true;
else false;
end match;
end isAllTrue;
function isFalse
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(false) then true;
else false;
end match;
end isFalse;
function isEqual
"Returns true if the two expressions are equal, otherwise false."
input Expression exp1;
input Expression exp2;
output Boolean isEqual;
algorithm
isEqual := 0 == compare(exp1, exp2);
end isEqual;
function compare
"Checks whether two expressions are equal, and returns 0 if they are.
If the first expression is 'less' than the second it returns an integer
less than 0, otherwise an integer greater than 0."
input Expression exp1;
input Expression exp2;
output Integer comp;
algorithm
// Check if the expressions are the same object.
if referenceEq(exp1, exp2) then
comp := 0;
return;
end if;
// Return false if the expressions are of different kinds.
comp := Util.intCompare(valueConstructor(exp1), valueConstructor(exp2));
if comp <> 0 then
return;
end if;
comp := match (exp1)
local
Integer i;
Real r;
String s;
Boolean b;
ComponentRef cr;
Type ty;
list<Expression> expl;
Expression e1, e2, e3;
Option<Expression> oe;
Path p;
Operator op;
Call c;
list<Subscript> subs;
ClockKind clk1, clk2;
Mutable<Expression> me;
list<list<Expression>> mat;
case INTEGER()
algorithm
INTEGER(value = i) := exp2;
then
Util.intCompare(exp1.value, i);
case REAL()
algorithm
REAL(value = r) := exp2;
then
Util.realCompare(exp1.value, r);
case STRING()
algorithm
STRING(value = s) := exp2;
then
Util.stringCompare(exp1.value, s);
case BOOLEAN()
algorithm
BOOLEAN(value = b) := exp2;
then
Util.boolCompare(exp1.value, b);
case ENUM_LITERAL()
algorithm
ENUM_LITERAL(ty = ty, index = i) := exp2;
comp := AbsynUtil.pathCompare(Type.enumName(exp1.ty), Type.enumName(ty));
if comp == 0 then
comp := Util.intCompare(exp1.index, i);
end if;
then
comp;
case CLKCONST(clk1)
algorithm
CLKCONST(clk2) := exp2;
then
ClockKind.compare(clk1, clk2);
case CREF()
algorithm
CREF(cref = cr) := exp2;
then
ComponentRef.compare(exp1.cref, cr);
case TYPENAME()
algorithm
TYPENAME(ty = ty) := exp2;
then
valueCompare(exp1.ty, ty);
case ARRAY()
algorithm
ARRAY(ty = ty, elements = expl) := exp2;
comp := valueCompare(ty, exp1.ty);
then
if comp == 0 then List.compare(exp1.elements, expl, compare) else comp;
case MATRIX()
algorithm
MATRIX(elements = mat) := exp2;
then
List.compare(exp1.elements, mat, function List.compare(compareFn = compare));
case RANGE()
algorithm
RANGE(start = e1, step = oe, stop = e2) := exp2;
comp := compare(exp1.start, e1);
if comp == 0 then
comp := compare(exp1.stop, e2);
if comp == 0 then
comp := compareOpt(exp1.step, oe);
end if;
end if;
then
comp;
case TUPLE()
algorithm
TUPLE(elements = expl) := exp2;
then
List.compare(exp1.elements, expl, compare);
case RECORD()
algorithm
RECORD(path = p, elements = expl) := exp2;
comp := AbsynUtil.pathCompare(exp1.path, p);
then
if comp == 0 then List.compare(exp1.elements, expl, compare) else comp;
case CALL()
algorithm
CALL(call = c) := exp2;
then
Call.compare(exp1.call, c);
case SIZE()
algorithm
SIZE(exp = e1, dimIndex = oe) := exp2;
comp := compareOpt(exp1.dimIndex, oe);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case END() then 0;
case BINARY()
algorithm
BINARY(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case UNARY()
algorithm
UNARY(operator = op, exp = e1) := exp2;
comp := Operator.compare(exp1.operator, op);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case LBINARY()
algorithm
LBINARY(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case LUNARY()
algorithm
LUNARY(operator = op, exp = e1) := exp2;
comp := Operator.compare(exp1.operator, op);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case RELATION()
algorithm
RELATION(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case IF()
algorithm
IF(condition = e1, trueBranch = e2, falseBranch = e3) := exp2;
comp := compare(exp1.condition, e1);
if comp == 0 then
comp := compare(exp1.trueBranch, e2);
if comp == 0 then
comp := compare(exp1.falseBranch, e3);
end if;
end if;
then
comp;
case CAST()
algorithm
e1 := match exp2
case CAST(exp = e1) then e1;
case e1 then e1;
end match;
then
compare(exp1.exp, e1);
case BOX()
algorithm
BOX(exp = e2) := exp2;
then
compare(exp1.exp, e2);
case UNBOX()
algorithm
UNBOX(exp = e1) := exp2;
then
compare(exp1.exp, e1);
case SUBSCRIPTED_EXP()
algorithm
SUBSCRIPTED_EXP(exp = e1, subscripts = subs) := exp2;
comp := compare(exp1.exp, e1);
if comp == 0 then
comp := Subscript.compareList(exp1.subscripts, subs);
end if;
then
comp;
case TUPLE_ELEMENT()
algorithm
TUPLE_ELEMENT(tupleExp = e1, index = i) := exp2;
comp := Util.intCompare(exp1.index, i);
if comp == 0 then
comp := compare(exp1.tupleExp, e1);
end if;
then
comp;
case RECORD_ELEMENT()
algorithm
RECORD_ELEMENT(recordExp = e1, index = i) := exp2;
comp := Util.intCompare(exp1.index, i);
if comp == 0 then
comp := compare(exp1.recordExp, e1);
end if;
then
comp;
case MUTABLE()
algorithm
MUTABLE(exp = me) := exp2;
then
compare(Mutable.access(exp1.exp), Mutable.access(me));
case EMPTY()
algorithm
EMPTY(ty = ty) := exp2;
then
valueCompare(exp1.ty, ty);
case PARTIAL_FUNCTION_APPLICATION()
algorithm
PARTIAL_FUNCTION_APPLICATION(fn = cr, args = expl) := exp2;
comp := ComponentRef.compare(exp1.fn, cr);
if comp == 0 then
comp := List.compare(exp1.args, expl, compare);
end if;
then
comp;
case BINDING_EXP()
algorithm
BINDING_EXP(exp = e2) := exp2;
then
compare(exp1.exp, e2);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression.", sourceInfo());
then
fail();
end match;
end compare;
function compareOpt
input Option<Expression> expl1;
input Option<Expression> expl2;
output Integer comp;
protected
Expression e1, e2;
algorithm
comp := match(expl1, expl2)
case (NONE(), NONE()) then 0;
case (NONE(), _) then -1;
case (_, NONE()) then 1;
case (SOME(e1), SOME(e2)) then compare(e1, e2);
end match;
end compareOpt;
function compareList
input list<Expression> expl1;
input list<Expression> expl2;
output Integer comp = List.compare(expl1, expl2, compare);
end compareList;
function typeOf
input Expression exp;
output Type ty;
algorithm
ty := match exp
case INTEGER() then Type.INTEGER();
case REAL() then Type.REAL();
case STRING() then Type.STRING();
case BOOLEAN() then Type.BOOLEAN();
case ENUM_LITERAL() then exp.ty;
case CLKCONST() then Type.CLOCK();
case CREF() then exp.ty;
case TYPENAME() then exp.ty;
case ARRAY() then exp.ty;
case RANGE() then exp.ty;
case TUPLE() then exp.ty;
case RECORD() then exp.ty;
case CALL() then Call.typeOf(exp.call);
case SIZE() then if isSome(exp.dimIndex) then
Type.INTEGER() else Type.sizeType(typeOf(exp.exp));
case END() then Type.INTEGER();
case BINARY() then Operator.typeOf(exp.operator);
case UNARY() then Operator.typeOf(exp.operator);
case LBINARY() then Operator.typeOf(exp.operator);
case LUNARY() then Operator.typeOf(exp.operator);
case RELATION() then Operator.typeOf(exp.operator);
case IF() then exp.ty;
case CAST() then exp.ty;
case BOX() then Type.METABOXED(typeOf(exp.exp));
case UNBOX() then exp.ty;
case SUBSCRIPTED_EXP() then exp.ty;
case TUPLE_ELEMENT() then exp.ty;
case RECORD_ELEMENT() then exp.ty;
case MUTABLE() then typeOf(Mutable.access(exp.exp));
case EMPTY() then exp.ty;
case PARTIAL_FUNCTION_APPLICATION() then exp.ty;
case BINDING_EXP() then exp.bindingType;
else Type.UNKNOWN();
end match;
end typeOf;
function setType
input Type ty;
input output Expression exp;
algorithm
exp := match exp
case ENUM_LITERAL() algorithm exp.ty := ty; then exp;
case CREF() algorithm exp.ty := ty; then exp;
case TYPENAME() algorithm exp.ty := ty; then exp;
case ARRAY() algorithm exp.ty := ty; then exp;
case RANGE() algorithm exp.ty := ty; then exp;
case TUPLE() algorithm exp.ty := ty; then exp;
case RECORD() algorithm exp.ty := ty; then exp;
case CALL() algorithm exp.call := Call.setType(exp.call, ty); then exp;
case BINARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then exp;
case UNARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then exp;
case LBINARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then exp;
case LUNARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then exp;
case RELATION() algorithm exp.operator := Operator.setType(ty, exp.operator); then exp;
case IF() algorithm exp.ty := ty; then exp;
case CAST() algorithm exp.ty := ty; then exp;
case UNBOX() algorithm exp.ty := ty; then exp;
case SUBSCRIPTED_EXP() algorithm exp.ty := ty; then exp;
case TUPLE_ELEMENT() algorithm exp.ty := ty; then exp;
case RECORD_ELEMENT() algorithm exp.ty := ty; then exp;
case PARTIAL_FUNCTION_APPLICATION() algorithm exp.ty := ty; then exp;
case BINDING_EXP() then setBindingExpType(ty, exp);
else exp;
end match;
end setType;
function setBindingExpType
"Sets the type of a binding expression while taking into account any extra
dimensions the expression contained in the binding expression might have
due to modifier propagation."
input Type ty;
input output Expression bindingExp;
protected
Expression exp;
Type exp_ty, bind_ty;
list<InstNode> parents;
Boolean is_each;
Integer dim_diff;
algorithm
BINDING_EXP(exp, exp_ty, bind_ty, parents, is_each) := bindingExp;
dim_diff := Type.dimensionDiff(exp_ty, bind_ty);
bind_ty := ty;
if dim_diff > 0 then
// If the expression type has more dimensions than the binding type, add
// those dimensions to the new binding type.
exp_ty := Type.liftArrayLeftList(ty, List.firstN(Type.arrayDims(exp_ty), dim_diff));
else
// Otherwise the expression type and the binding type is the same.
exp_ty := ty;
end if;
// Also set the type of the contained expression.
exp := setType(exp_ty, exp);
bindingExp := BINDING_EXP(exp, exp_ty, bind_ty, parents, is_each);
end setBindingExpType;
function typeCastOpt
input Option<Expression> exp;
input Type ty;
output Option<Expression> outExp = Util.applyOption(exp, function typeCast(ty = ty));
end typeCastOpt;
function typeCast
"Converts an expression to the given type. Dimensions of array types can be
omitted, and are ignored by this function, since arrays can't be cast to a
different size. Only the element type of the type is used, so for example:
typeCast({1, 2, 3}, Type.REAL()) => {1.0, 2.0, 3.0}
The function does not check that the cast is valid, and expressions that
can't be converted outright will be wrapped as a CAST expression."
input output Expression exp;
input Type ty;
protected
Type t, t2, ety;
list<Expression> el;
Expression e1, e2;
algorithm
ety := Type.arrayElementType(ty);
exp := match exp
// Integer can be cast to Real.
case INTEGER()
then if Type.isReal(ety) then REAL(intReal(exp.value)) else typeCastGeneric(exp, ety);
// Boolean can be cast to Real (only if -d=nfAPI is on)
// as there are annotations having expressions such as Boolean x > 0.5
case BOOLEAN()
then if Type.isReal(ety) and Flags.isSet(Flags.NF_API) then
REAL(if exp.value then 1.0 else 0.0) else typeCastGeneric(exp, ety);
// Real doesn't need to be cast to Real, since we convert e.g. array with
// a mix of Integers and Reals to only Reals.
case REAL()
then if Type.isReal(ety) then exp else typeCastGeneric(exp, ety);
// For arrays we typecast each element and update the type of the array.
case ARRAY(ty = t, elements = el)
algorithm
el := list(typeCast(e, ety) for e in el);
t := Type.setArrayElementType(t, ety);
then
ARRAY(t, el, exp.literal);
case RANGE(ty = t)
algorithm
t := Type.setArrayElementType(t, ety);
then
RANGE(t, typeCast(exp.start, ety), typeCastOpt(exp.step, ety), typeCast(exp.stop, ety));
// Unary operators (i.e. -) are handled by casting the operand.
case UNARY()
algorithm
t := Type.setArrayElementType(Operator.typeOf(exp.operator), ety);
then
UNARY(Operator.setType(t, exp.operator), typeCast(exp.exp, ety));
// If-expressions are handled by casting each of the branches.
case IF()
algorithm
e1 := typeCast(exp.trueBranch, ety);
e2 := typeCast(exp.falseBranch, ety);
t := if Type.isConditionalArray(exp.ty) then
Type.setConditionalArrayTypes(exp.ty, typeOf(e1), typeOf(e2)) else typeOf(e1);
then
IF(t, exp.condition, e1, e2);
// Calls are handled by Call.typeCast, which has special rules for some functions.
case CALL()
then Call.typeCast(exp, ety);
// Casting a cast expression overrides its current cast type.
case CAST() then typeCast(exp.exp, ty);
case BINDING_EXP()
algorithm
t := Type.setArrayElementType(exp.expType, ety);
t2 := Type.setArrayElementType(exp.bindingType, ety);
then
BINDING_EXP(typeCast(exp.exp, ety), t, t2, exp.parents, exp.isEach);
// Other expressions are handled by making a CAST expression.
else typeCastGeneric(exp, ety);
end match;
end typeCast;
function typeCastGeneric
input output Expression exp;
input Type ty;
algorithm
exp := CAST(Type.setArrayElementType(typeOf(exp), ty), exp);
end typeCastGeneric;
function realValue
input Expression exp;
output Real value;
algorithm
value := match exp
case REAL() then exp.value;
case INTEGER() then intReal(exp.value);
end match;
end realValue;
function makeReal
input Real value;
output Expression exp = REAL(value);
end makeReal;
function integerValue
input Expression exp;
output Integer value;
algorithm
INTEGER(value=value) := exp;
end integerValue;
function makeInteger
input Integer value;
output Expression exp = INTEGER(value);
end makeInteger;
function stringValue
input Expression exp;
output String value;
algorithm
STRING(value=value) := exp;
end stringValue;
function makeArray
input Type ty;
input list<Expression> expl;
input Boolean literal = false;
output Expression outExp;
algorithm
outExp := ARRAY(ty, expl, literal);
annotation(__OpenModelica_EarlyInline = true);
end makeArray;
function makeEmptyArray
input Type ty;
output Expression outExp;
algorithm
outExp := ARRAY(ty, {}, true);
annotation(__OpenModelica_EarlyInline = true);
end makeEmptyArray;
function makeIntegerArray
input list<Integer> values;
output Expression exp;
algorithm
exp := makeArray(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(listLength(values))}),
list(INTEGER(v) for v in values),
literal = true);
end makeIntegerArray;
function makeRealArray
input list<Real> values;
output Expression exp;
algorithm
exp := makeArray(Type.ARRAY(Type.REAL(), {Dimension.fromInteger(listLength(values))}),
list(REAL(v) for v in values),
literal = true);
end makeRealArray;
function makeRealMatrix
input list<list<Real>> values;
output Expression exp;
protected
Type ty;
list<Expression> expl;
algorithm
if listEmpty(values) then
ty := Type.ARRAY(Type.REAL(), {Dimension.fromInteger(0), Dimension.UNKNOWN()});
exp := makeEmptyArray(ty);
else
ty := Type.ARRAY(Type.REAL(), {Dimension.fromInteger(listLength(listHead(values)))});
expl := list(makeArray(ty, list(REAL(v) for v in row), literal = true) for row in values);
ty := Type.liftArrayLeft(ty, Dimension.fromInteger(listLength(expl)));
exp := makeArray(ty, expl, literal = true);
end if;
end makeRealMatrix;
function makeExpArray
input list<Expression> elements;
input Boolean isLiteral = false;
output Expression exp;
protected
Type ty;
algorithm
ty := typeOf(listHead(elements));
ty := Type.liftArrayLeft(ty, Dimension.fromInteger(listLength(elements)));
exp := makeArray(ty, elements, isLiteral);
end makeExpArray;
function makeRecord
input Absyn.Path recordName;
input Type recordType;
input list<Expression> fields;
output Expression exp;
algorithm
exp := RECORD(recordName, recordType, fields);
end makeRecord;
function applySubscripts
"Subscripts an expression with the given list of subscripts."
input list<Subscript> subscripts;
input Expression exp;
output Expression outExp;
algorithm
if listEmpty(subscripts) then