2019-01-19 14:23:26 -06:00
|
|
|
//! Name resolution.
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-05-12 07:58:37 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2019-01-19 14:23:26 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-05-23 13:14:19 -05:00
|
|
|
code_model::Crate,
|
2019-01-26 15:52:04 -06:00
|
|
|
db::HirDatabase,
|
2019-07-04 15:05:17 -05:00
|
|
|
expr::{
|
|
|
|
scope::{ExprScopes, ScopeId},
|
|
|
|
PatId,
|
|
|
|
},
|
2019-01-19 14:23:26 -06:00
|
|
|
generics::GenericParams,
|
|
|
|
impl_block::ImplBlock,
|
2019-07-07 16:29:38 -05:00
|
|
|
name::{Name, SELF_PARAM, SELF_TYPE},
|
2019-07-04 15:05:17 -05:00
|
|
|
nameres::{CrateDefMap, CrateModuleId, PerNs},
|
2019-04-17 16:40:00 -05:00
|
|
|
path::Path,
|
2019-07-04 15:05:17 -05:00
|
|
|
MacroDef, ModuleDef, Trait,
|
2019-01-19 14:23:26 -06:00
|
|
|
};
|
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2019-04-13 03:02:23 -05:00
|
|
|
pub(crate) struct Resolver {
|
2019-01-26 15:52:04 -06:00
|
|
|
scopes: Vec<Scope>,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 02:53:48 -05:00
|
|
|
// FIXME how to store these best
|
2019-01-19 14:23:26 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ModuleItemMap {
|
2019-03-14 04:54:03 -05:00
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-03-16 10:57:53 -05:00
|
|
|
module_id: CrateModuleId,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ExprScope {
|
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:04:14 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2019-02-22 02:15:23 -06:00
|
|
|
pub(crate) struct PathResult {
|
2019-02-21 15:57:07 -06:00
|
|
|
/// The actual path resolution
|
|
|
|
resolution: PerNs<Resolution>,
|
|
|
|
/// The first index in the path that we
|
|
|
|
/// were unable to resolve.
|
|
|
|
/// When path is fully resolved, this is 0.
|
|
|
|
remaining_index: usize,
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PathResult {
|
2019-02-21 15:57:07 -06:00
|
|
|
/// Returns the remaining index in the result
|
|
|
|
/// returns None if the path was fully resolved
|
2019-02-22 02:15:23 -06:00
|
|
|
pub(crate) fn remaining_index(&self) -> Option<usize> {
|
2019-02-21 15:57:07 -06:00
|
|
|
if self.remaining_index > 0 {
|
|
|
|
Some(self.remaining_index)
|
|
|
|
} else {
|
|
|
|
None
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes `PathResult` and returns the contained `PerNs<Resolution>`
|
2019-02-21 15:57:07 -06:00
|
|
|
/// if the path was fully resolved, meaning we have no remaining items
|
2019-02-22 02:15:23 -06:00
|
|
|
pub(crate) fn into_fully_resolved(self) -> PerNs<Resolution> {
|
|
|
|
if self.is_fully_resolved() {
|
2019-02-21 15:57:07 -06:00
|
|
|
self.resolution
|
|
|
|
} else {
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes `PathResult` and returns the resolution and the
|
|
|
|
/// remaining_index as a tuple.
|
2019-02-22 02:15:23 -06:00
|
|
|
pub(crate) fn into_inner(self) -> (PerNs<Resolution>, Option<usize>) {
|
2019-02-21 15:57:07 -06:00
|
|
|
let index = self.remaining_index();
|
|
|
|
(self.resolution, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Path is fully resolved when `remaining_index` is none
|
|
|
|
/// and the resolution contains anything
|
2019-02-22 02:15:23 -06:00
|
|
|
pub(crate) fn is_fully_resolved(&self) -> bool {
|
2019-02-21 15:57:07 -06:00
|
|
|
!self.resolution.is_none() && self.remaining_index().is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty() -> PathResult {
|
|
|
|
PathResult { resolution: PerNs::none(), remaining_index: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_resolution(res: PerNs<Resolution>) -> PathResult {
|
|
|
|
PathResult::from_resolution_with_index(res, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_resolution_with_index(res: PerNs<Resolution>, remaining_index: usize) -> PathResult {
|
|
|
|
if res.is_none() {
|
|
|
|
PathResult::empty()
|
|
|
|
} else {
|
|
|
|
PathResult { resolution: res, remaining_index }
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 14:23:26 -06:00
|
|
|
#[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 {
|
|
|
|
/// 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 {
|
2019-04-13 03:02:23 -05:00
|
|
|
pub(crate) fn resolve_name(&self, db: &impl HirDatabase, 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-02-10 13:44:34 -06:00
|
|
|
resolution = resolution.or(scope.resolve_name(db, name));
|
2019-09-09 07:54:02 -05:00
|
|
|
if resolution.is_all() {
|
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-06-08 12:42:02 -05:00
|
|
|
pub(crate) fn resolve_path_as_macro(
|
2019-06-01 06:34:19 -05:00
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
2019-06-08 12:42:02 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Option<MacroDef> {
|
|
|
|
let (item_map, module) = self.module()?;
|
2019-09-09 07:54:02 -05:00
|
|
|
item_map.resolve_path(db, module, path).0.get_macros()
|
2019-04-15 05:01:29 -05:00
|
|
|
}
|
|
|
|
|
2019-02-22 02:15:23 -06:00
|
|
|
/// Returns the resolved path segments
|
|
|
|
/// Which may be fully resolved, empty or partially resolved.
|
|
|
|
pub(crate) fn resolve_path_segments(&self, db: &impl HirDatabase, path: &Path) -> PathResult {
|
2019-01-26 15:52:04 -06:00
|
|
|
if let Some(name) = path.as_ident() {
|
2019-02-21 15:57:07 -06:00
|
|
|
PathResult::from_resolution(self.resolve_name(db, name))
|
2019-01-26 15:52:04 -06:00
|
|
|
} else if path.is_self() {
|
2019-07-07 16:29:38 -05:00
|
|
|
PathResult::from_resolution(self.resolve_name(db, &SELF_PARAM))
|
2019-01-26 15:52:04 -06:00
|
|
|
} else {
|
|
|
|
let (item_map, module) = match self.module() {
|
2019-06-08 06:55:25 -05:00
|
|
|
Some(it) => it,
|
|
|
|
None => return PathResult::empty(),
|
2019-01-26 15:52:04 -06:00
|
|
|
};
|
2019-02-21 04:04:14 -06:00
|
|
|
let (module_res, segment_index) = item_map.resolve_path(db, module, path);
|
|
|
|
|
|
|
|
let def = module_res.map(Resolution::Def);
|
|
|
|
|
|
|
|
if let Some(index) = segment_index {
|
2019-02-21 15:57:07 -06:00
|
|
|
PathResult::from_resolution_with_index(def, index)
|
2019-02-21 04:04:14 -06:00
|
|
|
} else {
|
2019-02-21 15:57:07 -06:00
|
|
|
PathResult::from_resolution(def)
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-02-22 02:15:23 -06:00
|
|
|
/// Returns the fully resolved path if we were able to resolve it.
|
|
|
|
/// otherwise returns `PerNs::none`
|
2019-06-08 10:38:14 -05:00
|
|
|
pub(crate) fn resolve_path_without_assoc_items(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
path: &Path,
|
|
|
|
) -> PerNs<Resolution> {
|
2019-02-22 02:15:23 -06:00
|
|
|
// into_fully_resolved() returns the fully resolved path or PerNs::none() otherwise
|
|
|
|
self.resolve_path_segments(db, path).into_fully_resolved()
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:02:23 -05:00
|
|
|
pub(crate) fn all_names(&self, db: &impl HirDatabase) -> FxHashMap<Name, PerNs<Resolution>> {
|
2019-01-27 13:50:57 -06:00
|
|
|
let mut names = FxHashMap::default();
|
2019-02-01 16:06:57 -06:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-02-13 13:53:42 -06:00
|
|
|
scope.collect_names(db, &mut |name, res| {
|
2019-01-27 13:50:57 -06:00
|
|
|
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;
|
|
|
|
}
|
2019-09-09 07:54:02 -05:00
|
|
|
if current.macros.is_none() {
|
|
|
|
current.macros = res.macros;
|
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
names
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
|
2019-05-12 07:58:37 -05:00
|
|
|
pub(crate) fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<Trait> {
|
|
|
|
let mut traits = FxHashSet::default();
|
|
|
|
for scope in &self.scopes {
|
|
|
|
if let Scope::ModuleScope(m) = scope {
|
|
|
|
if let Some(prelude) = m.crate_def_map.prelude() {
|
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
|
|
|
traits.extend(prelude_def_map[prelude.module_id].scope.traits());
|
2019-03-24 11:36:15 -05:00
|
|
|
}
|
2019-05-12 07:58:37 -05:00
|
|
|
traits.extend(m.crate_def_map[m.module_id].scope.traits());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traits
|
2019-03-24 11:36:15 -05:00
|
|
|
}
|
|
|
|
|
2019-04-18 13:35:47 -05:00
|
|
|
fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
|
2019-01-29 13:49:31 -06:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
2019-03-14 04:54:03 -05:00
|
|
|
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
|
2019-01-29 13:49:31 -06:00
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 16:40:00 -05:00
|
|
|
|
|
|
|
pub(crate) fn krate(&self) -> Option<Crate> {
|
|
|
|
self.module().map(|t| t.0.krate())
|
|
|
|
}
|
2019-07-06 09:41:04 -05:00
|
|
|
|
|
|
|
pub(crate) fn where_predicates_in_scope<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a {
|
|
|
|
self.scopes
|
|
|
|
.iter()
|
|
|
|
.filter_map(|scope| match scope {
|
|
|
|
Scope::GenericParams(params) => Some(params),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flat_map(|params| params.where_predicates.iter())
|
|
|
|
}
|
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-03-14 04:54:03 -05:00
|
|
|
pub(crate) fn push_module_scope(
|
|
|
|
self,
|
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-03-16 10:57:53 -05:00
|
|
|
module_id: CrateModuleId,
|
2019-03-14 04:54:03 -05:00
|
|
|
) -> Resolver {
|
|
|
|
self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
|
2019-01-23 16:08:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_expr_scope(
|
|
|
|
self,
|
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
) -> Resolver {
|
2019-02-08 05:49:43 -06:00
|
|
|
self.push_scope(Scope::ExprScope(ExprScope { expr_scopes, scope_id }))
|
2019-01-23 16:08:41 -06:00
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scope {
|
2019-02-10 13:44:34 -06:00
|
|
|
fn resolve_name(&self, db: &impl HirDatabase, name: &Name) -> PerNs<Resolution> {
|
2019-01-26 15:52:04 -06:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-07-07 16:29:38 -05:00
|
|
|
if name == &SELF_PARAM {
|
2019-03-14 04:54:03 -05:00
|
|
|
PerNs::types(Resolution::Def(m.crate_def_map.mk_module(m.module_id).into()))
|
2019-01-26 15:52:04 -06:00
|
|
|
} else {
|
2019-03-14 04:54:03 -05:00
|
|
|
m.crate_def_map
|
|
|
|
.resolve_name_in_module(db, m.module_id, name)
|
|
|
|
.map(Resolution::Def)
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) => {
|
2019-07-07 16:29:38 -05:00
|
|
|
if name == &SELF_TYPE {
|
2019-07-04 12:26:44 -05:00
|
|
|
PerNs::types(Resolution::SelfType(*i))
|
2019-01-26 15:52:04 -06:00
|
|
|
} else {
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(e) => {
|
2019-02-08 05:49:43 -06:00
|
|
|
let entry =
|
|
|
|
e.expr_scopes.entries(e.scope_id).iter().find(|entry| entry.name() == name);
|
2019-01-26 15:52:04 -06:00
|
|
|
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
|
|
|
|
2019-02-13 13:53:42 -06:00
|
|
|
fn collect_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, PerNs<Resolution>)) {
|
2019-01-27 13:50:57 -06:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-03-23 02:53:48 -05:00
|
|
|
// FIXME: should we provide `self` here?
|
2019-01-27 10:23:49 -06:00
|
|
|
// f(
|
|
|
|
// Name::self_param(),
|
|
|
|
// PerNs::types(Resolution::Def {
|
|
|
|
// def: m.module.into(),
|
|
|
|
// }),
|
|
|
|
// );
|
2019-03-14 04:54:03 -05:00
|
|
|
m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| {
|
2019-02-08 05:49:43 -06:00
|
|
|
f(name.clone(), res.def.map(Resolution::Def));
|
|
|
|
});
|
2019-09-09 07:54:02 -05:00
|
|
|
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
|
|
|
|
f(name.clone(), PerNs::macros(macro_));
|
|
|
|
});
|
2019-03-14 04:54:03 -05:00
|
|
|
m.crate_def_map.extern_prelude().iter().for_each(|(name, def)| {
|
2019-02-04 15:09:56 -06:00
|
|
|
f(name.clone(), PerNs::types(Resolution::Def(*def)));
|
|
|
|
});
|
2019-03-14 04:54:03 -05:00
|
|
|
if let Some(prelude) = m.crate_def_map.prelude() {
|
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
|
|
|
prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
|
2019-02-13 13:53:42 -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 {
|
2019-02-08 05:49:43 -06:00
|
|
|
f(param.name.clone(), PerNs::types(Resolution::GenericParam(param.idx)))
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(i) => {
|
2019-07-07 16:29:38 -05:00
|
|
|
f(SELF_TYPE, PerNs::types(Resolution::SelfType(*i)));
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
|
|
|
Scope::ExprScope(e) => {
|
|
|
|
e.expr_scopes.entries(e.scope_id).iter().for_each(|e| {
|
2019-02-08 05:49:43 -06:00
|
|
|
f(e.name().clone(), PerNs::values(Resolution::LocalBinding(e.pat())));
|
2019-01-27 13:50:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|