]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
Rollup merge of #55343 - Keruspe:remap-debuginfo-release, r=alexcrichton
[rust.git] / src / librustc / mir / interpret / value.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(unknown_lints)]
12
13 use ty::layout::{HasDataLayout, Size};
14 use ty::subst::Substs;
15 use hir::def_id::DefId;
16
17 use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
18
19 /// Represents a constant value in Rust. Scalar and ScalarPair are optimizations which
20 /// matches the LocalValue optimizations for easy conversions between Value and ConstValue.
21 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash)]
22 pub enum ConstValue<'tcx> {
23     /// Never returned from the `const_eval` query, but the HIR contains these frequently in order
24     /// to allow HIR creation to happen for everything before needing to be able to run constant
25     /// evaluation
26     Unevaluated(DefId, &'tcx Substs<'tcx>),
27
28     /// Used only for types with layout::abi::Scalar ABI and ZSTs
29     ///
30     /// Not using the enum `Value` to encode that this must not be `Undef`
31     Scalar(Scalar),
32
33     /// Used only for *fat pointers* with layout::abi::ScalarPair
34     ///
35     /// Needed for pattern matching code related to slices and strings.
36     ScalarPair(Scalar, Scalar),
37
38     /// An allocation + offset into the allocation.
39     /// Invariant: The AllocId matches the allocation.
40     ByRef(AllocId, &'tcx Allocation, Size),
41 }
42
43 impl<'tcx> ConstValue<'tcx> {
44     #[inline]
45     pub fn try_to_scalar(&self) -> Option<Scalar> {
46         match *self {
47             ConstValue::Unevaluated(..) |
48             ConstValue::ByRef(..) |
49             ConstValue::ScalarPair(..) => None,
50             ConstValue::Scalar(val) => Some(val),
51         }
52     }
53
54     #[inline]
55     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
56         self.try_to_scalar()?.to_bits(size).ok()
57     }
58
59     #[inline]
60     pub fn try_to_ptr(&self) -> Option<Pointer> {
61         self.try_to_scalar()?.to_ptr().ok()
62     }
63
64     #[inline]
65     pub fn new_slice(
66         val: Scalar,
67         len: u64,
68         cx: impl HasDataLayout
69     ) -> Self {
70         ConstValue::ScalarPair(val, Scalar::Bits {
71             bits: len as u128,
72             size: cx.data_layout().pointer_size.bytes() as u8,
73         })
74     }
75
76     #[inline]
77     pub fn new_dyn_trait(val: Scalar, vtable: Pointer) -> Self {
78         ConstValue::ScalarPair(val, Scalar::Ptr(vtable))
79     }
80 }
81
82 /// A `Scalar` represents an immediate, primitive value existing outside of a
83 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
84 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
85 /// of a simple value or a pointer into another `Allocation`
86 #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
87 pub enum Scalar<Tag=(), Id=AllocId> {
88     /// The raw bytes of a simple value.
89     Bits {
90         /// The first `size` bytes are the value.
91         /// Do not try to read less or more bytes that that. The remaining bytes must be 0.
92         size: u8,
93         bits: u128,
94     },
95
96     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
97     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
98     /// relocation and its associated offset together as a `Pointer` here.
99     Ptr(Pointer<Tag, Id>),
100 }
101
102 impl<'tcx> Scalar<()> {
103     #[inline]
104     pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
105         where Tag: Default
106     {
107         match self {
108             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_default_tag()),
109             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
110         }
111     }
112 }
113
114 impl<'tcx, Tag> Scalar<Tag> {
115     #[inline]
116     pub fn erase_tag(self) -> Scalar {
117         match self {
118             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
119             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
120         }
121     }
122
123     #[inline]
124     pub fn ptr_null(cx: impl HasDataLayout) -> Self {
125         Scalar::Bits {
126             bits: 0,
127             size: cx.data_layout().pointer_size.bytes() as u8,
128         }
129     }
130
131     #[inline]
132     pub fn zst() -> Self {
133         Scalar::Bits { bits: 0, size: 0 }
134     }
135
136     #[inline]
137     pub fn ptr_signed_offset(self, i: i64, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
138         let layout = cx.data_layout();
139         match self {
140             Scalar::Bits { bits, size } => {
141                 assert_eq!(size as u64, layout.pointer_size.bytes());
142                 Ok(Scalar::Bits {
143                     bits: layout.signed_offset(bits as u64, i)? as u128,
144                     size,
145                 })
146             }
147             Scalar::Ptr(ptr) => ptr.signed_offset(i, layout).map(Scalar::Ptr),
148         }
149     }
150
151     #[inline]
152     pub fn ptr_offset(self, i: Size, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
153         let layout = cx.data_layout();
154         match self {
155             Scalar::Bits { bits, size } => {
156                 assert_eq!(size as u64, layout.pointer_size.bytes());
157                 Ok(Scalar::Bits {
158                     bits: layout.offset(bits as u64, i.bytes())? as u128,
159                     size,
160                 })
161             }
162             Scalar::Ptr(ptr) => ptr.offset(i, layout).map(Scalar::Ptr),
163         }
164     }
165
166     #[inline]
167     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: impl HasDataLayout) -> Self {
168         let layout = cx.data_layout();
169         match self {
170             Scalar::Bits { bits, size } => {
171                 assert_eq!(size as u64, layout.pointer_size.bytes());
172                 Scalar::Bits {
173                     bits: layout.wrapping_signed_offset(bits as u64, i) as u128,
174                     size,
175                 }
176             }
177             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, layout)),
178         }
179     }
180
181     #[inline]
182     pub fn is_null_ptr(self, cx: impl HasDataLayout) -> bool {
183         match self {
184             Scalar::Bits { bits, size } => {
185                 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
186                 bits == 0
187             },
188             Scalar::Ptr(_) => false,
189         }
190     }
191
192     #[inline]
193     pub fn from_bool(b: bool) -> Self {
194         Scalar::Bits { bits: b as u128, size: 1 }
195     }
196
197     #[inline]
198     pub fn from_char(c: char) -> Self {
199         Scalar::Bits { bits: c as u128, size: 4 }
200     }
201
202     #[inline]
203     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
204         let i = i.into();
205         debug_assert_eq!(truncate(i, size), i,
206                          "Unsigned value {} does not fit in {} bits", i, size.bits());
207         Scalar::Bits { bits: i, size: size.bytes() as u8 }
208     }
209
210     #[inline]
211     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
212         let i = i.into();
213         // `into` performed sign extension, we have to truncate
214         let truncated = truncate(i as u128, size);
215         debug_assert_eq!(sign_extend(truncated, size) as i128, i,
216                          "Signed value {} does not fit in {} bits", i, size.bits());
217         Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
218     }
219
220     #[inline]
221     pub fn from_f32(f: f32) -> Self {
222         Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
223     }
224
225     #[inline]
226     pub fn from_f64(f: f64) -> Self {
227         Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
228     }
229
230     #[inline]
231     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
232         match self {
233             Scalar::Bits { bits, size } => {
234                 assert_eq!(target_size.bytes(), size as u64);
235                 assert_ne!(size, 0, "to_bits cannot be used with zsts");
236                 Ok(bits)
237             }
238             Scalar::Ptr(_) => err!(ReadPointerAsBytes),
239         }
240     }
241
242     #[inline]
243     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
244         match self {
245             Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
246             Scalar::Bits { .. } => err!(ReadBytesAsPointer),
247             Scalar::Ptr(p) => Ok(p),
248         }
249     }
250
251     #[inline]
252     pub fn is_bits(self) -> bool {
253         match self {
254             Scalar::Bits { .. } => true,
255             _ => false,
256         }
257     }
258
259     #[inline]
260     pub fn is_ptr(self) -> bool {
261         match self {
262             Scalar::Ptr(_) => true,
263             _ => false,
264         }
265     }
266
267     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
268         match self {
269             Scalar::Bits { bits: 0, size: 1 } => Ok(false),
270             Scalar::Bits { bits: 1, size: 1 } => Ok(true),
271             _ => err!(InvalidBool),
272         }
273     }
274
275     pub fn to_char(self) -> EvalResult<'tcx, char> {
276         let val = self.to_u32()?;
277         match ::std::char::from_u32(val) {
278             Some(c) => Ok(c),
279             None => err!(InvalidChar(val as u128)),
280         }
281     }
282
283     pub fn to_u8(self) -> EvalResult<'static, u8> {
284         let sz = Size::from_bits(8);
285         let b = self.to_bits(sz)?;
286         assert_eq!(b as u8 as u128, b);
287         Ok(b as u8)
288     }
289
290     pub fn to_u32(self) -> EvalResult<'static, u32> {
291         let sz = Size::from_bits(32);
292         let b = self.to_bits(sz)?;
293         assert_eq!(b as u32 as u128, b);
294         Ok(b as u32)
295     }
296
297     pub fn to_u64(self) -> EvalResult<'static, u64> {
298         let sz = Size::from_bits(64);
299         let b = self.to_bits(sz)?;
300         assert_eq!(b as u64 as u128, b);
301         Ok(b as u64)
302     }
303
304     pub fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64> {
305         let b = self.to_bits(cx.data_layout().pointer_size)?;
306         assert_eq!(b as u64 as u128, b);
307         Ok(b as u64)
308     }
309
310     pub fn to_i8(self) -> EvalResult<'static, i8> {
311         let sz = Size::from_bits(8);
312         let b = self.to_bits(sz)?;
313         let b = sign_extend(b, sz) as i128;
314         assert_eq!(b as i8 as i128, b);
315         Ok(b as i8)
316     }
317
318     pub fn to_i32(self) -> EvalResult<'static, i32> {
319         let sz = Size::from_bits(32);
320         let b = self.to_bits(sz)?;
321         let b = sign_extend(b, sz) as i128;
322         assert_eq!(b as i32 as i128, b);
323         Ok(b as i32)
324     }
325
326     pub fn to_i64(self) -> EvalResult<'static, i64> {
327         let sz = Size::from_bits(64);
328         let b = self.to_bits(sz)?;
329         let b = sign_extend(b, sz) as i128;
330         assert_eq!(b as i64 as i128, b);
331         Ok(b as i64)
332     }
333
334     pub fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64> {
335         let b = self.to_bits(cx.data_layout().pointer_size)?;
336         let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
337         assert_eq!(b as i64 as i128, b);
338         Ok(b as i64)
339     }
340
341     #[inline]
342     pub fn to_f32(self) -> EvalResult<'static, f32> {
343         Ok(f32::from_bits(self.to_u32()?))
344     }
345
346     #[inline]
347     pub fn to_f64(self) -> EvalResult<'static, f64> {
348         Ok(f64::from_bits(self.to_u64()?))
349     }
350 }
351
352 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
353     #[inline(always)]
354     fn from(ptr: Pointer<Tag>) -> Self {
355         Scalar::Ptr(ptr)
356     }
357 }