]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/constant.rs
Rollup merge of #61389 - Zoxc:arena-cleanup, r=eddyb
[rust.git] / src / librustc_codegen_ssa / mir / constant.rs
1 use rustc::mir::interpret::ErrorHandled;
2 use rustc_mir::const_eval::const_field;
3 use rustc::mir;
4 use rustc_data_structures::indexed_vec::Idx;
5 use rustc::ty::{self, Ty};
6 use rustc::ty::layout::{self, HasTyCtxt};
7 use syntax::source_map::Span;
8 use crate::traits::*;
9
10 use super::FunctionCx;
11
12 impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13     pub fn eval_mir_constant(
14         &mut self,
15         constant: &mir::Constant<'tcx>,
16     ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
17         match constant.literal.val {
18             mir::interpret::ConstValue::Unevaluated(def_id, ref substs) => {
19                 let substs = self.monomorphize(substs);
20                 let instance = ty::Instance::resolve(
21                     self.cx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs,
22                 ).unwrap();
23                 let cid = mir::interpret::GlobalId {
24                     instance,
25                     promoted: None,
26                 };
27                 self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid))
28             },
29             _ => Ok(self.monomorphize(&constant.literal)),
30         }
31     }
32
33     /// process constant containing SIMD shuffle indices
34     pub fn simd_shuffle_indices(
35         &mut self,
36         bx: &Bx,
37         span: Span,
38         ty: Ty<'tcx>,
39         constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
40     ) -> (Bx::Value, Ty<'tcx>) {
41         constant
42             .map(|c| {
43                 let field_ty = c.ty.builtin_index().unwrap();
44                 let fields = match c.ty.sty {
45                     ty::Array(_, n) => n.unwrap_usize(bx.tcx()),
46                     _ => bug!("invalid simd shuffle type: {}", c.ty),
47                 };
48                 let values: Vec<_> = (0..fields).map(|field| {
49                     let field = const_field(
50                         bx.tcx(),
51                         ty::ParamEnv::reveal_all(),
52                         None,
53                         mir::Field::new(field as usize),
54                         c,
55                     );
56                     if let Some(prim) = field.val.try_to_scalar() {
57                         let layout = bx.layout_of(field_ty);
58                         let scalar = match layout.abi {
59                             layout::Abi::Scalar(ref x) => x,
60                             _ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
61                         };
62                         bx.scalar_to_backend(
63                             prim, scalar,
64                             bx.immediate_backend_type(layout),
65                         )
66                     } else {
67                         bug!("simd shuffle field {:?}", field)
68                     }
69                 }).collect();
70                 let llval = bx.const_struct(&values, false);
71                 (llval, c.ty)
72             })
73             .unwrap_or_else(|_| {
74                 bx.tcx().sess.span_err(
75                     span,
76                     "could not evaluate shuffle_indices at compile time",
77                 );
78                 // We've errored, so we don't have to produce working code.
79                 let ty = self.monomorphize(&ty);
80                 let llty = bx.backend_type(bx.layout_of(ty));
81                 (bx.const_undef(llty), ty)
82             })
83     }
84 }