aboutsummaryrefslogtreecommitdiff
path: root/source/Utility/FastDemangle.cpp
blob: 90326c5f15c33aab246e4898c792638b1bb77510 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
//===-- FastDemangle.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Utility/FastDemangle.h"

#include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH

#include <functional>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#define DEBUG_FAILURES 1
//#define DEBUG_SUBSTITUTIONS 1
//#define DEBUG_TEMPLATE_ARGS 1
//#define DEBUG_HIGHWATER 1
//#define DEBUG_REORDERING 1

namespace {

/// @brief Represents the collection of qualifiers on a type

enum Qualifiers {
  QualifierNone = 0,
  QualifierConst = 1,
  QualifierRestrict = 2,
  QualifierVolatile = 4,
  QualifierReference = 8,
  QualifierRValueReference = 16,
  QualifierPointer = 32
};

/// @brief Categorizes the recognized operators

enum class OperatorKind {
  Unary,
  Postfix,
  Binary,
  Ternary,
  Other,
  ConversionOperator,
  Vendor,
  NoMatch
};

/// @brief Represents one of the recognized two-character operator
/// abbreviations used when parsing operators as names and expressions

struct Operator {
  const char *name;
  OperatorKind kind;
};

/// @brief Represents a range of characters in the output buffer, typically for
/// use with RewriteRange()

struct BufferRange {
  int offset;
  int length;
};

/// @brief Transient state required while parsing a name

struct NameState {
  bool parse_function_params;
  bool is_last_generic;
  bool has_no_return_type;
  BufferRange last_name_range;
};

/// @brief LLDB's fast C++ demangler
///
/// This is an incomplete implementation designed to speed up the demangling
/// process that is often a bottleneck when LLDB stops a process for the first
/// time.  Where the implementation doesn't know how to demangle a symbol it
/// fails gracefully to allow the caller to fall back to the existing demangler.
///
/// Over time the full mangling spec should be supported without compromising
/// performance for the most common cases.

class SymbolDemangler {
public:
  //----------------------------------------------------
  // Public API
  //----------------------------------------------------

  /// @brief Create a SymbolDemangler
  ///
  /// The newly created demangler allocates and owns scratch memory sufficient
  /// for demangling typical symbols.  Additional memory will be allocated if
  /// needed and managed by the demangler instance.

  SymbolDemangler() {
    m_buffer = (char *)malloc(8192);
    m_buffer_end = m_buffer + 8192;
    m_owns_buffer = true;

    m_rewrite_ranges = (BufferRange *)malloc(128 * sizeof(BufferRange));
    m_rewrite_ranges_size = 128;
    m_owns_m_rewrite_ranges = true;
  }

  /// @brief Create a SymbolDemangler that uses provided scratch memory
  ///
  /// The provided memory is not owned by the demangler.  It will be
  /// overwritten during calls to GetDemangledCopy() but can be used for
  /// other purposes between calls.  The provided memory will not be freed
  /// when this instance is destroyed.
  ///
  /// If demangling a symbol requires additional space it will be allocated
  /// and managed by the demangler instance.
  ///
  /// @param storage_ptr Valid pointer to at least storage_size bytes of
  /// space that the SymbolDemangler can use during demangling
  ///
  /// @param storage_size Number of bytes of space available scratch memory
  /// referenced by storage_ptr

  SymbolDemangler(void *storage_ptr, size_t storage_size,
                  std::function<void(const char *)> builtins_hook = nullptr)
      : m_builtins_hook(builtins_hook) {
    // Use up to 1/8th of the provided space for rewrite ranges
    m_rewrite_ranges_size = (storage_size >> 3) / sizeof(BufferRange);
    m_rewrite_ranges = (BufferRange *)storage_ptr;
    m_owns_m_rewrite_ranges = false;

    // Use the rest for the character buffer
    m_buffer =
        (char *)storage_ptr + m_rewrite_ranges_size * sizeof(BufferRange);
    m_buffer_end = (const char *)storage_ptr + storage_size;
    m_owns_buffer = false;
  }

  /// @brief Destroys the SymbolDemangler and deallocates any scratch
  /// memory that it owns

  ~SymbolDemangler() {
    if (m_owns_buffer)
      free(m_buffer);
    if (m_owns_m_rewrite_ranges)
      free(m_rewrite_ranges);
  }

#ifdef DEBUG_HIGHWATER
  int highwater_store = 0;
  int highwater_buffer = 0;
#endif

  /// @brief Parses the provided mangled name and returns a newly allocated
  /// demangling
  ///
  /// @param mangled_name Valid null-terminated C++ mangled name following
  /// the Itanium C++ ABI mangling specification as implemented by Clang
  ///
  /// @result Newly allocated null-terminated demangled name when demangling
  /// is successful, and nullptr when demangling fails.  The caller is
  /// responsible for freeing the allocated memory.

  char *GetDemangledCopy(const char *mangled_name,
                         long mangled_name_length = 0) {
    if (!ParseMangling(mangled_name, mangled_name_length))
      return nullptr;

#ifdef DEBUG_HIGHWATER
    int rewrite_count = m_next_substitute_index +
                        (m_rewrite_ranges_size - 1 - m_next_template_arg_index);
    int buffer_size = (int)(m_write_ptr - m_buffer);
    if (rewrite_count > highwater_store)
      highwater_store = rewrite_count;
    if (buffer_size > highwater_buffer)
      highwater_buffer = buffer_size;
#endif

    int length = (int)(m_write_ptr - m_buffer);
    char *copy = (char *)malloc(length + 1);
    memcpy(copy, m_buffer, length);
    copy[length] = '\0';
    return copy;
  }

private:
  //----------------------------------------------------
  // Grow methods
  //
  // Manage the storage used during demangling
  //----------------------------------------------------

  void GrowBuffer(long min_growth = 0) {
    // By default, double the size of the buffer
    long growth = m_buffer_end - m_buffer;

    // Avoid growing by more than 1MB at a time
    if (growth > 1 << 20)
      growth = 1 << 20;

    // ... but never grow by less than requested,
    // or 1K, whichever is greater
    if (min_growth < 1024)
      min_growth = 1024;
    if (growth < min_growth)
      growth = min_growth;

    // Allocate the new m_buffer and migrate content
    long new_size = (m_buffer_end - m_buffer) + growth;
    char *new_buffer = (char *)malloc(new_size);
    memcpy(new_buffer, m_buffer, m_write_ptr - m_buffer);
    if (m_owns_buffer)
      free(m_buffer);
    m_owns_buffer = true;

    // Update references to the new buffer
    m_write_ptr = new_buffer + (m_write_ptr - m_buffer);
    m_buffer = new_buffer;
    m_buffer_end = m_buffer + new_size;
  }

  void GrowRewriteRanges() {
    // By default, double the size of the array
    int growth = m_rewrite_ranges_size;

    // Apply reasonable minimum and maximum sizes for growth
    if (growth > 128)
      growth = 128;
    if (growth < 16)
      growth = 16;

    // Allocate the new array and migrate content
    int bytes = (m_rewrite_ranges_size + growth) * sizeof(BufferRange);
    BufferRange *new_ranges = (BufferRange *)malloc(bytes);
    for (int index = 0; index < m_next_substitute_index; index++) {
      new_ranges[index] = m_rewrite_ranges[index];
    }
    for (int index = m_rewrite_ranges_size - 1;
         index > m_next_template_arg_index; index--) {
      new_ranges[index + growth] = m_rewrite_ranges[index];
    }
    if (m_owns_m_rewrite_ranges)
      free(m_rewrite_ranges);
    m_owns_m_rewrite_ranges = true;

    // Update references to the new array
    m_rewrite_ranges = new_ranges;
    m_rewrite_ranges_size += growth;
    m_next_template_arg_index += growth;
  }

  //----------------------------------------------------
  // Range and state management
  //----------------------------------------------------

  int GetStartCookie() { return (int)(m_write_ptr - m_buffer); }

  BufferRange EndRange(int start_cookie) {
    return {start_cookie, (int)(m_write_ptr - (m_buffer + start_cookie))};
  }

