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
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
**
** $Id$
\* */
//todo: implement rule single
package scalac.symtab;
import scalac.ApplicationError;
import scalac.util.*;
import scalac.Global;
public class Type implements Modifiers, Kinds, TypeTags {
public static boolean debugSwitch = false;
private static int indent = 0;
//todo: convert C with {} to C.
public case ErrorType; // not used after analysis
public case AnyType; // not used after analysis
public case NoType;
public case ThisType(Symbol sym);
public case TypeRef(Type pre, Symbol sym, Type[] args);
public case SingleType(Type pre, Symbol sym) {
assert this instanceof ExtSingleType;
}
public case CompoundType(Type[] parts, Scope members) {
assert this instanceof ExtCompoundType;
}
public case MethodType(Symbol[] vparams, Type result);
public case PolyType(Symbol[] tparams, Type result);
public case OverloadedType(Symbol[] alts, Type[] alttypes);
/** Type such as +T. Only used as type arguments.
*/
public case CovarType(Type tp) {
assert !(tp instanceof CovarType);
}
/** Hidden case to implement delayed evaluation of types.
* No need to pattern match on this type; it will never come up.
*/
public case LazyType();
/** Hidden case to implement local type inference.
* Later phases do not need to match on this type.
*/
public case TypeVar(Type origin, Constraint constr);
/** Hidden cases to implement type erasure.
* Earlier phases do not need to match on these types.
*/
public case UnboxedType(int tag);
public case UnboxedArrayType(Type elemtp);
/** Force evaluation of a lazy type. No cycle
* check is needed; since this is done in Symbol.
* @see Symbol.info().
*/
public void complete(Symbol p) {}
// Creators ---------------------------------------------------------------------
/** An owner-less ThisType
*/
public static Type localThisType = ThisType(Symbol.NONE);
/** An empty Type array */
public static final Type[] EMPTY_ARRAY = new Type[0];
/** A non-existing Type array; used to express type erasure */
public static final Type[] NO_ARRAY = new Type[0];
public static SingleType singleType(Type pre, Symbol sym) {
if (pre.isStable() || pre == ErrorType)
return new ExtSingleType(pre, sym);
else
throw new Type.Error(
"malformed type: " + pre + "." + sym.nameString() + ".type");
}
public static TypeRef appliedType(Type tycon, Type[] args) {
switch (tycon) {
case TypeRef(Type pre, Symbol sym, Type[] args1):
if (args == args1) return (TypeRef)tycon;
else return TypeRef(pre, sym, args);
default:
throw new ApplicationError();
}
}
public static CovarType covarType(Type tp) {
if (tp instanceof CovarType) return (CovarType)tp;
else return CovarType(tp);
}
public static CompoundType compoundType(Type[] parts, Scope members,
Symbol clazz) {
ExtCompoundType res = new ExtCompoundType(parts, members);
res.tsym = clazz;
return res;
}
public static CompoundType compoundType(Type[] parts, Scope members) {
ExtCompoundType res = new ExtCompoundType(parts, members);
res.tsym = new ClassSymbol(
Position.NOPOS, Names.COMPOUND_NAME.toTypeName(), Symbol.NONE,
SYNTHETIC | ABSTRACTCLASS);
res.tsym.setInfo(res);
res.tsym.constructor().setInfo(
Type.PolyType(Symbol.EMPTY_ARRAY, Type.NoType));
return res;
}
public static Type typeRef(Type pre, Symbol sym, Type[] args) {
if (pre.isLegalPrefix() || pre == ErrorType)
return TypeRef(pre, sym, args);
else if (sym.kind == ALIAS)
return pre.memberInfo(sym);
else // todo: handle Java-style inner classes
throw new Type.Error(
"malformed type: " + pre + "." + sym.nameString());
}
static class ExtSingleType extends SingleType {
Type tp = null;
int definedId = -1;
ExtSingleType(Type pre, Symbol sym) {
super(pre, sym);
}
private Type type() {
if (definedId != Global.instance.currentPhase.id) {
definedId = Global.instance.currentPhase.id;
tp = pre.memberType(sym).resultType();
}
return tp;
}
/** If this type is a thistype or singleton type, its underlying object type,
* otherwise the type itself.
*/
public Type widen() {
return type().widen();
}
/** If this type is a singleton type whose type is another, the end of the chain,
* otherwise the type itself.
*/
public Type aliasedType() {
Type tp = type();
if (tp.isStable()) return tp.aliasedType();
else return this;
}
}
static class ExtCompoundType extends CompoundType {
Symbol tsym;
ExtCompoundType(Type[] parts, Scope members) {
super(parts, members);
}
public Symbol symbol() {
return tsym;
}
void validate() {//debug
for (Scope.Entry e = members.elems; e != Scope.Entry.NONE; e = e.next)
assert e.sym.owner() == tsym;
}
}
void validate() {//debug
}
// Access methods ---------------------------------------------------------------
/** If this is a thistype, named type, applied type, singleton type, or compound type,
* its symbol, otherwise Symbol.NONE.
*/
public Symbol symbol() {
switch (this) {
case ErrorType:
return Symbol.ERROR;
case ThisType(Symbol sym):
return sym;
case TypeRef(_, Symbol sym, _):
return sym;
case SingleType(_, Symbol sym):
return sym;
case TypeVar(Type origin, _):
return origin.symbol();
case CompoundType(_, _):
// overridden in ExtCompoundType
throw new ApplicationError();
default:
return Symbol.NONE;
}
}
public static Symbol[] symbol(Type[] tps) {
Symbol[] syms = new Symbol[tps.length];
for (int i = 0; i < syms.length; i++)
syms[i] = tps[i].symbol();
return syms;
}
/** If this type is a thistype or singleton type, its underlying object type,
* otherwise the type itself.
*/
public Type widen() {
switch (this) {
case ThisType(Symbol sym):
return sym.typeOfThis();
case SingleType(Type pre, Symbol sym):
// overridden in ExtSingleType
throw new ApplicationError();
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType) return constr.inst.widen();
else return this;
default:
return this;
}
}
public static Type[] widen(Type[] tps) {
if (tps.length == 0) return Type.EMPTY_ARRAY;
Type[] tps1 = new Type[tps.length];
for (int i = 0; i < tps1.length; i++) {
tps1[i] = tps[i].widen();
assert !(tps1[i] instanceof SingleType) : tps[i];//debug
}
return tps1;
}
/** If this type is a singleton type whose type is another, the end of the chain,
* otherwise the type itself.
*/
public Type aliasedType() {
return this;
}
/** The thistype or singleton type corresponding to values of this type.
*/
public Type narrow() {
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.kind == ALIAS) return pre.memberInfo(sym).narrow();
else if (sym.kind == CLASS) return sym.thisType();
else return ThisType(sym);
case CompoundType(_, _):
return symbol().thisType();
default:
return this;
}
}
/** The upper bound of this type.
*/
public Type bound() {
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.kind == ALIAS || sym.kind == TYPE)
return pre.memberInfo(sym).bound();
}
return this;
}
/** The this is a this-type, named-type, applied type or single-type, its prefix,
* otherwise NoType.
*/
public Type prefix() {
switch (this) {
case ThisType(Symbol sym): return sym.owner().thisType();
case TypeRef(Type pre, _, _): return pre;
case SingleType(Type pre, _): return pre;
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType) return constr.inst.prefix();
else return NoType;
default: return NoType;
}
}
/** Get all type arguments of this type.
*/
public Type[] typeArgs() {
switch (unalias()) {
case TypeRef(_, _, Type[] args):
return args;
default:
return Type.EMPTY_ARRAY;
}
}
/** Get type of `this' symbol corresponding to this type, extend
* homomorphically to function types and method types.
*/
public Type instanceType() {
switch (unalias()) {
case TypeRef(Type pre, Symbol sym, Type[] args):
Type tp1 = sym.typeOfThis();
if (tp1 != sym.type())
return tp1.asSeenFrom(pre, sym.owner()).subst(sym.typeParams(), args);
break;
case MethodType(Symbol[] params, Type restp):
Type restp1 = restp.instanceType();
if (restp1 != restp)
return MethodType(params, restp1);
break;
case PolyType(Symbol[] tparams, Type restp):
Type restp1 = restp.instanceType();
if (restp1 != restp)
return PolyType(tparams, restp1);
break;
}
return this;
}
/** Remove all aliases
*/
public Type unalias() {
Type result = unalias(0);//debug
//if (this != result) System.out.println(this + " ==> " + result);//DEBUG
return result;
}
private Type unalias(int n) {
if (n == 20) throw new Type.Error("recursive type alias: " + this);
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.kind == ALIAS) return pre.memberInfo(sym).unalias(n + 1);
break;
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType) return constr.inst.unalias(n + 1);
else return this;
}
return this;
}
/** The (prefix-adapted) parents of this type.
*/
public Type[] parents() {
switch (unalias()) {
case ThisType(_):
case SingleType(_, _):
return widen().parents();
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.kind == ALIAS)
return unalias().parents();
else if (sym.kind == CLASS) {
assert sym.typeParams().length == args.length : sym + " " + ArrayApply.toString(args);//debug
return subst(asSeenFrom(sym.info().parents(), pre, sym.owner()),
sym.typeParams(), args);
} else
return new Type[]{sym.info().asSeenFrom(pre, sym.owner())};
case CompoundType(Type[] parts, _):
return parts;
default:
return Type.EMPTY_ARRAY;
}
}
/** Get type parameters of polymorphic method
* or EMPTY_ARRAY if not applicable.
*/
public Symbol[] typeParams() {
switch (this) {
case PolyType(Symbol[] tparams, _):
return tparams;
case TypeRef(_, Symbol sym, _):
if (sym.kind == CLASS) return sym.typeParams();
else return sym.info().typeParams();
}
return Symbol.EMPTY_ARRAY;
}
/** If this type is a (possibly polymorphic) method type, its result type
* after applying all method argument sections,
* otherwise the type itself.
*/
public Type resultType() {
switch (this) {
case PolyType(_, Type tpe):
return tpe.resultType();
case MethodType(_, Type tpe):
return tpe.resultType();
default:
return this;
}
}
/** The number of value parameter sections of this type.
*/
public int paramSectionCount() {
switch (this) {
case PolyType(_, Type restpe):
return restpe.paramSectionCount();
case MethodType(_, Type restpe):
return restpe.paramSectionCount() + 1;
default: return 0;
}
}
/** The first parameter section of this type.
*/
public Symbol[] firstParams() {
switch (this) {
case PolyType(_, Type restpe):
return restpe.firstParams();
case MethodType(Symbol[] params, _):
return params;
default: return Symbol.EMPTY_ARRAY;
}
}
/** If this type is overloaded, its alternative types,
* otherwise an array consisting of this type itself.
*/
public Type[] alternatives() {
switch (this) {
case OverloadedType(_, Type[] alttypes):
return alttypes;
default:
return new Type[]{this};
}
}
/** If type is a this type of a module class, transform to singletype of
* module.
*/
public Type expandModuleThis() {
switch (this) {
case ThisType(Symbol sym):
if ((sym.flags & MODUL) != 0 && sym.module() != Symbol.NONE) {
return singleType(
sym.owner().thisType().expandModuleThis(), sym.module());
}
}
return this;
}
/** If this is a covariant type, its underlying type, otherwise the type itself.
*/
public Type dropVariance() {
switch (this) {
case CovarType(Type tp): return tp;
default: return this;
}
}
public static Map dropVarianceMap = new Map() {
public Type apply(Type t) { return t.dropVariance(); }
};
// Tests --------------------------------------------------------------------
/** Is this type a this type or singleton type?
*/
public boolean isStable() {
switch (unalias()) {
case ThisType(_):
case SingleType(_, _):
return true;
default:
return false;
}
}
/** Is this type a legal prefix?
*/
public boolean isLegalPrefix() {
switch (unalias()) {
case ThisType(_):
case SingleType(_, _):
return true;
case TypeRef(_, Symbol sym, _):
return sym.kind == CLASS &&
((sym.flags & JAVA) != 0 ||
(sym.flags & (TRAIT | ABSTRACTCLASS)) == 0);
default:
return false;
}
}
/** Is this type a reference to an object type?
*/
public boolean isObjectType() {
switch (unalias()) {
case ThisType(_):
case SingleType(_, _):
case CompoundType(_, _):
return true;
case TypeRef(Type pre, Symbol sym, _):
return sym.kind != ALIAS || unalias().isObjectType();
default:
return false;
}
}
/** Is this type a covariant type?
*/
public boolean isCovarType() {
switch (this) {
case CovarType(_):
return true;
default:
return false;
}
}
/** Is this type of the form scala.FunctionN[T_1, ..., T_n, +T]?
*/
public boolean isFunctionType() {
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.fullName().startsWith(Names.scala_Function)) {
for (int i = 0; i < args.length - 1; i++)
if (args[i].isCovarType()) return false;
return args.length > 0 && args[args.length - 1].isCovarType();
}
}
return false;
}
// Members and Lookup -------------------------------------------------------
/** Get the scope containing the local members of this type.
* Symbols in this scope are not prefix-adapted!
*/
public Scope members() {
switch (this) {
case ErrorType:
return new Scope();
case TypeRef(_, Symbol sym, _):
return sym.info().members();
case SingleType(_, Symbol sym):
return widen().members();
case CompoundType(Type[] basetypes, Scope members):
return members;
default:
return Scope.EMPTY;
}
}
/** Lookup symbol with given name among all local and inherited members
* of this type; return Symbol.NONE if not found.
*/
public Symbol lookup(Name name) {
switch (this) {
case ErrorType:
return Symbol.ERROR;
case ThisType(_):
case SingleType(_, _):
return widen().lookup(name);
case TypeRef(_, Symbol sym, _):
return sym.info().lookup(name);
case CompoundType(Type[] parts, Scope members):
Symbol sym = members.lookup(name);
if (sym.kind != NONE) return sym;
else return lookupNonPrivate(name);
default:
return Symbol.NONE;
}
}
/** Lookup non-private symbol with given name among all local and
* inherited members of this type; return Symbol.NONE if not found.
*/
public Symbol lookupNonPrivate(Name name) {
return lookupNonPrivate(name, 0);
}
/** Same as before, but with additional parameter `start'.
* If start == 0, lookup in all basetypes of a compound type.
* If start == 1, lookup only in mixin classes.
*/
private Symbol lookupNonPrivate(Name name, int start) {
switch (this) {
case ErrorType:
return Symbol.ERROR;
case ThisType(_):
case SingleType(_, _):
return widen().lookupNonPrivate(name);
case TypeRef(_, Symbol sym, _):
return sym.info().lookupNonPrivate(name, start);
case CompoundType(Type[] parts, Scope members):
Symbol sym = members.lookup(name);
if (sym.kind != NONE && (sym.flags & PRIVATE) == 0)
return sym;
// search base types in reverse; non-abstract members
// take precedence over abstract ones.
int i = parts.length;
sym = Symbol.NONE;
while (i > start && (sym.kind == NONE || (sym.flags & DEFERRED) != 0)) {
i--;
Symbol sym1 = parts[i].lookupNonPrivate(name, i == 0 ? 0 : 1);
if (sym1.kind != NONE &&
(sym1.flags & PRIVATE) == 0 &&
(sym.kind == NONE || (sym1.flags & DEFERRED) == 0))
sym = sym1;
}
return sym;
default:
return Symbol.NONE;
}
}
// Maps --------------------------------------------------------------------------
/** The type of type-to-type functions.
*/
public abstract static class Map {
boolean covariantOK = true;
public abstract Type apply(Type t);
/** Apply map to all top-level components of this type.
*/
public Type map(Type tp) {
switch (tp) {
case ErrorType:
case AnyType:
case NoType:
case UnboxedType(_):
case TypeVar(_, _):
case ThisType(_):
return tp;
case TypeRef(Type pre, Symbol sym, Type[] args):
Type pre1 = apply(pre);
Type[] args1 = map(args);
if (pre1 == pre && args1 == args) return tp;
else return TypeRef(pre1, sym, args1);
case SingleType(Type pre, Symbol sym):
Type pre1 = apply(pre);
if (pre1 == pre) return tp;
else return singleType(pre1, sym);
case CompoundType(Type[] parts, Scope members):
Type[] parts1 = map(parts);
Scope members1 = map(members);
if (parts1 == parts && members1 == members) {
return tp;
} else if (members1 == members && !tp.symbol().isCompoundSym()) {
return compoundType(parts1, members, tp.symbol());
} else {
Scope members2 = new Scope();
//Type tp1 = compoundType(parts1, members2);
Type tp1 = (tp.symbol().isCompoundSym()) ? compoundType(parts1, members2)
: compoundType(parts1, members2, tp.symbol());
Symbol[] syms1 = members1.elements();
Symbol[] syms2 = new Symbol[syms1.length];
for (int i = 0; i < syms2.length; i++) {
syms2[i] = syms1[i].cloneSymbol().setOwner(tp1.symbol());
}
for (int i = 0; i < syms2.length; i++) {
syms2[i].setInfo(syms1[i].info().subst(syms1, syms2));
}
for (int i = 0; i < syms2.length; i++) {
members2.enter(syms2[i]);
}
return tp1;
}
case MethodType(Symbol[] vparams, Type result):
covariantOK = false;
Symbol[] vparams1 = map(vparams);
covariantOK = true;
Type result1 = apply(result);
if (vparams1 == vparams && result1 == result) return tp;
else return MethodType(vparams1, result1);
case PolyType(Symbol[] tparams, Type result):
covariantOK = false;
Symbol[] tparams1 = map(tparams);
covariantOK = true;
Type result1 = apply(result);
if (tparams1 != tparams) result1 = result1.subst(tparams, tparams1);
if (tparams1 == tparams && result1 == result) return tp;
else return PolyType(tparams1, result1);
case OverloadedType(Symbol[] alts, Type[] alttypes):
Type[] alttypes1 = map(alttypes);
if (alttypes1 == alttypes) return tp;
else return OverloadedType(alts, alttypes1);
case CovarType(Type t):
Type t1 = apply(t);
if (t1 == t) return tp;
else return covarType(t1);
case UnboxedArrayType(Type elemtp):
Type elemtp1 = apply(elemtp);
if (elemtp1 == elemtp) return tp;
else return UnboxedArrayType(elemtp1);
default:
throw new ApplicationError(tp);
}
}
public Symbol map(Symbol sym) {
Type tp = sym.info();
Type tp1 = apply(tp);
if (tp == tp1) return sym;
else return sym.cloneSymbol().setInfo(tp1);
}
public Type[] map(Type[] tps) {
Type[] tps1 = tps;
for (int i = 0; i < tps.length; i++) {
Type tp = tps[i];
Type tp1 = apply(tp);
if (tp1 != tp && tps1 == tps) {
tps1 = new Type[tps.length];
System.arraycopy(tps, 0, tps1, 0, i);
}
tps1[i] = tp1;
}
return tps1;
}
/** Apply map to all elements of this array of symbols,
* preserving recursive references to symbols in the array.
*/
public Symbol[] map(Symbol[] syms) {
Symbol[] syms1 = syms;
for (int i = 0; i < syms.length; i++) {
Symbol sym = syms[i];
Symbol sym1 = map(sym);
if (sym != sym1 && syms1 == syms) {
syms1 = new Symbol[syms.length];
System.arraycopy(syms, 0, syms1, 0, i);
}
syms1[i] = sym1;
}
if (syms1 != syms) {
for (int i = 0; i < syms1.length; i++) {
if (syms1[i] == syms[i])
syms1[i] = syms[i].cloneSymbol();
}
for (int i = 0; i < syms1.length; i++) {
syms1[i].setInfo(syms1[i].info().subst(syms, syms1));
}
}
return syms1;
}
/** Apply map to all elements of this array of this scope.
*/
public Scope map(Scope s) {
Symbol[] members = s.elements();
Symbol[] members1 = map(members);
if (members == members1) return s;
else return new Scope(members1);
}
}
public abstract static class MapOnlyTypes extends Map {
public Symbol map(Symbol sym) { return sym; }
public Symbol[] map(Symbol[] syms) { return syms; }
public Scope map(Scope s) { return s; }
}
// baseType and asSeenFrom --------------------------------------------------------
/** Return the base type of this type whose symbol is `clazz', or NoType, if
* such a type does not exist.
*/
public Type baseType(Symbol clazz) {
//System.out.println(this + ".baseType(" + clazz + ")");//DEBUG
switch (this) {
case ErrorType:
return ErrorType;
case ThisType(_):
case SingleType(_, _):
return widen().baseType(clazz);
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym == clazz)
return this;
else if (sym.kind == TYPE || sym.kind == ALIAS)
return pre.memberInfo(sym).baseType(clazz);
else if (clazz.isCompoundSym())
return NoType;
else {
return sym.baseType(clazz)
.asSeenFrom(pre, clazz.owner())
.subst(sym.typeParams(), args);
}
case CompoundType(Type[] parts, _):
for (int i = parts.length - 1; i >= 0; i--) {
Type result = parts[i].baseType(clazz);
if (result != NoType) return result;
}
break;
case UnboxedArrayType(_):
if (clazz == Global.instance.definitions.ANY_CLASS)
return clazz.type();
}
return NoType;
}
/** Return overriding instance of `sym' in this type,
* or `sym' itself if none exists.
*/
public Symbol rebind(Symbol sym) {
Symbol sym1 = lookupNonPrivate(sym.name);
if (sym1.kind != NONE) {
//System.out.println("rebinding " + sym + " to " + sym1);//DEBUG
return sym1;
}
else return sym;
}
/** A map to implement `asSeenFrom'.
*/
static class AsSeenFromMap extends Map {
private Type pre;
private Symbol clazz;
private Type illegalType = NoType;
private boolean typeArg = false;
AsSeenFromMap(Type pre, Symbol clazz) {
this.pre = pre; this.clazz = clazz;
}
public Type apply0(Type t) {
Type t1 = apply0(t);
System.out.println(t + " as seen from (" + pre + "," + clazz + ") = " + t1);//debug
return t1;
}
public Type apply(Type t) {
if (pre == NoType || clazz.kind != CLASS)
return t;
switch (t) {
case ThisType(Symbol sym):
return t.toPrefix(pre, clazz);
case TypeRef(Type prefix, Symbol sym, Type[] args):
if (sym.kind == ALIAS) {
return apply(t.unalias());
} else if (sym.owner().isPrimaryConstructor()) {
assert sym.kind == TYPE;
Type t1 = t.toInstance(pre, clazz);
//System.out.println(t + ".toInstance(" + pre + "," + clazz + ") = " + t1);//DEBUG
switch (t1) {
case CovarType(Type tp):
if (!covariantOK) {
if (illegalType == NoType) illegalType = t1;
} else if (!typeArg) {
t1 = tp;
}
}
return t1;
} else {
//Type prefix1 = prefix.toPrefix(pre, clazz);
Type prefix1 = apply(prefix);
Symbol sym1 = (prefix1 == prefix || (sym.flags & MODUL) != 0)
? sym : prefix1.rebind(sym);
boolean prevTypeArg = typeArg;
typeArg = true;
Type[] args1 = map(args);
typeArg = prevTypeArg;
if (prefix1 == prefix && args1 == args) return t;
else return TypeRef(prefix1, sym1, args1);
}
case SingleType(Type prefix, Symbol sym):
try {
//Type prefix1 = prefix.toPrefix(pre, clazz);
Type prefix1 = apply(prefix);
if (prefix1 == prefix) return t;
else return singleType(prefix1, prefix1.rebind(sym));
} catch (Type.Error ex) {}
return apply(t.widen());
default:
return map(t);
}
}
void checkLegal(Type tp) {
if (illegalType != NoType) {
throw new ApplicationError(
"malformed type: " + tp + "; " +
illegalType + " does not occur in covariant position");
}
}
}
//where
Type toInstance(Type pre, Symbol clazz) {
if (pre == NoType || clazz.kind != CLASS)
return this;
Symbol sym = symbol();
Symbol ownclass = sym.owner().primaryConstructorClass();
if (ownclass.isSubClass(clazz) &&
pre.widen().symbol().isSubClass(ownclass)) {
switch (pre.baseType(ownclass)) {
case TypeRef(_, Symbol basesym, Type[] baseargs):
Symbol[] baseparams = basesym.typeParams();
for (int i = 0; i < baseparams.length; i++) {
if (sym == baseparams[i]) return baseargs[i];
}
break;
case ErrorType:
return ErrorType;
}
throw new ApplicationError(
this + " in " + ownclass + " cannot be instantiated from " + pre.widen());
} else {
return toInstance(
pre.baseType(clazz).prefix(), clazz.owner());
}
}
Type toPrefix(Type pre, Symbol clazz) {
if (pre == NoType || clazz.kind != CLASS)
return this;
else if (symbol().isSubClass(clazz) &&
pre.widen().symbol().isSubClass(symbol()))
return pre;
else
return toPrefix(pre.baseType(clazz).prefix(), clazz.owner());
}
/** This type Types as seen from prefix `pre' and class `clazz'. This means:
* Replace all this thistypes of `clazz' or one of its superclasses by `pre'.
* Proceed analogously for thistypes referring to outer classes.
*/
public Type asSeenFrom(Type pre, Symbol clazz) {
//System.out.println("computing asseenfrom of " + this + " with " + pre + "," + clazz);//DEBUG
AsSeenFromMap f = new AsSeenFromMap(pre, clazz);
Type t = f.apply(this);
f.checkLegal(t);
return t;
}
/** Types `these' as seen from prefix `pre' and class `clazz'.
*/
public static Type[] asSeenFrom(Type[] these, Type pre, Symbol clazz) {
AsSeenFromMap f = new AsSeenFromMap(pre, clazz);
Type[] these1 = f.map(these);
for (int i = 0; i < these1.length; i++)
f.checkLegal(these1[i]);
return these1;
}
/** The info of `sym', seen as a member of this type.
*/
public Type memberInfo(Symbol sym) {
return memberTransform(sym, sym.info());
}
/** The type of `sym', seen as a member of this type.
*/
public Type memberType(Symbol sym) {
return memberTransform(sym, sym.type());
}
private Type memberTransform(Symbol sym, Type tp) {
Type tp1 = tp.asSeenFrom(this, sym.owner());
//if (Global.instance.debug) System.out.println(this + ".memberType(" + sym + ":" + tp + ") = " + tp1);//DEBUG
return tp1;
}
// Substitutions ---------------------------------------------------------------
/** A common map superclass for symbol/symbol and type/symbol substitutions.
*/
public static abstract class SubstMap extends Map {
private Symbol[] from;
SubstMap(Symbol[] from) {
this.from = from;
}
public boolean matches(Symbol sym1, Symbol sym2) {
return sym1 == sym2;
}
/** Produce replacement type
* @param i The index in `from' of the symbol to be replaced.
* @param fromtp The type referring to this symbol.
*/
protected abstract Type replacement(int i, Type fromtp);
/** Produce new substitution where some symbols are excluded.
* @param newfrom The new array of from symbols (without excluded syms)
* @param excluded The array of excluded sysmbols
*/
protected abstract SubstMap exclude(Symbol[] newfrom, Symbol[] excluded);
public Type apply(Type t) {
switch (t) {
case TypeRef(ThisType(_), Symbol sym, Type[] args):
for (int i = 0; i < from.length; i++) {
if (matches(sym, from[i])) return replacement(i, t);
}
break;
case SingleType(ThisType(_), Symbol sym):
for (int i = 0; i < from.length; i++) {
if (matches(sym, from[i])) return replacement(i, t);
}
break;
case PolyType(Symbol[] tparams, Type result):
Symbol[] from1 = excludeSyms(from, tparams, from);
if (from1 != from) {
SubstMap f = exclude(from1, tparams);
Symbol[] tparams1 = f.map(tparams);
Type result1 = f.apply(result);
if (tparams1 != tparams)
result1 = result1.subst(tparams, tparams1);
if (tparams1 == tparams && result1 == result) return t;
else return PolyType(tparams1, result1);
}
}
return map(t);
}
//where
private boolean contains1(Symbol[] syms, Symbol sym) {
int i = 0;
while (i < syms.length && syms[i] != sym) i++;
return i < syms.length;
}
private int nCommon(Symbol[] from, Symbol[] tparams) {
int cnt = 0;
for (int i = 0; i < from.length; i++) {
if (contains1(tparams, from[i])) cnt++;
}
return cnt;
}
private Symbol[] excludeSyms(Symbol[] from, Symbol[] tparams, Symbol[] syms) {
int n = nCommon(from, tparams);
if (n == 0) {
return syms;
} else {
Symbol[] syms1 = new Symbol[syms.length - n];
int j = 0;
for (int i = 0; i < from.length; i++) {
if (!contains1(tparams, from[i])) syms1[j++] = syms[i];
}
return syms1;
}
}
private Type[] excludeTypes(Symbol[] from, Symbol[] tparams, Type[] types) {
int n = nCommon(from, tparams);
if (n == 0) {
return types;
} else {
Type[] types1 = new Type[types.length - n];
int j = 0;
for (int i = 0; i < from.length; i++) {
if (!contains1(tparams, from[i])) types1[j++] = types[i];
}
return types1;
}
}
}
/** A map for symbol/symbol substitutions
*/
public static class SubstSymMap extends SubstMap {
Symbol[] to;
protected SubstSymMap(Symbol[] from, Symbol[] to) {
super(from);
this.to = to;
}
protected Type replacement(int i, Type fromtp) {
switch (fromtp) {
case TypeRef(Type pre, Symbol sym, Type[] args):
return TypeRef(pre, to[i], args);
case SingleType(Type pre, Symbol sym):
return singleType(pre, to[i]);
default:
throw new ApplicationError();
}
}
protected SubstMap exclude(Symbol[] newfrom, Symbol[] excluded) {
return new SubstSymMap(newfrom, excludeSyms(from, excluded, to));
}
}
/** A map for type/symbol substitutions
*/
public static class SubstTypeMap extends SubstMap {
Type[] to;
protected SubstTypeMap(Symbol[] from, Type[] to) {
super(from);
this.to = to;
}
protected Type replacement(int i, Type fromtp) {
return to[i];
}
protected SubstMap exclude(Symbol[] newfrom, Symbol[] excluded) {
return new SubstTypeMap(newfrom, excludeTypes(from, excluded, to));
}
}
/** Substitute symbols `to' for occurrences of symbols `from' in this type.
*/
public Type subst(Symbol[] from, Symbol[] to) {
assert from.length == to.length
: this + ": " + from.length + " != " + to.length;
if (from.length != 0 && from != to)
return new SubstSymMap(from, to).apply(this);
else return this;
}
/** Substitute symbols `to' for occurrences of symbols `from' in these types.
*/
public static Type[] subst(Type[] these, Symbol[] from, Symbol[] to) {
assert from.length == to.length;
if (these.length != 0 && from.length != 0 && from != to)
return new SubstSymMap(from, to).map(these);
else return these;
}
/** Substitute types `to' for occurrences of symbols `from' in this type.
*/
public Type subst(Symbol[] from, Type[] to) {
assert from.length == to.length
: this + ": " + from.length + " != " + to.length;
if (from.length != 0)
return new SubstTypeMap(from, to).apply(this);
else return this;
}
/** Substitute types `to' for occurrences of symbols `from' in these types.
*/
public static Type[] subst(Type[] these, Symbol[] from, Type[] to) {
assert from.length == to.length;
if (these.length != 0 && from.length != 0)
return new SubstTypeMap(from, to).map(these);
else return these;
}
/** A map for substitutions of thistypes.
*/
public static class SubstThisMap extends Map {
Symbol from;
Type to;
protected SubstThisMap(Symbol from, Type to) {
this.from = from;
this.to = to;
}
public Type apply(Type t) {
switch (t) {
case ThisType(Symbol sym):
if (sym == from) return to;
else return t;
default:
return map(t);
}
}
}
public Type substThis(Symbol from, Type to) {
return new SubstThisMap(from, to).apply(this);
}
public static Type[] substThis(Type[] these, Symbol from, Type to) {
return new SubstThisMap(from, to).map(these);
}
static class ContainsMap extends Map {
boolean result = false;
Symbol sym;
ContainsMap(Symbol sym) {
this.sym = sym;
}
public Type apply(Type t) {
switch (t) {
case TypeRef(Type pre, Symbol sym1, Type[] args):
if (sym == sym1) result = true;
else { map(pre); map(args); }
break;
case SingleType(Type pre, Symbol sym1):
map(pre);
if (sym == sym1) result = true;
break;
default:
map(t);
}
return t;
}
}
/** Does this type contain symbol `sym'?
*/
public boolean contains(Symbol sym) {
ContainsMap f = new ContainsMap(sym);
f.apply(this);
return f.result;
}
/** Does this type contain any of the symbols `syms'?
*/
public boolean containsSome(Symbol[] syms) {
for (int i = 0; i < syms.length; i++)
if (contains(syms[i])) return true;
return false;
}
// Comparisons ------------------------------------------------------------------
/** Is this type a subtype of that type?
*/
public boolean isSubType(Type that) {
if (debugSwitch) {
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(this + " < " + that + "?");
indent++;
}
boolean result = isSubType0(that);
if (debugSwitch) {
indent--;
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(result);
}
return result;
}
public boolean isSubType0(Type that) {
if (this == that) return true;
switch (this) {
case ErrorType:
case AnyType:
return true;
}
switch (that) {
case ErrorType:
case AnyType:
return true;
case NoType:
return false;
case ThisType(_):
case SingleType(_, _):
switch (this) {
case ThisType(_):
case SingleType(_, _):
return this.isSameAs(that);
}
break;
case TypeRef(Type pre1, Symbol sym1, Type[] args1):
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym == sym1 && pre.isSameAs(pre1) && isSubArgs(args, args1))
return true;
break;
}
if (sym1.kind == CLASS) {
Type base = this.baseType(sym1);
if (this != base && base.isSubType(that))
return true;
}
break;
case CompoundType(Type[] parts1, Scope members1):
int i = 0;
while (i < parts1.length && isSubType(parts1[i])) i++;
if (i == parts1.length && specializes(members1))
return true;
break;
case MethodType(Symbol[] ps1, Type res1):
switch (this) {
case MethodType(Symbol[] ps, Type res):
if (ps.length != ps1.length) return false;
for (int i = 0; i < ps.length; i++) {
Symbol p1 = ps1[i];
Symbol p = ps[i];
if (!p1.type().isSubType(p.type()) ||
(p1.flags & Modifiers.DEF) != (p.flags & Modifiers.DEF))
return false;
}
return res.isSubType(res1);
}
break;
case PolyType(Symbol[] ps1, Type res1):
switch (this) {
case PolyType(Symbol[] ps, Type res):
if (ps.length != ps1.length) return false;
for (int i = 0; i < ps.length; i++)
if (!ps1[i].info().subst(ps1, ps).isSubType(ps[i].info()))
return false;
return res.isSubType(res1.subst(ps1, ps));
}
break;
case OverloadedType(Symbol[] alts1, Type[] alttypes1):
if (isSubSet(alttypes1, alternatives()))
return true;
break;
case UnboxedType(int tag1):
switch (this) {
case UnboxedType(int tag):
return tag <= tag1 && tag1 <= DOUBLE && tag1 != CHAR;
}
break;
case UnboxedArrayType(Type elemtp1):
switch (this) {
case UnboxedArrayType(Type elemtp):
return !(elemtp1 instanceof UnboxedType) && elemtp.isSubType(elemtp1);
}
break;
case TypeVar(Type origin, Constraint constr):
//todo: should we test for equality with origin?
if (constr.inst != NoType) {
return this.isSubType(constr.inst);
} else if (!this.isCovarType()) {
constr.lobounds = new List(this, constr.lobounds);
return true;
}
break;
default:
throw new ApplicationError(this + " <: " + that);
}
switch (this) {
case NoType:
return false;
case ThisType(_):
case SingleType(_, _):
if (this.widen().isSubType(that)) return true;
break;
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType) {
return constr.inst.isSubType(that);
} else {
constr.hibounds = new List(that.dropVariance(), constr.hibounds);
return true;
}
case TypeRef(_, Symbol sym, _):
if (sym.kind == ALIAS) return this.unalias().isSubType(that);
break;
case OverloadedType(Symbol[] alts, Type[] alttypes):
for (int i = 0; i < alttypes.length; i++) {
if (alttypes[i].isSameAs0(that)) return true;
}
break;
}
switch (that) {
case TypeRef(_, Symbol sym1, _):
if (sym1.kind == ALIAS) return this.isSubType(that.unalias());
break;
}
return false;
}
/** Are types `these' subtypes of corresponding types `those'?
*/
public static boolean isSubType(Type[] these, Type[] those) {
if (these.length != those.length) return false;
for (int i = 0; i < these.length; i++) {
if (!these[i].isSubType(those[i])) return false;
}
return true;
}
/** Are types `these' arguments types conforming to corresponding types `those'?
*/
static boolean isSubArgs(Type[] these, Type[] those) {
if (these.length != those.length) return false;
for (int i = 0; i < these.length; i++) {
switch (those[i]) {
case CovarType(Type tp1):
switch (these[i]) {
case CovarType(Type tp):
if (!tp.isSubType(tp1)) return false;
break;
default:
if (!these[i].isSubType(tp1)) return false;
}
break;
default:
if (these[i].isCovarType() || !these[i].isSameAs(those[i]))
return false;
}
}
return true;
}
public static boolean isSubSet(Type[] alts, Type[] alts1) {
for (int i = 0; i < alts.length; i++) {
int j = 0;
while (j < alts1.length && !alts1[j].isSameAs(alts[i])) j++;
if (j == alts1.length) return false;
}
return true;
}
/** Does this type implement all symbols in scope `s' with same or stronger types?
*/
public boolean specializes(Scope s) {
for (Scope.Entry e = s.elems; e != Scope.Entry.NONE; e = e.next)
if (!specializes(e.sym)) return false;
return true;
}
/** Does this type implement symbol `sym1' with same or stronger type?
*/
public boolean specializes(Symbol sym1) {
if (debugSwitch) {
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(this + " specializes " + sym1 + "?");
indent++;
}
boolean result = specializes0(sym1);
if (debugSwitch) {
indent--;
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(result);
}
return result;
}
public boolean specializes0(Symbol sym1) {
Symbol sym = lookup(sym1.name);
Type self = narrow();
return
sym == sym1 ||
((sym.kind == sym1.kind || sym1.kind == TYPE) &&
self.memberInfo(sym).subst(symbol().typeParams(), typeArgs())
.isSubType(sym1.info().substThis(sym.owner(), self))) ||
(sym.kind == TYPE && sym1.kind == ALIAS &&
sym1.info().unalias().isSameAs(sym.type()));
}
/** Is this type the same as that type?
*/
public boolean isSameAs(Type that) {
if (debugSwitch) {
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(this + " = " + that + "?");
indent++;
}
boolean result = isSameAs0(that);
if (debugSwitch) {
indent--;
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println(result);
}
return result;
}
public boolean isSameAs0(Type that) {
if (this == that) return true;
switch (this) {
case ErrorType:
case AnyType:
return true;
case ThisType(Symbol sym):
switch (that) {
case ThisType(Symbol sym1):
return sym == sym1;
case SingleType(Type pre1, Symbol sym1):
return sym1.isModule()
&& sym == sym1.moduleClass()
&& sym.owner().thisType().isSameAs(pre1)
||
that != that.aliasedType() &&
this.isSameAs(that.aliasedType());
}
break;
case SingleType(Type pre, Symbol sym):
switch (that) {
case SingleType(Type pre1, Symbol sym1):
return sym == sym1 && pre.isSameAs(pre1)
||
(this != this.aliasedType() || that != that.aliasedType()) &&
this.aliasedType().isSameAs(that.aliasedType());
case ThisType(Symbol sym1):
return sym.isModule()
&& sym.moduleClass() == sym1
&& pre.isSameAs(sym1.owner().thisType())
||
this != this.aliasedType() &&
this.aliasedType().isSameAs(that);
}
break;
case TypeRef(Type pre, Symbol sym, Type[] args):
switch (that) {
case TypeRef(Type pre1, Symbol sym1, Type[] args1):
if (sym == sym1 && pre.isSameAs(pre1) && isSameArgs(args, args1))
return true;
}
break;
case CompoundType(Type[] parts, Scope members):
switch (that) {
case CompoundType(Type[] parts1, Scope members1):
if (parts.length != parts1.length) return false;
for (int i = 0; i < parts.length; i++)
if (!parts[i].isSameAs(parts1[i])) return false;
return isSameAs(members, members1);
}
break;
case MethodType(Symbol[] ps, Type res):
switch (that) {
case MethodType(Symbol[] ps1, Type res1):
if (ps.length != ps1.length) return false;
for (int i = 0; i < ps.length; i++) {
Symbol p1 = ps1[i];
Symbol p = ps[i];
if (!p1.type().isSameAs(p.type()) ||
(p1.flags & Modifiers.DEF) != (p.flags & Modifiers.DEF))
return false;
}
return res.isSameAs(res1);
}
break;
case PolyType(Symbol[] ps, Type res):
switch (that) {
case PolyType(Symbol[] ps1, Type res1):
if (ps.length != ps1.length) return false;
for (int i = 0; i < ps.length; i++)
if (!ps1[i].info().subst(ps1, ps).isSameAs(ps[i].info()))
return false;
return res.isSameAs(res1.subst(ps1, ps));
}
break;
case OverloadedType(Symbol[] alts, Type[] alttypes):
switch (that) {
case OverloadedType(Symbol[] alts1, Type[] alttypes1):
return isSubSet(alttypes1, alttypes)
&& isSubSet(alttypes, alttypes1);
}
break;
case UnboxedType(int kind):
switch (that) {
case UnboxedType(int kind1):
return kind == kind1;
}
break;
case UnboxedArrayType(Type elemtp):
switch (that) {
case UnboxedArrayType(Type elemtp1):
return elemtp.isSameAs(elemtp1);
}
break;
}
switch (that) {
case ErrorType:
case AnyType:
return true;
case NoType:
return false;
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType)
return constr.inst.isSameAs(this);
else if (!this.isCovarType())
return constr.instantiate(this.any2typevar());
}
switch (this) {
case NoType:
return false;
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType)
return constr.inst.isSameAs(that);
else if (!that.isCovarType())
return constr.instantiate(that.any2typevar());
break;
case TypeRef(_, Symbol sym, _):
if (sym.kind == ALIAS) return this.unalias().isSameAs(that);
}
switch (that) {
case TypeRef(_, Symbol sym, _):
if (sym.kind == ALIAS) return this.isSameAs(that.unalias());
}
return false;
}
/** Is this type argument that same as `that'?
*/
public boolean isSameArg(Type that) {
switch (this) {
case CovarType(Type tp):
switch (that) {
case CovarType(Type tp1):
return tp.isSameAs(tp1);
}
}
return this.isSameAs(that);
}
/** Are types `these' the same as corresponding types `those'?
*/
public static boolean isSameAs(Type[] these, Type[] those) {
if (these.length != those.length) return false;
for (int i = 0; i < these.length; i++) {
if (!these[i].isSameAs(those[i])) return false;
}
return true;
}
/** Are type arguments `these' the same as corresponding types `those'?
*/
public static boolean isSameArgs(Type[] these, Type[] those) {
if (these.length != those.length) return false;
for (int i = 0; i < these.length; i++) {
if (!these[i].isSameArg(those[i])) return false;
}
return true;
}
/** Do scopes `s1' and `s2' define he same symbols with the same kinds and infos?
*/
public boolean isSameAs(Scope s1, Scope s2) {
return isSubScope(s1, s2) && isSubScope(s2, s1);
}
/** Does scope `s1' define all symbols of scope `s2' with the same kinds and infos?
*/
private boolean isSubScope(Scope s1, Scope s2) {
for (Scope.Entry e = s2.elems; e != Scope.Entry.NONE; e = e.next) {
Symbol sym2 = e.sym;
Symbol sym1 = s1.lookup(sym2.name);
if (sym1.kind != sym2.kind ||
!sym1.info().isSameAs(
sym2.info().substThis(sym2.owner(), sym1.owner().thisType())))
return false;
}
return true;
}
boolean isSameAsAll(Type[] tps) {
int i = 1;
while (i < tps.length && isSameAs(tps[i])) i++;
return i == tps.length;
}
/** Map every occurrence of AnyType to a fresh type variable.
*/
public static Map any2typevarMap = new Map() {
public Type apply(Type t) { return t.any2typevar(); }
};
public Type any2typevar() {
switch (this) {
case AnyType:
return TypeVar(this, new Constraint());
default:
return any2typevarMap.map(this);
}
}
// Closures and Least Upper Bounds ---------------------------------------------------
/** The closure of this type, i.e. the widened type itself followed by all
* its direct and indirect (pre-) base types, sorted by Symbol.isLess().
* Note that (pre-) base types do _not_ carry type parameters; these
* are added by baseType().
*/
public Type[] closure() {
switch (this.widen().unalias()) {
case TypeRef(Type pre, Symbol sym, Type[] args):
return subst(
asSeenFrom(sym.closure(), pre, sym.owner()),
sym.typeParams(), args);
case CompoundType(Type[] parts, Scope members):
Type[][] closures = new Type[parts.length][];
for (int i = 0; i < parts.length; i++)
closures[i] = parts[i].closure();
return union(closures);
default:
return new Type[]{this};
}
}
/** return union of array of closures
*/
static private Type[] union(Type[][] closures) {
if (closures.length == 1) return closures[0]; // fast special case
int[] index = new int[closures.length];
int totalsize = 0;
for (int i = 0; i < index.length; i++) {
index[i] = 0;
totalsize = totalsize + closures[i].length;
}
Type[] res = new Type[totalsize];
int j = 0;
while (true) {
// find minimal element
Type min = null;
for (int i = 0; i < index.length; i++) {
if (index[i] < closures[i].length) {
Type cltype = closures[i][index[i]];
if (min == null ||
cltype.symbol().isLess(min.symbol()) ||
cltype.symbol() == min.symbol() && cltype.isSubType(min)) {
min = cltype;
}
}
}
if (min == null) break;
res[j] = min;
j = j + 1;
// bump all indices that start with minimal element
for (int i = 0; i < index.length; i++) {
if (index[i] < closures[i].length &&
closures[i][index[i]].symbol() == min.symbol())
index[i] = index[i] + 1;
}
}
Type[] result = new Type[j];
System.arraycopy(res, 0, result, 0, j);
return result;
}
/** return intersection of array of closures
*/
static private Type[] intersection(Type[][] closures) {
if (closures.length == 1) return closures[0]; // fast special case
int[] index = new int[closures.length];
Type[] mintypes = new Type[closures.length];
int minsize = Integer.MAX_VALUE;
for (int i = 0; i < index.length; i++) {
index[i] = 0;
if (closures[i].length < minsize) minsize = closures[i].length;
}
Type[] res = new Type[minsize];
int j = 0;
L:
while (true) {
// find minimal element
Symbol min = null;
for (int i = 0; i < index.length; i++) {
if (index[i] == closures[i].length) break L;
Symbol clsym = closures[i][index[i]].symbol();
if (min == null || clsym.isLess(min)) min = clsym;
}
boolean agree = true;
// bump all indices that start with minimal element
for (int i = 0; i < index.length; i++) {
Type cltype = closures[i][index[i]];
if (cltype.symbol() == min) {
mintypes[i] = cltype;
index[i] = index[i] + 1;
} else {
agree = false;
}
}
if (agree) {
Type mintype;
mintype = commonType(mintypes);
if (mintype == NoType)
mintype = arglub(mintypes);
if (mintype.symbol().kind == CLASS) {
res[j] = mintype;
j = j + 1;
}
}
}
Type[] result = new Type[j];
System.arraycopy(res, 0, result, 0, j);
return result;
}
//todo: catch lubs not within bounds.
static Type arglub(Type[] types) {
Type pre = types[0].prefix();
Symbol sym = types[0].symbol();
Type[] args = new Type[sym.typeParams().length];
Type[][] argss = new Type[args.length][types.length];
for (int i = 0; i < types.length; i++) {
switch (types[i]) {
case TypeRef(Type pre1, Symbol sym1, Type[] args1):
assert sym == sym1;
assert args1.length == args.length;
if (!pre.isSameAs(pre1)) return NoType;
for (int j = 0; j < args1.length; j++)
argss[j][i] = args1[j];
case ErrorType:
return ErrorType;
default:
assert false : types[i];
}
}
for (int j = 0; j < args.length; j++) {
args[j] = commonType(argss[j]);
if (args[j] == NoType)
args[j] = CovarType(lub(argss[j]));
}
return TypeRef(pre, sym, args);
}
/** The frontier of a closure C is the minimal set of types such that
* the union of the closures of these types equals C.
*/
static private Type[] frontier(Type[] closure) {
Type[] front = new Type[closure.length];
int j = 0;
for (int i = 0; i < closure.length; i++) {
int k = 0;
Type tp = closure[i];
while (k < j && !front[k].symbol().isSubClass(tp.symbol()))
k++;
if (k == j) {
front[j] = tp;
j++;
}
}
Type[] result = new Type[j];
System.arraycopy(front, 0, result, 0, j);
return result;
}
/** Return the least upper bound of non-empty array of types `tps'.
* todo: do we need to consider refinements as well?
*/
public static Type lub(Type[] tps) {
//System.out.println("lub" + ArrayApply.toString(tps));//DEBUG
Type lubType = commonType(tps);
if (lubType != NoType) return lubType;
Type[][] closures = new Type[tps.length][];
for (int i = 0; i < tps.length; i++) {
if (!tps[i].isObjectType()) return Type.NoType;//todo: change
closures[i] = tps[i].closure();
}
Type[] allBaseTypes = intersection(closures);
Type[] leastBaseTypes = frontier(allBaseTypes);
if (leastBaseTypes.length == 0) return Type.NoType;
Scope members = new Scope();
lubType = compoundType(leastBaseTypes, members);
Type lubThisType = lubType.narrow();
//System.out.println("lubtype = " + lubType);//DEBUG
Symbol[] rsyms = new Symbol[tps.length];
Type[] rtps = new Type[tps.length];
for (int i = 0; i < allBaseTypes.length; i++) {
for (Scope.Entry e = allBaseTypes[i].members().elems;
e != Scope.Entry.NONE;
e = e.next) {
Name name = e.sym.name;
if ((e.sym.flags & PRIVATE) == 0 && lubType.lookup(name) == e.sym) {
//todo: not memberType?
Type symType = lubThisType.memberInfo(e.sym);
int j = 0;
while (j < tps.length) {
rsyms[j] = tps[j].lookupNonPrivate(name);
if (rsyms[j] == e.sym) break;
rtps[j] = tps[j].memberType(rsyms[j])
.substThis(tps[j].symbol(), lubThisType);
if (rtps[j].isSameAs(symType)) break;
j++;
}
if (j == tps.length) {
Symbol lubSym = lub(rsyms, rtps, lubType.symbol());
if (lubSym.kind != NONE && !lubSym.info().isSameAs(symType))
members.enter(lubSym);
}
}
}
}
//System.out.print("lub "); System.out.print(ArrayApply.toString(tps)); System.out.println(" = " + lubType);//DEBUG
if (leastBaseTypes.length == 1 && members.elems == Scope.Entry.NONE)
return leastBaseTypes[0];
else return lubType;
}
private static Type commonType(Type[] tps) {
Type tp = tps[0];
if (tp.isSameAsAll(tps)) return tp;
tp = tp.widen();
if (tp.isSameAsAll(widen(tps))) return tp;
return NoType;
}
private static Symbol lub(Symbol[] syms, Type[] tps, Symbol owner) {
//System.out.println("lub" + ArrayApply.toString(syms));//DEBUG
int lubKind = syms[0].kind;
for (int i = 1; i < syms.length; i++) {
Symbol sym = syms[i];
if (sym.kind == ERROR) return Symbol.NONE;
if (sym.isType() && sym.kind != lubKind) lubKind = TYPE;
}
if (lubKind == syms[0].kind && tps[0].isSameAsAll(tps)) {
return syms[0].cloneSymbol();
}
Type lubType = lub(tps);
if (lubType == Type.NoType) return Symbol.NONE;
Symbol lubSym;
switch (lubKind) {
case VAL:
lubSym = new TermSymbol(syms[0].pos, syms[0].name, owner, 0);
break;
case TYPE: case ALIAS: case CLASS:
lubSym = new TypeSymbol(TYPE, syms[0].pos, syms[0].name, owner, 0);
break;
default:
throw new ApplicationError();
}
lubSym.setInfo(lubType);
return lubSym;
}
// Erasure --------------------------------------------------------------------------
public static Map erasureMap = new MapOnlyTypes() {
public Type apply(Type t) { return t.erasure(); }
};
private static final Type[] unboxedType =
new Type[LastUnboxedTag + 1 - FirstUnboxedTag];
private static final Type[] unboxedArrayType =
new Type[LastUnboxedTag + 1 - FirstUnboxedTag];
private static final Name[] unboxedName =
new Name[LastUnboxedTag + 1 - FirstUnboxedTag];
private static final Name[] boxedName =
new Name[LastUnboxedTag + 1 - FirstUnboxedTag];
private static final Name[] boxedFullName =
new Name[LastUnboxedTag + 1 - FirstUnboxedTag];
private static void mkStdClassType(int kind, String unboxedstr, String boxedstr) {
unboxedType[kind - FirstUnboxedTag] = UnboxedType(kind);
unboxedArrayType[kind - FirstUnboxedTag] = UnboxedArrayType(unboxedType(kind));
unboxedName[kind - FirstUnboxedTag] = Name.fromString(unboxedstr);
boxedName[kind - FirstUnboxedTag] = Name.fromString(boxedstr);
boxedFullName[kind - FirstUnboxedTag] = Name.fromString("scala." + boxedstr);
}
static {
mkStdClassType(BYTE, "byte", "Byte");
mkStdClassType(SHORT, "short", "Short");
mkStdClassType(CHAR, "char", "Char");
mkStdClassType(INT, "int", "Int");
mkStdClassType(LONG, "long", "Long");
mkStdClassType(FLOAT, "float", "Float");
mkStdClassType(DOUBLE, "double", "Double");
mkStdClassType(BOOLEAN, "boolean", "Boolean");
mkStdClassType(UNIT, "void", "Unit");
}
/** Return unboxed type of given kind.
*/
public static Type unboxedType(int kind) {
return unboxedType[kind - FirstUnboxedTag];
}
/** Return unboxed array type of given element kind.
*/
public static Type unboxedArrayType(int kind) {
return unboxedArrayType[kind - FirstUnboxedTag];
}
/** Return the name of unboxed type of given kind.
*/
public static Name unboxedName(int kind) {
return unboxedName[kind - FirstUnboxedTag];
}
/** Return the name of boxed type of given kind.
*/
public static Name boxedName(int kind) {
return boxedName[kind - FirstUnboxedTag];
}
/** Return the full name of boxed type of given kind.
*/
public static Name boxedFullName(int kind) {
return boxedFullName[kind - FirstUnboxedTag];
}
/** If type is boxed, return its unboxed equivalent; otherwise return the type
* itself.
*/
public Type unbox() {
switch (this) {
case TypeRef(Type pre, Symbol sym, Type[] args):
if ((sym.flags & MODUL) == 0) {
Name fullname = sym.fullName();
if (fullname == Names.scala_Array && args.length == 1
/*&& args[0].unalias().symbol().kind != TYPE Q: why needed?*/) {
Global global = Global.instance;
Type bound = args[0].bound();
if (bound.symbol() != global.definitions.ANY_CLASS &&
bound.symbol() != global.definitions.ANYVAL_CLASS)
{
return UnboxedArrayType(args[0].erasure());
}
}
for (int i = 0; i < boxedFullName.length; i++) {
if (boxedFullName[i] == fullname) return unboxedType[i];
}
}
}
return this;
}
/** Return the erasure of this type.
*/
public Type erasure() {
switch (this) {
case ThisType(_):
case SingleType(_, _):
return widen().erasure();
case TypeRef(Type pre, Symbol sym, Type[] args):
switch (sym.kind) {
case ALIAS: case TYPE:
return pre.memberInfo(sym).erasure();
case CLASS:
if (Global.instance.definitions.UNIT_CLASS == sym) return this;
if (sym.fullName() == Names.java_lang_Object ||
sym.fullName() == Names.scala_AnyRef ||
sym.fullName() == Names.scala_AnyVal)
return Global.instance.definitions.ANY_TYPE;
else {
Type this1 = unbox();
if (this1 != this) return this1;
else if (args.length == 0) return this;
else return TypeRef(pre, sym, Type.EMPTY_ARRAY);
}
default: throw new ApplicationError();
}
case CompoundType(Type[] parents, _):
if (parents.length > 0) return parents[0].erasure();
else return this;
case CovarType(Type tp):
return tp.erasure(); // note: needed because of UnboxedArrayType
case MethodType(Symbol[] params, Type tp):
Symbol[] params1 = erasureMap.map(params);
Type tp1 = tp.fullErasure();
switch (tp1) {
case MethodType(Symbol[] params2, Type tp2):
Symbol[] newparams = new Symbol[params1.length + params2.length];
System.arraycopy(params1, 0, newparams, 0, params1.length);
System.arraycopy(params2, 0, newparams, params1.length, params2.length);
return MethodType(newparams, tp2);
default:
if (params1 == params && tp1 == tp) return this;
else return MethodType(params1, tp1);
}
case PolyType(_, Type result):
return result.erasure();
default:
return erasureMap.map(this);
}
}
/** Return the full erasure of the type. Full erasure is the same
* as "normal" erasure, except that the "Unit" type is erased to
* the "void" type.
*/
public Type fullErasure() {
if (Global.instance.definitions.UNIT_CLASS == symbol())
return unbox();
else
return erasure();
}
// Object Interface -----------------------------------------------------------------
public String toString() {
switch (this) {
case ErrorType:
return "<error>";
case AnyType:
return "<any type>";
case NoType:
return "<notype>";
case ThisType(Symbol sym):
if (sym.isRoot()) return "<root>.this.type";
else if (this == localThisType) return "<local>.this.type";
else {
Type this1 = (Global.instance.debug) ? this : expandModuleThis();
if (this1 == this) return sym.nameString() + ".this.type";
else return this1.toString();
}
case TypeRef(Type pre, Symbol sym, Type[] args):
if (sym.isRoot()) return "<root>";
if (!Global.instance.debug) {
if (isFunctionType()) {
Type[] params = new Type[args.length - 1];
System.arraycopy(args, 0, params, 0, params.length);
return ArrayApply.toString(params, "(", ",", ") => ") +
args[params.length].dropVariance();
} else if (sym.isAnonymousClass()) {
return "<template: " +
ArrayApply.toString(
pre.memberInfo(sym).parents(), "", "", " with ") +
"{...}>";
}
}
Type pre1 = (Global.instance.debug) ? pre : pre.expandModuleThis();
String result = pre1.prefixString() + sym.nameString() + sym.idString();
if (args.length != 0)
result = result + ArrayApply.toString(args, "[", ",", "]");
return result;
case SingleType(Type pre, Symbol sym):
if ((sym.flags & SYNTHETIC) != 0 && !Global.instance.debug)
return widen().toString();
if (sym.isRoot()) return "<root>.type";
Type pre1 = (Global.instance.debug) ? pre : pre.expandModuleThis();
return pre1.prefixString() + sym.nameString() + sym.idString() + ".type";
case CompoundType(Type[] parts, Scope members):
validate();//debug
StringBuffer buf = new StringBuffer();
if (parts.length > 0) {
buf.append(parts[0].toString());
int i = 1;
while (i < parts.length) {
buf.append(" with ");
buf.append(parts[i].toString());
i++;
}
}
boolean first = true;
for (Scope.SymbolIterator it = members.iterator(); it.hasNext(); ) {
Symbol sym = it.next();
buf.append(first ? " with {" : ", ");
first = false;
buf.append(sym.defString());
}
if (!first) buf.append("}");
return buf.toString();
case MethodType(Symbol[] vparams, Type result):
return ArrayApply.toString(paramTypeString(vparams), "(", ",", ")") + result;
case PolyType(Symbol[] tparams, Type result):
return ArrayApply.toString(Symbol.defString(tparams), "[", ",", "]") +
result;
case CovarType(Type tp):
return "+" + tp;
case OverloadedType(Symbol[] alts, Type[] alttypes):
return ArrayApply.toString(alttypes, "", " <and> ", "");
case TypeVar(Type origin, Constraint constr):
if (constr.inst != NoType) return constr.inst.toString();
else return origin + "?";
case UnboxedType(int kind):
return unboxedName(kind).toString();
case UnboxedArrayType(Type elemtp):
return elemtp.toString() + "[]";
case LazyType():
return "<lazy type " + getClass() + ">";
default:
return "<unknown type " + getClass() + ">";
}
}
public String toLongString() {
String str = toString();
if (str.endsWith(".type")) return str + " (with underlying type " + widen() + ")";
else return str;
}
private String prefixString() {
if ((this == localThisType || symbol().isRoot()) && !Global.instance.debug) {
return "";
} else {
String spre = toString();
if (spre.length() == 0)
return "";
else if (spre.endsWith(".type"))
return spre.substring(0, spre.length() - 4);
else
return spre + "@";
}
}
private String[] paramTypeString(Symbol[] vparams) {
String[] ss = new String[vparams.length];
for (int i = 0; i < ss.length; i++) {
Type tp = vparams[i].type();
if ((vparams[i].flags & REPEATED) != 0 &&
tp.symbol() == Global.instance.definitions.SEQ_CLASS &&
tp.typeArgs().length == 1)
ss[i] = tp.typeArgs()[0].toString() + "*";
else
ss[i] = tp.toString();
}
return ss;
}
public int hashCode() {
switch (this) {
case ErrorType:
return ERROR;
case NoType:
return NOtpe;
case ThisType(Symbol sym):
return THIStpe
^ (sym.hashCode() * 41);
case TypeRef(Type pre, Symbol sym, Type[] args):
return NAMEDtpe
^ (pre.hashCode() * 41)
^ (sym.hashCode() * (41*41))
^ (hashCode(args) * (41*41*41));
case SingleType(Type pre, Symbol sym):
return SINGLEtpe
^ (pre.hashCode() * 41)
^ (sym.hashCode() * (41*41));
case CompoundType(Type[] parts, Scope members):
return COMPOUNDtpe
^ (hashCode(parts) * 41)
^ (members.hashCode() * (41 * 41));
case MethodType(Symbol[] vparams, Type result):
return METHODtpe
^ (hashCode(Symbol.type(vparams)) * 41)
^ (result.hashCode() * (41 * 41));
case PolyType(Symbol[] tparams, Type result):
return POLYtpe
^ (hashCode(tparams) * 41)
^ (result.hashCode() * (41 * 41));
case CovarType(Type tp):
return COVARtpe
^ (tp.hashCode() * (41));
case OverloadedType(Symbol[] alts, Type[] alttypes):
return OVERLOADEDtpe
^ (hashCode(alts) * 41)
^ (hashCode(alttypes) * (41 * 41));
case UnboxedType(int kind):
return UNBOXEDtpe
^ (kind * 41);
case UnboxedArrayType(Type elemtp):
return UNBOXEDARRAYtpe
^ (elemtp.hashCode() * 41);
default:
throw new ApplicationError();
}
}
public static int hashCode(Object[] elems) {
int h = 0;
for (int i = 0; i < elems.length; i++)
h = h * 41 + elems[i].hashCode();
return h;
}
public static int hashCode(Scope.Entry elems) {
int h = 0;
for (Scope.Entry e = elems; e != Scope.Entry.NONE; e = e.next)
h = h * 41
+ e.sym.kind
+ e.sym.name.hashCode()
+ e.sym.info().hashCode();
return h;
}
// todo: change in relation to needs.
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Type) {
Type that = (Type) other;
switch (this) {
case ErrorType:
return that == ErrorType;
case NoType:
return that == NoType;
case ThisType(Symbol sym):
switch (that) {
case ThisType(Symbol sym1):
return sym == sym1;
default: return false;
}
case TypeRef(Type pre, Symbol sym, Type[] args):
switch (that) {
case TypeRef(Type pre1, Symbol sym1, Type[] args1):
return pre.equals(pre1) && sym == sym1 && equals(args, args1);
default: return false;
}
case SingleType(Type pre, Symbol sym):
switch (that) {
case SingleType(Type pre1, Symbol sym1):
return pre.equals(pre1) && sym == sym1;
default: return false;
}
case CompoundType(Type[] parts, Scope members):
switch (that) {
case CompoundType(Type[] parts1, Scope members1):
return parts.equals(parts1) && members.equals(members1);
default: return false;
}
case MethodType(Symbol[] vparams, Type result):
switch (that) {
case MethodType(Symbol[] vparams1, Type result1):
return equals(Symbol.type(vparams), Symbol.type(vparams1)) &&
result.equals(result1);
default: return false;
}
case PolyType(Symbol[] tparams, Type result):
switch (that) {
case PolyType(Symbol[] tparams1, Type result1):
return equals(tparams, tparams1) && result.equals(result1);
default: return false;
}
case CovarType(Type tp):
switch (that) {
case CovarType(Type tp1):
return tp.equals(tp1);
default: return false;
}
case OverloadedType(Symbol[] alts, Type[] alttypes):
switch (that) {
case OverloadedType(Symbol[] alts1, Type[] alttypes1):
return equals(alts, alts1) && equals(alttypes, alttypes1);
default: return false;
}
case UnboxedType(int kind):
switch (that) {
case UnboxedType(int kind1):
return kind == kind1;
default: return false;
}
case UnboxedArrayType(Type elemtp):
switch (that) {
case UnboxedArrayType(Type elemtp1):
return elemtp.equals(elemtp1);
default: return false;
}
default:
}
}
return false;
}
public static boolean equals(Object[] elems1, Object[] elems2) {
if (elems1.length != elems2.length) return false;
for (int i = 0; i < elems1.length; i++) {
if (!elems1[i].equals(elems2[i])) return false;
}
return true;
}
// Type.List class -----------------------------------------------------------------
/** A class for lists of types.
*/
public static class List {
public Type head;
public List tail;
public List(Type head, List tail) {
this.head = head; this.tail = tail;
}
public int length() {
return (this == EMPTY) ? 0 : 1 + tail.length();
}
public Type[] toArray() {
Type[] ts = new Type[length()];
copyToArray(ts, 0, 1);
return ts;
}
public void copyToArray(Type[] ts, int start, int delta) {
if (this != EMPTY) {
ts[start] = head;
tail.copyToArray(ts, start+delta, delta);
}
}
public Type[] toArrayReverse() {
Type[] ts = new Type[length()];
copyToArray(ts, ts.length - 1, -1);
return ts;
}
public static List EMPTY = new List(null, null);
public static List append(List l, Type tp) {
return (l == EMPTY) ? new List(tp, EMPTY)
: new List(l.head, append(l.tail, tp));
}
}
// Type.Constraint class -------------------------------------------------------
/** A class for keeping sub/supertype constraints and instantiations
* of type variables.
*/
public static class Constraint {
public List lobounds = List.EMPTY;
public List hibounds = List.EMPTY;
public Type inst = NoType;
public boolean instantiate(Type tp) {
for (List l = lobounds; l != List.EMPTY; l = l.tail) {
if (!l.head.isSubType(tp)) return false;
}
for (List l = hibounds; l != List.EMPTY; l = l.tail) {
if (!tp.isSubType(l.head)) return false;
}
inst = tp;
return true;
}
}
// Type.Error class --------------------------------------------------------------
/** A class for throwing type errors
*/
public static class Error extends java.lang.Error {
public String msg;
public Error(String msg) {
super(msg);
this.msg = msg;
}
}
/** A class for throwing type errors
*/
public static class VarianceError extends Error {
public VarianceError(String msg) {
super(msg);
}
}
}
/* A standard pattern match:
case ErrorType:
case AnyType:
case NoType:
case ThisType(Symbol sym):
case TypeRef(Type pre, Symbol sym, Type[] args):
case SingleType(Type pre, Symbol sym):
case CompoundType(Type[] parts, Scope members):
case MethodType(Symbol[] vparams, Type result):
case PolyType(Symbol[] tparams, Type result):
case OverloadedType(Symbol[] alts, Type[] alttypes):
case CovarType(Type tp):
*/
|