Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 3846 lines (3155 sloc) 103.563 kb
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
// d-codegen.cc -- D frontend for GCC.
// Copyright (C) 2011-2013 Free Software Foundation, Inc.

// GCC 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 3, or (at your option) any later
// version.

// GCC 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 GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "d-system.h"
#include "d-lang.h"
#include "d-codegen.h"

#include "template.h"
#include "init.h"
#include "id.h"
#include "dfrontend/target.h"


Module *current_module_decl;
IRState *current_irstate;


// Return the DECL_CONTEXT for symbol DSYM.

tree
d_decl_context (Dsymbol *dsym)
{
  Dsymbol *parent = dsym;
  AggregateDeclaration *ad;

  while ((parent = parent->toParent2()))
    {
      if (parent->isFuncDeclaration())
{
FuncDeclaration *fd = dsym->isFuncDeclaration();
if (fd && !needs_static_chain (fd))
return NULL_TREE;

return parent->toSymbol()->Stree;
}
      else if ((ad = parent->isAggregateDeclaration()))
{
tree context = ad->type->toCtype();
// Want the underlying RECORD_TYPE.
if (ad->isClassDeclaration())
context = TREE_TYPE (context);

return context;
}
      else if (parent->isModule())
{
// We've reached the top-level module namespace.
// Set DECL_CONTEXT as the NAMESPACE_DECL of the enclosing
// module, but only for extern(D) symbols.
Declaration *decl = dsym->isDeclaration();
if (decl != NULL && decl->linkage != LINKd)
return NULL_TREE;

return parent->toImport()->Stree;
}
    }

  return NULL_TREE;
}

// Add local variable VD into the current body of function fd.

void
build_local_var (VarDeclaration *vd, FuncDeclaration *fd)
{
  gcc_assert (!vd->isDataseg() && !vd->isMember());

  Symbol *sym = vd->toSymbol();
  tree var = sym->Stree;

  gcc_assert (!TREE_STATIC (var));

  set_input_location (vd->loc);
  d_pushdecl (var);
  DECL_CONTEXT (var) = fd->toSymbol()->Stree;

  // Compiler generated symbols
  if (vd == fd->vresult || vd == fd->v_argptr || vd == fd->v_arguments_var)
    DECL_ARTIFICIAL (var) = 1;

  if (sym->SframeField)
    {
      // Fixes debugging local variables.
      SET_DECL_VALUE_EXPR (var, get_decl_tree (vd, fd));
      DECL_HAS_VALUE_EXPR_P (var) = 1;
    }
}

// Return an unnamed local temporary of type TYPE.

tree
build_local_temp (tree type)
{
  tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);

  DECL_CONTEXT (decl) = current_function_decl;
  DECL_ARTIFICIAL (decl) = 1;
  DECL_IGNORED_P (decl) = 1;
  d_pushdecl (decl);

  return decl;
}

// Return an undeclared local temporary of type TYPE
// for use with BIND_EXPR.

tree
create_temporary_var (tree type)
{
  tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
  DECL_CONTEXT (decl) = current_function_decl;
  DECL_ARTIFICIAL (decl) = 1;
  DECL_IGNORED_P (decl) = 1;
  layout_decl (decl, 0);
  return decl;
}

// Return an undeclared local temporary OUT_VAR initialised
// with result of expression EXP.

tree
maybe_temporary_var (tree exp, tree *out_var)
{
  tree t = exp;

  // Get the base component.
  while (TREE_CODE (t) == COMPONENT_REF)
    t = TREE_OPERAND (t, 0);

  if (!DECL_P (t) && !REFERENCE_CLASS_P (t))
    {
      *out_var = create_temporary_var (TREE_TYPE (exp));
      DECL_INITIAL (*out_var) = exp;
      return *out_var;
    }
  else
    {
      *out_var = NULL_TREE;
      return exp;
    }
}

// Emit an INIT_EXPR for decl DECL.

void
expand_decl (tree decl)
{
  // Nothing, d_pushdecl will add decl to a BIND_EXPR
  if (DECL_INITIAL (decl))
    {
      tree exp = build_vinit (decl, DECL_INITIAL (decl));
      current_irstate->addExp (exp);
      DECL_INITIAL (decl) = NULL_TREE;
    }
}

// Return the correct decl to be used for variable DECL accessed from
// function FUNC. Could be a VAR_DECL, or a FIELD_DECL from a closure.

tree
get_decl_tree (Declaration *decl, FuncDeclaration *func)
{
  VarDeclaration *vd = decl->isVarDeclaration();

  if (vd)
    {
      Symbol *vsym = vd->toSymbol();
      if (vsym->SnamedResult != NULL_TREE)
{
// Get the named return value.
gcc_assert (TREE_CODE (vsym->SnamedResult) == RESULT_DECL);
return vsym->SnamedResult;
}
      else if (vsym->SframeField != NULL_TREE)
{
     // Get the closure holding the var decl.
     FuncDeclaration *parent = vd->toParent2()->isFuncDeclaration();
     tree frame_ref = get_framedecl (func, parent);

     return component_ref (build_deref (frame_ref), vsym->SframeField);
     }
    }

  // Static var or auto var that the back end will handle for us
  return decl->toSymbol()->Stree;
}

// Return expression EXP, whose type has been converted to TYPE.

tree
d_convert (tree type, tree exp)
{
  // Check this first before passing to build_dtype.
  if (error_operand_p (type) || error_operand_p (exp))
    return error_mark_node;

  Type *totype = build_dtype (type);
  Type *etype = build_dtype (TREE_TYPE (exp));

  if (totype && etype)
    return convert_expr (exp, etype, totype);

  return convert (type, exp);
}

// Return expression EXP, whose type has been convert from ETYPE to TOTYPE.

tree
convert_expr (tree exp, Type *etype, Type *totype)
{
  tree result = NULL_TREE;

  gcc_assert (etype && totype);
  Type *ebtype = etype->toBasetype();
  Type *tbtype = totype->toBasetype();

  if (d_types_same (etype, totype))
    return exp;

  if (error_operand_p (exp))
    return exp;

  switch (ebtype->ty)
    {
    case Tdelegate:
      if (tbtype->ty == Tdelegate)
{
exp = maybe_make_temp (exp);
return build_delegate_cst (delegate_method (exp), delegate_object (exp), totype);
}
      else if (tbtype->ty == Tpointer)
{
// The front-end converts <delegate>.ptr to cast (void *)<delegate>.
// Maybe should only allow void* ?
exp = delegate_object (exp);
}
      else
{
error ("can't convert a delegate expression to %s", totype->toChars());
return error_mark_node;
}
      break;

    case Tstruct:
      if (tbtype->ty == Tstruct)
      {
if (totype->size() == etype->size())
{
// Allowed to cast to structs with same type size.
result = build_vconvert (totype->toCtype(), exp);
}
else if (tbtype->ty == Taarray)
{
tbtype = ((TypeAArray *) tbtype)->getImpl()->type;
return convert_expr (exp, etype, tbtype);
}
else
{
error ("can't convert struct %s to %s", etype->toChars(), totype->toChars());
return error_mark_node;
}
      }
      // else, default conversion, which should produce an error
      break;

    case Tclass:
      if (tbtype->ty == Tclass)
      {
ClassDeclaration *cdfrom = tbtype->isClassHandle();
ClassDeclaration *cdto = ebtype->isClassHandle();
int offset;

if (cdfrom->isBaseOf (cdto, &offset) && offset != OFFSET_RUNTIME)
{
// Casting up the inheritance tree: Don't do anything special.
// Cast to an implemented interface: Handle at compile time.
if (offset)
{
tree t = totype->toCtype();
exp = maybe_make_temp (exp);
return build3 (COND_EXPR, t,
build_boolop (NE_EXPR, exp, d_null_pointer),
build_nop (t, build_offset (exp, size_int (offset))),
build_nop (t, d_null_pointer));
}

// d_convert will make a no-op cast
break;
}

// More cases for no-op cast
if (cdfrom == cdto)
break;

if (cdfrom->cpp && cdto->cpp)
break;

// Casting from a C++ interface to a class/non-C++ interface
// always results in null as there is no runtime information,
// and no way one can derive from the other.
if (cdto->isCOMclass() || cdfrom->cpp != cdto->cpp)
{
warning (OPT_Wcast_result, "cast to %s will produce null result", totype->toChars());
result = d_convert (totype->toCtype(), d_null_pointer);
// Make sure the expression is still evaluated if necessary
if (TREE_SIDE_EFFECTS (exp))
result = compound_expr (exp, result);

return result;
}

// The offset can only be determined at runtime, do dynamic cast
tree args[2];
args[0] = exp;
args[1] = build_address (cdfrom->toSymbol()->Stree);

return build_libcall (cdto->isInterfaceDeclaration()
? LIBCALL_INTERFACE_CAST : LIBCALL_DYNAMIC_CAST, 2, args);
      }
      // else default conversion
      break;

    case Tsarray:
      if (tbtype->ty == Tpointer)
{
result = build_nop (totype->toCtype(), build_address (exp));
}
      else if (tbtype->ty == Tarray)
{
dinteger_t dim = ((TypeSArray *) ebtype)->dim->toInteger();
dinteger_t esize = ebtype->nextOf()->size();
dinteger_t tsize = tbtype->nextOf()->size();

tree ptrtype = tbtype->nextOf()->pointerTo()->toCtype();

if ((dim * esize) % tsize != 0)
{
error ("cannot cast %s to %s since sizes don't line up",
etype->toChars(), totype->toChars());
return error_mark_node;
}
dim = (dim * esize) / tsize;

// Assumes casting to dynamic array of same type or void
return d_array_value (totype->toCtype(), size_int (dim),
build_nop (ptrtype, build_address (exp)));
}
      else if (tbtype->ty == Tsarray)
{
// D apparently allows casting a static array to any static array type
return build_vconvert (totype->toCtype(), exp);
}
      else if (tbtype->ty == Tstruct)
{
// And allows casting a static array to any struct type too.
// %% type sizes should have already been checked by the frontend.
gcc_assert (totype->size() == etype->size());
result = build_vconvert (totype->toCtype(), exp);
}
      else
{
error ("cannot cast expression of type %s to type %s",
etype->toChars(), totype->toChars());
return error_mark_node;
}
      break;

    case Tarray:
      if (tbtype->ty == Tpointer)
{
return d_convert (totype->toCtype(), d_array_ptr (exp));
}
      else if (tbtype->ty == Tarray)
{
// assume tvoid->size() == 1
Type *src_elem_type = ebtype->nextOf()->toBasetype();
Type *dst_elem_type = tbtype->nextOf()->toBasetype();
d_uns64 sz_src = src_elem_type->size();
d_uns64 sz_dst = dst_elem_type->size();

if (sz_src == sz_dst)
{
// Convert from void[] or elements are the same size -- don't change length
return build_vconvert (totype->toCtype(), exp);
}
else
{
unsigned mult = 1;
tree args[3];

args[0] = build_integer_cst (sz_dst, Type::tsize_t->toCtype());
args[1] = build_integer_cst (sz_src * mult, Type::tsize_t->toCtype());
args[2] = exp;

return build_libcall (LIBCALL_ARRAYCAST, 3, args, totype->toCtype());
}
}
      else if (tbtype->ty == Tsarray)
{
// %% Strings are treated as dynamic arrays D2.
if (ebtype->isString() && tbtype->isString())
return indirect_ref (totype->toCtype(), d_array_ptr (exp));
}
      else
{
error ("cannot cast expression of type %s to %s",
etype->toChars(), totype->toChars());
return error_mark_node;
}
      break;

    case Taarray:
      if (tbtype->ty == Taarray)
return build_vconvert (totype->toCtype(), exp);
      else if (tbtype->ty == Tstruct)
{
ebtype = ((TypeAArray *) ebtype)->getImpl()->type;
return convert_expr (exp, ebtype, totype);
}
      // Can convert associative arrays to void pointers.
      else if (tbtype == Type::tvoidptr)
return build_vconvert (totype->toCtype(), exp);
      // else, default conversion, which should product an error
      break;

    case Tpointer:
      // Can convert void pointers to associative arrays too...
      if (tbtype->ty == Taarray && ebtype == Type::tvoidptr)
return build_vconvert (totype->toCtype(), exp);
      break;

    case Tnull:
      if (tbtype->ty == Tarray)
{
tree ptrtype = tbtype->nextOf()->pointerTo()->toCtype();
return d_array_value (totype->toCtype(), size_int (0),
build_nop (ptrtype, exp));
}
      break;

    case Tvector:
      if (tbtype->ty == Tsarray)
{
if (tbtype->size() == ebtype->size())
return build_vconvert (totype->toCtype(), exp);
}
      break;

    default:
      exp = fold_convert (etype->toCtype(), exp);
      gcc_assert (TREE_CODE (exp) != STRING_CST);
      break;
    }

  return result ? result :
    convert (totype->toCtype(), exp);
}


// Apply semantics of assignment to a values of type TOTYPE to EXPR
// (e.g., pointer = array -> pointer = &array[0])

// Return a TREE representation of EXPR implictly converted to TOTYPE
// for use in assignment expressions MODIFY_EXPR, INIT_EXPR...

