use crate::ast::{IntSuffix, Type, TypeKind};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Ty {
Int { bits: u8, signed: bool },
Bool,
Float { bits: u8 },
Struct(u16),
}
impl Ty {
pub const I64: Ty = Ty::Int {
bits: 64,
signed: true,
};
pub const I32: Ty = Ty::Int {
bits: 32,
signed: true,
};
pub fn range(self) -> Option<(i128, i128)> {
match self {
Ty::Int { bits, signed: true } => {
let half = 1i128 << (bits - 1);
Some((-half, half - 1))
}
Ty::Int {
bits,
signed: false,
} => Some((0, (1i128 << bits) - 1)),
Ty::Bool | Ty::Float { .. } | Ty::Struct(_) => None,
}
}
pub fn holds(self, v: i128) -> bool {
match self.range() {
Some((lo, hi)) => lo <= v && v <= hi,
None if self == Ty::Bool => v == 0 || v == 1,
None => false,
}
}
pub fn wrap(self, v: i128) -> i128 {
match self {
Ty::Int { bits, signed } => {
let m = 1i128 << bits;
let r = v.rem_euclid(m);
if signed && r >= m / 2 {
r - m
} else {
r
}
}
Ty::Bool => (v != 0) as i128,
Ty::Float { .. } | Ty::Struct(_) => v,
}
}
pub fn bits(self) -> u8 {
match self {
Ty::Int { bits, .. } | Ty::Float { bits } => bits,
Ty::Bool => 1,
Ty::Struct(_) => 0,
}
}
pub fn is_int(self) -> bool {
matches!(self, Ty::Int { .. })
}
pub fn is_float(self) -> bool {
matches!(self, Ty::Float { .. })
}
pub const F64: Ty = Ty::Float { bits: 64 };
pub const F32: Ty = Ty::Float { bits: 32 };
pub fn from_name_tv(name: &str) -> Option<Ty> {
match name {
"f64" => Some(Ty::F64),
"f32" => Some(Ty::F32),
_ => Ty::from_name(name),
}
}
pub fn from_ast_tv(ty: &Type) -> Option<Ty> {
match &ty.kind {
TypeKind::Path(p) if p.segments.len() == 1 && p.segments[0].generics.is_empty() => {
Ty::from_name_tv(&p.segments[0].ident.name)
}
_ => None,
}
}
pub fn from_name(name: &str) -> Option<Ty> {
let (signed, rest) = match name.as_bytes().first()? {
b'i' => (true, &name[1..]),
b'u' => (false, &name[1..]),
_ => return (name == "bool").then_some(Ty::Bool),
};
let bits = match rest {
"8" => 8,
"16" => 16,
"32" => 32,
"64" => 64,
"size" => 64,
_ => return None,
};
Some(Ty::Int { bits, signed })
}
pub fn from_ast(ty: &Type) -> Option<Ty> {
match &ty.kind {
TypeKind::Path(p) if p.segments.len() == 1 && p.segments[0].generics.is_empty() => {
Ty::from_name(&p.segments[0].ident.name)
}
_ => None,
}
}
pub fn from_suffix(s: IntSuffix) -> Option<Ty> {
let name = format!("{s:?}").to_lowercase();
Ty::from_name(&name)
}
pub fn c_name(self) -> String {
match self {
Ty::Bool => "bool".into(),
Ty::Float { bits: 32 } => "float".into(),
Ty::Float { .. } => "double".into(),
Ty::Int { bits, signed } => {
format!("{}int{}_t", if signed { "" } else { "u" }, bits)
}
Ty::Struct(id) => super::agg::name_of(id),
}
}
}
impl std::fmt::Display for Ty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ty::Bool => write!(f, "bool"),
Ty::Float { bits } => write!(f, "f{bits}"),
Ty::Int { bits, signed } => write!(f, "{}{}", if *signed { "i" } else { "u" }, bits),
Ty::Struct(id) => write!(f, "{}", super::agg::name_of(*id)),
}
}
}