Obtain ModuleId's DefMap through a method

This commit is contained in:
Jonas Schievink 2021-01-22 16:31:40 +01:00
parent a5322e3d5b
commit ce29730bc7
14 changed files with 43 additions and 32 deletions

View file

@ -281,7 +281,7 @@ impl Module {
/// Name of this module.
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
let parent = def_map[self.id.local_id].parent?;
def_map[parent].children.iter().find_map(|(name, module_id)| {
if *module_id == self.id.local_id {
@ -307,7 +307,7 @@ impl Module {
/// Iterates over all child modules.
pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
let children = def_map[self.id.local_id]
.children
.iter()
@ -318,7 +318,7 @@ impl Module {
/// Finds a parent module.
pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
let parent_id = def_map[self.id.local_id].parent?;
Some(self.with_module_id(parent_id))
}
@ -339,7 +339,7 @@ impl Module {
db: &dyn HirDatabase,
visible_from: Option<Module>,
) -> Vec<(Name, ScopeDef)> {
db.crate_def_map(self.id.krate)[self.id.local_id]
self.id.def_map(db.upcast())[self.id.local_id]
.scope
.entries()
.filter_map(|(name, def)| {
@ -362,14 +362,14 @@ impl Module {
}
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into())
self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into())
}
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
let _p = profile::span("Module::diagnostics").detail(|| {
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
});
let crate_def_map = db.crate_def_map(self.id.krate);
let crate_def_map = self.id.def_map(db.upcast());
crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink);
for decl in self.declarations(db) {
match decl {
@ -396,12 +396,12 @@ impl Module {
}
pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect()
}
pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
def_map[self.id.local_id].scope.impls().map(Impl::from).collect()
}

View file

@ -24,12 +24,12 @@ pub trait HasSource {
impl Module {
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
def_map[self.id.local_id].definition_source(db.upcast())
}
pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
match def_map[self.id.local_id].origin {
ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs,
_ => false,
@ -39,7 +39,7 @@ impl Module {
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
/// `None` for the crate root.
pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
let def_map = db.crate_def_map(self.id.krate);
let def_map = self.id.def_map(db.upcast());
def_map[self.id.local_id].declaration_source(db.upcast())
}
}

View file

@ -31,6 +31,7 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> {
let _p = profile::span("SourceBinder::to_module_def");
let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| {
// FIXME: inner items
let crate_def_map = self.db.crate_def_map(crate_id);
let local_id = crate_def_map.modules_for_file(file).next()?;
Some((crate_id, local_id))
@ -60,7 +61,7 @@ impl SourceToDefCtx<'_, '_> {
}?;
let child_name = src.value.name()?.as_name();
let def_map = self.db.crate_def_map(parent_module.krate);
let def_map = parent_module.def_map(self.db.upcast());
let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
Some(ModuleId { krate: parent_module.krate, local_id: child_id })
}