  void ReorderRange(BufferRange source_range, int insertion_point_cookie) {
    // Ensure there's room the preserve the source range
    if (m_write_ptr + source_range.length > m_buffer_end) {
      GrowBuffer(m_write_ptr + source_range.length - m_buffer_end);
    }

    // Reorder the content
    memcpy(m_write_ptr, m_buffer + source_range.offset, source_range.length);
    memmove(m_buffer + insertion_point_cookie + source_range.length,
            m_buffer + insertion_point_cookie,
            source_range.offset - insertion_point_cookie);
    memcpy(m_buffer + insertion_point_cookie, m_write_ptr, source_range.length);

    // Fix up rewritable ranges, covering both substitutions and templates
    int index = 0;
    while (true) {
      if (index == m_next_substitute_index)
        index = m_next_template_arg_index + 1;
      if (index == m_rewrite_ranges_size)
        break;

      // Affected ranges are either shuffled forward when after the
      // insertion but before the source, or backward when inside the
      // source
      int candidate_offset = m_rewrite_ranges[index].offset;
      if (candidate_offset >= insertion_point_cookie) {
        if (candidate_offset < source_range.offset) {
          m_rewrite_ranges[index].offset += source_range.length;
        } else if (candidate_offset >= source_range.offset) {
          m_rewrite_ranges[index].offset -=
              (source_range.offset - insertion_point_cookie);
        }
      }
      ++index;
    }
  }

  void EndSubstitution(int start_cookie) {
    if (m_next_substitute_index == m_next_template_arg_index)
      GrowRewriteRanges();

    int index = m_next_substitute_index++;
    m_rewrite_ranges[index] = EndRange(start_cookie);
#ifdef DEBUG_SUBSTITUTIONS
    printf("Saved substitution # %d = %.*s\n", index,
           m_rewrite_ranges[index].length, m_buffer + start_cookie);
#endif
  }

  void EndTemplateArg(int start_cookie) {
    if (m_next_substitute_index == m_next_template_arg_index)
      GrowRewriteRanges();

    int index = m_next_template_arg_index--;
    m_rewrite_ranges[index] = EndRange(start_cookie);
#ifdef DEBUG_TEMPLATE_ARGS
    printf("Saved template arg # %d = %.*s\n",
           m_rewrite_ranges_size - index - 1, m_rewrite_ranges[index].length,
           m_buffer + start_cookie);
#endif
  }

  void ResetTemplateArgs() {
    // TODO: this works, but is it the right thing to do?
    // Should we push/pop somehow at the call sites?
    m_next_template_arg_index = m_rewrite_ranges_size - 1;
  }

  //----------------------------------------------------
  // Write methods
  //
  // Appends content to the existing output buffer
  //----------------------------------------------------

  void Write(char character) {
    if (m_write_ptr == m_buffer_end)
      GrowBuffer();
    *m_write_ptr++ = character;
  }

  void Write(const char *content) { Write(content, strlen(content)); }

  void Write(const char *content, long content_length) {
    char *end_m_write_ptr = m_write_ptr + content_length;
    if (end_m_write_ptr > m_buffer_end) {
      if (content >= m_buffer && content < m_buffer_end) {
        long offset = content - m_buffer;
        GrowBuffer(end_m_write_ptr - m_buffer_end);
        content = m_buffer + offset;
      } else {
        GrowBuffer(end_m_write_ptr - m_buffer_end);
      }
      end_m_write_ptr = m_write_ptr + content_length;
    }
    memcpy(m_write_ptr, content, content_length);
    m_write_ptr = end_m_write_ptr;
  }
#define WRITE(x) Write(x, sizeof(x) - 1)

  void WriteTemplateStart() { Write('<'); }

  void WriteTemplateEnd() {
    // Put a space between terminal > characters when nesting templates
    if (m_write_ptr != m_buffer && *(m_write_ptr - 1) == '>')
      WRITE(" >");
    else
      Write('>');
  }

  void WriteCommaSpace() { WRITE(", "); }

  void WriteNamespaceSeparator() { WRITE("::"); }

  void WriteStdPrefix() { WRITE("std::"); }

  void WriteQualifiers(int qualifiers, bool space_before_reference = true) {
    if (qualifiers & QualifierPointer)
      Write('*');
    if (qualifiers & QualifierConst)
      WRITE(" const");
    if (qualifiers & QualifierVolatile)
      WRITE(" volatile");
    if (qualifiers & QualifierRestrict)
      WRITE(" restrict");
    if (qualifiers & QualifierReference) {
      if (space_before_reference)
        WRITE(" &");
      else
        Write('&');
    }
    if (qualifiers & QualifierRValueReference) {
      if (space_before_reference)
        WRITE(" &&");
      else
        WRITE("&&");
    }
  }

  //----------------------------------------------------
  // Rewrite methods
  //
  // Write another copy of content already present
  // earlier in the output buffer
  //----------------------------------------------------

  void RewriteRange(BufferRange range) {
    Write(m_buffer + range.offset, range.length);
  }

  bool RewriteSubstitution(int index) {
    if (index < 0 || index >= m_next_substitute_index) {
#ifdef DEBUG_FAILURES
      printf("*** Invalid substitution #%d\n", index);
#endif
      return false;
    }
    RewriteRange(m_rewrite_ranges[index]);
    return true;
  }

  bool RewriteTemplateArg(int template_index) {
    int index = m_rewrite_ranges_size - 1 - template_index;
    if (template_index < 0 || index <= m_next_template_arg_index) {
#ifdef DEBUG_FAILURES
      printf("*** Invalid template arg reference #%d\n", template_index);
#endif
      return false;
    }
    RewriteRange(m_rewrite_ranges[index]);
    return true;
  }

  //----------------------------------------------------
  // TryParse methods
  //
  // Provide information with return values instead of
  // writing to the output buffer
  //
  // Values indicating failure guarantee that the pre-
  // call m_read_ptr is unchanged
  //----------------------------------------------------

  int TryParseNumber() {
    unsigned char digit = *m_read_ptr - '0';
    if (digit > 9)
      return -1;

    int count = digit;
    while (true) {
      digit = *++m_read_ptr - '0';
      if (digit > 9)
        break;

      count = count * 10 + digit;
    }
    return count;
  }

  int TryParseBase36Number() {
    char digit = *m_read_ptr;
    int count;
    if (digit >= '0' && digit <= '9')
      count = digit -= '0';
    else if (digit >= 'A' && digit <= 'Z')
      count = digit -= ('A' - 10);
    else
      return -1;

    while (true) {
      digit = *++m_read_ptr;
      if (digit >= '0' && digit <= '9')
        digit -= '0';
      else if (digit >= 'A' && digit <= 'Z')
        digit -= ('A' - 10);
      else
        break;

      count = count * 36 + digit;
    }
    return count;
  }

  // <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
  //                ::= Dd   # IEEE 754r decimal floating point (64 bits)
  //                ::= De   # IEEE 754r decimal floating point (128 bits)
  //                ::= Df   # IEEE 754r decimal floating point (32 bits)
  //                ::= Dh   # IEEE 754r half-precision floating point (16 bits)
  //                ::= Di   # char32_t
  //                ::= Ds   # char16_t
  //                ::= Da   # auto (in dependent new-expressions)
  //                ::= Dn   # std::nullptr_t (i.e., decltype(nullptr))
  //                ::= u <source-name>    # vendor extended type

  const char *TryParseBuiltinType() {
    if (m_builtins_hook)
      m_builtins_hook(m_read_ptr);

    switch (*m_read_ptr++) {
    case 'v':
      return "void";
    case 'w':
      return "wchar_t";
    case 'b':
      return "bool";
    case 'c':
      return "char";
    case 'a':
      return "signed char";
    case 'h':
      return "unsigned char";
    case 's':
      return "short";
    case 't':
      return "unsigned short";
    case 'i':
      return "int";
    case 'j':
      return "unsigned int";
    case 'l':
      return "long";
    case 'm':
      return "unsigned long";
    case 'x':
      return "long long";
    case 'y':
      return "unsigned long long";
    case 'n':
      return "__int128";
    case 'o':
      return "unsigned __int128";
    case 'f':
      return "float";
    case 'd':
      return "double";
    case 'e':
      return "long double";
    case 'g':
      return "__float128";
    case 'z':
      return "...";
    case 'D': {
      switch (*m_read_ptr++) {
      case 'd':
        return "decimal64";
      case 'e':
        return "decimal128";
      case 'f':
        return "decimal32";
      case 'h':
        return "decimal16";
      case 'i':
        return "char32_t";
      case 's':
        return "char16_t";
      case 'a':
        return "auto";
      case 'c':
        return "decltype(auto)";
      case 'n':
        return "std::nullptr_t";
      default:
        --m_read_ptr;
      }
    }
    }
    --m_read_ptr;
    return nullptr;
  }

