]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/mir/constant.rs
Rollup merge of #30774 - nagisa:mir-fix-constval-adts, r=arielb1
[rust.git] / src / librustc_trans / trans / mir / constant.rs
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.
4 //
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.
10
11 use back::abi;
12 use llvm::ValueRef;
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};
18 use trans::consts;
19 use trans::expr;
20 use trans::type_of;
21
22 use super::operand::{OperandRef, OperandValue};
23 use super::MirContext;
24
25
26 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
27     pub fn trans_constval(&mut self,
28                           bcx: Block<'bcx, 'tcx>,
29                           cv: &ConstVal,
30                           ty: Ty<'tcx>)
31                           -> OperandRef<'tcx>
32     {
33         let ccx = bcx.ccx();
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)
41         } else {
42             OperandValue::Ref(val)
43         };
44
45         assert!(!ty.has_erasable_regions());
46
47         OperandRef {
48             ty: ty,
49             val: val
50         }
51     }
52
53     /// Translate ConstVal into a bare LLVM ValueRef.
54     fn trans_constval_inner(&mut self,
55                             bcx: common::Block<'bcx, 'tcx>,
56                             cv: &ConstVal,
57                             ty: Ty<'tcx>,
58                             param_substs: &'tcx Substs<'tcx>)
59                             -> ValueRef
60     {
61         let ccx = bcx.ccx();
62         let llty = type_of::type_of(ccx, ty);
63         match *cv {
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
74             },
75             ConstVal::Function(did) =>
76                 self.trans_fn_ref(bcx, ty, param_substs, did).immediate()
77         }
78     }
79
80     pub fn trans_constant(&mut self,
81                           bcx: Block<'bcx, 'tcx>,
82                           constant: &mir::Constant<'tcx>)
83                           -> OperandRef<'tcx>
84     {
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)
90             }
91             mir::Literal::Value { ref value } => {
92                 self.trans_constval(bcx, value, ty)
93             }
94         }
95     }
96 }