aboutsummaryrefslogtreecommitdiff
path: root/gcc/fixed-value.c
diff options
context:
space:
mode:
authorRichard Sandiford <richard.sandiford@linaro.org>2017-09-13 17:04:08 +0000
committerRichard Sandiford <rsandifo@gcc.gnu.org>2017-09-13 17:04:08 +0000
commit94e23f53d700769c453d31881c089d06cde823dd (patch)
treec40d61202954230108cceb97d333c2c34f9c4ddb /gcc/fixed-value.c
parentbb5d97112a8272c608b18167de9255949129890c (diff)
downloadgcc-94e23f53d700769c453d31881c089d06cde823dd.zip
gcc-94e23f53d700769c453d31881c089d06cde823dd.tar.gz
gcc-94e23f53d700769c453d31881c089d06cde823dd.tar.bz2
Turn SECONDARY_MEMORY_NEEDED_MODE into a target hook
This includes a change to LRA. Previously the code was: if (sclass == NO_REGS && dclass == NO_REGS) return false; #ifdef SECONDARY_MEMORY_NEEDED if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)) #ifdef SECONDARY_MEMORY_NEEDED_MODE && ((sclass != NO_REGS && dclass != NO_REGS) || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src))) #endif ) { *sec_mem_p = true; return false; } #endif in which the positioning of the second ifdef meant that defining SECONDARY_MEMORY_NEEDED_MODE to its default value was not a no-op: without a definition, we would consider using secondary reloads for mem<-reg and reg<-mem reloads even if the secondary memory has the same mode as the original mem, while defining it would avoid this. The latter behaviour seems correct. The default is different for reload and LRA. For LRA the default is to use the original mode, while reload promotes smaller-than-word integral modes to word mode: if (GET_MODE_BITSIZE (mode) < BITS_PER_WORD && INTEGRAL_MODE_P (mode)) mode = mode_for_size (BITS_PER_WORD, GET_MODE_CLASS (mode), 0).require (); Some of the ports that have switched to LRA seemed to have SECONDARY_MEMORY_NEEDED_MDOEs based on the old reload definition, and still referred to the reload.c:get_secondary_mem function in the comments. The patch just keeps them as-is. 2017-09-13 Richard Sandiford <richard.sandiford@linaro.org> Alan Hayward <alan.hayward@arm.com> David Sherwood <david.sherwood@arm.com> gcc/ * target.def (secondary_memory_needed_mode): New hook: * targhooks.c (default_secondary_memory_needed_mode): Declare. * targhooks.h (default_secondary_memory_needed_mode): New function. * doc/tm.texi.in (SECONDARY_MEMORY_NEEDED_MODE): Replace with... (TARGET_SECONDARY_MEMORY_NEEDED_MODE): ...this. * doc/tm.texi: Regenerate. * lra-constraints.c (check_and_process_move): Use targetm.secondary_memory_needed_mode instead of TARGET_SECONDARY_MEMORY_NEEDED_MODE. (curr_insn_transform): Likewise. * reload.c (get_secondary_mem): Likewise. * config/alpha/alpha.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/alpha/alpha.c (alpha_secondary_memory_needed_mode): New function. (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. * config/i386/i386.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/i386/i386.c (ix86_secondary_memory_needed_mode): New function. (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. * config/powerpcspe/powerpcspe.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/powerpcspe/powerpcspe-protos.h (rs6000_secondary_memory_needed_mode): Delete. * config/powerpcspe/powerpcspe.c (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. (rs6000_secondary_memory_needed_mode): Make static. * config/rs6000/rs6000.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/rs6000/rs6000-protos.h (rs6000_secondary_memory_needed_mode): Delete. * config/rs6000/rs6000.c (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. (rs6000_secondary_memory_needed_mode): Make static. * config/s390/s390.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/s390/s390.c (s390_secondary_memory_needed_mode): New function. (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. * config/sparc/sparc.h (SECONDARY_MEMORY_NEEDED_MODE): Delete. * config/sparc/sparc.c (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Redefine. (sparc_secondary_memory_needed_mode): New function. * system.h (TARGET_SECONDARY_MEMORY_NEEDED_MODE): Poison. Co-Authored-By: Alan Hayward <alan.hayward@arm.com> Co-Authored-By: David Sherwood <david.sherwood@arm.com> From-SVN: r252455
Diffstat (limited to 'gcc/fixed-value.c')
0 files changed, 0 insertions, 0 deletions
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159
/****************************************************************************
 *                                                                          *
 *                         GNAT COMPILER COMPONENTS                         *
 *                                                                          *
 *                                U T I L S                                 *
 *                                                                          *
 *                          C Implementation File                           *
 *                                                                          *
 *          Copyright (C) 1992-2021, Free Software Foundation, Inc.         *
 *                                                                          *
 * GNAT is free software;  you can  redistribute it  and/or modify it under *
 * terms of the  GNU General Public License as published  by the Free Soft- *
 * ware  Foundation;  either version 3,  or (at your option) any later ver- *
 * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
 * OUT 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/>.                                          *
 *                                                                          *
 * GNAT was originally developed  by the GNAT team at  New York University. *
 * Extensive contributions were provided by Ada Core Technologies Inc.      *
 *                                                                          *
 ****************************************************************************/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "function.h"
#include "tree.h"
#include "stringpool.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "alias.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "attribs.h"
#include "varasm.h"
#include "toplev.h"
#include "opts.h"
#include "output.h"
#include "debug.h"
#include "convert.h"
#include "common/common-target.h"
#include "langhooks.h"
#include "tree-dump.h"
#include "tree-inline.h"

#include "ada.h"
#include "types.h"
#include "atree.h"
#include "nlists.h"
#include "snames.h"
#include "uintp.h"
#include "fe.h"
#include "sinfo.h"
#include "einfo.h"
#include "ada-tree.h"
#include "gigi.h"

/* If nonzero, pretend we are allocating at global level.  */
int force_global;

/* The default alignment of "double" floating-point types, i.e. floating
   point types whose size is equal to 64 bits, or 0 if this alignment is
   not specifically capped.  */
int double_float_alignment;

/* The default alignment of "double" or larger scalar types, i.e. scalar
   types whose size is greater or equal to 64 bits, or 0 if this alignment
   is not specifically capped.  */
int double_scalar_alignment;

/* True if floating-point arithmetics may use wider intermediate results.  */
bool fp_arith_may_widen = true;

/* Tree nodes for the various types and decls we create.  */
tree gnat_std_decls[(int) ADT_LAST];

/* Functions to call for each of the possible raise reasons.  */
tree gnat_raise_decls[(int) LAST_REASON_CODE + 1];

/* Likewise, but with extra info for each of the possible raise reasons.  */
tree gnat_raise_decls_ext[(int) LAST_REASON_CODE + 1];

/* Forward declarations for handlers of attributes.  */
static tree handle_const_attribute (tree *, tree, tree, int, bool *);
static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
static tree handle_no_stack_protector_attribute (tree *, tree, tree, int, bool *);
static tree handle_strub_attribute (tree *, tree, tree, int, bool *);
static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
static tree handle_always_inline_attribute (tree *, tree, tree, int, bool *);
static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
static tree handle_used_attribute (tree *, tree, tree, int, bool *);
static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
static tree handle_target_attribute (tree *, tree, tree, int, bool *);
static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
static tree handle_vector_size_attribute (tree *, tree, tree, int, bool *);
static tree handle_vector_type_attribute (tree *, tree, tree, int, bool *);
static tree handle_zero_call_used_regs_attribute (tree *, tree, tree, int,
						  bool *);

static const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
{
  { "cold", true,  true,  true  },
  { "hot" , true,  true,  true  },
  { NULL  , false, false, false }
};

static const struct attribute_spec::exclusions attr_stack_protect_exclusions[] =
{
  { "stack_protect", true, false, false },
  { "no_stack_protector", true, false, false },
  { NULL, false, false, false },
};

/* Fake handler for attributes we don't properly support, typically because
   they'd require dragging a lot of the common-c front-end circuitry.  */
static tree fake_attribute_handler (tree *, tree, tree, int, bool *);

/* Table of machine-independent internal attributes for Ada.  We support
   this minimal set of attributes to accommodate the needs of builtins.  */
const struct attribute_spec gnat_internal_attribute_table[] =
{
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
       affects_type_identity, handler, exclude } */
  { "const",        0, 0,  true,  false, false, false,
    handle_const_attribute, NULL },
  { "nothrow",      0, 0,  true,  false, false, false,
    handle_nothrow_attribute, NULL },
  { "pure",         0, 0,  true,  false, false, false,
    handle_pure_attribute, NULL },
  { "no vops",      0, 0,  true,  false, false, false,
    handle_novops_attribute, NULL },
  { "nonnull",      0, -1, false, true,  true,  false,
    handle_nonnull_attribute, NULL },
  { "sentinel",     0, 1,  false, true,  true,  false,
    handle_sentinel_attribute, NULL },
  { "noreturn",     0, 0,  true,  false, false, false,
    handle_noreturn_attribute, NULL },
  { "stack_protect",0, 0, true,  false, false, false,
    handle_stack_protect_attribute,
    attr_stack_protect_exclusions },
  { "no_stack_protector",0, 0, true,  false, false, false,
    handle_no_stack_protector_attribute,
    attr_stack_protect_exclusions },
  { "strub",	    0, 1, false, true, false, true,
    handle_strub_attribute, NULL },
  { "noinline",     0, 0,  true,  false, false, false,
    handle_noinline_attribute, NULL },
  { "noclone",      0, 0,  true,  false, false, false,
    handle_noclone_attribute, NULL },
  { "no_icf",       0, 0,  true,  false, false, false,
    handle_noicf_attribute, NULL },
  { "noipa",        0, 0,  true,  false, false, false,
    handle_noipa_attribute, NULL },
  { "leaf",         0, 0,  true,  false, false, false,
    handle_leaf_attribute, NULL },
  { "always_inline",0, 0,  true,  false, false, false,
    handle_always_inline_attribute, NULL },
  { "malloc",       0, 0,  true,  false, false, false,
    handle_malloc_attribute, NULL },
  { "type generic", 0, 0,  false, true,  true,  false,
    handle_type_generic_attribute, NULL },

  { "flatten",      0, 0,  true,  false, false, false,
    handle_flatten_attribute, NULL },
  { "used",         0, 0,  true,  false, false, false,
    handle_used_attribute, NULL },
  { "cold",         0, 0,  true,  false, false, false,
    handle_cold_attribute, attr_cold_hot_exclusions },
  { "hot",          0, 0,  true,  false, false, false,
    handle_hot_attribute, attr_cold_hot_exclusions },
  { "target",       1, -1, true,  false, false, false,
    handle_target_attribute, NULL },
  { "target_clones",1, -1, true,  false, false, false,
    handle_target_clones_attribute, NULL },

  { "vector_size",  1, 1,  false, true,  false, false,
    handle_vector_size_attribute, NULL },
  { "vector_type",  0, 0,  false, true,  false, false,
    handle_vector_type_attribute, NULL },
  { "may_alias",    0, 0,  false, true,  false, false,
    NULL, NULL },

  { "zero_call_used_regs", 1, 1, true, false, false, false,
    handle_zero_call_used_regs_attribute, NULL },

  /* ??? format and format_arg are heavy and not supported, which actually
     prevents support for stdio builtins, which we however declare as part
     of the common builtins.def contents.  */
  { "format",       3, 3,  false, true,  true,  false,
    fake_attribute_handler, NULL },
  { "format_arg",   1, 1,  false, true,  true,  false,
    fake_attribute_handler, NULL },

  { NULL,           0, 0,  false, false, false, false,
    NULL, NULL }
};

/* Associates a GNAT tree node to a GCC tree node. It is used in
   `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
   of `save_gnu_tree' for more info.  */
static GTY((length ("max_gnat_nodes"))) tree *associate_gnat_to_gnu;

#define GET_GNU_TREE(GNAT_ENTITY)	\
  associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id]

#define SET_GNU_TREE(GNAT_ENTITY,VAL)	\
  associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] = (VAL)

#define PRESENT_GNU_TREE(GNAT_ENTITY)	\
  (associate_gnat_to_gnu[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)

/* Associates a GNAT entity to a GCC tree node used as a dummy, if any.  */
static GTY((length ("max_gnat_nodes"))) tree *dummy_node_table;

#define GET_DUMMY_NODE(GNAT_ENTITY)	\
  dummy_node_table[(GNAT_ENTITY) - First_Node_Id]

#define SET_DUMMY_NODE(GNAT_ENTITY,VAL)	\
  dummy_node_table[(GNAT_ENTITY) - First_Node_Id] = (VAL)

#define PRESENT_DUMMY_NODE(GNAT_ENTITY)	\
  (dummy_node_table[(GNAT_ENTITY) - First_Node_Id] != NULL_TREE)

/* This variable keeps a table for types for each precision so that we only
   allocate each of them once. Signed and unsigned types are kept separate.

   Note that these types are only used when fold-const requests something
   special.  Perhaps we should NOT share these types; we'll see how it
   goes later.  */
static GTY(()) tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];

/* Likewise for float types, but record these by mode.  */
static GTY(()) tree float_types[NUM_MACHINE_MODES];

/* For each binding contour we allocate a binding_level structure to indicate
   the binding depth.  */

struct GTY((chain_next ("%h.chain"))) gnat_binding_level {
  /* The binding level containing this one (the enclosing binding level). */
  struct gnat_binding_level *chain;
  /* The BLOCK node for this level.  */
  tree block;
  /* If nonzero, the setjmp buffer that needs to be updated for any
     variable-sized definition within this context.  */
  tree jmpbuf_decl;
};

/* The binding level currently in effect.  */
static GTY(()) struct gnat_binding_level *current_binding_level;

/* A chain of gnat_binding_level structures awaiting reuse.  */
static GTY((deletable)) struct gnat_binding_level *free_binding_level;

/* The context to be used for global declarations.  */
static GTY(()) tree global_context;

/* An array of global declarations.  */
static GTY(()) vec<tree, va_gc> *global_decls;

/* An array of builtin function declarations.  */
static GTY(()) vec<tree, va_gc> *builtin_decls;

/* A chain of unused BLOCK nodes. */
static GTY((deletable)) tree free_block_chain;

/* A hash table of packable types.  It is modelled on the generic type
   hash table in tree.c, which must thus be used as a reference.  */

struct GTY((for_user)) packable_type_hash
{
  hashval_t hash;
  tree type;
};

struct packable_type_hasher : ggc_cache_ptr_hash<packable_type_hash>
{
  static inline hashval_t hash (packable_type_hash *t) { return t->hash; }
  static bool equal (packable_type_hash *a, packable_type_hash *b);

  static int
  keep_cache_entry (packable_type_hash *&t)
  {
    return ggc_marked_p (t->type);
  }
};

static GTY ((cache)) hash_table<packable_type_hasher> *packable_type_hash_table;

/* A hash table of padded types.  It is modelled on the generic type
   hash table in tree.c, which must thus be used as a reference.  */

struct GTY((for_user)) pad_type_hash
{
  hashval_t hash;
  tree type;
};

struct pad_type_hasher : ggc_cache_ptr_hash<pad_type_hash>
{
  static inline hashval_t hash (pad_type_hash *t) { return t->hash; }
  static bool equal (pad_type_hash *a, pad_type_hash *b);

  static int
  keep_cache_entry (pad_type_hash *&t)
  {
    return ggc_marked_p (t->type);
  }
};

static GTY ((cache)) hash_table<pad_type_hasher> *pad_type_hash_table;

static tree merge_sizes (tree, tree, tree, bool, bool);
static tree fold_bit_position (const_tree);
static tree compute_related_constant (tree, tree);
static tree split_plus (tree, tree *);
static tree float_type_for_precision (int, machine_mode);
static tree convert_to_fat_pointer (tree, tree);
static unsigned int scale_by_factor_of (tree, unsigned int);

/* Linked list used as a queue to defer the initialization of the DECL_CONTEXT
   of ..._DECL nodes and of the TYPE_CONTEXT of ..._TYPE nodes.  */
struct deferred_decl_context_node
{
  /* The ..._DECL node to work on.  */
  tree decl;

  /* The corresponding entity's Scope.  */
  Entity_Id gnat_scope;

  /* The value of force_global when DECL was pushed.  */
  int force_global;

  /* The list of ..._TYPE nodes to propagate the context to.  */
  vec<tree> types;

  /* The next queue item.  */
  struct deferred_decl_context_node *next;
};

static struct deferred_decl_context_node *deferred_decl_context_queue = NULL;

/* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
   feed it with the elaboration of GNAT_SCOPE.  */
static struct deferred_decl_context_node *
add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global);

/* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
   feed it with the DECL_CONTEXT computed as part of N as soon as it is
   computed.  */
static void add_deferred_type_context (struct deferred_decl_context_node *n,
				       tree type);

/* Initialize data structures of the utils.c module.  */

void
init_gnat_utils (void)
{
  /* Initialize the association of GNAT nodes to GCC trees.  */
  associate_gnat_to_gnu = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);

  /* Initialize the association of GNAT nodes to GCC trees as dummies.  */
  dummy_node_table = ggc_cleared_vec_alloc<tree> (max_gnat_nodes);

  /* Initialize the hash table of packable types.  */
  packable_type_hash_table = hash_table<packable_type_hasher>::create_ggc (512);

  /* Initialize the hash table of padded types.  */
  pad_type_hash_table = hash_table<pad_type_hasher>::create_ggc (512);
}

/* Destroy data structures of the utils.c module.  */

void
destroy_gnat_utils (void)
{
  /* Destroy the association of GNAT nodes to GCC trees.  */
  ggc_free (associate_gnat_to_gnu);
  associate_gnat_to_gnu = NULL;

  /* Destroy the association of GNAT nodes to GCC trees as dummies.  */
  ggc_free (dummy_node_table);
  dummy_node_table = NULL;

  /* Destroy the hash table of packable types.  */
  packable_type_hash_table->empty ();
  packable_type_hash_table = NULL;

  /* Destroy the hash table of padded types.  */
  pad_type_hash_table->empty ();
  pad_type_hash_table = NULL;
}

/* GNAT_ENTITY is a GNAT tree node for an entity.  Associate GNU_DECL, a GCC
   tree node, with GNAT_ENTITY.  If GNU_DECL is not a ..._DECL node, abort.
   If NO_CHECK is true, the latter check is suppressed.

   If GNU_DECL is zero, reset a previous association.  */

void
save_gnu_tree (Entity_Id gnat_entity, tree gnu_decl, bool no_check)
{
  /* Check that GNAT_ENTITY is not already defined and that it is being set
     to something which is a decl.  If that is not the case, this usually
     means GNAT_ENTITY is defined twice, but occasionally is due to some
     Gigi problem.  */
  gcc_assert (!(gnu_decl
		&& (PRESENT_GNU_TREE (gnat_entity)
		    || (!no_check && !DECL_P (gnu_decl)))));

  SET_GNU_TREE (gnat_entity, gnu_decl);
}

/* GNAT_ENTITY is a GNAT tree node for an entity.  Return the GCC tree node
   that was associated with it.  If there is no such tree node, abort.

   In some cases, such as delayed elaboration or expressions that need to
   be elaborated only once, GNAT_ENTITY is really not an entity.  */

tree
get_gnu_tree (Entity_Id gnat_entity)
{
  gcc_assert (PRESENT_GNU_TREE (gnat_entity));
  return GET_GNU_TREE (gnat_entity);
}

/* Return nonzero if a GCC tree has been associated with GNAT_ENTITY.  */

bool
present_gnu_tree (Entity_Id gnat_entity)
{
  return PRESENT_GNU_TREE (gnat_entity);
}

/* Make a dummy type corresponding to GNAT_TYPE.  */

tree
make_dummy_type (Entity_Id gnat_type)
{
  Entity_Id gnat_equiv = Gigi_Equivalent_Type (Underlying_Type (gnat_type));
  tree gnu_type, debug_type;

  /* If there was no equivalent type (can only happen when just annotating
     types) or underlying type, go back to the original type.  */
  if (No (gnat_equiv))
    gnat_equiv = gnat_type;

  /* If it there already a dummy type, use that one.  Else make one.  */
  if (PRESENT_DUMMY_NODE (gnat_equiv))
    return GET_DUMMY_NODE (gnat_equiv);

  /* If this is a record, make a RECORD_TYPE or UNION_TYPE; else make
     an ENUMERAL_TYPE.  */
  gnu_type = make_node (Is_Record_Type (gnat_equiv)
			? tree_code_for_record_type (gnat_equiv)
			: ENUMERAL_TYPE);
  TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
  TYPE_DUMMY_P (gnu_type) = 1;
  TYPE_STUB_DECL (gnu_type)
    = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type);
  if (Is_By_Reference_Type (gnat_equiv))
    TYPE_BY_REFERENCE_P (gnu_type) = 1;
  if (Has_Discriminants (gnat_equiv))
    decl_attributes (&gnu_type,
		     tree_cons (get_identifier ("may_alias"), NULL_TREE,
				NULL_TREE),
		     ATTR_FLAG_TYPE_IN_PLACE);

  SET_DUMMY_NODE (gnat_equiv, gnu_type);

  /* Create a debug type so that debuggers only see an unspecified type.  */
  if (Needs_Debug_Info (gnat_type))
    {
      debug_type = make_node (LANG_TYPE);
      TYPE_NAME (debug_type) = TYPE_NAME (gnu_type);
      TYPE_ARTIFICIAL (debug_type) = TYPE_ARTIFICIAL (gnu_type);
      SET_TYPE_DEBUG_TYPE (gnu_type, debug_type);
    }

  return gnu_type;
}

/* Return the dummy type that was made for GNAT_TYPE, if any.  */

tree
get_dummy_type (Entity_Id gnat_type)
{
  return GET_DUMMY_NODE (gnat_type);
}

/* Build dummy fat and thin pointer types whose designated type is specified
   by GNAT_DESIG_TYPE/GNU_DESIG_TYPE and attach them to the latter.  */

void
build_dummy_unc_pointer_types (Entity_Id gnat_desig_type, tree gnu_desig_type)
{
  tree gnu_template_type, gnu_ptr_template, gnu_array_type, gnu_ptr_array;
  tree gnu_fat_type, fields, gnu_object_type;

  gnu_template_type = make_node (RECORD_TYPE);
  TYPE_NAME (gnu_template_type) = create_concat_name (gnat_desig_type, "XUB");
  TYPE_DUMMY_P (gnu_template_type) = 1;
  gnu_ptr_template = build_pointer_type (gnu_template_type);

  gnu_array_type = make_node (ENUMERAL_TYPE);
  TYPE_NAME (gnu_array_type) = create_concat_name (gnat_desig_type, "XUA");
  TYPE_DUMMY_P (gnu_array_type) = 1;
  gnu_ptr_array = build_pointer_type (gnu_array_type);

  gnu_fat_type = make_node (RECORD_TYPE);
  /* Build a stub DECL to trigger the special processing for fat pointer types
     in gnat_pushdecl.  */
  TYPE_NAME (gnu_fat_type)
    = create_type_stub_decl (create_concat_name (gnat_desig_type, "XUP"),
			     gnu_fat_type);
  fields = create_field_decl (get_identifier ("P_ARRAY"), gnu_ptr_array,
			      gnu_fat_type, NULL_TREE, NULL_TREE, 0, 1);
  DECL_CHAIN (fields)
    = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
			 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 1);
  finish_fat_pointer_type (gnu_fat_type, fields);
  SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_desig_type);
  /* Suppress debug info until after the type is completed.  */
  TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 1;

  gnu_object_type = make_node (RECORD_TYPE);
  TYPE_NAME (gnu_object_type) = create_concat_name (gnat_desig_type, "XUT");
  TYPE_DUMMY_P (gnu_object_type) = 1;

  TYPE_POINTER_TO (gnu_desig_type) = gnu_fat_type;
  TYPE_REFERENCE_TO (gnu_desig_type) = gnu_fat_type;
  TYPE_OBJECT_RECORD_TYPE (gnu_desig_type) = gnu_object_type;
}

/* Return true if we are in the global binding level.  */

bool
global_bindings_p (void)
{
  return force_global || !current_function_decl;
}

/* Enter a new binding level.  */

void
gnat_pushlevel (void)
{
  struct gnat_binding_level *newlevel = NULL;

  /* Reuse a struct for this binding level, if there is one.  */
  if (free_binding_level)
    {
      newlevel = free_binding_level;
      free_binding_level = free_binding_level->chain;
    }
  else
    newlevel = ggc_alloc<gnat_binding_level> ();

  /* Use a free BLOCK, if any; otherwise, allocate one.  */
  if (free_block_chain)
    {
      newlevel->block = free_block_chain;
      free_block_chain = BLOCK_CHAIN (free_block_chain);
      BLOCK_CHAIN (newlevel->block) = NULL_TREE;
    }
  else
    newlevel->block = make_node (BLOCK);

  /* Point the BLOCK we just made to its parent.  */
  if (current_binding_level)
    BLOCK_SUPERCONTEXT (newlevel->block) = current_binding_level->block;

  BLOCK_VARS (newlevel->block) = NULL_TREE;
  BLOCK_SUBBLOCKS (newlevel->block) = NULL_TREE;
  TREE_USED (newlevel->block) = 1;

  /* Add this level to the front of the chain (stack) of active levels.  */
  newlevel->chain = current_binding_level;
  newlevel->jmpbuf_decl = NULL_TREE;
  current_binding_level = newlevel;
}

/* Set SUPERCONTEXT of the BLOCK for the current binding level to FNDECL
   and point FNDECL to this BLOCK.  */

void
set_current_block_context (tree fndecl)
{
  BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
  DECL_INITIAL (fndecl) = current_binding_level->block;
  set_block_for_group (current_binding_level->block);
}

/* Set the jmpbuf_decl for the current binding level to DECL.  */

void
set_block_jmpbuf_decl (tree decl)
{
  current_binding_level->jmpbuf_decl = decl;
}

/* Get the jmpbuf_decl, if any, for the current binding level.  */

tree
get_block_jmpbuf_decl (void)
{
  return current_binding_level->jmpbuf_decl;
}

/* Exit a binding level.  Set any BLOCK into the current code group.  */

void
gnat_poplevel (void)
{
  struct gnat_binding_level *level = current_binding_level;
  tree block = level->block;

  BLOCK_VARS (block) = nreverse (BLOCK_VARS (block));
  BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));

  /* If this is a function-level BLOCK don't do anything.  Otherwise, if there
     are no variables free the block and merge its subblocks into those of its
     parent block.  Otherwise, add it to the list of its parent.  */
  if (TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL)
    ;
  else if (!BLOCK_VARS (block))
    {
      BLOCK_SUBBLOCKS (level->chain->block)
	= block_chainon (BLOCK_SUBBLOCKS (block),
			 BLOCK_SUBBLOCKS (level->chain->block));
      BLOCK_CHAIN (block) = free_block_chain;
      free_block_chain = block;
    }
  else
    {
      BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (level->chain->block);
      BLOCK_SUBBLOCKS (level->chain->block) = block;
      TREE_USED (block) = 1;
      set_block_for_group (block);
    }

  /* Free this binding structure.  */
  current_binding_level = level->chain;
  level->chain = free_binding_level;
  free_binding_level = level;
}

/* Exit a binding level and discard the associated BLOCK.  */

void
gnat_zaplevel (void)
{
  struct gnat_binding_level *level = current_binding_level;
  tree block = level->block;

  BLOCK_CHAIN (block) = free_block_chain;
  free_block_chain = block;

  /* Free this binding structure.  */
  current_binding_level = level->chain;
  level->chain = free_binding_level;
  free_binding_level = level;
}

/* Set the context of TYPE and its parallel types (if any) to CONTEXT.  */

static void
gnat_set_type_context (tree type, tree context)
{
  tree decl = TYPE_STUB_DECL (type);

  TYPE_CONTEXT (type) = context;

  while (decl && DECL_PARALLEL_TYPE (decl))
    {
      tree parallel_type = DECL_PARALLEL_TYPE (decl);

      /* Give a context to the parallel types and their stub decl, if any.
	 Some parallel types seems to be present in multiple parallel type
	 chains, so don't mess with their context if they already have one.  */
      if (!TYPE_CONTEXT (parallel_type))
	{
	  if (TYPE_STUB_DECL (parallel_type))
	    DECL_CONTEXT (TYPE_STUB_DECL (parallel_type)) = context;
	  TYPE_CONTEXT (parallel_type) = context;
	}

      decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));
    }
}

/* Return the innermost scope, starting at GNAT_NODE, we are be interested in
   the debug info, or Empty if there is no such scope.  If not NULL, set
   IS_SUBPROGRAM to whether the returned entity is a subprogram.  */

Entity_Id
get_debug_scope (Node_Id gnat_node, bool *is_subprogram)
{
  Entity_Id gnat_entity;

  if (is_subprogram)
    *is_subprogram = false;

  if (Nkind (gnat_node) == N_Defining_Identifier
      || Nkind (gnat_node) == N_Defining_Operator_Symbol)
    gnat_entity = Scope (gnat_node);
  else
    return Empty;

  while (Present (gnat_entity))
    {
      switch (Ekind (gnat_entity))
	{
	case E_Function:
	case E_Procedure:
	  if (Present (Protected_Body_Subprogram (gnat_entity)))
	    gnat_entity = Protected_Body_Subprogram (gnat_entity);

	  /* If the scope is a subprogram, then just rely on
	     current_function_decl, so that we don't have to defer
	     anything.  This is needed because other places rely on the
	     validity of the DECL_CONTEXT attribute of FUNCTION_DECL nodes. */
	  if (is_subprogram)
	    *is_subprogram = true;
	  return gnat_entity;

	case E_Record_Type:
	case E_Record_Subtype:
	  return gnat_entity;

	default:
	  /* By default, we are not interested in this particular scope: go to
	     the outer one.  */
	  break;
	}

      gnat_entity = Scope (gnat_entity);
    }

  return Empty;
}

