Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/item_tree.rs')
-rw-r--r--crates/hir-def/src/item_tree.rs78
1 files changed, 43 insertions, 35 deletions
diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs
index 336e0de7fd..be16a5e31a 100644
--- a/crates/hir-def/src/item_tree.rs
+++ b/crates/hir-def/src/item_tree.rs
@@ -337,20 +337,18 @@ from_attrs!(
LifetimeParamData(Idx<LifetimeParamData>),
);
-/// Trait implemented by all item nodes in the item tree.
-pub trait ItemTreeModItemNode: Clone {
- type Source: AstIdNode + Into<ast::Item>;
+/// Trait implemented by all nodes in the item tree.
+pub trait ItemTreeNode: Clone {
+ type Source: AstIdNode;
fn ast_id(&self) -> FileAstId<Self::Source>;
/// Looks up an instance of `Self` in an item tree.
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
-
- /// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
- fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
-
- /// Upcasts a `FileItemTreeId` to a generic `ModItem`.
- fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
+ fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner;
+}
+pub trait GenericsItemTreeNode: ItemTreeNode {
+ fn generic_params(&self) -> &Interned<GenericParams>;
}
pub struct FileItemTreeId<N>(Idx<N>);
@@ -372,7 +370,7 @@ impl<N> FileItemTreeId<N> {
impl<N> Clone for FileItemTreeId<N> {
fn clone(&self) -> Self {
- Self(self.0)
+ *self
}
}
impl<N> Copy for FileItemTreeId<N> {}
@@ -478,7 +476,7 @@ impl<N> Hash for ItemTreeId<N> {
}
macro_rules! mod_items {
- ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
+ ( $( $typ:ident $(<$generic_params:ident>)? in $fld:ident -> $ast:ty ),+ $(,)? ) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ModItem {
$(
@@ -495,7 +493,7 @@ macro_rules! mod_items {
)+
$(
- impl ItemTreeModItemNode for $typ {
+ impl ItemTreeNode for $typ {
type Source = $ast;
fn ast_id(&self) -> FileAstId<Self::Source> {
@@ -506,15 +504,8 @@ macro_rules! mod_items {
&tree.data().$fld[index]
}
- fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
- match mod_item {
- ModItem::$typ(id) => Some(id),
- _ => None,
- }
- }
-
- fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
- ModItem::$typ(id)
+ fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
+ AttrOwner::ModItem(ModItem::$typ(id))
}
}
@@ -525,6 +516,14 @@ macro_rules! mod_items {
&self.data().$fld[index]
}
}
+
+ $(
+ impl GenericsItemTreeNode for $typ {
+ fn generic_params(&self) -> &Interned<GenericParams> {
+ &self.$generic_params
+ }
+ }
+ )?
)+
};
}
@@ -533,16 +532,16 @@ mod_items! {
Use in uses -> ast::Use,
ExternCrate in extern_crates -> ast::ExternCrate,
ExternBlock in extern_blocks -> ast::ExternBlock,
- Function in functions -> ast::Fn,
- Struct in structs -> ast::Struct,
- Union in unions -> ast::Union,
- Enum in enums -> ast::Enum,
+ Function<explicit_generic_params> in functions -> ast::Fn,
+ Struct<generic_params> in structs -> ast::Struct,
+ Union<generic_params> in unions -> ast::Union,
+ Enum<generic_params> in enums -> ast::Enum,
Const in consts -> ast::Const,
Static in statics -> ast::Static,
- Trait in traits -> ast::Trait,
- TraitAlias in trait_aliases -> ast::TraitAlias,
- Impl in impls -> ast::Impl,
- TypeAlias in type_aliases -> ast::TypeAlias,
+ Trait<generic_params> in traits -> ast::Trait,
+ TraitAlias<generic_params> in trait_aliases -> ast::TraitAlias,
+ Impl<generic_params> in impls -> ast::Impl,
+ TypeAlias<generic_params> in type_aliases -> ast::TypeAlias,
Mod in mods -> ast::Module,
MacroCall in macro_calls -> ast::MacroCall,
MacroRules in macro_rules -> ast::MacroRules,
@@ -578,17 +577,26 @@ impl Index<RawVisibilityId> for ItemTree {
}
}
-impl<N: ItemTreeModItemNode> Index<FileItemTreeId<N>> for ItemTree {
+impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
type Output = N;
fn index(&self, id: FileItemTreeId<N>) -> &N {
N::lookup(self, id.index())
}
}
-impl Index<FileItemTreeId<Variant>> for ItemTree {
- type Output = Variant;
- fn index(&self, id: FileItemTreeId<Variant>) -> &Variant {
- &self[id.index()]
+impl ItemTreeNode for Variant {
+ type Source = ast::Variant;
+
+ fn ast_id(&self) -> FileAstId<Self::Source> {
+ self.ast_id
+ }
+
+ fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
+ &tree.data().variants[index]
+ }
+
+ fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
+ AttrOwner::Variant(id)
}
}
@@ -1027,7 +1035,7 @@ impl AssocItem {
}
}
-#[derive(Debug, Eq, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variant {
pub name: Name,
pub fields: Fields,