-
Notifications
You must be signed in to change notification settings - Fork 297
/
Lookup.mo
2060 lines (1892 loc) · 74.3 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
package Lookup "
This file is part of OpenModelica.
Copyright (c) 1998-2005, Linköpings universitet, Department of
Computer and Information Science, PELAB
All rights reserved.
(The new BSD license, see also
http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of Linköpings universitet nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
file: Lookup.rml
module: 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:
lookup_class - to find a class
lookup_type - to find types (e.g. functions, types, etc.)
lookup_var - to find a variable in the instance hierarchy.
"
public import OpenModelica.Compiler.ClassInf;
public import OpenModelica.Compiler.Types;
public import OpenModelica.Compiler.Absyn;
public import OpenModelica.Compiler.Exp;
public import OpenModelica.Compiler.Env;
public import OpenModelica.Compiler.SCode;
protected import OpenModelica.Compiler.Debug;
protected import OpenModelica.Compiler.Inst;
protected import OpenModelica.Compiler.Mod;
protected import OpenModelica.Compiler.Prefix;
protected import OpenModelica.Compiler.Builtin;
protected import OpenModelica.Compiler.ModUtil;
protected import OpenModelica.Compiler.Static;
protected import OpenModelica.Compiler.Connect;
protected import OpenModelica.Compiler.Error;
/* - 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.
Arg1: Env.Env is the environment which to perform the lookup in
Arg2: Absyn.Path is the type to look for
"
input Env.Cache inCache;
input Env.Env inEnv "environment to search in";
input Absyn.Path inPath "type to look for";
input Boolean inBoolean "Messaage flag, true outputs lookup error messages";
output Env.Cache outCache;
output Types.Type outType "the found type";
output Env.Env outEnv "The environment the type was found in";
algorithm
(outCache,outType,outEnv):=
matchcontinue (inCache,inEnv,inPath,inBoolean)
local
tuple<Types.TType, Option<Absyn.Path>> t,c_1;
list<Env.Frame> env_1,env,env_2,env3,env2,env_3;
Absyn.Path path,p;
Boolean msg,encflag;
SCode.Class c;
String id,pack,classname,scope;
SCode.Restriction restr;
ClassInf.State ci_state,cistate1;
Env.Cache cache;
/*For simple names */
case (cache,env,(path as Absyn.IDENT(name = _)),msg/* message flag*/)
equation
(cache,t,env_1) = lookupTypeInEnv(cache,env, path);
then
(cache,t,env_1);
/* Classes that are external objects. Implicityly instantiate to get type */
case (cache,env,path ,msg) local String ident,s;
equation
(cache,c ,env_1) = lookupClass(cache,env, path, false);
true = Inst.classIsExternalObject(c);
(cache,_,_::env_1,_,_,_) = Inst.instClass(cache,env_1, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c,
{}, false, Inst.TOP_CALL());
ident = Absyn.pathLastIdent(path); /* Once class has instantiated we only need to look up the last
part of the name as a type */
(cache,t,env_2) = lookupTypeInEnv(cache,env_1, Absyn.IDENT(ident));
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,path,msg)
local String s,ident;
SCode.Restriction restr;
equation
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,env, path, false);
true = SCode.isFunctionOrExtFunction(restr);
(cache,env_2) = Inst.implicitFunctionTypeInstantiation(cache,env_1, c);
ident = Absyn.pathLastIdent(path);
(cache,t,env3) = lookupTypeInEnv(cache,env_2, Absyn.IDENT(ident));
then
(cache,t,env3);
/* Error for type not found */
case (cache,env,path,true)
equation
classname = Absyn.pathString(path);
classname = stringAppend(classname," (its type) ");
scope = Env.printEnvPathStr(env);
Error.addMessage(Error.LOOKUP_ERROR, {classname,scope});
then
fail();
end matchcontinue;
end lookupType;
protected function isPrimitive "function: isPrimitive
author: PA
Returns true if classname is any of the builtin classes:
Real, Integer, String, Boolean
"
input Absyn.Path inPath;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "Integer")) then true;
case (Absyn.IDENT(name = "Real")) then true;
case (Absyn.IDENT(name = "Boolean")) then true;
case (Absyn.IDENT(name = "String")) then true;
case (_) then false;
end matchcontinue;
end isPrimitive;
public function lookupClass "function: lookupClass
Tries to find a specified class in an environment
Arg1: The enviroment where to look
Arg2: The path for the class
Arg3: A Bool to control the output of error-messages. If it is true
then it outputs a error message if the class is not found.
"
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path inPath;
input Boolean inBoolean;
output Env.Cache outCache;
output SCode.Class outClass;
output Env.Env outEnv;
algorithm
(inCache,outClass,outEnv):=
matchcontinue (inCache,inEnv,inPath,inBoolean)
local
Env.Frame f;
Env.Cache cache;
SCode.Class c,c_1;
list<Env.Frame> env,env_1,env2,env_2,env_3,env1,env4,env5,fs;
Absyn.Path path,ep,packp,p,scope;
String id,s,name,pack;
Boolean msg,encflag,msgflag;
SCode.Restriction restr;
ClassInf.State ci_state,cistate1;
/* First look in cache for environment. If found look up class in that environment*/
case (cache,env,path,msg)
equation
SOME(scope) = Env.getEnvPath(env);
f::fs = Env.cacheGet(scope,path,cache);
id = Absyn.pathLastIdent(path);
(cache,c,env) = lookupClassInEnv(cache,fs,Absyn.IDENT(id),msg);
then
(cache,c,env);
/* Builtin classes Integer, Real, String, Boolean can not be overridden
search top environment directly. */
case (cache,env,(path as Absyn.IDENT(name = id)),msg)
equation
true = isPrimitive(path);
f = Env.topFrame(env);
(cache,c,env) = lookupClassInFrame(cache,f, {f}, id, msg);
then
(cache,c,env);
case (cache,env,path,msg)
equation
true = isPrimitive(path);
print("ERROR, primitive class not found on top env: ");
s = Env.printEnvStr(env);
print(s);
then
fail();
case (cache,env,(path as Absyn.IDENT(name = name)),msgflag)
equation
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClassInEnv(cache,env, path, msgflag) "print \"lookup_class \" & print name & print \"\\nenv:\" & Env.print_env_str env => s & print s & print \"\\n\" &" ;
//print("found class ");print(name);print("\n");
then
(cache,c,env_1);
case (cache,env,(p as Absyn.QUALIFIED(name = _)),msgflag)
equation
SOME(ep) = Env.getEnvPath(env) "If we search for A1.A2....An.x while in scope A1.A2...An
, just search for x. Must do like this to ensure finite recursion" ;
packp = Absyn.stripLast(p);
true = ModUtil.pathEqual(ep, packp);
id = Absyn.pathLastIdent(p);
(cache,c,env_1) = lookupClass(cache,env, Absyn.IDENT(id), msgflag);
then
(cache,c,env_1);
case (cache,env,(p as Absyn.QUALIFIED(name = pack,path = path)),msgflag) /* Qualified name in non package */
equation
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,env, Absyn.IDENT(pack), msgflag);
env2 = Env.openScope(env_1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
(cache,_,env_2,_,cistate1,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false/*FIXME:prot*/, {}, false);
failure(ClassInf.valid(cistate1, SCode.R_PACKAGE()));
(cache,c_1,env_3) = lookupClass(cache,env_2, path, msgflag) "Has to do additional check for encapsulated classes, see rule below" ;
then
(cache,c_1,env_3);
case (cache,env,(p as Absyn.QUALIFIED(name = pack,path = path)),msgflag) /* Qualified names in package */
equation
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env1) = lookupClass(cache,env, Absyn.IDENT(pack), msgflag);
env2 = Env.openScope(env1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
(cache,env4,cistate1) = Inst.partialInstClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {});
/*(cache,_,env4,_,cistate1,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false/*FIXME:prot*/, {}, false);*/
ClassInf.valid(cistate1, SCode.R_PACKAGE());
(cache,c_1,env5) = lookupClass(cache,env4, path, msgflag) "Has NOT to do additional check for encapsulated classes, see rule above" ;
then
(cache,c_1,env5);
case (cache,env,path,true)
equation
s = Absyn.pathString(path) ;
Debug.fprint("failtrace", "- lookup_class failed\n - looked for ") ;
//print("lookup class ");print(s);print("failed\n");
Debug.fprint("failtrace", s);
Debug.fprint("failtrace", "\n env:");
s = Env.printEnvStr(env);
//print("env:");print(s);
//print("Cache:");print(Env.printCacheStr(cache));print("\n");
Debug.fprint("failtrace", s);
Debug.fprint("failtrace", "\n");
then
fail();
case (cache,env,path,false)
equation
//print("lookupClass(");print(Absyn.pathString(path));print(",msg=false failed, env:");
//print(Env.printEnvStr(env));print("\n");
then
fail();
end matchcontinue;
end lookupClass;
protected function lookupQualifiedImportedVarInFrame "function: lookupQualifiedImportedVarInFrame
author: PA
Looking up variables (constants) imported using qualified imports,
i.e. import Modelica.Constants.PI;
"
input Env.Cache inCache;
input list<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Env.Env outEnv;
output Types.Attributes outAttributes;
output Types.Type outType;
output Types.Binding outBinding;
algorithm
(outCache,outEnv,outAttributes,outType,outBinding):=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr;
Types.Attributes attr;
tuple<Types.TType, Option<Absyn.Path>> ty;
Types.Binding bind;
String id,ident,str;
list<Env.Item> fs;
list<Env.Frame> env,p_env;
Exp.ComponentRef cref;
Absyn.Path strippath,path;
SCode.Class c2;
Env.Cache cache;
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = Absyn.IDENT(name = id))) :: fs),env,ident) /* For imported simple name, e.g. A, not possible to assert
sub-path package */
equation
equality(id = ident);
fr = Env.topFrame(env);
(cache,attr,ty,bind) = lookupVar(cache,{fr}, Exp.CREF_IDENT(ident,{}));
then
(cache,{fr},attr,ty,bind);
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = path)) :: fs),env,ident) /* For imported qualified name, e.g. A.B.C, assert A.B is package */
equation
id = Absyn.pathLastIdent(path);
equality(id = ident);
fr = Env.topFrame(env);
cref = Exp.pathToCref(path);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{fr}, cref);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
assertPackage(c2);
then
(cache,p_env,attr,ty,bind);
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = path)) :: fs),env,ident) /* importing qualified name, If not package, error */
equation
id = Absyn.pathLastIdent(path);
equality(id = ident);
fr = Env.topFrame(env);
cref = Exp.pathToCref(path);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{fr}, cref);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
failure(assertPackage(c2));
str = Absyn.pathString(strippath);
Error.addMessage(Error.IMPORT_PACKAGES_ONLY, {str});
then
fail();
case (cache,(Env.IMPORT(import_ = Absyn.NAMED_IMPORT(name = id,path = path)) :: fs),env,ident) /* Named imports */
equation
equality(id = ident);
fr = Env.topFrame(env);
cref = Exp.pathToCref(path);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{fr}, cref);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
assertPackage(c2);
then
(cache,p_env,attr,ty,bind);
case (cache,(Env.IMPORT(import_ = Absyn.NAMED_IMPORT(name = id,path = path)) :: fs),env,ident) /* Assert package for Named imports */
equation
equality(id = ident);
fr = Env.topFrame(env);
cref = Exp.pathToCref(path);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{fr}, cref);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
failure(assertPackage(c2));
str = Absyn.pathString(strippath);
Error.addMessage(Error.IMPORT_PACKAGES_ONLY, {str});
then
fail();
case (cache,(_ :: fs),env,ident) /* Check next frame. */
equation
(cache,p_env,attr,ty,bind) = lookupQualifiedImportedVarInFrame(cache,fs, env, ident);
then
(cache,p_env,attr,ty,bind);
end matchcontinue;
end lookupQualifiedImportedVarInFrame;
protected function moreLookupUnqualifiedImportedVarInFrame "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<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Boolean outBoolean;
algorithm
(outCache,outBoolean) :=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr,f;
SCode.Class c;
String id,ident;
Boolean encflag,res;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,env;
ClassInf.State ci_state;
Absyn.Path path;
list<Env.Item> fs;
Env.Cache cache;
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident)
local
Exp.ComponentRef cr;
Absyn.Path path,scope;
Absyn.Ident firstIdent;
equation
firstIdent = Absyn.pathFirstIdent(path);
f::_ = Env.cacheGet(Absyn.IDENT(firstIdent),path,cache);
(cache,_,_,_,_) = lookupVarInPackages(cache,{f}, Exp.CREF_IDENT(ident,{}));
then
(cache,true);
// if not found in cache , try to instantiate
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident)
equation
fr = Env.topFrame(env);
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,{fr}, path, false);
env2 = Env.openScope(env_1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
/*(cache,_,f::_,_,_,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false/*FIXME:prot*/, {}, false);*/
(cache,(f :: _),_) = Inst.partialInstClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {});
(cache,_,_,_,_) = lookupVarInPackages(cache,{f}, Exp.CREF_IDENT(ident,{}));
then
(cache,true);
case (cache,(_ :: fs),env,ident)
equation
(cache,res) = moreLookupUnqualifiedImportedVarInFrame(cache,fs, env, ident);
then
(cache,res);
case (cache,{},_,_) then (cache,false);
end matchcontinue;
end moreLookupUnqualifiedImportedVarInFrame;
protected function lookupUnqualifiedImportedVarInFrame "function: lookupUnqualifiedImportedVarInFrame
Find a variable from an unqualified import locally in a frame
"
input Env.Cache inCache;
input list<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Env.Env outEnv;
output Types.Attributes outAttributes;
output Types.Type outType;
output Types.Binding outBinding;
output Boolean outBoolean;
algorithm
(outCache,outEnv,outAttributes,outType,outBinding,outBoolean):=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr,f;
Exp.ComponentRef cref;
SCode.Class c;
String id,ident;
Boolean encflag,more,unique;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,env,p_env;
ClassInf.State ci_state;
Types.Attributes attr;
tuple<Types.TType, Option<Absyn.Path>> ty;
Types.Binding bind;
Absyn.Path path;
list<Env.Item> fs;
Env.Cache cache;
// First look in cache
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident) /* unique */
local
Exp.ComponentRef cr;
Absyn.Path path,scope;
Absyn.Ident firstIdent;
equation
//print("look in cache\n");
firstIdent = Absyn.pathFirstIdent(path);
f::_ = Env.cacheGet(Absyn.IDENT(firstIdent),path,cache);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{f}, Exp.CREF_IDENT(ident,{}));
(cache,more) = moreLookupUnqualifiedImportedVarInFrame(cache,fs, env, ident);
unique = boolNot(more);
then
(cache,p_env,attr,ty,bind,unique);
// if not in cache, try to instantiate
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident) /* unique */
equation
fr = Env.topFrame(env);
cref = Exp.pathToCref(path);
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,{fr}, path, false);
env2 = Env.openScope(env_1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
(cache,_,(f :: _),_,_,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {}, false);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,{f}, Exp.CREF_IDENT(ident,{}));
(cache,more) = moreLookupUnqualifiedImportedVarInFrame(cache,fs, env, ident);
unique = boolNot(more);
then
(cache,p_env,attr,ty,bind,unique);
case (cache,(_ :: fs),env,ident)
equation
(cache,p_env,attr,ty,bind,unique) = lookupUnqualifiedImportedVarInFrame(cache,fs, env, ident);
then
(cache,p_env,attr,ty,bind,unique);
end matchcontinue;
end lookupUnqualifiedImportedVarInFrame;
protected function lookupQualifiedImportedClassInFrame "function: lookupQualifiedImportedClassInFrame
Helper function to lookup_qualified_imported_class_in_env.
"
input Env.Cache inCache;
input list<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output SCode.Class outClass;
output Env.Env outEnv;
algorithm
(outCache,outClass,outEnv):=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr;
SCode.Class c,c2;
list<Env.Frame> env_1,env;
String id,ident,str;
list<Env.Item> fs;
Absyn.Path strippath,path;
Env.Cache cache;
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = Absyn.IDENT(name = id))) :: fs),env,ident)
equation
equality(id = ident) "For imported paths A, not possible to assert sub-path package" ;
fr = Env.topFrame(env);
(cache,c,env_1) = lookupClass(cache,{fr}, Absyn.IDENT(id), true);
then
(cache,c,env_1);
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = path)) :: fs),env,ident)
equation
id = Absyn.pathLastIdent(path) "For imported path A.B.C, assert A.B is package" ;
equality(id = ident);
fr = Env.topFrame(env);
(cache,c,env_1) = lookupClass(cache,{fr}, path, true);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
assertPackage(c2);
then
(cache,c,env_1);
case (cache,(Env.IMPORT(import_ = Absyn.QUAL_IMPORT(path = path)) :: fs),env,ident)
equation
id = Absyn.pathLastIdent(path) "If not package, error" ;
equality(id = ident);
fr = Env.topFrame(env);
(cache,c,env_1) = lookupClass(cache,{fr}, path, true);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
failure(assertPackage(c2));
str = Absyn.pathString(strippath);
Error.addMessage(Error.IMPORT_PACKAGES_ONLY, {str});
then
fail();
case (cache,(Env.IMPORT(import_ = Absyn.NAMED_IMPORT(name = id,path = path)) :: fs),env,ident)
equation
equality(id = ident) "Named imports" ;
fr = Env.topFrame(env);
(cache,c,env_1) = lookupClass(cache,{fr}, path, true) " Print.print_buf \"NAMED IMPORT, top frame:\" &
Env.print_env {fr} &" ;
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
assertPackage(c2);
then
(cache,c,env_1);
case (cache,(Env.IMPORT(import_ = Absyn.NAMED_IMPORT(name = id,path = path)) :: fs),env,ident)
equation
equality(id = ident) "Assert package for Named imports" ;
fr = Env.topFrame(env);
(cache,c,env_1) = lookupClass(cache,{fr}, path, true);
strippath = Absyn.stripLast(path);
(cache,c2,_) = lookupClass(cache,{fr}, strippath, true);
failure(assertPackage(c2));
str = Absyn.pathString(strippath);
Error.addMessage(Error.IMPORT_PACKAGES_ONLY, {str});
then
fail();
case (cache,(_ :: fs),env,ident)
equation
(cache,c,env_1) = lookupQualifiedImportedClassInFrame(cache,fs, env, ident);
then
(cache,c,env_1);
end matchcontinue;
end lookupQualifiedImportedClassInFrame;
protected function moreLookupUnqualifiedImportedClassInFrame "function: moreLookupUnqualifiedImportedClassInFrame
Helper function for lookup_unqualified_imported_class_in_frame
"
input Env.Cache inCache;
input list<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output Boolean outBoolean;
algorithm
(outCache,outBoolean) :=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr,f;
SCode.Class c;
String id,ident;
Boolean encflag,res;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,env;
ClassInf.State ci_state;
Absyn.Path path;
Absyn.Ident firstIdent;
list<Env.Item> fs;
Env.Cache cache;
// Look in cache
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident)
equation
firstIdent = Absyn.pathFirstIdent(path);
f::_= Env.cacheGet(Absyn.IDENT(firstIdent),path,cache);
(cache,_,_) = lookupClass(cache,{f}, Absyn.IDENT(ident), false);
then
(cache,true);
// Not found, instantiate
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident)
equation
fr = Env.topFrame(env);
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,{fr}, path, false);
env2 = Env.openScope(env_1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
/* (cache,_,f::_,_,_,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false/*FIXME:prot*/, {}, false); */
(cache,(f :: _),_) = Inst.partialInstClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {});
(cache,_,_) = lookupClass(cache,{f}, Absyn.IDENT(ident), false);
then
(cache,true);
case (cache,(_ :: fs),env,ident)
equation
(cache,res) = moreLookupUnqualifiedImportedClassInFrame(cache,fs, env, ident);
then
(cache,res);
case (cache,{},_,_) then (cache,false);
end matchcontinue;
end moreLookupUnqualifiedImportedClassInFrame;
protected function lookupUnqualifiedImportedClassInFrame "function: lookupUnqualifiedImportedClassInFrame
Finds a class from an unqualified import locally in a frame
"
input Env.Cache inCache;
input list<Env.Item> inEnvItemLst;
input Env.Env inEnv;
input SCode.Ident inIdent;
output Env.Cache outCache;
output SCode.Class outClass;
output Env.Env outEnv;
output Boolean outBoolean;
algorithm
(outCache,outClass,outEnv,outBoolean):=
matchcontinue (inCache,inEnvItemLst,inEnv,inIdent)
local
Env.Frame fr,f,f_1;
SCode.Class c,c_1;
String id,ident;
Boolean encflag,more,unique;
SCode.Restriction restr;
list<Env.Frame> env_1,env2,fs_1,env;
ClassInf.State ci_state,cistate1;
Absyn.Path path;
list<Env.Item> fs;
Env.Cache cache;
Absyn.Ident firstIdent;
// Look in cache
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident) /* unique */
equation
firstIdent = Absyn.pathFirstIdent(path);
f::fs_1 = Env.cacheGet(Absyn.IDENT(firstIdent),path,cache);
(cache,c_1,(f_1 :: _)) = lookupClass(cache,{f}, Absyn.IDENT(ident), false) "Restrict import to the imported scope only, not its parents..." ;
(cache,more) = moreLookupUnqualifiedImportedClassInFrame(cache,fs, env, ident);
unique = boolNot(more);
then
(cache,c_1,(f_1 :: fs_1),unique);
// Not in cache, instantiate.
case (cache,(Env.IMPORT(import_ = Absyn.UNQUAL_IMPORT(path = path)) :: fs),env,ident) /* unique */
equation
fr = Env.topFrame(env);
(cache,(c as SCode.CLASS(id,_,encflag,restr,_)),env_1) = lookupClass(cache,{fr}, path, false);
env2 = Env.openScope(env_1, encflag, SOME(id));
ci_state = ClassInf.start(restr, id);
/*(cache,_,f::fs_1,_,_,_,_) = Inst.instClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false/*FIXME:prot*/, {}, false);*/
(cache,(f :: fs_1),cistate1) = Inst.partialInstClassIn(cache,env2, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {});
(cache,c_1,(f_1 :: _)) = lookupClass(cache,{f}, Absyn.IDENT(ident), false) "Restrict import to the imported scope only, not its parents..." ;
(cache,more) = moreLookupUnqualifiedImportedClassInFrame(cache,fs, env, ident);
unique = boolNot(more);
then
(cache,c_1,(f_1 :: fs_1),unique);
case (cache,(_ :: fs),env,ident)
equation
(cache,c,env_1,unique) = lookupUnqualifiedImportedClassInFrame(cache,fs, env, ident);
then
(cache,c,env_1,unique);
end matchcontinue;
end lookupUnqualifiedImportedClassInFrame;
public function lookupRecordConstructorClass "function: lookupRecordConstructorClass
Searches for a record constructor implicitly
defined by a record class.
"
input Env.Env inEnv;
input Absyn.Path inPath;
output SCode.Class outClass;
output Env.Env outEnv;
algorithm
(outClass,outEnv):=
matchcontinue (inEnv,inPath)
local
SCode.Class c;
list<Env.Frame> env_1,env;
Absyn.Path path;
case (env,path)
equation
(c,env_1) = lookupRecconstInEnv(env, path);
then
(c,env_1);
end matchcontinue;
end lookupRecordConstructorClass;
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.rml). Is this correct?
lookup_var is modified to implement 2. Is this correct?
old lookup_var is changed to lookup_var_internal and a new lookup_var
is written, that first tests the old lookup_var, 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 Exp.ComponentRef inComponentRef;
output Env.Cache outCache;
output Types.Attributes outAttributes;
output Types.Type outType;
output Types.Binding outBinding;
algorithm
(outCache,outAttributes,outType,outBinding):=
matchcontinue (inCache,inEnv,inComponentRef)
local
Types.Attributes attr;
tuple<Types.TType, Option<Absyn.Path>> ty;
Types.Binding binding;
list<Env.Frame> env,p_env;
Exp.ComponentRef cref;
Env.Cache cache;
case (cache,env,cref) /* try the old lookup_var */
equation
(cache,attr,ty,binding) = lookupVarInternal(cache,env, cref);
then
(cache,attr,ty,binding);
case (cache,env,cref) /* then look in classes (implicitly instantiated packages) */
equation
(cache,p_env,attr,ty,binding) = lookupVarInPackages(cache,env, cref);
checkPackageVariableConstant(p_env,attr,ty,cref);
then
(cache,attr,ty,binding);
case (_,env,cref) equation
/* Debug.fprint(\"failtrace\", \"- lookup_var failed\\n\") */ then fail();
end matchcontinue;
end lookupVar;
protected function checkPackageVariableConstant "
Variables in packages must be constant. This function produces an error message and fails
if variable is not constant."
input Env.Env env;
input Types.Attributes attr;
input Types.Type tp;
input Exp.ComponentRef cref;
algorithm
_ := matchcontinue(env,attr,tp,cref)
local Absyn.Path path;
case (env, Types.ATTR(parameter_= SCode.CONST()),_,cref)
then ();
case (env,attr,tp,cref) local String s1,s2;
equation
s1=Exp.printComponentRefStr(cref);
s2 = Env.printEnvPathStr(env);
Error.addMessage(Error.PACKAGE_VARIABLE_NOT_CONSTANT,{s1,s2});
then fail();
end matchcontinue;
end checkPackageVariableConstant;
protected function lookupVarInternal "function: lookupVarInternal
Helper function to lookup_var. Searches the frames for variables.
"
input Env.Cache inCache;
input Env.Env inEnv;
input Exp.ComponentRef inComponentRef;
output Env.Cache outCache;
output Types.Attributes outAttributes;
output Types.Type outType;
output Types.Binding outBinding;
algorithm
(outCache,outAttributes,outType,outBinding):=
matchcontinue (inCache,inEnv,inComponentRef)
local
Types.Attributes attr;
tuple<Types.TType, Option<Absyn.Path>> ty;
Types.Binding binding;
Option<String> sid;
Env.BinTree ht;
list<Env.Item> imps;
list<Env.Frame> fs;
Env.Frame frame;
Exp.ComponentRef ref;
Env.Cache cache;
case (cache,((frame as Env.FRAME(class_1 = sid,list_2 = ht,list_4 = imps)) :: fs),ref)
equation
//print("lookup var in frame:");print(Env.printEnvStr({frame}));print("end lookupVar frame\n");
(cache,attr,ty,binding) = lookupVarF(cache,ht, ref);
then
(cache,attr,ty,binding);
case (cache,(_ :: fs),ref)
equation
(cache,attr,ty,binding) = lookupVarInternal(cache,fs, ref);
then
(cache,attr,ty,binding);
end matchcontinue;
end lookupVarInternal;
protected function lookupVarInPackages "function: lookupVarInPackages
This function is called when a lookup of a variable with qualified names
does not have the first element as a component, e.g. A.B.C is looked up
where A is not a component. This implies that A is a class, and this
class should be temporary instantiated, and the lookup should
be performed within that class. I.e. the function performs lookup of
variables in the class hierarchy.
Arg1: The environment to search in
Arg2: The variable to search for
"
input Env.Cache inCache;
input Env.Env inEnv;
input Exp.ComponentRef inComponentRef;
output Env.Cache outCache;
output Env.Env outEnv;
output Types.Attributes outAttributes;
output Types.Type outType;
output Types.Binding outBinding;
algorithm
(outCache,outEnv,outAttributes,outType,outBinding):=
matchcontinue (inCache,inEnv,inComponentRef)
local
SCode.Class c;
String n,id1,id;
Boolean encflag;
SCode.Restriction r;
list<Env.Frame> env2,env3,env5,env,fs,bcframes,p_env;
ClassInf.State ci_state;
list<Types.Var> types;
Types.Attributes attr;
tuple<Types.TType, Option<Absyn.Path>> ty;
Types.Binding bind;
Exp.ComponentRef id2,cref,cr;
list<Exp.Subscript> sb;
Option<String> sid;
list<Env.Item> items;
Env.Frame f;
Env.Cache cache;
// Lookup of enumeration variables
case (cache,env,Exp.CREF_QUAL(ident = id1,subscriptLst = {},componentRef = (id2 as Exp.CREF_IDENT(ident = _))))
equation
(cache,(c as SCode.CLASS(n,_,encflag,(r as SCode.R_ENUMERATION()),_)),env2)
= lookupClass(cache,env, Absyn.IDENT(id1), false) "Special case for looking up enumerations" ;
env3 = Env.openScope(env2, encflag, SOME(n));
ci_state = ClassInf.start(r, n);
(cache,_,env5,_,_,types,_) = Inst.instClassIn(cache,env3, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {}, false);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,env5, id2);
then
(cache,p_env,attr,ty,bind);
// lookup of constants on form A.B in packages. First look in cache.
case (cache,env,cr as Exp.CREF_QUAL(ident = id,subscriptLst = {},componentRef = cref)) /* First part of name is a class. */
local
Exp.ComponentRef cr;
Absyn.Path path,scope;
equation
SOME(scope) = Env.getEnvPath(env);
path = Exp.crefToPath(cr);
id = Absyn.pathLastIdent(path);
path = Absyn.stripLast(path);
f::fs = Env.cacheGet(scope,path,cache);
(cache,attr,ty,bind) = lookupVarLocal(cache,f::fs, Exp.CREF_IDENT(id,{}));
//print("found ");print(Exp.printComponentRefStr(cr));print(" in cache\n");
then
(cache,f::fs,attr,ty,bind);
/* If we search for A1.A2....An.x while in scope A1.A2...An, just search for x.
Must do like this to ensure finite recursion */
case (cache,env,cr as Exp.CREF_QUAL(ident = id,subscriptLst = {},componentRef = cref))
local Absyn.Path ep,p,packp;
equation
p = Exp.crefToPath(cr);
SOME(ep) = Env.getEnvPath(env);
packp = Absyn.stripLast(p);
true = ModUtil.pathEqual(ep, packp);
id = Absyn.pathLastIdent(p);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,env, Exp.CREF_IDENT(id,{}));
then
(cache,p_env,attr,ty,bind);
// lookup of constants on form A.B in packages. instantiate package and look inside.
case (cache,env,cr as Exp.CREF_QUAL(ident = id,subscriptLst = {},componentRef = cref)) /* First part of name is a class. */
equation
(cache,(c as SCode.CLASS(n,_,encflag,r,_)),env2) = lookupClass(cache,env, Absyn.IDENT(id), false);
env3 = Env.openScope(env2, encflag, SOME(n));
ci_state = ClassInf.start(r, n);
(cache,_,env5,_,_,types,_) = Inst.instClassIn(cache,env3, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {}, /*true*/false);
(cache,p_env,attr,ty,bind) = lookupVarInPackages(cache,env5, cref);
then
(cache,p_env,attr,ty,bind);
case (cache,env,(cr as Exp.CREF_IDENT(ident = id,subscriptLst = sb))) local String str;
equation
(cache,attr,ty,bind) = lookupVarLocal(cache,env, cr);
then
(cache,env,attr,ty,bind);
/* Search base classes */
case (cache,Env.FRAME(list_5 = (bcframes as (_ :: _)))::fs,cref)
equation
//print("searching base classes for ");print(Exp.printComponentRefStr(cref));print("\n");
(cache,attr,ty,bind) = lookupVar(cache,bcframes, cref);
//print("found in baseclass\n");
then
(cache,bcframes,attr,ty,bind);
/* Search among qualified imports, e.g. import A.B; or import D=A.B; */