tree
convert_for_assignment (tree expr, Type *etype, Type *totype)
{
  Type *ebtype = etype->toBasetype();
  Type *tbtype = totype->toBasetype();

  // Assuming this only has to handle converting a non Tsarray type to
  // arbitrarily dimensioned Tsarrays.
  if (tbtype->ty == Tsarray)
    {
      Type *telem = tbtype->nextOf()->baseElemOf();

      if (d_types_compatible (telem, ebtype))
{
// %% what about implicit converions...?
TypeSArray *sa_type = (TypeSArray *) tbtype;
uinteger_t count = sa_type->dim->toUInteger();

tree ctor = build_constructor (totype->toCtype(), NULL);
if (count)
{
vec<constructor_elt, va_gc> *ce = NULL;
tree index = build2 (RANGE_EXPR, Type::tsize_t->toCtype(),
integer_zero_node, build_integer_cst (count - 1));
tree value = convert_for_assignment (expr, etype, sa_type->next);

// Can't use VAR_DECLs in CONSTRUCTORS.
if (TREE_CODE (value) == VAR_DECL)
{
value = DECL_INITIAL (value);
gcc_assert (value);
}

CONSTRUCTOR_APPEND_ELT (ce, index, value);
CONSTRUCTOR_ELTS (ctor) = ce;
}
TREE_READONLY (ctor) = 1;
TREE_CONSTANT (ctor) = 1;
return ctor;
}
    }

  if (tbtype->ty == Tstruct && ebtype->isintegral())
    {
      // D Front end uses IntegerExp (0) to mean zero-init a structure.
      // Use memset to fill struct.
      if (integer_zerop (expr))
{
StructDeclaration *sd = ((TypeStruct *) tbtype)->sym;
tree var = build_local_temp (totype->toCtype());

tree init = d_build_call_nary (builtin_decl_explicit (BUILT_IN_MEMSET), 3,
build_address (var), expr,
size_int (sd->structsize));

return compound_expr (init, var);
}
      else
gcc_unreachable();
    }

  return convert_expr (expr, etype, totype);
}

// Return a TREE representation of EXPR converted to represent parameter type ARG.

tree
convert_for_argument (tree exp_tree, Expression *expr, Parameter *arg)
{
  if (arg_reference_p (arg))
    {
      // Front-end already sometimes automatically takes the address
      if (expr->op != TOKaddress && expr->op != TOKsymoff && expr->op != TOKadd)
exp_tree = build_address (exp_tree);

      return convert (type_passed_as (arg), exp_tree);
    }

  // Lazy arguments: expr should already be a delegate
  return exp_tree;
}

// Perform default promotions for data used in expressions.
// Arrays and functions are converted to pointers;
// enumeral types or short or char, to int.
// In addition, manifest constants symbols are replaced by their values.

// Return truth-value conversion of expression EXPR from value type TYPE.

tree
convert_for_condition (tree expr, Type *type)
{
  tree result = NULL_TREE;
  tree obj, func, tmp;

  switch (type->toBasetype()->ty)
    {
    case Taarray:
      // Shouldn't this be...
      // result = _aaLen (&expr);
      result = component_ref (expr, TYPE_FIELDS (TREE_TYPE (expr)));
      break;

    case Tarray:
      // Checks (length || ptr) (i.e ary !is null)
      tmp = maybe_make_temp (expr);
      obj = delegate_object (tmp);
      func = delegate_method (tmp);
      if (TYPE_MODE (TREE_TYPE (obj)) == TYPE_MODE (TREE_TYPE (func)))
{
result = build2 (BIT_IOR_EXPR, TREE_TYPE (obj), obj,
d_convert (TREE_TYPE (obj), func));
}
      else
{
obj = d_truthvalue_conversion (obj);
func = d_truthvalue_conversion (func);
// probably not worth using TRUTH_OROR ...
result = build2 (TRUTH_OR_EXPR, TREE_TYPE (obj), obj, func);
}
      break;

    case Tdelegate:
      // Checks (function || object), but what good is it
      // if there is a null function pointer?
      if (D_METHOD_CALL_EXPR (expr))
extract_from_method_call (expr, obj, func);
      else
{
tmp = maybe_make_temp (expr);
obj = delegate_object (tmp);
func = delegate_method (tmp);
}

      obj = d_truthvalue_conversion (obj);
      func = d_truthvalue_conversion (func);
      // probably not worth using TRUTH_ORIF ...
      result = build2 (BIT_IOR_EXPR, TREE_TYPE (obj), obj, func);
      break;

    default:
      result = expr;
      break;
    }

  return d_truthvalue_conversion (result);
}


// Convert EXP to a dynamic array.
// EXP must be a static array or dynamic array.

tree
d_array_convert (Expression *exp)
{
  TY ty = exp->type->toBasetype()->ty;

  if (ty == Tarray)
    return exp->toElem (current_irstate);
  else if (ty == Tsarray)
    {
      Type *totype = exp->type->toBasetype()->nextOf()->arrayOf();
      return convert_expr (exp->toElem (current_irstate), exp->type, totype);
    }

  // Invalid type passed.
  gcc_unreachable();
}

// Return TRUE if declaration DECL is a reference type.

bool
decl_reference_p (Declaration *decl)
{
  Type *base_type = decl->type->toBasetype();

  if (base_type->ty == Treference)
    return true;

  if (decl->storage_class & (STCout | STCref))
    return true;

  return false;
}

// Returns the real type for declaration DECL.
// Reference decls are converted to reference-to-types.
// Lazy decls are converted into delegates.

tree
declaration_type (Declaration *decl)
{
  tree decl_type = decl->type->toCtype();

  if (decl_reference_p (decl))
    decl_type = build_reference_type (decl_type);
  else if (decl->storage_class & STClazy)
    {
      TypeFunction *tf = new TypeFunction (NULL, decl->type, false, LINKd);
      TypeDelegate *t = new TypeDelegate (tf);
      decl_type = t->merge()->toCtype();
    }
  else if (decl->isThisDeclaration())
    decl_type = insert_type_modifiers (decl_type, MODconst);

  return decl_type;
}

// These should match the Declaration versions above
// Return TRUE if parameter ARG is a reference type.

bool
arg_reference_p (Parameter *arg)
{
  Type *base_type = arg->type->toBasetype();

  if (base_type->ty == Treference)
    return true;

  if (arg->storageClass & (STCout | STCref))
    return true;

  return false;
}

// Returns the real type for parameter ARG.
// Reference parameters are converted to reference-to-types.
// Lazy parameters are converted into delegates.

tree
type_passed_as (Parameter *arg)
{
  tree arg_type = arg->type->toCtype();

  if (arg_reference_p (arg))
    arg_type = build_reference_type (arg_type);
  else if (arg->storageClass & STClazy)
    {
      TypeFunction *tf = new TypeFunction (NULL, arg->type, false, LINKd);
      TypeDelegate *t = new TypeDelegate (tf);
      arg_type = t->merge()->toCtype();
    }

  return arg_type;
}

// Returns an array of type TYPE_NODE which has SIZE number of elements.

tree
d_array_type (Type *d_type, uinteger_t size)
{
  tree index_type_node;
  tree type_node = d_type->toCtype();

  if (size > 0)
    {
      index_type_node = size_int (size - 1);
      index_type_node = build_index_type (index_type_node);
    }
  else
    index_type_node = build_range_type (sizetype, size_zero_node,
NULL_TREE);

  tree array_type = build_array_type (type_node, index_type_node);

  if (size == 0)
    {
      TYPE_SIZE (array_type) = bitsize_zero_node;
      TYPE_SIZE_UNIT (array_type) = size_zero_node;
    }

  return array_type;
}

// Appends the type attribute ATTRNAME with value VALUE onto type TYPE.

tree
insert_type_attribute (tree type, const char *attrname, tree value)
{
  tree attrib;
  tree ident = get_identifier (attrname);

  if (value)
    value = tree_cons (NULL_TREE, value, NULL_TREE);

  // types built by functions in tree.c need to be treated as immutabl
  if (!TYPE_ATTRIBUTES (type))
    type = build_variant_type_copy (type);

  attrib = tree_cons (ident, value, NULL_TREE);
  TYPE_ATTRIBUTES (type) = merge_attributes (TYPE_ATTRIBUTES (type), attrib);

  return type;
}

// Appends the decl attribute ATTRNAME with value VALUE onto decl DECL.

void
insert_decl_attribute (tree decl, const char *attrname, tree value)
{
  tree attrib;
  tree ident = get_identifier (attrname);

  if (value)
    value = tree_cons (NULL_TREE, value, NULL_TREE);

  attrib = tree_cons (ident, value, NULL_TREE);
  DECL_ATTRIBUTES (decl) = merge_attributes (DECL_ATTRIBUTES (decl), attrib);
}

bool
d_attribute_p (const char* name)
{
  static StringTable* table;

  if(table == NULL)
    {
      // Build the table of attributes exposed to the language.
      // Common and format attributes are kept internal.
      size_t n = 0;
      table = new StringTable();

      for (const attribute_spec *p = lang_hooks.attribute_table; p->name; p++)
n++;

      for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
n++;

      if(n != 0)
{
table->_init(n);

for (const attribute_spec *p = lang_hooks.attribute_table; p->name; p++)
table->insert(p->name, strlen(p->name));

for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
table->insert(p->name, strlen(p->name));
}
    }

  return table->lookup(name, strlen(name)) != NULL;
}

// Return chain of all GCC attributes found in list IN_ATTRS.

tree
build_attributes (Expressions *in_attrs)
{
  if (!in_attrs)
    return NULL_TREE;

  expandTuples(in_attrs);

  tree out_attrs = NULL_TREE;

  for (size_t i = 0; i < in_attrs->dim; i++)
    {
      Expression *attr = (*in_attrs)[i]->optimize (WANTexpand);
      Dsymbol *sym = attr->type->toDsymbol (0);

      if (!sym)
continue;

      Dsymbol *mod = (Dsymbol*) sym->getModule();
      if (!(strcmp(mod->toChars(), "attribute") == 0
          && mod->parent != NULL
          && strcmp(mod->parent->toChars(), "gcc") == 0
          && !mod->parent->parent))
        continue;

      if (attr->op == TOKcall)
attr = attr->ctfeInterpret();

      gcc_assert(attr->op == TOKstructliteral);
      Expressions *elem = ((StructLiteralExp*) attr)->elements;

      if ((*elem)[0]->op == TOKnull)
{
error ("expected string attribute, not null");
return error_mark_node;
}

      gcc_assert((*elem)[0]->op == TOKstring);
      StringExp *nameExp = (StringExp*) (*elem)[0];
      gcc_assert(nameExp->sz == 1);
      const char* name = (const char*) nameExp->string;

      if (!d_attribute_p (name))
      {
        error ("unknown attribute %s", name);
        return error_mark_node;
      }

      tree args = NULL_TREE;

      for (size_t j = 1; j < elem->dim; j++)
        {
Expression *ae = (*elem)[j];
tree aet;
if (ae->op == TOKstring && ((StringExp *) ae)->sz == 1)
{
StringExp *s = (StringExp *) ae;
aet = build_string (s->len, (const char *) s->string);
}
else
aet = ae->toElem (current_irstate);

args = chainon (args, build_tree_list (0, aet));
        }

      tree list = build_tree_list (get_identifier (name), args);
      out_attrs = chainon (out_attrs, list);
    }

  return out_attrs;
}

// Return qualified type variant of TYPE determined by modifier value MOD.

tree
insert_type_modifiers (tree type, unsigned mod)
{
  int quals = 0;
  gcc_assert (type);

  switch (mod)
    {
    case 0:
      break;

    case MODconst:
    case MODwild:
    case MODimmutable:
      quals |= TYPE_QUAL_CONST;
      break;

    case MODshared:
      quals |= TYPE_QUAL_VOLATILE;
      break;

    case MODshared | MODwild:
    case MODshared | MODconst:
      quals |= TYPE_QUAL_CONST;
      quals |= TYPE_QUAL_VOLATILE;
      break;

    default:
      gcc_unreachable();
    }

  return build_qualified_type (type, quals);
}

// Build INTEGER_CST of type TYPE with the value VALUE.

tree
build_integer_cst (dinteger_t value, tree type)
{
  // The type is error_mark_node, we can't do anything.
  if (error_operand_p (type))
    return type;

  return build_int_cst_type (type, value);
}

// Build REAL_CST of type TOTYPE with the value VALUE.

tree
build_float_cst (const real_t& value, Type *totype)
{
  real_t new_value;
  TypeBasic *tb = totype->isTypeBasic();

  gcc_assert (tb != NULL);

  tree type_node = tb->toCtype();
  real_convert (&new_value.rv(), TYPE_MODE (type_node), &value.rv());

  // Value grew as a result of the conversion. %% precision bug ??
  // For now just revert back to original.
  if (new_value > value)
    new_value = value;

  return build_real (type_node, new_value.rv());
}

// Convert LOW / HIGH pair into dinteger_t type.

dinteger_t
cst_to_hwi (double_int cst)
{
  if (cst.high == 0 || (cst.high == -1 && (HOST_WIDE_INT) cst.low < 0))
    return cst.low;
  else if (cst.low == 0 && cst.high == 1)
    return (~(dinteger_t) 0);

  gcc_unreachable();
}

// Return host integer value for INT_CST T.

dinteger_t
tree_to_hwi (tree t)
{
  if (TREE_INT_CST_HIGH (t) == 0
      || (TREE_INT_CST_HIGH (t) == -1
&& (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
&& !TYPE_UNSIGNED (TREE_TYPE (t))))
    return TREE_INT_CST_LOW (t);

  return cst_to_hwi (TREE_INT_CST (t));
}

// Returns the .length component from the D dynamic array EXP.

tree
d_array_length (tree exp)
{
  // backend will ICE otherwise
  if (error_operand_p (exp))
    return exp;

  // Get the backend type for the array and pick out the array
  // length field (assumed to be the first field.)
  tree len_field = TYPE_FIELDS (TREE_TYPE (exp));
  return component_ref (exp, len_field);
}

// Returns the .ptr component from the D dynamic array EXP.

tree
d_array_ptr (tree exp)
{
  // backend will ICE otherwise
  if (error_operand_p (exp))
    return exp;

  // Get the backend type for the array and pick out the array
  // data pointer field (assumed to be the second field.)
  tree ptr_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp)));
  return component_ref (exp, ptr_field);
}

// Returns a constructor for D dynamic array type TYPE of .length LEN
// and .ptr pointing to DATA.

tree
d_array_value (tree type, tree len, tree data)
{
  // %% assert type is a darray
  tree len_field, ptr_field;
  vec<constructor_elt, va_gc> *ce = NULL;

  len_field = TYPE_FIELDS (type);
  ptr_field = TREE_CHAIN (len_field);

  len = convert (TREE_TYPE (len_field), len);
  data = convert (TREE_TYPE (ptr_field), data);

  CONSTRUCTOR_APPEND_ELT (ce, len_field, len);
  CONSTRUCTOR_APPEND_ELT (ce, ptr_field, data);

  tree ctor = build_constructor (type, ce);
  // TREE_STATIC and TREE_CONSTANT can be set by caller if needed
  TREE_STATIC (ctor) = 0;
  TREE_CONSTANT (ctor) = 0;

  return ctor;
}

