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
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
|
/* NSC -- new Scala compiler
* Copyright 2005-2006 LAMP/EPFL
* @author Martin Odersky
*/
// $Id$
package scala.tools.nsc.symtab
import compat.Platform.currentTime
import scala.collection.mutable.ListBuffer
import scala.tools.nsc.util.{HashSet, Position}
import Flags._
/* A standard type pattern match:
case ErrorType =>
case WildcardType =>
case NoType =>
case NoPrefix =>
case ThisType(_) =>
case SingleType(pre, sym) =>
case ConstantType(value) =>
case TypeRef(pre, sym, args) =>
case TypeBounds(lo, hi) =>
case RefinedType(parents, defs) =>
case ClassInfoType(parents, defs, clazz) =>
case MethodType(paramtypes, result) =>
case PolyType(tparams, result) =>
// the last three types are not used after phase `typer'.
case OverloadedType(pre, tparams, alts) =>
case AntiPolyType(pre: Type, targs) =>
case TypeVar(_, _) =>
case AttributedType(attribs, tp) =>
*/
trait Types requires SymbolTable {
import definitions._
//statistics
var singletonClosureCount = 0
var compoundClosureCount = 0
var typerefClosureCount = 0
var findMemberCount = 0
var noMemberCount = 0
var multMemberCount = 0
var findMemberMillis = 0l
var subtypeCount = 0
var subtypeMillis = 0l
private var explainSwitch = false
private var checkMalformedSwitch = true
private var globalVariance = 1//only necessary if healTypes = true?
private final val healTypes = false
private final val LubGlbMargin = 0
val emptyTypeArray = new Array[Type](0)
/** The base class for all types */
abstract class Type {
/** Add an attribute to this type */
def withAttribute(attrib: Any) = withAttributes(List(attrib))
/** Add a number of attributes to this type */
def withAttributes(attribs: List[Any]): Type =
attribs match {
case Nil => this
case _ => AttributedType(attribs, this)
}
/** Remove any attributes from this type */
def withoutAttributes = this
/** Types for which asSeenFrom always is the identity, no matter what
* prefix or owner.
*/
def isTrivial: boolean = false
/** The symbol associated with the type */
def symbol: Symbol = NoSymbol
/** The base type underlying a singleton type,
* identity on all other types */
def singleDeref: Type = this
/** Widen from singleton type to its underlying non-singleton base type
* by applying one or more singleDeref steps,
* identity for all other types */
def widen: Type = this
/** The type of <code>this</code> of a class type or reference type
*/
def typeOfThis = symbol.typeOfThis
/** Map to a this type which is a subtype of this type.
*/
def narrow: Type =
if (phase.erasedTypes) this
else refinedType(List(this), commonOwner(this), EmptyScope).narrow
/** Map a constant type to its underlying base type,
* identity for all other types */
def deconst: Type = this
/** For a TypeBounds type, itself;
* for a reference denoting an abstract type, its bounds,
* for all other types, a TypeBounds type all of whose bounds are this type.
*/
def bounds: TypeBounds = mkTypeBounds(this, this)
/** For a class or intersection type, its parents.
* For a TypeBounds type, the parents of its hi bound.
* inherited by typerefs, singleton types, and refinement types,
* The empty list for all other types */
def parents: List[Type] = List()
/** For a typeref or single-type, its prefix. NoType for all other types. */
def prefix: Type = NoType
/** For a typeref, its arguments. The empty list for all other types */
def typeArgs: List[Type] = List()
/** For a method or poly type, its direct result type,
* the type itself for all other types */
def resultType: Type = this
/** For a curried method or poly type its non-method result type,
* the type itself for all other types */
def finalResultType: Type = this
/** For a method or poly type, the number of its value parameter sections,
* 0 for all other types */
def paramSectionCount: int = 0
/** For a method or poly type, the types of its first value parameter section,
* the empty list for all other types */
def paramTypes: List[Type] = List()
/** For a poly type, its type parameters,
* the empty list for all other types */
def typeParams: List[Symbol] = List()
/** Is this type produced as a repair for an error? */
def isError: boolean = symbol.isError
/** Is this type produced as a repair for an error? */
def isErroneous: boolean = {
ErroneousTraverser.result = false
ErroneousTraverser.traverse(this)
ErroneousTraverser.result
}
/** Does this type denote a stable reference (i.e. singleton type)? */
def isStable: boolean = false
/** Does this type denote a reference type which can be null? */
// def isNullable: boolean = false
/** Is this type guaranteed to be non-null? */
// def isNonNull: boolean = false
/** For a classtype or refined type, its defined or declared members;
* inherited by subtypes and typerefs.
* The empty scope for all other types */
def decls: Scope = EmptyScope
/** The defined or declared members with name <code>name</code> in this type;
* an OverloadedSymbol if several exist, NoSymbol if none exist.
* Alternatives of overloaded symbol appear in the order they are declared.
*/
def decl(name: Name): Symbol = findDecl(name, 0)
/** The non-private defined or declared members with name `name' in this type;
* an OverloadedSymbol if several exist, NoSymbol if none exist.
* Alternatives of overloaded symbol appear in the order they are declared.
*/
def nonPrivateDecl(name: Name): Symbol = findDecl(name, PRIVATE)
/** A list of all members of this type (defined or inherited)
* Members appear in linearization order of their owners.
* Members with the same owner appear in reverse order of their declarations.
*/
def members: List[Symbol] = findMember(nme.ANYNAME, 0, 0, false).alternatives
/** A list of all non-private members of this type (defined or inherited) */
def nonPrivateMembers: List[Symbol] =
findMember(nme.ANYNAME, PRIVATE | BRIDGE, 0, false).alternatives
/** A list of all implicit symbols of this type (defined or inherited) */
def implicitMembers: List[Symbol] =
findMember(nme.ANYNAME, BRIDGE, IMPLICIT, false).alternatives
/** The member with given name,
* an OverloadedSymbol if several exist, NoSymbol if none exist */
def member(name: Name): Symbol = findMember(name, BRIDGE, 0, false)
/** The non-private member with given name,
* an OverloadedSymbol if several exist, NoSymbol if none exist */
def nonPrivateMember(name: Name): Symbol =
findMember(name, PRIVATE | BRIDGE, 0, false)
/** The non-local member with given name,
* an OverloadedSymbol if several exist, NoSymbol if none exist */
def nonLocalMember(name: Name): Symbol =
findMember(name, LOCAL | BRIDGE, 0, false)
/** The least type instance of given class which is a supertype
* of this type */
def baseType(clazz: Symbol): Type = NoType
/** This type as seen from prefix <code>pre</code> and class
* <code>clazz</code>. This means:
* Replace all thistypes of <code>clazz</code> or one of its subclasses
* by <code>pre</code> and instantiate all parameters by arguments of
* <code>pre</code>.
* Proceed analogously for thistypes referring to outer classes.
*/
def asSeenFrom(pre: Type, clazz: Symbol): Type =
if (!isTrivial && (!phase.erasedTypes || pre.symbol == ArrayClass)) {
new AsSeenFromMap(pre, clazz) apply this
} else this
/** The info of <code>sym</code>, seen as a member of this type.
*/
def memberInfo(sym: Symbol): Type =
sym.info.asSeenFrom(this, sym.owner)
/** The type of `sym', seen as a member of this type. */
def memberType(sym: Symbol): Type = {
sym.tpe match {
case ov @ OverloadedType(pre, alts) =>
OverloadedType(this, alts)
/*
val pre1 = pre match {
case ClassInfoType(_, _, clazz) => clazz.tpe
case _ => pre
}
if (this =:= pre1) ov
else if (this =:= pre1.narrow) OverloadedType(this, alts)
else {
Console.println("bad memberType of overloaded symbol: "+this+"/"+pre1+"/"+pre1.narrow)
assert(false)
ov
}
*/
case _ =>
//Console.println("" + this + ".memberType(" + sym +":" + sym.tpe +")" + sym.ownerChain);//DEBUG
sym.tpe.asSeenFrom(this, sym.owner)
}
}
/** Substitute types <code>to</code> for occurrences of references to
* symbols <code>from</code> in this type.
*/
def subst(from: List[Symbol], to: List[Type]): Type =
new SubstTypeMap(from, to) apply this
/** Substitute symbols <code>to</code> for occurrences of symbols
* <code>from</code> in this type.
*/
def substSym(from: List[Symbol], to: List[Symbol]): Type =
new SubstSymMap(from, to) apply this
/** Substitute all occurrences of <code>ThisType(from)</code> in this type
* by <code>to</code>.
*/
def substThis(from: Symbol, to: Type): Type =
new SubstThisMap(from, to) apply this
def substSuper(from: Type, to: Type): Type =
new SubstSuperMap(from, to) apply this
/** Does this type contain a reference to this symbol? */
def contains(sym: Symbol): boolean =
new ContainsTraverser(sym).traverse(this).result
/** Is this type a subtype of that type? */
def <:<(that: Type): boolean = {
if (util.Statistics.enabled) subtypeCount = subtypeCount + 1
val startTime = if (util.Statistics.enabled) currentTime else 0l
val result =
((this eq that) ||
(if (explainSwitch) explain("<", isSubType, this, that)
else isSubType(this, that)));
if (util.Statistics.enabled)
subtypeMillis = subtypeMillis + currentTime - startTime
result
}
/** Is this type equivalent to that type? */
def =:=(that: Type): boolean = (
(this eq that) ||
(if (explainSwitch) explain("=", isSameType, this, that)
else isSameType(this, that))
);
/** Does this type implement symbol <code>sym</code> with same or stronger type?
*/
def specializes(sym: Symbol): boolean =
if (explainSwitch) explain("specializes", specializesSym, this, sym)
else specializesSym(this, sym)
/** Is this type close enough to that type so that
* members with the two type would override each other?
* This means:
* - Either both types are polytypes with the same number of
* type parameters and their result types match after renaming
* corresponding type parameters
* - Or both types are method types with equivalent type parameter types
* and matching result types
* - Or both types are equivalent
* - Or phase.erasedTypes is false and both types are neither method nor
* poly types.
*/
def matches(that: Type): boolean = matchesType(this, that)
/** The shortest sorted upwards closed array of types that contains
* this type as first element.
*
* A list or array of types ts is upwards closed if
*
* for all t in ts:
* for all typerefs p.s[args] such that t <: p.s[args]
* there exists a typeref p'.s[args'] in ts such that
* t <: p'.s['args] <: p.s[args],
* and
* for all singleton types p.s such that t <: p.s
* there exists a singleton type p'.s in ts such that
* t <: p'.s <: p.s
*
* Sorting is with respect to Symbol.isLess() on type symbols.
*/
def closure: Array[Type] = Array(this)
/** The maximum depth (@see maxDepth) of each type in the closure of this type. */
def closureDepth: int = 1
def baseClasses: List[Symbol] = List()
/**
* @param sym the class symbol
* @return the index of given class symbol in the closure of this type,
* or -1 if no base type with given class symbol exists.
*/
def closurePos(sym: Symbol): int = {
val cl = closure
var lo = 0
var hi = cl.length - 1
while (lo <= hi) {
val mid = (lo + hi) / 2
val clsym = cl(mid).symbol
if (sym == clsym) return mid
else if (sym isLess clsym) hi = mid - 1
else if (clsym isLess sym) lo = mid + 1
else throw new Error()
}
-1
}
/** If this is a polytype, a copy with cloned type parameters owned
* by <code>owner</code>. Identity for all other types.
*/
def cloneInfo(owner: Symbol) = this
protected def objectPrefix = "object "
protected def packagePrefix = "package "
def trimPrefix(str: String) =
if (str.startsWith(objectPrefix)) str.substring(objectPrefix.length)
else if (str.startsWith(packagePrefix)) str.substring(packagePrefix.length)
else str
/** The string representation of this type used as a prefix */
def prefixString = trimPrefix(toString()) + "#"
/** The string representation of this type, with singletypes explained */
def toLongString = {
val str = toString()
if (str endsWith ".type") str + " (with underlying type " + widen + ")"
else str
}
/** Is this type completed (i.e. not a lazy type)?
*/
def isComplete: boolean = true
/** If this is a lazy type, assign a new type to <code>sym</code>. */
def complete(sym: Symbol) = {}
/** If this is a symbol loader type, load and assign a new type to
* <code>sym</code>.
*/
def load(sym: Symbol): unit = {}
private def findDecl(name: Name, excludedFlags: int): Symbol = {
var alts: List[Symbol] = List()
var sym: Symbol = NoSymbol
var e: ScopeEntry = decls.lookupEntry(name)
while (e ne null) {
if (!e.sym.hasFlag(excludedFlags)) {
if (sym == NoSymbol) sym = e.sym
else {
if (alts.isEmpty) alts = List(sym)
alts = e.sym :: alts
}
}
e = decls.lookupNextEntry(e)
}
if (alts.isEmpty) sym
else baseClasses.head.newOverloaded(this, alts)
}
/**
* @param name ...
* @param excludedFlags ...
* @param requiredFlags ...
* @param stableOnly ...
* @return ...
*/
//TODO: use narrow only for modules? (correct? efficiency gain?)
def findMember(name: Name, excludedFlags: int, requiredFlags: long, stableOnly: boolean): Symbol = {
if (util.Statistics.enabled) findMemberCount = findMemberCount + 1
val startTime = if (util.Statistics.enabled) currentTime else 0l
//Console.println("find member " + name.decode + " in " + this + ":" + this.baseClasses)//DEBUG
var members: Scope = null
var member: Symbol = NoSymbol
var excluded = excludedFlags | DEFERRED
var self: Type = null
var continue = true
var savedCheckMalformedSwitch = checkMalformedSwitch
checkMalformedSwitch = false
while (continue) {
continue = false
var bcs = baseClasses
while (!bcs.isEmpty) {
val decls = bcs.head.info.decls
bcs = if (name == nme.CONSTRUCTOR) Nil else bcs.tail
var entry =
if (name == nme.ANYNAME) decls.elems else decls lookupEntry name
while (entry ne null) {
val sym = entry.sym
if (sym.getFlag(requiredFlags) == requiredFlags) {
val excl = sym.getFlag(excluded)
if (excl == 0) {
if (name.isTypeName || stableOnly) {
checkMalformedSwitch = savedCheckMalformedSwitch
if (util.Statistics.enabled)
findMemberMillis = findMemberMillis + currentTime - startTime
return sym
} else if (member == NoSymbol) {
member = sym
} else if (members eq null) {
if (member.name != sym.name ||
!(member == sym ||
member.owner != sym.owner &&
!sym.hasFlag(PRIVATE) && {
if (self eq null) self = this.narrow;
(self.memberType(member) matches self.memberType(sym))
})) {
members = newScope(List(member, sym))
}
} else {
var prevEntry = members lookupEntry sym.name
while ((prevEntry ne null) &&
!(prevEntry.sym == sym ||
prevEntry.sym.owner != sym.owner &&
!sym.hasFlag(PRIVATE) && {
if (self eq null) self = this.narrow;
(self.memberType(prevEntry.sym) matches self.memberType(sym))
})) {
prevEntry = members lookupNextEntry prevEntry
}
if (prevEntry eq null) {
members enter sym
}
}
} else if (excl == DEFERRED) {
continue = true
}
}
entry = if (name == nme.ANYNAME) entry.next else decls lookupNextEntry entry
} // while (entry ne null)
// excluded = excluded | LOCAL
} // while (!bcs.isEmpty)
excluded = excludedFlags
} // while (continue)
checkMalformedSwitch = savedCheckMalformedSwitch
if (util.Statistics.enabled)
findMemberMillis = findMemberMillis + currentTime - startTime
if (members eq null) {
if (util.Statistics.enabled) if (member == NoSymbol) noMemberCount = noMemberCount + 1;
member
} else {
if (util.Statistics.enabled) multMemberCount = multMemberCount + 1;
//val pre = if (this.symbol.isClass) this.symbol.thisType else this;
baseClasses.head.newOverloaded(this, members.toList)
}
}
}
// Subclasses ------------------------------------------------------------
trait UniqueType {
private val hashcode = { val h = super.hashCode(); if (h < 0) -h else h }
override def hashCode() = hashcode
}
/** A base class for types that defer some operations
* to their immediate supertype.
*/
abstract class SubType extends Type {
def supertype: Type
override def parents: List[Type] = supertype.parents
override def decls: Scope = supertype.decls
override def baseType(clazz: Symbol): Type = supertype.baseType(clazz)
override def closure: Array[Type] = supertype.closure
override def closureDepth: int = supertype.closureDepth
override def baseClasses: List[Symbol] = supertype.baseClasses
// override def isNonNull = supertype.isNonNull
}
/** A base class for types that represent a single value
* (single-types and this-types).
*/
abstract class SingletonType extends SubType {
override def singleDeref: Type
def supertype: Type = singleDeref
override def isStable: boolean = true
override def widen: Type = singleDeref.widen
override def closure: Array[Type] = {
if (util.Statistics.enabled) singletonClosureCount = singletonClosureCount + 1
addClosure(this, supertype.closure)
}
override def toString(): String = prefixString + "type"
}
/** An object representing an erroneous type */
case object ErrorType extends Type {
// todo see whether we can do without
override def isError: boolean = true
override def decls: Scope = new ErrorScope(NoSymbol)
override def findMember(name: Name, excludedFlags: int,
requiredFlags: long, stableOnly: boolean): Symbol = {
var sym = decls lookup name
if (sym == NoSymbol) {
sym = NoSymbol.newErrorSymbol(name)
decls enter sym
}
sym
}
override def baseType(clazz: Symbol): Type = this
override def toString(): String = "<error>"
override def narrow: Type = this
// override def isNullable: boolean = true
}
/** An object representing an unknown type */
case object WildcardType extends Type {
override def toString(): String = "?"
// override def isNullable: boolean = true
}
case class BoundedWildcardType(override val bounds: TypeBounds) extends Type {
override def toString(): String = "?" + bounds
}
/** An object representing a non-existing type */
case object NoType extends Type {
override def isTrivial: boolean = true
override def toString(): String = "<notype>"
// override def isNullable: boolean = true
}
/** An object representing a non-existing prefix */
case object NoPrefix extends Type {
override def isTrivial: boolean = true
override def isStable: boolean = true
override def prefixString = ""
override def toString(): String = "<noprefix>"
// override def isNullable: boolean = true
}
/** A class for this-types of the form <sym>.this.type
*/
case class ThisType(sym: Symbol) extends SingletonType {
//assert(sym.isClass && !sym.isModuleClass || sym.isRoot, sym)
override def isTrivial: boolean = sym.isPackageClass
// override def isNonNull = true
override def symbol = sym
override def singleDeref: Type = sym.typeOfThis
override def prefixString =
if (settings.debug.value) sym.nameString + ".this."
else if (sym.isRoot || sym.isEmptyPackageClass || sym.isInterpreterWrapper) ""
else if (sym.isAnonymousClass || sym.isRefinementClass) "this."
else if (sym.isPackageClass) sym.fullNameString + "."
else sym.nameString + ".this."
override def toString(): String =
if (sym.isRoot) "<root>"
else if (sym.isEmptyPackageClass) "<empty>"
else super.toString()
override def narrow: Type = this
}
/** A class for singleton types of the form <prefix>.<sym.name>.type.
* Cannot be created directly; one should always use
* <code>singleType</code> for creation.
*/
case class SingleType(pre: Type, sym: Symbol) extends SingletonType {
override val isTrivial: boolean = pre.isTrivial
// override def isNullable = supertype.isNullable
// override def isNonNull = supertype.isNonNull
private var singleDerefCache: Type = _
private var singleDerefPeriod = NoPeriod
override def singleDeref: Type = {
val period = singleDerefPeriod
if (period != currentPeriod) {
singleDerefPeriod = currentPeriod
if (!isValid(period)) {
singleDerefCache = pre.memberType(sym).resultType;
}
}
singleDerefCache
}
override def narrow: Type = {
if (phase.erasedTypes) this
else {
val thissym = refinedType(List(this), sym.owner, EmptyScope).symbol
if (sym.owner != NoSymbol) {
//Console.println("narrowing module " + sym + thissym.owner);
thissym.typeOfThis = this
}
thissym.thisType
}
}
override def symbol = sym
override def prefix: Type = pre
override def prefixString: String =
if ((sym.isEmptyPackage || sym.isInterpreterWrapper) && !settings.debug.value) ""
else pre.prefixString + sym.nameString + "."
}
case class SuperType(thistpe: Type, supertp: Type) extends SingletonType {
override val isTrivial: boolean = thistpe.isTrivial && supertp.isTrivial
// override def isNonNull = true;
override def symbol = thistpe.symbol
override def singleDeref = supertp
override def prefix: Type = supertp.prefix
override def prefixString =
if (thistpe.prefixString.endsWith("this."))
thistpe.prefixString.substring(0, thistpe.prefixString.length() - 5) + "super."
else thistpe.prefixString;
override def narrow: Type = thistpe.narrow
}
/** A class for the bounds of abstract types and type parameters
*/
case class TypeBounds(lo: Type, hi: Type) extends SubType {
override val isTrivial: boolean = lo.isTrivial && hi.isTrivial
def supertype: Type = hi
override def bounds: TypeBounds = this
def containsType(that: Type) = that <:< this || lo <:< that && that <:< hi;
// override def isNullable: boolean = AllRefClass.tpe <:< lo;
override def toString() = ">: " + lo + " <: " + hi
}
/** A common base class for intersection types and class types
*/
abstract class CompoundType extends Type {
private var closureCache: Array[Type] = _
private var closurePeriod = NoPeriod
private var baseClassesCache: List[Symbol] = _
private var baseClassesPeriod = NoPeriod
private var closureDepthCache: int = _
override def closure: Array[Type] = {
def computeClosure: Array[Type] =
try {
if (util.Statistics.enabled)
compoundClosureCount = compoundClosureCount + 1
//Console.println("computing closure of " + symbol.tpe + " " + parents)//DEBUG
val buf = new ListBuffer[Type]
buf += symbol.tpe
var clSize = 1
val nparents = parents.length
if (nparents != 0) {
val pclosure = new Array[Array[Type]](nparents)
val index = new Array[int](nparents)
var i = 0
for (val p <- parents) {
pclosure(i) = if (p.closure eq null) AnyClass.info.closure // cyclic reference
else p.closure
index(i) = 0
i = i + 1
}
def nextBaseType(i: int): Type = {
val j = index(i)
val pci = pclosure(i)
if (j < pci.length) pci(j) else AnyClass.tpe
}
var minSym: Symbol = NoSymbol
while (minSym != AnyClass) {
minSym = nextBaseType(0).symbol
i = 1
while (i < nparents) {
if (nextBaseType(i).symbol isLess minSym)
minSym = nextBaseType(i).symbol
i = i + 1
}
var minTypes: List[Type] = List()
i = 0
while (i < nparents) {
val tp = nextBaseType(i)
if (tp.symbol == minSym) {
if (!(minTypes exists (tp =:=))) minTypes = tp :: minTypes;
index(i) = index(i) + 1
}
i = i + 1
}
buf += intersectionType(minTypes)
clSize = clSize + 1
}
}
closureCache = new Array[Type](clSize)
buf.copyToArray(closureCache, 0)
//Console.println("closureCache of " + symbol.tpe + " = " + List.fromArray(closureCache))//DEBUG
var j = 0
while (j < clSize) {
closureCache(j) match {
case RefinedType(parents, decls) =>
assert(decls.isEmpty)
//Console.println("compute closure of "+this+" => glb("+parents+")")
closureCache(j) = mergePrefixAndArgs(parents, -1, maxClosureDepth(parents) + LubGlbMargin) match {
case Some(tp0) => tp0
case None => throw new MalformedClosure(parents)
}
assert(!closureCache(j).isInstanceOf[RefinedType], closureCache(j))
case _ =>
}
j = j + 1
}
//Console.println("closure of " + symbol.tpe + " = " + List.fromArray(closureCache))//DEBUG
closureCache
} catch {
case ex: MalformedClosure =>
throw new MalformedType(
"the type intersection " + this + " is malformed" +
"\n --- because ---\n" + ex.getMessage())
}
val period = closurePeriod;
if (period != currentPeriod) {
closurePeriod = currentPeriod
if (!isValidForBaseClasses(period)) {
closureCache = null
closureCache = computeClosure
closureDepthCache = maxDepth(closureCache)
}
//Console.println("closure(" + symbol + ") = " + List.fromArray(closureCache));//DEBUG
}
if (closureCache eq null)
throw new TypeError("illegal cyclic reference involving " + symbol)
closureCache
}
override def closureDepth: int = { closure; closureDepthCache }
override def baseClasses: List[Symbol] = {
def computeBaseClasses: List[Symbol] =
if (parents.isEmpty) List(symbol)
else {
//Console.println("computing base classes of " + symbol + " at phase " + phase);//DEBUG
// optimized, since this seems to be performance critical
val superclazz = parents.head
var mixins = parents.tail
val sbcs = superclazz.baseClasses
var bcs = sbcs
def isNew(clazz: Symbol): boolean = (
superclazz.closurePos(clazz) < 0 &&
{ var p = bcs;
while ((p ne sbcs) && (p.head != clazz)) p = p.tail;
p eq sbcs
}
);
while (!mixins.isEmpty) {
def addMixinBaseClasses(mbcs: List[Symbol]): List[Symbol] =
if (mbcs.isEmpty) bcs
else if (isNew(mbcs.head)) mbcs.head :: addMixinBaseClasses(mbcs.tail)
else addMixinBaseClasses(mbcs.tail);
bcs = addMixinBaseClasses(mixins.head.baseClasses)
mixins = mixins.tail
}
symbol :: bcs
}
val period = baseClassesPeriod
if (period != currentPeriod) {
baseClassesPeriod = currentPeriod
if (!isValidForBaseClasses(period)) {
baseClassesCache = null
baseClassesCache = computeBaseClasses
}
}
if (baseClassesCache eq null)
throw new TypeError("illegal cyclic reference involving " + symbol)
baseClassesCache
}
override def baseType(sym: Symbol): Type = {
val index = closurePos(sym)
if (index >= 0) closure(index) else NoType
}
override def narrow: Type = symbol.thisType
// override def isNonNull: boolean = parents forall (.isNonNull);
// override def isNullable: boolean =
// parents forall (p => p.isNullable && !p.symbol.isAbstractType);
override def toString(): String = (
parents.mkString("", " with ", "") +
(if (settings.debug.value || parents.isEmpty || (decls.elems ne null))
decls.mkString("{", "; ", "}") else "")
);
}
/** A class representing intersection types with refinements of the form
* <code><parents_0> with ... with <parents_n> { decls }</code>
* Cannot be created directly;
* one should always use <code>refinedType</code> for creation.
*/
case class RefinedType(override val parents: List[Type],
override val decls: Scope) extends CompoundType
/** A class representing a class info
*/
case class ClassInfoType(
override val parents: List[Type],
override val decls: Scope,
override val symbol: Symbol) extends CompoundType {
// override def isNullable: boolean =
// symbol == AnyClass ||
// symbol != AllClass && (symbol isSubClass ObjectClass) && !(symbol isSubClass NonNullClass);
// override def isNonNull: boolean = symbol == NonNullClass || super.isNonNull;
}
class PackageClassInfoType(decls: Scope, clazz: Symbol)
extends ClassInfoType(List(), decls, clazz)
/** A class representing a constant type.
*
* @param value ...
*/
case class ConstantType(value: Constant) extends SingletonType {
assert(value.tpe.symbol != UnitClass)
override def isTrivial: boolean = true
override def symbol: Symbol = value.tpe.symbol
override def singleDeref: Type = value.tpe
override def deconst: Type = value.tpe
override def toString(): String =
value.tpe.toString() + "(" + value.escapedStringValue + ")"
// override def isNullable: boolean = value.value eq null
// override def isNonNull: boolean = value.value ne null
}
/** A class for named types of the form
* <code><prefix>.<sym.name>[args]</code>
* Cannot be created directly; one should always use <code>typeRef</code>
* for creation.
*
* @param pre ...
* @param sym ...
* @param args ...
*/
case class TypeRef(pre: Type, sym: Symbol, args: List[Type]) extends Type {
assert(!checkMalformedSwitch || !sym.isAbstractType || pre.isStable || pre.isError)
assert(!pre.isInstanceOf[ClassInfoType], this)
assert(!sym.isTypeParameterOrSkolem || pre == NoPrefix, this)
private var parentsCache: List[Type] = _
private var parentsPeriod = NoPeriod
private var closureCache: Array[Type] = _
private var closurePeriod = NoPeriod
private var closureDepthCache: int = _
override val isTrivial: boolean =
pre.isTrivial && !sym.isTypeParameter && args.forall(.isTrivial)
def transform(tp: Type): Type =
tp.asSeenFrom(pre, sym.owner).subst(sym.typeParams, args)
def thisInfo = if (sym.isAbstractType) transform(sym.info) else sym.info
def transform(cl: Array[Type]): Array[Type] = {
val cl1 = new Array[Type](cl.length)
var i = 0
while (i < cl.length) { cl1(i) = transform(cl(i)); i = i + 1 }
cl1
}
override def symbol = sym
override def bounds: TypeBounds =
if (sym.isAbstractType) transform(thisInfo.bounds).asInstanceOf[TypeBounds]
else super.bounds
override def parents: List[Type] = {
val period = parentsPeriod
if (period != currentPeriod) {
parentsPeriod = currentPeriod
if (!isValidForBaseClasses(period)) {
parentsCache = thisInfo.parents map transform
}
}
parentsCache
}
override def typeOfThis = transform(sym.typeOfThis)
override def narrow =
if (sym.isModuleClass) transform(sym.thisType) else super.narrow
override def prefix: Type = pre
override def typeArgs: List[Type] = args
override def typeParams: List[Symbol] =
if (args.isEmpty) symbol.unsafeTypeParams else List()
override def decls: Scope = {
sym.info match {
case TypeRef(_, sym1, _) =>
assert(sym1 != symbol, this)
case _ =>
}
thisInfo.decls
}
override def baseType(clazz: Symbol): Type =
if (sym == clazz) this
else if (sym.isClass) transform(sym.info.baseType(clazz))
else pre.memberInfo(sym).baseType(clazz)
override def closure: Array[Type] = {
val period = closurePeriod
if (period != currentPeriod) {
closurePeriod = currentPeriod
if (!isValidForBaseClasses(period)) {
if (util.Statistics.enabled)
typerefClosureCount = typerefClosureCount + 1
closureCache =
if (sym.isAbstractType) addClosure(this, transform(bounds.hi).closure)
else transform(sym.info.closure)
closureDepthCache = maxDepth(closureCache)
}
}
closureCache
}
override def closureDepth: int = { closure; closureDepthCache }
override def baseClasses: List[Symbol] = thisInfo.baseClasses
// override def isNullable: boolean = sym.info.isNullable
override def toString(): String = {
if (!settings.debug.value) {
if (sym == RepeatedParamClass && !args.isEmpty)
return args(0).toString() + "*"
if (sym == ByNameParamClass && !args.isEmpty)
return "=> " + args(0).toString()
if (isFunctionType(this))
return args.init.mkString("(", ", ", ")") + " => " + args.last
if (isTupleType(this))
return args.mkString("{", ", ", if (args.length == 1) ",}" else "}")
}
val str = (pre.prefixString + sym.nameString +
(if (args.isEmpty) "" else args.mkString("[", ",", "]")))
if (sym.isPackageClass)
packagePrefix + str
else if (sym.isModuleClass)
objectPrefix + str
else if (sym.isAnonymousClass && sym.isInitialized && !settings.debug.value)
thisInfo.parents.mkString("", " with ", "{ ... }")
else if (sym.isRefinementClass && sym.isInitialized)
thisInfo.toString()
else str
}
override def prefixString =
if (settings.debug.value) super.prefixString
else if (sym.isRoot || sym.isEmptyPackageClass || sym.isInterpreterWrapper ||
sym.isAnonymousClass || sym.isRefinementClass) ""
else if (sym.isPackageClass) sym.fullNameString + "."
else super.prefixString
}
/** A class representing a method type with parameters.
*/
case class MethodType(override val paramTypes: List[Type],
override val resultType: Type) extends Type {
override val isTrivial: boolean =
paramTypes.forall(.isTrivial) && resultType.isTrivial
//assert(paramTypes forall (pt => !pt.symbol.isImplClass))//DEBUG
override def paramSectionCount: int = resultType.paramSectionCount + 1
override def finalResultType: Type = resultType.finalResultType
override def toString(): String =
paramTypes.mkString("(", ",", ")") + resultType
}
class ImplicitMethodType(pts: List[Type], rt: Type) extends MethodType(pts, rt) {
override def toString(): String =
paramTypes.mkString("(implicit ", ",", ")") + resultType
}
class JavaMethodType(pts: List[Type], rt: Type) extends MethodType(pts, rt)
/** A class representing a polymorphic type or, if tparams.length == 0,
* a parameterless method type.
*/
case class PolyType(override val typeParams: List[Symbol], override val resultType: Type)
extends Type {
override def paramSectionCount: int = resultType.paramSectionCount
override def paramTypes: List[Type] = resultType.paramTypes
override def finalResultType: Type = resultType.finalResultType
override def parents: List[Type] = resultType.parents
override def decls: Scope = resultType.decls
override def symbol: Symbol = resultType.symbol
override def closure: Array[Type] = resultType.closure
override def closureDepth: int = resultType.closureDepth
override def baseClasses: List[Symbol] = resultType.baseClasses
override def baseType(clazz: Symbol): Type = resultType.baseType(clazz)
override def narrow: Type = resultType.narrow
// override def isNullable: boolean = resultType.isNullable;
override def toString(): String =
(if (typeParams.isEmpty) "=> "
else (typeParams map (.defString)).mkString("[", ",", "]")) + resultType
override def cloneInfo(owner: Symbol) = {
val tparams = cloneSymbols(typeParams, owner)
PolyType(tparams, resultType.substSym(typeParams, tparams))
}
}
/** A class containing the alternatives and type prefix of an overloaded symbol.
* Not used after phase `typer'.
*/
case class OverloadedType(pre: Type, alternatives: List[Symbol]) extends Type {
override def prefix: Type = pre
override def toString() =
(alternatives map pre.memberType).mkString("", " <and> ", "")
}
/** A class remembering a type instantiation for some a set of overloaded
* polymorphic symbols.
* Not used after phase `typer'.
*/
case class AntiPolyType(pre: Type, targs: List[Type]) extends Type {
override def toString() =
pre.toString() + targs.mkString("(with type arguments ", ",", ")");
override def memberType(sym: Symbol) = pre.memberType(sym) match {
case PolyType(tparams, restp) => restp.subst(tparams, targs)
case ErrorType => ErrorType
}
}
/** A class representing a type variable
* Not used after phase `typer'.
*/
case class TypeVar(origin: Type, constr: TypeConstraint) extends Type {
override def symbol = origin.symbol
override def toString(): String =
if (constr.inst eq null) "<null " + origin + ">"
else if (constr.inst eq NoType) "?" + origin
else constr.inst.toString;
}
/** A type carrying some attributes. The attributes have no significance
* to the core compiler, but can be observed by type-system plugins. The
* core compiler does take care to propagate attributes and to save them
* in the symbol tables of object files. */
case class AttributedType(attributes: List[Any], tp: Type) extends Type {
override def toString(): String = {
val attString =
if(attributes.isEmpty)
""
else
attributes.mkString("[", "] [", "] ")
attString + tp
}
/** Add a number of attributes to this type */
override def withAttributes(attribs: List[Any]): Type =
AttributedType(attribs:::this.attributes, this)
/** Remove any attributes from this type */
override def withoutAttributes = tp.withoutAttributes
override def isTrivial: boolean = tp.isTrivial
/** Return the argument, unless it is eq to tp, in which case return the
* receiver. This method is used for methods like singleDeref and widen,
* so that the attributes are remembered more frequently. */
private def maybeRewrap(newtp: Type) =
if(newtp eq tp) this else newtp
// ---------------- methods forwarded to tp ------------------ \\
override def symbol: Symbol = tp.symbol
override def singleDeref: Type = maybeRewrap(tp.singleDeref)
override def widen: Type = maybeRewrap(tp.widen)
override def deconst: Type = maybeRewrap(tp.deconst)
override def bounds: TypeBounds = {
val oftp = tp.bounds
oftp match {
case TypeBounds(lo, hi) if((lo eq this) && (hi eq this)) => mkTypeBounds(this,this)
case _ => oftp
}
}
override def parents: List[Type] = tp.parents
override def prefix: Type = tp.prefix
override def typeArgs: List[Type] = tp.typeArgs
override def resultType: Type = maybeRewrap(tp.resultType)
override def finalResultType: Type = maybeRewrap(tp.finalResultType)
override def paramSectionCount: int = tp.paramSectionCount
override def paramTypes: List[Type] = tp.paramTypes
override def typeParams: List[Symbol] = tp.typeParams
override def isStable: boolean = tp.isStable
override def nonPrivateDecl(name: Name): Symbol = tp.nonPrivateDecl(name)
override def baseType(clazz: Symbol): Type = tp.baseType(clazz)
override def closure: Array[Type] = {
val oftp = tp.closure
if((oftp.length == 1 &&) (oftp(0) eq this))
Array(this)
else
oftp
}
override def closureDepth: int = tp.closureDepth
override def baseClasses: List[Symbol] = tp.baseClasses
override def cloneInfo(owner: Symbol) = maybeRewrap(cloneInfo(owner))
override def isComplete: boolean = tp.isComplete
override def complete(sym: Symbol) = tp.complete(sym)
override def load(sym: Symbol): unit = tp.load(sym)
override def findMember(name: Name, excludedFlags: int, requiredFlags: long, stableOnly: boolean): Symbol =
tp.findMember(name, excludedFlags, requiredFlags, stableOnly)
}
/** A class representing an as-yet unevaluated type.
*/
abstract class LazyType extends Type {
override def isComplete: boolean = false
override def complete(sym: Symbol): unit
override def toString = "LazyType"
}
/** A class representing a lazy type with known type parameters.
*/
class LazyPolyType(override val typeParams: List[Symbol], restp: Type) extends LazyType {
override def complete(sym: Symbol): unit = {
restp.complete(sym)
}
}
// Creators ---------------------------------------------------------------
/** Rebind symbol <code>sym</code> to an overriding member in type
* <code>pre</code>.
*/
private def rebind(pre: Type, sym: Symbol): Symbol = {
val owner = sym.owner
if (owner.isClass && owner != pre.symbol && !sym.isFinal && !sym.isClass) {
//Console.println("rebind "+pre+" "+sym)//DEBUG
val rebind = pre.nonPrivateMember(sym.name).suchThat(sym => sym.isType || sym.isStable)
if (rebind == NoSymbol) sym else rebind
} else sym
}
/** Convert a <code>super</code> prefix to a this-type if <code>sym</code>
* is abstract or final.
*/
private def removeSuper(tp: Type, sym: Symbol): Type = tp match {
case SuperType(thistp, _) =>
if (sym.isFinal || sym.hasFlag(DEFERRED)) thistp
else tp
case _ =>
tp
}
/** The canonical creator for this-types */
def mkThisType(sym: Symbol): Type =
if (phase.erasedTypes) sym.tpe else unique(new ThisType(sym) with UniqueType)
/** The canonical creator for single-types */
def singleType(pre: Type, sym: Symbol): Type = singleType(pre, sym, 0)
/** The canonical creator for single-types,
* with possibility of approximating in case of malformed types */
def singleType(pre: Type, sym: Symbol, variance: int): Type = {
if (phase.erasedTypes)
sym.tpe.resultType
else if (sym.isRootPackage)
mkThisType(RootClass)
else {
var sym1 = rebind(pre, sym)
val pre1 = removeSuper(pre, sym1)
if (pre1 ne pre) sym1 = rebind(pre1, sym1)
if (checkMalformedSwitch && !pre1.isStable && !pre1.isError) {
if (healTypes && variance == 1) pre.memberType(sym).resultType
// else if (variance == -1) AllClass.tpe
else throw new MalformedType(pre, sym.nameString)
} else {
unique(new SingleType(pre1, sym1) with UniqueType)
}
}
}
/** The canonical creator for super-types */
def mkSuperType(thistp: Type, supertp: Type): Type =
if (phase.erasedTypes) supertp
else unique(new SuperType(thistp, supertp) with UniqueType)
/** The canonical creator for type bounds */
def mkTypeBounds(lo: Type, hi: Type): TypeBounds =
unique(new TypeBounds(lo, hi) with UniqueType)
def refinementOfClass(clazz: Symbol, parents: List[Type], decls: Scope) =
new RefinedType(parents, decls) { override def symbol: Symbol = clazz }
/** the canonical creator for a refined type with a given scope */
def refinedType(parents: List[Type], owner: Symbol, decls: Scope): Type = {
if (phase.erasedTypes)
if (parents.isEmpty) ObjectClass.tpe else parents.head
else {
val clazz = owner.newRefinementClass(NoPos)
val result = refinementOfClass(clazz, parents, decls)
clazz.setInfo(result)
result
}
}
/** The canonical creator for a refined type with an initially empty scope.
*
* @param parents ...
* @param owner ...
* @return ...
*/
def refinedType(parents: List[Type], owner: Symbol): Type =
refinedType(parents, owner, newScope)
def copyRefinedType(original: RefinedType, parents: List[Type], decls: Scope) =
if ((parents eq original.parents) && (decls eq original.decls)) original
else {
val result = refinedType(parents, original.symbol.owner)
val syms1 = decls.toList
for (val sym <- syms1)
result.decls.enter(sym.cloneSymbol(result.symbol))
val syms2 = result.decls.toList
val resultThis = result.symbol.thisType
for (val sym <- syms2)
sym.setInfo(sym.info.substSym(syms1, syms2).substThis(original.symbol, resultThis))
result
}
/** the canonical creator for a constant type */
def mkConstantType(value: Constant): ConstantType =
unique(new ConstantType(value) with UniqueType)
/** The canonical creator for typerefs. */
def typeRef(pre: Type, sym: Symbol, args: List[Type]): Type = typeRef(pre, sym, args, 0)
/** The canonical creator for typerefs,
* with possibility of approximating in case of malformed types */
def typeRef(pre: Type, sym: Symbol, args: List[Type], variance: int): Type = {
var sym1 = if (sym.isAbstractType) rebind(pre, sym) else sym
if (sym1.isAliasType && sym1.info.typeParams.length == args.length) {
// note: we require that object is initialized,
// that's why we use info.typeParams instead of typeParams.
if (sym1.hasFlag(LOCKED))
throw new TypeError("illegal cyclic reference involving " + sym1)
sym1.setFlag(LOCKED)
val result = sym1.info.resultType.asSeenFrom(pre, sym1.owner).subst(sym1.typeParams, args)
sym1.resetFlag(LOCKED)
result
} else {
val pre1 = removeSuper(pre, sym1)
if (pre1 ne pre) {
if (sym1.isAbstractType) sym1 = rebind(pre1, sym1)
typeRef(pre1, sym1, args, variance)
} else if (checkMalformedSwitch && sym1.isAbstractType && !pre.isStable && !pre.isError) {
def transform(tp: Type): Type =
tp.asSeenFrom(pre, sym1.owner).subst(sym1.typeParams, args)
if (healTypes && variance == 1 && !(sym1.info.bounds.hi contains sym1)) transform(sym1.info.bounds.hi)
//else if (variance == -1 && !(sym1.info.bounds.lo contains sym1)) transform(sym1.info.bounds.lo)
else throw new MalformedType(pre, sym1.nameString)
} else {
rawTypeRef(pre, sym1, args)
}
}
}
/** create a type-ref as found, without checks or rebinds */
def rawTypeRef(pre: Type, sym: Symbol, args: List[Type]): Type =
unique(new TypeRef(pre, sym, args) with UniqueType)
/** The canonical creator for implicit method types */
def ImplicitMethodType(paramTypes: List[Type], resultType: Type): ImplicitMethodType =
new ImplicitMethodType(paramTypes, resultType) // don't unique this!
/** The canonical creator for implicit method types */
def JavaMethodType(paramTypes: List[Type], resultType: Type): JavaMethodType =
new JavaMethodType(paramTypes, resultType) // don't unique this!
/** A creator for intersection type where intersections of a single type are
* replaced by the type itself, and repeated parent classes are merged.
*/
def intersectionType(tps: List[Type], owner: Symbol): Type = tps match {
case List(tp) =>
tp
case _ =>
refinedType(tps, owner)
/*
def merge(tps: List[Type]): List[Type] = tps match {
case tp :: tps1 =>
val tps1a = tps1 filter (.symbol.==(tp.symbol))
val tps1b = tps1 filter (.symbol.!=(tp.symbol))
mergePrefixAndArgs(tps1a, -1) match {
case Some(tp1) => tp1 :: merge(tps1b)
case None => throw new MalformedType(
"malformed type: "+refinedType(tps, owner)+" has repeated parent class "+
tp.symbol+" with incompatible prefixes or type arguments")
}
case _ => tps
}
refinedType(merge(tps), owner)
*/
}
/** A creator for intersection type where intersections of a single type are
* replaced by the type itself. */
def intersectionType(tps: List[Type]): Type = tps match {
case List(tp) => tp
case _ => refinedType(tps, commonOwner(tps))
}
/** A creator for type applications */
def appliedType(tycon: Type, args: List[Type]): Type = tycon match {
case TypeRef(pre, sym, _) => typeRef(pre, sym, args)
case PolyType(tparams, restpe) => restpe.subst(tparams, args)
case ErrorType => tycon
case _ =>
Console.println(tycon.getClass())
Console.println(tycon.$tag())
throw new Error()
}
// Hash consing --------------------------------------------------------------
private var uniques: HashSet[AnyRef] = _
private var uniqueRunId = NoRunId
def uniqueTypeCount = uniques.size // for statistics
private def unique[T <: AnyRef](tp: T): T = {
if (uniqueRunId != currentRunId) {
uniques = new HashSet(20000)
uniqueRunId = currentRunId
}
val tp1 = uniques.findEntry(tp)
if (tp1 eq null) {
uniques.addEntry(tp); tp
} else {
tp1.asInstanceOf[T]
}
}
// Helper Classes ---------------------------------------------------------
/** A class expressing upper and lower bounds constraints
* for type variables, as well as their instantiations */
class TypeConstraint(lo: List[Type], hi: List[Type]) {
def this() = this(List(), List())
var lobounds: List[Type] = lo
var hibounds: List[Type] = hi
var inst: Type = NoType
def instantiate(tp: Type): boolean =
if (lobounds.forall(.<:<(tp)) && hibounds.forall(tp.<:<)) {
inst = tp; true
} else false
override def toString() =
lobounds.mkString("[ _>:(", ",", ") ") +
hibounds.mkString("| _<:(", ",", ") | _= ") + inst
}
/** A prototype for mapping a function over all possible types
*/
abstract class TypeMap extends Function1[Type, Type] {
// deferred inherited: def apply(tp: Type): Type
var variance = globalVariance
/** Map this function over given type */
def mapOver(tp: Type): Type = tp match {
case ErrorType => tp
case WildcardType => tp
case NoType => tp
case NoPrefix => tp
case ThisType(_) => tp
case ConstantType(_) => tp
case SingleType(pre, sym) =>
if (sym.isPackageClass) tp // short path
else {
val v = variance; variance = 0
val pre1 = this(pre)
variance = v
if (pre1 eq pre) tp
else singleType(pre1, sym, variance)
}
case SuperType(thistp, supertp) =>
val thistp1 = this(thistp)
val supertp1 = this(supertp)
if ((thistp1 eq thistp) && (supertp1 eq supertp)) tp
else mkSuperType(thistp1, supertp1)
case TypeRef(pre, sym, args) =>
val pre1 = this(pre)
//val args1 = List.mapConserve(args)(this)
val args1 = if (args.isEmpty) args
else {
val tparams = sym.typeParams
if (tparams.isEmpty) args
else mapOverArgs(args, tparams)
}
if ((pre1 eq pre) && (args1 eq args)) tp
else typeRef(pre1, sym, args1, variance)
case TypeBounds(lo, hi) =>
variance = -variance
val lo1 = this(lo)
variance = -variance
val hi1 = this(hi)
if ((lo1 eq lo) && (hi1 eq hi)) tp
else mkTypeBounds(lo1, hi1)
case BoundedWildcardType(bounds) =>
val bounds1 = this(bounds)
if (bounds1 eq bounds) tp
else BoundedWildcardType(bounds1.asInstanceOf[TypeBounds])
case rtp @ RefinedType(parents, decls) =>
val parents1 = List.mapConserve(parents)(this)
val decls1 = mapOver(decls)
//if ((parents1 eq parents) && (decls1 eq decls)) tp
//else refinementOfClass(tp.symbol, parents1, decls1)
copyRefinedType(rtp, parents1, decls1)
/*
case ClassInfoType(parents, decls, clazz) =>
val parents1 = List.mapConserve(parents)(this);
val decls1 = mapOver(decls);
if ((parents1 eq parents) && (decls1 eq decls)) tp
else cloneDecls(ClassInfoType(parents1, new Scope(), clazz), tp, decls1)
*/
case MethodType(paramtypes, result) =>
variance = -variance
val paramtypes1 = List.mapConserve(paramtypes)(this)
variance = -variance
val result1 = this(result)
if ((paramtypes1 eq paramtypes) && (result1 eq result)) tp
else if (tp.isInstanceOf[ImplicitMethodType]) ImplicitMethodType(paramtypes1, result1)
else if (tp.isInstanceOf[JavaMethodType]) JavaMethodType(paramtypes1, result1)
else MethodType(paramtypes1, result1)
case PolyType(tparams, result) =>
variance = -variance
val tparams1 = mapOver(tparams)
variance = -variance
var result1 = this(result)
if ((tparams1 eq tparams) && (result1 eq result)) tp
else PolyType(tparams1, result1.substSym(tparams, tparams1))
case OverloadedType(pre, alts) =>
val pre1 = if (pre.isInstanceOf[ClassInfoType]) pre else this(pre)
if (pre1 eq pre) tp
else OverloadedType(pre1, alts)
case AntiPolyType(pre, args) =>
val pre1 = this(pre)
val args1 = List.mapConserve(args)(this)
if ((pre1 eq pre) && (args1 eq args)) tp
else AntiPolyType(pre1, args1)
case TypeVar(_, constr) =>
if (constr.inst != NoType) this(constr.inst)
else tp
case AttributedType(attribs, atp) =>
val atp1 = this(atp)
if(atp1 eq atp)
tp
else
AttributedType(attribs, atp1)
case _ =>
tp
// throw new Error("mapOver inapplicable for " + tp);
}
def mapOverArgs(args: List[Type], tparams: List[Symbol]): List[Type] =
map2Conserve(args, tparams) { (arg, tparam) =>
val v = variance
if (tparam.isContravariant) variance = -variance
else if (!tparam.isCovariant) variance = 0
val arg1 = this(arg)
variance = v
arg1
}
/** Map this function over given scope */
private def mapOver(scope: Scope): Scope = {
val elems = scope.toList
val elems1 = mapOver(elems)
if (elems1 eq elems) scope
else newScope(elems1)
}
/** Map this function over given list of symbols */
def mapOver(syms: List[Symbol]): List[Symbol] = {
val infos = syms map (.info)
val infos1 = List.mapConserve(infos)(this)
if (infos1 eq infos) syms
else {
val syms1 = syms map (.cloneSymbol)
List.map2(syms1, infos1) {
((sym1, info1) => sym1.setInfo(info1.substSym(syms, syms1)))
}
}
}
}
abstract class TypeTraverser extends TypeMap {
def traverse(tp: Type): TypeTraverser //todo: return unit instead?
def apply(tp: Type): Type = { traverse(tp); tp }
}
/** A map to compute the asSeenFrom method */
class AsSeenFromMap(pre: Type, clazz: Symbol) extends TypeMap {
/** Return pre.baseType(clazz), or if that's NoType and clazz is a refinement, pre itself.
* See bug397.scala for an example where the second alternative is needed.
* The problem is that when forming the closure of an abstract type,
* any refinements in the base type list might be regenerated, and thus acquire
* new class symbols. However, since refinements always have non-interesting prefixes
* it looks OK to me to just take the prefix directly. */
def base(pre: Type, clazz: Symbol) = {
val b = pre.baseType(clazz)
if (b == NoType && clazz.isRefinementClass) pre
else b
}
def apply(tp: Type): Type =
if ((pre eq NoType) || (pre eq NoPrefix) || !clazz.isClass) tp
else tp match {
case ThisType(sym) =>
def toPrefix(pre: Type, clazz: Symbol): Type =
if ((pre eq NoType) || (pre eq NoPrefix) || !clazz.isClass) tp
else if ((sym isNonBottomSubClass clazz) &&
(pre.widen.symbol isNonBottomSubClass sym))
pre match {
case SuperType(thistp, _) => thistp
case _ => pre
}
else toPrefix(base(pre, clazz).prefix, clazz.owner);
toPrefix(pre, clazz)
case TypeRef(prefix, sym, args) if (sym.isTypeParameter) =>
def toInstance(pre: Type, clazz: Symbol): Type =
if ((pre eq NoType) || (pre eq NoPrefix) || !clazz.isClass) tp
else {
val symclazz = sym.owner;
def throwError =
throw new Error("" + tp + " in " + symclazz +
" cannot be instantiated from " + pre.widen);
def instParam(ps: List[Symbol], as: List[Type]): Type =
if (ps.isEmpty) throwError
else if (sym eq ps.head) as.head
else instParam(ps.tail, as.tail);
if (symclazz == clazz && (pre.widen.symbol isNonBottomSubClass symclazz))
pre.baseType(symclazz) match {
case TypeRef(_, basesym, baseargs) =>
//Console.println("instantiating " + sym + " from " + basesym + " with " + basesym.typeParams + " and " + baseargs+", pre = "+pre+", symclazz = "+symclazz);//DEBUG
if (basesym.typeParams.length != baseargs.length)
throw new TypeError(
"something is wrong (wrong class file?): "+basesym+
" with type parameters "+
basesym.typeParams.map(.name).mkString("[",",","]")+
" gets applied to arguments "+baseargs.mkString("(",",",")")+", phase = "+phase)
instParam(basesym.typeParams, baseargs);
case _ =>
throwError
}
else toInstance(base(pre, clazz).prefix, clazz.owner)
}
toInstance(pre, clazz)
case _ =>
mapOver(tp)
}
}
/** A base class to compute all substitutions */
abstract class SubstMap[T](from: List[Symbol], to: List[T]) extends TypeMap {
/** Are <code>sym</code> and <code>sym1</code> the same.
* Can be tuned by subclasses.
*/
protected def matches(sym: Symbol, sym1: Symbol): boolean = sym eq sym1
/** Map target to type, can be tuned by subclasses */
protected def toType(fromtp: Type, t: T): Type
def apply(tp: Type): Type = {
def subst(sym: Symbol, from: List[Symbol], to: List[T]): Type =
if (from.isEmpty) tp
else if (matches(from.head, sym)) toType(tp, to.head)
else subst(sym, from.tail, to.tail)
tp match {
case TypeRef(NoPrefix, sym, _) =>
subst(sym, from, to)
case SingleType(NoPrefix, sym) =>
subst(sym, from, to)
case PolyType(tparams, restp) =>
assert(!(tparams exists (from contains)))
mapOver(tp)
case _ =>
mapOver(tp)
}
}
}
/** A map to implement the <code>substSym</code> method. */
class SubstSymMap(from: List[Symbol], to: List[Symbol])
extends SubstMap(from, to) {
protected def toType(fromtp: Type, sym: Symbol) = fromtp match {
case TypeRef(pre, _, args) => typeRef(pre, sym, args, variance)
case SingleType(pre, _) => singleType(pre, sym, variance)
}
override def apply(tp: Type): Type = {
def subst(sym: Symbol, from: List[Symbol], to: List[Symbol]): Symbol =
if (from.isEmpty) sym
else if (matches(from.head, sym)) to.head
else subst(sym, from.tail, to.tail)
tp match {
case TypeRef(pre, sym, args) if !(pre eq NoPrefix) =>
mapOver(typeRef(pre, subst(sym, from, to), args, variance))
case SingleType(pre, sym) if !(pre eq NoPrefix) =>
mapOver(singleType(pre, subst(sym, from, to), variance))
case _ =>
super.apply(tp)
}
}
}
/** A map to implement the <code>subst</code> method. */
class SubstTypeMap(from: List[Symbol], to: List[Type])
extends SubstMap(from, to) {
protected def toType(fromtp: Type, tp: Type) = tp
}
/** A map to implement the <code>substThis</code> method. */
class SubstThisMap(from: Symbol, to: Type) extends TypeMap {
def apply(tp: Type): Type = tp match {
case ThisType(sym) if (sym == from) => to
case _ => mapOver(tp)
}
}
class SubstSuperMap(from: Type, to: Type) extends TypeMap {
def apply(tp: Type): Type = if (tp eq from) to else mapOver(tp)
}
/** A map to convert every occurrence of a wildcard type to a fresh
* type variable */
object wildcardToTypeVarMap extends TypeMap {
def apply(tp: Type): Type = tp match {
case WildcardType =>
TypeVar(tp, new TypeConstraint)
case BoundedWildcardType(bounds) =>
TypeVar(tp, new TypeConstraint(List(bounds.lo), List(bounds.hi)))
case _ =>
mapOver(tp)
}
}
/** A map to implement the <code>contains</code> method */
class ContainsTraverser(sym: Symbol) extends TypeTraverser {
var result = false
def traverse(tp: Type): ContainsTraverser = {
if (!result) {
tp match {
case TypeRef(_, sym1, _) if (sym == sym1) => result = true
case SingleType(_, sym1) if (sym == sym1) => result = true
case _ => mapOver(tp)
}
}
this
}
}
/** A map to implement the <code>contains</code> method */
object ErroneousTraverser extends TypeTraverser {
var result: boolean = _
def traverse(tp: Type): TypeTraverser = {
if (!result) {
result = tp.isError
mapOver(tp)
}
this
}
}
/** A map to compute the most deeply nested owner that contains all the symbols
* of thistype or prefixless typerefs/singletype occurrences in given type.
*/
object commonOwnerMap extends TypeMap {
var result: Symbol = _
def init = { result = NoSymbol }
def apply(tp: Type): Type = {
assert(tp ne null)
tp match {
case ThisType(sym) =>
register(sym)
case TypeRef(NoPrefix, sym, args) =>
register(sym.owner); args foreach {arg => apply(arg); ()}
case SingleType(NoPrefix, sym) =>
register(sym.owner)
case _ =>
mapOver(tp)
}
tp
}
private def register(sym: Symbol): unit = {
while (result != NoSymbol && sym != result && !(sym isNestedIn result))
result = result.owner;
}
}
object adaptToNewRunMap extends TypeMap {
private def adaptToNewRun(pre: Type, sym: Symbol): Symbol = {
if (sym.isModuleClass && !phase.flatClasses) {
adaptToNewRun(pre, sym.sourceModule).moduleClass
} else if ((pre eq NoPrefix) || (pre eq NoType) || sym.owner.isPackageClass) {
sym
} else {
var rebind0 = pre.findMember(sym.name, BRIDGE, 0, true)
/** The two symbols have the same fully qualified name */
def corresponds(sym1: Symbol, sym2: Symbol): boolean =
sym1.name == sym2.name && (sym1.isPackageClass || corresponds(sym1.owner, sym2.owner))
assert(sym != NoSymbol)
if (rebind0 == NoSymbol) assert(false, ""+pre+"."+sym+" does no longer exist, phase = "+phase)
if (!corresponds(sym.owner, rebind0.owner)) {
if (settings.debug.value) Console.println("ADAPT1 pre = "+pre+", sym = "+sym+sym.locationString+", rebind = "+rebind0+rebind0.locationString)
val bcs = pre.baseClasses.dropWhile(bc => !corresponds(bc, sym.owner));
assert(!bcs.isEmpty)
rebind0 = pre.baseType(bcs.head).member(sym.name)
if (settings.debug.value) Console.println("ADAPT2 pre = "+pre+", bcs.head = "+bcs.head+", sym = "+sym+sym.locationString+", rebind = "+rebind0+(if (rebind0 == NoSymbol) "" else rebind0.locationString))
}
val rebind = rebind0.suchThat(sym => sym.isType || sym.isStable)
if (rebind == NoSymbol) {
if (settings.debug.value) Console.println("" + phase + " " + phase.flatClasses+sym.owner+sym.name)
throw new MalformedType(pre, sym.nameString)
}
rebind
}
}
def apply(tp: Type): Type = tp match {
case ThisType(sym) if (sym.isModuleClass) =>
val sym1 = adaptToNewRun(sym.owner.thisType, sym)
if (sym1 == sym) tp else mkThisType(sym1)
case SingleType(pre, sym) =>
if (sym.isPackage) tp
else {
val pre1 = this(pre)
val sym1 = adaptToNewRun(pre1, sym)
if ((pre1 eq pre) && (sym1 eq sym)) tp
else singleType(pre1, sym1, variance)
}
case TypeRef(pre, sym, args) =>
if (sym.isPackageClass) tp
else {
val pre1 = this(pre)
val args1 = List.mapConserve(args)(this)
val sym1 = adaptToNewRun(pre1, sym)
if ((pre1 eq pre) && (sym1 eq sym) && (args1 eq args)/* && sym.isExternal*/) tp
else typeRef(pre1, sym1, args1, variance)
}
case PolyType(tparams, restp) =>
val restp1 = this(restp)
if (restp1 eq restp) tp
else PolyType(tparams, restp1)
case ClassInfoType(parents, decls, clazz) =>
if (clazz.isPackageClass) tp
else {
val parents1 = List.mapConserve(parents)(this)
if (parents1 eq parents) tp
else ClassInfoType(parents1, decls, clazz)
}
case RefinedType(parents, decls) =>
val parents1 = List.mapConserve(parents)(this)
if (parents1 eq parents) tp
else refinedType(parents1, tp.symbol.owner, decls)
case SuperType(_, _) => mapOver(tp)
case TypeBounds(_, _) => mapOver(tp)
case MethodType(_, _) => mapOver(tp)
case TypeVar(_, _) => mapOver(tp)
case AttributedType(_,_) => mapOver(tp)
case _ => tp
}
}
// Helper Methods -------------------------------------------------------------
import Math.max
/** The maximum depth of all types in the closures of each of the types `tps' */
final def maxClosureDepth(tps: Seq[Type]): int = {
var d = 0
for (val tp <- tps) d = max(d, tp.closureDepth)
d
}
/** The maximum depth of all types `tps' */
final def maxDepth(tps: Seq[Type]): int = {
var d = 0
for (val tp <- tps) d = max(d, maxDepth(tp))
d
}
/** The maximum depth of type `tp' */
final def maxDepth(tp: Type): int = tp match {
case TypeRef(pre, sym, args) =>
max(maxDepth(pre), maxDepth(args) + 1)
case RefinedType(parents, decls) =>
max(maxDepth(parents), maxDepth(decls.toList.map(.info)) + 1)
case TypeBounds(lo, hi) =>
max(maxDepth(lo), maxDepth(hi))
case MethodType(paramtypes, result) =>
maxDepth(result)
case PolyType(tparams, result) =>
max(maxDepth(result), maxDepth(tparams map (.info)) + 1)
case _ =>
1
}
final def isValid(period: Period): boolean =
period != 0 && runId(period) == currentRunId && {
val pid = phaseId(period)
if (phase.id > pid) infoTransformers.nextFrom(pid).pid >= phase.id
else infoTransformers.nextFrom(phase.id).pid >= pid
}
final def isValidForBaseClasses(period: Period): boolean = {
def noChangeInBaseClasses(it: InfoTransformer, limit: Phase#Id): boolean = (
it.pid >= limit ||
!it.changesBaseClasses && noChangeInBaseClasses(it.next, limit)
);
period != 0 && runId(period) == currentRunId && {
val pid = phaseId(period)
if (phase.id > pid) noChangeInBaseClasses(infoTransformers.nextFrom(pid), phase.id)
else noChangeInBaseClasses(infoTransformers.nextFrom(phase.id), pid)
}
}
/** Do <code>tp1</code> and <code>tp2</code> denote equivalent types?
*
* @param tp1 ...
* @param tp2 ...
* @return true, iff <code>tp1</code> and <code>tp2</code> denote
* equivalent types.
*/
def isSameType(tp1: Type, tp2: Type): boolean = {
Pair(tp1, tp2) match {
case Pair(ErrorType, _) => true
case Pair(WildcardType, _) => true
case Pair(_, ErrorType) => true
case Pair(_, WildcardType) => true
case Pair(NoType, _) => false
case Pair(NoPrefix, _) => tp2.symbol.isPackageClass
case Pair(_, NoType) => false
case Pair(_, NoPrefix) => tp1.symbol.isPackageClass
case Pair(ThisType(sym1), ThisType(sym2))
if (sym1 == sym2) =>
true
case Pair(SingleType(pre1, sym1), SingleType(pre2, sym2))
if ((sym1 == sym2) && (pre1 =:= pre2)) =>
true
/*
case Pair(SingleType(pre1, sym1), ThisType(sym2))
if (sym1.isModule &&
sym1.moduleClass == sym2 &&
pre1 =:= sym2.owner.thisType) =>
true
case Pair(ThisType(sym1), SingleType(pre2, sym2))
if (sym2.isModule &&
sym2.moduleClass == sym1 &&
pre2 =:= sym1.owner.thisType) =>
true
*/
case Pair(ConstantType(value1), ConstantType(value2)) =>
value1 == value2
case Pair(TypeRef(pre1, sym1, args1), TypeRef(pre2, sym2, args2)) =>
sym1 == sym2 && (phase.erasedTypes || pre1 =:= pre2) && isSameTypes(args1, args2)
case Pair(RefinedType(parents1, ref1), RefinedType(parents2, ref2)) =>
def isSubScope(s1: Scope, s2: Scope): boolean = s2.toList.forall {
sym2 =>
val sym1 = s1.lookup(sym2.name)
sym1 != NoSymbol &&
sym1.info =:= sym2.info.substThis(sym2.owner, sym1.owner.thisType)
}
//Console.println("is same? " + tp1 + " " + tp2 + " " + tp1.symbol.owner + " " + tp2.symbol.owner)//DEBUG
isSameTypes(parents1, parents2) && isSubScope(ref1, ref2) && isSubScope(ref2, ref1)
case Pair(MethodType(pts1, res1), MethodType(pts2, res2)) =>
(pts1.length == pts2.length &&
isSameTypes(pts1, pts2) &&
res1 =:= res2 &&
tp1.isInstanceOf[ImplicitMethodType] == tp2.isInstanceOf[ImplicitMethodType])
case Pair(PolyType(tparams1, res1), PolyType(tparams2, res2)) =>
(tparams1.length == tparams2.length &&
List.forall2(tparams1, tparams2)
((p1, p2) => p1.info =:= p2.info.substSym(tparams2, tparams1)) &&
res1 =:= res2.substSym(tparams2, tparams1))
case Pair(TypeBounds(lo1, hi1), TypeBounds(lo2, hi2)) =>
lo1 =:= lo2 && hi1 =:= hi2
case Pair(BoundedWildcardType(bounds), _) =>
bounds containsType tp2
case Pair(_, BoundedWildcardType(bounds)) =>
bounds containsType tp1
case Pair(TypeVar(_, constr1), _) =>
if (constr1.inst != NoType) constr1.inst =:= tp2
else constr1 instantiate (wildcardToTypeVarMap(tp2))
case Pair(_, TypeVar(_, constr2)) =>
if (constr2.inst != NoType) tp1 =:= constr2.inst
else constr2 instantiate (wildcardToTypeVarMap(tp1))
case Pair(AttributedType(_,atp), _) =>
isSameType(atp, tp2)
case Pair(_, AttributedType(_,atp)) =>
isSameType(tp1, atp)
case _ =>
if (tp1.isStable && tp2.isStable) {
var origin1 = tp1
while (origin1.singleDeref.isStable) origin1 = origin1.singleDeref
var origin2 = tp2
while (origin2.singleDeref.isStable) origin2 = origin2.singleDeref
((origin1 ne tp1) || (origin2 ne tp2)) && (origin1 =:= origin2)
} else false
}
}
/** Are <code>tps1</code> and <code>tps2</code> lists of pairwise equivalent
* types?
*/
def isSameTypes(tps1: List[Type], tps2: List[Type]): boolean =
tps1.length == tps2.length &&
List.forall2(tps1, tps2)((tp1, tp2) => tp1 =:= tp2)
private var stc = 0
def isSubType(tp1: Type, tp2: Type): boolean =
try {
stc = stc + 1
if (stc == 100) throw new Error("recursive <:<")
isSubType0(tp1, tp2)
} finally {
stc = stc - 1
}
/** Does type <code>tp1</code> conform to <code>tp2</code>?
*
* @param tp1 ...
* @param tp2 ...
* @return ...
*/
def isSubType0(tp1: Type, tp2: Type): boolean = {
Pair(tp1, tp2) match {
case Pair(ErrorType, _) => true
case Pair(WildcardType, _) => true
case Pair(_, ErrorType) => true
case Pair(_, WildcardType) => true
case Pair(NoType, _) => false
case Pair(NoPrefix, _) => tp2.symbol.isPackageClass
case Pair(_, NoType) => false
case Pair(_, NoPrefix) => tp1.symbol.isPackageClass
case Pair(ThisType(_), ThisType(_)) => tp1 =:= tp2
case Pair(ThisType(_), SingleType(_, _)) => tp1 =:= tp2
case Pair(SingleType(_, _), ThisType(_)) => tp1 =:= tp2
case Pair(SingleType(_, _), SingleType(_, _)) => tp1 =:= tp2
case Pair(ConstantType(_), ConstantType(_)) => tp1 =:= tp2
case Pair(TypeRef(pre1, sym1, args1), TypeRef(pre2, sym2, args2)) =>
//Console.println("isSubType " + tp1 + " " + tp2);//DEBUG
def isSubArgs(tps1: List[Type], tps2: List[Type],
tparams: List[Symbol]): boolean = (
tps1.isEmpty && tps2.isEmpty
||
!tps1.isEmpty && !tps2.isEmpty &&
(tparams.head.isCovariant || (tps2.head <:< tps1.head)) &&
(tparams.head.isContravariant || (tps1.head <:< tps2.head)) &&
isSubArgs(tps1.tail, tps2.tail, tparams.tail)
);
(sym1 == sym2 &&
(phase.erasedTypes || pre1 <:< pre2) &&
isSubArgs(args1, args2, sym1.typeParams)
||
sym1.isAbstractType && !(tp1 =:= tp1.bounds.hi) && (tp1.bounds.hi <:< tp2)
||
sym2.isAbstractType && !(tp2 =:= tp2.bounds.lo) && (tp1 <:< tp2.bounds.lo)
||
// sym2 == NonNullClass && tp1.isNonNull
// ||
sym2.isClass &&
({ val base = tp1 baseType sym2; !(base eq tp1) && (base <:< tp2) })
||
sym1 == AllClass
||
// Console.println("last chance " + sym1 + " " + sym2 + " " + sym2.isClass + " " (sym2 isSubClass ObjectClass))
sym1 == AllRefClass && sym2.isClass && sym2 != AllClass && (sym2 isSubClass ObjectClass))
case Pair(MethodType(pts1, res1), MethodType(pts2, res2)) =>
(pts1.length == pts2.length &&
matchingParams(pts1, pts2, tp2.isInstanceOf[JavaMethodType]) &&
(res1 <:< res2) &&
tp1.isInstanceOf[ImplicitMethodType] == tp2.isInstanceOf[ImplicitMethodType])
case Pair(PolyType(tparams1, res1), PolyType(tparams2, res2)) =>
(tparams1.length == tparams2.length &&
List.forall2(tparams1, tparams2)
((p1, p2) => p2.info.substSym(tparams2, tparams1) <:< p1.info) &&
res1 <:< res2.substSym(tparams2, tparams1))
case Pair(TypeBounds(lo1, hi1), TypeBounds(lo2, hi2)) =>
lo2 <:< lo1 && hi1 <:< hi2
case Pair(BoundedWildcardType(bounds), _) =>
bounds.lo <:< tp2
case Pair(_, BoundedWildcardType(bounds)) =>
tp1 <:< bounds.hi
case Pair(_, TypeVar(_, constr2)) =>
if (constr2.inst != NoType) tp1 <:< constr2.inst
else { constr2.lobounds = tp1 :: constr2.lobounds; true }
case Pair(TypeVar(_, constr1), _) =>
if (constr1.inst != NoType) constr1.inst <:< tp2
else { constr1.hibounds = tp2 :: constr1.hibounds; true }
case Pair(AttributedType(_,atp1), _) =>
atp1 <:< tp2
case Pair(_, AttributedType(_,atp2)) =>
tp1 <:< atp2
case Pair(_, TypeRef(pre2, sym2, args2))
if sym2.isAbstractType && !(tp2 =:= tp2.bounds.lo) && (tp1 <:< tp2.bounds.lo) =>
true
case Pair(_, RefinedType(parents2, ref2)) =>
(parents2 forall tp1.<:<) && (ref2.toList forall tp1.specializes) &&
(!parents2.exists(.symbol.isAbstractType) || tp1.symbol != AllRefClass)
case Pair(RefinedType(parents1, ref1), _) =>
parents1 exists (.<:<(tp2))
/* todo: replace following with
case Pair(ThisType(_), _)
| Pair(SingleType(_, _), _)
| Pair(ConstantType(_), _) =>
once patern matching bug is fixed */
case Pair(ThisType(_), _) => tp1.singleDeref <:< tp2
case Pair(SingleType(_, _), _) => tp1.singleDeref <:< tp2
case Pair(ConstantType(_), _) => tp1.singleDeref <:< tp2
case Pair(TypeRef(pre1, sym1, args1), _) =>
(sym1 == AllClass && tp2 <:< AnyClass.tpe
||
sym1 == AllRefClass && tp2.isInstanceOf[SingletonType] && (tp1 <:< tp2.widen))
/* ||
sym1 == AllRefClass && tp2.symbol != AllClass &&
tp2 <:< AnyRefClass.tpe))
if (X) tp2.isNullable
else tp2.symbol != AllClass && tp2 <:< AnyRefClass.tpe))*/
case _ =>
false
}
}
/** Are <code>tps1</code> and <code>tps2</code> lists of equal length such
* that all elements of <code>tps1</code> conform to corresponding elements
* of <code>tps2</code>?
*/
def isSubTypes(tps1: List[Type], tps2: List[Type]): boolean =
tps1.length == tps2.length &&
List.forall2(tps1, tps2)((tp1, tp2) => tp1 <:< tp2)
/** Does type <code>tp</code> implement symbol <code>sym</code> with same or
* stronger type? Exact only if <code>sym</code> is a member of some
* refinement type, otherwise we might return false negatives.
*/
def specializesSym(tp: Type, sym: Symbol): boolean =
tp.symbol == AllClass ||
tp.symbol == AllRefClass && (sym.owner isSubClass ObjectClass) ||
(tp.nonPrivateMember(sym.name).alternatives exists
(alt => sym == alt || specializesSym(tp.narrow, alt, sym.owner.thisType, sym)))
/** Does member <code>sym1</code> of <code>tp1</code> have a stronger type
* than member <code>sym2</code> of <code>tp2</code>?
*/
private def specializesSym(tp1: Type, sym1: Symbol, tp2: Type, sym2: Symbol): boolean = {
val info1 = tp1.memberInfo(sym1)
val info2 = tp2.memberInfo(sym2).substThis(tp2.symbol, tp1)
//System.out.println("specializes "+tp1+"."+sym1+":"+info1+sym1.locationString+" AND "+tp2+"."+sym2+":"+info2)//DEBUG
sym2.isTerm && (info1 <:< info2) ||
sym2.isAbstractType && info2.bounds.containsType(tp1.memberType(sym1)) ||
sym2.isAliasType && tp2.memberType(sym2).substThis(tp2.symbol, tp1) =:= tp1.memberType(sym1)
}
/** A function implementing <code>tp1</code> matches <code>tp2</code> */
private def matchesType(tp1: Type, tp2: Type): boolean = Pair(tp1, tp2) match {
case Pair(MethodType(pts1, res1), MethodType(pts2, res2)) =>
(matchingParams(pts1, pts2, tp2.isInstanceOf[JavaMethodType]) && (res1 matches res2) &&
tp1.isInstanceOf[ImplicitMethodType] == tp2.isInstanceOf[ImplicitMethodType])
case Pair(PolyType(tparams1, res1), PolyType(tparams2, res2)) =>
(tparams1.length == tparams2.length &&
(res1 matches res2.substSym(tparams2, tparams1)))
case Pair(PolyType(List(), rtp1), MethodType(List(), rtp2)) => matchesType(rtp1, rtp2)
case Pair(MethodType(List(), rtp1), PolyType(List(), rtp2)) => matchesType(rtp1, rtp2)
case Pair(PolyType(List(), rtp1), _) => matchesType(rtp1, tp2)
case Pair(_, PolyType(List(), rtp2)) => matchesType(tp1, rtp2)
case Pair(MethodType(_, _), _) => false
case Pair(PolyType(_, _), _) => false
case Pair(_, MethodType(_, _)) => false
case Pair(_, PolyType(_, _)) => false
case _ =>
!phase.erasedTypes || tp1 =:= tp2
}
/** Are <code>tps1</code> and <code>tps2</code> lists of pairwise equivalent types? */
private def matchingParams(tps1: List[Type], tps2: List[Type], tps2isJava: boolean): boolean = (
tps1.length == tps2.length &&
List.forall2(tps1, tps2)((tp1, tp2) =>
(tp1 =:= tp2) || tps2isJava & tp1.symbol == ObjectClass && tp2.symbol == AnyClass)
);
/** Prepend type <code>tp</code> to closure <code>cl</code>.
*
* @param tp ...
* @param cl ...
* @return ...
*/
private def addClosure(tp: Type, cl: Array[Type]): Array[Type] = {
val cl1 = new Array[Type](cl.length + 1)
cl1(0) = tp
Array.copy(cl, 0, cl1, 1, cl.length)
cl1
}
/** like map2, but returns list `xs' itself - instead of a copy - if function
* <code>f</code> maps all elements to themselves.
*/
def map2Conserve[A <: AnyRef, B](xs: List[A], ys: List[B])(f: (A, B) => A): List[A] =
if (xs.isEmpty) xs
else {
val x1 = f(xs.head, ys.head)
val xs1 = map2Conserve(xs.tail, ys.tail)(f)
if ((x1 eq xs.head) && (xs1 eq xs.tail)) xs
else x1 :: xs1
}
// Lubs and Glbs ---------------------------------------------------------
/** The greatest sorted upwards closed lower bound of a list of lists of
* types relative to the following ordering <= between lists of types:
*
* xs <= ys iff forall y in ys exists x in xs such that x <: y
*
* @See closure for a definition of sorted and upwards closed.
*/
private def glbList(tss: List[List[Type]], depth: int): List[Type] = {
val tss1 = tss filter (ts => !ts.isEmpty)
if (tss1.isEmpty) List()
else if (tss1.tail.isEmpty) tss.head
else {
val ts0 = tss1 map (.head)
val sym = minSym(ts0)
val ts1 = elimSuper(ts0 filter (.symbol.==(sym)))
mergePrefixAndArgs(ts1, -1, depth) match {
case Some(tp0) =>
tp0 :: glbList(tss1 map (ts => if (ts.head.symbol == sym) ts.tail else ts), depth)
case None =>
throw new MalformedClosure(ts1)
}
}
}
/** The greatest sorted upwards closed lower bound of a list of closures.
*
* @See glbList for more explanations.
*/
private def glbArray(tss: List[Array[Type]], depth: int): Array[Type] = {
val tss1 = tss map { ts: Array[Type] => List.fromArray(ts) }
val glbs = glbList(tss1, depth)
val result = new Array[Type](glbs.length)
var i = 0
for (val x <- glbs.elements) { result(i) = x; i = i + 1; }
result
// Array(glbs: _*);
}
/** The least sorted upwards closed upper bound of a non-empty list
* of lists of types.
*
* @See glbList for more explanations.
*/
private def lubList(tss: List[List[Type]], depth: int): List[Type] =
if (tss.tail.isEmpty) tss.head
else if (tss exists (.isEmpty)) List()
else {
val ts0 = tss map (.head)
val sym = minSym(ts0)
if (ts0 forall (t => t.symbol == sym))
mergePrefixAndArgs(elimSub(ts0), 1, depth).toList ::: lubList(tss map (.tail), depth)
else
lubList(tss map (ts => if (ts.head.symbol == sym) ts.tail else ts), depth)
}
/** The least sorted upwards closed upper bound of a non-empty list
* of closures.
*
* @See lubList for more explanations.
*/
private def lubArray(tss: List[Array[Type]], depth: int): Array[Type] = {
var lubs = lubList(tss map { ts: Array[Type] => List.fromArray(ts) }, depth)
var arr = new Array[Type](lubs.length)
var i = 0
while (i < arr.length) {
arr(i) = lubs.head
i = i + 1
lubs = lubs.tail
}
arr
// todo: replace by Array(lubs: _* )
}
/** The minimal symbol (wrt Symbol.isLess) of a list of types */
private def minSym(tps: List[Type]): Symbol =
(tps.head.symbol /: tps.tail) {
(sym1, tp2) => if (tp2.symbol isLess sym1) tp2.symbol else sym1
}
/** A minimal type list which has a given array of types as its closure */
def spanningTypes(ts: List[Type]): List[Type] = ts match {
case List() => List()
case first :: rest =>
first :: spanningTypes(
rest filter (t => !first.symbol.isSubClass(t.symbol)))
}
/** Eliminate from list of types all elements which are a supertype
* of some other element of the list. */
private def elimSuper(ts: List[Type]): List[Type] = ts match {
case List() => List()
case t :: ts1 =>
val rest = ts1 filter (t1 => !(t <:< t1));
if (rest exists (t1 => t1 <:< t)) rest else t :: rest
}
/** Eliminate from list of types all elements which are a subtype
* of some other element of the list. */
private def elimSub(ts: List[Type]): List[Type] = ts match {
case List() => List()
case t :: ts1 =>
val rest = ts1 filter (t1 => !(t1 <:< t));
if (rest exists (t1 => t <:< t1)) rest else t :: rest
}
def lub(ts: List[Type]): Type = lub(ts, maxClosureDepth(ts) + LubGlbMargin)
/** The least upper bound wrt <:< of a list of types */
def lub(ts: List[Type], depth: int): Type = {
def lub0(ts0: List[Type]): Type = elimSub(ts0 map (.deconst)) match {
case List() => AllClass.tpe
case List(t) => t
case ts @ PolyType(tparams, _) :: _ =>
PolyType(
List.map2(tparams, List.transpose(matchingBounds(ts, tparams)))
((tparam, bounds) => tparam.cloneSymbol.setInfo(glb(bounds, depth))),
lub0(matchingInstTypes(ts, tparams)))
case ts @ MethodType(pts, _) :: rest =>
MethodType(pts, lub0(matchingRestypes(ts, pts)))
case ts @ TypeBounds(_, _) :: rest =>
assert(false)
mkTypeBounds(glb(ts map (.bounds.lo), depth), lub(ts map (.bounds.hi), depth))
case ts =>
val closures: List[Array[Type]] = ts map (.closure)
val lubBaseTypes: Array[Type] = lubArray(closures, depth)
//log("closures = " + (closures map (cl => List.fromArray(cl))) + ", lubbases = " + List.fromArray(lubBaseTypes));//DEBUG
val lubParents = spanningTypes(List.fromArray(lubBaseTypes))
val lubOwner = commonOwner(ts)
val lubBase = intersectionType(lubParents, lubOwner)
if (phase.erasedTypes || depth == 0) lubBase
else {
val lubType = refinedType(lubParents, lubOwner)
val lubThisType = lubType.symbol.thisType
val narrowts = ts map (.narrow)
def lubsym(proto: Symbol): Symbol = try {
val prototp = lubThisType.memberInfo(proto)
val syms = narrowts map (t =>
t.nonPrivateMember(proto.name).suchThat(sym =>
sym.tpe matches prototp.substThis(lubThisType.symbol, t)))
if (syms contains NoSymbol) NoSymbol
else {
val symtypes =
(List.map2(narrowts, syms)
((t, sym) => t.memberInfo(sym).substThis(t.symbol, lubThisType)));
if (proto.isTerm)
proto.cloneSymbol(lubType.symbol).setInfo(lub(symtypes, depth-1))
else if (symtypes.tail forall (symtypes.head =:=))
proto.cloneSymbol(lubType.symbol).setInfo(symtypes.head)
else {
def lubBounds(bnds: List[TypeBounds]): TypeBounds =
mkTypeBounds(glb(bnds map (.lo), depth-1), lub(bnds map (.hi), depth-1))
proto.owner.newAbstractType(proto.pos, proto.name)
.setInfo(lubBounds(symtypes map (.bounds)))
}
}
}
def refines(tp: Type, sym: Symbol): boolean = {
val syms = tp.nonPrivateMember(sym.name).alternatives;
!syms.isEmpty && (syms forall (alt =>
// todo alt != sym is strictly speaking not correct, but without it we lose
// efficiency.
alt != sym && !specializesSym(lubThisType, sym, tp, alt)))
}
for (val sym <- lubBase.nonPrivateMembers)
// add a refinement symbol for all non-class members of lubBase
// which are refined by every type in ts.
if (!sym.isClass && !sym.isConstructor && (narrowts forall (t => refines(t, sym))))
try {
val lsym = lubsym(sym)
if (lsym != NoSymbol) addMember(lubThisType, lubType, lubsym(sym))
} catch {
case ex: NoCommonType =>
}
if (lubType.decls.isEmpty) lubBase else lubType
}
}
if (settings.debug.value) {
log(indent + "lub of " + ts + " at depth "+depth)//debug
indent = indent + " "
}
val res = lub0(ts)
if (settings.debug.value) {
indent = indent.substring(0, indent.length() - 2)
log(indent + "lub of " + ts + " is " + res)//debug
}
res
}
def glb(ts: List[Type]): Type = glb(ts, maxClosureDepth(ts) + LubGlbMargin)
/** The greatest lower bound wrt <:< of a list of types */
private def glb(ts: List[Type], depth: int): Type = {
def glb0(ts0: List[Type]): Type = elimSuper(ts0 map (.deconst)) match {
case List() => AnyClass.tpe
case List(t) => t
case ts @ PolyType(tparams, _) :: _ =>
PolyType(
List.map2(tparams, List.transpose(matchingBounds(ts, tparams)))
((tparam, bounds) => tparam.cloneSymbol.setInfo(lub(bounds, depth))),
glb0(matchingInstTypes(ts, tparams)))
case ts @ MethodType(pts, _) :: rest =>
MethodType(pts, glb0(matchingRestypes(ts, pts)))
case ts @ TypeBounds(_, _) :: rest =>
mkTypeBounds(lub(ts map (.bounds.lo), depth), glb(ts map (.bounds.hi), depth))
case ts =>
try {
val glbOwner = commonOwner(ts)
val glbBase = intersectionType(ts, glbOwner)
if (phase.erasedTypes || depth == 0) glbBase
else {
val glbType = refinedType(ts, glbOwner)
val glbThisType = glbType.symbol.thisType
def glbsym(proto: Symbol): Symbol = try {
val prototp = glbThisType.memberInfo(proto)
val syms = for (
val t <- ts;
val alt <- t.nonPrivateMember(proto.name).alternatives;
glbThisType.memberInfo(alt) matches prototp) yield alt;
val symtypes = syms map glbThisType.memberInfo
assert(!symtypes.isEmpty)
proto.cloneSymbol(glbType.symbol).setInfo(
if (proto.isTerm) glb(symtypes, depth-1)
else {
def isTypeBound(tp: Type) = tp match {
case TypeBounds(_, _) => true
case _ => false
}
def glbBounds(bnds: List[Type]): TypeBounds = {
val lo = lub(bnds map (.bounds.lo), depth-1)
val hi = glb(bnds map (.bounds.hi), depth-1)
if (lo <:< hi) mkTypeBounds(lo, hi)
else throw new MalformedClosure(bnds)
}
val symbounds = symtypes filter isTypeBound
var result: Type =
if (symbounds.isEmpty)
mkTypeBounds(AllClass.tpe, AnyClass.tpe)
else glbBounds(symbounds)
for (val t <- symtypes; !isTypeBound(t))
if (result.bounds containsType t) result = t
else throw new MalformedClosure(symtypes);
result
})
}
for (val t <- ts; val sym <- t.nonPrivateMembers)
if (!sym.isClass && !sym.isConstructor && !(glbThisType specializes sym))
try {
addMember(glbThisType, glbType, glbsym(sym))
} catch {
case ex: NoCommonType =>
}
if (glbType.decls.isEmpty) glbBase else glbType
}
} catch {
case _: MalformedClosure =>
if (ts forall (t => AllRefClass.tpe <:< t)) AllRefClass.tpe
else AllClass.tpe
}
}
if (settings.debug.value) {
log(indent + "glb of " + ts + " at depth "+depth)//debug
indent = indent + " "
}
val res = glb0(ts)
if (settings.debug.value) {
indent = indent.substring(0, indent.length() - 2)
log(indent + "glb of " + ts + " is " + res)//debug
}
res
}
/** The most deeply nested owner that contains all the symbols
* of thistype or prefixless typerefs/singletype occurrences in given type.
*/
private def commonOwner(t: Type): Symbol = {
commonOwnerMap.init
commonOwnerMap.apply(t)
commonOwnerMap.result
}
/** The most deeply nested owner that contains all the symbols
* of thistype or prefixless typerefs/singletype occurrences in given list
* of types.
*/
private def commonOwner(tps: List[Type]): Symbol = {
if (settings.debug.value) log("computing common owner of types " + tps)//debug
commonOwnerMap.init
tps foreach { tp => commonOwnerMap.apply(tp); () }
commonOwnerMap.result
}
/** Compute lub (if variance == 1) or glb (if variance == -1) of given list
* of types <code>tps</code>. All types in `tps' are typerefs or singletypes
* with the same symbol.
* Return <code>Some(x)</code> if the computation succeeds with result `x'.
* Return <code>None</code> if the computuation fails.
*/
private def mergePrefixAndArgs(tps: List[Type], variance: int, depth: int): Option[Type] = tps match {
case List(tp) =>
Some(tp)
case TypeRef(_, sym, _) :: rest =>
val pres = tps map (.prefix)
val pre = if (variance == 1) lub(pres, depth) else glb(pres, depth)
val argss = tps map (.typeArgs)
val args = List.map2(sym.typeParams, List.transpose(argss)) {
(tparam, as) =>
if (depth == 0)
if (tparam.variance == variance) AnyClass.tpe
else if (tparam.variance == -variance) AllClass.tpe
else NoType
else
if (tparam.variance == variance) lub(as, depth-1)
else if (tparam.variance == -variance) glb(as, depth-1)
else {
val l = lub(as, depth-1)
if (l <:< glb(as, depth-1)) l else NoType
}
}
try {
if (args contains NoType) None
else Some(typeRef(pre, sym, args, variance))
} catch {
case ex: MalformedType => None
}
case SingleType(_, sym) :: rest =>
val pres = tps map (.prefix)
val pre = if (variance == 1) lub(pres, depth) else glb(pres, depth)
try {
Some(singleType(pre, sym, variance))
} catch {
case ex: MalformedType => None
}
}
/** Make symbol <code>sym</code> a member of scope <code>tp.decls</code>
* where <code>thistp</code> is the narrowed owner type of the scope.
*/
def addMember(thistp: Type, tp: Type, sym: Symbol): unit = {
assert(sym != NoSymbol)
if (settings.debug.value) log("add member " + sym)//debug
if (!(thistp specializes sym)) {
if (sym.isTerm)
for (val alt <- tp.nonPrivateDecl(sym.name).alternatives)
if (specializesSym(thistp, sym, thistp, alt))
tp.decls unlink alt;
tp.decls enter sym
}
}
/** All types in list must be polytypes with type parameter lists of
* same length as tparams.
* Returns list of list of bounds infos, where corresponding type
* parameters are renamed to tparams.
*/
private def matchingBounds(tps: List[Type], tparams: List[Symbol]): List[List[Type]] =
tps map {
case PolyType(tparams1, _) if (tparams1.length == tparams.length) =>
tparams1 map (tparam => tparam.info.substSym(tparams1, tparams))
case _ =>
throw new NoCommonType(tps)
}
/** All types in list must be polytypes with type parameter lists of
* same length as tparams.
* Returns list of instance types, where corresponding type
* parameters are renamed to tparams.
*/
private def matchingInstTypes(tps: List[Type], tparams: List[Symbol]): List[Type] =
tps map {
case PolyType(tparams1, restpe) if (tparams1.length == tparams.length) =>
restpe.substSym(tparams1, tparams)
case _ =>
throw new NoCommonType(tps)
}
/** All types in list must be method types with equal parameter types.
* Returns list of their result types.
*/
private def matchingRestypes(tps: List[Type], pts: List[Type]): List[Type] =
tps map {
case MethodType(pts1, res) if (isSameTypes(pts1, pts)) =>
res
case _ =>
throw new NoCommonType(tps)
}
// Errors and Diagnostics -----------------------------------------------------
/** An exception signalling a type error */
class TypeError(val pos: PositionType, val msg: String) extends java.lang.Error(msg) {
def this(msg: String) = this(NoPos, msg)
}
class NoCommonType(tps: List[Type]) extends java.lang.Error(
"lub/glb of incompatible types: " + tps.mkString("", " and ", ""))
/** An exception signalling a malformed type */
class MalformedType(msg: String) extends TypeError(msg) {
def this(pre: Type, tp: String) = this("malformed type: " + pre + "#" + tp)
}
/** An exception signalling a malformed closure */
class MalformedClosure(ts: List[Type])
extends TypeError("no common type instance of base types " +
ts.mkString("", " and ", "") + " exists")
/** An exception signalling a variance annotation/usage conflict */
class VarianceError(msg: String) extends TypeError(msg)
/** The current indentation string for traces */
private var indent: String = ""
/** Perform operation <code>p</code> on arguments <code>tp1</code>,
* <code>arg2</code> and print trace of computation.
*/
private def explain[T](op: String, p: (Type, T) => boolean, tp1: Type, arg2: T): boolean = {
Console.println(indent + tp1 + " " + op + " " + arg2 + "?")
indent = indent + " "
val result = p(tp1, arg2)
indent = indent.substring(0, indent.length() - 2)
Console.println(indent + result)
result
}
/** If option <code>explaintypes</code> is set, print a subtype trace for
* <code>found <: required</code>.
*
* @param found ...
* @param required ...
*/
def explainTypes(found: Type, required: Type): unit =
if (settings.explaintypes.value) withTypesExplained(found <:< required)
def withTypesExplained[A](op: => A): A = {
val s = explainSwitch
try { explainSwitch = true; op } finally { explainSwitch = s }
}
def withoutMalformedChecks[T](op: => T): T = {
val s = checkMalformedSwitch
try { checkMalformedSwitch = false; op } finally { checkMalformedSwitch = s }
}
def withNoGlobalVariance[T](op: => T): T = {
val s = globalVariance
try { globalVariance = 0; op } finally { globalVariance = s }
}
}
|