diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 38c110570..b9a13776f 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -199,16 +199,34 @@ pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone { | |||
199 | pub struct FunctionId(salsa::InternId); | 199 | pub struct FunctionId(salsa::InternId); |
200 | impl_intern_key!(FunctionId); | 200 | impl_intern_key!(FunctionId); |
201 | 201 | ||
202 | impl AstItemDef<ast::FnDef> for FunctionId { | 202 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
203 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self { | 203 | pub struct FunctionLoc { |
204 | db.intern_function(loc) | 204 | pub container: FunctionContainerId, |
205 | pub ast_id: AstId<ast::FnDef>, | ||
206 | } | ||
207 | |||
208 | impl Intern for FunctionLoc { | ||
209 | type ID = FunctionId; | ||
210 | fn intern(self, db: &impl db::DefDatabase2) -> FunctionId { | ||
211 | db.intern_function(self) | ||
205 | } | 212 | } |
206 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> { | 213 | } |
207 | db.lookup_intern_function(self) | 214 | |
215 | impl Lookup for FunctionId { | ||
216 | type Data = FunctionLoc; | ||
217 | fn lookup(&self, db: &impl db::DefDatabase2) -> FunctionLoc { | ||
218 | db.lookup_intern_function(*self) | ||
208 | } | 219 | } |
209 | } | 220 | } |
210 | 221 | ||
211 | #[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)] | ||
212 | pub struct StructOrUnionId(salsa::InternId); | 230 | pub struct StructOrUnionId(salsa::InternId); |
213 | impl_intern_key!(StructOrUnionId); | 231 | impl_intern_key!(StructOrUnionId); |
214 | impl AstItemDef<ast::StructDef> for StructOrUnionId { | 232 | impl AstItemDef<ast::StructDef> for StructOrUnionId { |
@@ -433,3 +451,41 @@ impl_froms!( | |||
433 | EnumVariantId, | 451 | EnumVariantId, |
434 | ConstId | 452 | ConstId |
435 | ); | 453 | ); |
454 | |||
455 | trait Intern { | ||
456 | type ID; | ||
457 | fn intern(self, db: &impl db::DefDatabase2) -> Self::ID; | ||
458 | } | ||
459 | |||
460 | pub trait Lookup { | ||
461 | type Data; | ||
462 | fn lookup(&self, db: &impl db::DefDatabase2) -> Self::Data; | ||
463 | } | ||
464 | |||
465 | pub trait HasModule { | ||
466 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId; | ||
467 | } | ||
468 | |||
469 | impl HasModule for FunctionLoc { | ||
470 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | ||
471 | match self.container { | ||
472 | FunctionContainerId::ModuleId(it) => it, | ||
473 | FunctionContainerId::ImplId(it) => it.module(db), | ||
474 | FunctionContainerId::TraitId(it) => it.module(db), | ||
475 | } | ||
476 | } | ||
477 | } | ||
478 | |||
479 | pub trait HasSource { | ||
480 | type Value; | ||
481 | fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>; | ||
482 | } | ||
483 | |||
484 | impl HasSource for FunctionLoc { | ||
485 | type Value = ast::FnDef; | ||
486 | |||
487 | fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::FnDef> { | ||
488 | let node = self.ast_id.to_node(db); | ||
489 | Source::new(self.ast_id.file_id(), node) | ||
490 | } | ||
491 | } | ||