aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-12 14:20:02 +0000
committerGitHub <[email protected]>2019-12-12 14:20:02 +0000
commitc213c3b36b7987504c6c3703b76a637dd0d01355 (patch)
tree36f9066ab25826f9401216d6ff371954eb9f447c /crates/ra_hir_def/src
parentd8dba7fbe587ba5fb6cfe939083ccd92c61f5827 (diff)
parent7a255a2f9381ba5886cacc48c1dd0420a739a55c (diff)
Merge #2539
2539: Remove old location infra r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/adt.rs16
-rw-r--r--crates/ra_hir_def/src/attr.rs11
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs22
-rw-r--r--crates/ra_hir_def/src/data.rs6
-rw-r--r--crates/ra_hir_def/src/db.rs16
-rw-r--r--crates/ra_hir_def/src/docs.rs10
-rw-r--r--crates/ra_hir_def/src/generics.rs10
-rw-r--r--crates/ra_hir_def/src/keys.rs7
-rw-r--r--crates/ra_hir_def/src/lib.rs169
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs31
-rw-r--r--crates/ra_hir_def/src/resolver.rs8
-rw-r--r--crates/ra_hir_def/src/src.rs41
12 files changed, 204 insertions, 143 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index db3e63ef8..ec3d57d1a 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -11,8 +11,8 @@ use ra_arena::{map::ArenaMap, Arena};
11use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 11use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
12 12
13use crate::{ 13use crate::{
14 db::DefDatabase, src::HasChildSource, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, 14 db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId,
15 LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, 15 LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId,
16}; 16};
17 17
18/// Note that we use `StructData` for unions as well! 18/// Note that we use `StructData` for unions as well!
@@ -50,14 +50,14 @@ pub struct StructFieldData {
50 50
51impl StructData { 51impl StructData {
52 pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { 52 pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> {
53 let src = id.source(db); 53 let src = id.lookup(db).source(db);
54 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 54 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
55 let variant_data = VariantData::new(src.value.kind()); 55 let variant_data = VariantData::new(src.value.kind());
56 let variant_data = Arc::new(variant_data); 56 let variant_data = Arc::new(variant_data);
57 Arc::new(StructData { name, variant_data }) 57 Arc::new(StructData { name, variant_data })
58 } 58 }
59 pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc<StructData> { 59 pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc<StructData> {
60 let src = id.source(db); 60 let src = id.lookup(db).source(db);
61 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 61 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
62 let variant_data = VariantData::new( 62 let variant_data = VariantData::new(
63 src.value 63 src.value
@@ -72,7 +72,7 @@ impl StructData {
72 72
73impl EnumData { 73impl EnumData {
74 pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> { 74 pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> {
75 let src = e.source(db); 75 let src = e.lookup(db).source(db);
76 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 76 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
77 let mut trace = Trace::new_for_arena(); 77 let mut trace = Trace::new_for_arena();
78 lower_enum(&mut trace, &src.value); 78 lower_enum(&mut trace, &src.value);
@@ -89,7 +89,7 @@ impl HasChildSource for EnumId {
89 type ChildId = LocalEnumVariantId; 89 type ChildId = LocalEnumVariantId;
90 type Value = ast::EnumVariant; 90 type Value = ast::EnumVariant;
91 fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { 91 fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
92 let src = self.source(db); 92 let src = self.lookup(db).source(db);
93 let mut trace = Trace::new_for_map(); 93 let mut trace = Trace::new_for_map();
94 lower_enum(&mut trace, &src.value); 94 lower_enum(&mut trace, &src.value);
95 src.with_value(trace.into_map()) 95 src.with_value(trace.into_map())
@@ -153,8 +153,8 @@ impl HasChildSource for VariantId {
153 let src = it.parent.child_source(db); 153 let src = it.parent.child_source(db);
154 src.map(|map| map[it.local_id].kind()) 154 src.map(|map| map[it.local_id].kind())
155 } 155 }
156 VariantId::StructId(it) => it.source(db).map(|it| it.kind()), 156 VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()),
157 VariantId::UnionId(it) => it.source(db).map(|it| { 157 VariantId::UnionId(it) => it.lookup(db).source(db).map(|it| {
158 it.record_field_def_list() 158 it.record_field_def_list()
159 .map(ast::StructKind::Record) 159 .map(ast::StructKind::Record)
160 .unwrap_or(ast::StructKind::Unit) 160 .unwrap_or(ast::StructKind::Unit)
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 12d4e777a..5bf82e191 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -12,8 +12,7 @@ use ra_syntax::{
12use tt::Subtree; 12use tt::Subtree;
13 13
14use crate::{ 14use crate::{
15 db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AstItemDef, AttrDefId, 15 db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AttrDefId, Lookup,
16 Lookup,
17}; 16};
18 17
19#[derive(Default, Debug, Clone, PartialEq, Eq)] 18#[derive(Default, Debug, Clone, PartialEq, Eq)]
@@ -56,11 +55,11 @@ impl Attrs {
56 Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) 55 Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner))
57 } 56 }
58 AttrDefId::AdtId(it) => match it { 57 AttrDefId::AdtId(it) => match it {
59 AdtId::StructId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 58 AdtId::StructId(it) => attrs_from_loc(it.lookup(db), db),
60 AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 59 AdtId::EnumId(it) => attrs_from_loc(it.lookup(db), db),
61 AdtId::UnionId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 60 AdtId::UnionId(it) => attrs_from_loc(it.lookup(db), db),
62 }, 61 },
63 AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 62 AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db),
64 AttrDefId::MacroDefId(it) => { 63 AttrDefId::MacroDefId(it) => {
65 it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) 64 it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
66 } 65 }
diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs
index 821549bd5..3c9379b15 100644
--- a/crates/ra_hir_def/src/child_by_source.rs
+++ b/crates/ra_hir_def/src/child_by_source.rs
@@ -11,8 +11,8 @@ use crate::{
11 dyn_map::DynMap, 11 dyn_map::DynMap,
12 keys, 12 keys,
13 src::{HasChildSource, HasSource}, 13 src::{HasChildSource, HasSource},
14 AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId, StructFieldId, 14 AdtId, AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId,
15 TraitId, VariantId, 15 StructFieldId, TraitId, VariantId,
16}; 16};
17 17
18pub trait ChildBySource { 18pub trait ChildBySource {
@@ -94,6 +94,24 @@ impl ChildBySource for ModuleId {
94 let src = ty.lookup(db).source(db); 94 let src = ty.lookup(db).source(db);
95 res[keys::TYPE_ALIAS].insert(src, ty) 95 res[keys::TYPE_ALIAS].insert(src, ty)
96 } 96 }
97 ModuleDefId::TraitId(trait_) => {
98 let src = trait_.lookup(db).source(db);
99 res[keys::TRAIT].insert(src, trait_)
100 }
101 ModuleDefId::AdtId(adt) => match adt {
102 AdtId::StructId(strukt) => {
103 let src = strukt.lookup(db).source(db);
104 res[keys::STRUCT].insert(src, strukt)
105 }
106 AdtId::UnionId(union_) => {
107 let src = union_.lookup(db).source(db);
108 res[keys::UNION].insert(src, union_)
109 }
110 AdtId::EnumId(enum_) => {
111 let src = enum_.lookup(db).source(db);
112 res[keys::ENUM].insert(src, enum_)
113 }
114 },
97 _ => (), 115 _ => (),
98 } 116 }
99 } 117 }
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 42821b9b1..b2dac183e 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -12,8 +12,8 @@ use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::HasSource, 13 src::HasSource,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, 15 AssocItemId, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup,
16 Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17}; 17};
18 18
19#[derive(Debug, Clone, PartialEq, Eq)] 19#[derive(Debug, Clone, PartialEq, Eq)]
@@ -94,7 +94,7 @@ pub struct TraitData {
94 94
95impl TraitData { 95impl TraitData {
96 pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> { 96 pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> {
97 let src = tr.source(db); 97 let src = tr.lookup(db).source(db);
98 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 98 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
99 let auto = src.value.is_auto(); 99 let auto = src.value.is_auto();
100 let ast_id_map = db.ast_id_map(src.file_id); 100 let ast_id_map = db.ast_id_map(src.file_id);
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 8907aacca..98bff6cb7 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -3,7 +3,7 @@ use std::sync::Arc;
3 3
4use hir_expand::{db::AstDatabase, HirFileId}; 4use hir_expand::{db::AstDatabase, HirFileId};
5use ra_db::{salsa, CrateId, SourceDatabase}; 5use ra_db::{salsa, CrateId, SourceDatabase};
6use ra_syntax::{ast, SmolStr}; 6use ra_syntax::SmolStr;
7 7
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
@@ -17,9 +17,9 @@ use crate::{
17 raw::{ImportSourceMap, RawItems}, 17 raw::{ImportSourceMap, RawItems},
18 CrateDefMap, 18 CrateDefMap,
19 }, 19 },
20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, 20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
21 ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, 21 GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
22 TypeAliasLoc, UnionId, 22 TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
23}; 23};
24 24
25#[salsa::query_group(InternDatabaseStorage)] 25#[salsa::query_group(InternDatabaseStorage)]
@@ -27,17 +27,17 @@ pub trait InternDatabase: SourceDatabase {
27 #[salsa::interned] 27 #[salsa::interned]
28 fn intern_function(&self, loc: FunctionLoc) -> FunctionId; 28 fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
29 #[salsa::interned] 29 #[salsa::interned]
30 fn intern_struct(&self, loc: ItemLoc<ast::StructDef>) -> StructId; 30 fn intern_struct(&self, loc: StructLoc) -> StructId;
31 #[salsa::interned] 31 #[salsa::interned]
32 fn intern_union(&self, loc: ItemLoc<ast::UnionDef>) -> UnionId; 32 fn intern_union(&self, loc: UnionLoc) -> UnionId;
33 #[salsa::interned] 33 #[salsa::interned]
34 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> EnumId; 34 fn intern_enum(&self, loc: EnumLoc) -> EnumId;
35 #[salsa::interned] 35 #[salsa::interned]
36 fn intern_const(&self, loc: ConstLoc) -> ConstId; 36 fn intern_const(&self, loc: ConstLoc) -> ConstId;
37 #[salsa::interned] 37 #[salsa::interned]
38 fn intern_static(&self, loc: StaticLoc) -> StaticId; 38 fn intern_static(&self, loc: StaticLoc) -> StaticId;
39 #[salsa::interned] 39 #[salsa::interned]
40 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> TraitId; 40 fn intern_trait(&self, loc: TraitLoc) -> TraitId;
41 #[salsa::interned] 41 #[salsa::interned]
42 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; 42 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
43 #[salsa::interned] 43 #[salsa::interned]
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
index 61727bd26..b29f142e3 100644
--- a/crates/ra_hir_def/src/docs.rs
+++ b/crates/ra_hir_def/src/docs.rs
@@ -11,7 +11,7 @@ use ra_syntax::ast;
11use crate::{ 11use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::{HasChildSource, HasSource}, 13 src::{HasChildSource, HasSource},
14 AdtId, AstItemDef, AttrDefId, Lookup, 14 AdtId, AttrDefId, Lookup,
15}; 15};
16 16
17/// Holds documentation 17/// Holds documentation
@@ -51,15 +51,15 @@ impl Documentation {
51 } 51 }
52 } 52 }
53 AttrDefId::AdtId(it) => match it { 53 AttrDefId::AdtId(it) => match it {
54 AdtId::StructId(it) => docs_from_ast(&it.source(db).value), 54 AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value),
55 AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), 55 AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value),
56 AdtId::UnionId(it) => docs_from_ast(&it.source(db).value), 56 AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
57 }, 57 },
58 AttrDefId::EnumVariantId(it) => { 58 AttrDefId::EnumVariantId(it) => {
59 let src = it.parent.child_source(db); 59 let src = it.parent.child_source(db);
60 docs_from_ast(&src.value[it.local_id]) 60 docs_from_ast(&src.value[it.local_id])
61 } 61 }
62 AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), 62 AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value),
63 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)), 63 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)),
64 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), 64 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
65 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), 65 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs
index 5d1100945..e502dd798 100644
--- a/crates/ra_hir_def/src/generics.rs
+++ b/crates/ra_hir_def/src/generics.rs
@@ -21,7 +21,7 @@ use crate::{
21 src::HasChildSource, 21 src::HasChildSource,
22 src::HasSource, 22 src::HasSource,
23 type_ref::{TypeBound, TypeRef}, 23 type_ref::{TypeBound, TypeRef},
24 AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, 24 AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
25}; 25};
26 26
27/// Data about a generic parameter (to a function, struct, impl, ...). 27/// Data about a generic parameter (to a function, struct, impl, ...).
@@ -71,22 +71,22 @@ impl GenericParams {
71 src.file_id 71 src.file_id
72 } 72 }
73 GenericDefId::AdtId(AdtId::StructId(it)) => { 73 GenericDefId::AdtId(AdtId::StructId(it)) => {
74 let src = it.source(db); 74 let src = it.lookup(db).source(db);
75 generics.fill(&mut sm, &src.value); 75 generics.fill(&mut sm, &src.value);
76 src.file_id 76 src.file_id
77 } 77 }
78 GenericDefId::AdtId(AdtId::UnionId(it)) => { 78 GenericDefId::AdtId(AdtId::UnionId(it)) => {
79 let src = it.source(db); 79 let src = it.lookup(db).source(db);
80 generics.fill(&mut sm, &src.value); 80 generics.fill(&mut sm, &src.value);
81 src.file_id 81 src.file_id
82 } 82 }
83 GenericDefId::AdtId(AdtId::EnumId(it)) => { 83 GenericDefId::AdtId(AdtId::EnumId(it)) => {
84 let src = it.source(db); 84 let src = it.lookup(db).source(db);
85 generics.fill(&mut sm, &src.value); 85 generics.fill(&mut sm, &src.value);
86 src.file_id 86 src.file_id
87 } 87 }
88 GenericDefId::TraitId(it) => { 88 GenericDefId::TraitId(it) => {
89 let src = it.source(db); 89 let src = it.lookup(db).source(db);
90 90
91 // traits get the Self type as an implicit first type parameter 91 // traits get the Self type as an implicit first type parameter
92 let self_param_id = 92 let self_param_id =
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
index d318b2451..4611c6e38 100644
--- a/crates/ra_hir_def/src/keys.rs
+++ b/crates/ra_hir_def/src/keys.rs
@@ -8,7 +8,8 @@ use rustc_hash::FxHashMap;
8 8
9use crate::{ 9use crate::{
10 dyn_map::{DynMap, Policy}, 10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId, 11 ConstId, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId,
12 TypeAliasId, TypeParamId, UnionId,
12}; 13};
13 14
14type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; 15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
@@ -18,6 +19,10 @@ pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
18pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new(); 19pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
19pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); 20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
20pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new(); 21pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
22pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
23pub const STRUCT: Key<ast::StructDef, StructId> = Key::new();
24pub const UNION: Key<ast::UnionDef, UnionId> = Key::new();
25pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new();
21 26
22pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new(); 27pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
23pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); 28pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 5564b166b..f085bbe87 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -40,14 +40,14 @@ mod test_db;
40#[cfg(test)] 40#[cfg(test)]
41mod marks; 41mod marks;
42 42
43use std::hash::{Hash, Hasher}; 43use std::hash::Hash;
44 44
45use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; 45use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId};
46use ra_arena::{impl_arena_id, RawId}; 46use ra_arena::{impl_arena_id, RawId};
47use ra_db::{impl_intern_key, salsa, CrateId}; 47use ra_db::{impl_intern_key, salsa, CrateId};
48use ra_syntax::{ast, AstNode}; 48use ra_syntax::ast;
49 49
50use crate::{builtin_type::BuiltinType, db::InternDatabase}; 50use crate::builtin_type::BuiltinType;
51 51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct LocalImportId(RawId); 53pub struct LocalImportId(RawId);
@@ -65,63 +65,6 @@ pub struct ModuleId {
65pub struct LocalModuleId(RawId); 65pub struct LocalModuleId(RawId);
66impl_arena_id!(LocalModuleId); 66impl_arena_id!(LocalModuleId);
67 67
68#[derive(Debug)]
69pub struct ItemLoc<N: AstNode> {
70 pub(crate) module: ModuleId,
71 ast_id: AstId<N>,
72}
73
74impl<N: AstNode> PartialEq for ItemLoc<N> {
75 fn eq(&self, other: &Self) -> bool {
76 self.module == other.module && self.ast_id == other.ast_id
77 }
78}
79impl<N: AstNode> Eq for ItemLoc<N> {}
80impl<N: AstNode> Hash for ItemLoc<N> {
81 fn hash<H: Hasher>(&self, hasher: &mut H) {
82 self.module.hash(hasher);
83 self.ast_id.hash(hasher);
84 }
85}
86
87impl<N: AstNode> Clone for ItemLoc<N> {
88 fn clone(&self) -> ItemLoc<N> {
89 ItemLoc { module: self.module, ast_id: self.ast_id }
90 }
91}
92
93#[derive(Clone, Copy)]
94pub struct LocationCtx<DB> {
95 db: DB,
96 module: ModuleId,
97 file_id: HirFileId,
98}
99
100impl<'a, DB> LocationCtx<&'a DB> {
101 pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> {
102 LocationCtx { db, module, file_id }
103 }
104}
105
106pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
107 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
108 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
109
110 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
111 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
112 Self::intern(ctx.db, loc)
113 }
114 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile<N> {
115 let loc = self.lookup_intern(db);
116 let value = loc.ast_id.to_node(db);
117 InFile { file_id: loc.ast_id.file_id, value }
118 }
119 fn module(self, db: &impl InternDatabase) -> ModuleId {
120 let loc = self.lookup_intern(db);
121 loc.module
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub struct FunctionId(salsa::InternId); 69pub struct FunctionId(salsa::InternId);
127impl_intern_key!(FunctionId); 70impl_intern_key!(FunctionId);
@@ -149,36 +92,72 @@ impl Lookup for FunctionId {
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
150pub struct StructId(salsa::InternId); 93pub struct StructId(salsa::InternId);
151impl_intern_key!(StructId); 94impl_intern_key!(StructId);
152impl AstItemDef<ast::StructDef> for StructId { 95
153 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { 96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
154 db.intern_struct(loc) 97pub struct StructLoc {
98 pub container: ModuleId,
99 pub ast_id: AstId<ast::StructDef>,
100}
101
102impl Intern for StructLoc {
103 type ID = StructId;
104 fn intern(self, db: &impl db::DefDatabase) -> StructId {
105 db.intern_struct(self)
155 } 106 }
156 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { 107}
157 db.lookup_intern_struct(self) 108
109impl Lookup for StructId {
110 type Data = StructLoc;
111 fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc {
112 db.lookup_intern_struct(*self)
158 } 113 }
159} 114}
160 115
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162pub struct UnionId(salsa::InternId); 117pub struct UnionId(salsa::InternId);
163impl_intern_key!(UnionId); 118impl_intern_key!(UnionId);
164impl AstItemDef<ast::UnionDef> for UnionId { 119
165 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::UnionDef>) -> Self { 120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
166 db.intern_union(loc) 121pub struct UnionLoc {
122 pub container: ModuleId,
123 pub ast_id: AstId<ast::UnionDef>,
124}
125
126impl Intern for UnionLoc {
127 type ID = UnionId;
128 fn intern(self, db: &impl db::DefDatabase) -> UnionId {
129 db.intern_union(self)
167 } 130 }
168 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::UnionDef> { 131}
169 db.lookup_intern_union(self) 132
133impl Lookup for UnionId {
134 type Data = UnionLoc;
135 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
136 db.lookup_intern_union(*self)
170 } 137 }
171} 138}
172 139
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
174pub struct EnumId(salsa::InternId); 141pub struct EnumId(salsa::InternId);
175impl_intern_key!(EnumId); 142impl_intern_key!(EnumId);
176impl AstItemDef<ast::EnumDef> for EnumId { 143
177 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self { 144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
178 db.intern_enum(loc) 145pub struct EnumLoc {
146 pub container: ModuleId,
147 pub ast_id: AstId<ast::EnumDef>,
148}
149
150impl Intern for EnumLoc {
151 type ID = EnumId;
152 fn intern(self, db: &impl db::DefDatabase) -> EnumId {
153 db.intern_enum(self)
179 } 154 }
180 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> { 155}
181 db.lookup_intern_enum(self) 156
157impl Lookup for EnumId {
158 type Data = EnumLoc;
159 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
160 db.lookup_intern_enum(*self)
182 } 161 }
183} 162}
184 163
@@ -253,12 +232,24 @@ impl Lookup for StaticId {
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 232#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254pub struct TraitId(salsa::InternId); 233pub struct TraitId(salsa::InternId);
255impl_intern_key!(TraitId); 234impl_intern_key!(TraitId);
256impl AstItemDef<ast::TraitDef> for TraitId { 235
257 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self { 236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
258 db.intern_trait(loc) 237pub struct TraitLoc {
238 pub container: ModuleId,
239 pub ast_id: AstId<ast::TraitDef>,
240}
241
242impl Intern for TraitLoc {
243 type ID = TraitId;
244 fn intern(self, db: &impl db::DefDatabase) -> TraitId {
245 db.intern_trait(self)
259 } 246 }
260 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> { 247}
261 db.lookup_intern_trait(self) 248
249impl Lookup for TraitId {
250 type Data = TraitLoc;
251 fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
252 db.lookup_intern_trait(*self)
262 } 253 }
263} 254}
264 255
@@ -492,7 +483,7 @@ impl HasModule for FunctionLoc {
492 match self.container { 483 match self.container {
493 ContainerId::ModuleId(it) => it, 484 ContainerId::ModuleId(it) => it,
494 ContainerId::ImplId(it) => it.lookup(db).container, 485 ContainerId::ImplId(it) => it.lookup(db).container,
495 ContainerId::TraitId(it) => it.module(db), 486 ContainerId::TraitId(it) => it.lookup(db).container,
496 } 487 }
497 } 488 }
498} 489}
@@ -502,7 +493,7 @@ impl HasModule for TypeAliasLoc {
502 match self.container { 493 match self.container {
503 ContainerId::ModuleId(it) => it, 494 ContainerId::ModuleId(it) => it,
504 ContainerId::ImplId(it) => it.lookup(db).container, 495 ContainerId::ImplId(it) => it.lookup(db).container,
505 ContainerId::TraitId(it) => it.module(db), 496 ContainerId::TraitId(it) => it.lookup(db).container,
506 } 497 }
507 } 498 }
508} 499}
@@ -512,7 +503,7 @@ impl HasModule for ConstLoc {
512 match self.container { 503 match self.container {
513 ContainerId::ModuleId(it) => it, 504 ContainerId::ModuleId(it) => it,
514 ContainerId::ImplId(it) => it.lookup(db).container, 505 ContainerId::ImplId(it) => it.lookup(db).container,
515 ContainerId::TraitId(it) => it.module(db), 506 ContainerId::TraitId(it) => it.lookup(db).container,
516 } 507 }
517 } 508 }
518} 509}
@@ -520,9 +511,9 @@ impl HasModule for ConstLoc {
520impl HasModule for AdtId { 511impl HasModule for AdtId {
521 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 512 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
522 match self { 513 match self {
523 AdtId::StructId(it) => it.module(db), 514 AdtId::StructId(it) => it.lookup(db).container,
524 AdtId::UnionId(it) => it.module(db), 515 AdtId::UnionId(it) => it.lookup(db).container,
525 AdtId::EnumId(it) => it.module(db), 516 AdtId::EnumId(it) => it.lookup(db).container,
526 } 517 }
527 } 518 }
528} 519}
@@ -542,10 +533,10 @@ impl HasModule for GenericDefId {
542 match self { 533 match self {
543 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 534 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
544 GenericDefId::AdtId(it) => it.module(db), 535 GenericDefId::AdtId(it) => it.module(db),
545 GenericDefId::TraitId(it) => it.module(db), 536 GenericDefId::TraitId(it) => it.lookup(db).container,
546 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
547 GenericDefId::ImplId(it) => it.lookup(db).container, 538 GenericDefId::ImplId(it) => it.lookup(db).container,
548 GenericDefId::EnumVariantId(it) => it.parent.module(db), 539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
549 GenericDefId::ConstId(it) => it.lookup(db).module(db), 540 GenericDefId::ConstId(it) => it.lookup(db).module(db),
550 } 541 }
551 } 542 }
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index b33507a9a..04aadead1 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -24,9 +24,9 @@ use crate::{
24 }, 24 },
25 path::{Path, PathKind}, 25 path::{Path, PathKind},
26 per_ns::PerNs, 26 per_ns::PerNs,
27 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, 27 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
28 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, 28 LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc,
29 TraitId, TypeAliasLoc, UnionId, 29 TypeAliasLoc, UnionLoc,
30}; 30};
31 31
32pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 32pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -753,8 +753,6 @@ where
753 753
754 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) { 754 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
755 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; 755 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id };
756 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
757
758 // FIXME: check attrs to see if this is an attribute macro invocation; 756 // FIXME: check attrs to see if this is an attribute macro invocation;
759 // in which case we don't add the invocation, just a single attribute 757 // in which case we don't add the invocation, just a single attribute
760 // macro invocation 758 // macro invocation
@@ -773,14 +771,20 @@ where
773 PerNs::values(def.into()) 771 PerNs::values(def.into())
774 } 772 }
775 raw::DefKind::Struct(ast_id) => { 773 raw::DefKind::Struct(ast_id) => {
776 let id = StructId::from_ast_id(ctx, ast_id).into(); 774 let def = StructLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
777 PerNs::both(id, id) 775 .intern(self.def_collector.db);
776 PerNs::both(def.into(), def.into())
778 } 777 }
779 raw::DefKind::Union(ast_id) => { 778 raw::DefKind::Union(ast_id) => {
780 let id = UnionId::from_ast_id(ctx, ast_id).into(); 779 let def = UnionLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
781 PerNs::both(id, id) 780 .intern(self.def_collector.db);
781 PerNs::both(def.into(), def.into())
782 }
783 raw::DefKind::Enum(ast_id) => {
784 let def = EnumLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
785 .intern(self.def_collector.db);
786 PerNs::types(def.into())
782 } 787 }
783 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
784 raw::DefKind::Const(ast_id) => { 788 raw::DefKind::Const(ast_id) => {
785 let def = ConstLoc { 789 let def = ConstLoc {
786 container: ContainerId::ModuleId(module), 790 container: ContainerId::ModuleId(module),
@@ -796,7 +800,12 @@ where
796 800
797 PerNs::values(def.into()) 801 PerNs::values(def.into())
798 } 802 }
799 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), 803 raw::DefKind::Trait(ast_id) => {
804 let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
805 .intern(self.def_collector.db);
806
807 PerNs::types(def.into())
808 }
800 raw::DefKind::TypeAlias(ast_id) => { 809 raw::DefKind::TypeAlias(ast_id) => {
801 let def = TypeAliasLoc { 810 let def = TypeAliasLoc {
802 container: ContainerId::ModuleId(module), 811 container: ContainerId::ModuleId(module),
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index f87b16b44..17b2169d2 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -17,9 +17,9 @@ use crate::{
17 nameres::{BuiltinShadowMode, CrateDefMap}, 17 nameres::{BuiltinShadowMode, CrateDefMap},
18 path::{Path, PathKind}, 18 path::{Path, PathKind},
19 per_ns::PerNs, 19 per_ns::PerNs,
20 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, 20 AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
21 GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, 21 HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
22 StructId, TraitId, TypeAliasId, TypeParamId, VariantId, 22 TypeAliasId, TypeParamId, VariantId,
23}; 23};
24 24
25#[derive(Debug, Clone, Default)] 25#[derive(Debug, Clone, Default)]
@@ -524,7 +524,7 @@ impl HasResolver for ModuleId {
524 524
525impl HasResolver for TraitId { 525impl HasResolver for TraitId {
526 fn resolver(self, db: &impl DefDatabase) -> Resolver { 526 fn resolver(self, db: &impl DefDatabase) -> Resolver {
527 self.module(db).resolver(db).push_generic_params_scope(db, self.into()) 527 self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
528 } 528 }
529} 529}
530 530
diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs
index a5c4359a7..20200d1db 100644
--- a/crates/ra_hir_def/src/src.rs
+++ b/crates/ra_hir_def/src/src.rs
@@ -4,7 +4,10 @@ use hir_expand::InFile;
4use ra_arena::map::ArenaMap; 4use ra_arena::map::ArenaMap;
5use ra_syntax::ast; 5use ra_syntax::ast;
6 6
7use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TypeAliasLoc}; 7use crate::{
8 db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc,
9 TypeAliasLoc, UnionLoc,
10};
8 11
9pub trait HasSource { 12pub trait HasSource {
10 type Value; 13 type Value;
@@ -56,6 +59,42 @@ impl HasSource for ImplLoc {
56 } 59 }
57} 60}
58 61
62impl HasSource for TraitLoc {
63 type Value = ast::TraitDef;
64
65 fn source(&self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
66 let node = self.ast_id.to_node(db);
67 InFile::new(self.ast_id.file_id, node)
68 }
69}
70
71impl HasSource for StructLoc {
72 type Value = ast::StructDef;
73
74 fn source(&self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
75 let node = self.ast_id.to_node(db);
76 InFile::new(self.ast_id.file_id, node)
77 }
78}
79
80impl HasSource for UnionLoc {
81 type Value = ast::UnionDef;
82
83 fn source(&self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
84 let node = self.ast_id.to_node(db);
85 InFile::new(self.ast_id.file_id, node)
86 }
87}
88
89impl HasSource for EnumLoc {
90 type Value = ast::EnumDef;
91
92 fn source(&self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
93 let node = self.ast_id.to_node(db);
94 InFile::new(self.ast_id.file_id, node)
95 }
96}
97
59pub trait HasChildSource { 98pub trait HasChildSource {
60 type ChildId; 99 type ChildId;
61 type Value; 100 type Value;