]> git.lizzy.rs Git - rust.git/commitdiff
Generate ModItem via macro
authorJonas Schievink <jonas.schievink@ferrous-systems.com>
Tue, 23 Jun 2020 16:41:32 +0000 (18:41 +0200)
committerJonas Schievink <jonas.schievink@ferrous-systems.com>
Wed, 24 Jun 2020 14:53:56 +0000 (16:53 +0200)
crates/ra_hir_def/src/item_tree.rs

index 6eb0c1b912003d39a7485a0bbf7e3e9ef4db51f0..5beb11df7b23e246210961bccf2b585aeb3a6b21 100644 (file)
@@ -223,71 +223,75 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 
 pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
 
-macro_rules! nodes {
-    ( $($node:ident in $fld:ident),+ $(,)? ) => { $(
-        impl ItemTreeNode for $node {
-            fn lookup(tree: &ItemTree, index: Idx<Self>) -> &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<FileItemTreeId<$typ>> for ModItem {
+                fn from(id: FileItemTreeId<$typ>) -> ModItem {
+                    ModItem::$typ(id)
+                }
             }
+        )+
+
+        $(
+            impl ItemTreeNode for $typ {
+                fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
+                    &tree.$fld[index]
+                }
 
+                fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
+                    if let ModItem::$typ(id) = mod_item {
+                        Some(id)
+                    } else {
+                        None
+                    }
+                }
 
-            fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
-                if let ModItem::$node(id) = mod_item {
-                    Some(id)
-                } else {
-                    None
+                fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
+                    ModItem::$typ(id)
                 }
             }
 
-            fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
-                ModItem::$node(id)
+            impl ItemTreeSource for $typ {
+                type Source = $ast;
+
+                fn ast_id(&self) -> FileAstId<Self::Source> {
+                    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::Source> {
-                self.ast_id
+
+            impl Index<Idx<$typ>> 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 @@ fn index(&self, index: Idx<$t>) -> &Self::Output {
     };
 }
 
-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<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
     type Output = N;
@@ -500,23 +488,6 @@ fn from(it: $t) -> $e {
     }
 }
 
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum ModItem {
-    Import(FileItemTreeId<Import>),
-    ExternCrate(FileItemTreeId<ExternCrate>),
-    Function(FileItemTreeId<Function>),
-    Struct(FileItemTreeId<Struct>),
-    Union(FileItemTreeId<Union>),
-    Enum(FileItemTreeId<Enum>),
-    Const(FileItemTreeId<Const>),
-    Static(FileItemTreeId<Static>),
-    Trait(FileItemTreeId<Trait>),
-    Impl(FileItemTreeId<Impl>),
-    TypeAlias(FileItemTreeId<TypeAlias>),
-    Mod(FileItemTreeId<Mod>),
-    MacroCall(FileItemTreeId<MacroCall>),
-}
-
 impl ModItem {
     pub fn as_assoc_item(&self) -> Option<AssocItem> {
         match self {
@@ -541,22 +512,6 @@ pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
     }
 }
 
-impl_froms!(ModItem {
-    Import(FileItemTreeId<Import>),
-    ExternCrate(FileItemTreeId<ExternCrate>),
-    Function(FileItemTreeId<Function>),
-    Struct(FileItemTreeId<Struct>),
-    Union(FileItemTreeId<Union>),
-    Enum(FileItemTreeId<Enum>),
-    Const(FileItemTreeId<Const>),
-    Static(FileItemTreeId<Static>),
-    Trait(FileItemTreeId<Trait>),
-    Impl(FileItemTreeId<Impl>),
-    TypeAlias(FileItemTreeId<TypeAlias>),
-    Mod(FileItemTreeId<Mod>),
-    MacroCall(FileItemTreeId<MacroCall>),
-});
-
 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
 pub enum AssocItem {
     Function(FileItemTreeId<Function>),