// Builds a D string value from the C string STR.

tree
d_array_string (const char *str)
{
  unsigned len = strlen (str);
  // Assumes STR is 0-terminated.
  tree str_tree = build_string (len + 1, str);

  TREE_TYPE (str_tree) = d_array_type (Type::tchar, len);

  return d_array_value (Type::tchar->arrayOf()->toCtype(),
size_int (len), build_address (str_tree));
}

// Returns value representing the array length of expression EXP.
// TYPE could be a dynamic or static array.

tree
get_array_length (tree exp, Type *type)
{
  Type *tb = type->toBasetype();

  switch (tb->ty)
    {
    case Tsarray:
      return size_int (((TypeSArray *) tb)->dim->toUInteger());

    case Tarray:
      return d_array_length (exp);

    default:
      error ("can't determine the length of a %s", type->toChars());
      return error_mark_node;
    }
}

// Return TRUE if binary expression EXP is an unhandled array operation,
// in which case we error that it is not implemented.

bool
unhandled_arrayop_p (BinExp *exp)
{
  TY ty1 = exp->e1->type->toBasetype()->ty;
  TY ty2 = exp->e2->type->toBasetype()->ty;

  if ((ty1 == Tarray || ty1 == Tsarray
       || ty2 == Tarray || ty2 == Tsarray))
    {
      exp->error ("Array operation %s not implemented", exp->toChars());
      return true;
    }
  return false;
}

// Create BINFO for a ClassDeclaration's inheritance tree.
// Interfaces are not included.

tree
build_class_binfo (tree super, ClassDeclaration *cd)
{
  tree binfo = make_tree_binfo (1);
  tree ctype = cd->type->toCtype();

  // Want RECORD_TYPE, not REFERENCE_TYPE
  BINFO_TYPE (binfo) = TREE_TYPE (ctype);
  BINFO_INHERITANCE_CHAIN (binfo) = super;
  BINFO_OFFSET (binfo) = integer_zero_node;

  if (cd->baseClass)
    BINFO_BASE_APPEND (binfo, build_class_binfo (binfo, cd->baseClass));

  return binfo;
}

// Create BINFO for an InterfaceDeclaration's inheritance tree.
// In order to access all inherited methods in the debugger,
// the entire tree must be described.
// This function makes assumptions about interface layout.

tree
build_interface_binfo (tree super, ClassDeclaration *cd, unsigned& offset)
{
  tree binfo = make_tree_binfo (cd->baseclasses->dim);
  tree ctype = cd->type->toCtype();

  // Want RECORD_TYPE, not REFERENCE_TYPE
  BINFO_TYPE (binfo) = TREE_TYPE (ctype);
  BINFO_INHERITANCE_CHAIN (binfo) = super;
  BINFO_OFFSET (binfo) = size_int (offset * Target::ptrsize);
  BINFO_VIRTUAL_P (binfo) = 1;

  for (size_t i = 0; i < cd->baseclasses->dim; i++, offset++)
    {
      BaseClass *bc = (*cd->baseclasses)[i];
      BINFO_BASE_APPEND (binfo, build_interface_binfo (binfo, bc->base, offset));
    }

  return binfo;
}

// Returns the .funcptr component from the D delegate EXP.

tree
delegate_method (tree exp)
{
  // Get the backend type for the array and pick out the array length
  // field (assumed to be the second field.)
  tree method_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp)));
  return component_ref (exp, method_field);
}

// Returns the .object component from the delegate EXP.

tree
delegate_object (tree exp)
{
  // Get the backend type for the array and pick out the array data
  // pointer field (assumed to be the first field.)
  tree obj_field = TYPE_FIELDS (TREE_TYPE (exp));
  return component_ref (exp, obj_field);
}

// Build a delegate literal of type TYPE whose pointer function is
// METHOD, and hidden object is OBJECT.

tree
build_delegate_cst (tree method, tree object, Type *type)
{
  Type *base_type = type->toBasetype();

  // Called from DotVarExp. These are just used to make function calls
  // and not to make Tdelegate variables. Clearing the type makes sure of this.
  if (base_type->ty == Tfunction)
    base_type = NULL;
  else
    gcc_assert (base_type->ty == Tdelegate);

  tree ctype = base_type ? base_type->toCtype() : NULL_TREE;
  tree ctor = make_node (CONSTRUCTOR);
  tree obj_field = NULL_TREE;
  tree func_field = NULL_TREE;
  vec<constructor_elt, va_gc> *ce = NULL;

  if (ctype)
    {
      TREE_TYPE (ctor) = ctype;
      obj_field = TYPE_FIELDS (ctype);
      func_field = TREE_CHAIN (obj_field);
    }
  CONSTRUCTOR_APPEND_ELT (ce, obj_field, object);
  CONSTRUCTOR_APPEND_ELT (ce, func_field, method);

  CONSTRUCTOR_ELTS (ctor) = ce;
  return ctor;
}

// Builds a temporary tree to store the CALLEE and OBJECT
// of a method call expression of type TYPE.

tree
build_method_call (tree callee, tree object, Type *type)
{
  tree t = build_delegate_cst (callee, object, type);
  D_METHOD_CALL_EXPR (t) = 1;
  return t;
}

// Extract callee and object from T and return in to CALLEE and OBJECT.

void
extract_from_method_call (tree t, tree& callee, tree& object)
{
  gcc_assert (D_METHOD_CALL_EXPR (t));
  object = CONSTRUCTOR_ELT (t, 0)->value;
  callee = CONSTRUCTOR_ELT (t, 1)->value;
}

// Return correct callee for method FUNC, which is dereferenced from
// the 'this' pointer OBJEXP. TYPE is the return type for the method.
// THISEXP is the tree representation of OBJEXP.

tree
get_object_method (tree thisexp, Expression *objexp, FuncDeclaration *func, Type *type)
{
  Type *objtype = objexp->type->toBasetype();
  bool is_dottype = false;

  gcc_assert (func->isThis());

  Expression *ex = objexp;

  while (1)
    {
      if (ex->op == TOKsuper || ex->op == TOKdottype)
{
// super.member() and type.member() calls directly.
is_dottype = true;
break;
}
      else if (ex->op == TOKcast)
{
ex = ((CastExp *) ex)->e1;
continue;
}
      break;
    }

  // Calls to super are static (func is the super's method)
  // Structs don't have vtables.
  // Final and non-virtual methods can be called directly.
  // DotTypeExp means non-virtual

  if (objexp->op == TOKsuper
      || objtype->ty == Tstruct || objtype->ty == Tpointer
      || func->isFinalFunc() || !func->isVirtual() || is_dottype)
    {
      if (objtype->ty == Tstruct)
thisexp = build_address (thisexp);

      return build_method_call (build_address (func->toSymbol()->Stree),
thisexp, type);
    }
  else
    {
      // Interface methods are also in the class's vtable, so we don't
      // need to convert from a class pointer to an interface pointer.
      thisexp = maybe_make_temp (thisexp);
      tree vtbl_ref = build_deref (thisexp);
      // The vtable is the first field.
      tree field = TYPE_FIELDS (TREE_TYPE (vtbl_ref));
      tree fntype = TREE_TYPE (func->toSymbol()->Stree);

      vtbl_ref = component_ref (vtbl_ref, field);
      vtbl_ref = build_offset (vtbl_ref, size_int (Target::ptrsize * func->vtblIndex));
      vtbl_ref = indirect_ref (build_pointer_type (fntype), vtbl_ref);

      return build_method_call (vtbl_ref, thisexp, type);
    }
}


// Builds a record type from field types T1 and T2. TYPE is the D frontend
// type we are building. N1 and N2 are the names of the two fields.

tree
build_two_field_type (tree t1, tree t2, Type *type, const char *n1, const char *n2)
{
  tree rectype = make_node (RECORD_TYPE);
  tree f0 = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier (n1), t1);
  tree f1 = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier (n2), t2);

  DECL_CONTEXT (f0) = rectype;
  DECL_CONTEXT (f1) = rectype;
  TYPE_FIELDS (rectype) = chainon (f0, f1);
  layout_type (rectype);

  if (type)
    {
      tree ident = get_identifier (type->toChars());
      tree stubdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, ident, rectype);

      TYPE_STUB_DECL (rectype) = stubdecl;
      TYPE_NAME (rectype) = stubdecl;
      DECL_ARTIFICIAL (stubdecl) = 1;
      rest_of_decl_compilation (stubdecl, 1, 0);
    }

  return rectype;
}

// Create a SAVE_EXPR if EXP might have unwanted side effects if referenced
// more than once in an expression.

tree
make_temp (tree exp)
{
  if (TREE_CODE (exp) == CALL_EXPR
      || TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE)
    return save_expr (exp);
  else
    return stabilize_reference (exp);
}

tree
maybe_make_temp (tree exp)
{
  if (d_has_side_effects (exp))
    return make_temp (exp);

  return exp;
}

// Return TRUE if EXP can not be evaluated multiple times (i.e., in a loop body)
// without unwanted side effects.

bool
d_has_side_effects (tree exp)
{
  tree t = STRIP_NOPS (exp);

  // SAVE_EXPR is safe to reference more than once, but not to
  // expand in a loop.
  if (TREE_CODE (t) == SAVE_EXPR)
    return false;

  if (DECL_P (t)
      || CONSTANT_CLASS_P (t))
    return false;

  if (INDIRECT_REF_P (t)
      || TREE_CODE (t) == ADDR_EXPR
      || TREE_CODE (t) == COMPONENT_REF)
    return d_has_side_effects (TREE_OPERAND (t, 0));

  return TREE_SIDE_EFFECTS (t);
}


// Returns the address of the expression EXP.

tree
build_address (tree exp)
{
  tree t, ptrtype;
  tree type = TREE_TYPE (exp);
  d_mark_addressable (exp);

  // Gimplify doesn't like &(* (ptr-to-array-type)) with static arrays
  if (TREE_CODE (exp) == INDIRECT_REF)
    {
      t = TREE_OPERAND (exp, 0);
      ptrtype = build_pointer_type (type);
      t = build_nop (ptrtype, t);
    }
  else
    {
      /* Just convert string literals (char[]) to C-style strings (char *), otherwise
the latter method (char[]*) causes conversion problems during gimplification. */
      if (TREE_CODE (exp) == STRING_CST)
ptrtype = build_pointer_type (TREE_TYPE (type));
      /* Special case for va_list. The backends will be expecting a pointer to vatype,
* but some targets use an array. So fix it. */
      else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (va_list_type_node))
{
if (TREE_CODE (TYPE_MAIN_VARIANT (type)) == ARRAY_TYPE)
ptrtype = build_pointer_type (TREE_TYPE (type));
else
ptrtype = build_pointer_type (type);
}
      else
ptrtype = build_pointer_type (type);

      t = build1 (ADDR_EXPR, ptrtype, exp);
    }

  if (TREE_CODE (exp) == FUNCTION_DECL)
    TREE_NO_TRAMPOLINE (t) = 1;

  return t;
}

tree
d_mark_addressable (tree exp)
{
  switch (TREE_CODE (exp))
    {
    case ADDR_EXPR:
    case COMPONENT_REF:
      /* If D had bit fields, we would need to handle that here */
    case ARRAY_REF:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
      d_mark_addressable (TREE_OPERAND (exp, 0));
      break;

      /* %% C++ prevents {& this} .... */
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case COMPOUND_EXPR:
      d_mark_addressable (TREE_OPERAND (exp, 1));
      break;

    case COND_EXPR:
      d_mark_addressable (TREE_OPERAND (exp, 1));
      d_mark_addressable (TREE_OPERAND (exp, 2));
      break;

    case CONSTRUCTOR:
      TREE_ADDRESSABLE (exp) = 1;
      break;

    case INDIRECT_REF:
      /* %% this was in Java, not sure for D */
      /* We sometimes add a cast *(TYPE *)&FOO to handle type and mode
incompatibility problems. Handle this case by marking FOO. */
      if (TREE_CODE (TREE_OPERAND (exp, 0)) == NOP_EXPR
&& TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) == ADDR_EXPR)
{
d_mark_addressable (TREE_OPERAND (TREE_OPERAND (exp, 0), 0));
break;
}
      if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
{
d_mark_addressable (TREE_OPERAND (exp, 0));
break;
}
      break;

    case VAR_DECL:
    case CONST_DECL:
    case PARM_DECL:
    case RESULT_DECL:
    case FUNCTION_DECL:
      TREE_USED (exp) = 1;
      TREE_ADDRESSABLE (exp) = 1;

      /* drops through */
    default:
      break;
    }

  return exp;
}

/* Mark EXP as "used" in the program for the benefit of
-Wunused warning purposes. */

tree
d_mark_used (tree exp)
{
  switch (TREE_CODE (exp))
    {
    case VAR_DECL:
    case PARM_DECL:
      TREE_USED (exp) = 1;
      break;

    case ARRAY_REF:
    case COMPONENT_REF:
    case MODIFY_EXPR:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case NOP_EXPR:
    case CONVERT_EXPR:
    case ADDR_EXPR:
      d_mark_used (TREE_OPERAND (exp, 0));
      break;

    case COMPOUND_EXPR:
      d_mark_used (TREE_OPERAND (exp, 0));
      d_mark_used (TREE_OPERAND (exp, 1));
      break;

    default:
      break;
    }
  return exp;
}

/* Mark EXP as read, not just set, for set but not used -Wunused
warning purposes. */

tree
d_mark_read (tree exp)
{
  switch (TREE_CODE (exp))
    {
    case VAR_DECL:
    case PARM_DECL:
      TREE_USED (exp) = 1;
      DECL_READ_P (exp) = 1;
      break;

    case ARRAY_REF:
    case COMPONENT_REF:
    case MODIFY_EXPR:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case NOP_EXPR:
    case CONVERT_EXPR:
    case ADDR_EXPR:
      d_mark_read (TREE_OPERAND (exp, 0));
      break;

    case COMPOUND_EXPR:
      d_mark_read (TREE_OPERAND (exp, 1));
      break;

    default:
      break;
    }
  return exp;
}

