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 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527
|
{
Copyright (c) 2000-2002 by Florian Klaempfl
Type checking and register allocation for type converting nodes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit ncnv;
{$i fpcdefs.inc}
interface
uses
node,
symtype,
defutil,defcmp,
nld
;
type
ttypeconvnodeflag = (
{ the typeconvnode is a proc_2_procvar, generated internally by an
address operator, such as @proc, Addr(proc), Ofs(proc) or Seg(proc),
which is then going to be converted to a void pointer. Why does it
matter? Because, on i8086 far code memory models you're allowed to
take the address of a _near_ procedure as a void pointer (which the
@ operator does in TP mode), but not as a procvar (in that case the
procedure must be far). }
tcnf_proc_2_procvar_2_voidpointer,
{ proc_2_procvar, generated internally by Ofs() }
tcnf_proc_2_procvar_get_offset_only
);
ttypeconvnodeflags = set of ttypeconvnodeflag;
ttypeconvnode = class(tunarynode)
totypedef : tdef;
totypedefderef : tderef;
convtype : tconverttype;
convnodeflags : ttypeconvnodeflags;
warn_pointer_to_signed,
assignment_side: boolean;
constructor create(node : tnode;def:tdef);virtual;
constructor create_explicit(node : tnode;def:tdef);
constructor create_internal(node : tnode;def:tdef);
constructor create_proc_to_procvar(node : tnode);
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
procedure buildderefimpl;override;
procedure derefimpl;override;
function dogetcopy : tnode;override;
procedure printnodeinfo(var t : text);override;
function pass_1 : tnode;override;
function pass_typecheck:tnode;override;
function simplify(forinline : boolean):tnode; override;
procedure mark_write;override;
function docompare(p: tnode) : boolean; override;
function retains_value_location:boolean;
function assign_allowed:boolean;
procedure second_call_helper(c : tconverttype);
{ always called before any other type conversion checks. If it
returns true, the type conversion is ok and no further checks/
handling are required. }
function target_specific_general_typeconv: boolean;virtual;
{ called in case of a valid explicit type conversion. Can be used to
replace this explicit type conversion with a different node, or to
reject it after all }
function target_specific_explicit_typeconv: boolean;virtual;
{ called when inserttypeconv is used to convert to a def that is equal
according to compare_defs() }
class function target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean; virtual;
protected
function typecheck_int_to_int : tnode; virtual;
function typecheck_cord_to_pointer : tnode; virtual;
function typecheck_chararray_to_string : tnode; virtual;
function typecheck_string_to_chararray : tnode; virtual;
function typecheck_string_to_string : tnode; virtual;
function typecheck_char_to_string : tnode; virtual;
function typecheck_char_to_chararray : tnode; virtual;
function typecheck_int_to_real : tnode; virtual;
function typecheck_real_to_real : tnode; virtual;
function typecheck_real_to_currency : tnode; virtual;
function typecheck_cchar_to_pchar : tnode; virtual;
function typecheck_cstring_to_pchar : tnode; virtual;
function typecheck_cstring_to_int : tnode; virtual;
function typecheck_char_to_char : tnode; virtual;
function typecheck_arrayconstructor_to_set : tnode; virtual;
function typecheck_set_to_set : tnode; virtual;
function typecheck_pchar_to_string : tnode; virtual;
function typecheck_interface_to_string : tnode; virtual;
function typecheck_interface_to_guid : tnode; virtual;
function typecheck_dynarray_to_openarray : tnode; virtual;
function typecheck_pwchar_to_string : tnode; virtual;
function typecheck_variant_to_dynarray : tnode; virtual;
function typecheck_dynarray_to_variant : tnode; virtual;
function typecheck_variant_to_enum : tnode; virtual;
function typecheck_enum_to_variant : tnode; virtual;
function typecheck_proc_to_procvar : tnode; virtual;
function typecheck_variant_to_interface : tnode; virtual;
function typecheck_interface_to_variant : tnode; virtual;
function typecheck_array_2_dynarray : tnode; virtual;
function typecheck_elem_2_openarray : tnode; virtual;
function typecheck_arrayconstructor_to_dynarray : tnode; virtual;
private
function _typecheck_int_to_int : tnode;
function _typecheck_cord_to_pointer : tnode;
function _typecheck_chararray_to_string : tnode;
function _typecheck_string_to_chararray : tnode;
function _typecheck_string_to_string : tnode;
function _typecheck_char_to_string : tnode;
function _typecheck_char_to_chararray : tnode;
function _typecheck_int_to_real : tnode;
function _typecheck_real_to_real : tnode;
function _typecheck_real_to_currency : tnode;
function _typecheck_cchar_to_pchar : tnode;
function _typecheck_cstring_to_pchar : tnode;
function _typecheck_cstring_to_int : tnode;
function _typecheck_char_to_char : tnode;
function _typecheck_arrayconstructor_to_set : tnode;
function _typecheck_set_to_set : tnode;
function _typecheck_pchar_to_string : tnode;
function _typecheck_interface_to_string : tnode;
function _typecheck_interface_to_guid : tnode;
function _typecheck_dynarray_to_openarray : tnode;
function _typecheck_pwchar_to_string : tnode;
function _typecheck_variant_to_dynarray : tnode;
function _typecheck_dynarray_to_variant : tnode;
function _typecheck_variant_to_enum : tnode;
function _typecheck_enum_to_variant : tnode;
function _typecheck_proc_to_procvar : tnode;
function _typecheck_variant_to_interface : tnode;
function _typecheck_interface_to_variant : tnode;
function _typecheck_array_2_dynarray : tnode;
function _typecheck_elem_2_openarray : tnode;
function _typecheck_arrayconstructor_to_dynarray: tnode;
protected
function first_int_to_int : tnode;virtual;
function first_cstring_to_pchar : tnode;virtual;
function first_cstring_to_int : tnode;virtual;
function first_string_to_chararray : tnode;virtual;
function first_char_to_string : tnode;virtual;
function first_char_to_chararray : tnode; virtual;
function first_nothing : tnode;virtual;
function first_array_to_pointer : tnode;virtual;
function first_int_to_real : tnode;virtual;
function first_real_to_real : tnode;virtual;
function first_pointer_to_array : tnode;virtual;
function first_cchar_to_pchar : tnode;virtual;
function first_bool_to_int : tnode;virtual;
function first_int_to_bool : tnode;virtual;
function first_bool_to_bool : tnode;virtual;
function first_proc_to_procvar : tnode;virtual;
function first_nil_to_methodprocvar : tnode;virtual;
function first_set_to_set : tnode;virtual;
function first_cord_to_pointer : tnode;virtual;
function first_ansistring_to_pchar : tnode;virtual;
function first_arrayconstructor_to_set : tnode;virtual;
function first_class_to_intf : tnode;virtual;
function first_char_to_char : tnode;virtual;
function first_string_to_string : tnode;virtual;
function first_call_helper(c : tconverttype) : tnode;
function typecheck_call_helper(c : tconverttype) : tnode;
private
{ these wrapper are necessary, because the first_* stuff is called }
{ through a table. Without the wrappers override wouldn't have }
{ any effect }
function _first_int_to_int : tnode;
function _first_cstring_to_pchar : tnode;
function _first_cstring_to_int : tnode;
function _first_string_to_chararray : tnode;
function _first_char_to_string : tnode;
function _first_char_to_chararray : tnode;
function _first_nothing : tnode;
function _first_array_to_pointer : tnode;
function _first_int_to_real : tnode;
function _first_real_to_real: tnode;
function _first_pointer_to_array : tnode;
function _first_cchar_to_pchar : tnode;
function _first_bool_to_int : tnode;
function _first_int_to_bool : tnode;
function _first_bool_to_bool : tnode;
function _first_proc_to_procvar : tnode;
function _first_nil_to_methodprocvar : tnode;
function _first_cord_to_pointer : tnode;
function _first_ansistring_to_pchar : tnode;
function _first_arrayconstructor_to_set : tnode;
function _first_class_to_intf : tnode;
function _first_char_to_char : tnode;
function _first_set_to_set : tnode;
function _first_string_to_string : tnode;
procedure _second_int_to_int;virtual;
procedure _second_string_to_string;virtual;
procedure _second_cstring_to_pchar;virtual;
procedure _second_cstring_to_int;virtual;
procedure _second_string_to_chararray;virtual;
procedure _second_array_to_pointer;virtual;
procedure _second_pointer_to_array;virtual;
procedure _second_chararray_to_string;virtual;
procedure _second_char_to_string;virtual;
procedure _second_int_to_real;virtual;
procedure _second_real_to_real;virtual;
procedure _second_cord_to_pointer;virtual;
procedure _second_proc_to_procvar;virtual;
procedure _second_nil_to_methodprocvar;virtual;
procedure _second_bool_to_int;virtual;
procedure _second_int_to_bool;virtual;
procedure _second_bool_to_bool;virtual;
procedure _second_set_to_set;virtual;
procedure _second_ansistring_to_pchar;virtual;
procedure _second_class_to_intf;virtual;
procedure _second_char_to_char;virtual;
procedure _second_elem_to_openarray;virtual;
procedure _second_nothing; virtual;
protected
procedure second_int_to_int;virtual;abstract;
procedure second_string_to_string;virtual;abstract;
procedure second_cstring_to_pchar;virtual;abstract;
procedure second_cstring_to_int;virtual;abstract;
procedure second_string_to_chararray;virtual;abstract;
procedure second_array_to_pointer;virtual;abstract;
procedure second_pointer_to_array;virtual;abstract;
procedure second_chararray_to_string;virtual;abstract;
procedure second_char_to_string;virtual;abstract;
procedure second_int_to_real;virtual;abstract;
procedure second_real_to_real;virtual;abstract;
procedure second_cord_to_pointer;virtual;abstract;
procedure second_proc_to_procvar;virtual;abstract;
procedure second_nil_to_methodprocvar;virtual;abstract;
procedure second_bool_to_int;virtual;abstract;
procedure second_int_to_bool;virtual;abstract;
procedure second_bool_to_bool;virtual;abstract;
procedure second_set_to_set;virtual;abstract;
procedure second_ansistring_to_pchar;virtual;abstract;
procedure second_class_to_intf;virtual;abstract;
procedure second_char_to_char;virtual;abstract;
procedure second_elem_to_openarray;virtual;abstract;
procedure second_nothing; virtual;abstract;
end;
ttypeconvnodeclass = class of ttypeconvnode;
{ common functionality of as-nodes and is-nodes }
tasisnode = class(tbinarynode)
protected
{ if non-standard usage of as-nodes is possible, targets can override
this method and return true in case the conditions are fulfilled }
function target_specific_typecheck: boolean;virtual;
public
function pass_typecheck:tnode;override;
end;
tasnode = class(tasisnode)
{ as nodes cannot be translated directly into call nodes bcause:
When using -CR, explicit class typecasts are replaced with as-nodes to perform
class type checking. The problem is that if a typecasted class instance is
passed as a var-parameter, then you cannot replace it with a function call. So the as-node
a) call the as helper to perform the type checking
b) still pass the original instance as parameter to var-parameters
(and in general: to return it as the result of the as-node)
so the call field is required
}
call: tnode;
constructor create(l,r : tnode);virtual;
constructor create_internal(l,r : tnode);virtual;
function pass_1 : tnode;override;
function dogetcopy: tnode;override;
function docompare(p: tnode): boolean; override;
destructor destroy; override;
end;
tasnodeclass = class of tasnode;
tisnode = class(tasisnode)
constructor create(l,r : tnode);virtual;
constructor create_internal(l,r : tnode);virtual;
function pass_1 : tnode;override;
procedure pass_generate_code;override;
end;
tisnodeclass = class of tisnode;
var
ctypeconvnode : ttypeconvnodeclass = ttypeconvnode;
casnode : tasnodeclass = tasnode;
cisnode : tisnodeclass=tisnode;
procedure inserttypeconv(var p:tnode;def:tdef);
procedure inserttypeconv_explicit(var p:tnode;def:tdef);
procedure inserttypeconv_internal(var p:tnode;def:tdef);
procedure arrayconstructor_to_set(var p : tnode);inline;
function arrayconstructor_to_set(p:tnode;freep:boolean):tnode;
function arrayconstructor_can_be_set(p:tnode):boolean;
procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
function maybe_global_proc_to_nested(var fromnode: tnode; todef: tdef): boolean;
implementation
uses
globtype,systems,constexp,compinnr,
cutils,verbose,globals,widestr,
symconst,symdef,symsym,symcpu,symtable,
ncon,ncal,nset,nadd,nmem,nmat,nbas,nutils,ninl,
cgbase,procinfo,
htypechk,blockutl,pass_1,cpuinfo;
{*****************************************************************************
Helpers
*****************************************************************************}
type
ttypeconvnodetype = (tct_implicit,tct_explicit,tct_internal);
procedure do_inserttypeconv(var p: tnode;def: tdef; convtype: ttypeconvnodetype);
begin
if not assigned(p.resultdef) then
begin
typecheckpass(p);
if codegenerror then
exit;
end;
{ don't insert superfluous type conversions, but
in case of bitpacked accesses, the original type must
remain too so that not too many/few bits are laoded.
Also, in case the deftyp changes, don't ignore because lots of code
expects that if the resultdef is set to e.g. stringdef, it remains
that way (e.g., in case of Java where java_jlstring equals
unicodestring according to equal_defs, but an add node for strings
still expects the resultdef of the node to be a stringdef) }
if equal_defs(p.resultdef,def) and
(p.resultdef.typ=def.typ) and
not is_bitpacked_access(p) and
((p.blocktype=bt_const) or
not ctypeconvnode.target_specific_need_equal_typeconv(p.resultdef,def)) then
begin
{ don't replace encoded string constants to rawbytestring encoding.
preserve the codepage }
if not (is_rawbytestring(def) and (p.nodetype=stringconstn)) then
p.resultdef:=def
end
else
begin
case convtype of
tct_implicit:
p:=ctypeconvnode.create(p,def);
tct_explicit:
p:=ctypeconvnode.create_explicit(p,def);
tct_internal:
p:=ctypeconvnode.create_internal(p,def);
end;
p.fileinfo:=ttypeconvnode(p).left.fileinfo;
typecheckpass(p);
end;
end;
procedure inserttypeconv(var p:tnode;def:tdef);
begin
do_inserttypeconv(p,def,tct_implicit);
end;
procedure inserttypeconv_explicit(var p: tnode; def: tdef);
begin
do_inserttypeconv(p,def,tct_explicit);
end;
procedure inserttypeconv_internal(var p:tnode;def:tdef);
begin
do_inserttypeconv(p,def,tct_internal);
end;
{*****************************************************************************
Array constructor to Set Conversion
*****************************************************************************}
procedure arrayconstructor_to_set(var p : tnode);
begin
p:=arrayconstructor_to_set(p,true);
end;
function arrayconstructor_to_set(p:tnode;freep:boolean):tnode;
var
constp : tsetconstnode;
p2,p3,p4 : tnode;
hdef : tdef;
constset : Pconstset;
constsetlo,
constsethi : TConstExprInt;
procedure update_constsethi(def:tdef; maybetruncenumrange: boolean);
begin
if (def.typ=orddef) and
((torddef(def).high>=constsethi) or
(torddef(def).low <=constsetlo)) then
begin
if torddef(def).ordtype=uwidechar then
begin
constsethi:=255;
constsetlo:=0;
if hdef=nil then
hdef:=def;
end
else
begin
if (torddef(def).high>=constsethi) then
constsethi:=torddef(def).high;
if (torddef(def).low<=constsetlo) then
constsetlo:=torddef(def).low;
if hdef=nil then
begin
if (constsethi>255) or
(torddef(def).low<0) then
hdef:=u8inttype
else
hdef:=def;
end;
if constsethi>255 then
constsethi:=255;
if constsetlo<0 then
constsetlo:=0;
end;
end
else if (def.typ=enumdef) and
((tenumdef(def).max>=constsethi) or
(tenumdef(def).min<=constsetlo)) then
begin
if hdef=nil then
hdef:=def;
if (tenumdef(def).max>=constsethi) then
constsethi:=tenumdef(def).max;
if (tenumdef(def).min<=constsetlo) then
constsetlo:=tenumdef(def).min;
{ for constant set elements, delphi allows the usage of elements of enumerations which
have value>255 if there is no element with a value > 255 used }
if (maybetruncenumrange) then
begin
if constsethi>255 then
constsethi:=255;
if constsetlo<0 then
constsetlo:=0;
end;
end;
end;
procedure do_set(pos : longint);
begin
if (pos and not $ff)<>0 then
Message(parser_e_illegal_set_expr);
if pos>constsethi then
constsethi:=pos;
if pos<constsetlo then
constsetlo:=pos;
if pos in constset^ then
Message(parser_e_illegal_set_expr);
include(constset^,pos);
end;
var
l : Longint;
lr,hr : TConstExprInt;
hp : tarrayconstructornode;
oldfilepos: tfileposinfo;
begin
{ keep in sync with arrayconstructor_can_be_set }
if p.nodetype<>arrayconstructorn then
internalerror(200205105);
new(constset);
constset^:=[];
hdef:=nil;
{ make sure to set constsetlo correctly for empty sets }
if assigned(tarrayconstructornode(p).left) then
constsetlo:=high(aint)
else
constsetlo:=0;
constsethi:=0;
constp:=csetconstnode.create(nil,hdef);
constp.value_set:=constset;
result:=constp;
hp:=tarrayconstructornode(p);
if assigned(hp.left) then
begin
while assigned(hp) do
begin
p4:=nil; { will contain the tree to create the set }
{split a range into p2 and p3 }
if hp.left.nodetype=arrayconstructorrangen then
begin
p2:=tarrayconstructorrangenode(hp.left).left;
p3:=tarrayconstructorrangenode(hp.left).right;
tarrayconstructorrangenode(hp.left).left:=nil;
tarrayconstructorrangenode(hp.left).right:=nil;
end
else
begin
p2:=hp.left;
hp.left:=nil;
p3:=nil;
end;
typecheckpass(p2);
set_varstate(p2,vs_read,[vsf_must_be_valid]);
if assigned(p3) then
begin
typecheckpass(p3);
set_varstate(p3,vs_read,[vsf_must_be_valid]);
end;
if codegenerror then
break;
oldfilepos:=current_filepos;
current_filepos:=p2.fileinfo;
case p2.resultdef.typ of
enumdef,
orddef:
begin
{ widechars are not yet supported }
if is_widechar(p2.resultdef) then
begin
inserttypeconv(p2,cansichartype);
if (p2.nodetype<>ordconstn) then
incompatibletypes(cwidechartype,cansichartype);
end;
getrange(p2.resultdef,lr,hr);
if assigned(p3) then
begin
if is_widechar(p3.resultdef) then
begin
inserttypeconv(p3,cansichartype);
if (p3.nodetype<>ordconstn) then
begin
current_filepos:=p3.fileinfo;
incompatibletypes(cwidechartype,cansichartype);
end;
end;
{ this isn't good, you'll get problems with
type t010 = 0..10;
ts = set of t010;
var s : ts;b : t010
begin s:=[1,2,b]; end.
if is_integer(p3^.resultdef) then
begin
inserttypeconv(p3,u8bitdef);
end;
}
if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
begin
CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
end
else
begin
if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
begin
if not(is_integer(p3.resultdef)) then
hdef:=p3.resultdef
else
begin
inserttypeconv(p3,u8inttype);
inserttypeconv(p2,u8inttype);
end;
if tordconstnode(p2).value.svalue>tordconstnode(p3).value.svalue then
CGMessagePos(p2.fileinfo,type_w_empty_constant_range_set);
for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
do_set(l);
p2.free;
p3.free;
end
else
begin
update_constsethi(p2.resultdef,false);
inserttypeconv(p2,hdef);
update_constsethi(p3.resultdef,false);
inserttypeconv(p3,hdef);
if assigned(hdef) then
inserttypeconv(p3,hdef)
else
inserttypeconv(p3,u8inttype);
p4:=csetelementnode.create(p2,p3);
end;
end;
end
else
begin
{ Single value }
if p2.nodetype=ordconstn then
begin
if not(is_integer(p2.resultdef)) then
update_constsethi(p2.resultdef,true);
if assigned(hdef) then
inserttypeconv(p2,hdef)
else
inserttypeconv(p2,u8inttype);
do_set(tordconstnode(p2).value.svalue);
p2.free;
end
else
begin
update_constsethi(p2.resultdef,false);
if assigned(hdef) then
inserttypeconv(p2,hdef)
else
inserttypeconv(p2,u8inttype);
p4:=csetelementnode.create(p2,nil);
end;
end;
end;
stringdef :
begin
if (p2.nodetype<>stringconstn) then
Message(parser_e_illegal_expression)
{ if we've already set elements which are constants }
{ throw an error }
else if ((hdef=nil) and assigned(result)) or
not(is_char(hdef)) then
CGMessage(type_e_typeconflict_in_set)
else
for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
if hdef=nil then
hdef:=cansichartype;
p2.free;
end;
else
CGMessage(type_e_ordinal_expr_expected);
end;
{ insert the set creation tree }
if assigned(p4) then
result:=caddnode.create(addn,result,p4);
{ load next and dispose current node }
p2:=hp;
hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
tarrayconstructornode(p2).right:=nil;
if freep then
p2.free;
current_filepos:=oldfilepos;
end;
if (hdef=nil) then
hdef:=u8inttype;
end
else
begin
{ empty set [], only remove node }
if freep then
p.free;
end;
{ set the initial set type }
constp.resultdef:=csetdef.create(hdef,constsetlo.svalue,constsethi.svalue,true);
{ determine the resultdef for the tree }
typecheckpass(result);
end;
function arrayconstructor_can_be_set(p:tnode):boolean;
var
p1,p2 : tnode;
hdef : tdef;
begin
{ keep in sync with arrayconstructor_to_set }
if not assigned(p) then
internalerror(2015050401);
if not assigned(tarrayconstructornode(p).left) then
begin
if assigned(tarrayconstructornode(p).right) then
internalerror(2015050103);
result:=true;
end
else
begin
result:=false;
hdef:=nil;
while assigned(p) do
begin
if tarrayconstructornode(p).left.nodetype=arrayconstructorrangen then
begin
p1:=tarrayconstructorrangenode(tarrayconstructornode(p).left).left;
p2:=tarrayconstructorrangenode(tarrayconstructornode(p).left).right;
end
else
begin
p1:=tarrayconstructornode(p).left;
p2:=nil;
end;
case p1.resultdef.typ of
orddef,
enumdef:
begin
if is_widechar(p1.resultdef) then
begin
if p1.nodetype<>ordconstn then
exit
else if tordconstnode(p1).value.uvalue>high(byte) then
exit;
end;
if assigned(p2) then
begin
if is_widechar(p2.resultdef) then
begin
if p2.nodetype<>ordconstn then
exit
else if tordconstnode(p2).value.uvalue>high(byte) then
exit;
end;
{ anything to exclude? }
end
else
begin
{ anything to exclude? }
end;
end;
stringdef:
if p1.nodetype<>stringconstn then
exit
else if assigned(hdef) and not is_char(hdef) then
exit;
else
exit;
end;
p:=tarrayconstructornode(p).right;
end;
result:=true;
end;
end;
procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
begin
{ procvars without arguments in variant arrays are always called by
Delphi }
if not(iscvarargs) then
maybe_call_procvar(p,true);
if not(iscvarargs) and
(p.nodetype=stringconstn) and
{ don't cast to AnsiString if already casted to Wide/UnicodeString, issue #18266 }
(tstringconstnode(p).cst_type in [cst_conststring,cst_shortstring,cst_longstring]) then
p:=ctypeconvnode.create_internal(p,getansistringdef)
else
case p.resultdef.typ of
enumdef :
p:=ctypeconvnode.create_internal(p,s32inttype);
arraydef :
begin
if is_chararray(p.resultdef) then
p:=ctypeconvnode.create_internal(p,charpointertype)
else
if is_widechararray(p.resultdef) then
p:=ctypeconvnode.create_internal(p,widecharpointertype)
else
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
end;
orddef :
begin
if is_integer(p.resultdef) and
not(is_64bitint(p.resultdef)) then
if not(m_delphi in current_settings.modeswitches) then
p:=ctypeconvnode.create(p,s32inttype)
else
{ delphi doesn't generate a range error when passing a
cardinal >= $80000000, but since these are seen as
longint on the callee side, this causes data loss;
as a result, we require an explicit longint()
typecast in FPC mode on the caller side if range
checking should be disabled, but not in Delphi mode }
p:=ctypeconvnode.create_internal(p,s32inttype)
else if is_void(p.resultdef) then
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
else if iscvarargs and is_currency(p.resultdef)
and (current_settings.fputype<>fpu_none) then
p:=ctypeconvnode.create(p,s64floattype);
end;
floatdef :
if not(iscvarargs) then
begin
if not(is_currency(p.resultdef)) then
p:=ctypeconvnode.create(p,pbestrealtype^);
end
else
begin
if is_constrealnode(p) and
not(nf_explicit in p.flags) then
MessagePos(p.fileinfo,type_w_double_c_varargs);
if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
(is_constrealnode(p) and
not(nf_explicit in p.flags)) then
p:=ctypeconvnode.create(p,s64floattype);
end;
procvardef :
p:=ctypeconvnode.create(p,voidpointertype);
stringdef:
if iscvarargs then
p:=ctypeconvnode.create(p,charpointertype);
variantdef:
if iscvarargs then
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
{ maybe warn in case it's not using "packrecords c"? }
recorddef:
if not iscvarargs then
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
pointerdef:
;
classrefdef:
if iscvarargs then
p:=ctypeconvnode.create(p,voidpointertype);
objectdef :
if is_objc_class_or_protocol(p.resultdef) then
p:=ctypeconvnode.create(p,voidpointertype)
else if iscvarargs or
is_object(p.resultdef) then
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
else
else
CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
end;
typecheckpass(p);
end;
{ in FPC mode, @procname immediately has to be evaluated as a
procvar. If procname is global, then this will be a global
procvar. Since converting global procvars to local procvars is
not allowed (see point d in defcmp.proc_to_procvar_equal()),
this results in errors when passing global procedures to local
procvar parameters or assigning them to nested procvars. The
solution is to remove the (wrong) conversion to a global procvar,
and instead insert a conversion to the local procvar type. }
function maybe_global_proc_to_nested(var fromnode: tnode; todef: tdef): boolean;
var
hp: tnode;
begin
result:=false;
if (m_nested_procvars in current_settings.modeswitches) and
not(m_tp_procvar in current_settings.modeswitches) and
(todef.typ=procvardef) and
is_nested_pd(tprocvardef(todef)) and
(fromnode.nodetype=typeconvn) and
(ttypeconvnode(fromnode).convtype=tc_proc_2_procvar) and
not is_nested_pd(tprocvardef(fromnode.resultdef)) and
(proc_to_procvar_equal(tprocdef(ttypeconvnode(fromnode).left.resultdef),tprocvardef(todef),false)>=te_convert_l1) then
begin
hp:=fromnode;
fromnode:=ctypeconvnode.create_proc_to_procvar(ttypeconvnode(fromnode).left);
ttypeconvnode(fromnode).totypedef:=todef;
typecheckpass(fromnode);
ttypeconvnode(hp).left:=nil;
hp.free;
result:=true;
end;
end;
{ similar as above, but for assigning @classtype.method to a
procvar of object. pexpr.do_proc_call() stores the symtable of classtype
in the loadnode so we can retrieve it here (rather than the symtable in
which method was found, which may be a parent class) }
function maybe_classmethod_to_methodprocvar(var fromnode: tnode; todef: tdef): boolean;
var
hp: tnode;
begin
result:=false;
if not(m_tp_procvar in current_settings.modeswitches) and
(todef.typ=procvardef) and
is_methodpointer(tprocvardef(todef)) and
(fromnode.nodetype=typeconvn) and
(ttypeconvnode(fromnode).convtype=tc_proc_2_procvar) and
is_methodpointer(fromnode.resultdef) and
(po_classmethod in tprocvardef(fromnode.resultdef).procoptions) and
not(po_staticmethod in tprocvardef(fromnode.resultdef).procoptions) and
(proc_to_procvar_equal(tprocdef(ttypeconvnode(fromnode).left.resultdef),tprocvardef(todef),false)>=te_convert_l1) then
begin
hp:=fromnode;
fromnode:=ttypeconvnode(fromnode).left;
if (fromnode.nodetype=loadn) and
not assigned(tloadnode(fromnode).left) then
tloadnode(fromnode).set_mp(cloadvmtaddrnode.create(ctypenode.create(tdef(tloadnode(fromnode).symtable.defowner))));
fromnode:=ctypeconvnode.create_proc_to_procvar(fromnode);
ttypeconvnode(fromnode).totypedef:=todef;
typecheckpass(fromnode);
ttypeconvnode(hp).left:=nil;
hp.free;
result:=true;
end;
end;
{*****************************************************************************
TTYPECONVNODE
*****************************************************************************}
constructor ttypeconvnode.create(node : tnode;def:tdef);
begin
inherited create(typeconvn,node);
convtype:=tc_none;
convnodeflags:=[];
totypedef:=def;
if def=nil then
internalerror(200103281);
fileinfo:=node.fileinfo;
{An attempt to convert the result of a floating point division
(with the / operator) to an integer type will fail. Give a hint
to use the div operator.}
if (node.nodetype=slashn) and (def.typ=orddef) then
cgmessage(type_h_use_div_for_int);
{In expressions like int64:=longint+longint, an integer overflow could be avoided
by simply converting the operands to int64 first. Give a hint to do this.}
if (node.nodetype in [addn,subn,muln]) and
(def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
case node.nodetype of
addn:
cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.typename);
subn:
cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.typename);
muln:
cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.typename);
end;
end;
constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
begin
self.create(node,def);
include(flags,nf_explicit);
end;
constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
begin
self.create(node,def);
{ handle like explicit conversions }
include(flags,nf_explicit);
include(flags,nf_internal);
end;
constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
begin
self.create(node,voidtype);
convtype:=tc_proc_2_procvar;
end;
constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
ppufile.getderef(totypedefderef);
convtype:=tconverttype(ppufile.getbyte);
ppufile.getsmallset(convnodeflags);
end;
procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
ppufile.putderef(totypedefderef);
ppufile.putbyte(byte(convtype));
ppufile.putsmallset(convnodeflags);
end;
procedure ttypeconvnode.buildderefimpl;
begin
inherited buildderefimpl;
totypedefderef.build(totypedef);
end;
procedure ttypeconvnode.derefimpl;
begin
inherited derefimpl;
totypedef:=tdef(totypedefderef.resolve);
end;
function ttypeconvnode.dogetcopy : tnode;
var
n : ttypeconvnode;
begin
n:=ttypeconvnode(inherited dogetcopy);
n.convtype:=convtype;
n.convnodeflags:=convnodeflags;
n.totypedef:=totypedef;
n.assignment_side:=assignment_side;
dogetcopy:=n;
end;
procedure ttypeconvnode.printnodeinfo(var t : text);
var
first: Boolean;
i: ttypeconvnodeflag;
begin
inherited printnodeinfo(t);
write(t,', convtype = ',convtype);
write(t,', convnodeflags = [');
first:=true;
for i:=low(ttypeconvnodeflag) to high(ttypeconvnodeflag) do
if i in convnodeflags then
begin
if not first then
write(t,',')
else
first:=false;
write(t,i);
end;
write(t,']');
end;
function ttypeconvnode.typecheck_cord_to_pointer : tnode;
begin
result:=nil;
if left.nodetype=ordconstn then
begin
{ check if we have a valid pointer constant (JM) }
{$if sizeof(pointer) > sizeof(TConstPtrUInt)}
{$if sizeof(TConstPtrUInt) = 4}
if (tordconstnode(left).value < int64(low(longint))) or
(tordconstnode(left).value > int64(high(cardinal))) then
CGMessage(parser_e_range_check_error);
{$else} {$if sizeof(TConstPtrUInt) = 8}
if (tordconstnode(left).value < int64(low(int64))) or
(tordconstnode(left).value > int64(high(qword))) then
CGMessage(parser_e_range_check_error);
{$else}
internalerror(2001020801);
{$endif} {$endif}
{$endif}
if not(nf_explicit in flags) then
if (tordconstnode(left).value.svalue=0) then
CGMessage(type_w_zero_to_nil)
else
{ in Delphi mode, these aren't caught in compare_defs_ext }
IncompatibleTypes(left.resultdef,resultdef);
result:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
end
else
internalerror(200104023);
end;
function ttypeconvnode.typecheck_chararray_to_string : tnode;
var
chartype : string[8];
newblock : tblocknode;
newstat : tstatementnode;
restemp : ttempcreatenode;
begin
if is_widechar(tarraydef(left.resultdef).elementdef) then
chartype:='widechar'
else
chartype:='char';
if tstringdef(resultdef).stringtype=st_shortstring then
begin
newblock:=internalstatements(newstat);
restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
addstatement(newstat,restemp);
addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
ccallparanode.create(cordconstnode.create(
ord(tarraydef(left.resultdef).lowrange=0),pasbool1type,false),
ccallparanode.create(left,ccallparanode.create(
ctemprefnode.create(restemp),nil)))));
addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
addstatement(newstat,ctemprefnode.create(restemp));
result:=newblock;
end
else if (tstringdef(resultdef).stringtype=st_ansistring) then
begin
result:=ccallnode.createinternres(
'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(
cordconstnode.create(
ord(tarraydef(left.resultdef).lowrange=0),
pasbool1type,
false
),
ccallparanode.create(
cordconstnode.create(
getparaencoding(resultdef),
u16inttype,
true
),
ccallparanode.create(left,nil)
)
),
resultdef
);
end
else
result:=ccallnode.createinternres(
'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(cordconstnode.create(
ord(tarraydef(left.resultdef).lowrange=0),pasbool1type,false),
ccallparanode.create(left,nil)),resultdef);
left:=nil;
end;
function ttypeconvnode.typecheck_string_to_chararray : tnode;
var
newblock : tblocknode;
newstat : tstatementnode;
restemp : ttempcreatenode;
pchtemp : pchar;
arrsize : aint;
chartype : string[8];
begin
result := nil;
with tarraydef(resultdef) do
begin
if highrange<lowrange then
internalerror(200501051);
arrsize := highrange-lowrange+1;
end;
if (left.nodetype = stringconstn) and
(tstringconstnode(left).cst_type=cst_conststring) then
begin
{ if the array of char is large enough we can use the string
constant directly. This is handled in ncgcnv }
if (arrsize>=tstringconstnode(left).len) and
is_char(tarraydef(resultdef).elementdef) then
begin
{ pad the constant string with #0 to the array len }
{ (2.0.x compatible) }
if (arrsize>tstringconstnode(left).len) then
begin
pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
left.free;
left:=cstringconstnode.createpchar(pchtemp,arrsize,nil);
typecheckpass(left);
end;
exit;
end;
{ Convert to wide/short/ansistring and call default helper }
if is_widechar(tarraydef(resultdef).elementdef) then
inserttypeconv(left,cunicodestringtype)
else
begin
if tstringconstnode(left).len>255 then
inserttypeconv(left,getansistringdef)
else
inserttypeconv(left,cshortstringtype);
end;
end;
if is_widechar(tarraydef(resultdef).elementdef) then
chartype:='widechar'
else
chartype:='char';
newblock:=internalstatements(newstat);
restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
addstatement(newstat,restemp);
addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
'_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
ctemprefnode.create(restemp),nil))));
addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
addstatement(newstat,ctemprefnode.create(restemp));
result:=newblock;
left:=nil;
end;
function ttypeconvnode.typecheck_char_to_string : tnode;
var
procname: string[31];
para : tcallparanode;
hp : tstringconstnode;
ws : pcompilerwidestring;
sa : ansistring;
cw : tcompilerwidechar;
l : SizeUInt;
exprtype : tdef;
begin
result:=nil;
sa:='';
if (left.nodetype=ordconstn) and
((tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring]) or
(torddef(left.resultdef).ordtype in [uchar,uwidechar])) then
begin
if (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
begin
initwidestring(ws);
if torddef(left.resultdef).ordtype=uwidechar then
concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
else
concatwidestringchar(ws,asciichar2unicode(chr(tordconstnode(left).value.uvalue)));
hp:=cstringconstnode.createunistr(ws);
hp.changestringtype(resultdef);
donewidestring(ws);
end
else
begin
if (torddef(left.resultdef).ordtype=uwidechar) then
begin
if (current_settings.sourcecodepage<>CP_UTF8) then
begin
if tordconstnode(left).value.uvalue>127 then
begin
Message(type_w_unicode_data_loss);
// compiler has different codepage than a system running an application
// to prevent wrong codepage and data loss we are converting unicode char
// using a helper routine. This is not delphi compatible behavior.
// Delphi converts UniocodeChar to ansistring at the compile time
// old behavior:
// hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
para:=ccallparanode.create(left,nil);
if tstringdef(resultdef).stringtype=st_ansistring then
para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
result:=ccallnode.createinternres('fpc_uchar_to_'+tstringdef(resultdef).stringtypname,
para,resultdef);
left:=nil;
exit;
end
else
hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
end
else
begin
cw:=tcompilerwidechar(tordconstnode(left).value.uvalue);
SetLength(sa,5);
l:=UnicodeToUtf8(@(sa[1]),Length(sa),@cw,1);
SetLength(sa,l-1);
hp:=cstringconstnode.createstr(sa);
end
end
else
hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
{ output string consts in local ansistring encoding }
if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
tstringconstnode(hp).changestringtype(getansistringdef)
else
tstringconstnode(hp).changestringtype(resultdef);
end;
result:=hp;
end
else
{ shortstrings are handled 'inline' (except for widechars) }
if (tstringdef(resultdef).stringtype<>st_shortstring) or
(torddef(left.resultdef).ordtype=uwidechar) or
(target_info.system in systems_managed_vm) then
begin
{ parameter }
para:=ccallparanode.create(left,nil);
{ encoding required? }
if tstringdef(resultdef).stringtype=st_ansistring then
para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
{ create the procname }
if torddef(left.resultdef).ordtype<>uwidechar then
begin
procname:='fpc_char_to_';
if tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring] then
if nf_explicit in flags then
Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
else
Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
end
else
begin
procname:='fpc_uchar_to_';
if not (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
if nf_explicit in flags then
Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
else
Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
end;
procname:=procname+tstringdef(resultdef).stringtypname;
{ and finally the call }
result:=ccallnode.createinternres(procname,para,resultdef);
left := nil;
end
else
begin
{ use at least u16inttype }
{$ifdef cpu8bitalu}
exprtype:=u16inttype;
{$else cpu8bitalu}
exprtype:=uinttype;
{$endif cpu8bitalu}
{ create word(byte(char) shl 8 or 1) for litte endian machines }
{ and word(byte(char) or 256) for big endian machines }
left := ctypeconvnode.create_internal(left,exprtype);
if (target_info.endian = endian_little) then
left := caddnode.create(orn,
cshlshrnode.create(shln,left,cordconstnode.create(8,exprtype,false)),
cordconstnode.create(1,exprtype,false))
else
left := caddnode.create(orn,left,
cordconstnode.create(1 shl 8,exprtype,false));
left := ctypeconvnode.create_internal(left,u16inttype);
typecheckpass(left);
end;
end;
function ttypeconvnode.typecheck_string_to_string : tnode;
begin
result:=nil;
if (left.nodetype=stringconstn) and
(((tstringdef(resultdef).stringtype=st_ansistring) and
(tstringdef(resultdef).encoding<>CP_NONE)
)
) and
(tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) then
begin
tstringconstnode(left).changestringtype(resultdef);
Result:=left;
left:=nil;
end
else if (tstringdef(resultdef).stringtype=st_ansistring) and
(tstringdef(left.resultdef).stringtype=st_ansistring) and
(tstringdef(resultdef).encoding<>tstringdef(left.resultdef).encoding) then
begin
result:=ccallnode.createinternres(
'fpc_ansistr_to_ansistr',
ccallparanode.create(
cordconstnode.create(
tstringdef(resultdef).encoding,
u16inttype,
true
),
ccallparanode.create(left,nil)
),
resultdef
);
left:=nil;
end
else if (left.nodetype=stringconstn) and
(tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
(tstringdef(resultdef).stringtype=st_shortstring) then
begin
if not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)) then
begin
tstringconstnode(left).changestringtype(resultdef);
Result:=left;
left:=nil;
end;
end
else if (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
not (tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
begin
if nf_explicit in flags then
Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
else
Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
end
else if not (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
(tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
begin
if nf_explicit in flags then
Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
else
Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
end
end;
function ttypeconvnode.typecheck_char_to_chararray : tnode;
begin
result:=nil;
end;
function ttypeconvnode.typecheck_char_to_char : tnode;
var
hp : tordconstnode;
begin
result:=nil;
if (left.nodetype=ordconstn) and
((torddef(resultdef).ordtype<>uchar) or
(torddef(left.resultdef).ordtype<>uwidechar) or
(current_settings.sourcecodepage<>CP_UTF8))
then
begin
if (torddef(resultdef).ordtype=uchar) and
(torddef(left.resultdef).ordtype=uwidechar) and
(current_settings.sourcecodepage<>CP_UTF8) then
begin
if tordconstnode(left).value.uvalue>127 then
Message(type_w_unicode_data_loss);
hp:=cordconstnode.create(
ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
cansichartype,true);
result:=hp;
end
else if (torddef(resultdef).ordtype=uwidechar) and
(torddef(left.resultdef).ordtype=uchar) then
begin
hp:=cordconstnode.create(
asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
cwidechartype,true);
result:=hp;
end
else
internalerror(200105131);
exit;
end;
end;
function ttypeconvnode.typecheck_int_to_int : tnode;
var
v : TConstExprInt;
begin
result:=nil;
if left.nodetype=ordconstn then
begin
v:=tordconstnode(left).value;
if is_currency(resultdef) and
not(nf_internal in flags) then
v:=v*10000;
if (resultdef.typ=pointerdef) then
result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
else
begin
if is_currency(left.resultdef) then
begin
if not(nf_internal in flags) then
v:=v div 10000;
end
else if (resultdef.typ in [orddef,enumdef]) then
adaptrange(resultdef,v,([nf_internal,nf_absolute]*flags)<>[],nf_explicit in flags,cs_check_range in localswitches);
result:=cordconstnode.create(v,resultdef,false);
end;
end
else if left.nodetype=pointerconstn then
begin
v:=tpointerconstnode(left).value;
if (resultdef.typ=pointerdef) then
result:=cpointerconstnode.create(v.uvalue,resultdef)
else
begin
if is_currency(resultdef) and
not(nf_internal in flags) then
v:=v*10000;
result:=cordconstnode.create(v,resultdef,false);
end;
end
else
begin
if (is_currency(resultdef) or
is_currency(left.resultdef)) and
(nf_internal in flags) then
begin
include(flags,nf_is_currency)
end
{ multiply by 10000 for currency. We need to use getcopy to pass
the argument because the current node is always disposed. Only
inserting the multiply in the left node is not possible because
it'll get in an infinite loop to convert int->currency }
else if is_currency(resultdef) then
begin
result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
include(result.flags,nf_is_currency);
include(taddnode(result).left.flags,nf_internal);
end
else if is_currency(left.resultdef) then
begin
result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
include(result.flags,nf_is_currency);
include(tmoddivnode(result).left.flags,nf_internal);
end;
end;
end;
function ttypeconvnode.typecheck_int_to_real : tnode;
var
rv : bestreal;
begin
result:=nil;
if left.nodetype=ordconstn then
begin
rv:=tordconstnode(left).value;
if is_currency(resultdef) and
not(nf_internal in flags) then
rv:=rv*10000.0
else if is_currency(left.resultdef) and
not(nf_internal in flags) then
rv:=rv/10000.0;
result:=crealconstnode.create(rv,resultdef);
end
else
begin
if (is_currency(resultdef) or
is_currency(left.resultdef)) and
(nf_internal in flags) then
begin
include(flags,nf_is_currency)
end
{ multiply by 10000 for currency. We need to use getcopy to pass
the argument because the current node is always disposed. Only
inserting the multiply in the left node is not possible because
it'll get in an infinite loop to convert int->currency }
else if is_currency(resultdef) then
begin
result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
include(result.flags,nf_is_currency);
end
else if is_currency(left.resultdef) then
begin
result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
include(result.flags,nf_is_currency);
end;
end;
end;
function ttypeconvnode.typecheck_real_to_currency : tnode;
begin
if not is_currency(resultdef) then
internalerror(200304221);
result:=nil;
left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
include(left.flags,nf_is_currency);
{ Convert constants directly, else call Round() }
if left.nodetype=realconstn then
result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
else
begin
result:=cinlinenode.create(in_round_real,false,left);
{ Internal type cast to currency }
result:=ctypeconvnode.create_internal(result,s64currencytype);
left:=nil;
end;
end;
function ttypeconvnode.typecheck_real_to_real : tnode;
begin
result:=nil;
if is_currency(left.resultdef) and not(is_currency(resultdef)) then
begin
left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
include(left.flags,nf_is_currency);
typecheckpass(left);
end
else
if is_currency(resultdef) and not(is_currency(left.resultdef)) then
begin
left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
include(left.flags,nf_is_currency);
include(flags,nf_is_currency);
typecheckpass(left);
end;
end;
function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
begin
result:=nil;
{ handle any constants via cunicodestringtype because the compiler
cannot convert arbitrary unicodechar constants at compile time to
a shortstring (since it doesn't know the code page to use) }
inserttypeconv(left,cunicodestringtype);
{ evaluate again, reset resultdef so the convert_typ
will be calculated again and cstring_to_pchar will
be used for futher conversion }
convtype:=tc_none;
result:=pass_typecheck;
end;
function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
begin
result:=nil;
if is_pwidechar(resultdef) then
inserttypeconv(left,cunicodestringtype)
else
if is_pchar(resultdef) and
(is_widestring(left.resultdef) or
is_unicodestring(left.resultdef)) then
begin
inserttypeconv(left,getansistringdef);
{ the second pass of second_cstring_to_pchar expects a }
{ strinconstn, but this may become a call to the }
{ widestring manager in case left contains "high ascii" }
if (left.nodetype<>stringconstn) then
begin
result:=left;
left:=nil;
end;
end;
end;
function ttypeconvnode.typecheck_cstring_to_int : tnode;
var
fcc : cardinal;
pb : pbyte;
begin
result:=nil;
if left.nodetype<>stringconstn then
internalerror(200510012);
if (m_mac in current_settings.modeswitches) and
is_integer(resultdef) and
(tstringconstnode(left).cst_type=cst_conststring) and
(tstringconstnode(left).len=4) then
begin
pb:=pbyte(tstringconstnode(left).value_str);
fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
result:=cordconstnode.create(fcc,u32inttype,false);
end
else
CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
end;
function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
var
hp : tnode;
begin
result:=nil;
if left.nodetype<>arrayconstructorn then
internalerror(5546);
{ remove typeconv node }
hp:=left;
left:=nil;
{ create a set constructor tree }
arrayconstructor_to_set(hp);
if is_emptyset(hp) then
begin
{ enforce the result type for an empty set }
hp.resultdef:=resultdef;
result:=hp;
end
else if hp.resultdef<>resultdef then
begin
{ the set might contain a subrange element (e.g. through a variable),
thus we need to insert another type conversion }
if nf_explicit in flags then
result:=ctypeconvnode.create_explicit(hp,totypedef)
else if nf_internal in flags then
result:=ctypeconvnode.create_internal(hp,totypedef)
else
result:=ctypeconvnode.create(hp,totypedef);
end
else
result:=hp;
end;
function ttypeconvnode.typecheck_set_to_set : tnode;
begin
result:=nil;
{ constant sets can be converted by changing the type only }
if (left.nodetype=setconstn) then
begin
left.resultdef:=resultdef;
result:=left;
left:=nil;
exit;
end;
end;
function ttypeconvnode.typecheck_pchar_to_string : tnode;
var
newblock : tblocknode;
newstat : tstatementnode;
restemp : ttempcreatenode;
begin
if tstringdef(resultdef).stringtype=st_shortstring then
begin
newblock:=internalstatements(newstat);
restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
addstatement(newstat,restemp);
addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
ctemprefnode.create(restemp),nil))));
addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
addstatement(newstat,ctemprefnode.create(restemp));
result:=newblock;
end
else if tstringdef(resultdef).stringtype=st_ansistring then
result := ccallnode.createinternres(
'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(
cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
ccallparanode.create(left,nil)
),
resultdef
)
else
result := ccallnode.createinternres(
'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(left,nil),resultdef);
left:=nil;
end;
function ttypeconvnode.typecheck_interface_to_string : tnode;
begin
if assigned(tobjectdef(left.resultdef).iidstr) then
begin
if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
tstringconstnode(result).changestringtype(cshortstringtype);
end
else
internalerror(2013112913);
end;
function ttypeconvnode.typecheck_interface_to_guid : tnode;
begin
if assigned(tobjectdef(left.resultdef).iidguid) then
begin
if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
end
else
internalerror(2013112914);
end;
function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
begin
if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then
CGMessage(type_e_no_addr_of_constant);
{ a dynamic array is a pointer to an array, so to convert it to }
{ an open array, we have to dereference it (JM) }
result := ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef));
typecheckpass(result);
{ left is reused }
left := nil;
result := cderefnode.create(result);
include(result.flags,nf_no_checkpointer);
end;
function ttypeconvnode.typecheck_pwchar_to_string : tnode;
var
newblock : tblocknode;
newstat : tstatementnode;
restemp : ttempcreatenode;
begin
if tstringdef(resultdef).stringtype=st_shortstring then
begin
newblock:=internalstatements(newstat);
restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
addstatement(newstat,restemp);
addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
ctemprefnode.create(restemp),nil))));
addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
addstatement(newstat,ctemprefnode.create(restemp));
result:=newblock;
end
else if tstringdef(resultdef).stringtype=st_ansistring then
begin
result:=ccallnode.createinternres(
'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(
cordconstnode.create(
getparaencoding(resultdef),
u16inttype,
true
),
ccallparanode.create(left,nil)
),
resultdef
);
end
else
result := ccallnode.createinternres(
'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
ccallparanode.create(left,nil),resultdef);
left:=nil;
end;
function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
begin
result := ccallnode.createinternres(
'fpc_variant_to_dynarray',
ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
ccallparanode.create(left,nil)
),resultdef);
typecheckpass(result);
left:=nil;
end;
function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
begin
result := ccallnode.createinternres(
'fpc_dynarray_to_variant',
ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
),resultdef);
typecheckpass(result);
left:=nil;
end;
function ttypeconvnode.typecheck_variant_to_interface : tnode;
begin
if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
result := ccallnode.createinternres(
'fpc_variant_to_idispatch',
ccallparanode.create(left,nil)
,resultdef)
else
result := ccallnode.createinternres(
'fpc_variant_to_interface',
ccallparanode.create(left,nil)
,resultdef);
typecheckpass(result);
left:=nil;
end;
function ttypeconvnode.typecheck_interface_to_variant : tnode;
begin
if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
result := ccallnode.createinternres(
'fpc_idispatch_to_variant',
ccallparanode.create(left,nil)
,resultdef)
else
result := ccallnode.createinternres(
'fpc_interface_to_variant',
ccallparanode.create(left,nil)
,resultdef);
typecheckpass(result);
left:=nil;
end;
function ttypeconvnode.typecheck_variant_to_enum : tnode;
begin
result := ctypeconvnode.create_internal(left,sinttype);
result := ctypeconvnode.create_internal(result,resultdef);
typecheckpass(result);
{ left is reused }
left := nil;
end;
function ttypeconvnode.typecheck_enum_to_variant : tnode;
begin
result := ctypeconvnode.create_internal(left,sinttype);
result := ctypeconvnode.create_internal(result,cvarianttype);
typecheckpass(result);
{ left is reused }
left := nil;
end;
function ttypeconvnode.typecheck_array_2_dynarray : tnode;
var
newstatement : tstatementnode;
temp : ttempcreatenode;
temp2 : ttempcreatenode;
begin
{ create statements with call to getmem+initialize }
result:=internalstatements(newstatement);
{ create temp for result }
temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
addstatement(newstatement,temp);
{ get temp for array of lengths }
temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
addstatement(newstatement,temp2);
{ one dimensional }
addstatement(newstatement,cassignmentnode.create(
ctemprefnode.create(temp2),
cordconstnode.create
(tarraydef(left.resultdef).highrange+1,s32inttype,true)));
{ create call to fpc_dynarr_setlength }
addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
ccallparanode.create(caddrnode.create_internal
(ctemprefnode.create(temp2)),
ccallparanode.create(cordconstnode.create
(1,s32inttype,true),
ccallparanode.create(caddrnode.create_internal
(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
ccallparanode.create(
ctypeconvnode.create_internal(
ctemprefnode.create(temp),voidpointertype),
nil))))
));
addstatement(newstatement,ctempdeletenode.create(temp2));
{ copy ... }
addstatement(newstatement,cassignmentnode.create(
ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
left
));
{ left is reused }
left:=nil;
{ the last statement should return the value as
location and type, this is done be referencing the
temp and converting it first from a persistent temp to
normal temp }
addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
addstatement(newstatement,ctemprefnode.create(temp));
end;
function ttypeconvnode.typecheck_elem_2_openarray : tnode;
begin
result:=nil;
end;
function ttypeconvnode.typecheck_arrayconstructor_to_dynarray : tnode;
var
newstatement,assstatement:tstatementnode;
arrnode:ttempcreatenode;
temp2:ttempcreatenode;
assnode:tnode;
paracount:integer;
elemnode:tarrayconstructornode;
begin
{ assignment of []? }
if (left.nodetype=arrayconstructorn) and not assigned(tarrayconstructornode(left).left) then
begin
result:=cnilnode.create;
exit;
end;
if resultdef.typ<>arraydef then
internalerror(2017050102);
tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
result:=internalstatements(newstatement);
{ create temp for result }
arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
addstatement(newstatement,arrnode);
paracount:=0;
{ create an assignment call for each element }
assnode:=internalstatements(assstatement);
if left.nodetype=arrayconstructorrangen then
internalerror(2016021902);
elemnode:=tarrayconstructornode(left);
while assigned(elemnode) do
begin
{ arr[i] := param_i }
if not assigned(elemnode.left) then
internalerror(2017050101);
addstatement(assstatement,
cassignmentnode.create(
cvecnode.create(
ctemprefnode.create(arrnode),
cordconstnode.create(paracount,tarraydef(totypedef).rangedef,false)),
elemnode.left));
elemnode.left:=nil;
inc(paracount);
elemnode:=tarrayconstructornode(elemnode.right);
if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
internalerror(2016021903);
end;
{ get temp for array of lengths }
temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
addstatement(newstatement,temp2);
{ create call to fpc_dynarr_setlength }
addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
ccallparanode.create(caddrnode.create_internal
(ctemprefnode.create(temp2)),
ccallparanode.create(cordconstnode.create
(1,s32inttype,true),
ccallparanode.create(caddrnode.create_internal
(crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
ccallparanode.create(
ctypeconvnode.create_internal(
ctemprefnode.create(arrnode),voidpointertype),
nil))))
));
{ add assignment statememnts }
addstatement(newstatement,ctempdeletenode.create(temp2));
addstatement(newstatement,assnode);
{ the last statement should return the value as
location and type, this is done be referencing the
temp and converting it first from a persistent temp to
normal temp }
addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
addstatement(newstatement,ctemprefnode.create(arrnode));
end;
function ttypeconvnode._typecheck_int_to_int : tnode;
begin
result := typecheck_int_to_int;
end;
function ttypeconvnode._typecheck_cord_to_pointer : tnode;
begin
result := typecheck_cord_to_pointer;
end;
function ttypeconvnode._typecheck_chararray_to_string : tnode;
begin
result := typecheck_chararray_to_string;
end;
function ttypeconvnode._typecheck_string_to_chararray : tnode;
begin
result := typecheck_string_to_chararray;
end;
function ttypeconvnode._typecheck_string_to_string: tnode;
begin
result := typecheck_string_to_string;
end;
function ttypeconvnode._typecheck_char_to_string : tnode;
begin
result := typecheck_char_to_string;
end;
function ttypeconvnode._typecheck_char_to_chararray : tnode;
begin
result := typecheck_char_to_chararray;
end;
function ttypeconvnode._typecheck_int_to_real : tnode;
begin
result := typecheck_int_to_real;
end;
function ttypeconvnode._typecheck_real_to_real : tnode;
begin
result := typecheck_real_to_real;
end;
function ttypeconvnode._typecheck_real_to_currency : tnode;
begin
result := typecheck_real_to_currency;
end;
function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
begin
result := typecheck_cchar_to_pchar;
end;
function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
begin
result := typecheck_cstring_to_pchar;
end;
function ttypeconvnode._typecheck_cstring_to_int : tnode;
begin
result := typecheck_cstring_to_int;
end;
function ttypeconvnode._typecheck_char_to_char : tnode;
begin
result := typecheck_char_to_char;
end;
function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
begin
result := typecheck_arrayconstructor_to_set;
end;
function ttypeconvnode._typecheck_set_to_set : tnode;
begin
result := typecheck_set_to_set;
end;
function ttypeconvnode._typecheck_pchar_to_string : tnode;
begin
result := typecheck_pchar_to_string;
end;
function ttypeconvnode._typecheck_interface_to_string : tnode;
begin
result := typecheck_interface_to_string;
end;
function ttypeconvnode._typecheck_interface_to_guid : tnode;
begin
result := typecheck_interface_to_guid;
end;
function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
begin
result := typecheck_dynarray_to_openarray;
end;
function ttypeconvnode._typecheck_pwchar_to_string : tnode;
begin
result := typecheck_pwchar_to_string;
end;
function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
begin
result := typecheck_variant_to_dynarray;
end;
function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
begin
result := typecheck_dynarray_to_variant;
end;
function ttypeconvnode._typecheck_variant_to_enum : tnode;
begin
result := typecheck_variant_to_enum;
end;
function ttypeconvnode._typecheck_enum_to_variant : tnode;
begin
result := typecheck_enum_to_variant;
end;
function ttypeconvnode._typecheck_proc_to_procvar : tnode;
begin
result := typecheck_proc_to_procvar;
end;
function ttypeconvnode._typecheck_variant_to_interface : tnode;
begin
result := typecheck_variant_to_interface;
end;
function ttypeconvnode._typecheck_interface_to_variant : tnode;
begin
result := typecheck_interface_to_variant;
end;
function ttypeconvnode._typecheck_array_2_dynarray : tnode;
begin
result := typecheck_array_2_dynarray;
end;
function ttypeconvnode._typecheck_elem_2_openarray : tnode;
begin
result := typecheck_elem_2_openarray;
end;
function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
begin
result:=typecheck_arrayconstructor_to_dynarray;
end;
function ttypeconvnode.target_specific_general_typeconv: boolean;
begin
result:=false;
end;
function ttypeconvnode.target_specific_explicit_typeconv: boolean;
begin
result:=false;
end;
class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
begin
result:=false;
end;
function ttypeconvnode.typecheck_proc_to_procvar : tnode;
var
pd : tabstractprocdef;
copytype : tproccopytyp;
source: pnode;
begin
result:=nil;
pd:=tabstractprocdef(left.resultdef);
{ create procvardef (default for create_proc_to_procvar is voiddef,
but if later a regular inserttypeconvnode() is used to insert a type
conversion to the actual procvardef, totypedef will be set to the
real procvartype that we are converting to) }
if assigned(totypedef) and
(totypedef.typ=procvardef) then
begin
{ have to do this in typecheckpass so that it's triggered for
typed constant declarations }
if po_is_block in tprocvardef(totypedef).procoptions then
begin
{ can only convert from procdef to procvardef, but in the mean
time other type conversions may have been inserted (pointers,
proc2procvar, ...) }
source:=actualtargetnode(@left);
while (source^.nodetype=typeconvn) and
(ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
(is_void(source^.resultdef) or
(source^.resultdef.typ=procvardef)) do
begin
{ won't skip proc2procvar }
source:=actualtargetnode(@ttypeconvnode(source^).left);
end;
if (source^.nodetype=loadn) and
(source^.resultdef.typ=procdef) and
not is_nested_pd(tprocdef(source^.resultdef)) and
not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
begin
result:=generate_block_for_procaddr(tloadnode(source^));
exit;
end
else
CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
end;
resultdef:=totypedef;
end
else
begin
{ only need the address of the method? this is needed
for @tobject.create. In this case there will be a loadn without
a methodpointer. }
if (left.nodetype=loadn) and
not assigned(tloadnode(left).left) and
(not(m_nested_procvars in current_settings.modeswitches) or
not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
copytype:=pc_address_only
else
copytype:=pc_normal;
resultdef:=pd.getcopyas(procvardef,copytype,'');
end;
end;
function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
const
resultdefconvert : array[tconverttype] of pointer = (
{none} nil,
{equal} nil,
{not_possible} nil,
{ string_2_string } @ttypeconvnode._typecheck_string_to_string,
{ char_2_string } @ttypeconvnode._typecheck_char_to_string,
{ char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
{ pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
{ cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
{ cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
{ cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
{ ansistring_2_pchar } nil,
{ string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
{ chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
{ array_2_pointer } nil,
{ pointer_2_array } nil,
{ int_2_int } @ttypeconvnode._typecheck_int_to_int,
{ int_2_bool } nil,
{ bool_2_bool } nil,
{ bool_2_int } nil,
{ real_2_real } @ttypeconvnode._typecheck_real_to_real,
{ int_2_real } @ttypeconvnode._typecheck_int_to_real,
{ real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
{ proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
{ nil_2_methodprocvar } nil,
{ arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
{ set_to_set } @ttypeconvnode._typecheck_set_to_set,
{ cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
{ intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
{ intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
{ class_2_intf } nil,
{ char_2_char } @ttypeconvnode._typecheck_char_to_char,
{ dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
{ pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
{ variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
{ dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
{ variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
{ enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
{ variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
{ interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
{ array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
{ elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
{ arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray
);
type
tprocedureofobject = function : tnode of object;
var
r : packed record
proc : pointer;
obj : pointer;
end;
begin
result:=nil;
{ this is a little bit dirty but it works }
{ and should be quite portable too }
r.proc:=resultdefconvert[c];
r.obj:=self;
if assigned(r.proc) then
result:=tprocedureofobject(r)();
end;
function ttypeconvnode.pass_typecheck:tnode;
var
hdef : tdef;
hp : tnode;
currprocdef : tabstractprocdef;
aprocdef : tprocdef;
eq : tequaltype;
cdoptions : tcompare_defs_options;
newblock: tblocknode;
newstatement: tstatementnode;
tempnode: ttempcreatenode;
begin
result:=nil;
resultdef:=totypedef;
typecheckpass(left);
if codegenerror then
exit;
{ When absolute force tc_equal }
if (nf_absolute in flags) then
begin
convtype:=tc_equal;
{ we need to check regability only if something is really regable }
if ((tstoreddef(left.resultdef).is_intregable) or
(tstoreddef(resultdef).is_fpuregable)) and
(
(tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
(tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) or
{ like in pdecvar.read_absolute(): if the size changes, the
register size would also have to change (but second_nothing
does not handle this) }
(tstoreddef(resultdef).size<>tstoreddef(left.resultdef).size)) then
make_not_regable(left,[ra_addr_regable]);
exit;
end;
{ tp procvar support. Skip typecasts to procvar, record or set. Those
convert on the procvar value. This is used to access the
fields of a methodpointer }
if not(nf_load_procvar in flags) and
not(resultdef.typ in [procvardef,recorddef,setdef]) then
maybe_call_procvar(left,true);
if target_specific_general_typeconv then
exit;
if convtype=tc_none then
begin
cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
{ overloaded operators require calls, which is not possible inside
a constant declaration }
if (block_type<>bt_const) and
not(nf_internal in flags) then
include(cdoptions,cdo_check_operator);
if nf_explicit in flags then
include(cdoptions,cdo_explicit);
if nf_internal in flags then
include(cdoptions,cdo_internal);
eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
case eq of
te_exact,
te_equal :
begin
result := simplify(false);
if assigned(result) then
exit;
{ in case of bitpacked accesses, the original type must
remain so that not too many/few bits are laoded }
if is_bitpacked_access(left) then
convtype:=tc_int_2_int;
{ Only leave when there is no conversion to do.
We can still need to call a conversion routine,
like the routine to convert a stringconstnode }
if (convtype in [tc_equal,tc_not_possible]) and
{ some conversions, like dynarray to pointer in Delphi
mode, must not be removed, because then we get memory
leaks due to missing temp finalization }
(not is_managed_type(left.resultdef) or
{ different kinds of refcounted types may need calls
to different kinds of refcounting helpers }
(resultdef=left.resultdef)) then
begin
{$ifdef llvm}
{ we still may have to insert a type conversion at the
llvm level }
if (blocktype<>bt_const) and
(left.resultdef<>resultdef) and
{ if unspecialised generic -> we won't generate any code
for this, and keeping the type conversion node will
cause valid_for_assign to fail because the typecast will be from/to something of 0
bytes to/from something with a non-zero size }
not is_typeparam(left.resultdef) and
not is_typeparam(resultdef) then
result:=nil
else
{$endif llvm}
begin
left.resultdef:=resultdef;
if (nf_explicit in flags) and (left.nodetype = addrn) then
include(taddrnode(left).addrnodeflags,anf_typedaddr);
result:=left;
left:=nil;
end;
exit;
end;
end;
te_convert_l1,
te_convert_l2,
te_convert_l3,
te_convert_l4,
te_convert_l5,
te_convert_l6:
{ nothing to do }
;
te_convert_operator :
begin
include(current_procinfo.flags,pi_do_call);
addsymref(aprocdef.procsym);
hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
{ tell explicitly which def we must use !! (PM) }
tcallnode(hp).procdefinition:=aprocdef;
left:=nil;
result:=hp;
exit;
end;
te_incompatible :
begin
{ convert an array constructor to a set so that we still get
the error "set of Y incompatible to Z" instead of "array of
X incompatible to Z" }
if (resultdef.typ<>arraydef) and
is_array_constructor(left.resultdef) then
begin
arrayconstructor_to_set(left);
typecheckpass(left);
end;
{ Procedures have a resultdef of voiddef and functions of their
own resultdef. They will therefore always be incompatible with
a procvar. Because isconvertable cannot check for procedures we
use an extra check for them.}
if (left.nodetype=calln) and
(tcallnode(left).required_para_count=0) and
(resultdef.typ=procvardef) and
(
(m_tp_procvar in current_settings.modeswitches) or
(m_mac_procvar in current_settings.modeswitches)
) then
begin
if assigned(tcallnode(left).right) then
begin
{ this is already a procvar, if it is really equal
is checked below }
convtype:=tc_equal;
hp:=tcallnode(left).right.getcopy;
currprocdef:=tabstractprocdef(hp.resultdef);
end
else
begin
convtype:=tc_proc_2_procvar;
currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
tprocdef(currprocdef),tcallnode(left).symtableproc);
if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
begin
if assigned(tcallnode(left).methodpointer) then
tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
else
tloadnode(hp).set_mp(load_self_node);
end;
typecheckpass(hp);
end;
left.free;
left:=hp;
{ Now check if the procedure we are going to assign to
the procvar, is compatible with the procvar's type }
if not(nf_explicit in flags) and
(proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
IncompatibleTypes(left.resultdef,resultdef)
else
result:=typecheck_call_helper(convtype);
exit;
end
else if maybe_global_proc_to_nested(left,resultdef) or
maybe_classmethod_to_methodprocvar(left,resultdef) then
begin
result:=left;
left:=nil;
exit;
end;
{ Handle explicit type conversions }
if nf_explicit in flags then
begin
{ do common tc_equal cast, except when dealing with proc -> procvar
(may have to get rid of method pointer) }
if (left.resultdef.typ<>procdef) or
(resultdef.typ<>procvardef) then
convtype:=tc_equal
else
convtype:=tc_proc_2_procvar;
{ ordinal constants can be resized to 1,2,4,8 bytes }
if (left.nodetype=ordconstn) then
begin
{ Insert typeconv for ordinal to the correct size first on left, after
that the other conversion can be done }
hdef:=nil;
case longint(resultdef.size) of
1 :
hdef:=s8inttype;
2 :
hdef:=s16inttype;
4 :
hdef:=s32inttype;
8 :
hdef:=s64inttype;
end;
{ we need explicit, because it can also be an enum }
if assigned(hdef) then
inserttypeconv_internal(left,hdef)
else
CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
end;
{ class/interface to class/interface, with checkobject support }
if is_class_or_interface_or_objc(resultdef) and
is_class_or_interface_or_objc(left.resultdef) then
begin
{ check if the types are related }
if not(nf_internal in flags) and
(not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
(not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
begin
{ Give an error when typecasting class to interface, this is compatible
with delphi }
if is_interface(resultdef) and
not is_interface(left.resultdef) then
CGMessage2(type_e_classes_not_related,
FullTypeName(left.resultdef,resultdef),
FullTypeName(resultdef,left.resultdef))
else
CGMessage2(type_w_classes_not_related,
FullTypeName(left.resultdef,resultdef),
FullTypeName(resultdef,left.resultdef))
end;
{ Add runtime check? }
if not is_objc_class_or_protocol(resultdef) and
not is_objc_class_or_protocol(left.resultdef) and
(cs_check_object in current_settings.localswitches) and
not(nf_internal in flags) then
begin
{ we can translate the typeconvnode to 'as' when
typecasting to a class or interface }
{ we need to make sure the result can still be
passed as a var parameter }
newblock:=internalstatements(newstatement);
if (valid_for_var(left,false)) then
begin
tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
addstatement(newstatement,tempnode);
addstatement(newstatement,cassignmentnode.create(
ctemprefnode.create(tempnode),
caddrnode.create_internal(left)));
left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
end
else
begin
tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
addstatement(newstatement,tempnode);
addstatement(newstatement,cassignmentnode.create(
ctemprefnode.create(tempnode),
left));
left:=ctemprefnode.create(tempnode);
end;
addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
left:=nil;
result:=newblock;
exit;
end;
end
else
begin
{ only if the same size or formal def, and }
{ don't allow type casting of constants to }
{ structured types }
if not(
(left.resultdef.typ=formaldef) or
{$ifdef jvm}
{ enums /are/ class instances on the JVM
platform }
(((left.resultdef.typ=enumdef) and
(resultdef.typ=objectdef)) or
((resultdef.typ=enumdef) and
(left.resultdef.typ=objectdef))) or
{$endif}
(
is_void(left.resultdef) and
(left.nodetype=derefn)
) or
(
not(is_open_array(left.resultdef)) and
not(is_array_constructor(left.resultdef)) and
not(is_array_of_const(left.resultdef)) and
(left.resultdef.size=resultdef.size) and
{ disallow casts of const nodes }
(not is_constnode(left) or
{ however, there are some exceptions }
(not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
filedef,variantdef,objectdef]) or
is_class_or_interface_or_objc(resultdef) or
{ the softfloat code generates casts <const. float> to record }
(nf_internal in flags)
))
)
) then
CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
else
begin
{ perform target-specific explicit typecast
checks }
if target_specific_explicit_typeconv then
begin
result:=simplify(false);
exit;
end;
end;
end;
end
else
IncompatibleTypes(left.resultdef,resultdef);
end;
else
internalerror(200211231);
end;
end;
{ Give hint or warning for unportable code, exceptions are
- typecasts from constants
- void }
if not(nf_internal in flags) and
(left.nodetype<>ordconstn) and
not(is_void(left.resultdef)) and
(((left.resultdef.typ=orddef) and
(resultdef.typ in [pointerdef,procvardef,classrefdef])) or
((resultdef.typ=orddef) and
(left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
begin
{Converting pointers to signed integers is a bad idea. Warn.}
warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
{ Give a warning when sizes don't match, because then info will be lost }
if left.resultdef.size=resultdef.size then
CGMessage(type_h_pointer_to_longint_conv_not_portable)
else
CGMessage(type_w_pointer_to_longint_conv_not_portable);
end;
{ tc_cord_2_pointer still requires a type check, which
simplify does not do }
if (convtype<>tc_cord_2_pointer) then
begin
result := simplify(false);
if assigned(result) then
exit;
end;
{ now call the resultdef helper to do constant folding }
result:=typecheck_call_helper(convtype);
end;
{ some code generators for 64 bit CPUs might not support 32 bit operations, so we can
disable the following optimization in fpcdefs.inc. Currently the only CPU for which
this applies is powerpc64
}
{$ifndef CPUNO32BITOPS}
{ checks whether we can safely remove typeconversions to bigger types
in case range and overflow checking are off, and in case
the result of this node tree is downcasted again to a
smaller type value afterwards,
the smaller types being allowed are described by validints, ordinal constants must fit into l..h
We do this on 64 bit CPUs as well, they benefit from it as well }
function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
var
gotdivmod: boolean;
{ checks whether a node has an accepted resultdef, or originally
had one but was implicitly converted to s64bit }
function wasoriginallysmallerint(n: tnode): boolean;
begin
if (n.resultdef.typ<>orddef) then
exit(false);
if (torddef(n.resultdef).ordtype in validints) then
begin
if is_signed(n.resultdef) then
gotsint:=true;
exit(true);
end;
{ type conv to a bigger int, we do not like to use? }
if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
{ nf_explicit is also set for explicitly typecasted }
{ ordconstn's }
([nf_internal,nf_explicit]*n.flags=[]) and
{ either a typeconversion node coming from a smaller type }
(((n.nodetype=typeconvn) and
(ttypeconvnode(n).left.resultdef.typ=orddef) and
(torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
{ or an ordconstnode which has a smaller type}
((n.nodetype=ordconstn) and
(tordconstnode(n).value>=l) and
(tordconstnode(n).value<=h))) then
begin
if ((n.nodetype=typeconvn) and
is_signed(ttypeconvnode(n).left.resultdef)) or
((n.nodetype=ordconstn) and
(tordconstnode(n).value<0)) then
gotsint:=true;
exit(true);
end;
result:=false;
end;
function docheckremoveinttypeconvs(n: tnode): boolean;
begin
result:=false;
if wasoriginallysmallerint(n) then
exit(true);
case n.nodetype of
subn,orn,xorn:
begin
{ the result could become negative in this case }
if n.nodetype=subn then
gotsint:=true;
result:=
docheckremoveinttypeconvs(tbinarynode(n).left) and
docheckremoveinttypeconvs(tbinarynode(n).right);
end;
addn,muln,divn,modn,andn:
begin
if n.nodetype in [divn,modn] then
gotdivmod:=true;
result:=
(docheckremoveinttypeconvs(tbinarynode(n).left) and
docheckremoveinttypeconvs(tbinarynode(n).right)) or
{ in case of div/mod, the result of that division/modulo can
usually be different in 32 and 64 bit }
(not gotdivmod and
(((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).left)) or
((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).right))));
end;
end;
end;
begin { checkremove64bittypeconvs }
gotdivmod:=false;
gotsint:=false;
result:=
docheckremoveinttypeconvs(n) and
not(gotdivmod and gotsint);
end;
{ remove int type conversions and set the result to the given type }
procedure doremoveinttypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
begin
case n.nodetype of
subn,addn,muln,divn,modn,xorn,andn,orn:
begin
exclude(n.flags,nf_internal);
if not forceunsigned and
is_signed(n.resultdef) then
begin
doremoveinttypeconvs(tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
doremoveinttypeconvs(tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
n.resultdef:=signedtype;
end
else
begin
doremoveinttypeconvs(tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
doremoveinttypeconvs(tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
n.resultdef:=unsignedtype;
end;
//if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
// ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
// ) then
// inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
//else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
// ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
// inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
end;
typeconvn:
begin
ttypeconvnode(n).totypedef:=todef;
{ may change the type conversion, e.g. if the old conversion was
from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
n.resultdef:=nil;
ttypeconvnode(n).convtype:=tc_none;
typecheckpass(n);
end;
else
inserttypeconv_internal(n,todef);
end;
end;
{$endif not CPUNO32BITOPS}
procedure swap_const_value (var val : TConstExprInt; size : longint);
begin
case size of
1 : {do nothing };
2 : if val.signed then
val.svalue:=swapendian(smallint(val.svalue))
else
val.uvalue:=swapendian(word(val.uvalue));
4 : if val.signed then
val.svalue:=swapendian(longint(val.svalue))
else
val.uvalue:=swapendian(qword(val.uvalue));
8 : if val.signed then
val.svalue:=swapendian(int64(val.svalue))
else
val.uvalue:=swapendian(qword(val.uvalue));
else
internalerror(2014111201);
end;
end;
function ttypeconvnode.simplify(forinline : boolean): tnode;
var
hp: tnode;
v: Tconstexprint;
{$ifndef CPUNO32BITOPS}
foundsint: boolean;
{$endif not CPUNO32BITOPS}
begin
result := nil;
{ Constant folding and other node transitions to
remove the typeconv node }
case left.nodetype of
stringconstn :
if (resultdef.typ=stringdef) and
((convtype=tc_equal) or
((convtype=tc_string_2_string) and
(
((not is_widechararray(left.resultdef) and
not is_wide_or_unicode_string(left.resultdef)) or
(tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
)
)
)
) then
begin
{ output string consts in local ansistring encoding }
if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
tstringconstnode(left).changestringtype(getansistringdef)
else
tstringconstnode(left).changestringtype(resultdef);
result:=left;
resultdef:=left.resultdef;
left:=nil;
exit;
end
else if
(convtype<>tc_cstring_2_pchar) and
is_dynamicstring(left.resultdef) and
(tstringconstnode(left).len=0) and
(resultdef.typ=pointerdef) and
cstringconstnode.emptydynstrnil then
begin
result:=cnilnode.create;
exit;
end;
realconstn :
begin
if (convtype = tc_real_2_currency) then
result := typecheck_real_to_currency
else if (convtype = tc_real_2_real) then
result := typecheck_real_to_real
else
exit;
if not(assigned(result)) then
begin
result := left;
left := nil;
end;
if (result.nodetype = realconstn) then
begin
hp:=result;
result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
if nf_is_currency in hp.flags then
include(result.flags,nf_is_currency);
if ([nf_explicit,nf_internal] * flags <> []) then
include(result.flags, nf_explicit);
hp.free;
end;
end;
niln :
begin
{ nil to ordinal node }
if (resultdef.typ=orddef) then
begin
hp:=cordconstnode.create(0,resultdef,true);
if ([nf_explicit,nf_internal] * flags <> []) then
include(hp.flags, nf_explicit);
result:=hp;
exit;
end
else
{ fold nil to any pointer type }
if (resultdef.typ=pointerdef) then
begin
hp:=cnilnode.create;
hp.resultdef:=resultdef;
if ([nf_explicit,nf_internal] * flags <> []) then
include(hp.flags, nf_explicit);
result:=hp;
exit;
end
else
{ remove typeconv after niln, but not when the result is a
methodpointer. The typeconv of the methodpointer will then
take care of updateing size of niln to OS_64 }
if not((resultdef.typ=procvardef) and
not(tprocvardef(resultdef).is_addressonly)) and
{ converting (dynamic array) nil to a an open array is not allowed }
not is_open_array(resultdef) then
begin
left.resultdef:=resultdef;
if ([nf_explicit,nf_internal] * flags <> []) then
include(left.flags, nf_explicit);
result:=left;
left:=nil;
exit;
end;
end;
ordconstn :
begin
{ ordinal contants can be directly converted }
{ but not char to char because it is a widechar to char or via versa }
{ which needs extra code to do the code page transistion }
{ constant ordinal to pointer }
if (resultdef.typ=pointerdef) and
(convtype<>tc_cchar_2_pchar) then
begin
if (target_info.system in systems_managed_vm) and
(tordconstnode(left).value<>0) then
message(parser_e_feature_unsupported_for_vm);
hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
if ([nf_explicit,nf_internal] * flags <> []) then
include(hp.flags, nf_explicit);
result:=hp;
exit;
end
else if is_ordinal(resultdef) and
not(convtype=tc_char_2_char) then
begin
{ replace the resultdef and recheck the range }
if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
include(left.flags, nf_explicit)
else
{ no longer an ordconst with an explicit typecast }
exclude(left.flags, nf_explicit);
{ when converting from one boolean type to another, force }
{ booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
{ (Delphi-compatibile) }
if is_boolean(left.resultdef) and
is_boolean(resultdef) and
(is_cbool(left.resultdef) or
is_cbool(resultdef)) then
begin
if is_pasbool(resultdef) then
tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
else
tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
end
else
begin
{ for constant values on absolute variables, swapping is required }
if (target_info.endian = endian_big) and (nf_absolute in flags) then
swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
adaptrange(resultdef,tordconstnode(left).value,([nf_internal,nf_absolute]*flags)<>[],nf_explicit in flags,cs_check_range in localswitches);
{ swap value back, but according to new type }
if (target_info.endian = endian_big) and (nf_absolute in flags) then
swap_const_value(tordconstnode(left).value,resultdef.size);
{ cut off the new value? }
if resultdef.size<left.resultdef.size then
case resultdef.size of
1:
if is_signed(resultdef) then
tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
else
tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
2:
if is_signed(resultdef) then
tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
else
tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
4:
if is_signed(resultdef) then
tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
else
tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
end;
end;
left.resultdef:=resultdef;
tordconstnode(left).typedef:=resultdef;
if is_signed(resultdef) then
tordconstnode(left).value.signed:=true
else
tordconstnode(left).value.signed:=false;
result:=left;
left:=nil;
exit;
end
else if (convtype=tc_int_2_int) and
is_currency(resultdef) then
begin
v:=tordconstnode(left).value;
if not(nf_internal in flags) and not(is_currency(left.resultdef)) then
v:=v*10000;
result:=cordconstnode.create(v,resultdef,false);
exit;
end;
end;
pointerconstn :
begin
{ pointerconstn to any pointer is folded too }
if (resultdef.typ=pointerdef) then
begin
left.resultdef:=resultdef;
if ([nf_explicit,nf_internal] * flags <> []) then
include(left.flags, nf_explicit)
else
{ no longer an ordconst with an explicit typecast }
exclude(left.flags, nf_explicit);
result:=left;
left:=nil;
exit;
end
{ constant pointer to ordinal }
else if is_ordinal(resultdef) then
begin
hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
resultdef,not(nf_explicit in flags));
if ([nf_explicit,nf_internal] * flags <> []) then
include(hp.flags, nf_explicit);
result:=hp;
exit;
end;
end;
end;
{$ifndef CPUNO32BITOPS}
{ must be done before code below, because we need the
typeconversions for ordconstn's as well }
case convtype of
tc_int_2_int:
begin
if (localswitches * [cs_check_range,cs_check_overflow] = []) and
(resultdef.typ in [pointerdef,orddef,enumdef]) then
begin
{ avoid unnecessary widening of intermediary calculations
to 64 bit }
if (resultdef.size <= 4) and
is_64bitint(left.resultdef) and
(left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
doremoveinttypeconvs(left,generrordef,not foundsint,s32inttype,u32inttype);
{$if defined(cpu16bitalu)}
if (resultdef.size <= 2) and
(is_32bitint(left.resultdef) or is_64bitint(left.resultdef)) and
(left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit],int64(low(smallint)),high(word)) then
doremoveinttypeconvs(left,generrordef,not foundsint,s16inttype,u16inttype);
{$endif defined(cpu16bitalu)}
{$if defined(cpu8bitalu)}
if (resultdef.size<left.resultdef.size) and
is_integer(left.resultdef) and
(left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
doremoveinttypeconvs(left,generrordef,not foundsint,s8inttype,u8inttype);
{$endif defined(cpu8bitalu)}
{ the above simplification may have left a redundant equal
typeconv (e.g. int32 to int32). If that's the case, we remove it }
if equal_defs(left.resultdef,resultdef) then
begin
result:=left;
left:=nil;
exit;
end;
end;
end;
end;
{$endif not CPUNO32BITOPS}
end;
procedure Ttypeconvnode.mark_write;
begin
left.mark_write;
end;
function ttypeconvnode.first_cord_to_pointer : tnode;
begin
result:=nil;
internalerror(200104043);
end;
function ttypeconvnode.first_int_to_int : tnode;
begin
first_int_to_int:=nil;
expectloc:=left.expectloc;
if not is_void(left.resultdef) then
begin
if (left.expectloc<>LOC_REGISTER) and
((resultdef.size>left.resultdef.size) or
(left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
expectloc:=LOC_REGISTER
else
if (left.expectloc=LOC_CREGISTER) and
(resultdef.size<left.resultdef.size) then
expectloc:=LOC_REGISTER;
end;
end;
function ttypeconvnode.first_cstring_to_pchar : tnode;
begin
result:=nil;
expectloc:=LOC_REGISTER;
{ Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
if (cs_create_pic in current_settings.moduleswitches) then
include(current_procinfo.flags,pi_needs_got);
end;
function ttypeconvnode.first_cstring_to_int : tnode;
begin
result:=nil;
internalerror(200510014);
end;
function ttypeconvnode.first_string_to_chararray : tnode;
begin
first_string_to_chararray:=nil;
expectloc:=left.expectloc;
end;
function ttypeconvnode.first_char_to_string : tnode;
begin
first_char_to_string:=nil;
if tstringdef(resultdef).stringtype=st_shortstring then
inc(current_procinfo.estimatedtempsize,256);
expectloc:=LOC_REFERENCE;
end;
function ttypeconvnode.first_char_to_chararray : tnode;
begin
if resultdef.size <> 1 then
begin
{ convert first to string, then to chararray }
inserttypeconv(left,cshortstringtype);
inserttypeconv(left,resultdef);
result:=left;
left := nil;
exit;
end;
result := nil;
end;
function ttypeconvnode.first_nothing : tnode;
begin
first_nothing:=nil;
end;
function ttypeconvnode.first_array_to_pointer : tnode;
begin
first_array_to_pointer:=nil;
expectloc:=LOC_REGISTER;
end;
function ttypeconvnode.first_int_to_real: tnode;
var
fname: string[32];
begin
if target_info.system in systems_wince then
begin
{ converting a 64bit integer to a float requires a helper }
if is_64bitint(left.resultdef) or
is_currency(left.resultdef) then
begin
{ hack to avoid double division by 10000, as it's
already done by typecheckpass.resultdef_int_to_real }
if is_currency(left.resultdef) then
left.resultdef := s64inttype;
if is_signed(left.resultdef) then
fname:='i64to'
else
fname:='ui64to';
end
else
{ other integers are supposed to be 32 bit }
begin
if is_signed(left.resultdef) then
fname:='ito'
else
fname:='uto';
firstpass(left);
end;
if tfloatdef(resultdef).floattype=s64real then
fname:=fname+'d'
else
fname:=fname+'s';
result:=ccallnode.createintern(fname,ccallparanode.create(
left,nil));
left:=nil;
firstpass(result);
exit;
end
else
begin
{ converting a 64bit integer to a float requires a helper }
if is_64bitint(left.resultdef) or
is_currency(left.resultdef) then
begin
{ hack to avoid double division by 10000, as it's
already done by typecheckpass.resultdef_int_to_real }
if is_currency(left.resultdef) then
left.resultdef := s64inttype;
if is_signed(left.resultdef) then
fname:='int64_to_'
else
{ we can't do better currently }
fname:='qword_to_';
end
else
{ other integers are supposed to be 32 bit }
begin
if is_signed(left.resultdef) then
fname:='int32_to_'
else
fname:='int64_to_';
firstpass(left);
end;
if tfloatdef(resultdef).floattype=s64real then
fname:=fname+'float64'
else
fname:=fname+'float32';
result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
left,nil)),resultdef);
left:=nil;
firstpass(result);
exit;
end;
end;
function ttypeconvnode.first_real_to_real : tnode;
begin
{$ifdef cpufpemu}
if cs_fp_emulation in current_settings.moduleswitches then
begin
if target_info.system in systems_wince then
begin
case tfloatdef(left.resultdef).floattype of
s32real:
case tfloatdef(resultdef).floattype of
s64real:
result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
s32real:
begin
result:=left;
left:=nil;
end;
else
internalerror(2005082704);
end;
s64real:
case tfloatdef(resultdef).floattype of
s32real:
result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
s64real:
begin
result:=left;
left:=nil;
end;
else
internalerror(2005082703);
end;
else
internalerror(2005082702);
end;
left:=nil;
firstpass(result);
exit;
end
else
begin
case tfloatdef(left.resultdef).floattype of
s32real:
case tfloatdef(resultdef).floattype of
s64real:
result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
s32real:
begin
result:=left;
left:=nil;
end;
else
internalerror(200610151);
end;
s64real:
case tfloatdef(resultdef).floattype of
s32real:
result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
s64real:
begin
result:=left;
left:=nil;
end;
else
internalerror(200610152);
end;
else
internalerror(200610153);
end;
left:=nil;
firstpass(result);
exit;
end;
end
else
{$endif cpufpemu}
begin
first_real_to_real:=nil;
if not use_vectorfpu(resultdef) then
expectloc:=LOC_FPUREGISTER
else
expectloc:=LOC_MMREGISTER;
end;
end;
function ttypeconvnode.first_pointer_to_array : tnode;
begin
first_pointer_to_array:=nil;
expectloc:=LOC_REFERENCE;
end;
function ttypeconvnode.first_cchar_to_pchar : tnode;
begin
first_cchar_to_pchar:=nil;
internalerror(200104021);
end;
function ttypeconvnode.first_bool_to_int : tnode;
begin
first_bool_to_int:=nil;
{ byte(boolean) or word(wordbool) or longint(longbool) must
be accepted for var parameters }
if (nf_explicit in flags) and
(left.resultdef.size=resultdef.size) and
(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
begin
expectloc:=left.expectloc;
exit;
end;
expectloc:=LOC_REGISTER;
end;
function ttypeconvnode.first_int_to_bool : tnode;
begin
first_int_to_bool:=nil;
{ byte(boolean) or word(wordbool) or longint(longbool) must
be accepted for var parameters }
if (nf_explicit in flags) and
(left.resultdef.size=resultdef.size) and
(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
begin
expectloc:=left.expectloc;
exit;
end;
{ when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
{ convert to a boolean (only necessary for 32bit processors) }
{ note: not if left is already a bool (qwordbool that is true, even if
only because the highest bit is set, must remain true if it is
--implicitly, unlike integers-- converted to another type of bool),
Left can already be a bool because this routine can also be called
from first_bool_to_bool }
if not is_boolean(left.resultdef) and
(left.resultdef.size > sizeof(aint)) and
(left.resultdef.size<>resultdef.size)
and is_cbool(resultdef) then
begin
left:=ctypeconvnode.create_internal(left,s32inttype);
firstpass(left);
exit;
end;
expectloc:=LOC_REGISTER;
end;
function ttypeconvnode.first_bool_to_bool : tnode;
begin
first_bool_to_bool:=nil;
if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
not is_cbool(resultdef) then
expectloc := left.expectloc
{ the following cases use the code generation for bool_to_int/
int_to_bool -> also set their expectlocs }
else if (resultdef.size=left.resultdef.size) and
(is_cbool(resultdef)=is_cbool(left.resultdef)) then
result:=first_bool_to_int
else
result:=first_int_to_bool
end;
function ttypeconvnode.first_char_to_char : tnode;
var
fname: string[18];
begin
if (torddef(resultdef).ordtype=uchar) and
(torddef(left.resultdef).ordtype=uwidechar) then
fname := 'fpc_uchar_to_char'
else if (torddef(resultdef).ordtype=uwidechar) and
(torddef(left.resultdef).ordtype=uchar) then
fname := 'fpc_char_to_uchar'
else
internalerror(2007081201);
result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
left:=nil;
firstpass(result);
end;
function ttypeconvnode.first_proc_to_procvar : tnode;
begin
first_proc_to_procvar:=nil;
{ if we take the address of a nested function, the current function/
procedure needs a stack frame since it's required to construct
the nested procvar }
if is_nested_pd(tprocvardef(resultdef)) then
include(current_procinfo.flags,pi_needs_stackframe);
if tabstractprocdef(resultdef).is_addressonly then
expectloc:=LOC_REGISTER
else
expectloc:=left.expectloc;
end;
function ttypeconvnode.first_nil_to_methodprocvar : tnode;
begin
first_nil_to_methodprocvar:=nil;
expectloc:=LOC_REGISTER;
end;
function ttypeconvnode.first_set_to_set : tnode;
var
newstatement : tstatementnode;
temp : ttempcreatenode;
begin
{ in theory, we should do range checking here,
but Delphi doesn't do it either (FK) }
if left.nodetype=setconstn then
begin
left.resultdef:=resultdef;
result:=left;
left:=nil;
end
{ equal sets for the code generator? }
else if (left.resultdef.size=resultdef.size) and
(tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
{ TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
{ e.g., this prints true because of this:
var
sa: set of 1..2;
sb: set of 5..6;
b: byte;
begin
b:=1;
sa:=[1..2];
sb:=sa;
writeln(b in sb);
end.
}
begin
result:=left;
left:=nil;
end
else
begin
result:=internalstatements(newstatement);
{ in case left is a smallset expression, it can be an addn or so. }
{ fpc_varset_load expects a formal const parameter, which doesn't }
{ accept set addn's -> assign to a temp first and pass the temp }
if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
begin
temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
addstatement(newstatement,temp);
{ temp := left }
addstatement(newstatement,cassignmentnode.create(
ctemprefnode.create(temp),left));
addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
addstatement(newstatement,ctemprefnode.create(temp));
left:=result;
firstpass(left);
{ recreate the result's internalstatements list }
result:=internalstatements(newstatement);
end;
{ create temp for result }
temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
addstatement(newstatement,temp);
addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
ccallparanode.create(ctemprefnode.create(temp),
ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
ccallparanode.create(left,nil))))))
);
addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
addstatement(newstatement,ctemprefnode.create(temp));
left:=nil;
end;
end;
function ttypeconvnode.first_ansistring_to_pchar : tnode;
begin
first_ansistring_to_pchar:=nil;
expectloc:=LOC_REGISTER;
{ Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
if (cs_create_pic in current_settings.moduleswitches) then
include(current_procinfo.flags,pi_needs_got);
end;
function ttypeconvnode.first_arrayconstructor_to_set : tnode;
begin
first_arrayconstructor_to_set:=nil;
internalerror(200104022);
end;
function ttypeconvnode.first_class_to_intf : tnode;
var
hd : tobjectdef;
ImplIntf : TImplementedInterface;
begin
result:=nil;
expectloc:=LOC_REGISTER;
hd:=tobjectdef(left.resultdef);
while assigned(hd) do
begin
ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
if assigned(ImplIntf) then
begin
case ImplIntf.IType of
etStandard:
{ handle in pass 2 }
;
etFieldValue, etFieldValueClass:
if is_interface(tobjectdef(resultdef)) then
begin
result:=left;
propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
{ this ensures proper refcounting when field is of class type }
if not is_interface(result.resultdef) then
inserttypeconv(result, resultdef);
left:=nil;
end
else
begin
internalerror(200802213);
end;
etStaticMethodResult, etStaticMethodClass,
etVirtualMethodResult, etVirtualMethodClass:
if is_interface(tobjectdef(resultdef)) then
begin
{ TODO: generating a call to TObject.GetInterface instead could yield
smaller code size. OTOH, refcounting gotchas are possible that way. }
{ constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
left,[],nil);
addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
{ if it is a class, process it further in a similar way }
if not is_interface(result.resultdef) then
inserttypeconv(result, resultdef);
left:=nil;
end
else if is_class(tobjectdef(resultdef)) then
begin
internalerror(200802211);
end
else
internalerror(200802231);
else
internalerror(200802165);
end;
break;
end;
hd:=hd.childof;
end;
if hd=nil then
internalerror(200802164);
end;
function ttypeconvnode.first_string_to_string : tnode;
var
procname: string[31];
newblock : tblocknode;
newstat : tstatementnode;
restemp : ttempcreatenode;
begin
{ get the correct procedure name }
procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
'_to_'+tstringdef(resultdef).stringtypname;
if tstringdef(resultdef).stringtype=st_shortstring then
begin
newblock:=internalstatements(newstat);
restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
addstatement(newstat,restemp);
addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
ctemprefnode.create(restemp),nil))));
addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
addstatement(newstat,ctemprefnode.create(restemp));
result:=newblock;
end
{ encoding parameter required? }
else if (tstringdef(resultdef).stringtype=st_ansistring) and
(tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
result:=ccallnode.createinternres(procname,
ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
ccallparanode.create(left,nil)),resultdef)
else
result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
left:=nil;
end;
function ttypeconvnode._first_int_to_int : tnode;
begin
result:=first_int_to_int;
end;
function ttypeconvnode._first_cstring_to_pchar : tnode;
begin
result:=first_cstring_to_pchar;
end;
function ttypeconvnode._first_cstring_to_int : tnode;
begin
result:=first_cstring_to_int;
end;
function ttypeconvnode._first_string_to_chararray : tnode;
begin
result:=first_string_to_chararray;
end;
function ttypeconvnode._first_char_to_string : tnode;
begin
result:=first_char_to_string;
end;
function ttypeconvnode._first_char_to_chararray: tnode;
begin
result:=first_char_to_chararray;
end;
function ttypeconvnode._first_nothing : tnode;
begin
result:=first_nothing;
end;
function ttypeconvnode._first_array_to_pointer : tnode;
begin
result:=first_array_to_pointer;
end;
function ttypeconvnode._first_int_to_real : tnode;
begin
result:=first_int_to_real;
end;
function ttypeconvnode._first_real_to_real : tnode;
begin
result:=first_real_to_real;
end;
function ttypeconvnode._first_pointer_to_array : tnode;
begin
result:=first_pointer_to_array;
end;
function ttypeconvnode._first_cchar_to_pchar : tnode;
begin
result:=first_cchar_to_pchar;
end;
function ttypeconvnode._first_bool_to_int : tnode;
begin
result:=first_bool_to_int;
end;
function ttypeconvnode._first_int_to_bool : tnode;
begin
result:=first_int_to_bool;
end;
function ttypeconvnode._first_bool_to_bool : tnode;
begin
result:=first_bool_to_bool;
end;
function ttypeconvnode._first_proc_to_procvar : tnode;
begin
result:=first_proc_to_procvar;
end;
function ttypeconvnode._first_nil_to_methodprocvar : tnode;
begin
result:=first_nil_to_methodprocvar;
end;
function ttypeconvnode._first_set_to_set : tnode;
begin
result:=first_set_to_set;
end;
function ttypeconvnode._first_cord_to_pointer : tnode;
begin
result:=first_cord_to_pointer;
end;
function ttypeconvnode._first_ansistring_to_pchar : tnode;
begin
result:=first_ansistring_to_pchar;
end;
function ttypeconvnode._first_arrayconstructor_to_set : tnode;
begin
result:=first_arrayconstructor_to_set;
end;
function ttypeconvnode._first_class_to_intf : tnode;
begin
result:=first_class_to_intf;
end;
function ttypeconvnode._first_char_to_char : tnode;
begin
result:=first_char_to_char;
end;
function ttypeconvnode._first_string_to_string : tnode;
begin
result:=first_string_to_string;
end;
function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
const
firstconvert : array[tconverttype] of pointer = (
nil, { none }
@ttypeconvnode._first_nothing, {equal}
@ttypeconvnode._first_nothing, {not_possible}
@ttypeconvnode._first_string_to_string,
@ttypeconvnode._first_char_to_string,
@ttypeconvnode._first_char_to_chararray,
nil, { removed in typecheck_chararray_to_string }
@ttypeconvnode._first_cchar_to_pchar,
@ttypeconvnode._first_cstring_to_pchar,
@ttypeconvnode._first_cstring_to_int,
@ttypeconvnode._first_ansistring_to_pchar,
@ttypeconvnode._first_string_to_chararray,
nil, { removed in typecheck_chararray_to_string }
@ttypeconvnode._first_array_to_pointer,
@ttypeconvnode._first_pointer_to_array,
@ttypeconvnode._first_int_to_int,
@ttypeconvnode._first_int_to_bool,
@ttypeconvnode._first_bool_to_bool,
@ttypeconvnode._first_bool_to_int,
@ttypeconvnode._first_real_to_real,
@ttypeconvnode._first_int_to_real,
nil, { removed in typecheck_real_to_currency }
@ttypeconvnode._first_proc_to_procvar,
@ttypeconvnode._first_nil_to_methodprocvar,
@ttypeconvnode._first_arrayconstructor_to_set,
@ttypeconvnode._first_set_to_set,
@ttypeconvnode._first_cord_to_pointer,
@ttypeconvnode._first_nothing,
@ttypeconvnode._first_nothing,
@ttypeconvnode._first_class_to_intf,
@ttypeconvnode._first_char_to_char,
@ttypeconvnode._first_nothing,
@ttypeconvnode._first_nothing,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
@ttypeconvnode._first_nothing,
@ttypeconvnode._first_nothing
);
type
tprocedureofobject = function : tnode of object;
var
r : packed record
proc : pointer;
obj : pointer;
end;
begin
{ this is a little bit dirty but it works }
{ and should be quite portable too }
r.proc:=firstconvert[c];
r.obj:=self;
if not assigned(r.proc) then
internalerror(200312081);
first_call_helper:=tprocedureofobject(r)()
end;
function ttypeconvnode.pass_1 : tnode;
begin
if warn_pointer_to_signed then
cgmessage(type_w_pointer_to_signed);
result:=nil;
firstpass(left);
if codegenerror then
exit;
expectloc:=left.expectloc;
if nf_explicit in flags then
{ check if the result could be in a register }
if (not(tstoreddef(resultdef).is_intregable) and
not(tstoreddef(resultdef).is_const_intregable) and
not(tstoreddef(resultdef).is_fpuregable)) or
((left.resultdef.typ = floatdef) and
(resultdef.typ <> floatdef)) then
make_not_regable(left,[ra_addr_regable]);
result:=first_call_helper(convtype);
end;
function ttypeconvnode.retains_value_location:boolean;
begin
result:=(convtype=tc_equal) or
{ typecasting from void is always allowed }
is_void(left.resultdef) or
(left.resultdef.typ=formaldef) or
{ int 2 int with same size reuses same location, or for
tp7 mode also allow size < orignal size }
(
(convtype=tc_int_2_int) and
(
not is_bitpacked_access(left) and
(resultdef.size=left.resultdef.size) or
((m_tp7 in current_settings.modeswitches) and
(resultdef.size<left.resultdef.size))
)
) or
{ int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
(nf_explicit in flags) and
(resultdef.size=left.resultdef.size)) or
{ on managed platforms, converting an element to an open array
involves creating an actual array -> value location changes }
((convtype=tc_elem_2_openarray) and
not(target_info.system in systems_managed_vm));
end;
function ttypeconvnode.assign_allowed:boolean;
begin
result:=retains_value_location;
{ When using only a part of the value it can't be in a register since
that will load the value in a new register first }
{ the same goes for changing the sign of equal-sized values which
are smaller than an entire register }
if result and
{ don't try to check the size of an open array }
(is_open_array(resultdef) or
(resultdef.size<left.resultdef.size) or
((resultdef.size=left.resultdef.size) and
(left.resultdef.size<sizeof(aint)) and
(is_signed(resultdef) xor is_signed(left.resultdef)))) then
make_not_regable(left,[ra_addr_regable]);
end;
function ttypeconvnode.docompare(p: tnode) : boolean;
begin
docompare :=
inherited docompare(p) and
(convtype = ttypeconvnode(p).convtype) and
(convnodeflags = ttypeconvnode(p).convnodeflags) and
equal_defs(totypedef,ttypeconvnode(p).totypedef);
end;
procedure ttypeconvnode._second_int_to_int;
begin
second_int_to_int;
end;
procedure ttypeconvnode._second_string_to_string;
begin
second_string_to_string;
end;
procedure ttypeconvnode._second_cstring_to_pchar;
begin
second_cstring_to_pchar;
end;
procedure ttypeconvnode._second_cstring_to_int;
begin
second_cstring_to_int;
end;
procedure ttypeconvnode._second_string_to_chararray;
begin
second_string_to_chararray;
end;
procedure ttypeconvnode._second_array_to_pointer;
begin
second_array_to_pointer;
end;
procedure ttypeconvnode._second_pointer_to_array;
begin
second_pointer_to_array;
end;
procedure ttypeconvnode._second_chararray_to_string;
begin
second_chararray_to_string;
end;
procedure ttypeconvnode._second_char_to_string;
begin
second_char_to_string;
end;
procedure ttypeconvnode._second_int_to_real;
begin
second_int_to_real;
end;
procedure ttypeconvnode._second_real_to_real;
begin
second_real_to_real;
end;
procedure ttypeconvnode._second_cord_to_pointer;
begin
second_cord_to_pointer;
end;
procedure ttypeconvnode._second_proc_to_procvar;
begin
second_proc_to_procvar;
end;
procedure ttypeconvnode._second_nil_to_methodprocvar;
begin
second_nil_to_methodprocvar;
end;
procedure ttypeconvnode._second_bool_to_int;
begin
second_bool_to_int;
end;
procedure ttypeconvnode._second_int_to_bool;
begin
second_int_to_bool;
end;
procedure ttypeconvnode._second_bool_to_bool;
begin
second_bool_to_bool;
end;
procedure ttypeconvnode._second_set_to_set;
begin
second_set_to_set;
end;
procedure ttypeconvnode._second_ansistring_to_pchar;
begin
second_ansistring_to_pchar;
end;
procedure ttypeconvnode._second_class_to_intf;
begin
second_class_to_intf;
end;
procedure ttypeconvnode._second_char_to_char;
begin
second_char_to_char;
end;
procedure ttypeconvnode._second_elem_to_openarray;
begin
second_elem_to_openarray;
end;
procedure ttypeconvnode._second_nothing;
begin
second_nothing;
end;
procedure ttypeconvnode.second_call_helper(c : tconverttype);
const
secondconvert : array[tconverttype] of pointer = (
@ttypeconvnode._second_nothing, {none}
@ttypeconvnode._second_nothing, {equal}
@ttypeconvnode._second_nothing, {not_possible}
@ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
@ttypeconvnode._second_char_to_string,
@ttypeconvnode._second_nothing, {char_to_charray}
@ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
@ttypeconvnode._second_nothing, {cchar_to_pchar}
@ttypeconvnode._second_cstring_to_pchar,
@ttypeconvnode._second_cstring_to_int,
@ttypeconvnode._second_ansistring_to_pchar,
@ttypeconvnode._second_string_to_chararray,
@ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
@ttypeconvnode._second_array_to_pointer,
@ttypeconvnode._second_pointer_to_array,
@ttypeconvnode._second_int_to_int,
@ttypeconvnode._second_int_to_bool,
@ttypeconvnode._second_bool_to_bool,
@ttypeconvnode._second_bool_to_int,
@ttypeconvnode._second_real_to_real,
@ttypeconvnode._second_int_to_real,
@ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
@ttypeconvnode._second_proc_to_procvar,
@ttypeconvnode._second_nil_to_methodprocvar,
@ttypeconvnode._second_nothing, { arrayconstructor_to_set }
@ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
@ttypeconvnode._second_cord_to_pointer,
@ttypeconvnode._second_nothing, { interface 2 string }
@ttypeconvnode._second_nothing, { interface 2 guid }
@ttypeconvnode._second_class_to_intf,
@ttypeconvnode._second_char_to_char,
@ttypeconvnode._second_nothing, { dynarray_2_openarray }
@ttypeconvnode._second_nothing, { pwchar_2_string }
@ttypeconvnode._second_nothing, { variant_2_dynarray }
@ttypeconvnode._second_nothing, { dynarray_2_variant}
@ttypeconvnode._second_nothing, { variant_2_enum }
@ttypeconvnode._second_nothing, { enum_2_variant }
@ttypeconvnode._second_nothing, { variant_2_interface }
@ttypeconvnode._second_nothing, { interface_2_variant }
@ttypeconvnode._second_nothing, { array_2_dynarray }
@ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
@ttypeconvnode._second_nothing { arrayconstructor_2_dynarray }
);
type
tprocedureofobject = procedure of object;
var
r : packed record
proc : pointer;
obj : pointer;
end;
begin
{ this is a little bit dirty but it works }
{ and should be quite portable too }
r.proc:=secondconvert[c];
r.obj:=self;
tprocedureofobject(r)();
end;
{*****************************************************************************
TASNODE
*****************************************************************************}
function tasisnode.target_specific_typecheck: boolean;
begin
result:=false;
end;
function tasisnode.pass_typecheck: tnode;
var
hp : tnode;
begin
result:=nil;
typecheckpass(right);
typecheckpass(left);
set_varstate(right,vs_read,[vsf_must_be_valid]);
set_varstate(left,vs_read,[vsf_must_be_valid]);
if codegenerror then
exit;
if target_specific_typecheck then
begin
// ok
end
else if (right.resultdef.typ=classrefdef) then
begin
{ left maybe an interface reference }
if is_interfacecom(left.resultdef) or
is_javainterface(left.resultdef) then
begin
{ relation checks are not possible }
end
{ or left must be a class }
else if is_class(left.resultdef) or
is_javaclass(left.resultdef) then
begin
{ the operands must be related }
if (not(def_is_related(tobjectdef(left.resultdef),
tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
(not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
tobjectdef(left.resultdef)))) then
CGMessage2(type_e_classes_not_related,
FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
end
else
CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
case nodetype of
isn:
resultdef:=pasbool1type;
asn:
resultdef:=tclassrefdef(right.resultdef).pointeddef;
end;
end
else if is_interface(right.resultdef) or
is_dispinterface(right.resultdef) or
is_javainterface(right.resultdef) then
begin
case nodetype of
isn:
resultdef:=pasbool1type;
asn:
resultdef:=right.resultdef;
end;
{ left is a class or interface }
if is_javainterface(right.resultdef) then
begin
if not is_java_class_or_interface(left.resultdef) then
CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
end
else if not(is_class(left.resultdef) or
is_interfacecom(left.resultdef)) then
CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
else
begin
{ load the GUID of the interface }
if (right.nodetype=typen) then
begin
if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
begin
if assigned(tobjectdef(right.resultdef).iidstr) then
begin
hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
tstringconstnode(hp).changestringtype(cshortstringtype);
right.free;
right:=hp;
end
else
internalerror(201006131);
end
else
begin
if assigned(tobjectdef(right.resultdef).iidguid) then
begin
if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
right.free;
right:=hp;
end
else
internalerror(201006132);
end;
typecheckpass(right);
end;
end;
end
else
CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
end;
{*****************************************************************************
TISNODE
*****************************************************************************}
constructor tisnode.create(l,r : tnode);
begin
inherited create(isn,l,r);
end;
constructor tisnode.create_internal(l, r: tnode);
begin
create(l,r);
include(flags,nf_internal);
end;
function tisnode.pass_1 : tnode;
var
procname: string;
statement : tstatementnode;
tempnode : ttempcreatenode;
begin
result:=nil;
{ Passing a class type to an "is" expression cannot result in a class
of that type to be constructed.
}
include(right.flags,nf_ignore_for_wpo);
if is_class(left.resultdef) and
(right.resultdef.typ=classrefdef) then
begin
if (right.nodetype=loadvmtaddrn) and
(tloadvmtaddrnode(right).left.nodetype=typen) and
(oo_is_sealed in tobjectdef(tloadvmtaddrnode(right).left.resultdef).objectoptions) and
equal_defs(left.resultdef,tclassrefdef(right.resultdef).pointeddef) then
begin
if might_have_sideeffects(left) or
(node_complexity(left)>2) then
begin
result:=internalstatements(statement);
tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
addstatement(statement,tempnode);
addstatement(statement,cassignmentnode.create_internal(ctemprefnode.create(tempnode),left));
addstatement(statement,caddnode.create_internal(andn,
caddnode.create_internal(unequaln,ctemprefnode.create(tempnode),cnilnode.create),
caddnode.create_internal(equaln,cloadvmtaddrnode.create(ctemprefnode.create(tempnode)),right)
)
);
left:=nil;
right:=nil;
end
else
begin
result:=caddnode.create_internal(andn,
caddnode.create_internal(unequaln,left.getcopy,cnilnode.create),
caddnode.create_internal(equaln,cloadvmtaddrnode.create(left.getcopy),right)
);
right:=nil;
end;
end
else
result := ccallnode.createinternres('fpc_do_is',
ccallparanode.create(left,ccallparanode.create(right,nil)),
resultdef);
end
else
begin
if is_class(left.resultdef) then
if is_shortstring(right.resultdef) then
procname := 'fpc_class_is_corbaintf'
else
procname := 'fpc_class_is_intf'
else
if right.resultdef.typ=classrefdef then
procname := 'fpc_intf_is_class'
else
procname := 'fpc_intf_is';
result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
end;
left := nil;
right := nil;
//firstpass(call);
if codegenerror then
exit;
end;
{ dummy pass_2, it will never be called, but we need one since }
{ you can't instantiate an abstract class }
procedure tisnode.pass_generate_code;
begin
end;
{*****************************************************************************
TASNODE
*****************************************************************************}
constructor tasnode.create(l,r : tnode);
begin
inherited create(asn,l,r);
call := nil;
end;
constructor tasnode.create_internal(l,r : tnode);
begin
create(l,r);
include(flags,nf_internal);
end;
destructor tasnode.destroy;
begin
call.free;
inherited destroy;
end;
function tasnode.dogetcopy: tnode;
begin
result := inherited dogetcopy;
if assigned(call) then
tasnode(result).call := call.getcopy
else
tasnode(result).call := nil;
end;
function tasnode.docompare(p: tnode): boolean;
begin
result:=
inherited docompare(p) and
tasnode(p).call.isequal(call);
end;
function tasnode.pass_1 : tnode;
var
procname: string;
begin
result:=nil;
{ Passing a class type to an "as" expression cannot result in a class
of that type to be constructed.
We could put this inside the if-block below, but this way it is
safe for sure even if the code below changes
}
if assigned(right) then
include(right.flags,nf_ignore_for_wpo);
if not assigned(call) then
begin
if is_class(left.resultdef) and
(right.resultdef.typ=classrefdef) then
call := ccallnode.createinternres('fpc_do_as',
ccallparanode.create(left,ccallparanode.create(right,nil)),
resultdef)
else
begin
if is_class(left.resultdef) then
if is_shortstring(right.resultdef) then
procname := 'fpc_class_as_corbaintf'
else
procname := 'fpc_class_as_intf'
else
if right.resultdef.typ=classrefdef then
procname := 'fpc_intf_as_class'
else
procname := 'fpc_intf_as';
call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
end;
left := nil;
right := nil;
firstpass(call);
if codegenerror then
exit;
expectloc:=call.expectloc;
end;
end;
end.
|