diff options
author | Jonas Schievink <[email protected]> | 2021-01-25 14:21:33 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-01-25 14:21:33 +0000 |
commit | 5c241b07666bc7b29e97b8206e505944775266a0 (patch) | |
tree | 09c289ee88d13b794356e2316e9305d9746069fe /crates/hir/src/code_model.rs | |
parent | 82af033f2fa19d370797b7c6f3a99acd3e47c89e (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/src/code_model.rs')
-rw-r--r-- | crates/hir/src/code_model.rs | 27 |
1 files changed, 10 insertions, 17 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 | }; |
24 | use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; | 24 | use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; |
25 | use hir_expand::{ | 25 | use 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 | ||
277 | impl Module { | 277 | impl 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 |