diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 283 |
1 files changed, 74 insertions, 209 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index f085bbe87..8ed1599ff 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; | |||
45 | use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; | 45 | use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; |
46 | use ra_arena::{impl_arena_id, RawId}; | 46 | use ra_arena::{impl_arena_id, RawId}; |
47 | use ra_db::{impl_intern_key, salsa, CrateId}; | 47 | use ra_db::{impl_intern_key, salsa, CrateId}; |
48 | use ra_syntax::ast; | 48 | use ra_syntax::{ast, AstNode}; |
49 | 49 | ||
50 | use crate::builtin_type::BuiltinType; | 50 | use crate::builtin_type::BuiltinType; |
51 | 51 | ||
@@ -65,101 +65,57 @@ pub struct ModuleId { | |||
65 | pub struct LocalModuleId(RawId); | 65 | pub struct LocalModuleId(RawId); |
66 | impl_arena_id!(LocalModuleId); | 66 | impl_arena_id!(LocalModuleId); |
67 | 67 | ||
68 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
69 | pub struct FunctionId(salsa::InternId); | ||
70 | impl_intern_key!(FunctionId); | ||
71 | |||
72 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 68 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
73 | pub struct FunctionLoc { | 69 | pub struct ItemLoc<N: AstNode> { |
74 | pub container: ContainerId, | 70 | pub container: ContainerId, |
75 | pub ast_id: AstId<ast::FnDef>, | 71 | pub ast_id: AstId<N>, |
76 | } | 72 | } |
77 | 73 | ||
78 | impl Intern for FunctionLoc { | 74 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
79 | type ID = FunctionId; | 75 | pub 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 | ||
85 | impl Lookup for FunctionId { | 80 | macro_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 { |
93 | pub struct StructId(salsa::InternId); | 85 | type ID = $id; |
94 | impl_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 { |
97 | pub struct StructLoc { | 92 | type Data = $loc; |
98 | pub container: ModuleId, | 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 | ||
102 | impl Intern for StructLoc { | 100 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
103 | type ID = StructId; | 101 | pub struct FunctionId(salsa::InternId); |
104 | fn intern(self, db: &impl db::DefDatabase) -> StructId { | 102 | type FunctionLoc = AssocItemLoc<ast::FnDef>; |
105 | db.intern_struct(self) | 103 | impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function); |
106 | } | ||
107 | } | ||
108 | 104 | ||
109 | impl Lookup for StructId { | 105 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
110 | type Data = StructLoc; | 106 | pub struct StructId(salsa::InternId); |
111 | fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc { | 107 | type StructLoc = ItemLoc<ast::StructDef>; |
112 | db.lookup_intern_struct(*self) | 108 | impl_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)] |
117 | pub struct UnionId(salsa::InternId); | 111 | pub struct UnionId(salsa::InternId); |
118 | impl_intern_key!(UnionId); | 112 | pub type UnionLoc = ItemLoc<ast::UnionDef>; |
119 | 113 | impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union); | |
120 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
121 | pub struct UnionLoc { | ||
122 | pub container: ModuleId, | ||
123 | pub ast_id: AstId<ast::UnionDef>, | ||
124 | } | ||
125 | |||
126 | impl Intern for UnionLoc { | ||
127 | type ID = UnionId; | ||
128 | fn intern(self, db: &impl db::DefDatabase) -> UnionId { | ||
129 | db.intern_union(self) | ||
130 | } | ||
131 | } | ||
132 | |||
133 | impl 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)] |
141 | pub struct EnumId(salsa::InternId); | 116 | pub struct EnumId(salsa::InternId); |
142 | impl_intern_key!(EnumId); | 117 | pub type EnumLoc = ItemLoc<ast::EnumDef>; |
143 | 118 | impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum); | |
144 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
145 | pub struct EnumLoc { | ||
146 | pub container: ModuleId, | ||
147 | pub ast_id: AstId<ast::EnumDef>, | ||
148 | } | ||
149 | |||
150 | impl Intern for EnumLoc { | ||
151 | type ID = EnumId; | ||
152 | fn intern(self, db: &impl db::DefDatabase) -> EnumId { | ||
153 | db.intern_enum(self) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl 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)] |
186 | pub struct ConstId(salsa::InternId); | 142 | pub struct ConstId(salsa::InternId); |
187 | impl_intern_key!(ConstId); | 143 | type ConstLoc = AssocItemLoc<ast::ConstDef>; |
188 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 144 | impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const); |
189 | pub struct ConstLoc { | ||
190 | pub container: ContainerId, | ||
191 | pub ast_id: AstId<ast::ConstDef>, | ||
192 | } | ||
193 | |||
194 | impl Intern for ConstLoc { | ||
195 | type ID = ConstId; | ||
196 | fn intern(self, db: &impl db::DefDatabase) -> ConstId { | ||
197 | db.intern_const(self) | ||
198 | } | ||
199 | } | ||
200 | |||
201 | impl 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)] |
209 | pub struct StaticId(salsa::InternId); | 147 | pub struct StaticId(salsa::InternId); |
210 | impl_intern_key!(StaticId); | 148 | pub type StaticLoc = ItemLoc<ast::StaticDef>; |
211 | 149 | impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static); | |
212 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
213 | pub struct StaticLoc { | ||
214 | pub container: ModuleId, | ||
215 | pub ast_id: AstId<ast::StaticDef>, | ||
216 | } | ||
217 | |||
218 | impl Intern for StaticLoc { | ||
219 | type ID = StaticId; | ||
220 | fn intern(self, db: &impl db::DefDatabase) -> StaticId { | ||
221 | db.intern_static(self) | ||
222 | } | ||
223 | } | ||
224 | |||
225 | impl 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)] |
233 | pub struct TraitId(salsa::InternId); | 152 | pub struct TraitId(salsa::InternId); |
234 | impl_intern_key!(TraitId); | 153 | pub type TraitLoc = ItemLoc<ast::TraitDef>; |
235 | 154 | impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait); | |
236 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
237 | pub struct TraitLoc { | ||
238 | pub container: ModuleId, | ||
239 | pub ast_id: AstId<ast::TraitDef>, | ||
240 | } | ||
241 | |||
242 | impl Intern for TraitLoc { | ||
243 | type ID = TraitId; | ||
244 | fn intern(self, db: &impl db::DefDatabase) -> TraitId { | ||
245 | db.intern_trait(self) | ||
246 | } | ||
247 | } | ||
248 | |||
249 | impl 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)] |
257 | pub struct TypeAliasId(salsa::InternId); | 157 | pub struct TypeAliasId(salsa::InternId); |
258 | impl_intern_key!(TypeAliasId); | 158 | type TypeAliasLoc = AssocItemLoc<ast::TypeAliasDef>; |
259 | 159 | impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias); | |
260 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
261 | pub struct TypeAliasLoc { | ||
262 | pub container: ContainerId, | ||
263 | pub ast_id: AstId<ast::TypeAliasDef>, | ||
264 | } | ||
265 | |||
266 | impl 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 | |||
273 | impl 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)] |
281 | pub struct ImplId(salsa::InternId); | 162 | pub struct ImplId(salsa::InternId); |
282 | impl_intern_key!(ImplId); | 163 | type ImplLoc = ItemLoc<ast::ImplBlock>; |
283 | 164 | impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); | |
284 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
285 | pub struct ImplLoc { | ||
286 | pub container: ModuleId, | ||
287 | pub ast_id: AstId<ast::ImplBlock>, | ||
288 | } | ||
289 | 165 | ||
290 | impl Intern for ImplLoc { | 166 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
291 | type ID = ImplId; | 167 | pub 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 | ||
297 | impl Lookup for ImplId { | 172 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
298 | type Data = ImplLoc; | 173 | pub struct LocalTypeParamId(RawId); |
299 | fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { | 174 | impl_arena_id!(LocalTypeParamId); |
300 | db.lookup_intern_impl(*self) | ||
301 | } | ||
302 | } | ||
303 | 175 | ||
304 | macro_rules! impl_froms { | 176 | macro_rules! impl_froms { |
305 | ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { | 177 | ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { |
@@ -321,21 +193,18 @@ macro_rules! impl_froms { | |||
321 | } | 193 | } |
322 | 194 | ||
323 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 195 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
324 | pub struct TypeParamId { | 196 | pub enum ContainerId { |
325 | pub parent: GenericDefId, | 197 | ModuleId(ModuleId), |
326 | pub local_id: LocalTypeParamId, | 198 | DefWithBodyId(DefWithBodyId), |
327 | } | 199 | } |
328 | 200 | ||
329 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 201 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
330 | pub struct LocalTypeParamId(RawId); | 202 | pub enum AssocContainerId { |
331 | impl_arena_id!(LocalTypeParamId); | 203 | ContainerId(ContainerId), |
332 | |||
333 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
334 | pub enum ContainerId { | ||
335 | ModuleId(ModuleId), | ||
336 | ImplId(ImplId), | 204 | ImplId(ImplId), |
337 | TraitId(TraitId), | 205 | TraitId(TraitId), |
338 | } | 206 | } |
207 | impl_froms!(AssocContainerId: ContainerId); | ||
339 | 208 | ||
340 | /// A Data Type | 209 | /// A Data Type |
341 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 210 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
@@ -478,33 +347,28 @@ pub trait HasModule { | |||
478 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId; | 347 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId; |
479 | } | 348 | } |
480 | 349 | ||
481 | impl HasModule for FunctionLoc { | 350 | impl HasModule for ContainerId { |
482 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 351 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
483 | match self.container { | 352 | match *self { |
484 | ContainerId::ModuleId(it) => it, | 353 | ContainerId::ModuleId(it) => it, |
485 | ContainerId::ImplId(it) => it.lookup(db).container, | 354 | ContainerId::DefWithBodyId(it) => it.module(db), |
486 | ContainerId::TraitId(it) => it.lookup(db).container, | ||
487 | } | 355 | } |
488 | } | 356 | } |
489 | } | 357 | } |
490 | 358 | ||
491 | impl HasModule for TypeAliasLoc { | 359 | impl HasModule for AssocContainerId { |
492 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 360 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
493 | match self.container { | 361 | match *self { |
494 | ContainerId::ModuleId(it) => it, | 362 | AssocContainerId::ContainerId(it) => it.module(db), |
495 | ContainerId::ImplId(it) => it.lookup(db).container, | 363 | AssocContainerId::ImplId(it) => it.lookup(db).container.module(db), |
496 | ContainerId::TraitId(it) => it.lookup(db).container, | 364 | AssocContainerId::TraitId(it) => it.lookup(db).container.module(db), |
497 | } | 365 | } |
498 | } | 366 | } |
499 | } | 367 | } |
500 | 368 | ||
501 | impl HasModule for ConstLoc { | 369 | impl<N: AstNode> HasModule for AssocItemLoc<N> { |
502 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 370 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
503 | match self.container { | 371 | self.container.module(db) |
504 | ContainerId::ModuleId(it) => it, | ||
505 | ContainerId::ImplId(it) => it.lookup(db).container, | ||
506 | ContainerId::TraitId(it) => it.lookup(db).container, | ||
507 | } | ||
508 | } | 372 | } |
509 | } | 373 | } |
510 | 374 | ||
@@ -515,6 +379,7 @@ impl HasModule for AdtId { | |||
515 | AdtId::UnionId(it) => it.lookup(db).container, | 379 | AdtId::UnionId(it) => it.lookup(db).container, |
516 | AdtId::EnumId(it) => it.lookup(db).container, | 380 | AdtId::EnumId(it) => it.lookup(db).container, |
517 | } | 381 | } |
382 | .module(db) | ||
518 | } | 383 | } |
519 | } | 384 | } |
520 | 385 | ||
@@ -533,17 +398,17 @@ impl HasModule for GenericDefId { | |||
533 | match self { | 398 | match self { |
534 | GenericDefId::FunctionId(it) => it.lookup(db).module(db), | 399 | GenericDefId::FunctionId(it) => it.lookup(db).module(db), |
535 | GenericDefId::AdtId(it) => it.module(db), | 400 | GenericDefId::AdtId(it) => it.module(db), |
536 | GenericDefId::TraitId(it) => it.lookup(db).container, | 401 | GenericDefId::TraitId(it) => it.lookup(db).container.module(db), |
537 | GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), | 402 | GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), |
538 | GenericDefId::ImplId(it) => it.lookup(db).container, | 403 | GenericDefId::ImplId(it) => it.lookup(db).container.module(db), |
539 | GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, | 404 | GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db), |
540 | GenericDefId::ConstId(it) => it.lookup(db).module(db), | 405 | GenericDefId::ConstId(it) => it.lookup(db).module(db), |
541 | } | 406 | } |
542 | } | 407 | } |
543 | } | 408 | } |
544 | 409 | ||
545 | impl HasModule for StaticLoc { | 410 | impl HasModule for StaticLoc { |
546 | fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { | 411 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
547 | self.container | 412 | self.container.module(db) |
548 | } | 413 | } |
549 | } | 414 | } |