llvm.org GIT mirror llvm / ec44cfd
Add an c++ itanium demangler to llvm. This adds a copy of the demangler in libcxxabi. The code also has no dependencies on anything else in LLVM. To enforce that I added it as another library. That way a BUILD_SHARED_LIBS will fail if anyone adds an use of StringRef for example. The no llvm dependency combined with the fact that this has to build on linux, OS X and Windows required a few changes to the code. In particular: No constexpr. No alignas On OS X at least this library has only one global symbol: __ZN4llvm16itanium_demangleEPKcPcPmPi My current plan is: Commit something like this Change lld to use it Change lldb to use it as the fallback Add a few #ifdefs so that exactly the same file can be used in libcxxabi to export abi::__cxa_demangle. Once the fast demangler in lldb can handle any names this implementation can be replaced with it and we will have the one true demangler. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280732 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 3 years ago
20 changed file(s) with 4539 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
8080 check_include_file(mach/mach.h HAVE_MACH_MACH_H)
8181 check_include_file(mach-o/dyld.h HAVE_MACH_O_DYLD_H)
8282 check_include_file(histedit.h HAVE_HISTEDIT_H)
83
84 # size_t must be defined before including cxxabi.h on FreeBSD 10.0.
85 check_cxx_source_compiles("
86 #include
87 #include
88 int main() { return 0; }
89 " HAVE_CXXABI_H)
9083
9184 # library checks
9285 if( NOT PURE_WINDOWS )
2626
2727 /* Define to 1 if you have the `closedir' function. */
2828 #cmakedefine HAVE_CLOSEDIR ${HAVE_CLOSEDIR}
29
30 /* Define to 1 if you have the header file. */
31 #cmakedefine HAVE_CXXABI_H ${HAVE_CXXABI_H}
3229
3330 /* Define to 1 if you have the header file. */
3431 #undef HAVE_CRASHREPORTERCLIENT_H
0 //===--- Demangle.h ---------------------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include
10
11 namespace llvm {
12 /// This is a llvm local version of __cxa_demangle. Other than the name and
13 /// being in the llvm namespace it is identical.
14 ///
15 /// The mangled_name is demangled into buf and returned. If the buffer is not
16 /// large enough, realloc is used to expand it.
17 ///
18 /// The *status will be set to
19 /// unknown_error: -4
20 /// invalid_args: -3
21 /// invalid_mangled_name: -2
22 /// memory_alloc_failure: -1
23 /// success: 0
24
25 char *itaniumDemangle(const char *mangled_name, char *buf, size_t *n,
26 int *status);
27 }
0 # `Support' and `TableGen' libraries are added on the top-level CMakeLists.txt
11
2 add_subdirectory(Demangle)
23 add_subdirectory(IR)
34 add_subdirectory(IRReader)
45 add_subdirectory(CodeGen)
0 add_llvm_library(LLVMDemangle
1 ItaniumDemangle.cpp
2 )
0 //===- ItaniumDemangle.cpp ------------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is dual licensed under the MIT and the University of Illinois Open
5 // Source Licenses. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include
10
11 // This file exports a single function: llvm::itanium_demangle.
12 // It also has no dependencies on the rest of llvm. It is implemented this way
13 // so that it can be easily reused in libcxxabi.
14
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22
23 #ifdef _MSC_VER
24 // snprintf is implemented in VS 2015
25 #if _MSC_VER < 1900
26 #define snprintf _snprintf_s
27 #endif
28 #endif
29
30 enum {
31 unknown_error = -4,
32 invalid_args = -3,
33 invalid_mangled_name,
34 memory_alloc_failure,
35 success
36 };
37
38 template
39 static const char *parse_type(const char *first, const char *last, C &db);
40 template
41 static const char *parse_encoding(const char *first, const char *last, C &db);
42 template
43 static const char *parse_name(const char *first, const char *last, C &db,
44 bool *ends_with_template_args = 0);
45 template
46 static const char *parse_expression(const char *first, const char *last, C &db);
47 template
48 static const char *parse_template_args(const char *first, const char *last,
49 C &db);
50 template
51 static const char *parse_operator_name(const char *first, const char *last,
52 C &db);
53 template
54 static const char *parse_unqualified_name(const char *first, const char *last,
55 C &db);
56 template
57 static const char *parse_decltype(const char *first, const char *last, C &db);
58
59 // ::= [n]
60
61 static const char *parse_number(const char *first, const char *last) {
62 if (first != last) {
63 const char *t = first;
64 if (*t == 'n')
65 ++t;
66 if (t != last) {
67 if (*t == '0') {
68 first = t + 1;
69 } else if ('1' <= *t && *t <= '9') {
70 first = t + 1;
71 while (first != last && std::isdigit(*first))
72 ++first;
73 }
74 }
75 }
76 return first;
77 }
78
79 namespace {
80 template struct float_data;
81
82 template <> struct float_data {
83 static const size_t mangled_size = 8;
84 static const size_t max_demangled_size = 24;
85 static const char *spec;
86 };
87 const char *float_data::spec = "%af";
88
89 template <> struct float_data {
90 static const size_t mangled_size = 16;
91 static const size_t max_demangled_size = 32;
92 static const char *spec;
93 };
94
95 const char *float_data::spec = "%a";
96
97 template <> struct float_data {
98 #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
99 defined(__wasm__)
100 static const size_t mangled_size = 32;
101 #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
102 static const size_t mangled_size = 16;
103 #else
104 static const size_t mangled_size =
105 20; // May need to be adjusted to 16 or 24 on other platforms
106 #endif
107 static const size_t max_demangled_size = 40;
108 static const char *spec;
109 };
110
111 const char *float_data::spec = "%LaL";
112 }
113
114 template
115 static const char *parse_floating_number(const char *first, const char *last,
116 C &db) {
117 const size_t N = float_data::mangled_size;
118 if (static_cast(last - first) > N) {
119 last = first + N;
120 union {
121 Float value;
122 char buf[sizeof(Float)];
123 };
124 const char *t = first;
125 char *e = buf;
126 for (; t != last; ++t, ++e) {
127 if (!isxdigit(*t))
128 return first;
129 unsigned d1 = isdigit(*t) ? static_cast(*t - '0')
130 : static_cast(*t - 'a' + 10);
131 ++t;
132 unsigned d0 = isdigit(*t) ? static_cast(*t - '0')
133 : static_cast(*t - 'a' + 10);
134 *e = static_cast((d1 << 4) + d0);
135 }
136 if (*t == 'E') {
137 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
138 std::reverse(buf, e);
139 #endif
140 char num[float_data::max_demangled_size] = {0};
141 int n = snprintf(num, sizeof(num), float_data::spec, value);
142 if (static_cast(n) >= sizeof(num))
143 return first;
144 db.names.push_back(typename C::String(num, static_cast(n)));
145 first = t + 1;
146 }
147 }
148 return first;
149 }
150
151 // ::=
152
153 template
154 static const char *parse_source_name(const char *first, const char *last,
155 C &db) {
156 if (first != last) {
157 char c = *first;
158 if (isdigit(c) && first + 1 != last) {
159 const char *t = first + 1;
160 size_t n = static_cast(c - '0');
161 for (c = *t; isdigit(c); c = *t) {
162 n = n * 10 + static_cast(c - '0');
163 if (++t == last)
164 return first;
165 }
166 if (static_cast(last - t) >= n) {
167 typename C::String r(t, n);
168 if (r.substr(0, 10) == "_GLOBAL__N")
169 db.names.push_back("(anonymous namespace)");
170 else
171 db.names.push_back(std::move(r));
172 first = t + n;
173 }
174 }
175 }
176 return first;
177 }
178
179 // ::= S _
180 // ::= S_
181 // ::= Sa # ::std::allocator
182 // ::= Sb # ::std::basic_string
183 // ::= Ss # ::std::basic_string < char,
184 // ::std::char_traits,
185 // ::std::allocator >
186 // ::= Si # ::std::basic_istream >
187 // ::= So # ::std::basic_ostream >
188 // ::= Sd # ::std::basic_iostream >
189
190 template
191 static const char *parse_substitution(const char *first, const char *last,
192 C &db) {
193 if (last - first >= 2) {
194 if (*first == 'S') {
195 switch (first[1]) {
196 case 'a':
197 db.names.push_back("std::allocator");
198 first += 2;
199 break;
200 case 'b':
201 db.names.push_back("std::basic_string");
202 first += 2;
203 break;
204 case 's':
205 db.names.push_back("std::string");
206 first += 2;
207 break;
208 case 'i':
209 db.names.push_back("std::istream");
210 first += 2;
211 break;
212 case 'o':
213 db.names.push_back("std::ostream");
214 first += 2;
215 break;
216 case 'd':
217 db.names.push_back("std::iostream");
218 first += 2;
219 break;
220 case '_':
221 if (!db.subs.empty()) {
222 for (const auto &n : db.subs.front())
223 db.names.push_back(n);
224 first += 2;
225 }
226 break;
227 default:
228 if (std::isdigit(first[1]) || std::isupper(first[1])) {
229 size_t sub = 0;
230 const char *t = first + 1;
231 if (std::isdigit(*t))
232 sub = static_cast(*t - '0');
233 else
234 sub = static_cast(*t - 'A') + 10;
235 for (++t; t != last && (std::isdigit(*t) || std::isupper(*t)); ++t) {
236 sub *= 36;
237 if (std::isdigit(*t))
238 sub += static_cast(*t - '0');
239 else
240 sub += static_cast(*t - 'A') + 10;
241 }
242 if (t == last || *t != '_')
243 return first;
244 ++sub;
245 if (sub < db.subs.size()) {
246 for (const auto &n : db.subs[sub])
247 db.names.push_back(n);
248 first = t + 1;
249 }
250 }
251 break;
252 }
253 }
254 }
255 return first;
256 }
257
258 // ::= v # void
259 // ::= w # wchar_t
260 // ::= b # bool
261 // ::= c # char
262 // ::= a # signed char
263 // ::= h # unsigned char
264 // ::= s # short
265 // ::= t # unsigned short
266 // ::= i # int
267 // ::= j # unsigned int
268 // ::= l # long
269 // ::= m # unsigned long
270 // ::= x # long long, __int64
271 // ::= y # unsigned long long, __int64
272 // ::= n # __int128
273 // ::= o # unsigned __int128
274 // ::= f # float
275 // ::= d # double
276 // ::= e # long double, __float80
277 // ::= g # __float128
278 // ::= z # ellipsis
279 // ::= Dd # IEEE 754r decimal floating point (64 bits)
280 // ::= De # IEEE 754r decimal floating point (128 bits)
281 // ::= Df # IEEE 754r decimal floating point (32 bits)
282 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
283 // ::= Di # char32_t
284 // ::= Ds # char16_t
285 // ::= Da # auto (in dependent new-expressions)
286 // ::= Dc # decltype(auto)
287 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
288 // ::= u # vendor extended type
289
290 template
291 static const char *parse_builtin_type(const char *first, const char *last,
292 C &db) {
293 if (first != last) {
294 switch (*first) {
295 case 'v':
296 db.names.push_back("void");
297 ++first;
298 break;
299 case 'w':
300 db.names.push_back("wchar_t");
301 ++first;
302 break;
303 case 'b':
304 db.names.push_back("bool");
305 ++first;
306 break;
307 case 'c':
308 db.names.push_back("char");
309 ++first;
310 break;
311 case 'a':
312 db.names.push_back("signed char");
313 ++first;
314 break;
315 case 'h':
316 db.names.push_back("unsigned char");
317 ++first;
318 break;
319 case 's':
320 db.names.push_back("short");
321 ++first;
322 break;
323 case 't':
324 db.names.push_back("unsigned short");
325 ++first;
326 break;
327 case 'i':
328 db.names.push_back("int");
329 ++first;
330 break;
331 case 'j':
332 db.names.push_back("unsigned int");
333 ++first;
334 break;
335 case 'l':
336 db.names.push_back("long");
337 ++first;
338 break;
339 case 'm':
340 db.names.push_back("unsigned long");
341 ++first;
342 break;
343 case 'x':
344 db.names.push_back("long long");
345 ++first;
346 break;
347 case 'y':
348 db.names.push_back("unsigned long long");
349 ++first;
350 break;
351 case 'n':
352 db.names.push_back("__int128");
353 ++first;
354 break;
355 case 'o':
356 db.names.push_back("unsigned __int128");
357 ++first;
358 break;
359 case 'f':
360 db.names.push_back("float");
361 ++first;
362 break;
363 case 'd':
364 db.names.push_back("double");
365 ++first;
366 break;
367 case 'e':
368 db.names.push_back("long double");
369 ++first;
370 break;
371 case 'g':
372 db.names.push_back("__float128");
373 ++first;
374 break;
375 case 'z':
376 db.names.push_back("...");
377 ++first;
378 break;
379 case 'u': {
380 const char *t = parse_source_name(first + 1, last, db);
381 if (t != first + 1)
382 first = t;
383 } break;
384 case 'D':
385 if (first + 1 != last) {
386 switch (first[1]) {
387 case 'd':
388 db.names.push_back("decimal64");
389 first += 2;
390 break;
391 case 'e':
392 db.names.push_back("decimal128");
393 first += 2;
394 break;
395 case 'f':
396 db.names.push_back("decimal32");
397 first += 2;
398 break;
399 case 'h':
400 db.names.push_back("decimal16");
401 first += 2;
402 break;
403 case 'i':
404 db.names.push_back("char32_t");
405 first += 2;
406 break;
407 case 's':
408 db.names.push_back("char16_t");
409 first += 2;
410 break;
411 case 'a':
412 db.names.push_back("auto");
413 first += 2;
414 break;
415 case 'c':
416 db.names.push_back("decltype(auto)");
417 first += 2;
418 break;
419 case 'n':
420 db.names.push_back("std::nullptr_t");
421 first += 2;
422 break;
423 }
424 }
425 break;
426 }
427 }
428 return first;
429 }
430
431 // ::= [r] [V] [K]
432
433 static const char *parse_cv_qualifiers(const char *first, const char *last,
434 unsigned &cv) {
435 cv = 0;
436 if (first != last) {
437 if (*first == 'r') {
438 cv |= 4;
439 ++first;
440 }
441 if (*first == 'V') {
442 cv |= 2;
443 ++first;
444 }
445 if (*first == 'K') {
446 cv |= 1;
447 ++first;
448 }
449 }
450 return first;
451 }
452
453 // ::= T_ # first template parameter
454 // ::= T _
455
456 template
457 static const char *parse_template_param(const char *first, const char *last,
458 C &db) {
459 if (last - first >= 2) {
460 if (*first == 'T') {
461 if (first[1] == '_') {
462 if (db.template_param.empty())
463 return first;
464 if (!db.template_param.back().empty()) {
465 for (auto &t : db.template_param.back().front())
466 db.names.push_back(t);
467 first += 2;
468 } else {
469 db.names.push_back("T_");
470 first += 2;
471 db.fix_forward_references = true;
472 }
473 } else if (isdigit(first[1])) {
474 const char *t = first + 1;
475 size_t sub = static_cast(*t - '0');
476 for (++t; t != last && isdigit(*t); ++t) {
477 sub *= 10;
478 sub += static_cast(*t - '0');
479 }
480 if (t == last || *t != '_' || db.template_param.empty())
481 return first;
482 ++sub;
483 if (sub < db.template_param.back().size()) {
484 for (auto &temp : db.template_param.back()[sub])
485 db.names.push_back(temp);
486 first = t + 1;
487 } else {
488 db.names.push_back(typename C::String(first, t + 1));
489 first = t + 1;
490 db.fix_forward_references = true;
491 }
492 }
493 }
494 }
495 return first;
496 }
497
498 // cc # const_cast
499 // (expression)
500
501 template
502 static const char *parse_const_cast_expr(const char *first, const char *last,
503 C &db) {
504 if (last - first >= 3 && first[0] == 'c' && first[1] == 'c') {
505 const char *t = parse_type(first + 2, last, db);
506 if (t != first + 2) {
507 const char *t1 = parse_expression(t, last, db);
508 if (t1 != t) {
509 if (db.names.size() < 2)
510 return first;
511 auto expr = db.names.back().move_full();
512 db.names.pop_back();
513 if (db.names.empty())
514 return first;
515 db.names.back() =
516 "const_cast<" + db.names.back().move_full() + ">(" + expr + ")";
517 first = t1;
518 }
519 }
520 }
521 return first;
522 }
523
524 // dc # dynamic_cast
525 // (expression)
526
527 template
528 static const char *parse_dynamic_cast_expr(const char *first, const char *last,
529 C &db) {
530 if (last - first >= 3 && first[0] == 'd' && first[1] == 'c') {
531 const char *t = parse_type(first + 2, last, db);
532 if (t != first + 2) {
533 const char *t1 = parse_expression(t, last, db);
534 if (t1 != t) {
535 if (db.names.size() < 2)
536 return first;
537 auto expr = db.names.back().move_full();
538 db.names.pop_back();
539 if (db.names.empty())
540 return first;
541 db.names.back() =
542 "dynamic_cast<" + db.names.back().move_full() + ">(" + expr + ")";
543 first = t1;
544 }
545 }
546 }
547 return first;
548 }
549
550 // rc # reinterpret_cast
551 // (expression)
552
553 template
554 static const char *parse_reinterpret_cast_expr(const char *first,
555 const char *last, C &db) {
556 if (last - first >= 3 && first[0] == 'r' && first[1] == 'c') {
557 const char *t = parse_type(first + 2, last, db);
558 if (t != first + 2) {
559 const char *t1 = parse_expression(t, last, db);
560 if (t1 != t) {
561 if (db.names.size() < 2)
562 return first;
563 auto expr = db.names.back().move_full();
564 db.names.pop_back();
565 if (db.names.empty())
566 return first;
567 db.names.back() = "reinterpret_cast<" + db.names.back().move_full() +
568 ">(" + expr + ")";
569 first = t1;
570 }
571 }
572 }
573 return first;
574 }
575
576 // sc # static_cast
577 // (expression)
578
579 template
580 static const char *parse_static_cast_expr(const char *first, const char *last,
581 C &db) {
582 if (last - first >= 3 && first[0] == 's' && first[1] == 'c') {
583 const char *t = parse_type(first + 2, last, db);
584 if (t != first + 2) {
585 const char *t1 = parse_expression(t, last, db);
586 if (t1 != t) {
587 if (db.names.size() < 2)
588 return first;
589 auto expr = db.names.back().move_full();
590 db.names.pop_back();
591 db.names.back() =
592 "static_cast<" + db.names.back().move_full() + ">(" + expr + ")";
593 first = t1;
594 }
595 }
596 }
597 return first;
598 }
599
600 // sp # pack expansion
601
602 template
603 static const char *parse_pack_expansion(const char *first, const char *last,
604 C &db) {
605 if (last - first >= 3 && first[0] == 's' && first[1] == 'p') {
606 const char *t = parse_expression(first + 2, last, db);
607 if (t != first + 2)
608 first = t;
609 }
610 return first;
611 }
612
613 // st # sizeof (a type)
614
615 template
616 static const char *parse_sizeof_type_expr(const char *first, const char *last,
617 C &db) {
618 if (last - first >= 3 && first[0] == 's' && first[1] == 't') {
619 const char *t = parse_type(first + 2, last, db);
620 if (t != first + 2) {
621 if (db.names.empty())
622 return first;
623 db.names.back() = "sizeof (" + db.names.back().move_full() + ")";
624 first = t;
625 }
626 }
627 return first;
628 }
629
630 // sz # sizeof (a expression)
631
632 template
633 static const char *parse_sizeof_expr_expr(const char *first, const char *last,
634 C &db) {
635 if (last - first >= 3 && first[0] == 's' && first[1] == 'z') {
636 const char *t = parse_expression(first + 2, last, db);
637 if (t != first + 2) {
638 if (db.names.empty())
639 return first;
640 db.names.back() = "sizeof (" + db.names.back().move_full() + ")";
641 first = t;
642 }
643 }
644 return first;
645 }
646
647 // sZ # size of a parameter
648 // pack
649
650 template
651 static const char *parse_sizeof_param_pack_expr(const char *first,
652 const char *last, C &db) {
653 if (last - first >= 3 && first[0] == 's' && first[1] == 'Z' &&
654 first[2] == 'T') {
655 size_t k0 = db.names.size();
656 const char *t = parse_template_param(first + 2, last, db);
657 size_t k1 = db.names.size();
658 if (t != first + 2) {
659 typename C::String tmp("sizeof...(");
660 size_t k = k0;
661 if (k != k1) {
662 tmp += db.names[k].move_full();
663 for (++k; k != k1; ++k)
664 tmp += ", " + db.names[k].move_full();
665 }
666 tmp += ")";
667 for (; k1 != k0; --k1)
668 db.names.pop_back();
669 db.names.push_back(std::move(tmp));
670 first = t;
671 }
672 }
673 return first;
674 }
675
676 // ::= fp _ # L == 0, first parameter
677 // ::= fp
678 // number> _ # L == 0, second and later parameters
679 // ::= fL p
680 // _ # L > 0, first parameter
681 // ::= fL p
682 // _ # L > 0, second and
683 // later parameters
684
685 template
686 static const char *parse_function_param(const char *first, const char *last,
687 C &db) {
688 if (last - first >= 3 && *first == 'f') {
689 if (first[1] == 'p') {
690 unsigned cv;
691 const char *t = parse_cv_qualifiers(first + 2, last, cv);
692 const char *t1 = parse_number(t, last);
693 if (t1 != last && *t1 == '_') {
694 db.names.push_back("fp" + typename C::String(t, t1));
695 first = t1 + 1;
696 }
697 } else if (first[1] == 'L') {
698 unsigned cv;
699 const char *t0 = parse_number(first + 2, last);
700 if (t0 != last && *t0 == 'p') {
701 ++t0;
702 const char *t = parse_cv_qualifiers(t0, last, cv);
703 const char *t1 = parse_number(t, last);
704 if (t1 != last && *t1 == '_') {
705 db.names.push_back("fp" + typename C::String(t, t1));
706 first = t1 + 1;
707 }
708 }
709 }
710 }
711 return first;
712 }
713
714 // sZ # size of a function
715 // parameter pack
716
717 template
718 static const char *parse_sizeof_function_param_pack_expr(const char *first,
719 const char *last,
720 C &db) {
721 if (last - first >= 3 && first[0] == 's' && first[1] == 'Z' &&
722 first[2] == 'f') {
723 const char *t = parse_function_param(first + 2, last, db);
724 if (t != first + 2) {
725 if (db.names.empty())
726 return first;
727 db.names.back() = "sizeof...(" + db.names.back().move_full() + ")";
728 first = t;
729 }
730 }
731 return first;
732 }
733
734 // te # typeid (expression)
735 // ti # typeid (type)
736
737 template
738 static const char *parse_typeid_expr(const char *first, const char *last,
739 C &db) {
740 if (last - first >= 3 && first[0] == 't' &&
741 (first[1] == 'e' || first[1] == 'i')) {
742 const char *t;
743 if (first[1] == 'e')
744 t = parse_expression(first + 2, last, db);
745 else
746 t = parse_type(first + 2, last, db);
747 if (t != first + 2) {
748 if (db.names.empty())
749 return first;
750 db.names.back() = "typeid(" + db.names.back().move_full() + ")";
751 first = t;
752 }
753 }
754 return first;
755 }
756
757 // tw # throw expression
758
759 template
760 static const char *parse_throw_expr(const char *first, const char *last,
761 C &db) {
762 if (last - first >= 3 && first[0] == 't' && first[1] == 'w') {
763 const char *t = parse_expression(first + 2, last, db);
764 if (t != first + 2) {
765 if (db.names.empty())
766 return first;
767 db.names.back() = "throw " + db.names.back().move_full();
768 first = t;
769 }
770 }
771 return first;
772 }
773
774 // ds # expr.*expr
775
776 template
777 static const char *parse_dot_star_expr(const char *first, const char *last,
778 C &db) {
779 if (last - first >= 3 && first[0] == 'd' && first[1] == 's') {
780 const char *t = parse_expression(first + 2, last, db);
781 if (t != first + 2) {
782 const char *t1 = parse_expression(t, last, db);
783 if (t1 != t) {
784 if (db.names.size() < 2)
785 return first;
786 auto expr = db.names.back().move_full();
787 db.names.pop_back();
788 db.names.back().first += ".*" + expr;
789 first = t1;
790 }
791 }
792 }
793 return first;
794 }
795
796 // ::= [ ]
797
798 template
799 static const char *parse_simple_id(const char *first, const char *last, C &db) {
800 if (first != last) {
801 const char *t = parse_source_name(first, last, db);
802 if (t != first) {
803 const char *t1 = parse_template_args(t, last, db);
804 if (t1 != t) {
805 if (db.names.size() < 2)
806 return first;
807 auto args = db.names.back().move_full();
808 db.names.pop_back();
809 db.names.back().first += std::move(args);
810 }
811 first = t1;
812 } else
813 first = t;
814 }
815 return first;
816 }
817
818 // ::=
819 // ::=
820 // ::=
821
822 template
823 static const char *parse_unresolved_type(const char *first, const char *last,
824 C &db) {
825 if (first != last) {
826 const char *t = first;
827 switch (*first) {
828 case 'T': {
829 size_t k0 = db.names.size();
830 t = parse_template_param(first, last, db);
831 size_t k1 = db.names.size();
832 if (t != first && k1 == k0 + 1) {
833 db.subs.push_back(
834 typename C::sub_type(1, db.names.back(), db.names.get_allocator()));
835 first = t;
836 } else {
837 for (; k1 != k0; --k1)
838 db.names.pop_back();
839 }
840 break;
841 }
842 case 'D':
843 t = parse_decltype(first, last, db);
844 if (t != first) {
845 if (db.names.empty())
846 return first;
847 db.subs.push_back(
848 typename C::sub_type(1, db.names.back(), db.names.get_allocator()));
849 first = t;
850 }
851 break;
852 case 'S':
853 t = parse_substitution(first, last, db);
854 if (t != first)
855 first = t;
856 else {
857 if (last - first > 2 && first[1] == 't') {
858 t = parse_unqualified_name(first + 2, last, db);
859 if (t != first + 2) {
860 if (db.names.empty())
861 return first;
862 db.names.back().first.insert(0, "std::");
863 db.subs.push_back(typename C::sub_type(1, db.names.back(),
864 db.names.get_allocator()));
865 first = t;
866 }
867 }
868 }
869 break;
870 }
871 }
872 return first;
873 }
874
875 // ::= # e.g.,
876 // ~T or ~decltype(f())
877 // ::= # e.g.,
878 // ~A<2*N>
879
880 template
881 static const char *parse_destructor_name(const char *first, const char *last,
882 C &db) {
883 if (first != last) {
884 const char *t = parse_unresolved_type(first, last, db);
885 if (t == first)
886 t = parse_simple_id(first, last, db);
887 if (t != first) {
888 if (db.names.empty())
889 return first;
890 db.names.back().first.insert(0, "~");
891 first = t;
892 }
893 }
894 return first;
895 }
896
897 // ::= #
898 // unresolved name
899 // extension ::= #
900 // unresolved operator-function-id
901 // extension ::= #
902 // unresolved operator template-id
903 // ::= on #
904 // unresolved operator-function-id
905 // ::= on #
906 // unresolved operator template-id
907 // ::= dn #
908 // destructor or pseudo-destructor;
909 // #
910 // e.g.
911 // ~X or
912 // ~X
913
914 template
915 static const char *parse_base_unresolved_name(const char *first,
916 const char *last, C &db) {
917 if (last - first >= 2) {
918 if ((first[0] == 'o' || first[0] == 'd') && first[1] == 'n') {
919 if (first[0] == 'o') {
920 const char *t = parse_operator_name(first + 2, last, db);
921 if (t != first + 2) {
922 first = parse_template_args(t, last, db);
923 if (first != t) {
924 if (db.names.size() < 2)
925 return first;
926 auto args = db.names.back().move_full();
927 db.names.pop_back();
928 db.names.back().first += std::move(args);
929 }
930 }
931 } else {
932 const char *t = parse_destructor_name(first + 2, last, db);
933 if (t != first + 2)
934 first = t;
935 }
936 } else {
937 const char *t = parse_simple_id(first, last, db);
938 if (t == first) {
939 t = parse_operator_name(first, last, db);
940 if (t != first) {
941 first = parse_template_args(t, last, db);
942 if (first != t) {
943 if (db.names.size() < 2)
944 return first;
945 auto args = db.names.back().move_full();
946 db.names.pop_back();
947 db.names.back().first += std::move(args);
948 }
949 }
950 } else
951 first = t;
952 }
953 }
954 return first;
955 }
956
957 // ::=
958
959 template
960 static const char *parse_unresolved_qualifier_level(const char *first,
961 const char *last, C &db) {
962 return parse_simple_id(first, last, db);
963 }
964
965 //
966 // extension ::= srN []
967 // * E
968 // ::= [gs] # x or
969 // (with "gs") ::x
970 // ::= [gs] sr + E
971 //
972 // # A::x,
973 // N::y,
974 // A::z;
975 // "gs"
976 // means
977 // leading
978 // "::"
979 // ::= sr # T::x
980 // / decltype(p)::x
981 // extension ::= sr
982 //
983 // #
984 // T::N::x
985 // /decltype(p)::N::x
986 // (ignored) ::= srN + E
987 //
988
989 template
990 static const char *parse_unresolved_name(const char *first, const char *last,
991 C &db) {
992 if (last - first > 2) {
993 const char *t = first;
994 bool global = false;
995 if (t[0] == 'g' && t[1] == 's') {
996 global = true;
997 t += 2;
998 }
999 const char *t2 = parse_base_unresolved_name(t, last, db);
1000 if (t2 != t) {
1001 if (global) {
1002 if (db.names.empty())
1003 return first;
1004 db.names.back().first.insert(0, "::");
1005 }
1006 first = t2;
1007 } else if (last - t > 2 && t[0] == 's' && t[1] == 'r') {
1008 if (t[2] == 'N') {
1009 t += 3;
1010 const char *t1 = parse_unresolved_type(t, last, db);
1011 if (t1 == t || t1 == last)
1012 return first;
1013 t = t1;
1014 t1 = parse_template_args(t, last, db);
1015 if (t1 != t) {
1016 if (db.names.size() < 2)
1017 return first;
1018 auto args = db.names.back().move_full();
1019 db.names.pop_back();
1020 db.names.back().first += std::move(args);
1021 t = t1;
1022 if (t == last) {
1023 db.names.pop_back();
1024 return first;
1025 }
1026 }
1027 while (*t != 'E') {
1028 t1 = parse_unresolved_qualifier_level(t, last, db);
1029 if (t1 == t || t1 == last || db.names.size() < 2)
1030 return first;
1031 auto s = db.names.back().move_full();
1032 db.names.pop_back();
1033 db.names.back().first += "::" + std::move(s);
1034 t = t1;
1035 }
1036 ++t;
1037 t1 = parse_base_unresolved_name(t, last, db);
1038 if (t1 == t) {
1039 if (!db.names.empty())
1040 db.names.pop_back();
1041 return first;
1042 }
1043 if (db.names.size() < 2)
1044 return first;
1045 auto s = db.names.back().move_full();
1046 db.names.pop_back();
1047 db.names.back().first += "::" + std::move(s);
1048 first = t1;
1049 } else {
1050 t += 2;
1051 const char *t1 = parse_unresolved_type(t, last, db);
1052 if (t1 != t) {
1053 t = t1;
1054 t1 = parse_template_args(t, last, db);
1055 if (t1 != t) {
1056 if (db.names.size() < 2)
1057 return first;
1058 auto args = db.names.back().move_full();
1059 db.names.pop_back();
1060 db.names.back().first += std::move(args);
1061 t = t1;
1062 }
1063 t1 = parse_base_unresolved_name(t, last, db);
1064 if (t1 == t) {
1065 if (!db.names.empty())
1066 db.names.pop_back();
1067 return first;
1068 }
1069 if (db.names.size() < 2)
1070 return first;
1071 auto s = db.names.back().move_full();
1072 db.names.pop_back();
1073 db.names.back().first += "::" + std::move(s);
1074 first = t1;
1075 } else {
1076 t1 = parse_unresolved_qualifier_level(t, last, db);
1077 if (t1 == t || t1 == last)
1078 return first;
1079 t = t1;
1080 if (global) {
1081 if (db.names.empty())
1082 return first;
1083 db.names.back().first.insert(0, "::");
1084 }
1085 while (*t != 'E') {
1086 t1 = parse_unresolved_qualifier_level(t, last, db);
1087 if (t1 == t || t1 == last || db.names.size() < 2)
1088 return first;
1089 auto s = db.names.back().move_full();
1090 db.names.pop_back();
1091 db.names.back().first += "::" + std::move(s);
1092 t = t1;
1093 }
1094 ++t;
1095 t1 = parse_base_unresolved_name(t, last, db);
1096 if (t1 == t) {
1097 if (!db.names.empty())
1098 db.names.pop_back();
1099 return first;
1100 }
1101 if (db.names.size() < 2)
1102 return first;
1103 auto s = db.names.back().move_full();
1104 db.names.pop_back();
1105 db.names.back().first += "::" + std::move(s);
1106 first = t1;
1107 }
1108 }
1109 }
1110 }
1111 return first;
1112 }
1113
1114 // dt # expr.name
1115
1116 template
1117 static const char *parse_dot_expr(const char *first, const char *last, C &db) {
1118 if (last - first >= 3 && first[0] == 'd' && first[1] == 't') {
1119 const char *t = parse_expression(first + 2, last, db);
1120 if (t != first + 2) {
1121 const char *t1 = parse_unresolved_name(t, last, db);
1122 if (t1 != t) {
1123 if (db.names.size() < 2)
1124 return first;
1125 auto name = db.names.back().move_full();
1126 db.names.pop_back();
1127 if (db.names.empty())
1128 return first;
1129 db.names.back().first += "." + name;
1130 first = t1;
1131 }
1132 }
1133 }
1134 return first;
1135 }
1136
1137 // cl + E # call
1138
1139 template
1140 static const char *parse_call_expr(const char *first, const char *last, C &db) {
1141 if (last - first >= 4 && first[0] == 'c' && first[1] == 'l') {
1142 const char *t = parse_expression(first + 2, last, db);
1143 if (t != first + 2) {
1144 if (t == last)
1145 return first;
1146 if (db.names.empty())
1147 return first;
1148 db.names.back().first += db.names.back().second;
1149 db.names.back().second = typename C::String();
1150 db.names.back().first.append("(");
1151 bool first_expr = true;
1152 while (*t != 'E') {
1153 const char *t1 = parse_expression(t, last, db);
1154 if (t1 == t || t1 == last)
1155 return first;
1156 if (db.names.empty())
1157 return first;
1158 auto tmp = db.names.back().move_full();
1159 db.names.pop_back();
1160 if (!tmp.empty()) {
1161 if (db.names.empty())
1162 return first;
1163 if (!first_expr) {
1164 db.names.back().first.append(", ");
1165 first_expr = false;
1166 }
1167 db.names.back().first.append(tmp);
1168 }
1169 t = t1;
1170 }
1171 ++t;
1172 if (db.names.empty())
1173 return first;
1174 db.names.back().first.append(")");
1175 first = t;
1176 }
1177 }
1178 return first;
1179 }
1180
1181 // [gs] nw * _ E # new (expr-list) type
1182 // [gs] nw * _ # new (expr-list) type
1183 // (init)
1184 // [gs] na * _ E # new[] (expr-list) type
1185 // [gs] na * _ # new[] (expr-list) type
1186 // (init)
1187 // ::= pi * E # parenthesized
1188 // initialization
1189
1190 template
1191 static const char *parse_new_expr(const char *first, const char *last, C &db) {
1192 if (last - first >= 4) {
1193 const char *t = first;
1194 bool parsed_gs = false;
1195 if (t[0] == 'g' && t[1] == 's') {
1196 t += 2;
1197 parsed_gs = true;
1198 }
1199 if (t[0] == 'n' && (t[1] == 'w' || t[1] == 'a')) {
1200 bool is_array = t[1] == 'a';
1201 t += 2;
1202 if (t == last)
1203 return first;
1204 bool has_expr_list = false;
1205 bool first_expr = true;
1206 while (*t != '_') {
1207 const char *t1 = parse_expression(t, last, db);
1208 if (t1 == t || t1 == last)
1209 return first;
1210 has_expr_list = true;
1211 if (!first_expr) {
1212 if (db.names.empty())
1213 return first;
1214 auto tmp = db.names.back().move_full();
1215 db.names.pop_back();
1216 if (!tmp.empty()) {
1217 if (db.names.empty())
1218 return first;
1219 db.names.back().first.append(", ");
1220 db.names.back().first.append(tmp);
1221 first_expr = false;
1222 }
1223 }
1224 t = t1;
1225 }
1226 ++t;
1227 const char *t1 = parse_type(t, last, db);
1228 if (t1 == t || t1 == last)
1229 return first;
1230 t = t1;
1231 bool has_init = false;
1232 if (last - t >= 3 && t[0] == 'p' && t[1] == 'i') {
1233 t += 2;
1234 has_init = true;
1235 first_expr = true;
1236 while (*t != 'E') {
1237 t1 = parse_expression(t, last, db);
1238 if (t1 == t || t1 == last)
1239 return first;
1240 if (!first_expr) {
1241 if (db.names.empty())
1242 return first;
1243 auto tmp = db.names.back().move_full();
1244 db.names.pop_back();
1245 if (!tmp.empty()) {
1246 if (db.names.empty())
1247 return first;
1248 db.names.back().first.append(", ");
1249 db.names.back().first.append(tmp);
1250 first_expr = false;
1251 }
1252 }
1253 t = t1;
1254 }
1255 }
1256 if (*t != 'E')
1257 return first;
1258 typename C::String init_list;
1259 if (has_init) {
1260 if (db.names.empty())
1261 return first;
1262 init_list = db.names.back().move_full();
1263 db.names.pop_back();
1264 }
1265 if (db.names.empty())
1266 return first;
1267 auto type = db.names.back().move_full();
1268 db.names.pop_back();
1269 typename C::String expr_list;
1270 if (has_expr_list) {
1271 if (db.names.empty())
1272 return first;
1273 expr_list = db.names.back().move_full();
1274 db.names.pop_back();
1275 }
1276 typename C::String r;
1277 if (parsed_gs)
1278 r = "::";
1279 if (is_array)
1280 r += "[] ";
1281 else
1282 r += " ";
1283 if (has_expr_list)
1284 r += "(" + expr_list + ") ";
1285 r += type;
1286 if (has_init)
1287 r += " (" + init_list + ")";
1288 db.names.push_back(std::move(r));
1289 first = t + 1;
1290 }
1291 }
1292 return first;
1293 }
1294
1295 // cv # conversion with one
1296 // argument
1297 // cv _ * E # conversion with a
1298 // different number of arguments
1299
1300 template
1301 static const char *parse_conversion_expr(const char *first, const char *last,
1302 C &db) {
1303 if (last - first >= 3 && first[0] == 'c' && first[1] == 'v') {
1304 bool try_to_parse_template_args = db.try_to_parse_template_args;
1305 db.try_to_parse_template_args = false;
1306 const char *t = parse_type(first + 2, last, db);
1307 db.try_to_parse_template_args = try_to_parse_template_args;
1308 if (t != first + 2 && t != last) {
1309 if (*t != '_') {
1310 const char *t1 = parse_expression(t, last, db);
1311 if (t1 == t)
1312 return first;
1313 t = t1;
1314 } else {
1315 ++t;
1316 if (t == last)
1317 return first;
1318 if (*t == 'E')
1319 db.names.emplace_back();
1320 else {
1321 bool first_expr = true;
1322 while (*t != 'E') {
1323 const char *t1 = parse_expression(t, last, db);
1324 if (t1 == t || t1 == last)
1325 return first;
1326 if (!first_expr) {
1327 if (db.names.empty())
1328 return first;
1329 auto tmp = db.names.back().move_full();
1330 db.names.pop_back();
1331 if (!tmp.empty()) {
1332 if (db.names.empty())
1333 return first;
1334 db.names.back().first.append(", ");
1335 db.names.back().first.append(tmp);
1336 first_expr = false;
1337 }
1338 }
1339 t = t1;
1340 }
1341 }
1342 ++t;
1343 }
1344 if (db.names.size() < 2)
1345 return first;
1346 auto tmp = db.names.back().move_full();
1347 db.names.pop_back();
1348 db.names.back() = "(" + db.names.back().move_full() + ")(" + tmp + ")";
1349 first = t;
1350 }
1351 }
1352 return first;
1353 }
1354
1355 // pt # expr->name
1356
1357 template
1358 static const char *parse_arrow_expr(const char *first, const char *last,
1359 C &db) {
1360 if (last - first >= 3 && first[0] == 'p' && first[1] == 't') {
1361 const char *t = parse_expression(first + 2, last, db);
1362 if (t != first + 2) {
1363 const char *t1 = parse_expression(t, last, db);
1364 if (t1 != t) {
1365 if (db.names.size() < 2)
1366 return first;
1367 auto tmp = db.names.back().move_full();
1368 db.names.pop_back();
1369 db.names.back().first += "->";
1370 db.names.back().first += tmp;
1371 first = t1;
1372 }
1373 }
1374 }
1375 return first;
1376 }
1377
1378 // ::= R # & ref-qualifier
1379 // ::= O # && ref-qualifier
1380
1381 // ::= F [Y] [] E
1382
1383 template
1384 static const char *parse_function_type(const char *first, const char *last,
1385 C &db) {
1386 if (first != last && *first == 'F') {
1387 const char *t = first + 1;
1388 if (t != last) {
1389 if (*t == 'Y') {
1390 /* extern "C" */
1391 if (++t == last)
1392 return first;
1393 }
1394 const char *t1 = parse_type(t, last, db);
1395 if (t1 != t) {
1396 t = t1;
1397 typename C::String sig("(");
1398 int ref_qual = 0;
1399 while (true) {
1400 if (t == last) {
1401 db.names.pop_back();
1402 return first;
1403 }
1404 if (*t == 'E') {
1405 ++t;
1406 break;
1407 }
1408 if (*t == 'v') {
1409 ++t;
1410 continue;
1411 }
1412 if (*t == 'R' && t + 1 != last && t[1] == 'E') {
1413 ref_qual = 1;
1414 ++t;
1415 continue;
1416 }
1417 if (*t == 'O' && t + 1 != last && t[1] == 'E') {
1418 ref_qual = 2;
1419 ++t;
1420 continue;
1421 }
1422 size_t k0 = db.names.size();
1423 t1 = parse_type(t, last, db);
1424 size_t k1 = db.names.size();
1425 if (t1 == t || t1 == last)
1426 return first;
1427 for (size_t k = k0; k < k1; ++k) {
1428 if (sig.size() > 1)
1429 sig += ", ";
1430 sig += db.names[k].move_full();
1431 }
1432 for (size_t k = k0; k < k1; ++k)
1433 db.names.pop_back();
1434 t = t1;
1435 }
1436 sig += ")";
1437 switch (ref_qual) {
1438 case 1:
1439 sig += " &";
1440 break;
1441 case 2:
1442 sig += " &&";
1443 break;
1444 }
1445 if (db.names.empty())
1446 return first;
1447 db.names.back().first += " ";
1448 db.names.back().second.insert(0, sig);
1449 first = t;
1450 }
1451 }
1452 }
1453 return first;
1454 }
1455
1456 // ::= M
1457
1458 template
1459 static const char *parse_pointer_to_member_type(const char *first,
1460 const char *last, C &db) {
1461 if (first != last && *first == 'M') {
1462 const char *t = parse_type(first + 1, last, db);
1463 if (t != first + 1) {
1464 const char *t2 = parse_type(t, last, db);
1465 if (t2 != t) {
1466 if (db.names.size() < 2)
1467 return first;
1468 auto func = std::move(db.names.back());
1469 db.names.pop_back();
1470 auto class_type = std::move(db.names.back());
1471 if (!func.second.empty() && func.second.front() == '(') {
1472 db.names.back().first =
1473 std::move(func.first) + "(" + class_type.move_full() + "::*";
1474 db.names.back().second = ")" + std::move(func.second);
1475 } else {
1476 db.names.back().first =
1477 std::move(func.first) + " " + class_type.move_full() + "::*";
1478 db.names.back().second = std::move(func.second);
1479 }
1480 first = t2;
1481 }
1482 }
1483 }
1484 return first;
1485 }
1486
1487 // ::= A _
1488 // ::= A [] _
1489
1490 template
1491 static const char *parse_array_type(const char *first, const char *last,
1492 C &db) {
1493 if (first != last && *first == 'A' && first + 1 != last) {
1494 if (first[1] == '_') {
1495 const char *t = parse_type(first + 2, last, db);
1496 if (t != first + 2) {
1497 if (db.names.empty())
1498 return first;
1499 if (db.names.back().second.substr(0, 2) == " [")
1500 db.names.back().second.erase(0, 1);
1501 db.names.back().second.insert(0, " []");
1502 first = t;
1503 }
1504 } else if ('1' <= first[1] && first[1] <= '9') {
1505 const char *t = parse_number(first + 1, last);
1506 if (t != last && *t == '_') {
1507 const char *t2 = parse_type(t + 1, last, db);
1508 if (t2 != t + 1) {
1509 if (db.names.empty())
1510 return first;
1511 if (db.names.back().second.substr(0, 2) == " [")
1512 db.names.back().second.erase(0, 1);
1513 db.names.back().second.insert(
1514 0, " [" + typename C::String(first + 1, t) + "]");
1515 first = t2;
1516 }
1517 }
1518 } else {
1519 const char *t = parse_expression(first + 1, last, db);
1520 if (t != first + 1 && t != last && *t == '_') {
1521 const char *t2 = parse_type(++t, last, db);
1522 if (t2 != t) {
1523 if (db.names.size() < 2)
1524 return first;
1525 auto type = std::move(db.names.back());
1526 db.names.pop_back();
1527 auto expr = std::move(db.names.back());
1528 db.names.back().first = std::move(type.first);
1529 if (type.second.substr(0, 2) == " [")
1530 type.second.erase(0, 1);
1531 db.names.back().second =
1532 " [" + expr.move_full() + "]" + std::move(type.second);
1533 first = t2;
1534 }
1535 }
1536 }
1537 }
1538 return first;
1539 }
1540
1541 // ::= Dt E # decltype of an id-expression or class
1542 // member access (C++0x)
1543 // ::= DT E # decltype of an expression (C++0x)
1544
1545 template
1546 static const char *parse_decltype(const char *first, const char *last, C &db) {
1547 if (last - first >= 4 && first[0] == 'D') {
1548 switch (first[1]) {
1549 case 't':
1550 case 'T': {
1551 const char *t = parse_expression(first + 2, last, db);
1552 if (t != first + 2 && t != last && *t == 'E') {
1553 if (db.names.empty())
1554 return first;
1555 db.names.back() = "decltype(" + db.names.back().move_full() + ")";
1556 first = t + 1;
1557 }
1558 } break;
1559 }
1560 }
1561 return first;
1562 }
1563
1564 // extension:
1565 // ::= Dv _
1566 //
1567 // ::= Dv [] _
1568 // ::=
1569 // ::= p # AltiVec vector pixel
1570
1571 template
1572 static const char *parse_vector_type(const char *first, const char *last,
1573 C &db) {
1574 if (last - first > 3 && first[0] == 'D' && first[1] == 'v') {
1575 if ('1' <= first[2] && first[2] <= '9') {
1576 const char *t = parse_number(first + 2, last);
1577 if (t == last || *t != '_')
1578 return first;
1579 const char *num = first + 2;
1580 size_t sz = static_cast(t - num);
1581 if (++t != last) {
1582 if (*t != 'p') {
1583 const char *t1 = parse_type(t, last, db);
1584 if (t1 != t) {
1585 if (db.names.empty())
1586 return first;
1587 db.names.back().first +=
1588 " vector[" + typename C::String(num, sz) + "]";
1589 first = t1;
1590 }
1591 } else {
1592 ++t;
1593 db.names.push_back("pixel vector[" + typename C::String(num, sz) +
1594 "]");
1595 first = t;
1596 }
1597 }
1598 } else {
1599 typename C::String num;
1600 const char *t1 = first + 2;
1601 if (*t1 != '_') {
1602 const char *t = parse_expression(t1, last, db);
1603 if (t != t1) {
1604 if (db.names.empty())
1605 return first;
1606 num = db.names.back().move_full();
1607 db.names.pop_back();
1608 t1 = t;
1609 }
1610 }
1611 if (t1 != last && *t1 == '_' && ++t1 != last) {
1612 const char *t = parse_type(t1, last, db);
1613 if (t != t1) {
1614 if (db.names.empty())
1615 return first;
1616 db.names.back().first += " vector[" + num + "]";
1617 first = t;
1618 }
1619 }
1620 }
1621 }
1622 return first;
1623 }
1624
1625 // ::=
1626 // ::=
1627 // ::=
1628 // ::=
1629 // ::=
1630 // ::=
1631 // ::=
1632 // ::=
1633 // ::=
1634 // ::=
1635 // ::= P # pointer-to
1636 // ::= R # reference-to
1637 // ::= O # rvalue reference-to (C++0x)
1638 // ::= C # complex pair (C 2000)
1639 // ::= G # imaginary (C 2000)
1640 // ::= Dp # pack expansion (C++0x)
1641 // ::= U # vendor extended type qualifier
1642 // extension := U # objc-type
1643 // extension := # starts with Dv
1644
1645 // ::= objcproto # k0 = 9 +
1646 // + k1
1647 // := # PU<11+>objcproto 11objc_object
1648 // 11objc_object -> id
1649
1650 template
1651 static const char *parse_type(const char *first, const char *last, C &db) {
1652 if (first != last) {
1653 switch (*first) {
1654 case 'r':
1655 case 'V':
1656 case 'K': {
1657 unsigned cv = 0;
1658 const char *t = parse_cv_qualifiers(first, last, cv);
1659 if (t != first) {
1660 bool is_function = *t == 'F';
1661 size_t k0 = db.names.size();
1662 const char *t1 = parse_type(t, last, db);
1663 size_t k1 = db.names.size();
1664 if (t1 != t) {
1665 if (is_function)
1666 db.subs.pop_back();
1667 db.subs.emplace_back(db.names.get_allocator());
1668 for (size_t k = k0; k < k1; ++k) {
1669 if (is_function) {
1670 size_t p = db.names[k].second.size();
1671 if (db.names[k].second[p - 2] == '&')
1672 p -= 3;
1673 else if (db.names[k].second.back() == '&')
1674 p -= 2;
1675 if (cv & 1) {
1676 db.names[k].second.insert(p, " const");
1677 p += 6;
1678 }
1679 if (cv & 2) {
1680 db.names[k].second.insert(p, " volatile");
1681 p += 9;
1682 }
1683 if (cv & 4)
1684 db.names[k].second.insert(p, " restrict");
1685 } else {
1686 if (cv & 1)
1687 db.names[k].first.append(" const");
1688 if (cv & 2)
1689 db.names[k].first.append(" volatile");
1690 if (cv & 4)
1691 db.names[k].first.append(" restrict");
1692 }
1693 db.subs.back().push_back(db.names[k]);
1694 }
1695 first = t1;
1696 }
1697 }
1698 } break;
1699 default: {
1700 const char *t = parse_builtin_type(first, last, db);
1701 if (t != first) {
1702 first = t;
1703 } else {
1704 switch (*first) {
1705 case 'A':
1706 t = parse_array_type(first, last, db);
1707 if (t != first) {
1708 if (db.names.empty())
1709 return first;
1710 first = t;
1711 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1712 db.names.get_allocator()));
1713 }
1714 break;
1715 case 'C':
1716 t = parse_type(first + 1, last, db);
1717 if (t != first + 1) {
1718 if (db.names.empty())
1719 return first;
1720 db.names.back().first.append(" complex");
1721 first = t;
1722 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1723 db.names.get_allocator()));
1724 }
1725 break;
1726 case 'F':
1727 t = parse_function_type(first, last, db);
1728 if (t != first) {
1729 if (db.names.empty())
1730 return first;
1731 first = t;
1732 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1733 db.names.get_allocator()));
1734 }
1735 break;
1736 case 'G':
1737 t = parse_type(first + 1, last, db);
1738 if (t != first + 1) {
1739 if (db.names.empty())
1740 return first;
1741 db.names.back().first.append(" imaginary");
1742 first = t;
1743 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1744 db.names.get_allocator()));
1745 }
1746 break;
1747 case 'M':
1748 t = parse_pointer_to_member_type(first, last, db);
1749 if (t != first) {
1750 if (db.names.empty())
1751 return first;
1752 first = t;
1753 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1754 db.names.get_allocator()));
1755 }
1756 break;
1757 case 'O': {
1758 size_t k0 = db.names.size();
1759 t = parse_type(first + 1, last, db);
1760 size_t k1 = db.names.size();
1761 if (t != first + 1) {
1762 db.subs.emplace_back(db.names.get_allocator());
1763 for (size_t k = k0; k < k1; ++k) {
1764 if (db.names[k].second.substr(0, 2) == " [") {
1765 db.names[k].first += " (";
1766 db.names[k].second.insert(0, ")");
1767 } else if (!db.names[k].second.empty() &&
1768 db.names[k].second.front() == '(') {
1769 db.names[k].first += "(";
1770 db.names[k].second.insert(0, ")");
1771 }
1772 db.names[k].first.append("&&");
1773 db.subs.back().push_back(db.names[k]);
1774 }
1775 first = t;
1776 }
1777 break;
1778 }
1779 case 'P': {
1780 size_t k0 = db.names.size();
1781 t = parse_type(first + 1, last, db);
1782 size_t k1 = db.names.size();
1783 if (t != first + 1) {
1784 db.subs.emplace_back(db.names.get_allocator());
1785 for (size_t k = k0; k < k1; ++k) {
1786 if (db.names[k].second.substr(0, 2) == " [") {
1787 db.names[k].first += " (";
1788 db.names[k].second.insert(0, ")");
1789 } else if (!db.names[k].second.empty() &&
1790 db.names[k].second.front() == '(') {
1791 db.names[k].first += "(";
1792 db.names[k].second.insert(0, ")");
1793 }
1794 if (first[1] != 'U' ||
1795 db.names[k].first.substr(0, 12) != "objc_object<") {
1796 db.names[k].first.append("*");
1797 } else {
1798 db.names[k].first.replace(0, 11, "id");
1799 }
1800 db.subs.back().push_back(db.names[k]);
1801 }
1802 first = t;
1803 }
1804 break;
1805 }
1806 case 'R': {
1807 size_t k0 = db.names.size();
1808 t = parse_type(first + 1, last, db);
1809 size_t k1 = db.names.size();
1810 if (t != first + 1) {
1811 db.subs.emplace_back(db.names.get_allocator());
1812 for (size_t k = k0; k < k1; ++k) {
1813 if (db.names[k].second.substr(0, 2) == " [") {
1814 db.names[k].first += " (";
1815 db.names[k].second.insert(0, ")");
1816 } else if (!db.names[k].second.empty() &&
1817 db.names[k].second.front() == '(') {
1818 db.names[k].first += "(";
1819 db.names[k].second.insert(0, ")");
1820 }
1821 db.names[k].first.append("&");
1822 db.subs.back().push_back(db.names[k]);
1823 }
1824 first = t;
1825 }
1826 break;
1827 }
1828 case 'T': {
1829 size_t k0 = db.names.size();
1830 t = parse_template_param(first, last, db);
1831 size_t k1 = db.names.size();
1832 if (t != first) {
1833 db.subs.emplace_back(db.names.get_allocator());
1834 for (size_t k = k0; k < k1; ++k)
1835 db.subs.back().push_back(db.names[k]);
1836 if (db.try_to_parse_template_args && k1 == k0 + 1) {
1837 const char *t1 = parse_template_args(t, last, db);
1838 if (t1 != t) {
1839 auto args = db.names.back().move_full();
1840 db.names.pop_back();
1841 db.names.back().first += std::move(args);
1842 db.subs.push_back(typename C::sub_type(
1843 1, db.names.back(), db.names.get_allocator()));
1844 t = t1;
1845 }
1846 }
1847 first = t;
1848 }
1849 break;
1850 }
1851 case 'U':
1852 if (first + 1 != last) {
1853 t = parse_source_name(first + 1, last, db);
1854 if (t != first + 1) {
1855 const char *t2 = parse_type(t, last, db);
1856 if (t2 != t) {
1857 if (db.names.size() < 2)
1858 return first;
1859 auto type = db.names.back().move_full();
1860 db.names.pop_back();
1861 if (db.names.back().first.substr(0, 9) != "objcproto") {
1862 db.names.back() = type + " " + db.names.back().move_full();
1863 } else {
1864 auto proto = db.names.back().move_full();
1865 db.names.pop_back();
1866 t = parse_source_name(proto.data() + 9,
1867 proto.data() + proto.size(), db);
1868 if (t != proto.data() + 9) {
1869 db.names.back() =
1870 type + "<" + db.names.back().move_full() + ">";
1871 } else {
1872 db.names.push_back(type + " " + proto);
1873 }
1874 }
1875 db.subs.push_back(typename C::sub_type(
1876 1, db.names.back(), db.names.get_allocator()));
1877 first = t2;
1878 }
1879 }
1880 }
1881 break;
1882 case 'S':
1883 if (first + 1 != last && first[1] == 't') {
1884 t = parse_name(first, last, db);
1885 if (t != first) {
1886 if (db.names.empty())
1887 return first;
1888 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1889 db.names.get_allocator()));
1890 first = t;
1891 }
1892 } else {
1893 t = parse_substitution(first, last, db);
1894 if (t != first) {
1895 first = t;
1896 // Parsed a substitution. If the substitution is a
1897 // it might be followed by .
1898 t = parse_template_args(first, last, db);
1899 if (t != first) {
1900 if (db.names.size() < 2)
1901 return first;
1902 auto template_args = db.names.back().move_full();
1903 db.names.pop_back();
1904 db.names.back().first += template_args;
1905 // Need to create substitution for
1906 //
1907 db.subs.push_back(typename C::sub_type(
1908 1, db.names.back(), db.names.get_allocator()));
1909 first = t;
1910 }
1911 }
1912 }
1913 break;
1914 case 'D':
1915 if (first + 1 != last) {
1916 switch (first[1]) {
1917 case 'p': {
1918 size_t k0 = db.names.size();
1919 t = parse_type(first + 2, last, db);
1920 size_t k1 = db.names.size();
1921 if (t != first + 2) {
1922 db.subs.emplace_back(db.names.get_allocator());
1923 for (size_t k = k0; k < k1; ++k)
1924 db.subs.back().push_back(db.names[k]);
1925 first = t;
1926 return first;
1927 }
1928 break;
1929 }
1930 case 't':
1931 case 'T':
1932 t = parse_decltype(first, last, db);
1933 if (t != first) {
1934 if (db.names.empty())
1935 return first;
1936 db.subs.push_back(typename C::sub_type(
1937 1, db.names.back(), db.names.get_allocator()));
1938 first = t;
1939 return first;
1940 }
1941 break;
1942 case 'v':
1943 t = parse_vector_type(first, last, db);
1944 if (t != first) {
1945 if (db.names.empty())
1946 return first;
1947 db.subs.push_back(typename C::sub_type(
1948 1, db.names.back(), db.names.get_allocator()));
1949 first = t;
1950 return first;
1951 }
1952 break;
1953 }
1954 }
1955 // drop through
1956 default:
1957 // must check for builtin-types before class-enum-types to avoid
1958 // ambiguities with operator-names
1959 t = parse_builtin_type(first, last, db);
1960 if (t != first) {
1961 first = t;
1962 } else {
1963 t = parse_name(first, last, db);
1964 if (t != first) {
1965 if (db.names.empty())
1966 return first;
1967 db.subs.push_back(typename C::sub_type(1, db.names.back(),
1968 db.names.get_allocator()));
1969 first = t;
1970 }
1971 }
1972 break;
1973 }
1974 }
1975 break;
1976 }
1977 }
1978 }
1979 return first;
1980 }
1981
1982 //
1983 // ::= aa # &&
1984 // ::= ad # & (unary)
1985 // ::= an # &
1986 // ::= aN # &=
1987 // ::= aS # =
1988 // ::= cl # ()
1989 // ::= cm # ,
1990 // ::= co # ~
1991 // ::= cv # (cast)
1992 // ::= da # delete[]
1993 // ::= de # * (unary)
1994 // ::= dl # delete
1995 // ::= dv # /
1996 // ::= dV # /=
1997 // ::= eo # ^
1998 // ::= eO # ^=
1999 // ::= eq # ==
2000 // ::= ge # >=
2001 // ::= gt # >
2002 // ::= ix # []
2003 // ::= le # <=
2004 // ::= li # operator ""
2005 // ::= ls # <<
2006 // ::= lS # <<=
2007 // ::= lt # <
2008 // ::= mi # -
2009 // ::= mI # -=
2010 // ::= ml # *
2011 // ::= mL # *=
2012 // ::= mm # -- (postfix in context)
2013 // ::= na # new[]
2014 // ::= ne # !=
2015 // ::= ng # - (unary)
2016 // ::= nt # !
2017 // ::= nw # new
2018 // ::= oo # ||
2019 // ::= or # |
2020 // ::= oR # |=
2021 // ::= pm # ->*
2022 // ::= pl # +
2023 // ::= pL # +=
2024 // ::= pp # ++ (postfix in context)
2025 // ::= ps # + (unary)
2026 // ::= pt # ->
2027 // ::= qu # ?
2028 // ::= rm # %
2029 // ::= rM # %=
2030 // ::= rs # >>
2031 // ::= rS # >>=
2032 // ::= v # vendor extended
2033 // operator
2034
2035 template
2036 static const char *parse_operator_name(const char *first, const char *last,
2037 C &db) {
2038 if (last - first >= 2) {
2039 switch (first[0]) {
2040 case 'a':
2041 switch (first[1]) {
2042 case 'a':
2043 db.names.push_back("operator&&");
2044 first += 2;
2045 break;
2046 case 'd':
2047 case 'n':
2048 db.names.push_back("operator&");
2049 first += 2;
2050 break;
2051 case 'N':
2052 db.names.push_back("operator&=");
2053 first += 2;
2054 break;
2055 case 'S':
2056 db.names.push_back("operator=");
2057 first += 2;
2058 break;
2059 }
2060 break;
2061 case 'c':
2062 switch (first[1]) {
2063 case 'l':
2064 db.names.push_back("operator()");
2065 first += 2;
2066 break;
2067 case 'm':
2068 db.names.push_back("operator,");
2069 first += 2;
2070 break;
2071 case 'o':
2072 db.names.push_back("operator~");
2073 first += 2;
2074 break;
2075 case 'v': {
2076 bool try_to_parse_template_args = db.try_to_parse_template_args;
2077 db.try_to_parse_template_args = false;
2078 const char *t = parse_type(first + 2, last, db);
2079 db.try_to_parse_template_args = try_to_parse_template_args;
2080 if (t != first + 2) {
2081 if (db.names.empty())
2082 return first;
2083 db.names.back().first.insert(0, "operator ");
2084 db.parsed_ctor_dtor_cv = true;
2085 first = t;
2086 }
2087 } break;
2088 }
2089 break;
2090 case 'd':
2091 switch (first[1]) {
2092 case 'a':
2093 db.names.push_back("operator delete[]");
2094 first += 2;
2095 break;
2096 case 'e':
2097 db.names.push_back("operator*");
2098 first += 2;
2099 break;
2100 case 'l':
2101 db.names.push_back("operator delete");
2102 first += 2;
2103 break;
2104 case 'v':
2105 db.names.push_back("operator/");
2106 first += 2;
2107 break;
2108 case 'V':
2109 db.names.push_back("operator/=");
2110 first += 2;
2111 break;
2112 }
2113 break;
2114 case 'e':
2115 switch (first[1]) {
2116 case 'o':
2117 db.names.push_back("operator^");
2118 first += 2;
2119 break;
2120 case 'O':
2121 db.names.push_back("operator^=");
2122 first += 2;
2123 break;
2124 case 'q':
2125 db.names.push_back("operator==");
2126 first += 2;
2127 break;
2128 }
2129 break;
2130 case 'g':
2131 switch (first[1]) {
2132 case 'e':
2133 db.names.push_back("operator>=");
2134 first += 2;
2135 break;
2136 case 't':
2137 db.names.push_back("operator>");
2138 first += 2;
2139 break;
2140 }
2141 break;
2142 case 'i':
2143 if (first[1] == 'x') {
2144 db.names.push_back("operator[]");
2145 first += 2;
2146 }
2147 break;
2148 case 'l':
2149 switch (first[1]) {
2150 case 'e':
2151 db.names.push_back("operator<=");
2152 first += 2;
2153 break;
2154 case 'i': {
2155 const char *t = parse_source_name(first + 2, last, db);
2156 if (t != first + 2) {
2157 if (db.names.empty())
2158 return first;
2159 db.names.back().first.insert(0, "operator\"\" ");
2160 first = t;
2161 }
2162 } break;
2163 case 's':
2164 db.names.push_back("operator<<");
2165 first += 2;
2166 break;
2167 case 'S':
2168 db.names.push_back("operator<<=");
2169 first += 2;
2170 break;
2171 case 't':
2172 db.names.push_back("operator<");
2173 first += 2;
2174 break;
2175 }
2176 break;
2177 case 'm':
2178 switch (first[1]) {
2179 case 'i':
2180 db.names.push_back("operator-");
2181 first += 2;
2182 break;
2183 case 'I':
2184 db.names.push_back("operator-=");
2185 first += 2;
2186 break;
2187 case 'l':
2188 db.names.push_back("operator*");
2189 first += 2;
2190 break;
2191 case 'L':
2192 db.names.push_back("operator*=");
2193 first += 2;
2194 break;
2195 case 'm':
2196 db.names.push_back("operator--");
2197 first += 2;
2198 break;
2199 }
2200 break;
2201 case 'n':
2202 switch (first[1]) {
2203 case 'a':
2204 db.names.push_back("operator new[]");
2205 first += 2;
2206 break;
2207 case 'e':
2208 db.names.push_back("operator!=");
2209 first += 2;
2210 break;
2211 case 'g':
2212 db.names.push_back("operator-");
2213 first += 2;
2214 break;
2215 case 't':
2216 db.names.push_back("operator!");
2217 first += 2;
2218 break;
2219 case 'w':
2220 db.names.push_back("operator new");
2221 first += 2;
2222 break;
2223 }
2224 break;
2225 case 'o':
2226 switch (first[1]) {
2227 case 'o':
2228 db.names.push_back("operator||");
2229 first += 2;
2230 break;
2231 case 'r':
2232 db.names.push_back("operator|");
2233 first += 2;
2234 break;
2235 case 'R':
2236 db.names.push_back("operator|=");
2237 first += 2;
2238 break;
2239 }
2240 break;
2241 case 'p':
2242 switch (first[1]) {
2243 case 'm':
2244 db.names.push_back("operator->*");
2245 first += 2;
2246 break;
2247 case 'l':
2248 db.names.push_back("operator+");
2249 first += 2;
2250 break;
2251 case 'L':
2252 db.names.push_back("operator+=");
2253 first += 2;
2254 break;
2255 case 'p':
2256 db.names.push_back("operator++");
2257 first += 2;
2258 break;
2259 case 's':
2260 db.names.push_back("operator+");
2261 first += 2;
2262 break;
2263 case 't':
2264 db.names.push_back("operator->");
2265 first += 2;
2266 break;
2267 }
2268 break;
2269 case 'q':
2270 if (first[1] == 'u') {
2271 db.names.push_back("operator?");
2272 first += 2;
2273 }
2274 break;
2275 case 'r':
2276 switch (first[1]) {
2277 case 'm':
2278 db.names.push_back("operator%");
2279 first += 2;
2280 break;
2281 case 'M':
2282 db.names.push_back("operator%=");
2283 first += 2;
2284 break;
2285 case 's':
2286 db.names.push_back("operator>>");
2287 first += 2;
2288 break;
2289 case 'S':
2290 db.names.push_back("operator>>=");
2291 first += 2;
2292 break;
2293 }
2294 break;
2295 case 'v':
2296 if (std::isdigit(first[1])) {
2297 const char *t = parse_source_name(first + 2, last, db);
2298 if (t != first + 2) {
2299 if (db.names.empty())
2300 return first;
2301 db.names.back().first.insert(0, "operator ");
2302 first = t;
2303 }
2304 }
2305 break;
2306 }
2307 }
2308 return first;
2309 }
2310
2311 template
2312 static const char *parse_integer_literal(const char *first, const char *last,
2313 const typename C::String &lit, C &db) {
2314 const char *t = parse_number(first, last);
2315 if (t != first && t != last && *t == 'E') {
2316 if (lit.size() > 3)
2317 db.names.push_back("(" + lit + ")");
2318 else
2319 db.names.emplace_back();
2320 if (*first == 'n') {
2321 db.names.back().first += '-';
2322 ++first;
2323 }
2324 db.names.back().first.append(first, t);
2325 if (lit.size() <= 3)
2326 db.names.back().first += lit;
2327 first = t + 1;
2328 }
2329 return first;
2330 }
2331
2332 // ::= L E #
2333 // integer literal
2334 // ::= L E #
2335 // floating literal
2336 // ::= L E #
2337 // string literal
2338 // ::= L E #
2339 // nullptr literal (i.e., "LDnE")
2340 // ::= L _ E #
2341 // complex floating point literal (C 2000)
2342 // ::= L E #
2343 // external name
2344
2345 template
2346 static const char *parse_expr_primary(const char *first, const char *last,
2347 C &db) {
2348 if (last - first >= 4 && *first == 'L') {
2349 switch (first[1]) {
2350 case 'w': {
2351 const char *t = parse_integer_literal(first + 2, last, "wchar_t", db);
2352 if (t != first + 2)
2353 first = t;
2354 } break;
2355 case 'b':
2356 if (first[3] == 'E') {
2357 switch (first[2]) {
2358 case '0':
2359 db.names.push_back("false");
2360 first += 4;
2361 break;
2362 case '1':
2363 db.names.push_back("true");
2364 first += 4;
2365 break;
2366 }
2367 }
2368 break;
2369 case 'c': {
2370 const char *t = parse_integer_literal(first + 2, last, "char", db);
2371 if (t != first + 2)
2372 first = t;
2373 } break;
2374 case 'a': {
2375 const char *t = parse_integer_literal(first + 2, last, "signed char", db);
2376 if (t != first + 2)
2377 first = t;
2378 } break;
2379 case 'h': {
2380 const char *t =
2381 parse_integer_literal(first + 2, last, "unsigned char", db);
2382 if (t != first + 2)
2383 first = t;
2384 } break;
2385 case 's': {
2386 const char *t = parse_integer_literal(first + 2, last, "short", db);
2387 if (t != first + 2)
2388 first = t;
2389 } break;
2390 case 't': {
2391 const char *t =
2392 parse_integer_literal(first + 2, last, "unsigned short", db);
2393 if (t != first + 2)
2394 first = t;
2395 } break;
2396 case 'i': {
2397 const char *t = parse_integer_literal(first + 2, last, "", db);
2398 if (t != first + 2)
2399 first = t;
2400 } break;
2401 case 'j': {
2402 const char *t = parse_integer_literal(first + 2, last, "u", db);
2403 if (t != first + 2)
2404 first = t;
2405 } break;
2406 case 'l': {
2407 const char *t = parse_integer_literal(first + 2, last, "l", db);
2408 if (t != first + 2)
2409 first = t;
2410 } break;
2411 case 'm': {
2412 const char *t = parse_integer_literal(first + 2, last, "ul", db);
2413 if (t != first + 2)
2414 first = t;
2415 } break;
2416 case 'x': {
2417 const char *t = parse_integer_literal(first + 2, last, "ll", db);
2418 if (t != first + 2)
2419 first = t;
2420 } break;
2421 case 'y': {
2422 const char *t = parse_integer_literal(first + 2, last, "ull", db);
2423 if (t != first + 2)
2424 first = t;
2425 } break;
2426 case 'n': {
2427 const char *t = parse_integer_literal(first + 2, last, "__int128", db);
2428 if (t != first + 2)
2429 first = t;
2430 } break;
2431 case 'o': {
2432 const char *t =
2433 parse_integer_literal(first + 2, last, "unsigned __int128", db);
2434 if (t != first + 2)
2435 first = t;
2436 } break;
2437 case 'f': {
2438 const char *t = parse_floating_number(first + 2, last, db);
2439 if (t != first + 2)
2440 first = t;
2441 } break;
2442 case 'd': {
2443 const char *t = parse_floating_number(first + 2, last, db);
2444 if (t != first + 2)
2445 first = t;
2446 } break;
2447 case 'e': {
2448 const char *t = parse_floating_number(first + 2, last, db);
2449 if (t != first + 2)
2450 first = t;
2451 } break;
2452 case '_':
2453 if (first[2] == 'Z') {
2454 const char *t = parse_encoding(first + 3, last, db);
2455 if (t != first + 3 && t != last && *t == 'E')
2456 first = t + 1;
2457 }
2458 break;
2459 case 'T':
2460 // Invalid mangled name per
2461 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
2462 break;
2463 default: {
2464 // might be named type
2465 const char *t = parse_type(first + 1, last, db);
2466 if (t != first + 1 && t != last) {
2467 if (*t != 'E') {
2468 const char *n = t;
2469 for (; n != last && isdigit(*n); ++n)
2470 ;
2471 if (n != t && n != last && *n == 'E') {
2472 if (db.names.empty())
2473 return first;
2474 db.names.back() = "(" + db.names.back().move_full() + ")" +
2475 typename C::String(t, n);
2476 first = n + 1;
2477 break;
2478 }
2479 } else {
2480 first = t + 1;
2481 break;
2482 }
2483 }
2484 }
2485 }
2486 }
2487 return first;
2488 }
2489
2490 template static String base_name(String &s) {
2491 if (s.empty())
2492 return s;
2493 if (s == "std::string") {
2494 s = "std::basic_string, std::allocator "
2495 ">";
2496 return "basic_string";
2497 }
2498 if (s == "std::istream") {
2499 s = "std::basic_istream >";
2500 return "basic_istream";
2501 }
2502 if (s == "std::ostream") {
2503 s = "std::basic_ostream >";
2504 return "basic_ostream";
2505 }
2506 if (s == "std::iostream") {
2507 s = "std::basic_iostream >";
2508 return "basic_iostream";
2509 }
2510 const char *const pf = s.data();
2511 const char *pe = pf + s.size();
2512 if (pe[-1] == '>') {
2513 unsigned c = 1;
2514 while (true) {
2515 if (--pe == pf)
2516 return String();
2517 if (pe[-1] == '<') {
2518 if (--c == 0) {
2519 --pe;
2520 break;
2521 }
2522 } else if (pe[-1] == '>')
2523 ++c;
2524 }
2525 }
2526 if (pe - pf <= 1)
2527 return String();
2528 const char *p0 = pe - 1;
2529 for (; p0 != pf; --p0) {
2530 if (*p0 == ':') {
2531 ++p0;
2532 break;
2533 }
2534 }
2535 return String(p0, pe);
2536 }
2537
2538 // ::= C1 # complete object constructor
2539 // ::= C2 # base object constructor
2540 // ::= C3 # complete object allocating constructor
2541 // extension ::= C5 # ?
2542 // ::= D0 # deleting destructor
2543 // ::= D1 # complete object destructor
2544 // ::= D2 # base object destructor
2545 // extension ::= D5 # ?
2546
2547 template
2548 static const char *parse_ctor_dtor_name(const char *first, const char *last,
2549 C &db) {
2550 if (last - first >= 2 && !db.names.empty()) {
2551 switch (first[0]) {
2552 case 'C':
2553 switch (first[1]) {
2554 case '1':
2555 case '2':
2556 case '3':
2557 case '5':
2558 if (db.names.empty())
2559 return first;
2560 db.names.push_back(base_name(db.names.back().first));
2561 first += 2;
2562 db.parsed_ctor_dtor_cv = true;
2563 break;
2564 }
2565 break;
2566 case 'D':
2567 switch (first[1]) {
2568 case '0':
2569 case '1':
2570 case '2':
2571 case '5':
2572 if (db.names.empty())
2573 return first;
2574 db.names.push_back("~" + base_name(db.names.back().first));
2575 first += 2;
2576 db.parsed_ctor_dtor_cv = true;
2577 break;
2578 }
2579 break;
2580 }
2581 }
2582 return first;
2583 }
2584
2585 // ::= Ut [ ] _
2586 // ::=
2587 //
2588 // ::= Ul E [ ] _
2589 //
2590 // ::= + # Parameter types or "v" if the lambda
2591 // has no parameters
2592
2593 template
2594 static const char *parse_unnamed_type_name(const char *first, const char *last,
2595 C &db) {
2596 if (last - first > 2 && first[0] == 'U') {
2597 char type = first[1];
2598 switch (type) {
2599 case 't': {
2600 db.names.push_back(typename C::String("'unnamed"));
2601 const char *t0 = first + 2;
2602 if (t0 == last) {
2603 db.names.pop_back();
2604 return first;
2605 }
2606 if (std::isdigit(*t0)) {
2607 const char *t1 = t0 + 1;
2608 while (t1 != last && std::isdigit(*t1))
2609 ++t1;
2610 db.names.back().first.append(t0, t1);
2611 t0 = t1;
2612 }
2613 db.names.back().first.push_back('\'');
2614 if (t0 == last || *t0 != '_') {
2615 db.names.pop_back();
2616 return first;
2617 }
2618 first = t0 + 1;
2619 } break;
2620 case 'l': {
2621 db.names.push_back(typename C::String("'lambda'("));
2622 const char *t0 = first + 2;
2623 if (first[2] == 'v') {
2624 db.names.back().first += ')';
2625 ++t0;
2626 } else {
2627 const char *t1 = parse_type(t0, last, db);
2628 if (t1 == t0) {
2629 if (!db.names.empty())
2630 db.names.pop_back();
2631 return first;
2632 }
2633 if (db.names.size() < 2)
2634 return first;
2635 auto tmp = db.names.back().move_full();
2636 db.names.pop_back();
2637 db.names.back().first.append(tmp);
2638 t0 = t1;
2639 while (true) {
2640 t1 = parse_type(t0, last, db);
2641 if (t1 == t0)
2642 break;
2643 if (db.names.size() < 2)
2644 return first;
2645 tmp = db.names.back().move_full();
2646 db.names.pop_back();
2647 if (!tmp.empty()) {
2648 db.names.back().first.append(", ");
2649 db.names.back().first.append(tmp);
2650 }
2651 t0 = t1;
2652 }
2653 if (db.names.empty())
2654 return first;
2655 db.names.back().first.append(")");
2656 }
2657 if (t0 == last || *t0 != 'E') {
2658 if (!db.names.empty())
2659 db.names.pop_back();
2660 return first;
2661 }
2662 ++t0;
2663 if (t0 == last) {
2664 if (!db.names.empty())
2665 db.names.pop_back();
2666 return first;
2667 }
2668 if (std::isdigit(*t0)) {
2669 const char *t1 = t0 + 1;
2670 while (t1 != last && std::isdigit(*t1))
2671 ++t1;
2672 db.names.back().first.insert(db.names.back().first.begin() + 7, t0, t1);
2673 t0 = t1;
2674 }
2675 if (t0 == last || *t0 != '_') {
2676 if (!db.names.empty())
2677 db.names.pop_back();
2678 return first;
2679 }
2680 first = t0 + 1;
2681 } break;
2682 }
2683 }
2684 return first;
2685 }
2686
2687 // ::=
2688 // ::=
2689 // ::=
2690 // ::=
2691
2692 template
2693 static const char *parse_unqualified_name(const char *first, const char *last,
2694 C &db) {
2695 if (first != last) {
2696 const char *t;
2697 switch (*first) {
2698 case 'C':
2699 case 'D':
2700 t = parse_ctor_dtor_name(first, last, db);
2701 if (t != first)
2702 first = t;
2703 break;
2704 case 'U':
2705 t = parse_unnamed_type_name(first, last, db);
2706 if (t != first)
2707 first = t;
2708 break;
2709 case '1':
2710 case '2':
2711 case '3':
2712 case '4':
2713 case '5':
2714 case '6':
2715 case '7':
2716 case '8':
2717 case '9':
2718 t = parse_source_name(first, last, db);
2719 if (t != first)
2720 first = t;
2721 break;
2722 default:
2723 t = parse_operator_name(first, last, db);
2724 if (t != first)
2725 first = t;
2726 break;
2727 };
2728 }
2729 return first;
2730 }
2731
2732 // ::=
2733 // ::= St # ::std::
2734 // extension ::= StL
2735
2736 template
2737 static const char *parse_unscoped_name(const char *first, const char *last,
2738 C &db) {
2739 if (last - first >= 2) {
2740 const char *t0 = first;
2741 bool St = false;
2742 if (first[0] == 'S' && first[1] == 't') {
2743 t0 += 2;
2744 St = true;
2745 if (t0 != last && *t0 == 'L')
2746 ++t0;
2747 }
2748 const char *t1 = parse_unqualified_name(t0, last, db);
2749 if (t1 != t0) {
2750 if (St) {
2751 if (db.names.empty())
2752 return first;
2753 db.names.back().first.insert(0, "std::");
2754 }
2755 first = t1;
2756 }
2757 }
2758 return first;
2759 }
2760
2761 // at # alignof (a type)
2762
2763 template
2764 static const char *parse_alignof_type(const char *first, const char *last,
2765 C &db) {
2766 if (last - first >= 3 && first[0] == 'a' && first[1] == 't') {
2767 const char *t = parse_type(first + 2, last, db);
2768 if (t != first + 2) {
2769 if (db.names.empty())
2770 return first;
2771 db.names.back().first = "alignof (" + db.names.back().move_full() + ")";
2772 first = t;
2773 }
2774 }
2775 return first;
2776 }
2777
2778 // az # alignof (a
2779 // expression)
2780
2781 template
2782 static const char *parse_alignof_expr(const char *first, const char *last,
2783 C &db) {
2784 if (last - first >= 3 && first[0] == 'a' && first[1] == 'z') {
2785 const char *t = parse_expression(first + 2, last, db);
2786 if (t != first + 2) {
2787 if (db.names.empty())
2788 return first;
2789 db.names.back().first = "alignof (" + db.names.back().move_full() + ")";
2790 first = t;
2791 }
2792 }
2793 return first;
2794 }
2795
2796 template
2797 static const char *parse_noexcept_expression(const char *first,
2798 const char *last, C &db) {
2799 const char *t1 = parse_expression(first, last, db);
2800 if (t1 != first) {
2801 if (db.names.empty())
2802 return first;
2803 db.names.back().first = "noexcept (" + db.names.back().move_full() + ")";
2804 first = t1;
2805 }
2806 return first;
2807 }
2808
2809 template
2810 static const char *parse_prefix_expression(const char *first, const char *last,
2811 const typename C::String &op,
2812 C &db) {
2813 const char *t1 = parse_expression(first, last, db);
2814 if (t1 != first) {
2815 if (db.names.empty())
2816 return first;
2817 db.names.back().first = op + "(" + db.names.back().move_full() + ")";
2818 first = t1;
2819 }
2820 return first;
2821 }
2822
2823 template
2824 static const char *parse_binary_expression(const char *first, const char *last,
2825 const typename C::String &op,
2826 C &db) {
2827 const char *t1 = parse_expression(first, last, db);
2828 if (t1 != first) {
2829 const char *t2 = parse_expression(t1, last, db);
2830 if (t2 != t1) {
2831 if (db.names.size() < 2)
2832 return first;
2833 auto op2 = db.names.back().move_full();
2834 db.names.pop_back();
2835 auto op1 = db.names.back().move_full();
2836 auto &nm = db.names.back().first;
2837 nm.clear();
2838 if (op == ">")
2839 nm += '(';
2840 nm += "(" + op1 + ") " + op + " (" + op2 + ")";
2841 if (op == ">")
2842 nm += ')';
2843 first = t2;
2844 } else if (!db.names.empty())
2845 db.names.pop_back();
2846 }
2847 return first;
2848 }
2849
2850 // ::=
2851 // ::=
2852 // ::=
2853 //
2854 // ::= cl + E # call
2855 // ::= cv #
2856 // conversion with one argument
2857 // ::= cv _ * E #
2858 // conversion with a different number of arguments
2859 // ::= [gs] nw * _ E # new
2860 // (expr-list) type
2861 // ::= [gs] nw * _ # new
2862 // (expr-list) type (init)
2863 // ::= [gs] na * _ E # new[]
2864 // (expr-list) type
2865 // ::= [gs] na * _ # new[]
2866 // (expr-list) type (init)
2867 // ::= [gs] dl #
2868 // delete expression
2869 // ::= [gs] da #
2870 // delete[] expression
2871 // ::= pp_ #
2872 // prefix ++
2873 // ::= mm_ #
2874 // prefix --
2875 // ::= ti #
2876 // typeid (type)
2877 // ::= te #
2878 // typeid (expression)
2879 // ::= dc #
2880 // dynamic_cast (expression)
2881 // ::= sc #
2882 // static_cast (expression)
2883 // ::= cc #
2884 // const_cast (expression)
2885 // ::= rc #
2886 // reinterpret_cast (expression)
2887 // ::= st #
2888 // sizeof (a type)
2889 // ::= sz #
2890 // sizeof (an expression)
2891 // ::= at #
2892 // alignof (a type)
2893 // ::= az #
2894 // alignof (an expression)
2895 // ::= nx #
2896 // noexcept (expression)
2897 // ::=
2898 // ::=
2899 // ::= dt #
2900 // expr.name
2901 // ::= pt #
2902 // expr->name
2903 // ::= ds #
2904 // expr.*expr
2905 // ::= sZ # size
2906 // of a parameter pack
2907 // ::= sZ # size
2908 // of a function parameter pack
2909 // ::= sp # pack
2910 // expansion
2911 // ::= tw # throw
2912 // expression
2913 // ::= tr # throw
2914 // with no operand (rethrow)
2915 // ::= # f(p),
2916 // N::f(p), ::f(p),
2917 // #
2918 // freestanding
2919 // dependent
2920 // name
2921 // (e.g.,
2922 // T::x),
2923 // #
2924 // objectless
2925 // nonstatic
2926 // member
2927 // reference
2928 // ::=
2929
2930 template
2931 static const char *parse_expression(const char *first, const char *last,
2932 C &db) {
2933 if (last - first >= 2) {
2934 const char *t = first;
2935 bool parsed_gs = false;
2936 if (last - first >= 4 && t[0] == 'g' && t[1] == 's') {
2937 t += 2;
2938 parsed_gs = true;
2939 }
2940 switch (*t) {
2941 case 'L':
2942 first = parse_expr_primary(first, last, db);
2943 break;
2944 case 'T':
2945 first = parse_template_param(first, last, db);
2946 break;
2947 case 'f':
2948 first = parse_function_param(first, last, db);
2949 break;
2950 case 'a':
2951 switch (t[1]) {
2952 case 'a':
2953 t = parse_binary_expression(first + 2, last, "&&", db);
2954 if (t != first + 2)
2955 first = t;
2956 break;
2957 case 'd':
2958 t = parse_prefix_expression(first + 2, last, "&", db);
2959 if (t != first + 2)
2960 first = t;
2961 break;
2962 case 'n':
2963 t = parse_binary_expression(first + 2, last, "&", db);
2964 if (t != first + 2)
2965 first = t;
2966 break;
2967 case 'N':
2968 t = parse_binary_expression(first + 2, last, "&=", db);
2969 if (t != first + 2)
2970 first = t;
2971 break;
2972 case 'S':
2973 t = parse_binary_expression(first + 2, last, "=", db);
2974 if (t != first + 2)
2975 first = t;
2976 break;
2977 case 't':
2978 first = parse_alignof_type(first, last, db);
2979 break;
2980 case 'z':
2981 first = parse_alignof_expr(first, last, db);
2982 break;
2983 }
2984 break;
2985 case 'c':
2986 switch (t[1]) {
2987 case 'c':
2988 first = parse_const_cast_expr(first, last, db);
2989 break;
2990 case 'l':
2991 first = parse_call_expr(first, last, db);
2992 break;
2993 case 'm':
2994 t = parse_binary_expression(first + 2, last, ",", db);
2995 if (t != first + 2)
2996 first = t;
2997 break;
2998 case 'o':
2999 t = parse_prefix_expression(first + 2, last, "~", db);
3000 if (t != first + 2)
3001 first = t;
3002 break;
3003 case 'v':
3004 first = parse_conversion_expr(first, last, db);
3005 break;
3006 }
3007 break;
3008 case 'd':
3009 switch (t[1]) {
3010 case 'a': {
3011 const char *t1 = parse_expression(t + 2, last, db);
3012 if (t1 != t + 2) {
3013 if (db.names.empty())
3014 return first;
3015 db.names.back().first =
3016 (parsed_gs ? typename C::String("::") : typename C::String()) +
3017 "delete[] " + db.names.back().move_full();
3018 first = t1;
3019 }
3020 } break;
3021 case 'c':
3022 first = parse_dynamic_cast_expr(first, last, db);
3023 break;
3024 case 'e':
3025 t = parse_prefix_expression(first + 2, last, "*", db);
3026 if (t != first + 2)
3027 first = t;
3028 break;
3029 case 'l': {
3030 const char *t1 = parse_expression(t + 2, last, db);
3031 if (t1 != t + 2) {
3032 if (db.names.empty())
3033 return first;
3034 db.names.back().first =
3035 (parsed_gs ? typename C::String("::") : typename C::String()) +
3036 "delete " + db.names.back().move_full();
3037 first = t1;
3038 }
3039 } break;
3040 case 'n':
3041 return parse_unresolved_name(first, last, db);
3042 case 's':
3043 first = parse_dot_star_expr(first, last, db);
3044 break;
3045 case 't':
3046 first = parse_dot_expr(first, last, db);
3047 break;
3048 case 'v':
3049 t = parse_binary_expression(first + 2, last, "/", db);
3050 if (t != first + 2)
3051 first = t;
3052 break;
3053 case 'V':
3054 t = parse_binary_expression(first + 2, last, "/=", db);
3055 if (t != first + 2)
3056 first = t;
3057 break;
3058 }
3059 break;
3060 case 'e':
3061 switch (t[1]) {
3062 case 'o':
3063 t = parse_binary_expression(first + 2, last, "^", db);
3064 if (t != first + 2)
3065 first = t;
3066 break;
3067 case 'O':
3068 t = parse_binary_expression(first + 2, last, "^=", db);
3069 if (t != first + 2)
3070 first = t;
3071 break;
3072 case 'q':
3073 t = parse_binary_expression(first + 2, last, "==", db);
3074 if (t != first + 2)
3075 first = t;
3076 break;
3077 }
3078 break;
3079 case 'g':
3080 switch (t[1]) {
3081 case 'e':
3082 t = parse_binary_expression(first + 2, last, ">=", db);
3083 if (t != first + 2)
3084 first = t;
3085 break;
3086 case 't':
3087 t = parse_binary_expression(first + 2, last, ">", db);
3088 if (t != first + 2)
3089 first = t;
3090 break;
3091 }
3092 break;
3093 case 'i':
3094 if (t[1] == 'x') {
3095 const char *t1 = parse_expression(first + 2, last, db);
3096 if (t1 != first + 2) {
3097 const char *t2 = parse_expression(t1, last, db);
3098 if (t2 != t1) {
3099 if (db.names.size() < 2)
3100 return first;
3101 auto op2 = db.names.back().move_full();
3102 db.names.pop_back();
3103 auto op1 = db.names.back().move_full();
3104 db.names.back() = "(" + op1 + ")[" + op2 + "]";
3105 first = t2;
3106 } else if (!db.names.empty())
3107 db.names.pop_back();
3108 }
3109 }
3110 break;
3111 case 'l':
3112 switch (t[1]) {
3113 case 'e':
3114 t = parse_binary_expression(first + 2, last, "<=", db);
3115 if (t != first + 2)
3116 first = t;
3117 break;
3118 case 's':
3119 t = parse_binary_expression(first + 2, last, "<<", db);
3120 if (t != first + 2)
3121 first = t;
3122 break;
3123 case 'S':
3124 t = parse_binary_expression(first + 2, last, "<<=", db);
3125 if (t != first + 2)
3126 first = t;
3127 break;
3128 case 't':
3129 t = parse_binary_expression(first + 2, last, "<", db);
3130 if (t != first + 2)
3131 first = t;
3132 break;
3133 }
3134 break;
3135 case 'm':
3136 switch (t[1]) {
3137 case 'i':
3138 t = parse_binary_expression(first + 2, last, "-", db);
3139 if (t != first + 2)
3140 first = t;
3141 break;
3142 case 'I':
3143 t = parse_binary_expression(first + 2, last, "-=", db);
3144 if (t != first + 2)
3145 first = t;
3146 break;
3147 case 'l':
3148 t = parse_binary_expression(first + 2, last, "*", db);
3149 if (t != first + 2)
3150 first = t;
3151 break;
3152 case 'L':
3153 t = parse_binary_expression(first + 2, last, "*=", db);
3154 if (t != first + 2)
3155 first = t;
3156 break;
3157 case 'm':
3158 if (first + 2 != last && first[2] == '_') {
3159 t = parse_prefix_expression(first + 3, last, "--", db);
3160 if (t != first + 3)
3161 first = t;
3162 } else {
3163 const char *t1 = parse_expression(first + 2, last, db);
3164 if (t1 != first + 2) {
3165 if (db.names.empty())
3166 return first;
3167 db.names.back() = "(" + db.names.back().move_full() + ")--";
3168 first = t1;
3169 }
3170 }
3171 break;
3172 }
3173 break;
3174 case 'n':
3175 switch (t[1]) {
3176 case 'a':
3177 case 'w':
3178 first = parse_new_expr(first, last, db);
3179 break;
3180 case 'e':
3181 t = parse_binary_expression(first + 2, last, "!=", db);
3182 if (t != first + 2)
3183 first = t;
3184 break;
3185 case 'g':
3186 t = parse_prefix_expression(first + 2, last, "-", db);
3187 if (t != first + 2)
3188 first = t;
3189 break;
3190 case 't':
3191 t = parse_prefix_expression(first + 2, last, "!", db);
3192 if (t != first + 2)
3193 first = t;
3194 break;
3195 case 'x':
3196 t = parse_noexcept_expression(first + 2, last, db);
3197 if (t != first + 2)
3198 first = t;
3199 break;
3200 }
3201 break;
3202 case 'o':
3203 switch (t[1]) {
3204 case 'n':
3205 return parse_unresolved_name(first, last, db);
3206 case 'o':
3207 t = parse_binary_expression(first + 2, last, "||", db);
3208 if (t != first + 2)
3209 first = t;
3210 break;
3211 case 'r':
3212 t = parse_binary_expression(first + 2, last, "|", db);
3213 if (t != first + 2)
3214 first = t;
3215 break;
3216 case 'R':
3217 t = parse_binary_expression(first + 2, last, "|=", db);
3218 if (t != first + 2)
3219 first = t;
3220 break;
3221 }
3222 break;
3223 case 'p':
3224 switch (t[1]) {
3225 case 'm':
3226 t = parse_binary_expression(first + 2, last, "->*", db);
3227 if (t != first + 2)
3228 first = t;
3229 break;
3230 case 'l':
3231 t = parse_binary_expression(first + 2, last, "+", db);
3232 if (t != first + 2)
3233 first = t;
3234 break;
3235 case 'L':
3236 t = parse_binary_expression(first + 2, last, "+=", db);
3237 if (t != first + 2)
3238 first = t;
3239 break;
3240 case 'p':
3241 if (first + 2 != last && first[2] == '_') {
3242 t = parse_prefix_expression(first + 3, last, "++", db);
3243 if (t != first + 3)
3244 first = t;
3245 } else {
3246 const char *t1 = parse_expression(first + 2, last, db);
3247 if (t1 != first + 2) {
3248 if (db.names.empty())
3249 return first;
3250 db.names.back() = "(" + db.names.back().move_full() + ")++";
3251 first = t1;
3252 }
3253 }
3254 break;
3255 case 's':
3256 t = parse_prefix_expression(first + 2, last, "+", db);
3257 if (t != first + 2)
3258 first = t;
3259 break;
3260 case 't':
3261 first = parse_arrow_expr(first, last, db);
3262 break;
3263 }
3264 break;
3265 case 'q':
3266 if (t[1] == 'u') {
3267 const char *t1 = parse_expression(first + 2, last, db);
3268 if (t1 != first + 2) {
3269 const char *t2 = parse_expression(t1, last, db);
3270 if (t2 != t1) {
3271 const char *t3 = parse_expression(t2, last, db);
3272 if (t3 != t2) {
3273 if (db.names.size() < 3)
3274 return first;
3275 auto op3 = db.names.back().move_full();
3276 db.names.pop_back();
3277 auto op2 = db.names.back().move_full();
3278 db.names.pop_back();
3279 auto op1 = db.names.back().move_full();
3280 db.names.back() = "(" + op1 + ") ? (" + op2 + ") : (" + op3 + ")";
3281 first = t3;
3282 } else {
3283 if (db.names.size() < 2)
3284 return first;
3285 db.names.pop_back();
3286 db.names.pop_back();
3287 }
3288 } else if (!db.names.empty())
3289 db.names.pop_back();
3290 }
3291 }
3292 break;
3293 case 'r':
3294 switch (t[1]) {
3295 case 'c':
3296 first = parse_reinterpret_cast_expr(first, last, db);
3297 break;
3298 case 'm':
3299 t = parse_binary_expression(first + 2, last, "%", db);
3300 if (t != first + 2)
3301 first = t;
3302 break;
3303 case 'M':
3304 t = parse_binary_expression(first + 2, last, "%=", db);
3305 if (t != first + 2)
3306 first = t;
3307 break;
3308 case 's':
3309 t = parse_binary_expression(first + 2, last, ">>", db);
3310 if (t != first + 2)
3311 first = t;
3312 break;
3313 case 'S':
3314 t = parse_binary_expression(first + 2, last, ">>=", db);
3315 if (t != first + 2)
3316 first = t;
3317 break;
3318 }
3319 break;
3320 case 's':
3321 switch (t[1]) {
3322 case 'c':
3323 first = parse_static_cast_expr(first, last, db);
3324 break;
3325 case 'p':
3326 first = parse_pack_expansion(first, last, db);
3327 break;
3328 case 'r':
3329 return parse_unresolved_name(first, last, db);
3330 case 't':
3331 first = parse_sizeof_type_expr(first, last, db);
3332 break;
3333 case 'z':
3334 first = parse_sizeof_expr_expr(first, last, db);
3335 break;
3336 case 'Z':
3337 if (last - t >= 3) {
3338 switch (t[2]) {
3339 case 'T':
3340 first = parse_sizeof_param_pack_expr(first, last, db);
3341 break;
3342 case 'f':
3343 first = parse_sizeof_function_param_pack_expr(first, last, db);
3344 break;
3345 }
3346 }
3347 break;
3348 }
3349 break;
3350 case 't':
3351 switch (t[1]) {
3352 case 'e':
3353 case 'i':
3354 first = parse_typeid_expr(first, last, db);
3355 break;
3356 case 'r':
3357 db.names.push_back("throw");
3358 first += 2;
3359 break;
3360 case 'w':
3361 first = parse_throw_expr(first, last, db);
3362 break;
3363 }
3364 break;
3365 case '1':
3366 case '2':
3367 case '3':
3368 case '4':
3369 case '5':
3370 case '6':
3371 case '7':
3372 case '8':
3373 case '9':
3374 return parse_unresolved_name(first, last, db);
3375 }
3376 }
3377 return first;
3378 }
3379
3380 // ::= # type
3381 // or template
3382 // ::= X E #
3383 // expression
3384 // ::= #
3385 // simple expressions
3386 // ::= J * E #
3387 // argument pack
3388 // ::= LZ E #
3389 // extension
3390
3391 template
3392 static const char *parse_template_arg(const char *first, const char *last,
3393 C &db) {
3394 if (first != last) {
3395 const char *t;
3396 switch (*first) {
3397 case 'X':
3398 t = parse_expression(first + 1, last, db);
3399 if (t != first + 1) {
3400 if (t != last && *t == 'E')
3401 first = t + 1;
3402 }
3403 break;
3404 case 'J':
3405 t = first + 1;
3406 if (t == last)
3407 return first;
3408 while (*t != 'E') {
3409 const char *t1 = parse_template_arg(t, last, db);
3410 if (t1 == t)
3411 return first;
3412 t = t1;
3413 }
3414 first = t + 1;
3415 break;
3416 case 'L':
3417 // or LZ E
3418 if (first + 1 != last && first[1] == 'Z') {
3419 t = parse_encoding(first + 2, last, db);
3420 if (t != first + 2 && t != last && *t == 'E')
3421 first = t + 1;
3422 } else
3423 first = parse_expr_primary(first, last, db);
3424 break;
3425 default:
3426 //
3427 first = parse_type(first, last, db);
3428 break;
3429 }
3430 }
3431 return first;
3432 }
3433
3434 // ::= I * E
3435 // extension, the abi says +
3436
3437 template
3438 static const char *parse_template_args(const char *first, const char *last,
3439 C &db) {
3440 if (last - first >= 2 && *first == 'I') {
3441 if (db.tag_templates)
3442 db.template_param.back().clear();
3443 const char *t = first + 1;
3444 typename C::String args("<");
3445 while (*t != 'E') {
3446 if (db.tag_templates)
3447 db.template_param.emplace_back(db.names.get_allocator());
3448 size_t k0 = db.names.size();
3449 const char *t1 = parse_template_arg(t, last, db);
3450 size_t k1 = db.names.size();
3451 if (db.tag_templates)
3452 db.template_param.pop_back();
3453 if (t1 == t || t1 == last)
3454 return first;
3455 if (db.tag_templates) {
3456 db.template_param.back().emplace_back(db.names.get_allocator());
3457 for (size_t k = k0; k < k1; ++k)
3458 db.template_param.back().back().push_back(db.names[k]);
3459 }
3460 for (size_t k = k0; k < k1; ++k) {
3461 if (args.size() > 1)
3462