]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/constant.rs
Add simpler entry points to const eval for common usages.
[rust.git] / src / librustc_codegen_ssa / mir / constant.rs
1 use rustc::mir::interpret::ErrorHandled;
2 use rustc::mir;
3 use rustc_index::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 use crate::mir::operand::OperandRef;
9
10 use super::FunctionCx;
11
12 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13     pub fn eval_mir_constant_to_operand(
14         &mut self,
15         bx: &mut Bx,
16         constant: &mir::Constant<'tcx>,
17     ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
18         match constant.literal.val {
19             // Special case unevaluated statics, because statics have an identity and thus should
20             // use `get_static` to get at their id.
21             // FIXME(oli-obk): can we unify this somehow, maybe by making const eval of statics
22             // always produce `&STATIC`. This may also simplify how const eval works with statics.
23             ty::ConstKind::Unevaluated(def_id, substs)
24                 if self.cx.tcx().is_static(def_id) => {
25                     assert!(substs.is_empty(), "we don't support generic statics yet");
26                     let static_ = bx.get_static(def_id);
27                     // we treat operands referring to statics as if they were `&STATIC` instead
28                     let ptr_ty = self.cx.tcx().mk_mut_ptr(self.monomorphize(&constant.literal.ty));
29                     let layout = bx.layout_of(ptr_ty);
30                     Ok(OperandRef::from_immediate_or_packed_pair(bx, static_, layout))
31                 }
32             _ => {
33                 let val = self.eval_mir_constant(constant)?;
34                 Ok(OperandRef::from_const(bx, val))
35             }
36         }
37     }
38
39     pub fn eval_mir_constant(
40         &mut self,
41         constant: &mir::Constant<'tcx>,
42     ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
43         match constant.literal.val {
44             ty::ConstKind::Unevaluated(def_id, substs) => {
45                 let substs = self.monomorphize(&substs);
46                 self.cx.tcx()
47                     .const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, None)
48                     .map_err(|err| {
49                         self.cx.tcx().sess.span_err(
50                             constant.span,
51                             "erroneous constant encountered");
52                         err
53                     })
54             },
55             _ => Ok(self.monomorphize(&constant.literal)),
56         }
57     }
58
59     /// process constant containing SIMD shuffle indices
60     pub fn simd_shuffle_indices(
61         &mut self,
62         bx: &Bx,
63         span: Span,
64         ty: Ty<'tcx>,
65         constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
66     ) -> (Bx::Value, Ty<'tcx>) {
67         constant
68             .map(|c| {
69                 let field_ty = c.ty.builtin_index().unwrap();
70                 let fields = match c.ty.kind {
71                     ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
72                     _ => bug!("invalid simd shuffle type: {}", c.ty),
73                 };
74                 let values: Vec<_> = (0..fields).map(|field| {
75                     let field = bx.tcx().const_field(
76                         ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize)))
77                     );
78                     if let Some(prim) = field.val.try_to_scalar() {
79                         let layout = bx.layout_of(field_ty);
80                         let scalar = match layout.abi {
81                             layout::Abi::Scalar(ref x) => x,
82                             _ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
83                         };
84                         bx.scalar_to_backend(
85                             prim, scalar,
86                             bx.immediate_backend_type(layout),
87                         )
88                     } else {
89                         bug!("simd shuffle field {:?}", field)
90                     }
91                 }).collect();
92                 let llval = bx.const_struct(&values, false);
93                 (llval, c.ty)
94             })
95             .unwrap_or_else(|_| {
96                 bx.tcx().sess.span_err(
97                     span,
98                     "could not evaluate shuffle_indices at compile time",
99                 );
100                 // We've errored, so we don't have to produce working code.
101                 let ty = self.monomorphize(&ty);
102                 let llty = bx.backend_type(bx.layout_of(ty));
103                 (bx.const_undef(llty), ty)
104             })
105     }
106 }