Refactor PerNs construction

This commit is contained in:
Aleksey Kladov 2019-12-22 15:08:57 +01:00
parent fe38fffaa9
commit e69af85962
2 changed files with 52 additions and 49 deletions

View file

@ -5,7 +5,7 @@ use hir_expand::name::Name;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ItemScope {
@ -153,3 +153,21 @@ pub struct Resolution {
pub def: PerNs,
pub(crate) import: bool,
}
impl From<ModuleDefId> for PerNs {
fn from(def: ModuleDefId) -> PerNs {
match def {
ModuleDefId::ModuleId(_) => PerNs::types(def),
ModuleDefId::FunctionId(_) => PerNs::values(def),
ModuleDefId::AdtId(adt) => match adt {
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
AdtId::EnumId(_) => PerNs::types(def),
},
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
ModuleDefId::TraitId(_) => PerNs::types(def),
ModuleDefId::TypeAliasId(_) => PerNs::types(def),
ModuleDefId::BuiltinType(_) => PerNs::types(def),
}
}
}

View file

@ -714,12 +714,9 @@ where
modules[res].scope.define_legacy_macro(name, mac)
}
modules[self.module_id].children.insert(name.clone(), res);
let resolution = Resolution {
def: PerNs::types(
ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
),
import: false,
};
let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
let def: ModuleDefId = module.into();
let resolution = Resolution { def: def.into(), import: false };
self.def_collector.update(self.module_id, None, &[(name, resolution)]);
res
}
@ -734,63 +731,51 @@ where
let name = def.name.clone();
let container = ContainerId::ModuleId(module);
let def: PerNs = match def.kind {
raw::DefKind::Function(ast_id) => {
let def = FunctionLoc {
container: container.into(),
ast_id: AstId::new(self.file_id, ast_id),
}
.intern(self.def_collector.db);
PerNs::values(def.into())
let def: ModuleDefId = match def.kind {
raw::DefKind::Function(ast_id) => FunctionLoc {
container: container.into(),
ast_id: AstId::new(self.file_id, ast_id),
}
.intern(self.def_collector.db)
.into(),
raw::DefKind::Struct(ast_id) => {
let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::both(def.into(), def.into())
StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::Union(ast_id) => {
let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::both(def.into(), def.into())
UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::Enum(ast_id) => {
let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::types(def.into())
EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::Const(ast_id) => {
let def = ConstLoc {
container: container.into(),
ast_id: AstId::new(self.file_id, ast_id),
}
.intern(self.def_collector.db);
PerNs::values(def.into())
ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::Static(ast_id) => {
let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::values(def.into())
StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::Trait(ast_id) => {
let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::types(def.into())
TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db)
.into()
}
raw::DefKind::TypeAlias(ast_id) => {
let def = TypeAliasLoc {
container: container.into(),
ast_id: AstId::new(self.file_id, ast_id),
}
.intern(self.def_collector.db);
PerNs::types(def.into())
raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc {
container: container.into(),
ast_id: AstId::new(self.file_id, ast_id),
}
.intern(self.def_collector.db)
.into(),
};
let resolution = Resolution { def, import: false };
let resolution = Resolution { def: def.into(), import: false };
self.def_collector.update(self.module_id, None, &[(name, resolution)])
}