/* If N is NULL, set TYPE's context to CONTEXT.  Defer this to the processing
   of N otherwise.  */

static void
defer_or_set_type_context (tree type, tree context,
			   struct deferred_decl_context_node *n)
{
  if (n)
    add_deferred_type_context (n, type);
  else
    gnat_set_type_context (type, context);
}

/* Return global_context, but create it first if need be.  */

static tree
get_global_context (void)
{
  if (!global_context)
    {
      global_context
	= build_translation_unit_decl (get_identifier (main_input_filename));
      debug_hooks->register_main_translation_unit (global_context);
    }

  return global_context;
}

/* Record DECL as belonging to the current lexical scope and use GNAT_NODE
   for location information and flag propagation.  */

void
gnat_pushdecl (tree decl, Node_Id gnat_node)
{
  tree context = NULL_TREE;
  struct deferred_decl_context_node *deferred_decl_context = NULL;

  /* If explicitly asked to make DECL global or if it's an imported nested
     object, short-circuit the regular Scope-based context computation.  */
  if (!((TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) || force_global == 1))
    {
      /* Rely on the GNAT scope, or fallback to the current_function_decl if
	 the GNAT scope reached the global scope, if it reached a subprogram
	 or the declaration is a subprogram or a variable (for them we skip
	 intermediate context types because the subprogram body elaboration
	 machinery and the inliner both expect a subprogram context).

	 Falling back to current_function_decl is necessary for implicit
	 subprograms created by gigi, such as the elaboration subprograms.  */
      bool context_is_subprogram = false;
      const Entity_Id gnat_scope
        = get_debug_scope (gnat_node, &context_is_subprogram);

      if (Present (gnat_scope)
	  && !context_is_subprogram
	  && TREE_CODE (decl) != FUNCTION_DECL
	  && TREE_CODE (decl) != VAR_DECL)
	/* Always assume the scope has not been elaborated, thus defer the
	   context propagation to the time its elaboration will be
	   available.  */
	deferred_decl_context
	  = add_deferred_decl_context (decl, gnat_scope, force_global);

      /* External declarations (when force_global > 0) may not be in a
	 local context.  */
      else if (current_function_decl && force_global == 0)
	context = current_function_decl;
    }

  /* If either we are forced to be in global mode or if both the GNAT scope and
     the current_function_decl did not help in determining the context, use the
     global scope.  */
  if (!deferred_decl_context && !context)
    context = get_global_context ();

  /* Functions imported in another function are not really nested.
     For really nested functions mark them initially as needing
     a static chain for uses of that flag before unnesting;
     lower_nested_functions will then recompute it.  */
  if (TREE_CODE (decl) == FUNCTION_DECL
      && !TREE_PUBLIC (decl)
      && context
      && (TREE_CODE (context) == FUNCTION_DECL
	  || decl_function_context (context)))
    DECL_STATIC_CHAIN (decl) = 1;

  if (!deferred_decl_context)
    DECL_CONTEXT (decl) = context;

  suppress_warning (decl, all_warnings,
		    No (gnat_node) || Warnings_Off (gnat_node));

  /* Set the location of DECL and emit a declaration for it.  */
  if (Present (gnat_node) && !renaming_from_instantiation_p (gnat_node))
    Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (decl));

  add_decl_expr (decl, gnat_node);

  /* Put the declaration on the list.  The list of declarations is in reverse
     order.  The list will be reversed later.  Put global declarations in the
     globals list and local ones in the current block.  But skip TYPE_DECLs
     for UNCONSTRAINED_ARRAY_TYPE in both cases, as they will cause trouble
     with the debugger and aren't needed anyway.  */
  if (!(TREE_CODE (decl) == TYPE_DECL
        && TREE_CODE (TREE_TYPE (decl)) == UNCONSTRAINED_ARRAY_TYPE))
    {
      /* External declarations must go to the binding level they belong to.
	 This will make corresponding imported entities are available in the
	 debugger at the proper time.  */
      if (DECL_EXTERNAL (decl)
	  && TREE_CODE (decl) == FUNCTION_DECL
	  && fndecl_built_in_p (decl))
	vec_safe_push (builtin_decls, decl);
      else if (global_bindings_p ())
	vec_safe_push (global_decls, decl);
      else
	{
	  DECL_CHAIN (decl) = BLOCK_VARS (current_binding_level->block);
	  BLOCK_VARS (current_binding_level->block) = decl;
	}
    }

  /* For the declaration of a type, set its name either if it isn't already
     set or if the previous type name was not derived from a source name.
     We'd rather have the type named with a real name and all the pointer
     types to the same object have the same node, except when the names are
     both derived from source names.  */
  if (TREE_CODE (decl) == TYPE_DECL && DECL_NAME (decl))
    {
      tree t = TREE_TYPE (decl);

      /* Array and pointer types aren't tagged types in the C sense so we need
	 to generate a typedef in DWARF for them and make sure it is preserved,
	 unless the type is artificial.  */
      if (!(TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
	  && ((TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != POINTER_TYPE)
	      || DECL_ARTIFICIAL (decl)))
	;
      /* For array and pointer types, create the DECL_ORIGINAL_TYPE that will
	 generate the typedef in DWARF.  Also do that for fat pointer types
	 because, even though they are tagged types in the C sense, they are
	 still XUP types attached to the base array type at this point.  */
      else if (!DECL_ARTIFICIAL (decl)
	       && (TREE_CODE (t) == ARRAY_TYPE
		   || TREE_CODE (t) == POINTER_TYPE
		   || TYPE_IS_FAT_POINTER_P (t)))
	{
	  tree tt = build_variant_type_copy (t);
	  TYPE_NAME (tt) = decl;
	  defer_or_set_type_context (tt,
				     DECL_CONTEXT (decl),
				     deferred_decl_context);
	  TREE_TYPE (decl) = tt;
	  if (TYPE_NAME (t)
	      && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
	      && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
	    DECL_ORIGINAL_TYPE (decl) = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
	  else
	    DECL_ORIGINAL_TYPE (decl) = t;
	  /* Array types need to have a name so that they can be related to
	     their GNAT encodings.  */
	  if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NAME (t))
	    TYPE_NAME (t) = DECL_NAME (decl);
	  /* Remark the canonical fat pointer type as artificial.  */
	  if (TYPE_IS_FAT_POINTER_P (t))
	    TYPE_ARTIFICIAL (t) = 1;
	  t = NULL_TREE;
	}
      else if (TYPE_NAME (t)
	       && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
	       && DECL_ARTIFICIAL (TYPE_NAME (t)) && !DECL_ARTIFICIAL (decl))
	;
      else
	t = NULL_TREE;

      /* Propagate the name to all the variants, this is needed for the type
	 qualifiers machinery to work properly (see check_qualified_type).
	 Also propagate the context to them.  Note that it will be propagated
	 to all parallel types too thanks to gnat_set_type_context.  */
      if (t)
	for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
	  /* ??? Because of the previous kludge, we can have variants of fat
	     pointer types with different names.  */
	  if (!(TYPE_IS_FAT_POINTER_P (t)
		&& TYPE_NAME (t)
		&& TREE_CODE (TYPE_NAME (t)) == TYPE_DECL))
	    {
	      TYPE_NAME (t) = decl;
	      defer_or_set_type_context (t,
					 DECL_CONTEXT (decl),
					 deferred_decl_context);
	    }
    }
}

/* Create a record type that contains a SIZE bytes long field of TYPE with a
   starting bit position so that it is aligned to ALIGN bits, and leaving at
   least ROOM bytes free before the field.  BASE_ALIGN is the alignment the
   record is guaranteed to get.  GNAT_NODE is used for the position of the
   associated TYPE_DECL.  */

tree
make_aligning_type (tree type, unsigned int align, tree size,
		    unsigned int base_align, int room, Node_Id gnat_node)
{
  /* We will be crafting a record type with one field at a position set to be
     the next multiple of ALIGN past record'address + room bytes.  We use a
     record placeholder to express record'address.  */
  tree record_type = make_node (RECORD_TYPE);
  tree record = build0 (PLACEHOLDER_EXPR, record_type);

  tree record_addr_st
    = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));

  /* The diagram below summarizes the shape of what we manipulate:

                    <--------- pos ---------->
                {  +------------+-------------+-----------------+
      record  =>{  |############|     ...     | field (type)    |
                {  +------------+-------------+-----------------+
		   |<-- room -->|<- voffset ->|<---- size ----->|
		   o            o
		   |            |
		   record_addr  vblock_addr

     Every length is in sizetype bytes there, except "pos" which has to be
     set as a bit position in the GCC tree for the record.  */
  tree room_st = size_int (room);
  tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
  tree voffset_st, pos, field;

  tree name = TYPE_IDENTIFIER (type);

  name = concat_name (name, "ALIGN");
  TYPE_NAME (record_type) = name;

  /* Compute VOFFSET and then POS.  The next byte position multiple of some
     alignment after some address is obtained by "and"ing the alignment minus
     1 with the two's complement of the address.   */
  voffset_st = size_binop (BIT_AND_EXPR,
			   fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
			   size_int ((align / BITS_PER_UNIT) - 1));

  /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype.  */
  pos = size_binop (MULT_EXPR,
		    convert (bitsizetype,
			     size_binop (PLUS_EXPR, room_st, voffset_st)),
                    bitsize_unit_node);

  /* Craft the GCC record representation.  We exceptionally do everything
     manually here because 1) our generic circuitry is not quite ready to
     handle the complex position/size expressions we are setting up, 2) we
     have a strong simplifying factor at hand: we know the maximum possible
     value of voffset, and 3) we have to set/reset at least the sizes in
     accordance with this maximum value anyway, as we need them to convey
     what should be "alloc"ated for this type.

     Use -1 as the 'addressable' indication for the field to prevent the
     creation of a bitfield.  We don't need one, it would have damaging
     consequences on the alignment computation, and create_field_decl would
     make one without this special argument, for instance because of the
     complex position expression.  */
  field = create_field_decl (get_identifier ("F"), type, record_type, size,
			     pos, 1, -1);
  TYPE_FIELDS (record_type) = field;

  SET_TYPE_ALIGN (record_type, base_align);
  TYPE_USER_ALIGN (record_type) = 1;

  TYPE_SIZE (record_type)
    = size_binop (PLUS_EXPR,
                  size_binop (MULT_EXPR, convert (bitsizetype, size),
                              bitsize_unit_node),
		  bitsize_int (align + room * BITS_PER_UNIT));
  TYPE_SIZE_UNIT (record_type)
    = size_binop (PLUS_EXPR, size,
		  size_int (room + align / BITS_PER_UNIT));

  SET_TYPE_MODE (record_type, BLKmode);
  relate_alias_sets (record_type, type, ALIAS_SET_COPY);

  /* Declare it now since it will never be declared otherwise.  This is
     necessary to ensure that its subtrees are properly marked.  */
  create_type_decl (name, record_type, true, false, gnat_node);

  return record_type;
}

/* Return true iff the packable types are equivalent.  */

bool
packable_type_hasher::equal (packable_type_hash *t1, packable_type_hash *t2)
{
  tree type1, type2;

  if (t1->hash != t2->hash)
    return 0;

  type1 = t1->type;
  type2 = t2->type;

  /* We consider that packable types are equivalent if they have the same name,
     size, alignment, RM size and storage order. Taking the mode into account
     is redundant since it is determined by the others.  */
  return
    TYPE_NAME (type1) == TYPE_NAME (type2)
    && TYPE_SIZE (type1) == TYPE_SIZE (type2)
    && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
    && TYPE_ADA_SIZE (type1) == TYPE_ADA_SIZE (type2)
    && TYPE_REVERSE_STORAGE_ORDER (type1) == TYPE_REVERSE_STORAGE_ORDER (type2);
}

/* Compute the hash value for the packable TYPE.  */

static hashval_t
hash_packable_type (tree type)
{
  hashval_t hashcode;

  hashcode = iterative_hash_expr (TYPE_NAME (type), 0);
  hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
  hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
  hashcode = iterative_hash_expr (TYPE_ADA_SIZE (type), hashcode);
  hashcode
    = iterative_hash_hashval_t (TYPE_REVERSE_STORAGE_ORDER (type), hashcode);

  return hashcode;
}

/* Look up the packable TYPE in the hash table and return its canonical version
   if it exists; otherwise, insert it into the hash table.  */

static tree
canonicalize_packable_type (tree type)
{
  const hashval_t hashcode = hash_packable_type (type);
  struct packable_type_hash in, *h, **slot;

  in.hash = hashcode;
  in.type = type;
  slot = packable_type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
  h = *slot;
  if (!h)
    {
      h = ggc_alloc<packable_type_hash> ();
      h->hash = hashcode;
      h->type = type;
      *slot = h;
    }

  return h->type;
}

/* TYPE is an ARRAY_TYPE that is being used as the type of a field in a packed
   record.  See if we can rewrite it as a type that has non-BLKmode, which we
   can pack tighter in the packed record.  If so, return the new type; if not,
   return the original type.  */

static tree
make_packable_array_type (tree type)
{
  const unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type));
  unsigned HOST_WIDE_INT new_size;
  unsigned int new_align;

  /* No point in doing anything if the size is either zero or too large for an
     integral mode, or if the type already has non-BLKmode.  */
  if (size == 0 || size > MAX_FIXED_MODE_SIZE || TYPE_MODE (type) != BLKmode)
    return type;

  /* Punt if the component type is an aggregate type for now.  */
  if (AGGREGATE_TYPE_P (TREE_TYPE (type)))
    return type;

  tree new_type = copy_type (type);

  new_size = ceil_pow2 (size);
  new_align = MIN (new_size, BIGGEST_ALIGNMENT);
  SET_TYPE_ALIGN (new_type, new_align);

  TYPE_SIZE (new_type) = bitsize_int (new_size);
  TYPE_SIZE_UNIT (new_type) = size_int (new_size / BITS_PER_UNIT);

  SET_TYPE_MODE (new_type, mode_for_size (new_size, MODE_INT, 1).else_blk ());

  return new_type;
}

/* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
   as the type of a field in a packed record if IN_RECORD is true, or as
   the component type of a packed array if IN_RECORD is false.  See if we
   can rewrite it either as a type that has non-BLKmode, which we can pack
   tighter in the packed record case, or as a smaller type with at most
   MAX_ALIGN alignment if the value is non-zero.  If so, return the new
   type; if not, return the original type.  */

tree
make_packable_type (tree type, bool in_record, unsigned int max_align)
{
  const unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type));
  const unsigned int align = TYPE_ALIGN (type);
  unsigned HOST_WIDE_INT new_size;
  unsigned int new_align;

  /* No point in doing anything if the size is zero.  */
  if (size == 0)
    return type;

  tree new_type = make_node (TREE_CODE (type));

  /* Copy the name and flags from the old type to that of the new.
     Note that we rely on the pointer equality created here for
     TYPE_NAME to look through conversions in various places.  */
  TYPE_NAME (new_type) = TYPE_NAME (type);
  TYPE_PACKED (new_type) = 1;
  TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
  TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
  TYPE_REVERSE_STORAGE_ORDER (new_type) = TYPE_REVERSE_STORAGE_ORDER (type);
  if (TREE_CODE (type) == RECORD_TYPE)
    TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);

  /* If we are in a record and have a small size, set the alignment to
     try for an integral mode.  Otherwise set it to try for a smaller
     type with BLKmode.  */
  if (in_record && size <= MAX_FIXED_MODE_SIZE)
    {
      new_size = ceil_pow2 (size);
      new_align = MIN (new_size, BIGGEST_ALIGNMENT);
      SET_TYPE_ALIGN (new_type, new_align);
    }
  else
    {
      tree ada_size = TYPE_ADA_SIZE (type);

      /* Do not try to shrink the size if the RM size is not constant.  */
      if (TYPE_CONTAINS_TEMPLATE_P (type) || !tree_fits_uhwi_p (ada_size))
	return type;

      /* Round the RM size up to a unit boundary to get the minimal size
	 for a BLKmode record.  Give up if it's already the size and we
	 don't need to lower the alignment.  */
      new_size = tree_to_uhwi (ada_size);
      new_size = (new_size + BITS_PER_UNIT - 1) & -BITS_PER_UNIT;
      if (new_size == size && (max_align == 0 || align <= max_align))
	return type;

      new_align = MIN (new_size & -new_size, BIGGEST_ALIGNMENT);
      if (max_align > 0 && new_align > max_align)
	new_align = max_align;
      SET_TYPE_ALIGN (new_type, MIN (align, new_align));
    }

  TYPE_USER_ALIGN (new_type) = 1;

  /* Now copy the fields, keeping the position and size as we don't want
     to change the layout by propagating the packedness downwards.  */
  tree new_field_list = NULL_TREE;
  for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    {
      tree new_field_type = TREE_TYPE (field);
      tree new_field, new_field_size;

      if (AGGREGATE_TYPE_P (new_field_type)
	  && tree_fits_uhwi_p (TYPE_SIZE (new_field_type)))
	{
	  if (RECORD_OR_UNION_TYPE_P (new_field_type)
	      && !TYPE_FAT_POINTER_P (new_field_type))
	    new_field_type
	      = make_packable_type (new_field_type, true, max_align);
	  else if (in_record
		   && max_align > 0
		   && max_align < BITS_PER_UNIT
		   && TREE_CODE (new_field_type) == ARRAY_TYPE)
	    new_field_type = make_packable_array_type (new_field_type);
	}

      /* However, for the last field in a not already packed record type
	 that is of an aggregate type, we need to use the RM size in the
	 packable version of the record type, see finish_record_type.  */
      if (!DECL_CHAIN (field)
	  && !TYPE_PACKED (type)
	  && RECORD_OR_UNION_TYPE_P (new_field_type)
	  && !TYPE_FAT_POINTER_P (new_field_type)
	  && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
	  && TYPE_ADA_SIZE (new_field_type))
	new_field_size = TYPE_ADA_SIZE (new_field_type);
      else
	{
	  new_field_size = DECL_SIZE (field);

	  /* Make sure not to use too small a type for the size.  */
	  if (TYPE_MODE (new_field_type) == BLKmode)
	    new_field_type = TREE_TYPE (field);
	}

      /* This is a layout with full representation, alignment and size clauses
	 so we simply pass 0 as PACKED like gnat_to_gnu_field in this case.  */
      new_field
	= create_field_decl (DECL_NAME (field), new_field_type, new_type,
			     new_field_size, bit_position (field), 0,
			     !DECL_NONADDRESSABLE_P (field));

      DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (field);
      SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
      if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
	DECL_QUALIFIER (new_field) = DECL_QUALIFIER (field);

      DECL_CHAIN (new_field) = new_field_list;
      new_field_list = new_field;
    }

  /* If this is a padding record, we never want to make the size smaller
     than what was specified.  For QUAL_UNION_TYPE, also copy the size.  */
  if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
    {
      TYPE_SIZE (new_type) = TYPE_SIZE (type);
      TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
      new_size = size;
    }
  else
    {
      TYPE_SIZE (new_type) = bitsize_int (new_size);
      TYPE_SIZE_UNIT (new_type) = size_int (new_size / BITS_PER_UNIT);
    }

  if (!TYPE_CONTAINS_TEMPLATE_P (type))
    SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));

  finish_record_type (new_type, nreverse (new_field_list), 2, false);
  relate_alias_sets (new_type, type, ALIAS_SET_COPY);
  if (gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
    SET_TYPE_DEBUG_TYPE (new_type, TYPE_DEBUG_TYPE (type));
  else if (TYPE_STUB_DECL (type))
    SET_DECL_PARALLEL_TYPE (TYPE_STUB_DECL (new_type),
			    DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type)));

  /* Try harder to get a packable type if necessary, for example in case
     the record itself contains a BLKmode field.  */
  if (in_record && TYPE_MODE (new_type) == BLKmode)
    SET_TYPE_MODE (new_type,
		   mode_for_size_tree (TYPE_SIZE (new_type),
				       MODE_INT, 1).else_blk ());

  /* If neither mode nor size nor alignment shrunk, return the old type.  */
  if (TYPE_MODE (new_type) == BLKmode && new_size >= size && max_align == 0)
    return type;

  /* If the packable type is named, we canonicalize it by means of the hash
     table.  This is consistent with the language semantics and ensures that
     gigi and the middle-end have a common view of these packable types.  */
  return
    TYPE_NAME (new_type) ? canonicalize_packable_type (new_type) : new_type;
}

/* Return true if TYPE has an unsigned representation.  This needs to be used
   when the representation of types whose precision is not equal to their size
   is manipulated based on the RM size.  */

static inline bool
type_unsigned_for_rm (tree type)
{
  /* This is the common case.  */
  if (TYPE_UNSIGNED (type))
    return true;

  /* See the E_Signed_Integer_Subtype case of gnat_to_gnu_entity.  */
  if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
      && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
    return true;

  return false;
}

/* Given a type TYPE, return a new type whose size is appropriate for SIZE.
   If TYPE is the best type, return it.  Otherwise, make a new type.  We
   only support new integral and pointer types.  FOR_BIASED is true if
   we are making a biased type.  */

tree
make_type_from_size (tree type, tree size_tree, bool for_biased)
{
  unsigned HOST_WIDE_INT size;
  bool biased_p;
  tree new_type;

  /* If size indicates an error, just return TYPE to avoid propagating
     the error.  Likewise if it's too large to represent.  */
  if (!size_tree || !tree_fits_uhwi_p (size_tree))
    return type;

  size = tree_to_uhwi (size_tree);

  switch (TREE_CODE (type))
    {
    case BOOLEAN_TYPE:
      /* Do not mess with boolean types that have foreign convention.  */
      if (TYPE_PRECISION (type) == 1 && TYPE_SIZE (type) == size_tree)
	break;

      /* ... fall through ... */

    case INTEGER_TYPE:
    case ENUMERAL_TYPE:
      biased_p = (TREE_CODE (type) == INTEGER_TYPE
		  && TYPE_BIASED_REPRESENTATION_P (type));

      /* Integer types with precision 0 are forbidden.  */
      if (size == 0)
	size = 1;

      /* Only do something if the type is not a bit-packed array type and does
	 not already have the proper size and the size is not too large.  */
      if (BIT_PACKED_ARRAY_TYPE_P (type)
	  || (TYPE_PRECISION (type) == size && biased_p == for_biased)
	  || size > (Enable_128bit_Types ? 128 : LONG_LONG_TYPE_SIZE))
	break;

      biased_p |= for_biased;

      /* The type should be an unsigned type if the original type is unsigned
	 or if the lower bound is constant and non-negative or if the type is
	 biased, see E_Signed_Integer_Subtype case of gnat_to_gnu_entity.  */
      if (type_unsigned_for_rm (type) || biased_p)
	new_type = make_unsigned_type (size);
      else
	new_type = make_signed_type (size);
      TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
      SET_TYPE_RM_MIN_VALUE (new_type, TYPE_MIN_VALUE (type));
      SET_TYPE_RM_MAX_VALUE (new_type, TYPE_MAX_VALUE (type));
      /* Copy the name to show that it's essentially the same type and
	 not a subrange type.  */
      TYPE_NAME (new_type) = TYPE_NAME (type);
      TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
      SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
      return new_type;

    case RECORD_TYPE:
      /* Do something if this is a fat pointer, in which case we
	 may need to return the thin pointer.  */
      if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
	{
	  scalar_int_mode p_mode;
	  if (!int_mode_for_size (size, 0).exists (&p_mode)
	      || !targetm.valid_pointer_mode (p_mode))
	    p_mode = ptr_mode;
	  return
	    build_pointer_type_for_mode
	      (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
	       p_mode, 0);
	}
      break;

    case POINTER_TYPE:
      /* Only do something if this is a thin pointer, in which case we
	 may need to return the fat pointer.  */
      if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
	return
	  build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
      break;

    default:
      break;
    }

  return type;
}

/* Return true iff the padded types are equivalent.  */

bool
pad_type_hasher::equal (pad_type_hash *t1, pad_type_hash *t2)
{
  tree type1, type2;

  if (t1->hash != t2->hash)
    return 0;

  type1 = t1->type;
  type2 = t2->type;

  /* We consider that padded types are equivalent if they pad the same type
     and have the same size, alignment, RM size and storage order.  Taking the
     mode into account is redundant since it is determined by the others.  */
  return
    TREE_TYPE (TYPE_FIELDS (type1)) == TREE_TYPE (TYPE_FIELDS (type2))
    && TYPE_SIZE (type1) == TYPE_SIZE (type2)
    && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
    && TYPE_ADA_SIZE (type1) == TYPE_ADA_SIZE (type2)
    && TYPE_REVERSE_STORAGE_ORDER (type1) == TYPE_REVERSE_STORAGE_ORDER (type2);
}

/* Compute the hash value for the padded TYPE.  */

static hashval_t
hash_pad_type (tree type)
{
  hashval_t hashcode;

  hashcode
    = iterative_hash_object (TYPE_HASH (TREE_TYPE (TYPE_FIELDS (type))), 0);
  hashcode = iterative_hash_expr (TYPE_SIZE (type), hashcode);
  hashcode = iterative_hash_hashval_t (TYPE_ALIGN (type), hashcode);
  hashcode = iterative_hash_expr (TYPE_ADA_SIZE (type), hashcode);
  hashcode
    = iterative_hash_hashval_t (TYPE_REVERSE_STORAGE_ORDER (type), hashcode);

  return hashcode;
}

/* Look up the padded TYPE in the hash table and return its canonical version
   if it exists; otherwise, insert it into the hash table.  */

static tree
canonicalize_pad_type (tree type)
{
  const hashval_t hashcode = hash_pad_type (type);
  struct pad_type_hash in, *h, **slot;

  in.hash = hashcode;
  in.type = type;
  slot = pad_type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
  h = *slot;
  if (!h)
    {
      h = ggc_alloc<pad_type_hash> ();
      h->hash = hashcode;
      h->type = type;
      *slot = h;
    }

  return h->type;
}

/* Ensure that TYPE has SIZE and ALIGN.  Make and return a new padded type
   if needed.  We have already verified that SIZE and ALIGN are large enough.
   GNAT_ENTITY is used to name the resulting record and to issue a warning.
   IS_COMPONENT_TYPE is true if this is being done for the component type of
   an array.  DEFINITION is true if this type is being defined.  SET_RM_SIZE
   is true if the RM size of the resulting type is to be set to SIZE too; in
   this case, the padded type is canonicalized before being returned.  */

