aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body.rs22
-rw-r--r--crates/ra_hir_def/src/data.rs119
-rw-r--r--crates/ra_hir_def/src/lib.rs1
3 files changed, 94 insertions, 48 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 401fe0b9b..a92c01f86 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -23,7 +23,7 @@ use crate::{
23 DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId, 23 DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId,
24}; 24};
25 25
26struct Expander { 26pub(crate) struct Expander {
27 crate_def_map: Arc<CrateDefMap>, 27 crate_def_map: Arc<CrateDefMap>,
28 current_file_id: HirFileId, 28 current_file_id: HirFileId,
29 hygiene: Hygiene, 29 hygiene: Hygiene,
@@ -32,18 +32,22 @@ struct Expander {
32} 32}
33 33
34impl Expander { 34impl Expander {
35 fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander { 35 pub(crate) fn new(
36 db: &impl DefDatabase,
37 current_file_id: HirFileId,
38 module: ModuleId,
39 ) -> Expander {
36 let crate_def_map = db.crate_def_map(module.krate); 40 let crate_def_map = db.crate_def_map(module.krate);
37 let hygiene = Hygiene::new(db, current_file_id); 41 let hygiene = Hygiene::new(db, current_file_id);
38 let ast_id_map = db.ast_id_map(current_file_id); 42 let ast_id_map = db.ast_id_map(current_file_id);
39 Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module } 43 Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module }
40 } 44 }
41 45
42 fn enter_expand( 46 pub(crate) fn enter_expand<T: ast::AstNode, DB: DefDatabase>(
43 &mut self, 47 &mut self,
44 db: &impl DefDatabase, 48 db: &DB,
45 macro_call: ast::MacroCall, 49 macro_call: ast::MacroCall,
46 ) -> Option<(Mark, ast::Expr)> { 50 ) -> Option<(Mark, T)> {
47 let ast_id = AstId::new( 51 let ast_id = AstId::new(
48 self.current_file_id, 52 self.current_file_id,
49 db.ast_id_map(self.current_file_id).ast_id(&macro_call), 53 db.ast_id_map(self.current_file_id).ast_id(&macro_call),
@@ -54,7 +58,7 @@ impl Expander {
54 let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id)); 58 let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id));
55 let file_id = call_id.as_file(); 59 let file_id = call_id.as_file();
56 if let Some(node) = db.parse_or_expand(file_id) { 60 if let Some(node) = db.parse_or_expand(file_id) {
57 if let Some(expr) = ast::Expr::cast(node) { 61 if let Some(expr) = T::cast(node) {
58 log::debug!("macro expansion {:#?}", expr.syntax()); 62 log::debug!("macro expansion {:#?}", expr.syntax());
59 63
60 let mark = Mark { 64 let mark = Mark {
@@ -77,14 +81,14 @@ impl Expander {
77 None 81 None
78 } 82 }
79 83
80 fn exit(&mut self, db: &impl DefDatabase, mut mark: Mark) { 84 pub(crate) fn exit(&mut self, db: &impl DefDatabase, mut mark: Mark) {
81 self.hygiene = Hygiene::new(db, mark.file_id); 85 self.hygiene = Hygiene::new(db, mark.file_id);
82 self.current_file_id = mark.file_id; 86 self.current_file_id = mark.file_id;
83 self.ast_id_map = mem::take(&mut mark.ast_id_map); 87 self.ast_id_map = mem::take(&mut mark.ast_id_map);
84 mark.bomb.defuse(); 88 mark.bomb.defuse();
85 } 89 }
86 90
87 fn to_source<T>(&self, value: T) -> InFile<T> { 91 pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
88 InFile { file_id: self.current_file_id, value } 92 InFile { file_id: self.current_file_id, value }
89 } 93 }
90 94
@@ -109,7 +113,7 @@ impl Expander {
109 } 113 }
110} 114}
111 115
112struct Mark { 116pub(crate) struct Mark {
113 file_id: HirFileId, 117 file_id: HirFileId,
114 ast_id_map: Arc<AstIdMap>, 118 ast_id_map: Arc<AstIdMap>,
115 bomb: DropBomb, 119 bomb: DropBomb,
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 14e86936b..b0a3f1784 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -4,16 +4,16 @@ use std::sync::Arc;
4 4
5use hir_expand::{ 5use hir_expand::{
6 name::{name, AsName, Name}, 6 name::{name, AsName, Name},
7 AstId, 7 AstId, InFile,
8}; 8};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 9use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner};
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::HasSource, 13 src::HasSource,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, ImplId, Intern, 15 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule,
16 Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17}; 17};
18 18
19#[derive(Debug, Clone, PartialEq, Eq)] 19#[derive(Debug, Clone, PartialEq, Eq)]
@@ -167,46 +167,24 @@ pub struct ImplData {
167 167
168impl ImplData { 168impl ImplData {
169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> { 169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> {
170 let src = id.lookup(db).source(db); 170 let impl_loc = id.lookup(db);
171 let items = db.ast_id_map(src.file_id); 171 let src = impl_loc.source(db);
172 172
173 let target_trait = src.value.target_trait().map(TypeRef::from_ast); 173 let target_trait = src.value.target_trait().map(TypeRef::from_ast);
174 let target_type = TypeRef::from_ast_opt(src.value.target_type()); 174 let target_type = TypeRef::from_ast_opt(src.value.target_type());
175 let is_negative = src.value.is_negative(); 175 let is_negative = src.value.is_negative();
176 let module_id = impl_loc.container.module(db);
176 177
177 let items = if let Some(item_list) = src.value.item_list() { 178 let mut items = Vec::new();
178 item_list 179 if let Some(item_list) = src.value.item_list() {
179 .impl_items() 180 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id));
180 .map(|item_node| match item_node { 181 items.extend(collect_impl_items_in_macros(
181 ast::ImplItem::FnDef(it) => { 182 db,
182 let def = FunctionLoc { 183 module_id,
183 container: AssocContainerId::ImplId(id), 184 &src.with_value(item_list),
184 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 185 id,
185 } 186 ));
186 .intern(db); 187 }
187 def.into()
188 }
189 ast::ImplItem::ConstDef(it) => {
190 let def = ConstLoc {
191 container: AssocContainerId::ImplId(id),
192 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
193 }
194 .intern(db);
195 def.into()
196 }
197 ast::ImplItem::TypeAliasDef(it) => {
198 let def = TypeAliasLoc {
199 container: AssocContainerId::ImplId(id),
200 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
201 }
202 .intern(db);
203 def.into()
204 }
205 })
206 .collect()
207 } else {
208 Vec::new()
209 };
210 188
211 let res = ImplData { target_trait, target_type, items, is_negative }; 189 let res = ImplData { target_trait, target_type, items, is_negative };
212 Arc::new(res) 190 Arc::new(res)
@@ -237,3 +215,66 @@ impl ConstData {
237 ConstData { name, type_ref } 215 ConstData { name, type_ref }
238 } 216 }
239} 217}
218
219fn collect_impl_items_in_macros(
220 db: &impl DefDatabase,
221 module_id: ModuleId,
222 impl_block: &InFile<ast::ItemList>,
223 id: ImplId,
224) -> Vec<AssocItemId> {
225 let mut expander = Expander::new(db, impl_block.file_id, module_id);
226 let mut res = Vec::new();
227
228 for m in impl_block.value.syntax().children().filter_map(ast::MacroCall::cast) {
229 if let Some((mark, items)) = expander.enter_expand(db, m) {
230 let items: InFile<ast::MacroItems> = expander.to_source(items);
231 expander.exit(db, mark);
232 res.extend(collect_impl_items(
233 db,
234 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())),
235 items.file_id,
236 id,
237 ));
238 }
239 }
240
241 res
242}
243
244fn collect_impl_items(
245 db: &impl DefDatabase,
246 impl_items: impl Iterator<Item = ImplItem>,
247 file_id: crate::HirFileId,
248 id: ImplId,
249) -> Vec<AssocItemId> {
250 let items = db.ast_id_map(file_id);
251
252 impl_items
253 .map(|item_node| match item_node {
254 ast::ImplItem::FnDef(it) => {
255 let def = FunctionLoc {
256 container: AssocContainerId::ImplId(id),
257 ast_id: AstId::new(file_id, items.ast_id(&it)),
258 }
259 .intern(db);
260 def.into()
261 }
262 ast::ImplItem::ConstDef(it) => {
263 let def = ConstLoc {
264 container: AssocContainerId::ImplId(id),
265 ast_id: AstId::new(file_id, items.ast_id(&it)),
266 }
267 .intern(db);
268 def.into()
269 }
270 ast::ImplItem::TypeAliasDef(it) => {
271 let def = TypeAliasLoc {
272 container: AssocContainerId::ImplId(id),
273 ast_id: AstId::new(file_id, items.ast_id(&it)),
274 }
275 .intern(db);
276 def.into()
277 }
278 })
279 .collect()
280}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 8ed1599ff..6bb5408a8 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -47,6 +47,7 @@ use ra_arena::{impl_arena_id, RawId};
47use ra_db::{impl_intern_key, salsa, CrateId}; 47use ra_db::{impl_intern_key, salsa, CrateId};
48use ra_syntax::{ast, AstNode}; 48use ra_syntax::{ast, AstNode};
49 49
50use crate::body::Expander;
50use crate::builtin_type::BuiltinType; 51use crate::builtin_type::BuiltinType;
51 52
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]