-
Notifications
You must be signed in to change notification settings - Fork 297
/
NFVariable.mo
587 lines (512 loc) · 17.9 KB
/
NFVariable.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
/*
* 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 NFVariable
import Attributes = NFAttributes;
import Binding = NFBinding;
import Class = NFClass;
import Component = NFComponent;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFPrefixes.Visibility;
import NFPrefixes.Variability;
import NFPrefixes.ConnectorType;
import NFPrefixes.Direction;
import NFPrefixes.AccessLevel;
import Type = NFType;
import BackendExtension = NFBackendExtension;
import NFBackendExtension.BackendInfo;
protected
import Ceval = NFCeval;
import ExpandExp = NFExpandExp;
import FlatModelicaUtil = NFFlatModelicaUtil;
import Inst = NFInst;
import IOStream;
import MetaModelica.Dangerous.listReverseInPlace;
import StringUtil;
import Typing = NFTyping;
import Util;
import Variable = NFVariable;
public
record VARIABLE
ComponentRef name;
Type ty;
Binding binding;
Visibility visibility;
Attributes attributes;
list<tuple<String, Binding>> typeAttributes;
list<Variable> children;
Option<SCode.Comment> comment;
SourceInfo info;
BackendInfo backendinfo "NFBackendExtension.DUMMY_BACKEND_INFO for all of frontend. Only used in Backend.";
end VARIABLE;
function fromCref
"creates a variable from a component reference.
Note: does not flatten, do not use for instantiated elements!"
input ComponentRef cref;
output Variable variable;
protected
list<ComponentRef> crefs;
InstNode node, child_node;
Component comp;
Type ty;
Binding binding;
Visibility vis;
Attributes attr;
Option<SCode.Comment> cmt;
SourceInfo info;
BackendExtension.BackendInfo binfo = NFBackendExtension.DUMMY_BACKEND_INFO;
list<Variable> children = {};
Option<Integer> complexSize;
algorithm
node := ComponentRef.node(cref);
comp := InstNode.component(node);
ty := ComponentRef.getSubscriptedType(cref);
vis := InstNode.visibility(node);
attr := Component.getAttributes(comp);
cmt := Component.comment(comp);
info := InstNode.info(node);
// kabdelhak: add dummy backend info, will be changed to actual value in
// conversion to backend process (except for iterators). NBackendDAE.lower
if ComponentRef.isIterator(cref) then
binding := NFBinding.EMPTY_BINDING;
binfo.varKind := BackendExtension.ITERATOR();
else
binding := Component.getImplicitBinding(comp);
end if;
// get the record children if the variable is a record
complexSize := Type.complexSize(ty);
if Util.isSome(complexSize) then
for i in Util.getOption(complexSize):-1:1 loop
child_node := Class.nthComponent(i, InstNode.getClass(node));
children := fromCref(ComponentRef.fromNode(child_node, InstNode.getType(child_node))) :: children;
end for;
end if;
variable := VARIABLE(cref, ty, binding, vis, attr, {}, children, cmt, info, binfo);
end fromCref;
function size
input Variable var;
output Integer s = Type.sizeOf(var.ty);
end size;
function hash
input Variable var;
output Integer i = ComponentRef.hash(var.name);
end hash;
function equalName
input Variable var1;
input Variable var2;
output Boolean b = ComponentRef.isEqual(var1.name, var2.name);
end equalName;
function expand
"Expands an array variable into its scalar elements."
input Variable var;
input Boolean backend = false;
output list<Variable> vars;
protected
list<ComponentRef> crefs;
Variable v;
Binding binding;
Variability bind_var;
Binding.Source bind_src;
Expression bind_exp, exp;
list<Expression> expl;
Integer crefs_len, expl_len;
algorithm
if Type.isArray(var.ty) then
// Expand the name.
exp := Expression.fromCref(var.name);
exp := ExpandExp.expandCref(exp, backend);
expl := Expression.arrayScalarElements(exp);
crefs := list(Expression.toCref(e) for e in expl);
v := var;
v.ty := Type.arrayElementType(v.ty);
vars := {};
binding := var.binding;
// If the variable has a binding we need to expand it too.
if Binding.isBound(binding) then
bind_exp := Binding.getTypedExp(binding);
expl := Expression.arrayScalarElements(ExpandExp.expand(bind_exp));
crefs_len := listLength(crefs);
expl_len := listLength(expl);
// If the binding has fewer dimensions than the variable, 'multiply' the
// list of binding expression until they match.
if expl_len < crefs_len then
if intMod(crefs_len, expl_len) <> 0 then
Error.assertion(false, getInstanceName() + " failed to expand " +
ComponentRef.toString(var.name), sourceInfo());
end if;
expl := List.flatten(List.fill(expl, intDiv(crefs_len, expl_len)));
end if;
bind_var := Binding.variability(binding);
bind_src := Binding.source(binding);
for cr in crefs loop
v.name := cr;
exp :: expl := expl;
v.binding := Binding.makeFlat(exp, bind_var, bind_src);
vars := v :: vars;
end for;
else
for cr in crefs loop
v.name := cr;
vars := v :: vars;
end for;
end if;
vars := listReverseInPlace(vars);
else
vars := {var};
end if;
end expand;
function expandChildren
"Expands a variable into itself and its children if its complex."
input Variable var;
output list<Variable> children;
protected
function expandChildType
"helper function to inherit the array type dimensions"
input output Variable child;
input list<Dimension> dimensions;
algorithm
child.ty := Type.liftArrayLeftList(child.ty, dimensions);
end expandChildType;
algorithm
// for non-complex variables the children are empty therefore it will be returned itself
var.children := List.flatten(list(expandChildren(v) for v in var.children));
if isComplexArray(var) then
// if the variable is an array, inherit the array dimensions
var.children := list(expandChildType(v, Type.arrayDims(var.ty)) for v in var.children);
end if;
// return all children and the variable itself
children := var :: var.children;
end expandChildren;
function typeOf
input Variable var;
output Type ty = var.ty;
end typeOf;
function attributes
input Variable variable;
output Attributes attributes = variable.attributes;
end attributes;
function variability
input Variable variable;
output Variability variability = variable.attributes.variability;
end variability;
function visibility
input Variable variable;
output Visibility visibility = variable.visibility;
end visibility;
function isComplex
input Variable var;
output Boolean b = Type.isComplex(var.ty);
end isComplex;
function isComplexArray
input Variable var;
output Boolean b = Type.isComplexArray(var.ty);
end isComplexArray;
function isStructural
input Variable variable;
output Boolean structural =
variable.attributes.variability <= Variability.STRUCTURAL_PARAMETER;
end isStructural;
function isEmptyArray
input Variable variable;
output Boolean isEmpty = Type.isEmptyArray(variable.ty);
end isEmptyArray;
function isDeleted
input Variable variable;
output Boolean deleted;
protected
InstNode node;
algorithm
node := ComponentRef.node(variable.name);
deleted := InstNode.isComponent(node) and Component.isDeleted(InstNode.component(node));
end isDeleted;
function isPresent
input Variable variable;
output Boolean present = not ConnectorType.isPotentiallyPresent(variable.attributes.connectorType);
end isPresent;
function isPotential
input Variable variable;
output Boolean potential = ConnectorType.isPotential(variable.attributes.connectorType);
end isPotential;
function isFlow
input Variable variable;
output Boolean potential = ConnectorType.isFlow(variable.attributes.connectorType);
end isFlow;
function isStream
input Variable variable;
output Boolean potential = ConnectorType.isStream(variable.attributes.connectorType);
end isStream;
function isInput
input Variable variable;
output Boolean b = variable.attributes.direction == Direction.INPUT;
end isInput;
function isTopLevelInput
input Variable variable;
output Boolean topInput = ComponentRef.isSimple(variable.name) and
variable.attributes.direction == Direction.INPUT;
end isTopLevelInput;
function isPublic
input Variable variable;
output Boolean isPublic = variable.visibility == Visibility.PUBLIC;
end isPublic;
function isProtected
input Variable variable;
output Boolean isProtected = variable.visibility == Visibility.PROTECTED;
end isProtected;
function isEncrypted
input Variable variable;
output Boolean isEncrypted = StringUtil.endsWith(variable.info.fileName, ".moc");
end isEncrypted;
function isAccessible
input Variable variable;
output Boolean isAccessible;
protected
Option<AccessLevel> oaccess;
AccessLevel access;
algorithm
oaccess := InstNode.getAccessLevel(ComponentRef.node(variable.name));
if isSome(oaccess) then
SOME(access) := oaccess;
else
access := if isEncrypted(variable) then AccessLevel.DOCUMENTATION else AccessLevel.PACKAGE_DUPLICATE;
end if;
if access < AccessLevel.ICON then
isAccessible := false;
elseif access < AccessLevel.NON_PACKAGE_TEXT then
isAccessible := not isProtected(variable);
else
isAccessible := true;
end if;
end isAccessible;
function lookupTypeAttribute
input String name;
input Variable var;
output Binding binding;
algorithm
for attr in var.typeAttributes loop
if Util.tuple21(attr) == name then
binding := Util.tuple22(attr);
return;
end if;
end for;
binding := NFBinding.EMPTY_BINDING;
end lookupTypeAttribute;
function propagateAnnotation
input String name;
input Boolean overwrite;
input Boolean evaluate = false;
input output Variable var;
protected
InstNode node;
SCode.Mod mod;
Absyn.Exp aexp;
Expression exp;
protected
SCode.Annotation anno;
InstNode scope;
algorithm
if ComponentRef.isCref(var.name) then
node := ComponentRef.node(var.name);
// InstNode.getAnnotation is recursive and returns the first annotation found.
// if the original is supposed to be overwritten, skip the node itself and look at the parent
if overwrite and InstNode.isComponent(node) then
node := InstNode.parent(node);
end if;
(mod, scope) := InstNode.getAnnotation(name, node);
if not SCodeUtil.isEmptyMod(mod) then
if evaluate then
() := matchcontinue mod
case SCode.Mod.MOD(binding = SOME(aexp))
algorithm
exp := Inst.instExp(aexp, scope, NFInstContext.ANNOTATION, mod.info);
exp := Typing.typeExp(exp, NFInstContext.ANNOTATION, mod.info);
exp := Ceval.evalExp(exp);
mod.binding := SOME(Expression.toAbsyn(exp));
then
();
else ();
end matchcontinue;
end if;
anno := SCode.ANNOTATION(modification = SCode.MOD(
finalPrefix = SCode.NOT_FINAL(),
eachPrefix = SCode.NOT_EACH(),
subModLst = {SCode.SubMod.NAMEMOD(name, mod)},
binding = NONE(),
info = sourceInfo()));
var.comment := SCodeUtil.appendAnnotationToCommentOption(anno, var.comment, true);
end if;
end if;
end propagateAnnotation;
function removeNonTopLevelDirection
"Removes input/output prefixes from a variable that's not a top-level
component, a component in a top-level connector, or a component in a
top-level input component. exposeLocalIOs can be used to keep the direction
for variables at lower levels as well, where 0 means top-level, 1 the level
below that, and so on."
input output Variable var;
input Integer exposeLocalIOs;
protected
ComponentRef rest_name;
InstNode node;
Attributes attr;
algorithm
if var.attributes.direction == Direction.NONE then
return;
end if;
if exposeLocalIOs > 0 and
var.attributes.connectorType <> ConnectorType.NON_CONNECTOR and
var.visibility == Visibility.PUBLIC and
ComponentRef.depth(var.name) < exposeLocalIOs then
return;
end if;
rest_name := ComponentRef.rest(var.name);
while not ComponentRef.isEmpty(rest_name) loop
node := ComponentRef.node(rest_name);
if not (InstNode.isConnector(node) or InstNode.isInput(node)) then
attr := var.attributes;
attr.direction := Direction.NONE;
var.attributes := attr;
return;
end if;
rest_name := ComponentRef.rest(rest_name);
end while;
end removeNonTopLevelDirection;
partial function MapFn
input output Expression exp;
end MapFn;
function mapExp
input output Variable var;
input MapFn fn;
algorithm
var.binding := Binding.mapExp(var.binding, fn);
var.typeAttributes := list(
(Util.tuple21(a), Binding.mapExp(Util.tuple22(a), fn)) for a in var.typeAttributes);
var.children := list(mapExp(v, fn) for v in var.children);
var.backendinfo := BackendExtension.BackendInfo.map(var.backendinfo, fn);
end mapExp;
function mapExpShallow
input output Variable var;
input MapFn fn;
algorithm
var.binding := Binding.mapExpShallow(var.binding, fn);
var.typeAttributes := list(
(Util.tuple21(a), Binding.mapExpShallow(Util.tuple22(a), fn)) for a in var.typeAttributes);
var.children := list(mapExpShallow(v, fn) for v in var.children);
end mapExpShallow;
function toString
input Variable var;
input String indent = "";
input Boolean printBindingType = false;
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toStream(var, indent, printBindingType, s);
str := IOStream.string(s);
IOStream.delete(s);
end toString;
function toStream
input Variable var;
input String indent = "";
input Boolean printBindingType = false;
input output IOStream.IOStream s;
protected
Boolean first;
Binding b;
algorithm
s := IOStream.append(s, indent);
if var.visibility == Visibility.PROTECTED then
s := IOStream.append(s, "protected ");
end if;
s := IOStream.append(s, Attributes.toString(var.attributes, var.ty));
s := IOStream.append(s, Type.toString(var.ty));
s := IOStream.append(s, " ");
s := IOStream.append(s, ComponentRef.toString(var.name));
if not listEmpty(var.typeAttributes) then
s := IOStream.append(s, "(");
first := true;
for a in var.typeAttributes loop
if first then
first := false;
else
s := IOStream.append(s, ", ");
end if;
b := Util.tuple22(a);
if Binding.isEach(b) then
s := IOStream.append(s, "each ");
end if;
s := IOStream.append(s, Util.tuple21(a));
s := IOStream.append(s, " = ");
s := IOStream.append(s, Binding.toString(b));
end for;
s := IOStream.append(s, ")");
end if;
if Binding.isBound(var.binding) then
s := IOStream.append(s, " = ");
if printBindingType then
s := IOStream.append(s, "(");
s := IOStream.append(s, Type.toString(Binding.getType(var.binding)));
s := IOStream.append(s, ") ");
end if;
s := IOStream.append(s, Binding.toString(var.binding));
end if;
end toStream;
function toFlatStream
input Variable var;
input String indent = "";
input Boolean printBindingType = false;
input output IOStream.IOStream s;
protected
Boolean first;
Binding b;
Integer var_dims, binding_dims;
algorithm
s := IOStream.append(s, indent);
s := Attributes.toFlatStream(var.attributes, var.ty, s, ComponentRef.isSimple(var.name));
s := IOStream.append(s, Type.toFlatString(var.ty));
s := IOStream.append(s, " ");
s := IOStream.append(s, ComponentRef.toFlatString(var.name));
s := Component.typeAttrsToFlatStream(var.typeAttributes, var.ty, s);
if Binding.isBound(var.binding) then
s := IOStream.append(s, " = ");
if printBindingType then
s := IOStream.append(s, "(");
s := IOStream.append(s, Type.toFlatString(Binding.getType(var.binding)));
s := IOStream.append(s, ") ");
end if;
s := IOStream.append(s, Binding.toFlatString(var.binding));
end if;
s := FlatModelicaUtil.appendComment(var.comment, s);
end toFlatStream;
annotation(__OpenModelica_Interface="frontend");
end NFVariable;