use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MirType {
UInt(u16),
Int(u16),
Bool,
Address,
FixedBytes(u8),
MemPtr,
StoragePtr,
CalldataPtr,
Function,
Void,
}
impl MirType {
#[must_use]
pub const fn stack_size(&self) -> usize {
match self {
Self::Bool => 1,
Self::UInt(bits) | Self::Int(bits) => (*bits as usize).div_ceil(8),
Self::Address => 20,
Self::FixedBytes(n) => *n as usize,
Self::MemPtr | Self::StoragePtr | Self::CalldataPtr => 32,
Self::Function => 24,
Self::Void => 0,
}
}
#[must_use]
pub const fn fits_in_word(&self) -> bool {
self.stack_size() <= 32
}
#[must_use]
pub const fn uint256() -> Self {
Self::UInt(256)
}
#[must_use]
pub const fn int256() -> Self {
Self::Int(256)
}
#[must_use]
pub const fn bytes32() -> Self {
Self::FixedBytes(32)
}
}
impl fmt::Display for MirType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UInt(bits) => write!(f, "u{bits}"),
Self::Int(bits) => write!(f, "i{bits}"),
Self::Bool => write!(f, "bool"),
Self::Address => write!(f, "address"),
Self::FixedBytes(n) => write!(f, "bytes{n}"),
Self::MemPtr => write!(f, "memptr"),
Self::StoragePtr => write!(f, "storageptr"),
Self::CalldataPtr => write!(f, "calldataptr"),
Self::Function => write!(f, "function"),
Self::Void => write!(f, "void"),
}
}
}