tree
maybe_pad_type (tree type, tree size, unsigned int align,
		Entity_Id gnat_entity, bool is_component_type,
		bool definition, bool set_rm_size)
{
  tree orig_size = TYPE_SIZE (type);
  unsigned int orig_align = TYPE_ALIGN (type);
  tree record, field;

  /* If TYPE is a padded type, see if it agrees with any size and alignment
     we were given.  If so, return the original type.  Otherwise, strip
     off the padding, since we will either be returning the inner type
     or repadding it.  If no size or alignment is specified, use that of
     the original padded type.  */
  if (TYPE_IS_PADDING_P (type))
    {
      if ((!size
	   || operand_equal_p (round_up (size, orig_align), orig_size, 0))
	  && (align == 0 || align == orig_align))
	return type;

      if (!size)
	size = orig_size;
      if (align == 0)
	align = orig_align;

      type = TREE_TYPE (TYPE_FIELDS (type));
      orig_size = TYPE_SIZE (type);
      orig_align = TYPE_ALIGN (type);
    }

  /* If the size is either not being changed or is being made smaller (which
     is not done here and is only valid for bitfields anyway), show the size
     isn't changing.  Likewise, clear the alignment if it isn't being
     changed.  Then return if we aren't doing anything.  */
  if (size
      && (operand_equal_p (size, orig_size, 0)
	  || (TREE_CODE (orig_size) == INTEGER_CST
	      && tree_int_cst_lt (size, orig_size))))
    size = NULL_TREE;

  if (align == orig_align)
    align = 0;

  if (align == 0 && !size)
    return type;

  /* We used to modify the record in place in some cases, but that could
     generate incorrect debugging information.  So make a new record
     type and name.  */
  record = make_node (RECORD_TYPE);
  TYPE_PADDING_P (record) = 1;

  if (Present (gnat_entity))
    TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");

  SET_TYPE_ALIGN (record, align ? align : orig_align);
  TYPE_SIZE (record) = size ? size : orig_size;
  TYPE_SIZE_UNIT (record)
    = convert (sizetype,
	       size_binop (EXACT_DIV_EXPR, TYPE_SIZE (record),
			   bitsize_unit_node));

  /* If we are changing the alignment and the input type is a record with
     BLKmode and a small constant size, try to make a form that has an
     integral mode.  This might allow the padding record to also have an
     integral mode, which will be much more efficient.  There is no point
     in doing so if a size is specified unless it is also a small constant
     size and it is incorrect to do so if we cannot guarantee that the mode
     will be naturally aligned since the field must always be addressable.

     ??? This might not always be a win when done for a stand-alone object:
     since the nominal and the effective type of the object will now have
     different modes, a VIEW_CONVERT_EXPR will be required for converting
     between them and it might be hard to overcome afterwards, including
     at the RTL level when the stand-alone object is accessed as a whole.  */
  if (align > 0
      && RECORD_OR_UNION_TYPE_P (type)
      && TYPE_MODE (type) == BLKmode
      && !TYPE_BY_REFERENCE_P (type)
      && TREE_CODE (orig_size) == INTEGER_CST
      && !TREE_OVERFLOW (orig_size)
      && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
      && (!size
	  || (TREE_CODE (size) == INTEGER_CST
	      && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
    {
      tree packable_type = make_packable_type (type, true, align);
      if (TYPE_MODE (packable_type) != BLKmode
	  && compare_tree_int (TYPE_SIZE (packable_type), align) <= 0)
        type = packable_type;
    }

  /* Now create the field with the original size.  */
  field = create_field_decl (get_identifier ("F"), type, record, orig_size,
			     bitsize_zero_node, 0, 1);
  DECL_INTERNAL_P (field) = 1;

  /* We will output additional debug info manually below.  */
  finish_record_type (record, field, 1, false);

  /* Set the RM size if requested.  */
  if (set_rm_size)
    {
      SET_TYPE_ADA_SIZE (record, size ? size : orig_size);

      /* If the padded type is complete and has constant size, we canonicalize
	 it by means of the hash table.  This is consistent with the language
	 semantics and ensures that gigi and the middle-end have a common view
	 of these padded types.  */
      if (TREE_CONSTANT (TYPE_SIZE (record)))
	{
	  tree canonical = canonicalize_pad_type (record);
	  if (canonical != record)
	    {
	      record = canonical;
	      goto built;
	    }
	}
    }

  /* Make the inner type the debug type of the padded type.  */
  if (gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
    SET_TYPE_DEBUG_TYPE (record, maybe_debug_type (type));

  /* Unless debugging information isn't being written for the input type,
     write a record that shows what we are a subtype of and also make a
     variable that indicates our size, if still variable.  */
  if (TREE_CODE (orig_size) != INTEGER_CST
      && TYPE_NAME (record)
      && TYPE_NAME (type)
      && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
	   && DECL_IGNORED_P (TYPE_NAME (type))))
    {
      tree name = TYPE_IDENTIFIER (record);
      tree size_unit = TYPE_SIZE_UNIT (record);

      /* A variable that holds the size is required even with no encoding since
	 it will be referenced by debugging information attributes.  At global
	 level, we need a single variable across all translation units.  */
      if (size
	  && TREE_CODE (size) != INTEGER_CST
	  && (definition || global_bindings_p ()))
	{
	  /* Whether or not gnat_entity comes from source, this XVZ variable is
	     is a compilation artifact.  */
	  size_unit
	    = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
			      size_unit, true, global_bindings_p (),
			      !definition && global_bindings_p (), false,
			      false, true, true, NULL, gnat_entity, false);
	  TYPE_SIZE_UNIT (record) = size_unit;
	}

      /* There is no need to show what we are a subtype of when outputting as
	 few encodings as possible: regular debugging infomation makes this
	 redundant.  */
      if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
	{
	  tree marker = make_node (RECORD_TYPE);
	  tree orig_name = TYPE_IDENTIFIER (type);

	  TYPE_NAME (marker) = concat_name (name, "XVS");
	  finish_record_type (marker,
			      create_field_decl (orig_name,
						 build_reference_type (type),
						 marker, NULL_TREE, NULL_TREE,
						 0, 0),
			      0, true);
	  TYPE_SIZE_UNIT (marker) = size_unit;

	  add_parallel_type (record, marker);
	}
    }

built:
  /* If a simple size was explicitly given, maybe issue a warning.  */
  if (!size
      || TREE_CODE (size) == COND_EXPR
      || TREE_CODE (size) == MAX_EXPR
      || No (gnat_entity))
    return record;

  /* But don't do it if we are just annotating types and the type is tagged or
     concurrent, since these types aren't fully laid out in this mode.  */
  if (type_annotate_only)
    {
      Entity_Id gnat_type
	= is_component_type
	  ? Component_Type (gnat_entity) : Etype (gnat_entity);

      if (Is_Tagged_Type (gnat_type) || Is_Concurrent_Type (gnat_type))
	return record;
    }

  /* Take the original size as the maximum size of the input if there was an
     unconstrained record involved and round it up to the specified alignment,
     if one was specified, but only for aggregate types.  */
  if (CONTAINS_PLACEHOLDER_P (orig_size))
    orig_size = max_size (orig_size, true);

  if (align && AGGREGATE_TYPE_P (type))
    orig_size = round_up (orig_size, align);

  if (!operand_equal_p (size, orig_size, 0)
      && !(TREE_CODE (size) == INTEGER_CST
	   && TREE_CODE (orig_size) == INTEGER_CST
	   && (TREE_OVERFLOW (size)
	       || TREE_OVERFLOW (orig_size)
	       || tree_int_cst_lt (size, orig_size))))
    {
      Node_Id gnat_error_node;

      /* For a packed array, post the message on the original array type.  */
      if (Is_Packed_Array_Impl_Type (gnat_entity))
	gnat_entity = Original_Array_Type (gnat_entity);

      if ((Ekind (gnat_entity) == E_Component
	   || Ekind (gnat_entity) == E_Discriminant)
	  && Present (Component_Clause (gnat_entity)))
	gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
      else if (Has_Size_Clause (gnat_entity))
	gnat_error_node = Expression (Size_Clause (gnat_entity));
      else if (Has_Object_Size_Clause (gnat_entity))
	gnat_error_node = Expression (Object_Size_Clause (gnat_entity));
      else
	gnat_error_node = Empty;

      /* Generate message only for entities that come from source, since
	 if we have an entity created by expansion, the message will be
	 generated for some other corresponding source entity.  */
      if (Comes_From_Source (gnat_entity))
	{
	  if (is_component_type)
	    post_error_ne_tree ("component of& padded{ by ^ bits}??",
				gnat_entity, gnat_entity,
				size_diffop (size, orig_size));
	  else if (Present (gnat_error_node))
	    post_error_ne_tree ("{^ }bits of & unused??",
				gnat_error_node, gnat_entity,
				size_diffop (size, orig_size));
	}
    }

  return record;
}

/* Return true if padded TYPE was built with an RM size.  */

bool
pad_type_has_rm_size (tree type)
{
  /* This is required for the lookup.  */
  if (!TREE_CONSTANT (TYPE_SIZE (type)))
    return false;

  const hashval_t hashcode = hash_pad_type (type);
  struct pad_type_hash in, *h;

  in.hash = hashcode;
  in.type = type;
  h = pad_type_hash_table->find_with_hash (&in, hashcode);

  /* The types built with an RM size are the canonicalized ones.  */
  return h && h->type == type;
}

/* Return a copy of the padded TYPE but with reverse storage order.  */

tree
set_reverse_storage_order_on_pad_type (tree type)
{
  if (flag_checking)
    {
      /* If the inner type is not scalar then the function does nothing.  */
      tree inner_type = TREE_TYPE (TYPE_FIELDS (type));
      gcc_assert (!AGGREGATE_TYPE_P (inner_type)
		  && !VECTOR_TYPE_P (inner_type));
    }

  /* This is required for the canonicalization.  */
  gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));

  tree field = copy_node (TYPE_FIELDS (type));
  type = copy_type (type);
  DECL_CONTEXT (field) = type;
  TYPE_FIELDS (type) = field;
  TYPE_REVERSE_STORAGE_ORDER (type) = 1;
  return canonicalize_pad_type (type);
}

/* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
   If this is a multi-dimensional array type, do this recursively.

   OP may be
   - ALIAS_SET_COPY:     the new set is made a copy of the old one.
   - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
   - ALIAS_SET_SUBSET:   the new set is made a subset of the old one.  */

void
relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
{
  /* Remove any padding from GNU_OLD_TYPE.  It doesn't matter in the case
     of a one-dimensional array, since the padding has the same alias set
     as the field type, but if it's a multi-dimensional array, we need to
     see the inner types.  */
  while (TREE_CODE (gnu_old_type) == RECORD_TYPE
	 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
	     || TYPE_PADDING_P (gnu_old_type)))
    gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));

  /* Unconstrained array types are deemed incomplete and would thus be given
     alias set 0.  Retrieve the underlying array type.  */
  if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
    gnu_old_type
      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
  if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
    gnu_new_type
      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));

  if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
      && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
      && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
    relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);

  switch (op)
    {
    case ALIAS_SET_COPY:
      /* The alias set shouldn't be copied between array types with different
	 aliasing settings because this can break the aliasing relationship
	 between the array type and its element type.  */
      if (flag_checking || flag_strict_aliasing)
	gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
		      && TREE_CODE (gnu_old_type) == ARRAY_TYPE
		      && TYPE_NONALIASED_COMPONENT (gnu_new_type)
			 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));

      TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
      break;

    case ALIAS_SET_SUBSET:
    case ALIAS_SET_SUPERSET:
      {
	alias_set_type old_set = get_alias_set (gnu_old_type);
	alias_set_type new_set = get_alias_set (gnu_new_type);

	/* Do nothing if the alias sets conflict.  This ensures that we
	   never call record_alias_subset several times for the same pair
	   or at all for alias set 0.  */
	if (!alias_sets_conflict_p (old_set, new_set))
	  {
	    if (op == ALIAS_SET_SUBSET)
	      record_alias_subset (old_set, new_set);
	    else
	      record_alias_subset (new_set, old_set);
	  }
      }
      break;

    default:
      gcc_unreachable ();
    }

  record_component_aliases (gnu_new_type);
}

/* Record TYPE as a builtin type for Ada.  NAME is the name of the type.
   ARTIFICIAL_P is true if the type was generated by the compiler.  */

void
record_builtin_type (const char *name, tree type, bool artificial_p)
{
  tree type_decl = build_decl (input_location,
			       TYPE_DECL, get_identifier (name), type);
  DECL_ARTIFICIAL (type_decl) = artificial_p;
  TYPE_ARTIFICIAL (type) = artificial_p;
  gnat_pushdecl (type_decl, Empty);

  if (debug_hooks->type_decl)
    debug_hooks->type_decl (type_decl, false);
}

/* Finish constructing the character type CHAR_TYPE.

  In Ada character types are enumeration types and, as a consequence, are
  represented in the front-end by integral types holding the positions of
  the enumeration values as defined by the language, which means that the
  integral types are unsigned.

  Unfortunately the signedness of 'char' in C is implementation-defined
  and GCC even has the option -f[un]signed-char to toggle it at run time.
  Since GNAT's philosophy is to be compatible with C by default, to wit
  Interfaces.C.char is defined as a mere copy of Character, we may need
  to declare character types as signed types in GENERIC and generate the
  necessary adjustments to make them behave as unsigned types.

  The overall strategy is as follows: if 'char' is unsigned, do nothing;
  if 'char' is signed, translate character types of CHAR_TYPE_SIZE and
  character subtypes with RM_Size = Esize = CHAR_TYPE_SIZE into signed
  types.  The idea is to ensure that the bit pattern contained in the
  Esize'd objects is not changed, even though the numerical value will
  be interpreted differently depending on the signedness.  */

void
finish_character_type (tree char_type)
{
  if (TYPE_UNSIGNED (char_type))
    return;

  /* Make a copy of a generic unsigned version since we'll modify it.  */
  tree unsigned_char_type
    = (char_type == char_type_node
       ? unsigned_char_type_node
       : copy_type (gnat_unsigned_type_for (char_type)));

  /* Create an unsigned version of the type and set it as debug type.  */
  TYPE_NAME (unsigned_char_type) = TYPE_NAME (char_type);
  TYPE_STRING_FLAG (unsigned_char_type) = TYPE_STRING_FLAG (char_type);
  TYPE_ARTIFICIAL (unsigned_char_type) = TYPE_ARTIFICIAL (char_type);
  SET_TYPE_DEBUG_TYPE (char_type, unsigned_char_type);

  /* If this is a subtype, make the debug type a subtype of the debug type
     of the base type and convert literal RM bounds to unsigned.  */
  if (TREE_TYPE (char_type))
    {
      tree base_unsigned_char_type = TYPE_DEBUG_TYPE (TREE_TYPE (char_type));
      tree min_value = TYPE_RM_MIN_VALUE (char_type);
      tree max_value = TYPE_RM_MAX_VALUE (char_type);

      if (TREE_CODE (min_value) == INTEGER_CST)
	min_value = fold_convert (base_unsigned_char_type, min_value);
      if (TREE_CODE (max_value) == INTEGER_CST)
	max_value = fold_convert (base_unsigned_char_type, max_value);

      TREE_TYPE (unsigned_char_type) = base_unsigned_char_type;
      SET_TYPE_RM_MIN_VALUE (unsigned_char_type, min_value);
      SET_TYPE_RM_MAX_VALUE (unsigned_char_type, max_value);
    }

  /* Adjust the RM bounds of the original type to unsigned; that's especially
     important for types since they are implicit in this case.  */
  SET_TYPE_RM_MIN_VALUE (char_type, TYPE_MIN_VALUE (unsigned_char_type));
  SET_TYPE_RM_MAX_VALUE (char_type, TYPE_MAX_VALUE (unsigned_char_type));
}

/* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
   finish constructing the record type as a fat pointer type.  */

void
finish_fat_pointer_type (tree record_type, tree field_list)
{
  /* Make sure we can put it into a register.  */
  if (STRICT_ALIGNMENT)
    SET_TYPE_ALIGN (record_type, MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE));

  /* Show what it really is.  */
  TYPE_FAT_POINTER_P (record_type) = 1;

  /* Do not emit debug info for it since the types of its fields may still be
     incomplete at this point.  */
  finish_record_type (record_type, field_list, 0, false);

  /* Force type_contains_placeholder_p to return true on it.  Although the
     PLACEHOLDER_EXPRs are referenced only indirectly, this isn't a pointer
     type but the representation of the unconstrained array.  */
  TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2;
}

/* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
   finish constructing the record or union type.  If REP_LEVEL is zero, this
   record has no representation clause and so will be entirely laid out here.
   If REP_LEVEL is one, this record has a representation clause and has been
   laid out already; only set the sizes and alignment.  If REP_LEVEL is two,
   this record is derived from a parent record and thus inherits its layout;
   only make a pass on the fields to finalize them.  DEBUG_INFO_P is true if
   additional debug info needs to be output for this type.  */

void
finish_record_type (tree record_type, tree field_list, int rep_level,
		    bool debug_info_p)
{
  const enum tree_code orig_code = TREE_CODE (record_type);
  const bool had_size = TYPE_SIZE (record_type) != NULL_TREE;
  const bool had_align = TYPE_ALIGN (record_type) > 0;
  /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
     out just like a UNION_TYPE, since the size will be fixed.  */
  const enum tree_code code
    = (orig_code == QUAL_UNION_TYPE && rep_level > 0 && had_size
       ? UNION_TYPE : orig_code);
  tree name = TYPE_IDENTIFIER (record_type);
  tree ada_size = bitsize_zero_node;
  tree size = bitsize_zero_node;
  tree field;

  TYPE_FIELDS (record_type) = field_list;

  /* Always attach the TYPE_STUB_DECL for a record type.  It is required to
     generate debug info and have a parallel type.  */
  TYPE_STUB_DECL (record_type) = create_type_stub_decl (name, record_type);

  /* Globally initialize the record first.  If this is a rep'ed record,
     that just means some initializations; otherwise, layout the record.  */
  if (rep_level > 0)
    {
      if (TYPE_ALIGN (record_type) < BITS_PER_UNIT)
	SET_TYPE_ALIGN (record_type, BITS_PER_UNIT);

      if (!had_size)
	TYPE_SIZE (record_type) = bitsize_zero_node;
    }
  else
    {
      /* Ensure there isn't a size already set.  There can be in an error
	 case where there is a rep clause but all fields have errors and
	 no longer have a position.  */
      TYPE_SIZE (record_type) = NULL_TREE;

      /* Ensure we use the traditional GCC layout for bitfields when we need
	 to pack the record type or have a representation clause.  The other
	 possible layout (Microsoft C compiler), if available, would prevent
	 efficient packing in almost all cases.  */
#ifdef TARGET_MS_BITFIELD_LAYOUT
      if (TARGET_MS_BITFIELD_LAYOUT && TYPE_PACKED (record_type))
	decl_attributes (&record_type,
			 tree_cons (get_identifier ("gcc_struct"),
				    NULL_TREE, NULL_TREE),
			 ATTR_FLAG_TYPE_IN_PLACE);
#endif

      layout_type (record_type);
    }

  /* At this point, the position and size of each field is known.  It was
     either set before entry by a rep clause, or by laying out the type above.

     We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
     to compute the Ada size; the GCC size and alignment (for rep'ed records
     that are not padding types); and the mode (for rep'ed records).  We also
     clear the DECL_BIT_FIELD indication for the cases we know have not been
     handled yet, and adjust DECL_NONADDRESSABLE_P accordingly.  */

  if (code == QUAL_UNION_TYPE)
    field_list = nreverse (field_list);

  for (field = field_list; field; field = DECL_CHAIN (field))
    {
      tree type = TREE_TYPE (field);
      tree pos = bit_position (field);
      tree this_size = DECL_SIZE (field);
      tree this_ada_size;

      if (RECORD_OR_UNION_TYPE_P (type)
	  && !TYPE_FAT_POINTER_P (type)
	  && !TYPE_CONTAINS_TEMPLATE_P (type)
	  && TYPE_ADA_SIZE (type))
	this_ada_size = TYPE_ADA_SIZE (type);
      else
	this_ada_size = this_size;

      const bool variant_part = (TREE_CODE (type) == QUAL_UNION_TYPE);

      /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle.  */
      if (DECL_BIT_FIELD (field)
	  && operand_equal_p (this_size, TYPE_SIZE (type), 0))
	{
	  const unsigned int align = TYPE_ALIGN (type);

	  /* In the general case, type alignment is required.  */
	  if (value_factor_p (pos, align))
	    {
	      /* The enclosing record type must be sufficiently aligned.
		 Otherwise, if no alignment was specified for it and it
		 has been laid out already, bump its alignment to the
		 desired one if this is compatible with its size and
		 maximum alignment, if any.  */
	      if (TYPE_ALIGN (record_type) >= align)
		{
		  SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
		  DECL_BIT_FIELD (field) = 0;
		}
	      else if (!had_align
		       && rep_level == 0
		       && value_factor_p (TYPE_SIZE (record_type), align)
		       && (!TYPE_MAX_ALIGN (record_type)
			   || TYPE_MAX_ALIGN (record_type) >= align))
		{
		  SET_TYPE_ALIGN (record_type, align);
		  SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align));
		  DECL_BIT_FIELD (field) = 0;
		}
	    }

	  /* In the non-strict alignment case, only byte alignment is.  */
	  if (!STRICT_ALIGNMENT
	      && DECL_BIT_FIELD (field)
	      && value_factor_p (pos, BITS_PER_UNIT))
	    DECL_BIT_FIELD (field) = 0;
	}

      /* Clear DECL_BIT_FIELD_TYPE for a variant part at offset 0, it's simply
	 not supported by the DECL_BIT_FIELD_REPRESENTATIVE machinery because
	 the variant part is always the last field in the list.  */
      if (variant_part && integer_zerop (pos))
	DECL_BIT_FIELD_TYPE (field) = NULL_TREE;

      /* If we still have DECL_BIT_FIELD set at this point, we know that the
	 field is technically not addressable.  Except that it can actually
	 be addressed if it is BLKmode and happens to be properly aligned.  */
      if (DECL_BIT_FIELD (field)
	  && !(DECL_MODE (field) == BLKmode
	       && value_factor_p (pos, BITS_PER_UNIT)))
	DECL_NONADDRESSABLE_P (field) = 1;

      /* A type must be as aligned as its most aligned field that is not
	 a bit-field.  But this is already enforced by layout_type.  */
      if (rep_level > 0 && !DECL_BIT_FIELD (field))
	SET_TYPE_ALIGN (record_type,
			MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field)));

      switch (code)
	{
	case UNION_TYPE:
	  ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
	  size = size_binop (MAX_EXPR, size, this_size);
	  break;

	case QUAL_UNION_TYPE:
	  ada_size
	    = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
			   this_ada_size, ada_size);
	  size = fold_build3 (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
			      this_size, size);
	  break;

	case RECORD_TYPE:
	  /* Since we know here that all fields are sorted in order of
	     increasing bit position, the size of the record is one
	     higher than the ending bit of the last field processed
	     unless we have a rep clause, because we might be processing
	     the REP part of a record with a variant part for which the
	     variant part has a rep clause but not the fixed part, in
	     which case this REP part may contain overlapping fields
	     and thus needs to be treated like a union tyoe above, so
	     use a MAX in that case.  Also, if this field is a variant
	     part, we need to take into account the previous size in
	     the case of empty variants.  */
	  ada_size
	    = merge_sizes (ada_size, pos, this_ada_size, rep_level > 0,
			   variant_part);
	  size
	    = merge_sizes (size, pos, this_size, rep_level > 0, variant_part);
	  break;

	default:
	  gcc_unreachable ();
	}
    }

  if (code == QUAL_UNION_TYPE)
    nreverse (field_list);

  /* We need to set the regular sizes if REP_LEVEL is one.  */
  if (rep_level == 1)
    {
      /* We round TYPE_SIZE and TYPE_SIZE_UNIT up to TYPE_ALIGN separately
	 to avoid having very large masking constants in TYPE_SIZE_UNIT.  */
      const unsigned int align = TYPE_ALIGN (record_type);

      /* If this is a padding record, we never want to make the size smaller
	 than what was specified in it, if any.  */
      if (TYPE_IS_PADDING_P (record_type) && had_size)
	size = TYPE_SIZE (record_type);
      else
	size = round_up (size, BITS_PER_UNIT);

      TYPE_SIZE (record_type) = variable_size (round_up (size, align));

      tree size_unit
	= convert (sizetype,
		   size_binop (EXACT_DIV_EXPR, size, bitsize_unit_node));
      TYPE_SIZE_UNIT (record_type)
	= variable_size (round_up (size_unit, align / BITS_PER_UNIT));
    }

  /* We need to set the Ada size if REP_LEVEL is zero or one.  */
  if (rep_level < 2)
    {
      /* Now set any of the values we've just computed that apply.  */
      if (!TYPE_FAT_POINTER_P (record_type)
	  && !TYPE_CONTAINS_TEMPLATE_P (record_type))
	SET_TYPE_ADA_SIZE (record_type, ada_size);
    }

  /* We need to set the mode if REP_LEVEL is one or two.  */
  if (rep_level > 0)
    {
      compute_record_mode (record_type);
      finish_bitfield_layout (record_type);
    }

  /* Reset the TYPE_MAX_ALIGN field since it's private to gigi.  */
  TYPE_MAX_ALIGN (record_type) = 0;

  if (debug_info_p)
    rest_of_record_type_compilation (record_type);
}

/* Append PARALLEL_TYPE on the chain of parallel types of TYPE.  If
   PARRALEL_TYPE has no context and its computation is not deferred yet, also
   propagate TYPE's context to PARALLEL_TYPE's or defer its propagation to the
   moment TYPE will get a context.  */

void
add_parallel_type (tree type, tree parallel_type)
{
  tree decl = TYPE_STUB_DECL (type);

  while (DECL_PARALLEL_TYPE (decl))
    decl = TYPE_STUB_DECL (DECL_PARALLEL_TYPE (decl));

  SET_DECL_PARALLEL_TYPE (decl, parallel_type);

  /* If PARALLEL_TYPE already has a context, we are done.  */
  if (TYPE_CONTEXT (parallel_type))
    return;

  /* Otherwise, try to get one from TYPE's context.  If so, simply propagate
     it to PARALLEL_TYPE.  */
  if (TYPE_CONTEXT (type))
    gnat_set_type_context (parallel_type, TYPE_CONTEXT (type));

  /* Otherwise TYPE has not context yet.  We know it will have one thanks to
     gnat_pushdecl and then its context will be propagated to PARALLEL_TYPE,
     so we have nothing to do in this case.  */
}

/* Return true if TYPE has a parallel type.  */

static bool
has_parallel_type (tree type)
{
  tree decl = TYPE_STUB_DECL (type);

  return DECL_PARALLEL_TYPE (decl) != NULL_TREE;
}

/* Wrap up compilation of RECORD_TYPE, i.e. output additional debug info
   associated with it.  It need not be invoked directly in most cases as
   finish_record_type takes care of doing so.  */

void
rest_of_record_type_compilation (tree record_type)
{
  bool var_size = false;
  tree field;

  /* If this is a padded type, the bulk of the debug info has already been
     generated for the field's type.  */
  if (TYPE_IS_PADDING_P (record_type))
    return;

  /* If the type already has a parallel type (XVS type), then we're done.  */
  if (has_parallel_type (record_type))
    return;

  for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
    {
      /* We need to make an XVE/XVU record if any field has variable size,
	 whether or not the record does.  For example, if we have a union,
	 it may be that all fields, rounded up to the alignment, have the
	 same size, in which case we'll use that size.  But the debug
	 output routines (except Dwarf2) won't be able to output the fields,
	 so we need to make the special record.  */
      if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
	  /* If a field has a non-constant qualifier, the record will have
	     variable size too.  */
	  || (TREE_CODE (record_type) == QUAL_UNION_TYPE
	      && TREE_CODE (DECL_QUALIFIER (field)) != INTEGER_CST))
	{
	  var_size = true;
	  break;
	}
    }

  /* If this record type is of variable size, make a parallel record type that
     will tell the debugger how the former is laid out (see exp_dbug.ads).  */
  if (var_size && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
    {
      tree new_record_type
	= make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
		     ? UNION_TYPE : TREE_CODE (record_type));
      tree orig_name = TYPE_IDENTIFIER (record_type), new_name;
      tree last_pos = bitsize_zero_node;

      new_name
	= concat_name (orig_name, TREE_CODE (record_type) == QUAL_UNION_TYPE
				  ? "XVU" : "XVE");
      TYPE_NAME (new_record_type) = new_name;
      SET_TYPE_ALIGN (new_record_type, BIGGEST_ALIGNMENT);
      TYPE_STUB_DECL (new_record_type)
	= create_type_stub_decl (new_name, new_record_type);
      DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
	= DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
      gnat_pushdecl (TYPE_STUB_DECL (new_record_type), Empty);
      TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
      TYPE_SIZE_UNIT (new_record_type)
	= size_int (TYPE_ALIGN (record_type) / BITS_PER_UNIT);

      /* Now scan all the fields, replacing each field with a new field
	 corresponding to the new encoding.  */
      for (tree old_field = TYPE_FIELDS (record_type);
	   old_field;
	   old_field = DECL_CHAIN (old_field))
	{
	  tree field_type = TREE_TYPE (old_field);
	  tree field_name = DECL_NAME (old_field);
	  tree curpos = fold_bit_position (old_field);
	  tree pos, new_field;
	  bool var = false;
	  unsigned int align = 0;

	  /* See how the position was modified from the last position.

	     There are two basic cases we support: a value was added
	     to the last position or the last position was rounded to
	     a boundary and they something was added.  Check for the
	     first case first.  If not, see if there is any evidence
	     of rounding.  If so, round the last position and retry.

	     If this is a union, the position can be taken as zero.  */
	  if (TREE_CODE (new_record_type) == UNION_TYPE)
	    pos = bitsize_zero_node;
	  else
	    pos = compute_related_constant (curpos, last_pos);

	  if (pos)
	    ;
	  else if (TREE_CODE (curpos) == MULT_EXPR
		   && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1)))
	    {
	      tree offset = TREE_OPERAND (curpos, 0);
	      align = tree_to_uhwi (TREE_OPERAND (curpos, 1));
	      align = scale_by_factor_of (offset, align);
	      last_pos = round_up (last_pos, align);
	      pos = compute_related_constant (curpos, last_pos);
	    }
	  else if (TREE_CODE (curpos) == PLUS_EXPR
		   && tree_fits_uhwi_p (TREE_OPERAND (curpos, 1))
		   && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
		   && tree_fits_uhwi_p
		      (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1)))
	    {
	      tree offset = TREE_OPERAND (TREE_OPERAND (curpos, 0), 0);
	      unsigned HOST_WIDE_INT addend
	        = tree_to_uhwi (TREE_OPERAND (curpos, 1));
	      align
		= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1));
	      align = scale_by_factor_of (offset, align);
	      align = MIN (align, addend & -addend);
	      last_pos = round_up (last_pos, align);
	      pos = compute_related_constant (curpos, last_pos);
	    }
	  else
	    {
	      align = DECL_ALIGN (old_field);
	      last_pos = round_up (last_pos, align);
	      pos = compute_related_constant (curpos, last_pos);
	    }

	  /* See if this type is variable-sized and make a pointer type
	     and indicate the indirection if so.  Beware that the debug
	     back-end may adjust the position computed above according
	     to the alignment of the field type, i.e. the pointer type
	     in this case, if we don't preventively counter that.  */
	  if (TREE_CODE (DECL_SIZE (old_field)) != INTEGER_CST)
	    {
	      field_type = copy_type (build_pointer_type (field_type));
	      SET_TYPE_ALIGN (field_type, BITS_PER_UNIT);
	      var = true;

	      /* ??? Kludge to work around a bug in Workbench's debugger.  */
	      if (align == 0)
		{
		  align = DECL_ALIGN (old_field);
		  last_pos = round_up (last_pos, align);
		  pos = compute_related_constant (curpos, last_pos);
		}
	    }

	  /* If we can't compute a position, set it to zero.

	     ??? We really should abort here, but it's too much work
	     to get this correct for all cases.  */
	  if (!pos)
	    pos = bitsize_zero_node;

	  /* Make a new field name, if necessary.  */
	  if (var || align != 0)
	    {
	      char suffix[16];

	      if (align != 0)
		sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
			 align / BITS_PER_UNIT);
	      else
		strcpy (suffix, "XVL");

	      field_name = concat_name (field_name, suffix);
	    }

	  new_field
	    = create_field_decl (field_name, field_type, new_record_type,
				 DECL_SIZE (old_field), pos, 0, 0);
	  /* The specified position is not the actual position of the field
	     but the gap with the previous field, so the computation of the
	     bit-field status may be incorrect.  We adjust it manually to
	     avoid generating useless attributes for the field in DWARF.  */
	  if (DECL_SIZE (old_field) == TYPE_SIZE (field_type)
	      && value_factor_p (pos, BITS_PER_UNIT))
	    {
	      DECL_BIT_FIELD (new_field) = 0;
	      DECL_BIT_FIELD_TYPE (new_field) = NULL_TREE;
	    }
	  DECL_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
	  TYPE_FIELDS (new_record_type) = new_field;

	  /* If old_field is a QUAL_UNION_TYPE, take its size as being
	     zero.  The only time it's not the last field of the record
	     is when there are other components at fixed positions after
	     it (meaning there was a rep clause for every field) and we
	     want to be able to encode them.  */
	  last_pos = size_binop (PLUS_EXPR, curpos,
				 (TREE_CODE (TREE_TYPE (old_field))
				  == QUAL_UNION_TYPE)
				 ? bitsize_zero_node
				 : DECL_SIZE (old_field));
	}

      TYPE_FIELDS (new_record_type) = nreverse (TYPE_FIELDS (new_record_type));

      add_parallel_type (record_type, new_record_type);
    }
}

