1 use core::char;
2 use core::fmt;
3 use core::fmt::Display;
4
5 /// Representation of a demangled symbol name.
6 pub struct Demangle<'a> {
7 inner: &'a str,
8 }
9
10 /// De-mangles a Rust symbol into a more readable version
11 ///
12 /// This function will take a **mangled** symbol and return a value. When printed,
13 /// the de-mangled version will be written. If the symbol does not look like
14 /// a mangled symbol, the original value will be written instead.
demangle(s: &str) -> Result<(Demangle, &str), Invalid>15 pub fn demangle(s: &str) -> Result<(Demangle, &str), Invalid> {
16 // First validate the symbol. If it doesn't look like anything we're
17 // expecting, we just print it literally. Note that we must handle non-Rust
18 // symbols because we could have any function in the backtrace.
19 let inner;
20 if s.len() > 2 && s.starts_with("_R") {
21 inner = &s[2..];
22 } else if s.len() > 1 && s.starts_with("R") {
23 // On Windows, dbghelp strips leading underscores, so we accept "R..."
24 // form too.
25 inner = &s[1..];
26 } else if s.len() > 3 && s.starts_with("__R") {
27 // On OSX, symbols are prefixed with an extra _
28 inner = &s[3..];
29 } else {
30 return Err(Invalid);
31 }
32
33 // Paths always start with uppercase characters.
34 match inner.as_bytes()[0] {
35 b'A'...b'Z' => {}
36 _ => return Err(Invalid),
37 }
38
39 // only work with ascii text
40 if inner.bytes().any(|c| c & 0x80 != 0) {
41 return Err(Invalid);
42 }
43
44 // Verify that the symbol is indeed a valid path.
45 let mut parser = Parser {
46 sym: inner,
47 next: 0,
48 };
49 try!(parser.skip_path());
50
51 // Instantiating crate (paths always start with uppercase characters).
52 match parser.sym.as_bytes().get(parser.next) {
53 Some(&b'A'...b'Z') => {
54 try!(parser.skip_path());
55 }
56 _ => {}
57 }
58
59 Ok((Demangle {
60 inner: inner,
61 }, &parser.sym[parser.next..]))
62 }
63
64 impl<'s> Display for Demangle<'s> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 let mut printer = Printer {
67 parser: Ok(Parser {
68 sym: self.inner,
69 next: 0,
70 }),
71 out: f,
72 bound_lifetime_depth: 0,
73 };
74 printer.print_path(true)
75 }
76 }
77
78 #[derive(PartialEq, Eq)]
79 pub struct Invalid;
80
81 struct Ident<'s> {
82 /// ASCII part of the identifier.
83 ascii: &'s str,
84 /// Punycode insertion codes for Unicode codepoints, if any.
85 punycode: &'s str,
86 }
87
88 const SMALL_PUNYCODE_LEN: usize = 128;
89
90 impl<'s> Ident<'s> {
91 /// Attempt to decode punycode on the stack (allocation-free),
92 /// and pass the char slice to the closure, if successful.
93 /// This supports up to `SMALL_PUNYCODE_LEN` characters.
try_small_punycode_decode<F: FnOnce(&[char]) -> R, R>( &self, f: F, ) -> Option<R>94 fn try_small_punycode_decode<F: FnOnce(&[char]) -> R, R>(
95 &self,
96 f: F,
97 ) -> Option<R> {
98 let mut out = ['\0'; SMALL_PUNYCODE_LEN];
99 let mut out_len = 0;
100 let r = self.punycode_decode(|i, c| {
101 // Check there's space left for another character.
102 try!(out.get(out_len).ok_or(()));
103
104 // Move the characters after the insert position.
105 let mut j = out_len;
106 out_len += 1;
107
108 while j > i {
109 out[j] = out[j - 1];
110 j -= 1;
111 }
112
113 // Insert the new character.
114 out[i] = c;
115
116 Ok(())
117 });
118 if r.is_ok() {
119 Some(f(&out[..out_len]))
120 } else {
121 None
122 }
123 }
124
125 /// Decode punycode as insertion positions and characters
126 /// and pass them to the closure, which can return `Err(())`
127 /// to stop the decoding process.
punycode_decode<F: FnMut(usize, char) -> Result<(), ()>>( &self, mut insert: F, ) -> Result<(), ()>128 fn punycode_decode<F: FnMut(usize, char) -> Result<(), ()>>(
129 &self,
130 mut insert: F,
131 ) -> Result<(), ()> {
132 let mut punycode_bytes = self.punycode.bytes().peekable();
133 if punycode_bytes.peek().is_none() {
134 return Err(());
135 }
136
137 let mut len = 0;
138
139 // Populate initial output from ASCII fragment.
140 for c in self.ascii.chars() {
141 try!(insert(len, c));
142 len += 1;
143 }
144
145 // Punycode parameters and initial state.
146 let base = 36;
147 let t_min = 1;
148 let t_max = 26;
149 let skew = 38;
150 let mut damp = 700;
151 let mut bias = 72;
152 let mut i: usize = 0;
153 let mut n: usize = 0x80;
154
155 loop {
156 // Read one delta value.
157 let mut delta: usize = 0;
158 let mut w = 1;
159 let mut k: usize = 0;
160 loop {
161 use core::cmp::{min, max};
162
163 k += base;
164 let t = min(max(k.saturating_sub(bias), t_min), t_max);
165
166 let d = match punycode_bytes.next() {
167 Some(d @ b'a'...b'z') => d - b'a',
168 Some(d @ b'0'...b'9') => 26 + (d - b'0'),
169 _ => return Err(()),
170 };
171 let d = d as usize;
172 delta = try!(delta.checked_add(
173 try!(d.checked_mul(w).ok_or(()))
174 ).ok_or(()));
175 if d < t {
176 break;
177 }
178 w = try!(w.checked_mul(base - t).ok_or(()));
179 }
180
181 // Compute the new insert position and character.
182 len += 1;
183 i = try!(i.checked_add(delta).ok_or(()));
184 n = try!(n.checked_add(i / len).ok_or(()));
185 i %= len;
186
187 let n_u32 = n as u32;
188 let c = if n_u32 as usize == n {
189 try!(char::from_u32(n_u32).ok_or(()))
190 } else {
191 return Err(());
192 };
193
194 // Insert the new character and increment the insert position.
195 try!(insert(i, c));
196 i += 1;
197
198 // If there are no more deltas, decoding is complete.
199 if punycode_bytes.peek().is_none() {
200 return Ok(());
201 }
202
203 // Perform bias adaptation.
204 delta /= damp;
205 damp = 2;
206
207 delta += delta / len;
208 let mut k = 0;
209 while delta > ((base - t_min) * t_max) / 2 {
210 delta /= base - t_min;
211 k += base;
212 }
213 bias = k + ((base - t_min + 1) * delta) / (delta + skew);
214 }
215 }
216 }
217
218 impl<'s> Display for Ident<'s> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result219 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
220 self.try_small_punycode_decode(|chars| {
221 for &c in chars {
222 try!(c.fmt(f));
223 }
224 Ok(())
225 }).unwrap_or_else(|| {
226 if !self.punycode.is_empty() {
227 try!(f.write_str("punycode{"));
228
229 // Reconstruct a standard Punycode encoding,
230 // by using `-` as the separator.
231 if !self.ascii.is_empty() {
232 try!(f.write_str(self.ascii));
233 try!(f.write_str("-"));
234 }
235 try!(f.write_str(self.punycode));
236
237 f.write_str("}")
238 } else {
239 f.write_str(self.ascii)
240 }
241 })
242 }
243 }
244
basic_type(tag: u8) -> Option<&'static str>245 fn basic_type(tag: u8) -> Option<&'static str> {
246 Some(match tag {
247 b'b' => "bool",
248 b'c' => "char",
249 b'e' => "str",
250 b'u' => "()",
251 b'a' => "i8",
252 b's' => "i16",
253 b'l' => "i32",
254 b'x' => "i64",
255 b'n' => "i128",
256 b'i' => "isize",
257 b'h' => "u8",
258 b't' => "u16",
259 b'm' => "u32",
260 b'y' => "u64",
261 b'o' => "u128",
262 b'j' => "usize",
263 b'f' => "f32",
264 b'd' => "f64",
265 b'z' => "!",
266 b'p' => "_",
267 b'v' => "...",
268
269 _ => return None,
270 })
271 }
272
273 struct Parser<'s> {
274 sym: &'s str,
275 next: usize,
276 }
277
278 impl<'s> Parser<'s> {
peek(&self) -> Option<u8>279 fn peek(&self) -> Option<u8> {
280 self.sym.as_bytes().get(self.next).cloned()
281 }
282
eat(&mut self, b: u8) -> bool283 fn eat(&mut self, b: u8) -> bool {
284 if self.peek() == Some(b) {
285 self.next += 1;
286 true
287 } else {
288 false
289 }
290 }
291
next(&mut self) -> Result<u8, Invalid>292 fn next(&mut self) -> Result<u8, Invalid> {
293 let b = try!(self.peek().ok_or(Invalid));
294 self.next += 1;
295 Ok(b)
296 }
297
hex_nibbles(&mut self) -> Result<&'s str, Invalid>298 fn hex_nibbles(&mut self) -> Result<&'s str, Invalid> {
299 let start = self.next;
300 loop {
301 match try!(self.next()) {
302 b'0'...b'9' | b'a'...b'f' => {}
303 b'_' => break,
304 _ => return Err(Invalid),
305 }
306 }
307 Ok(&self.sym[start..self.next - 1])
308 }
309
digit_10(&mut self) -> Result<u8, Invalid>310 fn digit_10(&mut self) -> Result<u8, Invalid> {
311 let d = match self.peek() {
312 Some(d @ b'0'...b'9') => d - b'0',
313 _ => return Err(Invalid),
314 };
315 self.next += 1;
316 Ok(d)
317 }
318
digit_62(&mut self) -> Result<u8, Invalid>319 fn digit_62(&mut self) -> Result<u8, Invalid> {
320 let d = match self.peek() {
321 Some(d @ b'0'...b'9') => d - b'0',
322 Some(d @ b'a'...b'z') => 10 + (d - b'a'),
323 Some(d @ b'A'...b'Z') => 10 + 26 + (d - b'A'),
324 _ => return Err(Invalid),
325 };
326 self.next += 1;
327 Ok(d)
328 }
329
integer_62(&mut self) -> Result<u64, Invalid>330 fn integer_62(&mut self) -> Result<u64, Invalid> {
331 if self.eat(b'_') {
332 return Ok(0);
333 }
334
335 let mut x: u64 = 0;
336 while !self.eat(b'_') {
337 let d = try!(self.digit_62()) as u64;
338 x = try!(x.checked_mul(62).ok_or(Invalid));
339 x = try!(x.checked_add(d).ok_or(Invalid));
340 }
341 x.checked_add(1).ok_or(Invalid)
342 }
343
opt_integer_62(&mut self, tag: u8) -> Result<u64, Invalid>344 fn opt_integer_62(&mut self, tag: u8) -> Result<u64, Invalid> {
345 if !self.eat(tag) {
346 return Ok(0);
347 }
348 try!(self.integer_62()).checked_add(1).ok_or(Invalid)
349 }
350
disambiguator(&mut self) -> Result<u64, Invalid>351 fn disambiguator(&mut self) -> Result<u64, Invalid> {
352 self.opt_integer_62(b's')
353 }
354
namespace(&mut self) -> Result<Option<char>, Invalid>355 fn namespace(&mut self) -> Result<Option<char>, Invalid> {
356 match try!(self.next()) {
357 // Special namespaces, like closures and shims.
358 ns @ b'A'...b'Z' => Ok(Some(ns as char)),
359
360 // Implementation-specific/unspecified namespaces.
361 b'a'...b'z' => Ok(None),
362
363 _ => Err(Invalid),
364 }
365 }
366
backref(&mut self) -> Result<Parser<'s>, Invalid>367 fn backref(&mut self) -> Result<Parser<'s>, Invalid> {
368 let s_start = self.next - 1;
369 let i = try!(self.integer_62());
370 if i >= s_start as u64 {
371 return Err(Invalid);
372 }
373 Ok(Parser {
374 sym: self.sym,
375 next: i as usize,
376 })
377 }
378
ident(&mut self) -> Result<Ident<'s>, Invalid>379 fn ident(&mut self) -> Result<Ident<'s>, Invalid> {
380 let is_punycode = self.eat(b'u');
381 let mut len = try!(self.digit_10()) as usize;
382 if len != 0 {
383 loop {
384 match self.digit_10() {
385 Ok(d) => {
386 len = try!(len.checked_mul(10).ok_or(Invalid));
387 len = try!(len.checked_add(d as usize).ok_or(Invalid));
388 }
389 Err(Invalid) => break,
390 }
391 }
392 }
393
394 // Skip past the optional `_` separator.
395 self.eat(b'_');
396
397 let start = self.next;
398 self.next = try!(self.next.checked_add(len).ok_or(Invalid));
399 if self.next > self.sym.len() {
400 return Err(Invalid);
401 }
402
403 let ident = &self.sym[start..self.next];
404
405 if is_punycode {
406 let ident = match ident.bytes().rposition(|b| b == b'_') {
407 Some(i) => Ident {
408 ascii: &ident[..i],
409 punycode: &ident[i + 1..],
410 },
411 None => Ident {
412 ascii: "",
413 punycode: ident,
414 },
415 };
416 if ident.punycode.is_empty() {
417 return Err(Invalid);
418 }
419 Ok(ident)
420 } else {
421 Ok(Ident {
422 ascii: ident,
423 punycode: "",
424 })
425 }
426 }
427
skip_path(&mut self) -> Result<(), Invalid>428 fn skip_path(&mut self) -> Result<(), Invalid> {
429 match try!(self.next()) {
430 b'C' => {
431 try!(self.disambiguator());
432 try!(self.ident());
433 }
434 b'N' => {
435 try!(self.namespace());
436 try!(self.skip_path());
437 try!(self.disambiguator());
438 try!(self.ident());
439 }
440 b'M' => {
441 try!(self.disambiguator());
442 try!(self.skip_path());
443 try!(self.skip_type());
444 }
445 b'X' => {
446 try!(self.disambiguator());
447 try!(self.skip_path());
448 try!(self.skip_type());
449 try!(self.skip_path());
450 }
451 b'Y' => {
452 try!(self.skip_type());
453 try!(self.skip_path());
454 }
455 b'I' => {
456 try!(self.skip_path());
457 while !self.eat(b'E') {
458 try!(self.skip_generic_arg());
459 }
460 }
461 b'B' => {
462 try!(self.backref());
463 }
464 _ => return Err(Invalid),
465 }
466 Ok(())
467 }
468
skip_generic_arg(&mut self) -> Result<(), Invalid>469 fn skip_generic_arg(&mut self) -> Result<(), Invalid> {
470 if self.eat(b'L') {
471 try!(self.integer_62());
472 Ok(())
473 } else if self.eat(b'K') {
474 self.skip_const()
475 } else {
476 self.skip_type()
477 }
478 }
479
skip_type(&mut self) -> Result<(), Invalid>480 fn skip_type(&mut self) -> Result<(), Invalid> {
481 match try!(self.next()) {
482 tag if basic_type(tag).is_some() => {}
483
484 b'R' | b'Q' => {
485 if self.eat(b'L') {
486 try!(self.integer_62());
487 }
488 try!(self.skip_type());
489 }
490 b'P' | b'O' | b'S' => try!(self.skip_type()),
491 b'A' => {
492 try!(self.skip_type());
493 try!(self.skip_const());
494 }
495 b'T' => while !self.eat(b'E') {
496 try!(self.skip_type());
497 },
498 b'F' => {
499 let _binder = try!(self.opt_integer_62(b'G'));
500 let _is_unsafe = self.eat(b'U');
501 if self.eat(b'K') {
502 let c_abi = self.eat(b'C');
503 if !c_abi {
504 let abi = try!(self.ident());
505 if abi.ascii.is_empty() || !abi.punycode.is_empty() {
506 return Err(Invalid);
507 }
508 }
509 }
510 while !self.eat(b'E') {
511 try!(self.skip_type());
512 }
513 try!(self.skip_type());
514 }
515 b'D' => {
516 let _binder = try!(self.opt_integer_62(b'G'));
517 while !self.eat(b'E') {
518 try!(self.skip_path());
519 while self.eat(b'p') {
520 try!(self.ident());
521 try!(self.skip_type());
522 }
523 }
524 if !self.eat(b'L') {
525 return Err(Invalid);
526 }
527 try!(self.integer_62());
528 }
529 b'B' => {
530 try!(self.backref());
531 }
532 _ => {
533 // Go back to the tag, so `skip_path` also sees it.
534 self.next -= 1;
535 try!(self.skip_path());
536 }
537 }
538 Ok(())
539 }
540
skip_const(&mut self) -> Result<(), Invalid>541 fn skip_const(&mut self) -> Result<(), Invalid> {
542 if self.eat(b'B') {
543 try!(self.backref());
544 return Ok(());
545 }
546
547 match try!(self.next()) {
548 // Unsigned integer types.
549 b'h' | b't' | b'm' | b'y' | b'o' | b'j' => {}
550
551 _ => return Err(Invalid),
552 }
553
554 if self.eat(b'p') {
555 return Ok(());
556 }
557 try!(self.hex_nibbles());
558 Ok(())
559 }
560 }
561
562 struct Printer<'a, 'b: 'a, 's> {
563 parser: Result<Parser<'s>, Invalid>,
564 out: &'a mut fmt::Formatter<'b>,
565 bound_lifetime_depth: u32,
566 }
567
568 /// Mark the parser as errored, print `?` and return early.
569 /// This allows callers to keep printing the approximate
570 /// syntax of the path/type/const, despite having errors.
571 /// E.g. `Vec<[(A, ?); ?]>` instead of `Vec<[(A, ?`.
572 macro_rules! invalid {
573 ($printer:ident) => {{
574 $printer.parser = Err(Invalid);
575 return $printer.out.write_str("?");
576 }}
577 }
578
579 /// Call a parser method (if the parser hasn't errored yet),
580 /// and mark the parser as errored if it returns `Err(Invalid)`.
581 ///
582 /// If the parser errored, before or now, prints `?`, and
583 /// returns early the current function (see `invalid!` above).
584 macro_rules! parse {
585 ($printer:ident, $method:ident $(($($arg:expr),*))*) => {
586 match $printer.parser_mut().and_then(|p| p.$method($($($arg),*)*)) {
587 Ok(x) => x,
588 Err(Invalid) => invalid!($printer),
589 }
590 };
591 }
592
593 impl<'a, 'b, 's> Printer<'a, 'b, 's> {
parser_mut<'c>(&'c mut self) -> Result<&'c mut Parser<'s>, Invalid>594 fn parser_mut<'c>(&'c mut self) -> Result<&'c mut Parser<'s>, Invalid> {
595 self.parser.as_mut().map_err(|_| Invalid)
596 }
597
598 /// Eat the given character from the parser,
599 /// returning `false` if the parser errored.
eat(&mut self, b: u8) -> bool600 fn eat(&mut self, b: u8) -> bool {
601 self.parser_mut().map(|p| p.eat(b)) == Ok(true)
602 }
603
604 /// Return a nested parser for a backref.
backref_printer<'c>(&'c mut self) -> Printer<'c, 'b, 's>605 fn backref_printer<'c>(&'c mut self) -> Printer<'c, 'b, 's> {
606 Printer {
607 parser: self.parser_mut().and_then(|p| p.backref()),
608 out: self.out,
609 bound_lifetime_depth: self.bound_lifetime_depth,
610 }
611 }
612
613 /// Print the lifetime according to the previously decoded index.
614 /// An index of `0` always refers to `'_`, but starting with `1`,
615 /// indices refer to late-bound lifetimes introduced by a binder.
print_lifetime_from_index(&mut self, lt: u64) -> fmt::Result616 fn print_lifetime_from_index(&mut self, lt: u64) -> fmt::Result {
617 try!(self.out.write_str("'"));
618 if lt == 0 {
619 return self.out.write_str("_");
620 }
621 match (self.bound_lifetime_depth as u64).checked_sub(lt) {
622 Some(depth) => {
623 // Try to print lifetimes alphabetically first.
624 if depth < 26 {
625 let c = (b'a' + depth as u8) as char;
626 c.fmt(self.out)
627 } else {
628 // Use `'_123` after running out of letters.
629 try!(self.out.write_str("_"));
630 depth.fmt(self.out)
631 }
632 }
633 None => invalid!(self),
634 }
635 }
636
637 /// Optionally enter a binder ('G') for late-bound lifetimes,
638 /// printing e.g. `for<'a, 'b> ` before calling the closure,
639 /// and make those lifetimes visible to it (via depth level).
in_binder<F>(&mut self, f: F) -> fmt::Result where F: FnOnce(&mut Self) -> fmt::Result,640 fn in_binder<F>(&mut self, f: F) -> fmt::Result
641 where F: FnOnce(&mut Self) -> fmt::Result,
642 {
643 let bound_lifetimes = parse!(self, opt_integer_62(b'G'));
644
645 if bound_lifetimes > 0 {
646 try!(self.out.write_str("for<"));
647 for i in 0..bound_lifetimes {
648 if i > 0 {
649 try!(self.out.write_str(", "));
650 }
651 self.bound_lifetime_depth += 1;
652 try!(self.print_lifetime_from_index(1));
653 }
654 try!(self.out.write_str("> "));
655 }
656
657 let r = f(self);
658
659 // Restore `bound_lifetime_depth` to the previous value.
660 self.bound_lifetime_depth -= bound_lifetimes as u32;
661
662 r
663 }
664
665 /// Print list elements using the given closure and separator,
666 /// until the end of the list ('E') is found, or the parser errors.
667 /// Returns the number of elements printed.
print_sep_list<F>(&mut self, f: F, sep: &str) -> Result<usize, fmt::Error> where F: Fn(&mut Self) -> fmt::Result,668 fn print_sep_list<F>(&mut self, f: F, sep: &str) -> Result<usize, fmt::Error>
669 where F: Fn(&mut Self) -> fmt::Result,
670 {
671 let mut i = 0;
672 while self.parser.is_ok() && !self.eat(b'E') {
673 if i > 0 {
674 try!(self.out.write_str(sep));
675 }
676 try!(f(self));
677 i += 1;
678 }
679 Ok(i)
680 }
681
print_path(&mut self, in_value: bool) -> fmt::Result682 fn print_path(&mut self, in_value: bool) -> fmt::Result {
683 let tag = parse!(self, next);
684 match tag {
685 b'C' => {
686 let dis = parse!(self, disambiguator);
687 let name = parse!(self, ident);
688
689 try!(name.fmt(self.out));
690 if !self.out.alternate() {
691 try!(self.out.write_str("["));
692 try!(fmt::LowerHex::fmt(&dis, self.out));
693 try!(self.out.write_str("]"));
694 }
695 }
696 b'N' => {
697 let ns = parse!(self, namespace);
698
699 try!(self.print_path(in_value));
700
701 let dis = parse!(self, disambiguator);
702 let name = parse!(self, ident);
703
704 match ns {
705 // Special namespaces, like closures and shims.
706 Some(ns) => {
707 try!(self.out.write_str("::{"));
708 match ns {
709 'C' => try!(self.out.write_str("closure")),
710 'S' => try!(self.out.write_str("shim")),
711 _ => try!(ns.fmt(self.out)),
712 }
713 if !name.ascii.is_empty() || !name.punycode.is_empty() {
714 try!(self.out.write_str(":"));
715 try!(name.fmt(self.out));
716 }
717 try!(self.out.write_str("#"));
718 try!(dis.fmt(self.out));
719 try!(self.out.write_str("}"));
720 }
721
722 // Implementation-specific/unspecified namespaces.
723 None => {
724 if !name.ascii.is_empty() || !name.punycode.is_empty() {
725 try!(self.out.write_str("::"));
726 try!(name.fmt(self.out));
727 }
728 }
729 }
730 }
731 b'M' | b'X' | b'Y' => {
732 if tag != b'Y' {
733 // Ignore the `impl`'s own path.
734 parse!(self, disambiguator);
735 parse!(self, skip_path);
736 }
737
738 try!(self.out.write_str("<"));
739 try!(self.print_type());
740 if tag != b'M' {
741 try!(self.out.write_str(" as "));
742 try!(self.print_path(false));
743 }
744 try!(self.out.write_str(">"));
745 }
746 b'I' => {
747 try!(self.print_path(in_value));
748 if in_value {
749 try!(self.out.write_str("::"));
750 }
751 try!(self.out.write_str("<"));
752 try!(self.print_sep_list(Self::print_generic_arg, ", "));
753 try!(self.out.write_str(">"));
754 }
755 b'B' => {
756 try!(self.backref_printer().print_path(in_value));
757 }
758 _ => invalid!(self),
759 }
760 Ok(())
761 }
762
print_generic_arg(&mut self) -> fmt::Result763 fn print_generic_arg(&mut self) -> fmt::Result {
764 if self.eat(b'L') {
765 let lt = parse!(self, integer_62);
766 self.print_lifetime_from_index(lt)
767 } else if self.eat(b'K') {
768 self.print_const()
769 } else {
770 self.print_type()
771 }
772 }
773
print_type(&mut self) -> fmt::Result774 fn print_type(&mut self) -> fmt::Result {
775 let tag = parse!(self, next);
776
777 match basic_type(tag) {
778 Some(ty) => return self.out.write_str(ty),
779 None => {}
780 }
781
782 match tag {
783 b'R' | b'Q' => {
784 try!(self.out.write_str("&"));
785 if self.eat(b'L') {
786 let lt = parse!(self, integer_62);
787 if lt != 0 {
788 try!(self.print_lifetime_from_index(lt));
789 try!(self.out.write_str(" "));
790 }
791 }
792 if tag != b'R' {
793 try!(self.out.write_str("mut "));
794 }
795 try!(self.print_type());
796 }
797
798 b'P' | b'O' => {
799 try!(self.out.write_str("*"));
800 if tag != b'P' {
801 try!(self.out.write_str("mut "));
802 } else {
803 try!(self.out.write_str("const "));
804 }
805 try!(self.print_type());
806 }
807
808 b'A' | b'S' => {
809 try!(self.out.write_str("["));
810 try!(self.print_type());
811 if tag == b'A' {
812 try!(self.out.write_str("; "));
813 try!(self.print_const());
814 }
815 try!(self.out.write_str("]"));
816 }
817 b'T' => {
818 try!(self.out.write_str("("));
819 let count = try!(self.print_sep_list(Self::print_type, ", "));
820 if count == 1 {
821 try!(self.out.write_str(","));
822 }
823 try!(self.out.write_str(")"));
824 }
825 b'F' => try!(self.in_binder(|this| {
826 let is_unsafe = this.eat(b'U');
827 let abi = if this.eat(b'K') {
828 if this.eat(b'C') {
829 Some("C")
830 } else {
831 let abi = parse!(this, ident);
832 if abi.ascii.is_empty() || !abi.punycode.is_empty() {
833 invalid!(this);
834 }
835 Some(abi.ascii)
836 }
837 } else {
838 None
839 };
840
841 if is_unsafe {
842 try!(this.out.write_str("unsafe "));
843 }
844
845 match abi {
846 Some(abi) => {
847 try!(this.out.write_str("extern \""));
848
849 // If the ABI had any `-`, they were replaced with `_`,
850 // so the parts between `_` have to be re-joined with `-`.
851 let mut parts = abi.split('_');
852 try!(this.out.write_str(parts.next().unwrap()));
853 for part in parts {
854 try!(this.out.write_str("-"));
855 try!(this.out.write_str(part));
856 }
857
858 try!(this.out.write_str("\" "));
859 }
860 None => {}
861 }
862
863 try!(this.out.write_str("fn("));
864 try!(this.print_sep_list(Self::print_type, ", "));
865 try!(this.out.write_str(")"));
866
867 if this.eat(b'u') {
868 // Skip printing the return type if it's 'u', i.e. `()`.
869 } else {
870 try!(this.out.write_str(" -> "));
871 try!(this.print_type());
872 }
873
874 Ok(())
875 })),
876 b'D' => {
877 try!(self.out.write_str("dyn "));
878 try!(self.in_binder(|this| {
879 try!(this.print_sep_list(Self::print_dyn_trait, " + "));
880 Ok(())
881 }));
882
883 if !self.eat(b'L') {
884 invalid!(self);
885 }
886 let lt = parse!(self, integer_62);
887 if lt != 0 {
888 try!(self.out.write_str(" + "));
889 try!(self.print_lifetime_from_index(lt));
890 }
891 }
892 b'B' => {
893 try!(self.backref_printer().print_type());
894 }
895 _ => {
896 // Go back to the tag, so `print_path` also sees it.
897 let _ = self.parser_mut().map(|p| p.next -= 1);
898 try!(self.print_path(false));
899 }
900 }
901 Ok(())
902 }
903
904 /// A trait in a trait object may have some "existential projections"
905 /// (i.e. associated type bindings) after it, which should be printed
906 /// in the `<...>` of the trait, e.g. `dyn Trait<T, U, Assoc=X>`.
907 /// To this end, this method will keep the `<...>` of an 'I' path
908 /// open, by omitting the `>`, and return `Ok(true)` in that case.
print_path_maybe_open_generics(&mut self) -> Result<bool, fmt::Error>909 fn print_path_maybe_open_generics(&mut self) -> Result<bool, fmt::Error> {
910 if self.eat(b'B') {
911 self.backref_printer().print_path_maybe_open_generics()
912 } else if self.eat(b'I') {
913 try!(self.print_path(false));
914 try!(self.out.write_str("<"));
915 try!(self.print_sep_list(Self::print_generic_arg, ", "));
916 Ok(true)
917 } else {
918 try!(self.print_path(false));
919 Ok(false)
920 }
921 }
922
print_dyn_trait(&mut self) -> fmt::Result923 fn print_dyn_trait(&mut self) -> fmt::Result {
924 let mut open = try!(self.print_path_maybe_open_generics());
925
926 while self.eat(b'p') {
927 if !open {
928 try!(self.out.write_str("<"));
929 open = true;
930 } else {
931 try!(self.out.write_str(", "));
932 }
933
934 let name = parse!(self, ident);
935 try!(name.fmt(self.out));
936 try!(self.out.write_str(" = "));
937 try!(self.print_type());
938 }
939
940 if open {
941 try!(self.out.write_str(">"));
942 }
943
944 Ok(())
945 }
946
print_const(&mut self) -> fmt::Result947 fn print_const(&mut self) -> fmt::Result {
948 if self.eat(b'B') {
949 return self.backref_printer().print_const();
950 }
951
952 let ty_tag = parse!(self, next);
953 let ty = match ty_tag {
954 // Unsigned integer types.
955 b'h' | b't' | b'm' | b'y' | b'o' | b'j' => {
956 basic_type(ty_tag).unwrap()
957 }
958
959 _ => invalid!(self),
960 };
961
962
963 if self.eat(b'p') {
964 try!(self.out.write_str("_"));
965 } else {
966 try!(self.print_const_uint());
967 }
968
969 if !self.out.alternate() {
970 try!(self.out.write_str(": "));
971 try!(self.out.write_str(ty));
972 }
973
974 Ok(())
975 }
976
print_const_uint(&mut self) -> fmt::Result977 fn print_const_uint(&mut self) -> fmt::Result {
978 let hex = parse!(self, hex_nibbles);
979
980 // Print anything that doesn't fit in `u64` verbatim.
981 if hex.len() > 16 {
982 try!(self.out.write_str("0x"));
983 return self.out.write_str(hex);
984 }
985
986 let mut v = 0;
987 for c in hex.chars() {
988 v = (v << 4) | (c.to_digit(16).unwrap() as u64);
989 }
990 v.fmt(self.out)
991 }
992 }
993
994 #[cfg(test)]
995 mod tests {
996 macro_rules! t_nohash {
997 ($a:expr, $b:expr) => ({
998 assert_eq!(format!("{:#}", ::demangle($a)), $b);
999 })
1000 }
1001 macro_rules! t_nohash_type {
1002 ($a:expr, $b:expr) => (
1003 t_nohash!(concat!("_RMC0", $a), concat!("<", $b, ">"))
1004 )
1005 }
1006
1007 #[test]
demangle_crate_with_leading_digit()1008 fn demangle_crate_with_leading_digit() {
1009 t_nohash!(
1010 "_RNvC6_123foo3bar",
1011 "123foo::bar"
1012 );
1013 }
1014
1015 #[test]
demangle_utf8_idents()1016 fn demangle_utf8_idents() {
1017 t_nohash!(
1018 "_RNqCs4fqI2P2rA04_11utf8_identsu30____7hkackfecea1cbdathfdh9hlq6y",
1019 "utf8_idents::საჭმელად_გემრიელი_სადილი"
1020 );
1021 }
1022
1023 #[test]
demangle_closure()1024 fn demangle_closure() {
1025 t_nohash!(
1026 "_RNCNCNgCs6DXkGYLi8lr_2cc5spawn00B5_",
1027 "cc::spawn::{closure#0}::{closure#0}"
1028 );
1029 t_nohash!(
1030 "_RNCINkXs25_NgCsbmNqQUJIY6D_4core5sliceINyB9_4IterhENuNgNoBb_4iter8iterator8Iterator9rpositionNCNgNpB9_6memchr7memrchrs_0E0Bb_",
1031 "<core::slice::Iter<u8> as core::iter::iterator::Iterator>::rposition::<core::slice::memchr::memrchr::{closure#1}>::{closure#0}"
1032 );
1033 }
1034
1035 #[test]
demangle_dyn_trait()1036 fn demangle_dyn_trait() {
1037 t_nohash!(
1038 "_RINbNbCskIICzLVDPPb_5alloc5alloc8box_freeDINbNiB4_5boxed5FnBoxuEp6OutputuEL_ECs1iopQbuBiw2_3std",
1039 "alloc::alloc::box_free::<dyn alloc::boxed::FnBox<(), Output = ()>>"
1040 );
1041 }
1042
1043 #[test]
demangle_const_generics()1044 fn demangle_const_generics() {
1045 // NOTE(eddyb) this was hand-written, before rustc had working
1046 // const generics support (but the mangling format did include them).
1047 t_nohash_type!(
1048 "INtC8arrayvec8ArrayVechKj7b_E",
1049 "arrayvec::ArrayVec<u8, 123>"
1050 );
1051 }
1052
1053 #[test]
demangle_exponential_explosion()1054 fn demangle_exponential_explosion() {
1055 // NOTE(eddyb) because of the prefix added by `t_nohash_type!` is
1056 // 3 bytes long, `B2_` refers to the start of the type, not `B_`.
1057 // 6 backrefs (`B8_E` through `B3_E`) result in 2^6 = 64 copies of `_`.
1058 // Also, because the `p` (`_`) type is after all of the starts of the
1059 // backrefs, it can be replaced with any other type, independently.
1060 t_nohash_type!(
1061 concat!("TTTTTT", "p", "B8_E", "B7_E", "B6_E", "B5_E", "B4_E", "B3_E"),
1062 "((((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _)))), \
1063 ((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _))))), \
1064 (((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _)))), \
1065 ((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _))))))"
1066 );
1067 }
1068
1069 #[test]
demangle_thinlto()1070 fn demangle_thinlto() {
1071 t_nohash!("_RC3foo.llvm.9D1C9369", "foo");
1072 t_nohash!("_RC3foo.llvm.9D1C9369@@16", "foo");
1073 t_nohash!("_RNvC9backtrace3foo.llvm.A5310EB9", "backtrace::foo");
1074 }
1075
1076 #[test]
demangle_extra_suffix()1077 fn demangle_extra_suffix() {
1078 // From alexcrichton/rustc-demangle#27:
1079 t_nohash!(
1080 "_RNvNtNtNtNtCs92dm3009vxr_4rand4rngs7adapter9reseeding4fork23FORK_HANDLER_REGISTERED.0.0",
1081 "rand::rngs::adapter::reseeding::fork::FORK_HANDLER_REGISTERED.0.0"
1082 );
1083 }
1084 }
1085