// Build equality expression between two RECORD_TYPES T1 and T2.
// CODE is the EQ_EXPR or NE_EXPR comparison.
// SD is the front-end struct type.

tree
build_struct_memcmp (tree_code code, StructDeclaration *sd, tree t1, tree t2)
{
  tree_code tcode = (code == EQ_EXPR) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
  tree tmemcmp = NULL_TREE;

  // Let backend take care of empty struct or union comparisons.
  if (!sd->fields.dim || sd->isUnionDeclaration())
    {
      tmemcmp = d_build_call_nary (builtin_decl_explicit (BUILT_IN_MEMCMP), 3,
build_address (t1), build_address (t2),
size_int (sd->structsize));

      return build_boolop (code, tmemcmp, integer_zero_node);
    }

  for (size_t i = 0; i < sd->fields.dim; i++)
    {
      VarDeclaration *vd = sd->fields[i];
      tree sfield = vd->toSymbol()->Stree;

      tree t1ref = component_ref (t1, sfield);
      tree t2ref = component_ref (t2, sfield);
      tree tcmp;

      if (vd->type->ty == Tstruct)
{
// Compare inner data structures.
StructDeclaration *decl = ((TypeStruct *) vd->type)->sym;
tcmp = build_struct_memcmp (code, decl, t1ref, t2ref);
}
      else
{
tree stype = vd->type->toCtype();
machine_mode mode = int_mode_for_mode (TYPE_MODE (stype));

if (vd->type->isintegral())
{
// Integer comparison, no special handling required.
tcmp = build_boolop (code, t1ref, t2ref);
}
else if (mode != BLKmode)
{
// Compare field bits as their corresponding integer type.
// *((T*) &t1) == *((T*) &t2)
tree tmode = lang_hooks.types.type_for_mode (mode, 1);

t1ref = build_vconvert (tmode, t1ref);
t2ref = build_vconvert (tmode, t2ref);

tcmp = build_boolop (code, t1ref, t2ref);
}
else
{
// Simple memcmp between types.
tcmp = d_build_call_nary (builtin_decl_explicit (BUILT_IN_MEMCMP), 3,
build_address (t1ref), build_address (t2ref),
TYPE_SIZE_UNIT (stype));

tcmp = build_boolop (code, tcmp, integer_zero_node);
}
}

      tmemcmp = (tmemcmp) ? build_boolop (tcode, tmemcmp, tcmp) : tcmp;
    }

  return tmemcmp;
}

// Cast EXP (which should be a pointer) to TYPE * and then indirect. The
// back-end requires this cast in many cases.

tree
indirect_ref (tree type, tree exp)
{
  if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE)
    return build1 (INDIRECT_REF, type, exp);

  return build1 (INDIRECT_REF, type,
build_nop (build_pointer_type (type), exp));
}

// Returns indirect reference of EXP, which must be a pointer type.

tree
build_deref (tree exp)
{
  tree type = TREE_TYPE (exp);
  gcc_assert (POINTER_TYPE_P (type));

  if (TREE_CODE (exp) == ADDR_EXPR)
    return TREE_OPERAND (exp, 0);

  return build1 (INDIRECT_REF, TREE_TYPE (type), exp);
}

// Builds pointer offset expression PTR[INDEX]

tree
build_array_index (tree ptr, tree index)
{
  tree result_type_node = TREE_TYPE (ptr);
  tree elem_type_node = TREE_TYPE (result_type_node);
  tree size_exp;

  tree prod_result_type;
  prod_result_type = sizetype;

  // array element size
  size_exp = size_in_bytes (elem_type_node);

  if (integer_zerop (size_exp))
    {
      // Test for void case...
      if (TYPE_MODE (elem_type_node) == TYPE_MODE (void_type_node))
index = fold_convert (prod_result_type, index);
      else
{
// FIXME: should catch this earlier.
error ("invalid use of incomplete type %qD", TYPE_NAME (elem_type_node));
result_type_node = error_mark_node;
}
    }
  else if (integer_onep (size_exp))
    {
      // ...or byte case -- No need to multiply.
      index = fold_convert (prod_result_type, index);
    }
  else
    {
      if (TYPE_PRECISION (TREE_TYPE (index)) != TYPE_PRECISION (sizetype)
|| TYPE_UNSIGNED (TREE_TYPE (index)) != TYPE_UNSIGNED (sizetype))
{
tree type = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype),
TYPE_UNSIGNED (sizetype));
index = d_convert (type, index);
}
      index = fold_convert (prod_result_type,
fold_build2 (MULT_EXPR, TREE_TYPE (size_exp),
index, d_convert (TREE_TYPE (index), size_exp)));
    }

  // backend will ICE otherwise
  if (error_operand_p (result_type_node))
    return result_type_node;

  if (integer_zerop (index))
    return ptr;

  return build2 (POINTER_PLUS_EXPR, result_type_node, ptr, index);
}

// Builds pointer offset expression *(PTR OP IDX)
// OP could be a plus or minus expression.

tree
build_offset_op (tree_code op, tree ptr, tree idx)
{
  gcc_assert (op == MINUS_EXPR || op == PLUS_EXPR);

  if (op == MINUS_EXPR)
    idx = fold_build1 (NEGATE_EXPR, sizetype, idx);

  return build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
fold_convert (sizetype, idx));
}

tree
build_offset (tree ptr_node, tree byte_offset)
{
  tree ofs = fold_convert (Type::tsize_t->toCtype(), byte_offset);
  return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr_node), ptr_node, ofs);
}


// Implicitly converts void* T to byte* as D allows { void[] a; &a[3]; }

tree
void_okay_p (tree t)
{
  tree type = TREE_TYPE (t);
  tree totype = Type::tuns8->pointerTo()->toCtype();

  if (VOID_TYPE_P (TREE_TYPE (type)))
    return convert (totype, t);

  return t;
}

// Build an expression of code CODE, data type TYPE, and operands ARG0
// and ARG1. Perform relevant conversions needs for correct code operations.

tree
build_binary_op (tree_code code, tree type, tree arg0, tree arg1)
{
  tree t0 = TREE_TYPE (arg0);
  tree t1 = TREE_TYPE (arg1);

  bool unsignedp = TYPE_UNSIGNED (t0) || TYPE_UNSIGNED (t1);

  tree t = NULL_TREE;

  // Deal with float mod expressions immediately.
  if (code == FLOAT_MOD_EXPR)
    return build_float_modulus (TREE_TYPE (arg0), arg0, arg1);

  if (POINTER_TYPE_P (t0) && INTEGRAL_TYPE_P (t1))
    return build_nop (type, build_offset_op (code, arg0, arg1));

  if (INTEGRAL_TYPE_P (t0) && POINTER_TYPE_P (t1))
    return build_nop (type, build_offset_op (code, arg1, arg0));

  if (POINTER_TYPE_P (t0) && POINTER_TYPE_P (t1))
    {
      // Need to convert pointers to integers because tree-vrp asserts
      // against (ptr MINUS ptr).
      tree ptrtype = lang_hooks.types.type_for_mode (ptr_mode, TYPE_UNSIGNED (type));
      arg0 = d_convert (ptrtype, arg0);
      arg1 = d_convert (ptrtype, arg1);

      t = build2 (code, ptrtype, arg0, arg1);
    }
  else if (INTEGRAL_TYPE_P (type) && (TYPE_UNSIGNED (type) != unsignedp))
    {
      tree inttype = unsignedp ? d_unsigned_type (type) : d_signed_type (type);
      t = build2 (code, inttype, arg0, arg1);
    }
  else
    {
      // Front-end does not do this conversion and GCC does not
      // always do it right.
      if (COMPLEX_FLOAT_TYPE_P (t0) && !COMPLEX_FLOAT_TYPE_P (t1))
arg1 = d_convert (t0, arg1);
      else if (COMPLEX_FLOAT_TYPE_P (t1) && !COMPLEX_FLOAT_TYPE_P (t0))
arg0 = d_convert (t1, arg0);

      t = build2 (code, type, arg0, arg1);
    }

  return d_convert (type, t);
}

// Builds an array bounds checking condition, returning INDEX if true,
// else throws a RangeError exception.

tree
d_checked_index (Loc loc, tree index, tree upr, bool inclusive)
{
  if (!array_bounds_check())
    return index;

  return build3 (COND_EXPR, TREE_TYPE (index),
d_bounds_condition (index, upr, inclusive),
index, d_assert_call (loc, LIBCALL_ARRAY_BOUNDS));
}

// Builds the condition [INDEX < UPR] and optionally [INDEX >= 0]
// if INDEX is a signed type. For use in array bound checking routines.
// If INCLUSIVE, we allow equality to return true also.
// INDEX must be wrapped in a SAVE_EXPR to prevent multiple evaluation.

tree
d_bounds_condition (tree index, tree upr, bool inclusive)
{
  tree uindex = d_convert (d_unsigned_type (TREE_TYPE (index)), index);

  // Build condition to test that INDEX < UPR.
  tree condition = build2 (inclusive ? LE_EXPR : LT_EXPR, boolean_type_node, uindex, upr);

  // Build condition to test that INDEX >= 0.
  if (!TYPE_UNSIGNED (TREE_TYPE (index)))
    condition = build2 (TRUTH_ANDIF_EXPR, boolean_type_node, condition,
build2 (GE_EXPR, boolean_type_node, index, integer_zero_node));

  return condition;
}

// Returns TRUE if array bounds checking code generation is turned on.

bool
array_bounds_check (void)
{
  int result = global.params.useArrayBounds;

  if (result == 2)
    return true;

  if (result == 1)
    {
      // For D2 safe functions only
      FuncDeclaration *func = current_irstate->func;
      if (func && func->type->ty == Tfunction)
{
TypeFunction *tf = (TypeFunction *) func->type;
if (tf->trust == TRUSTsafe)
return true;
}
    }

  return false;
}

// Builds a BIND_EXPR around BODY for the variables VAR_CHAIN.

tree
bind_expr (tree var_chain, tree body)
{
  // TODO: only handles one var
  gcc_assert (TREE_CHAIN (var_chain) == NULL_TREE);

  if (DECL_INITIAL (var_chain))
    {
      tree ini = build_vinit (var_chain, DECL_INITIAL (var_chain));
      DECL_INITIAL (var_chain) = NULL_TREE;
      body = compound_expr (ini, body);
    }

  return make_temp (build3 (BIND_EXPR, TREE_TYPE (body), var_chain, body, NULL_TREE));
}

// Like compound_expr, but ARG0 or ARG1 might be NULL_TREE.

tree
maybe_compound_expr (tree arg0, tree arg1)
{
  if (arg0 == NULL_TREE)
    return arg1;
  else if (arg1 == NULL_TREE)
    return arg0;
  else
    return compound_expr (arg0, arg1);
}

// Like vcompound_expr, but ARG0 or ARG1 might be NULL_TREE.

tree
maybe_vcompound_expr (tree arg0, tree arg1)
{
  if (arg0 == NULL_TREE)
    return arg1;
  else if (arg1 == NULL_TREE)
    return arg0;
  else
    return vcompound_expr (arg0, arg1);
}

// Returns the TypeFunction class for Type T.
// Assumes T is already ->toBasetype()

TypeFunction *
get_function_type (Type *t)
{
  TypeFunction *tf = NULL;
  if (t->ty == Tpointer)
    t = t->nextOf()->toBasetype();
  if (t->ty == Tfunction)
    tf = (TypeFunction *) t;
  else if (t->ty == Tdelegate)
    tf = (TypeFunction *) ((TypeDelegate *) t)->next;
  return tf;
}

// Returns TRUE if CALLEE is a plain nested function outside the scope of CALLER.
// In which case, CALLEE is being called through an alias that was passed to CALLER.

bool
call_by_alias_p (FuncDeclaration *caller, FuncDeclaration *callee)
{
  if (!callee->isNested())
    return false;

  Dsymbol *dsym = callee;

  while (dsym)
    {
      if (dsym->isTemplateInstance())
return false;
      else if (dsym->isFuncDeclaration() == caller)
return false;
      dsym = dsym->toParent();
    }

  return true;
}

// Entry point for call routines. Builds a function call to FD.
// OBJECT is the 'this' reference passed and ARGS are the arguments to FD.

tree
d_build_call (FuncDeclaration *fd, tree object, Expressions *args)
{
  return d_build_call (get_function_type (fd->type),
build_address (fd->toSymbol()->Stree), object, args);
}

// Builds a CALL_EXPR of type TF to CALLABLE. OBJECT holds the 'this' pointer,
// ARGUMENTS are evaluated in left to right order, saved and promoted before passing.

