/
NFClass.mo
424 lines (375 loc) · 11.1 KB
/
NFClass.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
/*
* 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 NFClass
import BaseAvlTree;
import NFEquation.Equation;
import NFInstNode.InstNode;
import NFMod.Modifier;
import NFStatement.Statement;
import SCode.Element;
import Type = NFType;
import Array;
import Error;
encapsulated package ClassTree
uniontype Entry
record CLASS
InstNode node;
end CLASS;
record COMPONENT
Integer node;
Integer index;
end COMPONENT;
end Entry;
import BaseAvlTree;
import NFInstNode.InstNode;
extends BaseAvlTree(redeclare type Key = String,
redeclare type Value = Entry);
redeclare function extends keyStr
algorithm
outString := inKey;
end keyStr;
redeclare function extends valueStr
algorithm
outString := match inValue
case Entry.CLASS() then "class " + InstNode.name(inValue.node);
case Entry.COMPONENT() then "comp " + String(inValue.index);
end match;
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := stringCompare(inKey1, inKey2);
end keyCompare;
annotation(__OpenModelica_Interface="util");
end ClassTree;
uniontype Class
record NOT_INSTANTIATED end NOT_INSTANTIATED;
record PARTIAL_CLASS
ClassTree.Tree classes;
list<SCode.Element> elements;
Modifier modifier;
end PARTIAL_CLASS;
record EXPANDED_CLASS
ClassTree.Tree elements;
array<InstNode> extendsNodes;
array<InstNode> components;
Modifier modifier;
list<Equation> equations;
list<Equation> initialEquations;
list<list<Statement>> algorithms;
list<list<Statement>> initialAlgorithms;
end EXPANDED_CLASS;
record INSTANCED_CLASS
ClassTree.Tree elements;
array<InstNode> components;
list<Equation> equations;
list<Equation> initialEquations;
list<list<Statement>> algorithms;
list<list<Statement>> initialAlgorithms;
end INSTANCED_CLASS;
record PARTIAL_BUILTIN
Type ty;
Modifier modifier;
end PARTIAL_BUILTIN;
record INSTANCED_BUILTIN
Type ty;
list<Modifier> attributes;
end INSTANCED_BUILTIN;
type Element = ClassTree.Entry;
function emptyInstancedClass
output Class cls;
algorithm
cls := INSTANCED_CLASS(ClassTree.new(), listArray({}), {}, {}, {}, {});
end emptyInstancedClass;
function initExpandedClass
input ClassTree.Tree classes;
output Class cls;
algorithm
cls := EXPANDED_CLASS(classes, listArray({}), listArray({}), Modifier.NOMOD(), {}, {}, {}, {});
end initExpandedClass;
function instExpandedClass
input array<InstNode> components;
input Class expandedClass;
output Class instancedClass;
protected
list<Equation> eqs;
list<Equation> ieqs;
list<list<Statement>> algs;
list<list<Statement>> ialgs;
algorithm
instancedClass := match expandedClass
case EXPANDED_CLASS()
algorithm
eqs := expandedClass.equations;
ieqs := expandedClass.initialEquations;
algs := expandedClass.algorithms;
ialgs := expandedClass.initialAlgorithms;
// ***TODO***: Sections should *not* be appended here, they need to be
// instantiated and typed in the correct scope. They should be
// collected from the extends nodes when flattening the class.
then
INSTANCED_CLASS(expandedClass.elements, components, eqs, ieqs, algs, ialgs);
end match;
end instExpandedClass;
function collectInherited
input InstNode cls;
input list<Equation> i_eqs;
input list<Equation> i_ieqs;
input list<list<Statement>> i_algs;
input list<list<Statement>> i_ialgs;
output list<Equation> eqs;
output list<Equation> ieqs;
output list<list<Statement>> algs;
output list<list<Statement>> ialgs;
protected
Class inheritedClass;
algorithm
inheritedClass := InstNode.getClass(cls);
(eqs, ieqs, algs, ialgs) :=
match inheritedClass
case EXPANDED_CLASS()
algorithm
eqs := listAppend(i_eqs, inheritedClass.equations);
ieqs := listAppend(i_ieqs, inheritedClass.initialEquations);
algs := listAppend(i_algs, inheritedClass.algorithms);
ialgs := listAppend(i_ialgs, inheritedClass.initialAlgorithms);
for ext in inheritedClass.extendsNodes loop
(eqs, ieqs, algs, ialgs) := collectInherited(ext, eqs, ieqs, algs, ialgs);
end for;
then
(eqs, ieqs, algs, ialgs);
end match;
end collectInherited;
function components
input Class cls;
output array<InstNode> components;
algorithm
components := match cls
case EXPANDED_CLASS() then cls.components;
case INSTANCED_CLASS() then cls.components;
end match;
end components;
function setComponents
input array<InstNode> components;
input output Class cls;
algorithm
_ := match cls
case EXPANDED_CLASS()
algorithm
cls.components := components;
then
();
case INSTANCED_CLASS()
algorithm
cls.components := components;
then
();
end match;
end setComponents;
function elements
input Class cls;
output ClassTree.Tree els;
algorithm
els := match cls
case EXPANDED_CLASS() then cls.elements;
case INSTANCED_CLASS() then cls.elements;
end match;
end elements;
function setElements
input ClassTree.Tree elements;
input output Class cls;
algorithm
_ := match cls
case EXPANDED_CLASS()
algorithm
cls.elements := elements;
then
();
case INSTANCED_CLASS()
algorithm
cls.elements := elements;
then
();
end match;
end setElements;
function extendsNodes
input Class cls;
output array<InstNode> extendsNodes;
algorithm
EXPANDED_CLASS(extendsNodes = extendsNodes) := cls;
end extendsNodes;
function setSections
input list<Equation> equations;
input list<Equation> initialEquations;
input list<list<Statement>> algorithms;
input list<list<Statement>> initialAlgorithms;
input output Class cls;
algorithm
cls := match cls
case EXPANDED_CLASS()
then EXPANDED_CLASS(cls.elements, cls.extendsNodes, cls.components,
cls.modifier, equations, initialEquations, algorithms, initialAlgorithms);
case INSTANCED_CLASS()
then INSTANCED_CLASS(cls.elements, cls.components, equations,
initialEquations, algorithms, initialAlgorithms);
end match;
end setSections;
function lookupElement
input String name;
input Class cls;
output InstNode node;
protected
ClassTree.Tree scope;
Class.Element element;
algorithm
scope := match cls
case EXPANDED_CLASS() then cls.elements;
case INSTANCED_CLASS() then cls.elements;
end match;
element := ClassTree.get(scope, name);
node := resolveElement(element, cls);
end lookupElement;
function resolveElement
input Class.Element element;
input Class cls;
output InstNode node;
algorithm
node := match element
case Element.CLASS() then element.node;
case Element.COMPONENT(node = 0)
then arrayGet(components(cls), element.index);
case Element.COMPONENT()
then arrayGet(components(InstNode.getClass(
arrayGet(extendsNodes(cls), element.node))), element.index);
end match;
end resolveElement;
function isBuiltin
input Class cls;
output Boolean isBuiltin;
algorithm
isBuiltin := match cls
case PARTIAL_BUILTIN() then true;
case INSTANCED_BUILTIN() then true;
else false;
end match;
end isBuiltin;
function setModifier
input Modifier modifier;
input output Class cls;
algorithm
_ := match cls
case PARTIAL_CLASS()
algorithm
cls.modifier := modifier;
then
();
case EXPANDED_CLASS()
algorithm
cls.modifier := modifier;
then
();
case PARTIAL_BUILTIN()
algorithm
cls.modifier := modifier;
then
();
else
algorithm
assert(false, getInstanceName() + " got unmodifiable instance");
then
fail();
end match;
end setModifier;
function getModifier
input Class cls;
output Modifier modifier;
algorithm
modifier := match cls
case PARTIAL_CLASS() then cls.modifier;
case EXPANDED_CLASS() then cls.modifier;
case PARTIAL_BUILTIN() then cls.modifier;
else Modifier.NOMOD();
end match;
end getModifier;
function clone
input output Class cls;
algorithm
() := match cls
local
ClassTree.Tree tree;
case EXPANDED_CLASS()
algorithm
cls.components := Array.map(cls.components, InstNode.clone);
then
();
case INSTANCED_CLASS()
algorithm
cls.components := Array.map(cls.components, InstNode.clone);
then
();
else ();
end match;
end clone;
function cloneEntry
input String name;
input ClassTree.Entry entry;
output ClassTree.Entry clone;
algorithm
clone := match entry
case ClassTree.Entry.CLASS() then ClassTree.Entry.CLASS(InstNode.clone(entry.node));
else entry;
end match;
end cloneEntry;
function resolveExtendsRef
input InstNode ref;
input Class scope;
output InstNode ext;
algorithm
ext := match (ref, scope)
case (InstNode.REF_NODE(), EXPANDED_CLASS())
then arrayGet(scope.extendsNodes, ref.index);
end match;
end resolveExtendsRef;
function updateExtends
input InstNode ref;
input InstNode ext;
input output Class scope;
algorithm
() := match (ref, scope)
case (InstNode.REF_NODE(), EXPANDED_CLASS())
algorithm
arrayUpdate(scope.extendsNodes, ref.index, ext);
then
();
end match;
end updateExtends;
end Class;
annotation(__OpenModelica_Interface="frontend");
end NFClass;