File: sem_dist.adb

package info (click to toggle)
gnat 3.10p-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 49,492 kB
  • ctags: 33,976
  • sloc: ansic: 347,844; ada: 227,415; sh: 8,759; yacc: 7,861; asm: 5,252; makefile: 3,632; objc: 475; cpp: 400; sed: 261; pascal: 95
file content (2788 lines) | stat: -rw-r--r-- 92,614 bytes parent folder | download
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
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             S E M _ D I S T                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                            $Revision: 1.144 $                            --
--                                                                          --
--          Copyright (C) 1992-1997, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
--                                                                          --
------------------------------------------------------------------------------

with Atree;    use Atree;
with Casing;   use Casing;
with Einfo;    use Einfo;
with Errout;   use Errout;
with Elists;   use Elists;
with Exp_Dist; use Exp_Dist;
with Exp_TSS;  use Exp_TSS;
with Fname;    use Fname;
with Lib;      use Lib;
with Lib.Load; use Lib.Load;
with Nlists;   use Nlists;
with Nmake;    use Nmake;
with Namet;    use Namet;
with Osint;    use Osint;
with Opt;      use Opt;
with Output;   use Output;
with Rtsfind;  use Rtsfind;
with Sem;      use Sem;
with Sem_Ch7;  use Sem_Ch7;
with Sem_Prag; use Sem_Prag;
with Sem_Res;  use Sem_Res;
with Sem_Util; use Sem_Util;
with Sinfo;    use Sinfo;
with Snames;   use Snames;
with Sprint;   use Sprint;
with Stand;    use Stand;
with Stringt;  use Stringt;
with Tbuild;   use Tbuild;
with Uintp;    use Uintp;
with Uname;    use Uname;

