2019-01-19 14:23:26 -06:00
|
|
|
//! Name resolution.
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
|
|
|
use crate::{
|
2019-01-26 15:52:04 -06:00
|
|
|
ModuleDef, Module,
|
|
|
|
db::HirDatabase,
|
|
|
|
name::{Name, KnownName},
|
|
|
|
nameres::{PerNs, ItemMap},
|
2019-01-19 14:23:26 -06:00
|
|
|
generics::GenericParams,
|
2019-01-29 13:49:31 -06:00
|
|
|
expr::{scope::{ExprScopes, ScopeId}, PatId, Body},
|
2019-01-19 14:23:26 -06:00
|
|
|
impl_block::ImplBlock,
|
|
|
|
path::Path,
|
|
|
|
};
|
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2019-01-19 14:23:26 -06:00
|
|
|
pub struct Resolver {
|
2019-01-26 15:52:04 -06:00
|
|
|
scopes: Vec<Scope>,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO how to store these best
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ModuleItemMap {
|
|
|
|
item_map: Arc<ItemMap>,
|
2019-01-26 15:52:04 -06:00
|
|
|
module: Module,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ExprScope {
|
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) enum Scope {
|
|
|
|
/// All the items and imported names of a module
|
|
|
|
ModuleScope(ModuleItemMap),
|
|
|
|
/// Brings the generic parameters of an item into scope
|
|
|
|
GenericParams(Arc<GenericParams>),
|
|
|
|
/// Brings `Self` into scope
|
|
|
|
ImplBlockScope(ImplBlock),
|
|
|
|
/// Local bindings
|
|
|
|
ExprScope(ExprScope),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Resolution {
|
2019-01-26 15:52:04 -06:00
|
|
|
// FIXME make these tuple variants
|
2019-01-19 14:23:26 -06:00
|
|
|
/// An item
|
2019-01-30 15:41:44 -06:00
|
|
|
Def(ModuleDef),
|
2019-01-19 14:23:26 -06:00
|
|
|
/// A local binding (only value namespace)
|
2019-01-30 15:41:44 -06:00
|
|
|
LocalBinding(PatId),
|
2019-01-19 14:23:26 -06:00
|
|
|
/// A generic parameter
|
2019-01-30 15:41:44 -06:00
|
|
|
GenericParam(u32),
|
2019-01-26 15:52:04 -06:00
|
|
|
SelfType(ImplBlock),
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
|
|
|
pub fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
|
2019-01-26 15:52:04 -06:00
|
|
|
let mut resolution = PerNs::none();
|
2019-01-19 14:23:26 -06:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-01-26 15:52:04 -06:00
|
|
|
resolution = resolution.combine(scope.resolve_name(name));
|
|
|
|
if resolution.is_both() {
|
2019-01-19 14:23:26 -06:00
|
|
|
return resolution;
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
resolution
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-01-26 15:52:04 -06:00
|
|
|
pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<Resolution> {
|
|
|
|
if let Some(name) = path.as_ident() {
|
|
|
|
self.resolve_name(name)
|
|
|
|
} else if path.is_self() {
|
|
|
|
self.resolve_name(&Name::self_param())
|
|
|
|
} else {
|
|
|
|
let (item_map, module) = match self.module() {
|
|
|
|
Some(m) => m,
|
|
|
|
_ => return PerNs::none(),
|
|
|
|
};
|
|
|
|
let module_res = item_map.resolve_path(db, module, path);
|
2019-01-30 15:41:44 -06:00
|
|
|
module_res.map(|def| Resolution::Def(def))
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-01-27 13:50:57 -06:00
|
|
|
pub fn all_names(&self) -> FxHashMap<Name, PerNs<Resolution>> {
|
|
|
|
let mut names = FxHashMap::default();
|
|
|
|
for scope in &self.scopes {
|
|
|
|
scope.collect_names(&mut |name, res| {
|
|
|
|
let current: &mut PerNs<Resolution> = names.entry(name).or_default();
|
|
|
|
if current.types.is_none() {
|
|
|
|
current.types = res.types;
|
|
|
|
}
|
|
|
|
if current.values.is_none() {
|
|
|
|
current.values = res.values;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
names
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
|
|
|
|
fn module(&self) -> Option<(&ItemMap, Module)> {
|
2019-01-29 13:49:31 -06:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
|
|
|
Scope::ModuleScope(m) => Some((&*m.item_map, m.module.clone())),
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The body from which any `LocalBinding` resolutions in this resolver come.
|
|
|
|
pub fn body(&self) -> Option<Arc<Body>> {
|
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
|
|
|
Scope::ExprScope(expr_scope) => Some(expr_scope.expr_scopes.body()),
|
|
|
|
_ => None,
|
|
|
|
})
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
|
|
|
pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver {
|
|
|
|
self.scopes.push(scope);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
pub(crate) fn push_generic_params_scope(self, params: Arc<GenericParams>) -> Resolver {
|
|
|
|
self.push_scope(Scope::GenericParams(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver {
|
|
|
|
self.push_scope(Scope::ImplBlockScope(impl_block))
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:52:04 -06:00
|
|
|
pub(crate) fn push_module_scope(self, item_map: Arc<ItemMap>, module: Module) -> Resolver {
|
|
|
|
self.push_scope(Scope::ModuleScope(ModuleItemMap { item_map, module }))
|
2019-01-23 16:08:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_expr_scope(
|
|
|
|
self,
|
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
) -> Resolver {
|
|
|
|
self.push_scope(Scope::ExprScope(ExprScope {
|
|
|
|
expr_scopes,
|
|
|
|
scope_id,
|
|
|
|
}))
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scope {
|
|
|
|
fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
|
2019-01-26 15:52:04 -06:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
|
|
|
if let Some(KnownName::SelfParam) = name.as_known_name() {
|
2019-01-30 15:41:44 -06:00
|
|
|
PerNs::types(Resolution::Def(m.module.into()))
|
2019-01-26 15:52:04 -06:00
|
|
|
} else {
|
|
|
|
match m.item_map[m.module.module_id].get(name) {
|
2019-01-30 15:41:44 -06:00
|
|
|
Some(res) => res.def.map(Resolution::Def),
|
2019-01-26 15:52:04 -06:00
|
|
|
None => PerNs::none(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::GenericParams(gp) => match gp.find_by_name(name) {
|
2019-01-30 15:41:44 -06:00
|
|
|
Some(gp) => PerNs::types(Resolution::GenericParam(gp.idx)),
|
2019-01-26 15:52:04 -06:00
|
|
|
None => PerNs::none(),
|
|
|
|
},
|
|
|
|
Scope::ImplBlockScope(i) => {
|
|
|
|
if name.as_known_name() == Some(KnownName::SelfType) {
|
|
|
|
PerNs::types(Resolution::SelfType(i.clone()))
|
|
|
|
} else {
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(e) => {
|
|
|
|
let entry = e
|
|
|
|
.expr_scopes
|
|
|
|
.entries(e.scope_id)
|
|
|
|
.iter()
|
|
|
|
.find(|entry| entry.name() == name);
|
|
|
|
match entry {
|
2019-01-30 15:41:44 -06:00
|
|
|
Some(e) => PerNs::values(Resolution::LocalBinding(e.pat())),
|
2019-01-26 15:52:04 -06:00
|
|
|
None => PerNs::none(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
|
|
|
|
fn collect_names(&self, f: &mut FnMut(Name, PerNs<Resolution>)) {
|
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-01-27 10:23:49 -06:00
|
|
|
// TODO: should we provide `self` here?
|
|
|
|
// f(
|
|
|
|
// Name::self_param(),
|
|
|
|
// PerNs::types(Resolution::Def {
|
|
|
|
// def: m.module.into(),
|
|
|
|
// }),
|
|
|
|
// );
|
2019-01-27 13:50:57 -06:00
|
|
|
m.item_map[m.module.module_id]
|
|
|
|
.entries()
|
|
|
|
.for_each(|(name, res)| {
|
2019-01-30 15:41:44 -06:00
|
|
|
f(name.clone(), res.def.map(Resolution::Def));
|
2019-01-27 13:50:57 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
Scope::GenericParams(gp) => {
|
|
|
|
for param in &gp.params {
|
|
|
|
f(
|
|
|
|
param.name.clone(),
|
2019-01-30 15:41:44 -06:00
|
|
|
PerNs::types(Resolution::GenericParam(param.idx)),
|
2019-01-27 13:50:57 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(i) => {
|
|
|
|
f(
|
|
|
|
Name::self_type(),
|
|
|
|
PerNs::types(Resolution::SelfType(i.clone())),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Scope::ExprScope(e) => {
|
|
|
|
e.expr_scopes.entries(e.scope_id).iter().for_each(|e| {
|
|
|
|
f(
|
|
|
|
e.name().clone(),
|
2019-01-30 15:41:44 -06:00
|
|
|
PerNs::values(Resolution::LocalBinding(e.pat())),
|
2019-01-27 13:50:57 -06:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|