File: xref.adb

package info (click to toggle)
gnat-gps 5.3dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 50,360 kB
  • ctags: 11,617
  • sloc: ada: 374,346; ansic: 92,327; python: 15,979; xml: 12,186; sh: 3,277; makefile: 1,113; awk: 154; perl: 128; java: 17
file content (3751 lines) | stat: -rw-r--r-- 115,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
------------------------------------------------------------------------------
--                                  G P S                                   --
--                                                                          --
--                     Copyright (C) 2012-2013, AdaCore                     --
--                                                                          --
-- This is free software;  you can redistribute it  and/or modify it  under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for  more details.  You should have  received  a copy of the GNU --
-- General  Public  License  distributed  with  this  software;   see  file --
-- COPYING3.  If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license.                                                          --
------------------------------------------------------------------------------

pragma Ada_2012;

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Exceptions;            use Ada.Exceptions;
with Ada.Strings.Maps;          use Ada.Strings.Maps;
with Ada.Unchecked_Deallocation;
with ALI_Parser;
with Glib.Convert;
with GNATCOLL.Projects;         use GNATCOLL.Projects;
with GNATCOLL.SQL.Sqlite;
with GNATCOLL.Symbols;          use GNATCOLL.Symbols;
with GNATCOLL.Utils;            use GNATCOLL.Utils;
with GNAT.Strings;              use GNAT.Strings;
with Language_Handlers;         use Language_Handlers;
with Language.Tree;             use Language.Tree;
with Language.Tree.Database;    use Language.Tree.Database;
with String_Utils;
with Traces;