/* Utility function of above to merge LAST_SIZE, the previous size of a record
   with FIRST_BIT and SIZE that describe a field.  If MAX is true, we take the
   MAX of the end position of this field with LAST_SIZE.  In all other cases,
   we use FIRST_BIT plus SIZE.  SPECIAL is true if it's for a QUAL_UNION_TYPE,
   in which case we must look for COND_EXPRs and replace a value of zero with
   the old size.  Return an expression for the size.  */

static tree
merge_sizes (tree last_size, tree first_bit, tree size, bool max, bool special)
{
  tree type = TREE_TYPE (last_size);
  tree new_size;

  if (!special || TREE_CODE (size) != COND_EXPR)
    {
      new_size = size_binop (PLUS_EXPR, first_bit, size);
      if (max)
	new_size = size_binop (MAX_EXPR, last_size, new_size);
    }

  else
    new_size = fold_build3 (COND_EXPR, type, TREE_OPERAND (size, 0),
			    integer_zerop (TREE_OPERAND (size, 1))
			    ? last_size : merge_sizes (last_size, first_bit,
						       TREE_OPERAND (size, 1),
						       max, special),
			    integer_zerop (TREE_OPERAND (size, 2))
			    ? last_size : merge_sizes (last_size, first_bit,
						       TREE_OPERAND (size, 2),
						       max, special));

  /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
     when fed through SUBSTITUTE_IN_EXPR) into thinking that a constant
     size is not constant.  */
  while (TREE_CODE (new_size) == NON_LVALUE_EXPR)
    new_size = TREE_OPERAND (new_size, 0);

  return new_size;
}

/* Convert the size expression EXPR to TYPE and fold the result.  */

static tree
fold_convert_size (tree type, tree expr)
{
  /* We assume that size expressions do not wrap around.  */
  if (TREE_CODE (expr) == MULT_EXPR || TREE_CODE (expr) == PLUS_EXPR)
    return size_binop (TREE_CODE (expr),
		       fold_convert_size (type, TREE_OPERAND (expr, 0)),
		       fold_convert_size (type, TREE_OPERAND (expr, 1)));

  return fold_convert (type, expr);
}

/* Return the bit position of FIELD, in bits from the start of the record,
   and fold it as much as possible.  This is a tree of type bitsizetype.  */

static tree
fold_bit_position (const_tree field)
{
  tree offset = fold_convert_size (bitsizetype, DECL_FIELD_OFFSET (field));
  return size_binop (PLUS_EXPR, DECL_FIELD_BIT_OFFSET (field),
 		     size_binop (MULT_EXPR, offset, bitsize_unit_node));
}

/* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
   related by the addition of a constant.  Return that constant if so.  */

static tree
compute_related_constant (tree op0, tree op1)
{
  tree factor, op0_var, op1_var, op0_cst, op1_cst, result;

  if (TREE_CODE (op0) == MULT_EXPR
      && TREE_CODE (op1) == MULT_EXPR
      && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
      && TREE_OPERAND (op1, 1) == TREE_OPERAND (op0, 1))
    {
      factor = TREE_OPERAND (op0, 1);
      op0 = TREE_OPERAND (op0, 0);
      op1 = TREE_OPERAND (op1, 0);
    }
  else
    factor = NULL_TREE;

  op0_cst = split_plus (op0, &op0_var);
  op1_cst = split_plus (op1, &op1_var);
  result = size_binop (MINUS_EXPR, op0_cst, op1_cst);

  if (operand_equal_p (op0_var, op1_var, 0))
    return factor ? size_binop (MULT_EXPR, factor, result) : result;

  return NULL_TREE;
}

/* Utility function of above to split a tree OP which may be a sum, into a
   constant part, which is returned, and a variable part, which is stored
   in *PVAR.  *PVAR may be bitsize_zero_node.  All operations must be of
   bitsizetype.  */

static tree
split_plus (tree in, tree *pvar)
{
  /* Strip conversions in order to ease the tree traversal and maximize the
     potential for constant or plus/minus discovery.  We need to be careful
     to always return and set *pvar to bitsizetype trees, but it's worth
     the effort.  */
  in = remove_conversions (in, false);

  *pvar = convert (bitsizetype, in);

  if (TREE_CODE (in) == INTEGER_CST)
    {
      *pvar = bitsize_zero_node;
      return convert (bitsizetype, in);
    }
  else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
    {
      tree lhs_var, rhs_var;
      tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
      tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);

      if (lhs_var == TREE_OPERAND (in, 0)
	  && rhs_var == TREE_OPERAND (in, 1))
	return bitsize_zero_node;

      *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
      return size_binop (TREE_CODE (in), lhs_con, rhs_con);
    }
  else
    return bitsize_zero_node;
}

/* Return a copy of TYPE but safe to modify in any way.  */

tree
copy_type (tree type)
{
  tree new_type = copy_node (type);

  /* Unshare the language-specific data.  */
  if (TYPE_LANG_SPECIFIC (type))
    {
      TYPE_LANG_SPECIFIC (new_type) = NULL;
      SET_TYPE_LANG_SPECIFIC (new_type, GET_TYPE_LANG_SPECIFIC (type));
    }

  /* And the contents of the language-specific slot if needed.  */
  if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
      && TYPE_RM_VALUES (type))
    {
      TYPE_RM_VALUES (new_type) = NULL_TREE;
      SET_TYPE_RM_SIZE (new_type, TYPE_RM_SIZE (type));
      SET_TYPE_RM_MIN_VALUE (new_type, TYPE_RM_MIN_VALUE (type));
      SET_TYPE_RM_MAX_VALUE (new_type, TYPE_RM_MAX_VALUE (type));
    }

  /* copy_node clears this field instead of copying it, because it is
     aliased with TREE_CHAIN.  */
  TYPE_STUB_DECL (new_type) = TYPE_STUB_DECL (type);

  TYPE_POINTER_TO (new_type) = NULL_TREE;
  TYPE_REFERENCE_TO (new_type) = NULL_TREE;
  TYPE_MAIN_VARIANT (new_type) = new_type;
  TYPE_NEXT_VARIANT (new_type) = NULL_TREE;
  TYPE_CANONICAL (new_type) = new_type;

  return new_type;
}

/* Return a subtype of sizetype with range MIN to MAX and whose
   TYPE_INDEX_TYPE is INDEX.  GNAT_NODE is used for the position
   of the associated TYPE_DECL.  */

tree
create_index_type (tree min, tree max, tree index, Node_Id gnat_node)
{
  /* First build a type for the desired range.  */
  tree type = build_nonshared_range_type (sizetype, min, max);

  /* Then set the index type.  */
  SET_TYPE_INDEX_TYPE (type, index);
  create_type_decl (NULL_TREE, type, true, false, gnat_node);

  return type;
}

/* Return a subtype of TYPE with range MIN to MAX.  If TYPE is NULL,
   sizetype is used.  */

tree
create_range_type (tree type, tree min, tree max)
{
  tree range_type;

  if (!type)
    type = sizetype;

  /* First build a type with the base range.  */
  range_type = build_nonshared_range_type (type, TYPE_MIN_VALUE (type),
						 TYPE_MAX_VALUE (type));

  /* Then set the actual range.  */
  SET_TYPE_RM_MIN_VALUE (range_type, min);
  SET_TYPE_RM_MAX_VALUE (range_type, max);

  return range_type;
}

/* Return an extra subtype of TYPE with range MIN to MAX.  */

tree
create_extra_subtype (tree type, tree min, tree max)
{
  const bool uns = TYPE_UNSIGNED (type);
  const unsigned prec = TYPE_PRECISION (type);
  tree subtype = uns ? make_unsigned_type (prec) : make_signed_type (prec);

  TREE_TYPE (subtype) = type;
  TYPE_EXTRA_SUBTYPE_P (subtype) = 1;

  SET_TYPE_RM_MIN_VALUE (subtype, min);
  SET_TYPE_RM_MAX_VALUE (subtype, max);

  return subtype;
}

/* Return a TYPE_DECL node suitable for the TYPE_STUB_DECL field of TYPE.
   NAME gives the name of the type to be used in the declaration.  */

tree
create_type_stub_decl (tree name, tree type)
{
  tree type_decl = build_decl (input_location, TYPE_DECL, name, type);
  DECL_ARTIFICIAL (type_decl) = 1;
  TYPE_ARTIFICIAL (type) = 1;
  return type_decl;
}

/* Return a TYPE_DECL node for TYPE.  NAME gives the name of the type to be
   used in the declaration.  ARTIFICIAL_P is true if the declaration was
   generated by the compiler.  DEBUG_INFO_P is true if we need to write
   debug information about this type.  GNAT_NODE is used for the position
   of the decl.  */

tree
create_type_decl (tree name, tree type, bool artificial_p, bool debug_info_p,
		  Node_Id gnat_node)
{
  enum tree_code code = TREE_CODE (type);
  bool is_named
    = TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL;
  tree type_decl;

  /* Only the builtin TYPE_STUB_DECL should be used for dummy types.  */
  gcc_assert (!TYPE_IS_DUMMY_P (type));

  /* If the type hasn't been named yet, we're naming it; preserve an existing
     TYPE_STUB_DECL that has been attached to it for some purpose.  */
  if (!is_named && TYPE_STUB_DECL (type))
    {
      type_decl = TYPE_STUB_DECL (type);
      DECL_NAME (type_decl) = name;
    }
  else
    type_decl = build_decl (input_location, TYPE_DECL, name, type);

  DECL_ARTIFICIAL (type_decl) = artificial_p;
  TYPE_ARTIFICIAL (type) = artificial_p;

  /* Add this decl to the current binding level.  */
  gnat_pushdecl (type_decl, gnat_node);

  /* If we're naming the type, equate the TYPE_STUB_DECL to the name.  This
     causes the name to be also viewed as a "tag" by the debug back-end, with
     the advantage that no DW_TAG_typedef is emitted for artificial "tagged"
     types in DWARF.

     Note that if "type" is used as a DECL_ORIGINAL_TYPE, it may be referenced
     from multiple contexts, and "type_decl" references a copy of it: in such a
     case, do not mess TYPE_STUB_DECL: we do not want to re-use the TYPE_DECL
     with the mechanism above.  */
  if (!is_named && type != DECL_ORIGINAL_TYPE (type_decl))
    TYPE_STUB_DECL (type) = type_decl;

  /* Do not generate debug info for UNCONSTRAINED_ARRAY_TYPE that the
     back-end doesn't support, and for others if we don't need to.  */
  if (code == UNCONSTRAINED_ARRAY_TYPE || !debug_info_p)
    DECL_IGNORED_P (type_decl) = 1;

  return type_decl;
}

/* Return a VAR_DECL or CONST_DECL node.

   NAME gives the name of the variable.  ASM_NAME is its assembler name
   (if provided).  TYPE is its data type (a GCC ..._TYPE node).  INIT is
   the GCC tree for an optional initial expression; NULL_TREE if none.

   CONST_FLAG is true if this variable is constant, in which case we might
   return a CONST_DECL node unless CONST_DECL_ALLOWED_P is false.

   PUBLIC_FLAG is true if this is for a reference to a public entity or for a
   definition to be made visible outside of the current compilation unit, for
   instance variable definitions in a package specification.

   EXTERN_FLAG is true when processing an external variable declaration (as
   opposed to a definition: no storage is to be allocated for the variable).

   STATIC_FLAG is only relevant when not at top level and indicates whether
   to always allocate storage to the variable.

   VOLATILE_FLAG is true if this variable is declared as volatile.

   ARTIFICIAL_P is true if the variable was generated by the compiler.

   DEBUG_INFO_P is true if we need to write debug information for it.

   ATTR_LIST is the list of attributes to be attached to the variable.

   GNAT_NODE is used for the position of the decl.  */

tree
create_var_decl (tree name, tree asm_name, tree type, tree init,
		 bool const_flag, bool public_flag, bool extern_flag,
		 bool static_flag, bool volatile_flag, bool artificial_p,
		 bool debug_info_p, struct attrib *attr_list,
		 Node_Id gnat_node, bool const_decl_allowed_p)
{
  /* Whether the object has static storage duration, either explicitly or by
     virtue of being declared at the global level.  */
  const bool static_storage = static_flag || global_bindings_p ();

  /* Whether the initializer is constant: for an external object or an object
     with static storage duration, we check that the initializer is a valid
     constant expression for initializing a static variable; otherwise, we
     only check that it is constant.  */
  const bool init_const
    = (init
       && gnat_types_compatible_p (type, TREE_TYPE (init))
       && (extern_flag || static_storage
	   ? initializer_constant_valid_p (init, TREE_TYPE (init))
	     != NULL_TREE
	   : TREE_CONSTANT (init)));

  /* Whether we will make TREE_CONSTANT the DECL we produce here, in which
     case the initializer may be used in lieu of the DECL node (as done in
     Identifier_to_gnu).  This is useful to prevent the need of elaboration
     code when an identifier for which such a DECL is made is in turn used
     as an initializer.  We used to rely on CONST_DECL vs VAR_DECL for this,
     but extra constraints apply to this choice (see below) and they are not
     relevant to the distinction we wish to make.  */
  const bool constant_p = const_flag && init_const;

  /* The actual DECL node.  CONST_DECL was initially intended for enumerals
     and may be used for scalars in general but not for aggregates.  */
  tree var_decl
    = build_decl (input_location,
		  (constant_p
		   && const_decl_allowed_p
		   && !AGGREGATE_TYPE_P (type) ? CONST_DECL : VAR_DECL),
		  name, type);

  /* Detect constants created by the front-end to hold 'reference to function
     calls for stabilization purposes.  This is needed for renaming.  */
  if (const_flag && init && POINTER_TYPE_P (type))
    {
      tree inner = init;
      if (TREE_CODE (inner) == COMPOUND_EXPR)
	inner = TREE_OPERAND (inner, 1);
      inner = remove_conversions (inner, true);
      if (TREE_CODE (inner) == ADDR_EXPR
	  && ((TREE_CODE (TREE_OPERAND (inner, 0)) == CALL_EXPR
	       && !call_is_atomic_load (TREE_OPERAND (inner, 0)))
	      || (TREE_CODE (TREE_OPERAND (inner, 0)) == VAR_DECL
		  && DECL_RETURN_VALUE_P (TREE_OPERAND (inner, 0)))))
	DECL_RETURN_VALUE_P (var_decl) = 1;
    }

  /* If this is external, throw away any initializations (they will be done
     elsewhere) unless this is a constant for which we would like to remain
     able to get the initializer.  If we are defining a global here, leave a
     constant initialization and save any variable elaborations for the
     elaboration routine.  If we are just annotating types, throw away the
     initialization if it isn't a constant.  */
  if ((extern_flag && !constant_p)
      || (type_annotate_only && init && !TREE_CONSTANT (init)))
    init = NULL_TREE;

  /* At the global level, a non-constant initializer generates elaboration
     statements.  Check that such statements are allowed, that is to say,
     not violating a No_Elaboration_Code restriction.  */
  if (init && !init_const && global_bindings_p ())
    Check_Elaboration_Code_Allowed (gnat_node);

  /* Attach the initializer, if any.  */
  DECL_INITIAL (var_decl) = init;

  /* Directly set some flags.  */
  DECL_ARTIFICIAL (var_decl) = artificial_p;
  DECL_EXTERNAL (var_decl) = extern_flag;

  TREE_CONSTANT (var_decl) = constant_p;
  TREE_READONLY (var_decl) = const_flag;

  /* The object is public if it is external or if it is declared public
     and has static storage duration.  */
  TREE_PUBLIC (var_decl) = extern_flag || (public_flag && static_storage);

  /* We need to allocate static storage for an object with static storage
     duration if it isn't external.  */
  TREE_STATIC (var_decl) = !extern_flag && static_storage;

  TREE_SIDE_EFFECTS (var_decl)
    = TREE_THIS_VOLATILE (var_decl)
    = TYPE_VOLATILE (type) | volatile_flag;

  if (TREE_SIDE_EFFECTS (var_decl))
    TREE_ADDRESSABLE (var_decl) = 1;

  /* Ada doesn't feature Fortran-like COMMON variables so we shouldn't
     try to fiddle with DECL_COMMON.  However, on platforms that don't
     support global BSS sections, uninitialized global variables would
     go in DATA instead, thus increasing the size of the executable.  */
  if (!flag_no_common
      && TREE_CODE (var_decl) == VAR_DECL
      && TREE_PUBLIC (var_decl)
      && !have_global_bss_p ())
    DECL_COMMON (var_decl) = 1;

  /* Do not emit debug info if not requested, or for an external constant whose
     initializer is not absolute because this would require a global relocation
     in a read-only section which runs afoul of the PE-COFF run-time relocation
     mechanism.  */
  if (!debug_info_p
      || (extern_flag
	  && constant_p
	  && init
	  && initializer_constant_valid_p (init, TREE_TYPE (init))
	     != null_pointer_node))
    DECL_IGNORED_P (var_decl) = 1;

  /* ??? Some attributes cannot be applied to CONST_DECLs.  */
  if (TREE_CODE (var_decl) == VAR_DECL)
    process_attributes (&var_decl, &attr_list, true, gnat_node);

  /* Add this decl to the current binding level.  */
  gnat_pushdecl (var_decl, gnat_node);

  if (TREE_CODE (var_decl) == VAR_DECL && asm_name)
    {
      /* Let the target mangle the name if this isn't a verbatim asm.  */
      if (*IDENTIFIER_POINTER (asm_name) != '*')
	asm_name = targetm.mangle_decl_assembler_name (var_decl, asm_name);

      SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
    }

  return var_decl;
}

/* Return true if TYPE, an aggregate type, contains (or is) an array.
   If SELF_REFERENTIAL is true, then an additional requirement on the
   array is that it be self-referential.  */

bool
aggregate_type_contains_array_p (tree type, bool self_referential)
{
  switch (TREE_CODE (type))
    {
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
      {
	tree field;
	for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
	  if (AGGREGATE_TYPE_P (TREE_TYPE (field))
	      && aggregate_type_contains_array_p (TREE_TYPE (field),
						  self_referential))
	    return true;
	return false;
      }

    case ARRAY_TYPE:
      return self_referential ? type_contains_placeholder_p (type) : true;

    default:
      gcc_unreachable ();
    }
}

/* Return true if TYPE is a type with variable size or a padding type with a
   field of variable size or a record that has a field with such a type.  */

static bool
type_has_variable_size (tree type)
{
  tree field;

  if (!TREE_CONSTANT (TYPE_SIZE (type)))
    return true;

  if (TYPE_IS_PADDING_P (type)
      && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
    return true;

  if (!RECORD_OR_UNION_TYPE_P (type))
    return false;

  for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    if (type_has_variable_size (TREE_TYPE (field)))
      return true;

  return false;
}

/* Return a FIELD_DECL node.  NAME is the field's name, TYPE is its type and
   RECORD_TYPE is the type of the enclosing record.  If SIZE is nonzero, it
   is the specified size of the field.  If POS is nonzero, it is the bit
   position.  PACKED is 1 if the enclosing record is packed, -1 if it has
   Component_Alignment of Storage_Unit.  If ADDRESSABLE is nonzero, it
   means we are allowed to take the address of the field; if it is negative,
   we should not make a bitfield, which is used by make_aligning_type.  */

tree
create_field_decl (tree name, tree type, tree record_type, tree size, tree pos,
		   int packed, int addressable)
{
  tree field_decl = build_decl (input_location, FIELD_DECL, name, type);

  DECL_CONTEXT (field_decl) = record_type;
  TREE_READONLY (field_decl) = TYPE_READONLY (type);

  /* If a size is specified, use it.  Otherwise, if the record type is packed
     compute a size to use, which may differ from the object's natural size.
     We always set a size in this case to trigger the checks for bitfield
     creation below, which is typically required when no position has been
     specified.  */
  if (size)
    size = convert (bitsizetype, size);
  else if (packed == 1)
    {
      size = rm_size (type);
      if (TYPE_MODE (type) == BLKmode)
	size = round_up (size, BITS_PER_UNIT);
    }

  /* If we may, according to ADDRESSABLE, then make a bitfield when the size
     is specified for two reasons: first, when it differs from the natural
     size; second, when the alignment is insufficient.

     We never make a bitfield if the type of the field has a nonconstant size,
     because no such entity requiring bitfield operations should reach here.

     We do *preventively* make a bitfield when there might be the need for it
     but we don't have all the necessary information to decide, as is the case
     of a field in a packed record.

     We also don't look at STRICT_ALIGNMENT here, and rely on later processing
     in layout_decl or finish_record_type to clear the bit_field indication if
     it is in fact not needed.  */
  if (addressable >= 0
      && size
      && TREE_CODE (size) == INTEGER_CST
      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
      && (packed
	  || !tree_int_cst_equal (size, TYPE_SIZE (type))
	  || (pos && !value_factor_p (pos, TYPE_ALIGN (type)))
	  || (TYPE_ALIGN (record_type)
	      && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))))
    {
      DECL_BIT_FIELD (field_decl) = 1;
      DECL_SIZE (field_decl) = size;
      if (!packed && !pos)
	{
	  if (TYPE_ALIGN (record_type)
	      && TYPE_ALIGN (record_type) < TYPE_ALIGN (type))
	    SET_DECL_ALIGN (field_decl, TYPE_ALIGN (record_type));
	  else
	    SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
	}
    }

  DECL_PACKED (field_decl) = pos ? DECL_BIT_FIELD (field_decl) : packed;

  /* If FIELD_TYPE has BLKmode, we must ensure this is aligned to at least
     a byte boundary since GCC cannot handle less aligned BLKmode bitfields.
     Likewise if it has a variable size and no specified position because
     variable-sized objects need to be aligned to at least a byte boundary.
     Likewise for an aggregate without specified position that contains an
     array because, in this case, slices of variable length of this array
     must be handled by GCC and have variable size.  */
  if (packed && (TYPE_MODE (type) == BLKmode
		 || (!pos && type_has_variable_size (type))
		 || (!pos
		     && AGGREGATE_TYPE_P (type)
		     && aggregate_type_contains_array_p (type, false))))
    SET_DECL_ALIGN (field_decl, BITS_PER_UNIT);

  /* Bump the alignment if need be, either for bitfield/packing purposes or
     to satisfy the type requirements if no such considerations apply.  When
     we get the alignment from the type, indicate if this is from an explicit
     user request, which prevents stor-layout from lowering it later on.  */
  else
    {
      const unsigned int field_align
	= DECL_BIT_FIELD (field_decl)
	  ? 1
	  : packed
	    ? BITS_PER_UNIT
	    : 0;

      if (field_align > DECL_ALIGN (field_decl))
	SET_DECL_ALIGN (field_decl, field_align);
      else if (!field_align && TYPE_ALIGN (type) > DECL_ALIGN (field_decl))
	{
	  SET_DECL_ALIGN (field_decl, TYPE_ALIGN (type));
	  DECL_USER_ALIGN (field_decl) = TYPE_USER_ALIGN (type);
	}
    }

  if (pos)
    {
      /* We need to pass in the alignment the DECL is known to have.
	 This is the lowest-order bit set in POS, but no more than
	 the alignment of the record, if one is specified.  Note
	 that an alignment of 0 is taken as infinite.  */
      unsigned int known_align;

      if (tree_fits_uhwi_p (pos))
	known_align = tree_to_uhwi (pos) & -tree_to_uhwi (pos);
      else
	known_align = BITS_PER_UNIT;

      if (TYPE_ALIGN (record_type)
	  && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
	known_align = TYPE_ALIGN (record_type);

      layout_decl (field_decl, known_align);
      SET_DECL_OFFSET_ALIGN (field_decl,
			     tree_fits_uhwi_p (pos)
			     ? BIGGEST_ALIGNMENT : BITS_PER_UNIT);
      pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
		    &DECL_FIELD_BIT_OFFSET (field_decl),
		    DECL_OFFSET_ALIGN (field_decl), pos);
    }

  /* In addition to what our caller says, claim the field is addressable if we
     know that its type is not suitable.

     The field may also be "technically" nonaddressable, meaning that even if
     we attempt to take the field's address we will actually get the address
     of a copy.  This is the case for true bitfields, but the DECL_BIT_FIELD
     value we have at this point is not accurate enough, so we don't account
     for this here and let finish_record_type decide.  */
  if (!addressable && !type_for_nonaliased_component_p (type))
    addressable = 1;

  /* Note that there is a trade-off in making a field nonaddressable because
     this will cause type-based alias analysis to use the same alias set for
     accesses to the field as for accesses to the whole record: while doing
     so will make it more likely to disambiguate accesses to other objects
     and accesses to the field, it will make it less likely to disambiguate
     accesses to the other fields of the record and accesses to the field.
     If the record is fully static, then the trade-off is irrelevant since
     the fields of the record can always be disambiguated by their offsets
     but, if the record is dynamic, then it can become problematic.  */
  DECL_NONADDRESSABLE_P (field_decl) = !addressable;

  return field_decl;
}

/* Return a PARM_DECL node with NAME and TYPE.  */

