9
9
// except according to those terms.
10
10
11
11
use llvm:: ValueRef ;
12
+ use middle:: ty:: Ty ;
12
13
use rustc:: middle:: const_eval:: ConstVal ;
13
14
use rustc_mir:: repr as mir;
14
15
use trans:: consts:: { self , TrueConst } ;
@@ -18,45 +19,54 @@ use trans::type_of;
18
19
use super :: MirContext ;
19
20
20
21
impl < ' bcx , ' tcx > MirContext < ' bcx , ' tcx > {
22
+ pub fn trans_constval ( & mut self ,
23
+ bcx : Block < ' bcx , ' tcx > ,
24
+ cv : & ConstVal ,
25
+ ty : Ty < ' tcx > )
26
+ -> ValueRef
27
+ {
28
+ let ccx = bcx. ccx ( ) ;
29
+ let llty = type_of:: type_of ( ccx, ty) ;
30
+ match * cv {
31
+ ConstVal :: Float ( v) => common:: C_floating_f64 ( v, llty) ,
32
+ ConstVal :: Bool ( v) => common:: C_bool ( ccx, v) ,
33
+ ConstVal :: Int ( v) => common:: C_integral ( llty, v as u64 , true ) ,
34
+ ConstVal :: Uint ( v) => common:: C_integral ( llty, v, false ) ,
35
+ ConstVal :: Str ( ref v) => common:: C_str_slice ( ccx, v. clone ( ) ) ,
36
+ ConstVal :: ByteStr ( ref v) => consts:: addr_of ( ccx,
37
+ common:: C_bytes ( ccx, v) ,
38
+ 1 ,
39
+ "byte_str" ) ,
40
+ ConstVal :: Struct ( id) | ConstVal :: Tuple ( id) => {
41
+ let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
42
+ let ( llval, _) = match consts:: const_expr ( ccx,
43
+ expr,
44
+ bcx. fcx . param_substs ,
45
+ None ,
46
+ TrueConst :: Yes ) {
47
+ Ok ( v) => v,
48
+ Err ( _) => panic ! ( "constant eval failure" ) ,
49
+ } ;
50
+ llval
51
+ }
52
+ ConstVal :: Function ( _) => {
53
+ unimplemented ! ( )
54
+ }
55
+ }
56
+ }
57
+
21
58
pub fn trans_constant ( & mut self ,
22
59
bcx : Block < ' bcx , ' tcx > ,
23
60
constant : & mir:: Constant < ' tcx > )
24
61
-> ValueRef
25
62
{
26
- let ccx = bcx. ccx ( ) ;
27
63
let constant_ty = bcx. monomorphize ( & constant. ty ) ;
28
- let llty = type_of:: type_of ( ccx, constant_ty) ;
29
64
match constant. literal {
30
65
mir:: Literal :: Item { .. } => {
31
66
unimplemented ! ( )
32
67
}
33
68
mir:: Literal :: Value { ref value } => {
34
- match * value {
35
- ConstVal :: Float ( v) => common:: C_floating_f64 ( v, llty) ,
36
- ConstVal :: Bool ( v) => common:: C_bool ( ccx, v) ,
37
- ConstVal :: Int ( v) => common:: C_integral ( llty, v as u64 , true ) ,
38
- ConstVal :: Uint ( v) => common:: C_integral ( llty, v, false ) ,
39
- ConstVal :: Str ( ref v) => common:: C_str_slice ( ccx, v. clone ( ) ) ,
40
- ConstVal :: ByteStr ( ref v) => consts:: addr_of ( ccx,
41
- common:: C_bytes ( ccx, v) ,
42
- 1 ,
43
- "byte_str" ) ,
44
- ConstVal :: Struct ( id) | ConstVal :: Tuple ( id) => {
45
- let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
46
- let ( llval, _) = match consts:: const_expr ( ccx,
47
- expr,
48
- bcx. fcx . param_substs ,
49
- None ,
50
- TrueConst :: Yes ) {
51
- Ok ( v) => v,
52
- Err ( _) => panic ! ( "constant eval failure" ) ,
53
- } ;
54
- llval
55
- }
56
- ConstVal :: Function ( _) => {
57
- unimplemented ! ( )
58
- }
59
- }
69
+ self . trans_constval ( bcx, value, constant_ty)
60
70
}
61
71
}
62
72
}
0 commit comments