package body Xref is
   Me : constant Trace_Handle := Create ("Xref");
   Constructs_Heuristics : constant Trace_Handle :=
     Create ("Entities.Constructs", On);

   ---------------------------
   --  Note for development --
   ---------------------------

   --  A lot of functions defined here use either the new system
   --  (GNATCOLL.Xref) or the legacy database (Entities.*), and
   --  sometimes fallback on the constructs database.

   use type Old_Entities.Entity_Information;
   use type Old_Entities.File_Location;

   type Hash_Type is range 0 .. 2 ** 20 - 1;
   function Hash is new String_Utils.Hash (Hash_Type);

   package Entity_Lists is new Ada.Containers.Doubly_Linked_Lists
      (General_Entity);
   use Entity_Lists;

   function Get_Location
     (Ref : Entity_Reference) return General_Location;
   --  Return the General Location of a GNATCOLL reference

   procedure Node_From_Entity
     (Self        : access General_Xref_Database_Record'Class;
      Handler     : access Abstract_Language_Handler_Record'Class;
      Decl        : General_Location;
      Ent         : out Entity_Access;
      Tree_Lang   : out Tree_Language_Access);
   --  Returns the constructs data for a given entity.

   function To_String (Loc : General_Location) return String;
   --  Display Loc

   function To_General_Entity
     (E : Old_Entities.Entity_Information) return General_Entity;
   function To_General_Entity
     (Db : access General_Xref_Database_Record'Class;
      E  : Entity_Information) return General_Entity;
   --  Convert Xref.Entity_Information to General_Entity

   procedure Fill_Entity_Array
     (Curs : in out Entities_Cursor'Class;
      Arr  : in out Entity_Lists.List);
   --  Store all entities returned by the cursor into the array

   function To_Entity_Array (Arr : Entity_Lists.List) return Entity_Array;
   --  Creates an entity array.
   --  ??? This is not very efficient

   function Get_Entity_At_Location
     (Db  : access General_Xref_Database_Record'Class;
      Loc : General_Location) return Entity_Access;
   --  Return the construct entity found at the location given in parameter.

   ----------------
   -- Assistants --
   ----------------
   --  These types are used for the constructs database.

   LI_Assistant_Id : constant String := "LI_ASSISTANT";

   type LI_Db_Assistant is new Database_Assistant with record
      LI_Key : Construct_Annotations_Pckg.Annotation_Key;
      Db     : General_Xref_Database;
   end record;

   type LI_Db_Assistant_Access is access all LI_Db_Assistant'Class;

   type LI_Annotation is new
     Construct_Annotations_Pckg.General_Annotation_Record
   with record
      Entity : General_Entity;
   end record;

   overriding procedure Free (Obj : in out LI_Annotation);

   function To_LI_Entity
     (Self : access General_Xref_Database_Record'Class;
      E    : Entity_Access) return General_Entity;
   --  Return an LI entity based on a construct entity. Create one if none.

   ---------------
   -- To_String --
   ---------------

   function To_String (Loc : General_Location) return String is
   begin
      if Loc = No_Location then
         return "<no_loc>";
      else
         return Loc.File.Display_Base_Name & ':'
           & Image (Loc.Line, Min_Width => 1) & ':'
           & Image (Integer (Loc.Column), Min_Width => 1);
      end if;
   end To_String;

   -------------------
   -- Documentation --
   -------------------

   function Documentation
     (Self             : access General_Xref_Database_Record;
      Handler          : Language_Handlers.Language_Handler;
      Entity           : General_Entity;
      Raw_Format       : Boolean := False;
      Check_Constructs : Boolean := True) return String
   is
      function Doc_From_Constructs return String;
      function Doc_From_LI return String;

      Decl : constant General_Location :=
        Get_Declaration (Self, Entity).Loc;
      Context : constant Language.Language_Context_Access :=
        Language.Get_Language_Context
          (Get_Language_From_File (Handler, Source_Filename => Decl.File));

      Form : constant Formatting :=
        (if Raw_Format then Text else HTML);

      -------------------------
      -- Doc_From_Constructs --
      -------------------------

      function Doc_From_Constructs return String is
         Ent       : Entity_Access;
         Tree_Lang : Tree_Language_Access;
         Buffer    : GNAT.Strings.String_Access;
         Node      : Construct_Tree_Iterator;
      begin
         Node_From_Entity (Self, Handler, Decl, Ent, Tree_Lang);

         if Ent = Null_Entity_Access then
            return "";
         end if;

         Buffer := Get_Buffer (Get_File (Ent));
         Node   := To_Construct_Tree_Iterator (Ent);

         --  If the constructs have been properly loaded
         if Get_Construct (Node).Sloc_Start.Index /= 0 then
            declare
               Comment : constant String :=
                 Extract_Comment
                   (Buffer            => Buffer.all,
                    Decl_Start_Index  => Get_Construct (Node).Sloc_Start.Index,
                    Decl_End_Index    => Get_Construct (Node).Sloc_End.Index,
                    Language          => Context.Syntax,
                    Format            => Form);
               Profile : constant String :=
                 Get_Profile (Tree_Lang, Ent, Raw_Format => Raw_Format);

            begin
               if Comment /= "" then
                  if Profile /= "" then
                     return Glib.Convert.Escape_Text (Comment)
                       & ASCII.LF & ASCII.LF & Profile;
                  else
                     return Glib.Convert.Escape_Text (Comment);
                  end if;
               else
                  return Profile;
               end if;
            end;
         else
            return "";
         end if;
      end Doc_From_Constructs;

      -----------------
      -- Doc_From_LI --
      -----------------

      function Doc_From_LI return String is
         Buffer : GNAT.Strings.String_Access;
         Loc    : Old_Entities.File_Location;
         Result : Unbounded_String;
      begin
         if Active (SQLITE) then
            if Entity.Entity /= No_Entity then
               Append
                 (Result,
                  Glib.Convert.Escape_Text
                    (Self.Xref.Comment (Entity.Entity, Context.Syntax, Form))
                  & ASCII.LF
                  & Self.Xref.Text_Declaration (Entity.Entity, Form)
                  & ASCII.LF);

               return To_String
                 (Ada.Strings.Unbounded.Trim
                    (Result,
                     Left => Ada.Strings.Maps.Null_Set,
                     Right => Ada.Strings.Maps.To_Set
                       (' ' & ASCII.HT & ASCII.LF & ASCII.CR)));
            end if;
         else
            Buffer := Decl.File.Read_File;

            if Buffer = null then
               return "";
            end if;

            Result := To_Unbounded_String
              (Glib.Convert.Escape_Text
                 (Extract_Comment
                    (Buffer            => Buffer.all,
                     Decl_Start_Line   => Decl.Line,
                     Decl_Start_Column => Integer (Decl.Column),
                     Language          => Context.Syntax,
                     Format            => Form)));

            if Result = "" and then Entity.Old_Entity /= null then
               Find_Next_Body
                 (Entity.Old_Entity,
                  Location => Loc,
                  No_Location_If_First => True);

               if Loc /= Old_Entities.No_File_Location then
                  Free (Buffer);
                  Buffer := Old_Entities.Get_Filename (Loc.File).Read_File;
                  Result := To_Unbounded_String
                    (Extract_Comment
                       (Buffer            => Buffer.all,
                        Decl_Start_Line   => Loc.Line,
                        Decl_Start_Column => Integer (Loc.Column),
                        Language          => Context.Syntax,
                        Format            => Form));
               end if;
            end if;

            Free (Buffer);
            return To_String (Result);
         end if;

         return "";
      end Doc_From_LI;

   --  Start of processing for Documentation

   begin
      if not Check_Constructs then
         return Doc_From_LI;
      else
         declare
            R : constant String := Doc_From_Constructs;
         begin
            if R = "" then
               return Doc_From_LI;
            end if;
            return R;
         end;
      end if;

      --  If still not found, we used to default to also searching just before
      --  the body. But when there is a separate spec, the doc should be there
      --  and when we don't have a separate spec the "declaration" is the
      --  location of the body.
   end Documentation;

   -------------------------------
   -- For_Each_Dispatching_Call --
   -------------------------------

   procedure For_Each_Dispatching_Call
     (Dbase     : access General_Xref_Database_Record;
      Entity    : General_Entity;
      Ref       : General_Entity_Reference;
      On_Callee : access function
        (Callee, Primitive_Of : General_Entity) return Boolean;
      Filter    : Reference_Kind_Filter := null;
      Policy    : Dispatching_Menu_Policy)

   is
      use type Old_Entities.Reference_Kind;

      Prim_Ent  : General_Entity;
      Typ_Ent   : General_Entity;

   begin
      --  Handle cases in which no action is needed

      if Entity = No_General_Entity
        or else Policy = Never
          or else not Dbase.Is_Dispatching_Call (Ref)
      then
         return;
      end if;

      if Active (SQLITE) then
         declare
            function Tagged_Type (E : Entity_Information)
               return Entity_Information;

            function Should_Show (E : Entity_Information) return Boolean;

            function Tagged_Type (E : Entity_Information)
               return Entity_Information is
               Cursor : Entities_Cursor;
            begin
               Dbase.Xref.Method_Of (E, Cursor);
               return Dbase.Xref.Declaration
                 (Element (Cursor)).Location.Entity;
            end Tagged_Type;

            function Should_Show (E : Entity_Information) return Boolean is
               R : References_Cursor;
            begin
               if Filter = null then
                  return True;
               end if;

               Dbase.Xref.References (E, R);
               while R.Has_Element loop
                  if Filter (Dbase, (Ref => R.Element, others => <>)) then
                     return True;
                  end if;
                  R.Next;
               end loop;
               return False;
            end Should_Show;

            Cursor : Recursive_Entities_Cursor;
            Prim   : Entity_Information;

         begin
            Prim     := Entity.Entity;
            Prim_Ent := To_General_Entity (Dbase, Prim);
            Typ_Ent  := To_General_Entity (Dbase, Tagged_Type (Prim));

            if Should_Show (Prim_Ent.Entity)
              and then not On_Callee
                (Callee => Prim_Ent, Primitive_Of => Typ_Ent)
            then
               return;
            end if;

            Recursive
              (Self    => Dbase.Xref,
               Entity  => Entity.Entity,
               Compute => Overridden_By'Unrestricted_Access,
               Cursor  => Cursor);

            while Cursor.Has_Element loop
               Prim     := Cursor.Element;
               Prim_Ent := To_General_Entity (Dbase, Prim);
               Typ_Ent  := To_General_Entity (Dbase, Tagged_Type (Prim));

               exit when Should_Show (Prim_Ent.Entity)
                 and then not On_Callee
                   (Callee       => Prim_Ent,
                    Primitive_Of => Typ_Ent);

               Cursor.Next;
            end loop;

         exception
            when E : others =>
               Trace (Traces.Exception_Handle, "Unexpected exception: "
                      & Exception_Information (E));
         end;

      --  Legacy functionality

      else
         declare
            function Proxy
              (Callee, Primitive_Of : Old_Entities.Entity_Information)
               return Boolean;
            function Proxy_Filter
              (R : Old_Entities.Entity_Reference) return Boolean;

            function Proxy
              (Callee, Primitive_Of : Old_Entities.Entity_Information)
               return Boolean is
            begin
               return On_Callee (From_Old (Callee), From_Old (Primitive_Of));
            end Proxy;

            function Proxy_Filter
              (R : Old_Entities.Entity_Reference) return Boolean is
            begin
               return Filter (Dbase, (Old_Ref => R, others => <>));
            end Proxy_Filter;

            P : Old_Entities.Queries.Reference_Filter_Function := null;
            Need_Bodies : constant Boolean :=
              Filter = Reference_Is_Body'Access;

         begin

            if Filter /= null then
               P := Proxy_Filter'Unrestricted_Access;
            end if;

            Old_Entities.Queries.For_Each_Dispatching_Call
              (Entity    => Entity.Old_Entity,
               Ref       => Ref.Old_Ref,
               On_Callee => Proxy'Access,
               Filter    => P,
               Need_Bodies => Need_Bodies,
               Policy    => Policy);
         end;
      end if;
   end For_Each_Dispatching_Call;

   ----------------
   -- Get_Entity --
   ----------------

   function Get_Entity
     (Ref : General_Entity_Reference) return General_Entity
   is
      E : General_Entity;
   begin
      --  Attempt to use the sqlite system

      if Active (SQLITE)
        and then Ref.Ref /= No_Entity_Reference
      then
         E.Entity := Ref.Ref.Entity;
      end if;

      --  Fall back on the old system

      E.Old_Entity := Old_Entities.Get_Entity (Ref.Old_Ref);

      return E;
   end Get_Entity;

   ----------------
   -- Get_Entity --
   ----------------

   function Get_Entity
     (Db   : access General_Xref_Database_Record;
      Name : String;
      Loc  : General_Location) return General_Entity
   is
      Entity : General_Entity;
      Ref    : General_Entity_Reference;
   begin
      Find_Declaration_Or_Overloaded
        (General_Xref_Database (Db),
         Loc               => Loc,
         Entity_Name       => Name,
         Ask_If_Overloaded => False,
         Entity            => Entity,
         Closest_Ref       => Ref);
      return Entity;
   end Get_Entity;

   ------------------------------------
   -- Find_Declaration_Or_Overloaded --
   ------------------------------------

   procedure Find_Declaration_Or_Overloaded
     (Self              : access General_Xref_Database_Record;
      Loc               : General_Location;
      Entity_Name       : String;
      Ask_If_Overloaded : Boolean := False;
      Entity            : out General_Entity;
      Closest_Ref       : out General_Entity_Reference)
   is
      Fuzzy : Boolean;

      function Internal_No_Constructs
        (Name : String; Loc : General_Location) return General_Entity;
      --  Search for the entity, without a fallback to the constructs db

      function Internal_No_Constructs
        (Name : String; Loc : General_Location) return General_Entity
      is
         Entity : General_Entity := No_General_Entity;
      begin
         if Active (SQLITE) then
            if Loc = No_Location then
               --  predefined entities
               Closest_Ref.Ref := Self.Xref.Get_Entity
                 (Name   => Name,
                  File   => No_File,
                  Project => No_Project);

            else
               --  Already handles the operators
               Closest_Ref.Ref := Self.Xref.Get_Entity
                 (Name   => Name,
                  File   => Loc.File,
                  Project => Self.Registry.Tree.Info (Loc.File).Project,
                  Line   => Loc.Line,
                  Column => Loc.Column);
            end if;

            Entity.Entity := Closest_Ref.Ref.Entity;
            Fuzzy := Entity.Entity /= No_Entity and then
              (Is_Fuzzy_Match (Entity.Entity)
                  --  or else not Self.Xref.Is_Up_To_Date (Loc.File)
              );

         else
            declare
               Status : Find_Decl_Or_Body_Query_Status;
               Source : Old_Entities.Source_File;
            begin
               --  ??? Should have a pref for the handling of fuzzy matches:
               --  - consider it as a no match: set Status to Entity_Not_Found
               --  - consider it as overloaded entity: same as below;
               --  - use the closest match: nothing to do.

               if Loc = No_Location then
                  --  A predefined entity
                  --  ??? Should not hard-code False here

                  Source := Old_Entities.Get_Predefined_File
                    (Self.Entities, Case_Sensitive => False);
                  Find_Declaration
                    (Db             => Self.Entities,
                     Source         => Source,
                     Entity_Name    => Name,
                     Line           => Loc.Line,  --  irrelevant
                     Column         => Loc.Column,  --  irrelevant
                     Entity         => Entity.Old_Entity,
                     Closest_Ref    => Closest_Ref.Old_Ref,
                     Status         => Status);

               else
                  Source := Old_Entities.Get_Or_Create
                    (Self.Entities, Loc.File, Allow_Create => True);
                  Find_Declaration
                    (Db             => Self.Entities,
                     Source         => Source,
                     Entity_Name    => Name,
                     Line           => Loc.Line,
                     Column         => Loc.Column,
                     Entity         => Entity.Old_Entity,
                     Closest_Ref    => Closest_Ref.Old_Ref,
                     Status         => Status);
               end if;

               Fuzzy := Status = Overloaded_Entity_Found
                 or else Status = Fuzzy_Match;

               if Status = Entity_Not_Found
                 and then Name /= ""
                 and then Name (Name'First) = '"'
               then
                  --  Try without the quotes
                  Find_Declaration_Or_Overloaded
                    (Self              => Self,
                     Loc               => Loc,
                     Entity_Name       => Entity_Name
                       (Entity_Name'First + 1 .. Entity_Name'Last - 1),
                     Ask_If_Overloaded => Ask_If_Overloaded,
                     Entity            => Entity,
                     Closest_Ref       => Closest_Ref);
               end if;
            end;
         end if;

         Entity.Is_Fuzzy := Fuzzy;
         return Entity;
      end Internal_No_Constructs;

   begin
      Closest_Ref := No_General_Entity_Reference;

      if Entity_Name = "" then
         Entity := No_General_Entity;
         return;
      end if;

      if Active (Me) then
         Increase_Indent (Me, "Find_Declaration of " & Entity_Name
                          & " file=" & Loc.File.Display_Base_Name
                          & " line=" & Loc.Line'Img
                          & " col=" & Loc.Column'Img);
      end if;

      Entity := Internal_No_Constructs (Entity_Name, Loc);  --  also sets Fuzzy

      if Fuzzy and then Ask_If_Overloaded then
         Entity := Select_Entity_Declaration
           (Self   => General_Xref_Database_Record'Class (Self.all)'Access,
            File   => Loc.File,
            Entity => Entity);

         if Active (Me) then
            Decrease_Indent (Me);
         end if;
         return;
      end if;

      --  Fallback on constructs

      if (Entity = No_General_Entity or else Fuzzy)
        and then Active (Constructs_Heuristics)
        and then Loc /= No_Location   --  Nothing for predefined entities
      then
         declare
            Tree_Lang : Tree_Language_Access;
            Result       : Entity_Access;
            New_Location : General_Location;
            New_Entity   : General_Entity;

         begin
            Trace (Me, "Searching entity declaration in constructs");
            Node_From_Entity
               (Self      => Self,
                Handler   => Self.Lang_Handler,
                Decl      => Loc,
                Ent       => Result,
                Tree_Lang => Tree_Lang);

            if Result /= Null_Entity_Access
               and then
                  (Entity_Name = "" or else
                   Get (Get_Construct (Result).Name).all = Entity_Name)
            then
               --  First, try to see if there's already a similar entity in
               --  the database. If that's the case, it's better to use it
               --  than the dummy one created from the construct.

               New_Location :=
                 (File   => Get_File_Path (Get_File (Result)),
                  Line   => Get_Construct (Result).Sloc_Entity.Line,
                  Column => To_Visible_Column
                    (Get_File (Result),
                     Get_Construct (Result).Sloc_Entity.Line,
                     String_Index_Type
                       (Get_Construct (Result).Sloc_Entity.Column)));

               New_Entity := Internal_No_Constructs
                 (Name  => Get (Get_Construct (Result).Name).all,
                  Loc   => (File   => New_Location.File,
                            Line   => New_Location.Line,
                            Column => New_Location.Column));

               if New_Entity /= No_General_Entity
                 and then not Is_Fuzzy (New_Entity)
               then
                  --  If we found an updated ALI entity, use it.
                  Entity := New_Entity;

               elsif Entity /= No_General_Entity then
                  --  Reuse the ALI entity, since that gives us a chance to
                  --  query its references as well.
                  Entity.Loc := New_Location;

               else
                  --  If we have no entity to connect to, then create one
                  --  from the construct database.

                  Entity := To_LI_Entity (Self, Result);
               end if;

               Entity.Is_Fuzzy := True;
            end if;
         end;
      end if;

      if Active (Me) then
         Decrease_Indent (Me);
      end if;
   end Find_Declaration_Or_Overloaded;

   --------------
   -- Get_Name --
   --------------

   function Get_Name
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return String is
   begin
      if Active (SQLITE) then
         if Entity.Entity /= No_Entity then
            return To_String
              (Declaration (Db.Xref.all, Entity.Entity).Name);
         end if;
      else
         if Entity.Old_Entity /= null then
            return Get (Old_Entities.Get_Name (Entity.Old_Entity)).all;
         end if;
      end if;

      return "";
   end Get_Name;

   --------------------
   -- Qualified_Name --
   --------------------

   function Qualified_Name
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return String
   is
   begin
      if Active (SQLITE) then
         if Entity.Entity /= No_Entity then
            return Self.Xref.Qualified_Name (Entity.Entity);
         end if;
      else
         if Entity.Old_Entity /= null then
            return Old_Entities.Queries.Get_Full_Name (Entity.Old_Entity);
         end if;
      end if;

      return "";
   end Qualified_Name;

   ------------------
   -- Get_Location --
   ------------------

   function Get_Location
     (Ref : General_Entity_Reference) return General_Location is
   begin
      if Active (SQLITE) then
         if Ref.Ref /= No_Entity_Reference then
            return Get_Location (Ref.Ref);
         end if;

      else
         declare
            use Old_Entities;
            Loc : constant Old_Entities.File_Location :=
              Old_Entities.Get_Location (Ref.Old_Ref);
         begin
            if Loc.File /= null then
               return (File => Old_Entities.Get_Filename (Loc.File),
                       Line => Loc.Line,
                       Column => Loc.Column);
            end if;
         end;
      end if;
      return No_Location;
   end Get_Location;

   ------------------
   -- Get_Location --
   ------------------

   function Get_Location
     (Ref : Entity_Reference) return General_Location is
   begin
      if Ref = No_Entity_Reference then
         return No_Location;
      else
         return
           (File => Ref.File,
            Line => Ref.Line,
            Column => Visible_Column_Type (Ref.Column));
      end if;
   end Get_Location;

   ---------------------------
   -- Caller_At_Declaration --
   ---------------------------

   function Caller_At_Declaration
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return (Entity => Db.Xref.Declaration (Entity.Entity).Location.Scope,
                 others => <>);
      else
         return (Old_Entity =>
                   Old_Entities.Queries.Get_Caller
                     (Old_Entities.Declaration_As_Reference
                          (Entity.Old_Entity)),
                 others => <>);
      end if;
   end Caller_At_Declaration;

   ---------------------
   -- Get_Declaration --
   ---------------------

   function Get_Declaration
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity_Declaration is
   begin
      if Active (SQLITE) then
         if Entity.Entity /= No_Entity then
            declare
               Ref : constant Entity_Declaration :=
                 Db.Xref.Declaration (Entity.Entity);
            begin
               if Ref /= No_Entity_Declaration then
                  return (Loc    => (File => Ref.Location.File,
                                     Line => Ref.Location.Line,
                                     Column => Ref.Location.Column),
                          Body_Is_Full_Declaration =>
                            Ref.Flags.Body_Is_Full_Declaration,
                          Name   => Ref.Name);
               end if;
            end;
         end if;

      else
         if Entity.Old_Entity /= null then
            declare
               Loc : constant Old_Entities.File_Location :=
                 Old_Entities.Get_Declaration_Of (Entity.Old_Entity);
            begin
               return (Loc => (File   => Old_Entities.Get_Filename (Loc.File),
                               Line   => Loc.Line,
                               Column => Loc.Column),
                       Body_Is_Full_Declaration =>
                         Old_Entities.Body_Is_Full_Declaration
                           (Old_Entities.Get_Kind (Entity.Old_Entity).Kind),
                       Name => To_Unbounded_String
                         (Get
                            (Old_Entities.Get_Name (Entity.Old_Entity)).all));
            end;
         end if;
      end if;

      if Entity.Loc /= No_Location then
         declare
            Result    : Entity_Access;
            Tree_Lang : Tree_Language_Access;
            Decl      : Entity_Access;
            Node      : Construct_Tree_Iterator;
         begin
            Node_From_Entity
               (Db, Db.Lang_Handler, Entity.Loc, Result, Tree_Lang);

            if Result /= Null_Entity_Access then
               Decl := Get_Declaration
                  (Get_Tree_Language (Get_File (Result)), Result);
               Node := To_Construct_Tree_Iterator (Decl);
               return (Loc => (File   => Get_File_Path (Get_File (Decl)),
                               Line   => Get_Construct (Node).Sloc_Start.Line,
                               Column => Visible_Column_Type
                                 (Get_Construct (Node).Sloc_Start.Column)),
                       Body_Is_Full_Declaration => False,
                       Name => Null_Unbounded_String);
            end if;
         end;
      end if;

      return No_General_Entity_Declaration;
   end Get_Declaration;

   ----------------------------
   -- Get_Entity_At_Location --
   ----------------------------

   function Get_Entity_At_Location
     (Db     : access General_Xref_Database_Record'Class;
      Loc : General_Location) return Entity_Access
   is
      S_File : constant Structured_File_Access :=
        Get_Or_Create
          (Db   => Db.Constructs,
           File => Loc.File);
      Construct : Construct_Tree_Iterator;
   begin
      Update_Contents (S_File);

      Construct :=
        Get_Iterator_At
          (Tree      => Get_Tree (S_File),
           Location  => To_Location
             (Loc.Line,
              To_Line_String_Index
                (S_File,
                 Loc.Line,
                 Loc.Column)),
           From_Type => Start_Name);

      if Construct /= Null_Construct_Tree_Iterator then
         return To_Entity_Access (S_File, Construct);
      else
         return Null_Entity_Access;
      end if;
   end Get_Entity_At_Location;

   --------------
   -- Get_Body --
   --------------

   function Get_Body
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity;
      After  : General_Location := No_Location) return General_Location
   is
      No_Location_If_First : constant Boolean := False;

      function Extract_Next_By_Heuristics return General_Location;
      --  Return the next body location using the construct heuristics

      function Is_Location_For_Entity
        (Location : General_Location) return Boolean;
      --  Return true if the location given in parameter indeed corresponds to
      --  a declaration construct, false otherwise, typically when the file has
      --  been modified and the ali retreived is not up to date.
      --  Note that if the construct database is deactivated, this will always
      --  return true (we're always on the expected construct, we don't expect
      --  anything in particular).

      ----------------------------
      -- Is_Location_For_Entity --
      ----------------------------

      function Is_Location_For_Entity
        (Location : General_Location) return Boolean
      is
         C_Entity : Entity_Access;
      begin
         if Active (Constructs_Heuristics) then
            C_Entity := Get_Entity_At_Location (Db, Location);

            --  Return true if we found a construct here and if it's of the
            --  appropriate name.

            return C_Entity /= Null_Entity_Access
              and then Get (Get_Identifier (C_Entity)).all =
              Db.Get_Name (Entity);
         end if;

         return True;
      end Is_Location_For_Entity;

      --------------------------------
      -- Extract_Next_By_Heuristics --
      --------------------------------

      function Extract_Next_By_Heuristics return General_Location is
         C_Entity, New_Entity : Entity_Access := Null_Entity_Access;
         Loc : General_Location;

      begin
         --  In order to locate the reference to look from, we check if there
         --  is a file associated to the input location. In certain cases, this
         --  location is computed from a context that does not have file
         --  information, so for safety purpose, we check that the file exist
         --  (there's nothing we can do at the completion level without a
         --  file). If there's no file, then the context has been partially
         --  provided (or not at all) so we start from the declaration of the
         --  Entity.

         if Active (Constructs_Heuristics) then
            if After /= No_Location then
               C_Entity := Get_Entity_At_Location (Db, After);
            end if;

            if C_Entity = Null_Entity_Access then
               if Entity.Loc /= No_Location then
                  C_Entity := Get_Entity_At_Location (Db, Entity.Loc);
               else
                  Loc := Db.Get_Declaration (Entity).Loc;
                  if Loc /= No_Location then
                     C_Entity := Get_Entity_At_Location (Db, Loc);
                  end if;
               end if;
            end if;

            if C_Entity /= Null_Entity_Access then
               declare
                  S_File : constant Structured_File_Access :=
                    Get_File (C_Entity);

                  Tree_Lang : constant Tree_Language_Access :=
                    Get_Tree_Language_From_File
                      (Db.Lang_Handler, Get_File_Path (S_File));
               begin
                  New_Entity := Tree_Lang.Find_Next_Part (C_Entity);

                  --  If we're initializing a loop, e.g. the current location
                  --  is no location, then return the result. Otherwise, don't
                  --  return it if we got back to the initial body and the
                  --  caller doesn't want to loop back.

                  if After /= No_Location
                    and then No_Location_If_First
                    and then C_Entity = Tree_Lang.Find_First_Part (C_Entity)
                  then
                     return No_Location;
                  end if;

                  if New_Entity /= C_Entity then
                     return
                       (File   => Get_File_Path (Get_File (New_Entity)),
                        Line   => Get_Construct (New_Entity).Sloc_Entity.Line,
                        Column =>
                          To_Visible_Column
                            (Get_File (New_Entity),
                             Get_Construct (New_Entity).Sloc_Entity.Line,
                             String_Index_Type
                               (Get_Construct (New_Entity).Sloc_Entity.Column
                               )));
                  end if;
               end;
            end if;
         end if;

         return No_Location;
      end Extract_Next_By_Heuristics;

      Candidate : General_Location := No_Location;

   begin
      if Active (Me) then
         Increase_Indent (Me, "Get_Body of "
                          & Db.Get_Name (Entity)
                          & " fuzzy=" & Is_Fuzzy (Entity)'Img);
      end if;

      if Active (SQLITE) then
         if Entity.Entity /= No_Entity then
            declare
               C   : References_Cursor;
               Ref : Entity_Reference;
               Matches : Boolean := After = No_Location;
               First  : General_Location := No_Location;
               Is_First : Boolean := True;
            begin
               Bodies (Db.Xref.all, Entity.Entity, Cursor => C);
               while Has_Element (C) loop
                  Ref := Element (C);

                  if Ref /= No_Entity_Reference then
                     if Is_First then
                        Is_First := False;
                        First := (File => Ref.File,
                                  Line => Ref.Line,
                                  Column => Visible_Column_Type (Ref.Column));
                     end if;

                     if Matches then
                        Candidate :=
                          (File => Ref.File,
                           Line => Ref.Line,
                           Column => Visible_Column_Type (Ref.Column));
                        exit;
                     else
                        Matches := Ref.Line = After.Line
                          and then Ref.Column = After.Column
                          and then Ref.File = After.File;
                     end if;
                  end if;

                  Next (C);
               end loop;

               if Candidate = No_Location then
                  --  The "After" parameter did not correspond to a body
                  Candidate := First;
               end if;
            end;
         end if;

      else
         if Entity.Old_Entity /= null then
            declare
               Loc : Old_Entities.File_Location;
            begin
               if After /= No_Location then
                  Find_Next_Body
                    (Entity           => Entity.Old_Entity,
                     Current_Location =>
                       (File   => Old_Entities.Get_Or_Create
                          (Db.Entities, After.File, Allow_Create => True),
                        Line   => After.Line,
                        Column => After.Column),
                     Location         => Loc);
               else
                  Find_Next_Body
                    (Entity           => Entity.Old_Entity,
                     Current_Location => Old_Entities.No_File_Location,
                     Location         => Loc);
               end if;

               if Loc /= Old_Entities.No_File_Location then
                  if Active (Me) then
                     Trace (Me, "Found " & Old_Entities.To_String (Loc));
                  end if;

                  Candidate :=
                    (File => Old_Entities.Get_Filename (Loc.File),
                     Line => Loc.Line,
                     Column => Loc.Column);
               else
                  Trace (Me, "No body found");
               end if;
            end;
         end if;
      end if;

      --  If no next body has been found at this stage, try to see what we can
      --  do using the construct database. The constructs database only knows
      --  about one body though, so we skip it when asking for other bodies.

      if After = No_Location then
         declare
            H_Loc : constant General_Location := Extract_Next_By_Heuristics;
         begin
            if Active (Me) then
               Trace (Me, "Body computed from constructs at "
                      & To_String (H_Loc));
            end if;

            if H_Loc /= No_Location and then
            --  If we found nothing, use the information from the constructs.
              (Candidate = No_Location

               --  it's OK to return the first entity.
               or else (not No_Location_If_First
                        and then not Is_Location_For_Entity (Candidate)))

            then
               Candidate := H_Loc;

               --  else if the candidate is at the expected location and if
               if Active (Me) then
                  Trace (Me, "Use body from constructs");
               end if;

               --  If we don't have any more information to extract from the
               --  construct database, then return the first entity if allowed
               --  by the flags, or null.

            elsif No_Location_If_First then
               Candidate := No_Location;
            end if;
         end;
      end if;

      if Active (Me) then
         Decrease_Indent (Me);
      end if;

      return Candidate;
   end Get_Body;

   -----------------
   -- Get_Type_Of --
   -----------------

   function Get_Type_Of
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if not Active (SQLITE) then
         declare
            E : constant Old_Entities.Entity_Information :=
                  Old_Entities.Get_Type_Of (Entity.Old_Entity);
         begin
            return From_Old (E);
         end;
      else
         return From_New (Db.Xref.Type_Of (Entity.Entity));
      end if;
   end Get_Type_Of;

   -------------------
   -- Returned_Type --
   -------------------

   function Returned_Type
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity is
   begin
      if Active (SQLITE) then
         return From_New (Db.Xref.Type_Of (Entity.Entity));
      else
         return From_Old (Old_Entities.Get_Returned_Type (Entity.Old_Entity));
      end if;
   end Returned_Type;

   --------------------------
   -- Is_Predefined_Entity --
   --------------------------

   function Is_Predefined_Entity
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean is
   begin
      if not Active (SQLITE) then
         return Old_Entities.Is_Predefined_Entity (E.Old_Entity);
      else
         return Is_Predefined_Entity (Declaration (Db.Xref.all, E.Entity));
      end if;
   end Is_Predefined_Entity;

   ----------------------
   -- Node_From_Entity --
   ----------------------

   procedure Node_From_Entity
     (Self        : access General_Xref_Database_Record'Class;
      Handler     : access Abstract_Language_Handler_Record'Class;
      Decl        : General_Location;
      Ent         : out Entity_Access;
      Tree_Lang   : out Tree_Language_Access)
   is
      Data_File   : Structured_File_Access;
   begin
      Ent := Null_Entity_Access;
      Tree_Lang := Get_Tree_Language_From_File (Handler, Decl.File, False);
      Data_File := Language.Tree.Database.Get_Or_Create
        (Db   => Self.Constructs,
         File => Decl.File);
      Update_Contents (Data_File);

      --  In some cases, the references are extracted from a place
      --  where there is still an ALI file, but no more source file.
      --  This will issue a null Structured_File_Access, which is why
      --  we're protecting the following code with the above condition

      if not Is_Null (Data_File) then
         --  Find_Declaration does more than Get_Iterator_At, so use it.
         Ent := Tree_Lang.Find_Declaration
            (Data_File, Decl.Line,
             To_Line_String_Index (Data_File, Decl.Line, Decl.Column));
      end if;
   end Node_From_Entity;

   ------------------
   -- Pointed_Type --
   ------------------

   function Pointed_Type
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Db.Xref.Pointed_Type (Entity.Entity));
      else
         return From_Old
           (Old_Entities.Queries.Pointed_Type (Entity.Old_Entity));
      end if;
   end Pointed_Type;

   ---------
   -- Ref --
   ---------

   procedure Ref (Entity : General_Entity) is
   begin
      Old_Entities.Ref (Entity.Old_Entity);
   end Ref;

   -----------------------
   -- To_General_Entity --
   -----------------------

   function To_General_Entity
     (Db : access General_Xref_Database_Record'Class;
      E  : Entity_Information) return General_Entity
   is
      Decl : constant Entity_Declaration := Declaration (Db.Xref.all, E);
      Loc  : General_Location;

   begin
      pragma Assert (Active (SQLITE));

      Loc :=
        (File   => Decl.Location.File,
         Line   => Decl.Location.Line,
         Column => Visible_Column_Type (Decl.Location.Column));

      return Get_Entity
        (Db   => Db,
         Name => To_String (Decl.Name),
         Loc  => Loc);
   end To_General_Entity;

   -----------------------
   -- To_General_Entity --
   -----------------------

   function To_General_Entity
     (E  : Old_Entities.Entity_Information) return General_Entity is
   begin
      pragma Assert (not Active (SQLITE));

      if E = null then
         return No_General_Entity;
      else
         return General_Entity'(Old_Entity => E, others => <>);
      end if;
   end To_General_Entity;

   -----------
   -- Unref --
   -----------

   procedure Unref (Entity : in out General_Entity) is
   begin
      Old_Entities.Unref (Entity.Old_Entity);
   end Unref;

   -----------------
   -- Renaming_Of --
   -----------------

   function Renaming_Of
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return (Entity => Self.Xref.Renaming_Of (Entity.Entity),
                 others => <>);
      else
         return (Old_Entity =>
                   Old_Entities.Queries.Renaming_Of (Entity.Old_Entity),
                 others     => <>);
      end if;
   end Renaming_Of;

   ---------
   -- "=" --
   ---------

   overriding function "="
     (Ref1, Ref2 : General_Entity_Reference) return Boolean
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Ref1.Ref = Ref2.Ref;
      else
         return Ref1.Old_Ref = Ref2.Old_Ref;
      end if;
   end "=";

   ---------
   -- "=" --
   ---------

   overriding function "=" (E1, E2 : General_Entity) return Boolean is
   begin
      if Active (SQLITE) then
         return E1.Entity = E2.Entity;
      else
         return E1.Old_Entity = E2.Old_Entity;
      end if;
   end "=";

   -------------------------
   -- Find_All_References --
   -------------------------

   procedure Find_All_References
     (Self                  : access General_Xref_Database_Record;
      Iter                  : out Entity_Reference_Iterator;
      Entity                : General_Entity;
      File_Has_No_LI_Report : Basic_Types.File_Error_Reporter := null;
      In_File              : GNATCOLL.VFS.Virtual_File := GNATCOLL.VFS.No_File;
      In_Scope              : General_Entity := No_General_Entity;
      Include_Overriding    : Boolean := False;
      Include_Overridden    : Boolean := False)
   is
      F      : Old_Entities.Source_File;
   begin
      if Active (SQLITE) then
         --  File_Has_No_LI_Report voluntarily ignored.

         Self.Xref.Recursive
           (Entity          => Entity.Entity,
            Compute         => GNATCOLL.Xref.References'Access,
            Cursor          => Iter.Iter,
            From_Overriding => Include_Overriding,
            From_Overridden => Include_Overridden,
            From_Renames    => True);
         Iter.In_File  := In_File;
         Iter.In_Scope := In_Scope;

         while Has_Element (Iter.Iter)
           and then
             ((Iter.In_File /= No_File
               and then Iter.Iter.Element.File /= Iter.In_File)
              or else
                (Iter.In_Scope /= No_General_Entity
                 and then Iter.Iter.Element.Scope /= Iter.In_Scope.Entity))
         loop
            Iter.Iter.Next;
         end loop;

      else
         declare
            use Old_Entities;
         begin
            if In_File /= No_File then
               F := Old_Entities.Get_Or_Create
                 (Db    => Self.Entities,
                  File  => In_File,
                  Allow_Create => True);
            end if;

            Old_Entities.Queries.Find_All_References
              (Iter.Old_Iter, Entity.Old_Entity,
               File_Has_No_LI_Report, F, In_Scope.Old_Entity,
               Include_Overriding => Include_Overriding,
               Include_Overridden => Include_Overridden);
         end;
      end if;
   end Find_All_References;

   ------------
   -- At_End --
   ------------

   function At_End (Iter : Entity_Reference_Iterator) return Boolean is
   begin
      if Active (SQLITE) then
         return not Has_Element (Iter.Iter);
      else
         return At_End (Iter.Old_Iter);
      end if;
   end At_End;

   ----------
   -- Next --
   ----------

   procedure Next (Iter : in out Entity_Reference_Iterator) is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         Next (Iter.Iter);

         while Has_Element (Iter.Iter)
           and then
             ((Iter.In_File /= No_File
               and then Iter.Iter.Element.File /= Iter.In_File)
              or else
                (Iter.In_Scope /= No_General_Entity
                 and then Iter.Iter.Element.Scope /= Iter.In_Scope.Entity))
         loop
            Iter.Iter.Next;
         end loop;

      else
         Next (Iter.Old_Iter);
      end if;
   end Next;

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

   function Get
     (Iter : Entity_Reference_Iterator) return General_Entity_Reference
   is
   begin
      if Active (SQLITE) then
         return (Old_Ref => Old_Entities.No_Entity_Reference,
                 Ref     => Iter.Iter.Element);
      else
         return (Old_Ref => Get (Iter.Old_Iter),
                 Ref     => No_Entity_Reference);
      end if;
   end Get;

   ----------------
   -- Get_Entity --
   ----------------

   function Get_Entity
     (Iter : Entity_Reference_Iterator) return General_Entity is
   begin
      if Active (SQLITE) then
         return (Entity     => Iter.Iter.Element.Entity,
                 others     => <>);
      else
         return To_General_Entity (Get_Entity (Iter.Old_Iter));
      end if;
   end Get_Entity;

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

   procedure Destroy (Iter : in out Entity_Reference_Iterator) is
   begin
      if Active (SQLITE) then
         null;
      else
         Destroy (Iter.Old_Iter);
      end if;
   end Destroy;

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

   procedure Destroy (Iter : in out Entity_Reference_Iterator_Access) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (Entity_Reference_Iterator,
         Entity_Reference_Iterator_Access);
   begin
      if Iter /= null then
         Destroy (Iter.all);
         Unchecked_Free (Iter);
      end if;
   end Destroy;

   --------------------------
   -- Get_Current_Progress --
   --------------------------

   function Get_Current_Progress
     (Iter : Entity_Reference_Iterator) return Integer is
   begin
      if Active (SQLITE) then
         return 1;
      else
         return Get_Current_Progress (Iter.Old_Iter);
      end if;
   end Get_Current_Progress;

   ------------------------
   -- Get_Total_Progress --
   ------------------------

   function Get_Total_Progress
     (Iter : Entity_Reference_Iterator) return Integer is
   begin
      if Active (SQLITE) then
         --  precomputing the number of references is expensive (basically
         --  requires doing the query twice), and won't be needed anymore when
         --  we get rid of the asynchronous iterators.
         return 1;
      else
         return Get_Total_Progress (Iter.Old_Iter);
      end if;
   end Get_Total_Progress;

   -----------------------
   -- Show_In_Callgraph --
   -----------------------

   function Show_In_Callgraph
     (Db  : access General_Xref_Database_Record;
      Ref : General_Entity_Reference) return Boolean
   is
   begin
      if Active (SQLITE) then
         return Db.Xref.Show_In_Callgraph (Ref.Ref);
      else
         return Old_Entities.Show_In_Call_Graph
           (Db.Entities, Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Show_In_Callgraph;

   ----------------
   -- Get_Caller --
   ----------------

   function Get_Caller
     (Ref : General_Entity_Reference) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return (Entity => Ref.Ref.Scope, others => <>);
      else
         return (Old_Entity => Old_Entities.Queries.Get_Caller (Ref.Old_Ref),
                 others => <>);
      end if;
   end Get_Caller;

   -------------------
   -- Is_Subprogram --
   -------------------

   function Is_Subprogram
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Subprogram;
      else
         return Old_Entities.Is_Subprogram (E.Old_Entity);
      end if;
   end Is_Subprogram;

   ------------------
   -- Is_Container --
   ------------------

   function Is_Container
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Container;
      else
         return Old_Entities.Is_Container
           (Old_Entities.Get_Kind (E.Old_Entity).Kind);
      end if;
   end Is_Container;

   ----------------
   -- Is_Generic --
   ----------------

   function Is_Generic
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Generic;
      else
         return Old_Entities.Get_Kind (E.Old_Entity).Is_Generic;
      end if;
   end Is_Generic;

   ---------------
   -- Is_Global --
   ---------------

   function Is_Global
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Global;
      else
         return Old_Entities.Get_Attributes
           (E.Old_Entity)(Old_Entities.Global);
      end if;
   end Is_Global;

   ---------------------
   -- Is_Static_Local --
   ---------------------

   function Is_Static_Local
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Static_Local;
      else
         return Old_Entities.Get_Attributes
           (E.Old_Entity)(Old_Entities.Static_Local);
      end if;
   end Is_Static_Local;

   -------------
   -- Is_Type --
   -------------

   function Is_Type
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Type;
      else
         return Old_Entities.Get_Category (E.Old_Entity) =
           Old_Entities.Type_Or_Subtype;
      end if;
   end Is_Type;

   -------------------------
   -- Is_Dispatching_Call --
   -------------------------

   function Is_Dispatching_Call
     (Db  : access General_Xref_Database_Record;
      Ref : General_Entity_Reference) return Boolean
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Dispatching_Call (Ref.Ref);
      else
         return Old_Entities.Get_Kind (Ref.Old_Ref) =
           Old_Entities.Dispatching_Call;
      end if;
   end Is_Dispatching_Call;

   ------------
   -- At_End --
   ------------

   function At_End (Iter : Base_Entities_Cursor) return Boolean is
   begin
      if Active (SQLITE) then
         return not Has_Element (Iter.Iter);
      else
         raise Program_Error;
      end if;
   end At_End;

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

   function Get (Iter : Base_Entities_Cursor) return General_Entity is
   begin
      if Active (SQLITE) then
         return (Entity => Element (Iter.Iter), others => <>);
      else
         raise Program_Error;
      end if;
   end Get;

   ----------
   -- Next --
   ----------

   procedure Next (Iter : in out Base_Entities_Cursor) is
   begin
      if Active (SQLITE) then
         Next (Iter.Iter);
      else
         raise Program_Error;
      end if;
   end Next;

   -----------------------------
   -- Get_All_Called_Entities --
   -----------------------------

   function Get_All_Called_Entities
     (Self   : access General_Xref_Database_Record'Class;
      Entity : General_Entity) return Calls_Iterator
   is
      Result : Calls_Iterator;
   begin
      if Active (SQLITE) then
         Self.Xref.Calls (Entity.Entity, Result.Iter);
      else
         Result.Old_Iter := Old_Entities.Queries.Get_All_Called_Entities
           (Entity.Old_Entity);
      end if;
      return Result;
   end Get_All_Called_Entities;

   ----------------------
   -- Entities_In_File --
   ----------------------

   function Entities_In_File
     (Self   : access General_Xref_Database_Record'Class;
      File   : GNATCOLL.VFS.Virtual_File;
      Name   : String := "") return Entities_In_File_Cursor
   is
      Result : Entities_In_File_Cursor;
      F      : Old_Entities.Source_File;
   begin
      if Active (SQLITE) then

         if Name = "" then
            Self.Xref.Referenced_In (File, Self.Registry.Tree.Info (File).Project, Cursor => Result.Iter);
         else
            Self.Xref.Referenced_In (File, Self.Registry.Tree.Info (File).Project, Name, Cursor => Result.Iter);
         end if;

      else
         F := Old_Entities.Get_Or_Create
           (Self.Entities, File, Allow_Create => True);
         Old_Entities.Queries.Find_All_Entities_In_File
           (Iter        => Result.Old_Iter,
            File        => F,
            Name        => Name);
      end if;
      return Result;
   end Entities_In_File;

   ------------
   -- At_End --
   ------------

   overriding function At_End
     (Iter : Entities_In_File_Cursor) return Boolean is
   begin
      if Active (SQLITE) then
         return At_End (Base_Entities_Cursor (Iter));
      else
         return At_End (Iter.Old_Iter);
      end if;
   end At_End;

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

   overriding function Get
     (Iter : Entities_In_File_Cursor) return General_Entity is
   begin
      if Active (SQLITE) then
         return Get (Base_Entities_Cursor (Iter));
      else
         return (Old_Entity => Get (Iter.Old_Iter), others => <>);
      end if;
   end Get;

   ----------
   -- Next --
   ----------

   overriding procedure Next
     (Iter : in out Entities_In_File_Cursor) is
   begin
      if Active (SQLITE) then
         Next (Base_Entities_Cursor (Iter));
      else
         Next (Iter.Old_Iter);
      end if;
   end Next;

   ------------------------------
   -- All_Entities_From_Prefix --
   ------------------------------

   function All_Entities_From_Prefix
     (Self       : access General_Xref_Database_Record'Class;
      Prefix     : String;
      Is_Partial : Boolean := True) return Entities_In_Project_Cursor
   is
      use Old_Entities.Entities_Search_Tries;
      Result : Entities_In_Project_Cursor;
   begin
      if Active (SQLITE) then
         if Active (Me) then
            Increase_Indent (Me, "All_Entities from Prefix '" & Prefix
                             & "' partial=" & Is_Partial'Img);
         end if;

         Self.Xref.From_Prefix
           (Prefix     => Prefix,
            Is_Partial => Is_Partial,
            Cursor     => Result.Iter);

         Decrease_Indent (Me);
      else
         Result.Old_Iter :=
           Start (Trie     => Old_Entities.Get_Name_Index
                     (Old_Entities.Get_LI_Handler (Self.Entities)),
                  Prefix   => Prefix,
                  Is_Partial => Is_Partial);
      end if;
      return Result;
   end All_Entities_From_Prefix;

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

   procedure Destroy (Iter : in out Entities_In_Project_Cursor) is
   begin
      if Active (SQLITE) then
         null;
      else
         Old_Entities.Entities_Search_Tries.Free (Iter.Old_Iter);
      end if;
   end Destroy;

   ------------
   -- At_End --
   ------------

   overriding function At_End
     (Iter : Entities_In_Project_Cursor) return Boolean
   is
      use Old_Entities.Entities_Search_Tries;
   begin
      if Active (SQLITE) then
         return not Has_Element (Iter.Iter);
      else
         return At_End (Iter.Old_Iter);
      end if;
   end At_End;

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

   overriding function Get
     (Iter : Entities_In_Project_Cursor) return General_Entity
   is
      use Old_Entities.Entities_Search_Tries;
   begin
      if Active (SQLITE) then
         return From_New (Element (Iter.Iter));
      else
         return From_Old (Get (Iter.Old_Iter));
      end if;
   end Get;

   ----------
   -- Next --
   ----------

   overriding procedure Next (Iter : in out Entities_In_Project_Cursor) is
      use Old_Entities.Entities_Search_Tries;
   begin
      if Active (SQLITE) then
         Next (Iter.Iter);
      else
         Next (Iter.Old_Iter);
      end if;
   end Next;

   ------------
   -- At_End --
   ------------

   overriding function At_End (Iter : Calls_Iterator) return Boolean is
   begin
      if Active (SQLITE) then
         return At_End (Base_Entities_Cursor (Iter));
      else
         return At_End (Iter.Old_Iter);
      end if;
   end At_End;

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

   overriding function Get (Iter : Calls_Iterator) return General_Entity is
   begin
      if Active (SQLITE) then
         return Get (Base_Entities_Cursor (Iter));
      else
         return (Old_Entity => Get (Iter.Old_Iter), others => <>);
      end if;
   end Get;

   ----------
   -- Next --
   ----------

   overriding procedure Next (Iter : in out Calls_Iterator) is
   begin
      if Active (SQLITE) then
         Next (Base_Entities_Cursor (Iter));
      else
         Next (Iter.Old_Iter);
      end if;
   end Next;

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

   procedure Destroy (Iter : in out Calls_Iterator) is
   begin
      if not Active (SQLITE) then
         Destroy (Iter.Old_Iter);
      end if;
   end Destroy;

   ------------
   -- To_Old --
   ------------

   function To_Old
     (Entity : General_Entity) return Old_Entities.Entity_Information is
   begin
      return Entity.Old_Entity;
   end To_Old;

   function From_Old
     (Entity : Old_Entities.Entity_Information) return General_Entity is
   begin
      return (Old_Entity => Entity, others => <>);
   end From_Old;

   ------------
   -- To_New --
   ------------

   function To_New
     (Entity : General_Entity) return GNATCOLL.Xref.Entity_Information is
   begin
      return Entity.Entity;
   end To_New;

   --------------
   -- From_New --
   --------------

   function From_New
     (Entity : GNATCOLL.Xref.Entity_Information) return General_Entity is
   begin
      return (Entity => Entity, others => <>);
   end From_New;

   ----------------
   -- Parameters --
   ----------------

   function Parameters
     (Dbase  : access General_Xref_Database_Record;
      Entity : General_Entity) return Parameter_Array
   is
      All_Params : Parameter_Array (1 .. 100);
      Count      : Integer := All_Params'First - 1;
   begin
      if Active (SQLITE) then
         declare
            Curs : Parameters_Cursor := Dbase.Xref.Parameters (Entity.Entity);
         begin
            while Curs.Has_Element loop
               Count := Count + 1;
               All_Params (Count) :=
                 (Kind => Curs.Element.Kind,
                  Parameter => From_New (Curs.Element.Parameter));
               Curs.Next;
            end loop;
         end;

      else
         declare
            Iter : Old_Entities.Queries.Subprogram_Iterator :=
              Old_Entities.Queries.Get_Subprogram_Parameters
                (Entity.Old_Entity);
            E    : Old_Entities.Entity_Information;
         begin
            loop
               Old_Entities.Queries.Get (Iter, E);
               exit when E = null;

               Count := Count + 1;
               All_Params (Count).Parameter := From_Old (E);
               case Old_Entities.Queries.Get_Type (Iter) is
                  when Old_Entities.Queries.In_Parameter =>
                     All_Params (Count).Kind := In_Parameter;
                  when Old_Entities.Queries.Out_Parameter =>
                     All_Params (Count).Kind := Out_Parameter;
                  when Old_Entities.Queries.In_Out_Parameter =>
                     All_Params (Count).Kind := In_Out_Parameter;
                  when Old_Entities.Queries.Access_Parameter =>
                     All_Params (Count).Kind := Access_Parameter;
               end case;

               Old_Entities.Queries.Next (Iter);
            end loop;
         end;
      end if;

      return All_Params (All_Params'First .. Count);
   end Parameters;

   ---------------------
   -- Is_Parameter_Of --
   ---------------------

   function Is_Parameter_Of
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Parameter_Of (Entity.Entity));
      else
         return From_Old
           (Old_Entities.Queries.Is_Parameter_Of (Entity.Old_Entity));
      end if;
   end Is_Parameter_Of;

   ---------------------
   -- Is_Primitive_Of --
   ---------------------

   function Is_Primitive_Of
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
      Cursor : Entities_Cursor;
   begin
      if Active (SQLITE) then
         Self.Xref.Method_Of (Entity.Entity, Cursor);
         return From_New (Element (Cursor));
      else
         return From_Old
           (Old_Entities.Is_Primitive_Operation_Of (Entity.Old_Entity));
      end if;
   end Is_Primitive_Of;

   -------------------
   -- Is_Up_To_Date --
   -------------------

   function Is_Up_To_Date
     (Self : access General_Xref_Database_Record;
      File : Virtual_File) return Boolean
   is
      Source : Old_Entities.Source_File;
   begin
      if Active (SQLITE) then
         return Self.Xref.Is_Up_To_Date (File);
      else
         Source := Old_Entities.Get_Or_Create
           (Self.Entities, File, Allow_Create => True);
         return Old_Entities.Is_Up_To_Date (Source);
      end if;
   end Is_Up_To_Date;

   -----------------
   -- Has_Methods --
   -----------------

   function Has_Methods
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
      use Old_Entities;
      K  : Old_Entities.E_Kinds;
   begin
      if Active (SQLITE) then
         if E.Entity /= No_Entity then
            return Db.Xref.Declaration (E.Entity).Flags.Has_Methods;
         end if;

      else
         if E.Old_Entity /= null then
            K := Old_Entities.Get_Kind (E.Old_Entity).Kind;
            return K = Old_Entities.Class
              or else K = Record_Kind
              or else K = Old_Entities.Interface_Kind;
         end if;
      end if;

      --  ??? Fallback on constructs
      return False;
   end Has_Methods;

   ---------------
   -- Is_Access --
   ---------------

   function Is_Access
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Access;

      else
         return E.Old_Entity /= null
           and then Old_Entities.Get_Kind (E.Old_Entity).Kind =
              Old_Entities.Access_Kind;
      end if;
   end Is_Access;

   -----------------
   -- Is_Abstract --
   -----------------

   function Is_Abstract
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Abstract;

      else
         return E.Old_Entity /= null
           and then Old_Entities.Get_Kind (E.Old_Entity).Is_Abstract;
      end if;
   end Is_Abstract;

   --------------
   -- Is_Array --
   --------------

   function Is_Array
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
      use Old_Entities;
      Is_Array_E : constant array (E_Kinds) of Boolean :=
        (Overloaded_Entity => True,
         Unresolved_Entity => True,
         Array_Kind        => True,
         String_Kind       => True,
         others            => False);
   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Array;

      else
         return E.Old_Entity /= null
           and then Is_Array_E (Old_Entities.Get_Kind (E.Old_Entity).Kind);
      end if;
   end Is_Array;

   ------------------------------
   -- Is_Printable_In_Debugger --
   ------------------------------

   function Is_Printable_In_Debugger
     (Db : access General_Xref_Database_Record;
      E  : General_Entity) return Boolean
   is
      use Old_Entities;
      Is_Printable_Entity : constant array (E_Kinds) of Boolean :=
        (Overloaded_Entity    => True,
         Unresolved_Entity    => True,
         Access_Kind          => True,
         Array_Kind           => True,
         Boolean_Kind         => True,
         Class_Wide           => True,
         Class                => True,
         Decimal_Fixed_Point  => True,
         Enumeration_Literal  => True,
         Enumeration_Kind     => True,
         Exception_Entity     => True,
         Floating_Point       => True,
         Modular_Integer      => True,
         Named_Number         => True,
         Ordinary_Fixed_Point => True,
         Record_Kind          => True,
         Signed_Integer       => True,
         String_Kind          => True,
         others               => False);

   begin
      if Active (SQLITE) then
         return Db.Xref.Declaration (E.Entity).Flags.Is_Printable_In_Gdb;

      else
         return E.Old_Entity /= null
           and then Is_Printable_Entity
             (Old_Entities.Get_Kind (E.Old_Entity).Kind);
      end if;
   end Is_Printable_In_Debugger;

   ----------------------
   -- Get_Display_Kind --
   ----------------------

   function Get_Display_Kind
     (Db     : access General_Xref_Database_Record;
      Entity : General_Entity) return String
   is
   begin
      if Active (SQLITE) then
         return To_String (Db.Xref.Declaration (Entity.Entity).Kind);
      else
         return Old_Entities.Kind_To_String
           (Old_Entities.Get_Kind (Entity.Old_Entity));
      end if;
   end Get_Display_Kind;

   ------------------------------
   -- Reference_Is_Declaration --
   ------------------------------

   function Reference_Is_Declaration
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean
   is
      pragma Unreferenced (Db);
   begin
      if Active (SQLITE) then
         return Ref.Ref.Kind_Id = Kind_Id_Declaration;
      else
         return Old_Entities.Queries.Reference_Is_Declaration
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Reference_Is_Declaration;

   -----------------------
   -- Reference_Is_Body --
   -----------------------

   function Reference_Is_Body
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean
   is
      pragma Unreferenced (Db);
   begin
      if Active (SQLITE) then
         return Ref.Ref.Kind = "body";
      else
         return Old_Entities.Queries.Reference_Is_Body
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Reference_Is_Body;

   -----------------------
   -- Is_Read_Reference --
   -----------------------

   function Is_Read_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Read_Reference (Ref.Ref);
      else
         return Old_Entities.Is_Read_Reference
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Is_Read_Reference;

   --------------------------------------------
   -- Is_Or_Read_Write_Or_Implicit_Reference --
   --------------------------------------------

   function Is_Read_Or_Write_Or_Implicit_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean
   is
   begin
      return Is_Read_Or_Write_Reference (Db, Ref)
        or else Is_Implicit_Reference (Db, Ref);
   end Is_Read_Or_Write_Or_Implicit_Reference;

   -----------------------------------
   -- Is_Read_Or_Implicit_Reference --
   -----------------------------------

   function Is_Read_Or_Implicit_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean
   is
   begin
      return Is_Read_Reference (Db, Ref)
        or else Is_Implicit_Reference (Db, Ref);
   end Is_Read_Or_Implicit_Reference;

   ---------------------------
   -- Is_Implicit_Reference --
   ---------------------------

   function Is_Implicit_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Implicit_Reference (Ref.Ref);
      else
         return Old_Entities.Get_Kind (Ref.Old_Ref) = Old_Entities.Implicit;
      end if;
   end Is_Implicit_Reference;

   -----------------------
   -- Is_Real_Reference --
   -----------------------

   function Is_Real_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Real_Reference (Ref.Ref);
      else
         return Old_Entities.Is_Real_Reference
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Is_Real_Reference;

   -----------------------------------
   -- Is_Real_Or_Implicit_Reference --
   -----------------------------------

   function Is_Real_Or_Implicit_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean is
   begin
      return Is_Real_Reference (Db, Ref)
        or else Is_Implicit_Reference (Db, Ref);
   end Is_Real_Or_Implicit_Reference;

   ------------------------
   -- Is_Write_Reference --
   ------------------------

   function Is_Write_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Write_Reference (Ref.Ref);
      else
         return Old_Entities.Is_Write_Reference
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Is_Write_Reference;

   --------------------------------
   -- Is_Read_Or_Write_Reference --
   --------------------------------

   function Is_Read_Or_Write_Reference
     (Db  : access General_Xref_Database_Record'Class;
      Ref : General_Entity_Reference) return Boolean is
   begin
      if Active (SQLITE) then
         return Db.Xref.Is_Read_Or_Write_Reference (Ref.Ref);
      else
         return Old_Entities.Is_Write_Reference
           (Old_Entities.Get_Kind (Ref.Old_Ref))
           or else Old_Entities.Is_Read_Reference
             (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Is_Read_Or_Write_Reference;

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

   procedure Destroy (Self : in out General_Xref_Database) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (General_Xref_Database_Record'Class, General_Xref_Database);
   begin
      if Active (SQLITE) then
         Self.Xref.Free;
         Self.Xref := null;
      else
         Old_Entities.Destroy (Self.Entities);
      end if;

      Free (Self.Constructs);

      Unchecked_Free (Self);
   end Destroy;

   ------------
   -- Freeze --
   ------------

   function Freeze
     (Self : access General_Xref_Database_Record) return Database_Lock is
   begin
      if not Active (SQLITE) then
         Old_Entities.Freeze (Self.Entities);
         return (Constructs =>
                   Old_Entities.Lock_Construct_Heuristics (Self.Entities));
      else
         return No_Lock;
      end if;
   end Freeze;

   ----------
   -- Thaw --
   ----------

   procedure Thaw
     (Self : access General_Xref_Database_Record;
      Lock : in out Database_Lock) is
   begin
      if not Active (SQLITE) then
         Old_Entities.Thaw (Self.Entities);
         Old_Entities.Unlock_Construct_Heuristics (Lock.Constructs);
      end if;
   end Thaw;

   ------------------
   -- End_Of_Scope --
   ------------------

   function End_Of_Scope
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Location
   is
      Iter : References_Cursor;
      Ref  : Entity_Reference;
   begin
      if Active (SQLITE) then
         Self.Xref.References (Entity.Entity, Cursor => Iter);
         while Has_Element (Iter) loop
            Ref := Element (Iter);
            if Ref.Is_End_Of_Scope then
               return (File   => Ref.File,
                       Line   => Ref.Line,
                       Column => Ref.Column);
            end if;

            Next (Iter);
         end loop;

      else
         declare
            use Old_Entities;
            Kind  : Old_Entities.Reference_Kind;
            Loc   : Old_Entities.File_Location;
         begin
            Old_Entities.Get_End_Of_Scope (Entity.Old_Entity, Loc, Kind);
            if Loc /= Old_Entities.No_File_Location then
               return (File => Get_Filename (Loc.File),
                       Line => Get_Line (Loc),
                       Column => Get_Column (Loc));
            end if;
         end;
      end if;
      return No_Location;
   end End_Of_Scope;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (Self         : access General_Xref_Database_Record;
      Lang_Handler :
         access Language.Tree.Database.Abstract_Language_Handler_Record'Class;
      Symbols      : GNATCOLL.Symbols.Symbol_Table_Access;
      Registry     : Projects.Project_Registry_Access;
      Subprogram_Ref_Is_Call : Boolean := False)
   is
      use Construct_Annotations_Pckg;
      LI_Entity_Key : Construct_Annotations_Pckg.Annotation_Key;
   begin
      Self.Constructs := new Language.Tree.Database.Construct_Database;
      Self.Lang_Handler := Abstract_Language_Handler (Lang_Handler);
      Set_Symbols (Self.Constructs, Symbols);

      Self.Symbols := Symbols;
      Self.Registry := Registry;

      Language.Tree.Database.Initialize
        (Db         => Self.Constructs,
         Lg_Handler => Abstract_Language_Handler (Lang_Handler));
      Get_Annotation_Key
        (Get_Construct_Annotation_Key_Registry (Self.Constructs).all,
         LI_Entity_Key);

      Register_Assistant
        (Self.Constructs,
         LI_Assistant_Id,
         new LI_Db_Assistant'
           (Database_Assistant with
            LI_Key => LI_Entity_Key,
            Db     => General_Xref_Database (Self)));

      if Active (SQLITE) then
         if Self.Xref = null then
            Self.Xref := new Extended_Xref_Database;
         end if;

      else
         Self.Entities := Old_Entities.Create
           (Registry,
            Self.Constructs,
            Normal_Ref_In_Call_Graph => Subprogram_Ref_Is_Call);

         Old_Entities.Set_Symbols (Self.Entities, Symbols);
         Old_Entities.Register_Language_Handler (Self.Entities, Lang_Handler);
         Old_Entities.Set_LI_Handler
           (Self.Entities, ALI_Parser.Create_ALI_Handler
              (Db           => Self.Entities,
               Registry     => Registry.all,
               Lang_Handler => Lang_Handler));
      end if;
   end Initialize;

   ------------------
   -- To_LI_Entity --
   ------------------

   function To_LI_Entity
     (Self : access General_Xref_Database_Record'Class;
      E    : Entity_Access) return General_Entity
   is
      use Construct_Annotations_Pckg;
      Entity : General_Entity;

      Assistant : constant LI_Db_Assistant_Access := LI_Db_Assistant_Access
        (Get_Assistant (Self.Constructs, LI_Assistant_Id));

      Construct_Annotation : Construct_Annotations_Pckg.Annotation;
      Loc : General_Location;
   begin
      Get_Annotation
        (Get_Annotation_Container
           (Get_Tree (Get_File (E)), To_Construct_Tree_Iterator (E)).all,
         Assistant.LI_Key,
         Construct_Annotation);

      if Construct_Annotation = Construct_Annotations_Pckg.Null_Annotation then
         Loc := (File => Get_File_Path (Get_File (E)),
                 Line => Get_Construct (E).Sloc_Entity.Line,
                 Column => To_Visible_Column
                  (Get_File (E),
                   Get_Construct (E).Sloc_Entity.Line,
                   String_Index_Type (Get_Construct (E).Sloc_Entity.Column)));

         --  Create a new LI entity

         if not Active (SQLITE) then
            declare
               use Old_Entities, Language;
               New_Entity  : Old_Entities.Entity_Information;
               Declaration : Old_Entities.File_Location;
               K           : E_Kinds;
               Is_Type     : Boolean := False;
            begin
               Declaration :=
                 (Get_Or_Create
                    (Db  => Assistant.Db.Entities, File  => Loc.File),
                  Loc.Line,
                  Loc.Column);

               --  Make a simple association between construct categories
               --  and entity categories. This association is known to be
               --  inaccurate, but is helpful when trying to categorize
               --  entities.

               case Get_Construct (E).Category is
               when Cat_Package | Cat_Namespace => K := Package_Kind;
               when Cat_Task
                  | Cat_Procedure
                  | Cat_Function
                  | Cat_Method
                  | Cat_Constructor
                  | Cat_Destructor
                  | Cat_Protected
                  | Cat_Entry =>

                  K := Procedure_Kind;

               when Cat_Class
                  | Cat_Structure
                  | Cat_Case_Inside_Record
                  | Cat_Union
                  | Cat_Type
                  | Cat_Subtype =>

                  K := Class;
                  Is_Type := True;

               when Cat_Variable
                  | Cat_Local_Variable
                  | Cat_Parameter
                  | Cat_Discriminant
                  | Cat_Field =>

                  K := Signed_Integer;

               when Cat_Literal =>

                  K := Enumeration_Literal;

               when Cat_With
                  | Cat_Use
                  | Cat_Include =>

                  K := Include_File;

               when Cat_Unknown
                  | Cat_Representation_Clause
                  | Cat_Loop_Statement
                  | Cat_If_Statement
                  | Cat_Case_Statement
                  | Cat_Select_Statement
                  | Cat_Accept_Statement
                  | Cat_Declare_Block
                  | Cat_Return_Block
                  | Cat_Simple_Block
                  | Cat_Exception_Handler
                  | Cat_Pragma
                  | Cat_Custom =>

                  K := Unresolved_Entity;
               end case;

               New_Entity := Old_Entities.Create_Dummy_Entity
                 (Name    => Get_Construct (E).Name,
                  Decl    => Declaration,
                  Kind    => K,
                  Is_Type => Is_Type);

               Entity := From_Old (New_Entity);
            end;

         else
            --  sqlite backend

            Entity := From_New
              (Self.Xref.Add_Entity
                 (Name        => Get (Get_Construct (E).Name).all,
                  Kind        => "procedure",
                  Decl_File   => Loc.File,
                  Decl_Project => Self.Registry.Tree.Info (Loc.File).Project,
                  Decl_Line   => Loc.Line,
                  Decl_Column => Integer (Loc.Column)));
         end if;

         Entity.Loc := Loc;
         Construct_Annotation := (Other_Kind, Other_Val => new LI_Annotation);
         LI_Annotation (Construct_Annotation.Other_Val.all).Entity := Entity;
         Set_Annotation
           (Get_Annotation_Container
              (Get_Tree (Get_File (E)), To_Construct_Tree_Iterator (E)).all,
            Assistant.LI_Key,
            Construct_Annotation);
      else
         null;
      end if;

      return LI_Annotation (Construct_Annotation.Other_Val.all).Entity;
   end To_LI_Entity;

   ----------
   -- Free --
   ----------

   overriding procedure Free (Obj : in out LI_Annotation) is
   begin
      Unref (Obj.Entity);
   end Free;

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

   function Hash
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return Integer
   is

      Decl : Entity_Declaration;
   begin
      if Active (SQLITE) then
         Decl := Self.Xref.Declaration (Entity.Entity);
         return Integer
           (Hash (To_String (Decl.Name)
            & Decl.Location.File.Display_Full_Name
            & Decl.Location.Line'Img
            & Decl.Location.Column'Img));

      elsif Entity.Old_Entity /= null then
         declare
            use Old_Entities;
            Loc : constant File_Location :=
              Get_Declaration_Of (Entity.Old_Entity);
         begin
            return Integer
              (Hash
                 (Get (Get_Name (Entity.Old_Entity)).all
                  & (+Full_Name (Get_Filename (Get_File (Loc))))
                  & Get_Line (Loc)'Img
                  & Get_Column (Loc)'Img));
         end;
      end if;

      return 0;
   end Hash;

   ---------
   -- Cmp --
   ---------

   function Cmp
     (Self   : access General_Xref_Database_Record;
      Entity1, Entity2 : General_Entity) return Integer
   is
   begin
      if Entity1 = No_General_Entity then
         if Entity2 = No_General_Entity then
            return 0;
         else
            return -1;
         end if;

      elsif Entity2 = No_General_Entity then
         return 1;

      else
         declare
            Name1 : constant String := Self.Get_Name (Entity1);
            Name2 : constant String := Self.Get_Name (Entity2);
         begin
            if Name1 < Name2 then
               return -1;

            elsif Name1 = Name2 then
               declare
                  File1 : constant Virtual_File :=
                    Self.Get_Declaration (Entity1).Loc.File;
                  File2 : constant Virtual_File :=
                    Self.Get_Declaration (Entity2).Loc.File;
               begin
                  if File1 < File2 then
                     return -1;
                  elsif File1 = File2 then
                     return 0;
                  else
                     return 1;
                  end if;
               end;

            else
               return 1;
            end if;
         end;
      end if;
   end Cmp;

   -----------------
   -- Has_Element --
   -----------------

   function Has_Element (Iter : File_Iterator) return Boolean is
   begin
      if Active (SQLITE) then
         return Has_Element (Iter.Iter);
      else
         if Iter.Is_Ancestor then
            return not Old_Entities.Queries.At_End (Iter.Old_Ancestor_Iter);
         else
            return not Old_Entities.Queries.At_End (Iter.Old_Iter);
         end if;
      end if;
   end Has_Element;

   ----------
   -- Next --
   ----------

   procedure Next (Iter : in out File_Iterator) is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         Next (Iter.Iter);
      else
         if Iter.Is_Ancestor then
            Old_Entities.Queries.Next (Iter.Old_Ancestor_Iter);
         else
            Old_Entities.Queries.Next (Iter.Old_Iter);
            while not Old_Entities.Queries.At_End (Iter.Old_Iter)
              and then
                (Get (Iter.Old_Iter) = null
                 or else not Old_Entities.Queries.Is_Explicit (Iter.Old_Iter))
            loop
               Old_Entities.Queries.Next (Iter.Old_Iter);
            end loop;
         end if;
      end if;
   end Next;

   -------------
   -- Element --
   -------------

   function Element (Iter : File_Iterator) return Virtual_File is
   begin
      if Active (SQLITE) then
         return Element (Iter.Iter);
      else
         if Iter.Is_Ancestor then
            return Old_Entities.Get_Filename
              (Old_Entities.Queries.Get (Iter.Old_Ancestor_Iter));
         else
            return Old_Entities.Get_Filename
              (Old_Entities.Queries.Get (Iter.Old_Iter));
         end if;
      end if;
   end Element;

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

   procedure Destroy (Iter : in out File_Iterator) is
   begin
      if not Active (SQLITE) then
         if Iter.Is_Ancestor then
            Old_Entities.Queries.Destroy (Iter.Old_Ancestor_Iter);
         end if;
      end if;
   end Destroy;

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

   procedure Destroy (Iter : in out File_Iterator_Access) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (File_Iterator'Class, File_Iterator_Access);
   begin
      if Iter /= null then
         Destroy (Iter.all);
         Unchecked_Free (Iter);
      end if;
   end Destroy;

   -----------------------
   -- Find_Dependencies --
   -----------------------

   function Find_Dependencies
     (Self : access General_Xref_Database_Record'Class;
      File : GNATCOLL.VFS.Virtual_File) return File_Iterator
   is
      use Old_Entities;
      Iter : File_Iterator;
   begin
      Iter.Is_Ancestor := False;

      if Active (SQLITE) then
         Iter.Iter := Self.Xref.Imports (File, Self.Registry.Tree.Info (File).Project);
      else
         Old_Entities.Queries.Find_Dependencies
           (Iter => Iter.Old_Iter,
            File => Old_Entities.Get_Or_Create
              (Self.Entities, File, Allow_Create => True));

         while not Old_Entities.Queries.At_End (Iter.Old_Iter)
           and then
             (Get (Iter.Old_Iter) = null
              or else not Old_Entities.Queries.Is_Explicit (Iter.Old_Iter))
         loop
            Old_Entities.Queries.Next (Iter.Old_Iter);
         end loop;
      end if;
      return Iter;
   end Find_Dependencies;

   --------------------------------
   -- Find_Ancestor_Dependencies --
   --------------------------------

   function Find_Ancestor_Dependencies
     (Self                  : access General_Xref_Database_Record'Class;
      File                  : GNATCOLL.VFS.Virtual_File) return File_Iterator
   is
      use Old_Entities;
      Iter : File_Iterator;
   begin
      Iter.Is_Ancestor := True;

      if Active (SQLITE) then
         Iter.Iter := Self.Xref.Imported_By (File, Self.Registry.Tree.Info (File).Project);
      else
         Old_Entities.Queries.Find_Ancestor_Dependencies
           (Iter               => Iter.Old_Ancestor_Iter,
            File               => Old_Entities.Get_Or_Create
              (Self.Entities, File, Allow_Create => True),
            Include_Self       => False,
            Single_Source_File => False);

         while not Old_Entities.Queries.At_End (Iter.Old_Ancestor_Iter)
           and then
             (Get (Iter.Old_Ancestor_Iter) = null
              or else
                not Old_Entities.Queries.Is_Explicit (Iter.Old_Ancestor_Iter))
         loop
            Old_Entities.Queries.Next (Iter.Old_Ancestor_Iter);
         end loop;
      end if;
      return Iter;
   end Find_Ancestor_Dependencies;

   ----------------------
   -- Get_Display_Kind --
   ----------------------

   function Get_Display_Kind
     (Ref  : General_Entity_Reference) return String is
   begin
      if Active (SQLITE) then
         return Ada.Strings.Unbounded.To_String (Ref.Ref.Kind);
      else
         return Old_Entities.Kind_To_String
           (Old_Entities.Get_Kind (Ref.Old_Ref));
      end if;
   end Get_Display_Kind;

   ------------------------------
   -- All_Real_Reference_Kinds --
   ------------------------------

   function All_Real_Reference_Kinds
     (Db  : access General_Xref_Database_Record)
      return GNAT.Strings.String_List
   is
      use Old_Entities;
   begin
      if Active (SQLITE) then
         return Db.Xref.All_Real_Reference_Kinds;
      else
         return Result : String_List
           (Reference_Kind'Pos (Reference_Kind'First) + 1 ..
              Reference_Kind'Pos (Reference_Kind'Last) + 1)
         do
            for R in Reference_Kind'Range loop
               Result (Reference_Kind'Pos (R) + 1) :=
                 new String'(Kind_To_String (R));
            end loop;
         end return;
      end if;
   end All_Real_Reference_Kinds;

   --------------
   -- Is_Fuzzy --
   --------------

   function Is_Fuzzy (Entity : General_Entity) return Boolean is
   begin
      return Entity.Is_Fuzzy;
   end Is_Fuzzy;

   ---------------------
   -- From_Constructs --
   ---------------------

   function From_Constructs
     (Entity : Language.Tree.Database.Entity_Access) return General_Entity
   is
      Loc : General_Location;
   begin
      Loc :=
         (File => Get_File_Path (Get_File (Entity)),
          Line => Get_Construct (Entity).Sloc_Entity.Line,
          Column => To_Visible_Column
            (Get_File (Entity),
             Get_Construct (Entity).Sloc_Entity.Line,
             String_Index_Type (Get_Construct (Entity).Sloc_Entity.Column)));
      return (Loc => Loc, others => <>);
   end From_Constructs;

   -----------------
   -- Instance_Of --
   -----------------

   function Instance_Of
      (Self   : access General_Xref_Database_Record;
       Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Instance_Of (Entity.Entity));
      else
         return From_Old
           (Old_Entities.Queries.Is_Instantiation_Of (Entity.Old_Entity));
      end if;
   end Instance_Of;

   --------------------
   -- From_Instances --
   --------------------

   function From_Instances
     (Self   : access General_Xref_Database_Record;
      Ref    : General_Entity_Reference) return Entity_Array
   is
   begin
      if Active (SQLITE) then
         declare
            R : constant GNATCOLL.Xref.Entity_Array :=
              Self.Xref.From_Instances (Ref.Ref);
            Result : Entity_Array (R'Range);
         begin
            for A in R'Range loop
               Result (A) := From_New (R (A));
            end loop;
            return Result;
         end;

      else
         declare
            use Old_Entities;
            Inst : constant Entity_Instantiation :=
              Old_Entities.From_Instantiation_At (Ref.Old_Ref);
            Current : Entity_Instantiation := Inst;
            Count : Natural := 0;
         begin
            while Current /= No_Instantiation loop
               Count := Count + 1;
               Current := Generic_Parent (Current);
            end loop;

            declare
               Result : Entity_Array (1 .. Count);
            begin
               Count := Result'First;
               Current := Inst;
               while Current /= No_Instantiation loop
                  Result (Count) := From_Old (Get_Entity (Current));
                  Count := Count + 1;
                  Current := Generic_Parent (Current);
               end loop;

               return Result;
            end;
         end;
      end if;
   end From_Instances;

   -----------------------
   -- Fill_Entity_Array --
   -----------------------

   procedure Fill_Entity_Array
     (Curs : in out Entities_Cursor'Class;
      Arr  : in out Entity_Lists.List)
   is
   begin
      while Curs.Has_Element loop
         Arr.Append (From_New (Curs.Element));
         Curs.Next;
      end loop;
   end Fill_Entity_Array;

   ---------------------
   -- To_Entity_Array --
   ---------------------

   function To_Entity_Array
     (Arr : Entity_Lists.List) return Entity_Array
   is
      Result : Entity_Array (1 .. Integer (Arr.Length));
      C      : Entity_Lists.Cursor := Arr.First;
   begin
      for R in Result'Range loop
         Result (R) := Element (C);
         Entity_Lists.Next (C);
      end loop;
      return Result;
   end To_Entity_Array;

   ---------------------
   -- Discriminant_Of --
   ---------------------

   function Discriminant_Of
      (Self              : access General_Xref_Database_Record;
       Entity            : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Discriminant_Of (Entity.Entity));
      else
         --  ??? Not implemented.
         --  Old_Entities.Queries.Is_Discriminant requires knowning the record
         --  itself before we event start.

         return No_General_Entity;
      end if;
   end Discriminant_Of;

   -------------------
   -- Discriminants --
   -------------------

   function Discriminants
      (Self              : access General_Xref_Database_Record;
       Entity            : General_Entity) return Entity_Array
   is
      Arr : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
         begin
            Self.Xref.Discriminants (Entity.Entity, Cursor => Curs);
            Fill_Entity_Array (Curs, Arr);
         end;

      else
         declare
            Iter  : Old_Entities.Queries.Entity_Reference_Iterator;
            Discr : Old_Entities.Entity_Information;
         begin
            Old_Entities.Queries.Find_All_References
              (Iter, Entity.Old_Entity,
               Filter => (Old_Entities.Discriminant => True, others => False));

            while not At_End (Iter) loop
               Discr := Get_Entity (Iter);
               if Discr /= null then
                  Append (Arr, From_Old (Discr));
               end if;

               Next (Iter);
            end loop;

            Destroy (Iter);
         end;
      end if;

      return To_Entity_Array (Arr);
   end Discriminants;

   -----------------------
   -- Formal_Parameters --
   -----------------------

   function Formal_Parameters
      (Self   : access General_Xref_Database_Record;
       Entity : General_Entity) return Entity_Array
   is
      use Old_Entities;
      Arr : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
         begin
            Self.Xref.Formal_Parameters (Entity.Entity, Cursor => Curs);
            Fill_Entity_Array (Curs, Arr);
         end;

      else
         declare
            Param : Old_Entities.Entity_Information;
            Iter  : Old_Entities.Queries.Generic_Iterator :=
              Get_Generic_Parameters (Entity.Old_Entity);
         begin
            loop
               Get (Iter, Param);
               exit when Param = null;

               Append (Arr, From_Old (Param));
               Next (Iter);
            end loop;
         end;
      end if;

      return To_Entity_Array (Arr);
   end Formal_Parameters;

   --------------
   -- Literals --
   --------------

   function Literals
      (Self              : access General_Xref_Database_Record;
       Entity            : General_Entity) return Entity_Array
   is
      use Old_Entities;
      Arr : Entity_Lists.List;
   begin
      if Active (Me) then
         Increase_Indent
           (Me, "Retrieving literals of " & Self.Get_Name (Entity));
      end if;

      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
         begin
            Self.Xref.Literals (Entity.Entity, Cursor => Curs);
            Fill_Entity_Array (Curs, Arr);
         end;

      elsif Get_Kind (Entity.Old_Entity).Kind = Enumeration_Kind then
         declare
            Field : Old_Entities.Entity_Information;
            Iter  : Old_Entities.Queries.Calls_Iterator :=
              Get_All_Called_Entities (Entity.Old_Entity);
         begin
            while not At_End (Iter) loop
               Field := Get (Iter);

               if Active (Me) then
                  Trace
                    (Me, "Old: candidate: "
                     & Self.Get_Name (From_Old (Field))
                     & " range="
                     & In_Range (Old_Entities.Get_Declaration_Of (Field),
                       Entity.Old_Entity)'Img
                     & " cat=" & Get_Category (Field)'Img);
               end if;

               if In_Range (Old_Entities.Get_Declaration_Of (Field),
                            Entity.Old_Entity)
                 and then Get_Category (Field) = Literal
               then
                  Append (Arr, From_Old (Field));
               end if;

               Next (Iter);
            end loop;

            Destroy (Iter);
         end;
      end if;

      if Active (Me) then
         Decrease_Indent (Me);
      end if;

      return To_Entity_Array (Arr);
   end Literals;

   -----------------
   -- Child_Types --
   -----------------

   function Child_Types
      (Self      : access General_Xref_Database_Record;
       Entity    : General_Entity;
       Recursive : Boolean) return Entity_Array
   is
      use Old_Entities;
      Arr : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
            Rec  : Recursive_Entities_Cursor;
         begin
            if Recursive then
               Self.Xref.Recursive
                 (Entity  => Entity.Entity,
                  Compute => GNATCOLL.Xref.Child_Types'Access,
                  Cursor  => Rec);
               Fill_Entity_Array (Rec, Arr);
            else
               Self.Xref.Child_Types (Entity.Entity, Cursor => Curs);
               Fill_Entity_Array (Curs, Arr);
            end if;
         end;

      else
         declare
            Children : Children_Iterator :=
              Get_Child_Types (Entity.Old_Entity, Recursive => Recursive);
         begin
            while not At_End (Children) loop
               if Get (Children) /= null then
                  Append (Arr, From_Old (Get (Children)));
               end if;
               Next (Children);
            end loop;
            Destroy (Children);
         end;
      end if;

      return To_Entity_Array (Arr);
   end Child_Types;

   ------------------
   -- Parent_Types --
   ------------------

   function Parent_Types
      (Self      : access General_Xref_Database_Record;
       Entity    : General_Entity;
       Recursive : Boolean) return Entity_Array
   is
      use Old_Entities;
      Arr : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
            Rec  : Recursive_Entities_Cursor;
         begin
            if Recursive then
               Self.Xref.Recursive
                 (Entity  => Entity.Entity,
                  Compute => GNATCOLL.Xref.Parent_Types'Access,
                  Cursor  => Rec);
               Fill_Entity_Array (Rec, Arr);
            else
               Self.Xref.Parent_Types (Entity.Entity, Cursor => Curs);
               Fill_Entity_Array (Curs, Arr);
            end if;
         end;

      else
         declare
            Parents : constant Entity_Information_Array :=
              Get_Parent_Types (Entity.Old_Entity, Recursive);
         begin
            for P in Parents'Range loop
               Append (Arr, From_Old (Parents (P)));
            end loop;
         end;
      end if;

      return To_Entity_Array (Arr);
   end Parent_Types;

   ------------
   -- Fields --
   ------------

   function Fields
      (Self              : access General_Xref_Database_Record;
       Entity            : General_Entity) return Entity_Array
   is
      use Old_Entities;
      Arr : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
         begin
            Self.Xref.Fields (Entity.Entity, Cursor => Curs);
            Fill_Entity_Array (Curs, Arr);
         end;

      --  Ignore for enumerations
      elsif Get_Kind (Entity.Old_Entity).Kind /= Enumeration_Kind then
         declare
            Field : Old_Entities.Entity_Information;
            Iter  : Old_Entities.Queries.Calls_Iterator :=
              Get_All_Called_Entities (Entity.Old_Entity);
         begin
            while not At_End (Iter) loop
               Field := Get (Iter);

               --  Hide discriminants and subprograms (would happen in C++,
               --  but these are primitive operations in this case)

               if In_Range (Old_Entities.Get_Declaration_Of (Field),
                            Entity.Old_Entity)
                 and then not Is_Discriminant (Field, Entity.Old_Entity)
                 and then not Old_Entities.Is_Subprogram (Field)
                 and then Get_Category (Field) /= Type_Or_Subtype
               then
                  Append (Arr, From_Old (Field));
               end if;

               Next (Iter);
            end loop;

            Destroy (Iter);
         end;
      end if;

      return To_Entity_Array (Arr);
   end Fields;

   -------------
   -- Methods --
   -------------

   function Methods
      (Self              : access General_Xref_Database_Record;
       Entity            : General_Entity;
       Include_Inherited : Boolean) return Entity_Array
   is
      Result : Entity_Lists.List;
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
         begin
            Self.Xref.Methods (Entity.Entity, Curs,
                               Include_Inherited  => Include_Inherited);
            Fill_Entity_Array (Curs, Result);
         end;
      else
         declare
            use Old_Entities;
            Prim : Primitive_Operations_Iterator;
         begin
            Find_All_Primitive_Operations
              (Iter              => Prim,
               Entity            => Entity.Old_Entity,
               Include_Inherited => Include_Inherited);

            while not At_End (Prim) loop
               Append (Result, From_Old (Get (Prim)));
               Next (Prim);
            end loop;

            Destroy (Prim);
         end;
      end if;

      return To_Entity_Array (Result);
   end Methods;

   --------------------
   -- Component_Type --
   --------------------

   function Component_Type
      (Self   : access General_Xref_Database_Record;
       Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Component_Type (Entity.Entity));
      else
         return From_Old (Old_Entities.Queries.Array_Contents_Type
                            (Entity.Old_Entity));
      end if;
   end Component_Type;

   --------------------
   -- Parent_Package --
   --------------------

   function Parent_Package
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Parent_Package (Entity.Entity));
      else
         return From_Old
           (Old_Entities.Queries.Get_Parent_Package (Entity.Old_Entity));
      end if;
   end Parent_Package;

   -----------------
   -- Index_Types --
   -----------------

   function Index_Types
      (Self   : access General_Xref_Database_Record;
       Entity : General_Entity) return Entity_Array
   is
   begin
      if Active (SQLITE) then
         declare
            Curs : Entities_Cursor;
            Arr  : Entity_Lists.List;
         begin
            Self.Xref.Index_Types (Entity.Entity, Cursor => Curs);
            Fill_Entity_Array (Curs, Arr);

            return To_Entity_Array (Arr);
         end;
      else
         declare
            Indexes : constant Old_Entities.Entity_Information_Array :=
              Old_Entities.Queries.Array_Index_Types (Entity.Old_Entity);
            Result : Entity_Array (Indexes'Range);
         begin
            for R in Result'Range loop
               Result (R) := From_Old (Indexes (R));
            end loop;
            return Result;
         end;
      end if;
   end Index_Types;

   ---------------
   -- Overrides --
   ---------------

   function Overrides
     (Self   : access General_Xref_Database_Record;
      Entity : General_Entity) return General_Entity
   is
   begin
      if Active (SQLITE) then
         return From_New (Self.Xref.Overrides (Entity.Entity));
      else
         return From_Old (Old_Entities.Queries.Overriden_Entity
                          (Entity.Old_Entity));
      end if;
   end Overrides;

   -------------------------------
   -- Select_Entity_Declaration --
   -------------------------------

   function Select_Entity_Declaration
     (Self   : access General_Xref_Database_Record;
      File   : GNATCOLL.VFS.Virtual_File;
      Entity : General_Entity) return General_Entity
   is
      pragma Unreferenced (Self, File);
   begin
      return Entity;
   end Select_Entity_Declaration;

   -----------
   -- Reset --
   -----------

   procedure Reset (Self : access General_Xref_Database_Record) is
   begin
      if not Active (SQLITE) then
         Old_Entities.Reset (Self.Entities);
      end if;
   end Reset;

   --------------------------
   -- Get_Entity_Reference --
   --------------------------

   function Get_Entity_Reference
     (Old_Ref : Old_Entities.Entity_Reference) return General_Entity_Reference
   is
      GER : constant General_Entity_Reference :=
        (Old_Ref => Old_Ref,
         Ref => No_Entity_Reference);
   begin
      return GER;
   end Get_Entity_Reference;

   ---------------------
   -- Project_Changed --
   ---------------------

   procedure Project_Changed (Self : General_Xref_Database) is
      Error : GNAT.Strings.String_Access;
   begin
      if Active (SQLITE) then
         --  Create an initial empty database. It will never be filled, and
         --  will be shortly replaced in Project_View_Changed, but it ensures
         --  that GPS does not raise exceptions if some action is performed
         --  while the project has not been computed (like loading of the
         --  desktop for instance).
         --  ??? We really should not be doing anything until the project has
         --  been computed.
         Trace (Me, "Set up xref database: :memory:");
         if Self.Xref = null then
            Trace (Me, "Set up xref database: :memory:");
         end if;

         Self.Xref.Setup_DB (Self.Registry.Tree, GNATCOLL.SQL.Sqlite.Setup (":memory:"), Error);
         --  Not interested in schema version errors, gnatinspect already
         --  displays them on the console
         Free (Error);
      else
         --  When loading a new project, we need to reset the cache containing
         --  LI information, otherwise this cache might contain dangling
         --  references to projects that have been freed. Recompute_View does
         --  something similar but tries to limit the files that are reset, so
         --  the calls below will just speed up the processing in
         --  Recompute_View when a new project is loaded.

         Old_Entities.Reset (Self.Entities);
      end if;
   end Project_Changed;

   --------------------------
   -- Project_View_Changed --
   --------------------------

   procedure Project_View_Changed
     (Self   : General_Xref_Database;
      Tree   : Project_Tree_Access)
   is

      procedure Reset_File_If_External (S : in out Old_Entities.Source_File);
      --  Reset the xref info for a source file that no longer belongs to the
      --  project.

      ----------------------------
      -- Reset_File_If_External --
      ----------------------------

      procedure Reset_File_If_External (S : in out Old_Entities.Source_File) is
         Info : constant File_Info :=
           Tree.Info (Old_Entities.Get_Filename (S));
      begin
         if Info.Project = No_Project then
            Old_Entities.Reset (S);
         end if;
      end Reset_File_If_External;

      Dir  : Virtual_File;
      File : Virtual_File;
      Error : GNAT.Strings.String_Access;
   begin
      if Active (SQLITE) then
         --  Self.Xref was initialized in Project_Changed.
         Self.Xref.Free;

         Dir := Tree.Root_Project.Object_Dir;

         if Dir = No_File then
            Trace (Me, "Object_Dir is unknown for the root project "
                   & Tree.Root_Project.Project_Path.Display_Full_Name);
            Dir := GNATCOLL.VFS.Get_Current_Dir;
         end if;

         File := Create_From_Dir
           (Dir       => Dir,
            Base_Name => "gnatinspect.db");

         Trace (Me, "Set up xref database: " & File.Display_Full_Name);
         Self.Xref.Setup_DB (Tree, GNATCOLL.SQL.Sqlite.Setup (+File.Full_Name.all), Error);
         --  Not interested in schema version errors, gnatinspect already
         --  displays them on the console
         Free (Error);

         --  ??? Now would be a good opportunity to update the cross-references
         --  rather than wait for the next compilation.

      else
         --  The list of source or ALI files might have changed, so we need to
         --  reset the cache containing LI information, otherwise this cache
         --  might contain dangling references to projects that have been
         --  freed. We used to do this only when loading a new project, but
         --  in fact that is not sufficient: when we look up xref info for a
         --  source file, if we haven't reset the cache we might get a reply
         --  pointing to a source file in a directory that is no longer part
         --  of the project in the new scenario.
         --
         --  In fact, we only reset the info for those source files that are no
         --  longer part of the project. This might take longer than dropping
         --  the whole database since in the former case we need to properly
         --  handle refcounting whereas Reset takes a shortcut. It is still
         --  probably cleaner to only reset what's needed.

         Old_Entities.Foreach_Source_File
           (Self.Entities, Reset_File_If_External'Access);
      end if;
   end Project_View_Changed;

end Xref;