use super::ty_prim::{TyDefId, TyId};
pub const GENERIC_OWNER_IMPL_BIT: u32 = 1 << 31;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GenericOwner {
Def(u32),
Impl(u32),
}
impl GenericOwner {
#[inline]
pub const fn to_raw(self) -> u32 {
match self {
Self::Def(idx) => idx & !GENERIC_OWNER_IMPL_BIT,
Self::Impl(idx) => GENERIC_OWNER_IMPL_BIT | (idx & !GENERIC_OWNER_IMPL_BIT),
}
}
#[inline]
pub const fn from_raw(raw: u32) -> Self {
if (raw & GENERIC_OWNER_IMPL_BIT) != 0 {
Self::Impl(raw & !GENERIC_OWNER_IMPL_BIT)
} else {
Self::Def(raw)
}
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GenericParamKind {
#[default]
Lifetime = 0,
Type = 1,
Const = 2,
}
impl GenericParamKind {
#[inline]
pub const fn from_u8(v: u8) -> Self {
match v {
0 => Self::Lifetime,
1 => Self::Type,
2 => Self::Const,
_ => Self::Type,
}
}
}
pub const GENERIC_PARAM_FLAG_HAS_DEFAULT: u8 = 1 << 0;
pub const GENERIC_PARAM_FLAG_SYNTHETIC: u8 = 1 << 1;
pub const GENERIC_PARAM_FLAG_IS_SELF: u8 = 1 << 2;
pub const GENERIC_PARAM_FLAG_SIZED: u8 = 1 << 3;
pub const GENERIC_PARAM_FLAG_HOST_EFFECT: u8 = 1 << 4;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct GenericParamEntry {
pub owner_key: u32,
pub def_index: u32,
pub name_offset: u32,
pub ty_or_default: u32,
pub param_idx: u16,
pub kind: u8,
pub flags: u8,
}
impl GenericParamEntry {
#[inline]
pub const fn to_le(self) -> Self {
Self {
owner_key: self.owner_key.to_le(),
def_index: self.def_index.to_le(),
name_offset: self.name_offset.to_le(),
ty_or_default: self.ty_or_default.to_le(),
param_idx: self.param_idx.to_le(),
kind: self.kind,
flags: self.flags,
}
}
#[inline]
pub const fn from_le(self) -> Self {
Self {
owner_key: u32::from_le(self.owner_key),
def_index: u32::from_le(self.def_index),
name_offset: u32::from_le(self.name_offset),
ty_or_default: u32::from_le(self.ty_or_default),
param_idx: u16::from_le(self.param_idx),
kind: self.kind,
flags: self.flags,
}
}
#[inline]
pub const fn owner_key(&self) -> u32 {
u32::from_le(self.owner_key)
}
#[inline]
pub const fn param_idx(&self) -> u16 {
u16::from_le(self.param_idx)
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum WherePredKind {
#[default]
TraitBound = 0,
ProjectionEq = 1,
TypeOutlives = 2,
RegionOutlives = 3,
}
impl WherePredKind {
#[inline]
pub const fn from_u8(v: u8) -> Self {
match v {
0 => Self::TraitBound,
1 => Self::ProjectionEq,
2 => Self::TypeOutlives,
3 => Self::RegionOutlives,
_ => Self::TraitBound,
}
}
}
pub const WHERE_FLAG_HRTB: u8 = 1 << 0;
pub const WHERE_FLAG_NEGATIVE: u8 = 1 << 1;
pub const WHERE_FLAG_CONST: u8 = 1 << 2;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct WherePredEntry {
pub owner_key: u32,
pub lhs_ty: u32,
pub trait_def: u32,
pub trait_args_start: u32,
pub rhs_or_region: u32,
pub assoc_name_offset: u32,
pub trait_args_len: u16,
pub kind: u8,
pub flags: u8,
}
impl WherePredEntry {
#[inline]
pub const fn to_le(self) -> Self {
Self {
owner_key: self.owner_key.to_le(),
lhs_ty: self.lhs_ty.to_le(),
trait_def: self.trait_def.to_le(),
trait_args_start: self.trait_args_start.to_le(),
rhs_or_region: self.rhs_or_region.to_le(),
assoc_name_offset: self.assoc_name_offset.to_le(),
trait_args_len: self.trait_args_len.to_le(),
kind: self.kind,
flags: self.flags,
}
}
#[inline]
pub const fn from_le(self) -> Self {
Self {
owner_key: u32::from_le(self.owner_key),
lhs_ty: u32::from_le(self.lhs_ty),
trait_def: u32::from_le(self.trait_def),
trait_args_start: u32::from_le(self.trait_args_start),
rhs_or_region: u32::from_le(self.rhs_or_region),
assoc_name_offset: u32::from_le(self.assoc_name_offset),
trait_args_len: u16::from_le(self.trait_args_len),
kind: self.kind,
flags: self.flags,
}
}
#[inline]
pub const fn owner_key(&self) -> u32 {
u32::from_le(self.owner_key)
}
#[inline]
pub const fn trait_def_opt(&self) -> Option<TyDefId> {
let d = u32::from_le(self.trait_def);
if d == u32::MAX {
None
} else {
Some(TyDefId(d))
}
}
#[inline]
pub const fn lhs_ty_id(&self) -> TyId {
TyId(u32::from_le(self.lhs_ty))
}
}
const _: () = {
assert!(std::mem::size_of::<GenericParamEntry>() == 20);
assert!(std::mem::align_of::<GenericParamEntry>() == 4);
assert!(std::mem::size_of::<WherePredEntry>() == 28);
assert!(std::mem::align_of::<WherePredEntry>() == 4);
};