/
NFTyping.mo
2211 lines (1931 loc) · 67.4 KB
/
NFTyping.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 NFTyping
" file: NFTyping.mo
package: NFTyping
description: NFInst typing.
Functions used by NFInst for typing.
"
import Binding = NFBinding;
import NFComponent.Component;
import Dimension = NFDimension;
import Equation = NFEquation;
import NFClass.Class;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFModifier.Modifier;
import Statement = NFStatement;
import NFType.Type;
import Operator = NFOperator;
import NFPrefixes.Variability;
import NFPrefixes.ConnectorType;
import Prefixes = NFPrefixes;
import ExpOrigin = NFExpOrigin;
import Connector = NFConnector;
import Connection = NFConnection;
protected
import Builtin = NFBuiltin;
import Ceval = NFCeval;
import ClassInf;
import ComponentRef = NFComponentRef;
import Origin = NFComponentRef.Origin;
import ExecStat.execStat;
import Inst = NFInst;
import InstUtil = NFInstUtil;
import Lookup = NFLookup;
import MatchKind = NFTypeCheck.MatchKind;
import NFCall.Call;
import NFClass.ClassTree;
import SimplifyExp = NFSimplifyExp;
import Subscript = NFSubscript;
import TypeCheck = NFTypeCheck;
import Types;
import NFSections.Sections;
import List;
import DAEUtil;
import MetaModelica.Dangerous.listReverseInPlace;
import ComplexType = NFComplexType;
import Restriction = NFRestriction;
import NFModifier.ModTable;
import Package = NFPackage;
import NFFunction.Function;
import NFInstNode.CachedData;
import Direction = NFPrefixes.Direction;
uniontype TypingError
record NO_ERROR end NO_ERROR;
record OUT_OF_BOUNDS
Integer upperBound;
end OUT_OF_BOUNDS;
function isError
input TypingError error;
output Boolean isError;
algorithm
isError := match error
case NO_ERROR() then false;
else true;
end match;
end isError;
end TypingError;
public
type EquationScope = enumeration(NORMAL, INITIAL, IF, IF_PARAMETER);
type ClassScope = enumeration(CLASS, FUNCTION);
public
function typeClass
input InstNode cls;
input String name;
algorithm
typeComponents(cls, ClassScope.CLASS);
execStat("NFTyping.typeComponents(" + name + ")");
typeBindings(cls, cls);
execStat("NFTyping.typeBindings(" + name + ")");
typeSections(cls);
execStat("NFTyping.typeSections(" + name + ")");
end typeClass;
function typeComponents
input InstNode cls;
input ClassScope clsScope;
protected
Class c = InstNode.getClass(cls), c2;
ClassTree cls_tree;
list<Dimension> dims;
algorithm
() := match c
case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE())
algorithm
for c in cls_tree.components loop
if not InstNode.isEmpty(c) then
typeComponent(c, clsScope);
end if;
end for;
then
();
case Class.DERIVED_CLASS()
algorithm
// At this stage most of the information in the derived class has been
// transferred to the component instance it belongs to, so we can
// collapse the extends hierarchy by replacing it with the base class.
c2 := InstNode.getClass(c.baseClass);
// But keep the restriction.
c2 := Class.setRestriction(c.restriction, c2);
InstNode.updateClass(c2, cls);
typeComponents(cls, clsScope);
then
();
case Class.INSTANCED_BUILTIN(restriction = Restriction.EXTERNAL_OBJECT())
algorithm
typeExternalObjectStructors(c.ty);
then
();
case Class.INSTANCED_BUILTIN() then ();
else
algorithm
assert(false, getInstanceName() + " got uninstantiated class " +
InstNode.name(cls));
then
fail();
end match;
end typeComponents;
function typeExternalObjectStructors
input Type ty;
protected
InstNode constructor, destructor;
Function fn;
Boolean typed, special;
algorithm
Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT(constructor, destructor)) := ty;
CachedData.FUNCTION({fn}, typed, special) := InstNode.getFuncCache(constructor);
if not typed then
fn := Function.typeFunction(fn);
InstNode.setFuncCache(constructor, CachedData.FUNCTION({fn}, true, special));
end if;
CachedData.FUNCTION({fn}, typed, special) := InstNode.getFuncCache(destructor);
if not typed then
fn := Function.typeFunction(fn);
InstNode.setFuncCache(destructor, CachedData.FUNCTION({fn}, true, special));
end if;
end typeExternalObjectStructors;
function makeClassType
input InstNode clsNode;
output Type ty;
protected
Class cls;
Restriction res;
algorithm
cls := InstNode.getClass(clsNode);
ty := match cls
case Class.INSTANCED_CLASS(restriction = Restriction.CONNECTOR())
then Type.COMPLEX(clsNode, makeConnectorType(cls.elements));
else Class.getType(cls, clsNode);
end match;
end makeClassType;
function makeConnectorType
input ClassTree ctree;
output ComplexType connectorTy;
protected
list<InstNode> pots = {}, flows = {}, streams = {};
ConnectorType cty;
algorithm
for c in ClassTree.enumerateComponents(ctree) loop
cty := Component.connectorType(InstNode.component(c));
if cty == ConnectorType.FLOW then
flows := c :: flows;
elseif cty == ConnectorType.STREAM then
streams := c :: streams;
else
pots := c :: pots;
end if;
end for;
connectorTy := ComplexType.CONNECTOR(pots, flows, streams, false);
end makeConnectorType;
function typeComponent
input InstNode component;
input ClassScope clsScope;
output Type ty;
protected
InstNode node = InstNode.resolveOuter(component);
Component c = InstNode.component(node);
algorithm
ty := match c
// An untyped component, type it.
case Component.UNTYPED_COMPONENT()
algorithm
// Type the component's dimensions.
typeDimensions(c.dimensions, node, c.binding, clsScope, c.info);
// Construct the type of the component and update the node with it.
ty := Type.liftArrayLeftList(makeClassType(c.classInst), arrayList(c.dimensions));
InstNode.updateComponent(Component.setType(ty, c), node);
// Check that the component's attributes are valid.
checkComponentAttributes(c.attributes, component);
// Type the component's children.
typeComponents(c.classInst, clsScope);
then
ty;
// A component that has already been typed, skip it.
case Component.TYPED_COMPONENT() then c.ty;
case Component.ITERATOR() then c.ty;
case Component.ENUM_LITERAL(literal = Expression.ENUM_LITERAL(ty = ty)) then ty;
// Any other type of component shouldn't show up here.
else
algorithm
assert(false, getInstanceName() + " got uninstantiated component " +
InstNode.name(component));
then
fail();
end match;
end typeComponent;
// TODO: Make this check part of the check that a class adheres to its
// restriction.
function checkComponentAttributes
input Component.Attributes attributes;
input InstNode component;
protected
Component.Attributes attr = attributes;
ConnectorType cty;
algorithm
() := match attr
case Component.ATTRIBUTES(connectorType = cty)
algorithm
// 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.
if cty <> ConnectorType.POTENTIAL and not checkConnectorType(component) then
Error.addSourceMessage(Error.CONNECTOR_PREFIX_OUTSIDE_CONNECTOR,
{Prefixes.connectorTypeString(cty)}, InstNode.info(component));
// Remove the prefix from the component, to avoid issues like a flow
// equation being generated for it.
attr.connectorType := ConnectorType.POTENTIAL;
InstNode.componentApply(component, Component.setAttributes, attr);
end if;
then
();
else ();
end match;
end checkComponentAttributes;
function checkConnectorType
input InstNode node;
output Boolean isConnector;
algorithm
isConnector := match node
case InstNode.COMPONENT_NODE()
then Class.isConnectorClass(InstNode.getClass(node)) or
checkConnectorType(node.parent);
else false;
end match;
end checkConnectorType;
function typeIterator
input InstNode iterator;
input SourceInfo info;
input Boolean structural "If the iteration range must be a parameter expression or not.";
protected
Component c = InstNode.component(iterator);
Binding binding;
Type ty;
Expression exp;
algorithm
() := match c
case Component.ITERATOR(binding = Binding.UNTYPED_BINDING())
algorithm
binding := typeBinding(c.binding, ExpOrigin.ITERATION_RANGE());
// If the iteration range is structural, it must be a parameter expression.
if structural then
if Binding.variability(binding) > Variability.PARAMETER then
Error.addSourceMessageAndFail(Error.NON_PARAMETER_ITERATOR_RANGE,
{Binding.toString(binding)}, info);
else
SOME(exp) := Binding.typedExp(binding);
exp := Ceval.evalExp(exp, Ceval.EvalTarget.RANGE(info));
exp := SimplifyExp.simplifyExp(exp);
binding := Binding.setTypedExp(exp, binding);
end if;
end if;
ty := Binding.getType(binding);
// The iteration range must be a vector expression.
if not Type.isVector(ty) then
Error.addSourceMessageAndFail(Error.FOR_EXPRESSION_TYPE_ERROR,
{Binding.toString(binding), Type.toString(ty)}, info);
end if;
// The type of the iterator is the element type of the range expression.
ty := Type.arrayElementType(ty);
c := Component.ITERATOR(ty, binding);
InstNode.updateComponent(c, iterator);
then
();
case Component.ITERATOR(binding = Binding.UNBOUND())
algorithm
assert(false, getInstanceName() + ": Implicit iteration ranges not yet implement");
then
fail();
else
algorithm
assert(false, getInstanceName() + " got non-iterator " + InstNode.name(iterator));
then
fail();
end match;
end typeIterator;
function typeDimensions
input output array<Dimension> dimensions;
input InstNode component;
input Binding binding;
input ClassScope clsScope;
input SourceInfo info;
algorithm
for i in 1:arrayLength(dimensions) loop
typeDimension(dimensions[i], component, binding, i, clsScope, dimensions, info);
end for;
end typeDimensions;
function typeDimension
input output Dimension dimension;
input InstNode component;
input Binding binding;
input Integer index;
input ClassScope clsScope;
input array<Dimension> dimensions;
input SourceInfo info;
algorithm
dimension := match dimension
local
Expression exp;
Variability var;
Dimension dim;
Binding b;
TypingError ty_err;
Type ty;
Integer prop_dims;
// Print an error when a dimension that's currently being processed is
// found, which indicates a dependency loop. Another way of handling this
// would be to instead view the dimension as unknown and infer it from the
// binding, which means that things like x[size(x, 1)] = {...} could be
// handled. But that is not specified and doesn't seem needed, and can also
// give different results depending on the declaration order of components.
case Dimension.UNTYPED(isProcessing = true)
algorithm
// TODO: Tell the user which variables are involved in the loop (can be
// found with DFS on the dimension expression. Maybe have a limit
// on the output in case there's a lot of dimensions involved.
Error.addSourceMessage(Error.CYCLIC_DIMENSIONS,
{String(index), InstNode.name(component), Expression.toString(dimension.dimension)}, info);
then
fail();
// If the dimension is not typed, type it.
case Dimension.UNTYPED()
algorithm
arrayUpdate(dimensions, index, Dimension.UNTYPED(dimension.dimension, true));
(exp, ty, var) := typeExp(dimension.dimension, info, ExpOrigin.DIMENSION());
TypeCheck.checkDimensionType(exp, ty, info);
if var <= Variability.PARAMETER then
// Evaluate the dimension if it's a parameter expression.
exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info));
exp := SimplifyExp.simplifyExp(exp);
else
// Dimensions must be parameter expressions, unless we're in a function.
if clsScope <> ClassScope.FUNCTION then
Error.addSourceMessage(Error.DIMENSION_NOT_KNOWN,
{Expression.toString(exp)}, info);
fail();
end if;
end if;
// It's possible to get an array expression here, for example if the
// dimension expression is a parameter whose binding comes from a
// modifier on an array component. If all the elements are equal we can
// just take one of them and use that, but we don't yet support the case
// where they are different. Creating a dimension from an array leads to
// weird things happening, so for now we print an error instead.
if not Expression.arrayAllEqual(exp) then
Error.addSourceMessage(Error.RAGGED_DIMENSION, {Expression.toString(exp)}, info);
fail();
end if;
dim := Dimension.fromExp(Expression.arrayFirstScalar(exp), var);
arrayUpdate(dimensions, index, dim);
then
dim;
// If the dimension is unknown in a function, keep it unknown.
case Dimension.UNKNOWN() guard clsScope == ClassScope.FUNCTION
then dimension;
// If the dimension is unknown in a class, try to infer it from the components binding.
case Dimension.UNKNOWN()
algorithm
dim := match binding
// Print an error if there's no binding.
case Binding.UNBOUND()
algorithm
Error.addSourceMessage(Error.FAILURE_TO_DEDUCE_DIMS_NO_MOD,
{String(index), InstNode.name(component)}, info);
then
fail();
// An untyped binding, type the expression only as much as is needed
// to get the dimension we're looking for.
case Binding.UNTYPED_BINDING()
algorithm
prop_dims := InstNode.countDimensions(InstNode.parent(component),
InstNode.level(component) - binding.originLevel);
dim := typeExpDim(binding.bindingExp, index + prop_dims, ExpOrigin.DIMENSION(), info);
then
dim;
// A typed binding, get the dimension from the binding's type.
case Binding.TYPED_BINDING()
algorithm
prop_dims := InstNode.countDimensions(InstNode.parent(component),
InstNode.level(component) - binding.originLevel);
dim := nthDimensionBoundsChecked(binding.bindingType, index + prop_dims);
then
dim;
else Dimension.UNKNOWN();
end match;
arrayUpdate(dimensions, index, dim);
then
dim;
// Other kinds of dimensions are already typed.
else dimension;
end match;
end typeDimension;
function typeBindings
input InstNode cls;
input InstNode component;
protected
Class c;
ClassTree cls_tree;
InstNode node;
algorithm
c := InstNode.getClass(cls);
() := match c
case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE())
algorithm
for c in cls_tree.components loop
typeComponentBinding(c);
end for;
then
();
case Class.INSTANCED_BUILTIN()
algorithm
c.attributes := typeTypeAttributes(c.attributes, c.ty, component);
InstNode.updateClass(c, cls);
then
();
else
algorithm
assert(false, getInstanceName() + " got uninstantiated class " +
InstNode.name(cls));
then
fail();
end match;
end typeBindings;
function typeComponentBinding
input InstNode component;
protected
InstNode node = InstNode.resolveOuter(component);
Component c;
algorithm
if InstNode.isEmpty(component) then
return;
end if;
c := InstNode.component(node);
() := match c
local
Binding binding;
InstNode cls;
MatchKind matchKind;
Boolean dirty;
String name;
// A component that's already been typed.
case Component.TYPED_COMPONENT(binding = Binding.TYPED_BINDING()) then ();
case Component.TYPED_COMPONENT()
algorithm
name := InstNode.name(component);
binding := typeBinding(c.binding);
dirty := not referenceEq(binding, c.binding);
// If the binding changed during typing it means it was an untyped
// binding which is now typed, and it needs to be type checked.
if dirty then
binding := TypeCheck.matchBinding(binding, c.ty, name, node);
if Binding.variability(binding) > Component.variability(c) then
Error.addSourceMessage(Error.HIGHER_VARIABILITY_BINDING,
{name, Prefixes.variabilityString(Component.variability(c)),
"'" + Binding.toString(binding) + "'", Prefixes.variabilityString(Binding.variability(binding))},
Binding.getInfo(binding));
fail();
end if;
c.binding := binding;
end if;
typeBindings(c.classInst, component);
if Binding.isBound(c.condition) then
c.condition := typeComponentCondition(c.condition);
dirty := true;
end if;
// Update the node if the component changed.
if dirty then
InstNode.updateComponent(c, node);
end if;
then
();
else
algorithm
assert(false, getInstanceName() + " got invalid node " + InstNode.name(node));
then
fail();
end match;
end typeComponentBinding;
function typeBinding
input output Binding binding;
input ExpOrigin origin = ExpOrigin.BINDING();
algorithm
binding := match binding
local
Expression exp;
Type ty;
Variability var;
case Binding.UNTYPED_BINDING(bindingExp = exp)
algorithm
(exp, ty, var) := typeExp(exp, binding.info, origin);
then
Binding.TYPED_BINDING(exp, ty, var, binding.originLevel, binding.info);
case Binding.TYPED_BINDING() then binding;
case Binding.UNBOUND() then binding;
else
algorithm
assert(false, getInstanceName() + " got uninstantiated binding");
then
fail();
end match;
end typeBinding;
function typeComponentCondition
input output Binding condition;
algorithm
condition := match condition
local
Expression exp;
Type ty;
Variability var;
SourceInfo info;
MatchKind mk;
case Binding.UNTYPED_BINDING(bindingExp = exp, info = info)
algorithm
(exp, ty, var) := typeExp(exp, info, ExpOrigin.CONDITION());
(exp, _, mk) := TypeCheck.matchTypes(ty, Type.BOOLEAN(), exp);
if TypeCheck.isIncompatibleMatch(mk) then
Error.addSourceMessage(Error.IF_CONDITION_TYPE_ERROR,
{Expression.toString(exp), Type.toString(ty)}, info);
fail();
end if;
if var > Variability.PARAMETER then
Error.addSourceMessage(Error.COMPONENT_CONDITION_VARIABILITY,
{Expression.toString(exp)}, info);
fail();
end if;
exp := Ceval.evalExp(exp, Ceval.EvalTarget.CONDITION(info));
exp := SimplifyExp.simplifyExp(exp);
then
Binding.FLAT_BINDING(exp);
end match;
end typeComponentCondition;
function typeTypeAttributes
input output list<Modifier> attributes;
input Type ty;
input InstNode component;
protected
partial function attrTypeFn
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
end attrTypeFn;
attrTypeFn ty_fn;
algorithm
ty_fn := match ty
case Type.REAL() then getRealAttributeType;
case Type.INTEGER() then getIntAttributeType;
case Type.BOOLEAN() then getBoolAttributeType;
case Type.STRING() then getStringAttributeType;
case Type.ENUMERATION() then getEnumAttributeType;
else getAnyAttributeType;
end match;
attributes := list(typeTypeAttribute(a, ty_fn, ty, component) for a in attributes);
end typeTypeAttributes;
function typeTypeAttribute
input output Modifier attribute;
input attrTypeFn attrTyFn;
input Type ty;
input InstNode component;
partial function attrTypeFn
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
end attrTypeFn;
protected
String name;
Binding binding;
Type expected_ty;
algorithm
() := match attribute
// Normal modifier with no submodifiers.
case Modifier.MODIFIER(name = name, binding = binding, subModifiers = ModTable.EMPTY())
algorithm
// Use the given function to get the expected type of the attribute.
expected_ty := attrTyFn(name, ty, Modifier.info(attribute));
// Type and type check the attribute.
binding := typeBinding(binding);
binding := TypeCheck.matchBinding(binding, expected_ty, name, component);
// Check the variability. All builtin attributes have parameter variability.
if Binding.variability(binding) > Variability.PARAMETER then
Error.addSourceMessage(Error.HIGHER_VARIABILITY_BINDING,
{name, Prefixes.variabilityString(Variability.PARAMETER),
"'" + Binding.toString(binding) + "'", Prefixes.variabilityString(Binding.variability(binding))},
Binding.getInfo(binding));
fail();
end if;
binding := match name
case "fixed" then evalBinding(binding);
case "stateSelect" then evalBinding(binding);
else Package.replaceBindingConstants(binding);
end match;
attribute.binding := binding;
then
();
// Modifier with submodifier, e.g. Real x(start(y = 1)), is an error.
case Modifier.MODIFIER()
algorithm
// Print an error for the first submodifier. The builtin attributes
// don't have types as such, so for the error message to make sense we
// join the attribute name and submodifier name together (e.g. start.y).
name := attribute.name + "." + Util.tuple21(listHead(ModTable.toList(attribute.subModifiers)));
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, attribute.info);
then
fail();
end match;
end typeTypeAttribute;
function evalBinding
input output Binding binding;
algorithm
binding := match binding
local
Expression exp;
case Binding.TYPED_BINDING()
algorithm
exp := Ceval.evalExp(binding.bindingExp, Ceval.EvalTarget.ATTRIBUTE(binding.bindingExp, binding.info));
exp := SimplifyExp.simplifyExp(exp);
then
Binding.TYPED_BINDING(exp, binding.bindingType, binding.variability, binding.originLevel, binding.info);
else
algorithm
assert(false, getInstanceName() + " failed for " + Binding.toString(binding));
then
fail();
end match;
end evalBinding;
function getAttributeNameBinding
input Modifier attr;
input String typeName;
output String name;
output Binding binding;
algorithm
(name, binding) := match attr
case Modifier.MODIFIER(name = name, binding = binding, subModifiers = ModTable.EMPTY())
then (name, binding);
case Modifier.MODIFIER()
algorithm
name := attr.name + "." + Util.tuple21(listHead(ModTable.toList(attr.subModifiers)));
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, typeName}, attr.info);
then
fail();
end match;
end getAttributeNameBinding;
function getRealAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
attrTy := match name
case "quantity" then Type.STRING();
case "unit" then Type.STRING();
case "displayUnit" then Type.STRING();
case "min" then ty;
case "max" then ty;
case "start" then ty;
case "fixed" then Type.BOOLEAN();
case "nominal" then ty;
case "unbounded" then Type.BOOLEAN();
case "stateSelect" then NFBuiltin.STATESELECT_TYPE;
else
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
then
fail();
end match;
end getRealAttributeType;
function getIntAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
attrTy := match name
case "quantity" then Type.STRING();
case "min" then ty;
case "max" then ty;
case "start" then ty;
case "fixed" then Type.BOOLEAN();
else
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
then
fail();
end match;
end getIntAttributeType;
function getBoolAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
attrTy := match name
case "quantity" then Type.STRING();
case "start" then ty;
case "fixed" then Type.BOOLEAN();
else
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
then
fail();
end match;
end getBoolAttributeType;
function getStringAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
attrTy := match name
case "quantity" then Type.STRING();
case "start" then ty;
case "fixed" then Type.BOOLEAN();
else
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
then
fail();
end match;
end getStringAttributeType;
function getEnumAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
attrTy := match name
case "quantity" then Type.STRING();
case "min" then ty;
case "max" then ty;
case "start" then ty;
case "fixed" then Type.BOOLEAN();
else
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
then
fail();
end match;
end getEnumAttributeType;
function getAnyAttributeType
input String name;
input Type ty;
input SourceInfo info;
output Type attrTy;
algorithm
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{name, Type.toString(ty)}, info);
fail();
end getAnyAttributeType;
function typeExp
input output Expression exp;
input SourceInfo info;
input ExpOrigin origin = ExpOrigin.NO_ORIGIN();
output Type ty;
output Variability variability;
algorithm
(exp, ty, variability) := match exp
local
Expression e1, e2, e3;
Variability var1, var2, var3;
Type ty1, ty2, ty3;
Operator op;
ComponentRef cref;
ExpOrigin next_origin;
case Expression.INTEGER() then (exp, Type.INTEGER(), Variability.CONSTANT);
case Expression.REAL() then (exp, Type.REAL(), Variability.CONSTANT);
case Expression.STRING() then (exp, Type.STRING(), Variability.CONSTANT);
case Expression.BOOLEAN() then (exp, Type.BOOLEAN(), Variability.CONSTANT);
case Expression.ENUM_LITERAL() then (exp, exp.ty, Variability.CONSTANT);
case Expression.CREF()
algorithm
(cref, ty, variability) := typeCref(exp.cref, info);
then
(Expression.CREF(ty, cref), ty, variability);
case Expression.TYPENAME()
algorithm
() := match origin
case ExpOrigin.ITERATION_RANGE() then ();
case ExpOrigin.DIMENSION() then ();
else
algorithm
Error.addSourceMessage(Error.INVALID_TYPENAME_USE,
{Type.typenameString(Type.arrayElementType(exp.ty))}, info);
then
fail();
end match;
then
(exp, exp.ty, Variability.CONSTANT);
case Expression.ARRAY() then typeArray(exp.elements, info);
case Expression.RANGE() then typeRange(exp, info);
case Expression.TUPLE() then typeTuple(exp.elements, info, origin);
case Expression.SIZE() then typeSize(exp, info);
case Expression.END() then typeEnd(origin, info);
case Expression.BINARY()
algorithm
next_origin := ExpOrigin.next(origin);
(e1, ty1, var1) := typeExp(exp.exp1, info, next_origin);
(e2, ty2, var2) := typeExp(exp.exp2, info, next_origin);
(exp, ty) := TypeCheck.checkBinaryOperation(e1, ty1, exp.operator, e2, ty2);
then
(exp, ty, Prefixes.variabilityMax(var1, var2));
case Expression.UNARY()
algorithm
(e1, ty1, var1) := typeExp(exp.exp, info, ExpOrigin.next(origin));
(exp, ty) := TypeCheck.checkUnaryOperation(e1, ty1, exp.operator);
then
(exp, ty, var1);
case Expression.LBINARY()
algorithm
next_origin := ExpOrigin.next(origin);
(e1, ty1, var1) := typeExp(exp.exp1, info, next_origin);
(e2, ty2, var2) := typeExp(exp.exp2, info, next_origin);
(exp, ty) := TypeCheck.checkLogicalBinaryOperation(e1, ty1, exp.operator, e2, ty2);
then
(exp, ty, Prefixes.variabilityMax(var1, var2));
case Expression.LUNARY()
algorithm
(e1, ty1, var1) := typeExp(exp.exp, info, ExpOrigin.next(origin));