diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8eb3c577d..3f1c36941 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -5,6 +5,7 @@ pub(crate) mod docs; | |||
5 | 5 | ||
6 | use std::sync::Arc; | 6 | use std::sync::Arc; |
7 | 7 | ||
8 | use hir_def::{CrateModuleId, ModuleId}; | ||
8 | use ra_db::{CrateId, Edition, FileId}; | 9 | use ra_db::{CrateId, Edition, FileId}; |
9 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 10 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; |
10 | 11 | ||
@@ -23,7 +24,7 @@ use crate::{ | |||
23 | BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64, | 24 | BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64, |
24 | U8, USIZE, | 25 | U8, USIZE, |
25 | }, | 26 | }, |
26 | nameres::{CrateModuleId, ImportId, ModuleScope, Namespace}, | 27 | nameres::{ImportId, ModuleScope, Namespace}, |
27 | resolve::{Resolver, Scope, TypeNs}, | 28 | resolve::{Resolver, Scope, TypeNs}, |
28 | traits::TraitData, | 29 | traits::TraitData, |
29 | ty::{ | 30 | ty::{ |
@@ -67,8 +68,7 @@ impl Crate { | |||
67 | 68 | ||
68 | pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { | 69 | pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { |
69 | let module_id = db.crate_def_map(self).root(); | 70 | let module_id = db.crate_def_map(self).root(); |
70 | let module = Module { krate: self, module_id }; | 71 | Some(Module::new(self, module_id)) |
71 | Some(module) | ||
72 | } | 72 | } |
73 | 73 | ||
74 | pub fn edition(self, db: &impl DefDatabase) -> Edition { | 74 | pub fn edition(self, db: &impl DefDatabase) -> Edition { |
@@ -83,8 +83,7 @@ impl Crate { | |||
83 | 83 | ||
84 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 84 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
85 | pub struct Module { | 85 | pub struct Module { |
86 | pub(crate) krate: Crate, | 86 | pub(crate) id: ModuleId, |
87 | pub(crate) module_id: CrateModuleId, | ||
88 | } | 87 | } |
89 | 88 | ||
90 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 89 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -175,12 +174,16 @@ impl ModuleSource { | |||
175 | } | 174 | } |
176 | 175 | ||
177 | impl Module { | 176 | impl Module { |
177 | pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module { | ||
178 | Module { id: ModuleId { krate: krate.crate_id, module_id: crate_module_id } } | ||
179 | } | ||
180 | |||
178 | /// Name of this module. | 181 | /// Name of this module. |
179 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { | 182 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { |
180 | let def_map = db.crate_def_map(self.krate); | 183 | let def_map = db.crate_def_map(self.krate()); |
181 | let parent = def_map[self.module_id].parent?; | 184 | let parent = def_map[self.id.module_id].parent?; |
182 | def_map[parent].children.iter().find_map(|(name, module_id)| { | 185 | def_map[parent].children.iter().find_map(|(name, module_id)| { |
183 | if *module_id == self.module_id { | 186 | if *module_id == self.id.module_id { |
184 | Some(name.clone()) | 187 | Some(name.clone()) |
185 | } else { | 188 | } else { |
186 | None | 189 | None |
@@ -200,29 +203,29 @@ impl Module { | |||
200 | } | 203 | } |
201 | 204 | ||
202 | /// Returns the crate this module is part of. | 205 | /// Returns the crate this module is part of. |
203 | pub fn krate(self, _db: &impl DefDatabase) -> Option<Crate> { | 206 | pub fn krate(self) -> Crate { |
204 | Some(self.krate) | 207 | Crate { crate_id: self.id.krate } |
205 | } | 208 | } |
206 | 209 | ||
207 | /// Topmost parent of this module. Every module has a `crate_root`, but some | 210 | /// Topmost parent of this module. Every module has a `crate_root`, but some |
208 | /// might be missing `krate`. This can happen if a module's file is not included | 211 | /// might be missing `krate`. This can happen if a module's file is not included |
209 | /// in the module tree of any target in `Cargo.toml`. | 212 | /// in the module tree of any target in `Cargo.toml`. |
210 | pub fn crate_root(self, db: &impl DefDatabase) -> Module { | 213 | pub fn crate_root(self, db: &impl DefDatabase) -> Module { |
211 | let def_map = db.crate_def_map(self.krate); | 214 | let def_map = db.crate_def_map(self.krate()); |
212 | self.with_module_id(def_map.root()) | 215 | self.with_module_id(def_map.root()) |
213 | } | 216 | } |
214 | 217 | ||
215 | /// Finds a child module with the specified name. | 218 | /// Finds a child module with the specified name. |
216 | pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { | 219 | pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { |
217 | let def_map = db.crate_def_map(self.krate); | 220 | let def_map = db.crate_def_map(self.krate()); |
218 | let child_id = def_map[self.module_id].children.get(name)?; | 221 | let child_id = def_map[self.id.module_id].children.get(name)?; |
219 | Some(self.with_module_id(*child_id)) | 222 | Some(self.with_module_id(*child_id)) |
220 | } | 223 | } |
221 | 224 | ||
222 | /// Iterates over all child modules. | 225 | /// Iterates over all child modules. |
223 | pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { | 226 | pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { |
224 | let def_map = db.crate_def_map(self.krate); | 227 | let def_map = db.crate_def_map(self.krate()); |
225 | let children = def_map[self.module_id] | 228 | let children = def_map[self.id.module_id] |
226 | .children | 229 | .children |
227 | .iter() | 230 | .iter() |
228 | .map(|(_, module_id)| self.with_module_id(*module_id)) | 231 | .map(|(_, module_id)| self.with_module_id(*module_id)) |
@@ -232,8 +235,8 @@ impl Module { | |||
232 | 235 | ||
233 | /// Finds a parent module. | 236 | /// Finds a parent module. |
234 | pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { | 237 | pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { |
235 | let def_map = db.crate_def_map(self.krate); | 238 | let def_map = db.crate_def_map(self.krate()); |
236 | let parent_id = def_map[self.module_id].parent?; | 239 | let parent_id = def_map[self.id.module_id].parent?; |
237 | Some(self.with_module_id(parent_id)) | 240 | Some(self.with_module_id(parent_id)) |
238 | } | 241 | } |
239 | 242 | ||
@@ -249,11 +252,11 @@ impl Module { | |||
249 | 252 | ||
250 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 253 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
251 | pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { | 254 | pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { |
252 | db.crate_def_map(self.krate)[self.module_id].scope.clone() | 255 | db.crate_def_map(self.krate())[self.id.module_id].scope.clone() |
253 | } | 256 | } |
254 | 257 | ||
255 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 258 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
256 | db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink); | 259 | db.crate_def_map(self.krate()).add_diagnostics(db, self.id.module_id, sink); |
257 | for decl in self.declarations(db) { | 260 | for decl in self.declarations(db) { |
258 | match decl { | 261 | match decl { |
259 | crate::ModuleDef::Function(f) => f.diagnostics(db, sink), | 262 | crate::ModuleDef::Function(f) => f.diagnostics(db, sink), |
@@ -277,13 +280,13 @@ impl Module { | |||
277 | } | 280 | } |
278 | 281 | ||
279 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { | 282 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
280 | let def_map = db.crate_def_map(self.krate); | 283 | let def_map = db.crate_def_map(self.krate()); |
281 | Resolver::default().push_module_scope(def_map, self.module_id) | 284 | Resolver::default().push_module_scope(def_map, self.id.module_id) |
282 | } | 285 | } |
283 | 286 | ||
284 | pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { | 287 | pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { |
285 | let def_map = db.crate_def_map(self.krate); | 288 | let def_map = db.crate_def_map(self.krate()); |
286 | def_map[self.module_id] | 289 | def_map[self.id.module_id] |
287 | .scope | 290 | .scope |
288 | .entries() | 291 | .entries() |
289 | .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) | 292 | .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) |
@@ -303,7 +306,7 @@ impl Module { | |||
303 | } | 306 | } |
304 | 307 | ||
305 | fn with_module_id(self, module_id: CrateModuleId) -> Module { | 308 | fn with_module_id(self, module_id: CrateModuleId) -> Module { |
306 | Module { module_id, krate: self.krate } | 309 | Module::new(self.krate(), module_id) |
307 | } | 310 | } |
308 | } | 311 | } |
309 | 312 | ||
@@ -344,7 +347,7 @@ impl Struct { | |||
344 | } | 347 | } |
345 | 348 | ||
346 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 349 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
347 | self.module(db).krate(db) | 350 | Some(self.module(db).krate()) |
348 | } | 351 | } |
349 | 352 | ||
350 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { | 353 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { |
@@ -432,7 +435,7 @@ impl Enum { | |||
432 | } | 435 | } |
433 | 436 | ||
434 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 437 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
435 | self.module(db).krate(db) | 438 | Some(self.module(db).krate()) |
436 | } | 439 | } |
437 | 440 | ||
438 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { | 441 | pub fn name(self, db: &impl DefDatabase) -> Option<Name> { |
@@ -523,12 +526,14 @@ impl Adt { | |||
523 | } | 526 | } |
524 | 527 | ||
525 | pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { | 528 | pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { |
526 | match self { | 529 | Some( |
527 | Adt::Struct(s) => s.module(db), | 530 | match self { |
528 | Adt::Union(s) => s.module(db), | 531 | Adt::Struct(s) => s.module(db), |
529 | Adt::Enum(e) => e.module(db), | 532 | Adt::Union(s) => s.module(db), |
530 | } | 533 | Adt::Enum(e) => e.module(db), |
531 | .krate(db) | 534 | } |
535 | .krate(), | ||
536 | ) | ||
532 | } | 537 | } |
533 | 538 | ||
534 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { | 539 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
@@ -696,7 +701,7 @@ impl Function { | |||
696 | } | 701 | } |
697 | 702 | ||
698 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 703 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
699 | self.module(db).krate(db) | 704 | Some(self.module(db).krate()) |
700 | } | 705 | } |
701 | 706 | ||
702 | pub fn name(self, db: &impl HirDatabase) -> Name { | 707 | pub fn name(self, db: &impl HirDatabase) -> Name { |
@@ -774,7 +779,7 @@ impl Const { | |||
774 | } | 779 | } |
775 | 780 | ||
776 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 781 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
777 | self.module(db).krate(db) | 782 | Some(self.module(db).krate()) |
778 | } | 783 | } |
779 | 784 | ||
780 | pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { | 785 | pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { |
@@ -871,7 +876,7 @@ impl Static { | |||
871 | } | 876 | } |
872 | 877 | ||
873 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 878 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
874 | self.module(db).krate(db) | 879 | Some(self.module(db).krate()) |
875 | } | 880 | } |
876 | 881 | ||
877 | pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { | 882 | pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { |
@@ -1002,7 +1007,7 @@ impl TypeAlias { | |||
1002 | } | 1007 | } |
1003 | 1008 | ||
1004 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 1009 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
1005 | self.module(db).krate(db) | 1010 | Some(self.module(db).krate()) |
1006 | } | 1011 | } |
1007 | 1012 | ||
1008 | /// The containing impl block, if this is a method. | 1013 | /// The containing impl block, if this is a method. |