-
Notifications
You must be signed in to change notification settings - Fork 299
/
NFAttributes.mo
629 lines (564 loc) · 20.7 KB
/
NFAttributes.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
/*
* 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 NFAttributes
import InstContext = NFInstContext;
import NFInstNode.InstNode;
import NFPrefixes.*;
import SCode;
import Restriction = NFRestriction;
protected
import Class = NFClass;
import IOStream;
import Prefixes = NFPrefixes;
import SCodeUtil;
import Attributes = NFAttributes;
public
constant Attributes DEFAULT_ATTR =
ATTRIBUTES(
ConnectorType.NON_CONNECTOR,
Parallelism.NON_PARALLEL,
Variability.CONTINUOUS,
Direction.NONE,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);
constant Attributes INPUT_ATTR =
ATTRIBUTES(
ConnectorType.NON_CONNECTOR,
Parallelism.NON_PARALLEL,
Variability.CONTINUOUS,
Direction.INPUT,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);
constant Attributes OUTPUT_ATTR =
ATTRIBUTES(
ConnectorType.NON_CONNECTOR,
Parallelism.NON_PARALLEL,
Variability.CONTINUOUS,
Direction.OUTPUT,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);
constant Attributes CONSTANT_ATTR =
ATTRIBUTES(
ConnectorType.NON_CONNECTOR,
Parallelism.NON_PARALLEL,
Variability.CONSTANT,
Direction.NONE,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);
constant Attributes IMPL_DISCRETE_ATTR =
ATTRIBUTES(
ConnectorType.NON_CONNECTOR,
Parallelism.NON_PARALLEL,
Variability.IMPLICITLY_DISCRETE,
Direction.NONE,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);
record ATTRIBUTES
ConnectorType.Type connectorType;
Parallelism parallelism;
Variability variability;
Direction direction;
InnerOuter innerOuter;
Boolean isFinal;
Boolean isRedeclare;
Replaceable isReplaceable;
end ATTRIBUTES;
function fromSCode
input SCode.Attributes compAttr;
input SCode.Prefixes compPrefs;
output Attributes attributes;
protected
ConnectorType.Type cty;
Parallelism par;
Variability var;
Direction dir;
InnerOuter io;
Boolean fin, redecl;
Replaceable repl;
algorithm
attributes := match (compAttr, compPrefs)
case (SCode.Attributes.ATTR(
connectorType = SCode.ConnectorType.POTENTIAL(),
parallelism = SCode.Parallelism.NON_PARALLEL(),
variability = SCode.Variability.VAR(),
direction = Absyn.Direction.BIDIR()),
SCode.Prefixes.PREFIXES(
redeclarePrefix = SCode.Redeclare.NOT_REDECLARE(),
finalPrefix = SCode.Final.NOT_FINAL(),
innerOuter = Absyn.InnerOuter.NOT_INNER_OUTER(),
replaceablePrefix = SCode.Replaceable.NOT_REPLACEABLE()))
then NFAttributes.DEFAULT_ATTR;
else
algorithm
cty := ConnectorType.fromSCode(compAttr.connectorType);
par := Prefixes.parallelismFromSCode(compAttr.parallelism);
var := Prefixes.variabilityFromSCode(compAttr.variability);
dir := Prefixes.directionFromSCode(compAttr.direction);
io := Prefixes.innerOuterFromSCode(compPrefs.innerOuter);
fin := SCodeUtil.finalBool(compPrefs.finalPrefix);
redecl := SCodeUtil.redeclareBool(compPrefs.redeclarePrefix);
repl := Replaceable.NOT_REPLACEABLE();
then
Attributes.ATTRIBUTES(cty, par, var, dir, io, fin, redecl, repl);
end match;
end fromSCode;
function fromDerivedSCode
input SCode.Attributes scodeAttr;
output Attributes attributes;
protected
ConnectorType.Type cty;
Variability var;
Direction dir;
algorithm
attributes := match scodeAttr
case SCode.Attributes.ATTR(
connectorType = SCode.ConnectorType.POTENTIAL(),
variability = SCode.Variability.VAR(),
direction = Absyn.Direction.BIDIR())
then DEFAULT_ATTR;
else
algorithm
cty := ConnectorType.fromSCode(scodeAttr.connectorType);
var := Prefixes.variabilityFromSCode(scodeAttr.variability);
dir := Prefixes.directionFromSCode(scodeAttr.direction);
then
ATTRIBUTES(cty, Parallelism.NON_PARALLEL,
var, dir, InnerOuter.NOT_INNER_OUTER, false, false, Replaceable.NOT_REPLACEABLE());
end match;
end fromDerivedSCode;
function mergeComponentAttributes
input Attributes outerAttr;
input Attributes innerAttr;
input InstNode node;
input Restriction parentRestriction;
output Attributes attr;
protected
ConnectorType.Type cty;
Parallelism par;
Variability var;
Direction dir;
Boolean fin, redecl;
Replaceable repl;
algorithm
if referenceEq(outerAttr, NFAttributes.DEFAULT_ATTR) and innerAttr.connectorType == 0 then
attr := innerAttr;
elseif referenceEq(innerAttr, NFAttributes.DEFAULT_ATTR) then
cty := ConnectorType.merge(outerAttr.connectorType, innerAttr.connectorType, node);
attr := Attributes.ATTRIBUTES(cty, outerAttr.parallelism,
outerAttr.variability, outerAttr.direction, innerAttr.innerOuter, outerAttr.isFinal,
innerAttr.isRedeclare, innerAttr.isReplaceable);
else
cty := ConnectorType.merge(outerAttr.connectorType, innerAttr.connectorType, node);
par := Prefixes.mergeParallelism(outerAttr.parallelism, innerAttr.parallelism, node);
var := Prefixes.variabilityMin(outerAttr.variability, innerAttr.variability);
if Restriction.isFunction(parentRestriction) then
dir := innerAttr.direction;
else
dir := Prefixes.mergeDirection(outerAttr.direction, innerAttr.direction, node);
end if;
fin := outerAttr.isFinal or innerAttr.isFinal;
redecl := innerAttr.isRedeclare;
repl := innerAttr.isReplaceable;
attr := Attributes.ATTRIBUTES(cty, par, var, dir, innerAttr.innerOuter, fin, redecl, repl);
end if;
end mergeComponentAttributes;
function mergeDerivedAttributes
input Attributes outerAttr;
input Attributes innerAttr;
input InstNode node;
output Attributes attr;
protected
ConnectorType.Type cty;
Parallelism par;
Variability var;
Direction dir;
InnerOuter io;
Boolean fin, redecl;
Replaceable repl;
algorithm
if referenceEq(innerAttr, NFAttributes.DEFAULT_ATTR) and outerAttr.connectorType == 0 then
attr := outerAttr;
elseif referenceEq(outerAttr, NFAttributes.DEFAULT_ATTR) and innerAttr.connectorType == 0 then
attr := innerAttr;
else
Attributes.ATTRIBUTES(cty, par, var, dir, io, fin, redecl, repl) := outerAttr;
cty := ConnectorType.merge(cty, innerAttr.connectorType, node, isClass = true);
var := Prefixes.variabilityMin(var, innerAttr.variability);
dir := Prefixes.mergeDirection(dir, innerAttr.direction, node, allowSame = true);
attr := Attributes.ATTRIBUTES(cty, par, var, dir, innerAttr.innerOuter, fin, redecl, repl);
end if;
end mergeDerivedAttributes;
function mergeRedeclaredComponentAttributes
input Attributes origAttr;
input Attributes redeclAttr;
input InstNode node;
output Attributes attr;
protected
ConnectorType.Type cty, rcty, cty_fs, rcty_fs;
Parallelism par, rpar;
Variability var, rvar;
Direction dir, rdir;
InnerOuter io, rio;
Boolean fin;
Boolean redecl;
Replaceable repl;
algorithm
if referenceEq(origAttr, NFAttributes.DEFAULT_ATTR) then
attr := redeclAttr;
elseif referenceEq(redeclAttr, NFAttributes.DEFAULT_ATTR) then
attr := origAttr;
else
Attributes.ATTRIBUTES(cty, par, var, dir, io, _, _, _) := origAttr;
Attributes.ATTRIBUTES(rcty, rpar, rvar, rdir, rio, fin, redecl, repl) := redeclAttr;
// If no prefix is given for one of these attributes in the redeclaration,
// then the one from the original declaration is used. The redeclare is not
// allowed to change an existing prefix on the original declaration, except
// for the variability which can be lowered (e.g. parameter -> constant) and
// final which is always taken from the redeclare (since redeclaring a final
// element isn't allowed).
rcty_fs := intBitAnd(rcty, ConnectorType.FLOW_STREAM_MASK);
cty_fs := intBitAnd(cty, ConnectorType.FLOW_STREAM_MASK);
if rcty_fs > 0 then
if cty_fs > 0 and rcty_fs <> cty_fs then
printRedeclarePrefixError(node, ConnectorType.toString(rcty), ConnectorType.toString(cty));
end if;
end if;
cty := intBitOr(rcty, cty_fs);
if rpar <> Parallelism.NON_PARALLEL then
if par <> Parallelism.NON_PARALLEL and par <> rpar then
printRedeclarePrefixError(node, Prefixes.parallelismString(rpar), Prefixes.parallelismString(par));
end if;
par := rpar;
end if;
if rvar <> Variability.CONTINUOUS then
if rvar > var then
printRedeclarePrefixError(node, Prefixes.variabilityString(rvar), Prefixes.variabilityString(var));
end if;
var := rvar;
end if;
if rdir <> Direction.NONE then
if dir <> Direction.NONE and rdir <> dir then
printRedeclarePrefixError(node, Prefixes.directionString(rdir), Prefixes.directionString(dir));
end if;
dir := rdir;
end if;
if rio <> InnerOuter.NOT_INNER_OUTER then
if io <> InnerOuter.NOT_INNER_OUTER and rio <> io then
printRedeclarePrefixError(node, Prefixes.innerOuterString(rio), Prefixes.innerOuterString(io));
end if;
io := rio;
end if;
attr := Attributes.ATTRIBUTES(cty, par, var, dir, io, fin, redecl, repl);
end if;
end mergeRedeclaredComponentAttributes;
function mergeRedeclaredClassPrefixes
input Class.Prefixes origPrefs;
input Class.Prefixes redeclPrefs;
input InstNode node;
output Class.Prefixes prefs;
protected
SCode.Encapsulated enc;
SCode.Partial par;
SCode.Final fin;
Absyn.InnerOuter io, rio;
SCode.Replaceable repl;
algorithm
if referenceEq(origPrefs, NFClass.DEFAULT_PREFIXES) then
prefs := redeclPrefs;
else
Class.Prefixes.PREFIXES(innerOuter = io) := origPrefs;
Class.Prefixes.PREFIXES(enc, par, fin, rio, repl) := redeclPrefs;
io := match (io, rio)
case (Absyn.InnerOuter.NOT_INNER_OUTER(), _) then rio;
case (_, Absyn.InnerOuter.NOT_INNER_OUTER()) then io;
case (Absyn.InnerOuter.INNER(), Absyn.InnerOuter.INNER()) then io;
case (Absyn.InnerOuter.OUTER(), Absyn.InnerOuter.OUTER()) then io;
case (Absyn.InnerOuter.INNER_OUTER(), Absyn.InnerOuter.INNER_OUTER()) then io;
else
algorithm
printRedeclarePrefixError(node,
Prefixes.innerOuterString(Prefixes.innerOuterFromSCode(rio)),
Prefixes.innerOuterString(Prefixes.innerOuterFromSCode(io)));
then
fail();
end match;
prefs := Class.Prefixes.PREFIXES(enc, par, fin, io, repl);
end if;
end mergeRedeclaredClassPrefixes;
function printRedeclarePrefixError
input InstNode node;
input String prefix1;
input String prefix2;
algorithm
Error.addSourceMessageAndFail(Error.REDECLARE_MISMATCHED_PREFIX,
{prefix1, InstNode.name(node), prefix2}, InstNode.info(node));
end printRedeclarePrefixError;
function checkDeclaredComponentAttributes
input output Attributes attr;
input Restriction parentRestriction;
input InstNode component;
algorithm
() := match parentRestriction
case Restriction.CONNECTOR()
algorithm
// Components of a connector may not have prefixes 'inner' or 'outer'.
assertNotInnerOuter(attr.innerOuter, component, parentRestriction);
if parentRestriction.isExpandable then
// Components of an expandable connector may not have the prefix 'flow'.
assertNotFlowStream(attr.connectorType, component, parentRestriction);
// Mark components in expandable connectors as potentially present.
attr.connectorType := intBitOr(attr.connectorType, ConnectorType.POTENTIALLY_PRESENT);
end if;
then
();
case Restriction.RECORD()
algorithm
// Elements of a record may not have prefixes 'input', 'output', 'inner', 'outer', 'stream', or 'flow'.
assertNotInputOutput(attr.direction, component, parentRestriction);
assertNotInnerOuter(attr.innerOuter, component, parentRestriction);
assertNotFlowStream(attr.connectorType, component, parentRestriction);
then
();
else ();
end match;
end checkDeclaredComponentAttributes;
function invalidComponentPrefixError
input String prefix;
input InstNode node;
input Restriction restriction;
algorithm
Error.addSourceMessage(Error.INVALID_COMPONENT_PREFIX,
{prefix, InstNode.name(node), Restriction.toString(restriction)}, InstNode.info(node));
end invalidComponentPrefixError;
function assertNotInputOutput
input Direction dir;
input InstNode node;
input Restriction restriction;
algorithm
if dir <> Direction.NONE then
invalidComponentPrefixError(Prefixes.directionString(dir), node, restriction);
fail();
end if;
end assertNotInputOutput;
function assertNotInnerOuter
input InnerOuter io;
input InstNode node;
input Restriction restriction;
algorithm
if io <> InnerOuter.NOT_INNER_OUTER then
invalidComponentPrefixError(Prefixes.innerOuterString(io), node, restriction);
fail();
end if;
end assertNotInnerOuter;
function assertNotFlowStream
input ConnectorType.Type cty;
input InstNode node;
input Restriction restriction;
algorithm
if ConnectorType.isFlowOrStream(cty) then
invalidComponentPrefixError(ConnectorType.toString(cty), node, restriction);
fail();
end if;
end assertNotFlowStream;
function updateComponentConnectorType
input output Attributes attributes;
input Restriction restriction;
input InstContext.Type context;
input InstNode component;
protected
ConnectorType.Type cty = attributes.connectorType;
algorithm
if ConnectorType.isConnectorType(cty) then
if Restriction.isConnector(restriction) then
if Restriction.isExpandableConnector(restriction) then
cty := ConnectorType.setPresent(cty);
else
cty := intBitAnd(cty, intBitNot(ConnectorType.EXPANDABLE));
end if;
else
// The connector type might have the connector or expandable bits set
// because of a parent node, but they should be unset if the component
// itself isn't a connector.
cty := intBitAnd(cty,
intBitNot(intBitOr(ConnectorType.CONNECTOR, ConnectorType.EXPANDABLE)));
end if;
// Connector elements that are not flow/stream are potentials.
if not ConnectorType.isFlowOrStream(cty) then
cty := ConnectorType.setPotential(cty);
end if;
if cty <> attributes.connectorType then
attributes.connectorType := cty;
end if;
elseif ConnectorType.isFlowOrStream(cty) and not InstContext.inRedeclared(context) then
// The Modelica specification forbids using stream outside connector
// declarations, but has no such restriction for flow. To compromise we
// print a warning for both flow and stream.
Error.addStrictMessage(Error.CONNECTOR_PREFIX_OUTSIDE_CONNECTOR,
{ConnectorType.toString(cty)}, InstNode.info(component));
// Remove the erroneous flow/stream prefix and keep going.
attributes.connectorType := ConnectorType.unsetFlowStream(cty);
end if;
end updateComponentConnectorType;
function updateClassConnectorType
input Restriction res;
input output Attributes attrs;
algorithm
if Restriction.isExpandableConnector(res) then
attrs.connectorType := ConnectorType.setExpandable(attrs.connectorType);
elseif Restriction.isConnector(res) then
attrs.connectorType := ConnectorType.setConnector(attrs.connectorType);
end if;
end updateClassConnectorType;
function updateVariability
"Updates the variability based on the type of the attributes' owner (e.g.
Integer is implicitly discrete)."
input output Attributes attr;
input Class cls;
input InstNode clsNode;
input InstNode compNode;
input InstContext.Type context;
protected
Variability var = attr.variability;
algorithm
if referenceEq(attr, NFAttributes.DEFAULT_ATTR) and InstNode.isDiscreteClass(clsNode) then
attr := NFAttributes.IMPL_DISCRETE_ATTR;
elseif var == Variability.CONTINUOUS and InstNode.isDiscreteClass(clsNode) then
attr.variability := Variability.IMPLICITLY_DISCRETE;
elseif var < Variability.CONTINUOUS and InstContext.inFunction(context) and
attr.direction <> Direction.NONE and
isNone(InstNode.getAnnotation("__OpenModelica_functionVariability", compNode)) then
// Variability prefixes on function parameters has no semantic meaning,
// remove them so we don't have to worry about accidentally evaluating
// e.g. an input declared as constant/parameter.
attr.variability := Variability.CONTINUOUS;
end if;
end updateVariability;
function setConnectorType
input ConnectorType.Type cty;
input output Attributes attr;
algorithm
attr.connectorType := cty;
end setConnectorType;
function setVariability
input Variability var;
input output Attributes attr;
algorithm
attr.variability := var;
end setVariability;
function setDirection
input Direction dir;
input output Attributes attr;
algorithm
attr.direction := dir;
end setDirection;
function setInnerOuter
input InnerOuter io;
input output Attributes attr;
algorithm
attr.innerOuter := io;
end setInnerOuter;
function setFinal
input Boolean fin;
input output Attributes attr;
algorithm
attr.isFinal := fin;
end setFinal;
function setRedeclare
input Boolean redecl;
input output Attributes attr;
algorithm
attr.isRedeclare := redecl;
end setRedeclare;
function setReplaceable
input Replaceable repl;
input output Attributes attr;
algorithm
attr.isReplaceable := repl;
end setReplaceable;
function toDAE
input Attributes ina;
input Visibility vis;
output DAE.Attributes outa;
algorithm
outa := DAE.ATTR(
ConnectorType.toDAE(ina.connectorType),
parallelismToSCode(ina.parallelism),
variabilityToSCode(ina.variability),
directionToAbsyn(ina.direction),
innerOuterToAbsyn(ina.innerOuter),
visibilityToSCode(vis)
);
end toDAE;
function toString
input Attributes attr;
input Type ty;
output String str;
algorithm
str := (if attr.isRedeclare then "redeclare " else "") +
(if attr.isFinal then "final " else "") +
Prefixes.unparseInnerOuter(attr.innerOuter) +
Prefixes.unparseReplaceable(attr.isReplaceable) +
Prefixes.unparseParallelism(attr.parallelism) +
ConnectorType.unparse(attr.connectorType) +
Prefixes.unparseVariability(attr.variability, ty) +
Prefixes.unparseDirection(attr.direction);
end toString;
function toFlatStream
input Attributes attr;
input Type ty;
input output IOStream.IOStream s;
input Boolean isTopLevel = true;
algorithm
if attr.isFinal then
s := IOStream.append(s, "final ");
end if;
s := IOStream.append(s, Prefixes.unparseVariability(attr.variability, ty));
if isTopLevel then
s := IOStream.append(s, Prefixes.unparseDirection(attr.direction));
end if;
end toFlatStream;
annotation(__OpenModelica_Interface="frontend");
end NFAttributes;