@@ -38,6 +38,7 @@ use value::Value;
38
38
use syntax:: ast;
39
39
use syntax_pos:: { Span , DUMMY_SP } ;
40
40
41
+ use std:: fmt;
41
42
use std:: ptr;
42
43
43
44
use super :: operand:: { OperandRef , OperandValue } ;
@@ -149,6 +150,12 @@ impl<'tcx> Const<'tcx> {
149
150
}
150
151
}
151
152
153
+ impl < ' tcx > fmt:: Debug for Const < ' tcx > {
154
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
155
+ write ! ( f, "Const({:?}: {:?})" , Value ( self . llval) , self . ty)
156
+ }
157
+ }
158
+
152
159
#[ derive( Copy , Clone ) ]
153
160
enum Base {
154
161
/// A constant value without an unique address.
@@ -472,7 +479,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
472
479
473
480
fn const_operand ( & self , operand : & mir:: Operand < ' tcx > , span : Span )
474
481
-> Result < Const < ' tcx > , ConstEvalErr > {
475
- match * operand {
482
+ debug ! ( "const_operand({:?} @ {:?})" , operand, span) ;
483
+ let result = match * operand {
476
484
mir:: Operand :: Consume ( ref lvalue) => {
477
485
Ok ( self . const_lvalue ( lvalue, span) ?. to_const ( span) )
478
486
}
@@ -501,29 +509,41 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
501
509
}
502
510
}
503
511
}
504
- }
512
+ } ;
513
+ debug ! ( "const_operand({:?} @ {:?}) = {:?}" , operand, span,
514
+ result. as_ref( ) . ok( ) ) ;
515
+ result
516
+ }
517
+
518
+ fn const_array ( & self , array_ty : Ty < ' tcx > , fields : & [ ValueRef ] )
519
+ -> Const < ' tcx >
520
+ {
521
+ let elem_ty = array_ty. builtin_index ( ) . unwrap_or_else ( || {
522
+ bug ! ( "bad array type {:?}" , array_ty)
523
+ } ) ;
524
+ let llunitty = type_of:: type_of ( self . ccx , elem_ty) ;
525
+ // If the array contains enums, an LLVM array won't work.
526
+ let val = if fields. iter ( ) . all ( |& f| val_ty ( f) == llunitty) {
527
+ C_array ( llunitty, fields)
528
+ } else {
529
+ C_struct ( self . ccx , fields, false )
530
+ } ;
531
+ Const :: new ( val, array_ty)
505
532
}
506
533
507
534
fn const_rvalue ( & self , rvalue : & mir:: Rvalue < ' tcx > ,
508
535
dest_ty : Ty < ' tcx > , span : Span )
509
536
-> Result < Const < ' tcx > , ConstEvalErr > {
510
537
let tcx = self . ccx . tcx ( ) ;
538
+ debug ! ( "const_rvalue({:?}: {:?} @ {:?})" , rvalue, dest_ty, span) ;
511
539
let val = match * rvalue {
512
540
mir:: Rvalue :: Use ( ref operand) => self . const_operand ( operand, span) ?,
513
541
514
542
mir:: Rvalue :: Repeat ( ref elem, ref count) => {
515
543
let elem = self . const_operand ( elem, span) ?;
516
544
let size = count. value . as_u64 ( tcx. sess . target . uint_type ) ;
517
545
let fields = vec ! [ elem. llval; size as usize ] ;
518
-
519
- let llunitty = type_of:: type_of ( self . ccx , elem. ty ) ;
520
- // If the array contains enums, an LLVM array won't work.
521
- let val = if val_ty ( elem. llval ) == llunitty {
522
- C_array ( llunitty, & fields)
523
- } else {
524
- C_struct ( self . ccx , & fields, false )
525
- } ;
526
- Const :: new ( val, dest_ty)
546
+ self . const_array ( dest_ty, & fields)
527
547
}
528
548
529
549
mir:: Rvalue :: Aggregate ( ref kind, ref operands) => {
@@ -547,22 +567,26 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
547
567
self . monomorphize ( & substs) ) ;
548
568
}
549
569
550
- let val = if let mir:: AggregateKind :: Adt ( adt_def, index, _, _) = * kind {
551
- let repr = adt:: represent_type ( self . ccx , dest_ty) ;
552
- let disr = Disr :: from ( adt_def. variants [ index] . disr_val ) ;
553
- adt:: trans_const ( self . ccx , & repr, disr, & fields)
554
- } else if let ty:: TyArray ( elem_ty, _) = dest_ty. sty {
555
- let llunitty = type_of:: type_of ( self . ccx , elem_ty) ;
556
- // If the array contains enums, an LLVM array won't work.
557
- if fields. iter ( ) . all ( |& f| val_ty ( f) == llunitty) {
558
- C_array ( llunitty, & fields)
559
- } else {
560
- C_struct ( self . ccx , & fields, false )
570
+ match * kind {
571
+ mir:: AggregateKind :: Vec => {
572
+ self . const_array ( dest_ty, & fields)
561
573
}
562
- } else {
563
- C_struct ( self . ccx , & fields, false )
564
- } ;
565
- Const :: new ( val, dest_ty)
574
+ mir:: AggregateKind :: Adt ( ..) |
575
+ mir:: AggregateKind :: Closure ( ..) |
576
+ mir:: AggregateKind :: Tuple => {
577
+ let disr = match * kind {
578
+ mir:: AggregateKind :: Adt ( adt_def, index, _, _) => {
579
+ Disr :: from ( adt_def. variants [ index] . disr_val )
580
+ }
581
+ _ => Disr ( 0 )
582
+ } ;
583
+ let repr = adt:: represent_type ( self . ccx , dest_ty) ;
584
+ Const :: new (
585
+ adt:: trans_const ( self . ccx , & repr, disr, & fields) ,
586
+ dest_ty
587
+ )
588
+ }
589
+ }
566
590
}
567
591
568
592
mir:: Rvalue :: Cast ( ref kind, ref source, cast_ty) => {
@@ -786,6 +810,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
786
810
_ => span_bug ! ( span, "{:?} in constant" , rvalue)
787
811
} ;
788
812
813
+ debug ! ( "const_rvalue({:?}: {:?} @ {:?}) = {:?}" , rvalue, dest_ty, span, val) ;
814
+
789
815
Ok ( val)
790
816
}
791
817
@@ -935,6 +961,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
935
961
constant : & mir:: Constant < ' tcx > )
936
962
-> Const < ' tcx >
937
963
{
964
+ debug ! ( "trans_constant({:?})" , constant) ;
938
965
let ty = bcx. monomorphize ( & constant. ty ) ;
939
966
let result = match constant. literal . clone ( ) {
940
967
mir:: Literal :: Item { def_id, substs } => {
@@ -959,11 +986,14 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
959
986
}
960
987
} ;
961
988
962
- result. unwrap_or_else ( |_| {
989
+ let result = result. unwrap_or_else ( |_| {
963
990
// We've errored, so we don't have to produce working code.
964
991
let llty = type_of:: type_of ( bcx. ccx ( ) , ty) ;
965
992
Const :: new ( C_undef ( llty) , ty)
966
- } )
993
+ } ) ;
994
+
995
+ debug ! ( "trans_constant({:?}) = {:?}" , constant, result) ;
996
+ result
967
997
}
968
998
}
969
999
0 commit comments