tree
create_param_decl (tree name, tree type)
{
  tree param_decl = build_decl (input_location, PARM_DECL, name, type);

  /* Honor TARGET_PROMOTE_PROTOTYPES like the C compiler, as not doing so
     can lead to various ABI violations.  */
  if (targetm.calls.promote_prototypes (NULL_TREE)
      && INTEGRAL_TYPE_P (type)
      && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
    {
      /* We have to be careful about biased types here.  Make a subtype
	 of integer_type_node with the proper biasing.  */
      if (TREE_CODE (type) == INTEGER_TYPE
	  && TYPE_BIASED_REPRESENTATION_P (type))
	{
	  tree subtype
	    = make_unsigned_type (TYPE_PRECISION (integer_type_node));
	  TREE_TYPE (subtype) = integer_type_node;
	  TYPE_BIASED_REPRESENTATION_P (subtype) = 1;
	  SET_TYPE_RM_MIN_VALUE (subtype, TYPE_MIN_VALUE (type));
	  SET_TYPE_RM_MAX_VALUE (subtype, TYPE_MAX_VALUE (type));
	  type = subtype;
	}
      else
	type = integer_type_node;
    }

  DECL_ARG_TYPE (param_decl) = type;
  return param_decl;
}

/* Process the attributes in ATTR_LIST for NODE, which is either a DECL or
   a TYPE.  If IN_PLACE is true, the tree pointed to by NODE should not be
   changed.  GNAT_NODE is used for the position of error messages.  */

void
process_attributes (tree *node, struct attrib **attr_list, bool in_place,
		    Node_Id gnat_node)
{
  struct attrib *attr;

  for (attr = *attr_list; attr; attr = attr->next)
    switch (attr->type)
      {
      case ATTR_MACHINE_ATTRIBUTE:
	Sloc_to_locus (Sloc (gnat_node), &input_location);
	decl_attributes (node, tree_cons (attr->name, attr->args, NULL_TREE),
			 in_place ? ATTR_FLAG_TYPE_IN_PLACE : 0);
	break;

      case ATTR_LINK_ALIAS:
        if (!DECL_EXTERNAL (*node))
	  {
	    TREE_STATIC (*node) = 1;
	    assemble_alias (*node, attr->name);
	  }
	break;

      case ATTR_WEAK_EXTERNAL:
	if (SUPPORTS_WEAK)
	  declare_weak (*node);
	else
	  post_error ("?weak declarations not supported on this target",
		      attr->error_point);
	break;

      case ATTR_LINK_SECTION:
	if (targetm_common.have_named_sections)
	  {
	    set_decl_section_name (*node, IDENTIFIER_POINTER (attr->name));
	    DECL_COMMON (*node) = 0;
	  }
	else
	  post_error ("?section attributes are not supported for this target",
		      attr->error_point);
	break;

      case ATTR_LINK_CONSTRUCTOR:
	DECL_STATIC_CONSTRUCTOR (*node) = 1;
	TREE_USED (*node) = 1;
	break;

      case ATTR_LINK_DESTRUCTOR:
	DECL_STATIC_DESTRUCTOR (*node) = 1;
	TREE_USED (*node) = 1;
	break;

      case ATTR_THREAD_LOCAL_STORAGE:
	set_decl_tls_model (*node, decl_default_tls_model (*node));
	DECL_COMMON (*node) = 0;
	break;
      }

  *attr_list = NULL;
}

/* Return true if VALUE is a known to be a multiple of FACTOR, which must be
   a power of 2. */

bool
value_factor_p (tree value, unsigned HOST_WIDE_INT factor)
{
  gcc_checking_assert (pow2p_hwi (factor));

  if (tree_fits_uhwi_p (value))
    return (tree_to_uhwi (value) & (factor - 1)) == 0;

  if (TREE_CODE (value) == MULT_EXPR)
    return (value_factor_p (TREE_OPERAND (value, 0), factor)
            || value_factor_p (TREE_OPERAND (value, 1), factor));

  return false;
}

/* Defer the initialization of DECL's DECL_CONTEXT attribute, scheduling to
   feed it with the elaboration of GNAT_SCOPE.  */

static struct deferred_decl_context_node *
add_deferred_decl_context (tree decl, Entity_Id gnat_scope, int force_global)
{
  struct deferred_decl_context_node *new_node;

  new_node
    = (struct deferred_decl_context_node * ) xmalloc (sizeof (*new_node));
  new_node->decl = decl;
  new_node->gnat_scope = gnat_scope;
  new_node->force_global = force_global;
  new_node->types.create (1);
  new_node->next = deferred_decl_context_queue;
  deferred_decl_context_queue = new_node;
  return new_node;
}

/* Defer the initialization of TYPE's TYPE_CONTEXT attribute, scheduling to
   feed it with the DECL_CONTEXT computed as part of N as soon as it is
   computed.  */

static void
add_deferred_type_context (struct deferred_decl_context_node *n, tree type)
{
  n->types.safe_push (type);
}

/* Get the GENERIC node corresponding to GNAT_SCOPE, if available.  Return
   NULL_TREE if it is not available.  */

static tree
compute_deferred_decl_context (Entity_Id gnat_scope)
{
  tree context;

  if (present_gnu_tree (gnat_scope))
    context = get_gnu_tree (gnat_scope);
  else
    return NULL_TREE;

  if (TREE_CODE (context) == TYPE_DECL)
    {
      tree context_type = TREE_TYPE (context);

      /* Skip dummy types: only the final ones can appear in the context
	 chain.  */
      if (TYPE_DUMMY_P (context_type))
	return NULL_TREE;

      /* ..._TYPE nodes are more useful than TYPE_DECL nodes in the context
	 chain.  */
      else
	context = context_type;
    }

  return context;
}

/* Try to process all deferred nodes in the queue.  Keep in the queue the ones
   that cannot be processed yet, remove the other ones.  If FORCE is true,
   force the processing for all nodes, use the global context when nodes don't
   have a GNU translation.  */

void
process_deferred_decl_context (bool force)
{
  struct deferred_decl_context_node **it = &deferred_decl_context_queue;
  struct deferred_decl_context_node *node;

  while (*it)
    {
      bool processed = false;
      tree context = NULL_TREE;
      Entity_Id gnat_scope;

      node = *it;

      /* If FORCE, get the innermost elaborated scope.  Otherwise, just try to
	 get the first scope.  */
      gnat_scope = node->gnat_scope;
      while (Present (gnat_scope))
	{
	  context = compute_deferred_decl_context (gnat_scope);
	  if (!force || context)
	    break;
	  gnat_scope = get_debug_scope (gnat_scope, NULL);
	}

      /* Imported declarations must not be in a local context (i.e. not inside
	 a function).  */
      if (context && node->force_global > 0)
	{
	  tree ctx = context;

	  while (ctx)
	    {
	      gcc_assert (TREE_CODE (ctx) != FUNCTION_DECL);
	      ctx = DECL_P (ctx) ? DECL_CONTEXT (ctx) : TYPE_CONTEXT (ctx);
	    }
	}

      /* If FORCE, we want to get rid of all nodes in the queue: in case there
	 was no elaborated scope, use the global context.  */
      if (force && !context)
	context = get_global_context ();

      if (context)
	{
	  tree t;
	  int i;

	  DECL_CONTEXT (node->decl) = context;

	  /* Propagate it to the TYPE_CONTEXT attributes of the requested
	     ..._TYPE nodes.  */
	  FOR_EACH_VEC_ELT (node->types, i, t)
	    {
	      gnat_set_type_context (t, context);
	    }
	  processed = true;
	}

      /* If this node has been successfuly processed, remove it from the
	 queue.  Then move to the next node.  */
      if (processed)
	{
	  *it = node->next;
	  node->types.release ();
	  free (node);
	}
      else
	it = &node->next;
    }
}

/* Return VALUE scaled by the biggest power-of-2 factor of EXPR.  */

static unsigned int
scale_by_factor_of (tree expr, unsigned int value)
{
  unsigned HOST_WIDE_INT addend = 0;
  unsigned HOST_WIDE_INT factor = 1;

  /* Peel conversions around EXPR and try to extract bodies from function
     calls: it is possible to get the scale factor from size functions.  */
  expr = remove_conversions (expr, true);
  if (TREE_CODE (expr) == CALL_EXPR)
    expr = maybe_inline_call_in_expr (expr);

  /* Sometimes we get PLUS_EXPR (BIT_AND_EXPR (..., X), Y), where Y is a
     multiple of the scale factor we are looking for.  */
  if (TREE_CODE (expr) == PLUS_EXPR
      && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
      && tree_fits_uhwi_p (TREE_OPERAND (expr, 1)))
    {
      addend = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
      expr = TREE_OPERAND (expr, 0);
    }

  /* An expression which is a bitwise AND with a mask has a power-of-2 factor
     corresponding to the number of trailing zeros of the mask.  */
  if (TREE_CODE (expr) == BIT_AND_EXPR
      && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
    {
      unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (TREE_OPERAND (expr, 1));
      unsigned int i = 0;

      while ((mask & 1) == 0 && i < HOST_BITS_PER_WIDE_INT)
	{
	  mask >>= 1;
	  factor *= 2;
	  i++;
	}
    }

  /* If the addend is not a multiple of the factor we found, give up.  In
     theory we could find a smaller common factor but it's useless for our
     needs.  This situation arises when dealing with a field F1 with no
     alignment requirement but that is following a field F2 with such
     requirements.  As long as we have F2's offset, we don't need alignment
     information to compute F1's.  */
  if (addend % factor != 0)
    factor = 1;

  return factor * value;
}

/* Return a LABEL_DECL with NAME.  GNAT_NODE is used for the position of
   the decl.  */

tree
create_label_decl (tree name, Node_Id gnat_node)
{
  tree label_decl
    = build_decl (input_location, LABEL_DECL, name, void_type_node);

  SET_DECL_MODE (label_decl, VOIDmode);

  /* Add this decl to the current binding level.  */
  gnat_pushdecl (label_decl, gnat_node);

  return label_decl;
}

/* Return a FUNCTION_DECL node.  NAME is the name of the subprogram, ASM_NAME
   its assembler name, TYPE its type (a FUNCTION_TYPE or METHOD_TYPE node),
   PARAM_DECL_LIST the list of its parameters (a list of PARM_DECL nodes
   chained through the DECL_CHAIN field).

   INLINE_STATUS describes the inline flags to be set on the FUNCTION_DECL.

   PUBLIC_FLAG is true if this is for a reference to a public entity or for a
   definition to be made visible outside of the current compilation unit.

   EXTERN_FLAG is true when processing an external subprogram declaration.

   ARTIFICIAL_P is true if the subprogram was generated by the compiler.

   DEBUG_INFO_P is true if we need to write debug information for it.

   DEFINITION is true if the subprogram is to be considered as a definition.

   ATTR_LIST is the list of attributes to be attached to the subprogram.

   GNAT_NODE is used for the position of the decl.  */

tree
create_subprog_decl (tree name, tree asm_name, tree type, tree param_decl_list,
		     enum inline_status_t inline_status, bool public_flag,
		     bool extern_flag, bool artificial_p, bool debug_info_p,
		     bool definition, struct attrib *attr_list,
		     Node_Id gnat_node)
{
  tree subprog_decl = build_decl (input_location, FUNCTION_DECL, name, type);
  DECL_ARGUMENTS (subprog_decl) = param_decl_list;

  DECL_ARTIFICIAL (subprog_decl) = artificial_p;
  DECL_EXTERNAL (subprog_decl) = extern_flag;
  DECL_FUNCTION_IS_DEF (subprog_decl) = definition;
  DECL_IGNORED_P (subprog_decl) = !debug_info_p;
  TREE_PUBLIC (subprog_decl) = public_flag;

  switch (inline_status)
    {
    case is_suppressed:
      DECL_UNINLINABLE (subprog_decl) = 1;
      break;

    case is_default:
      break;

    case is_required:
      if (Back_End_Inlining)
	{
	  decl_attributes (&subprog_decl,
			   tree_cons (get_identifier ("always_inline"),
				      NULL_TREE, NULL_TREE),
			   ATTR_FLAG_TYPE_IN_PLACE);

	  /* Inline_Always guarantees that every direct call is inlined and
	     that there is no indirect reference to the subprogram, so the
	     instance in the original package (as well as its clones in the
	     client packages created for inter-unit inlining) can be made
	     private, which causes the out-of-line body to be eliminated.  */
	  TREE_PUBLIC (subprog_decl) = 0;
	}

      /* ... fall through ... */

    case is_prescribed:
      DECL_DISREGARD_INLINE_LIMITS (subprog_decl) = 1;

      /* ... fall through ... */

    case is_requested:
      DECL_DECLARED_INLINE_P (subprog_decl) = 1;
      if (!Debug_Generated_Code)
	DECL_NO_INLINE_WARNING_P (subprog_decl) = artificial_p;
      break;

    default:
      gcc_unreachable ();
    }

  process_attributes (&subprog_decl, &attr_list, true, gnat_node);

  /* Once everything is processed, finish the subprogram declaration.  */
  finish_subprog_decl (subprog_decl, asm_name, type);

  /* Add this decl to the current binding level.  */
  gnat_pushdecl (subprog_decl, gnat_node);

  /* Output the assembler code and/or RTL for the declaration.  */
  rest_of_decl_compilation (subprog_decl, global_bindings_p (), 0);

  return subprog_decl;
}

/* Given a subprogram declaration DECL, its assembler name and its type,
   finish constructing the subprogram declaration from ASM_NAME and TYPE.  */

void
finish_subprog_decl (tree decl, tree asm_name, tree type)
{
  /* DECL_ARGUMENTS is set by the caller, but not its context.  */
  for (tree param_decl = DECL_ARGUMENTS (decl);
       param_decl;
       param_decl = DECL_CHAIN (param_decl))
    DECL_CONTEXT (param_decl) = decl;

  tree result_decl
    = build_decl (DECL_SOURCE_LOCATION (decl), RESULT_DECL, NULL_TREE,
		  TREE_TYPE (type));

  DECL_ARTIFICIAL (result_decl) = 1;
  DECL_IGNORED_P (result_decl) = 1;
  DECL_CONTEXT (result_decl) = decl;
  DECL_BY_REFERENCE (result_decl) = TREE_ADDRESSABLE (type);
  DECL_RESULT (decl) = result_decl;

  /* Propagate the "pure" property.  */
  DECL_PURE_P (decl) = TYPE_RESTRICT (type);

  /* Propagate the "noreturn" property.  */
  TREE_THIS_VOLATILE (decl) = TYPE_VOLATILE (type);

  if (asm_name)
    {
      /* Let the target mangle the name if this isn't a verbatim asm.  */
      if (*IDENTIFIER_POINTER (asm_name) != '*')
	asm_name = targetm.mangle_decl_assembler_name (decl, asm_name);

      SET_DECL_ASSEMBLER_NAME (decl, asm_name);

      /* The expand_main_function circuitry expects "main_identifier_node" to
	 designate the DECL_NAME of the 'main' entry point, in turn expected
	 to be declared as the "main" function literally by default.  Ada
	 program entry points are typically declared with a different name
	 within the binder generated file, exported as 'main' to satisfy the
	 system expectations.  Force main_identifier_node in this case.  */
      if (asm_name == main_identifier_node)
	DECL_NAME (decl) = main_identifier_node;
    }
}

/* Set up the framework for generating code for SUBPROG_DECL, a subprogram
   body.  This routine needs to be invoked before processing the declarations
   appearing in the subprogram.  */

void
begin_subprog_body (tree subprog_decl)
{
  announce_function (subprog_decl);

  /* This function is being defined.  */
  TREE_STATIC (subprog_decl) = 1;

  /* The failure of this assertion will likely come from a wrong context for
     the subprogram body, e.g. another procedure for a procedure declared at
     library level.  */
  gcc_assert (current_function_decl == decl_function_context (subprog_decl));

  current_function_decl = subprog_decl;

  /* Enter a new binding level and show that all the parameters belong to
     this function.  */
  gnat_pushlevel ();
}

/* Finish translating the current subprogram and set its BODY.  */

void
end_subprog_body (tree body)
{
  tree fndecl = current_function_decl;

  /* Attach the BLOCK for this level to the function and pop the level.  */
  BLOCK_SUPERCONTEXT (current_binding_level->block) = fndecl;
  DECL_INITIAL (fndecl) = current_binding_level->block;
  gnat_poplevel ();

  /* The body should be a BIND_EXPR whose BLOCK is the top-level one.  */
  if (TREE_CODE (body) == BIND_EXPR)
    {
      BLOCK_SUPERCONTEXT (BIND_EXPR_BLOCK (body)) = fndecl;
      DECL_INITIAL (fndecl) = BIND_EXPR_BLOCK (body);
    }

  DECL_SAVED_TREE (fndecl) = body;

  current_function_decl = decl_function_context (fndecl);
}

/* Wrap up compilation of SUBPROG_DECL, a subprogram body.  */

void
rest_of_subprog_body_compilation (tree subprog_decl)
{
  /* We cannot track the location of errors past this point.  */
  Current_Error_Node = Empty;

  /* If we're only annotating types, don't actually compile this function.  */
  if (type_annotate_only)
    return;

  /* Dump functions before gimplification.  */
  dump_function (TDI_original, subprog_decl);

  if (!decl_function_context (subprog_decl))
    cgraph_node::finalize_function (subprog_decl, false);
  else
    /* Register this function with cgraph just far enough to get it
       added to our parent's nested function list.  */
    (void) cgraph_node::get_create (subprog_decl);
}

tree
gnat_builtin_function (tree decl)
{
  gnat_pushdecl (decl, Empty);
  return decl;
}

/* Return an integer type with the number of bits of precision given by
   PRECISION.  UNSIGNEDP is nonzero if the type is unsigned; otherwise
   it is a signed type.  */

tree
gnat_type_for_size (unsigned precision, int unsignedp)
{
  tree t;
  char type_name[20];

  if (precision <= 2 * MAX_BITS_PER_WORD
      && signed_and_unsigned_types[precision][unsignedp])
    return signed_and_unsigned_types[precision][unsignedp];

 if (unsignedp)
    t = make_unsigned_type (precision);
  else
    t = make_signed_type (precision);
  TYPE_ARTIFICIAL (t) = 1;

  if (precision <= 2 * MAX_BITS_PER_WORD)
    signed_and_unsigned_types[precision][unsignedp] = t;

  if (!TYPE_NAME (t))
    {
      sprintf (type_name, "%sSIGNED_%u", unsignedp ? "UN" : "", precision);
      TYPE_NAME (t) = get_identifier (type_name);
    }

  return t;
}

/* Likewise for floating-point types.  */

static tree
float_type_for_precision (int precision, machine_mode mode)
{
  tree t;
  char type_name[20];

  if (float_types[(int) mode])
    return float_types[(int) mode];

  float_types[(int) mode] = t = make_node (REAL_TYPE);
  TYPE_PRECISION (t) = precision;
  layout_type (t);

  gcc_assert (TYPE_MODE (t) == mode);
  if (!TYPE_NAME (t))
    {
      sprintf (type_name, "FLOAT_%d", precision);
      TYPE_NAME (t) = get_identifier (type_name);
    }

  return t;
}

/* Return a data type that has machine mode MODE.  UNSIGNEDP selects
   an unsigned type; otherwise a signed type is returned.  */

tree
gnat_type_for_mode (machine_mode mode, int unsignedp)
{
  if (mode == BLKmode)
    return NULL_TREE;

  if (mode == VOIDmode)
    return void_type_node;

  if (COMPLEX_MODE_P (mode))
    return NULL_TREE;

  scalar_float_mode float_mode;
  if (is_a <scalar_float_mode> (mode, &float_mode))
    return float_type_for_precision (GET_MODE_PRECISION (float_mode),
				     float_mode);

  scalar_int_mode int_mode;
  if (is_a <scalar_int_mode> (mode, &int_mode))
    return gnat_type_for_size (GET_MODE_BITSIZE (int_mode), unsignedp);

  if (VECTOR_MODE_P (mode))
    {
      machine_mode inner_mode = GET_MODE_INNER (mode);
      tree inner_type = gnat_type_for_mode (inner_mode, unsignedp);
      if (inner_type)
	return build_vector_type_for_mode (inner_type, mode);
    }

  return NULL_TREE;
}

/* Return the signed or unsigned version of TYPE_NODE, a scalar type, the
   signedness being specified by UNSIGNEDP.  */

tree
gnat_signed_or_unsigned_type_for (int unsignedp, tree type_node)
{
  if (type_node == char_type_node)
    return unsignedp ? unsigned_char_type_node : signed_char_type_node;

  tree type = gnat_type_for_size (TYPE_PRECISION (type_node), unsignedp);

  if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
    {
      type = copy_type (type);
      TREE_TYPE (type) = type_node;
    }
  else if (TREE_TYPE (type_node)
	   && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
	   && TYPE_MODULAR_P (TREE_TYPE (type_node)))
    {
      type = copy_type (type);
      TREE_TYPE (type) = TREE_TYPE (type_node);
    }

  return type;
}

/* Return 1 if the types T1 and T2 are compatible, i.e. if they can be
   transparently converted to each other.  */

int
gnat_types_compatible_p (tree t1, tree t2)
{
  enum tree_code code;

  /* This is the default criterion.  */
  if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
    return 1;

  /* We only check structural equivalence here.  */
  if ((code = TREE_CODE (t1)) != TREE_CODE (t2))
    return 0;

  /* Vector types are also compatible if they have the same number of subparts
     and the same form of (scalar) element type.  */
  if (code == VECTOR_TYPE
      && known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
      && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2))
      && TYPE_PRECISION (TREE_TYPE (t1)) == TYPE_PRECISION (TREE_TYPE (t2)))
    return 1;

  /* Array types are also compatible if they are constrained and have the same
     domain(s), the same component type and the same scalar storage order.  */
  if (code == ARRAY_TYPE
      && (TYPE_DOMAIN (t1) == TYPE_DOMAIN (t2)
	  || (TYPE_DOMAIN (t1)
	      && TYPE_DOMAIN (t2)
	      && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)),
				     TYPE_MIN_VALUE (TYPE_DOMAIN (t2)))
	      && tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (t1)),
				     TYPE_MAX_VALUE (TYPE_DOMAIN (t2)))))
      && (TREE_TYPE (t1) == TREE_TYPE (t2)
	  || (TREE_CODE (TREE_TYPE (t1)) == ARRAY_TYPE
	      && gnat_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))))
      && TYPE_REVERSE_STORAGE_ORDER (t1) == TYPE_REVERSE_STORAGE_ORDER (t2))
    return 1;

  return 0;
}

/* Return true if EXPR is a useless type conversion.  */

bool
gnat_useless_type_conversion (tree expr)
{
  if (CONVERT_EXPR_P (expr)
      || TREE_CODE (expr) == VIEW_CONVERT_EXPR
      || TREE_CODE (expr) == NON_LVALUE_EXPR)
    return gnat_types_compatible_p (TREE_TYPE (expr),
				    TREE_TYPE (TREE_OPERAND (expr, 0)));

  return false;
}

/* Return true if T, a {FUNCTION,METHOD}_TYPE, has the specified flags.  */

bool
fntype_same_flags_p (const_tree t, tree cico_list, bool return_unconstrained_p,
		     bool return_by_direct_ref_p, bool return_by_invisi_ref_p)
{
  return TYPE_CI_CO_LIST (t) == cico_list
	 && TYPE_RETURN_UNCONSTRAINED_P (t) == return_unconstrained_p
	 && TYPE_RETURN_BY_DIRECT_REF_P (t) == return_by_direct_ref_p
	 && TREE_ADDRESSABLE (t) == return_by_invisi_ref_p;
}

/* EXP is an expression for the size of an object.  If this size contains
   discriminant references, replace them with the maximum (if MAX_P) or
   minimum (if !MAX_P) possible value of the discriminant.

   Note that the expression may have already been gimplified,in which case
   COND_EXPRs have VOID_TYPE and no operands, and this must be handled.  */

tree
max_size (tree exp, bool max_p)
{
  enum tree_code code = TREE_CODE (exp);
  tree type = TREE_TYPE (exp);
  tree op0, op1, op2;

  switch (TREE_CODE_CLASS (code))
    {
    case tcc_declaration:
    case tcc_constant:
      return exp;

    case tcc_exceptional:
      gcc_assert (code == SSA_NAME);
      return exp;

    case tcc_vl_exp:
      if (code == CALL_EXPR)
	{
	  tree t, *argarray;
	  int n, i;

	  t = maybe_inline_call_in_expr (exp);
	  if (t)
	    return max_size (t, max_p);

	  n = call_expr_nargs (exp);
	  gcc_assert (n > 0);
	  argarray = XALLOCAVEC (tree, n);
	  for (i = 0; i < n; i++)
	    argarray[i] = max_size (CALL_EXPR_ARG (exp, i), max_p);
	  return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
	}
      break;

    case tcc_reference:
      /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
	 modify.  Otherwise, we treat it like a variable.  */
      if (CONTAINS_PLACEHOLDER_P (exp))
	{
	  tree base_type = get_base_type (TREE_TYPE (TREE_OPERAND (exp, 1)));
	  tree val
	    = fold_convert (base_type,
			    max_p
			    ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));

	  /* Walk down the extra subtypes to get more restrictive bounds.  */
	  while (TYPE_IS_EXTRA_SUBTYPE_P (type))
	    {
	      type = TREE_TYPE (type);
	      if (max_p)
		val = fold_build2 (MIN_EXPR, base_type, val,
				   fold_convert (base_type,
						 TYPE_MAX_VALUE (type)));
	      else
		val = fold_build2 (MAX_EXPR, base_type, val,
				   fold_convert (base_type,
						 TYPE_MIN_VALUE (type)));
	    }

	  return fold_convert (type, max_size (val, max_p));
	}

      return exp;

    case tcc_comparison:
      return build_int_cst (type, max_p ? 1 : 0);

    case tcc_unary:
      op0 = TREE_OPERAND (exp, 0);

      if (code == NON_LVALUE_EXPR)
	return max_size (op0, max_p);

      if (VOID_TYPE_P (TREE_TYPE (op0)))
	return max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type);

      op0 = max_size (op0, code == NEGATE_EXPR ? !max_p : max_p);

      if (op0 == TREE_OPERAND (exp, 0))
	return exp;

      return fold_build1 (code, type, op0);

    case tcc_binary:
      op0 = TREE_OPERAND (exp, 0);
      op1 = TREE_OPERAND (exp, 1);

      /* If we have a multiply-add with a "negative" value in an unsigned
	 type, do a multiply-subtract with the negated value, in order to
	 avoid creating a spurious overflow below.  */
      if (code == PLUS_EXPR
	  && TREE_CODE (op0) == MULT_EXPR
	  && TYPE_UNSIGNED (type)
	  && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
	  && !TREE_OVERFLOW (TREE_OPERAND (op0, 1))
	  && tree_int_cst_sign_bit (TREE_OPERAND (op0, 1)))
	{
	  tree tmp = op1;
	  op1 = build2 (MULT_EXPR, type, TREE_OPERAND (op0, 0),
			fold_build1 (NEGATE_EXPR, type,
				    TREE_OPERAND (op0, 1)));
	  op0 = tmp;
	  code = MINUS_EXPR;
	}

      op0 = max_size (op0, max_p);
      op1 = max_size (op1, code == MINUS_EXPR ? !max_p : max_p);

      if ((code == MINUS_EXPR || code == PLUS_EXPR))
	{
	  /* If the op0 has overflowed and the op1 is a variable,
	     propagate the overflow by returning the op0.  */
	  if (TREE_CODE (op0) == INTEGER_CST
	      && TREE_OVERFLOW (op0)
	      && TREE_CODE (op1) != INTEGER_CST)
	    return op0;

	  /* If we have a "negative" value in an unsigned type, do the
	     opposite operation on the negated value, in order to avoid
	     creating a spurious overflow below.  */
	  if (TYPE_UNSIGNED (type)
	      && TREE_CODE (op1) == INTEGER_CST
	      && !TREE_OVERFLOW (op1)
	      && tree_int_cst_sign_bit (op1))
	    {
	      op1 = fold_build1 (NEGATE_EXPR, type, op1);
	      code = (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR);
	    }
	}

      if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
	return exp;

      /* We need to detect overflows so we call size_binop here.  */
      return size_binop (code, op0, op1);

    case tcc_expression:
      switch (TREE_CODE_LENGTH (code))
	{
	case 1:
	  if (code == SAVE_EXPR)
	    return exp;

	  op0 = max_size (TREE_OPERAND (exp, 0),
			  code == TRUTH_NOT_EXPR ? !max_p : max_p);

	  if (op0 == TREE_OPERAND (exp, 0))
	    return exp;

	  return fold_build1 (code, type, op0);

	case 2:
	  if (code == COMPOUND_EXPR)
	    return max_size (TREE_OPERAND (exp, 1), max_p);

	  op0 = max_size (TREE_OPERAND (exp, 0), max_p);
	  op1 = max_size (TREE_OPERAND (exp, 1), max_p);

	  if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
	    return exp;

	  return fold_build2 (code, type, op0, op1);

	case 3:
	  if (code == COND_EXPR)
	    {
	      op0 = TREE_OPERAND (exp, 0);
	      op1 = TREE_OPERAND (exp, 1);
	      op2 = TREE_OPERAND (exp, 2);

	      if (!op1 || !op2)
		return exp;

	      op1 = max_size (op1, max_p);
	      op2 = max_size (op2, max_p);

	      /* If we have the MAX of a "negative" value in an unsigned type
		 and zero for a length expression, just return zero.  */
	      if (max_p
		  && TREE_CODE (op0) == LE_EXPR
		  && TYPE_UNSIGNED (type)
		  && TREE_CODE (op1) == INTEGER_CST
		  && !TREE_OVERFLOW (op1)
		  && tree_int_cst_sign_bit (op1)
		  && integer_zerop (op2))
		return op2;

	      return fold_build2 (max_p ? MAX_EXPR : MIN_EXPR, type, op1, op2);
	    }
	  break;

	default:
	  break;
	}

      /* Other tree classes cannot happen.  */
    default:
      break;
    }

  gcc_unreachable ();
}

/* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
   EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
   Return a constructor for the template.  */

