aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model_api.rs15
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs22
-rw-r--r--crates/ra_hir/src/ids.rs5
-rw-r--r--crates/ra_hir/src/impl_block.rs43
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs29
5 files changed, 48 insertions, 66 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index fb7ad0867..99f004905 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -1,7 +1,7 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, FileId, SourceRootId, Edition}; 4use ra_db::{CrateId, SourceRootId, Edition};
5use ra_syntax::{ast::self, TreeArc, SyntaxNode}; 5use ra_syntax::{ast::self, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
@@ -16,7 +16,7 @@ use crate::{
16 docs::{Documentation, Docs, docs_from_ast}, 16 docs::{Documentation, Docs, docs_from_ast},
17 module_tree::ModuleId, 17 module_tree::ModuleId,
18 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, 18 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId},
19 impl_block::{ImplId, ImplBlock}, 19 impl_block::ImplBlock,
20 resolve::Resolver, 20 resolve::Resolver,
21}; 21};
22 22
@@ -107,7 +107,7 @@ impl Module {
107 } 107 }
108 108
109 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 109 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
110 pub fn definition_source(&self, db: &impl PersistentHirDatabase) -> (FileId, ModuleSource) { 110 pub fn definition_source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, ModuleSource) {
111 self.definition_source_impl(db) 111 self.definition_source_impl(db)
112 } 112 }
113 113
@@ -116,7 +116,7 @@ impl Module {
116 pub fn declaration_source( 116 pub fn declaration_source(
117 &self, 117 &self,
118 db: &impl HirDatabase, 118 db: &impl HirDatabase,
119 ) -> Option<(FileId, TreeArc<ast::Module>)> { 119 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
120 self.declaration_source_impl(db) 120 self.declaration_source_impl(db)
121 } 121 }
122 122
@@ -129,11 +129,6 @@ impl Module {
129 self.import_source_impl(db, import) 129 self.import_source_impl(db, import)
130 } 130 }
131 131
132 /// Returns the syntax of the impl block in this module
133 pub fn impl_source(&self, db: &impl HirDatabase, impl_id: ImplId) -> TreeArc<ast::ImplBlock> {
134 self.impl_source_impl(db, impl_id)
135 }
136
137 /// Returns the crate this module is part of. 132 /// Returns the crate this module is part of.
138 pub fn krate(&self, _db: &impl PersistentHirDatabase) -> Option<Crate> { 133 pub fn krate(&self, _db: &impl PersistentHirDatabase) -> Option<Crate> {
139 Some(self.krate) 134 Some(self.krate)
@@ -202,7 +197,7 @@ impl Module {
202 module_impl_blocks 197 module_impl_blocks
203 .impls 198 .impls
204 .iter() 199 .iter()
205 .map(|(impl_id, _)| ImplBlock::from_id(module_impl_blocks.clone(), impl_id)) 200 .map(|(impl_id, _)| ImplBlock::from_id(self, impl_id))
206 .collect() 201 .collect()
207 } 202 }
208} 203}
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index 1425fa693..2d3058afd 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -1,13 +1,12 @@
1use ra_db::FileId;
2use ra_syntax::{ast, SyntaxNode, TreeArc}; 1use ra_syntax::{ast, SyntaxNode, TreeArc};
3 2
4use crate::{ 3use crate::{
5 Module, ModuleSource, Problem, 4 Module, ModuleSource, Problem,
6 Name, 5 Name,
7 module_tree::ModuleId, 6 module_tree::ModuleId,
8 impl_block::ImplId, 7 nameres::lower::ImportId,
9 nameres::{lower::ImportId},
10 HirDatabase, PersistentHirDatabase, 8 HirDatabase, PersistentHirDatabase,
9 HirFileId
11}; 10};
12 11
13impl Module { 12impl Module {
@@ -24,22 +23,21 @@ impl Module {
24 pub(crate) fn definition_source_impl( 23 pub(crate) fn definition_source_impl(
25 &self, 24 &self,
26 db: &impl PersistentHirDatabase, 25 db: &impl PersistentHirDatabase,
27 ) -> (FileId, ModuleSource) { 26 ) -> (HirFileId, ModuleSource) {
28 let module_tree = db.module_tree(self.krate); 27 let module_tree = db.module_tree(self.krate);
29 let file_id = self.module_id.file_id(&module_tree); 28 let file_id = self.module_id.file_id(&module_tree);
30 let decl_id = self.module_id.decl_id(&module_tree); 29 let decl_id = self.module_id.decl_id(&module_tree);
31 let module_source = ModuleSource::new(db, file_id, decl_id); 30 let module_source = ModuleSource::new(db, file_id, decl_id);
32 let file_id = file_id.as_original_file();
33 (file_id, module_source) 31 (file_id, module_source)
34 } 32 }
35 33
36 pub(crate) fn declaration_source_impl( 34 pub(crate) fn declaration_source_impl(
37 &self, 35 &self,
38 db: &impl HirDatabase, 36 db: &impl HirDatabase,
39 ) -> Option<(FileId, TreeArc<ast::Module>)> { 37 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
40 let module_tree = db.module_tree(self.krate); 38 let module_tree = db.module_tree(self.krate);
41 let link = self.module_id.parent_link(&module_tree)?; 39 let link = self.module_id.parent_link(&module_tree)?;
42 let file_id = link.owner(&module_tree).file_id(&module_tree).as_original_file(); 40 let file_id = link.owner(&module_tree).file_id(&module_tree);
43 let src = link.source(&module_tree, db); 41 let src = link.source(&module_tree, db);
44 Some((file_id, src)) 42 Some((file_id, src))
45 } 43 }
@@ -54,16 +52,6 @@ impl Module {
54 source_map.get(&source, import) 52 source_map.get(&source, import)
55 } 53 }
56 54
57 pub(crate) fn impl_source_impl(
58 &self,
59 db: &impl HirDatabase,
60 impl_id: ImplId,
61 ) -> TreeArc<ast::ImplBlock> {
62 let source_map = db.impls_in_module_source_map(*self);
63 let (_, source) = self.definition_source(db);
64 source_map.get(&source, impl_id)
65 }
66
67 pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module { 55 pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module {
68 let module_tree = db.module_tree(self.krate); 56 let module_tree = db.module_tree(self.krate);
69 let module_id = self.module_id.crate_root(&module_tree); 57 let module_id = self.module_id.crate_root(&module_tree);
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 8d81d5ebf..3e11dd6ad 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -74,7 +74,10 @@ impl HirFileId {
74 } 74 }
75 } 75 }
76 76
77 pub(crate) fn as_original_file(self) -> FileId { 77 /// XXX: this is a temporary function, which should go away when we implement the
78 /// nameresolution+macro expansion combo. Prefer using `original_file` if
79 /// possible.
80 pub fn as_original_file(self) -> FileId {
78 match self.0 { 81 match self.0 {
79 HirFileIdRepr::File(file_id) => file_id, 82 HirFileIdRepr::File(file_id) => file_id,
80 HirFileIdRepr::Macro(_r) => panic!("macro generated file: {:?}", self), 83 HirFileIdRepr::Macro(_r) => panic!("macro generated file: {:?}", self),
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index b2fbee8d7..4d8bdf33a 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -38,9 +38,9 @@ impl ImplSourceMap {
38 } 38 }
39} 39}
40 40
41#[derive(Debug, Clone, PartialEq, Eq)] 41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct ImplBlock { 42pub struct ImplBlock {
43 module_impl_blocks: Arc<ModuleImplBlocks>, 43 module: Module,
44 impl_id: ImplId, 44 impl_id: ImplId,
45} 45}
46 46
@@ -50,42 +50,45 @@ impl ImplBlock {
50 item: ImplItem, 50 item: ImplItem,
51 ) -> Option<ImplBlock> { 51 ) -> Option<ImplBlock> {
52 let impl_id = *module_impl_blocks.impls_by_def.get(&item)?; 52 let impl_id = *module_impl_blocks.impls_by_def.get(&item)?;
53 Some(ImplBlock { module_impl_blocks, impl_id }) 53 Some(ImplBlock { module: module_impl_blocks.module, impl_id })
54 } 54 }
55 55
56 pub(crate) fn from_id(module_impl_blocks: Arc<ModuleImplBlocks>, impl_id: ImplId) -> ImplBlock { 56 pub(crate) fn from_id(module: Module, impl_id: ImplId) -> ImplBlock {
57 ImplBlock { module_impl_blocks, impl_id } 57 ImplBlock { module, impl_id }
58 } 58 }
59 59
60 pub fn id(&self) -> ImplId { 60 /// Returns the syntax of the impl block
61 self.impl_id 61 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ImplBlock>) {
62 let source_map = db.impls_in_module_source_map(self.module);
63 let (file_id, source) = self.module.definition_source(db);
64 (file_id, source_map.get(&source, self.impl_id))
62 } 65 }
63 66
64 fn impl_data(&self) -> &ImplData { 67 pub fn id(&self) -> ImplId {
65 &self.module_impl_blocks.impls[self.impl_id] 68 self.impl_id
66 } 69 }
67 70
68 pub fn module(&self) -> Module { 71 pub fn module(&self) -> Module {
69 self.module_impl_blocks.module 72 self.module
70 } 73 }
71 74
72 pub fn target_trait_ref(&self) -> Option<&TypeRef> { 75 pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TypeRef> {
73 self.impl_data().target_trait() 76 db.impls_in_module(self.module).impls[self.impl_id].target_trait().cloned()
74 } 77 }
75 78
76 pub fn target_type(&self) -> &TypeRef { 79 pub fn target_type(&self, db: &impl HirDatabase) -> TypeRef {
77 self.impl_data().target_type() 80 db.impls_in_module(self.module).impls[self.impl_id].target_type().clone()
78 } 81 }
79 82
80 pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { 83 pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
81 Ty::from_hir(db, &self.resolver(db), self.target_type()) 84 Ty::from_hir(db, &self.resolver(db), &self.target_type(db))
82 } 85 }
83 86
84 pub fn target_trait(&self, db: &impl HirDatabase) -> Option<Trait> { 87 pub fn target_trait(&self, db: &impl HirDatabase) -> Option<Trait> {
85 if let Some(TypeRef::Path(path)) = self.target_trait_ref() { 88 if let Some(TypeRef::Path(path)) = self.target_trait_ref(db) {
86 let resolver = self.resolver(db); 89 let resolver = self.resolver(db);
87 if let Some(Resolution::Def(ModuleDef::Trait(tr))) = 90 if let Some(Resolution::Def(ModuleDef::Trait(tr))) =
88 resolver.resolve_path(db, path).take_types() 91 resolver.resolve_path(db, &path).take_types()
89 { 92 {
90 return Some(tr); 93 return Some(tr);
91 } 94 }
@@ -93,8 +96,8 @@ impl ImplBlock {
93 None 96 None
94 } 97 }
95 98
96 pub fn items(&self) -> &[ImplItem] { 99 pub fn items(&self, db: &impl HirDatabase) -> Vec<ImplItem> {
97 self.impl_data().items() 100 db.impls_in_module(self.module).impls[self.impl_id].items().to_vec()
98 } 101 }
99 102
100 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 103 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
@@ -181,7 +184,7 @@ impl_arena_id!(ImplId);
181/// we don't need to do the second step again. 184/// we don't need to do the second step again.
182#[derive(Debug, PartialEq, Eq)] 185#[derive(Debug, PartialEq, Eq)]
183pub struct ModuleImplBlocks { 186pub struct ModuleImplBlocks {
184 module: Module, 187 pub(crate) module: Module,
185 pub(crate) impls: Arena<ImplId, ImplData>, 188 pub(crate) impls: Arena<ImplId, ImplData>,
186 impls_by_def: FxHashMap<ImplItem, ImplId>, 189 impls_by_def: FxHashMap<ImplItem, ImplId>,
187} 190}
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index e81bd3e06..8d1076774 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -40,32 +40,25 @@ pub struct CrateImplBlocks {
40} 40}
41 41
42impl CrateImplBlocks { 42impl CrateImplBlocks {
43 pub fn lookup_impl_blocks<'a>( 43 pub fn lookup_impl_blocks<'a>(&'a self, ty: &Ty) -> impl Iterator<Item = ImplBlock> + 'a {
44 &'a self,
45 db: &'a impl HirDatabase,
46 ty: &Ty,
47 ) -> impl Iterator<Item = (Module, ImplBlock)> + 'a {
48 let fingerprint = TyFingerprint::for_impl(ty); 44 let fingerprint = TyFingerprint::for_impl(ty);
49 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flat_map(|i| i.iter()).map( 45 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flat_map(|i| i.iter()).map(
50 move |(module_id, impl_id)| { 46 move |(module_id, impl_id)| {
51 let module = Module { krate: self.krate, module_id: *module_id }; 47 let module = Module { krate: self.krate, module_id: *module_id };
52 let module_impl_blocks = db.impls_in_module(module); 48 ImplBlock::from_id(module, *impl_id)
53 (module, ImplBlock::from_id(module_impl_blocks, *impl_id))
54 }, 49 },
55 ) 50 )
56 } 51 }
57 52
58 pub fn lookup_impl_blocks_for_trait<'a>( 53 pub fn lookup_impl_blocks_for_trait<'a>(
59 &'a self, 54 &'a self,
60 db: &'a impl HirDatabase,
61 tr: &Trait, 55 tr: &Trait,
62 ) -> impl Iterator<Item = (Module, ImplBlock)> + 'a { 56 ) -> impl Iterator<Item = ImplBlock> + 'a {
63 let id = tr.id; 57 let id = tr.id;
64 self.impls_by_trait.get(&id).into_iter().flat_map(|i| i.iter()).map( 58 self.impls_by_trait.get(&id).into_iter().flat_map(|i| i.iter()).map(
65 move |(module_id, impl_id)| { 59 move |(module_id, impl_id)| {
66 let module = Module { krate: self.krate, module_id: *module_id }; 60 let module = Module { krate: self.krate, module_id: *module_id };
67 let module_impl_blocks = db.impls_in_module(module); 61 ImplBlock::from_id(module, *impl_id)
68 (module, ImplBlock::from_id(module_impl_blocks, *impl_id))
69 }, 62 },
70 ) 63 )
71 } 64 }
@@ -74,7 +67,7 @@ impl CrateImplBlocks {
74 let module_impl_blocks = db.impls_in_module(module.clone()); 67 let module_impl_blocks = db.impls_in_module(module.clone());
75 68
76 for (impl_id, _) in module_impl_blocks.impls.iter() { 69 for (impl_id, _) in module_impl_blocks.impls.iter() {
77 let impl_block = ImplBlock::from_id(Arc::clone(&module_impl_blocks), impl_id); 70 let impl_block = ImplBlock::from_id(module_impl_blocks.module, impl_id);
78 71
79 let target_ty = impl_block.target_ty(db); 72 let target_ty = impl_block.target_ty(db);
80 73
@@ -159,11 +152,11 @@ impl Ty {
159 }; 152 };
160 let impls = db.impls_in_crate(krate); 153 let impls = db.impls_in_crate(krate);
161 154
162 for (_, impl_block) in impls.lookup_impl_blocks(db, &derefed_ty) { 155 for impl_block in impls.lookup_impl_blocks(&derefed_ty) {
163 for item in impl_block.items() { 156 for item in impl_block.items(db) {
164 match item { 157 match item {
165 ImplItem::Method(f) => { 158 ImplItem::Method(f) => {
166 if let Some(result) = callback(*f) { 159 if let Some(result) = callback(f) {
167 return Some(result); 160 return Some(result);
168 } 161 }
169 } 162 }
@@ -185,9 +178,9 @@ impl Ty {
185 let krate = def_crate(db, &self)?; 178 let krate = def_crate(db, &self)?;
186 let impls = db.impls_in_crate(krate); 179 let impls = db.impls_in_crate(krate);
187 180
188 for (_, impl_block) in impls.lookup_impl_blocks(db, &self) { 181 for impl_block in impls.lookup_impl_blocks(&self) {
189 for item in impl_block.items() { 182 for item in impl_block.items(db) {
190 if let Some(result) = callback(*item) { 183 if let Some(result) = callback(item) {
191 return Some(result); 184 return Some(result);
192 } 185 }
193 } 186 }