This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
Expression.mo
13200 lines (11736 loc) · 389 KB
/
Expression.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 package Expression
" file: Expression.mo
package: Expression
description: Expressions
This file contains the module `Expression\', which contains data types for
describing expressions, after they have been examined by the
static analyzer in the module `StaticExp\'. There are of course
great similarities with the expression types in the `Absyn\'
module, but there are also several important differences.
No overloading of operators occur, and subscripts have been
checked to see if they are slices. All expressions are also type
consistent, and all implicit type conversions in the AST are made
explicit here."
// public imports
public import Absyn;
public import DAE;
protected
type ComponentRef = DAE.ComponentRef;
type Exp = DAE.Exp;
type Operator = DAE.Operator;
type Type = DAE.Type;
type Subscript = DAE.Subscript;
type Var = DAE.Var;
// protected imports
protected import Array;
protected import ClassInf;
protected import ComponentReference;
protected import Config;
protected import DAEUtil;
protected import Debug;
protected import DoubleEndedList;
protected import FCore;
protected import FGraph;
protected import Error;
protected import ExpressionDump;
protected import ExpressionDump.printExpStr;
protected import ExpressionSimplify;
protected import Dump;
protected import Flags;
protected import List;
protected import Patternm;
protected import Prefix;
protected import Static;
protected import System; // stringReal
protected import Types;
protected import Util;
/***************************************************/
/* transform to other types */
/***************************************************/
public function intSubscript
"Converts an integer into an index subscript."
input Integer inInteger;
output DAE.Subscript outSubscript;
algorithm
outSubscript := DAE.INDEX(DAE.ICONST(inInteger));
end intSubscript;
public function intSubscripts
"Converts a list of integers into index subscripts."
input list<Integer> inIntegers;
output list<DAE.Subscript> outSubscripts;
algorithm
outSubscripts := List.map(inIntegers, intSubscript);
end intSubscripts;
public function subscriptInt
"Tries to convert a subscript to an integer index."
input DAE.Subscript inSubscript;
output Integer outInteger = expArrayIndex(subscriptIndexExp(inSubscript));
end subscriptInt;
public function subscriptsInt
"Tries to convert a list of subscripts to integer indices."
input list<DAE.Subscript> inSubscripts;
output list<Integer> outIntegers;
algorithm
outIntegers := List.map(inSubscripts, subscriptInt);
end subscriptsInt;
public function dimensionIsZero
input DAE.Dimension inDimension;
output Boolean outIsZero;
algorithm
outIsZero := 0 == dimensionSize(inDimension);
end dimensionIsZero;
public function unelabExp
"Transform an DAE.Exp into Absyn.Expression.
Note: This function currently only works for
constants and component references."
input DAE.Exp inExp;
output Absyn.Exp outExp;
algorithm
outExp := matchcontinue (inExp)
local
Integer i;
Real r;
String s;
Boolean b;
Absyn.ComponentRef cr_1;
ComponentRef cr;
list<Absyn.Exp> expl_1,aexpl;
list<DAE.Exp> expl;
DAE.Exp e1,e2,e3;
Operator op;
Absyn.Exp ae1,ae2,ae3;
Absyn.Operator aop;
list<list<DAE.Exp>> mexpl2;
list<list<Absyn.Exp>> amexpl;
Absyn.ComponentRef acref;
Absyn.Path path;
Absyn.CodeNode code;
DAE.ReductionIterators riters;
Absyn.ForIterators aiters;
DAE.Type ty;
DAE.Dimensions dims;
Absyn.ReductionIterType iterType;
case (DAE.ICONST(integer = i)) then Absyn.INTEGER(i);
case (DAE.RCONST(real = r))
equation
s = realString(r);
then Absyn.REAL(s);
case (DAE.SCONST(string = s)) then Absyn.STRING(s);
case (DAE.BCONST(bool = b)) then Absyn.BOOL(b);
case (DAE.ENUM_LITERAL(name = path))
equation
cr_1 = Absyn.pathToCref(path);
then Absyn.CREF(cr_1);
case (DAE.CREF(componentRef = cr))
equation
cr_1 = ComponentReference.unelabCref(cr);
then
Absyn.CREF(cr_1);
case(DAE.BINARY(e1,op,e2)) equation
aop = unelabOperator(op);
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
then Absyn.BINARY(ae1,aop,ae2);
case(DAE.UNARY(op,e1)) equation
aop = unelabOperator(op);
ae1 = unelabExp(e1);
then Absyn.UNARY(aop,ae1);
case(DAE.LBINARY(e1,op,e2)) equation
aop = unelabOperator(op);
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
then Absyn.LBINARY(ae1,aop,ae2);
case(DAE.LUNARY(op,e1)) equation
aop = unelabOperator(op);
ae1 = unelabExp(e1);
then Absyn.LUNARY(aop,ae1);
case(DAE.RELATION(exp1=e1,operator=op,exp2=e2)) equation
aop = unelabOperator(op);
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
then Absyn.RELATION(ae1,aop,ae2);
case(DAE.IFEXP(e1,e2,e3)) equation
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
ae3 = unelabExp(e3);
then Absyn.IFEXP(ae1,ae2,ae3,{});
case (DAE.CALL(path,expl,_))
equation
aexpl = List.map(expl,unelabExp);
acref = Absyn.pathToCref(path);
then Absyn.CALL(acref,Absyn.FUNCTIONARGS(aexpl,{}));
case (DAE.RECORD(path = path,exps = expl))
equation
aexpl = List.map(expl,unelabExp);
acref = Absyn.pathToCref(path);
then Absyn.CALL(acref,Absyn.FUNCTIONARGS(aexpl,{}));
case(DAE.PARTEVALFUNCTION(path,expl,_,_))
equation
aexpl = List.map(expl,unelabExp);
acref = Absyn.pathToCref(path);
then
Absyn.PARTEVALFUNCTION(acref,Absyn.FUNCTIONARGS(aexpl,{}));
case (DAE.ARRAY(array = {}, ty = ty))
equation
(ty, dims) = Types.flattenArrayType(ty);
ae1 = unleabZeroExpFromType(ty);
expl_1 = List.map(dims, unelabDimensionToFillExp);
then
Absyn.CALL(Absyn.CREF_IDENT("fill",{}),Absyn.FUNCTIONARGS(ae1::expl_1,{}));
case (DAE.ARRAY(array = expl))
equation
expl_1 = List.map(expl, unelabExp);
then
Absyn.ARRAY(expl_1);
case(DAE.MATRIX(matrix = mexpl2))
equation
amexpl = List.mapList(mexpl2,unelabExp);
then (Absyn.MATRIX(amexpl));
case(DAE.RANGE(_,e1,SOME(e2),e3)) equation
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
ae3 = unelabExp(e3);
then Absyn.RANGE(ae1,SOME(ae2),ae3);
case(DAE.RANGE(_,e1,NONE(),e3)) equation
ae1 = unelabExp(e1);
ae3 = unelabExp(e3);
then Absyn.RANGE(ae1,NONE(),ae3);
case(DAE.TUPLE(expl))
equation
expl_1 = List.map(expl, unelabExp);
then
Absyn.TUPLE(expl_1);
case(DAE.CAST(_,e1)) equation
ae1 = unelabExp(e1);
then ae1;
// ASUB can not be unelabed since it has no representation in Absyn.
case(DAE.ASUB(_,_)) equation
print("Internal Error, can not unelab ASUB\n");
then fail();
// TSUB(expression) => expression
case(DAE.TSUB(e1,_,_)) equation
ae1 = unelabExp(e1);
then ae1;
case(DAE.SIZE(e1,SOME(e2))) equation
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
then Absyn.CALL(Absyn.CREF_IDENT("size",{}),Absyn.FUNCTIONARGS({ae1,ae2},{}));
/* WHAT? exactly the same case as above???!!!
case(DAE.SIZE(e1,SOME(e2))) equation
ae1 = unelabExp(e1);
ae2 = unelabExp(e2);
then Absyn.CALL(Absyn.CREF_IDENT("size",{}),Absyn.FUNCTIONARGS({ae1,ae2},{}));
*/
case(DAE.CODE(code,_)) then Absyn.CODE(code);
case DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(iterType=iterType,path=path),expr=e1,iterators=riters)
equation
//print("unelab of reduction not impl. yet");
acref = Absyn.pathToCref(path);
ae1 = unelabExp(e1);
aiters = List.map(riters, unelabReductionIterator);
then Absyn.CALL(acref, Absyn.FOR_ITER_FARG(ae1, iterType, aiters));
else
equation
true = Flags.isSet(Flags.FAILTRACE);
print("Expression.unelabExp failed on: " + ExpressionDump.printExpStr(inExp) + "\n");
then
fail();
end matchcontinue;
end unelabExp;
public function unelabDimension
"Transform an DAE.Dimension into Absyn.Subscript, if possible"
input DAE.Dimension inDim;
output Absyn.Subscript outDim;
algorithm
outDim := match (inDim)
local
Integer i;
Absyn.Path p;
Absyn.ComponentRef c;
DAE.Exp e;
Absyn.Exp ae;
case (DAE.DIM_INTEGER(i)) then Absyn.SUBSCRIPT(Absyn.INTEGER(i));
case (DAE.DIM_BOOLEAN()) then Absyn.SUBSCRIPT(Absyn.CREF(Absyn.CREF_IDENT("Boolean", {})));
case (DAE.DIM_ENUM(enumTypeName = p))
equation
c = Absyn.pathToCref(p);
then
Absyn.SUBSCRIPT(Absyn.CREF(c));
case (DAE.DIM_EXP(e))
equation
ae = unelabExp(e);
then
Absyn.SUBSCRIPT(ae);
case (DAE.DIM_UNKNOWN()) then Absyn.NOSUB();
end match;
end unelabDimension;
protected function unleabZeroExpFromType
input DAE.Type ty;
output Absyn.Exp outExp;
algorithm
outExp := match ty
case DAE.T_BOOL() then Absyn.BOOL(false);
case DAE.T_STRING() then Absyn.STRING("");
case DAE.T_INTEGER() then Absyn.INTEGER(0);
case DAE.T_REAL() then Absyn.REAL("0.0");
case DAE.T_UNKNOWN() then Absyn.REAL("0.0"); /* Look at the crap unelabMod needs... */
end match;
end unleabZeroExpFromType;
protected function unelabDimensionToFillExp
"Transform an DAE.Dimension into Absyn.Exp, if possible"
input DAE.Dimension inDim;
output Absyn.Exp outExp;
algorithm
outExp := match (inDim)
local
Integer i;
DAE.Exp e;
case (DAE.DIM_INTEGER(i)) then Absyn.INTEGER(i);
case (DAE.DIM_EXP(e)) then unelabExp(e);
else Absyn.INTEGER(1); /* Probably bad, but only used with zero-length arrays */
end match;
end unelabDimensionToFillExp;
protected function unelabReductionIterator
input DAE.ReductionIterator riter;
output Absyn.ForIterator aiter;
algorithm
aiter := match riter
local
String id;
DAE.Exp exp;
Option<DAE.Exp> gexp;
Absyn.Exp aexp;
Option<Absyn.Exp> agexp;
case DAE.REDUCTIONITER(id=id,exp=exp,guardExp=gexp)
equation
aexp = unelabExp(exp);
agexp = Util.applyOption(gexp, unelabExp);
then Absyn.ITERATOR(id,agexp,SOME(aexp));
end match;
end unelabReductionIterator;
protected function unelabOperator "help function to unelabExpression."
input DAE.Operator op;
output Absyn.Operator aop;
algorithm
aop := match(op)
case(DAE.ADD(_)) then Absyn.ADD();
case(DAE.SUB(_)) then Absyn.SUB();
case(DAE.MUL(_)) then Absyn.MUL();
case(DAE.DIV(_)) then Absyn.DIV();
case(DAE.POW(_)) then Absyn.POW();
case(DAE.UMINUS(_)) then Absyn.UMINUS();
case(DAE.UMINUS_ARR(_)) then Absyn.UMINUS();
case(DAE.ADD_ARR(_)) then Absyn.ADD();
case(DAE.SUB_ARR(_)) then Absyn.SUB();
case(DAE.MUL_ARR(_)) then Absyn.MUL();
case(DAE.DIV_ARR(_)) then Absyn.DIV();
case(DAE.MUL_ARRAY_SCALAR(_)) then Absyn.MUL();
case(DAE.ADD_ARRAY_SCALAR(_)) then Absyn.ADD();
case(DAE.SUB_SCALAR_ARRAY(_)) then Absyn.SUB();
case(DAE.MUL_SCALAR_PRODUCT(_)) then Absyn.MUL();
case(DAE.MUL_MATRIX_PRODUCT(_)) then Absyn.MUL();
case(DAE.DIV_SCALAR_ARRAY(_)) then Absyn.DIV();
case(DAE.DIV_ARRAY_SCALAR(_)) then Absyn.DIV();
case(DAE.POW_SCALAR_ARRAY(_)) then Absyn.POW();
case(DAE.POW_ARRAY_SCALAR(_)) then Absyn.POW();
case(DAE.POW_ARR(_)) then Absyn.POW();
case(DAE.POW_ARR2(_)) then Absyn.POW();
case(DAE.AND(_)) then Absyn.AND();
case(DAE.OR(_)) then Absyn.OR();
case(DAE.NOT(_)) then Absyn.NOT();
case(DAE.LESS(_)) then Absyn.LESS();
case(DAE.LESSEQ(_)) then Absyn.LESSEQ();
case(DAE.GREATER(_)) then Absyn.GREATER();
case(DAE.GREATEREQ(_)) then Absyn.GREATEREQ();
case(DAE.EQUAL(_)) then Absyn.EQUAL();
case(DAE.NEQUAL(_)) then Absyn.NEQUAL();
end match;
end unelabOperator;
public function stringifyCrefs
"This function takes an expression and transforms all component
reference names contained in the expression to a simpler form.
For instance DAE.CREF_QUAL(a,{}, DAE.CREF_IDENT(b,{})) becomes
DAE.CREF_IDENT(a.b,{})
NOTE: This function should not be used in OMC, since the OMC backend no longer
uses stringified components. It is still used by MathCore though."
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := traverseExpDummy(inExp, traversingstringifyCrefFinder);
end stringifyCrefs;
public function traversingstringifyCrefFinder "
helper for stringifyCrefs"
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := match inExp
local
ComponentRef cr,crs;
Type ty;
DAE.Exp e;
list<Boolean> blist;
case DAE.CREF(ty = DAE.T_FUNCTION_REFERENCE_VAR())
then inExp;
case DAE.CREF(ty = DAE.T_FUNCTION_REFERENCE_FUNC())
then inExp;
case DAE.CREF(cr,ty)
equation
crs = ComponentReference.stringifyComponentRef(cr);
then makeCrefExp(crs,ty);
else inExp;
end match;
end traversingstringifyCrefFinder;
public function CodeVarToCref
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := match(inExp)
local
ComponentRef e_cref;
Absyn.ComponentRef cref;
DAE.Exp e;
case(DAE.CODE(Absyn.C_VARIABLENAME(cref),_))
equation
(_,e_cref) = Static.elabUntypedCref(FCore.emptyCache(),FGraph.empty(),cref,false,Prefix.NOPRE(),Absyn.dummyInfo);
e = crefExp(e_cref);
then
e;
case(DAE.CODE(Absyn.C_EXPRESSION(Absyn.CALL(Absyn.CREF_IDENT("der",{}),Absyn.FUNCTIONARGS({Absyn.CREF(cref)},{}))),_))
equation
(_,e_cref) = Static.elabUntypedCref(FCore.emptyCache(),FGraph.empty(),cref,false,Prefix.NOPRE(),Absyn.dummyInfo);
e = crefExp(e_cref);
then
DAE.CALL(Absyn.IDENT("der"),{e},DAE.callAttrBuiltinReal);
end match;
end CodeVarToCref;
public function realToIntIfPossible
"converts to ICONST if possible. If it does
not fit, a RCONST is returned instead."
input Real inVal;
output DAE.Exp outVal;
algorithm
try
outVal := DAE.ICONST(realInt(inVal));
else
outVal := DAE.RCONST(inVal);
end try;
end realToIntIfPossible;
public function liftArrayR "
function liftArrayR
Converts a type into an array type with dimension n as first dim"
input DAE.Type tp;
input DAE.Dimension n;
output DAE.Type outTp;
algorithm
outTp := match(tp,n)
local
Type elt_tp;
list<DAE.Dimension> dims;
case(DAE.T_ARRAY(elt_tp,dims),_)
equation
dims = n::dims;
then
DAE.T_ARRAY(elt_tp,dims);
else DAE.T_ARRAY(tp,{n});
end match;
end liftArrayR;
public function dimensionSizeConstantExp
"Converts (extracts) a dimension to an expression.
This function will fail if dimension is unknown or an expression (in case the dimension is from a different scope).
If you want to(kind of) handle unknown dims use dimensionSizeExpHandleUnkown."
input DAE.Dimension dim;
output DAE.Exp exp;
algorithm
exp := match(dim)
local
Integer i;
case DAE.DIM_INTEGER(integer = i) then DAE.ICONST(i);
case DAE.DIM_ENUM(size = i) then DAE.ICONST(i);
case DAE.DIM_BOOLEAN() then DAE.ICONST(2);
end match;
end dimensionSizeConstantExp;
public function dimensionSizeExp
"Converts (extracts) a dimension to an expression.
This function will fail if dimension is unknown. i.e. DIM_UNKNOWN.
If you want to(kind of) handle unknown dims use dimensionSizeExpHandleUnkown."
input DAE.Dimension dim;
output DAE.Exp exp;
algorithm
exp := match(dim)
local
Integer i;
DAE.Exp e;
case DAE.DIM_INTEGER(integer = i) then DAE.ICONST(i);
case DAE.DIM_ENUM(size = i) then DAE.ICONST(i);
case DAE.DIM_BOOLEAN() then DAE.ICONST(2);
case DAE.DIM_EXP(exp = e) then e;
end match;
end dimensionSizeExp;
public function dimensionSizeExpHandleUnkown
"Converts (extracts) a dimension to an expression.
This function will change unknown dims to DAE.ICONST(-1).
we use it to handle unknown dims in code generation. unknown dims
are okay if the variable is a function input (it's just holds the slot
and will not be generated). Otherwise it's an error
since it shouldn't have reached there."
input DAE.Dimension dim;
output DAE.Exp exp;
algorithm
exp := match(dim)
case DAE.DIM_UNKNOWN() then DAE.ICONST(-1);
else dimensionSizeExp(dim);
end match;
end dimensionSizeExpHandleUnkown;
public function intDimension
"Converts an integer to an array dimension."
input Integer value;
output DAE.Dimension dim;
annotation(__OpenModelica_EarlyInline = true);
algorithm
dim := DAE.DIM_INTEGER(value);
end intDimension;
public function dimensionSubscript
"Converts an array dimension to a subscript."
input DAE.Dimension dim;
output DAE.Subscript sub;
algorithm
sub := match(dim)
local
Integer i;
case DAE.DIM_INTEGER(integer = i) then DAE.INDEX(DAE.ICONST(i));
case DAE.DIM_ENUM(size = i) then DAE.INDEX(DAE.ICONST(i));
case DAE.DIM_BOOLEAN() then DAE.INDEX(DAE.ICONST(2));
case DAE.DIM_UNKNOWN() then DAE.WHOLEDIM();
end match;
end dimensionSubscript;
/***************************************************/
/* Change */
/***************************************************/
public function negate
"author: PA
Negates an expression."
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := match(inExp)
local
Type t;
Operator op;
Boolean b,b_1;
Real r,r_1;
Integer i,i_1;
DAE.Exp e,e1,e2;
// to avoid un-necessary --e
case(DAE.UNARY(DAE.UMINUS(_),e)) then e;
case(DAE.UNARY(DAE.UMINUS_ARR(_),e)) then e;
case(DAE.LUNARY(DAE.NOT(_),e)) then e;
// -(a*b) = (-a)*b
// -(a/b) = (-a)/b
case(DAE.BINARY(e1,op,e2)) guard(isMulOrDiv(op))
then DAE.BINARY(negate(e1),op,e2);
// -(a-b) = b-a
case(DAE.BINARY(e1,op,e2)) guard(isSub(op))
then DAE.BINARY(e2,op,e1);
case e // -0 = 0
guard isZero(e)
then e;
case (DAE.ICONST(i))
equation
i_1 = 0 - i;
then DAE.ICONST(i_1);
case (DAE.RCONST(r))
equation
r_1 = 0.0 - r;
then DAE.RCONST(r_1);
case (DAE.BCONST(b))
equation
b_1 = not b;
then DAE.BCONST(b_1);
case(e)
equation
t = typeof(e);
outExp = match (t)
case (DAE.T_BOOL()) // not e
then DAE.LUNARY(DAE.NOT(t),e);
else
equation
b = DAEUtil.expTypeArray(t);
op = if b then DAE.UMINUS_ARR(t) else DAE.UMINUS(t);
then DAE.UNARY(op,e);
end match;
then
outExp;
end match;
end negate;
public function negateReal
input DAE.Exp inReal;
output DAE.Exp outNegatedReal;
algorithm
outNegatedReal := DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT), inReal);
end negateReal;
public function expand "expands products
For example
a *(b+c) => a*b + a*c"
input DAE.Exp e;
output DAE.Exp outE;
algorithm
outE := match(e)
local
DAE.Type tp;
DAE.Operator op;
DAE.Exp e1,e2,e21,e22;
case(DAE.BINARY(e1,DAE.MUL(tp),e2 as DAE.BINARY(e21,op,e22))) guard isAddOrSub(op)
equation
DAE.BINARY(e21,op,e22) = expand(e2);
then
DAE.BINARY(DAE.BINARY(e1,DAE.MUL(tp),e21),op,DAE.BINARY(e1,DAE.MUL(tp),e22));
else e;
end match;
end expand;
public function expDer
"author: Frenkel TUD 2012-11
exp -> der(exp)"
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := DAE.CALL(Absyn.IDENT("der"),{inExp},DAE.callAttrBuiltinReal);
end expDer;
public function expAbs
"author: PA
Makes the expression absolute. i.e. non-negative."
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := match(inExp)
local
Integer i2,i;
Real r2,r;
DAE.Exp e_1,e,e1_1,e2_1,e1,e2;
Type tp;
Operator op;
case (DAE.ICONST(integer = i))
equation
i2 = intAbs(i);
then
DAE.ICONST(i2);
case (DAE.RCONST(real = r))
equation
r2 = realAbs(r);
then
DAE.RCONST(r2);
case (DAE.UNARY(operator = DAE.UMINUS(),exp = e))
equation
e_1 = expAbs(e);
then
e_1;
case (DAE.BINARY(exp1 = e1,operator = op,exp2 = e2))
equation
e1_1 = expAbs(e1);
e2_1 = expAbs(e2);
then
DAE.BINARY(e1_1,op,e2_1);
else inExp;
end match;
end expAbs;
public function stripNoEvent
"Function that strips all noEvent() calls in an expression"
input DAE.Exp e;
output DAE.Exp outE;
algorithm
outE := traverseExpDummy(e,stripNoEventExp);
end stripNoEvent;
protected function stripNoEventExp "
traversal function for stripNoEvent"
input DAE.Exp e;
output DAE.Exp outExp;
algorithm
outExp := match e
case DAE.CALL(path=Absyn.IDENT("noEvent"),expLst={outExp}) then outExp;
else e;
end match;
end stripNoEventExp;
public function addNoEventToRelations
"Function that adds a noEvent() call to all relations in an expression"
input DAE.Exp e;
output DAE.Exp outE;
algorithm
outE := traverseExpDummy(e,addNoEventToRelationExp);
end addNoEventToRelations;
protected function addNoEventToRelationExp "
traversal function for addNoEventToRelations"
input DAE.Exp e;
output DAE.Exp outExp;
algorithm
outExp := match e
case DAE.RELATION() then makeNoEvent(e);
else e;
end match;
end addNoEventToRelationExp;
public function addNoEventToRelationsAndConds
"Function that adds a noEvent() call to all relations in an expression"
input DAE.Exp e;
output DAE.Exp outE;
algorithm
outE := traverseExpDummy(e,addNoEventToRelationandCondExp);
end addNoEventToRelationsAndConds;
protected function addNoEventToRelationandCondExp "
traversal function for addNoEventToRelationsAndConds"
input DAE.Exp e;
output DAE.Exp outExp;
algorithm
outExp := match e
local
DAE.Exp e1,e2,e3;
case DAE.RELATION() then makeNoEvent(e);
case DAE.IFEXP(e1,e2,e3) then DAE.IFEXP(makeNoEvent(e1),e2,e3);
else e;
end match;
end addNoEventToRelationandCondExp;
public function addNoEventToEventTriggeringFunctions
" Function that adds a noEvent() call to all event triggering functions in an expression"
input DAE.Exp e;
output DAE.Exp outE;
algorithm
outE := traverseExpDummy(e,addNoEventToEventTriggeringFunctionsExp);
end addNoEventToEventTriggeringFunctions;
protected function addNoEventToEventTriggeringFunctionsExp "
traversal function for addNoEventToEventTriggeringFunctions"
input DAE.Exp e;
output DAE.Exp outExp;
algorithm
outExp := match e
case DAE.CALL()
guard
isEventTriggeringFunctionExp(e)
then makeNoEvent(e);
else e;
end match;
end addNoEventToEventTriggeringFunctionsExp;
public function expStripLastSubs
"Strips the last subscripts of a Exp"
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp := match (inExp)
local
ComponentRef cr,cr_1;
Type ty;
Operator op,op1;
DAE.Exp e,e_1;
Boolean b;
case (DAE.CREF(componentRef=cr))
equation
ty = ComponentReference.crefLastType(cr);
cr_1 = ComponentReference.crefStripLastSubs(cr);
e = makeCrefExp(cr_1, ty);
then
e;
case (DAE.UNARY(exp=e))
equation
e_1 = expStripLastSubs(e);
ty = typeof(e_1);
b = DAEUtil.expTypeArray(ty);
op1 = if b then DAE.UMINUS_ARR(ty) else DAE.UMINUS(ty);
then
DAE.UNARY(op1,e_1);
end match;
end expStripLastSubs;
public function expStripLastIdent
"Strips the last identifier of a cref Exp"
input DAE.Exp inExp;
output DAE.Exp outExp;
algorithm
outExp:=
match (inExp)
local
ComponentRef cr,cr_1;
Type ty;
Operator op,op1;
DAE.Exp e,e_1;
Boolean b;
case (DAE.CREF(componentRef=cr))
equation
cr_1 = ComponentReference.crefStripLastIdent(cr);
ty = ComponentReference.crefLastType(cr_1);
e = makeCrefExp(cr_1, ty);
then
e;
case (DAE.UNARY(exp=e))
equation
e_1 = expStripLastIdent(e);
ty = typeof(e_1);
b = DAEUtil.expTypeArray(ty);
op1 = if b then DAE.UMINUS_ARR(ty) else DAE.UMINUS(ty);
then
DAE.UNARY(op1,e_1);
end match;
end expStripLastIdent;
public function prependSubscriptExp
"Prepends a subscript to a CREF expression
For instance a.b[1,2] with subscript 'i' becomes a.b[i,1,2]."
input DAE.Exp exp;
input DAE.Subscript subscr;
output DAE.Exp outExp;
algorithm
outExp := match(exp,subscr)
local
Type t; ComponentRef cr,cr1,cr2;
list<DAE.Subscript> subs;
DAE.Exp e;
case (DAE.CREF(cr,t),_)
equation
cr1 = ComponentReference.crefStripLastSubs(cr);
subs = ComponentReference.crefLastSubs(cr);
cr2 = ComponentReference.subscriptCref(cr1,subscr::subs);
e = makeCrefExp(cr2, t);
then
e;
end match;
end prependSubscriptExp;
public function applyExpSubscripts "
author: PA
Takes an arbitrary expression and applies subscripts to it. This is done by creating asub
expressions given the original expression and then simplify them.
Note: The subscripts must be INDEX
alternative names: subsriptExp (but already taken), subscriptToAsub"
input output DAE.Exp exp;
input list<DAE.Subscript> inSubs;
protected
DAE.Exp s;
algorithm
exp := applyExpSubscriptsFoldCheckSimplify(exp, inSubs);
end applyExpSubscripts;
public function applyExpSubscriptsFoldCheckSimplify "
author: PA
Takes an arbitrary expression and applies subscripts to it. This is done by creating asub
expressions given the original expression and then simplify them.
Note: The subscripts must be INDEX
alternative names: subsriptExp (but already taken), subscriptToAsub
This version of the function also returns a boolean stating if simplify
improved anything (can be used as a heuristic if you want to apply
the subscript when scalarizing)"
input output DAE.Exp exp;
input list<DAE.Subscript> inSubs;
input output Boolean checkSimplify = false;
protected
Boolean b;
DAE.Exp s;
algorithm
for sub in inSubs loop
// Apply one subscript at a time, so simplify works fine on it.
//s := subscriptIndexExp(sub);
try
s := getSubscriptExp(sub);
(exp,b) := ExpressionSimplify.simplify(makeASUB(exp,{s}));
checkSimplify := b or checkSimplify;
else
// skipped DAE.WHOLEDIM
end try;
end for;
end applyExpSubscriptsFoldCheckSimplify;
public function subscriptExp
"@mahge
This function does the same job as 'applyExpSubscripts' function.
However this one doesn't use ExpressionSimplify.simplify.
Takes an expression and a list of subscripts and subscripts
the given expression.
If a component reference is given the subs are applied to it.
If an array(DAE.ARRAY) is given the element at the specified
subscripts is returned.
e.g. subscriptExp on ({{1,2},{3,4}}) with sub [2,1] gives 3
subscriptExp on (a) with sub [2,1] gives a[2,1]
"
input DAE.Exp inExp;
input list<DAE.Subscript> inSubs;
output DAE.Exp outArg;
algorithm