Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/lib.rs')
-rw-r--r--crates/hir-def/src/lib.rs187
1 files changed, 115 insertions, 72 deletions
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index 1e8859a9cc..dcc18666a5 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -74,12 +74,11 @@ use hir_expand::{
name::Name,
proc_macro::{CustomProcMacroExpander, ProcMacroKind},
};
-use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
use stdx::impl_from;
-use syntax::ast;
+use syntax::{AstNode, ast};
pub use hir_expand::{Intern, Lookup, tt};
@@ -88,10 +87,6 @@ use crate::{
builtin_type::BuiltinType,
db::DefDatabase,
hir::generics::{LocalLifetimeParamId, LocalTypeOrConstParamId},
- item_tree::{
- Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
- Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
- },
nameres::{LocalDefMap, block_def_map, crate_def_map, crate_local_def_map},
signatures::VariantFields,
};
@@ -113,70 +108,110 @@ pub struct ImportPathConfig {
}
#[derive(Debug)]
-pub struct ItemLoc<N: ItemTreeNode> {
+pub struct ItemLoc<N: AstIdNode> {
pub container: ModuleId,
- pub id: ItemTreeId<N>,
+ pub id: AstId<N>,
}
-impl<N: ItemTreeNode> Clone for ItemLoc<N> {
+impl<N: AstIdNode> Clone for ItemLoc<N> {
fn clone(&self) -> Self {
*self
}
}
-impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
+impl<N: AstIdNode> Copy for ItemLoc<N> {}
-impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
+impl<N: AstIdNode> PartialEq for ItemLoc<N> {
fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id
}
}
-impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
+impl<N: AstIdNode> Eq for ItemLoc<N> {}
-impl<N: ItemTreeNode> Hash for ItemLoc<N> {
+impl<N: AstIdNode> Hash for ItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state);
self.id.hash(state);
}
}
+impl<N: AstIdNode> HasModule for ItemLoc<N> {
+ #[inline]
+ fn module(&self, _db: &dyn DefDatabase) -> ModuleId {
+ self.container
+ }
+}
+
#[derive(Debug)]
-pub struct AssocItemLoc<N: ItemTreeNode> {
+pub struct AssocItemLoc<N: AstIdNode> {
pub container: ItemContainerId,
- pub id: ItemTreeId<N>,
+ pub id: AstId<N>,
}
-impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
+impl<N: AstIdNode> Clone for AssocItemLoc<N> {
fn clone(&self) -> Self {
*self
}
}
-impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
+impl<N: AstIdNode> Copy for AssocItemLoc<N> {}
-impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
+impl<N: AstIdNode> PartialEq for AssocItemLoc<N> {
fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id
}
}
-impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
+impl<N: AstIdNode> Eq for AssocItemLoc<N> {}
-impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
+impl<N: AstIdNode> Hash for AssocItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state);
self.id.hash(state);
}
}
-pub trait ItemTreeLoc {
+impl<N: AstIdNode> HasModule for AssocItemLoc<N> {
+ #[inline]
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
+ self.container.module(db)
+ }
+}
+
+pub trait AstIdLoc {
type Container;
- type Id;
- fn item_tree_id(&self) -> ItemTreeId<Self::Id>;
+ type Ast: AstNode;
+ fn ast_id(&self) -> AstId<Self::Ast>;
fn container(&self) -> Self::Container;
}
+impl<N: AstIdNode> AstIdLoc for ItemLoc<N> {
+ type Container = ModuleId;
+ type Ast = N;
+ #[inline]
+ fn ast_id(&self) -> AstId<Self::Ast> {
+ self.id
+ }
+ #[inline]
+ fn container(&self) -> Self::Container {
+ self.container
+ }
+}
+
+impl<N: AstIdNode> AstIdLoc for AssocItemLoc<N> {
+ type Container = ItemContainerId;
+ type Ast = N;
+ #[inline]
+ fn ast_id(&self) -> AstId<Self::Ast> {
+ self.id
+ }
+ #[inline]
+ fn container(&self) -> Self::Container {
+ self.container
+ }
+}
+
macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id, $loc);
@@ -186,74 +221,68 @@ macro_rules! impl_intern {
macro_rules! impl_loc {
($loc:ident, $id:ident: $id_ty:ident, $container:ident: $container_type:ident) => {
- impl ItemTreeLoc for $loc {
+ impl AstIdLoc for $loc {
type Container = $container_type;
- type Id = $id_ty;
- fn item_tree_id(&self) -> ItemTreeId<Self::Id> {
+ type Ast = ast::$id_ty;
+ fn ast_id(&self) -> AstId<Self::Ast> {
self.$id
}
fn container(&self) -> Self::Container {
self.$container
}
}
+
+ impl HasModule for $loc {
+ #[inline]
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
+ self.$container.module(db)
+ }
+ }
};
}
-type FunctionLoc = AssocItemLoc<Function>;
+type FunctionLoc = AssocItemLoc<ast::Fn>;
impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
-impl_loc!(FunctionLoc, id: Function, container: ItemContainerId);
-type StructLoc = ItemLoc<Struct>;
+type StructLoc = ItemLoc<ast::Struct>;
impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
-impl_loc!(StructLoc, id: Struct, container: ModuleId);
-pub type UnionLoc = ItemLoc<Union>;
+pub type UnionLoc = ItemLoc<ast::Union>;
impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
-impl_loc!(UnionLoc, id: Union, container: ModuleId);
-pub type EnumLoc = ItemLoc<Enum>;
+pub type EnumLoc = ItemLoc<ast::Enum>;
impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
-impl_loc!(EnumLoc, id: Enum, container: ModuleId);
-type ConstLoc = AssocItemLoc<Const>;
+type ConstLoc = AssocItemLoc<ast::Const>;
impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
-impl_loc!(ConstLoc, id: Const, container: ItemContainerId);
-pub type StaticLoc = AssocItemLoc<Static>;
+pub type StaticLoc = AssocItemLoc<ast::Static>;
impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
-impl_loc!(StaticLoc, id: Static, container: ItemContainerId);
-pub type TraitLoc = ItemLoc<Trait>;
+pub type TraitLoc = ItemLoc<ast::Trait>;
impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
-impl_loc!(TraitLoc, id: Trait, container: ModuleId);
-pub type TraitAliasLoc = ItemLoc<TraitAlias>;
+pub type TraitAliasLoc = ItemLoc<ast::TraitAlias>;
impl_intern!(TraitAliasId, TraitAliasLoc, intern_trait_alias, lookup_intern_trait_alias);
-impl_loc!(TraitAliasLoc, id: TraitAlias, container: ModuleId);
-type TypeAliasLoc = AssocItemLoc<TypeAlias>;
+type TypeAliasLoc = AssocItemLoc<ast::TypeAlias>;
impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
-impl_loc!(TypeAliasLoc, id: TypeAlias, container: ItemContainerId);
-type ImplLoc = ItemLoc<Impl>;
+type ImplLoc = ItemLoc<ast::Impl>;
impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
-impl_loc!(ImplLoc, id: Impl, container: ModuleId);
-type UseLoc = ItemLoc<Use>;
+type UseLoc = ItemLoc<ast::Use>;
impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use);
-impl_loc!(UseLoc, id: Use, container: ModuleId);
-type ExternCrateLoc = ItemLoc<ExternCrate>;
+type ExternCrateLoc = ItemLoc<ast::ExternCrate>;
impl_intern!(ExternCrateId, ExternCrateLoc, intern_extern_crate, lookup_intern_extern_crate);
-impl_loc!(ExternCrateLoc, id: ExternCrate, container: ModuleId);
-type ExternBlockLoc = ItemLoc<ExternBlock>;
+type ExternBlockLoc = ItemLoc<ast::ExternBlock>;
impl_intern!(ExternBlockId, ExternBlockLoc, intern_extern_block, lookup_intern_extern_block);
-impl_loc!(ExternBlockLoc, id: ExternBlock, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EnumVariantLoc {
- pub id: ItemTreeId<Variant>,
+ pub id: AstId<ast::Variant>,
pub parent: EnumId,
pub index: u32,
}
@@ -262,18 +291,18 @@ impl_loc!(EnumVariantLoc, id: Variant, parent: EnumId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Macro2Loc {
pub container: ModuleId,
- pub id: ItemTreeId<Macro2>,
+ pub id: AstId<ast::MacroDef>,
pub expander: MacroExpander,
pub allow_internal_unsafe: bool,
pub edition: Edition,
}
impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2);
-impl_loc!(Macro2Loc, id: Macro2, container: ModuleId);
+impl_loc!(Macro2Loc, id: MacroDef, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroRulesLoc {
pub container: ModuleId,
- pub id: ItemTreeId<MacroRules>,
+ pub id: AstId<ast::MacroRules>,
pub expander: MacroExpander,
pub flags: MacroRulesLocFlags,
pub edition: Edition,
@@ -301,13 +330,13 @@ pub enum MacroExpander {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProcMacroLoc {
pub container: CrateRootModuleId,
- pub id: ItemTreeId<Function>,
+ pub id: AstId<ast::Fn>,
pub expander: CustomProcMacroExpander,
pub kind: ProcMacroKind,
pub edition: Edition,
}
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);
-impl_loc!(ProcMacroLoc, id: Function, container: CrateRootModuleId);
+impl_loc!(ProcMacroLoc, id: Fn, container: CrateRootModuleId);
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct BlockLoc {
@@ -338,6 +367,18 @@ impl CrateRootModuleId {
}
}
+impl HasModule for CrateRootModuleId {
+ #[inline]
+ fn module(&self, _db: &dyn DefDatabase) -> ModuleId {
+ ModuleId { krate: self.krate, block: None, local_id: DefMap::ROOT }
+ }
+
+ #[inline]
+ fn krate(&self, _db: &dyn DefDatabase) -> Crate {
+ self.krate
+ }
+}
+
impl PartialEq<ModuleId> for CrateRootModuleId {
fn eq(&self, other: &ModuleId) -> bool {
other.block.is_none() && other.local_id == DefMap::ROOT && self.krate == other.krate
@@ -466,6 +507,13 @@ impl ModuleId {
}
}
+impl HasModule for ModuleId {
+ #[inline]
+ fn module(&self, _db: &dyn DefDatabase) -> ModuleId {
+ *self
+ }
+}
+
/// An ID of a module, **local** to a `DefMap`.
pub type LocalModuleId = Idx<nameres::ModuleData>;
@@ -642,15 +690,10 @@ impl GeneralConstId {
pub fn name(self, db: &dyn DefDatabase) -> String {
match self {
GeneralConstId::StaticId(it) => {
- let loc = it.lookup(db);
- let tree = loc.item_tree_id().item_tree(db);
- let name = tree[loc.id.value].name.display(db, Edition::CURRENT);
- name.to_string()
+ db.static_signature(it).name.display(db, Edition::CURRENT).to_string()
}
GeneralConstId::ConstId(const_id) => {
- let loc = const_id.lookup(db);
- let tree = loc.item_tree_id().item_tree(db);
- tree[loc.id.value].name.as_ref().map_or_else(
+ db.const_signature(const_id).name.as_ref().map_or_else(
|| "_".to_owned(),
|name| name.display(db, Edition::CURRENT).to_string(),
)
@@ -768,8 +811,8 @@ impl GenericDefId {
GenericDefId::TraitId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::TraitAliasId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::ImplId(it) => file_id_and_params_of_item_loc(db, it),
- GenericDefId::ConstId(it) => (it.lookup(db).id.file_id(), None),
- GenericDefId::StaticId(it) => (it.lookup(db).id.file_id(), None),
+ GenericDefId::ConstId(it) => (it.lookup(db).id.file_id, None),
+ GenericDefId::StaticId(it) => (it.lookup(db).id.file_id, None),
}
}
@@ -935,9 +978,9 @@ impl VariantId {
pub fn file_id(self, db: &dyn DefDatabase) -> HirFileId {
match self {
- VariantId::EnumVariantId(it) => it.lookup(db).id.file_id(),
- VariantId::StructId(it) => it.lookup(db).id.file_id(),
- VariantId::UnionId(it) => it.lookup(db).id.file_id(),
+ VariantId::EnumVariantId(it) => it.lookup(db).id.file_id,
+ VariantId::StructId(it) => it.lookup(db).id.file_id,
+ VariantId::UnionId(it) => it.lookup(db).id.file_id,
}
}
@@ -977,7 +1020,7 @@ pub trait HasModule {
impl<N, ItemId> HasModule for ItemId
where
- N: ItemTreeNode,
+ N: AstIdNode,
ItemId: Lookup<Database = dyn DefDatabase, Data = ItemLoc<N>> + Copy,
{
#[inline]
@@ -1003,7 +1046,7 @@ where
#[inline]
fn module_for_assoc_item_loc<'db>(
db: &(dyn 'db + DefDatabase),
- id: impl Lookup<Database = dyn DefDatabase, Data = AssocItemLoc<impl ItemTreeNode>>,
+ id: impl Lookup<Database = dyn DefDatabase, Data = AssocItemLoc<impl AstIdNode>>,
) -> ModuleId {
id.lookup(db).container.module(db)
}