awa_unit_2.4.0_59135a52/dynamo/src/asis/a4g-expr_sem.adb

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
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
------------------------------------------------------------------------------
--                                                                          --
--                 ASIS-for-GNAT IMPLEMENTATION COMPONENTS                  --
--                                                                          --
--                          A 4 G . E X P R _ S E M                         --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--            Copyright (C) 1995-2012, Free Software Foundation, Inc.       --
--                                                                          --
-- ASIS-for-GNAT is free software; you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software Foundation;  either version 2,  or  (at your option)  any later --
-- version. ASIS-for-GNAT is distributed  in the hope  that it will be use- --
-- ful, but WITHOUT ANY WARRANTY; without even the implied warranty of MER- --
-- CHANTABILITY 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 ASIS-for-GNAT;  see file --
-- COPYING.  If not,  write  to the  Free Software Foundation,  51 Franklin --
-- Street, Fifth Floor, Boston, MA 02110-1301, USA.                         --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- ASIS-for-GNAT was originally developed  by the ASIS-for-GNAT team at the --
-- Software  Engineering  Laboratory  of  the Swiss  Federal  Institute  of --
-- Technology (LGL-EPFL) in Lausanne,  Switzerland, in cooperation with the --
-- Scientific  Research  Computer  Center of  Moscow State University (SRCC --
-- MSU), Russia,  with funding partially provided  by grants from the Swiss --
-- National  Science  Foundation  and  the  Swiss  Academy  of  Engineering --
-- Sciences. ASIS-for-GNAT is now maintained by AdaCore                     --
-- (http://www.adacore.com).                                                --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Wide_Characters.Unicode;

with Asis.Clauses;           use Asis.Clauses;
with Asis.Compilation_Units; use Asis.Compilation_Units;
with Asis.Declarations;      use Asis.Declarations;
with Asis.Elements;          use Asis.Elements;
with Asis.Expressions;       use Asis.Expressions;
with Asis.Extensions;        use Asis.Extensions;
with Asis.Iterator;          use Asis.Iterator;
with Asis.Statements;        use Asis.Statements;

with Asis.Set_Get;           use Asis.Set_Get;

with A4G.A_Debug;            use A4G.A_Debug;
with A4G.A_Output;           use A4G.A_Output;
with A4G.A_Sem;              use A4G.A_Sem;
with A4G.A_Stand;            use A4G.A_Stand;
with A4G.A_Types;            use A4G.A_Types;
with A4G.Asis_Tables;        use A4G.Asis_Tables;
with A4G.Contt.UT;           use A4G.Contt.UT;
with A4G.Int_Knds;           use A4G.Int_Knds;
with A4G.Knd_Conv;           use A4G.Knd_Conv;
with A4G.Mapping;            use A4G.Mapping;

with Atree;                  use Atree;
with Einfo;                  use Einfo;
with Namet;                  use Namet;
with Nlists;                 use Nlists;
with Output;                 use Output;
with Sem_Aux;                use Sem_Aux;
with Sinfo;                  use Sinfo;
with Snames;                 use Snames;
with Stand;                  use Stand;
with Types;                  use Types;

package body A4G.Expr_Sem is

   -----------------------
   -- Local subprograms --
   -----------------------

   function Explicit_Type_Declaration (Entity_Node : Node_Id) return Node_Id;
   --  Taking the Entity node obtained as a result of some call to Etype
   --  function, this function yields the node for corresponding explicit
   --  type or subtype declaration. This means that this function traverses all
   --  the internal types generated by the compiler.
   --
   --  In case of an anonymous access type, this function returns the entity
   --  node which is created by the compiler for this type (there is no tree
   --  type structure for the type declaration in this case), and a caller is
   --  responsible for further analysis
   --
   --  SHOULD WE MOVE THIS FUNCTION IN THE SPEC???

   function Explicit_Type_Declaration_Unwound
     (Entity_Node    : Node_Id;
      Reference_Node : Node_Id := Empty)
      return Node_Id;
   --  Does the same as Explicit_Type_Declaration and unwinds all the
   --  subtypings (if any), resulting in a root type declaration.
   --  Reference_Node is a node representing a "place" from which this function
   --  is called. If the result type is private, but from the "place" of the
   --  call the full view is visible, the full view is returned. If
   --  Reference_Node is Empty, no private/full view check is made

   function Explicit_Type_Declaration_Unwound_Unaccess
     (Entity_Node    : Node_Id;
      Reference_Node : Node_Id := Empty)
      return Node_Id;
   --  Does the same as Explicit_Type_Declaration_Unwound and in case of access
   --  types goes from the access to the designated type. --???
   --
   --  In case of an anonymous access type returns directly designated type.

   function Rewritten_Image (Selector_Name : Node_Id) return Node_Id;
   pragma Unreferenced (Rewritten_Image);
   --  this is an example of the tricky programming needed because of the
   --  tree rewriting. The problem is, that in the original tree structure
   --  for a record aggregate a N_Identifier node for a component selector
   --  name does not have an Entity field set. So we have to go to the
   --  corresponding (that is, to representing the same component selector
   --  name) node in the rewritten structure.
   --
   --  It is an error to use this function for a node which is does not
   --  represent a component selector name in the original tree structure
   --  for a record aggregate
   --
   --  This function is not used now, it is replaced by Search_Record_Comp

   function Search_Record_Comp (Selector_Name : Node_Id) return Entity_Id;
   --  This function looks for the entity node corresponding to a name from a
   --  choices list from a record or extension aggregate. The problem here is
   --  that an aggregate node is rewritten, and in the original tree structure
   --  the nodes corresponding to component names do not have the Entity
   --  field set. This function locates the corresponding entity node by
   --  detecting the aggregate type and searching the component defining
   --  identifier with the same name in the record definition.
   --  It might be the case (because of the absence of some semantic
   --  information in the tree or because of the ASIS bug) that Selector_Name
   --  actually does not represent a name from the aggregate choice list, in
   --  this case this function raises Assert_Failure or (if assertions are off)
   --  returns the Empty node.

   function Get_Statement_Identifier (Def_Id : Node_Id) return Node_Id;
   --  For Int_Node which should represent the defining identifier from an
   --  implicit declaration of a label (or a statement name?) (otherwise it
   --  is an error to use this function), this function returns the "defining"
   --  name representing the definition of this statement identifier in the
   --  ASIS sense.

   function GFP_Declaration (Par_Id : Node_Id) return Node_Id;
   --  this is (I hope, temporary) fix for the problem 14: the Entity
   --  field is not set for N_Identifier node representing the parameter
   --  name in a named generic association, so we need this function to
   --  compute the Entity for such an N_Identifier node.
   --  ???   what about formal parameters in associations like
   --  ???           "*" => Some_Function
   --
   --  This function is supposed to be called for an actual representing
   --  the name of a generic formal parameter in a named formal parameter
   --  association (it's an error to call it for any other actual)

   function Is_Explicit_Type_Component
     (Comp_Def_Name : Node_Id;
      Type_Decl     : Node_Id)
      return Boolean;
   --  Expects Comp_Def_Name to be a defining identifier of a record component
   --  and Type_Decl to be a type declaration. Checks if Comp_Def_Name denotes
   --  a component explicitly declared by this type declaration. (This function
   --  is useful for discriminants and components explicitly declared in
   --  derived type declarations.

   function Is_Type_Discriminant
     (Discr_Node : Node_Id;
      Type_Node  : Node_Id)
      return Boolean;
   --  Assuming that Discr_Node is N_Defining_Identifier node and Type_Node
   --  represents a type declaration, this function checks if Discr_Node is
   --  a discriminant of this type (we cannot just use Parent to check this
   --  because of tree rewriting for discriminant types.

   function Full_View_Visible
     (Priv_Type : Node_Id;
      Ref : Node_Id)
      return Boolean;
   --  Assuming that  Priv_Type is a node representing a private type
   --  declaration, checks, that in place of Ref the full view of the type is
   --  visible

   function Reset_To_Full_View
     (Full_View : Node_Id;
      Discr : Node_Id)
      return Node_Id;
   --  Assuming that Full_View is the full type declaration for some private
   --  type and Discr is a defining name of a discriminant of the type
   --  (probably, from its private view), this function returns the defining
   --  name of this discriminant in the full view

   function Is_Part_Of_Defining_Unit_Name (Name_Node : Node_Id) return Boolean;
   --  Assuming that Name_Node is of N_Identifier kind, this function checks,
   --  if it is a part of a defining program unit name

   function Reset_To_Spec (Name_Node : Node_Id) return Node_Id;
   --  Assuming that Name_Node is a part of a defining unit name which in turn
   --  is a part of a compilation unit body (for such nodes Entity field is not
   --  set), this function resets it to the node pointing to the same part of
   --  the defining unit name, but in the spec of the corresponding library
   --  unit

   function Reference_Kind
     (Name            : Asis.Element)
      return            Internal_Element_Kinds;
   --  If Name is of A_Defining_Name kind then this function returns the kind
   --  of An_Expression elements which may be simple-name-form references to
   --  the given name (that is, A_Defining_Identifier -> An_Identifier,
   --  A_Defining_And_Operator -> An_And_Operator), otherwise returns
   --  Not_An_Element. Note, that the result can never be
   --  A_Selected_Component, because only references which are simple names
   --  are considered.

   function Get_Specificed_Component
     (Comp     : Node_Id;
      Rec_Type : Entity_Id)
      return     Entity_Id;
   --  Provided that Comp is the reference to a record component from the
   --  component clause being a component of a record representation clause
   --  for the record type Rec_Type, this function computes the corresponding
   --  component entity

   function Get_Entity_From_Long_Name (N : Node_Id) return Entity_Id;
   --  Supposing that N denotes some component of a long expanded name
   --  and for N and for its prefix the Entity fields are not set, this
   --  function computes the corresponding entity node by traversing
   --  the "chain" of definitions corresponding to this expanded name

   function Get_Rewritten_Discr_Ref (N : Node_Id) return Node_Id;
   pragma Unreferenced (Get_Rewritten_Discr_Ref);
   --  This function is supposed to be called for a discriminant reference
   --  from the discriminant constraint from derived type, in case if the
   --  parent type is a task or protected type. In this case
   --  N_Subtype_Indication node from the derived type definition is rewritten
   --  in a subtype mark pointing to the internal subtype. The original
   --  structure is not decorated, so we have to go to the corresponding
   --  node in the definition of this internal subtype to get the semantic
   --  information. See F407-011
   --  Do we need this after fixing the regression caused by K120-031

   function Get_Discriminant_From_Type (N : Node_Id) return Entity_Id;
   --  Starting from the reference to discriminant in a discriminant
   --  constraint, tries to compute the corresponding discriminant entity by
   --  getting to the declaration of the corresponding type and traversing
   --  its discriminant part.

   function Is_Limited_Withed
     (E         : Entity_Id;
      Reference : Asis.Element)
      return Boolean;
   --  Assuming that Reference is an_Identifier Element and E is the entity
   --  node for the entity denoted by Reference, checks if this entity is
   --  defined in a compilation unit that is limited withed by the unit
   --  containing Reference

   function To_Upper_Case (S : Wide_String) return Wide_String;
   --  Folds the argument to upper case, may be used for string normalization
   --  before comparing strings if the casing is not important for comparing
   --  (Copied from ASIS_UL.Misc to avoid dependencies on ASIS UL in "pure"
   --  ASIS.

   ---------------------------------------
   -- Character_Literal_Name_Definition --
   ---------------------------------------

   function Character_Literal_Name_Definition
     (Reference_Ch : Element)
      return         Asis.Defining_Name
   is
      --  for now, the code is very similar to the code
      --  for Identifier_Name_Definition. Any aggregation has been
      --  put off till everything will work

      Arg_Node         : Node_Id;
      Special_Case     : Special_Cases          := Not_A_Special_Case;
      Result_Element   : Asis.Defining_Name     := Nil_Element;
      Is_Inherited     : Boolean                := False;
      Association_Type : Node_Id                := Empty;
      Set_Char_Code    : Boolean                := False;

      Result_Node : Node_Id;
      Result_Unit : Compilation_Unit;
      Result_Kind : constant Internal_Element_Kinds :=
        A_Defining_Character_Literal;
   begin
      --  We have to distinguish and to treat separately four (???)
      --  different situations:
      --
      --  1. a literal from user-defined character type (fully implemented
      --     for now);
      --
      --  2. a literal from a type derived from some user-defined character
      --     type (not implemented for now as related to Implicit Elements);
      --
      --  3. a literal from a character type defined in Standard (not
      --     implemented for now);
      --
      --  4. a literal from a type derived a character type defined in
      --     Standard  (not implemented for now as related to Implicit
      --     Elements);

      Arg_Node := Node (Reference_Ch);

      --  if Reference_Ch is a Selector_Name in some N_Expanded_Name,
      --  the corresponding Entity field is set not for the Node on which
      --  this Reference_En is based, but for the whole expanded name.
      --  (The same for Etype) So:

      if Nkind (Parent (Arg_Node)) = N_Expanded_Name or else
         Nkind (Parent (Arg_Node)) = N_Character_Literal
      then
         --  the last alternative of the condition corresponds to an expanded
         --  name of a predefined character literal or to an expanded name
         --  of a literal of a type derived from a predefined character type -
         --  such an expanded name is rewritten into (another) "instance"
         --  of the same literal
         Arg_Node := Parent (Arg_Node);
      end if;

      Result_Node := Entity (Arg_Node);
      --  will be Empty for any character literal belonging to
      --  Standard.Character, Standard.Whide_Character or any type
      --  derived (directly or indirectly) from any of these types

      Association_Type := Etype (Arg_Node);

      if No (Result_Node)      and then
         No (Association_Type) and then
         Is_From_Unknown_Pragma (R_Node (Reference_Ch))
      then
         return Nil_Element;
      end if;

      if No (Association_Type) then
         --  this may be the case if some character literals are
         --  rewritten into a string constant
         Association_Type := Arg_Node;

         while Present (Association_Type) loop
            exit when Nkind (Association_Type) = N_String_Literal;
            Association_Type := Parent (Association_Type);
         end loop;

         pragma Assert (Present (Association_Type));

         Association_Type := Etype (Association_Type);
         Association_Type := Component_Type (Association_Type);
      end if;

      Association_Type := Explicit_Type_Declaration_Unwound (Association_Type);

      if No (Result_Node) then
         Set_Char_Code := True;
         Result_Node   := Association_Type;
         Result_Node   := Sinfo.Type_Definition (Result_Node);

         if Char_Defined_In_Standard (Arg_Node) then
            Special_Case := Stand_Char_Literal;
         else
            Is_Inherited := True;
         end if;

      elsif not Comes_From_Source (Result_Node) then
         Is_Inherited := True;
      end if;

      if Char_Defined_In_Standard (Arg_Node) then
         Result_Unit  := Get_Comp_Unit
                           (Standard_Id, Encl_Cont_Id (Reference_Ch));
      else
         Result_Unit := Enclosing_Unit
                          (Encl_Cont_Id (Reference_Ch), Result_Node);
      end if;

      Result_Element := Node_To_Element_New
                          (Node          => Result_Node,
                           Node_Field_1  => Association_Type,
                           Internal_Kind => Result_Kind,
                           Spec_Case     => Special_Case,
                           Inherited     => Is_Inherited,
                           In_Unit       => Result_Unit);

      if Set_Char_Code then
         Set_Character_Code (Result_Element, Character_Code (Reference_Ch));
      end if;

      return Result_Element;

   end Character_Literal_Name_Definition;

   ---------------------------------
   -- Collect_Overloaded_Entities --
   ---------------------------------

   procedure Collect_Overloaded_Entities (Reference : Asis.Element) is
      Arg_Node               : Node_Id;
      Arg_Pragma_Chars       : Name_Id;

      Next_Entity            : Entity_Id;
      Result_Unit            : Asis.Compilation_Unit;
      Result_Context         : constant Context_Id := Encl_Cont_Id (Reference);

      Res_Node               : Node_Id;
      Res_NF_1               : Node_Id;
      Res_Ekind              : Entity_Kind;
      Res_Inherited          : Boolean;

      Is_Program_Unit_Pragma : Boolean := False;
      Enclosing_Scope_Entity : Entity_Id;
      Enclosing_List         : List_Id;

      function Should_Be_Collected (Ent : Entity_Id) return Boolean;
      --  When traversing the chain of homonyms potentially referred by
      --  Reference, it checks if Ent should be used to create the next
      --  Element in the Result list

      function Should_Be_Collected (Ent : Entity_Id) return Boolean is
         Result : Boolean := False;
         N      : Node_Id;
      begin

         if not (Ekind (Ent) = E_Operator and then
                 Is_Predefined (Ent))
         then

            if Is_Program_Unit_Pragma then
               Result := Scope (Ent) = Enclosing_Scope_Entity;
            else
               N := Parent (Ent);

               while Present (N) and then
                     not (Is_List_Member (N))
               loop
                  N := Parent (N);
               end loop;

               if Present (N) and then Is_List_Member (N) then
                  Result := List_Containing (N) = Enclosing_List;
               end if;

            end if;

         end if;

         return Result;
      end Should_Be_Collected;

   begin
      --  First, we decide what kind of pragma we have, because the search
      --  depends on this:
      Arg_Node         := Node (Reference);
      Arg_Pragma_Chars := Pragma_Name (Parent (Parent (Arg_Node)));

      if Arg_Pragma_Chars = Name_Inline then
         Is_Program_Unit_Pragma := True;
         --  ??? is it enough? what about GNAT-specific pragmas?
         --  In this case we have to search in the same declarative region
         --  (in the same scope):
         Enclosing_Scope_Entity := Scope (Entity (Arg_Node));
         --  This is no more than a trick: actually, we have to compute
         --  the scope node for the declarative region which encloses
         --  Arg_Node, but entry bodies makes a serious problem (at the
         --  moment of writing this code there is no semantic links between
         --  protected entry declarations and bodies). So we just assume
         --  that Arg_Node has the Entity field set, and this field
         --  points to some correct  (from the point of view of
         --  Corresponding_Name_Definition_List query) entity, so we
         --  just take the Scope of this entity...

      else
         Enclosing_List := List_Containing (Parent (Parent (Arg_Node)));
      end if;

      Next_Entity := Entity (Arg_Node);

      while Present (Next_Entity) and then
            Should_Be_Collected (Next_Entity)
      loop

         Result_Unit := Enclosing_Unit (Result_Context, Next_Entity);

         Res_Ekind   := Ekind (Next_Entity);

         if Res_Ekind in Subprogram_Kind then

            if Comes_From_Source (Next_Entity) then
               Res_Node      := Next_Entity;
               Res_NF_1      := Empty;
               Res_Inherited := False;
            else
               Res_Node := Alias (Next_Entity);

               while Present (Alias (Res_Node)) loop
                  Res_Node := Alias (Res_Node);
               end loop;

               Res_NF_1      := Next_Entity;
               Res_Inherited := True;
            end if;

            Asis_Element_Table.Append
            (Node_To_Element_New (Node         => Res_Node,
                                  Node_Field_1 => Res_NF_1,
                                  Inherited    => Res_Inherited,
                                  In_Unit      => Result_Unit));
         end if;

         Next_Entity := Homonym (Next_Entity);
      end loop;

   end Collect_Overloaded_Entities;

   ---------------------------
   -- Correct_Impl_Form_Par --
   ---------------------------

   procedure Correct_Impl_Form_Par
     (Result    : in out Element;
      Reference :        Element)
   is
      Res_Node        : Node_Id := Node (Result);
      Subprogram_Name : Element;
      Subprogram_Node : Node_Id := Node (Result);
      Res_Sloc        : Source_Ptr;
      Top_Node        : Node_Id;
      Result_Unit     : Compilation_Unit;
   begin
      Res_Node := Defining_Identifier (Parent (Res_Node));

      Subprogram_Name := Enclosing_Element (Enclosing_Element (Reference));

      case Int_Kind (Subprogram_Name) is
         when A_Function_Call =>
            Subprogram_Name := Prefix (Subprogram_Name);
         when A_Procedure_Call_Statement |
              An_Entry_Call_Statement    =>
            Subprogram_Name := Called_Name (Subprogram_Name);
         when others =>
            null;
            pragma Assert (False);
      end case;

      Subprogram_Node := Node (Subprogram_Name);
      Subprogram_Node := Associated_Node (Subprogram_Node);

      Top_Node := Parent (Subprogram_Node);

      while Nkind (Top_Node) /= N_Compilation_Unit loop
         Top_Node := Parent (Top_Node);
      end loop;

      Res_Sloc := Sloc (Res_Node) - Sloc (Top_Node);

      Result_Unit :=
        Enclosing_Unit (Encl_Cont_Id (Reference), Subprogram_Node);

      Set_Node           (Result, Res_Node);
      Set_R_Node         (Result, Res_Node);
      Set_From_Implicit  (Result, True);
      Set_From_Inherited (Result, True);
      Set_From_Instance  (Result, Is_From_Instance (Subprogram_Node));
      Set_Node_Field_1   (Result, Subprogram_Node);
      Set_Rel_Sloc       (Result, Res_Sloc);
      Set_Encl_Unit_Id   (Result, Get_Unit_Id (Result_Unit));
   end Correct_Impl_Form_Par;

   --------------------
   -- Correct_Result --
   --------------------

   procedure Correct_Result
     (Result    : in out Element;
      Reference :        Element)
   is
      Enclosing_Generic : Element := Nil_Element;
      Tmp               : Element;
      Tmp_Generic       : Element;
      Is_From_Body      : Boolean := False;
      Instance          : Element := Nil_Element;

      procedure Check_Number_Name
        (Element :        Asis.Element;
         Control : in out Traverse_Control;
         State   : in out No_State);
      --  Check if the argument is the defining name of the named number
      --  defining the same named number as Result, but in the template.
      --  As soon as the check is successful, replace Result with this
      --  defining name and terminates the traversal

      Control : Traverse_Control := Continue;
      State   : No_State := Not_Used;

      procedure Traverse_Instance is new Traverse_Element
        (State_Information => No_State,
         Pre_Operation     => Check_Number_Name,
         Post_Operation    => No_Op);

      procedure Check_Number_Name
        (Element :        Asis.Element;
         Control : in out Traverse_Control;
         State   : in out No_State)
      is
         pragma Unreferenced (State);
         El_Kind : constant Internal_Element_Kinds := Int_Kind (Element);
      begin

         case El_Kind is

            when A_Defining_Identifier =>

               if Int_Kind (Enclosing_Element (Element)) in
                  An_Integer_Number_Declaration .. A_Real_Number_Declaration
                 and then
                  Chars (Node (Result)) = Chars (Node (Element))
               then
                  Result  := Element;
                  Control := Terminate_Immediately;
               end if;

            when An_Integer_Number_Declaration |
                 A_Real_Number_Declaration     |
                 A_Procedure_Body_Declaration  |
                 A_Function_Body_Declaration   |
                 A_Package_Declaration         |
                 A_Package_Body_Declaration    |
                 A_Task_Body_Declaration       |
                 A_Protected_Body_Declaration  |
                 An_Entry_Body_Declaration     |
                 A_Generic_Package_Declaration |
                 A_Block_Statement             =>

               null;
            when others =>
               Control := Abandon_Children;
         end case;

      end Check_Number_Name;

   begin
      --  First, check if Result is declared in a template

      Tmp := Enclosing_Element (Result);

      while not Is_Nil (Tmp) loop

         if Int_Kind (Tmp) in An_Internal_Generic_Declaration
           or else
            (Int_Kind (Tmp) in A_Procedure_Body_Declaration ..
                               A_Package_Body_Declaration
              and then
             Int_Kind (Corresponding_Declaration (Tmp)) in
               An_Internal_Generic_Declaration)
         then

            if Int_Kind (Tmp) in A_Procedure_Body_Declaration ..
                                 A_Package_Body_Declaration
            then
               Enclosing_Generic := Corresponding_Declaration (Tmp);
               Is_From_Body := True;
            else
               Enclosing_Generic := Tmp;
            end if;

            exit;

         end if;

         Tmp := Enclosing_Element (Tmp);
      end loop;

      if Is_Nil (Enclosing_Generic) then
         --  No need to correct anything!
         return;
      end if;

      --  Now, traversing the instantiation chain from the Reference, looking
      --  for the instantiation of Enlosing_Generic:

      Tmp := Enclosing_Element (Reference);

      while not Is_Nil (Tmp) loop

         if Int_Kind (Tmp) in An_Internal_Generic_Instantiation then
            Tmp_Generic := Generic_Unit_Name (Tmp);

            if Int_Kind (Tmp_Generic) = A_Selected_Component then
               Tmp_Generic := Selector (Tmp_Generic);
            end if;

            Tmp_Generic := Corresponding_Name_Declaration (Tmp_Generic);

            if Is_Equal (Enclosing_Generic, Tmp_Generic) then
               Instance := Tmp;
               exit;
            end if;

         end if;

         Tmp := Enclosing_Element (Tmp);
      end loop;

      if Is_Nil (Instance) then
         --  No need to correct anything - we do not have a nested generics!
         return;
      end if;

      --  And now we have to find the "image' of Result in expanded Instance
      if Is_From_Body then
         Instance := Corresponding_Body (Instance);
      else
         Instance := Corresponding_Declaration (Instance);
      end if;

      Traverse_Instance (Instance, Control, State);

   end Correct_Result;

   -------------------------------
   -- Explicit_Type_Declaration --
   -------------------------------

   function Explicit_Type_Declaration (Entity_Node : Node_Id) return Node_Id is
      Next_Node   : Node_Id;
      Result_Node : Node_Id;
      Res_Ekind   : Entity_Kind;

      function Is_Explicit_Type_Declaration
        (Type_Entity_Node : Node_Id)
         return Boolean;
      --  checks if Type_Entity_Node corresponds to the explicit type
      --  declaration which is looked for (that is, the needed type declaration
      --  node is Parent (Type_Entity_Node) )

      function Is_Explicit_Type_Declaration
        (Type_Entity_Node : Node_Id)
         return Boolean
      is
         Type_Decl_Node  : constant Node_Id := Parent (Type_Entity_Node);
         Type_Decl_Nkind : Node_Kind;

         Is_Full_Type_Decl    : Boolean := False;
         Is_Derived_Type_Decl : Boolean := False;
         Is_Formal_Type_Decl  : Boolean := False;
      begin

         if not Is_Itype (Entity_Node)
          and then
            Present (Type_Decl_Node)
         then

            Is_Full_Type_Decl :=
               Comes_From_Source (Type_Decl_Node) and then
               (not Is_Rewrite_Substitution (Type_Decl_Node));

            if not Is_Full_Type_Decl and then
               Is_Rewrite_Substitution (Type_Decl_Node)
            then
               --  The second part of the condition is common for all the cases
               --  which require special analysis

               Type_Decl_Nkind := Nkind (Type_Decl_Node);

               Is_Derived_Type_Decl :=
                 (Type_Decl_Nkind = N_Subtype_Declaration   or else
                  Type_Decl_Nkind = N_Full_Type_Declaration or else
                  Type_Decl_Nkind = N_Formal_Type_Declaration)
                 and then
                (Nkind (Original_Node (Type_Decl_Node)) =
                    N_Full_Type_Declaration and then
                 Nkind (Sinfo.Type_Definition (Original_Node (Type_Decl_Node)))
                    = N_Derived_Type_Definition);

               if not Is_Derived_Type_Decl then

                  Is_Formal_Type_Decl :=
                      (Type_Decl_Nkind = N_Private_Extension_Declaration
                      or else
                       Type_Decl_Nkind = N_Full_Type_Declaration)
                     and then
                  Nkind (Original_Node (Type_Decl_Node)) =
                     N_Formal_Type_Declaration;

               end if;

            end if;

         end if;

         return Is_Full_Type_Decl    or else
                Is_Derived_Type_Decl or else
                Is_Formal_Type_Decl;

      end Is_Explicit_Type_Declaration;

   begin
      --  well, here we have a (sub)type entity node passed as an actual...
      --  the aim is to return the _explicit_ type declaration corresponding
      --  to this (sub)type entity. It should be such a declaration, if this
      --  function is called...
      --
      --  We try to organize the processing in a recursive way - may be,
      --  not the most effective one, but easy-to maintain

      if Is_Explicit_Type_Declaration (Entity_Node) then
         --  the first part of the condition is the protection from
         --  non-accurate settings of Comes_From_Source flag :((
         Result_Node := Parent (Entity_Node);

      elsif Sloc (Entity_Node) <= Standard_Location then
         --  here we have a predefined type declared in Standard.
         --  it may be the type entity or the entity for its 'Base
         --  type. In the latter case we have to go to the type
         --  entity

         if Present (Parent (Entity_Node)) then
            --  type entity, therefore simply
            Result_Node := Parent (Entity_Node);
         else
            --  'Base type, so we have to compute the first named
            --  type. The code which does it looks tricky, but for now we
            --  do not know any better solution:
            Result_Node := Parent (Parent (Scalar_Range (Entity_Node)));
         end if;

      elsif Etype (Entity_Node) = Entity_Node                 and then
            Present (Associated_Node_For_Itype (Entity_Node)) and then
            Nkind (Associated_Node_For_Itype (Entity_Node)) =
               N_Object_Declaration
      then
         --  this corresponds to an anonymous array subtype created by an
         --  object declaration with array_type_definition
         Result_Node := Empty;

      else
         --  Entity_Node corresponds to some internal or implicit type created
         --  by the compiler. Here  we have to traverse the tree till the
         --  explicit type declaration being the cause for generating this
         --  implicit type will be found

         Res_Ekind := Ekind (Entity_Node);

         if Res_Ekind = E_Anonymous_Access_Type then
            --  There is no type declaration node in this case at all,
            --  so we just return this N_Defining_Identifier node for
            --  further analysis in the calling context:
            return Entity_Node;

            --  ??? Why do not we return Empty in this case???

         elsif Res_Ekind = E_Anonymous_Access_Subprogram_Type then
            --  No explicit type declaration, so
            return Empty;

         elsif Res_Ekind = E_String_Literal_Subtype
            or else
               (Res_Ekind = E_Array_Subtype
               and then
                Present (Parent (Entity_Node)))
         then
            --  The first part of the condition corresponds to a special case
            --  E_String_Literal_Subtype is created for, see Einfo (spec) for
            --  the details. The second part corresponds to the access to
            --  string type, see E626-002

            Result_Node := Parent (Etype (Entity_Node));

            if No (Result_Node) then
               Result_Node := Associated_Node_For_Itype (Etype (Entity_Node));
            end if;

         elsif Ekind (Entity_Node) = E_Enumeration_Type then

            if Present (Associated_Node_For_Itype (Entity_Node)) then
               Result_Node := Associated_Node_For_Itype (Entity_Node);
            else
               --  Entity_Node represents an implicit type created for
               --  a derived enumeration type. we have to go down to this
               --  derived type
               Result_Node := Parent (Entity_Node);

               while Present (Result_Node) loop
                  Result_Node := Next (Result_Node);

                  exit when Nkind (Result_Node) = N_Subtype_Declaration
                           and then
                            Is_Rewrite_Substitution (Result_Node);
               end loop;

            end if;

            pragma Assert (Present (Result_Node));

         elsif  (No (Parent (Entity_Node)) or else
                 not Comes_From_Source (Parent (Entity_Node)))
               and then
                 Etype (Entity_Node) /= Entity_Node
               and then
                 not (Ekind (Entity_Node) = E_Floating_Point_Type or else
                      Ekind (Entity_Node) = E_Signed_Integer_Type or else
                      Ekind (Entity_Node) = E_Array_Type          or else
                      Ekind (Entity_Node) = E_Private_Type        or else
                      Ekind (Entity_Node) = E_Limited_Private_Type)
         then

            if Is_Itype (Entity_Node)
              and then
                Nkind (Associated_Node_For_Itype (Entity_Node)) =
                       N_Subtype_Declaration
            then
               Next_Node :=
                 Defining_Identifier (Associated_Node_For_Itype (Entity_Node));

               if Next_Node = Entity_Node then
                  Next_Node := Etype (Entity_Node);
               end if;

            else
               --  subtypes created for objects when an explicit constraint
               --  presents in the object declaration ???

               Next_Node := Etype (Entity_Node);
            end if;

            Result_Node := Explicit_Type_Declaration (Next_Node);

         else

            Next_Node := Associated_Node_For_Itype (Entity_Node);

            pragma Assert (Present (Next_Node));

            if Nkind (Original_Node (Next_Node)) = N_Full_Type_Declaration
               or else
               Nkind (Original_Node (Next_Node)) = N_Formal_Type_Declaration
            then
               Result_Node := Next_Node;

            elsif Nkind (Next_Node) = N_Loop_Parameter_Specification then
               --  here we have to traverse the loop parameter specification,
               --  because otherwise we may get the base type instead of
               --  the actually needed named subtype.
               Result_Node := Next_Node;
               Result_Node := Sinfo.Discrete_Subtype_Definition (Result_Node);

               case Nkind (Result_Node) is

                  when N_Subtype_Indication =>
                     Result_Node := Sinfo.Subtype_Mark (Result_Node);
                     Result_Node := Parent (Entity (Result_Node));

                  when N_Identifier | N_Expanded_Name =>
                     Result_Node := Parent (Entity (Result_Node));

                  when N_Range =>
                     --  and here we have to use the Etype field of
                     --  the implicit type itself, because we do not have
                     --  any type mark to start from in the loop parameter
                     --  specification:
                     Result_Node := Explicit_Type_Declaration
                                      (Etype (Entity_Node));
                  when others =>
                     null;
                     pragma Assert (False);
                     --  this is definitely wrong! Should be corrected
                     --  during debugging!!!
               end case;

            else

               if Etype (Entity_Node) /= Entity_Node then
                  --  otherwise we will be in dead circle
                  Result_Node := Etype (Entity_Node);
                  Result_Node := Explicit_Type_Declaration (Result_Node);

               else
                  --  for now, the only guess is that we have an object
                  --  defined by an object declaration with constrained
                  --  array definition, or an initialization expression
                  --  from such a declaration
                  pragma Assert (
                     Nkind (Next_Node) = N_Object_Declaration and then
                     Nkind (Object_Definition (Next_Node)) =
                        N_Constrained_Array_Definition);
                  return Empty;
                  --  what else could we return here?
               end if;

            end if;

         end if;

      end if;

      return Result_Node;

   end Explicit_Type_Declaration;

   ---------------------------------------
   -- Explicit_Type_Declaration_Unwound --
   ---------------------------------------

   function Explicit_Type_Declaration_Unwound
     (Entity_Node    : Node_Id;
      Reference_Node : Node_Id := Empty)
      return           Node_Id
   is
      Result_Node       : Node_Id;
      Subtype_Mark_Node : Node_Id;
   begin

      Result_Node := Explicit_Type_Declaration (Entity_Node);

      while Nkind (Original_Node (Result_Node)) = N_Subtype_Declaration loop
         Subtype_Mark_Node :=
            Sinfo.Subtype_Indication (Original_Node (Result_Node));

         if Nkind (Subtype_Mark_Node) = N_Subtype_Indication then
            Subtype_Mark_Node := Sinfo.Subtype_Mark (Subtype_Mark_Node);
         end if;

         Result_Node := Explicit_Type_Declaration (Entity (Subtype_Mark_Node));
      end loop;

      if Present (Reference_Node) and then
         (Nkind (Original_Node (Result_Node)) = N_Private_Type_Declaration
         or else
          Nkind (Original_Node (Result_Node)) =
             N_Private_Extension_Declaration)
        and then
          Full_View_Visible (Result_Node, Reference_Node)
      then
         Result_Node := Parent (Full_View (Defining_Identifier (Result_Node)));
      end if;

      return Result_Node;

   end Explicit_Type_Declaration_Unwound;

   ------------------------------------------------
   -- Explicit_Type_Declaration_Unwound_Unaccess --
   ------------------------------------------------

   function Explicit_Type_Declaration_Unwound_Unaccess
     (Entity_Node    : Node_Id;
      Reference_Node : Node_Id := Empty)
      return           Node_Id
   is
      Result_Node       : Node_Id;
      Subtype_Mark_Node : Node_Id;
      Tmp               : Node_Id;
   begin

      Result_Node := Explicit_Type_Declaration_Unwound (
                        Entity_Node, Reference_Node);

      if Nkind (Result_Node) = N_Defining_Identifier and then
         Ekind (Result_Node) = E_Anonymous_Access_Type
      then

         Result_Node := Explicit_Type_Declaration_Unwound (
            Directly_Designated_Type (Result_Node), Reference_Node);
      end if;

      --  This loop unwinds accessing^

      while (Nkind (Original_Node (Result_Node)) = N_Full_Type_Declaration
             and then
             Nkind (Sinfo.Type_Definition (Original_Node (Result_Node))) =
             N_Access_To_Object_Definition)
          or else
            (Nkind (Original_Node (Result_Node)) = N_Formal_Type_Declaration
             and then
             Nkind (Sinfo.Formal_Type_Definition (Original_Node (
                Result_Node))) = N_Access_To_Object_Definition)
      loop
         Subtype_Mark_Node := Original_Node (Result_Node);

         if Nkind (Subtype_Mark_Node) = N_Full_Type_Declaration then
            Subtype_Mark_Node := Sinfo.Subtype_Indication (
               Sinfo.Type_Definition (Subtype_Mark_Node));
         else
            Subtype_Mark_Node := Sinfo.Subtype_Indication (
               Sinfo.Formal_Type_Definition (Subtype_Mark_Node));
         end if;

         if Nkind (Subtype_Mark_Node) = N_Subtype_Indication then
            Subtype_Mark_Node := Sinfo.Subtype_Mark (Subtype_Mark_Node);
         end if;

         Result_Node := Explicit_Type_Declaration_Unwound (
            Entity (Subtype_Mark_Node), Reference_Node);

         if Nkind (Result_Node) = N_Incomplete_Type_Declaration then
            --  To be 100% honest, we have to check that at place of
            --  Reference_Node the full view is visible. But we could hardly
            --  call this routine (for a legal code) if we do not see the full
            --  view from Reference_Node.

            Tmp := Full_View (Defining_Identifier (Result_Node));

            if Present (Tmp) then
               Result_Node :=  Parent (Tmp);
            end if;

         end if;

      end loop;

      --  If we have a type derived from an access type, we have to go through
      --  this derivation and unwind accessing

      if Nkind (Result_Node) = N_Full_Type_Declaration
        and then
         Nkind (Sinfo.Type_Definition (Result_Node)) =
         N_Derived_Type_Definition
      then
         Tmp := Defining_Identifier (Result_Node);

         if Ekind (Tmp) in Access_Kind then
            Result_Node :=
              Explicit_Type_Declaration_Unwound_Unaccess
                (Directly_Designated_Type (Tmp),
                 Reference_Node);
         end if;

      end if;

      return Result_Node;

   end Explicit_Type_Declaration_Unwound_Unaccess;

   ---------------
   -- Expr_Type --
   ---------------

   function Expr_Type (Expression : Asis.Expression) return Asis.Declaration is
      Arg_Node      : Node_Id;
      Arg_Kind      : constant Internal_Element_Kinds := Int_Kind (Expression);
      Result_Entity : Node_Id;
      Result_Node   : Node_Id;
      Result_Unit   : Compilation_Unit;
      Res_Spec_Case : Special_Cases       := Not_A_Special_Case;
      Encl_Cont     : constant Context_Id := Encl_Cont_Id (Expression);
   begin
      --  first, we should check whether Expression has a universal
      --  numeric type and return the corresponding ASIS universal type.
      --  For now, this check includes numeric literals and some of the
      --  attribute references is:
      if Arg_Kind = An_Integer_Literal                       or else
         Arg_Kind = An_Alignment_Attribute                   or else
         Arg_Kind = A_Component_Size_Attribute               or else
         Arg_Kind = A_Digits_Attribute                       or else
         Arg_Kind = A_Count_Attribute                        or else
         Arg_Kind = An_Exponent_Attribute                    or else
         Arg_Kind = A_First_Bit_Attribute                    or else
         Arg_Kind = A_Fore_Attribute                         or else
         Arg_Kind = A_Last_Bit_Attribute                     or else
         Arg_Kind = A_Length_Attribute                       or else
         Arg_Kind = A_Machine_Emax_Attribute                 or else
         Arg_Kind = A_Machine_Emin_Attribute                 or else
         Arg_Kind = A_Machine_Mantissa_Attribute             or else
         Arg_Kind = A_Machine_Radix_Attribute                or else
         Arg_Kind = A_Max_Size_In_Storage_Elements_Attribute or else
         Arg_Kind = A_Model_Emin_Attribute                   or else
         Arg_Kind = A_Model_Mantissa_Attribute               or else
         Arg_Kind = A_Modulus_Attribute                      or else
         Arg_Kind = A_Partition_ID_Attribute                 or else
         Arg_Kind = A_Pos_Attribute                          or else
         Arg_Kind = A_Position_Attribute                     or else
         Arg_Kind = A_Scale_Attribute                        or else
         Arg_Kind = A_Size_Attribute                         or else
         Arg_Kind = A_Storage_Size_Attribute                 or else
         Arg_Kind = A_Wide_Width_Attribute                   or else
         Arg_Kind = A_Width_Attribute                        or else
        (Special_Case (Expression) = Rewritten_Named_Number
         and then Nkind (R_Node (Expression)) = N_Integer_Literal)
      then
         return Set_Root_Type_Declaration
                  (A_Universal_Integer_Definition,
                   Encl_Cont);
      elsif Arg_Kind = A_Real_Literal            or else
            Arg_Kind = A_Delta_Attribute         or else
            Arg_Kind = A_Model_Epsilon_Attribute or else
            Arg_Kind = A_Model_Small_Attribute   or else
            Arg_Kind = A_Safe_First_Attribute    or else
            Arg_Kind = A_Safe_Last_Attribute     or else
            Arg_Kind = A_Small_Attribute         or else
           (Special_Case (Expression) = Rewritten_Named_Number
            and then Nkind (R_Node (Expression)) = N_Real_Literal)
      then
         return Set_Root_Type_Declaration
                  (A_Universal_Real_Definition,
                   Encl_Cont);
      end if;

      Arg_Node := Node (Expression);

      --  In some cases we have to use the rewritten node
      if Is_Rewrite_Substitution (R_Node (Expression)) and then
         (Nkind (Arg_Node) = N_Aggregate and then
          Nkind (R_Node (Expression)) = N_String_Literal)
      then
         Arg_Node := R_Node (Expression);
      end if;

      while Nkind (Arg_Node) = N_String_Literal
        and then
         Nkind (Parent (Arg_Node)) = N_String_Literal
      loop
         --  Trick for F109-A24: for string literals in a static expression,
         --  Etype points to some dummy subtype node (the tree structure is
         --  rewritten for the whole expression, and the original subtree is
         --  not fully decorated), so we take the type information from the
         --  rewritten result of the expression
         Arg_Node := Parent  (Arg_Node);
      end loop;

      --  if the expression node is rewritten, all the semantic
      --  information can be found only through the rewritten node

      if Nkind (Parent (Arg_Node)) = N_Expanded_Name and then
         Arg_Node = Selector_Name (Parent (Arg_Node))
      then
         --  selector in an expanded name - all the semantic fields
         --  are set for the whole name, but not for this selector.
         --  So:
         Arg_Node := Parent (Arg_Node);
      end if;

--  ??? <tree problem 1>
--  this fragment should be revised when the problem is fixed (as it should)
      if Nkind (Arg_Node) = N_Selected_Component then
         if Etype (Arg_Node) = Any_Type then
            --  for now (GNAT 3.05) this means, that Expression is an expanded
            --  name of the character literal of ether a predefined character
            --  type or of the type derived from a predefined character type
            Arg_Node := R_Node (Expression);
            --  resetting Arg_Node pointing to the rewritten node for the
            --  expanded name
            --
            --  ???
            --  This looks strange... Should be revised
         else
            Arg_Node := Selector_Name (Arg_Node);
            --  here the actual type is!
         end if;
      elsif Nkind (Arg_Node) = N_Character_Literal and then
            No (Etype (Arg_Node))
            --  for now (GNAT 3.05) this means, that Expression is the
            --  selector in an expanded name of the character literal of
            --  ether a predefined character type or of the type derived
            --  from a predefined character type
      then
         Arg_Node := Parent (Arg_Node);
         --  resetting Arg_Node pointing to the rewritten node for the whole
         --  expanded name
      end if;
--  ??? <tree problem 1> - end

      --  now the idea is to take the Etype attribute of the expression
      --  and to go to the corresponding type declaration. But
      --  special processing for computing the right Etype is
      --  required for some cases
      if Nkind (Parent (Arg_Node)) = N_Qualified_Expression and then
         Arg_Node = Sinfo.Expression (Parent (Arg_Node))
      then
         Result_Entity := Etype (Sinfo.Subtype_Mark (Parent (Arg_Node)));

         --  we'll keep the commented code below for a while...
--      elsif (Arg_Kind = A_First_Attribute or else
--             Arg_Kind = A_Last_Attribute)
--          and then not Comes_From_Source (Etype (Arg_Node))
--          and then Sloc (Etype (Arg_Node)) > Standard_Location
--          and then Etype (Etype (Arg_Node)) = Etype (Arg_Node)
--      then
--         --  this tricky condition corresponds to the situation, when
--         --  'First or 'Last attribute is applied to a formal discrete
--         --  type @:-(
--         --  In this case we simply use the attribute prefix to define
--         --  the result type
--         Result_Entity := Etype (Prefix (Arg_Node));
      else
         --  how nice it would be if *everything* would be so simple
         Result_Entity := Etype (Arg_Node);
      end if;

      if Result_Entity = Any_Composite then
         --  Here we have an aggregate in some original tree structure that has
         --  not been properly decorated. All the semantic decorations are in
         --  the corresponding rewritten structure, so we have to find the
         --  corresponding node there.

         declare
            Tmp          : Node_Id;
            New_Arg_Node : Node_Id := Empty;

            Arg_Kind : constant Node_Kind  := Nkind (Arg_Node);
            Arg_Sloc : constant Source_Ptr := Sloc (Arg_Node);

            function Find (Node : Node_Id) return Traverse_Result;
            --  Check if its argument represents the same construct as
            --  Arg_Node, and if it does, stores Node in New_Arg_Node and
            --  returns Abandon, otherwise returns OK.

            procedure Find_Rewr_Aggr is new  Traverse_Proc (Find);

            function Find (Node : Node_Id)  return Traverse_Result is
            begin

               if Nkind (Node) = Arg_Kind
                 and then
                  Sloc (Node) =  Arg_Sloc
               then
                  New_Arg_Node := Node;
                  return Abandon;
               else
                  return OK;
               end if;

            end Find;

         begin
            Tmp := Parent (Arg_Node);

            while not Is_Rewrite_Substitution (Tmp) loop
               Tmp := Parent (Tmp);
            end loop;

            Find_Rewr_Aggr (Tmp);

            pragma Assert (Present (New_Arg_Node));

            Result_Entity := Etype (New_Arg_Node);
         end;

      end if;

      Result_Node := Explicit_Type_Declaration (Result_Entity);

      if No (Result_Node) then
         return Nil_Element;
         --  we cannot represent the type declaration in ASIS;
         --  for example, an object defined by an object declaration
         --  with constrained array definition
      end if;

      if Sloc (Result_Entity) <= Standard_Location then
         Result_Unit :=  Get_Comp_Unit
                           (Standard_Id, Encl_Cont_Id (Expression));
         Res_Spec_Case := Explicit_From_Standard;
      else
         Result_Unit := Enclosing_Unit
                          (Encl_Cont_Id (Expression), Result_Node);
      end if;

      return Node_To_Element_New (Node      => Result_Node,
                                  Spec_Case => Res_Spec_Case,
                                  In_Unit   => Result_Unit);
   end Expr_Type;

   -----------------------
   -- Full_View_Visible --
   -----------------------

   function Full_View_Visible
     (Priv_Type : Node_Id;
      Ref       : Node_Id)
      return      Boolean
   is
      Type_Scope      : constant Node_Id :=
        Scope (Defining_Identifier (Priv_Type));
      Type_Scope_Body : Node_Id;
      Type_Full_View  : Node_Id;
      Scope_Node      : Node_Id := Empty;
      Next_Node       : Node_Id := Parent (Ref);
      Next_Node_Inner : Node_Id := Ref;

      Result          : Boolean := False;

   begin
      Type_Scope_Body := Parent (Type_Scope);

      if Nkind (Type_Scope_Body) = N_Defining_Program_Unit_Name then
         Type_Scope_Body := Parent (Type_Scope_Body);
      end if;

      Type_Scope_Body := Corresponding_Body (Parent (Type_Scope_Body));

      if Nkind (Parent (Type_Scope_Body)) = N_Defining_Program_Unit_Name then
         Type_Scope_Body := Parent (Type_Scope_Body);
      end if;

      while Present (Next_Node) loop

         if  (Nkind (Next_Node) = N_Package_Specification and then
              Defining_Unit_Name (Next_Node) = Type_Scope)
            or else
             (Nkind (Next_Node) = N_Package_Body and then
             Defining_Unit_Name (Next_Node) = Type_Scope_Body)
         then
            Scope_Node := Next_Node;
            exit;
         end if;

         Next_Node_Inner := Next_Node;
         Next_Node := Parent (Next_Node);

      end loop;

      if Present (Scope_Node) then

         if Nkind (Scope_Node) = N_Package_Body then
            Result := True;

         elsif List_Containing (Next_Node_Inner) =
               Private_Declarations (Scope_Node)
         then
            --  That is, Ref is in the private part of the package where
            --  Priv_Type is declared, and we have to check what goes first:
            --  Ref (or a construct it is enclosed into - it is pointed by
            --  Next_Node_Inner) or the full view of the private type:

            Type_Full_View := Parent (Full_View
              (Defining_Identifier (Priv_Type)));

            Next_Node := First_Non_Pragma (Private_Declarations (Scope_Node));

            while Present (Next_Node) loop

               if Next_Node = Type_Full_View then
                  Result := True;
                  exit;
               elsif Next_Node = Next_Node_Inner then
                  exit;
               else
                  Next_Node := Next_Non_Pragma (Next_Node);
               end if;

            end loop;

         end if;

      end if;

      return Result;

   end Full_View_Visible;

   --------------------------------
   -- Get_Discriminant_From_Type --
   --------------------------------

   function Get_Discriminant_From_Type (N : Node_Id) return Entity_Id is
      Type_Entity : Entity_Id        := Parent (N);
      Res_Chars   : constant Name_Id := Chars (N);
      Result      : Entity_Id;
   begin
      while not (Nkind (Type_Entity) = N_Subtype_Declaration
               or else
                 Nkind (Type_Entity) = N_Subtype_Indication)
      loop
         Type_Entity := Parent (Type_Entity);

         if Nkind (Type_Entity) = N_Allocator then
            Type_Entity := Etype (Type_Entity);

            while Ekind (Type_Entity) in Access_Kind loop
               Type_Entity := Directly_Designated_Type (Type_Entity);
            end loop;

            exit;
         end if;
      end loop;

      if Nkind (Type_Entity) = N_Subtype_Indication and then
         Nkind (Parent (Type_Entity)) = N_Subtype_Declaration
      then
         Type_Entity := Parent (Type_Entity);
      end if;

      if Nkind (Type_Entity) = N_Subtype_Declaration then
         Type_Entity := Defining_Identifier (Type_Entity);
      else
         Type_Entity := Entity (Sinfo.Subtype_Mark (Type_Entity));
      end if;

      while
        Type_Entity /= Etype (Type_Entity)
      loop
         exit when Comes_From_Source (Type_Entity)
                 and then
                    Comes_From_Source (Original_Node (Parent (Type_Entity)))
                 and then
                    Nkind (Parent (Type_Entity)) /= N_Subtype_Declaration;
         Type_Entity := Etype (Type_Entity);

         if Ekind (Type_Entity) = E_Access_Type then
            Type_Entity := Directly_Designated_Type (Type_Entity);
         elsif (Ekind (Type_Entity) = E_Private_Type
              or else
                Ekind (Type_Entity) = E_Limited_Private_Type)
             and then
               Present (Full_View (Type_Entity))
         then
            Type_Entity := Full_View (Type_Entity);
         end if;
      end loop;

      --  Take care of a private type with unknown discriminant part:

      if Nkind (Parent (Type_Entity)) in
         N_Private_Extension_Declaration .. N_Private_Type_Declaration
       and then
         Unknown_Discriminants_Present (Parent (Type_Entity))
      then
         Type_Entity := Full_View (Type_Entity);
      end if;

      --  In case of a derived types, we may have discriminants declared for an
      --  ansector type and then redefined for some child type

      Search_Discriminant : loop

         Result := Original_Node (Parent (Type_Entity));
         Result := First (Discriminant_Specifications (Result));

         while Present (Result) loop
            if Chars (Defining_Identifier (Result)) = Res_Chars then
               Result := Defining_Identifier (Result);
               exit Search_Discriminant;
            else
               Result := Next (Result);
            end if;
         end loop;

         exit Search_Discriminant when Type_Entity = Etype (Type_Entity);

         Type_Entity := Etype (Type_Entity);

      end loop Search_Discriminant;

      pragma Assert (Present (Result));

      return Result;
   end Get_Discriminant_From_Type;

   -------------------------------
   -- Get_Entity_From_Long_Name --
   -------------------------------

   function Get_Entity_From_Long_Name (N : Node_Id) return Entity_Id is
      Result      : Entity_Id        := Empty;
      Arg_Chars   : constant Name_Id := Chars (N);
      Res_Chars   : Name_Id;

      P           : Node_Id;
      Next_Entity : Entity_Id;
   begin

      P := Parent (N);

      while No (Entity (P)) loop
         P := Parent (P);
      end loop;

      Next_Entity := Entity (P);
      Res_Chars   := Chars (Next_Entity);

      loop

         if Res_Chars = Arg_Chars then
            Result := Next_Entity;
            exit;
         end if;

         if Nkind (Parent (Next_Entity)) = N_Defining_Program_Unit_Name then
            P := Sinfo.Name (Parent (Next_Entity));
            Next_Entity := Entity (P);
            Res_Chars   := Chars (Next_Entity);
         else
            exit;
         end if;

      end loop;

      pragma Assert (Present (Result));

      return Result;
   end Get_Entity_From_Long_Name;

   -----------------------------
   -- Get_Rewritten_Discr_Ref --
   -----------------------------

   function Get_Rewritten_Discr_Ref (N : Node_Id) return Node_Id is
      Res_Chars : constant Name_Id := Chars (N);
      Result    : Node_Id          := Parent (N);
   begin

      while not (Nkind (Result) = N_Identifier
               and then
                 Is_Rewrite_Substitution (Result)
               and then
                 Nkind (Original_Node (Result)) = N_Subtype_Indication)
      loop
         Result := Parent (Result);
      end loop;

      --  Go to the declaration of this internal subtype
      Result := Parent (Entity (Result));

      --  Now - no the constraint
      Result := Sinfo.Constraint (Sinfo.Subtype_Indication (Result));

      --  And iterating through discriminant names
      Result := First (Constraints (Result));
      Result := First (Selector_Names (Result));

      while Present (Result) loop

         if Chars (Result) = Res_Chars then
            exit;
         end if;

         --  Get to the next discriminant

         if Present (Next (Result)) then
            Result := Next (Result);
         else
            Result := Next (Parent (Result));

            if Present (Result) then
               Result := First (Selector_Names (Result));
            end if;

         end if;

      end loop;

      pragma Assert (Present (Result));

      return Result;
   end Get_Rewritten_Discr_Ref;

   ------------------------------
   -- Get_Specificed_Component --
   ------------------------------

   function Get_Specificed_Component
     (Comp     : Node_Id;
      Rec_Type : Entity_Id)
      return     Entity_Id
   is
      Rec_Type_Entity : Entity_Id;
      Result          : Entity_Id        := Empty;
      Res_Chars       : constant Name_Id := Chars (Comp);
      Next_Comp       : Node_Id;

   begin

      if Ekind (Rec_Type) = E_Private_Type or else
         Ekind (Rec_Type) = E_Limited_Private_Type
      then
         Rec_Type_Entity := Full_View (Rec_Type);
      else
         Rec_Type_Entity := Rec_Type;
      end if;

      Next_Comp := First_Entity (Rec_Type_Entity);

      while Present (Next_Comp) loop

         if Chars (Next_Comp) = Res_Chars then
            Result := Next_Comp;
            exit;
         end if;

         Next_Comp := Next_Entity (Next_Comp);

      end loop;

      pragma Assert (Present (Result));

      return Result;
   end Get_Specificed_Component;

   ------------------------------
   -- Get_Statement_Identifier --
   ------------------------------

   function Get_Statement_Identifier (Def_Id : Node_Id) return Node_Id is
      Result_Node  : Node_Id := Empty;
      --  List_Elem    : Node_Id;
   begin
      Result_Node := Label_Construct (Parent (Def_Id));

      if not (Nkind (Result_Node) = N_Label) then
         --  this means, that Result_Node is of N_Block_Statement or
         --  of N_Loop_Statement kind, therefore
         Result_Node := Sinfo.Identifier (Result_Node);
      end if;

      return Result_Node;

   end Get_Statement_Identifier;

   ---------------------
   -- GFP_Declaration --
   ---------------------

   function GFP_Declaration (Par_Id : Node_Id) return Node_Id is
      Par_Chars    : constant Name_Id := Chars (Par_Id);
      Result_Node  : Node_Id;
      Gen_Par_Decl : Node_Id;
   begin
      --  First, going up to the generic instantiation itself:
      Result_Node := Parent (Parent (Par_Id));
      --  then taking the name of the generic unit being instantiated
      --  and going to its definition - and declaration:
      Result_Node := Parent (Parent (Entity (Sinfo.Name (Result_Node))));
      --  and now - searching the declaration of the corresponding
      --  generic parameter:
      Gen_Par_Decl :=
         First_Non_Pragma (Generic_Formal_Declarations (Result_Node));

      while Present (Gen_Par_Decl) loop

         if Nkind (Gen_Par_Decl) in N_Formal_Subprogram_Declaration then
            Result_Node := Defining_Unit_Name (Specification (Gen_Par_Decl));
         else
            Result_Node := Defining_Identifier (Gen_Par_Decl);
         end if;

         if Chars (Result_Node) = Par_Chars then
            exit;
         else
            Gen_Par_Decl := Next_Non_Pragma (Gen_Par_Decl);
         end if;

      end loop;

      return Result_Node;

   end GFP_Declaration;

   --------------------------------
   -- Identifier_Name_Definition --
   --------------------------------

   function Identifier_Name_Definition
     (Reference_I : Element)
      return        Asis.Defining_Name
   is
      Arg_Node      : Node_Id;
      Arg_Node_Kind : Node_Kind;
      Arg_Kind : constant Internal_Element_Kinds := Int_Kind (Reference_I);

      Result_Node      : Node_Id                := Empty;
      Result_Unit      : Compilation_Unit;
      Spec_Case        : Special_Cases          := Not_A_Special_Case;
      Result_Kind      : Internal_Element_Kinds := Not_An_Element;
      Is_Inherited     : Boolean                := False;
      Association_Type : Node_Id                := Empty;
      --  ??? Is it a good name for a parameter?
      Componnet_Name   : Node_Id                := Empty;

      Tmp_Node         : Node_Id;

      Result : Asis.Element;

      function Ekind (N : Node_Id) return Entity_Kind;
      --  This function differs from Atree.Ekind in that it can operate
      --  with N_Defining_Program_Unit_Name (in this case it returns
      --  Atree.Ekind for the corresponding Defining_Identifier node.

      function Ekind (N : Node_Id) return Entity_Kind is
         Arg_Node : Node_Id := N;
      begin

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

         return Atree.Ekind (Arg_Node);

      end Ekind;

   begin
      --  this function is currently integrated with
      --  Enumeration_Literal_Name_Definition and
      --  Operator_Symbol_Name_Definition

      --  The implementation approach is very similar to that one of
      --  A4G.A_Sem.Get_Corr_Called_Entity. Now the implicit *predefined*
      --  operations are turned off for a while

      ------------------------------------------------------------------
      --  1. Defining Result_Node (and adjusting Arg_Node, if needed) --
      ------------------------------------------------------------------

      if Arg_Kind = An_Identifier then
         Result_Kind := A_Defining_Identifier;
         --  may be changed to A_Defining_Expanded_Name later
      elsif Arg_Kind = An_Enumeration_Literal then
         Result_Kind := A_Defining_Enumeration_Literal;
      elsif Arg_Kind in Internal_Operator_Symbol_Kinds then
         Result_Kind := Def_Operator_Kind (Int_Kind (Reference_I));
      end if;

      if Special_Case (Reference_I) = Rewritten_Named_Number then
         Arg_Node := R_Node (Reference_I);
      else
--         Arg_Node := Get_Actual_Type_Name (Node (Reference_I));
         Arg_Node := Node (Reference_I);
      end if;

      --  the code below is really awful! In some future we'll have
      --  to revise this "patch on patch" approach!!!

      if Is_Part_Of_Defining_Unit_Name (Arg_Node) and then
         Kind (Encl_Unit (Reference_I)) in A_Library_Unit_Body
      then
         --  this means, that we have a part of a prefix of a defining
         --  unit name which is a part of a body. These components do not
         --  have Entity field set, so we have to go to the spec:

         Arg_Node := Reset_To_Spec (Arg_Node);

      end if;

      if Nkind (Arg_Node) in N_Entity then
         --  This is the case of the reference to a formal type inside
         --  the expanded code when the actual type is a derived type
         --  In this case Get_Actual_Type_Name returns the entity node
         --  (see 8924-006)

         Result_Node := Arg_Node;
         Arg_Node    := Node (Reference_I);
         --  For the rest of the processing we need Arg_Node properly set as
         --  the reference, but not as an entity node

      elsif Special_Case (Reference_I) = Rewritten_Named_Number then
         --  See BB10-002
         Result_Node := Original_Entity (Arg_Node);

      elsif No (Entity (Arg_Node)) then

         Arg_Node_Kind := Nkind (Original_Node (Parent (Arg_Node)));

         --  in some cases we can try to "repair" the situation:

         if Arg_Node_Kind = N_Expanded_Name then

            --  the Entity field is set for the whole expanded name:
            if Entity_Present (Original_Node (Parent (Arg_Node))) or else
               Entity_Present (Parent (Arg_Node))
            then
               Arg_Node := Parent (Arg_Node);

               --  In case of renamings, here we may have the expanded name
               --  rewritten, and the Entity field for the new name pointing
               --  to the renamed entity, but not to the entity defined by
               --  the renamed declaration, see B924-A13

               if Is_Rewrite_Substitution (Arg_Node) and then
                  Entity_Present (Original_Node (Arg_Node))
               then
                  Arg_Node := Original_Node (Arg_Node);
               end if;

            else
               --  Trying to "traverse a "long" defining program unit
               --  name (see 7917-005)
               Result_Node := Get_Entity_From_Long_Name (Arg_Node);
            end if;

         elsif Arg_Node_Kind = N_Component_Definition and then
               Sloc (Arg_Node) = Standard_Location
         then
            --  Special case of Subtype_Indication for predefined String
            --  and Wide_String types:

            Result_Node := Parent (Parent (Parent (Arg_Node)));
            --  Here we are in N_Full_Type_Declaration node
            Result_Node := Defining_Identifier (Result_Node);
            Result_Node := Component_Type (Result_Node);

            Spec_Case := Explicit_From_Standard;

         elsif Arg_Node_Kind = N_Function_Call then
            --  this is a special case of a parameterless function call
            --  of the form P.F
            Arg_Node := Sinfo.Name (Original_Node (Parent (Arg_Node)));

         elsif Arg_Node_Kind = N_Integer_Literal   or else
               Arg_Node_Kind = N_Real_Literal      or else
               Arg_Node_Kind = N_Character_Literal or else
               Arg_Node_Kind = N_String_Literal    or else
               Arg_Node_Kind = N_Identifier
         then
            --  All but last conditions are a result of some compile-time
            --  optimization. The last one is a kind of
            --  semantically-transparent transformation which loses some
            --  semantic information for replaced structures (see the test
            --  for 9429-006).
            --
            --  The last condition may need some more attention in case if new
            --  Corresponding_Name_Definition problems are detected

            Arg_Node := Original_Node (Parent (Arg_Node));

         elsif Arg_Node_Kind = N_Component_Association and then
               Nkind (Parent (Parent (Arg_Node))) = N_Raise_Constraint_Error
         then
            --  A whole aggregate is rewritten into N_Raise_Constraint_Error
            --  node, see G628-026

            Tmp_Node := Parent (Parent (Arg_Node));
            Tmp_Node := Etype (Tmp_Node);
            Tmp_Node := First_Entity (Tmp_Node);

            while Present (Tmp_Node) loop

               if Chars (Tmp_Node) = Chars (Arg_Node) then
                  Result_Node := Tmp_Node;
                  exit;
               end if;

               Tmp_Node := Next_Entity (Tmp_Node);
            end loop;

            pragma Assert (Present (Result_Node));

            if not (Comes_From_Source (Result_Node))
              and then
               Comes_From_Source (Parent (Result_Node))
            then
               Result_Node := Defining_Identifier (Parent (Result_Node));
            end if;

         elsif Arg_Node_Kind = N_Component_Association and then
               Nkind (Sinfo.Expression (Parent (Arg_Node))) =
               N_Raise_Constraint_Error
         then
            --  here we are guessing for the situation when a compiler
            --  optimization take place. We can probably be non-accurate
            --  for inherited record components, but what can we do....
            --
            --  first, defining the corresponding Entity Node, we assume
            --  it to be a record component definition

            Result_Node      := Parent (Parent (Arg_Node)); --  aggregate
            Association_Type := Etype (Result_Node);

            if Ekind (Association_Type) in Private_Kind then
               Association_Type := Full_View (Association_Type);
            end if;

            Result_Node := First_Entity (Association_Type);

            while Chars (Result_Node) /= Chars (Arg_Node) loop
               Result_Node := Next_Entity (Result_Node);
            end loop;

         elsif Arg_Node_Kind = N_Parameter_Association and then
               Arg_Node = Selector_Name (Parent (Arg_Node))
         then

            --  Currently we assume, that this corresponds to the case of
            --  formal parameters of predefined operations
            return Nil_Element;

         elsif Arg_Node_Kind = N_Component_Clause then
            --  Component clause in record representation clause - Entity
            --  field is not set, we have to traverse the list of components
            --  of the record type

            Association_Type :=
               Entity (Sinfo.Identifier (Parent (Parent (Arg_Node))));

            if Ekind (Association_Type) = E_Record_Subtype then
               --  In case of a subtype it may be the case that some components
               --  depending on discriminant are skipped in case of a static
               --  discriminnat constraint, see also
               --  A4G.Mapping.Set_Inherited_Components
               Association_Type := Etype (Association_Type);
            end if;

            Result_Node :=
               Get_Specificed_Component (Arg_Node, Association_Type);

            Association_Type := Empty;
            --  Association_Type is set back to Empty to make it possible
            --  to use the general approach for computing Association_Type
            --  later

         elsif Nkind (Arg_Node) = N_Identifier and then
               Sloc (Parent (Arg_Node)) = Standard_ASCII_Location
         then
            --  reference to Character in a constant definition in the
            --  ASCII package, see 8303-011

            Result_Node := Standard_Character;

         elsif not (Arg_Node_Kind = N_Discriminant_Association or else
                    Arg_Node_Kind = N_Generic_Association)
             and then
               not Is_From_Unknown_Pragma (R_Node (Reference_I))
         then
            --  now we are considering all the other cases as component simple
            --  names in a (rewritten!) record aggregate, and we go from the
            --  original to the rewritten structure (because the original
            --  structure is not decorated). If this is not the case, we should
            --  get the Assert_Failure raised in Rewritten_Image
            --  Arg_Node := Rewritten_Image (Arg_Node);

            Result_Node := Search_Record_Comp (Arg_Node);
         end if;

      end if;

      if No (Result_Node) and then
         No (Entity (Arg_Node)) and then
         not (Nkind (Parent (Arg_Node)) = N_Discriminant_Association or else
              Nkind (Parent (Arg_Node)) = N_Generic_Association or else
              Is_From_Unknown_Pragma (R_Node (Reference_I)))
      then

         if Debug_Flag_S then
            Write_Str ("A4G.Expr_Sem.Identifier_Name_Definition:");
            Write_Eol;
            Write_Str ("no Entity field is set for Node ");
            Write_Int (Int (Arg_Node));
            Write_Eol;
            Write_Str ("   the debug image of the query argument is:");
            Write_Eol;
            Debug_String (Reference_I);
            Write_Str (Debug_Buffer (1 .. Debug_Buffer_Len));
            Write_Eol;
         end if;

         raise Internal_Implementation_Error;
      end if;

      if Present (Result_Node) then
         null;

      elsif Is_From_Unknown_Pragma (R_Node (Reference_I)) then
         return Nil_Element;
      elsif Nkind (Parent (Arg_Node)) = N_Discriminant_Association and then
         Arg_Node /= Original_Node (Sinfo.Expression (Parent (Arg_Node)))
      then
         --  We use Original_Node (Sinfo.Expression (Parent (Arg_Node)))
         --  because of C730-016 (named numbers rewritten into their values)

         if No (Original_Discriminant (Arg_Node)) then
            Result_Node := Get_Discriminant_From_Type (Arg_Node);
         else
            Result_Node := Original_Discriminant (Arg_Node);

            if Present (Corresponding_Discriminant (Result_Node)) then
               Result_Node := Corresponding_Discriminant (Result_Node);
            end if;
         end if;

      elsif No (Entity (Arg_Node)) and then
            Nkind (Parent (Arg_Node)) = N_Generic_Association
      then
         --  this is the problem up to 3.10p. We have to compute
         --  N_Defining_Identifier_Node for this generic formal
         --  parameter "by hands"
         --  ??? should be rechecked for 3.11w!!!
         Result_Node := GFP_Declaration (Arg_Node);

      else
         Result_Node := Entity (Arg_Node);
      end if;

      --  Here we have Result_Node set. And now we have a whole bunch of
      --  situations when we have to correct Result_Node because of different
      --  reasons

      --  If Result_Node is the type reference, and the type has both private
      --  and full view, Result_Node will point to the private view. In some
      --  situations we have to replace it with the full view.

      if Ekind (Result_Node) in Einfo.Type_Kind
        and then
          Nkind (Original_Node (Parent (Result_Node))) in
           N_Private_Extension_Declaration .. N_Private_Type_Declaration
        and then
          Full_View_Visible
            (Priv_Type => Parent (Result_Node),
             Ref       => Arg_Node)
      then
         Result_Node := Full_View (Result_Node);
      end if;

      --  FB02-015: Ada 2005 - reference to a record type with self-referencing
      --            components, The front-end creates an incomplete type
      --            declaration, and the Entity field may point to this
      --            incomplete type.

      if Ekind (Result_Node) = E_Incomplete_Type
        and then
         not Comes_From_Source (Result_Node)
        and then
         Nkind (Parent (Result_Node)) = N_Incomplete_Type_Declaration
      then
         Tmp_Node := Full_View (Result_Node);

         if Present (Tmp_Node) then
            Result_Node := Full_View (Result_Node);
         end if;

      end if;

      --  F818-A05: reference to a formal parameter of a child subprogram in
      --            case when the subprogram does not have a separate spec.
      --            The front-end creates some artificial data structures to
      --            represent this separate spec, so the entity field of a
      --            parameter reference points to some artificial node

      if Nkind (Parent (Result_Node)) = N_Parameter_Specification
        and then
         not (Comes_From_Source (Result_Node))
      then

         --  Check if we are in the artificial spec created for child
         --  subprogram body:

         Tmp_Node := Scope (Result_Node);
         Tmp_Node := Parent (Parent (Parent (Tmp_Node)));

         if  Nkind (Tmp_Node) = N_Subprogram_Declaration
            and then
             not Comes_From_Source (Tmp_Node)
            and then
             Present (Parent_Spec (Tmp_Node))
            and then
             Present (Corresponding_Body (Tmp_Node))
         then

            --  Go to the defining identifier of this parameter in subprogram
            --  body:
            Tmp_Node := Corresponding_Body (Tmp_Node);
            Tmp_Node := Parent (Parent (Tmp_Node));
            Tmp_Node := First_Non_Pragma (Parameter_Specifications (Tmp_Node));

            while Present (Tmp_Node) loop
               if Chars (Defining_Identifier (Tmp_Node)) =
                    Chars (Result_Node)
               then
                  Result_Node := Defining_Identifier (Tmp_Node);
                  exit;
               end if;

               Tmp_Node := Next_Non_Pragma (Tmp_Node);
            end loop;

            pragma Assert (Present (Tmp_Node));

         end if;

      end if;

      --  E802-015: for a protected operation items that do not have separate
      --  specs the front-end creates these specs and sets all the Entity
      --  fields pointing to the entities from these artificial specs.

      if Is_Artificial_Protected_Op_Item_Spec (Result_Node) then

         if Ekind (Result_Node) in Formal_Kind then
            Tmp_Node := Parent (Parent (Parent (Result_Node)));
            Tmp_Node := Parent (Corresponding_Body (Tmp_Node));
            Tmp_Node := First_Non_Pragma (Parameter_Specifications (Tmp_Node));

            while Present (Tmp_Node) loop

               if Chars (Defining_Identifier (Tmp_Node)) =
                 Chars (Result_Node)
               then
                  Result_Node := Defining_Identifier (Tmp_Node);
                  exit;
               else
                  Tmp_Node := Next_Non_Pragma (Tmp_Node);
               end if;

            end loop;
         else
            --  The only possibility - the protected operation entity
            Result_Node := Corresponding_Body (Parent (Parent (Result_Node)));
         end if;

      end if;

      --  See E421-006: problem with reference to a formal type in an expanded
      --  code.
      if Present (Result_Node)
        and then
         Is_Itype (Result_Node)
--        and then Present (Cloned_Subtype (Result_Node))
      then

         if Special_Case (Reference_I) = Dummy_Base_Attribute_Prefix then
            Result_Node := Associated_Node_For_Itype (Result_Node);
         else
            Result_Node := Etype (Result_Node);
         end if;

         --  This is for E912-013
         if No (Parent (Result_Node))
          and then
            Present (Associated_Node_For_Itype (Result_Node))
         then
            Result_Node :=
              Defining_Identifier (Associated_Node_For_Itype (Result_Node));
         elsif Special_Case (Reference_I) = Dummy_Base_Attribute_Prefix then
            Result_Node := Defining_Identifier (Result_Node);
         end if;

      end if;

      --  Problem with System redefined with Extend_System pragma (E315-001)

      if Nkind (Arg_Node) in N_Has_Chars
       and then
         Chars (Arg_Node) = Name_System
       and then
         Chars (Result_Node) /= Name_System
       and then
         Nkind (Parent (Result_Node)) = N_Defining_Program_Unit_Name
      then
         Result_Node := Entity (Sinfo.Name (Parent (Result_Node)));
         pragma Assert (Chars (Result_Node) = Name_System);
      end if;

      --  Problem with tasks defined by a single task definition: for such a
      --  definition the front-end creates an artificial variable declaration
      --  node, and for the references to such task, the Entity field points to
      --  the entity node from this artificial variable declaration (E224-024).
      --  The same problem exists for a single protected declaration
      --  (E418-015)

      Tmp_Node := Parent (Result_Node);

      if Comes_From_Source (Result_Node)
        and then
          not Comes_From_Source (Tmp_Node)
        and then
          Nkind (Tmp_Node) = N_Object_Declaration
        and then
          not Constant_Present (Tmp_Node)
        and then
          No (Corresponding_Generic_Association (Tmp_Node))
      then
         Tmp_Node := Etype (Result_Node);

         if Ekind (Tmp_Node) in Concurrent_Kind then
            Result_Node := Parent (Result_Node);

            while not (Nkind (Result_Node) = N_Task_Type_Declaration
                     or else
                       Nkind (Result_Node) = N_Protected_Type_Declaration)
            loop
               Result_Node := Prev (Result_Node);
            end loop;

         end if;

         Result_Node := Defining_Identifier (Original_Node (Result_Node));

      end if;

      --  F703-020: see the comment marked by this TN in the body of
      --  A4G.A_Sem.Get_Corr_Called_Entity

      if not Comes_From_Source (Result_Node)
        and then
         Is_Overloadable (Result_Node)
        and then
          Present (Alias (Result_Node))
        and then
            not (Is_Intrinsic_Subprogram (Result_Node))
        and then
            Pass_Generic_Actual (Parent (Result_Node))
      then
         --  ???
         Result_Node := Alias (Result_Node);
      end if;

      --  and here we have to solve the problem with generic instances:
      --  for them Result_Node as it has been obtained above points not
      --  to the defining identifier from the corresponding instantiation,
      --  but to an entity defined in a "implicit" package created by the
      --  compiler

      if Is_Generic_Instance (Result_Node) then
         Result_Node := Get_Instance_Name (Result_Node);
      end if;

      --  If the argument is Is_Part_Of_Implicit reference to a type, we
      --  have to check if it is the reference to a type mark in parameter
      --  or parameter and result profile of inherited subprogram and if it
      --  should be substituted by the reference to the corresponding
      --  derived type

      Tmp_Node := Node_Field_1 (Reference_I);

      if Ekind (Result_Node) in Einfo.Type_Kind
        and then
         Is_From_Inherited (Reference_I)
        and then
          Nkind (Tmp_Node) in Sinfo.N_Entity
        and then
         (Ekind (Tmp_Node) = E_Procedure or else
          Ekind (Tmp_Node) = E_Function)
      then
         Result_Node := Get_Derived_Type (Type_Entity     => Result_Node,
                                          Inherited_Subpr => Tmp_Node);

      end if;

      --  labels (and, probably, statement names!!) makes another problem:
      --  we have to return not the implicit label (statement identifier??)
      --  declaration, but the label (statement name) attached to the
      --  corresponding statement

      if Nkind (Parent (Result_Node)) = N_Implicit_Label_Declaration then
         Result_Node := Get_Statement_Identifier (Result_Node);
      end if;

      Tmp_Node := Original_Node (Parent (Parent (Result_Node)));

      while Nkind (Tmp_Node) = N_Subprogram_Renaming_Declaration
         and then
            not (Comes_From_Source (Tmp_Node))
         and then
            not Pass_Generic_Actual (Tmp_Node)
      loop
         --  Result_Node is a defining name from the artificial renaming
         --  declarations created by the compiler in the for wrapper
         --  package for expanded subprogram instantiation. We
         --  have to go to expanded subprogram spec which is renamed.
         --
         --  We have to do this in a loop in case of nested instantiations

         Result_Node := Sinfo.Name   (Tmp_Node);

         if Nkind (Result_Node) = N_Selected_Component then
            Result_Node := Selector_Name (Result_Node);
         end if;

         Result_Node := Entity (Result_Node);

         Tmp_Node := Parent (Parent (Result_Node));
      end loop;

--      --  ???

--      if Ekind (Result_Node) = E_Operator then
--         Result_Kind := N_Defining_Identifier_Mapping (Result_Node);
--      end if;

      if Nkind (Parent (Result_Node)) = N_Defining_Program_Unit_Name or else
         Nkind (Result_Node) = N_Defining_Program_Unit_Name
      then
         --  if we are processing the reference to a child unit, we have to
         --  go from a defining identifier to the corresponding defining
         --  unit name (the first part of the condition).
         --  If this is a reference to a child subprogram, for which
         --  the separate subprogram specification does not exist,
         --  GNAT generates the tree structure corresponding to such a
         --  separate subprogram specification, and it set the Entity
         --  field for all references to this subprogram pointing
         --  to the defining identifier in this inserted subprogram
         --  specification. This case may be distinguished by the fact,
         --  that Comes_From_Source field for this defining identifier
         --  is set OFF. And in this case we have to go to the defining
         --  identifier in the subprogram body:

         if not Comes_From_Source (Result_Node) then
            --  we have to go to the defining identifier in the
            --  corresponding body:
            while not (Nkind (Result_Node) = N_Subprogram_Declaration) loop
               Result_Node := Parent (Result_Node);
            end loop;

            Result_Node := Corresponding_Body (Result_Node);
         end if;

         if Nkind (Result_Node) /= N_Defining_Program_Unit_Name then
            Result_Node := Parent (Result_Node);
         end if;

         Result_Kind := A_Defining_Expanded_Name;

         if not Comes_From_Source (Result_Node) then
            --  now it means that we have a library level instantiation
            --  of a generic child package
            Result_Node := Parent (Parent (Result_Node));
            Result_Node := Original_Node (Result_Node);

            if Nkind (Result_Node) = N_Package_Declaration then
               Result_Node := Sinfo.Corresponding_Body (Result_Node);

               while Nkind (Result_Node) /= N_Package_Body loop
                  Result_Node := Parent (Result_Node);
               end loop;

               Result_Node := Original_Node (Result_Node);
            end if;

            Result_Node := Defining_Unit_Name (Result_Node);
         end if;

      end if;

      if Nkind (Result_Node) = N_Defining_Identifier and then
         (Ekind (Result_Node) = E_In_Parameter or else
          Ekind (Result_Node) = E_Constant) and then
         Present (Discriminal_Link (Result_Node))
      then
         --  here we have to go to an original discriminant
         Result_Node := Discriminal_Link (Result_Node);
      end if;

      --  FA13-008: subtype mark in parameter specification in implicit "/="
      --  declaration in case if in the corresponding "=" the parameter is
      --  specified by 'Class attribute:

      if Nkind (Arg_Node) = N_Identifier
       and then
        not Comes_From_Source (Arg_Node)
       and then
        Ekind (Result_Node) = E_Class_Wide_Type
       and then
        Result_Node /= Defining_Identifier (Parent (Result_Node))
      then
         Result_Node := Defining_Identifier (Parent (Result_Node));
      end if;

      --  Now we have Result_Node pointing to some defining name. There are
      --  some kinds of entities which require special processing. For
      --  implicitly declared  entities we have to set Association_Type
      --  pointing to a type which "generates" the corresponding implicit
      --  declaration (there is no harm to set Association_Type for explicitly
      --  declared entities, but for them it is of no use). For predefined
      --  entities the special case attribute should be set.

      ----------------------------------------
      -- temporary solution for 5522-003 ???--
      ----------------------------------------

      --  The problem for record components:
      --
      --  1. The Entity field for references to record components and
      --     disciminants may point to field of some implicit types created
      --     by the compiler
      --
      --  2. The Entity field for the references to the (implicitly declared!)
      --     components of a derived record type point to the explicit
      --     declarations of the component of the ancestor record type
      --
      --  3. Probably, all this stuff should be incapsulated in a separate
      --     subprogram???

      --  Here we already have Result_Node:

      if Nkind (Result_Node) = N_Defining_Identifier and then
         (Ekind (Result_Node) = E_Component    or else
          Ekind (Result_Node) = E_Discriminant or else
          Ekind (Result_Node) = E_Entry        or else
          Ekind (Result_Node) = E_Procedure    or else
          Ekind (Result_Node) = E_Function)
      then

         --  first, we compute Association_Type as pointed to a type
         --  declaration for which Agr_Node is a component:

         if No (Association_Type) then
            Association_Type := Parent (Arg_Node);

            if Nkind (Association_Type) = N_Function_Call then
               Association_Type := Sinfo.Name (Association_Type);
            end if;

            case Nkind (Association_Type) is

               when N_Component_Clause   =>
                  Association_Type :=
                     Sinfo.Identifier (Parent (Association_Type));

               when N_Selected_Component =>
                  Association_Type := Prefix (Association_Type);

                  if Nkind (Association_Type) = N_Attribute_Reference
                   and then
                      (Attribute_Name (Association_Type) =
                         Name_Unrestricted_Access
                      or else
                       Attribute_Name (Association_Type) = Name_Access)
                  then
                     --  See G222-012
                     Association_Type := Prefix (Association_Type);
                  end if;

                  if Nkind (Association_Type) = N_Selected_Component then
                     Association_Type := Selector_Name (Association_Type);

                  end if;

               when N_Component_Association =>
                  Association_Type := Parent (Association_Type);

               when N_Discriminant_Association =>

                  if Arg_Node = Sinfo.Expression (Association_Type) then
                     --  using a discriminant in initialization expression
                     Association_Type := Empty;
                  else

                     Association_Type := Scope (Result_Node);

                  end if;

               when others =>
                  --  We set Association_Type as Empty to indicate the case of
                  --  a definitely explicit result

                  Association_Type := Empty;
            end case;

         end if;

         if Present (Association_Type) then

            if not (Comes_From_Source (Association_Type)
                  and then
                    Nkind (Association_Type) in N_Entity
                  and then
                    Ekind (Association_Type) in Einfo.Type_Kind)
            then
               Association_Type := Etype (Association_Type);
            end if;

            if Nkind (Original_Node (Parent (Association_Type))) =
               N_Single_Task_Declaration
              or else
               Nkind (Original_Node (Parent (Association_Type))) =
               N_Single_Protected_Declaration
            then
               Association_Type := Empty;
            else

               if Ekind (Result_Node) = E_Component
                 and then
                  not Comes_From_Source (Parent (Result_Node))
                 and then
                  Ekind (Association_Type) in Private_Kind
               then
                  Association_Type := Full_View (Association_Type);
               end if;

               Association_Type :=
                  Explicit_Type_Declaration_Unwound_Unaccess
                    (Association_Type, Arg_Node);

               if Nkind (Original_Node (Association_Type)) in
                 N_Protected_Type_Declaration ..
                 N_Private_Extension_Declaration
               then
                  Association_Type :=
                    Parent
                      (Full_View (Defining_Identifier (Original_Node
                        (Association_Type))));
               end if;

            end if;

         end if;

         --  then, we have to adjust result Node:

         if Ekind (Result_Node) = E_Discriminant and then
            Chars (Discriminal (Result_Node)) /=
            Chars (Original_Record_Component (Result_Node))
         then
            --  This condition is the clue for discriminants explicitly
            --  declared in  declarations of derived types.
            --  These assignments below resets Result_Node to
            --  N_Defining_Identifier node which denotes the same discriminant
            --  but has a properly set bottom-up chain of Parent nodes
            Result_Node := Discriminal (Result_Node);
            Result_Node := Discriminal_Link (Result_Node);

         else
            --  There we have to come from an implicit type to a explicitly
            --  declared type:

            Tmp_Node := Scope (Result_Node);

            if Ekind (Tmp_Node) = E_Record_Subtype then
               Tmp_Node := Etype (Tmp_Node);
            end if;

            if (Ekind (Result_Node) = E_Component
               or else
                Ekind (Result_Node) = E_Discriminant)
             and then
               not (Comes_From_Source (Result_Node)
                  and then
                    not Comes_From_Source (Parent (Result_Node)))
            then
               --  This condition leaves unchanged inherited discriminants
               --  of derived record types

               Tmp_Node := First_Entity (Tmp_Node);

               while Present (Tmp_Node) loop
                  if Chars (Tmp_Node) = Chars (Result_Node) then
                     Result_Node := Tmp_Node;
                     exit;
                  end if;

                  Tmp_Node := Next_Entity (Tmp_Node);
               end loop;

            end if;

         end if;

         --  A private type may require some special adjustment in case if
         --  full view is visible: if Result_Node is a discriminant:
         --  it points to a discriminant in a private view, and we have
         --  to reset it to point to the discriminant in the full view
         if Present (Association_Type)
           and then
            Has_Private_Declaration (Defining_Identifier (Association_Type))
           and then
            Ekind (Result_Node) = E_Discriminant
           and then
            Nkind (Association_Type) /= N_Private_Type_Declaration
           and then
            Nkind (Association_Type) /= N_Private_Extension_Declaration
           and then
            Is_Type_Discriminant (Result_Node,
                                  Original_Node (Association_Type))
         then
            Result_Node := Reset_To_Full_View (Association_Type, Result_Node);
         end if;
         --  Now, we have to define if we have an implicit component here.
         --  Result_Context_Node is finally supposed to be set to the
         --  declaration of the type to which the argument component belongs

         if No (Association_Type) then
            --  definitely explicit result:
            Is_Inherited := False;

         elsif Is_Rewrite_Substitution (Association_Type) then
            --  here we have a derived type with no record extension part
            --  but it can have an explicitly declared discriminant

            if Ekind (Result_Node) = E_Discriminant then
               Is_Inherited := not (Is_Type_Discriminant (
                  Result_Node, Original_Node (Association_Type)));
            else
               Is_Inherited := True;
            end if;

         elsif Nkind (Association_Type) = N_Incomplete_Type_Declaration
               or else
               Nkind (Association_Type) = N_Private_Extension_Declaration
               or else
               Nkind (Association_Type) = N_Private_Type_Declaration
               or else
               Nkind (Association_Type) = N_Task_Type_Declaration
               or else
               Nkind (Association_Type) = N_Protected_Type_Declaration
               or else
               (Nkind (Association_Type) = N_Formal_Type_Declaration and then
                Nkind (Sinfo.Formal_Type_Definition (Association_Type)) =
                N_Formal_Private_Type_Definition)
               or else
                Nkind (Sinfo.Type_Definition (Association_Type)) =
                N_Record_Definition
         then
            --  should be an explicit component
            Is_Inherited := False;

            --  Patch for E407-A08

            if Ekind (Result_Node) = E_Component then
               Result_Node := Original_Record_Component (Result_Node);
            end if;

         elsif Nkind (Sinfo.Type_Definition (Association_Type)) =
               N_Derived_Type_Definition
         then
            --  it may be an inherited component or an explicitly declared
            --  discriminant or a component from a record extension part

            if Is_Explicit_Type_Component (Result_Node, Association_Type) then
               Is_Inherited := False;
            else
               Is_Inherited := True;
            end if;

         else
            --  ??? this Assert pragma - only for development/debug period
            --  ??? what else except N_Selected_Component could be here
            null;
            pragma Assert (False);
         end if;

      end if;

      -------------------------------------------------
      -- end for the temporary solution for 5522-003 --
      -------------------------------------------------

      --------------------------
      -- Enumeration literals --
      --------------------------

      if not (Defined_In_Standard (Arg_Node))
        and then
         Nkind (Result_Node) = N_Defining_Identifier
         --  or else
         --  Nkind (Result_Node) = N_Defining_Character_Literal)
         and then
          Ekind (Result_Node) = E_Enumeration_Literal
         and then (not Comes_From_Source (Result_Node))
      then
         --  an enumeration literal inherited by a derived type definition
         --  (character literals are still processed by a separate function
         --  Character_Literal_Name_Definition, that's why the corresponding
         --  part of the condition is commented out)

         --  ???Needs revising for the new model of implicit Elements

         Is_Inherited     := True;
         Association_Type := Etype (Arg_Node);
         Association_Type :=
            Explicit_Type_Declaration_Unwound (Association_Type);
      end if;

      ---------------------------------------
      -- The rest of special processing:   --
      -- somewhat messy and needs revising --
      ---------------------------------------

      --  We have to turn off for a while the full processing of the
      --  implicit elements (Hope to fix this soon).

      if Defined_In_Standard (Arg_Node)
        or else
         Sloc (Arg_Node) <= Standard_Location
        or else
         Sloc (Result_Node) <= Standard_Location
      then
         --  We need the second part of the condition for references to
         --  Standard.Characters which are parts of the definitions in
         --  the ASCII package

         if Ekind (Result_Node) = E_Operator then
            return Nil_Element;
         else
            --  I hope, that I'm right, that all the *identifiers* declared
            --  in standard are declared explicitly, and all the rest
            --  (which are defined in Standard) are implicit
            --  Root and universal types can make a problem, but let's
            --  see it before...
            Spec_Case := Explicit_From_Standard;
         end if;

      else

         if Result_Kind in Internal_Defining_Operator_Kinds then

            if Is_Predefined (Result_Node) then
               Spec_Case := Predefined_Operation;
--            --  note, that Predefined_Operation corresponds to an
--            --  implicitly declared operation of a type, which is defined
--            --  not in the Standard package
--            Association_Type := Enclosed_Type (Result_Node);
--            --  we have to use namely Association_Type, but not Result_Node
--            --  to define Result_Unit, because sometimes Result_Node
--            --  does not have the Parent field set
               return Nil_Element;
               --  ???!!! this turns off all the predefined operations
               --  !!!??? defined not in Standard
            elsif Is_Impl_Neq (Result_Node) then
               Spec_Case := Is_From_Imp_Neq_Declaration;
            end if;

         end if;
      end if;

      -------------------
      -- Limited views --
      -------------------

      if Spec_Case = Not_A_Special_Case then

         Tmp_Node := Result_Node;

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

         if Nkind (Tmp_Node) in N_Entity then
            case Ekind (Tmp_Node) is
               when Einfo.Type_Kind =>
                  if not Comes_From_Source (Tmp_Node)
                    and then
                     Ekind (Tmp_Node) in Incomplete_Kind
                    and then
                     Present (Non_Limited_View (Tmp_Node))
                  then
                     Spec_Case   := From_Limited_View;
                     Result_Node := Non_Limited_View (Result_Node);
                  end if;
               when E_Package =>
                  if not Is_Generic_Instance (Tmp_Node) then

                     if not Analyzed (Parent (Result_Node)) then
                        Spec_Case := From_Limited_View;
                     elsif Is_Limited_Withed (Result_Node, Reference_I) then
                        Spec_Case := From_Limited_View;
                     end if;

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

      if Spec_Case not in Predefined
        and then
         Spec_Case /= Is_From_Imp_Neq_Declaration
        and then
         Spec_Case /= From_Limited_View
        and then
         not Comes_From_Source (Result_Node)
        and then
         No (Association_Type)
        and then
         not Part_Of_Pass_Generic_Actual (Result_Node)
      then
         --  Here we may have the following possibilities:
         --  - library-level subprogram instantiation;
         --  - artificial entity created for an inner package from a package
         --    "withed" by a limited with clause;
         --  - defining name from the artificial spec created for subprogram
         --    body which acts as a spec;
         --  - prefix of the artificial 'Class attribute reference (ASIS has
         --    to emulate such an attribute reference in case if a class-wide
         --    type is use as an actual type in the instantiation);
         --  - index (sub)type in case if the corresponding type is declared as
         --    private (F424-A01);
         --  - F619-024;
         --  - F627-001
         --  - inherited subprogram;

         if Nkind (Parent (Result_Node)) in N_Subprogram_Specification then

            if Is_Generic_Instance (Result_Node) then
               --  Library-level subprogram instantiation
               --  Here we have to go from the rewritten to the original
               --  tree structure

               --  This code appeared at some point, but it seems that it is
               --  of no real need. Will be for a while - just in case.
               --  It does not allow to fix G312-006
               --  ???

--             Result_Node := Parent (Parent (Parent (Parent (Result_Node))));
--             Result_Node := Original_Node (Result_Node);
--             Result_Node := Sinfo.Defining_Unit_Name (Result_Node);
               null;
            else
               --  Artificial subprogram spec created for the body acting
               --  as spec
               Result_Node := Parent (Parent (Result_Node));
               Result_Node := Corresponding_Body (Result_Node);
            end if;

         elsif Nkind (Parent (Result_Node)) = N_Package_Specification
             and then
               Comes_From_Source (Parent (Result_Node))
         then
            --  An artificial internal entity created for a local package
            --  from a package that is "withed" by limited with clause
            --  We go to the entity node the package spec points to.
            --  See F310-025 and F311-003.
            Result_Node := Defining_Unit_Name (Parent (Result_Node));

         elsif Special_Case (Reference_I) = Dummy_Class_Attribute_Prefix
            and then
               Ekind (Result_Node) = E_Class_Wide_Type
         then
            Result_Node := Defining_Identifier (Parent (Result_Node));

         elsif Ekind (Result_Node) in Discrete_Kind
            and then
               Nkind (Parent (Result_Node)) = N_Subtype_Declaration
         then
            --  Go to the full view of the corresponding private type:
            Result_Node := Sinfo.Subtype_Indication (Parent (Result_Node));
            Result_Node := Entity (Result_Node);
            pragma Assert (Ekind (Result_Node) in Private_Kind);
            Result_Node := Full_View (Result_Node);

         elsif Ekind (Result_Node) = E_Package
            and then
               Is_Hidden (Result_Node)
            and then
               Is_Rewrite_Substitution (R_Node (Reference_I))
         then
            --  This is the case when we have a reference to the instantiation
            --  of generic parent in the instantiation of generic child,
            --  see F619-024

            Result_Node := Entity (R_Node (Reference_I));

            if Nkind (Parent (Result_Node)) = N_Defining_Program_Unit_Name then
               Result_Node := Parent (Result_Node);
               Result_Kind := A_Defining_Expanded_Name;
            end if;

         elsif Ekind (Result_Node) = E_Package
            and then
               Nkind (Parent (Result_Node)) = N_Package_Renaming_Declaration
            and then
               not Comes_From_Source (Parent (Result_Node))
         then
            --  Reference_I is the reference to the name of the instantiation
            --  inside an expanded template, but the name of the template is
            --  the defining expanded name. In this case we have to use the
            --  entity of the rewritten node (F627-001)

            Result_Node := Entity (R_Node (Reference_I));
         else
            --  It should be inherited!
            --  The last condition is needed to filter out already processed
            --  cases. This case corresponds to inherited user-defined
            --  subprograms

            Is_Inherited     := True;

            if Ekind (Result_Node) = E_Function or else
               Ekind (Result_Node) = E_Procedure
            then
               Association_Type := Result_Node;
               --  Points to the defining identifier of implicit inherited
               --  subprogram

               Result_Node := Explicit_Parent_Subprogram (Result_Node);

               --  Now Result_Node points to the defining identifier of
               --  explicit subprogram which is inherited

            else
               --  ??? Probably will need revising when inherited record
               --  components and enumeration literals are fully
               --  implemented
               Association_Type := Defining_Identifier (Parent (Result_Node));
               Association_Type := First_Subtype (Association_Type);
            end if;

         end if;

      end if;

      if Defined_In_Standard (Arg_Node) then

         --  Here we may need to adjust the result node in case if it is an
         --  entity representing an unconstrained base type for a signed
         --  integer type (see Cstand.Create_Unconstrained_Base_Type)

         if No (Parent (Result_Node))
           and then
            Ekind (Result_Node) = E_Signed_Integer_Type
         then
            Result_Node := Parent (Scalar_Range (Result_Node));
         end if;

         Result_Unit  := Get_Comp_Unit
                           (Standard_Id, Encl_Cont_Id (Reference_I));

      else
         if Result_Kind in Internal_Defining_Operator_Kinds and then
            Is_Predefined (Result_Node)
         then
            null;
--            --  note, that Predefined_Operation corresponds to an
--            --  implicitly declared operation of a type, which is defined
--            --  not in the Standard package
--            Association_Type := Enclosed_Type (Result_Node);
--            --  we have to use namely Association_Type, but not Result_Node
--            --  to define Result_Unit, because sometimes Result_Node
--            --  does not have the Parent field set
--            Result_Unit :=
--               Enclosing_Unit (Encl_Cont_Id (Reference_I), Association_Type);
            return Nil_Element;
            --  ???!!! this turns off all the predefined operations
            --  !!!??? defined not in Standard

         elsif Is_Inherited then

            Result_Unit :=
               Enclosing_Unit (Encl_Cont_Id (Reference_I), Association_Type);

         else

            Result_Unit :=
               Enclosing_Unit (Encl_Cont_Id (Reference_I), Result_Node);

         end if;
      end if;

      if Is_Inherited
        and then
         (Ekind (Result_Node) = E_Component or else
          Ekind (Result_Node) = E_Discriminant)
      then
         Componnet_Name := Result_Node;
      end if;

      --  A special case of fake Numeric_Error renaming is handled
      --  separately (see B712-0050)

      if Result_Node = Standard_Constraint_Error and then
         Chars (Result_Node) /= Chars (Arg_Node)
      then
         Result := Get_Numeric_Error_Renaming;
         Set_Int_Kind (Result, A_Defining_Identifier);
      else

         Result :=
            Node_To_Element_New (Node          => Result_Node,
                                 Node_Field_1  => Association_Type,
                                 Node_Field_2  => Componnet_Name,
                                 Internal_Kind => Result_Kind,
                                 Spec_Case     => Spec_Case,
                                 Inherited     => Is_Inherited,
                                 In_Unit       => Result_Unit);
      end if;

      --  See the comment in the body of A4G.A_Sem.Get_Corr_Called_Entity

      if Present (Association_Type) then

         if Is_From_Instance (Association_Type) then
            Set_From_Instance (Result, True);
         else
            Set_From_Instance (Result, False);
         end if;

      end if;

      if Spec_Case = From_Limited_View then
         Set_From_Implicit (Result, True);
      end if;

      return Result;
   end Identifier_Name_Definition;

   --------------------------------
   -- Is_Explicit_Type_Component --
   --------------------------------

   function Is_Explicit_Type_Component
     (Comp_Def_Name : Node_Id;
      Type_Decl     : Node_Id)
      return          Boolean
   is
      Result    : Boolean := False;
      Cont_Node : Node_Id;
   begin
      Cont_Node := Parent (Comp_Def_Name);

      while Present (Cont_Node) loop

         if Cont_Node = Type_Decl then
            Result := True;
            exit;
         end if;

         Cont_Node := Parent (Cont_Node);
      end loop;

      return Result;

   end Is_Explicit_Type_Component;

   ------------------------------
   -- Is_From_Dispatching_Call --
   ------------------------------

   function Is_From_Dispatching_Call (Reference : Element) return Boolean is
      Can_Be_Dynamically_Identified : Boolean := False;

      Ref_Node    : Node_Id;
      Parent_Ref_Node : Node_Id;

      Ref_Entity  : Entity_Id;
      Parent_Call : Node_Id := Empty;
      Result      : Boolean := False;
   begin

      Ref_Node := R_Node (Reference);

      if not (Nkind (Ref_Node) = N_Identifier
            or else
             Nkind (Ref_Node) = N_Operator_Symbol)
      then
         return False;
      end if;

      Parent_Ref_Node := Parent (Ref_Node);

      if Nkind (Parent_Ref_Node) = N_Expanded_Name
        and then
         Ref_Node = Selector_Name (Parent_Ref_Node)
      then
         Ref_Node        := Parent (Ref_Node);
         Parent_Ref_Node := Parent (Ref_Node);
      end if;

      --  First, detect if Reference indeed can be dynamically identified, that
      --  is, it is either a subprogram name in a call or a formal parameter
      --  name in a parameter association. Because of the performance reasons,
      --  we do this on the tree structures, but not using ASIS queries

      case Nkind (Parent_Ref_Node) is
         when N_Parameter_Association =>

            if Selector_Name (Parent_Ref_Node) = Ref_Node then
               Can_Be_Dynamically_Identified := True;
            end if;

         when N_Procedure_Call_Statement |
              N_Function_Call            =>

            if Sinfo.Name (Parent_Ref_Node) = Ref_Node then
               Can_Be_Dynamically_Identified := True;
            end if;

         when others =>
            null;
      end case;

      if Can_Be_Dynamically_Identified then
         Ref_Entity := Entity (Ref_Node);

         if No (Ref_Entity)
           and then
            Nkind (Parent (Ref_Node)) = N_Expanded_Name
           and then
            Ref_Node = Selector_Name (Parent (Ref_Node))
         then
            Ref_Node := Parent (Ref_Node);
            Ref_Entity := Entity (Ref_Node);
         end if;

         if Present (Ref_Entity) then

            case Ekind (Ref_Entity) is
               when Formal_Kind =>
                  Parent_Call := Parent (Parent (Ref_Node));

               when Subprogram_Kind =>
                  Parent_Call := Parent (Ref_Node);
               when others =>
                  null;
            end case;

         end if;

         if Present (Parent_Call)
          and then
            (Nkind (Parent_Call) = N_Procedure_Call_Statement
            or else
             Nkind (Parent_Call) = N_Function_Call)
          and then
            Present (Controlling_Argument (Parent_Call))
         then
            Result := True;
         end if;

      end if;

      return Result;

   end Is_From_Dispatching_Call;

   ----------------------------
   -- Is_Implicit_Formal_Par --
   ----------------------------

   function Is_Implicit_Formal_Par (Result_El : Element) return Boolean is
      Result      :          Boolean := False;
      Res_Node    : constant Node_Id := Node (Result_El);
      Parent_Node :          Node_Id;
   begin

      if Nkind (Res_Node) in N_Entity
       and then
         Ekind (Res_Node) in Formal_Kind
      then
         Parent_Node := Parent (Res_Node);

         if Present (Parent_Node)
           and then
            Nkind (Parent_Node) = N_Parameter_Specification
           and then
            Res_Node /= Defining_Identifier (Parent_Node)
         then
            --  The condition is no more than just a clue...
            Result := True;
         end if;

      end if;

      return Result;
   end Is_Implicit_Formal_Par;

   -----------------------
   -- Is_Limited_Withed --
   -----------------------

   function Is_Limited_Withed
     (E         : Entity_Id;
      Reference : Asis.Element)
      return Boolean
   is
      Result : Boolean := False;
      CU_E   : Asis.Compilation_Unit;
      CU_R   : Asis.Compilation_Unit;
   begin
      CU_E :=  Enclosing_Unit (Encl_Cont_Id (Reference), E);

      if Unit_Kind (CU_E) = A_Package then
         CU_R := Enclosing_Compilation_Unit (Reference);

         if not Is_Equal (CU_R, CU_E) then
            declare
               CU_E_Name : constant Program_Text :=
                 To_Upper_Case (Unit_Full_Name (CU_E));

               Comp_Clauses : constant Asis.Element_List :=
                 Context_Clause_Elements (CU_R);

               Name_List : Element_List_Access;
            begin
               for C in Comp_Clauses'Range loop
                  if Trait_Kind (Comp_Clauses (C)) in
                       A_Limited_Trait .. A_Limited_Private_Trait
                  then
                     Name_List :=
                       new Asis.Element_List'(Clause_Names (Comp_Clauses (C)));

                     for N in Name_List'Range loop
                        if To_Upper_Case (Full_Name_Image (Name_List (N))) =
                                CU_E_Name
                        then
                           Free (Name_List);
                           Result := True;
                           exit;
                        end if;
                     end loop;

                     Free (Name_List);
                  end if;
               end loop;
            end;
         end if;
      end if;

      return Result;
   end Is_Limited_Withed;

   -----------------------------------
   -- Is_Part_Of_Defining_Unit_Name --
   -----------------------------------

   function Is_Part_Of_Defining_Unit_Name
     (Name_Node : Node_Id)
      return      Boolean
   is
      Result    : Boolean := False;
      Next_Node : Node_Id := Parent (Name_Node);
   begin
      while Present (Next_Node) loop

         if Nkind (Next_Node) = N_Defining_Program_Unit_Name then
            Result := True;
            exit;
         elsif not (Nkind (Next_Node) = N_Expanded_Name or else
                    Nkind (Next_Node) = N_Selected_Component)
         then
            --  theoretically, we need only the first part of the condition,
            --  but the unit name in the body is not fully decorated and,
            --  therefore, has the wrong syntax structure, so we need the
            --  second part. We are keeping both in order to have the correct
            --  code if it is changed in the tree.
            exit;
         else
            Next_Node := Parent (Next_Node);
         end if;

      end loop;

      return Result;

   end Is_Part_Of_Defining_Unit_Name;

   ------------------
   -- Is_Reference --
   ------------------

   function Is_Reference
     (Name : Asis.Element;
      Ref  : Asis.Element)
      return Boolean
   is
      Ref_Kind : constant Internal_Element_Kinds := Reference_Kind (Name);
      Result   : Boolean                         := False;
   begin

      if Int_Kind (Ref) = Ref_Kind then

         begin

            if Is_Equal (Corresponding_Name_Definition (Ref), Name) then
               Result := True;
            end if;

         exception
            --  Corresponding_Name_Definition may raise Asis_Failed with
            --  Value_Error status when applied to identifiers which
            --  cannot have definitions (see section 17.6). Here we
            --  have to skip such Elements paying no attention to
            --  exception raising
            when others => null;
         end;

      end if;

      return Result;

   end Is_Reference;

   --------------------------
   -- Is_Type_Discriminant --
   --------------------------

   function Is_Type_Discriminant
     (Discr_Node : Node_Id;
      Type_Node  : Node_Id)
      return       Boolean
   is
      Discr_Chars     : constant Name_Id := Chars (Discr_Node);
      Discr_List      : List_Id;
      Next_Discr_Spec : Node_Id;
      Result          : Boolean := False;
   begin
      Discr_List := Discriminant_Specifications (Type_Node);

      if Present (Discr_List) then
         Next_Discr_Spec := First (Discr_List);

         while Present (Next_Discr_Spec) loop

            if Chars (Defining_Identifier (Next_Discr_Spec)) = Discr_Chars then
               Result := True;
               exit;
            end if;

            Next_Discr_Spec := Next (Next_Discr_Spec);
         end loop;

      end if;

      return Result;

   end Is_Type_Discriminant;

   ----------------
   -- Needs_List --
   ----------------

   function Needs_List (Reference : Asis.Element) return Boolean is
      Result         : Boolean := False;
      N              : Node_Id := R_Node (Reference);
      Entity_N       : Entity_Id;
      Pragma_Name_Id : Name_Id;
   begin

      if Nkind (Parent (N)) = N_Pragma_Argument_Association then
         Pragma_Name_Id := Pragma_Name (Parent (Parent (N)));

         if Pragma_Name_Id = Name_Asynchronous or else
            Pragma_Name_Id = Name_Convention   or else
            Pragma_Name_Id = Name_Export       or else
            Pragma_Name_Id = Name_Import       or else
            Pragma_Name_Id = Name_Inline
         then
            Entity_N := Entity (N);

            if Present (Entity_N) and then
               Is_Overloadable (Entity_N) and then
               Has_Homonym (Entity_N)
            then
               --  ??? Is this the right condition???
               --  ??? At the moment we do not consider any GNAT-specific
               --      pragma

               N := Homonym (Entity_N);

               if Present (N)
                 and then
                  (not (Sloc (N) <= Standard_Location
                  --  !!! Note, that this check filters out the predefined
                  --  implicitly declared operations!!!
                     or else
                        Part_Of_Pass_Generic_Actual (N)
                     or else
                       (Ekind (N) in Subprogram_Kind and then
                        Is_Formal_Subprogram (N))))
               then
                  Result := True;

               end if;

            end if;

         end if;

      end if;

      return Result;
   end Needs_List;

   --------------------
   -- Reference_Kind --
   --------------------

   function Reference_Kind
     (Name            : Asis.Element)
      return            Internal_Element_Kinds
   is
      Arg_Kind : Internal_Element_Kinds := Int_Kind (Name);
      Result   : Internal_Element_Kinds := Not_An_Element;
   begin

      if Arg_Kind in Internal_Defining_Name_Kinds then

         if Arg_Kind = A_Defining_Expanded_Name then
            Arg_Kind := Int_Kind (Defining_Selector (Name));
         end if;

      end if;

         case Arg_Kind is
            when A_Defining_Identifier          =>
               Result := An_Identifier;
            when A_Defining_Character_Literal   =>
               Result := A_Character_Literal;
            when A_Defining_Enumeration_Literal =>
               Result := An_Enumeration_Literal;
            when A_Defining_And_Operator        =>
               Result := An_And_Operator;
            when A_Defining_Or_Operator         =>
               Result := An_Or_Operator;
            when A_Defining_Xor_Operator        =>
               Result := An_Xor_Operator;
            when A_Defining_Equal_Operator      =>
               Result := An_Equal_Operator;
            when A_Defining_Not_Equal_Operator  =>
               Result := A_Not_Equal_Operator;
            when A_Defining_Less_Than_Operator  =>
               Result := A_Less_Than_Operator;
            when A_Defining_Less_Than_Or_Equal_Operator =>
               Result := A_Less_Than_Or_Equal_Operator;
            when A_Defining_Greater_Than_Operator =>
               Result := A_Greater_Than_Operator;
            when A_Defining_Greater_Than_Or_Equal_Operator =>
               Result := A_Greater_Than_Or_Equal_Operator;
            when A_Defining_Plus_Operator =>
               Result := A_Plus_Operator;
            when A_Defining_Minus_Operator =>
               Result := A_Minus_Operator;
            when A_Defining_Concatenate_Operator =>
               Result := A_Concatenate_Operator;
            when A_Defining_Unary_Plus_Operator =>
               Result := A_Unary_Plus_Operator;
            when A_Defining_Unary_Minus_Operator =>
               Result := A_Unary_Minus_Operator;
            when A_Defining_Multiply_Operator =>
               Result := A_Multiply_Operator;
            when A_Defining_Divide_Operator =>
               Result := A_Divide_Operator;
            when A_Defining_Mod_Operator =>
               Result := A_Mod_Operator;
            when A_Defining_Rem_Operator =>
               Result := A_Rem_Operator;
            when A_Defining_Exponentiate_Operator =>
               Result := An_Exponentiate_Operator;
            when A_Defining_Abs_Operator =>
               Result := An_Abs_Operator;
            when A_Defining_Not_Operator =>
               Result := A_Not_Operator;

            when others =>
               null;
         end case;

      return Result;

   end Reference_Kind;

   ------------------------
   -- Reset_To_Full_View --
   ------------------------

   function Reset_To_Full_View
     (Full_View : Node_Id;
      Discr     : Node_Id)
      return      Node_Id
   is
      Result      : Node_Id;
      Discr_Chars : constant Name_Id := Chars (Discr);
   begin

      Result := First (Discriminant_Specifications (Full_View));

      while Present (Result) loop
         exit when Chars (Defining_Identifier (Result)) = Discr_Chars;
         Result := Next (Result);
      end loop;

      pragma Assert (Present (Result));

      Result := Defining_Identifier (Result);

      return Result;

   end Reset_To_Full_View;

   -------------------
   -- Reset_To_Spec --
   -------------------

   function Reset_To_Spec (Name_Node : Node_Id) return Node_Id is
      Result     : Node_Id          := Empty;
      Next_Node  : Node_Id          := Parent (Name_Node);
      Name_Chars : constant Name_Id := Chars (Name_Node);
   begin

      while Nkind (Next_Node) /= N_Defining_Program_Unit_Name loop
         Next_Node := Parent (Next_Node);
      end loop;

      if Nkind (Parent (Next_Node)) in N_Subprogram_Specification then
         Next_Node := Parent (Next_Node);
      end if;

      Next_Node := Corresponding_Spec (Parent (Next_Node));

      while Nkind (Next_Node) /= N_Defining_Program_Unit_Name loop
         Next_Node := Parent (Next_Node);
      end loop;

      Next_Node := Parent (Next_Node);

      Next_Node := Defining_Unit_Name (Next_Node);
      --  Now Next_Node should point to the defining program unit name in the
      --  spec:

      Next_Node := Sinfo.Name (Next_Node);

      while Present (Next_Node) loop

         if Nkind (Next_Node) = N_Expanded_Name then
            Next_Node := Selector_Name (Next_Node);
         end if;

         if Name_Chars = Chars (Next_Node) then
            Result := Next_Node;
            exit;
         end if;

         Next_Node := Parent (Next_Node);

         if Nkind (Next_Node) = N_Expanded_Name then
            Next_Node := Prefix (Next_Node);
         else
            exit;
         end if;

      end loop;

      pragma Assert (Present (Result));

      return Result;

   end Reset_To_Spec;

   ---------------------
   -- Rewritten_Image --
   ---------------------

   function Rewritten_Image (Selector_Name : Node_Id) return Node_Id is
      Name_Chars       : constant Name_Id := Chars (Selector_Name);
      Aggr_Node        : Node_Id;
      Result_Node      : Node_Id := Empty;
      Association_Node : Node_Id;
      Choice_Node      : Node_Id;
   begin
      --  may be, we have to be more smart for aggregates in aggregates...
      Aggr_Node := Parent (Selector_Name);
      --  we are in N_Component_Association node, and its Parent points not
      --  to the original, but to the rewritten structure for aggregate
      Aggr_Node := Parent (Aggr_Node);
      --  we are in the rewritten node for the aggregate
      pragma Assert (
                (Nkind (Aggr_Node) = N_Aggregate or else
                 Nkind (Aggr_Node) = N_Extension_Aggregate)
                and then
                 Is_Rewrite_Substitution (Aggr_Node));
      --  and now - traversing the rewritten structure

      Association_Node :=
         First_Non_Pragma (Component_Associations (Aggr_Node));

      Associations : while Present (Association_Node) loop
         Choice_Node := First_Non_Pragma (Choices (Association_Node));

         --  in the rewritten aggregate it is exactly one choice in any
         --  component association
         if Chars (Choice_Node) = Name_Chars then
            Result_Node := Choice_Node;
            exit Associations;
         end if;

         Association_Node := Next_Non_Pragma (Association_Node);
      end loop Associations;

      pragma Assert (Present (Result_Node));

      return Result_Node;

   end Rewritten_Image;

   ------------------------
   -- Search_Record_Comp --
   ------------------------

   function Search_Record_Comp (Selector_Name : Node_Id) return Entity_Id is
      Result    :           Entity_Id := Empty;
      Res_Chars : constant Name_Id   := Chars (Selector_Name);
      Aggr_Type :           Entity_Id;
   begin
      Aggr_Type := Parent (Selector_Name);

      while not (Nkind (Aggr_Type) = N_Extension_Aggregate
                or else
                 Nkind (Aggr_Type) = N_Aggregate
                or else
                 No (Aggr_Type))
      loop
         Aggr_Type := Parent (Aggr_Type);
      end loop;

      if No (Aggr_Type) then
         --  This definitely means that something went wrong...
         pragma Assert (False);
         return Empty;
      end if;

      Aggr_Type := Etype (Aggr_Type);

      while Ekind (Aggr_Type) /= E_Record_Type loop

         if Ekind (Aggr_Type) = E_Private_Type
           or else
            Ekind (Aggr_Type) = E_Limited_Private_Type
           or else
            Ekind (Aggr_Type) = E_Record_Type_With_Private
         then
            Aggr_Type := Full_View (Aggr_Type);
         else
            Aggr_Type := Etype (Aggr_Type);
         end if;
      end loop;

      Result := First_Entity (Aggr_Type);

      while Chars (Result) /= Res_Chars loop
         Result := Next_Entity (Result);
      end loop;

      pragma Assert (Present (Result));
      return Result;
   end Search_Record_Comp;

   -------------------
   -- To_Upper_Case --
   -------------------

   function To_Upper_Case (S : Wide_String) return Wide_String is
      Result : Wide_String (S'Range);
   begin
      for J in Result'Range loop
         Result (J) := Ada.Wide_Characters.Unicode.To_Upper_Case (S (J));
      end loop;

      return Result;
   end To_Upper_Case;

end A4G.Expr_Sem;