Store impls in CrateDefMap

This commit is contained in:
Aleksey Kladov 2019-11-15 19:32:56 +03:00
parent 080dd31f84
commit ea3540c1a8
3 changed files with 39 additions and 4 deletions

View file

@ -73,7 +73,7 @@ use crate::{
diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs, raw::ImportId,
},
path::Path,
AstId, CrateModuleId, FunctionId, ModuleDefId, ModuleId, TraitId,
AstId, CrateModuleId, FunctionId, ImplId, ModuleDefId, ModuleId, TraitId,
};
/// Contains all top-level defs from a macro-expanded crate
@ -122,6 +122,7 @@ pub struct ModuleData {
///
/// Note that non-inline modules, by definition, live inside non-macro file.
pub definition: Option<FileId>,
pub impls: Vec<ImplId>,
}
#[derive(Default, Debug, PartialEq, Eq, Clone)]

View file

@ -19,7 +19,7 @@ use crate::{
per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode,
},
path::{Path, PathKind},
AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId,
AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId, ImplId,
LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasId,
UnionId,
};
@ -571,6 +571,15 @@ where
.push((self.module_id, import_id, self.raw_items[import_id].clone())),
raw::RawItemKind::Def(def) => self.define_def(&self.raw_items[def]),
raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]),
raw::RawItemKind::Impl(imp) => {
let module = ModuleId {
krate: self.def_collector.def_map.krate,
module_id: self.module_id,
};
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id);
self.def_collector.def_map.modules[self.module_id].impls.push(imp_id)
}
}
}
}

View file

@ -28,6 +28,7 @@ pub struct RawItems {
imports: Arena<ImportId, ImportData>,
defs: Arena<Def, DefData>,
macros: Arena<Macro, MacroData>,
impls: Arena<Impl, ImplData>,
/// items for top-level module
items: Vec<RawItem>,
}
@ -121,6 +122,13 @@ impl Index<Macro> for RawItems {
}
}
impl Index<Impl> for RawItems {
type Output = ImplData;
fn index(&self, idx: Impl) -> &ImplData {
&self.impls[idx]
}
}
// Avoid heap allocation on items without attributes.
type Attrs = Option<Arc<[Attr]>>;
@ -142,6 +150,7 @@ pub(super) enum RawItemKind {
Import(ImportId),
Def(Def),
Macro(Macro),
Impl(Impl),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -203,6 +212,15 @@ pub(super) struct MacroData {
pub(super) builtin: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) struct Impl(RawId);
impl_arena_id!(Impl);
#[derive(Debug, PartialEq, Eq)]
pub(super) struct ImplData {
pub(super) ast_id: FileAstId<ast::ImplBlock>,
}
struct RawItemsCollector {
raw_items: RawItems,
source_ast_id_map: Arc<AstIdMap>,
@ -236,8 +254,8 @@ impl RawItemsCollector {
self.add_extern_crate_item(current_module, extern_crate);
return;
}
ast::ModuleItem::ImplBlock(_) => {
// impls don't participate in name resolution
ast::ModuleItem::ImplBlock(it) => {
self.add_impl(current_module, it);
return;
}
ast::ModuleItem::StructDef(it) => {
@ -376,6 +394,13 @@ impl RawItemsCollector {
self.push_item(current_module, attrs, RawItemKind::Macro(m));
}
fn add_impl(&mut self, current_module: Option<Module>, imp: ast::ImplBlock) {
let attrs = self.parse_attrs(&imp);
let ast_id = self.source_ast_id_map.ast_id(&imp);
let imp = self.raw_items.impls.alloc(ImplData { ast_id });
self.push_item(current_module, attrs, RawItemKind::Impl(imp))
}
fn push_import(
&mut self,
current_module: Option<Module>,