-
Notifications
You must be signed in to change notification settings - Fork 297
/
NFLookup.mo
1224 lines (1104 loc) · 38.9 KB
/
NFLookup.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 NFLookup
" file: NFLookup.mo
package: NFLookup
description: Lookup functions for NFInst
"
import Absyn;
import AbsynUtil;
import Attributes = NFAttributes;
import SCode;
import Dump;
import ErrorTypes;
import Global;
import NFBuiltin;
import Inst = NFInst;
import Class = NFClass;
import NFInstNode.InstNode;
import NFLookupState.LookupState;
import Type = NFType;
import ComponentRef = NFComponentRef;
import InstContext = NFInstContext;
import InstNodeType = NFInstNode.InstNodeType;
protected
import NFInstNode.NodeTree;
import NFInstNode.CachedData;
import Component = NFComponent;
import Subscript = NFSubscript;
import ComplexType = NFComplexType;
import Error;
import ErrorExt;
import UnorderedMap;
import Modifier = NFModifier;
import BackendInterface;
import Settings;
import Testsuite;
import AbsynToSCode;
import NFClassTree.ClassTree;
import SCodeUtil;
import System;
public
type MatchType = enumeration(FOUND, NOT_FOUND, PARTIAL);
function lookupClassName
input Absyn.Path name;
input InstNode scope;
input InstContext.Type context;
input SourceInfo info;
input Boolean checkAccessViolations = true;
output InstNode node;
protected
LookupState state;
algorithm
(node, state) := lookupNameWithError(name, scope, context, info, Error.LOOKUP_ERROR, checkAccessViolations);
LookupState.assertClass(state, node, name, context, info);
end lookupClassName;
function lookupBaseClassName
input Absyn.Path name;
input InstNode scope;
input SourceInfo info;
output list<InstNode> nodes;
protected
LookupState state;
algorithm
try
(nodes, state) := lookupNames(name, scope, NFInstContext.NO_CONTEXT);
else
Error.addSourceMessage(Error.LOOKUP_BASECLASS_ERROR,
{AbsynUtil.pathString(name), InstNode.scopeName(scope)}, info);
fail();
end try;
LookupState.assertClass(state, listHead(nodes), name, NFInstContext.NO_CONTEXT, info);
end lookupBaseClassName;
function lookupComponent
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
input SourceInfo info;
output ComponentRef foundCref;
output InstNode foundScope "The scope the cref was found in.";
protected
LookupState state;
InstNode node;
algorithm
try
(foundCref, foundScope, state) := lookupCref(cref, scope, context);
node := ComponentRef.node(foundCref);
false := InstNode.isName(node);
else
Error.addSourceMessageAndFail(Error.LOOKUP_VARIABLE_ERROR,
{Dump.printComponentRefStr(cref), InstNode.scopeName(scope)}, info);
end try;
state := fixTypenameState(node, state);
LookupState.assertComponent(state, node, cref, context, info);
end lookupComponent;
function lookupConnector
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
input SourceInfo info;
output ComponentRef foundCref;
output InstNode foundScope "The scope the cref was found in.";
protected
LookupState state;
InstNode node;
algorithm
try
(foundCref, foundScope, state) := lookupCref(cref, scope, context);
else
Error.addSourceMessageAndFail(Error.LOOKUP_VARIABLE_ERROR,
{Dump.printComponentRefStr(cref), InstNode.scopeName(scope)}, info);
end try;
node := ComponentRef.node(foundCref);
state := fixTypenameState(node, state);
LookupState.assertComponent(state, node, cref, context, info);
end lookupConnector;
function fixTypenameState
input InstNode component;
input output LookupState state;
protected
Type ty;
algorithm
if InstNode.isClass(component) then
ty := InstNode.getType(Inst.expand(component));
state := match ty
case Type.ENUMERATION() then LookupState.COMP();
case Type.BOOLEAN() then LookupState.COMP();
else state;
end match;
end if;
end fixTypenameState;
function lookupLocalComponent
"Looks up a component in the local scope, without searching in any enclosing
scopes. The found scope is returned since it can be different from the given
scope in the case where the cref refers to an outer component."
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
input SourceInfo info;
output ComponentRef foundCref;
output InstNode foundScope "The scope the cref was found in.";
protected
LookupState state;
InstNode node;
algorithm
(foundCref, foundScope, state) := lookupLocalCref(cref, scope, context, info);
LookupState.assertComponent(state, ComponentRef.node(foundCref), cref, context, info);
end lookupLocalComponent;
function lookupFunctionName
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
input SourceInfo info;
output ComponentRef foundCref;
output InstNode foundScope;
protected
LookupState state;
InstNode node;
algorithm
try
(foundCref, foundScope, state) := lookupCref(cref, scope, context);
node := ComponentRef.node(foundCref);
false := InstNode.isName(node);
else
Error.addSourceMessageAndFail(Error.LOOKUP_FUNCTION_ERROR,
{Dump.printComponentRefStr(cref), InstNode.scopeName(scope)}, info);
end try;
(foundCref, state) := fixExternalObjectCall(node, foundCref, state);
LookupState.assertFunction(state, node, cref, context, info);
end lookupFunctionName;
function lookupFunctionNameSilent
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
output ComponentRef foundCref;
output InstNode foundScope;
protected
LookupState state;
InstNode node;
algorithm
(foundCref, foundScope, state) := lookupCref(cref, scope, context);
node := ComponentRef.node(foundCref);
(foundCref, state) := fixExternalObjectCall(node, foundCref, state);
true := LookupState.isFunction(state, node);
end lookupFunctionNameSilent;
function fixExternalObjectCall
"Changes calls to external objects so that the constructor is called instead,
i.e. a call such as
'ExtObj eo = ExtObj(...)'
is changed to
'ExtObj eo = ExtObj.constructor(...)'"
input InstNode node;
input output ComponentRef cref;
input output LookupState state;
protected
Class cls;
InstNode constructor;
algorithm
// If it's not a class it can't be an external object.
if not LookupState.isClass(state) then
return;
end if;
// External objects are identified by extending from ExternalObject, so the
// node needs to be expanded before we know whether it's an external object or
// not. Components are instantiated before their bindings, so in proper models
// we shouldn't get any non-expanded external objects here. But to avoid
// getting weird errors in erroneous models we make sure it's expanded anyway.
Inst.expand(node);
cls := InstNode.getClass(node);
() := match cls
case Class.PARTIAL_BUILTIN(ty = Type.COMPLEX(complexTy =
ComplexType.EXTERNAL_OBJECT(constructor = constructor)))
algorithm
cref := ComponentRef.prefixCref(constructor, Type.UNKNOWN(), {}, cref);
state := LookupState.FUNC();
then
();
else ();
end match;
end fixExternalObjectCall;
function lookupImport
input Absyn.Path name;
input InstNode scope;
input SourceInfo info;
output InstNode element;
protected
LookupState state;
algorithm
(element, state) := lookupNameWithError(name, InstNode.topScope(scope),
NFInstContext.NO_CONTEXT, info, Error.LOOKUP_IMPORT_ERROR);
LookupState.assertImport(state, element, name, info);
end lookupImport;
function lookupCrefWithError
input Absyn.ComponentRef cref;
input InstNode scope;
input InstContext.Type context;
input SourceInfo info;
input ErrorTypes.Message errMsg;
output ComponentRef foundCref;
output InstNode foundScope;
output LookupState state;
algorithm
try
(foundCref, foundScope, state) := lookupCref(cref, scope, context);
else
Error.addSourceMessage(errMsg,
{Dump.printComponentRefStr(cref), InstNode.scopeName(scope)}, info);
fail();
end try;
end lookupCrefWithError;
function lookupCref
"This function will look up an Absyn.ComponentRef in the given scope, and
construct a ComponentRef from the found nodes. The scope where the first part
of the cref was found will also be returned."
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
output ComponentRef foundCref;
output InstNode foundScope "The scope where the first part of the cref was found.";
output LookupState state;
protected
InstNode node;
Boolean in_enclosing;
algorithm
(foundCref, foundScope, state) := match cref
case Absyn.ComponentRef.CREF_IDENT()
algorithm
(_, foundCref, foundScope, in_enclosing, state) := lookupSimpleCref(cref.name, cref.subscripts, scope, context);
state := LookupState.checkCrefVariability(foundCref, in_enclosing, context, state);
then
(foundCref, foundScope, state);
case Absyn.ComponentRef.CREF_QUAL()
algorithm
(node, foundCref, foundScope, in_enclosing, state) := lookupSimpleCref(cref.name, cref.subscripts, scope, context);
(foundCref, foundScope, state) :=
lookupCrefInNode(cref.componentRef, node, foundCref, foundScope, state, context);
state := LookupState.checkCrefVariability(foundCref, in_enclosing, context, state);
then
(foundCref, foundScope, state);
case Absyn.ComponentRef.CREF_FULLYQUALIFIED()
then lookupCref(cref.componentRef, InstNode.topScope(scope), context);
case Absyn.ComponentRef.WILD()
then (ComponentRef.WILD(), scope, LookupState.PREDEF_COMP());
case Absyn.ComponentRef.ALLWILD()
then (ComponentRef.WILD(), scope, LookupState.PREDEF_COMP());
end match;
end lookupCref;
function lookupLocalCref
"Looks up a cref in the local scope without going into any enclosing scopes."
input Absyn.ComponentRef cref;
input InstNode scope "The scope to look in.";
input InstContext.Type context;
input SourceInfo info;
output ComponentRef foundCref;
output InstNode foundScope "The scope where the first part of the cref was found.";
output LookupState state;
protected
MatchType match_ty;
InstNode node;
algorithm
(foundCref, foundScope, state) := matchcontinue cref
local
InstNode found_scope;
case Absyn.ComponentRef.CREF_IDENT()
algorithm
(node, foundScope) := lookupLocalSimpleCref(cref.name, scope);
state := LookupState.nodeState(node);
then
(ComponentRef.fromAbsyn(node, cref.subscripts), foundScope, state);
case Absyn.ComponentRef.CREF_QUAL()
algorithm
(node, foundScope) := lookupLocalSimpleCref(cref.name, scope);
state := LookupState.nodeState(node);
foundCref := ComponentRef.fromAbsyn(node, cref.subscripts);
(foundCref, foundScope, state) :=
lookupCrefInNode(cref.componentRef, node, foundCref, foundScope, state, context);
then
(foundCref, foundScope, state);
else
algorithm
Error.addSourceMessage(Error.LOOKUP_VARIABLE_ERROR,
{Dump.printComponentRefStr(cref), InstNode.scopeName(scope)}, info);
then
fail();
end matchcontinue;
end lookupLocalCref;
function lookupInner
"Looks up the corresponding inner node given an outer node."
input InstNode outerNode;
input InstNode scope;
output InstNode innerNode;
protected
String name = InstNode.name(outerNode);
InstNode cur_scope = scope;
InstNode prev_scope = scope;
algorithm
while not InstNode.isEmpty(cur_scope) loop
try
// Check if we have an element with the same name as the outer node in this scope.
innerNode := InstNode.resolveOuter(Class.lookupElement(name, InstNode.getClass(cur_scope)));
true := InstNode.isInner(innerNode);
return;
else
if InstNode.isRootClass(cur_scope) then
// Stop looking if we reach the root class.
prev_scope := InstNode.topScope(cur_scope);
cur_scope := InstNode.EMPTY_NODE();
else
// Otherwise continue looking in the instance parent's scope.
prev_scope := cur_scope;
cur_scope := InstNode.instanceParent(cur_scope);
end if;
end try;
end while;
// No inner found, try to generate one.
innerNode := generateInner(outerNode, InstNode.topScope(prev_scope));
end lookupInner;
function lookupLocalSimpleName
"Looks up a name in the given scope, without continuing the search in any
enclosing scopes if the name isn't found."
input String name;
input InstNode scope;
output InstNode node;
output Boolean isImport;
algorithm
(node, isImport) := Class.lookupElement(name, InstNode.getClass(scope));
node := InstNode.resolveInner(node);
end lookupLocalSimpleName;
function lookupSimpleName
input String name;
input InstNode scope;
input InstContext.Type context;
output InstNode node;
protected
InstNode cur_scope = scope;
Boolean require_builtin = false;
Boolean loaded = false;
algorithm
if InstContext.inAnnotation(context) then
// If in an annotation context, check the annotation environment first.
try
node := lookupLocalSimpleName(name, InstNode.annotationScope(scope));
return;
else
end try;
end if;
// Look for the name in each enclosing scope, until it's either found or we
// run out of scopes.
for i in 1:Global.recursionDepthLimit loop
try
node := lookupLocalSimpleName(name, cur_scope);
if require_builtin then
true := InstNode.isBuiltin(node);
end if;
return;
else
// If the scope is encapsulated, continue looking among the builtin
// classes in the top scope.
if InstNode.isEncapsulated(cur_scope) then
// Do parentScope first to avoid an infinite loop if we're already in the top scope.
cur_scope := InstNode.topScope(InstNode.parentScope(cur_scope));
require_builtin := true;
elseif name == InstNode.name(cur_scope) and InstNode.isClass(cur_scope) then
// If the scope has the same name as we're looking for we can just return it.
node := cur_scope;
return;
else
if InstNode.isTopScope(cur_scope) and not loaded and not require_builtin then
// If the name couldn't be found in any scope, try to load a library
// with that name and then try to look it up in the top scope again.
loaded := true;
loadLibrary(name, cur_scope);
else
// Otherwise, continue in the enclosing scope.
cur_scope := InstNode.parentScope(cur_scope);
end if;
end if;
end try;
end for;
Error.addMessage(Error.RECURSION_DEPTH_REACHED,
{String(Global.recursionDepthLimit), InstNode.name(scope)});
fail();
end lookupSimpleName;
function lookupSimpleNameRootPath
"Returns the qualified path needed to find the given name in the given scope
that's either a root class or a class somewhere inside a root class. If the
name is found outside the root class it's fully qualified, otherwise it's
returned as it is."
input String name;
input InstNode scope;
input InstContext.Type context;
output Absyn.Path path;
protected
InstNode node, cur_scope = scope;
Boolean in_root_class = true;
algorithm
if InstContext.inAnnotation(context) then
try
_ := lookupLocalSimpleName(name, InstNode.annotationScope(scope));
// Name refers to builtin annotation that shouldn't be qualified.
path := Absyn.Path.IDENT(name);
return;
else
end try;
end if;
for i in 1:Global.recursionDepthLimit loop
try
node := Class.lookupElement(name, InstNode.getClass(cur_scope));
if in_root_class then
// If we're still inside the root class, then the name doesn't need to be qualified.
path := Absyn.Path.IDENT(name);
else
// Otherwise, fully qualify the path.
path := AbsynUtil.makeFullyQualified(InstNode.fullPath(node));
end if;
return;
else
if InstNode.isEncapsulated(cur_scope) then
// If the scope is encapsulated then the name can only refer to builtin
// classes, which are already fully qualified.
path := Absyn.Path.IDENT(name);
return;
elseif name == InstNode.name(cur_scope) and InstNode.isClass(cur_scope) then
// The current scope is the class we're looking for.
path := if in_root_class then Absyn.Path.IDENT(name) else
AbsynUtil.makeFullyQualified(InstNode.fullPath(cur_scope));
return;
elseif InstNode.isTopScope(cur_scope) then
// If we reach the top scope then the name is either already fully
// qualified or couldn't be found, in either case return name as it is.
path := Absyn.Path.IDENT(name);
return;
else
if in_root_class and InstNode.isRootClass(cur_scope) then
// The scope was the root class, now we're start to look in the
// enclosing scopes of the root class.
in_root_class := false;
end if;
// Continue in the enclosing scope.
cur_scope := InstNode.parentScope(cur_scope);
end if;
end try;
end for;
fail();
end lookupSimpleNameRootPath;
function lookupNameWithError
input Absyn.Path name;
input InstNode scope;
input InstContext.Type context;
input SourceInfo info;
input ErrorTypes.Message errorType;
input Boolean checkAccessViolations = true;
output InstNode node;
output LookupState state;
algorithm
try
(node, state) := lookupName(name, scope, context, checkAccessViolations);
else
Error.addSourceMessage(errorType, {AbsynUtil.pathString(name), InstNode.scopeName(scope)}, info);
fail();
end try;
end lookupNameWithError;
function lookupName
input Absyn.Path name;
input InstNode scope;
input InstContext.Type context;
input Boolean checkAccessViolations;
output InstNode node;
output LookupState state;
algorithm
(node, state) := match name
// Simple name, look it up in the given scope.
case Absyn.Path.IDENT()
then lookupFirstIdent(name.name, scope, context);
// Qualified name, look up first part in the given scope and look up the
// rest of the name in the found element.
case Absyn.Path.QUALIFIED()
algorithm
(node, state) := lookupFirstIdent(name.name, scope, context);
then
lookupLocalName(name.path, node, state, context, checkAccessViolations, isSelfReference(node, scope));
// Fully qualified path, start from top scope.
case Absyn.Path.FULLYQUALIFIED()
then lookupName(name.path, InstNode.topScope(scope), context, checkAccessViolations);
end match;
end lookupName;
function isSelfReference
input InstNode node;
input InstNode scope;
output Boolean res;
protected
InstNode parent = scope;
algorithm
while not InstNode.isEmpty(parent) loop
if InstNode.refEqual(node, parent) then
res := true;
return;
end if;
parent := InstNode.instanceParent(parent);
end while;
res := false;
end isSelfReference;
function lookupNames
input Absyn.Path name;
input InstNode scope;
input InstContext.Type context;
output list<InstNode> nodes;
output LookupState state;
algorithm
(nodes, state) := match name
local
InstNode node;
// Simple name, look it up in the given scope.
case Absyn.Path.IDENT()
algorithm
(node, state) := lookupFirstIdent(name.name, scope, context);
then
({node}, state);
// Qualified name, look up first part in the given scope and look up the
// rest of the name in the found element.
case Absyn.Path.QUALIFIED()
algorithm
(node, state) := lookupFirstIdent(name.name, scope, context);
then
lookupLocalNames(name.path, node, {node}, state, context, isSelfReference(node, scope));
// Fully qualified path, start from top scope.
case Absyn.Path.FULLYQUALIFIED()
then lookupNames(name.path, InstNode.topScope(scope), context);
end match;
end lookupNames;
function lookupFirstIdent
"Looks up the first part of a name."
input String name;
input InstNode scope;
input InstContext.Type context;
output InstNode node;
output LookupState state;
algorithm
try
// Check if the name refers to a reserved builtin name.
node := lookupSimpleBuiltinName(name);
state := LookupState.PREDEF_CLASS();
else
// Otherwise, check each scope until the name is found.
node := lookupSimpleName(name, scope, context);
state := LookupState.nodeState(node);
end try;
end lookupFirstIdent;
function lookupLocalName
"Looks up a path in the given scope, without continuing the search in any
enclosing scopes if the path isn't found."
input Absyn.Path name;
input output InstNode node;
input output LookupState state;
input InstContext.Type context;
input Boolean checkAccessViolations = true;
input Boolean selfReference = false;
protected
Boolean is_import;
algorithm
// Looking something up in a component is only legal when the name begins with
// a component reference, and for that we use lookupCref. So if the given node
// is a component we can immediately quit and give an error.
if not InstNode.isClass(node) then
state := LookupState.COMP_CLASS();
return;
end if;
if not selfReference then
node := Inst.instPackage(node, context);
// allow lookup in partial nodes if -d=nfAPI is on
if InstNode.isPartial(node) and not InstContext.inRelaxed(context) then
state := LookupState.ERROR(LookupState.PARTIAL_CLASS());
return;
end if;
end if;
// Look up the path in the scope.
() := match name
case Absyn.Path.IDENT()
algorithm
(node, is_import) := lookupLocalSimpleName(name.name, node);
if is_import then
state := LookupState.ERROR(LookupState.IMPORT());
else
state := LookupState.next(node, state, checkAccessViolations);
end if;
then
();
case Absyn.Path.QUALIFIED()
algorithm
(node, is_import) := lookupLocalSimpleName(name.name, node);
if is_import then
state := LookupState.ERROR(LookupState.IMPORT());
else
state := LookupState.next(node, state, checkAccessViolations);
(node, state) := lookupLocalName(name.path, node, state, context, checkAccessViolations);
end if;
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " was called with an invalid path.", sourceInfo());
then
fail();
end match;
end lookupLocalName;
function lookupLocalNames
"Looks up a path in the given scope, without continuing the search in any
enclosing scopes if the path isn't found."
input Absyn.Path name;
input InstNode scope;
input output list<InstNode> nodes;
input output LookupState state;
input InstContext.Type context;
input Boolean selfReference = false;
protected
InstNode node = scope;
algorithm
// Looking something up in a component is only legal when the name begins with
// a component reference, and for that we use lookupCref. So if the given node
// is a component we can immediately quit and give an error.
if not InstNode.isClass(scope) then
state := LookupState.COMP_CLASS();
return;
end if;
// If the given node extends from itself, like 'extends Modelica.Icons.***' in
// the MSL, then it's already being instantiated here.
if not selfReference then
node := Inst.instPackage(node, context);
// PartialModelicaServices is mistakenly partial in MSL versions older than
// 3.2.3. We can't just check for Modelica 3.2 since that will break e.g. 3.2.2,
// so just disable the check specifically for PartialModelicaServices instead.
if InstNode.isPartial(node) and not InstContext.inRelaxed(context) and
not InstNode.name(node) == "PartialModelicaServices" then
state := LookupState.ERROR(LookupState.PARTIAL_CLASS());
return;
end if;
end if;
// Look up the path in the scope.
(nodes, state) := match name
case Absyn.Path.IDENT()
algorithm
node := lookupLocalSimpleName(name.name, node);
state := LookupState.next(node, state);
then
(node :: nodes, state);
case Absyn.Path.QUALIFIED()
algorithm
node := lookupLocalSimpleName(name.name, node);
state := LookupState.next(node, state);
then
lookupLocalNames(name.path, node, node :: nodes, state, context);
else
algorithm
Error.assertion(false, getInstanceName() + " was called with an invalid path.", sourceInfo());
then
fail();
end match;
end lookupLocalNames;
function lookupSimpleBuiltinName
input String name;
output InstNode builtin;
algorithm
builtin := match name
case "Real" then NFBuiltin.REAL_NODE;
case "Integer" then NFBuiltin.INTEGER_NODE;
case "Boolean" then NFBuiltin.BOOLEAN_NODE;
case "String" then NFBuiltin.STRING_NODE;
end match;
end lookupSimpleBuiltinName;
function lookupSimpleBuiltinCref
input String name;
input list<Absyn.Subscript> subs;
output InstNode node;
output ComponentRef cref;
output LookupState state;
algorithm
(node, cref, state) := match name
case "time"
then (NFBuiltin.TIME, NFBuiltin.TIME_CREF, LookupState.PREDEF_COMP());
case "Boolean"
then (NFBuiltin.BOOLEAN_NODE, NFBuiltin.BOOLEAN_CREF, LookupState.PREDEF_CLASS());
case "Integer"
then (NFBuiltinFuncs.INTEGER_NODE, NFBuiltinFuncs.INTEGER_CREF, LookupState.FUNC());
case "String"
then (NFBuiltinFuncs.STRING_NODE, NFBuiltinFuncs.STRING_CREF, LookupState.FUNC());
end match;
if not listEmpty(subs) then
cref := ComponentRef.setSubscripts(list(Subscript.RAW_SUBSCRIPT(s) for s in subs), cref);
end if;
end lookupSimpleBuiltinCref;
function lookupSimpleCref
"This function look up a simple name as a cref in a given component."
input String name;
input list<Absyn.Subscript> subs;
input InstNode scope;
input InstContext.Type context;
output InstNode node;
output ComponentRef cref;
output InstNode foundScope = scope;
output Boolean inEnclosingScope = false;
output LookupState state;
protected
Boolean require_builtin = false;
Boolean loaded = false;
Boolean is_enclosing = false;
algorithm
try
(node, cref, state) := lookupSimpleBuiltinCref(name, subs);
foundScope := InstNode.topScope(foundScope);
else
if InstContext.inAnnotation(context) then
// If in an annotation context, check the annotation environment first.
try
(node, foundScope) := lookupLocalSimpleCref(name, InstNode.annotationScope(foundScope));
state := LookupState.nodeState(node);
cref := ComponentRef.fromAbsyn(node, subs);
return;
else
end try;
end if;
// Look for the name in the given scope, and if not found there continue
// through the enclosing scopes of that scope until we either run out of
// scopes or for some reason exceed the recursion depth limit.
for i in 1:Global.recursionDepthLimit loop
try
(node, foundScope) := lookupLocalSimpleCref(name, foundScope);
if require_builtin then
true := InstNode.isBuiltin(node);
end if;
// We found a node, return it.
state := LookupState.nodeState(node);
cref := ComponentRef.fromAbsyn(node, subs);
return;
else
// Stop if the current scope is encapsulated.
if InstNode.isEncapsulated(foundScope) then
foundScope := InstNode.topScope(InstNode.parentScope(foundScope));
require_builtin := true;
else
if InstNode.isTopScope(foundScope) and not loaded and not require_builtin then
// If the name couldn't be found in any scope, try to load a library
// with that name and then try to look it up in the top scope again.
loaded := true;
loadLibrary(name, foundScope);
else
// Look in the next enclosing scope.
inEnclosingScope := not InstNode.isImplicit(foundScope);
foundScope := InstNode.parentScope(foundScope);
end if;
end if;
end try;
end for;
Error.addMessage(Error.RECURSION_DEPTH_REACHED,
{String(Global.recursionDepthLimit), InstNode.scopeName(foundScope)});
fail();
end try;
end lookupSimpleCref;
function lookupLocalSimpleCref
"This function look up a simple name as a cref in a given component, without
searching in any enclosing scope."
input String name;
input InstNode scope;
output InstNode node;
output InstNode foundScope = scope;
protected
Boolean is_import;
algorithm
(node, is_import) := match foundScope
case InstNode.IMPLICIT_SCOPE()
then (lookupIterator(name, foundScope.locals), false);
case InstNode.CLASS_NODE()
then Class.lookupElement(name, InstNode.getClass(foundScope));
case InstNode.COMPONENT_NODE()
then Class.lookupElement(name, InstNode.getClass(foundScope));
case InstNode.INNER_OUTER_NODE()
then Class.lookupElement(name, InstNode.getClass(foundScope.innerNode));
end match;
if is_import then
foundScope := InstNode.parent(node);
elseif InstNode.isInnerOuterNode(node) then
// If the node is an outer node, return the inner instead.
node := InstNode.resolveInner(node);
foundScope := InstNode.parent(node);
end if;
end lookupLocalSimpleCref;
function lookupIterator
input String name;
input list<InstNode> iterators;
output InstNode iterator;
algorithm
for i in iterators loop
if name == InstNode.name(i) then
iterator := i;
return;
end if;
end for;
fail();
end lookupIterator;
function lookupCrefInNode
input Absyn.ComponentRef cref;
input InstNode node;
input output ComponentRef foundCref;
input output InstNode foundScope;
input output LookupState state;
input InstContext.Type context;
protected
InstNode scope;
InstNode n, cls_node;
String name;
Class cls;
Boolean is_import, scope_is_class;
algorithm
if LookupState.isError(state) then
return;
end if;
scope := node;
scope_is_class := InstNode.isClass(scope);
if scope_is_class then
scope := Inst.instPackage(node, context);
if InstNode.isPartial(scope) and not InstContext.inRelaxed(context) then
state := LookupState.ERROR(LookupState.PARTIAL_CLASS());
return;
end if;
elseif InstNode.isGeneratedInner(scope) and Component.isDefinition(InstNode.component(scope)) then
// The scope is a generated inner component that hasn't been instantiated,
// it needs to be instantiated to continue lookup.
Inst.instComponent(scope, NFAttributes.DEFAULT_ATTR, Modifier.NOMOD(), true, 0, NFInstContext.CLASS);
end if;
name := AbsynUtil.crefFirstIdent(cref);
cls_node := InstNode.classScope(scope);
if InstNode.isEmpty(cls_node) then
foundCref := ComponentRef.fromAbsynCref(cref, foundCref);
return;
end if;
cls := InstNode.getClass(cls_node);
try
(n, is_import) := Class.lookupElement(name, cls);
else
true := InstNode.isComponent(node);