diff options
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r-- | crates/hir_def/src/attr.rs | 57 | ||||
-rw-r--r-- | crates/hir_def/src/db.rs | 15 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 8 |
3 files changed, 70 insertions, 10 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 8a25b8731..d9df7564d 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -16,7 +16,7 @@ use mbe::ast_to_token_tree; | |||
16 | use smallvec::{smallvec, SmallVec}; | 16 | use smallvec::{smallvec, SmallVec}; |
17 | use syntax::{ | 17 | use syntax::{ |
18 | ast::{self, AstNode, AttrsOwner}, | 18 | ast::{self, AstNode, AttrsOwner}, |
19 | match_ast, AstToken, SmolStr, SyntaxNode, TextRange, TextSize, | 19 | match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize, |
20 | }; | 20 | }; |
21 | use tt::Subtree; | 21 | use tt::Subtree; |
22 | 22 | ||
@@ -403,10 +403,14 @@ impl AttrsWithOwner { | |||
403 | return AttrSourceMap { attrs }; | 403 | return AttrSourceMap { attrs }; |
404 | } | 404 | } |
405 | AttrDefId::FieldId(id) => { | 405 | AttrDefId::FieldId(id) => { |
406 | id.parent.child_source(db).map(|source| match &source[id.local_id] { | 406 | let map = db.fields_attrs_source_map(id.parent); |
407 | Either::Left(field) => ast::AttrsOwnerNode::new(field.clone()), | 407 | let file_id = id.parent.file_id(db); |
408 | Either::Right(field) => ast::AttrsOwnerNode::new(field.clone()), | 408 | let root = db.parse_or_expand(file_id).unwrap(); |
409 | }) | 409 | let owner = match &map[id.local_id] { |
410 | Either::Left(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
411 | Either::Right(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
412 | }; | ||
413 | InFile::new(file_id, owner) | ||
410 | } | 414 | } |
411 | AttrDefId::AdtId(adt) => match adt { | 415 | AttrDefId::AdtId(adt) => match adt { |
412 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 416 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -414,10 +418,12 @@ impl AttrsWithOwner { | |||
414 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 418 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
415 | }, | 419 | }, |
416 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 420 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
417 | AttrDefId::EnumVariantId(id) => id | 421 | AttrDefId::EnumVariantId(id) => { |
418 | .parent | 422 | let map = db.variants_attrs_source_map(id.parent); |
419 | .child_source(db) | 423 | let file_id = id.parent.lookup(db).id.file_id(); |
420 | .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())), | 424 | let root = db.parse_or_expand(file_id).unwrap(); |
425 | InFile::new(file_id, ast::AttrsOwnerNode::new(map[id.local_id].to_node(&root))) | ||
426 | } | ||
421 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 427 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
422 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 428 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
423 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 429 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -746,3 +752,36 @@ fn collect_attrs( | |||
746 | 752 | ||
747 | attrs.into_iter().map(|(_, attr)| attr) | 753 | attrs.into_iter().map(|(_, attr)| attr) |
748 | } | 754 | } |
755 | |||
756 | pub(crate) fn variants_attrs_source_map( | ||
757 | db: &dyn DefDatabase, | ||
758 | def: EnumId, | ||
759 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> { | ||
760 | let mut res = ArenaMap::default(); | ||
761 | let child_source = def.child_source(db); | ||
762 | |||
763 | for (idx, variant) in child_source.value.iter() { | ||
764 | res.insert(idx, AstPtr::new(variant)); | ||
765 | } | ||
766 | |||
767 | Arc::new(res) | ||
768 | } | ||
769 | |||
770 | pub(crate) fn fields_attrs_source_map( | ||
771 | db: &dyn DefDatabase, | ||
772 | def: VariantId, | ||
773 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> { | ||
774 | let mut res = ArenaMap::default(); | ||
775 | let child_source = def.child_source(db); | ||
776 | |||
777 | for (idx, variant) in child_source.value.iter() { | ||
778 | res.insert( | ||
779 | idx, | ||
780 | variant | ||
781 | .as_ref() | ||
782 | .either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))), | ||
783 | ); | ||
784 | } | ||
785 | |||
786 | Arc::new(res) | ||
787 | } | ||
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs index 9b7a213a1..7eadc8e0d 100644 --- a/crates/hir_def/src/db.rs +++ b/crates/hir_def/src/db.rs | |||
@@ -2,9 +2,10 @@ | |||
2 | use std::sync::Arc; | 2 | use std::sync::Arc; |
3 | 3 | ||
4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; | 4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; |
5 | use either::Either; | ||
5 | use hir_expand::{db::AstDatabase, HirFileId}; | 6 | use hir_expand::{db::AstDatabase, HirFileId}; |
6 | use la_arena::ArenaMap; | 7 | use la_arena::ArenaMap; |
7 | use syntax::SmolStr; | 8 | use syntax::{ast, AstPtr, SmolStr}; |
8 | 9 | ||
9 | use crate::{ | 10 | use crate::{ |
10 | adt::{EnumData, StructData}, | 11 | adt::{EnumData, StructData}, |
@@ -122,6 +123,18 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
122 | #[salsa::invoke(Attrs::fields_attrs_query)] | 123 | #[salsa::invoke(Attrs::fields_attrs_query)] |
123 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; | 124 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; |
124 | 125 | ||
126 | #[salsa::invoke(crate::attr::variants_attrs_source_map)] | ||
127 | fn variants_attrs_source_map( | ||
128 | &self, | ||
129 | def: EnumId, | ||
130 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>>; | ||
131 | |||
132 | #[salsa::invoke(crate::attr::fields_attrs_source_map)] | ||
133 | fn fields_attrs_source_map( | ||
134 | &self, | ||
135 | def: VariantId, | ||
136 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>; | ||
137 | |||
125 | #[salsa::invoke(AttrsWithOwner::attrs_query)] | 138 | #[salsa::invoke(AttrsWithOwner::attrs_query)] |
126 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; | 139 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; |
127 | 140 | ||
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index abd6c553f..e2af0e514 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -453,6 +453,14 @@ impl VariantId { | |||
453 | } | 453 | } |
454 | } | 454 | } |
455 | } | 455 | } |
456 | |||
457 | pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId { | ||
458 | match self { | ||
459 | VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(), | ||
460 | VariantId::StructId(it) => it.lookup(db).id.file_id(), | ||
461 | VariantId::UnionId(it) => it.lookup(db).id.file_id(), | ||
462 | } | ||
463 | } | ||
456 | } | 464 | } |
457 | 465 | ||
458 | trait Intern { | 466 | trait Intern { |