2019-01-19 20:23:26 +00:00
|
|
|
//! Name resolution.
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-10-30 14:19:30 +00:00
|
|
|
use hir_def::{
|
2019-10-31 07:51:54 +00:00
|
|
|
builtin_type::BuiltinType,
|
2019-11-21 09:21:46 +00:00
|
|
|
db::DefDatabase2,
|
|
|
|
generics::GenericParams,
|
2019-10-31 15:45:10 +00:00
|
|
|
nameres::CrateDefMap,
|
2019-10-30 14:24:36 +00:00
|
|
|
path::{Path, PathKind},
|
2019-11-21 09:21:46 +00:00
|
|
|
AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId,
|
|
|
|
StructId, TraitId, TypeAliasId,
|
2019-10-30 14:19:30 +00:00
|
|
|
};
|
2019-10-30 15:56:20 +00:00
|
|
|
use hir_expand::name::{self, Name};
|
2019-09-12 20:35:53 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-01-19 20:23:26 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-05-23 18:14:19 +00:00
|
|
|
code_model::Crate,
|
2019-11-21 09:21:46 +00:00
|
|
|
db::DefDatabase,
|
2019-11-14 08:56:13 +00:00
|
|
|
expr::{ExprScopes, PatId, ScopeId},
|
2019-11-21 09:21:46 +00:00
|
|
|
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local,
|
2019-11-20 19:00:57 +00:00
|
|
|
MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
|
2019-01-19 20:23:26 +00:00
|
|
|
};
|
|
|
|
|
2019-01-23 22:08:41 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2019-04-13 08:02:23 +00:00
|
|
|
pub(crate) struct Resolver {
|
2019-01-26 21:52:04 +00:00
|
|
|
scopes: Vec<Scope>,
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 07:53:48 +00:00
|
|
|
// FIXME how to store these best
|
2019-01-19 20:23:26 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ModuleItemMap {
|
2019-03-14 09:54:03 +00:00
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ExprScope {
|
2019-11-21 09:21:46 +00:00
|
|
|
owner: DefWithBodyId,
|
2019-01-19 20:23:26 +00:00
|
|
|
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
|
2019-11-21 09:21:46 +00:00
|
|
|
GenericParams { def: GenericDefId, params: Arc<GenericParams> },
|
2019-09-27 04:19:52 +00:00
|
|
|
/// Brings `Self` in `impl` block into scope
|
2019-11-21 09:21:46 +00:00
|
|
|
ImplBlockScope(ImplId),
|
2019-10-08 11:25:37 +00:00
|
|
|
/// Brings `Self` in enum, struct and union definitions into scope
|
2019-11-21 09:21:46 +00:00
|
|
|
AdtScope(AdtId),
|
2019-01-19 20:23:26 +00:00
|
|
|
/// Local bindings
|
|
|
|
ExprScope(ExprScope),
|
|
|
|
}
|
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-15 09:24:30 +00:00
|
|
|
pub(crate) enum TypeNs {
|
2019-11-21 09:21:46 +00:00
|
|
|
SelfType(ImplId),
|
2019-09-12 20:35:53 +00:00
|
|
|
GenericParam(u32),
|
2019-11-21 09:21:46 +00:00
|
|
|
AdtId(AdtId),
|
|
|
|
AdtSelfType(AdtId),
|
|
|
|
EnumVariantId(EnumVariantId),
|
|
|
|
TypeAliasId(TypeAliasId),
|
2019-09-12 20:35:53 +00:00
|
|
|
BuiltinType(BuiltinType),
|
2019-11-21 09:21:46 +00:00
|
|
|
TraitId(TraitId),
|
2019-09-14 14:26:03 +00:00
|
|
|
// Module belong to type ns, but the resolver is used when all module paths
|
2019-09-12 20:35:53 +00:00
|
|
|
// are fully resolved.
|
2019-11-21 09:21:46 +00:00
|
|
|
// ModuleId(ModuleId)
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-15 09:24:30 +00:00
|
|
|
pub(crate) enum ResolveValueResult {
|
2019-09-12 20:35:53 +00:00
|
|
|
ValueNs(ValueNs),
|
|
|
|
Partial(TypeNs, usize),
|
|
|
|
}
|
2019-09-12 17:39:10 +00:00
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-15 09:24:30 +00:00
|
|
|
pub(crate) enum ValueNs {
|
2019-01-30 21:41:44 +00:00
|
|
|
LocalBinding(PatId),
|
2019-09-12 20:35:53 +00:00
|
|
|
Function(Function),
|
|
|
|
Const(Const),
|
|
|
|
Static(Static),
|
|
|
|
Struct(Struct),
|
|
|
|
EnumVariant(EnumVariant),
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
2019-09-12 17:39:10 +00:00
|
|
|
/// Resolve known trait from std, like `std::futures::Future`
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn resolve_known_trait(
|
|
|
|
&self,
|
|
|
|
db: &impl DefDatabase2,
|
|
|
|
path: &Path,
|
|
|
|
) -> Option<TraitId> {
|
2019-09-12 20:35:53 +00:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::TraitId(it) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known struct from std, like `std::boxed::Box`
|
|
|
|
pub(crate) fn resolve_known_struct(
|
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-09-12 17:39:10 +00:00
|
|
|
path: &Path,
|
2019-11-21 09:21:46 +00:00
|
|
|
) -> Option<StructId> {
|
2019-09-12 20:35:53 +00:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known enum from std, like `std::result::Result`
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option<EnumId> {
|
2019-09-12 20:35:53 +00:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
/// pub only for source-binder
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs {
|
2019-09-12 20:35:53 +00:00
|
|
|
let (item_map, module) = match self.module() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return PerNs::none(),
|
|
|
|
};
|
|
|
|
let (module_res, segment_index) = item_map.resolve_path(db, module, path);
|
|
|
|
if segment_index.is_some() {
|
|
|
|
return PerNs::none();
|
|
|
|
}
|
|
|
|
module_res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn resolve_path_in_type_ns(
|
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-09-12 20:35:53 +00:00
|
|
|
path: &Path,
|
|
|
|
) -> Option<(TypeNs, Option<usize>)> {
|
2019-09-16 19:38:27 +00:00
|
|
|
if path.is_type_relative() {
|
|
|
|
return None;
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
let first_name = &path.segments.first()?.name;
|
|
|
|
let skip_to_mod = path.kind != PathKind::Plain;
|
2019-01-19 20:23:26 +00:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 20:35:53 +00:00
|
|
|
match scope {
|
|
|
|
Scope::ExprScope(_) => continue,
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue,
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { params, .. } => {
|
2019-09-12 20:35:53 +00:00
|
|
|
if let Some(param) = params.find_by_name(first_name) {
|
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::GenericParam(param.idx), idx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(impl_) => {
|
2019-10-30 14:19:30 +00:00
|
|
|
if first_name == &name::SELF_TYPE {
|
2019-09-12 20:35:53 +00:00
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::SelfType(*impl_), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 04:19:52 +00:00
|
|
|
Scope::AdtScope(adt) => {
|
2019-10-30 14:19:30 +00:00
|
|
|
if first_name == &name::SELF_TYPE {
|
2019-09-27 04:19:52 +00:00
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::AdtSelfType(*adt), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
Scope::ModuleScope(m) => {
|
|
|
|
let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
|
|
|
|
let res = match module_def.take_types()? {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
|
|
|
ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
2019-10-31 15:45:10 +00:00
|
|
|
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2019-10-31 15:45:10 +00:00
|
|
|
ModuleDefId::FunctionId(_)
|
|
|
|
| ModuleDefId::ConstId(_)
|
|
|
|
| ModuleDefId::StaticId(_)
|
|
|
|
| ModuleDefId::ModuleId(_) => return None,
|
2019-09-12 20:35:53 +00:00
|
|
|
};
|
|
|
|
return Some((res, idx));
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
None
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
pub(crate) fn resolve_path_in_type_ns_fully(
|
2019-06-01 11:34:19 +00:00
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-06-08 17:42:02 +00:00
|
|
|
path: &Path,
|
2019-09-12 20:35:53 +00:00
|
|
|
) -> Option<TypeNs> {
|
|
|
|
let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?;
|
|
|
|
if unresolved.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(res)
|
2019-04-15 10:01:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 18:01:07 +00:00
|
|
|
pub(crate) fn resolve_path_in_value_ns<'p>(
|
2019-09-12 20:35:53 +00:00
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-09-11 18:01:07 +00:00
|
|
|
path: &'p Path,
|
2019-09-16 19:38:27 +00:00
|
|
|
) -> Option<ResolveValueResult> {
|
|
|
|
if path.is_type_relative() {
|
|
|
|
return None;
|
2019-09-11 18:01:07 +00:00
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
let n_segments = path.segments.len();
|
2019-10-30 14:19:30 +00:00
|
|
|
let tmp = name::SELF_PARAM;
|
2019-09-12 20:35:53 +00:00
|
|
|
let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name };
|
|
|
|
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
|
|
|
|
for scope in self.scopes.iter().rev() {
|
|
|
|
match scope {
|
2019-09-27 04:19:52 +00:00
|
|
|
Scope::AdtScope(_)
|
|
|
|
| Scope::ExprScope(_)
|
2019-11-20 17:50:34 +00:00
|
|
|
| Scope::GenericParams { .. }
|
2019-09-27 04:19:52 +00:00
|
|
|
| Scope::ImplBlockScope(_)
|
2019-09-12 20:35:53 +00:00
|
|
|
if skip_to_mod =>
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::ExprScope(scope) if n_segments <= 1 => {
|
|
|
|
let entry = scope
|
|
|
|
.expr_scopes
|
|
|
|
.entries(scope.scope_id)
|
|
|
|
.iter()
|
|
|
|
.find(|entry| entry.name() == first_name);
|
|
|
|
|
|
|
|
if let Some(e) = entry {
|
2019-09-11 18:01:07 +00:00
|
|
|
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(_) => continue,
|
|
|
|
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { params, .. } if n_segments > 1 => {
|
2019-09-12 20:35:53 +00:00
|
|
|
if let Some(param) = params.find_by_name(first_name) {
|
|
|
|
let ty = TypeNs::GenericParam(param.idx);
|
2019-09-11 18:01:07 +00:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { .. } => continue,
|
2019-09-12 20:35:53 +00:00
|
|
|
|
|
|
|
Scope::ImplBlockScope(impl_) if n_segments > 1 => {
|
2019-10-30 14:19:30 +00:00
|
|
|
if first_name == &name::SELF_TYPE {
|
2019-09-12 20:35:53 +00:00
|
|
|
let ty = TypeNs::SelfType(*impl_);
|
2019-09-11 18:01:07 +00:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 04:19:52 +00:00
|
|
|
Scope::AdtScope(adt) if n_segments > 1 => {
|
2019-10-30 14:19:30 +00:00
|
|
|
if first_name == &name::SELF_TYPE {
|
2019-09-27 04:19:52 +00:00
|
|
|
let ty = TypeNs::AdtSelfType(*adt);
|
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue,
|
2019-09-12 20:35:53 +00:00
|
|
|
|
|
|
|
Scope::ModuleScope(m) => {
|
|
|
|
let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
|
|
|
|
return match idx {
|
|
|
|
None => {
|
|
|
|
let value = match module_def.take_values()? {
|
2019-10-31 15:45:10 +00:00
|
|
|
ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()),
|
|
|
|
ModuleDefId::AdtId(AdtId::StructId(it)) => {
|
|
|
|
ValueNs::Struct(it.into())
|
|
|
|
}
|
|
|
|
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()),
|
|
|
|
ModuleDefId::ConstId(it) => ValueNs::Const(it.into()),
|
|
|
|
ModuleDefId::StaticId(it) => ValueNs::Static(it.into()),
|
|
|
|
|
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(_))
|
|
|
|
| ModuleDefId::AdtId(AdtId::UnionId(_))
|
|
|
|
| ModuleDefId::TraitId(_)
|
|
|
|
| ModuleDefId::TypeAliasId(_)
|
|
|
|
| ModuleDefId::BuiltinType(_)
|
|
|
|
| ModuleDefId::ModuleId(_) => return None,
|
2019-09-12 20:35:53 +00:00
|
|
|
};
|
2019-09-11 18:01:07 +00:00
|
|
|
Some(ResolveValueResult::ValueNs(value))
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
Some(idx) => {
|
|
|
|
let ty = match module_def.take_types()? {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
|
|
|
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
|
|
|
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
2019-10-31 15:45:10 +00:00
|
|
|
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDefId::ModuleId(_)
|
|
|
|
| ModuleDefId::FunctionId(_)
|
|
|
|
| ModuleDefId::EnumVariantId(_)
|
|
|
|
| ModuleDefId::ConstId(_)
|
|
|
|
| ModuleDefId::StaticId(_) => return None,
|
2019-09-12 20:35:53 +00:00
|
|
|
};
|
2019-09-11 18:01:07 +00:00
|
|
|
Some(ResolveValueResult::Partial(ty, idx))
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-02-21 10:04:14 +00:00
|
|
|
}
|
2019-01-26 21:52:04 +00:00
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
None
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
pub(crate) fn resolve_path_in_value_ns_fully(
|
2019-06-08 15:38:14 +00:00
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-06-08 15:38:14 +00:00
|
|
|
path: &Path,
|
2019-09-12 20:35:53 +00:00
|
|
|
) -> Option<ValueNs> {
|
|
|
|
match self.resolve_path_in_value_ns(db, path)? {
|
2019-09-11 18:01:07 +00:00
|
|
|
ResolveValueResult::ValueNs(it) => Some(it),
|
2019-09-16 19:38:27 +00:00
|
|
|
ResolveValueResult::Partial(..) => None,
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
2019-02-22 08:15:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
pub(crate) fn resolve_path_as_macro(
|
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-09-12 20:35:53 +00:00
|
|
|
path: &Path,
|
|
|
|
) -> Option<MacroDef> {
|
|
|
|
let (item_map, module) = self.module()?;
|
2019-10-31 15:45:10 +00:00
|
|
|
item_map.resolve_path(db, module, path).0.get_macros().map(MacroDef::from)
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn process_all_names(
|
|
|
|
&self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
2019-09-12 20:35:53 +00:00
|
|
|
f: &mut dyn FnMut(Name, ScopeDef),
|
|
|
|
) {
|
2019-02-01 22:06:57 +00:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 20:35:53 +00:00
|
|
|
scope.process_names(db, f);
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
2019-01-26 21:52:04 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet<TraitId> {
|
2019-05-12 12:58:37 +00:00
|
|
|
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() {
|
2019-10-31 15:45:10 +00:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
2019-11-21 09:21:46 +00:00
|
|
|
traits.extend(prelude_def_map[prelude.module_id].scope.traits());
|
2019-03-24 16:36:15 +00:00
|
|
|
}
|
2019-11-21 09:21:46 +00:00
|
|
|
traits.extend(m.crate_def_map[m.module_id].scope.traits());
|
2019-05-12 12:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
traits
|
2019-03-24 16:36:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:35:47 +00:00
|
|
|
fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
|
2019-01-29 19:49:31 +00:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
2019-03-14 09:54:03 +00:00
|
|
|
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
|
2019-01-29 19:49:31 +00:00
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 21:40:00 +00:00
|
|
|
|
|
|
|
pub(crate) fn krate(&self) -> Option<Crate> {
|
2019-10-31 15:45:10 +00:00
|
|
|
self.module().map(|t| Crate { crate_id: t.0.krate() })
|
2019-04-17 21:40:00 +00:00
|
|
|
}
|
2019-07-06 14:41:04 +00: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 {
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { params, .. } => Some(params),
|
2019-07-06 14:41:04 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flat_map(|params| params.where_predicates.iter())
|
|
|
|
}
|
2019-09-22 18:01:12 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn generic_def(&self) -> Option<GenericDefId> {
|
2019-09-22 18:01:12 +00:00
|
|
|
self.scopes.iter().find_map(|scope| match scope {
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { def, .. } => Some(*def),
|
2019-09-22 18:01:12 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
|
|
|
pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver {
|
|
|
|
self.scopes.push(scope);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-20 17:50:34 +00:00
|
|
|
pub(crate) fn push_generic_params_scope(
|
|
|
|
self,
|
2019-11-21 09:21:46 +00:00
|
|
|
db: &impl DefDatabase2,
|
|
|
|
def: GenericDefId,
|
2019-11-20 17:50:34 +00:00
|
|
|
) -> Resolver {
|
2019-11-21 09:21:46 +00:00
|
|
|
let params = db.generic_params(def);
|
2019-11-20 17:50:34 +00:00
|
|
|
if params.params.is_empty() {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
self.push_scope(Scope::GenericParams { def, params })
|
|
|
|
}
|
2019-01-23 22:08:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
|
2019-01-23 22:08:41 +00:00
|
|
|
self.push_scope(Scope::ImplBlockScope(impl_block))
|
|
|
|
}
|
|
|
|
|
2019-03-14 09:54:03 +00:00
|
|
|
pub(crate) fn push_module_scope(
|
|
|
|
self,
|
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-03-14 09:54:03 +00:00
|
|
|
) -> Resolver {
|
|
|
|
self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
|
2019-01-23 22:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_expr_scope(
|
|
|
|
self,
|
2019-11-21 09:21:46 +00:00
|
|
|
owner: DefWithBodyId,
|
2019-01-23 22:08:41 +00:00
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
) -> Resolver {
|
2019-11-15 09:00:36 +00:00
|
|
|
self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id }))
|
2019-01-23 22:08:41 +00:00
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
/// For IDE only
|
|
|
|
pub enum ScopeDef {
|
|
|
|
ModuleDef(ModuleDef),
|
|
|
|
MacroDef(MacroDef),
|
|
|
|
GenericParam(u32),
|
2019-09-27 04:19:52 +00:00
|
|
|
ImplSelfType(ImplBlock),
|
|
|
|
AdtSelfType(Adt),
|
2019-11-15 09:00:36 +00:00
|
|
|
Local(Local),
|
2019-09-12 20:35:53 +00:00
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:38:59 +00:00
|
|
|
impl From<PerNs> for ScopeDef {
|
|
|
|
fn from(def: PerNs) -> Self {
|
2019-09-12 20:35:53 +00:00
|
|
|
def.take_types()
|
|
|
|
.or_else(|| def.take_values())
|
2019-10-31 15:45:10 +00:00
|
|
|
.map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into()))
|
|
|
|
.or_else(|| {
|
|
|
|
def.get_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into()))
|
|
|
|
})
|
2019-09-12 20:35:53 +00:00
|
|
|
.unwrap_or(ScopeDef::Unknown)
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
2019-01-27 19:50:57 +00:00
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
impl Scope {
|
2019-11-21 09:21:46 +00:00
|
|
|
fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) {
|
2019-01-27 19:50:57 +00:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-03-23 07:53:48 +00:00
|
|
|
// FIXME: should we provide `self` here?
|
2019-01-27 16:23:49 +00:00
|
|
|
// f(
|
|
|
|
// Name::self_param(),
|
|
|
|
// PerNs::types(Resolution::Def {
|
|
|
|
// def: m.module.into(),
|
|
|
|
// }),
|
|
|
|
// );
|
2019-03-14 09:54:03 +00:00
|
|
|
m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| {
|
2019-09-12 20:35:53 +00:00
|
|
|
f(name.clone(), res.def.into());
|
2019-02-08 11:49:43 +00:00
|
|
|
});
|
2019-09-09 12:54:02 +00:00
|
|
|
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
|
2019-10-31 15:45:10 +00:00
|
|
|
f(name.clone(), ScopeDef::MacroDef(macro_.into()));
|
2019-09-09 12:54:02 +00:00
|
|
|
});
|
2019-10-31 15:45:10 +00:00
|
|
|
m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| {
|
|
|
|
f(name.clone(), ScopeDef::ModuleDef(def.into()));
|
2019-02-04 21:09:56 +00:00
|
|
|
});
|
2019-03-14 09:54:03 +00:00
|
|
|
if let Some(prelude) = m.crate_def_map.prelude() {
|
2019-10-31 15:45:10 +00:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
|
|
|
prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
|
|
|
|
f(name.clone(), res.def.into());
|
|
|
|
});
|
2019-02-13 19:53:42 +00:00
|
|
|
}
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { params, .. } => {
|
|
|
|
for param in params.params.iter() {
|
2019-09-12 20:35:53 +00:00
|
|
|
f(param.name.clone(), ScopeDef::GenericParam(param.idx))
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(i) => {
|
2019-11-21 09:21:46 +00:00
|
|
|
f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into()));
|
2019-09-27 04:19:52 +00:00
|
|
|
}
|
|
|
|
Scope::AdtScope(i) => {
|
2019-11-21 09:21:46 +00:00
|
|
|
f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into()));
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2019-11-15 09:00:36 +00:00
|
|
|
Scope::ExprScope(scope) => {
|
|
|
|
scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
|
2019-11-21 09:21:46 +00:00
|
|
|
let local = Local { parent: scope.owner.into(), pat_id: e.pat() };
|
2019-11-15 09:00:36 +00:00
|
|
|
f(e.name().clone(), ScopeDef::Local(local));
|
2019-01-27 19:50:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
2019-11-20 18:55:33 +00:00
|
|
|
|
|
|
|
pub(crate) trait HasResolver {
|
|
|
|
/// Builds a resolver for type references inside this def.
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Module {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
let def_map = db.crate_def_map(self.id.krate);
|
|
|
|
Resolver::default().push_module_scope(def_map, self.id.module_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Trait {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
2019-11-21 09:21:46 +00:00
|
|
|
self.module(db).resolver(db).push_generic_params_scope(db, self.id.into())
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:00:57 +00:00
|
|
|
impl<T: Into<Adt>> HasResolver for T {
|
2019-11-20 18:55:33 +00:00
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
2019-11-20 19:00:57 +00:00
|
|
|
let def = self.into();
|
|
|
|
def.module(db)
|
2019-11-20 18:55:33 +00:00
|
|
|
.resolver(db)
|
2019-11-20 19:00:57 +00:00
|
|
|
.push_generic_params_scope(db, def.into())
|
2019-11-21 09:21:46 +00:00
|
|
|
.push_scope(Scope::AdtScope(def.into()))
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Function {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
self.container(db)
|
|
|
|
.map(|c| c.resolver(db))
|
|
|
|
.unwrap_or_else(|| self.module(db).resolver(db))
|
2019-11-21 09:21:46 +00:00
|
|
|
.push_generic_params_scope(db, self.id.into())
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for DefWithBody {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
match self {
|
|
|
|
DefWithBody::Const(c) => c.resolver(db),
|
|
|
|
DefWithBody::Function(f) => f.resolver(db),
|
|
|
|
DefWithBody::Static(s) => s.resolver(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Const {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
self.container(db).map(|c| c.resolver(db)).unwrap_or_else(|| self.module(db).resolver(db))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Static {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
self.module(db).resolver(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for TypeAlias {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
self.container(db)
|
|
|
|
.map(|ib| ib.resolver(db))
|
|
|
|
.unwrap_or_else(|| self.module(db).resolver(db))
|
2019-11-21 09:21:46 +00:00
|
|
|
.push_generic_params_scope(db, self.id.into())
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for Container {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
match self {
|
|
|
|
Container::Trait(trait_) => trait_.resolver(db),
|
|
|
|
Container::ImplBlock(impl_block) => impl_block.resolver(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for GenericDef {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> crate::Resolver {
|
|
|
|
match self {
|
|
|
|
GenericDef::Function(inner) => inner.resolver(db),
|
|
|
|
GenericDef::Adt(adt) => adt.resolver(db),
|
|
|
|
GenericDef::Trait(inner) => inner.resolver(db),
|
|
|
|
GenericDef::TypeAlias(inner) => inner.resolver(db),
|
|
|
|
GenericDef::ImplBlock(inner) => inner.resolver(db),
|
|
|
|
GenericDef::EnumVariant(inner) => inner.parent_enum(db).resolver(db),
|
|
|
|
GenericDef::Const(inner) => inner.resolver(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for ImplBlock {
|
|
|
|
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
|
|
|
self.module(db)
|
|
|
|
.resolver(db)
|
2019-11-21 09:21:46 +00:00
|
|
|
.push_generic_params_scope(db, self.id.into())
|
|
|
|
.push_impl_block_scope(self.id)
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|