tree
build_template (tree template_type, tree array_type, tree expr)
{
  vec<constructor_elt, va_gc> *template_elts = NULL;
  tree bound_list = NULL_TREE;
  tree field;

  while (TREE_CODE (array_type) == RECORD_TYPE
	 && (TYPE_PADDING_P (array_type)
	     || TYPE_JUSTIFIED_MODULAR_P (array_type)))
    array_type = TREE_TYPE (TYPE_FIELDS (array_type));

  if (TREE_CODE (array_type) == ARRAY_TYPE
      || (TREE_CODE (array_type) == INTEGER_TYPE
	  && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
    bound_list = TYPE_ACTUAL_BOUNDS (array_type);

  /* First make the list for a CONSTRUCTOR for the template.  Go down
     the field list of the template instead of the type chain because
     this array might be an Ada array of array and we can't tell where
     the nested array stop being the underlying object.  */
  for (field = TYPE_FIELDS (template_type);
       field;
       field = DECL_CHAIN (DECL_CHAIN (field)))
    {
      tree bounds, min, max;

      /* If we have a bound list, get the bounds from there.  Likewise
	 for an ARRAY_TYPE.  Otherwise, if expr is a PARM_DECL with
	 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the
	 template, but this will only give us a maximum range.  */
      if (bound_list)
	{
	  bounds = TREE_VALUE (bound_list);
	  bound_list = TREE_CHAIN (bound_list);
	}
      else if (TREE_CODE (array_type) == ARRAY_TYPE)
	{
	  bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
	  array_type = TREE_TYPE (array_type);
	}
      else if (expr && TREE_CODE (expr) == PARM_DECL
	       && DECL_BY_COMPONENT_PTR_P (expr))
	bounds = TREE_TYPE (field);
      else
	gcc_unreachable ();

      min = convert (TREE_TYPE (field), TYPE_MIN_VALUE (bounds));
      max = convert (TREE_TYPE (DECL_CHAIN (field)), TYPE_MAX_VALUE (bounds));

      /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
	 substitute it from OBJECT.  */
      min = SUBSTITUTE_PLACEHOLDER_IN_EXPR (min, expr);
      max = SUBSTITUTE_PLACEHOLDER_IN_EXPR (max, expr);

      CONSTRUCTOR_APPEND_ELT (template_elts, field, min);
      CONSTRUCTOR_APPEND_ELT (template_elts, DECL_CHAIN (field), max);
    }

  return gnat_build_constructor (template_type, template_elts);
}

/* Return true if TYPE is suitable for the element type of a vector.  */

static bool
type_for_vector_element_p (tree type)
{
  machine_mode mode;

  if (!INTEGRAL_TYPE_P (type)
      && !SCALAR_FLOAT_TYPE_P (type)
      && !FIXED_POINT_TYPE_P (type))
    return false;

  mode = TYPE_MODE (type);
  if (GET_MODE_CLASS (mode) != MODE_INT
      && !SCALAR_FLOAT_MODE_P (mode)
      && !ALL_SCALAR_FIXED_POINT_MODE_P (mode))
    return false;

  return true;
}

/* Return a vector type given the SIZE and the INNER_TYPE, or NULL_TREE if
   this is not possible.  If ATTRIBUTE is non-zero, we are processing the
   attribute declaration and want to issue error messages on failure.  */

static tree
build_vector_type_for_size (tree inner_type, tree size, tree attribute)
{
  unsigned HOST_WIDE_INT size_int, inner_size_int;
  int nunits;

  /* Silently punt on variable sizes.  We can't make vector types for them,
     need to ignore them on front-end generated subtypes of unconstrained
     base types, and this attribute is for binding implementors, not end
     users, so we should never get there from legitimate explicit uses.  */
  if (!tree_fits_uhwi_p (size))
    return NULL_TREE;
  size_int = tree_to_uhwi (size);

  if (!type_for_vector_element_p (inner_type))
    {
      if (attribute)
	error ("invalid element type for attribute %qs",
	       IDENTIFIER_POINTER (attribute));
      return NULL_TREE;
    }
  inner_size_int = tree_to_uhwi (TYPE_SIZE_UNIT (inner_type));

  if (size_int % inner_size_int)
    {
      if (attribute)
	error ("vector size not an integral multiple of component size");
      return NULL_TREE;
    }

  if (size_int == 0)
    {
      if (attribute)
	error ("zero vector size");
      return NULL_TREE;
    }

  nunits = size_int / inner_size_int;
  if (nunits & (nunits - 1))
    {
      if (attribute)
	error ("number of components of vector not a power of two");
      return NULL_TREE;
    }

  return build_vector_type (inner_type, nunits);
}

/* Return a vector type whose representative array type is ARRAY_TYPE, or
   NULL_TREE if this is not possible.  If ATTRIBUTE is non-zero, we are
   processing the attribute and want to issue error messages on failure.  */

static tree
build_vector_type_for_array (tree array_type, tree attribute)
{
  tree vector_type = build_vector_type_for_size (TREE_TYPE (array_type),
						 TYPE_SIZE_UNIT (array_type),
						 attribute);
  if (!vector_type)
    return NULL_TREE;

  TYPE_REPRESENTATIVE_ARRAY (vector_type) = array_type;
  return vector_type;
}

/* Build a type to be used to represent an aliased object whose nominal type
   is an unconstrained array.  This consists of a RECORD_TYPE containing a
   field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an ARRAY_TYPE.
   If ARRAY_TYPE is that of an unconstrained array, this is used to represent
   an arbitrary unconstrained object.  Use NAME as the name of the record.
   DEBUG_INFO_P is true if we need to write debug information for the type.  */

tree
build_unc_object_type (tree template_type, tree object_type, tree name,
		       bool debug_info_p)
{
  tree type = make_node (RECORD_TYPE);
  tree template_field
    = create_field_decl (get_identifier ("BOUNDS"), template_type, type,
			 NULL_TREE, NULL_TREE, 0, 1);
  tree array_field
    = create_field_decl (get_identifier ("ARRAY"), object_type, type,
			 NULL_TREE, NULL_TREE, 0, 1);

  TYPE_NAME (type) = name;
  TYPE_CONTAINS_TEMPLATE_P (type) = 1;
  DECL_CHAIN (template_field) = array_field;
  finish_record_type (type, template_field, 0, true);

  /* Declare it now since it will never be declared otherwise.  This is
     necessary to ensure that its subtrees are properly marked.  */
  create_type_decl (name, type, true, debug_info_p, Empty);

  return type;
}

/* Same, taking a thin or fat pointer type instead of a template type. */

tree
build_unc_object_type_from_ptr (tree thin_fat_ptr_type, tree object_type,
				tree name, bool debug_info_p)
{
  tree template_type;

  gcc_assert (TYPE_IS_FAT_OR_THIN_POINTER_P (thin_fat_ptr_type));

  template_type
    = (TYPE_IS_FAT_POINTER_P (thin_fat_ptr_type)
       ? TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (thin_fat_ptr_type))))
       : TREE_TYPE (TYPE_FIELDS (TREE_TYPE (thin_fat_ptr_type))));

  return
    build_unc_object_type (template_type, object_type, name, debug_info_p);
}

/* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE.
   In the normal case this is just two adjustments, but we have more to
   do if NEW_TYPE is an UNCONSTRAINED_ARRAY_TYPE.  */

void
update_pointer_to (tree old_type, tree new_type)
{
  tree ptr = TYPE_POINTER_TO (old_type);
  tree ref = TYPE_REFERENCE_TO (old_type);
  tree t;

  /* If this is the main variant, process all the other variants first.  */
  if (TYPE_MAIN_VARIANT (old_type) == old_type)
    for (t = TYPE_NEXT_VARIANT (old_type); t; t = TYPE_NEXT_VARIANT (t))
      update_pointer_to (t, new_type);

  /* If no pointers and no references, we are done.  */
  if (!ptr && !ref)
    return;

  /* Merge the old type qualifiers in the new type.

     Each old variant has qualifiers for specific reasons, and the new
     designated type as well.  Each set of qualifiers represents useful
     information grabbed at some point, and merging the two simply unifies
     these inputs into the final type description.

     Consider for instance a volatile type frozen after an access to constant
     type designating it; after the designated type's freeze, we get here with
     a volatile NEW_TYPE and a dummy OLD_TYPE with a readonly variant, created
     when the access type was processed.  We will make a volatile and readonly
     designated type, because that's what it really is.

     We might also get here for a non-dummy OLD_TYPE variant with different
     qualifiers than those of NEW_TYPE, for instance in some cases of pointers
     to private record type elaboration (see the comments around the call to
     this routine in gnat_to_gnu_entity <E_Access_Type>).  We have to merge
     the qualifiers in those cases too, to avoid accidentally discarding the
     initial set, and will often end up with OLD_TYPE == NEW_TYPE then.  */
  new_type
    = build_qualified_type (new_type,
			    TYPE_QUALS (old_type) | TYPE_QUALS (new_type));

  /* If old type and new type are identical, there is nothing to do.  */
  if (old_type == new_type)
    return;

  /* Otherwise, first handle the simple case.  */
  if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
    {
      tree new_ptr, new_ref;

      /* If pointer or reference already points to new type, nothing to do.
	 This can happen as update_pointer_to can be invoked multiple times
	 on the same couple of types because of the type variants.  */
      if ((ptr && TREE_TYPE (ptr) == new_type)
	  || (ref && TREE_TYPE (ref) == new_type))
	return;

      /* Chain PTR and its variants at the end.  */
      new_ptr = TYPE_POINTER_TO (new_type);
      if (new_ptr)
	{
	  while (TYPE_NEXT_PTR_TO (new_ptr))
	    new_ptr = TYPE_NEXT_PTR_TO (new_ptr);
	  TYPE_NEXT_PTR_TO (new_ptr) = ptr;
	}
      else
	TYPE_POINTER_TO (new_type) = ptr;

      /* Now adjust them.  */
      for (; ptr; ptr = TYPE_NEXT_PTR_TO (ptr))
	for (t = TYPE_MAIN_VARIANT (ptr); t; t = TYPE_NEXT_VARIANT (t))
	  {
	    TREE_TYPE (t) = new_type;
	    if (TYPE_NULL_BOUNDS (t))
	      TREE_TYPE (TREE_OPERAND (TYPE_NULL_BOUNDS (t), 0)) = new_type;
	    TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_POINTER_TO (new_type));
	  }

      /* Chain REF and its variants at the end.  */
      new_ref = TYPE_REFERENCE_TO (new_type);
      if (new_ref)
	{
	  while (TYPE_NEXT_REF_TO (new_ref))
	    new_ref = TYPE_NEXT_REF_TO (new_ref);
	  TYPE_NEXT_REF_TO (new_ref) = ref;
	}
      else
	TYPE_REFERENCE_TO (new_type) = ref;

      /* Now adjust them.  */
      for (; ref; ref = TYPE_NEXT_REF_TO (ref))
	for (t = TYPE_MAIN_VARIANT (ref); t; t = TYPE_NEXT_VARIANT (t))
	  {
	    TREE_TYPE (t) = new_type;
	    TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_REFERENCE_TO (new_type));
	  }

      TYPE_POINTER_TO (old_type) = NULL_TREE;
      TYPE_REFERENCE_TO (old_type) = NULL_TREE;
    }

  /* Now deal with the unconstrained array case.  In this case the pointer
     is actually a record where both fields are pointers to dummy nodes.
     Turn them into pointers to the correct types using update_pointer_to.
     Likewise for the pointer to the object record (thin pointer).  */
  else
    {
      tree new_ptr = TYPE_POINTER_TO (new_type);

      gcc_assert (TYPE_IS_FAT_POINTER_P (ptr));

      /* If PTR already points to NEW_TYPE, nothing to do.  This can happen
	 since update_pointer_to can be invoked multiple times on the same
	 couple of types because of the type variants.  */
      if (TYPE_UNCONSTRAINED_ARRAY (ptr) == new_type)
	return;

      update_pointer_to
	(TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
	 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (new_ptr))));

      update_pointer_to
	(TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (ptr)))),
	 TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (new_ptr)))));

      update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type),
			 TYPE_OBJECT_RECORD_TYPE (new_type));

      TYPE_POINTER_TO (old_type) = NULL_TREE;
      TYPE_REFERENCE_TO (old_type) = NULL_TREE;
    }
}

/* Convert EXPR, a pointer to a constrained array, into a pointer to an
   unconstrained one.  This involves making or finding a template.  */

static tree
convert_to_fat_pointer (tree type, tree expr)
{
  tree template_type = TREE_TYPE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))));
  tree p_array_type = TREE_TYPE (TYPE_FIELDS (type));
  tree etype = TREE_TYPE (expr);
  tree template_addr;
  vec<constructor_elt, va_gc> *v;
  vec_alloc (v, 2);

  /* If EXPR is null, make a fat pointer that contains a null pointer to the
     array (compare_fat_pointers ensures that this is the full discriminant)
     and a valid pointer to the bounds.  This latter property is necessary
     since the compiler can hoist the load of the bounds done through it.  */
  if (integer_zerop (expr))
    {
      tree ptr_template_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
      tree null_bounds, t;

      if (TYPE_NULL_BOUNDS (ptr_template_type))
	null_bounds = TYPE_NULL_BOUNDS (ptr_template_type);
      else
	{
	  /* The template type can still be dummy at this point so we build an
	     empty constructor.  The middle-end will fill it in with zeros.  */
	  t = build_constructor (template_type, NULL);
	  TREE_CONSTANT (t) = TREE_STATIC (t) = 1;
	  null_bounds = build_unary_op (ADDR_EXPR, NULL_TREE, t);
	  SET_TYPE_NULL_BOUNDS (ptr_template_type, null_bounds);
	}

      CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
			      fold_convert (p_array_type, null_pointer_node));
      CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), null_bounds);
      t = build_constructor (type, v);
      /* Do not set TREE_CONSTANT so as to force T to static memory.  */
      TREE_CONSTANT (t) = 0;
      TREE_STATIC (t) = 1;

      return t;
    }

  /* If EXPR is a thin pointer, make template and data from the record.  */
  if (TYPE_IS_THIN_POINTER_P (etype))
    {
      tree field = TYPE_FIELDS (TREE_TYPE (etype));

      expr = gnat_protect_expr (expr);

      /* If we have a TYPE_UNCONSTRAINED_ARRAY attached to the RECORD_TYPE,
	 the thin pointer value has been shifted so we shift it back to get
	 the template address.  */
      if (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype)))
	{
	  template_addr
	    = build_binary_op (POINTER_PLUS_EXPR, etype, expr,
			       fold_build1 (NEGATE_EXPR, sizetype,
					    byte_position
					    (DECL_CHAIN (field))));
	  template_addr
	    = fold_convert (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))),
			    template_addr);
	}

      /* Otherwise we explicitly take the address of the fields.  */
      else
	{
	  expr = build_unary_op (INDIRECT_REF, NULL_TREE, expr);
	  template_addr
	    = build_unary_op (ADDR_EXPR, NULL_TREE,
			      build_component_ref (expr, field, false));
	  expr = build_unary_op (ADDR_EXPR, NULL_TREE,
				 build_component_ref (expr, DECL_CHAIN (field),
						      false));
	}
    }

  /* Otherwise, build the constructor for the template.  */
  else
    template_addr
      = build_unary_op (ADDR_EXPR, NULL_TREE,
			build_template (template_type, TREE_TYPE (etype),
					expr));

  /* The final result is a constructor for the fat pointer.

     If EXPR is an argument of a foreign convention subprogram, the type it
     points to is directly the component type.  In this case, the expression
     type may not match the corresponding FIELD_DECL type at this point, so we
     call "convert" here to fix that up if necessary.  This type consistency is
     required, for instance because it ensures that possible later folding of
     COMPONENT_REFs against this constructor always yields something of the
     same type as the initial reference.

     Note that the call to "build_template" above is still fine because it
     will only refer to the provided TEMPLATE_TYPE in this case.  */
  CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type), convert (p_array_type, expr));
  CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)), template_addr);
  return gnat_build_constructor (type, v);
}

/* Create an expression whose value is that of EXPR,
   converted to type TYPE.  The TREE_TYPE of the value
   is always TYPE.  This function implements all reasonable
   conversions; callers should filter out those that are
   not permitted by the language being compiled.  */

tree
convert (tree type, tree expr)
{
  tree etype = TREE_TYPE (expr);
  enum tree_code ecode = TREE_CODE (etype);
  enum tree_code code = TREE_CODE (type);

  /* If the expression is already of the right type, we are done.  */
  if (etype == type)
    return expr;

  /* If both input and output have padding and are of variable size, do this
     as an unchecked conversion.  Likewise if one is a mere variant of the
     other, so we avoid a pointless unpad/repad sequence.  */
  else if (code == RECORD_TYPE && ecode == RECORD_TYPE
	   && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
	   && (!TREE_CONSTANT (TYPE_SIZE (type))
	       || !TREE_CONSTANT (TYPE_SIZE (etype))
	       || TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
	       || TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type)))
		  == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (etype)))))
    ;

  /* If the output type has padding, convert to the inner type and make a
     constructor to build the record, unless a variable size is involved.  */
  else if (code == RECORD_TYPE && TYPE_PADDING_P (type))
    {
      /* If we previously converted from another type and our type is
	 of variable size, remove the conversion to avoid the need for
	 variable-sized temporaries.  Likewise for a conversion between
	 original and packable version.  */
      if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
	  && (!TREE_CONSTANT (TYPE_SIZE (type))
	      || (ecode == RECORD_TYPE
		  && TYPE_NAME (etype)
		     == TYPE_NAME (TREE_TYPE (TREE_OPERAND (expr, 0))))))
	expr = TREE_OPERAND (expr, 0);

      /* If we are just removing the padding from expr, convert the original
	 object if we have variable size in order to avoid the need for some
	 variable-sized temporaries.  Likewise if the padding is a variant
	 of the other, so we avoid a pointless unpad/repad sequence.  */
      if (TREE_CODE (expr) == COMPONENT_REF
	  && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
	  && (!TREE_CONSTANT (TYPE_SIZE (type))
	      || TYPE_MAIN_VARIANT (type)
		 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))
	      || (ecode == RECORD_TYPE
		  && TYPE_NAME (etype)
		     == TYPE_NAME (TREE_TYPE (TYPE_FIELDS (type))))))
	return convert (type, TREE_OPERAND (expr, 0));

      /* If the inner type is of self-referential size and the expression type
	 is a record, do this as an unchecked conversion unless both types are
	 essentially the same.  */
      if (ecode == RECORD_TYPE
	  && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type)))
	  && TYPE_MAIN_VARIANT (etype)
	     != TYPE_MAIN_VARIANT (TREE_TYPE (TYPE_FIELDS (type))))
	return unchecked_convert (type, expr, false);

      /* If we are converting between array types with variable size, do the
	 final conversion as an unchecked conversion, again to avoid the need
	 for some variable-sized temporaries.  If valid, this conversion is
	 very likely purely technical and without real effects.  */
      if (ecode == ARRAY_TYPE
	  && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == ARRAY_TYPE
	  && !TREE_CONSTANT (TYPE_SIZE (etype))
	  && !TREE_CONSTANT (TYPE_SIZE (type)))
	return unchecked_convert (type,
				  convert (TREE_TYPE (TYPE_FIELDS (type)),
					   expr),
				  false);

      tree t = convert (TREE_TYPE (TYPE_FIELDS (type)), expr);

      /* If converting to the inner type has already created a CONSTRUCTOR with
         the right size, then reuse it instead of creating another one.  This
         can happen for the padding type built to overalign local variables.  */
      if (TREE_CODE (t) == VIEW_CONVERT_EXPR
	  && TREE_CODE (TREE_OPERAND (t, 0)) == CONSTRUCTOR
	  && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (t, 0))))
	  && tree_int_cst_equal (TYPE_SIZE (type),
				 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (t, 0)))))
	return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (t, 0));

      vec<constructor_elt, va_gc> *v;
      vec_alloc (v, 1);
      CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type), t);
      return gnat_build_constructor (type, v);
    }

  /* If the input type has padding, remove it and convert to the output type.
     The conditions ordering is arranged to ensure that the output type is not
     a padding type here, as it is not clear whether the conversion would
     always be correct if this was to happen.  */
  else if (ecode == RECORD_TYPE && TYPE_PADDING_P (etype))
    {
      tree unpadded;

      /* If we have just converted to this padded type, just get the
	 inner expression.  */
      if (TREE_CODE (expr) == CONSTRUCTOR)
	unpadded = CONSTRUCTOR_ELT (expr, 0)->value;

      /* Otherwise, build an explicit component reference.  */
      else
	unpadded = build_component_ref (expr, TYPE_FIELDS (etype), false);

      return convert (type, unpadded);
    }

  /* If the input is a biased type, convert first to the base type and add
     the bias.  Note that the bias must go through a full conversion to the
     base type, lest it is itself a biased value; this happens for subtypes
     of biased types.  */
  if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
    return convert (type, fold_build2 (PLUS_EXPR, TREE_TYPE (etype),
				       fold_convert (TREE_TYPE (etype), expr),
				       convert (TREE_TYPE (etype),
						TYPE_MIN_VALUE (etype))));

  /* If the input is a justified modular type, we need to extract the actual
     object before converting it to an other type with the exceptions of an
     [unconstrained] array or a mere type variant.  It is useful to avoid
     the extraction and conversion in these cases because it could end up
     replacing a VAR_DECL by a constructor and we might be about the take
     the address of the result.  */
  if (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)
      && code != ARRAY_TYPE
      && code != UNCONSTRAINED_ARRAY_TYPE
      && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (etype))
    return
      convert (type, build_component_ref (expr, TYPE_FIELDS (etype), false));

  /* If converting to a type that contains a template, convert to the data
     type and then build the template. */
  if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type))
    {
      tree obj_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
      vec<constructor_elt, va_gc> *v;
      vec_alloc (v, 2);

      /* If the source already has a template, get a reference to the
	 associated array only, as we are going to rebuild a template
	 for the target type anyway.  */
      expr = maybe_unconstrained_array (expr);

      CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
			      build_template (TREE_TYPE (TYPE_FIELDS (type)),
					      obj_type, NULL_TREE));
      if (expr)
	CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (type)),
				convert (obj_type, expr));
      return gnat_build_constructor (type, v);
    }

  /* There are some cases of expressions that we process specially.  */
  switch (TREE_CODE (expr))
    {
    case ERROR_MARK:
      return expr;

    case NULL_EXPR:
      /* Just set its type here.  For TRANSFORM_EXPR, we will do the actual
	 conversion in gnat_expand_expr.  NULL_EXPR does not represent
	 and actual value, so no conversion is needed.  */
      expr = copy_node (expr);
      TREE_TYPE (expr) = type;
      return expr;

    case STRING_CST:
      /* If we are converting a STRING_CST to another constrained array type,
	 just make a new one in the proper type.  */
      if (code == ecode
	  && !(TREE_CONSTANT (TYPE_SIZE (etype))
	       && !TREE_CONSTANT (TYPE_SIZE (type))))
	{
	  expr = copy_node (expr);
	  TREE_TYPE (expr) = type;
	  return expr;
	}
      break;

    case VECTOR_CST:
      /* If we are converting a VECTOR_CST to a mere type variant, just make
	 a new one in the proper type.  */
      if (code == ecode && gnat_types_compatible_p (type, etype))
	{
	  expr = copy_node (expr);
	  TREE_TYPE (expr) = type;
	  return expr;
	}
      break;

    case CONSTRUCTOR:
      /* If we are converting a CONSTRUCTOR to a mere type variant, or to
	 another padding type around the same type, just make a new one in
	 the proper type.  */
      if (code == ecode
	  && (gnat_types_compatible_p (type, etype)
	      || (code == RECORD_TYPE
		  && TYPE_PADDING_P (type) && TYPE_PADDING_P (etype)
		  && TREE_TYPE (TYPE_FIELDS (type))
		     == TREE_TYPE (TYPE_FIELDS (etype)))))
	{
	  expr = copy_node (expr);
	  TREE_TYPE (expr) = type;
	  CONSTRUCTOR_ELTS (expr) = vec_safe_copy (CONSTRUCTOR_ELTS (expr));
	  return expr;
	}

      /* Likewise for a conversion between original and packable version, or
	 conversion between types of the same size and with the same list of
	 fields, but we have to work harder to preserve type consistency.  */
      if (code == ecode
	  && code == RECORD_TYPE
	  && (TYPE_NAME (type) == TYPE_NAME (etype)
	      || tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (etype))))

	{
	  vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
	  unsigned HOST_WIDE_INT len = vec_safe_length (e);
	  vec<constructor_elt, va_gc> *v;
	  vec_alloc (v, len);
	  tree efield = TYPE_FIELDS (etype), field = TYPE_FIELDS (type);
	  unsigned HOST_WIDE_INT idx;
	  tree index, value;

	  /* Whether we need to clear TREE_CONSTANT et al. on the output
	     constructor when we convert in place.  */
	  bool clear_constant = false;

	  FOR_EACH_CONSTRUCTOR_ELT(e, idx, index, value)
	    {
	      /* Skip the missing fields in the CONSTRUCTOR.  */
	      while (efield && field && !SAME_FIELD_P (efield, index))
	        {
		  efield = DECL_CHAIN (efield);
		  field = DECL_CHAIN (field);
		}
	      /* The field must be the same.  */
	      if (!(efield && field && SAME_FIELD_P (efield, field)))
		break;
	      constructor_elt elt
	        = {field, convert (TREE_TYPE (field), value)};
	      v->quick_push (elt);

	      /* If packing has made this field a bitfield and the input
		 value couldn't be emitted statically any more, we need to
		 clear TREE_CONSTANT on our output.  */
	      if (!clear_constant
		  && TREE_CONSTANT (expr)
		  && !CONSTRUCTOR_BITFIELD_P (efield)
		  && CONSTRUCTOR_BITFIELD_P (field)
		  && !initializer_constant_valid_for_bitfield_p (value))
		clear_constant = true;

	      efield = DECL_CHAIN (efield);
	      field = DECL_CHAIN (field);
	    }

	  /* If we have been able to match and convert all the input fields
	     to their output type, convert in place now.  We'll fallback to a
	     view conversion downstream otherwise.  */
	  if (idx == len)
	    {
	      expr = copy_node (expr);
	      TREE_TYPE (expr) = type;
	      CONSTRUCTOR_ELTS (expr) = v;
	      if (clear_constant)
		TREE_CONSTANT (expr) = TREE_STATIC (expr) = 0;
	      return expr;
	    }
	}

      /* Likewise for a conversion between array type and vector type with a
         compatible representative array.  */
      else if (code == VECTOR_TYPE
	       && ecode == ARRAY_TYPE
	       && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
					   etype))
	{
	  vec<constructor_elt, va_gc> *e = CONSTRUCTOR_ELTS (expr);
	  unsigned HOST_WIDE_INT len = vec_safe_length (e);
	  vec<constructor_elt, va_gc> *v;
	  unsigned HOST_WIDE_INT ix;
	  tree value;

	  /* Build a VECTOR_CST from a *constant* array constructor.  */
	  if (TREE_CONSTANT (expr))
	    {
	      bool constant_p = true;

	      /* Iterate through elements and check if all constructor
		 elements are *_CSTs.  */
	      FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
		if (!CONSTANT_CLASS_P (value))
		  {
		    constant_p = false;
		    break;
		  }

	      if (constant_p)
		return build_vector_from_ctor (type,
					       CONSTRUCTOR_ELTS (expr));
	    }

	  /* Otherwise, build a regular vector constructor.  */
	  vec_alloc (v, len);
	  FOR_EACH_CONSTRUCTOR_VALUE (e, ix, value)
	    {
	      constructor_elt elt = {NULL_TREE, value};
	      v->quick_push (elt);
	    }
	  expr = copy_node (expr);
	  TREE_TYPE (expr) = type;
	  CONSTRUCTOR_ELTS (expr) = v;
	  return expr;
	}
      break;

    case UNCONSTRAINED_ARRAY_REF:
      /* First retrieve the underlying array.  */
      expr = maybe_unconstrained_array (expr);
      etype = TREE_TYPE (expr);
      ecode = TREE_CODE (etype);
      break;

    case VIEW_CONVERT_EXPR:
      {
	/* GCC 4.x is very sensitive to type consistency overall, and view
	   conversions thus are very frequent.  Even though just "convert"ing
	   the inner operand to the output type is fine in most cases, it
	   might expose unexpected input/output type mismatches in special
	   circumstances so we avoid such recursive calls when we can.  */
	tree op0 = TREE_OPERAND (expr, 0);

	/* If we are converting back to the original type, we can just
	   lift the input conversion.  This is a common occurrence with
	   switches back-and-forth amongst type variants.  */
	if (type == TREE_TYPE (op0))
	  return op0;

	/* Otherwise, if we're converting between two aggregate or vector
	   types, we might be allowed to substitute the VIEW_CONVERT_EXPR
	   target type in place or to just convert the inner expression.  */
	if ((AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype))
	    || (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (etype)))
	  {
	    /* If we are converting between mere variants, we can just
	       substitute the VIEW_CONVERT_EXPR in place.  */
	    if (gnat_types_compatible_p (type, etype))
	      return build1 (VIEW_CONVERT_EXPR, type, op0);

	    /* Otherwise, we may just bypass the input view conversion unless
	       one of the types is a fat pointer,  which is handled by
	       specialized code below which relies on exact type matching.  */
	    else if (!TYPE_IS_FAT_POINTER_P (type)
		     && !TYPE_IS_FAT_POINTER_P (etype))
	      return convert (type, op0);
	  }

	break;
      }

    default:
      break;
    }

  /* Check for converting to a pointer to an unconstrained array.  */
  if (TYPE_IS_FAT_POINTER_P (type) && !TYPE_IS_FAT_POINTER_P (etype))
    return convert_to_fat_pointer (type, expr);

  /* If we are converting between two aggregate or vector types that are mere
     variants, just make a VIEW_CONVERT_EXPR.  Likewise when we are converting
     to a vector type from its representative array type.  */
  else if ((code == ecode
	    && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
	    && gnat_types_compatible_p (type, etype))
	   || (code == VECTOR_TYPE
	       && ecode == ARRAY_TYPE
	       && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
					   etype)))
    return build1 (VIEW_CONVERT_EXPR, type, expr);

  /* If we are converting between tagged types, try to upcast properly.
     But don't do it if we are just annotating types since tagged types
     aren't fully laid out in this mode.  */
  else if (ecode == RECORD_TYPE && code == RECORD_TYPE
	   && TYPE_ALIGN_OK (etype) && TYPE_ALIGN_OK (type)
	   && !type_annotate_only)
    {
      tree child_etype = etype;
      do {
	tree field = TYPE_FIELDS (child_etype);
	if (DECL_NAME (field) == parent_name_id && TREE_TYPE (field) == type)
	  return build_component_ref (expr, field, false);
	child_etype = TREE_TYPE (field);
      } while (TREE_CODE (child_etype) == RECORD_TYPE);
    }

  /* If we are converting from a smaller form of record type back to it, just
     make a VIEW_CONVERT_EXPR.  But first pad the expression to have the same
     size on both sides.  */
  else if (ecode == RECORD_TYPE && code == RECORD_TYPE
	   && smaller_form_type_p (etype, type))
    {
      expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
				      false, false, true),
		      expr);
      return build1 (VIEW_CONVERT_EXPR, type, expr);
    }

  /* In all other cases of related types, make a NOP_EXPR.  */
  else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype))
    return fold_convert (type, expr);

  switch (code)
    {
    case VOID_TYPE:
      return fold_build1 (CONVERT_EXPR, type, expr);

    case INTEGER_TYPE:
      if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
	  && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE
	      || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))))
	return unchecked_convert (type, expr, false);

      /* If the output is a biased type, convert first to the base type and
	 subtract the bias.  Note that the bias itself must go through a full
	 conversion to the base type, lest it is a biased value; this happens
	 for subtypes of biased types.  */
      if (TYPE_BIASED_REPRESENTATION_P (type))
	return fold_convert (type,
			     fold_build2 (MINUS_EXPR, TREE_TYPE (type),
					  convert (TREE_TYPE (type), expr),
					  convert (TREE_TYPE (type),
						   TYPE_MIN_VALUE (type))));

      /* If we are converting an additive expression to an integer type
	 with lower precision, be wary of the optimization that can be
	 applied by convert_to_integer.  There are 2 problematic cases:
	   - if the first operand was originally of a biased type,
	     because we could be recursively called to convert it
	     to an intermediate type and thus rematerialize the
	     additive operator endlessly,
	   - if the expression contains a placeholder, because an
	     intermediate conversion that changes the sign could
	     be inserted and thus introduce an artificial overflow
	     at compile time when the placeholder is substituted.  */
      if (ecode == INTEGER_TYPE
	  && TYPE_PRECISION (type) < TYPE_PRECISION (etype)
	  && (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR))
	{
	  tree op0 = get_unwidened (TREE_OPERAND (expr, 0), type);

	  if ((TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
	       && TYPE_BIASED_REPRESENTATION_P (TREE_TYPE (op0)))
	      || CONTAINS_PLACEHOLDER_P (expr))
	    return fold_convert (type, expr);
	}

      /* ... fall through ... */

    case ENUMERAL_TYPE:
      return fold (convert_to_integer (type, expr));

    case BOOLEAN_TYPE:
      /* Do not use convert_to_integer with boolean types.  */
      return fold_convert_loc (EXPR_LOCATION (expr), type, expr);

    case POINTER_TYPE:
    case REFERENCE_TYPE:
      /* If converting between two thin pointers, adjust if needed to account
	 for differing offsets from the base pointer, depending on whether
	 there is a TYPE_UNCONSTRAINED_ARRAY attached to the record type.  */
      if (TYPE_IS_THIN_POINTER_P (etype) && TYPE_IS_THIN_POINTER_P (type))
	{
	  tree etype_pos
	    = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (etype))
	      ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (etype))))
	      : size_zero_node;
	  tree type_pos
	    = TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))
	      ? byte_position (DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (type))))
	      : size_zero_node;
	  tree byte_diff = size_diffop (type_pos, etype_pos);

	  expr = build1 (NOP_EXPR, type, expr);
	  if (integer_zerop (byte_diff))
	    return expr;

	  return build_binary_op (POINTER_PLUS_EXPR, type, expr,
				  fold_convert (sizetype, byte_diff));
	}

      /* If converting fat pointer to normal or thin pointer, get the pointer
	 to the array and then convert it.  */
      if (TYPE_IS_FAT_POINTER_P (etype))
	expr = build_component_ref (expr, TYPE_FIELDS (etype), false);

      return fold (convert_to_pointer (type, expr));

    case REAL_TYPE:
      return fold (convert_to_real (type, expr));

    case RECORD_TYPE:
      /* Do a normal conversion between scalar and justified modular type.  */
      if (TYPE_JUSTIFIED_MODULAR_P (type) && !AGGREGATE_TYPE_P (etype))
	{
	  vec<constructor_elt, va_gc> *v;
	  vec_alloc (v, 1);

	  CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (type),
				  convert (TREE_TYPE (TYPE_FIELDS (type)),
					   expr));
	  return gnat_build_constructor (type, v);
	}

      /* In these cases, assume the front-end has validated the conversion.
	 If the conversion is valid, it will be a bit-wise conversion, so
	 it can be viewed as an unchecked conversion.  */
      return unchecked_convert (type, expr, false);

    case ARRAY_TYPE:
      /* Do a normal conversion between unconstrained and constrained array
	 type, assuming the latter is a constrained version of the former.  */
      if (TREE_CODE (expr) == INDIRECT_REF
	  && ecode == ARRAY_TYPE
	  && TREE_TYPE (etype) == TREE_TYPE (type))
	{
	  tree ptr_type = build_pointer_type (type);
	  tree t = build_unary_op (INDIRECT_REF, NULL_TREE,
				   fold_convert (ptr_type,
						 TREE_OPERAND (expr, 0)));
	  TREE_READONLY (t) = TREE_READONLY (expr);
	  TREE_THIS_NOTRAP (t) = TREE_THIS_NOTRAP (expr);
	  return t;
	}

      /* In these cases, assume the front-end has validated the conversion.
	 If the conversion is valid, it will be a bit-wise conversion, so
	 it can be viewed as an unchecked conversion.  */
      return unchecked_convert (type, expr, false);

    case UNION_TYPE:
      /* This is a either a conversion between a tagged type and some
	 subtype, which we have to mark as a UNION_TYPE because of
	 overlapping fields or a conversion of an Unchecked_Union.  */
      return unchecked_convert (type, expr, false);

    case UNCONSTRAINED_ARRAY_TYPE:
      /* If the input is a VECTOR_TYPE, convert to the representative
	 array type first.  */
      if (ecode == VECTOR_TYPE)
	{
	  expr = convert (TYPE_REPRESENTATIVE_ARRAY (etype), expr);
	  etype = TREE_TYPE (expr);
	  ecode = TREE_CODE (etype);
	}

      /* If EXPR is a constrained array, take its address, convert it to a
	 fat pointer, and then dereference it.  Likewise if EXPR is a
	 record containing both a template and a constrained array.
	 Note that a record representing a justified modular type
	 always represents a packed constrained array.  */
      if (ecode == ARRAY_TYPE
	  || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
	  || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
	  || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype)))
	return
	  build_unary_op
	    (INDIRECT_REF, NULL_TREE,
	     convert_to_fat_pointer (TREE_TYPE (type),
				     build_unary_op (ADDR_EXPR,
						     NULL_TREE, expr)));

      /* Do something very similar for converting one unconstrained
	 array to another.  */
      else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
	return
	  build_unary_op (INDIRECT_REF, NULL_TREE,
			  convert (TREE_TYPE (type),
				   build_unary_op (ADDR_EXPR,
						   NULL_TREE, expr)));
      else
	gcc_unreachable ();

    case COMPLEX_TYPE:
      return fold (convert_to_complex (type, expr));

    default:
      gcc_unreachable ();
    }
}

