2019-01-19 14:23:26 -06:00
|
|
|
//! Name resolution.
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-10-30 04:27:54 -05:00
|
|
|
use hir_def::CrateModuleId;
|
2019-09-12 15:35:53 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-01-19 14:23:26 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-05-23 13:14:19 -05:00
|
|
|
code_model::Crate,
|
2019-09-17 14:22:40 -05:00
|
|
|
db::{DefDatabase, 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-10-30 04:27:54 -05:00
|
|
|
nameres::{CrateDefMap, PerNs},
|
2019-09-12 15:35:53 -05:00
|
|
|
path::{Path, PathKind},
|
|
|
|
Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct,
|
|
|
|
Trait, TypeAlias,
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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>),
|
2019-09-26 23:19:52 -05:00
|
|
|
/// Brings `Self` in `impl` block into scope
|
2019-01-19 14:23:26 -06:00
|
|
|
ImplBlockScope(ImplBlock),
|
2019-10-08 06:25:37 -05:00
|
|
|
/// Brings `Self` in enum, struct and union definitions into scope
|
2019-09-26 23:19:52 -05:00
|
|
|
AdtScope(Adt),
|
2019-01-19 14:23:26 -06:00
|
|
|
/// Local bindings
|
|
|
|
ExprScope(ExprScope),
|
|
|
|
}
|
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-09-12 15:35:53 -05:00
|
|
|
pub enum TypeNs {
|
|
|
|
SelfType(ImplBlock),
|
|
|
|
GenericParam(u32),
|
|
|
|
Adt(Adt),
|
2019-09-26 23:19:52 -05:00
|
|
|
AdtSelfType(Adt),
|
2019-09-12 15:35:53 -05:00
|
|
|
EnumVariant(EnumVariant),
|
|
|
|
TypeAlias(TypeAlias),
|
|
|
|
BuiltinType(BuiltinType),
|
|
|
|
Trait(Trait),
|
2019-09-14 09:26:03 -05:00
|
|
|
// Module belong to type ns, but the resolver is used when all module paths
|
2019-09-12 15:35:53 -05:00
|
|
|
// are fully resolved.
|
|
|
|
// Module(Module)
|
|
|
|
}
|
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-09-16 14:38:27 -05:00
|
|
|
pub enum ResolveValueResult {
|
2019-09-12 15:35:53 -05:00
|
|
|
ValueNs(ValueNs),
|
|
|
|
Partial(TypeNs, usize),
|
|
|
|
}
|
2019-09-12 12:39:10 -05:00
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-09-12 15:35:53 -05:00
|
|
|
pub enum ValueNs {
|
2019-01-30 15:41:44 -06:00
|
|
|
LocalBinding(PatId),
|
2019-09-12 15:35:53 -05:00
|
|
|
Function(Function),
|
|
|
|
Const(Const),
|
|
|
|
Static(Static),
|
|
|
|
Struct(Struct),
|
|
|
|
EnumVariant(EnumVariant),
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
2019-09-12 12:39:10 -05:00
|
|
|
/// Resolve known trait from std, like `std::futures::Future`
|
|
|
|
pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option<Trait> {
|
2019-09-12 15:35:53 -05:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-09-12 15:35:53 -05:00
|
|
|
ModuleDef::Trait(it) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known struct from std, like `std::boxed::Box`
|
|
|
|
pub(crate) fn resolve_known_struct(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
path: &Path,
|
|
|
|
) -> Option<Struct> {
|
2019-09-12 15:35:53 -05:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-09-12 15:35:53 -05:00
|
|
|
ModuleDef::Adt(Adt::Struct(it)) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known enum from std, like `std::result::Result`
|
|
|
|
pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> {
|
2019-09-12 15:35:53 -05:00
|
|
|
let res = self.resolve_module_path(db, path).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-09-12 15:35:53 -05:00
|
|
|
ModuleDef::Adt(Adt::Enum(it)) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
/// pub only for source-binder
|
2019-09-13 08:38:59 -05:00
|
|
|
pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs {
|
2019-09-12 15:35:53 -05: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,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
path: &Path,
|
|
|
|
) -> Option<(TypeNs, Option<usize>)> {
|
2019-09-16 14:38:27 -05:00
|
|
|
if path.is_type_relative() {
|
|
|
|
return None;
|
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
let first_name = &path.segments.first()?.name;
|
|
|
|
let skip_to_mod = path.kind != PathKind::Plain;
|
2019-01-19 14:23:26 -06:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 15:35:53 -05:00
|
|
|
match scope {
|
|
|
|
Scope::ExprScope(_) => continue,
|
|
|
|
Scope::GenericParams(_) | Scope::ImplBlockScope(_) if skip_to_mod => continue,
|
|
|
|
|
|
|
|
Scope::GenericParams(params) => {
|
|
|
|
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_) => {
|
|
|
|
if first_name == &SELF_TYPE {
|
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::SelfType(*impl_), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 23:19:52 -05:00
|
|
|
Scope::AdtScope(adt) => {
|
|
|
|
if first_name == &SELF_TYPE {
|
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::AdtSelfType(*adt), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:35:53 -05: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()? {
|
|
|
|
ModuleDef::Adt(it) => TypeNs::Adt(it),
|
|
|
|
ModuleDef::EnumVariant(it) => TypeNs::EnumVariant(it),
|
|
|
|
|
|
|
|
ModuleDef::TypeAlias(it) => TypeNs::TypeAlias(it),
|
|
|
|
ModuleDef::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDef::Trait(it) => TypeNs::Trait(it),
|
|
|
|
|
|
|
|
ModuleDef::Function(_)
|
|
|
|
| ModuleDef::Const(_)
|
|
|
|
| ModuleDef::Static(_)
|
|
|
|
| ModuleDef::Module(_) => return None,
|
|
|
|
};
|
|
|
|
return Some((res, idx));
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
None
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
pub(crate) fn resolve_path_in_type_ns_fully(
|
2019-06-01 06:34:19 -05:00
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
2019-06-08 12:42:02 -05:00
|
|
|
path: &Path,
|
2019-09-12 15:35:53 -05: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 05:01:29 -05:00
|
|
|
}
|
|
|
|
|
2019-09-11 13:01:07 -05:00
|
|
|
pub(crate) fn resolve_path_in_value_ns<'p>(
|
2019-09-12 15:35:53 -05:00
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
2019-09-11 13:01:07 -05:00
|
|
|
path: &'p Path,
|
2019-09-16 14:38:27 -05:00
|
|
|
) -> Option<ResolveValueResult> {
|
|
|
|
if path.is_type_relative() {
|
|
|
|
return None;
|
2019-09-11 13:01:07 -05:00
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
let n_segments = path.segments.len();
|
|
|
|
let tmp = SELF_PARAM;
|
|
|
|
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-26 23:19:52 -05:00
|
|
|
Scope::AdtScope(_)
|
|
|
|
| Scope::ExprScope(_)
|
|
|
|
| Scope::GenericParams(_)
|
|
|
|
| Scope::ImplBlockScope(_)
|
2019-09-12 15:35:53 -05: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 13:01:07 -05:00
|
|
|
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(_) => continue,
|
|
|
|
|
|
|
|
Scope::GenericParams(params) if n_segments > 1 => {
|
|
|
|
if let Some(param) = params.find_by_name(first_name) {
|
|
|
|
let ty = TypeNs::GenericParam(param.idx);
|
2019-09-11 13:01:07 -05:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::GenericParams(_) => continue,
|
|
|
|
|
|
|
|
Scope::ImplBlockScope(impl_) if n_segments > 1 => {
|
|
|
|
if first_name == &SELF_TYPE {
|
|
|
|
let ty = TypeNs::SelfType(*impl_);
|
2019-09-11 13:01:07 -05:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 23:19:52 -05:00
|
|
|
Scope::AdtScope(adt) if n_segments > 1 => {
|
|
|
|
if first_name == &SELF_TYPE {
|
|
|
|
let ty = TypeNs::AdtSelfType(*adt);
|
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue,
|
2019-09-12 15:35:53 -05: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()? {
|
|
|
|
ModuleDef::Function(it) => ValueNs::Function(it),
|
|
|
|
ModuleDef::Adt(Adt::Struct(it)) => ValueNs::Struct(it),
|
|
|
|
ModuleDef::EnumVariant(it) => ValueNs::EnumVariant(it),
|
|
|
|
ModuleDef::Const(it) => ValueNs::Const(it),
|
|
|
|
ModuleDef::Static(it) => ValueNs::Static(it),
|
|
|
|
|
|
|
|
ModuleDef::Adt(Adt::Enum(_))
|
|
|
|
| ModuleDef::Adt(Adt::Union(_))
|
|
|
|
| ModuleDef::Trait(_)
|
|
|
|
| ModuleDef::TypeAlias(_)
|
|
|
|
| ModuleDef::BuiltinType(_)
|
|
|
|
| ModuleDef::Module(_) => return None,
|
|
|
|
};
|
2019-09-11 13:01:07 -05:00
|
|
|
Some(ResolveValueResult::ValueNs(value))
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
Some(idx) => {
|
|
|
|
let ty = match module_def.take_types()? {
|
|
|
|
ModuleDef::Adt(it) => TypeNs::Adt(it),
|
|
|
|
ModuleDef::Trait(it) => TypeNs::Trait(it),
|
|
|
|
ModuleDef::TypeAlias(it) => TypeNs::TypeAlias(it),
|
|
|
|
ModuleDef::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDef::Module(_)
|
|
|
|
| ModuleDef::Function(_)
|
|
|
|
| ModuleDef::EnumVariant(_)
|
|
|
|
| ModuleDef::Const(_)
|
|
|
|
| ModuleDef::Static(_) => return None,
|
|
|
|
};
|
2019-09-11 13:01:07 -05:00
|
|
|
Some(ResolveValueResult::Partial(ty, idx))
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
None
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
pub(crate) fn resolve_path_in_value_ns_fully(
|
2019-06-08 10:38:14 -05:00
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
path: &Path,
|
2019-09-12 15:35:53 -05:00
|
|
|
) -> Option<ValueNs> {
|
|
|
|
match self.resolve_path_in_value_ns(db, path)? {
|
2019-09-11 13:01:07 -05:00
|
|
|
ResolveValueResult::ValueNs(it) => Some(it),
|
2019-09-16 14:38:27 -05:00
|
|
|
ResolveValueResult::Partial(..) => None,
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
2019-02-22 02:15:23 -06:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
pub(crate) fn resolve_path_as_macro(
|
|
|
|
&self,
|
2019-09-17 14:22:40 -05:00
|
|
|
db: &impl DefDatabase,
|
2019-09-12 15:35:53 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Option<MacroDef> {
|
|
|
|
let (item_map, module) = self.module()?;
|
|
|
|
item_map.resolve_path(db, module, path).0.get_macros()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn process_all_names(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
f: &mut dyn FnMut(Name, ScopeDef),
|
|
|
|
) {
|
2019-02-01 16:06:57 -06:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 15:35:53 -05:00
|
|
|
scope.process_names(db, f);
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
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() {
|
2019-10-30 04:27:54 -05:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate());
|
|
|
|
traits.extend(prelude_def_map[prelude.id.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-09-22 13:01:12 -05:00
|
|
|
|
|
|
|
pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> {
|
|
|
|
self.scopes.iter().find_map(|scope| match scope {
|
|
|
|
Scope::GenericParams(params) => Some(params.def),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
/// For IDE only
|
|
|
|
pub enum ScopeDef {
|
|
|
|
ModuleDef(ModuleDef),
|
|
|
|
MacroDef(MacroDef),
|
|
|
|
GenericParam(u32),
|
2019-09-26 23:19:52 -05:00
|
|
|
ImplSelfType(ImplBlock),
|
|
|
|
AdtSelfType(Adt),
|
2019-09-12 15:35:53 -05:00
|
|
|
LocalBinding(PatId),
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:38:59 -05:00
|
|
|
impl From<PerNs> for ScopeDef {
|
|
|
|
fn from(def: PerNs) -> Self {
|
2019-09-12 15:35:53 -05:00
|
|
|
def.take_types()
|
|
|
|
.or_else(|| def.take_values())
|
|
|
|
.map(ScopeDef::ModuleDef)
|
|
|
|
.or_else(|| def.get_macros().map(ScopeDef::MacroDef))
|
|
|
|
.unwrap_or(ScopeDef::Unknown)
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
impl Scope {
|
|
|
|
fn process_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
|
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-09-12 15:35:53 -05:00
|
|
|
f(name.clone(), res.def.into());
|
2019-02-08 05:49:43 -06:00
|
|
|
});
|
2019-09-09 07:54:02 -05:00
|
|
|
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
|
2019-09-12 15:35:53 -05:00
|
|
|
f(name.clone(), ScopeDef::MacroDef(macro_));
|
2019-09-09 07:54:02 -05:00
|
|
|
});
|
2019-03-14 04:54:03 -05:00
|
|
|
m.crate_def_map.extern_prelude().iter().for_each(|(name, def)| {
|
2019-09-12 15:35:53 -05:00
|
|
|
f(name.clone(), ScopeDef::ModuleDef(*def));
|
2019-02-04 15:09:56 -06:00
|
|
|
});
|
2019-03-14 04:54:03 -05:00
|
|
|
if let Some(prelude) = m.crate_def_map.prelude() {
|
2019-10-30 04:27:54 -05:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate());
|
|
|
|
prelude_def_map[prelude.id.module_id].scope.entries().for_each(
|
|
|
|
|(name, res)| {
|
|
|
|
f(name.clone(), res.def.into());
|
|
|
|
},
|
|
|
|
);
|
2019-02-13 13:53:42 -06:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
|
|
|
Scope::GenericParams(gp) => {
|
|
|
|
for param in &gp.params {
|
2019-09-12 15:35:53 -05:00
|
|
|
f(param.name.clone(), ScopeDef::GenericParam(param.idx))
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ImplBlockScope(i) => {
|
2019-09-26 23:19:52 -05:00
|
|
|
f(SELF_TYPE, ScopeDef::ImplSelfType(*i));
|
|
|
|
}
|
|
|
|
Scope::AdtScope(i) => {
|
|
|
|
f(SELF_TYPE, ScopeDef::AdtSelfType(*i));
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
|
|
|
Scope::ExprScope(e) => {
|
|
|
|
e.expr_scopes.entries(e.scope_id).iter().for_each(|e| {
|
2019-09-12 15:35:53 -05:00
|
|
|
f(e.name().clone(), ScopeDef::LocalBinding(e.pat()));
|
2019-01-27 13:50:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|