aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-20 15:00:01 +0000
committerAleksey Kladov <[email protected]>2019-11-20 15:41:23 +0000
commit111891dc2dc1d2c7ea87144e8e3ddefe23fc7b6d (patch)
tree7e96d773620a3b03254d00386711cdc7c909e3ee /crates/ra_hir_def
parentee95a35664e6fe9153f6324cfc57872ca365887c (diff)
Move constants to new ID
This allows us to get rid of trait item index
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body.rs1
-rw-r--r--crates/ra_hir_def/src/db.rs9
-rw-r--r--crates/ra_hir_def/src/impls.rs12
-rw-r--r--crates/ra_hir_def/src/lib.rs40
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs12
-rw-r--r--crates/ra_hir_def/src/traits.rs45
6 files changed, 65 insertions, 54 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index b69d4dea6..dfb79a30a 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -155,6 +155,7 @@ impl Body {
155 (src.file_id, f.module(db), src.value.body().map(ast::Expr::from)) 155 (src.file_id, f.module(db), src.value.body().map(ast::Expr::from))
156 } 156 }
157 DefWithBodyId::ConstId(c) => { 157 DefWithBodyId::ConstId(c) => {
158 let c = c.lookup(db);
158 let src = c.source(db); 159 let src = c.source(db);
159 (src.file_id, c.module(db), src.value.body()) 160 (src.file_id, c.module(db), src.value.body())
160 } 161 }
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index d6d32fb8c..c6cd4369b 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -13,8 +13,8 @@ use crate::{
13 raw::{ImportSourceMap, RawItems}, 13 raw::{ImportSourceMap, RawItems},
14 CrateDefMap, 14 CrateDefMap,
15 }, 15 },
16 traits::{TraitData, TraitItemsIndex}, 16 traits::TraitData,
17 DefWithBodyId, EnumId, ImplId, ItemLoc, ModuleId, StructOrUnionId, TraitId, 17 DefWithBodyId, EnumId, ImplId, ItemLoc, StructOrUnionId, TraitId,
18}; 18};
19 19
20#[salsa::query_group(InternDatabaseStorage)] 20#[salsa::query_group(InternDatabaseStorage)]
@@ -26,7 +26,7 @@ pub trait InternDatabase: SourceDatabase {
26 #[salsa::interned] 26 #[salsa::interned]
27 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> crate::EnumId; 27 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> crate::EnumId;
28 #[salsa::interned] 28 #[salsa::interned]
29 fn intern_const(&self, loc: ItemLoc<ast::ConstDef>) -> crate::ConstId; 29 fn intern_const(&self, loc: crate::ConstLoc) -> crate::ConstId;
30 #[salsa::interned] 30 #[salsa::interned]
31 fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> crate::StaticId; 31 fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> crate::StaticId;
32 #[salsa::interned] 32 #[salsa::interned]
@@ -63,9 +63,6 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
63 #[salsa::invoke(TraitData::trait_data_query)] 63 #[salsa::invoke(TraitData::trait_data_query)]
64 fn trait_data(&self, e: TraitId) -> Arc<TraitData>; 64 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
65 65
66 #[salsa::invoke(TraitItemsIndex::trait_items_index)]
67 fn trait_items_index(&self, module: ModuleId) -> TraitItemsIndex;
68
69 #[salsa::invoke(Body::body_with_source_map_query)] 66 #[salsa::invoke(Body::body_with_source_map_query)]
70 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); 67 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
71 68
diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs
index 574086ac7..750a869f2 100644
--- a/crates/ra_hir_def/src/impls.rs
+++ b/crates/ra_hir_def/src/impls.rs
@@ -9,8 +9,8 @@ use hir_expand::AstId;
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, ContainerId, 12 db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId,
13 FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasLoc, 13 FunctionLoc, ImplId, Intern, TypeAliasLoc,
14}; 14};
15 15
16#[derive(Debug, Clone, PartialEq, Eq)] 16#[derive(Debug, Clone, PartialEq, Eq)]
@@ -31,7 +31,6 @@ impl ImplData {
31 let negative = src.value.is_negative(); 31 let negative = src.value.is_negative();
32 32
33 let items = if let Some(item_list) = src.value.item_list() { 33 let items = if let Some(item_list) = src.value.item_list() {
34 let ctx = LocationCtx::new(db, id.module(db), src.file_id);
35 item_list 34 item_list
36 .impl_items() 35 .impl_items()
37 .map(|item_node| match item_node { 36 .map(|item_node| match item_node {
@@ -44,7 +43,12 @@ impl ImplData {
44 def.into() 43 def.into()
45 } 44 }
46 ast::ImplItem::ConstDef(it) => { 45 ast::ImplItem::ConstDef(it) => {
47 ConstId::from_ast_id(ctx, items.ast_id(&it)).into() 46 let def = ConstLoc {
47 container: ContainerId::ImplId(id),
48 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
49 }
50 .intern(db);
51 def.into()
48 } 52 }
49 ast::ImplItem::TypeAliasDef(it) => { 53 ast::ImplItem::TypeAliasDef(it) => {
50 let def = TypeAliasLoc { 54 let def = TypeAliasLoc {
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index da6506fcd..0af41de87 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -289,12 +289,23 @@ impl_arena_id!(LocalStructFieldId);
289#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 289#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
290pub struct ConstId(salsa::InternId); 290pub struct ConstId(salsa::InternId);
291impl_intern_key!(ConstId); 291impl_intern_key!(ConstId);
292impl AstItemDef<ast::ConstDef> for ConstId { 292#[derive(Debug, Clone, PartialEq, Eq, Hash)]
293 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self { 293pub struct ConstLoc {
294 db.intern_const(loc) 294 pub container: ContainerId,
295 pub ast_id: AstId<ast::ConstDef>,
296}
297
298impl Intern for ConstLoc {
299 type ID = ConstId;
300 fn intern(self, db: &impl db::DefDatabase2) -> ConstId {
301 db.intern_const(self)
295 } 302 }
296 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> { 303}
297 db.lookup_intern_const(self) 304
305impl Lookup for ConstId {
306 type Data = ConstLoc;
307 fn lookup(&self, db: &impl db::DefDatabase2) -> ConstLoc {
308 db.lookup_intern_const(*self)
298 } 309 }
299} 310}
300 311
@@ -498,6 +509,16 @@ impl HasModule for TypeAliasLoc {
498 } 509 }
499} 510}
500 511
512impl HasModule for ConstLoc {
513 fn module(&self, db: &impl db::DefDatabase2) -> ModuleId {
514 match self.container {
515 ContainerId::ModuleId(it) => it,
516 ContainerId::ImplId(it) => it.module(db),
517 ContainerId::TraitId(it) => it.module(db),
518 }
519 }
520}
521
501pub trait HasSource { 522pub trait HasSource {
502 type Value; 523 type Value;
503 fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>; 524 fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>;
@@ -520,3 +541,12 @@ impl HasSource for TypeAliasLoc {
520 Source::new(self.ast_id.file_id(), node) 541 Source::new(self.ast_id.file_id(), node)
521 } 542 }
522} 543}
544
545impl HasSource for ConstLoc {
546 type Value = ast::ConstDef;
547
548 fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::ConstDef> {
549 let node = self.ast_id.to_node(db);
550 Source::new(self.ast_id.file_id(), node)
551 }
552}
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 71e01279d..aae3dcadf 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::{
19 per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, 19 per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode,
20 }, 20 },
21 path::{Path, PathKind}, 21 path::{Path, PathKind},
22 AdtId, AstId, AstItemDef, ConstId, ContainerId, CrateModuleId, EnumId, EnumVariantId, 22 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId,
23 FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, 23 FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId,
24 StructOrUnionId, TraitId, TypeAliasLoc, UnionId, 24 StructOrUnionId, TraitId, TypeAliasLoc, UnionId,
25}; 25};
@@ -692,7 +692,15 @@ where
692 PerNs::both(u, u) 692 PerNs::both(u, u)
693 } 693 }
694 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), 694 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
695 raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()), 695 raw::DefKind::Const(ast_id) => {
696 let def = ConstLoc {
697 container: ContainerId::ModuleId(module),
698 ast_id: AstId::new(self.file_id, ast_id),
699 }
700 .intern(self.def_collector.db);
701
702 PerNs::values(def.into())
703 }
696 raw::DefKind::Static(ast_id) => { 704 raw::DefKind::Static(ast_id) => {
697 PerNs::values(StaticId::from_ast_id(ctx, ast_id).into()) 705 PerNs::values(StaticId::from_ast_id(ctx, ast_id).into())
698 } 706 }
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
index bb61e852a..877d73d66 100644
--- a/crates/ra_hir_def/src/traits.rs
+++ b/crates/ra_hir_def/src/traits.rs
@@ -8,11 +8,10 @@ use hir_expand::{
8}; 8};
9 9
10use ra_syntax::ast::{self, NameOwner}; 10use ra_syntax::ast::{self, NameOwner};
11use rustc_hash::FxHashMap;
12 11
13use crate::{ 12use crate::{
14 db::DefDatabase2, AssocItemId, AstItemDef, ConstId, ContainerId, FunctionLoc, Intern, 13 db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
15 LocationCtx, ModuleDefId, ModuleId, TraitId, TypeAliasLoc, 14 TypeAliasLoc,
16}; 15};
17 16
18#[derive(Debug, Clone, PartialEq, Eq)] 17#[derive(Debug, Clone, PartialEq, Eq)]
@@ -26,8 +25,6 @@ impl TraitData {
26 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> { 25 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
27 let src = tr.source(db); 26 let src = tr.source(db);
28 let name = src.value.name().map(|n| n.as_name()); 27 let name = src.value.name().map(|n| n.as_name());
29 let module = tr.module(db);
30 let ctx = LocationCtx::new(db, module, src.file_id);
31 let auto = src.value.is_auto(); 28 let auto = src.value.is_auto();
32 let ast_id_map = db.ast_id_map(src.file_id); 29 let ast_id_map = db.ast_id_map(src.file_id);
33 let items = if let Some(item_list) = src.value.item_list() { 30 let items = if let Some(item_list) = src.value.item_list() {
@@ -40,7 +37,12 @@ impl TraitData {
40 } 37 }
41 .intern(db) 38 .intern(db)
42 .into(), 39 .into(),
43 ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).into(), 40 ast::ImplItem::ConstDef(it) => ConstLoc {
41 container: ContainerId::TraitId(tr),
42 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
43 }
44 .intern(db)
45 .into(),
44 ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { 46 ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
45 container: ContainerId::TraitId(tr), 47 container: ContainerId::TraitId(tr),
46 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), 48 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
@@ -55,34 +57,3 @@ impl TraitData {
55 Arc::new(TraitData { name, items, auto }) 57 Arc::new(TraitData { name, items, auto })
56 } 58 }
57} 59}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct TraitItemsIndex {
61 traits_by_def: FxHashMap<AssocItemId, TraitId>,
62}
63
64impl TraitItemsIndex {
65 pub fn trait_items_index(db: &impl DefDatabase2, module: ModuleId) -> TraitItemsIndex {
66 let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
67 let crate_def_map = db.crate_def_map(module.krate);
68 for decl in crate_def_map[module.module_id].scope.declarations() {
69 if let ModuleDefId::TraitId(tr) = decl {
70 for item in db.trait_data(tr).items.iter() {
71 match item {
72 AssocItemId::FunctionId(_) => (),
73 AssocItemId::TypeAliasId(_) => (),
74 _ => {
75 let prev = index.traits_by_def.insert(*item, tr);
76 assert!(prev.is_none());
77 }
78 }
79 }
80 }
81 }
82 index
83 }
84
85 pub fn get_parent_trait(&self, item: AssocItemId) -> Option<TraitId> {
86 self.traits_by_def.get(&item).cloned()
87 }
88}