/
SCodeLookup.mo
1373 lines (1228 loc) · 44.3 KB
/
SCodeLookup.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 SCodeLookup
" file: SCodeLookup.mo
package: SCodeLookup
description: SCode flattening
RCS: $Id$
This module flattens the SCode representation by removing all extends, imports
and redeclares, and fully qualifying class names.
"
public import Absyn;
public import Error;
public import SCode;
public import SCodeEnv;
public type Env = SCodeEnv.Env;
public type Item = SCodeEnv.Item;
public type Extends = SCodeEnv.Extends;
public type Frame = SCodeEnv.Frame;
public type FrameType = SCodeEnv.FrameType;
public type AvlTree = SCodeEnv.AvlTree;
public type Import = Absyn.Import;
public uniontype RedeclareReplaceStrategy
record INSERT_REDECLARES end INSERT_REDECLARES;
record IGNORE_REDECLARES end IGNORE_REDECLARES;
end RedeclareReplaceStrategy;
public constant Item BUILTIN_REAL = SCodeEnv.CLASS(
SCode.CLASS("Real", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
public constant Item BUILTIN_INTEGER = SCodeEnv.CLASS(
SCode.CLASS("Integer", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
public constant Item BUILTIN_BOOLEAN = SCodeEnv.CLASS(
SCode.CLASS("Boolean", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
public constant Item BUILTIN_STRING = SCodeEnv.CLASS(
SCode.CLASS("String", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
public constant Item BUILTIN_STATESELECT = SCodeEnv.CLASS(
SCode.CLASS("StateSelect", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_CLASS(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
public constant Item BUILTIN_EXTERNALOBJECT = SCodeEnv.CLASS(
SCode.CLASS("ExternalObject", SCode.defaultPrefixes,
SCode.NOT_ENCAPSULATED(), SCode.PARTIAL(), SCode.R_CLASS(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()),
Absyn.dummyInfo), SCodeEnv.emptyEnv, SCodeEnv.BUILTIN());
protected import Debug;
protected import RTOpts;
protected import SCodeCheck;
protected import SCodeFlattenImports;
protected import SCodeFlattenRedeclare;
protected import Dump;
public function lookupSimpleName
"Looks up a simple identifier in the environment and returns the environment
item, the path, and the enclosing scope of the name."
input Absyn.Ident inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
algorithm
(SOME(outItem), SOME(outPath), SOME(outEnv)) :=
lookupSimpleName2(inName, inEnv, {});
end lookupSimpleName;
public function lookupSimpleName2
"Helper function to lookupSimpleName. Looks up a simple identifier in the
environment."
input Absyn.Ident inName;
input Env inEnv;
input list<String> inVisitedScopes;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inEnv, inVisitedScopes)
local
FrameType frame_type;
Env rest_env;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
Option<Env> opt_env;
String scope_name;
// Check the local scope.
case (_, _, _)
equation
(opt_item, opt_path, opt_env) =
lookupInLocalScope(inName, inEnv, inVisitedScopes);
then
(opt_item, opt_path, opt_env);
// If not found in the local scope, check the next frame unless the current
// frame is encapsulated.
case (_, SCodeEnv.FRAME(name = SOME(scope_name), frameType = frame_type) ::
rest_env, _)
equation
frameNotEncapsulated(frame_type);
(opt_item, opt_path, opt_env) =
lookupSimpleName2(inName, rest_env, scope_name :: inVisitedScopes);
then
(opt_item, opt_path, opt_env);
// If the current frame is encapsulated, check for builtin types and
// functions in the top scope.
case (_, SCodeEnv.FRAME(frameType = SCodeEnv.ENCAPSULATED_SCOPE()) ::
rest_env, _)
equation
rest_env = SCodeEnv.getEnvTopScope(rest_env);
(opt_item, opt_path, opt_env) = lookupSimpleName2(inName, rest_env, {});
checkBuiltinItem(opt_item);
then
(opt_item, opt_path, opt_env);
end matchcontinue;
end lookupSimpleName2;
public function frameNotEncapsulated
"Fails if the frame type is encapsulated, otherwise succeeds."
input FrameType frameType;
algorithm
_ := match(frameType)
case SCodeEnv.ENCAPSULATED_SCOPE() then fail();
else then ();
end match;
end frameNotEncapsulated;
protected function checkBuiltinItem
input Option<Item> inItem;
algorithm
_ := match(inItem)
local
String name;
case (SOME(SCodeEnv.CLASS(classType = SCodeEnv.BUILTIN()))) then ();
case (NONE()) then ();
end match;
end checkBuiltinItem;
public function lookupInLocalScope
"Looks up a simple identifier in the environment. Returns SOME(item) if an
item is found, NONE() if a partial match was found (for example when the name
matches the import name of an import, but the imported class couldn't be
found), or fails if no match is found."
input Absyn.Ident inName;
input Env inEnv;
input list<String> inVisitedScopes;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inEnv, inVisitedScopes)
local
AvlTree cls_and_vars;
Env rest_env, env;
Item item;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
list<Import> imps;
FrameType frame_type;
Absyn.Path path;
Option<Env> opt_env;
// Look among the locally declared components.
case (_, SCodeEnv.FRAME(clsAndVars = cls_and_vars) :: _, _)
equation
item = SCodeEnv.avlTreeGet(cls_and_vars, inName);
item = SCodeEnv.resolveAlias(item, cls_and_vars);
then
(SOME(item), SOME(Absyn.IDENT(inName)), SOME(inEnv));
// Look among the inherited components.
case (_, _, _)
equation
(opt_item, opt_path, _, opt_env) =
lookupInBaseClasses(inName, inEnv, INSERT_REDECLARES(), inVisitedScopes);
then
(opt_item, opt_path, opt_env);
// Look among the qualified imports.
case (_, SCodeEnv.FRAME(importTable =
SCodeEnv.IMPORT_TABLE(hidden = false, qualifiedImports = imps)) :: _, _)
equation
(opt_item, opt_path, opt_env) =
lookupInQualifiedImports(inName, imps, inEnv);
then
(opt_item, opt_path, opt_env);
// Look among the unqualified imports.
case (_, SCodeEnv.FRAME(importTable =
SCodeEnv.IMPORT_TABLE(hidden = false, unqualifiedImports = imps)) :: _, _)
equation
(item, path, env) =
lookupInUnqualifiedImports(inName, imps, inEnv);
then
(SOME(item), SOME(path), SOME(env));
// Look in the next scope only if the current scope is an implicit scope
// (for example a for or match/matchcontinue scope).
case (_, SCodeEnv.FRAME(frameType = SCodeEnv.IMPLICIT_SCOPE()) :: rest_env, _)
equation
(opt_item, opt_path, opt_env) =
lookupInLocalScope(inName, rest_env, inVisitedScopes);
then
(opt_item, opt_path, opt_env);
end matchcontinue;
end lookupInLocalScope;
public function lookupInBaseClasses
"Looks up an identifier by following the extends clauses in a scope."
input Absyn.Ident inName;
input Env inEnv;
input RedeclareReplaceStrategy inReplaceRedeclares;
input list<String> inVisitedScopes;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Absyn.Path outBaseClass;
output Option<Env> outEnv;
protected
Env env;
list<Extends> bcl;
algorithm
SCodeEnv.FRAME(extendsTable =
SCodeEnv.EXTENDS_TABLE(baseClasses = bcl as _ :: _)) :: _ := inEnv;
// We need to remove the extends from the current scope, because the names of
// extended classes should not be found by lookup through the extends-clauses
// (Modelica Specification 3.2, section 5.6.1.).
env := SCodeEnv.removeExtendsFromLocalScope(inEnv);
env := SCodeEnv.setImportTableHidden(env, false);
(outItem, outPath, outBaseClass, outEnv) :=
lookupInBaseClasses2(inName, bcl, env, inReplaceRedeclares, inVisitedScopes);
end lookupInBaseClasses;
public function lookupInBaseClasses2
"Helper function to lookupInBaseClasses. Looks up an identifier through the
extends clauses in a scope."
input Absyn.Ident inName;
input list<Extends> inBaseClasses;
input Env inEnv;
input RedeclareReplaceStrategy inReplaceRedeclares;
input list<String> inVisitedScopes;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Absyn.Path outBaseClass;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outBaseClass, outEnv) :=
matchcontinue(inName, inBaseClasses, inEnv, inReplaceRedeclares, inVisitedScopes)
local
Absyn.Path bc, path;
list<Extends> rest_bc;
Item item;
Env env;
list<SCodeEnv.Redeclaration> redecls;
Absyn.Info info;
Option<Absyn.Path> opt_path;
Option<Item> opt_item;
Option<Env> opt_env;
// Look in the first base class.
case (_, SCodeEnv.EXTENDS(baseClass = bc, redeclareModifiers = redecls,
info = info) :: _, _, _, _)
equation
// Find the base class.
(item, path, env) = lookupBaseClassName(bc, inEnv, info);
true = checkVisitedScopes(inVisitedScopes, inEnv, path);
// Hide the imports to make sure that we don't find the name via them
// (imports are not inherited).
item = SCodeEnv.setImportsInItemHidden(item, true);
// Look in the base class.
(opt_item, opt_env) = SCodeFlattenRedeclare.replaceRedeclares(redecls,
bc, item, env, inEnv, inReplaceRedeclares);
(opt_item, opt_path, opt_env) =
lookupInBaseClasses3(Absyn.IDENT(inName), opt_item, opt_env);
then
(opt_item, opt_path, bc, opt_env);
// No match, check the rest of the base classes.
case (_, _ :: rest_bc, _, _, _)
equation
(opt_item, opt_path, bc, opt_env) =
lookupInBaseClasses2(inName, rest_bc, inEnv, inReplaceRedeclares, inVisitedScopes);
then
(opt_item, opt_path, bc, opt_env);
end matchcontinue;
end lookupInBaseClasses2;
protected function checkVisitedScopes
"Checks if we are trying to look up a base class that we are coming from when
going up in the environment, to avoid infinite loops."
input list<String> inVisitedScopes;
input Env inEnv;
input Absyn.Path inBaseClass;
output Boolean outRes;
algorithm
outRes := matchcontinue(inVisitedScopes, inEnv, inBaseClass)
local
Absyn.Path env_path, visited_path, bc_path;
case ({}, _, _) then true;
case (_, _, _)
equation
env_path = SCodeEnv.getEnvPath(inEnv);
bc_path = Absyn.removePrefix(env_path, inBaseClass);
visited_path = Absyn.stringListPath(inVisitedScopes);
true = Absyn.pathPrefixOf(visited_path, bc_path);
then
false;
else true;
end matchcontinue;
end checkVisitedScopes;
protected function lookupInBaseClasses3
input Absyn.Path inName;
input Option<Item> inItem;
input Option<Env> inEnv;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := match(inName, inItem, inEnv)
local
Item item;
Absyn.Path path;
Env env;
case (_, NONE(), NONE()) then (NONE(), NONE(), NONE());
case (_, SOME(item), SOME(env))
equation
(item, path, env) = lookupNameInItem(inName, item, env);
then
(SOME(item), SOME(path), SOME(env));
end match;
end lookupInBaseClasses3;
public function lookupInQualifiedImports
"Looks up a name through the qualified imports in a scope. If it finds the
name it returns the item, path, and environment for the name. It can also find
a partial match, in which case it returns NONE() to signal that the lookup
shouldn't look further. This can happen if the have an 'import A.B' and an
element 'B.C', but C is not in A.B. Finally it can also fail to find anything,
in which case it simply fails as normal."
input Absyn.Ident inName;
input list<Import> inImports;
input Env inEnv;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inImports, inEnv)
local
Absyn.Ident name;
Absyn.Path path;
Item item;
list<Import> rest_imps;
Import imp;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
Option<Env> opt_env;
Env env;
// No match, search the rest of the list of imports.
case (_, Absyn.NAMED_IMPORT(name = name) :: rest_imps, _)
equation
false = stringEqual(inName, name);
(opt_item, opt_path, opt_env) =
lookupInQualifiedImports(inName, rest_imps, inEnv);
then
(opt_item, opt_path, opt_env);
// Match, look up the fully qualified import path.
case (_, Absyn.NAMED_IMPORT(name = name, path = path) :: _, _)
equation
true = stringEqual(inName, name);
(item, path, env) = lookupFullyQualified(path, inEnv);
then
(SOME(item), SOME(path), SOME(env));
// Partial match, return NONE(). This is when only part of the import path
// can be found, in which case we should stop looking further.
case (_, Absyn.NAMED_IMPORT(name = name, path = path) :: _, _)
equation
true = stringEqual(inName, name);
then
(NONE(), NONE(), NONE());
end matchcontinue;
end lookupInQualifiedImports;
public function lookupInUnqualifiedImports
"Looks up a name through the qualified imports in a scope. If it finds the
name it returns the item, path, and environment for the name, otherwise it
fails."
input Absyn.Ident inName;
input list<Import> inImports;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inImports, inEnv)
local
Item item;
Absyn.Path path, path2;
list<Import> rest_imps;
Env env;
// For each unqualified import we have to look up the package the import
// points to, and then look among the public member of the package for the
// name we are looking for.
case (_, Absyn.UNQUAL_IMPORT(path = path) :: _, _)
equation
// Look up the import path.
(item, path, env) = lookupFullyQualified(path, inEnv);
// Look up the name among the public member of the found package.
(item, path2, env) = lookupNameInItem(Absyn.IDENT(inName), item, env);
// Combine the paths for the name and the package it was found in.
path = SCodeEnv.joinPaths(path, path2);
then
(item, path, env);
// No match, continue with the rest of the imports.
case (_, _ :: rest_imps, _)
equation
(item, path, env) =
lookupInUnqualifiedImports(inName, rest_imps, inEnv);
then
(item, path, env);
end matchcontinue;
end lookupInUnqualifiedImports;
public function lookupFullyQualified
"Looks up a fully qualified path in the environment, returning the
environment item, path and environment of the name if found."
input Absyn.Path inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
protected
Env env;
algorithm
env := SCodeEnv.getEnvTopScope(inEnv);
(outItem, outPath, outEnv) := lookupNameInPackage(inName, env);
outPath := Absyn.makeFullyQualified(outPath);
end lookupFullyQualified;
public function lookupNameInPackage
"Looks up a name inside the environment of a package, returning the
environment item, path and environment of the name if found."
input Absyn.Path inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
algorithm
(outItem, outPath, outEnv) := match(inName, inEnv)
local
Absyn.Ident name;
Absyn.Path path, new_path;
AvlTree cls_and_vars;
Frame top_scope;
Env rest_env, env;
Item item;
// Simple name, look in the local scope.
case (Absyn.IDENT(name = name), _)
equation
(SOME(item), SOME(path), SOME(env)) = lookupInLocalScope(name, inEnv, {});
env = SCodeEnv.setImportTableHidden(env, false);
then
(item, path, env);
// Qualified name.
case (Absyn.QUALIFIED(name = name, path = path), top_scope :: _)
equation
// Look up the name in the local scope.
(SOME(item), SOME(new_path), SOME(env)) =
lookupInLocalScope(name, inEnv, {});
env = SCodeEnv.setImportTableHidden(env, false);
// Look for the rest of the path in the found item.
(item, path, env) = lookupNameInItem(path, item, env);
path = SCodeEnv.joinPaths(new_path, path);
then
(item, path, env);
end match;
end lookupNameInPackage;
public function lookupCrefInPackage
"Looks up a component reference inside the environment of a package, returning
the environment item, path and environment of the reference if found."
input Absyn.ComponentRef inCref;
input Env inEnv;
output Item outItem;
output Absyn.ComponentRef outCref;
algorithm
(outItem, outCref) := match(inCref, inEnv)
local
Absyn.Ident name;
Absyn.Path new_path;
list<Absyn.Subscript> subs;
Absyn.ComponentRef cref, cref_rest;
Item item;
Frame top_scope;
Env env;
// Simple identifier, look in the local scope.
case (Absyn.CREF_IDENT(name = name, subscripts = subs), _)
equation
(SOME(item), SOME(new_path), _) = lookupInLocalScope(name, inEnv, {});
cref = Absyn.pathToCrefWithSubs(new_path, subs);
then
(item, cref);
// Qualified identifier.
case (Absyn.CREF_QUAL(name = name, subscripts = subs,
componentRef = cref_rest), _)
equation
// Look in the local scope.
(SOME(item), SOME(new_path), SOME(env)) =
lookupInLocalScope(name, inEnv, {});
// Look for the rest of the reference in the found item.
(item, cref_rest) = lookupCrefInItem(cref_rest, item, env);
cref = Absyn.pathToCrefWithSubs(new_path, subs);
cref = Absyn.joinCrefs(cref, cref_rest);
then
(item, cref);
end match;
end lookupCrefInPackage;
public function lookupNameInItem
"Looks up a name inside of an item, which can be either a variable or a
class."
input Absyn.Path inName;
input Item inItem;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
algorithm
(outItem, outPath, outEnv) := match(inName, inItem, inEnv)
local
SCode.Element var;
Item item;
Absyn.Path path;
Frame class_env;
Env env, type_env;
Absyn.TypeSpec type_spec;
SCode.Mod mods;
list<SCodeEnv.Redeclaration> redeclares;
Absyn.Info info;
// A variable.
case (_, SCodeEnv.VAR(var = SCode.COMPONENT(typeSpec = type_spec,
modifications = mods, info = info)), env)
equation
//env = SCodeEnv.setImportTableHidden(env, false);
// Look up the variable type.
(item, type_env) = lookupTypeSpec(type_spec, env, info);
// Apply redeclares to the type and look for the name inside the type.
redeclares = SCodeFlattenRedeclare.extractRedeclaresFromModifier(mods, env);
(item, type_env) = SCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(
redeclares, item, type_env, inEnv);
(item, path, env) = lookupNameInItem(inName, item, type_env);
then
(item, path, env);
// A class.
case (_, SCodeEnv.CLASS(env = {class_env}), _)
equation
// Look in the class's environment.
env = SCodeEnv.enterFrame(class_env, inEnv);
(item, path, env) = lookupNameInPackage(inName, env);
then
(item, path, env);
end match;
end lookupNameInItem;
public function lookupCrefInItem
"Looks up a component reference inside of an item, which can be either a
variable or a class."
input Absyn.ComponentRef inCref;
input Item inItem;
input Env inEnv;
output Item outItem;
output Absyn.ComponentRef outCref;
algorithm
(outItem, outCref) := match(inCref, inItem, inEnv)
local
Item item;
Absyn.ComponentRef cref;
Frame class_env;
Env env, type_env;
Absyn.TypeSpec type_spec;
SCode.Mod mods;
list<SCodeEnv.Redeclaration> redeclares;
Absyn.Info info;
// A variable.
case (_, SCodeEnv.VAR(var = SCode.COMPONENT(typeSpec = type_spec,
modifications = mods, info = info)), _)
equation
// Look up the variables' type.
(item, type_env) = lookupTypeSpec(type_spec, inEnv, info);
// Apply redeclares to the type and look for the name inside the type.
redeclares = SCodeFlattenRedeclare.extractRedeclaresFromModifier(
mods, inEnv);
(item, type_env) = SCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(
redeclares, item, type_env, inEnv);
(item, cref) = lookupCrefInItem(inCref, item, type_env);
then
(item, cref);
// A class.
case (_, SCodeEnv.CLASS(env = {class_env}), _)
equation
// Look in the class's environment.
env = SCodeEnv.enterFrame(class_env, inEnv);
(item, cref) = lookupCrefInPackage(inCref, env);
then
(item, cref);
end match;
end lookupCrefInItem;
public function lookupBaseClass
"Looks up from which base class a certain class is inherited from by searching
the extends in the local scope."
input SCode.Ident inClass;
input Env inEnv;
input Absyn.Info inInfo;
output Absyn.Path outBaseClass;
output Item outItem;
algorithm
(SOME(outItem), _, outBaseClass, _) := lookupInBaseClasses(inClass, inEnv,
INSERT_REDECLARES(), {});
end lookupBaseClass;
public function lookupRedeclaredClassByItem
input Item inItem;
input Env inEnv;
input Absyn.Info inInfo;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := matchcontinue(inItem, inEnv, inInfo)
local
SCode.Ident name;
Item item;
Env env;
SCode.Redeclare rdp;
SCode.Replaceable rpp;
case (SCodeEnv.CLASS(cls = SCode.CLASS(name = name)), _, _)
equation
(SOME(item), _, _, SOME(env)) = lookupInBaseClasses(name, inEnv,
IGNORE_REDECLARES(), {});
SCode.PREFIXES(redeclarePrefix = rdp, replaceablePrefix = rpp) =
SCodeEnv.getItemPrefixes(item);
(item, env) = lookupRedeclaredClass2(item, rdp, rpp, env, inInfo);
then
(item, env);
// No error message is output if the previous case fails. This is because
// lookupInBaseClasses is used by SCodeEnv.extendEnvWithClassExtends when
// adding the redeclaration to the environment, and lookupRedeclaredClass2
// outputs its own errors.
else
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- SCodeLookup.lookupRedeclaredClass2 failed on " +&
SCodeEnv.getItemName(inItem) +& " in " +&
SCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end lookupRedeclaredClassByItem;
protected function lookupRedeclaredClass2
input Item inItem;
input SCode.Redeclare inRedeclarePrefix;
input SCode.Replaceable inReplaceablePrefix;
input Env inEnv;
input Absyn.Info inInfo;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) :=
matchcontinue(inItem, inRedeclarePrefix, inReplaceablePrefix, inEnv, inInfo)
local
SCode.Ident name;
String scope_str;
Item item;
Env env;
Absyn.Info info;
SCode.Redeclare rdp;
SCode.Replaceable rpp;
// Replaceable element which is not a redeclaration => return the element.
case (_, SCode.NOT_REDECLARE(), SCode.REPLACEABLE(cc = _), _, _)
then (inItem, inEnv);
// Replaceable element which is a redeclaration => continue.
case (SCodeEnv.CLASS(cls = SCode.CLASS(name = name)),
SCode.REDECLARE(), SCode.REPLACEABLE(cc = _), _, _)
equation
(SOME(item), _, _, SOME(env)) = lookupInBaseClasses(name, inEnv,
IGNORE_REDECLARES(), {});
SCode.PREFIXES(redeclarePrefix = rdp, replaceablePrefix = rpp) =
SCodeEnv.getItemPrefixes(item);
(item, env) = lookupRedeclaredClass2(item, rdp, rpp, env, inInfo);
then
(item, env);
// Non-replaceable element => error.
case (SCodeEnv.CLASS(cls = SCode.CLASS(name = name, info = info)),
_, SCode.NOT_REPLACEABLE(), _, _)
equation
Error.addSourceMessage(Error.ERROR_FROM_HERE, {}, inInfo);
Error.addSourceMessage(Error.REDECLARE_NON_REPLACEABLE, {name}, info);
then
fail();
// Redeclaration of class to component => error.
case (SCodeEnv.VAR(var = SCode.COMPONENT(name = name, info = info)), _, _, _, _)
equation
Error.addSourceMessage(Error.ERROR_FROM_HERE, {}, inInfo);
Error.addSourceMessage(Error.REDECLARE_CLASS_AS_VAR, {name}, info);
then
fail();
else
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- SCodeLookup.lookupRedeclaredClass2 failed on " +&
SCodeEnv.getItemName(inItem) +& " in " +&
SCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end lookupRedeclaredClass2;
public function lookupBuiltinType
"Checks if a name references a builtin type, and returns an environment item
for that type or fails."
input Absyn.Ident inName;
output Item outItem;
algorithm
outItem := match(inName)
case "Real" then BUILTIN_REAL;
case "Integer" then BUILTIN_INTEGER;
case "Boolean" then BUILTIN_BOOLEAN;
case "String" then BUILTIN_STRING;
case "StateSelect" then BUILTIN_STATESELECT;
case "ExternalObject" then BUILTIN_EXTERNALOBJECT;
end match;
end lookupBuiltinType;
public function lookupName
"Looks up a simple or qualified name in the environment and returns the
environment item corresponding to the name, the path for the name and
optionally the enclosing scope of the name if the name references a class.
This function doesn't know what kind of thing the name references, so to get
meaningful error messages you should use one of the lookup****Name below
instead."
input Absyn.Path inName;
input Env inEnv;
input Absyn.Info inInfo;
input Option<Error.Message> inErrorType;
output Item outItem;
output Absyn.Path outName;
output Env outEnv;
algorithm
(outItem, outName, outEnv) :=
matchcontinue(inName, inEnv, inInfo, inErrorType)
local
Absyn.Ident id;
Item item;
Absyn.Path path, new_path;
Env env;
String name_str, env_str;
Error.Message error_id;
// A builtin type.
case (Absyn.IDENT(name = id), _, _, _)
equation
item = lookupBuiltinType(id);
then
(item, inName, SCodeEnv.emptyEnv);
// A builtin type with qualified path, i.e. StateSelect.something.
case (Absyn.QUALIFIED(name = id), _, _, _)
equation
item = lookupBuiltinType(id);
then
(item, inName, SCodeEnv.emptyEnv);
// Simple name.
case (Absyn.IDENT(name = id), _, _, _)
equation
(item, new_path, env) = lookupSimpleName(id, inEnv);
then
(item, new_path, env);
// Qualified name.
case (Absyn.QUALIFIED(name = id, path = path), _, _, _)
equation
// Look up the first identifier.
(item, new_path, env) = lookupSimpleName(id, inEnv);
// Look up the rest of the name in the environment of the first
// identifier.
(item, path, env) = lookupNameInItem(path, item, env);
path = SCodeEnv.joinPaths(new_path, path);
then
(item, path, env);
case (Absyn.FULLYQUALIFIED(path = path), _, _, _)
equation
(item, path, env) = lookupFullyQualified(path, inEnv);
then
(item, path, env);
case (_, _, _, SOME(error_id))
equation
name_str = Absyn.pathString(inName);
env_str = SCodeEnv.getEnvName(inEnv);
Error.addSourceMessage(error_id, {name_str, env_str}, inInfo);
then
fail();
end matchcontinue;
end lookupName;
public function lookupClassName
"Calls lookupName with the 'Class not found' error message."
input Absyn.Path inName;
input Env inEnv;
input Absyn.Info inInfo;
output Item outItem;
output Absyn.Path outName;
output Env outEnv;
algorithm
(outItem, outName, outEnv) := lookupName(inName, inEnv, inInfo,
SOME(Error.LOOKUP_ERROR));
end lookupClassName;
public function lookupBaseClassName
"Calls lookupName with the 'Baseclass not found' error message."
input Absyn.Path inName;
input Env inEnv;
input Absyn.Info inInfo;
output Item outItem;
output Absyn.Path outName;
output Env outEnv;
algorithm
(outItem, outName, outEnv) := lookupName(inName, inEnv, inInfo,
SOME(Error.LOOKUP_BASECLASS_ERROR));
end lookupBaseClassName;
public function lookupVariableName
"Calls lookupName with the 'Variable not found' error message."
input Absyn.Path inName;
input Env inEnv;
input Absyn.Info inInfo;
output Item outItem;
output Absyn.Path outName;
output Env outEnv;
algorithm
(outItem, outName, outEnv) := lookupName(inName, inEnv, inInfo,
SOME(Error.LOOKUP_VARIABLE_ERROR));
end lookupVariableName;
protected function crefStripEnvPrefix
"Removes the entire environment prefix from the given component reference, or
returns the unchanged reference. This is done because models might import
local packages, for example:
package P
import myP = InsideP;
package InsideP
function f end f;
end InsideP;
constant c = InsideP.f();
end P;
package P2
extends P;
end P2;
When P2 is instantiated all elements from P will be brought into P2's scope
due to the extends. The binding of c will still point to P.InsideP.f though, so
the lookup will try to instantiate P which might fail if P is a partial
package or for other reasons. This is really a bug in Lookup (it shouldn't
need to instantiate the whole package just to find a function), but to work
around this problem for now this function will remove the environment prefix
when InsideP.f is looked up in P, so that it resolves to InsideP.f and not
P.InsideP.f. This allows P2 to find it in the local scope instead, since the
InsideP package has been inherited from P."
input Absyn.ComponentRef inCref;
input Env inEnv;
output Absyn.ComponentRef outCref;
algorithm
outCref := matchcontinue(inCref, inEnv)
local
Absyn.Path env_path;
Absyn.ComponentRef cref;
case (_, _)
equation
env_path = SCodeEnv.getEnvPath(inEnv);
cref = Absyn.unqualifyCref(inCref);
then
crefStripEnvPrefix2(cref, env_path);
else inCref;
end matchcontinue;
end crefStripEnvPrefix;
protected function crefStripEnvPrefix2
input Absyn.ComponentRef inCref;
input Absyn.Path inEnvPath;
output Absyn.ComponentRef outCref;
algorithm
outCref := matchcontinue(inCref, inEnvPath)
local
Absyn.Ident id1, id2;
Absyn.ComponentRef cref;
Absyn.Path env_path;
case (Absyn.CREF_QUAL(name = id1, subscripts = {}, componentRef = cref),
Absyn.QUALIFIED(name = id2, path = env_path))
equation
true = stringEqual(id1, id2);
then
crefStripEnvPrefix2(cref, env_path);
case (Absyn.CREF_QUAL(name = id1, subscripts = {}, componentRef = cref),
Absyn.IDENT(name = id2))