From ea3540c1a8efb3b9c56847f831b08abb3be2d978 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Nov 2019 19:32:56 +0300 Subject: Store impls in CrateDefMap --- crates/ra_hir_def/src/nameres.rs | 3 ++- crates/ra_hir_def/src/nameres/collector.rs | 11 ++++++++++- crates/ra_hir_def/src/nameres/raw.rs | 29 +++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index dfcec6443..49e33ccc4 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -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, + pub impls: Vec, } #[derive(Default, Debug, PartialEq, Eq, Clone)] diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 5c899aff3..c9ccb9023 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -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) + } } } } diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index f52002bc0..a0a2c7273 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -28,6 +28,7 @@ pub struct RawItems { imports: Arena, defs: Arena, macros: Arena, + impls: Arena, /// items for top-level module items: Vec, } @@ -121,6 +122,13 @@ impl Index for RawItems { } } +impl Index for RawItems { + type Output = ImplData; + fn index(&self, idx: Impl) -> &ImplData { + &self.impls[idx] + } +} + // Avoid heap allocation on items without attributes. type Attrs = Option>; @@ -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, +} + struct RawItemsCollector { raw_items: RawItems, source_ast_id_map: Arc, @@ -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, 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, -- cgit v1.2.3