use crate::name_res::{GlobalDefId, PrimTy};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct InferTyId(pub u32);
impl InferTyId {
pub const NONE: Self = Self(u32::MAX);
pub const UNKNOWN: Self = Self(0);
pub const NEVER: Self = Self(1);
pub const UNIT: Self = Self(2);
pub const I8: Self = Self(3);
pub const I16: Self = Self(4);
pub const I32: Self = Self(5);
pub const I64: Self = Self(6);
pub const I128: Self = Self(7);
pub const ISIZE: Self = Self(8);
pub const U8: Self = Self(9);
pub const U16: Self = Self(10);
pub const U32: Self = Self(11);
pub const U64: Self = Self(12);
pub const U128: Self = Self(13);
pub const USIZE: Self = Self(14);
pub const F32: Self = Self(15);
pub const F64: Self = Self(16);
pub const BOOL: Self = Self(17);
pub const CHAR: Self = Self(18);
pub const STR: Self = Self(19);
pub const STR_REF: Self = Self(20);
pub const BUILTIN_COUNT: usize = 21;
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
#[inline]
pub const fn from_prim(p: PrimTy) -> Self {
Self((p as u32) + 2)
}
}
impl Default for InferTyId {
#[inline]
fn default() -> Self {
Self::UNKNOWN
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TyVarId(pub u32);
impl TyVarId {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TyVarKind {
General = 0,
IntVar = 1,
FloatVar = 2,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct TySliceRange {
pub start: u32,
pub len: u16,
}
impl TySliceRange {
pub const EMPTY: Self = Self { start: 0, len: 0 };
#[inline]
pub const fn is_empty(self) -> bool {
self.len == 0
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TyFlags(pub u8);
impl TyFlags {
pub const EMPTY: Self = Self(0);
pub const HAS_INFER: Self = Self(1 << 0);
pub const HAS_PARAM: Self = Self(1 << 1);
pub const HAS_UNKNOWN: Self = Self(1 << 2);
pub const HAS_ALIAS: Self = Self(1 << 3);
#[inline]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[inline]
pub const fn has_infer(self) -> bool {
(self.0 & Self::HAS_INFER.0) != 0
}
#[inline]
pub const fn has_param(self) -> bool {
(self.0 & Self::HAS_PARAM.0) != 0
}
#[inline]
pub const fn has_unknown(self) -> bool {
(self.0 & Self::HAS_UNKNOWN.0) != 0
}
#[inline]
pub const fn has_alias(self) -> bool {
(self.0 & Self::HAS_ALIAS.0) != 0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InferTyKind {
Unknown,
Never,
Prim(PrimTy),
Tuple(TySliceRange),
Adt {
def: GlobalDefId,
args: TySliceRange,
},
Ref {
is_mut: bool,
inner: InferTyId,
},
Ptr {
is_mut: bool,
inner: InferTyId,
},
Slice(InferTyId),
Array {
elem: InferTyId,
len: u64,
},
FnPtr {
sig: TySliceRange,
},
FnDef {
def: GlobalDefId,
args: TySliceRange,
},
Closure {
def_start: u32,
def_end: u32,
sig: TySliceRange,
},
Param {
idx: u16,
def: GlobalDefId,
},
Alias {
trait_def: GlobalDefId,
assoc_def: GlobalDefId,
self_ty: InferTyId,
args: TySliceRange,
},
Dyn {
principal: GlobalDefId,
args: TySliceRange,
},
Opaque {
def: GlobalDefId,
args: TySliceRange,
},
Infer(TyVarId),
}
#[inline]
pub const fn is_int_prim(p: PrimTy) -> bool {
matches!(
p,
PrimTy::I8 | PrimTy::I16 | PrimTy::I32 | PrimTy::I64 | PrimTy::I128 | PrimTy::Isize | PrimTy::U8 | PrimTy::U16 | PrimTy::U32 | PrimTy::U64 | PrimTy::U128 | PrimTy::Usize
)
}
#[inline]
pub const fn is_float_prim(p: PrimTy) -> bool {
matches!(p, PrimTy::F32 | PrimTy::F64)
}
pub fn clone_ty_into(src: &super::arena::InferTyArena, id: InferTyId, dst: &mut super::arena::InferTyArena) -> InferTyId {
clone_ty_rec(src, id, dst, 0)
}
fn clone_ty_rec(src: &super::arena::InferTyArena, id: InferTyId, dst: &mut super::arena::InferTyArena, depth: u8) -> InferTyId {
if !id.is_some() || (id.0 as usize) < InferTyId::BUILTIN_COUNT || depth > 64 {
return id;
}
match src.kind(id) {
InferTyKind::Unknown => InferTyId::UNKNOWN,
InferTyKind::Never => InferTyId::NEVER,
InferTyKind::Prim(p) => InferTyId::from_prim(p),
InferTyKind::Ref { is_mut, inner } => {
let sub = clone_ty_rec(src, inner, dst, depth + 1);
dst.mk_ref(is_mut, sub)
}
InferTyKind::Ptr { is_mut, inner } => {
let sub = clone_ty_rec(src, inner, dst, depth + 1);
dst.mk_ptr(is_mut, sub)
}
InferTyKind::Slice(elem) => {
let sub = clone_ty_rec(src, elem, dst, depth + 1);
dst.mk_slice(sub)
}
InferTyKind::Array { elem, len } => {
let sub = clone_ty_rec(src, elem, dst, depth + 1);
dst.mk_array(sub, len)
}
InferTyKind::Param { idx, def } => dst.mk_param(idx, def),
InferTyKind::Tuple(r) => {
let b = clone_slice(src, r, dst, depth + 1);
dst.mk_tuple(b.as_slice())
}
InferTyKind::Adt { def, args } => {
let b = clone_slice(src, args, dst, depth + 1);
dst.mk_adt(def, b.as_slice())
}
InferTyKind::FnPtr { sig } => {
let buf = clone_slice(src, sig, dst, depth + 1);
let s = buf.as_slice();
dst.mk_fn_ptr(&s[..s.len() - 1], s[s.len() - 1])
}
InferTyKind::FnDef { def, args } => {
let b = clone_slice(src, args, dst, depth + 1);
dst.mk_fn_def(def, b.as_slice())
}
InferTyKind::Closure { def_start, def_end, sig } => {
let buf = clone_slice(src, sig, dst, depth + 1);
let s = buf.as_slice();
dst.mk_closure(def_start, def_end, &s[..s.len() - 1], s[s.len() - 1])
}
InferTyKind::Alias { trait_def, assoc_def, self_ty, args } => {
let ns = clone_ty_rec(src, self_ty, dst, depth + 1);
let buf = clone_slice(src, args, dst, depth + 1);
dst.mk_alias(trait_def, assoc_def, ns, buf.as_slice())
}
InferTyKind::Dyn { principal, args } => {
let buf = clone_slice(src, args, dst, depth + 1);
dst.mk_dyn(principal, buf.as_slice())
}
InferTyKind::Opaque { def, args } => {
let buf = clone_slice(src, args, dst, depth + 1);
dst.mk_opaque(def, buf.as_slice())
}
InferTyKind::Infer(_) => InferTyId::UNKNOWN,
}
}
fn clone_slice(src: &super::arena::InferTyArena, r: TySliceRange, dst: &mut super::arena::InferTyArena, depth: u8) -> super::uf_table::InlineTyBuf {
let mut buf = super::uf_table::InlineTyBuf::new();
for &elem in src.slice(r) {
buf.push(clone_ty_rec(src, elem, dst, depth));
}
buf
}