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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
**
** $Id$
\* */
//todo check significance of JAVA flag.
package scalac.symtab;
import scalac.ApplicationError;
import scalac.Global;
import scalac.PhaseDescriptor;
import scalac.util.ArrayApply;
import scalac.util.Name;
import scalac.util.Names;
import scalac.util.NameTransformer;
import scalac.util.Position;
import scalac.util.Debug;
import scalac.symtab.classfile.*;
public abstract class Symbol implements Modifiers, Kinds {
/** An empty symbol array */
public static final Symbol[] EMPTY_ARRAY = new Symbol[0];
/** An empty array of symbol arrays */
public static final Symbol[][] EMPTY_ARRAY_ARRAY = new Symbol[0][];
/** The error symbol */
public static final ErrorSymbol ERROR = new ErrorSymbol();
/** The absent symbol */
public static final NoSymbol NONE = new NoSymbol();
// Fields -------------------------------------------------------------
/** The kind of the symbol */
public int kind;
/** The position of the symbol */
public int pos;
/** The name of the symbol */
public Name name;
/** The modifiers of the symbol */
public int flags;
/** The owner of the symbol */
private Symbol owner;
/** The infos of the symbol */
private TypeIntervalList infos = TypeIntervalList.EMPTY;
// Constructors -----------------------------------------------------------
/** Generic symbol constructor */
public Symbol(int kind, int pos, Name name, Symbol owner, int flags) {
assert (!isTerm() || !name.isTypeName()) && (!isType() || name.isTypeName());
this.kind = kind;
this.pos = pos;
this.name = name;
this.owner = owner;
this.flags = flags & ~(INITIALIZED | LOCKED); // safety first
}
/** Return a fresh symbol with the same fields as this one.
*/
public abstract Symbol cloneSymbol();
/** copy all fields to `sym'
*/
public void copyTo(Symbol sym) {
sym.kind = kind;
sym.pos = pos;
sym.name = name;
sym.flags = flags;
sym.owner = owner;
sym.infos = infos;
}
// Setters ---------------------------------------------------------------
/** Set the mangled name of this Symbol */
public Symbol setMangledName(Name name) {
throw new ApplicationError("illegal operation on " + getClass());
}
/** Set owner */
public Symbol setOwner(Symbol owner) {
this.owner = owner;
return this;
}
/** Set information, except if symbol is both initialized and locked.
*/
public Symbol setInfo(Type info) {
return setInfo(info, currentPhaseId());
}
public Symbol setInfo(Type info, int limit) {
assert !isConstructor()
|| info instanceof Type.LazyType
|| info == Type.ErrorType
|| info instanceof Type.MethodType
|| info instanceof Type.OverloadedType
|| info instanceof Type.PolyType &&
((Type.PolyType)info).result instanceof Type.MethodType
: "illegal type for " + this + ": " + info;
if ((flags & (INITIALIZED | LOCKED)) != (INITIALIZED | LOCKED)) {
if (infos == TypeIntervalList.EMPTY)
infos = new TypeIntervalList(TypeIntervalList.EMPTY);
infos.limit = limit;
infos.info = info;
}
return this;
}
/** Set type -- this is an alias for setInfo(Type info)
*/
public Symbol setType(Type info) { return setInfo(info); }
/** Set type of `this' in current class
*/
public Symbol setTypeOfThis(Type tp) {
throw new ApplicationError(this + ".setTypeOfThis");
}
public Symbol updateInfo(Type info) {
// Global.instance.currentPhase.setInfo(this, info);
assert infos.limit <= Global.instance.currentPhase.id + 1 : this;
if (infos.limit > Global.instance.currentPhase.id) infos.limit--;
infos = new TypeIntervalList(infos);
infos.limit = Global.instance.currentPhase.id + 1;
infos.info = info;
return this;
}
// Symbol classification ----------------------------------------------------
/** Does this symbol denote a type? */
public final boolean isType() {
return kind == TYPE || kind == CLASS || kind == ALIAS;
}
/** Does this symbol denote a term? */
public final boolean isTerm() {
return kind == VAL;
}
/** Does this symbol denote a value? */
public final boolean isValue() {
return kind == VAL && !(isModule() && isJava()) && !isPackage();
}
/** Does this symbol denote a stable value? */
public final boolean isStable() {
return kind == VAL &&
((flags & STABLE) != 0 ||
(flags & MUTABLE) == 0 && type().isObjectType()) &&
!owner.isPrimaryConstructor();
}
/** Does this symbol denote a variable? */
public final boolean isVariable() {
return kind == VAL && (flags & MUTABLE) != 0;
}
/** Does this symbol denote a method?
*/
public final boolean isInitializedMethod() {
if (infos.limit < 0) return false;
switch (rawInfo()) {
case MethodType(_, _):
case PolyType(_, _): return true;
default: return false;
}
}
public final boolean isMethod() {
initialize();
return isInitializedMethod();
}
/* Does this symbol denote an anonymous class? */
public final boolean isAnonymousClass() {
return kind == CLASS &&
(name == Names.EMPTY.toTypeName() ||
name == Names.ANON_CLASS_NAME.toTypeName());
}
/** Does this symbol denote the root class or root module?
*/
public final boolean isRoot() {
return this.moduleClass() == Global.instance.definitions.ROOT_CLASS;
}
/** Does this symbol denote something loaded from a Java class? */
public final boolean isJava() {
return (flags & JAVA) != 0;
}
/** Does this symbol denote a Java package? */
public final boolean isPackage() {
return (flags & PACKAGE) != 0;
}
/** Does this symbol denote a module? */
public final boolean isModule() {
return kind == VAL && (flags & MODUL) != 0;
}
/** Does this symbol denote a module? */
public final boolean isModuleClass() {
return kind == CLASS && (flags & MODUL) != 0;
}
/** Does this symbol denote a module? */
public final boolean isClass() {
return kind == CLASS;
}
/** Does this symbol denote a case class?
*/
public final boolean isCaseClass() {
return kind == CLASS && (flags & CASE) != 0;
}
/** Does this symbol denote a uniform (i.e. parameterless) class? */
public final boolean isTrait() {
return kind == CLASS && (flags & TRAIT) != 0;
}
/** Does this class symbol denote a compound type symbol?
*/
public final boolean isCompoundSym() {
return name == Names.COMPOUND_NAME.toTypeName();
}
/** Does this symbol denote an interface? */
public final boolean isInterface() {
return (flags & INTERFACE) != 0;
}
/** Does this symbol denote a public symbol? */
public final boolean isPublic() {
return !isProtected() && !isPrivate();
}
/** Does this symbol denote a protected symbol? */
public final boolean isProtected() {
return (flags & PROTECTED) != 0;
}
/** Does this symbol denote a private symbol? */
public final boolean isPrivate() {
return (flags & PRIVATE) != 0;
}
/** Does this symbol denote a synthetic symbol? */
public final boolean isSynthetic() {
return (flags & SYNTHETIC) != 0;
}
/** Does this symbol denote a static member? */
public final boolean isStatic() {
return (flags & STATIC) != 0;
}
/** Does this symbol denote an accessor? */
public final boolean isAccessor() {
return (flags & ACCESSOR) != 0;
}
/** Is this symbol locally defined? I.e. not a member of a class or module */
public final boolean isLocal() {
return owner.kind == VAL && !owner.isPrimaryConstructor();
}
/** Is this symbol a parameter? Includes type parameters of methods.
*/
public final boolean isParameter() {
return (flags & PARAM) != 0;
}
/** Is this symbol a def parameter?
*/
public final boolean isDefParameter() {
return (flags & (PARAM | DEF)) == (PARAM | DEF);
}
/** Is this class locally defined?
* A class is local, if
* - it is anonymous, or
* - its owner is a value
* - it is defined within a local class
*/
public final boolean isLocalClass() {
return kind == CLASS &&
!isPackage() &&
(name == Names.EMPTY.toTypeName() ||
owner.isValue() ||
owner.isLocalClass());
}
/** Is this symbol a constructor? */
public final boolean isConstructor() {
return name.isConstrName();
}
/** Is this symbol the primary constructor of a type? */
public final boolean isPrimaryConstructor() {
return isConstructor() && this == primaryConstructorClass().constructor();
}
public boolean isGenerated() {
return name.pos((byte)'$') < name.length();
}
/** Symbol was preloaded from package
*/
public boolean isPreloaded() {
return owner.isPackage() && pos == Position.NOPOS;
}
/** Is this symbol an overloaded symbol? */
public boolean isOverloaded() {
switch (info()) {
case OverloadedType(_,_): return true;
default : return false;
}
}
// Symbol names ----------------------------------------------------------------
/** Get the fully qualified name of this Symbol
* (this is always a normal name, never a type name)
*/
public Name fullName() {
return name.toTermName();
}
/** Get the mangled name of this Symbol
* (this is always a normal name, never a type name)
*/
public Name mangledName() {
return name.toTermName();
}
/** Get the fully qualified mangled name of this Symbol */
public Name mangledFullName() {
return fullName().replace((byte)'.', (byte)'$');
}
// Acess to related symbols -----------------------------------------------------
/** Get type parameters */
public Symbol[] typeParams() {
return EMPTY_ARRAY;
}
/** Get primary constructor of class */
public Symbol constructor() {
return NONE;
}
/** Get module associated with class */
public Symbol module() {
return NONE;
}
/** Get owner */
public Symbol owner() {
return owner;
}
/** Get owner, but if owner is primary constructor of a class,
* get class symbol instead. This is useful for type parameters
* and value parameters in classes which have the primary constructor
* as owner.
*/
public Symbol classOwner() {
Symbol owner = owner();
Symbol clazz = owner.primaryConstructorClass();
if (clazz.constructor() == owner) return clazz;
else return owner;
}
/** The next enclosing class */
public Symbol enclClass() {
return owner().enclClass();
}
/** The top-level class enclosing `sym'
*/
Symbol enclToplevelClass() {
Symbol sym = this;
while (sym.kind == VAL ||
(sym.kind == CLASS && !sym.owner().isPackage())) {
sym = sym.owner();
}
return sym;
}
/* If this is a primary constructor, return the class it constructs.
* Otherwise return the symbol itself.
*/
public Symbol primaryConstructorClass() {
return this;
}
/* If this is a module, return its class.
* Otherwise return the symbol itself.
*/
public Symbol moduleClass() {
return this;
}
/** The symbol accessed by this accessor function.
*/
public Symbol accessed() {
assert (flags & ACCESSOR) != 0;
Name name1 = name;
if (name1.endsWith(Names._EQ))
name1 = name1.subName(0, name1.length() - Names._EQ.length());
return owner.info().lookup(Name.fromString(name1 + "$"));
}
/** The members of this class or module symbol
*/
public Scope members() {
return info().members();
}
/** Lookup symbol with given name; return Symbol.NONE if not found.
*/
public Symbol lookup(Name name) {
return info().lookup(name);
}
// Symbol types --------------------------------------------------------------
/** Was symbol's type updated during phase `id'?
*/
public boolean isUpdated(int id) {
return infos.limit >= id;
}
/** the current phase id, or the id after analysis, whichever is larger.
*/
int currentPhaseId() {
int id = Global.instance.currentPhase.id;
if (id > Global.instance.POST_ANALYZER_PHASE_ID)
id = Global.instance.POST_ANALYZER_PHASE_ID;
return id;
}
/** Is this symbol initialized? */
public final boolean isInitialized() {
return (flags & INITIALIZED) != 0;
}
/** Initialize the symbol */
public final Symbol initialize() {
info();
return this;
}
/** Get info; This is:
* for a term symbol, its type
* for a type variable, its bound
* for a type alias, its right-hand side
* for a class symbol, the compound type consisting of
* its baseclasses and members.
*/
public Type info() {
if ((flags & INITIALIZED) == 0) {
int id = currentPhaseId();
Type info = rawInfoAt(id);
assert info != null : this;
if ((flags & LOCKED) != 0) {
setInfo(Type.ErrorType);
flags |= INITIALIZED;
throw new CyclicReference(this, info);
}
flags |= LOCKED;
//System.out.println("completing " + this);//DEBUG
info.complete(this);
flags = flags & ~LOCKED;
if (info instanceof SourceCompleter && (flags & SNDTIME) == 0) {
flags |= SNDTIME;
return info();
} else {
assert !(rawInfoAt(id) instanceof Type.LazyType) : this;
flags |= INITIALIZED;
}
//System.out.println("done: " + this);//DEBUG
}
return rawInfoAt(Global.instance.currentPhase.id);
}
/** Get info at phase #id
*/
public Type infoAt(int id) {
info();
return rawInfoAt(id);
}
/** Get info at next phase
*/
public Type nextInfo() {
Global.instance.nextPhase();
Type info = info();
Global.instance.prevPhase();
return info;
}
/** get info at phase #id, without forcing lazy types.
*/
private Type rawInfoAt(int id) {
int nextid = infos.limit;
assert infos != TypeIntervalList.EMPTY : this;
if (nextid < id) {
PhaseDescriptor curphase = Global.instance.currentPhase;
do {
Global.instance.currentPhase = Global.instance.phases[nextid];
Type newInfo =
Global.instance.currentPhase.transformInfo(this, infos.info);
if (newInfo != infos.info) {
infos = new TypeIntervalList(infos);
infos.info = newInfo;
}
nextid++;
infos.limit = nextid;
} while (nextid < id);
Global.instance.currentPhase = curphase;
return infos.info;
} else {
TypeIntervalList infos1 = infos;
while (infos1.prev.limit >= id) {
infos1 = infos1.prev;
}
return infos1.info;
}
}
public Type rawInfo() {
return rawInfoAt(Global.instance.currentPhase.id);
}
/** The type of a symbol is:
* for a type symbol, the type corresponding to the symbol itself
* for a term symbol, its usual type
*/
public Type type() {
return info();
}
/** The type at phase #id
*/
public Type typeAt(int id) {
return infoAt(id);
}
/** The types of these symbols as an array.
*/
static public Type[] type(Symbol[] syms) {
Type[] tps = new Type[syms.length];
for (int i = 0; i < syms.length; i++)
tps[i] = syms[i].type();
return tps;
}
/** The type constructor of a symbol is:
* For a type symbol, the type corresponding to the symbol itself, excluding
* parameters.
* Not applicable for term symbols.
*/
public Type typeConstructor() {
throw new ApplicationError("typeConstructor inapplicable for " + this);
}
/** Get this.type corresponding to this symbol
*/
public Type thisType() {
return Type.localThisType;
}
/** Get type of `this' in current class.
*/
public Type typeOfThis() {
return type();
}
/** A total ordering between symbols that refines the class
* inheritance graph (i.e. subclass.isLess(superclass) always holds).
*/
public boolean isLess(Symbol that) {
if (this == that) return false;
int diff;
if (this.isType()) {
if (that.isType()) {
diff = this.closure().length - that.closure().length;
if (diff > 0) return true;
if (diff < 0) return false;
} else {
return true;
}
} else if (that.isType()) {
return false;
}
diff = that.mangledName().index - this.mangledName().index;
if (diff > 0) return true;
if (diff < 0) return false;
diff = that.mangledFullName().index - this.mangledFullName().index;
if (diff > 0) return true;
if (diff < 0) return false;
diff = that.hashCode() - this.hashCode();
if (diff > 0) return true;
if (diff < 0) return false;
if (owner().isLess(that.owner())) return true;
if (that.owner().isLess(owner())) return false;
throw new ApplicationError(
"Giving up: can't order two incarnations of class " +
this.mangledFullName());
}
/** Return the symbol's type itself followed by all its direct and indirect
* base types, sorted by isLess(). Overridden for class symbols.
*/
public Type[] closure() {
return info().closure();
}
/** Return position of `c' in the closure of this type; -1 if not there.
*/
public int closurePos(Symbol c) {
if (this == c) return 0;
if (c.isCompoundSym()) return -1;
Type[] closure = closure();
int lo = 0;
int hi = closure.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
Symbol clsym = closure[mid].symbol();
if (c == clsym) return mid;
else if (c.isLess(clsym)) hi = mid - 1;
else if (clsym.isLess(c)) lo = mid + 1;
else throw new ApplicationError();
}
return -1;
}
Type baseType(Symbol sym) {
int i = closurePos(sym);
if (i >= 0) return closure()[i];
else return Type.NoType;
}
/** Is this class a subclass of `c'? I.e. does it have a type instance
* of `c' as indirect base class?
*/
public boolean isSubClass(Symbol c) {
return this == c || c.kind == Kinds.ERROR || closurePos(c) >= 0;
}
/** Get base types of this symbol */
public Type[] parents() {
return type().parents();
}
// ToString -------------------------------------------------------------------
/** String representation of symbol's simple name.
* Translates expansions of operators back to operator symbol. E.g.
* $eq => =.
*/
public String nameString() {
return NameTransformer.decode(name).toString();
}
/** String representation of symbol's full name.
* Translates expansions of operators back to operator symbol. E.g.
* $eq => =.
*/
public String fullNameString() {
return NameTransformer.decode(fullName()).toString();
}
public String idString() {
if (Global.instance.uniqid &&
(kind == TYPE || Global.instance.debug))
return "#" + Global.instance.uniqueID.id(this);
else return "";
}
/** String representation, including symbol's kind
* e.g., "class Foo", "function Bar".
*/
public String toString() {
if (isRoot()) return "<root package>";
String kstr = kindString();
String str;
if (isAnonymousClass()) str = "<template>";
else if (kstr.length() == 0) str = fullNameString();
else str = kstr + " " + fullNameString();
return str + idString();
}
/** String representation of location.
*/
public String locationString() {
if (owner.kind == CLASS && !owner.isAnonymousClass())
return " in " + owner;
else
return "";
}
/** String representation of definition.
*/
public String defString() {
String inner;
if (kind == CLASS) inner = " extends ";
else if (kind == TYPE) inner = " <: ";
else if (kind == ALIAS) inner = " = ";
else inner = " : ";
return
(isParameter() ? "" : defKeyword() + " ") +
nameString() + idString() + inner +
(rawInfoAt(Global.instance.POST_ANALYZER_PHASE_ID)
instanceof Type.LazyType ? "?" : info());
}
public static String[] defString(Symbol[] defs) {
String[] strs = new String[defs.length];
for (int i = 0; i < defs.length; i++)
strs[i] = defs[i].defString();
return strs;
}
/** String representation of kind */
public String kindString() {
switch (kind) {
case CLASS:
if ((flags & TRAIT) != 0)
return "trait";
else if ((flags & MODUL) != 0 && Global.instance.debug)
return "object class";
else
return "class";
case TYPE:
case ALIAS:
return "type";
case VAL:
if (isVariable()) return "variable";
else if (isModule()) return "object";
else if (isConstructor()) return "constructor";
else if (isInitializedMethod() &&
(Global.instance.debug || (flags & STABLE) == 0) )
return "method";
else return "value";
default: return "";
}
}
/** Definition keyword of kind
*/
public String defKeyword() {
switch (kind) {
case CLASS: if ((flags & TRAIT) != 0) return "trait"; else return "class";
case TYPE:
case ALIAS: return "type";
case VAL:
if (isVariable()) return "var";
else if (isModule()) return "object";
else if (isInitializedMethod()) return "def";
else return "val";
default: return "";
}
}
// Overloading and Overriding -------------------------------------------
/** Add another overloaded alternative to this symbol.
*/
public Symbol overloadWith(Symbol that) {
assert isTerm() : Debug.show(this);
assert this.name == that.name : Debug.show(this) + " <> " + Debug.show(that);
assert this.owner == that.owner : Debug.show(this) + " != " + Debug.show(that);
assert (this.flags & that.flags & JAVA) != 0 ||
(this.flags & (SOURCEFLAGS | JAVA) & ~ACCESSFLAGS) ==
(that.flags & (SOURCEFLAGS | JAVA) & ~ACCESSFLAGS) : Integer.toHexString(this.flags) + "@" + Debug.show(this) + " <> " + Integer.toHexString(that.flags) + "@" + Debug.show(that);
TermSymbol overloaded = new TermSymbol(
pos, name, owner,
((this.flags | that.flags) & (SOURCEFLAGS | JAVA) & ~ACCESSFLAGS) |
(this.flags & that.flags & ACCESSFLAGS));
overloaded.setInfo(new LazyOverloadedType(this, that));
return overloaded;
}
/** A lazy type which, when forced computed the overloaded type
* of symbols `sym1' and `sym2'. It also checks that this type is well-formed.
*/
private static class LazyOverloadedType extends Type.LazyType {
Symbol sym1;
Symbol sym2;
LazyOverloadedType(Symbol sym1, Symbol sym2) {
this.sym1 = sym1;
this.sym2 = sym2;
}
private Symbol[] alts(Symbol sym) {
if (sym == null) return Symbol.EMPTY_ARRAY;
switch (sym.type()) {
case OverloadedType(Symbol[] alts, _): return alts;
default: return new Symbol[]{sym};
}
}
private Type[] alttypes(Symbol sym) {
if (sym == null) return Type.EMPTY_ARRAY;
switch (sym.type()) {
case OverloadedType(_, Type[] alttypes): return alttypes;
default: return new Type[]{sym.type()};
}
}
public void complete(Symbol overloaded) {
if (sym1 != null) sym1.initialize();
if (sym2 != null) sym2.initialize();
Symbol[] alts1 = alts(sym1);
Symbol[] alts2 = alts(sym2);
Symbol[] alts3 = new Symbol[alts1.length + alts2.length];
System.arraycopy(alts1, 0, alts3, 0, alts1.length);
System.arraycopy(alts2, 0, alts3, alts1.length, alts2.length);
Type[] alttypes1 = alttypes(sym1);
Type[] alttypes2 = alttypes(sym2);
Type[] alttypes3 = new Type[alttypes1.length + alttypes2.length];
System.arraycopy(alttypes1, 0, alttypes3, 0, alttypes1.length);
System.arraycopy(alttypes2, 0, alttypes3, alttypes1.length, alttypes2.length);
overloaded.setInfo(Type.OverloadedType(alts3, alttypes3));
}
}
/** All the alternatives of this symbol if it's overloaded, the
* symbol alone otherwise.
*/
public Symbol[] alternatives() {
switch (type()) {
case OverloadedType(Symbol[] alts, _): return alts;
default: return new Symbol[]{this};
}
}
/** The symbol which is overridden by this symbol in base class `base'
* `base' must be a superclass of this.owner().
*/
public Symbol overriddenSymbol(Type base) {
Symbol sym1 = base.lookupNonPrivate(name);
if (sym1.kind == Kinds.NONE || (sym1.flags & STATIC) != 0) {
return Symbol.NONE;
} else {
//System.out.println(this + ":" + this.type() + locationString() + " overrides? " + sym1 + sym1.type() + sym1.locationString()); //DEBUG
Type symtype = owner.thisType().memberType(this);
//todo: try whether we can do: this.type(); instead
Type sym1type = owner.thisType().memberType(sym1);
switch (sym1type) {
case OverloadedType(Symbol[] alts, Type[] alttypes):
for (int i = 0; i < alts.length; i++) {
if (symtype.isSameAs(alttypes[i])) return alts[i];
}
return Symbol.NONE;
default:
if (symtype.isSubType(sym1type)) return sym1;
else {
if (Global.instance.debug) System.out.println(this + locationString() + " does not override " + sym1 + sym1.locationString() + ", since " + symtype + " !<= " + sym1type);//DEBUG
return Symbol.NONE;
}
}
}
}
public void reset(Type completer) {
this.flags &= (FINAL | MODUL);
this.pos = 0;
this.infos = TypeIntervalList.EMPTY;
this.setInfo(completer);
}
}
/** A class for term symbols
*/
public class TermSymbol extends Symbol {
private Symbol clazz;
/** Constructor */
public TermSymbol(int pos, Name name, Symbol owner, int flags) {
super(VAL, pos, name, owner, flags);
}
public static TermSymbol newConstructor(Symbol clazz, int flags) {
TermSymbol sym = new TermSymbol(
clazz.pos, clazz.name.toConstrName(), clazz.owner(),
flags | FINAL);
sym.clazz = clazz;
return sym;
}
public static TermSymbol newJavaConstructor(Symbol clazz) {
return newConstructor(clazz, clazz.flags & (ACCESSFLAGS | QUALIFIED | JAVA));
}
public static TermSymbol newModule(int pos, Name name, Symbol owner, int flags) {
TermSymbol sym = new TermSymbol(pos, name, owner, flags | MODUL | FINAL);
Symbol clazz = new ClassSymbol(
pos, name.toTypeName(), owner, flags | MODUL | FINAL, sym);
clazz.constructor().setInfo(
Type.MethodType(Symbol.EMPTY_ARRAY, clazz.typeConstructor()));
sym.clazz = clazz;
sym.setInfo(clazz.typeConstructor());
return sym;
}
/** Constructor for companion modules to classes, which need to be completed.
*/
public static TermSymbol newCompanionModule(Symbol clazz, int flags, Type.LazyType parser) {
TermSymbol sym = newModule(Position.NOPOS, clazz.name.toTermName(), clazz.owner(),
flags);
sym.clazz.setInfo(parser);
return sym;
}
/** Java package module constructor
*/
public static TermSymbol newJavaPackageModule(Name name, Symbol owner, Type.LazyType parser) {
TermSymbol sym = newModule(Position.NOPOS, name, owner, JAVA | PACKAGE);
sym.clazz.flags |= SYNTHETIC;
sym.clazz.setInfo(parser);
return sym;
}
/** Get this.type corresponding to this class or module
*/
public Type thisType() {
if ((flags & MODUL) != 0) return moduleClass().thisType();
else return Type.localThisType;
}
/** Get the fully qualified name of this Symbol */
public Name fullName() {
if (clazz != null) return clazz.fullName();
else return super.fullName();
}
/** Return a fresh symbol with the same fields as this one.
*/
public Symbol cloneSymbol() {
assert !isPrimaryConstructor() : Debug.show(this);
TermSymbol other;
if (isModule()) {
other = newModule(pos, name, owner(), flags);
} else {
other = new TermSymbol(pos, name, owner(), flags);
other.clazz = clazz;
}
other.setInfo(info());
return other;
}
public Symbol[] typeParams() {
return type().typeParams();
}
public Symbol primaryConstructorClass() {
return isConstructor() && clazz != null ? clazz : this;
}
public Symbol moduleClass() {
return (flags & MODUL) != 0 ? clazz : this;
}
}
/** A class for (abstract and alias) type symbols. It has ClassSymbol as a subclass.
*/
public class TypeSymbol extends Symbol {
/** A cache for closures
*/
private ClosureIntervalList closures = ClosureIntervalList.EMPTY;
/** A cache for type constructors
*/
private Type tycon = null;
/** Constructor */
public TypeSymbol(int kind, int pos, Name name, Symbol owner, int flags) {
super(kind, pos, name, owner, flags);
}
/** Return a fresh symbol with the same fields as this one.
*/
public Symbol cloneSymbol() {
if (Global.instance.debug) System.out.println("cloning " + this + this.locationString() + " in phase " + Global.instance.currentPhase.name());
TypeSymbol other = new TypeSymbol(kind, pos, name, owner(), flags);
other.setInfo(info());
return other;
}
/** Get type constructor */
public Type typeConstructor() {
if (tycon == null)
tycon = Type.TypeRef(owner().thisType(), this, Type.EMPTY_ARRAY);
return tycon;
}
/** Get type */
public Type type() {
return typeConstructor();
}
/** Get type at phase id */
public Type typeAt(int id) {
return type();
}
public Type[] closure() {
if (kind == ALIAS) return info().symbol().closure();
int id = Global.instance.currentPhase.id;
if (closures.limit < id) {
if (id == 0 || changes(closureAt(id - 1))) {
closures = new ClosureIntervalList(closures);
closures.limit = id;
computeClosure();
} else {
closures.limit = id;
}
return closures.closure;
} else {
ClosureIntervalList closures1 = closures;
while (closures1.prev.limit >= id) {
closures1 = closures1.prev;
}
return closures1.closure;
}
}
//todo: needed?
private Type[] closureAt(int id) {
PhaseDescriptor savedPhase = Global.instance.currentPhase;
Global.instance.currentPhase = Global.instance.phases[id];
Type[] c = closure();
Global.instance.currentPhase = savedPhase;
return c;
}
private boolean changes(Type[] closure) {
for (int i = 0; i < closure.length; i++) {
Symbol c = closure[i].symbol();
if (c.infoAt(Global.instance.currentPhase.id - 1) != c.info())
return true;
}
return false;
}
private static Type[] BAD_CLOSURE = new Type[0];
/** Return the type itself followed by all direct and indirect
* base types of this type, sorted by isLess().
*/
private void computeClosure() {
assert closures.closure != BAD_CLOSURE : this;
closures.closure = BAD_CLOSURE; // to catch cycles.
SymSet closureClassSet = inclClosureBases(SymSet.EMPTY, this);
Symbol[] closureClasses = new Symbol[closureClassSet.size() + 1];
closureClasses[0] = this;
closureClassSet.copyToArray(closureClasses, 1);
//System.out.println(ArrayApply.toString(closureClasses));//DEBUG
closures.closure = Symbol.type(closureClasses);
//System.out.println(ArrayApply.toString(closures.closure));//DEBUG
adjustType(type());
//System.out.println("closure(" + this + ") at " + Global.instance.currentPhase.name() + " = " + ArrayApply.toString(closures.closure));//DEBUG
}
//where
private SymSet inclClosureBases(SymSet set, Symbol c) {
Type[] parents = c.type().parents();
for (int i = 0; i < parents.length; i++) {
set = inclClosure(set, parents[i].symbol());
}
return set;
}
private SymSet inclClosure(SymSet set, Symbol c) {
Symbol c1 = c;
while (c1.kind == ALIAS) c1 = c1.info().symbol();
return inclClosureBases(set.incl(c1), c1);
}
void adjustType(Type tp) {
Type tp1 = tp.unalias();
int pos = closurePos(tp1.symbol());
assert pos >= 0 : this + " " + tp1 + " " + tp1.symbol();
closures.closure[pos] = tp1;
Type[] parents = tp1.parents();
for (int i = 0; i < parents.length; i++) {
adjustType(parents[i]);
}
}
public void reset(Type completer) {
super.reset(completer);
closures = ClosureIntervalList.EMPTY;
tycon = null;
}
}
/** A class for class symbols. It has JavaClassSymbol as a subclass.
*/
public class ClassSymbol extends TypeSymbol {
/** The mangled class name */
private Name mangled;
/** The symbol's type template */
private Type template;
/** The primary constructor of this type */
public final Symbol constructor;
/** The module belonging to the class. This means:
* For Java classes, its statics parts.
* For module classes, the corresponding module.
* For other classes, null.
*/
private Symbol module = NONE;
/** The given type of self, or NoType, if no explicit type was given.
*/
private Symbol thisSym = this;
/** A cache for this.thisType()
*/
final private Type thistp = Type.ThisType(this);
/** Principal Constructor
*/
public ClassSymbol(int pos, Name name, Symbol owner, int flags) {
super(CLASS, pos, name, owner, flags);
this.constructor = TermSymbol.newConstructor(this, flags);
this.mangled = name;
}
/** Constructor for module classes and classes with static members.
*/
public ClassSymbol(int pos, Name name, Symbol owner, int flags, Symbol module) {
this(pos, name, owner, flags);
this.module = module;
}
/** Constructor for classes to load as source files
*/
public ClassSymbol(Name name, Symbol owner, SourceCompleter parser) {
this(Position.NOPOS, name, owner, 0);
this.module = TermSymbol.newCompanionModule(this, 0, parser);
this.mangled = name;
this.setInfo(parser);
}
/** Constructor for classes to load as class files.
*/
public ClassSymbol(Name name, Symbol owner, ClassParser parser) {
super(CLASS, Position.NOPOS, name, owner, JAVA);
this.constructor = TermSymbol.newConstructor(this, flags);
this.module = TermSymbol.newCompanionModule(this, JAVA, parser.staticsParser(this));
this.mangled = name;
this.setInfo(parser);
}
/** Return a fresh symbol with the same fields as this one.
*/
public Symbol cloneSymbol() {
ClassSymbol other = new ClassSymbol(pos, name, owner(), flags);
other.setInfo(info());
other.constructor.setInfo(constructor.info());
other.mangled = mangled;
other.module = module;
if (thisSym != this) other.setTypeOfThis(typeOfThis());
return other;
}
/** copy all fields to `sym'
*/
public void copyTo(Symbol sym) {
super.copyTo(sym);
if (thisSym != this) sym.setTypeOfThis(typeOfThis());
}
/** Get module */
public Symbol module() {
return module;
}
/** Set the mangled name of this Symbol */
public Symbol setMangledName(Name name) {
this.mangled = name;
return this;
}
/** Get the fully qualified name of this Symbol */
public Name fullName() {
if (owner().kind == CLASS && owner().name.length() != 0)
return Name.fromString(owner().fullName() + "." + name);
else
return name.toTermName();
}
/** Get the mangled name of this Symbol */
public Name mangledName() {
return mangled;
}
/** Get the fully qualified mangled name of this Symbol */
public Name mangledFullName() {
if (mangled == name) {
return fullName().replace((byte)'.', (byte)'$');
} else {
Symbol tc = enclToplevelClass();
if (tc != this) {
return Name.fromString(
enclToplevelClass().mangledFullName() + "$" + mangled);
} else {
return mangled;
}
}
}
/** Get type parameters */
public Symbol[] typeParams() {
return constructor.info().typeParams();
}
/** Get type */
public Type type() {
if (template == null || template.typeArgs().length != typeParams().length) {
template = Type.TypeRef(
owner().thisType(), this, type(typeParams()));
}
return template;
}
public Type thisType() {
return thistp;
}
public Type typeOfThis() {
return thisSym.type();
}
public Symbol setTypeOfThis(Type tp) {
thisSym = new TermSymbol(this.pos, Names.this_, this, SYNTHETIC);
thisSym.setInfo(tp);
return this;
}
/** Get primary constructor */
public Symbol constructor() {
return constructor;
}
/** Return the next enclosing class */
public Symbol enclClass() {
return this;
}
public Symbol caseFieldAccessor(int index) {
assert (flags & CASE) != 0 : this;
Scope.SymbolIterator it = info().members().iterator();
Symbol sym = null;
for (int i = 0; i <= index; i++) {
do {
sym = it.next();
} while (sym.kind != VAL || (sym.flags & CASEACCESSOR) == 0 || !sym.isMethod());
}
//System.out.println(this + ", case field[" + index + "] = " + sym);//DEBUG
assert sym != null : this;
return sym;
}
public void reset(Type completer) {
super.reset(completer);
constructor().reset(completer);
module().reset(completer);
template = null;
thisSym = this;
}
}
/** A class for error symbols.
*/
public final class ErrorSymbol extends Symbol {
/** Constructor */
public ErrorSymbol() {
super(Kinds.ERROR, Position.NOPOS, Name.fromString("<error>"), null,
INITIALIZED);
this.setOwner(this);
this.setInfo(Type.ErrorType);
}
public Symbol cloneSymbol() {
return this;
}
/** Set the mangled name of this Symbol */
public Symbol mangled(Name name) {
return this;
}
/** Set owner */
public Symbol setOwner(Symbol owner) {
if (owner != this)
throw new ApplicationError("illegal operation on " + getClass());
return super.setOwner(owner);
}
/** Set type */
public Symbol setInfo(Type info) {
if (info != Type.ErrorType)
throw new ApplicationError("illegal operation on " + getClass());
return super.setInfo(info);
}
/** Get primary constructor */
public Symbol constructor() {
return TermSymbol.newConstructor(this, 0).setInfo(Type.ErrorType);
}
/** Return the next enclosing class */
public Symbol enclClass() {
return this;
}
public void reset(Type completer) {
}
}
/** The class of Symbol.NONE
*/
public final class NoSymbol extends Symbol {
/** Constructor */
public NoSymbol() {
super(Kinds.NONE, Position.NOPOS, Name.fromString("<none>"), null, INITIALIZED);
this.setInfo(Type.NoType);
this.setOwner(this);
}
/** Return a fresh symbol with the same fields as this one.
*/
public Symbol cloneSymbol() {
return this;
}
/** Set the mangled name of this Symbol */
public Symbol mangled(Name name) {
throw new ApplicationError("illegal operation on " + getClass());
}
/** Set owner */
public Symbol setOwner(Symbol owner) {
if (owner != this)
throw new ApplicationError("illegal operation on " + getClass());
return super.setOwner(owner);
}
/** Set type */
public Symbol setInfo(Type info) {
if (info != Type.NoType)
throw new ApplicationError("illegal operation on " + getClass());
return super.setInfo(info);
}
/** Return the next enclosing class */
public Symbol enclClass() {
return this;
}
public Symbol owner() {
throw new ApplicationError();
}
public void reset(Type completer) {
}
}
/** A class for symbols generated in label definitions.
*/
public class LabelSymbol extends TermSymbol {
/** give as argument the symbol of the function that triggered
the creation of this label */
public LabelSymbol(Symbol f) {
super(f.pos, f.name, f, LABEL);
}
}
/** An exception for signalling cyclic references.
*/
public class CyclicReference extends Type.Error {
public Symbol sym;
public Type info;
public CyclicReference(Symbol sym, Type info) {
super("illegal cyclic reference involving " + sym);
this.sym = sym;
this.info = info;
}
}
/** A class for types indexed by phase numbers.
*/
class TypeIntervalList {
int limit;
Type info;
TypeIntervalList prev;
TypeIntervalList(TypeIntervalList prev) {
this.prev = prev;
}
static TypeIntervalList EMPTY = new TypeIntervalList(null);
static {
EMPTY.limit = -1;
}
}
/** A class for closures indexed by phase numbers.
*/
class ClosureIntervalList {
int limit;
Type[] closure;
ClosureIntervalList prev;
ClosureIntervalList(ClosureIntervalList prev) {
this.prev = prev;
}
static ClosureIntervalList EMPTY = new ClosureIntervalList(null);
static {
EMPTY.limit = -1;
}
}
|