diff options
Diffstat (limited to 'crates/hir_def/src/lib.rs')
-rw-r--r-- | crates/hir_def/src/lib.rs | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index c9e07de86..000567d99 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -27,6 +27,7 @@ pub mod dyn_map; | |||
27 | pub mod keys; | 27 | pub mod keys; |
28 | 28 | ||
29 | pub mod item_tree; | 29 | pub mod item_tree; |
30 | pub mod intern; | ||
30 | 31 | ||
31 | pub mod adt; | 32 | pub mod adt; |
32 | pub mod data; | 33 | pub mod data; |
@@ -55,15 +56,17 @@ use std::{ | |||
55 | sync::Arc, | 56 | sync::Arc, |
56 | }; | 57 | }; |
57 | 58 | ||
59 | use adt::VariantData; | ||
58 | use base_db::{impl_intern_key, salsa, CrateId}; | 60 | use base_db::{impl_intern_key, salsa, CrateId}; |
59 | use hir_expand::{ | 61 | use hir_expand::{ |
60 | ast_id_map::FileAstId, | 62 | ast_id_map::FileAstId, |
61 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, | 63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, |
62 | hygiene::Hygiene, | 64 | hygiene::Hygiene, |
63 | AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 65 | AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
64 | }; | 66 | }; |
65 | use la_arena::Idx; | 67 | use la_arena::Idx; |
66 | use nameres::DefMap; | 68 | use nameres::DefMap; |
69 | use path::ModPath; | ||
67 | use syntax::ast; | 70 | use syntax::ast; |
68 | 71 | ||
69 | use crate::builtin_type::BuiltinType; | 72 | use crate::builtin_type::BuiltinType; |
@@ -433,6 +436,16 @@ impl_from!( | |||
433 | for AttrDefId | 436 | for AttrDefId |
434 | ); | 437 | ); |
435 | 438 | ||
439 | impl From<AssocContainerId> for AttrDefId { | ||
440 | fn from(acid: AssocContainerId) -> Self { | ||
441 | match acid { | ||
442 | AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid), | ||
443 | AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid), | ||
444 | AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid), | ||
445 | } | ||
446 | } | ||
447 | } | ||
448 | |||
436 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 449 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
437 | pub enum VariantId { | 450 | pub enum VariantId { |
438 | EnumVariantId(EnumVariantId), | 451 | EnumVariantId(EnumVariantId), |
@@ -441,6 +454,26 @@ pub enum VariantId { | |||
441 | } | 454 | } |
442 | impl_from!(EnumVariantId, StructId, UnionId for VariantId); | 455 | impl_from!(EnumVariantId, StructId, UnionId for VariantId); |
443 | 456 | ||
457 | impl VariantId { | ||
458 | pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> { | ||
459 | match self { | ||
460 | VariantId::StructId(it) => db.struct_data(it).variant_data.clone(), | ||
461 | VariantId::UnionId(it) => db.union_data(it).variant_data.clone(), | ||
462 | VariantId::EnumVariantId(it) => { | ||
463 | db.enum_data(it.parent).variants[it.local_id].variant_data.clone() | ||
464 | } | ||
465 | } | ||
466 | } | ||
467 | |||
468 | pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId { | ||
469 | match self { | ||
470 | VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(), | ||
471 | VariantId::StructId(it) => it.lookup(db).id.file_id(), | ||
472 | VariantId::UnionId(it) => it.lookup(db).id.file_id(), | ||
473 | } | ||
474 | } | ||
475 | } | ||
476 | |||
444 | trait Intern { | 477 | trait Intern { |
445 | type ID; | 478 | type ID; |
446 | fn intern(self, db: &dyn db::DefDatabase) -> Self::ID; | 479 | fn intern(self, db: &dyn db::DefDatabase) -> Self::ID; |
@@ -643,7 +676,9 @@ impl<T: ast::AstNode> AstIdWithPath<T> { | |||
643 | } | 676 | } |
644 | } | 677 | } |
645 | 678 | ||
646 | pub struct UnresolvedMacro; | 679 | pub struct UnresolvedMacro { |
680 | pub path: ModPath, | ||
681 | } | ||
647 | 682 | ||
648 | fn macro_call_as_call_id( | 683 | fn macro_call_as_call_id( |
649 | call: &AstIdWithPath<ast::MacroCall>, | 684 | call: &AstIdWithPath<ast::MacroCall>, |
@@ -652,7 +687,8 @@ fn macro_call_as_call_id( | |||
652 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 687 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
653 | error_sink: &mut dyn FnMut(mbe::ExpandError), | 688 | error_sink: &mut dyn FnMut(mbe::ExpandError), |
654 | ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { | 689 | ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { |
655 | let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?; | 690 | let def: MacroDefId = |
691 | resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?; | ||
656 | 692 | ||
657 | let res = if let MacroDefKind::BuiltInEager(..) = def.kind { | 693 | let res = if let MacroDefKind::BuiltInEager(..) = def.kind { |
658 | let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); | 694 | let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); |
@@ -668,24 +704,36 @@ fn macro_call_as_call_id( | |||
668 | ) | 704 | ) |
669 | .map(MacroCallId::from) | 705 | .map(MacroCallId::from) |
670 | } else { | 706 | } else { |
671 | Ok(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(call.ast_id)).into()) | 707 | Ok(def |
708 | .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id }) | ||
709 | .into()) | ||
672 | }; | 710 | }; |
673 | Ok(res) | 711 | Ok(res) |
674 | } | 712 | } |
675 | 713 | ||
676 | fn derive_macro_as_call_id( | 714 | fn derive_macro_as_call_id( |
677 | item_attr: &AstIdWithPath<ast::Item>, | 715 | item_attr: &AstIdWithPath<ast::Item>, |
716 | derive_attr: AttrId, | ||
678 | db: &dyn db::DefDatabase, | 717 | db: &dyn db::DefDatabase, |
679 | krate: CrateId, | 718 | krate: CrateId, |
680 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 719 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
681 | ) -> Result<MacroCallId, UnresolvedMacro> { | 720 | ) -> Result<MacroCallId, UnresolvedMacro> { |
682 | let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?; | 721 | let def: MacroDefId = resolver(item_attr.path.clone()) |
683 | let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?; | 722 | .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; |
723 | let last_segment = item_attr | ||
724 | .path | ||
725 | .segments() | ||
726 | .last() | ||
727 | .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; | ||
684 | let res = def | 728 | let res = def |
685 | .as_lazy_macro( | 729 | .as_lazy_macro( |
686 | db.upcast(), | 730 | db.upcast(), |
687 | krate, | 731 | krate, |
688 | MacroCallKind::Derive(item_attr.ast_id, last_segment.to_string()), | 732 | MacroCallKind::Derive { |
733 | ast_id: item_attr.ast_id, | ||
734 | derive_name: last_segment.to_string(), | ||
735 | derive_attr, | ||
736 | }, | ||
689 | ) | 737 | ) |
690 | .into(); | 738 | .into(); |
691 | Ok(res) | 739 | Ok(res) |