  //   <operator-name>
  //                   ::= aa    # &&
  //                   ::= ad    # & (unary)
  //                   ::= an    # &
  //                   ::= aN    # &=
  //                   ::= aS    # =
  //                   ::= cl    # ()
  //                   ::= cm    # ,
  //                   ::= co    # ~
  //                   ::= da    # delete[]
  //                   ::= de    # * (unary)
  //                   ::= dl    # delete
  //                   ::= dv    # /
  //                   ::= dV    # /=
  //                   ::= eo    # ^
  //                   ::= eO    # ^=
  //                   ::= eq    # ==
  //                   ::= ge    # >=
  //                   ::= gt    # >
  //                   ::= ix    # []
  //                   ::= le    # <=
  //                   ::= ls    # <<
  //                   ::= lS    # <<=
  //                   ::= lt    # <
  //                   ::= mi    # -
  //                   ::= mI    # -=
  //                   ::= ml    # *
  //                   ::= mL    # *=
  //                   ::= mm    # -- (postfix in <expression> context)
  //                   ::= na    # new[]
  //                   ::= ne    # !=
  //                   ::= ng    # - (unary)
  //                   ::= nt    # !
  //                   ::= nw    # new
  //                   ::= oo    # ||
  //                   ::= or    # |
  //                   ::= oR    # |=
  //                   ::= pm    # ->*
  //                   ::= pl    # +
  //                   ::= pL    # +=
  //                   ::= pp    # ++ (postfix in <expression> context)
  //                   ::= ps    # + (unary)
  //                   ::= pt    # ->
  //                   ::= qu    # ?
  //                   ::= rm    # %
  //                   ::= rM    # %=
  //                   ::= rs    # >>
  //                   ::= rS    # >>=
  //                   ::= cv <type>    # (cast)
  //                   ::= v <digit> <source-name>        # vendor extended
  //                   operator

