diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 103 |
1 files changed, 82 insertions, 21 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index b9a13776f..0af41de87 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -201,7 +201,7 @@ impl_intern_key!(FunctionId); | |||
201 | 201 | ||
202 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 202 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
203 | pub struct FunctionLoc { | 203 | pub struct FunctionLoc { |
204 | pub container: FunctionContainerId, | 204 | pub container: ContainerId, |
205 | pub ast_id: AstId<ast::FnDef>, | 205 | pub ast_id: AstId<ast::FnDef>, |
206 | } | 206 | } |
207 | 207 | ||
@@ -220,13 +220,6 @@ impl Lookup for FunctionId { | |||
220 | } | 220 | } |
221 | 221 | ||
222 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 222 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
223 | pub enum FunctionContainerId { | ||
224 | ModuleId(ModuleId), | ||
225 | ImplId(ImplId), | ||
226 | TraitId(TraitId), | ||
227 | } | ||
228 | |||
229 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
230 | pub struct StructOrUnionId(salsa::InternId); | 223 | pub struct StructOrUnionId(salsa::InternId); |
231 | impl_intern_key!(StructOrUnionId); | 224 | impl_intern_key!(StructOrUnionId); |
232 | impl AstItemDef<ast::StructDef> for StructOrUnionId { | 225 | impl AstItemDef<ast::StructDef> for StructOrUnionId { |
@@ -296,12 +289,23 @@ impl_arena_id!(LocalStructFieldId); | |||
296 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 289 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
297 | pub struct ConstId(salsa::InternId); | 290 | pub struct ConstId(salsa::InternId); |
298 | impl_intern_key!(ConstId); | 291 | impl_intern_key!(ConstId); |
299 | impl AstItemDef<ast::ConstDef> for ConstId { | 292 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
300 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self { | 293 | pub struct ConstLoc { |
301 | db.intern_const(loc) | 294 | pub container: ContainerId, |
295 | pub ast_id: AstId<ast::ConstDef>, | ||
296 | } | ||
297 | |||
298 | impl Intern for ConstLoc { | ||
299 | type ID = ConstId; | ||
300 | fn intern(self, db: &impl db::DefDatabase2) -> ConstId { | ||
301 | db.intern_const(self) | ||
302 | } | 302 | } |
303 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> { | 303 | } |
304 | db.lookup_intern_const(self) | 304 | |
305 | impl Lookup for ConstId { | ||
306 | type Data = ConstLoc; | ||
307 | fn lookup(&self, db: &impl db::DefDatabase2) -> ConstLoc { | ||
308 | db.lookup_intern_const(*self) | ||
305 | } | 309 | } |
306 | } | 310 | } |
307 | 311 | ||
@@ -332,12 +336,24 @@ impl AstItemDef<ast::TraitDef> for TraitId { | |||
332 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 336 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
333 | pub struct TypeAliasId(salsa::InternId); | 337 | pub struct TypeAliasId(salsa::InternId); |
334 | impl_intern_key!(TypeAliasId); | 338 | impl_intern_key!(TypeAliasId); |
335 | impl AstItemDef<ast::TypeAliasDef> for TypeAliasId { | 339 | |
336 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self { | 340 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
337 | db.intern_type_alias(loc) | 341 | pub struct TypeAliasLoc { |
342 | pub container: ContainerId, | ||
343 | pub ast_id: AstId<ast::TypeAliasDef>, | ||
344 | } | ||
345 | |||
346 | impl Intern for TypeAliasLoc { | ||
347 | type ID = TypeAliasId; | ||
348 | fn intern(self, db: &impl db::DefDatabase2) -> TypeAliasId { | ||
349 | db.intern_type_alias(self) | ||
338 | } | 350 | } |
339 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> { | 351 | } |
340 | db.lookup_intern_type_alias(self) | 352 | |
353 | impl Lookup for TypeAliasId { | ||
354 | type Data = TypeAliasLoc; | ||
355 | fn lookup(&self, db: &impl db::DefDatabase2) -> TypeAliasLoc { | ||
356 | db.lookup_intern_type_alias(*self) | ||
341 | } | 357 | } |
342 | } | 358 | } |
343 | 359 | ||
@@ -372,6 +388,13 @@ macro_rules! impl_froms { | |||
372 | } | 388 | } |
373 | } | 389 | } |
374 | 390 | ||
391 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
392 | pub enum ContainerId { | ||
393 | ModuleId(ModuleId), | ||
394 | ImplId(ImplId), | ||
395 | TraitId(TraitId), | ||
396 | } | ||
397 | |||
375 | /// A Data Type | 398 | /// A Data Type |
376 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 399 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
377 | pub enum AdtId { | 400 | pub enum AdtId { |
@@ -469,9 +492,29 @@ pub trait HasModule { | |||
469 | impl HasModule for FunctionLoc { | 492 | impl HasModule for FunctionLoc { |
470 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | 493 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { |
471 | match self.container { | 494 | match self.container { |
472 | FunctionContainerId::ModuleId(it) => it, | 495 | ContainerId::ModuleId(it) => it, |
473 | FunctionContainerId::ImplId(it) => it.module(db), | 496 | ContainerId::ImplId(it) => it.module(db), |
474 | FunctionContainerId::TraitId(it) => it.module(db), | 497 | ContainerId::TraitId(it) => it.module(db), |
498 | } | ||
499 | } | ||
500 | } | ||
501 | |||
502 | impl HasModule for TypeAliasLoc { | ||
503 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | ||
504 | match self.container { | ||
505 | ContainerId::ModuleId(it) => it, | ||
506 | ContainerId::ImplId(it) => it.module(db), | ||
507 | ContainerId::TraitId(it) => it.module(db), | ||
508 | } | ||
509 | } | ||
510 | } | ||
511 | |||
512 | impl 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), | ||
475 | } | 518 | } |
476 | } | 519 | } |
477 | } | 520 | } |
@@ -489,3 +532,21 @@ impl HasSource for FunctionLoc { | |||
489 | Source::new(self.ast_id.file_id(), node) | 532 | Source::new(self.ast_id.file_id(), node) |
490 | } | 533 | } |
491 | } | 534 | } |
535 | |||
536 | impl HasSource for TypeAliasLoc { | ||
537 | type Value = ast::TypeAliasDef; | ||
538 | |||
539 | fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::TypeAliasDef> { | ||
540 | let node = self.ast_id.to_node(db); | ||
541 | Source::new(self.ast_id.file_id(), node) | ||
542 | } | ||
543 | } | ||
544 | |||
545 | impl 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 | } | ||