@@ -31,6 +31,7 @@ use trans::{adt, closure, debuginfo, expr, inline, machine};
3131use trans:: base:: { self , push_ctxt} ;
3232use trans:: common:: { self , type_is_sized, ExprOrMethodCall , node_id_substs, C_nil , const_get_elt} ;
3333use trans:: common:: { CrateContext , C_integral , C_floating , C_bool , C_str_slice , C_bytes , val_ty} ;
34+ use trans:: common:: C_floating_f64 ;
3435use trans:: common:: { C_struct , C_undef , const_to_opt_int, const_to_opt_uint, VariantInfo , C_uint } ;
3536use trans:: common:: { type_is_fat_ptr, Field , C_vector , C_array , C_null , ExprId , MethodCallKey } ;
3637use trans:: declare;
@@ -107,6 +108,39 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
107108 }
108109}
109110
111+ pub fn trans_constval < ' a , ' tcx > ( ccx : & CrateContext < ' a , ' tcx > ,
112+ cv : & ConstVal ,
113+ ty : Ty < ' tcx > ,
114+ param_substs : & ' tcx Substs < ' tcx > )
115+ -> ValueRef
116+ {
117+ let llty = type_of:: type_of ( ccx, ty) ;
118+ match * cv {
119+ ConstVal :: Float ( v) => C_floating_f64 ( v, llty) ,
120+ ConstVal :: Bool ( v) => C_bool ( ccx, v) ,
121+ ConstVal :: Int ( v) => C_integral ( llty, v as u64 , true ) ,
122+ ConstVal :: Uint ( v) => C_integral ( llty, v, false ) ,
123+ ConstVal :: Str ( ref v) => C_str_slice ( ccx, v. clone ( ) ) ,
124+ ConstVal :: ByteStr ( ref v) => addr_of ( ccx, C_bytes ( ccx, v) , 1 , "byte_str" ) ,
125+ ConstVal :: Struct ( id) | ConstVal :: Tuple ( id) => {
126+ let expr = ccx. tcx ( ) . map . expect_expr ( id) ;
127+ match const_expr ( ccx, expr, param_substs, None , TrueConst :: Yes ) {
128+ Ok ( ( val, _) ) => val,
129+ Err ( e) => panic ! ( "const eval failure: {}" , e. description( ) ) ,
130+ }
131+ } ,
132+ ConstVal :: Function ( _) => {
133+ unimplemented ! ( )
134+ } ,
135+ ConstVal :: Array ( ..) => {
136+ unimplemented ! ( )
137+ } ,
138+ ConstVal :: Repeat ( ..) => {
139+ unimplemented ! ( )
140+ } ,
141+ }
142+ }
143+
110144pub fn ptrcast ( val : ValueRef , ty : Type ) -> ValueRef {
111145 unsafe {
112146 llvm:: LLVMConstPointerCast ( val, ty. to_ref ( ) )
0 commit comments