]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/constant.rs
Remove unnecessary lift calls
[rust.git] / src / librustc_mir / hair / constant.rs
1 use syntax::ast;
2 use rustc::ty::{self, Ty, TyCtxt, ParamEnv, layout::Size};
3 use syntax_pos::symbol::Symbol;
4 use rustc::mir::interpret::{ConstValue, Scalar};
5
6 #[derive(PartialEq)]
7 crate enum LitToConstError {
8     UnparseableFloat,
9     Reported,
10 }
11
12 crate fn lit_to_const<'tcx>(
13     lit: &'tcx ast::LitKind,
14     tcx: TyCtxt<'tcx>,
15     ty: Ty<'tcx>,
16     neg: bool,
17 ) -> Result<&'tcx ty::Const<'tcx>, LitToConstError> {
18     use syntax::ast::*;
19
20     let trunc = |n| {
21         let param_ty = ParamEnv::reveal_all().and(ty);
22         let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
23         trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
24         let result = truncate(n, width);
25         trace!("trunc result: {}", result);
26         Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
27     };
28
29     use rustc::mir::interpret::*;
30     let lit = match *lit {
31         LitKind::Str(ref s, _) => {
32             let s = s.as_str();
33             let allocation = Allocation::from_byte_aligned_bytes(s.as_bytes());
34             let allocation = tcx.intern_const_alloc(allocation);
35             ConstValue::Slice { data: allocation, start: 0, end: s.len() }
36         },
37         LitKind::ByteStr(ref data) => {
38             let id = tcx.allocate_bytes(data);
39             ConstValue::Scalar(Scalar::Ptr(id.into()))
40         },
41         LitKind::Byte(n) => ConstValue::Scalar(Scalar::from_uint(n, Size::from_bytes(1))),
42         LitKind::Int(n, _) if neg => {
43             let n = n as i128;
44             let n = n.overflowing_neg().0;
45             trunc(n as u128)?
46         },
47         LitKind::Int(n, _) => trunc(n)?,
48         LitKind::Float(n, fty) => {
49             parse_float(n, fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
50         }
51         LitKind::FloatUnsuffixed(n) => {
52             let fty = match ty.sty {
53                 ty::Float(fty) => fty,
54                 _ => bug!()
55             };
56             parse_float(n, fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
57         }
58         LitKind::Bool(b) => ConstValue::Scalar(Scalar::from_bool(b)),
59         LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)),
60         LitKind::Err(_) => unreachable!(),
61     };
62     Ok(tcx.mk_const(ty::Const { val: lit, ty }))
63 }
64
65 fn parse_float<'tcx>(
66     num: Symbol,
67     fty: ast::FloatTy,
68     neg: bool,
69 ) -> Result<ConstValue<'tcx>, ()> {
70     let num = num.as_str();
71     use rustc_apfloat::ieee::{Single, Double};
72     let scalar = match fty {
73         ast::FloatTy::F32 => {
74             num.parse::<f32>().map_err(|_| ())?;
75             let mut f = num.parse::<Single>().unwrap_or_else(|e| {
76                 panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
77             });
78             if neg {
79                 f = -f;
80             }
81             Scalar::from_f32(f)
82         }
83         ast::FloatTy::F64 => {
84             num.parse::<f64>().map_err(|_| ())?;
85             let mut f = num.parse::<Double>().unwrap_or_else(|e| {
86                 panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
87             });
88             if neg {
89                 f = -f;
90             }
91             Scalar::from_f64(f)
92         }
93     };
94
95     Ok(ConstValue::Scalar(scalar))
96 }