Skip to main content

fic/ty/
value.rs

1//! A constant's value: a Rust integer of its fic width, or a bool.
2
3use std::fmt;
4
5use derive_more::{From, TryInto};
6use serde::Serialize;
7
8use crate::ast::PrimitiveType;
9
10/// The value of a constant: a Rust integer of the constant's own width
11/// (`int8` is an `i8`, `uint`/`Address` a `u64`, `byte` a `u8`), or a
12/// bool. Nothing is widened: a value only ever meets a value of its own
13/// width.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, From, TryInto)]
15pub enum ConstValue {
16    /// An `int8`.
17    Int8(i8),
18    /// An `int16`.
19    Int16(i16),
20    /// An `int32`.
21    Int32(i32),
22    /// An `int64`, which `int` is.
23    Int64(i64),
24    /// A `uint8`, which `byte` is.
25    UInt8(u8),
26    /// A `uint16`.
27    UInt16(u16),
28    /// A `uint32`.
29    UInt32(u32),
30    /// A `uint64`, which `uint` and `Address` are.
31    UInt64(u64),
32    /// A `bool`.
33    Bool(bool),
34}
35
36impl ConstValue {
37    /// The primitive type this is a value of.
38    #[must_use]
39    pub fn prim(self) -> PrimitiveType {
40        match self {
41            ConstValue::Int8(_) => PrimitiveType::Int8,
42            ConstValue::Int16(_) => PrimitiveType::Int16,
43            ConstValue::Int32(_) => PrimitiveType::Int32,
44            ConstValue::Int64(_) => PrimitiveType::Int64,
45            ConstValue::UInt8(_) => PrimitiveType::UInt8,
46            ConstValue::UInt16(_) => PrimitiveType::UInt16,
47            ConstValue::UInt32(_) => PrimitiveType::UInt32,
48            ConstValue::UInt64(_) => PrimitiveType::UInt64,
49            ConstValue::Bool(_) => PrimitiveType::Bool,
50        }
51    }
52
53    /// The value as an `i128`, which holds every width; `None` for a bool.
54    #[must_use]
55    pub fn integer(self) -> Option<i128> {
56        match self {
57            ConstValue::Int8(v) => Some(v.into()),
58            ConstValue::Int16(v) => Some(v.into()),
59            ConstValue::Int32(v) => Some(v.into()),
60            ConstValue::Int64(v) => Some(v.into()),
61            ConstValue::UInt8(v) => Some(v.into()),
62            ConstValue::UInt16(v) => Some(v.into()),
63            ConstValue::UInt32(v) => Some(v.into()),
64            ConstValue::UInt64(v) => Some(v.into()),
65            ConstValue::Bool(_) => None,
66        }
67    }
68
69    /// The value as a count (an array length): a non-negative integer.
70    #[must_use]
71    pub fn count(self) -> Option<u64> {
72        match self {
73            ConstValue::Int8(v) => u64::try_from(v).ok(),
74            ConstValue::Int16(v) => u64::try_from(v).ok(),
75            ConstValue::Int32(v) => u64::try_from(v).ok(),
76            ConstValue::Int64(v) => u64::try_from(v).ok(),
77            ConstValue::UInt8(v) => Some(u64::from(v)),
78            ConstValue::UInt16(v) => Some(u64::from(v)),
79            ConstValue::UInt32(v) => Some(u64::from(v)),
80            ConstValue::UInt64(v) => Some(v),
81            ConstValue::Bool(_) => None,
82        }
83    }
84
85    /// Whether this is the integer zero.
86    #[must_use]
87    pub fn is_zero(self) -> bool {
88        matches!(
89            self,
90            ConstValue::Int8(0)
91                | ConstValue::Int16(0)
92                | ConstValue::Int32(0)
93                | ConstValue::Int64(0)
94                | ConstValue::UInt8(0)
95                | ConstValue::UInt16(0)
96                | ConstValue::UInt32(0)
97                | ConstValue::UInt64(0)
98        )
99    }
100}
101
102impl fmt::Display for ConstValue {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        match self {
105            ConstValue::Int8(v) => v.fmt(f),
106            ConstValue::Int16(v) => v.fmt(f),
107            ConstValue::Int32(v) => v.fmt(f),
108            ConstValue::Int64(v) => v.fmt(f),
109            ConstValue::UInt8(v) => v.fmt(f),
110            ConstValue::UInt16(v) => v.fmt(f),
111            ConstValue::UInt32(v) => v.fmt(f),
112            ConstValue::UInt64(v) => v.fmt(f),
113            ConstValue::Bool(v) => v.fmt(f),
114        }
115    }
116}