/
NFConvertDAE.mo
1349 lines (1179 loc) · 41.3 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;
import Restriction = NFRestriction;
protected
import Algorithm = NFAlgorithm;
import Attributes = NFAttributes;
import ComponentReference;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import ElementSource;
import ExecStat.execStat;
import Expression = NFExpression;
import Flags;
import Flatten = NFFlatten;
import Function = NFFunction.Function;
import MetaModelica.Dangerous.listReverseInPlace;
import Class = NFClass;
import NFClassTree.ClassTree;
import Component = NFComponent;
import NFModifier.Modifier;
import NFPrefixes.ConnectorType;
import NFPrefixes.Direction;
import NFPrefixes.Variability;
import NFPrefixes.Visibility;
import Prefixes = NFPrefixes;
import Sections = NFSections;
import Type = NFType;
import Util;
import Variable = NFVariable;
public
function convert
input FlatModel flatModel;
input FunctionTree functions;
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(flatModel.name, elems, flatModel.source, ElementSource.getOptComment(flatModel.source));
dae := DAE.DAE({class_elem});
execStat(getInstanceName());
end convert;
function convertStatements
input list<Statement> statements;
output list<DAE.Statement> elements;
algorithm
elements := list(convertStatement(s) for s in statements);
end convertStatements;
protected
uniontype VariableConversionSettings
record VARIABLE_CONVERSION_SETTINGS
Boolean useLocalDirection;
Boolean isFunctionParameter;
Boolean addTypeToSource;
end VARIABLE_CONVERSION_SETTINGS;
end VariableConversionSettings;
constant VariableConversionSettings FUNCTION_VARIABLE_CONVERSION_SETTINGS =
VARIABLE_CONVERSION_SETTINGS(false, true, false);
function convertVariables
input list<Variable> variables;
input output list<DAE.Element> elements;
protected
VariableConversionSettings settings;
algorithm
settings := VariableConversionSettings.VARIABLE_CONVERSION_SETTINGS(
useLocalDirection = Flags.getConfigBool(Flags.USE_LOCAL_DIRECTION),
isFunctionParameter = false,
addTypeToSource = Flags.isSet(Flags.INFO_XML_OPERATIONS) or Flags.isSet(Flags.VISUAL_XML)
);
for var in listReverse(variables) loop
elements := convertVariable(var, settings) :: elements;
end for;
end convertVariables;
function convertVariable
input Variable var;
input VariableConversionSettings settings;
output DAE.Element daeVar;
protected
Option<DAE.VariableAttributes> var_attr;
Option<DAE.Exp> binding_exp;
algorithm
binding_exp := Binding.toDAEExp(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, settings, var.info);
end convertVariable;
function makeDAEVar
input ComponentRef cref;
input Type ty;
input Option<DAE.Exp> binding;
input Attributes attr;
input Visibility vis;
input Option<DAE.VariableAttributes> vattr;
input Option<SCode.Comment> comment;
input VariableConversionSettings settings;
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 settings.isFunctionParameter then Type.arrayElementType(ty) else ty);
source := ElementSource.createElementSource(info);
if settings.addTypeToSource then
source := addComponentTypeToSource(cref, source);
end if;
var := match attr
case Attributes.ATTRIBUTES()
algorithm
// Strip input/output from non top-level components unless
// --useLocalDirection=true has been set.
if attr.direction == Direction.NONE or settings.useLocalDirection 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),
ConnectorType.toDAE(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 addComponentTypeToSource
input ComponentRef cref;
input output DAE.ElementSource source;
algorithm
source := match cref
case ComponentRef.CREF()
algorithm
source := ElementSource.addElementSourceType(source,
InstNode.scopePath(InstNode.classScope(InstNode.getDerivedNode(InstNode.parent(cref.node)))));
then
addComponentTypeToSource(cref.restCref, source);
else source;
end match;
end addComponentTypeToSource;
function getComponentDirection
"Returns the given direction if the cref refers to a top-level component,
a component in a top-level connector, or a component in a top-level input
component, 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) or InstNode.isInput(rest_cref.node) then
getComponentDirection(dir, rest_cref) else Direction.NONE;
end match;
end getComponentDirection;
function convertVarAttributes
input list<tuple<String, Binding>> attrs;
input Type ty;
input Attributes compAttrs;
output Option<DAE.VariableAttributes> attributes;
protected
Boolean is_final;
Option<Boolean> is_final_opt;
Type elTy;
Boolean is_array = false;
algorithm
is_final := compAttrs.isFinal or
compAttrs.variability == Variability.STRUCTURAL_PARAMETER;
if listEmpty(attrs) and not is_final then
attributes := NONE();
return;
end if;
is_final_opt := SOME(is_final);
attributes := match Type.arrayElementType(ty)
case Type.REAL() then convertRealVarAttributes(attrs, is_final_opt);
case Type.INTEGER() then convertIntVarAttributes(attrs, is_final_opt);
case Type.BOOLEAN() then convertBoolVarAttributes(attrs, is_final_opt);
case Type.STRING() then convertStringVarAttributes(attrs, is_final_opt);
case Type.ENUMERATION() then convertEnumVarAttributes(attrs, is_final_opt);
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();
Option<DAE.Uncertainty> uncertain = NONE();
Option<DAE.Exp> start_origin = 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);
start_origin := convertStartOrigin(b); then ();
case "stateSelect" algorithm state_select := convertStateSelectAttribute(b); then ();
// TODO: VAR_ATTR_REAL has no field for unbounded.
case "unbounded" then ();
case "uncertain" algorithm uncertain := convertUncertaintyAttribute(b); 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, uncertain, NONE(), NONE(), NONE(), isFinal, start_origin));
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(), start_origin = 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);
start_origin := convertStartOrigin(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, start_origin));
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();
Option<DAE.Exp> start_origin = 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);
start_origin := convertStartOrigin(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, start_origin));
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();
Option<DAE.Exp> start_origin = 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);
start_origin := convertStartOrigin(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, start_origin));
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(), start_origin = 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);
start_origin := convertStartOrigin(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, start_origin));
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 convertUncertaintyAttribute
input Binding binding;
output Option<DAE.Uncertainty> 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 Uncertainty expression " + Expression.toString(exp), sourceInfo());
then
fail();
end match;
stateSelect := SOME(lookupUncertaintyMember(name));
end convertUncertaintyAttribute;
function lookupUncertaintyMember
input String name;
output DAE.Uncertainty stateSelect;
algorithm
stateSelect := match name
case "given" then DAE.Uncertainty.GIVEN();
case "sought" then DAE.Uncertainty.SOUGHT();
case "refine" then DAE.Uncertainty.REFINE();
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown Uncertainty literal " + name, sourceInfo());
then
fail();
end match;
end lookupUncertaintyMember;
function convertStartOrigin
input Binding binding;
output Option<DAE.Exp> startOrigin =
SOME(DAE.Exp.SCONST(if Binding.source(binding) == NFBinding.Source.TYPE then "binding" else "type"));
end convertStartOrigin;
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()
algorithm
e1 := Expression.toDAE(eq.lhs);
e2 := Expression.toDAE(eq.rhs);
then
(if Type.isComplex(eq.ty) then
DAE.Element.COMPLEX_EQUATION(e1, e2, eq.source)
elseif Type.isArray(eq.ty) then
DAE.Element.ARRAY_EQUATION(list(Dimension.toDAE(d) for d in Type.arrayDims(eq.ty)), e1, e2, eq.source)
else
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;
case Equation.FOR()
then convertForEquation(eq, isInitial = false) :: elements;
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 convertForEquation
input Equation forEquation;
input Boolean isInitial;
output DAE.Element forDAE;
protected
InstNode iterator;
Type ty;
Expression range;
list<Equation> body;
list<DAE.Element> dbody;
DAE.ElementSource source;
algorithm
Equation.FOR(iterator = iterator, range = SOME(range), body = body, source = source) := forEquation;
if isInitial then
dbody := convertInitialEquations(body);
else
dbody := convertEquations(body);
end if;
Component.ITERATOR(ty = ty) := InstNode.component(iterator);
if isInitial then
forDAE := DAE.Element.INITIAL_FOR_EQUATION(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), 0, Expression.toDAE(range), dbody, source);
else
forDAE := DAE.Element.FOR_EQUATION(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), 0, Expression.toDAE(range), dbody, source);
end if;
end convertForEquation;
function convertIfEquation
input list<Equation.Branch> 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
for branch in ifBranches loop
(conds, branches) := match branch
case Equation.Branch.BRANCH()
then (branch.condition :: conds, branch.body :: branches);
case Equation.Branch.INVALID_BRANCH()
algorithm
Equation.Branch.triggerErrors(branch);
then
fail();
end match;
end for;
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);
dbranches := listReverseInPlace(dbranches);
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<Equation.Branch> 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
when_eq := match b
case Equation.Branch.BRANCH()
algorithm
cond := Expression.toDAE(b.condition);
els := convertEquations(b.body);
then
SOME(DAE.Element.WHEN_EQUATION(cond, els, when_eq, source));
end match;
end for;
SOME(whenEquation) := when_eq;
end convertWhenEquation;
function convertInitialEquations
input list<Equation> equations;
input output list<DAE.Element> elements = {};
algorithm
for eq in listReverse(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
(if Type.isComplex(eq.ty) then
DAE.Element.INITIAL_COMPLEX_EQUATION(e1, e2, eq.source) else
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;
case Equation.FOR()
then convertForEquation(eq, isInitial = true) :: elements;
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<Algorithm> 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 Algorithm alg;
input output list<DAE.Element> elements;
protected
list<DAE.Statement> stmts;
DAE.Algorithm dalg;
DAE.ElementSource src;
algorithm
stmts := convertStatements(alg.statements);
dalg := DAE.ALGORITHM_STMTS(stmts);
elements := DAE.ALGORITHM(dalg, alg.source) :: elements;
end convertAlgorithm;
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, ty, src) := stmt;
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
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;
Statement.ForType for_type;
list<tuple<DAE.ComponentRef, SourceInfo>> loop_vars;
algorithm
Statement.FOR(iterator = iterator, range = SOME(range), body = body, forType = for_type, source = source) := forStmt;
dbody := convertStatements(body);
Component.ITERATOR(ty = ty) := InstNode.component(iterator);
forDAE := match for_type
case Statement.ForType.NORMAL()
then DAE.Statement.STMT_FOR(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), Expression.toDAE(range), dbody, source);
case Statement.ForType.PARALLEL()
algorithm
loop_vars := list(convertForStatementParallelVar(v) for v in for_type.vars);
then
DAE.Statement.STMT_PARFOR(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), Expression.toDAE(range), dbody, loop_vars, source);
end match;
end convertForStatement;
function convertForStatementParallelVar
input tuple<ComponentRef, SourceInfo> var;
output tuple<DAE.ComponentRef, SourceInfo> outVar;
protected
ComponentRef cref;
DAE.ComponentRef dcref;
SourceInfo info;
algorithm
(cref, info) := var;
dcref := ComponentRef.toDAE(cref);
outVar := (dcref, info);
end convertForStatementParallelVar;
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));