tree
d_build_call (TypeFunction *tf, tree callable, tree object, Expressions *arguments)
{
  IRState *irs = current_irstate;
  tree ctype = TREE_TYPE (callable);
  tree callee = callable;
  tree saved_args = NULL_TREE;

  tree arg_list = NULL_TREE;

  if (POINTER_TYPE_P (ctype))
    ctype = TREE_TYPE (ctype);
  else
    callee = build_address (callable);

  gcc_assert (function_type_p (ctype));
  gcc_assert (tf != NULL);
  gcc_assert (tf->ty == Tfunction);

  // Evaluate the callee before calling it.
  if (TREE_SIDE_EFFECTS (callee))
    {
      callee = maybe_make_temp (callee);
      saved_args = callee;
    }

  if (TREE_CODE (ctype) == FUNCTION_TYPE)
    {
      if (object != NULL_TREE)
gcc_unreachable();
    }
  else if (object == NULL_TREE)
    {
      // Front-end apparently doesn't check this.
      if (TREE_CODE (callable) == FUNCTION_DECL)
{
error ("need 'this' to access member %s", IDENTIFIER_POINTER (DECL_NAME (callable)));
return error_mark_node;
}

      // Probably an internal error
      gcc_unreachable();
    }

  /* If this is a delegate call or a nested function being called as
a delegate, the object should not be NULL. */
  if (object != NULL_TREE)
    arg_list = build_tree_list (NULL_TREE, object);

  if (arguments)
    {
      // First pass, evaluated expanded tuples in function arguments.
      for (size_t i = 0; i < arguments->dim; ++i)
{
Lagain:
Expression *arg = (*arguments)[i];
gcc_assert (arg->op != TOKtuple);

if (arg->op == TOKcomma)
{
CommaExp *ce = (CommaExp *) arg;
tree tce = ce->e1->toElem (irs);
saved_args = maybe_vcompound_expr (saved_args, tce);
(*arguments)[i] = ce->e2;
goto Lagain;
}
}

      // if _arguments[] is the first argument.
      size_t dvarargs = (tf->linkage == LINKd && tf->varargs == 1);
      size_t nparams = Parameter::dim (tf->parameters);

      // Assumes arguments->dim <= formal_args->dim if (!this->varargs)
      for (size_t i = 0; i < arguments->dim; ++i)
{
Expression *arg = (*arguments)[i];
tree targ;

if (i < dvarargs)
{
// The hidden _arguments parameter
targ = arg->toElem (irs);
}
else if (i - dvarargs < nparams && i >= dvarargs)
{
// Actual arguments for declared formal arguments
Parameter *parg = Parameter::getNth (tf->parameters, i - dvarargs);
targ = convert_for_argument (arg->toElem (irs), arg, parg);
}
else
{
// Not all targets support passing unpromoted types, so
// promote anyway.
targ = arg->toElem (irs);
tree ptype = lang_hooks.types.type_promotes_to (TREE_TYPE (targ));

if (ptype != TREE_TYPE (targ))
targ = convert (ptype, targ);
}

// Evaluate the argument before passing to the function.
// Needed for left to right evaluation.
if (tf->linkage == LINKd && TREE_SIDE_EFFECTS (targ))
{
targ = maybe_make_temp (targ);
saved_args = maybe_vcompound_expr (saved_args, targ);
}
arg_list = chainon (arg_list, build_tree_list (0, targ));
}
    }

  tree result = d_build_call_list (TREE_TYPE (ctype), callee, arg_list);
  result = maybe_expand_builtin (result);

  return maybe_compound_expr (saved_args, result);
}

// Builds a call to AssertError or AssertErrorMsg.

tree
d_assert_call (Loc loc, LibCall libcall, tree msg)
{
  tree args[3];
  int nargs;

  if (msg != NULL)
    {
      args[0] = msg;
      args[1] = d_array_string (loc.filename ? loc.filename : "");
      args[2] = build_integer_cst (loc.linnum, Type::tuns32->toCtype());
      nargs = 3;
    }
  else
    {
      args[0] = d_array_string (loc.filename ? loc.filename : "");
      args[1] = build_integer_cst (loc.linnum, Type::tuns32->toCtype());
      args[2] = NULL_TREE;
      nargs = 2;
    }

  return build_libcall (libcall, nargs, args);
}


// Our internal list of library functions.
// Most are extern(C) - for those that are not, correct mangling must be ensured.
// List kept in ascii collating order to allow binary search

static const char *libcall_ids[LIBCALL_count] = {
    "_D9invariant12_d_invariantFC6ObjectZv",
    "_aaDelX", "_aaEqual",
    "_aaGetRvalueX", "_aaGetX",
    "_aaInX",
    "_adCmp2", "_adEq2",
    "_d_allocmemory", "_d_array_bounds",
    "_d_arrayappendT", "_d_arrayappendcTX",
    "_d_arrayappendcd", "_d_arrayappendwd",
    "_d_arrayassign", "_d_arraycast",
    "_d_arraycatT", "_d_arraycatnT",
    "_d_arraycopy", "_d_arrayctor",
    "_d_arrayliteralTX",
    "_d_arraysetassign", "_d_arraysetctor",
    "_d_arraysetlengthT", "_d_arraysetlengthiT",
    "_d_assert", "_d_assert_msg",
    "_d_assocarrayliteralTX",
    "_d_callfinalizer", "_d_callinterfacefinalizer",
    "_d_delarray", "_d_delarray_t", "_d_delclass",
    "_d_delinterface", "_d_delmemory",
    "_d_dynamic_cast", "_d_hidden_func", "_d_interface_cast",
    "_d_newarrayT", "_d_newarrayiT",
    "_d_newarraymTX", "_d_newarraymiTX",
    "_d_newclass", "_d_newitemT", "_d_newitemiT",
    "_d_switch_dstring", "_d_switch_error",
    "_d_switch_string", "_d_switch_ustring",
    "_d_throw", "_d_unittest", "_d_unittest_msg",
};

static FuncDeclaration *libcall_decls[LIBCALL_count];

// Library functions are generated as needed.
// This could probably be changed in the future to be
// more like GCC builtin trees.

FuncDeclaration *
get_libcall (LibCall libcall)
{
  FuncDeclaration *decl = libcall_decls[libcall];

  static Type *aatype = NULL;

  if (!decl)
    {
      Types targs;
      Type *treturn = Type::tvoid;
      bool varargs = false;

      // Build generic AA type void*[void*]
      if (aatype == NULL)
aatype = new TypeAArray (Type::tvoidptr, Type::tvoidptr);

      switch (libcall)
{
case LIBCALL_ASSERT:
case LIBCALL_ARRAY_BOUNDS:
case LIBCALL_SWITCH_ERROR:
// need to spec chararray/string because internal code passes string constants
targs.push (Type::tchar->arrayOf());
targs.push (Type::tuns32);
break;

case LIBCALL_ASSERT_MSG:
targs.push (Type::tchar->arrayOf());
targs.push (Type::tchar->arrayOf());
targs.push (Type::tuns32);
break;

case LIBCALL_UNITTEST:
targs.push (Type::tchar->arrayOf());
targs.push (Type::tuns32);
break;

case LIBCALL_UNITTEST_MSG:
targs.push (Type::tchar->arrayOf());
targs.push (Type::tchar->arrayOf());
targs.push (Type::tuns32);
break;

case LIBCALL_NEWCLASS:
targs.push (Type::typeinfoclass->type->constOf());
treturn = build_object_type();
break;

case LIBCALL_NEWARRAYT:
case LIBCALL_NEWARRAYIT:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_NEWARRAYMTX:
case LIBCALL_NEWARRAYMITX:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
targs.push (Type::tsize_t);
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_NEWITEMT:
case LIBCALL_NEWITEMIT:
targs.push (Type::dtypeinfo->type->constOf());
treturn = Type::tvoidptr;
break;

case LIBCALL_ALLOCMEMORY:
targs.push (Type::tsize_t);
treturn = Type::tvoidptr;
break;

case LIBCALL_DELCLASS:
case LIBCALL_DELINTERFACE:
targs.push (Type::tvoidptr);
break;

case LIBCALL_DELARRAY:
targs.push (Type::tvoid->arrayOf()->pointerTo());
break;

case LIBCALL_DELARRAYT:
targs.push (Type::tvoid->arrayOf()->pointerTo());
targs.push (Type::dtypeinfo->type->constOf());
break;

case LIBCALL_DELMEMORY:
targs.push (Type::tvoidptr->pointerTo());
break;

case LIBCALL_CALLFINALIZER:
case LIBCALL_CALLINTERFACEFINALIZER:
targs.push (Type::tvoidptr);
break;

case LIBCALL_ARRAYSETLENGTHT:
case LIBCALL_ARRAYSETLENGTHIT:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
targs.push (Type::tvoid->arrayOf()->pointerTo());
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_DYNAMIC_CAST:
case LIBCALL_INTERFACE_CAST:
targs.push (build_object_type());
targs.push (Type::typeinfoclass->type);
treturn = build_object_type();
break;

case LIBCALL_ADEQ2:
case LIBCALL_ADCMP2:
targs.push (Type::tvoid->arrayOf());
targs.push (Type::tvoid->arrayOf());
targs.push (Type::dtypeinfo->type->constOf());
treturn = Type::tint32;
break;

case LIBCALL_AAEQUAL:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (aatype);
targs.push (aatype);
treturn = Type::tint32;
break;

case LIBCALL_AAINX:
targs.push (aatype);
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tvoidptr);
treturn = Type::tvoidptr;
break;

case LIBCALL_AAGETX:
targs.push (aatype->pointerTo());
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
targs.push (Type::tvoidptr);
treturn = Type::tvoidptr;
break;

case LIBCALL_AAGETRVALUEX:
targs.push (aatype);
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
targs.push (Type::tvoidptr);
treturn = Type::tvoidptr;
break;

case LIBCALL_AADELX:
targs.push (aatype);
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tvoidptr);
treturn = Type::tbool;
break;

case LIBCALL_ARRAYCAST:
targs.push (Type::tsize_t);
targs.push (Type::tsize_t);
targs.push (Type::tvoid->arrayOf());
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYCOPY:
targs.push (Type::tsize_t);
targs.push (Type::tint8->arrayOf());
targs.push (Type::tint8->arrayOf());
treturn = Type::tint8->arrayOf();
break;

case LIBCALL_ARRAYCATT:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tint8->arrayOf());
targs.push (Type::tint8->arrayOf());
treturn = Type::tint8->arrayOf();
break;

case LIBCALL_ARRAYCATNT:
targs.push (Type::dtypeinfo->type->constOf());
// Currently 'uint', even if 64-bit
targs.push (Type::tuns32);
varargs = true;
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYAPPENDT:
targs.push (Type::dtypeinfo->type); //->constOf());
targs.push (Type::tint8->arrayOf()->pointerTo());
targs.push (Type::tint8->arrayOf());
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYAPPENDCTX:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tint8->arrayOf()->pointerTo());
targs.push (Type::tsize_t);
treturn = Type::tint8->arrayOf();
break;

case LIBCALL_ARRAYAPPENDCD:
targs.push (Type::tint8->arrayOf()->pointerTo());
targs.push (Type::tdchar);
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYAPPENDWD:
targs.push (Type::tint8->arrayOf()->pointerTo());
targs.push (Type::tdchar);
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYASSIGN:
case LIBCALL_ARRAYCTOR:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tvoid->arrayOf());
targs.push (Type::tvoid->arrayOf());
treturn = Type::tvoid->arrayOf();
break;

case LIBCALL_ARRAYSETASSIGN:
case LIBCALL_ARRAYSETCTOR:
targs.push (Type::tvoidptr);
targs.push (Type::tvoidptr);
targs.push (Type::tsize_t);
targs.push (Type::dtypeinfo->type->constOf());
treturn = Type::tvoidptr;
break;

case LIBCALL_THROW:
case LIBCALL_INVARIANT:
targs.push (build_object_type());
break;

case LIBCALL_SWITCH_USTRING:
targs.push (Type::twchar->arrayOf()->arrayOf());
targs.push (Type::twchar->arrayOf());
treturn = Type::tint32;
break;

case LIBCALL_SWITCH_DSTRING:
targs.push (Type::tdchar->arrayOf()->arrayOf());
targs.push (Type::tdchar->arrayOf());
treturn = Type::tint32;
break;

case LIBCALL_SWITCH_STRING:
targs.push (Type::tchar->arrayOf()->arrayOf());
targs.push (Type::tchar->arrayOf());
treturn = Type::tint32;
break;

case LIBCALL_ASSOCARRAYLITERALTX:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tvoid->arrayOf());
targs.push (Type::tvoid->arrayOf());
treturn = Type::tvoidptr;
break;

case LIBCALL_ARRAYLITERALTX:
targs.push (Type::dtypeinfo->type->constOf());
targs.push (Type::tsize_t);
treturn = Type::tvoidptr;
break;

case LIBCALL_HIDDEN_FUNC:
/* Argument is an Object, but can't use that as
LIBCALL_HIDDEN_FUNC is needed before the Object type is
created. */
targs.push (Type::tvoidptr);
break;

default:
gcc_unreachable();
}

      // Add parameter types.
      Parameters *args = new Parameters;
      args->setDim (targs.dim);
      for (size_t i = 0; i < targs.dim; i++)
(*args)[i] = new Parameter (0, targs[i], NULL, NULL);

      // Build extern(C) function.
      decl = FuncDeclaration::genCfunc (args, treturn, libcall_ids[libcall]);

      TypeFunction *tf = (TypeFunction *) decl->type;
      tf->varargs = varargs ? 1 : 0;
      libcall_decls[libcall] = decl;

      // These functions do not return except through catching a thrown exception.
      if (libcall == LIBCALL_ASSERT || libcall == LIBCALL_ASSERT_MSG
|| libcall == LIBCALL_UNITTEST || libcall == LIBCALL_UNITTEST_MSG
|| libcall == LIBCALL_ARRAY_BOUNDS || libcall == LIBCALL_SWITCH_ERROR)
TREE_THIS_VOLATILE (decl->toSymbol()->Stree) = 1;
    }

  return decl;
}

// Build call to LIBCALL. N_ARGS is the number of call arguments which are
// specified in as a tree array ARGS. The caller can force the return type
// of the call to FORCE_TYPE if the library call returns a generic value.

// This does not perform conversions on the arguments. This allows arbitrary data
// to be passed through varargs without going through the usual conversions.

tree
build_libcall (LibCall libcall, unsigned n_args, tree *args, tree force_type)
{
  FuncDeclaration *lib_decl = get_libcall (libcall);
  Type *type = lib_decl->type->nextOf();
  tree callee = build_address (lib_decl->toSymbol()->Stree);
  tree arg_list = NULL_TREE;

  for (int i = n_args - 1; i >= 0; i--)
    arg_list = tree_cons (NULL_TREE, args[i], arg_list);

  tree result = d_build_call_list (type->toCtype(), callee, arg_list);

  // Assumes caller knows what it is doing.
  if (force_type != NULL_TREE)
    return convert (force_type, result);

  return result;
}

// Build a call to CALLEE, passing ARGS as arguments. The expected return
// type is TYPE. TREE_SIDE_EFFECTS gets set depending on the const/pure
// attributes of the funcion and the SIDE_EFFECTS flags of the arguments.

tree
d_build_call_list (tree type, tree callee, tree args)
{
  int nargs = list_length (args);
  tree *pargs = new tree[nargs];
  for (size_t i = 0; args; args = TREE_CHAIN (args), i++)
    pargs[i] = TREE_VALUE (args);

  return build_call_array (type, callee, nargs, pargs);
}

