svd2ada_0.1.0_6eb0b591/src/ada_gen.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
------------------------------------------------------------------------------
--                                                                          --
--                          SVD Binding Generator                           --
--                                                                          --
--                    Copyright (C) 2015-2020, AdaCore                      --
--                                                                          --
-- SVD2Ada is free software;  you can  redistribute it  and/or modify it    --
-- under terms of the  GNU General Public License as published  by the Free --
-- Software  Foundation;  either version 3,  or (at your option) any later  --
-- version.  SVD2Ada is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY;  without even the  implied warranty of MERCHANTA-  --
-- BILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  --
-- License for  more details.  You should have  received  a copy of the GNU --
-- General Public License  distributed with SVD2Ada; see file COPYING3.  If --
-- not, go to http://www.gnu.org/licenses for a complete copy of the        --
-- license.                                                                 --
--                                                                          --
------------------------------------------------------------------------------

with Interfaces; use Interfaces;

with Ada.Calendar;
with Ada.Characters.Handling;
with Ada.Strings.Fixed;          use Ada.Strings;
with Ada.Strings.Unbounded.Equal_Case_Insensitive;
with Ada.Tags;

with GNAT.Directory_Operations;
with GNAT.OS_Lib;

with SVD2Ada_Utils;

package body Ada_Gen is

   Max_Width      : constant Natural := 79;
   G_Input_File   : Unbounded_String;
   G_Empty_Line   : Boolean := False;
   G_Withed_All   : Unbounded_String := Null_Unbounded_String;
   G_License_Text : Unbounded_String := Null_Unbounded_String;

   function Is_Parent
     (Spec        : Ada_Spec;
      With_Clause : Ada_With_Clause) return Boolean;

   function Ada_Identifier
     (Id           : String;
      Container_Id : String) return Unbounded_String;
   --  return Id with any changes required to conform to Ada syntax

   procedure Dump_Aspects
     (Aspects : String_Vectors.Vector;
      File    : Ada.Text_IO.File_Type;
      Inline  : Boolean := False);

   procedure Dump_Record_Fields
     (Element : Ada_Type_Record'Class;
      Max_Id  : Natural;
      File    : Ada.Text_IO.File_Type);

   function Add_Enum_Id_Internal
     (Spec     : Ada_Spec;
      Enum     : in out Ada_Type_Enum;
      Id       : String;
      Has_Repr : Boolean;
      Repr     : Unsigned;
      Comment  : String := "")  return Ada_Enum_Value;

   procedure Add_Field_Internal
     (Rec         : in out Ada_Type_Record'Class;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Has_Default : Boolean;
      Default     : Unbounded_String;
      Properties  : Field_Properties;
      Comment     : String := "");

   ----------------------
   -- Protect_Keywords --
   ----------------------

   function Ada_Identifier
     (Id           : String;
      Container_Id : String)
      return Unbounded_String
   is
      Low_Id : constant String := Ada.Characters.Handling.To_Lower (Id);
   begin
      --  If the last character is an underscore, remove it
      if Id (Id'Last) = '_' then
         return Ada_Identifier (Id (Id'First .. Id'Last - 1), Container_Id);
      --  Same for the first character
      elsif Id (Id'First) = '_' then
         return Ada_Identifier (Id (Id'First + 1 .. Id'Last), Container_Id);
      else
         --  change two successive underscores into one
         for J in Id'First + 1 .. Id'Last loop
            if Id (J) = '_' and then Id (J - 1) = '_' then
               return Ada_Identifier
                 (Id (Id'First .. J - 1) & Id (J + 1 .. Id'Last), Container_Id);
            end if;
         end loop;
      end if;

      if SVD2Ada_Utils.Is_Reserved_Word (Low_Id) then
         return To_Unbounded_String (Id & "_k");
      end if;

      --  Now check that the first character is a letter
      if Low_Id (Low_Id'First) not in 'a' .. 'z' then
         return To_Unbounded_String (Container_Id & "_" & Id);
      end if;

      return To_Unbounded_String (Id);
   end Ada_Identifier;

   -------------------------
   -- Set_Input_File_Name --
   -------------------------

   procedure Set_Input_File_Name (FName : String)
   is
   begin
      G_Input_File := To_Unbounded_String (FName);
   end Set_Input_File_Name;

   ----------------------
   -- Set_License_Text --
   ----------------------

   procedure Set_License_Text (Text : Unbounded_String)
   is
      Is_New_Line : Boolean := True;
      J           : Natural := 1;

      function Valid_Index (Index : Natural) return Boolean
        is (Index <= Length (Text));

   begin
      while Valid_Index (J) loop

         --  Beginning of a line...
         Is_New_Line := True;

         --  ... add the comment marker.
         Append (G_License_Text, "--");

         --  Process all the characters until end of text or a new line marker
         Line_Loop :  while Valid_Index (J) loop

            if (Is_New_Line and then Element (Text, J) = ' ')
              or else
                Element (Text, J) = ASCII.CR
              or else
                Element (Text, J) = ASCII.LF
            then
               --  Ignore leading white space and CR and LF from the input SVD
               --  file.
               J := J + 1;

            elsif Valid_Index (J + 1)
              and then Slice (Text, J, J + 1) = "\n"
            then
               --  New line delimiter in the input license text

               --  Skip it
               J := J + 2;

               --  Trim trailing whitespace
               Trim (G_License_Text, Right);

               --  Start new line in the output
               Append (G_License_Text, ASCII.LF);

               --  Start procesing the next line
               exit Line_Loop;
            else
               if Is_New_Line then
                  --  First character of a new line, we know that the line will
                  --  not be empty so we can add the two leading white spaces
                  --  of the comment.
                  Append (G_License_Text, "  ");
                  Is_New_Line := False;
               end if;
               Append (G_License_Text, Element (Text, J));
               J := J + 1;
            end if;
         end loop Line_Loop;
      end loop;

      --  Trim trailing whitespace
      Trim (G_License_Text, Right);
   end Set_License_Text;

   --------------
   -- Is_Empty --
   --------------

   function Is_Empty (Comment : Ada_Comment) return Boolean is
   begin
      return Length (Comment.Comment) = 0;
   end Is_Empty;

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

   procedure Dump
     (Comment : Ada_Comment;
      F       : Ada.Text_IO.File_Type;
      Indent  : Natural;
      Inline  : Boolean)
   is
      First      : Natural := 1;
      Last_Space : Integer := -1;
      Pre        : constant String (1 .. 3 * Indent) := (others => ' ');

   begin
      if Comment.Is_Empty then
         return;
      end if;

      if not Inline then
         for J in 1 .. Length (Comment.Comment) loop
            if Element (Comment.Comment, J) = ' ' then
               Last_Space := J;
            end if;

            if Element (Comment.Comment, J) = ASCII.LF then
               if J = First then
                  Ada.Text_IO.Put_Line (F, "--");
               else
                  Ada.Text_IO.Put_Line
                    (F,
                     Pre & "--  " & Slice (Comment.Comment, First, J - 1));
               end if;

               First := J + 1;
            end if;

            if J - First + Pre'Length + 4 >= Max_Width
              and then Last_Space > 0
            then
               Ada.Text_IO.Put_Line
                 (F,
                  Pre & "--  " &
                    Slice (Comment.Comment, First, Last_Space - 1));
               First := Last_Space + 1;
            end if;
         end loop;
      end if;

      Ada.Text_IO.Put_Line
        (F,
         Pre & "--  " &
           Slice (Comment.Comment, First, Length (Comment.Comment)));
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Comment;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      Ada.Text_IO.Put_Line (File, "   --  " & To_String (Element.Comment));
      G_Empty_Line := False;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Comment_Box;
      File    : Ada.Text_IO.File_Type)
   is
      Center : constant String := "-- " & To_String (Element.Comment) & " --";
      Top    : constant String (Center'Range) := (others => '-');
   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;
      Ada.Text_IO.Put_Line (File, "   " & Top);
      Ada.Text_IO.Put_Line (File, "   " & Center);
      Ada.Text_IO.Put_Line (File, "   " & Top);
      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Pragma;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;

      if not Element.Comment.Is_Empty then
         Dump (Element.Comment, File);
      end if;

      Ada.Text_IO.New_Line (File);
      Ada.Text_IO.Put_Line (File, "   pragma " & To_String (Element.Id) & ";");
      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

   ------------------
   -- Dump_Aspects --
   ------------------

   procedure Dump_Aspects (Aspects : String_Vectors.Vector;
                           File    : Ada.Text_IO.File_Type;
                           Inline  : Boolean := False)
   is
      First : Boolean := True;
      Col   : Natural;
   begin
      if Inline then
         Ada.Text_IO.Put (File, " with ");
      else
         Ada.Text_IO.Put (File, "     with ");
      end if;

      Col := 11;

      for A of Aspects loop
         if First then
            Ada.Text_IO.Put (File, A);
            Col := Col + A'Length;
            First := False;

         elsif Col + A'Length + 2 > Max_Width and then not Inline then
            Ada.Text_IO.Put_Line (File, ",");
            Ada.Text_IO.Put (File, (1 .. 10 => ' ') & A);
            Col := A'Length + 11;

         else
            Ada.Text_IO.Put (File, ", " & A);
            Col := Col + A'Length + 2;
         end if;
      end loop;

      Ada.Text_IO.Put_Line (File, ";");
      G_Empty_Line := False;
   end Dump_Aspects;

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

   overriding procedure Dump
     (Element : Ada_Type_Scalar;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      if Element.Size = 8
        or else Element.Size = 16
        or else Element.Size = 32
        or else Element.Size = 64
      then
         Ada.Text_IO.Put_Line
           (File,
            "   type " & To_String (Element.Id) &
            " is new Interfaces.Unsigned_" & To_String (Element.Size) & ";");
      else
         Ada.Text_IO.Put
           (File,
            "   type " & To_String (Element.Id) &
            " is mod 2**" & To_String (Element.Size));
         if Element.Aspects.Is_Empty then
            Ada.Text_IO.Put_Line (File, ";");
         else
            Ada.Text_IO.New_Line (File);
            Dump_Aspects (Element.Aspects, File);
         end if;
      end if;

      G_Empty_Line := False;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Subtype_Scalar;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      declare
         Subt_String : constant String :=
                         "   subtype " & To_String (Element.Id) & " is";
         Val         : constant String := To_String (Element.Typ);
      begin
         Ada.Text_IO.Put (File, Subt_String);

         if Subt_String'Length + 2 + Val'Length > Max_Width then
            Ada.Text_IO.New_Line (File);
            Ada.Text_IO.Put_Line (File, "     " & Val & ";");
         else
            Ada.Text_IO.Put_Line  (File, " " & Val & ";");
         end if;
      end;

      G_Empty_Line := False;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Type_Array;
      File    : Ada.Text_IO.File_Type)
   is
      Line  : Unbounded_String;
      Line2 : Unbounded_String;

   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;

      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Line := To_Unbounded_String (String'(1 .. 3 => ' ')) &
        "type " & Element.Id & " is array (";

      if Length (Element.Index_Type) > 0 then
         Append (Line, Element.Index_Type & " range ");
      end if;

      Append (Line,
              To_String (Element.Index_First) & " .. " &
                  To_String (Element.Index_Last) & ")");

      Line2 := To_Unbounded_String (" of ") & Element.Element_Type;

      if Length (Line) + Length (Line2) + 1 < Max_Width then
         Append (Line, Line2);
         Ada.Text_IO.Put (File, To_String (Line));
      else
         Ada.Text_IO.Put_Line (File, To_String (Line));
         Ada.Text_IO.Put (File, (1 .. 4 => ' ') & To_String (Line2));
      end if;

      if Element.Aspects.Is_Empty then
         Ada.Text_IO.Put_Line (File, ";");
      else
         Ada.Text_IO.New_Line (File);
         Dump_Aspects (Element.Aspects, File);
      end if;

      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Type_Enum;
      File    : Ada.Text_IO.File_Type)
   is
      Has_Repr : Boolean := False;
      Value    : Ada_Enum_Value;

   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;

      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Ada.Text_IO.Put_Line
        (File, "   type " & To_String (Element.Id) & " is");

      for J in Element.Values.First_Index .. Element.Values.Last_Index loop
         Value := Element.Values (J);

         if J = Element.Values.First_Index then
            Ada.Text_IO.Put (File, (1 .. 5 => ' ') & '(');
         else
            Ada.Text_IO.Put (File, (1 .. 6 => ' '));
         end if;

         if not Is_Empty (Value.Comment) then
            Dump
              (Value.Comment,
               F      => File,
               Indent => 0,
               Inline => False);
            Ada.Text_IO.Put (File, (1 .. 6 => ' '));
         end if;

         Ada.Text_IO.Put (File, To_String (Value.Id));

         if J < Element.Values.Last_Index then
            Ada.Text_IO.Put_Line (File, ",");
         else
            Ada.Text_IO.Put (File, ")");
         end if;

         if Value.Has_Repr then
            Has_Repr := True;
         end if;
      end loop;

      if Element.Aspects.Is_Empty then
         Ada.Text_IO.Put_Line (File, ";");
      else
         Ada.Text_IO.New_Line (File);
         Dump_Aspects (Element.Aspects, File, False);
      end if;

      if Has_Repr then
         Ada.Text_IO.Put_Line
           (File, "   for " & To_String (Element.Id) & " use");

         for J in Element.Values.First_Index .. Element.Values.Last_Index loop
            Value := Element.Values (J);
            if J = Element.Values.First_Index then
               Ada.Text_IO.Put (File, (1 .. 5 => ' ') & '(');
            else
               Ada.Text_IO.Put_Line (File, ",");
               Ada.Text_IO.Put (File, (1 .. 6 => ' '));
            end if;

            Ada.Text_IO.Put
              (File, To_String (Value.Id) & " => " & To_String (Value.Repr));
         end loop;

         Ada.Text_IO.Put_Line (File, ");");
      end if;

      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

   ------------------------
   -- Dump_Record_Fields --
   ------------------------

   procedure Dump_Record_Fields
     (Element : Ada_Type_Record'Class;
      Max_Id  : Natural;
      File    : Ada.Text_IO.File_Type)
   is
      function Get_Id (F : Record_Field) return String;

      ------------
      -- Get_Id --
      ------------

      function Get_Id (F : Record_Field) return String
      is
         Id : String (1 .. Max_Id) := (others => ' ');
      begin
         Id (1 .. Length (F.Id)) := To_String (F.Id);
         return Id;
      end Get_Id;

   begin
      for F of Element.Fields loop
         if not F.Comment.Is_Empty then
            Dump (Comment => F.Comment,
                  F       => File,
                  Indent  => 2,
                  Inline  => False);
         end if;

         declare
            Id   : constant String := Get_Id (F);
            Line : constant String :=
              (1 .. 6 => ' ') & Id & " : " &
              (if F.Properties.Is_Aliased then "aliased " else "") &
              To_String (F.Typ);
         begin
            Ada.Text_IO.Put (File, Line);

            if F.Has_Default then
               declare
                  Val : constant String := To_String (F.Default);
               begin
                  if Line'Length + 5 + Val'Length >= Max_Width then
                     Ada.Text_IO.Put_Line (File, " :=");
                     Ada.Text_IO.Put_Line (File, (1 .. Id'Length + 10 => ' ') &
                                             Val & ";");
                  else
                     Ada.Text_IO.Put_Line
                       (File, " := " & To_String (F.Default) & ";");
                  end if;
               end;

            else
               Ada.Text_IO.Put_Line (File, ";");
            end if;

            if F.Properties.Is_Volatile_FA then
               Ada.Text_IO.Put (File, (1 .. 6 => ' '));
               Ada.Text_IO.Put_Line
                 (File,
                  "pragma Volatile_Full_Access (" & To_String (F.Id) & ");");
            end if;
         end;
      end loop;
   end Dump_Record_Fields;

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

   overriding procedure Dump
     (Element : Ada_Type_Record;
      File    : Ada.Text_IO.File_Type)
   is
      Max_Id  : Natural := 0;
      As_Hex  : Boolean := False;

      function Get_Id (F : Record_Field) return String;

      function Get_Id (F : Record_Field) return String
      is
         Id : String (1 .. Max_Id) := (others => ' ');
      begin
         Id (1 .. Length (F.Id)) := To_String (F.Id);

         return Id;
      end Get_Id;
   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;

      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Ada.Text_IO.Put_Line
        (File, "   type " & To_String (Element.Id) & " is record");

      for F of Element.Fields loop
         Max_Id  := Natural'Max (Length (F.Id), Max_Id);
      end loop;

      Dump_Record_Fields (Element, Max_Id, File);

      Ada.Text_IO.Put_Line (File, "   end record");
      Dump_Aspects (Element.Aspects, File);
      Ada.Text_IO.New_Line (File);

      Ada.Text_IO.Put_Line
        (File, "   for " & To_String (Element.Id) & " use record");

      for F of Element.Fields loop
         if F.Offset /= 0 then
            As_Hex := True;
            exit;
         end if;
      end loop;

      for F of Element.Fields loop
         Ada.Text_IO.Put (File, (1 .. 6 => ' '));
         Ada.Text_IO.Put_Line
           (File,
            Get_Id (F) &
              " at " &
              (if As_Hex
                 then To_Hex (F.Offset)
                 else To_String (F.Offset)) &
              " range " & To_String (F.LSB) & " .. " & To_String (F.MSB) &
              ";");
      end loop;

      Ada.Text_IO.Put_Line (File, "   end record;");
      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Type_Union;
      File    : Ada.Text_IO.File_Type)
   is
      Max_Id : Natural := 0;
      As_Hex : Boolean := False;

      function Get_Id (F : Record_Field) return String;

      function Get_Id (F : Record_Field) return String
      is
         Id : String (1 .. Max_Id) := (others => ' ');
      begin
         Id (1 .. Length (F.Id)) := To_String (F.Id);
         return Id;
      end Get_Id;

   begin
      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Ada.Text_IO.Put_Line
        (File,
         "   type " & To_String (Element.Id));
      Ada.Text_IO.Put_Line
        (File, "     (" & To_String (Element.Disc_Name) &
           " : " & To_String (Element.Discriminent.Id) &
           " := " & To_String (Element.Discriminent.Values.First_Element.Id) &
           ")");
      Ada.Text_IO.Put_Line (File, "   is record");

      --  First dump the values that are common to all discriminent values
      for F of Element.Fields loop
         Max_Id  := Natural'Max (Length (F.Id), Max_Id);
      end loop;

      for Vect of Element.Disc_Fields loop
         for F of Vect loop
            Max_Id := Natural'Max (Length (F.Id), Max_Id);
         end loop;
      end loop;

      Dump_Record_Fields (Element, Max_Id, File);

      Ada.Text_IO.Put (File, (1 .. 2 * 3 => ' '));
      Ada.Text_IO.Put_Line
        (File, "case " & To_String (Element.Disc_Name) & " is");

      for Val of Element.Discriminent.Values loop
         Ada.Text_IO.Put (File, (1 .. 3 * 3 => ' '));
         Ada.Text_IO.Put_Line (File, "when " & To_String (Val.Id) & " =>");

         for F of Element.Disc_Fields (To_String (Val.Id)) loop
            if not F.Comment.Is_Empty then
               Dump (Comment => F.Comment,
                     F       => File,
                     Indent  => 4,
                     Inline  => False);
            end if;
            Ada.Text_IO.Put (File, (1 .. 4 * 3 => ' '));
            Ada.Text_IO.Put_Line
              (File, To_String (F.Id) & " : " &
                 (if F.Properties.Is_Aliased then "aliased " else "") &
                 To_String (F.Typ) & ";");
            if F.Properties.Is_Volatile_FA then
               Ada.Text_IO.Put (File, (1 .. 4 * 3 => ' '));
               Ada.Text_IO.Put_Line
                 (File,
                  "pragma Volatile_Full_Access (" & To_String (F.Id) & ");");
            end if;
         end loop;
      end loop;

      Ada.Text_IO.Put (File, (1 .. 2 * 3 => ' '));
      Ada.Text_IO.Put_Line (File, "end case;");
      Ada.Text_IO.Put_Line (File, "   end record");
      Dump_Aspects (Element.Aspects, File);
      Ada.Text_IO.New_Line (File);

      Ada.Text_IO.Put_Line
        (File, "   for " & To_String (Element.Id) & " use record");

      --  If one of the field offset is different from 0, dump the offsets
      --  as hexa values. Else just dump a regular decimal value.
      for F of Element.Fields loop
         if F.Offset /= 0 then
            As_Hex := True;
            exit;
         end if;
      end loop;

      for Val of Element.Discriminent.Values loop
         exit when As_Hex;
         for F of Element.Disc_Fields (To_String (Val.Id)) loop
            if F.Offset /= 0 then
               As_Hex := True;
               exit;
            end if;
         end loop;
      end loop;

      for F of Element.Fields loop
         Ada.Text_IO.Put (File, (1 .. 6 => ' '));
         Ada.Text_IO.Put_Line
           (File,
            Get_Id (F) &
              " at " &
              (if As_Hex
                 then To_Hex (F.Offset)
                 else To_String (F.Offset)) &
              " range " & To_String (F.LSB) & " .. " & To_String (F.MSB) &
              ";");
      end loop;

      for Val of Element.Discriminent.Values loop
         for F of Element.Disc_Fields (To_String (Val.Id)) loop
            Ada.Text_IO.Put (File, (1 .. 6 => ' '));
            Ada.Text_IO.Put_Line
              (File,
               Get_Id (F) &
                 " at " &
                 (if As_Hex
                   then To_Hex (F.Offset)
                   else To_String (F.Offset)) &
                 " range " & To_String (F.LSB) & " .. " & To_String (F.MSB) &
                 ";");
         end loop;
      end loop;

      Ada.Text_IO.Put_Line (File, "   end record;");
      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Constant_Value;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      if not Element.Comment.Is_Empty then
         if not G_Empty_Line then
            Ada.Text_IO.New_Line (File);
            G_Empty_Line := True;
         end if;

         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Ada.Text_IO.Put (File, "   ");

      if Element.Id_Size > Length (Element.Id) then
         declare
            Str : String (1 .. Element.Id_Size) := (others => ' ');
            S   : constant String := To_String (Element.Id);
         begin
            Str (1 .. S'Length) := S;
            Ada.Text_IO.Put (File, Str);
         end;
      else
         Ada.Text_IO.Put (File, To_String (Element.Id));
      end if;

      Ada.Text_IO.Put (File, " : constant ");

      if Length (Element.Type_Name) > 0 then
         Ada.Text_IO.Put (File, To_String (Element.Type_Name) & " ");
      end if;

      declare
         Value : constant String := To_String (Element.Value);
      begin
         if Value'Length <= 40 then --  arbitrary
            Ada.Text_IO.Put_Line (File, ":= " & Value & ";");
         else
            Ada.Text_IO.Put_Line (File, ":=");
            Ada.Text_IO.Put_Line (File, "     " & Value & ";");
         end if;
      end;
      G_Empty_Line := False;
   end Dump;

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

   overriding procedure Dump
     (Element : Ada_Variable;
      File    : Ada.Text_IO.File_Type)
   is
   begin
      if not G_Empty_Line then
         Ada.Text_IO.New_Line (File);
      end if;

      if not Element.Comment.Is_Empty then
         Dump (Comment => Element.Comment,
               F       => File,
               Indent  => 1,
               Inline  => False);
      end if;

      Ada.Text_IO.Put (File, "   " & To_String (Element.Id) & " : ");

      if Element.Aliasd then
         Ada.Text_IO.Put (File, "aliased ");
      end if;

      Ada.Text_IO.Put (File, To_String (Element.Type_Name));

      if Element.Aspects.Is_Empty then
         Ada.Text_IO.Put_Line (File, ";");
      else
         Ada.Text_IO.New_Line (File);
         Dump_Aspects (Element.Aspects, File);
      end if;

      Ada.Text_IO.New_Line (File);
      G_Empty_Line := True;
   end Dump;

   --------------
   -- New_Spec --
   --------------

   function New_Spec
     (Name          : String;
      Descr         : String;
      Preelaborated : Boolean)
      return Ada_Spec
   is
      Spec : Ada_Spec;
   begin
      Spec.Id := Ada_Identifier (Name, "");
      Spec.Comment := New_Comment (Descr, False);
      Spec.Preelaborated := Preelaborated;

      if Length (G_Withed_All) /= 0 then
         Spec.With_Clauses.Insert
           (To_String (G_Withed_All), False);
      end if;

      return Spec;
   end New_Spec;

   --------------------
   -- New_Child_Spec --
   --------------------

   function New_Child_Spec
     (Name          : String;
      Parent        : String;
      Descr         : String;
      Preelaborated : Boolean)
      return Ada_Spec
   is
   begin
      return New_Spec
        (Parent & "." & To_String (Ada_Identifier (Name, Parent)),
         Descr, Preelaborated);
   end New_Child_Spec;

   --------
   -- Id --
   --------

   function Id (Spec : Ada_Spec) return Unbounded_String
   is
   begin
      return Spec.Id;
   end Id;

   ---------------------
   -- Add_Global_With --
   ---------------------

   procedure Add_Global_With (Spec : String)
   is
   begin
      G_Withed_All := To_Unbounded_String (Spec);
   end Add_Global_With;

   ---------------
   -- File_Name --
   ---------------

   function File_Name (Spec : Ada_Spec) return String
   is
      Ada_Name : String := To_String (Spec.Id);
   begin
      for J in Ada_Name'Range loop
         Ada_Name (J) :=
           Ada.Characters.Handling.To_Lower (Ada_Name (J));

         if Ada_Name (J) = '.' then
            Ada_Name (J) := '-';
         end if;
      end loop;

      if Ada_Name = "ada-interrupts-names" then
         return "a-intnam.ads";
      end if;

      if Ada_Name'Length >= 10
        and then Ada_Name (1 .. 10) = "interfaces"
        and then
          (Ada_Name'Last = 10
           or else Ada_Name (11) = '-')
      then
         return "i" & Ada_Name (11 .. Ada_Name'Last) & ".ads";
      end if;

      return Ada_Name & ".ads";
   end File_Name;

   ----------------
   -- Write_Spec --
   ----------------

   procedure Write_Spec
     (Spec       : Ada_Spec;
      Output_Dir : String)
   is
      function Spec_Id_Starts_With (Ptrn : String) return Boolean;

      -----------------
      -- Starts_With --
      -----------------

      function Spec_Id_Starts_With (Ptrn : String) return Boolean
      is
      begin
         return Length (Spec.Id) > Ptrn'Length
           and then Slice (Spec.Id, 1, Ptrn'Length) = Ptrn;
      end Spec_Id_Starts_With;

      Full      : constant String :=
                    GNAT.OS_Lib.Normalize_Pathname (Output_Dir);
      F_Name    : constant String :=
                    GNAT.OS_Lib.Normalize_Pathname
                      (File_Name (Spec),
                       Full);
      F         : Ada.Text_IO.File_Type;
      Max_Width : Natural := 0;
      Curs      : With_Maps.Cursor;

   begin
      if not GNAT.OS_Lib.Is_Directory (Full) then
         Ada.Text_IO.Put_Line ("Creating Dir " & Full);
         GNAT.Directory_Operations.Make_Dir (Full);
      end if;

      Ada.Text_IO.Create (F, Ada.Text_IO.Out_File,
                          F_Name);

      if Spec_Id_Starts_With ("Interfaces.")
        or else Spec_Id_Starts_With ("Ada.")
      then
         --  Add the AdaCore copyright notice to the spec.
         Ada.Text_IO.Put_Line
           (F, "--");
         Ada.Text_IO.Put_Line
           (F, "--  Copyright (C)" &
              Ada.Calendar.Year (Ada.Calendar.Clock)'Img &
              ", AdaCore");
         Ada.Text_IO.Put_Line
           (F, "--");
         Ada.Text_IO.New_Line (F);
      end if;

      Ada.Text_IO.Put_Line (F, "pragma Style_Checks (Off);");
      Ada.Text_IO.New_Line (F);

      if Length (Trim (G_License_Text, Both)) > 0 then
         Ada.Text_IO.Put_Line (F, To_String (G_License_Text));
         Ada.Text_IO.New_Line (F);
      end if;

      Ada.Text_IO.Put_Line
        (F,
         "--  This spec has been automatically generated from " &
           To_String (G_Input_File));
      Ada.Text_IO.New_Line (F);
      G_Empty_Line := True;

      if Spec.Preelaborated then
         if not SVD2Ada_Utils.In_Runtime then
            --  When part of the runtime, we need the more strict
            --  No_Elaboration_Code_All
            Ada.Text_IO.Put_Line
              (F, "pragma Restrictions (No_Elaboration_Code);");
         end if;

         Ada.Text_IO.New_Line (F);
         G_Empty_Line := True;
      end if;

      Curs := Spec.With_Clauses.First;

      while With_Maps.Has_Element (Curs) loop
         Max_Width :=
           Natural'Max (Max_Width, With_Maps.Key (Curs)'Length);
         With_Maps.Next (Curs);
      end loop;

      Curs := Spec.With_Clauses.First;

      while With_Maps.Has_Element (Curs) loop
         declare
            W     : constant String := With_Maps.Key (Curs);
            Use_C : constant Boolean := With_Maps.Element (Curs);
         begin
            Ada.Text_IO.Put (F, "with " & W & ";");

            if Use_C then
               declare
                  Space : constant String (W'Length .. Max_Width + 1) :=
                            (others => ' ');
               begin
                  Ada.Text_IO.Put (F, Space);
               end;
               Ada.Text_IO.Put (F, "use " & W & ";");
            end if;

            Ada.Text_IO.New_Line (F);
            G_Empty_Line := False;
            With_Maps.Next (Curs);
         end;
      end loop;

      if not G_Empty_Line then
         Ada.Text_IO.New_Line (F);
      end if;

      if not Spec.Comment.Is_Empty then
         Spec.Comment.Dump (F, Indent => 0, Inline => False);
      end if;

      Ada.Text_IO.Put_Line (F, "package " & To_String (Spec.Id) & " is");
      if Spec.Preelaborated then
         Ada.Text_IO.Put_Line
           (F, "   pragma Preelaborate;");
         if SVD2Ada_Utils.In_Runtime then
            Ada.Text_IO.Put_Line
              (F, "   pragma No_Elaboration_Code_All;");
         end if;
      end if;
      G_Empty_Line := False;

      for Elt of Spec.Elements loop
         Dump (Elt, F);
      end loop;

      if not G_Empty_Line then
         Ada.Text_IO.New_Line (F);
      end if;

      Ada.Text_IO.Put_Line (F, "end " & To_String (Spec.Id) & ";");

      Ada.Text_IO.Close (F);
   end Write_Spec;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_With_Clause)
   is
      With_Pkg : String renames To_String (Elt.Pkg);
   begin
      if Is_Parent (Spec, Elt) then
         return;
      end if;

      if Spec.With_Clauses.Contains (With_Pkg) then
         if Elt.Add_Use_Clause then
            --  Make sure we have use visibility for this package
            Spec.With_Clauses.Replace (With_Pkg, True);
         end if;

         return;
      end if;

      Spec.With_Clauses.Insert (With_Pkg, Elt.Add_Use_Clause);
   end Add;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_Comment)
   is
   begin
      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

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

   overriding
   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_Comment_Box)
   is
   begin
      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

   ------------------
   -- Add_No_Check --
   ------------------

   procedure Add_No_Check
     (Spec : in out Ada_Spec;
      Elt  : Ada_Type'Class)
   is
      use type Ada.Tags.Tag;
   begin
      for Prev of Spec.Elements loop
         if Prev'Tag = Elt'Tag then
            declare
               Prev_T : constant Ada_Type'Class := Ada_Type'Class (Prev);
            begin
               if Prev_T.Id = Elt.Id then
                  if Is_Similar (Elt, Prev_T) then
                     return;
                  else
                     raise Constraint_Error with
                       "Two different types with the same Id: " &
                       To_String (Elt.Id);
                  end if;
               end if;
            end;
         end if;
      end loop;

      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add_No_Check;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : in out Ada_Type'Class)
   is
      use type Ada.Tags.Tag;
      Retry     : Boolean := True;
      Idx       : Natural := 0;
      Name_Orig : constant Unbounded_String := Elt.Id;

   begin
      while Retry loop
         Retry := False;

         for Prev of Spec.Elements loop
            if Prev in Ada_Type'Class then
               declare
                  Prev_T : constant Ada_Type'Class := Ada_Type'Class (Prev);
               begin
                  if Prev_T.Id = Elt.Id then
                     if Elt'Tag = Prev_T'Tag
                       and then Is_Similar (Elt, Prev_T)
                     then
                        return;
                     else
                        Idx := Idx + 1;
                        Elt.Id := Name_Orig & "_" & To_String (Idx);
                        Retry := True;

                        exit;
                     end if;
                  end if;
               end;
            end if;
         end loop;
      end loop;

      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_Constant_Value)
   is
   begin
      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_Variable)
   is
   begin
      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

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

   procedure Add
     (Spec : in out Ada_Spec;
      Elt  : Ada_Pragma)
   is
   begin
      Spec.Elements.Append (Elt);
      Elt.Added_In_Spec (Spec);
   end Add;

   ---------------------
   -- New_With_Clause --
   ---------------------

   function New_With_Clause
     (Pkg         : String;
      Use_Visible : Boolean := False)
      return Ada_With_Clause
   is
   begin
      return (Pkg            => To_Unbounded_String (Pkg),
              Add_Use_Clause => Use_Visible);
   end New_With_Clause;

   ---------------
   -- Is_Parent --
   ---------------

   function Is_Parent
     (Spec        : Ada_Spec;
      With_Clause : Ada_With_Clause)
      return Boolean
   is
      Spec_Lower : constant String :=
                     Ada.Characters.Handling.To_Lower (To_String (Spec.Id));
      With_Lower : constant String :=
                     Ada.Characters.Handling.To_Lower
                       (To_String (With_Clause.Pkg));
   begin
      return With_Lower'Length < Spec_Lower'Length
        and then Spec_Lower (1 .. With_Lower'Length + 1) = (With_Lower & '.');
   end Is_Parent;

   -----------------
   -- New_Comment --
   -----------------

   function New_Comment
     (Comment : String;
      Strip   : Boolean)
      return Ada_Comment
   is
      function Strip_String (Str : String) return String;

      function Strip_String (Str : String) return String
      is
         Result  : String (Str'Range);
         Idx     : Natural := Result'First;
         Prev    : Character := ' ';
         Current : Character := ' ';
      begin
         for J in Str'Range loop
            if Str (J) = ASCII.CR
              or else Str (J) = ASCII.LF
            then
               Current := ' ';
            else
               Current := Str (J);
            end if;

            if Current /= ' ' or else Prev /= ' ' then
               Result (Idx) := Current;
               Idx := Idx + 1;
            end if;

            Prev := Current;
         end loop;

         --  Removes trailing spaces
         if Current = ' ' then
            Idx := Idx - 1;
         end if;

         return Result (Result'First .. Idx - 1);
      end Strip_String;

   begin
      if Strip then
         return (Comment => To_Unbounded_String
                 (Strip_String
                    (Ada.Strings.Fixed.Trim (Comment, Ada.Strings.Both))));
      else
         return (Comment => To_Unbounded_String (Comment));
      end if;
   end New_Comment;

   ---------------------
   -- New_Comment_Box --
   ---------------------

   function New_Comment_Box
     (Comment : String)
      return Ada_Comment_Box
   is
      C : constant Ada_Comment := New_Comment (Comment, Strip => True);
   begin
      return (Comment => C.Comment);
   end New_Comment_Box;

   ----------------
   -- New_Pragma --
   ----------------

   function New_Pragma
     (Identifier : String;
      Comment    : String := "")
      return Ada_Pragma
   is
      C : constant Ada_Pragma :=
            (Id      => To_Unbounded_String (Identifier),
             Comment => New_Comment (Comment, Strip => True));
   begin
      return C;
   end New_Pragma;

   --------
   -- Id --
   --------

   function Id (Elt : Ada_Type'Class) return Unbounded_String
   is
   begin
      return Elt.Id;
   end Id;

   function Id (Elt : Ada_Type'Class) return String
   is
   begin
      return To_String (Elt.Id);
   end Id;

   ----------------
   -- Add_Aspect --
   ----------------

   procedure Add_Aspect
     (Elt         : in out Ada_Type'Class;
      Aspect_Mark : String;
      Value       : Integer)
   is
      Specified_Value : String renames To_String (Value);
      Id_With_Arrow   : constant String := Aspect_Mark & " => ";
   begin
      for Aspect_Spec of Elt.Aspects loop
         declare
            subtype Id_With_Arrow_Slice is Integer range
              Aspect_Spec'First .. Aspect_Spec'First + Id_With_Arrow'Length - 1;
            subtype Value_Slice is Integer range
              Aspect_Spec'First + Id_With_Arrow'Length .. Aspect_Spec'Last;
         begin
            if Aspect_Spec'Length > Id_With_Arrow'Length
              and then Aspect_Spec (Id_With_Arrow_Slice) = Id_With_Arrow
            then  -- already specified this same aspect for this type
               if Aspect_Spec (Value_Slice) /= Specified_Value then
                  raise Constraint_Error with
                    Aspect_Mark & " is already specified for " & To_String (Elt.Id);
               end if;
               --  It is OK to specify a value that is the same as that which was
               --  already specified previously
               return;
            end if;
         end;
      end loop;

      Elt.Aspects.Append (Id_With_Arrow & Specified_Value);
   end Add_Aspect;

   ------------------
   -- Aspect_Value --
   ------------------

   function Aspect_Value
     (Elt         : Ada_Type'Class;
      Aspect_Mark : String)
      return Integer
   is
      Id_With_Arrow : constant String := Aspect_Mark & " => ";
   begin
      for Aspect_Spec of Elt.Aspects loop
         declare
            subtype Id_With_Arrow_Slice is Integer range
              Aspect_Spec'First .. Aspect_Spec'First + Id_With_Arrow'Length - 1;
            subtype Value_Slice is Integer range
              Aspect_Spec'First + Id_With_Arrow'Length .. Aspect_Spec'Last;
         begin
            if Aspect_Spec'Length > Id_With_Arrow'Length
              and then Aspect_Spec (Id_With_Arrow_Slice) = Id_With_Arrow
            then
               return Integer'Value (Aspect_Spec (Value_Slice));
            end if;
         end;
      end loop;

      return 0;
   end Aspect_Value;

   ---------------------
   -- Add_Size_Aspect --
   ---------------------

   procedure Add_Size_Aspect
     (Elt  : in out Ada_Type'Class;
      Size : Natural)
   is
   begin
      Add_Aspect (Elt, Aspect_Mark => "Size", Value => Size);
   end Add_Size_Aspect;

   ---------------------
   -- Get_Size_Aspect --
   ---------------------

   function Get_Size_Aspect (Elt  : Ada_Type'Class) return Unsigned
   is
   begin
      return Unsigned'Mod (Aspect_Value (Elt, Aspect_Mark => "Size"));
   end Get_Size_Aspect;

   ----------------------------
   -- Add_Object_Size_Aspect --
   ----------------------------

   procedure Add_Object_Size_Aspect
     (Elt  : in out Ada_Type'Class;
      Size : Natural)
   is
   begin
      Add_Aspect (Elt, Aspect_Mark => "Object_Size", Value => Size);
   end Add_Object_Size_Aspect;

   ----------------------------
   -- Get_Object_Size_Aspect --
   ----------------------------

   function Get_Object_Size_Aspect (Elt  : Ada_Type'Class) return Unsigned
   is
   begin
      return Unsigned'Mod (Aspect_Value (Elt, Aspect_Mark => "Object_Size"));
   end Get_Object_Size_Aspect;

   ----------------
   -- Add_Aspect --
   ----------------

   procedure Add_Aspect
     (Elt         : in out Ada_Type'Class;
      Aspect_Mark : String)
   is
   begin
      for A of Elt.Aspects loop
         if Aspect_Mark = A then
            --  Aspect already defined
            return;
         end if;
      end loop;

      Elt.Aspects.Append (Aspect_Mark);
   end Add_Aspect;

   ---------------------
   -- New_Type_Scalar --
   ---------------------

   function New_Type_Scalar
     (Id      : String;
      Size    : Natural;
      Comment : String := "")
      return Ada_Type_Scalar
   is
      Result : Ada_Type_Scalar;
   begin
      Result.Id      := Ada_Identifier (Id, "Scalar");
      Result.Comment := New_Comment (Comment, Strip => True);
      Result.Size    := Size;
      Add_Size_Aspect (Result, Size);
      return Result;
   end New_Type_Scalar;

   -----------------
   -- Target_Type --
   -----------------

   function Target_Type
     (Size            : Natural;
      Fully_Qualified : Boolean := True)
      return Ada_Type'Class
   is
      Result : Ada_Type_Scalar;
   begin
      Result.Id   :=
        To_Unbounded_String (Base_Types.Target_Type (Size, Fully_Qualified));
      Result.Size := Size;

      return Result;
   end Target_Type;

   ---------------------
   -- New_Type_Scalar --
   ---------------------

   function New_Subype_Scalar
     (Id      : String;
      Typ     : Ada_Type'Class;
      Comment : String := "")
      return Ada_Subtype_Scalar
   is
   begin
      return (Id      => Ada_Identifier (Id, "Scalar"),
              Comment => New_Comment (Comment, Strip => True),
              Typ     => Typ.Id,
              others  => <>);
   end New_Subype_Scalar;

   -------------------
   -- Added_In_Spec --
   -------------------

   overriding procedure Added_In_Spec
     (Element : Ada_Type_Scalar;
      Spec    : in out Ada_Spec)
   is
   begin
      if Element.Size in 8 | 16 | 32 | 64 then
         Add (Spec, New_With_Clause ("Interfaces", Use_Visible => False));
      end if;
   end Added_In_Spec;

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Type_Scalar)
      return Boolean
   is
   begin
      return T1.Id = T2.Id
        and then T1.Size = T2.Size;
   end Is_Similar;

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Subtype_Scalar)
      return Boolean
   is
   begin
      return T1.Id = T2.Id
        and then T1.Typ = T2.Typ;
   end Is_Similar;

   --------------------
   -- New_Type_Array --
   --------------------

   function New_Type_Array
     (Id           : String;
      Index_Type   : String;
      Index_First  : Natural;
      Index_Last   : Natural;
      Element_Type : Ada_Type'Class;
      Comment      : String := "")
      return Ada_Type_Array
   is
   begin
      return (Id           => Ada_Identifier (Id, "Arr"),
              Comment      => New_Comment (Comment, Strip => True),
              Aspects      => <>,
              Index_Type   => To_Unbounded_String (Index_Type),
              Index_First  => Index_First,
              Index_Last   => Index_Last,
              Element_Type => Element_Type.Id);
   end New_Type_Array;

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Type_Array)
      return Boolean
   is
   begin
      return T1.Id = T2.Id
        and then T1.Index_First = T2.Index_First
        and then T1.Index_Last = T2.Index_Last
        and then T1.Element_Type = T2.Element_Type;
   end Is_Similar;

   -----------------
   -- Get_Boolean --
   -----------------

   function Get_Boolean return Ada_Type_Enum
   is
      Result : Ada_Type_Enum;
      Dead   : Ada_Enum_Value;
      Dead2  : constant Ada_Spec := New_Spec ("Standard", "", False);
   begin
      Result := (Id      => To_Unbounded_String ("Boolean"),
                 Comment => New_Comment ("", False),
                 Aspects => <>,
                 Values  => <>);
      Dead := Add_Enum_Id (Dead2, Result, "False", 0);
      Dead := Add_Enum_Id (Dead2, Result, "True", 0);

      return Result;
   end Get_Boolean;

   -------------------
   -- New_Type_Enum --
   -------------------

   function New_Type_Enum
     (Id      : String;
      Size    : Natural := 0;
      Comment : String := "")
      return Ada_Type_Enum
   is
      Result : Ada_Type_Enum;
   begin
      Result := (Id      => Ada_Identifier (Id, "E"),
                 Comment => New_Comment (Comment, Strip => True),
                 Aspects => <>,
                 Values  => <>);
      if Size > 0 then
         Add_Size_Aspect (Result, Size);
      end if;

      return Result;
   end New_Type_Enum;

   --------------------------
   -- Add_Enum_Id_Internal --
   --------------------------

   function Add_Enum_Id_Internal
     (Spec     : Ada_Spec;
      Enum     : in out Ada_Type_Enum;
      Id       : String;
      Has_Repr : Boolean;
      Repr     : Unsigned;
      Comment  : String := "")
      return Ada_Enum_Value
   is
      Enum_Value : Ada_Enum_Value;
      The_Id     : Unbounded_String;
      Suffix     : Natural := 0;
      Done       : Boolean;
   begin
      The_Id := Ada_Identifier (Id, "Val");

      --  Check duplicated names
      Suffix := 0;
      Enum_Value := (Id       => The_Id,
                     Has_Repr => Has_Repr,
                     Repr     => Repr,
                     Comment  => New_Comment (Comment, Strip => True));

      loop
         Done   := True;

         for J in Enum.Values.First_Index .. Enum.Values.Last_Index loop
            if Enum.Values (J).Id = The_Id then
               Suffix := Suffix + 1;
               Done   := False;
               The_Id := Enum_Value.Id & "_" & To_String (Suffix);

               exit;
            end if;
         end loop;

         for Elt of Spec.Elements loop
            if Elt in Ada_Type'Class
              and then Ada.Strings.Unbounded.Equal_Case_Insensitive
                (The_Id, Ada_Type'Class (Elt).Id)
            then
               Done := False;
               Suffix := Suffix + 1;
               The_Id := Enum_Value.Id & "_" & To_String (Suffix);

               exit;
            end if;
         end loop;

         exit when Done;
      end loop;

      Enum_Value.Id := The_Id;

      --  Insert the value as an ordered list, as mandated by Ada
      if Has_Repr then
         for J in Enum.Values.First_Index .. Enum.Values.Last_Index loop
            --  simple search here, we don't really care about the bad perfs
            if Enum.Values (J).Repr > Repr then
               Enum.Values.Insert (J, Enum_Value);
               return Enum_Value;
            end if;
         end loop;
      end if;

      --  List is empty for now, just append it
      Enum.Values.Append (Enum_Value);

      return Enum_Value;
   end Add_Enum_Id_Internal;

   -----------------
   -- Add_Enum_Id --
   -----------------

   function Add_Enum_Id
     (Spec    : Ada_Spec;
      Enum    : in out Ada_Type_Enum;
      Id      : String;
      Comment : String := "")
      return Ada_Enum_Value
   is
   begin
      return Add_Enum_Id_Internal (Spec, Enum, Id, False, 0, Comment);
   end Add_Enum_Id;

   -----------------
   -- Add_Enum_Id --
   -----------------

   function Add_Enum_Id
     (Spec    : Ada_Spec;
      Enum    : in out Ada_Type_Enum;
      Id      : String;
      Repr    : Unsigned;
      Comment : String := "")
      return Ada_Enum_Value
   is
   begin
      return Add_Enum_Id_Internal (Spec, Enum, Id, True, Repr, Comment);
   end Add_Enum_Id;

   --------
   -- Id --
   --------

   function Id (Elt : Ada_Enum_Value) return Unbounded_String
   is
   begin
      return Elt.Id;
   end Id;

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

   overriding
   function "=" (V1, V2 : Ada_Enum_Value) return Boolean
   is
   begin
      return V1.Id = V2.Id
        and then V1.Has_Repr = V2.Has_Repr
        and then (if V1.Has_Repr then V1.Repr = V2.Repr else True);
   end "=";

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Type_Enum)
      return Boolean
   is
      use type Enum_Value_Vectors.Vector;
   begin
      return T1.Id = T2.Id
        and then T1.Values = T2.Values;
   end Is_Similar;

   ---------------------
   -- New_Type_Record --
   ---------------------

   function New_Type_Record
     (Id      : String;
      Comment : String := "")
      return Ada_Type_Record'Class
   is
   begin
      return Ada_Type_Record'
        (Id          => Ada_Identifier (Id, "Rec"),
         Comment     => New_Comment (Comment, Strip => True),
         Aspects     => <>,
         Fields      => <>,
         Need_System => False);
   end New_Type_Record;

   --------------------------
   -- Add_Bit_Order_Aspect --
   --------------------------

   procedure Add_Bit_Order_Aspect
     (Elt   : in out Ada_Type_Record'Class;
      Order : System.Bit_Order)
   is
   begin
      case Order is
         when System.High_Order_First =>
            Add_Aspect (Elt, "Bit_Order => System.High_Order_First");
         when System.Low_Order_First =>
            Add_Aspect (Elt, "Bit_Order => System.Low_Order_First");
      end case;

      Elt.Need_System := True;
   end Add_Bit_Order_Aspect;

   -------------------
   -- Added_In_Spec --
   -------------------

   overriding procedure Added_In_Spec
     (Element : Ada_Type_Record;
      Spec    : in out Ada_Spec)
   is
   begin
      if Element.Need_System then
         Add (Spec, New_With_Clause ("System"));
      end if;
   end Added_In_Spec;

   --------------
   -- Simplify --
   --------------

   function Simplify
     (Elt  : Ada_Type_Record;
      Spec : in out Ada_Spec)
      return Ada_Type'Class
   is
      function Find (Typ : Unbounded_String) return Ada_Type'Class;

      ----------
      -- Find --
      ----------

      function Find (Typ : Unbounded_String) return Ada_Type'Class
      is
         Curs : Element_Vectors.Cursor := Spec.Elements.First;
         use Element_Vectors;
      begin
         while Has_Element (Curs) loop
            if Element (Curs) in Ada_Type'Class
              and then Ada_Type'Class (Element (Curs)).Id = Typ
            then
               declare
                  Result : constant Ada_Type'Class := Ada_Type'Class (Element (Curs));
               begin
                  Spec.Elements.Delete (Curs);
                  return Result;
               end;
            end if;

            Next (Curs);
         end loop;

         raise Constraint_Error with "Cannot find type " & To_String (Typ);
      end Find;

      use type Ada.Containers.Count_Type;

   begin
      if Elt.Fields.Length /= 1 then
         return Elt;
      end if;

      declare
         Typ : Ada_Type'Class := Find (Elt.Fields.First_Element.Typ);
      begin
         Typ.Id := Elt.Id;
         for Aspect of Elt.Aspects loop
            if not Typ.Aspects.Contains (Aspect) then
               Typ.Aspects.Append (Aspect);
            end if;
         end loop;
         Typ.Comment := Elt.Comment;

         return Typ;
      end;
   end Simplify;

   ------------------------
   -- Add_Field_Internal --
   ------------------------

   procedure Add_Field_Internal
     (Rec         : in out Ada_Type_Record'Class;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Has_Default : Boolean;
      Default     : Unbounded_String;
      Properties  : Field_Properties;
      Comment     : String := "")
   is
      Idx     : Natural := 0;
      The_Id  : constant Unbounded_String := Ada_Identifier (Id, To_String (Rec.Id));
      Current : Unbounded_String := The_Id;
   begin
      for F of Rec.Fields loop
         if F.Id = Current then
            Idx := Idx + 1;
            Current := The_Id & "_" & To_String (Idx);
         end if;
      end loop;

      Rec.Fields.Append
        ((Id          => Current,
          Typ         => Typ.Id,
          Offset      => Offset,
          LSB         => LSB,
          MSB         => MSB,
          Has_Default => Has_Default,
          Default     => Default,
          Properties  => Properties,
          Comment     => New_Comment (Comment, Strip => True)));
   end Add_Field_Internal;

   ---------------
   -- Add_Field --
   ---------------

   procedure Add_Field
     (Rec         : in out Ada_Type_Record'Class;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Properties  : Field_Properties;
      Comment     : String := "")
   is
   begin
      Add_Field_Internal
        (Rec,
         Id          => Id,
         Typ         => Typ,
         Offset      => Offset,
         LSB         => LSB,
         MSB         => MSB,
         Has_Default => False,
         Default     => Null_Unbounded_String,
         Properties  => Properties,
         Comment     => Comment);
   end Add_Field;

   ---------------
   -- Add_Field --
   ---------------

   procedure Add_Field
     (Rec         : in out Ada_Type_Record'Class;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Default     : Unsigned;
      Properties  : Field_Properties;
      Comment     : String := "")
   is
   begin
      Add_Field_Internal
        (Rec,
         Id          => Id,
         Typ         => Typ,
         Offset      => Offset,
         LSB         => LSB,
         MSB         => MSB,
         Has_Default => True,
         Default     => To_Unbounded_String (To_Hex (Default)),
         Properties  => Properties,
         Comment     => Comment);
   end Add_Field;

   ---------------
   -- Add_Field --
   ---------------

   procedure Add_Field
     (Rec         : in out Ada_Type_Record'Class;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Default     : Unbounded_String;
      Properties  : Field_Properties;
      Comment     : String := "")
   is
   begin
      Add_Field_Internal
        (Rec,
         Id          => Id,
         Typ         => Typ,
         Offset      => Offset,
         LSB         => LSB,
         MSB         => MSB,
         Has_Default => True,
         Default     => Default,
         Properties  => Properties,
         Comment     => Comment);
   end Add_Field;

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

   overriding
   function "=" (R1, R2 : Record_Field) return Boolean
   is
   begin
      return R1.Id = R2.Id
        and then R1.Typ = R2.Typ
        and then R1.Offset = R2.Offset
        and then R1.LSB = R2.LSB
        and then R1.Has_Default = R2.Has_Default
        and then (if R1.Has_Default then R1.Default = R2.Default else True);
   end "=";

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Type_Record)
      return Boolean
   is
      use type Record_Field_Vectors.Vector;
   begin
      return T1.Id = T2.Id
        and then T1.Fields = T2.Fields;
   end Is_Similar;

   --------------------
   -- New_Type_Union --
   --------------------

   function New_Type_Union
     (Id        : String;
      Disc_Name : String;
      Disc_Type : Ada_Type_Enum'Class;
      Comment   : String := "")
      return Ada_Type_Union
   is
      Result : Ada_Type_Union;
   begin
      Result :=
        (Id           => Ada_Identifier (Id, "Rec"),
         Comment      => New_Comment (Comment, Strip => True),
         Aspects      => <>,
         Disc_Name    => To_Unbounded_String (Disc_Name),
         Discriminent => Ada_Type_Enum (Disc_Type),
         Fields       => <>,
         Disc_Fields  => <>,
         Need_System  => False);
      Add_Aspect (Result, "Unchecked_Union");

      for Val of Disc_Type.Values loop
         Result.Disc_Fields.Insert (To_String (Val.Id),
                                    Record_Field_Vectors.Empty_Vector);
      end loop;

      return Result;
   end New_Type_Union;

   ---------------
   -- Add_Field --
   ---------------

   procedure Add_Field
     (Rec         : in out Ada_Type_Union'Class;
      Enum_Val    : String;
      Id          : String;
      Typ         : Ada_Type'Class;
      Offset      : Natural;
      LSB         : Natural;
      MSB         : Natural;
      Properties  : Field_Properties;
      Comment     : String := "")
   is
      Idx     : Natural := 0;
      The_Id  : constant Unbounded_String := Ada_Identifier (Id, To_String (Rec.Id));
      Current : Unbounded_String := The_Id;
   begin
      for E of Rec.Disc_Fields loop
         for F of E loop
            if F.Id = Current then
               Idx := Idx + 1;
               Current := The_Id & "_" & To_String (Idx);
            end if;
         end loop;
      end loop;

      Rec.Disc_Fields (Enum_Val).Append
        ((Id          => Current,
          Typ         => Typ.Id,
          Offset      => Offset,
          LSB         => LSB,
          MSB         => MSB,
          Has_Default => False,
          Default     => Null_Unbounded_String,
          Properties  => Properties,
          Comment     => New_Comment (Comment, Strip => True)));
   end Add_Field;

   ----------------
   -- Is_Similar --
   ----------------

   overriding function Is_Similar
     (T1, T2 : Ada_Type_Union)
      return Boolean
   is
      use type Record_Field_Vectors.Vector;
   begin
      if T1.Id /= T2.Id
        or else T1.Fields /= T2.Fields
        or else T1.Disc_Name /= T2.Disc_Name
        or else not Is_Similar (T1.Discriminent, T2.Discriminent)
      then
         return False;
      end if;

      for Key of T1.Discriminent.Values loop
         declare
            Key_Str : String renames To_String (Key.Id);
         begin
            if T1.Disc_Fields (Key_Str) /= T2.Disc_Fields (Key_Str) then
               return False;
            end if;
         end;
      end loop;

      return True;
   end Is_Similar;

   ------------------------
   -- New_Constant_Value --
   ------------------------

   function New_Constant_Value
     (Id        : String;
      Align_Id  : Natural;
      Type_Name : String;
      Value     : String;
      Comment   : String := "")
      return Ada_Constant_Value
   is
   begin
      return (Id        => Ada_Identifier (Id, "Cst"),
              Id_Size   => Align_Id,
              Type_Name => To_Unbounded_String (Type_Name),
              Value     => To_Unbounded_String (Value),
              Comment   => New_Comment (Comment, Strip => True));
   end New_Constant_Value;

   ------------------
   -- New_Variable --
   ------------------

   function New_Variable
     (Id         : String;
      Type_Name  : String;
      Is_Aliased : Boolean;
      Comment    : String := "")
      return Ada_Variable
   is
   begin
      return (Id        => Ada_Identifier (Id, "I"),
              Type_Name => To_Unbounded_String (Type_Name),
              Aliasd    => Is_Aliased,
              Comment   => New_Comment (Comment, Strip => True),
              Aspects   => <>);
   end New_Variable;

   ----------------
   -- Add_Aspect --
   ----------------

   procedure Add_Address_Aspect
     (Elt     : in out Ada_Variable;
      Address : Unsigned)
   is
   begin
      Elt.Aspects.Append ("Address => System'To_Address (" & To_Hex (Address) & ")");
   end Add_Address_Aspect;

   ------------------------
   -- Add_Address_Aspect --
   ------------------------

   procedure Add_Address_Aspect
     (Elt : in out Ada_Variable;
      Val : String)
   is
   begin
      Elt.Aspects.Append ("Address => " & Val);
   end Add_Address_Aspect;

   ----------------
   -- Add_Aspect --
   ----------------

   procedure Add_Aspect
     (Elt         : in out Ada_Variable;
      Aspect_Mark : String)
   is
   begin
      Elt.Aspects.Append (Aspect_Mark);
   end Add_Aspect;

   -------------------
   -- Added_In_Spec --
   -------------------

   overriding procedure Added_In_Spec
     (Element : Ada_Variable;
      Spec    : in out Ada_Spec)
   is
   begin
      if not Element.Aspects.Is_Empty then
         --  Address aspect requires the System package
         Add (Spec, New_With_Clause ("System"));
      end if;
   end Added_In_Spec;

   -----------------
   -- Starts_With --
   -----------------

   function Starts_With (S1, S2 : String) return Boolean
   is (S1'Length >= S2'Length
       and then S1 (S1'First .. S1'First + S2'Length - 1) = S2);

   ---------------
   -- Ends_With --
   ---------------

   function Ends_With (S1, S2 : String) return Boolean
   is (S1'Length >= S2'Length
       and then S1 (S1'Last - S2'Length + 1 .. S1'Last) = S2);

end Ada_Gen;