1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- E X P _ U N S T --
-- --
-- B o d y --
-- --
-- Copyright (C) 2014-2022, 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 distributed with GNAT; see file COPYING3. If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Atree; use Atree;
with Debug; use Debug;
with Einfo; use Einfo;
with Einfo.Entities; use Einfo.Entities;
with Einfo.Utils; use Einfo.Utils;
with Elists; use Elists;
with Exp_Util; use Exp_Util;
with Lib; use Lib;
with Namet; use Namet;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt;
with Output; use Output;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Aux; use Sem_Aux;
with Sem_Ch8; use Sem_Ch8;
with Sem_Mech; use Sem_Mech;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Sinfo; use Sinfo;
with Sinfo.Nodes; use Sinfo.Nodes;
with Sinfo.Utils; use Sinfo.Utils;
with Sinput; use Sinput;
with Snames; use Snames;
with Stand; use Stand;
with Tbuild; use Tbuild;
with Uintp; use Uintp;
package body Exp_Unst is
-----------------------
-- Local Subprograms --
-----------------------
procedure Unnest_Subprogram
(Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False);
-- Subp is a library-level subprogram which has nested subprograms, and
-- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
-- declares the AREC types and objects, adds assignments to the AREC record
-- as required, defines the xxxPTR types for uplevel referenced objects,
-- adds the ARECP parameter to all nested subprograms which need it, and
-- modifies all uplevel references appropriately. If For_Inline is True,
-- we're unnesting this subprogram because it's on the list of inlined
-- subprograms and should unnest it despite it not being part of the main
-- unit.
-----------
-- Calls --
-----------
-- Table to record calls within the nest being analyzed. These are the
-- calls which may need to have an AREC actual added. This table is built
-- new for each subprogram nest and cleared at the end of processing each
-- subprogram nest.
type Call_Entry is record
N : Node_Id;
-- The actual call
Caller : Entity_Id;
-- Entity of the subprogram containing the call (can be at any level)
Callee : Entity_Id;
-- Entity of the subprogram called (always at level 2 or higher). Note
-- that in accordance with the basic rules of nesting, the level of To
-- is either less than or equal to the level of From, or one greater.
end record;
package Calls is new Table.Table (
Table_Component_Type => Call_Entry,
Table_Index_Type => Nat,
Table_Low_Bound => 1,
Table_Initial => 100,
Table_Increment => 200,
Table_Name => "Unnest_Calls");
-- Records each call within the outer subprogram and all nested subprograms
-- that are to other subprograms nested within the outer subprogram. These
-- are the calls that may need an additional parameter.
procedure Append_Unique_Call (Call : Call_Entry);
-- Append a call entry to the Calls table. A check is made to see if the
-- table already contains this entry and if so it has no effect.
----------------------------------
-- Subprograms For Fat Pointers --
----------------------------------
function Build_Access_Type_Decl
(E : Entity_Id;
Scop : Entity_Id) return Node_Id;
-- For an uplevel reference that involves an unconstrained array type,
-- build an access type declaration for the corresponding activation
-- record component. The relevant attributes of the access type are
-- set here to avoid a full analysis that would require a scope stack.
function Needs_Fat_Pointer (E : Entity_Id) return Boolean;
-- A formal parameter of an unconstrained array type that appears in an
-- uplevel reference requires the construction of an access type, to be
-- used in the corresponding component declaration.
-----------
-- Urefs --
-----------
-- Table to record explicit uplevel references to objects (variables,
-- constants, formal parameters). These are the references that will
-- need rewriting to use the activation table (AREC) pointers. Also
-- included are implicit and explicit uplevel references to types, but
-- these do not get rewritten by the front end. This table is built new
-- for each subprogram nest and cleared at the end of processing each
-- subprogram nest.
type Uref_Entry is record
Ref : Node_Id;
-- The reference itself. For objects this is always an entity reference
-- and the referenced entity will have its Is_Uplevel_Referenced_Entity
-- flag set and will appear in the Uplevel_Referenced_Entities list of
-- the subprogram declaring this entity.
Ent : Entity_Id;
-- The Entity_Id of the uplevel referenced object or type
Caller : Entity_Id;
-- The entity for the subprogram immediately containing this entity
Callee : Entity_Id;
-- The entity for the subprogram containing the referenced entity. Note
-- that the level of Callee must be less than the level of Caller, since
-- this is an uplevel reference.
end record;
package Urefs is new Table.Table (
Table_Component_Type => Uref_Entry,
Table_Index_Type => Nat,
Table_Low_Bound => 1,
Table_Initial => 100,
Table_Increment => 200,
Table_Name => "Unnest_Urefs");
------------------------
-- Append_Unique_Call --
------------------------
procedure Append_Unique_Call (Call : Call_Entry) is
begin
for J in Calls.First .. Calls.Last loop
if Calls.Table (J) = Call then
return;
end if;
end loop;
Calls.Append (Call);
end Append_Unique_Call;
-----------------------------
-- Build_Access_Type_Decl --
-----------------------------
function Build_Access_Type_Decl
(E : Entity_Id;
Scop : Entity_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (E);
Typ : Entity_Id;
begin
Typ := Make_Temporary (Loc, 'S');
Mutate_Ekind (Typ, E_General_Access_Type);
Set_Etype (Typ, Typ);
Set_Scope (Typ, Scop);
Set_Directly_Designated_Type (Typ, Etype (E));
return
Make_Full_Type_Declaration (Loc,
Defining_Identifier => Typ,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
Subtype_Indication => New_Occurrence_Of (Etype (E), Loc)));
end Build_Access_Type_Decl;
---------------
-- Get_Level --
---------------
function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
Lev : Nat;
S : Entity_Id;
begin
Lev := 1;
S := Sub;
loop
if S = Subp then
return Lev;
else
Lev := Lev + 1;
S := Enclosing_Subprogram (S);
end if;
end loop;
end Get_Level;
--------------------------
-- In_Synchronized_Unit --
--------------------------
function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is
S : Entity_Id := Scope (Subp);
begin
while Present (S) and then S /= Standard_Standard loop
if Is_Concurrent_Type (S) then
return True;
elsif Is_Private_Type (S)
and then Present (Full_View (S))
and then Is_Concurrent_Type (Full_View (S))
then
return True;
end if;
S := Scope (S);
end loop;
return False;
end In_Synchronized_Unit;
-----------------------
-- Needs_Fat_Pointer --
-----------------------
function Needs_Fat_Pointer (E : Entity_Id) return Boolean is
Typ : constant Entity_Id := Get_Fullest_View (Etype (E));
begin
return Is_Array_Type (Typ) and then not Is_Constrained (Typ);
end Needs_Fat_Pointer;
----------------
-- Subp_Index --
----------------
function Subp_Index (Sub : Entity_Id) return SI_Type is
E : Entity_Id := Sub;
begin
pragma Assert (Is_Subprogram (E));
if Field_Is_Initial_Zero (E, F_Subps_Index)
or else Subps_Index (E) = Uint_0
then
E := Ultimate_Alias (E);
-- The body of a protected operation has a different name and
-- has been scanned at this point, and thus has an entry in the
-- subprogram table.
if E = Sub and then Present (Protected_Body_Subprogram (E)) then
E := Protected_Body_Subprogram (E);
end if;
if Ekind (E) = E_Function
and then Rewritten_For_C (E)
and then Present (Corresponding_Procedure (E))
then
E := Corresponding_Procedure (E);
end if;
end if;
pragma Assert (Subps_Index (E) /= Uint_0);
return SI_Type (UI_To_Int (Subps_Index (E)));
end Subp_Index;
-----------------------
-- Unnest_Subprogram --
-----------------------
procedure Unnest_Subprogram
(Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False) is
function AREC_Name (J : Pos; S : String) return Name_Id;
-- Returns name for string ARECjS, where j is the decimal value of j
function Enclosing_Subp (Subp : SI_Type) return SI_Type;
-- Subp is the index of a subprogram which has a Lev greater than 1.
-- This function returns the index of the enclosing subprogram which
-- will have a Lev value one less than this.
function Img_Pos (N : Pos) return String;
-- Return image of N without leading blank
function Upref_Name
(Ent : Entity_Id;
Index : Pos;
Clist : List_Id) return Name_Id;
-- This function returns the name to be used in the activation record to
-- reference the variable uplevel. Clist is the list of components that
-- have been created in the activation record so far. Normally the name
-- is just a copy of the Chars field of the entity. The exception is
-- when the name has already been used, in which case we suffix the name
-- with the index value Index to avoid duplication. This happens with
-- declare blocks and generic parameters at least.
---------------
-- AREC_Name --
---------------
function AREC_Name (J : Pos; S : String) return Name_Id is
begin
return Name_Find ("AREC" & Img_Pos (J) & S);
end AREC_Name;
--------------------
-- Enclosing_Subp --
--------------------
function Enclosing_Subp (Subp : SI_Type) return SI_Type is
STJ : Subp_Entry renames Subps.Table (Subp);
Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
begin
pragma Assert (STJ.Lev > 1);
pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
return Ret;
end Enclosing_Subp;
-------------
-- Img_Pos --
-------------
function Img_Pos (N : Pos) return String is
Buf : String (1 .. 20);
Ptr : Natural;
NV : Nat;
begin
Ptr := Buf'Last;
NV := N;
while NV /= 0 loop
Buf (Ptr) := Character'Val (48 + NV mod 10);
Ptr := Ptr - 1;
NV := NV / 10;
end loop;
return Buf (Ptr + 1 .. Buf'Last);
end Img_Pos;
----------------
-- Upref_Name --
----------------
function Upref_Name
(Ent : Entity_Id;
Index : Pos;
Clist : List_Id) return Name_Id
is
C : Node_Id;
begin
C := First (Clist);
loop
if No (C) then
return Chars (Ent);
elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
return
Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
else
Next (C);
end if;
end loop;
end Upref_Name;
-- Start of processing for Unnest_Subprogram
begin
-- Nothing to do inside a generic (all processing is for instance)
if Inside_A_Generic then
return;
end if;
-- If the main unit is a package body then we need to examine the spec
-- to determine whether the main unit is generic (the scope stack is not
-- present when this is called on the main unit).
if not For_Inline
and then Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
then
return;
-- Only unnest when generating code for the main source unit or if
-- we're unnesting for inline. But in some Annex E cases the Sloc
-- points to a different unit, so also make sure that the Parent
-- isn't in something that we know we're generating code for.
elsif not For_Inline
and then not In_Extended_Main_Code_Unit (Subp_Body)
and then not In_Extended_Main_Code_Unit (Parent (Subp_Body))
then
return;
end if;
-- This routine is called late, after the scope stack is gone. The
-- following creates a suitable dummy scope stack to be used for the
-- analyze/expand calls made from this routine.
Push_Scope (Subp);
-- First step, we must mark all nested subprograms that require a static
-- link (activation record) because either they contain explicit uplevel
-- references (as indicated by Is_Uplevel_Referenced_Entity being set at
-- this point), or they make calls to other subprograms in the same nest
-- that require a static link (in which case we set this flag).
-- This is a recursive definition, and to implement this, we have to
-- build a call graph for the set of nested subprograms, and then go
-- over this graph to implement recursively the invariant that if a
-- subprogram has a call to a subprogram requiring a static link, then
-- the calling subprogram requires a static link.
-- First populate the above tables
Subps_First := Subps.Last + 1;
Calls.Init;
Urefs.Init;
Build_Tables : declare
Current_Subprogram : Entity_Id := Empty;
-- When we scan a subprogram body, we set Current_Subprogram to the
-- corresponding entity. This gets recursively saved and restored.
function Visit_Node (N : Node_Id) return Traverse_Result;
-- Visit a single node in Subp
-----------
-- Visit --
-----------
procedure Visit is new Traverse_Proc (Visit_Node);
-- Used to traverse the body of Subp, populating the tables
----------------
-- Visit_Node --
----------------
function Visit_Node (N : Node_Id) return Traverse_Result is
Ent : Entity_Id;
Caller : Entity_Id;
Callee : Entity_Id;
procedure Check_Static_Type
(In_T : Entity_Id;
N : Node_Id;
DT : in out Boolean;
Check_Designated : Boolean := False);
-- Given a type In_T, checks if it is a static type defined as
-- a type with no dynamic bounds in sight. If so, the only
-- action is to set Is_Static_Type True for In_T. If In_T is
-- not a static type, then all types with dynamic bounds
-- associated with In_T are detected, and their bounds are
-- marked as uplevel referenced if not at the library level,
-- and DT is set True. If N is specified, it's the node that
-- will need to be replaced. If not specified, it means we
-- can't do a replacement because the bound is implicit.
-- If Check_Designated is True and In_T or its full view
-- is an access type, check whether the designated type
-- has dynamic bounds.
procedure Note_Uplevel_Ref
(E : Entity_Id;
N : Node_Id;
Caller : Entity_Id;
Callee : Entity_Id);
-- Called when we detect an explicit or implicit uplevel reference
-- from within Caller to entity E declared in Callee. E can be a
-- an object or a type.
procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
-- Enter a subprogram whose body is visible or which is a
-- subprogram instance into the subprogram table.
-----------------------
-- Check_Static_Type --
-----------------------
procedure Check_Static_Type
(In_T : Entity_Id;
N : Node_Id;
DT : in out Boolean;
Check_Designated : Boolean := False)
is
T : constant Entity_Id := Get_Fullest_View (In_T);
procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
-- N is the bound of a dynamic type. This procedure notes that
-- this bound is uplevel referenced, it can handle references
-- to entities (typically _FIRST and _LAST entities), and also
-- attribute references of the form T'name (name is typically
-- FIRST or LAST) where T is the uplevel referenced bound.
-- Ref, if Present, is the location of the reference to
-- replace.
------------------------
-- Note_Uplevel_Bound --
------------------------
procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
begin
-- Entity name case. Make sure that the entity is declared
-- in a subprogram. This may not be the case for a type in a
-- loop appearing in a precondition.
-- Exclude explicitly discriminants (that can appear
-- in bounds of discriminated components) and enumeration
-- literals.
if Is_Entity_Name (N) then
if Present (Entity (N))
and then not Is_Type (Entity (N))
and then Present (Enclosing_Subprogram (Entity (N)))
and then
Ekind (Entity (N))
not in E_Discriminant | E_Enumeration_Literal
then
Note_Uplevel_Ref
(E => Entity (N),
N => Empty,
Caller => Current_Subprogram,
Callee => Enclosing_Subprogram (Entity (N)));
end if;
-- Attribute or indexed component case
elsif Nkind (N) in
N_Attribute_Reference | N_Indexed_Component
then
Note_Uplevel_Bound (Prefix (N), Ref);
-- The indices of the indexed components, or the
-- associated expressions of an attribute reference,
-- may also involve uplevel references.
declare
Expr : Node_Id;
begin
Expr := First (Expressions (N));
while Present (Expr) loop
Note_Uplevel_Bound (Expr, Ref);
Next (Expr);
end loop;
end;
-- The type of the prefix may be have an uplevel
-- reference if this needs bounds.
if Nkind (N) = N_Attribute_Reference then
declare
Attr : constant Attribute_Id :=
Get_Attribute_Id (Attribute_Name (N));
DT : Boolean := False;
begin
if (Attr = Attribute_First
or else Attr = Attribute_Last
or else Attr = Attribute_Length)
and then Is_Constrained (Etype (Prefix (N)))
then
Check_Static_Type
(Etype (Prefix (N)), Empty, DT);
end if;
end;
end if;
-- Binary operator cases. These can apply to arrays for
-- which we may need bounds.
elsif Nkind (N) in N_Binary_Op then
Note_Uplevel_Bound (Left_Opnd (N), Ref);
Note_Uplevel_Bound (Right_Opnd (N), Ref);
-- Unary operator case
elsif Nkind (N) in N_Unary_Op then
Note_Uplevel_Bound (Right_Opnd (N), Ref);
-- Explicit dereference and selected component case
elsif Nkind (N) in
N_Explicit_Dereference | N_Selected_Component
then
Note_Uplevel_Bound (Prefix (N), Ref);
-- Conditional expressions
elsif Nkind (N) = N_If_Expression then
declare
Expr : Node_Id;
begin
Expr := First (Expressions (N));
while Present (Expr) loop
Note_Uplevel_Bound (Expr, Ref);
Next (Expr);
end loop;
end;
elsif Nkind (N) = N_Case_Expression then
declare
Alternative : Node_Id;
begin
Note_Uplevel_Bound (Expression (N), Ref);
Alternative := First (Alternatives (N));
while Present (Alternative) loop
Note_Uplevel_Bound (Expression (Alternative), Ref);
end loop;
end;
-- Conversion case
elsif Nkind (N) = N_Type_Conversion then
Note_Uplevel_Bound (Expression (N), Ref);
end if;
end Note_Uplevel_Bound;
-- Start of processing for Check_Static_Type
begin
-- If already marked static, immediate return
if Is_Static_Type (T) and then not Check_Designated then
return;
end if;
-- If the type is at library level, always consider it static,
-- since such uplevel references are irrelevant.
if Is_Library_Level_Entity (T) then
Set_Is_Static_Type (T);
return;
end if;
-- Otherwise figure out what the story is with this type
-- For a scalar type, check bounds
if Is_Scalar_Type (T) then
-- If both bounds static, then this is a static type
declare
LB : constant Node_Id := Type_Low_Bound (T);
UB : constant Node_Id := Type_High_Bound (T);
begin
if not Is_Static_Expression (LB) then
Note_Uplevel_Bound (LB, N);
DT := True;
end if;
if not Is_Static_Expression (UB) then
Note_Uplevel_Bound (UB, N);
DT := True;
end if;
end;
-- For record type, check all components and discriminant
-- constraints if present.
elsif Is_Record_Type (T) then
declare
C : Entity_Id;
D : Elmt_Id;
begin
C := First_Component_Or_Discriminant (T);
while Present (C) loop
Check_Static_Type (Etype (C), N, DT);
Next_Component_Or_Discriminant (C);
end loop;
if Has_Discriminants (T)
and then Present (Discriminant_Constraint (T))
then
D := First_Elmt (Discriminant_Constraint (T));
while Present (D) loop
if not Is_Static_Expression (Node (D)) then
Note_Uplevel_Bound (Node (D), N);
DT := True;
end if;
Next_Elmt (D);
end loop;
end if;
end;
-- For array type, check index types and component type
elsif Is_Array_Type (T) then
declare
IX : Node_Id;
begin
Check_Static_Type (Component_Type (T), N, DT);
IX := First_Index (T);
while Present (IX) loop
Check_Static_Type (Etype (IX), N, DT);
Next_Index (IX);
end loop;
end;
-- For private type, examine whether full view is static
elsif Is_Incomplete_Or_Private_Type (T)
and then Present (Full_View (T))
then
Check_Static_Type (Full_View (T), N, DT, Check_Designated);
if Is_Static_Type (Full_View (T)) then
Set_Is_Static_Type (T);
end if;
-- For access types, check designated type when required
elsif Is_Access_Type (T) and then Check_Designated then
Check_Static_Type (Directly_Designated_Type (T), N, DT);
-- For now, ignore other types
else
return;
end if;
if not DT then
Set_Is_Static_Type (T);
end if;
end Check_Static_Type;
----------------------
-- Note_Uplevel_Ref --
----------------------
procedure Note_Uplevel_Ref
(E : Entity_Id;
N : Node_Id;
Caller : Entity_Id;
Callee : Entity_Id)
is
Full_E : Entity_Id := E;
begin
-- Nothing to do for static type
if Is_Static_Type (E) then
return;
end if;
-- Nothing to do if Caller and Callee are the same
if Caller = Callee then
return;
-- Callee may be a function that returns an array, and that has
-- been rewritten as a procedure. If caller is that procedure,
-- nothing to do either.
elsif Ekind (Callee) = E_Function
and then Rewritten_For_C (Callee)
and then Corresponding_Procedure (Callee) = Caller
then
return;
elsif Ekind (Callee) in E_Entry | E_Entry_Family then
return;
end if;
-- We have a new uplevel referenced entity
if Ekind (E) = E_Constant and then Present (Full_View (E)) then
Full_E := Full_View (E);
end if;
-- All we do at this stage is to add the uplevel reference to
-- the table. It's too early to do anything else, since this
-- uplevel reference may come from an unreachable subprogram
-- in which case the entry will be deleted.
Urefs.Append ((N, Full_E, Caller, Callee));
end Note_Uplevel_Ref;
-------------------------
-- Register_Subprogram --
-------------------------
procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
L : constant Nat := Get_Level (Subp, E);
begin
-- Subprograms declared in tasks and protected types cannot be
-- eliminated because calls to them may be in other units, so
-- they must be treated as reachable.
Subps.Append
((Ent => E,
Bod => Bod,
Lev => L,
Reachable => In_Synchronized_Unit (E)
or else Address_Taken (E),
Uplevel_Ref => L,
Declares_AREC => False,
Uents => No_Elist,
Last => 0,
ARECnF => Empty,
ARECn => Empty,
ARECnT => Empty,
ARECnPT => Empty,
ARECnP => Empty,
ARECnU => Empty));
Set_Subps_Index (E, UI_From_Int (Subps.Last));
-- If we marked this reachable because it's in a synchronized
-- unit, we have to mark all enclosing subprograms as reachable
-- as well. We do the same for subprograms with Address_Taken,
-- because otherwise we can run into problems with looking at
-- enclosing subprograms in Subps.Table due to their being
-- unreachable (the Subp_Index of unreachable subps is later
-- set to zero and their entry in Subps.Table is removed).
if In_Synchronized_Unit (E) or else Address_Taken (E) then
declare
S : Entity_Id := E;
begin
for J in reverse 1 .. L - 1 loop
S := Enclosing_Subprogram (S);
Subps.Table (Subp_Index (S)).Reachable := True;
end loop;
end;
end if;
end Register_Subprogram;
-- Start of processing for Visit_Node
begin
case Nkind (N) is
-- Record a subprogram call
when N_Function_Call
| N_Procedure_Call_Statement
=>
-- We are only interested in direct calls, not indirect
-- calls (where Name (N) is an explicit dereference) at
-- least for now!
if Nkind (Name (N)) in N_Has_Entity then
Ent := Entity (Name (N));
-- We are only interested in calls to subprograms nested
-- within Subp. Calls to Subp itself or to subprograms
-- outside the nested structure do not affect us.
if Is_Subprogram (Ent)
and then not Is_Generic_Subprogram (Ent)
and then not Is_Imported (Ent)
and then not Is_Intrinsic_Subprogram (Ent)
and then Scope_Within (Ultimate_Alias (Ent), Subp)
then
Append_Unique_Call ((N, Current_Subprogram, Ent));
end if;
end if;
-- For all calls where the formal is an unconstrained array
-- and the actual is constrained we need to check the bounds
-- for uplevel references.
declare
Actual : Entity_Id;
DT : Boolean := False;
Formal : Node_Id;
Subp : Entity_Id;
F_Type : Entity_Id;
A_Type : Entity_Id;
begin
if Nkind (Name (N)) = N_Explicit_Dereference then
Subp := Etype (Name (N));
else
Subp := Entity (Name (N));
end if;
Actual := First_Actual (N);
Formal := First_Formal_With_Extras (Subp);
while Present (Actual) loop
F_Type := Get_Fullest_View (Etype (Formal));
A_Type := Get_Fullest_View (Etype (Actual));
if Is_Array_Type (F_Type)
and then not Is_Constrained (F_Type)
and then Is_Constrained (A_Type)
then
Check_Static_Type (A_Type, Empty, DT);
end if;
Next_Actual (Actual);
Next_Formal_With_Extras (Formal);
end loop;
end;
-- An At_End_Proc in a statement sequence indicates that there
-- is a call from the enclosing construct or block to that
-- subprogram. As above, the called entity must be local and
-- not imported.
when N_Handled_Sequence_Of_Statements =>
if Present (At_End_Proc (N))
and then Scope_Within (Entity (At_End_Proc (N)), Subp)
and then not Is_Imported (Entity (At_End_Proc (N)))
then
Append_Unique_Call
((N, Current_Subprogram, Entity (At_End_Proc (N))));
end if;
-- Similarly, the following constructs include a semantic
-- attribute Procedure_To_Call that must be handled like
-- other calls. Likewise for attribute Storage_Pool.
when N_Allocator
| N_Extended_Return_Statement
| N_Free_Statement
| N_Simple_Return_Statement
=>
declare
Pool : constant Entity_Id := Storage_Pool (N);
Proc : constant Entity_Id := Procedure_To_Call (N);
begin
if Present (Proc)
and then Scope_Within (Proc, Subp)
and then not Is_Imported (Proc)
then
Append_Unique_Call ((N, Current_Subprogram, Proc));
end if;
if Present (Pool)
and then not Is_Library_Level_Entity (Pool)
and then Scope_Within_Or_Same (Scope (Pool), Subp)
then
Caller := Current_Subprogram;
Callee := Enclosing_Subprogram (Pool);
if Callee /= Caller then
Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
end if;
end if;
end;
-- For an allocator with a qualified expression, check type
-- of expression being qualified. The explicit type name is
-- handled as an entity reference.
if Nkind (N) = N_Allocator
and then Nkind (Expression (N)) = N_Qualified_Expression
then
declare
DT : Boolean := False;
begin
Check_Static_Type
(Etype (Expression (Expression (N))), Empty, DT);
end;
-- For a Return or Free (all other nodes we handle here),
-- we usually need the size of the object, so we need to be
-- sure that any nonstatic bounds of the expression's type
-- that are uplevel are handled.
elsif Nkind (N) /= N_Allocator
and then Present (Expression (N))
then
declare
DT : Boolean := False;
begin
Check_Static_Type
(Etype (Expression (N)),
Empty,
DT,
Check_Designated => Nkind (N) = N_Free_Statement);
end;
end if;
-- A 'Access reference is a (potential) call. So is 'Address,
-- in particular on imported subprograms. Other attributes
-- require special handling.
when N_Attribute_Reference =>
declare
Attr : constant Attribute_Id :=
Get_Attribute_Id (Attribute_Name (N));
begin
case Attr is
when Attribute_Access
| Attribute_Unchecked_Access
| Attribute_Unrestricted_Access
| Attribute_Address
=>
if Nkind (Prefix (N)) in N_Has_Entity then
Ent := Entity (Prefix (N));
-- We only need to examine calls to subprograms
-- nested within current Subp.
if Scope_Within (Ent, Subp) then
if Is_Imported (Ent) then
null;
elsif Is_Subprogram (Ent) then
Append_Unique_Call
((N, Current_Subprogram, Ent));
end if;
end if;
end if;
-- References to bounds can be uplevel references if
-- the type isn't static.
when Attribute_First
| Attribute_Last
| Attribute_Length
=>
-- Special-case attributes of objects whose bounds
-- may be uplevel references. More complex prefixes
-- handled during full traversal. Note that if the
-- nominal subtype of the prefix is unconstrained,
-- the bound must be obtained from the object, not
-- from the (possibly) uplevel reference. We call
-- Get_Referenced_Object to deal with prefixes that
-- are object renamings (prefixes that are types
-- can be passed and will simply be returned). But
-- it's also legal to get the bounds from the type
-- of the prefix, so we have to handle both cases.
declare
DT : Boolean := False;
begin
if Is_Constrained
(Etype (Get_Referenced_Object (Prefix (N))))
then
Check_Static_Type
(Etype (Get_Referenced_Object (Prefix (N))),
Empty, DT);
end if;
if Is_Constrained (Etype (Prefix (N))) then
Check_Static_Type
(Etype (Prefix (N)), Empty, DT);
end if;
end;
when others =>
null;
end case;
end;
-- Component associations in aggregates are either static or
-- else the aggregate will be expanded into assignments, in
-- which case the expression is analyzed later and provides
-- no relevant code generation.
when N_Component_Association =>
if No (Expression (N))
or else No (Etype (Expression (N)))
then
return Skip;
end if;
-- Generic associations are not analyzed: the actuals are
-- transferred to renaming and subtype declarations that
-- are the ones that must be examined.
when N_Generic_Association =>
return Skip;
-- Indexed references can be uplevel if the type isn't static
-- and if the lower bound (or an inner bound for a multi-
-- dimensional array) is uplevel.
when N_Indexed_Component
| N_Slice
=>
if Is_Constrained (Etype (Prefix (N))) then
declare
DT : Boolean := False;
begin
Check_Static_Type (Etype (Prefix (N)), Empty, DT);
end;
end if;
-- A selected component can have an implicit up-level
-- reference due to the bounds of previous fields in the
-- record. We simplify the processing here by examining
-- all components of the record.
-- Selected components appear as unit names and end labels
-- for child units. Prefixes of these nodes denote parent
-- units and carry no type information so they are skipped.
when N_Selected_Component =>
if Present (Etype (Prefix (N))) then
declare
DT : Boolean := False;
begin
Check_Static_Type (Etype (Prefix (N)), Empty, DT);
end;
end if;
-- For EQ/NE comparisons, we need the type of the operands
-- in order to do the comparison, which means we need the
-- bounds.
when N_Op_Eq
| N_Op_Ne
=>
declare
DT : Boolean := False;
begin
Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
end;
-- Likewise we need the sizes to compute how much to move in
-- an assignment.
when N_Assignment_Statement =>
declare
DT : Boolean := False;
begin
Check_Static_Type (Etype (Name (N)), Empty, DT);
Check_Static_Type (Etype (Expression (N)), Empty, DT);
end;
-- Record a subprogram. We record a subprogram body that acts
-- as a spec. Otherwise we record a subprogram declaration,
-- providing that it has a corresponding body we can get hold
-- of. The case of no corresponding body being available is
-- ignored for now.
when N_Subprogram_Body =>
Ent := Unique_Defining_Entity (N);
-- Ignore generic subprogram
if Is_Generic_Subprogram (Ent) then
return Skip;
end if;
-- Make new entry in subprogram table if not already made
Register_Subprogram (Ent, N);
-- We make a recursive call to scan the subprogram body, so
-- that we can save and restore Current_Subprogram.
declare
Save_CS : constant Entity_Id := Current_Subprogram;
Decl : Node_Id;
begin
Current_Subprogram := Ent;
-- Scan declarations
Decl := First (Declarations (N));
while Present (Decl) loop
Visit (Decl);
Next (Decl);
end loop;
-- Scan statements
Visit (Handled_Statement_Sequence (N));
-- Restore current subprogram setting
Current_Subprogram := Save_CS;
end;
-- Now at this level, return skipping the subprogram body
-- descendants, since we already took care of them!
return Skip;
-- If we have a body stub, visit the associated subunit, which
-- is a semantic descendant of the stub.
when N_Body_Stub =>
Visit (Library_Unit (N));
-- A declaration of a wrapper package indicates a subprogram
-- instance for which there is no explicit body. Enter the
-- subprogram instance in the table.
when N_Package_Declaration =>
if Is_Wrapper_Package (Defining_Entity (N)) then
Register_Subprogram
(Related_Instance (Defining_Entity (N)), Empty);
end if;
-- Skip generic declarations
when N_Generic_Declaration =>
return Skip;
-- Skip generic package body
when N_Package_Body =>
if Present (Corresponding_Spec (N))
and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
then
return Skip;
end if;
-- Pragmas and component declarations are ignored. Quantified
-- expressions are expanded into explicit loops and the
-- original epression must be ignored.
when N_Component_Declaration
| N_Pragma
| N_Quantified_Expression
=>
return Skip;
-- We want to skip the function spec for a generic function
-- to avoid looking at any generic types that might be in
-- its formals.
when N_Function_Specification =>
if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then
return Skip;
end if;
-- Otherwise record an uplevel reference in a local identifier
when others =>
if Nkind (N) in N_Has_Entity
and then Present (Entity (N))
then
Ent := Entity (N);
-- Only interested in entities declared within our nest
if not Is_Library_Level_Entity (Ent)
and then Scope_Within_Or_Same (Scope (Ent), Subp)
-- Skip entities defined in inlined subprograms
and then
Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
-- Constants and variables are potentially uplevel
-- references to global declarations.
and then
(Ekind (Ent) in E_Constant
| E_Loop_Parameter
| E_Variable
-- Formals are interesting, but not if being used
-- as mere names of parameters for name notation
-- calls.
or else
(Is_Formal (Ent)
and then not
(Nkind (Parent (N)) = N_Parameter_Association
and then Selector_Name (Parent (N)) = N))
-- Types other than known Is_Static types are
-- potentially interesting.
or else
(Is_Type (Ent) and then not Is_Static_Type (Ent)))
then
-- Here we have a potentially interesting uplevel
-- reference to examine.
if Is_Type (Ent) then
declare
DT : Boolean := False;
begin
Check_Static_Type (Ent, N, DT);
return OK;
end;
end if;
Caller := Current_Subprogram;
Callee := Enclosing_Subprogram (Ent);
if Callee /= Caller
and then (not Is_Static_Type (Ent)
or else Needs_Fat_Pointer (Ent))
then
Note_Uplevel_Ref (Ent, N, Caller, Callee);
-- Check the type of a formal parameter of the current
-- subprogram, whose formal type may be an uplevel
-- reference.
elsif Is_Formal (Ent)
and then Scope (Ent) = Current_Subprogram
then
declare
DT : Boolean := False;
begin
Check_Static_Type (Etype (Ent), Empty, DT);
end;
end if;
end if;
end if;
end case;
-- Fall through to continue scanning children of this node
return OK;
end Visit_Node;
-- Start of processing for Build_Tables
begin
-- Traverse the body to get subprograms, calls and uplevel references
Visit (Subp_Body);
end Build_Tables;
-- Now do the first transitive closure which determines which
-- subprograms in the nest are actually reachable.
Reachable_Closure : declare
Modified : Boolean;
begin
Subps.Table (Subps_First).Reachable := True;
-- We use a simple minded algorithm as follows (obviously this can
-- be done more efficiently, using one of the standard algorithms
-- for efficient transitive closure computation, but this is simple
-- and most likely fast enough that its speed does not matter).
-- Repeatedly scan the list of calls. Any time we find a call from
-- A to B, where A is reachable, but B is not, then B is reachable,
-- and note that we have made a change by setting Modified True. We
-- repeat this until we make a pass with no modifications.
Outer : loop
Modified := False;
Inner : for J in Calls.First .. Calls.Last loop
declare
CTJ : Call_Entry renames Calls.Table (J);
SINF : constant SI_Type := Subp_Index (CTJ.Caller);
SINT : constant SI_Type := Subp_Index (CTJ.Callee);
SUBF : Subp_Entry renames Subps.Table (SINF);
SUBT : Subp_Entry renames Subps.Table (SINT);
begin
if SUBF.Reachable and then not SUBT.Reachable then
SUBT.Reachable := True;
Modified := True;
end if;
end;
end loop Inner;
exit Outer when not Modified;
end loop Outer;
end Reachable_Closure;
-- Remove calls from unreachable subprograms
declare
New_Index : Nat;
begin
New_Index := 0;
for J in Calls.First .. Calls.Last loop
declare
CTJ : Call_Entry renames Calls.Table (J);
SINF : constant SI_Type := Subp_Index (CTJ.Caller);
SINT : constant SI_Type := Subp_Index (CTJ.Callee);
SUBF : Subp_Entry renames Subps.Table (SINF);
SUBT : Subp_Entry renames Subps.Table (SINT);
begin
if SUBF.Reachable then
pragma Assert (SUBT.Reachable);
New_Index := New_Index + 1;
Calls.Table (New_Index) := Calls.Table (J);
end if;
end;
end loop;
Calls.Set_Last (New_Index);
end;
-- Remove uplevel references from unreachable subprograms
declare
New_Index : Nat;
begin
New_Index := 0;
for J in Urefs.First .. Urefs.Last loop
declare
URJ : Uref_Entry renames Urefs.Table (J);
SINF : constant SI_Type := Subp_Index (URJ.Caller);
SINT : constant SI_Type := Subp_Index (URJ.Callee);
SUBF : Subp_Entry renames Subps.Table (SINF);
SUBT : Subp_Entry renames Subps.Table (SINT);
S : Entity_Id;
begin
-- Keep reachable reference
if SUBF.Reachable then
New_Index := New_Index + 1;
Urefs.Table (New_Index) := Urefs.Table (J);
-- And since we know we are keeping this one, this is a good
-- place to fill in information for a good reference.
-- Mark all enclosing subprograms need to declare AREC
S := URJ.Caller;
loop
S := Enclosing_Subprogram (S);
-- If we are at the top level, as can happen with
-- references to formals in aspects of nested subprogram
-- declarations, there are no further subprograms to mark
-- as requiring activation records.
exit when No (S);
declare
SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
begin
SUBI.Declares_AREC := True;
-- If this entity was marked reachable because it is
-- in a task or protected type, there may not appear
-- to be any calls to it, which would normally adjust
-- the levels of the parent subprograms. So we need to
-- be sure that the uplevel reference of that entity
-- takes into account possible calls.
if In_Synchronized_Unit (SUBF.Ent)
and then SUBT.Lev < SUBI.Uplevel_Ref
then
SUBI.Uplevel_Ref := SUBT.Lev;
end if;
end;
exit when S = URJ.Callee;
end loop;
-- Add to list of uplevel referenced entities for Callee.
-- We do not add types to this list, only actual references
-- to objects that will be referenced uplevel, and we use
-- the flag Is_Uplevel_Referenced_Entity to avoid making
-- duplicate entries in the list. Discriminants are also
-- excluded, only the enclosing object can appear in the
-- list.
if not Is_Uplevel_Referenced_Entity (URJ.Ent)
and then Ekind (URJ.Ent) /= E_Discriminant
then
Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
Append_New_Elmt (URJ.Ent, SUBT.Uents);
end if;
-- And set uplevel indication for caller
if SUBT.Lev < SUBF.Uplevel_Ref then
SUBF.Uplevel_Ref := SUBT.Lev;
end if;
end if;
end;
end loop;
Urefs.Set_Last (New_Index);
end;
-- Remove unreachable subprograms from Subps table. Note that we do
-- this after eliminating entries from the other two tables, since
-- those elimination steps depend on referencing the Subps table.
declare
New_SI : SI_Type;
begin
New_SI := Subps_First - 1;
for J in Subps_First .. Subps.Last loop
declare
STJ : Subp_Entry renames Subps.Table (J);
Spec : Node_Id;
Decl : Node_Id;
begin
-- Subprogram is reachable, copy and reset index
if STJ.Reachable then
New_SI := New_SI + 1;
Subps.Table (New_SI) := STJ;
Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
-- Subprogram is not reachable
else
-- Clear index, since no longer active
Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
-- Output debug information if -gnatd.3 set
if Debug_Flag_Dot_3 then
Write_Str ("Eliminate ");
Write_Name (Chars (Subps.Table (J).Ent));
Write_Str (" at ");
Write_Location (Sloc (Subps.Table (J).Ent));
Write_Str (" (not referenced)");
Write_Eol;
end if;
-- Rewrite declaration, body, and corresponding freeze node
-- to null statements.
-- A subprogram instantiation does not have an explicit
-- body. If unused, we could remove the corresponding
-- wrapper package and its body.
if Present (STJ.Bod) then
Spec := Corresponding_Spec (STJ.Bod);
if Present (Spec) then
Decl := Parent (Declaration_Node (Spec));
Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
if Present (Freeze_Node (Spec)) then
Rewrite (Freeze_Node (Spec),
Make_Null_Statement (Sloc (Decl)));
end if;
end if;
Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
end if;
end if;
end;
end loop;
Subps.Set_Last (New_SI);
end;
-- Now it is time for the second transitive closure, which follows calls
-- and makes sure that A calls B, and B has uplevel references, then A
-- is also marked as having uplevel references.
Closure_Uplevel : declare
Modified : Boolean;
begin
-- We use a simple minded algorithm as follows (obviously this can
-- be done more efficiently, using one of the standard algorithms
-- for efficient transitive closure computation, but this is simple
-- and most likely fast enough that its speed does not matter).
-- Repeatedly scan the list of calls. Any time we find a call from
-- A to B, where B has uplevel references, make sure that A is marked
-- as having at least the same level of uplevel referencing.
Outer2 : loop
Modified := False;
Inner2 : for J in Calls.First .. Calls.Last loop
declare
CTJ : Call_Entry renames Calls.Table (J);
SINF : constant SI_Type := Subp_Index (CTJ.Caller);
SINT : constant SI_Type := Subp_Index (CTJ.Callee);
SUBF : Subp_Entry renames Subps.Table (SINF);
SUBT : Subp_Entry renames Subps.Table (SINT);
begin
if SUBT.Lev > SUBT.Uplevel_Ref
and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
then
SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
Modified := True;
end if;
end;
end loop Inner2;
exit Outer2 when not Modified;
end loop Outer2;
end Closure_Uplevel;
-- We have one more step before the tables are complete. An uplevel
-- call from subprogram A to subprogram B where subprogram B has uplevel
-- references is in effect an uplevel reference, and must arrange for
-- the proper activation link to be passed.
for J in Calls.First .. Calls.Last loop
declare
CTJ : Call_Entry renames Calls.Table (J);
SINF : constant SI_Type := Subp_Index (CTJ.Caller);
SINT : constant SI_Type := Subp_Index (CTJ.Callee);
SUBF : Subp_Entry renames Subps.Table (SINF);
SUBT : Subp_Entry renames Subps.Table (SINT);
A : Entity_Id;
begin
-- If callee has uplevel references
if SUBT.Uplevel_Ref < SUBT.Lev
-- And this is an uplevel call
and then SUBT.Lev < SUBF.Lev
then
-- We need to arrange for finding the uplink
A := CTJ.Caller;
loop
A := Enclosing_Subprogram (A);
Subps.Table (Subp_Index (A)).Declares_AREC := True;
exit when A = CTJ.Callee;
-- In any case exit when we get to the outer level. This
-- happens in some odd cases with generics (in particular
-- sem_ch3.adb does not compile without this kludge ???).
exit when A = Subp;
end loop;
end if;
end;
end loop;
-- The tables are now complete, so we can record the last index in the
-- Subps table for later reference in Cprint.
Subps.Table (Subps_First).Last := Subps.Last;
-- Next step, create the entities for code we will insert. We do this
-- at the start so that all the entities are defined, regardless of the
-- order in which we do the code insertions.
Create_Entities : for J in Subps_First .. Subps.Last loop
declare
STJ : Subp_Entry renames Subps.Table (J);
Loc : constant Source_Ptr := Sloc (STJ.Bod);
begin
-- First we create the ARECnF entity for the additional formal for
-- all subprograms which need an activation record passed.
if STJ.Uplevel_Ref < STJ.Lev then
STJ.ARECnF :=
Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
end if;
-- Define the AREC entities for the activation record if needed
if STJ.Declares_AREC then
STJ.ARECn :=
Make_Defining_Identifier (Loc, AREC_Name (J, ""));
STJ.ARECnT :=
Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
STJ.ARECnPT :=
Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
STJ.ARECnP :=
Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
-- Define uplink component entity if inner nesting case
if Present (STJ.ARECnF) then
STJ.ARECnU :=
Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
end if;
end if;
end;
end loop Create_Entities;
-- Loop through subprograms
Subp_Loop : declare
Addr : Entity_Id := Empty;
begin
for J in Subps_First .. Subps.Last loop
declare
STJ : Subp_Entry renames Subps.Table (J);
begin
-- First add the extra formal if needed. This applies to all
-- nested subprograms that require an activation record to be
-- passed, as indicated by ARECnF being defined.
if Present (STJ.ARECnF) then
-- Here we need the extra formal. We do the expansion and
-- analysis of this manually, since it is fairly simple,
-- and it is not obvious how we can get what we want if we
-- try to use the normal Analyze circuit.
Add_Extra_Formal : declare
Encl : constant SI_Type := Enclosing_Subp (J);
STJE : Subp_Entry renames Subps.Table (Encl);
-- Index and Subp_Entry for enclosing routine
Form : constant Entity_Id := STJ.ARECnF;
-- The formal to be added. Note that n here is one less
-- than the level of the subprogram itself (STJ.Ent).
procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
-- S is an N_Function/Procedure_Specification node, and F
-- is the new entity to add to this subprogram spec as
-- the last Extra_Formal.
----------------------
-- Add_Form_To_Spec --
----------------------
procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
Sub : constant Entity_Id := Defining_Entity (S);
Ent : Entity_Id;
begin
-- Case of at least one Extra_Formal is present, set
-- ARECnF as the new last entry in the list.
if Present (Extra_Formals (Sub)) then
Ent := Extra_Formals (Sub);
while Present (Extra_Formal (Ent)) loop
Ent := Extra_Formal (Ent);
end loop;
Set_Extra_Formal (Ent, F);
-- No Extra formals present
else
Set_Extra_Formals (Sub, F);
Ent := Last_Formal (Sub);
if Present (Ent) then
Set_Extra_Formal (Ent, F);
end if;
end if;
end Add_Form_To_Spec;
-- Start of processing for Add_Extra_Formal
begin
-- Decorate the new formal entity
Set_Scope (Form, STJ.Ent);
Mutate_Ekind (Form, E_In_Parameter);
Set_Etype (Form, STJE.ARECnPT);
Set_Mechanism (Form, By_Copy);
Set_Never_Set_In_Source (Form, True);
Set_Analyzed (Form, True);
Set_Comes_From_Source (Form, False);
Set_Is_Activation_Record (Form, True);
-- Case of only body present
if Acts_As_Spec (STJ.Bod) then
Add_Form_To_Spec (Form, Specification (STJ.Bod));
-- Case of separate spec
else
Add_Form_To_Spec (Form, Parent (STJ.Ent));
end if;
end Add_Extra_Formal;
end if;
-- Processing for subprograms that declare an activation record
if Present (STJ.ARECn) then
-- Local declarations for one such subprogram
declare
Loc : constant Source_Ptr := Sloc (STJ.Bod);
Decls : constant List_Id := New_List;
-- List of new declarations we create
Clist : List_Id;
Comp : Entity_Id;
Decl_Assign : Node_Id;
-- Assignment to set uplink, Empty if none
Decl_ARECnT : Node_Id;
Decl_ARECnPT : Node_Id;
Decl_ARECn : Node_Id;
Decl_ARECnP : Node_Id;
-- Declaration nodes for the AREC entities we build
begin
-- Build list of component declarations for ARECnT and
-- load System.Address.
Clist := Empty_List;
if No (Addr) then
Addr := RTE (RE_Address);
end if;
-- If we are in a subprogram that has a static link that
-- is passed in (as indicated by ARECnF being defined),
-- then include ARECnU : ARECmPT where ARECmPT comes from
-- the level one higher than the current level, and the
-- entity ARECnPT comes from the enclosing subprogram.
if Present (STJ.ARECnF) then
declare
STJE : Subp_Entry
renames Subps.Table (Enclosing_Subp (J));
begin
Append_To (Clist,
Make_Component_Declaration (Loc,
Defining_Identifier => STJ.ARECnU,
Component_Definition =>
Make_Component_Definition (Loc,
Subtype_Indication =>
New_Occurrence_Of (STJE.ARECnPT, Loc))));
end;
end if;
-- Add components for uplevel referenced entities
if Present (STJ.Uents) then
declare
Elmt : Elmt_Id;
Ptr_Decl : Node_Id;
Uent : Entity_Id;
Indx : Nat;
-- 1's origin of index in list of elements. This is
-- used to uniquify names if needed in Upref_Name.
begin
Elmt := First_Elmt (STJ.Uents);
Indx := 0;
while Present (Elmt) loop
Uent := Node (Elmt);
Indx := Indx + 1;
Comp :=
Make_Defining_Identifier (Loc,
Chars => Upref_Name (Uent, Indx, Clist));
Set_Activation_Record_Component
(Uent, Comp);
if Needs_Fat_Pointer (Uent) then
-- Build corresponding access type
Ptr_Decl :=
Build_Access_Type_Decl
(Etype (Uent), STJ.Ent);
Append_To (Decls, Ptr_Decl);
-- And use its type in the corresponding
-- component.
Append_To (Clist,
Make_Component_Declaration (Loc,
Defining_Identifier => Comp,
Component_Definition =>
Make_Component_Definition (Loc,
Subtype_Indication =>
New_Occurrence_Of
(Defining_Identifier (Ptr_Decl),
Loc))));
else
Append_To (Clist,
Make_Component_Declaration (Loc,
Defining_Identifier => Comp,
Component_Definition =>
Make_Component_Definition (Loc,
Subtype_Indication =>
New_Occurrence_Of (Addr, Loc))));
end if;
Next_Elmt (Elmt);
end loop;
end;
end if;
-- Now we can insert the AREC declarations into the body
-- type ARECnT is record .. end record;
-- pragma Suppress_Initialization (ARECnT);
-- Note that we need to set the Suppress_Initialization
-- flag after Decl_ARECnT has been analyzed.
Decl_ARECnT :=
Make_Full_Type_Declaration (Loc,
Defining_Identifier => STJ.ARECnT,
Type_Definition =>
Make_Record_Definition (Loc,
Component_List =>
Make_Component_List (Loc,
Component_Items => Clist)));
Append_To (Decls, Decl_ARECnT);
-- type ARECnPT is access all ARECnT;
Decl_ARECnPT :=
Make_Full_Type_Declaration (Loc,
Defining_Identifier => STJ.ARECnPT,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
All_Present => True,
Subtype_Indication =>
New_Occurrence_Of (STJ.ARECnT, Loc)));
Append_To (Decls, Decl_ARECnPT);
-- ARECn : aliased ARECnT;
Decl_ARECn :=
Make_Object_Declaration (Loc,
Defining_Identifier => STJ.ARECn,
Aliased_Present => True,
Object_Definition =>
New_Occurrence_Of (STJ.ARECnT, Loc));
Append_To (Decls, Decl_ARECn);
-- ARECnP : constant ARECnPT := ARECn'Access;
Decl_ARECnP :=
Make_Object_Declaration (Loc,
Defining_Identifier => STJ.ARECnP,
Constant_Present => True,
Object_Definition =>
New_Occurrence_Of (STJ.ARECnPT, Loc),
Expression =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (STJ.ARECn, Loc),
Attribute_Name => Name_Access));
Append_To (Decls, Decl_ARECnP);
-- If we are in a subprogram that has a static link that
-- is passed in (as indicated by ARECnF being defined),
-- then generate ARECn.ARECmU := ARECmF where m is
-- one less than the current level to set the uplink.
if Present (STJ.ARECnF) then
Decl_Assign :=
Make_Assignment_Statement (Loc,
Name =>
Make_Selected_Component (Loc,
Prefix =>
New_Occurrence_Of (STJ.ARECn, Loc),
Selector_Name =>
New_Occurrence_Of (STJ.ARECnU, Loc)),
Expression =>
New_Occurrence_Of (STJ.ARECnF, Loc));
Append_To (Decls, Decl_Assign);
else
Decl_Assign := Empty;
end if;
if No (Declarations (STJ.Bod)) then
Set_Declarations (STJ.Bod, Decls);
else
Prepend_List_To (Declarations (STJ.Bod), Decls);
end if;
-- Analyze the newly inserted declarations. Note that we
-- do not need to establish the whole scope stack, since
-- we have already set all entity fields (so there will
-- be no searching of upper scopes to resolve names). But
-- we do set the scope of the current subprogram, so that
-- newly created entities go in the right entity chain.
-- We analyze with all checks suppressed (since we do
-- not expect any exceptions).
Push_Scope (STJ.Ent);
Analyze (Decl_ARECnT, Suppress => All_Checks);
-- Note that we need to call Set_Suppress_Initialization
-- after Decl_ARECnT has been analyzed, but before
-- analyzing Decl_ARECnP so that the flag is properly
-- taking into account.
Set_Suppress_Initialization (STJ.ARECnT);
Analyze (Decl_ARECnPT, Suppress => All_Checks);
Analyze (Decl_ARECn, Suppress => All_Checks);
Analyze (Decl_ARECnP, Suppress => All_Checks);
if Present (Decl_Assign) then
Analyze (Decl_Assign, Suppress => All_Checks);
end if;
Pop_Scope;
-- Next step, for each uplevel referenced entity, add
-- assignment operations to set the component in the
-- activation record.
if Present (STJ.Uents) then
declare
Elmt : Elmt_Id;
begin
Elmt := First_Elmt (STJ.Uents);
while Present (Elmt) loop
declare
Ent : constant Entity_Id := Node (Elmt);
Loc : constant Source_Ptr := Sloc (Ent);
Dec : constant Node_Id :=
Declaration_Node (Ent);
Asn : Node_Id;
Attr : Name_Id;
Comp : Entity_Id;
Ins : Node_Id;
Rhs : Node_Id;
begin
-- For parameters, we insert the assignment
-- right after the declaration of ARECnP.
-- For all other entities, we insert the
-- assignment immediately after the
-- declaration of the entity or after the
-- freeze node if present.
-- Note: we don't need to mark the entity
-- as being aliased, because the address
-- attribute will mark it as Address_Taken,
-- and that is good enough.
if Is_Formal (Ent) then
Ins := Decl_ARECnP;
elsif Has_Delayed_Freeze (Ent) then
Ins := Freeze_Node (Ent);
else
Ins := Dec;
end if;
-- Build and insert the assignment:
-- ARECn.nam := nam'Address
-- or else 'Unchecked_Access for
-- unconstrained array.
if Needs_Fat_Pointer (Ent) then
Attr := Name_Unchecked_Access;
else
Attr := Name_Address;
end if;
Rhs :=
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Ent, Loc),
Attribute_Name => Attr);
-- If the entity is an unconstrained formal
-- we wrap the attribute reference in an
-- unchecked conversion to the type of the
-- activation record component, to prevent
-- spurious subtype conformance errors within
-- instances.
if Is_Formal (Ent)
and then not Is_Constrained (Etype (Ent))
then
-- Find target component and its type
Comp := First_Component (STJ.ARECnT);
while Chars (Comp) /= Chars (Ent) loop
Next_Component (Comp);
end loop;
Rhs :=
Unchecked_Convert_To (Etype (Comp), Rhs);
end if;
Asn :=
Make_Assignment_Statement (Loc,
Name =>
Make_Selected_Component (Loc,
Prefix =>
New_Occurrence_Of (STJ.ARECn, Loc),
Selector_Name =>
New_Occurrence_Of
(Activation_Record_Component
(Ent),
Loc)),
Expression => Rhs);
-- If we have a loop parameter, we have
-- to insert before the first statement
-- of the loop. Ins points to the
-- N_Loop_Parameter_Specification or to
-- an N_Iterator_Specification.
if Nkind (Ins) in
N_Iterator_Specification |
N_Loop_Parameter_Specification
then
-- Quantified expression are rewritten as
-- loops during expansion.
if Nkind (Parent (Ins)) =
N_Quantified_Expression
then
null;
else
Ins :=
First
(Statements
(Parent (Parent (Ins))));
Insert_Before (Ins, Asn);
end if;
else
Insert_After (Ins, Asn);
end if;
-- Analyze the assignment statement. We do
-- not need to establish the relevant scope
-- stack entries here, because we have
-- already set the correct entity references,
-- so no name resolution is required, and no
-- new entities are created, so we don't even
-- need to set the current scope.
-- We analyze with all checks suppressed
-- (since we do not expect any exceptions).
Analyze (Asn, Suppress => All_Checks);
end;
Next_Elmt (Elmt);
end loop;
end;
end if;
end;
end if;
end;
end loop;
end Subp_Loop;
-- Next step, process uplevel references. This has to be done in a
-- separate pass, after completing the processing in Sub_Loop because we
-- need all the AREC declarations generated, inserted, and analyzed so
-- that the uplevel references can be successfully analyzed.
Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
declare
UPJ : Uref_Entry renames Urefs.Table (J);
begin
-- Ignore type references, these are implicit references that do
-- not need rewriting (e.g. the appearance in a conversion).
-- Also ignore if no reference was specified or if the rewriting
-- has already been done (this can happen if the N_Identifier
-- occurs more than one time in the tree). Also ignore references
-- when not generating C code (in particular for the case of LLVM,
-- since GNAT-LLVM will handle the processing for up-level refs).
if No (UPJ.Ref)
or else not Is_Entity_Name (UPJ.Ref)
or else not Present (Entity (UPJ.Ref))
or else not Opt.Generate_C_Code
then
goto Continue;
end if;
-- Rewrite one reference
Rewrite_One_Ref : declare
Loc : constant Source_Ptr := Sloc (UPJ.Ref);
-- Source location for the reference
Typ : constant Entity_Id := Etype (UPJ.Ent);
-- The type of the referenced entity
Atyp : Entity_Id;
-- The actual subtype of the reference
RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
-- Subp_Index for caller containing reference
STJR : Subp_Entry renames Subps.Table (RS_Caller);
-- Subp_Entry for subprogram containing reference
RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
-- Subp_Index for subprogram containing referenced entity
STJE : Subp_Entry renames Subps.Table (RS_Callee);
-- Subp_Entry for subprogram containing referenced entity
Pfx : Node_Id;
Comp : Entity_Id;
SI : SI_Type;
begin
Atyp := Etype (UPJ.Ref);
if Ekind (Atyp) /= E_Record_Subtype then
Atyp := Get_Actual_Subtype (UPJ.Ref);
end if;
-- Ignore if no ARECnF entity for enclosing subprogram which
-- probably happens as a result of not properly treating
-- instance bodies. To be examined ???
-- If this test is omitted, then the compilation of freeze.adb
-- and inline.adb fail in unnesting mode.
if No (STJR.ARECnF) then
goto Continue;
end if;
-- If this is a reference to a global constant, use its value
-- rather than create a reference. It is more efficient and
-- furthermore indispensable if the context requires a
-- constant, such as a branch of a case statement.
if Ekind (UPJ.Ent) = E_Constant
and then Is_True_Constant (UPJ.Ent)
and then Present (Constant_Value (UPJ.Ent))
and then Is_Static_Expression (Constant_Value (UPJ.Ent))
then
Rewrite (UPJ.Ref, New_Copy_Tree (Constant_Value (UPJ.Ent)));
goto Continue;
end if;
-- Push the current scope, so that the pointer type Tnn, and
-- any subsidiary entities resulting from the analysis of the
-- rewritten reference, go in the right entity chain.
Push_Scope (STJR.Ent);
-- Now we need to rewrite the reference. We have a reference
-- from level STJR.Lev to level STJE.Lev. The general form of
-- the rewritten reference for entity X is:
-- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
-- where a,b,c,d .. m =
-- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
pragma Assert (STJR.Lev > STJE.Lev);
-- Compute the prefix of X. Here are examples to make things
-- clear (with parens to show groupings, the prefix is
-- everything except the .X at the end).
-- level 2 to level 1
-- AREC1F.X
-- level 3 to level 1
-- (AREC2F.AREC1U).X
-- level 4 to level 1
-- ((AREC3F.AREC2U).AREC1U).X
-- level 6 to level 2
-- (((AREC5F.AREC4U).AREC3U).AREC2U).X
-- In the above, ARECnF and ARECnU are pointers, so there are
-- explicit dereferences required for these occurrences.
Pfx :=
Make_Explicit_Dereference (Loc,
Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
SI := RS_Caller;
for L in STJE.Lev .. STJR.Lev - 2 loop
SI := Enclosing_Subp (SI);
Pfx :=
Make_Explicit_Dereference (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix => Pfx,
Selector_Name =>
New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
end loop;
-- Get activation record component (must exist)
Comp := Activation_Record_Component (UPJ.Ent);
pragma Assert (Present (Comp));
-- Do the replacement. If the component type is an access type,
-- this is an uplevel reference for an entity that requires a
-- fat pointer, so dereference the component.
if Is_Access_Type (Etype (Comp)) then
Rewrite (UPJ.Ref,
Make_Explicit_Dereference (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix => Pfx,
Selector_Name =>
New_Occurrence_Of (Comp, Loc))));
else
Rewrite (UPJ.Ref,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Atyp, Loc),
Attribute_Name => Name_Deref,
Expressions => New_List (
Make_Selected_Component (Loc,
Prefix => Pfx,
Selector_Name =>
New_Occurrence_Of (Comp, Loc)))));
end if;
-- Analyze and resolve the new expression. We do not need to
-- establish the relevant scope stack entries here, because we
-- have already set all the correct entity references, so no
-- name resolution is needed. We have already set the current
-- scope, so that any new entities created will be in the right
-- scope.
-- We analyze with all checks suppressed (since we do not
-- expect any exceptions)
Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
-- Generate an extra temporary to facilitate the C backend
-- processing this dereference
if Opt.Modify_Tree_For_C
and then Nkind (Parent (UPJ.Ref)) in
N_Type_Conversion | N_Unchecked_Type_Conversion
then
Force_Evaluation (UPJ.Ref, Mode => Strict);
end if;
Pop_Scope;
end Rewrite_One_Ref;
end;
<<Continue>>
null;
end loop Uplev_Refs;
-- Finally, loop through all calls adding extra actual for the
-- activation record where it is required.
Adjust_Calls : for J in Calls.First .. Calls.Last loop
-- Process a single call, we are only interested in a call to a
-- subprogram that actually needs a pointer to an activation record,
-- as indicated by the ARECnF entity being set. This excludes the
-- top level subprogram, and any subprogram not having uplevel refs.
Adjust_One_Call : declare
CTJ : Call_Entry renames Calls.Table (J);
STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
Loc : constant Source_Ptr := Sloc (CTJ.N);
Extra : Node_Id;
ExtraP : Node_Id;
SubX : SI_Type;
Act : Node_Id;
begin
if Present (STT.ARECnF)
and then Nkind (CTJ.N) in N_Subprogram_Call
then
-- CTJ.N is a call to a subprogram which may require a pointer
-- to an activation record. The subprogram containing the call
-- is CTJ.From and the subprogram being called is CTJ.To, so we
-- have a call from level STF.Lev to level STT.Lev.
-- There are three possibilities:
-- For a call to the same level, we just pass the activation
-- record passed to the calling subprogram.
if STF.Lev = STT.Lev then
Extra := New_Occurrence_Of (STF.ARECnF, Loc);
-- For a call that goes down a level, we pass a pointer to the
-- activation record constructed within the caller (which may
-- be the outer-level subprogram, but also may be a more deeply
-- nested caller).
elsif STT.Lev = STF.Lev + 1 then
Extra := New_Occurrence_Of (STF.ARECnP, Loc);
-- Otherwise we must have an upcall (STT.Lev < STF.LEV),
-- since it is not possible to do a downcall of more than
-- one level.
-- For a call from level STF.Lev to level STT.Lev, we
-- have to find the activation record needed by the
-- callee. This is as follows:
-- ARECaF.ARECbU.ARECcU....ARECmU
-- where a,b,c .. m =
-- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
else
pragma Assert (STT.Lev < STF.Lev);
Extra := New_Occurrence_Of (STF.ARECnF, Loc);
SubX := Subp_Index (CTJ.Caller);
for K in reverse STT.Lev .. STF.Lev - 1 loop
SubX := Enclosing_Subp (SubX);
Extra :=
Make_Selected_Component (Loc,
Prefix => Extra,
Selector_Name =>
New_Occurrence_Of
(Subps.Table (SubX).ARECnU, Loc));
end loop;
end if;
-- Extra is the additional parameter to be added. Build a
-- parameter association that we can append to the actuals.
ExtraP :=
Make_Parameter_Association (Loc,
Selector_Name =>
New_Occurrence_Of (STT.ARECnF, Loc),
Explicit_Actual_Parameter => Extra);
if No (Parameter_Associations (CTJ.N)) then
Set_Parameter_Associations (CTJ.N, Empty_List);
end if;
Append (ExtraP, Parameter_Associations (CTJ.N));
-- We need to deal with the actual parameter chain as well. The
-- newly added parameter is always the last actual.
Act := First_Named_Actual (CTJ.N);
if No (Act) then
Set_First_Named_Actual (CTJ.N, Extra);
-- If call has been relocated (as with an expression in
-- an aggregate), set First_Named pointer in original node
-- as well, because that's the parent of the parameter list.
Set_First_Named_Actual
(Parent (List_Containing (ExtraP)), Extra);
-- Here we must follow the chain and append the new entry
else
loop
declare
PAN : Node_Id;
NNA : Node_Id;
begin
PAN := Parent (Act);
pragma Assert (Nkind (PAN) = N_Parameter_Association);
NNA := Next_Named_Actual (PAN);
if No (NNA) then
Set_Next_Named_Actual (PAN, Extra);
exit;
end if;
Act := NNA;
end;
end loop;
end if;
-- Analyze and resolve the new actual. We do not need to
-- establish the relevant scope stack entries here, because
-- we have already set all the correct entity references, so
-- no name resolution is needed.
-- We analyze with all checks suppressed (since we do not
-- expect any exceptions, and also we temporarily turn off
-- Unested_Subprogram_Mode to avoid trying to mark uplevel
-- references (not needed at this stage, and in fact causes
-- a bit of recursive chaos).
Opt.Unnest_Subprogram_Mode := False;
Analyze_And_Resolve
(Extra, Etype (STT.ARECnF), Suppress => All_Checks);
Opt.Unnest_Subprogram_Mode := True;
end if;
end Adjust_One_Call;
end loop Adjust_Calls;
return;
end Unnest_Subprogram;
------------------------
-- Unnest_Subprograms --
------------------------
procedure Unnest_Subprograms (N : Node_Id) is
function Search_Subprograms (N : Node_Id) return Traverse_Result;
-- Tree visitor that search for outer level procedures with nested
-- subprograms and invokes Unnest_Subprogram()
---------------
-- Do_Search --
---------------
procedure Do_Search is new Traverse_Proc (Search_Subprograms);
-- Subtree visitor instantiation
------------------------
-- Search_Subprograms --
------------------------
function Search_Subprograms (N : Node_Id) return Traverse_Result is
begin
if Nkind (N) in N_Subprogram_Body | N_Subprogram_Body_Stub then
declare
Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
begin
-- We are only interested in subprograms (not generic
-- subprograms), that have nested subprograms.
if Is_Subprogram (Spec_Id)
and then Has_Nested_Subprogram (Spec_Id)
and then Is_Library_Level_Entity (Spec_Id)
then
Unnest_Subprogram (Spec_Id, N);
end if;
end;
-- The proper body of a stub may contain nested subprograms, and
-- therefore must be visited explicitly. Nested stubs are examined
-- recursively in Visit_Node.
elsif Nkind (N) in N_Body_Stub then
Do_Search (Library_Unit (N));
-- Skip generic packages
elsif Nkind (N) = N_Package_Body
and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
then
return Skip;
end if;
return OK;
end Search_Subprograms;
Subp : Entity_Id;
Subp_Body : Node_Id;
-- Start of processing for Unnest_Subprograms
begin
if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
return;
end if;
-- A specification will contain bodies if it contains instantiations so
-- examine package or subprogram declaration of the main unit, when it
-- is present.
if Nkind (Unit (N)) = N_Package_Body
or else (Nkind (Unit (N)) = N_Subprogram_Body
and then not Acts_As_Spec (N))
then
Do_Search (Library_Unit (N));
end if;
Do_Search (N);
-- Unnest any subprograms passed on the list of inlined subprograms
Subp := First_Inlined_Subprogram (N);
while Present (Subp) loop
Subp_Body := Parent (Declaration_Node (Subp));
if Nkind (Subp_Body) = N_Subprogram_Declaration
and then Present (Corresponding_Body (Subp_Body))
then
Subp_Body := Parent (Declaration_Node
(Corresponding_Body (Subp_Body)));
end if;
Unnest_Subprogram (Subp, Subp_Body, For_Inline => True);
Next_Inlined_Subprogram (Subp);
end loop;
end Unnest_Subprograms;
end Exp_Unst;
|