aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-01-25 14:21:33 +0000
committerJonas Schievink <[email protected]>2021-01-25 14:21:33 +0000
commit5c241b07666bc7b29e97b8206e505944775266a0 (patch)
tree09c289ee88d13b794356e2316e9305d9746069fe /crates/hir
parent82af033f2fa19d370797b7c6f3a99acd3e47c89e (diff)
Create all `ModuleId`s through a `DefMap` method
`ModuleId` needs to be able to represent blocks, and only the associated `DefMap` will know how to construct that `ModuleId`
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/code_model.rs27
-rw-r--r--crates/hir/src/semantics/source_to_def.rs10
2 files changed, 14 insertions, 23 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index d9b4cdfce..e9bb4f541 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -18,8 +18,8 @@ use hir_def::{
18 type_ref::{Mutability, TypeRef}, 18 type_ref::{Mutability, TypeRef},
19 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, 19 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId,
20 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, 20 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId,
21 LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, 21 LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
22 TypeAliasId, TypeParamId, UnionId, 22 TypeParamId, UnionId,
23}; 23};
24use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; 24use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility};
25use hir_expand::{ 25use hir_expand::{
@@ -90,8 +90,8 @@ impl Crate {
90 } 90 }
91 91
92 pub fn root_module(self, db: &dyn HirDatabase) -> Module { 92 pub fn root_module(self, db: &dyn HirDatabase) -> Module {
93 let module_id = db.crate_def_map(self.id).root(); 93 let def_map = db.crate_def_map(self.id);
94 Module::new(self, module_id) 94 Module { id: def_map.module_id(def_map.root()) }
95 } 95 }
96 96
97 pub fn root_file(self, db: &dyn HirDatabase) -> FileId { 97 pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
@@ -275,10 +275,6 @@ impl ModuleDef {
275} 275}
276 276
277impl Module { 277impl Module {
278 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
279 Module { id: ModuleId::top_level(krate.id, crate_module_id) }
280 }
281
282 /// Name of this module. 278 /// Name of this module.
283 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 279 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
284 let def_map = self.id.def_map(db.upcast()); 280 let def_map = self.id.def_map(db.upcast());
@@ -302,7 +298,7 @@ impl Module {
302 /// in the module tree of any target in `Cargo.toml`. 298 /// in the module tree of any target in `Cargo.toml`.
303 pub fn crate_root(self, db: &dyn HirDatabase) -> Module { 299 pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
304 let def_map = db.crate_def_map(self.id.krate()); 300 let def_map = db.crate_def_map(self.id.krate());
305 self.with_module_id(def_map.root()) 301 Module { id: def_map.module_id(def_map.root()) }
306 } 302 }
307 303
308 /// Iterates over all child modules. 304 /// Iterates over all child modules.
@@ -311,7 +307,7 @@ impl Module {
311 let children = def_map[self.id.local_id] 307 let children = def_map[self.id.local_id]
312 .children 308 .children
313 .iter() 309 .iter()
314 .map(|(_, module_id)| self.with_module_id(*module_id)) 310 .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) })
315 .collect::<Vec<_>>(); 311 .collect::<Vec<_>>();
316 children.into_iter() 312 children.into_iter()
317 } 313 }
@@ -321,7 +317,7 @@ impl Module {
321 // FIXME: handle block expressions as modules (their parent is in a different DefMap) 317 // FIXME: handle block expressions as modules (their parent is in a different DefMap)
322 let def_map = self.id.def_map(db.upcast()); 318 let def_map = self.id.def_map(db.upcast());
323 let parent_id = def_map[self.id.local_id].parent?; 319 let parent_id = def_map[self.id.local_id].parent?;
324 Some(self.with_module_id(parent_id)) 320 Some(Module { id: def_map.module_id(parent_id) })
325 } 321 }
326 322
327 pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> { 323 pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
@@ -406,10 +402,6 @@ impl Module {
406 def_map[self.id.local_id].scope.impls().map(Impl::from).collect() 402 def_map[self.id.local_id].scope.impls().map(Impl::from).collect()
407 } 403 }
408 404
409 pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module {
410 Module::new(self.krate(), module_id)
411 }
412
413 /// Finds a path that can be used to refer to the given item from within 405 /// Finds a path that can be used to refer to the given item from within
414 /// this module, if possible. 406 /// this module, if possible.
415 pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { 407 pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
@@ -1013,8 +1005,9 @@ impl MacroDef {
1013 /// early, in `hir_expand`, where modules simply do not exist yet. 1005 /// early, in `hir_expand`, where modules simply do not exist yet.
1014 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { 1006 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
1015 let krate = self.id.krate; 1007 let krate = self.id.krate;
1016 let module_id = db.crate_def_map(krate).root(); 1008 let def_map = db.crate_def_map(krate);
1017 Some(Module::new(Crate { id: krate }, module_id)) 1009 let module_id = def_map.root();
1010 Some(Module { id: def_map.module_id(module_id) })
1018 } 1011 }
1019 1012
1020 /// XXX: this parses the file 1013 /// XXX: this parses the file
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index faede3269..6c612ee86 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -30,13 +30,12 @@ pub(super) struct SourceToDefCtx<'a, 'b> {
30impl SourceToDefCtx<'_, '_> { 30impl 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 self.db.relevant_crates(file).iter().find_map(|&crate_id| {
34 // FIXME: inner items 34 // FIXME: inner items
35 let crate_def_map = self.db.crate_def_map(crate_id); 35 let crate_def_map = self.db.crate_def_map(crate_id);
36 let local_id = crate_def_map.modules_for_file(file).next()?; 36 let local_id = crate_def_map.modules_for_file(file).next()?;
37 Some((crate_id, local_id)) 37 Some(crate_def_map.module_id(local_id))
38 })?; 38 })
39 Some(ModuleId::top_level(krate, local_id))
40 } 39 }
41 40
42 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { 41 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
@@ -63,8 +62,7 @@ impl SourceToDefCtx<'_, '_> {
63 let child_name = src.value.name()?.as_name(); 62 let child_name = src.value.name()?.as_name();
64 let def_map = parent_module.def_map(self.db.upcast()); 63 let def_map = parent_module.def_map(self.db.upcast());
65 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; 64 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
66 // FIXME: handle block expression modules 65 Some(def_map.module_id(child_id))
67 Some(ModuleId::top_level(parent_module.krate(), child_id))
68 } 66 }
69 67
70 pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> { 68 pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {