/
Lookup.mo
2978 lines (2715 loc) · 113 KB
/
Lookup.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 Lookup
" file: Lookup.mo
package: Lookup
description: Scoping rules
RCS: $Id$
This module is responsible for the lookup mechanism in Modelica.
It is responsible for looking up classes, variables, etc. in the
environment Env by following the lookup rules.
The most important functions are:
lookupClass - to find a class
lookupType - to find types (e.g. functions, types, etc.)
lookupVar - to find a variable in the instance hierarchy."
public import Absyn;
public import ClassInf;
public import DAE;
public import Env;
public import HashTableStringToPath;
public import InstTypes;
public import SCode;
public import Util;
public import Types;
protected import BaseHashTable;
protected import Builtin;
protected import ComponentReference;
protected import Config;
protected import Connect;
protected import ConnectionGraph;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import Inst;
protected import InstExtends;
protected import InstFunction;
protected import InstUtil;
protected import InnerOuter;
protected import List;
protected import Mod;
protected import Prefix;
protected import Static;
protected import UnitAbsyn;
protected import SCodeDump;
protected import ErrorExt;
/* - Lookup functions
These functions look up class and variable names in the environment.
The names are supplied as a path, and if the path is qualified, a
variable named as the first part of the path is searched for, and the
name is looked for in it.
*/
public function lookupType
" This function finds a specified type in the environment.
If it finds a function instead, this will be implicitly instantiated
and lookup will start over.
"
input Env.Cache inCache;
input Env.Env inEnv "environment to search in";
input Absyn.Path inPath "type to look for";
input Option<Absyn.Info> msg "Messaage flag, SOME() outputs lookup error messages";
output Env.Cache outCache;
output DAE.Type outType "the found type";
output Env.Env outEnv "The environment the type was found in";
algorithm
(outCache,outType,outEnv):=
matchcontinue (inCache,inEnv,inPath,msg)
local
DAE.Type t;
list<Env.Frame> env_1,env,env_2;
Absyn.Path path;
SCode.Element c;
String classname,scope;
Env.Cache cache;
Absyn.Info info;
// Special handling for Connections.isRoot
case (cache,env,Absyn.QUALIFIED("Connections", Absyn.IDENT("isRoot")),_)
equation
t = DAE.T_FUNCTION({("x", DAE.T_ANYTYPE_DEFAULT, DAE.C_VAR(), DAE.NON_PARALLEL(), NONE())}, DAE.T_BOOL_DEFAULT, DAE.FUNCTION_ATTRIBUTES_DEFAULT, DAE.emptyTypeSource);
then
(cache, t, env);
// Special handling for MultiBody 3.x rooted() operator
case (cache,env,Absyn.IDENT("rooted"),_)
equation
t = DAE.T_FUNCTION({("x", DAE.T_ANYTYPE_DEFAULT, DAE.C_VAR(), DAE.NON_PARALLEL(), NONE())}, DAE.T_BOOL_DEFAULT, DAE.FUNCTION_ATTRIBUTES_DEFAULT, DAE.emptyTypeSource);
then
(cache, t, env);
// For simple names
case (cache,env,(path as Absyn.IDENT(name = _)),_)
equation
(cache,t,env_1) = lookupTypeInEnv(cache,env,path);
then
(cache,t,env_1);
// Special classes (function, record, metarecord, external object)
case (cache,env,path,_)
equation
(cache,c,env_1) = lookupClass(cache,env,path,false);
(cache,t,env_2) = lookupType2(cache,env_1,path,c);
then
(cache,t,env_2);
// Error for type not found
case (cache,env,path,SOME(info))
equation
classname = Absyn.pathString(path);
classname = stringAppend(classname," (its type) ");
scope = Env.printEnvPathStr(env);
Error.addSourceMessage(Error.LOOKUP_ERROR, {classname,scope}, info);
then
fail();
end matchcontinue;
end lookupType;
protected function lookupType2
" This function handles the case when we looked up a class, but need to
check if it is function, record, metarecord, etc.
"
input Env.Cache inCache;
input Env.Env inEnv "environment to search in";
input Absyn.Path inPath "type to look for";
input SCode.Element inClass "the class lookupType found";
output Env.Cache outCache;
output DAE.Type outType "the found type";
output Env.Env outEnv "The environment the type was found in";
algorithm
(outCache,outType,outEnv) := matchcontinue (inCache,inEnv,inPath,inClass)
local
DAE.Type t;
list<Env.Frame> env_1,env_2,env_3;
Absyn.Path path;
SCode.Element c;
String id;
Env.Cache cache;
SCode.Restriction r;
list<DAE.Var> types;
list<String> names;
ClassInf.State ci_state;
SCode.Encapsulated encflag;
DAE.TypeSource ts;
/*/ possibly qualified path
case (cache,env_1,path,c)
equation
path = Env.pathStripEnvIfFullyQualifedInEnv(path, env_1);
(cache,t,env_1) = lookupType2(cache,env_1,path,c);
then
(cache,t,env_1);*/
// Record constructors
case (cache,env_1,path,c as SCode.CLASS(name=id,restriction=SCode.R_RECORD(_)))
equation
(cache,env_1,t) = buildRecordType(cache,env_1,c);
then
(cache,t,env_1);
// lookup of an enumeration type
case (cache,env_1,path,c as SCode.CLASS(name=id,encapsulatedPrefix=encflag,restriction=r as SCode.R_ENUMERATION()))
equation
env_2 = Env.openScope(env_1, encflag, SOME(id), SOME(Env.CLASS_SCOPE()));
ci_state = ClassInf.start(r, Env.getEnvName(env_2));
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP TYPE ICD: " +& Env.printEnvPathStr(env_1) +& " path:" +& Absyn.pathString(path));
(cache,env_3,_,_,_,_,_,types,_,_,_,_) =
Inst.instClassIn(
cache,env_2,InnerOuter.emptyInstHierarchy,UnitAbsyn.noStore,
DAE.NOMOD(), Prefix.NOPRE(),
ci_state, c, SCode.PUBLIC(), {}, false, InstTypes.INNER_CALL(),
ConnectionGraph.EMPTY, Connect.emptySet, NONE());
// build names
(_,names) = SCode.getClassComponents(c);
// generate the enumeration type
ts = Types.mkTypeSource(SOME(path));
t = DAE.T_ENUMERATION(NONE(), path, names, types, {}, ts);
env_3 = Env.extendFrameT(env_3, id, t);
then
(cache,t,env_3);
// Metamodelica extension, Uniontypes
case (cache,env_1,path,c as SCode.CLASS(restriction=SCode.R_METARECORD(index=_)))
equation
(cache,env_2,t) = buildMetaRecordType(cache,env_1,c);
then
(cache,t,env_2);
// Classes that are external objects. Implicitly instantiate to get type
case (cache,env_1,path,c)
equation
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP TYPE ICD: " +& Env.printEnvPathStr(env_1) +& " path:" +& Absyn.pathString(path));
true = SCode.classIsExternalObject(c);
(cache,_::env_1,_,_,_,_,_,_,_,_) = Inst.instClass(
cache,env_1,InnerOuter.emptyInstHierarchy, UnitAbsyn.noStore,
DAE.NOMOD(), Prefix.NOPRE(), c,
{}, false, InstTypes.TOP_CALL(), ConnectionGraph.EMPTY, Connect.emptySet);
SCode.CLASS(name=id) = c;
(cache,t,env_2) = lookupTypeInEnv(cache,env_1,Absyn.IDENT(id));
then
(cache,t,env_2);
// If we find a class definition that is a function or external function
// with the same name then we implicitly instantiate that function, look
// up the type.
case (cache,env_1,path,c as SCode.CLASS(name = id,restriction=SCode.R_FUNCTION(_)))
equation
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP TYPE ICD: " +& Env.printEnvPathStr(env_1) +& " path:" +& Absyn.pathString(path));
(cache,env_2,_) =
InstFunction.implicitFunctionTypeInstantiation(cache,env_1,InnerOuter.emptyInstHierarchy,c);
(cache,t,env_3) = lookupTypeInEnv(cache,env_2,Absyn.IDENT(id));
then
(cache,t,env_3);
end matchcontinue;
end lookupType2;
public function lookupMetarecordsRecursive
"Takes a list of paths to Uniontypes. Use this list to create a list of T_METARECORD.
The function is guarded against recursive definitions by accumulating all paths it
starts to traverse."
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Path> inUniontypePaths;
output Env.Cache outCache;
output list<DAE.Type> outMetarecordTypes;
algorithm
(outCache,_,outMetarecordTypes) := lookupMetarecordsRecursive2(inCache, inEnv, inUniontypePaths, HashTableStringToPath.emptyHashTable(), {});
end lookupMetarecordsRecursive;
protected function lookupMetarecordsRecursive2
"Takes a list of paths to Uniontypes. Use this list to create a list of T_METARECORD.
The function is guarded against recursive definitions by accumulating all paths it
starts to traverse."
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Path> inUniontypePaths;
input HashTableStringToPath.HashTable inHt;
input list<DAE.Type> inAcc;
output Env.Cache outCache;
output HashTableStringToPath.HashTable outHt;
output list<DAE.Type> outMetarecordTypes;
algorithm
(outCache,outHt,outMetarecordTypes) := match (inCache, inEnv, inUniontypePaths, inHt, inAcc)
local
Env.Cache cache;
Env.Env env;
Absyn.Path first;
list<Absyn.Path> rest;
HashTableStringToPath.HashTable ht;
list<DAE.Type> acc;
case (cache, _, {}, ht, acc) then (cache, ht, acc);
case (cache, env, first::rest, ht, acc)
equation
(cache,ht,acc) = lookupMetarecordsRecursive3(cache, env, first, Absyn.pathString(first), ht, acc);
(cache,ht,acc) = lookupMetarecordsRecursive2(cache, env, rest, ht, acc);
then (cache, ht, acc);
end match;
end lookupMetarecordsRecursive2;
protected function lookupMetarecordsRecursive3
"Takes a list of paths to Uniontypes. Use this list to create a list of T_METARECORD.
The function is guarded against recursive definitions by accumulating all paths it
starts to traverse."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path path;
input String str;
input HashTableStringToPath.HashTable inHt;
input list<DAE.Type> inAcc;
output Env.Cache outCache;
output HashTableStringToPath.HashTable outHt;
output list<DAE.Type> outMetarecordTypes;
algorithm
(outCache,outHt,outMetarecordTypes) := matchcontinue (inCache, inEnv, path, str, inHt, inAcc)
local
Env.Cache cache;
Env.Env env;
list<Absyn.Path> uniontypePaths;
list<DAE.Type> uniontypeTypes;
DAE.Type ty;
list<DAE.Type> acc;
HashTableStringToPath.HashTable ht;
case (cache, env, _, _, ht, acc)
equation
_ = BaseHashTable.get(str, ht);
then (cache, ht, acc);
case (cache, env, _, _, ht, acc)
equation
ht = BaseHashTable.add((str,path),ht);
(cache, ty, _) = lookupType(cache, env, path, SOME(Absyn.dummyInfo));
acc = ty::acc;
uniontypeTypes = Types.getAllInnerTypesOfType(ty, Types.uniontypeFilter);
uniontypePaths = List.flatten(List.map(uniontypeTypes, Types.getUniontypePaths));
(cache, ht, acc) = lookupMetarecordsRecursive2(cache, env, uniontypePaths, ht, acc);
then (cache,ht,acc);
end matchcontinue;
end lookupMetarecordsRecursive3;
public function lookupClass "Tries to find a specified class in an environment"
input Env.Cache inCache;
input Env.Env inEnv "Where to look";
input Absyn.Path inPath "Path of the class to look for";
input Boolean msg "Controls error messages";
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv;
algorithm
// print("Lookup C1: " +& Absyn.pathString(inPath) +& " env: " +& Env.printEnvPathStr(inEnv) +& " msg: " +& boolString(msg) +& "\n");
(outCache,outClass,outEnv) := matchcontinue(inCache, inEnv, inPath, msg)
local Absyn.Path p;
/*/ strip fully qualified paths in this env
case (_, _, _, _)
equation
p = Env.pathStripEnvIfFullyQualifedInEnv(inPath, inEnv);
(outCache,outClass,outEnv,_) = lookupClass1(inCache, inEnv, p, {}, Util.makeStatefulBoolean(false), msg);
then
(outCache,outClass,outEnv);*/
// normal case
case (_, _, _, _)
equation
(outCache,outClass,outEnv,_) = lookupClass1(inCache, inEnv, inPath, {}, Util.makeStatefulBoolean(false), msg);
then
(outCache,outClass,outEnv);
end matchcontinue;
// print("Lookup C2: " +& " outenv: " +& Env.printEnvPathStr(outEnv) +& "\n");
end lookupClass;
protected function lookupClass1 "help function to lookupClass, does all the work."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path inPath "The path of the class to lookup";
input list<Env.Frame> inPrevFrames "Environment in reverse order. Contains frames we previously had in the scope. Will be looked up instead of the environment in order to avoid infinite recursion.";
input Util.StatefulBoolean inState "If true, we have found a class. If the path was qualified, we should no longer look in previous frames of the environment";
input Boolean msg "Print error messages";
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv "The environment in which the class was found (not the environment inside the class)";
output list<Env.Frame> outPrevFrames;
algorithm
(outCache,outClass,outEnv,outPrevFrames) := matchcontinue (inCache,inEnv,inPath,inPrevFrames,inState,msg)
local
String id,scope;
case (_,_,_,_,_,_)
equation
(outCache,outClass,outEnv,outPrevFrames) = lookupClass2(inCache,inEnv,inPath,inPrevFrames,inState,false);
then (outCache,outClass,outEnv,outPrevFrames);
case (_,_,_,_,_,true)
equation
id = Absyn.pathString(inPath);
scope = Env.printEnvPathStr(inEnv);
Error.addMessage(Error.LOOKUP_ERROR, {id,scope});
then fail();
/*case (_,_,_,_,_,_)
equation
true = Flags.isSet(Flags.FAILTRACE);
id = Absyn.pathString(inPath);
scope = Env.printEnvPathStr(inEnv);
Debug.fprintln(Flags.FAILTRACE, "- Lookup.lookupClass failed:\n" +&
id +& " in:\n" +&
scope);
then fail();*/
end matchcontinue;
end lookupClass1;
protected function lookupClass2 "help function to lookupClass, does all the work."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path inPath "The path of the class to lookup";
input list<Env.Frame> inPrevFrames "Environment in reverse order. Contains frames we previously had in the scope. Will be looked up instead of the environment in order to avoid infinite recursion.";
input Util.StatefulBoolean inState "If true, we have found a class. If the path was qualified, we should no longer look in previous frames of the environment";
input Boolean msg "Print error messages";
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv "The environment in which the class was found (not the environment inside the class)";
output list<Env.Frame> outPrevFrames;
algorithm
(outCache,outClass,outEnv,outPrevFrames) := matchcontinue (inCache,inEnv,inPath,inPrevFrames,inState,msg)
local
Env.Frame f;
Env.Cache cache;
SCode.Element c;
list<Env.Frame> env,env_1,env_2,fs,prevFrames;
Absyn.Path path,p,scope;
String id,pack;
Option<Env.Frame> optFrame;
// Fully qualified names are looked up in top scope. With previous frames remembered.
case (cache,env,Absyn.FULLYQUALIFIED(path),{},_,_)
equation
f::prevFrames = listReverse(env);
Util.setStatefulBoolean(inState,true);
(cache,c,env_1,prevFrames) = lookupClass2(cache,{f},path,prevFrames,inState,msg);
then
(cache,c,env_1,prevFrames);
// Qualified names are handled in a special function in order to avoid infinite recursion.
case (cache,env,(p as Absyn.QUALIFIED(name = pack,path = path)),prevFrames,_,_)
equation
(optFrame,prevFrames) = lookupPrevFrames(pack,prevFrames);
(cache,c,env_2,prevFrames) = lookupClassQualified(cache,env,pack,path,optFrame,prevFrames,inState,msg);
then
(cache,c,env_2,prevFrames);
// Simple names
case (cache,env,Absyn.IDENT(name = id),prevFrames,_,_)
equation
(cache,c,env_1,prevFrames) = lookupClassInEnv(cache,env, id, prevFrames, inState, msg);
then
(cache,c,env_1,prevFrames);
/*
case (cache,env,p,_,_,_)
equation
Debug.traceln("lookupClass failed " +& Absyn.pathString(p) +& " " +& Env.printEnvPathStr(env));
then fail();
*/
end matchcontinue;
end lookupClass2;
protected function lookupClassQualified
input Env.Cache inCache;
input Env.Env inEnv;
input String id;
input Absyn.Path path;
input Option<Env.Frame> inOptFrame;
input list<Env.Frame> inPrevFrames "Environment in reverse order. Contains frames we previously had in the scope. Will be looked up instead of the environment in order to avoid infinite recursion.";
input Util.StatefulBoolean inState "If true, we have found a class. If the path was qualified, we should no longer look in previous frames of the environment";
input Boolean msg "Print error messages";
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv "The environment in which the class was found (not the environment inside the class)";
output list<Env.Frame> outPrevFrames;
algorithm
(outCache,outClass,outEnv,outPrevFrames) := matchcontinue (inCache,inEnv,id,path,inOptFrame,inPrevFrames,inState,msg)
local
SCode.Element c;
Absyn.Path scope;
Env.Cache cache;
Env.Env env,prevFrames;
Env.Frame frame;
Option<Env.Frame> optFrame;
// Qualified names first identifier cached in previous frames
case (cache,env,_,_,SOME(frame),prevFrames,_,_)
equation
Util.setStatefulBoolean(inState,true);
env = frame::env;
(cache,c,env,prevFrames) = lookupClass2(cache,env,path,prevFrames,inState,msg);
then
(cache,c,env,prevFrames);
// Qualified names in package and non-package
case (cache,env,_,_,NONE(),_,_,_)
equation
(cache,c,env,prevFrames) = lookupClass2(cache,env,Absyn.IDENT(id),{},inState,msg);
(optFrame,prevFrames) = lookupPrevFrames(id,prevFrames);
(cache,c,env,prevFrames) = lookupClassQualified2(cache,env,path,c,optFrame,prevFrames,inState,msg);
then
(cache,c,env,prevFrames);
end matchcontinue;
end lookupClassQualified;
protected function lookupClassQualified2
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path path;
input SCode.Element inC;
input Option<Env.Frame> optFrame;
input list<Env.Frame> inPrevFrames "Environment in reverse order. Contains frames we previously had in the scope. Will be looked up instead of the environment in order to avoid infinite recursion.";
input Util.StatefulBoolean inState "If true, we have found a class. If the path was qualified, we should no longer look in previous frames of the environment";
input Boolean msg "Print error messages";
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv "The environment in which the class was found (not the environment inside the class)";
output list<Env.Frame> outPrevFrames;
algorithm
(outCache,outClass,outEnv,outPrevFrames) := match (inCache,inEnv,path,inC,optFrame,inPrevFrames,inState,msg)
local
Env.Cache cache;
Env.Env env,prevFrames;
Env.Frame frame;
SCode.Restriction restr;
ClassInf.State ci_state;
SCode.Encapsulated encflag;
String id;
SCode.Element c;
case (cache,env,_,_,SOME(frame),prevFrames,_,_)
equation
env = frame::env;
(cache,c,env,prevFrames) = lookupClass2(cache,env,path,prevFrames,inState,msg);
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP CLASS QUALIFIED FRAME: " +& Env.printEnvPathStr(env) +& " path: " +& Absyn.pathString(path) +& " class: " +& SCodeDump.shortElementStr(c));
then (cache,c,env,prevFrames);
case (cache,env,_,SCode.CLASS(name=id,encapsulatedPrefix=encflag,restriction=restr),NONE(),_,_,_)
equation
env = Env.openScope(env, encflag, SOME(id), Env.restrictionToScopeType(restr));
ci_state = ClassInf.start(restr, Env.getEnvName(env));
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP CLASS QUALIFIED PARTIALICD: " +& Env.printEnvPathStr(env) +& " path: " +& Absyn.pathString(path) +& " class: " +& SCodeDump.shortElementStr(c));
(cache,env,_,_,_) =
Inst.partialInstClassIn(
cache,env,InnerOuter.emptyInstHierarchy,
DAE.NOMOD(), Prefix.NOPRE(),
ci_state, inC, SCode.PUBLIC(), {}, 0);
// Was 2 cases for package/non-package - all they did was fail or succeed on this
// If we comment it out, we get faster code, and less of it to maintain
// ClassInf.valid(cistate1, SCode.R_PACKAGE());
(cache,c,env,prevFrames) = lookupClass2(cache,env,path,{},inState,msg);
then (cache,c,env,prevFrames);
end match;
end lookupClassQualified2;
protected function lookupPrevFrames
input String id;
input list<Env.Frame> inPrevFrames;
output Option<Env.Frame> outFrame;
output list<Env.Frame> outPrevFrames;
algorithm
(outFrame,outPrevFrames) := matchcontinue (id,inPrevFrames)
local
String sid;
list<Env.Frame> prevFrames;
Env.Frame frame;
case (_,(frame as Env.FRAME(name = SOME(sid)))::prevFrames)
equation
true = id ==& sid;
then (SOME(frame),prevFrames);
case (_,_) then (NONE(),{});
end matchcontinue;
end lookupPrevFrames;
protected function lookupQualifiedImportedVarInFrame
"author: PA
Looking up variables (constants) imported using qualified imports,
i.e. import Modelica.Constants.PI;"
input list<Absyn.Import> inImports;
input SCode.Ident ident;
output DAE.ComponentRef outCref;
algorithm
(outCref) := matchcontinue (inImports,ident)
local
String id;
list<Absyn.Import> rest;
Absyn.Path path;
// For imported simple name, e.g. A, not possible to assert sub-path package
case (Absyn.QUAL_IMPORT(path = path) :: rest, _)
equation
id = Absyn.pathLastIdent(path);
true = id ==& ident;
then ComponentReference.pathToCref(path);
// Named imports, e.g. import A = B.C;
case (Absyn.NAMED_IMPORT(name = id,path = path) :: rest, _)
equation
true = id ==& ident;
then ComponentReference.pathToCref(path);
// Check next frame.
case (_ :: rest, _) then lookupQualifiedImportedVarInFrame(rest, ident);
end matchcontinue;
end lookupQualifiedImportedVarInFrame;
protected function moreLookupUnqualifiedImportedVarInFrame
"Helper function for lookup_unqualified_imported_var_in_frame. Returns
true if there are unqualified imports that matches a sought constant."
input Env.Cache inCache;
input list<Absyn.Import> inImports;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Boolean outBoolean;
algorithm
(outCache,outBoolean) := matchcontinue (inCache,inImports,inEnv,inIdent)
local
Env.Frame f;
String ident;
Boolean res;
list<Env.Frame> env,prevFrames;
list<Absyn.Import> rest;
Env.Cache cache;
DAE.ComponentRef cref;
Absyn.Path path;
case (cache,Absyn.UNQUAL_IMPORT(path = path) :: rest,env,ident)
equation
f::prevFrames = listReverse(env);
cref = ComponentReference.pathToCref(path);
cref = ComponentReference.crefPrependIdent(cref,ident,{},DAE.T_UNKNOWN_DEFAULT);
(cache,_,_,_,_,_,_,_,_) = lookupVarInPackages(cache,{f},cref,prevFrames,Util.makeStatefulBoolean(false));
then
(cache,true);
// look into the parent scope
case (cache,(_ :: rest),env,ident)
equation
(cache, res) = moreLookupUnqualifiedImportedVarInFrame(cache, rest, env, ident);
then
(cache, res);
// we reached the end, no more lookup
case (cache,{},_,_) then (cache, false);
end matchcontinue;
end moreLookupUnqualifiedImportedVarInFrame;
protected function lookupUnqualifiedImportedVarInFrame "Find a variable from an unqualified import locally in a frame"
input Env.Cache inCache;
input list<Absyn.Import> inImports;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Env.Env outClassEnv;
output DAE.Attributes outAttributes;
output DAE.Type outType;
output DAE.Binding outBinding;
output Option<DAE.Const> constOfForIteratorRange "SOME(constant-ness) of the range if this is a for iterator, NONE() if this is not a for iterator";
output Boolean outBoolean;
output InstTypes.SplicedExpData splicedExpData;
output Env.Env outComponentEnv;
output String name;
algorithm
(outCache,outClassEnv,outAttributes,outType,outBinding,constOfForIteratorRange,outBoolean,splicedExpData,outComponentEnv,name):=
matchcontinue (inCache,inImports,inEnv,inIdent)
local
Env.Frame f;
DAE.ComponentRef cref;
String ident;
Boolean more,unique;
list<Env.Frame> env,classEnv,componentEnv,prevFrames;
DAE.Attributes attr;
DAE.Type ty;
DAE.Binding bind;
list<Absyn.Import> rest;
Env.Cache cache;
Absyn.Path path;
Option<DAE.Const> cnstForRange;
// unique
case (cache,Absyn.UNQUAL_IMPORT(path = path) :: rest,env,ident)
equation
f::prevFrames = listReverse(env);
cref = ComponentReference.pathToCref(path);
cref = ComponentReference.crefPrependIdent(cref,ident,{},DAE.T_UNKNOWN_DEFAULT);
(cache,classEnv,attr,ty,bind,cnstForRange,splicedExpData,componentEnv,name) = lookupVarInPackages(cache,{f},cref,prevFrames,Util.makeStatefulBoolean(false));
(cache,more) = moreLookupUnqualifiedImportedVarInFrame(cache, rest, env, ident);
unique = boolNot(more);
then
(cache,classEnv,attr,ty,bind,cnstForRange,unique,splicedExpData,componentEnv,name);
// search in the parent scopes
case (cache,_ :: rest,env,ident)
equation
(cache,classEnv,attr,ty,bind,cnstForRange,unique,splicedExpData,componentEnv,name) = lookupUnqualifiedImportedVarInFrame(cache, rest, env, ident);
then
(cache,classEnv,attr,ty,bind,cnstForRange,unique,splicedExpData,componentEnv,name);
end matchcontinue;
end lookupUnqualifiedImportedVarInFrame;
protected function lookupQualifiedImportedClassInFrame
"Helper function to lookupQualifiedImportedClassInEnv."
input Env.Cache inCache;
input list<Absyn.Import> inImport;
input Env.Env inEnv;
input SCode.Ident inIdent;
input Util.StatefulBoolean inState;
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv;
output Env.Env outPrevFrames;
algorithm
(outCache,outClass,outEnv,outPrevFrames) := matchcontinue (inCache,inImport,inEnv,inIdent,inState)
local
Env.Frame fr;
SCode.Element c;
list<Env.Frame> env_1,env,prevFrames;
String id,ident;
list<Absyn.Import> rest;
Absyn.Path path;
Env.Cache cache;
case (cache,Absyn.QUAL_IMPORT(path = Absyn.IDENT(name = id)) :: _,env,ident,_)
equation
true = id ==& ident "For imported paths A, not possible to assert sub-path package";
Util.setStatefulBoolean(inState,true);
fr::prevFrames = listReverse(env);
(cache,c,env_1,prevFrames) = lookupClass2(cache,{fr},Absyn.IDENT(id),prevFrames,Util.makeStatefulBoolean(false),true);
then
(cache,c,env_1,prevFrames);
case (cache,Absyn.QUAL_IMPORT(path = path) :: rest,env,ident,_)
equation
id = Absyn.pathLastIdent(path) "For imported path A.B.C, assert A.B is package" ;
true = id ==& ident;
Util.setStatefulBoolean(inState,true);
fr::prevFrames = listReverse(env);
// strippath = Absyn.stripLast(path);
// (cache,c2,env_1,_) = lookupClass2(cache,{fr},strippath,prevFrames,Util.makeStatefulBoolean(false),true);
(cache,c,env_1,prevFrames) = lookupClass2(cache,{fr},path,prevFrames,Util.makeStatefulBoolean(false),true);
then
(cache,c,env_1,prevFrames);
case (cache,Absyn.NAMED_IMPORT(name = id,path = path) :: rest,env,ident,_)
equation
true = id ==& ident "Named imports";
Util.setStatefulBoolean(inState,true);
fr::prevFrames = listReverse(env);
// strippath = Absyn.stripLast(path);
// Debug.traceln("named import " +& id +& " is " +& Absyn.pathString(path));
// (cache,c2,env_1,prevFrames) = lookupClass2(cache,{fr},strippath,prevFrames,Util.makeStatefulBoolean(false),true);
(cache,c,env_1,prevFrames) = lookupClass2(cache,{fr},path,prevFrames,Util.makeStatefulBoolean(false),true);
then
(cache,c,env_1,prevFrames);
case (cache,_ :: rest,env,ident,_)
equation
(cache,c,env_1,prevFrames) = lookupQualifiedImportedClassInFrame(cache,rest,env,ident,inState);
then
(cache,c,env_1,prevFrames);
end matchcontinue;
end lookupQualifiedImportedClassInFrame;
protected function moreLookupUnqualifiedImportedClassInFrame
"Helper function for lookupUnqualifiedImportedClassInFrame"
input Env.Cache inCache;
input list<Absyn.Import> inImports;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Boolean outBoolean;
algorithm
(outCache,outBoolean) := matchcontinue (inCache,inImports,inEnv,inIdent)
local
Env.Frame fr,f;
SCode.Element c;
String id,ident;
SCode.Encapsulated encflag;
Boolean res;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,env;
ClassInf.State ci_state;
Absyn.Path path;
Absyn.Ident firstIdent;
list<Absyn.Import> rest;
Env.Cache cache;
// Not found, instantiate
case (cache,Absyn.UNQUAL_IMPORT(path = path) :: rest,env,ident)
equation
fr = Env.topFrame(env);
(cache,(c as SCode.CLASS(name=id,encapsulatedPrefix=encflag,restriction=restr)),env_1) = lookupClass(cache, {fr}, path, false);
env2 = Env.openScope(env_1, encflag, SOME(id), Env.restrictionToScopeType(restr));
ci_state = ClassInf.start(restr, Env.getEnvName(env2));
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP MORE UNQUALIFIED IMPORTED ICD: " +& Env.printEnvPathStr(env) +& "." +& ident);
(cache,(f :: _),_,_,_) = Inst.partialInstClassIn(cache, env2, InnerOuter.emptyInstHierarchy, DAE.NOMOD(), Prefix.NOPRE(), ci_state, c, SCode.PUBLIC(), {}, 0);
(cache,_,_) = lookupClass(cache,{f}, Absyn.IDENT(ident), false);
then
(cache, true);
// Look in the parent scope
case (cache,_ :: rest,env,ident)
equation
(cache, res) = moreLookupUnqualifiedImportedClassInFrame(cache, rest, env, ident);
then
(cache, res);
case (cache,{},_,_) then (cache,false);
end matchcontinue;
end moreLookupUnqualifiedImportedClassInFrame;
protected function lookupUnqualifiedImportedClassInFrame
"Finds a class from an unqualified import locally in a frame"
input Env.Cache inCache;
input list<Absyn.Import> inImports;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv;
output Env.Env outPrevFrames;
output Boolean outBoolean;
algorithm
(outCache,outClass,outEnv,outPrevFrames,outBoolean) := matchcontinue (inCache,inImports,inEnv,inIdent)
local
Env.Frame fr;
SCode.Element c,c_1;
String id,ident;
SCode.Encapsulated encflag;
Boolean more,unique;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,env,prevFrames;
ClassInf.State ci_state,cistate1;
Absyn.Path path;
list<Absyn.Import> rest;
Env.Cache cache;
Absyn.Ident firstIdent;
// Not in cache, instantiate, unique
case (cache,Absyn.UNQUAL_IMPORT(path = path) :: rest,env,ident)
equation
fr::prevFrames = listReverse(env);
(cache,(c as SCode.CLASS(name=id,encapsulatedPrefix=encflag,restriction=restr)),env_1,prevFrames) = lookupClass2(cache,{fr},path,prevFrames,Util.makeStatefulBoolean(false),false);
env2 = Env.openScope(env_1, encflag, SOME(id), Env.restrictionToScopeType(restr));
ci_state = ClassInf.start(restr, Env.getEnvName(env2));
// Debug.fprintln(Flags.INST_TRACE, "LOOKUP UNQUALIFIED IMPORTED ICD: " +& Env.printEnvPathStr(env) +& "." +& ident);
(cache,env2,_,cistate1,_) =
Inst.partialInstClassIn(cache, env2, InnerOuter.emptyInstHierarchy,
DAE.NOMOD(), Prefix.NOPRE(), ci_state, c, SCode.PUBLIC(), {}, 0);
// Restrict import to the imported scope only, not its parents, thus {f} below
(cache,c_1,env2,prevFrames) = lookupClass2(cache,env2,Absyn.IDENT(ident),prevFrames,Util.makeStatefulBoolean(true),false) "Restrict import to the imported scope only, not its parents..." ;
(cache,more) = moreLookupUnqualifiedImportedClassInFrame(cache, rest, env, ident);
unique = boolNot(more);
then
(cache,c_1,env2,prevFrames,unique);
// Look in the parent scope
case (cache,_ :: rest,env,ident)
equation
(cache,c,env_1,prevFrames,unique) = lookupUnqualifiedImportedClassInFrame(cache, rest, env, ident);
then
(cache,c,env_1,prevFrames,unique);
end matchcontinue;
end lookupUnqualifiedImportedClassInFrame;
public function lookupRecordConstructorClass
"Searches for a record constructor implicitly defined by a record class."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path inPath;
output Env.Cache outCache;
output SCode.Element outClass;
output Env.Env outEnv;
algorithm
(outCache,outClass,outEnv) := match (inCache,inEnv,inPath)
local
SCode.Element c;
Env.Env env,env_1;
Absyn.Path path;
String name;
Env.Cache cache;
case (cache,env,path)
equation
(cache,c,env_1) = lookupClass(cache,env, path, false);
SCode.CLASS(name = name, restriction=SCode.R_RECORD(_)) = c;
(cache,_,c) = buildRecordConstructorClass(cache,env_1,c);
then
(cache,c,env_1);
end match;
end lookupRecordConstructorClass;
public function lookupConnectorVar
"looks up a connector variable, but takes InnerOuter attribute from component if
inside connector, i.e. for connector reference a.b the innerOuter attribute is
fetched from a."
input Env.Cache inCache;
input Env.Env inEnv;
input DAE.ComponentRef cr;
output Env.Cache outCache;
output DAE.Attributes attr;
output DAE.Type tp;
algorithm
(outCache,attr,tp) := match(inCache,inEnv,cr)
local
DAE.ComponentRef cr1;
SCode.ConnectorType ct;
SCode.Parallelism prl;
SCode.Variability var;
Absyn.Direction dir;
Absyn.InnerOuter io;
DAE.Type ty1;
DAE.Attributes attr1;
Env.Cache cache;
Env.Env env;
SCode.Visibility vis;
// unqualified component reference
case(cache,env,DAE.CREF_IDENT(ident=_))
equation
(cache,attr1,ty1,_,_,_,_,_,_) = lookupVarLocal(cache,env,cr);
then
(cache,attr1,ty1);
// qualified component reference
case(cache,env,DAE.CREF_QUAL(ident=_))
equation
(cache,attr1 as DAE.ATTR(ct,prl,var,dir,_,vis),ty1,_,_,_,_,_,_) = lookupVarLocal(cache,env,cr);
cr1 = ComponentReference.crefStripLastIdent(cr);
// Find innerOuter attribute from "parent"
(cache,DAE.ATTR(innerOuter=io),_,_,_,_,_,_,_) = lookupVarLocal(cache,env,cr1);
then
(cache,DAE.ATTR(ct,prl,var,dir,io,vis),ty1);
end match;
end lookupConnectorVar;
public function lookupVar
"LS: when looking up qualified component reference, lookupVar only
checks variables when looking for the prefix, i.e. for Constants.PI
where Constants is a package and is implicitly instantiated, PI is not
found since Constants is not a variable (it is a type and/or class).
1) One option is to make it a variable and put it in the global frame.
2) Another option is to add a lookup rule that also looks in types.
Now implicitly instantiated packages exists both as a class and as a
type (see implicit_instantiation in Inst.mo). Is this correct?
lookupVar is modified to implement 2. Is this correct?
old lookupVar is changed to lookupVarInternal and a new lookupVar
is written, that first tests the old lookupVar, and if not found
looks in the types
function: lookupVar
This function tries to finds a variable in the environment
Arg1: The environment to search in
Arg2: The variable to search for."
input Env.Cache inCache;
input Env.Env inEnv;
input DAE.ComponentRef inComponentRef;
output Env.Cache outCache;
output DAE.Attributes outAttributes;
output DAE.Type outType;
output DAE.Binding outBinding;
output Option<DAE.Const> constOfForIteratorRange "SOME(constant-ness) of the range if this is a for iterator, NONE() if this is not a for iterator";
output InstTypes.SplicedExpData outSplicedExpData;
output Env.Env outClassEnv "only used for package constants";
output Env.Env outComponentEnv "only used for package constants";
output String name "so the FQ path can be constructed";
algorithm
(outCache,outAttributes,outType,outBinding,constOfForIteratorRange,outSplicedExpData,outClassEnv,outComponentEnv,name) :=
matchcontinue (inCache,inEnv,inComponentRef)
local
DAE.Attributes attr;
DAE.Type ty;
DAE.Binding binding;
list<Env.Frame> env, componentEnv, classEnv;
DAE.ComponentRef cref;
Env.Cache cache;
InstTypes.SplicedExpData splicedExpData;
Option<DAE.Const> cnstForRange;
/*
case (cache,env,cref)
equation
true = Flags.isSet(Flags.LOOKUP);
Debug.traceln("lookupVar: " +&
ComponentReference.printComponentRefStr(cref) +&
" in env: " +&
Env.printEnvPathStr(env) +& "\n");
then
fail();
*/
/*/ strip fully qualified crefs
case (cache,env,cref)