From 66e5ced894e430117d4d040e13721313237936ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 14:44:15 +0200 Subject: Remove AttrDef --- crates/hir/src/attrs.rs | 81 +++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 39 deletions(-) (limited to 'crates/hir/src/attrs.rs') diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index 953960e14..36027d183 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -4,60 +4,63 @@ use hir_def::{ db::DefDatabase, docs::Documentation, resolver::{HasResolver, Resolver}, - AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, + AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, }; use hir_ty::db::HirDatabase; -use stdx::impl_from; use crate::{ doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, }; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AttrDef { - Module(Module), - Field(Field), - Adt(Adt), - Function(Function), - EnumVariant(EnumVariant), - Static(Static), - Const(Const), - Trait(Trait), - TypeAlias(TypeAlias), - MacroDef(MacroDef), -} - -impl_from!( - Module, - Field, - Adt(Struct, Enum, Union), - EnumVariant, - Static, - Const, - Function, - Trait, - TypeAlias, - MacroDef - for AttrDef -); - pub trait HasAttrs { fn attrs(self, db: &dyn HirDatabase) -> Attrs; fn docs(self, db: &dyn HirDatabase) -> Option; } -impl> HasAttrs for T { - fn attrs(self, db: &dyn HirDatabase) -> Attrs { - let def: AttrDef = self.into(); - db.attrs(def.into()) - } - fn docs(self, db: &dyn HirDatabase) -> Option { - let def: AttrDef = self.into(); - db.documentation(def.into()) - } +macro_rules! impl_has_attrs { + ($(($def:ident, $def_id:ident),)*) => {$( + impl HasAttrs for $def { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { + let def = AttrDefId::$def_id(self.into()); + db.attrs(def) + } + fn docs(self, db: &dyn HirDatabase) -> Option { + let def = AttrDefId::$def_id(self.into()); + db.documentation(def) + } + } + )*}; +} + +impl_has_attrs![ + (Field, FieldId), + (EnumVariant, EnumVariantId), + (Static, StaticId), + (Const, ConstId), + (Trait, TraitId), + (TypeAlias, TypeAliasId), + (MacroDef, MacroDefId), + (Function, FunctionId), + (Adt, AdtId), + (Module, ModuleId), +]; + +macro_rules! impl_has_attrs_adt { + ($($adt:ident),*) => {$( + impl HasAttrs for $adt { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { + Adt::$adt(self).attrs(db) + } + fn docs(self, db: &dyn HirDatabase) -> Option { + Adt::$adt(self).docs(db) + } + } + )*}; } +impl_has_attrs_adt![Struct, Union, Enum]; + impl Resolvable for ModuleDef { fn resolver(&self, db: &D) -> Option { Some(match self { -- cgit v1.2.3