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, Block, C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice};
22 use super::operand::{OperandRef, OperandValue};
23 use super::MirContext;
26 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
27 pub fn trans_constval(&mut self,
28 bcx: Block<'bcx, 'tcx>,
34 let val = self.trans_constval_inner(bcx, cv, ty, bcx.fcx.param_substs);
35 let val = if common::type_is_immediate(ccx, ty) {
36 OperandValue::Immediate(val)
37 } else if common::type_is_fat_ptr(bcx.tcx(), ty) {
38 let data = common::const_get_elt(ccx, val, &[abi::FAT_PTR_ADDR as u32]);
39 let extra = common::const_get_elt(ccx, val, &[abi::FAT_PTR_EXTRA as u32]);
40 OperandValue::FatPtr(data, extra)
42 OperandValue::Ref(val)
45 assert!(!ty.has_erasable_regions());
53 /// Translate ConstVal into a bare LLVM ValueRef.
54 fn trans_constval_inner(&mut self,
55 bcx: common::Block<'bcx, 'tcx>,
58 param_substs: &'tcx Substs<'tcx>)
62 let llty = type_of::type_of(ccx, ty);
64 ConstVal::Float(v) => C_floating_f64(v, llty),
65 ConstVal::Bool(v) => C_bool(ccx, v),
66 ConstVal::Int(v) => C_integral(llty, v as u64, true),
67 ConstVal::Uint(v) => C_integral(llty, v, false),
68 ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
69 ConstVal::ByteStr(ref v) => consts::addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
70 ConstVal::Struct(id) | ConstVal::Tuple(id) |
71 ConstVal::Array(id, _) | ConstVal::Repeat(id, _) => {
72 let expr = bcx.tcx().map.expect_expr(id);
73 expr::trans(bcx, expr).datum.val
75 ConstVal::Function(did) =>
76 self.trans_fn_ref(bcx, ty, param_substs, did).immediate()
80 pub fn trans_constant(&mut self,
81 bcx: Block<'bcx, 'tcx>,
82 constant: &mir::Constant<'tcx>)
85 let ty = bcx.monomorphize(&constant.ty);
86 match constant.literal {
87 mir::Literal::Item { def_id, kind, substs } => {
88 let substs = bcx.tcx().mk_substs(bcx.monomorphize(&substs));
89 self.trans_item_ref(bcx, ty, kind, substs, def_id)
91 mir::Literal::Value { ref value } => {
92 self.trans_constval(bcx, value, ty)