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-25 08:00:10 -06:00
|
|
|
use crate::{
|
2019-12-26 09:00:10 -06:00
|
|
|
per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId,
|
|
|
|
TraitId,
|
2019-12-25 08:00:10 -06:00
|
|
|
};
|
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:37:07 -06:00
|
|
|
visible: FxHashMap<Name, PerNs>,
|
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
|
|
|
}
|
|
|
|
|
2020-02-21 08:34:06 -06:00
|
|
|
pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
|
2019-12-20 08:43:45 -06:00
|
|
|
BuiltinType::ALL
|
|
|
|
.iter()
|
2019-12-26 09:00:10 -06:00
|
|
|
.map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into(), Visibility::Public)))
|
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 {
|
2020-02-21 08:34:06 -06:00
|
|
|
/// Prefer user-defined modules (or other types) over builtins.
|
2019-12-20 08:43:45 -06:00
|
|
|
Module,
|
2020-02-21 08:34:06 -06:00
|
|
|
/// Prefer builtins over user-defined modules (but not other types).
|
2019-12-20 08:43:45 -06:00
|
|
|
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-22 08:37:07 -06:00
|
|
|
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
|
2019-12-20 08:43:45 -06:00
|
|
|
//FIXME: shadowing
|
2020-02-21 08:34:06 -06:00
|
|
|
self.visible.iter().map(|(n, def)| (n, *def))
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-22 13:12:23 -06:00
|
|
|
pub fn entries_without_primitives<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
|
|
|
|
self.visible.iter().map(|(n, def)| (n, *def))
|
|
|
|
}
|
|
|
|
|
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:37:07 -06:00
|
|
|
self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
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
|
2020-02-21 08:34:06 -06:00
|
|
|
pub(crate) fn get(&self, name: &Name) -> PerNs {
|
|
|
|
self.visible.get(name).copied().unwrap_or_else(PerNs::none)
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
|
2019-12-30 10:31:15 -06:00
|
|
|
for (name, per_ns) in &self.visible {
|
|
|
|
if let Some(vis) = item.match_with(*per_ns) {
|
|
|
|
return Some((name, vis));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:51:43 -06:00
|
|
|
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
|
2019-12-22 08:37:07 -06:00
|
|
|
self.visible.values().filter_map(|def| match 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 13:12:23 -06:00
|
|
|
pub(crate) fn push_res(&mut self, name: Name, def: PerNs) -> bool {
|
2019-12-20 09:47:22 -06:00
|
|
|
let mut changed = false;
|
2020-02-18 06:53:02 -06:00
|
|
|
let existing = self.visible.entry(name).or_default();
|
2019-12-20 09:47:22 -06:00
|
|
|
|
2019-12-22 08:37:07 -06:00
|
|
|
if existing.types.is_none() && def.types.is_some() {
|
|
|
|
existing.types = def.types;
|
2019-12-20 09:47:22 -06:00
|
|
|
changed = true;
|
|
|
|
}
|
2019-12-22 08:37:07 -06:00
|
|
|
if existing.values.is_none() && def.values.is_some() {
|
|
|
|
existing.values = def.values;
|
2019-12-20 09:47:22 -06:00
|
|
|
changed = true;
|
|
|
|
}
|
2019-12-22 08:37:07 -06:00
|
|
|
if existing.macros.is_none() && def.macros.is_some() {
|
|
|
|
existing.macros = def.macros;
|
2019-12-20 09:47:22 -06:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed
|
|
|
|
}
|
|
|
|
|
2019-12-25 11:05:16 -06:00
|
|
|
pub(crate) fn resolutions<'a>(&'a self) -> impl Iterator<Item = (Name, PerNs)> + 'a {
|
2020-02-18 07:32:19 -06:00
|
|
|
self.visible.iter().map(|(name, res)| (name.clone(), *res))
|
2019-12-25 08:00:10 -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
|
|
|
}
|
|
|
|
|
2019-12-25 08:00:10 -06:00
|
|
|
impl PerNs {
|
2019-12-26 09:00:10 -06:00
|
|
|
pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs {
|
2019-12-22 08:08:57 -06:00
|
|
|
match def {
|
2019-12-25 08:00:10 -06:00
|
|
|
ModuleDefId::ModuleId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::FunctionId(_) => PerNs::values(def, v),
|
2019-12-22 08:08:57 -06:00
|
|
|
ModuleDefId::AdtId(adt) => match adt {
|
2019-12-25 08:00:10 -06:00
|
|
|
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def, v),
|
|
|
|
AdtId::EnumId(_) => PerNs::types(def, v),
|
2019-12-22 08:08:57 -06:00
|
|
|
},
|
2019-12-25 08:00:10 -06:00
|
|
|
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v),
|
|
|
|
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v),
|
|
|
|
ModuleDefId::TraitId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::TypeAliasId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::BuiltinType(_) => PerNs::types(def, v),
|
2019-12-22 08:08:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
|
2019-12-30 15:51:37 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
2019-12-30 10:31:15 -06:00
|
|
|
pub enum ItemInNs {
|
|
|
|
Types(ModuleDefId),
|
|
|
|
Values(ModuleDefId),
|
|
|
|
Macros(MacroDefId),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemInNs {
|
|
|
|
fn match_with(self, per_ns: PerNs) -> Option<Visibility> {
|
|
|
|
match self {
|
|
|
|
ItemInNs::Types(def) => {
|
|
|
|
per_ns.types.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
ItemInNs::Values(def) => {
|
|
|
|
per_ns.values.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
ItemInNs::Macros(def) => {
|
|
|
|
per_ns.macros.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 14:33:25 -06:00
|
|
|
|
|
|
|
pub fn as_module_def_id(self) -> Option<ModuleDefId> {
|
|
|
|
match self {
|
2020-01-10 11:40:45 -06:00
|
|
|
ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
|
2019-12-30 14:33:25 -06:00
|
|
|
ItemInNs::Macros(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|