diff options
Diffstat (limited to 'crates/ra_hir/src/impl_block.rs')
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 36d72b103..5fa49d456 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -9,9 +9,11 @@ ast::{self, AstNode}}; | |||
9 | use crate::{ | 9 | use crate::{ |
10 | Const, Type, | 10 | Const, Type, |
11 | Function, HirFileId, | 11 | Function, HirFileId, |
12 | HirDatabase, | ||
12 | PersistentHirDatabase, | 13 | PersistentHirDatabase, |
13 | type_ref::TypeRef, | 14 | type_ref::TypeRef, |
14 | ids::LocationCtx, | 15 | ids::LocationCtx, |
16 | resolve::Resolver, | ||
15 | }; | 17 | }; |
16 | 18 | ||
17 | use crate::code_model_api::{Module, ModuleSource}; | 19 | use crate::code_model_api::{Module, ModuleSource}; |
@@ -69,6 +71,10 @@ impl ImplBlock { | |||
69 | &self.module_impl_blocks.impls[self.impl_id] | 71 | &self.module_impl_blocks.impls[self.impl_id] |
70 | } | 72 | } |
71 | 73 | ||
74 | pub fn module(&self) -> Module { | ||
75 | self.module_impl_blocks.module.clone() | ||
76 | } | ||
77 | |||
72 | pub fn target_trait(&self) -> Option<&TypeRef> { | 78 | pub fn target_trait(&self) -> Option<&TypeRef> { |
73 | self.impl_data().target_trait() | 79 | self.impl_data().target_trait() |
74 | } | 80 | } |
@@ -80,6 +86,13 @@ impl ImplBlock { | |||
80 | pub fn items(&self) -> &[ImplItem] { | 86 | pub fn items(&self) -> &[ImplItem] { |
81 | self.impl_data().items() | 87 | self.impl_data().items() |
82 | } | 88 | } |
89 | |||
90 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { | ||
91 | let r = self.module().resolver(db); | ||
92 | // FIXME: add generics | ||
93 | let r = r.push_impl_block_scope(self.clone()); | ||
94 | r | ||
95 | } | ||
83 | } | 96 | } |
84 | 97 | ||
85 | #[derive(Debug, Clone, PartialEq, Eq)] | 98 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -162,25 +175,24 @@ impl_arena_id!(ImplId); | |||
162 | /// we don't need to do the second step again. | 175 | /// we don't need to do the second step again. |
163 | #[derive(Debug, PartialEq, Eq)] | 176 | #[derive(Debug, PartialEq, Eq)] |
164 | pub struct ModuleImplBlocks { | 177 | pub struct ModuleImplBlocks { |
178 | module: Module, | ||
165 | pub(crate) impls: Arena<ImplId, ImplData>, | 179 | pub(crate) impls: Arena<ImplId, ImplData>, |
166 | impls_by_def: FxHashMap<ImplItem, ImplId>, | 180 | impls_by_def: FxHashMap<ImplItem, ImplId>, |
167 | } | 181 | } |
168 | 182 | ||
169 | impl ModuleImplBlocks { | 183 | impl ModuleImplBlocks { |
170 | fn new() -> Self { | ||
171 | ModuleImplBlocks { | ||
172 | impls: Arena::default(), | ||
173 | impls_by_def: FxHashMap::default(), | ||
174 | } | ||
175 | } | ||
176 | |||
177 | fn collect( | 184 | fn collect( |
178 | &mut self, | ||
179 | db: &impl PersistentHirDatabase, | 185 | db: &impl PersistentHirDatabase, |
180 | module: Module, | 186 | module: Module, |
181 | source_map: &mut ImplSourceMap, | 187 | source_map: &mut ImplSourceMap, |
182 | ) { | 188 | ) -> Self { |
183 | let (file_id, module_source) = module.definition_source(db); | 189 | let mut m = ModuleImplBlocks { |
190 | module, | ||
191 | impls: Arena::default(), | ||
192 | impls_by_def: FxHashMap::default(), | ||
193 | }; | ||
194 | |||
195 | let (file_id, module_source) = m.module.definition_source(db); | ||
184 | let file_id: HirFileId = file_id.into(); | 196 | let file_id: HirFileId = file_id.into(); |
185 | let node = match &module_source { | 197 | let node = match &module_source { |
186 | ModuleSource::SourceFile(node) => node.syntax(), | 198 | ModuleSource::SourceFile(node) => node.syntax(), |
@@ -191,14 +203,16 @@ impl ModuleImplBlocks { | |||
191 | }; | 203 | }; |
192 | 204 | ||
193 | for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) { | 205 | for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) { |
194 | let impl_block = ImplData::from_ast(db, file_id, module, impl_block_ast); | 206 | let impl_block = ImplData::from_ast(db, file_id, m.module, impl_block_ast); |
195 | let id = self.impls.alloc(impl_block); | 207 | let id = m.impls.alloc(impl_block); |
196 | for &impl_item in &self.impls[id].items { | 208 | for &impl_item in &m.impls[id].items { |
197 | self.impls_by_def.insert(impl_item, id); | 209 | m.impls_by_def.insert(impl_item, id); |
198 | } | 210 | } |
199 | 211 | ||
200 | source_map.insert(id, impl_block_ast); | 212 | source_map.insert(id, impl_block_ast); |
201 | } | 213 | } |
214 | |||
215 | m | ||
202 | } | 216 | } |
203 | } | 217 | } |
204 | 218 | ||
@@ -208,8 +222,7 @@ pub(crate) fn impls_in_module_with_source_map_query( | |||
208 | ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) { | 222 | ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) { |
209 | let mut source_map = ImplSourceMap::default(); | 223 | let mut source_map = ImplSourceMap::default(); |
210 | 224 | ||
211 | let mut result = ModuleImplBlocks::new(); | 225 | let result = ModuleImplBlocks::collect(db, module, &mut source_map); |
212 | result.collect(db, module, &mut source_map); | ||
213 | 226 | ||
214 | (Arc::new(result), Arc::new(source_map)) | 227 | (Arc::new(result), Arc::new(source_map)) |
215 | } | 228 | } |