/src/abseil-cpp/absl/debugging/internal/demangle.cc
Line | Count | Source |
1 | | // Copyright 2018 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // For reference check out: |
16 | | // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling |
17 | | // |
18 | | // Note that we only have partial C++11 support yet. |
19 | | |
20 | | #include "absl/debugging/internal/demangle.h" |
21 | | |
22 | | #include <cstdint> |
23 | | #include <cstdio> |
24 | | #include <cstdlib> |
25 | | #include <limits> |
26 | | #include <string> |
27 | | |
28 | | #include "absl/base/config.h" |
29 | | |
30 | | #if ABSL_INTERNAL_HAS_CXA_DEMANGLE |
31 | | #include <cxxabi.h> |
32 | | #endif |
33 | | |
34 | | namespace absl { |
35 | | ABSL_NAMESPACE_BEGIN |
36 | | namespace debugging_internal { |
37 | | |
38 | | typedef struct { |
39 | | const char *abbrev; |
40 | | const char *real_name; |
41 | | // Number of arguments in <expression> context, or 0 if disallowed. |
42 | | int arity; |
43 | | } AbbrevPair; |
44 | | |
45 | | // List of operators from Itanium C++ ABI. |
46 | | static const AbbrevPair kOperatorList[] = { |
47 | | // New has special syntax (not currently supported). |
48 | | {"nw", "new", 0}, |
49 | | {"na", "new[]", 0}, |
50 | | |
51 | | // Works except that the 'gs' prefix is not supported. |
52 | | {"dl", "delete", 1}, |
53 | | {"da", "delete[]", 1}, |
54 | | |
55 | | {"ps", "+", 1}, // "positive" |
56 | | {"ng", "-", 1}, // "negative" |
57 | | {"ad", "&", 1}, // "address-of" |
58 | | {"de", "*", 1}, // "dereference" |
59 | | {"co", "~", 1}, |
60 | | |
61 | | {"pl", "+", 2}, |
62 | | {"mi", "-", 2}, |
63 | | {"ml", "*", 2}, |
64 | | {"dv", "/", 2}, |
65 | | {"rm", "%", 2}, |
66 | | {"an", "&", 2}, |
67 | | {"or", "|", 2}, |
68 | | {"eo", "^", 2}, |
69 | | {"aS", "=", 2}, |
70 | | {"pL", "+=", 2}, |
71 | | {"mI", "-=", 2}, |
72 | | {"mL", "*=", 2}, |
73 | | {"dV", "/=", 2}, |
74 | | {"rM", "%=", 2}, |
75 | | {"aN", "&=", 2}, |
76 | | {"oR", "|=", 2}, |
77 | | {"eO", "^=", 2}, |
78 | | {"ls", "<<", 2}, |
79 | | {"rs", ">>", 2}, |
80 | | {"lS", "<<=", 2}, |
81 | | {"rS", ">>=", 2}, |
82 | | {"eq", "==", 2}, |
83 | | {"ne", "!=", 2}, |
84 | | {"lt", "<", 2}, |
85 | | {"gt", ">", 2}, |
86 | | {"le", "<=", 2}, |
87 | | {"ge", ">=", 2}, |
88 | | {"nt", "!", 1}, |
89 | | {"aa", "&&", 2}, |
90 | | {"oo", "||", 2}, |
91 | | {"pp", "++", 1}, |
92 | | {"mm", "--", 1}, |
93 | | {"cm", ",", 2}, |
94 | | {"pm", "->*", 2}, |
95 | | {"pt", "->", 0}, // Special syntax |
96 | | {"cl", "()", 0}, // Special syntax |
97 | | {"ix", "[]", 2}, |
98 | | {"qu", "?", 3}, |
99 | | {"st", "sizeof", 0}, // Special syntax |
100 | | {"sz", "sizeof", 1}, // Not a real operator name, but used in expressions. |
101 | | {nullptr, nullptr, 0}, |
102 | | }; |
103 | | |
104 | | // List of builtin types from Itanium C++ ABI. |
105 | | // |
106 | | // Invariant: only one- or two-character type abbreviations here. |
107 | | static const AbbrevPair kBuiltinTypeList[] = { |
108 | | {"v", "void", 0}, |
109 | | {"w", "wchar_t", 0}, |
110 | | {"b", "bool", 0}, |
111 | | {"c", "char", 0}, |
112 | | {"a", "signed char", 0}, |
113 | | {"h", "unsigned char", 0}, |
114 | | {"s", "short", 0}, |
115 | | {"t", "unsigned short", 0}, |
116 | | {"i", "int", 0}, |
117 | | {"j", "unsigned int", 0}, |
118 | | {"l", "long", 0}, |
119 | | {"m", "unsigned long", 0}, |
120 | | {"x", "long long", 0}, |
121 | | {"y", "unsigned long long", 0}, |
122 | | {"n", "__int128", 0}, |
123 | | {"o", "unsigned __int128", 0}, |
124 | | {"f", "float", 0}, |
125 | | {"d", "double", 0}, |
126 | | {"e", "long double", 0}, |
127 | | {"g", "__float128", 0}, |
128 | | {"z", "ellipsis", 0}, |
129 | | |
130 | | {"De", "decimal128", 0}, // IEEE 754r decimal floating point (128 bits) |
131 | | {"Dd", "decimal64", 0}, // IEEE 754r decimal floating point (64 bits) |
132 | | {"Dc", "decltype(auto)", 0}, |
133 | | {"Da", "auto", 0}, |
134 | | {"Dn", "std::nullptr_t", 0}, // i.e., decltype(nullptr) |
135 | | {"Df", "decimal32", 0}, // IEEE 754r decimal floating point (32 bits) |
136 | | {"Di", "char32_t", 0}, |
137 | | {"Du", "char8_t", 0}, |
138 | | {"Ds", "char16_t", 0}, |
139 | | {"Dh", "float16", 0}, // IEEE 754r half-precision float (16 bits) |
140 | | {nullptr, nullptr, 0}, |
141 | | }; |
142 | | |
143 | | // List of substitutions Itanium C++ ABI. |
144 | | static const AbbrevPair kSubstitutionList[] = { |
145 | | {"St", "", 0}, |
146 | | {"Sa", "allocator", 0}, |
147 | | {"Sb", "basic_string", 0}, |
148 | | // std::basic_string<char, std::char_traits<char>,std::allocator<char> > |
149 | | {"Ss", "string", 0}, |
150 | | // std::basic_istream<char, std::char_traits<char> > |
151 | | {"Si", "istream", 0}, |
152 | | // std::basic_ostream<char, std::char_traits<char> > |
153 | | {"So", "ostream", 0}, |
154 | | // std::basic_iostream<char, std::char_traits<char> > |
155 | | {"Sd", "iostream", 0}, |
156 | | {nullptr, nullptr, 0}, |
157 | | }; |
158 | | |
159 | | // State needed for demangling. This struct is copied in almost every stack |
160 | | // frame, so every byte counts. |
161 | | typedef struct { |
162 | | int mangled_idx; // Cursor of mangled name. |
163 | | int out_cur_idx; // Cursor of output string. |
164 | | int prev_name_idx; // For constructors/destructors. |
165 | | unsigned int prev_name_length : 16; // For constructors/destructors. |
166 | | signed int nest_level : 15; // For nested names. |
167 | | unsigned int append : 1; // Append flag. |
168 | | // Note: for some reason MSVC can't pack "bool append : 1" into the same int |
169 | | // with the above two fields, so we use an int instead. Amusingly it can pack |
170 | | // "signed bool" as expected, but relying on that to continue to be a legal |
171 | | // type seems ill-advised (as it's illegal in at least clang). |
172 | | } ParseState; |
173 | | |
174 | | static_assert(sizeof(ParseState) == 4 * sizeof(int), |
175 | | "unexpected size of ParseState"); |
176 | | |
177 | | // One-off state for demangling that's not subject to backtracking -- either |
178 | | // constant data, data that's intentionally immune to backtracking (steps), or |
179 | | // data that would never be changed by backtracking anyway (recursion_depth). |
180 | | // |
181 | | // Only one copy of this exists for each call to Demangle, so the size of this |
182 | | // struct is nearly inconsequential. |
183 | | typedef struct { |
184 | | const char *mangled_begin; // Beginning of input string. |
185 | | char *out; // Beginning of output string. |
186 | | int out_end_idx; // One past last allowed output character. |
187 | | int recursion_depth; // For stack exhaustion prevention. |
188 | | int steps; // Cap how much work we'll do, regardless of depth. |
189 | | ParseState parse_state; // Backtrackable state copied for most frames. |
190 | | } State; |
191 | | |
192 | | namespace { |
193 | | // Prevent deep recursion / stack exhaustion. |
194 | | // Also prevent unbounded handling of complex inputs. |
195 | | class ComplexityGuard { |
196 | | public: |
197 | 0 | explicit ComplexityGuard(State *state) : state_(state) { |
198 | 0 | ++state->recursion_depth; |
199 | 0 | ++state->steps; |
200 | 0 | } |
201 | 0 | ~ComplexityGuard() { --state_->recursion_depth; } |
202 | | |
203 | | // 256 levels of recursion seems like a reasonable upper limit on depth. |
204 | | // 128 is not enough to demagle synthetic tests from demangle_unittest.txt: |
205 | | // "_ZaaZZZZ..." and "_ZaaZcvZcvZ..." |
206 | | static constexpr int kRecursionDepthLimit = 256; |
207 | | |
208 | | // We're trying to pick a charitable upper-limit on how many parse steps are |
209 | | // necessary to handle something that a human could actually make use of. |
210 | | // This is mostly in place as a bound on how much work we'll do if we are |
211 | | // asked to demangle an mangled name from an untrusted source, so it should be |
212 | | // much larger than the largest expected symbol, but much smaller than the |
213 | | // amount of work we can do in, e.g., a second. |
214 | | // |
215 | | // Some real-world symbols from an arbitrary binary started failing between |
216 | | // 2^12 and 2^13, so we multiply the latter by an extra factor of 16 to set |
217 | | // the limit. |
218 | | // |
219 | | // Spending one second on 2^17 parse steps would require each step to take |
220 | | // 7.6us, or ~30000 clock cycles, so it's safe to say this can be done in |
221 | | // under a second. |
222 | | static constexpr int kParseStepsLimit = 1 << 17; |
223 | | |
224 | 0 | bool IsTooComplex() const { |
225 | 0 | return state_->recursion_depth > kRecursionDepthLimit || |
226 | 0 | state_->steps > kParseStepsLimit; |
227 | 0 | } |
228 | | |
229 | | private: |
230 | | State *state_; |
231 | | }; |
232 | | } // namespace |
233 | | |
234 | | // We don't use strlen() in libc since it's not guaranteed to be async |
235 | | // signal safe. |
236 | 0 | static size_t StrLen(const char *str) { |
237 | 0 | size_t len = 0; |
238 | 0 | while (*str != '\0') { |
239 | 0 | ++str; |
240 | 0 | ++len; |
241 | 0 | } |
242 | 0 | return len; |
243 | 0 | } |
244 | | |
245 | | // Returns true if "str" has at least "n" characters remaining. |
246 | 0 | static bool AtLeastNumCharsRemaining(const char *str, size_t n) { |
247 | 0 | for (size_t i = 0; i < n; ++i) { |
248 | 0 | if (str[i] == '\0') { |
249 | 0 | return false; |
250 | 0 | } |
251 | 0 | } |
252 | 0 | return true; |
253 | 0 | } |
254 | | |
255 | | // Returns true if "str" has "prefix" as a prefix. |
256 | 0 | static bool StrPrefix(const char *str, const char *prefix) { |
257 | 0 | size_t i = 0; |
258 | 0 | while (str[i] != '\0' && prefix[i] != '\0' && str[i] == prefix[i]) { |
259 | 0 | ++i; |
260 | 0 | } |
261 | 0 | return prefix[i] == '\0'; // Consumed everything in "prefix". |
262 | 0 | } |
263 | | |
264 | | static void InitState(State* state, |
265 | | const char* mangled, |
266 | | char* out, |
267 | 0 | size_t out_size) { |
268 | 0 | state->mangled_begin = mangled; |
269 | 0 | state->out = out; |
270 | 0 | state->out_end_idx = static_cast<int>(out_size); |
271 | 0 | state->recursion_depth = 0; |
272 | 0 | state->steps = 0; |
273 | |
|
274 | 0 | state->parse_state.mangled_idx = 0; |
275 | 0 | state->parse_state.out_cur_idx = 0; |
276 | 0 | state->parse_state.prev_name_idx = 0; |
277 | 0 | state->parse_state.prev_name_length = 0; |
278 | 0 | state->parse_state.nest_level = -1; |
279 | 0 | state->parse_state.append = true; |
280 | 0 | } |
281 | | |
282 | 0 | static inline const char *RemainingInput(State *state) { |
283 | 0 | return &state->mangled_begin[state->parse_state.mangled_idx]; |
284 | 0 | } |
285 | | |
286 | | // Returns true and advances "mangled_idx" if we find "one_char_token" |
287 | | // at "mangled_idx" position. It is assumed that "one_char_token" does |
288 | | // not contain '\0'. |
289 | 0 | static bool ParseOneCharToken(State *state, const char one_char_token) { |
290 | 0 | ComplexityGuard guard(state); |
291 | 0 | if (guard.IsTooComplex()) return false; |
292 | 0 | if (RemainingInput(state)[0] == one_char_token) { |
293 | 0 | ++state->parse_state.mangled_idx; |
294 | 0 | return true; |
295 | 0 | } |
296 | 0 | return false; |
297 | 0 | } |
298 | | |
299 | | // Returns true and advances "mangled_cur" if we find "two_char_token" |
300 | | // at "mangled_cur" position. It is assumed that "two_char_token" does |
301 | | // not contain '\0'. |
302 | 0 | static bool ParseTwoCharToken(State *state, const char *two_char_token) { |
303 | 0 | ComplexityGuard guard(state); |
304 | 0 | if (guard.IsTooComplex()) return false; |
305 | 0 | if (RemainingInput(state)[0] == two_char_token[0] && |
306 | 0 | RemainingInput(state)[1] == two_char_token[1]) { |
307 | 0 | state->parse_state.mangled_idx += 2; |
308 | 0 | return true; |
309 | 0 | } |
310 | 0 | return false; |
311 | 0 | } |
312 | | |
313 | | // Returns true and advances "mangled_cur" if we find any character in |
314 | | // "char_class" at "mangled_cur" position. |
315 | 0 | static bool ParseCharClass(State *state, const char *char_class) { |
316 | 0 | ComplexityGuard guard(state); |
317 | 0 | if (guard.IsTooComplex()) return false; |
318 | 0 | if (RemainingInput(state)[0] == '\0') { |
319 | 0 | return false; |
320 | 0 | } |
321 | 0 | const char *p = char_class; |
322 | 0 | for (; *p != '\0'; ++p) { |
323 | 0 | if (RemainingInput(state)[0] == *p) { |
324 | 0 | ++state->parse_state.mangled_idx; |
325 | 0 | return true; |
326 | 0 | } |
327 | 0 | } |
328 | 0 | return false; |
329 | 0 | } |
330 | | |
331 | 0 | static bool ParseDigit(State *state, int *digit) { |
332 | 0 | char c = RemainingInput(state)[0]; |
333 | 0 | if (ParseCharClass(state, "0123456789")) { |
334 | 0 | if (digit != nullptr) { |
335 | 0 | *digit = c - '0'; |
336 | 0 | } |
337 | 0 | return true; |
338 | 0 | } |
339 | 0 | return false; |
340 | 0 | } |
341 | | |
342 | | // This function is used for handling an optional non-terminal. |
343 | 0 | static bool Optional(bool /*status*/) { return true; } |
344 | | |
345 | | // This function is used for handling <non-terminal>+ syntax. |
346 | | typedef bool (*ParseFunc)(State *); |
347 | 0 | static bool OneOrMore(ParseFunc parse_func, State *state) { |
348 | 0 | if (parse_func(state)) { |
349 | 0 | while (parse_func(state)) { |
350 | 0 | } |
351 | 0 | return true; |
352 | 0 | } |
353 | 0 | return false; |
354 | 0 | } |
355 | | |
356 | | // This function is used for handling <non-terminal>* syntax. The function |
357 | | // always returns true and must be followed by a termination token or a |
358 | | // terminating sequence not handled by parse_func (e.g. |
359 | | // ParseOneCharToken(state, 'E')). |
360 | 0 | static bool ZeroOrMore(ParseFunc parse_func, State *state) { |
361 | 0 | while (parse_func(state)) { |
362 | 0 | } |
363 | 0 | return true; |
364 | 0 | } |
365 | | |
366 | | // Append "str" at "out_cur_idx". If there is an overflow, out_cur_idx is |
367 | | // set to out_end_idx+1. The output string is ensured to |
368 | | // always terminate with '\0' as long as there is no overflow. |
369 | 0 | static void Append(State *state, const char *const str, const size_t length) { |
370 | 0 | for (size_t i = 0; i < length; ++i) { |
371 | 0 | if (state->parse_state.out_cur_idx + 1 < |
372 | 0 | state->out_end_idx) { // +1 for '\0' |
373 | 0 | state->out[state->parse_state.out_cur_idx++] = str[i]; |
374 | 0 | } else { |
375 | | // signal overflow |
376 | 0 | state->parse_state.out_cur_idx = state->out_end_idx + 1; |
377 | 0 | break; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | if (state->parse_state.out_cur_idx < state->out_end_idx) { |
381 | 0 | state->out[state->parse_state.out_cur_idx] = |
382 | 0 | '\0'; // Terminate it with '\0' |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | | // We don't use equivalents in libc to avoid locale issues. |
387 | 0 | static bool IsLower(char c) { return c >= 'a' && c <= 'z'; } |
388 | | |
389 | 0 | static bool IsAlpha(char c) { |
390 | 0 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
391 | 0 | } |
392 | | |
393 | 0 | static bool IsDigit(char c) { return c >= '0' && c <= '9'; } |
394 | | |
395 | | // Returns true if "str" is a function clone suffix. These suffixes are used |
396 | | // by GCC 4.5.x and later versions (and our locally-modified version of GCC |
397 | | // 4.4.x) to indicate functions which have been cloned during optimization. |
398 | | // We treat any sequence (.<alpha>+.<digit>+)+ as a function clone suffix. |
399 | | // Additionally, '_' is allowed along with the alphanumeric sequence. |
400 | 0 | static bool IsFunctionCloneSuffix(const char *str) { |
401 | 0 | size_t i = 0; |
402 | 0 | while (str[i] != '\0') { |
403 | 0 | bool parsed = false; |
404 | | // Consume a single [.<alpha> | _]*[.<digit>]* sequence. |
405 | 0 | if (str[i] == '.' && (IsAlpha(str[i + 1]) || str[i + 1] == '_')) { |
406 | 0 | parsed = true; |
407 | 0 | i += 2; |
408 | 0 | while (IsAlpha(str[i]) || str[i] == '_') { |
409 | 0 | ++i; |
410 | 0 | } |
411 | 0 | } |
412 | 0 | if (str[i] == '.' && IsDigit(str[i + 1])) { |
413 | 0 | parsed = true; |
414 | 0 | i += 2; |
415 | 0 | while (IsDigit(str[i])) { |
416 | 0 | ++i; |
417 | 0 | } |
418 | 0 | } |
419 | 0 | if (!parsed) |
420 | 0 | return false; |
421 | 0 | } |
422 | 0 | return true; // Consumed everything in "str". |
423 | 0 | } |
424 | | |
425 | 0 | static bool EndsWith(State *state, const char chr) { |
426 | 0 | return state->parse_state.out_cur_idx > 0 && |
427 | 0 | state->parse_state.out_cur_idx < state->out_end_idx && |
428 | 0 | chr == state->out[state->parse_state.out_cur_idx - 1]; |
429 | 0 | } |
430 | | |
431 | | // Append "str" with some tweaks, iff "append" state is true. |
432 | | static void MaybeAppendWithLength(State *state, const char *const str, |
433 | 0 | const size_t length) { |
434 | 0 | if (state->parse_state.append && length > 0) { |
435 | | // Append a space if the output buffer ends with '<' and "str" |
436 | | // starts with '<' to avoid <<<. |
437 | 0 | if (str[0] == '<' && EndsWith(state, '<')) { |
438 | 0 | Append(state, " ", 1); |
439 | 0 | } |
440 | | // Remember the last identifier name for ctors/dtors, |
441 | | // but only if we haven't yet overflown the buffer. |
442 | 0 | if (state->parse_state.out_cur_idx < state->out_end_idx && |
443 | 0 | (IsAlpha(str[0]) || str[0] == '_')) { |
444 | 0 | state->parse_state.prev_name_idx = state->parse_state.out_cur_idx; |
445 | 0 | state->parse_state.prev_name_length = static_cast<unsigned int>(length); |
446 | 0 | } |
447 | 0 | Append(state, str, length); |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | | // Appends a positive decimal number to the output if appending is enabled. |
452 | 0 | static bool MaybeAppendDecimal(State *state, int val) { |
453 | | // Max {32-64}-bit unsigned int is 20 digits. |
454 | 0 | constexpr size_t kMaxLength = 20; |
455 | 0 | char buf[kMaxLength]; |
456 | | |
457 | | // We can't use itoa or sprintf as neither is specified to be |
458 | | // async-signal-safe. |
459 | 0 | if (state->parse_state.append) { |
460 | | // We can't have a one-before-the-beginning pointer, so instead start with |
461 | | // one-past-the-end and manipulate one character before the pointer. |
462 | 0 | char *p = &buf[kMaxLength]; |
463 | 0 | do { // val=0 is the only input that should write a leading zero digit. |
464 | 0 | *--p = static_cast<char>((val % 10) + '0'); |
465 | 0 | val /= 10; |
466 | 0 | } while (p > buf && val != 0); |
467 | | |
468 | | // 'p' landed on the last character we set. How convenient. |
469 | 0 | Append(state, p, kMaxLength - static_cast<size_t>(p - buf)); |
470 | 0 | } |
471 | |
|
472 | 0 | return true; |
473 | 0 | } |
474 | | |
475 | | // A convenient wrapper around MaybeAppendWithLength(). |
476 | | // Returns true so that it can be placed in "if" conditions. |
477 | 0 | static bool MaybeAppend(State *state, const char *const str) { |
478 | 0 | if (state->parse_state.append) { |
479 | 0 | size_t length = StrLen(str); |
480 | 0 | MaybeAppendWithLength(state, str, length); |
481 | 0 | } |
482 | 0 | return true; |
483 | 0 | } |
484 | | |
485 | | // This function is used for handling nested names. |
486 | 0 | static bool EnterNestedName(State *state) { |
487 | 0 | state->parse_state.nest_level = 0; |
488 | 0 | return true; |
489 | 0 | } |
490 | | |
491 | | // This function is used for handling nested names. |
492 | 0 | static bool LeaveNestedName(State *state, int16_t prev_value) { |
493 | 0 | state->parse_state.nest_level = prev_value; |
494 | 0 | return true; |
495 | 0 | } |
496 | | |
497 | | // Disable the append mode not to print function parameters, etc. |
498 | 0 | static bool DisableAppend(State *state) { |
499 | 0 | state->parse_state.append = false; |
500 | 0 | return true; |
501 | 0 | } |
502 | | |
503 | | // Restore the append mode to the previous state. |
504 | 0 | static bool RestoreAppend(State *state, bool prev_value) { |
505 | 0 | state->parse_state.append = prev_value; |
506 | 0 | return true; |
507 | 0 | } |
508 | | |
509 | | // Increase the nest level for nested names. |
510 | 0 | static void MaybeIncreaseNestLevel(State *state) { |
511 | 0 | if (state->parse_state.nest_level > -1) { |
512 | 0 | ++state->parse_state.nest_level; |
513 | 0 | } |
514 | 0 | } |
515 | | |
516 | | // Appends :: for nested names if necessary. |
517 | 0 | static void MaybeAppendSeparator(State *state) { |
518 | 0 | if (state->parse_state.nest_level >= 1) { |
519 | 0 | MaybeAppend(state, "::"); |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | | // Cancel the last separator if necessary. |
524 | 0 | static void MaybeCancelLastSeparator(State *state) { |
525 | 0 | if (state->parse_state.nest_level >= 1 && state->parse_state.append && |
526 | 0 | state->parse_state.out_cur_idx >= 2) { |
527 | 0 | state->parse_state.out_cur_idx -= 2; |
528 | 0 | state->out[state->parse_state.out_cur_idx] = '\0'; |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | // Returns true if the identifier of the given length pointed to by |
533 | | // "mangled_cur" is anonymous namespace. |
534 | 0 | static bool IdentifierIsAnonymousNamespace(State *state, size_t length) { |
535 | | // Returns true if "anon_prefix" is a proper prefix of "mangled_cur". |
536 | 0 | static const char anon_prefix[] = "_GLOBAL__N_"; |
537 | 0 | return (length > (sizeof(anon_prefix) - 1) && |
538 | 0 | StrPrefix(RemainingInput(state), anon_prefix)); |
539 | 0 | } |
540 | | |
541 | | // Forward declarations of our parsing functions. |
542 | | static bool ParseMangledName(State *state); |
543 | | static bool ParseEncoding(State *state); |
544 | | static bool ParseName(State *state); |
545 | | static bool ParseUnscopedName(State *state); |
546 | | static bool ParseNestedName(State *state); |
547 | | static bool ParsePrefix(State *state); |
548 | | static bool ParseUnqualifiedName(State *state); |
549 | | static bool ParseSourceName(State *state); |
550 | | static bool ParseLocalSourceName(State *state); |
551 | | static bool ParseUnnamedTypeName(State *state); |
552 | | static bool ParseNumber(State *state, int *number_out); |
553 | | static bool ParseFloatNumber(State *state); |
554 | | static bool ParseSeqId(State *state); |
555 | | static bool ParseIdentifier(State *state, size_t length); |
556 | | static bool ParseOperatorName(State *state, int *arity); |
557 | | static bool ParseSpecialName(State *state); |
558 | | static bool ParseCallOffset(State *state); |
559 | | static bool ParseNVOffset(State *state); |
560 | | static bool ParseVOffset(State *state); |
561 | | static bool ParseAbiTags(State *state); |
562 | | static bool ParseCtorDtorName(State *state); |
563 | | static bool ParseDecltype(State *state); |
564 | | static bool ParseType(State *state); |
565 | | static bool ParseCVQualifiers(State *state); |
566 | | static bool ParseBuiltinType(State *state); |
567 | | static bool ParseFunctionType(State *state); |
568 | | static bool ParseBareFunctionType(State *state); |
569 | | static bool ParseClassEnumType(State *state); |
570 | | static bool ParseArrayType(State *state); |
571 | | static bool ParsePointerToMemberType(State *state); |
572 | | static bool ParseTemplateParam(State *state); |
573 | | static bool ParseTemplateTemplateParam(State *state); |
574 | | static bool ParseTemplateArgs(State *state); |
575 | | static bool ParseTemplateArg(State *state); |
576 | | static bool ParseBaseUnresolvedName(State *state); |
577 | | static bool ParseUnresolvedName(State *state); |
578 | | static bool ParseExpression(State *state); |
579 | | static bool ParseExprPrimary(State *state); |
580 | | static bool ParseExprCastValue(State *state); |
581 | | static bool ParseLocalName(State *state); |
582 | | static bool ParseLocalNameSuffix(State *state); |
583 | | static bool ParseDiscriminator(State *state); |
584 | | static bool ParseSubstitution(State *state, bool accept_std); |
585 | | |
586 | | // Implementation note: the following code is a straightforward |
587 | | // translation of the Itanium C++ ABI defined in BNF with a couple of |
588 | | // exceptions. |
589 | | // |
590 | | // - Support GNU extensions not defined in the Itanium C++ ABI |
591 | | // - <prefix> and <template-prefix> are combined to avoid infinite loop |
592 | | // - Reorder patterns to shorten the code |
593 | | // - Reorder patterns to give greedier functions precedence |
594 | | // We'll mark "Less greedy than" for these cases in the code |
595 | | // |
596 | | // Each parsing function changes the parse state and returns true on |
597 | | // success, or returns false and doesn't change the parse state (note: |
598 | | // the parse-steps counter increases regardless of success or failure). |
599 | | // To ensure that the parse state isn't changed in the latter case, we |
600 | | // save the original state before we call multiple parsing functions |
601 | | // consecutively with &&, and restore it if unsuccessful. See |
602 | | // ParseEncoding() as an example of this convention. We follow the |
603 | | // convention throughout the code. |
604 | | // |
605 | | // Originally we tried to do demangling without following the full ABI |
606 | | // syntax but it turned out we needed to follow the full syntax to |
607 | | // parse complicated cases like nested template arguments. Note that |
608 | | // implementing a full-fledged demangler isn't trivial (libiberty's |
609 | | // cp-demangle.c has +4300 lines). |
610 | | // |
611 | | // Note that (foo) in <(foo) ...> is a modifier to be ignored. |
612 | | // |
613 | | // Reference: |
614 | | // - Itanium C++ ABI |
615 | | // <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling> |
616 | | |
617 | | // <mangled-name> ::= _Z <encoding> |
618 | 0 | static bool ParseMangledName(State *state) { |
619 | 0 | ComplexityGuard guard(state); |
620 | 0 | if (guard.IsTooComplex()) return false; |
621 | 0 | return ParseTwoCharToken(state, "_Z") && ParseEncoding(state); |
622 | 0 | } |
623 | | |
624 | | // <encoding> ::= <(function) name> <bare-function-type> |
625 | | // ::= <(data) name> |
626 | | // ::= <special-name> |
627 | 0 | static bool ParseEncoding(State *state) { |
628 | 0 | ComplexityGuard guard(state); |
629 | 0 | if (guard.IsTooComplex()) return false; |
630 | | // Implementing the first two productions together as <name> |
631 | | // [<bare-function-type>] avoids exponential blowup of backtracking. |
632 | | // |
633 | | // Since Optional(...) can't fail, there's no need to copy the state for |
634 | | // backtracking. |
635 | 0 | if (ParseName(state) && Optional(ParseBareFunctionType(state))) { |
636 | 0 | return true; |
637 | 0 | } |
638 | | |
639 | 0 | if (ParseSpecialName(state)) { |
640 | 0 | return true; |
641 | 0 | } |
642 | 0 | return false; |
643 | 0 | } |
644 | | |
645 | | // <name> ::= <nested-name> |
646 | | // ::= <unscoped-template-name> <template-args> |
647 | | // ::= <unscoped-name> |
648 | | // ::= <local-name> |
649 | 0 | static bool ParseName(State *state) { |
650 | 0 | ComplexityGuard guard(state); |
651 | 0 | if (guard.IsTooComplex()) return false; |
652 | 0 | if (ParseNestedName(state) || ParseLocalName(state)) { |
653 | 0 | return true; |
654 | 0 | } |
655 | | |
656 | | // We reorganize the productions to avoid re-parsing unscoped names. |
657 | | // - Inline <unscoped-template-name> productions: |
658 | | // <name> ::= <substitution> <template-args> |
659 | | // ::= <unscoped-name> <template-args> |
660 | | // ::= <unscoped-name> |
661 | | // - Merge the two productions that start with unscoped-name: |
662 | | // <name> ::= <unscoped-name> [<template-args>] |
663 | | |
664 | 0 | ParseState copy = state->parse_state; |
665 | | // "std<...>" isn't a valid name. |
666 | 0 | if (ParseSubstitution(state, /*accept_std=*/false) && |
667 | 0 | ParseTemplateArgs(state)) { |
668 | 0 | return true; |
669 | 0 | } |
670 | 0 | state->parse_state = copy; |
671 | | |
672 | | // Note there's no need to restore state after this since only the first |
673 | | // subparser can fail. |
674 | 0 | return ParseUnscopedName(state) && Optional(ParseTemplateArgs(state)); |
675 | 0 | } |
676 | | |
677 | | // <unscoped-name> ::= <unqualified-name> |
678 | | // ::= St <unqualified-name> |
679 | 0 | static bool ParseUnscopedName(State *state) { |
680 | 0 | ComplexityGuard guard(state); |
681 | 0 | if (guard.IsTooComplex()) return false; |
682 | 0 | if (ParseUnqualifiedName(state)) { |
683 | 0 | return true; |
684 | 0 | } |
685 | | |
686 | 0 | ParseState copy = state->parse_state; |
687 | 0 | if (ParseTwoCharToken(state, "St") && MaybeAppend(state, "std::") && |
688 | 0 | ParseUnqualifiedName(state)) { |
689 | 0 | return true; |
690 | 0 | } |
691 | 0 | state->parse_state = copy; |
692 | 0 | return false; |
693 | 0 | } |
694 | | |
695 | | // <ref-qualifer> ::= R // lvalue method reference qualifier |
696 | | // ::= O // rvalue method reference qualifier |
697 | 0 | static inline bool ParseRefQualifier(State *state) { |
698 | 0 | return ParseCharClass(state, "OR"); |
699 | 0 | } |
700 | | |
701 | | // <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> |
702 | | // <unqualified-name> E |
703 | | // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> |
704 | | // <template-args> E |
705 | 0 | static bool ParseNestedName(State *state) { |
706 | 0 | ComplexityGuard guard(state); |
707 | 0 | if (guard.IsTooComplex()) return false; |
708 | 0 | ParseState copy = state->parse_state; |
709 | 0 | if (ParseOneCharToken(state, 'N') && EnterNestedName(state) && |
710 | 0 | Optional(ParseCVQualifiers(state)) && |
711 | 0 | Optional(ParseRefQualifier(state)) && ParsePrefix(state) && |
712 | 0 | LeaveNestedName(state, copy.nest_level) && |
713 | 0 | ParseOneCharToken(state, 'E')) { |
714 | 0 | return true; |
715 | 0 | } |
716 | 0 | state->parse_state = copy; |
717 | 0 | return false; |
718 | 0 | } |
719 | | |
720 | | // This part is tricky. If we literally translate them to code, we'll |
721 | | // end up infinite loop. Hence we merge them to avoid the case. |
722 | | // |
723 | | // <prefix> ::= <prefix> <unqualified-name> |
724 | | // ::= <template-prefix> <template-args> |
725 | | // ::= <template-param> |
726 | | // ::= <substitution> |
727 | | // ::= # empty |
728 | | // <template-prefix> ::= <prefix> <(template) unqualified-name> |
729 | | // ::= <template-param> |
730 | | // ::= <substitution> |
731 | 0 | static bool ParsePrefix(State *state) { |
732 | 0 | ComplexityGuard guard(state); |
733 | 0 | if (guard.IsTooComplex()) return false; |
734 | 0 | bool has_something = false; |
735 | 0 | while (true) { |
736 | 0 | MaybeAppendSeparator(state); |
737 | 0 | if (ParseTemplateParam(state) || |
738 | 0 | ParseSubstitution(state, /*accept_std=*/true) || |
739 | 0 | ParseUnscopedName(state) || |
740 | 0 | (ParseOneCharToken(state, 'M') && ParseUnnamedTypeName(state))) { |
741 | 0 | has_something = true; |
742 | 0 | MaybeIncreaseNestLevel(state); |
743 | 0 | continue; |
744 | 0 | } |
745 | 0 | MaybeCancelLastSeparator(state); |
746 | 0 | if (has_something && ParseTemplateArgs(state)) { |
747 | 0 | return ParsePrefix(state); |
748 | 0 | } else { |
749 | 0 | break; |
750 | 0 | } |
751 | 0 | } |
752 | 0 | return true; |
753 | 0 | } |
754 | | |
755 | | // <unqualified-name> ::= <operator-name> [<abi-tags>] |
756 | | // ::= <ctor-dtor-name> [<abi-tags>] |
757 | | // ::= <source-name> [<abi-tags>] |
758 | | // ::= <local-source-name> [<abi-tags>] |
759 | | // ::= <unnamed-type-name> [<abi-tags>] |
760 | | // |
761 | | // <local-source-name> is a GCC extension; see below. |
762 | 0 | static bool ParseUnqualifiedName(State *state) { |
763 | 0 | ComplexityGuard guard(state); |
764 | 0 | if (guard.IsTooComplex()) return false; |
765 | 0 | if (ParseOperatorName(state, nullptr) || ParseCtorDtorName(state) || |
766 | 0 | ParseSourceName(state) || ParseLocalSourceName(state) || |
767 | 0 | ParseUnnamedTypeName(state)) { |
768 | 0 | return ParseAbiTags(state); |
769 | 0 | } |
770 | 0 | return false; |
771 | 0 | } |
772 | | |
773 | | // <abi-tags> ::= <abi-tag> [<abi-tags>] |
774 | | // <abi-tag> ::= B <source-name> |
775 | 0 | static bool ParseAbiTags(State *state) { |
776 | 0 | ComplexityGuard guard(state); |
777 | 0 | if (guard.IsTooComplex()) return false; |
778 | | |
779 | 0 | while (ParseOneCharToken(state, 'B')) { |
780 | 0 | ParseState copy = state->parse_state; |
781 | 0 | MaybeAppend(state, "[abi:"); |
782 | |
|
783 | 0 | if (!ParseSourceName(state)) { |
784 | 0 | state->parse_state = copy; |
785 | 0 | return false; |
786 | 0 | } |
787 | 0 | MaybeAppend(state, "]"); |
788 | 0 | } |
789 | | |
790 | 0 | return true; |
791 | 0 | } |
792 | | |
793 | | // <source-name> ::= <positive length number> <identifier> |
794 | 0 | static bool ParseSourceName(State *state) { |
795 | 0 | ComplexityGuard guard(state); |
796 | 0 | if (guard.IsTooComplex()) return false; |
797 | 0 | ParseState copy = state->parse_state; |
798 | 0 | int length = -1; |
799 | 0 | if (ParseNumber(state, &length) && |
800 | 0 | ParseIdentifier(state, static_cast<size_t>(length))) { |
801 | 0 | return true; |
802 | 0 | } |
803 | 0 | state->parse_state = copy; |
804 | 0 | return false; |
805 | 0 | } |
806 | | |
807 | | // <local-source-name> ::= L <source-name> [<discriminator>] |
808 | | // |
809 | | // References: |
810 | | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 |
811 | | // https://gcc.gnu.org/viewcvs?view=rev&revision=124467 |
812 | 0 | static bool ParseLocalSourceName(State *state) { |
813 | 0 | ComplexityGuard guard(state); |
814 | 0 | if (guard.IsTooComplex()) return false; |
815 | 0 | ParseState copy = state->parse_state; |
816 | 0 | if (ParseOneCharToken(state, 'L') && ParseSourceName(state) && |
817 | 0 | Optional(ParseDiscriminator(state))) { |
818 | 0 | return true; |
819 | 0 | } |
820 | 0 | state->parse_state = copy; |
821 | 0 | return false; |
822 | 0 | } |
823 | | |
824 | | // <unnamed-type-name> ::= Ut [<(nonnegative) number>] _ |
825 | | // ::= <closure-type-name> |
826 | | // <closure-type-name> ::= Ul <lambda-sig> E [<(nonnegative) number>] _ |
827 | | // <lambda-sig> ::= <(parameter) type>+ |
828 | 0 | static bool ParseUnnamedTypeName(State *state) { |
829 | 0 | ComplexityGuard guard(state); |
830 | 0 | if (guard.IsTooComplex()) return false; |
831 | 0 | ParseState copy = state->parse_state; |
832 | | // Type's 1-based index n is encoded as { "", n == 1; itoa(n-2), otherwise }. |
833 | | // Optionally parse the encoded value into 'which' and add 2 to get the index. |
834 | 0 | int which = -1; |
835 | | |
836 | | // Unnamed type local to function or class. |
837 | 0 | if (ParseTwoCharToken(state, "Ut") && Optional(ParseNumber(state, &which)) && |
838 | 0 | which <= std::numeric_limits<int>::max() - 2 && // Don't overflow. |
839 | 0 | ParseOneCharToken(state, '_')) { |
840 | 0 | MaybeAppend(state, "{unnamed type#"); |
841 | 0 | MaybeAppendDecimal(state, 2 + which); |
842 | 0 | MaybeAppend(state, "}"); |
843 | 0 | return true; |
844 | 0 | } |
845 | 0 | state->parse_state = copy; |
846 | | |
847 | | // Closure type. |
848 | 0 | which = -1; |
849 | 0 | if (ParseTwoCharToken(state, "Ul") && DisableAppend(state) && |
850 | 0 | OneOrMore(ParseType, state) && RestoreAppend(state, copy.append) && |
851 | 0 | ParseOneCharToken(state, 'E') && Optional(ParseNumber(state, &which)) && |
852 | 0 | which <= std::numeric_limits<int>::max() - 2 && // Don't overflow. |
853 | 0 | ParseOneCharToken(state, '_')) { |
854 | 0 | MaybeAppend(state, "{lambda()#"); |
855 | 0 | MaybeAppendDecimal(state, 2 + which); |
856 | 0 | MaybeAppend(state, "}"); |
857 | 0 | return true; |
858 | 0 | } |
859 | 0 | state->parse_state = copy; |
860 | |
|
861 | 0 | return false; |
862 | 0 | } |
863 | | |
864 | | // <number> ::= [n] <non-negative decimal integer> |
865 | | // If "number_out" is non-null, then *number_out is set to the value of the |
866 | | // parsed number on success. |
867 | 0 | static bool ParseNumber(State *state, int *number_out) { |
868 | 0 | ComplexityGuard guard(state); |
869 | 0 | if (guard.IsTooComplex()) return false; |
870 | 0 | bool negative = false; |
871 | 0 | if (ParseOneCharToken(state, 'n')) { |
872 | 0 | negative = true; |
873 | 0 | } |
874 | 0 | const char *p = RemainingInput(state); |
875 | 0 | uint64_t number = 0; |
876 | 0 | for (; *p != '\0'; ++p) { |
877 | 0 | if (IsDigit(*p)) { |
878 | 0 | number = number * 10 + static_cast<uint64_t>(*p - '0'); |
879 | 0 | } else { |
880 | 0 | break; |
881 | 0 | } |
882 | 0 | } |
883 | | // Apply the sign with uint64_t arithmetic so overflows aren't UB. Gives |
884 | | // "incorrect" results for out-of-range inputs, but negative values only |
885 | | // appear for literals, which aren't printed. |
886 | 0 | if (negative) { |
887 | 0 | number = ~number + 1; |
888 | 0 | } |
889 | 0 | if (p != RemainingInput(state)) { // Conversion succeeded. |
890 | 0 | state->parse_state.mangled_idx += p - RemainingInput(state); |
891 | 0 | if (number_out != nullptr) { |
892 | | // Note: possibly truncate "number". |
893 | 0 | *number_out = static_cast<int>(number); |
894 | 0 | } |
895 | 0 | return true; |
896 | 0 | } |
897 | 0 | return false; |
898 | 0 | } |
899 | | |
900 | | // Floating-point literals are encoded using a fixed-length lowercase |
901 | | // hexadecimal string. |
902 | 0 | static bool ParseFloatNumber(State *state) { |
903 | 0 | ComplexityGuard guard(state); |
904 | 0 | if (guard.IsTooComplex()) return false; |
905 | 0 | const char *p = RemainingInput(state); |
906 | 0 | for (; *p != '\0'; ++p) { |
907 | 0 | if (!IsDigit(*p) && !(*p >= 'a' && *p <= 'f')) { |
908 | 0 | break; |
909 | 0 | } |
910 | 0 | } |
911 | 0 | if (p != RemainingInput(state)) { // Conversion succeeded. |
912 | 0 | state->parse_state.mangled_idx += p - RemainingInput(state); |
913 | 0 | return true; |
914 | 0 | } |
915 | 0 | return false; |
916 | 0 | } |
917 | | |
918 | | // The <seq-id> is a sequence number in base 36, |
919 | | // using digits and upper case letters |
920 | 0 | static bool ParseSeqId(State *state) { |
921 | 0 | ComplexityGuard guard(state); |
922 | 0 | if (guard.IsTooComplex()) return false; |
923 | 0 | const char *p = RemainingInput(state); |
924 | 0 | for (; *p != '\0'; ++p) { |
925 | 0 | if (!IsDigit(*p) && !(*p >= 'A' && *p <= 'Z')) { |
926 | 0 | break; |
927 | 0 | } |
928 | 0 | } |
929 | 0 | if (p != RemainingInput(state)) { // Conversion succeeded. |
930 | 0 | state->parse_state.mangled_idx += p - RemainingInput(state); |
931 | 0 | return true; |
932 | 0 | } |
933 | 0 | return false; |
934 | 0 | } |
935 | | |
936 | | // <identifier> ::= <unqualified source code identifier> (of given length) |
937 | 0 | static bool ParseIdentifier(State *state, size_t length) { |
938 | 0 | ComplexityGuard guard(state); |
939 | 0 | if (guard.IsTooComplex()) return false; |
940 | 0 | if (!AtLeastNumCharsRemaining(RemainingInput(state), length)) { |
941 | 0 | return false; |
942 | 0 | } |
943 | 0 | if (IdentifierIsAnonymousNamespace(state, length)) { |
944 | 0 | MaybeAppend(state, "(anonymous namespace)"); |
945 | 0 | } else { |
946 | 0 | MaybeAppendWithLength(state, RemainingInput(state), length); |
947 | 0 | } |
948 | 0 | state->parse_state.mangled_idx += length; |
949 | 0 | return true; |
950 | 0 | } |
951 | | |
952 | | // <operator-name> ::= nw, and other two letters cases |
953 | | // ::= cv <type> # (cast) |
954 | | // ::= v <digit> <source-name> # vendor extended operator |
955 | 0 | static bool ParseOperatorName(State *state, int *arity) { |
956 | 0 | ComplexityGuard guard(state); |
957 | 0 | if (guard.IsTooComplex()) return false; |
958 | 0 | if (!AtLeastNumCharsRemaining(RemainingInput(state), 2)) { |
959 | 0 | return false; |
960 | 0 | } |
961 | | // First check with "cv" (cast) case. |
962 | 0 | ParseState copy = state->parse_state; |
963 | 0 | if (ParseTwoCharToken(state, "cv") && MaybeAppend(state, "operator ") && |
964 | 0 | EnterNestedName(state) && ParseType(state) && |
965 | 0 | LeaveNestedName(state, copy.nest_level)) { |
966 | 0 | if (arity != nullptr) { |
967 | 0 | *arity = 1; |
968 | 0 | } |
969 | 0 | return true; |
970 | 0 | } |
971 | 0 | state->parse_state = copy; |
972 | | |
973 | | // Then vendor extended operators. |
974 | 0 | if (ParseOneCharToken(state, 'v') && ParseDigit(state, arity) && |
975 | 0 | ParseSourceName(state)) { |
976 | 0 | return true; |
977 | 0 | } |
978 | 0 | state->parse_state = copy; |
979 | | |
980 | | // Other operator names should start with a lower alphabet followed |
981 | | // by a lower/upper alphabet. |
982 | 0 | if (!(IsLower(RemainingInput(state)[0]) && |
983 | 0 | IsAlpha(RemainingInput(state)[1]))) { |
984 | 0 | return false; |
985 | 0 | } |
986 | | // We may want to perform a binary search if we really need speed. |
987 | 0 | const AbbrevPair *p; |
988 | 0 | for (p = kOperatorList; p->abbrev != nullptr; ++p) { |
989 | 0 | if (RemainingInput(state)[0] == p->abbrev[0] && |
990 | 0 | RemainingInput(state)[1] == p->abbrev[1]) { |
991 | 0 | if (arity != nullptr) { |
992 | 0 | *arity = p->arity; |
993 | 0 | } |
994 | 0 | MaybeAppend(state, "operator"); |
995 | 0 | if (IsLower(*p->real_name)) { // new, delete, etc. |
996 | 0 | MaybeAppend(state, " "); |
997 | 0 | } |
998 | 0 | MaybeAppend(state, p->real_name); |
999 | 0 | state->parse_state.mangled_idx += 2; |
1000 | 0 | return true; |
1001 | 0 | } |
1002 | 0 | } |
1003 | 0 | return false; |
1004 | 0 | } |
1005 | | |
1006 | | // <special-name> ::= TV <type> |
1007 | | // ::= TT <type> |
1008 | | // ::= TI <type> |
1009 | | // ::= TS <type> |
1010 | | // ::= TH <type> # thread-local |
1011 | | // ::= Tc <call-offset> <call-offset> <(base) encoding> |
1012 | | // ::= GV <(object) name> |
1013 | | // ::= T <call-offset> <(base) encoding> |
1014 | | // G++ extensions: |
1015 | | // ::= TC <type> <(offset) number> _ <(base) type> |
1016 | | // ::= TF <type> |
1017 | | // ::= TJ <type> |
1018 | | // ::= GR <name> |
1019 | | // ::= GA <encoding> |
1020 | | // ::= Th <call-offset> <(base) encoding> |
1021 | | // ::= Tv <call-offset> <(base) encoding> |
1022 | | // |
1023 | | // Note: we don't care much about them since they don't appear in |
1024 | | // stack traces. The are special data. |
1025 | 0 | static bool ParseSpecialName(State *state) { |
1026 | 0 | ComplexityGuard guard(state); |
1027 | 0 | if (guard.IsTooComplex()) return false; |
1028 | 0 | ParseState copy = state->parse_state; |
1029 | 0 | if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "VTISH") && |
1030 | 0 | ParseType(state)) { |
1031 | 0 | return true; |
1032 | 0 | } |
1033 | 0 | state->parse_state = copy; |
1034 | |
|
1035 | 0 | if (ParseTwoCharToken(state, "Tc") && ParseCallOffset(state) && |
1036 | 0 | ParseCallOffset(state) && ParseEncoding(state)) { |
1037 | 0 | return true; |
1038 | 0 | } |
1039 | 0 | state->parse_state = copy; |
1040 | |
|
1041 | 0 | if (ParseTwoCharToken(state, "GV") && ParseName(state)) { |
1042 | 0 | return true; |
1043 | 0 | } |
1044 | 0 | state->parse_state = copy; |
1045 | |
|
1046 | 0 | if (ParseOneCharToken(state, 'T') && ParseCallOffset(state) && |
1047 | 0 | ParseEncoding(state)) { |
1048 | 0 | return true; |
1049 | 0 | } |
1050 | 0 | state->parse_state = copy; |
1051 | | |
1052 | | // G++ extensions |
1053 | 0 | if (ParseTwoCharToken(state, "TC") && ParseType(state) && |
1054 | 0 | ParseNumber(state, nullptr) && ParseOneCharToken(state, '_') && |
1055 | 0 | DisableAppend(state) && ParseType(state)) { |
1056 | 0 | RestoreAppend(state, copy.append); |
1057 | 0 | return true; |
1058 | 0 | } |
1059 | 0 | state->parse_state = copy; |
1060 | |
|
1061 | 0 | if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "FJ") && |
1062 | 0 | ParseType(state)) { |
1063 | 0 | return true; |
1064 | 0 | } |
1065 | 0 | state->parse_state = copy; |
1066 | |
|
1067 | 0 | if (ParseTwoCharToken(state, "GR") && ParseName(state)) { |
1068 | 0 | return true; |
1069 | 0 | } |
1070 | 0 | state->parse_state = copy; |
1071 | |
|
1072 | 0 | if (ParseTwoCharToken(state, "GA") && ParseEncoding(state)) { |
1073 | 0 | return true; |
1074 | 0 | } |
1075 | 0 | state->parse_state = copy; |
1076 | |
|
1077 | 0 | if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "hv") && |
1078 | 0 | ParseCallOffset(state) && ParseEncoding(state)) { |
1079 | 0 | return true; |
1080 | 0 | } |
1081 | 0 | state->parse_state = copy; |
1082 | 0 | return false; |
1083 | 0 | } |
1084 | | |
1085 | | // <call-offset> ::= h <nv-offset> _ |
1086 | | // ::= v <v-offset> _ |
1087 | 0 | static bool ParseCallOffset(State *state) { |
1088 | 0 | ComplexityGuard guard(state); |
1089 | 0 | if (guard.IsTooComplex()) return false; |
1090 | 0 | ParseState copy = state->parse_state; |
1091 | 0 | if (ParseOneCharToken(state, 'h') && ParseNVOffset(state) && |
1092 | 0 | ParseOneCharToken(state, '_')) { |
1093 | 0 | return true; |
1094 | 0 | } |
1095 | 0 | state->parse_state = copy; |
1096 | |
|
1097 | 0 | if (ParseOneCharToken(state, 'v') && ParseVOffset(state) && |
1098 | 0 | ParseOneCharToken(state, '_')) { |
1099 | 0 | return true; |
1100 | 0 | } |
1101 | 0 | state->parse_state = copy; |
1102 | |
|
1103 | 0 | return false; |
1104 | 0 | } |
1105 | | |
1106 | | // <nv-offset> ::= <(offset) number> |
1107 | 0 | static bool ParseNVOffset(State *state) { |
1108 | 0 | ComplexityGuard guard(state); |
1109 | 0 | if (guard.IsTooComplex()) return false; |
1110 | 0 | return ParseNumber(state, nullptr); |
1111 | 0 | } |
1112 | | |
1113 | | // <v-offset> ::= <(offset) number> _ <(virtual offset) number> |
1114 | 0 | static bool ParseVOffset(State *state) { |
1115 | 0 | ComplexityGuard guard(state); |
1116 | 0 | if (guard.IsTooComplex()) return false; |
1117 | 0 | ParseState copy = state->parse_state; |
1118 | 0 | if (ParseNumber(state, nullptr) && ParseOneCharToken(state, '_') && |
1119 | 0 | ParseNumber(state, nullptr)) { |
1120 | 0 | return true; |
1121 | 0 | } |
1122 | 0 | state->parse_state = copy; |
1123 | 0 | return false; |
1124 | 0 | } |
1125 | | |
1126 | | // <ctor-dtor-name> ::= C1 | C2 | C3 | CI1 <base-class-type> | CI2 |
1127 | | // <base-class-type> |
1128 | | // ::= D0 | D1 | D2 |
1129 | | // # GCC extensions: "unified" constructor/destructor. See |
1130 | | // # |
1131 | | // https://github.com/gcc-mirror/gcc/blob/7ad17b583c3643bd4557f29b8391ca7ef08391f5/gcc/cp/mangle.c#L1847 |
1132 | | // ::= C4 | D4 |
1133 | 0 | static bool ParseCtorDtorName(State *state) { |
1134 | 0 | ComplexityGuard guard(state); |
1135 | 0 | if (guard.IsTooComplex()) return false; |
1136 | 0 | ParseState copy = state->parse_state; |
1137 | 0 | if (ParseOneCharToken(state, 'C')) { |
1138 | 0 | if (ParseCharClass(state, "1234")) { |
1139 | 0 | const char *const prev_name = |
1140 | 0 | state->out + state->parse_state.prev_name_idx; |
1141 | 0 | MaybeAppendWithLength(state, prev_name, |
1142 | 0 | state->parse_state.prev_name_length); |
1143 | 0 | return true; |
1144 | 0 | } else if (ParseOneCharToken(state, 'I') && ParseCharClass(state, "12") && |
1145 | 0 | ParseClassEnumType(state)) { |
1146 | 0 | return true; |
1147 | 0 | } |
1148 | 0 | } |
1149 | 0 | state->parse_state = copy; |
1150 | |
|
1151 | 0 | if (ParseOneCharToken(state, 'D') && ParseCharClass(state, "0124")) { |
1152 | 0 | const char *const prev_name = state->out + state->parse_state.prev_name_idx; |
1153 | 0 | MaybeAppend(state, "~"); |
1154 | 0 | MaybeAppendWithLength(state, prev_name, |
1155 | 0 | state->parse_state.prev_name_length); |
1156 | 0 | return true; |
1157 | 0 | } |
1158 | 0 | state->parse_state = copy; |
1159 | 0 | return false; |
1160 | 0 | } |
1161 | | |
1162 | | // <decltype> ::= Dt <expression> E # decltype of an id-expression or class |
1163 | | // # member access (C++0x) |
1164 | | // ::= DT <expression> E # decltype of an expression (C++0x) |
1165 | 0 | static bool ParseDecltype(State *state) { |
1166 | 0 | ComplexityGuard guard(state); |
1167 | 0 | if (guard.IsTooComplex()) return false; |
1168 | | |
1169 | 0 | ParseState copy = state->parse_state; |
1170 | 0 | if (ParseOneCharToken(state, 'D') && ParseCharClass(state, "tT") && |
1171 | 0 | ParseExpression(state) && ParseOneCharToken(state, 'E')) { |
1172 | 0 | return true; |
1173 | 0 | } |
1174 | 0 | state->parse_state = copy; |
1175 | |
|
1176 | 0 | return false; |
1177 | 0 | } |
1178 | | |
1179 | | // <type> ::= <CV-qualifiers> <type> |
1180 | | // ::= P <type> # pointer-to |
1181 | | // ::= R <type> # reference-to |
1182 | | // ::= O <type> # rvalue reference-to (C++0x) |
1183 | | // ::= C <type> # complex pair (C 2000) |
1184 | | // ::= G <type> # imaginary (C 2000) |
1185 | | // ::= U <source-name> <type> # vendor extended type qualifier |
1186 | | // ::= <builtin-type> |
1187 | | // ::= <function-type> |
1188 | | // ::= <class-enum-type> # note: just an alias for <name> |
1189 | | // ::= <array-type> |
1190 | | // ::= <pointer-to-member-type> |
1191 | | // ::= <template-template-param> <template-args> |
1192 | | // ::= <template-param> |
1193 | | // ::= <decltype> |
1194 | | // ::= <substitution> |
1195 | | // ::= Dp <type> # pack expansion of (C++0x) |
1196 | | // ::= Dv <num-elems> _ # GNU vector extension |
1197 | | // |
1198 | 0 | static bool ParseType(State *state) { |
1199 | 0 | ComplexityGuard guard(state); |
1200 | 0 | if (guard.IsTooComplex()) return false; |
1201 | 0 | ParseState copy = state->parse_state; |
1202 | | |
1203 | | // We should check CV-qualifers, and PRGC things first. |
1204 | | // |
1205 | | // CV-qualifiers overlap with some operator names, but an operator name is not |
1206 | | // valid as a type. To avoid an ambiguity that can lead to exponential time |
1207 | | // complexity, refuse to backtrack the CV-qualifiers. |
1208 | | // |
1209 | | // _Z4aoeuIrMvvE |
1210 | | // => _Z 4aoeuI rM v v E |
1211 | | // aoeu<operator%=, void, void> |
1212 | | // => _Z 4aoeuI r Mv v E |
1213 | | // aoeu<void void::* restrict> |
1214 | | // |
1215 | | // By consuming the CV-qualifiers first, the former parse is disabled. |
1216 | 0 | if (ParseCVQualifiers(state)) { |
1217 | 0 | const bool result = ParseType(state); |
1218 | 0 | if (!result) state->parse_state = copy; |
1219 | 0 | return result; |
1220 | 0 | } |
1221 | 0 | state->parse_state = copy; |
1222 | | |
1223 | | // Similarly, these tag characters can overlap with other <name>s resulting in |
1224 | | // two different parse prefixes that land on <template-args> in the same |
1225 | | // place, such as "C3r1xI...". So, disable the "ctor-name = C3" parse by |
1226 | | // refusing to backtrack the tag characters. |
1227 | 0 | if (ParseCharClass(state, "OPRCG")) { |
1228 | 0 | const bool result = ParseType(state); |
1229 | 0 | if (!result) state->parse_state = copy; |
1230 | 0 | return result; |
1231 | 0 | } |
1232 | 0 | state->parse_state = copy; |
1233 | |
|
1234 | 0 | if (ParseTwoCharToken(state, "Dp") && ParseType(state)) { |
1235 | 0 | return true; |
1236 | 0 | } |
1237 | 0 | state->parse_state = copy; |
1238 | |
|
1239 | 0 | if (ParseOneCharToken(state, 'U') && ParseSourceName(state) && |
1240 | 0 | ParseType(state)) { |
1241 | 0 | return true; |
1242 | 0 | } |
1243 | 0 | state->parse_state = copy; |
1244 | |
|
1245 | 0 | if (ParseBuiltinType(state) || ParseFunctionType(state) || |
1246 | 0 | ParseClassEnumType(state) || ParseArrayType(state) || |
1247 | 0 | ParsePointerToMemberType(state) || ParseDecltype(state) || |
1248 | | // "std" on its own isn't a type. |
1249 | 0 | ParseSubstitution(state, /*accept_std=*/false)) { |
1250 | 0 | return true; |
1251 | 0 | } |
1252 | | |
1253 | 0 | if (ParseTemplateTemplateParam(state) && ParseTemplateArgs(state)) { |
1254 | 0 | return true; |
1255 | 0 | } |
1256 | 0 | state->parse_state = copy; |
1257 | | |
1258 | | // Less greedy than <template-template-param> <template-args>. |
1259 | 0 | if (ParseTemplateParam(state)) { |
1260 | 0 | return true; |
1261 | 0 | } |
1262 | | |
1263 | 0 | if (ParseTwoCharToken(state, "Dv") && ParseNumber(state, nullptr) && |
1264 | 0 | ParseOneCharToken(state, '_')) { |
1265 | 0 | return true; |
1266 | 0 | } |
1267 | 0 | state->parse_state = copy; |
1268 | |
|
1269 | 0 | return false; |
1270 | 0 | } |
1271 | | |
1272 | | // <CV-qualifiers> ::= [r] [V] [K] |
1273 | | // We don't allow empty <CV-qualifiers> to avoid infinite loop in |
1274 | | // ParseType(). |
1275 | 0 | static bool ParseCVQualifiers(State *state) { |
1276 | 0 | ComplexityGuard guard(state); |
1277 | 0 | if (guard.IsTooComplex()) return false; |
1278 | 0 | int num_cv_qualifiers = 0; |
1279 | 0 | num_cv_qualifiers += ParseOneCharToken(state, 'r'); |
1280 | 0 | num_cv_qualifiers += ParseOneCharToken(state, 'V'); |
1281 | 0 | num_cv_qualifiers += ParseOneCharToken(state, 'K'); |
1282 | 0 | return num_cv_qualifiers > 0; |
1283 | 0 | } |
1284 | | |
1285 | | // <builtin-type> ::= v, etc. # single-character builtin types |
1286 | | // ::= u <source-name> |
1287 | | // ::= Dd, etc. # two-character builtin types |
1288 | | // |
1289 | | // Not supported: |
1290 | | // ::= DF <number> _ # _FloatN (N bits) |
1291 | | // |
1292 | 0 | static bool ParseBuiltinType(State *state) { |
1293 | 0 | ComplexityGuard guard(state); |
1294 | 0 | if (guard.IsTooComplex()) return false; |
1295 | 0 | const AbbrevPair *p; |
1296 | 0 | for (p = kBuiltinTypeList; p->abbrev != nullptr; ++p) { |
1297 | | // Guaranteed only 1- or 2-character strings in kBuiltinTypeList. |
1298 | 0 | if (p->abbrev[1] == '\0') { |
1299 | 0 | if (ParseOneCharToken(state, p->abbrev[0])) { |
1300 | 0 | MaybeAppend(state, p->real_name); |
1301 | 0 | return true; |
1302 | 0 | } |
1303 | 0 | } else if (p->abbrev[2] == '\0' && ParseTwoCharToken(state, p->abbrev)) { |
1304 | 0 | MaybeAppend(state, p->real_name); |
1305 | 0 | return true; |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | 0 | ParseState copy = state->parse_state; |
1310 | 0 | if (ParseOneCharToken(state, 'u') && ParseSourceName(state)) { |
1311 | 0 | return true; |
1312 | 0 | } |
1313 | 0 | state->parse_state = copy; |
1314 | 0 | return false; |
1315 | 0 | } |
1316 | | |
1317 | | // <exception-spec> ::= Do # non-throwing |
1318 | | // exception-specification (e.g., |
1319 | | // noexcept, throw()) |
1320 | | // ::= DO <expression> E # computed (instantiation-dependent) |
1321 | | // noexcept |
1322 | | // ::= Dw <type>+ E # dynamic exception specification |
1323 | | // with instantiation-dependent types |
1324 | 0 | static bool ParseExceptionSpec(State *state) { |
1325 | 0 | ComplexityGuard guard(state); |
1326 | 0 | if (guard.IsTooComplex()) return false; |
1327 | | |
1328 | 0 | if (ParseTwoCharToken(state, "Do")) return true; |
1329 | | |
1330 | 0 | ParseState copy = state->parse_state; |
1331 | 0 | if (ParseTwoCharToken(state, "DO") && ParseExpression(state) && |
1332 | 0 | ParseOneCharToken(state, 'E')) { |
1333 | 0 | return true; |
1334 | 0 | } |
1335 | 0 | state->parse_state = copy; |
1336 | 0 | if (ParseTwoCharToken(state, "Dw") && OneOrMore(ParseType, state) && |
1337 | 0 | ParseOneCharToken(state, 'E')) { |
1338 | 0 | return true; |
1339 | 0 | } |
1340 | 0 | state->parse_state = copy; |
1341 | |
|
1342 | 0 | return false; |
1343 | 0 | } |
1344 | | |
1345 | | // <function-type> ::= [exception-spec] F [Y] <bare-function-type> [O] E |
1346 | 0 | static bool ParseFunctionType(State *state) { |
1347 | 0 | ComplexityGuard guard(state); |
1348 | 0 | if (guard.IsTooComplex()) return false; |
1349 | 0 | ParseState copy = state->parse_state; |
1350 | 0 | if (Optional(ParseExceptionSpec(state)) && ParseOneCharToken(state, 'F') && |
1351 | 0 | Optional(ParseOneCharToken(state, 'Y')) && ParseBareFunctionType(state) && |
1352 | 0 | Optional(ParseOneCharToken(state, 'O')) && |
1353 | 0 | ParseOneCharToken(state, 'E')) { |
1354 | 0 | return true; |
1355 | 0 | } |
1356 | 0 | state->parse_state = copy; |
1357 | 0 | return false; |
1358 | 0 | } |
1359 | | |
1360 | | // <bare-function-type> ::= <(signature) type>+ |
1361 | 0 | static bool ParseBareFunctionType(State *state) { |
1362 | 0 | ComplexityGuard guard(state); |
1363 | 0 | if (guard.IsTooComplex()) return false; |
1364 | 0 | ParseState copy = state->parse_state; |
1365 | 0 | DisableAppend(state); |
1366 | 0 | if (OneOrMore(ParseType, state)) { |
1367 | 0 | RestoreAppend(state, copy.append); |
1368 | 0 | MaybeAppend(state, "()"); |
1369 | 0 | return true; |
1370 | 0 | } |
1371 | 0 | state->parse_state = copy; |
1372 | 0 | return false; |
1373 | 0 | } |
1374 | | |
1375 | | // <class-enum-type> ::= <name> |
1376 | 0 | static bool ParseClassEnumType(State *state) { |
1377 | 0 | ComplexityGuard guard(state); |
1378 | 0 | if (guard.IsTooComplex()) return false; |
1379 | 0 | return ParseName(state); |
1380 | 0 | } |
1381 | | |
1382 | | // <array-type> ::= A <(positive dimension) number> _ <(element) type> |
1383 | | // ::= A [<(dimension) expression>] _ <(element) type> |
1384 | 0 | static bool ParseArrayType(State *state) { |
1385 | 0 | ComplexityGuard guard(state); |
1386 | 0 | if (guard.IsTooComplex()) return false; |
1387 | 0 | ParseState copy = state->parse_state; |
1388 | 0 | if (ParseOneCharToken(state, 'A') && ParseNumber(state, nullptr) && |
1389 | 0 | ParseOneCharToken(state, '_') && ParseType(state)) { |
1390 | 0 | return true; |
1391 | 0 | } |
1392 | 0 | state->parse_state = copy; |
1393 | |
|
1394 | 0 | if (ParseOneCharToken(state, 'A') && Optional(ParseExpression(state)) && |
1395 | 0 | ParseOneCharToken(state, '_') && ParseType(state)) { |
1396 | 0 | return true; |
1397 | 0 | } |
1398 | 0 | state->parse_state = copy; |
1399 | 0 | return false; |
1400 | 0 | } |
1401 | | |
1402 | | // <pointer-to-member-type> ::= M <(class) type> <(member) type> |
1403 | 0 | static bool ParsePointerToMemberType(State *state) { |
1404 | 0 | ComplexityGuard guard(state); |
1405 | 0 | if (guard.IsTooComplex()) return false; |
1406 | 0 | ParseState copy = state->parse_state; |
1407 | 0 | if (ParseOneCharToken(state, 'M') && ParseType(state) && ParseType(state)) { |
1408 | 0 | return true; |
1409 | 0 | } |
1410 | 0 | state->parse_state = copy; |
1411 | 0 | return false; |
1412 | 0 | } |
1413 | | |
1414 | | // <template-param> ::= T_ |
1415 | | // ::= T <parameter-2 non-negative number> _ |
1416 | 0 | static bool ParseTemplateParam(State *state) { |
1417 | 0 | ComplexityGuard guard(state); |
1418 | 0 | if (guard.IsTooComplex()) return false; |
1419 | 0 | if (ParseTwoCharToken(state, "T_")) { |
1420 | 0 | MaybeAppend(state, "?"); // We don't support template substitutions. |
1421 | 0 | return true; |
1422 | 0 | } |
1423 | | |
1424 | 0 | ParseState copy = state->parse_state; |
1425 | 0 | if (ParseOneCharToken(state, 'T') && ParseNumber(state, nullptr) && |
1426 | 0 | ParseOneCharToken(state, '_')) { |
1427 | 0 | MaybeAppend(state, "?"); // We don't support template substitutions. |
1428 | 0 | return true; |
1429 | 0 | } |
1430 | 0 | state->parse_state = copy; |
1431 | 0 | return false; |
1432 | 0 | } |
1433 | | |
1434 | | // <template-template-param> ::= <template-param> |
1435 | | // ::= <substitution> |
1436 | 0 | static bool ParseTemplateTemplateParam(State *state) { |
1437 | 0 | ComplexityGuard guard(state); |
1438 | 0 | if (guard.IsTooComplex()) return false; |
1439 | 0 | return (ParseTemplateParam(state) || |
1440 | | // "std" on its own isn't a template. |
1441 | 0 | ParseSubstitution(state, /*accept_std=*/false)); |
1442 | 0 | } |
1443 | | |
1444 | | // <template-args> ::= I <template-arg>+ E |
1445 | 0 | static bool ParseTemplateArgs(State *state) { |
1446 | 0 | ComplexityGuard guard(state); |
1447 | 0 | if (guard.IsTooComplex()) return false; |
1448 | 0 | ParseState copy = state->parse_state; |
1449 | 0 | DisableAppend(state); |
1450 | 0 | if (ParseOneCharToken(state, 'I') && OneOrMore(ParseTemplateArg, state) && |
1451 | 0 | ParseOneCharToken(state, 'E')) { |
1452 | 0 | RestoreAppend(state, copy.append); |
1453 | 0 | MaybeAppend(state, "<>"); |
1454 | 0 | return true; |
1455 | 0 | } |
1456 | 0 | state->parse_state = copy; |
1457 | 0 | return false; |
1458 | 0 | } |
1459 | | |
1460 | | // <template-arg> ::= <type> |
1461 | | // ::= <expr-primary> |
1462 | | // ::= J <template-arg>* E # argument pack |
1463 | | // ::= X <expression> E |
1464 | 0 | static bool ParseTemplateArg(State *state) { |
1465 | 0 | ComplexityGuard guard(state); |
1466 | 0 | if (guard.IsTooComplex()) return false; |
1467 | 0 | ParseState copy = state->parse_state; |
1468 | 0 | if (ParseOneCharToken(state, 'J') && ZeroOrMore(ParseTemplateArg, state) && |
1469 | 0 | ParseOneCharToken(state, 'E')) { |
1470 | 0 | return true; |
1471 | 0 | } |
1472 | 0 | state->parse_state = copy; |
1473 | | |
1474 | | // There can be significant overlap between the following leading to |
1475 | | // exponential backtracking: |
1476 | | // |
1477 | | // <expr-primary> ::= L <type> <expr-cast-value> E |
1478 | | // e.g. L 2xxIvE 1 E |
1479 | | // <type> ==> <local-source-name> <template-args> |
1480 | | // e.g. L 2xx IvE |
1481 | | // |
1482 | | // This means parsing an entire <type> twice, and <type> can contain |
1483 | | // <template-arg>, so this can generate exponential backtracking. There is |
1484 | | // only overlap when the remaining input starts with "L <source-name>", so |
1485 | | // parse all cases that can start this way jointly to share the common prefix. |
1486 | | // |
1487 | | // We have: |
1488 | | // |
1489 | | // <template-arg> ::= <type> |
1490 | | // ::= <expr-primary> |
1491 | | // |
1492 | | // First, drop all the productions of <type> that must start with something |
1493 | | // other than 'L'. All that's left is <class-enum-type>; inline it. |
1494 | | // |
1495 | | // <type> ::= <nested-name> # starts with 'N' |
1496 | | // ::= <unscoped-name> |
1497 | | // ::= <unscoped-template-name> <template-args> |
1498 | | // ::= <local-name> # starts with 'Z' |
1499 | | // |
1500 | | // Drop and inline again: |
1501 | | // |
1502 | | // <type> ::= <unscoped-name> |
1503 | | // ::= <unscoped-name> <template-args> |
1504 | | // ::= <substitution> <template-args> # starts with 'S' |
1505 | | // |
1506 | | // Merge the first two, inline <unscoped-name>, drop last: |
1507 | | // |
1508 | | // <type> ::= <unqualified-name> [<template-args>] |
1509 | | // ::= St <unqualified-name> [<template-args>] # starts with 'S' |
1510 | | // |
1511 | | // Drop and inline: |
1512 | | // |
1513 | | // <type> ::= <operator-name> [<template-args>] # starts with lowercase |
1514 | | // ::= <ctor-dtor-name> [<template-args>] # starts with 'C' or 'D' |
1515 | | // ::= <source-name> [<template-args>] # starts with digit |
1516 | | // ::= <local-source-name> [<template-args>] |
1517 | | // ::= <unnamed-type-name> [<template-args>] # starts with 'U' |
1518 | | // |
1519 | | // One more time: |
1520 | | // |
1521 | | // <type> ::= L <source-name> [<template-args>] |
1522 | | // |
1523 | | // Likewise with <expr-primary>: |
1524 | | // |
1525 | | // <expr-primary> ::= L <type> <expr-cast-value> E |
1526 | | // ::= LZ <encoding> E # cannot overlap; drop |
1527 | | // ::= L <mangled_name> E # cannot overlap; drop |
1528 | | // |
1529 | | // By similar reasoning as shown above, the only <type>s starting with |
1530 | | // <source-name> are "<source-name> [<template-args>]". Inline this. |
1531 | | // |
1532 | | // <expr-primary> ::= L <source-name> [<template-args>] <expr-cast-value> E |
1533 | | // |
1534 | | // Now inline both of these into <template-arg>: |
1535 | | // |
1536 | | // <template-arg> ::= L <source-name> [<template-args>] |
1537 | | // ::= L <source-name> [<template-args>] <expr-cast-value> E |
1538 | | // |
1539 | | // Merge them and we're done: |
1540 | | // <template-arg> |
1541 | | // ::= L <source-name> [<template-args>] [<expr-cast-value> E] |
1542 | 0 | if (ParseLocalSourceName(state) && Optional(ParseTemplateArgs(state))) { |
1543 | 0 | copy = state->parse_state; |
1544 | 0 | if (ParseExprCastValue(state) && ParseOneCharToken(state, 'E')) { |
1545 | 0 | return true; |
1546 | 0 | } |
1547 | 0 | state->parse_state = copy; |
1548 | 0 | return true; |
1549 | 0 | } |
1550 | | |
1551 | | // Now that the overlapping cases can't reach this code, we can safely call |
1552 | | // both of these. |
1553 | 0 | if (ParseType(state) || ParseExprPrimary(state)) { |
1554 | 0 | return true; |
1555 | 0 | } |
1556 | 0 | state->parse_state = copy; |
1557 | |
|
1558 | 0 | if (ParseOneCharToken(state, 'X') && ParseExpression(state) && |
1559 | 0 | ParseOneCharToken(state, 'E')) { |
1560 | 0 | return true; |
1561 | 0 | } |
1562 | 0 | state->parse_state = copy; |
1563 | 0 | return false; |
1564 | 0 | } |
1565 | | |
1566 | | // <unresolved-type> ::= <template-param> [<template-args>] |
1567 | | // ::= <decltype> |
1568 | | // ::= <substitution> |
1569 | 0 | static inline bool ParseUnresolvedType(State *state) { |
1570 | | // No ComplexityGuard because we don't copy the state in this stack frame. |
1571 | 0 | return (ParseTemplateParam(state) && Optional(ParseTemplateArgs(state))) || |
1572 | 0 | ParseDecltype(state) || ParseSubstitution(state, /*accept_std=*/false); |
1573 | 0 | } |
1574 | | |
1575 | | // <simple-id> ::= <source-name> [<template-args>] |
1576 | 0 | static inline bool ParseSimpleId(State *state) { |
1577 | | // No ComplexityGuard because we don't copy the state in this stack frame. |
1578 | | |
1579 | | // Note: <simple-id> cannot be followed by a parameter pack; see comment in |
1580 | | // ParseUnresolvedType. |
1581 | 0 | return ParseSourceName(state) && Optional(ParseTemplateArgs(state)); |
1582 | 0 | } |
1583 | | |
1584 | | // <base-unresolved-name> ::= <source-name> [<template-args>] |
1585 | | // ::= on <operator-name> [<template-args>] |
1586 | | // ::= dn <destructor-name> |
1587 | 0 | static bool ParseBaseUnresolvedName(State *state) { |
1588 | 0 | ComplexityGuard guard(state); |
1589 | 0 | if (guard.IsTooComplex()) return false; |
1590 | | |
1591 | 0 | if (ParseSimpleId(state)) { |
1592 | 0 | return true; |
1593 | 0 | } |
1594 | | |
1595 | 0 | ParseState copy = state->parse_state; |
1596 | 0 | if (ParseTwoCharToken(state, "on") && ParseOperatorName(state, nullptr) && |
1597 | 0 | Optional(ParseTemplateArgs(state))) { |
1598 | 0 | return true; |
1599 | 0 | } |
1600 | 0 | state->parse_state = copy; |
1601 | |
|
1602 | 0 | if (ParseTwoCharToken(state, "dn") && |
1603 | 0 | (ParseUnresolvedType(state) || ParseSimpleId(state))) { |
1604 | 0 | return true; |
1605 | 0 | } |
1606 | 0 | state->parse_state = copy; |
1607 | |
|
1608 | 0 | return false; |
1609 | 0 | } |
1610 | | |
1611 | | // <unresolved-name> ::= [gs] <base-unresolved-name> |
1612 | | // ::= sr <unresolved-type> <base-unresolved-name> |
1613 | | // ::= srN <unresolved-type> <unresolved-qualifier-level>+ E |
1614 | | // <base-unresolved-name> |
1615 | | // ::= [gs] sr <unresolved-qualifier-level>+ E |
1616 | | // <base-unresolved-name> |
1617 | 0 | static bool ParseUnresolvedName(State *state) { |
1618 | 0 | ComplexityGuard guard(state); |
1619 | 0 | if (guard.IsTooComplex()) return false; |
1620 | | |
1621 | 0 | ParseState copy = state->parse_state; |
1622 | 0 | if (Optional(ParseTwoCharToken(state, "gs")) && |
1623 | 0 | ParseBaseUnresolvedName(state)) { |
1624 | 0 | return true; |
1625 | 0 | } |
1626 | 0 | state->parse_state = copy; |
1627 | |
|
1628 | 0 | if (ParseTwoCharToken(state, "sr") && ParseUnresolvedType(state) && |
1629 | 0 | ParseBaseUnresolvedName(state)) { |
1630 | 0 | return true; |
1631 | 0 | } |
1632 | 0 | state->parse_state = copy; |
1633 | |
|
1634 | 0 | if (ParseTwoCharToken(state, "sr") && ParseOneCharToken(state, 'N') && |
1635 | 0 | ParseUnresolvedType(state) && |
1636 | 0 | OneOrMore(/* <unresolved-qualifier-level> ::= */ ParseSimpleId, state) && |
1637 | 0 | ParseOneCharToken(state, 'E') && ParseBaseUnresolvedName(state)) { |
1638 | 0 | return true; |
1639 | 0 | } |
1640 | 0 | state->parse_state = copy; |
1641 | |
|
1642 | 0 | if (Optional(ParseTwoCharToken(state, "gs")) && |
1643 | 0 | ParseTwoCharToken(state, "sr") && |
1644 | 0 | OneOrMore(/* <unresolved-qualifier-level> ::= */ ParseSimpleId, state) && |
1645 | 0 | ParseOneCharToken(state, 'E') && ParseBaseUnresolvedName(state)) { |
1646 | 0 | return true; |
1647 | 0 | } |
1648 | 0 | state->parse_state = copy; |
1649 | |
|
1650 | 0 | return false; |
1651 | 0 | } |
1652 | | |
1653 | | // <expression> ::= <1-ary operator-name> <expression> |
1654 | | // ::= <2-ary operator-name> <expression> <expression> |
1655 | | // ::= <3-ary operator-name> <expression> <expression> <expression> |
1656 | | // ::= cl <expression>+ E |
1657 | | // ::= cp <simple-id> <expression>* E # Clang-specific. |
1658 | | // ::= cv <type> <expression> # type (expression) |
1659 | | // ::= cv <type> _ <expression>* E # type (expr-list) |
1660 | | // ::= st <type> |
1661 | | // ::= <template-param> |
1662 | | // ::= <function-param> |
1663 | | // ::= <expr-primary> |
1664 | | // ::= dt <expression> <unresolved-name> # expr.name |
1665 | | // ::= pt <expression> <unresolved-name> # expr->name |
1666 | | // ::= sp <expression> # argument pack expansion |
1667 | | // ::= sr <type> <unqualified-name> <template-args> |
1668 | | // ::= sr <type> <unqualified-name> |
1669 | | // <function-param> ::= fp <(top-level) CV-qualifiers> _ |
1670 | | // ::= fp <(top-level) CV-qualifiers> <number> _ |
1671 | | // ::= fL <number> p <(top-level) CV-qualifiers> _ |
1672 | | // ::= fL <number> p <(top-level) CV-qualifiers> <number> _ |
1673 | 0 | static bool ParseExpression(State *state) { |
1674 | 0 | ComplexityGuard guard(state); |
1675 | 0 | if (guard.IsTooComplex()) return false; |
1676 | 0 | if (ParseTemplateParam(state) || ParseExprPrimary(state)) { |
1677 | 0 | return true; |
1678 | 0 | } |
1679 | | |
1680 | 0 | ParseState copy = state->parse_state; |
1681 | | |
1682 | | // Object/function call expression. |
1683 | 0 | if (ParseTwoCharToken(state, "cl") && OneOrMore(ParseExpression, state) && |
1684 | 0 | ParseOneCharToken(state, 'E')) { |
1685 | 0 | return true; |
1686 | 0 | } |
1687 | 0 | state->parse_state = copy; |
1688 | | |
1689 | | // Clang-specific "cp <simple-id> <expression>* E" |
1690 | | // https://clang.llvm.org/doxygen/ItaniumMangle_8cpp_source.html#l04338 |
1691 | 0 | if (ParseTwoCharToken(state, "cp") && ParseSimpleId(state) && |
1692 | 0 | ZeroOrMore(ParseExpression, state) && ParseOneCharToken(state, 'E')) { |
1693 | 0 | return true; |
1694 | 0 | } |
1695 | 0 | state->parse_state = copy; |
1696 | | |
1697 | | // Function-param expression (level 0). |
1698 | 0 | if (ParseTwoCharToken(state, "fp") && Optional(ParseCVQualifiers(state)) && |
1699 | 0 | Optional(ParseNumber(state, nullptr)) && ParseOneCharToken(state, '_')) { |
1700 | 0 | return true; |
1701 | 0 | } |
1702 | 0 | state->parse_state = copy; |
1703 | | |
1704 | | // Function-param expression (level 1+). |
1705 | 0 | if (ParseTwoCharToken(state, "fL") && Optional(ParseNumber(state, nullptr)) && |
1706 | 0 | ParseOneCharToken(state, 'p') && Optional(ParseCVQualifiers(state)) && |
1707 | 0 | Optional(ParseNumber(state, nullptr)) && ParseOneCharToken(state, '_')) { |
1708 | 0 | return true; |
1709 | 0 | } |
1710 | 0 | state->parse_state = copy; |
1711 | | |
1712 | | // Parse the conversion expressions jointly to avoid re-parsing the <type> in |
1713 | | // their common prefix. Parsed as: |
1714 | | // <expression> ::= cv <type> <conversion-args> |
1715 | | // <conversion-args> ::= _ <expression>* E |
1716 | | // ::= <expression> |
1717 | | // |
1718 | | // Also don't try ParseOperatorName after seeing "cv", since ParseOperatorName |
1719 | | // also needs to accept "cv <type>" in other contexts. |
1720 | 0 | if (ParseTwoCharToken(state, "cv")) { |
1721 | 0 | if (ParseType(state)) { |
1722 | 0 | ParseState copy2 = state->parse_state; |
1723 | 0 | if (ParseOneCharToken(state, '_') && ZeroOrMore(ParseExpression, state) && |
1724 | 0 | ParseOneCharToken(state, 'E')) { |
1725 | 0 | return true; |
1726 | 0 | } |
1727 | 0 | state->parse_state = copy2; |
1728 | 0 | if (ParseExpression(state)) { |
1729 | 0 | return true; |
1730 | 0 | } |
1731 | 0 | } |
1732 | 0 | } else { |
1733 | | // Parse unary, binary, and ternary operator expressions jointly, taking |
1734 | | // care not to re-parse subexpressions repeatedly. Parse like: |
1735 | | // <expression> ::= <operator-name> <expression> |
1736 | | // [<one-to-two-expressions>] |
1737 | | // <one-to-two-expressions> ::= <expression> [<expression>] |
1738 | 0 | int arity = -1; |
1739 | 0 | if (ParseOperatorName(state, &arity) && |
1740 | 0 | arity > 0 && // 0 arity => disabled. |
1741 | 0 | (arity < 3 || ParseExpression(state)) && |
1742 | 0 | (arity < 2 || ParseExpression(state)) && |
1743 | 0 | (arity < 1 || ParseExpression(state))) { |
1744 | 0 | return true; |
1745 | 0 | } |
1746 | 0 | } |
1747 | 0 | state->parse_state = copy; |
1748 | | |
1749 | | // sizeof type |
1750 | 0 | if (ParseTwoCharToken(state, "st") && ParseType(state)) { |
1751 | 0 | return true; |
1752 | 0 | } |
1753 | 0 | state->parse_state = copy; |
1754 | | |
1755 | | // Object and pointer member access expressions. |
1756 | 0 | if ((ParseTwoCharToken(state, "dt") || ParseTwoCharToken(state, "pt")) && |
1757 | 0 | ParseExpression(state) && ParseType(state)) { |
1758 | 0 | return true; |
1759 | 0 | } |
1760 | 0 | state->parse_state = copy; |
1761 | | |
1762 | | // Pointer-to-member access expressions. This parses the same as a binary |
1763 | | // operator, but it's implemented separately because "ds" shouldn't be |
1764 | | // accepted in other contexts that parse an operator name. |
1765 | 0 | if (ParseTwoCharToken(state, "ds") && ParseExpression(state) && |
1766 | 0 | ParseExpression(state)) { |
1767 | 0 | return true; |
1768 | 0 | } |
1769 | 0 | state->parse_state = copy; |
1770 | | |
1771 | | // Parameter pack expansion |
1772 | 0 | if (ParseTwoCharToken(state, "sp") && ParseExpression(state)) { |
1773 | 0 | return true; |
1774 | 0 | } |
1775 | 0 | state->parse_state = copy; |
1776 | |
|
1777 | 0 | return ParseUnresolvedName(state); |
1778 | 0 | } |
1779 | | |
1780 | | // <expr-primary> ::= L <type> <(value) number> E |
1781 | | // ::= L <type> <(value) float> E |
1782 | | // ::= L <mangled-name> E |
1783 | | // // A bug in g++'s C++ ABI version 2 (-fabi-version=2). |
1784 | | // ::= LZ <encoding> E |
1785 | | // |
1786 | | // Warning, subtle: the "bug" LZ production above is ambiguous with the first |
1787 | | // production where <type> starts with <local-name>, which can lead to |
1788 | | // exponential backtracking in two scenarios: |
1789 | | // |
1790 | | // - When whatever follows the E in the <local-name> in the first production is |
1791 | | // not a name, we backtrack the whole <encoding> and re-parse the whole thing. |
1792 | | // |
1793 | | // - When whatever follows the <local-name> in the first production is not a |
1794 | | // number and this <expr-primary> may be followed by a name, we backtrack the |
1795 | | // <name> and re-parse it. |
1796 | | // |
1797 | | // Moreover this ambiguity isn't always resolved -- for example, the following |
1798 | | // has two different parses: |
1799 | | // |
1800 | | // _ZaaILZ4aoeuE1x1EvE |
1801 | | // => operator&&<aoeu, x, E, void> |
1802 | | // => operator&&<(aoeu::x)(1), void> |
1803 | | // |
1804 | | // To resolve this, we just do what GCC's demangler does, and refuse to parse |
1805 | | // casts to <local-name> types. |
1806 | 0 | static bool ParseExprPrimary(State *state) { |
1807 | 0 | ComplexityGuard guard(state); |
1808 | 0 | if (guard.IsTooComplex()) return false; |
1809 | 0 | ParseState copy = state->parse_state; |
1810 | | |
1811 | | // The "LZ" special case: if we see LZ, we commit to accept "LZ <encoding> E" |
1812 | | // or fail, no backtracking. |
1813 | 0 | if (ParseTwoCharToken(state, "LZ")) { |
1814 | 0 | if (ParseEncoding(state) && ParseOneCharToken(state, 'E')) { |
1815 | 0 | return true; |
1816 | 0 | } |
1817 | | |
1818 | 0 | state->parse_state = copy; |
1819 | 0 | return false; |
1820 | 0 | } |
1821 | | |
1822 | | // The merged cast production. |
1823 | 0 | if (ParseOneCharToken(state, 'L') && ParseType(state) && |
1824 | 0 | ParseExprCastValue(state)) { |
1825 | 0 | return true; |
1826 | 0 | } |
1827 | 0 | state->parse_state = copy; |
1828 | |
|
1829 | 0 | if (ParseOneCharToken(state, 'L') && ParseMangledName(state) && |
1830 | 0 | ParseOneCharToken(state, 'E')) { |
1831 | 0 | return true; |
1832 | 0 | } |
1833 | 0 | state->parse_state = copy; |
1834 | |
|
1835 | 0 | return false; |
1836 | 0 | } |
1837 | | |
1838 | | // <number> or <float>, followed by 'E', as described above ParseExprPrimary. |
1839 | 0 | static bool ParseExprCastValue(State *state) { |
1840 | 0 | ComplexityGuard guard(state); |
1841 | 0 | if (guard.IsTooComplex()) return false; |
1842 | | // We have to be able to backtrack after accepting a number because we could |
1843 | | // have e.g. "7fffE", which will accept "7" as a number but then fail to find |
1844 | | // the 'E'. |
1845 | 0 | ParseState copy = state->parse_state; |
1846 | 0 | if (ParseNumber(state, nullptr) && ParseOneCharToken(state, 'E')) { |
1847 | 0 | return true; |
1848 | 0 | } |
1849 | 0 | state->parse_state = copy; |
1850 | |
|
1851 | 0 | if (ParseFloatNumber(state) && ParseOneCharToken(state, 'E')) { |
1852 | 0 | return true; |
1853 | 0 | } |
1854 | 0 | state->parse_state = copy; |
1855 | |
|
1856 | 0 | return false; |
1857 | 0 | } |
1858 | | |
1859 | | // <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>] |
1860 | | // ::= Z <(function) encoding> E s [<discriminator>] |
1861 | | // |
1862 | | // Parsing a common prefix of these two productions together avoids an |
1863 | | // exponential blowup of backtracking. Parse like: |
1864 | | // <local-name> := Z <encoding> E <local-name-suffix> |
1865 | | // <local-name-suffix> ::= s [<discriminator>] |
1866 | | // ::= <name> [<discriminator>] |
1867 | | |
1868 | 0 | static bool ParseLocalNameSuffix(State *state) { |
1869 | 0 | ComplexityGuard guard(state); |
1870 | 0 | if (guard.IsTooComplex()) return false; |
1871 | | |
1872 | 0 | if (MaybeAppend(state, "::") && ParseName(state) && |
1873 | 0 | Optional(ParseDiscriminator(state))) { |
1874 | 0 | return true; |
1875 | 0 | } |
1876 | | |
1877 | | // Since we're not going to overwrite the above "::" by re-parsing the |
1878 | | // <encoding> (whose trailing '\0' byte was in the byte now holding the |
1879 | | // first ':'), we have to rollback the "::" if the <name> parse failed. |
1880 | 0 | if (state->parse_state.append) { |
1881 | 0 | state->out[state->parse_state.out_cur_idx - 2] = '\0'; |
1882 | 0 | } |
1883 | |
|
1884 | 0 | return ParseOneCharToken(state, 's') && Optional(ParseDiscriminator(state)); |
1885 | 0 | } |
1886 | | |
1887 | 0 | static bool ParseLocalName(State *state) { |
1888 | 0 | ComplexityGuard guard(state); |
1889 | 0 | if (guard.IsTooComplex()) return false; |
1890 | 0 | ParseState copy = state->parse_state; |
1891 | 0 | if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) && |
1892 | 0 | ParseOneCharToken(state, 'E') && ParseLocalNameSuffix(state)) { |
1893 | 0 | return true; |
1894 | 0 | } |
1895 | 0 | state->parse_state = copy; |
1896 | 0 | return false; |
1897 | 0 | } |
1898 | | |
1899 | | // <discriminator> := _ <(non-negative) number> |
1900 | 0 | static bool ParseDiscriminator(State *state) { |
1901 | 0 | ComplexityGuard guard(state); |
1902 | 0 | if (guard.IsTooComplex()) return false; |
1903 | 0 | ParseState copy = state->parse_state; |
1904 | 0 | if (ParseOneCharToken(state, '_') && ParseNumber(state, nullptr)) { |
1905 | 0 | return true; |
1906 | 0 | } |
1907 | 0 | state->parse_state = copy; |
1908 | 0 | return false; |
1909 | 0 | } |
1910 | | |
1911 | | // <substitution> ::= S_ |
1912 | | // ::= S <seq-id> _ |
1913 | | // ::= St, etc. |
1914 | | // |
1915 | | // "St" is special in that it's not valid as a standalone name, and it *is* |
1916 | | // allowed to precede a name without being wrapped in "N...E". This means that |
1917 | | // if we accept it on its own, we can accept "St1a" and try to parse |
1918 | | // template-args, then fail and backtrack, accept "St" on its own, then "1a" as |
1919 | | // an unqualified name and re-parse the same template-args. To block this |
1920 | | // exponential backtracking, we disable it with 'accept_std=false' in |
1921 | | // problematic contexts. |
1922 | 0 | static bool ParseSubstitution(State *state, bool accept_std) { |
1923 | 0 | ComplexityGuard guard(state); |
1924 | 0 | if (guard.IsTooComplex()) return false; |
1925 | 0 | if (ParseTwoCharToken(state, "S_")) { |
1926 | 0 | MaybeAppend(state, "?"); // We don't support substitutions. |
1927 | 0 | return true; |
1928 | 0 | } |
1929 | | |
1930 | 0 | ParseState copy = state->parse_state; |
1931 | 0 | if (ParseOneCharToken(state, 'S') && ParseSeqId(state) && |
1932 | 0 | ParseOneCharToken(state, '_')) { |
1933 | 0 | MaybeAppend(state, "?"); // We don't support substitutions. |
1934 | 0 | return true; |
1935 | 0 | } |
1936 | 0 | state->parse_state = copy; |
1937 | | |
1938 | | // Expand abbreviations like "St" => "std". |
1939 | 0 | if (ParseOneCharToken(state, 'S')) { |
1940 | 0 | const AbbrevPair *p; |
1941 | 0 | for (p = kSubstitutionList; p->abbrev != nullptr; ++p) { |
1942 | 0 | if (RemainingInput(state)[0] == p->abbrev[1] && |
1943 | 0 | (accept_std || p->abbrev[1] != 't')) { |
1944 | 0 | MaybeAppend(state, "std"); |
1945 | 0 | if (p->real_name[0] != '\0') { |
1946 | 0 | MaybeAppend(state, "::"); |
1947 | 0 | MaybeAppend(state, p->real_name); |
1948 | 0 | } |
1949 | 0 | ++state->parse_state.mangled_idx; |
1950 | 0 | return true; |
1951 | 0 | } |
1952 | 0 | } |
1953 | 0 | } |
1954 | 0 | state->parse_state = copy; |
1955 | 0 | return false; |
1956 | 0 | } |
1957 | | |
1958 | | // Parse <mangled-name>, optionally followed by either a function-clone suffix |
1959 | | // or version suffix. Returns true only if all of "mangled_cur" was consumed. |
1960 | 0 | static bool ParseTopLevelMangledName(State *state) { |
1961 | 0 | ComplexityGuard guard(state); |
1962 | 0 | if (guard.IsTooComplex()) return false; |
1963 | 0 | if (ParseMangledName(state)) { |
1964 | 0 | if (RemainingInput(state)[0] != '\0') { |
1965 | | // Drop trailing function clone suffix, if any. |
1966 | 0 | if (IsFunctionCloneSuffix(RemainingInput(state))) { |
1967 | 0 | return true; |
1968 | 0 | } |
1969 | | // Append trailing version suffix if any. |
1970 | | // ex. _Z3foo@@GLIBCXX_3.4 |
1971 | 0 | if (RemainingInput(state)[0] == '@') { |
1972 | 0 | MaybeAppend(state, RemainingInput(state)); |
1973 | 0 | return true; |
1974 | 0 | } |
1975 | 0 | return false; // Unconsumed suffix. |
1976 | 0 | } |
1977 | 0 | return true; |
1978 | 0 | } |
1979 | 0 | return false; |
1980 | 0 | } |
1981 | | |
1982 | 0 | static bool Overflowed(const State *state) { |
1983 | 0 | return state->parse_state.out_cur_idx >= state->out_end_idx; |
1984 | 0 | } |
1985 | | |
1986 | | // The demangler entry point. |
1987 | 0 | bool Demangle(const char* mangled, char* out, size_t out_size) { |
1988 | 0 | State state; |
1989 | 0 | InitState(&state, mangled, out, out_size); |
1990 | 0 | return ParseTopLevelMangledName(&state) && !Overflowed(&state) && |
1991 | 0 | state.parse_state.out_cur_idx > 0; |
1992 | 0 | } |
1993 | | |
1994 | 0 | std::string DemangleString(const char* mangled) { |
1995 | 0 | std::string out; |
1996 | 0 | int status = 0; |
1997 | 0 | char* demangled = nullptr; |
1998 | 0 | #if ABSL_INTERNAL_HAS_CXA_DEMANGLE |
1999 | 0 | demangled = abi::__cxa_demangle(mangled, nullptr, nullptr, &status); |
2000 | 0 | #endif |
2001 | 0 | if (status == 0 && demangled != nullptr) { |
2002 | 0 | out.append(demangled); |
2003 | 0 | free(demangled); |
2004 | 0 | } else { |
2005 | 0 | out.append(mangled); |
2006 | 0 | } |
2007 | 0 | return out; |
2008 | 0 | } |
2009 | | |
2010 | | } // namespace debugging_internal |
2011 | | ABSL_NAMESPACE_END |
2012 | | } // namespace absl |