summaryrefslogtreecommitdiff
path: root/ortho/debug/ortho_debug.adb
blob: a1ef7b82a2f6cd75c798a387ea54cd53a8deaaea (plain)
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
--  Ortho debug back-end.
--  Copyright (C) 2005 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY 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
--  along with GCC; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.

with Ada.Unchecked_Deallocation;

package body Ortho_Debug is
   --  If True, disable some checks so that the output can be generated.
   Disable_Checks : constant Boolean := False;

   type ON_Op_To_OE_Type is array (ON_Op_Kind) of OE_Kind;
   ON_Op_To_OE : constant ON_Op_To_OE_Type :=
     (
      ON_Nil => OE_Nil,

      --  Dyadic operations.
      ON_Add_Ov => OE_Add_Ov,
      ON_Sub_Ov => OE_Sub_Ov,
      ON_Mul_Ov => OE_Mul_Ov,
      ON_Div_Ov => OE_Div_Ov,
      ON_Rem_Ov => OE_Rem_Ov,
      ON_Mod_Ov => OE_Mod_Ov,

      --  Binary operations.
      ON_And => OE_And,
      ON_Or => OE_Or,
      ON_Xor => OE_Xor,
      ON_And_Then => OE_And_Then,
      ON_Or_Else => OE_Or_Else,

      --  Monadic operations.
      ON_Not => OE_Not,
      ON_Neg_Ov => OE_Neg_Ov,
      ON_Abs_Ov => OE_Abs_Ov,

      --  Comparaisons
      ON_Eq => OE_Eq,
      ON_Neq => OE_Neq,
      ON_Le => OE_Le,
      ON_Lt => OE_Lt,
      ON_Ge => OE_Ge,
      ON_Gt => OE_Gt
      );

   type Decl_Scope_Type is record
      --  Declarations are chained.
      Parent : O_Snode;
      Last_Decl : O_Dnode;
      Last_Stmt : O_Snode;

      --  If this scope corresponds to a function, PREV_FUNCTION contains
      --  the previous function.
      Prev_Function : O_Dnode;

      --  Declaration scopes are chained.
      Prev : Decl_Scope_Acc;
   end record;

   type Stmt_Kind is
     (Stmt_Function, Stmt_Declare, Stmt_If, Stmt_Loop, Stmt_Case);
   type Stmt_Scope_Type (Kind : Stmt_Kind);
   type Stmt_Scope_Acc is access Stmt_Scope_Type;
   type Stmt_Scope_Type (Kind : Stmt_Kind) is record
      --  Statement which created this scope.
      Parent : O_Snode;
      --  Previous (parent) scope.
      Prev : Stmt_Scope_Acc;
      case Kind is
         when Stmt_Function =>
            Prev_Function : Stmt_Scope_Acc;
            --  Declaration for the function.
            Decl : O_Dnode;
         when Stmt_Declare =>
            null;
         when Stmt_If =>
            Last_Elsif : O_Snode;
         when Stmt_Loop =>
            null;
         when Stmt_Case =>
            Last_Branch : O_Snode;
            Last_Choice : O_Choice;
            Case_Type : O_Tnode;
      end case;
   end record;
   subtype Stmt_Function_Scope_Type is Stmt_Scope_Type (Stmt_Function);
   subtype Stmt_Declare_Scope_Type is Stmt_Scope_Type (Stmt_Declare);
   subtype Stmt_If_Scope_Type is Stmt_Scope_Type (Stmt_If);
   subtype Stmt_Loop_Scope_Type is Stmt_Scope_Type (Stmt_Loop);
   subtype Stmt_Case_Scope_Type is Stmt_Scope_Type (Stmt_Case);

   Current_Stmt_Scope : Stmt_Scope_Acc := null;
   Current_Function : Stmt_Scope_Acc := null;
   Current_Decl_Scope : Decl_Scope_Acc := null;
   Current_Loop_Level : Natural := 0;

   procedure Push_Decl_Scope (Parent : O_Snode)
   is
      Res : Decl_Scope_Acc;
   begin
      Res := new Decl_Scope_Type'(Parent => Parent,
                                  Last_Decl => null,
                                  Last_Stmt => null,
                                  Prev_Function => null,
                                  Prev => Current_Decl_Scope);
      Parent.Alive := True;
      Current_Decl_Scope := Res;
   end Push_Decl_Scope;

   procedure Pop_Decl_Scope
   is
      procedure Unchecked_Deallocation is new Ada.Unchecked_Deallocation
        (Object => Decl_Scope_Type, Name => Decl_Scope_Acc);
      Old : Decl_Scope_Acc;
   begin
      Old := Current_Decl_Scope;
      Old.Parent.Alive := False;
      Current_Decl_Scope := Old.Prev;
      Unchecked_Deallocation (Old);
   end Pop_Decl_Scope;

   procedure Add_Decl (El : O_Dnode; Check_Dup : Boolean := True) is
   begin
      if Current_Decl_Scope = null then
         --  Not yet initialized, or after compilation.
         raise Program_Error;
      end if;

      --  Note: this requires an hashed ident table.
      --  Use ortho_ident_hash.
      if False and then Check_Dup
        and then not Is_Nul (El.Name)
      then
         --  Check the name is not already defined.
         declare
            E : O_Dnode;
         begin
            E := Current_Decl_Scope.Parent.Decls;
            while E /= O_Dnode_Null loop
               if Is_Equal (E.Name, El.Name) then
                  raise Syntax_Error;
               end if;
               E := E.Next;
            end loop;
         end;
      end if;

      if Current_Decl_Scope.Last_Decl = null then
         if Current_Decl_Scope.Parent.Kind = ON_Declare_Stmt then
            Current_Decl_Scope.Parent.Decls := El;
         else
            raise Type_Error;
         end if;
      else
         Current_Decl_Scope.Last_Decl.Next := El;
      end if;
      El.Next := null;
      Current_Decl_Scope.Last_Decl := El;
   end Add_Decl;

   procedure Add_Stmt (Stmt : O_Snode)
   is
   begin
      if Current_Decl_Scope = null or Current_Function = null then
         --  You are adding a statement at the global level, ie not inside
         --  a function.
         raise Syntax_Error;
      end if;

      Stmt.Next := null;
      if Current_Decl_Scope.Last_Stmt = null then
         if Current_Decl_Scope.Parent.Kind = ON_Declare_Stmt then
            Current_Decl_Scope.Parent.Stmts := Stmt;
         else
            raise Syntax_Error;
         end if;
      else
         Current_Decl_Scope.Last_Stmt.Next := Stmt;
      end if;
      Current_Decl_Scope.Last_Stmt := Stmt;
   end Add_Stmt;

   procedure Push_Stmt_Scope (Scope : Stmt_Scope_Acc)
   is
   begin
      if Scope.Prev /= Current_Stmt_Scope then
         --  SCOPE was badly initialized.
         raise Program_Error;
      end if;
      Current_Stmt_Scope := Scope;
   end Push_Stmt_Scope;

   procedure Pop_Stmt_Scope (Kind : Stmt_Kind)
   is
      procedure Unchecked_Deallocation is new Ada.Unchecked_Deallocation
        (Object => Stmt_Scope_Type, Name => Stmt_Scope_Acc);
      Old : Stmt_Scope_Acc;
   begin
      Old := Current_Stmt_Scope;
      if Old.Kind /= Kind then
         raise Syntax_Error;
      end if;
      --Old.Parent.Last_Stmt := Current_Decl_Scope.Last_Stmt;
      Current_Stmt_Scope := Old.Prev;
      Unchecked_Deallocation (Old);
   end Pop_Stmt_Scope;

   --  Check declaration DECL is reachable, ie its scope is in the current
   --  stack of scopes.
   procedure Check_Scope (Decl : O_Dnode)
   is
      Res : Boolean;
   begin
      case Decl.Kind is
         when ON_Interface_Decl =>
            Res := Decl.Func_Scope.Alive;
         when others =>
            Res := Decl.Scope.Alive;
      end case;
      if not Res then
         raise Syntax_Error;
      end if;
   end Check_Scope;

   --  Raise SYNTAX_ERROR if OBJ is not at a constant address.
