1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: Satoru Takabayashi
32 // For reference check out:
33 // http://www.codesourcery.com/public/cxx-abi/abi.html#mangling
35 // Note that we only have partial C++0x support yet.
37 #include <stdio.h> // for NULL
40 _START_GOOGLE_NAMESPACE_
44 const char *real_name;
47 // List of operators from Itanium C++ ABI.
48 static const AbbrevPair kOperatorList[] = {
101 // List of builtin types from Itanium C++ ABI.
102 static const AbbrevPair kBuiltinTypeList[] = {
107 { "a", "signed char" },
108 { "h", "unsigned char" },
110 { "t", "unsigned short" },
112 { "j", "unsigned int" },
114 { "m", "unsigned long" },
115 { "x", "long long" },
116 { "y", "unsigned long long" },
118 { "o", "unsigned __int128" },
121 { "e", "long double" },
122 { "g", "__float128" },
127 // List of substitutions Itanium C++ ABI.
128 static const AbbrevPair kSubstitutionList[] = {
130 { "Sa", "allocator" },
131 { "Sb", "basic_string" },
132 // std::basic_string<char, std::char_traits<char>,std::allocator<char> >
134 // std::basic_istream<char, std::char_traits<char> >
136 // std::basic_ostream<char, std::char_traits<char> >
138 // std::basic_iostream<char, std::char_traits<char> >
139 { "Sd", "iostream" },
143 // State needed for demangling.
145 const char *mangled_cur; // Cursor of mangled name.
146 const char *mangled_end; // End of mangled name.
147 char *out_cur; // Cursor of output string.
148 const char *out_begin; // Beginning of output string.
149 const char *out_end; // End of output string.
150 const char *prev_name; // For constructors/destructors.
151 int prev_name_length; // For constructors/destructors.
152 int nest_level; // For nested names.
153 int number; // Remember the previous number.
154 bool append; // Append flag.
155 bool overflowed; // True if output gets overflowed.
158 // We don't use strlen() in libc since it's not guaranteed to be async
160 static size_t StrLen(const char *str) {
162 while (*str != '\0') {
169 // Returns true if "str" has "prefix" as a prefix.
170 static bool StrPrefix(const char *str, const char *prefix) {
172 while (str[i] != '\0' && prefix[i] != '\0' &&
173 str[i] == prefix[i]) {
176 return prefix[i] == '\0'; // Consumed everything in "prefix".
179 static void InitState(State *state, const char *mangled,
180 char *out, int out_size) {
181 state->mangled_cur = mangled;
182 state->mangled_end = mangled + StrLen(mangled);
183 state->out_cur = out;
184 state->out_begin = out;
185 state->out_end = out + out_size;
186 state->prev_name = NULL;
187 state->prev_name_length = -1;
188 state->nest_level = -1;
190 state->append = true;
191 state->overflowed = false;
194 // Calculates the remaining length of the mangled name.
195 static int RemainingLength(State *state) {
196 return state->mangled_end - state->mangled_cur;
199 // Returns true and advances "mangled_cur" if we find "c" at
200 // "mangled_cur" position.
201 static bool ParseChar(State *state, const char c) {
202 if (RemainingLength(state) >= 1 && *state->mangled_cur == c) {
203 ++state->mangled_cur;
209 // Returns true and advances "mangled_cur" if we find "two_chars" at
210 // "mangled_cur" position.
211 static bool ParseTwoChar(State *state, const char *two_chars) {
212 if (RemainingLength(state) >= 2 &&
213 state->mangled_cur[0] == two_chars[0] &&
214 state->mangled_cur[1] == two_chars[1]) {
215 state->mangled_cur += 2;
221 // Returns true and advances "mangled_cur" if we find any character in
222 // "char_class" at "mangled_cur" position.
223 static bool ParseCharClass(State *state, const char *char_class) {
224 if (state->mangled_cur == state->mangled_end) {
227 const char *p = char_class;
228 for (; *p != '\0'; ++p) {
229 if (*state->mangled_cur == *p) {
230 state->mangled_cur += 1;
237 // This function is used for handling an optional non-terminal.
238 static bool Optional(bool status) {
242 // This function is used for handling <non-terminal>+ syntax.
243 typedef bool (*ParseFunc)(State *);
244 static bool OneOrMore(ParseFunc parse_func, State *state) {
245 if (parse_func(state)) {
246 while (parse_func(state)) {
253 // This function is used for handling <non-terminal>* syntax. The function
254 // always returns true and must be followed by a termination symbol or a
255 // terminating sequence not handled by parse_func (e.g. ParseChar(state, 'E')).
256 static bool ZeroOrMore(ParseFunc parse_func, State *state) {
257 while (parse_func(state)) {
262 // Append "str" at "out_cur". If there is an overflow, "overflowed"
263 // is set to true for later use. The output string is ensured to
264 // always terminate with '\0' as long as there is no overflow.
265 static void Append(State *state, const char * const str, const int length) {
267 for (i = 0; i < length; ++i) {
268 if (state->out_cur + 1 < state->out_end) { // +1 for '\0'
269 *state->out_cur = str[i];
272 state->overflowed = true;
276 if (!state->overflowed) {
277 *state->out_cur = '\0'; // Terminate it with '\0'
281 // We don't use equivalents in libc to avoid locale issues.
282 static bool IsLower(char c) {
283 return c >= 'a' && c <= 'z';
286 static bool IsAlpha(char c) {
287 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
290 static bool IsDigit(char c) {
291 return c >= '0' && c <= '9';
294 // Returns true if "str" is a function clone suffix. These suffixes are used
295 // by GCC 4.5.x and later versions to indicate functions which have been
296 // cloned during optimization. We treat any sequence (.<alpha>+.<digit>+)+ as
297 // a function clone suffix.
298 static bool IsFunctionCloneSuffix(const char *str) {
300 while (str[i] != '\0') {
301 // Consume a single .<alpha>+.<digit>+ sequence.
302 if (str[i] != '.' || !IsAlpha(str[i + 1])) {
306 while (IsAlpha(str[i])) {
309 if (str[i] != '.' || !IsDigit(str[i + 1])) {
313 while (IsDigit(str[i])) {
317 return true; // Consumed everything in "str".
320 // Append "str" with some tweaks, iff "append" state is true.
321 // Returns true so that it can be placed in "if" conditions.
322 static void MaybeAppendWithLength(State *state, const char * const str,
324 if (state->append && length > 0) {
325 // Append a space if the output buffer ends with '<' and "str"
326 // starts with '<' to avoid <<<.
327 if (str[0] == '<' && state->out_begin < state->out_cur &&
328 state->out_cur[-1] == '<') {
329 Append(state, " ", 1);
331 // Remember the last identifier name for ctors/dtors.
332 if (IsAlpha(str[0]) || str[0] == '_') {
333 state->prev_name = state->out_cur;
334 state->prev_name_length = length;
336 Append(state, str, length);
340 // A convenient wrapper arount MaybeAppendWithLength().
341 static bool MaybeAppend(State *state, const char * const str) {
343 int length = StrLen(str);
344 MaybeAppendWithLength(state, str, length);
349 // This function is used for handling nested names.
350 static bool EnterNestedName(State *state) {
351 state->nest_level = 0;
355 // This function is used for handling nested names.
356 static bool LeaveNestedName(State *state, int prev_value) {
357 state->nest_level = prev_value;
361 // Disable the append mode not to print function parameters, etc.
362 static bool DisableAppend(State *state) {
363 state->append = false;
367 // Restore the append mode to the previous state.
368 static bool RestoreAppend(State *state, bool prev_value) {
369 state->append = prev_value;
373 // Increase the nest level for nested names.
374 static void MaybeIncreaseNestLevel(State *state) {
375 if (state->nest_level > -1) {
380 // Appends :: for nested names if necessary.
381 static void MaybeAppendSeparator(State *state) {
382 if (state->nest_level >= 1) {
383 MaybeAppend(state, "::");
387 // Cancel the last separator if necessary.
388 static void MaybeCancelLastSeparator(State *state) {
389 if (state->nest_level >= 1 && state->append &&
390 state->out_begin <= state->out_cur - 2) {
392 *state->out_cur = '\0';
396 // Returns true if identifier pointed by "mangled_cur" is anonymous
398 static bool IdentifierIsAnonymousNamespace(State *state) {
399 const char anon_prefix[] = "_GLOBAL__N_";
400 return (state->number > sizeof(anon_prefix) - 1 && // Should be longer.
401 StrPrefix(state->mangled_cur, anon_prefix));
404 // Forward declarations of our parsing functions.
405 static bool ParseMangledName(State *state);
406 static bool ParseEncoding(State *state);
407 static bool ParseName(State *state);
408 static bool ParseUnscopedName(State *state);
409 static bool ParseUnscopedTemplateName(State *state);
410 static bool ParseNestedName(State *state);
411 static bool ParsePrefix(State *state);
412 static bool ParseUnqualifiedName(State *state);
413 static bool ParseSourceName(State *state);
414 static bool ParseLocalSourceName(State *state);
415 static bool ParseNumber(State *state);
416 static bool ParseFloatNumber(State *state);
417 static bool ParseSeqId(State *state);
418 static bool ParseIdentifier(State *state);
419 static bool ParseOperatorName(State *state);
420 static bool ParseSpecialName(State *state);
421 static bool ParseCallOffset(State *state);
422 static bool ParseNVOffset(State *state);
423 static bool ParseVOffset(State *state);
424 static bool ParseCtorDtorName(State *state);
425 static bool ParseType(State *state);
426 static bool ParseCVQualifiers(State *state);
427 static bool ParseBuiltinType(State *state);
428 static bool ParseFunctionType(State *state);
429 static bool ParseBareFunctionType(State *state);
430 static bool ParseClassEnumType(State *state);
431 static bool ParseArrayType(State *state);
432 static bool ParsePointerToMemberType(State *state);
433 static bool ParseTemplateParam(State *state);
434 static bool ParseTemplateTemplateParam(State *state);
435 static bool ParseTemplateArgs(State *state);
436 static bool ParseTemplateArg(State *state);
437 static bool ParseExpression(State *state);
438 static bool ParseExprPrimary(State *state);
439 static bool ParseLocalName(State *state);
440 static bool ParseDiscriminator(State *state);
441 static bool ParseSubstitution(State *state);
443 // Implementation note: the following code is a straightforward
444 // translation of the Itanium C++ ABI defined in BNF with a couple of
447 // - Support GNU extensions not defined in the Itanium C++ ABI
448 // - <prefix> and <template-prefix> are combined to avoid infinite loop
449 // - Reorder patterns to shorten the code
450 // - Reorder patterns to give greedier functions precedence
451 // We'll mark "Less greedy than" for these cases in the code
453 // Each parsing function changes the state and returns true on
454 // success. Otherwise, don't change the state and returns false. To
455 // ensure that the state isn't changed in the latter case, we save the
456 // original state before we call more than one parsing functions
457 // consecutively with &&, and restore the state if unsuccessful. See
458 // ParseEncoding() as an example of this convention. We follow the
459 // convention throughout the code.
461 // Originally we tried to do demangling without following the full ABI
462 // syntax but it turned out we needed to follow the full syntax to
463 // parse complicated cases like nested template arguments. Note that
464 // implementing a full-fledged demangler isn't trivial (libiberty's
465 // cp-demangle.c has +4300 lines).
467 // Note that (foo) in <(foo) ...> is a modifier to be ignored.
471 // <http://www.codesourcery.com/cxx-abi/abi.html#mangling>
473 // <mangled-name> ::= _Z <encoding>
474 static bool ParseMangledName(State *state) {
475 if (ParseTwoChar(state, "_Z") && ParseEncoding(state)) {
476 // Drop trailing function clone suffix, if any.
477 if (IsFunctionCloneSuffix(state->mangled_cur)) {
478 state->mangled_cur = state->mangled_end;
480 // Append trailing version suffix if any.
481 // ex. _Z3foo@@GLIBCXX_3.4
482 if (state->mangled_cur < state->mangled_end &&
483 state->mangled_cur[0] == '@') {
484 MaybeAppend(state, state->mangled_cur);
485 state->mangled_cur = state->mangled_end;
492 // <encoding> ::= <(function) name> <bare-function-type>
494 // ::= <special-name>
495 static bool ParseEncoding(State *state) {
497 if (ParseName(state) && ParseBareFunctionType(state)) {
502 if (ParseName(state) || ParseSpecialName(state)) {
508 // <name> ::= <nested-name>
509 // ::= <unscoped-template-name> <template-args>
510 // ::= <unscoped-name>
512 static bool ParseName(State *state) {
513 if (ParseNestedName(state) || ParseLocalName(state)) {
518 if (ParseUnscopedTemplateName(state) &&
519 ParseTemplateArgs(state)) {
524 // Less greedy than <unscoped-template-name> <template-args>.
525 if (ParseUnscopedName(state)) {
531 // <unscoped-name> ::= <unqualified-name>
532 // ::= St <unqualified-name>
533 static bool ParseUnscopedName(State *state) {
534 if (ParseUnqualifiedName(state)) {
539 if (ParseTwoChar(state, "St") &&
540 MaybeAppend(state, "std::") &&
541 ParseUnqualifiedName(state)) {
548 // <unscoped-template-name> ::= <unscoped-name>
549 // ::= <substitution>
550 static bool ParseUnscopedTemplateName(State *state) {
551 return ParseUnscopedName(state) || ParseSubstitution(state);
554 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
555 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
556 static bool ParseNestedName(State *state) {
558 if (ParseChar(state, 'N') &&
559 EnterNestedName(state) &&
560 Optional(ParseCVQualifiers(state)) &&
561 ParsePrefix(state) &&
562 LeaveNestedName(state, copy.nest_level) &&
563 ParseChar(state, 'E')) {
570 // This part is tricky. If we literally translate them to code, we'll
571 // end up infinite loop. Hence we merge them to avoid the case.
573 // <prefix> ::= <prefix> <unqualified-name>
574 // ::= <template-prefix> <template-args>
575 // ::= <template-param>
576 // ::= <substitution>
578 // <template-prefix> ::= <prefix> <(template) unqualified-name>
579 // ::= <template-param>
580 // ::= <substitution>
581 static bool ParsePrefix(State *state) {
582 bool has_something = false;
584 MaybeAppendSeparator(state);
585 if (ParseTemplateParam(state) ||
586 ParseSubstitution(state) ||
587 ParseUnscopedName(state)) {
588 has_something = true;
589 MaybeIncreaseNestLevel(state);
592 MaybeCancelLastSeparator(state);
593 if (has_something && ParseTemplateArgs(state)) {
594 return ParsePrefix(state);
602 // <unqualified-name> ::= <operator-name>
603 // ::= <ctor-dtor-name>
605 // ::= <local-source-name>
606 static bool ParseUnqualifiedName(State *state) {
607 return (ParseOperatorName(state) ||
608 ParseCtorDtorName(state) ||
609 ParseSourceName(state) ||
610 ParseLocalSourceName(state));
613 // <source-name> ::= <positive length number> <identifier>
614 static bool ParseSourceName(State *state) {
616 if (ParseNumber(state) && ParseIdentifier(state)) {
623 // <local-source-name> ::= L <source-name> [<discriminator>]
626 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775
627 // http://gcc.gnu.org/viewcvs?view=rev&revision=124467
628 static bool ParseLocalSourceName(State *state) {
630 if (ParseChar(state, 'L') && ParseSourceName(state) &&
631 Optional(ParseDiscriminator(state))) {
638 // <number> ::= [n] <non-negative decimal integer>
639 static bool ParseNumber(State *state) {
641 if (ParseChar(state, 'n')) {
644 const char *p = state->mangled_cur;
646 for (;p < state->mangled_end; ++p) {
648 number = number * 10 + (*p - '0');
653 if (p != state->mangled_cur) { // Conversion succeeded.
654 state->mangled_cur = p;
655 state->number = number * sign;
661 // Floating-point literals are encoded using a fixed-length lowercase
662 // hexadecimal string.
663 static bool ParseFloatNumber(State *state) {
664 const char *p = state->mangled_cur;
666 for (;p < state->mangled_end; ++p) {
668 number = number * 16 + (*p - '0');
669 } else if (*p >= 'a' && *p <= 'f') {
670 number = number * 16 + (*p - 'a' + 10);
675 if (p != state->mangled_cur) { // Conversion succeeded.
676 state->mangled_cur = p;
677 state->number = number;
683 // The <seq-id> is a sequence number in base 36,
684 // using digits and upper case letters
685 static bool ParseSeqId(State *state) {
686 const char *p = state->mangled_cur;
688 for (;p < state->mangled_end; ++p) {
690 number = number * 36 + (*p - '0');
691 } else if (*p >= 'A' && *p <= 'Z') {
692 number = number * 36 + (*p - 'A' + 10);
697 if (p != state->mangled_cur) { // Conversion succeeded.
698 state->mangled_cur = p;
699 state->number = number;
705 // <identifier> ::= <unqualified source code identifier>
706 static bool ParseIdentifier(State *state) {
707 if (state->number == -1 ||
708 RemainingLength(state) < state->number) {
711 if (IdentifierIsAnonymousNamespace(state)) {
712 MaybeAppend(state, "(anonymous namespace)");
714 MaybeAppendWithLength(state, state->mangled_cur, state->number);
716 state->mangled_cur += state->number;
717 state->number = -1; // Reset the number.
721 // <operator-name> ::= nw, and other two letters cases
722 // ::= cv <type> # (cast)
723 // ::= v <digit> <source-name> # vendor extended operator
724 static bool ParseOperatorName(State *state) {
725 if (RemainingLength(state) < 2) {
728 // First check with "cv" (cast) case.
730 if (ParseTwoChar(state, "cv") &&
731 MaybeAppend(state, "operator ") &&
732 EnterNestedName(state) &&
734 LeaveNestedName(state, copy.nest_level)) {
739 // Then vendor extended operators.
740 if (ParseChar(state, 'v') && ParseCharClass(state, "0123456789") &&
741 ParseSourceName(state)) {
746 // Other operator names should start with a lower alphabet followed
747 // by a lower/upper alphabet.
748 if (!(IsLower(state->mangled_cur[0]) &&
749 IsAlpha(state->mangled_cur[1]))) {
752 // We may want to perform a binary search if we really need speed.
754 for (p = kOperatorList; p->abbrev != NULL; ++p) {
755 if (state->mangled_cur[0] == p->abbrev[0] &&
756 state->mangled_cur[1] == p->abbrev[1]) {
757 MaybeAppend(state, "operator");
758 if (IsLower(*p->real_name)) { // new, delete, etc.
759 MaybeAppend(state, " ");
761 MaybeAppend(state, p->real_name);
762 state->mangled_cur += 2;
769 // <special-name> ::= TV <type>
773 // ::= Tc <call-offset> <call-offset> <(base) encoding>
774 // ::= GV <(object) name>
775 // ::= T <call-offset> <(base) encoding>
777 // ::= TC <type> <(offset) number> _ <(base) type>
782 // ::= Th <call-offset> <(base) encoding>
783 // ::= Tv <call-offset> <(base) encoding>
785 // Note: we don't care much about them since they don't appear in
786 // stack traces. The are special data.
787 static bool ParseSpecialName(State *state) {
789 if (ParseChar(state, 'T') &&
790 ParseCharClass(state, "VTIS") &&
796 if (ParseTwoChar(state, "Tc") && ParseCallOffset(state) &&
797 ParseCallOffset(state) && ParseEncoding(state)) {
802 if (ParseTwoChar(state, "GV") &&
808 if (ParseChar(state, 'T') && ParseCallOffset(state) &&
809 ParseEncoding(state)) {
815 if (ParseTwoChar(state, "TC") && ParseType(state) &&
816 ParseNumber(state) && ParseChar(state, '_') &&
817 DisableAppend(state) &&
819 RestoreAppend(state, copy.append);
824 if (ParseChar(state, 'T') && ParseCharClass(state, "FJ") &&
830 if (ParseTwoChar(state, "GR") && ParseName(state)) {
835 if (ParseTwoChar(state, "GA") && ParseEncoding(state)) {
840 if (ParseChar(state, 'T') && ParseCharClass(state, "hv") &&
841 ParseCallOffset(state) && ParseEncoding(state)) {
848 // <call-offset> ::= h <nv-offset> _
849 // ::= v <v-offset> _
850 static bool ParseCallOffset(State *state) {
852 if (ParseChar(state, 'h') &&
853 ParseNVOffset(state) && ParseChar(state, '_')) {
858 if (ParseChar(state, 'v') &&
859 ParseVOffset(state) && ParseChar(state, '_')) {
867 // <nv-offset> ::= <(offset) number>
868 static bool ParseNVOffset(State *state) {
869 return ParseNumber(state);
872 // <v-offset> ::= <(offset) number> _ <(virtual offset) number>
873 static bool ParseVOffset(State *state) {
875 if (ParseNumber(state) && ParseChar(state, '_') &&
876 ParseNumber(state)) {
883 // <ctor-dtor-name> ::= C1 | C2 | C3
885 static bool ParseCtorDtorName(State *state) {
887 if (ParseChar(state, 'C') &&
888 ParseCharClass(state, "123")) {
889 const char * const prev_name = state->prev_name;
890 const int prev_name_length = state->prev_name_length;
891 MaybeAppendWithLength(state, prev_name, prev_name_length);
896 if (ParseChar(state, 'D') &&
897 ParseCharClass(state, "012")) {
898 const char * const prev_name = state->prev_name;
899 const int prev_name_length = state->prev_name_length;
900 MaybeAppend(state, "~");
901 MaybeAppendWithLength(state, prev_name, prev_name_length);
908 // <type> ::= <CV-qualifiers> <type>
909 // ::= P <type> # pointer-to
910 // ::= R <type> # reference-to
911 // ::= O <type> # rvalue reference-to (C++0x)
912 // ::= C <type> # complex pair (C 2000)
913 // ::= G <type> # imaginary (C 2000)
914 // ::= U <source-name> <type> # vendor extended type qualifier
915 // ::= <builtin-type>
916 // ::= <function-type>
917 // ::= <class-enum-type>
919 // ::= <pointer-to-member-type>
920 // ::= <template-template-param> <template-args>
921 // ::= <template-param>
922 // ::= <substitution>
923 // ::= Dp <type> # pack expansion of (C++0x)
924 // ::= Dt <expression> E # decltype of an id-expression or class
925 // # member access (C++0x)
926 // ::= DT <expression> E # decltype of an expression (C++0x)
928 static bool ParseType(State *state) {
929 // We should check CV-qualifers, and PRGC things first.
931 if (ParseCVQualifiers(state) && ParseType(state)) {
936 if (ParseCharClass(state, "OPRCG") && ParseType(state)) {
941 if (ParseTwoChar(state, "Dp") && ParseType(state)) {
946 if (ParseChar(state, 'D') && ParseCharClass(state, "tT") &&
947 ParseExpression(state) && ParseChar(state, 'E')) {
952 if (ParseChar(state, 'U') && ParseSourceName(state) &&
958 if (ParseBuiltinType(state) ||
959 ParseFunctionType(state) ||
960 ParseClassEnumType(state) ||
961 ParseArrayType(state) ||
962 ParsePointerToMemberType(state) ||
963 ParseSubstitution(state)) {
967 if (ParseTemplateTemplateParam(state) &&
968 ParseTemplateArgs(state)) {
973 // Less greedy than <template-template-param> <template-args>.
974 if (ParseTemplateParam(state)) {
981 // <CV-qualifiers> ::= [r] [V] [K]
982 // We don't allow empty <CV-qualifiers> to avoid infinite loop in
984 static bool ParseCVQualifiers(State *state) {
985 int num_cv_qualifiers = 0;
986 num_cv_qualifiers += ParseChar(state, 'r');
987 num_cv_qualifiers += ParseChar(state, 'V');
988 num_cv_qualifiers += ParseChar(state, 'K');
989 return num_cv_qualifiers > 0;
992 // <builtin-type> ::= v, etc.
993 // ::= u <source-name>
994 static bool ParseBuiltinType(State *state) {
996 for (p = kBuiltinTypeList; p->abbrev != NULL; ++p) {
997 if (state->mangled_cur[0] == p->abbrev[0]) {
998 MaybeAppend(state, p->real_name);
999 ++state->mangled_cur;
1004 State copy = *state;
1005 if (ParseChar(state, 'u') && ParseSourceName(state)) {
1012 // <function-type> ::= F [Y] <bare-function-type> E
1013 static bool ParseFunctionType(State *state) {
1014 State copy = *state;
1015 if (ParseChar(state, 'F') && Optional(ParseChar(state, 'Y')) &&
1016 ParseBareFunctionType(state) && ParseChar(state, 'E')) {
1023 // <bare-function-type> ::= <(signature) type>+
1024 static bool ParseBareFunctionType(State *state) {
1025 State copy = *state;
1026 DisableAppend(state);
1027 if (OneOrMore(ParseType, state)) {
1028 RestoreAppend(state, copy.append);
1029 MaybeAppend(state, "()");
1036 // <class-enum-type> ::= <name>
1037 static bool ParseClassEnumType(State *state) {
1038 return ParseName(state);
1041 // <array-type> ::= A <(positive dimension) number> _ <(element) type>
1042 // ::= A [<(dimension) expression>] _ <(element) type>
1043 static bool ParseArrayType(State *state) {
1044 State copy = *state;
1045 if (ParseChar(state, 'A') && ParseNumber(state) &&
1046 ParseChar(state, '_') && ParseType(state)) {
1051 if (ParseChar(state, 'A') && Optional(ParseExpression(state)) &&
1052 ParseChar(state, '_') && ParseType(state)) {
1059 // <pointer-to-member-type> ::= M <(class) type> <(member) type>
1060 static bool ParsePointerToMemberType(State *state) {
1061 State copy = *state;
1062 if (ParseChar(state, 'M') && ParseType(state) &&
1070 // <template-param> ::= T_
1071 // ::= T <parameter-2 non-negative number> _
1072 static bool ParseTemplateParam(State *state) {
1073 if (ParseTwoChar(state, "T_")) {
1074 MaybeAppend(state, "?"); // We don't support template substitutions.
1078 State copy = *state;
1079 if (ParseChar(state, 'T') && ParseNumber(state) &&
1080 ParseChar(state, '_')) {
1081 MaybeAppend(state, "?"); // We don't support template substitutions.
1089 // <template-template-param> ::= <template-param>
1090 // ::= <substitution>
1091 static bool ParseTemplateTemplateParam(State *state) {
1092 return (ParseTemplateParam(state) ||
1093 ParseSubstitution(state));
1096 // <template-args> ::= I <template-arg>+ E
1097 static bool ParseTemplateArgs(State *state) {
1098 State copy = *state;
1099 DisableAppend(state);
1100 if (ParseChar(state, 'I') &&
1101 OneOrMore(ParseTemplateArg, state) &&
1102 ParseChar(state, 'E')) {
1103 RestoreAppend(state, copy.append);
1104 MaybeAppend(state, "<>");
1111 // <template-arg> ::= <type>
1112 // ::= <expr-primary>
1113 // ::= I <template-arg>* E # argument pack
1114 // ::= X <expression> E
1115 static bool ParseTemplateArg(State *state) {
1116 State copy = *state;
1117 if (ParseChar(state, 'I') &&
1118 ZeroOrMore(ParseTemplateArg, state) &&
1119 ParseChar(state, 'E')) {
1124 if (ParseType(state) ||
1125 ParseExprPrimary(state)) {
1130 if (ParseChar(state, 'X') && ParseExpression(state) &&
1131 ParseChar(state, 'E')) {
1138 // <expression> ::= <template-param>
1139 // ::= <expr-primary>
1140 // ::= <unary operator-name> <expression>
1141 // ::= <binary operator-name> <expression> <expression>
1142 // ::= <trinary operator-name> <expression> <expression>
1145 // ::= sr <type> <unqualified-name> <template-args>
1146 // ::= sr <type> <unqualified-name>
1147 static bool ParseExpression(State *state) {
1148 if (ParseTemplateParam(state) || ParseExprPrimary(state)) {
1152 State copy = *state;
1153 if (ParseOperatorName(state) &&
1154 ParseExpression(state) &&
1155 ParseExpression(state) &&
1156 ParseExpression(state)) {
1161 if (ParseOperatorName(state) &&
1162 ParseExpression(state) &&
1163 ParseExpression(state)) {
1168 if (ParseOperatorName(state) &&
1169 ParseExpression(state)) {
1174 if (ParseTwoChar(state, "st") && ParseType(state)) {
1179 if (ParseTwoChar(state, "sr") && ParseType(state) &&
1180 ParseUnqualifiedName(state) &&
1181 ParseTemplateArgs(state)) {
1186 if (ParseTwoChar(state, "sr") && ParseType(state) &&
1187 ParseUnqualifiedName(state)) {
1194 // <expr-primary> ::= L <type> <(value) number> E
1195 // ::= L <type> <(value) float> E
1196 // ::= L <mangled-name> E
1197 // // A bug in g++'s C++ ABI version 2 (-fabi-version=2).
1198 // ::= LZ <encoding> E
1199 static bool ParseExprPrimary(State *state) {
1200 State copy = *state;
1201 if (ParseChar(state, 'L') && ParseType(state) &&
1202 ParseNumber(state) &&
1203 ParseChar(state, 'E')) {
1208 if (ParseChar(state, 'L') && ParseType(state) &&
1209 ParseFloatNumber(state) &&
1210 ParseChar(state, 'E')) {
1215 if (ParseChar(state, 'L') && ParseMangledName(state) &&
1216 ParseChar(state, 'E')) {
1221 if (ParseTwoChar(state, "LZ") && ParseEncoding(state) &&
1222 ParseChar(state, 'E')) {
1230 // <local-name> := Z <(function) encoding> E <(entity) name>
1231 // [<discriminator>]
1232 // := Z <(function) encoding> E s [<discriminator>]
1233 static bool ParseLocalName(State *state) {
1234 State copy = *state;
1235 if (ParseChar(state, 'Z') && ParseEncoding(state) &&
1236 ParseChar(state, 'E') && MaybeAppend(state, "::") &&
1237 ParseName(state) && Optional(ParseDiscriminator(state))) {
1242 if (ParseChar(state, 'Z') && ParseEncoding(state) &&
1243 ParseTwoChar(state, "Es") && Optional(ParseDiscriminator(state))) {
1250 // <discriminator> := _ <(non-negative) number>
1251 static bool ParseDiscriminator(State *state) {
1252 State copy = *state;
1253 if (ParseChar(state, '_') && ParseNumber(state)) {
1260 // <substitution> ::= S_
1263 static bool ParseSubstitution(State *state) {
1264 if (ParseTwoChar(state, "S_")) {
1265 MaybeAppend(state, "?"); // We don't support substitutions.
1269 State copy = *state;
1270 if (ParseChar(state, 'S') && ParseSeqId(state) &&
1271 ParseChar(state, '_')) {
1272 MaybeAppend(state, "?"); // We don't support substitutions.
1277 // Expand abbreviations like "St" => "std".
1278 if (ParseChar(state, 'S')) {
1279 const AbbrevPair *p;
1280 for (p = kSubstitutionList; p->abbrev != NULL; ++p) {
1281 if (state->mangled_cur[0] == p->abbrev[1]) {
1282 MaybeAppend(state, "std");
1283 if (p->real_name[0] != '\0') {
1284 MaybeAppend(state, "::");
1285 MaybeAppend(state, p->real_name);
1287 state->mangled_cur += 1;
1296 // The demangler entry point.
1297 bool Demangle(const char *mangled, char *out, int out_size) {
1299 InitState(&state, mangled, out, out_size);
1300 return (ParseMangledName(&state) &&
1301 state.overflowed == false &&
1302 RemainingLength(&state) == 0);
1305 _END_GOOGLE_NAMESPACE_