rust/crates/ra_hir_def/src/item_scope.rs

166 lines
6.0 KiB
Rust
Raw Normal View History

2019-12-20 08:44:40 -06:00
//! Describes items defined or visible (ie, imported) in a certain scope.
//! This is shared between modules and blocks.
2019-12-20 08:43:45 -06:00
use hir_expand::name::Name;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
2019-12-20 08:38:17 -06:00
2019-12-22 08:08:57 -06:00
use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
2019-12-20 08:43:45 -06:00
#[derive(Debug, Default, PartialEq, Eq)]
2019-12-20 08:45:12 -06:00
pub struct ItemScope {
2019-12-22 08:04:31 -06:00
visible: FxHashMap<Name, Resolution>,
2019-12-22 08:21:48 -06:00
defs: Vec<ModuleDefId>,
2019-12-20 09:55:38 -06:00
impls: Vec<ImplId>,
2019-12-20 08:44:40 -06:00
/// Macros visible in current module in legacy textual scope
2019-12-20 08:43:45 -06:00
///
2019-12-20 08:44:40 -06:00
/// For macros invoked by an unqualified identifier like `bar!()`, `legacy_macros` will be searched in first.
2019-12-20 08:43:45 -06:00
/// If it yields no result, then it turns to module scoped `macros`.
2019-12-20 08:44:40 -06:00
/// It macros with name qualified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
2019-12-20 08:43:45 -06:00
/// and only normal scoped `macros` will be searched in.
///
/// Note that this automatically inherit macros defined textually before the definition of module itself.
///
/// Module scoped macros will be inserted into `items` instead of here.
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
// be all resolved to the last one defined if shadowing happens.
2019-12-20 10:09:13 -06:00
legacy_macros: FxHashMap<Name, MacroDefId>,
2019-12-20 08:43:45 -06:00
}
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
BuiltinType::ALL
.iter()
2019-12-22 08:28:55 -06:00
.map(|(name, ty)| (name.clone(), Resolution { def: PerNs::types(ty.clone().into()) }))
2019-12-20 08:43:45 -06:00
.collect()
});
/// Shadow mode for builtin type which can be shadowed by module.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2019-12-20 08:51:43 -06:00
pub(crate) enum BuiltinShadowMode {
2019-12-20 08:43:45 -06:00
// Prefer Module
Module,
// Prefer Other Types
Other,
}
/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
/// Other methods will only resolve values, types and module scoped macros only.
2019-12-20 08:45:12 -06:00
impl ItemScope {
2019-12-20 08:43:45 -06:00
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
//FIXME: shadowing
2019-12-22 08:04:31 -06:00
self.visible.iter().chain(BUILTIN_SCOPE.iter())
2019-12-20 08:43:45 -06:00
}
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
2019-12-22 08:21:48 -06:00
self.defs.iter().copied()
2019-12-20 08:43:45 -06:00
}
2019-12-20 08:58:20 -06:00
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
self.impls.iter().copied()
}
2019-12-20 08:43:45 -06:00
/// Iterate over all module scoped macros
2019-12-20 08:51:43 -06:00
pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
2019-12-22 08:04:31 -06:00
self.visible
2019-12-20 08:43:45 -06:00
.iter()
.filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
}
2019-12-20 08:51:43 -06:00
/// Iterate over all legacy textual scoped macros visible at the end of the module
pub(crate) fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
2019-12-20 08:43:45 -06:00
self.legacy_macros.iter().map(|(name, def)| (name, *def))
}
/// Get a name from current module scope, legacy macros are not included
2019-12-20 08:51:43 -06:00
pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
2019-12-20 08:43:45 -06:00
match shadow {
2019-12-22 08:04:31 -06:00
BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
2019-12-20 08:43:45 -06:00
BuiltinShadowMode::Other => {
2019-12-22 08:04:31 -06:00
let item = self.visible.get(name);
2019-12-20 08:43:45 -06:00
if let Some(res) = item {
if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
return BUILTIN_SCOPE.get(name).or(item);
}
}
item.or_else(|| BUILTIN_SCOPE.get(name))
}
}
}
2019-12-20 08:51:43 -06:00
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
2019-12-22 08:04:31 -06:00
self.visible.values().filter_map(|r| match r.def.take_types() {
2019-12-20 08:43:45 -06:00
Some(ModuleDefId::TraitId(t)) => Some(t),
_ => None,
})
}
2019-12-22 08:21:48 -06:00
pub(crate) fn define_def(&mut self, def: ModuleDefId) {
self.defs.push(def)
}
2019-12-20 08:43:45 -06:00
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
self.legacy_macros.get(name).copied()
}
2019-12-20 09:47:22 -06:00
2019-12-20 09:55:38 -06:00
pub(crate) fn define_impl(&mut self, imp: ImplId) {
self.impls.push(imp)
}
2019-12-20 10:09:13 -06:00
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
self.legacy_macros.insert(name, mac);
}
2019-12-22 08:31:30 -06:00
pub(crate) fn push_res(&mut self, name: Name, res: &Resolution) -> bool {
2019-12-20 09:47:22 -06:00
let mut changed = false;
2019-12-22 08:04:31 -06:00
let existing = self.visible.entry(name.clone()).or_default();
2019-12-20 09:47:22 -06:00
if existing.def.types.is_none() && res.def.types.is_some() {
existing.def.types = res.def.types;
changed = true;
}
if existing.def.values.is_none() && res.def.values.is_some() {
existing.def.values = res.def.values;
changed = true;
}
if existing.def.macros.is_none() && res.def.macros.is_some() {
existing.def.macros = res.def.macros;
changed = true;
}
changed
}
pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> {
2019-12-22 08:04:31 -06:00
self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
2019-12-20 09:47:22 -06:00
}
2019-12-20 10:09:13 -06:00
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
self.legacy_macros.clone()
}
2019-12-20 08:43:45 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Resolution {
/// None for unresolved
pub def: PerNs,
}
2019-12-22 08:08:57 -06:00
impl From<ModuleDefId> for PerNs {
fn from(def: ModuleDefId) -> PerNs {
match def {
ModuleDefId::ModuleId(_) => PerNs::types(def),
ModuleDefId::FunctionId(_) => PerNs::values(def),
ModuleDefId::AdtId(adt) => match adt {
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
AdtId::EnumId(_) => PerNs::types(def),
},
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
ModuleDefId::TraitId(_) => PerNs::types(def),
ModuleDefId::TypeAliasId(_) => PerNs::types(def),
ModuleDefId::BuiltinType(_) => PerNs::types(def),
}
}
}