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 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
|
//
// generic.cs: Generics support
//
// Authors: Martin Baulig (martin@ximian.com)
// Miguel de Icaza (miguel@ximian.com)
// Marek Safar (marek.safar@gmail.com)
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
// Copyright 2004-2008 Novell, Inc
// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
#if STATIC
using MetaType = IKVM.Reflection.Type;
using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
using MetaType = System.Type;
using System.Reflection;
using System.Reflection.Emit;
#endif
namespace Mono.CSharp {
public enum Variance
{
//
// Don't add or modify internal values, they are used as -/+ calculation signs
//
None = 0,
Covariant = 1,
Contravariant = -1
}
[Flags]
public enum SpecialConstraint
{
None = 0,
Constructor = 1 << 2,
Class = 1 << 3,
Struct = 1 << 4
}
public class SpecialContraintExpr : FullNamedExpression
{
public SpecialContraintExpr (SpecialConstraint constraint, Location loc)
{
this.loc = loc;
this.Constraint = constraint;
}
public SpecialConstraint Constraint { get; private set; }
protected override Expression DoResolve (ResolveContext rc)
{
throw new NotImplementedException ();
}
public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext ec)
{
throw new NotImplementedException ();
}
}
//
// A set of parsed constraints for a type parameter
//
public class Constraints
{
SimpleMemberName tparam;
List<FullNamedExpression> constraints;
Location loc;
bool resolved;
bool resolving;
public IEnumerable<FullNamedExpression> ConstraintExpressions {
get {
return constraints;
}
}
public Constraints (SimpleMemberName tparam, List<FullNamedExpression> constraints, Location loc)
{
this.tparam = tparam;
this.constraints = constraints;
this.loc = loc;
}
#region Properties
public List<FullNamedExpression> TypeExpressions {
get {
return constraints;
}
}
public Location Location {
get {
return loc;
}
}
public SimpleMemberName TypeParameter {
get {
return tparam;
}
}
#endregion
public static bool CheckConflictingInheritedConstraint (TypeParameterSpec spec, TypeSpec bb, IMemberContext context, Location loc)
{
if (spec.HasSpecialClass && bb.IsStruct) {
context.Module.Compiler.Report.Error (455, loc,
"Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
spec.Name, "class", bb.GetSignatureForError ());
return false;
}
return CheckConflictingInheritedConstraint (spec, spec.BaseType, bb, context, loc);
}
static bool CheckConflictingInheritedConstraint (TypeParameterSpec spec, TypeSpec ba, TypeSpec bb, IMemberContext context, Location loc)
{
if (ba == bb)
return true;
if (TypeSpec.IsBaseClass (ba, bb, false) || TypeSpec.IsBaseClass (bb, ba, false))
return true;
Error_ConflictingConstraints (context, spec, ba, bb, loc);
return false;
}
public static void Error_ConflictingConstraints (IMemberContext context, TypeParameterSpec tp, TypeSpec ba, TypeSpec bb, Location loc)
{
context.Module.Compiler.Report.Error (455, loc,
"Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
tp.Name, ba.GetSignatureForError (), bb.GetSignatureForError ());
}
public void CheckGenericConstraints (IMemberContext context, bool obsoleteCheck)
{
foreach (var c in constraints) {
if (c == null)
continue;
var t = c.Type;
if (t == null)
continue;
if (obsoleteCheck) {
ObsoleteAttribute obsolete_attr = t.GetAttributeObsolete ();
if (obsolete_attr != null)
AttributeTester.Report_ObsoleteMessage (obsolete_attr, t.GetSignatureForError (), c.Location, context.Module.Compiler.Report);
}
ConstraintChecker.Check (context, t, c.Location);
}
}
//
// Resolve the constraints types with only possible early checks, return
// value `false' is reserved for recursive failure
//
public bool Resolve (IMemberContext context, TypeParameter tp)
{
if (resolved)
return true;
if (resolving)
return false;
resolving = true;
var spec = tp.Type;
List<TypeParameterSpec> tparam_types = null;
bool iface_found = false;
spec.BaseType = context.Module.Compiler.BuiltinTypes.Object;
for (int i = 0; i < constraints.Count; ++i) {
var constraint = constraints[i];
if (constraint is SpecialContraintExpr) {
spec.SpecialConstraint |= ((SpecialContraintExpr) constraint).Constraint;
if (spec.HasSpecialStruct)
spec.BaseType = context.Module.Compiler.BuiltinTypes.ValueType;
// Set to null as it does not have a type
constraints[i] = null;
continue;
}
var type = constraint.ResolveAsType (context);
if (type == null)
continue;
if (type.Arity > 0 && ((InflatedTypeSpec) type).HasDynamicArgument ()) {
context.Module.Compiler.Report.Error (1968, constraint.Location,
"A constraint cannot be the dynamic type `{0}'", type.GetSignatureForError ());
continue;
}
if (!context.CurrentMemberDefinition.IsAccessibleAs (type)) {
context.Module.Compiler.Report.SymbolRelatedToPreviousError (type);
context.Module.Compiler.Report.Error (703, loc,
"Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
type.GetSignatureForError (), context.GetSignatureForError ());
}
if (type.IsInterface) {
if (!spec.AddInterface (type)) {
context.Module.Compiler.Report.Error (405, constraint.Location,
"Duplicate constraint `{0}' for type parameter `{1}'", type.GetSignatureForError (), tparam.Value);
}
iface_found = true;
continue;
}
var constraint_tp = type as TypeParameterSpec;
if (constraint_tp != null) {
if (tparam_types == null) {
tparam_types = new List<TypeParameterSpec> (2);
} else if (tparam_types.Contains (constraint_tp)) {
context.Module.Compiler.Report.Error (405, constraint.Location,
"Duplicate constraint `{0}' for type parameter `{1}'", type.GetSignatureForError (), tparam.Value);
continue;
}
//
// Checks whether each generic method parameter constraint type
// is valid with respect to T
//
if (tp.IsMethodTypeParameter) {
TypeManager.CheckTypeVariance (type, Variance.Contravariant, context);
}
var tp_def = constraint_tp.MemberDefinition as TypeParameter;
if (tp_def != null && !tp_def.ResolveConstraints (context)) {
context.Module.Compiler.Report.Error (454, constraint.Location,
"Circular constraint dependency involving `{0}' and `{1}'",
constraint_tp.GetSignatureForError (), tp.GetSignatureForError ());
continue;
}
//
// Checks whether there are no conflicts between type parameter constraints
//
// class Foo<T, U>
// where T : A
// where U : B, T
//
// A and B are not convertible and only 1 class constraint is allowed
//
if (constraint_tp.HasTypeConstraint) {
if (spec.HasTypeConstraint || spec.HasSpecialStruct) {
if (!CheckConflictingInheritedConstraint (spec, constraint_tp.BaseType, context, constraint.Location))
continue;
} else {
for (int ii = 0; ii < tparam_types.Count; ++ii) {
if (!tparam_types[ii].HasTypeConstraint)
continue;
if (!CheckConflictingInheritedConstraint (spec, tparam_types[ii].BaseType, constraint_tp.BaseType, context, constraint.Location))
break;
}
}
}
if (constraint_tp.HasSpecialStruct) {
context.Module.Compiler.Report.Error (456, constraint.Location,
"Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
constraint_tp.GetSignatureForError (), tp.GetSignatureForError ());
continue;
}
tparam_types.Add (constraint_tp);
continue;
}
if (iface_found || spec.HasTypeConstraint) {
context.Module.Compiler.Report.Error (406, constraint.Location,
"The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
type.GetSignatureForError ());
}
if (spec.HasSpecialStruct || spec.HasSpecialClass) {
context.Module.Compiler.Report.Error (450, constraint.Location,
"`{0}': cannot specify both a constraint class and the `class' or `struct' constraint",
type.GetSignatureForError ());
}
switch (type.BuiltinType) {
case BuiltinTypeSpec.Type.Array:
case BuiltinTypeSpec.Type.Delegate:
case BuiltinTypeSpec.Type.MulticastDelegate:
case BuiltinTypeSpec.Type.Enum:
case BuiltinTypeSpec.Type.ValueType:
case BuiltinTypeSpec.Type.Object:
context.Module.Compiler.Report.Error (702, constraint.Location,
"A constraint cannot be special class `{0}'", type.GetSignatureForError ());
continue;
case BuiltinTypeSpec.Type.Dynamic:
context.Module.Compiler.Report.Error (1967, constraint.Location,
"A constraint cannot be the dynamic type");
continue;
}
if (type.IsSealed || !type.IsClass) {
context.Module.Compiler.Report.Error (701, loc,
"`{0}' is not a valid constraint. A constraint must be an interface, a non-sealed class or a type parameter",
type.GetSignatureForError ());
continue;
}
if (type.IsStatic) {
context.Module.Compiler.Report.Error (717, constraint.Location,
"`{0}' is not a valid constraint. Static classes cannot be used as constraints",
type.GetSignatureForError ());
}
spec.BaseType = type;
}
if (tparam_types != null)
spec.TypeArguments = tparam_types.ToArray ();
resolving = false;
resolved = true;
return true;
}
public void VerifyClsCompliance (Report report)
{
foreach (var c in constraints)
{
if (c == null)
continue;
if (!c.Type.IsCLSCompliant ()) {
report.SymbolRelatedToPreviousError (c.Type);
report.Warning (3024, 1, loc, "Constraint type `{0}' is not CLS-compliant",
c.Type.GetSignatureForError ());
}
}
}
}
//
// A type parameter for a generic type or generic method definition
//
public class TypeParameter : MemberCore, ITypeDefinition
{
static readonly string[] attribute_target = new string [] { "type parameter" };
Constraints constraints;
GenericTypeParameterBuilder builder;
readonly TypeParameterSpec spec;
public TypeParameter (int index, MemberName name, Constraints constraints, Attributes attrs, Variance variance)
: base (null, name, attrs)
{
this.constraints = constraints;
this.spec = new TypeParameterSpec (null, index, this, SpecialConstraint.None, variance, null);
}
//
// Used by parser
//
public TypeParameter (MemberName name, Attributes attrs, Variance variance)
: base (null, name, attrs)
{
this.spec = new TypeParameterSpec (null, -1, this, SpecialConstraint.None, variance, null);
}
public TypeParameter (TypeParameterSpec spec, TypeSpec parentSpec, MemberName name, Attributes attrs)
: base (null, name, attrs)
{
this.spec = new TypeParameterSpec (parentSpec, spec.DeclaredPosition, spec.MemberDefinition, spec.SpecialConstraint, spec.Variance, null) {
BaseType = spec.BaseType,
InterfacesDefined = spec.InterfacesDefined,
TypeArguments = spec.TypeArguments
};
}
#region Properties
public override AttributeTargets AttributeTargets {
get {
return AttributeTargets.GenericParameter;
}
}
public Constraints Constraints {
get {
return constraints;
}
set {
constraints = value;
}
}
public IAssemblyDefinition DeclaringAssembly {
get {
return Module.DeclaringAssembly;
}
}
public override string DocCommentHeader {
get {
throw new InvalidOperationException (
"Unexpected attempt to get doc comment from " + this.GetType ());
}
}
bool ITypeDefinition.IsComImport {
get {
return false;
}
}
bool ITypeDefinition.IsPartial {
get {
return false;
}
}
public bool IsMethodTypeParameter {
get {
return spec.IsMethodOwned;
}
}
bool ITypeDefinition.IsTypeForwarder {
get {
return false;
}
}
public string Name {
get {
return MemberName.Name;
}
}
public string Namespace {
get {
return null;
}
}
public TypeParameterSpec Type {
get {
return spec;
}
}
public int TypeParametersCount {
get {
return 0;
}
}
public TypeParameterSpec[] TypeParameters {
get {
return null;
}
}
public override string[] ValidAttributeTargets {
get {
return attribute_target;
}
}
public Variance Variance {
get {
return spec.Variance;
}
}
#endregion
//
// This is called for each part of a partial generic type definition.
//
// If partial type parameters constraints are not null and we don't
// already have constraints they become our constraints. If we already
// have constraints, we must check that they're same.
//
public bool AddPartialConstraints (TypeDefinition part, TypeParameter tp)
{
if (builder == null)
throw new InvalidOperationException ();
var new_constraints = tp.constraints;
if (new_constraints == null)
return true;
// TODO: could create spec only
//tp.Define (null, -1, part.Definition);
tp.spec.DeclaringType = part.Definition;
if (!tp.ResolveConstraints (part))
return false;
if (constraints != null)
return spec.HasSameConstraintsDefinition (tp.Type);
// Copy constraint from resolved part to partial container
spec.SpecialConstraint = tp.spec.SpecialConstraint;
spec.Interfaces = tp.spec.Interfaces;
spec.TypeArguments = tp.spec.TypeArguments;
spec.BaseType = tp.spec.BaseType;
return true;
}
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
}
public void CheckGenericConstraints (bool obsoleteCheck)
{
if (constraints != null)
constraints.CheckGenericConstraints (this, obsoleteCheck);
}
public TypeParameter CreateHoistedCopy (TypeSpec declaringSpec)
{
return new TypeParameter (spec, declaringSpec, MemberName, null);
}
public override bool Define ()
{
return true;
}
//
// This is the first method which is called during the resolving
// process; we're called immediately after creating the type parameters
// with SRE (by calling `DefineGenericParameters()' on the TypeBuilder /
// MethodBuilder).
//
public void Create (TypeSpec declaringType, TypeContainer parent)
{
if (builder != null)
throw new InternalErrorException ();
// Needed to get compiler reference
this.Parent = parent;
spec.DeclaringType = declaringType;
}
public void Define (GenericTypeParameterBuilder type)
{
this.builder = type;
spec.SetMetaInfo (type);
}
public void EmitConstraints (GenericTypeParameterBuilder builder)
{
var attr = GenericParameterAttributes.None;
if (spec.Variance == Variance.Contravariant)
attr |= GenericParameterAttributes.Contravariant;
else if (spec.Variance == Variance.Covariant)
attr |= GenericParameterAttributes.Covariant;
if (spec.HasSpecialClass)
attr |= GenericParameterAttributes.ReferenceTypeConstraint;
else if (spec.HasSpecialStruct)
attr |= GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint;
if (spec.HasSpecialConstructor)
attr |= GenericParameterAttributes.DefaultConstructorConstraint;
if (spec.BaseType.BuiltinType != BuiltinTypeSpec.Type.Object)
builder.SetBaseTypeConstraint (spec.BaseType.GetMetaInfo ());
if (spec.InterfacesDefined != null)
builder.SetInterfaceConstraints (spec.InterfacesDefined.Select (l => l.GetMetaInfo ()).ToArray ());
if (spec.TypeArguments != null)
builder.SetInterfaceConstraints (spec.TypeArguments.Select (l => l.GetMetaInfo ()).ToArray ());
builder.SetGenericParameterAttributes (attr);
}
public override void Emit ()
{
EmitConstraints (builder);
if (OptAttributes != null)
OptAttributes.Emit ();
base.Emit ();
}
public void ErrorInvalidVariance (IMemberContext mc, Variance expected)
{
Report.SymbolRelatedToPreviousError (mc.CurrentMemberDefinition);
string input_variance = Variance == Variance.Contravariant ? "contravariant" : "covariant";
string gtype_variance;
switch (expected) {
case Variance.Contravariant: gtype_variance = "contravariantly"; break;
case Variance.Covariant: gtype_variance = "covariantly"; break;
default: gtype_variance = "invariantly"; break;
}
Delegate d = mc as Delegate;
string parameters = d != null ? d.Parameters.GetSignatureForError () : "";
Report.Error (1961, Location,
"The {2} type parameter `{0}' must be {3} valid on `{1}{4}'",
GetSignatureForError (), mc.GetSignatureForError (), input_variance, gtype_variance, parameters);
}
public TypeSpec GetAttributeCoClass ()
{
return null;
}
public string GetAttributeDefaultMember ()
{
throw new NotSupportedException ();
}
public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
{
throw new NotSupportedException ();
}
public override string GetSignatureForDocumentation ()
{
throw new NotImplementedException ();
}
public override string GetSignatureForError ()
{
return MemberName.Name;
}
bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
{
return spec.MemberDefinition.DeclaringAssembly == assembly;
}
public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
{
throw new NotSupportedException ("Not supported for compiled definition");
}
//
// Resolves all type parameter constraints
//
public bool ResolveConstraints (IMemberContext context)
{
if (constraints != null)
return constraints.Resolve (context, this);
if (spec.BaseType == null)
spec.BaseType = context.Module.Compiler.BuiltinTypes.Object;
return true;
}
public override bool IsClsComplianceRequired ()
{
return false;
}
public new void VerifyClsCompliance ()
{
if (constraints != null)
constraints.VerifyClsCompliance (Report);
}
public void WarningParentNameConflict (TypeParameter conflict)
{
conflict.Report.SymbolRelatedToPreviousError (conflict.Location, null);
conflict.Report.Warning (693, 3, Location,
"Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
GetSignatureForError (), conflict.CurrentType.GetSignatureForError ());
}
}
[System.Diagnostics.DebuggerDisplay ("{DisplayDebugInfo()}")]
public class TypeParameterSpec : TypeSpec
{
public static readonly new TypeParameterSpec[] EmptyTypes = new TypeParameterSpec[0];
Variance variance;
SpecialConstraint spec;
int tp_pos;
TypeSpec[] targs;
TypeSpec[] ifaces_defined;
//
// Creates type owned type parameter
//
public TypeParameterSpec (TypeSpec declaringType, int index, ITypeDefinition definition, SpecialConstraint spec, Variance variance, MetaType info)
: base (MemberKind.TypeParameter, declaringType, definition, info, Modifiers.PUBLIC)
{
this.variance = variance;
this.spec = spec;
state &= ~StateFlags.Obsolete_Undetected;
tp_pos = index;
}
//
// Creates method owned type parameter
//
public TypeParameterSpec (int index, ITypeDefinition definition, SpecialConstraint spec, Variance variance, MetaType info)
: this (null, index, definition, spec, variance, info)
{
}
#region Properties
public int DeclaredPosition {
get {
return tp_pos;
}
set {
tp_pos = value;
}
}
public bool HasSpecialConstructor {
get {
return (spec & SpecialConstraint.Constructor) != 0;
}
}
public bool HasSpecialClass {
get {
return (spec & SpecialConstraint.Class) != 0;
}
}
public bool HasSpecialStruct {
get {
return (spec & SpecialConstraint.Struct) != 0;
}
}
public bool HasAnyTypeConstraint {
get {
return (spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0 || ifaces != null || targs != null || HasTypeConstraint;
}
}
public bool HasTypeConstraint {
get {
var bt = BaseType.BuiltinType;
return bt != BuiltinTypeSpec.Type.Object && bt != BuiltinTypeSpec.Type.ValueType;
}
}
public override IList<TypeSpec> Interfaces {
get {
if ((state & StateFlags.InterfacesExpanded) == 0) {
if (ifaces != null) {
if (ifaces_defined == null)
ifaces_defined = ifaces.ToArray ();
for (int i = 0; i < ifaces_defined.Length; ++i ) {
var iface_type = ifaces_defined[i];
var td = iface_type.MemberDefinition as TypeDefinition;
if (td != null)
td.DoExpandBaseInterfaces ();
if (iface_type.Interfaces != null) {
for (int ii = 0; ii < iface_type.Interfaces.Count; ++ii) {
var ii_iface_type = iface_type.Interfaces [ii];
AddInterface (ii_iface_type);
}
}
}
} else if (ifaces_defined == null) {
ifaces_defined = ifaces == null ? TypeSpec.EmptyTypes : ifaces.ToArray ();
}
//
// Include all base type interfaces too, see ImportTypeBase for details
//
if (BaseType != null) {
var td = BaseType.MemberDefinition as TypeDefinition;
if (td != null)
td.DoExpandBaseInterfaces ();
if (BaseType.Interfaces != null) {
foreach (var iface in BaseType.Interfaces) {
AddInterface (iface);
}
}
}
state |= StateFlags.InterfacesExpanded;
}
return ifaces;
}
}
//
// Unexpanded interfaces list
//
public TypeSpec[] InterfacesDefined {
get {
if (ifaces_defined == null) {
if (ifaces == null)
return null;
ifaces_defined = ifaces.ToArray ();
}
return ifaces_defined.Length == 0 ? null : ifaces_defined;
}
set {
ifaces_defined = value;
if (value != null && value.Length != 0)
ifaces = new List<TypeSpec> (value);
}
}
public bool IsConstrained {
get {
return spec != SpecialConstraint.None || ifaces != null || targs != null || HasTypeConstraint;
}
}
//
// Returns whether the type parameter is known to be a reference type
//
public new bool IsReferenceType {
get {
if ((spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0)
return (spec & SpecialConstraint.Class) != 0;
//
// Full check is needed (see IsValueType for details)
//
if (HasTypeConstraint && TypeSpec.IsReferenceType (BaseType))
return true;
if (targs != null) {
foreach (var ta in targs) {
//
// Secondary special constraints are ignored (I am not sure why)
//
var tp = ta as TypeParameterSpec;
if (tp != null && (tp.spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0)
continue;
if (TypeSpec.IsReferenceType (ta))
return true;
}
}
return false;
}
}
//
// Returns whether the type parameter is known to be a value type
//
public new bool IsValueType {
get {
//
// Even if structs/enums cannot be used directly as constraints
// they can apear as constraint type when inheriting base constraint
// which has dependant type parameter constraint which has been
// inflated using value type
//
// class A : B<int> { override void Foo<U> () {} }
// class B<T> { virtual void Foo<U> () where U : T {} }
//
if (HasSpecialStruct)
return true;
if (targs != null) {
foreach (var ta in targs) {
if (TypeSpec.IsValueType (ta))
return true;
}
}
return false;
}
}
public override string Name {
get {
return definition.Name;
}
}
public bool IsMethodOwned {
get {
return DeclaringType == null;
}
}
public SpecialConstraint SpecialConstraint {
get {
return spec;
}
set {
spec = value;
}
}
//
// Types used to inflate the generic type
//
public new TypeSpec[] TypeArguments {
get {
return targs;
}
set {
targs = value;
}
}
public Variance Variance {
get {
return variance;
}
}
#endregion
public string DisplayDebugInfo ()
{
var s = GetSignatureForError ();
return IsMethodOwned ? s + "!!" : s + "!";
}
//
// Finds effective base class. The effective base class is always a class-type
//
public TypeSpec GetEffectiveBase ()
{
if (HasSpecialStruct)
return BaseType;
//
// If T has a class-type constraint C but no type-parameter constraints, its effective base class is C
//
if (BaseType != null && targs == null) {
//
// If T has a constraint V that is a value-type, use instead the most specific base type of V that is a class-type.
//
// LAMESPEC: Is System.ValueType always the most specific base type in this case?
//
// Note: This can never happen in an explicitly given constraint, but may occur when the constraints of a generic method
// are implicitly inherited by an overriding method declaration or an explicit implementation of an interface method.
//
return BaseType.IsStruct ? BaseType.BaseType : BaseType;
}
var types = targs;
if (HasTypeConstraint) {
Array.Resize (ref types, types.Length + 1);
for (int i = 0; i < types.Length - 1; ++i) {
types[i] = types[i].BaseType;
}
types[types.Length - 1] = BaseType;
} else {
types = types.Select (l => l.BaseType).ToArray ();
}
if (types != null)
return Convert.FindMostEncompassedType (types);
return BaseType;
}
public override string GetSignatureForDocumentation ()
{
var prefix = IsMethodOwned ? "``" : "`";
return prefix + DeclaredPosition;
}
public override string GetSignatureForError ()
{
return Name;
}
//
// Constraints have to match by definition but not position, used by
// partial classes or methods
//
public bool HasSameConstraintsDefinition (TypeParameterSpec other)
{
if (spec != other.spec)
return false;
if (BaseType != other.BaseType)
return false;
if (!TypeSpecComparer.Override.IsSame (InterfacesDefined, other.InterfacesDefined))
return false;
if (!TypeSpecComparer.Override.IsSame (targs, other.targs))
return false;
return true;
}
//
// Constraints have to match by using same set of types, used by
// implicit interface implementation
//
public bool HasSameConstraintsImplementation (TypeParameterSpec other)
{
if (spec != other.spec)
return false;
//
// It can be same base type or inflated type parameter
//
// interface I<T> { void Foo<U> where U : T; }
// class A : I<int> { void Foo<X> where X : int {} }
//
bool found;
if (!TypeSpecComparer.Override.IsEqual (BaseType, other.BaseType)) {
if (other.targs == null)
return false;
found = false;
foreach (var otarg in other.targs) {
if (TypeSpecComparer.Override.IsEqual (BaseType, otarg)) {
found = true;
break;
}
}
if (!found)
return false;
}
// Check interfaces implementation -> definition
if (InterfacesDefined != null) {
//
// Iterate over inflated interfaces
//
foreach (var iface in Interfaces) {
found = false;
if (other.InterfacesDefined != null) {
foreach (var oiface in other.Interfaces) {
if (TypeSpecComparer.Override.IsEqual (iface, oiface)) {
found = true;
break;
}
}
}
if (found)
continue;
if (other.targs != null) {
foreach (var otarg in other.targs) {
if (TypeSpecComparer.Override.IsEqual (BaseType, otarg)) {
found = true;
break;
}
}
}
if (!found)
return false;
}
}
// Check interfaces implementation <- definition
if (other.InterfacesDefined != null) {
if (InterfacesDefined == null)
return false;
//
// Iterate over inflated interfaces
//
foreach (var oiface in other.Interfaces) {
found = false;
foreach (var iface in Interfaces) {
if (TypeSpecComparer.Override.IsEqual (iface, oiface)) {
found = true;
break;
}
}
if (!found)
return false;
}
}
// Check type parameters implementation -> definition
if (targs != null) {
if (other.targs == null)
return false;
foreach (var targ in targs) {
found = false;
foreach (var otarg in other.targs) {
if (TypeSpecComparer.Override.IsEqual (targ, otarg)) {
found = true;
break;
}
}
if (!found)
return false;
}
}
// Check type parameters implementation <- definition
if (other.targs != null) {
foreach (var otarg in other.targs) {
// Ignore inflated type arguments, were checked above
if (!otarg.IsGenericParameter)
continue;
if (targs == null)
return false;
found = false;
foreach (var targ in targs) {
if (TypeSpecComparer.Override.IsEqual (targ, otarg)) {
found = true;
break;
}
}
if (!found)
return false;
}
}
return true;
}
public static TypeParameterSpec[] InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec[] tparams)
{
return InflateConstraints (tparams, l => l, inflator);
}
public static TypeParameterSpec[] InflateConstraints<T> (TypeParameterSpec[] tparams, Func<T, TypeParameterInflator> inflatorFactory, T arg)
{
TypeParameterSpec[] constraints = null;
TypeParameterInflator? inflator = null;
for (int i = 0; i < tparams.Length; ++i) {
var tp = tparams[i];
if (tp.HasTypeConstraint || tp.InterfacesDefined != null || tp.TypeArguments != null) {
if (constraints == null) {
constraints = new TypeParameterSpec[tparams.Length];
Array.Copy (tparams, constraints, constraints.Length);
}
//
// Using a factory to avoid possibly expensive inflator build up
//
if (inflator == null)
inflator = inflatorFactory (arg);
constraints[i] = (TypeParameterSpec) constraints[i].InflateMember (inflator.Value);
}
}
if (constraints == null)
constraints = tparams;
return constraints;
}
public void InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec tps)
{
tps.BaseType = inflator.Inflate (BaseType);
var defined = InterfacesDefined;
if (defined != null) {
tps.ifaces_defined = new TypeSpec[defined.Length];
for (int i = 0; i < defined.Length; ++i)
tps.ifaces_defined [i] = inflator.Inflate (defined[i]);
}
var ifaces = Interfaces;
if (ifaces != null) {
tps.ifaces = new List<TypeSpec> (ifaces.Count);
for (int i = 0; i < ifaces.Count; ++i)
tps.ifaces.Add (inflator.Inflate (ifaces[i]));
}
if (targs != null) {
tps.targs = new TypeSpec[targs.Length];
for (int i = 0; i < targs.Length; ++i)
tps.targs[i] = inflator.Inflate (targs[i]);
}
}
public override MemberSpec InflateMember (TypeParameterInflator inflator)
{
var tps = (TypeParameterSpec) MemberwiseClone ();
InflateConstraints (inflator, tps);
return tps;
}
//
// Populates type parameter members using type parameter constraints
// The trick here is to be called late enough but not too late to
// populate member cache with all members from other types
//
protected override void InitializeMemberCache (bool onlyTypes)
{
cache = new MemberCache ();
//
// For a type parameter the membercache is the union of the sets of members of the types
// specified as a primary constraint or secondary constraint
//
if (BaseType.BuiltinType != BuiltinTypeSpec.Type.Object && BaseType.BuiltinType != BuiltinTypeSpec.Type.ValueType)
cache.AddBaseType (BaseType);
if (InterfacesDefined != null) {
foreach (var iface_type in InterfacesDefined) {
cache.AddInterface (iface_type);
}
}
if (targs != null) {
foreach (var ta in targs) {
var b_type = ta.BaseType;
if (b_type.BuiltinType != BuiltinTypeSpec.Type.Object && b_type.BuiltinType != BuiltinTypeSpec.Type.ValueType)
cache.AddBaseType (b_type);
var tps = ta as TypeParameterSpec;
var ifaces = tps != null ? tps.InterfacesDefined : ta.Interfaces;
if (ifaces != null) {
foreach (var iface_type in ifaces) {
cache.AddInterface (iface_type);
}
}
}
}
}
public bool IsConvertibleToInterface (TypeSpec iface)
{
if (Interfaces != null) {
foreach (var t in Interfaces) {
if (t == iface)
return true;
}
}
if (TypeArguments != null) {
foreach (var t in TypeArguments) {
if (((TypeParameterSpec) t).IsConvertibleToInterface (iface))
return true;
}
}
return false;
}
public static bool HasAnyTypeParameterTypeConstrained (IGenericMethodDefinition md)
{
var tps = md.TypeParameters;
for (int i = 0; i < md.TypeParametersCount; ++i) {
if (tps[i].HasAnyTypeConstraint) {
return true;
}
}
return false;
}
public static bool HasAnyTypeParameterConstrained (IGenericMethodDefinition md)
{
var tps = md.TypeParameters;
for (int i = 0; i < md.TypeParametersCount; ++i) {
if (tps[i].IsConstrained) {
return true;
}
}
return false;
}
public bool HasDependencyOn (TypeSpec type)
{
if (TypeArguments != null) {
foreach (var targ in TypeArguments) {
if (TypeSpecComparer.Override.IsEqual (targ, type))
return true;
var tps = targ as TypeParameterSpec;
if (tps != null && tps.HasDependencyOn (type))
return true;
}
}
return false;
}
public override TypeSpec Mutate (TypeParameterMutator mutator)
{
return mutator.Mutate (this);
}
}
public struct TypeParameterInflator
{
readonly TypeSpec type;
readonly TypeParameterSpec[] tparams;
readonly TypeSpec[] targs;
readonly IModuleContext context;
public TypeParameterInflator (TypeParameterInflator nested, TypeSpec type)
: this (nested.context, type, nested.tparams, nested.targs)
{
}
public TypeParameterInflator (IModuleContext context, TypeSpec type, TypeParameterSpec[] tparams, TypeSpec[] targs)
{
if (tparams.Length != targs.Length)
throw new ArgumentException ("Invalid arguments");
this.context = context;
this.tparams = tparams;
this.targs = targs;
this.type = type;
}
#region Properties
public IModuleContext Context {
get {
return context;
}
}
public TypeSpec TypeInstance {
get {
return type;
}
}
//
// Type parameters to inflate
//
public TypeParameterSpec[] TypeParameters {
get {
return tparams;
}
}
#endregion
public TypeSpec Inflate (TypeSpec type)
{
var tp = type as TypeParameterSpec;
if (tp != null)
return Inflate (tp);
var ac = type as ArrayContainer;
if (ac != null) {
var et = Inflate (ac.Element);
if (et != ac.Element)
return ArrayContainer.MakeType (context.Module, et, ac.Rank);
return ac;
}
if (type.Kind == MemberKind.MissingType)
return type;
//
// When inflating a nested type, inflate its parent first
// in case it's using same type parameters (was inflated within the type)
//
TypeSpec[] targs;
int i = 0;
if (type.IsNested) {
var parent = Inflate (type.DeclaringType);
//
// Keep the inflated type arguments
//
targs = type.TypeArguments;
//
// When inflating imported nested type used inside same declaring type, we get TypeSpec
// because the import cache helps us to catch it. However, that means we have to look at
// type definition to get type argument (they are in fact type parameter in this case)
//
if (targs.Length == 0 && type.Arity > 0)
targs = type.MemberDefinition.TypeParameters;
//
// Parent was inflated, find the same type on inflated type
// to use same cache for nested types on same generic parent
//
type = MemberCache.FindNestedType (parent, type.Name, type.Arity);
//
// Handle the tricky case where parent shares local type arguments
// which means inflating inflated type
//
// class Test<T> {
// public static Nested<T> Foo () { return null; }
//
// public class Nested<U> {}
// }
//
// return type of Test<string>.Foo() has to be Test<string>.Nested<string>
//
if (targs.Length > 0) {
var inflated_targs = new TypeSpec[targs.Length];
for (; i < targs.Length; ++i)
inflated_targs[i] = Inflate (targs[i]);
type = type.MakeGenericType (context, inflated_targs);
}
return type;
}
// Nothing to do for non-generic type
if (type.Arity == 0)
return type;
targs = new TypeSpec[type.Arity];
//
// Inflating using outside type arguments, var v = new Foo<int> (), class Foo<T> {}
//
if (type is InflatedTypeSpec) {
for (; i < targs.Length; ++i)
targs[i] = Inflate (type.TypeArguments[i]);
type = type.GetDefinition ();
} else {
//
// Inflating parent using inside type arguments, class Foo<T> { ITest<T> foo; }
//
var args = type.MemberDefinition.TypeParameters;
foreach (var ds_tp in args)
targs[i++] = Inflate (ds_tp);
}
return type.MakeGenericType (context, targs);
}
public TypeSpec Inflate (TypeParameterSpec tp)
{
for (int i = 0; i < tparams.Length; ++i)
if (tparams [i] == tp)
return targs[i];
// This can happen when inflating nested types
// without type arguments specified
return tp;
}
}
//
// Before emitting any code we have to change all MVAR references to VAR
// when the method is of generic type and has hoisted variables
//
public class TypeParameterMutator
{
readonly TypeParameters mvar;
readonly TypeParameters var;
readonly TypeParameterSpec[] src;
Dictionary<TypeSpec, TypeSpec> mutated_typespec;
public TypeParameterMutator (TypeParameters mvar, TypeParameters var)
{
if (mvar.Count != var.Count)
throw new ArgumentException ();
this.mvar = mvar;
this.var = var;
}
public TypeParameterMutator (TypeParameterSpec[] srcVar, TypeParameters destVar)
{
if (srcVar.Length != destVar.Count)
throw new ArgumentException ();
this.src = srcVar;
this.var = destVar;
}
#region Properties
public TypeParameters MethodTypeParameters {
get {
return mvar;
}
}
#endregion
public static TypeSpec GetMemberDeclaringType (TypeSpec type)
{
if (type is InflatedTypeSpec) {
if (type.DeclaringType == null)
return type.GetDefinition ();
var parent = GetMemberDeclaringType (type.DeclaringType);
type = MemberCache.GetMember<TypeSpec> (parent, type);
}
return type;
}
public TypeSpec Mutate (TypeSpec ts)
{
TypeSpec value;
if (mutated_typespec != null && mutated_typespec.TryGetValue (ts, out value))
return value;
value = ts.Mutate (this);
if (mutated_typespec == null)
mutated_typespec = new Dictionary<TypeSpec, TypeSpec> ();
mutated_typespec.Add (ts, value);
return value;
}
public TypeParameterSpec Mutate (TypeParameterSpec tp)
{
if (mvar != null) {
for (int i = 0; i < mvar.Count; ++i) {
if (mvar[i].Type == tp)
return var[i].Type;
}
} else {
for (int i = 0; i < src.Length; ++i) {
if (src[i] == tp)
return var[i].Type;
}
}
return tp;
}
public TypeSpec[] Mutate (TypeSpec[] targs)
{
TypeSpec[] mutated = new TypeSpec[targs.Length];
bool changed = false;
for (int i = 0; i < targs.Length; ++i) {
mutated[i] = Mutate (targs[i]);
changed |= targs[i] != mutated[i];
}
return changed ? mutated : targs;
}
}
/// <summary>
/// A TypeExpr which already resolved to a type parameter.
/// </summary>
public class TypeParameterExpr : TypeExpression
{
public TypeParameterExpr (TypeParameter type_parameter, Location loc)
: base (type_parameter.Type, loc)
{
this.eclass = ExprClass.TypeParameter;
}
}
public class InflatedTypeSpec : TypeSpec
{
TypeSpec[] targs;
TypeParameterSpec[] constraints;
readonly TypeSpec open_type;
readonly IModuleContext context;
public InflatedTypeSpec (IModuleContext context, TypeSpec openType, TypeSpec declaringType, TypeSpec[] targs)
: base (openType.Kind, declaringType, openType.MemberDefinition, null, openType.Modifiers)
{
if (targs == null)
throw new ArgumentNullException ("targs");
this.state &= ~SharedStateFlags;
this.state |= (openType.state & SharedStateFlags);
this.context = context;
this.open_type = openType;
this.targs = targs;
foreach (var arg in targs) {
if (arg.HasDynamicElement || arg.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
state |= StateFlags.HasDynamicElement;
break;
}
}
if (open_type.Kind == MemberKind.MissingType)
MemberCache = MemberCache.Empty;
if ((open_type.Modifiers & Modifiers.COMPILER_GENERATED) != 0)
state |= StateFlags.ConstraintsChecked;
}
#region Properties
public override TypeSpec BaseType {
get {
if (cache == null || (state & StateFlags.PendingBaseTypeInflate) != 0)
InitializeMemberCache (true);
return base.BaseType;
}
}
//
// Inflated type parameters with constraints array, mapping with type arguments is based on index
//
public TypeParameterSpec[] Constraints {
get {
if (constraints == null) {
constraints = TypeParameterSpec.InflateConstraints (MemberDefinition.TypeParameters, l => l.CreateLocalInflator (context), this);
}
return constraints;
}
}
//
// Used to cache expensive constraints validation on constructed types
//
public bool HasConstraintsChecked {
get {
return (state & StateFlags.ConstraintsChecked) != 0;
}
set {
state = value ? state | StateFlags.ConstraintsChecked : state & ~StateFlags.ConstraintsChecked;
}
}
public override IList<TypeSpec> Interfaces {
get {
if (cache == null)
InitializeMemberCache (true);
return base.Interfaces;
}
}
public override bool IsExpressionTreeType {
get {
return (open_type.state & StateFlags.InflatedExpressionType) != 0;
}
}
public override bool IsGenericIterateInterface {
get {
return (open_type.state & StateFlags.GenericIterateInterface) != 0;
}
}
public override bool IsGenericTask {
get {
return (open_type.state & StateFlags.GenericTask) != 0;
}
}
public override bool IsNullableType {
get {
return (open_type.state & StateFlags.InflatedNullableType) != 0;
}
}
//
// Types used to inflate the generic type
//
public override TypeSpec[] TypeArguments {
get {
return targs;
}
}
#endregion
public override bool AddInterface (TypeSpec iface)
{
var inflator = CreateLocalInflator (context);
iface = inflator.Inflate (iface);
if (iface == null)
return false;
return base.AddInterface (iface);
}
public static bool ContainsTypeParameter (TypeSpec type)
{
if (type.Kind == MemberKind.TypeParameter)
return true;
var element_container = type as ElementTypeSpec;
if (element_container != null)
return ContainsTypeParameter (element_container.Element);
foreach (var t in type.TypeArguments) {
if (ContainsTypeParameter (t)) {
return true;
}
}
return false;
}
TypeParameterInflator CreateLocalInflator (IModuleContext context)
{
TypeParameterSpec[] tparams_full;
TypeSpec[] targs_full = targs;
if (IsNested) {
//
// Special case is needed when we are inflating an open type (nested type definition)
// on inflated parent. Consider following case
//
// Foo<T>.Bar<U> => Foo<string>.Bar<U>
//
// Any later inflation of Foo<string>.Bar<U> has to also inflate T if used inside Bar<U>
//
List<TypeSpec> merged_targs = null;
List<TypeParameterSpec> merged_tparams = null;
var type = DeclaringType;
do {
if (type.TypeArguments.Length > 0) {
if (merged_targs == null) {
merged_targs = new List<TypeSpec> ();
merged_tparams = new List<TypeParameterSpec> ();
if (targs.Length > 0) {
merged_targs.AddRange (targs);
merged_tparams.AddRange (open_type.MemberDefinition.TypeParameters);
}
}
merged_tparams.AddRange (type.MemberDefinition.TypeParameters);
merged_targs.AddRange (type.TypeArguments);
}
type = type.DeclaringType;
} while (type != null);
if (merged_targs != null) {
// Type arguments are not in the right order but it should not matter in this case
targs_full = merged_targs.ToArray ();
tparams_full = merged_tparams.ToArray ();
} else if (targs.Length == 0) {
tparams_full = TypeParameterSpec.EmptyTypes;
} else {
tparams_full = open_type.MemberDefinition.TypeParameters;
}
} else if (targs.Length == 0) {
tparams_full = TypeParameterSpec.EmptyTypes;
} else {
tparams_full = open_type.MemberDefinition.TypeParameters;
}
return new TypeParameterInflator (context, this, tparams_full, targs_full);
}
MetaType CreateMetaInfo (TypeParameterMutator mutator)
{
//
// Converts nested type arguments into right order
// Foo<string, bool>.Bar<int> => string, bool, int
//
var all = new List<MetaType> ();
TypeSpec type = this;
TypeSpec definition = type;
do {
if (type.GetDefinition().IsGeneric) {
all.InsertRange (0,
type.TypeArguments != TypeSpec.EmptyTypes ?
type.TypeArguments.Select (l => l.GetMetaInfo ()) :
type.MemberDefinition.TypeParameters.Select (l => l.GetMetaInfo ()));
}
definition = definition.GetDefinition ();
type = type.DeclaringType;
} while (type != null);
return definition.GetMetaInfo ().MakeGenericType (all.ToArray ());
}
public override ObsoleteAttribute GetAttributeObsolete ()
{
return open_type.GetAttributeObsolete ();
}
protected override bool IsNotCLSCompliant (out bool attrValue)
{
if (base.IsNotCLSCompliant (out attrValue))
return true;
foreach (var ta in TypeArguments) {
if (ta.MemberDefinition.CLSAttributeValue == false)
return true;
}
return false;
}
public override TypeSpec GetDefinition ()
{
return open_type;
}
public override MetaType GetMetaInfo ()
{
if (info == null)
info = CreateMetaInfo (null);
return info;
}
public override string GetSignatureForError ()
{
if (IsNullableType)
return targs[0].GetSignatureForError () + "?";
return base.GetSignatureForError ();
}
protected override string GetTypeNameSignature ()
{
if (targs.Length == 0 || MemberDefinition is AnonymousTypeClass)
return null;
return "<" + TypeManager.CSharpName (targs) + ">";
}
public bool HasDynamicArgument ()
{
for (int i = 0; i < targs.Length; ++i) {
var item = targs[i];
if (item.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
return true;
if (item is InflatedTypeSpec) {
if (((InflatedTypeSpec) item).HasDynamicArgument ())
return true;
continue;
}
if (item.IsArray) {
while (item.IsArray) {
item = ((ArrayContainer) item).Element;
}
if (item.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
return true;
}
}
return false;
}
protected override void InitializeMemberCache (bool onlyTypes)
{
if (cache == null) {
var open_cache = onlyTypes ? open_type.MemberCacheTypes : open_type.MemberCache;
// Surprisingly, calling MemberCache on open type could meantime create cache on this type
// for imported type parameter constraints referencing nested type of this declaration
if (cache == null)
cache = new MemberCache (open_cache);
}
var inflator = CreateLocalInflator (context);
//
// Two stage inflate due to possible nested types recursive
// references
//
// class A<T> {
// B b;
// class B {
// T Value;
// }
// }
//
// When resolving type of `b' members of `B' cannot be
// inflated because are not yet available in membercache
//
if ((state & StateFlags.PendingMemberCacheMembers) == 0) {
open_type.MemberCacheTypes.InflateTypes (cache, inflator);
//
// Inflate any implemented interfaces
//
if (open_type.Interfaces != null) {
ifaces = new List<TypeSpec> (open_type.Interfaces.Count);
foreach (var iface in open_type.Interfaces) {
var iface_inflated = inflator.Inflate (iface);
if (iface_inflated == null)
continue;
base.AddInterface (iface_inflated);
}
}
//
// Handles the tricky case of recursive nested base generic type
//
// class A<T> : Base<A<T>.Nested> {
// class Nested {}
// }
//
// When inflating A<T>. base type is not yet known, secondary
// inflation is required (not common case) once base scope
// is known
//
if (open_type.BaseType == null) {
if (IsClass)
state |= StateFlags.PendingBaseTypeInflate;
} else {
BaseType = inflator.Inflate (open_type.BaseType);
}
} else if ((state & StateFlags.PendingBaseTypeInflate) != 0) {
//
// It can happen when resolving base type without being defined
// which is not allowed to happen and will always lead to an error
//
// class B { class N {} }
// class A<T> : A<B.N> {}
//
if (open_type.BaseType == null)
return;
BaseType = inflator.Inflate (open_type.BaseType);
state &= ~StateFlags.PendingBaseTypeInflate;
}
if (onlyTypes) {
state |= StateFlags.PendingMemberCacheMembers;
return;
}
var tc = open_type.MemberDefinition as TypeDefinition;
if (tc != null && !tc.HasMembersDefined) {
//
// Inflating MemberCache with undefined members
//
return;
}
if ((state & StateFlags.PendingBaseTypeInflate) != 0) {
BaseType = inflator.Inflate (open_type.BaseType);
state &= ~StateFlags.PendingBaseTypeInflate;
}
state &= ~StateFlags.PendingMemberCacheMembers;
open_type.MemberCache.InflateMembers (cache, open_type, inflator);
}
public override TypeSpec Mutate (TypeParameterMutator mutator)
{
var targs = TypeArguments;
if (targs != null)
targs = mutator.Mutate (targs);
var decl = DeclaringType;
if (IsNested && DeclaringType.IsGenericOrParentIsGeneric)
decl = mutator.Mutate (decl);
if (targs == TypeArguments && decl == DeclaringType)
return this;
var mutated = (InflatedTypeSpec) MemberwiseClone ();
if (decl != DeclaringType) {
// Gets back MethodInfo in case of metaInfo was inflated
//mutated.info = MemberCache.GetMember<TypeSpec> (DeclaringType.GetDefinition (), this).info;
mutated.declaringType = decl;
mutated.state |= StateFlags.PendingMetaInflate;
}
if (targs != null) {
mutated.targs = targs;
mutated.info = null;
}
return mutated;
}
}
//
// Tracks the type arguments when instantiating a generic type. It's used
// by both type arguments and type parameters
//
public class TypeArguments
{
List<FullNamedExpression> args;
TypeSpec[] atypes;
public List<FullNamedExpression> Args {
get { return this.args; }
}
public TypeArguments (params FullNamedExpression[] types)
{
this.args = new List<FullNamedExpression> (types);
}
public void Add (FullNamedExpression type)
{
args.Add (type);
}
/// <summary>
/// We may only be used after Resolve() is called and return the fully
/// resolved types.
/// </summary>
// TODO: Not needed, just return type from resolve
public TypeSpec[] Arguments {
get {
return atypes;
}
set {
atypes = value;
}
}
public int Count {
get {
return args.Count;
}
}
public virtual bool IsEmpty {
get {
return false;
}
}
public List<FullNamedExpression> TypeExpressions {
get {
return this.args;
}
}
public string GetSignatureForError()
{
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < Count; ++i) {
var expr = args[i];
if (expr != null)
sb.Append (expr.GetSignatureForError ());
if (i + 1 < Count)
sb.Append (',');
}
return sb.ToString ();
}
/// <summary>
/// Resolve the type arguments.
/// </summary>
public virtual bool Resolve (IMemberContext ec)
{
if (atypes != null)
return atypes.Length != 0;
int count = args.Count;
bool ok = true;
atypes = new TypeSpec [count];
for (int i = 0; i < count; i++){
var te = args[i].ResolveAsType (ec);
if (te == null) {
ok = false;
continue;
}
atypes[i] = te;
if (te.IsStatic) {
ec.Module.Compiler.Report.Error (718, args[i].Location, "`{0}': static classes cannot be used as generic arguments",
te.GetSignatureForError ());
ok = false;
}
if (te.IsPointer || te.IsSpecialRuntimeType) {
ec.Module.Compiler.Report.Error (306, args[i].Location,
"The type `{0}' may not be used as a type argument",
te.GetSignatureForError ());
ok = false;
}
}
if (!ok)
atypes = TypeSpec.EmptyTypes;
return ok;
}
public TypeArguments Clone ()
{
TypeArguments copy = new TypeArguments ();
foreach (var ta in args)
copy.args.Add (ta);
return copy;
}
}
public class UnboundTypeArguments : TypeArguments
{
public UnboundTypeArguments (int arity)
: base (new FullNamedExpression[arity])
{
}
public override bool IsEmpty {
get {
return true;
}
}
public override bool Resolve (IMemberContext ec)
{
// Nothing to be resolved
return true;
}
}
public class TypeParameters
{
List<TypeParameter> names;
TypeParameterSpec[] types;
public TypeParameters ()
{
names = new List<TypeParameter> ();
}
public TypeParameters (int count)
{
names = new List<TypeParameter> (count);
}
#region Properties
public int Count {
get {
return names.Count;
}
}
public TypeParameterSpec[] Types {
get {
return types;
}
}
#endregion
public void Add (TypeParameter tparam)
{
names.Add (tparam);
}
public void Add (TypeParameters tparams)
{
names.AddRange (tparams.names);
}
public void Create (TypeSpec declaringType, int parentOffset, TypeContainer parent)
{
types = new TypeParameterSpec[Count];
for (int i = 0; i < types.Length; ++i) {
var tp = names[i];
tp.Create (declaringType, parent);
types[i] = tp.Type;
types[i].DeclaredPosition = i + parentOffset;
if (tp.Variance != Variance.None && !(declaringType != null && (declaringType.Kind == MemberKind.Interface || declaringType.Kind == MemberKind.Delegate))) {
parent.Compiler.Report.Error (1960, tp.Location, "Variant type parameters can only be used with interfaces and delegates");
}
}
}
public void Define (GenericTypeParameterBuilder[] builders)
{
for (int i = 0; i < types.Length; ++i) {
var tp = names[i];
tp.Define (builders [types [i].DeclaredPosition]);
}
}
public TypeParameter this[int index] {
get {
return names [index];
}
set {
names[index] = value;
}
}
public TypeParameter Find (string name)
{
foreach (var tp in names) {
if (tp.Name == name)
return tp;
}
return null;
}
public string[] GetAllNames ()
{
return names.Select (l => l.Name).ToArray ();
}
public string GetSignatureForError ()
{
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < Count; ++i) {
if (i > 0)
sb.Append (',');
var name = names[i];
if (name != null)
sb.Append (name.GetSignatureForError ());
}
return sb.ToString ();
}
public void CheckPartialConstraints (Method part)
{
var partTypeParameters = part.CurrentTypeParameters;
for (int i = 0; i < Count; i++) {
var tp_a = names[i];
var tp_b = partTypeParameters [i];
if (tp_a.Constraints == null) {
if (tp_b.Constraints == null)
continue;
} else if (tp_b.Constraints != null && tp_a.Type.HasSameConstraintsDefinition (tp_b.Type)) {
continue;
}
part.Compiler.Report.SymbolRelatedToPreviousError (this[i].CurrentMemberDefinition.Location, "");
part.Compiler.Report.Error (761, part.Location,
"Partial method declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
part.GetSignatureForError (), partTypeParameters[i].GetSignatureForError ());
}
}
public void UpdateConstraints (TypeDefinition part)
{
var partTypeParameters = part.MemberName.TypeParameters;
for (int i = 0; i < Count; i++) {
var tp = names [i];
if (tp.AddPartialConstraints (part, partTypeParameters [i]))
continue;
part.Compiler.Report.SymbolRelatedToPreviousError (this[i].CurrentMemberDefinition);
part.Compiler.Report.Error (265, part.Location,
"Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
part.GetSignatureForError (), tp.GetSignatureForError ());
}
}
public void VerifyClsCompliance ()
{
foreach (var tp in names) {
tp.VerifyClsCompliance ();
}
}
}
//
// A type expression of generic type with type arguments
//
class GenericTypeExpr : TypeExpr
{
TypeArguments args;
TypeSpec open_type;
/// <summary>
/// Instantiate the generic type `t' with the type arguments `args'.
/// Use this constructor if you already know the fully resolved
/// generic type.
/// </summary>
public GenericTypeExpr (TypeSpec open_type, TypeArguments args, Location l)
{
this.open_type = open_type;
loc = l;
this.args = args;
}
public override string GetSignatureForError ()
{
return type.GetSignatureForError ();
}
public override TypeSpec ResolveAsType (IMemberContext mc)
{
if (eclass != ExprClass.Unresolved)
return type;
if (!args.Resolve (mc))
return null;
TypeSpec[] atypes = args.Arguments;
//
// Now bind the parameters
//
var inflated = open_type.MakeGenericType (mc, atypes);
type = inflated;
eclass = ExprClass.Type;
//
// The constraints can be checked only when full type hierarchy is known
//
if (!inflated.HasConstraintsChecked && mc.Module.HasTypesFullyDefined) {
var constraints = inflated.Constraints;
if (constraints != null) {
var cc = new ConstraintChecker (mc);
if (cc.CheckAll (open_type, atypes, constraints, loc)) {
inflated.HasConstraintsChecked = true;
}
}
}
return type;
}
public override bool Equals (object obj)
{
GenericTypeExpr cobj = obj as GenericTypeExpr;
if (cobj == null)
return false;
if ((type == null) || (cobj.type == null))
return false;
return type == cobj.type;
}
public override int GetHashCode ()
{
return base.GetHashCode ();
}
}
//
// Generic type with unbound type arguments, used for typeof (G<,,>)
//
class GenericOpenTypeExpr : TypeExpression
{
public GenericOpenTypeExpr (TypeSpec type, /*UnboundTypeArguments args,*/ Location loc)
: base (type.GetDefinition (), loc)
{
}
}
struct ConstraintChecker
{
IMemberContext mc;
bool recursive_checks;
public ConstraintChecker (IMemberContext ctx)
{
this.mc = ctx;
recursive_checks = false;
}
//
// Checks the constraints of open generic type against type
// arguments. This version is used for types which could not be
// checked immediatelly during construction because the type
// hierarchy was not yet fully setup (before Emit phase)
//
public static bool Check (IMemberContext mc, TypeSpec type, Location loc)
{
//
// Check declaring type first if there is any
//
if (type.DeclaringType != null && !Check (mc, type.DeclaringType, loc))
return false;
while (type is ElementTypeSpec)
type = ((ElementTypeSpec) type).Element;
if (type.Arity == 0)
return true;
var gtype = type as InflatedTypeSpec;
if (gtype == null)
return true;
var constraints = gtype.Constraints;
if (constraints == null)
return true;
if (gtype.HasConstraintsChecked)
return true;
var cc = new ConstraintChecker (mc);
cc.recursive_checks = true;
if (cc.CheckAll (gtype.GetDefinition (), type.TypeArguments, constraints, loc)) {
gtype.HasConstraintsChecked = true;
return true;
}
return false;
}
//
// Checks all type arguments againts type parameters constraints
// NOTE: It can run in probing mode when `this.mc' is null
//
public bool CheckAll (MemberSpec context, TypeSpec[] targs, TypeParameterSpec[] tparams, Location loc)
{
for (int i = 0; i < tparams.Length; i++) {
var targ = targs[i];
if (!CheckConstraint (context, targ, tparams [i], loc))
return false;
if (!recursive_checks)
continue;
if (!Check (mc, targ, loc))
return false;
}
return true;
}
bool CheckConstraint (MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, Location loc)
{
//
// First, check the `class' and `struct' constraints.
//
if (tparam.HasSpecialClass && !TypeSpec.IsReferenceType (atype)) {
if (mc != null) {
mc.Module.Compiler.Report.Error (452, loc,
"The type `{0}' must be a reference type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError ());
}
return false;
}
if (tparam.HasSpecialStruct && (!TypeSpec.IsValueType (atype) || atype.IsNullableType)) {
if (mc != null) {
mc.Module.Compiler.Report.Error (453, loc,
"The type `{0}' must be a non-nullable value type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError ());
}
return false;
}
bool ok = true;
//
// Check the class constraint
//
if (tparam.HasTypeConstraint) {
if (!CheckConversion (mc, context, atype, tparam, tparam.BaseType, loc)) {
if (mc == null)
return false;
ok = false;
}
}
//
// Check the interfaces constraints
//
if (tparam.Interfaces != null) {
foreach (TypeSpec iface in tparam.Interfaces) {
if (!CheckConversion (mc, context, atype, tparam, iface, loc)) {
if (mc == null)
return false;
ok = false;
break;
}
}
}
//
// Check the type parameter constraint
//
if (tparam.TypeArguments != null) {
foreach (var ta in tparam.TypeArguments) {
if (!CheckConversion (mc, context, atype, tparam, ta, loc)) {
if (mc == null)
return false;
ok = false;
break;
}
}
}
//
// Finally, check the constructor constraint.
//
if (!tparam.HasSpecialConstructor)
return ok;
if (!HasDefaultConstructor (atype)) {
if (mc != null) {
mc.Module.Compiler.Report.SymbolRelatedToPreviousError (atype);
mc.Module.Compiler.Report.Error (310, loc,
"The type `{0}' must have a public parameterless constructor in order to use it as parameter `{1}' in the generic type or method `{2}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError ());
}
return false;
}
return ok;
}
static bool HasDynamicTypeArgument (TypeSpec[] targs)
{
for (int i = 0; i < targs.Length; ++i) {
var targ = targs [i];
if (targ.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
return true;
if (HasDynamicTypeArgument (targ.TypeArguments))
return true;
}
return false;
}
bool CheckConversion (IMemberContext mc, MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, TypeSpec ttype, Location loc)
{
if (atype == ttype)
return true;
if (atype.IsGenericParameter) {
var tps = (TypeParameterSpec) atype;
if (tps.HasDependencyOn (ttype))
return true;
if (Convert.ImplicitTypeParameterConversion (null, tps, ttype) != null)
return true;
} else if (TypeSpec.IsValueType (atype)) {
if (atype.IsNullableType) {
//
// LAMESPEC: Only identity or base type ValueType or Object satisfy nullable type
//
if (TypeSpec.IsBaseClass (atype, ttype, false))
return true;
} else {
if (Convert.ImplicitBoxingConversion (null, atype, ttype) != null)
return true;
}
} else {
if (Convert.ImplicitReferenceConversionExists (atype, ttype) || Convert.ImplicitBoxingConversion (null, atype, ttype) != null)
return true;
}
if (mc != null) {
mc.Module.Compiler.Report.SymbolRelatedToPreviousError (tparam);
if (atype.IsGenericParameter) {
mc.Module.Compiler.Report.Error (314, loc,
"The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing or type parameter conversion from `{0}' to `{3}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
} else if (TypeSpec.IsValueType (atype)) {
if (atype.IsNullableType) {
if (ttype.IsInterface) {
mc.Module.Compiler.Report.Error (313, loc,
"The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. The nullable type `{0}' never satisfies interface constraint `{3}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
} else {
mc.Module.Compiler.Report.Error (312, loc,
"The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. The nullable type `{0}' does not satisfy constraint `{3}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
}
} else {
mc.Module.Compiler.Report.Error (315, loc,
"The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing conversion from `{0}' to `{3}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
}
} else {
mc.Module.Compiler.Report.Error (311, loc,
"The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no implicit reference conversion from `{0}' to `{3}'",
atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
}
}
return false;
}
static bool HasDefaultConstructor (TypeSpec atype)
{
var tp = atype as TypeParameterSpec;
if (tp != null) {
return tp.HasSpecialConstructor || tp.HasSpecialStruct;
}
if (atype.IsStruct || atype.IsEnum)
return true;
if (atype.IsAbstract)
return false;
var tdef = atype.GetDefinition ();
var found = MemberCache.FindMember (tdef,
MemberFilter.Constructor (ParametersCompiled.EmptyReadOnlyParameters),
BindingRestriction.DeclaredOnly | BindingRestriction.InstanceOnly);
return found != null && (found.Modifiers & Modifiers.PUBLIC) != 0;
}
}
public partial class TypeManager
{
public static Variance CheckTypeVariance (TypeSpec t, Variance expected, IMemberContext member)
{
var tp = t as TypeParameterSpec;
if (tp != null) {
Variance v = tp.Variance;
if (expected == Variance.None && v != expected ||
expected == Variance.Covariant && v == Variance.Contravariant ||
expected == Variance.Contravariant && v == Variance.Covariant) {
((TypeParameter)tp.MemberDefinition).ErrorInvalidVariance (member, expected);
}
return expected;
}
if (t.TypeArguments.Length > 0) {
var targs_definition = t.MemberDefinition.TypeParameters;
TypeSpec[] targs = GetTypeArguments (t);
for (int i = 0; i < targs.Length; ++i) {
Variance v = targs_definition[i].Variance;
CheckTypeVariance (targs[i], (Variance) ((int)v * (int)expected), member);
}
return expected;
}
if (t.IsArray)
return CheckTypeVariance (GetElementType (t), expected, member);
return Variance.None;
}
}
//
// Implements C# type inference
//
class TypeInference
{
//
// Tracks successful rate of type inference
//
int score = int.MaxValue;
readonly Arguments arguments;
readonly int arg_count;
public TypeInference (Arguments arguments)
{
this.arguments = arguments;
if (arguments != null)
arg_count = arguments.Count;
}
public int InferenceScore {
get {
return score;
}
}
public TypeSpec[] InferMethodArguments (ResolveContext ec, MethodSpec method)
{
var method_generic_args = method.GenericDefinition.TypeParameters;
TypeInferenceContext context = new TypeInferenceContext (method_generic_args);
if (!context.UnfixedVariableExists)
return TypeSpec.EmptyTypes;
AParametersCollection pd = method.Parameters;
if (!InferInPhases (ec, context, pd))
return null;
return context.InferredTypeArguments;
}
//
// Implements method type arguments inference
//
bool InferInPhases (ResolveContext ec, TypeInferenceContext tic, AParametersCollection methodParameters)
{
int params_arguments_start;
if (methodParameters.HasParams) {
params_arguments_start = methodParameters.Count - 1;
} else {
params_arguments_start = arg_count;
}
TypeSpec [] ptypes = methodParameters.Types;
//
// The first inference phase
//
TypeSpec method_parameter = null;
for (int i = 0; i < arg_count; i++) {
Argument a = arguments [i];
if (a == null)
continue;
if (i < params_arguments_start) {
method_parameter = methodParameters.Types [i];
} else if (i == params_arguments_start) {
if (arg_count == params_arguments_start + 1 && TypeManager.HasElementType (a.Type))
method_parameter = methodParameters.Types [params_arguments_start];
else
method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
ptypes = (TypeSpec[]) ptypes.Clone ();
ptypes [i] = method_parameter;
}
//
// When a lambda expression, an anonymous method
// is used an explicit argument type inference takes a place
//
AnonymousMethodExpression am = a.Expr as AnonymousMethodExpression;
if (am != null) {
if (am.ExplicitTypeInference (ec, tic, method_parameter))
--score;
continue;
}
if (a.IsByRef) {
score -= tic.ExactInference (a.Type, method_parameter);
continue;
}
if (a.Expr.Type == InternalType.NullLiteral)
continue;
if (TypeSpec.IsValueType (method_parameter)) {
score -= tic.LowerBoundInference (a.Type, method_parameter);
continue;
}
//
// Otherwise an output type inference is made
//
score -= tic.OutputTypeInference (ec, a.Expr, method_parameter);
}
//
// Part of the second phase but because it happens only once
// we don't need to call it in cycle
//
bool fixed_any = false;
if (!tic.FixIndependentTypeArguments (ec, ptypes, ref fixed_any))
return false;
return DoSecondPhase (ec, tic, ptypes, !fixed_any);
}
bool DoSecondPhase (ResolveContext ec, TypeInferenceContext tic, TypeSpec[] methodParameters, bool fixDependent)
{
bool fixed_any = false;
if (fixDependent && !tic.FixDependentTypes (ec, ref fixed_any))
return false;
// If no further unfixed type variables exist, type inference succeeds
if (!tic.UnfixedVariableExists)
return true;
if (!fixed_any && fixDependent)
return false;
// For all arguments where the corresponding argument output types
// contain unfixed type variables but the input types do not,
// an output type inference is made
for (int i = 0; i < arg_count; i++) {
// Align params arguments
TypeSpec t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
if (!t_i.IsDelegate) {
if (!t_i.IsExpressionTreeType)
continue;
t_i = TypeManager.GetTypeArguments (t_i) [0];
}
var mi = Delegate.GetInvokeMethod (t_i);
TypeSpec rtype = mi.ReturnType;
if (tic.IsReturnTypeNonDependent (ec, mi, rtype)) {
// It can be null for default arguments
if (arguments[i] == null)
continue;
score -= tic.OutputTypeInference (ec, arguments[i].Expr, t_i);
}
}
return DoSecondPhase (ec, tic, methodParameters, true);
}
}
public class TypeInferenceContext
{
protected enum BoundKind
{
Exact = 0,
Lower = 1,
Upper = 2
}
struct BoundInfo : IEquatable<BoundInfo>
{
public readonly TypeSpec Type;
public readonly BoundKind Kind;
public BoundInfo (TypeSpec type, BoundKind kind)
{
this.Type = type;
this.Kind = kind;
}
public override int GetHashCode ()
{
return Type.GetHashCode ();
}
public Expression GetTypeExpression ()
{
return new TypeExpression (Type, Location.Null);
}
#region IEquatable<BoundInfo> Members
public bool Equals (BoundInfo other)
{
return Type == other.Type && Kind == other.Kind;
}
#endregion
}
readonly TypeSpec[] tp_args;
readonly TypeSpec[] fixed_types;
readonly List<BoundInfo>[] bounds;
bool failed;
// TODO MemberCache: Could it be TypeParameterSpec[] ??
public TypeInferenceContext (TypeSpec[] typeArguments)
{
if (typeArguments.Length == 0)
throw new ArgumentException ("Empty generic arguments");
fixed_types = new TypeSpec [typeArguments.Length];
for (int i = 0; i < typeArguments.Length; ++i) {
if (typeArguments [i].IsGenericParameter) {
if (bounds == null) {
bounds = new List<BoundInfo> [typeArguments.Length];
tp_args = new TypeSpec [typeArguments.Length];
}
tp_args [i] = typeArguments [i];
} else {
fixed_types [i] = typeArguments [i];
}
}
}
//
// Used together with AddCommonTypeBound fo implement
// 7.4.2.13 Finding the best common type of a set of expressions
//
public TypeInferenceContext ()
{
fixed_types = new TypeSpec [1];
tp_args = new TypeSpec [1];
tp_args[0] = InternalType.Arglist; // it can be any internal type
bounds = new List<BoundInfo> [1];
}
public TypeSpec[] InferredTypeArguments {
get {
return fixed_types;
}
}
public void AddCommonTypeBound (TypeSpec type)
{
AddToBounds (new BoundInfo (type, BoundKind.Lower), 0);
}
void AddToBounds (BoundInfo bound, int index)
{
//
// Some types cannot be used as type arguments
//
if (bound.Type.Kind == MemberKind.Void || bound.Type.IsPointer || bound.Type.IsSpecialRuntimeType ||
bound.Type == InternalType.MethodGroup || bound.Type == InternalType.AnonymousMethod)
return;
var a = bounds [index];
if (a == null) {
a = new List<BoundInfo> (2);
a.Add (bound);
bounds [index] = a;
return;
}
if (a.Contains (bound))
return;
a.Add (bound);
}
bool AllTypesAreFixed (TypeSpec[] types)
{
foreach (TypeSpec t in types) {
if (t.IsGenericParameter) {
if (!IsFixed (t))
return false;
continue;
}
if (TypeManager.IsGenericType (t))
return AllTypesAreFixed (TypeManager.GetTypeArguments (t));
}
return true;
}
//
// 26.3.3.8 Exact Inference
//
public int ExactInference (TypeSpec u, TypeSpec v)
{
// If V is an array type
if (v.IsArray) {
if (!u.IsArray)
return 0;
var ac_u = (ArrayContainer) u;
var ac_v = (ArrayContainer) v;
if (ac_u.Rank != ac_v.Rank)
return 0;
return ExactInference (ac_u.Element, ac_v.Element);
}
// If V is constructed type and U is constructed type
if (TypeManager.IsGenericType (v)) {
if (!TypeManager.IsGenericType (u) || v.MemberDefinition != u.MemberDefinition)
return 0;
TypeSpec [] ga_u = TypeManager.GetTypeArguments (u);
TypeSpec [] ga_v = TypeManager.GetTypeArguments (v);
if (ga_u.Length != ga_v.Length)
return 0;
int score = 0;
for (int i = 0; i < ga_u.Length; ++i)
score += ExactInference (ga_u [i], ga_v [i]);
return System.Math.Min (1, score);
}
// If V is one of the unfixed type arguments
int pos = IsUnfixed (v);
if (pos == -1)
return 0;
AddToBounds (new BoundInfo (u, BoundKind.Exact), pos);
return 1;
}
public bool FixAllTypes (ResolveContext ec)
{
for (int i = 0; i < tp_args.Length; ++i) {
if (!FixType (ec, i))
return false;
}
return true;
}
//
// All unfixed type variables Xi are fixed for which all of the following hold:
// a, There is at least one type variable Xj that depends on Xi
// b, Xi has a non-empty set of bounds
//
public bool FixDependentTypes (ResolveContext ec, ref bool fixed_any)
{
for (int i = 0; i < tp_args.Length; ++i) {
if (fixed_types[i] != null)
continue;
if (bounds[i] == null)
continue;
if (!FixType (ec, i))
return false;
fixed_any = true;
}
return true;
}
//
// All unfixed type variables Xi which depend on no Xj are fixed
//
public bool FixIndependentTypeArguments (ResolveContext ec, TypeSpec[] methodParameters, ref bool fixed_any)
{
var types_to_fix = new List<TypeSpec> (tp_args);
for (int i = 0; i < methodParameters.Length; ++i) {
TypeSpec t = methodParameters[i];
if (!t.IsDelegate) {
if (!t.IsExpressionTreeType)
continue;
t = TypeManager.GetTypeArguments (t) [0];
}
if (t.IsGenericParameter)
continue;
var invoke = Delegate.GetInvokeMethod (t);
TypeSpec rtype = invoke.ReturnType;
while (rtype.IsArray)
rtype = ((ArrayContainer) rtype).Element;
if (!rtype.IsGenericParameter && !TypeManager.IsGenericType (rtype))
continue;
// Remove dependent types, they cannot be fixed yet
RemoveDependentTypes (types_to_fix, rtype);
}
foreach (TypeSpec t in types_to_fix) {
if (t == null)
continue;
int idx = IsUnfixed (t);
if (idx >= 0 && !FixType (ec, idx)) {
return false;
}
}
fixed_any = types_to_fix.Count > 0;
return true;
}
//
// 26.3.3.10 Fixing
//
public bool FixType (ResolveContext ec, int i)
{
// It's already fixed
if (fixed_types[i] != null)
throw new InternalErrorException ("Type argument has been already fixed");
if (failed)
return false;
var candidates = bounds [i];
if (candidates == null)
return false;
if (candidates.Count == 1) {
TypeSpec t = candidates[0].Type;
if (t == InternalType.NullLiteral)
return false;
fixed_types [i] = t;
return true;
}
//
// Determines a unique type from which there is
// a standard implicit conversion to all the other
// candidate types.
//
TypeSpec best_candidate = null;
int cii;
int candidates_count = candidates.Count;
for (int ci = 0; ci < candidates_count; ++ci) {
BoundInfo bound = candidates [ci];
for (cii = 0; cii < candidates_count; ++cii) {
if (cii == ci)
continue;
BoundInfo cbound = candidates[cii];
// Same type parameters with different bounds
if (cbound.Type == bound.Type) {
if (bound.Kind != BoundKind.Exact)
bound = cbound;
continue;
}
if (bound.Kind == BoundKind.Exact || cbound.Kind == BoundKind.Exact) {
if (cbound.Kind == BoundKind.Lower) {
if (!Convert.ImplicitConversionExists (ec, cbound.GetTypeExpression (), bound.Type)) {
break;
}
continue;
}
if (cbound.Kind == BoundKind.Upper) {
if (!Convert.ImplicitConversionExists (ec, bound.GetTypeExpression (), cbound.Type)) {
break;
}
continue;
}
if (bound.Kind != BoundKind.Exact) {
if (!Convert.ImplicitConversionExists (ec, bound.GetTypeExpression (), cbound.Type)) {
break;
}
bound = cbound;
continue;
}
break;
}
if (bound.Kind == BoundKind.Lower) {
if (cbound.Kind == BoundKind.Lower) {
if (!Convert.ImplicitConversionExists (ec, cbound.GetTypeExpression (), bound.Type)) {
break;
}
} else {
if (!Convert.ImplicitConversionExists (ec, bound.GetTypeExpression (), cbound.Type)) {
break;
}
bound = cbound;
}
continue;
}
if (bound.Kind == BoundKind.Upper) {
if (!Convert.ImplicitConversionExists (ec, bound.GetTypeExpression (), cbound.Type)) {
break;
}
} else {
throw new NotImplementedException ("variance conversion");
}
}
if (cii != candidates_count)
continue;
//
// We already have the best candidate, break if thet are different
//
// Dynamic is never ambiguous as we prefer dynamic over other best candidate types
//
if (best_candidate != null) {
if (best_candidate.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
continue;
if (bound.Type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && best_candidate != bound.Type)
return false;
}
best_candidate = bound.Type;
}
if (best_candidate == null)
return false;
fixed_types[i] = best_candidate;
return true;
}
public bool HasBounds (int pos)
{
return bounds[pos] != null;
}
//
// Uses inferred or partially infered types to inflate delegate type argument. Returns
// null when type parameter has not been fixed
//
public TypeSpec InflateGenericArgument (IModuleContext context, TypeSpec parameter)
{
var tp = parameter as TypeParameterSpec;
if (tp != null) {
//
// Type inference works on generic arguments (MVAR) only
//
if (!tp.IsMethodOwned)
return parameter;
//
// Ensure the type parameter belongs to same container
//
if (tp.DeclaredPosition < tp_args.Length && tp_args[tp.DeclaredPosition] == parameter)
return fixed_types[tp.DeclaredPosition] ?? parameter;
return parameter;
}
var gt = parameter as InflatedTypeSpec;
if (gt != null) {
var inflated_targs = new TypeSpec [gt.TypeArguments.Length];
for (int ii = 0; ii < inflated_targs.Length; ++ii) {
var inflated = InflateGenericArgument (context, gt.TypeArguments [ii]);
if (inflated == null)
return null;
inflated_targs[ii] = inflated;
}
return gt.GetDefinition ().MakeGenericType (context, inflated_targs);
}
var ac = parameter as ArrayContainer;
if (ac != null) {
var inflated = InflateGenericArgument (context, ac.Element);
if (inflated != ac.Element)
return ArrayContainer.MakeType (context.Module, inflated);
}
return parameter;
}
//
// Tests whether all delegate input arguments are fixed and generic output type
// requires output type inference
//
public bool IsReturnTypeNonDependent (ResolveContext ec, MethodSpec invoke, TypeSpec returnType)
{
while (returnType.IsArray)
returnType = ((ArrayContainer) returnType).Element;
if (returnType.IsGenericParameter) {
if (IsFixed (returnType))
return false;
} else if (TypeManager.IsGenericType (returnType)) {
if (returnType.IsDelegate) {
invoke = Delegate.GetInvokeMethod (returnType);
return IsReturnTypeNonDependent (ec, invoke, invoke.ReturnType);
}
TypeSpec[] g_args = TypeManager.GetTypeArguments (returnType);
// At least one unfixed return type has to exist
if (AllTypesAreFixed (g_args))
return false;
} else {
return false;
}
// All generic input arguments have to be fixed
AParametersCollection d_parameters = invoke.Parameters;
return AllTypesAreFixed (d_parameters.Types);
}
bool IsFixed (TypeSpec type)
{
return IsUnfixed (type) == -1;
}
int IsUnfixed (TypeSpec type)
{
if (!type.IsGenericParameter)
return -1;
for (int i = 0; i < tp_args.Length; ++i) {
if (tp_args[i] == type) {
if (fixed_types[i] != null)
break;
return i;
}
}
return -1;
}
//
// 26.3.3.9 Lower-bound Inference
//
public int LowerBoundInference (TypeSpec u, TypeSpec v)
{
return LowerBoundInference (u, v, false);
}
//
// Lower-bound (false) or Upper-bound (true) inference based on inversed argument
//
int LowerBoundInference (TypeSpec u, TypeSpec v, bool inversed)
{
// If V is one of the unfixed type arguments
int pos = IsUnfixed (v);
if (pos != -1) {
AddToBounds (new BoundInfo (u, inversed ? BoundKind.Upper : BoundKind.Lower), pos);
return 1;
}
// If U is an array type
var u_ac = u as ArrayContainer;
if (u_ac != null) {
var v_ac = v as ArrayContainer;
if (v_ac != null) {
if (u_ac.Rank != v_ac.Rank)
return 0;
if (TypeSpec.IsValueType (u_ac.Element))
return ExactInference (u_ac.Element, v_ac.Element);
return LowerBoundInference (u_ac.Element, v_ac.Element, inversed);
}
if (u_ac.Rank != 1 || !v.IsGenericIterateInterface)
return 0;
var v_i = TypeManager.GetTypeArguments (v) [0];
if (TypeSpec.IsValueType (u_ac.Element))
return ExactInference (u_ac.Element, v_i);
return LowerBoundInference (u_ac.Element, v_i);
}
if (v.IsGenericOrParentIsGeneric) {
//
// if V is a constructed type C<V1..Vk> and there is a unique type C<U1..Uk>
// such that U is identical to, inherits from (directly or indirectly),
// or implements (directly or indirectly) C<U1..Uk>
//
var u_candidates = new List<TypeSpec> ();
var open_v = v.MemberDefinition;
for (TypeSpec t = u; t != null; t = t.BaseType) {
if (open_v == t.MemberDefinition)
u_candidates.Add (t);
//
// Using this trick for dynamic type inference, the spec says the type arguments are "unknown" but
// that would complicate the process a lot, instead I treat them as dynamic
//
if (t.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
u_candidates.Add (t);
}
if (u.Interfaces != null) {
foreach (var iface in u.Interfaces) {
if (open_v == iface.MemberDefinition)
u_candidates.Add (iface);
}
}
TypeSpec[] unique_candidate_targs = null;
var ga_v = TypeSpec.GetAllTypeArguments (v);
foreach (TypeSpec u_candidate in u_candidates) {
//
// The unique set of types U1..Uk means that if we have an interface I<T>,
// class U : I<int>, I<long> then no type inference is made when inferring
// type I<T> by applying type U because T could be int or long
//
if (unique_candidate_targs != null) {
TypeSpec[] second_unique_candidate_targs = TypeSpec.GetAllTypeArguments (u_candidate);
if (TypeSpecComparer.Equals (unique_candidate_targs, second_unique_candidate_targs)) {
unique_candidate_targs = second_unique_candidate_targs;
continue;
}
//
// This should always cause type inference failure
//
failed = true;
return 1;
}
//
// A candidate is dynamic type expression, to simplify things use dynamic
// for all type parameter of this type. For methods like this one
//
// void M<T, U> (IList<T>, IList<U[]>)
//
// dynamic becomes both T and U when the arguments are of dynamic type
//
if (u_candidate.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
unique_candidate_targs = new TypeSpec[ga_v.Length];
for (int i = 0; i < unique_candidate_targs.Length; ++i)
unique_candidate_targs[i] = u_candidate;
} else {
unique_candidate_targs = TypeSpec.GetAllTypeArguments (u_candidate);
}
}
if (unique_candidate_targs != null) {
int score = 0;
int tp_index = -1;
TypeParameterSpec[] tps = null;
for (int i = 0; i < unique_candidate_targs.Length; ++i) {
if (tp_index < 0) {
while (v.Arity == 0)
v = v.DeclaringType;
tps = v.MemberDefinition.TypeParameters;
tp_index = tps.Length - 1;
}
Variance variance = tps [tp_index--].Variance;
TypeSpec u_i = unique_candidate_targs [i];
if (variance == Variance.None || TypeSpec.IsValueType (u_i)) {
if (ExactInference (u_i, ga_v [i]) == 0)
++score;
} else {
bool upper_bound = (variance == Variance.Contravariant && !inversed) ||
(variance == Variance.Covariant && inversed);
if (LowerBoundInference (u_i, ga_v [i], upper_bound) == 0)
++score;
}
}
return score;
}
}
return 0;
}
//
// 26.3.3.6 Output Type Inference
//
public int OutputTypeInference (ResolveContext ec, Expression e, TypeSpec t)
{
// If e is a lambda or anonymous method with inferred return type
AnonymousMethodExpression ame = e as AnonymousMethodExpression;
if (ame != null) {
TypeSpec rt = ame.InferReturnType (ec, this, t);
var invoke = Delegate.GetInvokeMethod (t);
if (rt == null) {
AParametersCollection pd = invoke.Parameters;
return ame.Parameters.Count == pd.Count ? 1 : 0;
}
TypeSpec rtype = invoke.ReturnType;
return LowerBoundInference (rt, rtype) + 1;
}
//
// if E is a method group and T is a delegate type or expression tree type
// return type Tb with parameter types T1..Tk and return type Tb, and overload
// resolution of E with the types T1..Tk yields a single method with return type U,
// then a lower-bound inference is made from U for Tb.
//
if (e is MethodGroupExpr) {
if (!t.IsDelegate) {
if (!t.IsExpressionTreeType)
return 0;
t = TypeManager.GetTypeArguments (t)[0];
}
var invoke = Delegate.GetInvokeMethod (t);
TypeSpec rtype = invoke.ReturnType;
if (!IsReturnTypeNonDependent (ec, invoke, rtype))
return 0;
// LAMESPEC: Standard does not specify that all methodgroup arguments
// has to be fixed but it does not specify how to do recursive type inference
// either. We choose the simple option and infer return type only
// if all delegate generic arguments are fixed.
TypeSpec[] param_types = new TypeSpec [invoke.Parameters.Count];
for (int i = 0; i < param_types.Length; ++i) {
var inflated = InflateGenericArgument (ec, invoke.Parameters.Types[i]);
if (inflated == null)
return 0;
param_types[i] = inflated;
}
MethodGroupExpr mg = (MethodGroupExpr) e;
Arguments args = DelegateCreation.CreateDelegateMethodArguments (ec, invoke.Parameters, param_types, e.Location);
mg = mg.OverloadResolve (ec, ref args, null, OverloadResolver.Restrictions.CovariantDelegate | OverloadResolver.Restrictions.ProbingOnly);
if (mg == null)
return 0;
return LowerBoundInference (mg.BestCandidateReturnType, rtype) + 1;
}
//
// if e is an expression with type U, then
// a lower-bound inference is made from U for T
//
return LowerBoundInference (e.Type, t) * 2;
}
void RemoveDependentTypes (List<TypeSpec> types, TypeSpec returnType)
{
int idx = IsUnfixed (returnType);
if (idx >= 0) {
types [idx] = null;
return;
}
if (TypeManager.IsGenericType (returnType)) {
foreach (TypeSpec t in TypeManager.GetTypeArguments (returnType)) {
RemoveDependentTypes (types, t);
}
}
}
public bool UnfixedVariableExists {
get {
foreach (TypeSpec ut in fixed_types) {
if (ut == null)
return true;
}
return false;
}
}
}
}
|