-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFFlatModel.mo
599 lines (518 loc) · 18.1 KB
/
NFFlatModel.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
/*
* 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 uniontype NFFlatModel
import Equation = NFEquation;
import Algorithm = NFAlgorithm;
import Variable = NFVariable;
protected
import Statement = NFStatement;
import NFFunction.Function;
import Expression = NFExpression;
import Type = NFType;
import Binding = NFBinding;
import Dimension = NFDimension;
import ComplexType = NFComplexType;
import NFInstNode.InstNode;
import IOStream;
import NFSubscript.Subscript;
import Class = NFClass;
import NFClassTree.ClassTree;
import Component = NFComponent;
import NFComponentRef.ComponentRef;
import DAE.ElementSource;
import MetaModelica.Dangerous.listReverseInPlace;
import FlatModel = NFFlatModel;
type TypeTree = TypeTreeImpl.Tree;
encapsulated package TypeTreeImpl
import BaseAvlTree;
import Absyn.Path;
import NFType.Type;
extends BaseAvlTree;
redeclare type Key = Absyn.Path;
redeclare type Value = Type;
redeclare function extends keyStr
algorithm
outString := AbsynUtil.pathString(inKey);
end keyStr;
redeclare function extends valueStr
algorithm
outString := Type.toString(inValue);
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := AbsynUtil.pathCompareNoQual(inKey1, inKey2);
end keyCompare;
redeclare function addConflictDefault = addConflictKeep;
end TypeTreeImpl;
public
record FLAT_MODEL
String name;
list<Variable> variables;
list<Equation> equations;
list<Equation> initialEquations;
list<Algorithm> algorithms;
list<Algorithm> initialAlgorithms;
ElementSource source;
end FLAT_MODEL;
function toString
input FlatModel flatModel;
input Boolean printBindingTypes = false;
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := IOStream.append(s, "class " + flatModel.name + "\n");
for v in flatModel.variables loop
s := Variable.toStream(v, " ", printBindingTypes, s);
s := IOStream.append(s, ";\n");
end for;
if not listEmpty(flatModel.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toStreamList(flatModel.initialEquations, " ", s);
end if;
if not listEmpty(flatModel.equations) then
s := IOStream.append(s, "equation\n");
s := Equation.toStreamList(flatModel.equations, " ", s);
end if;
for alg in flatModel.initialAlgorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "initial algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;
for alg in flatModel.algorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;
s := IOStream.append(s, "end " + flatModel.name + ";\n");
str := IOStream.string(s);
IOStream.delete(s);
end toString;
function toFlatString
"Returns a string containing the flat Modelica representation of the given model."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
output String str = IOStream.string(toFlatStream(flatModel, functions, printBindingTypes));
end toFlatString;
function printFlatString
"Prints a flat Modelica representation of the given model to standard output."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
protected
IOStream.IOStream s;
algorithm
s := toFlatStream(flatModel, functions, printBindingTypes);
IOStream.print(s, IOStream.stdOutput);
end printFlatString;
function toFlatStream
"Returns a new IOStream containing the flat Modelica representation of the given model."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
output IOStream.IOStream s;
algorithm
s := IOStream.create(flatModel.name, IOStream.IOStreamType.LIST());
s := appendFlatStream(flatModel, functions, printBindingTypes, s);
end toFlatStream;
function appendFlatStream
"Appends the flat Modelica representation of the given model to an existing IOStream."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
input output IOStream.IOStream s;
protected
FlatModel flat_model = flatModel;
algorithm
flat_model.variables := reconstructRecordInstances(flat_model.variables);
for fn in functions loop
if not Function.isDefaultRecordConstructor(fn) then
s := Function.toFlatStream(fn, s);
s := IOStream.append(s, ";\n\n");
end if;
end for;
for ty in TypeTree.listValues(collectFlatTypes(flat_model, functions)) loop
s := Type.toFlatDeclarationStream(ty, s);
s := IOStream.append(s, ";\n\n");
end for;
s := IOStream.append(s, "class '" + flat_model.name + "'\n");
for v in flat_model.variables loop
s := Variable.toFlatStream(v, " ", printBindingTypes, s);
s := IOStream.append(s, ";\n");
end for;
s := IOStream.append(s, "public\n");
if not listEmpty(flat_model.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toFlatStreamList(flat_model.initialEquations, " ", s);
end if;
if not listEmpty(flat_model.equations) then
s := IOStream.append(s, "equation\n");
s := Equation.toFlatStreamList(flat_model.equations, " ", s);
end if;
for alg in flat_model.initialAlgorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "initial algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end if;
end for;
for alg in flat_model.algorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end if;
end for;
s := IOStream.append(s, "end '" + flat_model.name + "';\n");
end appendFlatStream;
function collectFlatTypes
input FlatModel flatModel;
input list<Function> functions;
output TypeTree types;
algorithm
types := TypeTree.new();
types := List.fold(flatModel.variables, collectVariableFlatTypes, types);
types := List.fold(flatModel.equations, collectEquationFlatTypes, types);
types := List.fold(flatModel.initialEquations, collectEquationFlatTypes, types);
types := List.fold(flatModel.algorithms, collectAlgorithmFlatTypes, types);
types := List.fold(flatModel.initialAlgorithms, collectAlgorithmFlatTypes, types);
types := List.fold(functions, collectFunctionFlatTypes, types);
end collectFlatTypes;
function collectVariableFlatTypes
input Variable var;
input output TypeTree types;
algorithm
types := collectFlatType(var.ty, types);
types := collectBindingFlatTypes(var.binding, types);
for attr in var.typeAttributes loop
types := collectBindingFlatTypes(Util.tuple22(attr), types);
end for;
end collectVariableFlatTypes;
function collectFlatType
input Type ty;
input output TypeTree types;
algorithm
() := match ty
case Type.ENUMERATION()
algorithm
types := TypeTree.add(types, ty.typePath, ty);
then
();
case Type.ARRAY()
algorithm
types := Dimension.foldExpList(ty.dimensions, collectExpFlatTypes_traverse, types);
types := collectFlatType(ty.elementType, types);
then
();
case Type.COMPLEX(complexTy = ComplexType.RECORD())
algorithm
types := TypeTree.add(types, InstNode.scopePath(ty.cls), ty);
then
();
else ();
end match;
end collectFlatType;
function collectBindingFlatTypes
input Binding binding;
input output TypeTree types;
algorithm
if Binding.isExplicitlyBound(binding) then
types := collectExpFlatTypes(Binding.getTypedExp(binding), types);
end if;
end collectBindingFlatTypes;
function collectEquationFlatTypes
input Equation eq;
input output TypeTree types;
algorithm
() := match eq
case Equation.EQUALITY()
algorithm
types := collectExpFlatTypes(eq.lhs, types);
types := collectExpFlatTypes(eq.rhs, types);
types := collectFlatType(eq.ty, types);
then
();
case Equation.ARRAY_EQUALITY()
algorithm
types := collectExpFlatTypes(eq.lhs, types);
types := collectExpFlatTypes(eq.rhs, types);
types := collectFlatType(eq.ty, types);
then
();
case Equation.FOR()
algorithm
types := List.fold(eq.body, collectEquationFlatTypes, types);
then
();
case Equation.IF()
algorithm
types := List.fold(eq.branches, collectEqBranchFlatTypes, types);
then
();
case Equation.WHEN()
algorithm
types := List.fold(eq.branches, collectEqBranchFlatTypes, types);
then
();
case Equation.ASSERT()
algorithm
types := collectExpFlatTypes(eq.condition, types);
types := collectExpFlatTypes(eq.message, types);
types := collectExpFlatTypes(eq.level, types);
then
();
case Equation.TERMINATE()
algorithm
types := collectExpFlatTypes(eq.message, types);
then
();
case Equation.REINIT()
algorithm
types := collectExpFlatTypes(eq.reinitExp, types);
then
();
case Equation.NORETCALL()
algorithm
types := collectExpFlatTypes(eq.exp, types);
then
();
else ();
end match;
end collectEquationFlatTypes;
function collectEqBranchFlatTypes
input Equation.Branch branch;
input output TypeTree types;
algorithm
() := match branch
case Equation.Branch.BRANCH()
algorithm
types := collectExpFlatTypes(branch.condition, types);
types := List.fold(branch.body, collectEquationFlatTypes, types);
then
();
else ();
end match;
end collectEqBranchFlatTypes;
function collectAlgorithmFlatTypes
input Algorithm alg;
input output TypeTree types;
algorithm
types := List.fold(alg.statements, collectStatementFlatTypes, types);
end collectAlgorithmFlatTypes;
function collectStatementFlatTypes
input Statement stmt;
input output TypeTree types;
algorithm
() := match stmt
case Statement.ASSIGNMENT()
algorithm
types := collectExpFlatTypes(stmt.lhs, types);
types := collectExpFlatTypes(stmt.rhs, types);
types := collectFlatType(stmt.ty, types);
then
();
case Statement.FOR()
algorithm
types := List.fold(stmt.body, collectStatementFlatTypes, types);
types := collectExpFlatTypes(Util.getOption(stmt.range), types);
then
();
case Statement.IF()
algorithm
types := List.fold(stmt.branches, collectStmtBranchFlatTypes, types);
then
();
case Statement.WHEN()
algorithm
types := List.fold(stmt.branches, collectStmtBranchFlatTypes, types);
then
();
case Statement.ASSERT()
algorithm
types := collectExpFlatTypes(stmt.condition, types);
types := collectExpFlatTypes(stmt.message, types);
types := collectExpFlatTypes(stmt.level, types);
then
();
case Statement.TERMINATE()
algorithm
types := collectExpFlatTypes(stmt.message, types);
then
();
case Statement.NORETCALL()
algorithm
types := collectExpFlatTypes(stmt.exp, types);
then
();
case Statement.WHILE()
algorithm
types := collectExpFlatTypes(stmt.condition, types);
types := List.fold(stmt.body, collectStatementFlatTypes, types);
then
();
else ();
end match;
end collectStatementFlatTypes;
function collectStmtBranchFlatTypes
input tuple<Expression, list<Statement>> branch;
input output TypeTree types;
algorithm
types := collectExpFlatTypes(Util.tuple21(branch), types);
types := List.fold(Util.tuple22(branch), collectStatementFlatTypes, types);
end collectStmtBranchFlatTypes;
function collectExpFlatTypes
input Expression exp;
input output TypeTree types;
algorithm
types := Expression.fold(exp, collectExpFlatTypes_traverse, types);
end collectExpFlatTypes;
function collectExpFlatTypes_traverse
input Expression exp;
input output TypeTree types;
algorithm
types := match exp
case Expression.SUBSCRIPTED_EXP()
algorithm
types := collectSubscriptedFlatType(exp.exp, exp.subscripts, exp.ty, types);
then
types;
else collectFlatType(Expression.typeOf(exp), types);
end match;
end collectExpFlatTypes_traverse;
function collectFunctionFlatTypes
input Function fn;
input output TypeTree types;
protected
list<Statement> body;
algorithm
types := ClassTree.foldComponents(Class.classTree(InstNode.getClass(fn.node)),
collectComponentFlatTypes, types);
if not Function.isExternal(fn) then
body := Function.getBody(fn);
types := List.fold(body, collectStatementFlatTypes, types);
end if;
end collectFunctionFlatTypes;
function collectComponentFlatTypes
input InstNode component;
input output TypeTree types;
protected
Component comp;
algorithm
comp := InstNode.component(component);
types := collectFlatType(Component.getType(comp), types);
types := collectBindingFlatTypes(Component.getBinding(comp), types);
end collectComponentFlatTypes;
function collectSubscriptedFlatType
input Expression exp;
input list<Subscript> subs;
input Type subscriptedTy;
input output TypeTree types;
protected
Type exp_ty;
list<Type> sub_tyl;
list<Dimension> dims;
list<String> strl;
String name;
algorithm
exp_ty := Expression.typeOf(exp);
dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs));
sub_tyl := list(Dimension.subscriptType(d) for d in dims);
name := Type.subscriptedTypeName(exp_ty, sub_tyl);
types := TypeTree.add(types, Absyn.IDENT(name), Type.SUBSCRIPTED(name, exp_ty, sub_tyl, subscriptedTy));
end collectSubscriptedFlatType;
function reconstructRecordInstances
input list<Variable> variables;
output list<Variable> outVariables = {};
protected
list<Variable> rest_vars = variables, record_vars;
Variable var;
ComponentRef parent_cr;
Type parent_ty;
Integer field_count;
algorithm
while not listEmpty(rest_vars) loop
var :: rest_vars := rest_vars;
parent_cr := ComponentRef.rest(var.name);
if not ComponentRef.isEmpty(parent_cr) then
parent_ty := ComponentRef.nodeType(parent_cr);
if Type.isRecord(parent_ty) then
field_count := listLength(Type.recordFields(parent_ty));
(record_vars, rest_vars) := List.split(rest_vars, field_count - 1);
record_vars := var :: record_vars;
var := reconstructRecordInstance(parent_cr, record_vars);
end if;
end if;
outVariables := var :: outVariables;
end while;
outVariables := listReverseInPlace(outVariables);
end reconstructRecordInstances;
function reconstructRecordInstance
input ComponentRef recordName;
input list<Variable> variables;
output Variable recordVar;
protected
InstNode record_node;
Component record_comp;
Type record_ty;
list<Expression> field_exps;
Expression record_exp;
Binding record_binding;
algorithm
record_node := ComponentRef.node(recordName);
record_comp := InstNode.component(record_node);
record_ty := ComponentRef.nodeType(recordName);
// Reconstruct the record instance binding if possible. If any field is
// missing a binding we assume that the record instance didn't have a
// binding in the first place, or that the binding was moved to an equation
// during flattening.
field_exps := {};
for v in variables loop
if Binding.hasExp(v.binding) then
field_exps := Binding.getExp(v.binding) :: field_exps;
else
field_exps := {};
break;
end if;
end for;
if listEmpty(field_exps) then
record_binding := NFBinding.EMPTY_BINDING;
else
field_exps := listReverseInPlace(field_exps);
record_exp := Expression.makeRecord(InstNode.scopePath(InstNode.classScope(record_node)), record_ty, field_exps);
record_binding := Binding.FLAT_BINDING(record_exp, Component.variability(record_comp));
end if;
recordVar := Variable.VARIABLE(recordName, record_ty, record_binding, InstNode.visibility(record_node),
Component.getAttributes(record_comp), {}, Component.comment(record_comp), InstNode.info(record_node));
end reconstructRecordInstance;
annotation(__OpenModelica_Interface="frontend");
end NFFlatModel;