]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
Auto merge of #53830 - davidtwco:issue-53228, r=nikomatsakis
[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     /// Used only for types with layout::abi::Scalar ABI and ZSTs
28     ///
29     /// Not using the enum `Value` to encode that this must not be `Undef`
30     Scalar(Scalar),
31     /// Used only for types with layout::abi::ScalarPair
32     ///
33     /// The second field may be undef in case of `Option<usize>::None`
34     ScalarPair(Scalar, ScalarMaybeUndef),
35     /// Used only for the remaining cases. An allocation + offset into the allocation.
36     /// Invariant: The AllocId matches the allocation.
37     ByRef(AllocId, &'tcx Allocation, Size),
38 }
39
40 impl<'tcx> ConstValue<'tcx> {
41     #[inline]
42     pub fn try_to_scalar(&self) -> Option<Scalar> {
43         match *self {
44             ConstValue::Unevaluated(..) |
45             ConstValue::ByRef(..) |
46             ConstValue::ScalarPair(..) => None,
47             ConstValue::Scalar(val) => Some(val),
48         }
49     }
50
51     #[inline]
52     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
53         self.try_to_scalar()?.to_bits(size).ok()
54     }
55
56     #[inline]
57     pub fn try_to_ptr(&self) -> Option<Pointer> {
58         self.try_to_scalar()?.to_ptr().ok()
59     }
60
61     #[inline]
62     pub fn new_slice(
63         val: Scalar,
64         len: u64,
65         cx: impl HasDataLayout
66     ) -> Self {
67         ConstValue::ScalarPair(val, Scalar::Bits {
68             bits: len as u128,
69             size: cx.data_layout().pointer_size.bytes() as u8,
70         }.into())
71     }
72
73     #[inline]
74     pub fn new_dyn_trait(val: Scalar, vtable: Pointer) -> Self {
75         ConstValue::ScalarPair(val, Scalar::Ptr(vtable).into())
76     }
77 }
78
79 impl<'tcx> Scalar {
80     #[inline]
81     pub fn ptr_null(cx: impl HasDataLayout) -> Self {
82         Scalar::Bits {
83             bits: 0,
84             size: cx.data_layout().pointer_size.bytes() as u8,
85         }
86     }
87
88     #[inline]
89     pub fn zst() -> Self {
90         Scalar::Bits { bits: 0, size: 0 }
91     }
92
93     #[inline]
94     pub fn ptr_signed_offset(self, i: i64, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
95         let layout = cx.data_layout();
96         match self {
97             Scalar::Bits { bits, size } => {
98                 assert_eq!(size as u64, layout.pointer_size.bytes());
99                 Ok(Scalar::Bits {
100                     bits: layout.signed_offset(bits as u64, i)? as u128,
101                     size,
102                 })
103             }
104             Scalar::Ptr(ptr) => ptr.signed_offset(i, layout).map(Scalar::Ptr),
105         }
106     }
107
108     #[inline]
109     pub fn ptr_offset(self, i: Size, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
110         let layout = cx.data_layout();
111         match self {
112             Scalar::Bits { bits, size } => {
113                 assert_eq!(size as u64, layout.pointer_size.bytes());
114                 Ok(Scalar::Bits {
115                     bits: layout.offset(bits as u64, i.bytes())? as u128,
116                     size,
117                 })
118             }
119             Scalar::Ptr(ptr) => ptr.offset(i, layout).map(Scalar::Ptr),
120         }
121     }
122
123     #[inline]
124     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: impl HasDataLayout) -> Self {
125         let layout = cx.data_layout();
126         match self {
127             Scalar::Bits { bits, size } => {
128                 assert_eq!(size as u64, layout.pointer_size.bytes());
129                 Scalar::Bits {
130                     bits: layout.wrapping_signed_offset(bits as u64, i) as u128,
131                     size,
132                 }
133             }
134             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, layout)),
135         }
136     }
137
138     #[inline]
139     pub fn is_null_ptr(self, cx: impl HasDataLayout) -> bool {
140         match self {
141             Scalar::Bits { bits, size } =>  {
142                 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
143                 bits == 0
144             },
145             Scalar::Ptr(_) => false,
146         }
147     }
148
149     #[inline]
150     pub fn is_null(self) -> bool {
151         match self {
152             Scalar::Bits { bits, .. } => bits == 0,
153             Scalar::Ptr(_) => false
154         }
155     }
156
157     #[inline]
158     pub fn from_bool(b: bool) -> Self {
159         Scalar::Bits { bits: b as u128, size: 1 }
160     }
161
162     #[inline]
163     pub fn from_char(c: char) -> Self {
164         Scalar::Bits { bits: c as u128, size: 4 }
165     }
166
167     #[inline]
168     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
169         let i = i.into();
170         debug_assert_eq!(truncate(i, size), i,
171                     "Unsigned value {} does not fit in {} bits", i, size.bits());
172         Scalar::Bits { bits: i, size: size.bytes() as u8 }
173     }
174
175     #[inline]
176     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
177         let i = i.into();
178         // `into` performed sign extension, we have to truncate
179         let truncated = truncate(i as u128, size);
180         debug_assert_eq!(sign_extend(truncated, size) as i128, i,
181                     "Signed value {} does not fit in {} bits", i, size.bits());
182         Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
183     }
184
185     #[inline]
186     pub fn from_f32(f: f32) -> Self {
187         Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
188     }
189
190     #[inline]
191     pub fn from_f64(f: f64) -> Self {
192         Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
193     }
194
195     #[inline]
196     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
197         match self {
198             Scalar::Bits { bits, size } => {
199                 assert_eq!(target_size.bytes(), size as u64);
200                 assert_ne!(size, 0, "to_bits cannot be used with zsts");
201                 Ok(bits)
202             }
203             Scalar::Ptr(_) => err!(ReadPointerAsBytes),
204         }
205     }
206
207     #[inline]
208     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer> {
209         match self {
210             Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
211             Scalar::Bits { .. } => err!(ReadBytesAsPointer),
212             Scalar::Ptr(p) => Ok(p),
213         }
214     }
215
216     #[inline]
217     pub fn is_bits(self) -> bool {
218         match self {
219             Scalar::Bits { .. } => true,
220             _ => false,
221         }
222     }
223
224     #[inline]
225     pub fn is_ptr(self) -> bool {
226         match self {
227             Scalar::Ptr(_) => true,
228             _ => false,
229         }
230     }
231
232     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
233         match self {
234             Scalar::Bits { bits: 0, size: 1 } => Ok(false),
235             Scalar::Bits { bits: 1, size: 1 } => Ok(true),
236             _ => err!(InvalidBool),
237         }
238     }
239
240     pub fn to_char(self) -> EvalResult<'tcx, char> {
241         let val = self.to_u32()?;
242         match ::std::char::from_u32(val) {
243             Some(c) => Ok(c),
244             None => err!(InvalidChar(val as u128)),
245         }
246     }
247
248     pub fn to_u8(self) -> EvalResult<'static, u8> {
249         let sz = Size::from_bits(8);
250         let b = self.to_bits(sz)?;
251         assert_eq!(b as u8 as u128, b);
252         Ok(b as u8)
253     }
254
255     pub fn to_u32(self) -> EvalResult<'static, u32> {
256         let sz = Size::from_bits(32);
257         let b = self.to_bits(sz)?;
258         assert_eq!(b as u32 as u128, b);
259         Ok(b as u32)
260     }
261
262     pub fn to_u64(self) -> EvalResult<'static, u64> {
263         let sz = Size::from_bits(64);
264         let b = self.to_bits(sz)?;
265         assert_eq!(b as u64 as u128, b);
266         Ok(b as u64)
267     }
268
269     pub fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64> {
270         let b = self.to_bits(cx.data_layout().pointer_size)?;
271         assert_eq!(b as u64 as u128, b);
272         Ok(b as u64)
273     }
274
275     pub fn to_i8(self) -> EvalResult<'static, i8> {
276         let sz = Size::from_bits(8);
277         let b = self.to_bits(sz)?;
278         let b = sign_extend(b, sz) as i128;
279         assert_eq!(b as i8 as i128, b);
280         Ok(b as i8)
281     }
282
283     pub fn to_i32(self) -> EvalResult<'static, i32> {
284         let sz = Size::from_bits(32);
285         let b = self.to_bits(sz)?;
286         let b = sign_extend(b, sz) as i128;
287         assert_eq!(b as i32 as i128, b);
288         Ok(b as i32)
289     }
290
291     pub fn to_i64(self) -> EvalResult<'static, i64> {
292         let sz = Size::from_bits(64);
293         let b = self.to_bits(sz)?;
294         let b = sign_extend(b, sz) as i128;
295         assert_eq!(b as i64 as i128, b);
296         Ok(b as i64)
297     }
298
299     pub fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64> {
300         let b = self.to_bits(cx.data_layout().pointer_size)?;
301         let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
302         assert_eq!(b as i64 as i128, b);
303         Ok(b as i64)
304     }
305
306     #[inline]
307     pub fn to_f32(self) -> EvalResult<'static, f32> {
308         Ok(f32::from_bits(self.to_u32()?))
309     }
310
311     #[inline]
312     pub fn to_f64(self) -> EvalResult<'static, f64> {
313         Ok(f64::from_bits(self.to_u64()?))
314     }
315 }
316
317 impl From<Pointer> for Scalar {
318     #[inline(always)]
319     fn from(ptr: Pointer) -> Self {
320         Scalar::Ptr(ptr)
321     }
322 }
323
324 /// A `Scalar` represents an immediate, primitive value existing outside of a
325 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
326 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
327 /// of a simple value or a pointer into another `Allocation`
328 #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
329 pub enum Scalar<Id=AllocId> {
330     /// The raw bytes of a simple value.
331     Bits {
332         /// The first `size` bytes are the value.
333         /// Do not try to read less or more bytes that that. The remaining bytes must be 0.
334         size: u8,
335         bits: u128,
336     },
337
338     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
339     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
340     /// relocation and its associated offset together as a `Pointer` here.
341     Ptr(Pointer<Id>),
342 }
343
344 #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
345 pub enum ScalarMaybeUndef<Id=AllocId> {
346     Scalar(Scalar<Id>),
347     Undef,
348 }
349
350 impl From<Scalar> for ScalarMaybeUndef {
351     #[inline(always)]
352     fn from(s: Scalar) -> Self {
353         ScalarMaybeUndef::Scalar(s)
354     }
355 }
356
357 impl<'tcx> ScalarMaybeUndef {
358     #[inline]
359     pub fn not_undef(self) -> EvalResult<'static, Scalar> {
360         match self {
361             ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
362             ScalarMaybeUndef::Undef => err!(ReadUndefBytes),
363         }
364     }
365
366     #[inline(always)]
367     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer> {
368         self.not_undef()?.to_ptr()
369     }
370
371     #[inline(always)]
372     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
373         self.not_undef()?.to_bits(target_size)
374     }
375
376     #[inline(always)]
377     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
378         self.not_undef()?.to_bool()
379     }
380
381     #[inline(always)]
382     pub fn to_char(self) -> EvalResult<'tcx, char> {
383         self.not_undef()?.to_char()
384     }
385
386     #[inline(always)]
387     pub fn to_f32(self) -> EvalResult<'tcx, f32> {
388         self.not_undef()?.to_f32()
389     }
390
391     #[inline(always)]
392     pub fn to_f64(self) -> EvalResult<'tcx, f64> {
393         self.not_undef()?.to_f64()
394     }
395
396     #[inline(always)]
397     pub fn to_u8(self) -> EvalResult<'tcx, u8> {
398         self.not_undef()?.to_u8()
399     }
400
401     #[inline(always)]
402     pub fn to_u32(self) -> EvalResult<'tcx, u32> {
403         self.not_undef()?.to_u32()
404     }
405
406     #[inline(always)]
407     pub fn to_u64(self) -> EvalResult<'tcx, u64> {
408         self.not_undef()?.to_u64()
409     }
410
411     #[inline(always)]
412     pub fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'tcx, u64> {
413         self.not_undef()?.to_usize(cx)
414     }
415
416     #[inline(always)]
417     pub fn to_i8(self) -> EvalResult<'tcx, i8> {
418         self.not_undef()?.to_i8()
419     }
420
421     #[inline(always)]
422     pub fn to_i32(self) -> EvalResult<'tcx, i32> {
423         self.not_undef()?.to_i32()
424     }
425
426     #[inline(always)]
427     pub fn to_i64(self) -> EvalResult<'tcx, i64> {
428         self.not_undef()?.to_i64()
429     }
430
431     #[inline(always)]
432     pub fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'tcx, i64> {
433         self.not_undef()?.to_isize(cx)
434     }
435 }