11
11
use middle:: const_eval:: ConstVal ;
12
12
use middle:: def_id:: DefId ;
13
13
use middle:: subst:: Substs ;
14
- use middle:: ty:: { AdtDef , ClosureSubsts , FnOutput , Region , Ty } ;
14
+ use middle:: ty:: { self , AdtDef , ClosureSubsts , FnOutput , Region , Ty } ;
15
15
use rustc_back:: slice;
16
16
use rustc_data_structures:: tuple_slice:: TupleSlice ;
17
17
use rustc_front:: hir:: InlineAsm ;
18
18
use syntax:: ast:: Name ;
19
19
use syntax:: codemap:: Span ;
20
- use std:: fmt:: { Debug , Formatter , Error } ;
21
- use std:: u32;
20
+ use std:: borrow:: { Cow , IntoCow } ;
21
+ use std:: fmt:: { Debug , Formatter , Error , Write } ;
22
+ use std:: { iter, u32} ;
22
23
23
24
/// Lowered representation of a single function.
24
25
#[ derive( RustcEncodable , RustcDecodable ) ]
@@ -317,31 +318,77 @@ impl<'tcx> BasicBlockData<'tcx> {
317
318
318
319
impl < ' tcx > Debug for Terminator < ' tcx > {
319
320
fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
321
+ try!( self . fmt_head ( fmt) ) ;
322
+ let successors = self . successors ( ) ;
323
+ let labels = self . fmt_successor_labels ( ) ;
324
+ assert_eq ! ( successors. len( ) , labels. len( ) ) ;
325
+
326
+ match successors. len ( ) {
327
+ 0 => Ok ( ( ) ) ,
328
+
329
+ 1 => write ! ( fmt, " -> {:?}" , successors[ 0 ] ) ,
330
+
331
+ _ => {
332
+ try!( write ! ( fmt, " -> [" ) ) ;
333
+ for ( i, target) in successors. iter ( ) . enumerate ( ) {
334
+ if i > 0 {
335
+ try!( write ! ( fmt, ", " ) ) ;
336
+ }
337
+ try!( write ! ( fmt, "{}: {:?}" , labels[ i] , target) ) ;
338
+ }
339
+ write ! ( fmt, "]" )
340
+ }
341
+
342
+ }
343
+ }
344
+ }
345
+
346
+ impl < ' tcx > Terminator < ' tcx > {
347
+ pub fn fmt_head < W : Write > ( & self , fmt : & mut W ) -> Result < ( ) , Error > {
320
348
use self :: Terminator :: * ;
321
349
match * self {
322
- Goto { target } =>
323
- write ! ( fmt, "goto -> {:?}" , target) ,
324
- Panic { target } =>
325
- write ! ( fmt, "panic -> {:?}" , target) ,
326
- If { cond : ref lv, ref targets } =>
327
- write ! ( fmt, "if({:?}) -> {:?}" , lv, targets) ,
328
- Switch { discr : ref lv, adt_def : _, ref targets } =>
329
- write ! ( fmt, "switch({:?}) -> {:?}" , lv, targets) ,
330
- SwitchInt { discr : ref lv, switch_ty : _, ref values, ref targets } =>
331
- write ! ( fmt, "switchInt({:?}, {:?}) -> {:?}" , lv, values, targets) ,
332
- Diverge =>
333
- write ! ( fmt, "diverge" ) ,
334
- Return =>
335
- write ! ( fmt, "return" ) ,
336
- Call { data : ref c, targets } => {
350
+ Goto { .. } => write ! ( fmt, "goto" ) ,
351
+ Panic { .. } => write ! ( fmt, "panic" ) ,
352
+ If { cond : ref lv, .. } => write ! ( fmt, "if({:?})" , lv) ,
353
+ Switch { discr : ref lv, .. } => write ! ( fmt, "switch({:?})" , lv) ,
354
+ SwitchInt { discr : ref lv, .. } => write ! ( fmt, "switchInt({:?})" , lv) ,
355
+ Diverge => write ! ( fmt, "diverge" ) ,
356
+ Return => write ! ( fmt, "return" ) ,
357
+ Call { data : ref c, .. } => {
337
358
try!( write ! ( fmt, "{:?} = {:?}(" , c. destination, c. func) ) ;
338
359
for ( index, arg) in c. args . iter ( ) . enumerate ( ) {
339
360
if index > 0 {
340
361
try!( write ! ( fmt, ", " ) ) ;
341
362
}
342
363
try!( write ! ( fmt, "{:?}" , arg) ) ;
343
364
}
344
- write ! ( fmt, ") -> {:?}" , targets)
365
+ write ! ( fmt, ")" )
366
+ }
367
+ }
368
+ }
369
+
370
+ pub fn fmt_successor_labels ( & self ) -> Vec < Cow < ' static , str > > {
371
+ use self :: Terminator :: * ;
372
+ match * self {
373
+ Diverge | Return => vec ! [ ] ,
374
+ Goto { .. } | Panic { .. } => vec ! [ "" . into_cow( ) ] ,
375
+ If { .. } => vec ! [ "true" . into_cow( ) , "false" . into_cow( ) ] ,
376
+ Call { .. } => vec ! [ "return" . into_cow( ) , "unwind" . into_cow( ) ] ,
377
+ Switch { ref adt_def, .. } => {
378
+ adt_def. variants
379
+ . iter ( )
380
+ . map ( |variant| variant. name . to_string ( ) . into_cow ( ) )
381
+ . collect ( )
382
+ }
383
+ SwitchInt { ref values, .. } => {
384
+ values. iter ( )
385
+ . map ( |const_val| {
386
+ let mut buf = String :: new ( ) ;
387
+ fmt_const_val ( & mut buf, const_val) . unwrap ( ) ;
388
+ buf. into_cow ( )
389
+ } )
390
+ . chain ( iter:: once ( String :: from ( "otherwise" ) . into_cow ( ) ) )
391
+ . collect ( )
345
392
}
346
393
}
347
394
}
@@ -495,19 +542,19 @@ impl<'tcx> Debug for Lvalue<'tcx> {
495
542
496
543
match * self {
497
544
Var ( id) =>
498
- write ! ( fmt, "Var( {:?}) " , id) ,
545
+ write ! ( fmt, "v {:?}" , id) ,
499
546
Arg ( id) =>
500
- write ! ( fmt, "Arg( {:?}) " , id) ,
547
+ write ! ( fmt, "a {:?}" , id) ,
501
548
Temp ( id) =>
502
- write ! ( fmt, "Temp( {:?}) " , id) ,
549
+ write ! ( fmt, "t {:?}" , id) ,
503
550
Static ( id) =>
504
551
write ! ( fmt, "Static({:?})" , id) ,
505
552
ReturnPointer =>
506
553
write ! ( fmt, "ReturnPointer" ) ,
507
554
Projection ( ref data) =>
508
555
match data. elem {
509
- ProjectionElem :: Downcast ( _ , variant_index ) =>
510
- write ! ( fmt, "({:?} as {:? })" , data. base, variant_index ) ,
556
+ ProjectionElem :: Downcast ( ref adt_def , index ) =>
557
+ write ! ( fmt, "({:?} as {})" , data. base, adt_def . variants [ index ] . name ) ,
511
558
ProjectionElem :: Deref =>
512
559
write ! ( fmt, "(*{:?})" , data. base) ,
513
560
ProjectionElem :: Field ( field) =>
@@ -671,12 +718,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
671
718
Use ( ref lvalue) => write ! ( fmt, "{:?}" , lvalue) ,
672
719
Repeat ( ref a, ref b) => write ! ( fmt, "[{:?}; {:?}]" , a, b) ,
673
720
Ref ( ref a, bk, ref b) => write ! ( fmt, "&{:?} {:?} {:?}" , a, bk, b) ,
674
- Len ( ref a) => write ! ( fmt, "LEN ({:?})" , a) ,
675
- Cast ( ref kind, ref lv, ref ty) => write ! ( fmt, "{:?} as {:?} ({:?}" , lv, ty, kind) ,
676
- BinaryOp ( ref op, ref a, ref b) => write ! ( fmt, "{:?}({:?},{:?})" , op, a, b) ,
721
+ Len ( ref a) => write ! ( fmt, "Len ({:?})" , a) ,
722
+ Cast ( ref kind, ref lv, ref ty) => write ! ( fmt, "{:?} as {:?} ({:?}) " , lv, ty, kind) ,
723
+ BinaryOp ( ref op, ref a, ref b) => write ! ( fmt, "{:?}({:?}, {:?})" , op, a, b) ,
677
724
UnaryOp ( ref op, ref a) => write ! ( fmt, "{:?}({:?})" , op, a) ,
678
- Box ( ref t) => write ! ( fmt, "Box {:?}" , t) ,
679
- Aggregate ( ref kind, ref lvs) => write ! ( fmt, "Aggregate<{:?}>( {:?}) " , kind, lvs) ,
725
+ Box ( ref t) => write ! ( fmt, "Box( {:?}) " , t) ,
726
+ Aggregate ( ref kind, ref lvs) => write ! ( fmt, "Aggregate<{:?}>{:?}" , kind, lvs) ,
680
727
InlineAsm ( ref asm) => write ! ( fmt, "InlineAsm({:?})" , asm) ,
681
728
Slice { ref input, from_start, from_end } =>
682
729
write ! ( fmt, "{:?}[{:?}..-{:?}]" , input, from_start, from_end) ,
@@ -691,7 +738,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
691
738
// this does not necessarily mean that they are "==" in Rust -- in
692
739
// particular one must be wary of `NaN`!
693
740
694
- #[ derive( Clone , Debug , PartialEq , RustcEncodable , RustcDecodable ) ]
741
+ #[ derive( Clone , PartialEq , RustcEncodable , RustcDecodable ) ]
695
742
pub struct Constant < ' tcx > {
696
743
pub span : Span ,
697
744
pub ty : Ty < ' tcx > ,
@@ -707,7 +754,7 @@ pub enum ItemKind {
707
754
Method ,
708
755
}
709
756
710
- #[ derive( Clone , Debug , PartialEq , RustcEncodable , RustcDecodable ) ]
757
+ #[ derive( Clone , PartialEq , RustcEncodable , RustcDecodable ) ]
711
758
pub enum Literal < ' tcx > {
712
759
Item {
713
760
def_id : DefId ,
@@ -718,3 +765,37 @@ pub enum Literal<'tcx> {
718
765
value : ConstVal ,
719
766
} ,
720
767
}
768
+
769
+ impl < ' tcx > Debug for Constant < ' tcx > {
770
+ fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
771
+ write ! ( fmt, "{:?}" , self . literal)
772
+ }
773
+ }
774
+
775
+ impl < ' tcx > Debug for Literal < ' tcx > {
776
+ fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
777
+ use self :: Literal :: * ;
778
+ match * self {
779
+ Item { def_id, .. } =>
780
+ write ! ( fmt, "{}" , ty:: tls:: with( |tcx| tcx. item_path_str( def_id) ) ) ,
781
+ Value { ref value } => fmt_const_val ( fmt, value) ,
782
+ }
783
+ }
784
+ }
785
+
786
+ pub fn fmt_const_val < W : Write > ( fmt : & mut W , const_val : & ConstVal ) -> Result < ( ) , Error > {
787
+ use middle:: const_eval:: ConstVal :: * ;
788
+ match * const_val {
789
+ Float ( f) => write ! ( fmt, "{:?}" , f) ,
790
+ Int ( n) => write ! ( fmt, "{:?}" , n) ,
791
+ Uint ( n) => write ! ( fmt, "{:?}" , n) ,
792
+ Str ( ref s) => write ! ( fmt, "Str({:?})" , s) ,
793
+ ByteStr ( ref bytes) => write ! ( fmt, "ByteStr{:?}" , bytes) ,
794
+ Bool ( b) => write ! ( fmt, "{:?}" , b) ,
795
+ Struct ( id) => write ! ( fmt, "Struct({:?})" , id) ,
796
+ Tuple ( id) => write ! ( fmt, "Tuple({:?})" , id) ,
797
+ Function ( def_id) => write ! ( fmt, "Function({:?})" , def_id) ,
798
+ Array ( id, n) => write ! ( fmt, "Array({:?}, {:?})" , id, n) ,
799
+ Repeat ( id, n) => write ! ( fmt, "Repeat({:?}, {:?})" , id, n) ,
800
+ }
801
+ }
0 commit comments