From b5fd02d93cdfafeba23f50ca9c414053aaa548ae Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 18:41:32 +0200 Subject: Generate ModItem via macro --- crates/ra_hir_def/src/item_tree.rs | 165 ++++++++++++++----------------------- 1 file changed, 60 insertions(+), 105 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs index 6eb0c1b91..5beb11df7 100644 --- a/crates/ra_hir_def/src/item_tree.rs +++ b/crates/ra_hir_def/src/item_tree.rs @@ -223,71 +223,75 @@ impl fmt::Debug for FileItemTreeId { pub type ItemTreeId = InFile>; -macro_rules! nodes { - ( $($node:ident in $fld:ident),+ $(,)? ) => { $( - impl ItemTreeNode for $node { - fn lookup(tree: &ItemTree, index: Idx) -> &Self { - &tree.$fld[index] +macro_rules! mod_items { + ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => { + #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] + pub enum ModItem { + $( + $typ(FileItemTreeId<$typ>), + )+ + } + + $( + impl From> for ModItem { + fn from(id: FileItemTreeId<$typ>) -> ModItem { + ModItem::$typ(id) + } } + )+ + + $( + impl ItemTreeNode for $typ { + fn lookup(tree: &ItemTree, index: Idx) -> &Self { + &tree.$fld[index] + } + fn id_from_mod_item(mod_item: ModItem) -> Option> { + if let ModItem::$typ(id) = mod_item { + Some(id) + } else { + None + } + } - fn id_from_mod_item(mod_item: ModItem) -> Option> { - if let ModItem::$node(id) = mod_item { - Some(id) - } else { - None + fn id_to_mod_item(id: FileItemTreeId) -> ModItem { + ModItem::$typ(id) } } - fn id_to_mod_item(id: FileItemTreeId) -> ModItem { - ModItem::$node(id) + impl ItemTreeSource for $typ { + type Source = $ast; + + fn ast_id(&self) -> FileAstId { + self.ast_id + } } - } - )+ }; -} - -nodes!( - Import in imports, - ExternCrate in extern_crates, - Function in functions, - Struct in structs, - Union in unions, - Enum in enums, - Const in consts, - Static in statics, - Trait in traits, - Impl in impls, - TypeAlias in type_aliases, - Mod in mods, - MacroCall in macro_calls, -); - -macro_rules! source { - ( $($node:ident -> $ast:path),+ $(,)? ) => { $( - impl ItemTreeSource for $node { - type Source = $ast; - - fn ast_id(&self) -> FileAstId { - self.ast_id + + impl Index> for ItemTree { + type Output = $typ; + + fn index(&self, index: Idx<$typ>) -> &Self::Output { + &self.$fld[index] + } } - } - )+ }; + )+ + }; } -source! { - Import -> ast::UseItem, - ExternCrate -> ast::ExternCrateItem, - Function -> ast::FnDef, - Struct -> ast::StructDef, - Union -> ast::UnionDef, - Enum -> ast::EnumDef, - Const -> ast::ConstDef, - Static -> ast::StaticDef, - Trait -> ast::TraitDef, - Impl -> ast::ImplDef, - TypeAlias -> ast::TypeAliasDef, - Mod -> ast::Module, - MacroCall -> ast::MacroCall, +mod_items! { + Import in imports -> ast::UseItem, + ExternCrate in extern_crates -> ast::ExternCrateItem, + Function in functions -> ast::FnDef, + Struct in structs -> ast::StructDef, + Union in unions -> ast::UnionDef, + Enum in enums -> ast::EnumDef, + Const in consts -> ast::ConstDef, + Static in statics -> ast::StaticDef, + Trait in traits -> ast::TraitDef, + Impl in impls -> ast::ImplDef, + TypeAlias in type_aliases -> ast::TypeAliasDef, + Mod in mods -> ast::Module, + MacroCall in macro_calls -> ast::MacroCall, } macro_rules! impl_index { @@ -304,23 +308,7 @@ macro_rules! impl_index { }; } -impl_index!( - imports: Import, - functions: Function, - structs: Struct, - fields: Field, - unions: Union, - enums: Enum, - variants: Variant, - consts: Const, - statics: Static, - traits: Trait, - impls: Impl, - type_aliases: TypeAlias, - mods: Mod, - macro_calls: MacroCall, - exprs: Expr, -); +impl_index!(fields: Field, variants: Variant, exprs: Expr); impl Index> for ItemTree { type Output = N; @@ -500,23 +488,6 @@ macro_rules! impl_froms { } } -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum ModItem { - Import(FileItemTreeId), - ExternCrate(FileItemTreeId), - Function(FileItemTreeId), - Struct(FileItemTreeId), - Union(FileItemTreeId), - Enum(FileItemTreeId), - Const(FileItemTreeId), - Static(FileItemTreeId), - Trait(FileItemTreeId), - Impl(FileItemTreeId), - TypeAlias(FileItemTreeId), - Mod(FileItemTreeId), - MacroCall(FileItemTreeId), -} - impl ModItem { pub fn as_assoc_item(&self) -> Option { match self { @@ -541,22 +512,6 @@ impl ModItem { } } -impl_froms!(ModItem { - Import(FileItemTreeId), - ExternCrate(FileItemTreeId), - Function(FileItemTreeId), - Struct(FileItemTreeId), - Union(FileItemTreeId), - Enum(FileItemTreeId), - Const(FileItemTreeId), - Static(FileItemTreeId), - Trait(FileItemTreeId), - Impl(FileItemTreeId), - TypeAlias(FileItemTreeId), - Mod(FileItemTreeId), - MacroCall(FileItemTreeId), -}); - #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum AssocItem { Function(FileItemTreeId), -- cgit v1.2.3