aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/lib.rs248
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs3
2 files changed, 57 insertions, 194 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 140eccf26..d11b573e5 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -45,7 +45,7 @@ use std::hash::Hash;
45use hir_expand::{ast_id_map::FileAstId, 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; 48use ra_syntax::{ast, AstNode};
49 49
50use crate::builtin_type::BuiltinType; 50use crate::builtin_type::BuiltinType;
51 51
@@ -65,101 +65,57 @@ pub struct ModuleId {
65pub struct LocalModuleId(RawId); 65pub struct LocalModuleId(RawId);
66impl_arena_id!(LocalModuleId); 66impl_arena_id!(LocalModuleId);
67 67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
69pub struct FunctionId(salsa::InternId);
70impl_intern_key!(FunctionId);
71
72#[derive(Debug, Clone, PartialEq, Eq, Hash)] 68#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct FunctionLoc { 69pub struct ItemLoc<N: AstNode> {
74 pub container: AssocContainerId, 70 pub container: ContainerId,
75 pub ast_id: AstId<ast::FnDef>, 71 pub ast_id: AstId<N>,
76} 72}
77 73
78impl Intern for FunctionLoc { 74#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79 type ID = FunctionId; 75pub struct AssocItemLoc<N: AstNode> {
80 fn intern(self, db: &impl db::DefDatabase) -> FunctionId { 76 pub container: AssocContainerId,
81 db.intern_function(self) 77 pub ast_id: AstId<N>,
82 }
83} 78}
84 79
85impl Lookup for FunctionId { 80macro_rules! impl_intern {
86 type Data = FunctionLoc; 81 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
87 fn lookup(&self, db: &impl db::DefDatabase) -> FunctionLoc { 82 impl_intern_key!($id);
88 db.lookup_intern_function(*self)
89 }
90}
91 83
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 84 impl Intern for $loc {
93pub struct StructId(salsa::InternId); 85 type ID = $id;
94impl_intern_key!(StructId); 86 fn intern(self, db: &impl db::DefDatabase) -> $id {
87 db.$intern(self)
88 }
89 }
95 90
96#[derive(Debug, Clone, PartialEq, Eq, Hash)] 91 impl Lookup for $id {
97pub struct StructLoc { 92 type Data = $loc;
98 pub container: ContainerId, 93 fn lookup(&self, db: &impl db::DefDatabase) -> $loc {
99 pub ast_id: AstId<ast::StructDef>, 94 db.$lookup(*self)
95 }
96 }
97 };
100} 98}
101 99
102impl Intern for StructLoc { 100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103 type ID = StructId; 101pub struct FunctionId(salsa::InternId);
104 fn intern(self, db: &impl db::DefDatabase) -> StructId { 102type FunctionLoc = AssocItemLoc<ast::FnDef>;
105 db.intern_struct(self) 103impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
106 }
107}
108 104
109impl Lookup for StructId { 105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
110 type Data = StructLoc; 106pub struct StructId(salsa::InternId);
111 fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc { 107type StructLoc = ItemLoc<ast::StructDef>;
112 db.lookup_intern_struct(*self) 108impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
113 }
114}
115 109
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
117pub struct UnionId(salsa::InternId); 111pub struct UnionId(salsa::InternId);
118impl_intern_key!(UnionId); 112pub type UnionLoc = ItemLoc<ast::UnionDef>;
119 113impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
121pub struct UnionLoc {
122 pub container: ContainerId,
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)
130 }
131}
132
133impl Lookup for UnionId {
134 type Data = UnionLoc;
135 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
136 db.lookup_intern_union(*self)
137 }
138}
139 114
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 115#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub struct EnumId(salsa::InternId); 116pub struct EnumId(salsa::InternId);
142impl_intern_key!(EnumId); 117pub type EnumLoc = ItemLoc<ast::EnumDef>;
143 118impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
145pub struct EnumLoc {
146 pub container: ContainerId,
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)
154 }
155}
156
157impl Lookup for EnumId {
158 type Data = EnumLoc;
159 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
160 db.lookup_intern_enum(*self)
161 }
162}
163 119
164// FIXME: rename to `VariantId`, only enums can ave variants 120// FIXME: rename to `VariantId`, only enums can ave variants
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -184,122 +140,38 @@ impl_arena_id!(LocalStructFieldId);
184 140
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 141#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
186pub struct ConstId(salsa::InternId); 142pub struct ConstId(salsa::InternId);
187impl_intern_key!(ConstId); 143type ConstLoc = AssocItemLoc<ast::ConstDef>;
188#[derive(Debug, Clone, PartialEq, Eq, Hash)] 144impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
189pub struct ConstLoc {
190 pub container: AssocContainerId,
191 pub ast_id: AstId<ast::ConstDef>,
192}
193
194impl Intern for ConstLoc {
195 type ID = ConstId;
196 fn intern(self, db: &impl db::DefDatabase) -> ConstId {
197 db.intern_const(self)
198 }
199}
200
201impl Lookup for ConstId {
202 type Data = ConstLoc;
203 fn lookup(&self, db: &impl db::DefDatabase) -> ConstLoc {
204 db.lookup_intern_const(*self)
205 }
206}
207 145
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
209pub struct StaticId(salsa::InternId); 147pub struct StaticId(salsa::InternId);
210impl_intern_key!(StaticId); 148pub type StaticLoc = ItemLoc<ast::StaticDef>;
211 149impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
212#[derive(Debug, Clone, PartialEq, Eq, Hash)]
213pub struct StaticLoc {
214 pub container: ContainerId,
215 pub ast_id: AstId<ast::StaticDef>,
216}
217
218impl Intern for StaticLoc {
219 type ID = StaticId;
220 fn intern(self, db: &impl db::DefDatabase) -> StaticId {
221 db.intern_static(self)
222 }
223}
224
225impl Lookup for StaticId {
226 type Data = StaticLoc;
227 fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc {
228 db.lookup_intern_static(*self)
229 }
230}
231 150
232#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
233pub struct TraitId(salsa::InternId); 152pub struct TraitId(salsa::InternId);
234impl_intern_key!(TraitId); 153pub type TraitLoc = ItemLoc<ast::TraitDef>;
235 154impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
237pub struct TraitLoc {
238 pub container: ContainerId,
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)
246 }
247}
248
249impl Lookup for TraitId {
250 type Data = TraitLoc;
251 fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
252 db.lookup_intern_trait(*self)
253 }
254}
255 155
256#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
257pub struct TypeAliasId(salsa::InternId); 157pub struct TypeAliasId(salsa::InternId);
258impl_intern_key!(TypeAliasId); 158type TypeAliasLoc = AssocItemLoc<ast::TypeAliasDef>;
259 159impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
260#[derive(Debug, Clone, PartialEq, Eq, Hash)]
261pub struct TypeAliasLoc {
262 pub container: AssocContainerId,
263 pub ast_id: AstId<ast::TypeAliasDef>,
264}
265
266impl Intern for TypeAliasLoc {
267 type ID = TypeAliasId;
268 fn intern(self, db: &impl db::DefDatabase) -> TypeAliasId {
269 db.intern_type_alias(self)
270 }
271}
272
273impl Lookup for TypeAliasId {
274 type Data = TypeAliasLoc;
275 fn lookup(&self, db: &impl db::DefDatabase) -> TypeAliasLoc {
276 db.lookup_intern_type_alias(*self)
277 }
278}
279 160
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
281pub struct ImplId(salsa::InternId); 162pub struct ImplId(salsa::InternId);
282impl_intern_key!(ImplId); 163type ImplLoc = ItemLoc<ast::ImplBlock>;
283 164impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
284#[derive(Debug, Clone, PartialEq, Eq, Hash)]
285pub struct ImplLoc {
286 pub container: ModuleId,
287 pub ast_id: AstId<ast::ImplBlock>,
288}
289 165
290impl Intern for ImplLoc { 166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
291 type ID = ImplId; 167pub struct TypeParamId {
292 fn intern(self, db: &impl db::DefDatabase) -> ImplId { 168 pub parent: GenericDefId,
293 db.intern_impl(self) 169 pub local_id: LocalTypeParamId,
294 }
295} 170}
296 171
297impl Lookup for ImplId { 172#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298 type Data = ImplLoc; 173pub struct LocalTypeParamId(RawId);
299 fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { 174impl_arena_id!(LocalTypeParamId);
300 db.lookup_intern_impl(*self)
301 }
302}
303 175
304macro_rules! impl_froms { 176macro_rules! impl_froms {
305 ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { 177 ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => {
@@ -321,16 +193,6 @@ macro_rules! impl_froms {
321} 193}
322 194
323#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
324pub struct TypeParamId {
325 pub parent: GenericDefId,
326 pub local_id: LocalTypeParamId,
327}
328
329#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
330pub struct LocalTypeParamId(RawId);
331impl_arena_id!(LocalTypeParamId);
332
333#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
334pub enum ContainerId { 196pub enum ContainerId {
335 ModuleId(ModuleId), 197 ModuleId(ModuleId),
336 DefWithBodyId(DefWithBodyId), 198 DefWithBodyId(DefWithBodyId),
@@ -498,7 +360,7 @@ impl HasModule for AssocContainerId {
498 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 360 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
499 match *self { 361 match *self {
500 AssocContainerId::ContainerId(it) => it.module(db), 362 AssocContainerId::ContainerId(it) => it.module(db),
501 AssocContainerId::ImplId(it) => it.lookup(db).container, 363 AssocContainerId::ImplId(it) => it.lookup(db).container.module(db),
502 AssocContainerId::TraitId(it) => it.lookup(db).container.module(db), 364 AssocContainerId::TraitId(it) => it.lookup(db).container.module(db),
503 } 365 }
504 } 366 }
@@ -550,7 +412,7 @@ impl HasModule for GenericDefId {
550 GenericDefId::AdtId(it) => it.module(db), 412 GenericDefId::AdtId(it) => it.module(db),
551 GenericDefId::TraitId(it) => it.lookup(db).container.module(db), 413 GenericDefId::TraitId(it) => it.lookup(db).container.module(db),
552 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 414 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
553 GenericDefId::ImplId(it) => it.lookup(db).container, 415 GenericDefId::ImplId(it) => it.lookup(db).container.module(db),
554 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db), 416 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
555 GenericDefId::ConstId(it) => it.lookup(db).module(db), 417 GenericDefId::ConstId(it) => it.lookup(db).module(db),
556 } 418 }
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index a1ea130e0..e68bf4868 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -661,9 +661,10 @@ where
661 krate: self.def_collector.def_map.krate, 661 krate: self.def_collector.def_map.krate,
662 local_id: self.module_id, 662 local_id: self.module_id,
663 }; 663 };
664 let container = ContainerId::ModuleId(module);
664 let ast_id = self.raw_items[imp].ast_id; 665 let ast_id = self.raw_items[imp].ast_id;
665 let impl_id = 666 let impl_id =
666 ImplLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 667 ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
667 .intern(self.def_collector.db); 668 .intern(self.def_collector.db);
668 self.def_collector.def_map.modules[self.module_id].impls.push(impl_id) 669 self.def_collector.def_map.modules[self.module_id].impls.push(impl_id)
669 } 670 }