/
NFCeval.mo
3365 lines (2971 loc) · 99.7 KB
/
NFCeval.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 NFCeval
import NFBinding.Binding;
import ComponentRef = NFComponentRef;
import Error;
import NFComponent.Component;
import Expression = NFExpression;
import NFInstNode.InstNode;
import Operator = NFOperator;
import NFOperator.Op;
import Typing = NFTyping;
import NFCall.Call;
import Dimension = NFDimension;
import Type = NFType;
import NFTyping.ExpOrigin;
import ExpressionSimplify;
import NFPrefixes.Variability;
import NFClassTree.ClassTree;
import ComplexType = NFComplexType;
import Subscript = NFSubscript;
import NFTyping.TypingError;
import DAE;
protected
import NFFunction.Function;
import EvalFunction = NFEvalFunction;
import List;
import System;
import ExpressionIterator = NFExpressionIterator;
import MetaModelica.Dangerous.*;
import NFClass.Class;
import TypeCheck = NFTypeCheck;
import ExpandExp = NFExpandExp;
import ElementSource;
import Flags;
public
uniontype EvalTarget
record DIMENSION
InstNode component;
Integer index;
Expression exp;
SourceInfo info;
end DIMENSION;
record ATTRIBUTE
Binding binding;
end ATTRIBUTE;
record RANGE
SourceInfo info;
end RANGE;
record CONDITION
SourceInfo info;
end CONDITION;
record GENERIC
SourceInfo info;
end GENERIC;
record STATEMENT
DAE.ElementSource source;
end STATEMENT;
record IGNORE_ERRORS end IGNORE_ERRORS;
function isRange
input EvalTarget target;
output Boolean isRange;
algorithm
isRange := match target
case RANGE() then true;
else false;
end match;
end isRange;
function hasInfo
input EvalTarget target;
output Boolean hasInfo;
algorithm
hasInfo := match target
case DIMENSION() then true;
case ATTRIBUTE() then true;
case RANGE() then true;
case CONDITION() then true;
case GENERIC() then true;
case STATEMENT() then true;
else false;
end match;
end hasInfo;
function getInfo
input EvalTarget target;
output SourceInfo info;
algorithm
info := match target
case DIMENSION() then target.info;
case ATTRIBUTE() then Binding.getInfo(target.binding);
case RANGE() then target.info;
case CONDITION() then target.info;
case GENERIC() then target.info;
case STATEMENT() then ElementSource.getInfo(target.source);
else AbsynUtil.dummyInfo;
end match;
end getInfo;
end EvalTarget;
function evalExp
input output Expression exp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
algorithm
exp := Expression.getBindingExp(evalExp_impl(exp, target));
end evalExp;
function evalExp_impl
input output Expression exp;
input EvalTarget target;
algorithm
exp := match exp
local
InstNode c;
Binding binding;
Expression exp1, exp2, exp3;
list<Expression> expl = {};
Call call;
Component comp;
Option<Expression> oexp;
ComponentRef cref;
Dimension dim;
case Expression.CREF()
then evalCref(exp.cref, exp, target);
case Expression.TYPENAME()
then evalTypename(exp.ty, exp, target);
case Expression.ARRAY()
then if exp.literal then exp
else
Expression.makeArray(exp.ty,
list(evalExp_impl(e, target) for e in exp.elements),
literal = true);
case Expression.RANGE() then evalRange(exp, target);
case Expression.TUPLE()
algorithm
exp.elements := list(evalExp_impl(e, target) for e in exp.elements);
then
exp;
case Expression.RECORD()
algorithm
exp.elements := list(evalExp_impl(e, target) for e in exp.elements);
then
exp;
case Expression.CALL()
then evalCall(exp.call, target);
case Expression.SIZE()
then evalSize(exp.exp, exp.dimIndex, target);
case Expression.BINARY()
algorithm
exp1 := evalExp_impl(exp.exp1, target);
exp2 := evalExp_impl(exp.exp2, target);
then
evalBinaryOp(exp1, exp.operator, exp2, target);
case Expression.UNARY()
algorithm
exp1 := evalExp_impl(exp.exp, target);
then
evalUnaryOp(exp1, exp.operator);
case Expression.LBINARY()
algorithm
exp1 := evalExp_impl(exp.exp1, target);
then
evalLogicBinaryOp(exp1, exp.operator, exp.exp2, target);
case Expression.LUNARY()
algorithm
exp1 := evalExp_impl(exp.exp, target);
then
evalLogicUnaryOp(exp1, exp.operator);
case Expression.RELATION()
algorithm
exp1 := evalExp_impl(exp.exp1, target);
exp2 := evalExp_impl(exp.exp2, target);
then
evalRelationOp(exp1, exp.operator, exp2);
case Expression.IF() then evalIfExp(exp, target);
case Expression.CAST()
algorithm
exp1 := evalExp_impl(exp.exp, target);
then
evalCast(exp1, exp.ty);
case Expression.UNBOX()
algorithm
exp1 := evalExp_impl(exp.exp, target);
then Expression.UNBOX(exp1, exp.ty);
case Expression.SUBSCRIPTED_EXP()
then evalSubscriptedExp(exp.exp, exp.subscripts, target);
case Expression.TUPLE_ELEMENT()
algorithm
exp1 := evalExp_impl(exp.tupleExp, target);
then
Expression.tupleElement(exp1, exp.ty, exp.index);
case Expression.RECORD_ELEMENT()
then evalRecordElement(exp, target);
case Expression.MUTABLE()
algorithm
exp1 := evalExp_impl(Mutable.access(exp.exp), target);
then
exp1;
case Expression.BINDING_EXP()
algorithm
exp.exp := evalExp_impl(exp.exp, target);
then
exp;
else exp;
end match;
end evalExp_impl;
function evalExpOpt
input output Option<Expression> oexp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
algorithm
oexp := match oexp
local
Expression e;
case SOME(e) then SOME(evalExp_impl(e, target));
else oexp;
end match;
end evalExpOpt;
function evalExpPartial
"Evaluates the parts of an expression that are possible to evaluate. This
means leaving parts of the expression that contains e.g. iterators or mutable
expressions. This can be used to optimize an expression that is expected to
be evaluated many times, for example the expression in an array constructor."
input Expression exp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
input Boolean evaluated = true;
output Expression outExp;
output Boolean outEvaluated "True if the whole expression is evaluated, otherwise false.";
protected
Expression e, e1, e2;
Boolean eval1, eval2;
algorithm
(e, outEvaluated) :=
Expression.mapFoldShallow(exp, function evalExpPartial(target = target), true);
outExp := match e
case Expression.CREF()
algorithm
if ComponentRef.isIterator(e.cref) then
// Don't evaluate iterators.
outExp := e;
outEvaluated := false;
else
// Crefs can be evaluated even if they have non-evaluated subscripts.
outExp := evalCref(e.cref, e, target, evalSubscripts = false);
end if;
then
outExp;
// Don't evaluate mutable expressions. While they could technically be
// evaluated they're usually used as mutable iterators.
case Expression.MUTABLE()
algorithm
outEvaluated := false;
then
e;
else if outEvaluated then evalExp(e, target) else e;
end match;
outEvaluated := evaluated and outEvaluated;
end evalExpPartial;
function evalCref
input ComponentRef cref;
input Expression defaultExp;
input EvalTarget target;
input Boolean evalSubscripts = true;
output Expression exp;
protected
InstNode c;
Boolean evaled;
list<Subscript> subs;
algorithm
exp := match cref
case ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE())
guard not ComponentRef.isIterator(cref)
then evalComponentBinding(c, cref, defaultExp, target, evalSubscripts);
else defaultExp;
end match;
end evalCref;
function evalComponentBinding
input InstNode node;
input ComponentRef cref;
input Expression defaultExp "The expression returned if the binding couldn't be evaluated";
input EvalTarget target;
input Boolean evalSubscripts = true;
output Expression exp;
protected
ExpOrigin.Type exp_origin;
Component comp;
Binding binding;
Boolean evaluated;
list<Subscript> subs;
Variability var;
Option<Expression> start_exp;
algorithm
exp_origin := if InstNode.isFunction(InstNode.explicitParent(node))
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;
Typing.typeComponentBinding(node, exp_origin, typeChildren = false);
comp := InstNode.component(node);
binding := Component.getBinding(comp);
if Binding.isUnbound(binding) then
// In some cases we need to construct a binding for the node, for example when
// a record has bindings on the fields but not on the record instance as a whole.
binding := makeComponentBinding(comp, node, Expression.toCref(defaultExp), target);
if Binding.isUnbound(binding) then
// If we couldn't construct a binding, try to use the start value instead.
start_exp := evalComponentStartBinding(node, comp, cref, target, evalSubscripts);
if isSome(start_exp) then
// The component had a valid start value. The value has already been
// evaluated by evalComponentStartBinding, so skip the rest of the function.
SOME(exp) := start_exp;
return;
end if;
end if;
end if;
(exp, evaluated) := match binding
case Binding.TYPED_BINDING()
algorithm
if binding.evaluated then
exp := binding.bindingExp;
else
exp := evalExp_impl(binding.bindingExp, target);
binding.bindingExp := exp;
binding.evaluated := true;
comp := Component.setBinding(binding, comp);
InstNode.updateComponent(comp, node);
end if;
then
(exp, true);
case Binding.CEVAL_BINDING() then (binding.bindingExp, true);
case Binding.UNBOUND()
algorithm
printUnboundError(comp, target, defaultExp);
then
(defaultExp, false);
else
algorithm
Error.addInternalError(getInstanceName() + " failed on untyped binding", sourceInfo());
then
fail();
end match;
// Apply subscripts from the cref to the binding expression as needed.
if evaluated then
exp := subscriptEvaluatedBinding(exp, cref, evalSubscripts);
end if;
end evalComponentBinding;
function flattenBindingExp
input Expression exp;
output Expression outExp;
algorithm
outExp := match exp
case Expression.BINDING_EXP(exp = Expression.BINDING_EXP())
then flattenBindingExp(exp.exp);
else exp;
end match;
end flattenBindingExp;
function subscriptEvaluatedBinding
"Takes subscripts from the given component reference and applies them to an
evaluated expression."
input output Expression exp;
input ComponentRef cref;
input Boolean evalSubscripts;
protected
list<Subscript> subs;
ComponentRef cr;
algorithm
// The subscripts of the first part of the cref are always applied.
subs := ComponentRef.getSubscripts(cref);
cr := ComponentRef.stripSubscripts(cref);
if evalSubscripts then
subs := listReverse(Subscript.eval(s) for s in subs);
else
subs := listReverse(subs);
end if;
// The rest of the cref contributes subscripts based on where the expressions
// comes from in the instance tree.
exp := subscriptEvaluatedBinding2(exp, cr, evalSubscripts, subs, subs);
end subscriptEvaluatedBinding;
function subscriptEvaluatedBinding2
input output Expression exp;
input ComponentRef cref;
input Boolean evalSubscripts;
input list<Subscript> subscripts = {};
input list<Subscript> bindingSubs = {};
algorithm
exp := match exp
local
Expression e;
Type exp_ty, bind_ty;
list<InstNode> parents;
list<Subscript> accum_subs, subs;
ComponentRef cr;
InstNode cr_node;
case Expression.BINDING_EXP(bindingType = bind_ty, parents = parents)
algorithm
if exp.isEach then
parents := {listHead(parents)};
end if;
cr := cref;
accum_subs := subscripts;
subs := {};
if not ComponentRef.isEmpty(cr) then
cr_node := ComponentRef.node(cr);
// Remove binding parents until we find one referring to the first
// cref node, or we run out of parents.
while not (listEmpty(parents) or InstNode.refEqual(listHead(parents), cr_node)) loop
parents := listRest(parents);
end while;
// Collect subscripts from the part of the cref corresponding to the
// remaining parents.
while not listEmpty(parents) loop
if not InstNode.refEqual(listHead(parents), cr_node) then
break;
end if;
subs := List.append_reverse(ComponentRef.getSubscripts(cr), subs);
parents := listRest(parents);
cr := ComponentRef.rest(cr);
if ComponentRef.isEmpty(cr) then
break;
end if;
cr_node := ComponentRef.node(cr);
end while;
if evalSubscripts then
subs := listReverse(Subscript.eval(s) for s in subs);
else
subs := listReverse(subs);
end if;
accum_subs := List.append_reverse(subs, accum_subs);
end if;
// Subscript the binding type if bindingSubs was given.
if not listEmpty(bindingSubs) then
subs := bindingSubs;
bind_ty := Type.subscript(bind_ty, subs);
end if;
e := subscriptEvaluatedBinding2(exp.exp, cr, evalSubscripts, accum_subs, subs);
exp_ty := Expression.typeOf(e);
then
Expression.BINDING_EXP(e, exp_ty, bind_ty, exp.parents, exp.isEach);
else
algorithm
subs := listReverse(subscripts);
then
Expression.applySubscripts(subs, exp);
end match;
end subscriptEvaluatedBinding2;
function evalComponentStartBinding
"Tries to evaluate the given component's start value. NONE() is returned if
the component isn't a fixed parameter or if it doesn't have a start value.
Otherwise the evaluated binding expression is returned if it could be
evaluated, or the function will fail if it couldn't be."
input InstNode node;
input Component comp;
input ComponentRef cref;
input EvalTarget target;
input Boolean evalSubscripts;
output Option<Expression> outExp = NONE();
protected
Variability var;
InstNode start_node;
Component start_comp;
Binding binding;
Expression exp;
list<Subscript> subs;
Integer pcount;
algorithm
// Only use the start value if the component is a fixed parameter.
var := Component.variability(comp);
if (var <> Variability.PARAMETER and var <> Variability.STRUCTURAL_PARAMETER) or
not Component.getFixedAttribute(comp) then
return;
end if;
// Look up "start" in the class.
try
start_node := Class.lookupElement("start", InstNode.getClass(node));
else
return;
end try;
// Make sure we have an actual start attribute, and didn't just find some
// other element named start in the class.
start_comp := InstNode.component(start_node);
if not Component.isTypeAttribute(start_comp) then
return;
end if;
// Try to evaluate the binding if one exists.
binding := Component.getBinding(start_comp);
outExp := match binding
case Binding.TYPED_BINDING()
algorithm
exp := evalExp_impl(binding.bindingExp, target);
if not referenceEq(exp, binding.bindingExp) then
binding.bindingExp := exp;
start_comp := Component.setBinding(binding, start_comp);
InstNode.updateComponent(start_comp, start_node);
end if;
then
SOME(exp);
else outExp;
end match;
end evalComponentStartBinding;
function makeComponentBinding
input Component component;
input InstNode node;
input ComponentRef cref;
input EvalTarget target;
output Binding binding;
protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
Type ty, exp_ty;
InstNode rec_node;
Expression exp;
ComponentRef rest_cr;
algorithm
binding := matchcontinue (component, cref)
// A record component without an explicit binding, create one from its children.
case (Component.TYPED_COMPONENT(ty = Type.COMPLEX(complexTy = ComplexType.RECORD(rec_node))), _)
algorithm
exp := makeRecordBindingExp(component.classInst, rec_node, component.ty, cref);
exp_ty := Expression.typeOf(exp);
exp := Expression.BINDING_EXP(exp, exp_ty, exp_ty, {node}, true);
binding := Binding.CEVAL_BINDING(exp);
if not ComponentRef.hasSubscripts(cref) then
InstNode.updateComponent(Component.setBinding(binding, component), node);
end if;
then
binding;
// A record array component without an explicit binding, create one from its children.
case (Component.TYPED_COMPONENT(ty = ty as Type.ARRAY(elementType =
Type.COMPLEX(complexTy = ComplexType.RECORD(rec_node)))), _)
algorithm
exp := makeRecordBindingExp(component.classInst, rec_node, component.ty, cref);
exp := splitRecordArrayExp(exp);
exp_ty := Expression.typeOf(exp);
exp := Expression.BINDING_EXP(exp, exp_ty, exp_ty, {node}, true);
binding := Binding.CEVAL_BINDING(exp);
if not ComponentRef.hasSubscripts(cref) then
InstNode.updateComponent(Component.setBinding(binding, component), node);
end if;
then
binding;
// A record field without an explicit binding, evaluate the parent's binding
// if it has one and fetch the binding from it instead.
case (_, ComponentRef.CREF(restCref = rest_cr as ComponentRef.CREF(ty = ty)))
guard Type.isRecord(Type.arrayElementType(ty))
algorithm
exp := evalCref(rest_cr, Expression.EMPTY(ty), target);
exp := Expression.bindingExpMap(exp, function makeComponentBinding2(name = InstNode.name(node)));
then
Binding.CEVAL_BINDING(exp);
else NFBinding.EMPTY_BINDING;
end matchcontinue;
end makeComponentBinding;
function makeComponentBinding2
input Expression exp;
input String name;
output Expression result;
algorithm
result := match exp
local
list<Expression> expl;
Type ty;
Dimension dim;
case Expression.RECORD() then Expression.lookupRecordField(name, exp);
// An empty array of records will still be empty, only the type needs to be changed.
case Expression.ARRAY(elements = {})
algorithm
exp.ty := Type.lookupRecordFieldType(name, exp.ty);
then
exp;
// For a non-empty array of records, look up the field in each record and
// create an array from them.
// TODO: Optimize this, the index of the field will be the same for each
// element of the array so we only need to do lookup once.
case Expression.ARRAY(ty = Type.ARRAY(dimensions = dim :: _))
algorithm
expl := list(makeComponentBinding2(e, name) for e in exp.elements);
ty := Type.liftArrayLeft(Expression.typeOf(listHead(expl)), dim);
then
Expression.makeArray(ty, expl, literal = true);
case Expression.SUBSCRIPTED_EXP()
then Expression.SUBSCRIPTED_EXP(makeComponentBinding2(exp.exp, name), exp.subscripts, exp.ty);
end match;
end makeComponentBinding2;
function makeRecordBindingExp
input InstNode typeNode;
input InstNode recordNode;
input Type recordType;
input ComponentRef cref;
output Expression exp;
protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
list<String> field_names;
Type ty;
InstNode c;
ComponentRef cr;
Expression field_exp;
algorithm
tree := Class.classTree(InstNode.getClass(typeNode));
comps := ClassTree.getComponents(tree);
fields := {};
field_names := {};
for i in arrayLength(comps):-1:1 loop
c := comps[i];
ty := InstNode.getType(c);
cr := ComponentRef.CREF(c, {}, ty, NFComponentRef.Origin.CREF, cref);
field_exp := Expression.CREF(ty, cr);
if Component.variability(InstNode.component(c)) <= Variability.PARAMETER then
field_exp := evalExp_impl(field_exp, EvalTarget.IGNORE_ERRORS());
end if;
fields := field_exp :: fields;
field_names := InstNode.name(c) :: field_names;
end for;
ty := Type.setRecordFields(field_names, recordType);
exp := Expression.RECORD(InstNode.scopePath(recordNode), ty, fields);
end makeRecordBindingExp;
function splitRecordArrayExp
input output Expression exp;
protected
Absyn.Path path;
Type ty;
list<Expression> expl;
algorithm
Expression.RECORD(path, ty, expl) := exp;
exp := Expression.RECORD(path, Type.arrayElementType(ty), expl);
exp := Expression.fillType(ty, exp);
end splitRecordArrayExp;
function evalTypename
input Type ty;
input Expression originExp;
input EvalTarget target;
output Expression exp;
algorithm
// Only expand the typename into an array if it's used as a range, and keep
// them as typenames when used as e.g. dimensions.
exp := if EvalTarget.isRange(target) then ExpandExp.expandTypename(ty) else originExp;
end evalTypename;
function evalRange
input Expression rangeExp;
input EvalTarget target;
output Expression result;
protected
Type ty;
Expression start_exp, stop_exp;
Option<Expression> step_exp;
Expression max_prop_exp;
Integer max_prop_count;
algorithm
Expression.RANGE(ty = ty, start = start_exp, step = step_exp, stop = stop_exp) := rangeExp;
start_exp := evalExp(start_exp, target);
step_exp := evalExpOpt(step_exp, target);
stop_exp := evalExp(stop_exp, target);
if EvalTarget.isRange(target) then
ty := TypeCheck.getRangeType(start_exp, step_exp, stop_exp,
Type.arrayElementType(ty), EvalTarget.getInfo(target));
result := Expression.RANGE(ty, start_exp, step_exp, stop_exp);
else
result := Expression.RANGE(ty, start_exp, step_exp, stop_exp);
result := Expression.bindingExpMap(result, evalRangeExp);
end if;
end evalRange;
function evalRangeExp
input Expression rangeExp;
output Expression exp;
protected
Expression start, step, stop;
Option<Expression> opt_step;
list<Expression> expl;
Type ty;
list<String> literals;
Integer istep;
algorithm
Expression.RANGE(start = start, step = opt_step, stop = stop) := rangeExp;
if isSome(opt_step) then
SOME(step) := opt_step;
(ty, expl) := match (start, step, stop)
case (Expression.INTEGER(), Expression.INTEGER(istep), Expression.INTEGER())
algorithm
// The compiler decided to randomly dislike using step.value here, hence istep.
expl := list(Expression.INTEGER(i) for i in start.value:istep:stop.value);
then
(Type.INTEGER(), expl);
case (Expression.REAL(), Expression.REAL(), Expression.REAL())
algorithm
expl := evalRangeReal(start.value, step.value, stop.value);
then
(Type.REAL(), expl);
else
algorithm
printWrongArgsError(getInstanceName(), {start, step, stop}, sourceInfo());
then
fail();
end match;
else
(ty, expl) := match (start, stop)
case (Expression.INTEGER(), Expression.INTEGER())
algorithm
expl := list(Expression.INTEGER(i) for i in start.value:stop.value);
then
(Type.INTEGER(), expl);
case (Expression.REAL(), Expression.REAL())
algorithm
expl := evalRangeReal(start.value, 1.0, stop.value);
then
(Type.REAL(), expl);
case (Expression.BOOLEAN(), Expression.BOOLEAN())
algorithm
expl := list(Expression.BOOLEAN(b) for b in start.value:stop.value);
then
(Type.BOOLEAN(), expl);
case (Expression.ENUM_LITERAL(ty = ty as Type.ENUMERATION()), Expression.ENUM_LITERAL())
algorithm
expl := list(Expression.ENUM_LITERAL(ty, listGet(ty.literals, i), i) for i in start.index:stop.index);
then
(ty, expl);
else
algorithm
printWrongArgsError(getInstanceName(), {start, stop}, sourceInfo());
then
fail();
end match;
end if;
exp := Expression.makeArray(Type.ARRAY(ty, {Dimension.fromInteger(listLength(expl))}),
expl, literal = true);
end evalRangeExp;
function evalRangeReal
input Real start;
input Real step;
input Real stop;
output list<Expression> result;
protected
Integer steps;
algorithm
steps := Util.realRangeSize(start, step, stop);
// Real ranges are tricky, make sure that start and stop are reproduced
// exactly if they are part of the range.
if steps == 0 then
result := {};
elseif steps == 1 then
result := {Expression.REAL(start)};
else
result := {Expression.REAL(stop)};
for i in steps-2:-1:1 loop
result := Expression.REAL(start + i * step) :: result;
end for;
result := Expression.REAL(start) :: result;
end if;
end evalRangeReal;
function printFailedEvalError
input String name;
input Expression exp;
input SourceInfo info;
algorithm
Error.addInternalError(name + " failed to evaluate ‘" + Expression.toString(exp) + "‘", info);
end printFailedEvalError;
function evalBinaryOp
input Expression exp1;
input Operator op;
input Expression exp2;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
output Expression exp;
protected
Expression max_prop_exp;
Integer max_prop_count;
algorithm
(max_prop_exp, max_prop_count) := Expression.mostPropagatedSubExpBinary(exp1, exp2);
if max_prop_count >= 0 then
exp := Expression.bindingExpMap2(Expression.BINARY(exp1, op, exp2),
function evalBinaryExp(target = target), max_prop_count, max_prop_exp);
else
exp := evalBinaryOp_dispatch(exp1, op, exp2, target);
end if;
end evalBinaryOp;
function evalBinaryExp
input Expression binaryExp;
input EvalTarget target;
output Expression result;
protected
Expression e1, e2;
Operator op;
algorithm
Expression.BINARY(exp1 = e1, operator = op, exp2 = e2) := binaryExp;
result := evalBinaryOp_dispatch(e1, op, e2, target);
end evalBinaryExp;
function evalBinaryOp_dispatch
input Expression exp1;
input Operator op;
input Expression exp2;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
output Expression exp;
algorithm
exp := match op.op
case Op.ADD then evalBinaryAdd(exp1, exp2);
case Op.SUB then evalBinarySub(exp1, exp2);
case Op.MUL then evalBinaryMul(exp1, exp2);
case Op.DIV then evalBinaryDiv(exp1, exp2, target);
case Op.POW then evalBinaryPow(exp1, exp2);
case Op.ADD_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryAdd);
case Op.ADD_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryAdd);
case Op.SUB_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinarySub);
case Op.SUB_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinarySub);
case Op.MUL_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryMul);
case Op.MUL_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryMul);
case Op.MUL_VECTOR_MATRIX then evalBinaryMulVectorMatrix(exp1, exp2);
case Op.MUL_MATRIX_VECTOR then evalBinaryMulMatrixVector(exp1, exp2);
case Op.SCALAR_PRODUCT then evalBinaryScalarProduct(exp1, exp2);
case Op.MATRIX_PRODUCT then evalBinaryMatrixProduct(exp1, exp2);
case Op.DIV_SCALAR_ARRAY
then evalBinaryScalarArray(exp1, exp2, function evalBinaryDiv(target = target));
case Op.DIV_ARRAY_SCALAR
then evalBinaryArrayScalar(exp1, exp2, function evalBinaryDiv(target = target));
case Op.POW_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryPow);
case Op.POW_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryPow);
case Op.POW_MATRIX then evalBinaryPowMatrix(exp1, exp2);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expression.toString(Expression.BINARY(exp1, op, exp2)), sourceInfo());
then
fail();
end match;
end evalBinaryOp_dispatch;
function evalBinaryAdd
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.INTEGER(), Expression.INTEGER())
then Expression.INTEGER(exp1.value + exp2.value);
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value + exp2.value);
case (Expression.STRING(), Expression.STRING())
then Expression.STRING(exp1.value + exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.makeArray(exp1.ty,
list(evalBinaryAdd(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements),
literal = true);
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeAdd(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryAdd;
function evalBinarySub