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
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- S E M _ C H 1 2 --
-- --
-- B o d y --
-- --
-- $Revision: 1.522 $ --
-- --
-- Copyright (C) 1992-1997, 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 2, 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 COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
with Atree; use Atree;
with Einfo; use Einfo;
with Elists; use Elists;
with Errout; use Errout;
with Expander; use Expander;
with Exp_Ch7; use Exp_Ch7;
with Features; use Features;
with Fname; use Fname;
with Freeze; use Freeze;
with Hostparm;
with Inline; use Inline;
with Lib; use Lib;
with Lib.Load; use Lib.Load;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Output; use Output;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Ch3; use Sem_Ch3;
with Sem_Ch6; use Sem_Ch6;
with Sem_Ch7; use Sem_Ch7;
with Sem_Ch8; use Sem_Ch8;
with Sem_Ch10; use Sem_Ch10;
with Sem_Ch13; use Sem_Ch13;
with Sem_Dist; use Sem_Dist;
with Sem_Elab; use Sem_Elab;
with Sem_Eval; use Sem_Eval;
with Sem_Res; use Sem_Res;
with Sem_Type; use Sem_Type;
with Sem_Util; use Sem_Util;
with Stand; use Stand;
with Sinfo; use Sinfo;
with Sinfo.CN; use Sinfo.CN;
with Sinput; use Sinput;
with Sinput.L; use Sinput.L;
with Snames; use Snames;
with Stringt; use Stringt;
with Uname; use Uname;
with Table;
with Tbuild; use Tbuild;
with Uintp; use Uintp;
with Urealp; use Urealp;
with GNAT.HTable;
package body Sem_Ch12 is
use Atree.Unchecked_Access;
-- This package performs untyped traversals of the tree, therefore it
-- needs direct access to the fields of a node.
----------------------------------------------------------
-- Implementation of Generic Analysis and Instantiation --
-----------------------------------------------------------
-- GNAT implements generics by macro expansion. No attempt is made to
-- share generic instantiations (for now). Analysis of a generic definition
-- does not perform any expansion action, but the expander must be called
-- on the tree for each instantiation, because the expansion may of course
-- depend on the generic actuals. All of this is best achieved as follows:
--
-- a) Semantic analysis of a generic unit is performed on a copy of the
-- tree for the generic unit. All tree modifications that follow analysis
-- do not affect the original tree. Links are kept between the original
-- tree and the copy, in order to recognize non-local references within
-- the generic, and propagate them to each instance (recall that name
-- resolution is done on the generic declaration: generics are not really
-- macros!). This is summarized in the following diagram:
--
-- .-----------. .----------.
-- | semantic |<--------------| generic |
-- | copy | | unit |
-- | |==============>| |
-- |___________| global |__________|
-- references | | |
-- | | |
-- .-----|--|.
-- | .-----|---.
-- | | .----------.
-- | | | generic |
-- |__| | |
-- |__| instance |
-- |__________|
--
-- b) Each instantiation copies the original tree, and inserts into it a
-- series of declarations that describe the mapping between generic formals
-- and actuals. For example, a generic In OUT parameter is an object
-- renaming of the corresponing actual, etc. Generic IN parameters are
-- constant declarations.
--
-- c) In order to give the right visibility for these renamings, we use
-- a different scheme for package and subprogram instantiations. For
-- packages, the list of renamings is inserted into the package
-- specification, before the visible declarations of the package. The
-- renamings are analyzed before any of the text of the instance, and are
-- thus visible at the right place. Furthermore, outside of the instance,
-- the generic parameters are visible and denote their corresponding
-- actuals.
-- For subprograms, we create a container package to hold the renamings
-- and the subprogram instance itself. Analysis of the package makes the
-- renaming declarations visible to the subprogram. After analyzing the
-- package, the defining entity for the subprogram is touched-up so that
-- it appears declared in the current scope, and not inside the container
-- package.
-- If the instantiation is a compilation unit, the container package is
-- given the same name as the subprogram instance. This ensures that
-- the elaboration procedure called by the binder, using the compilation
-- unit name, calls in fact the elaboration procedure for the package.
-- Not surprisingly, private types complicate this approach. By saving in
-- the original generic object the non-local references, we guarantee that
-- the proper entities are referenced at the point of instantiation.
-- However, for private types, this by itself does not insure that the
-- proper VIEW of the entity is used (the full type may be visible at the
-- point of generic definition, but not at instantiation, or vice-versa).
-- In order to reference the proper view, we special-case any reference
-- to private types in the generic object, by saving both views, one in
-- the generic and one in the semantic copy. At time of instantiation, we
-- check whether the two views are consistent, and exchange declarations if
-- necessary, in order to restore the correct visibility. Similarly, if
-- the instance view is private when the generic view was not, we perform
-- the exchange. After completing the instantiation, we restore the
-- current visibility. The flag Has_Private_View marks identifiers in the
-- the generic unit that require checking.
-- Visibility within nested generic units requires special handling.
-- Consider the following scheme:
--
-- type Global is ... -- outside of generic unit.
-- generic ...
-- package Outer is
-- ...
-- type Semi_Global is ... -- global to inner.
--
-- generic ... -- 1
-- procedure inner (X1 : Global; X2 : Semi_Global);
--
-- procedure in2 is new inner (...); -- 4
-- end Outer;
-- package New_Outer is new Outer (...); -- 2
-- procedure New_Inner is new New_Outer.Inner (...); -- 3
-- The semantic analysis of Outer captures all occurrences of Global.
-- The semantic analysis of Inner (at 1) captures both occurrences of
-- Global and Semi_Global.
-- At point 2 (instantiation of Outer), we also produce a generic copy
-- of Inner, even though Inner is, at that point, not being instantiated.
-- (This is just part of the semantic analysis of New_Outer).
-- Critically, references to Global within Inner must be preserved, while
-- references to Semi_Global should not preserved, because they must now
-- resolve to an entity within New_Outer. To distinguish between these, we
-- use a global variable, Current_Instantiated_Parent, which is set when
-- performing a generic copy during instantiation (at 2). This variable is
-- used when performing a generic copy that is not an instantiation, but
-- that is nested within one, as the occurrence of 1 within 2. The analysis
-- of a nested generic only preserves references that are global to the
-- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
-- determine whether a reference is external to the given parent.
-- The instantiation at point 3 requires no special treatment. The method
-- works as well for further nestings of generic units, but of course the
-- variable Current_Instantiated_Parent must be stacked because nested
-- instantiations can occur, e.g. the occurrence of 4 within 2.
-- The instantiation of package and subprogram bodies is handled in a
-- similar manner, except that it is delayed until after semantic
-- analysis is complete. In this fashion complex cross-dependencies
-- between several package declarations and bodies containing generics
-- can be compiled which otherwise would diagnose spurious circularities.
-- For example, it is possible to compile two packages A and B that
-- have the following structure:
-- package A is package B is
-- generic ... generic ...
-- package G_A is package G_B is
-- with B; with A;
-- package body A is package body B is
-- package N_B is new G_B (..) package N_A is new G_A (..)
-- The table Pending_Instantiations in package Inline is used to keep
-- track of body instantiations that are delayed in this manner. Inline
-- handles the actual calls to do the body instantiations. This activity
-- is part of Inline, since the processing occurs at the same point, and
-- for essentially the same reason, as the handling of inlined routines.
----------------------------------------------
-- Detection of Instantiation Circularities --
----------------------------------------------
-- If we have a chain of instantiations that is circular, this is a
-- static error which must be detected at compile time. The detection
-- of these circularities is carried out at the point that we insert
-- a generic instance spec or body. If there is a circularity, then
-- the analysis of the offending spec or body will eventually result
-- in trying to load the same unit again, and we detect this problem
-- as we analyze the package instantiation for the second time.
-- At least in some cases after we have detected the circularity, we
-- get into trouble if we try to keep going. The following flag is
-- set if a circularity is detected, and used to abandon compilation
-- after the messages have been posted.
Circularity_Detected : Boolean := False;
-- This should really be reset on encountering a new main unit, but
-- in practice only gnatf runs into this at the moment so it is not
-- critical ???
-----------------------
-- Local subprograms --
-----------------------
procedure Abandon_Instantiation (N : Node_Id);
-- Posts an error message "instantiation abandoned" at the indicated
-- node and then raises the exception Instantiation_Error to do it.
procedure Analyze_Formal_Array_Type
(T : in out Entity_Id;
Def : Node_Id);
-- A formal array type is treated like an array type declaration, and
-- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
-- in-out, because in the case of an anonymous type the entity is
-- actually created in the procedure.
-- The following procedures treat other kinds of formal parameters.
procedure Analyze_Formal_Derived_Type
(N : Node_Id;
T : Entity_Id;
Def : Node_Id);
-- All the following need comments???
procedure Analyze_Formal_Decimal_Fixed_Point_Type
(T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Ordinary_Fixed_Point_Type
(T : Entity_Id; Def : Node_Id);
procedure Analyze_Formal_Private_Type
(N : Node_Id;
T : Entity_Id;
Def : Node_Id);
-- This needs comments???
procedure Analyze_Generic_Formal_Part (N : Node_Id);
procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
-- This needs comments ???
function Analyze_Associations
(I_Node : Node_Id;
Formals : List_Id;
F_Copy : List_Id)
return List_Id;
-- At instantiation time, build the list of associations between formals
-- and actuals. Each association becomes a renaming declaration for the
-- formal entity. F_Copy is the analyzed list of formals in the generic
-- copy. It is used to apply legality checks to the actuals. I_Node is the
-- instantiation node itself.
procedure Analyze_Subprogram_Instantiation
(N : Node_Id;
K : Entity_Kind);
procedure Build_Instance_Compilation_Unit_Nodes
(N : Node_Id;
Act_Body : Node_Id;
Act_Decl : Node_Id);
-- This procedure is used in the case where the generic instance of a
-- subprogram body or package body is a library unit. In this case, the
-- original library unit node for the generic instantiation must be
-- replaced by the resulting generic body, and a link made to a new
-- compilation unit node for the generic declaration. The argument N is
-- the original generic instantiation. Act_Body and Act_Decl are the body
-- and declaration of the instance (either package body and declaration
-- nodes or subprogram body and declaration nodes depending on the case).
-- On return, the node N has been rewritten with the actual body.
procedure Check_Formal_Packages (P_Id : Entity_Id);
-- Apply the following to all formal packages in generic associations.
procedure Check_Formal_Package_Instance
(Formal_Pack : Entity_Id;
Actual_Pack : Entity_Id);
-- Verify that the actuals of the actual instance match the actuals of
-- the template for a formal package that is not declared with a box.
procedure Check_Private_View (N : Node_Id);
-- Check whether the type of a generic entity has a different view between
-- the point of generic analysis and the point of instantiation. If the
-- view has changed, then at the point of instantiation we restore the
-- correct view to perform semantic analysis of the instance, and reset
-- the current view after instantiation.
procedure Check_Generic_Actuals
(Instance : Entity_Id;
Is_Formal_Box : Boolean);
-- Similar to previous one. Check the actuals in the instantiation,
-- whose views can change between the point of instantiation and the point
-- of instantiation of the body. In addition, mark the generic renamings
-- as generic actuals, so that they are not compatible with other actuals.
-- Recurse on an actual that is a formal package whose declaration has
-- a box.
procedure Check_Generic_Child_Unit
(Gen_Id : Node_Id;
Parent_Installed : in out Boolean);
-- If the name of the generic unit in an instantiation is a selected
-- component, then the prefix may be an instance and the selector may
-- designate a child unit. Retrieve the parent generic and search for
-- the child unit that must be declared within. Similarly, if this is
-- the name of a generic child unit within an instantiation of its own
-- parent, retrieve the parent generic.
function Contains_Instance_Of
(Inner : Entity_Id;
Outer : Entity_Id;
N : Node_Id)
return Boolean;
-- Inner is instantiated within the generic Outer. Check whether Inner
-- directly or indirectly contains an instance of Outer or of one of its
-- parents, in the case of a subunit. Each generic unit holds a list of
-- the entities instantiated within (at any depth). This procedure
-- determines whether the set of such lists contains a cycle, i.e. an
-- illegal circular instantiation.
function Denotes_Formal_Package (Pack : Entity_Id) return Boolean;
-- Returns True if E is a formal package of an enclosing generic, or
-- the actual for such a formal in an enclosing instantiation. Used in
-- Restore_Private_Views, to keep the formals of such a package visible
-- on exit from an inner instantiation.
function Find_Actual_Type
(Typ : Entity_Id;
Gen_Scope : Entity_Id)
return Entity_Id;
-- When validating the actual types of a child instance, check whether
-- the formal is a formal type of the parent unit, and retrieve the current
-- actual for it. Typ is the entity in the analyzed formal type declaration
-- (component or index type of an array type) and Gen_Scope is the scope of
-- the analyzed formal array type.
function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
-- Given the entity of a unit that is an instantiation, retrieve the
-- original instance node. This is used when loading the instantiations
-- of the ancestors of a child generic that is being instantiated.
procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
-- Associate analyzed generic parameter with corresponding
-- instance. Used for semantic checks at instantiation time.
function Has_Been_Exchanged (E : Entity_Id) return Boolean;
-- Traverse the Exchanged_Views list to see if a type was private
-- and has already been flipped during this phase of instantiation.
procedure Hide_Current_Scope;
-- When compiling a generic child unit, the parent context must be
-- present, but the instance and all entities that may be generated
-- must be inserted in the current scope. We leave the current scope
-- on the stack, but make its entities invisible to avoid visibility
-- problems. This is reversed at the end of instantiations. This is
-- not done for the instantiation of the bodies, which only require the
-- instances of the generic parents to be in scope.
procedure Install_Body
(Act_Body : Node_Id;
N : Node_Id;
Gen_Body : Node_Id;
Gen_Decl : Node_Id);
-- If the instantiation happens textually before the body of the generic,
-- the instantiation of the body must be analyzed after the generic body,
-- and not at the point of instantiation. Such early instantiations can
-- happen if the generic and the instance appear in a package declaration
-- because the generic body can only appear in the corresponding package
-- body. Early instantiations can also appear if generic, instance and
-- body are all in the declarative part of a subprogram or entry. Entities
-- of packages that are early instantiations are delayed, and their freeze
-- node appears after the generic body.
procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
-- When compiling an instance of a child unit the parent (which is
-- itself an instance) is an enclosing scope that must be made
-- immediately visible. This procedure is also used to install the non-
-- generic parent of a generic child unit when compiling its body, so that
-- full views of types in the parent are made visible.
procedure Remove_Parent (In_Body : Boolean := False);
-- Reverse effect after instantiation of child is complete.
-- The functions Instantiate_XXX perform various legality checks and build
-- the declarations for instantiated generic parameters.
-- Need to describe what the parameters are ???
function Instantiate_Object
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return List_Id;
function Instantiate_Type
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return Node_Id;
function Instantiate_Formal_Subprogram
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return Node_Id;
function Instantiate_Formal_Package
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return List_Id;
-- If the formal package is declared with a box, special visibility rules
-- apply to its formals: they are in the visible part of the package. This
-- is true in the declarative region of the formal package, that is to say
-- in the enclosing generic or instantiation. For an instantiation, the
-- parameters of the formal package are made visible in an explicit step.
-- Furthermore, if the actual is a visible use_clause, these formals must
-- be made potentially use_visible as well. On exit from the enclosing
-- instantiation, the reverse must be done.
-- For a formal package declared without a box, there are conformance rules
-- that apply to the actuals in the generic declaration and the actuals of
-- the actual package in the enclosing instantiation. The simplest way to
-- apply these rules is to repeat the instantiation of the formal package
-- in the context of the enclosing instance, and compare the generic
-- associations of this instantiation with those of the actual package.
function Is_In_Main_Unit (N : Node_Id) return Boolean;
-- Test if given node is in the main unit
procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
-- If the generic appears in a separate non-generic library unit,
-- load the corresponding body to retrieve the body of the generic.
-- N is the node for the generic instantiation, Spec is the generic
-- package declaration.
procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
-- This is used to inherit the context clause for a generic subunit ???
function Associated_Node (N : Node_Id) return Node_Id;
-- In order to propagate semantic information back from the analyzed
-- copy to the original generic, we maintain links between selected nodes
-- in the generic and their corresponding copies. At the end of generic
-- analysis, the routine Save_Global_References traverses the generic
-- tree, examines the semantic information, and preserves the links to
-- those nodes that contain global information. At instantiation, the
-- information from the associated node is placed on the new copy, so that
-- name resolution is not repeated.
-- Two kinds of nodes have associated nodes:
-- a) those that contain entities, that is to say identifiers, expanded_
-- names, and operators.
-- b) aggregates.
-- For the first class, the associated node preserves the entity if it is
-- global. If the generic contains nested instantiations, the associated_
-- node itself has been recopied, and a chain of them must be followed.
-- For aggregates, the associated node allows retrieval of the type, which
-- may otherwise not appear in the generic. The view of this type may be
-- different between generic and instantiation, and the full view can be
-- installed before the instantiation is analyzed. For aggregates of
-- type extensions, the same view exchange may have to be performed for
-- some of the ancestor types, if their view is private at the point of
-- instantiation.
-- The associated node is stored in Node4, using this field as a free
-- union in a fashion that should clearly be under control of sinfo ???
procedure Move_Freeze_Nodes
(Out_Of : Entity_Id;
After : Node_Id;
L : List_Id);
-- Freeze nodes can be generated in the analysis of a generic unit, but
-- will not be seen by the back-end. It is necessary to move those nodes
-- to the enclosing scope if they freeze an outer entity. We place them
-- at the end of the enclosing generic package, which is semantically
-- neutral.
procedure Set_Associated_Node
(Gen_Node : Node_Id;
Copy_Node : Node_Id);
-- Establish the link between an identifier in the generic unit, and the
-- corresponding node in the semantic copy.
procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
-- Verify that an attribute that appears as the default for a formal
-- subprogram is a function or procedure with the correct profile.
-------------------------------------------
-- Data Structures for Generic Renamings --
-------------------------------------------
-- The map Generic_Renamings associates generic entities with their
-- corresponding actuals. Currently used to validate type instances.
-- It will eventually be used for all generic parameters to eliminate
-- the need for overload resolution in the instance.
type Assoc_Ptr is new Int;
Assoc_Null : constant Assoc_Ptr := -1;
type Assoc is record
Gen_Id : Entity_Id;
Act_Id : Entity_Id;
Next_In_HTable : Assoc_Ptr;
end record;
package Generic_Renamings is new Table.Table
(Table_Component_Type => Assoc,
Table_Index_Type => Assoc_Ptr,
Table_Low_Bound => 0,
Table_Initial => 10,
Table_Increment => 100,
Table_Name => "Generic_Renamings");
-- Variable to hold enclosing instantiation.
Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
-- Hash table for associations
HTable_Size : constant := 37;
type HTable_Range is range 0 .. HTable_Size - 1;
procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
function Hash (F : Entity_Id) return HTable_Range;
package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
Header_Num => HTable_Range,
Element => Assoc,
Elmt_Ptr => Assoc_Ptr,
Null_Ptr => Assoc_Null,
Set_Next => Set_Next_Assoc,
Next => Next_Assoc,
Key => Entity_Id,
Get_Key => Get_Gen_Id,
Hash => Hash,
Equal => "=");
Exchanged_Views : Elist_Id;
-- This list holds the private views that have been exchanged during
-- instantiation to restore the visibility of the generic declaration.
-- (see comments above). After instantiation, the current visibility is
-- reestablished by means of a traversal of this list.
procedure Restore_Private_Views
(Pack_Id : Entity_Id;
Is_Package : Boolean := True);
-- Restore the private views of external types, and unmark the generic
-- renamings of actuals, so that they become comptible subtypes again.
-- For subprograms, Pack_Id is the package constructed to hold the
-- renamings.
procedure Switch_View (T : Entity_Id);
-- Switch the partial and full views of a type and its private
-- dependents (i.e. its subtypes and derived types).
------------------------------------
-- Structures for Error Reporting --
------------------------------------
Instantiation_Node : Node_Id;
-- Used by subprograms that validate instantiation of formal parameters
-- where there might be no actual on which to place the error message.
-- Also used to locate the instantiation node for generic subunits.
Instantiation_Error : exception;
-- When there is a semantic error in the generic parameter matching,
-- there is no point in continuing the instantiation, because the
-- number of cascaded errors is unpredictable. This exception aborts
-- the instantiation process altogether.
S_Adjustment : Sloc_Adjustment;
-- Offset created for each node in an instantiation, in order to keep
-- track of the source position of the instantiation in each of its nodes.
-- A subsequent semantic error or warning on a construct of the instance
-- points to both places: the original generic node, and the point of
-- instantiation. See Sinput and Sinput.L for additional details.
------------------------------------------------------------
-- Data structure for keeping track when inside a Generic --
------------------------------------------------------------
-- The following table is used to save values of the Inside_A_Generic
-- flag (see spec of Sem) when they are saved by Start_Generic.
package Generic_Flags is new Table.Table (
Table_Component_Type => Boolean,
Table_Index_Type => Int,
Table_Low_Bound => 0,
Table_Initial => 32,
Table_Increment => 200,
Table_Name => "Generic_Flags");
--------------------
-- Set_Next_Assoc --
--------------------
procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
begin
Generic_Renamings.Table (E).Next_In_HTable := Next;
end Set_Next_Assoc;
----------------
-- Next_Assoc --
----------------
function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
begin
return Generic_Renamings.Table (E).Next_In_HTable;
end Next_Assoc;
----------------
-- Get_Gen_Id --
----------------
function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
begin
return Generic_Renamings.Table (E).Gen_Id;
end Get_Gen_Id;
----------
-- Hash --
----------
function Hash (F : Entity_Id) return HTable_Range is
begin
return HTable_Range (F mod HTable_Size);
end Hash;
---------------------------
-- Abandon_Instantiation --
---------------------------
procedure Abandon_Instantiation (N : Node_Id) is
begin
Error_Msg_N ("instantiation abandoned!", N);
raise Instantiation_Error;
end Abandon_Instantiation;
-----------------
-- End_Generic --
-----------------
procedure End_Generic is
begin
-- ??? I am sure more things could be factored out in this
-- routine. Should probably be done at a later stage.
Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
Generic_Flags.Decrement_Last;
Expander_Mode_Restore;
end End_Generic;
-------------------
-- Start_Generic --
-------------------
procedure Start_Generic is
begin
-- ??? I am sure more things could be factored out in this
-- routine. Should probably be done at a later stage.
Generic_Flags.Increment_Last;
Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
Inside_A_Generic := True;
Expander_Mode_Save_And_Set (False);
end Start_Generic;
------------------------------------------
-- Analyze_Generic_Package_Declaration --
------------------------------------------
procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
Id : Entity_Id;
New_N : Node_Id;
Save_Parent : Node_Id;
begin
-- Create copy of generic unit, and save for instantiation.
-- If the unit is a child unit, do not copy the specifications
-- for the parent, which are not part of the generic tree.
Save_Parent := Parent_Spec (N);
Set_Parent_Spec (N, Empty);
New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
Set_Parent_Spec (New_N, Save_Parent);
Rewrite (N, New_N);
Id := Defining_Entity (N);
-- Expansion is not applied to generic units.
Start_Generic;
Enter_Name (Id);
Set_Ekind (Id, E_Generic_Package);
Set_Etype (Id, Standard_Void_Type);
New_Scope (Id);
Enter_Generic_Scope (Id);
Set_Inner_Instances (Id, New_Elmt_List);
Set_Categorization_From_Pragmas (N);
Set_Is_Pure (Id, Is_Pure (Current_Scope));
-- For a library unit, we have reconstructed the entity for the
-- unit, and must reset it in the library tables.
if Nkind (Parent (N)) = N_Compilation_Unit then
Set_Cunit_Entity (Current_Sem_Unit, Id);
end if;
Analyze_Generic_Formal_Part (N);
-- After processing the generic formals, analysis proceeds
-- as for a non-generic package.
Analyze (Specification (N));
Validate_Categorization_Dependency (N, Id);
End_Generic;
End_Package_Scope (Id);
Exit_Generic_Scope (Id);
if Nkind (Parent (N)) /= N_Compilation_Unit then
Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
else
Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
end if;
end Analyze_Generic_Package_Declaration;
--------------------------------------------
-- Analyze_Generic_Subprogram_Declaration --
--------------------------------------------
procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
Spec : Node_Id;
Id : Entity_Id;
Formals : List_Id;
New_N : Node_Id;
Save_Parent : Node_Id;
begin
-- Create copy of generic unit,and save for instantiation.
-- If the unit is a child unit, do not copy the specifications
-- for the parent, which are not part of the generic tree.
Save_Parent := Parent_Spec (N);
Set_Parent_Spec (N, Empty);
New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
Set_Parent_Spec (New_N, Save_Parent);
Rewrite (N, New_N);
Spec := Specification (N);
Id := Defining_Entity (Spec);
if Nkind (Id) = N_Defining_Operator_Symbol then
Error_Msg_N
("operator symbol not allowed for generic subprogram", Id);
end if;
Start_Generic;
Enter_Name (Id);
New_Scope (Id);
Set_Inner_Instances (Id, New_Elmt_List);
Set_Is_Pure (Id, Is_Pure (Current_Scope));
Analyze_Generic_Formal_Part (N);
Formals := Parameter_Specifications (Spec);
if Present (Formals) then
Process_Formals (Id, Formals, Spec);
end if;
if Nkind (Spec) = N_Function_Specification then
Set_Ekind (Id, E_Generic_Function);
Find_Type (Subtype_Mark (Spec));
Set_Etype (Id, Entity (Subtype_Mark (Spec)));
else
Set_Ekind (Id, E_Generic_Procedure);
Set_Etype (Id, Standard_Void_Type);
end if;
-- For a library unit, we have reconstructed the entity for the
-- unit, and must reset it in the library tables. We also need
-- to make sure that Body_Required is set properly in the original
-- compilation unit node.
if Nkind (Parent (N)) = N_Compilation_Unit then
Set_Cunit_Entity (Current_Sem_Unit, Id);
Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
end if;
Set_Categorization_From_Pragmas (N);
Validate_Categorization_Dependency (N, Id);
Save_Global_References (Original_Node (N));
End_Generic;
End_Scope;
end Analyze_Generic_Subprogram_Declaration;
---------------------------------
-- Analyze_Generic_Formal_Part --
---------------------------------
procedure Analyze_Generic_Formal_Part (N : Node_Id) is
Gen_Parm_Decl : Node_Id;
begin
-- The generic formals are processed in the scope of the generic
-- unit, where they are immediately visible. The scope is installed
-- by the caller.
Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
while Present (Gen_Parm_Decl) loop
Analyze (Gen_Parm_Decl);
Gen_Parm_Decl := Next (Gen_Parm_Decl);
end loop;
end Analyze_Generic_Formal_Part;
---------------------
-- Is_In_Main_Unit --
---------------------
function Is_In_Main_Unit (N : Node_Id) return Boolean is
Unum : constant Unit_Number_Type := Get_Sloc_Unit_Number (Sloc (N));
Current_Unit : Node_Id;
begin
if Unum = Main_Unit then
return True;
-- If the current unit is a subunit then it is either the main unit
-- or is being compiled as part of the main unit.
elsif Nkind (N) = N_Compilation_Unit then
return Nkind (Unit (N)) = N_Subunit;
end if;
Current_Unit := Parent (N);
while Present (Current_Unit)
and then Nkind (Current_Unit) /= N_Compilation_Unit
loop
Current_Unit := Parent (Current_Unit);
end loop;
-- The instantiation node is in the main unit, or else the current
-- node (perhaps as the result of nested instantiations) is in the
-- main unit, or in the declaration of the main unit, which in this
-- last case must be a body.
return Unum = Main_Unit
or else Current_Unit = Cunit (Main_Unit)
or else Current_Unit = Library_Unit (Cunit (Main_Unit))
or else (Present (Library_Unit (Current_Unit))
and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
end Is_In_Main_Unit;
-----------------------------------
-- Analyze_Package_Instantiation --
-----------------------------------
-- Note: this procedure is also used for formal package declarations,
-- in which case the argument N is an N_Formal_Package_Declaration
-- node. This should really be noted in the spec! ???
procedure Analyze_Package_Instantiation (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Gen_Id : constant Node_Id := Name (N);
Act_Decl : Node_Id;
Act_Decl_Name : Node_Id;
Act_Decl_Id : Entity_Id;
Act_Spec : Node_Id;
Act_Tree : Node_Id;
Gen_Decl : Node_Id;
Gen_Unit : Entity_Id;
Is_Actual_Pack : Boolean := Is_Internal (Defining_Entity (N));
Parent_Installed : Boolean := False;
Renaming_List : List_Id;
Unit_Renaming : Node_Id;
Needs_Body : Boolean;
Save_Instantiated_Parent : Assoc;
Save_Exchanged_Views : Elist_Id;
begin
-- Very first thing: apply the special kludge for Text_IO processing
-- in case we are instantiating one of the children of [Wide_]Text_IO.
Text_IO_Kludge (Name (N));
-- Make node global for error reporting.
Instantiation_Node := N;
-- Case of instantiation of a generic package
if Nkind (N) = N_Package_Instantiation then
Act_Decl_Id := New_Copy (Defining_Entity (N));
if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
Act_Decl_Name := Make_Defining_Program_Unit_Name (Loc,
Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
Defining_Identifier => Act_Decl_Id);
else
Act_Decl_Name := Act_Decl_Id;
end if;
-- Case of instantiation of a formal package
else
Act_Decl_Id := Defining_Identifier (N);
Act_Decl_Name := Act_Decl_Id;
end if;
Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
Gen_Unit := Entity (Gen_Id);
-- Verify that it is the name of a generic package
if Etype (Gen_Unit) = Any_Type then
return;
elsif Ekind (Gen_Unit) /= E_Generic_Package then
Error_Msg_N
("expect name of generic package in instantiation", Gen_Id);
return;
end if;
if Nkind (Gen_Id) = N_Identifier
and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
then
Error_Msg_NE
("& is hidden within declaration of instance", N, Gen_Unit);
end if;
-- If renaming, indicate this is an instantiation of renamed unit
if Present (Renamed_Object (Gen_Unit))
and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
then
Gen_Unit := Renamed_Object (Gen_Unit);
Set_Entity (Gen_Id, Gen_Unit);
end if;
-- Verify that there are no circular instantiations.
if In_Open_Scopes (Gen_Unit) then
Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
Error_Msg_Node_2 := Current_Scope;
Error_Msg_NE
("circular Instantiation: & instantiated in &!", N, Gen_Unit);
Circularity_Detected := True;
else
Gen_Decl := Get_Declaration_Node (Gen_Unit);
-- Initialize renamings map, for error checking, and the list
-- that holds private entities whose views have changed between
-- generic definition and instantiation. If this is the instance
-- created to validate an actual package, the instantiation
-- environment is that of the enclosing instance.
Save_Exchanged_Views := Exchanged_Views;
Exchanged_Views := New_Elmt_List;
Generic_Renamings.Set_Last (0);
Generic_Renamings_HTable.Reset;
Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
-- Copy original generic tree, to produce text for instantiation.
Save_Instantiated_Parent := Current_Instantiated_Parent;
Current_Instantiated_Parent := (Gen_Unit, Act_Decl_Id, Assoc_Null);
Act_Tree :=
Copy_Generic_Node
(Original_Node (Gen_Decl), Empty, Instantiating => True);
Act_Spec := Specification (Act_Tree);
-- If this is the instance created to validate an actual package,
-- only the formals matter, do not examine the package spec itself.
if Is_Actual_Pack then
Set_Visible_Declarations (Act_Spec, New_List);
Set_Private_Declarations (Act_Spec, New_List);
end if;
Renaming_List :=
Analyze_Associations
(N,
Generic_Formal_Declarations (Act_Tree),
Generic_Formal_Declarations (Gen_Decl));
Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
Set_Is_Generic_Instance (Act_Decl_Id);
Set_Generic_Parent (Act_Spec, Gen_Unit);
-- References to the generic in its own declaration or its body
-- are references to the instance. Add a renaming declaration for
-- the generic unit itself. This declaration, as well as the renaming
-- declarations for the generic formals, must remain private to the
-- unit: the formals, because this is the language semantics, and
-- the unit because its use is an artifact of the implementation.
Unit_Renaming :=
Make_Package_Renaming_Declaration (Loc,
Defining_Unit_Name =>
Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
Name => New_Reference_To (Act_Decl_Id, Loc));
Append (Unit_Renaming, Renaming_List);
-- The renaming declarations are the first local declarations of
-- the new unit.
if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
Insert_List_Before
(First (Visible_Declarations (Act_Spec)), Renaming_List);
else
Set_Visible_Declarations (Act_Spec, Renaming_List);
end if;
Act_Decl :=
Make_Package_Declaration (Loc,
Specification => Act_Spec);
-- Save the instantiation node, for subsequent instantiation
-- of the body, if there is one and we are generating code for
-- the current unit. Mark the unit as having a body, to avoid
-- a premature error message.
Needs_Body :=
(Unit_Requires_Body (Gen_Unit)
or else (Scope (Gen_Unit) /= Standard_Standard
and then not Is_Child_Unit (Gen_Unit)
and then Unit_Requires_Body (Scope (Gen_Unit)))
or else Present (Corresponding_Body (Gen_Decl)))
and then Is_In_Main_Unit (N)
and then not Is_Actual_Pack
and then (Operating_Mode = Generate_Code
or else Xref_Analyze);
if Needs_Body then
-- Here is a defence against a ludicrous number of instantiations
-- caused by a circular set of instantiation attempts.
if Pending_Instantiations.Last >
Hostparm.Max_Instantiations
then
Error_Msg_N ("too many instantiations", N);
raise Unrecoverable_Error;
end if;
-- If OK, then make entry in table.
-- Indicate that the enclosing scopes contain an instantiation,
-- and that cleanup actions should be delayed until after the
-- instance body is expanded.
declare
Enclosing_Master : Entity_Id := Current_Scope;
begin
while Enclosing_Master /= Standard_Standard
and then (Ekind (Enclosing_Master) = E_Package
or else
Ekind (Enclosing_Master) = E_Generic_Package)
loop
Enclosing_Master := Scope (Enclosing_Master);
end loop;
Set_Has_Pending_Instantiations (Enclosing_Master);
end;
Pending_Instantiations.Increment_Last;
Pending_Instantiations.Table (Pending_Instantiations.Last) :=
(N, Act_Decl, Expander_Active);
end if;
Set_Categorization_From_Pragmas (Act_Decl);
if Parent_Installed then
Hide_Current_Scope;
end if;
Set_Instance_Spec (N, Act_Decl);
-- Case of not a compilation unit
if Nkind (Parent (N)) /= N_Compilation_Unit then
Mark_Rewrite_Insertion (Act_Decl);
Insert_Before (N, Act_Decl);
Analyze (Act_Decl);
-- Case of compilation unit that is generic instantiation
-- Place declaration on current node so context is complete
-- for analysis (including nested instantiations).
else
Set_Unit (Parent (N), Act_Decl);
Set_Parent_Spec (Act_Decl, Parent_Spec (N));
Analyze (Act_Decl);
Set_Unit (Parent (N), N);
Set_Body_Required (Parent (N), False);
-- We never need elaboration checks on instantiations, since
-- by definition, the body instantiation is elaborated at the
-- same time as the spec instantiation.
Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
Set_Suppress_Elaboration_Checks (Act_Decl_Id);
end if;
Check_Elab_Instantiation (N);
if ABE_Is_Certain (N) and then Needs_Body then
Pending_Instantiations.Decrement_last;
end if;
Current_Instantiated_Parent := Save_Instantiated_Parent;
Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
First_Private_Entity (Act_Decl_Id));
if not Needs_Body
and then Nkind (Parent (N)) = N_Compilation_Unit
then
-- If this is the main unit, note that the main entity is now
-- the one created for the instance, because all scope info
-- is attached to it.
if Main_Unit_Entity = Defining_Entity (N) then
Main_Unit_Entity := Act_Decl_Id;
end if;
Rewrite (N, Act_Decl);
end if;
if Present (Corresponding_Body (Gen_Decl))
or else Unit_Requires_Body (Gen_Unit)
then
Set_Has_Completion (Act_Decl_Id);
end if;
Check_Formal_Packages (Act_Decl_Id);
Restore_Private_Views (Act_Decl_Id);
Exchanged_Views := Save_Exchanged_Views;
if not Generic_Separately_Compiled (Gen_Unit) then
Inherit_Context (Gen_Decl, N);
end if;
if Parent_Installed then
Remove_Parent;
end if;
end if;
Validate_Categorization_Dependency (N, Act_Decl_Id);
exception
when Instantiation_Error =>
null;
end Analyze_Package_Instantiation;
------------------------
-- Hide_Current_Scope --
------------------------
procedure Hide_Current_Scope is
E : Entity_Id;
begin
E := First_Entity (Current_Scope);
while Present (E) loop
Set_Is_Immediately_Visible (E, False);
E := Next_Entity (E);
end loop;
end Hide_Current_Scope;
------------------------------
-- Instantiate_Package_Body --
------------------------------
procedure Instantiate_Package_Body
(Body_Info : Pending_Body_Info)
is
Act_Decl : constant Node_Id := Body_Info.Act_Decl;
Inst_Node : constant Node_Id := Body_Info.Inst_Node;
Loc : constant Source_Ptr := Sloc (Inst_Node);
Gen_Id : constant Node_Id := Name (Inst_Node);
Gen_Unit : constant Entity_Id := Entity (Name (Inst_Node));
Gen_Decl : constant Node_Id := Get_Declaration_Node (Gen_Unit);
Act_Spec : constant Node_Id := Specification (Act_Decl);
Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
Act_Body_Name : Node_Id;
Gen_Body : Node_Id;
Gen_Body_Id : Node_Id;
Act_Body : Node_Id;
Act_Body_Id : Entity_Id;
Save_Instantiated_Parent : Assoc;
Save_Exchanged_Views : Elist_Id;
Parent_Installed : Boolean := False;
begin
Instantiation_Node := Inst_Node;
Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
Gen_Body_Id := Corresponding_Body (Gen_Decl);
if No (Gen_Body_Id) then
Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
Gen_Body_Id := Corresponding_Body (Gen_Decl);
end if;
if Present (Gen_Body_Id) then
Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
Save_Instantiated_Parent := Current_Instantiated_Parent;
Current_Instantiated_Parent := (Gen_Unit, Act_Decl_Id, Assoc_Null);
Save_Exchanged_Views := Exchanged_Views;
Exchanged_Views := New_Elmt_List;
Gen_Body := Get_Declaration_Node (Gen_Body_Id);
Create_Instantiation_Source
(Inst_Node, Gen_Body_Id, S_Adjustment);
Act_Body :=
Copy_Generic_Node
(Original_Node (Gen_Body), Empty, Instantiating => True);
-- Build new name (possible qualified) for body declaration.
Act_Body_Id := New_Copy (Act_Decl_Id);
if Nkind (Defining_Unit_Name (Act_Spec)) =
N_Defining_Program_Unit_Name
then
Act_Body_Name :=
Make_Defining_Program_Unit_Name (Loc,
Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
Defining_Identifier => Act_Body_Id);
else
Act_Body_Name := Act_Body_Id;
end if;
Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
Check_Generic_Actuals (Act_Decl_Id, False);
-- If it is a child unit, make the parent instance (which is an
-- instance of the parent of the generic) visible. The parent
-- instance is the prefix of the name of the generic unit.
if Ekind (Scope (Gen_Unit)) = E_Generic_Package
and then Nkind (Gen_Id) = N_Expanded_Name
then
Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
Parent_Installed := True;
elsif Is_Child_Unit (Gen_Unit) then
Install_Parent (Scope (Gen_Unit), In_Body => True);
end if;
-- If the instantiation is a library unit, and this is the main
-- unit, then build the resulting compilation unit nodes for the
-- instance. If this is a compilation unit but it is not the main
-- unit, then it is the body of a unit in the context, that is being
-- compiled because it is encloses some inlined unit or another
-- generic unit being instantiated. In that case, this body is not
-- part of the current compilation, and is not attached to the tree,
-- but its parent must be set for analysis.
if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
if Parent (Inst_Node) = Cunit (Main_Unit) then
Build_Instance_Compilation_Unit_Nodes
(Inst_Node, Act_Body, Act_Decl);
Analyze (Inst_Node);
-- If the instance is a child unit itself, then set the
-- scope of the expanded body to be the parent of the
-- instantiation (ensuring that the fully qualified name
-- will be generated for the elaboration subprogram).
if Nkind (Defining_Unit_Name (Act_Spec)) =
N_Defining_Program_Unit_Name
then
Set_Scope
(Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
end if;
else
Set_Parent (Act_Body, Parent (Inst_Node));
Analyze (Act_Body);
end if;
-- If this is an early instantiation, i.e. appears textually
-- before the corresponding body and must be elaborated first,
-- indicate that the body instance is to be delayed.
else
Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
Analyze (Act_Body);
end if;
if not Generic_Separately_Compiled (Gen_Unit) then
Inherit_Context (Gen_Body, Inst_Node);
end if;
Current_Instantiated_Parent := Save_Instantiated_Parent;
Restore_Private_Views (Act_Decl_Id);
Exchanged_Views := Save_Exchanged_Views;
Expander_Mode_Restore;
-- If we have no body, and the unit requires a body, then complain.
-- This complaint is suppressed if we have detected other errors
-- (since a common reason for missing the body is that it had errors).
elsif Unit_Requires_Body (Gen_Unit) then
if Errors_Detected = 0 then
Error_Msg_NE
("cannot find body of generic package &", Inst_Node, Gen_Unit);
-- Don't attempt to perform any cleanup actions if some other
-- error was aready detected, since this can cause blowups.
else
return;
end if;
-- Case of package that does not need a body
else
-- If the instantiation of the declaration is a library unit,
-- rewrite the original package instantiation as a package
-- declaration in the compilation unit node.
if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
Rewrite (Inst_Node, Act_Decl);
-- If the instantiation is not a library unit, then append the
-- declaration to the list of implicitly generated entities.
-- unless it is already a list member which means that it was
-- already processed
elsif not Is_List_Member (Act_Decl) then
Mark_Rewrite_Insertion (Act_Decl);
Insert_Before (Inst_Node, Act_Decl);
end if;
end if;
if Parent_Installed
or else Is_Child_Unit (Gen_Unit) then
Remove_Parent (In_Body => True);
end if;
end Instantiate_Package_Body;
---------------------------------
-- Instantiate_Subprogram_Body --
---------------------------------
procedure Instantiate_Subprogram_Body
(Body_Info : Pending_Body_Info)
is
Act_Decl : constant Node_Id := Body_Info.Act_Decl;
Inst_Node : constant Node_Id := Body_Info.Inst_Node;
Loc : constant Source_Ptr := Sloc (Inst_Node);
Decls : List_Id;
Gen_Id : constant Node_Id := Name (Inst_Node);
Gen_Unit : constant Entity_Id := Entity (Name (Inst_Node));
Gen_Decl : constant Node_Id := Get_Declaration_Node (Gen_Unit);
Anon_Id : constant Entity_Id :=
Defining_Unit_Name (Specification (Act_Decl));
Gen_Body : Node_Id;
Gen_Body_Id : Node_Id;
Act_Body : Node_Id;
Act_Body_Id : Entity_Id;
Pack_Id : Entity_Id := Defining_Unit_Name (Parent (Act_Decl));
Pack_Body : Node_Id;
Prev_Formal : Entity_Id;
Unit_Renaming : Node_Id;
Save_Instantiated_Parent : Assoc;
Save_Exchanged_Views : Elist_Id;
Parent_Installed : Boolean := False;
begin
Instantiation_Node := Inst_Node;
Gen_Body_Id := Corresponding_Body (Gen_Decl);
if No (Gen_Body_Id) then
Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
Gen_Body_Id := Corresponding_Body (Gen_Decl);
end if;
if Present (Gen_Body_Id) then
Gen_Body := Get_Declaration_Node (Gen_Body_Id);
if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
-- Either body is not present, or context is non-expanding, as
-- when compiling a subunit. Mark the instance as completed.
Set_Has_Completion (Anon_Id);
return;
end if;
Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
Save_Exchanged_Views := Exchanged_Views;
Exchanged_Views := New_Elmt_List;
Save_Instantiated_Parent := Current_Instantiated_Parent;
Current_Instantiated_Parent := (Gen_Unit, Anon_Id, Assoc_Null);
Create_Instantiation_Source (Inst_Node, Gen_Body_Id, S_Adjustment);
Act_Body :=
Copy_Generic_Node
(Original_Node (Gen_Body), Empty, Instantiating => True);
Act_Body_Id := Defining_Entity (Act_Body);
Set_Chars (Act_Body_Id, Chars (Anon_Id));
Set_Corresponding_Spec (Act_Body, Anon_Id);
Set_Has_Completion (Anon_Id);
Check_Generic_Actuals (Pack_Id, False);
-- If it is a child unit, make the parent instance (which is an
-- instance of the parent of the generic) visible. The parent
-- instance is the prefix of the name of the generic unit.
if Ekind (Scope (Gen_Unit)) = E_Generic_Package
and then Nkind (Gen_Id) = N_Expanded_Name
then
Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
Parent_Installed := True;
elsif Is_Child_Unit (Gen_Unit) then
Install_Parent (Scope (Gen_Unit), In_Body => True);
end if;
-- Inside its body, a reference to the generic unit is a reference
-- to the instance. The corresponding renaming is the first
-- declaration in the body.
Unit_Renaming :=
Make_Subprogram_Renaming_Declaration (Loc,
Specification =>
Copy_Generic_Node (
Specification (Original_Node (Gen_Body)),
Empty,
Instantiating => True),
Name => New_Occurrence_Of (Anon_Id, Loc));
-- If there is a formal subprogram with the same name as the
-- unit itself, do not add this renaming declaration. This is
-- a temporary fix for one ACVC test. ???
Prev_Formal := First_Entity (Pack_Id);
while Present (Prev_Formal) loop
if Chars (Prev_Formal) = Chars (Gen_Unit)
and then Is_Overloadable (Prev_Formal)
then
exit;
end if;
Prev_Formal := Next_Entity (Prev_Formal);
end loop;
if Present (Prev_Formal) then
Decls := New_List (Act_Body);
else
Decls := New_List (Unit_Renaming, Act_Body);
end if;
-- The subprogram body is placed in the body of a dummy package
-- body, whose spec contains the subprogram declaration as well
-- as the renaming declarations for the generic parameters.
Pack_Body := Make_Package_Body (Loc,
Defining_Unit_Name => New_Copy (Pack_Id),
Declarations => Decls);
Set_Corresponding_Spec (Pack_Body, Pack_Id);
-- If the instantiation is a library unit, then build resulting
-- compilation unit nodes for the instance. The declaration of
-- the enclosing package is the grandparent of the subprogram
-- declaration. First replace the instantiation node as the unit
-- of the corresponding compilation.
if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
Set_Unit (Parent (Inst_Node), Inst_Node);
Build_Instance_Compilation_Unit_Nodes
(Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
Analyze (Inst_Node);
else
Insert_Before (Inst_Node, Pack_Body);
Mark_Rewrite_Insertion (Pack_Body);
Analyze (Pack_Body);
-- Instance bodies are delayed so that Gigi can elaborate
-- them after all other declarations. Create freeze node
-- and insert it at end of current list of declarations.
if Expander_Active then
declare
F_Node : Node_Id;
begin
Ensure_Freeze_Node (Pack_Id);
F_Node := Freeze_Node (Pack_Id);
Insert_After (Last (List_Containing (Inst_Node)), F_Node);
end;
end if;
end if;
if not Generic_Separately_Compiled (Gen_Unit) then
Inherit_Context (Gen_Body, Inst_Node);
end if;
Restore_Private_Views (Pack_Id, False);
Exchanged_Views := Save_Exchanged_Views;
Expander_Mode_Restore;
if Parent_Installed
or else Is_Child_Unit (Gen_Unit)
then
Remove_Parent (In_Body => True);
end if;
-- Body not found. Error was emitted already.
else
null;
end if;
end Instantiate_Subprogram_Body;
------------------
-- Install_Body --
------------------
procedure Install_Body
(Act_Body : Node_Id;
N : Node_Id;
Gen_Body : Node_Id;
Gen_Decl : Node_Id)
is
Act_Id : Entity_Id := Corresponding_Spec (Act_Body);
Act_Unit : constant Node_Id :=
Unit (Cunit (Get_Sloc_Unit_Number (Sloc (N))));
F_Node : Node_Id;
Gen_Id : Entity_Id := Corresponding_Spec (Gen_Body);
Gen_Unit : constant Node_Id :=
Unit (Cunit (Get_Sloc_Unit_Number (Sloc (Gen_Decl))));
Orig_Body : Node_Id := Gen_Body;
Body_Unit : Node_Id;
Must_Delay : Boolean;
function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
-- Find subprogram (if any) that encloses instance and/or generic body.
function True_Sloc (N : Node_Id) return Source_Ptr;
-- If the instance is nested inside a generic unit, the Sloc of the
-- instance indicates the place of the original definition, not the
-- point of the current enclosing instance. Pending a better usage of
-- Slocs to indicate instantiation places, we determine the place of
-- origin of a node by finding the maximum sloc of any ancestor node.
function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
Scop : Entity_Id := Scope (Id);
begin
while Scop /= Standard_Standard
and then not Is_Overloadable (Scop)
loop
Scop := Scope (Scop);
end loop;
return Scop;
end Enclosing_Subp;
function True_Sloc (N : Node_Id) return Source_Ptr is
Res : Source_Ptr;
N1 : Node_Id;
begin
Res := Sloc (N);
N1 := N;
while Present (N1) and then N1 /= Act_Unit loop
if Sloc (N1) > Res then
Res := Sloc (N1);
end if;
N1 := Parent (N1);
end loop;
return Res;
end True_Sloc;
-- Start of processing for Install_Body
begin
-- if the body is a subunit, the freeze point is the corresponding
-- stub in the current compilation, not the subunit itself.
if Nkind (Parent (Gen_Body)) = N_Subunit then
Orig_Body := Corresponding_Stub (Parent (Gen_Body));
else
Orig_Body := Gen_Body;
end if;
Body_Unit := Unit (Cunit (Get_Sloc_Unit_Number (Sloc (Orig_Body))));
-- If the instantiation and the generic definition appear in the
-- same package declaration, this is an early instantiation.
-- If they appear in the same declarative part, it is an early
-- instantiation only if the generic body appears textually later,
-- and the generic body is also in the main unit.
-- If instance is nested within a subprogram, and the generic body is
-- not, the instance is delayed because the enclosing body is. If
-- instance and body are within the same scope, or the same sub-
-- program body, indicate explicitly that the instance is delayed.
Must_Delay :=
(Gen_Unit = Act_Unit
and then ((Nkind (Gen_Unit) = N_Package_Declaration)
or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
or else (Gen_Unit = Body_Unit
and then True_Sloc (N) < Sloc (Orig_Body)))
and then Is_In_Main_Unit (Gen_Unit)
and then (Scope (Act_Id) = Scope (Gen_Id)
or else
Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
-- The instance body must be delayed. Place the freeze node either
-- at the end of the current declarative part, or at the place of
-- the generic body, in the case of an early instantiation.
if Expander_Active then
Ensure_Freeze_Node (Act_Id);
F_Node := Freeze_Node (Act_Id);
if Must_Delay then
Insert_After (Orig_Body, F_Node);
else
Insert_After (Last (List_Containing (N)), F_Node);
end if;
end if;
Insert_Before (N, Act_Body);
Mark_Rewrite_Insertion (Act_Body);
end Install_Body;
--------------------
-- Install_Parent --
--------------------
procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
S : Entity_Id := Current_Scope;
Inst_Par : Entity_Id;
First_Par : Entity_Id;
Inst_Node : Node_Id;
Gen_Par : Entity_Id;
First_Gen : Entity_Id;
procedure Install_Spec (Par : Entity_Id);
-- The child unit is within the declarative part of the parent, so
-- the declarations within the parent are immediately visible.
procedure Install_Noninstance_Specs (Par : Entity_Id);
-- Install the scopes of noninstance parent units ending with Par.
procedure Install_Formal_Packages (Par : Entity_Id);
-- If any of the formals of the parent are formal packages with box,
-- their formal parts are visible in the parent and thus in the child
-- unit as well. Analogous to what is done in Check_Generic_Actuals
-- for the unit itself.
procedure Install_Formal_Packages (Par : Entity_Id) is
E : Entity_Id;
begin
E := First_Entity (Par);
while Present (E) loop
if Ekind (E) = E_Package
and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
then
-- If this is the renaming for the parent instance, done.
if Renamed_Object (E) = Par then
exit;
-- The visibility of a formal of an enclosing generic is
-- already correct.
elsif Denotes_Formal_Package (E) then
null;
elsif Present (Associated_Formal_Package (E))
and then Box_Present (Parent (Associated_Formal_Package (E)))
then
Check_Generic_Actuals (Renamed_Object (E), True);
Set_Is_Private (E, False);
end if;
end if;
E := Next_Entity (E);
end loop;
end Install_Formal_Packages;
procedure Install_Spec (Par : Entity_Id) is
Spec : constant Node_Id
:= Specification (Get_Declaration_Node (Par));
begin
New_Scope (Par);
Set_Is_Immediately_Visible (Par);
Install_Visible_Declarations (Par);
Install_Private_Declarations (Par);
Set_Use (Visible_Declarations (Spec));
Set_Use (Private_Declarations (Spec));
end Install_Spec;
procedure Install_Noninstance_Specs (Par : Entity_Id) is
begin
if Present (Par)
and then Par /= Standard_Standard
and then not In_Open_Scopes (Par)
then
Install_Noninstance_Specs (Scope (Par));
Install_Spec (Par);
end if;
end Install_Noninstance_Specs;
-- Start of processing for Install_Parent
begin
-- We need to install the parent instance to compile the instantiation
-- of the child, but the child instance must appear in the current
-- scope. Given that we cannot place the parent above the current
-- scope in the scope stack, we duplicate the current scope and unstack
-- both after the instantiation is complete.
-- If the parent is itself the instantiation of a child unit, we must
-- also stack the instantiation of its parent, and so on. Each such
-- ancestor is the prefix of the name in a prior instantiation.
-- If this is a nested instance, the parent unit itself resolves to
-- a renaming of the parent instance, whose declaration we need.
-- Finally, the parent may be a generic (not an instance) when the
-- child unit appears as a formal package.
Inst_Par := P;
if Present (Renamed_Entity (Inst_Par)) then
Inst_Par := Renamed_Entity (Inst_Par);
end if;
First_Par := Inst_Par;
Gen_Par :=
Generic_Parent (Specification (Get_Declaration_Node (Inst_Par)));
First_Gen := Gen_Par;
while Present (Gen_Par)
and then Is_Child_Unit (Gen_Par)
loop
-- Load grandparent instance as well.
Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
if Nkind (Name (Inst_Node)) = N_Expanded_Name then
Inst_Par := Entity (Prefix (Name (Inst_Node)));
if Present (Renamed_Entity (Inst_Par)) then
Inst_Par := Renamed_Entity (Inst_Par);
end if;
Gen_Par :=
Generic_Parent (Specification (Get_Declaration_Node (Inst_Par)));
if Present (Gen_Par) then
Install_Spec (Inst_Par);
Install_Formal_Packages (Inst_Par);
else
-- Parent is not the name of an instantiation.
Install_Noninstance_Specs (Inst_Par);
exit;
end if;
else
-- Previous error.
exit;
end if;
end loop;
if Present (First_Gen) then
Install_Spec (First_Par);
Install_Formal_Packages (First_Par);
else
Install_Noninstance_Specs (First_Par);
end if;
if not In_Body then
New_Scope (S);
end if;
end Install_Parent;
-------------------
-- Remove_Parent --
-------------------
procedure Remove_Parent (In_Body : Boolean := False) is
S : Entity_Id := Current_Scope;
E : Entity_Id;
P : Entity_Id;
begin
-- After child instantiation is complete, remove from scope stack
-- the extra copy of the current scope, and then remove parent
-- instances.
if not In_Body then
Pop_Scope;
while Current_Scope /= S loop
P := Current_Scope;
End_Package_Scope (Current_Scope);
if In_Open_Scopes (P) then
E := First_Entity (P);
while Present (E) loop
Set_Is_Immediately_Visible (E, True);
E := Next_Entity (E);
end loop;
end if;
end loop;
E := First_Entity (S);
while Present (E) loop
Set_Is_Immediately_Visible (E, True);
E := Next_Entity (E);
end loop;
else
-- Each body is analyzed separately, and there is no context
-- that needs preserving from one body instance to the next,
-- so remove all parent scopes.
while Present (S) loop
End_Package_Scope (S);
S := Scope (S);
exit when not Is_Child_Unit (S);
end loop;
end if;
end Remove_Parent;
-------------------------------------
-- Analyze_Procedure_Instantiation --
-------------------------------------
procedure Analyze_Procedure_Instantiation (N : Node_Id) is
begin
Analyze_Subprogram_Instantiation (N, E_Procedure);
end Analyze_Procedure_Instantiation;
------------------------------------
-- Analyze_Function_Instantiation --
------------------------------------
procedure Analyze_Function_Instantiation (N : Node_Id) is
begin
Analyze_Subprogram_Instantiation (N, E_Function);
end Analyze_Function_Instantiation;
--------------------------------------
-- Analyze_Subprogram_Instantiation --
--------------------------------------
procedure Analyze_Subprogram_Instantiation
(N : Node_Id;
K : Entity_Kind)
is
Loc : constant Source_Ptr := Sloc (N);
Gen_Id : constant Node_Id := Name (N);
Act_Decl_Id : Entity_Id;
Anon_Id : Entity_Id :=
Make_Defining_Identifier (Loc,
New_External_Name
(Chars (Defining_Entity (N)), 'R'));
Act_Decl : Node_Id;
Act_Spec : Node_Id;
Act_Tree : Node_Id;
Gen_Unit : Entity_Id;
Gen_Decl : Node_Id;
Pack_Id : Entity_Id;
Parent_Installed : Boolean := False;
Renaming_List : List_Id;
Spec : Node_Id;
Save_Instantiated_Parent : Assoc;
Save_Exchanged_Views : Elist_Id;
procedure Analyze_Instance_And_Renamings;
-- The instance must be analyzed in a context that includes the
-- mappings of generic parameters into actuals. We create a package
-- declaration for this purpose, and a subprogram with an internal
-- name within the package. The subprogram instance is simply an
-- alias for the internal subprogram, declared in the current scope.
procedure Analyze_Instance_And_Renamings is
Pack_Decl : Node_Id;
begin
if Nkind (Parent (N)) = N_Compilation_Unit then
-- The container package has the same name as the instantiation,
-- to insure that the binder calls the elaboration procedure
-- with the right name.
Pack_Id :=
Make_Defining_Identifier (Loc,
Chars => Chars (Defining_Entity (N)));
else
Pack_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
end if;
Pack_Decl := Make_Package_Declaration (Loc,
Specification => Make_Package_Specification (Loc,
Defining_Unit_Name => Pack_Id,
Visible_Declarations => Renaming_List));
Set_Instance_Spec (N, Pack_Decl);
-- Case of not a compilation unit
if Nkind (Parent (N)) /= N_Compilation_Unit then
Mark_Rewrite_Insertion (Pack_Decl);
Insert_Before (N, Pack_Decl);
Set_Has_Completion (Pack_Id);
-- Case of an instantiation that is a compilation unit
-- Place declaration on current node so context is complete
-- for analysis (including nested instantiations), and for
-- use in a context_clause (see Analyze_With_Clause).
else
Set_Unit (Parent (N), Pack_Decl);
Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
end if;
Analyze (Pack_Decl);
Check_Formal_Packages (Pack_Id);
-- Body of the enclosing package is supplied when instantiating
-- the subprogram body, after semantic analysis is completed.
if Nkind (Parent (N)) = N_Compilation_Unit then
-- Remove package itself from visibility, so it does not
-- conflict with subprogram.
Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
-- Set name and scope of internal subprogram so that the
-- proper external name will be generated.
Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
Set_Scope (Anon_Id, Standard_Standard);
end if;
Set_Is_Generic_Instance (Anon_Id);
Act_Decl_Id := New_Copy (Anon_Id);
Set_Parent (Act_Decl_Id, Parent (Anon_Id));
Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
New_Overloaded_Entity (Act_Decl_Id);
-- In compilation unit case, kill elaboration checks on the
-- instantiation, since they are never needed -- the body is
-- instantiated at the same point as the spec.
if Nkind (Parent (N)) = N_Compilation_Unit then
Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
Set_Suppress_Elaboration_Checks (Act_Decl_Id);
end if;
-- The instance is not a freezing point for the new subprogram.
Set_Is_Frozen (Act_Decl_Id, False);
if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
Valid_Operator_Definition (Act_Decl_Id);
end if;
Set_Alias (Act_Decl_Id, Anon_Id);
Set_Parent (Act_Decl_Id, Parent (Anon_Id));
Set_Has_Completion (Act_Decl_Id);
if Nkind (Parent (N)) = N_Compilation_Unit then
Set_Body_Required (Parent (N), False);
end if;
end Analyze_Instance_And_Renamings;
-- Start of processing for Analyze_Subprogram_Instantiation
begin
-- Very first thing: apply the special kludge for Text_IO processing
-- in case we are instantiating one of the children of [Wide_]Text_IO.
-- Of course such an instantiation is bogus (these are packages, not
-- subprograms), but we get a better error message if we do this.
Text_IO_Kludge (Name (N));
-- Make node global for error reporting.
Instantiation_Node := N;
Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
Gen_Unit := Entity (Gen_Id);
if Nkind (Gen_Id) = N_Identifier
and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
then
Error_Msg_NE
("& is hidden within declaration of instance", N, Gen_Unit);
end if;
-- If renaming, indicate that this is instantiation of renamed unit
if Present (Renamed_Object (Gen_Unit))
and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
or else
Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
then
Gen_Unit := Renamed_Object (Gen_Unit);
Set_Entity (Gen_Id, Gen_Unit);
end if;
if Etype (Gen_Unit) = Any_Type then return; end if;
-- Verify that it is a generic subprogram of the right kind, and that
-- it does not lead to a circular instantiation.
if Ekind (Gen_Unit) /= E_Generic_Procedure
and then Ekind (Gen_Unit) /= E_Generic_Function
then
Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
elsif In_Open_Scopes (Gen_Unit) then
Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
Error_Msg_Node_2 := Current_Scope;
Error_Msg_NE
("circular Instantiation: & instantiated in &!", N, Gen_Unit);
Circularity_Detected := True;
elsif K = E_Procedure
and then Ekind (Gen_Unit) /= E_Generic_Procedure
then
Error_Msg_N
("expect name of generic procedure in instantiation", Gen_Id);
elsif K = E_Function
and then Ekind (Gen_Unit) /= E_Generic_Function
then
Error_Msg_N
("expect name of generic function in instantiation", Gen_Id);
else
Gen_Decl := Get_Declaration_Node (Gen_Unit);
Spec := Specification (Gen_Decl);
-- Initialize renamings map, for error checking.
Save_Exchanged_Views := Exchanged_Views;
Exchanged_Views := New_Elmt_List;
Generic_Renamings.Set_Last (0);
Generic_Renamings_HTable.Reset;
Save_Instantiated_Parent := Current_Instantiated_Parent;
Current_Instantiated_Parent := (Gen_Unit, Act_Decl_Id, Assoc_Null);
Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
-- Copy original generic tree, to produce text for instantiation.
Act_Tree :=
Copy_Generic_Node
(Original_Node (Gen_Decl), Empty, Instantiating => True);
Act_Spec := Specification (Act_Tree);
Renaming_List :=
Analyze_Associations
(N,
Generic_Formal_Declarations (Act_Tree),
Generic_Formal_Declarations (Gen_Decl));
Set_Defining_Unit_Name (Act_Spec, Anon_Id);
Set_Generic_Parent (Act_Spec, Gen_Unit);
Act_Decl :=
Make_Subprogram_Declaration (Loc,
Specification => Act_Spec);
Set_Categorization_From_Pragmas (Act_Decl);
if Parent_Installed then
Hide_Current_Scope;
end if;
Append (Act_Decl, Renaming_List);
Analyze_Instance_And_Renamings;
-- If the generic is marked Import (Intrinsic), then so is the
-- instance. This indicates that there is no body to instantiate.
-- Other pragmas might also be inherited ???
if Is_Intrinsic_Subprogram (Gen_Unit) then
Set_Is_Intrinsic_Subprogram (Anon_Id);
Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
if Chars (Gen_Unit) = Name_Unchecked_Conversion then
Validate_Unchecked_Conversion (N, Act_Decl_Id);
end if;
end if;
Check_Elab_Instantiation (N);
Current_Instantiated_Parent := Save_Instantiated_Parent;
if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
if not Generic_Separately_Compiled (Gen_Unit) then
Inherit_Context (Gen_Decl, N);
end if;
Restore_Private_Views (Pack_Id, False);
-- If the context requires a full instantiation, mark node for
-- subsequent construction of the body.
if Is_In_Main_Unit (N)
and then (Operating_Mode = Generate_Code
or else Xref_Analyze)
and then not ABE_Is_Certain (N)
then
Pending_Instantiations.Increment_Last;
Pending_Instantiations.Table (Pending_Instantiations.Last) :=
(N, Act_Decl, Expander_Active);
-- The wrapper package is always delayed, because it does
-- not constitute a freeze point, but to insure that the
-- freeze node is placed properly, it is created directly
-- when instantiating the body (otherwise the freeze node
-- might appear to early for nested instantiations).
end if;
end if;
Exchanged_Views := Save_Exchanged_Views;
-- Subject to change, pending on if other pragmas are inherited ???
Validate_Categorization_Dependency (N, Act_Decl_Id);
if Parent_Installed then
Remove_Parent;
end if;
Generic_Renamings.Set_Last (0);
Generic_Renamings_HTable.Reset;
end if;
exception
when Instantiation_Error =>
null;
end Analyze_Subprogram_Instantiation;
----------------------------
-- Load_Parent_Of_Generic --
----------------------------
procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
Comp_Unit : constant Node_Id :=
Cunit (Get_Sloc_Unit_Number (Sloc (Spec)));
True_Parent : Node_Id;
Inst_Node : Node_Id;
OK : Boolean;
begin
if not In_Same_Unit (N, Spec)
or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
and then not Is_In_Main_Unit (Spec))
then
-- Find body of parent of spec, and analyze it. A special case
-- arises when the parent is an instantiation, that is to say when
-- we are currently instantiating a nested generic. In that case,
-- there is no separate file for the body of the enclosing instance.
-- Instead, the enclosing body must be instantiated as if it were
-- a pending instantiation, in order to produce the body for the
-- nested generic we require now. Note that in that case the
-- generic may be defined in a package body, the instance defined
-- in the same package body, and the original enclosing body may not
-- be in the main unit.
True_Parent := Parent (Spec);
Inst_Node := Empty;
while Present (True_Parent)
and then Nkind (True_Parent) /= N_Compilation_Unit
loop
if Nkind (True_Parent) = N_Package_Declaration
and then
Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
then
-- Parent is a compilation unit that is an instantiation.
-- Instantiation node has been replaced with package decl.
Inst_Node := Original_Node (True_Parent);
exit;
elsif Nkind (True_Parent) = N_Package_Declaration
and then Present (Generic_Parent (Specification (True_Parent)))
then
-- Parent is an instantiation within another specification.
-- Declaration for instance has been inserted before original
-- instantiation node. A direct link would be preferable?
Inst_Node := Next (True_Parent);
while Nkind (Inst_Node) /= N_Package_Instantiation loop
Inst_Node := Next (Inst_Node);
end loop;
exit;
else
True_Parent := Parent (True_Parent);
end if;
end loop;
if Present (Inst_Node) then
if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
-- Instantiation node and declaration of instantiated package
-- were exchanged when only the declaration was needed.
-- Restore instantiation node before proceeding with body.
Set_Unit (Parent (True_Parent), Inst_Node);
end if;
-- Now complete instantiation of enclosing body.
Instantiate_Package_Body
(Pending_Body_Info'(Inst_Node, True_Parent, Expander_Active));
else
Load_Needed_Body (Comp_Unit, OK);
if not OK then
declare
Bname : constant Unit_Name_Type :=
Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
begin
Error_Msg_Unit_1 := Bname;
Error_Msg_N ("this instantiation requires$!", N);
Error_Msg_Name_1 := Get_File_Name (Bname);
Error_Msg_N ("\but file{ was not found!", N);
raise Unrecoverable_Error;
end;
end if;
end if;
end if;
-- If loading the parent of the generic caused an instantiation
-- circularity, we abandon compilation at this point, because
-- otherwise in some cases we get into trouble with infinite
-- recursions after this point.
if Circularity_Detected then
raise Unrecoverable_Error;
end if;
end Load_Parent_Of_Generic;
---------------------
-- Inherit_Context --
---------------------
procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
Current_Context : List_Id;
Current_Unit : Node_Id;
Item : Node_Id;
New_I : Node_Id;
begin
if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
-- The inherited context is attached to the enclosing compilation
-- unit. This is either the main unit, or the declaration for the
-- main unit (in case the instantation appears within the package
-- declaration and the main unit is its body).
Current_Unit := Parent (Inst);
while Present (Current_Unit)
and then Nkind (Current_Unit) /= N_Compilation_Unit
loop
Current_Unit := Parent (Current_Unit);
end loop;
Current_Context := Context_Items (Current_Unit);
Item := First (Context_Items (Parent (Gen_Decl)));
while Present (Item) loop
if Nkind (Item) = N_With_Clause then
New_I := New_Copy (Item);
Set_Implicit_With (New_I, True);
Append (New_I, Current_Context);
end if;
Item := Next (Item);
end loop;
end if;
end Inherit_Context;
--------------------------
-- Analyze_Associations --
--------------------------
function Analyze_Associations
(I_Node : Node_Id;
Formals : List_Id;
F_Copy : List_Id)
return List_Id
is
Actuals : List_Id := Generic_Associations (I_Node);
Actual : Node_Id;
Actual_Types : Elist_Id := New_Elmt_List;
Assoc : List_Id := New_List;
Formal : Node_Id;
Next_Formal : Node_Id;
Temp_Formal : Node_Id;
Analyzed_Formal : Node_Id;
Defaults : Elist_Id := New_Elmt_List;
Match : Node_Id;
Named : Node_Id;
First_Named : Node_Id := Empty;
Found_Assoc : Node_Id;
Is_Named_Assoc : Boolean;
Num_Matched : Int := 0;
Num_Actuals : Int := 0;
function Matching_Actual (F : Entity_Id) return Node_Id;
-- Find actual that corresponds to a given a formal parameter. If the
-- actuals are positional, return the next one, if any. If the actuals
-- are named, scan the parameter associations to find the right one.
procedure Set_Analyzed_Formal;
-- Find the node in the generic copy that corresponds to a given formal.
-- The semantic information on this node is used to perform legality
-- checks on the actuals. Because semantic analysis can introduce some
-- anonymous entities or modify the declaration node itself, the
-- correspondence between the two lists is not one-one. In addition to
-- anonymous types, the presence a formal equality will introduce an
-- implicit declaration for the corresponding inequality.
---------------------
-- Matching_Actual --
---------------------
function Matching_Actual (F : Entity_Id) return Node_Id is
Found : Node_Id;
begin
Is_Named_Assoc := False;
-- End of list of purely positional parameters
if No (Actual) then
Found := Empty;
-- Case of positional parameter corresponding to current formal
elsif No (Selector_Name (Actual)) then
Found := Explicit_Generic_Actual_Parameter (Actual);
Found_Assoc := Actual;
Num_Matched := Num_Matched + 1;
Actual := Next (Actual);
-- Otherwise scan list of named actuals to find the one with the
-- desired name. All remaining actuals have explicit names.
else
Is_Named_Assoc := True;
Found := Empty;
while Present (Actual) loop
if Chars (Selector_Name (Actual)) = Chars (F) then
Found := Explicit_Generic_Actual_Parameter (Actual);
Found_Assoc := Actual;
Num_Matched := Num_Matched + 1;
exit;
end if;
Actual := Next (Actual);
end loop;
-- Reset for subsequent searches.
Actual := First_Named;
end if;
return Found;
end Matching_Actual;
-------------------------
-- Set_Analyzed_Formal --
-------------------------
procedure Set_Analyzed_Formal is
begin
while Present (Analyzed_Formal) loop
case Nkind (Formal) is
when N_Formal_Subprogram_Declaration =>
exit when Nkind (Analyzed_Formal)
= N_Formal_Subprogram_Declaration
and then
Chars
(Defining_Unit_Name (Specification (Formal))) =
Chars
(Defining_Unit_Name (Specification (Analyzed_Formal)));
when N_Formal_Package_Declaration =>
exit when
Nkind (Analyzed_Formal) = N_Formal_Package_Declaration
or else
Nkind (Analyzed_Formal) = N_Generic_Package_Declaration;
when N_Use_Package_Clause | N_Use_Type_Clause => exit;
when others =>
-- Skip freeze nodes, and nodes inserted to replace
-- unrecognized pragmas.
exit when
Nkind (Analyzed_Formal) /= N_Formal_Subprogram_Declaration
and then Nkind (Analyzed_Formal) /=
N_Subprogram_Declaration
and then Nkind (Analyzed_Formal) /= N_Freeze_Entity
and then Nkind (Analyzed_Formal) /= N_Null_Statement
and then Chars (Defining_Identifier (Formal)) =
Chars (Defining_Identifier (Analyzed_Formal));
end case;
Analyzed_Formal := Next (Analyzed_Formal);
end loop;
end Set_Analyzed_Formal;
-- Start of processing for Analyze_Associations
begin
-- If named associations are present, save the first named association
-- (it may of course be Empty) to facilitate subsequent name search.
if Present (Actuals) then
First_Named := First (Actuals);
while Present (First_Named)
and then No (Selector_Name (First_Named))
loop
Num_Actuals := Num_Actuals + 1;
First_Named := Next (First_Named);
end loop;
end if;
Named := First_Named;
while Present (Named) loop
if No (Selector_Name (Named)) then
Error_Msg_N ("invalid positional actual after named one", Named);
Abandon_Instantiation (Named);
end if;
Num_Actuals := Num_Actuals + 1;
Named := Next (Named);
end loop;
if Present (Formals) then
Formal := First_Non_Pragma (Formals);
Analyzed_Formal := First_Non_Pragma (F_Copy);
if Present (Actuals) then
Actual := First (Actuals);
-- All formals should have default values
else
Actual := Empty;
end if;
while Present (Formal) loop
Set_Analyzed_Formal;
Next_Formal := Next_Non_Pragma (Formal);
case Nkind (Formal) is
when N_Formal_Object_Declaration =>
Match := Matching_Actual (Defining_Identifier (Formal));
Append_List
(Instantiate_Object (Formal, Match, Analyzed_Formal),
Assoc);
when N_Formal_Type_Declaration =>
Match := Matching_Actual (Defining_Identifier (Formal));
if No (Match) then
Error_Msg_NE ("missing actual for instantiation of &",
Instantiation_Node, Defining_Identifier (Formal));
Abandon_Instantiation (Instantiation_Node);
else
Analyze (Match);
Append_To (Assoc,
Instantiate_Type (Formal, Match, Analyzed_Formal));
Append_Elmt (Entity (Match), Actual_Types);
end if;
-- A remote access-to-class-wide type must not be an
-- actual parameter for a generic formal (RM E.2.3(22))
Validate_Remote_Access_To_Class_Wide_Type (Match);
when N_Formal_Subprogram_Declaration =>
Match := Matching_Actual
(Defining_Unit_Name (Specification (Formal)));
-- If the formal subprogram has the same name as
-- another formal subprogram of the generic, then
-- a named association is illegal (12.3(9)). Exclude
-- named associations that are generated for a nested
-- instance.
if Present (Match)
and then Is_Named_Assoc
and then Comes_From_Source (Found_Assoc)
then
Temp_Formal := First (Formals);
while Present (Temp_Formal) loop
if Nkind (Temp_Formal) =
N_Formal_Subprogram_Declaration
and then Temp_Formal /= Formal
and then
Chars (Selector_Name (Found_Assoc)) =
Chars (Defining_Unit_Name
(Specification (Temp_Formal)))
then
Error_Msg_N
("name not allowed for overloaded formal",
Found_Assoc);
Abandon_Instantiation (Instantiation_Node);
end if;
Temp_Formal := Next (Temp_Formal);
end loop;
end if;
Append_To (Assoc,
Instantiate_Formal_Subprogram
(Formal, Match, Analyzed_Formal));
if No (Match)
and then Box_Present (Formal)
then
Append_Elmt
(Defining_Unit_Name (Specification (Last (Assoc))),
Defaults);
end if;
when N_Formal_Package_Declaration =>
Match := Matching_Actual (Defining_Identifier (Formal));
if No (Match) then
Error_Msg_NE
("missing actual for instantiation of&",
Instantiation_Node,
Defining_Identifier (Formal));
Abandon_Instantiation (Instantiation_Node);
else
Analyze (Match);
Append_List
(Instantiate_Formal_Package
(Formal, Match, Analyzed_Formal),
Assoc);
end if;
-- For use type and use package appearing in the context
-- clause, we have already copied them, so we can just
-- move them where they belong (we mustn't recopy them
-- since this would mess up the Sloc values).
when N_Use_Package_Clause |
N_Use_Type_Clause =>
Remove (Formal);
Append (Formal, Assoc);
when others =>
pragma Assert (False);
raise Program_Error;
end case;
Formal := Next_Formal;
Analyzed_Formal := Next_Non_Pragma (Analyzed_Formal);
end loop;
if Num_Actuals > Num_Matched then
Error_Msg_N
("unmatched actuals in instantiation", Instantiation_Node);
end if;
elsif Present (Actuals) then
Error_Msg_N
("too many actuals in generic instantiation", Instantiation_Node);
end if;
declare
Elmt : Elmt_Id := First_Elmt (Actual_Types);
begin
while Present (Elmt) loop
-- Even though the internal type appears as a subtype
-- of the actual, it inherits all operations and they
-- are immediately visible. This is equivalent to a use
-- type clause on the actual.
Append_To (Assoc,
Make_Use_Type_Clause (Sloc (First (Actuals)),
Subtype_Marks => New_List (New_Occurrence_Of
(Base_Type (Node (Elmt)), Sloc (First (Actuals))))));
Freeze_Before (Instantiation_Node, Node (Elmt));
Elmt := Next_Elmt (Elmt);
end loop;
end;
-- If there are default subprograms, normalize the tree by adding
-- explicit associations for them. This is required if the instance
-- appears within a generic.
declare
Elmt : Elmt_Id;
Subp : Entity_Id;
New_D : Node_Id;
begin
Elmt := First_Elmt (Defaults);
while Present (Elmt) loop
if No (Actuals) then
Actuals := New_List;
Set_Generic_Associations (I_Node, Actuals);
end if;
Subp := Node (Elmt);
New_D :=
Make_Generic_Association (Sloc (Subp),
Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
Explicit_Generic_Actual_Parameter =>
New_Occurrence_Of (Subp, Sloc (Subp)));
Mark_Rewrite_Insertion (New_D);
Append_To (Actuals, New_D);
Elmt := Next_Elmt (Elmt);
end loop;
end;
return Assoc;
end Analyze_Associations;
-------------------------------
-- Analyze_Formal_Array_Type --
-------------------------------
procedure Analyze_Formal_Array_Type
(T : in out Entity_Id;
Def : Node_Id)
is
DSS : Node_Id;
begin
-- Treated like a non-generic array declaration, with
-- additional semantic checks.
Enter_Name (T);
if Nkind (Def) = N_Constrained_Array_Definition then
DSS := First (Discrete_Subtype_Definitions (Def));
while Present (DSS) loop
if Nkind (DSS) = N_Subtype_Indication
or else Nkind (DSS) = N_Range
or else Nkind (DSS) = N_Attribute_Reference
then
Error_Msg_N ("only a subtype mark is allowed in a formal", Def);
end if;
DSS := Next (DSS);
end loop;
end if;
Array_Type_Declaration (T, Def);
Set_Is_Generic_Type (Base_Type (T));
if Ekind (Component_Type (T)) = E_Incomplete_Type
and then No (Full_View (Component_Type (T)))
then
Error_Msg_N ("premature usage of incomplete type", Def);
elsif Is_Internal (Component_Type (T)) then
Error_Msg_N
("only a subtype mark is allowed in a formal", Def);
end if;
end Analyze_Formal_Array_Type;
---------------------------------------------
-- Analyze_Formal_Decimal_Fixed_Point_Type --
---------------------------------------------
-- As for other generic types, we create a valid type representation
-- with legal but arbitrary attributes, whose values are never considered
-- static. For all scalar types we introduce an anonymous base type, with
-- the same attributes. We choose the corresponding integer type to be
-- Standard_Integer.
procedure Analyze_Formal_Decimal_Fixed_Point_Type
(T : Entity_Id;
Def : Node_Id)
is
Loc : constant Source_Ptr := Sloc (Def);
Base : constant Entity_Id :=
New_Internal_Entity
(E_Decimal_Fixed_Point_Type,
Current_Scope, Sloc (Def), 'G');
Int_Base : constant Entity_Id := Standard_Integer;
Delta_Val : constant Ureal := Ureal_1;
Digs_Val : constant Uint := Uint_6;
begin
Note_Feature (Generic_Formal_Decimal_Types, Loc);
Enter_Name (T);
Set_Etype (Base, Base);
Set_Size_Info (Base, (Int_Base));
Set_RM_Size (Base, RM_Size (Int_Base));
Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
Set_Digits_Value (Base, Digs_Val);
Set_Delta_Value (Base, Delta_Val);
Set_Small_Value (Base, Delta_Val);
Set_Scalar_Range (Base,
Make_Range (Loc,
Low_Bound => Make_Real_Literal (Loc, Ureal_1),
High_Bound => Make_Real_Literal (Loc, Ureal_1)));
Set_Is_Generic_Type (Base);
Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
Set_Etype (T, Base);
Set_Size_Info (T, (Int_Base));
Set_RM_Size (T, RM_Size (Int_Base));
Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
Set_Digits_Value (T, Digs_Val);
Set_Delta_Value (T, Delta_Val);
Set_Small_Value (T, Delta_Val);
Set_Scalar_Range (T, Scalar_Range (Base));
end Analyze_Formal_Decimal_Fixed_Point_Type;
---------------------------------
-- Analyze_Formal_Derived_Type --
---------------------------------
procedure Analyze_Formal_Derived_Type
(N : Node_Id;
T : Entity_Id;
Def : Node_Id)
is
Loc : constant Source_Ptr := Sloc (Def);
New_N : Node_Id;
Unk_Disc : Boolean := Unknown_Discriminants_Present (N);
begin
Note_Feature (Generic_Formal_Derived_Types, Loc);
Set_Is_Generic_Type (T);
if Private_Present (Def) then
New_N :=
Make_Private_Extension_Declaration (Loc,
Defining_Identifier => T,
Discriminant_Specifications => Discriminant_Specifications (N),
Unknown_Discriminants_Present => Unk_Disc,
Subtype_Indication => Subtype_Mark (Def));
Set_Abstract_Present (New_N, Abstract_Present (Def));
else
New_N :=
Make_Full_Type_Declaration (Loc,
Defining_Identifier => T,
Discriminant_Specifications =>
Discriminant_Specifications (Parent (T)),
Type_Definition =>
Make_Derived_Type_Definition (Loc,
Subtype_Indication => Subtype_Mark (Def)));
Set_Abstract_Present
(Type_Definition (New_N), Abstract_Present (Def));
end if;
Rewrite (N, New_N);
Analyze (N);
if Unk_Disc then
if not Is_Composite_Type (T) then
Error_Msg_N
("unknown discriminants not allowed for elementary types", N);
else
Set_Has_Unknown_Discriminants (T);
Set_Is_Constrained (T, False);
end if;
end if;
end Analyze_Formal_Derived_Type;
----------------------------------
-- Analyze_Formal_Discrete_Type --
----------------------------------
-- The operations defined for a discrete types are those of an
-- enumeration type. The size is set to an arbitrary value, for use
-- in analyzing the generic unit.
procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
Loc : constant Source_Ptr := Sloc (Def);
Lo : Node_Id;
Hi : Node_Id;
begin
Enter_Name (T);
Set_Ekind (T, E_Enumeration_Type);
Set_Etype (T, T);
Set_Esize (T, Uint_0);
Set_RM_Size (T, Uint_0);
-- For semantic analysis, the bounds of the type must be set to some
-- non-static value. The simplest is to create attribute nodes for
-- those bounds, that refer to the type itself. These bounds are never
-- analyzed but serve as place-holders.
Lo :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_First,
Prefix => New_Reference_To (T, Loc));
Set_Etype (Lo, T);
Hi :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Last,
Prefix => New_Reference_To (T, Loc));
Set_Etype (Hi, T);
Set_Scalar_Range (T,
Make_Range (Loc,
Low_Bound => Lo,
High_Bound => Hi));
end Analyze_Formal_Discrete_Type;
----------------------------------
-- Analyze_Formal_Floating_Type --
---------------------------------
procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
Base : constant Entity_Id :=
New_Internal_Entity
(E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
begin
-- The various semantic attributes are taken from the predefined type
-- Float, just so that all of them are initialized. Their values are
-- never used because no constant folding or expansion takes place in
-- the generic itself.
Enter_Name (T);
Set_Ekind (T, E_Floating_Point_Subtype);
Set_Etype (T, Base);
Set_Size_Info (T, (Standard_Float));
Set_Digits_Value (T, Digits_Value (Standard_Float));
Set_Scalar_Range (T, Scalar_Range (Standard_Float));
Set_Is_Generic_Type (Base);
Set_Etype (Base, Base);
Set_Size_Info (Base, (Standard_Float));
Set_Digits_Value (Base, Digits_Value (Standard_Float));
Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
end Analyze_Formal_Floating_Type;
---------------------------------
-- Analyze_Formal_Modular_Type --
---------------------------------
procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
begin
-- Apart from their entity kind, generic modular types are treated
-- like signed integer types, and have the same attributes.
Analyze_Formal_Signed_Integer_Type (T, Def);
Set_Ekind (T, E_Modular_Integer_Subtype);
Set_Ekind (Etype (T), E_Modular_Integer_Type);
end Analyze_Formal_Modular_Type;
---------------------------------------
-- Analyze_Formal_Object_Declaration --
---------------------------------------
procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
E : constant Node_Id := Expression (N);
Id : Node_Id := Defining_Identifier (N);
K : Entity_Kind;
T : Node_Id;
begin
Enter_Name (Id);
-- Determine the mode of the formal object
if Out_Present (N) then
K := E_Generic_In_Out_Parameter;
if not In_Present (N) then
Error_Msg_N ("formal generic objects cannot have mode OUT", N);
end if;
else
K := E_Generic_In_Parameter;
end if;
Find_Type (Subtype_Mark (N));
T := Entity (Subtype_Mark (N));
if Ekind (T) = E_Incomplete_Type then
Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
end if;
if K = E_Generic_In_Parameter then
if Is_Limited_Type (T) then
Error_Msg_N
("generic formal of mode IN must not be of limited type", N);
end if;
if Is_Abstract (T) then
Error_Msg_N
("generic formal of mode IN must not be of abstract type", N);
end if;
if Present (E) then
Analyze_Default_Expression (E, T);
end if;
Set_Ekind (Id, K);
Set_Etype (Id, T);
-- Case of generic IN OUT parameter.
else
-- If the formal has an unconstrained type, construct its
-- actual subtype, as is done for subprogram formals. In this
-- fashion, all its uses can refer to specific bounds.
Set_Ekind (Id, K);
Set_Etype (Id, T);
if (Is_Array_Type (T)
and then not Is_Constrained (T))
or else
(Ekind (T) = E_Record_Type
and then Has_Discriminants (T))
then
declare
Non_Freezing_Ref : constant Node_Id :=
New_Reference_To (Id, Sloc (Id));
Decl : Node_Id;
begin
-- Make sure that the actual subtype doesn't generate
-- bogus freezing.
Set_Must_Not_Freeze (Non_Freezing_Ref);
Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
Insert_Before_And_Analyze (N, Decl);
Set_Actual_Subtype (Id, Defining_Identifier (Decl));
end;
else
Set_Actual_Subtype (Id, T);
end if;
if Present (E) then
Error_Msg_N
("initialization not allowed for `IN OUT` formals", N);
end if;
end if;
end Analyze_Formal_Object_Declaration;
----------------------------------------------
-- Analyze_Formal_Ordinary_Fixed_Point_Type --
----------------------------------------------
procedure Analyze_Formal_Ordinary_Fixed_Point_Type
(T : Entity_Id;
Def : Node_Id)
is
Loc : constant Source_Ptr := Sloc (Def);
Base : constant Entity_Id :=
New_Internal_Entity
(E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
begin
-- The semantic attributes are set for completeness only, their
-- values will never be used, because all properties of the type
-- are non-static.
Enter_Name (T);
Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
Set_Etype (T, Base);
Set_Size_Info (T, (Standard_Integer));
Set_RM_Size (T, RM_Size (Standard_Integer));
Set_Small_Value (T, Ureal_1);
Set_Delta_Value (T, Ureal_1);
Set_Scalar_Range (T,
Make_Range (Loc,
Low_Bound => Make_Real_Literal (Loc, Ureal_1),
High_Bound => Make_Real_Literal (Loc, Ureal_1)));
Set_Is_Generic_Type (Base);
Set_Etype (Base, Base);
Set_Size_Info (Base, (Standard_Integer));
Set_RM_Size (Base, RM_Size (Standard_Integer));
Set_Small_Value (Base, Ureal_1);
Set_Delta_Value (Base, Ureal_1);
Set_Scalar_Range (Base, Scalar_Range (T));
end Analyze_Formal_Ordinary_Fixed_Point_Type;
----------------------------
-- Analyze_Formal_Package --
----------------------------
procedure Analyze_Formal_Package (N : Node_Id) is
Formal : Entity_Id := Defining_Identifier (N);
Gen_Id : constant Node_Id := Name (N);
Gen_Decl : Node_Id;
Gen_Unit : Entity_Id;
New_N : Node_Id;
Parent_Installed : Boolean := False;
Save_Exchanged_Views : Elist_Id;
Save_Instantiated_Parent : Assoc;
begin
Note_Feature (Generic_Formal_Packages, Sloc (N));
Text_IO_Kludge (Gen_Id);
Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
Gen_Unit := Entity (Gen_Id);
if Ekind (Gen_Unit) /= E_Generic_Package then
Error_Msg_N ("expect generic package name", Gen_Id);
return;
end if;
-- Check for a formal package that is a package renaming.
if Present (Renamed_Object (Gen_Unit)) then
Gen_Unit := Renamed_Object (Gen_Unit);
end if;
-- The formal package is treated like a regular instance, but only
-- the specification needs to be instantiated, to make entities visible.
if not Box_Present (N) then
Analyze_Package_Instantiation (N);
else
-- If there are no generic associations, the generic parameters
-- appear as local entities and are instantiated like them. We copy
-- the generic package declaration as if it were an instantiation,
-- and analyze it like a regular package, except that we treat the
-- formals as additional visible components.
Save_Exchanged_Views := Exchanged_Views;
Exchanged_Views := New_Elmt_List;
Gen_Decl := Get_Declaration_Node (Gen_Unit);
New_N :=
Copy_Generic_Node
(Original_Node (Gen_Decl), Empty, Instantiating => True);
Set_Defining_Unit_Name (Specification (New_N), Formal);
Rewrite (N, New_N);
Formal := Defining_Unit_Name (Specification (N));
Enter_Name (Formal);
Set_Ekind (Formal, E_Generic_Package);
Set_Etype (Formal, Standard_Void_Type);
Set_Inner_Instances (Formal, New_Elmt_List);
New_Scope (Formal);
Save_Instantiated_Parent := Current_Instantiated_Parent;
Current_Instantiated_Parent := (Gen_Unit, Formal, Assoc_Null);
Analyze_Generic_Formal_Part (N);
Analyze (Specification (N));
End_Package_Scope (Formal);
Exchanged_Views := Save_Exchanged_Views;
Current_Instantiated_Parent := Save_Instantiated_Parent;
-- Inside the generic unit, the formal package is a regular
-- package, but no body is needed for it. Note that after
-- instantiation, the defining_unit_name we need is in the
-- new tree and not in the original. (see Package_Instantiation).
-- A generic formal package is an instance, and can be used as
-- an actual for an inner instance. Mark its generic parent.
Set_Ekind (Formal, E_Package);
Set_Generic_Parent (Specification (N), Gen_Unit);
Set_Has_Completion (Formal, True);
end if;
if Parent_Installed then
Remove_Parent;
end if;
end Analyze_Formal_Package;
---------------------------------
-- Analyze_Formal_Private_Type --
---------------------------------
procedure Analyze_Formal_Private_Type
(N : Node_Id;
T : Entity_Id;
Def : Node_Id)
is
begin
New_Private_Type (N, T, Def);
-- Set the size to an arbitrary but legal value.
Set_Esize (T, Esize (Standard_Integer));
end Analyze_Formal_Private_Type;
----------------------------------------
-- Analyze_Formal_Signed_Integer_Type --
----------------------------------------
procedure Analyze_Formal_Signed_Integer_Type
(T : Entity_Id;
Def : Node_Id)
is
Base : constant Entity_Id :=
New_Internal_Entity
(E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
begin
Enter_Name (T);
Set_Ekind (T, E_Signed_Integer_Subtype);
Set_Etype (T, Base);
Set_Size_Info (T, (Standard_Integer));
Set_RM_Size (T, Esize (Standard_Integer));
Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
Set_Is_Generic_Type (Base);
Set_Size_Info (Base, (Standard_Integer));
Set_RM_Size (Base, Esize (Standard_Integer));
Set_Etype (Base, Base);
Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
end Analyze_Formal_Signed_Integer_Type;
-------------------------------
-- Analyze_Formal_Subprogram --
-------------------------------
procedure Analyze_Formal_Subprogram (N : Node_Id) is
Spec : constant Node_Id := Specification (N);
Def : constant Node_Id := Default_Name (N);
Nam : constant Entity_Id := Defining_Unit_Name (Spec);
Subp : Entity_Id;
begin
if Nkind (Nam) = N_Defining_Program_Unit_Name then
Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
return;
end if;
Analyze_Subprogram_Declaration (N);
Set_Is_Formal_Subprogram (Nam);
Set_Has_Completion (Nam);
-- Default name is resolved at the point of instantiation
if Box_Present (N) then
null;
-- Else default is bound at the point of generic declaration
elsif Present (Def) then
if Nkind (Def) = N_Operator_Symbol then
Find_Direct_Name (Def);
elsif Nkind (Def) /= N_Attribute_Reference then
Analyze (Def);
else
-- For an attribute reference, analyze the prefix and verify
-- that it has the proper profile for the subprogram.
Analyze (Prefix (Def));
Valid_Default_Attribute (Nam, Def);
return;
end if;
-- Default name may be overloaded, in which case the interpretation
-- with the correct profile must be selected, as for a renaming.
if Etype (Def) = Any_Type then
return;
elsif Nkind (Def) = N_Selected_Component then
Subp := Entity (Selector_Name (Def));
if Ekind (Subp) /= E_Entry then
Error_Msg_N ("expect valid subprogram name as default", Def);
return;
end if;
elsif Nkind (Def) = N_Indexed_Component then
if Nkind (Prefix (Def)) /= N_Selected_Component then
Error_Msg_N ("expect valid subprogram name as default", Def);
return;
else
Subp := Entity (Selector_Name (Prefix (Def)));
if Ekind (Subp) /= E_Entry_Family then
Error_Msg_N ("expect valid subprogram name as default", Def);
return;
end if;
end if;
elsif Nkind (Def) = N_Character_Literal then
-- Needs some type checks: subprogram should be parameterless???
Resolve (Def, (Etype (Nam)));
elsif (not Is_Entity_Name (Def)
or else not Is_Overloadable (Entity (Def)))
then
Error_Msg_N ("expect valid subprogram name as default", Def);
return;
elsif not Is_Overloaded (Def) then
Subp := Entity (Def);
if Subp = Nam then
Error_Msg_N ("premature usage of formal subprogram", Def);
elsif not Entity_Matches_Spec (Subp, Nam) then
Error_Msg_N ("no visible entity matches specification", Def);
end if;
else
declare
I : Interp_Index;
I1 : Interp_Index;
It : Interp;
It1 : Interp;
begin
Subp := Any_Id;
Get_First_Interp (Def, I, It);
while Present (It.Nam) loop
if Entity_Matches_Spec (It.Nam, Nam) then
if Subp /= Any_Id then
It1 := Disambiguate (Def, I1, I, Etype (Subp));
if It1 = No_Interp then
Error_Msg_N ("ambiguous default subprogram", Def);
else
Subp := It1.Nam;
end if;
exit;
else
I1 := I;
Subp := It.Nam;
end if;
end if;
Get_Next_Interp (I, It);
end loop;
end;
if Subp /= Any_Id then
Set_Entity (Def, Subp);
if Subp = Nam then
Error_Msg_N ("premature usage of formal subprogram", Def);
elsif Ekind (Subp) /= E_Operator then
Check_Mode_Conformant (Subp, Nam);
end if;
else
Error_Msg_N ("no visible subprogram matches specification", N);
end if;
end if;
end if;
end Analyze_Formal_Subprogram;
-----------------------------
-- Valid_Default_Attribute --
-----------------------------
procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
Attr_Id : constant Attribute_Id :=
Get_Attribute_Id (Attribute_Name (Def));
F : Entity_Id;
Num_F : Int;
T : Entity_Id := Entity (Prefix (Def));
OK : Boolean;
Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
begin
if No (T)
or else T = Any_Id
then
return;
end if;
Num_F := 0;
F := First_Formal (Nam);
while Present (F) loop
Num_F := Num_F + 1;
F := Next_Formal (F);
end loop;
case Attr_Id is
when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
Attribute_Floor | Attribute_Fraction | Attribute_Machine |
Attribute_Model | Attribute_Remainder | Attribute_Rounding |
Attribute_Unbiased_Rounding =>
OK := (Is_Fun and then Num_F = 1 and then Is_Floating_Point_Type (T));
when Attribute_Image | Attribute_Pred | Attribute_Succ |
Attribute_Value | Attribute_Wide_Image |
Attribute_Wide_Value =>
OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
when Attribute_Max | Attribute_Min =>
OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
when Attribute_Input =>
OK := (Is_Fun and then Num_F = 1);
when Attribute_Output | Attribute_Read | Attribute_Write =>
OK := (not Is_Fun and then Num_F = 2);
when others => OK := False;
end case;
if not OK then
Error_Msg_N ("attribute reference has wrong profile for subprogram",
Def);
end if;
end Valid_Default_Attribute;
-------------------------------------
-- Analyze_Formal_Type_Declaration --
-------------------------------------
procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
Def : constant Node_Id := Formal_Type_Definition (N);
T : Entity_Id;
begin
T := Defining_Identifier (N);
if Present (Discriminant_Specifications (N))
and then Nkind (Def) /= N_Formal_Private_Type_Definition
then
Error_Msg_N
("discriminants not allowed for this formal type",
Defining_Identifier (First (Discriminant_Specifications (N))));
end if;
-- Enter the new name, and branch to specific routine.
case Nkind (Def) is
when N_Formal_Private_Type_Definition
=> Analyze_Formal_Private_Type (N, T, Def);
when N_Formal_Derived_Type_Definition
=> Analyze_Formal_Derived_Type (N, T, Def);
when N_Formal_Discrete_Type_Definition
=> Analyze_Formal_Discrete_Type (T, Def);
when N_Formal_Signed_Integer_Type_Definition
=> Analyze_Formal_Signed_Integer_Type (T, Def);
when N_Formal_Modular_Type_Definition
=> Analyze_Formal_Modular_Type (T, Def);
when N_Formal_Floating_Point_Definition
=> Analyze_Formal_Floating_Type (T, Def);
when N_Formal_Ordinary_Fixed_Point_Definition
=> Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
when N_Formal_Decimal_Fixed_Point_Definition
=> Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
when N_Array_Type_Definition
=> Analyze_Formal_Array_Type (T, Def);
when N_Access_To_Object_Definition |
N_Access_Function_Definition |
N_Access_Procedure_Definition
=> Analyze_Generic_Access_Type (T, Def);
when others =>
pragma Assert (False);
raise Program_Error;
end case;
Set_Is_Generic_Type (T);
end Analyze_Formal_Type_Declaration;
---------------------------------
-- Analyze_Generic_Access_Type --
---------------------------------
procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
begin
Enter_Name (T);
if Nkind (Def) = N_Access_To_Object_Definition then
Access_Type_Declaration (T, Def);
if Is_Incomplete_Or_Private_Type (Designated_Type (T))
and then No (Full_View (Designated_Type (T)))
and then not Is_Generic_Type (Designated_Type (T))
then
Error_Msg_N ("premature usage of incomplete type", Def);
elsif Is_Internal (Designated_Type (T)) then
Error_Msg_N
("only a subtype mark is allowed in a formal", Def);
end if;
else
Access_Subprogram_Declaration (T, Def);
end if;
end Analyze_Generic_Access_Type;
---------------------
-- Associated_Node --
---------------------
function Associated_Node (N : Node_Id) return Node_Id is
Assoc : Node_Id := Node4 (N);
-- ??? what is Node4 being used for here?
begin
if Nkind (Assoc) /= Nkind (N) then
return Assoc;
elsif Nkind (Assoc) = N_Aggregate
or else Nkind (Assoc) = N_Extension_Aggregate
then
return Assoc;
else
-- If the node is part of an inner generic, it may itself have been
-- remapped into a further generic copy. Node4 is otherwise used for
-- the entity of the node, and will be of a different node kind, or
-- else N has been rewritten as a literal or function call.
while Present (Node4 (Assoc))
and then Nkind (Node4 (Assoc)) = Nkind (Assoc)
loop
Assoc := Node4 (Assoc);
end loop;
-- Follow and additional link in case the final node was rewritten.
-- This can only happen with nested generic units.
if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
and then Present (Node4 (Assoc))
and then (Nkind (Node4 (Assoc)) = N_Function_Call
or else Nkind (Node4 (Assoc)) = N_Explicit_Dereference
or else Nkind (Node4 (Assoc)) = N_Integer_Literal
or else Nkind (Node4 (Assoc)) = N_Real_Literal
or else Nkind (Node4 (Assoc)) = N_String_Literal)
then
Assoc := Node4 (Assoc);
end if;
return Assoc;
end if;
end Associated_Node;
-------------------------------------------
-- Build_Instance_Compilation_Unit_Nodes --
-------------------------------------------
procedure Build_Instance_Compilation_Unit_Nodes
(N : Node_Id;
Act_Body : Node_Id;
Act_Decl : Node_Id)
is
Decl_Cunit : Node_Id;
Body_Cunit : Node_Id;
Citem : Node_Id;
begin
-- A new compilation unit node is built for the instance declaration
Decl_Cunit :=
Make_Compilation_Unit (Sloc (N),
Context_Items => Empty_List,
Unit => Act_Decl,
Aux_Decls_Node =>
Make_Compilation_Unit_Aux (Sloc (N)));
Set_Parent_Spec (Act_Decl, Parent_Spec (N));
Set_Body_Required (Decl_Cunit, True);
-- We use the original instantiation compilation unit as the resulting
-- compilation unit of the instance, since this is the main unit.
Rewrite (N, Act_Body);
Body_Cunit := Parent (N);
-- The two compilation unit nodes are linked by the Library_Unit field
Set_Library_Unit (Decl_Cunit, Body_Cunit);
Set_Library_Unit (Body_Cunit, Decl_Cunit);
-- The context clause items on the instantiation, which are now
-- attached to the body compilation unit (since the body overwrote
-- the orginal instantiation node), semantically belong on the spec,
-- so copy them there. It's harmless to leave them on the body as well.
-- In fact one could argue that they belong in both places.
Citem := First (Context_Items (Body_Cunit));
while Present (Citem) loop
Append (New_Copy (Citem), Context_Items (Decl_Cunit));
Citem := Next (Citem);
end loop;
-- Make entry in Units table, so that binder can generate call to
-- elaboration procedure for body, if any.
Make_Instance_Unit (Body_Cunit);
Main_Unit_Entity := Defining_Entity (Act_Decl);
Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
end Build_Instance_Compilation_Unit_Nodes;
---------------------------
-- Check_Generic_Actuals --
---------------------------
-- The visibility of the actuals may be different between the
-- point of generic instantiation and the instantiation of the body.
procedure Check_Generic_Actuals
(Instance : Entity_Id;
Is_Formal_Box : Boolean)
is
E : Entity_Id;
Astype : Entity_Id;
begin
E := First_Entity (Instance);
while Present (E) loop
if Is_Type (E)
and then Nkind (Parent (E)) = N_Subtype_Declaration
and then Scope (Etype (E)) /= Instance
and then Is_Entity_Name (Subtype_Indication (Parent (E)))
then
Check_Private_View (Subtype_Indication (Parent (E)));
Set_Is_Generic_Actual_Type (E, True);
Set_Is_Private (E, False);
-- We constructed the generic actual type as a subtype of
-- the supplied type. This means that it normally would not
-- inherit subtype specific attributes of the actual, which
-- is wrong for the generic case.
Astype := Ancestor_Subtype (E);
Set_Size_Info (E, (Astype));
Set_First_Rep_Item (E, First_Rep_Item (Astype));
if Is_Discrete_Or_Fixed_Point_Type (E) then
Set_RM_Size (E, RM_Size (Astype));
-- In nested instances, the base type of an access actual
-- may itself be private, and need to be exchanged.
elsif Is_Access_Type (E)
and then Is_Private_Type (Etype (E))
then
Check_Private_View
(New_Occurrence_Of (Etype (E), Sloc (Instance)));
end if;
elsif Ekind (E) = E_Package then
-- If this is the renaming for the current instance, we're done.
-- Otherwise it is a formal package. If the corresponding formal
-- was declared with a box, the (instantiations of the) generic
-- formal part are also visible. Otherwise, ignore the entity
-- created to validate the actuals.
if Renamed_Object (E) = Instance then
exit;
elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
null;
-- The visibility of a formal of an enclosing generic is already
-- correct.
elsif Denotes_Formal_Package (E) then
null;
elsif Present (Associated_Formal_Package (E))
and then Box_Present (Parent (Associated_Formal_Package (E)))
then
Check_Generic_Actuals (Renamed_Object (E), True);
Set_Is_Private (E, False);
end if;
else
Set_Is_Private (E, not Is_Formal_Box);
end if;
E := Next_Entity (E);
end loop;
end Check_Generic_Actuals;
------------------------
-- Check_Private_View --
------------------------
procedure Check_Private_View (N : Node_Id) is
T : constant Entity_Id := Etype (N);
begin
if Present (T) then
if Is_Private_Type (T)
and then not Has_Private_View (N)
and then Present (Full_View (T))
then
-- In the generic, the full type was visible. Save the
-- private entity, for subsequent exchange.
Switch_View (T);
elsif Has_Private_View (N)
and then not Is_Private_Type (T)
and then not Has_Been_Exchanged (T)
and then Etype (Associated_Node (N)) /= T
then
-- Only the private declaration was visible in the generic.
Append_Elmt (T, Exchanged_Views);
Exchange_Declarations (Etype (Associated_Node (N)));
elsif Is_Access_Type (T)
and then Is_Private_Type (Designated_Type (T))
and then Present (Full_View (Designated_Type (T)))
then
Switch_View (Designated_Type (T));
elsif Is_Array_Type (T)
and then Is_Private_Type (Component_Type (T))
and then Present (Full_View (Component_Type (T)))
then
Switch_View (Component_Type (T));
end if;
end if;
end Check_Private_View;
-----------------
-- Switch_View --
-----------------
procedure Switch_View (T : Entity_Id) is
Priv_Elmt : Elmt_Id := No_Elmt;
Priv_Sub : Entity_Id;
BT : Entity_Id := Base_Type (T);
begin
-- T may be private but its base type may have been exchanged through
-- some other occurrence, in which case there is nothing to switch.
if not Is_Private_Type (BT) then
return;
end if;
Priv_Elmt := First_Elmt (Private_Dependents (BT));
Append_Elmt (Full_View (BT), Exchanged_Views);
Exchange_Declarations (BT);
while Present (Priv_Elmt) loop
Priv_Sub := (Node (Priv_Elmt));
-- We avoid flipping the subtype if the Etype of its full
-- view is private because this would result in a malformed
-- subtype. This occurs when the Etype of the subtype full
-- view is the full view of the base type (and since the
-- base types were just switched, the subtype is pointing
-- to the wrong view). This is currently the case for
-- tagged record types, access types (maybe more?) and
-- needs to be resolved. ???
if Present (Full_View (Priv_Sub))
and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
then
Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
Exchange_Declarations (Priv_Sub);
end if;
Priv_Elmt := Next_Elmt (Priv_Elmt);
end loop;
end Switch_View;
------------------------
-- Has_Been_Exchanged --
------------------------
function Has_Been_Exchanged (E : Entity_Id) return boolean is
Next : Elmt_Id := First_Elmt (Exchanged_Views);
begin
while Present (Next) loop
if Full_View (Node (Next)) = E then
return TRUE;
end if;
Next := Next_Elmt (Next);
end loop;
return FALSE;
end Has_Been_Exchanged;
-----------------------
-- Copy_Generic_Node --
-----------------------
function Copy_Generic_Node
(N : Node_Id;
Parent_Id : Node_Id;
Instantiating : Boolean)
return Node_Id
is
New_N : Node_Id;
function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
-- Check the given value of one of the Fields referenced by the
-- current node to determine whether to copy it recursively. The
-- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
-- value (Sloc, Uint, Char) in which case it need not be copied.
function Copy_Generic_List
(L : List_Id;
Parent_Id : Node_Id)
return List_Id;
-- Apply Copy_Node recursively to the members of a node list.
function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
-- Make copy of element list.
-----------------------------
-- Copy_Generic_Descendant --
-----------------------------
function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
begin
if D = Union_Id (Empty) then
return D;
elsif D in Node_Range then
return Union_Id
(Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
elsif D in List_Range then
return Union_Id (Copy_Generic_List (List_Id (D), New_N));
elsif D in Elist_Range then
return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
-- Nothing else is copyable (e.g. Uint values), return as is
else
return D;
end if;
end Copy_Generic_Descendant;
-----------------------
-- Copy_Generic_List --
-----------------------
function Copy_Generic_List
(L : List_Id;
Parent_Id : Node_Id)
return List_Id
is
N : Node_Id;
New_L : List_Id;
begin
if Present (L) then
New_L := New_List;
Set_Parent (New_L, Parent_Id);
N := First (L);
while Present (N) loop
Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
N := Next (N);
end loop;
return New_L;
else
return No_List;
end if;
end Copy_Generic_List;
------------------------
-- Copy_Generic_Elist --
------------------------
function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
M : Elmt_Id;
L : Elist_Id;
begin
if Present (E) then
L := New_Elmt_List;
M := First_Elmt (E);
while Present (M) loop
Append_Elmt
(Copy_Generic_Node (Node (M), Empty, Instantiating), L);
M := Next_Elmt (M);
end loop;
return L;
else
return No_Elist;
end if;
end Copy_Generic_Elist;
-- Start of processing for Copy_Generic_Node
begin
if N = Empty then
return N;
end if;
New_N := New_Copy (N);
if Instantiating then
Adjust_Instantiation_Sloc (New_N, S_Adjustment);
end if;
if not Is_List_Member (N) then
Set_Parent (New_N, Parent_Id);
end if;
-- If defining identifier, then all fields have been copied already
if Nkind (New_N) in N_Entity then
null;
-- Special casing for identifiers and other entity names and operators
elsif (Nkind (New_N) = N_Identifier
or else Nkind (New_N) = N_Character_Literal
or else Nkind (New_N) = N_Expanded_Name
or else Nkind (New_N) = N_Operator_Symbol
or else Nkind (New_N) in N_Op)
then
if not Instantiating then
-- Link both nodes in order to assign subsequently the
-- entity of the copy to the original node, in case this
-- is a global reference.
Set_Associated_Node (N, New_N);
-- If we are within an instantiation, this is a nested generic
-- that has already been analyzed at the point of definition. We
-- must preserve references that were global to the enclosing
-- parent at that point. Other occurrences, whether global or
-- local to the current generic, must be resolved anew, so we
-- reset the entity in the generic copy. A global reference has
-- a smaller depth than the parent, or else the same depth in
-- case both are distinct compilation units.
-- It is also possible for Current_Instantiated_Parent to be
-- defined, and for this not to be a nested generic, namely
-- if the unit is loaded through Rtsfind. In that case, the
-- entity of New_N is only a link to the associated node, and
-- not a defining occurrence.
if No (Current_Instantiated_Parent.Gen_Id)
or else No (Entity (New_N))
or else
not (Nkind (Entity (New_N)) = N_Defining_Identifier
or else
Nkind (Entity (New_N)) = N_Defining_Character_Literal
or else
Nkind (Entity (New_N)) = N_Defining_Operator_Symbol)
or else No (Scope (Entity (New_N)))
or else Scope (Entity (New_N)) =
Current_Instantiated_Parent.Gen_Id
or else (Scope_Depth (Scope (Entity (New_N))) >
Scope_Depth (Current_Instantiated_Parent.Gen_Id)
and then
Get_Sloc_Unit_Number (Sloc (Entity (New_N))) =
Get_Sloc_Unit_Number
(Sloc (Current_Instantiated_Parent.Gen_Id)))
then
Set_Associated_Node (New_N, Empty);
end if;
-- Case of instantiating identifier or some other name or operator
else
-- If the associated node is still defined, the entity in
-- it is global, and must be copied to the instance.
if Present (Associated_Node (N)) then
if Nkind (Associated_Node (N)) = Nkind (N) then
Set_Entity (New_N, Entity (Associated_Node (N)));
Check_Private_View (N);
elsif Nkind (Associated_Node (N)) = N_Function_Call then
Set_Entity (New_N, Entity (Name (Associated_Node (N))));
else
Set_Entity (New_N, Empty);
end if;
end if;
end if;
-- For expanded name, we must copy the Prefix and Selector_Name
if Nkind (N) = N_Expanded_Name then
Set_Prefix
(New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
Set_Selector_Name (New_N,
Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
-- For operators, we must copy the right operand
elsif Nkind (N) in N_Op then
Set_Right_Opnd (New_N,
Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
-- And for binary operators, the left operand as well
if Nkind (N) in N_Binary_Op then
Set_Left_Opnd (New_N,
Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
end if;
end if;
-- Special casing for stubs
elsif Nkind (N) in N_Body_Stub then
-- In any case, we must copy the specification or defining
-- identifier as appropriate.
if Nkind (N) = N_Subprogram_Body_Stub then
Set_Specification (New_N,
Copy_Generic_Node (Specification (N), New_N, Instantiating));
else
Set_Defining_Identifier (New_N,
Copy_Generic_Node
(Defining_Identifier (N), New_N, Instantiating));
end if;
-- If we are not instantiating, then this is where we load and
-- analyze subunits, i.e. at the point where the stub occurs. A
-- more permissivle system might defer this analysis to the point
-- of instantiation, but this seems to complicated for now.
if not Instantiating then
declare
Context : List_Id;
Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
Subunit : Node_Id;
New_Subunit : Node_Id;
Unum : Unit_Number_Type;
New_Body : Node_Id;
Lib : Node_Id;
begin
Unum := Load_Unit (Subunit_Name, False, N);
-- If the proper body is not found, a warning message will
-- be emitted when analyzing the stub, or later at the the
-- point of instantiation. Here we just leave the stub as is.
if Unum = No_Unit then
Subunits_Missing := True;
goto Subunit_Not_Found;
end if;
Subunit := Cunit (Unum);
-- We must create a generic copy of the subunit, in order
-- to perform semantic analysis on it, and we must replace
-- the stub in the original generic unit with the subunit,
-- in order to preserve non-local references within.
-- Only the proper body needs to be copied. Library_Unit and
-- context clause are simply inherited by the generic copy.
-- Note that the copy (which may be recursive if there are
-- nested subunits) must be done first, before attaching it
-- to the enclosing generic.
New_Body :=
Copy_Generic_Node
(Proper_Body (Unit (Subunit)),
Empty, Instantiating => False);
-- Now place the original proper body in the original
-- generic unit.
Rewrite (N, Proper_Body (Unit (Subunit)));
Set_Was_Originally_Stub (N);
-- Finally replace the body of the subunit with its copy,
-- and make this new subunit into the library unit of the
-- generic copy, which does not have stubs any longer.
Set_Proper_Body (Unit (Subunit), New_Body);
Set_Library_Unit (New_N, Subunit);
Inherit_Context (Unit (Subunit), N);
end;
-- If we are instantiating, this must be an error case, since
-- otherwise we would have replaced the stub node by the proper
-- body that corresponds. So just ignore it in the copy (i.e.
-- we have copied it, and that is good enough).
else
null;
end if;
<<Subunit_Not_Found>> null;
-- If the node is a compilation unit, it is the subunit of a stub,
-- which has been loaded already (see code below). In this case,
-- the library unit field of N points to the parent unit (which
-- is a compilation unit) and need not (and cannot!) be copied.
-- When the proper body of the stub is analyzed, thie library_unit
-- link is used to establish the proper context (see sem_ch10).
-- The other fields of a compilation unit are copied as usual
elsif Nkind (N) = N_Compilation_Unit then
-- This code can only be executed when not instantiating, because
-- in the copy made for an instantiation, the compilation unit
-- node has disappeared at the point that a stub is replaced by
-- its proper body.
pragma Assert (not Instantiating);
Set_Context_Items (New_N,
Copy_Generic_List (Context_Items (N), New_N));
Set_Unit (New_N,
Copy_Generic_Node (Unit (N), New_N, False));
Set_First_Inlined_Subprogram (New_N,
Copy_Generic_Node
(First_Inlined_Subprogram (N), New_N, False));
Set_Aux_Decls_Node (New_N,
Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
-- For an assignment node, the assignment is known to be semantically
-- legal if we are instantiating the template. This avoids incorrect
-- diagnostics in generated code.
elsif Nkind (N) = N_Assignment_Statement then
-- Copy name and expression fields in usual manner
Set_Name (New_N,
Copy_Generic_Node (Name (N), New_N, Instantiating));
Set_Expression (New_N,
Copy_Generic_Node (Expression (N), New_N, Instantiating));
if Instantiating then
Set_Assignment_OK (Name (New_N), True);
end if;
elsif Nkind (N) = N_Aggregate
or else Nkind (N) = N_Extension_Aggregate
then
if not Instantiating then
Set_Associated_Node (N, New_N);
else
if Present (Associated_Node (N))
and then Nkind (Associated_Node (N)) = Nkind (N)
then
-- In the generic the aggregate has some composite type.
-- If at the point of instantiation the type has a private
-- view, install the full view (and that of its ancestors,
-- if any).
declare
T : Entity_Id := (Etype (Associated_Node (New_N)));
Rt : Entity_Id;
begin
if Present (T)
and then Is_Private_Type (T)
then
Switch_View (T);
end if;
if Present (T)
and then Is_Tagged_Type (T)
and then Is_Derived_Type (T)
then
Rt := Root_Type (T);
loop
T := Etype (T);
if Is_Private_Type (T) then
Switch_View (T);
end if;
exit when T = Rt;
end loop;
end if;
end;
end if;
end if;
Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
-- For a proper body, we must catch the case of a proper body that
-- replaces a stub. This represents the point at which a separate
-- compilation unit, and hence template file, may be referenced, so
-- we must make a new source instantiation entry for the template
-- of the subunit, and ensure that all nodes in the subunit are
-- adjusted using this new source instantiation entry.
elsif Nkind (N) in N_Proper_Body then
declare
Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
begin
if Instantiating and then Was_Originally_Stub (N) then
Create_Instantiation_Source
(Instantiation_Node, Defining_Entity (N), S_Adjustment);
end if;
-- Now copy the fields of the proper body, using the new
-- adjustment factor if one was needed as per test above.
Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
-- Restore the original adjustment factor in case changed
S_Adjustment := Save_Adjustment;
end;
-- For the remaining nodes, copy recursively their descendants.
else
Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
end if;
return New_N;
end Copy_Generic_Node;
------------------------------
-- Check_Generic_Child_Unit --
------------------------------
procedure Check_Generic_Child_Unit
(Gen_Id : Node_Id;
Parent_Installed : in out Boolean)
is
Loc : constant Source_Ptr := Sloc (Gen_Id);
Gen_Par : Entity_Id := Empty;
Inst_Par : Entity_Id;
E : Entity_Id;
S : Node_Id;
function Find_Generic_Child
(Scop : Entity_Id;
Id : Node_Id)
return Entity_Id;
-- Search generic parent for possible child unit.
function Find_Generic_Child
(Scop : Entity_Id;
Id : Node_Id)
return Entity_Id
is
E : Entity_Id;
begin
E := First_Entity (Scop);
while Present (E) loop
if Chars (E) = Chars (Id)
and then Is_Child_Unit (E)
then
return E;
end if;
E := Next_Entity (E);
end loop;
return Empty;
end Find_Generic_Child;
-- Start of processing for Check_Generic_Child_Unit
begin
-- If the name of the generic is given by a selected component, it
-- may be the name of a generic child unit, and the prefix the name
-- of an instance of the parent, in which case the child unit must be
-- visible. If this instance is not in scope, it must be placed there
-- and removed after instantiation, because what is being instantiated
-- is not the original child, but the corresponding child present in
-- the instance of the parent.
-- If the child is instantiated within the parent, it can be given by
-- a simple name. In this case the instance is already in scope, but
-- the child generic must be recovered from the generic parent as well.
if Nkind (Gen_Id) = N_Selected_Component then
S := Selector_Name (Gen_Id);
Analyze (Prefix (Gen_Id));
Inst_Par := Entity (Prefix (Gen_Id));
if Ekind (Inst_Par) = E_Package
and then Present (Renamed_Object (Inst_Par))
then
Inst_Par := Renamed_Object (Inst_Par);
end if;
if Ekind (Inst_Par) = E_Package then
if Nkind (Parent (Inst_Par)) = N_Package_Specification then
Gen_Par := Generic_Parent (Parent (Inst_Par));
elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
and then
Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
then
Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
end if;
elsif Ekind (Inst_Par) = E_Generic_Package
and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
then
-- A formal package may be a real child package, and not the
-- implicit instance within a parent. In this case the child is
-- not visible and has to be retrieved explicitly as well.
Gen_Par := Inst_Par;
end if;
if Present (Gen_Par) then
-- The prefix denotes an instantiation. The entity itself
-- may be a nested generic, or a child unit.
E := Find_Generic_Child (Gen_Par, S);
if Present (E) then
Change_Selected_Component_To_Expanded_Name (Gen_Id);
Set_Entity (Gen_Id, E);
Set_Etype (Gen_Id, Etype (E));
Set_Entity (S, E);
Set_Etype (S, Etype (E));
-- A common mistake is to replicate the naming scheme of
-- a hierarchy by instantiating a generic child directly,
-- rather than the implicit child in a parent instance:
--
-- generic .. package Gpar is ..
-- generic .. package Gpar.Child is ..
-- package Par is new Gpar ();
-- with Gpar.Child;
-- package Par.Child is new Gpar.Child ();
-- rather than Par.Child
--
-- In this case the instantiation is within Par, which is
-- an instance, but Gpar does not denote Par because we are
-- not IN the instance of Gpar, so this is illegal. The test
-- below recognizes this particular case.
if Is_Child_Unit (E)
and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
and then (not In_Instance
or else Nkind (Parent (Parent (Gen_Id))) =
N_Compilation_Unit)
then
Error_Msg_N
("prefix of generic child unit must be instance of parent",
Gen_Id);
end if;
if not In_Open_Scopes (Inst_Par) then
Install_Parent (Inst_Par);
Parent_Installed := True;
end if;
else
-- If the generic parent does not contain an entity that
-- corresponds to the selector, the instance doesn't either.
-- Analyzing the node will yield the appropriate error message.
-- If the entity is not a child unit, then it is an inner
-- generic in the parent.
Analyze (Gen_Id);
end if;
else
Analyze (Gen_Id);
if Is_Child_Unit (Entity (Gen_Id))
and then not In_Open_Scopes (Inst_Par)
then
Install_Parent (Inst_Par);
Parent_Installed := True;
end if;
end if;
elsif Ekind (Current_Scope) = E_Package
and then Nkind (Parent (Current_Scope)) = N_Package_Specification
and then Present (Generic_Parent (Parent (Current_Scope)))
then
E := Find_Generic_Child
(Generic_Parent (Parent (Current_Scope)), Gen_Id);
if Present (E) then
Rewrite (Gen_Id,
Make_Expanded_Name (Loc,
Chars => Chars (E),
Prefix => New_Occurrence_Of (Current_Scope, Loc),
Selector_Name => New_Occurrence_Of (E, Loc)));
Set_Entity (Gen_Id, E);
Set_Etype (Gen_Id, Etype (E));
Parent_Installed := False; -- Already in scope.
else
Analyze (Gen_Id);
end if;
else
Analyze (Gen_Id);
end if;
end Check_Generic_Child_Unit;
--------------------------
-- Contains_Instance_Of --
--------------------------
function Contains_Instance_Of
(Inner : Entity_Id;
Outer : Entity_Id;
N : Node_Id)
return Boolean
is
Elmt : Elmt_Id;
Scop : Entity_Id;
begin
Scop := Outer;
-- Verify that there are no circular instantiations. We check whether
-- the unit contains an instance of the current scope or some enclosing
-- scope (in case one of the instances appears in a subunit). Longer
-- circularities involving subunits might seem too pathological to
-- consider, but they were not too pathological for the authors of
-- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
-- enclosing generic scopes as containing an instance.
loop
-- Within a generic subprogram body, the scope is not generic, to
-- allow for recursive subprograms. Use the declaration to determine
-- whether this is a generic unit.
if Ekind (Scop) = E_Generic_Package
or else (Is_Subprogram (Scop)
and then Nkind (Get_Declaration_Node (Scop)) =
N_Generic_Subprogram_Declaration)
then
Elmt := First_Elmt (Inner_Instances (Inner));
while Present (Elmt) loop
if Node (Elmt) = Scop then
Error_Msg_Node_2 := Inner;
Error_Msg_NE
("circular Instantiation: & instantiated within &!",
N, Scop);
return True;
elsif Node (Elmt) = Inner then
return True;
elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
Error_Msg_Node_2 := Inner;
Error_Msg_NE
("circular Instantiation: & instantiated within &!",
N, Node (Elmt));
return True;
end if;
Elmt := Next_Elmt (Elmt);
end loop;
-- Indicate that Inner is being instantiated within Scop.
Append_Elmt (Inner, Inner_Instances (Scop));
end if;
if Scop = Standard_Standard then
exit;
else
Scop := Scope (Scop);
end if;
end loop;
return False;
end Contains_Instance_Of;
---------------------
-- Get_Instance_Of --
---------------------
function Get_Instance_Of (A : Entity_Id) return Entity_Id is
Res : Assoc_Ptr := Generic_Renamings_HTable.Get (A);
begin
if Res /= Assoc_Null then
return Generic_Renamings.Table (Res).Act_Id;
else
-- On exit, entity is not instantiated: not a generic parameter,
-- or else parameter of an inner generic unit.
return A;
end if;
end Get_Instance_Of;
----------------------------
-- Denotes_Formal_Package --
----------------------------
function Denotes_Formal_Package (Pack : Entity_Id) return Boolean is
Par : constant Entity_Id := Current_Instantiated_Parent.Act_Id;
Scop : Entity_Id := Scope (Pack);
E : Entity_Id;
begin
if not In_Open_Scopes (Scop) then
return False;
elsif Ekind (Scop) = E_Generic_Package
or else Nkind (Get_Declaration_Node (Scop))
= N_Generic_Subprogram_Declaration
then
return True;
elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
return True;
elsif No (Par) then
return False;
else
-- Check whether this package is associated with a formal
-- package of the enclosing instantiation. Iterate over the
-- list of renamings.
E := First_Entity (Par);
while Present (E) loop
if Ekind (E) /= E_Package
or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
then
null;
elsif Renamed_Object (E) = Par then
return False;
elsif Renamed_Object (E) = Pack then
return True;
end if;
E := Next_Entity (E);
end loop;
return False;
end if;
end Denotes_Formal_Package;
------------------------------------
-- Get_Package_Instantiation_Node --
------------------------------------
function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
Decl : Node_Id := Get_Declaration_Node (A);
Inst : Node_Id;
begin
-- If the instantiation is a compilation unit, the instantiation node
-- has been replaced with the package declaration for the instance.
-- Otherwise the instantiation node appears after the declaration.
-- If the entity is a formal package, the declaration may have been
-- rewritten as a generic declaration (in the case of a formal with a
-- box) or left as a formal package declaration if it has actuals, and
-- is found with a forward search.
if Nkind (Parent (Decl)) = N_Compilation_Unit then
return Original_Node (Decl);
elsif Nkind (Decl) = N_Generic_Package_Declaration
and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
then
return Original_Node (Decl);
else
Inst := Next (Decl);
while Nkind (Inst) /= N_Package_Instantiation
and then Nkind (Inst) /= N_Formal_Package_Declaration
loop
Inst := Next (Inst);
end loop;
return Inst;
end if;
end Get_Package_Instantiation_Node;
------------------------
-- Instantiate_Object --
------------------------
function Instantiate_Object
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return List_Id
is
Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
Type_Id : constant Node_Id := Subtype_Mark (Formal);
Loc : constant Source_Ptr := Sloc (Actual);
Ftyp : Entity_Id;
Decl_Node : Node_Id;
Subt_Decl : Node_Id := Empty;
List : List_Id := New_List;
begin
if Get_Instance_Of (Formal_Id) /= Formal_Id then
Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
end if;
if Out_Present (Formal) then
-- An IN OUT generic actual must be a name. The instantiation is
-- a renaming declaration. The actual is the name being renamed.
-- We use the actual directly, rather than a copy, because it is not
-- used further in the list of actuals, and because a copy or a use
-- of relocate_node is incorrect if the instance is nested within
-- a generic. This may lead to problems with ASIS.
-- To be revisited ???
if No (Actual) then
Error_Msg_NE
("missing actual for instantiation of &",
Instantiation_Node, Formal_Id);
Abandon_Instantiation (Instantiation_Node);
end if;
Decl_Node :=
Make_Object_Renaming_Declaration (Loc,
Defining_Identifier => New_Copy (Formal_Id),
Subtype_Mark => New_Copy (Type_Id),
Name => Actual);
-- The analysis of the actual may produce insert_action nodes, so
-- the declaration must have a context in which to attach them.
Append (Decl_Node, List);
Analyze (Actual);
-- This check is performed here because Analyze_Object_Renaming
-- will not check it when Comes_From_Source is False. Note
-- though that the check for the actual being the name of an
-- object will be performed in Analyze_Object_Renaming.
if Is_Object_Reference (Actual)
and then Is_Dependent_Component_Of_Mutable_Object (Actual)
then
Error_Msg_N
("illegal discriminant-dependent component for in out parameter",
Actual);
end if;
-- The actual has to be resolved in order to check that it is
-- a variable (due to cases such as F(1), where F returns
-- access to an array, and for overloaded prefixes).
Ftyp :=
Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
if Is_Private_Type (Ftyp)
and then not Is_Private_Type (Etype (Actual))
and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
or else Base_Type (Etype (Actual)) = Ftyp)
then
-- If the actual has the type of the full view of the formal,
-- or else a non-private subtype of the formal, then
-- the visibility of the formal type has changed. Add to the
-- actuals a subtype declaration that will force the exchange
-- of views in the body of the instance as well.
Subt_Decl :=
Make_Subtype_Declaration (Loc,
Defining_Identifier =>
Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
Prepend (Subt_Decl, List);
Append_Elmt (Full_View (Ftyp), Exchanged_Views);
Exchange_Declarations (Ftyp);
end if;
Resolve (Actual, Ftyp);
if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
Error_Msg_NE
("actual for& must be a variable", Actual, Formal_Id);
end if;
Note_Possible_Modification (Actual);
else
-- The instantiation of a generic formal in-parameter
-- is a constant declaration. The actual is the expression for
-- that declaration.
if Present (Actual) then
Decl_Node := Make_Object_Declaration (Loc,
Defining_Identifier => New_Copy (Formal_Id),
Constant_Present => True,
Object_Definition => New_Copy (Type_Id),
Expression => Actual);
Append (Decl_Node, List);
Analyze (Actual);
Freeze_Before (Instantiation_Node, Etype (Expression (Decl_Node)));
elsif Present (Expression (Formal)) then
-- Use default to construct declaration.
Decl_Node :=
Make_Object_Declaration (Loc,
Defining_Identifier => New_Copy (Formal_Id),
Constant_Present => True,
Object_Definition => New_Copy (Type_Id),
Expression => New_Copy_Tree (Expression (Formal)));
Append (Decl_Node, List);
Set_Analyzed (Expression (Decl_Node), False);
else
Error_Msg_NE
("missing actual for instantiation of &",
Instantiation_Node, Formal_Id);
Abandon_Instantiation (Instantiation_Node);
end if;
end if;
return List;
end Instantiate_Object;
--------------------------------
-- Instantiate_Formal_Package --
--------------------------------
function Instantiate_Formal_Package
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return List_Id
is
Actual_Pack : Entity_Id;
Formal_Pack : Entity_Id;
Gen_Parent : Entity_Id;
Decls : List_Id;
Loc : constant Source_Ptr := Sloc (Actual);
Nod : Node_Id;
Parent_Spec : Node_Id;
function Formal_Entity
(F : Node_Id;
Act_Ent : Entity_Id)
return Entity_Id;
-- Returns the entity associated with the given formal F. In the
-- case where F is a formal package, this function will iterate
-- through all of F's formals and enter map associations from the
-- actuals occurring in the formal package's corresponding actual
-- package (obtained via Act_Ent) to the formal package's formal
-- parameters. This function is called recursively for arbitrary
-- levels of formal packages.
-------------------
-- Formal_Entity --
-------------------
function Formal_Entity
(F : Node_Id;
Act_Ent : Entity_Id)
return Entity_Id
is
Orig_Node : Node_Id := F;
begin
case Nkind (F) is
when N_Formal_Object_Declaration =>
return Defining_Identifier (F);
when N_Formal_Type_Declaration =>
return Defining_Identifier (F);
when N_Formal_Subprogram_Declaration =>
return Defining_Unit_Name (Specification (F));
when N_Formal_Package_Declaration |
N_Generic_Package_Declaration =>
if Nkind (F) = N_Generic_Package_Declaration then
Orig_Node := Original_Node (F);
end if;
declare
Actual_Ent : Entity_Id := First_Entity (Act_Ent);
Formal_Node : Node_Id;
Formal_Ent : Entity_Id;
Gen_Decl : Node_Id :=
Get_Declaration_Node
(Entity (Name (Orig_Node)));
Formals : List_Id :=
Generic_Formal_Declarations (Gen_Decl);
begin
if Present (Formals) then
Formal_Node := First_Non_Pragma (Formals);
end if;
-- As for the loop further below, this loop is making
-- a probably invalid assumption about the correspondence
-- between formals and actuals and eventually needs to
-- corrected to account for cases where the formals are
-- not synchronized and in one-to-one correspondence
-- with actuals. ???
while Present (Actual_Ent)
and then Present (Formal_Node)
and then Actual_Ent /= First_Private_Entity (Act_Ent)
loop
-- ??? Are the following calls also needed here:
--
-- Set_Is_Private (Actual_Ent, False);
-- Set_Is_Potentially_Use_Visible
-- (Actual_Ent, In_Use (Act_Ent));
Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
if Present (Formal_Ent) then
Set_Instance_Of (Formal_Ent, Actual_Ent);
end if;
Formal_Node := Next_Non_Pragma (Formal_Node);
Actual_Ent := Next_Entity (Actual_Ent);
end loop;
end;
return Defining_Identifier (Orig_Node);
when N_Use_Package_Clause =>
return Empty;
when N_Use_Type_Clause =>
return Empty;
-- We return Empty for all other encountered forms of
-- declarations because there are some cases of nonformal
-- sorts of declaration that can show up (e.g., when array
-- formals are present). Since it's not clear what kinds
-- can appear among the formals, we won't raise failure here.
when others =>
return Empty;
end case;
end Formal_Entity;
procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
-- Within the generic part, entities in the formal package are
-- visible. To validate subsequent type declarations, indicate
-- the correspondence betwen the entities in the analyzed formal,
-- and the entities in the actual package. There are three packages
-- involved in the instantiation of a formal package: the parent
-- generic P1 which appears in the generic declaration, the fake
-- instantiation P2 which appears in the analyzed generic, and whose
-- visible entities may be used in subsequent formals, and the actual
-- P3 in the instance. To validate subsequent formals, me indicate
-- that the entities in P2 are mapped into those of P3. The mapping of
-- entities has to be done recursively for nested packages.
procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
E1 : Entity_Id;
E2 : Entity_Id;
begin
Set_Instance_Of (Form, Act);
E1 := First_Entity (Form);
E2 := First_Entity (Act);
while Present (E1)
and then E1 /= First_Private_Entity (Form)
loop
if not Is_Internal (E1)
and then not Is_Class_Wide_Type (E1)
then
while Present (E2)
and then Chars (E2) /= Chars (E1)
loop
E2 := Next_Entity (E2);
end loop;
if No (E2) then
exit;
else
Set_Instance_Of (E1, E2);
if Ekind (E1) = E_Package
and then No (Renamed_Object (E1))
then
Map_Entities (E1, E2);
end if;
end if;
end if;
E1 := Next_Entity (E1);
end loop;
end Map_Entities;
-- Start of processing for Instantiate_Formal_Package
begin
Analyze (Actual);
if not Is_Entity_Name (Actual)
or else Ekind (Entity (Actual)) /= E_Package
then
Error_Msg_N
("expect package instance to instantiate formal", Actual);
Abandon_Instantiation (Actual);
raise Program_Error;
else
Actual_Pack := Entity (Actual);
-- The actual may be a renamed package, or an outer generic
-- formal package whose instantiation is converted into a renaming.
if Present (Renamed_Object (Actual_Pack)) then
Actual_Pack := Renamed_Object (Actual_Pack);
end if;
if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
Formal_Pack := Defining_Identifier (Analyzed_Formal);
else
Gen_Parent :=
Generic_Parent (Specification (Analyzed_Formal));
Formal_Pack :=
Defining_Unit_Name (Specification (Analyzed_Formal));
end if;
if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
Parent_Spec := Specification (Get_Declaration_Node (Actual_Pack));
else
Parent_Spec := Parent (Actual_Pack);
end if;
if Gen_Parent = Any_Id then
Error_Msg_N
("previous error in declaration of formal package", Actual);
Abandon_Instantiation (Actual);
elsif
Generic_Parent (Parent_Spec) /= Get_Instance_Of (Gen_Parent)
then
Error_Msg_NE
("actual parameter must be instance of&", Actual, Gen_Parent);
Abandon_Instantiation (Actual);
end if;
Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
Map_Entities (Formal_Pack, Actual_Pack);
Nod :=
Make_Package_Renaming_Declaration (Loc,
Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
Name => New_Reference_To (Actual_Pack, Loc));
Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
Defining_Identifier (Formal));
Decls := New_List (Nod);
-- If the formal F has a box, then the generic declarations are
-- visible in the generic G. In an instance of G, the corresponding
-- entities in the actual for F (which are the actuals for the
-- instantiation of the generic that F denotes) must also be made
-- visible for analysis of the current instance. On exit from the
-- current instance, those entities are made private again. If the
-- actual is currently in use, these entities are also use-visible.
-- The loop through the actual entities also steps through the
-- formal entities and enters associations from formals to
-- actuals into the renaming map. This is necessary to properly
-- handle checking of actual parameter associations for later
-- formals that depend on actuals declared in the formal package.
--
-- This processing needs to be reviewed at some point because
-- it is probably not entirely correct as written. For example
-- there may not be a strict one-to-one correspondence between
-- actuals and formals and this loop is currently assuming that
-- there is. ???
if Box_Present (Formal) then
declare
Actual_Ent : Entity_Id := First_Entity (Actual_Pack);
Formal_Node : Node_Id;
Formal_Ent : Entity_Id;
Gen_Decl : Node_Id := Get_Declaration_Node (Gen_Parent);
Formals : List_Id := Generic_Formal_Declarations (Gen_Decl);
begin
if Present (Formals) then
Formal_Node := First_Non_Pragma (Formals);
end if;
while Present (Actual_Ent)
and then Actual_Ent /= First_Private_Entity (Actual_Pack)
loop
Set_Is_Private (Actual_Ent, False);
Set_Is_Potentially_Use_Visible
(Actual_Ent, In_Use (Actual_Pack));
if Present (Formal_Node) then
Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
if Present (Formal_Ent) then
Set_Instance_Of (Formal_Ent, Actual_Ent);
end if;
Formal_Node := Next_Non_Pragma (Formal_Node);
end if;
Actual_Ent := Next_Entity (Actual_Ent);
end loop;
end;
else
-- If the formal is not declared with a box, reanalyze it as
-- an instantiation, to verify the matching rules of 12.7. The
-- actual checks are performed after the generic associations
-- been analyzed.
declare
I_Pack : constant Entity_Id :=
Make_Defining_Identifier (Sloc (Actual),
Chars => New_Internal_Name ('P'));
Decl : Node_Id;
begin
Set_Is_Internal (I_Pack);
Append_To (Decls,
Make_Package_Instantiation (Sloc (Actual),
Defining_Unit_Name => I_Pack,
Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
Generic_Associations =>
Generic_Associations (Formal)));
end;
end if;
return Decls;
end if;
end Instantiate_Formal_Package;
---------------------------
-- Check_Formal_Packages --
---------------------------
procedure Check_Formal_Packages (P_Id : Entity_Id) is
E : Entity_Id;
Formal_P : Entity_Id;
begin
E := First_Entity (P_Id);
-- Iterate through the declarations in the instance, looking for
-- package renaming declarations that denote instances of formal
-- packages. Stop when we find the renaming of the current package
-- itself. The declaration for a formal package without a box is
-- followed by an internal entity that repeats the instantiation.
while Present (E) loop
if Ekind (E) = E_Package then
if Renamed_Object (E) = P_Id then
exit;
elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
null;
elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
Formal_P := Next_Entity (E);
Check_Formal_Package_Instance (Formal_P, E);
end if;
end if;
E := Next_Entity (E);
end loop;
end Check_Formal_Packages;
-----------------------------------
-- Check_Formal_Package_Instance --
-----------------------------------
-- If the formal has specific parameters, they must match those of the
-- actual. Both of them are instances, and the renaming declarations
-- for their formal parameters appear in the same order in both. The
-- analyzed formal has been analyzed in the context of the current
-- instance.
procedure Check_Formal_Package_Instance
(Formal_Pack : Entity_Id;
Actual_Pack : Entity_Id)
is
E1 : Entity_Id := First_Entity (Actual_Pack);
E2 : Entity_Id := First_Entity (Formal_Pack);
Expr1 : Node_Id;
Expr2 : Node_Id;
procedure Check_Mismatch (B : Boolean);
-- Common error routine for mismatch between the parameters of
-- the actual instance and those of the formal package.
procedure Check_Mismatch (B : Boolean) is
begin
if B then
Error_Msg_NE
("actual for & in actual instance does not match formal",
Parent (Actual_Pack), E1);
end if;
end Check_Mismatch;
-- Start of processing for Check_Formal_Package_Instance
begin
while Present (E1)
and then Present (E2)
loop
exit when Ekind (E1) = E_Package
and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
if Is_Type (E1) then
-- Subtypes must statically match. E1 and E2 are the
-- local entities that are subtypes of the actuals.
Check_Mismatch
(not Is_Type (E2)
or else Etype (E1) /= Etype (E2)
or else not Subtypes_Statically_Match (E1, E2));
elsif Ekind (E1) = E_Constant then
-- IN parameters must denote the same static value, or
-- the same constant, or the literal null.
Expr1 := Expression (Parent (E1));
if Ekind (E2) /= E_Constant then
Check_Mismatch (True);
else
Expr2 := Expression (Parent (E2));
end if;
if Is_Static_Expression (Expr1) then
if not Is_Static_Expression (Expr2) then
Check_Mismatch (True);
elsif Is_Integer_Type (Etype (E1)) then
declare
V1 : Uint := Expr_Value (Expr1);
V2 : Uint := Expr_Value (Expr2);
begin
Check_Mismatch (V1 /= V2);
end;
elsif Is_Real_Type (Etype (E1)) then
declare
V1 : Ureal := Expr_Value_R (Expr1);
V2 : Ureal := Expr_Value_R (Expr2);
begin
Check_Mismatch (V1 /= V2);
end;
elsif Is_String_Type (Etype (E1))
and then Nkind (Expr1) = N_String_Literal
then
if Nkind (Expr2) /= N_String_Literal then
Check_Mismatch (True);
else
Check_Mismatch
(String_Equal (Strval (Expr1), Strval (Expr2)));
end if;
end if;
elsif Is_Entity_Name (Expr1) then
if Is_Entity_Name (Expr2) then
if Entity (Expr1) = Entity (Expr2) then
null;
elsif Ekind (Entity (Expr2)) = E_Constant
and then Is_Entity_Name (Constant_Value (Entity (Expr2)))
and then
Entity (Constant_Value (Entity (Expr2))) = Entity (Expr1)
then
null;
else
Check_Mismatch (True);
end if;
else
Check_Mismatch (True);
end if;
elsif Nkind (Expr1) = N_Null then
Check_Mismatch (Nkind (Expr1) /= N_Null);
else
Check_Mismatch (True);
end if;
elsif Ekind (E1) = E_Variable
or else Ekind (E1) = E_Package
then
Check_Mismatch
(Ekind (E1) /= Ekind (E2)
or else Renamed_Object (E1) /= Renamed_Object (E2));
elsif Is_Overloadable (E1) then
-- Verify that the names of the entities match.
-- What if actual is an attribute ???
Check_Mismatch
(Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
else
pragma Assert (False);
raise Program_Error;
end if;
E1 := Next_Entity (E1);
E2 := Next_Entity (E2);
end loop;
end Check_Formal_Package_Instance;
-----------------------------------
-- Instantiate_Formal_Subprogram --
-----------------------------------
function Instantiate_Formal_Subprogram
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return Node_Id
is
Loc : Source_Ptr := Sloc (Instantiation_Node);
Formal_Sub : constant Entity_Id :=
Defining_Unit_Name (Specification (Formal));
Analyzed_S : constant Entity_Id :=
Defining_Unit_Name (Specification (Analyzed_Formal));
Decl_Node : Node_Id;
Nam : Node_Id;
New_Spec : Node_Id;
procedure Valid_Actual_Subprogram (Act : Node_Id);
-- Perform legality check and raise exception on failure.
procedure Valid_Actual_Subprogram (Act : Node_Id) is
begin
if not Is_Entity_Name (Act)
and then Nkind (Act) /= N_Operator_Symbol
and then Nkind (Act) /= N_Attribute_Reference
and then Nkind (Act) /= N_Selected_Component
and then Nkind (Act) /= N_Indexed_Component
and then Nkind (Act) /= N_Character_Literal
then
if Etype (Act) /= Any_Type then
Error_Msg_NE
("Expect subprogram name to instantiate &",
Instantiation_Node, Formal_Sub);
end if;
-- In any case, instantiation cannot continue.
Abandon_Instantiation (Instantiation_Node);
end if;
end Valid_Actual_Subprogram;
-- Start of processing for Instantiate_Formal_Subprogram
begin
New_Spec := New_Copy_Tree (Specification (Formal));
-- Create new entity for the actual (New_Copy_Tree does not).
Set_Defining_Unit_Name
(New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
-- Find entity of actual. If the actual is an attribute reference, it
-- cannot be resolved here (its formal is missing) but is handled
-- instead in Attribute_Renaming. If the actual is overloaded, it is
-- fully resolved subsequently, when the renaming declaration for the
-- formal is analyzed.
if Present (Actual) then
Loc := Sloc (Actual);
Set_Sloc (New_Spec, Loc);
if Nkind (Actual) = N_Operator_Symbol then
Find_Direct_Name (Actual);
elsif Nkind (Actual) /= N_Attribute_Reference then
Analyze (Actual);
end if;
Valid_Actual_Subprogram (Actual);
Nam := Actual;
elsif Present (Default_Name (Formal)) then
if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
and then Nkind (Default_Name (Formal)) /= N_Selected_Component
and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
and then Nkind (Default_Name (Formal)) /= N_Character_Literal
and then Present (Entity (Default_Name (Formal)))
then
Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
else
Nam := New_Copy (Default_Name (Formal));
Set_Sloc (Nam, Loc);
end if;
elsif Box_Present (Formal) then
-- Actual is resolved at the point of instantiation. Create
-- an identifier or operator with the same name as the formal.
if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
Nam := Make_Operator_Symbol (Loc,
Chars => Chars (Formal_Sub),
Strval => No_String);
else
Nam := Make_Identifier (Loc, Chars (Formal_Sub));
end if;
else
Error_Msg_NE
("missing actual for instantiation of &",
Instantiation_Node, Formal_Sub);
Abandon_Instantiation (Instantiation_Node);
end if;
Decl_Node :=
Make_Subprogram_Renaming_Declaration (Loc,
Specification => New_Spec,
Name => Nam);
-- The generic instantiation freezes the actual. This can only be
-- done once the actual is resolved, in the analysis of the renaming
-- declaration. To indicate that must be done, we set the corresponding
-- spec of the node to point to the formal subprogram declaration.
Set_Corresponding_Spec (Decl_Node, Analyzed_Formal);
-- We cannot analyze the renaming declaration, and thus find the
-- actual, until the all the actuals are assembled in the instance.
-- For subsequent checks of other actuals, indicate the node that
-- will hold the instance of this formal.
Set_Instance_Of (Analyzed_S, Nam);
return Decl_Node;
end Instantiate_Formal_Subprogram;
----------------------
-- Find_Actual_Type --
----------------------
function Find_Actual_Type
(Typ : Entity_Id;
Gen_Scope : Entity_Id)
return Entity_Id
is
T : Entity_Id;
begin
if not Is_Child_Unit (Gen_Scope) then
return Get_Instance_Of (Typ);
elsif not Is_Generic_Type (Typ)
or else Scope (Typ) = Gen_Scope
then
return Get_Instance_Of (Typ);
else
T := Current_Entity (Typ);
while Present (T) loop
if In_Open_Scopes (Scope (T)) then
return T;
end if;
T := Homonym (T);
end loop;
return Typ;
end if;
end Find_Actual_Type;
----------------------
-- Instantiate_Type --
----------------------
function Instantiate_Type
(Formal : Node_Id;
Actual : Node_Id;
Analyzed_Formal : Node_Id)
return Node_Id
is
Loc : constant Source_Ptr := Sloc (Actual);
Gen_T : constant Entity_Id := Defining_Identifier (Formal);
A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
Ancestor : Entity_Id;
Def : constant Node_Id := Formal_Type_Definition (Formal);
Act_T : Entity_Id;
Decl_Node : Node_Id;
procedure Validate_Array_Type_Instance;
procedure Validate_Access_Subprogram_Instance;
procedure Validate_Access_Type_Instance;
procedure Validate_Derived_Type_Instance;
procedure Validate_Private_Type_Instance;
-- These procedures perform validation tests for the named case
function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
-- Check that base types are the same and that the subtypes match
-- statically. Used in several of the above.
--------------------
-- Subtypes_Match --
--------------------
function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
T : constant Entity_Id := Get_Instance_Of (Gen_T);
begin
return (Base_Type (T) = Base_Type (Act_T)
-- and then Is_Constrained (T) = Is_Constrained (Act_T)
-- commented out by RBKD ???
and then Subtypes_Statically_Match (T, Act_T))
or else (Is_Class_Wide_Type (Gen_T)
and then Is_Class_Wide_Type (Act_T)
and then
Subtypes_Match (
Get_Instance_Of (Root_Type (Gen_T)),
Root_Type (Act_T)));
end Subtypes_Match;
----------------------------------
-- Validate_Array_Type_Instance --
----------------------------------
procedure Validate_Array_Type_Instance is
I1 : Node_Id;
I2 : Node_Id;
T2 : Entity_Id;
function Formal_Dimensions return Int;
-- Count number of dimensions in array type formal
function Formal_Dimensions return Int is
Num : Int := 0;
Index : Node_Id;
begin
if Nkind (Def) = N_Constrained_Array_Definition then
Index := First (Discrete_Subtype_Definitions (Def));
else
Index := First (Subtype_Marks (Def));
end if;
while Present (Index) loop
Num := Num + 1;
Index := Next_Index (Index);
end loop;
return Num;
end Formal_Dimensions;
-- Start of processing for Validate_Array_Type_Instance
begin
if not Is_Array_Type (Act_T) then
Error_Msg_NE
("expect array type in instantiation of &", Actual, Gen_T);
Abandon_Instantiation (Actual);
elsif Nkind (Def) = N_Constrained_Array_Definition then
if not (Is_Constrained (Act_T)) then
Error_Msg_NE
("expect constrained array in instantiation of &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
else
if Is_Constrained (Act_T) then
Error_Msg_NE
("expect unconstrained array in instantiation of &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
end if;
if Formal_Dimensions /= Number_Dimensions (Act_T) then
Error_Msg_NE
("dimensions of actual do not match formal &", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
I1 := First_Index (A_Gen_T);
I2 := First_Index (Act_T);
for I in 1 .. Formal_Dimensions loop
-- If the indices of the actual were given by a subtype_mark,
-- the index was transformed into a range attribute. Retrieve
-- the original type mark for checking.
if Is_Entity_Name (Original_Node (I2)) then
T2 := Entity (Original_Node (I2));
else
T2 := Etype (I2);
end if;
if not Subtypes_Match
(Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
then
Error_Msg_NE
("index types of actual do not match those of formal &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
I1 := Next_Index (I1);
I2 := Next_Index (I2);
end loop;
if not Subtypes_Match (
Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
Component_Type (Act_T))
then
Error_Msg_NE
("component subtype of actual does not match that of formal &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
if Has_Aliased_Components (A_Gen_T)
and then not Has_Aliased_Components (Act_T)
then
Error_Msg_NE
("actual must have aliased components to match formal type &",
Actual, Gen_T);
end if;
end Validate_Array_Type_Instance;
-----------------------------------
-- Validate_Access_Type_Instance --
-----------------------------------
procedure Validate_Access_Type_Instance is
Desig_Type : Entity_Id := Get_Instance_Of (Designated_Type (A_Gen_T));
begin
if not Is_Access_Type (Act_T) then
Error_Msg_NE
("expect access type in instantiation of &", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
if Is_Access_Constant (A_Gen_T) then
if not Is_Access_Constant (Act_T) then
Error_Msg_N
("actual type must be access-to-constant type", Actual);
Abandon_Instantiation (Actual);
end if;
else
if Is_Access_Constant (Act_T) then
Error_Msg_N
("actual type must be access-to-variable type", Actual);
Abandon_Instantiation (Actual);
elsif Ekind (A_Gen_T) = E_General_Access_Type
and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
then
Error_Msg_N ("actual must be general access type!", Actual);
Error_Msg_NE ("add ALL to }!", Actual, Act_T);
Abandon_Instantiation (Actual);
end if;
end if;
if not Subtypes_Match
(Desig_Type, Designated_Type (Act_T))
then
Error_Msg_NE
("designated type of actual does not match that of formal &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
elsif Is_Access_Type (Designated_Type (Act_T))
and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
/=
Is_Constrained (Designated_Type (Desig_Type))
then
Error_Msg_NE
("designated type of actual does not match that of formal &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
end Validate_Access_Type_Instance;
----------------------------------
-- Validate_Subprogram_Instance --
----------------------------------
procedure Validate_Access_Subprogram_Instance is
begin
if not Is_Access_Type (Act_T)
or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
then
Error_Msg_NE
("expect access type in instantiation of &", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
Check_Mode_Conformant
(Designated_Type (Act_T),
Designated_Type (A_Gen_T),
Actual,
Get_Inst => True);
if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
Error_Msg_NE
("protected access type not allowed for formal &",
Actual, Gen_T);
end if;
elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
Error_Msg_NE
("expect protected access type for formal &",
Actual, Gen_T);
end if;
end Validate_Access_Subprogram_Instance;
------------------------------------
-- Validate_Private_Type_Instance --
------------------------------------
procedure Validate_Private_Type_Instance is
Formal_Discr : Entity_Id;
Actual_Discr : Entity_Id;
Formal_Subt : Entity_Id;
begin
if (Is_Limited_Type (Act_T)
or else Is_Limited_Composite (Act_T))
and then not Is_Limited_Type (A_Gen_T)
then
Error_Msg_NE
("actual for non-limited & cannot be a limited type", Actual,
Gen_T);
Abandon_Instantiation (Actual);
elsif Is_Indefinite_Subtype (Act_T)
and then not Is_Indefinite_Subtype (A_Gen_T)
and then Ada_95
then
Error_Msg_NE
("actual for & must be a definite subtype", Actual, Gen_T);
elsif not Is_Tagged_Type (Act_T)
and then Is_Tagged_Type (A_Gen_T)
then
Error_Msg_NE
("actual for & must be a tagged type", Actual, Gen_T);
elsif Has_Discriminants (A_Gen_T) then
if not Has_Discriminants (Act_T) then
Error_Msg_NE
("actual for & must have discriminants", Actual, Gen_T);
Abandon_Instantiation (Actual);
elsif Is_Constrained (Act_T) then
Error_Msg_NE
("actual for & must be unconstrained", Actual, Gen_T);
Abandon_Instantiation (Actual);
else
Formal_Discr := First_Discriminant (A_Gen_T);
Actual_Discr := First_Discriminant (Act_T);
while Formal_Discr /= Empty loop
if Actual_Discr = Empty then
Error_Msg_NE
("discriminants on actual do not match formal",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
if Base_Type (Formal_Subt) /=
Base_Type (Etype (Actual_Discr))
then
Error_Msg_NE
("types of actual discriminants must match formal",
Actual, Gen_T);
Abandon_Instantiation (Actual);
elsif not Subtypes_Statically_Match
(Formal_Subt, Etype (Actual_Discr))
and then Ada_95
then
Error_Msg_NE
("subtypes of actual discriminants must match formal",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
Formal_Discr := Next_Discriminant (Formal_Discr);
Actual_Discr := Next_Discriminant (Actual_Discr);
end loop;
if Actual_Discr /= Empty then
Error_Msg_NE
("discriminants on actual do not match formal",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
end if;
end if;
Ancestor := Gen_T;
end Validate_Private_Type_Instance;
------------------------------------
-- Validate_Derived_Type_Instance --
------------------------------------
procedure Validate_Derived_Type_Instance is
Actual_Discr : Entity_Id;
Ancestor_Discr : Entity_Id;
begin
-- If the parent type in the generic declaration is itself
-- a previous formal type, then it is local to the generic
-- and absent from the analyzed generic definition. In that
-- case the ancestor is the instance of the formal (which must
-- have been instantiatied previously). Otherwise, the analyzed
-- generic carries the parent type.
if Is_Entity_Name (Subtype_Mark (Def))
and then Present (Entity (Subtype_Mark (Def)))
then
Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
else
Ancestor :=
Get_Instance_Of (Root_Type (Get_Instance_Of (A_Gen_T)));
end if;
if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
Error_Msg_NE
("expect type derived from & in instantiation",
Actual, Ancestor);
Abandon_Instantiation (Actual);
end if;
-- It should not be necessary to check for unknown discriminants
-- on Formal, but for some reason Has_Unknown_Discriminants is
-- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
-- returns False. This needs fixing. ???
if not Is_Indefinite_Subtype (A_Gen_T)
and then not Unknown_Discriminants_Present (Formal)
and then Is_Indefinite_Subtype (Act_T)
then
Error_Msg_N
("actual subtype must be constrained", Actual);
Abandon_Instantiation (Actual);
end if;
if not Unknown_Discriminants_Present (Formal) then
if Is_Constrained (Ancestor) then
if not Is_Constrained (Act_T) then
Error_Msg_N
("actual subtype must be constrained", Actual);
Abandon_Instantiation (Actual);
end if;
-- Ancestor is unconstrained
elsif Is_Constrained (Act_T) then
if Ekind (Ancestor) = E_Access_Type
or else Is_Composite_Type (Ancestor)
then
Error_Msg_N
("actual subtype must be unconstrained", Actual);
Abandon_Instantiation (Actual);
end if;
-- A class-wide type is only allowed if the formal has
-- unknown discriminants.
elsif Is_Class_Wide_Type (Act_T)
and then not Has_Unknown_Discriminants (Ancestor)
then
Error_Msg_NE
("actual for & cannot be a class-wide type", Actual, Gen_T);
Abandon_Instantiation (Actual);
-- Otherwise, the formal and actual shall have the same
-- number of discriminants and each discriminant of the
-- actual must correspond to a discriminant of the formal.
elsif Has_Discriminants (Act_T)
and then Has_Discriminants (Ancestor)
then
Actual_Discr := First_Discriminant (Act_T);
Ancestor_Discr := First_Discriminant (Ancestor);
while Present (Actual_Discr)
and then Present (Ancestor_Discr)
loop
if Base_Type (Act_T) /= Base_Type (Ancestor) and then
not Present (Corresponding_Discriminant (Actual_Discr))
then
Error_Msg_NE
("discriminant & does not correspond " &
"to ancestor discriminant", Actual, Actual_Discr);
Abandon_Instantiation (Actual);
end if;
Actual_Discr := Next_Discriminant (Actual_Discr);
Ancestor_Discr := Next_Discriminant (Ancestor_Discr);
end loop;
if Present (Actual_Discr) or else Present (Ancestor_Discr) then
Error_Msg_NE
("actual for & must have same number of discriminants",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
-- This case should be caught by the earlier check for
-- for constrainedness, but the check here is added for
-- completeness.
elsif Has_Discriminants (Act_T) then
Error_Msg_NE
("actual for & must not have discriminants", Actual, Gen_T);
Abandon_Instantiation (Actual);
elsif Has_Discriminants (Ancestor) then
Error_Msg_NE
("actual for & must have known discriminants", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
Error_Msg_N
("constraint on actual is incompatible with formal", Actual);
Abandon_Instantiation (Actual);
end if;
end if;
end Validate_Derived_Type_Instance;
-- Start of processing for Instantiate_Type
begin
if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
Error_Msg_N ("duplicate instantiation of generic type", Actual);
return Error;
elsif not Is_Entity_Name (Actual)
or else not Is_Type (Entity (Actual))
then
Error_Msg_NE
("expect valid subtype mark to instantiate &", Actual, Gen_T);
Abandon_Instantiation (Actual);
else
Act_T := Entity (Actual);
if Ekind (Act_T) = E_Incomplete_Type then
if No (Underlying_Type (Act_T)) then
Error_Msg_N ("premature use of incomplete type", Actual);
Abandon_Instantiation (Actual);
else
Act_T := Full_View (Act_T);
Set_Entity (Actual, Act_T);
end if;
elsif Is_Private_Type (Act_T)
and then not Is_Generic_Type (Act_T)
and then not Is_Derived_Type (Act_T)
and then No (Full_View (Root_Type (Act_T)))
then
Error_Msg_N ("premature use of private type", Actual);
elsif Has_Private_Component (Act_T) then
Error_Msg_N
("premature use of type with private component", Actual);
end if;
Set_Instance_Of (A_Gen_T, Act_T);
-- If the type is generic, the class-wide type may also be used
if Is_Tagged_Type (A_Gen_T)
and then Is_Tagged_Type (Act_T)
and then not Is_Class_Wide_Type (A_Gen_T)
then
Set_Instance_Of (Class_Wide_Type (A_Gen_T),
Class_Wide_Type (Act_T));
end if;
if not Is_Abstract (A_Gen_T)
and then Is_Abstract (Act_T)
then
Error_Msg_N
("actual of non-abstract formal cannot be abstract", Actual);
end if;
if Is_Scalar_Type (Gen_T) then
Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
end if;
end if;
case Nkind (Def) is
when N_Formal_Private_Type_Definition =>
Validate_Private_Type_Instance;
when N_Formal_Derived_Type_Definition =>
Validate_Derived_Type_Instance;
when N_Formal_Discrete_Type_Definition =>
if not Is_Discrete_Type (Act_T) then
Error_Msg_NE
("expect discrete type in instantiation of&", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Formal_Signed_Integer_Type_Definition =>
if not Is_Signed_Integer_Type (Act_T) then
Error_Msg_NE
("expect signed integer type in instantiation of&",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Formal_Modular_Type_Definition =>
if not Is_Modular_Integer_Type (Act_T) then
Error_Msg_NE
("expect modular type in instantiation of &", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Formal_Floating_Point_Definition =>
if not Is_Floating_Point_Type (Act_T) then
Error_Msg_NE
("expect float type in instantiation of &", Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Formal_Ordinary_Fixed_Point_Definition =>
if not Is_Ordinary_Fixed_Point_Type (Act_T) then
Error_Msg_NE
("expect ordinary fixed point type in instantiation of &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Formal_Decimal_Fixed_Point_Definition =>
if not Is_Decimal_Fixed_Point_Type (Act_T) then
Error_Msg_NE
("expect decimal type in instantiation of &",
Actual, Gen_T);
Abandon_Instantiation (Actual);
end if;
when N_Array_Type_Definition =>
Validate_Array_Type_Instance;
when N_Access_To_Object_Definition =>
Validate_Access_Type_Instance;
when N_Access_Function_Definition |
N_Access_Procedure_Definition =>
Validate_Access_Subprogram_Instance;
when others =>
pragma Assert (False);
raise Program_Error;
end case;
Decl_Node :=
Make_Subtype_Declaration (Loc,
Defining_Identifier => New_Copy (Gen_T),
Subtype_Indication => New_Reference_To (Act_T, Loc));
if Is_Private_Type (Act_T) then
Set_Has_Private_View (Subtype_Indication (Decl_Node));
end if;
-- Flag actual derived types so their elaboration produces the
-- appropriate renamings for the primitive operations of the ancestor.
-- Flag actual for formal private types as well, to determine whether
-- operations in the private part may override inherited operations.
if Nkind (Def) = N_Formal_Derived_Type_Definition
or else Nkind (Def) = N_Formal_Private_Type_Definition
then
Set_Generic_Parent_Type (Decl_Node, Ancestor);
end if;
return Decl_Node;
end Instantiate_Type;
-----------------------
-- Move_Freeze_Nodes --
-----------------------
procedure Move_Freeze_Nodes
(Out_Of : Entity_Id;
After : Node_Id;
L : List_Id)
is
Decl : Node_Id;
Next_Decl : Node_Id;
Next_Node : Node_Id := After;
Spec : Node_Id;
function Is_Outer_Type (T : Entity_Id) return Boolean;
-- Check whether entity is declared in a scope external to that
-- of the generic unit.
function Is_Outer_Type (T : Entity_Id) return Boolean is
Scop : Entity_Id := Scope (T);
begin
if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
return True;
else
while Scop /= Standard_Standard loop
if Scop = Out_Of then
return False;
else
Scop := Scope (Scop);
end if;
end loop;
return True;
end if;
end Is_Outer_Type;
-- Start of processing for Move_Freeze_Nodes
begin
if No (L) then
return;
end if;
-- First remove the freeze nodes that may appear before all other
-- declarations.
Decl := First (L);
while Present (Decl)
and then Nkind (Decl) = N_Freeze_Entity
and then Is_Outer_Type (Entity (Decl))
loop
Decl := Remove_Head (L);
Insert_After (Next_Node, Decl);
Set_Analyzed (Decl, False);
Next_Node := Decl;
Decl := First (L);
end loop;
-- Next scan the list of declarations and remove each freeze node that
-- appears ahead of the current node.
while Present (Decl) loop
while Present (Next (Decl))
and then Nkind (Next (Decl)) = N_Freeze_Entity
and then Is_Outer_Type (Entity (Next (Decl)))
loop
Next_Decl := Remove_Next (Decl);
Insert_After (Next_Node, Next_Decl);
Set_Analyzed (Next_Decl, False);
Next_Node := Next_Decl;
end loop;
-- If the declaration is a nested package or concurrent type, then
-- recurse. Nested generic packages will have been processed from the
-- inside out.
if Nkind (Decl) = N_Package_Specification then
Spec := Decl;
elsif Nkind (Decl) = N_Task_Type_Declaration then
Spec := Task_Definition (Decl);
elsif Nkind (Decl) = N_Protected_Type_Declaration then
Spec := Protected_Definition (Decl);
else
Spec := Empty;
end if;
if Present (Spec) then
Move_Freeze_Nodes (Out_Of, After, Visible_Declarations (Spec));
Move_Freeze_Nodes (Out_Of, After, Private_Declarations (Spec));
end if;
Decl := Next (Decl);
end loop;
end Move_Freeze_Nodes;
---------------------------
-- Restore_Private_Views --
---------------------------
procedure Restore_Private_Views
(Pack_Id : Entity_Id;
Is_Package : Boolean := True)
is
M : Elmt_Id;
E : Entity_Id;
begin
M := First_Elmt (Exchanged_Views);
while Present (M) loop
Exchange_Declarations (Node (M));
M := Next_Elmt (M);
end loop;
-- Make the generic formal parameters private, and make the formal
-- types into subtypes of the actuals again.
E := First_Entity (Pack_Id);
while Present (E) loop
Set_Is_Private (E, True);
if Is_Type (E)
and then Nkind (Parent (E)) = N_Subtype_Declaration
then
Set_Is_Generic_Actual_Type (E, False);
-- An unusual case of aliasing: the actual may also be directly
-- visible in the generic, and be private there, while it is
-- fully visible in the context of the instance. The internal
-- subtype is private in the instance, but has full visibility
-- like its parent in the enclosing scope. This enforces the
-- invariant that the privacy status of all private dependents of
-- a type coincide with that of the parent type. This can only
-- happen when a generic child unit is instantiated within a
-- sibling.
if Is_Private_Type (E)
and then not Is_Private_Type (Etype (E))
then
Exchange_Declarations (E);
end if;
elsif Ekind (E) = E_Package then
-- The end of the renaming list is the renaming of the generic
-- package itself. If the instance is a subprogram, all entities
-- in the corresponding package are renamings. If this entity is
-- a formal package, make its own formals private as well. The
-- actual in this case is itself the renaming of an instantation.
-- If the entity is not a package renaming, it is the entity
-- created to validate formal package actuals: ignore.
-- If the actual is itself a formal package for the enclosing
-- generic, or the actual for such a formal package, it remains
-- visible after the current instance, and therefore nothing
-- needs to be done either.
if Is_Package
and then Renamed_Object (E) = Pack_Id
then
exit;
elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
null;
elsif Denotes_Formal_Package (Renamed_Object (E)) then
null;
else
declare
Act_P : Entity_Id := Renamed_Object (E);
Id : Entity_Id := First_Entity (Act_P);
begin
while Present (Id)
and then Id /= First_Private_Entity (Act_P)
loop
Set_Is_Private (Id, True);
Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
exit when Ekind (Id) = E_Package
and then Renamed_Object (Id) = Act_P;
Id := Next_Entity (Id);
end loop;
end;
null;
end if;
end if;
E := Next_Entity (E);
end loop;
end Restore_Private_Views;
----------------------------
-- Save_Global_References --
----------------------------
procedure Save_Global_References (N : Node_Id) is
Gen_Scope : Entity_Id;
E : Entity_Id;
N2 : Node_Id;
function Is_Global (E : Entity_Id) return Boolean;
-- Check whether entity is defined outside of generic unit.
-- Examine the scope of an entity, and the scope of the scope,
-- etc, until we find either Standard, in which case the entity
-- is global, or the generic unit itself, which indicates that
-- the entity is local. If the entity is the generic unit itself,
-- as in the case of a recursive call, or the enclosing generic unit,
-- if different from the current scope, then it is local as well,
-- because it will be replaced at the point of instantiation.
procedure Reset_Entity (N : Node_Id);
-- Save semantic information on global entity, so that it is not
-- resolved again at instantiation time.
procedure Save_Global_Defaults (N1, N2 : Node_Id);
-- Default actuals in nested instances must be handled specially
-- because there is no link to them from the original tree. When an
-- actual subprogram is given by a default, we add an explicit generic
-- association for it in the instantiation node. When we save the
-- global references on the name of the instance, we recover the list
-- of generic associations, and add an explicit one to the original
-- generic tree, through which a global actual can be preserved.
procedure Save_Global_Descendant (D : Union_Id);
-- Apply Save_Global_References recursively to the descendents of
-- current node.
procedure Save_References (N : Node_Id);
-- This is the recursive procedure that does the work, once the
-- enclosing generic scope has been established.
---------------
-- Is_Global --
---------------
function Is_Global (E : Entity_Id) return Boolean is
Se : Entity_Id := Scope (E);
begin
if E = Gen_Scope then
return False;
elsif E = Standard_Standard then
return True;
else
while Se /= Gen_Scope loop
if Se = Standard_Standard then
return true;
else
Se := Scope (Se);
end if;
end loop;
return False;
end if;
end Is_Global;
----------------------------
-- Save_Global_Descendant --
----------------------------
procedure Save_Global_Descendant (D : Union_Id) is
N1 : Node_Id;
begin
if D in Node_Range then
if D = Union_Id (Empty) then
null;
elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
Save_References (Node_Id (D));
end if;
elsif D in List_Range then
if D = Union_Id (No_List)
or else Is_Empty_List (List_Id (D))
then
null;
else
N1 := First (List_Id (D));
while Present (N1) loop
Save_References (N1);
N1 := Next (N1);
end loop;
end if;
-- Element list or other non-node field, nothing to do
else
null;
end if;
end Save_Global_Descendant;
--------------------------
-- Save_Global_Defaults --
--------------------------
procedure Save_Global_Defaults (N1, N2 : Node_Id) is
Loc : constant Source_Ptr := Sloc (N1);
Assoc1 : List_Id := Generic_Associations (N1);
Assoc2 : List_Id := Generic_Associations (N2);
Act1 : Node_Id;
Act2 : Node_Id;
Def : Node_Id;
Ndec : Node_Id;
Subp : Entity_Id;
begin
if Present (Assoc1) then
Act1 := First (Assoc1);
else
Act1 := Empty;
end if;
if Present (Assoc2) then
Act2 := First (Assoc2);
else
return;
end if;
while Present (Act1) and then Present (Act2) loop
Act1 := Next (Act1);
Act2 := Next (Act2);
end loop;
-- Find the associations added for default suprograms.
if Present (Act2) then
while Nkind (Act2) /= N_Generic_Association
or else No (Entity (Selector_Name (Act2)))
or else not Is_Overloadable (Entity (Selector_Name (Act2)))
loop
Act2 := Next (Act2);
end loop;
-- Add a similar association if the default is global. The
-- renaming declaration for the actual has been analyzed, and
-- its alias is the program it renames. Link the actual in the
-- original generic tree with the node in the analyzed tree.
while Present (Act2) loop
Subp := Entity (Selector_Name (Act2));
Def := Explicit_Generic_Actual_Parameter (Act2);
Set_Entity (Def, Alias (Subp));
Set_Etype (Def, Etype (Alias (Subp)));
if Is_Global (Alias (Subp)) then
Ndec := Make_Generic_Association (Loc,
Selector_Name => New_Occurrence_Of (Subp, Loc),
Explicit_Generic_Actual_Parameter =>
New_Occurrence_Of (Alias (Subp), Loc));
Set_Associated_Node
(Explicit_Generic_Actual_Parameter (Ndec), Def);
Append (Ndec, Assoc1);
end if;
Act2 := Next (Act2);
end loop;
end if;
end Save_Global_Defaults;
------------------
-- Reset_Entity --
------------------
procedure Reset_Entity (N : Node_Id) is
procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
-- The type of N2 is global to the generic unit. Save the
-- type in the generic node.
procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
Typ : constant Entity_Id := Etype (N2);
begin
Set_Etype (N, Typ);
-- If not a private type, nothing else to do
if not Is_Private_Type (Typ) then
null;
-- If it is a derivation of a private type in a context where
-- no full view is needed, nothing to do either.
elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
null;
-- Otherwise mark the type for flipping and use the full_view
-- when available.
else
Set_Has_Private_View (N);
if Present (Full_View (Typ)) then
Set_Etype (N2, Full_View (Typ));
end if;
end if;
end Set_Global_Type;
-- Start of processing for Reset_Entity
begin
N2 := Associated_Node (N);
E := Entity (N2);
if Present (E) then
if Is_Global (E) then
Set_Global_Type (N, N2);
elsif Nkind (N) = N_Op_Concat
and then Is_Generic_Type (Etype (N2))
and then (Etype (Right_Opnd (N2)) = Etype (N2)
or else Etype (Right_Opnd (N2)) = Etype (N2))
and then Is_Intrinsic_Subprogram (E)
then
null;
else
-- Entity is local. Mark generic node as unresolved.
-- Note that now it does not have an entity.
Set_Associated_Node (N, Empty);
Set_Etype (N, Empty);
end if;
if (Nkind (Parent (N)) = N_Package_Instantiation
or else Nkind (Parent (N)) = N_Function_Instantiation
or else Nkind (Parent (N)) = N_Procedure_Instantiation)
and then N = Name (Parent (N))
then
Save_Global_Defaults (Parent (N), Parent (N2));
end if;
elsif Nkind (Parent (N)) = N_Selected_Component
and then Nkind (Parent (N2)) = N_Expanded_Name
then
if Is_Global (Entity (Parent (N2))) then
Change_Selected_Component_To_Expanded_Name (Parent (N));
Set_Associated_Node (Parent (N), Parent (N2));
Set_Global_Type (Parent (N), Parent (N2));
Save_Global_Descendant (Field2 (N));
Save_Global_Descendant (Field3 (N));
-- If this is a reference to the current generic entity,
-- replace it with a simple name. This is to avoid anomalies
-- when the enclosing scope is also a generic unit, in which
-- case the selected component will not resolve to the current
-- unit within an instance of the outer one.
elsif Entity (Parent (N2)) = Current_Scope then
Rewrite (Parent (N),
Make_Identifier (Sloc (N),
Chars => Chars (Selector_Name (Parent (N2)))));
end if;
if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
or else Nkind (Parent (Parent (N)))
= N_Function_Instantiation
or else Nkind (Parent (Parent (N)))
= N_Procedure_Instantiation)
and then Parent (N) = Name (Parent (Parent (N)))
then
Save_Global_Defaults
(Parent (Parent (N)), Parent (Parent ((N2))));
end if;
-- A selected component may denote a static constant that has
-- been folded. Make the same replacement in original tree.
elsif Nkind (Parent (N)) = N_Selected_Component
and then (Nkind (Parent (N2)) = N_Integer_Literal
or else Nkind (Parent (N2)) = N_Real_Literal)
then
Rewrite (Parent (N),
New_Copy (Parent (N2)));
Set_Analyzed (Parent (N), False);
else
-- Entity is local. Reset in generic unit, so that node
-- is resolved anew at the point of instantiation.
Set_Associated_Node (N, Empty);
Set_Etype (N, Empty);
end if;
end Reset_Entity;
----------------------
-- Save_References --
----------------------
-- This is the recursive procedure that does the work, once the
-- enclosing generic scope has been established. We have to treat
-- specially a number of node rewritings that are required by semantic
-- processing and which change the kind of nodes in the generic copy:
-- typically constant-folding, replacing an operator node by a string
-- literal, or a selected component by an expanded name. In each of
-- those cases, the transformation is propagated to the generic unit.
procedure Save_References (N : Node_Id) is
begin
if N = Empty then
null;
elsif (Nkind (N) = N_Character_Literal
or else Nkind (N) = N_Operator_Symbol)
then
if Nkind (N) = Nkind (Associated_Node (N)) then
Reset_Entity (N);
elsif Nkind (N) = N_Operator_Symbol
and then Nkind (Associated_Node (N)) = N_String_Literal
then
Change_Operator_Symbol_To_String_Literal (N);
end if;
elsif Nkind (N) in N_Op then
if Nkind (N) = Nkind (Associated_Node (N)) then
if Nkind (N) = N_Op_Concat then
Set_Is_Component_Left_Opnd (N,
Is_Component_Left_Opnd (Associated_Node (N)));
Set_Is_Component_Right_Opnd (N,
Is_Component_Right_Opnd (Associated_Node (N)));
end if;
Reset_Entity (N);
else
-- Node may be transformed into call to a user-defined operator
N2 := Associated_Node (N);
if Nkind (N2) = N_Function_Call then
E := Entity (Name (N2));
if Present (E)
and then Is_Global (E)
then
Set_Etype (N, Etype (N2));
else
Set_Associated_Node (N, Empty);
Set_Etype (N, Empty);
end if;
elsif Nkind (N2) = N_Integer_Literal
or else Nkind (N2) = N_Real_Literal
or else Nkind (N2) = N_String_Literal
or else (Nkind (N2) = N_Identifier
and then
Ekind (Entity (N2)) = E_Enumeration_Literal)
then
-- Operation was constant-folded, perform the same
-- replacement in generic.
Rewrite (N, New_Copy (N2));
Set_Analyzed (N, False);
end if;
end if;
-- Complete the check on operands.
Save_Global_Descendant (Field2 (N));
Save_Global_Descendant (Field3 (N));
elsif Nkind (N) = N_Identifier then
if Nkind (N) = Nkind (Associated_Node (N)) then
-- If this is a discriminant reference, always save it.
-- It is used in the instance to find the corresponding
-- discriminant positionally rather than by name.
Set_Original_Discriminant
(N, Original_Discriminant (Associated_Node (N)));
Reset_Entity (N);
else
N2 := Associated_Node (N);
if Nkind (N2) = N_Function_Call then
E := Entity (Name (N2));
-- Name resolves to a call to parameterless function.
-- If original entity is global, mark node as resolved.
if Present (E)
and then Is_Global (E)
then
Set_Etype (N, Etype (N2));
else
Set_Associated_Node (N, Empty);
Set_Etype (N, Empty);
end if;
elsif
Nkind (N2) = N_Integer_Literal or else
Nkind (N2) = N_Real_Literal or else
Nkind (N2) = N_String_Literal
then
-- Name resolves to named number that is constant-folded,
-- or to string literal from concatenation.
-- Perform the same replacement in generic.
Rewrite (N, New_Copy (N2));
Set_Analyzed (N, False);
elsif Nkind (N2) = N_Explicit_Dereference then
-- An identifier is rewritten as a dereference if it is
-- the prefix in a selected component, and it denotes an
-- access to a composite type, or a parameterless function
-- call that returns an access type.
-- Check whether corresponding entity in prefix is global.
if Is_Entity_Name (Prefix (N2))
and then Present (Entity (Prefix (N2)))
and then Is_Global (Entity (Prefix (N2)))
then
Rewrite (N,
Make_Explicit_Dereference (Sloc (N),
Prefix => Make_Identifier (Sloc (N),
Chars => Chars (N))));
Set_Associated_Node (Prefix (N), Prefix (N2));
elsif Nkind (Prefix (N2)) = N_Function_Call
and then Is_Global (Entity (Name (Prefix (N2))))
then
Rewrite (N,
Make_Explicit_Dereference (Sloc (N),
Prefix => Make_Function_Call (Sloc (N),
Name =>
Make_Identifier (Sloc (N),
Chars => Chars (N)))));
Set_Associated_Node
(Name (Prefix (N)), Name (Prefix (N2)));
else
Set_Associated_Node (N, Empty);
Set_Etype (N, Empty);
end if;
else
null;
end if;
end if;
elsif Nkind (N) in N_Entity then
null;
elsif Nkind (N) = N_Aggregate
or else Nkind (N) = N_Extension_Aggregate
then
N2 := Associated_Node (N);
if No (N2)
or else No (Etype (N2))
or else not Is_Global (Etype (N2))
then
Set_Associated_Node (N, Empty);
end if;
Save_Global_Descendant (Field1 (N));
Save_Global_Descendant (Field2 (N));
Save_Global_Descendant (Field3 (N));
Save_Global_Descendant (Field5 (N));
else
Save_Global_Descendant (Field1 (N));
Save_Global_Descendant (Field2 (N));
Save_Global_Descendant (Field3 (N));
Save_Global_Descendant (Field4 (N));
Save_Global_Descendant (Field5 (N));
end if;
end Save_References;
-- Start of processing for Save_Global_References
begin
Gen_Scope := Current_Scope;
-- If the generic unit is a child unit, references to entities in
-- the parent are treated as local, because they will be resolved
-- anew in the context of the instance of the parent.
while Is_Child_Unit (Gen_Scope)
and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
loop
Gen_Scope := Scope (Gen_Scope);
end loop;
Save_References (N);
end Save_Global_References;
-------------------------
-- Set_Associated_Node --
-------------------------
procedure Set_Associated_Node
(Gen_Node : Node_Id;
Copy_Node : Node_Id)
is
begin
Set_Node4 (Gen_Node, Copy_Node);
end Set_Associated_Node;
---------------------
-- Set_Instance_Of --
---------------------
procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
begin
Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
Generic_Renamings_HTable.Set (Generic_Renamings.Last);
Generic_Renamings.Increment_Last;
end Set_Instance_Of;
end Sem_Ch12;
|