diff options
author | Jonas Schievink <[email protected]> | 2021-01-22 15:31:40 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-01-22 15:33:58 +0000 |
commit | ce29730bc773a27eaeaae7fa4122563df3b253b6 (patch) | |
tree | 57818d6bc1e176a421f63c01a6561fb5e6a291b2 /crates/hir/src | |
parent | a5322e3d5b813e4bce7a73762c14bebbd9a36e01 (diff) |
Obtain `ModuleId`'s `DefMap` through a method
Diffstat (limited to 'crates/hir/src')
-rw-r--r-- | crates/hir/src/code_model.rs | 16 | ||||
-rw-r--r-- | crates/hir/src/has_source.rs | 6 | ||||
-rw-r--r-- | crates/hir/src/semantics/source_to_def.rs | 3 |
3 files changed, 13 insertions, 12 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index aaa7013b6..c34a99d90 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -281,7 +281,7 @@ impl Module { | |||
281 | 281 | ||
282 | /// Name of this module. | 282 | /// Name of this module. |
283 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | 283 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { |
284 | let def_map = db.crate_def_map(self.id.krate); | 284 | let def_map = self.id.def_map(db.upcast()); |
285 | let parent = def_map[self.id.local_id].parent?; | 285 | let parent = def_map[self.id.local_id].parent?; |
286 | def_map[parent].children.iter().find_map(|(name, module_id)| { | 286 | def_map[parent].children.iter().find_map(|(name, module_id)| { |
287 | if *module_id == self.id.local_id { | 287 | if *module_id == self.id.local_id { |
@@ -307,7 +307,7 @@ impl Module { | |||
307 | 307 | ||
308 | /// Iterates over all child modules. | 308 | /// Iterates over all child modules. |
309 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { | 309 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { |
310 | let def_map = db.crate_def_map(self.id.krate); | 310 | let def_map = self.id.def_map(db.upcast()); |
311 | let children = def_map[self.id.local_id] | 311 | let children = def_map[self.id.local_id] |
312 | .children | 312 | .children |
313 | .iter() | 313 | .iter() |
@@ -318,7 +318,7 @@ impl Module { | |||
318 | 318 | ||
319 | /// Finds a parent module. | 319 | /// Finds a parent module. |
320 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { | 320 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { |
321 | let def_map = db.crate_def_map(self.id.krate); | 321 | let def_map = self.id.def_map(db.upcast()); |
322 | let parent_id = def_map[self.id.local_id].parent?; | 322 | let parent_id = def_map[self.id.local_id].parent?; |
323 | Some(self.with_module_id(parent_id)) | 323 | Some(self.with_module_id(parent_id)) |
324 | } | 324 | } |
@@ -339,7 +339,7 @@ impl Module { | |||
339 | db: &dyn HirDatabase, | 339 | db: &dyn HirDatabase, |
340 | visible_from: Option<Module>, | 340 | visible_from: Option<Module>, |
341 | ) -> Vec<(Name, ScopeDef)> { | 341 | ) -> Vec<(Name, ScopeDef)> { |
342 | db.crate_def_map(self.id.krate)[self.id.local_id] | 342 | self.id.def_map(db.upcast())[self.id.local_id] |
343 | .scope | 343 | .scope |
344 | .entries() | 344 | .entries() |
345 | .filter_map(|(name, def)| { | 345 | .filter_map(|(name, def)| { |
@@ -362,14 +362,14 @@ impl Module { | |||
362 | } | 362 | } |
363 | 363 | ||
364 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | 364 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { |
365 | db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into()) | 365 | self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) |
366 | } | 366 | } |
367 | 367 | ||
368 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 368 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
369 | let _p = profile::span("Module::diagnostics").detail(|| { | 369 | let _p = profile::span("Module::diagnostics").detail(|| { |
370 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) | 370 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) |
371 | }); | 371 | }); |
372 | let crate_def_map = db.crate_def_map(self.id.krate); | 372 | let crate_def_map = self.id.def_map(db.upcast()); |
373 | crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); | 373 | crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); |
374 | for decl in self.declarations(db) { | 374 | for decl in self.declarations(db) { |
375 | match decl { | 375 | match decl { |
@@ -396,12 +396,12 @@ impl Module { | |||
396 | } | 396 | } |
397 | 397 | ||
398 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { | 398 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { |
399 | let def_map = db.crate_def_map(self.id.krate); | 399 | let def_map = self.id.def_map(db.upcast()); |
400 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() | 400 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() |
401 | } | 401 | } |
402 | 402 | ||
403 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { | 403 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { |
404 | let def_map = db.crate_def_map(self.id.krate); | 404 | let def_map = self.id.def_map(db.upcast()); |
405 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() | 405 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() |
406 | } | 406 | } |
407 | 407 | ||
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 7c57d8378..262002671 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs | |||
@@ -24,12 +24,12 @@ pub trait HasSource { | |||
24 | impl Module { | 24 | impl Module { |
25 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 25 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
26 | pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> { | 26 | pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> { |
27 | let def_map = db.crate_def_map(self.id.krate); | 27 | let def_map = self.id.def_map(db.upcast()); |
28 | def_map[self.id.local_id].definition_source(db.upcast()) | 28 | def_map[self.id.local_id].definition_source(db.upcast()) |
29 | } | 29 | } |
30 | 30 | ||
31 | pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { | 31 | pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { |
32 | let def_map = db.crate_def_map(self.id.krate); | 32 | let def_map = self.id.def_map(db.upcast()); |
33 | match def_map[self.id.local_id].origin { | 33 | match def_map[self.id.local_id].origin { |
34 | ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs, | 34 | ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs, |
35 | _ => false, | 35 | _ => false, |
@@ -39,7 +39,7 @@ impl Module { | |||
39 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 39 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
40 | /// `None` for the crate root. | 40 | /// `None` for the crate root. |
41 | pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> { | 41 | pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> { |
42 | let def_map = db.crate_def_map(self.id.krate); | 42 | let def_map = self.id.def_map(db.upcast()); |
43 | def_map[self.id.local_id].declaration_source(db.upcast()) | 43 | def_map[self.id.local_id].declaration_source(db.upcast()) |
44 | } | 44 | } |
45 | } | 45 | } |
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 9bf60c72a..775f7ec8b 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs | |||
@@ -31,6 +31,7 @@ impl SourceToDefCtx<'_, '_> { | |||
31 | pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> { | 31 | pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> { |
32 | let _p = profile::span("SourceBinder::to_module_def"); | 32 | let _p = profile::span("SourceBinder::to_module_def"); |
33 | let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { | 33 | let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { |
34 | // FIXME: inner items | ||
34 | let crate_def_map = self.db.crate_def_map(crate_id); | 35 | let crate_def_map = self.db.crate_def_map(crate_id); |
35 | let local_id = crate_def_map.modules_for_file(file).next()?; | 36 | let local_id = crate_def_map.modules_for_file(file).next()?; |
36 | Some((crate_id, local_id)) | 37 | Some((crate_id, local_id)) |
@@ -60,7 +61,7 @@ impl SourceToDefCtx<'_, '_> { | |||
60 | }?; | 61 | }?; |
61 | 62 | ||
62 | let child_name = src.value.name()?.as_name(); | 63 | let child_name = src.value.name()?.as_name(); |
63 | let def_map = self.db.crate_def_map(parent_module.krate); | 64 | let def_map = parent_module.def_map(self.db.upcast()); |
64 | let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; | 65 | let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; |
65 | Some(ModuleId { krate: parent_module.krate, local_id: child_id }) | 66 | Some(ModuleId { krate: parent_module.krate, local_id: child_id }) |
66 | } | 67 | } |