/
NFClassTree.mo
2084 lines (1830 loc) · 69.5 KB
/
NFClassTree.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 NFClassTree
import NFInstNode.InstNode;
import SCode;
import NFType.Type;
import Mutable;
import NFModifier.Modifier;
import Import = NFImport;
import NFBuiltin;
protected
import Array;
import Error;
import MetaModelica.Dangerous.*;
import NFClass.Class;
import NFComponent.Component;
import Inst = NFInst;
import List;
import Lookup = NFLookup;
import SCodeDump;
import NFInstNode.InstNodeType;
public
encapsulated package LookupTree
uniontype Entry
record CLASS
Integer index;
end CLASS;
record COMPONENT
Integer index;
end COMPONENT;
record IMPORT
Integer index;
end IMPORT;
function index
input Entry entry;
output Integer index;
algorithm
index := match entry
case CLASS() then entry.index;
case COMPONENT() then entry.index;
case IMPORT() then entry.index;
end match;
end index;
function isEqual
input Entry entry1;
input Entry entry2;
output Boolean isEqual = index(entry1) == index(entry2);
end isEqual;
end Entry;
import BaseAvlTree;
extends BaseAvlTree(redeclare type Key = String,
redeclare type Value = Entry);
redeclare function extends keyStr
algorithm
outString := inKey;
end keyStr;
redeclare function extends valueStr
algorithm
outString := match inValue
case Entry.CLASS() then "class " + String(inValue.index);
case Entry.COMPONENT() then "comp " + String(inValue.index);
end match;
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := stringCompare(inKey1, inKey2);
end keyCompare;
annotation(__OpenModelica_Interface="util");
end LookupTree;
encapsulated package DuplicateTree
import NFClassTree.LookupTree;
import NFInstNode.InstNode;
type EntryType = enumeration(DUPLICATE, REDECLARE, ENTRY);
uniontype Entry
record ENTRY
LookupTree.Entry entry;
Option<InstNode> node;
list<Entry> children;
EntryType ty;
end ENTRY;
end Entry;
extends BaseAvlTree(redeclare type Key = String,
redeclare type Value = Entry);
redeclare function extends keyStr
algorithm
outString := inKey;
end keyStr;
redeclare function extends valueStr
algorithm
outString := "";
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := stringCompare(inKey1, inKey2);
end keyCompare;
function newRedeclare
input LookupTree.Entry entry;
output Entry redecl = ENTRY(entry, NONE(), {}, EntryType.REDECLARE);
end newRedeclare;
function newDuplicate
input LookupTree.Entry kept;
input LookupTree.Entry duplicate;
output Entry entry = ENTRY(kept, NONE(), {newEntry(duplicate)}, EntryType.DUPLICATE);
end newDuplicate;
function newEntry
input LookupTree.Entry lentry;
output Entry entry = ENTRY(lentry, NONE(), {}, EntryType.ENTRY);
end newEntry;
function idExistsInEntry
input LookupTree.Entry id;
input Entry entry;
output Boolean exists;
algorithm
exists := LookupTree.Entry.isEqual(id, entry.entry) or
List.exist(entry.children, function idExistsInEntry(id = id));
end idExistsInEntry;
annotation(__OpenModelica_Interface="util");
end DuplicateTree;
constant ClassTree EMPTY = ClassTree.PARTIAL_TREE(LookupTree.EMPTY(),
listArray({}), listArray({}), listArray({}), listArray({}), DuplicateTree.EMPTY());
constant ClassTree EMPTY_FLAT = ClassTree.FLAT_TREE(LookupTree.EMPTY(),
listArray({}), listArray({}), listArray({}), DuplicateTree.EMPTY());
uniontype ClassTree
record PARTIAL_TREE
"A partial tree allows lookup of local classes and imported elements."
LookupTree.Tree tree;
array<InstNode> classes;
array<InstNode> components;
array<InstNode> exts;
array<Import> imports;
DuplicateTree.Tree duplicates;
end PARTIAL_TREE;
record EXPANDED_TREE
"Like partial tree, but the lookup tree is populated with all named
elements. The elements have not yet been added to the arrays though, so
lookup is still restricted to local classes and imported elements."
LookupTree.Tree tree;
array<InstNode> classes;
array<InstNode> components;
array<InstNode> exts;
array<Import> imports;
DuplicateTree.Tree duplicates;
end EXPANDED_TREE;
record INSTANTIATED_TREE
"Allows lookup of both local and inherited elements."
LookupTree.Tree tree;
array<Mutable<InstNode>> classes;
array<Mutable<InstNode>> components;
list<Integer> localComponents;
array<InstNode> exts;
array<Import> imports;
DuplicateTree.Tree duplicates;
end INSTANTIATED_TREE;
record FLAT_TREE
"A flattened version of an instantiated tree."
LookupTree.Tree tree;
array<InstNode> classes;
array<InstNode> components;
array<Import> imports;
DuplicateTree.Tree duplicates;
end FLAT_TREE;
record EMPTY_TREE
end EMPTY_TREE;
function fromSCode
"Creates a new class tree from a list of SCode elements."
input list<SCode.Element> elements;
input Boolean isClassExtends;
input InstNode parent;
output ClassTree tree;
protected
LookupTree.Tree ltree;
LookupTree.Entry lentry;
Integer clsc, compc, extc, i;
array<InstNode> clss, comps, exts;
Integer cls_idx = 0, ext_idx = 0, comp_idx = 0;
DuplicateTree.Tree dups;
list<Import> imps = {};
array<Import> imps_arr;
SourceInfo info;
algorithm
ltree := LookupTree.new();
// Count the different types of elements.
(clsc, compc, extc) := countElements(elements);
// If the class is a class extends, reserve space for the extends.
if isClassExtends then
extc := extc + 1;
end if;
// Preallocate arrays for the elements. We can't do this for imports
// though, since an import clause might import multiple elements.
clss := arrayCreateNoInit(clsc, InstNode.EMPTY_NODE());
comps := arrayCreateNoInit(compc + extc, InstNode.EMPTY_NODE());
exts := arrayCreateNoInit(extc, InstNode.EMPTY_NODE());
dups := DuplicateTree.new();
// Make a temporary class tree so we can do lookup for error reporting.
tree := PARTIAL_TREE(ltree, clss, comps, exts, listArray({}), dups);
// If the class is a class extends, fill in the first extends with an
// empty node so we don't have unassigned memory after this step.
if isClassExtends then
exts[1] := InstNode.EMPTY_NODE();
comps[1] := InstNode.REF_NODE(1);
ext_idx := ext_idx + 1;
comp_idx := comp_idx + 1;
end if;
for e in elements loop
() := match e
// A class, add it to the class array and add an entry in the lookup tree.
case SCode.CLASS()
algorithm
cls_idx := cls_idx + 1;
arrayUpdateNoBoundsChecking(clss, cls_idx, InstNode.newClass(e, parent));
lentry := LookupTree.Entry.CLASS(cls_idx);
ltree := addLocalElement(e.name, lentry, tree, ltree);
// If the class is an element redeclare, add an entry in the duplicate
// tree so we can check later that it actually redeclares something.
if SCode.isElementRedeclare(e) or SCode.isClassExtends(e) then
dups := DuplicateTree.add(dups, e.name, DuplicateTree.newRedeclare(lentry));
end if;
then
();
// A component, add it to the component array but don't add an entry
// in the lookup tree. We need to preserve the components' order, but
// won't know their actual indices until we've expanded the extends.
// We don't really need to be able to look up components until after
// that happens, so we add them to the lookup tree later instead.
case SCode.COMPONENT()
algorithm
comp_idx := comp_idx + 1;
arrayUpdateNoBoundsChecking(comps, comp_idx, InstNode.newComponent(e));
then
();
// An extends clause, add it to the list of extends, and also add a
// reference in the component array so we can preserve the order of
// components.
case SCode.EXTENDS()
algorithm
ext_idx := ext_idx + 1;
arrayUpdateNoBoundsChecking(exts, ext_idx, InstNode.newExtends(e, parent));
comp_idx := comp_idx + 1;
arrayUpdateNoBoundsChecking(comps, comp_idx, InstNode.REF_NODE(ext_idx));
then
();
// An unqualified import clause. We need to know which names are
// imported by the clause, so it needs to be instantiated.
case SCode.IMPORT(imp = Absyn.Import.UNQUAL_IMPORT(), info = info)
algorithm
imps := Import.instUnqualified(e.imp, parent, info, imps);
then
();
// A qualified import clause. Since the import itself gives the name
// of the imported element we can delay resolving the path until we
// need it (i.e. when the name is used). Doing so avoids some
// dependency issues, like when a package is imported into one of it's
// enclosing scopes.
case SCode.IMPORT()
algorithm
imps := Import.UNRESOLVED_IMPORT(e.imp, parent, e.info) :: imps;
then
();
//else
// algorithm
// print(getInstanceName() + " skipping:\n" +
// SCodeDump.unparseElementStr(e) + "\n");
// then
// ();
end match;
end for;
// Add all the imported names to the lookup tree.
imps_arr := listArray(imps);
i := 1;
for e in imps loop
ltree := addImport(e, i, ltree, imps_arr);
i := i + 1;
end for;
tree := PARTIAL_TREE(ltree, clss, comps, exts, imps_arr, dups);
end fromSCode;
function fromEnumeration
"Creates a class tree for an enumeration type."
input list<SCode.Enum> literals "The SCode literals";
input Type enumType "The type of the enumeration";
input InstNode enumClass "The InstNode of the enumeration type";
output ClassTree tree;
protected
array<InstNode> comps;
Integer attr_count = 5;
Integer i = 0;
InstNode comp;
LookupTree.Tree ltree;
String name;
algorithm
comps := arrayCreateNoInit(listLength(literals) + attr_count, InstNode.EMPTY_NODE());
ltree := NFBuiltin.ENUM_LOOKUP_TREE;
arrayUpdateNoBoundsChecking(comps, 1, InstNode.fromComponent("quantity",
Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD()), enumClass));
arrayUpdateNoBoundsChecking(comps, 2, InstNode.fromComponent("min",
Component.TYPE_ATTRIBUTE(enumType, Modifier.NOMOD()), enumClass));
arrayUpdateNoBoundsChecking(comps, 3, InstNode.fromComponent("max",
Component.TYPE_ATTRIBUTE(enumType, Modifier.NOMOD()), enumClass));
arrayUpdateNoBoundsChecking(comps, 4, InstNode.fromComponent("start",
Component.TYPE_ATTRIBUTE(enumType, Modifier.NOMOD()), enumClass));
arrayUpdateNoBoundsChecking(comps, 5, InstNode.fromComponent("fixed",
Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD()), enumClass));
for l in literals loop
// Make a new component node for the literal and add it to the lookup tree.
name := l.literal;
i := i + 1;
comp := InstNode.fromComponent(name, Component.newEnum(enumType, name, i), enumClass);
arrayUpdateNoBoundsChecking(comps, i + attr_count, comp);
ltree := LookupTree.add(ltree, name, LookupTree.Entry.COMPONENT(i + attr_count),
function addEnumConflict(literal = comp));
end for;
// Enumerations can't contain extends, so we can go directly to a flat tree here.
tree := FLAT_TREE(ltree, listArray({}), comps, listArray({}), DuplicateTree.EMPTY());
end fromEnumeration;
function expand
"This function adds all local and inherited class and component names to
the lookup tree. Note that only their names are added, the elements
themselves are added to their respective arrays by the instantiation
function below."
input output ClassTree tree;
protected
LookupTree.Tree ltree;
LookupTree.Entry lentry;
array<InstNode> exts, clss, comps;
array<Import> imps;
list<tuple<Integer, Integer>> ext_idxs = {};
Integer ccount, cls_idx, comp_idx = 1;
DuplicateTree.Tree dups;
Mutable<DuplicateTree.Tree> dups_ptr;
algorithm
PARTIAL_TREE(ltree, clss, comps, exts, imps, dups) := tree;
cls_idx := arrayLength(clss) + 1;
// Since we now know the names of both local and inherited components we
// can add them to the lookup tree. First we add the local components'
// names, to be able to catch duplicate local elements easier.
for c in comps loop
() := match c
// A component. Add its name to the lookup tree.
case InstNode.COMPONENT_NODE()
algorithm
lentry := LookupTree.Entry.COMPONENT(comp_idx);
ltree := addLocalElement(InstNode.name(c), lentry, tree, ltree);
// If the component is an element redeclare, add an entry in the duplicate
// tree so we can check later that it actually redeclares something.
if InstNode.isRedeclare(c) then
dups := DuplicateTree.add(dups, c.name, DuplicateTree.newRedeclare(lentry));
end if;
comp_idx := comp_idx + 1;
then
();
// An extends node. Save the index so we know where to start adding
// components later, and increment the index with the number of
// components it contains.
case InstNode.REF_NODE()
algorithm
ext_idxs := (cls_idx - 1, comp_idx - 1) :: ext_idxs;
(cls_idx, comp_idx) := countInheritedElements(exts[c.index], cls_idx, comp_idx);
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid component", sourceInfo());
then
fail();
end match;
end for;
// Checking whether inherited duplicate elements are identical is hard to
// do correctly at this point. So we just detect them and store their
// indices in the class tree for now, and check them for identicalness
// later on instead.
dups_ptr := Mutable.create(dups);
// Add the names of inherited components and classes to the lookup tree.
if not listEmpty(ext_idxs) then
// Use the component indices we saved earlier to add the required
// elements from the extends nodes to the lookup tree.
ext_idxs := listReverseInPlace(ext_idxs);
for ext in exts loop
(cls_idx, comp_idx) :: ext_idxs := ext_idxs;
ltree := expandExtends(ext, ltree, cls_idx, comp_idx, dups_ptr);
end for;
end if;
tree := EXPANDED_TREE(ltree, clss, comps, exts, imps, Mutable.access(dups_ptr));
end expand;
function instantiate
"This function instantiates an expanded tree. clsNode is the class to
be instantiated, while instance is the instance the clsNode belongs to.
instance is usually the component which has the class as its type. In
some cases the class itself is the instance, like for the top-level
model that's being instantiated or packages used for lookup. Because the
actual instance of clsNode will then be the cloned clsNode created by
this function it's not possible to send in the correct instance in that
case, so setting the instance to an empty node is interpreted by this
function to mean that the instance should be set to the cloned clsNode."
input output InstNode clsNode;
input output InstNode instance = InstNode.EMPTY_NODE();
input InstNode scope = InstNode.EMPTY_NODE();
output Integer classCount = 0;
output Integer compCount = 0;
protected
Class cls;
ClassTree tree, ext_tree;
LookupTree.Tree ltree;
array<InstNode> exts, old_clss, old_comps;
array<Import> imps;
array<Mutable<InstNode>> clss, comps, ext_clss;
list<Integer> local_comps = {};
Integer cls_idx = 1, comp_idx = 1, cls_count, comp_count;
InstNode node, parent_scope, inner_node, inst_scope;
DuplicateTree.Tree dups;
Component comp;
SCode.Element ext_def;
algorithm
// TODO: If we don't have any extends we could probably generate a flat
// tree directly and skip a lot of this.
// Clone the class node by replacing the class in the node with itself.
cls := InstNode.getClass(clsNode);
clsNode := InstNode.replaceClass(cls, clsNode);
() := match cls
case Class.EXPANDED_CLASS(elements = INSTANTIATED_TREE())
then ();
case Class.EXPANDED_CLASS()
algorithm
// If the instance is an empty node, use the cloned clsNode as the instance.
if InstNode.isEmpty(instance) then
instance := clsNode;
parent_scope := InstNode.parent(clsNode);
else
parent_scope := instance;
inst_scope := scope;
end if;
inst_scope := if InstNode.isEmpty(scope) then instance else scope;
// Fetch the elements from the class tree.
EXPANDED_TREE(ltree, old_clss, old_comps, exts, imps, dups) := cls.elements;
// Count the number of local classes and components we have.
classCount := arrayLength(old_clss);
// The component array contains placeholders for extends, so the length of the
// extends array needs to be subtracted here to get the number of components.
compCount := arrayLength(old_comps) - arrayLength(exts);
// Make a new extends array, and recursively instantiate the extends nodes.
exts := arrayCopy(exts);
for i in 1:arrayLength(exts) loop
// Update the parent of the extends to be the new instance.
node := exts[i];
InstNodeType.BASE_CLASS(definition = ext_def) := InstNode.nodeType(node);
node := InstNode.setNodeType(InstNodeType.BASE_CLASS(instance, ext_def), node);
// Instantiate the class tree of the extends.
(node, _, cls_count, comp_count) := instantiate(node, InstNode.EMPTY_NODE(), inst_scope);
exts[i] := node;
// Add the inherited elements to the class/component counts.
classCount := cls_count + classCount;
compCount := comp_count + compCount;
end for;
// Create new arrays that can hold both local and inherited elements.
comps := arrayCreateNoInit(compCount, /*dummy*/Mutable.create(InstNode.EMPTY_NODE()));
clss := arrayCreateNoInit(classCount, /*dummy*/Mutable.create(InstNode.EMPTY_NODE()));
// Copy the local classes into the new class array, and set the
// class we're instantiating to be their parent.
for c in old_clss loop
c := InstNode.setParent(clsNode, c);
// If the class is outer, check that it's valid and link it with
// the corresponding inner class.
if InstNode.isOuter(c) then
checkOuterClass(c);
c := linkInnerOuter(c, parent_scope);
end if;
arrayUpdateNoBoundsChecking(clss, cls_idx, Mutable.create(c));
cls_idx := cls_idx + 1;
end for;
// Copy inherited classes into the new class array. Note that inherited
// classes are just inserted after the local ones, and not where the
// extends say they should go. The order shouldn't matter for classes,
// and otherwise we wouldn't be able to reuse the lookup tree.
for ext in exts loop
() := match Class.classTree(InstNode.getClass(ext))
case INSTANTIATED_TREE(classes = ext_clss)
algorithm
cls_count := arrayLength(ext_clss);
if cls_count > 0 then
Array.copyRange(ext_clss, clss, 1, cls_count, cls_idx);
cls_idx := cls_idx + cls_count;
end if;
then
();
else ();
end match;
end for;
// Copy both local and inherited components into the new array.
for c in old_comps loop
() := match c
case InstNode.COMPONENT_NODE()
algorithm
// Set the component's parent and create a unique instance for it.
node := InstNode.setParent(instance, c);
comp := InstNode.component(node);
node := InstNode.replaceComponent(comp, node);
// If the component is outer, link it with the corresponding
// inner component.
if Component.isOuter(comp) then
node := linkInnerOuter(node, inst_scope);
end if;
// Add the node to the component array.
arrayUpdateNoBoundsChecking(comps, comp_idx, Mutable.create(node));
local_comps := comp_idx :: local_comps;
comp_idx := comp_idx + 1;
then
();
case InstNode.REF_NODE()
algorithm
comp_idx := instExtendsComps(exts[c.index], comps, comp_idx);
then
();
end match;
end for;
// Sanity check.
if comp_idx <> compCount + 1 then
Error.assertion(false, getInstanceName() + " miscounted components in " +
InstNode.name(clsNode), sourceInfo());
end if;
if cls_idx <> classCount + 1 then
Error.assertion(false, getInstanceName() + " miscounted classes in " +
InstNode.name(clsNode), sourceInfo());
end if;
// Create a new class tree and update the class in the node.
cls.elements := INSTANTIATED_TREE(ltree, clss, comps, local_comps, exts, imps, dups);
then
();
case Class.EXPANDED_DERIVED(baseClass = node)
algorithm
node := InstNode.setNodeType(
InstNodeType.BASE_CLASS(clsNode, InstNode.definition(node)), node);
(node, _, classCount, compCount) := instantiate(node);
cls.baseClass := node;
then
();
case Class.PARTIAL_BUILTIN(elements = tree as FLAT_TREE(components = old_comps))
algorithm
instance := if InstNode.isEmpty(instance) then clsNode else instance;
old_comps := arrayCopy(old_comps);
for i in 1:arrayLength(old_comps) loop
node := old_comps[i];
node := InstNode.setParent(instance, node);
old_comps[i] := InstNode.replaceComponent(InstNode.component(node), node);
end for;
tree.components := old_comps;
cls.elements := tree;
compCount := arrayLength(old_comps);
then
();
case Class.PARTIAL_BUILTIN() then ();
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid class", sourceInfo());
then
fail();
end match;
InstNode.updateClass(cls, clsNode);
end instantiate;
function fromRecordConstructor
input list<InstNode> inputs;
input list<InstNode> locals;
input InstNode out;
output ClassTree tree = EMPTY;
protected
LookupTree.Tree ltree = LookupTree.new();
Integer i = 1;
array<InstNode> comps;
algorithm
comps := arrayCreateNoInit(listLength(inputs) + listLength(locals) + 1, InstNode.EMPTY_NODE());
for ci in inputs loop
comps[i] := ci;
ltree := addLocalElement(InstNode.name(ci), LookupTree.Entry.COMPONENT(i), tree, ltree);
i := i + 1;
end for;
for cl in locals loop
comps[i] := cl;
ltree := addLocalElement(InstNode.name(cl), LookupTree.Entry.COMPONENT(i), tree, ltree);
i := i + 1;
end for;
comps[i] := out;
ltree := addLocalElement(InstNode.name(out), LookupTree.Entry.COMPONENT(i), tree, ltree);
tree := FLAT_TREE(ltree, listArray({}), comps, listArray({}), DuplicateTree.new());
end fromRecordConstructor;
function mapRedeclareChains
input ClassTree tree;
input FuncT func;
partial function FuncT
input list<Mutable<InstNode>> chain;
end FuncT;
algorithm
() := match tree
case INSTANTIATED_TREE() guard not DuplicateTree.isEmpty(tree.duplicates)
algorithm
DuplicateTree.map(tree.duplicates,
function mapRedeclareChain(func = func, tree = tree));
then
();
else ();
end match;
end mapRedeclareChains;
function replaceDuplicates
"This function replaces all duplicate elements with the element that is
kept, such that lookup in the extends nodes will find the correct node."
input output ClassTree tree;
algorithm
() := match tree
case INSTANTIATED_TREE() guard not DuplicateTree.isEmpty(tree.duplicates)
algorithm
tree.duplicates := DuplicateTree.map(tree.duplicates,
function replaceDuplicates2(tree = tree));
then
();
else ();
end match;
end replaceDuplicates;
function appendComponentsToInstTree
"Appens a list of local components to an instantiated class tree."
input list<Mutable<InstNode>> components;
input output ClassTree tree;
algorithm
if listEmpty(components) then
return;
else
() := match tree
local
Integer comp_idx;
list<Integer> local_comps;
case INSTANTIATED_TREE()
algorithm
comp_idx := arrayLength(tree.components);
tree.components := Array.appendList(tree.components, components);
local_comps := tree.localComponents;
for i in comp_idx+1:comp_idx+listLength(components) loop
local_comps := i :: local_comps;
end for;
tree.localComponents := local_comps;
then
();
end match;
end if;
end appendComponentsToInstTree;
function flatten
input output ClassTree tree;
algorithm
tree := match tree
local
array<InstNode> clss, comps;
Integer clsc, compc;
list<Integer> dup_cls, dup_comp;
case INSTANTIATED_TREE()
algorithm
(dup_cls, dup_comp) := enumerateDuplicates(tree.duplicates);
clsc := arrayLength(tree.classes);
compc := arrayLength(tree.components);
clss := arrayCreateNoInit(clsc, InstNode.EMPTY_NODE());
comps := arrayCreateNoInit(compc, InstNode.EMPTY_NODE());
flatten2(tree.classes, clss, dup_cls);
flatten2(tree.components, comps, dup_comp);
then
FLAT_TREE(tree.tree, clss, comps, tree.imports, tree.duplicates);
else tree;
end match;
end flatten;
function flatten2
input array<Mutable<InstNode>> elements;
input array<InstNode> flatElements;
input list<Integer> duplicates;
algorithm
for i in 1:arrayLength(elements) loop
arrayUpdateNoBoundsChecking(flatElements, i,
Mutable.access(arrayGetNoBoundsChecking(elements, i)));
end for;
for i in duplicates loop
arrayUpdateNoBoundsChecking(flatElements, i, InstNode.EMPTY_NODE());
end for;
end flatten2;
function lookupElement
"Returns the class or component with the given name in the class tree."
input String name;
input ClassTree tree;
output InstNode element;
output Boolean isImport;
protected
LookupTree.Entry entry;
algorithm
entry := LookupTree.get(lookupTree(tree), name);
(element, isImport) := resolveEntry(entry, tree);
end lookupElement;
function lookupElementPtr
input String name;
input ClassTree tree;
output Mutable<InstNode> element;
protected
LookupTree.Entry entry;
algorithm
entry := LookupTree.get(lookupTree(tree), name);
element := resolveEntryPtr(entry, tree);
end lookupElementPtr;
function lookupElementsPtr
input String name;
input ClassTree tree;
output list<Mutable<InstNode>> elements;
protected
DuplicateTree.Entry dup_entry;
algorithm
try
dup_entry := DuplicateTree.get(getDuplicates(tree), name);
elements := resolveDuplicateEntriesPtr(dup_entry, tree);
else
elements := {lookupElementPtr(name, tree)};
end try;
end lookupElementsPtr;
function lookupComponentIndex
input String name;
input ClassTree tree;
output Integer index;
algorithm
LookupTree.Entry.COMPONENT(index = index) :=
LookupTree.get(lookupTree(tree), name);
end lookupComponentIndex;
function nthComponent
input Integer index;
input ClassTree tree;
output InstNode component;
algorithm
component := match tree
case PARTIAL_TREE() then arrayGet(tree.components, index);
case EXPANDED_TREE() then arrayGet(tree.components, index);
case INSTANTIATED_TREE() then Mutable.access(arrayGet(tree.components, index));
case FLAT_TREE() then arrayGet(tree.components, index);
end match;
end nthComponent;
function mapClasses
input ClassTree tree;
input FuncT func;
partial function FuncT
input output InstNode extendsNode;
end FuncT;
protected
array<InstNode> clss = getClasses(tree);
algorithm
for i in 1:arrayLength(clss) loop
arrayUpdateNoBoundsChecking(clss, i,
func(arrayGetNoBoundsChecking(clss, i)));
end for;
end mapClasses;
function foldClasses<ArgT>
input ClassTree tree;
input FuncT func;
input output ArgT arg;
partial function FuncT
input InstNode clsNode;
input output ArgT arg;
end FuncT;
protected
array<InstNode> clss = getClasses(tree);
algorithm
for cls in clss loop
arg := func(cls, arg);
end for;
end foldClasses;
function applyExtends
input ClassTree tree;
input FuncT func;
partial function FuncT
input InstNode extendsNode;
end FuncT;
protected
array<InstNode> exts = getExtends(tree);
algorithm
for ext in exts loop
func(ext);
end for;
end applyExtends;
function mapExtends
"Applies a function to each extends node in the class tree, and updates
the extends array with the returned nodes."
input ClassTree tree;
input FuncT func;
partial function FuncT
input output InstNode extendsNode;
end FuncT;
protected
array<InstNode> exts = getExtends(tree);
algorithm
for i in 1:arrayLength(exts) loop
arrayUpdateNoBoundsChecking(exts, i,
func(arrayGetNoBoundsChecking(exts, i)));
end for;
end mapExtends;
function foldExtends<ArgT>
input ClassTree tree;
input FuncT func;
input output ArgT arg;
partial function FuncT
input InstNode extendsNode;
input output ArgT arg;
end FuncT;
protected
array<InstNode> exts = getExtends(tree);
algorithm
for ext in exts loop
arg := func(ext, arg);
end for;
end foldExtends;
function mapFoldExtends<ArgT>
"Applies a mutating function to each extends node in the class tree.
A given argument is also folded and returned."
input ClassTree tree;
input FuncT func;
input output ArgT arg;
partial function FuncT
input output InstNode ext;
input output ArgT arg;
end FuncT;
protected
array<InstNode> exts = getExtends(tree);
InstNode ext;
algorithm
for i in 1:arrayLength(exts) loop
(ext, arg) := func(arrayGetNoBoundsChecking(exts, i), arg);
arrayUpdateNoBoundsChecking(exts, i, ext);
end for;
end mapFoldExtends;
function applyLocalComponents
input ClassTree tree;
input FuncT func;
partial function FuncT
input InstNode component;
end FuncT;
algorithm
() := match tree
case INSTANTIATED_TREE()
algorithm
for i in tree.localComponents loop
func(Mutable.access(arrayGetNoBoundsChecking(tree.components, i)));
end for;
then
();
case PARTIAL_TREE()
algorithm
for c in tree.components loop
func(c);