/* Create an expression whose value is that of EXPR converted to the common
   index type, which is sizetype.  EXPR is supposed to be in the base type
   of the GNAT index type.  Calling it is equivalent to doing

     convert (sizetype, expr)

   but we try to distribute the type conversion with the knowledge that EXPR
   cannot overflow in its type.  This is a best-effort approach and we fall
   back to the above expression as soon as difficulties are encountered.

   This is necessary to overcome issues that arise when the GNAT base index
   type and the GCC common index type (sizetype) don't have the same size,
   which is quite frequent on 64-bit architectures.  In this case, and if
   the GNAT base index type is signed but the iteration type of the loop has
   been forced to unsigned, the loop scalar evolution engine cannot compute
   a simple evolution for the general induction variables associated with the
   array indices, because it will preserve the wrap-around semantics in the
   unsigned type of their "inner" part.  As a result, many loop optimizations
   are blocked.

   The solution is to use a special (basic) induction variable that is at
   least as large as sizetype, and to express the aforementioned general
   induction variables in terms of this induction variable, eliminating
   the problematic intermediate truncation to the GNAT base index type.
   This is possible as long as the original expression doesn't overflow
   and if the middle-end hasn't introduced artificial overflows in the
   course of the various simplification it can make to the expression.  */

tree
convert_to_index_type (tree expr)
{
  enum tree_code code = TREE_CODE (expr);
  tree type = TREE_TYPE (expr);

  /* If the type is unsigned, overflow is allowed so we cannot be sure that
     EXPR doesn't overflow.  Keep it simple if optimization is disabled.  */
  if (TYPE_UNSIGNED (type) || !optimize || optimize_debug)
    return convert (sizetype, expr);

  switch (code)
    {
    case VAR_DECL:
      /* The main effect of the function: replace a loop parameter with its
	 associated special induction variable.  */
      if (DECL_LOOP_PARM_P (expr) && DECL_INDUCTION_VAR (expr))
	expr = DECL_INDUCTION_VAR (expr);
      break;

    CASE_CONVERT:
      {
	tree otype = TREE_TYPE (TREE_OPERAND (expr, 0));
	/* Bail out as soon as we suspect some sort of type frobbing.  */
	if (TYPE_PRECISION (type) != TYPE_PRECISION (otype)
	    || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (otype))
	  break;
      }

      /* ... fall through ... */

    case NON_LVALUE_EXPR:
      return fold_build1 (code, sizetype,
			  convert_to_index_type (TREE_OPERAND (expr, 0)));

    case PLUS_EXPR:
    case MINUS_EXPR:
    case MULT_EXPR:
      return fold_build2 (code, sizetype,
			  convert_to_index_type (TREE_OPERAND (expr, 0)),
			  convert_to_index_type (TREE_OPERAND (expr, 1)));

    case COMPOUND_EXPR:
      return fold_build2 (code, sizetype, TREE_OPERAND (expr, 0),
			  convert_to_index_type (TREE_OPERAND (expr, 1)));

    case COND_EXPR:
      return fold_build3 (code, sizetype, TREE_OPERAND (expr, 0),
			  convert_to_index_type (TREE_OPERAND (expr, 1)),
			  convert_to_index_type (TREE_OPERAND (expr, 2)));

    default:
      break;
    }

  return convert (sizetype, expr);
}

/* Remove all conversions that are done in EXP.  This includes converting
   from a padded type or to a justified modular type.  If TRUE_ADDRESS
   is true, always return the address of the containing object even if
   the address is not bit-aligned.  */

tree
remove_conversions (tree exp, bool true_address)
{
  switch (TREE_CODE (exp))
    {
    case CONSTRUCTOR:
      if (true_address
	  && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
	  && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
	return
	  remove_conversions (CONSTRUCTOR_ELT (exp, 0)->value, true);
      break;

    case COMPONENT_REF:
      if (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
	return remove_conversions (TREE_OPERAND (exp, 0), true_address);
      break;

    CASE_CONVERT:
    case VIEW_CONVERT_EXPR:
    case NON_LVALUE_EXPR:
      return remove_conversions (TREE_OPERAND (exp, 0), true_address);

    default:
      break;
    }

  return exp;
}

/* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
   refers to the underlying array.  If it has TYPE_CONTAINS_TEMPLATE_P,
   likewise return an expression pointing to the underlying array.  */

tree
maybe_unconstrained_array (tree exp)
{
  enum tree_code code = TREE_CODE (exp);
  tree type = TREE_TYPE (exp);

  switch (TREE_CODE (type))
    {
    case UNCONSTRAINED_ARRAY_TYPE:
      if (code == UNCONSTRAINED_ARRAY_REF)
	{
	  const bool read_only = TREE_READONLY (exp);
	  const bool no_trap = TREE_THIS_NOTRAP (exp);

	  exp = TREE_OPERAND (exp, 0);
	  type = TREE_TYPE (exp);

	  if (TREE_CODE (exp) == COND_EXPR)
	    {
	      tree op1
		= build_unary_op (INDIRECT_REF, NULL_TREE,
				  build_component_ref (TREE_OPERAND (exp, 1),
						       TYPE_FIELDS (type),
						       false));
	      tree op2
		= build_unary_op (INDIRECT_REF, NULL_TREE,
				  build_component_ref (TREE_OPERAND (exp, 2),
						       TYPE_FIELDS (type),
						       false));

	      exp = build3 (COND_EXPR,
			    TREE_TYPE (TREE_TYPE (TYPE_FIELDS (type))),
			    TREE_OPERAND (exp, 0), op1, op2);
	    }
	  else
	    {
	      exp = build_unary_op (INDIRECT_REF, NULL_TREE,
				    build_component_ref (exp,
							 TYPE_FIELDS (type),
						         false));
	      TREE_READONLY (exp) = read_only;
	      TREE_THIS_NOTRAP (exp) = no_trap;
	    }
	}

      else if (code == NULL_EXPR)
	exp = build1 (NULL_EXPR,
		      TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type)))),
		      TREE_OPERAND (exp, 0));
      break;

    case RECORD_TYPE:
      /* If this is a padded type and it contains a template, convert to the
	 unpadded type first.  */
      if (TYPE_PADDING_P (type)
	  && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
	  && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
	{
	  exp = convert (TREE_TYPE (TYPE_FIELDS (type)), exp);
	  code = TREE_CODE (exp);
	  type = TREE_TYPE (exp);
	}

      if (TYPE_CONTAINS_TEMPLATE_P (type))
	{
	  /* If the array initializer is a box, return NULL_TREE.  */
	  if (code == CONSTRUCTOR && CONSTRUCTOR_NELTS (exp) < 2)
	    return NULL_TREE;

	  exp = build_component_ref (exp, DECL_CHAIN (TYPE_FIELDS (type)),
				     false);

	  /* If the array is padded, remove the padding.  */
	  exp = maybe_padded_object (exp);
	}
      break;

    default:
      break;
    }

  return exp;
}

/* Return true if EXPR is an expression that can be folded as an operand
   of a VIEW_CONVERT_EXPR.  See ada-tree.h for a complete rationale.  */

static bool
can_fold_for_view_convert_p (tree expr)
{
  tree t1, t2;

  /* The folder will fold NOP_EXPRs between integral types with the same
     precision (in the middle-end's sense).  We cannot allow it if the
     types don't have the same precision in the Ada sense as well.  */
  if (TREE_CODE (expr) != NOP_EXPR)
    return true;

  t1 = TREE_TYPE (expr);
  t2 = TREE_TYPE (TREE_OPERAND (expr, 0));

  /* Defer to the folder for non-integral conversions.  */
  if (!(INTEGRAL_TYPE_P (t1) && INTEGRAL_TYPE_P (t2)))
    return true;

  /* Only fold conversions that preserve both precisions.  */
  if (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
      && operand_equal_p (rm_size (t1), rm_size (t2), 0))
    return true;

  return false;
}

/* Return an expression that does an unchecked conversion of EXPR to TYPE.
   If NOTRUNC_P is true, truncation operations should be suppressed.

   Special care is required with (source or target) integral types whose
   precision is not equal to their size, to make sure we fetch or assign
   the value bits whose location might depend on the endianness, e.g.

     Rmsize : constant := 8;
     subtype Int is Integer range 0 .. 2 ** Rmsize - 1;

     type Bit_Array is array (1 .. Rmsize) of Boolean;
     pragma Pack (Bit_Array);

     function To_Bit_Array is new Unchecked_Conversion (Int, Bit_Array);

     Value : Int := 2#1000_0001#;
     Vbits : Bit_Array := To_Bit_Array (Value);

   we expect the 8 bits at Vbits'Address to always contain Value, while
   their original location depends on the endianness, at Value'Address
   on a little-endian architecture but not on a big-endian one.

   One pitfall is that we cannot use TYPE_UNSIGNED directly to decide how
   the bits between the precision and the size are filled, because of the
   trick used in the E_Signed_Integer_Subtype case of gnat_to_gnu_entity.
   So we use the special predicate type_unsigned_for_rm above.  */

tree
unchecked_convert (tree type, tree expr, bool notrunc_p)
{
  tree etype = TREE_TYPE (expr);
  enum tree_code ecode = TREE_CODE (etype);
  enum tree_code code = TREE_CODE (type);
  const bool ebiased
    = (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype));
  const bool biased
    = (code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type));
  const bool ereverse
    = (AGGREGATE_TYPE_P (etype) && TYPE_REVERSE_STORAGE_ORDER (etype));
  const bool reverse
    = (AGGREGATE_TYPE_P (type) && TYPE_REVERSE_STORAGE_ORDER (type));
  tree tem;
  int c = 0;

  /* If the expression is already of the right type, we are done.  */
  if (etype == type)
    return expr;

  /* If both types are integral or regular pointer, then just do a normal
     conversion.  Likewise for a conversion to an unconstrained array.  */
  if (((INTEGRAL_TYPE_P (type)
	|| (POINTER_TYPE_P (type) && !TYPE_IS_THIN_POINTER_P (type))
	|| (code == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (type)))
       && (INTEGRAL_TYPE_P (etype)
	   || (POINTER_TYPE_P (etype) && !TYPE_IS_THIN_POINTER_P (etype))
	   || (ecode == RECORD_TYPE && TYPE_JUSTIFIED_MODULAR_P (etype))))
      || code == UNCONSTRAINED_ARRAY_TYPE)
    {
      if (ebiased)
	{
	  tree ntype = copy_type (etype);
	  TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
	  TYPE_MAIN_VARIANT (ntype) = ntype;
	  expr = build1 (NOP_EXPR, ntype, expr);
	}

      if (biased)
	{
	  tree rtype = copy_type (type);
	  TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
	  TYPE_MAIN_VARIANT (rtype) = rtype;
	  expr = convert (rtype, expr);
	  expr = build1 (NOP_EXPR, type, expr);
	}
      else
	expr = convert (type, expr);
    }

  /* If we are converting to an integral type whose precision is not equal
     to its size, first unchecked convert to a record type that contains a
     field of the given precision.  Then extract the result from the field.

     There is a subtlety if the source type is an aggregate type with reverse
     storage order because its representation is not contiguous in the native
     storage order, i.e. a direct unchecked conversion to an integral type
     with N bits of precision cannot read the first N bits of the aggregate
     type.  To overcome it, we do an unchecked conversion to an integral type
     with reverse storage order and return the resulting value.  This also
     ensures that the result of the unchecked conversion doesn't depend on
     the endianness of the target machine, but only on the storage order of
     the aggregate type.

     Finally, for the sake of consistency, we do the unchecked conversion
     to an integral type with reverse storage order as soon as the source
     type is an aggregate type with reverse storage order, even if there
     are no considerations of precision or size involved.  Ultimately, we
     further extend this processing to any scalar type.  */
  else if ((INTEGRAL_TYPE_P (type)
	    && TYPE_RM_SIZE (type)
	    && ((c = tree_int_cst_compare (TYPE_RM_SIZE (type),
					   TYPE_SIZE (type))) < 0
		|| ereverse))
	   || (SCALAR_FLOAT_TYPE_P (type) && ereverse))
    {
      tree rec_type = make_node (RECORD_TYPE);
      tree field_type, field;

      TYPE_REVERSE_STORAGE_ORDER (rec_type) = ereverse;

      if (c < 0)
	{
	  const unsigned HOST_WIDE_INT prec
	    = TREE_INT_CST_LOW (TYPE_RM_SIZE (type));
	  if (type_unsigned_for_rm (type))
	    field_type = make_unsigned_type (prec);
	  else
	    field_type = make_signed_type (prec);
	  SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (type));
	}
      else
	field_type = type;

      field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
				 NULL_TREE, bitsize_zero_node, c < 0, 0);

      finish_record_type (rec_type, field, 1, false);

      expr = unchecked_convert (rec_type, expr, notrunc_p);
      expr = build_component_ref (expr, field, false);
      expr = fold_build1 (NOP_EXPR, type, expr);
    }

  /* Similarly if we are converting from an integral type whose precision is
     not equal to its size, first copy into a field of the given precision
     and unchecked convert the record type.

     The same considerations as above apply if the target type is an aggregate
     type with reverse storage order and we also proceed similarly.  */
  else if ((INTEGRAL_TYPE_P (etype)
	    && TYPE_RM_SIZE (etype)
	    && ((c = tree_int_cst_compare (TYPE_RM_SIZE (etype),
					   TYPE_SIZE (etype))) < 0
		|| reverse))
	   || (SCALAR_FLOAT_TYPE_P (etype) && reverse))
    {
      tree rec_type = make_node (RECORD_TYPE);
      vec<constructor_elt, va_gc> *v;
      vec_alloc (v, 1);
      tree field_type, field;

      TYPE_REVERSE_STORAGE_ORDER (rec_type) = reverse;

      if (c < 0)
	{
	  const unsigned HOST_WIDE_INT prec
	    = TREE_INT_CST_LOW (TYPE_RM_SIZE (etype));
	  if (type_unsigned_for_rm (etype))
	    field_type = make_unsigned_type (prec);
	  else
	    field_type = make_signed_type (prec);
	  SET_TYPE_RM_SIZE (field_type, TYPE_RM_SIZE (etype));
	}
      else
	field_type = etype;

      field = create_field_decl (get_identifier ("OBJ"), field_type, rec_type,
				 NULL_TREE, bitsize_zero_node, c < 0, 0);

      finish_record_type (rec_type, field, 1, false);

      expr = fold_build1 (NOP_EXPR, field_type, expr);
      CONSTRUCTOR_APPEND_ELT (v, field, expr);
      expr = gnat_build_constructor (rec_type, v);
      expr = unchecked_convert (type, expr, notrunc_p);
    }

  /* If we are converting from a scalar type to a type with a different size,
     we need to pad to have the same size on both sides.

     ??? We cannot do it unconditionally because unchecked conversions are
     used liberally by the front-end to implement interface thunks:

       type ada__tags__addr_ptr is access system.address;
       S191s : constant ada__tags__addr_ptr := ada__tags__addr_ptr!(S190s);
       return p___size__4 (p__object!(S191s.all));

     so we need to skip dereferences.  */
  else if (!INDIRECT_REF_P (expr)
	   && !AGGREGATE_TYPE_P (etype)
	   && ecode != UNCONSTRAINED_ARRAY_TYPE
	   && TREE_CONSTANT (TYPE_SIZE (type))
	   && (c = tree_int_cst_compare (TYPE_SIZE (etype), TYPE_SIZE (type))))
    {
      if (c < 0)
	{
	  expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0, Empty,
					  false, false, true),
			  expr);
	  expr = unchecked_convert (type, expr, notrunc_p);
	}
      else
	{
	  tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
					  false, false, true);
	  expr = unchecked_convert (rec_type, expr, notrunc_p);
	  expr = build_component_ref (expr, TYPE_FIELDS (rec_type), false);
	}
    }

  /* Likewise if we are converting from a scalar type to a type with self-
     referential size.  We use the max size to do the padding in this case.  */
  else if (!INDIRECT_REF_P (expr)
	   && !AGGREGATE_TYPE_P (etype)
	   && ecode != UNCONSTRAINED_ARRAY_TYPE
	   && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type)))
    {
      tree new_size = max_size (TYPE_SIZE (type), true);
      c = tree_int_cst_compare (TYPE_SIZE (etype), new_size);
      if (c < 0)
	{
	  expr = convert (maybe_pad_type (etype, new_size, 0, Empty,
					  false, false, true),
			  expr);
	  expr = unchecked_convert (type, expr, notrunc_p);
	}
      else
	{
	  tree rec_type = maybe_pad_type (type, TYPE_SIZE (etype), 0, Empty,
					  false, false, true);
	  expr = unchecked_convert (rec_type, expr, notrunc_p);
	  expr = build_component_ref (expr, TYPE_FIELDS (rec_type), false);
	}
    }

  /* We have a special case when we are converting between two unconstrained
     array types.  In that case, take the address, convert the fat pointer
     types, and dereference.  */
  else if (ecode == code && code == UNCONSTRAINED_ARRAY_TYPE)
    expr = build_unary_op (INDIRECT_REF, NULL_TREE,
			   build1 (VIEW_CONVERT_EXPR, TREE_TYPE (type),
				   build_unary_op (ADDR_EXPR, NULL_TREE,
						   expr)));

  /* Another special case is when we are converting to a vector type from its
     representative array type; this a regular conversion.  */
  else if (code == VECTOR_TYPE
	   && ecode == ARRAY_TYPE
	   && gnat_types_compatible_p (TYPE_REPRESENTATIVE_ARRAY (type),
				       etype))
    expr = convert (type, expr);

  /* And, if the array type is not the representative, we try to build an
     intermediate vector type of which the array type is the representative
     and to do the unchecked conversion between the vector types, in order
     to enable further simplifications in the middle-end.  */
  else if (code == VECTOR_TYPE
	   && ecode == ARRAY_TYPE
	   && (tem = build_vector_type_for_array (etype, NULL_TREE)))
    {
      expr = convert (tem, expr);
      return unchecked_convert (type, expr, notrunc_p);
    }

  /* If we are converting a CONSTRUCTOR to a more aligned aggregate type, bump
     the alignment of the CONSTRUCTOR to speed up the copy operation.  But do
     not do it for a conversion between original and packable version to avoid
     an infinite recursion.  */
  else if (TREE_CODE (expr) == CONSTRUCTOR
	   && AGGREGATE_TYPE_P (type)
	   && TYPE_NAME (type) != TYPE_NAME (etype)
	   && TYPE_ALIGN (etype) < TYPE_ALIGN (type))
    {
      expr = convert (maybe_pad_type (etype, NULL_TREE, TYPE_ALIGN (type),
				      Empty, false, false, true),
		      expr);
      return unchecked_convert (type, expr, notrunc_p);
    }

  /* If we are converting a CONSTRUCTOR to a larger aggregate type, bump the
     size of the CONSTRUCTOR to make sure there are enough allocated bytes.
     But do not do it for a conversion between original and packable version
     to avoid an infinite recursion.  */
  else if (TREE_CODE (expr) == CONSTRUCTOR
	   && AGGREGATE_TYPE_P (type)
	   && TYPE_NAME (type) != TYPE_NAME (etype)
	   && TREE_CONSTANT (TYPE_SIZE (type))
	   && (!TREE_CONSTANT (TYPE_SIZE (etype))
	       || tree_int_cst_lt (TYPE_SIZE (etype), TYPE_SIZE (type))))
    {
      expr = convert (maybe_pad_type (etype, TYPE_SIZE (type), 0,
				      Empty, false, false, true),
		      expr);
      return unchecked_convert (type, expr, notrunc_p);
    }

  /* Otherwise, just build a VIEW_CONVERT_EXPR of the expression.  */
  else
    {
      expr = maybe_unconstrained_array (expr);
      etype = TREE_TYPE (expr);
      ecode = TREE_CODE (etype);
      if (can_fold_for_view_convert_p (expr))
	expr = fold_build1 (VIEW_CONVERT_EXPR, type, expr);
      else
	expr = build1 (VIEW_CONVERT_EXPR, type, expr);
    }

  /* If the result is a non-biased integral type whose precision is not equal
     to its size, sign- or zero-extend the result.  But we need not do this
     if the input is also an integral type and both are unsigned or both are
     signed and have the same precision.  */
  tree type_rm_size;
  if (!notrunc_p
      && !biased
      && INTEGRAL_TYPE_P (type)
      && (type_rm_size = TYPE_RM_SIZE (type))
      && tree_int_cst_compare (type_rm_size, TYPE_SIZE (type)) < 0
      && !(INTEGRAL_TYPE_P (etype)
	   && type_unsigned_for_rm (type) == type_unsigned_for_rm (etype)
	   && (type_unsigned_for_rm (type)
	       || tree_int_cst_compare (type_rm_size,
					TYPE_RM_SIZE (etype)
					? TYPE_RM_SIZE (etype)
					: TYPE_SIZE (etype)) == 0)))
    {
      if (integer_zerop (type_rm_size))
	expr = build_int_cst (type, 0);
      else
	{
	  tree base_type
	    = gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
				  type_unsigned_for_rm (type));
	  tree shift_expr
	    = convert (base_type,
		       size_binop (MINUS_EXPR,
				   TYPE_SIZE (type), type_rm_size));
	  expr
	    = convert (type,
		       build_binary_op (RSHIFT_EXPR, base_type,
				        build_binary_op (LSHIFT_EXPR, base_type,
							 convert (base_type,
								  expr),
							 shift_expr),
				        shift_expr));
	}
    }

  /* An unchecked conversion should never raise Constraint_Error.  The code
     below assumes that GCC's conversion routines overflow the same way that
     the underlying hardware does.  This is probably true.  In the rare case
     when it is false, we can rely on the fact that such conversions are
     erroneous anyway.  */
  if (TREE_CODE (expr) == INTEGER_CST)
    TREE_OVERFLOW (expr) = 0;

  /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
     show no longer constant.  */
  if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
      && !operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype),
			   OEP_ONLY_CONST))
    TREE_CONSTANT (expr) = 0;

  return expr;
}

/* Return the appropriate GCC tree code for the specified GNAT_TYPE,
   the latter being a record type as predicated by Is_Record_Type.  */

enum tree_code
tree_code_for_record_type (Entity_Id gnat_type)
{
  Node_Id component_list, component;

  /* Return UNION_TYPE if it's an Unchecked_Union whose non-discriminant
     fields are all in the variant part.  Otherwise, return RECORD_TYPE.  */
  if (!Is_Unchecked_Union (gnat_type))
    return RECORD_TYPE;

  gnat_type = Implementation_Base_Type (gnat_type);
  component_list
    = Component_List (Type_Definition (Declaration_Node (gnat_type)));

  for (component = First_Non_Pragma (Component_Items (component_list));
       Present (component);
       component = Next_Non_Pragma (component))
    if (Ekind (Defining_Entity (component)) == E_Component)
      return RECORD_TYPE;

  return UNION_TYPE;
}

/* Return true if GNAT_TYPE is a "double" floating-point type, i.e. whose
   size is equal to 64 bits, or an array of such a type.  Set ALIGN_CLAUSE
   according to the presence of an alignment clause on the type or, if it
   is an array, on the component type.  */

