aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-25 11:56:01 +0100
committerAleksey Kladov <[email protected]>2020-08-25 11:56:01 +0100
commit575fb9ab6a5f3d0dc60a8fc811ba9833f5f81b4e (patch)
treef644cb813f6e7f39b63030861b4ee8bda3ae8d6d /crates
parent663749beab2eeaaaf2b7d24fe2f721237d1d4dc0 (diff)
Move attrs from code_module to a separate module
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/attrs.rs132
-rw-r--r--crates/hir/src/code_model.rs123
-rw-r--r--crates/hir/src/lib.rs11
3 files changed, 140 insertions, 126 deletions
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
new file mode 100644
index 000000000..2603411eb
--- /dev/null
+++ b/crates/hir/src/attrs.rs
@@ -0,0 +1,132 @@
1use hir_def::{
2 attr::Attrs,
3 db::DefDatabase,
4 docs::Documentation,
5 resolver::{HasResolver, Resolver},
6 AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId,
7};
8use hir_ty::db::HirDatabase;
9use stdx::impl_from;
10
11use crate::{
12 doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef,
13 Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union,
14 VariantDef,
15};
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18pub enum AttrDef {
19 Module(Module),
20 Field(Field),
21 Adt(Adt),
22 Function(Function),
23 EnumVariant(EnumVariant),
24 Static(Static),
25 Const(Const),
26 Trait(Trait),
27 TypeAlias(TypeAlias),
28 MacroDef(MacroDef),
29}
30
31impl_from!(
32 Module,
33 Field,
34 Adt(Struct, Enum, Union),
35 EnumVariant,
36 Static,
37 Const,
38 Function,
39 Trait,
40 TypeAlias,
41 MacroDef
42 for AttrDef
43);
44
45pub trait HasAttrs {
46 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
47 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
48}
49
50impl<T: Into<AttrDef>> HasAttrs for T {
51 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
52 let def: AttrDef = self.into();
53 db.attrs(def.into())
54 }
55 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
56 let def: AttrDef = self.into();
57 db.documentation(def.into())
58 }
59}
60
61impl Resolvable for ModuleDef {
62 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
63 Some(match self {
64 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db),
65 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db),
66 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db),
67 ModuleDef::EnumVariant(ev) => {
68 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db)
69 }
70 ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db),
71 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db),
72 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db),
73 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db),
74 // FIXME: This should be a resolver relative to `std/core`
75 ModuleDef::BuiltinType(_t) => None?,
76 })
77 }
78
79 fn try_into_module_def(self) -> Option<ModuleDef> {
80 Some(self)
81 }
82}
83
84impl Resolvable for TypeParam {
85 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
86 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
87 }
88
89 fn try_into_module_def(self) -> Option<ModuleDef> {
90 None
91 }
92}
93
94impl Resolvable for MacroDef {
95 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
96 Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
97 }
98
99 fn try_into_module_def(self) -> Option<ModuleDef> {
100 None
101 }
102}
103
104impl Resolvable for Field {
105 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
106 Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
107 }
108
109 fn try_into_module_def(self) -> Option<ModuleDef> {
110 None
111 }
112}
113
114impl Resolvable for ImplDef {
115 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
116 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
117 }
118
119 fn try_into_module_def(self) -> Option<ModuleDef> {
120 None
121 }
122}
123
124impl Resolvable for Local {
125 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
126 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
127 }
128
129 fn try_into_module_def(self) -> Option<ModuleDef> {
130 None
131 }
132}
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index e553fc7dc..59d8b3073 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -9,7 +9,6 @@ use hir_def::{
9 adt::StructKind, 9 adt::StructKind,
10 adt::VariantData, 10 adt::VariantData,
11 builtin_type::BuiltinType, 11 builtin_type::BuiltinType,
12 docs::Documentation,
13 expr::{BindingAnnotation, Pat, PatId}, 12 expr::{BindingAnnotation, Pat, PatId},
14 import_map, 13 import_map,
15 lang_item::LangItemTarget, 14 lang_item::LangItemTarget,
@@ -20,7 +19,7 @@ use hir_def::{
20 type_ref::{Mutability, TypeRef}, 19 type_ref::{Mutability, TypeRef},
21 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, 20 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
22 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, 21 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
23 TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, 22 TraitId, TypeAliasId, TypeParamId, UnionId,
24}; 23};
25use hir_expand::{ 24use hir_expand::{
26 diagnostics::DiagnosticSink, 25 diagnostics::DiagnosticSink,
@@ -43,9 +42,8 @@ use tt::{Ident, Leaf, Literal, TokenTree};
43 42
44use crate::{ 43use crate::{
45 db::{DefDatabase, HirDatabase}, 44 db::{DefDatabase, HirDatabase},
46 doc_links::Resolvable,
47 has_source::HasSource, 45 has_source::HasSource,
48 HirDisplay, InFile, Name, 46 AttrDef, HirDisplay, InFile, Name,
49}; 47};
50 48
51/// hir::Crate describes a single crate. It's the main interface with which 49/// hir::Crate describes a single crate. It's the main interface with which
@@ -1741,50 +1739,6 @@ impl ScopeDef {
1741 } 1739 }
1742} 1740}
1743 1741
1744#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1745pub enum AttrDef {
1746 Module(Module),
1747 Field(Field),
1748 Adt(Adt),
1749 Function(Function),
1750 EnumVariant(EnumVariant),
1751 Static(Static),
1752 Const(Const),
1753 Trait(Trait),
1754 TypeAlias(TypeAlias),
1755 MacroDef(MacroDef),
1756}
1757
1758impl_from!(
1759 Module,
1760 Field,
1761 Adt(Struct, Enum, Union),
1762 EnumVariant,
1763 Static,
1764 Const,
1765 Function,
1766 Trait,
1767 TypeAlias,
1768 MacroDef
1769 for AttrDef
1770);
1771
1772pub trait HasAttrs {
1773 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
1774 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
1775}
1776
1777impl<T: Into<AttrDef>> HasAttrs for T {
1778 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
1779 let def: AttrDef = self.into();
1780 db.attrs(def.into())
1781 }
1782 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
1783 let def: AttrDef = self.into();
1784 db.documentation(def.into())
1785 }
1786}
1787
1788pub trait HasVisibility { 1742pub trait HasVisibility {
1789 fn visibility(&self, db: &dyn HirDatabase) -> Visibility; 1743 fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
1790 fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { 1744 fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
@@ -1792,76 +1746,3 @@ pub trait HasVisibility {
1792 vis.is_visible_from(db.upcast(), module.id) 1746 vis.is_visible_from(db.upcast(), module.id)
1793 } 1747 }
1794} 1748}
1795
1796impl Resolvable for ModuleDef {
1797 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1798 Some(match self {
1799 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db),
1800 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db),
1801 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db),
1802 ModuleDef::EnumVariant(ev) => {
1803 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db)
1804 }
1805 ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db),
1806 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db),
1807 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db),
1808 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db),
1809 // FIXME: This should be a resolver relative to `std/core`
1810 ModuleDef::BuiltinType(_t) => None?,
1811 })
1812 }
1813
1814 fn try_into_module_def(self) -> Option<ModuleDef> {
1815 Some(self)
1816 }
1817}
1818
1819impl Resolvable for TypeParam {
1820 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1821 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1822 }
1823
1824 fn try_into_module_def(self) -> Option<ModuleDef> {
1825 None
1826 }
1827}
1828
1829impl Resolvable for MacroDef {
1830 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1831 Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
1832 }
1833
1834 fn try_into_module_def(self) -> Option<ModuleDef> {
1835 None
1836 }
1837}
1838
1839impl Resolvable for Field {
1840 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1841 Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
1842 }
1843
1844 fn try_into_module_def(self) -> Option<ModuleDef> {
1845 None
1846 }
1847}
1848
1849impl Resolvable for ImplDef {
1850 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1851 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1852 }
1853
1854 fn try_into_module_def(self) -> Option<ModuleDef> {
1855 None
1856 }
1857}
1858
1859impl Resolvable for Local {
1860 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1861 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1862 }
1863
1864 fn try_into_module_def(self) -> Option<ModuleDef> {
1865 None
1866 }
1867}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 2326e3fbb..d1c198bff 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -28,15 +28,16 @@ pub mod diagnostics;
28mod from_id; 28mod from_id;
29mod code_model; 29mod code_model;
30mod doc_links; 30mod doc_links;
31 31mod attrs;
32mod has_source; 32mod has_source;
33 33
34pub use crate::{ 34pub use crate::{
35 attrs::{AttrDef, HasAttrs},
35 code_model::{ 36 code_model::{
36 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Callable, CallableKind, 37 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
37 Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, 38 Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
38 Function, GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, 39 GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
39 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, 40 Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
40 }, 41 },
41 doc_links::resolve_doc_link, 42 doc_links::resolve_doc_link,
42 has_source::HasSource, 43 has_source::HasSource,