File: parser.cxx

package info (click to toggle)
odb 2.4.0-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,716 kB
  • ctags: 6,755
  • sloc: cpp: 41,102; sh: 11,329; makefile: 35
file content (2254 lines) | stat: -rw-r--r-- 54,755 bytes parent folder | download | duplicates (2)
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
// file      : odb/parser.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/gcc.hxx> // Keep it first.

#include <set>
#include <map>
#include <vector>
#include <string>
#include <cassert>
#include <sstream>
#include <iostream>

#include <odb/diagnostics.hxx>
#include <odb/parser.hxx>
#include <odb/semantics.hxx>

using namespace std;
using namespace semantics;

class parser::impl
{
public:
  typedef parser::failed failed;

  impl (options const&, loc_pragmas&, ns_loc_pragmas&, decl_pragmas&);

  auto_ptr<unit>
  parse (tree global_scope, path const& main_file);

private:
  typedef semantics::access access;

  // Extended GCC tree declaration that is either a GCC tree
  // declaration, a virtual declaration, or a pragma. If it is
  // a pragma, then the assoc flag indicated whether this pragma
  // has been associated with a declaration. Otherwise, the assoc
  // flag indicates whether pragmas have been associated with this
  // declaration (we use this to ignore certain declarations for
  // pragma association purposes, e.g., the anonymous type in
  // struct {...} m_).
  //
  struct tree_decl
  {
    tree decl;
    virt_declaration const* vdecl;
    pragma const* prag;
    mutable bool assoc; // Allow modification via std::set iterator.

    tree_decl (tree d): decl (d), vdecl (0), prag (0), assoc (false) {}
    tree_decl (virt_declaration const& d)
        : decl (0), vdecl (&d), prag (0), assoc (false) {}
    tree_decl (pragma const& p)
        : decl (0), vdecl (0), prag (&p), assoc (false) {}

    bool
    operator< (tree_decl const& y) const;
  };

  typedef multiset<tree_decl> decl_set;

private:
  void
  collect (tree ns);

  void
  emit ();

  // Emit a type declaration. This is either a named class-type definition/
  // declaration or a typedef. In the former case the function returns the
  // newly created type node. In the latter case it returns 0.
  //
  type*
  emit_type_decl (tree);

  // Emit a template declaration.
  //
  void
  emit_template_decl (tree);

  class_template&
  emit_class_template (tree, bool stub = false);

  union_template&
  emit_union_template (tree, bool stub = false);

  template <typename T>
  T&
  emit_class (tree, path const& f, size_t l, size_t c, bool stub = false);

  template <typename T>
  T&
  emit_union (tree, path const& f, size_t l, size_t c, bool stub = false);

  // Access is not used when creating a stub.
  //
  enum_&
  emit_enum (tree,
             access,
             path const& f,
             size_t l,
             size_t c,
             bool stub = false);

  // Create new or find existing semantic graph type.
  //
  type&
  emit_type (tree, access, path const& f, size_t l, size_t c);

  type&
  create_type (tree, access, path const& f, size_t l, size_t c);

  string
  emit_type_name (tree, bool direct = true);


  //
  // Pragma handling.
  //
  void
  add_pragma (node&, pragma const&);

  // Process positioned and named pragmas.
  //
  void
  process_pragmas (declaration const&,
                   node&,
                   string const& name,
                   decl_set::const_iterator begin,
                   decl_set::const_iterator cur,
                   decl_set::const_iterator end);

  // Process named pragmas only.
  //
  void
  process_named_pragmas (declaration const&, node&);

  void
  diagnose_unassoc_pragmas (decl_set const&);

  // Return declaration's fully-qualified scope name (e.g., ::foo::bar).
  //
  string
  fq_scope (tree);

  // Return declaration's access.
  //
  access
  decl_access (tree decl)
  {
    // Note that TREE_PUBLIC() returns something other than what we need.
    //
    if (TREE_PRIVATE (decl))
      return access::private_;

    if (TREE_PROTECTED (decl))
      return access::protected_;

    return access::public_;
  }

  //
  //
  template <typename T>
  void
  define_fund (tree);

private:
  options const& ops_;
  loc_pragmas& loc_pragmas_;
  ns_loc_pragmas& ns_loc_pragmas_;
  decl_pragmas& decl_pragmas_;

  bool trace;
  ostream& ts;

  unit* unit_;
  scope* scope_;
  size_t error_;

  decl_set decls_;

  typedef map<location_t, tree> decl_map;
  decl_map all_decls_;
};

bool parser::impl::tree_decl::
operator< (tree_decl const& y) const
{
  location_t xloc (
    decl != 0
    ? real_source_location (decl)
    : (vdecl != 0 ? vdecl->loc : prag->loc));

  location_t yloc (
    y.decl != 0
    ? real_source_location (y.decl)
    : (y.vdecl != 0 ? y.vdecl->loc : y.prag->loc));

  return xloc < yloc;
}

//
// Function templates.
//

template <typename T>
void parser::impl::
define_fund (tree t)
{
  t = TYPE_MAIN_VARIANT (t);
  char const* name (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t))));

  T& node (unit_->new_fund_node<T> (t));
  unit_->new_edge<defines> (*scope_, node, name);
  unit_->insert (t, node);

  process_named_pragmas (t, node);
}

