diff options
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 192 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir_def/src/impls.rs | 86 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/traits.rs | 66 | ||||
-rw-r--r-- | crates/ra_hir_def/src/type_alias.rs | 27 |
6 files changed, 199 insertions, 186 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs new file mode 100644 index 000000000..ba47629db --- /dev/null +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -0,0 +1,192 @@ | |||
1 | //! Contains basic data about various HIR declarations. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir_expand::{ | ||
6 | name::{self, AsName, Name}, | ||
7 | AstId, | ||
8 | }; | ||
9 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | ||
10 | |||
11 | use crate::{ | ||
12 | db::DefDatabase2, | ||
13 | type_ref::{Mutability, TypeRef}, | ||
14 | AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, ImplId, | ||
15 | Intern, Lookup, TraitId, TypeAliasId, TypeAliasLoc, | ||
16 | }; | ||
17 | |||
18 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
19 | pub struct FunctionData { | ||
20 | pub name: Name, | ||
21 | pub params: Vec<TypeRef>, | ||
22 | pub ret_type: TypeRef, | ||
23 | /// True if the first param is `self`. This is relevant to decide whether this | ||
24 | /// can be called as a method. | ||
25 | pub has_self_param: bool, | ||
26 | } | ||
27 | |||
28 | impl FunctionData { | ||
29 | pub(crate) fn fn_data_query(db: &impl DefDatabase2, func: FunctionId) -> Arc<FunctionData> { | ||
30 | let src = func.lookup(db).source(db); | ||
31 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
32 | let mut params = Vec::new(); | ||
33 | let mut has_self_param = false; | ||
34 | if let Some(param_list) = src.value.param_list() { | ||
35 | if let Some(self_param) = param_list.self_param() { | ||
36 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | ||
37 | TypeRef::from_ast(type_ref) | ||
38 | } else { | ||
39 | let self_type = TypeRef::Path(name::SELF_TYPE.into()); | ||
40 | match self_param.kind() { | ||
41 | ast::SelfParamKind::Owned => self_type, | ||
42 | ast::SelfParamKind::Ref => { | ||
43 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
44 | } | ||
45 | ast::SelfParamKind::MutRef => { | ||
46 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
47 | } | ||
48 | } | ||
49 | }; | ||
50 | params.push(self_type); | ||
51 | has_self_param = true; | ||
52 | } | ||
53 | for param in param_list.params() { | ||
54 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
55 | params.push(type_ref); | ||
56 | } | ||
57 | } | ||
58 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { | ||
59 | TypeRef::from_ast(type_ref) | ||
60 | } else { | ||
61 | TypeRef::unit() | ||
62 | }; | ||
63 | |||
64 | let sig = FunctionData { name, params, ret_type, has_self_param }; | ||
65 | Arc::new(sig) | ||
66 | } | ||
67 | } | ||
68 | |||
69 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
70 | pub struct TypeAliasData { | ||
71 | pub name: Name, | ||
72 | pub type_ref: Option<TypeRef>, | ||
73 | } | ||
74 | |||
75 | impl TypeAliasData { | ||
76 | pub(crate) fn type_alias_data_query( | ||
77 | db: &impl DefDatabase2, | ||
78 | typ: TypeAliasId, | ||
79 | ) -> Arc<TypeAliasData> { | ||
80 | let node = typ.lookup(db).source(db).value; | ||
81 | let name = node.name().map_or_else(Name::missing, |n| n.as_name()); | ||
82 | let type_ref = node.type_ref().map(TypeRef::from_ast); | ||
83 | Arc::new(TypeAliasData { name, type_ref }) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
88 | pub struct TraitData { | ||
89 | pub name: Option<Name>, | ||
90 | pub items: Vec<AssocItemId>, | ||
91 | pub auto: bool, | ||
92 | } | ||
93 | |||
94 | impl TraitData { | ||
95 | pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> { | ||
96 | let src = tr.source(db); | ||
97 | let name = src.value.name().map(|n| n.as_name()); | ||
98 | let auto = src.value.is_auto(); | ||
99 | let ast_id_map = db.ast_id_map(src.file_id); | ||
100 | let items = if let Some(item_list) = src.value.item_list() { | ||
101 | item_list | ||
102 | .impl_items() | ||
103 | .map(|item_node| match item_node { | ||
104 | ast::ImplItem::FnDef(it) => FunctionLoc { | ||
105 | container: ContainerId::TraitId(tr), | ||
106 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
107 | } | ||
108 | .intern(db) | ||
109 | .into(), | ||
110 | ast::ImplItem::ConstDef(it) => ConstLoc { | ||
111 | container: ContainerId::TraitId(tr), | ||
112 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
113 | } | ||
114 | .intern(db) | ||
115 | .into(), | ||
116 | ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { | ||
117 | container: ContainerId::TraitId(tr), | ||
118 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
119 | } | ||
120 | .intern(db) | ||
121 | .into(), | ||
122 | }) | ||
123 | .collect() | ||
124 | } else { | ||
125 | Vec::new() | ||
126 | }; | ||
127 | Arc::new(TraitData { name, items, auto }) | ||
128 | } | ||
129 | |||
130 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { | ||
131 | self.items.iter().filter_map(|item| match item { | ||
132 | AssocItemId::TypeAliasId(t) => Some(*t), | ||
133 | _ => None, | ||
134 | }) | ||
135 | } | ||
136 | } | ||
137 | |||
138 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
139 | pub struct ImplData { | ||
140 | pub target_trait: Option<TypeRef>, | ||
141 | pub target_type: TypeRef, | ||
142 | pub items: Vec<AssocItemId>, | ||
143 | pub is_negative: bool, | ||
144 | } | ||
145 | |||
146 | impl ImplData { | ||
147 | pub(crate) fn impl_data_query(db: &impl DefDatabase2, id: ImplId) -> Arc<ImplData> { | ||
148 | let src = id.source(db); | ||
149 | let items = db.ast_id_map(src.file_id); | ||
150 | |||
151 | let target_trait = src.value.target_trait().map(TypeRef::from_ast); | ||
152 | let target_type = TypeRef::from_ast_opt(src.value.target_type()); | ||
153 | let is_negative = src.value.is_negative(); | ||
154 | |||
155 | let items = if let Some(item_list) = src.value.item_list() { | ||
156 | item_list | ||
157 | .impl_items() | ||
158 | .map(|item_node| match item_node { | ||
159 | ast::ImplItem::FnDef(it) => { | ||
160 | let def = FunctionLoc { | ||
161 | container: ContainerId::ImplId(id), | ||
162 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
163 | } | ||
164 | .intern(db); | ||
165 | def.into() | ||
166 | } | ||
167 | ast::ImplItem::ConstDef(it) => { | ||
168 | let def = ConstLoc { | ||
169 | container: ContainerId::ImplId(id), | ||
170 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
171 | } | ||
172 | .intern(db); | ||
173 | def.into() | ||
174 | } | ||
175 | ast::ImplItem::TypeAliasDef(it) => { | ||
176 | let def = TypeAliasLoc { | ||
177 | container: ContainerId::ImplId(id), | ||
178 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
179 | } | ||
180 | .intern(db); | ||
181 | def.into() | ||
182 | } | ||
183 | }) | ||
184 | .collect() | ||
185 | } else { | ||
186 | Vec::new() | ||
187 | }; | ||
188 | |||
189 | let res = ImplData { target_trait, target_type, items, is_negative }; | ||
190 | Arc::new(res) | ||
191 | } | ||
192 | } | ||
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 5bbdaa4b2..8c1784ec9 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -8,15 +8,14 @@ use ra_syntax::ast; | |||
8 | use crate::{ | 8 | use crate::{ |
9 | adt::{EnumData, StructData}, | 9 | adt::{EnumData, StructData}, |
10 | body::{scope::ExprScopes, Body, BodySourceMap}, | 10 | body::{scope::ExprScopes, Body, BodySourceMap}, |
11 | data::{FunctionData, ImplData, TraitData, TypeAliasData}, | ||
11 | generics::GenericParams, | 12 | generics::GenericParams, |
12 | impls::ImplData, | ||
13 | nameres::{ | 13 | nameres::{ |
14 | raw::{ImportSourceMap, RawItems}, | 14 | raw::{ImportSourceMap, RawItems}, |
15 | CrateDefMap, | 15 | CrateDefMap, |
16 | }, | 16 | }, |
17 | traits::TraitData, | 17 | DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, |
18 | type_alias::TypeAliasData, | 18 | TypeAliasId, |
19 | DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, TypeAliasId, | ||
20 | }; | 19 | }; |
21 | 20 | ||
22 | #[salsa::query_group(InternDatabaseStorage)] | 21 | #[salsa::query_group(InternDatabaseStorage)] |
@@ -68,6 +67,9 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { | |||
68 | #[salsa::invoke(TypeAliasData::type_alias_data_query)] | 67 | #[salsa::invoke(TypeAliasData::type_alias_data_query)] |
69 | fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>; | 68 | fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>; |
70 | 69 | ||
70 | #[salsa::invoke(FunctionData::fn_data_query)] | ||
71 | fn function_data(&self, func: FunctionId) -> Arc<FunctionData>; | ||
72 | |||
71 | #[salsa::invoke(Body::body_with_source_map_query)] | 73 | #[salsa::invoke(Body::body_with_source_map_query)] |
72 | fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); | 74 | fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); |
73 | 75 | ||
diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs deleted file mode 100644 index 750a869f2..000000000 --- a/crates/ra_hir_def/src/impls.rs +++ /dev/null | |||
@@ -1,86 +0,0 @@ | |||
1 | //! Defines hir-level representation of impls. | ||
2 | //! | ||
3 | //! The handling is similar, but is not quite the same as for other items, | ||
4 | //! because `impl`s don't have names. | ||
5 | |||
6 | use std::sync::Arc; | ||
7 | |||
8 | use hir_expand::AstId; | ||
9 | use ra_syntax::ast; | ||
10 | |||
11 | use crate::{ | ||
12 | db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId, | ||
13 | FunctionLoc, ImplId, Intern, TypeAliasLoc, | ||
14 | }; | ||
15 | |||
16 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
17 | pub struct ImplData { | ||
18 | target_trait: Option<TypeRef>, | ||
19 | target_type: TypeRef, | ||
20 | items: Vec<AssocItemId>, | ||
21 | negative: bool, | ||
22 | } | ||
23 | |||
24 | impl ImplData { | ||
25 | pub(crate) fn impl_data_query(db: &impl DefDatabase2, id: ImplId) -> Arc<ImplData> { | ||
26 | let src = id.source(db); | ||
27 | let items = db.ast_id_map(src.file_id); | ||
28 | |||
29 | let target_trait = src.value.target_trait().map(TypeRef::from_ast); | ||
30 | let target_type = TypeRef::from_ast_opt(src.value.target_type()); | ||
31 | let negative = src.value.is_negative(); | ||
32 | |||
33 | let items = if let Some(item_list) = src.value.item_list() { | ||
34 | item_list | ||
35 | .impl_items() | ||
36 | .map(|item_node| match item_node { | ||
37 | ast::ImplItem::FnDef(it) => { | ||
38 | let def = FunctionLoc { | ||
39 | container: ContainerId::ImplId(id), | ||
40 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
41 | } | ||
42 | .intern(db); | ||
43 | def.into() | ||
44 | } | ||
45 | ast::ImplItem::ConstDef(it) => { | ||
46 | let def = ConstLoc { | ||
47 | container: ContainerId::ImplId(id), | ||
48 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
49 | } | ||
50 | .intern(db); | ||
51 | def.into() | ||
52 | } | ||
53 | ast::ImplItem::TypeAliasDef(it) => { | ||
54 | let def = TypeAliasLoc { | ||
55 | container: ContainerId::ImplId(id), | ||
56 | ast_id: AstId::new(src.file_id, items.ast_id(&it)), | ||
57 | } | ||
58 | .intern(db); | ||
59 | def.into() | ||
60 | } | ||
61 | }) | ||
62 | .collect() | ||
63 | } else { | ||
64 | Vec::new() | ||
65 | }; | ||
66 | |||
67 | let res = ImplData { target_trait, target_type, items, negative }; | ||
68 | Arc::new(res) | ||
69 | } | ||
70 | |||
71 | pub fn target_trait(&self) -> Option<&TypeRef> { | ||
72 | self.target_trait.as_ref() | ||
73 | } | ||
74 | |||
75 | pub fn target_type(&self) -> &TypeRef { | ||
76 | &self.target_type | ||
77 | } | ||
78 | |||
79 | pub fn items(&self) -> &[AssocItemId] { | ||
80 | &self.items | ||
81 | } | ||
82 | |||
83 | pub fn is_negative(&self) -> bool { | ||
84 | self.negative | ||
85 | } | ||
86 | } | ||
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 268144462..3a0420da0 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -13,14 +13,12 @@ pub mod path; | |||
13 | pub mod type_ref; | 13 | pub mod type_ref; |
14 | pub mod builtin_type; | 14 | pub mod builtin_type; |
15 | pub mod adt; | 15 | pub mod adt; |
16 | pub mod impls; | ||
17 | pub mod diagnostics; | 16 | pub mod diagnostics; |
18 | pub mod expr; | 17 | pub mod expr; |
19 | pub mod body; | 18 | pub mod body; |
20 | pub mod generics; | 19 | pub mod generics; |
21 | pub mod traits; | ||
22 | pub mod resolver; | 20 | pub mod resolver; |
23 | pub mod type_alias; | 21 | pub mod data; |
24 | 22 | ||
25 | #[cfg(test)] | 23 | #[cfg(test)] |
26 | mod test_db; | 24 | mod test_db; |
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs deleted file mode 100644 index 6c2d5b2a9..000000000 --- a/crates/ra_hir_def/src/traits.rs +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | //! HIR for trait definitions. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir_expand::{ | ||
6 | name::{AsName, Name}, | ||
7 | AstId, | ||
8 | }; | ||
9 | |||
10 | use ra_syntax::ast::{self, NameOwner}; | ||
11 | |||
12 | use crate::{ | ||
13 | db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId, | ||
14 | TypeAliasId, TypeAliasLoc, | ||
15 | }; | ||
16 | |||
17 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
18 | pub struct TraitData { | ||
19 | pub name: Option<Name>, | ||
20 | pub items: Vec<AssocItemId>, | ||
21 | pub auto: bool, | ||
22 | } | ||
23 | |||
24 | impl TraitData { | ||
25 | pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> { | ||
26 | let src = tr.source(db); | ||
27 | let name = src.value.name().map(|n| n.as_name()); | ||
28 | let auto = src.value.is_auto(); | ||
29 | let ast_id_map = db.ast_id_map(src.file_id); | ||
30 | let items = if let Some(item_list) = src.value.item_list() { | ||
31 | item_list | ||
32 | .impl_items() | ||
33 | .map(|item_node| match item_node { | ||
34 | ast::ImplItem::FnDef(it) => FunctionLoc { | ||
35 | container: ContainerId::TraitId(tr), | ||
36 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
37 | } | ||
38 | .intern(db) | ||
39 | .into(), | ||
40 | ast::ImplItem::ConstDef(it) => ConstLoc { | ||
41 | container: ContainerId::TraitId(tr), | ||
42 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
43 | } | ||
44 | .intern(db) | ||
45 | .into(), | ||
46 | ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { | ||
47 | container: ContainerId::TraitId(tr), | ||
48 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
49 | } | ||
50 | .intern(db) | ||
51 | .into(), | ||
52 | }) | ||
53 | .collect() | ||
54 | } else { | ||
55 | Vec::new() | ||
56 | }; | ||
57 | Arc::new(TraitData { name, items, auto }) | ||
58 | } | ||
59 | |||
60 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { | ||
61 | self.items.iter().filter_map(|item| match item { | ||
62 | AssocItemId::TypeAliasId(t) => Some(*t), | ||
63 | _ => None, | ||
64 | }) | ||
65 | } | ||
66 | } | ||
diff --git a/crates/ra_hir_def/src/type_alias.rs b/crates/ra_hir_def/src/type_alias.rs deleted file mode 100644 index c0b49aa7c..000000000 --- a/crates/ra_hir_def/src/type_alias.rs +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | //! HIR for type aliases (i.e. the `type` keyword). | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir_expand::name::{AsName, Name}; | ||
6 | |||
7 | use ra_syntax::ast::NameOwner; | ||
8 | |||
9 | use crate::{db::DefDatabase2, type_ref::TypeRef, HasSource, Lookup, TypeAliasId}; | ||
10 | |||
11 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
12 | pub struct TypeAliasData { | ||
13 | pub name: Name, | ||
14 | pub type_ref: Option<TypeRef>, | ||
15 | } | ||
16 | |||
17 | impl TypeAliasData { | ||
18 | pub(crate) fn type_alias_data_query( | ||
19 | db: &impl DefDatabase2, | ||
20 | typ: TypeAliasId, | ||
21 | ) -> Arc<TypeAliasData> { | ||
22 | let node = typ.lookup(db).source(db).value; | ||
23 | let name = node.name().map_or_else(Name::missing, |n| n.as_name()); | ||
24 | let type_ref = node.type_ref().map(TypeRef::from_ast); | ||
25 | Arc::new(TypeAliasData { name, type_ref }) | ||
26 | } | ||
27 | } | ||