]>
Commit | Line | Data |
---|---|---|
ba9703b0 XL |
1 | use rustc_ast::ast; |
2 | use rustc_middle::mir::interpret::{ | |
dfeec247 XL |
3 | truncate, Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar, |
4 | }; | |
ba9703b0 | 5 | use rustc_middle::ty::{self, ParamEnv, TyCtxt, TyS}; |
dfeec247 | 6 | use rustc_span::symbol::Symbol; |
ba9703b0 | 7 | use rustc_target::abi::Size; |
a1dfa0c6 | 8 | |
dc9dc135 | 9 | crate fn lit_to_const<'tcx>( |
dc9dc135 | 10 | tcx: TyCtxt<'tcx>, |
dfeec247 | 11 | lit_input: LitToConstInput<'tcx>, |
dc9dc135 | 12 | ) -> Result<&'tcx ty::Const<'tcx>, LitToConstError> { |
dfeec247 | 13 | let LitToConstInput { lit, ty, neg } = lit_input; |
a1dfa0c6 XL |
14 | |
15 | let trunc = |n| { | |
dc9dc135 | 16 | let param_ty = ParamEnv::reveal_all().and(ty); |
a1dfa0c6 XL |
17 | let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size; |
18 | trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); | |
9fa01778 | 19 | let result = truncate(n, width); |
a1dfa0c6 | 20 | trace!("trunc result: {}", result); |
dc9dc135 | 21 | Ok(ConstValue::Scalar(Scalar::from_uint(result, width))) |
a1dfa0c6 XL |
22 | }; |
23 | ||
dfeec247 XL |
24 | let lit = match (lit, &ty.kind) { |
25 | (ast::LitKind::Str(s, _), ty::Ref(_, TyS { kind: ty::Str, .. }, _)) => { | |
a1dfa0c6 | 26 | let s = s.as_str(); |
dc9dc135 XL |
27 | let allocation = Allocation::from_byte_aligned_bytes(s.as_bytes()); |
28 | let allocation = tcx.intern_const_alloc(allocation); | |
29 | ConstValue::Slice { data: allocation, start: 0, end: s.len() } | |
dfeec247 XL |
30 | } |
31 | (ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Slice(_), .. }, _)) => { | |
32 | let allocation = Allocation::from_byte_aligned_bytes(data as &Vec<u8>); | |
33 | let allocation = tcx.intern_const_alloc(allocation); | |
34 | ConstValue::Slice { data: allocation, start: 0, end: data.len() } | |
35 | } | |
36 | (ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Array(_, _), .. }, _)) => { | |
a1dfa0c6 XL |
37 | let id = tcx.allocate_bytes(data); |
38 | ConstValue::Scalar(Scalar::Ptr(id.into())) | |
a1dfa0c6 | 39 | } |
dfeec247 XL |
40 | (ast::LitKind::Byte(n), ty::Uint(ast::UintTy::U8)) => { |
41 | ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1))) | |
42 | } | |
43 | (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { | |
44 | trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })? | |
45 | } | |
46 | (ast::LitKind::Float(n, _), ty::Float(fty)) => { | |
47 | parse_float(*n, *fty, neg).map_err(|_| LitToConstError::UnparseableFloat)? | |
48 | } | |
49 | (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)), | |
50 | (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)), | |
51 | (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported), | |
52 | _ => return Err(LitToConstError::TypeError), | |
a1dfa0c6 | 53 | }; |
74b04a01 | 54 | Ok(ty::Const::from_value(tcx, lit, ty)) |
a1dfa0c6 XL |
55 | } |
56 | ||
dfeec247 | 57 | fn parse_float<'tcx>(num: Symbol, fty: ast::FloatTy, neg: bool) -> Result<ConstValue<'tcx>, ()> { |
a1dfa0c6 | 58 | let num = num.as_str(); |
dfeec247 | 59 | use rustc_apfloat::ieee::{Double, Single}; |
dc9dc135 | 60 | let scalar = match fty { |
a1dfa0c6 XL |
61 | ast::FloatTy::F32 => { |
62 | num.parse::<f32>().map_err(|_| ())?; | |
63 | let mut f = num.parse::<Single>().unwrap_or_else(|e| { | |
64 | panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e) | |
65 | }); | |
66 | if neg { | |
67 | f = -f; | |
68 | } | |
dc9dc135 | 69 | Scalar::from_f32(f) |
a1dfa0c6 XL |
70 | } |
71 | ast::FloatTy::F64 => { | |
72 | num.parse::<f64>().map_err(|_| ())?; | |
73 | let mut f = num.parse::<Double>().unwrap_or_else(|e| { | |
dc9dc135 | 74 | panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e) |
a1dfa0c6 XL |
75 | }); |
76 | if neg { | |
77 | f = -f; | |
78 | } | |
dc9dc135 | 79 | Scalar::from_f64(f) |
a1dfa0c6 XL |
80 | } |
81 | }; | |
82 | ||
dc9dc135 | 83 | Ok(ConstValue::Scalar(scalar)) |
a1dfa0c6 | 84 | } |