// Conveniently construct the function arguments for passing
// to the d_build_call_list function.

tree
d_build_call_nary (tree callee, int n_args, ...)
{
  va_list ap;
  tree arg_list = NULL_TREE;
  tree fntype = TREE_TYPE (callee);

  va_start (ap, n_args);
  for (int i = n_args - 1; i >= 0; i--)
    arg_list = tree_cons (NULL_TREE, va_arg (ap, tree), arg_list);
  va_end (ap);

  return d_build_call_list (TREE_TYPE (fntype), build_address (callee), nreverse (arg_list));
}

// Call an fold the intrinsic call CALLEE with the argument ARG
// with the built-in function CODE passed.

static tree
expand_intrinsic_op (built_in_function code, tree callee, tree arg)
{
  tree exp = d_build_call_nary (builtin_decl_explicit (code), 1, arg);
  return fold_convert (TREE_TYPE (callee), fold (exp));
}

// Like expand_intrinsic_op, but takes two arguments.

static tree
expand_intrinsic_op2 (built_in_function code, tree callee, tree arg1, tree arg2)
{
  tree exp = d_build_call_nary (builtin_decl_explicit (code), 2, arg1, arg2);
  return fold_convert (TREE_TYPE (callee), fold (exp));
}

// Expand a front-end instrinsic call to bsr whose arguments are ARG.
// The original call expression is held in CALLEE.

static tree
expand_intrinsic_bsr (tree callee, tree arg)
{
  // Intrinsic bsr gets turned into (size - 1) - count_leading_zeros(arg).
  // %% TODO: The return value is supposed to be undefined if arg is zero.
  tree type = TREE_TYPE (arg);
  tree tsize = build_integer_cst (TREE_INT_CST_LOW (TYPE_SIZE (type)) - 1, type);
  tree exp = expand_intrinsic_op (BUILT_IN_CLZL, callee, arg);

  // Handle int -> long conversions.
  if (TREE_TYPE (exp) != type)
    exp = fold_convert (type, exp);

  exp = fold_build2 (MINUS_EXPR, type, tsize, exp);
  return fold_convert (TREE_TYPE (callee), exp);
}

// Expand the front-end built-in function INTRINSIC, which is either a
// call to bt, btc, btr, or bts. These intrinsics take two arguments,
// ARG1 and ARG2, and the original call expression is held in CALLEE.

static tree
expand_intrinsic_bt (Intrinsic intrinsic, tree callee, tree arg1, tree arg2)
{
  tree type = TREE_TYPE (TREE_TYPE (arg1));
  tree exp = build_integer_cst (TREE_INT_CST_LOW (TYPE_SIZE (type)), type);
  tree_code code;
  tree tval;

  // arg1[arg2 / exp]
  arg1 = build_array_index (arg1, fold_build2 (TRUNC_DIV_EXPR, type, arg2, exp));
  arg1 = indirect_ref (type, arg1);

  // mask = 1 << (arg2 % exp);
  arg2 = fold_build2 (TRUNC_MOD_EXPR, type, arg2, exp);
  arg2 = fold_build2 (LSHIFT_EXPR, type, size_one_node, arg2);

  // cond = arg1[arg2 / size] & mask;
  exp = fold_build2 (BIT_AND_EXPR, type, arg1, arg2);

  // cond ? -1 : 0;
  exp = fold_build3 (COND_EXPR, TREE_TYPE (callee), d_truthvalue_conversion (exp),
                    integer_minus_one_node, integer_zero_node);

  // Update the bit as needed.
  code = (intrinsic == INTRINSIC_BTC) ? BIT_XOR_EXPR :
    (intrinsic == INTRINSIC_BTR) ? BIT_AND_EXPR :
    (intrinsic == INTRINSIC_BTS) ? BIT_IOR_EXPR : ERROR_MARK;
  gcc_assert (code != ERROR_MARK);

  // arg1[arg2 / size] op= mask
  if (intrinsic == INTRINSIC_BTR)
    arg2 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg2), arg2);

  tval = build_local_temp (TREE_TYPE (callee));
  exp = vmodify_expr (tval, exp);
  arg1 = vmodify_expr (arg1, fold_build2 (code, TREE_TYPE (arg1), arg1, arg2));

  return compound_expr (exp, compound_expr (arg1, tval));
}

// Expand a front-end built-in call to va_arg, whose arguments are
// ARG1 and optionally ARG2.
// The original call expression is held in CALLEE.

// The cases handled here are:
// va_arg!T(ap);
// => return (T) VA_ARG_EXP<ap>
//
// va_arg!T(ap, T arg);
// => return arg = (T) VA_ARG_EXP<ap>;

static tree
expand_intrinsic_vaarg (tree callee, tree arg1, tree arg2)
{
  tree type;

  STRIP_NOPS (arg1);

  if (TREE_CODE (arg1) == ADDR_EXPR)
    arg1 = TREE_OPERAND (arg1, 0);

  if (arg2 == NULL_TREE)
    type = TREE_TYPE (callee);
  else
    {
      STRIP_NOPS (arg2);
      gcc_assert (TREE_CODE (arg2) == ADDR_EXPR);
      arg2 = TREE_OPERAND (arg2, 0);
      type = TREE_TYPE (arg2);
    }

  // Silently convert promoted types.
  tree ptype = lang_hooks.types.type_promotes_to (type);
  tree exp = build1 (VA_ARG_EXPR, ptype, arg1);

  if (type != ptype)
    exp = fold_convert (type, exp);

  if (arg2 != NULL_TREE)
    exp = vmodify_expr (arg2, exp);

  return exp;
}

// Expand a front-end built-in call to va_start, whose arguments are
// ARG1 and ARG2. The original call expression is held in CALLEE.

static tree
expand_intrinsic_vastart (tree callee, tree arg1, tree arg2)
{
  // The va_list argument should already have its address taken.
  // The second argument, however, is inout and that needs to be
  // fixed to prevent a warning.

  // Could be casting... so need to check type too?
  STRIP_NOPS (arg1);
  STRIP_NOPS (arg2);
  gcc_assert (TREE_CODE (arg1) == ADDR_EXPR && TREE_CODE (arg2) == ADDR_EXPR);

  arg2 = TREE_OPERAND (arg2, 0);
  // Assuming nobody tries to change the return type.
  return expand_intrinsic_op2 (BUILT_IN_VA_START, callee, arg1, arg2);
}

// If CALLEXP is a BUILT_IN_FRONTEND, expand and return inlined
// compiler generated instructions. Most map onto GCC builtins,
// others require a little extra work around them.

tree
maybe_expand_builtin (tree callexp)
{
  // More code duplication from C
  CallExpr ce (callexp);
  tree callee = ce.callee();

  if (POINTER_TYPE_P (TREE_TYPE (callee)))
    callee = TREE_OPERAND (callee, 0);

  if (TREE_CODE (callee) == FUNCTION_DECL
      && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_FRONTEND)
    {
      Intrinsic intrinsic = (Intrinsic) DECL_FUNCTION_CODE (callee);
      tree op1, op2;
      tree type;

      switch (intrinsic)
{
case INTRINSIC_BSF:
// builtin count_trailing_zeros matches behaviour of bsf.
// %% TODO: The return value is supposed to be undefined if op1 is zero.
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_CTZL, callexp, op1);

case INTRINSIC_BSR:
op1 = ce.nextArg();
return expand_intrinsic_bsr (callexp, op1);

case INTRINSIC_BTC:
case INTRINSIC_BTR:
case INTRINSIC_BTS:
op1 = ce.nextArg();
op2 = ce.nextArg();
return expand_intrinsic_bt (intrinsic, callexp, op1, op2);

case INTRINSIC_BSWAP:
/* Backend provides builtin bswap32.
Assumes first argument and return type is uint. */
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_BSWAP32, callexp, op1);

// Math intrinsics just map to their GCC equivalents.
case INTRINSIC_COS:
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_COSL, callexp, op1);

case INTRINSIC_SIN:
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_SINL, callexp, op1);

case INTRINSIC_RNDTOL:
// Not sure if llroundl stands as a good replacement for the
// expected behaviour of rndtol.
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_LLROUNDL, callexp, op1);

case INTRINSIC_SQRT:
// Have float, double and real variants of sqrt.
op1 = ce.nextArg();
type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
// op1 is an integral type - use double precision.
if (INTEGRAL_TYPE_P (type))
op1 = convert (double_type_node, op1);

if (type == double_type_node)
return expand_intrinsic_op (BUILT_IN_SQRT, callexp, op1);
else if (type == float_type_node)
return expand_intrinsic_op (BUILT_IN_SQRTF, callexp, op1);
else if (type == long_double_type_node)
return expand_intrinsic_op (BUILT_IN_SQRTL, callexp, op1);

gcc_unreachable();
break;

case INTRINSIC_LDEXP:
op1 = ce.nextArg();
op2 = ce.nextArg();
return expand_intrinsic_op2 (BUILT_IN_LDEXPL, callexp, op1, op2);

case INTRINSIC_FABS:
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_FABSL, callexp, op1);

case INTRINSIC_RINT:
op1 = ce.nextArg();
return expand_intrinsic_op (BUILT_IN_RINTL, callexp, op1);

case INTRINSIC_VA_ARG:
op1 = ce.nextArg();
op2 = ce.nextArg();
return expand_intrinsic_vaarg (callexp, op1, op2);

case INTRINSIC_C_VA_ARG:
op1 = ce.nextArg();
return expand_intrinsic_vaarg (callexp, op1, NULL_TREE);

case INTRINSIC_VA_START:
op1 = ce.nextArg();
op2 = ce.nextArg();
return expand_intrinsic_vastart (callexp, op1, op2);

default:
gcc_unreachable();
}
    }

  return callexp;
}

// Build and return the correct call to fmod depending on TYPE.
// ARG0 and ARG1 are the arguments pass to the function.

tree
build_float_modulus (tree type, tree arg0, tree arg1)
{
  tree fmodfn = NULL_TREE;
  tree basetype = type;

  if (COMPLEX_FLOAT_TYPE_P (basetype))
    basetype = TREE_TYPE (basetype);

  if (TYPE_MAIN_VARIANT (basetype) == double_type_node)
    fmodfn = builtin_decl_explicit (BUILT_IN_FMOD);
  else if (TYPE_MAIN_VARIANT (basetype) == float_type_node)
    fmodfn = builtin_decl_explicit (BUILT_IN_FMODF);
  else if (TYPE_MAIN_VARIANT (basetype) == long_double_type_node)
    fmodfn = builtin_decl_explicit (BUILT_IN_FMODL);

  if (!fmodfn)
    {
      // %qT pretty prints the tree type.
      error ("tried to perform floating-point modulo division on %qT", type);
      return error_mark_node;
    }

  if (COMPLEX_FLOAT_TYPE_P (type))
    return build2 (COMPLEX_EXPR, type,
d_build_call_nary (fmodfn, 2, real_part (arg0), arg1),
d_build_call_nary (fmodfn, 2, imaginary_part (arg0), arg1));

  if (SCALAR_FLOAT_TYPE_P (type))
    return d_build_call_nary (fmodfn, 2, arg0, arg1);

  // Should have caught this above.
  gcc_unreachable();
}

// Returns typeinfo reference for type T.

tree
build_typeinfo (Type *t)
{
  tree tinfo = t->getInternalTypeInfo (NULL)->toElem (current_irstate);
  gcc_assert (POINTER_TYPE_P (TREE_TYPE (tinfo)));
  return tinfo;
}

// Checks if DECL is an intrinsic or runtime library function that
// requires special processing. Marks the generated trees for DECL
// as BUILT_IN_FRONTEND so can be identified later.

