use super::field_item::ItemVis;
use super::ty_prim::TyDefId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UseOwner {
CrateRoot,
Def(u32),
}
impl UseOwner {
#[inline]
pub const fn to_raw(self) -> u32 {
match self {
Self::CrateRoot => 0,
Self::Def(idx) => idx.saturating_add(1),
}
}
#[inline]
pub const fn from_raw(raw: u32) -> Self {
if raw == 0 {
Self::CrateRoot
} else {
Self::Def(raw - 1)
}
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum UsePrimTy {
#[default]
None = 0,
Bool = 1,
Char = 2,
Str = 3,
I8 = 4,
I16 = 5,
I32 = 6,
I64 = 7,
I128 = 8,
Isize = 9,
U8 = 10,
U16 = 11,
U32 = 12,
U64 = 13,
U128 = 14,
Usize = 15,
F16 = 16,
F32 = 17,
F64 = 18,
F128 = 19,
}
impl UsePrimTy {
#[inline]
pub const fn from_u8(v: u8) -> Self {
if v <= 19 {
unsafe { std::mem::transmute::<u8, Self>(v) }
} else {
Self::None
}
}
#[inline]
pub const fn as_str(self) -> Option<&'static str> {
match self {
Self::None => None,
Self::Bool => Some("bool"),
Self::Char => Some("char"),
Self::Str => Some("str"),
Self::I8 => Some("i8"),
Self::I16 => Some("i16"),
Self::I32 => Some("i32"),
Self::I64 => Some("i64"),
Self::I128 => Some("i128"),
Self::Isize => Some("isize"),
Self::U8 => Some("u8"),
Self::U16 => Some("u16"),
Self::U32 => Some("u32"),
Self::U64 => Some("u64"),
Self::U128 => Some("u128"),
Self::Usize => Some("usize"),
Self::F16 => Some("f16"),
Self::F32 => Some("f32"),
Self::F64 => Some("f64"),
Self::F128 => Some("f128"),
}
}
}
pub const USE_FLAG_GLOB: u8 = 1 << 0;
pub const USE_FLAG_REEXPORT: u8 = 1 << 1;
pub const USE_FLAG_EXTERN_CRATE: u8 = 1 << 2;
pub const USE_FLAG_UNDERSCORE: u8 = 1 << 3;
pub const USE_FLAG_PRIM_TY: u8 = 1 << 4;
pub const USE_FLAG_PRELUDE: u8 = 1 << 5;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct UseEntry {
pub owner_raw: u32,
pub def_index: u32,
pub name_offset: u32,
pub type_def: u32,
pub value_def: u32,
pub macro_def: u32,
pub vis: u8,
pub flags: u8,
pub prim_tag: u8,
pub _pad: u8,
}
impl UseEntry {
#[inline]
pub const fn to_le(self) -> Self {
Self {
owner_raw: self.owner_raw.to_le(),
def_index: self.def_index.to_le(),
name_offset: self.name_offset.to_le(),
type_def: self.type_def.to_le(),
value_def: self.value_def.to_le(),
macro_def: self.macro_def.to_le(),
vis: self.vis,
flags: self.flags,
prim_tag: self.prim_tag,
_pad: 0,
}
}
#[inline]
pub const fn from_le(self) -> Self {
Self {
owner_raw: u32::from_le(self.owner_raw),
def_index: u32::from_le(self.def_index),
name_offset: u32::from_le(self.name_offset),
type_def: u32::from_le(self.type_def),
value_def: u32::from_le(self.value_def),
macro_def: u32::from_le(self.macro_def),
vis: self.vis,
flags: self.flags,
prim_tag: self.prim_tag,
_pad: 0,
}
}
#[inline]
pub const fn owner_raw(&self) -> u32 {
u32::from_le(self.owner_raw)
}
#[inline]
pub const fn owner(&self) -> UseOwner {
UseOwner::from_raw(self.owner_raw())
}
#[inline]
pub const fn def_index(&self) -> Option<u32> {
let v = u32::from_le(self.def_index);
if v == u32::MAX {
None
} else {
Some(v)
}
}
#[inline]
pub const fn name_offset(&self) -> u32 {
u32::from_le(self.name_offset)
}
#[inline]
pub const fn type_def(&self) -> Option<TyDefId> {
let v = u32::from_le(self.type_def);
if v == u32::MAX {
None
} else {
Some(TyDefId(v))
}
}
#[inline]
pub const fn value_def(&self) -> Option<TyDefId> {
let v = u32::from_le(self.value_def);
if v == u32::MAX {
None
} else {
Some(TyDefId(v))
}
}
#[inline]
pub const fn macro_def(&self) -> Option<TyDefId> {
let v = u32::from_le(self.macro_def);
if v == u32::MAX {
None
} else {
Some(TyDefId(v))
}
}
#[inline]
pub const fn visibility(&self) -> ItemVis {
ItemVis::from_u8(self.vis)
}
#[inline]
pub const fn prim_ty(&self) -> UsePrimTy {
UsePrimTy::from_u8(self.prim_tag)
}
}