diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 80 |
1 files changed, 12 insertions, 68 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 7850ea9a7..ecf883272 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -11,9 +11,9 @@ use hir_def::{ | |||
11 | per_ns::PerNs, | 11 | per_ns::PerNs, |
12 | resolver::HasResolver, | 12 | resolver::HasResolver, |
13 | type_ref::{Mutability, TypeRef}, | 13 | type_ref::{Mutability, TypeRef}, |
14 | AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, | 14 | AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, |
15 | LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, | 15 | LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, |
16 | StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, | 16 | TraitId, TypeAliasId, TypeParamId, UnionId, |
17 | }; | 17 | }; |
18 | use hir_expand::{ | 18 | use hir_expand::{ |
19 | diagnostics::DiagnosticSink, | 19 | diagnostics::DiagnosticSink, |
@@ -269,7 +269,7 @@ pub struct Struct { | |||
269 | 269 | ||
270 | impl Struct { | 270 | impl Struct { |
271 | pub fn module(self, db: &impl DefDatabase) -> Module { | 271 | pub fn module(self, db: &impl DefDatabase) -> Module { |
272 | Module { id: self.id.lookup(db).container } | 272 | Module { id: self.id.lookup(db).container.module(db) } |
273 | } | 273 | } |
274 | 274 | ||
275 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 275 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -290,7 +290,7 @@ impl Struct { | |||
290 | } | 290 | } |
291 | 291 | ||
292 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 292 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
293 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 293 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
294 | } | 294 | } |
295 | 295 | ||
296 | fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | 296 | fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { |
@@ -309,11 +309,11 @@ impl Union { | |||
309 | } | 309 | } |
310 | 310 | ||
311 | pub fn module(self, db: &impl DefDatabase) -> Module { | 311 | pub fn module(self, db: &impl DefDatabase) -> Module { |
312 | Module { id: self.id.lookup(db).container } | 312 | Module { id: self.id.lookup(db).container.module(db) } |
313 | } | 313 | } |
314 | 314 | ||
315 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 315 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
316 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 316 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
317 | } | 317 | } |
318 | 318 | ||
319 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { | 319 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { |
@@ -337,7 +337,7 @@ pub struct Enum { | |||
337 | 337 | ||
338 | impl Enum { | 338 | impl Enum { |
339 | pub fn module(self, db: &impl DefDatabase) -> Module { | 339 | pub fn module(self, db: &impl DefDatabase) -> Module { |
340 | Module { id: self.id.lookup(db).container } | 340 | Module { id: self.id.lookup(db).container.module(db) } |
341 | } | 341 | } |
342 | 342 | ||
343 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 343 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -357,7 +357,7 @@ impl Enum { | |||
357 | } | 357 | } |
358 | 358 | ||
359 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 359 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
360 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 360 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
361 | } | 361 | } |
362 | } | 362 | } |
363 | 363 | ||
@@ -529,30 +529,6 @@ impl Const { | |||
529 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { | 529 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
530 | db.const_data(self.id).name.clone() | 530 | db.const_data(self.id).name.clone() |
531 | } | 531 | } |
532 | |||
533 | /// The containing impl block, if this is a type alias. | ||
534 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | ||
535 | match self.container(db) { | ||
536 | Some(Container::ImplBlock(it)) => Some(it), | ||
537 | _ => None, | ||
538 | } | ||
539 | } | ||
540 | |||
541 | /// The containing trait, if this is a trait type alias definition. | ||
542 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | ||
543 | match self.container(db) { | ||
544 | Some(Container::Trait(it)) => Some(it), | ||
545 | _ => None, | ||
546 | } | ||
547 | } | ||
548 | |||
549 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | ||
550 | match self.id.lookup(db).container { | ||
551 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), | ||
552 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), | ||
553 | ContainerId::ModuleId(_) => None, | ||
554 | } | ||
555 | } | ||
556 | } | 532 | } |
557 | 533 | ||
558 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 534 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -577,7 +553,7 @@ pub struct Trait { | |||
577 | 553 | ||
578 | impl Trait { | 554 | impl Trait { |
579 | pub fn module(self, db: &impl DefDatabase) -> Module { | 555 | pub fn module(self, db: &impl DefDatabase) -> Module { |
580 | Module { id: self.id.lookup(db).container } | 556 | Module { id: self.id.lookup(db).container.module(db) } |
581 | } | 557 | } |
582 | 558 | ||
583 | pub fn name(self, db: &impl DefDatabase) -> Name { | 559 | pub fn name(self, db: &impl DefDatabase) -> Name { |
@@ -612,30 +588,6 @@ impl TypeAlias { | |||
612 | Some(self.module(db).krate()) | 588 | Some(self.module(db).krate()) |
613 | } | 589 | } |
614 | 590 | ||
615 | /// The containing impl block, if this is a type alias. | ||
616 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | ||
617 | match self.container(db) { | ||
618 | Some(Container::ImplBlock(it)) => Some(it), | ||
619 | _ => None, | ||
620 | } | ||
621 | } | ||
622 | |||
623 | /// The containing trait, if this is a trait type alias definition. | ||
624 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | ||
625 | match self.container(db) { | ||
626 | Some(Container::Trait(it)) => Some(it), | ||
627 | _ => None, | ||
628 | } | ||
629 | } | ||
630 | |||
631 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | ||
632 | match self.id.lookup(db).container { | ||
633 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), | ||
634 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), | ||
635 | ContainerId::ModuleId(_) => None, | ||
636 | } | ||
637 | } | ||
638 | |||
639 | pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { | 591 | pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { |
640 | db.type_alias_data(self.id).type_ref.clone() | 592 | db.type_alias_data(self.id).type_ref.clone() |
641 | } | 593 | } |
@@ -654,14 +606,6 @@ pub struct MacroDef { | |||
654 | pub(crate) id: MacroDefId, | 606 | pub(crate) id: MacroDefId, |
655 | } | 607 | } |
656 | 608 | ||
657 | impl MacroDef {} | ||
658 | |||
659 | pub enum Container { | ||
660 | Trait(Trait), | ||
661 | ImplBlock(ImplBlock), | ||
662 | } | ||
663 | impl_froms!(Container: Trait, ImplBlock); | ||
664 | |||
665 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 609 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
666 | pub enum AssocItem { | 610 | pub enum AssocItem { |
667 | Function(Function), | 611 | Function(Function), |
@@ -810,7 +754,7 @@ impl ImplBlock { | |||
810 | let environment = TraitEnvironment::lower(db, &resolver); | 754 | let environment = TraitEnvironment::lower(db, &resolver); |
811 | let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); | 755 | let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); |
812 | Type { | 756 | Type { |
813 | krate: self.id.lookup(db).container.krate, | 757 | krate: self.id.lookup(db).container.module(db).krate, |
814 | ty: InEnvironment { value: ty, environment }, | 758 | ty: InEnvironment { value: ty, environment }, |
815 | } | 759 | } |
816 | } | 760 | } |
@@ -824,7 +768,7 @@ impl ImplBlock { | |||
824 | } | 768 | } |
825 | 769 | ||
826 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 770 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
827 | self.id.lookup(db).container.into() | 771 | self.id.lookup(db).container.module(db).into() |
828 | } | 772 | } |
829 | 773 | ||
830 | pub fn krate(&self, db: &impl DefDatabase) -> Crate { | 774 | pub fn krate(&self, db: &impl DefDatabase) -> Crate { |