void
maybe_set_builtin_frontend (FuncDeclaration *decl)
{
  if (!decl->ident)
    return;

  LibCall libcall = (LibCall) binary (decl->ident->string, libcall_ids, LIBCALL_count);

  if (libcall != LIBCALL_NONE)
    {
      // It's a runtime library function, add to libcall_decls.
      if (libcall_decls[libcall] == decl)
return;

      // This should have been done either by the front-end or get_libcall.
      TypeFunction *tf = (TypeFunction *) decl->type;
      gcc_assert (tf->parameters != NULL);

      libcall_decls[libcall] = decl;
    }
  else
    {
      // Check if it's a front-end builtin.
      static const char FeZe [] = "FNaNbNfeZe"; // @safe pure nothrow real function(real)
      static const char FeZe2[] = "FNaNbNeeZe"; // @trusted pure nothrow real function(real)
      static const char FuintZint[] = "FNaNbNfkZi"; // @safe pure nothrow int function(uint)
      static const char FuintZuint[] = "FNaNbNfkZk"; // @safe pure nothrow uint function(uint)
      static const char FulongZint[] = "FNaNbNfmZi"; // @safe pure nothrow int function(uint)
      static const char FrealZlong [] = "FNaNbNfeZl"; // @safe pure nothrow long function(real)
      static const char FlongplongZint [] = "FNaNbPmmZi"; // pure nothrow int function(long*, long)
      static const char FintpintZint [] = "FNaNbPkkZi"; // pure nothrow int function(int*, int)
      static const char FrealintZint [] = "FNaNbNfeiZe"; // @safe pure nothrow real function(real, int)

      Dsymbol *dsym = decl->toParent();
      TypeFunction *ftype = (TypeFunction *) (decl->tintro ? decl->tintro : decl->type);
      Module *mod;

      if (dsym == NULL)
return;

      mod = dsym->getModule();

      if (is_intrinsic_module_p (mod))
{
// Matches order of Intrinsic enum
static const char *intrinsic_names[] = {
"bsf", "bsr", "bswap",
"btc", "btr", "bts",
};
const size_t sz = sizeof (intrinsic_names) / sizeof (char *);
int i = binary (decl->ident->string, intrinsic_names, sz);

if (i == -1)
return;

switch (i)
{
case INTRINSIC_BSF:
case INTRINSIC_BSR:
if (!(strcmp (ftype->deco, FuintZint) == 0 || strcmp (ftype->deco, FulongZint) == 0))
return;
break;

case INTRINSIC_BSWAP:
if (!(strcmp (ftype->deco, FuintZuint) == 0))
return;
break;

case INTRINSIC_BTC:
case INTRINSIC_BTR:
case INTRINSIC_BTS:
if (!(strcmp (ftype->deco, FlongplongZint) == 0 || strcmp (ftype->deco, FintpintZint) == 0))
return;
break;
}

// Make sure 'i' is within the range we require.
gcc_assert (i >= INTRINSIC_BSF && i <= INTRINSIC_BTS);
tree t = decl->toSymbol()->Stree;

DECL_BUILT_IN_CLASS (t) = BUILT_IN_FRONTEND;
DECL_FUNCTION_CODE (t) = (built_in_function) i;
}
      else if (is_math_module_p (mod))
{
// Matches order of Intrinsic enum
static const char *math_names[] = {
"cos", "fabs", "ldexp",
"rint", "rndtol", "sin", "sqrt",
};
const size_t sz = sizeof (math_names) / sizeof (char *);
int i = binary (decl->ident->string, math_names, sz);

if (i == -1)
return;

// Adjust 'i' for this range of enums
i += INTRINSIC_COS;
gcc_assert (i >= INTRINSIC_COS && i <= INTRINSIC_SQRT);

switch (i)
{
case INTRINSIC_COS:
case INTRINSIC_FABS:
case INTRINSIC_RINT:
case INTRINSIC_SIN:
if (!(strcmp (ftype->deco, FeZe) == 0 || strcmp (ftype->deco, FeZe2) == 0))
return;
break;

case INTRINSIC_LDEXP:
if (!(strcmp (ftype->deco, FrealintZint) == 0))
return;
break;

case INTRINSIC_RNDTOL:
if (!(strcmp (ftype->deco, FrealZlong) == 0))
return;
break;

case INTRINSIC_SQRT:
if (!(strcmp (ftype->deco, "FNaNbNfdZd") == 0
|| strcmp (ftype->deco, "FNaNbNffZf") == 0
|| strcmp (ftype->deco, FeZe) == 0
|| strcmp (ftype->deco, FeZe2) == 0))
return;
break;
}

tree t = decl->toSymbol()->Stree;

// rndtol returns a long type, sqrt any float type,
// every other math builtin returns a real type.
Type *tf = decl->type->nextOf();
if ((i == INTRINSIC_RNDTOL && tf->ty == Tint64)
|| (i == INTRINSIC_SQRT && tf->isreal())
|| (i != INTRINSIC_RNDTOL && tf->ty == Tfloat80))
{
DECL_BUILT_IN_CLASS (t) = BUILT_IN_FRONTEND;
DECL_FUNCTION_CODE (t) = (built_in_function) i;
}
}
      else
{
TemplateInstance *ti = dsym->isTemplateInstance();

if (ti == NULL)
return;

tree t = decl->toSymbol()->Stree;

if (is_builtin_va_arg_p (ti->tempdecl, false))
{
DECL_BUILT_IN_CLASS (t) = BUILT_IN_FRONTEND;
DECL_FUNCTION_CODE (t) = (built_in_function) INTRINSIC_VA_ARG;
}
else if (is_builtin_va_arg_p (ti->tempdecl, true))
{
DECL_BUILT_IN_CLASS (t) = BUILT_IN_FRONTEND;
DECL_FUNCTION_CODE (t) = (built_in_function) INTRINSIC_C_VA_ARG;
}
else if (is_builtin_va_start_p (ti->tempdecl))
{
DECL_BUILT_IN_CLASS (t) = BUILT_IN_FRONTEND;
DECL_FUNCTION_CODE (t) = (built_in_function) INTRINSIC_VA_START;
}
}
    }
}

// Build and return D's internal exception Object.
// Different from the generic exception pointer.

tree
build_exception_object (void)
{
  tree obj_type = build_object_type()->toCtype();

  if (TREE_CODE (TREE_TYPE (obj_type)) == REFERENCE_TYPE)
    obj_type = TREE_TYPE (obj_type);

  // Like Java, the actual D exception object is one
  // pointer behind the exception header
  tree eh = d_build_call_nary (builtin_decl_explicit (BUILT_IN_EH_POINTER),
1, integer_zero_node);

  // treat exception header as (Object *)
  eh = build1 (NOP_EXPR, build_pointer_type (obj_type), eh);
  eh = build_offset_op (MINUS_EXPR, eh, TYPE_SIZE_UNIT (TREE_TYPE (eh)));

  return build1 (INDIRECT_REF, obj_type, eh);
}

// Build LABEL_DECL at location LOC for IDENT given.

tree
d_build_label (Loc loc, Identifier *ident)
{
  tree decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
ident ? get_identifier (ident->string) : NULL_TREE, void_type_node);
  DECL_CONTEXT (decl) = current_function_decl;
  DECL_MODE (decl) = VOIDmode;

  // Not setting this doesn't seem to cause problems (unlike VAR_DECLs).
  if (loc.filename)
    set_decl_location (decl, loc);

  return decl;
}

// If SYM is a nested function, return the static chain to be
// used when calling that function from FUNC.

// If SYM is a nested class or struct, return the static chain
// to be used when creating an instance of the class from FUNC.

tree
get_frame_for_symbol (FuncDeclaration *func, Dsymbol *sym)
{
  FuncDeclaration *nested_func = sym->isFuncDeclaration();
  FuncDeclaration *outer_func = NULL;

  if (nested_func != NULL)
    {
      // Check that the nested function is properly defined.
      if (!nested_func->fbody)
{
// Should instead error on line that references nested_func
nested_func->error ("nested function missing body");
return d_null_pointer;
}

      outer_func = nested_func->toParent2()->isFuncDeclaration();
      gcc_assert (outer_func != NULL);

      if (func != outer_func)
{
// If no frame pointer for this function
if (!func->vthis)
{
sym->error ("is a nested function and cannot be accessed from %s", func->toChars());
return d_null_pointer;
}

Dsymbol *this_func = func;

// Make sure we can get the frame pointer to the outer function,
// else we'll ICE later in tree-ssa.
while (nested_func != this_func)
{
FuncDeclaration *fd;
ClassDeclaration *cd;
StructDeclaration *sd;

// Special case for __ensure and __require.
if (nested_func->ident == Id::ensure || nested_func->ident == Id::require)
{
outer_func = func;
break;
}

if ((fd = this_func->isFuncDeclaration()))
{
if (outer_func == fd->toParent2())
break;

gcc_assert (fd->isNested() || fd->vthis);
}
else if ((cd = this_func->isClassDeclaration()))
{
if (!cd->isNested() || !cd->vthis)
goto cannot_get_frame;

if (outer_func == cd->toParent2())
break;
}
else if ((sd = this_func->isStructDeclaration()))
{
if (!sd->isNested() || !sd->vthis)
goto cannot_get_frame;

if (outer_func == sd->toParent2())
break;
}
else
{
cannot_get_frame:
func->error ("cannot get frame pointer to %s", sym->toChars());
return d_null_pointer;
}
this_func = this_func->toParent2();
}
}
    }
  else
    {
      /* It's a class (or struct). NewExp::toElem has already determined its
outer scope is not another class, so it must be a function. */

      while (sym && !sym->isFuncDeclaration())
sym = sym->toParent2();

      outer_func = (FuncDeclaration *) sym;

      /* Make sure we can access the frame of outer_func. */
      if (outer_func != func)
{
nested_func = func;
while (nested_func && nested_func != outer_func)
{
Dsymbol *outer = nested_func->toParent2();

if (!nested_func->isNested())
{
if (!nested_func->isMember2())
goto cannot_access_frame;
}

while (outer)
{
if (outer->isFuncDeclaration())
break;

outer = outer->toParent2();
}

nested_func = (FuncDeclaration *) outer;
}

if (!nested_func)
{
cannot_access_frame:
error ("cannot access frame of function '%s' from '%s'",
outer_func->toChars(), func->toChars());
return d_null_pointer;
}
}
    }

  if (!outer_func)
    outer_func = nested_func->toParent2()->isFuncDeclaration();

  gcc_assert (outer_func != NULL);

  FuncFrameInfo *ffo = get_frameinfo (outer_func);
  if (ffo->creates_frame || ffo->static_chain)
    return get_framedecl (func, outer_func);

  return d_null_pointer;
}

// Return the parent function of a nested class CD.

static FuncDeclaration *
d_nested_class (ClassDeclaration *cd)
{
  FuncDeclaration *fd = NULL;
  while (cd && cd->isNested())
    {
      Dsymbol *dsym = cd->toParent2();
      if ((fd = dsym->isFuncDeclaration()))
return fd;
      else
cd = dsym->isClassDeclaration();
    }
  return NULL;
}

// Return the parent function of a nested struct SD.

static FuncDeclaration *
d_nested_struct (StructDeclaration *sd)
{
  FuncDeclaration *fd = NULL;
  while (sd && sd->isNested())
    {
      Dsymbol *dsym = sd->toParent2();
      if ((fd = dsym->isFuncDeclaration()))
return fd;
      else
sd = dsym->isStructDeclaration();
    }
  return NULL;
}


// Starting from the current function FUNC, try to find a suitable value of
// 'this' in nested function instances. A suitable 'this' value is an
// instance of OCD or a class that has OCD as a base.

static tree
find_this_tree (FuncDeclaration *func, ClassDeclaration *ocd)
{
  while (func)
    {
      AggregateDeclaration *ad = func->isThis();
      ClassDeclaration *cd = ad ? ad->isClassDeclaration() : NULL;

      if (cd != NULL)
{
if (ocd == cd)
return get_decl_tree (func->vthis, func);
else if (ocd->isBaseOf (cd, NULL))
return convert_expr (get_decl_tree (func->vthis, func), cd->type, ocd->type);

func = d_nested_class (cd);
}
      else
{
if (func->isNested())
{
func = func->toParent2()->isFuncDeclaration();
continue;
}

func = NULL;
}
    }

  return NULL_TREE;
}

// Retrieve the outer class/struct 'this' value of DECL from the function FD
// where E is the expression requiring 'this'.

tree
build_vthis (Dsymbol *decl, FuncDeclaration *fd, Expression *e)
{
  ClassDeclaration *cd = decl->isClassDeclaration();
  StructDeclaration *sd = decl->isStructDeclaration();

  tree vthis_value = d_null_pointer;

  if (cd)
    {
      Dsymbol *outer = cd->toParent2();
      ClassDeclaration *cdo = outer->isClassDeclaration();
      FuncDeclaration *fdo = outer->isFuncDeclaration();

      if (cdo)
{
vthis_value = find_this_tree (fd, cdo);
if (vthis_value == NULL_TREE)
e->error ("outer class %s 'this' needed to 'new' nested class %s",
cdo->toChars(), cd->toChars());
}
      else if (fdo)
{
// If a class nested in a function has no methods and there
// are no other nested functions, any static chain created
// here will never be translated. Use a null pointer for the
// link in this case.
FuncFrameInfo *ffo = get_frameinfo (fdo);
if (ffo->creates_frame || ffo->static_chain
|| fdo->hasNestedFrameRefs())
vthis_value = get_frame_for_symbol (fd, cd);
else if (fdo->vthis && fdo->vthis->type != Type::tvoidptr)
vthis_value = get_decl_tree (fdo->vthis, fd);
else
vthis_value = d_null_pointer;
}
      else
gcc_unreachable();
    }
  else if (sd)
    {
      Dsymbol *outer = sd->toParent2();
      ClassDeclaration *cdo = outer->isClassDeclaration();
      FuncDeclaration *fdo = outer->isFuncDeclaration();

      if (cdo)
{
vthis_value = find_this_tree (fd, cdo);
if (vthis_value == NULL_TREE)
e->error ("outer class %s 'this' needed to create nested struct %s",
cdo->toChars(), sd->toChars());
}
      else if (fdo)
{
FuncFrameInfo *ffo = get_frameinfo (fdo);
if (ffo->creates_frame || ffo->static_chain
|| fdo->hasNestedFrameRefs())
vthis_value = get_frame_for_symbol (fd, sd);
else
vthis_value = d_null_pointer;
}
      else
gcc_unreachable();
    }

  return vthis_value;
}

tree
build_frame_type (FuncDeclaration *func)
{
  FuncFrameInfo *ffi = get_frameinfo (func);

  if (ffi->frame_rec != NULL_TREE)
    return ffi->frame_rec;

  tree frame_rec_type = make_node (RECORD_TYPE);
  char *name = concat (ffi->is_closure ? "CLOSURE." : "FRAME.",
func->toPrettyChars(), NULL);
  TYPE_NAME (frame_rec_type) = get_identifier (name);
  free (name);

  tree ptr_field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("__chain"), ptr_type_node);
  DECL_CONTEXT (ptr_field) = frame_rec_type;
  TYPE_READONLY (frame_rec_type) = 1;

  tree fields = chainon (NULL_TREE, ptr_field);

  if (!ffi->is_closure)
    {
      // __ensure and __require never becomes a closure, but could still be referencing
      // parameters of the calling function. So we add all parameters as nested refs.
      // This is written as such so that all parameters appear at the front of the frame
      // so that overriding methods match the same layout when inheriting a contract.
      if ((global.params.useIn && func->frequire) || (global.params.useOut && func->fensure))
{
for (size_t i = 0; func->parameters && i < func->parameters->dim; i++)
{
VarDeclaration *v = (*func->parameters)[i];
// Remove if already in closureVars so can push to front.
for (size_t j = i; j < func->closureVars.dim; j++)
{
Dsymbol *s = func->closureVars[j];
if (s == v)
{
func->closureVars.remove (j);
break;
}
}
func->closureVars.insert (i, v);
}

// Also add hidden 'this' to outer context.
if (func->vthis)
{
for (size_t i = 0; i < func->closureVars.dim; i++)
{
Dsymbol *s = func->closureVars[i];
if (s == func->vthis)
{
func->closureVars.remove (i);
break;
}
}
func->closureVars.insert (0, func->vthis);
}
}
    }

  for (size_t i = 0; i < func->closureVars.dim; i++)
    {
      VarDeclaration *v = func->closureVars[i];
      Symbol *s = v->toSymbol();
      tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
v->ident ? get_identifier (v->ident->string) : NULL_TREE,
declaration_type (v));
      s->SframeField = field;
      set_decl_location (field, v);
      DECL_CONTEXT (field) = frame_rec_type;
      fields = chainon (fields, field);
      TREE_USED (s->Stree) = 1;

      // Can't do nrvo if the variable is put in a frame.
      if (func->nrvo_can && func->nrvo_var == v)