template <typename T>
T& parser::impl::
emit_class (tree c, path const& file, size_t line, size_t clmn, bool stub)
{
  c = TYPE_MAIN_VARIANT (c);

  // See if there is a stub already for this type.
  //
  T* c_node (0);

  if (node* n = unit_->find (c))
  {
    c_node = &dynamic_cast<T&> (*n);
  }
  else
  {
    c_node = &unit_->new_node<T> (file, line, clmn, c);
    unit_->insert (c, *c_node);
  }

  if (stub || !COMPLETE_TYPE_P (c))
    return *c_node;

  // Traverse base information.
  //
  tree bis (TYPE_BINFO (c));
  size_t n (bis ? BINFO_N_BASE_BINFOS (bis) : 0);

  for (size_t i (0); i < n; i++)
  {
    tree bi (BINFO_BASE_BINFO (bis, i));
    access a (access::public_);

    if (BINFO_BASE_ACCESSES (bis))
    {
      tree ac (BINFO_BASE_ACCESS (bis, i));

      if (ac == NULL_TREE || ac == access_public_node)
      {
        a = access::public_;
      }
      else if (ac == access_protected_node)
      {
        a = access::protected_;
      }
      else
      {
        assert (ac == access_private_node);
        a = access::private_;
      }
    }

    bool virt (BINFO_VIRTUAL_P (bi));
    tree base (TYPE_MAIN_VARIANT (BINFO_TYPE (bi)));

    // Find the corresponding graph node. If we cannot find one then
    // the base is a template instantiation since an ordinary class
    // has to be defined (complete) in order to be a base.
    //
    class_* b_node (0);
    string name;

    if (node* n = unit_->find (base))
    {
      b_node = &dynamic_cast<class_&> (*n);

      if (trace)
        name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (base)));
    }
    else
    {
      // Use public access for a template instantiation in the inheritance
      // declaration.
      //
      b_node = &dynamic_cast<class_&> (
        emit_type (base, access::public_, file, line, clmn));

      if (trace)
        name = emit_type_name (base);
    }

    unit_->new_edge<inherits> (*c_node, *b_node, a, virt);

    if (trace)
      ts << "\t" << a.string () << (virt ? " virtual" : "") << " base "
         << name << " (" << static_cast<type*> (b_node) << ")" << endl;
  }

  // Collect member declarations so that we can traverse them in
  // the source code order.
  //
  decl_set decls;

  for (tree d (TYPE_FIELDS (c)); d != NULL_TREE; d = TREE_CHAIN (d))
  {
    switch (TREE_CODE (d))
    {
    case TYPE_DECL:
      {
        if (!DECL_SELF_REFERENCE_P (d))
          decls.insert (d);
        break;
      }
    case TEMPLATE_DECL:
      {
        if (DECL_CLASS_TEMPLATE_P (d))
          decls.insert (d);
        break;
      }
    case FIELD_DECL:
      {
        if (!DECL_ARTIFICIAL (d))
          decls.insert (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Add virtual declarations if any.
  //
  {
    virt_declarations::const_iterator i (virt_declarations_.find (c));

    if (i != virt_declarations_.end ())
      decls.insert (i->second.begin (), i->second.end ());
  }

  // Add location pragmas if any.
  //
  {
    loc_pragmas::const_iterator i (loc_pragmas_.find (c));

    if (i != loc_pragmas_.end ())
      decls.insert (i->second.begin (), i->second.end ());
  }

  scope* prev_scope (scope_);
  scope_ = c_node;

  for (decl_set::const_iterator b (decls.begin ()), i (b), e (decls.end ());
       i != e; ++i)
  {
    // Skip pragmas.
    //
    if (i->prag != 0)
      continue;

    // Handle virtual declarations.
    //
    if (i->vdecl != 0)
    {
      virt_declaration const& vd (*i->vdecl);

      switch (vd.tree_code)
      {
      case FIELD_DECL:
        {
          // First check that it doesn't conflict with any of the real
          // data members defined in this class.
          //
          tree d (
            lookup_qualified_name (
              c, get_identifier (vd.name.c_str ()), false, false));

          if (d != error_mark_node && TREE_CODE (d) == FIELD_DECL)
          {
            error (vd.loc) << "virtual data member declaration '" << vd.name
                           << "' conflicts with a previous declaration"
                           << endl;

            location_t l (real_source_location (d));
            info (l) << "'" << vd.name << "' was previously declared here"
                     << endl;

            throw failed ();
          }

          path file (LOCATION_FILE (vd.loc));
          size_t line (LOCATION_LINE (vd.loc));
          size_t clmn (LOCATION_COLUMN (vd.loc));

          access a (access::public_);

          type& type_node (emit_type (vd.type, a, file, line, clmn));
          data_member& member_node (
            unit_->new_node<data_member> (file, line, clmn, tree (0)));

          unit_->new_edge<names> (*c_node, member_node, vd.name, a);
          belongs& edge (unit_->new_edge<belongs> (member_node, type_node));

          // See if there is a name hint for this type.
          //
          if (names* hint = unit_->find_hint (vd.type))
            edge.hint (*hint);

          // Process pragmas that may be associated with this field.
          //
          process_pragmas (vd, member_node, vd.name, b, i, e);
          break;
        }
      default:
        {
          assert (false);
          break;
        }
      }
      continue;
    }

    tree d (i->decl);

    switch (TREE_CODE (d))
    {
    case TYPE_DECL:
      {
        type* n (emit_type_decl (d));

        // If this is a named class-type definition, then handle
        // the pragmas.
        //
        if (n != 0)
          process_pragmas (n->tree_node (), *n, n->name (), b, i, e);

        break;
      }
    case TEMPLATE_DECL:
      {
        emit_template_decl (d);
        break;
      }
    case FIELD_DECL:
      {
        // If this is a bit-field then TREE_TYPE may be a modified type
        // with lesser precision. In this case, DECL_BIT_FIELD_TYPE
        // will be the type that was original specified. Use that type
        // for now. Furthermore, bitfields can be anonymous, which we
        // ignore.
        //
        //
        bool bf (DECL_C_BIT_FIELD (d));

        if (bf && DECL_NAME (d) == 0)
          break;

        // Another case where we can have NULL name is anonymous struct
        // or union extension, for example:
        //
        // struct s
        // {
        //   union
        //   {
        //     int a;
        //     int b;
        //   };
        // };
        //
        // GCC appears to create a fake member for such a struct/union
        // without any name. Ignore such members for now.
        //
        if (DECL_NAME (d) == 0)
          break;

        tree t (bf ? DECL_BIT_FIELD_TYPE (d) : TREE_TYPE (d));

        char const* name (IDENTIFIER_POINTER (DECL_NAME (d)));

        path file (DECL_SOURCE_FILE (d));
        size_t line (DECL_SOURCE_LINE (d));
        size_t clmn (DECL_SOURCE_COLUMN (d));

        access a (decl_access (d));

        type& type_node (emit_type (t, a, file, line, clmn));
        data_member& member_node (
          unit_->new_node<data_member> (file, line, clmn, d));
        unit_->insert (d, member_node);

        unit_->new_edge<names> (*c_node, member_node, name, a);
        belongs& edge (unit_->new_edge<belongs> (member_node, type_node));

        // See if there is a name hint for this type.
        //
        if (names* hint = unit_->find_hint (t))
          edge.hint (*hint);

        if (trace)
        {
          string type_name (emit_type_name (t));

          ts << "\t" << a.string () << " data member " << type_name
             << " (" << &type_node << ") " << name << " at "
             << file << ":" << line << endl;
        }

        // Process pragmas that may be associated with this field.
        //
        process_pragmas (d, member_node, name, b, i, e);

        break;
      }
    default:
      {
        assert (false);
        break;
      }
    }
  }

  // Diagnose any position pragmas that haven't been associated.
  //
  diagnose_unassoc_pragmas (decls);

  scope_ = prev_scope;
  return *c_node;
}

template <typename T>
T& parser::impl::
emit_union (tree u, path const& file, size_t line, size_t clmn, bool stub)
{
  u = TYPE_MAIN_VARIANT (u);

  // See if there is a stub already for this type.
  //
  T* u_node (0);

  if (node* n = unit_->find (u))
  {
    u_node = &dynamic_cast<T&> (*n);
  }
  else
  {
    u_node = &unit_->new_node<T> (file, line, clmn, u);
    unit_->insert (u, *u_node);
  }

  if (stub || !COMPLETE_TYPE_P (u))
    return *u_node;

  // Collect member declarations so that we can traverse them in
  // the source code order.
  //
  decl_set decls;

  for (tree d (TYPE_FIELDS (u)); d != NULL_TREE ; d = TREE_CHAIN (d))
  {
    switch (TREE_CODE (d))
    {
    case TYPE_DECL:
      {
        if (!DECL_SELF_REFERENCE_P (d))
          decls.insert (d);
        break;
      }
    case TEMPLATE_DECL:
      {
        if (DECL_CLASS_TEMPLATE_P (d))
          decls.insert (d);
        break;
      }
    case FIELD_DECL:
      {
        if (!DECL_ARTIFICIAL (d))
          decls.insert (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Add location pragmas if any.
  //
  {
    loc_pragmas::const_iterator i (loc_pragmas_.find (u));

    if (i != loc_pragmas_.end ())
      decls.insert (i->second.begin (), i->second.end ());
  }

  scope* prev_scope (scope_);
  scope_ = u_node;

  for (decl_set::const_iterator b (decls.begin ()), i (b), e (decls.end ());
       i != e; ++i)
  {
    // Skip pragmas.
    //
    if (i->prag)
      continue;

    tree d (i->decl);

    switch (TREE_CODE (d))
    {
    case TYPE_DECL:
      {
        type* n (emit_type_decl (d));

        // If this is a named class-type definition, then handle
        // the pragmas.
        //
        if (n != 0)
          process_pragmas (n->tree_node (), *n, n->name (), b, i, e);

        break;
      }
    case TEMPLATE_DECL:
      {
        emit_template_decl (d);
        break;
      }
    case FIELD_DECL:
      {
        // We can have NULL name in case of anonymous struct or union
        // extension, for example:
        //
        // union s
        // {
        //   struct
        //   {
        //     int a;
        //     int b;
        //   };
        //   int c;
        // };
        //
        // GCC appears to create a fake member for such a struct/union
        // without any name. Ignore such members for now.
        //
        if (DECL_NAME (d) == 0)
          break;

        tree t (TREE_TYPE (d));
        char const* name (IDENTIFIER_POINTER (DECL_NAME (d)));

        path file (DECL_SOURCE_FILE (d));
        size_t line (DECL_SOURCE_LINE (d));
        size_t clmn (DECL_SOURCE_COLUMN (d));

        access a (decl_access (d));

        type& type_node (emit_type (t, a, file, line, clmn));
        data_member& member_node (
          unit_->new_node<data_member> (file, line, clmn, d));
        unit_->insert (d, member_node);

        unit_->new_edge<names> (*u_node, member_node, name, a);
        belongs& edge (unit_->new_edge<belongs> (member_node, type_node));

        // See if there is a name hint for this type.
        //
        if (names* hint = unit_->find_hint (t))
          edge.hint (*hint);

        if (trace)
        {
          string type_name (emit_type_name (t));

          ts << "\t" << a.string () << " union member " << type_name
             << " (" << &type_node << ") " << name << " at "
             << file << ":" << line << endl;
        }

        // Process pragmas that may be associated with this field.
        //
        process_pragmas (d, member_node, name, b, i, e);

        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Diagnose any position pragmas that haven't been associated.
  //
  diagnose_unassoc_pragmas (decls);

  scope_ = prev_scope;
  return *u_node;
}

//
// Functions.
//

parser::impl::
impl (options const& ops,
      loc_pragmas & lp,
      ns_loc_pragmas& nslp,
      decl_pragmas& dp)
    : ops_ (ops),
      loc_pragmas_ (lp),
      ns_loc_pragmas_ (nslp),
      decl_pragmas_ (dp),
      trace (ops.trace ()),
      ts (cerr)
{
}

auto_ptr<unit> parser::impl::
parse (tree global_scope, path const& main_file)
{
  auto_ptr<unit> u (new unit (main_file));
  u->insert (global_namespace, *u);
  process_named_pragmas (global_namespace, *u);

  unit_ = u.get ();
  scope_ = unit_;
  error_ = 0;

  // Define fundamental types.
  //
  define_fund<fund_void> (void_type_node);
  define_fund<fund_bool> (boolean_type_node);
  define_fund<fund_char> (char_type_node);
  define_fund<fund_wchar> (wchar_type_node);

  if (ops_.std () >= cxx_version::cxx11)
  {
    define_fund<fund_char16> (char16_type_node);
    define_fund<fund_char32> (char32_type_node);
  }

  define_fund<fund_signed_char> (signed_char_type_node);
  define_fund<fund_unsigned_char> (unsigned_char_type_node);
  define_fund<fund_short> (short_integer_type_node);
  define_fund<fund_unsigned_short> (short_unsigned_type_node);
  define_fund<fund_int> (integer_type_node);
  define_fund<fund_unsigned_int> (unsigned_type_node);
  define_fund<fund_long> (long_integer_type_node);
  define_fund<fund_unsigned_long> (long_unsigned_type_node);
  define_fund<fund_long_long> (long_long_integer_type_node);
  define_fund<fund_unsigned_long_long> (long_long_unsigned_type_node);
  define_fund<fund_float> (float_type_node);
  define_fund<fund_double> (double_type_node);
  define_fund<fund_long_double> (long_double_type_node);

  // First collect all the namespace-level declarations we are
  // interested in in the line-decl map so that they appear in
  // the source code order.
  //
  collect (global_scope);

  // Add namespace-level position pragmas if any.
  //
  {
    loc_pragmas::const_iterator i (loc_pragmas_.find (global_namespace));

    if (i != loc_pragmas_.end ())
      decls_.insert (i->second.begin (), i->second.end ());
  }

  // Convert position namespace pragmas to name pragmas.
  //
  for (ns_loc_pragmas::const_iterator i (ns_loc_pragmas_.begin ());
       i != ns_loc_pragmas_.end (); ++i)
  {
    pragma const& p (i->pragma);

    decl_map::const_iterator j (all_decls_.lower_bound (p.loc));

    if (j == all_decls_.end ())
    {
      error (p.loc)
        << "db pragma '" << p.pragma_name << "' is not associated with a "
        << "namespace declaration" << endl;
      error_++;
      continue;
    }

    // Find the "namespace difference" between this declaration and
    // the pragma's namespace. The outermost namespace in the result
    // is what we are looking for.
    //
    tree ns (0);

    for (tree prev (j->second), scope (CP_DECL_CONTEXT (prev));;
         scope = CP_DECL_CONTEXT (scope))
    {
      if (scope == i->ns)
      {
        ns = prev;
        break;
      }

      if (scope == global_namespace)
        break;

      prev = scope;
    }

    if (ns == 0 || TREE_CODE (ns) != NAMESPACE_DECL)
    {
      error (p.loc)
        << "db pragma '" << p.pragma_name << "' is not associated with a "
        << "namespace declaration" << endl;
      error_++;
      continue;
    }

    pragma_set& s (decl_pragmas_[ns]);
    pragma_set::iterator it (s.find (p));

    // Make sure we override only if this pragma came after the one
    // already in the set.
    //
    if (it == s.end () || it->loc < p.loc)
      s.insert (p);
  }

  // Construct the semantic graph.
  //
  if (error_ == 0)
    emit ();

  if (error_ > 0)
    throw failed ();

  return u;
}

void parser::impl::
collect (tree ns)
{
  cp_binding_level* level = NAMESPACE_LEVEL (ns);
  tree decl = level->names;

  // Collect declarations.
  //
  for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
  {
    all_decls_[real_source_location (decl)] = decl;

    if (DECL_IS_BUILTIN (decl))
      continue;

    switch (TREE_CODE (decl))
    {
    case TYPE_DECL:
      {
        if (DECL_NAME (decl) != NULL_TREE)
          decls_.insert (decl);

        break;
      }
    case TEMPLATE_DECL:
      {
        if (DECL_CLASS_TEMPLATE_P (decl))
          decls_.insert (decl);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Traverse namespaces.
  //
  for (decl = level->namespaces; decl != NULL_TREE; decl = TREE_CHAIN (decl))
  {
    if (!DECL_IS_BUILTIN (decl) || DECL_NAMESPACE_STD_P (decl))
    {
      if (trace)
      {
        tree dn (DECL_NAME (decl));
        char const* name (dn ? IDENTIFIER_POINTER (dn) : "<anonymous>");

        ts << "namespace " << name << " at "
           << DECL_SOURCE_FILE (decl) << ":"
           << DECL_SOURCE_LINE (decl) << endl;
      }

      collect (decl);
    }
  }
}

void parser::impl::
emit ()
{
  for (decl_set::const_iterator b (decls_.begin ()), i (b),
         e (decls_.end ()); i != e; ++i)
  {
    // Skip pragmas.
    //
    if (i->prag)
      continue;

    tree decl (i->decl);

    // Get this declaration's namespace and unwind our scope until
    // we find a common prefix of namespaces.
    //
    string pfx;
    string ns (fq_scope (decl));

    for (pfx = scope_->fq_name (); !pfx.empty (); pfx = scope_->fq_name ())
    {
      if (ns.compare (0, pfx.size (), pfx) == 0)
        break;

      if (trace)
        ts << "closing namespace " << scope_->name () << endl;

      scope_ = &scope_->scope_ ();
    }

    // Build the rest of the namespace hierarchy for this declaration.
    //
    if (ns != pfx)
    {
      path f (DECL_SOURCE_FILE (decl));
      size_t l (DECL_SOURCE_LINE (decl));
      size_t c (DECL_SOURCE_COLUMN (decl));

      for (size_t b (pfx.size () + 2), e (ns.find ("::", b));
           b != string::npos;)
      {
        string n (ns, b, e == string::npos ? e : e - b);

        if (trace)
          ts << "opening namespace " << n << " for "
             << DECL_SOURCE_FILE (decl) << ":"
             << DECL_SOURCE_LINE (decl) << endl;

        // Use the declarations's file, line, and column as an
        // approximation for this namespace origin. Also resolve
        // the tree node for this namespace.
        //
        tree tree_node (
          namespace_binding (
            get_identifier (n.c_str ()), scope_->tree_node ()));

        namespace_& node (unit_->new_node<namespace_> (f, l, c, tree_node));
        unit_->new_edge<defines> (*scope_, node, n);

        if (namespace_* orig =
            dynamic_cast<namespace_*> (unit_->find (tree_node)))
        {
          // This is an extension.
          //
          node.original (*orig);
        }
        else
        {
          // This is the original. Add it to the map and process any
          // pragmas it might have (at this stage namespaces can only
          // have name pragmas).
          //
          unit_->insert (tree_node, node);
          process_named_pragmas (tree_node, node);
        }

        scope_ = &node;

        if (e == string::npos)
          b = e;
        else
        {
          b = e + 2;
          e = ns.find ("::", b);
        }
      }
    }

    switch (TREE_CODE (decl))
    {
    case TYPE_DECL:
      {
        type* n (emit_type_decl (decl));

        // If this is a named class-type definition, then handle
        // the pragmas.
        //
        if (n != 0)
          process_pragmas (n->tree_node (), *n, n->name (), b, i, e);

        break;
      }
    case TEMPLATE_DECL:
      {
        emit_template_decl (decl);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Diagnose any position pragmas that haven't been associated.
  //
  diagnose_unassoc_pragmas (decls_);
}

type* parser::impl::
emit_type_decl (tree decl)
{
  tree t (TREE_TYPE (decl));
  gcc_tree_code_type tc (TREE_CODE (t));

  tree decl_name (DECL_NAME (decl));
  char const* name (IDENTIFIER_POINTER (decl_name));

  if (DECL_ARTIFICIAL (decl) &&
      (tc == RECORD_TYPE || tc == UNION_TYPE || tc == ENUMERAL_TYPE))
  {
    // If we have an anonymous class typedef, use the user-
    // supplied name instead of the synthesized one. ARM
    // says that in typedef struct {} S; S becomes struct's
    // name.
    //
    if (anon_aggrname_p (decl_name))
    {
      tree d (TYPE_NAME (t));

      if (d != NULL_TREE &&
          !DECL_ARTIFICIAL (d) &&
          DECL_NAME (d) != NULL_TREE &&
          !anon_aggrname_p (DECL_NAME (d)))
      {
        decl = d;
        decl_name = DECL_NAME (decl);
        name = IDENTIFIER_POINTER (decl_name);
      }
      else
      {
        // This type has only the synthesized name which means that
        // it is either typedef'ed as a derived type or it is used
        // to declare a varibale or similar. The first case will be
        // covered by the typedef handling code below. The second
        // case will be covere by emit_type().
        //
        return 0;
      }
    }

    path file (DECL_SOURCE_FILE (decl));
    size_t line (DECL_SOURCE_LINE (decl));
    size_t clmn (DECL_SOURCE_COLUMN (decl));

    type* node (0);

    // Pointers to member functions are represented as record
    // types. Detect and handle this case.
    //
    if (TYPE_PTRMEMFUNC_P (t))
    {
      t = TYPE_MAIN_VARIANT (t);
      node = &unit_->new_node<unsupported_type> (
        file, line, clmn, t, "pointer_to_member_function_type");
      unit_->insert (t, *node);
    }
    else
    {

      if (trace)
        ts << "start " <<  gcc_tree_code_name(tc) << " " << name
           << " at " << file << ":" << line << endl;

      switch (tc)
      {
      case RECORD_TYPE:
        {
          node = &emit_class<class_> (t, file, line, clmn);
          break;
        }
      case UNION_TYPE:
        {
          node = &emit_union<union_> (t, file, line, clmn);
          break;
        }
      case ENUMERAL_TYPE:
        {
          node = &emit_enum (t, decl_access (decl), file, line, clmn);
          break;
        }
      default:
        break;
      }

      if (trace)
        ts << "end " <<  gcc_tree_code_name(tc) << " " << name
           << " (" << node << ") at "
           << DECL_SOURCE_FILE (decl) << ":"
           << DECL_SOURCE_LINE (decl) << endl;
    }

    if (COMPLETE_TYPE_P (t))
      unit_->new_edge<defines> (*scope_, *node, name);
    else
      unit_->new_edge<declares> (*scope_, *node, name);

    return node;
  }
  else
  {
    // Normal typedef. We need to detect and ignore the anonymous
    // class typedef case described above since we already used
    // this name to define the class.
    //
    if ((tc == RECORD_TYPE || tc == UNION_TYPE || tc == ENUMERAL_TYPE) &&
        TYPE_NAME (TYPE_MAIN_VARIANT (t)) == decl)
      return 0;

    path f (DECL_SOURCE_FILE (decl));
    size_t l (DECL_SOURCE_LINE (decl));
    size_t c (DECL_SOURCE_COLUMN (decl));

    type& node (emit_type (t, decl_access (decl), f, l, c));
    typedefs& edge (unit_->new_edge<typedefs> (*scope_, node, name));

    // Find our hint.
    //
    if (tree ot = DECL_ORIGINAL_TYPE (decl))
    {
      if (names* hint = unit_->find_hint (ot))
        edge.hint (*hint);
    }

    // Add this edge to the hint map. It may already be there if we
    // are handling something like this:
    //
    // typedef foo bar;
    // typedef bar foo;
    //
    // GCC also appears to re-purpose a node for another name (not
    // sure if its a bug or a feature), so use the latest seen name.
    //
    unit_->insert_hint (t, edge);

    if (trace)
    {
      string s (emit_type_name (t, false));

      ts << "typedef " << s << " (" << &node << ") -> " << name
         << " at " << f << ":" << l << endl;
    }

    return 0;
  }
}

void parser::impl::
emit_template_decl (tree decl)
{
  // Currently we only handle class/union templates.
  //
  tree t (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)));
  gcc_tree_code_type tc (TREE_CODE (t));

  if (trace)
  {
    ts << gcc_tree_code_name(tc) << " template (" << decl << ") "
       << IDENTIFIER_POINTER (DECL_NAME (decl)) << " (" << t << ") at "
       << DECL_SOURCE_FILE (decl) << ":"
       << DECL_SOURCE_LINE (decl) << endl;

    ts << "specializations:" << endl;

    for (tree s (DECL_TEMPLATE_SPECIALIZATIONS (decl));
         s != NULL_TREE; s = TREE_CHAIN (s))
    {
      tree t (TREE_TYPE (s));
      tree d (TYPE_NAME (t));

      ts << "\tspecialization " << t << " at "
         << DECL_SOURCE_FILE (d) << ":"
         << DECL_SOURCE_LINE (d) << endl;
    }

    ts << "instantiations:" << endl;

    for (tree i (DECL_TEMPLATE_INSTANTIATIONS (decl));
         i != NULL_TREE; i = TREE_CHAIN (i))
    {
      tree t (TREE_VALUE (i));
      tree d (TYPE_NAME (t));

      ts << "\tinstantiation " << t << " at "
         << DECL_SOURCE_FILE (d) << ":"
         << DECL_SOURCE_LINE (d) << endl;
    }
  }

  char const* name (IDENTIFIER_POINTER (DECL_NAME (decl)));

  if (trace)
    ts << "start " <<  gcc_tree_code_name(tc) << " template " << name << " at "
       << DECL_SOURCE_FILE (decl) << ":"
       << DECL_SOURCE_LINE (decl) << endl;

  type_template* t_node (0);

  if (tc == RECORD_TYPE)
    t_node = &emit_class_template (decl);
  else
    t_node = &emit_union_template (decl);

  if (COMPLETE_TYPE_P (t))
    unit_->new_edge<defines> (*scope_, *t_node, name);
  else
    unit_->new_edge<declares> (*scope_, *t_node, name);

  if (trace)
    ts << "end " <<  gcc_tree_code_name(tc) << " template " << name
       << " (" << t_node << ") at "
       << DECL_SOURCE_FILE (decl) << ":"
       << DECL_SOURCE_LINE (decl) << endl;
}

class_template& parser::impl::
emit_class_template (tree t, bool stub)
{
  // See if there is a stub already for this template.
  //
  class_template* ct_node (0);
  tree c (TREE_TYPE (DECL_TEMPLATE_RESULT (t)));

  if (node* n = unit_->find (t))
  {
    ct_node = &dynamic_cast<class_template&> (*n);
  }
  else
  {
    path f (DECL_SOURCE_FILE (t));
    size_t ln (DECL_SOURCE_LINE (t));
    size_t cl (DECL_SOURCE_COLUMN (t));

    ct_node = &unit_->new_node<class_template> (f, ln, cl, c);
    unit_->insert (t, *ct_node);
  }

  if (stub || !COMPLETE_TYPE_P (c))
    return *ct_node;

  // Collect member declarations so that we can traverse them in
  // the source code order. For now we are only interested in
  // nested class template declarations.
  //
  decl_set decls;

  for (tree d (TYPE_FIELDS (c)); d != NULL_TREE ; d = TREE_CHAIN (d))
  {
    switch (TREE_CODE (d))
    {
    case TEMPLATE_DECL:
      {
        if (DECL_CLASS_TEMPLATE_P (d))
          decls.insert (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  scope* prev_scope (scope_);
  scope_ = ct_node;

  for (decl_set::const_iterator i (decls.begin ()), e (decls.end ());
       i != e; ++i)
  {
    // Skip pragmas.
    //
    if (i->prag)
      continue;

    tree d (i->decl);

    switch (TREE_CODE (d))
    {
    case TEMPLATE_DECL:
      {
        emit_template_decl (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Diagnose any position pragmas that haven't been associated.
  //
  diagnose_unassoc_pragmas (decls);

  scope_ = prev_scope;
  return *ct_node;
}

union_template& parser::impl::
emit_union_template (tree t, bool stub)
{
  // See if there is a stub already for this template.
  //
  union_template* ut_node (0);
  tree u (TREE_TYPE (DECL_TEMPLATE_RESULT (t)));

  if (node* n = unit_->find (t))
  {
    ut_node = &dynamic_cast<union_template&> (*n);
  }
  else
  {
    path f (DECL_SOURCE_FILE (t));
    size_t l (DECL_SOURCE_LINE (t));
    size_t c (DECL_SOURCE_COLUMN (t));

    ut_node = &unit_->new_node<union_template> (f, l, c, u);
    unit_->insert (t, *ut_node);
  }

  if (stub || !COMPLETE_TYPE_P (u))
    return *ut_node;

  // Collect member declarations so that we can traverse them in
  // the source code order. For now we are only interested in
  // nested class template declarations.
  //
  decl_set decls;

  for (tree d (TYPE_FIELDS (u)); d != NULL_TREE ; d = TREE_CHAIN (d))
  {
    switch (TREE_CODE (d))
    {
    case TEMPLATE_DECL:
      {
        if (DECL_CLASS_TEMPLATE_P (d))
          decls.insert (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  scope* prev_scope (scope_);
  scope_ = ut_node;

  for (decl_set::const_iterator i (decls.begin ()), e (decls.end ());
       i != e; ++i)
  {
    // Skip pragmas.
    //
    if (i->prag)
      continue;

    tree d (i->decl);

    switch (TREE_CODE (d))
    {
    case TEMPLATE_DECL:
      {
        emit_template_decl (d);
        break;
      }
    default:
      {
        break;
      }
    }
  }

  // Diagnose any position pragmas that haven't been associated.
  //
  diagnose_unassoc_pragmas (decls);

  scope_ = prev_scope;
  return *ut_node;
}

enum_& parser::impl::
emit_enum (tree e,
           access access,
           path const& file,
           size_t line,
           size_t clmn,
           bool stub)
{
  e = TYPE_MAIN_VARIANT (e);

  // See if there is a stub already for this type.
  //
  enum_* e_node (0);

  if (node* n = unit_->find (e))
    e_node = &dynamic_cast<enum_&> (*n);
  else
  {
    e_node = &unit_->new_node<enum_> (file, line, clmn, e);

    // Set the underlying type even for incomplete (forward-declared) enums.
    //
    tree ut (ENUM_UNDERLYING_TYPE (e));
    names* hint (unit_->find_hint (ut));
    integral_type* un = dynamic_cast<integral_type*> (
      unit_->find (TYPE_MAIN_VARIANT (ut)));

    // For "old" enums GCC creates a distinct type node and the only way to
    // get to one of the known integrals is via its name.
    //
    if (un == 0)
    {
      ut =  TREE_TYPE (TYPE_NAME (ut));
      un = dynamic_cast<integral_type*> (unit_->find (TYPE_MAIN_VARIANT (ut)));
    }

    underlies& edge (unit_->new_edge<underlies> (*un, *e_node));

    if (hint != 0)
      edge.hint (*hint);

    unit_->insert (e, *e_node);
  }

  if (stub || !COMPLETE_TYPE_P (e))
    return *e_node;

  // Traverse enumerators.
  //
  for (tree er (TYPE_VALUES (e)); er != NULL_TREE ; er = TREE_CHAIN (er))
  {
    char const* name (IDENTIFIER_POINTER (TREE_PURPOSE (er)));
    tree decl (TREE_VALUE (er));
    tree tval (DECL_INITIAL (decl));

    unsigned long long val (integer_value (tval));

    // There doesn't seem to be a way to get the proper position for
    // each enumerator.
    //
    enumerator& er_node = unit_->new_node<enumerator> (
      file, line, clmn, er, val);
    unit_->new_edge<enumerates> (*e_node, er_node);
    unit_->insert (decl, er_node);

    // In C++11 the enumerators are always available in the enum's
    // scope, even for old enums.
    //
    if (ops_.std () >= cxx_version::cxx11)
      unit_->new_edge<names> (*e_node, er_node, name, access::public_);

    // Inject enumerators into the outer scope unless this is an
    // enum class.
    //
    if (UNSCOPED_ENUM_P (e))
      unit_->new_edge<names> (*scope_, er_node, name, access);

    if (trace)
      ts << "\tenumerator " << name << " at " << file << ":" << line << endl;
  }

  return *e_node;
}

type& parser::impl::
emit_type (tree t,
           access access,
           path const& file,
           size_t line,
           size_t clmn)
{
  tree mv (TYPE_MAIN_VARIANT (t));

  if (trace)
  {
    ts << gcc_tree_code_name(TREE_CODE (t)) << " " << t
       << " main " << mv << endl;

    for (tree v (TYPE_MAIN_VARIANT (t)); v != 0; v = TYPE_NEXT_VARIANT (v))
      ts << "\tvariant " << v << " " << CP_TYPE_CONST_P (v) << endl;
  }

  node* n (unit_->find (mv));

  type& r (n != 0
           ? dynamic_cast<type&> (*n)
           : create_type (t, access, file, line, clmn));

  if (trace && n != 0)
    ts << "found node " << &r << " for type " << mv << endl;

  if (cp_type_quals (t) == TYPE_UNQUALIFIED)
  {
    unit_->insert (t, r); // Add this variant to the map.
    return r;
  }

  // See if this type already has this variant.
  //
  bool qc (CP_TYPE_CONST_P (t));
  bool qv (CP_TYPE_VOLATILE_P (t));
  bool qr (CP_TYPE_RESTRICT_P (t));

  for (type::qualified_iterator i (r.qualified_begin ());
       i != r.qualified_end (); ++i)
  {
    qualifier& q (i->qualifier ());

    if (q.const_ () == qc && q.volatile_ () == qv && q.restrict_ () == qr)
    {
      if (trace)
        ts << "found qualifier variant " << &q << endl;

      unit_->insert (t, q); // Add this variant to the map.
      return q;
    }
  }

  // No such variant yet. Create a new one. Qualified types are not
  // unique in the tree so don't add this node to the map.
  //
  qualifier& q (unit_->new_node<qualifier> (file, line, clmn, t, qc, qv, qr));
  qualifies& e (unit_->new_edge<qualifies> (q, r));
  unit_->insert (t, q);

  // See if there is a name hint for this type.
  //
  // If TREE_TYPE (TYPE_NAME (t)) != t then we have an inline qualifier,
  // as in:
  //
  // const foo x;
  //
  // If they are equal, then there are two possible cases. The first is
  // when we have a qualifier typedef, as in:
  //
  // typedef const foo cfoo;
  // cfoo x;
  //
  // The second is when we have a qualifier typedef but what is actually
  // used in the declaration is an inline qualifier, as in:
  //
  // typedef const foo cfoo;
  // const foo x;
  //
  // Unfortunately, in GCC, these two cases are indistinguishable. In
  // certain cases this can lead to a wrong hint being used for the base
  // type, for example:
  //
  // typedef foo my_foo;
  // typedef foo foo_t;
  // typedef const foo_t cfoo;
  //
  // const my_foo x;
  //
  // Above, the hint will be foo_t while it should be my_foo.
  //
  tree bt (0);

  if (tree decl = TYPE_NAME (t))
  {
    bt = TREE_TYPE (decl);

    if (t == bt)
    {
      // A const type can be named only with a typedef. Get the
      // original type.
      //
      tree ot (DECL_ORIGINAL_TYPE (decl));

      // And chase it one more time to get rid of the qualification.
      //
      decl = TYPE_NAME (ot);
      bt = decl != 0 ? TREE_TYPE (decl) : 0;
    }
  }

  if (bt != 0)
  {
    if (names* hint = unit_->find_hint (bt))
      e.hint (*hint);
  }

  process_named_pragmas (t, q);

  return q;
}

type& parser::impl::
create_type (tree t,
             access access,
             path const& file,
             size_t line,
             size_t clmn)
{
  type* r (0);
  gcc_tree_code_type tc (TREE_CODE (t));

  switch (tc)
  {
    //
    // User-defined types.
    //
  case RECORD_TYPE:
  case UNION_TYPE:
    {
      t = TYPE_MAIN_VARIANT (t);
      tree ti (TYPE_TEMPLATE_INFO (t));

      if (ti == NULL_TREE)
      {
        // Ordinary class. There are two situations which can lead
        // here. First is when we have an anonymous class that is
        // part of the declaration, for example:
        //
        // typedef const struct {...} s;
        //
        // The second situation is a named class definition which
        // we haven't parsed yet. In this case we are going to
        // create a "stub" class node which will be processed and
        // filled in later.
        //

        // Pointers to member functions are represented as record
        // types. Detect and handle this case.
        //
        if (TYPE_PTRMEMFUNC_P (t))
        {
          r = &unit_->new_node<unsupported_type> (
            file, line, clmn, t, "pointer_to_member_function_type");
          unit_->insert (t, *r);
        }
        else
        {
          tree d (TYPE_NAME (t));

          if (trace)
            ts << "start anon/stub " << gcc_tree_code_name(tc) << " at "
               << file << ":" << line << endl;

          if (d == NULL_TREE || anon_aggrname_p (DECL_NAME (d)))
          {
            if (tc == RECORD_TYPE)
              r = &emit_class<class_> (t, file, line, clmn);
            else
              r = &emit_union<union_> (t, file, line, clmn);
          }
          else
          {
            // Use the "defining" declaration's file, line, and column
            // information to create the stub.
            //
            path f (DECL_SOURCE_FILE (d));
            size_t l (DECL_SOURCE_LINE (d));
            size_t c (DECL_SOURCE_COLUMN (d));

            if (tc == RECORD_TYPE)
              r = &emit_class<class_> (t, f, l, c, true);
            else
              r = &emit_union<union_> (t, f, l, c, true);
          }

          if (trace)
            ts << "end anon/stub " << gcc_tree_code_name(tc) << " (" << r << ")"
               << " at " << file << ":" << line << endl;
        }
      }
      else
      {
        // Template instantiation.
        //
        tree decl (TI_TEMPLATE (ti)); // DECL_TEMPLATE

        // Get to the most general template declaration.
        //
        while (DECL_TEMPLATE_INFO (decl))
          decl = DECL_TI_TEMPLATE (decl);

        type_template* t_node (0);

        // Find the template node or create a stub if none exist.
        //
        if (node* n = unit_->find (decl))
          t_node = &dynamic_cast<type_template&> (*n);
        else
        {
          if (trace)
            ts << "start stub " << gcc_tree_code_name(tc) << " template for ("
               << decl << ") at " << file << ":" << line << endl;

          if (tc == RECORD_TYPE)
            t_node = &emit_class_template (decl, true);
          else
            t_node = &emit_union_template (decl, true);

          if (trace)
            ts << "end stub " << gcc_tree_code_name(tc) << " template ("
               << t_node << ") at " << file << ":" << line << endl;
        }

        if (trace)
          ts << "start " << gcc_tree_code_name(tc) << " instantiation ("
             << t << ") for template (" << t_node << ")"
             << " at " << file << ":" << line << endl;

        type_instantiation* i_node (0);

        if (tc == RECORD_TYPE)
          i_node = &emit_class<class_instantiation> (t, file, line, clmn);
        else
          i_node = &emit_union<union_instantiation> (t, file, line, clmn);

        if (trace)
          ts << "end " << gcc_tree_code_name(tc) << " instantiation ("
             << static_cast<type*> (i_node) << ")"
             << " at " << file << ":" << line << endl;

        unit_->new_edge<instantiates> (*i_node, *t_node);
        process_named_pragmas (t, *i_node);
        r = i_node;
      }

      break;
    }
  case ENUMERAL_TYPE:
    {
      // The same logic as in the "ordinary class" case above
      // applies here.
      //

      t = TYPE_MAIN_VARIANT (t);
      tree d (TYPE_NAME (t));

      if (trace)
        ts << "start anon/stub " << gcc_tree_code_name(tc) << " at "
           << file << ":" << line << endl;

      if (d == NULL_TREE || anon_aggrname_p (DECL_NAME (d)))
      {
        r = &emit_enum (t, access, file, line, clmn);
      }
      else
      {
        // Use the "defining" declaration's file, line, and column
        // information to create the stub.
        //
        path f (DECL_SOURCE_FILE (d));
        size_t l (DECL_SOURCE_LINE (d));
        size_t c (DECL_SOURCE_COLUMN (d));

        r = &emit_enum (t, access, f, l, c, true);
      }

      if (trace)
        ts << "end anon/stub " << gcc_tree_code_name(tc) << " (" << r << ")"
           << " at " << file << ":" << line << endl;

      break;
    }

    //
    // Derived types.
    //

  case ARRAY_TYPE:
    {
      unsigned long long size (0);

      if (tree index = TYPE_DOMAIN (t))
      {
        tree max (TYPE_MAX_VALUE (index));

        if (TREE_CODE (max) == INTEGER_CST)
        {
          size = integer_value (max);

          // The docs say that TYPE_DOMAIN will be NULL if the
          // array doesn't specify bounds. In reality, it is
          // set to ~0.
          //
          if (size == ~(unsigned long long) (0))
            size = 0;

          size++; // Convert max index to size.
        }
        else
        {
          error (file, line, clmn)
            << "non-integer array index " <<
            gcc_tree_code_name(TREE_CODE (max)) << endl;

          throw failed ();
        }
      }

      // In GCC tree a const array has both the array type itself and the
      // element type marked as const. This doesn't bode well with our
      // semantic graph model where we have a separate type node for
      // qualifiers. To fix this, we are going to strip the const
      // qualification from the element type and only preserve it in
      // the array type. In other words, we view it as "constant array"
      // rather than "array of constant elements".
      //
      using semantics::array; // vs std::array.

      tree bt (TREE_TYPE (t));
      tree bt_mv (TYPE_MAIN_VARIANT (bt));
      type& bt_node (emit_type (bt_mv, access::public_, file, line, clmn));
      t = TYPE_MAIN_VARIANT (t);
      array& a (unit_->new_node<array> (file, line, clmn, t, size));
      unit_->insert (t, a);
      contains& edge (unit_->new_edge<contains> (a, bt_node));

      // See if there is a name hint for the base type.
      //
      if (names* hint = unit_->find_hint (
            cp_type_quals (bt) == TYPE_UNQUALIFIED ? bt : bt_mv))
        edge.hint (*hint);

      process_named_pragmas (t, a);
      r = &a;
      break;
    }
  case REFERENCE_TYPE:
    {
      tree bt (TREE_TYPE (t));
      type& bt_node (emit_type (bt, access::public_, file, line, clmn));
      t = TYPE_MAIN_VARIANT (t);
      reference& ref (unit_->new_node<reference> (file, line, clmn, t));
      unit_->insert (t, ref);
      references& edge (unit_->new_edge<references> (ref, bt_node));

      // See if there is a name hint for the base type.
      //
      if (names* hint = unit_->find_hint (bt))
        edge.hint (*hint);

      process_named_pragmas (t, ref);
      r = &ref;
      break;
    }
  case POINTER_TYPE:
    {
      if (!TYPE_PTRMEM_P (t))
      {
        tree bt (TREE_TYPE (t));
        type& bt_node (emit_type (bt, access::public_, file, line, clmn));
        t = TYPE_MAIN_VARIANT (t);
        pointer& p (unit_->new_node<pointer> (file, line, clmn, t));
        unit_->insert (t, p);
        points& edge (unit_->new_edge<points> (p, bt_node));

        // See if there is a name hint for the base type.
        //
        if (names* hint = unit_->find_hint (bt))
          edge.hint (*hint);

        process_named_pragmas (t, p);
        r = &p;
      }
      else
      {
        t = TYPE_MAIN_VARIANT (t);
        r = &unit_->new_node<unsupported_type> (
          file, line, clmn, t, "pointer_to_data_member_type");
        unit_->insert (t, *r);

        if (trace)
          ts << "unsupported pointer_to_data_member_type (" << r << ")"
             << " at " << file << ":" << line << endl;
      }
      break;
    }
  default:
    {
      t = TYPE_MAIN_VARIANT (t);
      r = &unit_->new_node<unsupported_type> (
        file, line, clmn, t, gcc_tree_code_name(tc));
      unit_->insert (t, *r);

      if (trace)
        ts << "unsupported " << gcc_tree_code_name(tc) << " (" << r << ")"
           << " at " << file << ":" << line << endl;

      break;
    }
  }

  return *r;
}

string parser::impl::
emit_type_name (tree type, bool direct)
{
  // First see if there is a "direct" name for this type.
  //
  if (direct)
  {
    if (tree decl = TYPE_NAME (type))
    {
      tree t (TREE_TYPE (decl));

      if (t != 0 && same_type_p (type, t))
        return IDENTIFIER_POINTER (DECL_NAME (decl));
    }
  }

  string r;

  if (CP_TYPE_CONST_P (type))
    r += " const";

  if (CP_TYPE_VOLATILE_P (type))
    r += " volatile";

  if (CP_TYPE_RESTRICT_P (type))
    r += " __restrict";

  gcc_tree_code_type tc (TREE_CODE (type));

  switch (tc)
  {
    //
    // User-defined types.
    //

  case RECORD_TYPE:
  case UNION_TYPE:
    {
      tree ti (TYPE_TEMPLATE_INFO (type));

      if (ti == NULL_TREE)
      {
        // Ordinary class.
        //
        type = TYPE_MAIN_VARIANT (type);

        // Pointers to member functions are represented as record
        // types and don't have names, not even the synthesized ones.
        //
        if (TYPE_PTRMEMFUNC_P (type))
          r = "<pointer-to-member-function>" + r;
        else
        {
          tree name (TYPE_NAME (type));
          r = IDENTIFIER_POINTER (DECL_NAME (name)) + r;
        }
      }
      else
      {
        // Template instantiation.
        //
        tree decl (TI_TEMPLATE (ti)); // DECL_TEMPLATE
        string id (IDENTIFIER_POINTER (DECL_NAME (decl)));

        id += '<';

        tree args (INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti)));

        for (size_t i (0), n (TREE_VEC_LENGTH (args)); i < n ; ++i)
        {
          tree a (TREE_VEC_ELT (args, i));

          if (i != 0)
            id += ", ";

          // Assume type-only arguments.
          //
          id += emit_type_name (a);
        }

        id += '>';

        r = id + r;
      }

      break;
    }

  case ENUMERAL_TYPE:
    {
      type = TYPE_MAIN_VARIANT (type);
      tree decl (TYPE_NAME (type));
      r = IDENTIFIER_POINTER (DECL_NAME (decl)) + r;
      break;
    }

    //
    // Derived types.
    //

  case ARRAY_TYPE:
    {
      unsigned long long size (0);

      if (tree index = TYPE_DOMAIN (type))
      {
        tree max (TYPE_MAX_VALUE (index));

        if (TREE_CODE (max) == INTEGER_CST)
        {
          size = integer_value (max);

          // Same as above.
          //
          if (size == ~(unsigned long long) (0))
            size = 0;

          size++;
        }
        else
        {
          // Non-integer array index which we do not support. The
          // error has been/will be issued in emit_type.
          //
        }
      }

      tree t (TREE_TYPE (type));

      if (size != 0)
      {
        ostringstream ostr;
        ostr << size;
        r = emit_type_name (t) + "[" + ostr.str () + "]" + r;
      }
      else
        r = emit_type_name (t) + "[]" + r;

      break;
    }
  case REFERENCE_TYPE:
    {
      tree t (TREE_TYPE (type));
      r = emit_type_name (t) + "&" + r;
      break;
    }
  case POINTER_TYPE:
    {
      if (!TYPE_PTRMEM_P (type))
      {
        tree t (TREE_TYPE (type));
        r = emit_type_name (t) + "*" + r;
      }
      else
        r = "<pointer_to_member_type>";

      break;
    }

    //
    // Fundamental types.
    //

  case VOID_TYPE:
  case REAL_TYPE:
  case BOOLEAN_TYPE:
  case INTEGER_TYPE:
    {
      type = TYPE_MAIN_VARIANT (type);
      tree decl (TYPE_NAME (type));
      r = IDENTIFIER_POINTER (DECL_NAME (decl)) + r;
      break;
    }
  default:
    {
      r = "<" + string (gcc_tree_code_name(tc)) + ">";
      break;
    }
  }

  return r;
}

void parser::impl::
process_pragmas (declaration const& decl,
                 node& node,
                 string const& name,
                 decl_set::const_iterator begin,
                 decl_set::const_iterator cur,
                 decl_set::const_iterator /*end*/)
{
  // First process the position pragmas by iterating backwards until
  // we get to the preceding non-pragma declaration that has been
  // associated.
  //
  pragma_set prags;

  if (cur != begin)
  {
    decl_set::const_iterator i (cur);
    for (--i; i != begin && (i->prag != 0 || !i->assoc); --i) ;

    for (; i != cur; ++i)
    {
      if (i->prag == 0) // Skip declarations.
        continue;

      assert (!i->assoc);

      if (i->prag->check (decl, name, i->prag->pragma_name, i->prag->loc))
        prags.insert (*i->prag);
      else
        error_++; // Diagnostic has already been issued.

      i->assoc = true; // Mark this pragma as associated.
    }

    cur->assoc = true; // Mark the declaration as associated.
  }

  // Now see if there are any named pragmas for this declaration. By
  // doing this after handling the position pragmas we ensure correct
  // overriding.
  //
  {
    decl_pragmas::const_iterator i (decl_pragmas_.find (decl));

    if (i != decl_pragmas_.end ())
      prags.insert (i->second.begin (), i->second.end ());
  }

  // Finally, copy the resulting pragma set to context.
  //
  for (pragma_set::iterator i (prags.begin ()); i != prags.end (); ++i)
    add_pragma (node, *i);
}

void parser::impl::
process_named_pragmas (declaration const& decl, node& node)
{
  pragma_set prags;

  decl_pragmas::const_iterator i (decl_pragmas_.find (decl));

  if (i != decl_pragmas_.end ())
    prags.insert (i->second.begin (), i->second.end ());

  // Copy the resulting pragma set to context.
  //
  for (pragma_set::iterator i (prags.begin ()); i != prags.end (); ++i)
    add_pragma (node, *i);
}

void parser::impl::
add_pragma (node& n, pragma const& p)
{
  if (trace)
    ts << "\t\t pragma " << p.pragma_name << endl;

  string const& k (p.context_name);

  if (p.add == 0)
  {
    n.set (k, p.value);
    n.set (k + "-location", p.loc);
  }
  else
    p.add (n, k, p.value, p.loc);
}

void parser::impl::
diagnose_unassoc_pragmas (decl_set const& decls)
{
  for (decl_set::const_iterator i (decls.begin ()), e (decls.end ());
       i != e; ++i)
  {
    if (i->prag && !i->assoc)
    {
      pragma const& p (*i->prag);
      error (p.loc)
        << "db pragma '" << p.pragma_name << "' is not associated with a "
        << "declaration" << endl;
      error_++;
    }
  }
}

string parser::impl::
fq_scope (tree decl)
{
  string s, tmp;

  for (tree scope (CP_DECL_CONTEXT (decl));
       scope != global_namespace;)
  {
    tree prev (CP_DECL_CONTEXT (scope));

    // If this is an inline namespace, pretend it doesn't exist.
    //
    if (!is_associated_namespace (prev, scope))
    {
      tree n = DECL_NAME (scope);

      tmp = "::";
      tmp += (n != NULL_TREE ? IDENTIFIER_POINTER (n) : "");
      tmp += s;
      s.swap (tmp);
    }

    scope = prev;
  }

  return s;
}

//
// parser
//

parser::
parser (options const& ops,
        loc_pragmas& lp,
        ns_loc_pragmas& nslp,
        decl_pragmas& dp)
    : impl_ (new impl (ops, lp, nslp, dp))
{
}

auto_ptr<unit> parser::
parse (tree global_scope, path const& main_file)
{
  return impl_->parse (global_scope, main_file);
}