use super::TyId;
pub const FIELD_FLAG_TUPLE: u8 = 1 << 0;
pub const FIELD_FLAG_UNSAFE: u8 = 1 << 1;
pub const DEF_TY_FLAG_NON_EXHAUSTIVE: u8 = 1 << 0;
pub const DEF_TY_FLAG_MUT_STATIC: u8 = 1 << 1;
pub const DEF_TY_FLAG_UNSAFE_STATIC: u8 = 1 << 2;
pub const DEF_TY_FLAG_FUNDAMENTAL: u8 = 1 << 3;
pub const DEF_TY_FLAG_DEFAULT_ASSOC: u8 = 1 << 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(u8)]
pub enum ItemVis {
#[default]
Public = 0,
Crate = 1,
Restricted = 2,
Private = 3,
}
impl ItemVis {
#[inline]
pub const fn from_u8(v: u8) -> Self {
match v {
0 => Self::Public,
1 => Self::Crate,
2 => Self::Restricted,
_ => Self::Private,
}
}
#[inline]
pub const fn is_public(self) -> bool {
matches!(self, Self::Public)
}
#[inline]
pub const fn is_crate_visible(self) -> bool {
matches!(self, Self::Public | Self::Crate)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(u8)]
pub enum CtorShape {
#[default]
None = 0,
Record = 1,
Tuple = 2,
Unit = 3,
}
impl CtorShape {
#[inline]
pub const fn from_u8(v: u8) -> Self {
match v {
1 => Self::Record,
2 => Self::Tuple,
3 => Self::Unit,
_ => Self::None,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct FieldEntry {
pub owner_def_index: u32,
pub def_index: u32,
pub name_offset: u32,
pub ty: u32,
pub field_idx: u16,
pub vis: u8,
pub flags: u8,
}
impl FieldEntry {
#[inline]
pub const fn to_le(self) -> Self {
Self {
owner_def_index: self.owner_def_index.to_le(),
def_index: self.def_index.to_le(),
name_offset: self.name_offset.to_le(),
ty: self.ty.to_le(),
field_idx: self.field_idx.to_le(),
vis: self.vis,
flags: self.flags,
}
}
#[inline]
pub const fn from_le(self) -> Self {
Self {
owner_def_index: u32::from_le(self.owner_def_index),
def_index: u32::from_le(self.def_index),
name_offset: u32::from_le(self.name_offset),
ty: u32::from_le(self.ty),
field_idx: u16::from_le(self.field_idx),
vis: self.vis,
flags: self.flags,
}
}
#[inline]
pub const fn owner_def_index(&self) -> u32 {
u32::from_le(self.owner_def_index)
}
#[inline]
pub const fn def_index_opt(&self) -> Option<u32> {
let idx = u32::from_le(self.def_index);
if idx == u32::MAX {
None
} else {
Some(idx)
}
}
#[inline]
pub const fn ty_id(&self) -> TyId {
TyId(u32::from_le(self.ty))
}
#[inline]
pub const fn field_idx(&self) -> u16 {
u16::from_le(self.field_idx)
}
#[inline]
pub const fn visibility(&self) -> ItemVis {
ItemVis::from_u8(self.vis)
}
#[inline]
pub const fn is_tuple_field(&self) -> bool {
(self.flags & FIELD_FLAG_TUPLE) != 0
}
#[inline]
pub const fn is_unsafe(&self) -> bool {
(self.flags & FIELD_FLAG_UNSAFE) != 0
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DefTyEntry {
pub def_index: u32,
pub ty: u32,
pub parent_def_index: u32,
pub variant_idx: u16,
pub ctor_shape: u8,
pub flags: u8,
}
impl DefTyEntry {
#[inline]
pub const fn to_le(self) -> Self {
Self {
def_index: self.def_index.to_le(),
ty: self.ty.to_le(),
parent_def_index: self.parent_def_index.to_le(),
variant_idx: self.variant_idx.to_le(),
ctor_shape: self.ctor_shape,
flags: self.flags,
}
}
#[inline]
pub const fn from_le(self) -> Self {
Self {
def_index: u32::from_le(self.def_index),
ty: u32::from_le(self.ty),
parent_def_index: u32::from_le(self.parent_def_index),
variant_idx: u16::from_le(self.variant_idx),
ctor_shape: self.ctor_shape,
flags: self.flags,
}
}
#[inline]
pub const fn def_index(&self) -> u32 {
u32::from_le(self.def_index)
}
#[inline]
pub const fn ty_id(&self) -> TyId {
TyId(u32::from_le(self.ty))
}
#[inline]
pub const fn parent_def_index_opt(&self) -> Option<u32> {
let p = u32::from_le(self.parent_def_index);
if p == u32::MAX {
None
} else {
Some(p)
}
}
#[inline]
pub const fn variant_idx_opt(&self) -> Option<u16> {
let v = u16::from_le(self.variant_idx);
if v == u16::MAX {
None
} else {
Some(v)
}
}
#[inline]
pub const fn ctor_shape(&self) -> CtorShape {
CtorShape::from_u8(self.ctor_shape)
}
}