func->nrvo_can = 0;

      // Because the value needs to survive the end of the scope.
      if (ffi->is_closure && v->needsAutoDtor())
v->error("has scoped destruction, cannot build closure");
    }

  TYPE_FIELDS (frame_rec_type) = fields;
  layout_type (frame_rec_type);
  d_keep (frame_rec_type);

  return frame_rec_type;
}

// Return the frame of FD. This could be a static chain or a closure
// passed via the hidden 'this' pointer.

FuncFrameInfo *
get_frameinfo (FuncDeclaration *fd)
{
  Symbol *fds = fd->toSymbol();
  if (fds->frameInfo)
    return fds->frameInfo;

  FuncFrameInfo *ffi = new FuncFrameInfo;
  ffi->creates_frame = false;
  ffi->static_chain = false;
  ffi->is_closure = false;
  ffi->frame_rec = NULL_TREE;

  fds->frameInfo = ffi;

  // Nested functions, or functions with nested refs must create
  // a static frame for local variables to be referenced from.
  if (fd->closureVars.dim != 0)
    ffi->creates_frame = true;

  if (fd->vthis && fd->vthis->type == Type::tvoidptr)
    ffi->creates_frame = true;

  // Functions with In/Out contracts pass parameters to nested frame.
  if (fd->fensure || fd->frequire)
    ffi->creates_frame = true;

  // D2 maybe setup closure instead.
  if (fd->needsClosure())
    {
      ffi->creates_frame = true;
      ffi->is_closure = true;
    }
  else if (fd->closureVars.dim == 0)
    {
      /* If fd is nested (deeply) in a function that creates a closure,
then fd inherits that closure via hidden vthis pointer, and
doesn't create a stack frame at all. */
      FuncDeclaration *ff = fd;

      while (ff)
{
FuncFrameInfo *ffo = get_frameinfo (ff);
AggregateDeclaration *ad;

if (ff != fd && ffo->creates_frame)
{
gcc_assert (ffo->frame_rec);
ffi->creates_frame = false;
ffi->static_chain = true;
ffi->is_closure = ffo->is_closure;
gcc_assert (COMPLETE_TYPE_P (ffo->frame_rec));
ffi->frame_rec = ffo->frame_rec;
break;
}

// Stop looking if no frame pointer for this function.
if (ff->vthis == NULL)
break;

ad = ff->isThis();
if (ad && ad->isNested())
{
while (ad->isNested())
{
Dsymbol *d = ad->toParent2();
ad = d->isAggregateDeclaration();
ff = d->isFuncDeclaration();

if (ad == NULL)
break;
}
}
else
ff = ff->toParent2()->isFuncDeclaration();
}
    }

  // Build type now as may be referenced from another module.
  if (ffi->creates_frame)
    ffi->frame_rec = build_frame_type (fd);

  return ffi;
}

// Return a pointer to the frame/closure block of OUTER
// so can be accessed from the function INNER.

tree
get_framedecl (FuncDeclaration *inner, FuncDeclaration *outer)
{
  tree result = current_irstate->sthis;
  FuncDeclaration *fd = inner;

  while (fd && fd != outer)
    {
      AggregateDeclaration *ad;
      ClassDeclaration *cd;
      StructDeclaration *sd;

      if (get_frameinfo (fd)->creates_frame)
{
// like compon (indirect, field0) parent frame link is the first field;
result = indirect_ref (ptr_type_node, result);
}

      if (fd->isNested())
fd = fd->toParent2()->isFuncDeclaration();
      /* get_framedecl is only used to get the pointer to a function's frame
(not a class instances). With the current implementation, the link
the frame/closure record always points to the outer function's frame even
if there are intervening nested classes or structs.
So, we can just skip over those... */
      else if ((ad = fd->isThis()) && (cd = ad->isClassDeclaration()))
fd = d_nested_class (cd);
      else if ((ad = fd->isThis()) && (sd = ad->isStructDeclaration()))
fd = d_nested_struct (sd);
      else
break;
    }

  if (fd == outer)
    {
      tree frame_rec = get_frameinfo (outer)->frame_rec;

      if (frame_rec != NULL_TREE)
{
result = build_nop (build_pointer_type (frame_rec), result);
return result;
}
      else
{
inner->error ("forward reference to frame of %s", outer->toChars());
return d_null_pointer;
}
    }
  else
    {
      inner->error ("cannot access frame of %s", outer->toChars());
      return d_null_pointer;
    }
}

// Special case: If a function returns a nested class with functions
// but there are no "closure variables" the frontend (needsClosure)
// returns false even though the nested class _is_ returned from the
// function. (See case 4 in needsClosure)
// A closure is strictly speaking not necessary, but we also can not
// use a static function chain for functions in the nested class as
// they can be called from outside. GCC's nested functions can't deal
// with those kind of functions. We have to detect them manually here
// and make sure we neither construct a static chain nor a closure.

static bool
is_degenerate_closure (FuncDeclaration *f)
{
  if (!f->needsClosure() && f->closureVars.dim == 0)
  {
    Type *tret = ((TypeFunction *) f->type)->next;
    gcc_assert(tret);
    tret = tret->toBasetype();
    if (tret->ty == Tclass || tret->ty == Tstruct)
    {
      Dsymbol *st = tret->toDsymbol(NULL);
      for (Dsymbol *s = st->parent; s; s = s->parent)
      {
if (s == f)
return true;
      }
    }
  }
  return false;
}

// Return true if function F needs to have the static chain passed to it.
// This only applies to nested function handling provided by the GDC
// front end (not D closures).

bool
needs_static_chain (FuncDeclaration *f)
{
  Dsymbol *s;
  FuncDeclaration *pf = NULL;
  TemplateInstance *ti = NULL;

  if (f->isNested())
    {
      s = f->toParent();
      ti = s->isTemplateInstance();
      if (ti && ti->enclosing == NULL && ti->parent->isModule())
return false;

      pf = f->toParent2()->isFuncDeclaration();
      if (pf && !get_frameinfo (pf)->is_closure)
return true;
    }

  if (f->isStatic())
    return false;

  s = f->toParent2();

  while (s)
    {
      AggregateDeclaration *ad = s->isAggregateDeclaration();
      if (!ad || !ad->isNested())
break;

      if (!s->isTemplateInstance())
break;

      s = s->toParent2();
      if ((pf = s->isFuncDeclaration())
&& !get_frameinfo (pf)->is_closure
&& !is_degenerate_closure (pf))
return true;
    }

  return false;
}


// Construct a WrappedExp, whose components are an EXP_NODE, which contains
// a list of instructions in GCC to be passed through.

WrappedExp::WrappedExp (Loc loc, TOK op, tree exp_node, Type *type)
    : Expression (loc, op, sizeof (WrappedExp))
{
  this->exp_node = exp_node;
  this->type = type;
}

// Write C-style representation of WrappedExp to BUF.

void
WrappedExp::toCBuffer (OutBuffer *buf, HdrGenState *hgs ATTRIBUTE_UNUSED)
{
  buf->printf ("<wrapped expression>");
}

// Build and return expression tree for WrappedExp.

elem *
WrappedExp::toElem (IRState *)
{
  return exp_node;
}

// Write out all fields for aggregate DECL. For classes, write
// out base class fields first, and adds all interfaces last.

void
layout_aggregate_type (AggLayout *al, AggregateDeclaration *decl)
{
  ClassDeclaration *cd = decl->isClassDeclaration();
  bool inherited_p = (al->decl != decl);

  if (cd != NULL)
    {
      if (cd->baseClass)
layout_aggregate_type (al, cd->baseClass);
      else
{
// This is the base class (Object) or interface.
tree objtype = TREE_TYPE (cd->type->toCtype());

// Add the virtual table pointer, and optionally the monitor fields.
tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
get_identifier ("__vptr"), d_vtbl_ptr_type_node);
DECL_ARTIFICIAL (field) = 1;
DECL_IGNORED_P (field) = inherited_p;

insert_aggregate_field (al, field, 0);

DECL_VIRTUAL_P (field) = 1;
DECL_FCONTEXT (field) = objtype;
TYPE_VFIELD (al->type) = field;

if (cd->cpp == false)
{
field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
get_identifier ("__monitor"), ptr_type_node);
DECL_FCONTEXT (field) = objtype;
DECL_ARTIFICIAL (field) = 1;
DECL_IGNORED_P (field) = inherited_p;
insert_aggregate_field (al, field, Target::ptrsize);
}
}
    }

  if (decl->fields.dim)
    {
      tree fcontext = decl->type->toCtype();

      if (POINTER_TYPE_P (fcontext))
fcontext = TREE_TYPE (fcontext);

      for (size_t i = 0; i < decl->fields.dim; i++)
{
// D anonymous unions just put the fields into the outer struct...
// Does this cause problems?
VarDeclaration *var = decl->fields[i];
gcc_assert (var && var->isField());

tree ident = var->ident ? get_identifier (var->ident->string) : NULL_TREE;
tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, ident,
declaration_type (var));
set_decl_location (field, var);
var->csym = new Symbol;
var->csym->Stree = field;

DECL_CONTEXT (field) = al->type;
DECL_FCONTEXT (field) = fcontext;
DECL_FIELD_OFFSET (field) = size_int (var->offset);
DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;

DECL_ARTIFICIAL (field) = inherited_p;
DECL_IGNORED_P (field) = inherited_p;
SET_DECL_OFFSET_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));

TREE_THIS_VOLATILE (field) = TYPE_VOLATILE (TREE_TYPE (field));
layout_decl (field, 0);

if (var->size (var->loc))
{
gcc_assert (DECL_MODE (field) != VOIDmode);
gcc_assert (DECL_SIZE (field) != NULL_TREE);
}

TYPE_FIELDS(al->type) = chainon (TYPE_FIELDS (al->type), field);
}
    }

  if (cd && cd->vtblInterfaces)
    {
      for (size_t i = 0; i < cd->vtblInterfaces->dim; i++)
{
BaseClass *bc = (*cd->vtblInterfaces)[i];
tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
Type::tvoidptr->pointerTo()->toCtype());
DECL_ARTIFICIAL (field) = 1;
DECL_IGNORED_P (field) = 1;
insert_aggregate_field (al, field, bc->offset);
}
    }
}

// Add a compiler generated field DECL at OFFSET into aggregate.

void
insert_aggregate_field (AggLayout *al, tree decl, size_t offset)
{
  DECL_CONTEXT (decl) = al->type;
  SET_DECL_OFFSET_ALIGN (decl, TYPE_ALIGN (TREE_TYPE (decl)));
  DECL_FIELD_OFFSET (decl) = size_int (offset);
  DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;

  // Must set this or we crash with DWARF debugging.
  set_decl_location (decl, al->decl->loc);

  TREE_THIS_VOLATILE (decl) = TYPE_VOLATILE (TREE_TYPE (decl));

  layout_decl (decl, 0);
  TYPE_FIELDS(al->type) = chainon (TYPE_FIELDS (al->type), decl);
}

// Wrap-up and compute finalised aggregate type. Writing out
// any GCC attributes that were applied to the type declaration.

void
finish_aggregate_type (AggLayout *al, Expressions *attrs)
{
  unsigned structsize = al->decl->structsize;
  unsigned alignsize = al->decl->alignsize;

  TYPE_SIZE (al->type) = NULL_TREE;

  if (attrs)
    decl_attributes (&al->type, build_attributes (attrs),
ATTR_FLAG_TYPE_IN_PLACE);

  TYPE_SIZE (al->type) = bitsize_int (structsize * BITS_PER_UNIT);
  TYPE_SIZE_UNIT (al->type) = size_int (structsize);
  TYPE_ALIGN (al->type) = alignsize * BITS_PER_UNIT;
  TYPE_PACKED (al->type) = (alignsize == 1);

  compute_record_mode (al->type);

  // Set up variants.
  for (tree x = TYPE_MAIN_VARIANT (al->type); x; x = TYPE_NEXT_VARIANT (x))
    {
      TYPE_FIELDS (x) = TYPE_FIELDS (al->type);
      TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (al->type);
      TYPE_ALIGN (x) = TYPE_ALIGN (al->type);
      TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (al->type);
    }
}

// Routines for getting an index or slice of an array where '$' was used
// in the slice. A temp var INI_V would have been created that needs to
// be bound into it's own scope.

ArrayScope::ArrayScope (VarDeclaration *ini_v, const Loc& loc) :
  var_(ini_v)
{
  /* If STCconst, the temp var is not required. */
  if (this->var_ && !(this->var_->storage_class & STCconst))
    {
      /* Need to set the location or the expand_decl in the BIND_EXPR will
cause the line numbering for the statement to be incorrect. */
      /* The variable itself is not included in the debugging information. */
      this->var_->loc = loc;
      Symbol *s = this->var_->toSymbol();
      tree decl = s->Stree;
      DECL_CONTEXT (decl) = current_function_decl;
    }
  else
    this->var_ = NULL;
}

// Set index expression E of type T as the initialiser for
// the temp var decl to be used.

tree
ArrayScope::setArrayExp (tree e, Type *t)
{
  if (this->var_)
    {
      tree v = this->var_->toSymbol()->Stree;
      if (t->toBasetype()->ty != Tsarray)
e = maybe_make_temp (e);
      DECL_INITIAL (v) = get_array_length (e, t);
    }
  return e;
}

// Wrap-up temp var into a BIND_EXPR.

tree
ArrayScope::finish (tree e)
{
  if (this->var_)
    {
      Symbol *s = this->var_->toSymbol();
      tree t = s->Stree;
      if (TREE_CODE (t) == VAR_DECL)
{
gcc_assert (!s->SframeField);
return bind_expr (t, e);
}
      else
gcc_unreachable();
    }
  return e;
}

Something went wrong with that request. Please try again.