]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
Rollup merge of #61173 - fabric-and-ink:minor-cleanup, r=varkor
[rust.git] / src / librustc / mir / interpret / value.rs
1 use std::fmt;
2 use rustc_macros::HashStable;
3
4 use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
5 use crate::ty::PlaceholderConst;
6 use crate::hir::def_id::DefId;
7
8 use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
9
10 /// Represents the result of a raw const operation, pre-validation.
11 #[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash, HashStable)]
12 pub struct RawConst<'tcx> {
13     // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
14     // (so you can use `AllocMap::unwrap_memory`).
15     pub alloc_id: AllocId,
16     pub ty: Ty<'tcx>,
17 }
18
19 /// Represents a constant value in Rust. `Scalar` and `ScalarPair` are optimizations that
20 /// match the `LocalState` optimizations for easy conversions between `Value` and `ConstValue`.
21 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
22          RustcEncodable, RustcDecodable, Hash, HashStable)]
23 pub enum ConstValue<'tcx> {
24     /// A const generic parameter.
25     Param(ParamConst),
26
27     /// Infer the value of the const.
28     Infer(InferConst<'tcx>),
29
30     /// A placeholder const - universally quantified higher-ranked const.
31     Placeholder(PlaceholderConst),
32
33     /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
34     ///
35     /// Not using the enum `Value` to encode that this must not be `Undef`.
36     Scalar(Scalar),
37
38     /// Used only for `&[u8]` and `&str`
39     Slice {
40         data: &'tcx Allocation,
41         start: usize,
42         end: usize,
43     },
44
45     /// An allocation together with a pointer into the allocation.
46     /// Invariant: the pointer's `AllocId` resolves to the allocation.
47     ByRef(Pointer, &'tcx Allocation),
48
49     /// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
50     /// variants when the code is monomorphic enough for that.
51     Unevaluated(DefId, SubstsRef<'tcx>),
52 }
53
54 #[cfg(target_arch = "x86_64")]
55 static_assert_size!(ConstValue<'_>, 32);
56
57 impl<'tcx> ConstValue<'tcx> {
58     #[inline]
59     pub fn try_to_scalar(&self) -> Option<Scalar> {
60         match *self {
61             ConstValue::Param(_) |
62             ConstValue::Infer(_) |
63             ConstValue::Placeholder(_) |
64             ConstValue::ByRef(..) |
65             ConstValue::Unevaluated(..) |
66             ConstValue::Slice { .. } => None,
67             ConstValue::Scalar(val) => Some(val),
68         }
69     }
70
71     #[inline]
72     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
73         self.try_to_scalar()?.to_bits(size).ok()
74     }
75
76     #[inline]
77     pub fn try_to_ptr(&self) -> Option<Pointer> {
78         self.try_to_scalar()?.to_ptr().ok()
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, Eq, PartialEq, Ord, PartialOrd,
87          RustcEncodable, RustcDecodable, Hash, HashStable)]
88 pub enum Scalar<Tag=(), Id=AllocId> {
89     /// The raw bytes of a simple value.
90     Bits {
91         /// The first `size` bytes are the value.
92         /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
93         size: u8,
94         bits: u128,
95     },
96
97     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
98     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
99     /// relocation and its associated offset together as a `Pointer` here.
100     Ptr(Pointer<Tag, Id>),
101 }
102
103 #[cfg(target_arch = "x86_64")]
104 static_assert_size!(Scalar, 24);
105
106 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
107     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108         match self {
109             Scalar::Ptr(ptr) =>
110                 write!(f, "{:?}", ptr),
111             &Scalar::Bits { bits, size } => {
112                 if size == 0 {
113                     assert_eq!(bits, 0, "ZST value must be 0");
114                     write!(f, "<ZST>")
115                 } else {
116                     assert_eq!(truncate(bits, Size::from_bytes(size as u64)), bits,
117                             "Scalar value {:#x} exceeds size of {} bytes", bits, size);
118                     // Format as hex number wide enough to fit any value of the given `size`.
119                     // So bits=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
120                     write!(f, "0x{:>0width$x}", bits, width=(size*2) as usize)
121                 }
122             }
123         }
124     }
125 }
126
127 impl<Tag> fmt::Display for Scalar<Tag> {
128     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129         match self {
130             Scalar::Ptr(_) => write!(f, "a pointer"),
131             Scalar::Bits { bits, .. } => write!(f, "{}", bits),
132         }
133     }
134 }
135
136 impl<'tcx> Scalar<()> {
137     #[inline]
138     pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
139         match self {
140             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
141             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
142         }
143     }
144
145     #[inline(always)]
146     pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
147         where Tag: Default
148     {
149         self.with_tag(Tag::default())
150     }
151 }
152
153 impl<'tcx, Tag> Scalar<Tag> {
154     #[inline]
155     pub fn erase_tag(self) -> Scalar {
156         match self {
157             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
158             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
159         }
160     }
161
162     #[inline]
163     pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
164         Scalar::Bits {
165             bits: 0,
166             size: cx.data_layout().pointer_size.bytes() as u8,
167         }
168     }
169
170     #[inline]
171     pub fn zst() -> Self {
172         Scalar::Bits { bits: 0, size: 0 }
173     }
174
175     #[inline]
176     pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
177         let dl = cx.data_layout();
178         match self {
179             Scalar::Bits { bits, size } => {
180                 assert_eq!(size as u64, dl.pointer_size.bytes());
181                 Ok(Scalar::Bits {
182                     bits: dl.offset(bits as u64, i.bytes())? as u128,
183                     size,
184                 })
185             }
186             Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
187         }
188     }
189
190     #[inline]
191     pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
192         let dl = cx.data_layout();
193         match self {
194             Scalar::Bits { bits, size } => {
195                 assert_eq!(size as u64, dl.pointer_size.bytes());
196                 Scalar::Bits {
197                     bits: dl.overflowing_offset(bits as u64, i.bytes()).0 as u128,
198                     size,
199                 }
200             }
201             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
202         }
203     }
204
205     #[inline]
206     pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
207         let dl = cx.data_layout();
208         match self {
209             Scalar::Bits { bits, size } => {
210                 assert_eq!(size as u64, dl.pointer_size().bytes());
211                 Ok(Scalar::Bits {
212                     bits: dl.signed_offset(bits as u64, i)? as u128,
213                     size,
214                 })
215             }
216             Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
217         }
218     }
219
220     #[inline]
221     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
222         let dl = cx.data_layout();
223         match self {
224             Scalar::Bits { bits, size } => {
225                 assert_eq!(size as u64, dl.pointer_size.bytes());
226                 Scalar::Bits {
227                     bits: dl.overflowing_signed_offset(bits as u64, i128::from(i)).0 as u128,
228                     size,
229                 }
230             }
231             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
232         }
233     }
234
235     /// Returns this pointers offset from the allocation base, or from NULL (for
236     /// integer pointers).
237     #[inline]
238     pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
239         match self {
240             Scalar::Bits { bits, size } => {
241                 assert_eq!(size as u64, cx.pointer_size().bytes());
242                 Size::from_bytes(bits as u64)
243             }
244             Scalar::Ptr(ptr) => ptr.offset,
245         }
246     }
247
248     #[inline]
249     pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
250         match self {
251             Scalar::Bits { bits, size } => {
252                 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
253                 bits == 0
254             },
255             Scalar::Ptr(_) => false,
256         }
257     }
258
259     #[inline]
260     pub fn from_bool(b: bool) -> Self {
261         Scalar::Bits { bits: b as u128, size: 1 }
262     }
263
264     #[inline]
265     pub fn from_char(c: char) -> Self {
266         Scalar::Bits { bits: c as u128, size: 4 }
267     }
268
269     #[inline]
270     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
271         let i = i.into();
272         debug_assert_eq!(truncate(i, size), i,
273                          "Unsigned value {} does not fit in {} bits", i, size.bits());
274         Scalar::Bits { bits: i, size: size.bytes() as u8 }
275     }
276
277     #[inline]
278     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
279         let i = i.into();
280         // `into` performed sign extension, we have to truncate
281         let truncated = truncate(i as u128, size);
282         debug_assert_eq!(sign_extend(truncated, size) as i128, i,
283                          "Signed value {} does not fit in {} bits", i, size.bits());
284         Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
285     }
286
287     #[inline]
288     pub fn from_f32(f: f32) -> Self {
289         Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
290     }
291
292     #[inline]
293     pub fn from_f64(f: f64) -> Self {
294         Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
295     }
296
297     #[inline]
298     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
299         match self {
300             Scalar::Bits { bits, size } => {
301                 assert_eq!(target_size.bytes(), size as u64);
302                 assert_ne!(size, 0, "to_bits cannot be used with zsts");
303                 Ok(bits)
304             }
305             Scalar::Ptr(_) => err!(ReadPointerAsBytes),
306         }
307     }
308
309     #[inline]
310     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
311         match self {
312             Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
313             Scalar::Bits { .. } => err!(ReadBytesAsPointer),
314             Scalar::Ptr(p) => Ok(p),
315         }
316     }
317
318     #[inline]
319     pub fn is_bits(self) -> bool {
320         match self {
321             Scalar::Bits { .. } => true,
322             _ => false,
323         }
324     }
325
326     #[inline]
327     pub fn is_ptr(self) -> bool {
328         match self {
329             Scalar::Ptr(_) => true,
330             _ => false,
331         }
332     }
333
334     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
335         match self {
336             Scalar::Bits { bits: 0, size: 1 } => Ok(false),
337             Scalar::Bits { bits: 1, size: 1 } => Ok(true),
338             _ => err!(InvalidBool),
339         }
340     }
341
342     pub fn to_char(self) -> EvalResult<'tcx, char> {
343         let val = self.to_u32()?;
344         match ::std::char::from_u32(val) {
345             Some(c) => Ok(c),
346             None => err!(InvalidChar(val as u128)),
347         }
348     }
349
350     pub fn to_u8(self) -> EvalResult<'static, u8> {
351         let sz = Size::from_bits(8);
352         let b = self.to_bits(sz)?;
353         assert_eq!(b as u8 as u128, b);
354         Ok(b as u8)
355     }
356
357     pub fn to_u32(self) -> EvalResult<'static, u32> {
358         let sz = Size::from_bits(32);
359         let b = self.to_bits(sz)?;
360         assert_eq!(b as u32 as u128, b);
361         Ok(b as u32)
362     }
363
364     pub fn to_u64(self) -> EvalResult<'static, u64> {
365         let sz = Size::from_bits(64);
366         let b = self.to_bits(sz)?;
367         assert_eq!(b as u64 as u128, b);
368         Ok(b as u64)
369     }
370
371     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'static, u64> {
372         let b = self.to_bits(cx.data_layout().pointer_size)?;
373         assert_eq!(b as u64 as u128, b);
374         Ok(b as u64)
375     }
376
377     pub fn to_i8(self) -> EvalResult<'static, i8> {
378         let sz = Size::from_bits(8);
379         let b = self.to_bits(sz)?;
380         let b = sign_extend(b, sz) as i128;
381         assert_eq!(b as i8 as i128, b);
382         Ok(b as i8)
383     }
384
385     pub fn to_i32(self) -> EvalResult<'static, i32> {
386         let sz = Size::from_bits(32);
387         let b = self.to_bits(sz)?;
388         let b = sign_extend(b, sz) as i128;
389         assert_eq!(b as i32 as i128, b);
390         Ok(b as i32)
391     }
392
393     pub fn to_i64(self) -> EvalResult<'static, i64> {
394         let sz = Size::from_bits(64);
395         let b = self.to_bits(sz)?;
396         let b = sign_extend(b, sz) as i128;
397         assert_eq!(b as i64 as i128, b);
398         Ok(b as i64)
399     }
400
401     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'static, i64> {
402         let b = self.to_bits(cx.data_layout().pointer_size)?;
403         let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
404         assert_eq!(b as i64 as i128, b);
405         Ok(b as i64)
406     }
407
408     #[inline]
409     pub fn to_f32(self) -> EvalResult<'static, f32> {
410         Ok(f32::from_bits(self.to_u32()?))
411     }
412
413     #[inline]
414     pub fn to_f64(self) -> EvalResult<'static, f64> {
415         Ok(f64::from_bits(self.to_u64()?))
416     }
417 }
418
419 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
420     #[inline(always)]
421     fn from(ptr: Pointer<Tag>) -> Self {
422         Scalar::Ptr(ptr)
423     }
424 }
425
426 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
427 pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
428     Scalar(Scalar<Tag, Id>),
429     Undef,
430 }
431
432 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
433     #[inline(always)]
434     fn from(s: Scalar<Tag>) -> Self {
435         ScalarMaybeUndef::Scalar(s)
436     }
437 }
438
439 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
440     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
441         match self {
442             ScalarMaybeUndef::Undef => write!(f, "Undef"),
443             ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
444         }
445     }
446 }
447
448 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
449     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
450         match self {
451             ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
452             ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
453         }
454     }
455 }
456
457 impl<'tcx> ScalarMaybeUndef<()> {
458     #[inline]
459     pub fn with_tag<Tag>(self, new_tag: Tag) -> ScalarMaybeUndef<Tag> {
460         match self {
461             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_tag(new_tag)),
462             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
463         }
464     }
465
466     #[inline(always)]
467     pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
468         where Tag: Default
469     {
470         self.with_tag(Tag::default())
471     }
472 }
473
474 impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
475     #[inline]
476     pub fn erase_tag(self) -> ScalarMaybeUndef
477     {
478         match self {
479             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_tag()),
480             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
481         }
482     }
483
484     #[inline]
485     pub fn not_undef(self) -> EvalResult<'static, Scalar<Tag>> {
486         match self {
487             ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
488             ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
489         }
490     }
491
492     #[inline(always)]
493     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
494         self.not_undef()?.to_ptr()
495     }
496
497     #[inline(always)]
498     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
499         self.not_undef()?.to_bits(target_size)
500     }
501
502     #[inline(always)]
503     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
504         self.not_undef()?.to_bool()
505     }
506
507     #[inline(always)]
508     pub fn to_char(self) -> EvalResult<'tcx, char> {
509         self.not_undef()?.to_char()
510     }
511
512     #[inline(always)]
513     pub fn to_f32(self) -> EvalResult<'tcx, f32> {
514         self.not_undef()?.to_f32()
515     }
516
517     #[inline(always)]
518     pub fn to_f64(self) -> EvalResult<'tcx, f64> {
519         self.not_undef()?.to_f64()
520     }
521
522     #[inline(always)]
523     pub fn to_u8(self) -> EvalResult<'tcx, u8> {
524         self.not_undef()?.to_u8()
525     }
526
527     #[inline(always)]
528     pub fn to_u32(self) -> EvalResult<'tcx, u32> {
529         self.not_undef()?.to_u32()
530     }
531
532     #[inline(always)]
533     pub fn to_u64(self) -> EvalResult<'tcx, u64> {
534         self.not_undef()?.to_u64()
535     }
536
537     #[inline(always)]
538     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, u64> {
539         self.not_undef()?.to_usize(cx)
540     }
541
542     #[inline(always)]
543     pub fn to_i8(self) -> EvalResult<'tcx, i8> {
544         self.not_undef()?.to_i8()
545     }
546
547     #[inline(always)]
548     pub fn to_i32(self) -> EvalResult<'tcx, i32> {
549         self.not_undef()?.to_i32()
550     }
551
552     #[inline(always)]
553     pub fn to_i64(self) -> EvalResult<'tcx, i64> {
554         self.not_undef()?.to_i64()
555     }
556
557     #[inline(always)]
558     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, i64> {
559         self.not_undef()?.to_isize(cx)
560     }
561 }
562
563 impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {
564     Scalar(v),
565     Undef
566 });