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
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- E X P _ C H 7 --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2005, 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. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
-- This package contains virtually all expansion mechanisms related to
-- - controlled types
-- - transient scopes
with Atree; use Atree;
with Debug; use Debug;
with Einfo; use Einfo;
with Errout; use Errout;
with Exp_Ch9; use Exp_Ch9;
with Exp_Ch11; use Exp_Ch11;
with Exp_Dbug; use Exp_Dbug;
with Exp_Tss; use Exp_Tss;
with Exp_Util; use Exp_Util;
with Freeze; use Freeze;
with Hostparm; use Hostparm;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Output; use Output;
with Restrict; use Restrict;
with Rident; use Rident;
with Rtsfind; use Rtsfind;
with Targparm; use Targparm;
with Sinfo; use Sinfo;
with Sem; use Sem;
with Sem_Ch3; use Sem_Ch3;
with Sem_Ch7; use Sem_Ch7;
with Sem_Ch8; use Sem_Ch8;
with Sem_Res; use Sem_Res;
with Sem_Type; use Sem_Type;
with Sem_Util; use Sem_Util;
with Snames; use Snames;
with Stand; use Stand;
with Tbuild; use Tbuild;
with Uintp; use Uintp;
package body Exp_Ch7 is
--------------------------------
-- Transient Scope Management --
--------------------------------
-- A transient scope is created when temporary objects are created by the
-- compiler. These temporary objects are allocated on the secondary stack
-- and the transient scope is responsible for finalizing the object when
-- appropriate and reclaiming the memory at the right time. The temporary
-- objects are generally the objects allocated to store the result of a
-- function returning an unconstrained or a tagged value. Expressions
-- needing to be wrapped in a transient scope (functions calls returning
-- unconstrained or tagged values) may appear in 3 different contexts which
-- lead to 3 different kinds of transient scope expansion:
-- 1. In a simple statement (procedure call, assignment, ...). In
-- this case the instruction is wrapped into a transient block.
-- (See Wrap_Transient_Statement for details)
-- 2. In an expression of a control structure (test in a IF statement,
-- expression in a CASE statement, ...).
-- (See Wrap_Transient_Expression for details)
-- 3. In a expression of an object_declaration. No wrapping is possible
-- here, so the finalization actions, if any are done right after the
-- declaration and the secondary stack deallocation is done in the
-- proper enclosing scope (see Wrap_Transient_Declaration for details)
-- Note about function returning tagged types: It has been decided to
-- always allocate their result in the secondary stack while it is not
-- absolutely mandatory when the tagged type is constrained because the
-- caller knows the size of the returned object and thus could allocate the
-- result in the primary stack. But, allocating them always in the
-- secondary stack simplifies many implementation hassles:
-- - If it is dispatching function call, the computation of the size of
-- the result is possible but complex from the outside.
-- - If the returned type is controlled, the assignment of the returned
-- value to the anonymous object involves an Adjust, and we have no
-- easy way to access the anonymous object created by the back-end
-- - If the returned type is class-wide, this is an unconstrained type
-- anyway
-- Furthermore, the little loss in efficiency which is the result of this
-- decision is not such a big deal because function returning tagged types
-- are not very much used in real life as opposed to functions returning
-- access to a tagged type
--------------------------------------------------
-- Transient Blocks and Finalization Management --
--------------------------------------------------
function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id;
-- N is a node wich may generate a transient scope. Loop over the
-- parent pointers of N until it find the appropriate node to
-- wrap. It it returns Empty, it means that no transient scope is
-- needed in this context.
function Make_Clean
(N : Node_Id;
Clean : Entity_Id;
Mark : Entity_Id;
Flist : Entity_Id;
Is_Task : Boolean;
Is_Master : Boolean;
Is_Protected_Subprogram : Boolean;
Is_Task_Allocation_Block : Boolean;
Is_Asynchronous_Call_Block : Boolean) return Node_Id;
-- Expand a the clean-up procedure for controlled and/or transient
-- block, and/or task master or task body, or blocks used to
-- implement task allocation or asynchronous entry calls, or
-- procedures used to implement protected procedures. Clean is the
-- entity for such a procedure. Mark is the entity for the secondary
-- stack mark, if empty only controlled block clean-up will be
-- performed. Flist is the entity for the local final list, if empty
-- only transient scope clean-up will be performed. The flags
-- Is_Task and Is_Master control the calls to the corresponding
-- finalization actions for a task body or for an entity that is a
-- task master.
procedure Set_Node_To_Be_Wrapped (N : Node_Id);
-- Set the field Node_To_Be_Wrapped of the current scope
procedure Insert_Actions_In_Scope_Around (N : Node_Id);
-- Insert the before-actions kept in the scope stack before N, and the
-- after after-actions, after N which must be a member of a list.
function Make_Transient_Block
(Loc : Source_Ptr;
Action : Node_Id) return Node_Id;
-- Create a transient block whose name is Scope, which is also a
-- controlled block if Flist is not empty and whose only code is
-- Action (either a single statement or single declaration).
type Final_Primitives is (Initialize_Case, Adjust_Case, Finalize_Case);
-- This enumeration type is defined in order to ease sharing code for
-- building finalization procedures for composite types.
Name_Of : constant array (Final_Primitives) of Name_Id :=
(Initialize_Case => Name_Initialize,
Adjust_Case => Name_Adjust,
Finalize_Case => Name_Finalize);
Deep_Name_Of : constant array (Final_Primitives) of TSS_Name_Type :=
(Initialize_Case => TSS_Deep_Initialize,
Adjust_Case => TSS_Deep_Adjust,
Finalize_Case => TSS_Deep_Finalize);
procedure Build_Record_Deep_Procs (Typ : Entity_Id);
-- Build the deep Initialize/Adjust/Finalize for a record Typ with
-- Has_Component_Component set and store them using the TSS mechanism.
procedure Build_Array_Deep_Procs (Typ : Entity_Id);
-- Build the deep Initialize/Adjust/Finalize for a record Typ with
-- Has_Controlled_Component set and store them using the TSS mechanism.
function Make_Deep_Proc
(Prim : Final_Primitives;
Typ : Entity_Id;
Stmts : List_Id) return Node_Id;
-- This function generates the tree for Deep_Initialize, Deep_Adjust
-- or Deep_Finalize procedures according to the first parameter,
-- these procedures operate on the type Typ. The Stmts parameter
-- gives the body of the procedure.
function Make_Deep_Array_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id;
-- This function generates the list of statements for implementing
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures
-- according to the first parameter, these procedures operate on the
-- array type Typ.
function Make_Deep_Record_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id;
-- This function generates the list of statements for implementing
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures
-- according to the first parameter, these procedures operate on the
-- record type Typ.
procedure Check_Visibly_Controlled
(Prim : Final_Primitives;
Typ : Entity_Id;
E : in out Entity_Id;
Cref : in out Node_Id);
-- The controlled operation declared for a derived type may not be
-- overriding, if the controlled operations of the parent type are
-- hidden, for example when the parent is a private type whose full
-- view is controlled. For other primitive operations we modify the
-- name of the operation to indicate that it is not overriding, but
-- this is not possible for Initialize, etc. because they have to be
-- retrievable by name. Before generating the proper call to one of
-- these operations we check whether Typ is known to be controlled at
-- the point of definition. If it is not then we must retrieve the
-- hidden operation of the parent and use it instead. This is one
-- case that might be solved more cleanly once Overriding pragmas or
-- declarations are in place.
function Convert_View
(Proc : Entity_Id;
Arg : Node_Id;
Ind : Pos := 1) return Node_Id;
-- Proc is one of the Initialize/Adjust/Finalize operations, and
-- Arg is the argument being passed to it. Ind indicates which
-- formal of procedure Proc we are trying to match. This function
-- will, if necessary, generate an conversion between the partial
-- and full view of Arg to match the type of the formal of Proc,
-- or force a conversion to the class-wide type in the case where
-- the operation is abstract.
-----------------------------
-- Finalization Management --
-----------------------------
-- This part describe how Initialization/Adjusment/Finalization procedures
-- are generated and called. Two cases must be considered, types that are
-- Controlled (Is_Controlled flag set) and composite types that contain
-- controlled components (Has_Controlled_Component flag set). In the first
-- case the procedures to call are the user-defined primitive operations
-- Initialize/Adjust/Finalize. In the second case, GNAT generates
-- Deep_Initialize, Deep_Adjust and Deep_Finalize that are in charge of
-- calling the former procedures on the controlled components.
-- For records with Has_Controlled_Component set, a hidden "controller"
-- component is inserted. This controller component contains its own
-- finalization list on which all controlled components are attached
-- creating an indirection on the upper-level Finalization list. This
-- technique facilitates the management of objects whose number of
-- controlled components changes during execution. This controller
-- component is itself controlled and is attached to the upper-level
-- finalization chain. Its adjust primitive is in charge of calling
-- adjust on the components and adusting the finalization pointer to
-- match their new location (see a-finali.adb).
-- It is not possible to use a similar technique for arrays that have
-- Has_Controlled_Component set. In this case, deep procedures are
-- generated that call initialize/adjust/finalize + attachment or
-- detachment on the finalization list for all component.
-- Initialize calls: they are generated for declarations or dynamic
-- allocations of Controlled objects with no initial value. They are
-- always followed by an attachment to the current Finalization
-- Chain. For the dynamic allocation case this the chain attached to
-- the scope of the access type definition otherwise, this is the chain
-- of the current scope.
-- Adjust Calls: They are generated on 2 occasions: (1) for
-- declarations or dynamic allocations of Controlled objects with an
-- initial value. (2) after an assignment. In the first case they are
-- followed by an attachment to the final chain, in the second case
-- they are not.
-- Finalization Calls: They are generated on (1) scope exit, (2)
-- assignments, (3) unchecked deallocations. In case (3) they have to
-- be detached from the final chain, in case (2) they must not and in
-- case (1) this is not important since we are exiting the scope
-- anyway.
-- Other details:
-- - Type extensions will have a new record controller at each derivation
-- level containing controlled components.
-- - For types that are both Is_Controlled and Has_Controlled_Components,
-- the record controller and the object itself are handled separately.
-- It could seem simpler to attach the object at the end of its record
-- controller but this would not tackle view conversions properly.
-- - A classwide type can always potentially have controlled components
-- but the record controller of the corresponding actual type may not
-- be known at compile time so the dispatch table contains a special
-- field that allows to compute the offset of the record controller
-- dynamically. See s-finimp.Deep_Tag_Attach and a-tags.RC_Offset
-- Here is a simple example of the expansion of a controlled block :
-- declare
-- X : Controlled ;
-- Y : Controlled := Init;
--
-- type R is record
-- C : Controlled;
-- end record;
-- W : R;
-- Z : R := (C => X);
-- begin
-- X := Y;
-- W := Z;
-- end;
--
-- is expanded into
--
-- declare
-- _L : System.FI.Finalizable_Ptr;
-- procedure _Clean is
-- begin
-- Abort_Defer;
-- System.FI.Finalize_List (_L);
-- Abort_Undefer;
-- end _Clean;
-- X : Controlled;
-- begin
-- Abort_Defer;
-- Initialize (X);
-- Attach_To_Final_List (_L, Finalizable (X), 1);
-- at end: Abort_Undefer;
-- Y : Controlled := Init;
-- Adjust (Y);
-- Attach_To_Final_List (_L, Finalizable (Y), 1);
--
-- type R is record
-- _C : Record_Controller;
-- C : Controlled;
-- end record;
-- W : R;
-- begin
-- Abort_Defer;
-- Deep_Initialize (W, _L, 1);
-- at end: Abort_Under;
-- Z : R := (C => X);
-- Deep_Adjust (Z, _L, 1);
-- begin
-- _Assign (X, Y);
-- Deep_Finalize (W, False);
-- <save W's final pointers>
-- W := Z;
-- <restore W's final pointers>
-- Deep_Adjust (W, _L, 0);
-- at end
-- _Clean;
-- end;
function Global_Flist_Ref (Flist_Ref : Node_Id) return Boolean;
-- Return True if Flist_Ref refers to a global final list, either
-- the object GLobal_Final_List which is used to attach standalone
-- objects, or any of the list controllers associated with library
-- level access to controlled objects
procedure Clean_Simple_Protected_Objects (N : Node_Id);
-- Protected objects without entries are not controlled types, and the
-- locks have to be released explicitly when such an object goes out
-- of scope. Traverse declarations in scope to determine whether such
-- objects are present.
----------------------------
-- Build_Array_Deep_Procs --
----------------------------
procedure Build_Array_Deep_Procs (Typ : Entity_Id) is
begin
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Initialize_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Initialize_Case, Typ)));
if not Is_Return_By_Reference_Type (Typ) then
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Adjust_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Adjust_Case, Typ)));
end if;
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Finalize_Case,
Typ => Typ,
Stmts => Make_Deep_Array_Body (Finalize_Case, Typ)));
end Build_Array_Deep_Procs;
-----------------------------
-- Build_Controlling_Procs --
-----------------------------
procedure Build_Controlling_Procs (Typ : Entity_Id) is
begin
if Is_Array_Type (Typ) then
Build_Array_Deep_Procs (Typ);
else pragma Assert (Is_Record_Type (Typ));
Build_Record_Deep_Procs (Typ);
end if;
end Build_Controlling_Procs;
----------------------
-- Build_Final_List --
----------------------
procedure Build_Final_List (N : Node_Id; Typ : Entity_Id) is
Loc : constant Source_Ptr := Sloc (N);
Decl : Node_Id;
begin
Set_Associated_Final_Chain (Typ,
Make_Defining_Identifier (Loc,
New_External_Name (Chars (Typ), 'L')));
Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier =>
Associated_Final_Chain (Typ),
Object_Definition =>
New_Reference_To
(RTE (RE_List_Controller), Loc));
-- The type may have been frozen already, and this is a late freezing
-- action, in which case the declaration must be elaborated at once.
-- If the call is for an allocator, the chain must also be created now,
-- because the freezing of the type does not build one. Otherwise, the
-- declaration is one of the freezing actions for a user-defined type.
if Is_Frozen (Typ)
or else (Nkind (N) = N_Allocator
and then Ekind (Etype (N)) = E_Anonymous_Access_Type)
then
Insert_Action (N, Decl);
else
Append_Freeze_Action (Typ, Decl);
end if;
end Build_Final_List;
---------------------
-- Build_Late_Proc --
---------------------
procedure Build_Late_Proc (Typ : Entity_Id; Nam : Name_Id) is
begin
for Final_Prim in Name_Of'Range loop
if Name_Of (Final_Prim) = Nam then
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Final_Prim,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Final_Prim, Typ)));
end if;
end loop;
end Build_Late_Proc;
-----------------------------
-- Build_Record_Deep_Procs --
-----------------------------
procedure Build_Record_Deep_Procs (Typ : Entity_Id) is
begin
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Initialize_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Initialize_Case, Typ)));
if not Is_Return_By_Reference_Type (Typ) then
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Adjust_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Adjust_Case, Typ)));
end if;
Set_TSS (Typ,
Make_Deep_Proc (
Prim => Finalize_Case,
Typ => Typ,
Stmts => Make_Deep_Record_Body (Finalize_Case, Typ)));
end Build_Record_Deep_Procs;
-------------------
-- Cleanup_Array --
-------------------
function Cleanup_Array
(N : Node_Id;
Obj : Node_Id;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (N);
Index_List : constant List_Id := New_List;
function Free_Component return List_Id;
-- Generate the code to finalize the task or protected subcomponents
-- of a single component of the array.
function Free_One_Dimension (Dim : Int) return List_Id;
-- Generate a loop over one dimension of the array
--------------------
-- Free_Component --
--------------------
function Free_Component return List_Id is
Stmts : List_Id := New_List;
Tsk : Node_Id;
C_Typ : constant Entity_Id := Component_Type (Typ);
begin
-- Component type is known to contain tasks or protected objects
Tsk :=
Make_Indexed_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Obj),
Expressions => Index_List);
Set_Etype (Tsk, C_Typ);
if Is_Task_Type (C_Typ) then
Append_To (Stmts, Cleanup_Task (N, Tsk));
elsif Is_Simple_Protected_Type (C_Typ) then
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
elsif Is_Record_Type (C_Typ) then
Stmts := Cleanup_Record (N, Tsk, C_Typ);
elsif Is_Array_Type (C_Typ) then
Stmts := Cleanup_Array (N, Tsk, C_Typ);
end if;
return Stmts;
end Free_Component;
------------------------
-- Free_One_Dimension --
------------------------
function Free_One_Dimension (Dim : Int) return List_Id is
Index : Entity_Id;
begin
if Dim > Number_Dimensions (Typ) then
return Free_Component;
-- Here we generate the required loop
else
Index :=
Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
Append (New_Reference_To (Index, Loc), Index_List);
return New_List (
Make_Implicit_Loop_Statement (N,
Identifier => Empty,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Index,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr (Obj),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Dim))))),
Statements => Free_One_Dimension (Dim + 1)));
end if;
end Free_One_Dimension;
-- Start of processing for Cleanup_Array
begin
return Free_One_Dimension (1);
end Cleanup_Array;
--------------------
-- Cleanup_Record --
--------------------
function Cleanup_Record
(N : Node_Id;
Obj : Node_Id;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (N);
Tsk : Node_Id;
Comp : Entity_Id;
Stmts : constant List_Id := New_List;
U_Typ : constant Entity_Id := Underlying_Type (Typ);
begin
if Has_Discriminants (U_Typ)
and then Nkind (Parent (U_Typ)) = N_Full_Type_Declaration
and then
Nkind (Type_Definition (Parent (U_Typ))) = N_Record_Definition
and then
Present
(Variant_Part
(Component_List (Type_Definition (Parent (U_Typ)))))
then
-- For now, do not attempt to free a component that may appear in
-- a variant, and instead issue a warning. Doing this "properly"
-- would require building a case statement and would be quite a
-- mess. Note that the RM only requires that free "work" for the
-- case of a task access value, so already we go way beyond this
-- in that we deal with the array case and non-discriminated
-- record cases.
Error_Msg_N
("task/protected object in variant record will not be freed?", N);
return New_List (Make_Null_Statement (Loc));
end if;
Comp := First_Component (Typ);
while Present (Comp) loop
if Has_Task (Etype (Comp))
or else Has_Simple_Protected_Object (Etype (Comp))
then
Tsk :=
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Obj),
Selector_Name => New_Occurrence_Of (Comp, Loc));
Set_Etype (Tsk, Etype (Comp));
if Is_Task_Type (Etype (Comp)) then
Append_To (Stmts, Cleanup_Task (N, Tsk));
elsif Is_Simple_Protected_Type (Etype (Comp)) then
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
elsif Is_Record_Type (Etype (Comp)) then
-- Recurse, by generating the prefix of the argument to
-- the eventual cleanup call.
Append_List_To
(Stmts, Cleanup_Record (N, Tsk, Etype (Comp)));
elsif Is_Array_Type (Etype (Comp)) then
Append_List_To
(Stmts, Cleanup_Array (N, Tsk, Etype (Comp)));
end if;
end if;
Next_Component (Comp);
end loop;
return Stmts;
end Cleanup_Record;
------------------------------
-- Cleanup_Protected_Object --
------------------------------
function Cleanup_Protected_Object
(N : Node_Id;
Ref : Node_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
begin
return
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Finalize_Protection), Loc),
Parameter_Associations => New_List (
Concurrent_Ref (Ref)));
end Cleanup_Protected_Object;
------------------------------------
-- Clean_Simple_Protected_Objects --
------------------------------------
procedure Clean_Simple_Protected_Objects (N : Node_Id) is
Stmts : constant List_Id := Statements (Handled_Statement_Sequence (N));
Stmt : Node_Id := Last (Stmts);
E : Entity_Id;
begin
E := First_Entity (Current_Scope);
while Present (E) loop
if (Ekind (E) = E_Variable
or else Ekind (E) = E_Constant)
and then Has_Simple_Protected_Object (Etype (E))
and then not Has_Task (Etype (E))
and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
then
declare
Typ : constant Entity_Id := Etype (E);
Ref : constant Node_Id := New_Occurrence_Of (E, Sloc (Stmt));
begin
if Is_Simple_Protected_Type (Typ) then
Append_To (Stmts, Cleanup_Protected_Object (N, Ref));
elsif Has_Simple_Protected_Object (Typ) then
if Is_Record_Type (Typ) then
Append_List_To (Stmts, Cleanup_Record (N, Ref, Typ));
elsif Is_Array_Type (Typ) then
Append_List_To (Stmts, Cleanup_Array (N, Ref, Typ));
end if;
end if;
end;
end if;
Next_Entity (E);
end loop;
-- Analyze inserted cleanup statements
if Present (Stmt) then
Stmt := Next (Stmt);
while Present (Stmt) loop
Analyze (Stmt);
Next (Stmt);
end loop;
end if;
end Clean_Simple_Protected_Objects;
------------------
-- Cleanup_Task --
------------------
function Cleanup_Task
(N : Node_Id;
Ref : Node_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
begin
return
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Free_Task), Loc),
Parameter_Associations =>
New_List (Concurrent_Ref (Ref)));
end Cleanup_Task;
---------------------------------
-- Has_Simple_Protected_Object --
---------------------------------
function Has_Simple_Protected_Object (T : Entity_Id) return Boolean is
Comp : Entity_Id;
begin
if Is_Simple_Protected_Type (T) then
return True;
elsif Is_Array_Type (T) then
return Has_Simple_Protected_Object (Component_Type (T));
elsif Is_Record_Type (T) then
Comp := First_Component (T);
while Present (Comp) loop
if Has_Simple_Protected_Object (Etype (Comp)) then
return True;
end if;
Next_Component (Comp);
end loop;
return False;
else
return False;
end if;
end Has_Simple_Protected_Object;
------------------------------
-- Is_Simple_Protected_Type --
------------------------------
function Is_Simple_Protected_Type (T : Entity_Id) return Boolean is
begin
return Is_Protected_Type (T) and then not Has_Entries (T);
end Is_Simple_Protected_Type;
------------------------------
-- Check_Visibly_Controlled --
------------------------------
procedure Check_Visibly_Controlled
(Prim : Final_Primitives;
Typ : Entity_Id;
E : in out Entity_Id;
Cref : in out Node_Id)
is
Parent_Type : Entity_Id;
Op : Entity_Id;
begin
if Is_Derived_Type (Typ)
and then Comes_From_Source (E)
and then not Is_Overriding_Operation (E)
then
-- We know that the explicit operation on the type does not override
-- the inherited operation of the parent, and that the derivation
-- is from a private type that is not visibly controlled.
Parent_Type := Etype (Typ);
Op := Find_Prim_Op (Parent_Type, Name_Of (Prim));
if Present (Op) then
E := Op;
-- Wrap the object to be initialized into the proper
-- unchecked conversion, to be compatible with the operation
-- to be called.
if Nkind (Cref) = N_Unchecked_Type_Conversion then
Cref := Unchecked_Convert_To (Parent_Type, Expression (Cref));
else
Cref := Unchecked_Convert_To (Parent_Type, Cref);
end if;
end if;
end if;
end Check_Visibly_Controlled;
---------------------
-- Controlled_Type --
---------------------
function Controlled_Type (T : Entity_Id) return Boolean is
function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
-- If type is not frozen yet, check explicitly among its components,
-- because flag is not necessarily set.
-----------------------------------
-- Has_Some_Controlled_Component --
-----------------------------------
function Has_Some_Controlled_Component
(Rec : Entity_Id) return Boolean
is
Comp : Entity_Id;
begin
if Has_Controlled_Component (Rec) then
return True;
elsif not Is_Frozen (Rec) then
if Is_Record_Type (Rec) then
Comp := First_Entity (Rec);
while Present (Comp) loop
if not Is_Type (Comp)
and then Controlled_Type (Etype (Comp))
then
return True;
end if;
Next_Entity (Comp);
end loop;
return False;
elsif Is_Array_Type (Rec) then
return Is_Controlled (Component_Type (Rec));
else
return Has_Controlled_Component (Rec);
end if;
else
return False;
end if;
end Has_Some_Controlled_Component;
-- Start of processing for Controlled_Type
begin
-- Class-wide types must be treated as controlled because they may
-- contain an extension that has controlled components
-- We can skip this if finalization is not available
return (Is_Class_Wide_Type (T)
and then not In_Finalization_Root (T)
and then not Restriction_Active (No_Finalization))
or else Is_Controlled (T)
or else Has_Some_Controlled_Component (T)
or else (Is_Concurrent_Type (T)
and then Present (Corresponding_Record_Type (T))
and then Controlled_Type (Corresponding_Record_Type (T)));
end Controlled_Type;
--------------------------
-- Controller_Component --
--------------------------
function Controller_Component (Typ : Entity_Id) return Entity_Id is
T : Entity_Id := Base_Type (Typ);
Comp : Entity_Id;
Comp_Scop : Entity_Id;
Res : Entity_Id := Empty;
Res_Scop : Entity_Id := Empty;
begin
if Is_Class_Wide_Type (T) then
T := Root_Type (T);
end if;
if Is_Private_Type (T) then
T := Underlying_Type (T);
end if;
-- Fetch the outermost controller
Comp := First_Entity (T);
while Present (Comp) loop
if Chars (Comp) = Name_uController then
Comp_Scop := Scope (Original_Record_Component (Comp));
-- If this controller is at the outermost level, no need to
-- look for another one
if Comp_Scop = T then
return Comp;
-- Otherwise record the outermost one and continue looking
elsif Res = Empty or else Is_Ancestor (Res_Scop, Comp_Scop) then
Res := Comp;
Res_Scop := Comp_Scop;
end if;
end if;
Next_Entity (Comp);
end loop;
-- If we fall through the loop, there is no controller component
return Res;
end Controller_Component;
------------------
-- Convert_View --
------------------
function Convert_View
(Proc : Entity_Id;
Arg : Node_Id;
Ind : Pos := 1) return Node_Id
is
Fent : Entity_Id := First_Entity (Proc);
Ftyp : Entity_Id;
Atyp : Entity_Id;
begin
for J in 2 .. Ind loop
Next_Entity (Fent);
end loop;
Ftyp := Etype (Fent);
if Nkind (Arg) = N_Type_Conversion
or else Nkind (Arg) = N_Unchecked_Type_Conversion
then
Atyp := Entity (Subtype_Mark (Arg));
else
Atyp := Etype (Arg);
end if;
if Is_Abstract (Proc) and then Is_Tagged_Type (Ftyp) then
return Unchecked_Convert_To (Class_Wide_Type (Ftyp), Arg);
elsif Ftyp /= Atyp
and then Present (Atyp)
and then
(Is_Private_Type (Ftyp) or else Is_Private_Type (Atyp))
and then
Base_Type (Underlying_Type (Atyp)) =
Base_Type (Underlying_Type (Ftyp))
then
return Unchecked_Convert_To (Ftyp, Arg);
-- If the argument is already a conversion, as generated by
-- Make_Init_Call, set the target type to the type of the formal
-- directly, to avoid spurious typing problems.
elsif (Nkind (Arg) = N_Unchecked_Type_Conversion
or else Nkind (Arg) = N_Type_Conversion)
and then not Is_Class_Wide_Type (Atyp)
then
Set_Subtype_Mark (Arg, New_Occurrence_Of (Ftyp, Sloc (Arg)));
Set_Etype (Arg, Ftyp);
return Arg;
else
return Arg;
end if;
end Convert_View;
-------------------------------
-- Establish_Transient_Scope --
-------------------------------
-- This procedure is called each time a transient block has to be inserted
-- that is to say for each call to a function with unconstrained ot tagged
-- result. It creates a new scope on the stack scope in order to enclose
-- all transient variables generated
procedure Establish_Transient_Scope (N : Node_Id; Sec_Stack : Boolean) is
Loc : constant Source_Ptr := Sloc (N);
Wrap_Node : Node_Id;
Sec_Stk : constant Boolean :=
Sec_Stack and not Functions_Return_By_DSP_On_Target;
-- We never need a secondary stack if functions return by DSP
begin
-- Do not create a transient scope if we are already inside one
for S in reverse Scope_Stack.First .. Scope_Stack.Last loop
if Scope_Stack.Table (S).Is_Transient then
if Sec_Stk then
Set_Uses_Sec_Stack (Scope_Stack.Table (S).Entity);
end if;
return;
-- If we have encountered Standard there are no enclosing
-- transient scopes.
elsif Scope_Stack.Table (S).Entity = Standard_Standard then
exit;
end if;
end loop;
Wrap_Node := Find_Node_To_Be_Wrapped (N);
-- Case of no wrap node, false alert, no transient scope needed
if No (Wrap_Node) then
null;
-- If the node to wrap is an iteration_scheme, the expression is
-- one of the bounds, and the expansion will make an explicit
-- declaration for it (see Analyze_Iteration_Scheme, sem_ch5.adb),
-- so do not apply any transformations here.
elsif Nkind (Wrap_Node) = N_Iteration_Scheme then
null;
else
New_Scope (New_Internal_Entity (E_Block, Current_Scope, Loc, 'B'));
Set_Scope_Is_Transient;
if Sec_Stk then
Set_Uses_Sec_Stack (Current_Scope);
Check_Restriction (No_Secondary_Stack, N);
end if;
Set_Etype (Current_Scope, Standard_Void_Type);
Set_Node_To_Be_Wrapped (Wrap_Node);
if Debug_Flag_W then
Write_Str (" <Transient>");
Write_Eol;
end if;
end if;
end Establish_Transient_Scope;
----------------------------
-- Expand_Cleanup_Actions --
----------------------------
procedure Expand_Cleanup_Actions (N : Node_Id) is
Loc : Source_Ptr;
S : constant Entity_Id :=
Current_Scope;
Flist : constant Entity_Id :=
Finalization_Chain_Entity (S);
Is_Task : constant Boolean :=
(Nkind (Original_Node (N)) = N_Task_Body);
Is_Master : constant Boolean :=
Nkind (N) /= N_Entry_Body
and then Is_Task_Master (N);
Is_Protected : constant Boolean :=
Nkind (N) = N_Subprogram_Body
and then Is_Protected_Subprogram_Body (N);
Is_Task_Allocation : constant Boolean :=
Nkind (N) = N_Block_Statement
and then Is_Task_Allocation_Block (N);
Is_Asynchronous_Call : constant Boolean :=
Nkind (N) = N_Block_Statement
and then Is_Asynchronous_Call_Block (N);
Clean : Entity_Id;
Mark : Entity_Id := Empty;
New_Decls : constant List_Id := New_List;
Blok : Node_Id;
End_Lab : Node_Id;
Wrapped : Boolean;
Chain : Entity_Id := Empty;
Decl : Node_Id;
Old_Poll : Boolean;
begin
-- Compute a location that is not directly in the user code in
-- order to avoid to generate confusing debug info. A good
-- approximation is the name of the outer user-defined scope
declare
S1 : Entity_Id := S;
begin
while not Comes_From_Source (S1) and then S1 /= Standard_Standard loop
S1 := Scope (S1);
end loop;
Loc := Sloc (S1);
end;
-- There are cleanup actions only if the secondary stack needs
-- releasing or some finalizations are needed or in the context
-- of tasking
if Uses_Sec_Stack (Current_Scope)
and then not Sec_Stack_Needed_For_Return (Current_Scope)
then
null;
elsif No (Flist)
and then not Is_Master
and then not Is_Task
and then not Is_Protected
and then not Is_Task_Allocation
and then not Is_Asynchronous_Call
then
Clean_Simple_Protected_Objects (N);
return;
end if;
-- If the current scope is the subprogram body that is the rewriting
-- of a task body, and the descriptors have not been delayed (due to
-- some nested instantiations) do not generate redundant cleanup
-- actions: the cleanup procedure already exists for this body.
if Nkind (N) = N_Subprogram_Body
and then Nkind (Original_Node (N)) = N_Task_Body
and then not Delay_Subprogram_Descriptors (Corresponding_Spec (N))
then
return;
end if;
-- Set polling off, since we don't need to poll during cleanup
-- actions, and indeed for the cleanup routine, which is executed
-- with aborts deferred, we don't want polling.
Old_Poll := Polling_Required;
Polling_Required := False;
-- Make sure we have a declaration list, since we will add to it
if No (Declarations (N)) then
Set_Declarations (N, New_List);
end if;
-- The task activation call has already been built for task
-- allocation blocks.
if not Is_Task_Allocation then
Build_Task_Activation_Call (N);
end if;
if Is_Master then
Establish_Task_Master (N);
end if;
-- If secondary stack is in use, expand:
-- _Mxx : constant Mark_Id := SS_Mark;
-- Suppress calls to SS_Mark and SS_Release if Java_VM,
-- since we never use the secondary stack on the JVM.
if Uses_Sec_Stack (Current_Scope)
and then not Sec_Stack_Needed_For_Return (Current_Scope)
and then not Java_VM
then
Mark := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
Append_To (New_Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Mark,
Object_Definition => New_Reference_To (RTE (RE_Mark_Id), Loc),
Expression =>
Make_Function_Call (Loc,
Name => New_Reference_To (RTE (RE_SS_Mark), Loc))));
Set_Uses_Sec_Stack (Current_Scope, False);
end if;
-- If finalization list is present then expand:
-- Local_Final_List : System.FI.Finalizable_Ptr;
if Present (Flist) then
Append_To (New_Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Flist,
Object_Definition =>
New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
end if;
-- Clean-up procedure definition
Clean := Make_Defining_Identifier (Loc, Name_uClean);
Set_Suppress_Elaboration_Warnings (Clean);
Append_To (New_Decls,
Make_Clean (N, Clean, Mark, Flist,
Is_Task,
Is_Master,
Is_Protected,
Is_Task_Allocation,
Is_Asynchronous_Call));
-- If exception handlers are present, wrap the Sequence of
-- statements in a block because it is not possible to get
-- exception handlers and an AT END call in the same scope.
if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
-- Preserve end label to provide proper cross-reference information
End_Lab := End_Label (Handled_Statement_Sequence (N));
Blok :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence => Handled_Statement_Sequence (N));
Set_Handled_Statement_Sequence (N,
Make_Handled_Sequence_Of_Statements (Loc, New_List (Blok)));
Set_End_Label (Handled_Statement_Sequence (N), End_Lab);
Wrapped := True;
-- Otherwise we do not wrap
else
Wrapped := False;
Blok := Empty;
end if;
-- Don't move the _chain Activation_Chain declaration in task
-- allocation blocks. Task allocation blocks use this object
-- in their cleanup handlers, and gigi complains if it is declared
-- in the sequence of statements of the scope that declares the
-- handler.
if Is_Task_Allocation then
Chain := Activation_Chain_Entity (N);
Decl := First (Declarations (N));
while Nkind (Decl) /= N_Object_Declaration
or else Defining_Identifier (Decl) /= Chain
loop
Next (Decl);
pragma Assert (Present (Decl));
end loop;
Remove (Decl);
Prepend_To (New_Decls, Decl);
end if;
-- Now we move the declarations into the Sequence of statements
-- in order to get them protected by the AT END call. It may seem
-- weird to put declarations in the sequence of statement but in
-- fact nothing forbids that at the tree level. We also set the
-- First_Real_Statement field so that we remember where the real
-- statements (i.e. original statements) begin. Note that if we
-- wrapped the statements, the first real statement is inside the
-- inner block. If the First_Real_Statement is already set (as is
-- the case for subprogram bodies that are expansions of task bodies)
-- then do not reset it, because its declarative part would migrate
-- to the statement part.
if not Wrapped then
if No (First_Real_Statement (Handled_Statement_Sequence (N))) then
Set_First_Real_Statement (Handled_Statement_Sequence (N),
First (Statements (Handled_Statement_Sequence (N))));
end if;
else
Set_First_Real_Statement (Handled_Statement_Sequence (N), Blok);
end if;
Append_List_To (Declarations (N),
Statements (Handled_Statement_Sequence (N)));
Set_Statements (Handled_Statement_Sequence (N), Declarations (N));
-- We need to reset the Sloc of the handled statement sequence to
-- properly reflect the new initial "statement" in the sequence.
Set_Sloc
(Handled_Statement_Sequence (N), Sloc (First (Declarations (N))));
-- The declarations of the _Clean procedure and finalization chain
-- replace the old declarations that have been moved inward
Set_Declarations (N, New_Decls);
Analyze_Declarations (New_Decls);
-- The At_End call is attached to the sequence of statements
declare
HSS : Node_Id;
begin
-- If the construct is a protected subprogram, then the call to
-- the corresponding unprotected program appears in a block which
-- is the last statement in the body, and it is this block that
-- must be covered by the At_End handler.
if Is_Protected then
HSS := Handled_Statement_Sequence
(Last (Statements (Handled_Statement_Sequence (N))));
else
HSS := Handled_Statement_Sequence (N);
end if;
Set_At_End_Proc (HSS, New_Occurrence_Of (Clean, Loc));
Expand_At_End_Handler (HSS, Empty);
end;
-- Restore saved polling mode
Polling_Required := Old_Poll;
end Expand_Cleanup_Actions;
-------------------------------
-- Expand_Ctrl_Function_Call --
-------------------------------
procedure Expand_Ctrl_Function_Call (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Rtype : constant Entity_Id := Etype (N);
Utype : constant Entity_Id := Underlying_Type (Rtype);
Ref : Node_Id;
Action : Node_Id;
Action2 : Node_Id := Empty;
Attach_Level : Uint := Uint_1;
Len_Ref : Node_Id := Empty;
function Last_Array_Component
(Ref : Node_Id;
Typ : Entity_Id) return Node_Id;
-- Creates a reference to the last component of the array object
-- designated by Ref whose type is Typ.
--------------------------
-- Last_Array_Component --
--------------------------
function Last_Array_Component
(Ref : Node_Id;
Typ : Entity_Id) return Node_Id
is
Index_List : constant List_Id := New_List;
begin
for N in 1 .. Number_Dimensions (Typ) loop
Append_To (Index_List,
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Ref),
Attribute_Name => Name_Last,
Expressions => New_List (
Make_Integer_Literal (Loc, N))));
end loop;
return
Make_Indexed_Component (Loc,
Prefix => Duplicate_Subexpr (Ref),
Expressions => Index_List);
end Last_Array_Component;
-- Start of processing for Expand_Ctrl_Function_Call
begin
-- Optimization, if the returned value (which is on the sec-stack)
-- is returned again, no need to copy/readjust/finalize, we can just
-- pass the value thru (see Expand_N_Return_Statement), and thus no
-- attachment is needed
if Nkind (Parent (N)) = N_Return_Statement then
return;
end if;
-- Resolution is now finished, make sure we don't start analysis again
-- because of the duplication
Set_Analyzed (N);
Ref := Duplicate_Subexpr_No_Checks (N);
-- Now we can generate the Attach Call, note that this value is
-- always in the (secondary) stack and thus is attached to a singly
-- linked final list:
-- Resx := F (X)'reference;
-- Attach_To_Final_List (_Lx, Resx.all, 1);
-- or when there are controlled components
-- Attach_To_Final_List (_Lx, Resx._controller, 1);
-- or when it is both is_controlled and has_controlled_components
-- Attach_To_Final_List (_Lx, Resx._controller, 1);
-- Attach_To_Final_List (_Lx, Resx, 1);
-- or if it is an array with is_controlled (and has_controlled)
-- Attach_To_Final_List (_Lx, Resx (Resx'last), 3);
-- An attach level of 3 means that a whole array is to be
-- attached to the finalization list (including the controlled
-- components)
-- or if it is an array with has_controlled components but not
-- is_controlled
-- Attach_To_Final_List (_Lx, Resx (Resx'last)._controller, 3);
if Has_Controlled_Component (Rtype) then
declare
T1 : Entity_Id := Rtype;
T2 : Entity_Id := Utype;
begin
if Is_Array_Type (T2) then
Len_Ref :=
Make_Attribute_Reference (Loc,
Prefix =>
Duplicate_Subexpr_Move_Checks
(Unchecked_Convert_To (T2, Ref)),
Attribute_Name => Name_Length);
end if;
while Is_Array_Type (T2) loop
if T1 /= T2 then
Ref := Unchecked_Convert_To (T2, Ref);
end if;
Ref := Last_Array_Component (Ref, T2);
Attach_Level := Uint_3;
T1 := Component_Type (T2);
T2 := Underlying_Type (T1);
end loop;
-- If the type has controlled components, go to the controller
-- except in the case of arrays of controlled objects since in
-- this case objects and their components are already chained
-- and the head of the chain is the last array element.
if Is_Array_Type (Rtype) and then Is_Controlled (T2) then
null;
elsif Has_Controlled_Component (T2) then
if T1 /= T2 then
Ref := Unchecked_Convert_To (T2, Ref);
end if;
Ref :=
Make_Selected_Component (Loc,
Prefix => Ref,
Selector_Name => Make_Identifier (Loc, Name_uController));
end if;
end;
-- Here we know that 'Ref' has a controller so we may as well
-- attach it directly
Action :=
Make_Attach_Call (
Obj_Ref => Ref,
Flist_Ref => Find_Final_List (Current_Scope),
With_Attach => Make_Integer_Literal (Loc, Attach_Level));
-- If it is also Is_Controlled we need to attach the global object
if Is_Controlled (Rtype) then
Action2 :=
Make_Attach_Call (
Obj_Ref => Duplicate_Subexpr_No_Checks (N),
Flist_Ref => Find_Final_List (Current_Scope),
With_Attach => Make_Integer_Literal (Loc, Attach_Level));
end if;
else
-- Here, we have a controlled type that does not seem to have
-- controlled components but it could be a class wide type whose
-- further derivations have controlled components. So we don't know
-- if the object itself needs to be attached or if it
-- has a record controller. We need to call a runtime function
-- (Deep_Tag_Attach) which knows what to do thanks to the
-- RC_Offset in the dispatch table.
Action :=
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Deep_Tag_Attach), Loc),
Parameter_Associations => New_List (
Find_Final_List (Current_Scope),
Make_Attribute_Reference (Loc,
Prefix => Ref,
Attribute_Name => Name_Address),
Make_Integer_Literal (Loc, Attach_Level)));
end if;
if Present (Len_Ref) then
Action :=
Make_Implicit_If_Statement (N,
Condition => Make_Op_Gt (Loc,
Left_Opnd => Len_Ref,
Right_Opnd => Make_Integer_Literal (Loc, 0)),
Then_Statements => New_List (Action));
end if;
Insert_Action (N, Action);
if Present (Action2) then
Insert_Action (N, Action2);
end if;
end Expand_Ctrl_Function_Call;
---------------------------
-- Expand_N_Package_Body --
---------------------------
-- Add call to Activate_Tasks if body is an activator (actual
-- processing is in chapter 9).
-- Generate subprogram descriptor for elaboration routine
-- ENcode entity names in package body
procedure Expand_N_Package_Body (N : Node_Id) is
Ent : constant Entity_Id := Corresponding_Spec (N);
begin
-- This is done only for non-generic packages
if Ekind (Ent) = E_Package then
New_Scope (Corresponding_Spec (N));
Build_Task_Activation_Call (N);
Pop_Scope;
end if;
Set_Elaboration_Flag (N, Corresponding_Spec (N));
-- Generate a subprogram descriptor for the elaboration routine of
-- a package body if the package body has no pending instantiations
-- and it has generated at least one exception handler
if Present (Handler_Records (Body_Entity (Ent)))
and then Is_Compilation_Unit (Ent)
and then not Delay_Subprogram_Descriptors (Body_Entity (Ent))
then
Generate_Subprogram_Descriptor_For_Package
(N, Body_Entity (Ent));
end if;
Set_In_Package_Body (Ent, False);
-- Set to encode entity names in package body before gigi is called
Qualify_Entity_Names (N);
end Expand_N_Package_Body;
----------------------------------
-- Expand_N_Package_Declaration --
----------------------------------
-- Add call to Activate_Tasks if there are tasks declared and the
-- package has no body. Note that in Ada83, this may result in
-- premature activation of some tasks, given that we cannot tell
-- whether a body will eventually appear.
procedure Expand_N_Package_Declaration (N : Node_Id) is
begin
if Nkind (Parent (N)) = N_Compilation_Unit
and then not Body_Required (Parent (N))
and then not Unit_Requires_Body (Defining_Entity (N))
and then Present (Activation_Chain_Entity (N))
then
New_Scope (Defining_Entity (N));
Build_Task_Activation_Call (N);
Pop_Scope;
end if;
-- Note: it is not necessary to worry about generating a subprogram
-- descriptor, since the only way to get exception handlers into a
-- package spec is to include instantiations, and that would cause
-- generation of subprogram descriptors to be delayed in any case.
-- Set to encode entity names in package spec before gigi is called
Qualify_Entity_Names (N);
end Expand_N_Package_Declaration;
---------------------
-- Find_Final_List --
---------------------
function Find_Final_List
(E : Entity_Id;
Ref : Node_Id := Empty) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Ref);
S : Entity_Id;
Id : Entity_Id;
R : Node_Id;
begin
-- Case of an internal component. The Final list is the record
-- controller of the enclosing record
if Present (Ref) then
R := Ref;
loop
case Nkind (R) is
when N_Unchecked_Type_Conversion | N_Type_Conversion =>
R := Expression (R);
when N_Indexed_Component | N_Explicit_Dereference =>
R := Prefix (R);
when N_Selected_Component =>
R := Prefix (R);
exit;
when N_Identifier =>
exit;
when others =>
raise Program_Error;
end case;
end loop;
return
Make_Selected_Component (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix => R,
Selector_Name => Make_Identifier (Loc, Name_uController)),
Selector_Name => Make_Identifier (Loc, Name_F));
-- Case of a dynamically allocated object. The final list is the
-- corresponding list controller (The next entity in the scope of
-- the access type with the right type). If the type comes from a
-- With_Type clause, no controller was created, and we use the
-- global chain instead.
elsif Is_Access_Type (E) then
if not From_With_Type (E) then
return
Make_Selected_Component (Loc,
Prefix =>
New_Reference_To
(Associated_Final_Chain (Base_Type (E)), Loc),
Selector_Name => Make_Identifier (Loc, Name_F));
else
return New_Reference_To (RTE (RE_Global_Final_List), Sloc (E));
end if;
else
if Is_Dynamic_Scope (E) then
S := E;
else
S := Enclosing_Dynamic_Scope (E);
end if;
-- When the finalization chain entity is 'Error', it means that
-- there should not be any chain at that level and that the
-- enclosing one should be used
-- This is a nasty kludge, see ??? note in exp_ch11
while Finalization_Chain_Entity (S) = Error loop
S := Enclosing_Dynamic_Scope (S);
end loop;
if S = Standard_Standard then
return New_Reference_To (RTE (RE_Global_Final_List), Sloc (E));
else
if No (Finalization_Chain_Entity (S)) then
Id := Make_Defining_Identifier (Sloc (S),
New_Internal_Name ('F'));
Set_Finalization_Chain_Entity (S, Id);
-- Set momentarily some semantics attributes to allow normal
-- analysis of expansions containing references to this chain.
-- Will be fully decorated during the expansion of the scope
-- itself
Set_Ekind (Id, E_Variable);
Set_Etype (Id, RTE (RE_Finalizable_Ptr));
end if;
return New_Reference_To (Finalization_Chain_Entity (S), Sloc (E));
end if;
end if;
end Find_Final_List;
-----------------------------
-- Find_Node_To_Be_Wrapped --
-----------------------------
function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id is
P : Node_Id;
The_Parent : Node_Id;
begin
The_Parent := N;
loop
P := The_Parent;
pragma Assert (P /= Empty);
The_Parent := Parent (P);
case Nkind (The_Parent) is
-- Simple statement can be wrapped
when N_Pragma =>
return The_Parent;
-- Usually assignments are good candidate for wrapping
-- except when they have been generated as part of a
-- controlled aggregate where the wrapping should take
-- place more globally.
when N_Assignment_Statement =>
if No_Ctrl_Actions (The_Parent) then
null;
else
return The_Parent;
end if;
-- An entry call statement is a special case if it occurs in
-- the context of a Timed_Entry_Call. In this case we wrap
-- the entire timed entry call.
when N_Entry_Call_Statement |
N_Procedure_Call_Statement =>
if Nkind (Parent (The_Parent)) = N_Entry_Call_Alternative
and then
(Nkind (Parent (Parent (The_Parent)))
= N_Timed_Entry_Call
or else
Nkind (Parent (Parent (The_Parent)))
= N_Conditional_Entry_Call)
then
return Parent (Parent (The_Parent));
else
return The_Parent;
end if;
-- Object declarations are also a boundary for the transient scope
-- even if they are not really wrapped
-- (see Wrap_Transient_Declaration)
when N_Object_Declaration |
N_Object_Renaming_Declaration |
N_Subtype_Declaration =>
return The_Parent;
-- The expression itself is to be wrapped if its parent is a
-- compound statement or any other statement where the expression
-- is known to be scalar
when N_Accept_Alternative |
N_Attribute_Definition_Clause |
N_Case_Statement |
N_Code_Statement |
N_Delay_Alternative |
N_Delay_Until_Statement |
N_Delay_Relative_Statement |
N_Discriminant_Association |
N_Elsif_Part |
N_Entry_Body_Formal_Part |
N_Exit_Statement |
N_If_Statement |
N_Iteration_Scheme |
N_Terminate_Alternative =>
return P;
when N_Attribute_Reference =>
if Is_Procedure_Attribute_Name
(Attribute_Name (The_Parent))
then
return The_Parent;
end if;
-- If the expression is within the iteration scheme of a loop,
-- we must create a declaration for it, followed by an assignment
-- in order to have a usable statement to wrap.
when N_Loop_Parameter_Specification =>
return Parent (The_Parent);
-- The following nodes contains "dummy calls" which don't
-- need to be wrapped.
when N_Parameter_Specification |
N_Discriminant_Specification |
N_Component_Declaration =>
return Empty;
-- The return statement is not to be wrapped when the function
-- itself needs wrapping at the outer-level
when N_Return_Statement =>
if Requires_Transient_Scope (Return_Type (The_Parent)) then
return Empty;
else
return The_Parent;
end if;
-- If we leave a scope without having been able to find a node to
-- wrap, something is going wrong but this can happen in error
-- situation that are not detected yet (such as a dynamic string
-- in a pragma export)
when N_Subprogram_Body |
N_Package_Declaration |
N_Package_Body |
N_Block_Statement =>
return Empty;
-- otherwise continue the search
when others =>
null;
end case;
end loop;
end Find_Node_To_Be_Wrapped;
----------------------
-- Global_Flist_Ref --
----------------------
function Global_Flist_Ref (Flist_Ref : Node_Id) return Boolean is
Flist : Entity_Id;
begin
-- Look for the Global_Final_List
if Is_Entity_Name (Flist_Ref) then
Flist := Entity (Flist_Ref);
-- Look for the final list associated with an access to controlled
elsif Nkind (Flist_Ref) = N_Selected_Component
and then Is_Entity_Name (Prefix (Flist_Ref))
then
Flist := Entity (Prefix (Flist_Ref));
else
return False;
end if;
return Present (Flist)
and then Present (Scope (Flist))
and then Enclosing_Dynamic_Scope (Flist) = Standard_Standard;
end Global_Flist_Ref;
----------------------------------
-- Has_New_Controlled_Component --
----------------------------------
function Has_New_Controlled_Component (E : Entity_Id) return Boolean is
Comp : Entity_Id;
begin
if not Is_Tagged_Type (E) then
return Has_Controlled_Component (E);
elsif not Is_Derived_Type (E) then
return Has_Controlled_Component (E);
end if;
Comp := First_Component (E);
while Present (Comp) loop
if Chars (Comp) = Name_uParent then
null;
elsif Scope (Original_Record_Component (Comp)) = E
and then Controlled_Type (Etype (Comp))
then
return True;
end if;
Next_Component (Comp);
end loop;
return False;
end Has_New_Controlled_Component;
--------------------------
-- In_Finalization_Root --
--------------------------
-- It would seem simpler to test Scope (RTE (RE_Root_Controlled)) but
-- the purpose of this function is to avoid a circular call to Rtsfind
-- which would been caused by such a test.
function In_Finalization_Root (E : Entity_Id) return Boolean is
S : constant Entity_Id := Scope (E);
begin
return Chars (Scope (S)) = Name_System
and then Chars (S) = Name_Finalization_Root
and then Scope (Scope (S)) = Standard_Standard;
end In_Finalization_Root;
------------------------------------
-- Insert_Actions_In_Scope_Around --
------------------------------------
procedure Insert_Actions_In_Scope_Around (N : Node_Id) is
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
Target : Node_Id;
begin
-- If the node to be wrapped is the triggering alternative of an
-- asynchronous select, it is not part of a statement list. The
-- actions must be inserted before the Select itself, which is
-- part of some list of statements.
if Nkind (Parent (Node_To_Be_Wrapped)) = N_Triggering_Alternative then
Target := Parent (Parent (Node_To_Be_Wrapped));
else
Target := N;
end if;
if Present (SE.Actions_To_Be_Wrapped_Before) then
Insert_List_Before (Target, SE.Actions_To_Be_Wrapped_Before);
SE.Actions_To_Be_Wrapped_Before := No_List;
end if;
if Present (SE.Actions_To_Be_Wrapped_After) then
Insert_List_After (Target, SE.Actions_To_Be_Wrapped_After);
SE.Actions_To_Be_Wrapped_After := No_List;
end if;
end Insert_Actions_In_Scope_Around;
-----------------------
-- Make_Adjust_Call --
-----------------------
function Make_Adjust_Call
(Ref : Node_Id;
Typ : Entity_Id;
Flist_Ref : Node_Id;
With_Attach : Node_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (Ref);
Res : constant List_Id := New_List;
Utyp : Entity_Id;
Proc : Entity_Id;
Cref : Node_Id := Ref;
Cref2 : Node_Id;
Attach : Node_Id := With_Attach;
begin
if Is_Class_Wide_Type (Typ) then
Utyp := Underlying_Type (Base_Type (Root_Type (Typ)));
else
Utyp := Underlying_Type (Base_Type (Typ));
end if;
Set_Assignment_OK (Cref);
-- Deal with non-tagged derivation of private views
if Is_Untagged_Derivation (Typ) then
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
Cref := Unchecked_Convert_To (Utyp, Cref);
Set_Assignment_OK (Cref);
-- To prevent problems with UC see 1.156 RH ???
end if;
-- If the underlying_type is a subtype, we are dealing with
-- the completion of a private type. We need to access
-- the base type and generate a conversion to it.
if Utyp /= Base_Type (Utyp) then
pragma Assert (Is_Private_Type (Typ));
Utyp := Base_Type (Utyp);
Cref := Unchecked_Convert_To (Utyp, Cref);
end if;
-- If the object is unanalyzed, set its expected type for use
-- in Convert_View in case an additional conversion is needed.
if No (Etype (Cref))
and then Nkind (Cref) /= N_Unchecked_Type_Conversion
then
Set_Etype (Cref, Typ);
end if;
-- We do not need to attach to one of the Global Final Lists
-- the objects whose type is Finalize_Storage_Only
if Finalize_Storage_Only (Typ)
and then (Global_Flist_Ref (Flist_Ref)
or else Entity (Constant_Value (RTE (RE_Garbage_Collected)))
= Standard_True)
then
Attach := Make_Integer_Literal (Loc, 0);
end if;
-- Generate:
-- Deep_Adjust (Flist_Ref, Ref, With_Attach);
if Has_Controlled_Component (Utyp)
or else Is_Class_Wide_Type (Typ)
then
if Is_Tagged_Type (Utyp) then
Proc := Find_Prim_Op (Utyp, TSS_Deep_Adjust);
else
Proc := TSS (Utyp, TSS_Deep_Adjust);
end if;
Cref := Convert_View (Proc, Cref, 2);
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations =>
New_List (Flist_Ref, Cref, Attach)));
-- Generate:
-- if With_Attach then
-- Attach_To_Final_List (Ref, Flist_Ref);
-- end if;
-- Adjust (Ref);
else -- Is_Controlled (Utyp)
Proc := Find_Prim_Op (Utyp, Name_Of (Adjust_Case));
Cref := Convert_View (Proc, Cref);
Cref2 := New_Copy_Tree (Cref);
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations => New_List (Cref2)));
Append_To (Res, Make_Attach_Call (Cref, Flist_Ref, Attach));
end if;
return Res;
end Make_Adjust_Call;
----------------------
-- Make_Attach_Call --
----------------------
-- Generate:
-- System.FI.Attach_To_Final_List (Flist, Ref, Nb_Link)
function Make_Attach_Call
(Obj_Ref : Node_Id;
Flist_Ref : Node_Id;
With_Attach : Node_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Obj_Ref);
begin
-- Optimization: If the number of links is statically '0', don't
-- call the attach_proc.
if Nkind (With_Attach) = N_Integer_Literal
and then Intval (With_Attach) = Uint_0
then
return Make_Null_Statement (Loc);
end if;
return
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Attach_To_Final_List), Loc),
Parameter_Associations => New_List (
Flist_Ref,
OK_Convert_To (RTE (RE_Finalizable), Obj_Ref),
With_Attach));
end Make_Attach_Call;
----------------
-- Make_Clean --
----------------
function Make_Clean
(N : Node_Id;
Clean : Entity_Id;
Mark : Entity_Id;
Flist : Entity_Id;
Is_Task : Boolean;
Is_Master : Boolean;
Is_Protected_Subprogram : Boolean;
Is_Task_Allocation_Block : Boolean;
Is_Asynchronous_Call_Block : Boolean) return Node_Id
is
Loc : constant Source_Ptr := Sloc (Clean);
Stmt : constant List_Id := New_List;
Sbody : Node_Id;
Spec : Node_Id;
Name : Node_Id;
Param : Node_Id;
Param_Type : Entity_Id;
Pid : Entity_Id := Empty;
Cancel_Param : Entity_Id;
begin
if Is_Task then
if Restricted_Profile then
Append_To
(Stmt, Build_Runtime_Call (Loc, RE_Complete_Restricted_Task));
else
Append_To (Stmt, Build_Runtime_Call (Loc, RE_Complete_Task));
end if;
elsif Is_Master then
if Restriction_Active (No_Task_Hierarchy) = False then
Append_To (Stmt, Build_Runtime_Call (Loc, RE_Complete_Master));
end if;
elsif Is_Protected_Subprogram then
-- Add statements to the cleanup handler of the (ordinary)
-- subprogram expanded to implement a protected subprogram,
-- unlocking the protected object parameter and undeferring abort.
-- If this is a protected procedure, and the object contains
-- entries, this also calls the entry service routine.
-- NOTE: This cleanup handler references _object, a parameter
-- to the procedure.
-- Find the _object parameter representing the protected object
Spec := Parent (Corresponding_Spec (N));
Param := First (Parameter_Specifications (Spec));
loop
Param_Type := Etype (Parameter_Type (Param));
if Ekind (Param_Type) = E_Record_Type then
Pid := Corresponding_Concurrent_Type (Param_Type);
end if;
exit when not Present (Param) or else Present (Pid);
Next (Param);
end loop;
pragma Assert (Present (Param));
-- If the associated protected object declares entries,
-- a protected procedure has to service entry queues.
-- In this case, add
-- Service_Entries (_object._object'Access);
-- _object is the record used to implement the protected object.
-- It is a parameter to the protected subprogram.
if Nkind (Specification (N)) = N_Procedure_Specification
and then Has_Entries (Pid)
then
if Abort_Allowed
or else Restriction_Active (No_Entry_Queue) = False
or else Number_Entries (Pid) > 1
then
Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
else
Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
end if;
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => Name,
Parameter_Associations => New_List (
Make_Attribute_Reference (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix => New_Reference_To (
Defining_Identifier (Param), Loc),
Selector_Name =>
Make_Identifier (Loc, Name_uObject)),
Attribute_Name => Name_Unchecked_Access))));
else
-- Unlock (_object._object'Access);
-- object is the record used to implement the protected object.
-- It is a parameter to the protected subprogram.
-- If the protected object is controlled (i.e it has entries or
-- needs finalization for interrupt handling), call
-- Unlock_Entries, except if the protected object follows the
-- ravenscar profile, in which case call Unlock_Entry, otherwise
-- call the simplified version, Unlock.
if Has_Entries (Pid)
or else Has_Interrupt_Handler (Pid)
or else (Has_Attach_Handler (Pid)
and then not Restricted_Profile)
then
if Abort_Allowed
or else Restriction_Active (No_Entry_Queue) = False
or else Number_Entries (Pid) > 1
then
Name := New_Reference_To (RTE (RE_Unlock_Entries), Loc);
else
Name := New_Reference_To (RTE (RE_Unlock_Entry), Loc);
end if;
else
Name := New_Reference_To (RTE (RE_Unlock), Loc);
end if;
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => Name,
Parameter_Associations => New_List (
Make_Attribute_Reference (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix =>
New_Reference_To (Defining_Identifier (Param), Loc),
Selector_Name =>
Make_Identifier (Loc, Name_uObject)),
Attribute_Name => Name_Unchecked_Access))));
end if;
if Abort_Allowed then
-- Abort_Undefer;
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Reference_To (
RTE (RE_Abort_Undefer), Loc),
Parameter_Associations => Empty_List));
end if;
elsif Is_Task_Allocation_Block then
-- Add a call to Expunge_Unactivated_Tasks to the cleanup
-- handler of a block created for the dynamic allocation of
-- tasks:
-- Expunge_Unactivated_Tasks (_chain);
-- where _chain is the list of tasks created by the allocator
-- but not yet activated. This list will be empty unless
-- the block completes abnormally.
-- This only applies to dynamically allocated tasks;
-- other unactivated tasks are completed by Complete_Task or
-- Complete_Master.
-- NOTE: This cleanup handler references _chain, a local
-- object.
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Reference_To (
RTE (RE_Expunge_Unactivated_Tasks), Loc),
Parameter_Associations => New_List (
New_Reference_To (Activation_Chain_Entity (N), Loc))));
elsif Is_Asynchronous_Call_Block then
-- Add a call to attempt to cancel the asynchronous entry call
-- whenever the block containing the abortable part is exited.
-- NOTE: This cleanup handler references C, a local object
-- Get the argument to the Cancel procedure
Cancel_Param := Entry_Cancel_Parameter (Entity (Identifier (N)));
-- If it is of type Communication_Block, this must be a
-- protected entry call.
if Is_RTE (Etype (Cancel_Param), RE_Communication_Block) then
Append_To (Stmt,
-- if Enqueued (Cancel_Parameter) then
Make_Implicit_If_Statement (Clean,
Condition => Make_Function_Call (Loc,
Name => New_Reference_To (
RTE (RE_Enqueued), Loc),
Parameter_Associations => New_List (
New_Reference_To (Cancel_Param, Loc))),
Then_Statements => New_List (
-- Cancel_Protected_Entry_Call (Cancel_Param);
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (
RTE (RE_Cancel_Protected_Entry_Call), Loc),
Parameter_Associations => New_List (
New_Reference_To (Cancel_Param, Loc))))));
-- Asynchronous delay
elsif Is_RTE (Etype (Cancel_Param), RE_Delay_Block) then
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Cancel_Async_Delay), Loc),
Parameter_Associations => New_List (
Make_Attribute_Reference (Loc,
Prefix => New_Reference_To (Cancel_Param, Loc),
Attribute_Name => Name_Unchecked_Access))));
-- Task entry call
else
-- Append call to Cancel_Task_Entry_Call (C);
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (
RTE (RE_Cancel_Task_Entry_Call),
Loc),
Parameter_Associations => New_List (
New_Reference_To (Cancel_Param, Loc))));
end if;
end if;
if Present (Flist) then
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Finalize_List), Loc),
Parameter_Associations => New_List (
New_Reference_To (Flist, Loc))));
end if;
if Present (Mark) then
Append_To (Stmt,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_SS_Release), Loc),
Parameter_Associations => New_List (
New_Reference_To (Mark, Loc))));
end if;
Sbody :=
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Clean),
Declarations => New_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmt));
if Present (Flist) or else Is_Task or else Is_Master then
Wrap_Cleanup_Procedure (Sbody);
end if;
-- We do not want debug information for _Clean routines,
-- since it just confuses the debugging operation unless
-- we are debugging generated code.
if not Debug_Generated_Code then
Set_Debug_Info_Off (Clean, True);
end if;
return Sbody;
end Make_Clean;
--------------------------
-- Make_Deep_Array_Body --
--------------------------
-- Array components are initialized and adjusted in the normal order
-- and finalized in the reverse order. Exceptions are handled and
-- Program_Error is re-raise in the Adjust and Finalize case
-- (RM 7.6.1(12)). Generate the following code :
--
-- procedure Deep_<P> -- with <P> being Initialize or Adjust or Finalize
-- (L : in out Finalizable_Ptr;
-- V : in out Typ)
-- is
-- begin
-- for J1 in Typ'First (1) .. Typ'Last (1) loop
-- ^ reverse ^ -- in the finalization case
-- ...
-- for J2 in Typ'First (n) .. Typ'Last (n) loop
-- Make_<P>_Call (Typ, V (J1, .. , Jn), L, V);
-- end loop;
-- ...
-- end loop;
-- exception -- not in the
-- when others => raise Program_Error; -- Initialize case
-- end Deep_<P>;
function Make_Deep_Array_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (Typ);
Index_List : constant List_Id := New_List;
-- Stores the list of references to the indexes (one per dimension)
function One_Component return List_Id;
-- Create one statement to initialize/adjust/finalize one array
-- component, designated by a full set of indices.
function One_Dimension (N : Int) return List_Id;
-- Create loop to deal with one dimension of the array. The single
-- statement in the body of the loop initializes the inner dimensions if
-- any, or else a single component.
-------------------
-- One_Component --
-------------------
function One_Component return List_Id is
Comp_Typ : constant Entity_Id := Component_Type (Typ);
Comp_Ref : constant Node_Id :=
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => Index_List);
begin
-- Set the etype of the component Reference, which is used to
-- determine whether a conversion to a parent type is needed.
Set_Etype (Comp_Ref, Comp_Typ);
case Prim is
when Initialize_Case =>
return Make_Init_Call (Comp_Ref, Comp_Typ,
Make_Identifier (Loc, Name_L),
Make_Identifier (Loc, Name_B));
when Adjust_Case =>
return Make_Adjust_Call (Comp_Ref, Comp_Typ,
Make_Identifier (Loc, Name_L),
Make_Identifier (Loc, Name_B));
when Finalize_Case =>
return Make_Final_Call (Comp_Ref, Comp_Typ,
Make_Identifier (Loc, Name_B));
end case;
end One_Component;
-------------------
-- One_Dimension --
-------------------
function One_Dimension (N : Int) return List_Id is
Index : Entity_Id;
begin
if N > Number_Dimensions (Typ) then
return One_Component;
else
Index :=
Make_Defining_Identifier (Loc, New_External_Name ('J', N));
Append_To (Index_List, New_Reference_To (Index, Loc));
return New_List (
Make_Implicit_Loop_Statement (Typ,
Identifier => Empty,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Index,
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, N))),
Reverse_Present => Prim = Finalize_Case)),
Statements => One_Dimension (N + 1)));
end if;
end One_Dimension;
-- Start of processing for Make_Deep_Array_Body
begin
return One_Dimension (1);
end Make_Deep_Array_Body;
--------------------
-- Make_Deep_Proc --
--------------------
-- Generate:
-- procedure DEEP_<prim>
-- (L : IN OUT Finalizable_Ptr; -- not for Finalize
-- V : IN OUT <typ>;
-- B : IN Short_Short_Integer) is
-- begin
-- <stmts>;
-- exception -- Finalize and Adjust Cases only
-- raise Program_Error; -- idem
-- end DEEP_<prim>;
function Make_Deep_Proc
(Prim : Final_Primitives;
Typ : Entity_Id;
Stmts : List_Id) return Entity_Id
is
Loc : constant Source_Ptr := Sloc (Typ);
Formals : List_Id;
Proc_Name : Entity_Id;
Handler : List_Id := No_List;
Type_B : Entity_Id;
begin
if Prim = Finalize_Case then
Formals := New_List;
Type_B := Standard_Boolean;
else
Formals := New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
In_Present => True,
Out_Present => True,
Parameter_Type =>
New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
Type_B := Standard_Short_Short_Integer;
end if;
Append_To (Formals,
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
In_Present => True,
Out_Present => True,
Parameter_Type => New_Reference_To (Typ, Loc)));
Append_To (Formals,
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
Parameter_Type => New_Reference_To (Type_B, Loc)));
if Prim = Finalize_Case or else Prim = Adjust_Case then
Handler := New_List (
Make_Exception_Handler (Loc,
Exception_Choices => New_List (Make_Others_Choice (Loc)),
Statements => New_List (
Make_Raise_Program_Error (Loc,
Reason => PE_Finalize_Raised_Exception))));
end if;
Proc_Name :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name (Typ, Deep_Name_Of (Prim)));
Discard_Node (
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Proc_Name,
Parameter_Specifications => Formals),
Declarations => Empty_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts,
Exception_Handlers => Handler)));
return Proc_Name;
end Make_Deep_Proc;
---------------------------
-- Make_Deep_Record_Body --
---------------------------
-- The Deep procedures call the appropriate Controlling proc on the
-- the controller component. In the init case, it also attach the
-- controller to the current finalization list.
function Make_Deep_Record_Body
(Prim : Final_Primitives;
Typ : Entity_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (Typ);
Controller_Typ : Entity_Id;
Obj_Ref : constant Node_Id := Make_Identifier (Loc, Name_V);
Controller_Ref : constant Node_Id :=
Make_Selected_Component (Loc,
Prefix => Obj_Ref,
Selector_Name =>
Make_Identifier (Loc, Name_uController));
Res : constant List_Id := New_List;
begin
if Is_Return_By_Reference_Type (Typ) then
Controller_Typ := RTE (RE_Limited_Record_Controller);
else
Controller_Typ := RTE (RE_Record_Controller);
end if;
case Prim is
when Initialize_Case =>
Append_List_To (Res,
Make_Init_Call (
Ref => Controller_Ref,
Typ => Controller_Typ,
Flist_Ref => Make_Identifier (Loc, Name_L),
With_Attach => Make_Identifier (Loc, Name_B)));
-- When the type is also a controlled type by itself,
-- Initialize it and attach it to the finalization chain
if Is_Controlled (Typ) then
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (
Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
Parameter_Associations =>
New_List (New_Copy_Tree (Obj_Ref))));
Append_To (Res, Make_Attach_Call (
Obj_Ref => New_Copy_Tree (Obj_Ref),
Flist_Ref => Make_Identifier (Loc, Name_L),
With_Attach => Make_Identifier (Loc, Name_B)));
end if;
when Adjust_Case =>
Append_List_To (Res,
Make_Adjust_Call (Controller_Ref, Controller_Typ,
Make_Identifier (Loc, Name_L),
Make_Identifier (Loc, Name_B)));
-- When the type is also a controlled type by itself,
-- Adjust it it and attach it to the finalization chain
if Is_Controlled (Typ) then
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (
Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
Parameter_Associations =>
New_List (New_Copy_Tree (Obj_Ref))));
Append_To (Res, Make_Attach_Call (
Obj_Ref => New_Copy_Tree (Obj_Ref),
Flist_Ref => Make_Identifier (Loc, Name_L),
With_Attach => Make_Identifier (Loc, Name_B)));
end if;
when Finalize_Case =>
if Is_Controlled (Typ) then
Append_To (Res,
Make_Implicit_If_Statement (Obj_Ref,
Condition => Make_Identifier (Loc, Name_B),
Then_Statements => New_List (
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
Parameter_Associations => New_List (
OK_Convert_To (RTE (RE_Finalizable),
New_Copy_Tree (Obj_Ref))))),
Else_Statements => New_List (
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (
Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
Parameter_Associations =>
New_List (New_Copy_Tree (Obj_Ref))))));
end if;
Append_List_To (Res,
Make_Final_Call (Controller_Ref, Controller_Typ,
Make_Identifier (Loc, Name_B)));
end case;
return Res;
end Make_Deep_Record_Body;
----------------------
-- Make_Final_Call --
----------------------
function Make_Final_Call
(Ref : Node_Id;
Typ : Entity_Id;
With_Detach : Node_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (Ref);
Res : constant List_Id := New_List;
Cref : Node_Id;
Cref2 : Node_Id;
Proc : Entity_Id;
Utyp : Entity_Id;
begin
if Is_Class_Wide_Type (Typ) then
Utyp := Root_Type (Typ);
Cref := Ref;
elsif Is_Concurrent_Type (Typ) then
Utyp := Corresponding_Record_Type (Typ);
Cref := Convert_Concurrent (Ref, Typ);
elsif Is_Private_Type (Typ)
and then Present (Full_View (Typ))
and then Is_Concurrent_Type (Full_View (Typ))
then
Utyp := Corresponding_Record_Type (Full_View (Typ));
Cref := Convert_Concurrent (Ref, Full_View (Typ));
else
Utyp := Typ;
Cref := Ref;
end if;
Utyp := Underlying_Type (Base_Type (Utyp));
Set_Assignment_OK (Cref);
-- Deal with non-tagged derivation of private views
if Is_Untagged_Derivation (Typ) then
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
Cref := Unchecked_Convert_To (Utyp, Cref);
Set_Assignment_OK (Cref);
-- To prevent problems with UC see 1.156 RH ???
end if;
-- If the underlying_type is a subtype, we are dealing with
-- the completion of a private type. We need to access
-- the base type and generate a conversion to it.
if Utyp /= Base_Type (Utyp) then
pragma Assert (Is_Private_Type (Typ));
Utyp := Base_Type (Utyp);
Cref := Unchecked_Convert_To (Utyp, Cref);
end if;
-- Generate:
-- Deep_Finalize (Ref, With_Detach);
if Has_Controlled_Component (Utyp)
or else Is_Class_Wide_Type (Typ)
then
if Is_Tagged_Type (Utyp) then
Proc := Find_Prim_Op (Utyp, TSS_Deep_Finalize);
else
Proc := TSS (Utyp, TSS_Deep_Finalize);
end if;
Cref := Convert_View (Proc, Cref);
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations =>
New_List (Cref, With_Detach)));
-- Generate:
-- if With_Detach then
-- Finalize_One (Ref);
-- else
-- Finalize (Ref);
-- end if;
else
Proc := Find_Prim_Op (Utyp, Name_Of (Finalize_Case));
if Chars (With_Detach) = Chars (Standard_True) then
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
Parameter_Associations => New_List (
OK_Convert_To (RTE (RE_Finalizable), Cref))));
elsif Chars (With_Detach) = Chars (Standard_False) then
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations =>
New_List (Convert_View (Proc, Cref))));
else
Cref2 := New_Copy_Tree (Cref);
Append_To (Res,
Make_Implicit_If_Statement (Ref,
Condition => With_Detach,
Then_Statements => New_List (
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
Parameter_Associations => New_List (
OK_Convert_To (RTE (RE_Finalizable), Cref)))),
Else_Statements => New_List (
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations =>
New_List (Convert_View (Proc, Cref2))))));
end if;
end if;
return Res;
end Make_Final_Call;
--------------------
-- Make_Init_Call --
--------------------
function Make_Init_Call
(Ref : Node_Id;
Typ : Entity_Id;
Flist_Ref : Node_Id;
With_Attach : Node_Id) return List_Id
is
Loc : constant Source_Ptr := Sloc (Ref);
Is_Conc : Boolean;
Res : constant List_Id := New_List;
Proc : Entity_Id;
Utyp : Entity_Id;
Cref : Node_Id;
Cref2 : Node_Id;
Attach : Node_Id := With_Attach;
begin
if Is_Concurrent_Type (Typ) then
Is_Conc := True;
Utyp := Corresponding_Record_Type (Typ);
Cref := Convert_Concurrent (Ref, Typ);
elsif Is_Private_Type (Typ)
and then Present (Full_View (Typ))
and then Is_Concurrent_Type (Underlying_Type (Typ))
then
Is_Conc := True;
Utyp := Corresponding_Record_Type (Underlying_Type (Typ));
Cref := Convert_Concurrent (Ref, Underlying_Type (Typ));
else
Is_Conc := False;
Utyp := Typ;
Cref := Ref;
end if;
Utyp := Underlying_Type (Base_Type (Utyp));
Set_Assignment_OK (Cref);
-- Deal with non-tagged derivation of private views
if Is_Untagged_Derivation (Typ)
and then not Is_Conc
then
Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
Cref := Unchecked_Convert_To (Utyp, Cref);
Set_Assignment_OK (Cref);
-- To prevent problems with UC see 1.156 RH ???
end if;
-- If the underlying_type is a subtype, we are dealing with
-- the completion of a private type. We need to access
-- the base type and generate a conversion to it.
if Utyp /= Base_Type (Utyp) then
pragma Assert (Is_Private_Type (Typ));
Utyp := Base_Type (Utyp);
Cref := Unchecked_Convert_To (Utyp, Cref);
end if;
-- We do not need to attach to one of the Global Final Lists
-- the objects whose type is Finalize_Storage_Only
if Finalize_Storage_Only (Typ)
and then (Global_Flist_Ref (Flist_Ref)
or else Entity (Constant_Value (RTE (RE_Garbage_Collected)))
= Standard_True)
then
Attach := Make_Integer_Literal (Loc, 0);
end if;
-- Generate:
-- Deep_Initialize (Ref, Flist_Ref);
if Has_Controlled_Component (Utyp) then
Proc := TSS (Utyp, Deep_Name_Of (Initialize_Case));
Cref := Convert_View (Proc, Cref, 2);
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations => New_List (
Node1 => Flist_Ref,
Node2 => Cref,
Node3 => Attach)));
-- Generate:
-- Attach_To_Final_List (Ref, Flist_Ref);
-- Initialize (Ref);
else -- Is_Controlled (Utyp)
Proc := Find_Prim_Op (Utyp, Name_Of (Initialize_Case));
Check_Visibly_Controlled (Initialize_Case, Typ, Proc, Cref);
Cref := Convert_View (Proc, Cref);
Cref2 := New_Copy_Tree (Cref);
Append_To (Res,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations => New_List (Cref2)));
Append_To (Res,
Make_Attach_Call (Cref, Flist_Ref, Attach));
end if;
return Res;
end Make_Init_Call;
--------------------------
-- Make_Transient_Block --
--------------------------
-- If finalization is involved, this function just wraps the instruction
-- into a block whose name is the transient block entity, and then
-- Expand_Cleanup_Actions (called on the expansion of the handled
-- sequence of statements will do the necessary expansions for
-- cleanups).
function Make_Transient_Block
(Loc : Source_Ptr;
Action : Node_Id) return Node_Id
is
Flist : constant Entity_Id := Finalization_Chain_Entity (Current_Scope);
Decls : constant List_Id := New_List;
Par : constant Node_Id := Parent (Action);
Instrs : constant List_Id := New_List (Action);
Blk : Node_Id;
begin
-- Case where only secondary stack use is involved
if Uses_Sec_Stack (Current_Scope)
and then No (Flist)
and then Nkind (Action) /= N_Return_Statement
and then Nkind (Par) /= N_Exception_Handler
then
declare
S : Entity_Id;
K : Entity_Kind;
begin
S := Scope (Current_Scope);
loop
K := Ekind (S);
-- At the outer level, no need to release the sec stack
if S = Standard_Standard then
Set_Uses_Sec_Stack (Current_Scope, False);
exit;
-- In a function, only release the sec stack if the
-- function does not return on the sec stack otherwise
-- the result may be lost. The caller is responsible for
-- releasing.
elsif K = E_Function then
Set_Uses_Sec_Stack (Current_Scope, False);
if not Requires_Transient_Scope (Etype (S)) then
if not Functions_Return_By_DSP_On_Target then
Set_Uses_Sec_Stack (S, True);
Check_Restriction (No_Secondary_Stack, Action);
end if;
end if;
exit;
-- In a loop or entry we should install a block encompassing
-- all the construct. For now just release right away.
elsif K = E_Loop or else K = E_Entry then
exit;
-- In a procedure or a block, we release on exit of the
-- procedure or block. ??? memory leak can be created by
-- recursive calls.
elsif K = E_Procedure
or else K = E_Block
then
if not Functions_Return_By_DSP_On_Target then
Set_Uses_Sec_Stack (S, True);
Check_Restriction (No_Secondary_Stack, Action);
end if;
Set_Uses_Sec_Stack (Current_Scope, False);
exit;
else
S := Scope (S);
end if;
end loop;
end;
end if;
-- Insert actions stuck in the transient scopes as well as all
-- freezing nodes needed by those actions
Insert_Actions_In_Scope_Around (Action);
declare
Last_Inserted : Node_Id := Prev (Action);
begin
if Present (Last_Inserted) then
Freeze_All (First_Entity (Current_Scope), Last_Inserted);
end if;
end;
Blk :=
Make_Block_Statement (Loc,
Identifier => New_Reference_To (Current_Scope, Loc),
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Statements => Instrs),
Has_Created_Identifier => True);
-- When the transient scope was established, we pushed the entry for
-- the transient scope onto the scope stack, so that the scope was
-- active for the installation of finalizable entities etc. Now we
-- must remove this entry, since we have constructed a proper block.
Pop_Scope;
return Blk;
end Make_Transient_Block;
------------------------
-- Node_To_Be_Wrapped --
------------------------
function Node_To_Be_Wrapped return Node_Id is
begin
return Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped;
end Node_To_Be_Wrapped;
----------------------------
-- Set_Node_To_Be_Wrapped --
----------------------------
procedure Set_Node_To_Be_Wrapped (N : Node_Id) is
begin
Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped := N;
end Set_Node_To_Be_Wrapped;
----------------------------------
-- Store_After_Actions_In_Scope --
----------------------------------
procedure Store_After_Actions_In_Scope (L : List_Id) is
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
begin
if Present (SE.Actions_To_Be_Wrapped_After) then
Insert_List_Before_And_Analyze (
First (SE.Actions_To_Be_Wrapped_After), L);
else
SE.Actions_To_Be_Wrapped_After := L;
if Is_List_Member (SE.Node_To_Be_Wrapped) then
Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
else
Set_Parent (L, SE.Node_To_Be_Wrapped);
end if;
Analyze_List (L);
end if;
end Store_After_Actions_In_Scope;
-----------------------------------
-- Store_Before_Actions_In_Scope --
-----------------------------------
procedure Store_Before_Actions_In_Scope (L : List_Id) is
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
begin
if Present (SE.Actions_To_Be_Wrapped_Before) then
Insert_List_After_And_Analyze (
Last (SE.Actions_To_Be_Wrapped_Before), L);
else
SE.Actions_To_Be_Wrapped_Before := L;
if Is_List_Member (SE.Node_To_Be_Wrapped) then
Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
else
Set_Parent (L, SE.Node_To_Be_Wrapped);
end if;
Analyze_List (L);
end if;
end Store_Before_Actions_In_Scope;
--------------------------------
-- Wrap_Transient_Declaration --
--------------------------------
-- If a transient scope has been established during the processing of the
-- Expression of an Object_Declaration, it is not possible to wrap the
-- declaration into a transient block as usual case, otherwise the object
-- would be itself declared in the wrong scope. Therefore, all entities (if
-- any) defined in the transient block are moved to the proper enclosing
-- scope, furthermore, if they are controlled variables they are finalized
-- right after the declaration. The finalization list of the transient
-- scope is defined as a renaming of the enclosing one so during their
-- initialization they will be attached to the proper finalization
-- list. For instance, the following declaration :
-- X : Typ := F (G (A), G (B));
-- (where G(A) and G(B) return controlled values, expanded as _v1 and _v2)
-- is expanded into :
-- _local_final_list_1 : Finalizable_Ptr;
-- X : Typ := [ complex Expression-Action ];
-- Finalize_One(_v1);
-- Finalize_One (_v2);
procedure Wrap_Transient_Declaration (N : Node_Id) is
S : Entity_Id;
LC : Entity_Id := Empty;
Nodes : List_Id;
Loc : constant Source_Ptr := Sloc (N);
Enclosing_S : Entity_Id;
Uses_SS : Boolean;
Next_N : constant Node_Id := Next (N);
begin
S := Current_Scope;
Enclosing_S := Scope (S);
-- Insert Actions kept in the Scope stack
Insert_Actions_In_Scope_Around (N);
-- If the declaration is consuming some secondary stack, mark the
-- Enclosing scope appropriately.
Uses_SS := Uses_Sec_Stack (S);
Pop_Scope;
-- Create a List controller and rename the final list to be its
-- internal final pointer:
-- Lxxx : Simple_List_Controller;
-- Fxxx : Finalizable_Ptr renames Lxxx.F;
if Present (Finalization_Chain_Entity (S)) then
LC := Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
Nodes := New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => LC,
Object_Definition =>
New_Reference_To (RTE (RE_Simple_List_Controller), Loc)),
Make_Object_Renaming_Declaration (Loc,
Defining_Identifier => Finalization_Chain_Entity (S),
Subtype_Mark => New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
Name =>
Make_Selected_Component (Loc,
Prefix => New_Reference_To (LC, Loc),
Selector_Name => Make_Identifier (Loc, Name_F))));
-- Put the declaration at the beginning of the declaration part
-- to make sure it will be before all other actions that have been
-- inserted before N.
Insert_List_Before_And_Analyze (First (List_Containing (N)), Nodes);
-- Generate the Finalization calls by finalizing the list
-- controller right away. It will be re-finalized on scope
-- exit but it doesn't matter. It cannot be done when the
-- call initializes a renaming object though because in this
-- case, the object becomes a pointer to the temporary and thus
-- increases its life span.
if Nkind (N) = N_Object_Renaming_Declaration
and then Controlled_Type (Etype (Defining_Identifier (N)))
then
null;
else
Nodes :=
Make_Final_Call (
Ref => New_Reference_To (LC, Loc),
Typ => Etype (LC),
With_Detach => New_Reference_To (Standard_False, Loc));
if Present (Next_N) then
Insert_List_Before_And_Analyze (Next_N, Nodes);
else
Append_List_To (List_Containing (N), Nodes);
end if;
end if;
end if;
-- Put the local entities back in the enclosing scope, and set the
-- Is_Public flag appropriately.
Transfer_Entities (S, Enclosing_S);
-- Mark the enclosing dynamic scope so that the sec stack will be
-- released upon its exit unless this is a function that returns on
-- the sec stack in which case this will be done by the caller.
if Uses_SS then
S := Enclosing_Dynamic_Scope (S);
if Ekind (S) = E_Function
and then Requires_Transient_Scope (Etype (S))
then
null;
else
Set_Uses_Sec_Stack (S);
Check_Restriction (No_Secondary_Stack, N);
end if;
end if;
end Wrap_Transient_Declaration;
-------------------------------
-- Wrap_Transient_Expression --
-------------------------------
-- Insert actions before <Expression>:
-- (lines marked with <CTRL> are expanded only in presence of Controlled
-- objects needing finalization)
-- _E : Etyp;
-- declare
-- _M : constant Mark_Id := SS_Mark;
-- Local_Final_List : System.FI.Finalizable_Ptr; <CTRL>
-- procedure _Clean is
-- begin
-- Abort_Defer;
-- System.FI.Finalize_List (Local_Final_List); <CTRL>
-- SS_Release (M);
-- Abort_Undefer;
-- end _Clean;
-- begin
-- _E := <Expression>;
-- at end
-- _Clean;
-- end;
-- then expression is replaced by _E
procedure Wrap_Transient_Expression (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
E : constant Entity_Id :=
Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
Etyp : constant Entity_Id := Etype (N);
begin
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => E,
Object_Definition => New_Reference_To (Etyp, Loc)),
Make_Transient_Block (Loc,
Action =>
Make_Assignment_Statement (Loc,
Name => New_Reference_To (E, Loc),
Expression => Relocate_Node (N)))));
Rewrite (N, New_Reference_To (E, Loc));
Analyze_And_Resolve (N, Etyp);
end Wrap_Transient_Expression;
------------------------------
-- Wrap_Transient_Statement --
------------------------------
-- Transform <Instruction> into
-- (lines marked with <CTRL> are expanded only in presence of Controlled
-- objects needing finalization)
-- declare
-- _M : Mark_Id := SS_Mark;
-- Local_Final_List : System.FI.Finalizable_Ptr ; <CTRL>
-- procedure _Clean is
-- begin
-- Abort_Defer;
-- System.FI.Finalize_List (Local_Final_List); <CTRL>
-- SS_Release (_M);
-- Abort_Undefer;
-- end _Clean;
-- begin
-- <Instr uction>;
-- at end
-- _Clean;
-- end;
procedure Wrap_Transient_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
New_Statement : constant Node_Id := Relocate_Node (N);
begin
Rewrite (N, Make_Transient_Block (Loc, New_Statement));
-- With the scope stack back to normal, we can call analyze on the
-- resulting block. At this point, the transient scope is being
-- treated like a perfectly normal scope, so there is nothing
-- special about it.
-- Note: Wrap_Transient_Statement is called with the node already
-- analyzed (i.e. Analyzed (N) is True). This is important, since
-- otherwise we would get a recursive processing of the node when
-- we do this Analyze call.
Analyze (N);
end Wrap_Transient_Statement;
end Exp_Ch7;
|