/src/binutils-gdb/libiberty/rust-demangle.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Demangler for the Rust programming language |
2 | | Copyright (C) 2016-2025 Free Software Foundation, Inc. |
3 | | Written by David Tolnay (dtolnay@gmail.com). |
4 | | Rewritten by Eduard-Mihai Burtescu (eddyb@lyken.rs) for v0 support. |
5 | | |
6 | | This file is part of the libiberty library. |
7 | | Libiberty is free software; you can redistribute it and/or |
8 | | modify it under the terms of the GNU Library General Public |
9 | | License as published by the Free Software Foundation; either |
10 | | version 2 of the License, or (at your option) any later version. |
11 | | |
12 | | In addition to the permissions in the GNU Library General Public |
13 | | License, the Free Software Foundation gives you unlimited permission |
14 | | to link the compiled version of this file into combinations with other |
15 | | programs, and to distribute those combinations without any restriction |
16 | | coming from the use of this file. (The Library Public License |
17 | | restrictions do apply in other respects; for example, they cover |
18 | | modification of the file, and distribution when not linked into a |
19 | | combined executable.) |
20 | | |
21 | | Libiberty is distributed in the hope that it will be useful, |
22 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 | | Library General Public License for more details. |
25 | | |
26 | | You should have received a copy of the GNU Library General Public |
27 | | License along with libiberty; see the file COPYING.LIB. |
28 | | If not, see <http://www.gnu.org/licenses/>. */ |
29 | | |
30 | | |
31 | | #ifdef HAVE_CONFIG_H |
32 | | #include "config.h" |
33 | | #endif |
34 | | |
35 | | #include "safe-ctype.h" |
36 | | |
37 | | #include <inttypes.h> |
38 | | #include <sys/types.h> |
39 | | #include <string.h> |
40 | | #include <stdio.h> |
41 | | #include <stdlib.h> |
42 | | |
43 | | #ifdef HAVE_STRING_H |
44 | | #include <string.h> |
45 | | #else |
46 | | extern size_t strlen(const char *s); |
47 | | extern int strncmp(const char *s1, const char *s2, size_t n); |
48 | | extern void *memset(void *s, int c, size_t n); |
49 | | #endif |
50 | | |
51 | | #include <demangle.h> |
52 | | #include "libiberty.h" |
53 | | |
54 | | struct rust_demangler |
55 | | { |
56 | | const char *sym; |
57 | | size_t sym_len; |
58 | | |
59 | | void *callback_opaque; |
60 | | demangle_callbackref callback; |
61 | | |
62 | | /* Position of the next character to read from the symbol. */ |
63 | | size_t next; |
64 | | |
65 | | /* Non-zero if any error occurred. */ |
66 | | int errored; |
67 | | |
68 | | /* Non-zero if nothing should be printed. */ |
69 | | int skipping_printing; |
70 | | |
71 | | /* Non-zero if printing should be verbose (e.g. include hashes). */ |
72 | | int verbose; |
73 | | |
74 | | /* Rust mangling version, with legacy mangling being -1. */ |
75 | | int version; |
76 | | |
77 | | /* Recursion depth. */ |
78 | | unsigned int recursion; |
79 | | /* Maximum number of times demangle_path may be called recursively. */ |
80 | 0 | #define RUST_MAX_RECURSION_COUNT 1024 |
81 | 0 | #define RUST_NO_RECURSION_LIMIT ((unsigned int) -1) |
82 | | |
83 | | uint64_t bound_lifetime_depth; |
84 | | }; |
85 | | |
86 | | /* Parsing functions. */ |
87 | | |
88 | | static char |
89 | | peek (const struct rust_demangler *rdm) |
90 | 0 | { |
91 | 0 | if (rdm->next < rdm->sym_len) |
92 | 0 | return rdm->sym[rdm->next]; |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | static int |
97 | | eat (struct rust_demangler *rdm, char c) |
98 | 0 | { |
99 | 0 | if (peek (rdm) == c) |
100 | 0 | { |
101 | 0 | rdm->next++; |
102 | 0 | return 1; |
103 | 0 | } |
104 | 0 | else |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | | static char |
109 | | next (struct rust_demangler *rdm) |
110 | 0 | { |
111 | 0 | char c = peek (rdm); |
112 | 0 | if (!c) |
113 | 0 | rdm->errored = 1; |
114 | 0 | else |
115 | 0 | rdm->next++; |
116 | 0 | return c; |
117 | 0 | } |
118 | | |
119 | | static uint64_t |
120 | | parse_integer_62 (struct rust_demangler *rdm) |
121 | 0 | { |
122 | 0 | char c; |
123 | 0 | uint64_t x; |
124 | |
|
125 | 0 | if (eat (rdm, '_')) |
126 | 0 | return 0; |
127 | | |
128 | 0 | x = 0; |
129 | 0 | while (!eat (rdm, '_') && !rdm->errored) |
130 | 0 | { |
131 | 0 | c = next (rdm); |
132 | 0 | x *= 62; |
133 | 0 | if (ISDIGIT (c)) |
134 | 0 | x += c - '0'; |
135 | 0 | else if (ISLOWER (c)) |
136 | 0 | x += 10 + (c - 'a'); |
137 | 0 | else if (ISUPPER (c)) |
138 | 0 | x += 10 + 26 + (c - 'A'); |
139 | 0 | else |
140 | 0 | { |
141 | 0 | rdm->errored = 1; |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | } |
145 | 0 | return x + 1; |
146 | 0 | } |
147 | | |
148 | | static uint64_t |
149 | | parse_opt_integer_62 (struct rust_demangler *rdm, char tag) |
150 | 0 | { |
151 | 0 | if (!eat (rdm, tag)) |
152 | 0 | return 0; |
153 | 0 | return 1 + parse_integer_62 (rdm); |
154 | 0 | } |
155 | | |
156 | | static uint64_t |
157 | | parse_disambiguator (struct rust_demangler *rdm) |
158 | 0 | { |
159 | 0 | return parse_opt_integer_62 (rdm, 's'); |
160 | 0 | } |
161 | | |
162 | | static size_t |
163 | | parse_hex_nibbles (struct rust_demangler *rdm, uint64_t *value) |
164 | 0 | { |
165 | 0 | char c; |
166 | 0 | size_t hex_len; |
167 | |
|
168 | 0 | hex_len = 0; |
169 | 0 | *value = 0; |
170 | |
|
171 | 0 | while (!eat (rdm, '_')) |
172 | 0 | { |
173 | 0 | *value <<= 4; |
174 | |
|
175 | 0 | c = next (rdm); |
176 | 0 | if (ISDIGIT (c)) |
177 | 0 | *value |= c - '0'; |
178 | 0 | else if (c >= 'a' && c <= 'f') |
179 | 0 | *value |= 10 + (c - 'a'); |
180 | 0 | else |
181 | 0 | { |
182 | 0 | rdm->errored = 1; |
183 | 0 | return 0; |
184 | 0 | } |
185 | 0 | hex_len++; |
186 | 0 | } |
187 | | |
188 | 0 | return hex_len; |
189 | 0 | } |
190 | | |
191 | | struct rust_mangled_ident |
192 | | { |
193 | | /* ASCII part of the identifier. */ |
194 | | const char *ascii; |
195 | | size_t ascii_len; |
196 | | |
197 | | /* Punycode insertion codes for Unicode codepoints, if any. */ |
198 | | const char *punycode; |
199 | | size_t punycode_len; |
200 | | }; |
201 | | |
202 | | static struct rust_mangled_ident |
203 | | parse_ident (struct rust_demangler *rdm) |
204 | 0 | { |
205 | 0 | char c; |
206 | 0 | size_t start, len; |
207 | 0 | int is_punycode = 0; |
208 | 0 | struct rust_mangled_ident ident; |
209 | |
|
210 | 0 | ident.ascii = NULL; |
211 | 0 | ident.ascii_len = 0; |
212 | 0 | ident.punycode = NULL; |
213 | 0 | ident.punycode_len = 0; |
214 | |
|
215 | 0 | if (rdm->version != -1) |
216 | 0 | is_punycode = eat (rdm, 'u'); |
217 | |
|
218 | 0 | c = next (rdm); |
219 | 0 | if (!ISDIGIT (c)) |
220 | 0 | { |
221 | 0 | rdm->errored = 1; |
222 | 0 | return ident; |
223 | 0 | } |
224 | 0 | len = c - '0'; |
225 | |
|
226 | 0 | if (c != '0') |
227 | 0 | while (ISDIGIT (peek (rdm))) |
228 | 0 | len = len * 10 + (next (rdm) - '0'); |
229 | | |
230 | | /* Skip past the optional `_` separator (v0). */ |
231 | 0 | if (rdm->version != -1) |
232 | 0 | eat (rdm, '_'); |
233 | |
|
234 | 0 | start = rdm->next; |
235 | 0 | rdm->next += len; |
236 | | /* Check for overflows. */ |
237 | 0 | if ((start > rdm->next) || (rdm->next > rdm->sym_len)) |
238 | 0 | { |
239 | 0 | rdm->errored = 1; |
240 | 0 | return ident; |
241 | 0 | } |
242 | | |
243 | 0 | ident.ascii = rdm->sym + start; |
244 | 0 | ident.ascii_len = len; |
245 | |
|
246 | 0 | if (is_punycode) |
247 | 0 | { |
248 | 0 | ident.punycode_len = 0; |
249 | 0 | while (ident.ascii_len > 0) |
250 | 0 | { |
251 | 0 | ident.ascii_len--; |
252 | | |
253 | | /* The last '_' is a separator between ascii & punycode. */ |
254 | 0 | if (ident.ascii[ident.ascii_len] == '_') |
255 | 0 | break; |
256 | | |
257 | 0 | ident.punycode_len++; |
258 | 0 | } |
259 | 0 | if (!ident.punycode_len) |
260 | 0 | { |
261 | 0 | rdm->errored = 1; |
262 | 0 | return ident; |
263 | 0 | } |
264 | 0 | ident.punycode = ident.ascii + (len - ident.punycode_len); |
265 | 0 | } |
266 | | |
267 | 0 | if (ident.ascii_len == 0) |
268 | 0 | ident.ascii = NULL; |
269 | |
|
270 | 0 | return ident; |
271 | 0 | } |
272 | | |
273 | | /* Printing functions. */ |
274 | | |
275 | | static void |
276 | | print_str (struct rust_demangler *rdm, const char *data, size_t len) |
277 | 0 | { |
278 | 0 | if (!rdm->errored && !rdm->skipping_printing) |
279 | 0 | rdm->callback (data, len, rdm->callback_opaque); |
280 | 0 | } |
281 | | |
282 | 0 | #define PRINT(s) print_str (rdm, s, strlen (s)) |
283 | | |
284 | | static void |
285 | | print_uint64 (struct rust_demangler *rdm, uint64_t x) |
286 | 0 | { |
287 | 0 | char s[21]; |
288 | 0 | snprintf (s, 21, "%" PRIu64, x); |
289 | 0 | PRINT (s); |
290 | 0 | } |
291 | | |
292 | | static void |
293 | | print_uint64_hex (struct rust_demangler *rdm, uint64_t x) |
294 | 0 | { |
295 | 0 | char s[17]; |
296 | 0 | snprintf (s, 17, "%" PRIx64, x); |
297 | 0 | PRINT (s); |
298 | 0 | } |
299 | | |
300 | | /* Return a 0x0-0xf value if the char is 0-9a-f, and -1 otherwise. */ |
301 | | static int |
302 | | decode_lower_hex_nibble (char nibble) |
303 | 0 | { |
304 | 0 | if ('0' <= nibble && nibble <= '9') |
305 | 0 | return nibble - '0'; |
306 | 0 | if ('a' <= nibble && nibble <= 'f') |
307 | 0 | return 0xa + (nibble - 'a'); |
308 | 0 | return -1; |
309 | 0 | } |
310 | | |
311 | | /* Return the unescaped character for a "$...$" escape, or 0 if invalid. */ |
312 | | static char |
313 | | decode_legacy_escape (const char *e, size_t len, size_t *out_len) |
314 | 0 | { |
315 | 0 | char c = 0; |
316 | 0 | size_t escape_len = 0; |
317 | 0 | int lo_nibble = -1, hi_nibble = -1; |
318 | |
|
319 | 0 | if (len < 3 || e[0] != '$') |
320 | 0 | return 0; |
321 | | |
322 | 0 | e++; |
323 | 0 | len--; |
324 | |
|
325 | 0 | if (e[0] == 'C') |
326 | 0 | { |
327 | 0 | escape_len = 1; |
328 | |
|
329 | 0 | c = ','; |
330 | 0 | } |
331 | 0 | else if (len > 2) |
332 | 0 | { |
333 | 0 | escape_len = 2; |
334 | |
|
335 | 0 | if (e[0] == 'S' && e[1] == 'P') |
336 | 0 | c = '@'; |
337 | 0 | else if (e[0] == 'B' && e[1] == 'P') |
338 | 0 | c = '*'; |
339 | 0 | else if (e[0] == 'R' && e[1] == 'F') |
340 | 0 | c = '&'; |
341 | 0 | else if (e[0] == 'L' && e[1] == 'T') |
342 | 0 | c = '<'; |
343 | 0 | else if (e[0] == 'G' && e[1] == 'T') |
344 | 0 | c = '>'; |
345 | 0 | else if (e[0] == 'L' && e[1] == 'P') |
346 | 0 | c = '('; |
347 | 0 | else if (e[0] == 'R' && e[1] == 'P') |
348 | 0 | c = ')'; |
349 | 0 | else if (e[0] == 'u' && len > 3) |
350 | 0 | { |
351 | 0 | escape_len = 3; |
352 | |
|
353 | 0 | hi_nibble = decode_lower_hex_nibble (e[1]); |
354 | 0 | if (hi_nibble < 0) |
355 | 0 | return 0; |
356 | 0 | lo_nibble = decode_lower_hex_nibble (e[2]); |
357 | 0 | if (lo_nibble < 0) |
358 | 0 | return 0; |
359 | | |
360 | | /* Only allow non-control ASCII characters. */ |
361 | 0 | if (hi_nibble > 7) |
362 | 0 | return 0; |
363 | 0 | c = (hi_nibble << 4) | lo_nibble; |
364 | 0 | if (c < 0x20) |
365 | 0 | return 0; |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | 0 | if (!c || len <= escape_len || e[escape_len] != '$') |
370 | 0 | return 0; |
371 | | |
372 | 0 | *out_len = 2 + escape_len; |
373 | 0 | return c; |
374 | 0 | } |
375 | | |
376 | | static void |
377 | | print_ident (struct rust_demangler *rdm, struct rust_mangled_ident ident) |
378 | 0 | { |
379 | 0 | char unescaped; |
380 | 0 | uint8_t *out, *p, d; |
381 | 0 | size_t len, cap, punycode_pos, j; |
382 | | /* Punycode parameters and state. */ |
383 | 0 | uint32_t c; |
384 | 0 | size_t base, t_min, t_max, skew, damp, bias, i; |
385 | 0 | size_t delta, w, k, t; |
386 | |
|
387 | 0 | if (rdm->errored || rdm->skipping_printing) |
388 | 0 | return; |
389 | | |
390 | 0 | if (rdm->version == -1) |
391 | 0 | { |
392 | | /* Ignore leading underscores preceding escape sequences. |
393 | | The mangler inserts an underscore to make sure the |
394 | | identifier begins with a XID_Start character. */ |
395 | 0 | if (ident.ascii_len >= 2 && ident.ascii[0] == '_' |
396 | 0 | && ident.ascii[1] == '$') |
397 | 0 | { |
398 | 0 | ident.ascii++; |
399 | 0 | ident.ascii_len--; |
400 | 0 | } |
401 | |
|
402 | 0 | while (ident.ascii_len > 0) |
403 | 0 | { |
404 | | /* Handle legacy escape sequences ("$...$", ".." or "."). */ |
405 | 0 | if (ident.ascii[0] == '$') |
406 | 0 | { |
407 | 0 | unescaped |
408 | 0 | = decode_legacy_escape (ident.ascii, ident.ascii_len, &len); |
409 | 0 | if (unescaped) |
410 | 0 | print_str (rdm, &unescaped, 1); |
411 | 0 | else |
412 | 0 | { |
413 | | /* Unexpected escape sequence, print the rest verbatim. */ |
414 | 0 | print_str (rdm, ident.ascii, ident.ascii_len); |
415 | 0 | return; |
416 | 0 | } |
417 | 0 | } |
418 | 0 | else if (ident.ascii[0] == '.') |
419 | 0 | { |
420 | 0 | if (ident.ascii_len >= 2 && ident.ascii[1] == '.') |
421 | 0 | { |
422 | | /* ".." becomes "::" */ |
423 | 0 | PRINT ("::"); |
424 | 0 | len = 2; |
425 | 0 | } |
426 | 0 | else |
427 | 0 | { |
428 | 0 | PRINT ("."); |
429 | 0 | len = 1; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | else |
433 | 0 | { |
434 | | /* Print everything before the next escape sequence, at once. */ |
435 | 0 | for (len = 0; len < ident.ascii_len; len++) |
436 | 0 | if (ident.ascii[len] == '$' || ident.ascii[len] == '.') |
437 | 0 | break; |
438 | |
|
439 | 0 | print_str (rdm, ident.ascii, len); |
440 | 0 | } |
441 | | |
442 | 0 | ident.ascii += len; |
443 | 0 | ident.ascii_len -= len; |
444 | 0 | } |
445 | | |
446 | 0 | return; |
447 | 0 | } |
448 | | |
449 | 0 | if (!ident.punycode) |
450 | 0 | { |
451 | 0 | print_str (rdm, ident.ascii, ident.ascii_len); |
452 | 0 | return; |
453 | 0 | } |
454 | | |
455 | 0 | len = 0; |
456 | 0 | cap = 4; |
457 | 0 | while (cap < ident.ascii_len) |
458 | 0 | { |
459 | 0 | cap *= 2; |
460 | | /* Check for overflows. */ |
461 | 0 | if ((cap * 4) / 4 != cap) |
462 | 0 | { |
463 | 0 | rdm->errored = 1; |
464 | 0 | return; |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | /* Store the output codepoints as groups of 4 UTF-8 bytes. */ |
469 | 0 | out = (uint8_t *)malloc (cap * 4); |
470 | 0 | if (!out) |
471 | 0 | { |
472 | 0 | rdm->errored = 1; |
473 | 0 | return; |
474 | 0 | } |
475 | | |
476 | | /* Populate initial output from ASCII fragment. */ |
477 | 0 | for (len = 0; len < ident.ascii_len; len++) |
478 | 0 | { |
479 | 0 | p = out + 4 * len; |
480 | 0 | p[0] = 0; |
481 | 0 | p[1] = 0; |
482 | 0 | p[2] = 0; |
483 | 0 | p[3] = ident.ascii[len]; |
484 | 0 | } |
485 | | |
486 | | /* Punycode parameters and initial state. */ |
487 | 0 | base = 36; |
488 | 0 | t_min = 1; |
489 | 0 | t_max = 26; |
490 | 0 | skew = 38; |
491 | 0 | damp = 700; |
492 | 0 | bias = 72; |
493 | 0 | i = 0; |
494 | 0 | c = 0x80; |
495 | |
|
496 | 0 | punycode_pos = 0; |
497 | 0 | while (punycode_pos < ident.punycode_len) |
498 | 0 | { |
499 | | /* Read one delta value. */ |
500 | 0 | delta = 0; |
501 | 0 | w = 1; |
502 | 0 | k = 0; |
503 | 0 | do |
504 | 0 | { |
505 | 0 | k += base; |
506 | 0 | t = k < bias ? 0 : (k - bias); |
507 | 0 | if (t < t_min) |
508 | 0 | t = t_min; |
509 | 0 | if (t > t_max) |
510 | 0 | t = t_max; |
511 | |
|
512 | 0 | if (punycode_pos >= ident.punycode_len) |
513 | 0 | goto cleanup; |
514 | 0 | d = ident.punycode[punycode_pos++]; |
515 | |
|
516 | 0 | if (ISLOWER (d)) |
517 | 0 | d = d - 'a'; |
518 | 0 | else if (ISDIGIT (d)) |
519 | 0 | d = 26 + (d - '0'); |
520 | 0 | else |
521 | 0 | { |
522 | 0 | rdm->errored = 1; |
523 | 0 | goto cleanup; |
524 | 0 | } |
525 | | |
526 | 0 | delta += d * w; |
527 | 0 | w *= base - t; |
528 | 0 | } |
529 | 0 | while (d >= t); |
530 | | |
531 | | /* Compute the new insert position and character. */ |
532 | 0 | len++; |
533 | 0 | i += delta; |
534 | 0 | c += i / len; |
535 | 0 | i %= len; |
536 | | |
537 | | /* Ensure enough space is available. */ |
538 | 0 | if (cap < len) |
539 | 0 | { |
540 | 0 | cap *= 2; |
541 | | /* Check for overflows. */ |
542 | 0 | if ((cap * 4) / 4 != cap || cap < len) |
543 | 0 | { |
544 | 0 | rdm->errored = 1; |
545 | 0 | goto cleanup; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | p = (uint8_t *)realloc (out, cap * 4); |
549 | 0 | if (!p) |
550 | 0 | { |
551 | 0 | rdm->errored = 1; |
552 | 0 | goto cleanup; |
553 | 0 | } |
554 | 0 | out = p; |
555 | | |
556 | | /* Move the characters after the insert position. */ |
557 | 0 | p = out + i * 4; |
558 | 0 | memmove (p + 4, p, (len - i - 1) * 4); |
559 | | |
560 | | /* Insert the new character, as UTF-8 bytes. */ |
561 | 0 | p[0] = c >= 0x10000 ? 0xf0 | (c >> 18) : 0; |
562 | 0 | p[1] = c >= 0x800 ? (c < 0x10000 ? 0xe0 : 0x80) | ((c >> 12) & 0x3f) : 0; |
563 | 0 | p[2] = (c < 0x800 ? 0xc0 : 0x80) | ((c >> 6) & 0x3f); |
564 | 0 | p[3] = 0x80 | (c & 0x3f); |
565 | | |
566 | | /* If there are no more deltas, decoding is complete. */ |
567 | 0 | if (punycode_pos == ident.punycode_len) |
568 | 0 | break; |
569 | | |
570 | 0 | i++; |
571 | | |
572 | | /* Perform bias adaptation. */ |
573 | 0 | delta /= damp; |
574 | 0 | damp = 2; |
575 | |
|
576 | 0 | delta += delta / len; |
577 | 0 | k = 0; |
578 | 0 | while (delta > ((base - t_min) * t_max) / 2) |
579 | 0 | { |
580 | 0 | delta /= base - t_min; |
581 | 0 | k += base; |
582 | 0 | } |
583 | 0 | bias = k + ((base - t_min + 1) * delta) / (delta + skew); |
584 | 0 | } |
585 | | |
586 | | /* Remove all the 0 bytes to leave behind an UTF-8 string. */ |
587 | 0 | for (i = 0, j = 0; i < len * 4; i++) |
588 | 0 | if (out[i] != 0) |
589 | 0 | out[j++] = out[i]; |
590 | |
|
591 | 0 | print_str (rdm, (const char *)out, j); |
592 | |
|
593 | 0 | cleanup: |
594 | 0 | free (out); |
595 | 0 | } |
596 | | |
597 | | /* Print the lifetime according to the previously decoded index. |
598 | | An index of `0` always refers to `'_`, but starting with `1`, |
599 | | indices refer to late-bound lifetimes introduced by a binder. */ |
600 | | static void |
601 | | print_lifetime_from_index (struct rust_demangler *rdm, uint64_t lt) |
602 | 0 | { |
603 | 0 | char c; |
604 | 0 | uint64_t depth; |
605 | |
|
606 | 0 | PRINT ("'"); |
607 | 0 | if (lt == 0) |
608 | 0 | { |
609 | 0 | PRINT ("_"); |
610 | 0 | return; |
611 | 0 | } |
612 | | |
613 | 0 | depth = rdm->bound_lifetime_depth - lt; |
614 | | /* Try to print lifetimes alphabetically first. */ |
615 | 0 | if (depth < 26) |
616 | 0 | { |
617 | 0 | c = 'a' + depth; |
618 | 0 | print_str (rdm, &c, 1); |
619 | 0 | } |
620 | 0 | else |
621 | 0 | { |
622 | | /* Use `'_123` after running out of letters. */ |
623 | 0 | PRINT ("_"); |
624 | 0 | print_uint64 (rdm, depth); |
625 | 0 | } |
626 | 0 | } |
627 | | |
628 | | /* Demangling functions. */ |
629 | | |
630 | | static void demangle_binder (struct rust_demangler *rdm); |
631 | | static void demangle_path (struct rust_demangler *rdm, int in_value); |
632 | | static void demangle_generic_arg (struct rust_demangler *rdm); |
633 | | static void demangle_type (struct rust_demangler *rdm); |
634 | | static int demangle_path_maybe_open_generics (struct rust_demangler *rdm); |
635 | | static void demangle_dyn_trait (struct rust_demangler *rdm); |
636 | | static void demangle_const (struct rust_demangler *rdm); |
637 | | static void demangle_const_uint (struct rust_demangler *rdm); |
638 | | static void demangle_const_int (struct rust_demangler *rdm); |
639 | | static void demangle_const_bool (struct rust_demangler *rdm); |
640 | | static void demangle_const_char (struct rust_demangler *rdm); |
641 | | |
642 | | /* Optionally enter a binder ('G') for late-bound lifetimes, |
643 | | printing e.g. `for<'a, 'b> `, and make those lifetimes visible |
644 | | to the caller (via depth level, which the caller should reset). */ |
645 | | static void |
646 | | demangle_binder (struct rust_demangler *rdm) |
647 | 0 | { |
648 | 0 | uint64_t i, bound_lifetimes; |
649 | |
|
650 | 0 | if (rdm->errored) |
651 | 0 | return; |
652 | | |
653 | 0 | bound_lifetimes = parse_opt_integer_62 (rdm, 'G'); |
654 | 0 | if (bound_lifetimes > 0) |
655 | 0 | { |
656 | 0 | PRINT ("for<"); |
657 | 0 | for (i = 0; i < bound_lifetimes; i++) |
658 | 0 | { |
659 | 0 | if (i > 0) |
660 | 0 | PRINT (", "); |
661 | 0 | rdm->bound_lifetime_depth++; |
662 | 0 | print_lifetime_from_index (rdm, 1); |
663 | 0 | } |
664 | 0 | PRINT ("> "); |
665 | 0 | } |
666 | 0 | } |
667 | | |
668 | | static void |
669 | | demangle_path (struct rust_demangler *rdm, int in_value) |
670 | 0 | { |
671 | 0 | char tag, ns; |
672 | 0 | int was_skipping_printing; |
673 | 0 | size_t i, backref, old_next; |
674 | 0 | uint64_t dis; |
675 | 0 | struct rust_mangled_ident name; |
676 | |
|
677 | 0 | if (rdm->errored) |
678 | 0 | return; |
679 | | |
680 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
681 | 0 | { |
682 | 0 | ++ rdm->recursion; |
683 | 0 | if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
684 | | /* FIXME: There ought to be a way to report |
685 | | that the recursion limit has been reached. */ |
686 | 0 | goto fail_return; |
687 | 0 | } |
688 | | |
689 | 0 | switch (tag = next (rdm)) |
690 | 0 | { |
691 | 0 | case 'C': |
692 | 0 | dis = parse_disambiguator (rdm); |
693 | 0 | name = parse_ident (rdm); |
694 | |
|
695 | 0 | print_ident (rdm, name); |
696 | 0 | if (rdm->verbose) |
697 | 0 | { |
698 | 0 | PRINT ("["); |
699 | 0 | print_uint64_hex (rdm, dis); |
700 | 0 | PRINT ("]"); |
701 | 0 | } |
702 | 0 | break; |
703 | 0 | case 'N': |
704 | 0 | ns = next (rdm); |
705 | 0 | if (!ISLOWER (ns) && !ISUPPER (ns)) |
706 | 0 | goto fail_return; |
707 | | |
708 | 0 | demangle_path (rdm, in_value); |
709 | |
|
710 | 0 | dis = parse_disambiguator (rdm); |
711 | 0 | name = parse_ident (rdm); |
712 | |
|
713 | 0 | if (ISUPPER (ns)) |
714 | 0 | { |
715 | | /* Special namespaces, like closures and shims. */ |
716 | 0 | PRINT ("::{"); |
717 | 0 | switch (ns) |
718 | 0 | { |
719 | 0 | case 'C': |
720 | 0 | PRINT ("closure"); |
721 | 0 | break; |
722 | 0 | case 'S': |
723 | 0 | PRINT ("shim"); |
724 | 0 | break; |
725 | 0 | default: |
726 | 0 | print_str (rdm, &ns, 1); |
727 | 0 | } |
728 | 0 | if (name.ascii || name.punycode) |
729 | 0 | { |
730 | 0 | PRINT (":"); |
731 | 0 | print_ident (rdm, name); |
732 | 0 | } |
733 | 0 | PRINT ("#"); |
734 | 0 | print_uint64 (rdm, dis); |
735 | 0 | PRINT ("}"); |
736 | 0 | } |
737 | 0 | else |
738 | 0 | { |
739 | | /* Implementation-specific/unspecified namespaces. */ |
740 | |
|
741 | 0 | if (name.ascii || name.punycode) |
742 | 0 | { |
743 | 0 | PRINT ("::"); |
744 | 0 | print_ident (rdm, name); |
745 | 0 | } |
746 | 0 | } |
747 | 0 | break; |
748 | 0 | case 'M': |
749 | 0 | case 'X': |
750 | | /* Ignore the `impl`'s own path.*/ |
751 | 0 | parse_disambiguator (rdm); |
752 | 0 | was_skipping_printing = rdm->skipping_printing; |
753 | 0 | rdm->skipping_printing = 1; |
754 | 0 | demangle_path (rdm, in_value); |
755 | 0 | rdm->skipping_printing = was_skipping_printing; |
756 | | /* fallthrough */ |
757 | 0 | case 'Y': |
758 | 0 | PRINT ("<"); |
759 | 0 | demangle_type (rdm); |
760 | 0 | if (tag != 'M') |
761 | 0 | { |
762 | 0 | PRINT (" as "); |
763 | 0 | demangle_path (rdm, 0); |
764 | 0 | } |
765 | 0 | PRINT (">"); |
766 | 0 | break; |
767 | 0 | case 'I': |
768 | 0 | demangle_path (rdm, in_value); |
769 | 0 | if (in_value) |
770 | 0 | PRINT ("::"); |
771 | 0 | PRINT ("<"); |
772 | 0 | for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++) |
773 | 0 | { |
774 | 0 | if (i > 0) |
775 | 0 | PRINT (", "); |
776 | 0 | demangle_generic_arg (rdm); |
777 | 0 | } |
778 | 0 | PRINT (">"); |
779 | 0 | break; |
780 | 0 | case 'B': |
781 | 0 | backref = parse_integer_62 (rdm); |
782 | 0 | if (!rdm->skipping_printing) |
783 | 0 | { |
784 | 0 | old_next = rdm->next; |
785 | 0 | rdm->next = backref; |
786 | 0 | demangle_path (rdm, in_value); |
787 | 0 | rdm->next = old_next; |
788 | 0 | } |
789 | 0 | break; |
790 | 0 | default: |
791 | 0 | goto fail_return; |
792 | 0 | } |
793 | 0 | goto pass_return; |
794 | | |
795 | 0 | fail_return: |
796 | 0 | rdm->errored = 1; |
797 | 0 | pass_return: |
798 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
799 | 0 | -- rdm->recursion; |
800 | 0 | } |
801 | | |
802 | | static void |
803 | | demangle_generic_arg (struct rust_demangler *rdm) |
804 | 0 | { |
805 | 0 | uint64_t lt; |
806 | 0 | if (eat (rdm, 'L')) |
807 | 0 | { |
808 | 0 | lt = parse_integer_62 (rdm); |
809 | 0 | print_lifetime_from_index (rdm, lt); |
810 | 0 | } |
811 | 0 | else if (eat (rdm, 'K')) |
812 | 0 | demangle_const (rdm); |
813 | 0 | else |
814 | 0 | demangle_type (rdm); |
815 | 0 | } |
816 | | |
817 | | static const char * |
818 | | basic_type (char tag) |
819 | 0 | { |
820 | 0 | switch (tag) |
821 | 0 | { |
822 | 0 | case 'b': |
823 | 0 | return "bool"; |
824 | 0 | case 'c': |
825 | 0 | return "char"; |
826 | 0 | case 'e': |
827 | 0 | return "str"; |
828 | 0 | case 'u': |
829 | 0 | return "()"; |
830 | 0 | case 'a': |
831 | 0 | return "i8"; |
832 | 0 | case 's': |
833 | 0 | return "i16"; |
834 | 0 | case 'l': |
835 | 0 | return "i32"; |
836 | 0 | case 'x': |
837 | 0 | return "i64"; |
838 | 0 | case 'n': |
839 | 0 | return "i128"; |
840 | 0 | case 'i': |
841 | 0 | return "isize"; |
842 | 0 | case 'h': |
843 | 0 | return "u8"; |
844 | 0 | case 't': |
845 | 0 | return "u16"; |
846 | 0 | case 'm': |
847 | 0 | return "u32"; |
848 | 0 | case 'y': |
849 | 0 | return "u64"; |
850 | 0 | case 'o': |
851 | 0 | return "u128"; |
852 | 0 | case 'j': |
853 | 0 | return "usize"; |
854 | 0 | case 'f': |
855 | 0 | return "f32"; |
856 | 0 | case 'd': |
857 | 0 | return "f64"; |
858 | 0 | case 'z': |
859 | 0 | return "!"; |
860 | 0 | case 'p': |
861 | 0 | return "_"; |
862 | 0 | case 'v': |
863 | 0 | return "..."; |
864 | | |
865 | 0 | default: |
866 | 0 | return NULL; |
867 | 0 | } |
868 | 0 | } |
869 | | |
870 | | static void |
871 | | demangle_type (struct rust_demangler *rdm) |
872 | 0 | { |
873 | 0 | char tag; |
874 | 0 | size_t i, old_next, backref; |
875 | 0 | uint64_t lt, old_bound_lifetime_depth; |
876 | 0 | const char *basic; |
877 | 0 | struct rust_mangled_ident abi; |
878 | |
|
879 | 0 | if (rdm->errored) |
880 | 0 | return; |
881 | | |
882 | 0 | tag = next (rdm); |
883 | |
|
884 | 0 | basic = basic_type (tag); |
885 | 0 | if (basic) |
886 | 0 | { |
887 | 0 | PRINT (basic); |
888 | 0 | return; |
889 | 0 | } |
890 | | |
891 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
892 | 0 | { |
893 | 0 | ++ rdm->recursion; |
894 | 0 | if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
895 | | /* FIXME: There ought to be a way to report |
896 | | that the recursion limit has been reached. */ |
897 | 0 | { |
898 | 0 | rdm->errored = 1; |
899 | 0 | -- rdm->recursion; |
900 | 0 | return; |
901 | 0 | } |
902 | 0 | } |
903 | | |
904 | 0 | switch (tag) |
905 | 0 | { |
906 | 0 | case 'R': |
907 | 0 | case 'Q': |
908 | 0 | PRINT ("&"); |
909 | 0 | if (eat (rdm, 'L')) |
910 | 0 | { |
911 | 0 | lt = parse_integer_62 (rdm); |
912 | 0 | if (lt) |
913 | 0 | { |
914 | 0 | print_lifetime_from_index (rdm, lt); |
915 | 0 | PRINT (" "); |
916 | 0 | } |
917 | 0 | } |
918 | 0 | if (tag != 'R') |
919 | 0 | PRINT ("mut "); |
920 | 0 | demangle_type (rdm); |
921 | 0 | break; |
922 | 0 | case 'P': |
923 | 0 | case 'O': |
924 | 0 | PRINT ("*"); |
925 | 0 | if (tag != 'P') |
926 | 0 | PRINT ("mut "); |
927 | 0 | else |
928 | 0 | PRINT ("const "); |
929 | 0 | demangle_type (rdm); |
930 | 0 | break; |
931 | 0 | case 'A': |
932 | 0 | case 'S': |
933 | 0 | PRINT ("["); |
934 | 0 | demangle_type (rdm); |
935 | 0 | if (tag == 'A') |
936 | 0 | { |
937 | 0 | PRINT ("; "); |
938 | 0 | demangle_const (rdm); |
939 | 0 | } |
940 | 0 | PRINT ("]"); |
941 | 0 | break; |
942 | 0 | case 'T': |
943 | 0 | PRINT ("("); |
944 | 0 | for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++) |
945 | 0 | { |
946 | 0 | if (i > 0) |
947 | 0 | PRINT (", "); |
948 | 0 | demangle_type (rdm); |
949 | 0 | } |
950 | 0 | if (i == 1) |
951 | 0 | PRINT (","); |
952 | 0 | PRINT (")"); |
953 | 0 | break; |
954 | 0 | case 'F': |
955 | 0 | old_bound_lifetime_depth = rdm->bound_lifetime_depth; |
956 | 0 | demangle_binder (rdm); |
957 | |
|
958 | 0 | if (eat (rdm, 'U')) |
959 | 0 | PRINT ("unsafe "); |
960 | |
|
961 | 0 | if (eat (rdm, 'K')) |
962 | 0 | { |
963 | 0 | if (eat (rdm, 'C')) |
964 | 0 | { |
965 | 0 | abi.ascii = "C"; |
966 | 0 | abi.ascii_len = 1; |
967 | 0 | } |
968 | 0 | else |
969 | 0 | { |
970 | 0 | abi = parse_ident (rdm); |
971 | 0 | if (!abi.ascii || abi.punycode) |
972 | 0 | { |
973 | 0 | rdm->errored = 1; |
974 | 0 | goto restore; |
975 | 0 | } |
976 | 0 | } |
977 | | |
978 | 0 | PRINT ("extern \""); |
979 | | |
980 | | /* If the ABI had any `-`, they were replaced with `_`, |
981 | | so the parts between `_` have to be re-joined with `-`. */ |
982 | 0 | for (i = 0; i < abi.ascii_len; i++) |
983 | 0 | { |
984 | 0 | if (abi.ascii[i] == '_') |
985 | 0 | { |
986 | 0 | print_str (rdm, abi.ascii, i); |
987 | 0 | PRINT ("-"); |
988 | 0 | abi.ascii += i + 1; |
989 | 0 | abi.ascii_len -= i + 1; |
990 | 0 | i = 0; |
991 | 0 | } |
992 | 0 | } |
993 | 0 | print_str (rdm, abi.ascii, abi.ascii_len); |
994 | |
|
995 | 0 | PRINT ("\" "); |
996 | 0 | } |
997 | | |
998 | 0 | PRINT ("fn("); |
999 | 0 | for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++) |
1000 | 0 | { |
1001 | 0 | if (i > 0) |
1002 | 0 | PRINT (", "); |
1003 | 0 | demangle_type (rdm); |
1004 | 0 | } |
1005 | 0 | PRINT (")"); |
1006 | |
|
1007 | 0 | if (eat (rdm, 'u')) |
1008 | 0 | { |
1009 | | /* Skip printing the return type if it's 'u', i.e. `()`. */ |
1010 | 0 | } |
1011 | 0 | else |
1012 | 0 | { |
1013 | 0 | PRINT (" -> "); |
1014 | 0 | demangle_type (rdm); |
1015 | 0 | } |
1016 | | |
1017 | | /* Restore `bound_lifetime_depth` to outside the binder. */ |
1018 | 0 | restore: |
1019 | 0 | rdm->bound_lifetime_depth = old_bound_lifetime_depth; |
1020 | 0 | break; |
1021 | 0 | case 'D': |
1022 | 0 | PRINT ("dyn "); |
1023 | |
|
1024 | 0 | old_bound_lifetime_depth = rdm->bound_lifetime_depth; |
1025 | 0 | demangle_binder (rdm); |
1026 | |
|
1027 | 0 | for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++) |
1028 | 0 | { |
1029 | 0 | if (i > 0) |
1030 | 0 | PRINT (" + "); |
1031 | 0 | demangle_dyn_trait (rdm); |
1032 | 0 | } |
1033 | | |
1034 | | /* Restore `bound_lifetime_depth` to outside the binder. */ |
1035 | 0 | rdm->bound_lifetime_depth = old_bound_lifetime_depth; |
1036 | |
|
1037 | 0 | if (!eat (rdm, 'L')) |
1038 | 0 | { |
1039 | 0 | rdm->errored = 1; |
1040 | 0 | return; |
1041 | 0 | } |
1042 | 0 | lt = parse_integer_62 (rdm); |
1043 | 0 | if (lt) |
1044 | 0 | { |
1045 | 0 | PRINT (" + "); |
1046 | 0 | print_lifetime_from_index (rdm, lt); |
1047 | 0 | } |
1048 | 0 | break; |
1049 | 0 | case 'B': |
1050 | 0 | backref = parse_integer_62 (rdm); |
1051 | 0 | if (!rdm->skipping_printing) |
1052 | 0 | { |
1053 | 0 | old_next = rdm->next; |
1054 | 0 | rdm->next = backref; |
1055 | 0 | demangle_type (rdm); |
1056 | 0 | rdm->next = old_next; |
1057 | 0 | } |
1058 | 0 | break; |
1059 | 0 | default: |
1060 | | /* Go back to the tag, so `demangle_path` also sees it. */ |
1061 | 0 | rdm->next--; |
1062 | 0 | demangle_path (rdm, 0); |
1063 | 0 | } |
1064 | | |
1065 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
1066 | 0 | -- rdm->recursion; |
1067 | 0 | } |
1068 | | |
1069 | | /* A trait in a trait object may have some "existential projections" |
1070 | | (i.e. associated type bindings) after it, which should be printed |
1071 | | in the `<...>` of the trait, e.g. `dyn Trait<T, U, Assoc=X>`. |
1072 | | To this end, this method will keep the `<...>` of an 'I' path |
1073 | | open, by omitting the `>`, and return `Ok(true)` in that case. */ |
1074 | | static int |
1075 | | demangle_path_maybe_open_generics (struct rust_demangler *rdm) |
1076 | 0 | { |
1077 | 0 | int open; |
1078 | 0 | size_t i, old_next, backref; |
1079 | |
|
1080 | 0 | open = 0; |
1081 | |
|
1082 | 0 | if (rdm->errored) |
1083 | 0 | return open; |
1084 | | |
1085 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
1086 | 0 | { |
1087 | 0 | ++ rdm->recursion; |
1088 | 0 | if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
1089 | 0 | { |
1090 | | /* FIXME: There ought to be a way to report |
1091 | | that the recursion limit has been reached. */ |
1092 | 0 | rdm->errored = 1; |
1093 | 0 | goto end_of_func; |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | 0 | if (eat (rdm, 'B')) |
1098 | 0 | { |
1099 | 0 | backref = parse_integer_62 (rdm); |
1100 | 0 | if (!rdm->skipping_printing) |
1101 | 0 | { |
1102 | 0 | old_next = rdm->next; |
1103 | 0 | rdm->next = backref; |
1104 | 0 | open = demangle_path_maybe_open_generics (rdm); |
1105 | 0 | rdm->next = old_next; |
1106 | 0 | } |
1107 | 0 | } |
1108 | 0 | else if (eat (rdm, 'I')) |
1109 | 0 | { |
1110 | 0 | demangle_path (rdm, 0); |
1111 | 0 | PRINT ("<"); |
1112 | 0 | open = 1; |
1113 | 0 | for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++) |
1114 | 0 | { |
1115 | 0 | if (i > 0) |
1116 | 0 | PRINT (", "); |
1117 | 0 | demangle_generic_arg (rdm); |
1118 | 0 | } |
1119 | 0 | } |
1120 | 0 | else |
1121 | 0 | demangle_path (rdm, 0); |
1122 | |
|
1123 | 0 | end_of_func: |
1124 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
1125 | 0 | -- rdm->recursion; |
1126 | |
|
1127 | 0 | return open; |
1128 | 0 | } |
1129 | | |
1130 | | static void |
1131 | | demangle_dyn_trait (struct rust_demangler *rdm) |
1132 | 0 | { |
1133 | 0 | int open; |
1134 | 0 | struct rust_mangled_ident name; |
1135 | |
|
1136 | 0 | if (rdm->errored) |
1137 | 0 | return; |
1138 | | |
1139 | 0 | open = demangle_path_maybe_open_generics (rdm); |
1140 | |
|
1141 | 0 | while (eat (rdm, 'p')) |
1142 | 0 | { |
1143 | 0 | if (!open) |
1144 | 0 | PRINT ("<"); |
1145 | 0 | else |
1146 | 0 | PRINT (", "); |
1147 | 0 | open = 1; |
1148 | |
|
1149 | 0 | name = parse_ident (rdm); |
1150 | 0 | print_ident (rdm, name); |
1151 | 0 | PRINT (" = "); |
1152 | 0 | demangle_type (rdm); |
1153 | 0 | } |
1154 | |
|
1155 | 0 | if (open) |
1156 | 0 | PRINT (">"); |
1157 | 0 | } |
1158 | | |
1159 | | static void |
1160 | | demangle_const (struct rust_demangler *rdm) |
1161 | 0 | { |
1162 | 0 | char ty_tag; |
1163 | 0 | size_t old_next, backref; |
1164 | |
|
1165 | 0 | if (rdm->errored) |
1166 | 0 | return; |
1167 | | |
1168 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
1169 | 0 | { |
1170 | 0 | ++ rdm->recursion; |
1171 | 0 | if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
1172 | | /* FIXME: There ought to be a way to report |
1173 | | that the recursion limit has been reached. */ |
1174 | 0 | goto fail_return; |
1175 | 0 | } |
1176 | | |
1177 | 0 | if (eat (rdm, 'B')) |
1178 | 0 | { |
1179 | 0 | backref = parse_integer_62 (rdm); |
1180 | 0 | if (!rdm->skipping_printing) |
1181 | 0 | { |
1182 | 0 | old_next = rdm->next; |
1183 | 0 | rdm->next = backref; |
1184 | 0 | demangle_const (rdm); |
1185 | 0 | rdm->next = old_next; |
1186 | 0 | } |
1187 | 0 | goto pass_return; |
1188 | 0 | } |
1189 | | |
1190 | 0 | ty_tag = next (rdm); |
1191 | 0 | switch (ty_tag) |
1192 | 0 | { |
1193 | | /* Placeholder. */ |
1194 | 0 | case 'p': |
1195 | 0 | PRINT ("_"); |
1196 | 0 | goto pass_return; |
1197 | | |
1198 | | /* Unsigned integer types. */ |
1199 | 0 | case 'h': |
1200 | 0 | case 't': |
1201 | 0 | case 'm': |
1202 | 0 | case 'y': |
1203 | 0 | case 'o': |
1204 | 0 | case 'j': |
1205 | 0 | demangle_const_uint (rdm); |
1206 | 0 | break; |
1207 | | |
1208 | | /* Signed integer types. */ |
1209 | 0 | case 'a': |
1210 | 0 | case 's': |
1211 | 0 | case 'l': |
1212 | 0 | case 'x': |
1213 | 0 | case 'n': |
1214 | 0 | case 'i': |
1215 | 0 | demangle_const_int (rdm); |
1216 | 0 | break; |
1217 | | |
1218 | | /* Boolean. */ |
1219 | 0 | case 'b': |
1220 | 0 | demangle_const_bool (rdm); |
1221 | 0 | break; |
1222 | | |
1223 | | /* Character. */ |
1224 | 0 | case 'c': |
1225 | 0 | demangle_const_char (rdm); |
1226 | 0 | break; |
1227 | | |
1228 | 0 | default: |
1229 | 0 | goto fail_return; |
1230 | 0 | } |
1231 | | |
1232 | 0 | if (!rdm->errored && rdm->verbose) |
1233 | 0 | { |
1234 | 0 | PRINT (": "); |
1235 | 0 | PRINT (basic_type (ty_tag)); |
1236 | 0 | } |
1237 | 0 | goto pass_return; |
1238 | | |
1239 | 0 | fail_return: |
1240 | 0 | rdm->errored = 1; |
1241 | 0 | pass_return: |
1242 | 0 | if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
1243 | 0 | -- rdm->recursion; |
1244 | 0 | } |
1245 | | |
1246 | | static void |
1247 | | demangle_const_uint (struct rust_demangler *rdm) |
1248 | 0 | { |
1249 | 0 | size_t hex_len; |
1250 | 0 | uint64_t value; |
1251 | |
|
1252 | 0 | if (rdm->errored) |
1253 | 0 | return; |
1254 | | |
1255 | 0 | hex_len = parse_hex_nibbles (rdm, &value); |
1256 | |
|
1257 | 0 | if (hex_len > 16) |
1258 | 0 | { |
1259 | | /* Print anything that doesn't fit in `uint64_t` verbatim. */ |
1260 | 0 | PRINT ("0x"); |
1261 | 0 | print_str (rdm, rdm->sym + (rdm->next - hex_len), hex_len); |
1262 | 0 | } |
1263 | 0 | else if (hex_len > 0) |
1264 | 0 | print_uint64 (rdm, value); |
1265 | 0 | else |
1266 | 0 | rdm->errored = 1; |
1267 | 0 | } |
1268 | | |
1269 | | static void |
1270 | | demangle_const_int (struct rust_demangler *rdm) |
1271 | 0 | { |
1272 | 0 | if (eat (rdm, 'n')) |
1273 | 0 | PRINT ("-"); |
1274 | 0 | demangle_const_uint (rdm); |
1275 | 0 | } |
1276 | | |
1277 | | static void |
1278 | | demangle_const_bool (struct rust_demangler *rdm) |
1279 | 0 | { |
1280 | 0 | uint64_t value; |
1281 | |
|
1282 | 0 | if (parse_hex_nibbles (rdm, &value) != 1) |
1283 | 0 | { |
1284 | 0 | rdm->errored = 1; |
1285 | 0 | return; |
1286 | 0 | } |
1287 | | |
1288 | 0 | if (value == 0) |
1289 | 0 | PRINT ("false"); |
1290 | 0 | else if (value == 1) |
1291 | 0 | PRINT ("true"); |
1292 | 0 | else |
1293 | 0 | rdm->errored = 1; |
1294 | 0 | } |
1295 | | |
1296 | | static void |
1297 | | demangle_const_char (struct rust_demangler *rdm) |
1298 | 0 | { |
1299 | 0 | size_t hex_len; |
1300 | 0 | uint64_t value; |
1301 | |
|
1302 | 0 | hex_len = parse_hex_nibbles (rdm, &value); |
1303 | |
|
1304 | 0 | if (hex_len == 0 || hex_len > 8) |
1305 | 0 | { |
1306 | 0 | rdm->errored = 1; |
1307 | 0 | return; |
1308 | 0 | } |
1309 | | |
1310 | | /* Match Rust's character "debug" output as best as we can. */ |
1311 | 0 | PRINT ("'"); |
1312 | 0 | if (value == '\t') |
1313 | 0 | PRINT ("\\t"); |
1314 | 0 | else if (value == '\r') |
1315 | 0 | PRINT ("\\r"); |
1316 | 0 | else if (value == '\n') |
1317 | 0 | PRINT ("\\n"); |
1318 | 0 | else if (value > ' ' && value < '~') |
1319 | 0 | { |
1320 | | /* Rust also considers many non-ASCII codepoints to be printable, but |
1321 | | that logic is not easily ported to C. */ |
1322 | 0 | char c = value; |
1323 | 0 | print_str (rdm, &c, 1); |
1324 | 0 | } |
1325 | 0 | else |
1326 | 0 | { |
1327 | 0 | PRINT ("\\u{"); |
1328 | 0 | print_uint64_hex (rdm, value); |
1329 | 0 | PRINT ("}"); |
1330 | 0 | } |
1331 | 0 | PRINT ("'"); |
1332 | 0 | } |
1333 | | |
1334 | | /* A legacy hash is the prefix "h" followed by 16 lowercase hex digits. |
1335 | | The hex digits must contain at least 5 distinct digits. */ |
1336 | | static int |
1337 | | is_legacy_prefixed_hash (struct rust_mangled_ident ident) |
1338 | 0 | { |
1339 | 0 | uint16_t seen; |
1340 | 0 | int nibble; |
1341 | 0 | size_t i, count; |
1342 | |
|
1343 | 0 | if (ident.ascii_len != 17 || ident.ascii[0] != 'h') |
1344 | 0 | return 0; |
1345 | | |
1346 | 0 | seen = 0; |
1347 | 0 | for (i = 0; i < 16; i++) |
1348 | 0 | { |
1349 | 0 | nibble = decode_lower_hex_nibble (ident.ascii[1 + i]); |
1350 | 0 | if (nibble < 0) |
1351 | 0 | return 0; |
1352 | 0 | seen |= (uint16_t)1 << nibble; |
1353 | 0 | } |
1354 | | |
1355 | | /* Count how many distinct digits were seen. */ |
1356 | 0 | count = 0; |
1357 | 0 | while (seen) |
1358 | 0 | { |
1359 | 0 | if (seen & 1) |
1360 | 0 | count++; |
1361 | 0 | seen >>= 1; |
1362 | 0 | } |
1363 | |
|
1364 | 0 | return count >= 5; |
1365 | 0 | } |
1366 | | |
1367 | | int |
1368 | | rust_demangle_callback (const char *mangled, int options, |
1369 | | demangle_callbackref callback, void *opaque) |
1370 | 0 | { |
1371 | 0 | const char *p; |
1372 | 0 | struct rust_demangler rdm; |
1373 | 0 | struct rust_mangled_ident ident; |
1374 | |
|
1375 | 0 | rdm.sym = mangled; |
1376 | 0 | rdm.sym_len = 0; |
1377 | |
|
1378 | 0 | rdm.callback_opaque = opaque; |
1379 | 0 | rdm.callback = callback; |
1380 | |
|
1381 | 0 | rdm.next = 0; |
1382 | 0 | rdm.errored = 0; |
1383 | 0 | rdm.skipping_printing = 0; |
1384 | 0 | rdm.verbose = (options & DMGL_VERBOSE) != 0; |
1385 | 0 | rdm.version = 0; |
1386 | 0 | rdm.recursion = (options & DMGL_NO_RECURSE_LIMIT) ? RUST_NO_RECURSION_LIMIT : 0; |
1387 | 0 | rdm.bound_lifetime_depth = 0; |
1388 | | |
1389 | | /* Rust symbols always start with _R (v0) or _ZN (legacy). */ |
1390 | 0 | if (rdm.sym[0] == '_' && rdm.sym[1] == 'R') |
1391 | 0 | rdm.sym += 2; |
1392 | 0 | else if (rdm.sym[0] == '_' && rdm.sym[1] == 'Z' && rdm.sym[2] == 'N') |
1393 | 0 | { |
1394 | 0 | rdm.sym += 3; |
1395 | 0 | rdm.version = -1; |
1396 | 0 | } |
1397 | 0 | else |
1398 | 0 | return 0; |
1399 | | |
1400 | | /* Paths (v0) always start with uppercase characters. */ |
1401 | 0 | if (rdm.version != -1 && !ISUPPER (rdm.sym[0])) |
1402 | 0 | return 0; |
1403 | | |
1404 | | /* Rust symbols (v0) use only [_0-9a-zA-Z] characters. */ |
1405 | 0 | for (p = rdm.sym; *p; p++) |
1406 | 0 | { |
1407 | | /* Rust v0 symbols can have '.' suffixes, ignore those. */ |
1408 | 0 | if (rdm.version == 0 && *p == '.') |
1409 | 0 | break; |
1410 | | |
1411 | 0 | rdm.sym_len++; |
1412 | |
|
1413 | 0 | if (*p == '_' || ISALNUM (*p)) |
1414 | 0 | continue; |
1415 | | |
1416 | | /* Legacy Rust symbols can also contain [.:$] characters. |
1417 | | Or @ in the .suffix (which will be skipped, see below). */ |
1418 | 0 | if (rdm.version == -1 && (*p == '$' || *p == '.' || *p == ':' |
1419 | 0 | || *p == '@')) |
1420 | 0 | continue; |
1421 | | |
1422 | 0 | return 0; |
1423 | 0 | } |
1424 | | |
1425 | | /* Legacy Rust symbols need to be handled separately. */ |
1426 | 0 | if (rdm.version == -1) |
1427 | 0 | { |
1428 | | /* Legacy Rust symbols always end with E. But can be followed by a |
1429 | | .suffix (which we want to ignore). */ |
1430 | 0 | int dot_suffix = 1; |
1431 | 0 | while (rdm.sym_len > 0 && |
1432 | 0 | !(dot_suffix && rdm.sym[rdm.sym_len - 1] == 'E')) |
1433 | 0 | { |
1434 | 0 | dot_suffix = rdm.sym[rdm.sym_len - 1] == '.'; |
1435 | 0 | rdm.sym_len--; |
1436 | 0 | } |
1437 | |
|
1438 | 0 | if (!(rdm.sym_len > 0 && rdm.sym[rdm.sym_len - 1] == 'E')) |
1439 | 0 | return 0; |
1440 | 0 | rdm.sym_len--; |
1441 | | |
1442 | | /* Legacy Rust symbols also always end with a path segment |
1443 | | that encodes a 16 hex digit hash, i.e. '17h[a-f0-9]{16}'. |
1444 | | This early check, before any parse_ident calls, should |
1445 | | quickly filter out most C++ symbols unrelated to Rust. */ |
1446 | 0 | if (!(rdm.sym_len > 19 |
1447 | 0 | && !memcmp (&rdm.sym[rdm.sym_len - 19], "17h", 3))) |
1448 | 0 | return 0; |
1449 | | |
1450 | 0 | do |
1451 | 0 | { |
1452 | 0 | ident = parse_ident (&rdm); |
1453 | 0 | if (rdm.errored || !ident.ascii) |
1454 | 0 | return 0; |
1455 | 0 | } |
1456 | 0 | while (rdm.next < rdm.sym_len); |
1457 | | |
1458 | | /* The last path segment should be the hash. */ |
1459 | 0 | if (!is_legacy_prefixed_hash (ident)) |
1460 | 0 | return 0; |
1461 | | |
1462 | | /* Reset the state for a second pass, to print the symbol. */ |
1463 | 0 | rdm.next = 0; |
1464 | 0 | if (!rdm.verbose && rdm.sym_len > 19) |
1465 | 0 | { |
1466 | | /* Hide the last segment, containing the hash, if not verbose. */ |
1467 | 0 | rdm.sym_len -= 19; |
1468 | 0 | } |
1469 | |
|
1470 | 0 | do |
1471 | 0 | { |
1472 | 0 | if (rdm.next > 0) |
1473 | 0 | print_str (&rdm, "::", 2); |
1474 | |
|
1475 | 0 | ident = parse_ident (&rdm); |
1476 | 0 | print_ident (&rdm, ident); |
1477 | 0 | } |
1478 | 0 | while (rdm.next < rdm.sym_len); |
1479 | 0 | } |
1480 | 0 | else |
1481 | 0 | { |
1482 | 0 | demangle_path (&rdm, 1); |
1483 | | |
1484 | | /* Skip instantiating crate. */ |
1485 | 0 | if (!rdm.errored && rdm.next < rdm.sym_len) |
1486 | 0 | { |
1487 | 0 | rdm.skipping_printing = 1; |
1488 | 0 | demangle_path (&rdm, 0); |
1489 | 0 | } |
1490 | | |
1491 | | /* It's an error to not reach the end. */ |
1492 | 0 | rdm.errored |= rdm.next != rdm.sym_len; |
1493 | 0 | } |
1494 | | |
1495 | 0 | return !rdm.errored; |
1496 | 0 | } |
1497 | | |
1498 | | /* Growable string buffers. */ |
1499 | | struct str_buf |
1500 | | { |
1501 | | char *ptr; |
1502 | | size_t len; |
1503 | | size_t cap; |
1504 | | int errored; |
1505 | | }; |
1506 | | |
1507 | | static void |
1508 | | str_buf_reserve (struct str_buf *buf, size_t extra) |
1509 | 0 | { |
1510 | 0 | size_t available, min_new_cap, new_cap; |
1511 | 0 | char *new_ptr; |
1512 | | |
1513 | | /* Allocation failed before. */ |
1514 | 0 | if (buf->errored) |
1515 | 0 | return; |
1516 | | |
1517 | 0 | available = buf->cap - buf->len; |
1518 | |
|
1519 | 0 | if (extra <= available) |
1520 | 0 | return; |
1521 | | |
1522 | 0 | min_new_cap = buf->cap + (extra - available); |
1523 | | |
1524 | | /* Check for overflows. */ |
1525 | 0 | if (min_new_cap < buf->cap) |
1526 | 0 | { |
1527 | 0 | buf->errored = 1; |
1528 | 0 | return; |
1529 | 0 | } |
1530 | | |
1531 | 0 | new_cap = buf->cap; |
1532 | |
|
1533 | 0 | if (new_cap == 0) |
1534 | 0 | new_cap = 4; |
1535 | | |
1536 | | /* Double capacity until sufficiently large. */ |
1537 | 0 | while (new_cap < min_new_cap) |
1538 | 0 | { |
1539 | 0 | new_cap *= 2; |
1540 | | |
1541 | | /* Check for overflows. */ |
1542 | 0 | if (new_cap < buf->cap) |
1543 | 0 | { |
1544 | 0 | buf->errored = 1; |
1545 | 0 | return; |
1546 | 0 | } |
1547 | 0 | } |
1548 | | |
1549 | 0 | new_ptr = (char *)realloc (buf->ptr, new_cap); |
1550 | 0 | if (new_ptr == NULL) |
1551 | 0 | { |
1552 | 0 | free (buf->ptr); |
1553 | 0 | buf->ptr = NULL; |
1554 | 0 | buf->len = 0; |
1555 | 0 | buf->cap = 0; |
1556 | 0 | buf->errored = 1; |
1557 | 0 | } |
1558 | 0 | else |
1559 | 0 | { |
1560 | 0 | buf->ptr = new_ptr; |
1561 | 0 | buf->cap = new_cap; |
1562 | 0 | } |
1563 | 0 | } |
1564 | | |
1565 | | static void |
1566 | | str_buf_append (struct str_buf *buf, const char *data, size_t len) |
1567 | 0 | { |
1568 | 0 | str_buf_reserve (buf, len); |
1569 | 0 | if (buf->errored) |
1570 | 0 | return; |
1571 | | |
1572 | 0 | memcpy (buf->ptr + buf->len, data, len); |
1573 | 0 | buf->len += len; |
1574 | 0 | } |
1575 | | |
1576 | | static void |
1577 | | str_buf_demangle_callback (const char *data, size_t len, void *opaque) |
1578 | 0 | { |
1579 | 0 | str_buf_append ((struct str_buf *)opaque, data, len); |
1580 | 0 | } |
1581 | | |
1582 | | char * |
1583 | | rust_demangle (const char *mangled, int options) |
1584 | 0 | { |
1585 | 0 | struct str_buf out; |
1586 | 0 | int success; |
1587 | |
|
1588 | 0 | out.ptr = NULL; |
1589 | 0 | out.len = 0; |
1590 | 0 | out.cap = 0; |
1591 | 0 | out.errored = 0; |
1592 | |
|
1593 | 0 | success = rust_demangle_callback (mangled, options, |
1594 | 0 | str_buf_demangle_callback, &out); |
1595 | |
|
1596 | 0 | if (!success) |
1597 | 0 | { |
1598 | 0 | free (out.ptr); |
1599 | 0 | return NULL; |
1600 | 0 | } |
1601 | | |
1602 | 0 | str_buf_append (&out, "\0", 1); |
1603 | 0 | return out.ptr; |
1604 | 0 | } |