This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
NFTyping.mo
2986 lines (2616 loc) · 97.5 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 NFBinding.Binding;
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 Connector = NFConnector;
import Connection = NFConnection;
import Algorithm = NFAlgorithm;
protected
import Builtin = NFBuiltin;
import BuiltinCall = NFBuiltinCall;
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 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;
import ElementSource;
import StringUtil;
import NFOCConnectionGraph;
import System;
import ErrorExt;
public
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;
package ExpOrigin
// ExpOrigin is used to keep track of where an expression is coming from,
// and is implemented as an integer bitfield.
type Type = Integer;
// Flag values:
constant Type CLASS = 0; // In class.
constant Type FUNCTION = intBitLShift(1, 0); // In function.
constant Type ALGORITHM = intBitLShift(1, 1); // In algorithm section.
constant Type EQUATION = intBitLShift(1, 2); // In equation section.
constant Type INITIAL = intBitLShift(1, 3); // In initial section.
constant Type LHS = intBitLShift(1, 4); // On left hand side of equality/assignment.
constant Type RHS = intBitLShift(1, 5); // On right hand side of equality/assignment.
constant Type WHEN = intBitLShift(1, 6); // In when equation/statement.
constant Type FOR = intBitLShift(1, 7); // In a for loop.
constant Type IF = intBitLShift(1, 8); // In an if equation/statement.
constant Type WHILE = intBitLShift(1, 9); // In a while loop.
constant Type NONEXPANDABLE = intBitLShift(1, 10); // In non-parameter if/for.
constant Type ITERATION_RANGE = intBitLShift(1, 11); // In range used for iteration.
constant Type DIMENSION = intBitLShift(1, 12); // In dimension.
constant Type BINDING = intBitLShift(1, 13); // In binding.
constant Type CONDITION = intBitLShift(1, 14); // In conditional expression.
constant Type SUBSCRIPT = intBitLShift(1, 15); // In subscript.
constant Type SUBEXPRESSION = intBitLShift(1, 16); // Part of a larger expression.
constant Type CONNECT = intBitLShift(1, 17); // Part of connect argument.
constant Type NOEVENT = intBitLShift(1, 18); // Part of noEvent argument.
// Combined flags:
constant Type EQ_SUBEXPRESSION = intBitOr(EQUATION, SUBEXPRESSION);
constant Type VALID_TYPENAME_SCOPE = intBitOr(ITERATION_RANGE, DIMENSION);
constant Type DISCRETE_SCOPE = intBitOr(WHEN, intBitOr(INITIAL, FUNCTION));
function isSingleExpression
"Returns true if the given origin indicates the expression is alone on
either side of an equality/assignment."
input Type origin;
output Boolean isSingle = origin < ITERATION_RANGE - 1;
end isSingleExpression;
function setFlag
input Type origin;
input Type flag;
output Type newOrigin;
algorithm
newOrigin := intBitOr(origin, flag);
annotation(__OpenModelica_EarlyInline=true);
end setFlag;
function flagSet
input Type origin;
input Type flag;
output Boolean set;
algorithm
set := intBitAnd(origin, flag) > 0;
annotation(__OpenModelica_EarlyInline=true);
end flagSet;
function flagNotSet
input Type origin;
input Type flag;
output Boolean notSet;
algorithm
notSet := intBitAnd(origin, flag) == 0;
annotation(__OpenModelica_EarlyInline=true);
end flagNotSet;
end ExpOrigin;
public
function typeClass
input InstNode cls;
input String name;
algorithm
typeClassType(cls, NFBinding.EMPTY_BINDING, ExpOrigin.CLASS, cls);
typeComponents(cls, ExpOrigin.CLASS);
execStat("NFTyping.typeComponents(" + name + ")");
typeBindings(cls, cls, ExpOrigin.CLASS);
execStat("NFTyping.typeBindings(" + name + ")");
typeClassSections(cls, ExpOrigin.CLASS);
execStat("NFTyping.typeClassSections(" + name + ")");
end typeClass;
function typeComponents
input InstNode cls;
input ExpOrigin.Type origin;
protected
Class c = InstNode.getClass(cls), c2;
ClassTree cls_tree;
InstNode ext_node, con, de;
algorithm
() := match c
case Class.INSTANCED_CLASS(restriction = Restriction.TYPE()) then ();
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, origin);
end if;
end for;
() := match c.ty
case Type.COMPLEX(complexTy = ComplexType.RECORD(constructor = con))
algorithm
typeStructor(con);
then
();
else ();
end match;
then
();
// For derived types with dimensions we keep them as they are, because we
// need to preserve the dimensions.
case Class.TYPED_DERIVED(ty = Type.ARRAY())
algorithm
typeComponents(c.baseClass, origin);
then
();
// Derived types without dimensions can be collapsed.
case Class.TYPED_DERIVED()
algorithm
typeComponents(c.baseClass, origin);
c2 := InstNode.getClass(c.baseClass);
c2 := Class.setRestriction(c.restriction, c2);
InstNode.updateClass(c2, cls);
then
();
case Class.INSTANCED_BUILTIN(ty = Type.COMPLEX(complexTy =
ComplexType.EXTERNAL_OBJECT(constructor = con, destructor = de)))
algorithm
typeStructor(con);
typeStructor(de);
then
();
case Class.INSTANCED_BUILTIN() then ();
else
algorithm
Error.assertion(false, getInstanceName() + " got uninstantiated class " + InstNode.name(cls), sourceInfo());
then
fail();
end match;
end typeComponents;
function typeStructor
input InstNode node;
protected
CachedData cache;
list<Function> fnl;
algorithm
cache := InstNode.getFuncCache(node);
() := match cache
case CachedData.FUNCTION(funcs = fnl, typed = false)
algorithm
fnl := list(Function.typeFunction(fn) for fn in fnl);
InstNode.setFuncCache(node, CachedData.FUNCTION(fnl, true, cache.specialBuiltin));
then
();
else ();
end match;
end typeStructor;
function typeClassType
input InstNode clsNode;
input Binding componentBinding;
input ExpOrigin.Type origin;
input InstNode instanceNode;
output Type ty;
protected
Class cls, ty_cls;
InstNode node;
Function fn;
algorithm
cls := InstNode.getClass(clsNode);
ty := match cls
case Class.INSTANCED_CLASS(restriction = Restriction.CONNECTOR())
algorithm
ty := Type.COMPLEX(clsNode, makeConnectorType(cls.elements));
cls.ty := ty;
InstNode.updateClass(cls, clsNode);
then
ty;
// A long class declaration of a type extending from a type has the type of the base class.
case Class.INSTANCED_CLASS(ty = Type.COMPLEX(complexTy = ComplexType.EXTENDS_TYPE(node)))
algorithm
ty := typeClassType(node, componentBinding, origin, instanceNode);
cls.ty := ty;
InstNode.updateClass(cls, clsNode);
then
ty;
// A component of function type, i.e. a functional input parameter.
case Class.INSTANCED_CLASS(restriction = Restriction.FUNCTION())
guard InstNode.isComponent(instanceNode)
algorithm
fn :: _ := Function.typeNodeCache(clsNode);
if not Function.isPartial(fn) then
Error.addSourceMessage(Error.META_FUNCTION_TYPE_NO_PARTIAL_PREFIX,
{Absyn.pathString(Function.name(fn))}, InstNode.info(instanceNode));
fail();
end if;
ty := Type.FUNCTION(fn, NFType.FunctionType.FUNCTIONAL_PARAMETER);
cls.ty := ty;
InstNode.updateClass(cls, clsNode);
then
ty;
case Class.INSTANCED_CLASS() then cls.ty;
case Class.EXPANDED_DERIVED()
algorithm
typeDimensions(cls.dims, clsNode, componentBinding, origin, InstNode.info(clsNode));
ty := typeClassType(cls.baseClass, componentBinding, origin, instanceNode);
ty := Type.liftArrayLeftList(ty, arrayList(cls.dims));
ty_cls := Class.TYPED_DERIVED(ty, cls.baseClass, cls.restriction);
InstNode.updateClass(ty_cls, clsNode);
then
ty;
case Class.INSTANCED_BUILTIN() then cls.ty;
case Class.TYPED_DERIVED() then cls.ty;
else
algorithm
Error.assertion(false, getInstanceName() + " got noninstantiated class " +
InstNode.name(clsNode), sourceInfo());
then
fail();
end match;
end typeClassType;
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 ExpOrigin.Type origin;
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, origin, c.info);
// Construct the type of the component and update the node with it.
ty := typeClassType(c.classInst, c.binding, origin, component);
ty := Type.liftArrayLeftList(ty, 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, origin);
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
Error.assertion(false, getInstanceName() + " got noninstantiated component " + InstNode.name(component), sourceInfo());
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
if InstNode.isEmpty(node) then
isConnector := false;
else
isConnector := Class.isConnectorClass(InstNode.getClass(node)) or
checkConnectorType(InstNode.parent(node));
end if;
end checkConnectorType;
function typeIterator
input InstNode iterator;
input Expression range;
input ExpOrigin.Type origin;
input Boolean structural "If the iteration range must be a parameter expression or not.";
output Expression outRange;
output Type ty;
output Variability var;
protected
Component c = InstNode.component(iterator);
Expression exp;
SourceInfo info;
algorithm
(outRange, ty, var) := match c
case Component.ITERATOR(info = info)
algorithm
(exp, ty, var) := typeExp(range, ExpOrigin.setFlag(origin, ExpOrigin.ITERATION_RANGE), info);
// If the iteration range is structural, it must be a parameter expression.
if structural and var > Variability.PARAMETER then
Error.addSourceMessageAndFail(Error.NON_PARAMETER_ITERATOR_RANGE,
{Expression.toString(exp)}, info);
end if;
// The iteration range must be a vector expression.
if not Type.isVector(ty) then
Error.addSourceMessageAndFail(Error.FOR_EXPRESSION_TYPE_ERROR,
{Expression.toString(exp), Type.toString(ty)}, info);
end if;
// The type of the iterator is the element type of the range expression.
c := Component.ITERATOR(Type.arrayElementType(ty), var, info);
InstNode.updateComponent(c, iterator);
then
(exp, ty, var);
else
algorithm
Error.assertion(false, getInstanceName() + " got non-iterator " + InstNode.name(iterator), sourceInfo());
then
fail();
end match;
end typeIterator;
function typeDimensions
input output array<Dimension> dimensions;
input InstNode component;
input Binding binding;
input ExpOrigin.Type origin;
input SourceInfo info;
algorithm
for i in 1:arrayLength(dimensions) loop
typeDimension(dimensions, i, component, binding, origin, info);
end for;
end typeDimensions;
function typeDimension
input array<Dimension> dimensions;
input Integer index;
input InstNode component;
input Binding binding;
input ExpOrigin.Type origin;
input SourceInfo info;
output Dimension dimension = dimensions[index];
algorithm
dimension := match dimension
local
Expression exp;
Variability var;
Dimension dim;
Binding b;
Type ty;
// 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
// Only give an error if we're not in a function.
if ExpOrigin.flagNotSet(origin, ExpOrigin.FUNCTION) then
// 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);
fail();
end if;
// If we are in a functions we allow e.g. size expression of unknown dimensions.
dim := Dimension.UNKNOWN();
arrayUpdate(dimensions, index, dim);
then
dim;
// 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, ExpOrigin.setFlag(origin, ExpOrigin.DIMENSION), info);
TypeCheck.checkDimensionType(exp, ty, info);
if ExpOrigin.flagNotSet(origin, ExpOrigin.FUNCTION) then
// Dimensions must be parameter expressions in a non-function class.
if var <= Variability.PARAMETER then
exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info));
else
Error.addSourceMessage(Error.DIMENSION_NOT_KNOWN,
{Expression.toString(exp)}, info);
fail();
end if;
else
// For functions, only evaluate constant and structural parameter expressions.
if var <= Variability.STRUCTURAL_PARAMETER then
exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info));
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 ExpOrigin.flagSet(origin, ExpOrigin.FUNCTION)
then dimension;
// If the dimension is unknown in a class, try to infer it from the components binding.
case Dimension.UNKNOWN()
algorithm
b := binding;
if Binding.isUnbound(binding) then
// If the component has no binding, try to use its parent's binding
// (i.e. for record fields where the record instance has a binding).
b := getRecordElementBinding(component);
if Binding.isUnbound(b) then
// If the component still doesn't have a binding, try to use the start attribute instead.
// TODO: Any attribute should actually be fine to use here.
b := Class.lookupAttributeBinding("start", InstNode.getClass(component));
end if;
end if;
dim := match b
// 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
dim := typeExpDim(b.bindingExp, index + Binding.countPropagatedDims(b),
ExpOrigin.setFlag(origin, ExpOrigin.DIMENSION), info);
then
dim;
// A typed binding, get the dimension from the binding's type.
case Binding.TYPED_BINDING()
algorithm
dim := nthDimensionBoundsChecked(b.bindingType, index + Binding.countPropagatedDims(b));
then
dim;
end match;
// Make sure the dimension is constant evaluted, and also mark it as structural.
dim := match dim
case Dimension.EXP(exp = exp)
algorithm
Inst.markStructuralParamsExp(exp);
exp := Ceval.evalExp(exp, Ceval.EvalTarget.DIMENSION(component, index, exp, info));
then
Dimension.fromExp(exp, dim.var);
else dim;
end match;
arrayUpdate(dimensions, index, dim);
then
dim;
// Other kinds of dimensions are already typed.
else dimension;
end match;
end typeDimension;
function getRecordElementBinding
"Tries to fetch the binding for a given record field by using the binding of
the record instance."
input InstNode component;
output Binding binding;
protected
InstNode parent;
Expression exp;
Binding parent_binding;
algorithm
parent := InstNode.parent(component);
if InstNode.isComponent(parent) then
// Get the binding of the component's parent.
parent_binding := Component.getBinding(InstNode.component(parent));
if Binding.isUnbound(parent_binding) then
// If the parent has no binding, try the parent's parent.
binding := getRecordElementBinding(parent);
else
// Otherwise type the binding, so we can safely look up the field name.
binding := typeBinding(parent_binding, ExpOrigin.CLASS);
// If the binding wasn't typed before, update the parent component with it
// so we don't have to type it again.
if not referenceEq(parent_binding, binding) then
InstNode.componentApply(parent, Component.setBinding, binding);
end if;
end if;
// If we found a binding, get the binding for the field from it.
if Binding.isBound(binding) then
binding := Binding.recordFieldBinding(InstNode.name(component), binding);
end if;
else
binding := NFBinding.EMPTY_BINDING;
end if;
end getRecordElementBinding;
function typeBindings
input InstNode cls;
input InstNode component;
input ExpOrigin.Type origin;
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, origin);
end for;
then
();
case Class.INSTANCED_BUILTIN(elements = cls_tree as ClassTree.FLAT_TREE())
algorithm
for c in cls_tree.components loop
typeComponentBinding(c, origin);
end for;
then
();
case Class.INSTANCED_BUILTIN() then ();
case Class.TYPED_DERIVED()
algorithm
typeBindings(c.baseClass, component, origin);
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " got uninstantiated class " + InstNode.name(cls), sourceInfo());
then
fail();
end match;
end typeBindings;
function typeComponentBinding
input InstNode component;
input ExpOrigin.Type origin;
input Boolean typeChildren = true;
protected
InstNode node = InstNode.resolveOuter(component);
Component c;
Binding binding;
InstNode cls;
MatchKind matchKind;
String name;
Variability comp_var, comp_eff_var, bind_var;
algorithm
if InstNode.isEmpty(component) then
return;
end if;
c := InstNode.component(node);
() := match c
case Component.TYPED_COMPONENT(binding = Binding.UNTYPED_BINDING())
algorithm
name := InstNode.name(component);
binding := c.binding;
ErrorExt.setCheckpoint(getInstanceName());
try
checkBindingEach(c.binding);
binding := typeBinding(binding, ExpOrigin.setFlag(origin, ExpOrigin.BINDING));
binding := TypeCheck.matchBinding(binding, c.ty, name, node);
comp_var := Component.variability(c);
comp_eff_var := Prefixes.effectiveVariability(comp_var);
bind_var := Prefixes.effectiveVariability(Binding.variability(binding));
if bind_var > comp_eff_var and ExpOrigin.flagNotSet(origin, ExpOrigin.FUNCTION) then
Error.addSourceMessage(Error.HIGHER_VARIABILITY_BINDING, {
name, Prefixes.variabilityString(comp_eff_var),
"'" + Binding.toString(c.binding) + "'", Prefixes.variabilityString(bind_var)},
Binding.getInfo(binding));
fail();
end if;
else
if Binding.isBound(c.condition) then
binding := Binding.INVALID_BINDING(binding, ErrorExt.getMessages());
else
fail();
end if;
end try;
ErrorExt.delCheckpoint(getInstanceName());
c.binding := binding;
if Binding.isBound(c.condition) then
c.condition := typeComponentCondition(c.condition, origin);
end if;
InstNode.updateComponent(c, node);
if typeChildren then
typeBindings(c.classInst, component, origin);
end if;
then
();
// A component without a binding, or with a binding that's already been typed.
case Component.TYPED_COMPONENT()
algorithm
checkBindingEach(c.binding);
if Binding.isBound(c.condition) then
c.condition := typeComponentCondition(c.condition, origin);
InstNode.updateComponent(c, node);
end if;
if typeChildren then
typeBindings(c.classInst, component, origin);
end if;
then
();
case Component.ENUM_LITERAL() then ();
case Component.TYPE_ATTRIBUTE(modifier = Modifier.NOMOD()) then ();
case Component.TYPE_ATTRIBUTE()
algorithm
c.modifier := typeTypeAttribute(c.modifier, c.ty, InstNode.parent(component), origin);
InstNode.updateComponent(c, node);
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid node " + InstNode.name(node), sourceInfo());
then
fail();
end match;
end typeComponentBinding;
function typeBinding
input output Binding binding;
input ExpOrigin.Type origin;
algorithm
binding := match binding
local
Expression exp;
Type ty;
Variability var;
SourceInfo info;
case Binding.UNTYPED_BINDING(bindingExp = exp)
algorithm
info := Binding.getInfo(binding);
(exp, ty, var) := typeExp(exp, origin, info);
then
Binding.TYPED_BINDING(exp, ty, var, binding.parents, binding.isEach, false, binding.info);
case Binding.TYPED_BINDING() then binding;
case Binding.UNBOUND() then binding;
else
algorithm
Error.assertion(false, getInstanceName() + " got uninstantiated binding", sourceInfo());
then
fail();
end match;
end typeBinding;
function checkBindingEach
input Binding binding;
protected
list<InstNode> parents;
algorithm
if Binding.isEach(binding) then
parents := listRest(Binding.parents(binding));
for parent in parents loop
if Type.isArray(InstNode.getType(parent)) then
return;
end if;
end for;
Error.addSourceMessage(Error.EACH_ON_NON_ARRAY,
{InstNode.name(listHead(parents))}, Binding.getInfo(binding));
end if;
end checkBindingEach;
function typeComponentCondition
input output Binding condition;
input ExpOrigin.Type origin;
algorithm
condition := match condition
local
Expression exp;
Type ty;
Variability var;
SourceInfo info;
MatchKind mk;
case Binding.UNTYPED_BINDING(bindingExp = exp)
algorithm
info := Binding.getInfo(condition);
(exp, ty, var) := typeExp(exp, ExpOrigin.setFlag(origin, ExpOrigin.CONDITION), info);
(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;
then
Binding.TYPED_BINDING(exp, ty, var, condition.parents, false, false, info);
end match;
end typeComponentCondition;
function typeTypeAttribute
input output Modifier attribute;
input Type ty;
input InstNode component;
input ExpOrigin.Type origin;
protected
String name;
Binding binding;
InstNode mod_parent;
algorithm
() := match attribute
// Normal modifier with no submodifiers.
case Modifier.MODIFIER(name = name, binding = binding, subModifiers = ModTable.EMPTY())
algorithm
// Type and type check the attribute.
checkBindingEach(binding);
binding := typeBinding(binding, origin);
binding := TypeCheck.matchBinding(binding, 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;
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 typeExp
"Types an untyped expression, returning the typed expression itself along with
its type and variability."
input output Expression exp;
input ExpOrigin.Type origin;
input SourceInfo info;
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.Type next_origin;