1 //===-- FastDemangle.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 //#define DEBUG_FAILURES 1
15 //#define DEBUG_SUBSTITUTIONS 1
16 //#define DEBUG_TEMPLATE_ARGS 1
17 //#define DEBUG_HIGHWATER 1
18 //#define DEBUG_REORDERING 1
22 /// @brief Represents the collection of qualifiers on a type
28 QualifierRestrict = 2,
29 QualifierVolatile = 4,
30 QualifierReference = 8,
31 QualifierRValueReference = 16,
35 /// @brief Categorizes the recognized operators
37 enum class OperatorKind
49 /// @brief Represents one of the recognized two-character operator
50 /// abbreviations used when parsing operators as names and expressions
58 /// @brief Represents a range of characters in the output buffer, typically for
59 /// use with RewriteRange()
67 /// @brief Transient state required while parsing a name
71 bool parse_function_params;
73 bool has_no_return_type;
74 BufferRange last_name_range;
77 /// @brief LLDB's fast C++ demangler
79 /// This is an incomplete implementation designed to speed up the demangling
80 /// process that is often a bottleneck when LLDB stops a process for the first
81 /// time. Where the implementation doesn't know how to demangle a symbol it
82 /// fails gracefully to allow the caller to fall back to the existing demangler.
84 /// Over time the full mangling spec should be supported without compromising
85 /// performance for the most common cases.
91 //----------------------------------------------------
93 //----------------------------------------------------
95 /// @brief Create a SymbolDemangler
97 /// The newly created demangler allocates and owns scratch memory sufficient
98 /// for demangling typical symbols. Additional memory will be allocated if
99 /// needed and managed by the demangler instance.
103 buffer = (char *) malloc(8192);
104 buffer_end = buffer + 8192;
107 rewrite_ranges = (BufferRange *) malloc(128 * sizeof(BufferRange));
108 rewrite_ranges_size = 128;
109 owns_rewrite_ranges = true;
112 /// @brief Create a SymbolDemangler that uses provided scratch memory
114 /// The provided memory is not owned by the demangler. It will be
115 /// overwritten during calls to GetDemangledCopy() but can be used for
116 /// other purposes between calls. The provided memory will not be freed
117 /// when this instance is destroyed.
119 /// If demangling a symbol requires additional space it will be allocated
120 /// and managed by the demangler instance.
122 /// @param storage_ptr Valid pointer to at least storage_size bytes of
123 /// space that the SymbolDemangler can use during demangling
125 /// @param storage_size Number of bytes of space available scratch memory
126 /// referenced by storage_ptr
128 SymbolDemangler(void * storage_ptr, int storage_size)
130 // Use up to 1/8th of the provided space for rewrite ranges
131 rewrite_ranges_size = (storage_size >> 3) / sizeof(BufferRange);
132 rewrite_ranges = (BufferRange *) storage_ptr;
133 owns_rewrite_ranges = false;
135 // Use the rest for the character buffer
136 buffer = (char *) storage_ptr + rewrite_ranges_size * sizeof(BufferRange);
137 buffer_end = (const char *)storage_ptr + storage_size;
141 /// @brief Destroys the SymbolDemangler and deallocates any scratch
142 /// memory that it owns
146 if (owns_buffer) free(buffer);
147 if (owns_rewrite_ranges) free(rewrite_ranges);
150 #ifdef DEBUG_HIGHWATER
151 int highwater_store = 0;
152 int highwater_buffer = 0;
155 /// @brief Parses the provided mangled name and returns a newly allocated
158 /// @param mangled_name Valid null-terminated C++ mangled name following
159 /// the Itanium C++ ABI mangling specification as implemented by Clang
161 /// @result Newly allocated null-terminated demangled name when demangling
162 /// is succesful, and nullptr when demangling fails. The caller is
163 /// responsible for freeing the allocated memory.
165 char * GetDemangledCopy(const char * mangled_name,
166 long mangled_name_length = 0)
168 if (!ParseMangling(mangled_name, mangled_name_length)) return nullptr;
170 #ifdef DEBUG_HIGHWATER
171 int rewrite_count = next_substitute_index +
172 (rewrite_ranges_size - 1 - next_template_arg_index);
173 int buffer_size = (int)(write_ptr - buffer);
174 if (rewrite_count > highwater_store) highwater_store = rewrite_count;
175 if (buffer_size > highwater_buffer) highwater_buffer = buffer_size;
178 int length = (int)(write_ptr - buffer);
179 char * copy = (char *)malloc(length + 1);
180 memcpy(copy, buffer, length);
187 //----------------------------------------------------
190 // Manage the storage used during demangling
191 //----------------------------------------------------
193 void GrowBuffer(long min_growth = 0)
195 // By default, double the size of the buffer
196 long growth = buffer_end - buffer;
198 // Avoid growing by more than 1MB at a time
199 if (growth > 1 << 20) growth = 1 << 20;
201 // ... but never grow by less than requested,
202 // or 1K, whichever is greater
203 if (min_growth < 1024) min_growth = 1024;
204 if (growth < min_growth) growth = min_growth;
206 // Allocate the new buffer and migrate content
207 long new_size = (buffer_end - buffer) + growth;
208 char * new_buffer = (char *)malloc(new_size);
209 memcpy(new_buffer, buffer, write_ptr - buffer);
210 if (owns_buffer) free(buffer);
213 // Update references to the new buffer
214 write_ptr = new_buffer + (write_ptr - buffer);
216 buffer_end = buffer + new_size;
219 void GrowRewriteRanges()
221 // By default, double the size of the array
222 int growth = rewrite_ranges_size;
224 // Apply reasonable minimum and maximum sizes for growth
225 if (growth > 128) growth = 128;
226 if (growth < 16) growth = 16;
228 // Allocate the new array and migrate content
229 int bytes = (rewrite_ranges_size + growth) * sizeof(BufferRange);
230 BufferRange * new_ranges = (BufferRange *) malloc(bytes);
231 for (int index = 0; index < next_substitute_index; index++)
233 new_ranges[index] = rewrite_ranges[index];
235 for (int index = rewrite_ranges_size - 1;
236 index > next_template_arg_index; index--)
238 new_ranges[index + growth] = rewrite_ranges[index];
240 if (owns_rewrite_ranges) free(rewrite_ranges);
241 owns_rewrite_ranges = true;
243 // Update references to the new array
244 rewrite_ranges = new_ranges;
245 rewrite_ranges_size += growth;
246 next_template_arg_index += growth;
249 //----------------------------------------------------
250 // Range and state management
251 //----------------------------------------------------
255 return (int)(write_ptr - buffer);
258 BufferRange EndRange(int start_cookie)
260 return { start_cookie, (int)(write_ptr - (buffer + start_cookie)) };
263 void ReorderRange(BufferRange source_range, int insertion_point_cookie)
265 // Ensure there's room the preserve the source range
266 if (write_ptr + source_range.length > buffer_end)
268 GrowBuffer(write_ptr + source_range.length - buffer_end);
271 // Reorder the content
272 memcpy(write_ptr, buffer + source_range.offset, source_range.length);
273 memmove(buffer + insertion_point_cookie + source_range.length,
274 buffer + insertion_point_cookie,
275 source_range.offset - insertion_point_cookie);
276 memcpy(buffer + insertion_point_cookie, write_ptr, source_range.length);
278 // Fix up rewritable ranges, covering both substitutions and templates
282 if (index == next_substitute_index) index = next_template_arg_index + 1;
283 if (index == rewrite_ranges_size) break;
285 // Affected ranges are either shuffled forward when after the
286 // insertion but before the source, or backward when inside the
288 int candidate_offset = rewrite_ranges[index].offset;
289 if (candidate_offset >= insertion_point_cookie)
291 if (candidate_offset < source_range.offset)
293 rewrite_ranges[index].offset += source_range.length;
295 else if (candidate_offset >= source_range.offset)
297 rewrite_ranges[index].offset -=
298 (source_range.offset - insertion_point_cookie);
305 void EndSubstitution(int start_cookie)
307 if (next_substitute_index == next_template_arg_index) GrowRewriteRanges();
309 int index = next_substitute_index++;
310 rewrite_ranges[index] = EndRange(start_cookie);
311 #ifdef DEBUG_SUBSTITUTIONS
312 printf("Saved substitution # %d = %.*s\n", index,
313 rewrite_ranges[index].length, buffer + start_cookie);
317 void EndTemplateArg(int start_cookie)
319 if (next_substitute_index == next_template_arg_index) GrowRewriteRanges();
321 int index = next_template_arg_index--;
322 rewrite_ranges[index] = EndRange(start_cookie);
323 #ifdef DEBUG_TEMPLATE_ARGS
324 printf("Saved template arg # %d = %.*s\n",
325 rewrite_ranges_size - index - 1,
326 rewrite_ranges[index].length, buffer + start_cookie);
330 void ResetTemplateArgs()
332 //TODO: this works, but is it the right thing to do?
333 // Should we push/pop somehow at the call sites?
334 next_template_arg_index = rewrite_ranges_size - 1;
337 //----------------------------------------------------
340 // Appends content to the existing output buffer
341 //----------------------------------------------------
343 void Write(char character)
345 if (write_ptr == buffer_end) GrowBuffer();
346 *write_ptr++ = character;
349 void Write(const char * content)
351 Write(content, strlen(content));
354 void Write(const char * content, long content_length)
356 char * end_write_ptr = write_ptr + content_length;
357 if (end_write_ptr > buffer_end)
359 GrowBuffer(end_write_ptr - buffer_end);
360 end_write_ptr = write_ptr + content_length;
362 memcpy(write_ptr, content, content_length);
363 write_ptr = end_write_ptr;
365 #define WRITE(x) Write(x, sizeof(x) - 1)
367 void WriteTemplateStart()
372 void WriteTemplateEnd()
374 // Put a space between terminal > characters when nesting templates
375 if (write_ptr != buffer && *(write_ptr - 1) == '>') WRITE(" >");
379 void WriteCommaSpace()
384 void WriteNamespaceSeparator()
389 void WriteStdPrefix()
394 void WriteQualifiers(int qualifiers, bool space_before_reference = true)
396 if (qualifiers & QualifierPointer) Write('*');
397 if (qualifiers & QualifierConst) WRITE(" const");
398 if (qualifiers & QualifierVolatile) WRITE(" volatile");
399 if (qualifiers & QualifierRestrict) WRITE(" restrict");
400 if (qualifiers & QualifierReference)
402 if (space_before_reference) WRITE(" &");
405 if (qualifiers & QualifierRValueReference)
407 if (space_before_reference) WRITE(" &&");
412 //----------------------------------------------------
415 // Write another copy of content already present
416 // earlier in the output buffer
417 //----------------------------------------------------
419 void RewriteRange(BufferRange range)
421 Write(buffer + range.offset, range.length);
424 bool RewriteSubstitution(int index)
426 if (index < 0 || index >= next_substitute_index)
428 #ifdef DEBUG_FAILURES
429 printf("*** Invalid substitution #%d\n", index);
433 RewriteRange(rewrite_ranges[index]);
437 bool RewriteTemplateArg(int template_index)
439 int index = rewrite_ranges_size - 1 - template_index;
440 if (template_index < 0 || index <= next_template_arg_index)
442 #ifdef DEBUG_FAILURES
443 printf("*** Invalid template arg reference #%d\n", template_index);
447 RewriteRange(rewrite_ranges[index]);
451 //----------------------------------------------------
454 // Provide information with return values instead of
455 // writing to the output buffer
457 // Values indicating failure guarantee that the pre-
458 // call read_ptr is unchanged
459 //----------------------------------------------------
463 unsigned char digit = *read_ptr - '0';
464 if (digit > 9) return -1;
469 digit = *++read_ptr - '0';
470 if (digit > 9) break;
472 count = count * 10 + digit;
477 int TryParseBase36Number()
479 char digit = *read_ptr;
481 if (digit >= '0' && digit <= '9') count = digit -= '0';
482 else if (digit >= 'A' && digit <= 'Z') count = digit -= ('A' - 10);
488 if (digit >= '0' && digit <= '9') digit -= '0';
489 else if (digit >= 'A' && digit <= 'Z') digit -= ('A' - 10);
492 count = count * 36 + digit;
497 // <builtin-type> ::= v # void
501 // ::= a # signed char
502 // ::= h # unsigned char
504 // ::= t # unsigned short
506 // ::= j # unsigned int
508 // ::= m # unsigned long
509 // ::= x # long long, __int64
510 // ::= y # unsigned long long, __int64
512 // ::= o # unsigned __int128
515 // ::= e # long double, __float80
516 // ::= g # __float128
518 // ::= Dd # IEEE 754r decimal floating point (64 bits)
519 // ::= De # IEEE 754r decimal floating point (128 bits)
520 // ::= Df # IEEE 754r decimal floating point (32 bits)
521 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
524 // ::= Da # auto (in dependent new-expressions)
525 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
526 // ::= u <source-name> # vendor extended type
528 const char * TryParseBuiltinType()
532 case 'v': return "void";
533 case 'w': return "wchar_t";
534 case 'b': return "bool";
535 case 'c': return "char";
536 case 'a': return "signed char";
537 case 'h': return "unsigned char";
538 case 's': return "short";
539 case 't': return "unsigned short";
540 case 'i': return "int";
541 case 'j': return "unsigned int";
542 case 'l': return "long";
543 case 'm': return "unsigned long";
544 case 'x': return "long long";
545 case 'y': return "unsigned long long";
546 case 'n': return "__int128";
547 case 'o': return "unsigned __int128";
548 case 'f': return "float";
549 case 'd': return "double";
550 case 'e': return "long double";
551 case 'g': return "__float128";
552 case 'z': return "...";
557 case 'd': return "decimal64";
558 case 'e': return "decimal128";
559 case 'f': return "decimal32";
560 case 'h': return "decimal16";
561 case 'i': return "char32_t";
562 case 's': return "char16_t";
563 case 'a': return "auto";
564 case 'c': return "decltype(auto)";
565 case 'n': return "std::nullptr_t";
577 // ::= ad # & (unary)
585 // ::= de # * (unary)
603 // ::= mm # -- (postfix in <expression> context)
606 // ::= ng # - (unary)
615 // ::= pp # ++ (postfix in <expression> context)
616 // ::= ps # + (unary)
623 // ::= cv <type> # (cast)
624 // ::= v <digit> <source-name> # vendor extended operator
626 Operator TryParseOperator()
633 case 'a': return { "&&", OperatorKind::Binary };
634 case 'd': return { "&", OperatorKind::Unary };
635 case 'n': return { "&", OperatorKind::Binary };
636 case 'N': return { "&=", OperatorKind::Binary };
637 case 'S': return { "=", OperatorKind::Binary };
644 case 'l': return { "()", OperatorKind::Other };
645 case 'm': return { ",", OperatorKind::Other };
646 case 'o': return { "~", OperatorKind::Unary };
647 case 'v': return { nullptr, OperatorKind::ConversionOperator };
654 case 'a': return { " delete[]", OperatorKind::Other };
655 case 'e': return { "*", OperatorKind::Unary };
656 case 'l': return { " delete", OperatorKind::Other };
657 case 'v': return { "/", OperatorKind::Binary };
658 case 'V': return { "/=", OperatorKind::Binary };
665 case 'o': return { "^", OperatorKind::Binary };
666 case 'O': return { "^=", OperatorKind::Binary };
667 case 'q': return { "==", OperatorKind::Binary };
674 case 'e': return { ">=", OperatorKind::Binary };
675 case 't': return { ">", OperatorKind::Binary };
682 case 'x': return { "[]", OperatorKind::Other };
689 case 'e': return { "<=", OperatorKind::Binary };
690 case 's': return { "<<", OperatorKind::Binary };
691 case 'S': return { "<<=", OperatorKind::Binary };
692 case 't': return { "<", OperatorKind::Binary };
693 // case 'i': return { "?", OperatorKind::Binary };
700 case 'i': return { "-", OperatorKind::Binary };
701 case 'I': return { "-=", OperatorKind::Binary };
702 case 'l': return { "*", OperatorKind::Binary };
703 case 'L': return { "*=", OperatorKind::Binary };
704 case 'm': return { "--", OperatorKind::Postfix };
711 case 'a': return { " new[]", OperatorKind::Other };
712 case 'e': return { "!=", OperatorKind::Binary };
713 case 'g': return { "-", OperatorKind::Unary };
714 case 't': return { "!", OperatorKind::Unary };
715 case 'w': return { " new", OperatorKind::Other };
722 case 'o': return { "||", OperatorKind::Binary };
723 case 'r': return { "|", OperatorKind::Binary };
724 case 'R': return { "|=", OperatorKind::Binary };
731 case 'm': return { "->*", OperatorKind::Binary };
732 case 's': return { "+", OperatorKind::Unary };
733 case 'l': return { "+", OperatorKind::Binary };
734 case 'L': return { "+=", OperatorKind::Binary };
735 case 'p': return { "++", OperatorKind::Postfix };
736 case 't': return { "->", OperatorKind::Binary };
743 case 'u': return { "?", OperatorKind::Ternary };
750 case 'm': return { "%", OperatorKind::Binary };
751 case 'M': return { "%=", OperatorKind::Binary };
752 case 's': return { ">>", OperatorKind::Binary };
753 case 'S': return { ">=", OperatorKind::Binary };
758 char digit = *read_ptr;
759 if (digit >= '0' && digit <= '9')
762 return { nullptr, OperatorKind::Vendor };
768 return { nullptr, OperatorKind::NoMatch };
771 // <CV-qualifiers> ::= [r] [V] [K]
772 // <ref-qualifier> ::= R # & ref-qualifier
773 // <ref-qualifier> ::= O # && ref-qualifier
775 int TryParseQualifiers(bool allow_cv, bool allow_ro)
777 int qualifiers = QualifierNone;
778 char next = *read_ptr;
781 if (next == 'r') // restrict
783 qualifiers |= QualifierRestrict;
786 if (next == 'V') // volatile
788 qualifiers |= QualifierVolatile;
791 if (next == 'K') // const
793 qualifiers |= QualifierConst;
802 qualifiers |= QualifierReference;
807 qualifiers |= QualifierRValueReference;
813 // <discriminator> := _ <non-negative number> # when number < 10
814 // := __ <non-negative number> _ # when number >= 10
815 // extension := decimal-digit+
817 int TryParseDiscriminator()
819 const char * discriminator_start = read_ptr;
821 // Test the extension first, since it's what Clang uses
822 int discriminator_value = TryParseNumber();
823 if (discriminator_value != -1) return discriminator_value;
825 char next = *read_ptr;
832 discriminator_value = TryParseNumber();
833 if (discriminator_value != -1 && *read_ptr++ != '_')
835 return discriminator_value;
838 else if (next >= '0' && next <= '9')
845 // Not a valid discriminator
846 read_ptr = discriminator_start;
850 //----------------------------------------------------
853 // Consume input starting from read_ptr and produce
854 // buffered output at write_ptr
856 // Failures return false and may leave read_ptr in an
857 // indeterminate state
858 //----------------------------------------------------
860 bool Parse(char character)
862 if (*read_ptr++ == character) return true;
863 #ifdef DEBUG_FAILURES
864 printf("*** Expected '%c'\n", character);
869 // <number> ::= [n] <non-negative decimal integer>
871 bool ParseNumber(bool allow_negative = false)
873 if (allow_negative && *read_ptr == 'n')
878 const char * before_digits = read_ptr;
881 unsigned char digit = *read_ptr - '0';
882 if (digit > 9) break;
885 if (int digit_count = (int)(read_ptr - before_digits))
887 Write(before_digits, digit_count);
890 #ifdef DEBUG_FAILURES
891 printf("*** Expected number\n");
896 // <substitution> ::= S <seq-id> _
898 // <substitution> ::= Sa # ::std::allocator
899 // <substitution> ::= Sb # ::std::basic_string
900 // <substitution> ::= Ss # ::std::basic_string < char,
901 // ::std::char_traits<char>,
902 // ::std::allocator<char> >
903 // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
904 // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
905 // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
907 bool ParseSubstitution()
909 const char * substitution;
912 case 'a': substitution = "std::allocator"; break;
913 case 'b': substitution = "std::basic_string"; break;
914 case 's': substitution = "std::string"; break;
915 case 'i': substitution = "std::istream"; break;
916 case 'o': substitution = "std::ostream"; break;
917 case 'd': substitution = "std::iostream"; break;
919 // A failed attempt to parse a number will return -1 which turns out to be
920 // perfect here as S_ is the first substitution, S0_ the next and so forth
921 int substitution_index = TryParseBase36Number();
922 if (*read_ptr++ != '_')
924 #ifdef DEBUG_FAILURES
925 printf("*** Expected terminal _ in substitution\n");
929 return RewriteSubstitution(substitution_index + 1);
936 // <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E
938 // <bare-function-type> ::= <signature type>+ # types are possible return type, then parameter types
940 bool ParseFunctionType(int inner_qualifiers = QualifierNone)
942 #ifdef DEBUG_FAILURES
943 printf("*** Function types not supported\n");
945 //TODO: first steps toward an implementation follow, but they're far
946 // from complete. Function types tend to bracket other types eg:
947 // int (*)() when used as the type for "name" becomes int (*name)().
948 // This makes substitution et al ... interesting.
951 if (*read_ptr == 'Y') ++read_ptr;;
953 int return_type_start_cookie = GetStartCookie();
954 if (!ParseType()) return false;
957 int insert_cookie = GetStartCookie();
959 bool first_param = true;
960 int qualifiers = QualifierNone;
974 if (*(read_ptr + 1) == 'E')
976 qualifiers = TryParseQualifiers(false, true);
983 if (first_param) first_param = false;
984 else WriteCommaSpace();
986 if (!ParseType()) return false;
995 WriteQualifiers(qualifiers);
996 EndSubstitution(return_type_start_cookie);
999 if (inner_qualifiers)
1001 int qualifier_start_cookie = GetStartCookie();
1003 WriteQualifiers(inner_qualifiers);
1005 ReorderRange(EndRange(qualifier_start_cookie), insert_cookie);
1010 // <array-type> ::= A <positive dimension number> _ <element type>
1011 // ::= A [<dimension expression>] _ <element type>
1013 bool ParseArrayType(int qualifiers = QualifierNone)
1015 #ifdef DEBUG_FAILURES
1016 printf("*** Array type unsupported\n");
1018 //TODO: We fail horribly when recalling these as substitutions or
1019 // templates and trying to constify them eg:
1020 // _ZN4llvm2cl5applyIA28_cNS0_3optIbLb0ENS0_6parserIbEEEEEEvRKT_PT0_
1022 //TODO: Chances are we don't do any better with references and pointers
1023 // that should be type (&) [] instead of type & []
1027 if (*read_ptr == '_')
1030 if (!ParseType()) return false;
1031 if (qualifiers) WriteQualifiers(qualifiers);
1037 const char * before_digits = read_ptr;
1038 if (TryParseNumber() != -1)
1040 const char * after_digits = read_ptr;
1041 if (!Parse('_')) return false;
1042 if (!ParseType()) return false;
1043 if (qualifiers) WriteQualifiers(qualifiers);
1046 Write(before_digits, after_digits - before_digits);
1050 int type_insertion_cookie = GetStartCookie();
1051 if (!ParseExpression()) return false;
1052 if (!Parse('_')) return false;
1054 int type_start_cookie = GetStartCookie();
1055 if (!ParseType()) return false;
1056 if (qualifiers) WriteQualifiers(qualifiers);
1059 ReorderRange(EndRange(type_start_cookie), type_insertion_cookie);
1066 // <pointer-to-member-type> ::= M <class type> <member type>
1068 //TODO: Determine how to handle pointers to function members correctly,
1069 // currently not an issue because we don't have function types at all...
1070 bool ParsePointerToMemberType()
1072 int insertion_cookie = GetStartCookie();
1074 if (!ParseType()) return false;
1077 int type_cookie = GetStartCookie();
1078 if (!ParseType()) return false;
1079 ReorderRange(EndRange(type_cookie), insertion_cookie);
1083 // <template-param> ::= T_ # first template parameter
1084 // ::= T <parameter-2 non-negative number> _
1086 bool ParseTemplateParam()
1088 int count = TryParseNumber();
1089 if (!Parse('_')) return false;
1091 // When no number is present we get -1, which is convenient since
1092 // T_ is the zeroth element T0_ is element 1, and so on
1093 return RewriteTemplateArg(count + 1);
1096 // <type> ::= <builtin-type>
1097 // ::= <function-type>
1098 // ::= <class-enum-type>
1100 // ::= <pointer-to-member-type>
1101 // ::= <template-param>
1102 // ::= <template-template-param> <template-args>
1104 // ::= <substitution>
1105 // ::= <CV-qualifiers> <type>
1106 // ::= P <type> # pointer-to
1107 // ::= R <type> # reference-to
1108 // ::= O <type> # rvalue reference-to (C++0x)
1109 // ::= C <type> # complex pair (C 2000)
1110 // ::= G <type> # imaginary (C 2000)
1111 // ::= Dp <type> # pack expansion (C++0x)
1112 // ::= U <source-name> <type> # vendor extended type qualifier
1113 // extension := U <objc-name> <objc-type> # objc-type<identifier>
1114 // extension := <vector-type> # <vector-type> starts with Dv
1116 // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
1117 // <objc-type> := <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
1121 #ifdef DEBUG_FAILURES
1122 const char * failed_type = read_ptr;
1124 int type_start_cookie = GetStartCookie();
1125 bool suppress_substitution = false;
1127 int qualifiers = TryParseQualifiers(true, false);
1132 switch (*read_ptr++)
1135 if (!ParseType()) return false;
1141 #ifdef DEBUG_FAILURES
1142 printf("*** Unsupported type: %.3s\n", failed_type);
1149 if (!ParseTemplateParam()) return false;
1153 if (!ParsePointerToMemberType()) return false;
1157 if (!ParseArrayType()) return false;
1161 if (!ParseFunctionType()) return false;
1164 if (*++read_ptr == 't')
1168 if (!ParseName()) return false;
1172 suppress_substitution = true;
1173 if (!ParseSubstitution()) return false;
1178 switch (*++read_ptr)
1182 if (!ParseFunctionType(QualifierPointer)) return false;
1185 if (!ParseType()) return false;
1194 if (!ParseType()) return false;
1201 if (!ParseType()) return false;
1209 #ifdef DEBUG_FAILURES
1210 printf("*** Unsupported type: %.3s\n", failed_type);
1213 // Test for common cases to avoid TryParseBuiltinType() overhead
1217 if (!ParseName()) return false;
1220 if (const char * builtin = TryParseBuiltinType())
1223 suppress_substitution = true;
1227 if (!ParseName()) return false;
1232 // Allow base substitutions to be suppressed, but always record
1233 // substitutions for the qualified variant
1234 if (!suppress_substitution) EndSubstitution(type_start_cookie);
1237 WriteQualifiers(qualifiers, false);
1238 EndSubstitution(type_start_cookie);
1243 // <unnamed-type-name> ::= Ut [ <nonnegative number> ] _
1244 // ::= <closure-type-name>
1246 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1248 // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
1250 bool ParseUnnamedTypeName(NameState & name_state)
1252 switch (*read_ptr++)
1256 int cookie = GetStartCookie();
1258 const char * before_digits = read_ptr;
1259 if (TryParseNumber() != -1) Write(before_digits,
1260 read_ptr - before_digits);
1261 if (!Parse('_')) return false;
1263 name_state.last_name_range = EndRange(cookie);
1268 int cookie = GetStartCookie();
1270 const char * before_digits = read_ptr;
1271 if (TryParseNumber() != -1) Write(before_digits,
1272 read_ptr - before_digits);
1273 if (!Parse('_')) return false;
1275 name_state.last_name_range = EndRange(cookie);
1279 #ifdef DEBUG_FAILURES
1280 printf("*** Lambda type names unsupported\n");
1284 #ifdef DEBUG_FAILURES
1285 printf("*** Unknown unnamed type %.3s\n", read_ptr - 2);
1290 // <ctor-dtor-name> ::= C1 # complete object constructor
1291 // ::= C2 # base object constructor
1292 // ::= C3 # complete object allocating constructor
1294 bool ParseCtor(NameState & name_state)
1296 char next = *read_ptr;
1297 if (next == '1' || next == '2' || next == '3' || next == '5')
1299 RewriteRange(name_state.last_name_range);
1300 name_state.has_no_return_type = true;
1304 #ifdef DEBUG_FAILURES
1305 printf("*** Broken constructor\n");
1310 // <ctor-dtor-name> ::= D0 # deleting destructor
1311 // ::= D1 # complete object destructor
1312 // ::= D2 # base object destructor
1314 bool ParseDtor(NameState & name_state)
1316 char next = *read_ptr;
1317 if (next == '0' || next == '1' || next == '2' || next == '5')
1320 RewriteRange(name_state.last_name_range);
1321 name_state.has_no_return_type = true;
1325 #ifdef DEBUG_FAILURES
1326 printf("*** Broken destructor\n");
1331 // See TryParseOperator()
1333 bool ParseOperatorName(NameState & name_state)
1335 #ifdef DEBUG_FAILURES
1336 const char * operator_ptr = read_ptr;
1338 Operator parsed_operator = TryParseOperator();
1339 if (parsed_operator.name)
1342 Write(parsed_operator.name);
1346 // Handle special operators
1347 switch (parsed_operator.kind)
1349 case OperatorKind::Vendor:
1351 return ParseSourceName();
1352 case OperatorKind::ConversionOperator:
1353 ResetTemplateArgs();
1354 name_state.has_no_return_type = true;
1358 #ifdef DEBUG_FAILURES
1359 printf("*** Unknown operator: %.2s\n", operator_ptr);
1365 // <source-name> ::= <positive length number> <identifier>
1367 bool ParseSourceName()
1369 int count = TryParseNumber();
1372 #ifdef DEBUG_FAILURES
1373 printf("*** Malformed source name, missing length count\n");
1378 const char * next_read_ptr = read_ptr + count;
1379 if (next_read_ptr > read_end)
1381 #ifdef DEBUG_FAILURES
1382 printf("*** Malformed source name, premature termination\n");
1387 if (count >= 10 && strncmp(read_ptr, "_GLOBAL__N", 10) == 0) WRITE("(anonymous namespace)");
1388 else Write(read_ptr, count);
1390 read_ptr = next_read_ptr;
1394 // <unqualified-name> ::= <operator-name>
1395 // ::= <ctor-dtor-name>
1396 // ::= <source-name>
1397 // ::= <unnamed-type-name>
1399 bool ParseUnqualifiedName(NameState & name_state)
1401 // Note that these are detected directly in ParseNestedName for
1402 // performance rather than switching on the same options twice
1403 char next = *read_ptr;
1408 return ParseCtor(name_state);
1411 return ParseDtor(name_state);
1414 return ParseUnnamedTypeName(name_state);
1426 int name_start_cookie = GetStartCookie();
1427 if (!ParseSourceName()) return false;
1428 name_state.last_name_range = EndRange(name_start_cookie);
1432 return ParseOperatorName(name_state);
1436 // <unscoped-name> ::= <unqualified-name>
1437 // ::= St <unqualified-name> # ::std::
1438 // extension ::= StL<unqualified-name>
1440 bool ParseUnscopedName(NameState & name_state)
1442 if (*read_ptr == 'S' && *(read_ptr + 1) == 't')
1445 if (*(read_ptr += 2) == 'L') ++read_ptr;
1447 return ParseUnqualifiedName(name_state);
1450 bool ParseIntegerLiteral(const char * prefix, const char * suffix,
1451 bool allow_negative)
1453 if (prefix) Write(prefix);
1454 if (!ParseNumber(allow_negative)) return false;
1455 if (suffix) Write(suffix);
1459 bool ParseBooleanLiteral()
1461 switch (*read_ptr++)
1463 case '0': WRITE("false"); break;
1464 case '1': WRITE("true"); break;
1466 #ifdef DEBUG_FAILURES
1467 printf("*** Boolean literal not 0 or 1\n");
1474 // <expr-primary> ::= L <type> <value number> E # integer literal
1475 // ::= L <type> <value float> E # floating literal
1476 // ::= L <string type> E # string literal
1477 // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
1478 // ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
1479 // ::= L <mangled-name> E # external name
1481 bool ParseExpressionPrimary()
1483 switch (*read_ptr++)
1485 case 'b': return ParseBooleanLiteral();
1486 case 'x': return ParseIntegerLiteral(nullptr, "ll", true);
1487 case 'l': return ParseIntegerLiteral(nullptr, "l", true);
1488 case 'i': return ParseIntegerLiteral(nullptr, nullptr, true);
1489 case 'n': return ParseIntegerLiteral("(__int128)", nullptr, true);
1490 case 'j': return ParseIntegerLiteral(nullptr, "u", false);
1491 case 'm': return ParseIntegerLiteral(nullptr, "ul", false);
1492 case 'y': return ParseIntegerLiteral(nullptr, "ull", false);
1493 case 'o': return ParseIntegerLiteral("(unsigned __int128)",
1496 if (*read_ptr++ == 'Z')
1498 if (!ParseEncoding()) return false;
1512 #ifdef DEBUG_FAILURES
1513 printf("*** Unsupported primary expression %.5s\n", read_ptr - 1);
1517 // Invalid mangled name per
1518 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
1519 #ifdef DEBUG_FAILURES
1520 printf("*** Invalid primary expr encoding\n");
1526 if (!ParseType()) return false;
1528 if (!ParseNumber()) return false;
1533 // <unresolved-type> ::= <template-param>
1535 // ::= <substitution>
1537 bool ParseUnresolvedType()
1539 int type_start_cookie = GetStartCookie();
1540 switch (*read_ptr++)
1543 if (!ParseTemplateParam()) return false;
1544 EndSubstitution(type_start_cookie);
1548 if (*read_ptr != 't') return ParseSubstitution();
1552 NameState type_name = {};
1553 if (!ParseUnqualifiedName(type_name)) return false;
1554 EndSubstitution(type_start_cookie);
1560 #ifdef DEBUG_FAILURES
1561 printf("*** Unsupported unqualified type: %3s\n", read_ptr - 1);
1567 // <base-unresolved-name> ::= <simple-id> # unresolved name
1568 // extension ::= <operator-name> # unresolved operator-function-id
1569 // extension ::= <operator-name> <template-args> # unresolved operator template-id
1570 // ::= on <operator-name> # unresolved operator-function-id
1571 // ::= on <operator-name> <template-args> # unresolved operator template-id
1572 // ::= dn <destructor-name> # destructor or pseudo-destructor;
1573 // # e.g. ~X or ~X<N-1>
1575 bool ParseBaseUnresolvedName()
1577 #ifdef DEBUG_FAILURES
1578 printf("*** Base unresolved name unsupported\n");
1583 // <unresolved-name>
1584 // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
1585 // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
1586 // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
1587 // # A::x, N::y, A<T>::z; "gs" means leading "::"
1588 // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
1589 // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
1590 // # T::N::x /decltype(p)::N::x
1591 // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
1593 bool ParseUnresolvedName()
1595 #ifdef DEBUG_FAILURES
1596 printf("*** Unresolved names not supported\n");
1598 //TODO: grammar for all of this seems unclear...
1601 if (*read_ptr == 'g' && *(read_ptr + 1) == 's')
1604 WriteNamespaceSeparator();
1608 // <expression> ::= <unary operator-name> <expression>
1609 // ::= <binary operator-name> <expression> <expression>
1610 // ::= <ternary operator-name> <expression> <expression> <expression>
1611 // ::= cl <expression>+ E # call
1612 // ::= cv <type> <expression> # conversion with one argument
1613 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
1614 // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
1615 // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
1616 // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
1617 // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
1618 // ::= [gs] dl <expression> # delete expression
1619 // ::= [gs] da <expression> # delete[] expression
1620 // ::= pp_ <expression> # prefix ++
1621 // ::= mm_ <expression> # prefix --
1622 // ::= ti <type> # typeid (type)
1623 // ::= te <expression> # typeid (expression)
1624 // ::= dc <type> <expression> # dynamic_cast<type> (expression)
1625 // ::= sc <type> <expression> # static_cast<type> (expression)
1626 // ::= cc <type> <expression> # const_cast<type> (expression)
1627 // ::= rc <type> <expression> # reinterpret_cast<type> (expression)
1628 // ::= st <type> # sizeof (a type)
1629 // ::= sz <expression> # sizeof (an expression)
1630 // ::= at <type> # alignof (a type)
1631 // ::= az <expression> # alignof (an expression)
1632 // ::= nx <expression> # noexcept (expression)
1633 // ::= <template-param>
1634 // ::= <function-param>
1635 // ::= dt <expression> <unresolved-name> # expr.name
1636 // ::= pt <expression> <unresolved-name> # expr->name
1637 // ::= ds <expression> <expression> # expr.*expr
1638 // ::= sZ <template-param> # size of a parameter pack
1639 // ::= sZ <function-param> # size of a function parameter pack
1640 // ::= sp <expression> # pack expansion
1641 // ::= tw <expression> # throw expression
1642 // ::= tr # throw with no operand (rethrow)
1643 // ::= <unresolved-name> # f(p), N::f(p), ::f(p),
1644 // # freestanding dependent name (e.g., T::x),
1645 // # objectless nonstatic member reference
1646 // ::= <expr-primary>
1648 bool ParseExpression()
1650 Operator expression_operator = TryParseOperator();
1651 switch (expression_operator.kind)
1653 case OperatorKind::Unary:
1654 Write(expression_operator.name);
1656 if (!ParseExpression()) return false;
1659 case OperatorKind::Binary:
1660 if (!ParseExpression()) return false;
1661 Write(expression_operator.name);
1662 return ParseExpression();
1663 case OperatorKind::Ternary:
1664 if (!ParseExpression()) return false;
1666 if (!ParseExpression()) return false;
1668 return ParseExpression();
1669 case OperatorKind::NoMatch:
1671 case OperatorKind::Other:
1673 #ifdef DEBUG_FAILURES
1674 printf("*** Unsupported operator: %s\n", expression_operator.name);
1679 switch (*read_ptr++)
1681 case 'T': return ParseTemplateParam();
1682 case 'L': return ParseExpressionPrimary();
1684 if (*read_ptr++ == 'r') return ParseUnresolvedName();
1688 return ParseExpressionPrimary();
1692 // <template-arg> ::= <type> # type or template
1693 // ::= X <expression> E # expression
1694 // ::= <expr-primary> # simple expressions
1695 // ::= J <template-arg>* E # argument pack
1696 // ::= LZ <encoding> E # extension
1698 bool ParseTemplateArg()
1700 switch (*read_ptr) {
1702 #ifdef DEBUG_FAILURES
1703 printf("*** Template argument packs unsupported\n");
1708 if (!ParseExpression()) return false;
1712 return ParseExpressionPrimary();
1718 // <template-args> ::= I <template-arg>* E
1719 // extension, the abi says <template-arg>+
1721 bool ParseTemplateArgs(bool record_template_args = false)
1723 if (record_template_args) ResetTemplateArgs();
1725 bool first_arg = true;
1726 while (*read_ptr != 'E')
1728 if (first_arg) first_arg = false;
1729 else WriteCommaSpace();
1731 int template_start_cookie = GetStartCookie();
1732 if (!ParseTemplateArg()) return false;
1733 if (record_template_args) EndTemplateArg(template_start_cookie);
1739 // <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1740 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1742 // <prefix> ::= <prefix> <unqualified-name>
1743 // ::= <template-prefix> <template-args>
1744 // ::= <template-param>
1747 // ::= <substitution>
1748 // ::= <prefix> <data-member-prefix>
1751 // <template-prefix> ::= <prefix> <template unqualified-name>
1752 // ::= <template-param>
1753 // ::= <substitution>
1755 // <unqualified-name> ::= <operator-name>
1756 // ::= <ctor-dtor-name>
1757 // ::= <source-name>
1758 // ::= <unnamed-type-name>
1760 bool ParseNestedName(NameState & name_state, bool parse_discriminator = false)
1762 int qualifiers = TryParseQualifiers(true, true);
1763 bool first_part = true;
1764 bool suppress_substitution = true;
1765 int name_start_cookie = GetStartCookie();
1768 char next = *read_ptr;
1775 // Record a substitution candidate for all prefixes, but not the full name
1776 if (suppress_substitution) suppress_substitution = false;
1777 else EndSubstitution(name_start_cookie);
1782 name_state.is_last_generic = true;
1783 WriteTemplateStart();
1784 if (!ParseTemplateArgs(name_state.parse_function_params)) return false;
1789 if (first_part) first_part = false;
1790 else WriteNamespaceSeparator();
1792 name_state.is_last_generic = false;
1806 int name_start_cookie = GetStartCookie();
1807 if (!ParseSourceName()) return false;
1808 name_state.last_name_range = EndRange(name_start_cookie);
1812 if (*++read_ptr == 't')
1816 if (!ParseUnqualifiedName(name_state)) return false;
1820 if (!ParseSubstitution()) return false;
1821 suppress_substitution = true;
1826 if (!ParseTemplateParam()) return false;
1830 if (!ParseCtor(name_state)) return false;
1834 switch (*(read_ptr + 1))
1838 #ifdef DEBUG_FAILURES
1839 printf("*** Decltype unsupported\n");
1844 if (!ParseDtor(name_state)) return false;
1849 if (!ParseUnnamedTypeName(name_state)) return false;
1853 if (!ParseUnqualifiedName(name_state)) return false;
1856 if (!ParseOperatorName(name_state)) return false;
1860 if (parse_discriminator) TryParseDiscriminator();
1861 if (name_state.parse_function_params &&
1862 !ParseFunctionArgs(name_state, name_start_cookie)) return false;
1863 if (qualifiers) WriteQualifiers(qualifiers);
1867 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1868 // := Z <function encoding> E s [<discriminator>]
1869 // := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
1871 bool ParseLocalName(bool parse_function_params)
1873 if (!ParseEncoding()) return false;
1874 if (!Parse('E')) return false;
1879 TryParseDiscriminator(); // Optional and ignored
1880 WRITE("::string literal");
1883 TryParseNumber(); // Optional and ignored
1884 WriteNamespaceSeparator();
1885 if (!ParseName()) return false;
1888 WriteNamespaceSeparator();
1889 if (!ParseName(parse_function_params, true)) return false;
1890 TryParseDiscriminator(); // Optional and ignored
1895 // <name> ::= <nested-name>
1897 // ::= <unscoped-template-name> <template-args>
1898 // ::= <unscoped-name>
1900 // <unscoped-template-name> ::= <unscoped-name>
1901 // ::= <substitution>
1903 bool ParseName(bool parse_function_params = false,
1904 bool parse_discriminator = false)
1906 NameState name_state = { parse_function_params };
1907 int name_start_cookie = GetStartCookie();
1913 return ParseNestedName(name_state, parse_discriminator);
1917 if (!ParseLocalName(parse_function_params)) return false;
1925 if (!ParseUnscopedName(name_state)) return false;
1927 if (*read_ptr == 'I')
1929 EndSubstitution(name_start_cookie);
1932 name_state.is_last_generic = true;
1933 WriteTemplateStart();
1934 if (!ParseTemplateArgs(parse_function_params)) return false;
1940 if (parse_discriminator) TryParseDiscriminator();
1941 if (parse_function_params &&
1942 !ParseFunctionArgs(name_state, name_start_cookie)) return false;
1946 // <call-offset> ::= h <nv-offset> _
1947 // ::= v <v-offset> _
1949 // <nv-offset> ::= <offset number>
1950 // # non-virtual base override
1952 // <v-offset> ::= <offset number> _ <virtual offset number>
1953 // # virtual base override, with vcall offset
1955 bool ParseCallOffset()
1957 switch (*read_ptr++)
1960 if (*read_ptr == 'n') ++read_ptr;
1961 if (TryParseNumber() == -1 || *read_ptr++ != '_') break;
1964 if (*read_ptr == 'n') ++read_ptr;
1965 if (TryParseNumber() == -1 || *read_ptr++ != '_') break;
1966 if (*read_ptr == 'n') ++read_ptr;
1967 if (TryParseNumber() == -1 || *read_ptr++ != '_') break;
1970 #ifdef DEBUG_FAILURES
1971 printf("*** Malformed call offset\n");
1976 // <special-name> ::= TV <type> # virtual table
1977 // ::= TT <type> # VTT structure (construction vtable index)
1978 // ::= TI <type> # typeinfo structure
1979 // ::= TS <type> # typeinfo name (null-terminated byte string)
1980 // ::= Tc <call-offset> <call-offset> <base encoding>
1981 // # base is the nominal target function of thunk
1982 // # first call-offset is 'this' adjustment
1983 // # second call-offset is result adjustment
1984 // ::= T <call-offset> <base encoding>
1985 // # base is the nominal target function of thunk
1986 // extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
1988 bool ParseSpecialNameT()
1990 switch (*read_ptr++)
1993 WRITE("vtable for ");
1999 WRITE("typeinfo for ");
2002 WRITE("typeinfo name for ");
2006 #ifdef DEBUG_FAILURES
2007 printf("*** Unsupported thunk or construction vtable name: %.3s\n", read_ptr - 1);
2011 if (*--read_ptr == 'v')
2013 WRITE("virtual thunk to ");
2017 WRITE("non-virtual thunk to ");
2019 if (!ParseCallOffset()) return false;
2020 return ParseEncoding();
2024 // <special-name> ::= GV <object name> # Guard variable for one-time initialization
2026 // extension ::= GR <object name> # reference temporary for object
2028 bool ParseSpecialNameG()
2030 switch (*read_ptr++)
2033 WRITE("guard variable for ");
2034 if (!ParseName(true)) return false;
2037 WRITE("reference temporary for ");
2038 if (!ParseName(true)) return false;
2041 #ifdef DEBUG_FAILURES
2042 printf("*** Unknown G encoding\n");
2049 // <bare-function-type> ::= <signature type>+ # types are possible return type, then parameter types
2051 bool ParseFunctionArgs(NameState & name_state, int return_insert_cookie)
2053 char next = *read_ptr;
2054 if (next == 'E' || next == '\0' || next == '.') return true;
2056 // Clang has a bad habit of making unique manglings by just sticking numbers on the end of a symbol,
2057 // which is ambiguous with malformed source name manglings
2058 const char * before_clang_uniquing_test = read_ptr;
2059 if (TryParseNumber())
2061 if (*read_ptr == '\0') return true;
2062 read_ptr = before_clang_uniquing_test;
2065 if (name_state.is_last_generic && !name_state.has_no_return_type)
2067 int return_type_start_cookie = GetStartCookie();
2068 if (!ParseType()) return false;
2070 ReorderRange(EndRange(return_type_start_cookie),
2071 return_insert_cookie);
2075 bool first_param = true;
2088 // Not a formal part of the mangling specification, but clang emits suffixes starting with _block_invoke
2089 if (strncmp(read_ptr, "_block_invoke", 13) == 0)
2091 read_ptr += strlen(read_ptr);
2096 if (first_param) first_param = false;
2097 else WriteCommaSpace();
2099 if (!ParseType()) return false;
2108 // <encoding> ::= <function name> <bare-function-type>
2110 // ::= <special-name>
2112 bool ParseEncoding()
2118 if (!ParseSpecialNameT()) return false;
2122 if (!ParseSpecialNameG()) return false;
2125 if (!ParseName(true)) return false;
2131 bool ParseMangling(const char * mangled_name, long mangled_name_length = 0)
2133 if (!mangled_name_length) mangled_name_length = strlen(mangled_name);
2134 read_end = mangled_name + mangled_name_length;
2135 read_ptr = mangled_name;
2137 next_substitute_index = 0;
2138 next_template_arg_index = rewrite_ranges_size - 1;
2140 if (*read_ptr++ != '_' || *read_ptr++ != 'Z')
2142 #ifdef DEBUG_FAILURES
2143 printf("*** Missing _Z prefix\n");
2147 if (!ParseEncoding()) return false;
2153 Write(read_ptr, read_end - read_ptr);
2158 #ifdef DEBUG_FAILURES
2159 printf("*** Unparsed mangled content\n");
2167 // External scratch storage used during demanglings
2170 const char * buffer_end;
2171 BufferRange * rewrite_ranges;
2172 int rewrite_ranges_size;
2174 bool owns_rewrite_ranges;
2176 // Internal state used during demangling
2178 const char * read_ptr;
2179 const char * read_end;
2181 int next_template_arg_index;
2182 int next_substitute_index;
2185 } // Anonymous namespace
2187 // Public entry points referenced from Mangled.cpp
2188 namespace lldb_private
2190 char * FastDemangle(const char* mangled_name)
2193 SymbolDemangler demangler(buffer, sizeof(buffer));
2194 return demangler.GetDemangledCopy(mangled_name);
2197 char * FastDemangle(const char* mangled_name, long mangled_name_length)
2200 SymbolDemangler demangler(buffer, sizeof(buffer));
2201 return demangler.GetDemangledCopy(mangled_name, mangled_name_length);
2203 } // lldb_private namespace