99// except according to those terms.
1010
1111use back:: abi;
12+ use llvm:: ValueRef ;
13+ use middle:: subst:: Substs ;
1214use middle:: ty:: { Ty , HasTypeFlags } ;
1315use rustc:: middle:: const_eval:: ConstVal ;
1416use rustc:: mir:: repr as mir;
15- use trans:: consts;
16- use trans:: common:: { self , Block } ;
17+ use trans:: common:: { self , Block , C_bool , C_bytes , C_floating_f64 , C_integral , C_str_slice } ;
18+ use trans:: consts:: { self , TrueConst } ;
19+ use trans:: { type_of, expr} ;
1720
1821
1922use super :: operand:: { OperandRef , OperandValue } ;
@@ -27,7 +30,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
2730 -> OperandRef < ' tcx >
2831 {
2932 let ccx = bcx. ccx ( ) ;
30- let val = consts :: trans_constval ( bcx, cv, ty, bcx. fcx . param_substs ) ;
33+ let val = self . trans_constval_inner ( bcx, cv, ty, bcx. fcx . param_substs ) ;
3134 let val = if common:: type_is_immediate ( ccx, ty) {
3235 OperandValue :: Immediate ( val)
3336 } else if common:: type_is_fat_ptr ( bcx. tcx ( ) , ty) {
@@ -46,6 +49,39 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
4649 }
4750 }
4851
52+ /// Translate ConstVal into a bare LLVM ValueRef.
53+ fn trans_constval_inner ( & mut self ,
54+ bcx : common:: Block < ' bcx , ' tcx > ,
55+ cv : & ConstVal ,
56+ ty : Ty < ' tcx > ,
57+ param_substs : & ' tcx Substs < ' tcx > )
58+ -> ValueRef
59+ {
60+ let ccx = bcx. ccx ( ) ;
61+ let llty = type_of:: type_of ( ccx, ty) ;
62+ match * cv {
63+ ConstVal :: Float ( v) => C_floating_f64 ( v, llty) ,
64+ ConstVal :: Bool ( v) => C_bool ( ccx, v) ,
65+ ConstVal :: Int ( v) => C_integral ( llty, v as u64 , true ) ,
66+ ConstVal :: Uint ( v) => C_integral ( llty, v, false ) ,
67+ ConstVal :: Str ( ref v) => C_str_slice ( ccx, v. clone ( ) ) ,
68+ ConstVal :: ByteStr ( ref v) => consts:: addr_of ( ccx, C_bytes ( ccx, v) , 1 , "byte_str" ) ,
69+ ConstVal :: Struct ( id) | ConstVal :: Tuple ( id) => {
70+ let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
71+ match consts:: const_expr ( ccx, expr, param_substs, None , TrueConst :: Yes ) {
72+ Ok ( ( val, _) ) => val,
73+ Err ( e) => panic ! ( "const eval failure: {}" , e. description( ) ) ,
74+ }
75+ } ,
76+ ConstVal :: Array ( id, _) | ConstVal :: Repeat ( id, _) => {
77+ let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
78+ expr:: trans ( bcx, expr) . datum . val
79+ } ,
80+ ConstVal :: Function ( did) =>
81+ self . trans_fn_ref ( bcx, ty, param_substs, did) . immediate ( )
82+ }
83+ }
84+
4985 pub fn trans_constant ( & mut self ,
5086 bcx : Block < ' bcx , ' tcx > ,
5187 constant : & mir:: Constant < ' tcx > )
0 commit comments