package body Sem_Dist is

   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Add_Unchecked_Conversion (C_Unit : Node_Id);
   --  Load, analyze and add the package Unchecked_Conversion
   --  to the context clauses of the enclosing library unit

   procedure Check_Categorization_Dependencies
     (Unit_Entity     : Entity_Id;
      Depended_Entity : Entity_Id;
      Info_Node       : Node_Id);
   --  This procedure checks that the categorization of a lib unit and that
   --  of the depended unit satisfy dependency restrictions.
   --  What is the info-Node param, need more documentation ???

   procedure Check_Non_Static_Default_Expr (Type_Def : Node_Id);
   --  Iterate through the component list of a record definition, check
   --  that no component is declared with a non-static default value.

   procedure RAS_E_Dereference (Pref : Node_Id);
   --  Handles explicit dereference of Remote Access to Subprograms.

   function Build_Parent_Full_Name (P : Node_Id)  return Node_Id;
   --  Build prefix of child unit name. Recurse if needed.

   function Build_Unit_Full_Name (U : Node_Id) return Node_Id;
   --  If the unit is a child unit, build name with all ancestors. otherwise,
   --  returns a new reference to the unit name.

   function Full_Qualified_Name (E : Entity_Id) return String_Id;
   --  returns the full qualified name of the entity in lower case.

   function Get_Name_Id (Name : String) return Name_Id;
   --  Given a string, return the Name_Id that represent the string

   function Has_Pragma_All_Calls_Remote (L : List_Id) return Boolean;
   --  Return true if L contains a pragma All_Calls_Remote node.

   function Static_Discriminant_Expr (L : List_Id) return Boolean;
   --  Iterate through the list of discriminants to check if any of them
   --  contains non-static default expression, which is a violation in
   --  a preelaborated library unit.

   procedure Rewrite_Or_Replace (N : Node_Id; New_Node : Node_Id);
   --  In a number of situations, we rewrite a node and if we are in stub
   --  generation mode, we want the stub output to include this rewriting.
   --  Since stub generation uses the original tree, this means we need
   --  to do a Replace if we are generating stubs, but if we are not
   --  generating stubs, then semantic accuracy of the original tree
   --  properly preserved by using Rewrite.

   ------------------------------
   -- Add_Unchecked_Conversion --
   ------------------------------

   procedure Add_Unchecked_Conversion (C_Unit : Node_Id) is
      Contexts : List_Id := Context_Items (C_Unit);
      Lib_Unit : Node_Id;
      Withn    : Node_Id;
      Uname    : Unit_Name_Type;
      Unum     : Unit_Number_Type;
      UEntity  : Entity_Id;
      Withed   : Boolean := False;
      Context  : Node_Id;

      procedure Failure (S : String);
      --  Internal procedure called if an error occurs. The parameter
      --  is a detailed error message that is to be given

      procedure Failure (S : String) is
      begin
         Set_Standard_Error;

         Write_Str ("fatal error: runtime library configuration error");
         Write_Eol;
         Write_Char ('"');
         Write_Name (Get_File_Name (Uname));
         Write_Str (""" (");
         Write_Str (S);
         Write_Char (')');
         Write_Eol;
         Set_Standard_Output;
         raise Unrecoverable_Error;
      end Failure;

   --  Start of processing for Add_Unchecked_Conversion

   begin
      Name_Buffer (1 .. 22) := "unchecked_conversion%s";
      Name_Len := 22;
      Uname := Name_Find;
      Unum := Load_Unit (Uname, False, Empty);

      if Unum = No_Unit then
         Failure ("unit not found");
      elsif Fatal_Error (Unum) then
         Failure ("parser errors");
      end if;

      --  Make sure that the unit is analyzed

      if not Analyzed (Cunit (Unum)) then
         Semantics (Cunit (Unum));

         if Fatal_Error (Unum) then
            Failure ("semantic errors");
         end if;
      end if;

      Lib_Unit := Unit (Cunit (Unum));

      UEntity := Defining_Entity (Lib_Unit);

      --  Add to the context clause

      if Present (Contexts) then
         Context := First (Contexts);
         while Present (Context) and not Withed loop
            Withed :=
              Nkind (Context) = N_With_Clause
                and then
              Defining_Entity (Unit (Library_Unit (Context))) = UEntity;
            Context := Next (Context);
         end loop;
      end if;

      if not Withed then
         Withn :=
           Make_With_Clause (Standard_Location,
             Name => New_Reference_To (UEntity, Standard_Location));
         Set_Library_Unit       (Withn, Cunit (Unum));
         Set_Corresponding_Spec (Withn, UEntity);
         Set_First_Name         (Withn, True);
         Set_Implicit_With      (Withn, True);
         Mark_Rewrite_Insertion (Withn);
         Prepend (Withn, Contexts);
      end if;
   end Add_Unchecked_Conversion;

   ----------------------
   -- Append_System_PI --
   ----------------------

   procedure Append_System_PI (N : Node_Id; L : Node_Id) is
      Loc : constant Source_Ptr := Sloc (N);

      procedure Append_Items (Items : List_Id);
      --  Given Items, a list of visible declarations or following pragmas
      --  of L, append System.Partition_Interface to the context items
      --  of N if any of there is Remote_Call_Interface or Remote_Types
      --  pragma in the list.

      ------------------
      -- Append_Items --
      ------------------

      procedure Append_Items (Items : List_Id) is
         Nd : Node_Id;

      begin
         if Present (Items) then
            Nd := First (Items);
            while Present (Nd) loop

               --  Search ends when non-pragma is met since they appear first

               exit when Nkind (Nd) /= N_Pragma;

               if Chars (Nd) = Name_Remote_Call_Interface
                 or else Chars (Nd) = Name_Remote_Types
               then
                  Append_To (Context_Items (N),
                    Make_With_Clause (Loc,
                      Make_Selected_Component (Loc,
                        Prefix        =>
                          Make_Identifier (Loc, Name_System),
                        Selector_Name =>
                          Make_Identifier (Loc, Name_Partition_Interface))));
               end if;

               Nd := Next (Nd);
            end loop;
         end if;
      end Append_Items;

   --  Start processing of Append_System_PI

   begin
      if Nkind (Unit (L)) = N_Package_Declaration then
         Append_Items (Visible_Declarations (Specification (Unit (L))));
         Append_Items (Pragmas_After (Aux_Decls_Node (L)));
      end if;
   end Append_System_PI;

   -----------------------
   -- Append_System_RPC --
   -----------------------

   procedure Append_System_RPC (N : Node_Id) is
      Decls  : constant List_Id := Visible_Declarations
                                     (Specification (Unit (N)));

      S      : constant Source_Ptr := Sloc (N);
      Items  : List_Id := Context_Items (N);
      F      : List_Id := Pragmas_After (Aux_Decls_Node (N));
      Decl   : Node_Id;
      Item   : Node_Id;

      procedure Append_Items (N : in out Node_Id);
      --  Given N, first node in a list (visible declarations or following
      --  pragmas) append to the list context items "with System.Rpc" if
      --  unit is either RCI or remote types.

      ------------------
      -- Append_Items --
      ------------------

      procedure Append_Items (N : in out Node_Id) is
      begin
         while Present (N) and then Nkind (N) = N_Pragma loop

            if Chars (N) = Name_Remote_Call_Interface
              or else Chars (N) = Name_Remote_Types
            then
               Item := Make_With_Clause (S,
                 Make_Selected_Component (S,
                   Prefix        => Make_Identifier (S, Name_System),
                   Selector_Name => Make_Identifier (S, Name_Rpc)));

               if Present (Items) then
                  Append (Item, Items);
               else
                  Items := New_List (Item);
               end if;
            end if;

            N := Next (N);
         end loop;
      end Append_Items;

   --  Start processing of Append_System_Rpc

   begin
      if not Present (Decls) then
         return;
      end if;

      Decl := First (Decls);
      Append_Items (Decl);

      if not Present (F) then
         return;
      end if;

      Decl := First (F);
      Append_Items (Decl);
   end Append_System_RPC;

   ----------------------------
   -- Build_Parent_Full_Name --
   ----------------------------

   function Build_Parent_Full_Name (P : Node_Id) return Node_Id is
      Loc   : Source_Ptr := Sloc (P);
      P_Ref : Node_Id := New_Reference_To (Defining_Entity (P), Loc);

   begin
      if No (Parent_Spec (P)) then
         return P_Ref;
      else
         return
           Make_Selected_Component (Loc,
             Prefix => Build_Parent_Full_Name (Unit (Parent_Spec (P))),
             Selector_Name => P_Ref);
      end if;
   end Build_Parent_Full_Name;


   --------------------------
   -- Build_Unit_Full_Name --
   --------------------------

   function Build_Unit_Full_Name (U : Node_Id) return Node_Id is
      Loc    : Source_Ptr := Sloc (U);
      U_Name : Entity_Id  := Defining_Entity (U);
      Result : Node_Id;

   begin
      if No (Parent_Spec (U)) then
         return New_Reference_To (U_Name, Loc);

      else
         Result :=
           Make_Expanded_Name (Loc,
             Chars => Chars (U_Name),
             Prefix => Build_Parent_Full_Name (Unit (Parent_Spec (U))),
             Selector_Name => New_Reference_To (U_Name, Loc));

         Set_Entity (Result, U_Name);
         Set_Etype (Result, Etype (U_Name));
         return Result;
      end if;
   end Build_Unit_Full_Name;

   ---------------------------------------
   -- Check_Categorization_Dependencies --
   ---------------------------------------

   procedure Check_Categorization_Dependencies
     (Unit_Entity     : Entity_Id;
      Depended_Entity : Entity_Id;
      Info_Node       : Node_Id)
   is
      N                  : Node_Id := Info_Node;
      Depended_Unit_Node : Node_Id;

      type Categorization is
         (Pure, Shared_Passive, Remote_Types,
           Remote_Call_Interface, Pre_Elaborated, Normal);

      Unit_Category : Categorization;
      With_Category : Categorization;

      function Get_Categorization (E : Entity_Id) return Categorization;
      --  To simplify checking code below.

      function Get_Categorization (E : Entity_Id) return Categorization is
      begin
         if Is_Preelaborated (E) then
            return Pre_Elaborated;
         elsif Is_Pure (E) then
            return Pure;
         elsif Is_Shared_Passive (E) then
            return Shared_Passive;
         elsif Is_Remote_Types (E) then
            return Remote_Types;
         elsif Is_Remote_Call_Interface (E) then
            return Remote_Call_Interface;
         else
            return Normal;
         end if;
      end Get_Categorization;

   begin
      if Nkind (Info_Node) = N_With_Clause then

         --  Compilation unit node of withed unit.

         Depended_Unit_Node := Library_Unit (Info_Node);

      else
         --  Parent spec compilation unit node.

         Depended_Unit_Node := Info_Node;
      end if;

      --  KLUDGE ALERT !!!

      --  Right now, there is junk code in sem_prag that deliberately
      --  does not set the Is_Preelaborated flag for generic units,
      --  but that's wrong, since it means that we have trouble with
      --  this check with we "with" a preelaborated unit. Probably there
      --  is similar junk for the other cases.

      --  The reason for the junk code was to stop problems at the
      --  instantiation level, but this should be cured in other ways
      --  (e.g. not copying these flags to the instantiation).

      --  For now, we just disconnect the dependency checks for generic units

      if Ekind (Depended_Entity) = E_Generic_Procedure
           or else
         Ekind (Depended_Entity) = E_Generic_Function
           or else
         Ekind (Depended_Entity) = E_Generic_Package
      then
         return;
      end if;

      Unit_Category := Get_Categorization (Unit_Entity);
      With_Category := Get_Categorization (Depended_Entity);

      if With_Category > Unit_Category then

         --  System.Partition_Interface and System.Rpc are with'ed in
         --  processing remote access to subprogram type by RCI and remote
         --  types units to generate fat pointer type. Since they are not
         --  categorized (not an error, by the way), we will get a dependency
         --  violation if we don't skip checking at this point.

         if Unit_Category = Remote_Types
           and then Chars (Depended_Entity) = Name_Rpc
           and then Present (Scope (Depended_Entity))
           and then Chars (Scope (Depended_Entity)) = Name_System
         then
            null;

         elsif Unit_Category = Remote_Call_Interface
           and then (Chars (Depended_Entity) = Name_Rpc
                      and then Present (Scope (Depended_Entity))
           and then Chars (Scope (Depended_Entity)) = Name_System)
         then
            null;

         elsif Unit_Category = Remote_Call_Interface
           and then Chars (Depended_Entity) = Name_Partition_Interface
         then
            null;

         else
            Error_Msg_NE ("current unit cannot depend on&"
              & " (wrong categorization)", N, Depended_Entity);
         end if;
      end if;

   end Check_Categorization_Dependencies;

   -----------------------------------
   -- Check_Non_Static_Default_Expr --
   -----------------------------------

   procedure Check_Non_Static_Default_Expr (Type_Def : Node_Id) is
      Component_Decl : Node_Id;

   begin
      --  Check that component declarations do not involve:

      --    a. a non-static default expression, where the object is
      --       declared to be default initialized.

      --    b. a dynamic Itype (discriminants and constraints)

      if Null_Present (Type_Def) then
         return;
      else
         Component_Decl := First (Component_Items (Component_List (Type_Def)));
      end if;

      while Present (Component_Decl)
        and then Nkind (Component_Decl) = N_Component_Declaration
      loop
         if Present (Expression (Component_Decl))
           and then Nkind (Expression (Component_Decl)) /= N_Null
           and then not Is_Static_Expression (Expression (Component_Decl))
         then
            Error_Msg_N
              ("non-static expression in declaration in preelaborated unit",
               Component_Decl);

         --  Fix this later!

         --  elsif Has_Dynamic_Itype (Component_Decl) then
         --     Error_Msg_N
         --       ("dynamic type discriminant," &
         --        " constraint in preelaborated unit",
         --        Component_Decl);
         end if;

         Component_Decl := Next (Component_Decl);
      end loop;
   end Check_Non_Static_Default_Expr;

   --------------------------------------
   -- CW_Remote_Extension_Add_Receiver --
   --------------------------------------

   procedure CW_Remote_Extension_Add_Receiver (N : Node_Id) is
      PN : constant Node_Id := Parent (N);
      LU : Node_Id;
      PD : Node_Id;
      SP : Node_Id;
      BL : List_Id;
      LN : Node_Id;

      procedure Add_Receiver (L : List_Id);
      --  In case there is a classwide type remote extension (check spec
      --  for definition) on the list, append a receiver for such type
      --  (extension)

      procedure Add_Receiver (L : List_Id) is
         Decl : Node_Id;

      begin
         if not Present (L) then
            return;
         end if;

         Decl := First (L);

         while Present (Decl) loop

            if Is_Class_Wide_Type_Remote_Extension (Decl) then

               if not Is_Remote_Call_Interface (Defining_Identifier
                 (Decl))
               then

                  --  Add to BL (package body declaration list) the
                  --  receiver subprogram for the type (extension)

                  null; --  ??? To be updated soon
               end if;

            end if;

            Decl := Next (Decl);
         end loop;
      end Add_Receiver;

   --  Start of processing CW_Remote_Extension_Add_Receiver

   begin
      if Nkind (PN) /= N_Compilation_Unit then
         return;
      end if;

      LU := Library_Unit (PN);

      if not Present (LU) then
         return;
      end if;

      PD := Unit (LU);

      if Nkind (PD) /= N_Package_Declaration then
         return;
      end if;

      SP := Specification (PD);
      BL := Declarations (N);

      LN := Last (BL);
      Add_Receiver (Visible_Declarations (SP));
      Add_Receiver (Private_Declarations (SP));
      Add_Receiver (BL);

   end CW_Remote_Extension_Add_Receiver;

   -------------------------------
   -- Enclosing_Lib_Unit_Entity --
   -------------------------------

   function Enclosing_Lib_Unit_Entity return Entity_Id is
      Unit_Entity : Entity_Id := Current_Scope;

   begin
      --  Look for enclosing library unit entity by following scope links.
      --  Equivalent to, but faster than indexing through the scope stack.

      while (Present (Scope (Unit_Entity))
        and then Scope (Unit_Entity) /= Standard_Standard)
        and not Is_Child_Unit (Unit_Entity)
      loop
         Unit_Entity := Scope (Unit_Entity);
      end loop;

      return Unit_Entity;
   end Enclosing_Lib_Unit_Entity;

   -----------------------------
   -- Enclosing_Lib_Unit_Node --
   -----------------------------

   function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
      Current_Node : Node_Id := N;

   begin
      while Present (Current_Node)
        and then Nkind (Current_Node) /= N_Compilation_Unit
      loop
         Current_Node := Parent (Current_Node);
      end loop;

      if Nkind (Current_Node) /= N_Compilation_Unit then
         return Empty;
      end if;

      return Current_Node;
   end Enclosing_Lib_Unit_Node;

   -------------------------
   -- Full_Qualified_Name --
   -------------------------

   function Full_Qualified_Name (E : Entity_Id) return String_Id is
      Ent         : Entity_Id := E;
      Parent_Name : String_Id := No_String;

   begin
      --  Deals properly with child units

      if Nkind (Ent) = N_Defining_Program_Unit_Name then
         Ent := Defining_Identifier (Ent);
      end if;

      --  Compute recursively the qualification. Only "Standard" has no scope.

      if Present (Scope (Scope (Ent))) then
         Parent_Name := Full_Qualified_Name (Scope (Ent));
      end if;

      --  Every entity should have a name except some expanded blocks
      --  don't bother about those.

      if Chars (Ent) = No_Name then
         return Parent_Name;
      end if;

      --  Add a period between Name and qualification

      if Parent_Name /= No_String then
         Start_String (Parent_Name);
         Store_String_Char (Get_Char_Code ('.'));

      else
         Start_String;
      end if;

      --  Generates the entity name in upper case

      Get_Name_String (Chars (Ent));
      Set_Casing (All_Lower_Case);
      Store_String_Chars (Name_Buffer (1 .. Name_Len));
      return End_String;
   end Full_Qualified_Name;

   --------------------------
   -- Generate_Stubs_Files --
   --------------------------

   procedure Generate_Stubs_Files (N : Node_Id) is
      Unit_Node : Node_Id := Unit (N);
      Nd        : Node_Id;

      procedure Output_Stubs_File (Stubs_Node : Node_Id);
      --  Create the source file for a stubs node

      procedure Output_Stubs_File (Stubs_Node : Node_Id) is
      begin
         Stub_Output_Start;
         Sprint_Node_Pure_Ada (Stubs_Node);
         Write_Eol;
         Stub_Output_Stop;
      end Output_Stubs_File;

   --  Start of processing for Generate_Stubs_Files

   begin
      if Distribution_Stub_Mode = Generate_Caller_Stub_Body then
         if Nkind (Unit_Node) = N_Package_Declaration then
            if Is_Shared_Passive
              (Defining_Entity (Specification (Unit_Node)))
            then
               Nd := Build_Passive_Partition_Stub (N, Is_Client => True);
            else
               Init_Names;
               Nd := Build_Calling_Stubs_Bodies_Cunit (N);
            end if;

            Output_Stubs_File (Nd);

         else
            Error_Msg_N ("Specification file expected from command line",
              Unit_Node);
         end if;

      elsif Distribution_Stub_Mode = Generate_Receiver_Stub_Body then
         if Nkind (Unit_Node) = N_Package_Declaration
           and then Is_Shared_Passive
             (Defining_Entity (Specification (Unit_Node)))
         then
            Nd := Build_Passive_Partition_Stub (N, Is_Client => False);
         else
            Init_Names;
            Nd := Build_Receiving_Stubs_Bodies_Cunit (N);
         end if;

         Output_Stubs_File (Nd);
      end if;

   end Generate_Stubs_Files;

   -----------------
   -- Get_Name_Id --
   -----------------

   function Get_Name_Id (Name : String) return Name_Id is
   begin
      Name_Len := Name'Length;
      Name_Buffer (1 ..  Name_Len) := Name;
      return Name_Find;
   end Get_Name_Id;

   ---------------------------------
   -- Has_Pragma_All_Calls_Remote --
   ---------------------------------

   function Has_Pragma_All_Calls_Remote (L : List_Id) return Boolean is
      Decl : Node_Id;

   begin
      if Present (L) then
         Decl := First (L);
         while Present (Decl)
           and then (Nkind (Decl) /= N_Pragma
                      or else Chars (Decl) /= Name_All_Calls_Remote)
         loop
            Decl := Next (Decl);
         end loop;

         if Present (Decl) then
            return True;
         end if;
      end if;

      return False;
   end Has_Pragma_All_Calls_Remote;

   ---------------------------
   -- In_Preelaborated_Unit --
   ---------------------------

   function In_Preelaborated_Unit return Boolean is
      Unit_Entity : constant Entity_Id := Current_Scope;
      Unit_Kind   : constant Node_Kind :=
                      Nkind (Unit (Cunit (Current_Sem_Unit)));

   begin
      --  There are no constraints on body of remote_call_interface or
      --  remote_types packages..

      return (Unit_Entity /= Standard_Standard)
        and then (Is_Preelaborated (Unit_Entity)
                    or else Is_Pure (Unit_Entity)
                    or else Is_Shared_Passive (Unit_Entity)
                    or else
                      ((Is_Remote_Types (Unit_Entity)
                               or else Is_Remote_Call_Interface (Unit_Entity))
                         and then Ekind (Unit_Entity) = E_Package
                         and then Unit_Kind /= N_Package_Body
                         and then not In_Package_Body (Unit_Entity)
                         and then not In_Instance));
   end In_Preelaborated_Unit;

   ------------------
   -- In_Pure_Unit --
   ------------------

   function In_Pure_Unit return Boolean is
   begin
      return Is_Pure (Current_Scope);
   end In_Pure_Unit;

   -----------------------------------
   -- In_Remote_Call_Interface_Unit --
   -----------------------------------

   function In_Remote_Call_Interface_Unit return Boolean is
      Unit_Entity : constant Entity_Id := Current_Scope;
      Unit_Kind   : constant Node_Kind :=
                      Nkind (Unit (Cunit (Current_Sem_Unit)));

   begin
      --  There are no restrictions on the body of an RCI unit.

      return Is_Remote_Call_Interface (Unit_Entity)
        and then Ekind (Unit_Entity) = E_Package
        and then Unit_Kind /= N_Package_Body
        and then not In_Package_Body (Unit_Entity)
        and then not In_Instance;
   end In_Remote_Call_Interface_Unit;

   --------------------------
   -- In_Remote_Types_Unit --
   --------------------------

   function In_Remote_Types_Unit return Boolean is
      Unit_Entity : constant Entity_Id := Current_Scope;
      Unit_Kind   : constant Node_Kind :=
                      Nkind (Unit (Cunit (Current_Sem_Unit)));

   begin
      --  There are no restrictions on the body of a Remote Types unit.

      return Is_Remote_Types (Unit_Entity)
        and then Ekind (Unit_Entity) = E_Package
        and then Unit_Kind /= N_Package_Body
        and then not In_Package_Body (Unit_Entity)
        and then not In_Instance;
   end In_Remote_Types_Unit;

   ----------------------------
   -- In_Shared_Passive_Unit --
   ----------------------------

   function In_Shared_Passive_Unit return Boolean is
      Unit_Entity : constant Entity_Id := Current_Scope;

   begin
      return Is_Shared_Passive (Unit_Entity);
   end In_Shared_Passive_Unit;

   ---------------------------------------
   -- In_Subprogram_Task_Protected_Unit --
   ---------------------------------------

   function In_Subprogram_Task_Protected_Unit return Boolean is
      E : Entity_Id;
      K : Entity_Kind;

   begin
      --  The following is to verify that a declaration is inside
      --  subprogram, generic subprogram, task unit, protected unit.
      --  Used to validate if a lib. unit is Pure. RM 10.2.1(16).

      --  Use scope chain to check successively outer scopes

      E := Current_Scope;
      loop
         K := Ekind (E);

         if        K = E_Procedure
           or else K = E_Function
           or else K = E_Generic_Procedure
           or else K = E_Generic_Function
           or else K = E_Task_Type
           or else K = E_Task_Subtype
           or else K = E_Protected_Type
           or else K = E_Protected_Subtype
         then
            return True;

         elsif E = Standard_Standard then
            return False;
         end if;

         E := Scope (E);
      end loop;

   end In_Subprogram_Task_Protected_Unit;

   -----------------------------------------
   -- Is_Class_Wide_Type_Remote_Extension --
   -----------------------------------------

   function Is_Class_Wide_Type_Remote_Extension (N : Node_Id) return Boolean is
      Derived  : Entity_Id;
      Root_Ty  : Entity_Id;
      Contexts : List_Id;
      Item     : Node_Id;
      Item_Ety : Entity_Id;
      RACW     : Entity_Id;
      DD       : Node_Id;

      function Compare_Root_W_RACW (E : Entity_Id) return Boolean;
      --  Return True if the list containing input entity E has a
      --  remote access to classwide type and whose designated type is
      --  the root abstract type of the Derived type

      function Compare_Root_W_RACW (E : Entity_Id) return Boolean is
         Remote_Access : Entity_Id := E;

      begin
         while Present (Remote_Access) loop
            if Is_Remote_Access_To_Class_Wide_Type (Remote_Access) then
               DD := Designated_Type (Remote_Access);

               --  Test if the designated type of this Remote-Access-To-
               --  Classwide-type is the Root abstract type of the
               --  derived type.

               if Etype (DD) = Root_Ty then
                  return True;
               end if;
            end if;

            Remote_Access := Next_Entity (Remote_Access);
         end loop;

         return False;
      end Compare_Root_W_RACW;

   --  Start of processing for Is_Class_Wide_Type_Remote_Extension

   begin
      if Nkind (N) /= N_Full_Type_Declaration then
         return False;
      end if;

      if Nkind (Type_Definition (N)) /= N_Derived_Type_Definition then
         return False;
      end if;

      Derived := Defining_Identifier (N);

      if not Is_Limited_Record (Derived) then
         return False;
      end if;

      if not Is_Tagged_Type (Derived) then
         return False;
      end if;

      Root_Ty := Etype (Derived);
      Contexts := Context_Items (Cunit (Current_Sem_Unit));

      if not Present (Contexts) then
         return False;
      end if;

      Item := First (Contexts);

      while Present (Item) loop

         if Nkind (Item) = N_With_Clause then
            Item_Ety := Entity (Name (Item));

            if Is_Remote_Call_Interface (Item_Ety) then
               RACW := First_Entity (Item_Ety);

               if Compare_Root_W_RACW (RACW) then
                  return True;
               end if;
            end if;
         end if;

         Item := Next (Item);
      end loop;

      --  For compiler generated classwide extensions "object_stub" in
      --  an RCI unit (spec and body)

      if Is_Remote_Call_Interface (Derived) then
         RACW := First_Entity (Scope (Derived));

         if Compare_Root_W_RACW (RACW) then
            return True;
         end if;
      end if;

      return False;
   end Is_Class_Wide_Type_Remote_Extension;

   -----------------------------------------
   -- Is_Remote_Access_To_Class_Wide_Type --
   -----------------------------------------

   function Is_Remote_Access_To_Class_Wide_Type
     (E    : Entity_Id)
      return Boolean
   is
      DD : Node_Id;
      ED : Node_Id;

   begin
      --  This type entity would have been set Is_Remote_Call_Interface
      --  during the type declaration in case it is inside an RCI unit.
      --  This type entity would have been set Is_Remote_Types during
      --  the type declaration in case it is inside a Remote_Types unit.

      if not Is_Remote_Call_Interface (E)
        and then not Is_Remote_Types (E)
      then
         return False;
      end if;

      if Ekind (E) = E_General_Access_Type then
         DD := Designated_Type (E);
         ED := Parent (Etype (DD));

         if Nkind (ED) = N_Private_Type_Declaration
           and then Limited_Present (ED)
           and then Ekind (DD) = E_Class_Wide_Type
         then
            return True;
         end if;
      end if;

      return False;
   end Is_Remote_Access_To_Class_Wide_Type;

   -----------------------------------------
   -- Is_Remote_Access_To_Subprogram_Type --
   -----------------------------------------

   function Is_Remote_Access_To_Subprogram_Type
     (E    : Entity_Id)
      return Boolean
   is
   begin
      --  This type entity would have been set Is_Remote_Call_Interface
      --  during the type declaration in case it is inside an RCI unit.
      --  This type entity would have been set Is_Remote_Types during
      --  the type declaration in case it is inside a Remote_Types unit.

      return Ekind (E) = E_Access_Subprogram_Type
        and then (Is_Remote_Call_Interface (E)
                   or else Is_Remote_Types (E));
   end Is_Remote_Access_To_Subprogram_Type;

   --------------------------
   -- Process_Partition_ID --
   --------------------------

   procedure Process_Partition_ID (N : Node_Id) is
      Loc            : constant Source_Ptr := Sloc (N);
      Ety            : Entity_Id;
      Nd             : Node_Id;
      Get_Pt_Id      : Node_Id;
      Get_Pt_Id_Call : Node_Id;
      Prefix_String  : String_Id;
      Typ            : constant Entity_Id := Etype (N);

   begin
      Ety := Entity (Prefix (N));

      --  In case prefix is not a library unit entity, get the entity
      --  of library unit.

      while (Present (Scope (Ety))
        and then Scope (Ety) /= Standard_Standard)
        and not Is_Child_Unit (Ety)
      loop
         Ety := Scope (Ety);
      end loop;

      Nd := Enclosing_Lib_Unit_Node (N);

      --  Retrieve the proper function to call.

      if Is_Remote_Call_Interface (Ety) then
         Get_Pt_Id := New_Occurrence_Of
           (RTE (RE_Get_Active_Partition_Id), Loc);

      elsif Is_Shared_Passive (Ety) then
         Get_Pt_Id := New_Occurrence_Of
           (RTE (RE_Get_Passive_Partition_Id), Loc);

      else
         Get_Pt_Id := New_Occurrence_Of
           (RTE (RE_Get_Local_Partition_Id), Loc);
      end if;

      --  Get and store the String_Id corresponding to the name of the
      --  library unit whose Partition_Id is needed

      Get_Unit_Name_String (Get_Unit_Name (Get_Declaration_Node (Ety)));
      Name_Len := Name_Len - 7;
      --  Remove seven last character ("(spec)" or " (body)").

      Start_String;
      Store_String_Chars (Name_Buffer (1 .. Name_Len));
      Prefix_String := End_String;

      --  Build the function call which will replace the attribute

      if Is_Remote_Call_Interface (Ety) or Is_Shared_Passive (Ety) then

         Get_Pt_Id_Call :=
           Make_Function_Call (Loc,
             Name => Get_Pt_Id,
             Parameter_Associations =>
               New_List (Make_String_Literal (Loc, Prefix_String)));

      else
         Get_Pt_Id_Call := Make_Function_Call (Loc, Get_Pt_Id);

      end if;

      --  Replace the attribute node by a conversion of the function call
      --  to the target type.

      Rewrite (N, Convert_To (Typ, Get_Pt_Id_Call));
      Analyze_And_Resolve (N, Typ);

   end Process_Partition_ID;

   -------------------------------------------
   -- Process_Remote_Access_Subprogram_Type --
   -------------------------------------------

   procedure Process_Remote_Access_Subprogram_Type (N : Node_Id) is
      Id : constant Entity_Id := Defining_Entity (N);
      Vi : constant List_Id   := Visible_Declarations (N);
      Pr : constant List_Id   := Private_Declarations (N);
      Fl : List_Id := New_List;
      Pl : List_Id := New_List;
      Tl : List_Id := New_List;
      Sp : Node_Id;
      Df : Node_Id;

      procedure Build_Lists (L : List_Id);
      --  Given input list L, seperate declarations into three lists, one
      --  access type list, one function specification list and one
      --  procedure specification list

      -----------------
      -- Build_Lists --
      -----------------

      procedure Build_Lists (L : List_Id) is
         Decl : Node_Id := First (L);

      begin
         while Present (Decl) loop
            if Nkind (Decl) = N_Subprogram_Declaration then
               Sp := Specification (Decl);

               if Nkind (Sp) = N_Procedure_Specification then
                  Append (Sp, Pl);

               elsif Nkind (Sp) = N_Function_Specification then
                  Append (Sp, Fl);
               end if;

            elsif Nkind (Decl) = N_Full_Type_Declaration then
               Df := Type_Definition (Decl);

               if Present (Df) then
                  if Nkind (Df) = N_Access_Procedure_Definition
                    or else Nkind (Df) = N_Access_Function_Definition
                  then
                     Append (Df, Tl);
                  end if;
               end if;
            end if;

            Decl := Next (Decl);
         end loop;
      end Build_Lists;

   --  Start processing of Process_Remote_Access_Subprogram_Type

   begin
      if Present (Vi) then
         Build_Lists (Vi);
      end if;

      if Present (Pr) then
         Build_Lists (Pr);
      end if;

      --  Return if no remote access to subprogram type declaration

      if not Present (Tl) then
         return;
      end if;

      return;
   end Process_Remote_Access_Subprogram_Type;

   ----------------------------------
   -- Process_Remote_AST_Attribute --
   ----------------------------------

   procedure Process_Remote_AST_Attribute
     (N        : Node_Id;
      New_Type : Entity_Id)
   is
      Loc                   : constant Source_Ptr := Sloc (N);
      Remote_Subp           : Entity_Id;
      Tick_Access_Conv_Call : Node_Id;
      Remote_Subp_Decl      : Node_Id;
      RAS_Decl              : Node_Id;
      Decl                  : Node_Id;
      RS_Pkg_Specif         : Node_Id;
      RS_Pkg_E              : Entity_Id;
      RAS_Pkg_E             : Entity_Id;
      RAS_Type              : Entity_Id;
      RAS_Name              : Name_Id;
      Async_E               : Entity_Id;
      Subp_Id               : Int := 1;
      Attribute_Subp        : Entity_Id;

   --  Start processing of Process_Remote_AST_Attribute

   begin

      --  check if we have to expand the access attribute

      if Distribution_Stub_Mode = No_Stubs then
         return;
      else
         Remote_Subp := Entity (Prefix (N));
         RAS_Type  := Corresponding_Remote_Type (New_Type);
         RAS_Name  := Chars (RAS_Type);
         RAS_Decl := Parent (RAS_Type);
         Attribute_Subp := TSS (New_Type, Name_uRAS_Access);

         if No (Attribute_Subp) then
            Add_RAST_Features (RAS_Decl);
            Attribute_Subp := TSS (New_Type, Name_uRAS_Access);
         end if;
      end if;

      RAS_Pkg_E  := Defining_Entity (Parent (RAS_Decl));
      Remote_Subp_Decl := Get_Declaration_Node (Remote_Subp);

      if Nkind (Remote_Subp_Decl) = N_Subprogram_Body then
         Remote_Subp := Corresponding_Spec (Remote_Subp_Decl);
         Remote_Subp_Decl := Get_Declaration_Node (Remote_Subp);
      end if;

      RS_Pkg_Specif := Parent (Remote_Subp_Decl);
      RS_Pkg_E := Defining_Entity (RS_Pkg_Specif);

      --  Compute the subprogram Id

      Decl := Prev (Remote_Subp_Decl);

      while Present (Decl)
        and then Nkind (Decl) = N_Subprogram_Declaration
        and then Comes_From_Source (Decl)
      loop
         Subp_Id := Subp_Id + 1;
         Decl := Prev (Decl);
      end loop;

      if Ekind (Remote_Subp) = E_Procedure
        and then Is_Asynchronous (Remote_Subp)
      then
         Async_E := Standard_True;
      else
         Async_E := Standard_False;
      end if;

      Tick_Access_Conv_Call :=
        Make_Function_Call (Loc,
          Name => New_Occurrence_Of (Attribute_Subp, Loc),
          Parameter_Associations =>
            New_List (
              Relocate_Node (N),
              Make_String_Literal (Loc, Full_Qualified_Name (RS_Pkg_E)),
              Make_Integer_Literal (Loc, UI_From_Int (Subp_Id)),
              New_Reference_To (Async_E, Loc)));

      Rewrite_Or_Replace (N, Tick_Access_Conv_Call);
      Analyze (N);

   end Process_Remote_AST_Attribute;

   ------------------------------------
   -- Process_Remote_AST_Declaration --
   ------------------------------------

   procedure Process_Remote_AST_Declaration (N : Node_Id) is
      Loc                   : constant Source_Ptr := Sloc (N);
      User_Type             : constant Node_Id := Defining_Identifier (N);
      Distributed_Name      : constant Name_Id := Get_Name_Id ("distributed");
      Origin_Name           : constant Name_Id := Get_Name_Id ("origin");
      Receiver_Name         : constant Name_Id := Get_Name_Id ("receiver");
      Ras_Name              : constant Name_Id := Get_Name_Id ("ras");
      Pkg_Name              : constant Name_Id := Get_Name_Id ("pkg_name");
      Subp_Id_Name          : constant Name_Id := Get_Name_Id ("subp_id");
      Async_Name            : constant Name_Id := Get_Name_Id ("async");
      Param_Specs           : List_Id := New_List;
      Fat_Type              : constant Entity_Id :=
                                Make_Defining_Identifier
                                  (Loc, Chars (User_Type));
      New_Type_Decl         : Node_Id;

   begin

      --  We add a record type declaration for the equivalent fat pointer type.

      New_Type_Decl :=
        Make_Full_Type_Declaration (Loc,
          Defining_Identifier => Fat_Type,
          Discriminant_Specifications =>
            New_List (
              Make_Discriminant_Specification (Loc,
                Defining_Identifier =>
                  Make_Defining_Identifier (Loc, Distributed_Name),
                Discriminant_Type =>
                  New_Reference_To (Standard_Boolean, Loc),
                Expression =>
                  New_Reference_To (Standard_False, Loc))),

          Type_Definition =>
            Make_Record_Definition (Loc,
              Component_List =>
                Make_Component_List (Loc,
                  Component_Items => New_List,
                  Variant_Part    =>
                    Make_Variant_Part (Loc,
                      Name => Make_Identifier (Loc, Distributed_Name),
                      Variants =>

                        New_List (
                          Make_Variant (Loc,
                            Discrete_Choices =>
                              New_List (
                                New_Reference_To (Standard_True, Loc)),
                            Component_List =>
                              Make_Component_List (Loc,
                                Component_Items =>

                                  New_List (
                                    Make_Component_Declaration (Loc,
                                      Defining_Identifier =>
                                        Make_Defining_Identifier (Loc,
                                          Chars => Origin_Name),
                                      Subtype_Indication =>
                                        New_Reference_To
                                          (Standard_Integer,
                                           Loc)),

                                    Make_Component_Declaration (Loc,
                                      Defining_Identifier =>
                                        Make_Defining_Identifier (Loc,
                                          Chars => Receiver_Name),
                                      Subtype_Indication =>
                                        New_Reference_To
                                          (RTE (RE_Address), Loc)),

                                    Make_Component_Declaration (Loc,
                                      Defining_Identifier =>
                                        Make_Defining_Identifier (Loc,
                                          Chars => Subp_Id_Name),
                                      Subtype_Indication =>
                                        New_Reference_To
                                          (Standard_Natural,
                                           Loc)),

                                    Make_Component_Declaration (Loc,
                                      Defining_Identifier =>
                                        Make_Defining_Identifier (Loc,
                                          Chars => Async_Name),
                                      Subtype_Indication =>
                                        New_Reference_To
                                          (Standard_Boolean,
                                           Loc))))),
                          Make_Variant (Loc,
                            Discrete_Choices =>
                              New_List (
                                New_Reference_To (Standard_False, Loc)),
                            Component_List =>
                              Make_Component_List (Loc,
                                Component_Items =>

                                  New_List (
                                    Make_Component_Declaration (Loc,
                                      Defining_Identifier =>
                                        Make_Defining_Identifier (Loc,
                                          Chars => Ras_Name),
                                      Subtype_Indication =>
                                        New_Occurrence_Of
                                          (User_Type, Loc))))))))));

      Insert_After (N, New_Type_Decl);
      Set_Equivalent_Type (User_Type, Fat_Type);
      Set_Corresponding_Remote_Type (Fat_Type, User_Type);

   end Process_Remote_AST_Declaration;

   -----------------------
   -- RAS_E_Dereference --
   -----------------------

   procedure RAS_E_Dereference (Pref : Node_Id) is
      Loc             : constant Source_Ptr := Sloc (Pref);
      Call_Node       : Node_Id;
      New_Type        : constant Entity_Id := Etype (Pref);
      RAS             : constant Entity_Id :=
                          Corresponding_Remote_Type (New_Type);
      RAS_Decl        : constant Node_Id   := Parent (RAS);
      Explicit_Deref  : constant Node_Id   := Parent (Pref);
      Deref_Subp_Call : constant Node_Id   := Parent (Explicit_Deref);
      Deref_Proc      : Entity_Id;
      Params          : List_Id;

   begin
      if Nkind (Deref_Subp_Call) = N_Procedure_Call_Statement then
         Params := Parameter_Associations (Deref_Subp_Call);

         if Present (Params) then
            Prepend (Pref, Params);
         else
            Params := New_List (Pref);
         end if;

      elsif Nkind (Deref_Subp_Call) = N_Indexed_Component then

         Params := Expressions (Deref_Subp_Call);

         if Present (Params) then
            Prepend (Pref, Params);
         else
            Params := New_List (Pref);
         end if;

      else
         --  context is not a call.

         return;
      end if;

      Deref_Proc := TSS (New_Type, Name_uRAS_Dereference);

      if No (Deref_Proc) then
         Add_RAST_Features (RAS_Decl);
         Deref_Proc := TSS (New_Type, Name_uRAS_Dereference);
      end if;

      if Ekind (Deref_Proc) = E_Function then
         Call_Node :=
           Make_Function_Call (Loc,
              Name => New_Occurrence_Of (Deref_Proc, Loc),
              Parameter_Associations => Params);
      else
         Call_Node :=
           Make_Procedure_Call_Statement (Loc,
              Name => New_Occurrence_Of (Deref_Proc, Loc),
              Parameter_Associations => Params);
      end if;

      Rewrite_Or_Replace (Deref_Subp_Call, Call_Node);
      Analyze (Deref_Subp_Call);
   end RAS_E_Dereference;


   ------------------------------
   -- Remote_AST_E_Dereference --
   ------------------------------

   function Remote_AST_E_Dereference (P : Node_Id) return Boolean
   is
      ET       : constant Entity_Id  := Etype (P);
   begin
      --  Perform the changes only on original dereferences.

      if Comes_From_Source (P)
        and then Is_Record_Type (ET)
        and then (Is_Remote_Call_Interface (ET)
                   or else Is_Remote_Types (ET))
        and then Present (Corresponding_Remote_Type (ET))
        and then (Nkind (Parent (Parent (P))) = N_Procedure_Call_Statement
                   or else Nkind (Parent (Parent (P))) = N_Indexed_Component)
        and then Distribution_Stub_Mode /= No_Stubs
      then
         RAS_E_Dereference (P);
         return True;
      else
         return False;
      end if;
   end Remote_AST_E_Dereference;

   ------------------------------
   -- Remote_AST_I_Dereference --
   ------------------------------

   function Remote_AST_I_Dereference (P : Node_Id) return Boolean
   is
      ET     : constant Entity_Id  := Etype (P);
      Deref  : Node_Id;
   begin

      if Comes_From_Source (P)
        and then (Is_Remote_Call_Interface (ET)
                   or else Is_Remote_Types (ET))
        and then Present (Corresponding_Remote_Type (ET))
        and then Distribution_Stub_Mode /= No_Stubs
      then
         Deref := Make_Explicit_Dereference (Sloc (P),
           Prefix => Relocate_Node (P));
         Rewrite (P, Deref);
         Set_Etype (P, ET);
         RAS_E_Dereference (Prefix (P));
         return True;
      end if;

      return False;
   end Remote_AST_I_Dereference;

   ------------------------
   -- Rewrite_Or_Replace --
   ------------------------

   procedure Rewrite_Or_Replace (N : Node_Id; New_Node : Node_Id) is
   begin
      if Distribution_Stub_Mode = Generate_Receiver_Stub_Body
        or else Distribution_Stub_Mode = Generate_Caller_Stub_Body
      then
         Replace (N, New_Node);
      else
         Rewrite (N, New_Node);
      end if;
   end Rewrite_Or_Replace;

   -------------------------------------
   -- Set_Categorization_From_Pragmas --
   -------------------------------------

   procedure Set_Categorization_From_Pragmas (N : Node_Id) is
      P : constant Node_Id := Parent (N);

   begin

      --  Deal with categorization pragmas in Pragmas of Compilation_Unit.
      --  The purpose is to set categorization flags before analyzing the
      --  unit itself, so as to diagnose violations of categorization as
      --  we process each declaration, even though the pragma appears after
      --  the unit.

      if Nkind (P) /= N_Compilation_Unit then
         return;
      end if;

      declare
         PN : Node_Id := First (Pragmas_After (Aux_Decls_Node (P)));
      begin
         while Present (PN) loop

            --  Skip implicit types that may have been introduced by
            --  previous analysis.

            if Nkind (PN) = N_Pragma then

               case Get_Pragma_Id (Chars (PN)) is
                  when Pragma_All_Calls_Remote   |
                    Pragma_Preelaborate          |
                    Pragma_Pure                  |
                    Pragma_Remote_Call_Interface |
                    Pragma_Remote_Types          |
                    Pragma_Shared_Passive        => Analyze (PN);
                  when others                    => null;
               end case;
            end if;

            PN := Next (PN);
         end loop;
      end;
   end Set_Categorization_From_Pragmas;

   ---------------------------------
   -- Should_Declare_Partition_ID --
   ---------------------------------

   function Should_Declare_Partition_ID (L : List_Id) return Boolean is
      Nd : Node_Id := First (L);
      Ch : Name_Id;
      Na : Node_Id := Defining_Entity (Parent (L));

   begin
      while Present (Nd) loop
         if Nkind (Nd) = N_Pragma then
            Ch := Chars (Nd);

            if Ch = Name_Preelaborate
              or else Ch = Name_Remote_Call_Interface
              or else Ch = Name_Shared_Passive
              or else Ch = Name_Remote_Types
            then
               return True;

            elsif Ch = Name_Pure then
               return False;
            end if;
         end if;

         Nd := Next (Nd);
      end loop;

      --  This is a non-categorizaed library unit

      if Nkind (Na) = N_Defining_Program_Unit_Name
        and then Nkind (Name (Na)) = N_Identifier
        and then Chars (Name (Na)) = Name_System
        and then Nkind (Defining_Identifier (Na)) = N_Defining_Identifier
        and then Chars (Defining_Identifier (Na)) = Name_Rpc
      then
         return True;
      end if;

      return False;
   end Should_Declare_Partition_ID;

   ------------------------------
   -- Static_Discriminant_Expr --
   ------------------------------

   function Static_Discriminant_Expr (L : List_Id) return Boolean is
      Discriminant_Spec : Node_Id;

   begin
      Discriminant_Spec := First (L);
      while Present (Discriminant_Spec) loop
         if Present (Expression (Discriminant_Spec))
           and then not Is_Static_Expression (Expression (Discriminant_Spec))
         then
            return False;
         end if;

         Discriminant_Spec := Next (Discriminant_Spec);
      end loop;

      return True;
   end Static_Discriminant_Expr;

   --------------------------------------
   -- Validate_Access_Type_Declaration --
   --------------------------------------

   procedure Validate_Access_Type_Declaration (T : Entity_Id; N : Node_Id) is
      Def : constant Node_Id := Type_Definition (N);

   begin
      case Nkind (Def) is
         when N_Access_To_Subprogram_Definition =>

            --  A pure library_item must not contain the declaration of a
            --  named access type, except within a subprogram, generic
            --  subprogram, task unit, or protected unit (RM 10.2.1(16)).

            if Comes_From_Source (T)
               and then In_Pure_Unit
               and then not In_Subprogram_Task_Protected_Unit
            then
               Error_Msg_N ("named access type not allowed in pure unit", T);
            end if;

            --  Set Is_Remote_Call_Interface flag on entity to allow easy
            --  checks later on for required validations of RCI units. This
            --  is only done for entities that are in the original source.

            if Comes_From_Source (T)
              and then In_Remote_Call_Interface_Unit
            then
               Set_Is_Remote_Call_Interface (T);
            end if;

            --  Set Is_Remote_Types flag on entity to allow easy
            --  checks later on for required validations of such units. This
            --  is only done for entities that are in the original source.

            if Comes_From_Source (T)
              and then In_Remote_Types_Unit
            then
               Set_Is_Remote_Types (T);
            end if;

         when N_Access_To_Object_Definition =>

            if Comes_From_Source (T)
              and then In_Pure_Unit
              and then not In_Subprogram_Task_Protected_Unit
            then
               Error_Msg_N
                 ("named access type not allowed in pure unit", T);
            end if;

            --  Check for RCI unit type declaration. It should not contain
            --  the declaration of an access-to-object type unless it is a
            --  general access type that designates a class-wide limited
            --  private type. There are also constraints about the primitive
            --  subprograms of the class-wide type.

            Validate_RCI_Access_Object_Type_Declaration (T);

            --  Check for shared passive unit type declaration. It should
            --  not contain the declaration of access to class wide type,
            --  access to task type and access to protected type with entry.

            Validate_SP_Access_Object_Type_Decl (T);

            --  Set Is_Remote_Types flag on entity to allow easy
            --  checks later on for required validations of such units. This
            --  is only done for entities that are in the original source.

            if Comes_From_Source (T)
              and then In_Remote_Types_Unit
            then
               Set_Is_Remote_Types (T);
            end if;

         when others => null;

      end case;

   end Validate_Access_Type_Declaration;

   ----------------------------------------
   -- Validate_Categorization_Dependency --
   ----------------------------------------

   procedure Validate_Categorization_Dependency
     (N : Node_Id;
      E : Entity_Id)
   is
      K : constant Node_Kind := Nkind (N);
      P : constant Node_Id   := Parent (N);

   begin
      --  Validate library unit only

      if Nkind (P) /= N_Compilation_Unit then
         return;
      end if;

      --  Body of RCI unit does not need validation.

      if Is_Remote_Call_Interface (E)
        and then (Nkind (N) = N_Package_Body
                   or else Nkind (N) = N_Subprogram_Body)
      then
         return;
      end if;

      --  Process with clauses

      declare
         Item             : Node_Id;
         Entity_Of_Withed : Entity_Id;

      begin
         Item := First (Context_Items (P));

         while Present (Item) loop
            if Nkind (Item) = N_With_Clause
              and then not Implicit_With (Item)
            then
               Entity_Of_Withed := Entity (Name (Item));
               Check_Categorization_Dependencies (E, Entity_Of_Withed, Item);
            end if;

            Item := Next (Item);
         end loop;
      end;

      --  Child depends on parent therefore parent should also
      --  be categorized and satify the dependecy hierarchy.

      --  Check if N is a child spec.

      if (K in N_Generic_Declaration              or else
          K in N_Generic_Instantiation            or else
          K in N_Generic_Renaming_Declaration     or else
          K =  N_Package_Declaration              or else
          K =  N_Package_Renaming_Declaration     or else
          K =  N_Subprogram_Declaration           or else
          K =  N_Subprogram_Renaming_Declaration)
        and then Present (Parent_Spec (N))
      then
         declare
            Parent_Lib_U  : constant Node_Id   := Parent_Spec (N);
            Parent_Kind   : constant Node_Kind :=
                              Nkind (Unit (Parent_Lib_U));
            Parent_Entity : Entity_Id;

         begin
            if        Parent_Kind =  N_Package_Instantiation
              or else Parent_Kind =  N_Procedure_Instantiation
              or else Parent_Kind =  N_Function_Instantiation
              or else Parent_Kind =  N_Package_Renaming_Declaration
              or else Parent_Kind in N_Generic_Renaming_Declaration
            then
               Parent_Entity := Defining_Entity (Unit (Parent_Lib_U));

            else
               Parent_Entity :=
                 Defining_Entity (Specification (Unit (Parent_Lib_U)));
            end if;

            Check_Categorization_Dependencies (E, Parent_Entity, N);

            --  Verify that public child of an RCI library unit
            --  must also be an RCI library unit (RM E.2.3(15)).

            if Is_Remote_Call_Interface (Parent_Entity)
              and then not Private_Present (P)
              and then not Is_Remote_Call_Interface (E)
            then
               Error_Msg_N
                 ("public child of rci unit must also be rci unit", N);
               return;
            end if;
         end;
      end if;

   end Validate_Categorization_Dependency;

   --------------------------------------
   -- Validate_Null_Statement_Sequence --
   --------------------------------------

   procedure Validate_Null_Statement_Sequence (N : Node_Id) is
      Item : Node_Id;

   begin
      if In_Preelaborated_Unit then
         Item := First (Statements (Handled_Statement_Sequence (N)));

         while Present (Item) loop
            if Nkind (Item) /= N_Label
              and then Nkind (Item) /= N_Null_Statement
            then
               Error_Msg_N
                 ("statements not allowed in preelaborated unit", Item);
               exit;
            end if;

            Item := Next (Item);
         end loop;
      end if;
   end Validate_Null_Statement_Sequence;

   ---------------------------------
   -- Validate_Object_Declaration --
   ---------------------------------

   procedure Validate_Object_Declaration
     (N   : Node_Id;
      Id  : Entity_Id;
      E   : Node_Id;
      Odf : Node_Id;
      T   : Entity_Id)
   is
   begin
      --  Verify that any access to subprogram object does not have in its
      --  subprogram profile access type parameters or limited parameters
      --  without Read and Write attributes (E.2.3(13)).

      Validate_RCI_Subprogram_Declaration (N);

      --  Check that if we are in preelaborated elaboration code, then we
      --  do not have an instance of a default initialized private, task or
      --  protected object declaration which would violate (RM 10.2.1(9)).
      --  Note that constants are never default initialized (and the test
      --  below also filters out deferred constants). A variable is default
      --  initialized if it does *not* have an initialization expression.

      --  Filter out cases that are not declaration of a variable from source.

      if Nkind (N) /= N_Object_Declaration
        or else Constant_Present (N)
        or else not Comes_From_Source (Id)
      then
         return;
      end if;

      if In_Preelaborated_Unit
        and then not In_Subprogram_Or_Concurrent_Unit
      then
         if No (E) then
            declare
               Ent : Entity_Id;

            begin
               --  Object decl. that is of record type and has no default expr.
               --  should check if there is any non-static default expression
               --  in component decl. of the record type decl.

               if Is_Record_Type (T) then
                  if Nkind (Parent (T)) = N_Full_Type_Declaration then
                     Check_Non_Static_Default_Expr
                       (Type_Definition (Parent (T)));

                  elsif Nkind (Odf) = N_Subtype_Indication then
                     Check_Non_Static_Default_Expr (Type_Definition
                       (Parent (Entity (Subtype_Mark (Odf)))));
                  end if;
               end if;

               --  Similarly, array whose component type is record of component
               --  declarations with default expression that is non-static
               --  is a violation.

               if Is_Array_Type (T) then
                  if Nkind (Parent (T)) = N_Full_Type_Declaration then
                     declare
                        Comp_Type : Entity_Id := Component_Type (T);

                     begin
                        while Is_Array_Type (Comp_Type) loop
                           Comp_Type := Component_Type (Comp_Type);
                        end loop;

                        if Is_Record_Type (Comp_Type) then
                           if Nkind (Parent (Comp_Type)) =
                             N_Full_Type_Declaration
                           then
                              Check_Non_Static_Default_Expr
                                  (Type_Definition (Parent (Comp_Type)));
                           end if;
                        end if;
                     end;
                  end if;
               end if;

               if Is_Private_Type (Id)
                 or else
                   (Is_Access_Type (T)
                     and then
                       Depends_On_Private (Designated_Type (T)))
                 or else Depends_On_Private (T)
               then
                  Error_Msg_N
                    ("private object not allowed in preelaborated unit", N);
                  return;

               --  Access to Task or Protected type

               elsif Nkind (Odf) = N_Identifier
                 and then Present (Etype (Odf))
                 and then Is_Access_Type (Etype (Odf))
               then
                  Ent := Designated_Type (Etype (Odf));

               elsif Nkind (Odf) = N_Identifier then
                  Ent := Entity (Odf);

               elsif Nkind (Odf) = N_Subtype_Indication then
                  Ent := Etype (Subtype_Mark (Odf));

               elsif
                  Nkind (Odf) = N_Constrained_Array_Definition
               then
                  Ent := Component_Type (T);

               else
                  return;
               end if;

               if Is_Task_Type (Ent)
                 or else (Is_Protected_Type (Ent) and then Has_Entries (Ent))
               then
                  Error_Msg_N
                    ("concurrent object not allowed in preelaborated unit",
                     N);
                  return;

               end if;
            end;
         end if;

         --  Evaluation of discriminant default expr. is done when obj.
         --  is created. And it has to be static expr.

         if Is_Record_Type (Etype (Id)) then
            declare
               ET  : constant Entity_Id := Etype (Id);
               EE  : constant Entity_Id := Etype (Etype (Id));
               PEE : Node_Id;

            begin
               if Has_Discriminants (ET)
                 and then Present (EE)
               then
                  PEE := Parent (EE);

                  if Nkind (PEE) = N_Full_Type_Declaration
                    and then not Static_Discriminant_Expr
                                  (Discriminant_Specifications (PEE))
                  then
                     Error_Msg_N
                       ("non-static discriminant in preelaborated unit",
                        PEE);
                  end if;
               end if;
            end;
         end if;

         --  Similarly, array whose component type is record of component
         --  declarations with discriminant expression that is non-static
         --  is a violation.

         if Is_Array_Type (T) then
            if Nkind (Parent (T)) = N_Full_Type_Declaration then
               declare
                  Comp_Type : Entity_Id := Component_Type (T);

               begin
                  while Is_Array_Type (Comp_Type) loop
                     Comp_Type := Component_Type (Comp_Type);
                  end loop;

                  if Is_Record_Type (Comp_Type)
                    and then Has_Discriminants (Comp_Type)
                    and then
                      Nkind (Parent (Comp_Type)) = N_Full_Type_Declaration
                    and then not Static_Discriminant_Expr
                      (Discriminant_Specifications (Parent (Comp_Type)))
                  then
                     Error_Msg_N
                       ("non-static discriminant in preelaborated unit",
                        Comp_Type);
                  end if;
               end;
            end if;
         end if;

      end if;

      --  A pure library_item must not contain the declaration of any
      --  variable except within  a subprogram, generic subprogram, task
      --  unit or protected unit (RM 10.2.1(16)).

      if In_Pure_Unit
        and then not In_Subprogram_Task_Protected_Unit
      then
         Error_Msg_N ("declaration of variable not allowed in pure unit", N);

      --  The visible part of an RCI library unit must not contain the
      --  declaration of a variable (RM E.1.3(9))

      elsif In_Remote_Call_Interface_Unit then
         Error_Msg_N ("declaration of variable not allowed in rci unit", N);

      --  The visible part of a Shared Passive library unit must not contain
      --  the declaration of a variable (RM E.2.2(7))

      elsif In_Remote_Types_Unit then
         Error_Msg_N
           ("variable declaration not allowed in remote types unit", N);
      end if;

   end Validate_Object_Declaration;

   --------------------------------
   --  Validate_RCI_Declarations --
   --------------------------------

   procedure Validate_RCI_Declarations (P : Entity_Id) is
      E : Entity_Id;

   begin

      E := First_Entity (P);

      while Present (E) loop
         if Comes_From_Source (E) then

            if Is_Limited_Type (E) then
               Error_Msg_N
                 ("Limited type not allowed in rci unit", Parent (E));

            elsif Ekind (E) = E_Generic_Function
              or else Ekind (E) = E_Generic_Package
              or else Ekind (E) = E_Generic_Procedure
            then
               Error_Msg_N ("generic declaration not allowed in rci unit",
                 Parent (E));

            elsif (Ekind (E) = E_Function
                    or else Ekind (E) = E_Procedure)
              and then Is_Inlined (E)
            then
               Error_Msg_N
                 ("inlined subprogram not allowed in rci unit", Parent (E));

            elsif Ekind (E) = E_Package then
               Validate_RCI_Declarations (E);
            end if;
         end if;

         E := Next_Entity (E);
      end loop;
   end Validate_RCI_Declarations;

   -------------------------------------------------
   -- Validate_RCI_Access_Object_Type_Declaration --
   -------------------------------------------------

   procedure Validate_RCI_Access_Object_Type_Declaration (T : Entity_Id) is
      Direct_Designated_Type : Entity_Id;
      Desig_Type         : Entity_Id;
      Primitive_Subprograms  : Elist_Id;
      Type_Decl              : Node_Id;
      Subprogram             : Elmt_Id;
      Subprogram_Node        : Node_Id;
      Profile                : List_Id;
      Param_Spec             : Node_Id;
      Param_Type             : Entity_Id;
      Limited_Type_Decl      : Node_Id;

   begin
      --  We are called from Analyze_Type_Declaration, and the Nkind
      --  of the given node is N_Access_To_Object_Definition.

      if not Comes_From_Source (T)
        or else not In_Remote_Call_Interface_Unit
      then
         return;
      end if;

      --  Check RCI unit type declaration. It should not contain the
      --  declaration of an access-to-object type unless it is a
      --  general access type that designates a class-wide limited
      --  private type. There are also constraints about the primitive
      --  subprograms of the class-wide type (RM E.2.3(14)).

      if Ekind (T) /= E_General_Access_Type
        or else Ekind (Designated_Type (T)) /= E_Class_Wide_Type
      then
         Error_Msg_N
           ("Access type in RCI unit must be general access-to-class-wide",
            T);
         return;
      end if;

      Direct_Designated_Type := Designated_Type (T);

      Desig_Type := Etype (Direct_Designated_Type);
      Type_Decl       := Parent (Desig_Type);

      if Nkind (Type_Decl) /= N_Private_Type_Declaration
        or else not Limited_Present (Type_Decl)
        or else Primitive_Operations (Desig_Type) = No_Elist
      then
         Error_Msg_N
           ("designated type must be limited private and have primitive ops",
            T);
         return;
      end if;

      Primitive_Subprograms := Primitive_Operations (Desig_Type);
      Subprogram            := First_Elmt (Primitive_Subprograms);

      while Subprogram /= No_Elmt loop
         Subprogram_Node := Node (Subprogram);

         if not Comes_From_Source (Subprogram_Node) then
            goto Next_Subprogram;
         end if;

         Profile := Parameter_Specifications (Parent (Subprogram_Node));

         --  Profile must exist, otherwise not primitive operation

         Param_Spec := First (Profile);

         while Present (Param_Spec) loop

            --  Now find out if this parameter is a controlling parameter

            Param_Type := Parameter_Type (Param_Spec);

            if Nkind (Param_Type) = N_Identifier
              and then Etype (Param_Type) = Desig_Type
            then
               --  It is indeed a controlling parameter, and since it's not
               --  an access parameter, this is a violation.

               Error_Msg_N
                 ("not access control parameter in rci unit", Param_Spec);

            elsif Nkind (Param_Type) = N_Access_Definition
              and then Subtype_Mark (Param_Type) = Desig_Type
            then
               --  It is indeed controlling parameter but since it's an
               --  access parameter, this is not a violation.

               null;

            elsif
              Is_Limited_Type (Etype (Defining_Identifier (Param_Spec)))
            then
               --  Not a controlling parameter, so type must have Read
               --  and Write attributes.

               if Nkind (Param_Type) = N_Identifier
                 and then Nkind (Parent (Etype (Param_Type))) =
                          N_Private_Type_Declaration
               then
                  Param_Type := Etype (Param_Type);
                  Limited_Type_Decl := Parent (Param_Type);


                  if No (TSS (Param_Type, Name_uRead))
                    or else No (TSS (Param_Type, Name_uWrite))
                  then
                     Error_Msg_N
                       ("limited formal must have read/write attributes",
                         Param_Spec);
                  end if;
               end if;
            end if;

            --  Check next parameter in this subprogram

            Param_Spec  := Next (Param_Spec);
         end loop;

         <<Next_Subprogram>>
            Subprogram := Next_Elmt (Subprogram);
      end loop;

      --  Now this is an RCI unit access-to-class-wide-limited-private type
      --  declaration. Set the type entity to be Is_Remote_Call_Interface to
      --  optimize later checks by avoiding tree traversal to find out if this
      --  entity is inside an RCI unit.

      Set_Is_Remote_Call_Interface (T);

   end Validate_RCI_Access_Object_Type_Declaration;

   -----------------------------------------
   -- Validate_RCI_Subprogram_Declaration --
   -----------------------------------------

   procedure Validate_RCI_Subprogram_Declaration (N : Node_Id) is
      K           : Node_Kind := Nkind (N);
      Profile     : List_Id;
      Id          : Node_Id;
      Param_Spec  : Node_Id;
      Param_Type  : Entity_Id;
      Type_Decl   : Node_Id;
      Error_Node  : Node_Id := N;

   begin
      --  There are two possible cases in which this procedure is called:

      --    1. called from Analyze_Subprogram_Declaration.
      --    2. called from Validate_Object_Declaration (access to subprogram).

      if not In_Remote_Call_Interface_Unit then
         return;
      end if;

      if K = N_Subprogram_Declaration then
         Profile := Parameter_Specifications (Specification (N));

      elsif K = N_Object_Declaration then
         Id := Defining_Identifier (N);

         if Nkind (Id) = N_Defining_Identifier
           and then Nkind (Parent (Etype (Id))) = N_Full_Type_Declaration
           and then Ekind (Etype (Id)) = E_Access_Subprogram_Type
         then
            Profile :=
              Parameter_Specifications (Type_Definition (Parent (Etype (Id))));
         else
            return;
         end if;
      end if;

      --  Iterate through the parameter specification list, checking that
      --  no access parameter and no limited type paramter in the list.

      if Present (Profile) then
         Param_Spec := First (Profile);

         while Present (Param_Spec) loop
            Param_Type := Etype (Defining_Identifier (Param_Spec));
            Type_Decl  := Parent (Param_Type);

            if Ekind (Param_Type) = E_Anonymous_Access_Type then

               if K = N_Subprogram_Declaration then
                  Error_Node := Param_Spec;
               end if;

               --  Report error only if declaration is in source program.

               if Comes_From_Source
                 (Defining_Entity (Specification (N)))
               then
                  Error_Msg_N
                    ("subprogram in rci unit cannot have access parameter",
                      Error_Node);
               end if;

            --  For limited private type parameter, we check only the
            --  private declaration and ignore full type declaration.

            elsif Is_Limited_Type (Param_Type)
              and then Nkind (Type_Decl) = N_Private_Type_Declaration
            then

               --  A limited parameter is legal only if user-specified
               --  Read and Write attributes exist for it.

               if No (TSS (Param_Type, Name_uRead))
                 or else No (TSS (Param_Type, Name_uWrite))
               then

                  if K = N_Subprogram_Declaration then
                     Error_Node := Param_Spec;
                  end if;

                  Error_Msg_N
                    ("limited parameter in rci unit "
                       & "must have read/write attributes ", Error_Node);
               end if;
            end if;

            Param_Spec  := Next (Param_Spec);
         end loop;
      end if;
   end Validate_RCI_Subprogram_Declaration;

   -----------------------------------------------
   -- Validate_Remote_Access_To_Class_Wide_Type --
   -----------------------------------------------

   procedure Validate_Remote_Access_To_Class_Wide_Type (N : Node_Id) is
      K  : constant Node_Kind := Nkind (N);
      PK : constant Node_Kind := Nkind (Parent (N));
      E  : Entity_Id;

   begin
      --  This subprogram enforces the checks in (RM E.2.2(8)) for
      --  certain uses of class-wide limited private types.

      --    Storage_Pool and Storage_Size are not defined for such types
      --
      --    The expected type of allocator must not not be such a type.

      --    The actual parameter of generic instantiation must not
      --    be such a type.

      --  On entry, there are four cases

      --    1. called from sem_attr Analyze_Attribute where attribute
      --       name is either Storage_Pool or Storage_Size.

      --    2. called from exp_ch4 Expand_N_Allocator

      --    3. called from sem_ch12 Analyze_Associations

      --    4. called from sem_ch4 Analyze_Explicit_Dereference

      if not Present (N)
        or else Distribution_Stub_Mode = Compile_Receiver_Stub_Spec
        or else Distribution_Stub_Mode = Compile_Caller_Stub_Spec
      then
         return;
      end if;

      if K = N_Attribute_Reference then
         E := Etype (Prefix (N));

         if Is_Remote_Access_To_Class_Wide_Type (E) then
            Error_Msg_N ("incorrect attribute of remote operand", N);
            return;
         end if;

      elsif K = N_Allocator then
         E := Etype (N);

         if Is_Remote_Access_To_Class_Wide_Type (E) then
            Error_Msg_N ("incorrect expected remote type of allocator", N);
            return;
         end if;

      elsif K = N_Identifier then
         E := Entity (N);

         if Is_Remote_Access_To_Class_Wide_Type (E) then
            Error_Msg_N ("incorrect remote type generic actual", N);
            return;
         end if;

      --  This subprogram also enforces the checks in E.2.2(13).
      --  A value of such type must not be explicitly dereferenced
      --  unless in a dispatching call.

      elsif K = N_Explicit_Dereference then
         E := Etype (Prefix (N));

         if Is_Remote_Access_To_Class_Wide_Type (E)
           and then PK /= N_Procedure_Call_Statement
           and then PK /= N_Function_Call
         then
            --  The following is to let the compiler generated tags check
            --  pass through without error message. This is a bit kludgy
            --  isn't there some better way of making this exclusion ???

            if (PK = N_Selected_Component
                 and then Present (Parent (Parent (N)))
                 and then Nkind (Parent (Parent (N))) = N_Op_Ne)
              or else (PK = N_Unchecked_Type_Conversion
                        and then Present (Parent (Parent (N)))
                        and then
                          Nkind (Parent (Parent (N))) = N_Selected_Component)
            then
               return;
            end if;

            --  The following is to let the compiler generated membership
            --  check and type conversion pass through without error message.

            if (PK = N_Not_In
                 and then Present (Parent (Parent (N)))
                 and then Nkind (Parent (Parent (N))) = N_If_Statement)
              or else (PK = N_Indexed_Component
                        and then Present (Parent (Parent (N)))
                        and then
                          Nkind (Parent (Parent (N))) = N_Selected_Component)
            then
               return;
            end if;

            Error_Msg_N ("incorrect remote type dereference", N);
         end if;
      end if;
   end Validate_Remote_Access_To_Class_Wide_Type;

   ------------------------------------------
   -- Validate_Remote_Type_Type_Conversion --
   ------------------------------------------

   procedure Validate_Remote_Type_Type_Conversion (N : Node_Id) is
      S  : constant Entity_Id := Etype (N);
      E  : constant Entity_Id := Etype (Expression (N));

   begin
      --  This test is required in the case where a conversion apears
      --  inside a normal package, it does not necessarily have to be
      --  inside an RCI, Remote_Types unit (RM E.2.2(9,12)).

      if Is_Remote_Access_To_Subprogram_Type (E)
        and then not Is_Remote_Access_To_Subprogram_Type (S)
      then
         Error_Msg_N ("incorrect conversion of remote operand", N);
         return;

      elsif Is_Remote_Access_To_Class_Wide_Type (E)
        and then not Is_Remote_Access_To_Class_Wide_Type (S)
      then
         Error_Msg_N ("incorrect conversion of remote operand", N);
         return;
      end if;
   end Validate_Remote_Type_Type_Conversion;

   -------------------------------
   -- Validate_RT_RAT_Component --
   -------------------------------

   procedure Validate_RT_RAT_Component (N : Node_Id) is
      Spec     : constant Node_Id   := Specification (N);
      Name_U   : constant Entity_Id := Defining_Entity (Spec);
      Visible  : constant List_Id   := Visible_Declarations (Spec);
      Privat   : constant List_Id   := Private_Declarations (Spec);
      Is_RT    : constant Boolean   := Is_Remote_Types (Name_U);
      Ty_Decl  : Node_Id;
      P_Id     : Entity_Id;
      F_Id     : Entity_Id;
      Comp_Lst : List_Id;
      Comp_Itm : Node_Id;
      Comp_Typ : Entity_Id;
      Clause   : Node_Id;
      Found_R  : Boolean;
      Found_W  : Boolean;

   begin
      --  By the time we get here, the unit is already analyzed and it's
      --  safe to test if unit is Remote_Types from unit name.

      if not Is_RT then
         return;
      end if;

      --  Now, violation happens when a visible private type has a
      --  non-visible remote access type component without read/write

      if not Present (Visible)
        or else not Present (Privat)
      then
         return;
      end if;

      Ty_Decl := First (Visible);
      while Present (Ty_Decl) loop
         if Nkind (Ty_Decl) = N_Private_Type_Declaration then

            --  Private Identifier and Full view Identifier

            P_Id := Defining_Identifier (Ty_Decl);
            F_Id := Full_View (P_Id);

            if Ekind (F_Id) /= E_Record_Type then
               return;
            end if;

            --  Now get to the full view definition component items

            if Null_Present (Type_Definition (Parent (F_Id))) then
               goto Next_Type;
            else
               Comp_Lst := Component_Items (Component_List (Type_Definition
                             (Parent (F_Id))));
            end if;

            --  Check if any component is a violation

            Comp_Itm := First (Comp_Lst);
            while Present (Comp_Itm) loop
               if Nkind (Subtype_Indication (Comp_Itm))
                  = N_Subtype_Indication
               then
                  Comp_Typ :=
                    Entity (Subtype_Mark (Subtype_Indication (Comp_Itm)));
               else
                  Comp_Typ :=
                    Entity (Subtype_Indication (Comp_Itm));
               end if;

               Found_R := False;
               Found_W := False;

               --  Candidates are private access types

               if Is_Private (Comp_Typ)
                 and then Ekind (Comp_Typ) = E_Access_Type
               then
                  Clause := First (Privat);

                  while Present (Clause) loop

                     --  Check if read/write present

                     if Nkind (Clause) = N_Attribute_Definition_Clause
                       and then Entity (Name (Clause)) = Comp_Typ
                     then
                        if Chars (Clause) = Name_Read then
                           Found_R := True;
                        elsif  Chars (Clause) = Name_Write then
                           Found_W := True;
                        end if;
                     end if;

                     Clause := Next (Clause);
                  end loop;

                  --  Both read and write have to be defined

                  if not Found_R
                    or else not Found_W
                  then
                     Error_Msg_N
                     ("invalid component type in remote types unit",
                       Comp_Itm);
                  end if;
               end if;
               Comp_Itm := Next (Comp_Itm);
            end loop;
         end if;

         <<Next_Type>>
            Ty_Decl := Next (Ty_Decl);
      end loop;

   end Validate_RT_RAT_Component;

   -----------------------------------------
   -- Validate_SP_Access_Object_Type_Decl --
   -----------------------------------------

   procedure Validate_SP_Access_Object_Type_Decl (T : Entity_Id) is
      Direct_Designated_Type : Entity_Id;

      function Has_Entry_Declarations (E : Entity_Id) return Boolean;
      --  Return true if the protected type designated by T has
      --  entry declarations.

      function Has_Entry_Declarations (E : Entity_Id) return Boolean is
         Ety : Entity_Id;

      begin
         if Nkind (Parent (E)) = N_Protected_Type_Declaration then
            Ety := First_Entity (E);
            while Present (Ety) loop
               if Ekind (Ety) = E_Entry then
                  return True;
               end if;

               Ety := Next (Ety);
            end loop;
         end if;

         return False;
      end Has_Entry_Declarations;

   --  Start of processing for Validate_SP_Access_Object_Type_Decl

   begin
      --  We are called from Sem_Ch3.Analyze_Type_Declaration, and the
      --  Nkind of the given entity is N_Access_To_Object_Definition.

      if not Comes_From_Source (T)
        or else not In_Shared_Passive_Unit
        or else In_Subprogram_Task_Protected_Unit
      then
         return;
      end if;

      --  Check Shared Passive unit. It should not contain the declaration
      --  of an access-to-object type whose designated type is a class-wide
      --  type, task type or protected type with entry (RM E.2.1(7)).

      Direct_Designated_Type := Designated_Type (T);

      if Ekind (Direct_Designated_Type) = E_Class_Wide_Type then
         Error_Msg_N
           ("invalid access-to-class-wide type in shared passive unit", T);
         return;

      elsif Ekind (Direct_Designated_Type) in Task_Kind then
         Error_Msg_N
           ("invalid access-to-task type in shared passive unit", T);
         return;

      elsif Ekind (Direct_Designated_Type) in Protected_Kind
        and then Has_Entry_Declarations (Direct_Designated_Type)
      then
         Error_Msg_N
           ("invalid access-to-protected type in shared passive unit", T);
         return;
      end if;
   end Validate_SP_Access_Object_Type_Decl;

   ---------------------------------
   -- Validate_Static_Object_Name --
   ---------------------------------

   procedure Validate_Static_Object_Name (N : Node_Id) is
      E : Entity_Id;

      function Is_Primary (N : Node_Id) return Boolean;
      --  Determine whether node is syntactically a primary in an expression.

      function Is_Primary (N : Node_Id) return Boolean is
         K : constant Node_Kind := Nkind (Parent (N));

      begin
         case K is

            when N_Op | N_In | N_Not_In =>
               return True;

            when N_Aggregate | N_Index_Or_Discriminant_Constraint =>
               return True;

            when N_Attribute_Reference =>
               return Attribute_Name (Parent (N)) /= Name_Address
                 and then Attribute_Name (Parent (N)) /= Name_Access
                 and then Attribute_Name (Parent (N)) /= Name_Unchecked_Access
                 and then
                   Attribute_Name (Parent (N)) /= Name_Unrestricted_Access;

            when N_Indexed_Component =>
               return (N /= Prefix (Parent (N))
                 or else Is_Primary (Parent (N)));

            when N_Qualified_Expression | N_Type_Conversion =>
               return Is_Primary (Parent (N));

            when N_Assignment_Statement | N_Object_Declaration =>
               return (N = Expression (Parent (N)));

            when N_Selected_Component =>
               return Is_Primary (Parent (N));

            when others =>
               return False;
         end case;
      end Is_Primary;

   --  Start of processing for Validate_Static_Object_Name

   begin

      if not In_Preelaborated_Unit
        or else not Comes_From_Source (N)
        or else In_Subprogram_Or_Concurrent_Unit
      then
         return;

      --  Filter out cases where primary is default in a component
      --  declaration, discriminant specification, or actual in a record
      --  type initialization call.

      --  Initialization call of internal types.

      elsif Nkind (Parent (N)) = N_Procedure_Call_Statement then

         if Present (Parent (Parent (N)))
           and then Nkind (Parent (Parent (N))) = N_Freeze_Entity
         then
            return;
         end if;

         if Nkind (Name (Parent (N))) = N_Identifier
           and then not Comes_From_Source (Entity (Name (Parent (N))))
         then
            return;
         end if;
      end if;

      --  Error if the name is a primary in an expression. The parent must not
      --  be an operator, or a selected component or an indexed component that
      --  is itself a primary. Entities that are actuals do not need to be
      --  checked, because the call itself will be diagnosed.

      if Is_Primary (N) then
         if Ekind (Entity (N)) = E_Variable then
            Error_Msg_N ("non-static object name in preelaborated unit", N);

         --  We take the view that a constant defined in another preelaborated
         --  unit is preelaborable, even though it may have a private type and
         --  thus appear non-static in a client. This must be the intent of
         --  the language, but currently is an RM gap.

         elsif Ekind (Entity (N)) = E_Constant
           and then not Is_Static_Expression (N)
         then
            E := Entity (N);

            if Enclosing_Lib_Unit_Node (N) /= Enclosing_Lib_Unit_Node (E)
              and then (Is_Preelaborated (Scope (E))
                or else Is_Pure (Scope (E))
                or else (Present (Renamed_Object (E))
                  and then Is_Entity_Name (Renamed_Object (E))
                  and then (Is_Preelaborated (Scope (Renamed_Object (E)))
                             or else Is_Pure (Scope (Renamed_Object (E))))))
            then
               null;
            else
               Error_Msg_N ("non-static constant in preelaborated unit", N);
            end if;
         end if;
      end if;
   end Validate_Static_Object_Name;

end Sem_Dist;