/
NFInstNode.mo
1628 lines (1447 loc) · 43.9 KB
/
NFInstNode.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 NFInstNode
import NFComponent.Component;
import NFClass.Class;
import SCode;
import Absyn;
import AbsynUtil;
import Type = NFType;
import NFFunction.Function;
import Pointer;
import Error;
import Prefixes = NFPrefixes;
import Visibility = NFPrefixes.Visibility;
import NFModifier.Modifier;
import SCodeDump;
import DAE;
import Expression = NFExpression;
protected
import List;
import ConvertDAE = NFConvertDAE;
import Restriction = NFRestriction;
import NFClassTree.ClassTree;
import SCodeUtil;
public
uniontype InstNodeType
record NORMAL_CLASS
"An element with no specific characteristics."
end NORMAL_CLASS;
record BASE_CLASS
"A base class extended by another class."
InstNode parent;
SCode.Element definition "The extends clause definition.";
end BASE_CLASS;
record DERIVED_CLASS
"A short class definition."
InstNodeType ty "The base node type not considering that it's a derived class.";
end DERIVED_CLASS;
record BUILTIN_CLASS
"A builtin element."
end BUILTIN_CLASS;
record TOP_SCOPE
"The unnamed class containing all the top-level classes."
end TOP_SCOPE;
record ROOT_CLASS
"The root of the instance tree, i.e. the class that the instantiation starts from."
InstNode parent "The parent of the class, e.g. when instantiating a function
in a component where the component is the parent.";
end ROOT_CLASS;
record NORMAL_COMP
end NORMAL_COMP;
record REDECLARED_COMP
InstNode parent "The parent of the replaced component";
end REDECLARED_COMP;
end InstNodeType;
encapsulated package NodeTree
import BaseAvlTree;
import NFInstNode.InstNode;
extends BaseAvlTree(redeclare type Key = String,
redeclare type Value = InstNode);
redeclare function extends keyStr
algorithm
outString := inKey;
end keyStr;
redeclare function extends valueStr
algorithm
outString := InstNode.toString(inValue);
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := stringCompare(inKey1, inKey2);
end keyCompare;
annotation(__OpenModelica_Interface="util");
end NodeTree;
constant Integer NUMBER_OF_CACHES = 3;
uniontype CachedData
record NO_CACHE end NO_CACHE;
record PACKAGE
InstNode instance;
end PACKAGE;
record FUNCTION
list<Function> funcs;
Boolean typed;
Boolean specialBuiltin;
end FUNCTION;
record TOP_SCOPE
NodeTree.Tree addedInner;
InstNode rootClass;
end TOP_SCOPE;
function empty
output array<CachedData> cache = arrayCreate(NUMBER_OF_CACHES, NO_CACHE());
end empty;
function initFunc
input array<CachedData> caches;
protected
CachedData func_cache;
algorithm
func_cache := getFuncCache(caches);
func_cache := match func_cache
case NO_CACHE() then FUNCTION({}, false, false);
case FUNCTION() then func_cache;
end match;
setFuncCache(caches, func_cache);
end initFunc;
function addFunc
input Function fn;
input Boolean specialBuiltin;
input array<CachedData> caches;
protected
CachedData func_cache;
algorithm
func_cache := getFuncCache(caches);
func_cache := match func_cache
case NO_CACHE() then FUNCTION({fn}, false, specialBuiltin);
// Append to end so the error messages are ordered properly.
case FUNCTION() then FUNCTION(listAppend(func_cache.funcs,{fn}), false,
func_cache.specialBuiltin or specialBuiltin);
else
algorithm
Error.assertion(false, getInstanceName() + ": Invalid cache for function", sourceInfo());
then
fail();
end match;
setFuncCache(caches, func_cache);
end addFunc;
function getFuncCache
input array<CachedData> in_caches;
output CachedData out_cache = arrayGet(in_caches, 1);
end getFuncCache;
function setFuncCache
input array<CachedData> in_caches;
input CachedData in_cache;
algorithm
arrayUpdate(in_caches, 1, in_cache);
end setFuncCache;
function getPackageCache
input array<CachedData> in_caches;
output CachedData out_cache = arrayGet(in_caches, 2);
end getPackageCache;
function setPackageCache
input array<CachedData> in_caches;
input CachedData in_cache;
output array<CachedData> out_caches = arrayUpdate(in_caches, 2, in_cache);
end setPackageCache;
function clearPackageCache
input array<CachedData> in_caches;
output array<CachedData> out_caches = arrayUpdate(in_caches, 2, NO_CACHE());
end clearPackageCache;
function getInnerOuterCache
input array<CachedData> in_caches;
output CachedData out_cache = arrayGet(in_caches, 3);
end getInnerOuterCache;
function setInnerOuterCache
input array<CachedData> in_caches;
input CachedData in_cache;
output array<CachedData> out_caches = arrayUpdate(in_caches, 3, in_cache);
end setInnerOuterCache;
end CachedData;
uniontype InstNode
record CLASS_NODE
String name;
SCode.Element definition;
Visibility visibility;
Pointer<Class> cls;
array<CachedData> caches;
InstNode parentScope;
InstNodeType nodeType;
end CLASS_NODE;
record COMPONENT_NODE
String name;
Visibility visibility;
Pointer<Component> component;
InstNode parent "The instance that this component is part of.";
InstNodeType nodeType;
end COMPONENT_NODE;
record INNER_OUTER_NODE
"A node representing an outer element, with a reference to the corresponding inner."
InstNode innerNode;
InstNode outerNode;
end INNER_OUTER_NODE;
record REF_NODE
Integer index;
end REF_NODE;
record NAME_NODE
String name;
end NAME_NODE;
record IMPLICIT_SCOPE
InstNode parentScope;
list<InstNode> locals;
end IMPLICIT_SCOPE;
record EXP_NODE
Expression exp;
end EXP_NODE;
record EMPTY_NODE end EMPTY_NODE;
function new
input SCode.Element definition;
input InstNode parent;
output InstNode node;
algorithm
node := match definition
case SCode.CLASS() then newClass(definition, parent);
case SCode.COMPONENT() then newComponent(definition, parent);
end match;
end new;
function newClass
input SCode.Element definition;
input InstNode parent;
input InstNodeType nodeType = NORMAL_CLASS();
output InstNode node;
protected
String name;
SCode.Visibility vis;
algorithm
SCode.CLASS(name = name, prefixes = SCode.PREFIXES(visibility = vis)) := definition;
node := CLASS_NODE(name, definition, Prefixes.visibilityFromSCode(vis),
Pointer.create(Class.NOT_INSTANTIATED()), CachedData.empty(), parent, nodeType);
end newClass;
function newComponent
input SCode.Element definition;
input InstNode parent = EMPTY_NODE();
output InstNode node;
protected
String name;
SCode.Visibility vis;
algorithm
SCode.COMPONENT(name = name, prefixes = SCode.PREFIXES(visibility = vis)) := definition;
node := COMPONENT_NODE(name, Prefixes.visibilityFromSCode(vis),
Pointer.create(Component.new(definition)), parent, InstNodeType.NORMAL_COMP());
end newComponent;
function newExtends
input SCode.Element definition;
input InstNode parent;
output InstNode node;
protected
Absyn.Path base_path;
String name;
SCode.Visibility vis;
algorithm
SCode.Element.EXTENDS(baseClassPath = base_path, visibility = vis) := definition;
name := AbsynUtil.pathLastIdent(base_path);
node := CLASS_NODE(name, definition, Prefixes.visibilityFromSCode(vis),
Pointer.create(Class.NOT_INSTANTIATED()), CachedData.empty(), parent,
InstNodeType.BASE_CLASS(parent, definition));
end newExtends;
function fromComponent
input String name;
input Component component;
input InstNode parent;
output InstNode node;
algorithm
node := COMPONENT_NODE(name, Visibility.PUBLIC, Pointer.create(component),
parent, InstNodeType.NORMAL_COMP());
end fromComponent;
function isClass
input InstNode node;
output Boolean isClass;
algorithm
isClass := match node
case CLASS_NODE() then true;
case INNER_OUTER_NODE() then isClass(node.innerNode);
else false;
end match;
end isClass;
function isBaseClass
input InstNode node;
output Boolean isBaseClass;
algorithm
isBaseClass := match node
case CLASS_NODE(nodeType = InstNodeType.BASE_CLASS()) then true;
else false;
end match;
end isBaseClass;
function isUserdefinedClass
input InstNode node;
output Boolean isUserdefined;
algorithm
isUserdefined := match node
case CLASS_NODE()
then match node.nodeType
case InstNodeType.NORMAL_CLASS() then true;
case InstNodeType.BASE_CLASS() then true;
case InstNodeType.DERIVED_CLASS() then true;
else false;
end match;
else false;
end match;
end isUserdefinedClass;
function isDerivedClass
input InstNode node;
output Boolean isDerived;
algorithm
isDerived := match node
case CLASS_NODE(nodeType = InstNodeType.DERIVED_CLASS()) then true;
else false;
end match;
end isDerivedClass;
function isFunction
input InstNode node;
output Boolean isFunc;
algorithm
isFunc := match node
case CLASS_NODE() then Class.isFunction(Pointer.access(node.cls));
else false;
end match;
end isFunction;
function isComponent
input InstNode node;
output Boolean isComponent;
algorithm
isComponent := match node
case COMPONENT_NODE() then true;
case INNER_OUTER_NODE() then isComponent(node.innerNode);
else false;
end match;
end isComponent;
function isRef
input InstNode node;
output Boolean isRef;
algorithm
isRef := match node
case REF_NODE() then true;
else false;
end match;
end isRef;
function isEmpty
input InstNode node;
output Boolean isEmpty;
algorithm
isEmpty := match node
case EMPTY_NODE() then true;
else false;
end match;
end isEmpty;
function isImplicit
input InstNode node;
output Boolean isImplicit;
algorithm
isImplicit := match node
case IMPLICIT_SCOPE() then true;
else false;
end match;
end isImplicit;
function isName
input InstNode node;
output Boolean isName;
algorithm
isName := match node
case NAME_NODE() then true;
else false;
end match;
end isName;
function isConnector
input InstNode node;
output Boolean isConnector;
algorithm
isConnector := match node
case COMPONENT_NODE() then Component.isConnector(component(node));
case NAME_NODE() then true;
else false;
end match;
end isConnector;
function isExpandableConnector
input InstNode node;
output Boolean isConnector;
algorithm
isConnector := match node
case COMPONENT_NODE() then Component.isExpandableConnector(component(node));
else false;
end match;
end isExpandableConnector;
function hasParentExpandableConnector
"@author: adrpo
returns true if itself or any of the parents are expandable connectors"
input InstNode node;
output Boolean b = isExpandableConnector(node);
protected
InstNode p;
algorithm
p := node;
while not isEmpty(p) loop
p := parent(p);
b := boolOr(b, isExpandableConnector(p));
if b then
break;
end if;
end while;
end hasParentExpandableConnector;
function isOperator
input InstNode node;
output Boolean op;
algorithm
op := match node
case CLASS_NODE() then SCodeUtil.isOperator(node.definition);
case INNER_OUTER_NODE() then isOperator(node.innerNode);
else false;
end match;
end isOperator;
function name
input InstNode node;
output String name;
algorithm
name := match node
case CLASS_NODE() then node.name;
case COMPONENT_NODE() then node.name;
case INNER_OUTER_NODE() then name(node.innerNode);
// For bug catching, these names should never be used.
case REF_NODE() then "$REF[" + String(node.index) + "]";
case NAME_NODE() then node.name;
case IMPLICIT_SCOPE() then "$IMPLICIT";
case EMPTY_NODE() then "$EMPTY";
end match;
end name;
function className
input InstNode node;
output String name;
algorithm
CLASS_NODE(name = name) := node;
end className;
function scopeName
"Returns the name of a scope, which in the case of a component is the name
of the component's type, and for a class simply the name of the class."
input InstNode node;
output String outName = name(classScope(explicitScope(node)));
end scopeName;
function typeName
"Returns the type of node the given node is as a string."
input InstNode node;
output String name;
algorithm
name := match node
case CLASS_NODE() then "class";
case COMPONENT_NODE() then "component";
case INNER_OUTER_NODE() then typeName(node.innerNode);
case REF_NODE() then "ref node";
case NAME_NODE() then "name node";
case IMPLICIT_SCOPE() then "implicit scope";
case EMPTY_NODE() then "empty node";
end match;
end typeName;
function rename
input String name;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE()
algorithm
node.name := name;
then
();
case COMPONENT_NODE()
algorithm
node.name := name;
then
();
end match;
end rename;
function parent
input InstNode node;
output InstNode parent;
algorithm
parent := match node
case CLASS_NODE() then node.parentScope;
case COMPONENT_NODE() then node.parent;
case IMPLICIT_SCOPE() then node.parentScope;
else EMPTY_NODE();
end match;
end parent;
function explicitParent
input InstNode node;
output InstNode parentNode = explicitScope(parent(node));
end explicitParent;
function classParent
input InstNode node;
output InstNode parent;
algorithm
CLASS_NODE(parentScope = parent) := node;
end classParent;
function derivedParent
input InstNode node;
output InstNode parent;
algorithm
parent := match node
case CLASS_NODE() then getDerivedNode(node.parentScope);
case COMPONENT_NODE() then getDerivedNode(node.parent);
case IMPLICIT_SCOPE() then getDerivedNode(node.parentScope);
else EMPTY_NODE();
end match;
end derivedParent;
function rootParent
input InstNode node;
output InstNode parent;
algorithm
parent := match node
case CLASS_NODE() then rootTypeParent(node.nodeType, node);
else parent(node);
end match;
end rootParent;
function rootTypeParent
input InstNodeType nodeType;
input InstNode node;
output InstNode parent;
algorithm
parent := match nodeType
case InstNodeType.ROOT_CLASS() guard not isEmpty(nodeType.parent) then nodeType.parent;
case InstNodeType.DERIVED_CLASS() then rootTypeParent(nodeType.ty, node);
else parent(node);
end match;
end rootTypeParent;
function parentScope
"Returns the parent scope of a node. In the case of a class this is simply
the enclosing class. In the case of a component it is the enclosing class of
the component's type."
input InstNode node;
output InstNode scope;
algorithm
scope := match node
case CLASS_NODE(nodeType = InstNodeType.DERIVED_CLASS())
then parentScope(Class.lastBaseClass(node));
case CLASS_NODE() then node.parentScope;
case COMPONENT_NODE() then parentScope(Component.classInstance(Pointer.access(node.component)));
case IMPLICIT_SCOPE() then node.parentScope;
end match;
end parentScope;
function classScope
input InstNode node;
output InstNode scope;
algorithm
scope := match node
case COMPONENT_NODE()
then Component.classInstance(Pointer.access(node.component));
else node;
end match;
end classScope;
function topScope
input InstNode node;
output InstNode topScope;
algorithm
topScope := match node
case CLASS_NODE(nodeType = InstNodeType.TOP_SCOPE()) then node;
else topScope(parentScope(node));
end match;
end topScope;
function topComponent
input InstNode node;
output InstNode topComponent;
algorithm
topComponent := match node
case COMPONENT_NODE(parent = EMPTY_NODE()) then node;
case COMPONENT_NODE() then topComponent(node.parent);
end match;
end topComponent;
function setParent
input InstNode parent;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE()
algorithm
node.parentScope := parent;
then
();
case COMPONENT_NODE()
algorithm
node.parent := parent;
then
();
case IMPLICIT_SCOPE()
algorithm
node.parentScope := parent;
then
();
end match;
end setParent;
function setOrphanParent
"Sets the parent of a node if the node lacks a parent, otherwise does nothing."
input InstNode parent;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE(parentScope = EMPTY_NODE())
algorithm
node.parentScope := parent;
then
();
case COMPONENT_NODE(parent = EMPTY_NODE())
algorithm
node.parent := parent;
then
();
else ();
end match;
end setOrphanParent;
function getClass
input InstNode node;
output Class cls;
algorithm
cls := match node
case CLASS_NODE() then Pointer.access(node.cls);
case COMPONENT_NODE()
then getClass(Component.classInstance(Pointer.access(node.component)));
end match;
end getClass;
function getDerivedClass
input InstNode node;
output Class cls;
algorithm
cls := match node
case CLASS_NODE() then getClass(getDerivedNode(node));
case COMPONENT_NODE()
then getClass(getDerivedNode(Component.classInstance(Pointer.access(node.component))));
end match;
end getDerivedClass;
function getDerivedNode
input InstNode node;
output InstNode derived;
algorithm
derived := match node
case CLASS_NODE(nodeType = InstNodeType.BASE_CLASS(parent = derived))
then getDerivedNode(derived);
else node;
end match;
end getDerivedNode;
function updateClass
input Class cls;
input output InstNode node;
algorithm
node := match node
case CLASS_NODE()
algorithm
Pointer.update(node.cls, cls);
then
node;
end match;
end updateClass;
function component
input InstNode node;
output Component component;
algorithm
component := match node
case COMPONENT_NODE() then Pointer.access(node.component);
end match;
end component;
function updateComponent
input Component component;
input output InstNode node;
algorithm
node := match node
case COMPONENT_NODE()
algorithm
Pointer.update(node.component, component);
then
node;
end match;
end updateComponent;
function replaceComponent
input Component component;
input output InstNode node;
algorithm
() := match node
case COMPONENT_NODE()
algorithm
node.component := Pointer.create(component);
then
();
end match;
end replaceComponent;
function replaceClass
input Class cls;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE()
algorithm
node.cls := Pointer.create(cls);
then
();
end match;
end replaceClass;
function nodeType
input InstNode node;
output InstNodeType nodeType;
algorithm
nodeType := match node
case CLASS_NODE() then node.nodeType;
case COMPONENT_NODE() then node.nodeType;
end match;
end nodeType;
function setNodeType
input InstNodeType nodeType;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE()
algorithm
node.nodeType := nodeType;
then
();
case COMPONENT_NODE()
algorithm
node.nodeType := nodeType;
then
();
else ();
end match;
end setNodeType;
function definition
input InstNode node;
output SCode.Element definition;
algorithm
definition := match node
case CLASS_NODE() then node.definition;
case COMPONENT_NODE() then Component.definition(Pointer.access(node.component));
end match;
end definition;
function setDefinition
input SCode.Element definition;
input output InstNode node;
algorithm
() := match node
case CLASS_NODE()
algorithm
node.definition := definition;
then
();
end match;
end setDefinition;
function info
input InstNode node;
output SourceInfo info;
algorithm
info := matchcontinue node
local
InstNodeType ty;
case CLASS_NODE(nodeType = ty as InstNodeType.BASE_CLASS())
then SCodeUtil.elementInfo(ty.definition);
case CLASS_NODE() then SCodeUtil.elementInfo(node.definition);
case COMPONENT_NODE() then Component.info(Pointer.access(node.component));
case COMPONENT_NODE() then info(node.parent);
else AbsynUtil.dummyInfo;
end matchcontinue;
end info;
function getType
input InstNode node;
output Type ty;
algorithm
ty := match node
case CLASS_NODE() then Class.getType(Pointer.access(node.cls), node);
case COMPONENT_NODE() then Component.getType(Pointer.access(node.component));
end match;
end getType;
function classApply<ArgT>
input output InstNode node;
input FuncType func;
input ArgT arg;
partial function FuncType
input ArgT arg;
input output Class cls;
end FuncType;
algorithm
() := match node
case CLASS_NODE()
algorithm
Pointer.update(node.cls, func(arg, Pointer.access(node.cls)));
then
();
end match;
end classApply;
function componentApply<ArgT>
input output InstNode node;
input FuncType func;
input ArgT arg;
partial function FuncType
input ArgT arg;
input output Component node;
end FuncType;
algorithm
() := match node
case COMPONENT_NODE()
algorithm
Pointer.update(node.component, func(arg, Pointer.access(node.component)));
then
();
end match;
end componentApply;
function scopeList
input InstNode node;
input Boolean includeRoot = false "Whether to include the root class name or not.";
input list<InstNode> accumScopes = {};
output list<InstNode> scopes;
algorithm
scopes := match node
local
InstNode parent;
case CLASS_NODE() then scopeListClass(node, node.nodeType, includeRoot, accumScopes);
case COMPONENT_NODE(parent = EMPTY_NODE()) then accumScopes;
case COMPONENT_NODE(nodeType = InstNodeType.REDECLARED_COMP(parent = parent))
then scopeList(parent, includeRoot, node :: accumScopes);
case COMPONENT_NODE() then scopeList(node.parent, includeRoot, node :: accumScopes);
case IMPLICIT_SCOPE() then scopeList(node.parentScope, includeRoot, accumScopes);
else accumScopes;
end match;
end scopeList;
function scopeListClass
input InstNode clsNode;
input InstNodeType ty;
input Boolean includeRoot;
input list<InstNode> accumScopes = {};
output list<InstNode> scopes;
algorithm
scopes := match ty
case InstNodeType.NORMAL_CLASS()
then scopeList(parent(clsNode), includeRoot, clsNode :: accumScopes);
case InstNodeType.BASE_CLASS()
then scopeList(ty.parent, includeRoot, accumScopes);
case InstNodeType.DERIVED_CLASS()
then scopeListClass(clsNode, ty.ty, includeRoot, accumScopes);
case InstNodeType.BUILTIN_CLASS()
then clsNode :: accumScopes;
case InstNodeType.TOP_SCOPE()
then accumScopes;
case InstNodeType.ROOT_CLASS()
then if includeRoot then
scopeList(parent(clsNode), includeRoot, clsNode :: accumScopes)
else
accumScopes;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown node type", sourceInfo());
then
fail();
end match;
end scopeListClass;
function scopePath
input InstNode node;
input Boolean includeRoot = false "Whether to include the root class name or not.";
output Absyn.Path path;
algorithm
path := match node
local
InstNodeType it;
case CLASS_NODE(nodeType = it)
then
match it
case InstNodeType.BASE_CLASS() then scopePath(it.parent, includeRoot);
else scopePath2(node.parentScope, includeRoot, Absyn.IDENT(node.name));
end match;
case COMPONENT_NODE() then scopePath2(node.parent, includeRoot, Absyn.IDENT(node.name));
case IMPLICIT_SCOPE() then scopePath(node.parentScope, includeRoot);
// For debugging.
else Absyn.IDENT(name(node));
end match;
end scopePath;
function scopePath2
input InstNode node;
input Boolean includeRoot;
input Absyn.Path accumPath;
output Absyn.Path path;
algorithm