bool
is_double_float_or_array (Entity_Id gnat_type, bool *align_clause)
{
  gnat_type = Underlying_Type (gnat_type);

  *align_clause = Present (Alignment_Clause (gnat_type));

  if (Is_Array_Type (gnat_type))
    {
      gnat_type = Underlying_Type (Component_Type (gnat_type));
      if (Present (Alignment_Clause (gnat_type)))
	*align_clause = true;
    }

  if (!Is_Floating_Point_Type (gnat_type))
    return false;

  if (UI_To_Int (Esize (gnat_type)) != 64)
    return false;

  return true;
}

/* Return true if GNAT_TYPE is a "double" or larger scalar type, i.e. whose
   size is greater or equal to 64 bits, or an array of such a type.  Set
   ALIGN_CLAUSE according to the presence of an alignment clause on the
   type or, if it is an array, on the component type.  */

bool
is_double_scalar_or_array (Entity_Id gnat_type, bool *align_clause)
{
  gnat_type = Underlying_Type (gnat_type);

  *align_clause = Present (Alignment_Clause (gnat_type));

  if (Is_Array_Type (gnat_type))
    {
      gnat_type = Underlying_Type (Component_Type (gnat_type));
      if (Present (Alignment_Clause (gnat_type)))
	*align_clause = true;
    }

  if (!Is_Scalar_Type (gnat_type))
    return false;

  if (UI_To_Int (Esize (gnat_type)) < 64)
    return false;

  return true;
}

/* Return true if GNU_TYPE is suitable as the type of a non-aliased
   component of an aggregate type.  */

bool
type_for_nonaliased_component_p (tree gnu_type)
{
  /* If the type is passed by reference, we may have pointers to the
     component so it cannot be made non-aliased. */
  if (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type))
    return false;

  /* We used to say that any component of aggregate type is aliased
     because the front-end may take 'Reference of it.  The front-end
     has been enhanced in the meantime so as to use a renaming instead
     in most cases, but the back-end can probably take the address of
     such a component too so we go for the conservative stance.

     For instance, we might need the address of any array type, even
     if normally passed by copy, to construct a fat pointer if the
     component is used as an actual for an unconstrained formal.

     Likewise for record types: even if a specific record subtype is
     passed by copy, the parent type might be passed by ref (e.g. if
     it's of variable size) and we might take the address of a child
     component to pass to a parent formal.  We have no way to check
     for such conditions here.  */
  if (AGGREGATE_TYPE_P (gnu_type))
    return false;

  return true;
}

/* Return true if TYPE is a smaller form of ORIG_TYPE.  */

bool
smaller_form_type_p (tree type, tree orig_type)
{
  tree size, osize;

  /* We're not interested in variants here.  */
  if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig_type))
    return false;

  /* Like a variant, a packable version keeps the original TYPE_NAME.  */
  if (TYPE_NAME (type) != TYPE_NAME (orig_type))
    return false;

  size = TYPE_SIZE (type);
  osize = TYPE_SIZE (orig_type);

  if (!(TREE_CODE (size) == INTEGER_CST && TREE_CODE (osize) == INTEGER_CST))
    return false;

  return tree_int_cst_lt (size, osize) != 0;
}

/* Return whether EXPR, which is the renamed object in an object renaming
   declaration, can be materialized as a reference (with a REFERENCE_TYPE).
   This should be synchronized with Exp_Dbug.Debug_Renaming_Declaration.  */

bool
can_materialize_object_renaming_p (Node_Id expr)
{
  while (true)
    {
      expr = Original_Node (expr);

      switch (Nkind (expr))
	{
	case N_Identifier:
	case N_Expanded_Name:
	  if (!Present (Renamed_Object (Entity (expr))))
	    return true;
	  expr = Renamed_Object (Entity (expr));
	  break;

	case N_Selected_Component:
	  {
	    if (Is_Packed (Underlying_Type (Etype (Prefix (expr)))))
	      return false;

	    const Uint bitpos
	      = Normalized_First_Bit (Entity (Selector_Name (expr)));
	    if (bitpos != UI_No_Uint && bitpos != Uint_0)
	      return false;

	    expr = Prefix (expr);
	    break;
	  }

	case N_Indexed_Component:
	case N_Slice:
	  {
	    const Entity_Id t = Underlying_Type (Etype (Prefix (expr)));

	    if (Is_Array_Type (t) && Present (Packed_Array_Impl_Type (t)))
	      return false;

	    expr = Prefix (expr);
	    break;
	  }

	case N_Explicit_Dereference:
	  expr = Prefix (expr);
	  break;

	default:
	  return true;
	};
    }
}

/* Perform final processing on global declarations.  */

static GTY (()) tree dummy_global;

void
gnat_write_global_declarations (void)
{
  unsigned int i;
  tree iter;

  /* If we have declared types as used at the global level, insert them in
     the global hash table.  We use a dummy variable for this purpose, but
     we need to build it unconditionally to avoid -fcompare-debug issues.  */
  if (first_global_object_name)
    {
      struct varpool_node *node;
      char *label;

      ASM_FORMAT_PRIVATE_NAME (label, first_global_object_name, ULONG_MAX);
      dummy_global
	= build_decl (BUILTINS_LOCATION, VAR_DECL, get_identifier (label),
		      void_type_node);
      DECL_HARD_REGISTER (dummy_global) = 1;
      TREE_STATIC (dummy_global) = 1;
      node = varpool_node::get_create (dummy_global);
      node->definition = 1;
      node->force_output = 1;

      if (types_used_by_cur_var_decl)
	while (!types_used_by_cur_var_decl->is_empty ())
	  {
	    tree t = types_used_by_cur_var_decl->pop ();
	    types_used_by_var_decl_insert (t, dummy_global);
	  }
    }

  /* First output the integral global variables, so that they can be referenced
     as bounds by the global dynamic types.  Skip external variables, unless we
     really need to emit debug info for them:, e.g. imported variables.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
    if (TREE_CODE (iter) == VAR_DECL
	&& INTEGRAL_TYPE_P (TREE_TYPE (iter))
	&& (!DECL_EXTERNAL (iter) || !DECL_IGNORED_P (iter)))
      rest_of_decl_compilation (iter, true, 0);

  /* Now output debug information for the global type declarations.  This
     ensures that global types whose compilation hasn't been finalized yet,
     for example pointers to Taft amendment types, have their compilation
     finalized in the right context.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
    if (TREE_CODE (iter) == TYPE_DECL && !DECL_IGNORED_P (iter))
      debug_hooks->type_decl (iter, false);

  /* Then output the other global variables.  We need to do that after the
     information for global types is emitted so that they are finalized.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
    if (TREE_CODE (iter) == VAR_DECL
	&& !INTEGRAL_TYPE_P (TREE_TYPE (iter))
	&& (!DECL_EXTERNAL (iter) || !DECL_IGNORED_P (iter)))
      rest_of_decl_compilation (iter, true, 0);

  /* Output debug information for the global constants.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
    if (TREE_CODE (iter) == CONST_DECL && !DECL_IGNORED_P (iter))
      debug_hooks->early_global_decl (iter);

  /* Output it for the imported functions.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
    if (TREE_CODE (iter) == FUNCTION_DECL
	&& DECL_EXTERNAL (iter)
	&& DECL_INITIAL (iter) == NULL
	&& !DECL_IGNORED_P (iter)
	&& DECL_FUNCTION_IS_DEF (iter))
      debug_hooks->early_global_decl (iter);

  /* Output it for the imported modules/declarations.  In GNAT, these are only
     materializing subprogram.  */
  FOR_EACH_VEC_SAFE_ELT (global_decls, i, iter)
   if (TREE_CODE (iter) == IMPORTED_DECL && !DECL_IGNORED_P (iter))
     debug_hooks->imported_module_or_decl (iter, DECL_NAME (iter),
					   DECL_CONTEXT (iter), false, false);
}

/* ************************************************************************
 * *                           GCC builtins support                       *
 * ************************************************************************ */

/* The general scheme is fairly simple:

   For each builtin function/type to be declared, gnat_install_builtins calls
   internal facilities which eventually get to gnat_pushdecl, which in turn
   tracks the so declared builtin function decls in the 'builtin_decls' global
   datastructure. When an Intrinsic subprogram declaration is processed, we
   search this global datastructure to retrieve the associated BUILT_IN DECL
   node.  */

/* Search the chain of currently available builtin declarations for a node
   corresponding to function NAME (an IDENTIFIER_NODE).  Return the first node
   found, if any, or NULL_TREE otherwise.  */
tree
builtin_decl_for (tree name)
{
  unsigned i;
  tree decl;

  FOR_EACH_VEC_SAFE_ELT (builtin_decls, i, decl)
    if (DECL_NAME (decl) == name)
      return decl;

  return NULL_TREE;
}

/* The code below eventually exposes gnat_install_builtins, which declares
   the builtin types and functions we might need, either internally or as
   user accessible facilities.

   ??? This is a first implementation shot, still in rough shape.  It is
   heavily inspired from the "C" family implementation, with chunks copied
   verbatim from there.

   Two obvious improvement candidates are:
   o Use a more efficient name/decl mapping scheme
   o Devise a middle-end infrastructure to avoid having to copy
     pieces between front-ends.  */

/* ----------------------------------------------------------------------- *
 *                         BUILTIN ELEMENTARY TYPES                        *
 * ----------------------------------------------------------------------- */

/* Standard data types to be used in builtin argument declarations.  */

enum c_tree_index
{
    CTI_SIGNED_SIZE_TYPE, /* For format checking only.  */
    CTI_STRING_TYPE,
    CTI_CONST_STRING_TYPE,

    CTI_MAX
};

static tree c_global_trees[CTI_MAX];

#define signed_size_type_node	c_global_trees[CTI_SIGNED_SIZE_TYPE]
#define string_type_node	c_global_trees[CTI_STRING_TYPE]
#define const_string_type_node	c_global_trees[CTI_CONST_STRING_TYPE]

/* ??? In addition some attribute handlers, we currently don't support a
   (small) number of builtin-types, which in turns inhibits support for a
   number of builtin functions.  */
#define wint_type_node    void_type_node
#define intmax_type_node  void_type_node
#define uintmax_type_node void_type_node

/* Used to help initialize the builtin-types.def table.  When a type of
   the correct size doesn't exist, use error_mark_node instead of NULL.
   The later results in segfaults even when a decl using the type doesn't
   get invoked.  */

static tree
builtin_type_for_size (int size, bool unsignedp)
{
  tree type = gnat_type_for_size (size, unsignedp);
  return type ? type : error_mark_node;
}

/* Build/push the elementary type decls that builtin functions/types
   will need.  */

static void
install_builtin_elementary_types (void)
{
  signed_size_type_node = gnat_signed_type_for (size_type_node);
  pid_type_node = integer_type_node;

  string_type_node = build_pointer_type (char_type_node);
  const_string_type_node
    = build_pointer_type (build_qualified_type
			  (char_type_node, TYPE_QUAL_CONST));
}

/* ----------------------------------------------------------------------- *
 *                          BUILTIN FUNCTION TYPES                         *
 * ----------------------------------------------------------------------- */

/* Now, builtin function types per se.  */

enum c_builtin_type
{
#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6) NAME,
#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7) NAME,
#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7, ARG8) NAME,
#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7, ARG8, ARG9) NAME,
#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			     ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
				NAME,
#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
				ARG6) NAME,
#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
				ARG6, ARG7) NAME,
#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
#include "builtin-types.def"
#include "ada-builtin-types.def"
#undef DEF_PRIMITIVE_TYPE
#undef DEF_FUNCTION_TYPE_0
#undef DEF_FUNCTION_TYPE_1
#undef DEF_FUNCTION_TYPE_2
#undef DEF_FUNCTION_TYPE_3
#undef DEF_FUNCTION_TYPE_4
#undef DEF_FUNCTION_TYPE_5
#undef DEF_FUNCTION_TYPE_6
#undef DEF_FUNCTION_TYPE_7
#undef DEF_FUNCTION_TYPE_8
#undef DEF_FUNCTION_TYPE_9
#undef DEF_FUNCTION_TYPE_10
#undef DEF_FUNCTION_TYPE_11
#undef DEF_FUNCTION_TYPE_VAR_0
#undef DEF_FUNCTION_TYPE_VAR_1
#undef DEF_FUNCTION_TYPE_VAR_2
#undef DEF_FUNCTION_TYPE_VAR_3
#undef DEF_FUNCTION_TYPE_VAR_4
#undef DEF_FUNCTION_TYPE_VAR_5
#undef DEF_FUNCTION_TYPE_VAR_6
#undef DEF_FUNCTION_TYPE_VAR_7
#undef DEF_POINTER_TYPE
  BT_LAST
};

typedef enum c_builtin_type builtin_type;

/* A temporary array used in communication with def_fn_type.  */
static GTY(()) tree builtin_types[(int) BT_LAST + 1];

/* A helper function for install_builtin_types.  Build function type
   for DEF with return type RET and N arguments.  If VAR is true, then the
   function should be variadic after those N arguments.

   Takes special care not to ICE if any of the types involved are
   error_mark_node, which indicates that said type is not in fact available
   (see builtin_type_for_size).  In which case the function type as a whole
   should be error_mark_node.  */

static void
def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
{
  tree t;
  tree *args = XALLOCAVEC (tree, n);
  va_list list;
  int i;

  va_start (list, n);
  for (i = 0; i < n; ++i)
    {
      builtin_type a = (builtin_type) va_arg (list, int);
      t = builtin_types[a];
      if (t == error_mark_node)
	goto egress;
      args[i] = t;
    }

  t = builtin_types[ret];
  if (t == error_mark_node)
    goto egress;
  if (var)
    t = build_varargs_function_type_array (t, n, args);
  else
    t = build_function_type_array (t, n, args);

 egress:
  builtin_types[def] = t;
  va_end (list);
}

/* Build the builtin function types and install them in the builtin_types
   array for later use in builtin function decls.  */

static void
install_builtin_function_types (void)
{
  tree va_list_ref_type_node;
  tree va_list_arg_type_node;

  if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
    {
      va_list_arg_type_node = va_list_ref_type_node =
	build_pointer_type (TREE_TYPE (va_list_type_node));
    }
  else
    {
      va_list_arg_type_node = va_list_type_node;
      va_list_ref_type_node = build_reference_type (va_list_type_node);
    }

#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
  builtin_types[ENUM] = VALUE;
#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
  def_fn_type (ENUM, RETURN, 0, 0);
#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
  def_fn_type (ENUM, RETURN, 0, 1, ARG1);
#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
  def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
  def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
  def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5)	\
  def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6)					\
  def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7)					\
  def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7, ARG8)				\
  def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
	       ARG7, ARG8);
#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
			    ARG6, ARG7, ARG8, ARG9)			\
  def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
	       ARG7, ARG8, ARG9);
#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
			     ARG6, ARG7, ARG8, ARG9, ARG10)		\
  def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
	       ARG7, ARG8, ARG9, ARG10);
#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5,\
			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11)	\
  def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
	       ARG7, ARG8, ARG9, ARG10, ARG11);
#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
  def_fn_type (ENUM, RETURN, 1, 0);
#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
  def_fn_type (ENUM, RETURN, 1, 1, ARG1);
#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
  def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
  def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
  def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
  def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
				ARG6)				\
  def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
				ARG6, ARG7)				\
  def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
#define DEF_POINTER_TYPE(ENUM, TYPE) \
  builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);

#include "builtin-types.def"
#include "ada-builtin-types.def"

#undef DEF_PRIMITIVE_TYPE
#undef DEF_FUNCTION_TYPE_0
#undef DEF_FUNCTION_TYPE_1
#undef DEF_FUNCTION_TYPE_2
#undef DEF_FUNCTION_TYPE_3
#undef DEF_FUNCTION_TYPE_4
#undef DEF_FUNCTION_TYPE_5
#undef DEF_FUNCTION_TYPE_6
#undef DEF_FUNCTION_TYPE_7
#undef DEF_FUNCTION_TYPE_8
#undef DEF_FUNCTION_TYPE_9
#undef DEF_FUNCTION_TYPE_10
#undef DEF_FUNCTION_TYPE_11
#undef DEF_FUNCTION_TYPE_VAR_0
#undef DEF_FUNCTION_TYPE_VAR_1
#undef DEF_FUNCTION_TYPE_VAR_2
#undef DEF_FUNCTION_TYPE_VAR_3
#undef DEF_FUNCTION_TYPE_VAR_4
#undef DEF_FUNCTION_TYPE_VAR_5
#undef DEF_FUNCTION_TYPE_VAR_6
#undef DEF_FUNCTION_TYPE_VAR_7
#undef DEF_POINTER_TYPE
  builtin_types[(int) BT_LAST] = NULL_TREE;
}

/* ----------------------------------------------------------------------- *
 *                            BUILTIN ATTRIBUTES                           *
 * ----------------------------------------------------------------------- */

enum built_in_attribute
{
#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
#include "builtin-attrs.def"
#undef DEF_ATTR_NULL_TREE
#undef DEF_ATTR_INT
#undef DEF_ATTR_STRING
#undef DEF_ATTR_IDENT
#undef DEF_ATTR_TREE_LIST
  ATTR_LAST
};

static GTY(()) tree built_in_attributes[(int) ATTR_LAST];

static void
install_builtin_attributes (void)
{
  /* Fill in the built_in_attributes array.  */
#define DEF_ATTR_NULL_TREE(ENUM)				\
  built_in_attributes[(int) ENUM] = NULL_TREE;
#define DEF_ATTR_INT(ENUM, VALUE)				\
  built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
#define DEF_ATTR_STRING(ENUM, VALUE)				\
  built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
#define DEF_ATTR_IDENT(ENUM, STRING)				\
  built_in_attributes[(int) ENUM] = get_identifier (STRING);
#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)	\
  built_in_attributes[(int) ENUM]			\
    = tree_cons (built_in_attributes[(int) PURPOSE],	\
		 built_in_attributes[(int) VALUE],	\
		 built_in_attributes[(int) CHAIN]);
#include "builtin-attrs.def"
#undef DEF_ATTR_NULL_TREE
#undef DEF_ATTR_INT
#undef DEF_ATTR_STRING
#undef DEF_ATTR_IDENT
#undef DEF_ATTR_TREE_LIST
}

/* Handle a "const" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_const_attribute (tree *node, tree ARG_UNUSED (name),
			tree ARG_UNUSED (args), int ARG_UNUSED (flags),
			bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    TREE_READONLY (*node) = 1;
  else
    *no_add_attrs = true;

  return NULL_TREE;
}

/* Handle a "nothrow" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
			  tree ARG_UNUSED (args), int ARG_UNUSED (flags),
			  bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    TREE_NOTHROW (*node) = 1;
  else
    *no_add_attrs = true;

  return NULL_TREE;
}

/* Handle a "pure" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
		       int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    DECL_PURE_P (*node) = 1;
  /* TODO: support types.  */
  else
    {
      warning (OPT_Wattributes, "%qs attribute ignored",
	       IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "no vops" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
			 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
			 bool *ARG_UNUSED (no_add_attrs))
{
  gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
  DECL_IS_NOVOPS (*node) = 1;
  return NULL_TREE;
}

/* Helper for nonnull attribute handling; fetch the operand number
   from the attribute argument list.  */

static bool
get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
{
  /* Verify the arg number is a constant.  */
  if (!tree_fits_uhwi_p (arg_num_expr))
    return false;

  *valp = TREE_INT_CST_LOW (arg_num_expr);
  return true;
}

/* Handle the "nonnull" attribute.  */
static tree
handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
			  tree args, int ARG_UNUSED (flags),
			  bool *no_add_attrs)
{
  tree type = *node;
  unsigned HOST_WIDE_INT attr_arg_num;

  /* If no arguments are specified, all pointer arguments should be
     non-null.  Verify a full prototype is given so that the arguments
     will have the correct types when we actually check them later.
     Avoid diagnosing type-generic built-ins since those have no
     prototype.  */
  if (!args)
    {
      if (!prototype_p (type)
	  && (!TYPE_ATTRIBUTES (type)
	      || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
	{
	  error ("%qs attribute without arguments on a non-prototype",
		 "nonnull");
	  *no_add_attrs = true;
	}
      return NULL_TREE;
    }

  /* Argument list specified.  Verify that each argument number references
     a pointer argument.  */
  for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
    {
      unsigned HOST_WIDE_INT arg_num = 0, ck_num;

      if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
	{
	  error ("%qs argument has invalid operand number (argument %lu)",
		 "nonnull", (unsigned long) attr_arg_num);
	  *no_add_attrs = true;
	  return NULL_TREE;
	}

      if (prototype_p (type))
	{
	  function_args_iterator iter;
	  tree argument;

	  function_args_iter_init (&iter, type);
	  for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
	    {
	      argument = function_args_iter_cond (&iter);
	      if (!argument || ck_num == arg_num)
		break;
	    }

	  if (!argument
	      || TREE_CODE (argument) == VOID_TYPE)
	    {
	      error ("%qs argument with out-of-range operand number "
		     "(argument %lu, operand %lu)", "nonnull",
		     (unsigned long) attr_arg_num, (unsigned long) arg_num);
	      *no_add_attrs = true;
	      return NULL_TREE;
	    }

	  if (TREE_CODE (argument) != POINTER_TYPE)
	    {
	      error ("%qs argument references non-pointer operand "
		     "(argument %lu, operand %lu)", "nonnull",
		   (unsigned long) attr_arg_num, (unsigned long) arg_num);
	      *no_add_attrs = true;
	      return NULL_TREE;
	    }
	}
    }

  return NULL_TREE;
}

/* Handle a "sentinel" attribute.  */

static tree
handle_sentinel_attribute (tree *node, tree name, tree args,
			   int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (!prototype_p (*node))
    {
      warning (OPT_Wattributes,
	       "%qs attribute requires prototypes with named arguments",
	       IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }
  else
    {
      if (!stdarg_p (*node))
        {
	  warning (OPT_Wattributes,
		   "%qs attribute only applies to variadic functions",
		   IDENTIFIER_POINTER (name));
	  *no_add_attrs = true;
	}
    }

  if (args)
    {
      tree position = TREE_VALUE (args);

      if (TREE_CODE (position) != INTEGER_CST)
        {
	  warning (0, "requested position is not an integer constant");
	  *no_add_attrs = true;
	}
      else
        {
	  if (tree_int_cst_lt (position, integer_zero_node))
	    {
	      warning (0, "requested position is less than zero");
	      *no_add_attrs = true;
	    }
	}
    }

  return NULL_TREE;
}

/* Handle a "noreturn" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
			   int ARG_UNUSED (flags), bool *no_add_attrs)
{
  tree type = TREE_TYPE (*node);

  /* See FIXME comment in c_common_attribute_table.  */
  if (TREE_CODE (*node) == FUNCTION_DECL)
    TREE_THIS_VOLATILE (*node) = 1;
  else if (TREE_CODE (type) == POINTER_TYPE
	   && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
    TREE_TYPE (*node)
      = build_pointer_type
	(change_qualified_type (TREE_TYPE (type), TYPE_QUAL_VOLATILE));
  else
    {
      warning (OPT_Wattributes, "%qs attribute ignored",
	       IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "stack_protect" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_stack_protect_attribute (tree *node, tree name, tree, int,
				bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "no_stack_protector" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_no_stack_protector_attribute (tree *node, tree name, tree, int,
				   bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "strub" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_strub_attribute (tree *, tree, tree, int, bool *no_add_attrs)
{
  *no_add_attrs = true;
  return NULL_TREE;
}

/* Handle a "noinline" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_noinline_attribute (tree *node, tree name,
			   tree ARG_UNUSED (args),
			   int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    {
      if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
	{
	  warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
		   "with attribute %qs", name, "always_inline");
	  *no_add_attrs = true;
	}
      else
	DECL_UNINLINABLE (*node) = 1;
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "noclone" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_noclone_attribute (tree *node, tree name,
			  tree ARG_UNUSED (args),
			  int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "no_icf" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_noicf_attribute (tree *node, tree name,
			tree ARG_UNUSED (args),
			int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "noipa" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "leaf" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_leaf_attribute (tree *node, tree name, tree ARG_UNUSED (args),
		       int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }
  if (!TREE_PUBLIC (*node))
    {
      warning (OPT_Wattributes, "%qE attribute has no effect", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "always_inline" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_always_inline_attribute (tree *node, tree name, tree ARG_UNUSED (args),
				int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    {
      /* Set the attribute and mark it for disregarding inline limits.  */
      DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "malloc" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
			 int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL
      && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
    DECL_IS_MALLOC (*node) = 1;
  else
    {
      warning (OPT_Wattributes, "%qs attribute ignored",
	       IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Fake handler for attributes we don't properly support.  */

tree
fake_attribute_handler (tree * ARG_UNUSED (node),
			tree ARG_UNUSED (name),
			tree ARG_UNUSED (args),
			int  ARG_UNUSED (flags),
			bool * ARG_UNUSED (no_add_attrs))
{
  return NULL_TREE;
}

/* Handle a "type_generic" attribute.  */

static tree
handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
			       tree ARG_UNUSED (args), int ARG_UNUSED (flags),
			       bool * ARG_UNUSED (no_add_attrs))
{
  /* Ensure we have a function type.  */
  gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);

  /* Ensure we have a variadic function.  */
  gcc_assert (!prototype_p (*node) || stdarg_p (*node));

  return NULL_TREE;
}

/* Handle a "flatten" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_flatten_attribute (tree *node, tree name,
			  tree args ATTRIBUTE_UNUSED,
			  int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    /* Do nothing else, just set the attribute.  We'll get at
       it later with lookup_attribute.  */
    ;
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "used" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
		       int ARG_UNUSED (flags), bool *no_add_attrs)
{
  tree node = *pnode;

  if (TREE_CODE (node) == FUNCTION_DECL
      || (VAR_P (node) && TREE_STATIC (node))
      || (TREE_CODE (node) == TYPE_DECL))
    {
      TREE_USED (node) = 1;
      DECL_PRESERVE_P (node) = 1;
      if (VAR_P (node))
	DECL_READ_P (node) = 1;
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "cold" and attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
		       int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL
      || TREE_CODE (*node) == LABEL_DECL)
    {
      /* Attribute cold processing is done later with lookup_attribute.  */
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "hot" and attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
		      int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL
      || TREE_CODE (*node) == LABEL_DECL)
    {
      /* Attribute hot processing is done later with lookup_attribute.  */
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

/* Handle a "target" attribute.  */

static tree
handle_target_attribute (tree *node, tree name, tree args, int flags,
			 bool *no_add_attrs)
{
  /* Ensure we have a function type.  */
  if (TREE_CODE (*node) != FUNCTION_DECL)
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }
  else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
    {
      warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
		   "with %qs attribute", name, "target_clones");
      *no_add_attrs = true;
    }
  else if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
    *no_add_attrs = true;

  /* Check that there's no empty string in values of the attribute.  */
  for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
    {
      tree value = TREE_VALUE (t);
      if (TREE_CODE (value) == STRING_CST
	  && TREE_STRING_LENGTH (value) == 1
	  && TREE_STRING_POINTER (value)[0] == '\0')
	{
	  warning (OPT_Wattributes, "empty string in attribute %<target%>");
	  *no_add_attrs = true;
	}
    }

  return NULL_TREE;
}

/* Handle a "target_clones" attribute.  */

static tree
handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
			  int ARG_UNUSED (flags), bool *no_add_attrs)
{
  /* Ensure we have a function type.  */
  if (TREE_CODE (*node) == FUNCTION_DECL)
    {
      if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
	{
	  warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
		   "with %qs attribute", name, "always_inline");
	  *no_add_attrs = true;
	}
      else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
	{
	  warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
		   "with %qs attribute", name, "target");
	  *no_add_attrs = true;
	}
      else
	/* Do not inline functions with multiple clone targets.  */
	DECL_UNINLINABLE (*node) = 1;
    }
  else
    {
      warning (OPT_Wattributes, "%qE attribute ignored", name);
      *no_add_attrs = true;
    }
  return NULL_TREE;
}

/* Handle a "vector_size" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_vector_size_attribute (tree *node, tree name, tree args,
			      int ARG_UNUSED (flags), bool *no_add_attrs)
{
  tree type = *node;
  tree vector_type;

  *no_add_attrs = true;

  /* We need to provide for vector pointers, vector arrays, and
     functions returning vectors.  For example:

       __attribute__((vector_size(16))) short *foo;

     In this case, the mode is SI, but the type being modified is
     HI, so we need to look further.  */
  while (POINTER_TYPE_P (type)
	 || TREE_CODE (type) == FUNCTION_TYPE
	 || TREE_CODE (type) == ARRAY_TYPE)
    type = TREE_TYPE (type);

  vector_type = build_vector_type_for_size (type, TREE_VALUE (args), name);
  if (!vector_type)
    return NULL_TREE;

  /* Build back pointers if needed.  */
  *node = reconstruct_complex_type (*node, vector_type);

  return NULL_TREE;
}

/* Handle a "vector_type" attribute; arguments as in
   struct attribute_spec.handler.  */

static tree
handle_vector_type_attribute (tree *node, tree name, tree ARG_UNUSED (args),
			      int ARG_UNUSED (flags), bool *no_add_attrs)
{
  tree type = *node;
  tree vector_type;

  *no_add_attrs = true;

  if (TREE_CODE (type) != ARRAY_TYPE)
    {
      error ("attribute %qs applies to array types only",
	     IDENTIFIER_POINTER (name));
      return NULL_TREE;
    }

  vector_type = build_vector_type_for_array (type, name);
  if (!vector_type)
    return NULL_TREE;

  TYPE_REPRESENTATIVE_ARRAY (vector_type) = type;
  *node = vector_type;

  return NULL_TREE;
}

/* Handle a "zero_call_used_regs" attribute; arguments as in