This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
NFBinding.mo
542 lines (475 loc) · 13.5 KB
/
NFBinding.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
/*
* 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 NFBinding
public
import Expression = NFExpression;
import NFInstNode.InstNode;
import SCode;
import Type = NFType;
import NFPrefixes.Variability;
protected
import Dump;
public
constant Binding EMPTY_BINDING = Binding.UNBOUND({}, false, Absyn.dummyInfo);
uniontype Binding
record UNBOUND
// NOTE: Use the EMPTY_BINDING constant above when a default unbound binding
// is needed, to save memory. UNBOUND contains this information to be
// able to check that 'each' is used correctly, so parents and info
// are only needed when isEach is true.
list<InstNode> parents;
Boolean isEach;
SourceInfo info;
end UNBOUND;
record RAW_BINDING
Absyn.Exp bindingExp;
InstNode scope;
list<InstNode> parents;
Boolean isEach;
SourceInfo info;
end RAW_BINDING;
record UNTYPED_BINDING
Expression bindingExp;
Boolean isProcessing;
InstNode scope;
list<InstNode> parents;
Boolean isEach;
SourceInfo info;
end UNTYPED_BINDING;
record TYPED_BINDING
Expression bindingExp;
Type bindingType;
Variability variability;
list<InstNode> parents;
Boolean isEach;
SourceInfo info;
end TYPED_BINDING;
record FLAT_BINDING
Expression bindingExp;
Variability variability;
end FLAT_BINDING;
record CEVAL_BINDING
"Used by the constant evaluation for generated bindings (e.g. record
bindings constructed from the record fields) that should be discarded
during flattening."
Expression bindingExp;
list<InstNode> parents;
end CEVAL_BINDING;
public
function fromAbsyn
input Option<Absyn.Exp> bindingExp;
input Boolean eachPrefix;
input list<InstNode> parents;
input InstNode scope;
input SourceInfo info;
output Binding binding;
algorithm
binding := match bindingExp
local
Absyn.Exp exp;
case SOME(exp)
then RAW_BINDING(exp, scope, parents, eachPrefix, info);
else if eachPrefix then UNBOUND(parents, true, info) else NFBinding.EMPTY_BINDING;
end match;
end fromAbsyn;
function isBound
input Binding binding;
output Boolean isBound;
algorithm
isBound := match binding
case UNBOUND() then false;
else true;
end match;
end isBound;
function isExplicitlyBound
input Binding binding;
output Boolean isBound;
algorithm
isBound := match binding
case UNBOUND() then false;
case CEVAL_BINDING() then false;
else true;
end match;
end isExplicitlyBound;
function isUnbound
input Binding binding;
output Boolean isUnbound;
algorithm
isUnbound := match binding
case UNBOUND() then true;
else false;
end match;
end isUnbound;
function untypedExp
input Binding binding;
output Option<Expression> exp;
algorithm
exp := match binding
case UNTYPED_BINDING() then SOME(binding.bindingExp);
else NONE();
end match;
end untypedExp;
function typedExp
input Binding binding;
output Option<Expression> exp;
algorithm
exp := match binding
case TYPED_BINDING() then SOME(binding.bindingExp);
case FLAT_BINDING() then SOME(binding.bindingExp);
else NONE();
end match;
end typedExp;
function getUntypedExp
input Binding binding;
output Expression exp;
algorithm
UNTYPED_BINDING(bindingExp = exp) := binding;
end getUntypedExp;
function getTypedExp
input Binding binding;
output Expression exp;
algorithm
exp := match binding
case TYPED_BINDING() then binding.bindingExp;
case FLAT_BINDING() then binding.bindingExp;
end match;
end getTypedExp;
function setTypedExp
input Expression exp;
input output Binding binding;
algorithm
() := match binding
case TYPED_BINDING()
algorithm
binding.bindingExp := exp;
then
();
case FLAT_BINDING()
algorithm
binding.bindingExp := exp;
then
();
end match;
end setTypedExp;
function getExp
input Binding binding;
output Expression exp;
algorithm
exp := match binding
case UNTYPED_BINDING() then binding.bindingExp;
case TYPED_BINDING() then binding.bindingExp;
case FLAT_BINDING() then binding.bindingExp;
end match;
end getExp;
function setExp
input Expression exp;
input output Binding binding;
algorithm
() := match binding
case UNTYPED_BINDING()
algorithm
binding.bindingExp := exp;
then
();
case TYPED_BINDING()
algorithm
binding.bindingExp := exp;
then
();
case FLAT_BINDING()
algorithm
binding.bindingExp := exp;
then
();
end match;
end setExp;
function isRecordExp
input Binding binding;
output Boolean isRecordExp;
algorithm
isRecordExp := match binding
case TYPED_BINDING(bindingExp = Expression.RECORD()) then true;
else false;
end match;
end isRecordExp;
function variability
input Binding binding;
output Variability var;
algorithm
var := match binding
case TYPED_BINDING() then binding.variability;
case FLAT_BINDING() then binding.variability;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown binding", sourceInfo());
then
fail();
end match;
end variability;
function getInfo
input Binding binding;
output SourceInfo info;
algorithm
info := match binding
case UNBOUND() then binding.info;
case RAW_BINDING() then binding.info;
case UNTYPED_BINDING() then binding.info;
case TYPED_BINDING() then binding.info;
else Absyn.dummyInfo;
end match;
end getInfo;
function getType
input Binding binding;
output Type ty;
algorithm
TYPED_BINDING(bindingType = ty) := binding;
end getType;
function isEach
input Binding binding;
output Boolean isEach;
algorithm
isEach := match binding
case UNBOUND() then binding.isEach;
case RAW_BINDING() then binding.isEach;
case UNTYPED_BINDING() then binding.isEach;
case TYPED_BINDING() then binding.isEach;
else false;
end match;
end isEach;
function isTyped
input Binding binding;
output Boolean isTyped;
algorithm
isTyped := match binding
case TYPED_BINDING() then true;
else false;
end match;
end isTyped;
function parents
input Binding binding;
output list<InstNode> parents;
algorithm
parents := match binding
case UNBOUND() then binding.parents;
case RAW_BINDING() then binding.parents;
case UNTYPED_BINDING() then binding.parents;
case TYPED_BINDING() then binding.parents;
case CEVAL_BINDING() then binding.parents;
else {};
end match;
end parents;
function parentCount
input Binding binding;
output Integer count = listLength(parents(binding));
end parentCount;
function addParent
input InstNode parent;
input output Binding binding;
algorithm
() := match binding
case UNBOUND(isEach = true)
algorithm
binding.parents := parent :: binding.parents;
then
();
case RAW_BINDING()
algorithm
binding.parents := parent :: binding.parents;
then
();
case UNTYPED_BINDING()
algorithm
binding.parents := parent :: binding.parents;
then
();
case TYPED_BINDING()
algorithm
binding.parents := parent :: binding.parents;
then
();
case CEVAL_BINDING()
algorithm
binding.parents := parent :: binding.parents;
then
();
else ();
end match;
end addParent;
function isClassBinding
input Binding binding;
output Boolean isClass;
algorithm
for parent in parents(binding) loop
if InstNode.isClass(parent) then
isClass := true;
return;
end if;
end for;
isClass := false;
end isClassBinding;
function countPropagatedDims
"Returns the number of dimensions that the binding was propagated through to
get to the element it belongs to."
input Binding binding;
output Integer count = 0;
protected
list<InstNode> pars;
algorithm
pars := match binding
case UNTYPED_BINDING(isEach = false) then listRest(binding.parents);
case TYPED_BINDING(isEach = false) then listRest(binding.parents);
else {};
end match;
for parent in pars loop
count := count + Type.dimensionCount(InstNode.getType(parent));
end for;
end countPropagatedDims;
function toString
input Binding binding;
input String prefix = "";
output String string;
algorithm
string := match binding
case UNBOUND() then "";
case RAW_BINDING() then prefix + Dump.printExpStr(binding.bindingExp);
case UNTYPED_BINDING() then prefix + Expression.toString(binding.bindingExp);
case TYPED_BINDING() then prefix + Expression.toString(binding.bindingExp);
end match;
end toString;
function isEqual
input Binding binding1;
input Binding binding2;
output Boolean equal;
algorithm
equal := match (binding1, binding2)
case (UNBOUND(), UNBOUND()) then true;
// TODO: Handle propagated dims.
case (RAW_BINDING(), RAW_BINDING())
then Absyn.expEqual(binding1.bindingExp, binding2.bindingExp);
case (UNTYPED_BINDING(), UNTYPED_BINDING())
then Expression.isEqual(binding1.bindingExp, binding2.bindingExp);
case (TYPED_BINDING(), TYPED_BINDING())
then Expression.isEqual(binding1.bindingExp, binding2.bindingExp);
else false;
end match;
end isEqual;
function toDAE
input Binding binding;
output DAE.Binding outBinding;
algorithm
outBinding := match binding
case UNBOUND() then DAE.UNBOUND();
case TYPED_BINDING() then makeDAEBinding(binding.bindingExp, binding.variability);
case FLAT_BINDING() then makeDAEBinding(binding.bindingExp, binding.variability);
case CEVAL_BINDING() then DAE.UNBOUND();
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped binding", sourceInfo());
then
fail();
end match;
end toDAE;
function makeDAEBinding
input Expression exp;
input Variability var;
output DAE.Binding binding;
algorithm
binding := DAE.EQBOUND(
Expression.toDAE(exp),
NONE(),
Variability.variabilityToDAEConst(var),
DAE.BINDING_FROM_DEFAULT_VALUE() // TODO: revise this.
);
end makeDAEBinding;
function toDAEExp
input Binding binding;
output Option<DAE.Exp> bindingExp;
algorithm
bindingExp := match binding
case UNBOUND() then NONE();
case TYPED_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
case FLAT_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
case CEVAL_BINDING() then NONE();
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped binding", sourceInfo());
then
fail();
end match;
end toDAEExp;
function mapExp
input output Binding binding;
input MapFunc mapFn;
partial function MapFunc
input output Expression exp;
end MapFunc;
protected
Expression e1, e2;
algorithm
() := match binding
case UNTYPED_BINDING(bindingExp = e1)
algorithm
e2 := mapFn(e1);
if not referenceEq(e1, e2) then
binding.bindingExp := e2;
end if;
then
();
case TYPED_BINDING(bindingExp = e1)
algorithm
e2 := mapFn(e1);
if not referenceEq(e1, e2) then
binding.bindingExp := e2;
end if;
then
();
case FLAT_BINDING(bindingExp = e1)
algorithm
e2 := mapFn(e1);
if not referenceEq(e1, e2) then
binding.bindingExp := e2;
end if;
then
();
case CEVAL_BINDING(bindingExp = e1)
algorithm
e2 := mapFn(e1);
if not referenceEq(e1, e2) then
binding.bindingExp := e2;
end if;
then
();
else ();
end match;
end mapExp;
end Binding;
annotation(__OpenModelica_Interface="frontend");
end NFBinding;