  Operator TryParseOperator() {
    switch (*m_read_ptr++) {
    case 'a':
      switch (*m_read_ptr++) {
      case 'a':
        return {"&&", OperatorKind::Binary};
      case 'd':
        return {"&", OperatorKind::Unary};
      case 'n':
        return {"&", OperatorKind::Binary};
      case 'N':
        return {"&=", OperatorKind::Binary};
      case 'S':
        return {"=", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'c':
      switch (*m_read_ptr++) {
      case 'l':
        return {"()", OperatorKind::Other};
      case 'm':
        return {",", OperatorKind::Other};
      case 'o':
        return {"~", OperatorKind::Unary};
      case 'v':
        return {nullptr, OperatorKind::ConversionOperator};
      }
      --m_read_ptr;
      break;
    case 'd':
      switch (*m_read_ptr++) {
      case 'a':
        return {" delete[]", OperatorKind::Other};
      case 'e':
        return {"*", OperatorKind::Unary};
      case 'l':
        return {" delete", OperatorKind::Other};
      case 'v':
        return {"/", OperatorKind::Binary};
      case 'V':
        return {"/=", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'e':
      switch (*m_read_ptr++) {
      case 'o':
        return {"^", OperatorKind::Binary};
      case 'O':
        return {"^=", OperatorKind::Binary};
      case 'q':
        return {"==", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'g':
      switch (*m_read_ptr++) {
      case 'e':
        return {">=", OperatorKind::Binary};
      case 't':
        return {">", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'i':
      switch (*m_read_ptr++) {
      case 'x':
        return {"[]", OperatorKind::Other};
      }
      --m_read_ptr;
      break;
    case 'l':
      switch (*m_read_ptr++) {
      case 'e':
        return {"<=", OperatorKind::Binary};
      case 's':
        return {"<<", OperatorKind::Binary};
      case 'S':
        return {"<<=", OperatorKind::Binary};
      case 't':
        return {"<", OperatorKind::Binary};
        // case 'i': return { "?", OperatorKind::Binary };
      }
      --m_read_ptr;
      break;
    case 'm':
      switch (*m_read_ptr++) {
      case 'i':
        return {"-", OperatorKind::Binary};
      case 'I':
        return {"-=", OperatorKind::Binary};
      case 'l':
        return {"*", OperatorKind::Binary};
      case 'L':
        return {"*=", OperatorKind::Binary};
      case 'm':
        return {"--", OperatorKind::Postfix};
      }
      --m_read_ptr;
      break;
    case 'n':
      switch (*m_read_ptr++) {
      case 'a':
        return {" new[]", OperatorKind::Other};
      case 'e':
        return {"!=", OperatorKind::Binary};
      case 'g':
        return {"-", OperatorKind::Unary};
      case 't':
        return {"!", OperatorKind::Unary};
      case 'w':
        return {" new", OperatorKind::Other};
      }
      --m_read_ptr;
      break;
    case 'o':
      switch (*m_read_ptr++) {
      case 'o':
        return {"||", OperatorKind::Binary};
      case 'r':
        return {"|", OperatorKind::Binary};
      case 'R':
        return {"|=", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'p':
      switch (*m_read_ptr++) {
      case 'm':
        return {"->*", OperatorKind::Binary};
      case 's':
        return {"+", OperatorKind::Unary};
      case 'l':
        return {"+", OperatorKind::Binary};
      case 'L':
        return {"+=", OperatorKind::Binary};
      case 'p':
        return {"++", OperatorKind::Postfix};
      case 't':
        return {"->", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'q':
      switch (*m_read_ptr++) {
      case 'u':
        return {"?", OperatorKind::Ternary};
      }
      --m_read_ptr;
      break;
    case 'r':
      switch (*m_read_ptr++) {
      case 'm':
        return {"%", OperatorKind::Binary};
      case 'M':
        return {"%=", OperatorKind::Binary};
      case 's':
        return {">>", OperatorKind::Binary};
      case 'S':
        return {">=", OperatorKind::Binary};
      }
      --m_read_ptr;
      break;
    case 'v':
      char digit = *m_read_ptr;
      if (digit >= '0' && digit <= '9') {
        m_read_ptr++;
        return {nullptr, OperatorKind::Vendor};
      }
      --m_read_ptr;
      break;
    }
    --m_read_ptr;
    return {nullptr, OperatorKind::NoMatch};
  }

  // <CV-qualifiers> ::= [r] [V] [K]
  // <ref-qualifier> ::= R                   # & ref-qualifier
  // <ref-qualifier> ::= O                   # && ref-qualifier

  int TryParseQualifiers(bool allow_cv, bool allow_ro) {
    int qualifiers = QualifierNone;
    char next = *m_read_ptr;
    if (allow_cv) {
      if (next == 'r') // restrict
      {
        qualifiers |= QualifierRestrict;
        next = *++m_read_ptr;
      }
      if (next == 'V') // volatile
      {
        qualifiers |= QualifierVolatile;
        next = *++m_read_ptr;
      }
      if (next == 'K') // const
      {
        qualifiers |= QualifierConst;
        next = *++m_read_ptr;
      }
    }
    if (allow_ro) {
      if (next == 'R') {
        ++m_read_ptr;
        qualifiers |= QualifierReference;
      } else if (next == 'O') {
        ++m_read_ptr;
        qualifiers |= QualifierRValueReference;
      }
    }
    return qualifiers;
  }

  // <discriminator> := _ <non-negative number>      # when number < 10
  //                 := __ <non-negative number> _   # when number >= 10
  //  extension      := decimal-digit+

  int TryParseDiscriminator() {
    const char *discriminator_start = m_read_ptr;

    // Test the extension first, since it's what Clang uses
    int discriminator_value = TryParseNumber();
    if (discriminator_value != -1)
      return discriminator_value;

    char next = *m_read_ptr;
    if (next == '_') {
      next = *++m_read_ptr;
      if (next == '_') {
        ++m_read_ptr;
        discriminator_value = TryParseNumber();
        if (discriminator_value != -1 && *m_read_ptr++ != '_') {
          return discriminator_value;
        }
      } else if (next >= '0' && next <= '9') {
        ++m_read_ptr;
        return next - '0';
      }
    }

    // Not a valid discriminator
    m_read_ptr = discriminator_start;
    return -1;
  }

  //----------------------------------------------------
  // Parse methods
  //
  // Consume input starting from m_read_ptr and produce
  // buffered output at m_write_ptr
  //
  // Failures return false and may leave m_read_ptr in an
  // indeterminate state
  //----------------------------------------------------

  bool Parse(char character) {
    if (*m_read_ptr++ == character)
      return true;
#ifdef DEBUG_FAILURES
    printf("*** Expected '%c'\n", character);
#endif
    return false;
  }

  // <number> ::= [n] <non-negative decimal integer>

  bool ParseNumber(bool allow_negative = false) {
    if (allow_negative && *m_read_ptr == 'n') {
      Write('-');
      ++m_read_ptr;
    }
    const char *before_digits = m_read_ptr;
    while (true) {
      unsigned char digit = *m_read_ptr - '0';
      if (digit > 9)
        break;
      ++m_read_ptr;
    }
    if (int digit_count = (int)(m_read_ptr - before_digits)) {
      Write(before_digits, digit_count);
      return true;
    }
#ifdef DEBUG_FAILURES
    printf("*** Expected number\n");
#endif
    return false;
  }

  // <substitution> ::= S <seq-id> _
  //                ::= S_
  // <substitution> ::= Sa # ::std::allocator
  // <substitution> ::= Sb # ::std::basic_string
  // <substitution> ::= Ss # ::std::basic_string < char,
  //                                               ::std::char_traits<char>,
  //                                               ::std::allocator<char> >
  // <substitution> ::= Si # ::std::basic_istream<char,  std::char_traits<char>
  // >
  // <substitution> ::= So # ::std::basic_ostream<char,  std::char_traits<char>
  // >
  // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char>
  // >

  bool ParseSubstitution() {
    const char *substitution;
    switch (*m_read_ptr) {
    case 'a':
      substitution = "std::allocator";
      break;
    case 'b':
      substitution = "std::basic_string";
      break;
    case 's':
      substitution = "std::string";
      break;
    case 'i':
      substitution = "std::istream";
      break;
    case 'o':
      substitution = "std::ostream";
      break;
    case 'd':
      substitution = "std::iostream";
      break;
    default:
      // A failed attempt to parse a number will return -1 which turns out to be
      // perfect here as S_ is the first substitution, S0_ the next and so forth
      int substitution_index = TryParseBase36Number();
      if (*m_read_ptr++ != '_') {
#ifdef DEBUG_FAILURES
        printf("*** Expected terminal _ in substitution\n");
#endif
        return false;
      }
      return RewriteSubstitution(substitution_index + 1);
    }
    Write(substitution);
    ++m_read_ptr;
    return true;
  }

  // <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E
  //
  // <bare-function-type> ::= <signature type>+      # types are possible return
  // type, then parameter types

  bool ParseFunctionType(int inner_qualifiers = QualifierNone) {
#ifdef DEBUG_FAILURES
    printf("*** Function types not supported\n");
#endif
    // TODO: first steps toward an implementation follow, but they're far
    // from complete.  Function types tend to bracket other types eg:
    // int (*)() when used as the type for "name" becomes int (*name)().
    // This makes substitution et al ... interesting.
    return false;

#if 0  // TODO
        if (*m_read_ptr == 'Y')
            ++m_read_ptr;

        int return_type_start_cookie = GetStartCookie();
        if (!ParseType())
            return false;
        Write(' ');

        int insert_cookie = GetStartCookie();
        Write('(');
        bool first_param = true;
        int qualifiers = QualifierNone;
        while (true)
        {
            switch (*m_read_ptr)
            {
                case 'E':
                    ++m_read_ptr;
                    Write(')');
                    break;
                case 'v':
                    ++m_read_ptr;
                    continue;
                case 'R':
                case 'O':
                    if (*(m_read_ptr + 1) == 'E')
                    {
                        qualifiers = TryParseQualifiers (false, true);
                        Parse('E');
                        break;
                    }
                    // fallthrough
                default:
                {
                    if (first_param)
                        first_param = false;
                    else WriteCommaSpace();

                    if (!ParseType())
                        return false;
                    continue;
                }
            }
            break;
        }

        if (qualifiers)
        {
            WriteQualifiers (qualifiers);
            EndSubstitution (return_type_start_cookie);
        }

        if (inner_qualifiers)
        {
            int qualifier_start_cookie = GetStartCookie();
            Write ('(');
            WriteQualifiers (inner_qualifiers);
            Write (')');
            ReorderRange (EndRange (qualifier_start_cookie), insert_cookie);
        }
        return true;
#endif // TODO
  }

  // <array-type> ::= A <positive dimension number> _ <element type>
  //              ::= A [<dimension expression>] _ <element type>

  bool ParseArrayType(int qualifiers = QualifierNone) {
#ifdef DEBUG_FAILURES
    printf("*** Array type unsupported\n");
#endif
    // TODO: We fail horribly when recalling these as substitutions or
    // templates and trying to constify them eg:
    // _ZN4llvm2cl5applyIA28_cNS0_3optIbLb0ENS0_6parserIbEEEEEEvRKT_PT0_
    //
    // TODO: Chances are we don't do any better with references and pointers
    // that should be type (&) [] instead of type & []

    return false;

#if 0  // TODO
        if (*m_read_ptr == '_')
        {
            ++m_read_ptr;
            if (!ParseType())
                return false;
            if (qualifiers)
                WriteQualifiers(qualifiers);
            WRITE(" []");
            return true;
        }
        else
        {
            const char *before_digits = m_read_ptr;
            if (TryParseNumber() != -1)
            {
                const char *after_digits = m_read_ptr;
                if (!Parse('_'))
                    return false;
                if (!ParseType())
                    return false;
                if (qualifiers)
                    WriteQualifiers(qualifiers);
                Write(' ');
                Write('[');
                Write(before_digits, after_digits - before_digits);
            }
            else
            {
                int type_insertion_cookie = GetStartCookie();
                if (!ParseExpression())
                    return false;
                if (!Parse('_'))
                    return false;

                int type_start_cookie = GetStartCookie();
                if (!ParseType())
                    return false;
                if (qualifiers)
                    WriteQualifiers(qualifiers);
                Write(' ');
                Write('[');
                ReorderRange (EndRange (type_start_cookie), type_insertion_cookie);
            }
            Write(']');
            return true;
        }
#endif // TODO
  }

  // <pointer-to-member-type> ::= M <class type> <member type>

  // TODO: Determine how to handle pointers to function members correctly,
  // currently not an issue because we don't have function types at all...
  bool ParsePointerToMemberType() {
    int insertion_cookie = GetStartCookie();
    Write(' ');
    if (!ParseType())
      return false;
    WRITE("::*");

    int type_cookie = GetStartCookie();
    if (!ParseType())
      return false;
    ReorderRange(EndRange(type_cookie), insertion_cookie);
    return true;
  }

  // <template-param> ::= T_    # first template parameter
  //                  ::= T <parameter-2 non-negative number> _

  bool ParseTemplateParam() {
    int count = TryParseNumber();
    if (!Parse('_'))
      return false;

    // When no number is present we get -1, which is convenient since
    // T_ is the zeroth element T0_ is element 1, and so on
    return RewriteTemplateArg(count + 1);
  }

  // <vector-type>
  // Dv <dimension number> _ <vector type>
  bool TryParseVectorType() {
    const int dimension = TryParseNumber();
    if (dimension == -1)
      return false;

    if (*m_read_ptr++ != '_')
      return false;

    char vec_dimens[32] = {'\0'};
    ::snprintf(vec_dimens, sizeof vec_dimens - 1, " __vector(%d)", dimension);
    ParseType();
    Write(vec_dimens);
    return true;
  }

  // <type> ::= <builtin-type>
  //        ::= <function-type>
  //        ::= <class-enum-type>
  //        ::= <array-type>
  //        ::= <pointer-to-member-type>
  //        ::= <template-param>
  //        ::= <template-template-param> <template-args>
  //        ::= <decltype>
  //        ::= <substitution>
  //        ::= <CV-qualifiers> <type>
  //        ::= P <type>        # pointer-to
  //        ::= R <type>        # reference-to
  //        ::= O <type>        # rvalue reference-to (C++0x)
  //        ::= C <type>        # complex pair (C 2000)
  //        ::= G <type>        # imaginary (C 2000)
  //        ::= Dp <type>       # pack expansion (C++0x)
  //        ::= U <source-name> <type>  # vendor extended type qualifier
  // extension := U <objc-name> <objc-type>  # objc-type<identifier>
  // extension := <vector-type> # <vector-type> starts with Dv

  // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier>  # k0 = 9 +
  // <number of digits in k1> + k1
  // <objc-type> := <source-name>  # PU<11+>objcproto 11objc_object<source-name>
  // 11objc_object -> id<source-name>

  bool ParseType() {
#ifdef DEBUG_FAILURES
    const char *failed_type = m_read_ptr;
#endif
    int type_start_cookie = GetStartCookie();
    bool suppress_substitution = false;

    int qualifiers = TryParseQualifiers(true, false);
    switch (*m_read_ptr) {
    case 'D':
      ++m_read_ptr;
      switch (*m_read_ptr++) {
      case 'p':
        if (!ParseType())
          return false;
        break;
      case 'v':
        if (!TryParseVectorType())
          return false;
        break;
      case 'T':
      case 't':
      default:
#ifdef DEBUG_FAILURES
        printf("*** Unsupported type: %.3s\n", failed_type);
#endif
        return false;
      }
      break;
    case 'T':
      ++m_read_ptr;
      if (!ParseTemplateParam())
        return false;
      break;
    case 'M':
      ++m_read_ptr;
      if (!ParsePointerToMemberType())
        return false;
      break;
    case 'A':
      ++m_read_ptr;
      if (!ParseArrayType())
        return false;
      break;
    case 'F':
      ++m_read_ptr;
      if (!ParseFunctionType())
        return false;
      break;
    case 'S':
      if (*++m_read_ptr == 't') {
        ++m_read_ptr;
        WriteStdPrefix();
        if (!ParseName())
          return false;
      } else {
        suppress_substitution = true;
        if (!ParseSubstitution())
          return false;
      }
      break;
    case 'P': {
      switch (*++m_read_ptr) {
      case 'F':
        ++m_read_ptr;
        if (!ParseFunctionType(QualifierPointer))
          return false;
        break;
      default:
        if (!ParseType())
          return false;
        Write('*');
        break;
      }
      break;
    }
    case 'R': {
      ++m_read_ptr;
      if (!ParseType())
        return false;
      Write('&');
      break;
    }
    case 'O': {
      ++m_read_ptr;
      if (!ParseType())
        return false;
      Write('&');
      Write('&');
      break;
    }
    case 'C':
    case 'G':
    case 'U':
#ifdef DEBUG_FAILURES
      printf("*** Unsupported type: %.3s\n", failed_type);
#endif
      return false;
    // Test for common cases to avoid TryParseBuiltinType() overhead
    case 'N':
    case 'Z':
    case 'L':
      if (!ParseName())
        return false;
      break;
    default:
      if (const char *builtin = TryParseBuiltinType()) {
        Write(builtin);
        suppress_substitution = true;
      } else {
        if (!ParseName())
          return false;
      }
      break;
    }

    // Allow base substitutions to be suppressed, but always record
    // substitutions for the qualified variant
    if (!suppress_substitution)
      EndSubstitution(type_start_cookie);
    if (qualifiers) {
      WriteQualifiers(qualifiers, false);
      EndSubstitution(type_start_cookie);
    }
    return true;
  }

  // <unnamed-type-name> ::= Ut [ <nonnegative number> ] _
  //                     ::= <closure-type-name>
  //
  // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
  //
  // <lambda-sig> ::= <parameter type>+  # Parameter types or "v" if the lambda
  // has no parameters

  bool ParseUnnamedTypeName(NameState &name_state) {
    switch (*m_read_ptr++) {
    case 't': {
      int cookie = GetStartCookie();
      WRITE("'unnamed");
      const char *before_digits = m_read_ptr;
      if (TryParseNumber() != -1)
        Write(before_digits, m_read_ptr - before_digits);
      if (!Parse('_'))
        return false;
      Write('\'');
      name_state.last_name_range = EndRange(cookie);
      return true;
    }
    case 'b': {
      int cookie = GetStartCookie();
      WRITE("'block");
      const char *before_digits = m_read_ptr;
      if (TryParseNumber() != -1)
        Write(before_digits, m_read_ptr - before_digits);
      if (!Parse('_'))
        return false;
      Write('\'');
      name_state.last_name_range = EndRange(cookie);
      return true;
    }
    case 'l':
#ifdef DEBUG_FAILURES
      printf("*** Lambda type names unsupported\n");
#endif
      return false;
    }
#ifdef DEBUG_FAILURES
    printf("*** Unknown unnamed type %.3s\n", m_read_ptr - 2);
#endif
    return false;
  }

  // <ctor-dtor-name> ::= C1      # complete object constructor
  //                  ::= C2      # base object constructor
  //                  ::= C3      # complete object allocating constructor

  bool ParseCtor(NameState &name_state) {
    char next = *m_read_ptr;
    if (next == '1' || next == '2' || next == '3' || next == '5') {
      RewriteRange(name_state.last_name_range);
      name_state.has_no_return_type = true;
      ++m_read_ptr;
      return true;
    }
#ifdef DEBUG_FAILURES
    printf("*** Broken constructor\n");
#endif
    return false;
  }

  // <ctor-dtor-name> ::= D0      # deleting destructor
  //                  ::= D1      # complete object destructor
  //                  ::= D2      # base object destructor

  bool ParseDtor(NameState &name_state) {
    char next = *m_read_ptr;
    if (next == '0' || next == '1' || next == '2' || next == '5') {
      Write('~');
      RewriteRange(name_state.last_name_range);
      name_state.has_no_return_type = true;
      ++m_read_ptr;
      return true;
    }
#ifdef DEBUG_FAILURES
    printf("*** Broken destructor\n");
#endif
    return false;
  }

  // See TryParseOperator()

  bool ParseOperatorName(NameState &name_state) {
#ifdef DEBUG_FAILURES
    const char *operator_ptr = m_read_ptr;
#endif
    Operator parsed_operator = TryParseOperator();
    if (parsed_operator.name) {
      WRITE("operator");
      Write(parsed_operator.name);
      return true;
    }

    // Handle special operators
    switch (parsed_operator.kind) {
    case OperatorKind::Vendor:
      WRITE("operator ");
      return ParseSourceName();
    case OperatorKind::ConversionOperator:
      ResetTemplateArgs();
      name_state.has_no_return_type = true;
      WRITE("operator ");
      return ParseType();
    default:
#ifdef DEBUG_FAILURES
      printf("*** Unknown operator: %.2s\n", operator_ptr);
#endif
      return false;
    }
  }

  // <source-name> ::= <positive length number> <identifier>

  bool ParseSourceName() {
    int count = TryParseNumber();
    if (count == -1) {
#ifdef DEBUG_FAILURES
      printf("*** Malformed source name, missing length count\n");
#endif
      return false;
    }

    const char *next_m_read_ptr = m_read_ptr + count;
    if (next_m_read_ptr > m_read_end) {
#ifdef DEBUG_FAILURES
      printf("*** Malformed source name, premature termination\n");
#endif
      return false;
    }

    if (count >= 10 && strncmp(m_read_ptr, "_GLOBAL__N", 10) == 0)
      WRITE("(anonymous namespace)");
    else
      Write(m_read_ptr, count);

    m_read_ptr = next_m_read_ptr;
    return true;
  }

  // <unqualified-name> ::= <operator-name>
  //                    ::= <ctor-dtor-name>
  //                    ::= <source-name>
  //                    ::= <unnamed-type-name>

  bool ParseUnqualifiedName(NameState &name_state) {
    // Note that these are detected directly in ParseNestedName for
    // performance rather than switching on the same options twice
    char next = *m_read_ptr;
    switch (next) {
    case 'C':
      ++m_read_ptr;
      return ParseCtor(name_state);
    case 'D':
      ++m_read_ptr;
      return ParseDtor(name_state);
    case 'U':
      ++m_read_ptr;
      return ParseUnnamedTypeName(name_state);
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9': {
      int name_start_cookie = GetStartCookie();
      if (!ParseSourceName())
        return false;
      name_state.last_name_range = EndRange(name_start_cookie);
      return true;
    }
    default:
      return ParseOperatorName(name_state);
    };
  }

  // <unscoped-name> ::= <unqualified-name>
  //                 ::= St <unqualified-name>   # ::std::
  // extension       ::= StL<unqualified-name>

  bool ParseUnscopedName(NameState &name_state) {
    if (*m_read_ptr == 'S' && *(m_read_ptr + 1) == 't') {
      WriteStdPrefix();
      if (*(m_read_ptr += 2) == 'L')
        ++m_read_ptr;
    }
    return ParseUnqualifiedName(name_state);
  }

  bool ParseIntegerLiteral(const char *prefix, const char *suffix,
                           bool allow_negative) {
    if (prefix)
      Write(prefix);
    if (!ParseNumber(allow_negative))
      return false;
    if (suffix)
      Write(suffix);
    return Parse('E');
  }

  bool ParseBooleanLiteral() {
    switch (*m_read_ptr++) {
    case '0':
      WRITE("false");
      break;
    case '1':
      WRITE("true");
      break;
    default:
#ifdef DEBUG_FAILURES
      printf("*** Boolean literal not 0 or 1\n");
#endif
      return false;
    }
    return Parse('E');
  }

  // <expr-primary> ::= L <type> <value number> E                          #
  // integer literal
  //                ::= L <type> <value float> E                           #
  //                floating literal
  //                ::= L <string type> E                                  #
  //                string literal
  //                ::= L <nullptr type> E                                 #
  //                nullptr literal (i.e., "LDnE")
  //                ::= L <type> <real-part float> _ <imag-part float> E   #
  //                complex floating point literal (C 2000)
  //                ::= L <mangled-name> E                                 #
  //                external name

  bool ParseExpressionPrimary() {
    switch (*m_read_ptr++) {
    case 'b':
      return ParseBooleanLiteral();
    case 'x':
      return ParseIntegerLiteral(nullptr, "ll", true);
    case 'l':
      return ParseIntegerLiteral(nullptr, "l", true);
    case 'i':
      return ParseIntegerLiteral(nullptr, nullptr, true);
    case 'n':
      return ParseIntegerLiteral("(__int128)", nullptr, true);
    case 'j':
      return ParseIntegerLiteral(nullptr, "u", false);
    case 'm':
      return ParseIntegerLiteral(nullptr, "ul", false);
    case 'y':
      return ParseIntegerLiteral(nullptr, "ull", false);
    case 'o':
      return ParseIntegerLiteral("(unsigned __int128)", nullptr, false);
    case '_':
      if (*m_read_ptr++ == 'Z') {
        if (!ParseEncoding())
          return false;
        return Parse('E');
      }
      --m_read_ptr;
      LLVM_FALLTHROUGH;
    case 'w':
    case 'c':
    case 'a':
    case 'h':
    case 's':
    case 't':
    case 'f':
    case 'd':
    case 'e':
#ifdef DEBUG_FAILURES
      printf("*** Unsupported primary expression %.5s\n", m_read_ptr - 1);
#endif
      return false;
    case 'T':
// Invalid mangled name per
//   http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
#ifdef DEBUG_FAILURES
      printf("*** Invalid primary expr encoding\n");
#endif
      return false;
    default:
      --m_read_ptr;
      Write('(');
      if (!ParseType())
        return false;
      Write(')');
      if (!ParseNumber())
        return false;
      return Parse('E');
    }
  }

  // <unresolved-type> ::= <template-param>
  //                   ::= <decltype>
  //                   ::= <substitution>

  bool ParseUnresolvedType() {
    int type_start_cookie = GetStartCookie();
    switch (*m_read_ptr++) {
    case 'T':
      if (!ParseTemplateParam())
        return false;
      EndSubstitution(type_start_cookie);
      return true;
    case 'S': {
      if (*m_read_ptr != 't')
        return ParseSubstitution();

      ++m_read_ptr;
      WriteStdPrefix();
      NameState type_name = {};
      if (!ParseUnqualifiedName(type_name))
        return false;
      EndSubstitution(type_start_cookie);
      return true;
    }
    case 'D':
    default:
#ifdef DEBUG_FAILURES
      printf("*** Unsupported unqualified type: %3s\n", m_read_ptr - 1);
#endif
      return false;
    }
  }

  // <base-unresolved-name> ::= <simple-id>                                #
  // unresolved name
  //          extension     ::= <operator-name>                            #
  //          unresolved operator-function-id
  //          extension     ::= <operator-name> <template-args>            #
  //          unresolved operator template-id
  //                        ::= on <operator-name>                         #
  //                        unresolved operator-function-id
  //                        ::= on <operator-name> <template-args>         #
  //                        unresolved operator template-id
  //                        ::= dn <destructor-name>                       #
  //                        destructor or pseudo-destructor;
  //                                                                         #
  //                                                                         e.g.
  //                                                                         ~X
  //                                                                         or
  //                                                                         ~X<N-1>

  bool ParseBaseUnresolvedName() {
#ifdef DEBUG_FAILURES
    printf("*** Base unresolved name unsupported\n");
#endif
    return false;
  }

  // <unresolved-name>
  //  extension        ::= srN <unresolved-type> [<template-args>]
  //  <unresolved-qualifier-level>* E <base-unresolved-name>
  //                   ::= [gs] <base-unresolved-name>                     # x
  //                   or (with "gs") ::x
  //                   ::= [gs] sr <unresolved-qualifier-level>+ E
  //                   <base-unresolved-name>
  //                                                                       #
  //                                                                       A::x,
  //                                                                       N::y,
  //                                                                       A<T>::z;
  //                                                                       "gs"
  //                                                                       means
  //                                                                       leading
  //                                                                       "::"
  //                   ::= sr <unresolved-type> <base-unresolved-name>     #
  //                   T::x / decltype(p)::x
  //  extension        ::= sr <unresolved-type> <template-args>
  //  <base-unresolved-name>
  //                                                                       #
  //                                                                       T::N::x
  //                                                                       /decltype(p)::N::x
  //  (ignored)        ::= srN <unresolved-type>  <unresolved-qualifier-level>+
  //  E <base-unresolved-name>

  bool ParseUnresolvedName() {
#ifdef DEBUG_FAILURES
    printf("*** Unresolved names not supported\n");
#endif
    // TODO: grammar for all of this seems unclear...
    return false;

#if 0  // TODO
        if (*m_read_ptr == 'g' && *(m_read_ptr + 1) == 's')
        {
            m_read_ptr += 2;
            WriteNamespaceSeparator();
        }
#endif // TODO
  }

  // <expression> ::= <unary operator-name> <expression>
  //              ::= <binary operator-name> <expression> <expression>
  //              ::= <ternary operator-name> <expression> <expression>
  //              <expression>
  //              ::= cl <expression>+ E                                   #
  //              call
  //              ::= cv <type> <expression>                               #
  //              conversion with one argument
  //              ::= cv <type> _ <expression>* E                          #
  //              conversion with a different number of arguments
  //              ::= [gs] nw <expression>* _ <type> E                     # new
  //              (expr-list) type
  //              ::= [gs] nw <expression>* _ <type> <initializer>         # new
  //              (expr-list) type (init)
  //              ::= [gs] na <expression>* _ <type> E                     #
  //              new[] (expr-list) type
  //              ::= [gs] na <expression>* _ <type> <initializer>         #
  //              new[] (expr-list) type (init)
  //              ::= [gs] dl <expression>                                 #
  //              delete expression
  //              ::= [gs] da <expression>                                 #
  //              delete[] expression
  //              ::= pp_ <expression>                                     #
  //              prefix ++
  //              ::= mm_ <expression>                                     #
  //              prefix --
  //              ::= ti <type>                                            #
  //              typeid (type)
  //              ::= te <expression>                                      #
  //              typeid (expression)
  //              ::= dc <type> <expression>                               #
  //              dynamic_cast<type> (expression)
  //              ::= sc <type> <expression>                               #
  //              static_cast<type> (expression)
  //              ::= cc <type> <expression>                               #
  //              const_cast<type> (expression)
  //              ::= rc <type> <expression>                               #
  //              reinterpret_cast<type> (expression)
  //              ::= st <type>                                            #
  //              sizeof (a type)
  //              ::= sz <expression>                                      #
  //              sizeof (an expression)
  //              ::= at <type>                                            #
  //              alignof (a type)
  //              ::= az <expression>                                      #
  //              alignof (an expression)
  //              ::= nx <expression>                                      #
  //              noexcept (expression)
  //              ::= <template-param>
  //              ::= <function-param>
  //              ::= dt <expression> <unresolved-name>                    #
  //              expr.name
  //              ::= pt <expression> <unresolved-name>                    #
  //              expr->name
  //              ::= ds <expression> <expression>                         #
  //              expr.*expr
  //              ::= sZ <template-param>                                  #
  //              size of a parameter pack
  //              ::= sZ <function-param>                                  #
  //              size of a function parameter pack
  //              ::= sp <expression>                                      #
  //              pack expansion
  //              ::= tw <expression>                                      #
  //              throw expression
  //              ::= tr                                                   #
  //              throw with no operand (rethrow)
  //              ::= <unresolved-name>                                    #
  //              f(p), N::f(p), ::f(p),
  //                                                                       #
  //                                                                       freestanding
  //                                                                       dependent
  //                                                                       name
  //                                                                       (e.g.,
  //                                                                       T::x),
  //                                                                       #
  //                                                                       objectless
  //                                                                       nonstatic
  //                                                                       member
  //                                                                       reference
  //              ::= <expr-primary>

  bool ParseExpression() {
    Operator expression_operator = TryParseOperator();
    switch (expression_operator.kind) {
    case OperatorKind::Unary:
      Write(expression_operator.name);
      Write('(');
      if (!ParseExpression())
        return false;
      Write(')');
      return true;
    case OperatorKind::Binary:
      if (!ParseExpression())
        return false;
      Write(expression_operator.name);
      return ParseExpression();
    case OperatorKind::Ternary:
      if (!ParseExpression())
        return false;
      Write('?');
      if (!ParseExpression())
        return false;
      Write(':');
      return ParseExpression();
    case OperatorKind::NoMatch:
      break;
    case OperatorKind::Other:
    default:
#ifdef DEBUG_FAILURES
      printf("*** Unsupported operator: %s\n", expression_operator.name);
#endif
      return false;
    }

    switch (*m_read_ptr++) {
    case 'T':
      return ParseTemplateParam();
    case 'L':
      return ParseExpressionPrimary();
    case 's':
      if (*m_read_ptr++ == 'r')
        return ParseUnresolvedName();
      --m_read_ptr;
      LLVM_FALLTHROUGH;
    default:
      return ParseExpressionPrimary();
    }
  }

  // <template-arg> ::= <type>                                             #
  // type or template
  //                ::= X <expression> E                                   #
  //                expression
  //                ::= <expr-primary>                                     #
  //                simple expressions
  //                ::= J <template-arg>* E                                #
  //                argument pack
  //                ::= LZ <encoding> E                                    #
  //                extension

  bool ParseTemplateArg() {
    switch (*m_read_ptr) {
    case 'J':
#ifdef DEBUG_FAILURES
      printf("*** Template argument packs unsupported\n");
#endif
      return false;
    case 'X':
      ++m_read_ptr;
      if (!ParseExpression())
        return false;
      return Parse('E');
    case 'L':
      ++m_read_ptr;
      return ParseExpressionPrimary();
    default:
      return ParseType();
    }
  }

  // <template-args> ::= I <template-arg>* E
  //     extension, the abi says <template-arg>+

  bool ParseTemplateArgs(bool record_template_args = false) {
    if (record_template_args)
      ResetTemplateArgs();

    bool first_arg = true;
    while (*m_read_ptr != 'E') {
      if (first_arg)
        first_arg = false;
      else
        WriteCommaSpace();

      int template_start_cookie = GetStartCookie();
      if (!ParseTemplateArg())
        return false;
      if (record_template_args)
        EndTemplateArg(template_start_cookie);
    }
    ++m_read_ptr;
    return true;
  }

  // <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix>
  // <unqualified-name> E
  //               ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
  //               <template-args> E
  //
  // <prefix> ::= <prefix> <unqualified-name>
  //          ::= <template-prefix> <template-args>
  //          ::= <template-param>
  //          ::= <decltype>
  //          ::= # empty
  //          ::= <substitution>
  //          ::= <prefix> <data-member-prefix>
  //  extension ::= L
  //
  // <template-prefix> ::= <prefix> <template unqualified-name>
  //                   ::= <template-param>
  //                   ::= <substitution>
  //
  // <unqualified-name> ::= <operator-name>
  //                    ::= <ctor-dtor-name>
  //                    ::= <source-name>
  //                    ::= <unnamed-type-name>

  bool ParseNestedName(NameState &name_state,
                       bool parse_discriminator = false) {
    int qualifiers = TryParseQualifiers(true, true);
    bool first_part = true;
    bool suppress_substitution = true;
    int name_start_cookie = GetStartCookie();
    while (true) {
      char next = *m_read_ptr;
      if (next == 'E') {
        ++m_read_ptr;
        break;
      }

      // Record a substitution candidate for all prefixes, but not the full name
      if (suppress_substitution)
        suppress_substitution = false;
      else
        EndSubstitution(name_start_cookie);

      if (next == 'I') {
        ++m_read_ptr;
        name_state.is_last_generic = true;
        WriteTemplateStart();
        if (!ParseTemplateArgs(name_state.parse_function_params))
          return false;
        WriteTemplateEnd();
        continue;
      }

      if (first_part)
        first_part = false;
      else
        WriteNamespaceSeparator();

      name_state.is_last_generic = false;
      switch (next) {
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9': {
        int name_start_cookie = GetStartCookie();
        if (!ParseSourceName())
          return false;
        name_state.last_name_range = EndRange(name_start_cookie);
        continue;
      }
      case 'S':
        if (*++m_read_ptr == 't') {
          WriteStdPrefix();
          ++m_read_ptr;
          if (!ParseUnqualifiedName(name_state))
            return false;
        } else {
          if (!ParseSubstitution())
            return false;
          suppress_substitution = true;
        }
        continue;
      case 'T':
        ++m_read_ptr;
        if (!ParseTemplateParam())
          return false;
        continue;
      case 'C':
        ++m_read_ptr;
        if (!ParseCtor(name_state))
          return false;
        continue;
      case 'D': {
        switch (*(m_read_ptr + 1)) {
        case 't':
        case 'T':
#ifdef DEBUG_FAILURES
          printf("*** Decltype unsupported\n");
#endif
          return false;
        }
        ++m_read_ptr;
        if (!ParseDtor(name_state))
          return false;
        continue;
      }
      case 'U':
        ++m_read_ptr;
        if (!ParseUnnamedTypeName(name_state))
          return false;
        continue;
      case 'L':
        ++m_read_ptr;
        if (!ParseUnqualifiedName(name_state))
          return false;
        continue;
      default:
        if (!ParseOperatorName(name_state))
          return false;
      }
    }

    if (parse_discriminator)
      TryParseDiscriminator();
    if (name_state.parse_function_params &&
        !ParseFunctionArgs(name_state, name_start_cookie)) {
      return false;
    }
    if (qualifiers)
      WriteQualifiers(qualifiers);
    return true;
  }

  // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
  //              := Z <function encoding> E s [<discriminator>]
  //              := Z <function encoding> Ed [ <parameter number> ] _ <entity
  //              name>

  bool ParseLocalName(bool parse_function_params) {
    if (!ParseEncoding())
      return false;
    if (!Parse('E'))
      return false;

    switch (*m_read_ptr) {
    case 's':
      ++m_read_ptr;
      TryParseDiscriminator(); // Optional and ignored
      WRITE("::string literal");
      break;
    case 'd':
      ++m_read_ptr;
      TryParseNumber(); // Optional and ignored
      if (!Parse('_'))
        return false;
      WriteNamespaceSeparator();
      if (!ParseName())
        return false;
      break;
    default:
      WriteNamespaceSeparator();
      if (!ParseName(parse_function_params, true))
        return false;
      TryParseDiscriminator(); // Optional and ignored
    }
    return true;
  }

  // <name> ::= <nested-name>
  //        ::= <local-name>
  //        ::= <unscoped-template-name> <template-args>
  //        ::= <unscoped-name>

  // <unscoped-template-name> ::= <unscoped-name>
  //                          ::= <substitution>

  bool ParseName(bool parse_function_params = false,
                 bool parse_discriminator = false) {
    NameState name_state = {parse_function_params, false, false, {0, 0}};
    int name_start_cookie = GetStartCookie();

    switch (*m_read_ptr) {
    case 'N':
      ++m_read_ptr;
      return ParseNestedName(name_state, parse_discriminator);
    case 'Z': {
      ++m_read_ptr;
      if (!ParseLocalName(parse_function_params))
        return false;
      break;
    }
    case 'L':
      ++m_read_ptr;
      LLVM_FALLTHROUGH;
    default: {
      if (!ParseUnscopedName(name_state))
        return false;

      if (*m_read_ptr == 'I') {
        EndSubstitution(name_start_cookie);

        ++m_read_ptr;
        name_state.is_last_generic = true;
        WriteTemplateStart();
        if (!ParseTemplateArgs(parse_function_params))
          return false;
        WriteTemplateEnd();
      }
      break;
    }
    }
    if (parse_discriminator)
      TryParseDiscriminator();
    if (parse_function_params &&
        !ParseFunctionArgs(name_state, name_start_cookie)) {
      return false;
    }
    return true;
  }

  // <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

  bool ParseCallOffset() {
    switch (*m_read_ptr++) {
    case 'h':
      if (*m_read_ptr == 'n')
        ++m_read_ptr;
      if (TryParseNumber() == -1 || *m_read_ptr++ != '_')
        break;
      return true;
    case 'v':
      if (*m_read_ptr == 'n')
        ++m_read_ptr;
      if (TryParseNumber() == -1 || *m_read_ptr++ != '_')
        break;
      if (*m_read_ptr == 'n')
        ++m_read_ptr;
      if (TryParseNumber() == -1 || *m_read_ptr++ != '_')
        break;
      return true;
    }
#ifdef DEBUG_FAILURES
    printf("*** Malformed call offset\n");
#endif
    return false;
  }

  // <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)
  //                ::= 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
  //                ::= T <call-offset> <base encoding>
  //                    # base is the nominal target function of thunk
  //      extension ::= TC <first type> <number> _ <second type> # construction
  //      vtable for second-in-first

  bool ParseSpecialNameT() {
    switch (*m_read_ptr++) {
    case 'V':
      WRITE("vtable for ");
      return ParseType();
    case 'T':
      WRITE("VTT for ");
      return ParseType();
    case 'I':
      WRITE("typeinfo for ");
      return ParseType();
    case 'S':
      WRITE("typeinfo name for ");
      return ParseType();
    case 'c':
    case 'C':
#ifdef DEBUG_FAILURES
      printf("*** Unsupported thunk or construction vtable name: %.3s\n",
             m_read_ptr - 1);
#endif
      return false;
    default:
      if (*--m_read_ptr == 'v') {
        WRITE("virtual thunk to ");
      } else {
        WRITE("non-virtual thunk to ");
      }
      if (!ParseCallOffset())
        return false;
      return ParseEncoding();
    }
  }

  // <special-name> ::= GV <object name> # Guard variable for one-time
  // initialization
  //                                     # No <type>
  //      extension ::= GR <object name> # reference temporary for object

  bool ParseSpecialNameG() {
    switch (*m_read_ptr++) {
    case 'V':
      WRITE("guard variable for ");
      if (!ParseName(true))
        return false;
      break;
    case 'R':
      WRITE("reference temporary for ");
      if (!ParseName(true))
        return false;
      break;
    default:
#ifdef DEBUG_FAILURES
      printf("*** Unknown G encoding\n");
#endif
      return false;
    }
    return true;
  }

  // <bare-function-type> ::= <signature type>+        # types are possible
  // return type, then parameter types

  bool ParseFunctionArgs(NameState &name_state, int return_insert_cookie) {
    char next = *m_read_ptr;
    if (next == 'E' || next == '\0' || next == '.')
      return true;

    // Clang has a bad habit of making unique manglings by just sticking numbers
    // on the end of a symbol,
    // which is ambiguous with malformed source name manglings
    const char *before_clang_uniquing_test = m_read_ptr;
    if (TryParseNumber()) {
      if (*m_read_ptr == '\0')
        return true;
      m_read_ptr = before_clang_uniquing_test;
    }

    if (name_state.is_last_generic && !name_state.has_no_return_type) {
      int return_type_start_cookie = GetStartCookie();
      if (!ParseType())
        return false;
      Write(' ');
      ReorderRange(EndRange(return_type_start_cookie), return_insert_cookie);
    }

    Write('(');
    bool first_param = true;
    while (true) {
      switch (*m_read_ptr) {
      case '\0':
      case 'E':
      case '.':
        break;
      case 'v':
        ++m_read_ptr;
        continue;
      case '_':
        // Not a formal part of the mangling specification, but clang emits
        // suffixes starting with _block_invoke
        if (strncmp(m_read_ptr, "_block_invoke", 13) == 0) {
          m_read_ptr += strlen(m_read_ptr);
          break;
        }
        LLVM_FALLTHROUGH;
      default:
        if (first_param)
          first_param = false;
        else
          WriteCommaSpace();

        if (!ParseType())
          return false;
        continue;
      }
      break;
    }
    Write(')');
    return true;
  }

  // <encoding> ::= <function name> <bare-function-type>
  //            ::= <data name>
  //            ::= <special-name>

  bool ParseEncoding() {
    switch (*m_read_ptr) {
    case 'T':
      ++m_read_ptr;
      if (!ParseSpecialNameT())
        return false;
      break;
    case 'G':
      ++m_read_ptr;
      if (!ParseSpecialNameG())
        return false;
      break;
    default:
      if (!ParseName(true))
        return false;
      break;
    }
    return true;
  }

  bool ParseMangling(const char *mangled_name, long mangled_name_length = 0) {
    if (!mangled_name_length)
      mangled_name_length = strlen(mangled_name);
    m_read_end = mangled_name + mangled_name_length;
    m_read_ptr = mangled_name;
    m_write_ptr = m_buffer;
    m_next_substitute_index = 0;
    m_next_template_arg_index = m_rewrite_ranges_size - 1;

    if (*m_read_ptr++ != '_' || *m_read_ptr++ != 'Z') {
#ifdef DEBUG_FAILURES
      printf("*** Missing _Z prefix\n");
#endif
      return false;
    }
    if (!ParseEncoding())
      return false;
    switch (*m_read_ptr) {
    case '.':
      Write(' ');
      Write('(');
      Write(m_read_ptr, m_read_end - m_read_ptr);
      Write(')');
      LLVM_FALLTHROUGH;
    case '\0':
      return true;
    default:
#ifdef DEBUG_FAILURES
      printf("*** Unparsed mangled content\n");
#endif
      return false;
    }
  }

private:
  // External scratch storage used during demanglings

  char *m_buffer;
  const char *m_buffer_end;
  BufferRange *m_rewrite_ranges;
  int m_rewrite_ranges_size;
  bool m_owns_buffer;
  bool m_owns_m_rewrite_ranges;

  // Internal state used during demangling

  const char *m_read_ptr;
  const char *m_read_end;
  char *m_write_ptr;
  int m_next_template_arg_index;
  int m_next_substitute_index;
  std::function<void(const char *s)> m_builtins_hook;
};

} // Anonymous namespace

// Public entry points referenced from Mangled.cpp
namespace lldb_private {
char *FastDemangle(const char *mangled_name) {
  char buffer[16384];
  SymbolDemangler demangler(buffer, sizeof(buffer));
  return demangler.GetDemangledCopy(mangled_name);
}

char *FastDemangle(const char *mangled_name, size_t mangled_name_length,
                   std::function<void(const char *s)> builtins_hook) {
  char buffer[16384];
  SymbolDemangler demangler(buffer, sizeof(buffer), builtins_hook);
  return demangler.GetDemangledCopy(mangled_name, mangled_name_length);
}
} // lldb_private namespace