lal_refactor_25.0.0_15241508/src/lal_refactor-subprogram_signature.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
--
--  Copyright (C) 2021-2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Assertions; use Ada.Assertions;
with Ada.Characters.Conversions; use Ada.Characters.Conversions;
with Ada.Containers.Generic_Array_Sort;
with Ada.Containers.Vectors;

with Langkit_Support.Text; use Langkit_Support.Text;

with Laltools.Subprogram_Hierarchy; use Laltools.Subprogram_Hierarchy;

with VSS.Strings.Conversions;

package body LAL_Refactor.Subprogram_Signature is

   Problem_Info : constant VSS.Strings.Virtual_String :=
      "Can't change the controlling parameter of a primitive";

   --------------------------
   -- Generic_Array_Unique --
   --------------------------

   function Generic_Array_Unique (Container : Array_Type) return Array_Type
   is
      procedure Sort is new Ada.Containers.Generic_Array_Sort
        (Index_Type   => Index_Type,
         Element_Type => Element_Type,
         Array_Type   => Array_Type,
         "<"          => "<");

      Unique_Container : Array_Type := Container;
      Cursor           : Index_Type := Container'First;
      Last_New_Element : Element_Type;

   begin
      --  Empty arrays or arrays with only one element do not need to be
      --  processed.

      if Unique_Container'Length < 2 then
         return Unique_Container;
      end if;

      Sort (Unique_Container);

      Last_New_Element := Unique_Container (Unique_Container'First);

      for J in
        Index_Type'Succ (Unique_Container'First) .. Unique_Container'Last
      loop
         if Unique_Container (J) /= Last_New_Element then
            Cursor := Index_Type'Succ (Cursor);
            Unique_Container (Cursor) := Unique_Container (J);
            Last_New_Element := Unique_Container (J);
         end if;
      end loop;

      return Unique_Container (Unique_Container'First .. Cursor);
   end Generic_Array_Unique;

   function Unique is new Generic_Array_Unique
     (Index_Type   => Positive,
      Element_Type => Positive,
      Array_Type   => Parameter_Indices_Type,
      "<"          => "<");
   --  Sorts and removes duplicates of a Parameter_Indices_Type

   type Param_Spec_Indices_Range_Type is
      record
         First, Last : Positive;
      end record;

   type Param_Spec_Indices_Ranges_Type is
     array (Positive range <>) of Param_Spec_Indices_Range_Type;

   type Optional_Designator_Index (Exists : Boolean) is
      record
         case Exists is
            when True =>
               Value : Positive;
            when False =>
               null;
         end case;
      end record;

   function Argument_SLOC
     (Call            : Call_Expr;
      Parameter_Index : Positive)
      return Source_Location_Range
     with Pre => not Call.Is_Null;
   --  Returns the source location range of the argument associated to
   --  'Parameter_Index'.
   --  An Assertion_Error exception is raised if 'Parameter_Index' is greater
   --  than the number of arguments 'Call' has.

   function Arguments_SLOC
     (Call                    : Call_Expr;
      Parameter_Indices_Range : Parameter_Indices_Range_Type)
      return Source_Location_Range
     with Pre => not Call.Is_Null;
   --  Returns a set of source location ranges of the arguments associated to
   --  'Parameter_Indices_Range'.
   --  An Assertion_Error exception is raised if 'Parameter_Indices_Range'
   --  contains an element that is greater than the number of arguments
   --  'Call' has.

   function Arguments_SLOC
     (Call                     : Call_Expr;
      Parameter_Indices_Ranges : Parameter_Indices_Ranges_Type)
      return Source_Location_Range_Set
     with Pre => not Call.Is_Null and then Parameter_Indices_Ranges'Length > 0;
   --  Returns a set of source location ranges of the arguments associated to
   --  'Parameter_Indices_Ranges'.
   --  An Assertion_Error exception is raised if 'Parameter_Indices_Ranges'
   --  contains an element that is greater than the number of arguments
   --  'Call' has, if it is not sorted or if it has overlapping ranges.

   function Arguments_SLOC (Call : Call_Expr) return Source_Location_Range
     with Pre => not Call.Is_Null;
   --  Returns the source location range of Call's arguments

   procedure Change_Mode
     (Subp               : Basic_Decl'Class;
      Parameters_Indices : Parameter_Indices_Range_Type;
      New_Mode           : Ada_Mode;
      Edits              : in out Text_Edit_Map);
   --  Changes the parameter mode of the parameters with indices defined by
   --  'Parameter_Indices_Range' to 'New_Mode'.

   function First_Designator_Index
     (Arguments : Assoc_List)
      return Optional_Designator_Index;
   --  Finds the index of the first parameter association that has a designator

   procedure Move_Backward
     (Subp            : Basic_Decl'Class;
      Parameter_Index : Positive;
      Edits           : in out Text_Edit_Map);
   --  Moves the parameter defined by 'Parameter_Index' backward

   function Param_Specs_SLOC
     (Subp                      : Basic_Decl'Class;
      Param_Spec_Indices_Ranges : Param_Spec_Indices_Ranges_Type)
      return Source_Location_Range_Set
     with Pre => Is_Subprogram (Subp)
                 and then Param_Spec_Indices_Ranges'Length > 0;
   --  Returns a map with the source location range of the Param_Spec nodes
   --  with indices given by Param_Spec_Indices_Ranges. If maximum value of
   --  Param_Spec_Indices_Ranges is > than the amout of Param_Spec nodes 'Subp'
   --  has, then a 'Program_Error' exception is raised.

   -------------------
   -- Argument_SLOC --
   -------------------

   function Argument_SLOC
     (Call            : Call_Expr;
      Parameter_Index : Positive)
      return Source_Location_Range
   is (Arguments_SLOC
       (Call,
        Parameter_Indices_Range_Type'(Parameter_Index, Parameter_Index)));

   --------------------
   -- Arguments_SLOC --
   --------------------

   function Arguments_SLOC
     (Call              : Call_Expr;
      Parameter_Indices : Parameter_Indices_Type)
      return Source_Location_Range_Set
   is
      Max_Length     : constant Positive := Parameter_Indices'Length;
      Indices_Ranges : Parameter_Indices_Ranges_Type (1 .. Max_Length);
      Real_Length    : Positive := 1;
      Last_Index     : Positive;
      Sorted_Indices : constant Parameter_Indices_Type :=
        Unique (Parameter_Indices);

   begin
      --  Start by transforming 'Sorted_Indices' in 'Indices_Ranges'.
      --
      --  Example 1: If 'Sorted_Indices' is [1, 3, 5] then Max_Length = 3,
      --  Indices_Ranges = [{1, 1}, {3, 3}, {5, 5}] and Real_Length = 3.
      --
      --  Example 2: If 'Sorted_Indices' is [1, 2, 3] then Max_Length = 3,
      --  Indices_Ranges = [{1, 3}] and Real_Length = 1.
      --
      --  Then dispatch to an 'Arguments_SLOC' function that receives a
      --  'Parameter_Indices_Ranges_Type'.

      if Max_Length = 1 then
         Real_Length := 1;
         Indices_Ranges (1).First := Sorted_Indices (1);
         Indices_Ranges (1).Last := Sorted_Indices (1);

      else
         Indices_Ranges (Real_Length).First := Sorted_Indices (1);
         Last_Index := Sorted_Indices (1);

         for Index of Sorted_Indices (2 .. Max_Length) loop
            if Index > Last_Index + 1 then
               Indices_Ranges (Real_Length).Last := Last_Index;
               Real_Length := Real_Length + 1;
               Indices_Ranges (Real_Length).First := Index;
            end if;

            Last_Index := Index;
         end loop;

         Indices_Ranges (Real_Length).Last := Last_Index;
      end if;

      return Arguments_SLOC (Call, Indices_Ranges (1 .. Real_Length));
   end Arguments_SLOC;

   --------------------
   -- Arguments_SLOC --
   --------------------

   function Arguments_SLOC
     (Call                    : Call_Expr;
      Parameter_Indices_Range : Parameter_Indices_Range_Type)
      return Source_Location_Range
   is
      Sloc : Source_Location_Range := No_Source_Location_Range;

      Args         : constant Assoc_List := Call.F_Suffix.As_Assoc_List;
      Args_Length  : constant Positive := Length (Args);
      Arg_Index    : Positive := 1;

      First_Arg    : Param_Assoc := No_Param_Assoc;
      Last_Arg     : Param_Assoc := No_Param_Assoc;
      Previous_Arg : Param_Assoc := No_Param_Assoc;

      Remove_Leading_Comma : Boolean := False;

   begin
      if Parameter_Indices_Range.Last > Args_Length then
         raise Assertion_Error;
      end if;

      --  First check if 'Parameter_Indices_Range' refers to all arguments

      if Parameter_Indices_Range.First = 1
        and then Parameter_Indices_Range.Last = Args_Length
      then
         return Arguments_SLOC (Call);
      end if;

      --  The following loop makes sure that two corner cases are dealt with:
      --
      --  Example 1: Remove A from 'My_Call (A, B, C)'
      --
      --  Example 2: Remove B or C from 'My_Call (A, B, C)'
      --
      --  In Example 1, it's 'A' and the leading comma that must be remove.
      --  However, in example 2, it's B and the trailing comma that must be
      --  removed. The leading comma must only be removed if the first argument
      --  we want to remove is also the first argument of the call.

      for Arg of Args loop
         if Remove_Leading_Comma then
            Last_Arg := Arg.As_Param_Assoc;
            Sloc.Start_Line := First_Arg.Sloc_Range.Start_Line;
            Sloc.Start_Column := First_Arg.Sloc_Range.Start_Column;
            Sloc.End_Line := Last_Arg.Sloc_Range.Start_Line;
            Sloc.End_Column := Last_Arg.Sloc_Range.Start_Column;
            Remove_Leading_Comma := False;
         end if;

         exit when Args_Length < Arg_Index;

         if Arg_Index = Parameter_Indices_Range.First then
            if Arg_Index = 1 then
               First_Arg := Arg.As_Param_Assoc;

            else
               First_Arg := Previous_Arg;
            end if;
         end if;

         if Arg_Index = Parameter_Indices_Range.Last then
            Last_Arg := Arg.As_Param_Assoc;

            if Parameter_Indices_Range.First = 1 then
               Remove_Leading_Comma := True;

            else
               Sloc.Start_Line := First_Arg.Sloc_Range.End_Line;
               Sloc.Start_Column := First_Arg.Sloc_Range.End_Column;
               Sloc.End_Line := Last_Arg.Sloc_Range.End_Line;
               Sloc.End_Column := Last_Arg.Sloc_Range.End_Column;
            end if;
         end if;

         Previous_Arg := Arg.As_Param_Assoc;
         Arg_Index := Arg_Index + 1;
      end loop;

      --  'Parameter_Indices_Range' might have been computed using the amount
      --  of parameters a subprogram has. If at least one of those parameters
      --  has a default value, then we might not have found the last arguments
      --  or even any arguments on this 'Call'.

      if Last_Arg = No_Param_Assoc then
         if First_Arg = No_Param_Assoc then
            --  No arguments were found.

            return No_Source_Location_Range;

         else
            --  The last arguments were not found.

            if Parameter_Indices_Range.First = 1 then
               --  The first argument we want to remove is also the first
               --  argument on this 'Call', therefore, remove all arguments.

               return Arguments_SLOC (Call);

            else
               Sloc.Start_Line := First_Arg.Sloc_Range.End_Line;
               Sloc.Start_Column := First_Arg.Sloc_Range.End_Column;
               Sloc.End_Line := Previous_Arg.Sloc_Range.End_Line;
               Sloc.End_Column := Previous_Arg.Sloc_Range.End_Column;
            end if;
         end if;
      end if;

      return Sloc;
   end Arguments_SLOC;

   --------------------
   -- Arguments_SLOC --
   --------------------

   function Arguments_SLOC
     (Call                     : Call_Expr;
      Parameter_Indices_Ranges : Parameter_Indices_Ranges_Type)
      return Source_Location_Range_Set
   is
      Previous_Last_Index : Natural := 0;

   begin
      return Slocs : Source_Location_Range_Set do
         for Parameter_Indices_Range of Parameter_Indices_Ranges loop
            if Parameter_Indices_Range.First <= Previous_Last_Index then
               raise Assertion_Error;
            end if;

            Previous_Last_Index := Parameter_Indices_Range.Last;

            Slocs.Insert (Arguments_SLOC (Call, Parameter_Indices_Range));
         end loop;
      end return;
   end Arguments_SLOC;

   ---------------------
   -- Arguments_SLOC --
   ---------------------

   function Arguments_SLOC (Call : Call_Expr) return Source_Location_Range
   is
      Call_Expr_SLOC_Range : constant Source_Location_Range :=
        Call.Sloc_Range;
      Call_Name_SLOC_Range : constant Source_Location_Range :=
        Call.F_Name.Sloc_Range;

   begin
      return Args_Sloc_Range : Source_Location_Range do
         Args_Sloc_Range.Start_Line := Call_Name_SLOC_Range.End_Line;
         Args_Sloc_Range.Start_Column := Call_Name_SLOC_Range.End_Column;
         Args_Sloc_Range.End_Line   := Call_Expr_SLOC_Range.End_Line;
         Args_Sloc_Range.End_Column := Call_Expr_SLOC_Range.End_Column;
      end return;
   end Arguments_SLOC;

   ----------------------
   -- Param_Specs_SLOC --
   ----------------------

   function Param_Specs_SLOC
     (Subp                      : Basic_Decl'Class;
      Param_Spec_Indices_Ranges : Param_Spec_Indices_Ranges_Type)
      return Source_Location_Range_Set
   is
      Slocs : Source_Location_Range_Set;

      Subp_Params : constant Params := Get_Subp_Params (Subp);

      procedure Add_To_Slocs (Sloc_Range : Source_Location_Range);
      --  Adds Sloc_Range to Sloc. Does not add No_Source_Location_Range.

      -------------------
      -- Add_To_Result --
      -------------------

      procedure Add_To_Slocs (Sloc_Range : Source_Location_Range) is
      begin
         if Sloc_Range = No_Source_Location_Range then
            return;
         end if;

         Slocs.Insert (Sloc_Range);
      end Add_To_Slocs;

      Indices_Ranges_Index : Positive := 1;
      Param_Spec_Index : Positive := 1;
      Param_Spec_List_Length : Positive;

      First_Param_Spec : Param_Spec := No_Param_Spec;
      Last_Param_Spec : Param_Spec := No_Param_Spec;
      Before_First_Param_Spec : Param_Spec := No_Param_Spec;
      Previous_Param_Spec : Param_Spec := No_Param_Spec;

      Next_Is_Last : Boolean := False;

      Param_Specs_Range : Source_Location_Range := No_Source_Location_Range;

   begin
      if Subp_Params.Is_Null or else Subp_Params = No_Params then
         raise Program_Error
           with Subp.Image & " does not have any params";
      end if;

      if Param_Spec_Indices_Ranges'Length = 0 then
         return Slocs;
      end if;

      Param_Spec_List_Length := Length (Subp_Params.F_Params);

      if Param_Spec_Indices_Ranges (1).First = 1 and then
        Param_Spec_Indices_Ranges (1).Last = Param_Spec_List_Length
      then
         if Param_Spec_Indices_Ranges'Length /= 1 then
            raise Program_Error
              with "Param_Spec_Indices_Ranges is invalid";
         end if;

         Add_To_Slocs (Params_SLOC (Subp));
      else
         for Param_Spec of Subp_Params.F_Params loop
            if Next_Is_Last then
               Last_Param_Spec := Param_Spec.As_Param_Spec;

               Param_Specs_Range.Start_Line :=
                 First_Param_Spec.Sloc_Range.Start_Line;
               Param_Specs_Range.Start_Column :=
                 First_Param_Spec.Sloc_Range.Start_Column;
               Param_Specs_Range.End_Line :=
                 Last_Param_Spec.Sloc_Range.Start_Line;
               Param_Specs_Range.End_Column :=
                 Last_Param_Spec.Sloc_Range.Start_Column;

               Add_To_Slocs (Param_Specs_Range);

               Param_Specs_Range := No_Source_Location_Range;

               Next_Is_Last := False;
            end if;

            exit when Indices_Ranges_Index > Param_Spec_Indices_Ranges'Length;

            if Param_Spec_Index =
              Param_Spec_Indices_Ranges (Indices_Ranges_Index).First
            then
               First_Param_Spec := Param_Spec.As_Param_Spec;
               Before_First_Param_Spec := Previous_Param_Spec.As_Param_Spec;
            end if;

            if Param_Spec_Index =
              Param_Spec_Indices_Ranges (Indices_Ranges_Index).Last
            then
               if Param_Spec_Index = Param_Spec_List_Length then
                  Last_Param_Spec := Param_Spec.As_Param_Spec;

                  Param_Specs_Range.Start_Line :=
                    Before_First_Param_Spec.Sloc_Range.End_Line;
                  Param_Specs_Range.Start_Column :=
                    Before_First_Param_Spec.Sloc_Range.End_Column;
                  Param_Specs_Range.End_Line :=
                    Last_Param_Spec.Sloc_Range.End_Line;
                  Param_Specs_Range.End_Column :=
                    Last_Param_Spec.Sloc_Range.End_Column;

                  Add_To_Slocs (Param_Specs_Range);

                  Param_Specs_Range := No_Source_Location_Range;
               else
                  Next_Is_Last := True;
               end if;

               Indices_Ranges_Index := Indices_Ranges_Index + 1;
            end if;

            Previous_Param_Spec := Param_Spec.As_Param_Spec;
            Param_Spec_Index := Param_Spec_Index + 1;
         end loop;
      end if;

      return Slocs;
   end Param_Specs_SLOC;

   ----------------------
   -- Parameters_SLOC --
   ----------------------

   function Parameters_SLOC
     (Subp                     : Basic_Decl'Class;
      Parameter_Indices_Ranges : Parameter_Indices_Ranges_Type)
      return Source_Location_Range_Set
   is
      Slocs : Source_Location_Range_Set;

      Subp_Params : constant  Params := Get_Subp_Params (Subp);

      procedure Add_To_Slocs (Sloc_Range : Source_Location_Range);
      --  Adds Sloc_Range to Sloc. Does not add No_Source_Location_Range.

      procedure Process_Subp_Params (Subp_Params : Params);
      --  Finds the source location range of the parameters with index given
      --  by 'Parameter_Indices_Ranges', and adds them to 'Slocs'.

      ------------------
      -- Add_To_Slocs --
      ------------------

      procedure Add_To_Slocs (Sloc_Range : Source_Location_Range) is
      begin
         if Sloc_Range = No_Source_Location_Range then
            return;
         end if;

         Slocs.Insert (Sloc_Range);
      end Add_To_Slocs;

      ------------------
      -- Process_Subp --
      ------------------

      procedure Process_Subp_Params (Subp_Params : Params) is
         Indices_Index        : Positive := 1;
         Param_Spec_Index     : Positive := 1;
         Param_Spec_Length    : Positive;
         Relative_Index       : Positive;
         Absolute_Index       : Positive;
         First_Relative_Index : Natural := 0;
         First_Param          : Defining_Name := No_Defining_Name;
         Before_First_Param   : Defining_Name := No_Defining_Name;
         Update_First_Param   : Boolean := False;
         Last_Relative_Index  : Natural := 0;
         Last_Param           : Defining_Name := No_Defining_Name;
         Last_Param_Aux       : Defining_Name := No_Defining_Name;
         Next_Is_Last         : Boolean := False;
         Params_Sloc_Range    : Source_Location_Range :=
           No_Source_Location_Range;

         package Positive_Vectors is new Ada.Containers.Vectors
           (Index_Type   => Positive,
            Element_Type => Positive,
            "="          => "=");

         use Positive_Vectors;

         Param_Spec_Indices : Vector;

         use type Ada.Containers.Count_Type;

      begin
         Absolute_Index := 1;
         for Param_Spec of Subp_Params.F_Params loop
            Param_Spec_Length := Length (Param_Spec.F_Ids);
            Relative_Index := 1;

            for Param of Param_Spec.F_Ids loop
               if Next_Is_Last then
                  Last_Param := Param.As_Defining_Name;
                  Params_Sloc_Range.Start_Line :=
                    First_Param.Sloc_Range.Start_Line;
                  Params_Sloc_Range.Start_Column :=
                    First_Param.Sloc_Range.Start_Column;
                  Params_Sloc_Range.End_Line :=
                    Last_Param.Sloc_Range.Start_Line;
                  Params_Sloc_Range.End_Column :=
                    Last_Param.Sloc_Range.Start_Column;
                  Add_To_Slocs (Params_Sloc_Range);
                  Params_Sloc_Range := No_Source_Location_Range;
                  First_Param := No_Defining_Name;
                  Last_Param := No_Defining_Name;
                  Next_Is_Last := False;
               end if;

               if Indices_Index > Parameter_Indices_Ranges'Length then
                  exit;
               end if;

               if Update_First_Param
                 or else Absolute_Index =
                   Parameter_Indices_Ranges (Indices_Index).First
               then
                  First_Relative_Index := Relative_Index;
                  First_Param := Param.As_Defining_Name;

                  if Relative_Index /= 1 then
                     Before_First_Param := Last_Param_Aux.As_Defining_Name;
                  end if;

                  if Update_First_Param then
                     Update_First_Param := False;
                  end if;
               end if;

               if Absolute_Index =
                 Parameter_Indices_Ranges (Indices_Index).Last
               then
                  Last_Relative_Index := Relative_Index;
                  Last_Param := Param.As_Defining_Name;

                  if First_Relative_Index = 1 then
                     if Last_Relative_Index = Param_Spec_Length then
                        Param_Spec_Indices.Append (Param_Spec_Index);

                     else
                        Next_Is_Last := True;
                     end if;

                  else
                     Params_Sloc_Range.Start_Line :=
                       Before_First_Param.Sloc_Range.End_Line;
                     Params_Sloc_Range.Start_Column :=
                       Before_First_Param.Sloc_Range.End_Column;
                     Params_Sloc_Range.End_Line :=
                       Last_Param.Sloc_Range.End_Line;
                     Params_Sloc_Range.End_Column :=
                       Last_Param.Sloc_Range.End_Column;
                     Add_To_Slocs (Params_Sloc_Range);
                     Params_Sloc_Range := No_Source_Location_Range;
                  end if;

                  Indices_Index := Indices_Index + 1;
                  First_Relative_Index := 0;
                  Last_Relative_Index := 0;
                  if not Next_Is_Last then
                     First_Param := No_Defining_Name;
                     Last_Param := No_Defining_Name;
                  end if;
               end if;

               Relative_Index := Relative_Index + 1;
               Absolute_Index := Absolute_Index + 1;

               Last_Param_Aux := Param.As_Defining_Name;
            end loop;

            if First_Relative_Index /= 0 then
               Update_First_Param := True;

               if First_Relative_Index = 1 then
                  Param_Spec_Indices.Append (Param_Spec_Index);

               else
                  Params_Sloc_Range.Start_Line :=
                    Before_First_Param.Sloc_Range.End_Line;
                  Params_Sloc_Range.Start_Column :=
                    Before_First_Param.Sloc_Range.End_Column;
                  Params_Sloc_Range.End_Line :=
                    Last_Param_Aux.Sloc_Range.End_Line;
                  Params_Sloc_Range.End_Column :=
                    Last_Param_Aux.Sloc_Range.End_Column;
                  Add_To_Slocs (Params_Sloc_Range);
                  Params_Sloc_Range := No_Source_Location_Range;
               end if;
            end if;

            Param_Spec_Index := Param_Spec_Index + 1;
         end loop;

         if Param_Spec_Indices.Length /= 0 then
            declare
               Param_Spec_Indices_Ranges : Param_Spec_Indices_Ranges_Type
                 (1 .. Positive (Param_Spec_Indices.Length));
               This_Indices_Range : Param_Spec_Indices_Range_Type;
               Real_Length : Positive := 1;
               Update_First_Index : Boolean := True;
               Last_Index : Positive := 1;  -- Default value does not matter

            begin
               for Index of Param_Spec_Indices loop
                  if Update_First_Index then
                     This_Indices_Range.First := Index;
                     Update_First_Index := False;

                  else
                     if Index /= Last_Index + 1 then
                        This_Indices_Range.Last := Last_Index;
                        Param_Spec_Indices_Ranges (Real_Length) :=
                          This_Indices_Range;
                        Real_Length := Real_Length + 1;
                        This_Indices_Range.First := Index;
                     end if;
                  end if;

                  Last_Index := Index;
               end loop;

               This_Indices_Range.Last := Last_Index;
               Param_Spec_Indices_Ranges (Real_Length) := This_Indices_Range;

               for Sloc of
                 Param_Specs_SLOC
                   (Subp_Params.P_Semantic_Parent.As_Basic_Decl,
                    Param_Spec_Indices_Ranges (1 .. Real_Length))
               loop
                  Slocs.Insert (Sloc);
               end loop;
            end;
         end if;
      end Process_Subp_Params;

   begin
      Process_Subp_Params (Subp_Params);

      return Slocs;
   end Parameters_SLOC;

   ----------------------
   -- To_Unique_Ranges --
   ----------------------

   function To_Unique_Ranges
     (Parameter_Indices : Parameter_Indices_Type)
         return Parameter_Indices_Ranges_Type
   is
      Unique_Indices : constant Parameter_Indices_Type :=
        Unique (Parameter_Indices);
      Max_Length     : constant Natural := Unique_Indices'Length;
      Unique_Ranges  : Parameter_Indices_Ranges_Type (1 .. Max_Length);
      Real_Length    : Positive := 1;
      Last_Index     : Positive;

   begin
      if Max_Length = 0 then
         return [];
      end if;

      --  Start by transforming 'Sorted_Indices' in 'Indices_Ranges'.
      --
      --  Example 1: If 'Sorted_Indices' is [1, 3, 5] then Max_Length = 3,
      --  Indices_Ranges = [{1, 1}, {3, 3}, {5, 5}] and Real_Length = 3.
      --
      --  Example 2: If 'Sorted_Indices' is [1, 2, 3] then Max_Length = 3,
      --  Indices_Ranges = [{1, 3}] and Real_Length = 1.
      --
      --  Then dispatch to an 'Arguments_SLOC' function that receives a
      --  'Parameter_Indices_Ranges_Type'.

      if Max_Length = 1 then
         Real_Length := 1;
         Unique_Ranges (1).First := Unique_Indices (Unique_Indices'First);
         Unique_Ranges (1).Last := Unique_Indices (Unique_Indices'First);

      else
         Unique_Ranges (Real_Length).First := Unique_Indices (1);
         Last_Index := Unique_Indices (1);

         for Index of Unique_Indices (2 .. Max_Length) loop
            if Index > Last_Index + 1 then
               Unique_Ranges (Real_Length).Last := Last_Index;
               Real_Length := Real_Length + 1;
               Unique_Ranges (Real_Length).First := Index;
            end if;

            Last_Index := Index;
         end loop;

         Unique_Ranges (Real_Length).Last := Last_Index;
      end if;

      return Unique_Ranges (1 .. Real_Length);
   end To_Unique_Ranges;

   --------------------------------
   -- Is_Add_Parameter_Available --
   --------------------------------

   function Is_Add_Parameter_Available
     (Unit                        : Analysis_Unit;
      Location                    : Source_Location;
      Requires_Full_Specification : out Boolean)
      return Boolean
   is
      Aux_Node : Ada_Node :=
        (if Unit /= No_Analysis_Unit
           and then Location /= No_Source_Location
         then
            Unit.Root.Lookup (Location)
         else
            No_Ada_Node);

   begin
      if Aux_Node.Is_Null then
         return False;
      end if;

      if Aux_Node.Kind in Ada_Subp_Decl_Range
        and then Is_Subprogram (Aux_Node.As_Basic_Decl)
      then
         Aux_Node := Get_Subp_Spec (Aux_Node.As_Basic_Decl).As_Ada_Node;

      else
         while not Aux_Node.Is_Null loop
            exit when Aux_Node.Kind in Ada_Subp_Spec_Range;

            Aux_Node := Aux_Node.Parent;
         end loop;
      end if;

      if Aux_Node.Is_Null then
         return False;
      end if;

      Requires_Full_Specification :=
        Aux_Node.As_Subp_Spec.F_Subp_Params.Is_Null;

      return True;

   exception
      when E : others =>
         Refactor_Trace.Trace
           (E,
            Is_Refactoring_Tool_Available_Default_Error_Message
              ("Add Parameter"));
         return False;
   end Is_Add_Parameter_Available;

   ------------------------------
   -- Is_Change_Mode_Available --
   ------------------------------

   function Is_Change_Mode_Available
     (Node                    : Ada_Node'Class;
      Subp                    : out Basic_Decl;
      Parameter_Indices_Range : out Parameter_Indices_Range_Type;
      Mode_Alternatives       : out Mode_Alternatives_Type)
      return Boolean
   is
      procedure Initialize_Out_Parameters;
      --  Initiazes the out parameters of this function with values as if
      --  the refactoring is not available.

      -------------------------------
      -- Initialize_Out_Parameters --
      -------------------------------

      procedure Initialize_Out_Parameters is
      begin
         Subp := No_Basic_Decl;
         Parameter_Indices_Range := (1, 1);
         Mode_Alternatives :=
           [Ada_Mode_Default, Ada_Mode_Default, Ada_Mode_Default];
      end Initialize_Out_Parameters;

      Parent_Defining_Name       : Defining_Name := No_Defining_Name;
      Parent_Subptype_Indication : Subtype_Indication := No_Subtype_Indication;
      Parent_Defining_Name_List  : Defining_Name_List := No_Defining_Name_List;
      Parent_Param_Spec          : Param_Spec := No_Param_Spec;
      Parent_Param_Spec_List     : Param_Spec_List := No_Param_Spec_List;
      Parent_Subp_Decl           : Basic_Decl := No_Basic_Decl;

      Parameter_Absolute_Index : Positive;

      Aux_Node : Ada_Node := Node.As_Ada_Node;

   begin
      Initialize_Out_Parameters;

      Find_Subp :
      while not Aux_Node.Is_Null loop
         exit Find_Subp when Aux_Node.Kind in Ada_Subp_Spec_Range;

         case Aux_Node.Kind is
            when Ada_Defining_Name =>
               Parent_Defining_Name := Aux_Node.As_Defining_Name;

            when Ada_Subtype_Indication =>
               Parent_Subptype_Indication := Aux_Node.As_Subtype_Indication;

            when Ada_Defining_Name_List =>
               Parent_Defining_Name_List := Aux_Node.As_Defining_Name_List;

            when Ada_Param_Spec =>
               Parent_Param_Spec := Aux_Node.As_Param_Spec;

            when Ada_Param_Spec_List =>
               Parent_Param_Spec_List := Aux_Node.As_Param_Spec_List;

            when others => null;
         end case;

         Aux_Node := Aux_Node.Parent;
      end loop Find_Subp;

      if Aux_Node.Is_Null then
         return False;
      end if;

      Parent_Subp_Decl := Aux_Node.As_Subp_Spec.P_Parent_Basic_Decl;

      if not Is_Subprogram (Parent_Subp_Decl) then
         return False;
      end if;

      if Parent_Defining_Name_List /= No_Defining_Name_List
        and then Parent_Defining_Name /= No_Defining_Name
      then
         Parameter_Absolute_Index :=
           Get_Parameter_Absolute_Index (Parent_Defining_Name);

         Subp := Parent_Subp_Decl;
         Parameter_Indices_Range.First := Parameter_Absolute_Index;
         Parameter_Indices_Range.Last := Parameter_Absolute_Index;
         Mode_Alternatives :=
           Mode_Alternatives_Map (Parent_Param_Spec.F_Mode.Kind);

         return True;

      elsif Parent_Subptype_Indication /= No_Subtype_Indication then
         Subp := Parent_Subp_Decl;

         Parameter_Absolute_Index := 1;

         for Param_Spec of Parent_Param_Spec_List loop
            if Param_Spec = Parent_Param_Spec then
               Parameter_Indices_Range.First := Parameter_Absolute_Index;
               Parameter_Indices_Range.Last :=
                 Parameter_Absolute_Index + Length (Param_Spec.F_Ids) - 1;

            else
               Parameter_Absolute_Index :=
                 Parameter_Absolute_Index + Length (Param_Spec.F_Ids);
            end if;
         end loop;

         Mode_Alternatives :=
           Mode_Alternatives_Map (Parent_Param_Spec.F_Mode.Kind);

         return True;

      else
         return False;
      end if;

   exception
      when E : others =>
         --  Assume that exceptions can be raised due to invalid code and
         --  set again the out parameters since they might have been changed
         --  since initialized.
         Initialize_Out_Parameters;
         Refactor_Trace.Trace
           (E,
            Is_Refactoring_Tool_Available_Default_Error_Message
              ("Change Parameter Mode"));
         return False;
   end Is_Change_Mode_Available;

   ---------------------------------
   -- Is_Move_Parameter_Available --
   ---------------------------------

   function Is_Move_Parameter_Available
     (Node            : Ada_Node'Class;
      Subp            : out Basic_Decl;
      Parameter_Index : out Positive;
      Move_Directions : out Move_Direction_Availability_Type)
      return Boolean
   is
      procedure Initialize_Out_Parameters;
      --  Initiazes the out parameters of this function with values as if
      --  the refactoring is not available.

      -------------------------------
      -- Initialize_Out_Parameters --
      -------------------------------

      procedure Initialize_Out_Parameters is
      begin
         Subp := No_Basic_Decl;
         Parameter_Index := 1;
         Move_Directions := [False, False];
      end Initialize_Out_Parameters;

      --  Aux_Node must have the following parent nodes in order to guarantee
      --  that Aux_Node is related to a parameter.
      Parent_Defining_Name       : Defining_Name := No_Defining_Name;
      Parent_Defining_Name_List  : Defining_Name_List := No_Defining_Name_List;
      Parent_Param_Spec          : Param_Spec := No_Param_Spec;

      --  Number of parameters the target subprogram has
      Total_Parameters         : Positive;

      --  Auxiliary node to climb the tree
      Aux_Node : Ada_Node := Node.As_Ada_Node;

   begin
      Initialize_Out_Parameters;

      Find_Subp :
      while not Aux_Node.Is_Null loop
         exit Find_Subp when Aux_Node.Kind in Ada_Subp_Spec_Range;

         case Aux_Node.Kind is
            when Ada_Defining_Name =>
               Parent_Defining_Name := Aux_Node.As_Defining_Name;

            when Ada_Defining_Name_List =>
               Parent_Defining_Name_List := Aux_Node.As_Defining_Name_List;

            when Ada_Param_Spec =>
               Parent_Param_Spec := Aux_Node.As_Param_Spec;

            when others => null;
         end case;

         Aux_Node := Aux_Node.Parent;
      end loop Find_Subp;

      if Aux_Node.Is_Null
        or else Parent_Defining_Name = No_Defining_Name
        or else Parent_Defining_Name_List = No_Defining_Name_List
        or else Parent_Param_Spec = No_Param_Spec
      then
         return False;
      end if;

      Subp := Aux_Node.As_Subp_Spec.P_Parent_Basic_Decl;

      Parameter_Index :=
        Get_Parameter_Absolute_Index (Parent_Defining_Name);

      Total_Parameters :=
        Count_Subp_Parameters (Get_Subp_Params (Subp));

      --  If the target subprogram only has one parameter, then it can't be
      --  moved.

      if Total_Parameters = 1 then
         return False;
      end if;

      Assert (Total_Parameters >= Parameter_Index);

      --  If the target parameter is the first one, then it can only be moved
      --  forward. If it is the last one, then it can only be moved backward.
      --  Otherwise, both directions are valid.

      if Parameter_Index = 1 then
         Move_Directions := Only_Forward;

      elsif Parameter_Index = Total_Parameters then
         Move_Directions := Only_Backward;

      else
         Move_Directions := Both_Directions;
      end if;

      return True;

   exception
      when E : others =>
         --  Assume that exceptions can be raised due to invalid code and
         --  set again the out parameters since they might have been changed
         --  since initialized.
         Initialize_Out_Parameters;
         Refactor_Trace.Trace
           (E,
            Is_Refactoring_Tool_Available_Default_Error_Message
              ("Move Parameter"));
         return False;
   end Is_Move_Parameter_Available;

   -----------------
   -- Change_Mode --
   -----------------

   function Change_Mode
     (Subp                    : Basic_Decl;
      Parameter_Indices_Range : Parameter_Indices_Range_Type;
      New_Mode                : Ada_Mode;
      Units                   : Analysis_Unit_Array)
      return Text_Edit_Map
   is
      Edits : Text_Edit_Map;

      procedure Change_Mode_Callback (Relative_Subp : Basic_Decl'Class);
      --  Determines the necessary changes to 'Relative_Subp' specification,
      --  and adds them to 'Edits'.

      --------------------------
      -- Change_Mode_Callback --
      --------------------------

      procedure Change_Mode_Callback (Relative_Subp : Basic_Decl'Class) is
         Relative_Subp_Body : constant Base_Subp_Body :=
           Find_Subp_Body (Relative_Subp);

      begin
         if Is_Subprogram (Relative_Subp) then
            Change_Mode
              (Relative_Subp,
               Parameter_Indices_Range,
               New_Mode,
               Edits);

            if not Relative_Subp_Body.Is_Null then
               Change_Mode
                 (Relative_Subp_Body,
                  Parameter_Indices_Range,
                  New_Mode,
                  Edits);
            end if;
         end if;
      end Change_Mode_Callback;

   begin
      Find_Subp_Relatives
        (Subp           => Subp,
         Units          => Units,
         Decls_Callback => Change_Mode_Callback'Access);

      return Edits;
   end Change_Mode;

   -------------------
   -- Move_Backward --
   -------------------

   function Move_Backward
     (Subp            : Basic_Decl;
      Parameter_Index : Positive;
      Units           : Analysis_Unit_Array)
      return Text_Edit_Map
   is
      Edits : Text_Edit_Map;

      procedure Move_Parameter_Callback (Relative_Subp : Basic_Decl'Class);
      --  Callback that adds to Edits the necessary edits to 'Relative_Subp'
      --  specification.

      procedure Move_Arguments_Callback
        (Call_Identifier : Base_Id'Class;
         Kind            : Ref_Result_Kind;
         Cancel          : in out Boolean);
      --  Callback that adds to Edits the necessary edits to 'Call'

      -----------------------------
      -- Move_Parameter_Callback --
      -----------------------------

      procedure Move_Parameter_Callback (Relative_Subp : Basic_Decl'Class) is
         Relative_Subp_Body : constant Base_Subp_Body :=
           Find_Subp_Body (Relative_Subp);

      begin
         if Is_Subprogram (Relative_Subp) then
            Move_Backward
              (Relative_Subp, Parameter_Index, Edits);

            if not Relative_Subp_Body.Is_Null then
               Move_Backward
                 (Relative_Subp_Body, Parameter_Index, Edits);
            end if;
         end if;
      end Move_Parameter_Callback;

      -----------------------------
      -- Move_Arguments_Callback --
      -----------------------------

      procedure Move_Arguments_Callback
        (Call_Identifier : Base_Id'Class;
         Kind            : Ref_Result_Kind;
         Cancel          : in out Boolean)
      is
         pragma Unreferenced (Kind);

         Call_Expression : Call_Expr;

         function Has_Parameter_Association return Boolean;
         --  Returns True if 'Call_Expression' has a parameter association

         procedure Process_Dot_Call;
         --  Processes calls with the dot notation

         procedure Process_Non_Dot_Call (Target_Index : Positive);
         --  PRocesses calls without the dot notation

         -------------------------------
         -- Has_Parameter_Association --
         -------------------------------

         function Has_Parameter_Association return Boolean
         is
            Has_Designator : Boolean;

         begin
            for Param_Assoc of Call_Expression.F_Suffix.As_Assoc_List loop
               Has_Designator :=
                 not Param_Assoc.As_Param_Assoc.F_Designator.Is_Null;

               --  We just need to check the first argument. If it has a
               --  designator, then all the other ones must also have.

               exit;
            end loop;

            return Has_Designator;
         end Has_Parameter_Association;

         ----------------------
         -- Process_Dot_Call --
         ----------------------

         procedure Process_Dot_Call is
            Edits_Set : Text_Edit_Ordered_Set;

            Object_Name : Base_Id := No_Base_Id;
            Call_Name   : Base_Id := No_Base_Id;

            Object_Name_Sloc : Source_Location_Range :=
              No_Source_Location_Range;
            Call_Name_Sloc : Source_Location_Range :=
              No_Source_Location_Range;

            Object_Name_Text : Unbounded_String := Null_Unbounded_String;

            Delition_Sloc : Source_Location_Range :=
              No_Source_Location_Range;

            Insertion_Sloc : Source_Location_Range :=
              No_Source_Location_Range;

            First_Parameter_Name : Unbounded_String := Null_Unbounded_String;

            New_Text : Unbounded_String := Null_Unbounded_String;

         begin
            if Parameter_Index = 2 then
               Call_Name :=
                 Call_Expression.F_Name.As_Dotted_Name.F_Suffix;

               if Call_Expression.F_Name.As_Dotted_Name.F_Prefix.Kind =
                 Ada_Dotted_Name
               then
                  Object_Name :=
                    Call_Expression.F_Name.
                      As_Dotted_Name.F_Prefix.As_Dotted_Name.F_Suffix;

               else
                  Object_Name :=
                    Call_Expression.F_Name.
                      As_Dotted_Name.F_Prefix.As_Base_Id;
               end if;

               Object_Name_Sloc := Object_Name.Sloc_Range;
               Call_Name_Sloc := Call_Name.Sloc_Range;

               Object_Name_Text := To_Unbounded_String
                 (Ada.Characters.Conversions.To_String
                    (Object_Name.Text));

               for Param_Assoc of
                 Call_Expression.F_Suffix.As_Assoc_List
               loop
                  Insertion_Sloc := Source_Location_Range'
                    (Param_Assoc.Sloc_Range.End_Line,
                     Param_Assoc.Sloc_Range.End_Line,
                     Param_Assoc.Sloc_Range.End_Column,
                     Param_Assoc.Sloc_Range.End_Column);

                  exit;
               end loop;

               if Has_Parameter_Association then
                  First_Parameter_Name := To_Unbounded_String
                    (Ada.Characters.Conversions.To_String
                       (Get_Parameter_Name (Subp, 1)));

                  New_Text := ", "
                    & First_Parameter_Name
                    & " => "
                    & Object_Name_Text;
               else
                  New_Text := ", " & Object_Name_Text;
               end if;

               Delition_Sloc := Source_Location_Range'
                 (Object_Name_Sloc.Start_Line,
                  Call_Name_Sloc.Start_Line,
                  Object_Name_Sloc.Start_Column,
                  Call_Name_Sloc.Start_Column);

               Edits_Set.Insert
                 ((Delition_Sloc, Null_Unbounded_String));
               Edits_Set.Insert
                 ((Insertion_Sloc, New_Text));

            elsif not Has_Parameter_Association then
               Process_Non_Dot_Call (Parameter_Index - 1);
            end if;
         end Process_Dot_Call;

         --------------------------
         -- Process_Non_Dot_Call --
         --------------------------

         procedure Process_Non_Dot_Call (Target_Index : Positive) is
            Arg_A : Param_Assoc := No_Param_Assoc;
            Arg_B : Param_Assoc := No_Param_Assoc;

            Arg_A_Sloc : Source_Location_Range := No_Source_Location_Range;
            Arg_B_Sloc : Source_Location_Range := No_Source_Location_Range;

            Arg_A_Text : Unbounded_String := Null_Unbounded_String;
            Arg_B_Text : Unbounded_String := Null_Unbounded_String;

            Param_Assoc_Index : Positive := 1;

            Edits_Set : Text_Edit_Ordered_Set;

         begin
            for Param_Assoc of Call_Expression.F_Suffix.As_Assoc_List loop
               if Param_Assoc_Index = Target_Index - 1 then
                  Arg_A := Param_Assoc.As_Param_Assoc;

               elsif Param_Assoc_Index = Target_Index then
                  Arg_B := Param_Assoc.As_Param_Assoc;

                  exit;
               end if;

               Param_Assoc_Index := Param_Assoc_Index + 1;
            end loop;

            --  Assert that Arg_A and Arg_B have been found and have changed
            Assert (Arg_A /= No_Param_Assoc and Arg_B /= No_Param_Assoc);

            Arg_A_Sloc := Arg_A.Sloc_Range;
            Arg_B_Sloc := Arg_B.Sloc_Range;

            Arg_A_Text := To_Unbounded_String
              (Ada.Characters.Conversions.To_String (Arg_A.Text));
            Arg_B_Text := To_Unbounded_String
              (Ada.Characters.Conversions.To_String (Arg_B.Text));

            Edits_Set.Insert ((Arg_B_Sloc, Arg_A_Text));
            Edits_Set.Insert ((Arg_A_Sloc, Arg_B_Text));

            for Edit of Edits_Set loop
               Safe_Insert
                 (Edits,
                  Call_Expression.Unit.Get_Filename,
                  (Edit.Location, Edit.Text));
            end loop;
         end Process_Non_Dot_Call;

      begin
         Cancel := False;

         if Call_Identifier.Is_Null
           or else Call_Identifier.Parent.Is_Null
           or else Call_Identifier.Parent.Parent.Is_Null
         then
            return;
         end if;

         if Call_Identifier.Parent.Kind in Ada_Dotted_Name then
            if Call_Identifier.Parent.Parent.Kind in Ada_Call_Expr_Range then
               Call_Expression := Call_Identifier.Parent.Parent.As_Call_Expr;
            else
               return;
            end if;
         else
            if Call_Identifier.Parent.Kind in Ada_Call_Expr_Range then
               Call_Expression := Call_Identifier.Parent.As_Call_Expr;
            else
               return;
            end if;
         end if;

         case Call_Identifier.P_Is_Dot_Call is
            when True =>
               Process_Dot_Call;

            when False =>
               if not Has_Parameter_Association then
                  Process_Non_Dot_Call (Parameter_Index);
               end if;

         end case;
      end Move_Arguments_Callback;

   begin
      Find_Subp_Relatives
        (Subp           => Subp,
         Units          => Units,
         Decls_Callback => Move_Parameter_Callback'Access,
         Find_Calls     => True,
         Calls_Callback => Move_Arguments_Callback'Access);

      return Edits;
   end Move_Backward;

   ----------------------------
   -- First_Designator_Index --
   ----------------------------

   function First_Designator_Index
     (Arguments : Assoc_List)
      return Optional_Designator_Index
   is
      Index : Positive := 1;

   begin
      if Arguments.Is_Null then
         return Optional_Designator_Index'(Exists => False);
      end if;

      for Argument of Arguments loop
         if not Argument.As_Param_Assoc.F_Designator.Is_Null then
            return Optional_Designator_Index'
              (Exists => True,
               Value  => Index);
         end if;

         Index := Index + 1;
      end loop;

      return Optional_Designator_Index'(Exists => False);
   end First_Designator_Index;

   ---------------------------------
   -- Map_Parameters_To_Arguments --
   ---------------------------------

   function Map_Parameters_To_Arguments
     (Parameters : Params'Class;
      Call       : Call_Expr'Class)
      return Extended_Argument_Indicies_Type
   is
      Parameters_Count : constant Natural :=
        Count_Subp_Parameters (Parameters.As_Params);
      Arguments        : constant Assoc_List := Call.F_Suffix.As_Assoc_List;
      Arguments_Count  : constant Natural := Length (Arguments);
      First_Designator : constant Optional_Designator_Index :=
        First_Designator_Index (Arguments);

      function Map_Parameter_To_Named_Argument
        (Parameter_Index : Positive)
         return Natural;
      --  Searches for a actual parameter on 'Call' that has the same name as
      --  the parameter with index 'Parameter_Index'. Returns the index of the
      --  actual parameter if found, otherwise, returns 0.

      function Map_Parameter_To_Named_Argument
        (Parameter_Index : Positive)
         return Natural
      is
         Parameter_Name : constant Text_Type :=
           Get_Parameter_Name (Parameters, Parameter_Index);
         Argument_Index : Positive := 1;

      begin
         for Argument of Arguments loop
            --  FIXME: Do a case insensitive comparison
            if not Argument.As_Param_Assoc.F_Designator.Is_Null
              and then Argument.As_Param_Assoc.F_Designator.Text
                = Parameter_Name
            then
               return Argument_Index;
            end if;

            Argument_Index := Argument_Index + 1;
         end loop;

         return 0;
      end Map_Parameter_To_Named_Argument;

      Map : Extended_Argument_Indicies_Type (1 .. Parameters_Count) :=
        [others => 0];

   begin
      Assert
        (Parameters_Count /= 0
         and then Arguments_Count /= 0
         and then Parameters_Count >= Arguments_Count);

      if First_Designator.Exists then
         Assert (Arguments_Count >= First_Designator.Value);

         for J in 1 .. First_Designator.Value - 1 loop
            Map (J) := J;
         end loop;

         for J in First_Designator.Value .. Parameters_Count loop
            Map (J) := Map_Parameter_To_Named_Argument (J);
         end loop;

      else
         for J in 1 .. Arguments_Count loop
            Map (J) := J;
         end loop;
      end if;

      return Map;
   end Map_Parameters_To_Arguments;

   function Get_Parameter_Relative_Position
     (Subp_Spec : Libadalang.Analysis.Subp_Spec;
      Location  : Source_Location)
      return Parameter_Relative_Position_Type;
   --  Returns the relative position of Location in relation to a parameter in
   --  Subp_Spec.
   --
   --  Example:
   --
   --  procedure Foo (A, B : Bar; C : Baz);
   --                ||  |       |
   --                ||  |       `-> Location_0
   --                ||  `-> Location_1
   --                |`-> Location_2
   --                `->Location_3
   --  Location 0 = Before C
   --  Location 1 = After B
   --  Location 2 = Before B
   --  Location 3 = Before A
   --
   --  Note that the distintic between "Before C" and "After B" is important
   --  when adding a parameter without fully specifying it. In that case, the
   --  new parameter will have the same Param_Spec as the already existing one.
   --  Therefore, "After B" means that the new parameter will be on the
   --  "A, B : Bar" Param_Spec, after parameter B. "After C" means that the
   --  new parameter will be on the "C : Baz" Param_Spec, before parameter C.

   -------------------------------------
   -- Get_Parameter_Relative_Position --
   -------------------------------------

   function Get_Parameter_Relative_Position
     (Subp_Spec : Libadalang.Analysis.Subp_Spec;
      Location  : Source_Location)
      return Parameter_Relative_Position_Type
   is
      Subp_Params : constant Params := Subp_Spec.F_Subp_Params;

      Total_Param_Specs           : constant Natural :=
        Count_Subp_Param_Specs (Subp_Params);
      Total_Param_Spec_Parameters : Natural;

      Is_First_Param_Spec : Boolean := True;
      Is_Last_Param_Spec  : Boolean;

      Is_First_Parameter : Boolean := True;
      Is_Last_Parameter  : Boolean;

      Previous_Param_Spec : Param_Spec := No_Param_Spec;
      Previous_Parameter  : Defining_Name := No_Defining_Name;

      Param_Spec_Idx           : Positive := 1;
      Param_Spec_Parameter_Idx : Positive := 1;
      Parameter_Idx            : Positive := 1;

   begin
      if Total_Param_Specs = 0 then
         return (Before, 1);
      end if;

      Foo :
      for Param_Spec of Subp_Params.F_Params loop
         Is_Last_Param_Spec := Param_Spec_Idx = Total_Param_Specs;
         Is_First_Parameter := True;
         Total_Param_Spec_Parameters :=
           Count_Param_Spec_Parameters (Param_Spec.As_Param_Spec);
         Param_Spec_Parameter_Idx := 1;

         for Parameter of Param_Spec.F_Ids loop
            Is_Last_Parameter :=
              Param_Spec_Parameter_Idx = Total_Param_Spec_Parameters;

            if Is_First_Parameter
              and then Is_Last_Parameter
              and then Is_First_Param_Spec
              and then Is_Last_Param_Spec
            then
               declare
                  Tail   : constant Source_Location :=
                    Start_Sloc (Subp_Spec.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Subp_Spec.Sloc_Range);
               begin
                  Assert
                    (Parameter_Idx = 1,
                     "First parameter does not have index 1");
                  if Location < Middle then
                     Assert (Location >= Tail);
                     return (Before, Parameter_Idx);
                  else
                     Assert (Location <= Trail);
                     return (After, Parameter_Idx);
                  end if;
               end;

            elsif Is_First_Parameter
              and then Is_Last_Parameter
              and then Is_First_Param_Spec
              and then not Is_Last_Param_Spec
            then
               declare
                  Tail   : constant Source_Location :=
                    Start_Sloc (Subp_Spec.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Param_Spec.Sloc_Range);

               begin
                  Assert (Parameter_Idx = 1);

                  if Location >= Tail and then Location < Middle then
                     return (Before, Parameter_Idx);
                  elsif Location >= Middle and Location <= Trail then
                     return (After, Parameter_Idx);
                  end if;
               end;

            elsif Is_First_Parameter
              and then Is_Last_Parameter
              and then not Is_First_Param_Spec
              and then not Is_Last_Param_Spec
            then
               Assert (not Previous_Param_Spec.Is_Null);

               declare
                  Tail   : constant Source_Location :=
                    End_Sloc (Previous_Param_Spec.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Param_Spec.Sloc_Range);

               begin
                  Assert (Parameter_Idx > 1);

                  if Location >= Tail and then Location < Middle then
                     return (Before, Parameter_Idx);
                  elsif Location >= Middle and Location <= Trail then
                     return (After, Parameter_Idx);
                  end if;
               end;

            elsif Is_First_Parameter
              and then Is_Last_Parameter
              and then not Is_First_Param_Spec
              and then Is_Last_Param_Spec
            then
               Assert (not Previous_Param_Spec.Is_Null);

               declare
                  Tail   : constant Source_Location :=
                    End_Sloc (Previous_Param_Spec.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Subp_Spec.Sloc_Range);

               begin
                  Assert (Parameter_Idx > 1);

                  if Location >= Tail and then Location < Middle then
                     return (Before, Parameter_Idx);
                  elsif Location >= Middle and Location <= Trail then
                     return (After, Parameter_Idx);
                  end if;
               end;

            elsif Is_First_Parameter
              and then not Is_Last_Parameter
              and then Is_First_Param_Spec
            then
               Assert
                 (Parameter_Idx = 1,
                  "First parameter does not have index 1");

               declare
                  Tail   : constant Source_Location :=
                    Start_Sloc (Subp_Spec.Sloc_Range);
                  Trail  : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);

               begin
                  Assert (Location >= Tail);
                  if Location < Trail then
                     return (Before, Parameter_Idx);
                  end if;
               end;

            elsif not Is_First_Parameter
              and then Is_Last_Parameter
              and then not Is_Last_Param_Spec
            then
               Assert (not Previous_Parameter.Is_Null);
               Assert (Parameter_Idx > 1);

               declare
                  Tail   : constant Source_Location :=
                    Start_Sloc (Previous_Parameter.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Param_Spec.Sloc_Range);

               begin
                  if Location >= Tail and then Location < Middle then
                     return (Before, Parameter_Idx);
                  elsif Location >= Middle and Location <= Trail then
                     return (After, Parameter_Idx);
                  end if;
               end;

            elsif Is_First_Parameter
              and then not Is_Last_Parameter
              and then not Is_First_Param_Spec
            then
               Assert (not Previous_Param_Spec.Is_Null);

               declare
                  Tail  : constant Source_Location :=
                    End_Sloc (Previous_Param_Spec.Sloc_Range);
                  Trail : constant Source_Location :=
                    Start_Sloc (Param_Spec.Sloc_Range);

               begin
                  Assert (Parameter_Idx > 1);

                  if Location >= Tail and then Location < Trail then
                     return (Before, Parameter_Idx);
                  end if;
               end;

            elsif not Is_First_Parameter
              and then not Is_Last_Parameter
            then
               Assert (not Previous_Parameter.Is_Null);

               declare
                  Tail  : constant Source_Location :=
                    Start_Sloc (Previous_Parameter.Sloc_Range);
                  Trail : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);

               begin
                  if Location >= Tail and then Location < Trail then
                     return (Before, Parameter_Idx);
                  end if;
               end;

            elsif not Is_First_Parameter
              and then Is_Last_Parameter
              and then Is_Last_Param_Spec
            then
               Assert (not Previous_Parameter.Is_Null);

               declare
                  Tail   : constant Source_Location :=
                    Start_Sloc (Previous_Parameter.Sloc_Range);
                  Middle : constant Source_Location :=
                    Start_Sloc (Parameter.Sloc_Range);
                  Trail  : constant Source_Location :=
                    End_Sloc (Subp_Spec.Sloc_Range);

               begin
                  Assert (Parameter_Idx > 1);

                  if Location >= Tail and then Location < Middle then
                     return (Before, Parameter_Idx);
                  elsif Location >= Middle and Location <= Trail then
                     return (After, Parameter_Idx);
                  end if;
               end;

            end if;

            Previous_Parameter := Parameter.As_Defining_Name;
            Param_Spec_Parameter_Idx := Param_Spec_Parameter_Idx + 1;
            Parameter_Idx := Parameter_Idx + 1;
            Is_First_Parameter := False;
         end loop;

         Previous_Param_Spec := Param_Spec.As_Param_Spec;
         Param_Spec_Idx := Param_Spec_Idx + 1;
         Is_First_Param_Spec := False;
      end loop Foo;

      raise Program_Error;
   end Get_Parameter_Relative_Position;

   ------------
   -- Create --
   ------------

   function Create
     (Unit          : Analysis_Unit;
      Location      : Source_Location;
      New_Parameter : Unbounded_String)
      return Parameter_Adder
   is
      Is_Defining_Id      : constant Boolean :=
        Validate_Syntax (New_Parameter, Defining_Id_Rule);
      Is_Defining_Id_List : constant Boolean :=
        Validate_Syntax (New_Parameter, Defining_Id_List_Rule);
      Is_Param_Spec       : constant Boolean :=
        Validate_Syntax (New_Parameter, Param_Spec_Rule);

      Is_Valid : constant Boolean :=
        Is_Defining_Id or else Is_Defining_Id_List or else Is_Param_Spec;

      Aux_Node : Ada_Node :=
        (if Location /= No_Source_Location
         and then Unit /= No_Analysis_Unit
         then
            Unit.Root.Lookup (Location)
         else
            No_Ada_Node);

      Subp_Spec : Libadalang.Analysis.Subp_Spec := No_Subp_Spec;

   begin
      if Aux_Node.Is_Null or not Is_Valid then
         raise Program_Error;
      end if;

      if not (Aux_Node.Kind in Ada_Basic_Decl
              and then (Aux_Node.Kind in Ada_Generic_Subp_Decl_Range
                        or else Aux_Node.As_Basic_Decl.P_Is_Subprogram))
      then
         while not Aux_Node.Is_Null loop

            exit when Aux_Node.Kind in Ada_Subp_Spec_Range;

            Aux_Node := Aux_Node.Parent;
         end loop;
      end if;

      if Aux_Node.Is_Null then
         raise Program_Error;
      end if;

      Subp_Spec := (if Aux_Node.Kind in Ada_Subp_Spec_Range then
                       Aux_Node.As_Subp_Spec
                    else
                       Get_Subp_Spec (Aux_Node.As_Basic_Decl).As_Subp_Spec);

      return Parameter_Adder'
        (Spec               => Subp_Spec,
         New_Parameter      => New_Parameter,
         Relative_Position  =>
           Get_Parameter_Relative_Position (Subp_Spec, Location),
         Full_Specification => Is_Param_Spec);
   end Create;

   ------------
   -- Create --
   ------------

   function Create
     (Subp : Ada_Node;
      Info : VSS.Strings.Virtual_String) return Subprogram_Signature_Problem
   is
      (Subprogram_Signature_Problem'(Subp, Info));

   --------------
   -- Filename --
   --------------

   overriding function Filename
     (Self : Subprogram_Signature_Problem) return String
   is
     (Self.Subp.Unit.Get_Filename);

   --------------
   -- Location --
   --------------

   overriding function Location
     (Self : Subprogram_Signature_Problem) return Source_Location_Range
   is
     (Self.Subp.Sloc_Range);

   ----------
   -- Info --
   ----------

   overriding function Info
     (Self : Subprogram_Signature_Problem) return String
   is
     (VSS.Strings.Conversions.To_UTF_8_String (Self.Info));

   --------------
   -- Refactor --
   --------------

   overriding
   function Refactor
     (Self           : Parameter_Adder;
      Analysis_Units : access function return Analysis_Unit_Array)
      return Refactoring_Edits
   is
      Edits : Text_Edit_Map;

      generic
         with procedure Add_Parameter
           (Self   : Parameter_Adder;
            Target : Basic_Decl'Class;
            Edits  : in out Text_Edit_Map);
      procedure Add_Parameter_Callback (Relative_Subp : Basic_Decl'Class);
      --  Generic callback to add a parameter.
      --  Add_Parameter can be adjusted depending on where/how we're adding
      --  a parameter, for instance, if we're adding an identifier or a full
      --  parameter specification

      ----------------------------
      -- Add_Parameter_Callback --
      ----------------------------

      procedure Add_Parameter_Callback (Relative_Subp : Basic_Decl'Class)
      is
         Relative_Subp_Body : constant Base_Subp_Body :=
           Find_Subp_Body (Relative_Subp);

      begin
         if Relative_Subp.P_Is_Subprogram
           or else Relative_Subp.Kind in Ada_Generic_Subp_Decl_Range
         then
            Add_Parameter (Self, Relative_Subp, Edits);

            if not Relative_Subp_Body.Is_Null then
               Add_Parameter (Self, Relative_Subp_Body, Edits);
            end if;
         end if;
      end Add_Parameter_Callback;

      procedure Add_Full_Parameter_Specification_Callback is
        new Add_Parameter_Callback (Add_Full_Parameter_Specification);
      --  Callback to add a parameter fully specified

      procedure Add_Parameter_Defining_Id_Or_Ids_Callback is
        new Add_Parameter_Callback (Add_Parameter_Defining_Id_Or_Ids);
      --  Callback to add a parameter identifier or a list of identifiers
      Subp_Hierarchy : constant Basic_Decl_Array :=
        Get_Subp_Hierarchy
          (Subp               =>
             Self.Spec.P_Parent_Basic_Decl.P_Canonical_Part,
           Units              => Analysis_Units.all,
           Include_Base_Subps => True,
           Include_Overrides  => True);
   begin
      --  If we are trying to add a new parameter at the first position (i.e:
      --  controlling parameter) on a subprogram that is a primitive, abort the
      --  refactoring: we don't want to break the whole primitive hierarchy in
      --  that case.
      if Self.Relative_Position = (Before, 1)
        and then Subp_Hierarchy'Length > 1
      then
         return Result : Refactoring_Edits do
            Result.Diagnostics.Append
              (Subprogram_Signature_Problem'
                 (Subp => Self.Spec.P_Parent_Basic_Decl.As_Ada_Node,
                  Info => Problem_Info));
         end return;
      end if;

      if Self.Full_Specification then
         Find_Subp_Relatives
           (Subp           => Self.Spec.P_Parent_Basic_Decl,
            Units          => Analysis_Units.all,
            Decls_Callback =>
              Add_Full_Parameter_Specification_Callback'Access);
      else
         Find_Subp_Relatives
           (Subp           => Self.Spec.P_Parent_Basic_Decl,
            Units          => Analysis_Units.all,
            Decls_Callback =>
              Add_Parameter_Defining_Id_Or_Ids_Callback'Access);
      end if;

      return Refactoring_Edits'(Text_Edits => Edits, others => <>);

   exception
      when E : others =>
         Refactor_Trace.Trace
           (E,
            Refactoring_Tool_Refactor_Default_Error_Message ("Add Parameter"));
         return No_Refactoring_Edits;
   end Refactor;

   --------------------------------------
   -- Add_Full_Parameter_Specification --
   --------------------------------------

   procedure Add_Full_Parameter_Specification
     (Self   : Parameter_Adder;
      Target : Basic_Decl'Class;
      Edits  : in out Text_Edit_Map)
   is
      function "+" (T : Text_Type) return Unbounded_String is
         (To_Unbounded_String (To_UTF8 (T)));

      Subp_Spec   : constant Libadalang.Analysis.Subp_Spec :=
        Get_Subp_Spec (Target).As_Subp_Spec;
      Subp_Params : constant Params :=
        (if Subp_Spec.Is_Null then No_Params else Subp_Spec.F_Subp_Params);

      Total_Param_Specs           : constant Natural :=
        Count_Subp_Param_Specs (Subp_Params);
      Total_Param_Spec_Parameters : Natural;

      Is_First_Param_Spec : Boolean := True;
      Is_Last_Param_Spec  : Boolean;

      Is_First_Parameter : Boolean := True;
      Is_Last_Parameter  : Boolean;

      Previous_Parameter  : Defining_Name := No_Defining_Name;

      Param_Spec_Idx           : Positive := 1;
      Param_Spec_Parameter_Idx : Positive := 1;
      Parameter_Idx  : Positive := 1;

      Aliased_Text   : Unbounded_String;
      Mode_Text      : Unbounded_String;
      Type_Expr_Text : Unbounded_String;
      Default_Text   : Unbounded_String;

   begin
      if Subp_Params.Is_Null then
         Safe_Insert
           (Edits     => Edits,
            File_Name => Target.Unit.Get_Filename,
            Edit      => Text_Edit'
              (Location =>
                 Make_Range
                   (End_Sloc (Subp_Spec.Sloc_Range),
                    End_Sloc (Subp_Spec.Sloc_Range)),
               Text     => " (" & Self.New_Parameter & ")"));
         return;
      end if;

      for Param_Spec of Subp_Params.F_Params loop

         Is_First_Parameter := True;
         Total_Param_Spec_Parameters :=
           Count_Param_Spec_Parameters (Param_Spec.As_Param_Spec);
         Param_Spec_Parameter_Idx := 1;
         Is_Last_Param_Spec :=
           Param_Spec_Parameter_Idx = Total_Param_Specs;

         for Parameter of Param_Spec.F_Ids loop
            Is_Last_Parameter :=
              Param_Spec_Parameter_Idx = Total_Param_Spec_Parameters;

            if Parameter_Idx = Self.Relative_Position.Index then
               if Is_First_Parameter
                 and then Is_Last_Parameter
                 and then Is_First_Param_Spec
                 and then Is_Last_Param_Spec
               then
                  case Self.Relative_Position.Side is
                     when Before =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (Start_Sloc (Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     => Self.New_Parameter & "; "));

                     when After =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Param_Spec.Sloc_Range),
                                   End_Sloc (Param_Spec.Sloc_Range)),
                              Text     => "; " & Self.New_Parameter));
                  end case;

                  return;

               elsif (Is_First_Parameter
                      and then Is_Last_Parameter
                      and then Is_First_Param_Spec
                      and then not Is_Last_Param_Spec)
                 or else (Is_First_Parameter
                          and then Is_Last_Parameter
                          and then not Is_First_Param_Spec
                          and then not Is_Last_Param_Spec)
                 or else (Is_First_Parameter
                          and then Is_Last_Parameter
                          and then not Is_First_Param_Spec
                          and then Is_Last_Param_Spec)
               then
                  case Self.Relative_Position.Side is
                     when Before =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (Start_Sloc (Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     => Self.New_Parameter & "; "));

                     when After =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Param_Spec.Sloc_Range),
                                   End_Sloc (Param_Spec.Sloc_Range)),
                              Text     => "; " & Self.New_Parameter));
                  end case;

                  return;

               elsif Is_First_Parameter
                 and then not Is_Last_Parameter
                 and then Is_First_Param_Spec
               then
                  Assert (Self.Relative_Position.Side = Before);

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Target.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location =>
                          Make_Range
                            (Start_Sloc (Parameter.Sloc_Range),
                             Start_Sloc (Parameter.Sloc_Range)),
                        Text     => Self.New_Parameter & "; "));

                  return;

               elsif not Is_First_Parameter
                 and then Is_Last_Parameter
                 and then not Is_Last_Param_Spec
               then
                  case Self.Relative_Position.Side is
                     when Before =>
                        Aliased_Text :=
                          (if Param_Spec.F_Has_Aliased.Kind
                             in Ada_Aliased_Absent_Range
                           then Null_Unbounded_String
                           else +Param_Spec.F_Has_Aliased.Text & " ");
                        Mode_Text :=
                          (if Param_Spec.F_Mode.Kind
                             in Ada_Mode_Default_Range
                           then Null_Unbounded_String
                           else +Param_Spec.F_Mode.Text & " ");
                        Type_Expr_Text := +Param_Spec.F_Type_Expr.Text;
                        Default_Text :=
                          (if Param_Spec.F_Default_Expr.Is_Null
                           then Null_Unbounded_String
                           else " := " & (+Param_Spec.F_Default_Expr.Text));

                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Previous_Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     =>
                                 " : " & Aliased_Text & Mode_Text
                                 & Type_Expr_Text & Default_Text & "; "));
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (Start_Sloc (Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     => Self.New_Parameter & "; "));

                     when After =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Param_Spec.Sloc_Range),
                                   End_Sloc (Param_Spec.Sloc_Range)),
                              Text     => "; " & Self.New_Parameter));
                  end case;

                  return;

               elsif Is_First_Parameter
                 and then not Is_Last_Parameter
                 and then not Is_First_Param_Spec
               then
                  Assert (Self.Relative_Position.Side = Before);

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Target.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location =>
                          Make_Range
                            (Start_Sloc (Parameter.Sloc_Range),
                             Start_Sloc (Parameter.Sloc_Range)),
                        Text     => Self.New_Parameter & "; "));

                  return;

               elsif not Is_First_Parameter
                 and then not Is_Last_Parameter
               then
                  Assert (Self.Relative_Position.Side = Before);

                  Aliased_Text :=
                    (if Param_Spec.F_Has_Aliased.Kind
                       in Ada_Aliased_Absent_Range
                     then Null_Unbounded_String
                     else +Param_Spec.F_Has_Aliased.Text & " ");
                  Mode_Text :=
                    (if Param_Spec.F_Mode.Kind in Ada_Mode_Default_Range
                     then Null_Unbounded_String
                     else +Param_Spec.F_Mode.Text & " ");
                  Type_Expr_Text := +Param_Spec.F_Type_Expr.Text;
                  Default_Text :=
                    (if Param_Spec.F_Default_Expr.Is_Null
                     then Null_Unbounded_String
                     else " := " & (+Param_Spec.F_Default_Expr.Text));

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Target.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location =>
                          Make_Range
                            (End_Sloc (Previous_Parameter.Sloc_Range),
                             Start_Sloc (Parameter.Sloc_Range)),
                        Text     => " : " & Aliased_Text & Mode_Text
                                    & Type_Expr_Text & Default_Text  & "; "));
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Target.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location =>
                          Make_Range
                            (Start_Sloc (Parameter.Sloc_Range),
                             Start_Sloc (Parameter.Sloc_Range)),
                        Text     => Self.New_Parameter & "; "));

                  return;

               elsif not Is_First_Parameter
                 and then Is_Last_Parameter
                 and then Is_Last_Param_Spec
               then
                  case Self.Relative_Position.Side is
                     when Before =>
                        Aliased_Text :=
                          (if Param_Spec.F_Has_Aliased.Kind
                             in Ada_Aliased_Absent_Range
                           then Null_Unbounded_String
                           else +Param_Spec.F_Has_Aliased.Text & " ");
                        Mode_Text :=
                          (if Param_Spec.F_Mode.Kind
                             in Ada_Mode_Default_Range
                           then Null_Unbounded_String
                           else +Param_Spec.F_Mode.Text & " ");
                        Type_Expr_Text := +Param_Spec.F_Type_Expr.Text;
                        Default_Text :=
                          (if Param_Spec.F_Default_Expr.Is_Null then
                              Null_Unbounded_String
                           else " := " & (+Param_Spec.F_Default_Expr.Text));

                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Previous_Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     => " : " & Aliased_Text & Mode_Text
                                 & Type_Expr_Text & Default_Text  & "; "));
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (Start_Sloc (Parameter.Sloc_Range),
                                   Start_Sloc (Parameter.Sloc_Range)),
                              Text     => Self.New_Parameter & "; "));

                     when After =>
                        Safe_Insert
                          (Edits     => Edits,
                           File_Name => Target.Unit.Get_Filename,
                           Edit      => Text_Edit'
                             (Location =>
                                Make_Range
                                  (End_Sloc (Param_Spec.Sloc_Range),
                                   End_Sloc (Param_Spec.Sloc_Range)),
                              Text     => "; " & Self.New_Parameter));
                  end case;

                  return;
               end if;

               raise Program_Error;
            end if;

            Previous_Parameter := Parameter.As_Defining_Name;
            Param_Spec_Parameter_Idx := Param_Spec_Parameter_Idx + 1;
            Parameter_Idx := Parameter_Idx + 1;
            Is_First_Parameter := False;
         end loop;

         Param_Spec_Idx := Param_Spec_Idx + 1;
         Is_First_Param_Spec := False;
         Is_Last_Param_Spec := Total_Param_Specs = Param_Spec_Idx;
      end loop;
   end Add_Full_Parameter_Specification;

   ------------------------------
   -- Add_Parameter_Identifier --
   ------------------------------

   procedure Add_Parameter_Defining_Id_Or_Ids
     (Self   : Parameter_Adder;
      Target : Basic_Decl'Class;
      Edits  : in out Text_Edit_Map)
   is
      Subp_Spec   : constant Libadalang.Analysis.Subp_Spec :=
        Get_Subp_Spec (Target).As_Subp_Spec;
      Subp_Params : constant Params :=
        (if Subp_Spec.Is_Null then No_Params else Subp_Spec.F_Subp_Params);

      Parameter_Idx  : Positive := 1;

   begin
      if Subp_Params.Is_Null then
         raise Assertion_Error with
           "Cannot add an identifier as a parameter of a subprogram "
           & "without parameters";
      end if;

      for Param_Spec of Subp_Params.F_Params loop
         for Parameter of Param_Spec.F_Ids loop
            if Parameter_Idx = Self.Relative_Position.Index then
               case Self.Relative_Position.Side is
                  when Before =>
                     Safe_Insert
                       (Edits     => Edits,
                        File_Name => Target.Unit.Get_Filename,
                        Edit      => Text_Edit'
                          (Location =>
                             Make_Range
                               (Start_Sloc (Parameter.Sloc_Range),
                                Start_Sloc (Parameter.Sloc_Range)),
                           Text     => Self.New_Parameter & ", "));

                  when After =>
                     Safe_Insert
                       (Edits     => Edits,
                        File_Name => Target.Unit.Get_Filename,
                        Edit      => Text_Edit'
                          (Location =>
                             Make_Range
                               (End_Sloc (Parameter.Sloc_Range),
                                End_Sloc (Parameter.Sloc_Range)),
                           Text     => ", " & Self.New_Parameter));
               end case;

               return;
            end if;

            Parameter_Idx := Parameter_Idx + 1;
         end loop;
      end loop;

      raise Assertion_Error with
        "Bug detected: Unreachable code. Failed to add an Identifier as a new "
        & "parameter due to not finding the target parameter index.";
   end Add_Parameter_Defining_Id_Or_Ids;

   ------------
   -- Create --
   ------------

   function Create
     (Target          : Basic_Decl;
      Parameter_Index : Natural;
      New_Mode        : Ada_Mode;
      Configuration   : Signature_Changer_Configuration_Type :=
        Default_Configuration)
      return Mode_Changer
   is
      Parameter_Indices_Range : constant Parameter_Indices_Range_Type :=
        (Parameter_Index, Parameter_Index);
   begin
      return Mode_Changer'
        (Subp                    => Target,
         Parameter_Indices_Range => Parameter_Indices_Range,
         New_Mode                => New_Mode,
         Configuration           => Configuration);
   end Create;

   ------------
   -- Create --
   ------------

   function Create
     (Target                  : Basic_Decl;
      Parameter_Indices_Range : Parameter_Indices_Range_Type;
      New_Mode                : Ada_Mode;
      Configuration           : Signature_Changer_Configuration_Type :=
        Default_Configuration)
      return Mode_Changer is
   begin
      return Mode_Changer'
        (Subp                    => Target,
         Parameter_Indices_Range => Parameter_Indices_Range,
         New_Mode                => New_Mode,
         Configuration           => Configuration);
   end Create;

   --------------
   -- Refactor --
   --------------

   overriding
   function Refactor
     (Self : Mode_Changer;
      Analysis_Units : access function return Analysis_Unit_Array)
      return Refactoring_Edits is
   begin
      return Refactoring_Edits'
        (Text_Edits =>
           Change_Mode
             (Self.Subp,
              Self.Parameter_Indices_Range,
              Self.New_Mode,
              Analysis_Units.all),
         others     => <>);

   exception
      when E : others =>
         Refactor_Trace.Trace
           (E,
            Refactoring_Tool_Refactor_Default_Error_Message
              ("Change Parameter Mode"));
         return No_Refactoring_Edits;
   end Refactor;

   -----------------
   -- Change_Mode --
   -----------------

   procedure Change_Mode
     (Subp               : Basic_Decl'Class;
      Parameters_Indices : Parameter_Indices_Range_Type;
      New_Mode           : Ada_Mode;
      Edits              : in out Text_Edit_Map)
   is
      Current_Parameter_Index  : Positive := 1;
      First_Parameter_Index    : Positive :=
        Parameters_Indices.First;
      Last_Parameter_Index     : constant Positive :=
        Parameters_Indices.Last;
      N_Of_Parameters_Left     : Natural :=
        Last_Parameter_Index - First_Parameter_Index + 1;

      Param_Spec_Length               : Positive;
      Param_Spec_Last_Parameter_Index : Positive;

      New_Mode_Text : Unbounded_String;

   begin
      case New_Mode is
         when Ada_Mode_Default =>
            New_Mode_Text := To_Unbounded_String ("");

         when Ada_Mode_In =>
            New_Mode_Text := To_Unbounded_String ("in");

         when Ada_Mode_Out =>
            New_Mode_Text := To_Unbounded_String ("out");

         when Ada_Mode_In_Out =>
            New_Mode_Text := To_Unbounded_String ("in out");
      end case;

      for Param_Spec of Get_Subp_Params (Subp).F_Params loop

         exit when N_Of_Parameters_Left = 0;

         Param_Spec_Length := Length (Param_Spec.F_Ids);
         Param_Spec_Last_Parameter_Index :=
           Current_Parameter_Index + Param_Spec_Length - 1;

         if First_Parameter_Index = Current_Parameter_Index then
            if Param_Spec_Last_Parameter_Index < Last_Parameter_Index then
               --  Case 1: Checked

               Safe_Insert
                 (Edits    => Edits,
                  File_Name => Subp.Unit.Get_Filename,
                  Edit => Text_Edit'
                    (Location => Source_Location_Range'
                      (Start_Line   => Param_Spec.F_Mode.Sloc_Range.Start_Line,
                       End_Line     => Param_Spec.F_Mode.Sloc_Range.End_Line,
                       Start_Column =>
                         Param_Spec.F_Mode.Sloc_Range.Start_Column,
                       End_Column   =>
                         Param_Spec.F_Mode.Sloc_Range.End_Column),
                     Text     => New_Mode_Text));

               First_Parameter_Index := Param_Spec_Last_Parameter_Index + 1;
               N_Of_Parameters_Left :=
                 N_Of_Parameters_Left - Param_Spec_Length;

            elsif Param_Spec_Last_Parameter_Index = Last_Parameter_Index then
               --  Case 2: Checked

               if To_String (Param_Spec.F_Mode.Text) /= New_Mode_Text then
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location   => Source_Location_Range'
                         (Start_Line   =>
                            Param_Spec.F_Mode.Sloc_Range.Start_Line,
                          End_Line     =>
                            Param_Spec.F_Mode.Sloc_Range.End_Line,
                          Start_Column =>
                            Param_Spec.F_Mode.Sloc_Range.Start_Column,
                          End_Column   =>
                            Param_Spec.F_Mode.Sloc_Range.End_Column),
                        Text       => (if New_Mode_Text = "" then
                                          Null_Unbounded_String
                                       else
                                          " " & New_Mode_Text)));
               end if;

               N_Of_Parameters_Left := 0;

               exit;

            else
               --  Case 3: Checked

               declare
                  Parameter_A  : Defining_Name := No_Defining_Name;
                  Parameter_B  : Defining_Name := No_Defining_Name;
                  Subtype_Text : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index = Last_Parameter_Index then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index =
                       Last_Parameter_Index + 1
                     then
                        Parameter_B := Parameter.As_Defining_Name;
                        exit;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                         (Start_Line   => Parameter_A.Sloc_Range.End_Line,
                          End_Line     => Parameter_B.Sloc_Range.Start_Line,
                          Start_Column => Parameter_A.Sloc_Range.End_Column,
                          End_Column   => Parameter_B.Sloc_Range.Start_Column),
                        Text     =>
                          " : " & New_Mode_Text & " " & Subtype_Text & ";"));
               end;

               N_Of_Parameters_Left := 0;

               exit;

            end if;

         elsif First_Parameter_Index in
           Current_Parameter_Index + 1 .. Param_Spec_Last_Parameter_Index
         then
            if First_Parameter_Index = Param_Spec_Last_Parameter_Index then
               --  Case 4: Checked

               declare
                  Parameter_A  : Defining_Name := No_Defining_Name;
                  Parameter_B  : Defining_Name := No_Defining_Name;

                  Parameter_B_Text : Unbounded_String;
                  Subtype_Text     : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index =
                       First_Parameter_Index - 1
                     then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index = First_Parameter_Index then
                        Parameter_B := Parameter.As_Defining_Name;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'(
                     Location  => Source_Location_Range'
                       (Start_Line   => Parameter_A.Sloc_Range.End_Line,
                        End_Line     => Parameter_B.Sloc_Range.End_Line,
                        Start_Column => Parameter_A.Sloc_Range.End_Column,
                        End_Column   => Parameter_B.Sloc_Range.End_Column),
                     Text     => To_Unbounded_String ("")));

                  Parameter_B_Text := To_Unbounded_String
                    (To_String (Parameter_B.Text));
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                         (Start_Line   => Param_Spec.Sloc_Range.End_Line,
                          End_Line     => Param_Spec.Sloc_Range.End_Line,
                          Start_Column => Param_Spec.Sloc_Range.End_Column,
                          End_Column   => Param_Spec.Sloc_Range.End_Column),
                        Text     =>
                          "; " & Parameter_B_Text & " : " & New_Mode_Text
                        & " " & Subtype_Text));
               end;

               First_Parameter_Index := Current_Parameter_Index;
               N_Of_Parameters_Left := N_Of_Parameters_Left - 1;

            elsif Last_Parameter_Index > Param_Spec_Last_Parameter_Index then
               --  Case 5: Checked

               declare
                  Parameter_A  : Defining_Name := No_Defining_Name;
                  Parameter_B  : Defining_Name := No_Defining_Name;

                  Mode_Text       : constant String :=
                    To_String (Param_Spec.F_Mode.Text);
                  Subtype_Text     : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index =
                       First_Parameter_Index - 1
                     then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index = First_Parameter_Index then
                        Parameter_B := Parameter.As_Defining_Name;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  if To_String (Param_Spec.F_Mode.Text) /= New_Mode_Text then
                     Safe_Insert
                       (Edits     => Edits,
                        File_Name => Subp.Unit.Get_Filename,
                        Edit      =>
                          (Location => Param_Spec.F_Mode.Sloc_Range,
                           Text     => (if New_Mode_Text = "" then
                                          Null_Unbounded_String
                                       else
                                          " " & New_Mode_Text)));
                  end if;

                  Safe_Insert
                    (Edits    => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      =>
                       (Location => Source_Location_Range'
                         (Start_Line   => Parameter_A.Sloc_Range.End_Line,
                          End_Line     => Parameter_B.Sloc_Range.Start_Line,
                          Start_Column => Parameter_A.Sloc_Range.End_Column,
                          End_Column   => Parameter_B.Sloc_Range.Start_Column),
                        Text     =>
                          To_Unbounded_String
                            (" : " & Mode_Text & " " & Subtype_Text & "; ")));
               end;

               N_Of_Parameters_Left :=
                 N_Of_Parameters_Left -
                   ((Current_Parameter_Index - 1) - First_Parameter_Index);
               First_Parameter_Index := Current_Parameter_Index;

            elsif Last_Parameter_Index = Param_Spec_Last_Parameter_Index then
               --  Case 6: Checked

               declare
                  Parameter_A  : Defining_Name := No_Defining_Name;
                  Parameter_B  : Defining_Name := No_Defining_Name;

                  Mode_Text    : constant String :=
                    To_String (Param_Spec.F_Mode.Text);
                  Subtype_Text : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index =
                       First_Parameter_Index - 1
                     then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index = First_Parameter_Index then
                        Parameter_B := Parameter.As_Defining_Name;
                        exit;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  if To_String (Param_Spec.F_Mode.Text) /= New_Mode_Text then
                     Safe_Insert
                       (Edits     => Edits,
                        File_Name => Subp.Unit.Get_Filename,
                        Edit      => Text_Edit'
                          (Location => Param_Spec.F_Mode.Sloc_Range,
                           Text     => " " & New_Mode_Text));
                  end if;

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      =>
                       Text_Edit'
                         (Location => Source_Location_Range'
                           (Start_Line   => Parameter_A.Sloc_Range.End_Line,
                            End_Line     => Parameter_B.Sloc_Range.Start_Line,
                            Start_Column => Parameter_A.Sloc_Range.End_Column,
                            End_Column   =>
                              Parameter_B.Sloc_Range.Start_Column),
                          Text     => To_Unbounded_String
                            (" : " & Mode_Text & " " & Subtype_Text & "; ")));
               end;

               N_Of_Parameters_Left := 0;

               exit;

            else
               --  Case 7: Checked

               Assert (Last_Parameter_Index < Param_Spec_Last_Parameter_Index);

               declare
                  Parameter_A  : Defining_Name := No_Defining_Name;
                  Parameter_B  : Defining_Name := No_Defining_Name;
                  Parameter_C  : Defining_Name := No_Defining_Name;
                  Parameter_D  : Defining_Name := No_Defining_Name;

                  Mode_Text    : constant String :=
                    To_String  (Param_Spec.F_Mode.Text);
                  Subtype_Text : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index =
                       First_Parameter_Index - 1
                     then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index = First_Parameter_Index then
                        Parameter_B := Parameter.As_Defining_Name;
                     end if;

                     if Current_Parameter_Index = Last_Parameter_Index
                     then
                        Parameter_C := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index =
                       Last_Parameter_Index + 1
                     then
                        Parameter_D := Parameter.As_Defining_Name;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  Safe_Insert
                    (Edits    => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit => (Location => Source_Location_Range'
                                  (Start_Line   =>
                                     Parameter_A.Sloc_Range.End_Line,
                                   End_Line     =>
                                     Parameter_B.Sloc_Range.Start_Line,
                                   Start_Column =>
                                     Parameter_A.Sloc_Range.End_Column,
                                   End_Column   =>
                                     Parameter_B.Sloc_Range.Start_Column),
                              Text     =>
                                To_Unbounded_String
                                  (" : " & Mode_Text & " " & Subtype_Text
                                   & "; ")));

                  Safe_Insert
                    (Edits    => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit => (Location => Source_Location_Range'
                                  (Start_Line   =>
                                     Parameter_C.Sloc_Range.End_Line,
                                   End_Line     =>
                                     Parameter_D.Sloc_Range.Start_Line,
                                   Start_Column =>
                                     Parameter_C.Sloc_Range.End_Column,
                                   End_Column   =>
                                     Parameter_D.Sloc_Range.Start_Column),
                              Text     =>
                                " : " & New_Mode_Text & " " & Subtype_Text
                              & "; "));
               end;

               N_Of_Parameters_Left := 0;

               exit;

            end if;
         end if;

         Current_Parameter_Index := Param_Spec_Last_Parameter_Index + 1;
      end loop;
   end Change_Mode;

   ------------
   -- Create --
   ------------

   function Create
     (Target          : Basic_Decl;
      Parameter_Index : Natural;
      Configuration   : Signature_Changer_Configuration_Type :=
        Default_Configuration)
      return Backward_Mover
   is
   begin
      return Backward_Mover'
        (Subp            => Target,
         Parameter_Index => Parameter_Index,
         Configuration   => Configuration);
   end Create;

   --------------
   -- Refactor --
   --------------

   overriding
   function Refactor
     (Self           : Backward_Mover;
      Analysis_Units : access function return Analysis_Unit_Array)
      return Refactoring_Edits is
   begin
      return Refactoring_Edits'
        (Text_Edits =>
           Move_Backward (Self.Subp, Self.Parameter_Index, Analysis_Units.all),
         others     => <>);

   exception
      when E : others =>
         Refactor_Trace.Trace
           (E,
            Refactoring_Tool_Refactor_Default_Error_Message
              ("Move Parameter Backward"));
         return No_Refactoring_Edits;
   end Refactor;

   -------------------
   -- Move_Backward --
   -------------------

   procedure Move_Backward
     (Subp            : Basic_Decl'Class;
      Parameter_Index : Positive;
      Edits           : in out Text_Edit_Map)
   is
      Param_Spec_Length          : Positive;
      Current_Parameter_Index    : Positive := 1;
      Previous_Param_Spec        : Param_Spec := No_Param_Spec;
      Previous_Param_Spec_Length : Positive := 1;

   begin
      for Param_Spec of Get_Subp_Params (Subp).F_Params loop
         Param_Spec_Length := Length (Param_Spec.F_Ids);

         if Parameter_Index = Current_Parameter_Index
           and then Param_Spec_Length = 1
         then
            if Previous_Param_Spec_Length = 1 then
               --  Case 1:
               --
               --  Parameter B is the only one of the Param_Spec, and the
               --  previous parameter is also the only one of the
               --  Previous_Param_Spec.
               --
               --  A : in Integer; B : out Float
               --  --------------  -------------
               --
               --  B : out Float; A : in Integer
               --  +++++++++++++  ++++++++++++++

               Safe_Insert
                 (Edits     => Edits,
                  File_Name => Subp.Unit.Get_Filename,
                  Edit      => Text_Edit'
                    (Location => Param_Spec.Sloc_Range,
                     Text     =>
                       To_Unbounded_String
                         ((To_String (Previous_Param_Spec.Text)))));

               Safe_Insert
                 (Edits     => Edits,
                  File_Name => Subp.Unit.Get_Filename,
                  Edit      => Text_Edit'
                    (Location => Previous_Param_Spec.Sloc_Range,
                     Text     =>
                       To_Unbounded_String ((To_String (Param_Spec.Text)))));

            else
               --  Case 2:
               --
               --  Parameter C is the only one of the Param_Spec, and the
               --  previous parameter is not the only one on the
               --  Previous_Param_Spec
               --
               --  A, B : in Integer; C : out Float
               --   ---
               --
               --  A : in Integer; C : out Float; B : in Integer
               --                               ++++++++++++++++

               declare
                  Parameter_A : Defining_Name := No_Defining_Name;
                  Parameter_B : Defining_Name := No_Defining_Name;

                  Mode_Text : constant String :=
                    To_String (Previous_Param_Spec.F_Mode.Text);
                  Type_Text : constant String :=
                    To_String (Previous_Param_Spec.F_Type_Expr.Text);

                  New_Text : Unbounded_String;

                  Relative_Index : Positive := 1;

               begin
                  for Parameter of Previous_Param_Spec.F_Ids loop
                     if Relative_Index = Previous_Param_Spec_Length - 1 then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Relative_Index = Previous_Param_Spec_Length then
                        Parameter_B := Parameter.As_Defining_Name;
                     end if;

                     Relative_Index := Relative_Index + 1;
                  end loop;

                  New_Text := To_Unbounded_String
                    ("; "
                     & To_String (Parameter_B.Text)
                     & " : "
                     & Mode_Text
                     & " "
                     & Type_Text);

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                       (Parameter_A.Sloc_Range.End_Line,
                        Parameter_B.Sloc_Range.End_Line,
                        Parameter_A.Sloc_Range.End_Column,
                        Parameter_B.Sloc_Range.End_Column),
                        Text     => Null_Unbounded_String));
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                       (Param_Spec.Sloc_Range.End_Line,
                        Param_Spec.Sloc_Range.End_Line,
                        Param_Spec.Sloc_Range.End_Column,
                        Param_Spec.Sloc_Range.End_Column),
                        Text     => New_Text));
               end;
            end if;

            exit;

         elsif Parameter_Index = Current_Parameter_Index
           and then Param_Spec_Length > 1
         then
            if Previous_Param_Spec_Length = 1 then
               --  Case 3 :
               --
               --  Parameter B is the first one of Param_Spec, and the
               --  previous parameter is the only one on the previous
               --  Previous_Param_Spec.
               --
               --  A : in Integer; B, C : out Float
               --                  ---
               --
               --  B : out Float; A : in Integer; C : out Float
               --  +++++++++++++++
               --

               declare
                  Parameter_B : Defining_Name := No_Defining_Name;
                  Parameter_C : Defining_Name := No_Defining_Name;

                  Mode_Text : constant String :=
                    To_String (Param_Spec.F_Mode.Text);
                  Type_Text : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

                  New_Text : Unbounded_String;

               begin
                  for Parameter of Param_Spec.F_Ids loop
                     if Current_Parameter_Index = Parameter_Index then
                        Parameter_B := Parameter.As_Defining_Name;

                     elsif Current_Parameter_Index =
                       Parameter_Index + 1
                     then
                        Parameter_C := Parameter.As_Defining_Name;
                     end if;

                     Current_Parameter_Index := Current_Parameter_Index + 1;
                  end loop;

                  --  Assert that Parameter_B and Parameter_C have been found
                  --  and have changed.
                  Assert (Parameter_B /= No_Defining_Name
                          and then Parameter_C /= No_Defining_Name);

                  New_Text := To_Unbounded_String
                    (To_String (Parameter_B.Text)
                     & " : "
                     & Mode_Text
                     & " "
                     & Type_Text
                     & "; ");

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                            (Parameter_B.Sloc_Range.Start_Line,
                             Parameter_C.Sloc_Range.Start_Line,
                             Parameter_B.Sloc_Range.Start_Column,
                             Parameter_C.Sloc_Range.Start_Column),
                        Text     => Null_Unbounded_String));
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                            (Previous_Param_Spec.Sloc_Range.Start_Line,
                             Previous_Param_Spec.Sloc_Range.Start_Line,
                             Previous_Param_Spec.Sloc_Range.Start_Column,
                             Previous_Param_Spec.Sloc_Range.Start_Column),
                        Text     => New_Text));
               end;

            else
               --  Case 4
               --
               --  Parameter C is the first one of Param_Spec, and the
               --  previous parameter is not the only one of the previous
               --  Previous_Param_Spec.
               --
               --  A, B : in Integer; C, D : out Float
               --   --                ---
               --
               --  A : in Integer; C : out Float; B : in Integer; D : out Float
               --   ++++++++++++++++++++++++++++++
               --

               declare
                  Parameter_A : Defining_Name := No_Defining_Name;
                  Parameter_B : Defining_Name := No_Defining_Name;
                  Parameter_C : Defining_Name := No_Defining_Name;
                  Parameter_D : Defining_Name := No_Defining_Name;

                  First_Mode_Text : constant String :=
                    To_String (Previous_Param_Spec.F_Mode.Text);
                  First_Type_Text : constant String :=
                    To_String (Previous_Param_Spec.F_Type_Expr.Text);
                  Second_Mode_Text : constant String :=
                    To_String (Param_Spec.F_Mode.Text);
                  Second_Type_Text : constant String :=
                    To_String (Param_Spec.F_Type_Expr.Text);

                  New_Text : Unbounded_String;

                  Relative_Index : Positive := 1;

               begin
                  for Parameter of Previous_Param_Spec.F_Ids loop
                     if Relative_Index = Previous_Param_Spec_Length - 1 then
                        Parameter_A := Parameter.As_Defining_Name;

                     elsif Relative_Index = Previous_Param_Spec_Length then
                        Parameter_B := Parameter.As_Defining_Name;
                     end if;

                     Relative_Index := Relative_Index + 1;
                  end loop;

                  Relative_Index := 1;

                  for Parameter of Param_Spec.F_Ids loop
                     if Relative_Index = 1 then
                        Parameter_C := Parameter.As_Defining_Name;

                     elsif Relative_Index = 2 then
                        Parameter_D := Parameter.As_Defining_Name;
                     end if;

                     Relative_Index := Relative_Index + 1;
                  end loop;

                  New_Text := To_Unbounded_String
                    (" : "
                     & First_Mode_Text
                     & " "
                     & First_Type_Text
                     & "; "
                     & To_String (Parameter_C.Text)
                     & " : "
                     & Second_Mode_Text
                     & " "
                     & Second_Type_Text
                     & "; ");

                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                            (Parameter_C.Sloc_Range.Start_Line,
                             Parameter_D.Sloc_Range.Start_Line,
                             Parameter_C.Sloc_Range.Start_Column,
                             Parameter_D.Sloc_Range.Start_Column),
                        Text     => Null_Unbounded_String));
                  Safe_Insert
                    (Edits     => Edits,
                     File_Name => Subp.Unit.Get_Filename,
                     Edit      => Text_Edit'
                       (Location => Source_Location_Range'
                            (Parameter_A.Sloc_Range.End_Line,
                             Parameter_B.Sloc_Range.Start_Line,
                             Parameter_A.Sloc_Range.End_Column,
                             Parameter_B.Sloc_Range.Start_Column),
                        Text     => New_Text));
               end;
            end if;

            exit;

         elsif Param_Spec_Length > 1
           and then Parameter_Index in
             Current_Parameter_Index + 1 ..
               Current_Parameter_Index + Param_Spec_Length - 1
         then
            --  Case 5
            --
            --  Parameter B is not the first one of Param_Spec.
            --
            --  A, B : in Integer
            --  -  -
            --
            --  B, A : in Integer
            --  +  +

            declare
               Parameter_A : Defining_Name := No_Defining_Name;
               Parameter_B : Defining_Name := No_Defining_Name;

            begin
               for Parameter of Param_Spec.F_Ids loop
                  if Current_Parameter_Index = Parameter_Index - 1 then
                     Parameter_A := Parameter.As_Defining_Name;

                  elsif Current_Parameter_Index = Parameter_Index then
                     Parameter_B := Parameter.As_Defining_Name;
                     exit;
                  end if;

                  Current_Parameter_Index := Current_Parameter_Index + 1;
               end loop;

               --  Assert that Parameter_A and Parameter_B have been found
               --  have changed.
               Assert (Parameter_A /= No_Defining_Name
                       and then Parameter_B /= No_Defining_Name);

               Safe_Insert
                 (Edits     => Edits,
                  File_Name => Subp.Unit.Get_Filename,
                  Edit      => Text_Edit'
                    (Location => Parameter_A.Sloc_Range,
                     Text     => To_Unbounded_String
                       (To_String (Parameter_B.Text))));
               Safe_Insert
                 (Edits     => Edits,
                  File_Name => Subp.Unit.Get_Filename,
                  Edit      => Text_Edit'
                    (Location => Parameter_B.Sloc_Range,
                     Text     => To_Unbounded_String
                       (To_String (Parameter_A.Text))));
            end;

            exit;

         else
            Current_Parameter_Index :=
              Current_Parameter_Index + Param_Spec_Length;
         end if;

         Previous_Param_Spec := Param_Spec.As_Param_Spec;
         Previous_Param_Spec_Length := Param_Spec_Length;
      end loop;
   end Move_Backward;

   ------------
   -- Create --
   ------------

   function Create
     (Target          : Basic_Decl;
      Parameter_Index : Natural;
      Configuration   : Signature_Changer_Configuration_Type :=
        Default_Configuration)
      return Forward_Mover
   is (Forward_Mover'
         (Mover => Create (Target, Parameter_Index + 1, Configuration)));

   --------------
   -- Refactor --
   --------------

   overriding
   function Refactor
     (Self           : Forward_Mover;
      Analysis_Units : access function return Analysis_Unit_Array)
      return Refactoring_Edits is
   begin
      return Self.Mover.Refactor (Analysis_Units);

   exception
      when E : others =>
         Refactor_Trace.Trace
           (E,
            Refactoring_Tool_Refactor_Default_Error_Message
              ("Move Parameter Forward"));
         return No_Refactoring_Edits;
   end Refactor;

end LAL_Refactor.Subprogram_Signature;