aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/demangle.h
blob: 9b1ff0fdb663788300b722328e4282ef1d3f3d2f (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
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
// C++ IA64 / g++ v3 demangler  -*- C++ -*-

// Copyright (C) 2003 Free Software Foundation, Inc.
// Written by Carlo Wood <carlo@alinoe.com>
//
// This file is part of the GNU ISO C++ Library.  This library 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.

// This library 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 this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

#ifndef _DEMANGLER_H
#define _DEMANGLER_H 1

#include <vector>
#include <string>

#ifndef _GLIBCXX_DEMANGLER_DEBUG
#define _GLIBCXX_DEMANGLER_CWDEBUG 0
#define _GLIBCXX_DEMANGLER_DEBUG(x)
#define _GLIBCXX_DEMANGLER_DOUT(cntrl, data)
#define _GLIBCXX_DEMANGLER_DOUT_ENTERING(x)
#define _GLIBCXX_DEMANGLER_DOUT_ENTERING2(x)
#define _GLIBCXX_DEMANGLER_RETURN \
    return M_result
#define _GLIBCXX_DEMANGLER_RETURN2 \
    return M_result
#define _GLIBCXX_DEMANGLER_FAILURE \
    do { M_result = false; return false; } while(0)
#else
#define _GLIBCXX_DEMANGLER_CWDEBUG 1
#endif

// The following defines change the behaviour of the demangler.  The
// default behaviour is that none of these macros is defined.

// _GLIBCXX_DEMANGLER_STYLE_VOID
// Default behaviour:					int f()
// Uses (void) instead of ():				int f(void)

// _GLIBCXX_DEMANGLER_STYLE_LITERAL
// Default behaviour:					(long)13, 
//							(unsigned long long)19
// Use extensions 'u', 'l' and 'll' for integral
// literals (as in template arguments):			13l, 19ull

// _GLIBCXX_DEMANGLER_STYLE_LITERAL_INT
// Default behaviour:					4
// Use also an explicit cast for int in literals:	(int)4

namespace __gnu_cxx
{
  namespace demangler
  {

    enum substitution_nt
    {
      type,
      template_template_param,
      nested_name_prefix,
      nested_name_template_prefix,
      unscoped_template_name,
    };

    struct substitution_st
    {
      int M_start_pos;
      substitution_nt M_type;
      int M_number_of_prefixes;
      
      substitution_st(int start_pos,
		      substitution_nt type,
		      int number_of_prefixes)
      : M_start_pos(start_pos), M_type(type),
	M_number_of_prefixes(number_of_prefixes)
      { }
    };

    enum simple_qualifier_nt
    {
      complex_or_imaginary = 'G',
      pointer = 'P',
      reference = 'R'
    };

    enum cv_qualifier_nt
    {
      cv_qualifier = 'K'
    };

    enum param_qualifier_nt
    {
      vendor_extension = 'U',
      array = 'A',
      pointer_to_member = 'M'
    };

    template<typename Allocator>
      class qualifier;

    template<typename Allocator>
      class qualifier_list;

    template<typename Allocator>
      class session;

    template<typename Allocator>
      class qualifier
      {
	typedef std::basic_string<char, std::char_traits<char>, Allocator>
	    string_type;

      private:
	char M_qualifier1;
	char M_qualifier2;
	char M_qualifier3;
	mutable unsigned char M_cnt;
	string_type M_optional_type;
	int M_start_pos;
	bool M_part_of_substitution;

      public:
	qualifier(int start_pos,
	          simple_qualifier_nt simple_qualifier,
		  int inside_substitution)
	: M_qualifier1(simple_qualifier),
	  M_start_pos(start_pos),
	  M_part_of_substitution(inside_substitution)
	{ }

	qualifier(int start_pos,
	          cv_qualifier_nt,
		  char const* start,
		  int count,
		  int inside_substitution)
	: M_qualifier1(start[0]),
	  M_qualifier2((count > 1) ? start[1] : '\0'),
	  M_qualifier3((count > 2) ? start[2] : '\0'),
	  M_start_pos(start_pos),
	  M_part_of_substitution(inside_substitution)
	{ }

	qualifier(int start_pos,
	          param_qualifier_nt param_qualifier,
		  string_type optional_type,
		  int inside_substitution)
	: M_qualifier1(param_qualifier),
	  M_optional_type(optional_type),
	  M_start_pos(start_pos),
	  M_part_of_substitution(inside_substitution)
	{ }

	int
	get_start_pos(void) const
	{ return M_start_pos; }

	char
	first_qualifier(void) const
	{ M_cnt = 1; return M_qualifier1; }

	char
	next_qualifier(void) const
	{
	  return (++M_cnt == 2) ? M_qualifier2
	                        : ((M_cnt == 3) ? M_qualifier3 : 0);
	}

	string_type const&
	get_optional_type(void) const
	{ return M_optional_type; }

	bool
	part_of_substitution(void) const
	{ return M_part_of_substitution; }

      };

    template<typename Allocator>
      class qualifier_list
      {
	typedef std::basic_string<char, std::char_traits<char>, Allocator>
	  string_type;

      private:
	bool M_printing_suppressed;
	std::vector<qualifier<Allocator>, Allocator> M_qualifier_starts;
	session<Allocator>& M_demangler;

      public:
	qualifier_list(session<Allocator>& demangler_obj)
	: M_printing_suppressed(false), M_demangler(demangler_obj)
	{ }

	void
	add_qualifier_start(simple_qualifier_nt simple_qualifier,
			    int start_pos,
			    int inside_substitution)
	{ M_qualifier_starts.
	      push_back(qualifier<Allocator>(start_pos,
		  simple_qualifier, inside_substitution)); }

	void
	add_qualifier_start(cv_qualifier_nt cv_qualifier,
			    int start_pos,
			    int count,
			    int inside_substitution)
	{ M_qualifier_starts.
	      push_back(qualifier<Allocator>(start_pos,
		    cv_qualifier, &M_demangler.M_str[start_pos],
		    count, inside_substitution)); }

	void
	add_qualifier_start(param_qualifier_nt param_qualifier,
	    		    int start_pos,
			    string_type optional_type,
			    int inside_substitution)
	{ M_qualifier_starts.
	      push_back(qualifier<Allocator>(start_pos,
		    param_qualifier, optional_type, inside_substitution)); }

	void
	decode_qualifiers(string_type& prefix,
	    		  string_type& postfix,
			  bool member_function_pointer_qualifiers);

	bool
	suppressed(void) const
	{ return M_printing_suppressed; }

	void
	printing_suppressed(void)
	{ M_printing_suppressed = true; }

	size_t
	size(void) const
	{ return M_qualifier_starts.size(); }

      };

    template<typename Allocator>
      class session
      {
	friend class qualifier_list<Allocator>;
	typedef std::basic_string<char, std::char_traits<char>, Allocator>
	    string_type;

      private:
	char const* M_str;
	int M_pos;
	int M_maxpos;
	bool M_result;
	int M_inside_template_args;
	int M_inside_type;
	int M_inside_substitution;
	bool M_saw_destructor;
	bool M_name_is_cdtor;
	bool M_name_is_template;
	bool M_name_is_conversion_operator;
	bool M_template_args_need_space;
	string_type M_function_name;
	std::vector<int, Allocator> M_template_arg_pos;
	int M_template_arg_pos_offset;
	std::vector<substitution_st, Allocator> M_substitutions_pos;
#if _GLIBCXX_DEMANGLER_CWDEBUG
	bool M_inside_add_substitution;
#endif

      public:
	explicit session(char const* in, int len)
	: M_str(in), M_pos(0), M_maxpos(len - 1), M_result(true),
	  M_inside_template_args(0), M_inside_type(0),
	  M_inside_substitution(0), M_saw_destructor(false),
	  M_name_is_cdtor(false), M_name_is_template(false),
	  M_name_is_conversion_operator(false),
	  M_template_args_need_space(false), M_template_arg_pos_offset(0)
#if _GLIBCXX_DEMANGLER_CWDEBUG
	  , M_inside_add_substitution(false)
#endif
	{ }

	static int
	decode_encoding(string_type& output, char const* input, int len);

	bool
	decode_type_with_postfix(string_type& prefix,
		                 string_type& postfix,
	            qualifier_list<Allocator>* qualifiers = NULL);

	bool
	decode_type(string_type& output,
	            qualifier_list<Allocator>* qualifiers = NULL)
	{
	  string_type postfix;
	  bool res = decode_type_with_postfix(output, postfix, qualifiers);
	  output += postfix;
	  return res;
	}

	bool
	remaining_input_characters(void) const
	{ return current() != 0; }

      private:
	char
	current(void) const
	{ return (M_pos > M_maxpos) ? 0 : M_str[M_pos]; }

	char
	next(void)
	{ return (M_pos >= M_maxpos) ? 0 : M_str[++M_pos]; }

	char
	eat_current(void)
	{ return (M_pos > M_maxpos) ? 0 : M_str[M_pos++]; }

	void
	store(int& saved_pos)
	{ saved_pos = M_pos; }

	void
	restore(int saved_pos)
	{ M_pos = saved_pos; M_result = true; }

	void
	add_substitution(int start_pos,
	                 substitution_nt sub_type,
			 int number_of_prefixes);

	bool decode_bare_function_type(string_type& output);
	bool decode_builtin_type(string_type& output);
	bool decode_call_offset(string_type& output);
	bool decode_class_enum_type(string_type& output);
	bool decode_expression(string_type& output);
	bool decode_literal(string_type& output);
	bool decode_local_name(string_type& output);
	bool decode_name(string_type& output,
	    string_type& nested_name_qualifiers);
	bool decode_nested_name(string_type& output,
	    string_type& qualifiers);
	bool decode_number(string_type& output);
	bool decode_operator_name(string_type& output);
	bool decode_source_name(string_type& output);
	bool decode_substitution(string_type& output,
	    qualifier_list<Allocator>* qualifiers = NULL);
	bool decode_template_args(string_type& output);
	bool decode_template_param(string_type& output,
	    qualifier_list<Allocator>* qualifiers = NULL);
	bool decode_unqualified_name(string_type& output);
	bool decode_unscoped_name(string_type& output);
	bool decode_decimal_integer(string_type& output);
	bool decode_special_name(string_type& output);
      };

    template<typename Allocator>
#if !_GLIBCXX_DEMANGLER_CWDEBUG
      inline
#endif
      void
      session<Allocator>::add_substitution(int start_pos,
					   substitution_nt sub_type,
					   int number_of_prefixes = 0)
      {
	if (!M_inside_substitution)
	{
#if _GLIBCXX_DEMANGLER_CWDEBUG
	  if (M_inside_add_substitution)
	    return;
#endif
	  M_substitutions_pos.
	      push_back(substitution_st(start_pos,
		  sub_type, number_of_prefixes));
#if _GLIBCXX_DEMANGLER_CWDEBUG
	  if (!DEBUGCHANNELS::dc::demangler.is_on())
	    return;
	  string_type substitution_name("S");
	  int n = M_substitutions_pos.size() - 1;
	  if (n > 0)
	    substitution_name += (n <= 10) ? (char)(n + '0' - 1)
	      				   : (char)(n + 'A' - 11);
	  substitution_name += '_';
	  string_type subst;
	  int saved_pos = M_pos;
	  M_pos = start_pos;
	  M_inside_add_substitution = true;
	  _GLIBCXX_DEMANGLER_DEBUG( dc::demangler.off() );
	  switch(sub_type)
	  {
	    case type:
	      decode_type(subst);
	      break;
	    case template_template_param:
	      decode_template_param(subst);
	      break;
	    case nested_name_prefix:
	    case nested_name_template_prefix:
	      for (int cnt = number_of_prefixes; cnt > 0; --cnt)
	      {
		if (current() == 'I')
		{
		  subst += ' ';
		  decode_template_args(subst);
		}
		else
		{
		  if (cnt < number_of_prefixes)
		    subst += "::";
		  if (current() == 'S')
		    decode_substitution(subst);
		  else
		    decode_unqualified_name(subst);
		}
	      }
	      break;
	    case unscoped_template_name:
	      decode_unscoped_name(subst);
	      break;
	  }
	  M_pos = saved_pos;
	  _GLIBCXX_DEMANGLER_DEBUG( dc::demangler.on() );
	  _GLIBCXX_DEMANGLER_DOUT(dc::demangler,
	      "Adding substitution " << substitution_name
	      << " : " << subst
	      << " (from " << location_ct((char*)__builtin_return_address(0)
		                          + builtin_return_address_offset)
	      << " <- " << location_ct((char*)__builtin_return_address(1)
		                       + builtin_return_address_offset)
	      << " <- " << location_ct((char*)__builtin_return_address(2)
		                       + builtin_return_address_offset)
	      << ").");
	  M_inside_add_substitution = false;
#endif
	}
      }

    // We don't want to depend on locale (or include <cctype> for that matter).
    // We also don't want to use "safe-ctype.h" because that headerfile is not
    // available to the users.
    inline bool isdigit(char c) { return c >= '0' && c <= '9'; }
    inline bool islower(char c) { return c >= 'a' && c <= 'z'; }
    inline bool isupper(char c) { return c >= 'A' && c <= 'Z'; }
    inline char tolower(char c) { return isupper(c) ? c - 'A' + 'a' : c; }

    //
    // <decimal-integer> ::= 0
    //                   ::= 1|2|3|4|5|6|7|8|9 [<digit>+]
    // <digit>           ::= 0|1|2|3|4|5|6|7|8|9
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_decimal_integer(string_type& output)
      {
	char c = current();
	if (c == '0')
	{
	  output += '0';
	  eat_current();
	}
	else if (!isdigit(c))
	  M_result = false;
	else
	{
	  do
	  {
	    output += c;
	  }
	  while (isdigit((c = next())));
	}
	return M_result;
      }

    // <number> ::= [n] <decimal-integer>
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_number(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_number");
	if (current() != 'n')
	  decode_decimal_integer(output);
	else
	{
	  output += '-';
	  eat_current();
	  decode_decimal_integer(output);
	}
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <builtin-type> ::= v  # void
    //                ::= w  # wchar_t
    //                ::= b  # bool
    //                ::= c  # char
    //                ::= a  # signed char
    //                ::= h  # unsigned char
    //                ::= s  # short
    //                ::= t  # unsigned short
    //                ::= i  # int
    //                ::= j  # unsigned int
    //                ::= l  # long
    //                ::= m  # unsigned long
    //                ::= x  # long long, __int64
    //                ::= y  # unsigned long long, __int64
    //                ::= n  # __int128
    //                ::= o  # unsigned __int128
    //                ::= f  # float
    //                ::= d  # double
    //                ::= e  # long double, __float80
    //                ::= g  # __float128
    //                ::= z  # ellipsis
    //                ::= u <source-name>    # vendor extended type
    //
    char const* const builtin_type_c[26] =
    {
      "signed char",	// a
      "bool",		// b
      "char",		// c
      "double",		// d
      "long double",	// e
      "float",		// f
      "__float128",		// g
      "unsigned char",	// h
      "int",		// i
      "unsigned int",	// j
      NULL,			// k
      "long",		// l
      "unsigned long",	// m
      "__int128",		// n
      "unsigned __int128",	// o
      NULL,			// p
      NULL,			// q
      NULL,			// r
      "short",		// s
      "unsigned short",	// t
      NULL,			// u
      "void",		// v
      "wchar_t",		// w
      "long long",		// x
      "unsigned long long",	// y
      "..."			// z
    };

    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_builtin_type(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_builtin_type");
	char const* bt;
	if (!islower(current()) || !(bt = builtin_type_c[current() - 'a']))
	  _GLIBCXX_DEMANGLER_FAILURE;
	output += bt;
	eat_current();
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <class-enum-type> ::= <name>
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_class_enum_type(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_class_enum_type");
	string_type nested_name_qualifiers;
	if (!decode_name(output, nested_name_qualifiers))
	  _GLIBCXX_DEMANGLER_FAILURE;
	output += nested_name_qualifiers;
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <substitution> ::=
    //   S <seq-id> _
    //   S_
    //   St # ::std::
    //   Sa # ::std::allocator
    //   Sb # ::std::basic_string
    //   Ss # ::std::basic_string<char, std::char_traits<char>,
    //                            std::allocator<char> >
    //   Si # ::std::basic_istream<char,  std::char_traits<char> >
    //   So # ::std::basic_ostream<char,  std::char_traits<char> >
    //   Sd # ::std::basic_iostream<char, std::char_traits<char> >
    //
    // <seq-id> ::=
    //   0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
    //       [<seq-id>]	# Base 36 number
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_substitution(string_type& output,
	  qualifier_list<Allocator>* qualifiers)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_substitution");
	unsigned int value = 0;
	char c = next();
	if (c != '_')
	{
	  switch(c)
	  {
	    case 'a':
	    {
	      output += "std::allocator";
	      if (!M_inside_template_args)
	      {
		M_function_name = "allocator";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    }
	    case 'b':
	    {
	      output += "std::basic_string";
	      if (!M_inside_template_args)
	      {
		M_function_name = "basic_string";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    }
	    case 'd':
	      output += "std::iostream";
	      if (!M_inside_template_args)
	      {
		M_function_name = "iostream";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    case 'i':
	      output += "std::istream";
	      if (!M_inside_template_args)
	      {
		M_function_name = "istream";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    case 'o':
	      output += "std::ostream";
	      if (!M_inside_template_args)
	      {
		M_function_name = "ostream";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    case 's':
	      output += "std::string";
	      if (!M_inside_template_args)
	      {
		M_function_name = "string";
		M_name_is_template = true;
		M_name_is_cdtor = false;
		M_name_is_conversion_operator = false;
	      }
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    case 't':
	      output += "std";
	      eat_current();
	      if (qualifiers)
		qualifiers->printing_suppressed();
	      _GLIBCXX_DEMANGLER_RETURN;
	    default:
	      for(;; c = next())
	      {
		if (isdigit(c))
		  value = value * 36 + c - '0';
		else if (isupper(c))
		  value = value * 36 + c - 'A' + 10;
		else if (c == '_')
		  break;
		else
		  _GLIBCXX_DEMANGLER_FAILURE;
	      }
	      ++value;
	      break;
	  }
	}
	eat_current();
	if (value >= M_substitutions_pos.size() ||
	    M_inside_type > 20)			// Rather than core dump.
	  _GLIBCXX_DEMANGLER_FAILURE;
	++M_inside_substitution;
	int saved_pos = M_pos;
	substitution_st& substitution(M_substitutions_pos[value]);
	M_pos = substitution.M_start_pos;
	switch(substitution.M_type)
	{
	  case type:
	    decode_type(output, qualifiers);
	    break;
	  case template_template_param:
	    decode_template_param(output, qualifiers);
	    break;
	  case nested_name_prefix:
	  case nested_name_template_prefix:
	    for (int cnt = substitution.M_number_of_prefixes; cnt > 0; --cnt)
	    {
	      if (current() == 'I')
	      {
		if (M_template_args_need_space)
		  output += ' ';
		M_template_args_need_space = false;
		if (!decode_template_args(output))
		  _GLIBCXX_DEMANGLER_FAILURE;
	      }
	      else
	      {
		if (cnt < substitution.M_number_of_prefixes)
		  output += "::";
		if (current() == 'S')
		{
		  if (!decode_substitution(output))
		    _GLIBCXX_DEMANGLER_FAILURE;
		}
		else if (!decode_unqualified_name(output))
		  _GLIBCXX_DEMANGLER_FAILURE;
	      }
	    }
	    if (qualifiers)
	      qualifiers->printing_suppressed();
	    break;
	  case unscoped_template_name:
	    decode_unscoped_name(output);
	    if (qualifiers)
	      qualifiers->printing_suppressed();
	    break;
	}
	M_pos = saved_pos;
	--M_inside_substitution;
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <template-param> ::= T_			# first template parameter
    //                  ::= T <parameter-2 non-negative number> _
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_template_param(string_type& output,
	  qualifier_list<Allocator>* qualifiers)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_template_parameter");
	if (current() != 'T')
	  _GLIBCXX_DEMANGLER_FAILURE;
	unsigned int value = 0;
	char c;
	if ((c = next()) != '_')
	{
	  while(isdigit(c))
	  {
	    value = value * 10 + c - '0';
	    c = next();
	  }
	  ++value;
	}
	if (eat_current() != '_')
	  _GLIBCXX_DEMANGLER_FAILURE;
	value += M_template_arg_pos_offset;
	if (value >= M_template_arg_pos.size())
	  _GLIBCXX_DEMANGLER_FAILURE;
	int saved_pos = M_pos;
	M_pos = M_template_arg_pos[value];
	if (M_inside_type > 20)		// Rather than core dump.
	  _GLIBCXX_DEMANGLER_FAILURE;
	++M_inside_substitution;
	if (current() == 'X')
	{
	  eat_current();
	  decode_expression(output);
	}
	else if (current() == 'L')
	  decode_literal(output);
	else
	  decode_type(output, qualifiers);
	--M_inside_substitution;
	M_pos = saved_pos;
	_GLIBCXX_DEMANGLER_RETURN;
      }

    template<typename Allocator>
      bool
      session<Allocator>::decode_literal(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_literal");
	eat_current();	// Eat the 'L'.
	if (current() == '_')
	{
	  if (next() != 'Z')
	    _GLIBCXX_DEMANGLER_FAILURE;
	  eat_current();
	  if ((M_pos += decode_encoding(output, M_str + M_pos,
		  M_maxpos - M_pos + 1)) < 0)
	    _GLIBCXX_DEMANGLER_FAILURE;
	}
	else
	{
	  // Special cases
	  if (current() == 'b')
	  {
	    if (next() == '0')
	      output += "false";
	    else
	      output += "true";
	    eat_current();
	    _GLIBCXX_DEMANGLER_RETURN;
	  }
	  char c = current();
#ifdef _GLIBCXX_DEMANGLER_STYLE_LITERAL
	  if (c == 'i' || c == 'j' || c == 'l' ||
	      c == 'm' || c == 'x' || c == 'y')
	    eat_current();
	  else
#else
#ifndef _GLIBCXX_DEMANGLER_STYLE_LITERAL_INT
	  if (c == 'i')
	    eat_current();
	  else
#endif
#endif
	  {
	    output += '(';
	    if (!decode_type(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    output += ')';
	  }
	  if (!decode_number(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
#ifdef _GLIBCXX_DEMANGLER_STYLE_LITERAL
	  if (c == 'j' || c == 'm' || c == 'y')
	    output += 'u';
	  if (c == 'l' || c == 'm')
	    output += 'l';
	  if (c == 'x' || c == 'y')
	    output += "ll";
#endif
	}
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <operator-name> ::=
    //   nw				# new           
    //   na				# new[]
    //   dl				# delete        
    //   da				# delete[]      
    //   ng				# - (unary)     
    //   ad				# & (unary)     
    //   de				# * (unary)     
    //   co				# ~             
    //   pl				# +             
    //   mi				# -             
    //   ml				# *             
    //   dv				# /             
    //   rm				# %             
    //   an				# &             
    //   or				# |             
    //   eo				# ^             
    //   aS				# =             
    //   pL				# +=            
    //   mI				# -=            
    //   mL				# *=            
    //   dV				# /=            
    //   rM				# %=            
    //   aN				# &=            
    //   oR				# |=            
    //   eO				# ^=            
    //   ls				# <<            
    //   rs				# >>            
    //   lS				# <<=           
    //   rS				# >>=           
    //   eq				# ==            
    //   ne				# !=            
    //   lt				# <             
    //   gt				# >             
    //   le				# <=            
    //   ge				# >=            
    //   nt				# !             
    //   aa				# &&            
    //   oo				# ||            
    //   pp				# ++            
    //   mm				# --            
    //   cm				# ,             
    //   pm				# ->*           
    //   pt				# ->            
    //   cl				# ()            
    //   ix				# []            
    //   qu				# ?             
    //   sz				# sizeof        
    //   sr				# scope resolution (::), see below        
    //   cv <type>			# (cast)        
    //   v <digit> <source-name>	# vendor extended operator
    //
    //
    // Symbol operator codes exist of two characters, we need to find a
    // quick hash so that their names can be looked up in a table.
    //
    // The puzzle :)
    // Shift the rows so that there is at most one character per column.
    //
    // A perfect solution:
    //                                              horizontal
    //    .....................................     offset + 'a'
    // a, ||a||d|||||||||n||||s||||||||||||||||||	    2
    // c, || || ||lm|o||| |||| ||||||||||||||||||	   -3
    // d, || a| |e  | ||l |||| |||v||||||||||||||	    3
    // e, ||  | |   o q|  |||| ||| ||||||||||||||	   -4
    // g, |e  | |      |  t||| ||| ||||||||||||||	   -3
    // i, |   | |      |   ||| ||| ||||||||||x|||    12
    // l, |   | |      e   ||| ||| ||st|||||| |||	    9
    // m, |   | |          ||| ||| |i  lm|||| |||	   18
    // n, a   e g          ||t |w| |     |||| |||	    0
    // o,                  ||  | | |     ||o| r||	   19
    // p,                  lm  p | t     || |  ||	    6
    // q,                        |       || u  ||	   14
    // r,                        |       |m    |s	   20
    // s,                        r       z     | 	    6
    //    .....................................
    // ^            ^__ second character
    // |___ first character
    //

    // Putting that solution in tables:

    char const offset_table_c [1 + CHAR_MAX - CHAR_MIN ] =
    {
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
#if (CHAR_MIN < 0)
      // Add -CHAR_MIN extra zeroes (128):
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      //   a    b    c    d    e    f    g    h    i    j    k
      0, -95,   0,-100, -94,-101,   0,-100,   0, -85,   0,   0,
      //   l    m    n    o    p    q    r    s    t    u    v
	 -88, -79, -97, -78, -91, -83, -77, -91,   0,   0,   0,
#else
      //   a    b    c    d    e    f    g    h    i    j    k
      0, 161,   0, 156, 162, 155,   0, 156,   0, 171,   0,   0,
      //   l    m    n    o    p    q    r    s    t    u    v
	 168, 177, 159, 178, 165, 173, 179, 165,   0,   0,   0,
#endif
      // ... more zeros
    };

    struct entry_st
    {
      char const* opcode;
      char const* symbol_name;
      bool unary;
    };

    entry_st const symbol_name_table_c[39] = {
      { "na",  "operator new[]", true },
      { "ge",  "operator>=", false },
      { "aa",  "operator&&", false },
      { "da",  "operator delete[]", true },
      { "ne",  "operator!=", false },
      { "ad",  "operator&", true },	// unary
      { "ng",  "operator-", true },	// unary
      { "de",  "operator*", true },	// unary
      { "cl",  "operator()", true },
      { "cm",  "operator,", false },
      { "eo=", "operator^", false },
      { "co",  "operator~", false },
      { "eq",  "operator==", false },
      { "le",  "operator<=", false },
      { "dl",  "operator delete", true },
      { "an=", "operator&", false },
      { "gt",  "operator>", false },
      { "pl=", "operator+", false },
      { "pm",  "operator->*", false },
      { "nt",  "operator!", true },
      { "as=", "operator", false },
      { "pp",  "operator++", true },
      { "nw",  "operator new", true },
      { "sr",  "::", true },
      { "dv=", "operator/", false },
      { "pt",  "operator->", false },
      { "mi=", "operator-", false },
      { "ls=", "operator<<", false },
      { "lt",  "operator<", false },
      { "ml=", "operator*", false },
      { "mm",  "operator--", true },
      { "sz",  "sizeof", true },
      { "rm=", "operator%", false },
      { "oo",  "operator||", false },
      { "qu",  "operator?", false },
      { "ix",  "operator[]", true },
      { "or=", "operator|", false },
      { "", NULL, false },
      { "rs=", "operator>>", false }
    };

    template<typename Allocator>
      bool
      session<Allocator>::decode_operator_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_operator_name");

	char opcode0 = current();
	char opcode1 = tolower(next());

	register char hash;
	if ((hash = offset_table_c[opcode0 - CHAR_MIN]))
	{
	  hash += opcode1;
	  if (
#if (CHAR_MIN < 0)
	      hash >= 0 &&
#endif
	      hash < 39)
	  {
	    int index = static_cast<int>(static_cast<unsigned char>(hash));
	    entry_st entry = symbol_name_table_c[index];
	    if (entry.opcode[0] == opcode0 && entry.opcode[1] == opcode1
		&& (opcode1 == current() || entry.opcode[2] == '='))
	    {
	      output += entry.symbol_name;
	      if (opcode1 != current())
		output += '=';
	      eat_current();
	      if (hash == 27 || hash == 28)
		M_template_args_need_space = true;
	      _GLIBCXX_DEMANGLER_RETURN;
	    }
	    else if (opcode0 == 'c' && opcode1 == 'v')
	    {
	      eat_current();
	      output += "operator ";
	      if (current() == 'T')
	      {
		// This is a templated cast operator.
		// It must be of the form "cvT_I...E".
		// Let M_template_arg_pos already point
		// to the template argument.
		M_template_arg_pos_offset = M_template_arg_pos.size();
		M_template_arg_pos.push_back(M_pos + 3);
	      }
	      if (!decode_type(output))
		_GLIBCXX_DEMANGLER_FAILURE;
	      if (!M_inside_template_args)
		M_name_is_conversion_operator = true;
	      _GLIBCXX_DEMANGLER_RETURN;
	    }
	  }
	}
	_GLIBCXX_DEMANGLER_FAILURE;
      }

    //
    // <expression> ::= <unary operator-name> <expression>
    //              ::= <binary operator-name> <expression> <expression>
    //              ::= <expr-primary>
    //
    // <expr-primary> ::= <template-param>		# Starts with a T
    //                ::= L <type> <value number> E	# literal
    //                ::= L <mangled-name> E		# external name
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_expression(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_expression");
	if (current() == 'T')
	{
	  if (!decode_template_param(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	else if (current() == 'L')
	{
	  if (!decode_literal(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  if (current() != 'E')
	    _GLIBCXX_DEMANGLER_FAILURE;
	  eat_current();
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	else
	{
	  char opcode0 = current();
	  char opcode1 = tolower(next());

	  register char hash;
	  if ((hash = offset_table_c[opcode0 - CHAR_MIN]))
	  {
	    hash += opcode1;
	    if (
#if (CHAR_MIN < 0)
		hash >= 0 &&
#endif
		hash < 39)
	    {
	      int index = static_cast<int>(static_cast<unsigned char>(hash));
	      entry_st entry = symbol_name_table_c[index];
	      if (entry.opcode[0] == opcode0 && entry.opcode[1] == opcode1
		  && (opcode1 == current() || entry.opcode[2] == '='))
	      {
		char const* p = entry.symbol_name;
		if (!strncmp("operator", p, 8))
		  p += 8;
		if (*p == ' ')
		  ++p;
		if (entry.unary)
		  output += p;
		bool is_eq = (opcode1 != current());
		eat_current();
		output += '(';
		if (!decode_expression(output))
		  _GLIBCXX_DEMANGLER_FAILURE;
		output += ')';
		if (!entry.unary)
		{
		  output += ' ';
		  output += p;
		  if (is_eq)
		    output += '=';
		  output += ' ';
		  output += '(';
		  if (!decode_expression(output))
		    _GLIBCXX_DEMANGLER_FAILURE;
		  output += ')';
		}
		_GLIBCXX_DEMANGLER_RETURN;
	      }
	    }
	  }
	}
	_GLIBCXX_DEMANGLER_FAILURE;
      }

    //
    // <template-args> ::= I <template-arg>+ E
    // <template-arg> ::= <type>			# type or template
    //                ::= L <type> <value number> E	# literal
    //                ::= L_Z <encoding> E		# external name
    //                ::= X <expression> E		# expression
    template<typename Allocator>
      bool
      session<Allocator>::decode_template_args(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_template_args");
	if (eat_current() != 'I')
	  _GLIBCXX_DEMANGLER_FAILURE;
	int prev_size = M_template_arg_pos.size();
	++M_inside_template_args;
	if (M_template_args_need_space)
	{
	  output += ' ';
	  M_template_args_need_space = false;
	}
	output += '<';
	for(;;)
	{
	  if (M_inside_template_args == 1 && !M_inside_type)
	    M_template_arg_pos.push_back(M_pos);
	  if (current() == 'X')
	  {
	    eat_current();
	    if (!decode_expression(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (current() != 'E')
	      _GLIBCXX_DEMANGLER_FAILURE;
	    eat_current();
	  }
	  else if (current() == 'L')
	  {
	    if (!decode_literal(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (current() != 'E')
	      _GLIBCXX_DEMANGLER_FAILURE;
	    eat_current();
	  }
	  else if (!decode_type(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  if (current() == 'E')
	    break;
	  output += ", ";
	}
	eat_current();
	if (*(output.rbegin()) == '>')
	  output += ' ';
	output += '>';
	--M_inside_template_args;
	if (!M_inside_template_args && !M_inside_type)
	{
	  M_name_is_template = true;
	  M_template_arg_pos_offset = prev_size;
	}
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <bare-function-type> ::=
    //   <signature type>+		# types are parameter types
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_bare_function_type(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_bare_function_type");
	if (M_saw_destructor)
	{
	  if (eat_current() != 'v' || (current() != 'E' && current() != 0))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  output += "()";
	  M_saw_destructor = false;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
#ifndef _GLIBCXX_DEMANGLER_STYLE_VOID
	if (current() == 'v')
	{
	  eat_current();
	  if (current() != 'E' && current() != 0)
	    _GLIBCXX_DEMANGLER_FAILURE;
	  output += "()";
	  M_saw_destructor = false;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
#endif
	output += '(';
	M_template_args_need_space = false;
	if (!decode_type(output))	// Must have at least one parameter.
	  _GLIBCXX_DEMANGLER_FAILURE;
	while (current() != 'E' && current() != 0)
	{
	  output += ", ";
	  if (!decode_type(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	}
	output += ')';
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <type> ::=
    //   <builtin-type>		# Starts with a lower case character != r.
    //   <function-type>	# Starts with F
    //   <class-enum-type>	# Starts with N, S, C, D, Z, a digit or a lower
    //   			# case character.  Since a lower case character
    //   			# would be an operator name, that would be an
    //   			# error.  The S is a substitution or St
    //   			# (::std::).  A 'C' would be a constructor and
    //   			# thus also an error.
    //   <template-param>	# Starts with T
    //   <substitution>         # Starts with S
    //   <template-template-param> <template-args>  # Starts with T or S,
    //   					    # equivalent with the above.
    //
    //   <array-type>			# Starts with A
    //   <pointer-to-member-type>	# Starts with M
    //   <CV-qualifiers> <type>		# Starts with r, V or K
    //   P <type>   # pointer-to	# Starts with P
    //   R <type>   # reference-to	# Starts with R
    //   C <type>   # complex (C 2000)	# Starts with C
    //   G <type>   # imaginary (C 2000)# Starts with G
    //   U <source-name> <type>     	# vendor extended type qualifier,
    //   				# starts with U
    //
    // <template-template-param> ::= <template-param>
    //                           ::= <substitution>

    // My own analysis of how to decode qualifiers:
    //
    // F is a <function-type>, <T> is a <builtin-type>, <class-enum-type>,
    //   <template-param> or <template-template-param> <template-args>.
    // <Q> represents a series of qualifiers (not G or C).
    // <C> is an unqualified type.
    // <R> is a qualified type.
    // <B> is the bare-function-type without return type.
    // <I> is the array index.
    //						Substitutions:
    // <Q>M<C><Q2>F<R><B>E  ==> R (C::*Q)B Q2	"<C>", "F<R><B>E"
    // 						    (<R> and <B> recursive),
    // 						    "M<C><Q2>F<R><B>E".
    // <Q>F<R><B>E 	    ==> R (Q)B		"<R>", "<B>" (<B> recursive)
    //                                              and "F<R><B>E".
    //
    // Note that if <R> has postfix qualifiers (an array), then those
    // are added AFTER the (member) function type.  For example:
    // <Q>FPA<R><B>E ==> R (*(Q)B) [], where the PA added the prefix
    // "(*" and the postfix ") []".
    //
    // <Q>G<T>     	    ==> imaginary T Q	"<T>", "G<T>" (<T> recursive).
    // <Q>C<T>     	    ==> complex T Q	"<T>", "C<T>" (<T> recursive).
    // <Q><T>      	    ==> T Q		"<T>" (<T> recursive).
    //
    // where <Q> is any of:
    //
    // <Q>P   		==> *Q				"P..."
    // <Q>R   		==> &Q				"R..."
    // <Q>[K|V|r]+	==> [ const| volatile| restrict]+Q	"KVr..."
    // <Q>U<S>		==>  SQ				"U<S>..."
    // <Q>M<C>		==> C::*Q			"M<C>..." (<C> recurs.)
    // A<I>		==> [I]				"A<I>..." (<I> recurs.)
    // <Q>A<I>		==>  (Q) [I]			"A<I>..." (<I> recurs.)
    //   Note that when <Q> ends on an A<I2> then the brackets are omitted:
    //   A<I2>A<I>	  ==> [I2][I]
    //  
    // A <substitution> is handled with an input position switch during which
    // new substitutions are turned off.  Because recursive handling of types
    // (and therefore the order in which substitutions must be generated) must
    // be done left to right, but the generation of Q needs processing right to
    // left, substitutions per <type> are generated by reading the input left
    // to right and marking the starts of all substitutions only - implicitly
    // finishing them at the end of the type.  Then the output and real
    // substitutions are generated.
    //
    // The following comment was for the demangling of g++ version 3.0.x.  The
    // mangling (and I believe even the ABI description) have been fixed now
    // (as of g++ version 3.1).	
    //
    // g++ 3.0.x only:
    // The ABI specifies for pointer-to-member function types the format
    // <Q>M<T>F<R><B>E.  In other words, the qualifier <Q2> (see above) is
    // implicitely contained in <T> instead of explicitly part of the M format.
    // I am convinced that this is a bug in the ABI.  Unfortunately, this is
    // how we have to demangle things as it has a direct impact on the order
    // in which substitutions are stored.  This ill-formed design results in
    // rather ill-formed demangler code too however :/
    //
    // <Q2> is now explicitely part of the M format.
    // For some weird reason, g++ (3.2.1) does not add substitutions for
    // qualified member function pointers.  I think that is another bug.
    //
    template<typename Allocator>
      void
      qualifier_list<Allocator>::decode_qualifiers(
	  string_type& prefix,
	  string_type& postfix,
	  bool member_function_pointer_qualifiers = false)
      {
	for(typename std::vector<qualifier<Allocator>, Allocator>::
	    reverse_iterator iter = M_qualifier_starts.rbegin();
	    iter != M_qualifier_starts.rend();)
	{
	  if (!member_function_pointer_qualifiers
	      && !(*iter).part_of_substitution())
	  {
	    int saved_inside_substitution = M_demangler.M_inside_substitution;
	    M_demangler.M_inside_substitution = 0;
	    M_demangler.add_substitution((*iter).get_start_pos(), type);
	    M_demangler.M_inside_substitution = saved_inside_substitution;
	  }
	  char qualifier_char = (*iter).first_qualifier();
	  for(; qualifier_char; qualifier_char = (*iter).next_qualifier())
	  {
	    switch(qualifier_char)
	    {
	      case 'P':
		prefix += "*";
		break;
	      case 'R':
		prefix += "&";
		break;
	      case 'K':
		prefix += " const";
		continue;
	      case 'V':
		prefix += " volatile";
		continue;
	      case 'r':
		prefix += " restrict";
		continue;
	      case 'A':
	      {
		string_type index = (*iter).get_optional_type();
		if (++iter != M_qualifier_starts.rend()
		    && (*iter).first_qualifier() != 'A')
		{
		  prefix += " (";
		  postfix = ") [" + index + "]" + postfix;
		}
		else
		  postfix = "[" + index + "]" + postfix;
		break;
	      }
	      case 'M':
		prefix += " ";
		prefix += (*iter).get_optional_type();
		prefix += "::*";
		break;
	      case 'U':
		prefix += " ";
		prefix += (*iter).get_optional_type();
		break;
	      case 'G':	// Only here so we added a substitution.
		break;
	    }
	    break;
	  }
	  if (qualifier_char != 'A')
	    ++iter;
	}
	M_printing_suppressed = false;
      }

    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_type_with_postfix(
	  string_type& prefix, string_type& postfix,
	  qualifier_list<Allocator>* qualifiers)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING2
	    (qualifiers ? "decode_type" : "decode_type[with qualifiers]");
	++M_inside_type;
	bool recursive_template_param_or_substitution_call;
	if (!(recursive_template_param_or_substitution_call = qualifiers))
	    qualifiers = new qualifier_list<Allocator>(*this);
	// First eat all qualifiers.
	bool failure = false;
	for(;;)		// So we can use 'continue' to eat the next qualifier.
	{
	  int start_pos = M_pos;
	  switch(current())
	  {
	    case 'P':
	      qualifiers->add_qualifier_start(pointer, start_pos,
		  M_inside_substitution);
	      eat_current();
	      continue;
	    case 'R':
	      qualifiers->add_qualifier_start(reference, start_pos,
		  M_inside_substitution);
	      eat_current();
	      continue;
	    case 'K':
	    case 'V':
	    case 'r':
	    {
	      char c;
	      int count = 0;
	      do
	      {
		++count;
		c = next();
	      }
	      while(c == 'K' || c == 'V' || c == 'r');
	      qualifiers->add_qualifier_start(cv_qualifier, start_pos, count,
		  M_inside_substitution);
	      continue;
	    }
	    case 'U':
	    {
	      eat_current();
	      string_type source_name;
	      if (!decode_source_name(source_name))
	      {
		failure = true;
		break;
	      }
	      qualifiers->add_qualifier_start(vendor_extension, start_pos,
		  source_name, M_inside_substitution);
	      continue;
	    }
	    case 'A':
	    {
	      // <array-type> ::= A <positive dimension number> _ <element type>
	      //              ::= A [<dimension expression>] _ <element type>
	      //
	      string_type index;
	      int saved_pos;
	      store(saved_pos);
	      if (next() == 'n' || !decode_number(index))
	      {
		restore(saved_pos);
		if (next() != '_' && !decode_expression(index))
		{
		  failure = true;
		  break;
		}
	      }
	      if (eat_current() != '_')
	      {
		failure = true;
		break;
	      }
	      qualifiers->add_qualifier_start(array, start_pos, index,
		  M_inside_substitution);
	      continue;
	    }
	    case 'M':
	    {
	      // <Q>M<C> or <Q>M<C><Q2>F<R><B>E
	      eat_current();
	      string_type class_type;
	      if (!decode_type(class_type))		// Substitution: "<C>".
	      {
		failure = true;
		break;
	      }
	      char c = current();
	      if (c == 'F' || c == 'K' || c == 'V' || c == 'r')
		  // Must be CV-qualifiers and a member function pointer.
	      {
		// <Q>M<C><Q2>F<R><B>E	==> R (C::*Q)B Q2
		//     substitutions: "<C>", "F<R><B>E" (<R> and <B>
		//                    recursive), "M<C><Q2>F<R><B>E".
		int count = 0;
		int Q2_start_pos = M_pos;
		while(c == 'K' || c == 'V' || c == 'r')		// Decode <Q2>.
		{
		  ++count;
		  c = next();
		}
		qualifier_list<Allocator> class_type_qualifiers(*this);
		if (count)
		  class_type_qualifiers.
		      add_qualifier_start(cv_qualifier, Q2_start_pos,
			  count, M_inside_substitution);
		string_type member_function_qualifiers;
		// It is unclear why g++ doesn't add a substitution for
		// "<Q2>F<R><B>E" as it should I think.
		string_type member_function_qualifiers_postfix;
		class_type_qualifiers.
		    decode_qualifiers(member_function_qualifiers,
			member_function_qualifiers_postfix, true);
		member_function_qualifiers +=
		    member_function_qualifiers_postfix;
		// I don't think this substitution is actually ever used.
		int function_pos = M_pos;
		if (eat_current() != 'F')
		{
		  failure = true;
		  break;
		}
		// Return type.
		// Constructors, destructors and conversion operators don't
		// have a return type, but seem to never get here.
		if (!decode_type_with_postfix(prefix, postfix))
		    // substitution: <R> recursive
		{
		  failure = true;
		  break;
		}
		prefix += " (";
		prefix += class_type;
		prefix += "::*";
		string_type bare_function_type;
		if (!decode_bare_function_type(bare_function_type)
		    || eat_current() != 'E')	// Substitution: <B> recursive.
		{
		  failure = true;
		  break;
		}
		// substitution: "F<R><B>E".
		add_substitution(function_pos, type);
		// substitution: "M<C><Q2>F<R><B>E".
		add_substitution(start_pos, type);
		// substitution: all qualified types if any.
		qualifiers->decode_qualifiers(prefix, postfix);
		prefix += ")";
		prefix += bare_function_type;
		prefix += member_function_qualifiers;
		goto decode_type_exit;
	      }
	      qualifiers->add_qualifier_start(pointer_to_member, start_pos,
		  class_type, M_inside_substitution);
	      continue;
	    }
	    default:
	      break;
	  }
	  break;
	}
	if (!failure)
	{
	  // <Q>G<T>     		==> imaginary T Q
	  //     substitutions: "<T>", "G<T>" (<T> recursive).
	  // <Q>C<T>     		==> complex T Q
	  //     substitutions: "<T>", "C<T>" (<T> recursive).
	  if (current() == 'C' || current() == 'G')
	  {
	    prefix += current() == 'C' ? "complex " : "imaginary ";
	    qualifiers->add_qualifier_start(complex_or_imaginary, M_pos,
		M_inside_substitution);
	    eat_current();
	  }
	  int start_pos = M_pos;
	  switch(current())
	  {
	    case 'F':
	    {
	      // <Q>F<R><B>E 		==> R (Q)B
	      //     substitution: "<R>", "<B>" (<B> recursive) and "F<R><B>E".
	      eat_current();
	      // Return type.
	      if (!decode_type_with_postfix(prefix, postfix))
		  // Substitution: "<R>".
	      {
		failure = true;
		break;
	      }
	      // Only array (pointer) types have a postfix.
	      // In that case we don't want the space but
	      // expect something like prefix is "int (*"
	      // and postfix is ") [1]".
	      if (postfix.size() == 0)
		prefix += ' ';
	      prefix += '(';
	      string_type bare_function_type;
	      if (!decode_bare_function_type(bare_function_type)
		  // substitution: "<B>" (<B> recursive).
		  || eat_current() != 'E')
	      {
		failure = true;
		break;
	      }
	      add_substitution(start_pos, type);  // Substitution: "F<R><B>E".
	      qualifiers->decode_qualifiers(prefix, postfix);
		  // substitution: all qualified types, if any.
	      prefix += ")";
	      prefix += bare_function_type;
	      break;
	    }
	    case 'T':
	      if (!decode_template_param(prefix, qualifiers))
	      {
		failure = true;
		break;
	      }
	      if (current() == 'I')
	      {
		add_substitution(start_pos, template_template_param);
		    // substitution: "<template-template-param>".
		if (!decode_template_args(prefix))
		{
		  failure = true;
		  break;
		}
	      }
	      if (!recursive_template_param_or_substitution_call
		  && qualifiers->suppressed())
	      {
		add_substitution(start_pos, type);
		    // substitution: "<template-param>" or
		    // "<template-template-param> <template-args>".
		qualifiers->decode_qualifiers(prefix, postfix);
		    // substitution: all qualified types, if any.
	      }
	      break;
	    case 'S':
	      if (M_pos >= M_maxpos)
	      {
		failure = true;
		break;
	      }
	      if (M_str[M_pos + 1] != 't')
	      {
		if (!decode_substitution(prefix, qualifiers))
		{
		  failure = true;
		  break;
		}
		if (current() == 'I')
		{
		  if (!decode_template_args(prefix))
		  {
		    failure = true;
		    break;
		  }
		  if (!recursive_template_param_or_substitution_call
		      && qualifiers->suppressed())
		    add_substitution(start_pos, type);
			// Substitution:
			//   "<template-template-param> <template-args>".
		}
		if (!recursive_template_param_or_substitution_call
		    && qualifiers->suppressed())
		  qualifiers->decode_qualifiers(prefix, postfix);
		      // Substitution: all qualified types, if any.
		break;
	      }
	      /* Fall-through for St */
	    case 'N':
	    case 'Z':
	    case '0':
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	    case '8':
	    case '9':
	      // <Q><T>      		==> T Q
	      //     substitutions: "<T>" (<T> recursive).
	      if (!decode_class_enum_type(prefix))
	      {
		failure = true;
		break;
	      }
	      if (!recursive_template_param_or_substitution_call)
	      {
		add_substitution(start_pos, type);
		    // substitution: "<class-enum-type>".
		qualifiers->decode_qualifiers(prefix, postfix);
		    // substitution: all qualified types, if any.
	      }
	      else
		qualifiers->printing_suppressed();
	      break;
	    default:
	      // <Q><T>      		==> T Q
	      //     substitutions: "<T>" (<T> recursive).
	      if (!decode_builtin_type(prefix))
	      {
		failure = true;
		break;
	      }
	      // If decode_type was called from decode_template_param then we
	      // need to suppress calling qualifiers here in order to get a
	      // substitution added anyway (for the <template-param>).
	      if (!recursive_template_param_or_substitution_call)
		qualifiers->decode_qualifiers(prefix, postfix);
	      else
		qualifiers->printing_suppressed();
	      break;
	  }
	}
    decode_type_exit:
	--M_inside_type;
	if (!recursive_template_param_or_substitution_call)
	  delete qualifiers;
	if (failure)
	  _GLIBCXX_DEMANGLER_FAILURE;
	_GLIBCXX_DEMANGLER_RETURN2;
      }

    // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
    //               ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
    //
    // <prefix> ::= <prefix> <unqualified-name>
    //          ::= <template-prefix> <template-args>
    //          ::= # empty
    //          ::= <substitution>
    //
    // <template-prefix> ::= <prefix> <template unqualified-name>
    //                   ::= <substitution>
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_nested_name(string_type& output,
					     string_type& qualifiers)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_nested_name");

	if (current() != 'N' || M_pos >= M_maxpos)
	  _GLIBCXX_DEMANGLER_FAILURE;

	// <CV-qualifiers> ::= [r] [V] [K]  # restrict (C99), volatile, const
	char const* qualifiers_start = &M_str[M_pos + 1];
	for (char c = next(); c == 'K' || c == 'V' || c == 'r'; c = next());
	for (char const* qualifier_ptr = &M_str[M_pos - 1];
	     qualifier_ptr >= qualifiers_start; --qualifier_ptr)
	  switch(*qualifier_ptr)
	  {
	    case 'K':
	      qualifiers += " const";
	      break;
	    case 'V':
	      qualifiers += " volatile";
	      break;
	    case 'r':
	      qualifiers += " restrict";
	      break;
	  }

	int number_of_prefixes = 0;
	int substitution_start = M_pos;
	for(;;)
	{
	  ++number_of_prefixes;
	  if (current() == 'S')
	  {
	    if (!decode_substitution(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	  }
	  else if (current() == 'I')
	  {
	    if (!decode_template_args(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (current() != 'E')
	    {
	      // substitution: "<template-prefix> <template-args>".
	      add_substitution(substitution_start, nested_name_prefix,
		  	       number_of_prefixes);
	    }
	  }
	  else
	  {
	    if (!decode_unqualified_name(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (current() != 'E')
	    {
	      // substitution: "<prefix> <unqualified-name>" or
	      // "<prefix> <template unqualified-name>".
	      add_substitution(substitution_start,
		  (current() == 'I') ?  nested_name_template_prefix
		                     : nested_name_prefix,
		  number_of_prefixes);
	    }
	  }
	  if (current() == 'E')
	  {
	    eat_current();
	    _GLIBCXX_DEMANGLER_RETURN;
	  }
	  if (current() != 'I')
	    output += "::";
	  else if (M_template_args_need_space)
	    output += ' ';
	  M_template_args_need_space = false;
	}
	_GLIBCXX_DEMANGLER_FAILURE;
      }

    // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
    //              := Z <function encoding> E s [<discriminator>]
    // <discriminator> := _ <non-negative number>
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_local_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_local_name");
	if (current() != 'Z' || M_pos >= M_maxpos)
	  _GLIBCXX_DEMANGLER_FAILURE;
	if ((M_pos += decode_encoding(output, M_str + M_pos + 1,
		M_maxpos - M_pos) + 1) < 0 || eat_current() != 'E')
	  _GLIBCXX_DEMANGLER_FAILURE;
	output += "::";
	if (current() == 's')
	{
	  eat_current();
	  output += "string literal";
	}
	else
	{
	  string_type nested_name_qualifiers;
	  if (!decode_name(output, nested_name_qualifiers))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  output += nested_name_qualifiers;
	}
	string_type discriminator;
	if (current() == '_' && next() != 'n' && !decode_number(discriminator))
	  _GLIBCXX_DEMANGLER_FAILURE;
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <source-name> ::= <positive length number> <identifier>
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_source_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_source_name");
	int length = current() - '0';
	if (length < 1 || length > 9)
	  _GLIBCXX_DEMANGLER_FAILURE;
	while(isdigit(next()))
	  length = 10 * length + current() - '0';
	char const* ptr = &M_str[M_pos];
	if (length > 11 && !strncmp(ptr, "_GLOBAL_", 8) && ptr[9] == 'N'
	    && ptr[8] == ptr[10])
	{
	  output += "(anonymous namespace)";
	  if ((M_pos += length) > M_maxpos + 1)
	    _GLIBCXX_DEMANGLER_FAILURE;
	}
	else
	  while(length--)
	  {
	    if (current() == 0)
	      _GLIBCXX_DEMANGLER_FAILURE;
	    output += eat_current();
	  }
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <unqualified-name> ::= <operator-name>	# Starts with lower case.
    //                    ::= <ctor-dtor-name>  # Starts with 'C' or 'D'.
    //                    ::= <source-name>   	# Starts with a digit.
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_unqualified_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_unqualified_name");
	if (isdigit(current()))
	{
	  if (!M_inside_template_args)
	  {
	    bool recursive_unqualified_name = (&M_function_name == &output);
	    // This can be a recursive call when we are decoding
	    // an <operator-name> that is a cast operator for a some
	    // <unqualified-name>; for example "operator Foo()".
	    // In that case this is thus not a ctor or dtor and we
	    // are not interested in updating M_function_name.
	    if (!recursive_unqualified_name)
	      M_function_name.clear();
	    M_name_is_template = false;
	    M_name_is_cdtor = false;
	    M_name_is_conversion_operator = false;
	    if (!decode_source_name(M_function_name))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (!recursive_unqualified_name)
	      output += M_function_name;
	  }
	  else if (!decode_source_name(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	if (islower(current()))
	{
	  if (!M_inside_template_args)
	  {
	    M_function_name.clear();
	    M_name_is_template = false;
	    M_name_is_cdtor = false;
	    M_name_is_conversion_operator = false;
	    if (!decode_operator_name(M_function_name))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    output += M_function_name;
	  }
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	if (current() == 'C' || current() == 'D')
	{
	  if (M_inside_template_args)
	    _GLIBCXX_DEMANGLER_FAILURE;
	  // <ctor-dtor-name> ::=
	  //   C1	# complete object (in-charge) constructor
	  //   C2	# base object (not-in-charge) constructor
	  //   C3	# complete object (in-charge) allocating constructor
	  //   D0	# deleting (in-charge) destructor
	  //   D1	# complete object (in-charge) destructor
	  //   D2	# base object (not-in-charge) destructor
	  //
	  if (current() == 'C')
	  {
	    char c = next();
	    if (c < '1' || c > '3')
	      _GLIBCXX_DEMANGLER_FAILURE;
	  }
	  else
	  {
	    char c = next();
	    if (c < '0' || c > '2')
	      _GLIBCXX_DEMANGLER_FAILURE;
	    output += '~';
	    M_saw_destructor = true;
	  }
	  M_name_is_cdtor = true;
	  eat_current();
	  output += M_function_name;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	_GLIBCXX_DEMANGLER_FAILURE;
      }

    // <unscoped-name> ::=
    //   <unqualified-name>		# Starts not with an 'S'
    //   St <unqualified-name>	# ::std::
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_unscoped_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_unscoped_name");
	if (current() == 'S')
	{
	  if (next() != 't')
	    _GLIBCXX_DEMANGLER_FAILURE;
	  eat_current();
	  output += "std::";
	}
	decode_unqualified_name(output);
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <name> ::=
    //   <nested-name>				# Starts with 'N'
    //   <unscoped-template-name> <template-args> # idem
    //   <local-name>				# Starts with 'Z'
    //   <unscoped-name>			# Starts with 'S', 'C', 'D',
    //   					# a digit or a lower case
    //   					# character.
    //
    // <unscoped-template-name> ::= <unscoped-name>
    //                          ::= <substitution>
    template<typename Allocator>
      bool
      session<Allocator>::decode_name(string_type& output,
				      string_type& nested_name_qualifiers)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_name");
	int substitution_start = M_pos;
	if (current() == 'S' && (M_pos >= M_maxpos || M_str[M_pos + 1] != 't'))
	{
	  if (!decode_substitution(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	}
	else if (current() == 'N')
	{
	  decode_nested_name(output, nested_name_qualifiers);
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	else if (current() == 'Z')
	{
	  decode_local_name(output);
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	else if (!decode_unscoped_name(output))
	  _GLIBCXX_DEMANGLER_FAILURE;
	if (current() == 'I')
	{
	  // Must have been an <unscoped-template-name>.
	  add_substitution(substitution_start, unscoped_template_name);
	  if (!decode_template_args(output))
	    _GLIBCXX_DEMANGLER_FAILURE;
	}
	M_template_args_need_space = false;
	_GLIBCXX_DEMANGLER_RETURN;
      }

    // <call-offset> ::= h <nv-offset> _
    //               ::= v <v-offset> _
    // <nv-offset>   ::= <offset number> 
    //     non-virtual base override
    //
    // <v-offset>    ::= <offset number> _ <virtual offset number>
    //     virtual base override, with vcall offset
    template<typename Allocator>
      bool
      session<Allocator>::decode_call_offset(string_type&
#if _GLIBCXX_DEMANGLER_CWDEBUG
	  output
#endif
	  )
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_call_offset");
	if (current() == 'h')
	{
	  string_type dummy;
	  eat_current();
	  if (decode_number(dummy) && current() == '_')
	  {
	    eat_current();
	    _GLIBCXX_DEMANGLER_RETURN;
	  }
	}
	else if (current() == 'v')
	{
	  string_type dummy;
	  eat_current();
	  if (decode_number(dummy) && current() == '_')
	  {
	    eat_current();
	    if (decode_number(dummy) && current() == '_')
	    {
	      eat_current();
	      _GLIBCXX_DEMANGLER_RETURN;
	    }
	  }
	}
	_GLIBCXX_DEMANGLER_FAILURE;
      }

    //
    // <special-name> ::=
    //   TV <type>			# virtual table
    //   TT <type>			# VTT structure (construction
    //                                    vtable index).
    //   TI <type>			# typeinfo structure
    //   TS <type>			# typeinfo name (null-terminated
    //                                    byte string).
    //   GV <object name>		# Guard variable for one-time
    //   				  initialization of static objects in
    //   				  a local scope.
    //   T <call-offset> <base encoding># base is the nominal target function
    //   				  of thunk.
    //   Tc <call-offset> <call-offset> <base encoding> # base is the nominal
    //                                    target function of thunk; first
    //                                    call-offset is 'this' adjustment;
    //					  second call-offset is result
    //					  adjustment
    //
    template<typename Allocator>
      bool
      session<Allocator>::decode_special_name(string_type& output)
      {
	_GLIBCXX_DEMANGLER_DOUT_ENTERING("decode_special_name");
	if (current() == 'G')
	{
	  if (next() != 'V')
	    _GLIBCXX_DEMANGLER_FAILURE;
	  output += "guard variable for ";
	  string_type nested_name_qualifiers;
	  eat_current();
	  if (!decode_name(output, nested_name_qualifiers))
	    _GLIBCXX_DEMANGLER_FAILURE;
	  output += nested_name_qualifiers;
	  _GLIBCXX_DEMANGLER_RETURN;
	}
	else if (current() != 'T')
	  _GLIBCXX_DEMANGLER_FAILURE;
	switch(next())
	{
	  case 'V':
	    output += "vtable for ";
	    eat_current();
	    decode_type(output);
	    _GLIBCXX_DEMANGLER_RETURN;
	  case 'T':
	    output += "VTT for ";
	    eat_current();
	    decode_type(output);
	    _GLIBCXX_DEMANGLER_RETURN;
	  case 'I':
	    output += "typeinfo for ";
	    eat_current();
	    decode_type(output);
	    _GLIBCXX_DEMANGLER_RETURN;
	  case 'S':
	    output += "typeinfo name for ";
	    eat_current();
	    decode_type(output);
	    _GLIBCXX_DEMANGLER_RETURN;
	  case 'c':
	    output += "covariant return thunk to ";
	    if (!decode_call_offset(output)
		|| !decode_call_offset(output)
		|| (M_pos += decode_encoding(output, M_str + M_pos,
		    M_maxpos - M_pos + 1)) < 0)
	      _GLIBCXX_DEMANGLER_FAILURE;
	    _GLIBCXX_DEMANGLER_RETURN;
	  case 'C':		// GNU extension?
	  {
	    string_type first;
	    output += "construction vtable for ";
	    eat_current();
	    if (!decode_type(first))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    while(isdigit(current()))
	      eat_current();
	    if (eat_current() != '_')
	      _GLIBCXX_DEMANGLER_FAILURE;
	    if (!decode_type(output))
	      _GLIBCXX_DEMANGLER_FAILURE;
	    output += "-in-";
	    output += first;
	    _GLIBCXX_DEMANGLER_RETURN;
	  }
	  default:
	    if (current() == 'v')
	      output += "virtual thunk to ";
	    else
	      output += "non-virtual thunk to ";
	    if (!decode_call_offset(output)
		|| (M_pos += decode_encoding(output, M_str + M_pos,
		    M_maxpos - M_pos + 1)) < 0)
	      _GLIBCXX_DEMANGLER_FAILURE;
	    _GLIBCXX_DEMANGLER_RETURN;
	}
      }

    // <encoding> ::=
    //   <function name> <bare-function-type>	# Starts with 'C', 'D', 'N',
    //                                        	  'S', a digit or a lower case
    //                                        	  character.
    //   <data name>				# Idem.
    //   <special-name>				# Starts with 'T' or 'G'.
    template<typename Allocator>
      int
      session<Allocator>::decode_encoding(string_type& output,
					  char const* in,
					  int len)
      {
#if _GLIBCXX_DEMANGLER_CWDEBUG
	_GLIBCXX_DEMANGLER_DOUT(dc::demangler,
	    "Output thus far: \"" << output << '"');
	string_type input(in, len > 0x40000000 ? strlen(in) : len);
	_GLIBCXX_DEMANGLER_DOUT(
	    dc::demangler, "Entering decode_encoding(\"" << input << "\")");
#endif
	if (len <= 0)
	  return INT_MIN;
	session<Allocator> demangler_session(in, len);
	string_type nested_name_qualifiers;
	int saved_pos;
	demangler_session.store(saved_pos);
	if (demangler_session.decode_special_name(output))
	  return demangler_session.M_pos;
	demangler_session.restore(saved_pos);
	string_type name;
	if (!demangler_session.decode_name(name, nested_name_qualifiers))
	  return INT_MIN;
	if (demangler_session.current() == 0
	    || demangler_session.current() == 'E')
	{
	  output += name;
	  output += nested_name_qualifiers;
	  return demangler_session.M_pos;
	}
	// Must have been a <function name>.
	if (demangler_session.M_name_is_template
	    && !(demangler_session.M_name_is_cdtor
	         || demangler_session.M_name_is_conversion_operator))
	{
	  if (!demangler_session.decode_type(output))
	      // Return type of function
	    return INT_MIN;
	  output += ' ';
	}
	output += name;
	if (!demangler_session.decode_bare_function_type(output))
	  return INT_MIN;
	output += nested_name_qualifiers;
	return demangler_session.M_pos;
      }

    } // namespace demangler

  // Public interface
  template<typename Allocator>
    struct demangle
    {
      typedef Allocator allocator_type;
      typedef std::basic_string<char, std::char_traits<char>, Allocator> 
	  string_type;
      static string_type symbol(char const* in);
      static string_type type(char const* in);
    };

  // demangle::symbol()
  //
  // Demangle `input' which should be a mangled function name as for
  // instance returned by nm(1).
  template<typename Allocator>
    std::basic_string<char, std::char_traits<char>, Allocator>
    demangle<Allocator>::symbol(char const* input)
    {
      // <mangled-name> ::= _Z <encoding>
      // <mangled-name> ::= _GLOBAL_ _<type>_ _Z <encoding>		
      //                    <type> can be I or D (GNU extension)
      typedef demangler::session<Allocator> demangler_type;
      string_type result;
      bool failure = (input[0] != '_');

      if (!failure)
      {
	if (input[1] == 'G')
	{
	  if (!strncmp(input, "_GLOBAL__", 9)
	      && (input[9] == 'D' || input[9] == 'I')
	      && input[10] == '_' && input[11] == '_' && input[12] == 'Z')
	  {
	    if (input[9] == 'D')
	      result.assign("global destructors keyed to ", 28);
	    else
	      result.assign("global constructors keyed to ", 29);
	    int cnt = demangler_type::decode_encoding(result, input + 13,
						      INT_MAX);
	    if (cnt < 0 || input[cnt + 13] != 0)
	      failure = true;
	  }
	  else
	    failure = true;
	}
	else if (input[1] == 'Z')
	{
	  int cnt = demangler_type::decode_encoding(result, input + 2,
						    INT_MAX);
	  if (cnt < 0 || input[cnt + 2] != 0)
	    failure = true;
	}
	else
	  failure = true;
      }

      // Failure to demangle, return the mangled name.
      if (failure)
	result.assign(input, strlen(input));

      return result;
    }

  // demangle::type()
  // Demangle `input' which must be a zero terminated mangled type
  // name as for instance returned by std::type_info::name().
  template<typename Allocator>
    std::basic_string<char, std::char_traits<char>, Allocator> 
    demangle<Allocator>::type(char const* input)
    {
      std::basic_string<char, std::char_traits<char>, Allocator> result;
      if (input == NULL)
	result = "(null)";
      else
      {
	demangler::session<Allocator> demangler_session(input, INT_MAX);
	if (!demangler_session.decode_type(result)
	    || demangler_session.remaining_input_characters())
	{
	  // Failure to demangle, return the mangled name.
	  result = input;				
	}
      }
      return result;
    }

} // namespace __gnu_cxx

#endif // __DEMANGLE_H