1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use middle
::subst
::Substs
;
14 use middle
::ty
::{Ty, TypeFoldable}
;
15 use rustc
::middle
::const_eval
::ConstVal
;
16 use rustc
::mir
::repr
as mir
;
17 use trans
::common
::{self, BlockAndBuilder
, C_bool
, C_bytes
, C_floating_f64
, C_integral
,
23 use super::operand
::{OperandRef, OperandValue}
;
24 use super::MirContext
;
27 impl<'bcx
, 'tcx
> MirContext
<'bcx
, 'tcx
> {
28 pub fn trans_constval(&mut self,
29 bcx
: &BlockAndBuilder
<'bcx
, 'tcx
>,
35 let val
= self.trans_constval_inner(bcx
, cv
, ty
, bcx
.fcx().param_substs
);
36 let val
= if common
::type_is_immediate(ccx
, ty
) {
37 OperandValue
::Immediate(val
)
38 } else if common
::type_is_fat_ptr(bcx
.tcx(), ty
) {
39 let data
= common
::const_get_elt(ccx
, val
, &[abi
::FAT_PTR_ADDR
as u32]);
40 let extra
= common
::const_get_elt(ccx
, val
, &[abi
::FAT_PTR_EXTRA
as u32]);
41 OperandValue
::FatPtr(data
, extra
)
43 OperandValue
::Ref(val
)
46 assert
!(!ty
.has_erasable_regions());
54 /// Translate ConstVal into a bare LLVM ValueRef.
55 fn trans_constval_inner(&mut self,
56 bcx
: &BlockAndBuilder
<'bcx
, 'tcx
>,
59 param_substs
: &'tcx Substs
<'tcx
>)
63 let llty
= type_of
::type_of(ccx
, ty
);
65 ConstVal
::Float(v
) => C_floating_f64(v
, llty
),
66 ConstVal
::Bool(v
) => C_bool(ccx
, v
),
67 ConstVal
::Int(v
) => C_integral(llty
, v
as u64, true),
68 ConstVal
::Uint(v
) => C_integral(llty
, v
, false),
69 ConstVal
::Str(ref v
) => C_str_slice(ccx
, v
.clone()),
70 ConstVal
::ByteStr(ref v
) => consts
::addr_of(ccx
, C_bytes(ccx
, v
), 1, "byte_str"),
71 ConstVal
::Struct(id
) | ConstVal
::Tuple(id
) |
72 ConstVal
::Array(id
, _
) | ConstVal
::Repeat(id
, _
) => {
73 let expr
= bcx
.tcx().map
.expect_expr(id
);
74 bcx
.with_block(|bcx
| {
75 expr
::trans(bcx
, expr
).datum
.val
78 ConstVal
::Function(did
) =>
79 self.trans_fn_ref(bcx
, ty
, param_substs
, did
).immediate()
83 pub fn trans_constant(&mut self,
84 bcx
: &BlockAndBuilder
<'bcx
, 'tcx
>,
85 constant
: &mir
::Constant
<'tcx
>)
88 match constant
.literal
{
89 mir
::Literal
::Item { def_id, kind, substs }
=> {
90 let substs
= bcx
.tcx().mk_substs(bcx
.monomorphize(&substs
));
91 self.trans_item_ref(bcx
, constant
.ty
, kind
, substs
, def_id
)
93 mir
::Literal
::Value { ref value }
=> {
94 let ty
= bcx
.monomorphize(&constant
.ty
);
95 self.trans_constval(bcx
, value
, ty
)