1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 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 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- E X P _ C H 7 --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2024, 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. --
-- --
------------------------------------------------------------------------------
-- This package contains virtually all expansion mechanisms related to
-- - controlled types
-- - transient scopes
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 Errout; use Errout;
with Exp_Ch6; use Exp_Ch6;
with Exp_Ch9; use Exp_Ch9;
with Exp_Ch11; use Exp_Ch11;
with Exp_Dbug; use Exp_Dbug;
with Exp_Dist; use Exp_Dist;
with Exp_Disp; use Exp_Disp;
with Exp_Prag; use Exp_Prag;
with Exp_Tss; use Exp_Tss;
with Exp_Util; use Exp_Util;
with Freeze; use Freeze;
with GNAT_CUDA; use GNAT_CUDA;
with Lib; use Lib;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Output; use Output;
with Restrict; use Restrict;
with Rident; use Rident;
with Rtsfind; use Rtsfind;
with Sinfo; use Sinfo;
with Sinfo.Nodes; use Sinfo.Nodes;
with Sinfo.Utils; use Sinfo.Utils;
with Sem; use Sem;
with Sem_Aux; use Sem_Aux;
with Sem_Ch7; use Sem_Ch7;
with Sem_Ch8; use Sem_Ch8;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Snames; use Snames;
with Stand; use Stand;
with Tbuild; use Tbuild;
with Ttypes; use Ttypes;
with Uintp; use Uintp;
package body Exp_Ch7 is
-----------------------------
-- Finalization Management --
-----------------------------
-- This part describes how Initialization/Adjustment/Finalization
-- procedures are generated and called. Two cases must be considered: types
-- that are Controlled (Is_Controlled flag set) and composite types that
-- contain controlled components (Has_Controlled_Component flag set). In
-- the first case the procedures to call are the user-defined primitive
-- operations Initialize/Adjust/Finalize. In the second case, GNAT
-- generates Deep_Initialize, Deep_Adjust and Deep_Finalize that are in
-- charge of calling the former procedures on the controlled components.
-- For records with Has_Controlled_Component set, a hidden "controller"
-- component is inserted. This controller component contains its own
-- finalization list on which all controlled components are attached
-- creating an indirection on the upper-level Finalization list. This
-- technique facilitates the management of objects whose number of
-- controlled components changes during execution. This controller
-- component is itself controlled and is attached to the upper-level
-- finalization chain. Its adjust primitive is in charge of calling adjust
-- on the components and adjusting the finalization pointer to match their
-- new location (see a-finali.adb).
-- It is not possible to use a similar technique for arrays that have
-- Has_Controlled_Component set. In this case, deep procedures are
-- generated that call initialize/adjust/finalize + attachment or
-- detachment on the finalization list for all component.
-- Initialize calls: they are generated for declarations or dynamic
-- allocations of Controlled objects with no initial value. They are always
-- followed by an attachment to the current Finalization Chain. For the
-- dynamic allocation case this the chain attached to the scope of the
-- access type definition otherwise, this is the chain of the current
-- scope.
-- Adjust Calls: They are generated on 2 occasions: (1) for declarations
-- or dynamic allocations of Controlled objects with an initial value.
-- (2) after an assignment. In the first case they are followed by an
-- attachment to the final chain, in the second case they are not.
-- Finalization Calls: They are generated on (1) scope exit, (2)
-- assignments, (3) unchecked deallocations. In case (3) they have to
-- be detached from the final chain, in case (2) they must not and in
-- case (1) this is not important since we are exiting the scope anyway.
-- Other details:
-- Type extensions will have a new record controller at each derivation
-- level containing controlled components. The record controller for
-- the parent/ancestor is attached to the finalization list of the
-- extension's record controller (i.e. the parent is like a component
-- of the extension).
-- For types that are both Is_Controlled and Has_Controlled_Components,
-- the record controller and the object itself are handled separately.
-- It could seem simpler to attach the object at the end of its record
-- controller but this would not tackle view conversions properly.
-- A classwide type can always potentially have controlled components
-- but the record controller of the corresponding actual type may not
-- be known at compile time so the dispatch table contains a special
-- field that allows computation of the offset of the record controller
-- dynamically. See s-finimp.Deep_Tag_Attach and a-tags.RC_Offset.
-- Here is a simple example of the expansion of a controlled block :
-- declare
-- X : Controlled;
-- Y : Controlled := Init;
--
-- type R is record
-- C : Controlled;
-- end record;
-- W : R;
-- Z : R := (C => X);
-- begin
-- X := Y;
-- W := Z;
-- end;
--
-- is expanded into
--
-- declare
-- _L : System.FI.Finalizable_Ptr;
-- procedure _Clean is
-- begin
-- Abort_Defer;
-- System.FI.Finalize_List (_L);
-- Abort_Undefer;
-- end _Clean;
-- X : Controlled;
-- begin
-- Abort_Defer;
-- Initialize (X);
-- Attach_To_Final_List (_L, Finalizable (X), 1);
-- at end: Abort_Undefer;
-- Y : Controlled := Init;
-- Adjust (Y);
-- Attach_To_Final_List (_L, Finalizable (Y), 1);
--
-- type R is record
-- C : Controlled;
-- end record;
-- W : R;
-- begin
-- Abort_Defer;
-- Deep_Initialize (W, _L, 1);
-- at end: Abort_Under;
-- Z : R := (C => X);
-- Deep_Adjust (Z, _L, 1);
-- begin
-- _Assign (X, Y);
-- Deep_Finalize (W, False);
-- <save W's final pointers>
-- W := Z;
-- <restore W's final pointers>
-- Deep_Adjust (W, _L, 0);
-- at end
-- _Clean;
-- end;
type Final_Primitives is
(Initialize_Case, Adjust_Case, Finalize_Case, Address_Case);
-- This enumeration type is defined in order to ease sharing code for
-- building finalization procedures for composite types.
Name_Of : constant array (Final_Primitives) of Name_Id :=
(Initialize_Case => Name_Initialize,
Adjust_Case => Name_Adjust,
Finalize_Case => Name_Finalize,
Address_Case => Name_Finalize_Address);
Deep_Name_Of : constant array (Final_Primitives) of TSS_Name_Type :=
(Initialize_Case => TSS_Deep_Initialize,
Adjust_Case => TSS_Deep_Adjust,
Finalize_Case => TSS_Deep_Finalize,
Address_Case => TSS_Finalize_Address);
function Allows_Finalization_Master (Typ : Entity_Id) return Boolean;
-- Determine whether access type Typ may have a finalization master
procedure Build_Array_Deep_Procs (Typ : Entity_Id);
-- Build the deep Initialize/Adjust/Finalize for a record Typ with
-- Has_Controlled_Component set and store them using the TSS mechanism.
function Build_Cleanup_Statements
(N : Node_Id;
Additional_Cleanup : List_Id) return List_Id;
-- Create the cleanup calls for an asynchronous call block, task master,
-- protected subprogram body, task allocation block or task body, or
-- additional cleanup actions parked on a transient block. If the context
-- does not contain the above constructs, the routine returns an empty
-- list.
procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id);
-- N is a construct that contains a handled sequence of statements, Fin_Id
-- is the entity of a finalizer. Create an At_End handler that covers the
-- statements of N and calls Fin_Id. If the handled statement sequence has
-- an exception handler, the statements will be wrapped in a block to avoid
-- unwanted interaction with the new At_End handler.
procedure Build_Record_Deep_Procs (Typ : Entity_Id);
-- Build the deep Initialize/Adjust/Finalize for a record Typ with
-- Has_Component_Component set and store them using the TSS mechanism.
--------------------------------
-- Transient Scope Management --
--------------------------------
-- A transient scope is needed when certain temporary objects are created
-- by the compiler. These temporary objects are allocated on the secondary
-- stack and/or need finalization, and the transient scope is responsible
-- for finalizing the objects and reclaiming the memory of the secondary
-- stack at the appropriate time. They are generally objects allocated to
-- store the result of a function returning an unconstrained or controlled
-- value. Expressions needing to be wrapped in a transient scope may appear
-- in three different contexts, which lead to different kinds of transient
-- scope expansion:
-- 1. In a simple statement (procedure call, assignment, ...). In this
-- case the statement is wrapped into a transient block, which takes
-- care of the finalization actions as well as the secondary stack
-- deallocation, See Wrap_Transient_Statement for details.
-- 2. In an expression of a control structure (test in a If statement,
-- expression in a Case statement, ...). In this case the expression
-- is replaced by a temporary and the enclosing statement is wrapped
-- into a transient block, which takes care of the finalization actions
-- and the secondary stack deallocation. See Wrap_Transient_Expression
-- for details.
-- 3. In an expression of an object declaration. No wrapping is possible
-- here, so the finalization actions performed on the normal path, if
-- any, are done right after the declaration, and those performed on
-- the exceptional path, as well as the secondary stack deallocation,
-- are deferred to the enclosing scope. See Wrap_Transient_Declaration
-- for details.
-- A transient scope is created by calling Establish_Transient_Scope on the
-- node that needs to be serviced by it (the serviced node can subsequently
-- be retrieved by invoking Node_To_Be_Wrapped when the current scope is a
-- transient scope). Once this has been done, the normal processing of the
-- Insert_Actions procedures is blocked and the procedures are redirected
-- to the Store_xxx_Actions_In_Scope procedures and Store_Actions_In_Scope
-- is ultimately invoked to store the pending actions.
-- A transient scope is finalized by calling one of the Wrap_Transient_xxx
-- procedures depending on the context as explained above. They ultimately
-- invoke Insert_Actions_In_Scope_Around as per the following picture:
-- Wrap_Transient_Expression Wrap_Transient_Statement
-- | |
-- V V
-- Make_Transient_Block
-- |
-- Wrap_Transient_Declaration |
-- | |
-- V V
-- Insert_Actions_In_Scope_Around
procedure Insert_Actions_In_Scope_Around
(N : Node_Id;
Clean : Boolean;
Manage_SS : Boolean);
-- Insert the before-actions kept in the scope stack before N, and the
-- after-actions after N, which must be a member of a list. If Clean is
-- true, insert any cleanup actions kept in the scope stack and generate
-- required finalization actions for the before-actions and after-actions.
-- If Manage_SS is true, insert calls to mark/release the secondary stack.
function Make_Transient_Block
(Loc : Source_Ptr;
Action : Node_Id;
Par : Node_Id) return Node_Id;
-- Action is a single statement or object declaration. Par is the proper
-- parent of the generated block. Create a transient block whose name is
-- the current scope and the only handled statement is Action. If Action
-- involves controlled objects or secondary stack usage, the corresponding
-- cleanup actions are performed at the end of the block.
procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id);
-- Shared processing for Store_xxx_Actions_In_Scope
-------------------------------------------
-- Unnesting procedures for CCG and LLVM --
-------------------------------------------
-- Expansion generates subprograms for controlled types management that
-- may appear in declarative lists in package declarations and bodies.
-- These subprograms appear within generated blocks that contain local
-- declarations and a call to finalization procedures. To ensure that
-- such subprograms get activation records when needed, we transform the
-- block into a procedure body, followed by a call to it in the same
-- declarative list.
procedure Check_Unnesting_Elaboration_Code (N : Node_Id);
-- The statement part of a package body that is a compilation unit may
-- contain blocks that declare local subprograms. In Subprogram_Unnesting_
-- Mode such subprograms must be handled as nested inside the (implicit)
-- elaboration procedure that executes that statement part. To handle
-- properly uplevel references we construct that subprogram explicitly,
-- to contain blocks and inner subprograms, the statement part becomes
-- a call to this subprogram. This is only done if blocks are present
-- in the statement list of the body. (It would be nice to unify this
-- procedure with Check_Unnesting_In_Decls_Or_Stmts, if possible, since
-- they're doing very similar work, but are structured differently. ???)
procedure Check_Unnesting_In_Decls_Or_Stmts (Decls_Or_Stmts : List_Id);
-- Similarly, the declarations or statements in library-level packages may
-- have created blocks with nested subprograms. Such a block must be
-- transformed into a procedure followed by a call to it, so that unnesting
-- can handle uplevel references within these nested subprograms (typically
-- subprograms that handle finalization actions). This also applies to
-- nested packages, including instantiations, in which case it must
-- recursively process inner bodies.
procedure Check_Unnesting_In_Handlers (N : Node_Id);
-- Similarly, check for blocks with nested subprograms occurring within
-- a set of exception handlers associated with a package body N.
procedure Unnest_Block (Decl : Node_Id);
-- Blocks that contain nested subprograms with up-level references need to
-- create activation records for them. We do this by rewriting the block as
-- a procedure, followed by a call to it in the same declarative list, to
-- replicate the semantics of the original block.
--
-- A common source for such block is a transient block created for a
-- construct (declaration, assignment, etc.) that involves controlled
-- actions or secondary-stack management, in which case the nested
-- subprogram is a finalizer.
procedure Unnest_If_Statement (If_Stmt : Node_Id);
-- The separate statement lists associated with an if-statement (then part,
-- elsif parts, else part) may require unnesting if they directly contain
-- a subprogram body that references up-level objects. Each statement list
-- is traversed to locate such subprogram bodies, and if a part's statement
-- list contains a body, then the list is replaced with a new procedure
-- containing the part's statements followed by a call to the procedure.
-- Furthermore, any nested blocks, loops, or if statements will also be
-- traversed to determine the need for further unnesting transformations.
procedure Unnest_Statement_List (Stmts : in out List_Id);
-- A list of statements that directly contains a subprogram at its outer
-- level, that may reference objects declared in that same statement list,
-- is rewritten as a procedure containing the statement list Stmts (which
-- includes any such objects as well as the nested subprogram), followed by
-- a call to the new procedure, and Stmts becomes the list containing the
-- procedure and the call. This ensures that Unnest_Subprogram will later
-- properly handle up-level references from the nested subprogram to
-- objects declared earlier in statement list, by creating an activation
-- record and passing it to the nested subprogram. This procedure also
-- resets the Scope of objects declared in the statement list, as well as
-- the Scope of the nested subprogram, to refer to the new procedure.
-- Also, the new procedure is marked Has_Nested_Subprogram, so this should
-- only be called when known that the statement list contains a subprogram.
procedure Unnest_Loop (Loop_Stmt : Node_Id);
-- Top-level Loops that contain nested subprograms with up-level references
-- need to have activation records. We do this by rewriting the loop as a
-- procedure containing the loop, followed by a call to the procedure in
-- the same library-level declarative list, to replicate the semantics of
-- the original loop. Such loops can occur due to aggregate expansions and
-- other constructs.
procedure Check_Visibly_Controlled
(Prim : Final_Primitives;
Typ : Entity_Id;
E : in out Entity_Id;
Cref : in out Node_Id);
-- The controlled operation declared for a derived type may not be
-- overriding, if the controlled operations of the parent type are hidden,
-- for example when the parent is a private type whose full view is
-- controlled. For other primitive operations we modify the name of the
-- operation to indicate that it is not overriding, but this is not
-- possible for Initialize, etc. because they have to be retrievable by
-- name. Before generating the proper call to one of these operations we
-- check whether Typ is known to be controlled at the point of definition.
-- If it is not then we must retrieve the hidden operation of the parent
-- and use it instead. This is one case that might be solved more cleanly
-- once Overriding pragmas or declarations are in place.
function Contains_Subprogram (Blk : Entity_Id) return Boolean;
-- Check recursively whether a loop or block contains a subprogram that
-- may need an activation record.
function Convert_View (Proc : Entity_Id; Arg : Node_Id) return Node_Id;
-- Proc is one of the Initialize/Adjust/Finalize operations, and Arg is the
-- argument being passed to it. This function will, if necessary, generate
-- a conversion between the partial and full view of Arg to match the type
-- of the formal of Proc, or force a conversion to the class-wide type in
-- the case where the operation is abstract.
function Make_Call
(Loc : Source_Ptr;
Proc_Id : Entity_Id;
Param : Node_Id;
Skip_Self : Boolean := False) return Node_Id;
-- Subsidiary to Make_Adjust_Call and Make_Final_Call. Given the entity of
-- routine [Deep_]Adjust or [Deep_]Finalize and an object parameter, create
-- an adjust or finalization call. When flag Skip_Self is set, the related
-- action has an effect on the components only (if any).
function Make_Deep_Proc
(Prim : Final_Primitives;
Typ : Entity_Id;
Stmts : List_Id) return Entity_Id;
-- This function generates the tree for Deep_Initialize, Deep_Adjust or
-- Deep_Finalize procedures according to the first parameter. These
-- procedures operate on the type Typ. The Stmts parameter gives the
-- body of the procedure.
function Make_Deep_Array_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id;
-- This function generates the list of statements for implementing
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
-- the first parameter, these procedures operate on the array type Typ.
function Make_Deep_Record_Body
(Prim : Final_Primitives;
Typ : Entity_Id;
Is_Local : Boolean := False) return List_Id;
-- This function generates the list of statements for implementing
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
-- the first parameter, these procedures operate on the record type Typ.
-- Flag Is_Local is used in conjunction with Deep_Finalize to designate
-- whether the inner logic should be dictated by state counters.
function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id;
-- Subsidiary to Make_Finalize_Address_Body, Make_Deep_Array_Body and
-- Make_Deep_Record_Body. Generate the following statements:
--
-- declare
-- type Acc_Typ is access all Typ;
-- for Acc_Typ'Storage_Size use 0;
-- begin
-- [Deep_]Finalize (Acc_Typ (V).all);
-- end;
--------------------------------
-- Allows_Finalization_Master --
--------------------------------
function Allows_Finalization_Master (Typ : Entity_Id) return Boolean is
function In_Deallocation_Instance (E : Entity_Id) return Boolean;
-- Determine whether entity E is inside a wrapper package created for
-- an instance of Ada.Unchecked_Deallocation.
------------------------------
-- In_Deallocation_Instance --
------------------------------
function In_Deallocation_Instance (E : Entity_Id) return Boolean is
Pkg : constant Entity_Id := Scope (E);
Par : Node_Id := Empty;
begin
if Ekind (Pkg) = E_Package
and then Present (Related_Instance (Pkg))
and then Ekind (Related_Instance (Pkg)) = E_Procedure
then
Par := Generic_Parent (Parent (Related_Instance (Pkg)));
return
Present (Par)
and then Chars (Par) = Name_Unchecked_Deallocation
and then Chars (Scope (Par)) = Name_Ada
and then Scope (Scope (Par)) = Standard_Standard;
end if;
return False;
end In_Deallocation_Instance;
-- Local variables
Desig_Typ : constant Entity_Id := Designated_Type (Typ);
Ptr_Typ : constant Entity_Id :=
Root_Type_Of_Full_View (Base_Type (Typ));
-- Start of processing for Allows_Finalization_Master
begin
-- Certain run-time configurations and targets do not provide support
-- for controlled types and therefore do not need masters.
if Restriction_Active (No_Finalization) then
return False;
-- Do not consider C and C++ types since it is assumed that the non-Ada
-- side will handle their cleanup.
elsif Convention (Desig_Typ) = Convention_C
or else Convention (Desig_Typ) = Convention_CPP
then
return False;
-- Do not consider an access type that returns on the secondary stack
elsif Present (Associated_Storage_Pool (Ptr_Typ))
and then Is_RTE (Associated_Storage_Pool (Ptr_Typ), RE_SS_Pool)
then
return False;
-- Do not consider an access type that can never allocate an object
elsif No_Pool_Assigned (Ptr_Typ) then
return False;
-- Do not consider an access type coming from an Unchecked_Deallocation
-- instance. Even though the designated type may be controlled, the
-- access type will never participate in any allocations.
elsif In_Deallocation_Instance (Ptr_Typ) then
return False;
-- Do not consider a non-library access type when No_Nested_Finalization
-- is in effect since finalization masters are controlled objects and if
-- created will violate the restriction.
elsif Restriction_Active (No_Nested_Finalization)
and then not Is_Library_Level_Entity (Ptr_Typ)
then
return False;
-- Do not consider an access type subject to pragma No_Heap_Finalization
-- because objects allocated through such a type are not to be finalized
-- when the access type goes out of scope.
elsif No_Heap_Finalization (Ptr_Typ) then
return False;
-- Do not create finalization masters in GNATprove mode because this
-- causes unwanted extra expansion. A compilation in this mode must
-- keep the tree as close as possible to the original sources.
elsif GNATprove_Mode then
return False;
-- Otherwise the access type may use a finalization master
else
return True;
end if;
end Allows_Finalization_Master;
----------------------------
-- Build_Anonymous_Master --
----------------------------
procedure Build_Anonymous_Master (Ptr_Typ : Entity_Id) is
function Create_Anonymous_Master
(Desig_Typ : Entity_Id;
Unit_Id : Entity_Id;
Unit_Decl : Node_Id) return Entity_Id;
-- Create a new anonymous master for access type Ptr_Typ with designated
-- type Desig_Typ. The declaration of the master and its initialization
-- are inserted in the declarative part of unit Unit_Decl. Unit_Id is
-- the entity of Unit_Decl.
function Current_Anonymous_Master
(Desig_Typ : Entity_Id;
Unit_Id : Entity_Id) return Entity_Id;
-- Find an anonymous master declared within unit Unit_Id which services
-- designated type Desig_Typ. If there is no such master, return Empty.
-----------------------------
-- Create_Anonymous_Master --
-----------------------------
function Create_Anonymous_Master
(Desig_Typ : Entity_Id;
Unit_Id : Entity_Id;
Unit_Decl : Node_Id) return Entity_Id
is
Loc : constant Source_Ptr := Sloc (Unit_Id);
All_FMs : Elist_Id;
Decls : List_Id;
FM_Decl : Node_Id;
FM_Id : Entity_Id;
FM_Init : Node_Id;
Unit_Spec : Node_Id;
begin
-- Generate:
-- <FM_Id> : Finalization_Master;
FM_Id := Make_Temporary (Loc, 'A');
FM_Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier => FM_Id,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Finalization_Master), Loc));
-- Generate:
-- Set_Base_Pool
-- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
FM_Init :=
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (FM_Id, Loc),
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
Attribute_Name => Name_Unrestricted_Access)));
-- Find the declarative list of the unit
if Nkind (Unit_Decl) = N_Package_Declaration then
Unit_Spec := Specification (Unit_Decl);
Decls := Visible_Declarations (Unit_Spec);
if No (Decls) then
Decls := New_List;
Set_Visible_Declarations (Unit_Spec, Decls);
end if;
-- Package body or subprogram case
-- ??? A subprogram spec or body that acts as a compilation unit may
-- contain a formal parameter of an anonymous access-to-controlled
-- type initialized by an allocator.
-- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl);
-- There is no suitable place to create the master as the subprogram
-- is not in a declarative list.
else
Decls := Declarations (Unit_Decl);
if No (Decls) then
Decls := New_List;
Set_Declarations (Unit_Decl, Decls);
end if;
end if;
Prepend_To (Decls, FM_Init);
Prepend_To (Decls, FM_Decl);
-- Use the scope of the unit when analyzing the declaration of the
-- master and its initialization actions.
Push_Scope (Unit_Id);
Analyze (FM_Decl);
Analyze (FM_Init);
Pop_Scope;
-- Mark the master as servicing this specific designated type
Set_Anonymous_Designated_Type (FM_Id, Desig_Typ);
-- Include the anonymous master in the list of existing masters which
-- appear in this unit. This effectively creates a mapping between a
-- master and a designated type which in turn allows for the reuse of
-- masters on a per-unit basis.
All_FMs := Anonymous_Masters (Unit_Id);
if No (All_FMs) then
All_FMs := New_Elmt_List;
Set_Anonymous_Masters (Unit_Id, All_FMs);
end if;
Prepend_Elmt (FM_Id, All_FMs);
return FM_Id;
end Create_Anonymous_Master;
------------------------------
-- Current_Anonymous_Master --
------------------------------
function Current_Anonymous_Master
(Desig_Typ : Entity_Id;
Unit_Id : Entity_Id) return Entity_Id
is
All_FMs : constant Elist_Id := Anonymous_Masters (Unit_Id);
FM_Elmt : Elmt_Id;
FM_Id : Entity_Id;
begin
-- Inspect the list of anonymous masters declared within the unit
-- looking for an existing master which services the same designated
-- type.
if Present (All_FMs) then
FM_Elmt := First_Elmt (All_FMs);
while Present (FM_Elmt) loop
FM_Id := Node (FM_Elmt);
-- The currect master services the same designated type. As a
-- result the master can be reused and associated with another
-- anonymous access-to-controlled type.
if Anonymous_Designated_Type (FM_Id) = Desig_Typ then
return FM_Id;
end if;
Next_Elmt (FM_Elmt);
end loop;
end if;
return Empty;
end Current_Anonymous_Master;
-- Local variables
Desig_Typ : Entity_Id;
FM_Id : Entity_Id;
Priv_View : Entity_Id;
Scop : Entity_Id;
Unit_Decl : Node_Id;
Unit_Id : Entity_Id;
-- Start of processing for Build_Anonymous_Master
begin
-- Nothing to do if the circumstances do not allow for a finalization
-- master.
if not Allows_Finalization_Master (Ptr_Typ) then
return;
end if;
Unit_Decl := Unit (Cunit (Current_Sem_Unit));
Unit_Id := Unique_Defining_Entity (Unit_Decl);
-- The compilation unit is a package instantiation. In this case the
-- anonymous master is associated with the package spec as both the
-- spec and body appear at the same level.
if Nkind (Unit_Decl) = N_Package_Body
and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation
then
Unit_Id := Corresponding_Spec (Unit_Decl);
Unit_Decl := Unit_Declaration_Node (Unit_Id);
end if;
-- Use the initial declaration of the designated type when it denotes
-- the full view of an incomplete or private type. This ensures that
-- types with one and two views are treated the same.
Desig_Typ := Directly_Designated_Type (Ptr_Typ);
Priv_View := Incomplete_Or_Partial_View (Desig_Typ);
if Present (Priv_View) then
Desig_Typ := Priv_View;
end if;
-- For a designated type not declared at library level, we cannot create
-- a finalization collection attached to an outer unit since this would
-- generate dangling references to the dynamic scope through access-to-
-- procedure values designating the local Finalize_Address primitive.
Scop := Enclosing_Dynamic_Scope (Desig_Typ);
if Scop /= Standard_Standard
and then Scope_Depth (Scop) > Scope_Depth (Unit_Id)
then
return;
end if;
-- Determine whether the current semantic unit already has an anonymous
-- master which services the designated type.
FM_Id := Current_Anonymous_Master (Desig_Typ, Unit_Id);
-- If this is not the case, create a new master
if No (FM_Id) then
FM_Id := Create_Anonymous_Master (Desig_Typ, Unit_Id, Unit_Decl);
end if;
Set_Finalization_Master (Ptr_Typ, FM_Id);
end Build_Anonymous_Master;
----------------------------
-- Build_Array_Deep_Procs --
----------------------------
procedure Build_Array_Deep_Procs (Typ : Entity_Id) is
begin
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Initialize_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Initialize_Case, Typ)));
if not Is_Inherently_Limited_Type (Typ) then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Adjust_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Adjust_Case, Typ)));
end if;
-- Do not generate Deep_Finalize and Finalize_Address if finalization is
-- suppressed since these routine will not be used.
if not Restriction_Active (No_Finalization) then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Finalize_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Finalize_Case, Typ)));
-- Create TSS primitive Finalize_Address (unless CodePeer_Mode)
if not CodePeer_Mode then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Address_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Address_Case, Typ)));
end if;
end if;
end Build_Array_Deep_Procs;
------------------------------
-- Build_Cleanup_Statements --
------------------------------
function Build_Cleanup_Statements
(N : Node_Id;
Additional_Cleanup : List_Id) return List_Id
is
Is_Asynchronous_Call : constant Boolean :=
Nkind (N) = N_Block_Statement and then Is_Asynchronous_Call_Block (N);
Is_Master : constant Boolean :=
Nkind (N) /= N_Entry_Body and then Is_Task_Master (N);
Is_Protected_Subp_Body : constant Boolean :=
Nkind (N) = N_Subprogram_Body
and then Is_Protected_Subprogram_Body (N);
Is_Task_Allocation : constant Boolean :=
Nkind (N) = N_Block_Statement and then Is_Task_Allocation_Block (N);
Is_Task_Body : constant Boolean :=
Nkind (Original_Node (N)) = N_Task_Body;
Loc : constant Source_Ptr := Sloc (N);
Stmts : constant List_Id := New_List;
begin
if Is_Task_Body then
if Restricted_Profile then
Append_To (Stmts,
Build_Runtime_Call (Loc, RE_Complete_Restricted_Task));
else
Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Task));
end if;
elsif Is_Master then
if Restriction_Active (No_Task_Hierarchy) = False then
Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Master));
end if;
-- Add statements to unlock the protected object parameter and to
-- undefer abort. If the context is a protected procedure and the object
-- has entries, call the entry service routine.
-- NOTE: The generated code references _object, a parameter to the
-- procedure.
elsif Is_Protected_Subp_Body then
declare
Spec : constant Node_Id := Parent (Corresponding_Spec (N));
Conc_Typ : Entity_Id := Empty;
Param : Node_Id;
Param_Typ : Entity_Id;
begin
-- Find the _object parameter representing the protected object
Param := First (Parameter_Specifications (Spec));
loop
Param_Typ := Etype (Parameter_Type (Param));
if Ekind (Param_Typ) = E_Record_Type then
Conc_Typ := Corresponding_Concurrent_Type (Param_Typ);
end if;
exit when No (Param) or else Present (Conc_Typ);
Next (Param);
end loop;
pragma Assert (Present (Param));
pragma Assert (Present (Conc_Typ));
Build_Protected_Subprogram_Call_Cleanup
(Specification (N), Conc_Typ, Loc, Stmts);
end;
-- Add a call to Expunge_Unactivated_Tasks for dynamically allocated
-- tasks. Other unactivated tasks are completed by Complete_Task or
-- Complete_Master.
-- NOTE: The generated code references _chain, a local object
elsif Is_Task_Allocation then
-- Generate:
-- Expunge_Unactivated_Tasks (_chain);
-- where _chain is the list of tasks created by the allocator but not
-- yet activated. This list will be empty unless the block completes
-- abnormally.
Append_To (Stmts,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Expunge_Unactivated_Tasks), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Activation_Chain_Entity (N), Loc))));
-- Attempt to cancel an asynchronous entry call whenever the block which
-- contains the abortable part is exited.
-- NOTE: The generated code references Cnn, a local object
elsif Is_Asynchronous_Call then
declare
Cancel_Param : constant Entity_Id :=
Entry_Cancel_Parameter (Entity (Identifier (N)));
begin
-- If it is of type Communication_Block, this must be a protected
-- entry call. Generate:
-- if Enqueued (Cancel_Param) then
-- Cancel_Protected_Entry_Call (Cancel_Param);
-- end if;
if Is_RTE (Etype (Cancel_Param), RE_Communication_Block) then
Append_To (Stmts,
Make_If_Statement (Loc,
Condition =>
Make_Function_Call (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Enqueued), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Cancel_Param, Loc))),
Then_Statements => New_List (
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Cancel_Protected_Entry_Call), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Cancel_Param, Loc))))));
-- Asynchronous delay, generate:
-- Cancel_Async_Delay (Cancel_Param);
elsif Is_RTE (Etype (Cancel_Param), RE_Delay_Block) then
Append_To (Stmts,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Cancel_Async_Delay), Loc),
Parameter_Associations => New_List (
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Cancel_Param, Loc),
Attribute_Name => Name_Unchecked_Access))));
-- Task entry call, generate:
-- Cancel_Task_Entry_Call (Cancel_Param);
else
Append_To (Stmts,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Cancel_Task_Entry_Call), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Cancel_Param, Loc))));
end if;
end;
end if;
Append_List_To (Stmts, Additional_Cleanup);
return Stmts;
end Build_Cleanup_Statements;
-----------------------------
-- Build_Controlling_Procs --
-----------------------------
procedure Build_Controlling_Procs (Typ : Entity_Id) is
begin
if Is_Array_Type (Typ) then
Build_Array_Deep_Procs (Typ);
else pragma Assert (Is_Record_Type (Typ));
Build_Record_Deep_Procs (Typ);
end if;
end Build_Controlling_Procs;
-----------------------------
-- Build_Exception_Handler --
-----------------------------
function Build_Exception_Handler
(Data : Finalization_Exception_Data;
For_Library : Boolean := False) return Node_Id
is
Actuals : List_Id;
Proc_To_Call : Entity_Id;
Except : Node_Id;
Stmts : List_Id;
begin
pragma Assert (Present (Data.Raised_Id));
if Exception_Extra_Info
or else (For_Library and not Restricted_Profile)
then
if Exception_Extra_Info then
-- Generate:
-- Get_Current_Excep.all
Except :=
Make_Function_Call (Data.Loc,
Name =>
Make_Explicit_Dereference (Data.Loc,
Prefix =>
New_Occurrence_Of
(RTE (RE_Get_Current_Excep), Data.Loc)));
else
-- Generate:
-- null
Except := Make_Null (Data.Loc);
end if;
if For_Library and then not Restricted_Profile then
Proc_To_Call := RTE (RE_Save_Library_Occurrence);
Actuals := New_List (Except);
else
Proc_To_Call := RTE (RE_Save_Occurrence);
-- The dereference occurs only when Exception_Extra_Info is true,
-- and therefore Except is not null.
Actuals :=
New_List (
New_Occurrence_Of (Data.E_Id, Data.Loc),
Make_Explicit_Dereference (Data.Loc, Except));
end if;
-- Generate:
-- when others =>
-- if not Raised_Id then
-- Raised_Id := True;
-- Save_Occurrence (E_Id, Get_Current_Excep.all.all);
-- or
-- Save_Library_Occurrence (Get_Current_Excep.all);
-- end if;
Stmts :=
New_List (
Make_If_Statement (Data.Loc,
Condition =>
Make_Op_Not (Data.Loc,
Right_Opnd => New_Occurrence_Of (Data.Raised_Id, Data.Loc)),
Then_Statements => New_List (
Make_Assignment_Statement (Data.Loc,
Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc),
Expression => New_Occurrence_Of (Standard_True, Data.Loc)),
Make_Procedure_Call_Statement (Data.Loc,
Name =>
New_Occurrence_Of (Proc_To_Call, Data.Loc),
Parameter_Associations => Actuals))));
else
-- Generate:
-- Raised_Id := True;
Stmts := New_List (
Make_Assignment_Statement (Data.Loc,
Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc),
Expression => New_Occurrence_Of (Standard_True, Data.Loc)));
end if;
-- Generate:
-- when others =>
return
Make_Exception_Handler (Data.Loc,
Exception_Choices => New_List (Make_Others_Choice (Data.Loc)),
Statements => Stmts);
end Build_Exception_Handler;
-------------------------------
-- Build_Finalization_Master --
-------------------------------
procedure Build_Finalization_Master
(Typ : Entity_Id;
For_Lib_Level : Boolean := False;
For_Private : Boolean := False;
Context_Scope : Entity_Id := Empty;
Insertion_Node : Node_Id := Empty)
is
procedure Add_Pending_Access_Type
(Typ : Entity_Id;
Ptr_Typ : Entity_Id);
-- Add access type Ptr_Typ to the pending access type list for type Typ
-----------------------------
-- Add_Pending_Access_Type --
-----------------------------
procedure Add_Pending_Access_Type
(Typ : Entity_Id;
Ptr_Typ : Entity_Id)
is
List : Elist_Id;
begin
if Present (Pending_Access_Types (Typ)) then
List := Pending_Access_Types (Typ);
else
List := New_Elmt_List;
Set_Pending_Access_Types (Typ, List);
end if;
Prepend_Elmt (Ptr_Typ, List);
end Add_Pending_Access_Type;
-- Local variables
Desig_Typ : constant Entity_Id := Designated_Type (Typ);
Ptr_Typ : constant Entity_Id := Root_Type_Of_Full_View (Base_Type (Typ));
-- A finalization master created for a named access type is associated
-- with the full view (if applicable) as a consequence of freezing. The
-- full view criteria does not apply to anonymous access types because
-- those cannot have a private and a full view.
-- Start of processing for Build_Finalization_Master
begin
-- Nothing to do if the circumstances do not allow for a finalization
-- master.
if not Allows_Finalization_Master (Typ) then
return;
-- Various machinery such as freezing may have already created a
-- finalization master.
elsif Present (Finalization_Master (Ptr_Typ)) then
return;
end if;
declare
Actions : constant List_Id := New_List;
Loc : constant Source_Ptr := Sloc (Ptr_Typ);
Fin_Mas_Id : Entity_Id;
Pool_Id : Entity_Id;
begin
-- Source access types use fixed master names since the master is
-- inserted in the same source unit only once. The only exception to
-- this are instances using the same access type as generic actual.
if Comes_From_Source (Ptr_Typ) and then not Inside_A_Generic then
Fin_Mas_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name (Chars (Ptr_Typ), "FM"));
-- Internally generated access types use temporaries as their names
-- due to possible collision with identical names coming from other
-- packages.
else
Fin_Mas_Id := Make_Temporary (Loc, 'F');
end if;
Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
-- Generate:
-- <Ptr_Typ>FM : aliased Finalization_Master;
Append_To (Actions,
Make_Object_Declaration (Loc,
Defining_Identifier => Fin_Mas_Id,
Aliased_Present => True,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)));
if Debug_Generated_Code then
Set_Debug_Info_Needed (Fin_Mas_Id);
end if;
-- Set the associated pool and primitive Finalize_Address of the new
-- finalization master.
-- The access type has a user-defined storage pool, use it
if Present (Associated_Storage_Pool (Ptr_Typ)) then
Pool_Id := Associated_Storage_Pool (Ptr_Typ);
-- Otherwise the default choice is the global storage pool
else
Pool_Id := RTE (RE_Global_Pool_Object);
Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
end if;
-- Generate:
-- Set_Base_Pool (<Ptr_Typ>FM, Pool_Id'Unchecked_Access);
Append_To (Actions,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Fin_Mas_Id, Loc),
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Pool_Id, Loc),
Attribute_Name => Name_Unrestricted_Access))));
-- Finalize_Address is not generated in CodePeer mode because the
-- body contains address arithmetic. Skip this step.
if CodePeer_Mode then
null;
-- Associate the Finalize_Address primitive of the designated type
-- with the finalization master of the access type. The designated
-- type must be frozen, as Finalize_Address is generated when the
-- freeze node is expanded.
elsif Is_Frozen (Desig_Typ)
and then Present (Finalize_Address (Desig_Typ))
-- The finalization master of an anonymous access type may need
-- to be inserted in a specific place in the tree. For instance:
-- type Comp_Typ;
-- <finalization master of "access Comp_Typ">
-- type Rec_Typ is record
-- Comp : access Comp_Typ;
-- end record;
-- <freeze node for Comp_Typ>
-- <freeze node for Rec_Typ>
-- Due to this oddity, the anonymous access type is stored for
-- later processing (see below).
and then Ekind (Ptr_Typ) /= E_Anonymous_Access_Type
then
-- Generate:
-- Set_Finalize_Address
-- (<Ptr_Typ>FM, <Desig_Typ>FD'Unrestricted_Access);
Append_To (Actions,
Make_Set_Finalize_Address_Call
(Loc => Loc,
Ptr_Typ => Ptr_Typ));
-- Otherwise the designated type is either anonymous access or a
-- Taft-amendment type and has not been frozen. Store the access
-- type for later processing (see Freeze_Type).
else
Add_Pending_Access_Type (Desig_Typ, Ptr_Typ);
end if;
-- A finalization master created for an access designating a type
-- with private components is inserted before a context-dependent
-- node.
if For_Private then
-- At this point both the scope of the context and the insertion
-- mode must be known.
pragma Assert (Present (Context_Scope));
pragma Assert (Present (Insertion_Node));
Push_Scope (Context_Scope);
-- Treat use clauses as declarations and insert directly in front
-- of them.
if Nkind (Insertion_Node) in
N_Use_Package_Clause | N_Use_Type_Clause
then
Insert_List_Before_And_Analyze (Insertion_Node, Actions);
else
Insert_Actions (Insertion_Node, Actions);
end if;
Pop_Scope;
-- The finalization master belongs to an access result type related
-- to a build-in-place function call used to initialize a library
-- level object. The master must be inserted in front of the access
-- result type declaration denoted by Insertion_Node.
elsif For_Lib_Level then
pragma Assert (Present (Insertion_Node));
Insert_Actions (Insertion_Node, Actions);
-- Otherwise the finalization master and its initialization become a
-- part of the freeze node.
else
Append_Freeze_Actions (Ptr_Typ, Actions);
end if;
Analyze_List (Actions);
-- When the type the finalization master is being generated for was
-- created to store a 'Old object, then mark it as such so its
-- finalization can be delayed until after postconditions have been
-- checked.
if Stores_Attribute_Old_Prefix (Ptr_Typ) then
Set_Stores_Attribute_Old_Prefix (Fin_Mas_Id);
end if;
end;
end Build_Finalization_Master;
---------------------
-- Build_Finalizer --
---------------------
procedure Build_Finalizer
(N : Node_Id;
Clean_Stmts : List_Id;
Mark_Id : Entity_Id;
Top_Decls : List_Id;
Defer_Abort : Boolean;
Fin_Id : out Entity_Id)
is
Acts_As_Clean : constant Boolean :=
Present (Mark_Id)
or else
(Present (Clean_Stmts)
and then Is_Non_Empty_List (Clean_Stmts));
For_Package_Body : constant Boolean := Nkind (N) = N_Package_Body;
For_Package_Spec : constant Boolean := Nkind (N) = N_Package_Declaration;
For_Package : constant Boolean :=
For_Package_Body or else For_Package_Spec;
Loc : constant Source_Ptr := Sloc (N);
-- NOTE: Local variable declarations are conservative and do not create
-- structures right from the start. Entities and lists are created once
-- it has been established that N has at least one controlled object.
Components_Built : Boolean := False;
-- A flag used to avoid double initialization of entities and lists. If
-- the flag is set then the following variables have been initialized:
-- Counter_Id
-- Finalizer_Decls
-- Finalizer_Stmts
-- Jump_Alts
Counter_Id : Entity_Id := Empty;
Counter_Val : Nat := 0;
-- Name and value of the state counter
Decls : List_Id := No_List;
-- Declarative region of N (if available). If N is a package declaration
-- Decls denotes the visible declarations.
Finalizer_Data : Finalization_Exception_Data;
-- Data for the exception
Finalizer_Decls : List_Id := No_List;
-- Local variable declarations. This list holds the label declarations
-- of all jump block alternatives as well as the declaration of the
-- local exception occurrence and the raised flag:
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- L<counter value> : label;
Finalizer_Insert_Nod : Node_Id := Empty;
-- Insertion point for the finalizer body. Depending on the context
-- (Nkind of N) and the individual grouping of controlled objects, this
-- node may denote a package declaration or body, package instantiation,
-- block statement or a counter update statement.
Finalizer_Stmts : List_Id := No_List;
-- The statement list of the finalizer body. It contains the following:
--
-- Abort_Defer; -- Added if abort is allowed
-- <call to Prev_At_End> -- Added if exists
-- <cleanup statements> -- Added if Acts_As_Clean
-- <jump block> -- Added if Has_Ctrl_Objs
-- <finalization statements> -- Added if Has_Ctrl_Objs
-- <stack release> -- Added if Mark_Id exists
-- Abort_Undefer; -- Added if abort is allowed
Has_Ctrl_Objs : Boolean := False;
-- A general flag which denotes whether N has at least one controlled
-- object.
Has_Tagged_Types : Boolean := False;
-- A general flag which indicates whether N has at least one library-
-- level tagged type declaration.
HSS : Node_Id := Empty;
-- The sequence of statements of N (if available)
Jump_Alts : List_Id := No_List;
-- Jump block alternatives. Depending on the value of the state counter,
-- the control flow jumps to a sequence of finalization statements. This
-- list contains the following:
--
-- when <counter value> =>
-- goto L<counter value>;
Jump_Block_Insert_Nod : Node_Id := Empty;
-- Specific point in the finalizer statements where the jump block is
-- inserted.
Last_Top_Level_Ctrl_Construct : Node_Id := Empty;
-- The last controlled construct encountered when processing the top
-- level lists of N. This can be a nested package, an instantiation or
-- an object declaration.
Prev_At_End : Entity_Id := Empty;
-- The previous at end procedure of the handled statements block of N
Priv_Decls : List_Id := No_List;
-- The private declarations of N if N is a package declaration
Spec_Id : Entity_Id := Empty;
Spec_Decls : List_Id := Top_Decls;
Stmts : List_Id := No_List;
Tagged_Type_Stmts : List_Id := No_List;
-- Contains calls to Ada.Tags.Unregister_Tag for all library-level
-- tagged types found in N.
-----------------------
-- Local subprograms --
-----------------------
procedure Build_Components;
-- Create all entites and initialize all lists used in the creation of
-- the finalizer.
procedure Create_Finalizer;
-- Create the spec and body of the finalizer and insert them in the
-- proper place in the tree depending on the context.
function New_Finalizer_Name
(Spec_Id : Node_Id; For_Spec : Boolean) return Name_Id;
-- Create a fully qualified name of a package spec or body finalizer.
-- The generated name is of the form: xx__yy__finalize_[spec|body].
procedure Process_Declarations
(Decls : List_Id;
Preprocess : Boolean := False;
Top_Level : Boolean := False);
-- Inspect a list of declarations or statements which may contain
-- objects that need finalization. When flag Preprocess is set, the
-- routine will simply count the total number of controlled objects in
-- Decls and set Counter_Val accordingly. Top_Level is only relevant
-- when Preprocess is set and if True, the processing is performed for
-- objects in nested package declarations or instances.
procedure Process_Object_Declaration
(Decl : Node_Id;
Has_No_Init : Boolean := False;
Is_Protected : Boolean := False);
-- Generate all the machinery associated with the finalization of a
-- single object. Flag Has_No_Init is used to denote certain contexts
-- where Decl does not have initialization call(s). Flag Is_Protected
-- is set when Decl denotes a simple protected object.
procedure Process_Tagged_Type_Declaration (Decl : Node_Id);
-- Generate all the code necessary to unregister the external tag of a
-- tagged type.
----------------------
-- Build_Components --
----------------------
procedure Build_Components is
Counter_Decl : Node_Id;
Counter_Typ : Entity_Id;
Counter_Typ_Decl : Node_Id;
begin
pragma Assert (Present (Decls));
-- This routine might be invoked several times when dealing with
-- constructs that have two lists (either two declarative regions
-- or declarations and statements). Avoid double initialization.
if Components_Built then
return;
end if;
Components_Built := True;
if Has_Ctrl_Objs then
-- Create entities for the counter, its type, the local exception
-- and the raised flag.
Counter_Id := Make_Temporary (Loc, 'C');
Counter_Typ := Make_Temporary (Loc, 'T');
Finalizer_Decls := New_List;
Build_Object_Declarations
(Finalizer_Data, Finalizer_Decls, Loc, For_Package);
-- Since the total number of controlled objects is always known,
-- build a subtype of Natural with precise bounds. This allows
-- the backend to optimize the case statement. Generate:
--
-- subtype Tnn is Natural range 0 .. Counter_Val;
Counter_Typ_Decl :=
Make_Subtype_Declaration (Loc,
Defining_Identifier => Counter_Typ,
Subtype_Indication =>
Make_Subtype_Indication (Loc,
Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
Constraint =>
Make_Range_Constraint (Loc,
Range_Expression =>
Make_Range (Loc,
Low_Bound =>
Make_Integer_Literal (Loc, Uint_0),
High_Bound =>
Make_Integer_Literal (Loc, Counter_Val)))));
-- Generate the declaration of the counter itself:
--
-- Counter : Integer := 0;
Counter_Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier => Counter_Id,
Object_Definition => New_Occurrence_Of (Counter_Typ, Loc),
Expression => Make_Integer_Literal (Loc, 0));
-- Set the type of the counter explicitly to prevent errors when
-- examining object declarations later on.
Set_Etype (Counter_Id, Counter_Typ);
if Debug_Generated_Code then
Set_Debug_Info_Needed (Counter_Id);
end if;
-- The counter and its type are inserted before the source
-- declarations of N.
Prepend_To (Decls, Counter_Decl);
Prepend_To (Decls, Counter_Typ_Decl);
-- The counter and its associated type must be manually analyzed
-- since N has already been analyzed.
Analyze (Counter_Typ_Decl);
Analyze (Counter_Decl);
Jump_Alts := New_List;
end if;
-- If the context requires additional cleanup, the finalization
-- machinery is added after the cleanup code.
if Acts_As_Clean then
Finalizer_Stmts := Clean_Stmts;
Jump_Block_Insert_Nod := Last (Finalizer_Stmts);
else
Finalizer_Stmts := New_List;
end if;
if Has_Tagged_Types then
Tagged_Type_Stmts := New_List;
end if;
end Build_Components;
----------------------
-- Create_Finalizer --
----------------------
procedure Create_Finalizer is
Body_Id : Entity_Id;
Fin_Body : Node_Id;
Fin_Spec : Node_Id;
Jump_Block : Node_Id;
Label : Node_Id;
Label_Id : Entity_Id;
begin
-- Step 1: Creation of the finalizer name
-- Packages must use a distinct name for their finalizers since the
-- binder will have to generate calls to them by name. The name is
-- of the following form:
-- xx__yy__finalize_[spec|body]
if For_Package then
Fin_Id := Make_Defining_Identifier
(Loc, New_Finalizer_Name (Spec_Id, For_Package_Spec));
Set_Has_Qualified_Name (Fin_Id);
Set_Has_Fully_Qualified_Name (Fin_Id);
-- The default name is _finalizer
else
-- Generation of a finalization procedure exclusively for 'Old
-- interally generated constants requires different name since
-- there will need to be multiple finalization routines in the
-- same scope. See Build_Finalizer for details.
Fin_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name (Name_uFinalizer));
-- The visibility semantics of AT_END handlers force a strange
-- separation of spec and body for stack-related finalizers:
-- declare : Enclosing_Scope
-- procedure _finalizer;
-- begin
-- <controlled objects>
-- procedure _finalizer is
-- ...
-- at end
-- _finalizer;
-- end;
-- Both spec and body are within the same construct and scope, but
-- the body is part of the handled sequence of statements. This
-- placement confuses the elaboration mechanism on targets where
-- AT_END handlers are expanded into "when all others" handlers:
-- exception
-- when all others =>
-- _finalizer; -- appears to require elab checks
-- at end
-- _finalizer;
-- end;
-- Since the compiler guarantees that the body of a _finalizer is
-- always inserted in the same construct where the AT_END handler
-- resides, there is no need for elaboration checks.
Set_Kill_Elaboration_Checks (Fin_Id);
-- Inlining the finalizer produces a substantial speedup at -O2.
-- It is inlined by default at -O3. Either way, it is called
-- exactly twice (once on the normal path, and once for
-- exceptions/abort), so this won't bloat the code too much.
Set_Is_Inlined (Fin_Id);
end if;
if Debug_Generated_Code then
Set_Debug_Info_Needed (Fin_Id);
end if;
-- Step 2: Creation of the finalizer specification
-- Generate:
-- procedure Fin_Id;
Fin_Spec :=
Make_Subprogram_Declaration (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Fin_Id));
if For_Package then
Set_Is_Exported (Fin_Id);
Set_Interface_Name (Fin_Id,
Make_String_Literal (Loc,
Strval => Get_Name_String (Chars (Fin_Id))));
end if;
-- Step 3: Creation of the finalizer body
-- Has_Ctrl_Objs might be set because of a generic package body having
-- controlled objects. In this case, Jump_Alts may be empty and no
-- case nor goto statements are needed.
if Has_Ctrl_Objs
and then not Is_Empty_List (Jump_Alts)
then
-- Add L0, the default destination to the jump block
Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
Set_Entity (Label_Id,
Make_Defining_Identifier (Loc, Chars (Label_Id)));
Label := Make_Label (Loc, Label_Id);
-- Generate:
-- L0 : label;
Prepend_To (Finalizer_Decls,
Make_Implicit_Label_Declaration (Loc,
Defining_Identifier => Entity (Label_Id),
Label_Construct => Label));
-- Generate:
-- when others =>
-- goto L0;
Append_To (Jump_Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => New_List (Make_Others_Choice (Loc)),
Statements => New_List (
Make_Goto_Statement (Loc,
Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
-- Generate:
-- <<L0>>
Append_To (Finalizer_Stmts, Label);
-- Create the jump block which controls the finalization flow
-- depending on the value of the state counter.
Jump_Block :=
Make_Case_Statement (Loc,
Expression => Make_Identifier (Loc, Chars (Counter_Id)),
Alternatives => Jump_Alts);
if Acts_As_Clean and then Present (Jump_Block_Insert_Nod) then
Insert_After (Jump_Block_Insert_Nod, Jump_Block);
else
Prepend_To (Finalizer_Stmts, Jump_Block);
end if;
end if;
-- Add the library-level tagged type unregistration machinery before
-- the jump block circuitry. This ensures that external tags will be
-- removed even if a finalization exception occurs at some point.
if Has_Tagged_Types then
Prepend_List_To (Finalizer_Stmts, Tagged_Type_Stmts);
end if;
-- Add a call to the previous At_End handler if it exists. The call
-- must always precede the jump block.
if Present (Prev_At_End) then
Prepend_To (Finalizer_Stmts,
Make_Procedure_Call_Statement (Loc, Prev_At_End));
-- Clear the At_End handler since we have already generated the
-- proper replacement call for it.
Set_At_End_Proc (HSS, Empty);
end if;
-- Release the secondary stack
if Present (Mark_Id) then
declare
Release : Node_Id := Build_SS_Release_Call (Loc, Mark_Id);
begin
-- If the context is a build-in-place function, the secondary
-- stack must be released, unless the build-in-place function
-- itself is returning on the secondary stack. Generate:
--
-- if BIP_Alloc_Form /= Secondary_Stack then
-- SS_Release (Mark_Id);
-- end if;
--
-- Note that if the function returns on the secondary stack,
-- then the responsibility of reclaiming the space is always
-- left to the caller (recursively if needed).
if Nkind (N) = N_Subprogram_Body then
declare
Spec_Id : constant Entity_Id :=
Unique_Defining_Entity (N);
BIP_SS : constant Boolean :=
Is_Build_In_Place_Function (Spec_Id)
and then Needs_BIP_Alloc_Form (Spec_Id);
begin
if BIP_SS then
Release :=
Make_If_Statement (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd =>
New_Occurrence_Of
(Build_In_Place_Formal
(Spec_Id, BIP_Alloc_Form), Loc),
Right_Opnd =>
Make_Integer_Literal (Loc,
UI_From_Int
(BIP_Allocation_Form'Pos
(Secondary_Stack)))),
Then_Statements => New_List (Release));
end if;
end;
end if;
Append_To (Finalizer_Stmts, Release);
end;
end if;
-- Protect the statements with abort defer/undefer. This is only when
-- aborts are allowed and the cleanup statements require deferral or
-- there are controlled objects to be finalized. Note that the abort
-- defer/undefer pair does not require an extra block because each
-- finalization exception is caught in its corresponding finalization
-- block. As a result, the call to Abort_Defer always takes place.
if Abort_Allowed and then (Defer_Abort or Has_Ctrl_Objs) then
Prepend_To (Finalizer_Stmts,
Build_Runtime_Call (Loc, RE_Abort_Defer));
Append_To (Finalizer_Stmts,
Build_Runtime_Call (Loc, RE_Abort_Undefer));
end if;
-- The local exception does not need to be reraised for library-level
-- finalizers. Note that this action must be carried out after object
-- cleanup, secondary stack release, and abort undeferral. Generate:
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
if Has_Ctrl_Objs and Exceptions_OK and not For_Package then
Append_To (Finalizer_Stmts,
Build_Raise_Statement (Finalizer_Data));
end if;
-- Generate:
-- procedure Fin_Id is
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence; -- All added if flag
-- Raised : Boolean := False; -- Has_Ctrl_Objs is set
-- L0 : label;
-- ...
-- Lnn : label;
-- begin
-- Abort_Defer; -- Added if abort is allowed
-- <call to Prev_At_End> -- Added if exists
-- <cleanup statements> -- Added if Acts_As_Clean
-- <jump block> -- Added if Has_Ctrl_Objs
-- <finalization statements> -- Added if Has_Ctrl_Objs
-- <stack release> -- Added if Mark_Id exists
-- Abort_Undefer; -- Added if abort is allowed
-- <exception propagation> -- Added if Has_Ctrl_Objs
-- end Fin_Id;
-- Create the body of the finalizer
Body_Id := Make_Defining_Identifier (Loc, Chars (Fin_Id));
if Debug_Generated_Code then
Set_Debug_Info_Needed (Body_Id);
end if;
if For_Package then
Set_Has_Qualified_Name (Body_Id);
Set_Has_Fully_Qualified_Name (Body_Id);
end if;
Fin_Body :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Body_Id),
Declarations => Finalizer_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Finalizer_Stmts));
-- Step 4: Spec and body insertion, analysis
if For_Package then
-- If the package spec has private declarations, the finalizer
-- body must be added to the end of the list in order to have
-- visibility of all private controlled objects.
if For_Package_Spec then
if Present (Priv_Decls) then
Append_To (Priv_Decls, Fin_Spec);
Append_To (Priv_Decls, Fin_Body);
else
Append_To (Decls, Fin_Spec);
Append_To (Decls, Fin_Body);
end if;
-- For package bodies, both the finalizer spec and body are
-- inserted at the end of the package declarations.
else
Append_To (Decls, Fin_Spec);
Append_To (Decls, Fin_Body);
end if;
Analyze (Fin_Spec);
Analyze (Fin_Body);
-- Non-package case
else
-- Create the spec for the finalizer. The At_End handler must be
-- able to call the body which resides in a nested structure.
-- Generate:
-- declare
-- procedure Fin_Id; -- Spec
-- begin
-- <objects and possibly statements>
-- procedure Fin_Id is ... -- Body
-- <statements>
-- at end
-- Fin_Id; -- At_End handler
-- end;
pragma Assert (Present (Spec_Decls));
-- It maybe possible that we are finalizing 'Old objects which
-- exist in the spec declarations. When this is the case the
-- Finalizer_Insert_Node will come before the end of the
-- Spec_Decls. So, to mitigate this, we insert the finalizer spec
-- earlier at the Finalizer_Insert_Nod instead of appending to the
-- end of Spec_Decls to prevent its body appearing before its
-- corresponding spec.
if Present (Finalizer_Insert_Nod)
and then List_Containing (Finalizer_Insert_Nod) = Spec_Decls
then
Insert_After_And_Analyze (Finalizer_Insert_Nod, Fin_Spec);
Finalizer_Insert_Nod := Fin_Spec;
-- Otherwise, Finalizer_Insert_Nod is not in Spec_Decls
else
Append_To (Spec_Decls, Fin_Spec);
Analyze (Fin_Spec);
end if;
-- When the finalizer acts solely as a cleanup routine, the body
-- is inserted right after the spec.
if Acts_As_Clean and not Has_Ctrl_Objs then
Insert_After (Fin_Spec, Fin_Body);
-- In all other cases the body is inserted after either:
--
-- 1) The counter update statement of the last controlled object
-- 2) The last top level nested controlled package
-- 3) The last top level controlled instantiation
else
-- Manually freeze the spec. This is somewhat of a hack because
-- a subprogram is frozen when its body is seen and the freeze
-- node appears right before the body. However, in this case,
-- the spec must be frozen earlier since the At_End handler
-- must be able to call it.
--
-- declare
-- procedure Fin_Id; -- Spec
-- [Fin_Id] -- Freeze node
-- begin
-- ...
-- at end
-- Fin_Id; -- At_End handler
-- end;
Ensure_Freeze_Node (Fin_Id);
Insert_After (Fin_Spec, Freeze_Node (Fin_Id));
Set_Is_Frozen (Fin_Id);
-- In the case where the last construct to contain a controlled
-- object is either a nested package, an instantiation or a
-- freeze node, the body must be inserted directly after the
-- construct, except if the insertion point is already placed
-- after the construct, typically in the statement list.
if Nkind (Last_Top_Level_Ctrl_Construct) in
N_Freeze_Entity | N_Package_Declaration | N_Package_Body
and then not
(List_Containing (Last_Top_Level_Ctrl_Construct) = Spec_Decls
and then Present (Stmts)
and then List_Containing (Finalizer_Insert_Nod) = Stmts)
then
Finalizer_Insert_Nod := Last_Top_Level_Ctrl_Construct;
if Nkind (Finalizer_Insert_Nod) = N_Package_Body
and then Nkind (Parent (Finalizer_Insert_Nod)) = N_Subunit
then
Finalizer_Insert_Nod :=
Corresponding_Stub (Parent (Finalizer_Insert_Nod));
end if;
end if;
Insert_After (Finalizer_Insert_Nod, Fin_Body);
end if;
Analyze (Fin_Body, Suppress => All_Checks);
end if;
-- Never consider that the finalizer procedure is enabled Ghost, even
-- when the corresponding unit is Ghost, as this would lead to an
-- an external name with a ___ghost_ prefix that the binder cannot
-- generate, as it has no knowledge of the Ghost status of units.
Set_Is_Checked_Ghost_Entity (Fin_Id, False);
end Create_Finalizer;
------------------------
-- New_Finalizer_Name --
------------------------
function New_Finalizer_Name
(Spec_Id : Node_Id; For_Spec : Boolean) return Name_Id
is
procedure New_Finalizer_Name (Id : Entity_Id);
-- Place "__<name-of-Id>" in the name buffer. If the identifier
-- has a non-standard scope, process the scope first.
------------------------
-- New_Finalizer_Name --
------------------------
procedure New_Finalizer_Name (Id : Entity_Id) is
begin
if Scope (Id) = Standard_Standard then
Get_Name_String (Chars (Id));
else
New_Finalizer_Name (Scope (Id));
Add_Str_To_Name_Buffer ("__");
Get_Name_String_And_Append (Chars (Id));
end if;
end New_Finalizer_Name;
-- Start of processing for New_Finalizer_Name
begin
-- Create the fully qualified name of the enclosing scope
New_Finalizer_Name (Spec_Id);
-- Generate:
-- __finalize_[spec|body]
Add_Str_To_Name_Buffer ("__finalize_");
if For_Spec then
Add_Str_To_Name_Buffer ("spec");
else
Add_Str_To_Name_Buffer ("body");
end if;
return Name_Find;
end New_Finalizer_Name;
--------------------------
-- Process_Declarations --
--------------------------
procedure Process_Declarations
(Decls : List_Id;
Preprocess : Boolean := False;
Top_Level : Boolean := False)
is
Decl : Node_Id;
Expr : Node_Id;
Obj_Id : Entity_Id;
Obj_Typ : Entity_Id;
Pack_Id : Entity_Id;
Spec : Node_Id;
Typ : Entity_Id;
Old_Counter_Val : Nat;
-- This variable is used to determine whether a nested package or
-- instance contains at least one controlled object.
procedure Process_Package_Body (Decl : Node_Id);
-- Process an N_Package_Body node
procedure Processing_Actions
(Has_No_Init : Boolean := False;
Is_Protected : Boolean := False);
-- Depending on the mode of operation of Process_Declarations, either
-- increment the controlled object counter, set the controlled object
-- flag and store the last top level construct or process the current
-- declaration. Flag Has_No_Init is used to propagate scenarios where
-- the current declaration may not have initialization proc(s). Flag
-- Is_Protected should be set when the current declaration denotes a
-- simple protected object.
--------------------------
-- Process_Package_Body --
--------------------------
procedure Process_Package_Body (Decl : Node_Id) is
begin
-- Do not inspect an ignored Ghost package body because all
-- code found within will not appear in the final tree.
if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
null;
elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package then
Old_Counter_Val := Counter_Val;
Process_Declarations (Declarations (Decl), Preprocess);
-- The nested package body is the last construct to contain
-- a controlled object.
if Preprocess
and then Top_Level
and then No (Last_Top_Level_Ctrl_Construct)
and then Counter_Val > Old_Counter_Val
then
Last_Top_Level_Ctrl_Construct := Decl;
end if;
end if;
end Process_Package_Body;
------------------------
-- Processing_Actions --
------------------------
procedure Processing_Actions
(Has_No_Init : Boolean := False;
Is_Protected : Boolean := False)
is
begin
-- Library-level tagged type
if Nkind (Decl) = N_Full_Type_Declaration then
if Preprocess then
Has_Tagged_Types := True;
if Top_Level and then No (Last_Top_Level_Ctrl_Construct) then
Last_Top_Level_Ctrl_Construct := Decl;
end if;
-- Unregister tagged type, unless No_Tagged_Type_Registration
-- is active.
elsif not Restriction_Active (No_Tagged_Type_Registration) then
Process_Tagged_Type_Declaration (Decl);
end if;
-- Controlled object declaration
else
if Preprocess then
Counter_Val := Counter_Val + 1;
Has_Ctrl_Objs := True;
if Top_Level and then No (Last_Top_Level_Ctrl_Construct) then
Last_Top_Level_Ctrl_Construct := Decl;
end if;
else
Process_Object_Declaration (Decl, Has_No_Init, Is_Protected);
end if;
end if;
end Processing_Actions;
-- Start of processing for Process_Declarations
begin
if Is_Empty_List (Decls) then
return;
end if;
-- Process all declarations in reverse order
Decl := Last_Non_Pragma (Decls);
while Present (Decl) loop
-- Library-level tagged types
if Nkind (Decl) = N_Full_Type_Declaration then
Typ := Defining_Identifier (Decl);
-- Ignored Ghost types do not need any cleanup actions because
-- they will not appear in the final tree.
if Is_Ignored_Ghost_Entity (Typ) then
null;
elsif Is_Tagged_Type (Typ)
and then Is_Library_Level_Entity (Typ)
and then Convention (Typ) = Convention_Ada
and then Present (Access_Disp_Table (Typ))
and then not Is_Abstract_Type (Typ)
and then not No_Run_Time_Mode
and then not Restriction_Active (No_Tagged_Type_Registration)
and then RTE_Available (RE_Register_Tag)
then
Processing_Actions;
end if;
-- Regular object declarations
elsif Nkind (Decl) = N_Object_Declaration then
Obj_Id := Defining_Identifier (Decl);
Obj_Typ := Base_Type (Etype (Obj_Id));
Expr := Expression (Decl);
-- Bypass any form of processing for objects which have their
-- finalization disabled. This applies only to objects at the
-- library level.
if For_Package and then Finalize_Storage_Only (Obj_Typ) then
null;
-- Finalization of transient objects is treated separately in
-- order to handle sensitive cases. These include:
-- * Conditional expressions
-- * Expressions with actions
-- * Transient scopes
elsif Is_Finalized_Transient (Obj_Id) then
null;
-- Finalization of specific objects is also treated separately
elsif Is_Ignored_For_Finalization (Obj_Id) then
null;
-- Ignored Ghost objects do not need any cleanup actions
-- because they will not appear in the final tree.
elsif Is_Ignored_Ghost_Entity (Obj_Id) then
null;
-- The object is of the form:
-- Obj : [constant] Typ [:= Expr];
-- Do not process the incomplete view of a deferred constant.
-- Note that an object initialized by means of a BIP function
-- call may appear as a deferred constant after expansion
-- activities. These kinds of objects must be finalized.
elsif not Is_Imported (Obj_Id)
and then Needs_Finalization (Obj_Typ)
and then not (Ekind (Obj_Id) = E_Constant
and then not Has_Completion (Obj_Id)
and then No (BIP_Initialization_Call (Obj_Id)))
then
Processing_Actions;
-- The object is of the form:
-- Obj : Access_Typ := Non_BIP_Function_Call'reference;
-- Obj : Access_Typ :=
-- BIP_Function_Call (BIPalloc => 2, ...)'reference;
elsif Is_Access_Type (Obj_Typ)
and then Needs_Finalization
(Available_View (Designated_Type (Obj_Typ)))
and then Present (Expr)
and then
(Is_Secondary_Stack_BIP_Func_Call (Expr)
or else
(Is_Non_BIP_Func_Call (Expr)
and then not Is_Related_To_Func_Return (Obj_Id)))
then
Processing_Actions (Has_No_Init => True);
-- Processing for "hook" objects generated for transient
-- objects declared inside an Expression_With_Actions.
elsif Is_Access_Type (Obj_Typ)
and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
N_Object_Declaration
then
Processing_Actions (Has_No_Init => True);
-- Process intermediate results of an if expression with one
-- of the alternatives using a controlled function call.
elsif Is_Access_Type (Obj_Typ)
and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
N_Defining_Identifier
and then Present (Expr)
and then Nkind (Expr) = N_Null
then
Processing_Actions (Has_No_Init => True);
-- Simple protected objects which use type System.Tasking.
-- Protected_Objects.Protection to manage their locks should
-- be treated as controlled since they require manual cleanup.
-- The only exception is illustrated in the following example:
-- package Pkg is
-- type Ctrl is new Controlled ...
-- procedure Finalize (Obj : in out Ctrl);
-- Lib_Obj : Ctrl;
-- end Pkg;
-- package body Pkg is
-- protected Prot is
-- procedure Do_Something (Obj : in out Ctrl);
-- end Prot;
-- protected body Prot is
-- procedure Do_Something (Obj : in out Ctrl) is ...
-- end Prot;
-- procedure Finalize (Obj : in out Ctrl) is
-- begin
-- Prot.Do_Something (Obj);
-- end Finalize;
-- end Pkg;
-- Since for the most part entities in package bodies depend on
-- those in package specs, Prot's lock should be cleaned up
-- first. The subsequent cleanup of the spec finalizes Lib_Obj.
-- This act however attempts to invoke Do_Something and fails
-- because the lock has disappeared.
elsif Ekind (Obj_Id) = E_Variable
and then not In_Library_Level_Package_Body (Obj_Id)
and then Has_Simple_Protected_Object (Obj_Typ)
then
Processing_Actions (Is_Protected => True);
end if;
-- Specific cases of object renamings
elsif Nkind (Decl) = N_Object_Renaming_Declaration then
Obj_Id := Defining_Identifier (Decl);
Obj_Typ := Base_Type (Etype (Obj_Id));
-- Bypass any form of processing for objects which have their
-- finalization disabled. This applies only to objects at the
-- library level.
if For_Package and then Finalize_Storage_Only (Obj_Typ) then
null;
-- Ignored Ghost object renamings do not need any cleanup
-- actions because they will not appear in the final tree.
elsif Is_Ignored_Ghost_Entity (Obj_Id) then
null;
-- Return object of extended return statements. This case is
-- recognized and marked by the expansion of extended return
-- statements (see Expand_N_Extended_Return_Statement).
elsif Needs_Finalization (Obj_Typ)
and then Is_Return_Object (Obj_Id)
and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
then
Processing_Actions (Has_No_Init => True);
end if;
-- Inspect the freeze node of an access-to-controlled type and
-- look for a delayed finalization master. This case arises when
-- the freeze actions are inserted at a later time than the
-- expansion of the context. Since Build_Finalizer is never called
-- on a single construct twice, the master will be ultimately
-- left out and never finalized. This is also needed for freeze
-- actions of designated types themselves, since in some cases the
-- finalization master is associated with a designated type's
-- freeze node rather than that of the access type (see handling
-- for freeze actions in Build_Finalization_Master).
elsif Nkind (Decl) = N_Freeze_Entity
and then Present (Actions (Decl))
then
Typ := Entity (Decl);
-- Freeze nodes for ignored Ghost types do not need cleanup
-- actions because they will never appear in the final tree.
if Is_Ignored_Ghost_Entity (Typ) then
null;
elsif (Is_Access_Object_Type (Typ)
and then Needs_Finalization
(Available_View (Designated_Type (Typ))))
or else (Is_Type (Typ) and then Needs_Finalization (Typ))
then
Old_Counter_Val := Counter_Val;
-- Freeze nodes are considered to be identical to packages
-- and blocks in terms of nesting. The difference is that
-- a finalization master created inside the freeze node is
-- at the same nesting level as the node itself.
Process_Declarations (Actions (Decl), Preprocess);
-- The freeze node contains a finalization master
if Preprocess
and then Top_Level
and then No (Last_Top_Level_Ctrl_Construct)
and then Counter_Val > Old_Counter_Val
then
Last_Top_Level_Ctrl_Construct := Decl;
end if;
end if;
-- Nested package declarations, avoid generics
elsif Nkind (Decl) = N_Package_Declaration then
Pack_Id := Defining_Entity (Decl);
Spec := Specification (Decl);
-- Do not inspect an ignored Ghost package because all code
-- found within will not appear in the final tree.
if Is_Ignored_Ghost_Entity (Pack_Id) then
null;
elsif Ekind (Pack_Id) /= E_Generic_Package then
Old_Counter_Val := Counter_Val;
Process_Declarations
(Private_Declarations (Spec), Preprocess);
Process_Declarations
(Visible_Declarations (Spec), Preprocess);
-- Either the visible or the private declarations contain a
-- controlled object. The nested package declaration is the
-- last such construct.
if Preprocess
and then Top_Level
and then No (Last_Top_Level_Ctrl_Construct)
and then Counter_Val > Old_Counter_Val
then
Last_Top_Level_Ctrl_Construct := Decl;
end if;
end if;
-- Nested package bodies, avoid generics
elsif Nkind (Decl) = N_Package_Body then
Process_Package_Body (Decl);
elsif Nkind (Decl) = N_Package_Body_Stub
and then Present (Library_Unit (Decl))
then
Process_Package_Body (Proper_Body (Unit (Library_Unit (Decl))));
end if;
Prev_Non_Pragma (Decl);
end loop;
end Process_Declarations;
--------------------------------
-- Process_Object_Declaration --
--------------------------------
procedure Process_Object_Declaration
(Decl : Node_Id;
Has_No_Init : Boolean := False;
Is_Protected : Boolean := False)
is
Loc : constant Source_Ptr := Sloc (Decl);
Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
Init_Typ : Entity_Id;
-- The initialization type of the related object declaration. Note
-- that this is not necessarily the same type as Obj_Typ because of
-- possible type derivations.
Obj_Typ : Entity_Id;
-- The type of the related object declaration
function Build_BIP_Cleanup_Stmts (Func_Id : Entity_Id) return Node_Id;
-- Func_Id denotes a build-in-place function. Generate the following
-- cleanup code:
--
-- if BIPallocfrom > Secondary_Stack'Pos
-- and then BIPfinalizationmaster /= null
-- then
-- declare
-- type Ptr_Typ is access Obj_Typ;
-- for Ptr_Typ'Storage_Pool
-- use Base_Pool (BIPfinalizationmaster);
-- begin
-- Free (Ptr_Typ (Temp));
-- end;
-- end if;
--
-- Obj_Typ is the type of the current object, Temp is the original
-- allocation which Obj_Id renames.
procedure Find_Last_Init
(Last_Init : out Node_Id;
Body_Insert : out Node_Id);
-- Find the last initialization call related to object declaration
-- Decl. Last_Init denotes the last initialization call which follows
-- Decl. Body_Insert denotes a node where the finalizer body could be
-- potentially inserted after (if blocks are involved).
-----------------------------
-- Build_BIP_Cleanup_Stmts --
-----------------------------
function Build_BIP_Cleanup_Stmts
(Func_Id : Entity_Id) return Node_Id
is
Decls : constant List_Id := New_List;
Fin_Mas_Id : constant Entity_Id :=
Build_In_Place_Formal
(Func_Id, BIP_Finalization_Master);
Func_Typ : constant Entity_Id := Etype (Func_Id);
Temp_Id : constant Entity_Id :=
Entity (Prefix (Name (Parent (Obj_Id))));
Cond : Node_Id;
Free_Blk : Node_Id;
Free_Stmt : Node_Id;
Pool_Id : Entity_Id;
Ptr_Typ : Entity_Id;
begin
-- Generate:
-- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
Pool_Id := Make_Temporary (Loc, 'P');
Append_To (Decls,
Make_Object_Renaming_Declaration (Loc,
Defining_Identifier => Pool_Id,
Subtype_Mark =>
New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
Name =>
Make_Explicit_Dereference (Loc,
Prefix =>
Make_Function_Call (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
Parameter_Associations => New_List (
Make_Explicit_Dereference (Loc,
Prefix =>
New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
-- Create an access type which uses the storage pool of the
-- caller's finalization master.
-- Generate:
-- type Ptr_Typ is access Func_Typ;
Ptr_Typ := Make_Temporary (Loc, 'P');
Append_To (Decls,
Make_Full_Type_Declaration (Loc,
Defining_Identifier => Ptr_Typ,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
Subtype_Indication => New_Occurrence_Of (Func_Typ, Loc))));
-- Perform minor decoration in order to set the master and the
-- storage pool attributes.
Mutate_Ekind (Ptr_Typ, E_Access_Type);
Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
if Debug_Generated_Code then
Set_Debug_Info_Needed (Pool_Id);
end if;
-- Create an explicit free statement. Note that the free uses the
-- caller's pool expressed as a renaming.
Free_Stmt :=
Make_Free_Statement (Loc,
Expression =>
Unchecked_Convert_To (Ptr_Typ,
New_Occurrence_Of (Temp_Id, Loc)));
Set_Storage_Pool (Free_Stmt, Pool_Id);
-- Create a block to house the dummy type and the instantiation as
-- well as to perform the cleanup the temporary.
-- Generate:
-- declare
-- <Decls>
-- begin
-- Free (Ptr_Typ (Temp_Id));
-- end;
Free_Blk :=
Make_Block_Statement (Loc,
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Free_Stmt)));
-- Generate:
-- if BIPfinalizationmaster /= null then
Cond :=
Make_Op_Ne (Loc,
Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
Right_Opnd => Make_Null (Loc));
-- For unconstrained or tagged results, escalate the condition to
-- include the allocation format. Generate:
-- if BIPallocform > Secondary_Stack'Pos
-- and then BIPfinalizationmaster /= null
-- then
if Needs_BIP_Alloc_Form (Func_Id) then
declare
Alloc : constant Entity_Id :=
Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
begin
Cond :=
Make_And_Then (Loc,
Left_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => New_Occurrence_Of (Alloc, Loc),
Right_Opnd =>
Make_Integer_Literal (Loc,
UI_From_Int
(BIP_Allocation_Form'Pos (Secondary_Stack)))),
Right_Opnd => Cond);
end;
end if;
-- Generate:
-- if <Cond> then
-- <Free_Blk>
-- end if;
return
Make_If_Statement (Loc,
Condition => Cond,
Then_Statements => New_List (Free_Blk));
end Build_BIP_Cleanup_Stmts;
--------------------
-- Find_Last_Init --
--------------------
procedure Find_Last_Init
(Last_Init : out Node_Id;
Body_Insert : out Node_Id)
is
function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id;
-- Find the last initialization call within the statements of
-- block Blk.
function Is_Init_Call (N : Node_Id) return Boolean;
-- Determine whether node N denotes one of the initialization
-- procedures of types Init_Typ or Obj_Typ.
function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id;
-- Obtain the next statement which follows list member Stmt while
-- ignoring artifacts related to access-before-elaboration checks.
-----------------------------
-- Find_Last_Init_In_Block --
-----------------------------
function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id is
HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
Stmt : Node_Id;
begin
-- Examine the individual statements of the block in reverse to
-- locate the last initialization call.
if Present (HSS) and then Present (Statements (HSS)) then
Stmt := Last (Statements (HSS));
while Present (Stmt) loop
-- Peek inside nested blocks in case aborts are allowed
if Nkind (Stmt) = N_Block_Statement then
return Find_Last_Init_In_Block (Stmt);
elsif Is_Init_Call (Stmt) then
return Stmt;
end if;
Prev (Stmt);
end loop;
end if;
return Empty;
end Find_Last_Init_In_Block;
------------------
-- Is_Init_Call --
------------------
function Is_Init_Call (N : Node_Id) return Boolean is
function Is_Init_Proc_Of
(Subp_Id : Entity_Id;
Typ : Entity_Id) return Boolean;
-- Determine whether subprogram Subp_Id is a valid init proc of
-- type Typ.
---------------------
-- Is_Init_Proc_Of --
---------------------
function Is_Init_Proc_Of
(Subp_Id : Entity_Id;
Typ : Entity_Id) return Boolean
is
Deep_Init : Entity_Id := Empty;
Prim_Init : Entity_Id := Empty;
Type_Init : Entity_Id := Empty;
begin
-- Obtain all possible initialization routines of the
-- related type and try to match the subprogram entity
-- against one of them.
-- Deep_Initialize
Deep_Init := TSS (Typ, TSS_Deep_Initialize);
-- Primitive Initialize
if Is_Controlled (Typ) then
Prim_Init := Find_Optional_Prim_Op (Typ, Name_Initialize);
if Present (Prim_Init) then
Prim_Init := Ultimate_Alias (Prim_Init);
end if;
end if;
-- Type initialization routine
if Has_Non_Null_Base_Init_Proc (Typ) then
Type_Init := Base_Init_Proc (Typ);
end if;
return
(Present (Deep_Init) and then Subp_Id = Deep_Init)
or else
(Present (Prim_Init) and then Subp_Id = Prim_Init)
or else
(Present (Type_Init) and then Subp_Id = Type_Init);
end Is_Init_Proc_Of;
-- Local variables
Call_Id : Entity_Id;
-- Start of processing for Is_Init_Call
begin
if Nkind (N) = N_Procedure_Call_Statement
and then Nkind (Name (N)) = N_Identifier
then
Call_Id := Entity (Name (N));
-- Consider both the type of the object declaration and its
-- related initialization type.
return
Is_Init_Proc_Of (Call_Id, Init_Typ)
or else
Is_Init_Proc_Of (Call_Id, Obj_Typ);
end if;
return False;
end Is_Init_Call;
-----------------------------
-- Next_Suitable_Statement --
-----------------------------
function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id is
Result : Node_Id;
begin
-- Skip call markers and Program_Error raises installed by the
-- ABE mechanism.
Result := Next (Stmt);
while Present (Result) loop
exit when Nkind (Result) not in
N_Call_Marker | N_Raise_Program_Error;
Next (Result);
end loop;
return Result;
end Next_Suitable_Statement;
-- Local variables
Call : Node_Id;
Stmt : Node_Id;
Stmt_2 : Node_Id;
Deep_Init_Found : Boolean := False;
-- A flag set when a call to [Deep_]Initialize has been found
-- Start of processing for Find_Last_Init
begin
Last_Init := Decl;
Body_Insert := Empty;
-- Object renamings and objects associated with controlled
-- function results do not require initialization.
if Has_No_Init then
return;
end if;
Stmt := Next_Suitable_Statement (Decl);
-- For an object with suppressed initialization, we check whether
-- there is in fact no initialization expression. If there is not,
-- then this is an object declaration that has been turned into a
-- different object declaration that calls the build-in-place
-- function in a 'Reference attribute, as in "F(...)'Reference".
-- We search for that later object declaration, so that the
-- Inc_Decl will be inserted after the call. Otherwise, if the
-- call raises an exception, we will finalize the (uninitialized)
-- object, which is wrong.
if No_Initialization (Decl) then
if No (Expression (Last_Init)) then
loop
Next (Last_Init);
exit when No (Last_Init);
exit when Nkind (Last_Init) = N_Object_Declaration
and then Nkind (Expression (Last_Init)) = N_Reference
and then Nkind (Prefix (Expression (Last_Init))) =
N_Function_Call
and then Is_Expanded_Build_In_Place_Call
(Prefix (Expression (Last_Init)));
end loop;
end if;
return;
-- If the initialization is in the declaration, we're done, so
-- early return if we have no more statements or they have been
-- rewritten, which means that they were in the source code.
elsif No (Stmt) or else Original_Node (Stmt) /= Stmt then
return;
-- In all other cases the initialization calls follow the related
-- object. The general structure of object initialization built by
-- routine Default_Initialize_Object is as follows:
-- [begin -- aborts allowed
-- Abort_Defer;]
-- Type_Init_Proc (Obj);
-- [begin] -- exceptions allowed
-- Deep_Initialize (Obj);
-- [exception -- exceptions allowed
-- when others =>
-- Deep_Finalize (Obj, Self => False);
-- raise;
-- end;]
-- [at end -- aborts allowed
-- Abort_Undefer;
-- end;]
-- When aborts are allowed, the initialization calls are housed
-- within a block.
elsif Nkind (Stmt) = N_Block_Statement then
Last_Init := Find_Last_Init_In_Block (Stmt);
Body_Insert := Stmt;
-- Otherwise the initialization calls follow the related object
else
Stmt_2 := Next_Suitable_Statement (Stmt);
-- Check for an optional call to Deep_Initialize which may
-- appear within a block depending on whether the object has
-- controlled components.
if Present (Stmt_2) then
if Nkind (Stmt_2) = N_Block_Statement then
Call := Find_Last_Init_In_Block (Stmt_2);
if Present (Call) then
Deep_Init_Found := True;
Last_Init := Call;
Body_Insert := Stmt_2;
end if;
elsif Is_Init_Call (Stmt_2) then
Deep_Init_Found := True;
Last_Init := Stmt_2;
Body_Insert := Last_Init;
end if;
end if;
-- If the object lacks a call to Deep_Initialize, then it must
-- have a call to its related type init proc.
if not Deep_Init_Found and then Is_Init_Call (Stmt) then
Last_Init := Stmt;
Body_Insert := Last_Init;
end if;
end if;
end Find_Last_Init;
-- Local variables
Body_Ins : Node_Id;
Count_Ins : Node_Id;
Fin_Call : Node_Id;
Fin_Stmts : List_Id := No_List;
Inc_Decl : Node_Id;
Label : Node_Id;
Label_Id : Entity_Id;
Obj_Ref : Node_Id;
-- Start of processing for Process_Object_Declaration
begin
-- Handle the object type and the reference to the object. Note
-- that objects having simple protected components must retain
-- their original form for the processing below to work.
Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
Obj_Typ := Base_Type (Etype (Obj_Id));
loop
if Is_Access_Type (Obj_Typ) then
Obj_Typ := Directly_Designated_Type (Obj_Typ);
Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
elsif Is_Concurrent_Type (Obj_Typ)
and then Present (Corresponding_Record_Type (Obj_Typ))
and then not Is_Protected
then
Obj_Typ := Corresponding_Record_Type (Obj_Typ);
Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
elsif Is_Private_Type (Obj_Typ)
and then Present (Full_View (Obj_Typ))
then
Obj_Typ := Full_View (Obj_Typ);
Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
elsif Obj_Typ /= Base_Type (Obj_Typ) then
Obj_Typ := Base_Type (Obj_Typ);
Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
else
exit;
end if;
end loop;
Set_Etype (Obj_Ref, Obj_Typ);
-- Handle the initialization type of the object declaration
Init_Typ := Obj_Typ;
loop
if Is_Private_Type (Init_Typ)
and then Present (Full_View (Init_Typ))
then
Init_Typ := Full_View (Init_Typ);
elsif Is_Untagged_Derivation (Init_Typ) then
Init_Typ := Root_Type (Init_Typ);
else
exit;
end if;
end loop;
-- Set a new value for the state counter and insert the statement
-- after the object declaration. Generate:
-- Counter := <value>;
Inc_Decl :=
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Counter_Id, Loc),
Expression => Make_Integer_Literal (Loc, Counter_Val));
-- Insert the counter after all initialization has been done. The
-- place of insertion depends on the context.
if Ekind (Obj_Id) in E_Constant | E_Variable then
-- The object is initialized by a build-in-place function call.
-- The counter insertion point is after the function call.
if Present (BIP_Initialization_Call (Obj_Id)) then
Count_Ins := BIP_Initialization_Call (Obj_Id);
Body_Ins := Empty;
-- The object is initialized by an aggregate. Insert the counter
-- after the last aggregate assignment.
elsif Present (Last_Aggregate_Assignment (Obj_Id)) then
Count_Ins := Last_Aggregate_Assignment (Obj_Id);
Body_Ins := Empty;
-- In all other cases the counter is inserted after the last call
-- to either [Deep_]Initialize or the type-specific init proc.
else
Find_Last_Init (Count_Ins, Body_Ins);
end if;
-- In all other cases the counter is inserted after the last call to
-- either [Deep_]Initialize or the type-specific init proc.
else
Find_Last_Init (Count_Ins, Body_Ins);
end if;
-- If the Initialize function is null or trivial, the call will have
-- been replaced with a null statement, in which case place counter
-- declaration after object declaration itself.
if No (Count_Ins) then
Count_Ins := Decl;
end if;
Insert_After (Count_Ins, Inc_Decl);
Analyze (Inc_Decl);
-- If the current declaration is the last in the list, the finalizer
-- body needs to be inserted after the set counter statement for the
-- current object declaration. This is complicated by the fact that
-- the set counter statement may appear in abort deferred block. In
-- that case, the proper insertion place is after the block.
if No (Finalizer_Insert_Nod) then
-- Insertion after an abort deferred block
if Present (Body_Ins) then
Finalizer_Insert_Nod := Body_Ins;
else
Finalizer_Insert_Nod := Inc_Decl;
end if;
end if;
-- Create the associated label with this object, generate:
-- L<counter> : label;
Label_Id :=
Make_Identifier (Loc, New_External_Name ('L', Counter_Val));
Set_Entity
(Label_Id, Make_Defining_Identifier (Loc, Chars (Label_Id)));
Label := Make_Label (Loc, Label_Id);
Prepend_To (Finalizer_Decls,
Make_Implicit_Label_Declaration (Loc,
Defining_Identifier => Entity (Label_Id),
Label_Construct => Label));
-- Create the associated jump with this object, generate:
-- when <counter> =>
-- goto L<counter>;
Prepend_To (Jump_Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => New_List (
Make_Integer_Literal (Loc, Counter_Val)),
Statements => New_List (
Make_Goto_Statement (Loc,
Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
-- Insert the jump destination, generate:
-- <<L<counter>>>
Append_To (Finalizer_Stmts, Label);
-- Disable warnings on Obj_Id. This works around an issue where GCC
-- is not able to detect that Obj_Id is protected by a counter and
-- emits spurious warnings.
if not Comes_From_Source (Obj_Id) then
Set_Warnings_Off (Obj_Id);
end if;
-- Processing for simple protected objects. Such objects require
-- manual finalization of their lock managers.
if Is_Protected then
if Is_Simple_Protected_Type (Obj_Typ) then
Fin_Call := Cleanup_Protected_Object (Decl, Obj_Ref);
if Present (Fin_Call) then
Fin_Stmts := New_List (Fin_Call);
end if;
elsif Is_Array_Type (Obj_Typ) then
Fin_Stmts := Cleanup_Array (Decl, Obj_Ref, Obj_Typ);
else
Fin_Stmts := Cleanup_Record (Decl, Obj_Ref, Obj_Typ);
end if;
-- Generate:
-- begin
-- System.Tasking.Protected_Objects.Finalize_Protection
-- (Obj._object);
-- exception
-- when others =>
-- null;
-- end;
if Present (Fin_Stmts) and then Exceptions_OK then
Fin_Stmts := New_List (
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Fin_Stmts,
Exception_Handlers => New_List (
Make_Exception_Handler (Loc,
Exception_Choices => New_List (
Make_Others_Choice (Loc)),
Statements => New_List (
Make_Null_Statement (Loc)))))));
end if;
-- Processing for regular controlled objects
else
-- Generate:
-- begin
-- [Deep_]Finalize (Obj);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Id);
-- end if;
-- end;
Fin_Call :=
Make_Final_Call (
Obj_Ref => Obj_Ref,
Typ => Obj_Typ);
-- Guard against a missing [Deep_]Finalize when the object type
-- was not properly frozen.
if No (Fin_Call) then
Fin_Call := Make_Null_Statement (Loc);
end if;
-- For CodePeer, the exception handlers normally generated here
-- generate complex flowgraphs which result in capacity problems.
-- Omitting these handlers for CodePeer is justified as follows:
-- If a handler is dead, then omitting it is surely ok
-- If a handler is live, then CodePeer should flag the
-- potentially-exception-raising construct that causes it
-- to be live. That is what we are interested in, not what
-- happens after the exception is raised.
if Exceptions_OK and not CodePeer_Mode then
Fin_Stmts := New_List (
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Call),
Exception_Handlers => New_List (
Build_Exception_Handler
(Finalizer_Data, For_Package)))));
-- When exception handlers are prohibited, the finalization call
-- appears unprotected. Any exception raised during finalization
-- will bypass the circuitry which ensures the cleanup of all
-- remaining objects.
else
Fin_Stmts := New_List (Fin_Call);
end if;
-- If we are dealing with a return object of a build-in-place
-- function, generate the following cleanup statements:
-- if BIPallocfrom > Secondary_Stack'Pos
-- and then BIPfinalizationmaster /= null
-- then
-- declare
-- type Ptr_Typ is access Obj_Typ;
-- for Ptr_Typ'Storage_Pool use
-- Base_Pool (BIPfinalizationmaster.all).all;
-- begin
-- Free (Ptr_Typ (Temp));
-- end;
-- end if;
-- The generated code effectively detaches the temporary from the
-- caller finalization master and deallocates the object.
if Is_Return_Object (Obj_Id) then
declare
Func_Id : constant Entity_Id :=
Return_Applies_To (Scope (Obj_Id));
begin
if Is_Build_In_Place_Function (Func_Id)
and then Needs_BIP_Finalization_Master (Func_Id)
then
Append_To (Fin_Stmts, Build_BIP_Cleanup_Stmts (Func_Id));
end if;
end;
end if;
if Ekind (Obj_Id) in E_Constant | E_Variable
and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
then
-- Temporaries created for the purpose of "exporting" a
-- transient object out of an Expression_With_Actions (EWA)
-- need guards. The following illustrates the usage of such
-- temporaries.
-- Access_Typ : access [all] Obj_Typ;
-- Temp : Access_Typ := null;
-- <Counter> := ...;
-- do
-- Ctrl_Trans : [access [all]] Obj_Typ := ...;
-- Temp := Access_Typ (Ctrl_Trans); -- when a pointer
-- <or>
-- Temp := Ctrl_Trans'Unchecked_Access;
-- in ... end;
-- The finalization machinery does not process EWA nodes as
-- this may lead to premature finalization of expressions. Note
-- that Temp is marked as being properly initialized regardless
-- of whether the initialization of Ctrl_Trans succeeded. Since
-- a failed initialization may leave Temp with a value of null,
-- add a guard to handle this case:
-- if Obj /= null then
-- <object finalization statements>
-- end if;
if Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
N_Object_Declaration
then
Fin_Stmts := New_List (
Make_If_Statement (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd => New_Occurrence_Of (Obj_Id, Loc),
Right_Opnd => Make_Null (Loc)),
Then_Statements => Fin_Stmts));
-- Return objects use a flag to aid in processing their
-- potential finalization when the enclosing function fails
-- to return properly. Generate:
-- if not Flag then
-- <object finalization statements>
-- end if;
else
Fin_Stmts := New_List (
Make_If_Statement (Loc,
Condition =>
Make_Op_Not (Loc,
Right_Opnd =>
New_Occurrence_Of
(Status_Flag_Or_Transient_Decl (Obj_Id), Loc)),
Then_Statements => Fin_Stmts));
end if;
end if;
end if;
Append_List_To (Finalizer_Stmts, Fin_Stmts);
-- Since the declarations are examined in reverse, the state counter
-- must be decremented in order to keep with the true position of
-- objects.
Counter_Val := Counter_Val - 1;
end Process_Object_Declaration;
-------------------------------------
-- Process_Tagged_Type_Declaration --
-------------------------------------
procedure Process_Tagged_Type_Declaration (Decl : Node_Id) is
Typ : constant Entity_Id := Defining_Identifier (Decl);
DT_Ptr : constant Entity_Id :=
Node (First_Elmt (Access_Disp_Table (Typ)));
begin
-- Generate:
-- Ada.Tags.Unregister_Tag (<Typ>P);
Append_To (Tagged_Type_Stmts,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Unregister_Tag), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (DT_Ptr, Loc))));
end Process_Tagged_Type_Declaration;
-- Start of processing for Build_Finalizer
begin
Fin_Id := Empty;
-- Do not perform this expansion in SPARK mode because it is not
-- necessary.
if GNATprove_Mode then
return;
end if;
-- Step 1: Extract all lists which may contain controlled objects or
-- library-level tagged types.
if For_Package_Spec then
Decls := Visible_Declarations (Specification (N));
Priv_Decls := Private_Declarations (Specification (N));
-- Retrieve the package spec id
Spec_Id := Defining_Unit_Name (Specification (N));
if Nkind (Spec_Id) = N_Defining_Program_Unit_Name then
Spec_Id := Defining_Identifier (Spec_Id);
end if;
-- Accept statement, block, entry body, package body, protected body,
-- subprogram body or task body.
else
Decls := Declarations (N);
HSS := Handled_Statement_Sequence (N);
if Present (HSS) then
if Present (Statements (HSS)) then
Stmts := Statements (HSS);
end if;
if Present (At_End_Proc (HSS)) then
Prev_At_End := At_End_Proc (HSS);
end if;
end if;
-- Retrieve the package spec id for package bodies
if For_Package_Body then
Spec_Id := Corresponding_Spec (N);
end if;
end if;
-- We do not need to process nested packages since they are handled by
-- the finalizer of the enclosing scope, including at library level.
-- And we do not build two finalizers for an instance without body that
-- is a library unit (see Analyze_Package_Instantiation).
if For_Package
and then (not Is_Compilation_Unit (Spec_Id)
or else (Is_Generic_Instance (Spec_Id)
and then Package_Instantiation (Spec_Id) = N))
then
return;
end if;
-- Step 2: Object [pre]processing
if For_Package then
-- For package specs and bodies, we are invoked from the Standard
-- scope, so we need to push the specs onto the scope stack first.
Push_Scope (Spec_Id);
-- Preprocess the visible declarations now in order to obtain the
-- correct number of controlled object by the time the private
-- declarations are processed.
Process_Declarations (Decls, Preprocess => True, Top_Level => True);
-- From all the possible contexts, only package specifications may
-- have private declarations.
if For_Package_Spec then
Process_Declarations
(Priv_Decls, Preprocess => True, Top_Level => True);
end if;
-- The current context may lack controlled objects, but require some
-- other form of completion (task termination for instance). In such
-- cases, the finalizer must be created and carry the additional
-- statements.
if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
Build_Components;
end if;
-- The preprocessing has determined that the context has controlled
-- objects or library-level tagged types.
if Has_Ctrl_Objs or Has_Tagged_Types then
-- Private declarations are processed first in order to preserve
-- possible dependencies between public and private objects.
if For_Package_Spec then
Process_Declarations (Priv_Decls);
end if;
Process_Declarations (Decls);
end if;
-- Non-package case
else
-- Preprocess both declarations and statements
Process_Declarations (Decls, Preprocess => True, Top_Level => True);
Process_Declarations (Stmts, Preprocess => True, Top_Level => True);
-- At this point it is known that N has controlled objects. Ensure
-- that N has a declarative list since the finalizer spec will be
-- attached to it.
if Has_Ctrl_Objs and then No (Decls) then
Set_Declarations (N, New_List);
Decls := Declarations (N);
Spec_Decls := Decls;
end if;
-- The current context may lack controlled objects, but require some
-- other form of completion (task termination for instance). In such
-- cases, the finalizer must be created and carry the additional
-- statements.
if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
Build_Components;
end if;
if Has_Ctrl_Objs or Has_Tagged_Types then
Process_Declarations (Stmts);
Process_Declarations (Decls);
end if;
end if;
-- Step 3: Finalizer creation
if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
Create_Finalizer;
end if;
-- Pop the scope that was pushed above for package specs and bodies
if For_Package then
Pop_Scope;
end if;
end Build_Finalizer;
--------------------------
-- Build_Finalizer_Call --
--------------------------
procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id) is
begin
-- Do not perform this expansion in SPARK mode because we do not create
-- finalizers in the first place.
if GNATprove_Mode then
return;
end if;
-- If the construct to be cleaned up is a protected subprogram body, the
-- finalizer call needs to be associated with the block that wraps the
-- unprotected version of the subprogram. The following illustrates this
-- scenario:
-- procedure Prot_SubpP is
-- procedure finalizer is
-- begin
-- Service_Entries (Prot_Obj);
-- Abort_Undefer;
-- end finalizer;
-- begin
-- . . .
-- begin
-- Prot_SubpN (Prot_Obj);
-- at end
-- finalizer;
-- end;
-- end Prot_SubpP;
declare
Loc : constant Source_Ptr := Sloc (N);
Is_Protected_Subp_Body : constant Boolean :=
Nkind (N) = N_Subprogram_Body
and then Is_Protected_Subprogram_Body (N);
-- True if N is the protected version of a subprogram that belongs to
-- a protected type.
HSS : constant Node_Id :=
(if Is_Protected_Subp_Body
then Handled_Statement_Sequence
(Last (Statements (Handled_Statement_Sequence (N))))
else Handled_Statement_Sequence (N));
-- We attach the At_End_Proc to the HSS if this is an accept
-- statement or extended return statement. Also in the case of
-- a protected subprogram, because if Service_Entries raises an
-- exception, we do not lock the PO, so we also do not want to
-- unlock it.
Use_HSS : constant Boolean :=
Nkind (N) in N_Accept_Statement | N_Extended_Return_Statement
or else Is_Protected_Subp_Body;
At_End_Proc_Bearer : constant Node_Id := (if Use_HSS then HSS else N);
begin
pragma Assert (No (At_End_Proc (At_End_Proc_Bearer)));
Set_At_End_Proc (At_End_Proc_Bearer, New_Occurrence_Of (Fin_Id, Loc));
-- Attach reference to finalizer to tree, for LLVM use
Set_Parent (At_End_Proc (At_End_Proc_Bearer), At_End_Proc_Bearer);
Analyze (At_End_Proc (At_End_Proc_Bearer));
Expand_At_End_Handler (At_End_Proc_Bearer, Empty);
end;
end Build_Finalizer_Call;
---------------------
-- Build_Late_Proc --
---------------------
procedure Build_Late_Proc (Typ : Entity_Id; Nam : Name_Id) is
begin
for Final_Prim in Name_Of'Range loop
if Name_Of (Final_Prim) = Nam then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Final_Prim,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Final_Prim, Typ)));
end if;
end loop;
end Build_Late_Proc;
-------------------------------
-- Build_Object_Declarations --
-------------------------------
procedure Build_Object_Declarations
(Data : out Finalization_Exception_Data;
Decls : List_Id;
Loc : Source_Ptr;
For_Package : Boolean := False)
is
Decl : Node_Id;
Dummy : Entity_Id;
-- This variable captures an unused dummy internal entity, see the
-- comment associated with its use.
begin
pragma Assert (Decls /= No_List);
-- Always set the proper location as it may be needed even when
-- exception propagation is forbidden.
Data.Loc := Loc;
if Restriction_Active (No_Exception_Propagation) then
Data.Abort_Id := Empty;
Data.E_Id := Empty;
Data.Raised_Id := Empty;
return;
end if;
Data.Raised_Id := Make_Temporary (Loc, 'R');
-- In certain scenarios, finalization can be triggered by an abort. If
-- the finalization itself fails and raises an exception, the resulting
-- Program_Error must be supressed and replaced by an abort signal. In
-- order to detect this scenario, save the state of entry into the
-- finalization code.
-- This is not needed for library-level finalizers as they are called by
-- the environment task and cannot be aborted.
if not For_Package then
if Abort_Allowed then
Data.Abort_Id := Make_Temporary (Loc, 'A');
-- Generate:
-- Abort_Id : constant Boolean := <A_Expr>;
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Data.Abort_Id,
Constant_Present => True,
Object_Definition =>
New_Occurrence_Of (Standard_Boolean, Loc),
Expression =>
New_Occurrence_Of (RTE (RE_Triggered_By_Abort), Loc)));
-- Abort is not required
else
-- Generate a dummy entity to ensure that the internal symbols are
-- in sync when a unit is compiled with and without aborts.
Dummy := Make_Temporary (Loc, 'A');
Data.Abort_Id := Empty;
end if;
-- Library-level finalizers
else
Data.Abort_Id := Empty;
end if;
if Exception_Extra_Info then
Data.E_Id := Make_Temporary (Loc, 'E');
-- Generate:
-- E_Id : Exception_Occurrence;
Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier => Data.E_Id,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc));
Set_No_Initialization (Decl);
Append_To (Decls, Decl);
else
Data.E_Id := Empty;
end if;
-- Generate:
-- Raised_Id : Boolean := False;
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Data.Raised_Id,
Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
Expression => New_Occurrence_Of (Standard_False, Loc)));
if Debug_Generated_Code then
Set_Debug_Info_Needed (Data.Raised_Id);
end if;
end Build_Object_Declarations;
---------------------------
-- Build_Raise_Statement --
---------------------------
function Build_Raise_Statement
(Data : Finalization_Exception_Data) return Node_Id
is
Stmt : Node_Id;
Expr : Node_Id;
begin
-- Standard run-time use the specialized routine
-- Raise_From_Controlled_Operation.
if Exception_Extra_Info
and then RTE_Available (RE_Raise_From_Controlled_Operation)
then
Stmt :=
Make_Procedure_Call_Statement (Data.Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Raise_From_Controlled_Operation), Data.Loc),
Parameter_Associations =>
New_List (New_Occurrence_Of (Data.E_Id, Data.Loc)));
-- Restricted run-time: exception messages are not supported and hence
-- Raise_From_Controlled_Operation is not supported. Raise Program_Error
-- instead.
else
Stmt :=
Make_Raise_Program_Error (Data.Loc,
Reason => PE_Finalize_Raised_Exception);
end if;
-- Generate:
-- Raised_Id and then not Abort_Id
-- <or>
-- Raised_Id
Expr := New_Occurrence_Of (Data.Raised_Id, Data.Loc);
if Present (Data.Abort_Id) then
Expr := Make_And_Then (Data.Loc,
Left_Opnd => Expr,
Right_Opnd =>
Make_Op_Not (Data.Loc,
Right_Opnd => New_Occurrence_Of (Data.Abort_Id, Data.Loc)));
end if;
-- Generate:
-- if Raised_Id and then not Abort_Id then
-- Raise_From_Controlled_Operation (E_Id);
-- <or>
-- raise Program_Error; -- restricted runtime
-- end if;
return
Make_If_Statement (Data.Loc,
Condition => Expr,
Then_Statements => New_List (Stmt));
end Build_Raise_Statement;
-----------------------------
-- Build_Record_Deep_Procs --
-----------------------------
procedure Build_Record_Deep_Procs (Typ : Entity_Id) is
begin
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Initialize_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Initialize_Case, Typ)));
if not Is_Inherently_Limited_Type (Typ) then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Adjust_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Adjust_Case, Typ)));
end if;
-- Do not generate Deep_Finalize and Finalize_Address if finalization is
-- suppressed since these routine will not be used.
if not Restriction_Active (No_Finalization) then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Finalize_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Finalize_Case, Typ)));
-- Create TSS primitive Finalize_Address (unless CodePeer_Mode)
if not CodePeer_Mode then
Set_TSS (Typ,
Make_Deep_Proc
(Prim => Address_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Address_Case, Typ)));
end if;
end if;
end Build_Record_Deep_Procs;
-------------------
-- Cleanup_Array --
-------------------
function Cleanup_Array
(N : Node_Id;
Obj : Node_Id;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (N);
Index_List : constant List_Id := New_List;
function Free_Component return List_Id;
-- Generate the code to finalize the task or protected subcomponents
-- of a single component of the array.
function Free_One_Dimension (Dim : Int) return List_Id;
-- Generate a loop over one dimension of the array
--------------------
-- Free_Component --
--------------------
function Free_Component return List_Id is
Stmts : List_Id := New_List;
Tsk : Node_Id;
C_Typ : constant Entity_Id := Component_Type (Typ);
begin
-- Component type is known to contain tasks or protected objects
Tsk :=
Make_Indexed_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Obj),
Expressions => Index_List);
Set_Etype (Tsk, C_Typ);
if Is_Task_Type (C_Typ) then
Append_To (Stmts, Cleanup_Task (N, Tsk));
elsif Is_Simple_Protected_Type (C_Typ) then
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
elsif Is_Record_Type (C_Typ) then
Stmts := Cleanup_Record (N, Tsk, C_Typ);
elsif Is_Array_Type (C_Typ) then
Stmts := Cleanup_Array (N, Tsk, C_Typ);
end if;
return Stmts;
end Free_Component;
------------------------
-- Free_One_Dimension --
------------------------
function Free_One_Dimension (Dim : Int) return List_Id is
Index : Entity_Id;
begin
if Dim > Number_Dimensions (Typ) then
return Free_Component;
-- Here we generate the required loop
else
Index := Make_Temporary (Loc, 'J');
Append (New_Occurrence_Of (Index, Loc), Index_List);
return New_List (
Make_Implicit_Loop_Statement (N,
Identifier => Empty,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Index,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr (Obj),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))))),
Statements => Free_One_Dimension (Dim + 1)));
end if;
end Free_One_Dimension;
-- Start of processing for Cleanup_Array
begin
return Free_One_Dimension (1);
end Cleanup_Array;
--------------------
-- Cleanup_Record --
--------------------
function Cleanup_Record
(N : Node_Id;
Obj : Node_Id;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (N);
Stmts : constant List_Id := New_List;
U_Typ : constant Entity_Id := Underlying_Type (Typ);
Comp : Entity_Id;
Tsk : Node_Id;
begin
if Has_Discriminants (U_Typ)
and then Nkind (Parent (U_Typ)) = N_Full_Type_Declaration
and then Nkind (Type_Definition (Parent (U_Typ))) = N_Record_Definition
and then
Present
(Variant_Part (Component_List (Type_Definition (Parent (U_Typ)))))
then
-- For now, do not attempt to free a component that may appear in a
-- variant, and instead issue a warning. Doing this "properly" would
-- require building a case statement and would be quite a mess. Note
-- that the RM only requires that free "work" for the case of a task
-- access value, so already we go way beyond this in that we deal
-- with the array case and non-discriminated record cases.
Error_Msg_N
("task/protected object in variant record will not be freed??", N);
return New_List (Make_Null_Statement (Loc));
end if;
Comp := First_Component (U_Typ);
while Present (Comp) loop
if Chars (Comp) /= Name_uParent
and then (Has_Task (Etype (Comp))
or else Has_Simple_Protected_Object (Etype (Comp)))
then
Tsk :=
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Obj),
Selector_Name => New_Occurrence_Of (Comp, Loc));
Set_Etype (Tsk, Etype (Comp));
if Is_Task_Type (Etype (Comp)) then
Append_To (Stmts, Cleanup_Task (N, Tsk));
elsif Is_Simple_Protected_Type (Etype (Comp)) then
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
elsif Is_Record_Type (Etype (Comp)) then
-- Recurse, by generating the prefix of the argument to the
-- eventual cleanup call.
Append_List_To (Stmts, Cleanup_Record (N, Tsk, Etype (Comp)));
elsif Is_Array_Type (Etype (Comp)) then
Append_List_To (Stmts, Cleanup_Array (N, Tsk, Etype (Comp)));
end if;
end if;
Next_Component (Comp);
end loop;
return Stmts;
end Cleanup_Record;
------------------------------
-- Cleanup_Protected_Object --
------------------------------
function Cleanup_Protected_Object
(N : Node_Id;
Ref : Node_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
begin
-- For restricted run-time libraries (Ravenscar), tasks are
-- non-terminating, and protected objects can only appear at library
-- level, so we do not want finalization of protected objects.
if Restricted_Profile then
return Empty;
else
return
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Finalize_Protection), Loc),
Parameter_Associations => New_List (Concurrent_Ref (Ref)));
end if;
end Cleanup_Protected_Object;
------------------
-- Cleanup_Task --
------------------
function Cleanup_Task
(N : Node_Id;
Ref : Node_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
begin
-- For restricted run-time libraries (Ravenscar), tasks are
-- non-terminating and they can only appear at library level,
-- so we do not want finalization of task objects.
if Restricted_Profile then
return Empty;
else
return
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Free_Task), Loc),
Parameter_Associations => New_List (Concurrent_Ref (Ref)));
end if;
end Cleanup_Task;
--------------------------------------
-- Check_Unnesting_Elaboration_Code --
--------------------------------------
procedure Check_Unnesting_Elaboration_Code (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Block_Elab_Proc : Entity_Id := Empty;
procedure Set_Block_Elab_Proc;
-- Create a defining identifier for a procedure that will replace
-- a block with nested subprograms (unless it has already been created,
-- in which case this is a no-op).
procedure Set_Block_Elab_Proc is
begin
if No (Block_Elab_Proc) then
Block_Elab_Proc := Make_Temporary (Loc, 'I');
end if;
end Set_Block_Elab_Proc;
procedure Reset_Scopes_To_Block_Elab_Proc (L : List_Id);
-- Find entities in the elaboration code of a library package body that
-- contain or represent a subprogram body. A body can appear within a
-- block or a loop or can appear by itself if generated for an object
-- declaration that involves controlled actions. The first such entity
-- forces creation of a new procedure entity (via Set_Block_Elab_Proc)
-- that will be used to reset the scopes of all entities that become
-- local to the new elaboration procedure. This is needed for subsequent
-- unnesting actions, which depend on proper setting of the Scope links
-- to determine the nesting level of each subprogram.
-----------------------
-- Find_Local_Scope --
-----------------------
procedure Reset_Scopes_To_Block_Elab_Proc (L : List_Id) is
Id : Entity_Id;
Stat : Node_Id;
Node : Node_Id;
begin
Stat := First (L);
while Present (Stat) loop
case Nkind (Stat) is
when N_Block_Statement =>
if Present (Identifier (Stat)) then
Id := Entity (Identifier (Stat));
-- The Scope of this block needs to be reset to the new
-- procedure if the block contains nested subprograms.
if Present (Id) and then Contains_Subprogram (Id) then
Set_Block_Elab_Proc;
Set_Scope (Id, Block_Elab_Proc);
end if;
end if;
when N_Loop_Statement =>
Id := Entity (Identifier (Stat));
if Present (Id) and then Contains_Subprogram (Id) then
if Scope (Id) = Current_Scope then
Set_Block_Elab_Proc;
Set_Scope (Id, Block_Elab_Proc);
end if;
end if;
-- We traverse the loop's statements as well, which may
-- include other block (etc.) statements that need to have
-- their Scope set to Block_Elab_Proc. (Is this really the
-- case, or do such nested blocks refer to the loop scope
-- rather than the loop's enclosing scope???.)
Reset_Scopes_To_Block_Elab_Proc (Statements (Stat));
when N_If_Statement =>
Reset_Scopes_To_Block_Elab_Proc (Then_Statements (Stat));
Reset_Scopes_To_Block_Elab_Proc (Else_Statements (Stat));
Node := First (Elsif_Parts (Stat));
while Present (Node) loop
Reset_Scopes_To_Block_Elab_Proc (Then_Statements (Node));
Next (Node);
end loop;
when N_Case_Statement =>
Node := First (Alternatives (Stat));
while Present (Node) loop
Reset_Scopes_To_Block_Elab_Proc (Statements (Node));
Next (Node);
end loop;
-- Reset the Scope of a subprogram occurring at the top level
when N_Subprogram_Body =>
Id := Defining_Entity (Stat);
Set_Block_Elab_Proc;
Set_Scope (Id, Block_Elab_Proc);
when others =>
null;
end case;
Next (Stat);
end loop;
end Reset_Scopes_To_Block_Elab_Proc;
-- Local variables
H_Seq : constant Node_Id := Handled_Statement_Sequence (N);
Elab_Body : Node_Id;
Elab_Call : Node_Id;
-- Start of processing for Check_Unnesting_Elaboration_Code
begin
if Present (H_Seq) then
Reset_Scopes_To_Block_Elab_Proc (Statements (H_Seq));
-- There may be subprograms declared in the exception handlers
-- of the current body.
if Present (Exception_Handlers (H_Seq)) then
declare
Handler : Node_Id := First (Exception_Handlers (H_Seq));
begin
while Present (Handler) loop
Reset_Scopes_To_Block_Elab_Proc (Statements (Handler));
Next (Handler);
end loop;
end;
end if;
if Present (Block_Elab_Proc) then
Elab_Body :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Block_Elab_Proc),
Declarations => New_List,
Handled_Statement_Sequence =>
Relocate_Node (Handled_Statement_Sequence (N)));
Elab_Call :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Block_Elab_Proc, Loc));
Append_To (Declarations (N), Elab_Body);
Analyze (Elab_Body);
Set_Has_Nested_Subprogram (Block_Elab_Proc);
Set_Handled_Statement_Sequence (N,
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Elab_Call)));
Analyze (Elab_Call);
-- Could we reset the scopes of entities associated with the new
-- procedure here via a loop over entities rather than doing it in
-- the recursive Reset_Scopes_To_Elab_Proc procedure???
end if;
end if;
end Check_Unnesting_Elaboration_Code;
---------------------------------------
-- Check_Unnesting_In_Decls_Or_Stmts --
---------------------------------------
procedure Check_Unnesting_In_Decls_Or_Stmts (Decls_Or_Stmts : List_Id) is
Decl_Or_Stmt : Node_Id;
begin
if Unnest_Subprogram_Mode
and then Present (Decls_Or_Stmts)
then
Decl_Or_Stmt := First (Decls_Or_Stmts);
while Present (Decl_Or_Stmt) loop
if Nkind (Decl_Or_Stmt) = N_Block_Statement
and then Contains_Subprogram (Entity (Identifier (Decl_Or_Stmt)))
then
Unnest_Block (Decl_Or_Stmt);
-- If-statements may contain subprogram bodies at the outer level
-- of their statement lists, and the subprograms may make up-level
-- references (such as to objects declared in the same statement
-- list). Unlike block and loop cases, however, we don't have an
-- entity on which to test the Contains_Subprogram flag, so
-- Unnest_If_Statement must traverse the statement lists to
-- determine whether there are nested subprograms present.
elsif Nkind (Decl_Or_Stmt) = N_If_Statement then
Unnest_If_Statement (Decl_Or_Stmt);
elsif Nkind (Decl_Or_Stmt) = N_Loop_Statement then
declare
Id : constant Entity_Id :=
Entity (Identifier (Decl_Or_Stmt));
begin
-- When a top-level loop within declarations of a library
-- package spec or body contains nested subprograms, we wrap
-- it in a procedure to handle possible up-level references
-- to entities associated with the loop (such as loop
-- parameters).
if Present (Id) and then Contains_Subprogram (Id) then
Unnest_Loop (Decl_Or_Stmt);
end if;
end;
elsif Nkind (Decl_Or_Stmt) = N_Package_Declaration
and then not Modify_Tree_For_C
then
Check_Unnesting_In_Decls_Or_Stmts
(Visible_Declarations (Specification (Decl_Or_Stmt)));
Check_Unnesting_In_Decls_Or_Stmts
(Private_Declarations (Specification (Decl_Or_Stmt)));
elsif Nkind (Decl_Or_Stmt) = N_Package_Body
and then not Modify_Tree_For_C
then
Check_Unnesting_In_Decls_Or_Stmts (Declarations (Decl_Or_Stmt));
if Present (Statements
(Handled_Statement_Sequence (Decl_Or_Stmt)))
then
Check_Unnesting_In_Decls_Or_Stmts (Statements
(Handled_Statement_Sequence (Decl_Or_Stmt)));
Check_Unnesting_In_Handlers (Decl_Or_Stmt);
end if;
end if;
Next (Decl_Or_Stmt);
end loop;
end if;
end Check_Unnesting_In_Decls_Or_Stmts;
---------------------------------
-- Check_Unnesting_In_Handlers --
---------------------------------
procedure Check_Unnesting_In_Handlers (N : Node_Id) is
Stmt_Seq : constant Node_Id := Handled_Statement_Sequence (N);
begin
if Present (Stmt_Seq)
and then Present (Exception_Handlers (Stmt_Seq))
then
declare
Handler : Node_Id := First (Exception_Handlers (Stmt_Seq));
begin
while Present (Handler) loop
if Present (Statements (Handler)) then
Check_Unnesting_In_Decls_Or_Stmts (Statements (Handler));
end if;
Next (Handler);
end loop;
end;
end if;
end Check_Unnesting_In_Handlers;
------------------------------
-- Check_Visibly_Controlled --
------------------------------
procedure Check_Visibly_Controlled
(Prim : Final_Primitives;
Typ : Entity_Id;
E : in out Entity_Id;
Cref : in out Node_Id)
is
Parent_Type : Entity_Id;
Op : Entity_Id;
begin
if Is_Derived_Type (Typ)
and then Comes_From_Source (E)
and then No (Overridden_Operation (E))
then
-- We know that the explicit operation on the type does not override
-- the inherited operation of the parent, and that the derivation
-- is from a private type that is not visibly controlled.
Parent_Type := Etype (Typ);
Op := Find_Optional_Prim_Op (Parent_Type, Name_Of (Prim));
if Present (Op) then
E := Op;
-- Wrap the object to be initialized into the proper
-- unchecked conversion, to be compatible with the operation
-- to be called.
if Nkind (Cref) = N_Unchecked_Type_Conversion then
Cref := Unchecked_Convert_To (Parent_Type, Expression (Cref));
else
Cref := Unchecked_Convert_To (Parent_Type, Cref);
end if;
end if;
end if;
end Check_Visibly_Controlled;
--------------------------
-- Contains_Subprogram --
--------------------------
function Contains_Subprogram (Blk : Entity_Id) return Boolean is
E : Entity_Id;
begin
E := First_Entity (Blk);
-- The compiler may generate loops with a declare block containing
-- nested procedures used for finalization. Recursively search for
-- subprograms in such constructs.
if Ekind (Blk) = E_Loop
and then Parent_Kind (Blk) = N_Loop_Statement
then
declare
Stmt : Node_Id := First (Statements (Parent (Blk)));
begin
while Present (Stmt) loop
if Nkind (Stmt) = N_Block_Statement then
declare
Id : constant Entity_Id :=
Entity (Identifier (Stmt));
begin
if Contains_Subprogram (Id) then
return True;
end if;
end;
end if;
Next (Stmt);
end loop;
end;
end if;
while Present (E) loop
if Is_Subprogram (E) then
return True;
elsif Ekind (E) in E_Block | E_Loop
and then Contains_Subprogram (E)
then
return True;
end if;
Next_Entity (E);
end loop;
return False;
end Contains_Subprogram;
------------------
-- Convert_View --
------------------
function Convert_View (Proc : Entity_Id; Arg : Node_Id) return Node_Id is
Ftyp : constant Entity_Id := Etype (First_Formal (Proc));
Atyp : Entity_Id;
begin
if Nkind (Arg) in N_Type_Conversion | N_Unchecked_Type_Conversion then
Atyp := Entity (Subtype_Mark (Arg));
else
Atyp := Etype (Arg);
end if;
if Is_Abstract_Subprogram (Proc) and then Is_Tagged_Type (Ftyp) then
return Unchecked_Convert_To (Class_Wide_Type (Ftyp), Arg);
elsif Present (Atyp)
and then Atyp /= Ftyp
and then (Is_Private_Type (Ftyp)
or else Is_Private_Type (Atyp)
or else Is_Private_Type (Base_Type (Atyp)))
and then Implementation_Base_Type (Atyp) =
Implementation_Base_Type (Ftyp)
then
return Unchecked_Convert_To (Ftyp, Arg);
-- If the argument is already a conversion, as generated by
-- Make_Init_Call, set the target type to the type of the formal
-- directly, to avoid spurious typing problems.
elsif Nkind (Arg) in N_Unchecked_Type_Conversion | N_Type_Conversion
and then not Is_Class_Wide_Type (Atyp)
then
Set_Subtype_Mark (Arg, New_Occurrence_Of (Ftyp, Sloc (Arg)));
Set_Etype (Arg, Ftyp);
return Arg;
-- Otherwise, introduce a conversion when the designated object
-- has a type derived from the formal of the controlled routine.
elsif Is_Private_Type (Ftyp)
and then Present (Atyp)
and then Is_Derived_Type (Underlying_Type (Base_Type (Atyp)))
then
return Unchecked_Convert_To (Ftyp, Arg);
else
return Arg;
end if;
end Convert_View;
-------------------------------
-- Establish_Transient_Scope --
-------------------------------
-- This procedure is called each time a transient block has to be inserted
-- that is to say for each call to a function with unconstrained or tagged
-- result. It creates a new scope on the scope stack in order to enclose
-- all transient variables generated.
procedure Establish_Transient_Scope
(N : Node_Id;
Manage_Sec_Stack : Boolean)
is
function Is_Package_Or_Subprogram (Id : Entity_Id) return Boolean;
-- Determine whether arbitrary Id denotes a package or subprogram [body]
function Find_Enclosing_Transient_Scope return Int;
-- Examine the scope stack looking for the nearest enclosing transient
-- scope within the innermost enclosing package or subprogram. Return
-- its index in the table or else -1 if no such scope exists.
function Find_Transient_Context (N : Node_Id) return Node_Id;
-- Locate a suitable context for arbitrary node N which may need to be
-- serviced by a transient scope. Return Empty if no suitable context
-- is available.
procedure Delegate_Sec_Stack_Management;
-- Move the management of the secondary stack to the nearest enclosing
-- suitable scope.
procedure Create_Transient_Scope (Context : Node_Id);
-- Place a new scope on the scope stack in order to service construct
-- Context. Context is the node found by Find_Transient_Context. The
-- new scope may also manage the secondary stack.
----------------------------
-- Create_Transient_Scope --
----------------------------
procedure Create_Transient_Scope (Context : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Iter_Loop : Entity_Id;
Trans_Scop : constant Entity_Id :=
New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
begin
Set_Etype (Trans_Scop, Standard_Void_Type);
-- Push a new scope, and set its Node_To_Be_Wrapped and Is_Transient
-- fields.
Push_Scope (Trans_Scop);
Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped := Context;
Scope_Stack.Table (Scope_Stack.Last).Is_Transient := True;
-- The transient scope must also manage the secondary stack
if Manage_Sec_Stack then
Set_Uses_Sec_Stack (Trans_Scop);
Check_Restriction (No_Secondary_Stack, N);
-- The expansion of iterator loops generates references to objects
-- in order to extract elements from a container:
-- Ref : Reference_Type_Ptr := Reference (Container, Cursor);
-- Obj : <object type> renames Ref.all.Element.all;
-- These references are controlled and returned on the secondary
-- stack. A new reference is created at each iteration of the loop
-- and as a result it must be finalized and the space occupied by
-- it on the secondary stack reclaimed at the end of the current
-- iteration.
-- When the context that requires a transient scope is a call to
-- routine Reference, the node to be wrapped is the source object:
-- for Obj of Container loop
-- Routine Wrap_Transient_Declaration however does not generate
-- a physical block as wrapping a declaration will kill it too
-- early. To handle this peculiar case, mark the related iterator
-- loop as requiring the secondary stack. This signals the
-- finalization machinery to manage the secondary stack (see
-- routine Process_Statements_For_Controlled_Objects).
Iter_Loop := Find_Enclosing_Iterator_Loop (Trans_Scop);
if Present (Iter_Loop) then
Set_Uses_Sec_Stack (Iter_Loop);
end if;
end if;
if Debug_Flag_W then
Write_Str (" <Transient>");
Write_Eol;
end if;
end Create_Transient_Scope;
-----------------------------------
-- Delegate_Sec_Stack_Management --
-----------------------------------
procedure Delegate_Sec_Stack_Management is
begin
for Index in reverse Scope_Stack.First .. Scope_Stack.Last loop
declare
Scope : Scope_Stack_Entry renames Scope_Stack.Table (Index);
begin
-- Prevent the search from going too far or within the scope
-- space of another unit.
if Scope.Entity = Standard_Standard then
return;
-- No transient scope should be encountered during the
-- traversal because Establish_Transient_Scope should have
-- already handled this case.
elsif Scope.Is_Transient then
raise Program_Error;
-- The construct that requires secondary stack management is
-- always enclosed by a package or subprogram scope.
elsif Is_Package_Or_Subprogram (Scope.Entity) then
Set_Uses_Sec_Stack (Scope.Entity);
Check_Restriction (No_Secondary_Stack, N);
return;
end if;
end;
end loop;
-- At this point no suitable scope was found. This should never occur
-- because a construct is always enclosed by a compilation unit which
-- has a scope.
pragma Assert (False);
end Delegate_Sec_Stack_Management;
------------------------------------
-- Find_Enclosing_Transient_Scope --
------------------------------------
function Find_Enclosing_Transient_Scope return Int is
begin
for Index in reverse Scope_Stack.First .. Scope_Stack.Last loop
declare
Scope : Scope_Stack_Entry renames Scope_Stack.Table (Index);
begin
-- Prevent the search from going too far or within the scope
-- space of another unit.
if Scope.Entity = Standard_Standard
or else Is_Package_Or_Subprogram (Scope.Entity)
then
exit;
elsif Scope.Is_Transient then
return Index;
end if;
end;
end loop;
return -1;
end Find_Enclosing_Transient_Scope;
----------------------------
-- Find_Transient_Context --
----------------------------
function Find_Transient_Context (N : Node_Id) return Node_Id is
Curr : Node_Id := N;
Prev : Node_Id := Empty;
begin
while Present (Curr) loop
case Nkind (Curr) is
-- Declarations
-- Declarations act as a boundary for a transient scope even if
-- they are not wrapped, see Wrap_Transient_Declaration.
when N_Object_Declaration
| N_Object_Renaming_Declaration
| N_Subtype_Declaration
=>
return Curr;
-- Statements
-- Statements and statement-like constructs act as a boundary
-- for a transient scope.
when N_Accept_Alternative
| N_Attribute_Definition_Clause
| N_Case_Statement
| N_Case_Statement_Alternative
| N_Code_Statement
| N_Delay_Alternative
| N_Delay_Until_Statement
| N_Delay_Relative_Statement
| N_Discriminant_Association
| N_Elsif_Part
| N_Entry_Body_Formal_Part
| N_Exit_Statement
| N_If_Statement
| N_Iteration_Scheme
| N_Terminate_Alternative
=>
pragma Assert (Present (Prev));
return Prev;
when N_Assignment_Statement =>
return Curr;
when N_Entry_Call_Statement
| N_Procedure_Call_Statement
=>
-- When an entry or procedure call acts as the alternative
-- of a conditional or timed entry call, the proper context
-- is that of the alternative.
if Nkind (Parent (Curr)) = N_Entry_Call_Alternative
and then Nkind (Parent (Parent (Curr))) in
N_Conditional_Entry_Call | N_Timed_Entry_Call
then
return Parent (Parent (Curr));
-- General case for entry or procedure calls
else
return Curr;
end if;
when N_Pragma =>
-- Pragma Check is not a valid transient context in
-- GNATprove mode because the pragma must remain unchanged.
if GNATprove_Mode
and then Get_Pragma_Id (Curr) = Pragma_Check
then
return Empty;
-- General case for pragmas
else
return Curr;
end if;
when N_Raise_Statement =>
return Curr;
when N_Simple_Return_Statement =>
declare
Fun_Id : constant Entity_Id :=
Return_Applies_To (Return_Statement_Entity (Curr));
begin
-- A transient context that must manage the secondary
-- stack cannot be a return statement of a function that
-- itself requires secondary stack management, because
-- the function's result would be reclaimed too early.
-- And returns of thunks never require transient scopes.
if (Manage_Sec_Stack
and then Needs_Secondary_Stack (Etype (Fun_Id)))
or else Is_Thunk (Fun_Id)
then
return Empty;
-- General case for return statements
else
return Curr;
end if;
end;
-- Special
when N_Attribute_Reference =>
if Is_Procedure_Attribute_Name (Attribute_Name (Curr)) then
return Curr;
end if;
-- An Ada 2012 iterator specification is not a valid context
-- because Analyze_Iterator_Specification already employs
-- special processing for it.
when N_Iterator_Specification =>
return Empty;
when N_Loop_Parameter_Specification =>
-- An iteration scheme is not a valid context because
-- routine Analyze_Iteration_Scheme already employs
-- special processing.
if Nkind (Parent (Curr)) = N_Iteration_Scheme then
return Empty;
else
return Parent (Curr);
end if;
-- Termination
-- The following nodes represent "dummy contexts" which do not
-- need to be wrapped.
when N_Component_Declaration
| N_Discriminant_Specification
| N_Parameter_Specification
=>
return Empty;
-- If the traversal leaves a scope without having been able to
-- find a construct to wrap, something is going wrong, but this
-- can happen in error situations that are not detected yet
-- (such as a dynamic string in a pragma Export).
when N_Block_Statement
| N_Entry_Body
| N_Package_Body
| N_Package_Declaration
| N_Protected_Body
| N_Subprogram_Body
| N_Task_Body
=>
return Empty;
-- Default
when others =>
null;
end case;
Prev := Curr;
Curr := Parent (Curr);
end loop;
return Empty;
end Find_Transient_Context;
------------------------------
-- Is_Package_Or_Subprogram --
------------------------------
function Is_Package_Or_Subprogram (Id : Entity_Id) return Boolean is
begin
return Ekind (Id) in E_Entry
| E_Entry_Family
| E_Function
| E_Package
| E_Procedure
| E_Subprogram_Body;
end Is_Package_Or_Subprogram;
-- Local variables
Trans_Idx : constant Int := Find_Enclosing_Transient_Scope;
Context : Node_Id;
-- Start of processing for Establish_Transient_Scope
begin
-- Do not create a new transient scope if there is already an enclosing
-- transient scope within the innermost enclosing package or subprogram.
if Trans_Idx >= 0 then
-- If the transient scope was requested for purposes of managing the
-- secondary stack, then the existing scope must perform this task,
-- unless the node to be wrapped is a return statement of a function
-- that requires secondary stack management, because the function's
-- result would be reclaimed too early (see Find_Transient_Context).
if Manage_Sec_Stack then
declare
SE : Scope_Stack_Entry renames Scope_Stack.Table (Trans_Idx);
begin
if Nkind (SE.Node_To_Be_Wrapped) /= N_Simple_Return_Statement
or else not
Needs_Secondary_Stack
(Etype
(Return_Applies_To
(Return_Statement_Entity (SE.Node_To_Be_Wrapped))))
then
Set_Uses_Sec_Stack (SE.Entity);
end if;
end;
end if;
return;
end if;
-- Find the construct that must be serviced by a new transient scope, if
-- it exists.
Context := Find_Transient_Context (N);
if Present (Context) then
if Nkind (Context) = N_Assignment_Statement then
-- An assignment statement with suppressed controlled semantics
-- does not need a transient scope because finalization is not
-- desirable at this point. Note that No_Ctrl_Actions is also
-- set for non-controlled assignments to suppress dispatching
-- _assign.
if No_Ctrl_Actions (Context)
and then Needs_Finalization (Etype (Name (Context)))
then
-- When a controlled component is initialized by a function
-- call, the result on the secondary stack is always assigned
-- to the component. Signal the nearest suitable scope that it
-- is safe to manage the secondary stack.
if Manage_Sec_Stack and then Within_Init_Proc then
Delegate_Sec_Stack_Management;
end if;
-- Otherwise the assignment is a normal transient context and thus
-- requires a transient scope.
else
Create_Transient_Scope (Context);
end if;
-- General case
else
Create_Transient_Scope (Context);
end if;
end if;
end Establish_Transient_Scope;
----------------------------
-- Expand_Cleanup_Actions --
----------------------------
procedure Expand_Cleanup_Actions (N : Node_Id) is
pragma Assert
(Nkind (N) in N_Block_Statement
| N_Subprogram_Body
| N_Task_Body
| N_Entry_Body
| N_Extended_Return_Statement);
Scop : constant Entity_Id := Current_Scope;
Is_Asynchronous_Call : constant Boolean :=
Nkind (N) = N_Block_Statement
and then Is_Asynchronous_Call_Block (N);
Is_Master : constant Boolean :=
Nkind (N) /= N_Extended_Return_Statement
and then Nkind (N) /= N_Entry_Body
and then Is_Task_Master (N);
Is_Protected_Subp_Body : constant Boolean :=
Nkind (N) = N_Subprogram_Body
and then Is_Protected_Subprogram_Body (N);
Is_Task_Allocation : constant Boolean :=
Nkind (N) = N_Block_Statement
and then Is_Task_Allocation_Block (N);
Is_Task_Body : constant Boolean :=
Nkind (Original_Node (N)) = N_Task_Body;
-- We mark the secondary stack if it is used in this construct, and
-- we're not returning a function result on the secondary stack, except
-- that a build-in-place function that might or might not return on the
-- secondary stack always needs a mark. A run-time test is required in
-- the case where the build-in-place function has a BIP_Alloc extra
-- parameter (see Create_Finalizer).
Needs_Sec_Stack_Mark : constant Boolean :=
(Uses_Sec_Stack (Scop)
and then
not Sec_Stack_Needed_For_Return (Scop))
or else
(Is_Build_In_Place_Function (Scop)
and then Needs_BIP_Alloc_Form (Scop));
Needs_Custom_Cleanup : constant Boolean :=
Nkind (N) = N_Block_Statement
and then Present (Cleanup_Actions (N));
Actions_Required : constant Boolean :=
Requires_Cleanup_Actions (N, True)
or else Is_Asynchronous_Call
or else Is_Master
or else Is_Protected_Subp_Body
or else Is_Task_Allocation
or else Is_Task_Body
or else Needs_Sec_Stack_Mark
or else Needs_Custom_Cleanup;
Loc : Source_Ptr;
Cln : List_Id;
-- Start of processing for Expand_Cleanup_Actions
begin
-- The current construct does not need any form of servicing
if not Actions_Required then
return;
end if;
-- If an extended return statement contains something like
--
-- X := F (...);
--
-- where F is a build-in-place function call returning a controlled
-- type, then a temporary object will be implicitly declared as part
-- of the statement list, and this will need cleanup. In such cases,
-- we transform:
--
-- return Result : T := ... do
-- <statements> -- possibly with handlers
-- end return;
--
-- into:
--
-- return Result : T := ... do
-- declare -- no declarations
-- begin
-- <statements> -- possibly with handlers
-- end; -- no handlers
-- end return;
--
-- So Expand_Cleanup_Actions will end up being called recursively on the
-- block statement.
if Nkind (N) = N_Extended_Return_Statement then
declare
Block : constant Node_Id :=
Make_Block_Statement (Sloc (N),
Declarations => Empty_List,
Handled_Statement_Sequence =>
Handled_Statement_Sequence (N));
begin
Set_Handled_Statement_Sequence (N,
Make_Handled_Sequence_Of_Statements (Sloc (N),
Statements => New_List (Block)));
Analyze (Block);
end;
-- Analysis of the block did all the work
return;
end if;
if Needs_Custom_Cleanup then
Cln := Cleanup_Actions (N);
else
Cln := No_List;
end if;
if No (Declarations (N)) then
Set_Declarations (N, New_List);
end if;
declare
Decls : constant List_Id := Declarations (N);
Fin_Id : Entity_Id;
Mark : Entity_Id := Empty;
begin
-- If we are generating expanded code for debugging purposes, use the
-- Sloc of the point of insertion for the cleanup code. The Sloc will
-- be updated subsequently to reference the proper line in .dg files.
-- If we are not debugging generated code, use No_Location instead,
-- so that no debug information is generated for the cleanup code.
-- This makes the behavior of the NEXT command in GDB monotonic, and
-- makes the placement of breakpoints more accurate.
if Debug_Generated_Code then
Loc := Sloc (Scop);
else
Loc := No_Location;
end if;
-- A task activation call has already been built for a task
-- allocation block.
if not Is_Task_Allocation then
Build_Task_Activation_Call (N);
end if;
if Is_Master then
Establish_Task_Master (N);
end if;
-- If secondary stack is in use, generate:
--
-- Mnn : constant Mark_Id := SS_Mark;
if Needs_Sec_Stack_Mark then
Set_Uses_Sec_Stack (Scop, False); -- avoid duplicate SS marks
Mark := Make_Temporary (Loc, 'M');
declare
Mark_Call : constant Node_Id := Build_SS_Mark_Call (Loc, Mark);
begin
Prepend_To (Decls, Mark_Call);
Analyze (Mark_Call);
end;
end if;
-- Generate finalization calls for all controlled objects appearing
-- in the statements of N. Add context specific cleanup for various
-- constructs.
Build_Finalizer
(N => N,
Clean_Stmts => Build_Cleanup_Statements (N, Cln),
Mark_Id => Mark,
Top_Decls => Decls,
Defer_Abort => Nkind (Original_Node (N)) = N_Task_Body
or else Is_Master,
Fin_Id => Fin_Id);
if Present (Fin_Id) then
Build_Finalizer_Call (N, Fin_Id);
end if;
end;
end Expand_Cleanup_Actions;
---------------------------
-- Expand_N_Package_Body --
---------------------------
-- Add call to Activate_Tasks if body is an activator (actual processing
-- is in chapter 9).
-- Generate subprogram descriptor for elaboration routine
-- Encode entity names in package body
procedure Expand_N_Package_Body (N : Node_Id) is
Id : constant Entity_Id := Defining_Entity (N);
Spec_Id : constant Entity_Id := Corresponding_Spec (N);
Fin_Id : Entity_Id;
begin
-- This is done only for non-generic packages
if Ekind (Spec_Id) = E_Package then
-- Build dispatch tables of library-level tagged types for bodies
-- that are not compilation units (see Analyze_Compilation_Unit),
-- except for instances because they have no N_Compilation_Unit.
if Tagged_Type_Expansion
and then Is_Library_Level_Entity (Spec_Id)
and then (not Is_Compilation_Unit (Spec_Id)
or else Is_Generic_Instance (Spec_Id))
then
Build_Static_Dispatch_Tables (N);
end if;
Push_Scope (Spec_Id);
Expand_CUDA_Package (N);
Build_Task_Activation_Call (N);
-- Verify the run-time semantics of pragma Initial_Condition at the
-- end of the body statements.
Expand_Pragma_Initial_Condition (Spec_Id, N);
-- If this is a library-level package and unnesting is enabled,
-- check for the presence of blocks with nested subprograms occurring
-- in elaboration code, and generate procedures to encapsulate the
-- blocks in case the nested subprograms make up-level references.
if Unnest_Subprogram_Mode
and then
Is_Library_Level_Entity (Current_Scope)
then
Check_Unnesting_Elaboration_Code (N);
Check_Unnesting_In_Decls_Or_Stmts (Declarations (N));
Check_Unnesting_In_Handlers (N);
end if;
Pop_Scope;
end if;
Set_Elaboration_Flag (N, Spec_Id);
Set_In_Package_Body (Spec_Id, False);
-- Set to encode entity names in package body before gigi is called
Qualify_Entity_Names (N);
if Ekind (Spec_Id) /= E_Generic_Package
and then not Delay_Cleanups (Id)
then
Build_Finalizer
(N => N,
Clean_Stmts => No_List,
Mark_Id => Empty,
Top_Decls => No_List,
Defer_Abort => False,
Fin_Id => Fin_Id);
if Present (Fin_Id) then
Set_Finalizer (Defining_Entity (N), Fin_Id);
end if;
end if;
end Expand_N_Package_Body;
----------------------------------
-- Expand_N_Package_Declaration --
----------------------------------
-- Add call to Activate_Tasks if there are tasks declared and the package
-- has no body. Note that in Ada 83 this may result in premature activation
-- of some tasks, given that we cannot tell whether a body will eventually
-- appear.
procedure Expand_N_Package_Declaration (N : Node_Id) is
Id : constant Entity_Id := Defining_Entity (N);
Spec : constant Node_Id := Specification (N);
Decls : List_Id;
Fin_Id : Entity_Id;
No_Body : Boolean := False;
-- True in the case of a package declaration that is a compilation
-- unit and for which no associated body will be compiled in this
-- compilation.
begin
-- Case of a package declaration other than a compilation unit
if Nkind (Parent (N)) /= N_Compilation_Unit then
null;
-- Case of a compilation unit that does not require a body
elsif not Body_Required (Parent (N))
and then not Unit_Requires_Body (Id)
then
No_Body := True;
-- Special case of generating calling stubs for a remote call interface
-- package: even though the package declaration requires one, the body
-- won't be processed in this compilation (so any stubs for RACWs
-- declared in the package must be generated here, along with the spec).
elsif Parent (N) = Cunit (Main_Unit)
and then Is_Remote_Call_Interface (Id)
and then Distribution_Stub_Mode = Generate_Caller_Stub_Body
then
No_Body := True;
end if;
-- For a nested instance, delay processing until freeze point
if Has_Delayed_Freeze (Id)
and then Nkind (Parent (N)) /= N_Compilation_Unit
then
return;
end if;
-- For a package declaration that implies no associated body, generate
-- task activation call and RACW supporting bodies now (since we won't
-- have a specific separate compilation unit for that).
if No_Body then
Push_Scope (Id);
-- Generate RACW subprogram bodies
if Has_RACW (Id) then
Decls := Private_Declarations (Spec);
if No (Decls) then
Decls := Visible_Declarations (Spec);
end if;
if No (Decls) then
Decls := New_List;
Set_Visible_Declarations (Spec, Decls);
end if;
Append_RACW_Bodies (Decls, Id);
Analyze_List (Decls);
end if;
-- Generate task activation call as last step of elaboration
if Present (Activation_Chain_Entity (N)) then
Build_Task_Activation_Call (N);
end if;
-- Verify the run-time semantics of pragma Initial_Condition at the
-- end of the private declarations when the package lacks a body.
Expand_Pragma_Initial_Condition (Id, N);
Pop_Scope;
end if;
-- Build dispatch tables of library-level tagged types for instances
-- that are not compilation units (see Analyze_Compilation_Unit).
if Tagged_Type_Expansion
and then Is_Library_Level_Entity (Id)
and then Is_Generic_Instance (Id)
and then not Is_Compilation_Unit (Id)
then
Build_Static_Dispatch_Tables (N);
end if;
-- Note: it is not necessary to worry about generating a subprogram
-- descriptor, since the only way to get exception handlers into a
-- package spec is to include instantiations, and that would cause
-- generation of subprogram descriptors to be delayed in any case.
-- Set to encode entity names in package spec before gigi is called
Qualify_Entity_Names (N);
if Ekind (Id) /= E_Generic_Package
and then not Delay_Cleanups (Id)
then
Build_Finalizer
(N => N,
Clean_Stmts => No_List,
Mark_Id => Empty,
Top_Decls => No_List,
Defer_Abort => False,
Fin_Id => Fin_Id);
if Present (Fin_Id) then
Set_Finalizer (Id, Fin_Id);
end if;
end if;
-- If this is a library-level package and unnesting is enabled,
-- check for the presence of blocks with nested subprograms occurring
-- in elaboration code, and generate procedures to encapsulate the
-- blocks in case the nested subprograms make up-level references.
if Unnest_Subprogram_Mode
and then Is_Library_Level_Entity (Current_Scope)
then
Check_Unnesting_In_Decls_Or_Stmts (Visible_Declarations (Spec));
Check_Unnesting_In_Decls_Or_Stmts (Private_Declarations (Spec));
end if;
end Expand_N_Package_Declaration;
---------------------------------
-- Has_Simple_Protected_Object --
---------------------------------
function Has_Simple_Protected_Object (T : Entity_Id) return Boolean is
begin
if Has_Task (T) then
return False;
elsif Is_Simple_Protected_Type (T) then
return True;
elsif Is_Array_Type (T) then
return Has_Simple_Protected_Object (Component_Type (T));
elsif Is_Record_Type (T) then
declare
Comp : Entity_Id;
begin
Comp := First_Component (T);
while Present (Comp) loop
if Has_Simple_Protected_Object (Etype (Comp)) then
return True;
end if;
Next_Component (Comp);
end loop;
return False;
end;
else
return False;
end if;
end Has_Simple_Protected_Object;
------------------------------------
-- Insert_Actions_In_Scope_Around --
------------------------------------
procedure Insert_Actions_In_Scope_Around
(N : Node_Id;
Clean : Boolean;
Manage_SS : Boolean)
is
Act_Before : constant List_Id :=
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Before);
Act_After : constant List_Id :=
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (After);
Act_Cleanup : constant List_Id :=
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup);
-- Note: We used to use renamings of Scope_Stack.Table (Scope_Stack.
-- Last), but this was incorrect as Process_Transients_In_Scope may
-- introduce new scopes and cause a reallocation of Scope_Stack.Table.
procedure Process_Transients_In_Scope
(First_Object : Node_Id;
Last_Object : Node_Id;
Related_Node : Node_Id);
-- Find all transient objects in the list First_Object .. Last_Object
-- and generate finalization actions for them. Related_Node denotes the
-- node which created all transient objects.
---------------------------------
-- Process_Transients_In_Scope --
---------------------------------
procedure Process_Transients_In_Scope
(First_Object : Node_Id;
Last_Object : Node_Id;
Related_Node : Node_Id)
is
Must_Hook : Boolean;
-- Flag denoting whether the context requires transient object
-- export to the outer finalizer.
function Is_Subprogram_Call (N : Node_Id) return Traverse_Result;
-- Return Abandon if arbitrary node denotes a subprogram call
function Has_Subprogram_Call is
new Traverse_Func (Is_Subprogram_Call);
procedure Process_Transient_In_Scope
(Obj_Decl : Node_Id;
Blk_Data : Finalization_Exception_Data;
Blk_Stmts : List_Id);
-- Generate finalization actions for a single transient object
-- denoted by object declaration Obj_Decl. Blk_Data is the
-- exception data of the enclosing block. Blk_Stmts denotes the
-- statements of the enclosing block.
------------------------
-- Is_Subprogram_Call --
------------------------
function Is_Subprogram_Call (N : Node_Id) return Traverse_Result is
begin
-- A regular procedure or function call
if Nkind (N) in N_Subprogram_Call then
return Abandon;
-- Special cases
-- Heavy expansion may relocate function calls outside the related
-- node. Inspect the original node to detect the initial placement
-- of the call.
elsif Is_Rewrite_Substitution (N) then
return Has_Subprogram_Call (Original_Node (N));
-- Generalized indexing always involves a function call
elsif Nkind (N) = N_Indexed_Component
and then Present (Generalized_Indexing (N))
then
return Abandon;
-- Keep searching
else
return OK;
end if;
end Is_Subprogram_Call;
--------------------------------
-- Process_Transient_In_Scope --
--------------------------------
procedure Process_Transient_In_Scope
(Obj_Decl : Node_Id;
Blk_Data : Finalization_Exception_Data;
Blk_Stmts : List_Id)
is
Loc : constant Source_Ptr := Sloc (Obj_Decl);
Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
Fin_Call : Node_Id;
Fin_Stmts : List_Id;
Hook_Assign : Node_Id;
Hook_Clear : Node_Id;
Hook_Decl : Node_Id;
Hook_Insert : Node_Id;
Ptr_Decl : Node_Id;
begin
-- Mark the transient object as successfully processed to avoid
-- double finalization.
Set_Is_Finalized_Transient (Obj_Id);
-- Construct all the pieces necessary to hook and finalize the
-- transient object.
Build_Transient_Object_Statements
(Obj_Decl => Obj_Decl,
Fin_Call => Fin_Call,
Hook_Assign => Hook_Assign,
Hook_Clear => Hook_Clear,
Hook_Decl => Hook_Decl,
Ptr_Decl => Ptr_Decl);
-- The context contains at least one subprogram call which may
-- raise an exception. This scenario employs "hooking" to pass
-- transient objects to the enclosing finalizer in case of an
-- exception.
if Must_Hook then
-- Add the access type which provides a reference to the
-- transient object. Generate:
-- type Ptr_Typ is access all Desig_Typ;
Insert_Action (Obj_Decl, Ptr_Decl);
-- Add the temporary which acts as a hook to the transient
-- object. Generate:
-- Hook : Ptr_Typ := null;
Insert_Action (Obj_Decl, Hook_Decl);
-- When the transient object is initialized by an aggregate,
-- the hook must capture the object after the last aggregate
-- assignment takes place. Only then is the object considered
-- fully initialized. Generate:
-- Hook := Ptr_Typ (Obj_Id);
-- <or>
-- Hook := Obj_Id'Unrestricted_Access;
-- Similarly if we have a build in place call: we must
-- initialize Hook only after the call has happened, otherwise
-- Obj_Id will not be initialized yet.
if Ekind (Obj_Id) in E_Constant | E_Variable then
if Present (Last_Aggregate_Assignment (Obj_Id)) then
Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
elsif Present (BIP_Initialization_Call (Obj_Id)) then
Hook_Insert := BIP_Initialization_Call (Obj_Id);
else
Hook_Insert := Obj_Decl;
end if;
-- Otherwise the hook seizes the related object immediately
else
Hook_Insert := Obj_Decl;
end if;
Insert_After_And_Analyze (Hook_Insert, Hook_Assign);
end if;
-- When exception propagation is enabled wrap the hook clear
-- statement and the finalization call into a block to catch
-- potential exceptions raised during finalization. Generate:
-- begin
-- [Hook := null;]
-- [Deep_]Finalize (Obj_Ref);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence
-- (Enn, Get_Current_Excep.all.all);
-- end if;
-- end;
if Exceptions_OK then
Fin_Stmts := New_List;
if Must_Hook then
Append_To (Fin_Stmts, Hook_Clear);
end if;
Append_To (Fin_Stmts, Fin_Call);
Prepend_To (Blk_Stmts,
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Fin_Stmts,
Exception_Handlers => New_List (
Build_Exception_Handler (Blk_Data)))));
-- Otherwise generate:
-- [Hook := null;]
-- [Deep_]Finalize (Obj_Ref);
-- Note that the statements are inserted in reverse order to
-- achieve the desired final order outlined above.
else
Prepend_To (Blk_Stmts, Fin_Call);
if Must_Hook then
Prepend_To (Blk_Stmts, Hook_Clear);
end if;
end if;
end Process_Transient_In_Scope;
-- Local variables
Built : Boolean := False;
Blk_Data : Finalization_Exception_Data;
Blk_Decl : Node_Id := Empty;
Blk_Decls : List_Id := No_List;
Blk_Ins : Node_Id;
Blk_Stmts : List_Id := No_List;
Loc : Source_Ptr := No_Location;
Obj_Decl : Node_Id;
-- Start of processing for Process_Transients_In_Scope
begin
-- The expansion performed by this routine is as follows:
-- type Ptr_Typ_1 is access all Ctrl_Trans_Obj_1_Typ;
-- Hook_1 : Ptr_Typ_1 := null;
-- Ctrl_Trans_Obj_1 : ...;
-- Hook_1 := Ctrl_Trans_Obj_1'Unrestricted_Access;
-- . . .
-- type Ptr_Typ_N is access all Ctrl_Trans_Obj_N_Typ;
-- Hook_N : Ptr_Typ_N := null;
-- Ctrl_Trans_Obj_N : ...;
-- Hook_N := Ctrl_Trans_Obj_N'Unrestricted_Access;
-- declare
-- Abrt : constant Boolean := ...;
-- Ex : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- Abort_Defer;
-- begin
-- Hook_N := null;
-- [Deep_]Finalize (Ctrl_Trans_Obj_N);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (Ex, Get_Current_Excep.all.all);
-- end;
-- . . .
-- begin
-- Hook_1 := null;
-- [Deep_]Finalize (Ctrl_Trans_Obj_1);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (Ex, Get_Current_Excep.all.all);
-- end;
-- Abort_Undefer;
-- if Raised and not Abrt then
-- Raise_From_Controlled_Operation (Ex);
-- end if;
-- end;
-- Recognize a scenario where the transient context is an object
-- declaration initialized by a build-in-place function call:
-- Obj : ... := BIP_Function_Call (Ctrl_Func_Call);
-- The rough expansion of the above is:
-- Temp : ... := Ctrl_Func_Call;
-- Obj : ...;
-- Res : ... := BIP_Func_Call (..., Obj, ...);
-- The finalization of any transient object must happen after the
-- build-in-place function call is executed.
if Nkind (N) = N_Object_Declaration
and then Present (BIP_Initialization_Call (Defining_Identifier (N)))
then
Must_Hook := True;
Blk_Ins := BIP_Initialization_Call (Defining_Identifier (N));
-- Search the context for at least one subprogram call. If found, the
-- machinery exports all transient objects to the enclosing finalizer
-- due to the possibility of abnormal call termination.
else
Must_Hook := Has_Subprogram_Call (N) = Abandon;
Blk_Ins := Last_Object;
end if;
Insert_List_After_And_Analyze (Blk_Ins, Act_Cleanup);
-- Examine all objects in the list First_Object .. Last_Object
Obj_Decl := First_Object;
while Present (Obj_Decl) loop
if Nkind (Obj_Decl) = N_Object_Declaration
and then Analyzed (Obj_Decl)
and then Is_Finalizable_Transient (Obj_Decl, N)
-- Do not process the node to be wrapped since it will be
-- handled by the enclosing finalizer.
and then Obj_Decl /= Related_Node
then
Loc := Sloc (Obj_Decl);
-- Before generating the cleanup code for the first transient
-- object, create a wrapper block which houses all hook clear
-- statements and finalization calls. This wrapper is needed by
-- the back end.
if not Built then
Built := True;
Blk_Stmts := New_List;
-- Generate:
-- Abrt : constant Boolean := ...;
-- Ex : Exception_Occurrence;
-- Raised : Boolean := False;
if Exceptions_OK then
Blk_Decls := New_List;
Build_Object_Declarations (Blk_Data, Blk_Decls, Loc);
end if;
Blk_Decl :=
Make_Block_Statement (Loc,
Declarations => Blk_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Blk_Stmts));
end if;
-- Construct all necessary circuitry to hook and finalize a
-- single transient object.
pragma Assert (Present (Blk_Stmts));
Process_Transient_In_Scope
(Obj_Decl => Obj_Decl,
Blk_Data => Blk_Data,
Blk_Stmts => Blk_Stmts);
end if;
-- Terminate the scan after the last object has been processed to
-- avoid touching unrelated code.
if Obj_Decl = Last_Object then
exit;
end if;
Next (Obj_Decl);
end loop;
-- Complete the decoration of the enclosing finalization block and
-- insert it into the tree.
if Present (Blk_Decl) then
pragma Assert (Present (Blk_Stmts));
pragma Assert (Loc /= No_Location);
-- Note that this Abort_Undefer does not require a extra block or
-- an AT_END handler because each finalization exception is caught
-- in its own corresponding finalization block. As a result, the
-- call to Abort_Defer always takes place.
if Abort_Allowed then
Prepend_To (Blk_Stmts,
Build_Runtime_Call (Loc, RE_Abort_Defer));
Append_To (Blk_Stmts,
Build_Runtime_Call (Loc, RE_Abort_Undefer));
end if;
-- Generate:
-- if Raised and then not Abrt then
-- Raise_From_Controlled_Operation (Ex);
-- end if;
if Exceptions_OK then
Append_To (Blk_Stmts, Build_Raise_Statement (Blk_Data));
end if;
Insert_After_And_Analyze (Blk_Ins, Blk_Decl);
end if;
end Process_Transients_In_Scope;
-- Local variables
Loc : constant Source_Ptr := Sloc (N);
Node_To_Wrap : constant Node_Id := Node_To_Be_Wrapped;
First_Obj : Node_Id;
Last_Obj : Node_Id;
Mark_Id : Entity_Id;
Target : Node_Id;
-- Start of processing for Insert_Actions_In_Scope_Around
begin
-- Nothing to do if the scope does not manage the secondary stack or
-- does not contain meaningful actions for insertion.
if not Manage_SS
and then No (Act_Before)
and then No (Act_After)
and then No (Act_Cleanup)
then
return;
end if;
-- If the node to be wrapped is the trigger of an asynchronous select,
-- it is not part of a statement list. The actions must be inserted
-- before the select itself, which is part of some list of statements.
-- Note that the triggering alternative includes the triggering
-- statement and an optional statement list. If the node to be
-- wrapped is part of that list, the normal insertion applies.
if Nkind (Parent (Node_To_Wrap)) = N_Triggering_Alternative
and then not Is_List_Member (Node_To_Wrap)
then
Target := Parent (Parent (Node_To_Wrap));
else
Target := N;
end if;
First_Obj := Target;
Last_Obj := Target;
-- Add all actions associated with a transient scope into the main tree.
-- There are several scenarios here:
-- +--- Before ----+ +----- After ---+
-- 1) First_Obj ....... Target ........ Last_Obj
-- 2) First_Obj ....... Target
-- 3) Target ........ Last_Obj
-- Flag declarations are inserted before the first object
if Present (Act_Before) then
First_Obj := First (Act_Before);
Insert_List_Before (Target, Act_Before);
end if;
-- Finalization calls are inserted after the last object
if Present (Act_After) then
Last_Obj := Last (Act_After);
Insert_List_After (Target, Act_After);
end if;
-- Mark and release the secondary stack when the context warrants it
if Manage_SS then
Mark_Id := Make_Temporary (Loc, 'M');
-- Generate:
-- Mnn : constant Mark_Id := SS_Mark;
Insert_Before_And_Analyze
(First_Obj, Build_SS_Mark_Call (Loc, Mark_Id));
-- Generate:
-- SS_Release (Mnn);
Insert_After_And_Analyze
(Last_Obj, Build_SS_Release_Call (Loc, Mark_Id));
end if;
-- If we are handling cleanups, check for transient objects associated
-- with Target and generate the required finalization actions for them.
if Clean then
Process_Transients_In_Scope
(First_Object => First_Obj,
Last_Object => Last_Obj,
Related_Node => Target);
end if;
-- Reset the action lists
Scope_Stack.Table
(Scope_Stack.Last).Actions_To_Be_Wrapped (Before) := No_List;
Scope_Stack.Table
(Scope_Stack.Last).Actions_To_Be_Wrapped (After) := No_List;
if Clean then
Scope_Stack.Table
(Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup) := No_List;
end if;
end Insert_Actions_In_Scope_Around;
------------------------------
-- Is_Simple_Protected_Type --
------------------------------
function Is_Simple_Protected_Type (T : Entity_Id) return Boolean is
begin
return
Is_Protected_Type (T)
and then not Uses_Lock_Free (T)
and then not Has_Entries (T)
and then Is_RTE (Find_Protection_Type (T), RE_Protection);
end Is_Simple_Protected_Type;
-----------------------
-- Make_Adjust_Call --
-----------------------
function Make_Adjust_Call
(Obj_Ref : Node_Id;
Typ : Entity_Id;
Skip_Self : Boolean := False) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Obj_Ref);
Adj_Id : Entity_Id := Empty;
Ref : Node_Id;
Utyp : Entity_Id;
begin
Ref := Obj_Ref;
-- Recover the proper type which contains Deep_Adjust
if Is_Class_Wide_Type (Typ) then
Utyp := Root_Type (Typ);
else
Utyp := Typ;
end if;
Utyp := Underlying_Type (Base_Type (Utyp));
Set_Assignment_OK (Ref);
-- Deal with untagged derivation of private views
if Present (Utyp) and then Is_Untagged_Derivation (Typ) then
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
Ref := Unchecked_Convert_To (Utyp, Ref);
Set_Assignment_OK (Ref);
end if;
-- When dealing with the completion of a private type, use the base
-- type instead.
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
pragma Assert (Is_Private_Type (Typ));
Utyp := Base_Type (Utyp);
Ref := Unchecked_Convert_To (Utyp, Ref);
end if;
-- The underlying type may not be present due to a missing full view. In
-- this case freezing did not take place and there is no [Deep_]Adjust
-- primitive to call.
if No (Utyp) then
return Empty;
elsif Skip_Self then
if Has_Controlled_Component (Utyp) then
if Is_Tagged_Type (Utyp) then
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
else
Adj_Id := TSS (Utyp, TSS_Deep_Adjust);
end if;
end if;
-- Class-wide types, interfaces and types with controlled components
elsif Is_Class_Wide_Type (Typ)
or else Is_Interface (Typ)
or else Has_Controlled_Component (Utyp)
then
if Is_Tagged_Type (Utyp) then
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
else
Adj_Id := TSS (Utyp, TSS_Deep_Adjust);
end if;
-- Derivations from [Limited_]Controlled
elsif Is_Controlled (Utyp) then
Adj_Id := Find_Optional_Prim_Op (Utyp, Name_Of (Adjust_Case));
-- Tagged types
elsif Is_Tagged_Type (Utyp) then
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
else
raise Program_Error;
end if;
if Present (Adj_Id) then
-- If the object is unanalyzed, set its expected type for use in
-- Convert_View in case an additional conversion is needed.
if No (Etype (Ref))
and then Nkind (Ref) /= N_Unchecked_Type_Conversion
then
Set_Etype (Ref, Typ);
end if;
-- The object reference may need another conversion depending on the
-- type of the formal and that of the actual.
if not Is_Class_Wide_Type (Typ) then
Ref := Convert_View (Adj_Id, Ref);
end if;
return
Make_Call (Loc,
Proc_Id => Adj_Id,
Param => Ref,
Skip_Self => Skip_Self);
else
return Empty;
end if;
end Make_Adjust_Call;
---------------
-- Make_Call --
---------------
function Make_Call
(Loc : Source_Ptr;
Proc_Id : Entity_Id;
Param : Node_Id;
Skip_Self : Boolean := False) return Node_Id
is
Params : constant List_Id := New_List (Param);
begin
-- Do not apply the controlled action to the object itself by signaling
-- the related routine to avoid self.
if Skip_Self then
Append_To (Params, New_Occurrence_Of (Standard_False, Loc));
end if;
return
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Proc_Id, Loc),
Parameter_Associations => Params);
end Make_Call;
--------------------------
-- Make_Deep_Array_Body --
--------------------------
function Make_Deep_Array_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id
is
function Build_Adjust_Or_Finalize_Statements
(Typ : Entity_Id) return List_Id;
-- Create the statements necessary to adjust or finalize an array of
-- controlled elements. Generate:
--
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
--
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
--
-- begin
-- for J1 in [reverse] Typ'First (1) .. Typ'Last (1) loop
-- ^-- in the finalization case
-- ...
-- for Jn in [reverse] Typ'First (n) .. Typ'Last (n) loop
-- begin
-- [Deep_]Adjust / Finalize (V (J1, ..., Jn));
--
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- end loop;
-- ...
-- end loop;
--
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
function Build_Initialize_Statements (Typ : Entity_Id) return List_Id;
-- Create the statements necessary to initialize an array of controlled
-- elements. Include a mechanism to carry out partial finalization if an
-- exception occurs. Generate:
--
-- declare
-- Counter : Integer := 0;
--
-- begin
-- for J1 in V'Range (1) loop
-- ...
-- for JN in V'Range (N) loop
-- begin
-- [Deep_]Initialize (V (J1, ..., JN));
--
-- Counter := Counter + 1;
--
-- exception
-- when others =>
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- Counter :=
-- V'Length (1) *
-- V'Length (2) *
-- ...
-- V'Length (N) - Counter;
-- for F1 in reverse V'Range (1) loop
-- ...
-- for FN in reverse V'Range (N) loop
-- if Counter > 0 then
-- Counter := Counter - 1;
-- else
-- begin
-- [Deep_]Finalize (V (F1, ..., FN));
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
-- end loop;
-- ...
-- end loop;
-- end;
--
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
--
-- raise;
-- end;
-- end loop;
-- end loop;
-- end;
function New_References_To
(L : List_Id;
Loc : Source_Ptr) return List_Id;
-- Given a list of defining identifiers, return a list of references to
-- the original identifiers, in the same order as they appear.
-----------------------------------------
-- Build_Adjust_Or_Finalize_Statements --
-----------------------------------------
function Build_Adjust_Or_Finalize_Statements
(Typ : Entity_Id) return List_Id
is
Comp_Typ : constant Entity_Id := Component_Type (Typ);
Index_List : constant List_Id := New_List;
Loc : constant Source_Ptr := Sloc (Typ);
Num_Dims : constant Int := Number_Dimensions (Typ);
procedure Build_Indexes;
-- Generate the indexes used in the dimension loops
-------------------
-- Build_Indexes --
-------------------
procedure Build_Indexes is
begin
-- Generate the following identifiers:
-- Jnn - for initialization
for Dim in 1 .. Num_Dims loop
Append_To (Index_List,
Make_Defining_Identifier (Loc, New_External_Name ('J', Dim)));
end loop;
end Build_Indexes;
-- Local variables
Final_Decls : List_Id := No_List;
Final_Data : Finalization_Exception_Data;
Block : Node_Id;
Call : Node_Id;
Comp_Ref : Node_Id;
Core_Loop : Node_Id;
Dim : Int;
J : Entity_Id;
Loop_Id : Entity_Id;
Stmts : List_Id;
-- Start of processing for Build_Adjust_Or_Finalize_Statements
begin
Final_Decls := New_List;
Build_Indexes;
Build_Object_Declarations (Final_Data, Final_Decls, Loc);
Comp_Ref :=
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => New_References_To (Index_List, Loc));
Set_Etype (Comp_Ref, Comp_Typ);
-- Generate:
-- [Deep_]Adjust (V (J1, ..., JN))
if Prim = Adjust_Case then
Call := Make_Adjust_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
-- Generate:
-- [Deep_]Finalize (V (J1, ..., JN))
else pragma Assert (Prim = Finalize_Case);
Call := Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
end if;
if Present (Call) then
-- Generate the block which houses the adjust or finalize call:
-- begin
-- <adjust or finalize call>
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
if Exceptions_OK then
Core_Loop :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Call),
Exception_Handlers => New_List (
Build_Exception_Handler (Final_Data))));
else
Core_Loop := Call;
end if;
-- Generate the dimension loops starting from the innermost one
-- for Jnn in [reverse] V'Range (Dim) loop
-- <core loop>
-- end loop;
J := Last (Index_List);
Dim := Num_Dims;
while Present (J) and then Dim > 0 loop
Loop_Id := J;
Prev (J);
Remove (Loop_Id);
Core_Loop :=
Make_Loop_Statement (Loc,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Loop_Id,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))),
Reverse_Present =>
Prim = Finalize_Case)),
Statements => New_List (Core_Loop),
End_Label => Empty);
Dim := Dim - 1;
end loop;
-- Generate the block which contains the core loop, declarations
-- of the abort flag, the exception occurrence, the raised flag
-- and the conditional raise:
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- <core loop>
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
Stmts := New_List (Core_Loop);
if Exceptions_OK then
Append_To (Stmts, Build_Raise_Statement (Final_Data));
end if;
Block :=
Make_Block_Statement (Loc,
Declarations => Final_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts));
-- Otherwise previous errors or a missing full view may prevent the
-- proper freezing of the component type. If this is the case, there
-- is no [Deep_]Adjust or [Deep_]Finalize primitive to call.
else
Block := Make_Null_Statement (Loc);
end if;
return New_List (Block);
end Build_Adjust_Or_Finalize_Statements;
---------------------------------
-- Build_Initialize_Statements --
---------------------------------
function Build_Initialize_Statements (Typ : Entity_Id) return List_Id is
Comp_Typ : constant Entity_Id := Component_Type (Typ);
Final_List : constant List_Id := New_List;
Index_List : constant List_Id := New_List;
Loc : constant Source_Ptr := Sloc (Typ);
Num_Dims : constant Int := Number_Dimensions (Typ);
function Build_Assignment (Counter_Id : Entity_Id) return Node_Id;
-- Generate the following assignment:
-- Counter := V'Length (1) *
-- ...
-- V'Length (N) - Counter;
--
-- Counter_Id denotes the entity of the counter.
function Build_Finalization_Call return Node_Id;
-- Generate a deep finalization call for an array element
procedure Build_Indexes;
-- Generate the initialization and finalization indexes used in the
-- dimension loops.
function Build_Initialization_Call return Node_Id;
-- Generate a deep initialization call for an array element
----------------------
-- Build_Assignment --
----------------------
function Build_Assignment (Counter_Id : Entity_Id) return Node_Id is
Dim : Int;
Expr : Node_Id;
begin
-- Start from the first dimension and generate:
-- V'Length (1)
Dim := 1;
Expr :=
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Length,
Expressions => New_List (Make_Integer_Literal (Loc, Dim)));
-- Process the rest of the dimensions, generate:
-- Expr * V'Length (N)
Dim := Dim + 1;
while Dim <= Num_Dims loop
Expr :=
Make_Op_Multiply (Loc,
Left_Opnd => Expr,
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Length,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))));
Dim := Dim + 1;
end loop;
-- Generate:
-- Counter := Expr - Counter;
return
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Counter_Id, Loc),
Expression =>
Make_Op_Subtract (Loc,
Left_Opnd => Expr,
Right_Opnd => New_Occurrence_Of (Counter_Id, Loc)));
end Build_Assignment;
-----------------------------
-- Build_Finalization_Call --
-----------------------------
function Build_Finalization_Call return Node_Id is
Comp_Ref : constant Node_Id :=
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => New_References_To (Final_List, Loc));
begin
Set_Etype (Comp_Ref, Comp_Typ);
-- Generate:
-- [Deep_]Finalize (V);
return Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
end Build_Finalization_Call;
-------------------
-- Build_Indexes --
-------------------
procedure Build_Indexes is
begin
-- Generate the following identifiers:
-- Jnn - for initialization
-- Fnn - for finalization
for Dim in 1 .. Num_Dims loop
Append_To (Index_List,
Make_Defining_Identifier (Loc, New_External_Name ('J', Dim)));
Append_To (Final_List,
Make_Defining_Identifier (Loc, New_External_Name ('F', Dim)));
end loop;
end Build_Indexes;
-------------------------------
-- Build_Initialization_Call --
-------------------------------
function Build_Initialization_Call return Node_Id is
Comp_Ref : constant Node_Id :=
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => New_References_To (Index_List, Loc));
begin
Set_Etype (Comp_Ref, Comp_Typ);
-- Generate:
-- [Deep_]Initialize (V (J1, ..., JN));
return Make_Init_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
end Build_Initialization_Call;
-- Local variables
Counter_Id : Entity_Id;
Dim : Int;
F : Node_Id;
Fin_Stmt : Node_Id;
Final_Block : Node_Id;
Final_Data : Finalization_Exception_Data;
Final_Decls : List_Id := No_List;
Final_Loop : Node_Id;
Init_Block : Node_Id;
Init_Call : Node_Id;
Init_Loop : Node_Id;
J : Node_Id;
Loop_Id : Node_Id;
Stmts : List_Id;
-- Start of processing for Build_Initialize_Statements
begin
Counter_Id := Make_Temporary (Loc, 'C');
Final_Decls := New_List;
Build_Indexes;
Build_Object_Declarations (Final_Data, Final_Decls, Loc);
-- Generate the block which houses the finalization call, the index
-- guard and the handler which triggers Program_Error later on.
-- if Counter > 0 then
-- Counter := Counter - 1;
-- else
-- begin
-- [Deep_]Finalize (V (F1, ..., FN));
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
Fin_Stmt := Build_Finalization_Call;
if Present (Fin_Stmt) then
if Exceptions_OK then
Fin_Stmt :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Stmt),
Exception_Handlers => New_List (
Build_Exception_Handler (Final_Data))));
end if;
-- This is the core of the loop, the dimension iterators are added
-- one by one in reverse.
Final_Loop :=
Make_If_Statement (Loc,
Condition =>
Make_Op_Gt (Loc,
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
Right_Opnd => Make_Integer_Literal (Loc, 0)),
Then_Statements => New_List (
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Counter_Id, Loc),
Expression =>
Make_Op_Subtract (Loc,
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
Right_Opnd => Make_Integer_Literal (Loc, 1)))),
Else_Statements => New_List (Fin_Stmt));
-- Generate all finalization loops starting from the innermost
-- dimension.
-- for Fnn in reverse V'Range (Dim) loop
-- <final loop>
-- end loop;
F := Last (Final_List);
Dim := Num_Dims;
while Present (F) and then Dim > 0 loop
Loop_Id := F;
Prev (F);
Remove (Loop_Id);
Final_Loop :=
Make_Loop_Statement (Loc,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Loop_Id,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))),
Reverse_Present => True)),
Statements => New_List (Final_Loop),
End_Label => Empty);
Dim := Dim - 1;
end loop;
-- Generate the block which contains the finalization loops, the
-- declarations of the abort flag, the exception occurrence, the
-- raised flag and the conditional raise.
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- Counter :=
-- V'Length (1) *
-- ...
-- V'Length (N) - Counter;
-- <final loop>
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- raise;
-- end;
Stmts := New_List (Build_Assignment (Counter_Id), Final_Loop);
if Exceptions_OK then
Append_To (Stmts, Build_Raise_Statement (Final_Data));
Append_To (Stmts, Make_Raise_Statement (Loc));
end if;
Final_Block :=
Make_Block_Statement (Loc,
Declarations => Final_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts));
-- Otherwise previous errors or a missing full view may prevent the
-- proper freezing of the component type. If this is the case, there
-- is no [Deep_]Finalize primitive to call.
else
Final_Block := Make_Null_Statement (Loc);
end if;
-- Generate the block which contains the initialization call and
-- the partial finalization code.
-- begin
-- [Deep_]Initialize (V (J1, ..., JN));
-- Counter := Counter + 1;
-- exception
-- when others =>
-- <finalization code>
-- end;
Init_Call := Build_Initialization_Call;
-- Only create finalization block if there is a nontrivial call
-- to initialization or a Default_Initial_Condition check to be
-- performed.
if (Present (Init_Call)
and then Nkind (Init_Call) /= N_Null_Statement)
or else
(Has_DIC (Comp_Typ)
and then not GNATprove_Mode
and then Present (DIC_Procedure (Comp_Typ))
and then not Has_Null_Body (DIC_Procedure (Comp_Typ)))
then
declare
Init_Stmts : constant List_Id := New_List;
begin
if Present (Init_Call) then
Append_To (Init_Stmts, Init_Call);
end if;
if Has_DIC (Comp_Typ)
and then Present (DIC_Procedure (Comp_Typ))
then
Append_To
(Init_Stmts,
Build_DIC_Call (Loc,
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => New_References_To (Index_List, Loc)),
Comp_Typ));
end if;
Init_Loop :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Init_Stmts,
Exception_Handlers => New_List (
Make_Exception_Handler (Loc,
Exception_Choices => New_List (
Make_Others_Choice (Loc)),
Statements => New_List (Final_Block)))));
end;
Append_To (Statements (Handled_Statement_Sequence (Init_Loop)),
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Counter_Id, Loc),
Expression =>
Make_Op_Add (Loc,
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
Right_Opnd => Make_Integer_Literal (Loc, 1))));
-- Generate all initialization loops starting from the innermost
-- dimension.
-- for Jnn in V'Range (Dim) loop
-- <init loop>
-- end loop;
J := Last (Index_List);
Dim := Num_Dims;
while Present (J) and then Dim > 0 loop
Loop_Id := J;
Prev (J);
Remove (Loop_Id);
Init_Loop :=
Make_Loop_Statement (Loc,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Loop_Id,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))))),
Statements => New_List (Init_Loop),
End_Label => Empty);
Dim := Dim - 1;
end loop;
-- Generate the block which contains the counter variable and the
-- initialization loops.
-- declare
-- Counter : Integer := 0;
-- begin
-- <init loop>
-- end;
Init_Block :=
Make_Block_Statement (Loc,
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Counter_Id,
Object_Definition =>
New_Occurrence_Of (Standard_Integer, Loc),
Expression => Make_Integer_Literal (Loc, 0))),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Init_Loop)));
if Debug_Generated_Code then
Set_Debug_Info_Needed (Counter_Id);
end if;
-- Otherwise previous errors or a missing full view may prevent the
-- proper freezing of the component type. If this is the case, there
-- is no [Deep_]Initialize primitive to call.
else
Init_Block := Make_Null_Statement (Loc);
end if;
return New_List (Init_Block);
end Build_Initialize_Statements;
-----------------------
-- New_References_To --
-----------------------
function New_References_To
(L : List_Id;
Loc : Source_Ptr) return List_Id
is
Refs : constant List_Id := New_List;
Id : Node_Id;
begin
Id := First (L);
while Present (Id) loop
Append_To (Refs, New_Occurrence_Of (Id, Loc));
Next (Id);
end loop;
return Refs;
end New_References_To;
-- Start of processing for Make_Deep_Array_Body
begin
case Prim is
when Address_Case =>
return Make_Finalize_Address_Stmts (Typ);
when Adjust_Case
| Finalize_Case
=>
return Build_Adjust_Or_Finalize_Statements (Typ);
when Initialize_Case =>
return Build_Initialize_Statements (Typ);
end case;
end Make_Deep_Array_Body;
--------------------
-- Make_Deep_Proc --
--------------------
function Make_Deep_Proc
(Prim : Final_Primitives;
Typ : Entity_Id;
Stmts : List_Id) return Entity_Id
is
Loc : constant Source_Ptr := Sloc (Typ);
Formals : List_Id;
Proc_Id : Entity_Id;
begin
-- Create the object formal, generate:
-- V : System.Address
if Prim = Address_Case then
Formals := New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Parameter_Type =>
New_Occurrence_Of (RTE (RE_Address), Loc)));
-- Default case
else
-- V : in out Typ
Formals := New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
In_Present => True,
Out_Present => True,
Parameter_Type => New_Occurrence_Of (Typ, Loc)));
-- F : Boolean := True
if Prim = Adjust_Case
or else Prim = Finalize_Case
then
Append_To (Formals,
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
Parameter_Type =>
New_Occurrence_Of (Standard_Boolean, Loc),
Expression =>
New_Occurrence_Of (Standard_True, Loc)));
end if;
end if;
Proc_Id :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name (Typ, Deep_Name_Of (Prim)));
-- Generate:
-- procedure Deep_Initialize / Adjust / Finalize (V : in out <typ>) is
-- begin
-- <stmts>
-- exception -- Finalize and Adjust cases only
-- raise Program_Error;
-- end Deep_Initialize / Adjust / Finalize;
-- or
-- procedure Finalize_Address (V : System.Address) is
-- begin
-- <stmts>
-- end Finalize_Address;
Discard_Node (
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Proc_Id,
Parameter_Specifications => Formals),
Declarations => Empty_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
-- If there are no calls to component initialization, indicate that
-- the procedure is trivial, so prevent calls to it.
if Is_Empty_List (Stmts)
or else Nkind (First (Stmts)) = N_Null_Statement
then
Set_Is_Trivial_Subprogram (Proc_Id);
end if;
return Proc_Id;
end Make_Deep_Proc;
---------------------------
-- Make_Deep_Record_Body --
---------------------------
function Make_Deep_Record_Body
(Prim : Final_Primitives;
Typ : Entity_Id;
Is_Local : Boolean := False) return List_Id
is
function Build_Adjust_Statements (Typ : Entity_Id) return List_Id;
-- Build the statements necessary to adjust a record type. The type may
-- have discriminants and contain variant parts. Generate:
--
-- begin
-- begin
-- [Deep_]Adjust (V.Comp_1);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- . . .
-- begin
-- [Deep_]Adjust (V.Comp_N);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
--
-- begin
-- Deep_Adjust (V._parent, False); -- If applicable
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
--
-- if F then
-- begin
-- Adjust (V); -- If applicable
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
--
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
function Build_Finalize_Statements (Typ : Entity_Id) return List_Id;
-- Build the statements necessary to finalize a record type. The type
-- may have discriminants and contain variant parts. Generate:
--
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
--
-- begin
-- if F then
-- begin
-- Finalize (V); -- If applicable
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
--
-- case Variant_1 is
-- when Value_1 =>
-- case State_Counter_N => -- If Is_Local is enabled
-- when N => .
-- goto LN; .
-- ... .
-- when 1 => .
-- goto L1; .
-- when others => .
-- goto L0; .
-- end case; .
--
-- <<LN>> -- If Is_Local is enabled
-- begin
-- [Deep_]Finalize (V.Comp_N);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- . . .
-- <<L1>>
-- begin
-- [Deep_]Finalize (V.Comp_1);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
-- <<L0>>
-- end case;
--
-- case State_Counter_1 => -- If Is_Local is enabled
-- when M => .
-- goto LM; .
-- ...
--
-- begin
-- Deep_Finalize (V._parent, False); -- If applicable
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
--
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
function Parent_Field_Type (Typ : Entity_Id) return Entity_Id;
-- Given a derived tagged type Typ, traverse all components, find field
-- _parent and return its type.
procedure Preprocess_Components
(Comps : Node_Id;
Num_Comps : out Nat;
Has_POC : out Boolean);
-- Examine all components in component list Comps, count all controlled
-- components and determine whether at least one of them is per-object
-- constrained. Component _parent is always skipped.
-----------------------------
-- Build_Adjust_Statements --
-----------------------------
function Build_Adjust_Statements (Typ : Entity_Id) return List_Id is
Loc : constant Source_Ptr := Sloc (Typ);
Typ_Def : constant Node_Id := Type_Definition (Parent (Typ));
Finalizer_Data : Finalization_Exception_Data;
function Process_Component_List_For_Adjust
(Comps : Node_Id) return List_Id;
-- Build all necessary adjust statements for a single component list
---------------------------------------
-- Process_Component_List_For_Adjust --
---------------------------------------
function Process_Component_List_For_Adjust
(Comps : Node_Id) return List_Id
is
Stmts : constant List_Id := New_List;
procedure Process_Component_For_Adjust (Decl : Node_Id);
-- Process the declaration of a single controlled component
----------------------------------
-- Process_Component_For_Adjust --
----------------------------------
procedure Process_Component_For_Adjust (Decl : Node_Id) is
Id : constant Entity_Id := Defining_Identifier (Decl);
Typ : constant Entity_Id := Etype (Id);
Adj_Call : Node_Id;
begin
-- begin
-- [Deep_]Adjust (V.Id);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E, Get_Current_Excep.all.all);
-- end if;
-- end;
Adj_Call :=
Make_Adjust_Call (
Obj_Ref =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name => Make_Identifier (Loc, Chars (Id))),
Typ => Typ);
-- Guard against a missing [Deep_]Adjust when the component
-- type was not properly frozen.
if Present (Adj_Call) then
if Exceptions_OK then
Adj_Call :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Adj_Call),
Exception_Handlers => New_List (
Build_Exception_Handler (Finalizer_Data))));
end if;
Append_To (Stmts, Adj_Call);
end if;
end Process_Component_For_Adjust;
-- Local variables
Decl : Node_Id;
Decl_Id : Entity_Id;
Decl_Typ : Entity_Id;
Has_POC : Boolean;
Num_Comps : Nat;
Var_Case : Node_Id;
-- Start of processing for Process_Component_List_For_Adjust
begin
-- Perform an initial check, determine the number of controlled
-- components in the current list and whether at least one of them
-- is per-object constrained.
Preprocess_Components (Comps, Num_Comps, Has_POC);
-- The processing in this routine is done in the following order:
-- 1) Regular components
-- 2) Per-object constrained components
-- 3) Variant parts
if Num_Comps > 0 then
-- Process all regular components in order of declarations
Decl := First_Non_Pragma (Component_Items (Comps));
while Present (Decl) loop
Decl_Id := Defining_Identifier (Decl);
Decl_Typ := Etype (Decl_Id);
-- Skip _parent as well as per-object constrained components
if Chars (Decl_Id) /= Name_uParent
and then Needs_Finalization (Decl_Typ)
then
if Has_Access_Constraint (Decl_Id)
and then No (Expression (Decl))
then
null;
else
Process_Component_For_Adjust (Decl);
end if;
end if;
Next_Non_Pragma (Decl);
end loop;
-- Process all per-object constrained components in order of
-- declarations.
if Has_POC then
Decl := First_Non_Pragma (Component_Items (Comps));
while Present (Decl) loop
Decl_Id := Defining_Identifier (Decl);
Decl_Typ := Etype (Decl_Id);
-- Skip _parent
if Chars (Decl_Id) /= Name_uParent
and then Needs_Finalization (Decl_Typ)
and then Has_Access_Constraint (Decl_Id)
and then No (Expression (Decl))
then
Process_Component_For_Adjust (Decl);
end if;
Next_Non_Pragma (Decl);
end loop;
end if;
end if;
-- Process all variants, if any
Var_Case := Empty;
if Present (Variant_Part (Comps)) then
declare
Var_Alts : constant List_Id := New_List;
Var : Node_Id;
begin
Var := First_Non_Pragma (Variants (Variant_Part (Comps)));
while Present (Var) loop
-- Generate:
-- when <discrete choices> =>
-- <adjust statements>
Append_To (Var_Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices =>
New_Copy_List (Discrete_Choices (Var)),
Statements =>
Process_Component_List_For_Adjust (
Component_List (Var))));
Next_Non_Pragma (Var);
end loop;
-- Generate:
-- case V.<discriminant> is
-- when <discrete choices 1> =>
-- <adjust statements 1>
-- ...
-- when <discrete choices N> =>
-- <adjust statements N>
-- end case;
Var_Case :=
Make_Case_Statement (Loc,
Expression =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name =>
Make_Identifier (Loc,
Chars => Chars (Name (Variant_Part (Comps))))),
Alternatives => Var_Alts);
end;
end if;
-- Add the variant case statement to the list of statements
if Present (Var_Case) then
Append_To (Stmts, Var_Case);
end if;
-- If the component list did not have any controlled components
-- nor variants, return null.
if Is_Empty_List (Stmts) then
Append_To (Stmts, Make_Null_Statement (Loc));
end if;
return Stmts;
end Process_Component_List_For_Adjust;
-- Local variables
Bod_Stmts : List_Id := No_List;
Finalizer_Decls : List_Id := No_List;
Rec_Def : Node_Id;
-- Start of processing for Build_Adjust_Statements
begin
Finalizer_Decls := New_List;
Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc);
if Nkind (Typ_Def) = N_Derived_Type_Definition then
Rec_Def := Record_Extension_Part (Typ_Def);
else
Rec_Def := Typ_Def;
end if;
-- Create an adjust sequence for all record components
if Present (Component_List (Rec_Def)) then
Bod_Stmts :=
Process_Component_List_For_Adjust (Component_List (Rec_Def));
end if;
-- A derived record type must adjust all inherited components. This
-- action poses the following problem:
-- procedure Deep_Adjust (Obj : in out Parent_Typ) is
-- begin
-- Adjust (Obj);
-- ...
-- procedure Deep_Adjust (Obj : in out Derived_Typ) is
-- begin
-- Deep_Adjust (Obj._parent);
-- ...
-- Adjust (Obj);
-- ...
-- Adjusting the derived type will invoke Adjust of the parent and
-- then that of the derived type. This is undesirable because both
-- routines may modify shared components. Only the Adjust of the
-- derived type should be invoked.
-- To prevent this double adjustment of shared components,
-- Deep_Adjust uses a flag to control the invocation of Adjust:
-- procedure Deep_Adjust
-- (Obj : in out Some_Type;
-- Flag : Boolean := True)
-- is
-- begin
-- if Flag then
-- Adjust (Obj);
-- end if;
-- ...
-- When Deep_Adjust is invoked for field _parent, a value of False is
-- provided for the flag:
-- Deep_Adjust (Obj._parent, False);
if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
declare
Par_Typ : constant Entity_Id := Parent_Field_Type (Typ);
Adj_Stmt : Node_Id;
Call : Node_Id;
begin
if Needs_Finalization (Par_Typ) then
Call :=
Make_Adjust_Call
(Obj_Ref =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name =>
Make_Identifier (Loc, Name_uParent)),
Typ => Par_Typ,
Skip_Self => True);
-- Generate:
-- begin
-- Deep_Adjust (V._parent, False);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
if Present (Call) then
Adj_Stmt := Call;
if Exceptions_OK then
Adj_Stmt :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Adj_Stmt),
Exception_Handlers => New_List (
Build_Exception_Handler (Finalizer_Data))));
end if;
Prepend_To (Bod_Stmts, Adj_Stmt);
end if;
end if;
end;
end if;
-- Adjust the object. This action must be performed last after all
-- components have been adjusted.
if Is_Controlled (Typ) then
declare
Adj_Stmt : Node_Id;
Proc : Entity_Id;
begin
Proc := Find_Optional_Prim_Op (Typ, Name_Adjust);
-- Generate:
-- if F then
-- begin
-- Adjust (V);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
if Present (Proc) then
Adj_Stmt :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Proc, Loc),
Parameter_Associations => New_List (
Make_Identifier (Loc, Name_V)));
if Exceptions_OK then
Adj_Stmt :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Adj_Stmt),
Exception_Handlers => New_List (
Build_Exception_Handler
(Finalizer_Data))));
end if;
Append_To (Bod_Stmts,
Make_If_Statement (Loc,
Condition => Make_Identifier (Loc, Name_F),
Then_Statements => New_List (Adj_Stmt)));
end if;
end;
end if;
-- At this point either all adjustment statements have been generated
-- or the type is not controlled.
if Is_Empty_List (Bod_Stmts) then
Append_To (Bod_Stmts, Make_Null_Statement (Loc));
return Bod_Stmts;
-- Generate:
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- <adjust statements>
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
else
if Exceptions_OK then
Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data));
end if;
return
New_List (
Make_Block_Statement (Loc,
Declarations =>
Finalizer_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts)));
end if;
end Build_Adjust_Statements;
-------------------------------
-- Build_Finalize_Statements --
-------------------------------
function Build_Finalize_Statements (Typ : Entity_Id) return List_Id is
Loc : constant Source_Ptr := Sloc (Typ);
Typ_Def : constant Node_Id := Type_Definition (Parent (Typ));
Counter : Nat := 0;
Finalizer_Data : Finalization_Exception_Data;
Last_POC_Call : Node_Id := Empty;
function Process_Component_List_For_Finalize
(Comps : Node_Id;
In_Variant_Part : Boolean := False) return List_Id;
-- Build all necessary finalization statements for a single component
-- list. The statements may include a jump circuitry if flag Is_Local
-- is enabled. In_Variant_Part indicates whether this is a recursive
-- call.
-----------------------------------------
-- Process_Component_List_For_Finalize --
-----------------------------------------
function Process_Component_List_For_Finalize
(Comps : Node_Id;
In_Variant_Part : Boolean := False) return List_Id
is
procedure Process_Component_For_Finalize
(Decl : Node_Id;
Alts : List_Id;
Decls : List_Id;
Stmts : List_Id;
Num_Comps : in out Nat);
-- Process the declaration of a single controlled component. If
-- flag Is_Local is enabled, create the corresponding label and
-- jump circuitry. Alts is the list of case alternatives, Decls
-- is the top level declaration list where labels are declared
-- and Stmts is the list of finalization actions. Num_Comps
-- denotes the current number of components needing finalization.
------------------------------------
-- Process_Component_For_Finalize --
------------------------------------
procedure Process_Component_For_Finalize
(Decl : Node_Id;
Alts : List_Id;
Decls : List_Id;
Stmts : List_Id;
Num_Comps : in out Nat)
is
Id : constant Entity_Id := Defining_Identifier (Decl);
Typ : constant Entity_Id := Etype (Id);
Fin_Call : Node_Id;
begin
if Is_Local then
declare
Label : Node_Id;
Label_Id : Entity_Id;
begin
-- Generate:
-- LN : label;
Label_Id :=
Make_Identifier (Loc,
Chars => New_External_Name ('L', Num_Comps));
Set_Entity (Label_Id,
Make_Defining_Identifier (Loc, Chars (Label_Id)));
Label := Make_Label (Loc, Label_Id);
Append_To (Decls,
Make_Implicit_Label_Declaration (Loc,
Defining_Identifier => Entity (Label_Id),
Label_Construct => Label));
-- Generate:
-- when N =>
-- goto LN;
Append_To (Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => New_List (
Make_Integer_Literal (Loc, Num_Comps)),
Statements => New_List (
Make_Goto_Statement (Loc,
Name =>
New_Occurrence_Of (Entity (Label_Id), Loc)))));
-- Generate:
-- <<LN>>
Append_To (Stmts, Label);
-- Decrease the number of components to be processed.
-- This action yields a new Label_Id in future calls.
Num_Comps := Num_Comps - 1;
end;
end if;
-- Generate:
-- [Deep_]Finalize (V.Id); -- No_Exception_Propagation
-- begin -- Exception handlers allowed
-- [Deep_]Finalize (V.Id);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
Fin_Call :=
Make_Final_Call
(Obj_Ref =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name => Make_Identifier (Loc, Chars (Id))),
Typ => Typ);
-- Guard against a missing [Deep_]Finalize when the component
-- type was not properly frozen.
if Present (Fin_Call) then
if Exceptions_OK then
Fin_Call :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Call),
Exception_Handlers => New_List (
Build_Exception_Handler (Finalizer_Data))));
end if;
Append_To (Stmts, Fin_Call);
end if;
end Process_Component_For_Finalize;
-- Local variables
Alts : List_Id;
Counter_Id : Entity_Id := Empty;
Decl : Node_Id;
Decl_Id : Entity_Id;
Decl_Typ : Entity_Id;
Decls : List_Id;
Has_POC : Boolean;
Jump_Block : Node_Id;
Label : Node_Id;
Label_Id : Entity_Id;
Num_Comps : Nat;
Stmts : List_Id;
Var_Case : Node_Id;
-- Start of processing for Process_Component_List_For_Finalize
begin
-- Perform an initial check, look for controlled and per-object
-- constrained components.
Preprocess_Components (Comps, Num_Comps, Has_POC);
-- Create a state counter to service the current component list.
-- This step is performed before the variants are inspected in
-- order to generate the same state counter names as those from
-- Build_Initialize_Statements.
if Num_Comps > 0 and then Is_Local then
Counter := Counter + 1;
Counter_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name ('C', Counter));
end if;
-- Process the component in the following order:
-- 1) Variants
-- 2) Per-object constrained components
-- 3) Regular components
-- Start with the variant parts
Var_Case := Empty;
if Present (Variant_Part (Comps)) then
declare
Var_Alts : constant List_Id := New_List;
Var : Node_Id;
begin
Var := First_Non_Pragma (Variants (Variant_Part (Comps)));
while Present (Var) loop
-- Generate:
-- when <discrete choices> =>
-- <finalize statements>
Append_To (Var_Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices =>
New_Copy_List (Discrete_Choices (Var)),
Statements =>
Process_Component_List_For_Finalize (
Component_List (Var),
In_Variant_Part => True)));
Next_Non_Pragma (Var);
end loop;
-- Generate:
-- case V.<discriminant> is
-- when <discrete choices 1> =>
-- <finalize statements 1>
-- ...
-- when <discrete choices N> =>
-- <finalize statements N>
-- end case;
Var_Case :=
Make_Case_Statement (Loc,
Expression =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name =>
Make_Identifier (Loc,
Chars => Chars (Name (Variant_Part (Comps))))),
Alternatives => Var_Alts);
end;
end if;
-- The current component list does not have a single controlled
-- component, however it may contain variants. Return the case
-- statement for the variants or nothing.
if Num_Comps = 0 then
if Present (Var_Case) then
return New_List (Var_Case);
else
return New_List (Make_Null_Statement (Loc));
end if;
end if;
-- Prepare all lists
Alts := New_List;
Decls := New_List;
Stmts := New_List;
-- Process all per-object constrained components in reverse order
if Has_POC then
Decl := Last_Non_Pragma (Component_Items (Comps));
while Present (Decl) loop
Decl_Id := Defining_Identifier (Decl);
Decl_Typ := Etype (Decl_Id);
-- Skip _parent
if Chars (Decl_Id) /= Name_uParent
and then Needs_Finalization (Decl_Typ)
and then Has_Access_Constraint (Decl_Id)
and then No (Expression (Decl))
then
Process_Component_For_Finalize
(Decl, Alts, Decls, Stmts, Num_Comps);
end if;
Prev_Non_Pragma (Decl);
end loop;
end if;
if not In_Variant_Part then
Last_POC_Call := Last (Stmts);
-- In the case of a type extension, the deep-finalize call
-- for the _Parent component will be inserted here.
end if;
-- Process the rest of the components in reverse order
Decl := Last_Non_Pragma (Component_Items (Comps));
while Present (Decl) loop
Decl_Id := Defining_Identifier (Decl);
Decl_Typ := Etype (Decl_Id);
-- Skip _parent
if Chars (Decl_Id) /= Name_uParent
and then Needs_Finalization (Decl_Typ)
then
-- Skip per-object constrained components since they were
-- handled in the above step.
if Has_Access_Constraint (Decl_Id)
and then No (Expression (Decl))
then
null;
else
Process_Component_For_Finalize
(Decl, Alts, Decls, Stmts, Num_Comps);
end if;
end if;
Prev_Non_Pragma (Decl);
end loop;
-- Generate:
-- declare
-- LN : label; -- If Is_Local is enabled
-- ... .
-- L0 : label; .
-- begin .
-- case CounterX is .
-- when N => .
-- goto LN; .
-- ... .
-- when 1 => .
-- goto L1; .
-- when others => .
-- goto L0; .
-- end case; .
-- <<LN>> -- If Is_Local is enabled
-- begin
-- [Deep_]Finalize (V.CompY);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
-- ...
-- <<L0>> -- If Is_Local is enabled
-- end;
if Is_Local then
-- Add the declaration of default jump location L0, its
-- corresponding alternative and its place in the statements.
Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
Set_Entity (Label_Id,
Make_Defining_Identifier (Loc, Chars (Label_Id)));
Label := Make_Label (Loc, Label_Id);
Append_To (Decls, -- declaration
Make_Implicit_Label_Declaration (Loc,
Defining_Identifier => Entity (Label_Id),
Label_Construct => Label));
Append_To (Alts, -- alternative
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => New_List (
Make_Others_Choice (Loc)),
Statements => New_List (
Make_Goto_Statement (Loc,
Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
Append_To (Stmts, Label); -- statement
-- Create the jump block
Prepend_To (Stmts,
Make_Case_Statement (Loc,
Expression => Make_Identifier (Loc, Chars (Counter_Id)),
Alternatives => Alts));
end if;
Jump_Block :=
Make_Block_Statement (Loc,
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Stmts));
if Present (Var_Case) then
return New_List (Var_Case, Jump_Block);
else
return New_List (Jump_Block);
end if;
end Process_Component_List_For_Finalize;
-- Local variables
Bod_Stmts : List_Id := No_List;
Finalizer_Decls : List_Id := No_List;
Rec_Def : Node_Id;
-- Start of processing for Build_Finalize_Statements
begin
Finalizer_Decls := New_List;
Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc);
if Nkind (Typ_Def) = N_Derived_Type_Definition then
Rec_Def := Record_Extension_Part (Typ_Def);
else
Rec_Def := Typ_Def;
end if;
-- Create a finalization sequence for all record components
if Present (Component_List (Rec_Def)) then
Bod_Stmts :=
Process_Component_List_For_Finalize (Component_List (Rec_Def));
end if;
-- A derived record type must finalize all inherited components. This
-- action poses the following problem:
-- procedure Deep_Finalize (Obj : in out Parent_Typ) is
-- begin
-- Finalize (Obj);
-- ...
-- procedure Deep_Finalize (Obj : in out Derived_Typ) is
-- begin
-- Deep_Finalize (Obj._parent);
-- ...
-- Finalize (Obj);
-- ...
-- Finalizing the derived type will invoke Finalize of the parent and
-- then that of the derived type. This is undesirable because both
-- routines may modify shared components. Only the Finalize of the
-- derived type should be invoked.
-- To prevent this double adjustment of shared components,
-- Deep_Finalize uses a flag to control the invocation of Finalize:
-- procedure Deep_Finalize
-- (Obj : in out Some_Type;
-- Flag : Boolean := True)
-- is
-- begin
-- if Flag then
-- Finalize (Obj);
-- end if;
-- ...
-- When Deep_Finalize is invoked for field _parent, a value of False
-- is provided for the flag:
-- Deep_Finalize (Obj._parent, False);
if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
declare
Par_Typ : constant Entity_Id := Parent_Field_Type (Typ);
Call : Node_Id;
Fin_Stmt : Node_Id;
begin
if Needs_Finalization (Par_Typ) then
Call :=
Make_Final_Call
(Obj_Ref =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name =>
Make_Identifier (Loc, Name_uParent)),
Typ => Par_Typ,
Skip_Self => True);
-- Generate:
-- begin
-- Deep_Finalize (V._parent, False);
-- exception
-- when Id : others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
if Present (Call) then
Fin_Stmt := Call;
if Exceptions_OK then
Fin_Stmt :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Stmt),
Exception_Handlers => New_List (
Build_Exception_Handler
(Finalizer_Data))));
end if;
-- The intended component finalization order is
-- 1) POC components of extension
-- 2) _Parent component
-- 3) non-POC components of extension.
--
-- With this "finalize the parent part in the middle"
-- ordering, we can avoid the need for making two
-- calls to the parent's subprogram in the way that
-- is necessary for Init_Procs. This does have the
-- peculiar (but legal) consequence that the parent's
-- non-POC components are finalized before the
-- non-POC extension components. This violates the
-- usual "finalize in reverse declaration order"
-- principle, but that's ok (see Ada RM 7.6.1(9)).
--
-- Last_POC_Call should be non-empty if the extension
-- has at least one POC. Interactions with variant
-- parts are incorrectly ignored.
if Present (Last_POC_Call) then
Insert_After (Last_POC_Call, Fin_Stmt);
else
-- At this point, we could look for the common case
-- where there are no POC components anywhere in
-- sight (inherited or not) and, in that common case,
-- call Append_To instead of Prepend_To. That would
-- result in finalizing the parent part after, rather
-- than before, the extension components. That might
-- be more intuitive (as discussed in preceding
-- comment), but it is not required.
Prepend_To (Bod_Stmts, Fin_Stmt);
end if;
end if;
end if;
end;
end if;
-- Finalize the object. This action must be performed first before
-- all components have been finalized.
if Is_Controlled (Typ) and then not Is_Local then
declare
Fin_Stmt : Node_Id;
Proc : Entity_Id;
begin
Proc := Find_Optional_Prim_Op (Typ, Name_Finalize);
-- Generate:
-- if F then
-- begin
-- Finalize (V);
-- exception
-- when others =>
-- if not Raised then
-- Raised := True;
-- Save_Occurrence (E,
-- Get_Current_Excep.all.all);
-- end if;
-- end;
-- end if;
if Present (Proc) then
Fin_Stmt :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Proc, Loc),
Parameter_Associations => New_List (
Make_Identifier (Loc, Name_V)));
if Exceptions_OK then
Fin_Stmt :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Stmt),
Exception_Handlers => New_List (
Build_Exception_Handler
(Finalizer_Data))));
end if;
Prepend_To (Bod_Stmts,
Make_If_Statement (Loc,
Condition => Make_Identifier (Loc, Name_F),
Then_Statements => New_List (Fin_Stmt)));
end if;
end;
end if;
-- At this point either all finalization statements have been
-- generated or the type is not controlled.
if No (Bod_Stmts) then
return New_List (Make_Null_Statement (Loc));
-- Generate:
-- declare
-- Abort : constant Boolean := Triggered_By_Abort;
-- <or>
-- Abort : constant Boolean := False; -- no abort
-- E : Exception_Occurrence;
-- Raised : Boolean := False;
-- begin
-- <finalize statements>
-- if Raised and then not Abort then
-- Raise_From_Controlled_Operation (E);
-- end if;
-- end;
else
if Exceptions_OK then
Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data));
end if;
return
New_List (
Make_Block_Statement (Loc,
Declarations =>
Finalizer_Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts)));
end if;
end Build_Finalize_Statements;
-----------------------
-- Parent_Field_Type --
-----------------------
function Parent_Field_Type (Typ : Entity_Id) return Entity_Id is
Field : Entity_Id;
begin
Field := First_Entity (Typ);
while Present (Field) loop
if Chars (Field) = Name_uParent then
return Etype (Field);
end if;
Next_Entity (Field);
end loop;
-- A derived tagged type should always have a parent field
raise Program_Error;
end Parent_Field_Type;
---------------------------
-- Preprocess_Components --
---------------------------
procedure Preprocess_Components
(Comps : Node_Id;
Num_Comps : out Nat;
Has_POC : out Boolean)
is
Decl : Node_Id;
Id : Entity_Id;
Typ : Entity_Id;
begin
Num_Comps := 0;
Has_POC := False;
Decl := First_Non_Pragma (Component_Items (Comps));
while Present (Decl) loop
Id := Defining_Identifier (Decl);
Typ := Etype (Id);
-- Skip field _parent
if Chars (Id) /= Name_uParent
and then Needs_Finalization (Typ)
then
Num_Comps := Num_Comps + 1;
if Has_Access_Constraint (Id)
and then No (Expression (Decl))
then
Has_POC := True;
end if;
end if;
Next_Non_Pragma (Decl);
end loop;
end Preprocess_Components;
-- Start of processing for Make_Deep_Record_Body
begin
case Prim is
when Address_Case =>
return Make_Finalize_Address_Stmts (Typ);
when Adjust_Case =>
return Build_Adjust_Statements (Typ);
when Finalize_Case =>
return Build_Finalize_Statements (Typ);
when Initialize_Case =>
declare
Loc : constant Source_Ptr := Sloc (Typ);
begin
if Is_Controlled (Typ) then
return New_List (
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of
(Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
Parameter_Associations => New_List (
Make_Identifier (Loc, Name_V))));
else
return Empty_List;
end if;
end;
end case;
end Make_Deep_Record_Body;
----------------------
-- Make_Final_Call --
----------------------
function Make_Final_Call
(Obj_Ref : Node_Id;
Typ : Entity_Id;
Skip_Self : Boolean := False) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Obj_Ref);
Atyp : Entity_Id;
Prot_Typ : Entity_Id := Empty;
Fin_Id : Entity_Id := Empty;
Ref : Node_Id;
Utyp : Entity_Id;
begin
Ref := Obj_Ref;
-- Recover the proper type which contains [Deep_]Finalize
if Is_Class_Wide_Type (Typ) then
Utyp := Root_Type (Typ);
Atyp := Utyp;
elsif Is_Concurrent_Type (Typ) then
Utyp := Corresponding_Record_Type (Typ);
Atyp := Empty;
Ref := Convert_Concurrent (Ref, Typ);
elsif Is_Private_Type (Typ)
and then Present (Underlying_Type (Typ))
and then Is_Concurrent_Type (Underlying_Type (Typ))
then
Utyp := Corresponding_Record_Type (Underlying_Type (Typ));
Atyp := Typ;
Ref := Convert_Concurrent (Ref, Underlying_Type (Typ));
else
Utyp := Typ;
Atyp := Typ;
end if;
Utyp := Underlying_Type (Base_Type (Utyp));
Set_Assignment_OK (Ref);
-- Deal with untagged derivation of private views. If the parent type
-- is a protected type, Deep_Finalize is found on the corresponding
-- record of the ancestor.
if Is_Untagged_Derivation (Typ) then
if Is_Protected_Type (Typ) then
Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
else
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
if Is_Protected_Type (Utyp) then
Utyp := Corresponding_Record_Type (Utyp);
end if;
end if;
Ref := Unchecked_Convert_To (Utyp, Ref);
Set_Assignment_OK (Ref);
end if;
-- Deal with derived private types which do not inherit primitives from
-- their parents. In this case, [Deep_]Finalize can be found in the full
-- view of the parent type.
if Present (Utyp)
and then Is_Tagged_Type (Utyp)
and then Is_Derived_Type (Utyp)
and then Is_Empty_Elmt_List (Primitive_Operations (Utyp))
and then Is_Private_Type (Etype (Utyp))
and then Present (Full_View (Etype (Utyp)))
then
Utyp := Full_View (Etype (Utyp));
Ref := Unchecked_Convert_To (Utyp, Ref);
Set_Assignment_OK (Ref);
end if;
-- When dealing with the completion of a private type, use the base type
-- instead.
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
pragma Assert (Present (Atyp) and then Is_Private_Type (Atyp));
Utyp := Base_Type (Utyp);
Ref := Unchecked_Convert_To (Utyp, Ref);
Set_Assignment_OK (Ref);
end if;
-- Detect if Typ is a protected type or an expanded protected type and
-- store the relevant type within Prot_Typ for later processing.
if Is_Protected_Type (Typ) then
Prot_Typ := Typ;
elsif Ekind (Typ) = E_Record_Type
and then Present (Corresponding_Concurrent_Type (Typ))
and then Is_Protected_Type (Corresponding_Concurrent_Type (Typ))
then
Prot_Typ := Corresponding_Concurrent_Type (Typ);
end if;
-- The underlying type may not be present due to a missing full view. In
-- this case freezing did not take place and there is no [Deep_]Finalize
-- primitive to call.
if No (Utyp) then
return Empty;
elsif Skip_Self then
if Has_Controlled_Component (Utyp) then
if Is_Tagged_Type (Utyp) then
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
else
Fin_Id := TSS (Utyp, TSS_Deep_Finalize);
end if;
end if;
-- Class-wide types, interfaces and types with controlled components
elsif Is_Class_Wide_Type (Typ)
or else Is_Interface (Typ)
or else Has_Controlled_Component (Utyp)
then
if Is_Tagged_Type (Utyp) then
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
else
Fin_Id := TSS (Utyp, TSS_Deep_Finalize);
end if;
-- Derivations from [Limited_]Controlled
elsif Is_Controlled (Utyp) then
Fin_Id := Find_Optional_Prim_Op (Utyp, Name_Of (Finalize_Case));
-- Tagged types
elsif Is_Tagged_Type (Utyp) then
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
-- Protected types: these also require finalization even though they
-- are not marked controlled explicitly.
elsif Present (Prot_Typ) then
-- Protected objects do not need to be finalized on restricted
-- runtimes.
if Restricted_Profile then
return Empty;
-- ??? Only handle the simple case for now. Will not support a record
-- or array containing protected objects.
elsif Is_Simple_Protected_Type (Prot_Typ) then
Fin_Id := RTE (RE_Finalize_Protection);
else
raise Program_Error;
end if;
else
raise Program_Error;
end if;
if Present (Fin_Id) then
-- When finalizing a class-wide object, do not convert to the root
-- type in order to produce a dispatching call.
if Is_Class_Wide_Type (Typ) then
null;
-- Ensure that a finalization routine is at least decorated in order
-- to inspect the object parameter.
elsif Analyzed (Fin_Id)
or else Ekind (Fin_Id) = E_Procedure
then
-- In certain cases, such as the creation of Stream_Read, the
-- visible entity of the type is its full view. Since Stream_Read
-- will have to create an object of type Typ, the local object
-- will be finalzed by the scope finalizer generated later on. The
-- object parameter of Deep_Finalize will always use the private
-- view of the type. To avoid such a clash between a private and a
-- full view, perform an unchecked conversion of the object
-- reference to the private view.
declare
Formal_Typ : constant Entity_Id :=
Etype (First_Formal (Fin_Id));
begin
if Is_Private_Type (Formal_Typ)
and then Present (Full_View (Formal_Typ))
and then Full_View (Formal_Typ) = Utyp
then
Ref := Unchecked_Convert_To (Formal_Typ, Ref);
end if;
end;
-- If the object is unanalyzed, set its expected type for use in
-- Convert_View in case an additional conversion is needed.
if No (Etype (Ref))
and then Nkind (Ref) /= N_Unchecked_Type_Conversion
then
Set_Etype (Ref, Typ);
end if;
Ref := Convert_View (Fin_Id, Ref);
end if;
return
Make_Call (Loc,
Proc_Id => Fin_Id,
Param => Ref,
Skip_Self => Skip_Self);
else
pragma Assert (Serious_Errors_Detected > 0
or else not Has_Controlled_Component (Utyp));
return Empty;
end if;
end Make_Final_Call;
--------------------------------
-- Make_Finalize_Address_Body --
--------------------------------
procedure Make_Finalize_Address_Body (Typ : Entity_Id) is
Is_Task : constant Boolean :=
Ekind (Typ) = E_Record_Type
and then Is_Concurrent_Record_Type (Typ)
and then Ekind (Corresponding_Concurrent_Type (Typ)) =
E_Task_Type;
Loc : constant Source_Ptr := Sloc (Typ);
Proc_Id : Entity_Id;
Stmts : List_Id;
begin
-- The corresponding records of task types are not controlled by design.
-- For the sake of completeness, create an empty Finalize_Address to be
-- used in task class-wide allocations.
if Is_Task then
null;
-- Nothing to do if the type is not controlled or it already has a
-- TSS entry for Finalize_Address. Skip class-wide subtypes which do not
-- come from source. These are usually generated for completeness and
-- do not need the Finalize_Address primitive.
elsif not Needs_Finalization (Typ)
or else Present (TSS (Typ, TSS_Finalize_Address))
or else
(Is_Class_Wide_Type (Typ)
and then Ekind (Root_Type (Typ)) = E_Record_Subtype
and then not Comes_From_Source (Root_Type (Typ)))
then
return;
end if;
-- Do not generate Finalize_Address routine for CodePeer
if CodePeer_Mode then
return;
end if;
Proc_Id :=
Make_Defining_Identifier (Loc,
Make_TSS_Name (Typ, TSS_Finalize_Address));
-- Generate:
-- procedure <Typ>FD (V : System.Address) is
-- begin
-- null; -- for tasks
-- declare -- for all other types
-- type Pnn is access all Typ;
-- for Pnn'Storage_Size use 0;
-- begin
-- [Deep_]Finalize (Pnn (V).all);
-- end;
-- end TypFD;
if Is_Task then
Stmts := New_List (Make_Null_Statement (Loc));
else
Stmts := Make_Finalize_Address_Stmts (Typ);
end if;
Discard_Node (
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Proc_Id,
Parameter_Specifications => New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier =>
Make_Defining_Identifier (Loc, Name_V),
Parameter_Type =>
New_Occurrence_Of (RTE (RE_Address), Loc)))),
Declarations => No_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts)));
Set_TSS (Typ, Proc_Id);
end Make_Finalize_Address_Body;
---------------------------------
-- Make_Finalize_Address_Stmts --
---------------------------------
function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id is
Loc : constant Source_Ptr := Sloc (Typ);
Decls : List_Id;
Desig_Typ : Entity_Id;
Fin_Block : Node_Id;
Fin_Call : Node_Id;
Obj_Expr : Node_Id;
Ptr_Typ : Entity_Id;
begin
if Is_Array_Type (Typ) then
if Is_Constrained (First_Subtype (Typ)) then
Desig_Typ := First_Subtype (Typ);
else
Desig_Typ := Base_Type (Typ);
end if;
-- Class-wide types of constrained root types
elsif Is_Class_Wide_Type (Typ)
and then Has_Discriminants (Root_Type (Typ))
and then not
Is_Empty_Elmt_List (Discriminant_Constraint (Root_Type (Typ)))
then
declare
Parent_Typ : Entity_Id;
Parent_Utyp : Entity_Id;
begin
-- Climb the parent type chain looking for a non-constrained type
Parent_Typ := Root_Type (Typ);
while Parent_Typ /= Etype (Parent_Typ)
and then Has_Discriminants (Parent_Typ)
and then not
Is_Empty_Elmt_List (Discriminant_Constraint (Parent_Typ))
loop
Parent_Typ := Etype (Parent_Typ);
end loop;
-- Handle views created for tagged types with unknown
-- discriminants.
if Is_Underlying_Record_View (Parent_Typ) then
Parent_Typ := Underlying_Record_View (Parent_Typ);
end if;
Parent_Utyp := Underlying_Type (Parent_Typ);
-- Handle views created for a synchronized private extension with
-- known, non-defaulted discriminants. In that case, parent_typ
-- will be the private extension, as it is the first "non
-- -constrained" type in the parent chain. Unfortunately, the
-- underlying type, being a protected or task type, is not the
-- "real" type needing finalization. Rather, the "corresponding
-- record type" should be the designated type here. In fact, TSS
-- finalizer generation is specifically skipped for the nominal
-- class-wide type of (the full view of) a concurrent type (see
-- exp_ch7.Expand_Freeze_Class_Wide_Type). If we don't designate
-- the underlying record (Tprot_typeVC), we will end up trying to
-- dispatch to prot_typeVDF from an incorrectly designated
-- Tprot_typeC, which is, of course, not actually a member of
-- prot_typeV'Class, and thus incompatible.
if Ekind (Parent_Utyp) in Concurrent_Kind
and then Present (Corresponding_Record_Type (Parent_Utyp))
then
Parent_Utyp := Corresponding_Record_Type (Parent_Utyp);
end if;
Desig_Typ := Class_Wide_Type (Parent_Utyp);
end;
-- General case
else
Desig_Typ := Typ;
end if;
-- Generate:
-- type Ptr_Typ is access all Typ;
-- for Ptr_Typ'Storage_Size use 0;
Ptr_Typ := Make_Temporary (Loc, 'P');
Decls := New_List (
Make_Full_Type_Declaration (Loc,
Defining_Identifier => Ptr_Typ,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
All_Present => True,
Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))),
Make_Attribute_Definition_Clause (Loc,
Name => New_Occurrence_Of (Ptr_Typ, Loc),
Chars => Name_Storage_Size,
Expression => Make_Integer_Literal (Loc, 0)));
Obj_Expr := Make_Identifier (Loc, Name_V);
-- Unconstrained arrays require special processing in order to retrieve
-- the elements. To achieve this, we have to skip the dope vector which
-- lays in front of the elements and then use a thin pointer to perform
-- the address-to-access conversion.
if Is_Array_Type (Typ)
and then not Is_Constrained (First_Subtype (Typ))
then
declare
Dope_Id : Entity_Id;
begin
-- Ensure that Ptr_Typ is a thin pointer; generate:
-- for Ptr_Typ'Size use System.Address'Size;
Append_To (Decls,
Make_Attribute_Definition_Clause (Loc,
Name => New_Occurrence_Of (Ptr_Typ, Loc),
Chars => Name_Size,
Expression =>
Make_Integer_Literal (Loc, System_Address_Size)));
-- Generate:
-- Dnn : constant Storage_Offset :=
-- Desig_Typ'Descriptor_Size / Storage_Unit;
Dope_Id := Make_Temporary (Loc, 'D');
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Dope_Id,
Constant_Present => True,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
Expression =>
Make_Op_Divide (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Desig_Typ, Loc),
Attribute_Name => Name_Descriptor_Size),
Right_Opnd =>
Make_Integer_Literal (Loc, System_Storage_Unit))));
-- Shift the address from the start of the dope vector to the
-- start of the elements:
--
-- V + Dnn
--
-- Note that this is done through a wrapper routine since RTSfind
-- cannot retrieve operations with string names of the form "+".
Obj_Expr :=
Make_Function_Call (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Add_Offset_To_Address), Loc),
Parameter_Associations => New_List (
Obj_Expr,
New_Occurrence_Of (Dope_Id, Loc)));
end;
end if;
Fin_Call :=
Make_Final_Call (
Obj_Ref =>
Make_Explicit_Dereference (Loc,
Prefix => Unchecked_Convert_To (Ptr_Typ, Obj_Expr)),
Typ => Desig_Typ);
if Present (Fin_Call) then
Fin_Block :=
Make_Block_Statement (Loc,
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Fin_Call)));
-- Otherwise previous errors or a missing full view may prevent the
-- proper freezing of the designated type. If this is the case, there
-- is no [Deep_]Finalize primitive to call.
else
Fin_Block := Make_Null_Statement (Loc);
end if;
return New_List (Fin_Block);
end Make_Finalize_Address_Stmts;
-------------------------------------
-- Make_Handler_For_Ctrl_Operation --
-------------------------------------
-- Generate:
-- when E : others =>
-- Raise_From_Controlled_Operation (E);
-- or:
-- when others =>
-- raise Program_Error [finalize raised exception];
-- depending on whether Raise_From_Controlled_Operation is available
function Make_Handler_For_Ctrl_Operation
(Loc : Source_Ptr) return Node_Id
is
E_Occ : Entity_Id;
-- Choice parameter (for the first case above)
Raise_Node : Node_Id;
-- Procedure call or raise statement
begin
-- Standard run-time: add choice parameter E and pass it to
-- Raise_From_Controlled_Operation so that the original exception
-- name and message can be recorded in the exception message for
-- Program_Error.
if RTE_Available (RE_Raise_From_Controlled_Operation) then
E_Occ := Make_Defining_Identifier (Loc, Name_E);
Raise_Node :=
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Raise_From_Controlled_Operation), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (E_Occ, Loc)));
-- Restricted run-time: exception messages are not supported
else
E_Occ := Empty;
Raise_Node :=
Make_Raise_Program_Error (Loc,
Reason => PE_Finalize_Raised_Exception);
end if;
return
Make_Implicit_Exception_Handler (Loc,
Exception_Choices => New_List (Make_Others_Choice (Loc)),
Choice_Parameter => E_Occ,
Statements => New_List (Raise_Node));
end Make_Handler_For_Ctrl_Operation;
--------------------
-- Make_Init_Call --
--------------------
function Make_Init_Call
(Obj_Ref : Node_Id;
Typ : Entity_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Obj_Ref);
Is_Conc : Boolean;
Proc : Entity_Id;
Ref : Node_Id;
Utyp : Entity_Id;
begin
Ref := Obj_Ref;
-- Deal with the type and object reference. Depending on the context, an
-- object reference may need several conversions.
if Is_Concurrent_Type (Typ) then
Is_Conc := True;
Utyp := Corresponding_Record_Type (Typ);
Ref := Convert_Concurrent (Ref, Typ);
elsif Is_Private_Type (Typ)
and then Present (Full_View (Typ))
and then Is_Concurrent_Type (Underlying_Type (Typ))
then
Is_Conc := True;
Utyp := Corresponding_Record_Type (Underlying_Type (Typ));
Ref := Convert_Concurrent (Ref, Underlying_Type (Typ));
else
Is_Conc := False;
Utyp := Typ;
end if;
Utyp := Underlying_Type (Base_Type (Utyp));
Set_Assignment_OK (Ref);
-- Deal with untagged derivation of private views
if Is_Untagged_Derivation (Typ) and then not Is_Conc then
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
Ref := Unchecked_Convert_To (Utyp, Ref);
-- The following is to prevent problems with UC see 1.156 RH ???
Set_Assignment_OK (Ref);
end if;
-- If the underlying_type is a subtype, then we are dealing with the
-- completion of a private type. We need to access the base type and
-- generate a conversion to it.
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
pragma Assert (Is_Private_Type (Typ));
Utyp := Base_Type (Utyp);
Ref := Unchecked_Convert_To (Utyp, Ref);
end if;
-- The underlying type may not be present due to a missing full view.
-- In this case freezing did not take place and there is no suitable
-- [Deep_]Initialize primitive to call.
-- If Typ is protected then no additional processing is needed either.
if No (Utyp)
or else Is_Protected_Type (Typ)
then
return Empty;
end if;
-- Select the appropriate version of initialize
if Has_Controlled_Component (Utyp) then
Proc := TSS (Utyp, Deep_Name_Of (Initialize_Case));
else
Proc := Find_Prim_Op (Utyp, Name_Of (Initialize_Case));
Check_Visibly_Controlled (Initialize_Case, Typ, Proc, Ref);
end if;
-- If initialization procedure for an array of controlled objects is
-- trivial, do not generate a useless call to it.
-- The initialization procedure may be missing altogether in the case
-- of a derived container whose components have trivial initialization.
if No (Proc)
or else (Is_Array_Type (Utyp) and then Is_Trivial_Subprogram (Proc))
or else
(not Comes_From_Source (Proc)
and then Present (Alias (Proc))
and then Is_Trivial_Subprogram (Alias (Proc)))
then
return Empty;
end if;
-- The object reference may need another conversion depending on the
-- type of the formal and that of the actual.
Ref := Convert_View (Proc, Ref);
-- Generate:
-- [Deep_]Initialize (Ref);
return
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Proc, Loc),
Parameter_Associations => New_List (Ref));
end Make_Init_Call;
------------------------------
-- Make_Local_Deep_Finalize --
------------------------------
function Make_Local_Deep_Finalize
(Typ : Entity_Id;
Nam : Entity_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Typ);
Formals : List_Id;
begin
Formals := New_List (
-- V : in out Typ
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
In_Present => True,
Out_Present => True,
Parameter_Type => New_Occurrence_Of (Typ, Loc)),
-- F : Boolean := True
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
Expression => New_Occurrence_Of (Standard_True, Loc)));
-- Add the necessary number of counters to represent the initialization
-- state of an object.
return
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Nam,
Parameter_Specifications => Formals),
Declarations => No_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Make_Deep_Record_Body (Finalize_Case, Typ, True)));
end Make_Local_Deep_Finalize;
------------------------------------
-- Make_Set_Finalize_Address_Call --
------------------------------------
function Make_Set_Finalize_Address_Call
(Loc : Source_Ptr;
Ptr_Typ : Entity_Id) return Node_Id
is
-- It is possible for Ptr_Typ to be a partial view, if the access type
-- is a full view declared in the private part of a nested package, and
-- the finalization actions take place when completing analysis of the
-- enclosing unit. For this reason use Underlying_Type twice below.
Desig_Typ : constant Entity_Id :=
Available_View
(Designated_Type (Underlying_Type (Ptr_Typ)));
Fin_Addr : constant Entity_Id := Finalize_Address (Desig_Typ);
Fin_Mas : constant Entity_Id :=
Finalization_Master (Underlying_Type (Ptr_Typ));
begin
-- Both the finalization master and primitive Finalize_Address must be
-- available.
pragma Assert (Present (Fin_Addr) and Present (Fin_Mas));
-- Generate:
-- Set_Finalize_Address
-- (<Ptr_Typ>FM, <Desig_Typ>FD'Unrestricted_Access);
return
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Set_Finalize_Address), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Fin_Mas, Loc),
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Fin_Addr, Loc),
Attribute_Name => Name_Unrestricted_Access)));
end Make_Set_Finalize_Address_Call;
--------------------------
-- Make_Transient_Block --
--------------------------
function Make_Transient_Block
(Loc : Source_Ptr;
Action : Node_Id;
Par : Node_Id) return Node_Id
is
function Manages_Sec_Stack (Id : Entity_Id) return Boolean;
-- Determine whether scoping entity Id manages the secondary stack
function Within_Loop_Statement (N : Node_Id) return Boolean;
-- Return True when N appears within a loop and no block is containing N
-----------------------
-- Manages_Sec_Stack --
-----------------------
function Manages_Sec_Stack (Id : Entity_Id) return Boolean is
begin
case Ekind (Id) is
-- An exception handler with a choice parameter utilizes a dummy
-- block to provide a declarative region. Such a block should not
-- be considered because it never manifests in the tree and can
-- never release the secondary stack.
when E_Block =>
return
Uses_Sec_Stack (Id) and then not Is_Exception_Handler (Id);
when E_Entry
| E_Entry_Family
| E_Function
| E_Procedure
=>
return Uses_Sec_Stack (Id);
when others =>
return False;
end case;
end Manages_Sec_Stack;
---------------------------
-- Within_Loop_Statement --
---------------------------
function Within_Loop_Statement (N : Node_Id) return Boolean is
Par : Node_Id := Parent (N);
begin
while Nkind (Par) not in
N_Handled_Sequence_Of_Statements | N_Loop_Statement |
N_Package_Specification | N_Proper_Body
loop
pragma Assert (Present (Par));
Par := Parent (Par);
end loop;
return Nkind (Par) = N_Loop_Statement;
end Within_Loop_Statement;
-- Local variables
Decls : constant List_Id := New_List;
Instrs : constant List_Id := New_List (Action);
Trans_Id : constant Entity_Id := Current_Scope;
Block : Node_Id;
Insert : Node_Id;
Scop : Entity_Id;
-- Start of processing for Make_Transient_Block
begin
-- Even though the transient block is tasked with managing the secondary
-- stack, the block may forgo this functionality depending on how the
-- secondary stack is managed by enclosing scopes.
if Manages_Sec_Stack (Trans_Id) then
-- Determine whether an enclosing scope already manages the secondary
-- stack.
Scop := Scope (Trans_Id);
while Present (Scop) loop
-- It should not be possible to reach Standard without hitting one
-- of the other cases first unless Standard was manually pushed.
if Scop = Standard_Standard then
exit;
-- The transient block is within a function which returns on the
-- secondary stack. Take a conservative approach and assume that
-- the value on the secondary stack is part of the result. Note
-- that it is not possible to detect this dependency without flow
-- analysis which the compiler does not have. Letting the object
-- live longer than the transient block will not leak any memory
-- because the caller will reclaim the total storage used by the
-- function.
elsif Ekind (Scop) = E_Function
and then Sec_Stack_Needed_For_Return (Scop)
then
Set_Uses_Sec_Stack (Trans_Id, False);
exit;
-- The transient block must manage the secondary stack when the
-- block appears within a loop in order to reclaim the memory at
-- each iteration.
elsif Ekind (Scop) = E_Loop then
exit;
-- Ditto when the block appears without a block that does not
-- manage the secondary stack and is located within a loop.
elsif Ekind (Scop) = E_Block
and then not Manages_Sec_Stack (Scop)
and then Present (Block_Node (Scop))
and then Within_Loop_Statement (Block_Node (Scop))
then
exit;
-- The transient block does not need to manage the secondary stack
-- when there is an enclosing construct which already does that.
-- This optimization saves on SS_Mark and SS_Release calls but may
-- allow objects to live a little longer than required.
-- The transient block must manage the secondary stack when switch
-- -gnatd.s (strict management) is in effect.
elsif Manages_Sec_Stack (Scop) and then not Debug_Flag_Dot_S then
Set_Uses_Sec_Stack (Trans_Id, False);
exit;
-- Prevent the search from going too far because transient blocks
-- are bounded by packages and subprogram scopes.
elsif Ekind (Scop) in E_Entry
| E_Entry_Family
| E_Function
| E_Package
| E_Procedure
| E_Subprogram_Body
then
exit;
end if;
Scop := Scope (Scop);
end loop;
end if;
-- Create the transient block. Set the parent now since the block itself
-- is not part of the tree. The current scope is the E_Block entity that
-- has been pushed by Establish_Transient_Scope.
pragma Assert (Ekind (Trans_Id) = E_Block);
Block :=
Make_Block_Statement (Loc,
Identifier => New_Occurrence_Of (Trans_Id, Loc),
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Statements => Instrs),
Has_Created_Identifier => True);
Set_Parent (Block, Par);
-- Insert actions stuck in the transient scopes as well as all freezing
-- nodes needed by those actions. Do not insert cleanup actions here,
-- they will be transferred to the newly created block.
Insert_Actions_In_Scope_Around
(Action, Clean => False, Manage_SS => False);
Insert := Prev (Action);
if Present (Insert) then
Freeze_All (First_Entity (Trans_Id), Insert);
end if;
-- Transfer cleanup actions to the newly created block
declare
Cleanup_Actions : List_Id
renames Scope_Stack.Table (Scope_Stack.Last).
Actions_To_Be_Wrapped (Cleanup);
begin
Set_Cleanup_Actions (Block, Cleanup_Actions);
Cleanup_Actions := No_List;
end;
-- When the transient scope was established, we pushed the entry for the
-- transient scope onto the scope stack, so that the scope was active
-- for the installation of finalizable entities etc. Now we must remove
-- this entry, since we have constructed a proper block.
Pop_Scope;
return Block;
end Make_Transient_Block;
------------------------
-- Node_To_Be_Wrapped --
------------------------
function Node_To_Be_Wrapped return Node_Id is
begin
return Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped;
end Node_To_Be_Wrapped;
----------------------------
-- Store_Actions_In_Scope --
----------------------------
procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id) is
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
Actions : List_Id renames SE.Actions_To_Be_Wrapped (AK);
begin
if Is_Empty_List (Actions) then
Actions := L;
if Is_List_Member (SE.Node_To_Be_Wrapped) then
Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
else
Set_Parent (L, SE.Node_To_Be_Wrapped);
end if;
Analyze_List (L);
elsif AK = Before then
Insert_List_After_And_Analyze (Last (Actions), L);
else
Insert_List_Before_And_Analyze (First (Actions), L);
end if;
end Store_Actions_In_Scope;
----------------------------------
-- Store_After_Actions_In_Scope --
----------------------------------
procedure Store_After_Actions_In_Scope (L : List_Id) is
begin
Store_Actions_In_Scope (After, L);
end Store_After_Actions_In_Scope;
-----------------------------------
-- Store_Before_Actions_In_Scope --
-----------------------------------
procedure Store_Before_Actions_In_Scope (L : List_Id) is
begin
Store_Actions_In_Scope (Before, L);
end Store_Before_Actions_In_Scope;
-----------------------------------
-- Store_Cleanup_Actions_In_Scope --
-----------------------------------
procedure Store_Cleanup_Actions_In_Scope (L : List_Id) is
begin
Store_Actions_In_Scope (Cleanup, L);
end Store_Cleanup_Actions_In_Scope;
------------------
-- Unnest_Block --
------------------
procedure Unnest_Block (Decl : Node_Id) is
Loc : constant Source_Ptr := Sloc (Decl);
Ent : Entity_Id;
Local_Body : Node_Id;
Local_Call : Node_Id;
Local_Proc : Entity_Id;
Local_Scop : Entity_Id;
begin
Local_Scop := Entity (Identifier (Decl));
Ent := First_Entity (Local_Scop);
Local_Proc := Make_Temporary (Loc, 'P');
Local_Body :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Local_Proc),
Declarations => Declarations (Decl),
Handled_Statement_Sequence =>
Handled_Statement_Sequence (Decl));
-- Handlers in the block may contain nested subprograms that require
-- unnesting.
Check_Unnesting_In_Handlers (Local_Body);
Rewrite (Decl, Local_Body);
Analyze (Decl);
Set_Has_Nested_Subprogram (Local_Proc);
Local_Call :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Local_Proc, Loc));
Insert_After (Decl, Local_Call);
Analyze (Local_Call);
-- The new subprogram has the same scope as the original block
Set_Scope (Local_Proc, Scope (Local_Scop));
-- And the entity list of the new procedure is that of the block
Set_First_Entity (Local_Proc, Ent);
-- Reset the scopes of all the entities to the new procedure
while Present (Ent) loop
Set_Scope (Ent, Local_Proc);
Next_Entity (Ent);
end loop;
end Unnest_Block;
-------------------------
-- Unnest_If_Statement --
-------------------------
procedure Unnest_If_Statement (If_Stmt : Node_Id) is
procedure Check_Stmts_For_Subp_Unnesting (Stmts : in out List_Id);
-- A list of statements (that may be a list associated with a then,
-- elsif, or else part of an if-statement) is traversed at the top
-- level to determine whether it contains a subprogram body, and if so,
-- the statements will be replaced with a new procedure body containing
-- the statements followed by a call to the procedure. The individual
-- statements may also be blocks, loops, or other if statements that
-- themselves may require contain nested subprograms needing unnesting.
procedure Check_Stmts_For_Subp_Unnesting (Stmts : in out List_Id) is
Subp_Found : Boolean := False;
begin
if Is_Empty_List (Stmts) then
return;
end if;
declare
Stmt : Node_Id := First (Stmts);
begin
while Present (Stmt) loop
if Nkind (Stmt) = N_Subprogram_Body then
Subp_Found := True;
exit;
end if;
Next (Stmt);
end loop;
end;
-- The statements themselves may be blocks, loops, etc. that in turn
-- contain nested subprograms requiring an unnesting transformation.
-- We perform this traversal after looking for subprogram bodies, to
-- avoid considering procedures created for one of those statements
-- (such as a block rewritten as a procedure) as a nested subprogram
-- of the statement list (which could result in an unneeded wrapper
-- procedure).
Check_Unnesting_In_Decls_Or_Stmts (Stmts);
-- If there was a top-level subprogram body in the statement list,
-- then perform an unnesting transformation on the list by replacing
-- the statements with a wrapper procedure body containing the
-- original statements followed by a call to that procedure.
if Subp_Found then
Unnest_Statement_List (Stmts);
end if;
end Check_Stmts_For_Subp_Unnesting;
-- Local variables
Then_Stmts : List_Id := Then_Statements (If_Stmt);
Else_Stmts : List_Id := Else_Statements (If_Stmt);
-- Start of processing for Unnest_If_Statement
begin
Check_Stmts_For_Subp_Unnesting (Then_Stmts);
Set_Then_Statements (If_Stmt, Then_Stmts);
if not Is_Empty_List (Elsif_Parts (If_Stmt)) then
declare
Elsif_Part : Node_Id :=
First (Elsif_Parts (If_Stmt));
Elsif_Stmts : List_Id;
begin
while Present (Elsif_Part) loop
Elsif_Stmts := Then_Statements (Elsif_Part);
Check_Stmts_For_Subp_Unnesting (Elsif_Stmts);
Set_Then_Statements (Elsif_Part, Elsif_Stmts);
Next (Elsif_Part);
end loop;
end;
end if;
Check_Stmts_For_Subp_Unnesting (Else_Stmts);
Set_Else_Statements (If_Stmt, Else_Stmts);
end Unnest_If_Statement;
-----------------
-- Unnest_Loop --
-----------------
procedure Unnest_Loop (Loop_Stmt : Node_Id) is
procedure Fixup_Inner_Scopes (Loop_Stmt : Node_Id);
-- The loops created by the compiler for array aggregates can have
-- nested finalization procedure when the type of the array components
-- needs finalization. It has the following form:
-- for J4b in 10 .. 12 loop
-- declare
-- procedure __finalizer;
-- begin
-- procedure __finalizer is
-- ...
-- end;
-- ...
-- obj (J4b) := ...;
-- When the compiler creates the N_Block_Statement, it sets its scope to
-- the upper scope (the one containing the loop).
-- The Unnest_Loop procedure moves the N_Loop_Statement inside a new
-- procedure and correctly sets the scopes for both the new procedure
-- and the loop entity. The inner block scope is not modified and this
-- leaves the Tree in an incoherent state (i.e. the inner procedure must
-- have its enclosing procedure in its scope ancestries).
-- This procedure fixes the scope links.
-- Another (better) fix would be to have the block scope set to be the
-- loop entity earlier (when the block is created or when the loop gets
-- an actual entity set). But unfortunately this proved harder to
-- implement ???
procedure Fixup_Inner_Scopes (Loop_Stmt : Node_Id) is
Stmt : Node_Id := First (Statements (Loop_Stmt));
Loop_Stmt_Ent : constant Entity_Id := Entity (Identifier (Loop_Stmt));
Ent_To_Fix : Entity_Id;
begin
while Present (Stmt) loop
if Nkind (Stmt) = N_Block_Statement
and then Is_Abort_Block (Stmt)
then
Ent_To_Fix := Entity (Identifier (Stmt));
Set_Scope (Ent_To_Fix, Loop_Stmt_Ent);
elsif Nkind (Stmt) = N_Loop_Statement then
Fixup_Inner_Scopes (Stmt);
end if;
Next (Stmt);
end loop;
end Fixup_Inner_Scopes;
Loc : constant Source_Ptr := Sloc (Loop_Stmt);
Ent : Entity_Id;
Local_Body : Node_Id;
Local_Call : Node_Id;
Loop_Ent : Entity_Id;
Local_Proc : Entity_Id;
Loop_Copy : constant Node_Id :=
Relocate_Node (Loop_Stmt);
begin
Loop_Ent := Entity (Identifier (Loop_Stmt));
Ent := First_Entity (Loop_Ent);
Local_Proc := Make_Temporary (Loc, 'P');
Local_Body :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Local_Proc),
Declarations => Empty_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (Loop_Copy)));
Rewrite (Loop_Stmt, Local_Body);
Analyze (Loop_Stmt);
Set_Has_Nested_Subprogram (Local_Proc);
Local_Call :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Local_Proc, Loc));
Insert_After (Loop_Stmt, Local_Call);
Analyze (Local_Call);
-- New procedure has the same scope as the original loop, and the scope
-- of the loop is the new procedure.
Set_Scope (Local_Proc, Scope (Loop_Ent));
Set_Scope (Loop_Ent, Local_Proc);
Fixup_Inner_Scopes (Loop_Copy);
-- The entity list of the new procedure is that of the loop
Set_First_Entity (Local_Proc, Ent);
-- Note that the entities associated with the loop don't need to have
-- their Scope fields reset, since they're still associated with the
-- same loop entity that now belongs to the copied loop statement.
end Unnest_Loop;
---------------------------
-- Unnest_Statement_List --
---------------------------
procedure Unnest_Statement_List (Stmts : in out List_Id) is
Loc : constant Source_Ptr := Sloc (First (Stmts));
Local_Body : Node_Id;
Local_Call : Node_Id;
Local_Proc : Entity_Id;
New_Stmts : constant List_Id := Empty_List;
begin
Local_Proc := Make_Temporary (Loc, 'P');
Local_Body :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Local_Proc),
Declarations => Empty_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts));
Append_To (New_Stmts, Local_Body);
Analyze (Local_Body);
Set_Has_Nested_Subprogram (Local_Proc);
Local_Call :=
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Local_Proc, Loc));
Append_To (New_Stmts, Local_Call);
Analyze (Local_Call);
-- Traverse the statements, and for any that are declarations or
-- subprogram bodies that have entities, set the Scope of those
-- entities to the new procedure's Entity_Id.
declare
Stmt : Node_Id := First (Stmts);
begin
while Present (Stmt) loop
case Nkind (Stmt) is
when N_Declaration
| N_Renaming_Declaration
=>
Set_Scope (Defining_Identifier (Stmt), Local_Proc);
when N_Subprogram_Body =>
Set_Scope
(Defining_Unit_Name (Specification (Stmt)), Local_Proc);
when others =>
null;
end case;
Next (Stmt);
end loop;
end;
Stmts := New_Stmts;
end Unnest_Statement_List;
--------------------------------
-- Wrap_Transient_Declaration --
--------------------------------
-- If a transient scope has been established during the processing of the
-- Expression of an Object_Declaration, it is not possible to wrap the
-- declaration into a transient block as usual case, otherwise the object
-- would be itself declared in the wrong scope. Therefore, all entities (if
-- any) defined in the transient block are moved to the proper enclosing
-- scope. Furthermore, if they are controlled variables they are finalized
-- right after the declaration. The finalization list of the transient
-- scope is defined as a renaming of the enclosing one so during their
-- initialization they will be attached to the proper finalization list.
-- For instance, the following declaration :
-- X : Typ := F (G (A), G (B));
-- (where G(A) and G(B) return controlled values, expanded as _v1 and _v2)
-- is expanded into :
-- X : Typ := [ complex Expression-Action ];
-- [Deep_]Finalize (_v1);
-- [Deep_]Finalize (_v2);
procedure Wrap_Transient_Declaration (N : Node_Id) is
Curr_S : Entity_Id;
Encl_S : Entity_Id;
begin
Curr_S := Current_Scope;
Encl_S := Scope (Curr_S);
-- Insert all actions including cleanup generated while analyzing or
-- expanding the transient context back into the tree. Manage the
-- secondary stack when the object declaration appears in a library
-- level package [body].
Insert_Actions_In_Scope_Around
(N => N,
Clean => True,
Manage_SS =>
Uses_Sec_Stack (Curr_S)
and then Nkind (N) = N_Object_Declaration
and then Ekind (Encl_S) in E_Package | E_Package_Body
and then Is_Library_Level_Entity (Encl_S));
Pop_Scope;
-- Relocate local entities declared within the transient scope to the
-- enclosing scope. This action sets their Is_Public flag accordingly.
Transfer_Entities (Curr_S, Encl_S);
-- Mark the enclosing dynamic scope to ensure that the secondary stack
-- is properly released upon exiting the said scope.
if Uses_Sec_Stack (Curr_S) then
Curr_S := Enclosing_Dynamic_Scope (Curr_S);
-- Do not mark a function that returns on the secondary stack as the
-- reclamation is done by the caller.
if Ekind (Curr_S) = E_Function
and then Needs_Secondary_Stack (Etype (Curr_S))
then
null;
-- Otherwise mark the enclosing dynamic scope
else
Set_Uses_Sec_Stack (Curr_S);
Check_Restriction (No_Secondary_Stack, N);
end if;
end if;
end Wrap_Transient_Declaration;
-------------------------------
-- Wrap_Transient_Expression --
-------------------------------
procedure Wrap_Transient_Expression (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Expr : Node_Id := Relocate_Node (N);
Temp : constant Entity_Id := Make_Temporary (Loc, 'E', N);
Typ : constant Entity_Id := Etype (N);
begin
-- Generate:
-- Temp : Typ;
-- declare
-- M : constant Mark_Id := SS_Mark;
-- procedure Finalizer is ... (See Build_Finalizer)
-- begin
-- Temp := <Expr>; -- general case
-- Temp := (if <Expr> then True else False); -- boolean case
-- at end
-- Finalizer;
-- end;
-- A special case is made for Boolean expressions so that the back end
-- knows to generate a conditional branch instruction, if running with
-- -fpreserve-control-flow. This ensures that a control-flow change
-- signaling the decision outcome occurs before the cleanup actions.
if Opt.Suppress_Control_Flow_Optimizations
and then Is_Boolean_Type (Typ)
then
Expr :=
Make_If_Expression (Loc,
Expressions => New_List (
Expr,
New_Occurrence_Of (Standard_True, Loc),
New_Occurrence_Of (Standard_False, Loc)));
end if;
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Temp,
Object_Definition => New_Occurrence_Of (Typ, Loc)),
Make_Transient_Block (Loc,
Action =>
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Temp, Loc),
Expression => Expr),
Par => Parent (N))));
if Debug_Generated_Code then
Set_Debug_Info_Needed (Temp);
end if;
Rewrite (N, New_Occurrence_Of (Temp, Loc));
Analyze_And_Resolve (N, Typ);
end Wrap_Transient_Expression;
------------------------------
-- Wrap_Transient_Statement --
------------------------------
procedure Wrap_Transient_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
New_Stmt : constant Node_Id := Relocate_Node (N);
begin
-- Generate:
-- declare
-- M : constant Mark_Id := SS_Mark;
-- procedure Finalizer is ... (See Build_Finalizer)
--
-- begin
-- <New_Stmt>;
--
-- at end
-- Finalizer;
-- end;
Rewrite (N,
Make_Transient_Block (Loc,
Action => New_Stmt,
Par => Parent (N)));
-- With the scope stack back to normal, we can call analyze on the
-- resulting block. At this point, the transient scope is being
-- treated like a perfectly normal scope, so there is nothing
-- special about it.
-- Note: Wrap_Transient_Statement is called with the node already
-- analyzed (i.e. Analyzed (N) is True). This is important, since
-- otherwise we would get a recursive processing of the node when
-- we do this Analyze call.
Analyze (N);
end Wrap_Transient_Statement;
end Exp_Ch7;
|