/
Static.mo
12702 lines (11481 loc) · 449 KB
/
Static.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 Static
" file: Static.mo
package: Static
description: Static analysis of expressions
This module does static analysis on expressions.
The analyzed expressions are built using the
constructors in the Expression module from expressions defined in Absyn.
Also, a set of properties of the expressions is calculated during analysis.
Properties of expressions include type information and a boolean indicating if the
expression is constant or not.
If the expression is constant, the Ceval module is used to evaluate the expression
value. A value of an expression is described using the Values module.
The main function in this module is evalExp which takes an Absyn.Exp and transform it
into an DAE.Exp, while performing type checking and automatic type conversions, etc.
To determine types of builtin functions and operators, the module also contain an elaboration
handler for functions and operators. This function is called elabBuiltinHandler.
NOTE: These functions should only determine the type and properties of the builtin functions and
operators and not evaluate them. Constant evaluation is performed by the Ceval module.
The module also contain a function for deoverloading of operators, in the \'deoverload\' function.
It transforms operators like + to its specific form, ADD, ADD_ARR, etc.
Interactive function calls are also given their types by elabExp, which calls
elabCallInteractive.
Elaboration for functions involve checking the types of the arguments by filling slots of the
argument list with first positional and then named arguments to find a matching function. The
details of this mechanism can be found in the Modelica specification.
The elaboration also contain function deoverloading which will be added to Modelica in the future."
import Absyn;
import DAE;
import FCore;
import SCode;
import Values;
protected
import AbsynToSCode;
import AbsynUtil;
import FGraph;
import FNode;
import InstMeta;
import MetaUtil;
import Util;
constant Integer SLOT_NOT_EVALUATED = 0;
constant Integer SLOT_EVALUATING = 1;
constant Integer SLOT_EVALUATED = 2;
uniontype Slot
record SLOT
DAE.FuncArg defaultArg "The slots default argument.";
Boolean slotFilled "True if the slot has been filled, otherwise false.";
Option<DAE.Exp> arg "The argument for the slot given by the function call.";
DAE.Dimensions dims "The dimensions of the slot.";
Integer idx "The index of the slot, 1 = first slot etc.";
Integer evalStatus;
end SLOT;
end Slot;
constant Option<tuple<DAE.Exp, DAE.Properties, DAE.Attributes>> BUILTIN_TIME =
SOME((DAE.CREF(DAE.CREF_IDENT("time", DAE.T_REAL_DEFAULT, {}), DAE.T_REAL_DEFAULT),
DAE.PROP(DAE.T_REAL_DEFAULT, DAE.C_VAR()),
DAE.dummyAttrInput));
import Array;
import BackendInterface;
import Ceval;
import ClassInf;
import ComponentReference;
import Config;
import DAEUtil;
import Debug;
import Dump;
import Error;
import ErrorExt;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Flags;
import Global;
import Inline;
import Inst;
import InstFunction;
import InstTypes;
import InnerOuter;
import List;
import Lookup;
import Mutable;
import OperatorOverloading;
import Patternm;
import PrefixUtil;
import Print;
import SCodeDump;
import SCodeUtil;
import System;
import Types;
import ValuesUtil;
import VarTransform;
public function elabExpList "Expression elaboration of Absyn.Exp list, i.e. lists of expressions."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input list<Absyn.Exp> inExpl;
input Boolean inImplicit;
input Boolean inDoVect;
input DAE.Prefix inPrefix;
input SourceInfo inInfo;
input DAE.Type inLastType = DAE.T_UNKNOWN_DEFAULT "The type of the last evaluated expression; used to speed up instantiation of enumeration :)";
output FCore.Cache outCache = inCache;
output list<DAE.Exp> outExpl = {};
output list<DAE.Properties> outProperties = {};
protected
DAE.Exp exp;
DAE.Properties prop;
DAE.Type last_ty = inLastType;
algorithm
for e in inExpl loop
_ := matchcontinue(e, last_ty)
local
Absyn.ComponentRef cr;
Absyn.Path path, path1, path2;
String name;
list<String> names;
Integer idx;
// Hack to make enumeration arrays elaborate a _lot_ faster
case (Absyn.CREF(cr as Absyn.CREF_FULLYQUALIFIED()),
DAE.T_ENUMERATION(path = path2, names = names))
algorithm
path := AbsynUtil.crefToPath(cr);
(path1, Absyn.IDENT(name)) := AbsynUtil.splitQualAndIdentPath(path);
true := AbsynUtil.pathEqual(path1, path2);
idx := List.position(name, names);
exp := DAE.ENUM_LITERAL(path, idx);
prop := DAE.PROP(last_ty, DAE.C_CONST());
then
();
else
algorithm
(outCache, exp, prop) := elabExpInExpression(outCache, inEnv,
e, inImplicit, inDoVect, inPrefix, inInfo);
last_ty := Types.getPropType(prop);
then
();
end matchcontinue;
outExpl := exp :: outExpl;
outProperties := prop :: outProperties;
end for;
outExpl := listReverse(outExpl);
outProperties := listReverse(outProperties);
end elabExpList;
protected function elabExpList_enum
input Absyn.Exp inExp;
input DAE.Type inLastType;
output Integer outIndex;
algorithm
outIndex := matchcontinue(inExp, inLastType)
local
Absyn.ComponentRef cr;
Absyn.Path path, path1, path2;
String name;
list<String> names;
case (Absyn.CREF(cr as Absyn.CREF_FULLYQUALIFIED()),
DAE.T_ENUMERATION(path = path2, names = names))
algorithm
path := AbsynUtil.crefToPath(cr);
(path1, Absyn.IDENT(name)) := AbsynUtil.splitQualAndIdentPath(path);
true := AbsynUtil.pathEqual(path1, path2);
then
List.position(name, names);
else -1;
end matchcontinue;
end elabExpList_enum;
public function elabExpListList
"Expression elaboration of lists of lists of expressions.
Used in for instance matrices, etc."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input list<list<Absyn.Exp>> inExpl;
input Boolean inImplicit;
input Boolean inDoVect;
input DAE.Prefix inPrefix;
input SourceInfo inInfo;
input DAE.Type inLastType = DAE.T_UNKNOWN_DEFAULT "The type of the last evaluated expression; used to speed up instantiation of enumerations :)";
output FCore.Cache outCache = inCache;
output list<list<DAE.Exp>> outExpl = {};
output list<list<DAE.Properties>> outProperties = {};
protected
list<DAE.Exp> expl;
list<DAE.Properties> props;
DAE.Type last_ty = inLastType;
algorithm
for lst in inExpl loop
(outCache, expl, props) := elabExpList(outCache, inEnv, lst,
inImplicit, inDoVect, inPrefix, inInfo, last_ty);
outExpl := expl :: outExpl;
outProperties := props :: outProperties;
last_ty := Types.getPropType(listHead(props));
end for;
outExpl := listReverse(outExpl);
outProperties := listReverse(outProperties);
end elabExpListList;
protected function elabExpOptAndMatchType "
elabExp, but for Option<Absyn.Exp>,DAE.Type => Option<DAE.Exp>"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Option<Absyn.Exp> inExp;
input DAE.Type inDefaultType;
input Boolean inImplicit;
input Boolean inDoVect;
input DAE.Prefix inPrefix;
input SourceInfo inInfo;
output FCore.Cache outCache = inCache;
output Option<DAE.Exp> outExp;
output DAE.Properties outProperties;
protected
Absyn.Exp exp;
DAE.Exp dexp;
DAE.Properties prop;
algorithm
outProperties := DAE.PROP(inDefaultType, DAE.C_CONST());
if isSome(inExp) then
SOME(exp) := inExp;
(outCache, dexp, prop) := elabExpInExpression(outCache, inEnv,
exp, inImplicit, inDoVect, inPrefix, inInfo);
(dexp, outProperties) := Types.matchProp(dexp, prop, outProperties, true);
outExp := SOME(dexp);
else
outExp := NONE();
end if;
end elabExpOptAndMatchType;
public function elabExp "
function: elabExp
Static analysis of expressions means finding out the properties of
the expression. These properties are described by the
DAE.Properties type, and include the type and the variability of the
expression. This function performs analysis, and returns an
DAE.Exp and the properties."
extends PartialElabExpFunc;
protected
Absyn.Exp e;
Integer num_errmsgs;
DAE.Exp exp, exp1, exp2;
DAE.Properties prop1, prop2;
DAE.Type ty;
DAE.Const c;
PartialElabExpFunc elabfunc;
algorithm
// Apply any rewrite rules we have, if any.
e := if BackendInterface.noRewriteRulesFrontEnd() then inExp else
BackendInterface.rewriteFrontEnd(inExp);
num_errmsgs := Error.getNumErrorMessages();
try
elabfunc := match(e)
case Absyn.END()
algorithm
Error.addSourceMessage(Error.END_ILLEGAL_USE_ERROR, {}, inInfo);
then
fail();
case Absyn.CREF() then elabExp_Cref;
case Absyn.BINARY() then elabExp_Binary;
case Absyn.UNARY() then elabExp_Unary;
case Absyn.LBINARY() then elabExp_Binary;
case Absyn.LUNARY() then elabExp_LUnary;
case Absyn.RELATION() then elabExp_Binary;
case Absyn.IFEXP() then elabExp_If;
case Absyn.CALL() then elabExp_Call;
case Absyn.PARTEVALFUNCTION() then elabExp_PartEvalFunction;
case Absyn.TUPLE() then elabExp_Tuple;
case Absyn.RANGE() then elabExp_Range;
case Absyn.ARRAY() then elabExp_Array;
case Absyn.MATRIX() then elabExp_Matrix;
case Absyn.CODE() then elabExp_Code;
case Absyn.CONS() then elabExp_Cons;
case Absyn.LIST() then elabExp_List;
case Absyn.MATCHEXP() then Patternm.elabMatchExpression;
case Absyn.DOT() then elabExp_Dot;
else elabExp_BuiltinType;
end match;
(outCache, outExp, outProperties) :=
elabfunc(inCache, inEnv, e, inImplicit, inDoVect, inPrefix, inInfo);
else
true := num_errmsgs == Error.getNumErrorMessages();
Error.addSourceMessage(Error.GENERIC_ELAB_EXPRESSION,
{Dump.printExpStr(e)}, inInfo);
fail();
end try;
end elabExp;
public partial function PartialElabExpFunc
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Exp inExp;
input Boolean inImplicit;
input Boolean inDoVect;
input DAE.Prefix inPrefix;
input SourceInfo inInfo;
output FCore.Cache outCache = inCache;
output DAE.Exp outExp;
output DAE.Properties outProperties;
end PartialElabExpFunc;
protected function elabExp_BuiltinType
extends PartialElabExpFunc;
algorithm
(outExp, outProperties) := match(inExp)
// The types below should contain the default values of the attributes of the builtin
// types. But since they are default, we can leave them out for now, unit=\"\" is not
// that interesting to find out.
case Absyn.INTEGER()
then (DAE.ICONST(inExp.value),
DAE.PROP(DAE.T_INTEGER_DEFAULT, DAE.C_CONST()));
case Absyn.REAL()
then (DAE.RCONST(System.stringReal(inExp.value)),
DAE.PROP(DAE.T_REAL_DEFAULT, DAE.C_CONST()));
case Absyn.STRING()
then (DAE.SCONST(System.unescapedString(inExp.value)),
DAE.PROP(DAE.T_STRING_DEFAULT, DAE.C_CONST()));
case Absyn.BOOL()
then (DAE.BCONST(inExp.value),
DAE.PROP(DAE.T_BOOL_DEFAULT, DAE.C_CONST()));
end match;
end elabExp_BuiltinType;
protected function elabExp_Cref
extends PartialElabExpFunc;
protected
Absyn.ComponentRef cr;
DAE.Type ty;
DAE.Const c;
algorithm
Absyn.CREF(componentRef = cr) := inExp;
(outCache, SOME((outExp, outProperties, _))) := elabCref(inCache, inEnv, cr,
inImplicit, inDoVect, inPrefix, inInfo);
// BoschRexroth specifics, convert param to var.
if not Flags.getConfigBool(Flags.CEVAL_EQUATION) then
DAE.PROP(ty, c) := outProperties;
outProperties := if Types.isParameter(c) then
DAE.PROP(ty, DAE.C_VAR()) else outProperties;
end if;
end elabExp_Cref;
protected function elabExp_Binary
extends PartialElabExpFunc;
protected
Absyn.Exp e1, e2;
Absyn.Operator op;
DAE.Properties prop1, prop2;
DAE.Exp exp1, exp2;
algorithm
_ := match(inExp)
case Absyn.BINARY(exp1 = e1, op = op, exp2 = e2) then ();
case Absyn.LBINARY(exp1 = e1, op = op, exp2 = e2) then ();
case Absyn.RELATION(exp1 = e1, op = op, exp2 = e2) then ();
end match;
(outCache, exp1, prop1) := elabExpInExpression(inCache, inEnv,
e1, inImplicit, inDoVect, inPrefix, inInfo);
(outCache, exp2, prop2) := elabExpInExpression(outCache, inEnv,
e2, inImplicit, inDoVect, inPrefix, inInfo);
(outCache, outExp, outProperties) := OperatorOverloading.binary(outCache,
inEnv, op, prop1, exp1, prop2, exp2, inExp, e1, e2, inImplicit, inPrefix, inInfo);
end elabExp_Binary;
protected function elabExp_Unary
extends PartialElabExpFunc;
protected
Absyn.Exp e;
Absyn.Operator op;
DAE.Type ty;
DAE.Const c;
algorithm
Absyn.UNARY(op = op, exp = e) := inExp;
(outCache, outExp, outProperties as DAE.PROP(ty, c)) :=
elabExpInExpression(inCache, inEnv, e, inImplicit, inDoVect, inPrefix, inInfo);
if not (valueEq(op, Absyn.UPLUS()) and
Types.isIntegerOrRealOrSubTypeOfEither(Types.arrayElementType(ty)))
then
(outCache, outExp, outProperties) := OperatorOverloading.unary(outCache, inEnv,
op, outProperties, outExp, inExp, e, inImplicit, inPrefix, inInfo);
end if;
end elabExp_Unary;
protected function elabExp_LUnary
extends PartialElabExpFunc;
protected
Absyn.Exp e;
Absyn.Operator op;
algorithm
Absyn.LUNARY(op = op, exp = e) := inExp;
(outCache, outExp, outProperties) := elabExpInExpression(outCache, inEnv, e,
inImplicit, inDoVect, inPrefix, inInfo);
(outCache, outExp, outProperties) := OperatorOverloading.unary(outCache,
inEnv, op, outProperties, outExp, inExp, e, inImplicit, inPrefix, inInfo);
end elabExp_LUnary;
protected function elabExp_If
"Elaborates an if-expression. If one of the branches can not be elaborated and
the condition is parameter or constant; it is evaluated and the correct branch is selected.
This is a dirty hack to make MSL CombiTable models work!
Note: Because of this, the function has to rollback or delete an ErrorExt checkpoint."
extends PartialElabExpFunc;
protected
Absyn.Exp cond_e, true_e, false_e;
DAE.Exp cond_exp, true_exp, false_exp;
DAE.Properties cond_prop, true_prop, false_prop;
FCore.Cache cache;
Boolean b;
algorithm
Absyn.IFEXP(ifExp = cond_e, trueBranch = true_e, elseBranch = false_e) :=
AbsynUtil.canonIfExp(inExp);
(cache, cond_exp, cond_prop) := elabExpInExpression(inCache,
inEnv, cond_e, inImplicit, inDoVect, inPrefix, inInfo);
_ := matchcontinue()
case ()
algorithm
ErrorExt.setCheckpoint("Static.elabExp:IFEXP");
(outCache, true_exp, true_prop) := elabExpInExpression(cache,
inEnv, true_e, inImplicit, inDoVect, inPrefix, inInfo);
(outCache, false_exp, false_prop) := elabExpInExpression(outCache,
inEnv, false_e, inImplicit, inDoVect, inPrefix, inInfo);
(outCache, outExp, outProperties) := makeIfExp(outCache, inEnv, cond_exp,
cond_prop, true_exp, true_prop, false_exp, false_prop, inImplicit,
inPrefix, inInfo);
ErrorExt.delCheckpoint("Static.elabExp:IFEXP");
then
();
case ()
algorithm
ErrorExt.setCheckpoint("Static.elabExp:IFEXP:HACK") "Extra rollback point so we get the regular error message only once if the hack fails";
true := Types.isParameterOrConstant(Types.propAllConst(cond_prop));
(outCache, Values.BOOL(b)) := Ceval.ceval(cache, inEnv, cond_exp,
inImplicit, Absyn.MSG(inInfo));
(outCache, outExp, outProperties) := elabExpInExpression(outCache, inEnv,
if b then true_e else false_e, inImplicit, inDoVect, inPrefix, inInfo);
ErrorExt.delCheckpoint("Static.elabExp:IFEXP:HACK");
ErrorExt.rollBack("Static.elabExp:IFEXP");
then
();
else
algorithm
ErrorExt.rollBack("Static.elabExp:IFEXP:HACK");
ErrorExt.delCheckpoint("Static.elabExp:IFEXP");
then
fail();
end matchcontinue;
end elabExp_If;
protected function elabExp_Call
extends PartialElabExpFunc;
protected
Absyn.ComponentRef func_name;
Absyn.FunctionArgs args;
Absyn.Exp arg;
String last_id;
list<Absyn.Path> type_vars;
algorithm
Absyn.CALL(function_ = func_name, functionArgs = args, typeVars = type_vars) := inExp;
_ := match(args)
case Absyn.FUNCTIONARGS()
algorithm
(outCache, outExp, outProperties) := elabCall(inCache, inEnv,
func_name, args.args, args.argNames, type_vars, inImplicit, inPrefix, inInfo);
outExp := ExpressionSimplify.simplify1(outExp);
then
();
case Absyn.FOR_ITER_FARG()
algorithm
(outCache, outExp, outProperties) := elabCallReduction(inCache,
inEnv, func_name, args.exp, args.iterType, args.iterators, inImplicit,
inDoVect, inPrefix, inInfo);
then
();
end match;
end elabExp_Call;
protected function elabExp_Dot
extends PartialElabExpFunc;
algorithm
(outExp, outProperties) := match(inExp)
local
String s;
DAE.Type ty;
case Absyn.DOT()
algorithm
s := match inExp.index
case Absyn.CREF(Absyn.CREF_IDENT(name=s)) then s;
else
algorithm
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator is only allowed when indexing using a single simple name, got: " + Dump.printExpStr(inExp.index)}, inInfo);
then fail();
end match;
(outCache,outExp,outProperties) := elabExp(inCache,inEnv,inExp.exp,inImplicit,inDoVect, inPrefix, inInfo);
ty := Types.getPropType(outProperties);
_ := match ty
local
list<String> names;
Integer i;
case DAE.T_TUPLE(names=SOME(names))
algorithm
if not listMember(s, names) then
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator could not find " + s + " in " + Types.unparseType(ty)}, inInfo);
fail();
end if;
i := List.position(s, names);
outExp := DAE.TSUB(outExp, i, listGet(ty.types,i));
outProperties := DAE.PROP(listGet(ty.types,i), Types.propAllConst(outProperties));
then ();
else
algorithm
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator is only allowed when the expression returns a named tuple. Got expression: " + ExpressionDump.printExpStr(outExp) + " with type " + Types.unparseType(ty)}, inInfo);
then fail();
end match;
then (outExp, outProperties);
end match;
end elabExp_Dot;
protected function elabExp_PartEvalFunction
"turns an Absyn.PARTEVALFUNCTION into an DAE.PARTEVALFUNCTION"
extends PartialElabExpFunc;
protected
Absyn.ComponentRef cref;
list<Absyn.Exp> pos_args;
list<Absyn.NamedArg> named_args;
Absyn.Path path;
DAE.Type ty, tty, tty2;
list<DAE.Exp> args;
list<DAE.Const> consts;
list<Slot> slots;
DAE.Const c;
algorithm
Absyn.PARTEVALFUNCTION(cref, Absyn.FUNCTIONARGS(pos_args, named_args)) := inExp;
if listEmpty(pos_args) and listEmpty(named_args) then
(outCache, outExp, outProperties) := elabExpInExpression(inCache,
inEnv, Absyn.CREF(cref), inImplicit, inDoVect, inPrefix, inInfo);
else
path := AbsynUtil.crefToPath(cref);
(outCache, {tty}) := Lookup.lookupFunctionsInEnv(inCache, inEnv, path, inInfo);
tty := Types.makeFunctionPolymorphicReference(tty);
(outCache, args, consts, _, tty, _, slots) := elabTypes(outCache, inEnv, pos_args,
named_args, {}, {tty}, true, true, inImplicit,
inPrefix, inInfo);
if not Types.isFunctionPointer(tty) then
(outCache, path) := Inst.makeFullyQualified(outCache, inEnv, path);
(outCache, Util.SUCCESS()) := instantiateDaeFunction(outCache, inEnv,
path, false, NONE(), true);
end if;
tty2 := stripExtraArgsFromType(slots, tty);
tty2 := Types.makeFunctionPolymorphicReference(tty2);
ty := Types.simplifyType(tty2);
tty := Types.simplifyType(tty);
c := List.fold(consts, Types.constAnd, DAE.C_CONST());
outExp := DAE.PARTEVALFUNCTION(path, args, ty, tty);
outProperties := DAE.PROP(tty2, c);
end if;
end elabExp_PartEvalFunction;
protected function elabExp_Tuple
extends PartialElabExpFunc;
algorithm
(outCache, outExp, outProperties) := elabExp_Tuple_LHS_RHS(inCache, inEnv, inExp, inImplicit, inDoVect, inPrefix, inInfo);
end elabExp_Tuple;
protected function elabExp_Tuple_LHS_RHS
extends PartialElabExpFunc;
input Boolean isLhs=false;
protected
list<Absyn.Exp> el;
list<DAE.Exp> expl;
list<DAE.Properties> props;
list<DAE.Type> types;
list<DAE.TupleConst> consts;
algorithm
Absyn.TUPLE(expressions = el) := inExp;
(outCache, expl, props) := elabTuple(outCache, inEnv, el, inImplicit,
inDoVect, inPrefix, inInfo, isLhs);
(types, consts) := splitProps(props);
(outExp, outProperties) := fixTupleMetaModelica(expl, types, consts);
end elabExp_Tuple_LHS_RHS;
public function elabExpLHS "Special check for tuples, which only occur on the LHS"
extends PartialElabExpFunc;
algorithm
(outCache, outExp, outProperties) := match inExp
case Absyn.TUPLE()
algorithm
(outCache, outExp, outProperties) := elabExp_Tuple_LHS_RHS(inCache, inEnv, inExp, inImplicit, inDoVect, inPrefix, inInfo, isLhs=true);
then (outCache, outExp, outProperties);
else
algorithm
(outCache, outExp, outProperties) := elabExp(inCache, inEnv, inExp, inImplicit, inDoVect, inPrefix, inInfo);
then (outCache, outExp, outProperties);
end match;
end elabExpLHS;
protected function elabExp_Range
"Elaborates a range expression on the form start:stop or start:step:stop."
extends PartialElabExpFunc;
protected
Absyn.Exp start, step, stop;
Option<Absyn.Exp> ostep;
DAE.Exp start_exp, step_exp, stop_exp;
Option<DAE.Exp> ostep_exp = NONE();
DAE.Type start_ty, step_ty, stop_ty, ety, ty;
Option<DAE.Type> ostep_ty = NONE();
DAE.Const start_c, step_c, stop_c, c;
algorithm
Absyn.RANGE(start = start, step = ostep, stop = stop) := inExp;
// Elaborate start and stop of the range.
(outCache, start_exp, DAE.PROP(start_ty, start_c)) :=
elabExpInExpression(inCache, inEnv, start, inImplicit, inDoVect, inPrefix, inInfo);
(outCache, stop_exp, DAE.PROP(stop_ty, stop_c)) :=
elabExpInExpression(outCache, inEnv, stop, inImplicit, inDoVect, inPrefix, inInfo);
c := Types.constAnd(start_c, stop_c);
// If step was given, elaborate it too.
if isSome(ostep) then
SOME(step) := ostep;
(outCache, step_exp, DAE.PROP(step_ty, step_c)) :=
elabExpInExpression(outCache, inEnv, step, inImplicit, inDoVect, inPrefix, inInfo);
ostep_exp := SOME(step_exp);
ostep_ty := SOME(step_ty);
c := Types.constAnd(c, step_c);
end if;
if Types.isBoxedType(start_ty) then
(start_exp, start_ty) := Types.matchType(start_exp, start_ty, Types.unboxedType(start_ty), true);
end if;
if Types.isBoxedType(stop_ty) then
(stop_exp, stop_ty) := Types.matchType(stop_exp, stop_ty, Types.unboxedType(stop_ty), true);
end if;
(start_exp, ostep_exp, stop_exp, ety) :=
deoverloadRange(start_exp, start_ty, ostep_exp, ostep_ty, stop_exp, stop_ty, inInfo);
(outCache, ty) := elabRangeType(outCache, inEnv, start_exp, ostep_exp,
stop_exp, start_ty, ety, c, inImplicit);
outExp := DAE.RANGE(ty, start_exp, ostep_exp, stop_exp);
outProperties := DAE.PROP(ty, c);
end elabExp_Range;
protected function elabExp_Array
extends PartialElabExpFunc;
protected
list<Absyn.Exp> es;
list<DAE.Exp> expl;
list<DAE.Properties> props;
DAE.Type ty, arr_ty;
DAE.Const c;
DAE.Exp exp;
algorithm
(outExp, outProperties) := matchcontinue(inExp)
// Part of the MetaModelica extension. This eliminates elabArray failed
// failtraces when using the empty list. sjoelund
case Absyn.ARRAY({}) guard(Config.acceptMetaModelicaGrammar())
then (DAE.LIST({}), DAE.PROP(DAE.T_METALIST_DEFAULT, DAE.C_CONST()));
// array expressions, e.g. {1,2,3}
case Absyn.ARRAY(arrayExp = es)
algorithm
(outCache, expl, props) := elabExpList(inCache, inEnv, es, inImplicit,
inDoVect, inPrefix, inInfo);
(expl, DAE.PROP(ty, c)) := elabArray(expl, props, inPrefix, inInfo); // type-checking the array
arr_ty := DAE.T_ARRAY(ty, {DAE.DIM_INTEGER(listLength(expl))});
exp := DAE.ARRAY(Types.simplifyType(arr_ty), not Types.isArray(ty), expl);
InstMeta.checkArrayType(ty);
exp := elabMatrixToMatrixExp(exp);
then
(exp, DAE.PROP(arr_ty, c));
// Part of the MetaModelica extension. KS
case Absyn.ARRAY(arrayExp = es) guard(Config.acceptMetaModelicaGrammar())
algorithm
(outCache, outExp, outProperties) := elabExpInExpression(inCache,
inEnv, Absyn.LIST(es), inImplicit, inDoVect, inPrefix, inInfo);
then
(outExp, outProperties);
end matchcontinue;
end elabExp_Array;
protected function elabExp_Matrix
extends PartialElabExpFunc;
protected
list<list<Absyn.Exp>> ess;
list<list<DAE.Exp>> dess, dess2;
list<list<DAE.Properties>> props;
list<list<DAE.Type>> tps;
list<DAE.Type> tys, tys2;
Integer nmax;
Boolean have_real;
DAE.Type ty;
DAE.Const c;
DAE.Dimension dim1, dim2;
list<DAE.Exp> expl;
algorithm
// Elaborate the individual expressions.
Absyn.MATRIX(matrix = ess) := inExp;
(outCache, dess, props) := elabExpListList(inCache, inEnv, ess, inImplicit,
inDoVect, inPrefix, inInfo);
// Check if any of the expressions is of Real type.
tys := listAppend(list(Types.getPropType(p) for p in pl) for pl in props);
nmax := matrixConstrMaxDim(tys);
have_real := Types.containReal(tys);
// If we have any Real expressions, cast any Integer expressions to Real.
if have_real then
(dess, props) := List.threadMapList_2(dess, props, elabExp_Matrix_realCast);
end if;
(outCache, outExp, DAE.PROP(ty, c), dim1, dim2) := elabMatrixSemi(outCache,
inEnv, dess, props, inImplicit, have_real, nmax, inDoVect, inPrefix, inInfo);
outExp := elabMatrixToMatrixExp(outExp);
ty := Types.unliftArray(Types.unliftArray(ty)); // All elts promoted to matrix, therefore unlifting.
ty := DAE.T_ARRAY(ty, {dim2});
ty := DAE.T_ARRAY(ty, {dim1});
outProperties := DAE.PROP(ty, c);
end elabExp_Matrix;
protected function elabExp_Matrix_realCast
"Casts an expression and property to Real if it's current type is Integer."
input DAE.Exp inExp;
input DAE.Properties inProperties;
output DAE.Exp outExp;
output DAE.Properties outProperties;
protected
DAE.Type ty;
algorithm
ty := Types.getPropType(inProperties);
if Types.isInteger(ty) then
ty := Types.setArrayElementType(ty, DAE.T_REAL_DEFAULT);
outProperties := Types.setPropType(inProperties, ty);
ty := Types.simplifyType(ty);
outExp := ExpressionSimplify.simplify1(DAE.CAST(ty, inExp));
else
outExp := inExp;
outProperties := inProperties;
end if;
end elabExp_Matrix_realCast;
protected function elabExp_Code
extends PartialElabExpFunc;
protected
DAE.Type ty, ty2;
Absyn.CodeNode cn;
algorithm
Absyn.CODE(code = cn) := inExp;
ty := elabCodeType(cn);
ty2 := Types.simplifyType(ty);
outExp := DAE.CODE(cn, ty2);
outProperties := DAE.PROP(ty, DAE.C_CONST());
end elabExp_Code;
protected function elabExp_Cons
extends PartialElabExpFunc;
protected
Absyn.Exp e1, e2;
DAE.Exp exp1, exp2;
DAE.Properties prop1;
DAE.Type ty, ty1, ty2;
DAE.Const c1, c2;
String exp_str, ty1_str, ty2_str;
algorithm
Absyn.CONS(e1, e2) := inExp;
{e1, e2} := MetaUtil.transformArrayNodesToListNodes({e1, e2});
// Elaborate both sides of the cons expression.
(outCache, exp1, prop1) := elabExpInExpression(outCache, inEnv, e1,
inImplicit, inDoVect, inPrefix, inInfo);
(outCache, exp2, DAE.PROP(DAE.T_METALIST(ty = ty2), c2)) := elabExpInExpression(
outCache, inEnv, e2, inImplicit, inDoVect, inPrefix, inInfo);
try
// Replace all metarecords with uniontypes with.
ty1 := Types.getUniontypeIfMetarecordReplaceAllSubtypes(Types.getPropType(prop1));
ty2 := Types.getUniontypeIfMetarecordReplaceAllSubtypes(ty2);
c1 := Types.propAllConst(prop1);
ty := Types.getUniontypeIfMetarecordReplaceAllSubtypes(
Types.superType(Types.boxIfUnboxedType(ty1), Types.boxIfUnboxedType(ty2)));
// Make sure the operands have correct types.
exp1 := Types.matchType(exp1, ty1, ty, true);
ty := DAE.T_METALIST(ty);
exp2 := Types.matchType(exp2, ty, DAE.T_METALIST(ty2), true);
outExp := DAE.CONS(exp1, exp2);
outProperties := DAE.PROP(ty, Types.constAnd(c1, c2));
else
exp_str := Dump.printExpStr(inExp);
ty1_str := Types.unparseType(Types.getPropType(prop1));
ty2_str := Types.unparseType(ty2);
Error.addSourceMessage(Error.META_CONS_TYPE_MATCH, {exp_str, ty1_str, ty2_str}, inInfo);
fail();
end try;
end elabExp_Cons;
protected function elabExp_List
extends PartialElabExpFunc;
protected
list<Absyn.Exp> es;
list<DAE.Exp> expl;
list<DAE.Properties> props;
list<DAE.Type> types;
list<DAE.Const> consts;
DAE.Const c;
DAE.Type ty;
algorithm
Absyn.LIST(exps = es) := inExp;
// The Absyn.LIST() node is used for list expressions that are transformed
// from Absyn.ARRAY()
if listEmpty(es) then
outExp := DAE.LIST({});
outProperties := DAE.PROP(DAE.T_METALIST_DEFAULT, DAE.C_CONST());
else
(outCache, expl, props) := elabExpList(inCache, inEnv, es, inImplicit,
inDoVect, inPrefix, inInfo);
types := list(Types.getPropType(p) for p in props);
consts := Types.getConstList(props);
c := List.fold(consts, Types.constAnd, DAE.C_CONST());
ty := Types.boxIfUnboxedType(List.reduce(types, Types.superType));
expl := Types.matchTypes(expl, types, ty, true);
outExp := DAE.LIST(expl);
outProperties := DAE.PROP(DAE.T_METALIST(ty), c);
end if;
end elabExp_List;
public function elabExpInExpression "Like elabExp but casts PROP_TUPLE to a PROP"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Exp inExp;
input Boolean inImplicit;
input Boolean performVectorization;
input DAE.Prefix inPrefix;
input SourceInfo info;
output FCore.Cache outCache;
output DAE.Exp outExp;
output DAE.Properties outProperties;
algorithm
(outCache,outExp,outProperties) := elabExp(inCache,inEnv,inExp,inImplicit,performVectorization,inPrefix,info);
(outExp,outProperties) := elabExpInExpression2(outExp,outProperties);
end elabExpInExpression;
protected function elabExpInExpression2
input DAE.Exp inExp;
input DAE.Properties inProperties;
output DAE.Exp outExp;
output DAE.Properties outProperties;
algorithm
(outExp,outProperties) := match (inExp,inProperties)
local
DAE.Type ty;
DAE.Const c;
case (_,DAE.PROP_TUPLE(type_ = DAE.T_TUPLE(types = ty :: _), tupleConst = DAE.TUPLE_CONST(tupleConstLst = DAE.SINGLE_CONST(const = c) :: _)))
then (DAE.TSUB(inExp, 1, ty), DAE.PROP(ty,c));
else (inExp,inProperties);
end match;
end elabExpInExpression2;
public function checkAssignmentToInput
input Absyn.Exp inExp;
input DAE.Attributes inAttributes;
input FCore.Graph inEnv;
input Boolean inAllowTopLevelInputs;
input SourceInfo inInfo;
algorithm
// If we don't allow top level inputs and we're in a function scope and not
// using parmodelica, check for assignment to input.
if not inAllowTopLevelInputs and FGraph.inFunctionScope(inEnv) and
not Config.acceptParModelicaGrammar() then
checkAssignmentToInput2(inExp, inAttributes, inInfo);
end if;
end checkAssignmentToInput;
protected function checkAssignmentToInput2
input Absyn.Exp inExp;
input DAE.Attributes inAttributes;
input SourceInfo inInfo;
algorithm
_ := match(inExp, inAttributes, inInfo)
local
Absyn.ComponentRef cr;
String cr_str;
case (Absyn.CREF(cr), DAE.ATTR(direction = Absyn.INPUT()), _)
equation
cr_str = Dump.printComponentRefStr(cr);
Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR,
{"input", cr_str}, inInfo);
then
fail();
else ();
end match;
end checkAssignmentToInput2;
public function checkAssignmentToInputs
input list<Absyn.Exp> inExpCrefs;
input list<DAE.Attributes> inAttributes;
input FCore.Graph inEnv;
input SourceInfo inInfo;
algorithm
if FGraph.inFunctionScope(inEnv) then
List.threadMap1_0(inExpCrefs, inAttributes, checkAssignmentToInput2, inInfo);
end if;
end checkAssignmentToInputs;
public function elabExpCrefNoEvalList
"elaborates a list of expressions that are only component references."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input list<Absyn.Exp> inExpl;
input Boolean inImplicit;
input Boolean inDoVect;
input DAE.Prefix inPrefix;
input SourceInfo inInfo;
output FCore.Cache outCache = inCache;
output list<DAE.Exp> outExpl = {};
output list<DAE.Properties> outProperties = {};
output list<DAE.Attributes> outAttributes = {};
protected
Integer num_err = Error.getNumErrorMessages();
DAE.Exp exp;
DAE.Properties prop;
list<DAE.Properties> props = {};
DAE.Attributes attr;