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 char *out_cur; // Cursor of output string.
147 const char *out_begin; // Beginning of output string.
148 const char *out_end; // End of output string.
149 const char *prev_name; // For constructors/destructors.
150 int prev_name_length; // For constructors/destructors.
151 short nest_level; // For nested names.
152 bool append; // Append flag.
153 bool overflowed; // True if output gets overflowed.
156 // We don't use strlen() in libc since it's not guaranteed to be async
158 static size_t StrLen(const char *str) {
160 while (*str != '\0') {
167 // Returns true if "str" has at least "n" characters remaining.
168 static bool AtLeastNumCharsRemaining(const char *str, int n) {
169 for (int i = 0; i < n; ++i) {
177 // Returns true if "str" has "prefix" as a prefix.
178 static bool StrPrefix(const char *str, const char *prefix) {
180 while (str[i] != '\0' && prefix[i] != '\0' &&
181 str[i] == prefix[i]) {
184 return prefix[i] == '\0'; // Consumed everything in "prefix".
187 static void InitState(State *state, const char *mangled,
188 char *out, int out_size) {
189 state->mangled_cur = mangled;
190 state->out_cur = out;
191 state->out_begin = out;
192 state->out_end = out + out_size;
193 state->prev_name = NULL;
194 state->prev_name_length = -1;
195 state->nest_level = -1;
196 state->append = true;
197 state->overflowed = false;
200 // Returns true and advances "mangled_cur" if we find "one_char_token"
201 // at "mangled_cur" position. It is assumed that "one_char_token" does
203 static bool ParseOneCharToken(State *state, const char one_char_token) {
204 if (state->mangled_cur[0] == one_char_token) {
205 ++state->mangled_cur;
211 // Returns true and advances "mangled_cur" if we find "two_char_token"
212 // at "mangled_cur" position. It is assumed that "two_char_token" does
214 static bool ParseTwoCharToken(State *state, const char *two_char_token) {
215 if (state->mangled_cur[0] == two_char_token[0] &&
216 state->mangled_cur[1] == two_char_token[1]) {
217 state->mangled_cur += 2;
223 // Returns true and advances "mangled_cur" if we find any character in
224 // "char_class" at "mangled_cur" position.
225 static bool ParseCharClass(State *state, const char *char_class) {
226 if (state->mangled_cur == '\0') {
229 const char *p = char_class;
230 for (; *p != '\0'; ++p) {
231 if (state->mangled_cur[0] == *p) {
232 ++state->mangled_cur;
239 // This function is used for handling an optional non-terminal.
240 static bool Optional(bool status) {
244 // This function is used for handling <non-terminal>+ syntax.
245 typedef bool (*ParseFunc)(State *);
246 static bool OneOrMore(ParseFunc parse_func, State *state) {
247 if (parse_func(state)) {
248 while (parse_func(state)) {
255 // This function is used for handling <non-terminal>* syntax. The function
256 // always returns true and must be followed by a termination token or a
257 // terminating sequence not handled by parse_func (e.g.
258 // ParseOneCharToken(state, 'E')).
259 static bool ZeroOrMore(ParseFunc parse_func, State *state) {
260 while (parse_func(state)) {
265 // Append "str" at "out_cur". If there is an overflow, "overflowed"
266 // is set to true for later use. The output string is ensured to
267 // always terminate with '\0' as long as there is no overflow.
268 static void Append(State *state, const char * const str, const int length) {
270 for (i = 0; i < length; ++i) {
271 if (state->out_cur + 1 < state->out_end) { // +1 for '\0'
272 *state->out_cur = str[i];
275 state->overflowed = true;
279 if (!state->overflowed) {
280 *state->out_cur = '\0'; // Terminate it with '\0'
284 // We don't use equivalents in libc to avoid locale issues.
285 static bool IsLower(char c) {
286 return c >= 'a' && c <= 'z';
289 static bool IsAlpha(char c) {
290 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
293 static bool IsDigit(char c) {
294 return c >= '0' && c <= '9';
297 // Returns true if "str" is a function clone suffix. These suffixes are used
298 // by GCC 4.5.x and later versions to indicate functions which have been
299 // cloned during optimization. We treat any sequence (.<alpha>+.<digit>+)+ as
300 // a function clone suffix.
301 static bool IsFunctionCloneSuffix(const char *str) {
303 while (str[i] != '\0') {
304 // Consume a single .<alpha>+.<digit>+ sequence.
305 if (str[i] != '.' || !IsAlpha(str[i + 1])) {
309 while (IsAlpha(str[i])) {
312 if (str[i] != '.' || !IsDigit(str[i + 1])) {
316 while (IsDigit(str[i])) {
320 return true; // Consumed everything in "str".
323 // Append "str" with some tweaks, iff "append" state is true.
324 // Returns true so that it can be placed in "if" conditions.
325 static void MaybeAppendWithLength(State *state, const char * const str,
327 if (state->append && length > 0) {
328 // Append a space if the output buffer ends with '<' and "str"
329 // starts with '<' to avoid <<<.
330 if (str[0] == '<' && state->out_begin < state->out_cur &&
331 state->out_cur[-1] == '<') {
332 Append(state, " ", 1);
334 // Remember the last identifier name for ctors/dtors.
335 if (IsAlpha(str[0]) || str[0] == '_') {
336 state->prev_name = state->out_cur;
337 state->prev_name_length = length;
339 Append(state, str, length);
343 // A convenient wrapper arount MaybeAppendWithLength().
344 static bool MaybeAppend(State *state, const char * const str) {
346 int length = StrLen(str);
347 MaybeAppendWithLength(state, str, length);
352 // This function is used for handling nested names.
353 static bool EnterNestedName(State *state) {
354 state->nest_level = 0;
358 // This function is used for handling nested names.
359 static bool LeaveNestedName(State *state, short prev_value) {
360 state->nest_level = prev_value;
364 // Disable the append mode not to print function parameters, etc.
365 static bool DisableAppend(State *state) {
366 state->append = false;
370 // Restore the append mode to the previous state.
371 static bool RestoreAppend(State *state, bool prev_value) {
372 state->append = prev_value;
376 // Increase the nest level for nested names.
377 static void MaybeIncreaseNestLevel(State *state) {
378 if (state->nest_level > -1) {
383 // Appends :: for nested names if necessary.
384 static void MaybeAppendSeparator(State *state) {
385 if (state->nest_level >= 1) {
386 MaybeAppend(state, "::");
390 // Cancel the last separator if necessary.
391 static void MaybeCancelLastSeparator(State *state) {
392 if (state->nest_level >= 1 && state->append &&
393 state->out_begin <= state->out_cur - 2) {
395 *state->out_cur = '\0';
399 // Returns true if the identifier of the given length pointed to by
400 // "mangled_cur" is anonymous namespace.
401 static bool IdentifierIsAnonymousNamespace(State *state, int length) {
402 static const char anon_prefix[] = "_GLOBAL__N_";
403 return (length > sizeof(anon_prefix) - 1 && // Should be longer.
404 StrPrefix(state->mangled_cur, anon_prefix));
407 // Forward declarations of our parsing functions.
408 static bool ParseMangledName(State *state);
409 static bool ParseEncoding(State *state);
410 static bool ParseName(State *state);
411 static bool ParseUnscopedName(State *state);
412 static bool ParseUnscopedTemplateName(State *state);
413 static bool ParseNestedName(State *state);
414 static bool ParsePrefix(State *state);
415 static bool ParseUnqualifiedName(State *state);
416 static bool ParseSourceName(State *state);
417 static bool ParseLocalSourceName(State *state);
418 static bool ParseNumber(State *state, int *number_out);
419 static bool ParseFloatNumber(State *state);
420 static bool ParseSeqId(State *state);
421 static bool ParseIdentifier(State *state, int length);
422 static bool ParseOperatorName(State *state);
423 static bool ParseSpecialName(State *state);
424 static bool ParseCallOffset(State *state);
425 static bool ParseNVOffset(State *state);
426 static bool ParseVOffset(State *state);
427 static bool ParseCtorDtorName(State *state);
428 static bool ParseType(State *state);
429 static bool ParseCVQualifiers(State *state);
430 static bool ParseBuiltinType(State *state);
431 static bool ParseFunctionType(State *state);
432 static bool ParseBareFunctionType(State *state);
433 static bool ParseClassEnumType(State *state);
434 static bool ParseArrayType(State *state);
435 static bool ParsePointerToMemberType(State *state);
436 static bool ParseTemplateParam(State *state);
437 static bool ParseTemplateTemplateParam(State *state);
438 static bool ParseTemplateArgs(State *state);
439 static bool ParseTemplateArg(State *state);
440 static bool ParseExpression(State *state);
441 static bool ParseExprPrimary(State *state);
442 static bool ParseLocalName(State *state);
443 static bool ParseDiscriminator(State *state);
444 static bool ParseSubstitution(State *state);
446 // Implementation note: the following code is a straightforward
447 // translation of the Itanium C++ ABI defined in BNF with a couple of
450 // - Support GNU extensions not defined in the Itanium C++ ABI
451 // - <prefix> and <template-prefix> are combined to avoid infinite loop
452 // - Reorder patterns to shorten the code
453 // - Reorder patterns to give greedier functions precedence
454 // We'll mark "Less greedy than" for these cases in the code
456 // Each parsing function changes the state and returns true on
457 // success. Otherwise, don't change the state and returns false. To
458 // ensure that the state isn't changed in the latter case, we save the
459 // original state before we call more than one parsing functions
460 // consecutively with &&, and restore the state if unsuccessful. See
461 // ParseEncoding() as an example of this convention. We follow the
462 // convention throughout the code.
464 // Originally we tried to do demangling without following the full ABI
465 // syntax but it turned out we needed to follow the full syntax to
466 // parse complicated cases like nested template arguments. Note that
467 // implementing a full-fledged demangler isn't trivial (libiberty's
468 // cp-demangle.c has +4300 lines).
470 // Note that (foo) in <(foo) ...> is a modifier to be ignored.
474 // <http://www.codesourcery.com/cxx-abi/abi.html#mangling>
476 // <mangled-name> ::= _Z <encoding>
477 static bool ParseMangledName(State *state) {
478 return ParseTwoCharToken(state, "_Z") && ParseEncoding(state);
481 // <encoding> ::= <(function) name> <bare-function-type>
483 // ::= <special-name>
484 static bool ParseEncoding(State *state) {
486 if (ParseName(state) && ParseBareFunctionType(state)) {
491 if (ParseName(state) || ParseSpecialName(state)) {
497 // <name> ::= <nested-name>
498 // ::= <unscoped-template-name> <template-args>
499 // ::= <unscoped-name>
501 static bool ParseName(State *state) {
502 if (ParseNestedName(state) || ParseLocalName(state)) {
507 if (ParseUnscopedTemplateName(state) &&
508 ParseTemplateArgs(state)) {
513 // Less greedy than <unscoped-template-name> <template-args>.
514 if (ParseUnscopedName(state)) {
520 // <unscoped-name> ::= <unqualified-name>
521 // ::= St <unqualified-name>
522 static bool ParseUnscopedName(State *state) {
523 if (ParseUnqualifiedName(state)) {
528 if (ParseTwoCharToken(state, "St") &&
529 MaybeAppend(state, "std::") &&
530 ParseUnqualifiedName(state)) {
537 // <unscoped-template-name> ::= <unscoped-name>
538 // ::= <substitution>
539 static bool ParseUnscopedTemplateName(State *state) {
540 return ParseUnscopedName(state) || ParseSubstitution(state);
543 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
544 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
545 static bool ParseNestedName(State *state) {
547 if (ParseOneCharToken(state, 'N') &&
548 EnterNestedName(state) &&
549 Optional(ParseCVQualifiers(state)) &&
550 ParsePrefix(state) &&
551 LeaveNestedName(state, copy.nest_level) &&
552 ParseOneCharToken(state, 'E')) {
559 // This part is tricky. If we literally translate them to code, we'll
560 // end up infinite loop. Hence we merge them to avoid the case.
562 // <prefix> ::= <prefix> <unqualified-name>
563 // ::= <template-prefix> <template-args>
564 // ::= <template-param>
565 // ::= <substitution>
567 // <template-prefix> ::= <prefix> <(template) unqualified-name>
568 // ::= <template-param>
569 // ::= <substitution>
570 static bool ParsePrefix(State *state) {
571 bool has_something = false;
573 MaybeAppendSeparator(state);
574 if (ParseTemplateParam(state) ||
575 ParseSubstitution(state) ||
576 ParseUnscopedName(state)) {
577 has_something = true;
578 MaybeIncreaseNestLevel(state);
581 MaybeCancelLastSeparator(state);
582 if (has_something && ParseTemplateArgs(state)) {
583 return ParsePrefix(state);
591 // <unqualified-name> ::= <operator-name>
592 // ::= <ctor-dtor-name>
594 // ::= <local-source-name>
595 static bool ParseUnqualifiedName(State *state) {
596 return (ParseOperatorName(state) ||
597 ParseCtorDtorName(state) ||
598 ParseSourceName(state) ||
599 ParseLocalSourceName(state));
602 // <source-name> ::= <positive length number> <identifier>
603 static bool ParseSourceName(State *state) {
606 if (ParseNumber(state, &length) && ParseIdentifier(state, length)) {
613 // <local-source-name> ::= L <source-name> [<discriminator>]
616 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775
617 // http://gcc.gnu.org/viewcvs?view=rev&revision=124467
618 static bool ParseLocalSourceName(State *state) {
620 if (ParseOneCharToken(state, 'L') && ParseSourceName(state) &&
621 Optional(ParseDiscriminator(state))) {
628 // <number> ::= [n] <non-negative decimal integer>
629 // If "number_out" is non-null, then *number_out is set to the value of the
630 // parsed number on success.
631 static bool ParseNumber(State *state, int *number_out) {
633 if (ParseOneCharToken(state, 'n')) {
636 const char *p = state->mangled_cur;
638 for (;*p != '\0'; ++p) {
640 number = number * 10 + (*p - '0');
645 if (p != state->mangled_cur) { // Conversion succeeded.
646 state->mangled_cur = p;
647 if (number_out != NULL) {
648 *number_out = number * sign;
655 // Floating-point literals are encoded using a fixed-length lowercase
656 // hexadecimal string.
657 static bool ParseFloatNumber(State *state) {
658 const char *p = state->mangled_cur;
659 for (;*p != '\0'; ++p) {
660 if (!IsDigit(*p) && !(*p >= 'a' && *p <= 'f')) {
664 if (p != state->mangled_cur) { // Conversion succeeded.
665 state->mangled_cur = p;
671 // The <seq-id> is a sequence number in base 36,
672 // using digits and upper case letters
673 static bool ParseSeqId(State *state) {
674 const char *p = state->mangled_cur;
675 for (;*p != '\0'; ++p) {
676 if (!IsDigit(*p) && !(*p >= 'A' && *p <= 'Z')) {
680 if (p != state->mangled_cur) { // Conversion succeeded.
681 state->mangled_cur = p;
687 // <identifier> ::= <unqualified source code identifier> (of given length)
688 static bool ParseIdentifier(State *state, int length) {
690 !AtLeastNumCharsRemaining(state->mangled_cur, length)) {
693 if (IdentifierIsAnonymousNamespace(state, length)) {
694 MaybeAppend(state, "(anonymous namespace)");
696 MaybeAppendWithLength(state, state->mangled_cur, length);
698 state->mangled_cur += length;
702 // <operator-name> ::= nw, and other two letters cases
703 // ::= cv <type> # (cast)
704 // ::= v <digit> <source-name> # vendor extended operator
705 static bool ParseOperatorName(State *state) {
706 if (!AtLeastNumCharsRemaining(state->mangled_cur, 2)) {
709 // First check with "cv" (cast) case.
711 if (ParseTwoCharToken(state, "cv") &&
712 MaybeAppend(state, "operator ") &&
713 EnterNestedName(state) &&
715 LeaveNestedName(state, copy.nest_level)) {
720 // Then vendor extended operators.
721 if (ParseOneCharToken(state, 'v') && ParseCharClass(state, "0123456789") &&
722 ParseSourceName(state)) {
727 // Other operator names should start with a lower alphabet followed
728 // by a lower/upper alphabet.
729 if (!(IsLower(state->mangled_cur[0]) &&
730 IsAlpha(state->mangled_cur[1]))) {
733 // We may want to perform a binary search if we really need speed.
735 for (p = kOperatorList; p->abbrev != NULL; ++p) {
736 if (state->mangled_cur[0] == p->abbrev[0] &&
737 state->mangled_cur[1] == p->abbrev[1]) {
738 MaybeAppend(state, "operator");
739 if (IsLower(*p->real_name)) { // new, delete, etc.
740 MaybeAppend(state, " ");
742 MaybeAppend(state, p->real_name);
743 state->mangled_cur += 2;
750 // <special-name> ::= TV <type>
754 // ::= Tc <call-offset> <call-offset> <(base) encoding>
755 // ::= GV <(object) name>
756 // ::= T <call-offset> <(base) encoding>
758 // ::= TC <type> <(offset) number> _ <(base) type>
763 // ::= Th <call-offset> <(base) encoding>
764 // ::= Tv <call-offset> <(base) encoding>
766 // Note: we don't care much about them since they don't appear in
767 // stack traces. The are special data.
768 static bool ParseSpecialName(State *state) {
770 if (ParseOneCharToken(state, 'T') &&
771 ParseCharClass(state, "VTIS") &&
777 if (ParseTwoCharToken(state, "Tc") && ParseCallOffset(state) &&
778 ParseCallOffset(state) && ParseEncoding(state)) {
783 if (ParseTwoCharToken(state, "GV") &&
789 if (ParseOneCharToken(state, 'T') && ParseCallOffset(state) &&
790 ParseEncoding(state)) {
796 if (ParseTwoCharToken(state, "TC") && ParseType(state) &&
797 ParseNumber(state, NULL) && ParseOneCharToken(state, '_') &&
798 DisableAppend(state) &&
800 RestoreAppend(state, copy.append);
805 if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "FJ") &&
811 if (ParseTwoCharToken(state, "GR") && ParseName(state)) {
816 if (ParseTwoCharToken(state, "GA") && ParseEncoding(state)) {
821 if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "hv") &&
822 ParseCallOffset(state) && ParseEncoding(state)) {
829 // <call-offset> ::= h <nv-offset> _
830 // ::= v <v-offset> _
831 static bool ParseCallOffset(State *state) {
833 if (ParseOneCharToken(state, 'h') &&
834 ParseNVOffset(state) && ParseOneCharToken(state, '_')) {
839 if (ParseOneCharToken(state, 'v') &&
840 ParseVOffset(state) && ParseOneCharToken(state, '_')) {
848 // <nv-offset> ::= <(offset) number>
849 static bool ParseNVOffset(State *state) {
850 return ParseNumber(state, NULL);
853 // <v-offset> ::= <(offset) number> _ <(virtual offset) number>
854 static bool ParseVOffset(State *state) {
856 if (ParseNumber(state, NULL) && ParseOneCharToken(state, '_') &&
857 ParseNumber(state, NULL)) {
864 // <ctor-dtor-name> ::= C1 | C2 | C3
866 static bool ParseCtorDtorName(State *state) {
868 if (ParseOneCharToken(state, 'C') &&
869 ParseCharClass(state, "123")) {
870 const char * const prev_name = state->prev_name;
871 const int prev_name_length = state->prev_name_length;
872 MaybeAppendWithLength(state, prev_name, prev_name_length);
877 if (ParseOneCharToken(state, 'D') &&
878 ParseCharClass(state, "012")) {
879 const char * const prev_name = state->prev_name;
880 const int prev_name_length = state->prev_name_length;
881 MaybeAppend(state, "~");
882 MaybeAppendWithLength(state, prev_name, prev_name_length);
889 // <type> ::= <CV-qualifiers> <type>
890 // ::= P <type> # pointer-to
891 // ::= R <type> # reference-to
892 // ::= O <type> # rvalue reference-to (C++0x)
893 // ::= C <type> # complex pair (C 2000)
894 // ::= G <type> # imaginary (C 2000)
895 // ::= U <source-name> <type> # vendor extended type qualifier
896 // ::= <builtin-type>
897 // ::= <function-type>
898 // ::= <class-enum-type>
900 // ::= <pointer-to-member-type>
901 // ::= <template-template-param> <template-args>
902 // ::= <template-param>
903 // ::= <substitution>
904 // ::= Dp <type> # pack expansion of (C++0x)
905 // ::= Dt <expression> E # decltype of an id-expression or class
906 // # member access (C++0x)
907 // ::= DT <expression> E # decltype of an expression (C++0x)
909 static bool ParseType(State *state) {
910 // We should check CV-qualifers, and PRGC things first.
912 if (ParseCVQualifiers(state) && ParseType(state)) {
917 if (ParseCharClass(state, "OPRCG") && ParseType(state)) {
922 if (ParseTwoCharToken(state, "Dp") && ParseType(state)) {
927 if (ParseOneCharToken(state, 'D') && ParseCharClass(state, "tT") &&
928 ParseExpression(state) && ParseOneCharToken(state, 'E')) {
933 if (ParseOneCharToken(state, 'U') && ParseSourceName(state) &&
939 if (ParseBuiltinType(state) ||
940 ParseFunctionType(state) ||
941 ParseClassEnumType(state) ||
942 ParseArrayType(state) ||
943 ParsePointerToMemberType(state) ||
944 ParseSubstitution(state)) {
948 if (ParseTemplateTemplateParam(state) &&
949 ParseTemplateArgs(state)) {
954 // Less greedy than <template-template-param> <template-args>.
955 if (ParseTemplateParam(state)) {
962 // <CV-qualifiers> ::= [r] [V] [K]
963 // We don't allow empty <CV-qualifiers> to avoid infinite loop in
965 static bool ParseCVQualifiers(State *state) {
966 int num_cv_qualifiers = 0;
967 num_cv_qualifiers += ParseOneCharToken(state, 'r');
968 num_cv_qualifiers += ParseOneCharToken(state, 'V');
969 num_cv_qualifiers += ParseOneCharToken(state, 'K');
970 return num_cv_qualifiers > 0;
973 // <builtin-type> ::= v, etc.
974 // ::= u <source-name>
975 static bool ParseBuiltinType(State *state) {
977 for (p = kBuiltinTypeList; p->abbrev != NULL; ++p) {
978 if (state->mangled_cur[0] == p->abbrev[0]) {
979 MaybeAppend(state, p->real_name);
980 ++state->mangled_cur;
986 if (ParseOneCharToken(state, 'u') && ParseSourceName(state)) {
993 // <function-type> ::= F [Y] <bare-function-type> E
994 static bool ParseFunctionType(State *state) {
996 if (ParseOneCharToken(state, 'F') &&
997 Optional(ParseOneCharToken(state, 'Y')) &&
998 ParseBareFunctionType(state) && ParseOneCharToken(state, 'E')) {
1005 // <bare-function-type> ::= <(signature) type>+
1006 static bool ParseBareFunctionType(State *state) {
1007 State copy = *state;
1008 DisableAppend(state);
1009 if (OneOrMore(ParseType, state)) {
1010 RestoreAppend(state, copy.append);
1011 MaybeAppend(state, "()");
1018 // <class-enum-type> ::= <name>
1019 static bool ParseClassEnumType(State *state) {
1020 return ParseName(state);
1023 // <array-type> ::= A <(positive dimension) number> _ <(element) type>
1024 // ::= A [<(dimension) expression>] _ <(element) type>
1025 static bool ParseArrayType(State *state) {
1026 State copy = *state;
1027 if (ParseOneCharToken(state, 'A') && ParseNumber(state, NULL) &&
1028 ParseOneCharToken(state, '_') && ParseType(state)) {
1033 if (ParseOneCharToken(state, 'A') && Optional(ParseExpression(state)) &&
1034 ParseOneCharToken(state, '_') && ParseType(state)) {
1041 // <pointer-to-member-type> ::= M <(class) type> <(member) type>
1042 static bool ParsePointerToMemberType(State *state) {
1043 State copy = *state;
1044 if (ParseOneCharToken(state, 'M') && ParseType(state) &&
1052 // <template-param> ::= T_
1053 // ::= T <parameter-2 non-negative number> _
1054 static bool ParseTemplateParam(State *state) {
1055 if (ParseTwoCharToken(state, "T_")) {
1056 MaybeAppend(state, "?"); // We don't support template substitutions.
1060 State copy = *state;
1061 if (ParseOneCharToken(state, 'T') && ParseNumber(state, NULL) &&
1062 ParseOneCharToken(state, '_')) {
1063 MaybeAppend(state, "?"); // We don't support template substitutions.
1071 // <template-template-param> ::= <template-param>
1072 // ::= <substitution>
1073 static bool ParseTemplateTemplateParam(State *state) {
1074 return (ParseTemplateParam(state) ||
1075 ParseSubstitution(state));
1078 // <template-args> ::= I <template-arg>+ E
1079 static bool ParseTemplateArgs(State *state) {
1080 State copy = *state;
1081 DisableAppend(state);
1082 if (ParseOneCharToken(state, 'I') &&
1083 OneOrMore(ParseTemplateArg, state) &&
1084 ParseOneCharToken(state, 'E')) {
1085 RestoreAppend(state, copy.append);
1086 MaybeAppend(state, "<>");
1093 // <template-arg> ::= <type>
1094 // ::= <expr-primary>
1095 // ::= I <template-arg>* E # argument pack
1096 // ::= X <expression> E
1097 static bool ParseTemplateArg(State *state) {
1098 State copy = *state;
1099 if (ParseOneCharToken(state, 'I') &&
1100 ZeroOrMore(ParseTemplateArg, state) &&
1101 ParseOneCharToken(state, 'E')) {
1106 if (ParseType(state) ||
1107 ParseExprPrimary(state)) {
1112 if (ParseOneCharToken(state, 'X') && ParseExpression(state) &&
1113 ParseOneCharToken(state, 'E')) {
1120 // <expression> ::= <template-param>
1121 // ::= <expr-primary>
1122 // ::= <unary operator-name> <expression>
1123 // ::= <binary operator-name> <expression> <expression>
1124 // ::= <trinary operator-name> <expression> <expression>
1127 // ::= sr <type> <unqualified-name> <template-args>
1128 // ::= sr <type> <unqualified-name>
1129 static bool ParseExpression(State *state) {
1130 if (ParseTemplateParam(state) || ParseExprPrimary(state)) {
1134 State copy = *state;
1135 if (ParseOperatorName(state) &&
1136 ParseExpression(state) &&
1137 ParseExpression(state) &&
1138 ParseExpression(state)) {
1143 if (ParseOperatorName(state) &&
1144 ParseExpression(state) &&
1145 ParseExpression(state)) {
1150 if (ParseOperatorName(state) &&
1151 ParseExpression(state)) {
1156 if (ParseTwoCharToken(state, "st") && ParseType(state)) {
1161 if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
1162 ParseUnqualifiedName(state) &&
1163 ParseTemplateArgs(state)) {
1168 if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
1169 ParseUnqualifiedName(state)) {
1176 // <expr-primary> ::= L <type> <(value) number> E
1177 // ::= L <type> <(value) float> E
1178 // ::= L <mangled-name> E
1179 // // A bug in g++'s C++ ABI version 2 (-fabi-version=2).
1180 // ::= LZ <encoding> E
1181 static bool ParseExprPrimary(State *state) {
1182 State copy = *state;
1183 if (ParseOneCharToken(state, 'L') && ParseType(state) &&
1184 ParseNumber(state, NULL) &&
1185 ParseOneCharToken(state, 'E')) {
1190 if (ParseOneCharToken(state, 'L') && ParseType(state) &&
1191 ParseFloatNumber(state) &&
1192 ParseOneCharToken(state, 'E')) {
1197 if (ParseOneCharToken(state, 'L') && ParseMangledName(state) &&
1198 ParseOneCharToken(state, 'E')) {
1203 if (ParseTwoCharToken(state, "LZ") && ParseEncoding(state) &&
1204 ParseOneCharToken(state, 'E')) {
1212 // <local-name> := Z <(function) encoding> E <(entity) name>
1213 // [<discriminator>]
1214 // := Z <(function) encoding> E s [<discriminator>]
1215 static bool ParseLocalName(State *state) {
1216 State copy = *state;
1217 if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
1218 ParseOneCharToken(state, 'E') && MaybeAppend(state, "::") &&
1219 ParseName(state) && Optional(ParseDiscriminator(state))) {
1224 if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
1225 ParseTwoCharToken(state, "Es") && Optional(ParseDiscriminator(state))) {
1232 // <discriminator> := _ <(non-negative) number>
1233 static bool ParseDiscriminator(State *state) {
1234 State copy = *state;
1235 if (ParseOneCharToken(state, '_') && ParseNumber(state, NULL)) {
1242 // <substitution> ::= S_
1245 static bool ParseSubstitution(State *state) {
1246 if (ParseTwoCharToken(state, "S_")) {
1247 MaybeAppend(state, "?"); // We don't support substitutions.
1251 State copy = *state;
1252 if (ParseOneCharToken(state, 'S') && ParseSeqId(state) &&
1253 ParseOneCharToken(state, '_')) {
1254 MaybeAppend(state, "?"); // We don't support substitutions.
1259 // Expand abbreviations like "St" => "std".
1260 if (ParseOneCharToken(state, 'S')) {
1261 const AbbrevPair *p;
1262 for (p = kSubstitutionList; p->abbrev != NULL; ++p) {
1263 if (state->mangled_cur[0] == p->abbrev[1]) {
1264 MaybeAppend(state, "std");
1265 if (p->real_name[0] != '\0') {
1266 MaybeAppend(state, "::");
1267 MaybeAppend(state, p->real_name);
1269 ++state->mangled_cur;
1278 // Parse <mangled-name>, optionally followed by either a function-clone suffix
1279 // or version suffix. Returns true only if all of "mangled_cur" was consumed.
1280 static bool ParseTopLevelMangledName(State *state) {
1281 if (ParseMangledName(state)) {
1282 if (state->mangled_cur[0] != '\0') {
1283 // Drop trailing function clone suffix, if any.
1284 if (IsFunctionCloneSuffix(state->mangled_cur)) {
1287 // Append trailing version suffix if any.
1288 // ex. _Z3foo@@GLIBCXX_3.4
1289 if (state->mangled_cur[0] == '@') {
1290 MaybeAppend(state, state->mangled_cur);
1293 return false; // Unconsumed suffix.
1300 // The demangler entry point.
1301 bool Demangle(const char *mangled, char *out, int out_size) {
1303 InitState(&state, mangled, out, out_size);
1304 return ParseTopLevelMangledName(&state) && !state.overflowed;
1307 _END_GOOGLE_NAMESPACE_