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