aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r--crates/ra_hir_def/src/lib.rs52
1 files changed, 32 insertions, 20 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index b063530c2..f60feb5fa 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -94,25 +94,10 @@ impl<'a, DB> LocationCtx<&'a DB> {
94 } 94 }
95} 95}
96 96
97impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> {
98 pub fn to_def<N, DEF>(self, ast: &N) -> DEF
99 where
100 N: AstNode,
101 DEF: AstItemDef<N>,
102 {
103 DEF::from_ast(self, ast)
104 }
105}
106
107pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone { 97pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
108 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self; 98 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
109 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>; 99 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
110 100
111 fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self {
112 let items = ctx.db.ast_id_map(ctx.file_id);
113 let item_id = items.ast_id(ast);
114 Self::from_ast_id(ctx, item_id)
115 }
116 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self { 101 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
117 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; 102 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
118 Self::intern(ctx.db, loc) 103 Self::intern(ctx.db, loc)
@@ -245,12 +230,24 @@ impl Lookup for ConstId {
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
246pub struct StaticId(salsa::InternId); 231pub struct StaticId(salsa::InternId);
247impl_intern_key!(StaticId); 232impl_intern_key!(StaticId);
248impl AstItemDef<ast::StaticDef> for StaticId { 233
249 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self { 234#[derive(Debug, Clone, PartialEq, Eq, Hash)]
250 db.intern_static(loc) 235pub struct StaticLoc {
236 pub container: ModuleId,
237 pub ast_id: AstId<ast::StaticDef>,
238}
239
240impl Intern for StaticLoc {
241 type ID = StaticId;
242 fn intern(self, db: &impl db::DefDatabase) -> StaticId {
243 db.intern_static(self)
251 } 244 }
252 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> { 245}
253 db.lookup_intern_static(self) 246
247impl Lookup for StaticId {
248 type Data = StaticLoc;
249 fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc {
250 db.lookup_intern_static(*self)
254 } 251 }
255} 252}
256 253
@@ -481,6 +478,12 @@ impl HasModule for ConstLoc {
481 } 478 }
482} 479}
483 480
481impl HasModule for StaticLoc {
482 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId {
483 self.container
484 }
485}
486
484pub trait HasSource { 487pub trait HasSource {
485 type Value; 488 type Value;
486 fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>; 489 fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>;
@@ -513,6 +516,15 @@ impl HasSource for ConstLoc {
513 } 516 }
514} 517}
515 518
519impl HasSource for StaticLoc {
520 type Value = ast::StaticDef;
521
522 fn source(&self, db: &impl db::DefDatabase) -> Source<ast::StaticDef> {
523 let node = self.ast_id.to_node(db);
524 Source::new(self.ast_id.file_id(), node)
525 }
526}
527
516pub trait HasChildSource { 528pub trait HasChildSource {
517 type ChildId; 529 type ChildId;
518 type Value; 530 type Value;