libgpr2_24.0.0_eda3c693/langkit/generated/src/gpr_parser_support-lexical_envs_impl.adb

   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
--
--  Copyright (C) 2014-2022, AdaCore
--  SPDX-License-Identifier: Apache-2.0
--

with Ada.Containers.Generic_Array_Sort;
with Ada.Strings.Unbounded;           use Ada.Strings.Unbounded;
with Ada.Strings.Wide_Wide_Unbounded; use Ada.Strings.Wide_Wide_Unbounded;
with Ada.Text_IO;                     use Ada.Text_IO;

with System.Address_Image;
with System.Assertions;

with Gpr_Parser_Support.Errors; use Gpr_Parser_Support.Errors;
with Gpr_Parser_Support.Images; use Gpr_Parser_Support.Images;

package body Gpr_Parser_Support.Lexical_Envs_Impl is

   procedure Invalidate_Cache (Env : Lexical_Env_Access);

   procedure Initialize_Foreign_Nodes (El : in out Internal_Map_Element)
   with Inline_Always;
   --  Initialize foreign nodes map in ``El``

   function Image (E : Entity) return String
   is
     (Image (Node_Text_Image (E.Node, False)));

   function Entity_Vectors_Image is new Entity_Vectors.Image (Image);

   No_Entity_Info : constant Entity_Info := (Empty_Metadata, null, False);

   function Is_Lookup_Cache_Valid (Self : Lexical_Env) return Boolean
      with Pre => Self.Kind = Static_Primary;
   --  Return whether Env's lookup cache is valid. This will check every
   --  Lookup_Cache_Valid flag up Env's parent chain.

   function Wrap
     (Env   : Lexical_Env_Access;
      Owner : Generic_Unit_Ptr := No_Generic_Unit) return Lexical_Env
   is
     ((Env     => Wrap (Env),
       Hash    => Hash (Env),
       Kind    => (if Env = null then Static_Primary else Env.Kind),
       Owner   => Owner,
       Version => (if Owner /= No_Generic_Unit
                   then Get_Unit_Version (Owner) else 0)));

   function OK_For_Rebindings (Self : Lexical_Env) return Boolean is
     (Self.Kind in Primary_Kind and then Env_Node (Self) /= No_Node);

   function Extract_Rebinding
     (Rebindings  : in out Env_Rebindings;
      Rebound_Env : Lexical_Env;
      Found       : out Boolean) return Lexical_Env;
   --  Look for a pair in Rebindings whose Old_Env field is "Rebound_Env".
   --
   --  If there is one, return the env it is associated to, and put the
   --  remaining set of rebindings in rebindings. Otherwise, return
   --  "Rebound_Env".
   --
   --  If Rebindings is bound to a new set of rebindings upon return, the
   --  ownership share of the old rebinding set will have been forfeited.

   function Shed_Rebindings
     (From_Env   : Lexical_Env;
      Rebindings : Env_Rebindings) return Env_Rebindings
     with Inline;
   --  Shed env rebindings that are not in the parent chain for From_Env

   procedure Check_Rebindings_Uniqueness (Self : Env_Rebindings);
   --  Perform a uniqueness check on the various rebindings in Self. In
   --  particular, check that there are no two identical Old_Env and no two
   --  identical New_Env in the set of rebindings. If there are, raise a
   --  property error.

   procedure Get_Internal_Impl
     (Self                    : Lexical_Env;
      Key                     : Symbol_Type;
      Lookup_Kind             : Lookup_Kind_Type := Recursive;
      Rebindings              : Env_Rebindings := null;
      Metadata                : Node_Metadata := Empty_Metadata;
      Categories              : Ref_Categories;
      Local_Results           : in out Lookup_Result_Vector;
      Toplevel                : Boolean := True;
      Recursive_Check_Reached : in out Boolean);
   --  This is the real Env.Get implementation.
   --
   --  ``Toplevel`` is used to discriminate between the toplevel call and
   --  further calls, for the ``Toplevel_Only`` cache mode.
   --
   --  ``Recursive_Check_Reached`` is an helper variable passed down to
   --  recursive calls, to down propagate to parent calls when the recursion
   --  protection is activated, i.e. when an lexical environment is visited a
   --  second time in a recursive call. When this recursive check is reached,
   --  all intermediate calls won't be cached because the origin of the request
   --  has potentially changed the results. See the Ada example below::
   --
   --      package R is
   --          package N is
   --               type O is ...;
   --               -- <- Original env request on "O" from here
   --          end N;
   --          --  <- When exploring parents, env_get will get here. The cache
   --          --     is empty.
   --          use N;
   --          --  <-  When traversing the "use", since the request originates
   --          --      from `N`, the recursion protection will stop it's
   --          --      re-traversal
   --          --  <- If we cache, we'll cache a wrong result for the request
   --          --     `R_env.get("O")`, when the request starts from `R`'s env
   --      end R;

   procedure Get_Internal
     (Self          : Lexical_Env;
      Key           : Symbol_Type;
      Lookup_Kind   : Lookup_Kind_Type := Recursive;
      Rebindings    : Env_Rebindings := null;
      Metadata      : Node_Metadata := Empty_Metadata;
      Categories    : Ref_Categories;
      Local_Results : in out Lookup_Result_Vector);
   --  This is the ``Get_Internal_Impl`` wrapper. It is used to forward default
   --  values to some internal parameters used by ``Get_Internal_Impl``. We're
   --  not using a nested function for ``Get_Internal_Impl`` because the level
   --  of nesting is bad enough already.
   --
   --  ``Local_Results`` is the vector in which results will be accumulated by
   --  the recursive calls.
   --
   --  TODO: It might be exactly the same to return Local_Result rather than
   --  make it an in out param, now that we have a wrapper.

   procedure Reset_Lookup_Cache (Self : Lexical_Env);
   --  Reset Self's lexical environment lookup cache

   ----------------
   -- Text_Image --
   ----------------

   function Text_Image (Cats : Ref_Categories) return Text_Type is
      Ret : Unbounded_Text_Type;
   begin
      Append (Ret, "(");

      for Cat in Ref_Category'Range loop
         Append (Ret, To_Text (Cat'Image & " => " & Cats (Cat)'Image & ", "));
      end loop;
      Append (Ret, ")");
      return To_Text (Ret);
   end Text_Image;

   ---------------------------
   -- Is_Lookup_Cache_Valid --
   ---------------------------

   function Is_Lookup_Cache_Valid (Self : Lexical_Env) return Boolean is
      P : Lexical_Env;
   begin
      if Unwrap (Self).Lookup_Cache_Valid then
         P := Parent (Self);
         return (P in Null_Lexical_Env | Empty_Env
                 or else Is_Lookup_Cache_Valid (P));
      else
         return False;
      end if;
   end Is_Lookup_Cache_Valid;

   ------------------------
   -- Reset_Lookup_Cache --
   ------------------------

   procedure Reset_Lookup_Cache (Self : Lexical_Env) is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      for C of Env.Lookup_Cache loop
         C.Elements.Destroy;
      end loop;

      Env.Lookup_Cache.Clear;
      Env.Lookup_Cache_Valid := True;
   end Reset_Lookup_Cache;

   -----------------------
   -- Simple_Env_Getter --
   -----------------------

   function Simple_Env_Getter (E : Lexical_Env) return Env_Getter is
   begin
      Inc_Ref (E);
      return Env_Getter'(Dynamic => False,
                         Env     => E);
   end Simple_Env_Getter;

   --------------------
   -- Dyn_Env_Getter --
   --------------------

   function Dyn_Env_Getter
     (Resolver : Lexical_Env_Resolver; Node : Node_Type) return Env_Getter is
   begin
      return Env_Getter'(True, Null_Lexical_Env, Node, Resolver);
   end Dyn_Env_Getter;

   -------------
   -- Resolve --
   -------------

   procedure Resolve (Self : in out Env_Getter; Info : Entity_Info) is
      Env : Lexical_Env;
   begin
      case Self.Dynamic is
         when True =>
            Env := Get_Env (Self, Info);

            --  Get_Env returns an ownership share for the returned reference,
            --  but we don't use it here, so dec ref.
            Dec_Ref (Env);
         when False =>
            null;
      end case;
   exception
      when Exc : others =>

         --  Resolution failed. If ``Exc`` is an exception that properties are
         --  allowed to raise, there is nothing else to do as ``Get_Env`` took
         --  care of invalidating the cache.
         --
         --  Note that in that case there is no need to propagate the error, as
         --  the job of this procedure is just to do precomputation for
         --  ``Get_Env``. The next call to ``Get_Env`` will propagate this
         --  error, so all is fine.

         if not Properties_May_Raise (Exc) then
            raise;
         end if;
   end Resolve;

   -------------
   -- Get_Env --
   -------------

   function Get_Env (Self : in out Env_Getter;
                     Info : Entity_Info) return Lexical_Env
   is
      Cache_Enabled : constant Boolean := Info = No_Entity_Info;
      --  The cache (Self.Env) can be used only if No_Entity_Info is passed

      --  Note: in this whole function, we take special care for empty
      --  environments: while these are represented using the ``Empty_Env``
      --  constant through the whole codebase, here rules are slightly twisted
      --  for performance and appropriate cache invalidation (see documentation
      --  for the ``Env_Getter.Env`` field).
      --
      --  The bottom line is: cached empty environments differ from the
      --  ``Empty_Env`` constant: the ``Version`` component is not 0 and is
      --  used to determine if the cached empty environment is stale (see
      --  ``Is_Stale``), and we must not returned these "twisted" empty
      --  environments to ``Get_Env``'s caller, but rather return the
      --  ``Empty_Env`` constant instead (the version hack shall not go to the
      --  outside world).

      function Is_Stale (Env : Lexical_Env) return Boolean
      with Pre => Self.Dynamic;
      --  Return whether the ``Env`` cached environment refers to an obsolete.
      --  ``Node`` is the node used to compute it.

      --------------
      -- Is_Stale --
      --------------

      function Is_Stale (Env : Lexical_Env) return Boolean is
         E : constant Lexical_Env_Access := Unwrap (Env);
         L : Lexical_Env;
      begin
         --  Take care of the special case of empty environments, which may
         --  differ from ``Empty_Env`` in ``Env.Version`` (see above).
         if Env.Env = Empty_Env.Env then
            return Env.Version < Get_Context_Version (Self.Node);
         end if;

         case Env.Kind is
            when Primary_Kind =>
               if Env.Owner /= No_Generic_Unit then
                  --  If there is an owner, check that the unit version has not
                  --  been incremented since then.
                  return Get_Unit_Version (Env.Owner) > Env.Version;
               end if;

               for I in E.Referenced_Envs.First_Index
                     .. E.Referenced_Envs.Last_Index
               loop
                  L := Get_Env
                    (E.Referenced_Envs.Get_Access (I).Getter, No_Entity_Info);
                  if Is_Stale (L) then
                     return True;
                  end if;
                  Dec_Ref (L);
               end loop;
               return False;

            when Orphaned =>
               pragma Assert (Env.Owner = No_Generic_Unit);
               return Is_Stale (E.Orphaned_Env);

            when Grouped =>
               pragma Assert (Env.Owner = No_Generic_Unit);
               return (for some GE of E.Grouped_Envs.all => Is_Stale (GE));

            when Rebound =>
               --  This env is stale as soon as either the rebound env or the
               --  rebindings are stale.
               return Is_Stale (E.Rebound_Env)
                      or else E.Rebindings.Version /= E.Rebindings_Version;
         end case;
      end Is_Stale;

   begin
      if Self.Dynamic then
         --  Resolve the dynamic lexical env getter. For this, use the cache if
         --  possible.
         if Cache_Enabled and then Self.Env /= Null_Lexical_Env then

            --  If it is not stale, return it. Make sure to return genuine
            --  ``Empty_Env`` copies for empty environments.
            if not Is_Stale (Self.Env) then
               if Self.Env.Env = Empty_Env.Env then
                  return Empty_Env;
               else
                  Inc_Ref (Self.Env);
                  return Self.Env;
               end if;
            end if;

            --  If it is stale, release and clear it
            Dec_Ref (Self.Env);
         end if;

         --  For some reason we could not use the cache: do the resolution and
         --  cache its result if applicable.
         declare
            E      : constant Entity := (Node => Self.Node, Info => Info);
            Result : constant Lexical_Env := Self.Resolver.all (E);
         begin
            if Cache_Enabled then
               --  Since the call to Self.Resolver above may have invoked
               --  Get_Env on the same Env_Getter object (Self), we need to be
               --  re-entrant. This means that though called Dec_Ref on
               --  Self.Env above, once we got here, Self.Env may have another
               --  value: we need to remove its ownership share before
               --  overriding below.
               Dec_Ref (Self.Env);

               --  The ownership share returned by the resolver goes to the
               --  cache: the call to Inc_Ref below will create a new one for
               --  the returned value.
               Self.Env := Result;

               --  Don't forget to record the context version for caches in the
               --  case of Empty_Env (see Env_Getter.Env and the cache hit code
               --  above).
               if Self.Env.Env = Empty_Env.Env then
                  Self.Env.Version := Get_Context_Version (Self.Node);
               end if;
            else
               return Result;
            end if;
         end;
      end if;

      --  Return a copy of the cached resolved lexical env, so create a new
      --  ownership share.
      Inc_Ref (Self.Env);
      return Self.Env;
   end Get_Env;

   ----------------
   -- Equivalent --
   ----------------

   function Equivalent (L, R : Env_Getter) return Boolean is
   begin
      if L.Dynamic or else R.Dynamic then
         raise Constraint_Error with "trying to compare dynamic env getters";
      else
         return L.Env = R.Env;
      end if;
   end Equivalent;

   -------------
   -- Inc_Ref --
   -------------

   procedure Inc_Ref (Self : Env_Getter) is
   begin
      Inc_Ref (Self.Env);
   end Inc_Ref;

   -------------
   -- Dec_Ref --
   -------------

   procedure Dec_Ref (Self : in out Env_Getter) is
   begin
      Dec_Ref (Self.Env);
   end Dec_Ref;

   -------------
   -- Combine --
   -------------

   function Combine (L, R : Env_Rebindings) return Env_Rebindings is
      function Append_All (Cur : Env_Rebindings) return Env_Rebindings;
      --  Given Cur = C1 -> C2 .. -> Cn, return C1 -> C2 .. -> Cn -> L

      ----------------
      -- Append_All --
      ----------------

      function Append_All (Cur : Env_Rebindings) return Env_Rebindings is
      begin
         if Cur = null then
            return L;
         end if;
         return Append (Append_All (Cur.Parent), Cur.Old_Env, Cur.New_Env);
      end Append_All;
   begin
      if L = null then
         return R;
      elsif R = null then
         return L;
      end if;
      return Result : constant Env_Rebindings := Append_All (R) do
         Check_Rebindings_Uniqueness (Result);
      end return;
   end Combine;

   ------------
   -- Append --
   ------------

   function Append
     (Self             : Env_Rebindings;
      Old_Env, New_Env : Lexical_Env) return Env_Rebindings
   is
      O : constant Lexical_Env_Access := Unwrap (Old_Env);
   begin
      --  Look for an existing rebinding for the result: in the Old_Env's pool
      --  if there is no parent, otherwise in the parent's children.
      if Self = null then
         if O.Rebindings_Pool /= null then
            declare
               use Env_Rebindings_Pools;
               Cur : constant Cursor := O.Rebindings_Pool.Find (New_Env);
            begin
               if Cur /= Env_Rebindings_Pools.No_Element then
                  return Element (Cur);
               end if;
            end;
         end if;

      else
         for C of Self.Children loop
            if C.Old_Env = Old_Env and then C.New_Env = New_Env then
               return C;
            end if;
         end loop;
      end if;

      --  No luck? then create a new rebinding and register it where required
      declare
         Result : constant Env_Rebindings := Acquire_Rebinding
           (Env_Node (Old_Env), Self, Old_Env, New_Env);
      begin
         if Self /= null then
            Self.Children.Append (Result);
         else
            if O.Rebindings_Pool = null then
               O.Rebindings_Pool := new Env_Rebindings_Pools.Map;
            end if;
            O.Rebindings_Pool.Insert (New_Env, Result);
         end if;

         Register_Rebinding (Env_Node (Old_Env), Result);
         Register_Rebinding (Env_Node (New_Env), Result);
         Check_Rebindings_Uniqueness (Result);
         return Result;
      end;
   end Append;

   ----------------------
   -- Append_Rebinding --
   ----------------------

   function Append_Rebinding
     (Self    : Env_Rebindings;
      Old_Env : Lexical_Env;
      New_Env : Lexical_Env) return Env_Rebindings is
   begin
      if not Is_Rebindable (Env_Node (Old_Env)) then
         raise Property_Error with "Illegal lexical environment rebinding";
      end if;

      return Append (Self, Old_Env, New_Env);
   end Append_Rebinding;

   -------------------
   -- Create_Entity --
   -------------------

   function Create_Entity (Node : Node_Type; Md : Node_Metadata) return Entity
   is
   begin
      return Entity'
        (Node => Node,
         Info => (Md => Md, Rebindings => null, From_Rebound => False));
   end Create_Entity;

   ----------------
   -- Equivalent --
   ----------------

   function Equivalent (L, R : Entity_Info) return Boolean is
   begin
      return L.Md = R.Md and then L.Rebindings = R.Rebindings;
   end Equivalent;

   ----------------
   -- Equivalent --
   ----------------

   function Equivalent (L, R : Entity) return Boolean is
   begin
      if L.Node /= R.Node then
         return False;
      end if;

      --  All null nodes are equals, regardless of their entity info
      if L.Node = No_Node then
         return True;
      end if;

      --  For all other cases, make sure the entity info is equivalent
      return Equivalent (L.Info, R.Info);
   end Equivalent;

   ------------------------
   -- Create_Lexical_Env --
   ------------------------

   function Create_Lexical_Env
     (Parent            : Lexical_Env;
      Node              : Node_Type;
      Transitive_Parent : Boolean := False;
      Owner             : Generic_Unit_Ptr) return Lexical_Env is
   begin
      if Parent /= Null_Lexical_Env then
         Inc_Ref (Parent);
      end if;
      return Wrap
        (new Lexical_Env_Record'
           (Kind                     => Static_Primary,
            Parent                   => Parent,
            Transitive_Parent        => Transitive_Parent,
            Node                     => Node,
            Referenced_Envs          => <>,
            Map                      => new Internal_Envs.Map,
            Rebindings_Pool          => null,
            Lookup_Cache_Valid       => True,
            Lookup_Cache             => Lookup_Cache_Maps.Empty_Map,
            Rebindings_Assoc_Ref_Env => -1),
         Owner => Owner);
   end Create_Lexical_Env;

   --------------------------------
   -- Create_Dynamic_Lexical_Env --
   --------------------------------

   function Create_Dynamic_Lexical_Env
     (Parent            : Lexical_Env;
      Node              : Node_Type;
      Transitive_Parent : Boolean := False;
      Owner             : Generic_Unit_Ptr;
      Assocs_Getter     : Inner_Env_Assocs_Resolver;
      Assoc_Resolver    : Entity_Resolver := null) return Lexical_Env is
   begin
      if Parent /= Null_Lexical_Env then
         Inc_Ref (Parent);
      end if;
      return Wrap
        (new Lexical_Env_Record'
           (Kind              => Dynamic_Primary,
            Parent            => Parent,
            Transitive_Parent => Transitive_Parent,
            Node              => Node,
            Rebindings_Pool   => null,
            Assocs_Getter     => Assocs_Getter,
            Assoc_Resolver    => Assoc_Resolver),
         Owner => Owner);
   end Create_Dynamic_Lexical_Env;

   ------------------------------
   -- Initialize_Foreign_Nodes --
   ------------------------------

   procedure Initialize_Foreign_Nodes (El : in out Internal_Map_Element) is
   begin
      if El.Foreign_Nodes = null then
         El.Foreign_Nodes := new Internal_Map_Node_Maps.Map;
      end if;
   end Initialize_Foreign_Nodes;

   ---------
   -- Add --
   ---------

   procedure Add
     (Self     : Lexical_Env;
      Key      : Symbol_Type;
      Value    : Node_Type;
      Md       : Node_Metadata := Empty_Metadata;
      Resolver : Entity_Resolver := null)
   is
      use Internal_Envs;

      Env   : constant Lexical_Env_Access := Unwrap (Self);
      Node  : constant Internal_Map_Node := (Value, Md, Resolver);
      C     : Cursor;
      Dummy : Boolean;
      Map   : Internal_Envs.Map renames Env.Map.all;
   begin
      --  See Empty_Env's documentation

      if Self = Empty_Env then
         return;
      end if;

      Me.Trace
        ("ADDING VAL key=""" & Image (Key) & """, env=" & Env_Image (Self));

      --  Invalidate the cache, and make sure we have an entry in the internal
      --  map for the given key.
      Invalidate_Cache (Env);
      Map.Insert (Key, Empty_Internal_Map_Element, C, Dummy);

      declare
         E : Internal_Map_Element renames Reference (Map, C).Element.all;
      begin
         --  If Self and Value belong to the same analysis unit, consider Node
         --  as native. In all other cases, it's a foreign node.
         if Is_Foreign (Self, Value) then
            Initialize_Foreign_Nodes (E);
            E.Foreign_Nodes.Insert (Node.Node, Node);
         else
            E.Native_Nodes.Append (Node);
         end if;
      end;
   end Add;

   ------------
   -- Remove --
   ------------

   procedure Remove (Self : Lexical_Env; Key : Symbol_Type; Value : Node_Type)
   is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      if Self = Empty_Env then
         return;
      end if;

      declare
         Ref : constant Internal_Envs.Reference_Type :=
           Env.Map.Reference (Key);
         V   : Internal_Map_Node_Vectors.Vector renames Ref.Native_Nodes;
      begin
         if Is_Foreign (Self, Value) then
            Ref.Foreign_Nodes.Delete (Value);
         else
            --  Get rid of the element. Do this in reverse order so that
            --  removing one element does not make us "step over" the next
            --  item. Also don't do this in place (using V.Pop) as we need to
            --  preserve the order of elements.

            for I in reverse 1 .. V.Length loop
               if V.Get (I).Node = Value then
                  V.Remove_At (I);
                  exit;
               end if;
            end loop;
         end if;

         Invalidate_Cache (Env);
      end;
   end Remove;

   ---------------
   -- Reference --
   ---------------

   procedure Reference
     (Self             : Lexical_Env;
      Referenced_From  : Node_Type;
      Resolver         : Lexical_Env_Resolver;
      Kind             : Ref_Kind := Normal;
      Categories       : Ref_Categories := All_Cats;
      Rebindings_Assoc : Boolean := False)
   is
      Env      : constant Lexical_Env_Access := Unwrap (Self);
      Refd_Env : Referenced_Env :=
        (Kind,
         Dyn_Env_Getter (Resolver, Referenced_From),
         False, Inactive, Categories);
   begin
      if Self = Empty_Env then
         return;
      end if;

      if Has_Trace then
         Rec.Trace ("REFERENCE " & Env_Image (Self));
      end if;

      Resolve (Refd_Env.Getter, No_Entity_Info);
      Refd_Env.State := Active;

      Referenced_Envs_Vectors.Append (Env.Referenced_Envs, Refd_Env);

      if Rebindings_Assoc then
         if Env.Rebindings_Assoc_Ref_Env /= -1 then
            raise Property_Error with
               "Env already has a rebindings associated reference env";
         end if;

         Env.Rebindings_Assoc_Ref_Env := Env.Referenced_Envs.Last_Index;
      end if;

      Invalidate_Cache (Env);
   end Reference;

   ---------------
   -- Reference --
   ---------------

   procedure Reference
     (Self             : Lexical_Env;
      To_Reference     : Lexical_Env;
      Kind             : Ref_Kind := Normal;
      Categories       : Ref_Categories := All_Cats;
      Rebindings_Assoc : Boolean := False)
   is
      Env : constant Lexical_Env_Access := Unwrap (Self);
      Ref : constant Referenced_Env :=
        (Kind, Simple_Env_Getter (To_Reference), False, Active, Categories);
   begin
      if Has_Trace then
         Rec.Trace ("REFERENCE" & Env_Image (Self));
      end if;

      if Self = Empty_Env then
         return;
      end if;
      Referenced_Envs_Vectors.Append (Env.Referenced_Envs, Ref);
      if Rebindings_Assoc then
         Env.Rebindings_Assoc_Ref_Env := Env.Referenced_Envs.Last_Index;
      end if;
      Invalidate_Cache (Env);
   end Reference;

   ------------------
   -- Get_Internal --
   ------------------

   procedure Get_Internal
     (Self          : Lexical_Env;
      Key           : Symbol_Type;
      Lookup_Kind   : Lookup_Kind_Type := Recursive;
      Rebindings    : Env_Rebindings := null;
      Metadata      : Node_Metadata := Empty_Metadata;
      Categories    : Ref_Categories;
      Local_Results : in out Lookup_Result_Vector)
   is
      Dummy : Boolean := False;
   begin
      Get_Internal_Impl
        (Self, Key, Lookup_Kind, Rebindings,
         Metadata, Categories, Local_Results,
         Toplevel                => True,
         Recursive_Check_Reached => Dummy);
   end Get_Internal;

   -----------------------
   -- Get_Internal_Impl --
   -----------------------

   procedure Get_Internal_Impl
     (Self                    : Lexical_Env;
      Key                     : Symbol_Type;
      Lookup_Kind             : Lookup_Kind_Type := Recursive;
      Rebindings              : Env_Rebindings := null;
      Metadata                : Node_Metadata := Empty_Metadata;
      Categories              : Ref_Categories;
      Local_Results           : in out Lookup_Result_Vector;
      Toplevel                : Boolean := True;
      Recursive_Check_Reached : in out Boolean)
   is
      function Do_Cache return Boolean
      is
        (Has_Lookup_Cache (Self)
         and then
           (Lookup_Cache_Mode = Full
            or else (Lookup_Cache_Mode = Toplevel_Only and then Toplevel)));
      --  Return whether to cache a particular request or not

      function Log_Id return String is
        ("env=" & Env_Image (Self) & ", key=" & Image (Key.all));

      Env : constant Lexical_Env_Access := Unwrap (Self);

      Outer_Results :  Lookup_Result_Vector :=
        Lookup_Result_Item_Vectors.Empty_Vector;
      Need_Cache    : Boolean := False;

      Current_Rebindings : Env_Rebindings;

      procedure Get_Refd_Nodes (Self : in out Referenced_Env);

      procedure Append_Result
        (Node         : Internal_Map_Node;
         Md           : Node_Metadata;
         Rebindings   : Env_Rebindings;
         From_Rebound : Boolean := False);
      --  Add E to results, if it passes the Can_Reach filter. Return whether
      --  result was appended or not.

      use Internal_Envs;

      procedure Append_All_Nodes
        (Self : Lexical_Env; From_Rebound : Boolean);
      --  Add all nodes of the given Env to the results. Make sure the output
      --  is deterministic using a sorting procedure.

      function Get_Nodes
        (Self : Lexical_Env; From_Rebound : Boolean := False) return Boolean;
      --  Lookup for matching nodes in Env's internal map and append them to
      --  Local_Results. Return whether we found some.

      procedure Recurse
        (Self                    : Lexical_Env;
         Key                     : Symbol_Type;
         Lookup_Kind             : Lookup_Kind_Type := Recursive;
         Rebindings              : Env_Rebindings := null;
         Metadata                : Node_Metadata := Empty_Metadata;
         Categories              : Ref_Categories;
         Local_Results           : in out Lookup_Result_Vector);
      --  Helper procedure for all env lookup recursions, so that the Toplevel
      --  argument is automatically passed to False for recursive calls, and
      --  ``Recursive_Check_Reached`` is automatically passed down the call
      --  chain.
      --
      --  NOTE: Every recursive call to ``Get_Internal_Impl`` should go through
      --  this helper.

      -------------
      -- Recurse --
      -------------

      procedure Recurse
        (Self                    : Lexical_Env;
         Key                     : Symbol_Type;
         Lookup_Kind             : Lookup_Kind_Type := Recursive;
         Rebindings              : Env_Rebindings := null;
         Metadata                : Node_Metadata := Empty_Metadata;
         Categories              : Ref_Categories;
         Local_Results           : in out Lookup_Result_Vector)
      is
      begin
         Get_Internal_Impl
           (Self, Key, Lookup_Kind, Rebindings, Metadata,
            Categories, Local_Results,
            Toplevel                => False,
            Recursive_Check_Reached => Recursive_Check_Reached);
      end Recurse;

      ----------------------
      -- Append_All_Nodes --
      ----------------------

      procedure Append_All_Nodes
        (Self : Lexical_Env; From_Rebound : Boolean)
      is
         Env : constant Lexical_Env_Access := Unwrap (Self);

         type Cursor_Array is array
           (Natural range <>) of Internal_Envs.Cursor;

         function "<" (A, B : Internal_Envs.Cursor) return Boolean is
           (Internal_Envs.Key (A).all < Internal_Envs.Key (B).all);

         procedure Cursor_Array_Sort is new Ada.Containers.Generic_Array_Sort
           (Index_Type   => Natural,
            Element_Type => Internal_Envs.Cursor,
            Array_Type   => Cursor_Array);

         All_Elements : Cursor_Array (1 .. Natural (Env.Map.Length));

         I : Positive := All_Elements'First;
      begin
         for C in Env.Map.Iterate loop
            All_Elements (I) := C;
            I := I + 1;
         end loop;

         Cursor_Array_Sort (All_Elements);

         for C of All_Elements loop
            declare
               I_Nodes : constant Internal_Map_Node_Vectors.Vector :=
                  Internal_Envs.Element (C).Native_Nodes;
               F_Nodes : constant Internal_Map_Node_Map :=
                  Internal_Envs.Element (C).Foreign_Nodes;
            begin

               --  Append internal nodes
               for I in reverse I_Nodes.First_Index .. I_Nodes.Last_Index loop
                  Append_Result
                    (I_Nodes.Get (I), Metadata, Current_Rebindings,
                     From_Rebound);
               end loop;

               --  Append foreign nodes
               if F_Nodes /= null then
                  for C in F_Nodes.Iterate loop
                     Append_Result
                       (Internal_Map_Node_Maps.Element (C),
                        Metadata, Current_Rebindings,
                        From_Rebound);
                  end loop;
               end if;
            end;
         end loop;
      end Append_All_Nodes;

      ---------------
      -- Get_Nodes --
      ---------------

      function Get_Nodes
        (Self : Lexical_Env; From_Rebound : Boolean := False) return Boolean
      is
         Env : constant Lexical_Env_Access := Unwrap (Self);
         Map : Internal_Map;
         C   : Cursor;
      begin
         if Self.Kind = Static_Primary then
            Map := Env.Map;
            C := Internal_Envs.No_Element;

            if Env.Map /= null then

               --  If Key is null, we want to get every entity stored in the
               --  map regardless of the symbol.
               if Key = null then
                  Append_All_Nodes (Self, From_Rebound);
                  return True;
               end if;

               --  Else, find the elements in the map corresponding to Key
               C := Map.Find (Key);
            end if;

            if Has_Element (C) then
               declare
                  E : Internal_Map_Element renames Reference (Map.all, C);
               begin
                  --  First add nodes that belong to the same environment as
                  --  Self.  Add them in reverse order, so that last inserted
                  --  results are returned first.
                  for I in reverse 1 .. E.Native_Nodes.Last_Index loop
                     Append_Result
                       (E.Native_Nodes.Get (I),
                        Metadata, Current_Rebindings, From_Rebound);
                  end loop;

                  --  Then add foreign nodes: just iterate on the ordered map
                  if E.Foreign_Nodes /= null then
                     for Pos in E.Foreign_Nodes.Iterate loop
                        Append_Result
                          (Internal_Map_Node_Maps.Element (Pos),
                           Metadata, Current_Rebindings, From_Rebound);
                     end loop;
                  end if;
               end;
               return True;
            end if;

         else
            pragma Assert (Self.Kind = Dynamic_Primary);
            declare
               --  Query the dynamic list of associations for this env
               Assocs : Inner_Env_Assoc_Array :=
                  Env.Assocs_Getter.all ((Env.Node, No_Entity_Info));
               A      : Inner_Env_Assoc;

            begin
               for I in 1 .. Length (Assocs) loop
                  --  For each individual assoc: add it if Key is null, or only
                  --  assocs with matching symbols.
                  A := Get (Assocs, I);
                  if Key = null or else Key = Get_Key (A) then
                     declare
                        IMN : constant Internal_Map_Node :=
                          (Get_Node (A), Get_Metadata (A), Env.Assoc_Resolver);
                     begin
                        Append_Result
                          (IMN, Metadata, Current_Rebindings, From_Rebound);
                     end;
                  end if;
               end loop;

               Dec_Ref (Assocs);
            end;
         end if;

         return False;
      end Get_Nodes;

      -------------------
      -- Append_Result --
      -------------------

      procedure Append_Result
        (Node         : Internal_Map_Node;
         Md           : Node_Metadata;
         Rebindings   : Env_Rebindings;
         From_Rebound : Boolean := False)
      is
         E : constant Entity :=
           (Node => Node.Node,
            Info => (Md           => Combine (Node.Md, Md),
                     Rebindings   => Rebindings,
                     From_Rebound => From_Rebound));
      begin

         if Has_Trace then
            Rec.Trace
              ("Found " & Image (Node_Text_Image (E.Node, False))
               & " from_rebound => " & From_Rebound'Img);
         end if;

         declare
            Resolved_Entity : Entity :=
              (if Node.Resolver = null
               then E
               else Node.Resolver.all (E));
         begin
            --  Silently discard null resolved nodes: we tolerate them as they
            --  likely come from semantic analysis routines running on invalid
            --  code.

            if Resolved_Entity.Node = No_Node then
               return;
            end if;

            Resolved_Entity.Info.From_Rebound := From_Rebound;
            Local_Results.Append
              (Lookup_Result_Item'
                 (E                    => Resolved_Entity,
                  Filter_From          => Node.Resolver = null,
                  Override_Filter_Node => No_Node));
         end;
      end Append_Result;

      --------------------
      -- Get_Refd_Nodes --
      --------------------

      procedure Get_Refd_Nodes (Self : in out Referenced_Env) is
         Env : Lexical_Env := Empty_Env;
         --  Make sure this holds a valid environment at all times so that the
         --  exception handler below can always call Dec_Ref on it.
      begin
         --  Don't follow the referenced environment if either:
         --   * the node from which this reference starts cannot reach From;
         --   * the node that created this environment reference is a parent of
         --     From.

         if (Lookup_Kind /= Recursive and then Self.Kind /= Transitive)
           or else Self.Being_Visited
           or else Self.State = Inactive
         then
            --  We reached a loop in the env graph, propagate the information
            --  back to callers.
            Recursive_Check_Reached := True;
            return;
         end if;

         if Self.Categories /= All_Cats
            and then Categories /= All_Cats
            and then not (for some C
                          of Ref_Categories'(Categories and Self.Categories)
                          => C)
         then
            return;
         end if;

         Self.Being_Visited := True;

         --  Get the env for the referenced env getter. Pass the metadata and
         --  the relevant rebindings.
         Env := Get_Env
           (Self.Getter, Entity_Info'(Metadata, Rebindings, False));

         --  TODO: Do not create a temp vector here, rather keep track of prior
         --  size, and only modify appended elements if the getter is dynamic.
         declare
            Refd_Results : Lookup_Result_Vector;
         begin
            Recurse
              (Env, Key,
               Lookup_Kind   =>
                 (if Lookup_Kind = Recursive and then Self.Kind = Transitive
                  then Recursive
                  elsif Lookup_Kind = Minimal
                  then raise System.Assertions.Assert_Failure
                       with "Should not happen"
                  else Flat),

               --  We do not force the propagation of rebindings here:
               --  the call to ``Get_Env`` above also receives them, and
               --  therefore can decide itself how they should be propagated
               --  by returning a Rebound_Env. If it doesn't return a
               --  Rebound_Env, we propagate them normally.
               Rebindings    =>
                 (if Env.Kind in Rebound
                  then null
                  else Shed_Rebindings (Env, Current_Rebindings)),

               Metadata      => Metadata,
               Categories    => Categories,
               Local_Results => Refd_Results);

            if Self.Getter.Dynamic then
               for Res of Refd_Results loop
                  declare
                     New_Res : Lookup_Result_Item := Res;
                  begin
                     New_Res.Override_Filter_Node := Self.Getter.Node;
                     Local_Results.Append (New_Res);
                  end;
               end loop;
            else
               Local_Results.Concat (Refd_Results);
            end if;

            Refd_Results.Destroy;
         end;

         Self.Being_Visited := False;
         Dec_Ref (Env);

      exception
         when others =>
            --  Make sure that we always Dec_Ref the returned environment so we
            --  don't leak in case of error.
            Dec_Ref (Env);
            Self.Being_Visited := False;
            raise;
      end Get_Refd_Nodes;

      Extracted : Lexical_Env;

      Res_Key           : constant Lookup_Cache_Key :=
        (Key, Rebindings, Metadata, Categories);
      Cached_Res_Cursor : Lookup_Cache_Maps.Cursor;
      Res_Val           : Lookup_Cache_Entry;
      Inserted, Dummy   : Boolean;
      use Lookup_Cache_Maps;

      Found_Rebinding : Boolean := False;

   begin
      if Self in Empty_Env then
         return;
      end if;

      if Has_Trace then
         Rec.Trace
           ("GET_INTERNAL "
            & Log_Id
            & " lookup kind = " & Lookup_Kind_Type'Image (Lookup_Kind));
      end if;

      case Self.Kind is
         when Orphaned =>
            Recurse
              (Env.Orphaned_Env, Key, Flat, Rebindings, Metadata,
               Categories, Local_Results);
            return;

         when Grouped =>
            --  Just concatenate lookups for all grouped environments
            if Has_Trace then
               Rec.Increase_Indent;
            end if;

            declare
               Md : constant Node_Metadata :=
                  Combine (Env.Default_Md, Metadata);
            begin
               for E of Env.Grouped_Envs.all loop
                  Recurse (E, Key, Lookup_Kind, Rebindings, Md,
                           Categories, Local_Results);
               end loop;
            end;

            if Has_Trace then
               Rec.Decrease_Indent;
            end if;

            return;

         when Rebound =>
            Recurse
              (Env.Rebound_Env, Key, Lookup_Kind,
               Combine (Env.Rebindings, Rebindings),
               Metadata, Categories, Local_Results);
            return;

         when Primary_Kind =>
            --  Handled below to avoid extra nesting levels
            null;
      end case;

      --  At this point, we know that Self is a primary lexical environment

      if Do_Cache
        and then Lookup_Kind = Recursive
      then

         if not Is_Lookup_Cache_Valid (Self) then
            Reset_Lookup_Cache (Self);
         end if;

         declare
            Val : constant Lookup_Cache_Entry :=
              (Computing, Empty_Lookup_Result_Vector);
         begin
            Env.Lookup_Cache.Insert
              (Res_Key, Val, Cached_Res_Cursor, Inserted);
         end;

         if Inserted then
            Need_Cache := True;
            Outer_Results := Local_Results;
            Local_Results := Lookup_Result_Item_Vectors.Empty_Vector;
         else

            Res_Val := Element (Cached_Res_Cursor);

            if Has_Trace then
               Caches_Trace.Trace
                 ("CACHE ENTRY " & Log_Id & ": "
                  & Lookup_Cache_Entry_State'Image (Res_Val.State));
            end if;

            case Res_Val.State is
               when Computing =>
                  --  When the cache state is "Computing", it means that we're
                  --  doing a given env request inside the same env request.
                  --  This is a thing that legitimately happens in some cases,
                  --  and in this case you need to return the correct results,
                  --  not using the cache mechanism at all. So we just bail out
                  --  of the caching setup, and this will behave like a no
                  --  cache request.
                  null;
               when Computed =>
                  Local_Results.Concat (Res_Val.Elements);
                  return;
               when None =>
                  Need_Cache := True;
                  Outer_Results := Local_Results;
                  Local_Results := Lookup_Result_Item_Vectors.Empty_Vector;
            end case;
         end if;
      end if;

      --  If there is an environment corresponding to Self in env rebindings,
      --  we'll get it here. We'll also shed it from the set of current
      --  rebindings.

      Current_Rebindings := Rebindings;

      --  Phase 1: Get nodes in own env if there are any

      Extracted :=
         Extract_Rebinding (Current_Rebindings, Self, Found_Rebinding);
      if not Get_Nodes (Extracted, Found_Rebinding)
         and then Extracted /= Self
      then
         --  Getting the nodes in Self (env before extract rebinding) should
         --  still have the proper env rebindings, so we do Get_Nodes in the
         --  context of Old rebindings. TODO??? This code is kludgy ugly. There
         --  might be a better way, for example, passing rebindings to
         --  Get_Nodes explicitly.
         declare
            Tmp : Env_Rebindings;
         begin
            Tmp := Current_Rebindings;
            Current_Rebindings := Rebindings;
            Dummy := Get_Nodes (Self);
            Current_Rebindings := Tmp;
         end;
      end if;

      if Lookup_Kind /= Minimal then

         --  Phase 2: Get nodes in transitive and prioritary referenced envs

         if Self.Kind = Static_Primary then
            for I in Env.Referenced_Envs.First_Index
                  .. Env.Referenced_Envs.Last_Index
            loop
               if Env.Referenced_Envs.Get_Access (I).Kind
               in Transitive | Prioritary
               then
                  Get_Refd_Nodes (Env.Referenced_Envs.Get_Access (I).all);
               end if;
            end loop;
         end if;

         --  Phase 3: Get nodes in parent envs

         if Lookup_Kind = Recursive or else Env.Transitive_Parent then
            declare
               Parent_Env        : Lexical_Env := Parent (Self);
               Parent_Rebindings : constant Env_Rebindings :=
                 Shed_Rebindings (Parent_Env, Current_Rebindings);
            begin
               if Has_Trace then
                  Rec.Trace ("Recursing on parent environments");
                  Rec.Increase_Indent;
               end if;

               Recurse
                 (Parent_Env, Key, Lookup_Kind,
                  Parent_Rebindings,
                  Metadata, Categories, Local_Results);
               if Has_Trace then
                  Rec.Decrease_Indent;
               end if;

               Dec_Ref (Parent_Env);
            end;
         end if;

         --  Phase 4: Get nodes in normal referenced envs

         if Self.Kind = Static_Primary and then Lookup_Kind = Recursive then
            if Has_Trace then
               Rec.Trace
                 ("Recursing on non transitive referenced environments");
               Rec.Increase_Indent;
            end if;

            for I in Env.Referenced_Envs.First_Index
                  .. Env.Referenced_Envs.Last_Index
            loop
               if Env.Referenced_Envs.Get_Access (I).Kind
               not in Transitive | Prioritary
               then
                  Get_Refd_Nodes (Env.Referenced_Envs.Get_Access (I).all);
               end if;
            end loop;

            if Has_Trace then
               Rec.Decrease_Indent;
            end if;
         end if;
      end if;

      Dec_Ref (Extracted);

      if Do_Cache
        and then Lookup_Kind = Recursive
        and then Need_Cache
      then

         --  Only cache if there was no loop in the env graph (see comment on
         --  ``Get_Internal_Impl``).

         if Recursive_Check_Reached then
            Env.Lookup_Cache.Include
              (Res_Key, (None, Empty_Lookup_Result_Vector));

            Outer_Results.Concat (Local_Results);
            --  We won't keep the local results in the cache, so destroy them
            Local_Results.Destroy;
            Local_Results := Outer_Results;

         else
            if Has_Trace then
               Caches_Trace.Trace ("INSERTING CACHE ENTRY " & Log_Id);
            end if;

            Env.Lookup_Cache.Include (Res_Key, (Computed, Local_Results));
            Outer_Results.Concat (Local_Results);
            Local_Results := Outer_Results;
         end if;

      end if;

   exception
      when Exc : others =>

         --  If ``Exc`` is an exception that properties are allowed to raise,
         --  free the locally allocated resources and propagate the exception.
         --  All bets are off for other kinds of exceptions: just propagate in
         --  that case.

         if Properties_May_Raise (Exc) then
            Local_Results.Destroy;
         end if;

         raise;
   end Get_Internal_Impl;

   function Get
     (Self        : Lexical_Env;
      Key         : Symbol_Type;
      From        : Node_Type := No_Node;
      Lookup_Kind : Lookup_Kind_Type := Recursive;
      Categories  : Ref_Categories := All_Cats) return Entity_Array
   is
      Vec : Entity_Vectors.Vector :=
        Get (Self, Key, From, Lookup_Kind, Categories);
   begin
      return Ret : constant Entity_Array := Vec.To_Array do
         Vec.Destroy;
      end return;
   end Get;

   ---------
   -- Get --
   ---------

   function Get
     (Self        : Lexical_Env;
      Key         : Symbol_Type;
      From        : Node_Type := No_Node;
      Lookup_Kind : Lookup_Kind_Type := Recursive;
      Categories  : Ref_Categories := All_Cats)
      return Entity_Vectors.Vector
   is
      FV : Entity_Vectors.Vector;
   begin
      if Has_Trace then
         Me.Trace
           ("===== In Env get, key=" & Image (Key)
            & ", env=" & Lexical_Env_Image (Self, Dump_Content => False)
            & " =====");
         Me.Increase_Indent;
      end if;

      declare
         Results : Lookup_Result_Vector;
      begin
         Get_Internal
           (Self, Key, Lookup_Kind, null, Empty_Metadata, Categories, Results);

         for El of Results loop
            if From = No_Node
              or else (if El.Override_Filter_Node /= No_Node
                       then Can_Reach (El.Override_Filter_Node, From)
                       else Can_Reach (El.E.Node, From))
              or else not El.Filter_From
            then
               FV.Append (El.E);
            end if;
         end loop;

         if Has_Trace then
            Me.Trace ("Returning vector " & Entity_Vectors_Image (FV));
         end if;

         if Min.Active then
            Min.Trace
              ("Env.Get("
               & (if Self.Kind = Static_Primary
                  then Image
                    (Node_Text_Image (Lexical_Env_Record (Self.Env.all).Node))
                  else Env_Image (Self))
               & ", " & Image (Key.all) & ") -> " & Entity_Vectors_Image (FV));
         end if;

         if Has_Trace then
            Me.Decrease_Indent;
            Me.Trace ("===== Out Env get =====");
         end if;

         Results.Destroy;
         return FV;
      end;
   end Get;

   ---------------
   -- Get_First --
   ---------------

   function Get_First
     (Self        : Lexical_Env;
      Key         : Symbol_Type;
      From        : Node_Type := No_Node;
      Lookup_Kind : Lookup_Kind_Type := Recursive;
      Categories  : Ref_Categories := All_Cats) return Entity
   is
      FV : Entity_Vectors.Vector;
   begin

      if Has_Trace then
         Me.Trace ("==== In Env Get_First, key=" & Image (Key) & " ====");
         Me.Increase_Indent;
      end if;

      declare
         V : Lookup_Result_Vector;
      begin
         Get_Internal
           (Self, Key, Lookup_Kind, null, Empty_Metadata, Categories, V);

         for El of V loop
            if From = No_Node
              or else (if El.Override_Filter_Node /= No_Node
                       then Can_Reach (El.Override_Filter_Node, From)
                       else Can_Reach (El.E.Node, From))
              or else not El.Filter_From
            then
               FV.Append (El.E);
            end if;
         end loop;
         V.Destroy;

         if Has_Trace then
            Me.Trace ("Returning vector with length " & FV.Length'Image);
         end if;

         return Ret : constant Entity :=
           (if FV.Length > 0 then FV.First_Element
            else (No_Node, No_Entity_Info))
         do
            FV.Destroy;
            if Has_Trace then
               Me.Decrease_Indent;
               Me.Trace ("===== Out Env Get_First =====");
            end if;
         end return;
      end;
   end Get_First;

   ------------
   -- Orphan --
   ------------

   function Orphan (Self : Lexical_Env) return Lexical_Env is

      procedure Check_Valid (Self : Lexical_Env);
      --  Raise a property error if we can't create an orphan for Env. Do
      --  nothing otherwise.

      -----------------
      -- Check_Valid --
      -----------------

      procedure Check_Valid (Self : Lexical_Env) is
         Env : constant Lexical_Env_Access := Unwrap (Self);
      begin
         case Env.Kind is
            when Primary_Kind =>
               if Env.Transitive_Parent then
                  raise Property_Error with
                     "Cannot create an orphan for an environment with a"
                     & " transitive parent";
               end if;

            when Orphaned => null;

            when Grouped =>
               raise Property_Error with
                  "Cannot create an orphan for a grouped environment";

            when Rebound =>
               Check_Valid (Env.Rebound_Env);
         end case;
      end Check_Valid;

   begin
      Check_Valid (Self);

      --  If Self is already an orphan, don't create yet another lexical env
      --  wrapper: just return Self itself.
      Inc_Ref (Self);
      return (if Self.Kind = Orphaned
              then Self
              else Wrap (new Lexical_Env_Record'
                          (Kind         => Orphaned,
                           Ref_Count    => <>,
                           Orphaned_Env => Self),
                         Owner => Self.Owner));
   end Orphan;

   -----------
   -- Group --
   -----------

   function Group
     (Envs    : Lexical_Env_Array;
      With_Md : Node_Metadata := Empty_Metadata) return Lexical_Env
   is
      V : Lexical_Env_Vectors.Vector;

      procedure Append_Envs (E : Lexical_Env);
      --  Append envs from E to the envs vector. This takes care of flattening
      --  potential grouped envs.

      function Already_Has (E : Lexical_Env) return Boolean;
      --  Returns whether the results already contain E

      -----------------
      -- Already_Has --
      -----------------

      function Already_Has (E : Lexical_Env) return Boolean is
      begin
         for El of V loop
            if Equivalent (El, E) then
               return True;
            end if;
         end loop;
         return False;
      end Already_Has;

      -----------------
      -- Append_Envs --
      -----------------

      procedure Append_Envs (E : Lexical_Env) is
      begin
         --  Flatten grouped envs only when we don't lose the `default_md`
         --  field of a nested grouped env.
         if E.Kind in Grouped
            and then Unwrap (E).Default_Md in Empty_Metadata | With_Md
         then
            for C of Unwrap (E).Grouped_Envs.all loop
               Append_Envs (C);
            end loop;
         elsif not Already_Has (E) then
            Inc_Ref (E);
            V.Append (E);
         end if;
      end Append_Envs;
   begin
      if Envs'Length = 0 then
         return Empty_Env;
      end if;

      for E of Envs loop
         Append_Envs (E);
      end loop;

      return L : constant Lexical_Env := Wrap
        (new Lexical_Env_Record'
           (Kind         => Grouped,
            Ref_Count    => <>,
            Grouped_Envs =>
               new Lexical_Env_Array'(Lexical_Env_Array (V.To_Array)),
            Default_Md   => With_Md))
      do
         Lexical_Env_Vectors.Destroy (V);
      end return;
   end Group;

   ----------------
   -- Rebind_Env --
   ----------------

   function Rebind_Env
     (Base_Env : Lexical_Env; Rebindings : Env_Rebindings) return Lexical_Env
   is
   begin
      --  If no rebindings were passed, just return the original base env
      Inc_Ref (Base_Env);
      return (if Rebindings = null
              then Base_Env
              else Wrap (new Lexical_Env_Record'
                           (Kind               => Rebound,
                            Ref_Count          => <>,
                            Rebound_Env        => Base_Env,
                            Rebindings         => Rebindings,
                            Rebindings_Version => Rebindings.Version),
                         Owner => Base_Env.Owner));
   end Rebind_Env;

   ----------------
   -- Rebind_Env --
   ----------------

   function Rebind_Env
     (Base_Env : Lexical_Env; E_Info : Entity_Info) return Lexical_Env is
   begin
      return Rebind_Env (Base_Env, E_Info.Rebindings);
   end Rebind_Env;

   -------------
   -- Destroy --
   -------------

   procedure Destroy (Self : in out Lexical_Env) is
      Env : Lexical_Env_Access := Unwrap (Self);

      procedure Free is new Ada.Unchecked_Deallocation
        (Lexical_Env_Record, Lexical_Env_Access);
      procedure Free is new Ada.Unchecked_Deallocation
        (Lexical_Env_Array, Lexical_Env_Array_Access);

      procedure Free is new Ada.Unchecked_Deallocation
        (Internal_Map_Node_Maps.Map, Internal_Map_Node_Map);
   begin
      if Self in Null_Lexical_Env | Empty_Env then
         return;
      end if;

      case Self.Kind is
         when Primary_Kind =>
            --  Release the reference to the parent environment
            Dec_Ref (Env.Parent);

            --  Release the pool of rebindings
            Destroy (Env.Rebindings_Pool);

            if Self.Kind = Static_Primary then
               --  Release referenced environments
               for Ref_Env of Env.Referenced_Envs loop
                  declare
                     Getter : Env_Getter := Ref_Env.Getter;
                  begin
                     Dec_Ref (Getter);
                  end;
               end loop;
               Env.Referenced_Envs.Destroy;

               --  Release the internal map. Don't assume it was allocated, as
               --  it's convenient for testing not to allocate it.
               if Env.Map /= null then
                  for Element of Env.Map.all loop
                     Internal_Map_Node_Vectors.Destroy (Element.Native_Nodes);
                     Free (Element.Foreign_Nodes);
                  end loop;
                  Destroy (Env.Map);
               end if;

               --  Release the lookup cache
               Reset_Lookup_Cache (Self);
            end if;

         when Orphaned =>
            Dec_Ref (Env.Orphaned_Env);

         when Grouped =>
            for E of Env.Grouped_Envs.all loop
               Dec_Ref (E);
            end loop;
            Free (Env.Grouped_Envs);

         when Rebound =>
            Dec_Ref (Env.Rebound_Env);
      end case;

      Free (Env);
      Self := Null_Lexical_Env;
   end Destroy;

   -------------
   -- Inc_Ref --
   -------------

   procedure Inc_Ref (Self : Lexical_Env) is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      if Self.Kind not in Primary_Kind then
         Env.Ref_Count := Env.Ref_Count + 1;
      end if;
   end Inc_Ref;

   -------------
   -- Dec_Ref --
   -------------

   procedure Dec_Ref (Self : in out Lexical_Env) is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      if Self.Kind in Primary_Kind then
         return;
      end if;

      Env.Ref_Count := Env.Ref_Count - 1;
      if Env.Ref_Count = 0 then
         Destroy (Self);
      end if;
      Self := Null_Lexical_Env;
   end Dec_Ref;

   function Pop (Rebindings : Env_Rebindings) return Env_Rebindings is
     (if Rebindings = null then null else Rebindings.Parent);

   -----------------------
   -- Extract_Rebinding --
   -----------------------

   function Extract_Rebinding
     (Rebindings  : in out Env_Rebindings;
      Rebound_Env : Lexical_Env;
      Found       : out Boolean) return Lexical_Env
   is
      Return_Env : Lexical_Env := Rebound_Env;
   begin
      Found := False;

      if Rebindings /= null then
         --  Look for a rebinding in the chain whose Old_Env is Rebound_Env.
         --  The correct behavior is to extract the *last* rebinding, because
         --  due to the stacked nature of rebindings, the user is never
         --  supposed to extract a rebinding other than the last one that was
         --  added.
         --
         --  TODO: We're still doing a full for-loop to check if this invariant
         --  is consistently respected. This means that the case where the env
         --  is not found is linear, which is probably the most common case.
         --  Ideally we would have the loop only in debug builds.

         declare
            R : Env_Rebindings := Rebindings;
         begin
            while R /= null loop
               declare
                  R_Old_Env : constant Lexical_Env := R.Old_Env;
               begin
                  if Rebound_Env = R_Old_Env then
                     Return_Env := R.New_Env;
                     Found := True;

                     --  Extracted rebinding *must* be the last one
                     if R /= Rebindings then
                        raise Property_Error with "Incorrect rebindings";
                     end if;
                     exit;
                  end if;
               end;
               R := R.Parent;
            end loop;
         end;
      end if;

      Inc_Ref (Return_Env);

      --  If we found the rebinding to extract, create the new rebindings set
      if Return_Env /= Rebound_Env then
         Rebindings := Pop (Rebindings);
      end if;

      return Return_Env;
   end Extract_Rebinding;

   ---------------------
   -- Shed_Rebindings --
   ---------------------

   function Shed_Rebindings
     (From_Env   : Lexical_Env;
      Rebindings : Env_Rebindings) return Env_Rebindings
   is
      First_Rebindable_Parent : Lexical_Env;
      Assoc_Ref_Env           : Lexical_Env;
      Result                  : Env_Rebindings := Rebindings;
   begin
      --  If there is no bindings, nothing to do here
      if Rebindings = null then
         return null;
      end if;

      --  Look for the first environment in From_Env's parent chain whose Node
      --  is rebindable. Use null if there is no such env.
      First_Rebindable_Parent := From_Env;
      Inc_Ref (First_Rebindable_Parent);
      while
         First_Rebindable_Parent /= Empty_Env
         and then
           (Env_Node (First_Rebindable_Parent) = No_Node
            or else not Is_Rebindable (Env_Node (First_Rebindable_Parent)))
      loop

         --  Search for an environment that would be associated to this one and
         --  that is rebindable.
         if First_Rebindable_Parent.Kind in Primary_Kind
            and then
            Unwrap (First_Rebindable_Parent).Rebindings_Assoc_Ref_Env /= -1
         then
            Assoc_Ref_Env := Get_Env
              (Unwrap (First_Rebindable_Parent).Referenced_Envs.Get_Access
                 (Unwrap (First_Rebindable_Parent).Rebindings_Assoc_Ref_Env)
               .Getter, No_Entity_Info);

            declare
               N : Node_Type renames Env_Node (Assoc_Ref_Env);
            begin
               if N /= No_Node and then Is_Rebindable (N) then
                  Dec_Ref (First_Rebindable_Parent);
                  First_Rebindable_Parent := Assoc_Ref_Env;
                  exit;
               end if;
            end;
         end if;

         declare
            Next : constant Lexical_Env := Parent (First_Rebindable_Parent);
         begin
            Dec_Ref (First_Rebindable_Parent);
            First_Rebindable_Parent := Next;
         end;
      end loop;

      --  If there is no rebindable parent anywhere, it means we cannot have
      --  rebindings. In that case, shed them all, i.e. return null rebindings.
      if First_Rebindable_Parent = Empty_Env then
         return null;
      end if;

      --  If we found a rebindable parent, then we will shed all rebindings
      --  between the top of the rebinding stack and the corresponding
      --  rebinding.
      while Result /= null and then Result.Old_Env /= First_Rebindable_Parent
      loop
         Result := Result.Parent;
      end loop;

      Dec_Ref (First_Rebindable_Parent);
      return Result;
   end Shed_Rebindings;

   ---------------------
   -- Shed_Rebindings --
   ---------------------

   function Shed_Rebindings
     (E_Info : Entity_Info; Env : Lexical_Env) return Entity_Info is
   begin
      return (Md              => E_Info.Md,
              Rebindings      => Shed_Rebindings (Env, E_Info.Rebindings),
              From_Rebound => False);
   end Shed_Rebindings;

   ---------------------------------
   -- Check_Rebindings_Uniqueness --
   ---------------------------------

   procedure Check_Rebindings_Uniqueness (Self : Env_Rebindings) is
      L, R : Env_Rebindings := Self;
   begin
      while L /= null loop
         R := L.Parent;
         while R /= null loop
            if L.Old_Env = R.Old_Env then
               raise Property_Error with
                  "Old_Env present twice in rebindings";
            elsif L.New_Env = R.New_Env then
               raise Property_Error with
                  "New_Env present twice in rebindings";
            end if;
            R := R.Parent;
         end loop;
         L := L.Parent;
      end loop;
   end Check_Rebindings_Uniqueness;

   ----------------
   -- Text_Image --
   ----------------

   function Text_Image (Self : Env_Rebindings) return Text_Type is

      function Text_Image (Self : Lexical_Env) return Text_Type is
        (Node_Text_Image (Env_Node (Self)));

      function Text_Image (Self : Env_Rebindings) return Text_Type is
        (Text_Image (Self.New_Env));

   begin
      if Self = null then
         return "<null>";
      end if;
      declare
         use Env_Rebindings_Vectors;
         Rebindings_Vector : Vector := Empty_Vector;
         Cur               : Env_Rebindings := Self;
         Buffer            : Unbounded_Wide_Wide_String;
      begin
         --  Get rebindings in reverse order: older first, most recent last
         while Cur /= null loop
            Rebindings_Vector.Append (Cur);
            Cur := Cur.Parent;
         end loop;

         Append (Buffer, "[");
         for I in reverse 1 .. Rebindings_Vector.Last_Index loop
            if I < Rebindings_Vector.Last_Index then
               Append (Buffer, ", ");
            end if;
            Append (Buffer, Text_Image (Rebindings_Vector.Get (I)));
         end loop;
         Append (Buffer, "]");

         Rebindings_Vector.Destroy;
         return To_Wide_Wide_String (Buffer);
      end;
   end Text_Image;

   ----------------
   -- Equivalent --
   ----------------

   function Equivalent (Left, Right : Lexical_Env) return Boolean is
      L : constant Lexical_Env_Access := Unwrap (Left);
      R : constant Lexical_Env_Access := Unwrap (Right);
   begin
      if L = R then
         return True;
      elsif L.Kind /= R.Kind then
         return False;
      end if;

      case L.Kind is
         when Primary_Kind =>
            --  If we have two primary environments, don't go through
            --  structural comparison: we can use pointer equality as each
            --  instance has its own identity.
            return False;

         when Orphaned =>
            return Equivalent (L.Orphaned_Env, R.Orphaned_Env);

         when Grouped =>
            return (L.Grouped_Envs'Length = R.Grouped_Envs'Length
                    and then L.Default_Md = R.Default_Md
                    and then (for all I in L.Grouped_Envs'Range =>
                              Equivalent (L.Grouped_Envs (I),
                                          R.Grouped_Envs (I))));

         when Rebound =>
            return (L.Rebindings = R.Rebindings
                    and then Equivalent (L.Rebound_Env,
                                         R.Rebound_Env));
      end case;
   end Equivalent;

   ----------
   -- Hash --
   ----------

   function Hash (Env : Lexical_Env_Access) return Hash_Type is

      function Convert is new Ada.Unchecked_Conversion
        (Lexical_Env_Resolver, System.Address);
      function Hash is new Hash_Address
        (System.Word_Size / System.Storage_Unit);
      function Hash is new Hash_Access (Internal_Envs.Map, Internal_Map);
      function Hash (Getter : Env_Getter) return Hash_Type;
      function Hash (Ref : Referenced_Env) return Hash_Type;
      function Hash (Refs : Referenced_Envs_Vectors.Vector) return Hash_Type;

      ----------
      -- Hash --
      ----------

      function Hash (Getter : Env_Getter) return Hash_Type is
      begin
         case Getter.Dynamic is
            when True =>
               return Combine
                 ((1,
                   Node_Hash (Getter.Node),
                   Hash (Convert (Getter.Resolver))));
            when False =>
               return Combine (0, Hash (Getter.Env));
         end case;
      end Hash;

      ----------
      -- Hash --
      ----------

      function Hash (Ref : Referenced_Env) return Hash_Type is
      begin
         return Combine ((Ref_Kind'Pos (Ref.Kind), Hash (Ref.Getter)));
      end Hash;

      ----------
      -- Hash --
      ----------

      function Hash (Refs : Referenced_Envs_Vectors.Vector) return Hash_Type is
         Hashes : Hash_Array (1 .. Refs.Length);
      begin
         for I in Hashes'Range loop
            Hashes (I) := Hash (Refs.Get (I));
         end loop;
         return Combine (Hashes);
      end Hash;

      Base_Hash : constant Hash_Type :=
        (if Env = null
         then Initial_Hash
         else Lexical_Env_Kind'Pos (Env.Kind));
   begin
      if Env = null then
         return Base_Hash;
      end if;

      case Env.Kind is
         when Static_Primary =>
            return Combine
              ((Base_Hash,
                Hash (Env.Parent),
                (if Env.Transitive_Parent then 1 else 0),
                Node_Hash (Env.Node),
                Hash (Env.Referenced_Envs),
                Hash (Env.Map)));

         when Dynamic_Primary =>
            return Combine
              ((Base_Hash,
                Hash (Env.Parent),
                (if Env.Transitive_Parent then 1 else 0),
                Node_Hash (Env.Node)));

         when Orphaned =>
            return Combine (Base_Hash, Hash (Env.Orphaned_Env));

         when Grouped =>
            declare
               Result : Hash_Type := Base_Hash;
            begin
               for E of Env.Grouped_Envs.all loop
                  Result := Combine (Result, Hash (E));
               end loop;
               return Combine (Result, Metadata_Hash (Env.Default_Md));
            end;

         when Rebound =>
            return Combine
              ((Base_Hash, Hash (Env.Rebound_Env), Hash (Env.Rebindings)));
      end case;
   end Hash;

   --  We want lexical env dumps to be deterministic, so sort maps before
   --  iterating through their key/value pairs.

   type Env_Pair is record
      Key   : Symbol_Type;
      Value : Internal_Map_Node_Vectors.Vector;
   end record;

   function "<" (L, R : Env_Pair) return Boolean
   is (L.Key.all < R.Key.all);

   package Env_Pair_Vectors is new Gpr_Parser_Support.Vectors (Env_Pair);
   procedure Sort is new Env_Pair_Vectors.Generic_Sort;

   function To_Sorted_Env
     (Env : Internal_Envs.Map) return Env_Pair_Vectors.Vector;

   -------------------
   -- To_Sorted_Env --
   -------------------

   function To_Sorted_Env
     (Env : Internal_Envs.Map) return Env_Pair_Vectors.Vector
   is
      use Internal_Envs;
   begin
      return Vector : Env_Pair_Vectors.Vector do
         for Pos in Env.Iterate loop
            --  Append a new entry, and then fill it in place to avoid needless
            --  copying.
            Vector.Append
              ((Key (Pos), Internal_Map_Node_Vectors.Empty_Vector));
            declare
               Element : Internal_Map_Element renames
                  Env.Constant_Reference (Pos);
               V       : Internal_Map_Node_Vectors.Vector renames
                  Vector.Last_Element.all.Value;
            begin
               V.Concat (Element.Native_Nodes);
               if Element.Foreign_Nodes /= null then
                  for Node of Element.Foreign_Nodes.all loop
                     V.Append (Node);
                  end loop;
               end if;
            end;
         end loop;
         Sort (Vector);
      end return;
   end To_Sorted_Env;

   ----------
   -- Dump --
   ----------

   function Lexical_Env_Image
     (Self           : Lexical_Env;
      Env_Id         : String := "";
      Parent_Env_Id  : String := "";
      Dump_Addresses : Boolean := False;
      Dump_Content   : Boolean := True;
      Prefix         : String := "";
      Short_Node     : Boolean := False) return String
   is
      Env : constant Lexical_Env_Access := Unwrap (Self);

      Result : Unbounded_String;

      Sub_Prefix : constant String := Prefix & "  ";

      function Image (N : Node_Type) return String
      is (if N = No_Node then "<null>"
          else Image (Node_Text_Image (N, Short => Short_Node)));
      --  Wrapper around Node_Text_Image to handle null nodes.
      --
      --  TODO??? This is slightly hackish, because we're converting a wide
      --  string back to string. But since we're using this solely for
      --  test/debug purposes, it should not matter. Still, would be good to
      --  have Text_Type everywhere at some point.

      function Image (Node : Internal_Map_Node) return String is
        (Image (Node.Node));
      --  Wrapper around Node_Text_Image to format a lexical env map node

      function Image is new Internal_Map_Node_Vectors.Image (Image);

      procedure New_Arg;
      --  Helper to be called before emitting a new lexical env. "argument".
      --  Used to separate each argument with a comma in Result.

      -------------
      -- New_Arg --
      -------------

      procedure New_Arg is
      begin
         Append (Result, ", ");
      end New_Arg;

   begin
      if Self = Null_Lexical_Env then
         return "";
      end if;

      --  No matter what, emit a short description of this environment: kind,
      --  whether it's empty, parent/node/... if asked.

      if Env_Id'Length /= 0 then
         Append (Result, Env_Id & " = ");
      end if;
      Append (Result, "LexEnv(" & (case Self.Kind is
                                   when Static_Primary => "Static_Primary",
                                   when Dynamic_Primary => "Dynamic_Primary",
                                   when Orphaned => "Orphaned",
                                   when Grouped => "Grouped",
                                   when Rebound => "Rebound"));

      if Self = Empty_Env then
         New_Arg;
         Append (Result, "Empty");
      end if;

      if Self.Kind in Primary_Kind then
         if Parent_Env_Id'Length > 0 then
            New_Arg;
            Append (Result,
                    "Parent="
                    & (if Parent (Self) /= Empty_Env
                       then Parent_Env_Id else "null"));
         end if;

         if Env_Node (Self) /= No_Node then
            New_Arg;
            Append (Result, "Node="
                    & Image (Env_Node (Self)));
         end if;
      end if;

      if Dump_Addresses then
         New_Arg;
         Append (Result, "0x" & System.Address_Image (Env.all'Address));
      end if;
      Append (Result, ")");

      --  If that was all that was asked, stop here
      if not Dump_Content then
         return To_String (Result);
      end if;

      --  Otherwise, go to details...
      Append (Result, ":" & ASCII.LF);

      case Self.Kind is
         when Static_Primary =>
            declare
               Refs : Referenced_Envs_Vectors.Vector
                  renames Env.Referenced_Envs;
            begin
               for I in Refs.First_Index .. Refs.Last_Index loop
                  declare
                     G   : Env_Getter renames Refs.Get_Access (I).Getter;
                     Env : Lexical_Env := Get_Env (G, No_Entity_Info);
                  begin
                     if Env /= Empty_Env then
                        Append (Result, Sub_Prefix & "Referenced: ");
                        if G.Dynamic then
                           Append (Result, Image (G.Node) & ": ");
                        end if;

                        Append
                          (Result,
                           Lexical_Env_Image (Self           => Env,
                                              Dump_Addresses => Dump_Addresses,
                                              Dump_Content   => False));
                        Append (Result, ASCII.LF);
                        Dec_Ref (Env);
                     end if;
                  end;
               end loop;
            end;

            if Env.Map.Is_Empty then
               Append (Result, Sub_Prefix & "  <empty>" & ASCII.LF);
            else
               declare
                  V : Env_Pair_Vectors.Vector := To_Sorted_Env (Env.Map.all);
               begin
                  for I in V.First_Index .. V.Last_Index loop
                     declare
                        Pair : Env_Pair renames V.Get_Access (I).all;
                     begin
                        Append
                          (Result,
                           Sub_Prefix & "  "
                           & Gpr_Parser_Support.Text.Image (Pair.Key.all) & ": "
                           & Image (Pair.Value)
                           & ASCII.LF);
                        Internal_Map_Node_Vectors.Destroy (Pair.Value);
                     end;
                  end loop;
                  V.Destroy;
               end;
            end if;

         when Dynamic_Primary =>
            Append (Result, Sub_Prefix & "... dynamic");

         when Orphaned =>
            Append
              (Result, Sub_Prefix & "Orphaned: " & Lexical_Env_Image
                 (Self           => Env.Orphaned_Env,
                  Dump_Addresses => Dump_Addresses,
                  Dump_Content   => Dump_Content,
                  Prefix         => Sub_Prefix));

         when Grouped =>
            for E of Env.Grouped_Envs.all loop
               Append
                 (Result, Sub_Prefix & "Grouped: " & Lexical_Env_Image
                    (Self           => E,
                     Dump_Addresses => Dump_Addresses,
                     Dump_Content   => Dump_Content,
                     Prefix         => Sub_Prefix));
            end loop;

         when Rebound =>
            Append
              (Result, Sub_Prefix & "Rebindings: "
                       & Image (Text_Image (Env.Rebindings))
                       & ASCII.LF);
            Append
              (Result, Sub_Prefix & "Rebound: " & Lexical_Env_Image
                 (Self           => Env.Rebound_Env,
                  Dump_Addresses => Dump_Addresses,
                  Dump_Content   => Dump_Content,
                  Prefix         => Sub_Prefix));
      end case;

      return To_String (Result);
   end Lexical_Env_Image;

   ------------------------------
   -- Lexical_Env_Parent_Chain --
   ------------------------------

   function Lexical_Env_Parent_Chain (Self : Lexical_Env) return String is
      Id     : Positive := 1;
      E      : Lexical_Env := Self;
      Result : Unbounded_String;
   begin

      if E = Null_Lexical_Env then
         Append (Result, "<null>" & ASCII.LF);
      end if;

      while E /= Empty_Env loop
         declare
            Id_Str : constant String := '@' & Stripped_Image (Id);
         begin
            Append
              (Result,
               Lexical_Env_Image
                 (Self           => E,
                  Env_Id         => Id_Str,
                  Parent_Env_Id  => '@' & Stripped_Image (Id + 1),
                  Dump_Addresses => True));
         end;
         Id := Id + 1;
         E := Parent (E);
      end loop;
      return To_String (Result);
   end Lexical_Env_Parent_Chain;

   --------------------------
   -- Dump_One_Lexical_Env --
   --------------------------

   procedure Dump_One_Lexical_Env
     (Self           : Lexical_Env;
      Env_Id         : String := "";
      Parent_Env_Id  : String := "";
      Dump_Addresses : Boolean := False;
      Dump_Content   : Boolean := True)
   is
   begin
      Put_Line
        (Lexical_Env_Image
           (Self, Env_Id, Parent_Env_Id, Dump_Addresses, Dump_Content));
   end Dump_One_Lexical_Env;

   -----------------------------------
   -- Dump_Lexical_Env_Parent_Chain --
   -----------------------------------

   procedure Dump_Lexical_Env_Parent_Chain (Self : Lexical_Env) is
   begin
      Put_Line (Lexical_Env_Parent_Chain (Self));
   end Dump_Lexical_Env_Parent_Chain;

   ------------
   -- Parent --
   ------------

   function Parent (Self : Lexical_Env) return Lexical_Env is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      case Self.Kind is
         when Primary_Kind =>
            return (if Env.Parent = Null_Lexical_Env
                    then Empty_Env
                    else Env.Parent);
         when Orphaned =>
            return Parent (Env.Orphaned_Env);
         when Grouped =>
            return Empty_Env;
         when Rebound =>
            return Parent (Env.Rebound_Env);
      end case;
   end Parent;

   --------------
   -- Env_Node --
   --------------

   function Env_Node (Self : Lexical_Env) return Node_Type is
      Env : constant Lexical_Env_Access := Unwrap (Self);
   begin
      return (case Self.Kind is
              when Primary_Kind => Env.Node,
              when Orphaned     => Env_Node (Env.Orphaned_Env),
              when Grouped      => No_Node,
              when Rebound      => Env_Node (Env.Rebound_Env));
   end Env_Node;

   --------------------------------
   -- Deactivate_Referenced_Envs --
   --------------------------------

   procedure Deactivate_Referenced_Envs (Self : Lexical_Env) is
      Env : constant Lexical_Env_Access := Unwrap (Self);
      R   : access Referenced_Env;
   begin
      if Self in Null_Lexical_Env | Empty_Env then
         return;
      end if;

      Invalidate_Cache (Env);

      for I in Env.Referenced_Envs.First_Index
            .. Env.Referenced_Envs.Last_Index
      loop
         R := Env.Referenced_Envs.Get_Access (I);
         R.State := Inactive;
      end loop;
   end Deactivate_Referenced_Envs;

   -------------------------------
   -- Recompute_Referenced_Envs --
   -------------------------------

   procedure Recompute_Referenced_Envs (Self : Lexical_Env) is
      Env : constant Lexical_Env_Access := Unwrap (Self);
      R   : access Referenced_Env;
   begin
      if Self in Null_Lexical_Env | Empty_Env then
         return;
      end if;

      for I in Env.Referenced_Envs.First_Index
            .. Env.Referenced_Envs.Last_Index
      loop
         R := Env.Referenced_Envs.Get_Access (I);
         Resolve (R.Getter, No_Entity_Info);
         R.State := Active;
      end loop;
   end Recompute_Referenced_Envs;

   ------------------
   -- Reset_Caches --
   ------------------

   procedure Reset_Caches (Self : Lexical_Env) is
   begin
      Unwrap (Self).Lookup_Cache_Valid := False;
   end Reset_Caches;

   ----------------------
   -- Invalidate_Cache --
   ----------------------

   procedure Invalidate_Cache (Env : Lexical_Env_Access) is
   begin
      if Has_Trace and then Env.Lookup_Cache_Valid = True then
         Caches_Trace.Trace ("INVALIDATING CACHES " & Env_Image (Wrap (Env)));
      end if;

      Env.Lookup_Cache_Valid := False;
   end Invalidate_Cache;

end Gpr_Parser_Support.Lexical_Envs_Impl;