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.rs169
1 files changed, 80 insertions, 89 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 5564b166b..f085bbe87 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -40,14 +40,14 @@ mod test_db;
40#[cfg(test)] 40#[cfg(test)]
41mod marks; 41mod marks;
42 42
43use std::hash::{Hash, Hasher}; 43use std::hash::Hash;
44 44
45use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; 45use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId};
46use ra_arena::{impl_arena_id, RawId}; 46use ra_arena::{impl_arena_id, RawId};
47use ra_db::{impl_intern_key, salsa, CrateId}; 47use ra_db::{impl_intern_key, salsa, CrateId};
48use ra_syntax::{ast, AstNode}; 48use ra_syntax::ast;
49 49
50use crate::{builtin_type::BuiltinType, db::InternDatabase}; 50use crate::builtin_type::BuiltinType;
51 51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct LocalImportId(RawId); 53pub struct LocalImportId(RawId);
@@ -65,63 +65,6 @@ pub struct ModuleId {
65pub struct LocalModuleId(RawId); 65pub struct LocalModuleId(RawId);
66impl_arena_id!(LocalModuleId); 66impl_arena_id!(LocalModuleId);
67 67
68#[derive(Debug)]
69pub struct ItemLoc<N: AstNode> {
70 pub(crate) module: ModuleId,
71 ast_id: AstId<N>,
72}
73
74impl<N: AstNode> PartialEq for ItemLoc<N> {
75 fn eq(&self, other: &Self) -> bool {
76 self.module == other.module && self.ast_id == other.ast_id
77 }
78}
79impl<N: AstNode> Eq for ItemLoc<N> {}
80impl<N: AstNode> Hash for ItemLoc<N> {
81 fn hash<H: Hasher>(&self, hasher: &mut H) {
82 self.module.hash(hasher);
83 self.ast_id.hash(hasher);
84 }
85}
86
87impl<N: AstNode> Clone for ItemLoc<N> {
88 fn clone(&self) -> ItemLoc<N> {
89 ItemLoc { module: self.module, ast_id: self.ast_id }
90 }
91}
92
93#[derive(Clone, Copy)]
94pub struct LocationCtx<DB> {
95 db: DB,
96 module: ModuleId,
97 file_id: HirFileId,
98}
99
100impl<'a, DB> LocationCtx<&'a DB> {
101 pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> {
102 LocationCtx { db, module, file_id }
103 }
104}
105
106pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
107 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
108 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
109
110 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
111 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
112 Self::intern(ctx.db, loc)
113 }
114 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile<N> {
115 let loc = self.lookup_intern(db);
116 let value = loc.ast_id.to_node(db);
117 InFile { file_id: loc.ast_id.file_id, value }
118 }
119 fn module(self, db: &impl InternDatabase) -> ModuleId {
120 let loc = self.lookup_intern(db);
121 loc.module
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub struct FunctionId(salsa::InternId); 69pub struct FunctionId(salsa::InternId);
127impl_intern_key!(FunctionId); 70impl_intern_key!(FunctionId);
@@ -149,36 +92,72 @@ impl Lookup for FunctionId {
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
150pub struct StructId(salsa::InternId); 93pub struct StructId(salsa::InternId);
151impl_intern_key!(StructId); 94impl_intern_key!(StructId);
152impl AstItemDef<ast::StructDef> for StructId { 95
153 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { 96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
154 db.intern_struct(loc) 97pub struct StructLoc {
98 pub container: ModuleId,
99 pub ast_id: AstId<ast::StructDef>,
100}
101
102impl Intern for StructLoc {
103 type ID = StructId;
104 fn intern(self, db: &impl db::DefDatabase) -> StructId {
105 db.intern_struct(self)
155 } 106 }
156 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { 107}
157 db.lookup_intern_struct(self) 108
109impl Lookup for StructId {
110 type Data = StructLoc;
111 fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc {
112 db.lookup_intern_struct(*self)
158 } 113 }
159} 114}
160 115
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162pub struct UnionId(salsa::InternId); 117pub struct UnionId(salsa::InternId);
163impl_intern_key!(UnionId); 118impl_intern_key!(UnionId);
164impl AstItemDef<ast::UnionDef> for UnionId { 119
165 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::UnionDef>) -> Self { 120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
166 db.intern_union(loc) 121pub struct UnionLoc {
122 pub container: ModuleId,
123 pub ast_id: AstId<ast::UnionDef>,
124}
125
126impl Intern for UnionLoc {
127 type ID = UnionId;
128 fn intern(self, db: &impl db::DefDatabase) -> UnionId {
129 db.intern_union(self)
167 } 130 }
168 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::UnionDef> { 131}
169 db.lookup_intern_union(self) 132
133impl Lookup for UnionId {
134 type Data = UnionLoc;
135 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
136 db.lookup_intern_union(*self)
170 } 137 }
171} 138}
172 139
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
174pub struct EnumId(salsa::InternId); 141pub struct EnumId(salsa::InternId);
175impl_intern_key!(EnumId); 142impl_intern_key!(EnumId);
176impl AstItemDef<ast::EnumDef> for EnumId { 143
177 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self { 144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
178 db.intern_enum(loc) 145pub struct EnumLoc {
146 pub container: ModuleId,
147 pub ast_id: AstId<ast::EnumDef>,
148}
149
150impl Intern for EnumLoc {
151 type ID = EnumId;
152 fn intern(self, db: &impl db::DefDatabase) -> EnumId {
153 db.intern_enum(self)
179 } 154 }
180 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> { 155}
181 db.lookup_intern_enum(self) 156
157impl Lookup for EnumId {
158 type Data = EnumLoc;
159 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
160 db.lookup_intern_enum(*self)
182 } 161 }
183} 162}
184 163
@@ -253,12 +232,24 @@ impl Lookup for StaticId {
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 232#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254pub struct TraitId(salsa::InternId); 233pub struct TraitId(salsa::InternId);
255impl_intern_key!(TraitId); 234impl_intern_key!(TraitId);
256impl AstItemDef<ast::TraitDef> for TraitId { 235
257 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self { 236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
258 db.intern_trait(loc) 237pub struct TraitLoc {
238 pub container: ModuleId,
239 pub ast_id: AstId<ast::TraitDef>,
240}
241
242impl Intern for TraitLoc {
243 type ID = TraitId;
244 fn intern(self, db: &impl db::DefDatabase) -> TraitId {
245 db.intern_trait(self)
259 } 246 }
260 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> { 247}
261 db.lookup_intern_trait(self) 248
249impl Lookup for TraitId {
250 type Data = TraitLoc;
251 fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
252 db.lookup_intern_trait(*self)
262 } 253 }
263} 254}
264 255
@@ -492,7 +483,7 @@ impl HasModule for FunctionLoc {
492 match self.container { 483 match self.container {
493 ContainerId::ModuleId(it) => it, 484 ContainerId::ModuleId(it) => it,
494 ContainerId::ImplId(it) => it.lookup(db).container, 485 ContainerId::ImplId(it) => it.lookup(db).container,
495 ContainerId::TraitId(it) => it.module(db), 486 ContainerId::TraitId(it) => it.lookup(db).container,
496 } 487 }
497 } 488 }
498} 489}
@@ -502,7 +493,7 @@ impl HasModule for TypeAliasLoc {
502 match self.container { 493 match self.container {
503 ContainerId::ModuleId(it) => it, 494 ContainerId::ModuleId(it) => it,
504 ContainerId::ImplId(it) => it.lookup(db).container, 495 ContainerId::ImplId(it) => it.lookup(db).container,
505 ContainerId::TraitId(it) => it.module(db), 496 ContainerId::TraitId(it) => it.lookup(db).container,
506 } 497 }
507 } 498 }
508} 499}
@@ -512,7 +503,7 @@ impl HasModule for ConstLoc {
512 match self.container { 503 match self.container {
513 ContainerId::ModuleId(it) => it, 504 ContainerId::ModuleId(it) => it,
514 ContainerId::ImplId(it) => it.lookup(db).container, 505 ContainerId::ImplId(it) => it.lookup(db).container,
515 ContainerId::TraitId(it) => it.module(db), 506 ContainerId::TraitId(it) => it.lookup(db).container,
516 } 507 }
517 } 508 }
518} 509}
@@ -520,9 +511,9 @@ impl HasModule for ConstLoc {
520impl HasModule for AdtId { 511impl HasModule for AdtId {
521 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 512 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
522 match self { 513 match self {
523 AdtId::StructId(it) => it.module(db), 514 AdtId::StructId(it) => it.lookup(db).container,
524 AdtId::UnionId(it) => it.module(db), 515 AdtId::UnionId(it) => it.lookup(db).container,
525 AdtId::EnumId(it) => it.module(db), 516 AdtId::EnumId(it) => it.lookup(db).container,
526 } 517 }
527 } 518 }
528} 519}
@@ -542,10 +533,10 @@ impl HasModule for GenericDefId {
542 match self { 533 match self {
543 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 534 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
544 GenericDefId::AdtId(it) => it.module(db), 535 GenericDefId::AdtId(it) => it.module(db),
545 GenericDefId::TraitId(it) => it.module(db), 536 GenericDefId::TraitId(it) => it.lookup(db).container,
546 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
547 GenericDefId::ImplId(it) => it.lookup(db).container, 538 GenericDefId::ImplId(it) => it.lookup(db).container,
548 GenericDefId::EnumVariantId(it) => it.parent.module(db), 539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
549 GenericDefId::ConstId(it) => it.lookup(db).module(db), 540 GenericDefId::ConstId(it) => it.lookup(db).module(db),
550 } 541 }
551 } 542 }