use super::db::{GlobalDefId, ItemKind, ModuleId, NsMask, VisKind};
use super::mod_tree::ModuleTree;
use crate::sema_fn::norm_ident;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PrimTy {
I8 = 1,
I16 = 2,
I32 = 3,
I64 = 4,
I128 = 5,
Isize = 6,
U8 = 7,
U16 = 8,
U32 = 9,
U64 = 10,
U128 = 11,
Usize = 12,
F32 = 13,
F64 = 14,
Bool = 15,
Char = 16,
Str = 17,
}
impl PrimTy {
pub const ALL: [Self; 17] = [
Self::I8,
Self::I16,
Self::I32,
Self::I64,
Self::I128,
Self::Isize,
Self::U8,
Self::U16,
Self::U32,
Self::U64,
Self::U128,
Self::Usize,
Self::F32,
Self::F64,
Self::Bool,
Self::Char,
Self::Str,
];
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"i8" => Self::I8,
"i16" => Self::I16,
"i32" => Self::I32,
"i64" => Self::I64,
"i128" => Self::I128,
"isize" => Self::Isize,
"u8" => Self::U8,
"u16" => Self::U16,
"u32" => Self::U32,
"u64" => Self::U64,
"u128" => Self::U128,
"usize" => Self::Usize,
"f32" => Self::F32,
"f64" => Self::F64,
"bool" => Self::Bool,
"char" => Self::Char,
"str" => Self::Str,
_ => return None,
})
}
pub const fn from_code(code: u8) -> Option<Self> {
if code >= 1 && code <= 17 {
Some(Self::ALL[(code - 1) as usize])
} else {
None
}
}
pub const fn name(self) -> &'static str {
match self {
Self::I8 => "i8",
Self::I16 => "i16",
Self::I32 => "i32",
Self::I64 => "i64",
Self::I128 => "i128",
Self::Isize => "isize",
Self::U8 => "u8",
Self::U16 => "u16",
Self::U32 => "u32",
Self::U64 => "u64",
Self::U128 => "u128",
Self::Usize => "usize",
Self::F32 => "f32",
Self::F64 => "f64",
Self::Bool => "bool",
Self::Char => "char",
Self::Str => "str",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LocalItemId(pub u32);
impl LocalItemId {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
}
impl Default for LocalItemId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BindingOrigin {
#[default]
Builtin = 0,
Prelude = 1,
ExternPrelude = 2,
Glob = 3,
Explicit = 4,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ResolvedBinding {
pub def: GlobalDefId,
pub local: LocalItemId,
pub local_mod: ModuleId,
pub vis_mod: ModuleId,
pub kind: ItemKind,
pub vis: VisKind,
pub origin: BindingOrigin,
pub prim_ty: u8,
pub ambiguous: bool,
}
impl ResolvedBinding {
#[inline]
pub const fn primitive(p: PrimTy, vis: VisKind, vis_mod: ModuleId, origin: BindingOrigin) -> Self {
Self { def: GlobalDefId::NONE, local: LocalItemId::NONE, local_mod: ModuleId::NONE, vis_mod, kind: ItemKind::TypeAlias, vis, origin, prim_ty: p as u8, ambiguous: false }
}
#[inline]
pub const fn same_target(self, other: Self) -> bool {
if self.prim_ty != 0 || other.prim_ty != 0 {
return self.prim_ty == other.prim_ty;
}
if self.local_mod.is_some() || other.local_mod.is_some() {
return self.local_mod.0 == other.local_mod.0;
}
if self.def.is_some() && other.def.is_some() {
return self.def.krate.0 == other.def.krate.0 && self.def.index == other.def.index;
}
self.local.is_some() && self.local.0 == other.local.0
}
#[inline]
pub const fn rebind(mut self, vis: VisKind, vis_mod: ModuleId, origin: BindingOrigin) -> Self {
self.vis = vis;
self.vis_mod = vis_mod;
self.origin = origin;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PerNs {
pub types: Option<ResolvedBinding>,
pub values: Option<ResolvedBinding>,
pub macros: Option<ResolvedBinding>,
}
impl PerNs {
pub const EMPTY: Self = Self { types: None, values: None, macros: None };
#[inline]
pub const fn is_empty(self) -> bool {
self.types.is_none() && self.values.is_none() && self.macros.is_none()
}
#[inline]
pub fn from_binding(b: ResolvedBinding, mask: NsMask) -> Self {
Self { types: mask.contains(NsMask::TYPE).then_some(b), values: mask.contains(NsMask::VALUE).then_some(b), macros: mask.contains(NsMask::MACRO).then_some(b) }
}
#[inline]
pub const fn rebind(self, vis: VisKind, vis_mod: ModuleId, origin: BindingOrigin) -> Self {
Self {
types: match self.types {
Some(b) => Some(b.rebind(vis, vis_mod, origin)),
None => None,
},
values: match self.values {
Some(b) => Some(b.rebind(vis, vis_mod, origin)),
None => None,
},
macros: match self.macros {
Some(b) => Some(b.rebind(vis, vis_mod, origin)),
None => None,
},
}
}
pub fn merge_explicit(&mut self, incoming: Self) -> bool {
let mut changed = false;
changed |= merge_slot_explicit(&mut self.types, incoming.types);
changed |= merge_slot_explicit(&mut self.values, incoming.values);
changed |= merge_slot_explicit(&mut self.macros, incoming.macros);
changed
}
pub fn merge_glob(&mut self, incoming: Self) -> bool {
let mut changed = false;
changed |= merge_slot_glob(&mut self.types, incoming.types);
changed |= merge_slot_glob(&mut self.values, incoming.values);
changed |= merge_slot_glob(&mut self.macros, incoming.macros);
changed
}
pub fn filter_visible(self, tree: &ModuleTree, from_mod: ModuleId) -> Self {
let keep = |opt: Option<ResolvedBinding>| opt.filter(|b| !b.ambiguous && is_binding_visible(tree, *b, from_mod));
Self { types: keep(self.types), values: keep(self.values), macros: keep(self.macros) }
}
#[inline]
pub fn or_else_ns(self, fallback: Self) -> Self {
Self { types: self.types.or(fallback.types), values: self.values.or(fallback.values), macros: self.macros.or(fallback.macros) }
}
}
fn merge_slot_explicit(slot: &mut Option<ResolvedBinding>, inc: Option<ResolvedBinding>) -> bool {
let Some(mut b) = inc else { return false };
b.origin = BindingOrigin::Explicit;
b.ambiguous = false;
match *slot {
None => {
*slot = Some(b);
true
}
Some(cur) if cur.origin < BindingOrigin::Explicit => {
*slot = Some(b);
true
}
Some(_) => false,
}
}
fn merge_slot_glob(slot: &mut Option<ResolvedBinding>, inc: Option<ResolvedBinding>) -> bool {
let Some(mut b) = inc else { return false };
if b.ambiguous {
return false;
}
b.origin = BindingOrigin::Glob;
match *slot {
None => {
*slot = Some(b);
true
}
Some(cur) if cur.origin < BindingOrigin::Glob => {
*slot = Some(b);
true
}
Some(cur) if cur.origin > BindingOrigin::Glob => false,
Some(mut cur) => {
if cur.same_target(b) {
false
} else if !cur.ambiguous {
cur.ambiguous = true;
*slot = Some(cur);
true
} else {
false
}
}
}
}
#[inline]
pub fn is_descendant_or_eq(tree: &ModuleTree, mut descendant: ModuleId, ancestor: ModuleId) -> bool {
if !ancestor.is_some() {
return true;
}
while descendant.is_some() {
if descendant == ancestor {
return true;
}
descendant = tree.module(descendant).parent;
}
false
}
#[inline]
pub fn is_binding_visible(tree: &ModuleTree, b: ResolvedBinding, from_mod: ModuleId) -> bool {
match b.vis {
VisKind::Public | VisKind::Crate => true,
VisKind::Private | VisKind::Super | VisKind::Restricted => is_descendant_or_eq(tree, from_mod, b.vis_mod),
}
}
pub fn compute_vis_mod(tree: &ModuleTree, cur_mod: ModuleId, vis: VisKind, in_path: Option<&str>) -> (VisKind, ModuleId) {
match vis {
VisKind::Public => (VisKind::Public, ModuleId::NONE),
VisKind::Crate => (VisKind::Crate, ModuleId::ROOT),
VisKind::Private => (VisKind::Private, cur_mod),
VisKind::Super => {
let p = tree.module(cur_mod).parent;
(VisKind::Super, if p.is_some() { p } else { ModuleId::ROOT })
}
VisKind::Restricted => {
let Some(raw) = in_path else { return (VisKind::Private, cur_mod) };
let mut m = cur_mod;
let mut first = true;
for seg in raw.split("::").map(str::trim).filter(|s| !s.is_empty()) {
match seg {
"crate" if first => m = ModuleId::ROOT,
"self" if first => {}
"super" => {
let p = tree.module(m).parent;
m = if p.is_some() { p } else { ModuleId::ROOT };
}
other => match tree.find_child(m, norm_ident(other)) {
Some(c) => m = c,
None => return (VisKind::Private, cur_mod),
},
}
first = false;
}
(VisKind::Restricted, m)
}
}
}