/
NFConvertDAE.mo
1096 lines (957 loc) · 33.6 KB
/
NFConvertDAE.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 NFConvertDAE
import Binding = NFBinding;
import DAE;
import Equation = NFEquation;
import FlatModel = NFFlatModel;
import NFFlatten.FunctionTree;
import NFInstNode.InstNode;
import Statement = NFStatement;
protected
import ExecStat.execStat;
import ElementSource;
import ComponentRef = NFComponentRef;
import Type = NFType;
import NFModifier.Modifier;
import Expression = NFExpression;
import NFComponent.Component;
import Prefixes = NFPrefixes;
import NFClass.Class;
import DAEUtil;
import Dimension = NFDimension;
import List;
import Util;
import MetaModelica.Dangerous.listReverseInPlace;
import Sections = NFSections;
import Function = NFFunction.Function;
import NFClassTree.ClassTree;
import NFPrefixes.Visibility;
import NFPrefixes.Direction;
import Variable = NFVariable;
import ComponentReference;
public
function convert
input FlatModel flatModel;
input FunctionTree functions;
input String name;
input SourceInfo info;
output DAE.DAElist dae;
output DAE.FunctionTree daeFunctions;
protected
list<DAE.Element> elems;
DAE.Element class_elem;
algorithm
daeFunctions := convertFunctionTree(functions);
elems := convertVariables(flatModel.variables, {});
elems := convertEquations(flatModel.equations, elems);
elems := convertInitialEquations(flatModel.initialEquations, elems);
elems := convertAlgorithms(flatModel.algorithms, elems);
elems := convertInitialAlgorithms(flatModel.initialAlgorithms, elems);
class_elem := DAE.COMP(name, elems, ElementSource.createElementSource(info), flatModel.comment);
dae := DAE.DAE({class_elem});
execStat(getInstanceName() + "(" + name + ")");
end convert;
protected
function convertVariables
input list<Variable> variables;
input output list<DAE.Element> elements;
protected
Boolean localDir = Flags.getConfigBool(Flags.USE_LOCAL_DIRECTION);
algorithm
for var in listReverse(variables) loop
elements := convertVariable(var, localDir) :: elements;
end for;
end convertVariables;
function convertVariable
input Variable var;
input Boolean useLocalDir;
output DAE.Element daeVar;
protected
Option<DAE.VariableAttributes> var_attr;
Option<DAE.Exp> binding_exp;
algorithm
binding_exp := convertBinding(var.binding);
var_attr := convertVarAttributes(var.typeAttributes, var.ty, var.attributes);
daeVar := makeDAEVar(var.name, var.ty, binding_exp, var.attributes,
var.visibility, var_attr, var.comment, useLocalDir, false, var.info);
end convertVariable;
function makeDAEVar
input ComponentRef cref;
input Type ty;
input Option<DAE.Exp> binding;
input Component.Attributes attr;
input Visibility vis;
input Option<DAE.VariableAttributes> vattr;
input Option<SCode.Comment> comment;
input Boolean useLocalDir;
input Boolean isFunctionParam;
input SourceInfo info;
output DAE.Element var;
protected
DAE.ComponentRef dcref;
DAE.Type dty;
DAE.ElementSource source;
Direction dir;
algorithm
dcref := ComponentRef.toDAE(cref);
dty := Type.toDAE(if isFunctionParam then Type.arrayElementType(ty) else ty);
source := ElementSource.createElementSource(info);
var := match attr
case Component.Attributes.ATTRIBUTES()
algorithm
// Strip input/output from non top-level components unless
// --useLocalDirection=true has been set.
if attr.direction == Direction.NONE or useLocalDir then
dir := attr.direction;
else
dir := getComponentDirection(attr.direction, cref);
end if;
then
DAE.VAR(
dcref,
Prefixes.variabilityToDAE(attr.variability),
Prefixes.directionToDAE(dir),
Prefixes.parallelismToDAE(attr.parallelism),
Prefixes.visibilityToDAE(vis),
dty,
binding,
ComponentReference.crefDims(dcref),
Prefixes.connectorTypeToDAE(attr.connectorType),
source,
vattr,
comment,
Absyn.NOT_INNER_OUTER()
);
else
DAE.VAR(dcref, DAE.VarKind.VARIABLE(), DAE.VarDirection.BIDIR(),
DAE.VarParallelism.NON_PARALLEL(), Prefixes.visibilityToDAE(vis), dty,
binding, {}, DAE.ConnectorType.NON_CONNECTOR(), source, vattr, comment,
Absyn.NOT_INNER_OUTER());
end match;
end makeDAEVar;
function getComponentDirection
"Returns the given direction if the cref refers to a top-level component or to
a component in a top-level connector, otherwise returns Direction.NONE."
input output Direction dir;
input ComponentRef cref;
protected
ComponentRef rest_cref = ComponentRef.rest(cref);
algorithm
dir := match rest_cref
case ComponentRef.EMPTY() then dir;
case ComponentRef.CREF()
then if InstNode.isConnector(rest_cref.node) then
getComponentDirection(dir, rest_cref) else Direction.NONE;
end match;
end getComponentDirection;
function convertBinding
input Binding binding;
output Option<DAE.Exp> bindingExp;
algorithm
bindingExp := match binding
case Binding.UNBOUND() then NONE();
case Binding.TYPED_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
case Binding.FLAT_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
end match;
end convertBinding;
function convertVarAttributes
input list<tuple<String, Binding>> attrs;
input Type ty;
input Component.Attributes compAttrs;
output Option<DAE.VariableAttributes> attributes;
protected
Option<Boolean> is_final;
algorithm
if listEmpty(attrs) and not compAttrs.isFinal then
attributes := NONE();
return;
end if;
is_final := SOME(compAttrs.isFinal);
attributes := match ty
case Type.REAL() then convertRealVarAttributes(attrs, is_final);
case Type.INTEGER() then convertIntVarAttributes(attrs, is_final);
case Type.BOOLEAN() then convertBoolVarAttributes(attrs, is_final);
case Type.STRING() then convertStringVarAttributes(attrs, is_final);
case Type.ENUMERATION() then convertEnumVarAttributes(attrs, is_final);
else NONE();
end match;
end convertVarAttributes;
function convertRealVarAttributes
input list<tuple<String, Binding>> attrs;
input Option<Boolean> isFinal;
output Option<DAE.VariableAttributes> attributes;
protected
String name;
Binding b;
Option<DAE.Exp> quantity = NONE(), unit = NONE(), displayUnit = NONE();
Option<DAE.Exp> min = NONE(), max = NONE(), start = NONE(), fixed = NONE(), nominal = NONE();
Option<DAE.StateSelect> state_select = NONE();
algorithm
for attr in attrs loop
(name, b) := attr;
() := match name
case "displayUnit" algorithm displayUnit := convertVarAttribute(b); then ();
case "fixed" algorithm fixed := convertVarAttribute(b); then ();
case "max" algorithm max := convertVarAttribute(b); then ();
case "min" algorithm min := convertVarAttribute(b); then ();
case "nominal" algorithm nominal := convertVarAttribute(b); then ();
case "quantity" algorithm quantity := convertVarAttribute(b); then ();
case "start" algorithm start := convertVarAttribute(b); then ();
case "stateSelect" algorithm state_select := convertStateSelectAttribute(b); then ();
// TODO: VAR_ATTR_REAL has no field for unbounded.
case "unbounded" then ();
case "unit" algorithm unit := convertVarAttribute(b); then ();
// The attributes should already be type checked, so we shouldn't get any
// unknown attributes here.
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type attribute " + name, sourceInfo());
then
fail();
end match;
end for;
attributes := SOME(DAE.VariableAttributes.VAR_ATTR_REAL(
quantity, unit, displayUnit, min, max, start, fixed, nominal,
state_select, NONE(), NONE(), NONE(), NONE(), isFinal, NONE()));
end convertRealVarAttributes;
function convertIntVarAttributes
input list<tuple<String, Binding>> attrs;
input Option<Boolean> isFinal;
output Option<DAE.VariableAttributes> attributes;
protected
String name;
Binding b;
Option<DAE.Exp> quantity = NONE(), min = NONE(), max = NONE();
Option<DAE.Exp> start = NONE(), fixed = NONE();
algorithm
for attr in attrs loop
(name, b) := attr;
() := match name
case "quantity" algorithm quantity := convertVarAttribute(b); then ();
case "min" algorithm min := convertVarAttribute(b); then ();
case "max" algorithm max := convertVarAttribute(b); then ();
case "start" algorithm start := convertVarAttribute(b); then ();
case "fixed" algorithm fixed := convertVarAttribute(b); then ();
// The attributes should already be type checked, so we shouldn't get any
// unknown attributes here.
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type attribute " + name, sourceInfo());
then
fail();
end match;
end for;
attributes := SOME(DAE.VariableAttributes.VAR_ATTR_INT(
quantity, min, max, start, fixed,
NONE(), NONE(), NONE(), NONE(), isFinal, NONE()));
end convertIntVarAttributes;
function convertBoolVarAttributes
input list<tuple<String, Binding>> attrs;
input Option<Boolean> isFinal;
output Option<DAE.VariableAttributes> attributes;
protected
String name;
Binding b;
Option<DAE.Exp> quantity = NONE(), start = NONE(), fixed = NONE();
algorithm
for attr in attrs loop
(name, b) := attr;
() := match name
case "quantity" algorithm quantity := convertVarAttribute(b); then ();
case "start" algorithm start := convertVarAttribute(b); then ();
case "fixed" algorithm fixed := convertVarAttribute(b); then ();
// The attributes should already be type checked, so we shouldn't get any
// unknown attributes here.
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type attribute " + name, sourceInfo());
then
fail();
end match;
end for;
attributes := SOME(DAE.VariableAttributes.VAR_ATTR_BOOL(
quantity, start, fixed, NONE(), NONE(), isFinal, NONE()));
end convertBoolVarAttributes;
function convertStringVarAttributes
input list<tuple<String, Binding>> attrs;
input Option<Boolean> isFinal;
output Option<DAE.VariableAttributes> attributes;
protected
String name;
Binding b;
Option<DAE.Exp> quantity = NONE(), start = NONE(), fixed = NONE();
algorithm
for attr in attrs loop
(name, b) := attr;
() := match name
case "quantity" algorithm quantity := convertVarAttribute(b); then ();
case "start" algorithm start := convertVarAttribute(b); then ();
case "fixed" algorithm fixed := convertVarAttribute(b); then ();
// The attributes should already be type checked, so we shouldn't get any
// unknown attributes here.
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type attribute " + name, sourceInfo());
then
fail();
end match;
end for;
attributes := SOME(DAE.VariableAttributes.VAR_ATTR_STRING(
quantity, start, fixed, NONE(), NONE(), isFinal, NONE()));
end convertStringVarAttributes;
function convertEnumVarAttributes
input list<tuple<String, Binding>> attrs;
input Option<Boolean> isFinal;
output Option<DAE.VariableAttributes> attributes;
protected
String name;
Binding b;
Option<DAE.Exp> quantity = NONE(), min = NONE(), max = NONE();
Option<DAE.Exp> start = NONE(), fixed = NONE();
algorithm
for attr in attrs loop
(name, b) := attr;
() := match name
case "fixed" algorithm fixed := convertVarAttribute(b); then ();
case "max" algorithm max := convertVarAttribute(b); then ();
case "min" algorithm min := convertVarAttribute(b); then ();
case "quantity" algorithm quantity := convertVarAttribute(b); then ();
case "start" algorithm start := convertVarAttribute(b); then ();
// The attributes should already be type checked, so we shouldn't get any
// unknown attributes here.
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type attribute " + name, sourceInfo());
then
fail();
end match;
end for;
attributes := SOME(DAE.VariableAttributes.VAR_ATTR_ENUMERATION(
quantity, min, max, start, fixed, NONE(), NONE(), isFinal, NONE()));
end convertEnumVarAttributes;
function convertVarAttribute
input Binding binding;
output Option<DAE.Exp> attribute = SOME(Expression.toDAE(Binding.getTypedExp(binding)));
end convertVarAttribute;
function convertStateSelectAttribute
input Binding binding;
output Option<DAE.StateSelect> stateSelect;
protected
InstNode node;
String name;
Expression exp = Binding.getTypedExp(binding);
algorithm
name := match exp
case Expression.ENUM_LITERAL() then exp.name;
case Expression.CREF(cref = ComponentRef.CREF(node = node)) then InstNode.name(node);
else
algorithm
Error.assertion(false, getInstanceName() +
" got invalid StateSelect expression " + Expression.toString(exp), sourceInfo());
then
fail();
end match;
stateSelect := SOME(lookupStateSelectMember(name));
end convertStateSelectAttribute;
function lookupStateSelectMember
input String name;
output DAE.StateSelect stateSelect;
algorithm
stateSelect := match name
case "never" then DAE.StateSelect.NEVER();
case "avoid" then DAE.StateSelect.AVOID();
case "default" then DAE.StateSelect.DEFAULT();
case "prefer" then DAE.StateSelect.PREFER();
case "always" then DAE.StateSelect.ALWAYS();
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown StateSelect literal " + name, sourceInfo());
then
fail();
end match;
end lookupStateSelectMember;
function convertEquations
input list<Equation> equations;
input output list<DAE.Element> elements = {};
algorithm
for eq in listReverse(equations) loop
elements := convertEquation(eq, elements);
end for;
end convertEquations;
function convertEquation
input Equation eq;
input output list<DAE.Element> elements;
algorithm
elements := match eq
local
DAE.Exp e1, e2, e3;
DAE.ComponentRef cr1, cr2;
list<DAE.Dimension> dims;
list<DAE.Element> body;
case Equation.EQUALITY() guard Type.isComplex(eq.ty)
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
then
DAE.Element.COMPLEX_EQUATION(e1, e2, eq.source) :: elements;
case Equation.EQUALITY()
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
then
DAE.Element.EQUATION(e1, e2, eq.source) :: elements;
case Equation.CREF_EQUALITY()
algorithm
cr1 := ComponentRef.toDAE(eq.lhs);
cr2 := ComponentRef.toDAE(eq.rhs);
then
DAE.Element.EQUEQUATION(cr1, cr2, eq.source) :: elements;
case Equation.ARRAY_EQUALITY()
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
dims := list(Dimension.toDAE(d) for d in Type.arrayDims(eq.ty));
then
DAE.Element.ARRAY_EQUATION(dims, e1, e2, eq.source) :: elements;
// For equations should have been unrolled here.
case Equation.FOR()
algorithm
Error.assertion(false, getInstanceName() + " got a for equation", sourceInfo());
then
fail();
case Equation.IF()
then convertIfEquation(eq.branches, eq.source, isInitial = false) :: elements;
case Equation.WHEN()
then convertWhenEquation(eq.branches, eq.source) :: elements;
case Equation.ASSERT()
algorithm
e1 := Expression.toDAE(eq.condition);
e2 := Expression.toDAE(eq.message);
e3 := Expression.toDAE(eq.level);
then
DAE.Element.ASSERT(e1, e2, e3, eq.source) :: elements;
case Equation.TERMINATE()
then DAE.Element.TERMINATE(Expression.toDAE(eq.message), eq.source) :: elements;
case Equation.REINIT()
algorithm
cr1 := ComponentRef.toDAE(Expression.toCref(eq.cref));
e1 := Expression.toDAE(eq.reinitExp);
then
DAE.Element.REINIT(cr1, e1, eq.source) :: elements;
case Equation.NORETCALL()
then DAE.Element.NORETCALL(Expression.toDAE(eq.exp), eq.source) :: elements;
else elements;
end match;
end convertEquation;
function convertIfEquation
input list<tuple<Expression, list<Equation>>> ifBranches;
input DAE.ElementSource source;
input Boolean isInitial;
output DAE.Element ifEquation;
protected
list<Expression> conds;
list<list<Equation>> branches;
list<DAE.Exp> dconds;
list<list<DAE.Element>> dbranches;
list<DAE.Element> else_branch;
algorithm
(conds, branches) := List.unzipReverse(ifBranches);
dbranches := if isInitial then
list(convertInitialEquations(b) for b in branches) else
list(convertEquations(b) for b in branches);
// Transform the last branch to an else-branch if its condition is true.
if Expression.isTrue(listHead(conds)) then
else_branch :: dbranches := dbranches;
conds := listRest(conds);
else
else_branch := {};
end if;
dconds := listReverse(Expression.toDAE(c) for c in conds);
ifEquation := if isInitial then
DAE.Element.INITIAL_IF_EQUATION(dconds, dbranches, else_branch, source) else
DAE.Element.IF_EQUATION(dconds, dbranches, else_branch, source);
end convertIfEquation;
function convertWhenEquation
input list<tuple<Expression, list<Equation>>> whenBranches;
input DAE.ElementSource source;
output DAE.Element whenEquation;
protected
DAE.Exp cond;
list<DAE.Element> els;
Option<DAE.Element> when_eq = NONE();
algorithm
for b in listReverse(whenBranches) loop
cond := Expression.toDAE(Util.tuple21(b));
els := convertEquations(Util.tuple22(b));
when_eq := SOME(DAE.Element.WHEN_EQUATION(cond, els, when_eq, source));
end for;
SOME(whenEquation) := when_eq;
end convertWhenEquation;
function convertInitialEquations
input list<Equation> equations;
input output list<DAE.Element> elements = {};
algorithm
for eq in equations loop
elements := convertInitialEquation(eq, elements);
end for;
end convertInitialEquations;
function convertInitialEquation
input Equation eq;
input output list<DAE.Element> elements;
algorithm
elements := match eq
local
DAE.Exp e1, e2, e3;
DAE.ComponentRef cref;
list<DAE.Dimension> dims;
list<DAE.Element> body;
case Equation.EQUALITY()
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
then
DAE.Element.INITIALEQUATION(e1, e2, eq.source) :: elements;
case Equation.ARRAY_EQUALITY()
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
dims := list(Dimension.toDAE(d) for d in Type.arrayDims(eq.ty));
then
DAE.Element.INITIAL_ARRAY_EQUATION(dims, e1, e2, eq.source) :: elements;
// For equations should have been unrolled here.
case Equation.FOR()
algorithm
Error.assertion(false, getInstanceName() + " got a for equation", sourceInfo());
then
fail();
case Equation.IF()
then convertIfEquation(eq.branches, eq.source, isInitial = true) :: elements;
case Equation.ASSERT()
algorithm
e1 := Expression.toDAE(eq.condition);
e2 := Expression.toDAE(eq.message);
e3 := Expression.toDAE(eq.level);
then
DAE.Element.INITIAL_ASSERT(e1, e2, e3, eq.source) :: elements;
case Equation.TERMINATE()
then DAE.Element.INITIAL_TERMINATE(Expression.toDAE(eq.message), eq.source) :: elements;
case Equation.NORETCALL()
then DAE.Element.INITIAL_NORETCALL(Expression.toDAE(eq.exp), eq.source) :: elements;
else elements;
end match;
end convertInitialEquation;
function convertAlgorithms
input list<list<Statement>> algorithms;
input output list<DAE.Element> elements;
algorithm
for alg in listReverse(algorithms) loop
elements := convertAlgorithm(alg, elements);
end for;
end convertAlgorithms;
function convertAlgorithm
input list<Statement> statements;
input output list<DAE.Element> elements;
protected
list<DAE.Statement> stmts;
DAE.Algorithm alg;
algorithm
stmts := convertStatements(statements);
alg := DAE.ALGORITHM_STMTS(stmts);
elements := DAE.ALGORITHM(alg, DAE.emptyElementSource) :: elements;
end convertAlgorithm;
function convertStatements
input list<Statement> statements;
output list<DAE.Statement> elements;
algorithm
elements := list(convertStatement(s) for s in statements);
end convertStatements;
function convertStatement
input Statement stmt;
output DAE.Statement elem;
algorithm
elem := match stmt
local
DAE.Exp e1, e2, e3;
DAE.Type ty;
list<DAE.Statement> body;
case Statement.ASSIGNMENT() then convertAssignment(stmt);
case Statement.FUNCTION_ARRAY_INIT()
algorithm
ty := Type.toDAE(stmt.ty);
then
DAE.Statement.STMT_ARRAY_INIT(stmt.name, ty, stmt.source);
case Statement.FOR() then convertForStatement(stmt);
case Statement.IF() then convertIfStatement(stmt.branches, stmt.source);
case Statement.WHEN() then convertWhenStatement(stmt.branches, stmt.source);
case Statement.ASSERT()
algorithm
e1 := Expression.toDAE(stmt.condition);
e2 := Expression.toDAE(stmt.message);
e3 := Expression.toDAE(stmt.level);
then
DAE.Statement.STMT_ASSERT(e1, e2, e3, stmt.source);
case Statement.TERMINATE()
then DAE.Statement.STMT_TERMINATE(Expression.toDAE(stmt.message), stmt.source);
case Statement.NORETCALL()
then DAE.Statement.STMT_NORETCALL(Expression.toDAE(stmt.exp), stmt.source);
case Statement.WHILE()
algorithm
e1 := Expression.toDAE(stmt.condition);
body := convertStatements(stmt.body);
then
DAE.Statement.STMT_WHILE(e1, body, stmt.source);
case Statement.RETURN()
then DAE.Statement.STMT_RETURN(stmt.source);
case Statement.BREAK()
then DAE.Statement.STMT_BREAK(stmt.source);
case Statement.FAILURE()
then DAE.Statement.STMT_FAILURE(convertStatements(stmt.body), stmt.source);
end match;
end convertStatement;
function convertAssignment
input Statement stmt;
output DAE.Statement daeStmt;
protected
Expression lhs, rhs;
DAE.ElementSource src;
Type ty;
DAE.Type dty;
DAE.Exp dlhs, drhs;
list<Expression> expl;
algorithm
Statement.ASSIGNMENT(lhs, rhs, src) := stmt;
ty := Expression.typeOf(lhs);
if Type.isTuple(ty) then
Expression.TUPLE(elements = expl) := lhs;
daeStmt := match expl
// () := call(...) => call(...)
case {} then DAE.Statement.STMT_NORETCALL(Expression.toDAE(rhs), src);
// (lhs) := call(...) => lhs := TSUB[call(...), 1]
case {lhs}
algorithm
ty := Expression.typeOf(lhs);
dty := Type.toDAE(ty);
dlhs := Expression.toDAE(lhs);
drhs := DAE.Exp.TSUB(Expression.toDAE(rhs), 1, dty);
if Type.isArray(ty) then
daeStmt := DAE.Statement.STMT_ASSIGN_ARR(dty, dlhs, drhs, src);
else
daeStmt := DAE.Statement.STMT_ASSIGN(dty, dlhs, drhs, src);
end if;
then
daeStmt;
else
algorithm
dty := Type.toDAE(ty);
drhs := Expression.toDAE(rhs);
then
DAE.Statement.STMT_TUPLE_ASSIGN(dty, list(Expression.toDAE(e) for e in expl), drhs, src);
end match;
else
dty := Type.toDAE(ty);
dlhs := Expression.toDAE(lhs);
drhs := Expression.toDAE(rhs);
if Type.isArray(ty) then
daeStmt := DAE.Statement.STMT_ASSIGN_ARR(dty, dlhs, drhs, src);
else
daeStmt := DAE.Statement.STMT_ASSIGN(dty, dlhs, drhs, src);
end if;
end if;
end convertAssignment;
function convertForStatement
input Statement forStmt;
output DAE.Statement forDAE;
protected
InstNode iterator;
Type ty;
Expression range;
list<Statement> body;
list<DAE.Statement> dbody;
DAE.ElementSource source;
algorithm
Statement.FOR(iterator = iterator, range = SOME(range), body = body, source = source) := forStmt;
dbody := convertStatements(body);
Component.ITERATOR(ty = ty) := InstNode.component(iterator);
forDAE := DAE.Statement.STMT_FOR(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), 0, Expression.toDAE(range), dbody, source);
end convertForStatement;
function convertIfStatement
input list<tuple<Expression, list<Statement>>> ifBranches;
input DAE.ElementSource source;
output DAE.Statement ifStatement;
protected
Expression cond;
DAE.Exp dcond;
list<Statement> stmts;
list<DAE.Statement> dstmts;
Boolean first = true;
DAE.Else else_stmt = DAE.Else.NOELSE();
algorithm
for b in listReverse(ifBranches) loop
(cond, stmts) := b;
dcond := Expression.toDAE(cond);
dstmts := convertStatements(stmts);
if first and Expression.isTrue(cond) then
else_stmt := DAE.Else.ELSE(dstmts);
else
else_stmt := DAE.Else.ELSEIF(dcond, dstmts, else_stmt);
end if;
first := false;
end for;
// This should always be an ELSEIF due to branch selection in earlier phases.
DAE.Else.ELSEIF(dcond, dstmts, else_stmt) := else_stmt;
ifStatement := DAE.Statement.STMT_IF(dcond, dstmts, else_stmt, source);
end convertIfStatement;
function convertWhenStatement
input list<tuple<Expression, list<Statement>>> whenBranches;
input DAE.ElementSource source;
output DAE.Statement whenStatement;
protected
DAE.Exp cond;
list<DAE.Statement> stmts;
Option<DAE.Statement> when_stmt = NONE();
algorithm
for b in listReverse(whenBranches) loop
cond := Expression.toDAE(Util.tuple21(b));
stmts := convertStatements(Util.tuple22(b));
when_stmt := SOME(DAE.Statement.STMT_WHEN(cond, {}, false, stmts, when_stmt, source));
end for;
SOME(whenStatement) := when_stmt;
end convertWhenStatement;
function convertInitialAlgorithms
input list<list<Statement>> algorithms;
input output list<DAE.Element> elements;
algorithm
for alg in algorithms loop
elements := convertInitialAlgorithm(alg, elements);
end for;
end convertInitialAlgorithms;
function convertInitialAlgorithm
input list<Statement> statements;
input output list<DAE.Element> elements;
protected
list<DAE.Statement> stmts;
DAE.Algorithm alg;
algorithm
stmts := convertStatements(statements);
alg := DAE.ALGORITHM_STMTS(stmts);
elements := DAE.INITIALALGORITHM(alg, DAE.emptyElementSource) :: elements;
end convertInitialAlgorithm;
function convertFunctionTree
input FunctionTree funcs;
output DAE.FunctionTree dfuncs;
algorithm
dfuncs := match funcs
local
DAE.FunctionTree left, right;
DAE.Function fn;
case FunctionTree.NODE()
algorithm
fn := convertFunction(funcs.value);
left := convertFunctionTree(funcs.left);
right := convertFunctionTree(funcs.right);
then
DAE.FunctionTree.NODE(funcs.key, SOME(fn), funcs.height, left, right);
case FunctionTree.LEAF()
algorithm
fn := convertFunction(funcs.value);
then
DAE.FunctionTree.LEAF(funcs.key, SOME(fn));
case FunctionTree.EMPTY()
then DAE.FunctionTree.EMPTY();
end match;
end convertFunctionTree;
function convertFunction
input Function func;
output DAE.Function dfunc;
protected
Class cls;
list<DAE.Element> elems;
DAE.FunctionDefinition def;
Sections sections;
algorithm
cls := InstNode.getClass(Function.instance(func));
dfunc := match cls
case Class.INSTANCED_CLASS(sections = sections)
algorithm
elems := convertFunctionParams(func.inputs, {});
elems := convertFunctionParams(func.outputs, elems);
elems := convertFunctionParams(func.locals, elems);
def := match sections
// A function with an algorithm section.
case Sections.SECTIONS()
algorithm
elems := convertAlgorithms(sections.algorithms, elems);
then
DAE.FunctionDefinition.FUNCTION_DEF(listReverse(elems));
// An external function.
case Sections.EXTERNAL()
then convertExternalDecl(sections, listReverse(elems));
// A function without either algorithm or external section.
else DAE.FunctionDefinition.FUNCTION_DEF(listReverse(elems));
end match;
then
Function.toDAE(func, {def});
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown function", sourceInfo());
then
fail();
end match;
end convertFunction;
function convertFunctionParams
input list<InstNode> params;
input output list<DAE.Element> elements;
algorithm
for p in params loop
elements := convertFunctionParam(p) :: elements;
end for;
end convertFunctionParams;
function convertFunctionParam
input InstNode node;
output DAE.Element element;
protected
Component comp;
Class cls;
SourceInfo info;
Option<DAE.VariableAttributes> var_attr;
ComponentRef cref;
Component.Attributes attr;
Type ty;
Option<DAE.Exp> binding;
list<tuple<String, Binding>> ty_attr;
algorithm
comp := InstNode.component(node);
element := match comp
case Component.TYPED_COMPONENT(ty = ty, info = info)
algorithm
cref := ComponentRef.fromNode(node, ty);
binding := convertBinding(comp.binding);
cls := InstNode.getClass(comp.classInst);
ty_attr := list((Modifier.name(m), Modifier.binding(m)) for m in Class.getTypeAttributes(cls));
attr := comp.attributes;
var_attr := convertVarAttributes(ty_attr, ty, attr);
then
makeDAEVar(cref, ty, binding, attr, InstNode.visibility(node), var_attr, comp.comment, true, true, info);
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped component.", sourceInfo());
then
fail();
end match;
end convertFunctionParam;
function convertExternalDecl
input Sections extDecl;
input list<DAE.Element> parameters;
output DAE.FunctionDefinition funcDef;
protected
DAE.ExternalDecl decl;
list<DAE.ExtArg> args;
DAE.ExtArg ret_arg;