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