1 use rustc_apfloat::ieee::{Double, Single};
2 use rustc_apfloat::Float;
3 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4 use rustc_target::abi::Size;
5 use std::convert::{TryFrom, TryInto};
7 use std::num::NonZeroU8;
11 #[derive(Copy, Clone)]
12 /// A type for representing any integer. Only used for printing.
14 /// The "untyped" variant of `ConstInt`.
16 /// Whether the value is of a signed integer type.
18 /// Whether the value is a `usize` or `isize` type.
19 is_ptr_sized_integral: bool,
23 pub fn new(int: ScalarInt, signed: bool, is_ptr_sized_integral: bool) -> Self {
24 Self { int, signed, is_ptr_sized_integral }
28 impl std::fmt::Debug for ConstInt {
29 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 let Self { int, signed, is_ptr_sized_integral } = *self;
31 let size = int.size().bytes();
34 let bit_size = size * 8;
35 let min = 1u128 << (bit_size - 1);
38 match (size, is_ptr_sized_integral) {
39 (_, true) => write!(fmt, "isize::MIN"),
40 (1, _) => write!(fmt, "i8::MIN"),
41 (2, _) => write!(fmt, "i16::MIN"),
42 (4, _) => write!(fmt, "i32::MIN"),
43 (8, _) => write!(fmt, "i64::MIN"),
44 (16, _) => write!(fmt, "i128::MIN"),
45 _ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed),
47 } else if raw == max {
48 match (size, is_ptr_sized_integral) {
49 (_, true) => write!(fmt, "isize::MAX"),
50 (1, _) => write!(fmt, "i8::MAX"),
51 (2, _) => write!(fmt, "i16::MAX"),
52 (4, _) => write!(fmt, "i32::MAX"),
53 (8, _) => write!(fmt, "i64::MAX"),
54 (16, _) => write!(fmt, "i128::MAX"),
55 _ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed),
59 1 => write!(fmt, "{}", raw as i8)?,
60 2 => write!(fmt, "{}", raw as i16)?,
61 4 => write!(fmt, "{}", raw as i32)?,
62 8 => write!(fmt, "{}", raw as i64)?,
63 16 => write!(fmt, "{}", raw as i128)?,
64 _ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed),
67 match (size, is_ptr_sized_integral) {
68 (_, true) => write!(fmt, "_isize")?,
69 (1, _) => write!(fmt, "_i8")?,
70 (2, _) => write!(fmt, "_i16")?,
71 (4, _) => write!(fmt, "_i32")?,
72 (8, _) => write!(fmt, "_i64")?,
73 (16, _) => write!(fmt, "_i128")?,
80 let max = Size::from_bytes(size).truncate(u128::MAX);
82 match (size, is_ptr_sized_integral) {
83 (_, true) => write!(fmt, "usize::MAX"),
84 (1, _) => write!(fmt, "u8::MAX"),
85 (2, _) => write!(fmt, "u16::MAX"),
86 (4, _) => write!(fmt, "u32::MAX"),
87 (8, _) => write!(fmt, "u64::MAX"),
88 (16, _) => write!(fmt, "u128::MAX"),
89 _ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed),
93 1 => write!(fmt, "{}", raw as u8)?,
94 2 => write!(fmt, "{}", raw as u16)?,
95 4 => write!(fmt, "{}", raw as u32)?,
96 8 => write!(fmt, "{}", raw as u64)?,
97 16 => write!(fmt, "{}", raw as u128)?,
98 _ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed),
101 match (size, is_ptr_sized_integral) {
102 (_, true) => write!(fmt, "_usize")?,
103 (1, _) => write!(fmt, "_u8")?,
104 (2, _) => write!(fmt, "_u16")?,
105 (4, _) => write!(fmt, "_u32")?,
106 (8, _) => write!(fmt, "_u64")?,
107 (16, _) => write!(fmt, "_u128")?,
117 /// The raw bytes of a simple value.
119 /// This is a packed struct in order to allow this type to be optimally embedded in enums
121 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
123 pub struct ScalarInt {
124 /// The first `size` bytes of `data` are the value.
125 /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
130 // Cannot derive these, as the derives take references to the fields, and we
131 // can't take references to fields of packed structs.
132 impl<CTX> crate::ty::HashStable<CTX> for ScalarInt {
133 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut crate::ty::StableHasher) {
134 // Using a block `{self.data}` here to force a copy instead of using `self.data`
135 // directly, because `hash_stable` takes `&self` and would thus borrow `self.data`.
136 // Since `Self` is a packed struct, that would create a possibly unaligned reference,
138 { self.data }.hash_stable(hcx, hasher);
139 self.size.get().hash_stable(hcx, hasher);
143 impl<S: Encoder> Encodable<S> for ScalarInt {
144 fn encode(&self, s: &mut S) {
145 s.emit_u128(self.data);
146 s.emit_u8(self.size.get());
150 impl<D: Decoder> Decodable<D> for ScalarInt {
151 fn decode(d: &mut D) -> ScalarInt {
152 ScalarInt { data: d.read_u128(), size: NonZeroU8::new(d.read_u8()).unwrap() }
157 pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZeroU8::new(1).unwrap() };
159 pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZeroU8::new(1).unwrap() };
162 pub fn size(self) -> Size {
163 Size::from_bytes(self.size.get())
166 /// Make sure the `data` fits in `size`.
167 /// This is guaranteed by all constructors here, but having had this check saved us from
168 /// bugs many times in the past, so keeping it around is definitely worth it.
170 fn check_data(self) {
171 // Using a block `{self.data}` here to force a copy instead of using `self.data`
172 // directly, because `debug_assert_eq` takes references to its arguments and formatting
173 // arguments and would thus borrow `self.data`. Since `Self`
174 // is a packed struct, that would create a possibly unaligned reference, which
177 self.size().truncate(self.data),
179 "Scalar value {:#x} exceeds size of {} bytes",
186 pub fn null(size: Size) -> Self {
187 Self { data: 0, size: NonZeroU8::new(size.bytes() as u8).unwrap() }
191 pub fn is_null(self) -> bool {
196 pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
198 if size.truncate(data) == data {
199 Some(Self { data, size: NonZeroU8::new(size.bytes() as u8).unwrap() })
206 pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
208 // `into` performed sign extension, we have to truncate
209 let truncated = size.truncate(i as u128);
210 if size.sign_extend(truncated) as i128 == i {
211 Some(Self { data: truncated, size: NonZeroU8::new(size.bytes() as u8).unwrap() })
218 pub fn assert_bits(self, target_size: Size) -> u128 {
219 self.to_bits(target_size).unwrap_or_else(|size| {
220 bug!("expected int of size {}, but got size {}", target_size.bytes(), size.bytes())
225 pub fn to_bits(self, target_size: Size) -> Result<u128, Size> {
226 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
227 if target_size.bytes() == u64::from(self.size.get()) {
236 pub fn try_to_machine_usize<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Result<u64, Size> {
237 Ok(self.to_bits(tcx.data_layout.pointer_size)? as u64)
240 /// Tries to convert the `ScalarInt` to an unsigned integer of the given size.
241 /// Fails if the size of the `ScalarInt` is unequal to `size` and returns the
242 /// `ScalarInt`s size in that case.
244 pub fn try_to_uint(self, size: Size) -> Result<u128, Size> {
248 // Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
249 // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
250 // value of the `ScalarInt` in that case.
252 pub fn try_to_bool(self) -> Result<bool, Size> {
253 match self.try_to_u8()? {
256 _ => Err(self.size()),
260 // Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
261 // in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
264 pub fn try_to_u8(self) -> Result<u8, Size> {
265 self.to_bits(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
268 /// Tries to convert the `ScalarInt` to `u16`. Fails if the size of the `ScalarInt`
269 /// in not equal to `Size { raw: 2 }` and returns the `size` value of the `ScalarInt` in
272 pub fn try_to_u16(self) -> Result<u16, Size> {
273 self.to_bits(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
276 /// Tries to convert the `ScalarInt` to `u32`. Fails if the `size` of the `ScalarInt`
277 /// in not equal to `Size { raw: 4 }` and returns the `size` value of the `ScalarInt` in
280 pub fn try_to_u32(self) -> Result<u32, Size> {
281 self.to_bits(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
284 /// Tries to convert the `ScalarInt` to `u64`. Fails if the `size` of the `ScalarInt`
285 /// in not equal to `Size { raw: 8 }` and returns the `size` value of the `ScalarInt` in
288 pub fn try_to_u64(self) -> Result<u64, Size> {
289 self.to_bits(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
292 /// Tries to convert the `ScalarInt` to `u128`. Fails if the `size` of the `ScalarInt`
293 /// in not equal to `Size { raw: 16 }` and returns the `size` value of the `ScalarInt` in
296 pub fn try_to_u128(self) -> Result<u128, Size> {
297 self.to_bits(Size::from_bits(128))
300 /// Tries to convert the `ScalarInt` to a signed integer of the given size.
301 /// Fails if the size of the `ScalarInt` is unequal to `size` and returns the
302 /// `ScalarInt`s size in that case.
304 pub fn try_to_int(self, size: Size) -> Result<i128, Size> {
305 let b = self.to_bits(size)?;
306 Ok(size.sign_extend(b) as i128)
309 /// Tries to convert the `ScalarInt` to i8.
310 /// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 1 }`
311 /// and returns the `ScalarInt`s size in that case.
312 pub fn try_to_i8(self) -> Result<i8, Size> {
313 self.try_to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
316 /// Tries to convert the `ScalarInt` to i16.
317 /// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 2 }`
318 /// and returns the `ScalarInt`s size in that case.
319 pub fn try_to_i16(self) -> Result<i16, Size> {
320 self.try_to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
323 /// Tries to convert the `ScalarInt` to i32.
324 /// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 4 }`
325 /// and returns the `ScalarInt`s size in that case.
326 pub fn try_to_i32(self) -> Result<i32, Size> {
327 self.try_to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
330 /// Tries to convert the `ScalarInt` to i64.
331 /// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 8 }`
332 /// and returns the `ScalarInt`s size in that case.
333 pub fn try_to_i64(self) -> Result<i64, Size> {
334 self.try_to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
337 /// Tries to convert the `ScalarInt` to i128.
338 /// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 16 }`
339 /// and returns the `ScalarInt`s size in that case.
340 pub fn try_to_i128(self) -> Result<i128, Size> {
341 self.try_to_int(Size::from_bits(128)).map(|v| i128::try_from(v).unwrap())
348 impl From<$ty> for ScalarInt {
350 fn from(u: $ty) -> Self {
353 size: NonZeroU8::new(std::mem::size_of::<$ty>() as u8).unwrap(),
361 macro_rules! try_from {
364 impl TryFrom<ScalarInt> for $ty {
367 fn try_from(int: ScalarInt) -> Result<Self, Size> {
368 // The `unwrap` cannot fail because to_bits (if it succeeds)
369 // is guaranteed to return a value that fits into the size.
370 int.to_bits(Size::from_bytes(std::mem::size_of::<$ty>()))
371 .map(|u| u.try_into().unwrap())
378 from!(u8, u16, u32, u64, u128, bool);
379 try_from!(u8, u16, u32, u64, u128);
381 impl TryFrom<ScalarInt> for bool {
384 fn try_from(int: ScalarInt) -> Result<Self, Size> {
385 int.to_bits(Size::from_bytes(1)).and_then(|u| match u {
388 _ => Err(Size::from_bytes(1)),
393 impl From<char> for ScalarInt {
395 fn from(c: char) -> Self {
396 Self { data: c as u128, size: NonZeroU8::new(std::mem::size_of::<char>() as u8).unwrap() }
400 /// Error returned when a conversion from ScalarInt to char fails.
402 pub struct CharTryFromScalarInt;
404 impl TryFrom<ScalarInt> for char {
405 type Error = CharTryFromScalarInt;
408 fn try_from(int: ScalarInt) -> Result<Self, Self::Error> {
409 let Ok(bits) = int.to_bits(Size::from_bytes(std::mem::size_of::<char>())) else {
410 return Err(CharTryFromScalarInt);
412 match char::from_u32(bits.try_into().unwrap()) {
414 None => Err(CharTryFromScalarInt),
419 impl From<Single> for ScalarInt {
421 fn from(f: Single) -> Self {
422 // We trust apfloat to give us properly truncated data.
423 Self { data: f.to_bits(), size: NonZeroU8::new((Single::BITS / 8) as u8).unwrap() }
427 impl TryFrom<ScalarInt> for Single {
430 fn try_from(int: ScalarInt) -> Result<Self, Size> {
431 int.to_bits(Size::from_bytes(4)).map(Self::from_bits)
435 impl From<Double> for ScalarInt {
437 fn from(f: Double) -> Self {
438 // We trust apfloat to give us properly truncated data.
439 Self { data: f.to_bits(), size: NonZeroU8::new((Double::BITS / 8) as u8).unwrap() }
443 impl TryFrom<ScalarInt> for Double {
446 fn try_from(int: ScalarInt) -> Result<Self, Size> {
447 int.to_bits(Size::from_bytes(8)).map(Self::from_bits)
451 impl fmt::Debug for ScalarInt {
452 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
453 // Dispatch to LowerHex below.
454 write!(f, "0x{:x}", self)
458 impl fmt::LowerHex for ScalarInt {
459 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
462 // Like regular ints, alternate flag adds leading `0x`.
465 // Format as hex number wide enough to fit any value of the given `size`.
466 // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
467 // Using a block `{self.data}` here to force a copy instead of using `self.data`
468 // directly, because `write!` takes references to its formatting arguments and
469 // would thus borrow `self.data`. Since `Self`
470 // is a packed struct, that would create a possibly unaligned reference, which
472 write!(f, "{:01$x}", { self.data }, self.size.get() as usize * 2)
476 impl fmt::UpperHex for ScalarInt {
477 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
479 // Format as hex number wide enough to fit any value of the given `size`.
480 // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
481 // Using a block `{self.data}` here to force a copy instead of using `self.data`
482 // directly, because `write!` takes references to its formatting arguments and
483 // would thus borrow `self.data`. Since `Self`
484 // is a packed struct, that would create a possibly unaligned reference, which
486 write!(f, "{:01$X}", { self.data }, self.size.get() as usize * 2)
490 impl fmt::Display for ScalarInt {
491 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493 write!(f, "{}", { self.data })