--    procedure Check_Const_Address (Obj : O_Lnode) is
--    begin
--       case Obj.Kind is
--          when OL_Const_Ref
--            | OL_Var_Ref =>
--             case Obj.Decl.Storage is
--                when O_Storage_External
--                  | O_Storage_Public
--                  | O_Storage_Private =>
--                   null;
--                when O_Storage_Local =>
--                   raise Syntax_Error;
--             end case;
--          when others =>
--             --  FIXME: constant indexed element, selected element maybe
--             --   of const address.
--             raise Syntax_Error;
--       end case;
--    end Check_Const_Address;

   procedure Check_Type (T1, T2 : O_Tnode) is
   begin
      if T1 = T2 then
         return;
      end if;
      if T1.Kind = ON_Array_Sub_Type and then T2.Kind = ON_Array_Sub_Type
        and then T1.Base_Type = T2.Base_Type
        and then T1.Length.all = T2.Length.all
      then
         return;
      end if;
      raise Type_Error;
   end Check_Type;

   procedure Check_Ref (N : O_Enode) is
   begin
      if N.Ref then
         --  Already referenced.
         raise Syntax_Error;
      end if;
      N.Ref := True;
   end Check_Ref;

   procedure Check_Ref (N : O_Lnode) is
   begin
      if N.Ref then
         raise Syntax_Error;
      end if;
      N.Ref := True;
   end Check_Ref;

   procedure Check_Complete_Type (T : O_Tnode) is
   begin
      if not T.Complete then
         --  Uncomplete type cannot be used here (since its size is required,
         --   for example).
         raise Syntax_Error;
      end if;
   end Check_Complete_Type;

   function New_Dyadic_Op (Kind : ON_Dyadic_Op_Kind; Left, Right : O_Enode)
     return O_Enode
   is
      K : constant OE_Kind := ON_Op_To_OE (Kind);
      Res : O_Enode;
   begin
      Check_Type (Left.Rtype, Right.Rtype);
      Check_Ref (Left);
      Check_Ref (Right);
      Res := new O_Enode_Type (K);
      Res.Rtype := Left.Rtype;
      Res.Ref := False;
      Res.Left := Left;
      Res.Right := Right;
      return Res;
   end New_Dyadic_Op;

   function New_Monadic_Op (Kind : ON_Monadic_Op_Kind; Operand : O_Enode)
     return O_Enode
   is
      Res : O_Enode;
   begin
      Check_Ref (Operand);
      Res := new O_Enode_Type (ON_Op_To_OE (Kind));
      Res.Ref := False;
      Res.Operand := Operand;
      Res.Rtype := Operand.Rtype;
      return Res;
   end New_Monadic_Op;

   function New_Compare_Op
     (Kind : ON_Compare_Op_Kind; Left, Right : O_Enode; Ntype : O_Tnode)
     return O_Enode
   is
      Res : O_Enode;
   begin
      if Ntype.Kind /= ON_Boolean_Type then
         raise Type_Error;
      end if;
      if Left.Rtype /= Right.Rtype then
         raise Type_Error;
      end if;
      Check_Ref (Left);
      Check_Ref (Right);
      Res := new O_Enode_Type (ON_Op_To_OE (Kind));
      Res.Ref := False;
      Res.Left := Left;
      Res.Right := Right;
      Res.Rtype := Ntype;
      return Res;
   end New_Compare_Op;


   function New_Signed_Literal (Ltype : O_Tnode; Value : Integer_64)
     return O_Cnode
   is
      subtype O_Cnode_Signed_Lit is O_Cnode_Type (OC_Signed_Lit);
   begin
      if Ltype.Kind = ON_Signed_Type then
         return new O_Cnode_Signed_Lit'(Kind => OC_Signed_Lit,
                                        Ctype => Ltype,
                                        Ref => False,
                                        S_Val => Value);
      else
         raise Type_Error;
      end if;
   end New_Signed_Literal;

   function New_Unsigned_Literal (Ltype : O_Tnode; Value : Unsigned_64)
     return O_Cnode
   is
      subtype O_Cnode_Unsigned_Lit is O_Cnode_Type (OC_Unsigned_Lit);
   begin
      if Ltype.Kind = ON_Unsigned_Type then
         return new O_Cnode_Unsigned_Lit'(Kind => OC_Unsigned_Lit,
                                          Ctype => Ltype,
                                          Ref => False,
                                          U_Val => Value);
      else
         raise Type_Error;
      end if;
   end New_Unsigned_Literal;

   function New_Float_Literal (Ltype : O_Tnode; Value : IEEE_Float_64)
     return O_Cnode
   is
      subtype O_Cnode_Float_Lit is O_Cnode_Type (OC_Float_Lit);
   begin
      if Ltype.Kind = ON_Float_Type then
         return new O_Cnode_Float_Lit'(Kind => OC_Float_Lit,
                                       Ctype => Ltype,
                                       Ref => False,
                                       F_Val => Value);
      else
         raise Type_Error;
      end if;
   end New_Float_Literal;

   function New_Null_Access (Ltype : O_Tnode) return O_Cnode
   is
      subtype O_Cnode_Null_Lit_Type is O_Cnode_Type (OC_Null_Lit);
   begin
      if Ltype.Kind /= ON_Access_Type then
         raise Type_Error;
      end if;
      return  new O_Cnode_Null_Lit_Type'(Kind => OC_Null_Lit,
                                         Ctype => Ltype,
                                         Ref => False);
   end New_Null_Access;

   function New_Sizeof (Atype : O_Tnode; Rtype : O_Tnode) return O_Cnode
   is
      subtype O_Cnode_Sizeof_Type is O_Cnode_Type (OC_Sizeof_Lit);
   begin
      if Rtype.Kind /= ON_Unsigned_Type
        and then Rtype.Kind /= ON_Access_Type
      then
         raise Type_Error;
      end if;
      Check_Complete_Type (Atype);
      if Atype.Kind = ON_Array_Type then
         raise Type_Error;
      end if;
      return new O_Cnode_Sizeof_Type'(Kind => OC_Sizeof_Lit,
                                      Ctype => Rtype,
                                      Ref => False,
                                      S_Type => Atype);
   end New_Sizeof;

   function New_Alignof (Atype : O_Tnode; Rtype : O_Tnode) return O_Cnode
   is
      subtype O_Cnode_Alignof_Type is O_Cnode_Type (OC_Alignof_Lit);
   begin
      if Rtype.Kind /= ON_Unsigned_Type then
         raise Type_Error;
      end if;
      Check_Complete_Type (Atype);
      return new O_Cnode_Alignof_Type'(Kind => OC_Alignof_Lit,
                                       Ctype => Rtype,
                                       Ref => False,
                                       S_Type => Atype);
   end New_Alignof;

   function New_Offsetof (Rec_Type : O_Tnode; Field : O_Fnode; Rtype : O_Tnode)
                         return O_Cnode
   is
      subtype O_Cnode_Offsetof_Type is O_Cnode_Type (OC_Offsetof_Lit);
   begin
      if Rtype.Kind /= ON_Unsigned_Type
        and then Rtype.Kind /= ON_Access_Type
      then
         raise Type_Error;
      end if;
      if Field.Parent /= Rec_Type then
         raise Type_Error;
      end if;
      return new O_Cnode_Offsetof_Type'(Kind => OC_Offsetof_Lit,
                                        Ctype => Rtype,
                                        Ref => False,
                                        Off_Field => Field);
   end New_Offsetof;

   function New_Alloca (Rtype : O_Tnode; Size : O_Enode) return O_Enode
   is
      subtype O_Enode_Alloca_Type is O_Enode_Type (OE_Alloca);
      Res : O_Enode;
   begin
      if Rtype.Kind /= ON_Access_Type then
         raise Type_Error;
      end if;
      if Size.Rtype.Kind /= ON_Unsigned_Type then
         raise Type_Error;
      end if;
      Res := new O_Enode_Alloca_Type'(Kind => OE_Alloca,
                                      Rtype => Rtype,
                                      Ref => False,
                                      A_Size => Size);
      return Res;
   end New_Alloca;

   procedure Check_Constrained_Type (Atype : O_Tnode) is
   begin
      case Atype.Kind is
         when ON_Array_Type =>
            raise Type_Error;
         when ON_Unsigned_Type
           | ON_Signed_Type
           | ON_Boolean_Type
           | ON_Record_Type
           | ON_Union_Type
           | ON_Access_Type
           | ON_Float_Type
           | ON_Array_Sub_Type
           | ON_Enum_Type =>
            null;
      end case;
   end Check_Constrained_Type;

   procedure New_Completed_Type_Decl (Atype : O_Tnode)
   is
      N : O_Dnode;
   begin
      if Atype.Decl = null then
         --  The uncompleted type must have been declared.
         raise Type_Error;
      end if;
      N := new O_Dnode_Type (ON_Completed_Type_Decl);
      N.Name := Atype.Decl.Name;
      N.Dtype := Atype;
      Add_Decl (N, False);
   end New_Completed_Type_Decl;

   procedure New_Uncomplete_Record_Type (Res : out O_Tnode)
   is
      subtype O_Tnode_Record_Type is O_Tnode_Type (ON_Record_Type);
   begin
      Res := new O_Tnode_Record_Type'(Kind => ON_Record_Type,
                                      Decl => O_Dnode_Null,
                                      Uncomplete => True,
                                      Complete => False,
                                      Elements => O_Fnode_Null);
   end New_Uncomplete_Record_Type;

   procedure Start_Uncomplete_Record_Type (Res : O_Tnode;
                                           Elements : out O_Element_List) is
   begin
      if not Res.Uncomplete then
         --  RES record type is not an uncomplete record type.
         raise Syntax_Error;
      end if;
      if Res.Elements /= O_Fnode_Null then
         --  RES record type already has elements...
         raise Syntax_Error;
      end if;
      Elements.Res := Res;
      Elements.Last := null;
   end Start_Uncomplete_Record_Type;

   procedure Start_Record_Type (Elements : out O_Element_List)
   is
      subtype O_Tnode_Record_Type is O_Tnode_Type (ON_Record_Type);
   begin
      Elements.Res := new O_Tnode_Record_Type'(Kind => ON_Record_Type,
                                               Decl => O_Dnode_Null,
                                               Uncomplete => False,
                                               Complete => False,
                                               Elements => O_Fnode_Null);
      Elements.Last := null;
   end Start_Record_Type;

   procedure New_Record_Field
     (Elements : in out O_Element_List;
      El : out O_Fnode;
      Ident : O_Ident; Etype : O_Tnode)
   is
   begin
      Check_Complete_Type (Etype);
      Check_Constrained_Type (Etype);
      El := new O_Fnode_Type'(Parent => Elements.Res,
                              Next => null,
                              Ident => Ident,
                              Ftype => Etype,
                              Offset => 0);
      --  Append EL.
      if Elements.Last = null then
         Elements.Res.Elements := El;
      else
         Elements.Last.Next := El;
      end if;
      Elements.Last := El;
   end New_Record_Field;

   procedure Finish_Record_Type
     (Elements : in out O_Element_List; Res : out O_Tnode) is
   begin
      --  Align the structure.
      Res := Elements.Res;
      if Res.Uncomplete then
         New_Completed_Type_Decl (Res);
      end if;
      Res.Complete := True;
   end Finish_Record_Type;

   procedure Start_Union_Type (Elements : out O_Element_List)
   is
      subtype O_Tnode_Union_Type is O_Tnode_Type (ON_Union_Type);
   begin
      Elements.Res := new O_Tnode_Union_Type'(Kind => ON_Union_Type,
                                              Decl => O_Dnode_Null,
                                              Uncomplete => False,
                                              Complete => False,
                                              Elements => O_Fnode_Null);
      Elements.Last := null;
   end Start_Union_Type;

   procedure New_Union_Field
     (Elements : in out O_Element_List;
      El : out O_Fnode;
      Ident : O_Ident; Etype : O_Tnode)
   is
   begin
      New_Record_Field (Elements, El, Ident, Etype);
   end New_Union_Field;

   procedure Finish_Union_Type
     (Elements : in out O_Element_List; Res : out O_Tnode) is
   begin
      Res := Elements.Res;
      Res.Complete := True;
   end Finish_Union_Type;

   function New_Access_Type (Dtype : O_Tnode) return O_Tnode
   is
      subtype O_Tnode_Access is O_Tnode_Type (ON_Access_Type);
      Res : O_Tnode;
   begin
      if Dtype /= O_Tnode_Null
        and then Dtype.Kind = ON_Array_Sub_Type
      then
         --  Access to sub array are not allowed, use access to array.
         raise Type_Error;
      end if;
      Res := new O_Tnode_Access'(Kind => ON_Access_Type,
                                 Decl => O_Dnode_Null,
                                 Uncomplete => Dtype = O_Tnode_Null,
                                 Complete => True,
                                 D_Type => Dtype);
      return Res;
   end New_Access_Type;

   procedure Finish_Access_Type (Atype : O_Tnode; Dtype : O_Tnode)
   is
   begin
      if Dtype.Kind = ON_Array_Sub_Type then
         --  Access to sub array are not allowed, use access to array.
         raise Type_Error;
      end if;
      if Atype.D_Type /= O_Tnode_Null
        or Atype.Uncomplete = False
      then
         --  Type already completed.
         raise Syntax_Error;
      end if;
      Atype.D_Type := Dtype;
      New_Completed_Type_Decl (Atype);
   end Finish_Access_Type;

   function New_Array_Type (El_Type : O_Tnode; Index_Type : O_Tnode)
     return O_Tnode
   is
      subtype O_Tnode_Array is O_Tnode_Type (ON_Array_Type);
   begin
      Check_Constrained_Type (El_Type);
      Check_Complete_Type (El_Type);
      return new O_Tnode_Array'(Kind => ON_Array_Type,
                                Decl => O_Dnode_Null,
                                Uncomplete => False,
                                Complete => True,
                                El_Type => El_Type,
                                Index_Type => Index_Type);
   end New_Array_Type;

   function New_Constrained_Array_Type (Atype : O_Tnode; Length : O_Cnode)
     return O_Tnode
   is
      subtype O_Tnode_Sub_Array is O_Tnode_Type (ON_Array_Sub_Type);
   begin
      if Atype.Kind /= ON_Array_Type then
         raise Type_Error;
      end if;
      return new O_Tnode_Sub_Array'(Kind => ON_Array_Sub_Type,
                                    Decl => O_Dnode_Null,
                                    Uncomplete => False,
                                    Complete => True,
                                    Base_Type => Atype,
                                    Length => Length);
   end New_Constrained_Array_Type;

   function New_Unsigned_Type (Size : Natural) return O_Tnode
   is
      subtype O_Tnode_Unsigned is O_Tnode_Type (ON_Unsigned_Type);
   begin
      return new O_Tnode_Unsigned'(Kind => ON_Unsigned_Type,
                                   Decl => O_Dnode_Null,
                                   Uncomplete => False,
                                   Complete => True,
                                   Int_Size => Size);
   end New_Unsigned_Type;

   function New_Signed_Type (Size : Natural) return O_Tnode
   is
      subtype O_Tnode_Signed is O_Tnode_Type (ON_Signed_Type);
   begin
      return new O_Tnode_Signed'(Kind => ON_Signed_Type,
                                 Decl => O_Dnode_Null,
                                 Uncomplete => False,
                                 Complete => True,
                                 Int_Size => Size);
   end New_Signed_Type;

   function New_Float_Type return O_Tnode
   is
      subtype O_Tnode_Float is O_Tnode_Type (ON_Float_Type);
   begin
      return new O_Tnode_Float'(Kind => ON_Float_Type,
                                Decl => O_Dnode_Null,
                                Uncomplete => False,
                                Complete => True);
   end New_Float_Type;

   procedure New_Boolean_Type (Res : out O_Tnode;
                               False_Id : O_Ident;
                               False_E : out O_Cnode;
                               True_Id : O_Ident;
                               True_E : out O_Cnode)
   is
      subtype O_Tnode_Boolean is O_Tnode_Type (ON_Boolean_Type);
      subtype O_Cnode_Boolean_Lit is O_Cnode_Type (OC_Boolean_Lit);
   begin
      Res := new O_Tnode_Boolean'(Kind => ON_Boolean_Type,
                                  Decl => O_Dnode_Null,
                                  Uncomplete => False,
                                  Complete => True,
                                  True_N => O_Cnode_Null,
                                  False_N => O_Cnode_Null);
      True_E := new O_Cnode_Boolean_Lit'(Kind => OC_Boolean_Lit,
                                         Ctype => Res,
                                         Ref => False,
                                         B_Val => True,
                                         B_Id => True_Id);
      False_E := new O_Cnode_Boolean_Lit'(Kind => OC_Boolean_Lit,
                                          Ctype => Res,
                                          Ref => False,
                                          B_Val => False,
                                          B_Id => False_Id);
      Res.True_N := True_E;
      Res.False_N := False_E;
   end New_Boolean_Type;

   procedure Start_Enum_Type (List : out O_Enum_List; Size : Natural)
   is
      pragma Unreferenced (Size);
      subtype O_Tnode_Enum is O_Tnode_Type (ON_Enum_Type);
      Res : O_Tnode;
   begin
      Res := new O_Tnode_Enum'(Kind => ON_Enum_Type,
                               Decl => O_Dnode_Null,
                               Uncomplete => False,
                               Complete => False,
                               Nbr => 0,
                               Literals => O_Cnode_Null);
      List.Res := Res;
      List.Last := O_Cnode_Null;
   end Start_Enum_Type;

   procedure New_Enum_Literal (List : in out O_Enum_List;
                               Ident : O_Ident;
                               Res : out O_Cnode)
   is
      subtype O_Cnode_Enum_Lit is O_Cnode_Type (OC_Enum_Lit);
   begin
      Res := new O_Cnode_Enum_Lit'(Kind => OC_Enum_Lit,
                                   Ctype => List.Res,
                                   Ref => False,
                                   E_Val => List.Res.Nbr,
                                   E_Name => Ident,
                                   E_Next => O_Cnode_Null);
      --  Link it.
      if List.Last = O_Cnode_Null then
         List.Res.Literals := Res;
      else
         List.Last.E_Next := Res;
      end if;
      List.Last := Res;

      List.Res.Nbr := List.Res.Nbr + 1;
   end New_Enum_Literal;

   procedure Finish_Enum_Type (List : in out O_Enum_List; Res : out O_Tnode) is
   begin
      Res := List.Res;
      Res.Complete := True;
   end Finish_Enum_Type;

   function Get_Base_Type (Atype : O_Tnode) return O_Tnode
   is
   begin
      case Atype.Kind is
         when ON_Array_Sub_Type =>
            return Atype.Base_Type;
         when others =>
            return Atype;
      end case;
   end Get_Base_Type;

   procedure Start_Record_Aggr (List : out O_Record_Aggr_List; Atype : O_Tnode)
   is
      subtype O_Cnode_Aggregate is O_Cnode_Type (OC_Aggregate);
      Res : O_Cnode;
   begin
      if Atype.Kind /= ON_Record_Type then
         raise Type_Error;
      end if;
      Check_Complete_Type (Atype);
      Res := new O_Cnode_Aggregate'(Kind => OC_Aggregate,
                                    Ctype => Atype,
                                    Ref => False,
                                    Aggr_Els => null);
      List.Res := Res;
      List.Last := null;
      List.Field := Atype.Elements;
   end Start_Record_Aggr;

   procedure New_Record_Aggr_El (List : in out O_Record_Aggr_List;
                                 Value : O_Cnode)
   is
      subtype O_Cnode_Aggrel_Type is O_Cnode_Type (OC_Aggr_Element);
      El : O_Cnode;
   begin
      if List.Field = O_Fnode_Null then
         --  No more element in the aggregate.
         raise Syntax_Error;
      end if;
      Check_Type (Value.Ctype, List.Field.Ftype);
      El := new O_Cnode_Aggrel_Type'(Kind => OC_Aggr_Element,
                                     Ctype => Value.Ctype,
                                     Ref => False,
                                     Aggr_Value => Value,
                                     Aggr_Next => null);
      if List.Last = null then
         List.Res.Aggr_Els := El;
      else
         List.Last.Aggr_Next := El;
      end if;
      List.Last := El;
      List.Field := List.Field.Next;
   end New_Record_Aggr_El;

   procedure Finish_Record_Aggr
     (List : in out O_Record_Aggr_List; Res : out O_Cnode)
   is
   begin
      if List.Field /= null then
         --  Not enough elements in aggregate.
         raise Type_Error;
      end if;
      Res := List.Res;
   end Finish_Record_Aggr;

   procedure Start_Array_Aggr (List : out O_Array_Aggr_List; Atype : O_Tnode)
   is
      subtype O_Cnode_Aggregate is O_Cnode_Type (OC_Aggregate);
      Res : O_Cnode;
   begin
      if Atype.Kind /= ON_Array_Sub_Type then
         raise Type_Error;
      end if;
      Check_Complete_Type (Atype);
      Res := new O_Cnode_Aggregate'(Kind => OC_Aggregate,
                                    Ctype => Atype,
                                    Ref => False,
                                    Aggr_Els => null);
      List.Res := Res;
      List.Last := null;
      List.El_Type := Atype.Base_Type.El_Type;
   end Start_Array_Aggr;

   procedure New_Array_Aggr_El (List : in out O_Array_Aggr_List;
                                Value : O_Cnode)
   is
      subtype O_Cnode_Aggrel_Type is O_Cnode_Type (OC_Aggr_Element);
      El : O_Cnode;
   begin
      Check_Type (Value.Ctype, List.El_Type);
      El := new O_Cnode_Aggrel_Type'(Kind => OC_Aggr_Element,
                                     Ctype => Value.Ctype,
                                     Ref => False,
                                     Aggr_Value => Value,
                                     Aggr_Next => null);
      if List.Last = null then
         List.Res.Aggr_Els := El;
      else
         List.Last.Aggr_Next := El;
      end if;
      List.Last := El;
   end New_Array_Aggr_El;

   procedure Finish_Array_Aggr
     (List : in out O_Array_Aggr_List; Res : out O_Cnode) is
   begin
      Res := List.Res;
   end Finish_Array_Aggr;

   function New_Union_Aggr (Atype : O_Tnode; Field : O_Fnode; Value : O_Cnode)
                           return O_Cnode
   is
      subtype O_Cnode_Union_Aggr is O_Cnode_Type (OC_Union_Aggr);
      Res : O_Cnode;
   begin
      if Atype.Kind /= ON_Union_Type then
         raise Type_Error;
      end if;
      Check_Type (Value.Ctype, Field.Ftype);

      Res := new O_Cnode_Union_Aggr'(Kind => OC_Union_Aggr,
                                     Ctype => Atype,
                                     Ref => False,
                                     Uaggr_Field => Field,
                                     Uaggr_Value => Value);
      return Res;
   end New_Union_Aggr;

   function New_Obj (Obj : O_Dnode) return O_Lnode
   is
      subtype O_Lnode_Obj is O_Lnode_Type (OL_Obj);
   begin
      case Obj.Kind is
         when ON_Const_Decl
           | ON_Var_Decl
           | ON_Interface_Decl =>
            null;
         when others =>
            raise Program_Error;
      end case;
      Check_Scope (Obj);
      return new O_Lnode_Obj'(Kind => OL_Obj,
                              Rtype => Obj.Dtype,
                              Ref => False,
                              Obj => Obj);
   end New_Obj;

   function New_Indexed_Element (Arr : O_Lnode; Index : O_Enode)
     return O_Lnode
   is
      subtype O_Lnode_Indexed is O_Lnode_Type (OL_Indexed_Element);
      Res : O_Lnode;
   begin
      Check_Ref (Arr);
      Res := new O_Lnode_Indexed'(Kind => OL_Indexed_Element,
                                  Rtype => Get_Base_Type (Arr.Rtype).El_Type,
                                  Ref => False,
                                  Array_Base => Arr,
                                  Index => Index);
      return Res;
   end New_Indexed_Element;

   function New_Slice (Arr : O_Lnode; Res_Type : O_Tnode; Index : O_Enode)
     return O_Lnode
   is
      subtype O_Lnode_Slice is O_Lnode_Type (OL_Slice);
      Res : O_Lnode;
   begin
      if Res_Type.Kind /= ON_Array_Type
        and then Res_Type.Kind /= ON_Array_Sub_Type
      then
         raise Type_Error;
      end if;
      Check_Ref (Arr);
      Check_Ref (Index);
      -- FIXME: check type.
      Res := new O_Lnode_Slice'(Kind => OL_Slice,
                                Rtype => Res_Type,
                                Ref => False,
                                Slice_Base => Arr,
                                Slice_Index => Index);
      return Res;
   end New_Slice;

   function New_Selected_Element (Rec : O_Lnode; El : O_Fnode)
     return O_Lnode
   is
      subtype O_Lnode_Selected_Element is O_Lnode_Type (OL_Selected_Element);
   begin
      if Rec.Rtype.Kind /= ON_Record_Type then
         raise Type_Error;
      end if;
      if Rec.Rtype /= El.Parent then
         raise Type_Error;
      end if;
      Check_Ref (Rec);
      return new O_Lnode_Selected_Element'(Kind => OL_Selected_Element,
                                           Rtype => El.Ftype,
                                           Ref => False,
                                           Rec_Base => Rec,
                                           Rec_El => El);
   end New_Selected_Element;

   function New_Access_Element (Acc : O_Enode) return O_Lnode
   is
      subtype O_Lnode_Access_Element is O_Lnode_Type (OL_Access_Element);
   begin
      if Acc.Rtype.Kind /= ON_Access_Type then
         raise Type_Error;
      end if;
      Check_Ref (Acc);
      return new O_Lnode_Access_Element'(Kind => OL_Access_Element,
                                         Rtype => Acc.Rtype.D_Type,
                                         Ref => False,
                                         Acc_Base => Acc);
   end New_Access_Element;

   function Check_Conv (Source : ON_Type_Kind; Target : ON_Type_Kind)
     return Boolean
   is
      type Conv_Array is array (ON_Type_Kind, ON_Type_Kind) of Boolean;
      T : constant Boolean := True;
      F : constant Boolean := False;
      Conv_Allowed : constant Conv_Array :=
        (ON_Boolean_Type =>  (T, F, T, T, F, F, F, F, F, F),
         ON_Enum_Type =>     (F, F, T, T, F, F, F, F, F, F),
         ON_Unsigned_Type => (T, T, T, T, F, F, F, F, F, F),
         ON_Signed_Type =>   (T, T, T, T, T, F, F, F, F, F),
         ON_Float_Type =>    (F, F, F, T, T, F, F, F, F, F),
         ON_Array_Type =>    (F, F, F, F, F, F, T, F, F, F),
         ON_Array_Sub_Type =>(F, F, F, F, F, T, T, F, F, F),
         ON_Record_Type =>   (F, F, F, F, F, F, F, F, F, F),
         ON_Union_Type =>    (F, F, F, F, F, F, F, F, F, F),
         ON_Access_Type =>   (F, F, F, F, F, F, F, F, F, T));
   begin
      if Source = Target then
         return True;
      else
         return Conv_Allowed (Source, Target);
      end if;
   end Check_Conv;

   function New_Convert_Ov (Val : O_Enode; Rtype : O_Tnode) return O_Enode
   is
      subtype O_Enode_Convert is O_Enode_Type (OE_Convert_Ov);
      Res : O_Enode;
   begin
      Check_Ref (Val);
      if not Check_Conv (Val.Rtype.Kind, Rtype.Kind) then
         raise Type_Error;
      end if;
      Res := new O_Enode_Convert'(Kind => OE_Convert_Ov,
                                  Rtype => Rtype,
                                  Ref => False,
                                  Conv => Val);
      return Res;
   end New_Convert_Ov;

   function New_Unchecked_Address (Lvalue : O_Lnode; Atype : O_Tnode)
     return O_Enode
   is
      subtype O_Enode_Address is O_Enode_Type (OE_Unchecked_Address);
   begin
      Check_Ref (Lvalue);
      if Atype.Kind /= ON_Access_Type then
         --  An address is of type access.
         raise Type_Error;
      end if;
      return new O_Enode_Address'(Kind => OE_Unchecked_Address,
                                  Rtype => Atype,
                                  Ref => False,
                                  Lvalue => Lvalue);
   end New_Unchecked_Address;

   function New_Address (Lvalue : O_Lnode; Atype : O_Tnode) return O_Enode
   is
      subtype O_Enode_Address is O_Enode_Type (OE_Address);
   begin
      Check_Ref (Lvalue);
      if Atype.Kind /= ON_Access_Type then
         --  An address is of type access.
         raise Type_Error;
      end if;
      if Get_Base_Type (Lvalue.Rtype) /= Get_Base_Type (Atype.D_Type) then
         if not Disable_Checks then
            raise Type_Error;
         end if;
      end if;
      return new O_Enode_Address'(Kind => OE_Address,
                                  Rtype => Atype,
                                  Ref => False,
                                  Lvalue => Lvalue);
   end New_Address;

   function New_Global_Unchecked_Address (Decl : O_Dnode; Atype : O_Tnode)
     return O_Cnode
   is
      subtype O_Cnode_Address is O_Cnode_Type (OC_Unchecked_Address);
   begin
      Check_Scope (Decl);
      if Atype.Kind /= ON_Access_Type then
         --  An address is of type access.
         raise Type_Error;
      end if;
      return new O_Cnode_Address'(Kind => OC_Unchecked_Address,
                                  Ctype => Atype,
                                  Ref => False,
                                  Decl => Decl);
   end New_Global_Unchecked_Address;

   function New_Global_Address (Decl : O_Dnode; Atype : O_Tnode) return O_Cnode
   is
      subtype O_Cnode_Address is O_Cnode_Type (OC_Address);
   begin
      Check_Scope (Decl);
      if Atype.Kind /= ON_Access_Type then
         --  An address is of type access.
         raise Type_Error;
      end if;
      if Get_Base_Type (Decl.Dtype) /= Get_Base_Type (Atype.D_Type) then
         raise Type_Error;
      end if;
      return new O_Cnode_Address'(Kind => OC_Address,
                                  Ctype => Atype,
                                  Ref => False,
                                  Decl => Decl);
   end New_Global_Address;

   function New_Subprogram_Address (Subprg : O_Dnode; Atype : O_Tnode)
     return O_Cnode
   is
      subtype O_Cnode_Subprg_Address is O_Cnode_Type (OC_Subprogram_Address);
   begin
      if Atype.Kind /= ON_Access_Type then
         --  An address is of type access.
         raise Type_Error;
      end if;
      return new O_Cnode_Subprg_Address'(Kind => OC_Subprogram_Address,
                                         Ctype => Atype,
                                         Ref => False,
                                         Decl => Subprg);
   end New_Subprogram_Address;

   --  Raise TYPE_ERROR is ATYPE is a composite type.
   procedure Check_Not_Composite (Atype : O_Tnode) is
   begin
      case Atype.Kind is
         when ON_Boolean_Type
           | ON_Unsigned_Type
           | ON_Signed_Type
           | ON_Float_Type
           | ON_Enum_Type
           | ON_Access_Type=>
            return;
         when ON_Array_Type
           | ON_Record_Type
           | ON_Union_Type
           | ON_Array_Sub_Type =>
            raise Type_Error;
      end case;
   end Check_Not_Composite;

   function New_Value (Lvalue : O_Lnode) return O_Enode is
      subtype O_Enode_Value is O_Enode_Type (OE_Value);
   begin
      Check_Not_Composite (Lvalue.Rtype);
      Check_Ref (Lvalue);
      return new O_Enode_Value'(Kind => OE_Value,
                                Rtype => Lvalue.Rtype,
                                Ref => False,
                                Value => Lvalue);
   end New_Value;

   function New_Obj_Value (Obj : O_Dnode) return O_Enode is
   begin
      return New_Value (New_Obj (Obj));
   end New_Obj_Value;

   function New_Lit (Lit : O_Cnode) return O_Enode is
      subtype O_Enode_Lit is O_Enode_Type (OE_Lit);
   begin
      Check_Not_Composite (Lit.Ctype);
      return new O_Enode_Lit'(Kind => OE_Lit,
                              Rtype => Lit.Ctype,
                              Ref => False,
                              Lit => Lit);
   end New_Lit;

   ---------------------
   --  Declarations.  --
   ---------------------

   procedure New_Debug_Filename_Decl (Filename : String)
   is
      subtype O_Dnode_Filename_Decl is O_Dnode_Type (ON_Debug_Filename_Decl);
      N : O_Dnode;
   begin
      N := new O_Dnode_Filename_Decl;
      N.Filename := new String'(Filename);
      Add_Decl (N, False);
   end New_Debug_Filename_Decl;

   procedure New_Debug_Line_Decl (Line : Natural)
   is
      subtype O_Dnode_Line_Decl is O_Dnode_Type (ON_Debug_Line_Decl);
      N : O_Dnode;
   begin
      N := new O_Dnode_Line_Decl;
      N.Line := Line;
      Add_Decl (N, False);
   end New_Debug_Line_Decl;

   procedure New_Debug_Comment_Decl (Comment : String)
   is
      subtype O_Dnode_Comment_Decl is O_Dnode_Type (ON_Debug_Comment_Decl);
      N : O_Dnode;
   begin
      N := new O_Dnode_Comment_Decl;
      N.Comment := new String'(Comment);
      Add_Decl (N, False);
   end New_Debug_Comment_Decl;

   procedure New_Type_Decl (Ident : O_Ident; Atype : O_Tnode)
   is
      N : O_Dnode;
   begin
      if Atype.Decl /= null then
         --  Type was already declared.
         raise Type_Error;
      end if;
      N := new O_Dnode_Type (ON_Type_Decl);
      N.Name := Ident;
      N.Dtype := Atype;
      Atype.Decl := N;
      Add_Decl (N);
   end New_Type_Decl;

   procedure Check_Object_Storage (Storage : O_Storage) is
   begin
      if Current_Function /= null then
         --  Inside a subprogram.
         case Storage is
            when O_Storage_Public =>
               --  Cannot create public variables inside a subprogram.
               raise Syntax_Error;
            when O_Storage_Private
              | O_Storage_Local
              | O_Storage_External =>
               null;
         end case;
      else
         --  Global scope.
         case Storage is
            when O_Storage_Public
              | O_Storage_Private
              | O_Storage_External =>
               null;
            when O_Storage_Local =>
               --  Cannot create a local variables outside a subprogram.
               raise Syntax_Error;
         end case;
      end if;
   end Check_Object_Storage;

   procedure New_Const_Decl
     (Res : out O_Dnode;
      Ident : O_Ident;
      Storage : O_Storage;
      Atype : O_Tnode)
   is
      subtype O_Dnode_Const is O_Dnode_Type (ON_Const_Decl);
   begin
      Check_Complete_Type (Atype);
      if Storage = O_Storage_Local then
         --  A constant cannot be local.
         raise Syntax_Error;
      end if;
      Check_Object_Storage (Storage);
      Res := new O_Dnode_Const'(Kind => ON_Const_Decl,
                                Name => Ident,
                                Next => null,
                                Dtype => Atype,
                                Storage => Storage,
                                Scope => Current_Decl_Scope.Parent,
                                Lineno => 0,
                                Const_Value => O_Dnode_Null);
      Add_Decl (Res);
   end New_Const_Decl;

   --  Const is not modified
   pragma Warnings (Off, "*is not modified");

   procedure Start_Const_Value (Const : in out O_Dnode)
   is
      subtype O_Dnode_Const_Value is O_Dnode_Type (ON_Const_Value);
      N : O_Dnode;
   begin
      if Const.Const_Value /= O_Dnode_Null then
         --  Constant already has a value.
         raise Syntax_Error;
      end if;

      if Const.Storage = O_Storage_External then
         --  An external constant must not have a value.
         raise Syntax_Error;
      end if;

      --  FIXME: check scope is the same.

      N := new O_Dnode_Const_Value'(Kind => ON_Const_Value,
                                    Name => Const.Name,
                                    Next => null,
                                    Dtype => Const.Dtype,
                                    Storage => Const.Storage,
                                    Scope => Current_Decl_Scope.Parent,
                                    Lineno => 0,
                                    Const_Decl => Const,
                                    Value => O_Cnode_Null);
      Const.Const_Value := N;
      Add_Decl (N, False);
   end Start_Const_Value;

   procedure Finish_Const_Value (Const : in out O_Dnode; Val : O_Cnode)
   is
   begin
      if Const.Const_Value = O_Dnode_Null then
         --  Start_Const_Value not called.
         raise Syntax_Error;
      end if;
      if Const.Const_Value.Value /= O_Cnode_Null then
         --  Finish_Const_Value already called.
         raise Syntax_Error;
      end if;
      if Val = O_Cnode_Null then
         --  No value or bad type.
         raise Type_Error;
      end if;
      Check_Type (Val.Ctype, Const.Dtype);
      Const.Const_Value.Value := Val;
   end Finish_Const_Value;

   pragma Warnings (On, "*is not modified");

   procedure New_Var_Decl
     (Res : out O_Dnode;
      Ident : O_Ident;
      Storage : O_Storage;
      Atype : O_Tnode)
   is
      subtype O_Dnode_Var is O_Dnode_Type (ON_Var_Decl);
   begin
      Check_Complete_Type (Atype);
      Check_Object_Storage (Storage);
      Res := new O_Dnode_Var'(Kind => ON_Var_Decl,
                              Name => Ident,
                              Next => null,
                              Dtype => Atype,
                              Storage => Storage,
                              Lineno => 0,
                              Scope => Current_Decl_Scope.Parent);
      Add_Decl (Res);
   end New_Var_Decl;

   procedure Start_Subprogram_Decl_1
     (Interfaces : out O_Inter_List;
      Ident : O_Ident;
      Storage : O_Storage;
      Rtype : O_Tnode)
   is
      subtype O_Dnode_Function is O_Dnode_Type (ON_Function_Decl);
      N : O_Dnode;
   begin
      N := new O_Dnode_Function'(Kind => ON_Function_Decl,
                                 Next => null,
                                 Name => Ident,
                                 Dtype => Rtype,
                                 Storage => Storage,
                                 Scope => Current_Decl_Scope.Parent,
                                 Lineno => 0,
                                 Interfaces => null,
                                 Func_Body => null,
                                 Alive => False);
      Add_Decl (N);
      Interfaces.Func := N;
      Interfaces.Last := null;
   end Start_Subprogram_Decl_1;

   procedure Start_Function_Decl
     (Interfaces : out O_Inter_List;
      Ident : O_Ident;
      Storage : O_Storage;
      Rtype : O_Tnode)
   is
   begin
      Check_Not_Composite (Rtype);
      Check_Complete_Type (Rtype);
      Start_Subprogram_Decl_1 (Interfaces, Ident, Storage, Rtype);
   end Start_Function_Decl;

   procedure Start_Procedure_Decl
     (Interfaces : out O_Inter_List;
      Ident : O_Ident;
      Storage : O_Storage) is
   begin
      Start_Subprogram_Decl_1 (Interfaces, Ident, Storage, null);
   end Start_Procedure_Decl;

   procedure New_Interface_Decl
     (Interfaces : in out O_Inter_List;
      Res : out O_Dnode;
      Ident : O_Ident;
      Atype : O_Tnode)
   is
      subtype O_Dnode_Interface is O_Dnode_Type (ON_Interface_Decl);
   begin
      Check_Not_Composite (Atype);
      Check_Complete_Type (Atype);
      Res := new O_Dnode_Interface'(Kind => ON_Interface_Decl,
                                    Next => null,
                                    Name => Ident,
                                    Dtype => Atype,
                                    Storage => O_Storage_Private,
                                    Scope => Current_Decl_Scope.Parent,
                                    Lineno => 0,
                                    Func_Scope => Interfaces.Func);
      if Interfaces.Last = null then
         Interfaces.Func.Interfaces := Res;
      else
         Interfaces.Last.Next := Res;
      end if;
      Interfaces.Last := Res;
   end New_Interface_Decl;

   procedure Finish_Subprogram_Decl
     (Interfaces : in out O_Inter_List; Res : out O_Dnode)
   is
   begin
      Res := Interfaces.Func;
   end Finish_Subprogram_Decl;

   procedure Start_Subprogram_Body (Func : O_Dnode)
   is
      B : O_Dnode;
      S : O_Snode;
   begin
      if Func.Func_Body /= null then
         --  Function was already declared.
         raise Syntax_Error;
      end if;
      S := new O_Snode_Type (ON_Declare_Stmt);
      S.all := O_Snode_Type'(Kind => ON_Declare_Stmt,
                             Next => null,
                             Decls => null,
                             Stmts => null,
                             Lineno => 0,
                             Alive => True);
      B := new O_Dnode_Type (ON_Function_Body);
      B.all := O_Dnode_Type'(ON_Function_Body,
                             Name => Func.Name,
                             Dtype => Func.Dtype,
                             Storage => Func.Storage,
                             Scope => Current_Decl_Scope.Parent,
                             Lineno => 0,
                             Func_Decl => Func,
                             Func_Stmt => S,
                             Next => null);
      Add_Decl (B, False);
      Func.Func_Body := B;
      Push_Decl_Scope (S);
      Push_Stmt_Scope
        (new Stmt_Function_Scope_Type'(Kind => Stmt_Function,
                                       Parent => S,
                                       Prev => Current_Stmt_Scope,
                                       Prev_Function => Current_Function,
                                       Decl => Func));
      Current_Function := Current_Stmt_Scope;
      Func.Alive := True;
   end Start_Subprogram_Body;

   procedure Finish_Subprogram_Body is
   begin
      Pop_Decl_Scope;
      if Current_Function.Kind /= Stmt_Function then
         --  Internal error.
         raise Syntax_Error;
      end if;
      Current_Function.Decl.Alive := False;
      Current_Function := Current_Function.Prev_Function;
      Pop_Stmt_Scope (Stmt_Function);
   end Finish_Subprogram_Body;

   -------------------
   --  Statements.  --
   -------------------

   procedure New_Debug_Line_Stmt (Line : Natural)
   is
      subtype O_Snode_Line_Stmt is O_Snode_Type (ON_Debug_Line_Stmt);
   begin
      Add_Stmt (new O_Snode_Line_Stmt'(Kind => ON_Debug_Line_Stmt,
                                       Next => null,
                                       Lineno => 0,
                                       Line => Line));
   end New_Debug_Line_Stmt;

   procedure New_Debug_Comment_Stmt (Comment : String)
   is
      subtype O_Snode_Comment_Stmt is O_Snode_Type (ON_Debug_Comment_Stmt);
   begin
      Add_Stmt (new O_Snode_Comment_Stmt'(Kind => ON_Debug_Comment_Stmt,
                                          Next => null,
                                          Lineno => 0,
                                          Comment => new String'(Comment)));
   end New_Debug_Comment_Stmt;

   procedure Start_Declare_Stmt
   is
      N : O_Snode;
   begin
      N := new O_Snode_Type (ON_Declare_Stmt);
      Add_Stmt (N);
      Push_Decl_Scope (N);
      Push_Stmt_Scope
        (new Stmt_Declare_Scope_Type'(Kind => Stmt_Declare,
                                      Parent => N,
                                      Prev => Current_Stmt_Scope));
   end Start_Declare_Stmt;

   procedure Finish_Declare_Stmt is
   begin
      Pop_Decl_Scope;
      Pop_Stmt_Scope (Stmt_Declare);
   end Finish_Declare_Stmt;

   procedure New_Assign_Stmt (Target : O_Lnode; Value : O_Enode)
   is
      N : O_Snode;
   begin
      Check_Type (Target.Rtype, Value.Rtype);
      Check_Not_Composite (Target.Rtype);
      Check_Ref (Target);
      Check_Ref (Value);
      N := new O_Snode_Type (ON_Assign_Stmt);
      N.all := O_Snode_Type'(Kind => ON_Assign_Stmt,
                             Next => null,
                             Lineno => 0,
                             Target => Target,
                             Value => Value);
      Add_Stmt (N);
   end New_Assign_Stmt;

   procedure New_Return_Stmt_1 (Value : O_Enode)
   is
      subtype O_Snode_Return_Stmt is O_Snode_Type (ON_Return_Stmt);
      N : O_Snode;
   begin
      N := new O_Snode_Return_Stmt'(Kind => ON_Return_Stmt,
                                    Next => null,
                                    Lineno => 0,
                                    Ret_Val => Value);
      Add_Stmt (N);
   end New_Return_Stmt_1;

   procedure New_Return_Stmt (Value : O_Enode)
   is
   begin
      if Current_Function = null
        or else Current_Function.Decl.Dtype = O_Tnode_Null
      then
         -- Either not in a function or in a procedure.
         raise Syntax_Error;
      end if;
      Check_Type (Value.Rtype, Current_Function.Decl.Dtype);
      Check_Ref (Value);
      New_Return_Stmt_1 (Value);
   end New_Return_Stmt;

   procedure New_Return_Stmt is
   begin
      if Current_Function = null
        or else Current_Function.Decl.Dtype /= O_Tnode_Null
      then
         -- Not in a procedure.
         raise Syntax_Error;
      end if;
      New_Return_Stmt_1 (null);
   end New_Return_Stmt;

   procedure Start_Association (Assocs : out O_Assoc_List; Subprg : O_Dnode)
   is
   begin
      Check_Scope (Subprg);
      Assocs.Subprg := Subprg;
      Assocs.Interfaces := Subprg.Interfaces;
      Assocs.First := null;
      Assocs.Last := null;
   end Start_Association;

   procedure New_Association (Assocs : in out O_Assoc_List; Val : O_Enode)
   is
      N : O_Anode;
   begin
      Check_Type (Assocs.Interfaces.Dtype, Val.Rtype);
      Check_Ref (Val);
      N := new O_Anode_Type'(Next => null,
                             Formal => Assocs.Interfaces, Actual => Val);
      Assocs.Interfaces := Assocs.Interfaces.Next;
      if Assocs.Last = null then
         Assocs.First := N;
      else
         Assocs.Last.Next := N;
      end if;
      Assocs.Last := N;
   end New_Association;

   function New_Function_Call (Assocs : O_Assoc_List) return O_Enode
   is
      subtype O_Enode_Call is O_Enode_Type (OE_Function_Call);
      Res : O_Enode;
   begin
      if Assocs.Interfaces /= null then
         --  Not enough arguments.
         raise Syntax_Error;
      end if;
      if Assocs.Subprg.Dtype = null then
         --  This is a procedure.
         raise Syntax_Error;
      end if;

      Res := new O_Enode_Call'(Kind => OE_Function_Call,
                               Rtype => Assocs.Subprg.Dtype,
                               Ref => False,
                               Func => Assocs.Subprg,
                               Assoc => Assocs.First);
      return Res;
   end New_Function_Call;

   procedure New_Procedure_Call (Assocs : in out O_Assoc_List)
   is
      N : O_Snode;
   begin
      if Assocs.Interfaces /= null then
         --  Not enough arguments.
         raise Syntax_Error;
      end if;
      if Assocs.Subprg.Dtype /= null then
         --  This is a function.
         raise Syntax_Error;
      end if;
      N := new O_Snode_Type (ON_Call_Stmt);
      N.Proc := Assocs.Subprg;
      N.Assoc := Assocs.First;
      Add_Stmt (N);
   end New_Procedure_Call;

   procedure New_Elsif_Stmt (Block : in out O_If_Block; Cond : O_Enode);

   procedure Start_If_Stmt (Block : in out O_If_Block; Cond : O_Enode)
   is
      subtype O_Snode_If is O_Snode_Type (ON_If_Stmt);
      N : O_Snode;
   begin
      --  Note: no checks are performed here, since they are done in
      --  new_elsif_stmt.
      N := new O_Snode_If'(Kind => ON_If_Stmt,
                           Next => null,
                           Lineno => 0,
                           Elsifs => null,
                           If_Last => null);
      Add_Stmt (N);
      Push_Stmt_Scope (new Stmt_If_Scope_Type'(Kind => Stmt_If,
                                               Parent => N,
                                               Prev => Current_Stmt_Scope,
                                               Last_Elsif => null));
      New_Elsif_Stmt (Block, Cond);
   end Start_If_Stmt;

   procedure New_Elsif_Stmt (Block : in out O_If_Block; Cond : O_Enode)
   is
      pragma Unreferenced (Block);
      N : O_Snode;
   begin
      if Cond /= null then
         if Cond.Rtype.Kind /= ON_Boolean_Type then
            raise Type_Error;
         end if;
         Check_Ref (Cond);
      end if;
      N := new O_Snode_Type (ON_Elsif_Stmt);
      N.all := O_Snode_Type'(Kind => ON_Elsif_Stmt,
                             Next => null,
                             Lineno => 0,
                             Cond => Cond,
                             Next_Elsif => null);
      if Current_Stmt_Scope.Kind /= Stmt_If then
         raise Syntax_Error;
      end if;
      Add_Stmt (N);
      if Current_Stmt_Scope.Last_Elsif = null then
         Current_Stmt_Scope.Parent.Elsifs := N;
      else
         --  Check for double 'else'
         if Current_Stmt_Scope.Last_Elsif.Cond = null then
            raise Syntax_Error;
         end if;
         Current_Stmt_Scope.Last_Elsif.Next_Elsif := N;
      end if;
      Current_Stmt_Scope.Last_Elsif := N;
   end New_Elsif_Stmt;

   procedure New_Else_Stmt (Block : in out O_If_Block) is
   begin
      New_Elsif_Stmt (Block, null);
   end New_Else_Stmt;

   procedure Finish_If_Stmt (Block : in out O_If_Block)
   is
      pragma Unreferenced (Block);
      Parent : O_Snode;
   begin
      Parent := Current_Stmt_Scope.Parent;
      Pop_Stmt_Scope (Stmt_If);
      Parent.If_Last := Current_Decl_Scope.Last_Stmt;
   end Finish_If_Stmt;

   procedure Start_Loop_Stmt (Label : out O_Snode)
   is
      subtype O_Snode_Loop_Type is O_Snode_Type (ON_Loop_Stmt);
   begin
      Current_Loop_Level := Current_Loop_Level + 1;
      Label := new O_Snode_Loop_Type'(Kind => ON_Loop_Stmt,
                                      Next => null,
                                      Lineno => 0,
                                      Loop_Last => null,
                                      Loop_Level => Current_Loop_Level);
      Add_Stmt (Label);
      Push_Stmt_Scope (new Stmt_Loop_Scope_Type'(Kind => Stmt_Loop,
                                                 Parent => Label,
                                                 Prev => Current_Stmt_Scope));
   end Start_Loop_Stmt;

   procedure Finish_Loop_Stmt (Label : in out O_Snode)
   is
      pragma Unreferenced (Label);
      Parent : O_Snode;
   begin
      Parent := Current_Stmt_Scope.Parent;
      Pop_Stmt_Scope (Stmt_Loop);
      Parent.Loop_Last := Current_Decl_Scope.Last_Stmt;
      Current_Loop_Level := Current_Loop_Level - 1;
   end Finish_Loop_Stmt;

   procedure New_Exit_Next_Stmt (Kind : ON_Stmt_Kind; L : O_Snode)
   is
      N : O_Snode;
   begin
      N := new O_Snode_Type (Kind);
      N.Next := null;
      N.Loop_Id := L;
      Add_Stmt (N);
   end New_Exit_Next_Stmt;

   procedure New_Exit_Stmt (L : O_Snode) is
   begin
      New_Exit_Next_Stmt (ON_Exit_Stmt, L);
   end New_Exit_Stmt;

   procedure New_Next_Stmt (L : O_Snode) is
   begin
      New_Exit_Next_Stmt (ON_Next_Stmt, L);
   end New_Next_Stmt;

   procedure Start_Case_Stmt (Block : in out O_Case_Block; Value : O_Enode)
   is
      subtype O_Snode_Case_Type is O_Snode_Type (ON_Case_Stmt);
      N : O_Snode;
   begin
      case Value.Rtype.Kind is
         when ON_Boolean_Type
           | ON_Unsigned_Type
           | ON_Signed_Type
           | ON_Enum_Type =>
            null;
         when others =>
            raise Type_Error;
      end case;
      Check_Ref (Value);
      N := new O_Snode_Case_Type'(Kind => ON_Case_Stmt,
                                  Next => null,
                                  Lineno => 0,
                                  Case_Last => null,
                                  Selector => Value,
                                  Branches => null);
      Block.Case_Stmt := N;
      Add_Stmt (N);
      Push_Stmt_Scope (new Stmt_Case_Scope_Type'(Kind => Stmt_Case,
                                                 Parent => N,
                                                 Prev => Current_Stmt_Scope,
                                                 Last_Branch => null,
                                                 Last_Choice => null,
                                                 Case_Type => Value.Rtype));
   end Start_Case_Stmt;

   procedure Start_Choice (Block : in out O_Case_Block)
   is
      N : O_Snode;
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are adding a branch outside a case statment.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Last_Choice /= null then
         --  You are creating branch while the previous one was not finished.
         raise Syntax_Error;
      end if;

      N := new O_Snode_Type (ON_When_Stmt);
      N.all := O_Snode_Type'(Kind => ON_When_Stmt,
                             Next => null,
                             Lineno => 0,
                             Branch_Parent => Block.Case_Stmt,
                             Choice_List => null,
                             Next_Branch => null);
      if Current_Stmt_Scope.Last_Branch = null then
         Current_Stmt_Scope.Parent.Branches := N;
      else
         Current_Stmt_Scope.Last_Branch.Next_Branch := N;
      end if;
      Current_Stmt_Scope.Last_Branch := N;
      Current_Stmt_Scope.Last_Choice := null;
      Add_Stmt (N);
   end Start_Choice;

   procedure Add_Choice (Block : in out O_Case_Block; Choice : O_Choice)
   is
      pragma Unreferenced (Block);
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are adding a choice not inside a case statement.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Last_Branch = null then
         --  You are not inside a branch.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Last_Choice = null then
         if Current_Stmt_Scope.Last_Branch.Choice_List /= null then
            --  The branch was already closed.
            raise Syntax_Error;
         end if;
         Current_Stmt_Scope.Last_Branch.Choice_List := Choice;
      else
         Current_Stmt_Scope.Last_Choice.Next := Choice;
      end if;
      Current_Stmt_Scope.Last_Choice := Choice;
   end Add_Choice;

   procedure New_Expr_Choice (Block : in out O_Case_Block; Expr : O_Cnode)
   is
      N : O_Choice;
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are creating a choice not inside a case statement.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Case_Type /= Expr.Ctype then
         --  Expr type is not the same as choice type.
         raise Type_Error;
      end if;

      N := new O_Choice_Type (ON_Choice_Expr);
      N.all := O_Choice_Type'(Kind => ON_Choice_Expr,
                              Next => null,
                              Expr => Expr);
      Add_Choice (Block, N);
   end New_Expr_Choice;

   procedure New_Range_Choice (Block : in out O_Case_Block;
                               Low, High : O_Cnode)
   is
      N : O_Choice;
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are creating a choice not inside a case statement.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Case_Type /= Low.Ctype
        or Current_Stmt_Scope.Case_Type /= High.Ctype
      then
         --  Low/High type is not the same as choice type.
         raise Type_Error;
      end if;

      N := new O_Choice_Type (ON_Choice_Range);
      N.all := O_Choice_Type'(Kind => ON_Choice_Range,
                              Next => null,
                              Low => Low,
                              High => High);
      Add_Choice (Block, N);
   end New_Range_Choice;

   procedure New_Default_Choice (Block : in out O_Case_Block)
   is
      N : O_Choice;
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are creating a choice not inside a case statement.
         raise Syntax_Error;
      end if;

      N := new O_Choice_Type (ON_Choice_Default);
      N.all := O_Choice_Type'(Kind => ON_Choice_Default,
                             Next => null);
      Add_Choice (Block, N);
   end New_Default_Choice;

   procedure Finish_Choice (Block : in out O_Case_Block)
   is
      pragma Unreferenced (Block);
   begin
      if Current_Stmt_Scope.Kind /= Stmt_Case then
         --  You are adding a choice not inside a case statement.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Last_Branch = null then
         --  You are not inside a branch.
         raise Syntax_Error;
      end if;
      if Current_Stmt_Scope.Last_Choice = null then
         --  The branch is empty or you are not inside a branch.
         raise Syntax_Error;
      end if;
      Current_Stmt_Scope.Last_Choice := null;
   end Finish_Choice;

   procedure Finish_Case_Stmt (Block : in out O_Case_Block)
   is
      pragma Unreferenced (Block);
      Parent : O_Snode;
   begin
      Parent := Current_Stmt_Scope.Parent;
      Pop_Stmt_Scope (Stmt_Case);
      Parent.Case_Last := Current_Decl_Scope.Last_Stmt;
   end Finish_Case_Stmt;

   procedure Init is
   begin
      Top := new O_Snode_Type (ON_Declare_Stmt);
      Push_Decl_Scope (Top);
   end Init;

   procedure Finish is
   begin
      Pop_Decl_Scope;
   end Finish;
end Ortho_Debug;