This file is indexed.

/usr/share/ada/adainclude/gnatprj/prj-part.adb is in libgnatprj6-dev 6.4.0-17ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             P R J . P A R T                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 2001-2014, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Atree;    use Atree;
with Err_Vars; use Err_Vars;
with Opt;      use Opt;
with Osint;    use Osint;
with Output;   use Output;
with Prj.Com;  use Prj.Com;
with Prj.Dect;
with Prj.Env;  use Prj.Env;
with Prj.Err;  use Prj.Err;
with Sinput;   use Sinput;
with Sinput.P; use Sinput.P;
with Snames;
with Table;

with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Exceptions;          use Ada.Exceptions;

with GNAT.HTable;               use GNAT.HTable;

package body Prj.Part is

   Buffer      : String_Access;
   Buffer_Last : Natural := 0;

   Dir_Sep : Character renames GNAT.OS_Lib.Directory_Separator;

   ------------------------------------
   -- Local Packages and Subprograms --
   ------------------------------------

   type With_Id is new Nat;
   No_With : constant With_Id := 0;

   type With_Record is record
      Path         : Path_Name_Type;
      Location     : Source_Ptr;
      Limited_With : Boolean;
      Node         : Project_Node_Id;
      Next         : With_Id;
   end record;
   --  Information about an imported project, to be put in table Withs below

   package Withs is new Table.Table
     (Table_Component_Type => With_Record,
      Table_Index_Type     => With_Id,
      Table_Low_Bound      => 1,
      Table_Initial        => 10,
      Table_Increment      => 100,
      Table_Name           => "Prj.Part.Withs");
   --  Table used to store temporarily paths and locations of imported
   --  projects. These imported projects will be effectively parsed later: just
   --  before parsing the current project for the non limited withed projects,
   --  after getting its name; after complete parsing of the current project
   --  for the limited withed projects.

   type Names_And_Id is record
      Path_Name           : Path_Name_Type;
      Canonical_Path_Name : Path_Name_Type;
      Id                  : Project_Node_Id;
      Limited_With        : Boolean;
   end record;

   package Project_Stack is new Table.Table
     (Table_Component_Type => Names_And_Id,
      Table_Index_Type     => Nat,
      Table_Low_Bound      => 1,
      Table_Initial        => 10,
      Table_Increment      => 100,
      Table_Name           => "Prj.Part.Project_Stack");
   --  This table is used to detect circular dependencies
   --  for imported and extended projects and to get the project ids of
   --  limited imported projects when there is a circularity with at least
   --  one limited imported project file.

   package Virtual_Hash is new GNAT.HTable.Simple_HTable
     (Header_Num => Header_Num,
      Element    => Project_Node_Id,
      No_Element => Project_Node_High_Bound,
      Key        => Project_Node_Id,
      Hash       => Prj.Tree.Hash,
      Equal      => "=");
   --  Hash table to store the node ids of projects for which a virtual
   --  extending project need to be created. The corresponding value is the
   --  head of a list of WITH clauses corresponding to the context of the
   --  enclosing EXTEND ALL projects. Note: Default_Element is Project_Node_
   --  High_Bound because we want Empty_Node to be a possible value.

   package Processed_Hash is new GNAT.HTable.Simple_HTable
     (Header_Num => Header_Num,
      Element    => Boolean,
      No_Element => False,
      Key        => Project_Node_Id,
      Hash       => Prj.Tree.Hash,
      Equal      => "=");
   --  Hash table to store the project process when looking for project that
   --  need to have a virtual extending project, to avoid processing the same
   --  project twice.

   function Has_Circular_Dependencies
     (Flags               : Processing_Flags;
      Normed_Path_Name    : Path_Name_Type;
      Canonical_Path_Name : Path_Name_Type) return Boolean;
   --  Check for a circular dependency in the loaded project.
   --  Generates an error message in such a case.

   procedure Read_Project_Qualifier
     (Flags              : Processing_Flags;
      In_Tree            : Project_Node_Tree_Ref;
      Is_Config_File     : Boolean;
      Qualifier_Location : out Source_Ptr;
      Project            : Project_Node_Id);
   --  Check if there is a qualifier before the reserved word "project"

   --  Hash table to cache project path to avoid looking for them on the path

   procedure Check_Extending_All_Imports
     (Flags : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id);
   --  Check that a non extending-all project does not import an
   --  extending-all project.

   procedure Check_Aggregate_Imports
     (Flags   : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id);
   --  Check that an aggregate project only imports abstract projects

   procedure Check_Import_Aggregate
     (Flags   : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id);
   --  Check that a non aggregate project does not import an aggregate project

   procedure Create_Virtual_Extending_Project
     (For_Project     : Project_Node_Id;
      Main_Project    : Project_Node_Id;
      Extension_Withs : Project_Node_Id;
      In_Tree         : Project_Node_Tree_Ref);
   --  Create a virtual extending project of For_Project. Main_Project is
   --  the extending all project. Extension_Withs is the head of a WITH clause
   --  list to be added to the created virtual project.
   --
   --  The String_Value_Of is not set for the automatically added with
   --  clause and keeps the default value of No_Name. This enables Prj.PP
   --  to skip these automatically added with clauses to be processed.

   procedure Look_For_Virtual_Projects_For
     (Proj                : Project_Node_Id;
      In_Tree             : Project_Node_Tree_Ref;
      Potentially_Virtual : Boolean);
   --  Look for projects that need to have a virtual extending project.
   --  This procedure is recursive. If called with Potentially_Virtual set to
   --  True, then Proj may need an virtual extending project; otherwise it
   --  does not (because it is already extended), but other projects that it
   --  imports may need to be virtually extended.

   type Extension_Origin is (None, Extending_Simple, Extending_All);
   --  Type of parameter From_Extended for procedures Parse_Single_Project and
   --  Post_Parse_Context_Clause. Extending_All means that we are parsing the
   --  tree rooted at an extending all project.

   procedure Parse_Single_Project
     (In_Tree           : Project_Node_Tree_Ref;
      Project           : out Project_Node_Id;
      Extends_All       : out Boolean;
      Path_Name_Id      : Path_Name_Type;
      Extended          : Boolean;
      From_Extended     : Extension_Origin;
      In_Limited        : Boolean;
      Packages_To_Check : String_List_Access;
      Depth             : Natural;
      Current_Dir       : String;
      Is_Config_File    : Boolean;
      Env               : in out Environment;
      Implicit_Project  : Boolean := False);
   --  Parse a project file. This is a recursive procedure: it calls itself for
   --  imported and extended projects. When From_Extended is not None, if the
   --  project has already been parsed and is an extended project A, return the
   --  ultimate (not extended) project that extends A. When In_Limited is True,
   --  the importing path includes at least one "limited with". When parsing
   --  configuration projects, do not allow a depth > 1.
   --
   --  Is_Config_File should be set to True if the project represents a config
   --  file (.cgpr) since some specific checks apply.
   --
   --  If Implicit_Project is True, change the Directory of the project node
   --  to be the Current_Dir. Recursive calls to Parse_Single_Project are
   --  always done with the default False value for Implicit_Project.

   procedure Pre_Parse_Context_Clause
     (In_Tree        : Project_Node_Tree_Ref;
      Context_Clause : out With_Id;
      Is_Config_File : Boolean;
      Flags          : Processing_Flags);
   --  Parse the context clause of a project. Store the paths and locations of
   --  the imported projects in table Withs. Does nothing if there is no
   --  context clause (if the current token is not "with" or "limited" followed
   --  by "with").
   --  Is_Config_File should be set to True if the project represents a config
   --  file (.cgpr) since some specific checks apply.

   procedure Post_Parse_Context_Clause
     (Context_Clause    : With_Id;
      In_Tree           : Project_Node_Tree_Ref;
      In_Limited        : Boolean;
      Limited_Withs     : Boolean;
      Imported_Projects : in out Project_Node_Id;
      Project_Directory : Path_Name_Type;
      From_Extended     : Extension_Origin;
      Packages_To_Check : String_List_Access;
      Depth             : Natural;
      Current_Dir       : String;
      Is_Config_File    : Boolean;
      Env               : in out Environment);
   --  Parse the imported projects that have been stored in table Withs, if
   --  any. From_Extended is used for the call to Parse_Single_Project below.
   --
   --  When In_Limited is True, the importing path includes at least one
   --  "limited with". When Limited_Withs is False, only non limited withed
   --  projects are parsed. When Limited_Withs is True, only limited withed
   --  projects are parsed.
   --
   --  Is_Config_File should be set to True if the project represents a config
   --  file (.cgpr) since some specific checks apply.

   function Project_Name_From
     (Path_Name      : String;
      Is_Config_File : Boolean) return Name_Id;
   --  Returns the name of the project that corresponds to its path name.
   --  Returns No_Name if the path name is invalid, because the corresponding
   --  project name does not have the syntax of an ada identifier.

   function Copy_With_Clause
     (With_Clause : Project_Node_Id;
      In_Tree     : Project_Node_Tree_Ref;
      Next_Clause : Project_Node_Id) return Project_Node_Id;
   --  Return a copy of With_Clause in In_Tree, whose Next_With_Clause is the
   --  indicated one.

   ----------------------
   -- Copy_With_Clause --
   ----------------------

   function Copy_With_Clause
     (With_Clause : Project_Node_Id;
      In_Tree     : Project_Node_Tree_Ref;
      Next_Clause : Project_Node_Id) return Project_Node_Id
   is
      New_With_Clause : constant Project_Node_Id :=
                          Default_Project_Node (In_Tree, N_With_Clause);
   begin
      Set_Name_Of (New_With_Clause, In_Tree,
        Name_Of (With_Clause, In_Tree));
      Set_Path_Name_Of (New_With_Clause, In_Tree,
        Path_Name_Of (With_Clause, In_Tree));
      Set_Project_Node_Of (New_With_Clause, In_Tree,
        Project_Node_Of (With_Clause, In_Tree));
      Set_Next_With_Clause_Of (New_With_Clause, In_Tree, Next_Clause);

      return New_With_Clause;
   end Copy_With_Clause;

   --------------------------------------
   -- Create_Virtual_Extending_Project --
   --------------------------------------

   procedure Create_Virtual_Extending_Project
     (For_Project     : Project_Node_Id;
      Main_Project    : Project_Node_Id;
      Extension_Withs : Project_Node_Id;
      In_Tree         : Project_Node_Tree_Ref)
   is

      Virtual_Name : constant String :=
                       Virtual_Prefix &
                         Get_Name_String (Name_Of (For_Project, In_Tree));
      --  The name of the virtual extending project

      Virtual_Name_Id : Name_Id;
      --  Virtual extending project name id

      Virtual_Path_Id : Path_Name_Type;
      --  Fake path name of the virtual extending project. The directory is
      --  the same directory as the extending all project.

      --  The source of the virtual extending project is something like:

      --  project V$<project name> extends <project path> is

      --     for Source_Dirs use ();

      --  end V$<project name>;

      --  The project directory cannot be specified during parsing; it will be
      --  put directly in the virtual extending project data during processing.

      --  Nodes that made up the virtual extending project

      Virtual_Project         : Project_Node_Id;
      With_Clause             : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_With_Clause);
      Project_Declaration     : Project_Node_Id;
      Source_Dirs_Declaration : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_Declarative_Item);
      Source_Dirs_Attribute   : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_Attribute_Declaration, List);
      Source_Dirs_Expression  : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_Expression, List);
      Source_Dirs_Term        : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_Term, List);
      Source_Dirs_List        : constant Project_Node_Id :=
                                  Default_Project_Node
                                    (In_Tree, N_Literal_String_List, List);

   begin
      --  Get the virtual path name

      Get_Name_String (Path_Name_Of (Main_Project, In_Tree));

      while Name_Len > 0
        and then not Is_Directory_Separator (Name_Buffer (Name_Len))
      loop
         Name_Len := Name_Len - 1;
      end loop;

      Name_Buffer (Name_Len + 1 .. Name_Len + Virtual_Name'Length) :=
        Virtual_Name;
      Name_Len := Name_Len + Virtual_Name'Length;
      Virtual_Path_Id := Name_Find;

      --  Get the virtual name id

      Name_Len := Virtual_Name'Length;
      Name_Buffer (1 .. Name_Len) := Virtual_Name;
      Virtual_Name_Id := Name_Find;

      Virtual_Project := Create_Project
        (In_Tree        => In_Tree,
         Name           => Virtual_Name_Id,
         Full_Path      => Virtual_Path_Id,
         Is_Config_File => False);

      Project_Declaration := Project_Declaration_Of (Virtual_Project, In_Tree);

      --  Add a WITH clause to the main project to import the newly created
      --  virtual extending project.

      Set_Name_Of (With_Clause, In_Tree, Virtual_Name_Id);
      Set_Path_Name_Of (With_Clause, In_Tree, Virtual_Path_Id);
      Set_Project_Node_Of (With_Clause, In_Tree, Virtual_Project);
      Set_Next_With_Clause_Of
        (With_Clause, In_Tree, First_With_Clause_Of (Main_Project, In_Tree));
      Set_First_With_Clause_Of (Main_Project, In_Tree, With_Clause);

      --  Copy with clauses for projects imported by the extending-all project

      declare
         Org_With_Clause : Project_Node_Id := Extension_Withs;
         New_With_Clause : Project_Node_Id := Empty_Node;

      begin
         while Present (Org_With_Clause) loop
            New_With_Clause :=
              Copy_With_Clause (Org_With_Clause, In_Tree, New_With_Clause);

            Org_With_Clause := Next_With_Clause_Of (Org_With_Clause, In_Tree);
         end loop;

         Set_First_With_Clause_Of (Virtual_Project, In_Tree, New_With_Clause);
      end;

      --  Virtual project node

      Set_Location_Of
        (Virtual_Project, In_Tree, Location_Of (Main_Project, In_Tree));
      Set_Extended_Project_Path_Of
        (Virtual_Project, In_Tree, Path_Name_Of (For_Project, In_Tree));

      --  Project declaration

      Set_First_Declarative_Item_Of
        (Project_Declaration, In_Tree, Source_Dirs_Declaration);
      Set_Extended_Project_Of (Project_Declaration, In_Tree, For_Project);

      --  Source_Dirs declaration

      Set_Current_Item_Node
        (Source_Dirs_Declaration, In_Tree, Source_Dirs_Attribute);

      --  Source_Dirs attribute

      Set_Name_Of (Source_Dirs_Attribute, In_Tree, Snames.Name_Source_Dirs);
      Set_Expression_Of
        (Source_Dirs_Attribute, In_Tree, Source_Dirs_Expression);

      --  Source_Dirs expression

      Set_First_Term (Source_Dirs_Expression, In_Tree, Source_Dirs_Term);

      --  Source_Dirs term

      Set_Current_Term (Source_Dirs_Term, In_Tree, Source_Dirs_List);

      --  Source_Dirs empty list: nothing to do
   end Create_Virtual_Extending_Project;

   -----------------------------------
   -- Look_For_Virtual_Projects_For --
   -----------------------------------

   Extension_Withs : Project_Node_Id;
   --  Head of the current EXTENDS ALL imports list. When creating virtual
   --  projects for an EXTENDS ALL, we import in each virtual project all
   --  of the projects that appear in WITH clauses of the extending projects.
   --  This ensures that virtual projects share a consistent environment (in
   --  particular if a project imported by one of the extending projects
   --  replaces some runtime units).

   procedure Look_For_Virtual_Projects_For
     (Proj                : Project_Node_Id;
      In_Tree             : Project_Node_Tree_Ref;
      Potentially_Virtual : Boolean)
   is
      Declaration : Project_Node_Id := Empty_Node;
      --  Node for the project declaration of Proj

      With_Clause : Project_Node_Id := Empty_Node;
      --  Node for a with clause of Proj

      Imported : Project_Node_Id := Empty_Node;
      --  Node for a project imported by Proj

      Extended : Project_Node_Id := Empty_Node;
      --  Node for the eventual project extended by Proj

      Extends_All : Boolean := False;
      --  Set True if Proj is an EXTENDS ALL project

      Saved_Extension_Withs : constant Project_Node_Id := Extension_Withs;

   begin
      --  Nothing to do if Proj is undefined or has already been processed

      if Present (Proj) and then not Processed_Hash.Get (Proj) then

         --  Make sure the project will not be processed again

         Processed_Hash.Set (Proj, True);

         Declaration := Project_Declaration_Of (Proj, In_Tree);

         if Present (Declaration) then
            Extended := Extended_Project_Of (Declaration, In_Tree);
            Extends_All := Is_Extending_All (Proj, In_Tree);
         end if;

         --  If this is a project that may need a virtual extending project
         --  and it is not itself an extending project, put it in the list.

         if Potentially_Virtual and then No (Extended) then
            Virtual_Hash.Set (Proj, Extension_Withs);
         end if;

         --  Now check the projects it imports

         With_Clause := First_With_Clause_Of (Proj, In_Tree);
         while Present (With_Clause) loop
            Imported := Project_Node_Of (With_Clause, In_Tree);

            if Present (Imported) then
               Look_For_Virtual_Projects_For
                 (Imported, In_Tree, Potentially_Virtual => True);
            end if;

            if Extends_All then

               --  This is an EXTENDS ALL project: prepend each of its WITH
               --  clauses to the currently active list of extension deps.

               Extension_Withs :=
                 Copy_With_Clause (With_Clause, In_Tree, Extension_Withs);
            end if;

            With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
         end loop;

         --  Check also the eventual project extended by Proj. As this project
         --  is already extended, call recursively with Potentially_Virtual
         --  being False.

         Look_For_Virtual_Projects_For
           (Extended, In_Tree, Potentially_Virtual => False);

         Extension_Withs := Saved_Extension_Withs;
      end if;
   end Look_For_Virtual_Projects_For;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (In_Tree           : Project_Node_Tree_Ref;
      Project           : out Project_Node_Id;
      Project_File_Name : String;
      Errout_Handling   : Errout_Mode := Always_Finalize;
      Packages_To_Check : String_List_Access;
      Store_Comments    : Boolean := False;
      Current_Directory : String := "";
      Is_Config_File    : Boolean;
      Env               : in out Prj.Tree.Environment;
      Target_Name       : String := "";
      Implicit_Project  : Boolean := False)
   is
      Dummy : Boolean;
      pragma Warnings (Off, Dummy);

      Real_Project_File_Name : String_Access :=
                                 Osint.To_Canonical_File_Spec
                                   (Project_File_Name);
      Path_Name_Id : Path_Name_Type;

   begin
      In_Tree.Incomplete_With := False;
      Project_Stack.Init;
      Tree_Private_Part.Projects_Htable.Reset (In_Tree.Projects_HT);

      if not Is_Initialized (Env.Project_Path) then
         Prj.Env.Initialize_Default_Project_Path
           (Env.Project_Path, Target_Name);
      end if;

      if Real_Project_File_Name = null then
         Real_Project_File_Name := new String'(Project_File_Name);
      end if;

      Project := Empty_Node;

      Find_Project (Env.Project_Path,
                    Project_File_Name => Real_Project_File_Name.all,
                    Directory         => Current_Directory,
                    Path              => Path_Name_Id);
      Free (Real_Project_File_Name);

      if Errout_Handling /= Never_Finalize then
         Prj.Err.Initialize;
      end if;

      Prj.Err.Scanner.Set_Comment_As_Token (Store_Comments);
      Prj.Err.Scanner.Set_End_Of_Line_As_Token (Store_Comments);

      if Path_Name_Id = No_Path then
         declare
            P : String_Access;
         begin
            Get_Path (Env.Project_Path, Path => P);

            Prj.Com.Fail
              ("project file """
               & Project_File_Name
               & """ not found in "
               & P.all);
            Project := Empty_Node;
            return;
         end;
      end if;

      --  Parse the main project file

      begin
         Parse_Single_Project
           (In_Tree           => In_Tree,
            Project           => Project,
            Extends_All       => Dummy,
            Path_Name_Id      => Path_Name_Id,
            Extended          => False,
            From_Extended     => None,
            In_Limited        => False,
            Packages_To_Check => Packages_To_Check,
            Depth             => 0,
            Current_Dir       => Current_Directory,
            Is_Config_File    => Is_Config_File,
            Env               => Env,
            Implicit_Project  => Implicit_Project);

      exception
         when Types.Unrecoverable_Error =>

            --  Unrecoverable_Error is raised when a line is too long.
            --  A meaningful error message will be displayed later.

            Project := Empty_Node;
      end;

      --  If Project is an extending-all project, create the eventual
      --  virtual extending projects and check that there are no illegally
      --  imported projects.

      if Present (Project)
        and then Is_Extending_All (Project, In_Tree)
      then
         --  First look for projects that potentially need a virtual
         --  extending project.

         Virtual_Hash.Reset;
         Processed_Hash.Reset;

         --  Mark the extending all project as processed, to avoid checking
         --  the imported projects in case of a "limited with" on this
         --  extending all project.

         Processed_Hash.Set (Project, True);

         declare
            Declaration : constant Project_Node_Id :=
                            Project_Declaration_Of (Project, In_Tree);
         begin
            Extension_Withs := First_With_Clause_Of (Project, In_Tree);
            Look_For_Virtual_Projects_For
              (Extended_Project_Of (Declaration, In_Tree), In_Tree,
               Potentially_Virtual => False);
         end;

         --  Now, check the projects directly imported by the main project.
         --  Remove from the potentially virtual any project extended by one
         --  of these imported projects.

         declare
            With_Clause : Project_Node_Id;
            Imported    : Project_Node_Id := Empty_Node;
            Declaration : Project_Node_Id := Empty_Node;

         begin
            With_Clause := First_With_Clause_Of (Project, In_Tree);
            while Present (With_Clause) loop
               Imported := Project_Node_Of (With_Clause, In_Tree);

               if Present (Imported) then
                  Declaration := Project_Declaration_Of (Imported, In_Tree);

                  if Extended_Project_Of (Declaration, In_Tree) /=
                    Empty_Node
                  then
                     loop
                        Imported :=
                          Extended_Project_Of (Declaration, In_Tree);
                        exit when No (Imported);
                        Virtual_Hash.Remove (Imported);
                        Declaration :=
                          Project_Declaration_Of (Imported, In_Tree);
                     end loop;
                  end if;
               end if;

               With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
            end loop;
         end;

         --  Now create all the virtual extending projects

         declare
            Proj  : Project_Node_Id := Empty_Node;
            Withs : Project_Node_Id;
         begin
            Virtual_Hash.Get_First (Proj, Withs);
            while Withs /= Project_Node_High_Bound loop
               Create_Virtual_Extending_Project
                 (Proj, Project, Withs, In_Tree);
               Virtual_Hash.Get_Next (Proj, Withs);
            end loop;
         end;
      end if;

      --  If there were any kind of error during the parsing, serious
      --  or not, then the parsing fails.

      if Total_Errors_Detected > 0 then
         Project := Empty_Node;
      end if;

      case Errout_Handling is
         when Always_Finalize =>
            Prj.Err.Finalize;

            --  Reinitialize to avoid duplicate warnings later on
            Prj.Err.Initialize;

         when Finalize_If_Error =>
            if No (Project) then
               Prj.Err.Finalize;
               Prj.Err.Initialize;
            end if;

         when Never_Finalize =>
            null;
      end case;

   exception
      when X : others =>

         --  Internal error

         Write_Line (Exception_Information (X));
         Write_Str  ("Exception ");
         Write_Str  (Exception_Name (X));
         Write_Line (" raised, while processing project file");
         Project := Empty_Node;
   end Parse;

   ------------------------------
   -- Pre_Parse_Context_Clause --
   ------------------------------

   procedure Pre_Parse_Context_Clause
     (In_Tree        : Project_Node_Tree_Ref;
      Context_Clause : out With_Id;
      Is_Config_File : Boolean;
      Flags          : Processing_Flags)
   is
      Current_With_Clause : With_Id := No_With;
      Limited_With        : Boolean := False;
      Current_With        : With_Record;
      Current_With_Node   : Project_Node_Id := Empty_Node;

   begin
      --  Assume no context clause

      Context_Clause := No_With;
      With_Loop :

      --  If Token is not WITH or LIMITED, there is no context clause, or we
      --  have exhausted the with clauses.

      while Token = Tok_With or else Token = Tok_Limited loop
         Current_With_Node :=
           Default_Project_Node (Of_Kind => N_With_Clause, In_Tree => In_Tree);
         Limited_With := Token = Tok_Limited;

         if Is_Config_File then
            Error_Msg
              (Flags,
               "configuration project cannot import " &
               "other configuration projects",
               Token_Ptr);
         end if;

         if Limited_With then
            Scan (In_Tree);  --  past LIMITED
            Expect (Tok_With, "WITH");
            exit With_Loop when Token /= Tok_With;
         end if;

         Comma_Loop :
         loop
            Scan (In_Tree); -- past WITH or ","

            Expect (Tok_String_Literal, "literal string");

            if Token /= Tok_String_Literal then
               return;
            end if;

            --  Store path and location in table Withs

            Current_With :=
              (Path         => Path_Name_Type (Token_Name),
               Location     => Token_Ptr,
               Limited_With => Limited_With,
               Node         => Current_With_Node,
               Next         => No_With);

            Withs.Increment_Last;
            Withs.Table (Withs.Last) := Current_With;

            if Current_With_Clause = No_With then
               Context_Clause := Withs.Last;

            else
               Withs.Table (Current_With_Clause).Next := Withs.Last;
            end if;

            Current_With_Clause := Withs.Last;

            Scan (In_Tree);

            if Token = Tok_Semicolon then
               Set_End_Of_Line (Current_With_Node);
               Set_Previous_Line_Node (Current_With_Node);

               --  End of (possibly multiple) with clause;

               Scan (In_Tree); -- past semicolon
               exit Comma_Loop;

            elsif Token = Tok_Comma then
               Set_Is_Not_Last_In_List (Current_With_Node, In_Tree);

            else
               Error_Msg (Flags, "expected comma or semi colon", Token_Ptr);
               exit Comma_Loop;
            end if;

            Current_With_Node :=
              Default_Project_Node
                (Of_Kind => N_With_Clause, In_Tree => In_Tree);
         end loop Comma_Loop;
      end loop With_Loop;
   end Pre_Parse_Context_Clause;

   -------------------------------
   -- Post_Parse_Context_Clause --
   -------------------------------

   procedure Post_Parse_Context_Clause
     (Context_Clause    : With_Id;
      In_Tree           : Project_Node_Tree_Ref;
      In_Limited        : Boolean;
      Limited_Withs     : Boolean;
      Imported_Projects : in out Project_Node_Id;
      Project_Directory : Path_Name_Type;
      From_Extended     : Extension_Origin;
      Packages_To_Check : String_List_Access;
      Depth             : Natural;
      Current_Dir       : String;
      Is_Config_File    : Boolean;
      Env               : in out Environment)
   is
      Current_With_Clause : With_Id := Context_Clause;

      Current_Project  : Project_Node_Id := Imported_Projects;
      Previous_Project : Project_Node_Id := Empty_Node;
      Next_Project     : Project_Node_Id := Empty_Node;

      Project_Directory_Path : constant String :=
                                 Get_Name_String (Project_Directory);

      Current_With : With_Record;
      Extends_All  : Boolean := False;
      Imported_Path_Name_Id : Path_Name_Type;

   begin
      --  Set Current_Project to the last project in the current list, if the
      --  list is not empty.

      if Present (Current_Project) then
         while
           Present (Next_With_Clause_Of (Current_Project, In_Tree))
         loop
            Current_Project := Next_With_Clause_Of (Current_Project, In_Tree);
         end loop;
      end if;

      while Current_With_Clause /= No_With loop
         Current_With := Withs.Table (Current_With_Clause);
         Current_With_Clause := Current_With.Next;

         if Limited_Withs = Current_With.Limited_With then
            Find_Project
              (Env.Project_Path,
               Project_File_Name => Get_Name_String (Current_With.Path),
               Directory         => Project_Directory_Path,
               Path              => Imported_Path_Name_Id);

            if Imported_Path_Name_Id = No_Path then
               if Env.Flags.Ignore_Missing_With then
                  In_Tree.Incomplete_With := True;
                  Env.Flags.Incomplete_Withs := True;

               else
                  --  The project file cannot be found

                  Error_Msg_File_1 := File_Name_Type (Current_With.Path);
                  Error_Msg
                    (Env.Flags, "unknown project file: {",
                     Current_With.Location);

                  --  If this is not imported by the main project file, display
                  --  the import path.

                  if Project_Stack.Last > 1 then
                     for Index in reverse 1 .. Project_Stack.Last loop
                        Error_Msg_File_1 :=
                          File_Name_Type
                            (Project_Stack.Table (Index).Path_Name);
                        Error_Msg
                          (Env.Flags, "\imported by {", Current_With.Location);
                     end loop;
                  end if;
               end if;

            else
               --  New with clause

               declare
                  Resolved_Path : constant String :=
                                 Normalize_Pathname
                                   (Get_Name_String (Imported_Path_Name_Id),
                                    Directory      => Current_Dir,
                                    Resolve_Links  =>
                                      Opt.Follow_Links_For_Files,
                                    Case_Sensitive => True);

                  Withed_Project : Project_Node_Id := Empty_Node;

               begin
                  Previous_Project := Current_Project;

                  if No (Current_Project) then

                     --  First with clause of the context clause

                     Current_Project := Current_With.Node;
                     Imported_Projects := Current_Project;

                  else
                     Next_Project := Current_With.Node;
                     Set_Next_With_Clause_Of
                       (Current_Project, In_Tree, Next_Project);
                     Current_Project := Next_Project;
                  end if;

                  Set_String_Value_Of
                    (Current_Project,
                     In_Tree,
                     Name_Id (Current_With.Path));
                  Set_Location_Of
                    (Current_Project, In_Tree, Current_With.Location);

                  --  If it is a limited with, check if we have a circularity.
                  --  If we have one, get the project id of the limited
                  --  imported project file, and do not parse it.

                  if (In_Limited or Limited_Withs)
                    and then Project_Stack.Last > 1
                  then
                     declare
                        Canonical_Path_Name : Path_Name_Type;

                     begin
                        Name_Len := Resolved_Path'Length;
                        Name_Buffer (1 .. Name_Len) := Resolved_Path;
                        Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
                        Canonical_Path_Name := Name_Find;

                        for Index in 1 .. Project_Stack.Last loop
                           if Project_Stack.Table (Index).Canonical_Path_Name =
                             Canonical_Path_Name
                           then
                              --  We have found the limited imported project,
                              --  get its project id, and do not parse it.

                              Withed_Project := Project_Stack.Table (Index).Id;
                              exit;
                           end if;
                        end loop;
                     end;
                  end if;

                  --  Parse the imported project if its project id is unknown

                  if No (Withed_Project) then
                     Parse_Single_Project
                       (In_Tree           => In_Tree,
                        Project           => Withed_Project,
                        Extends_All       => Extends_All,
                        Path_Name_Id      => Imported_Path_Name_Id,
                        Extended          => False,
                        From_Extended     => From_Extended,
                        In_Limited        => In_Limited or Limited_Withs,
                        Packages_To_Check => Packages_To_Check,
                        Depth             => Depth,
                        Current_Dir       => Current_Dir,
                        Is_Config_File    => Is_Config_File,
                        Env               => Env);

                  else
                     Extends_All := Is_Extending_All (Withed_Project, In_Tree);
                  end if;

                  if No (Withed_Project) then

                     --  If parsing unsuccessful, remove the context clause

                     Current_Project := Previous_Project;

                     if No (Current_Project) then
                        Imported_Projects := Empty_Node;

                     else
                        Set_Next_With_Clause_Of
                          (Current_Project, In_Tree, Empty_Node);
                     end if;
                  else
                     --  If parsing was successful, record project name and
                     --  path name in with clause

                     Set_Project_Node_Of
                       (Node         => Current_Project,
                        In_Tree      => In_Tree,
                        To           => Withed_Project,
                        Limited_With => Current_With.Limited_With);
                     Set_Name_Of
                       (Current_Project,
                        In_Tree,
                        Name_Of (Withed_Project, In_Tree));

                     Name_Len := Resolved_Path'Length;
                     Name_Buffer (1 .. Name_Len) := Resolved_Path;
                     Set_Path_Name_Of (Current_Project, In_Tree, Name_Find);

                     if Extends_All then
                        Set_Is_Extending_All (Current_Project, In_Tree);
                     end if;
                  end if;
               end;
            end if;
         end if;
      end loop;
   end Post_Parse_Context_Clause;

   ---------------------------------
   -- Check_Extending_All_Imports --
   ---------------------------------

   procedure Check_Extending_All_Imports
     (Flags   : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id)
   is
      With_Clause : Project_Node_Id;
      Imported    : Project_Node_Id;

   begin
      if not Is_Extending_All (Project, In_Tree) then
         With_Clause := First_With_Clause_Of (Project, In_Tree);
         while Present (With_Clause) loop
            Imported := Project_Node_Of (With_Clause, In_Tree);

            if Is_Extending_All (With_Clause, In_Tree) then
               Error_Msg_Name_1 := Name_Of (Imported, In_Tree);
               Error_Msg (Flags, "cannot import extending-all project %%",
                          Token_Ptr);
               exit;
            end if;

            With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
         end loop;
      end if;
   end Check_Extending_All_Imports;

   -----------------------------
   -- Check_Aggregate_Imports --
   -----------------------------

   procedure Check_Aggregate_Imports
     (Flags   : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id)
   is
      With_Clause, Imported : Project_Node_Id;
   begin
      if Project_Qualifier_Of (Project, In_Tree) = Aggregate then
         With_Clause := First_With_Clause_Of (Project, In_Tree);

         while Present (With_Clause) loop
            Imported := Project_Node_Of (With_Clause, In_Tree);

            if Project_Qualifier_Of (Imported, In_Tree) /= Abstract_Project
            then
               Error_Msg_Name_1 := Name_Id (Path_Name_Of (Imported, In_Tree));
               Error_Msg (Flags, "can only import abstract projects, not %%",
                          Token_Ptr);
               exit;
            end if;

            With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
         end loop;
      end if;
   end Check_Aggregate_Imports;

   ----------------------------
   -- Check_Import_Aggregate --
   ----------------------------

   procedure Check_Import_Aggregate
     (Flags   : Processing_Flags;
      In_Tree : Project_Node_Tree_Ref;
      Project : Project_Node_Id)
   is
      With_Clause : Project_Node_Id;
      Imported    : Project_Node_Id;

   begin
      if Project_Qualifier_Of (Project, In_Tree) /= Aggregate then
         With_Clause := First_With_Clause_Of (Project, In_Tree);
         while Present (With_Clause) loop
            Imported := Project_Node_Of (With_Clause, In_Tree);

            if Project_Qualifier_Of (Imported, In_Tree) = Aggregate then
               Error_Msg_Name_1 := Name_Id (Path_Name_Of (Imported, In_Tree));
               Error_Msg
                 (Flags, "cannot import aggregate project %%", Token_Ptr);
               exit;
            end if;

            With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
         end loop;
      end if;
   end Check_Import_Aggregate;

   ----------------------------
   -- Read_Project_Qualifier --
   ----------------------------

   procedure Read_Project_Qualifier
     (Flags              : Processing_Flags;
      In_Tree            : Project_Node_Tree_Ref;
      Is_Config_File     : Boolean;
      Qualifier_Location : out Source_Ptr;
      Project            : Project_Node_Id)
   is
      Proj_Qualifier : Project_Qualifier := Unspecified;
   begin
      Qualifier_Location := Token_Ptr;

      if Token = Tok_Abstract then
         Proj_Qualifier := Abstract_Project;
         Scan (In_Tree);

      elsif Token = Tok_Identifier then
         case Token_Name is
            when Snames.Name_Standard =>
               Proj_Qualifier := Standard;
               Scan (In_Tree);

            when Snames.Name_Aggregate =>
               Proj_Qualifier := Aggregate;
               Scan (In_Tree);

               if Token = Tok_Identifier
                 and then Token_Name = Snames.Name_Library
               then
                  Proj_Qualifier := Aggregate_Library;
                  Scan (In_Tree);
               end if;

            when Snames.Name_Library =>
               Proj_Qualifier := Library;
               Scan (In_Tree);

            when Snames.Name_Configuration =>
               if not Is_Config_File then
                  Error_Msg
                    (Flags,
                     "configuration projects cannot belong to a user" &
                     " project tree",
                     Token_Ptr);
               end if;

               Proj_Qualifier := Configuration;
               Scan (In_Tree);

            when others =>
               null;
         end case;
      end if;

      if Is_Config_File and then Proj_Qualifier = Unspecified then

         --  Set the qualifier to Configuration, even if the token doesn't
         --  exist in the source file itself, so that we can differentiate
         --  project files and configuration files later on.

         Proj_Qualifier := Configuration;
      end if;

      if Proj_Qualifier /= Unspecified then
         if Is_Config_File
           and then Proj_Qualifier /= Configuration
         then
            Error_Msg (Flags,
                       "a configuration project cannot be qualified except " &
                       "as configuration project",
                       Qualifier_Location);
         end if;

         Set_Project_Qualifier_Of (Project, In_Tree, Proj_Qualifier);
      end if;
   end Read_Project_Qualifier;

   -------------------------------
   -- Has_Circular_Dependencies --
   -------------------------------

   function Has_Circular_Dependencies
     (Flags               : Processing_Flags;
      Normed_Path_Name    : Path_Name_Type;
      Canonical_Path_Name : Path_Name_Type) return Boolean is
   begin
      for Index in reverse 1 .. Project_Stack.Last loop
         exit when Project_Stack.Table (Index).Limited_With;

         if Canonical_Path_Name =
           Project_Stack.Table (Index).Canonical_Path_Name
         then
            Error_Msg (Flags, "circular dependency detected", Token_Ptr);
            Error_Msg_Name_1 := Name_Id (Normed_Path_Name);
            Error_Msg (Flags, "\  %% is imported by", Token_Ptr);

            for Current in reverse 1 .. Project_Stack.Last loop
               Error_Msg_Name_1 :=
                 Name_Id (Project_Stack.Table (Current).Path_Name);

               if Project_Stack.Table (Current).Canonical_Path_Name /=
                 Canonical_Path_Name
               then
                  Error_Msg
                    (Flags, "\  %% which itself is imported by", Token_Ptr);

               else
                  Error_Msg (Flags, "\  %%", Token_Ptr);
                  exit;
               end if;
            end loop;

            return True;
         end if;
      end loop;
      return False;
   end Has_Circular_Dependencies;

   --------------------------
   -- Parse_Single_Project --
   --------------------------

   procedure Parse_Single_Project
     (In_Tree           : Project_Node_Tree_Ref;
      Project           : out Project_Node_Id;
      Extends_All       : out Boolean;
      Path_Name_Id      : Path_Name_Type;
      Extended          : Boolean;
      From_Extended     : Extension_Origin;
      In_Limited        : Boolean;
      Packages_To_Check : String_List_Access;
      Depth             : Natural;
      Current_Dir       : String;
      Is_Config_File    : Boolean;
      Env               : in out Environment;
      Implicit_Project  : Boolean := False)
   is
      Path_Name : constant String := Get_Name_String (Path_Name_Id);

      Normed_Path_Name    : Path_Name_Type;
      Canonical_Path_Name : Path_Name_Type;
      Resolved_Path_Name  : Path_Name_Type;
      Project_Directory   : Path_Name_Type;
      Project_Scan_State  : Saved_Project_Scan_State;
      Source_Index        : Source_File_Index;

      Extending : Boolean := False;

      Extended_Project : Project_Node_Id := Empty_Node;

      A_Project_Name_And_Node : Tree_Private_Part.Project_Name_And_Node :=
                                  Tree_Private_Part.Projects_Htable.Get_First
                                    (In_Tree.Projects_HT);

      Name_From_Path  : constant Name_Id :=
        Project_Name_From (Path_Name, Is_Config_File => Is_Config_File);
      Name_Of_Project : Name_Id := No_Name;

      Duplicated : Boolean := False;

      First_With        : With_Id;
      Imported_Projects : Project_Node_Id := Empty_Node;

      use Tree_Private_Part;

      Project_Comment_State : Tree.Comment_State;

      Qualifier_Location : Source_Ptr;

   begin
      Extends_All := False;

      declare
         Normed_Path    : constant String := Normalize_Pathname
                            (Path_Name,
                             Directory      => Current_Dir,
                             Resolve_Links  => False,
                             Case_Sensitive => True);
         Canonical_Path : constant String := Normalize_Pathname
                            (Normed_Path,
                             Directory      => Current_Dir,
                             Resolve_Links  => Opt.Follow_Links_For_Files,
                             Case_Sensitive => False);
      begin
         Name_Len := Normed_Path'Length;
         Name_Buffer (1 .. Name_Len) := Normed_Path;
         Normed_Path_Name := Name_Find;
         Name_Len := Canonical_Path'Length;
         Name_Buffer (1 .. Name_Len) := Canonical_Path;
         Canonical_Path_Name := Name_Find;

         if Opt.Follow_Links_For_Files then
            Resolved_Path_Name := Canonical_Path_Name;

         else
            Name_Len := 0;
            Add_Str_To_Name_Buffer
              (Normalize_Pathname
                 (Canonical_Path,
                  Resolve_Links => True,
                  Case_Sensitive => False));
            Resolved_Path_Name := Name_Find;
         end if;

      end;

      if Has_Circular_Dependencies
           (Env.Flags, Normed_Path_Name, Canonical_Path_Name)
      then
         Project := Empty_Node;
         return;
      end if;

      --  Put the new path name on the stack

      Project_Stack.Append
        ((Path_Name           => Normed_Path_Name,
          Canonical_Path_Name => Canonical_Path_Name,
          Id                  => Empty_Node,
          Limited_With        => In_Limited));

      --  Check if the project file has already been parsed

      while
        A_Project_Name_And_Node /= Tree_Private_Part.No_Project_Name_And_Node
      loop
         if A_Project_Name_And_Node.Resolved_Path = Resolved_Path_Name then
            if Extended then

               if A_Project_Name_And_Node.Extended then
                  if A_Project_Name_And_Node.Proj_Qualifier /= Abstract_Project
                  then
                     Error_Msg
                       (Env.Flags,
                        "cannot extend the same project file several times",
                        Token_Ptr);
                  end if;
               elsif not A_Project_Name_And_Node.From_Extended then
                  Error_Msg
                    (Env.Flags,
                     "cannot extend an already imported project file",
                     Token_Ptr);

               else
                  --  Register this project as being extended

                  A_Project_Name_And_Node.Extended := True;
                  Tree_Private_Part.Projects_Htable.Set
                    (In_Tree.Projects_HT,
                     A_Project_Name_And_Node.Name,
                     A_Project_Name_And_Node);
               end if;

            elsif A_Project_Name_And_Node.Extended then
               Extends_All :=
                 Is_Extending_All (A_Project_Name_And_Node.Node, In_Tree);

               --  If the imported project is an extended project A, and we are
               --  in an extended project, replace A with the ultimate project
               --  extending A.

               if From_Extended /= None then
                  declare
                     Decl : Project_Node_Id :=
                              Project_Declaration_Of
                                (A_Project_Name_And_Node.Node, In_Tree);

                     Prj  : Project_Node_Id :=
                              A_Project_Name_And_Node.Node;

                  begin
                     --  Loop through extending projects to find the ultimate
                     --  extending project, that is the one that is not
                     --  extended. For an abstract project, as it can be
                     --  extended several times, there is no extending project
                     --  registered, so the loop does not execute and the
                     --  resulting project is the abstract project.

                     while
                       Extending_Project_Of (Decl, In_Tree) /= Empty_Node
                     loop
                        Prj := Extending_Project_Of (Decl, In_Tree);
                        Decl := Project_Declaration_Of (Prj, In_Tree);
                     end loop;

                     A_Project_Name_And_Node.Node := Prj;
                  end;
               else
                  Error_Msg
                    (Env.Flags,
                     "cannot import an already extended project file",
                     Token_Ptr);
               end if;

            elsif A_Project_Name_And_Node.From_Extended then
               --  This project is now imported from a non extending project.
               --  Indicate this in has table Projects.HT.

               A_Project_Name_And_Node.From_Extended := False;
               Tree_Private_Part.Projects_Htable.Set
                 (In_Tree.Projects_HT,
                  A_Project_Name_And_Node.Name,
                  A_Project_Name_And_Node);
            end if;

            Project := A_Project_Name_And_Node.Node;
            Project_Stack.Decrement_Last;
            return;
         end if;

         A_Project_Name_And_Node :=
           Tree_Private_Part.Projects_Htable.Get_Next (In_Tree.Projects_HT);
      end loop;

      --  We never encountered this project file. Save the scan state, load the
      --  project file and start to scan it.

      Save_Project_Scan_State (Project_Scan_State);
      Source_Index := Load_Project_File (Path_Name);
      Tree.Save (Project_Comment_State);

      --  If we cannot find it, we stop

      if Source_Index = No_Source_File then
         Project := Empty_Node;
         Project_Stack.Decrement_Last;
         return;
      end if;

      Prj.Err.Scanner.Initialize_Scanner (Source_Index);
      Tree.Reset_State;
      Scan (In_Tree);

      if not Is_Config_File
        and then Name_From_Path = No_Name
        and then not Implicit_Project
      then

         --  The project file name is not correct (no or bad extension, or not
         --  following Ada identifier's syntax).

         Error_Msg_File_1 := File_Name_Type (Canonical_Path_Name);
         Error_Msg (Env.Flags,
                    "?{ is not a valid path name for a project file",
                    Token_Ptr);
      end if;

      if Current_Verbosity >= Medium then
         Debug_Increase_Indent ("Parsing """ & Path_Name & '"');
      end if;

      Project_Directory :=
        Path_Name_Type (Get_Directory (File_Name_Type (Normed_Path_Name)));

      --  Is there any imported project?

      Pre_Parse_Context_Clause
        (In_Tree        => In_Tree,
         Is_Config_File => Is_Config_File,
         Context_Clause => First_With,
         Flags          => Env.Flags);

      Project := Default_Project_Node
                   (Of_Kind => N_Project, In_Tree => In_Tree);
      Project_Stack.Table (Project_Stack.Last).Id := Project;
      Set_Directory_Of (Project, In_Tree, Project_Directory);
      Set_Path_Name_Of (Project, In_Tree,  Normed_Path_Name);

      Read_Project_Qualifier
        (Env.Flags, In_Tree, Is_Config_File, Qualifier_Location, Project);

      Set_Location_Of (Project, In_Tree, Token_Ptr);

      Expect (Tok_Project, "PROJECT");

      --  Mark location of PROJECT token if present

      if Token = Tok_Project then
         Scan (In_Tree); -- past PROJECT
         Set_Location_Of (Project, In_Tree, Token_Ptr);
      end if;

      --  Clear the Buffer

      Buffer_Last := 0;
      loop
         Expect (Tok_Identifier, "identifier");

         --  If the token is not an identifier, clear the buffer before
         --  exiting to indicate that the name of the project is ill-formed.

         if Token /= Tok_Identifier then
            Buffer_Last := 0;
            exit;
         end if;

         --  Add the identifier name to the buffer

         Get_Name_String (Token_Name);
         Add_To_Buffer (Name_Buffer (1 .. Name_Len), Buffer, Buffer_Last);

         --  Scan past the identifier

         Scan (In_Tree);

         --  If we have a dot, add a dot to the Buffer and look for the next
         --  identifier.

         exit when Token /= Tok_Dot;
         Add_To_Buffer (".", Buffer, Buffer_Last);

         --  Scan past the dot

         Scan (In_Tree);
      end loop;

      --  See if this is an extending project

      if Token = Tok_Extends then

         if Is_Config_File then
            Error_Msg
              (Env.Flags,
               "extending configuration project not allowed", Token_Ptr);
         end if;

         --  Make sure that gnatmake will use mapping files

         Opt.Create_Mapping_File := True;

         --  We are extending another project

         Extending := True;

         Scan (In_Tree); -- past EXTENDS

         if Token = Tok_All then
            Extends_All := True;
            Set_Is_Extending_All (Project, In_Tree);
            Scan (In_Tree); --  scan past ALL
         end if;
      end if;

      --  If the name is well formed, Buffer_Last is > 0

      if Buffer_Last > 0 then

         --  The Buffer contains the name of the project

         Name_Len := Buffer_Last;
         Name_Buffer (1 .. Name_Len) := Buffer (1 .. Buffer_Last);
         Name_Of_Project := Name_Find;
         Set_Name_Of (Project, In_Tree, Name_Of_Project);

         --  To get expected name of the project file, replace dots by dashes

         for Index in 1 .. Name_Len loop
            if Name_Buffer (Index) = '.' then
               Name_Buffer (Index) := '-';
            end if;
         end loop;

         Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));

         declare
            Expected_Name : constant Name_Id := Name_Find;
            Extension     : String_Access;

         begin
            --  Output a warning if the actual name is not the expected name

            if not Is_Config_File
              and then (Name_From_Path /= No_Name)
              and then Expected_Name /= Name_From_Path
            then
               Error_Msg_Name_1 := Expected_Name;

               if Is_Config_File then
                  Extension := new String'(Config_Project_File_Extension);

               else
                  Extension := new String'(Project_File_Extension);
               end if;

               Error_Msg
                 (Env.Flags,
                  "?file name does not match project name, should be `%%"
                  & Extension.all & "`",
                  Token_Ptr);
            end if;
         end;

         --  Read the original casing of the project name and put it in the
         --  project node.

         declare
            Loc : Source_Ptr;
         begin
            Loc := Location_Of (Project, In_Tree);
            for J in 1 .. Name_Len loop
               Name_Buffer (J) := Sinput.Source (Loc);
               Loc := Loc + 1;
            end loop;

            Set_Display_Name_Of (Project, In_Tree, Name_Find);
         end;

         declare
            From_Ext : Extension_Origin := None;

         begin
            --  Extending_All is always propagated

            if From_Extended = Extending_All or else Extends_All then
               From_Ext := Extending_All;

            --  Otherwise, From_Extended is set to Extending_Single if the
            --  current project is an extending project.

            elsif Extended then
               From_Ext := Extending_Simple;
            end if;

            Post_Parse_Context_Clause
              (In_Tree           => In_Tree,
               Context_Clause    => First_With,
               In_Limited        => In_Limited,
               Limited_Withs     => False,
               Imported_Projects => Imported_Projects,
               Project_Directory => Project_Directory,
               From_Extended     => From_Ext,
               Packages_To_Check => Packages_To_Check,
               Depth             => Depth + 1,
               Current_Dir       => Current_Dir,
               Is_Config_File    => Is_Config_File,
               Env               => Env);
            Set_First_With_Clause_Of (Project, In_Tree, Imported_Projects);
         end;

         if not Is_Config_File then
            declare
               Name_And_Node : Tree_Private_Part.Project_Name_And_Node :=
                                 Tree_Private_Part.Projects_Htable.Get_First
                                   (In_Tree.Projects_HT);
               Project_Name  : Name_Id := Name_And_Node.Name;

            begin
               --  Check if we already have a project with this name

               while Project_Name /= No_Name
                 and then Project_Name /= Name_Of_Project
               loop
                  Name_And_Node :=
                    Tree_Private_Part.Projects_Htable.Get_Next
                      (In_Tree.Projects_HT);
                  Project_Name := Name_And_Node.Name;
               end loop;

               --  Report an error if we already have a project with this name

               if Project_Name /= No_Name then
                  Duplicated := True;
                  Error_Msg_Name_1 := Project_Name;
                  Error_Msg
                    (Env.Flags, "duplicate project name %%",
                     Location_Of (Project, In_Tree));
                  Error_Msg_Name_1 :=
                    Name_Id (Path_Name_Of (Name_And_Node.Node, In_Tree));
                  Error_Msg
                    (Env.Flags,
                     "\already in %%", Location_Of (Project, In_Tree));
               end if;
            end;
         end if;

      end if;

      if Extending then
         Expect (Tok_String_Literal, "literal string");

         if Token = Tok_String_Literal then
            Set_Extended_Project_Path_Of
              (Project,
               In_Tree,
               Path_Name_Type (Token_Name));

            declare
               Original_Path_Name : constant String :=
                                      Get_Name_String (Token_Name);

               Extended_Project_Path_Name_Id : Path_Name_Type;

            begin
               Find_Project
                 (Env.Project_Path,
                  Project_File_Name => Original_Path_Name,
                  Directory         => Get_Name_String (Project_Directory),
                  Path              => Extended_Project_Path_Name_Id);

               if Extended_Project_Path_Name_Id = No_Path then

                  --  We could not find the project file to extend

                  Error_Msg_Name_1 := Token_Name;

                  Error_Msg (Env.Flags, "unknown project file: %%", Token_Ptr);

                  --  If not in the main project file, display the import path

                  if Project_Stack.Last > 1 then
                     Error_Msg_Name_1 :=
                       Name_Id
                         (Project_Stack.Table (Project_Stack.Last).Path_Name);
                     Error_Msg (Env.Flags, "\extended by %%", Token_Ptr);

                     for Index in reverse 1 .. Project_Stack.Last - 1 loop
                        Error_Msg_Name_1 :=
                          Name_Id
                            (Project_Stack.Table (Index).Path_Name);
                        Error_Msg (Env.Flags, "\imported by %%", Token_Ptr);
                     end loop;
                  end if;

               else
                  declare
                     From_Ext : Extension_Origin := None;

                  begin
                     if From_Extended = Extending_All or else Extends_All then
                        From_Ext := Extending_All;
                     end if;

                     Parse_Single_Project
                       (In_Tree           => In_Tree,
                        Project           => Extended_Project,
                        Extends_All       => Extends_All,
                        Path_Name_Id      => Extended_Project_Path_Name_Id,
                        Extended          => True,
                        From_Extended     => From_Ext,
                        In_Limited        => In_Limited,
                        Packages_To_Check => Packages_To_Check,
                        Depth             => Depth + 1,
                        Current_Dir       => Current_Dir,
                        Is_Config_File    => Is_Config_File,
                        Env               => Env);
                  end;

                  if Present (Extended_Project) then

                     if Project_Qualifier_Of (Extended_Project, In_Tree) =
                                                                   Aggregate
                     then
                        Error_Msg_Name_1 :=
                          Name_Id (Path_Name_Of (Extended_Project, In_Tree));
                        Error_Msg
                          (Env.Flags,
                           "cannot extend aggregate project %%",
                           Location_Of (Project, In_Tree));
                     end if;

                     --  A project that extends an extending-all project is
                     --  also an extending-all project.

                     if Is_Extending_All (Extended_Project, In_Tree) then
                        Set_Is_Extending_All (Project, In_Tree);
                     end if;

                     --  An abstract project can only extend an abstract
                     --  project. Otherwise we may have an abstract project
                     --  with sources if it inherits sources from the project
                     --  it extends.

                     if Project_Qualifier_Of (Project, In_Tree) =
                                                           Abstract_Project
                       and then
                         Project_Qualifier_Of (Extended_Project, In_Tree) /=
                                                           Abstract_Project
                     then
                        Error_Msg
                          (Env.Flags, "an abstract project can only extend " &
                           "another abstract project",
                           Qualifier_Location);
                     end if;
                  end if;
               end if;
            end;

            Scan (In_Tree); -- past the extended project path
         end if;
      end if;

      Check_Extending_All_Imports (Env.Flags, In_Tree, Project);
      Check_Aggregate_Imports (Env.Flags, In_Tree, Project);
      Check_Import_Aggregate (Env.Flags, In_Tree, Project);

      --  Check that a project with a name including a dot either imports
      --  or extends the project whose name precedes the last dot.

      if Name_Of_Project /= No_Name then
         Get_Name_String (Name_Of_Project);

      else
         Name_Len := 0;
      end if;

      --  Look for the last dot

      while Name_Len > 0 and then Name_Buffer (Name_Len) /= '.' loop
         Name_Len := Name_Len - 1;
      end loop;

      --  If a dot was found, check if parent project is imported or extended

      if Name_Len > 0 then
         Name_Len := Name_Len - 1;

         declare
            Parent_Name   : constant Name_Id := Name_Find;
            Parent_Found  : Boolean := False;
            Parent_Node   : Project_Node_Id := Empty_Node;
            With_Clause   : Project_Node_Id :=
                              First_With_Clause_Of (Project, In_Tree);
            Imp_Proj_Name : Name_Id;

         begin
            --  If there is an extended project, check its name

            if Present (Extended_Project) then
               Parent_Node := Extended_Project;
               Parent_Found :=
                 Name_Of (Extended_Project, In_Tree) = Parent_Name;
            end if;

            --  If the parent project is not the extended project,
            --  check each imported project until we find the parent project.

            Imported_Loop :
            while not Parent_Found and then Present (With_Clause) loop
               Parent_Node := Project_Node_Of (With_Clause, In_Tree);
               Extension_Loop : while Present (Parent_Node) loop
                  Imp_Proj_Name := Name_Of (Parent_Node, In_Tree);
                  Parent_Found := Imp_Proj_Name = Parent_Name;
                  exit Imported_Loop when Parent_Found;
                  Parent_Node :=
                    Extended_Project_Of
                      (Project_Declaration_Of (Parent_Node, In_Tree),
                       In_Tree);
               end loop Extension_Loop;

               With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
            end loop Imported_Loop;

            if Parent_Found then
               Set_Parent_Project_Of (Project, In_Tree, To => Parent_Node);

            else
               --  If the parent project was not found, report an error

               Error_Msg_Name_1 := Name_Of_Project;
               Error_Msg_Name_2 := Parent_Name;
               Error_Msg (Env.Flags,
                          "project %% does not import or extend project %%",
                          Location_Of (Project, In_Tree));
            end if;
         end;
      end if;

      Expect (Tok_Is, "IS");
      Set_End_Of_Line (Project);
      Set_Previous_Line_Node (Project);
      Set_Next_End_Node (Project);

      declare
         Project_Declaration : Project_Node_Id := Empty_Node;

      begin
         --  No need to Scan past "is", Prj.Dect.Parse will do it

         Prj.Dect.Parse
           (In_Tree           => In_Tree,
            Declarations      => Project_Declaration,
            Current_Project   => Project,
            Extends           => Extended_Project,
            Packages_To_Check => Packages_To_Check,
            Is_Config_File    => Is_Config_File,
            Flags             => Env.Flags);
         Set_Project_Declaration_Of (Project, In_Tree, Project_Declaration);

         if Present (Extended_Project)
           and then Project_Qualifier_Of (Extended_Project, In_Tree) /=
                                                        Abstract_Project
         then
            Set_Extending_Project_Of
              (Project_Declaration_Of (Extended_Project, In_Tree), In_Tree,
               To => Project);
         end if;
      end;

      Expect (Tok_End, "END");
      Remove_Next_End_Node;

      --  Skip "end" if present

      if Token = Tok_End then
         Scan (In_Tree);
      end if;

      --  Clear the Buffer

      Buffer_Last := 0;

      --  Store the name following "end" in the Buffer. The name may be made of
      --  several simple names.

      loop
         Expect (Tok_Identifier, "identifier");

         --  If we don't have an identifier, clear the buffer before exiting to
         --  avoid checking the name.

         if Token /= Tok_Identifier then
            Buffer_Last := 0;
            exit;
         end if;

         --  Add the identifier to the Buffer
         Get_Name_String (Token_Name);
         Add_To_Buffer (Name_Buffer (1 .. Name_Len), Buffer, Buffer_Last);

         --  Scan past the identifier

         Scan (In_Tree);
         exit when Token /= Tok_Dot;
         Add_To_Buffer (".", Buffer, Buffer_Last);
         Scan (In_Tree);
      end loop;

      --  If we have a valid name, check if it is the name of the project

      if Name_Of_Project /= No_Name and then Buffer_Last > 0 then
         if To_Lower (Buffer (1 .. Buffer_Last)) /=
            Get_Name_String (Name_Of (Project, In_Tree))
         then
            --  Invalid name: report an error

            Error_Msg (Env.Flags, "expected """ &
                       Get_Name_String (Name_Of (Project, In_Tree)) & """",
                       Token_Ptr);
         end if;
      end if;

      Expect (Tok_Semicolon, "`;`");

      --  Check that there is no more text following the end of the project
      --  source.

      if Token = Tok_Semicolon then
         Set_Previous_End_Node (Project);
         Scan (In_Tree);

         if Token /= Tok_EOF then
            Error_Msg
              (Env.Flags,
               "unexpected text following end of project", Token_Ptr);
         end if;
      end if;

      if not Duplicated and then Name_Of_Project /= No_Name then

         --  Add the name of the project to the hash table, so that we can
         --  check that no other subsequent project will have the same name.

         Tree_Private_Part.Projects_Htable.Set
           (T => In_Tree.Projects_HT,
            K => Name_Of_Project,
            E => (Name           => Name_Of_Project,
                  Node           => Project,
                  Resolved_Path  => Resolved_Path_Name,
                  Extended       => Extended,
                  From_Extended  => From_Extended /= None,
                  Proj_Qualifier => Project_Qualifier_Of (Project, In_Tree)));
      end if;

      declare
         From_Ext : Extension_Origin := None;

      begin
         --  Extending_All is always propagated

         if From_Extended = Extending_All or else Extends_All then
            From_Ext := Extending_All;

            --  Otherwise, From_Extended is set to Extending_Single if the
            --  current project is an extending project.

         elsif Extended then
            From_Ext := Extending_Simple;
         end if;

         Post_Parse_Context_Clause
           (In_Tree           => In_Tree,
            Context_Clause    => First_With,
            In_Limited        => In_Limited,
            Limited_Withs     => True,
            Imported_Projects => Imported_Projects,
            Project_Directory => Project_Directory,
            From_Extended     => From_Ext,
            Packages_To_Check => Packages_To_Check,
            Depth             => Depth + 1,
            Current_Dir       => Current_Dir,
            Is_Config_File    => Is_Config_File,
            Env               => Env);
         Set_First_With_Clause_Of (Project, In_Tree, Imported_Projects);
      end;

      --  Restore the scan state, in case we are not the main project

      Restore_Project_Scan_State (Project_Scan_State);

      --  And remove the project from the project stack

      Project_Stack.Decrement_Last;

      --  Indicate if there are unkept comments

      Tree.Set_Project_File_Includes_Unkept_Comments
        (Node    => Project,
         In_Tree => In_Tree,
         To      => Tree.There_Are_Unkept_Comments);

      --  And restore the comment state that was saved

      Tree.Restore_And_Free (Project_Comment_State);

      Debug_Decrease_Indent;

      if Project /= Empty_Node and then Implicit_Project then
         Name_Len := 0;
         Add_Str_To_Name_Buffer (Current_Dir);
         Add_Char_To_Name_Buffer (Dir_Sep);
         In_Tree.Project_Nodes.Table (Project).Directory := Name_Find;
      end if;
   end Parse_Single_Project;

   -----------------------
   -- Project_Name_From --
   -----------------------

   function Project_Name_From
     (Path_Name      : String;
      Is_Config_File : Boolean) return Name_Id
   is
      Canonical : String (1 .. Path_Name'Length) := Path_Name;
      First     : Natural := Canonical'Last;
      Last      : Natural := First;
      Index     : Positive;

   begin
      if Current_Verbosity = High then
         Debug_Output ("Project_Name_From (""" & Canonical & """)");
      end if;

      --  If the path name is empty, return No_Name to indicate failure

      if First = 0 then
         return No_Name;
      end if;

      Canonical_Case_File_Name (Canonical);

      --  Look for the last dot in the path name

      while First > 0
        and then
        Canonical (First) /= '.'
      loop
         First := First - 1;
      end loop;

      --  If we have a dot, check that it is followed by the correct extension

      if First > 0 and then Canonical (First) = '.' then
         if (not Is_Config_File
              and then Canonical (First .. Last) = Project_File_Extension
              and then First /= 1)
           or else
             (Is_Config_File
               and then
                 Canonical (First .. Last) = Config_Project_File_Extension
               and then First /= 1)
         then
            --  Look for the last directory separator, if any

            First := First - 1;
            Last := First;
            while First > 0
              and then Canonical (First) /= '/'
              and then Canonical (First) /= Dir_Sep
            loop
               First := First - 1;
            end loop;

         else
            --  Not the correct extension, return No_Name to indicate failure

            return No_Name;
         end if;

      --  If no dot in the path name, return No_Name to indicate failure

      else
         return No_Name;
      end if;

      First := First + 1;

      --  If the extension is the file name, return No_Name to indicate failure

      if First > Last then
         return No_Name;
      end if;

      --  Put the name in lower case into Name_Buffer

      Name_Len := Last - First + 1;
      Name_Buffer (1 .. Name_Len) := To_Lower (Canonical (First .. Last));

      Index := 1;

      --  Check if it is a well formed project name. Return No_Name if it is
      --  ill formed.

      loop
         if not Is_Letter (Name_Buffer (Index)) then
            return No_Name;

         else
            loop
               Index := Index + 1;

               exit when Index >= Name_Len;

               if Name_Buffer (Index) = '_' then
                  if Name_Buffer (Index + 1) = '_' then
                     return No_Name;
                  end if;
               end if;

               exit when Name_Buffer (Index) = '-';

               if Name_Buffer (Index) /= '_'
                 and then not Is_Alphanumeric (Name_Buffer (Index))
               then
                  return No_Name;
               end if;

            end loop;
         end if;

         if Index >= Name_Len then
            if Is_Alphanumeric (Name_Buffer (Name_Len)) then

               --  All checks have succeeded. Return name in Name_Buffer

               return Name_Find;

            else
               return No_Name;
            end if;

         elsif Name_Buffer (Index) = '-' then
            Index := Index + 1;
         end if;
      end loop;
   end Project_Name_From;

end Prj.Part;