diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 199 |
1 files changed, 101 insertions, 98 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 569da4f28..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)] |
41 | mod marks; | 41 | mod marks; |
42 | 42 | ||
43 | use std::hash::{Hash, Hasher}; | 43 | use std::hash::Hash; |
44 | 44 | ||
45 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; | 45 | use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; |
46 | use ra_arena::{impl_arena_id, RawId}; | 46 | use ra_arena::{impl_arena_id, RawId}; |
47 | use ra_db::{impl_intern_key, salsa, CrateId}; | 47 | use ra_db::{impl_intern_key, salsa, CrateId}; |
48 | use ra_syntax::{ast, AstNode}; | 48 | use ra_syntax::ast; |
49 | 49 | ||
50 | use crate::{builtin_type::BuiltinType, db::InternDatabase}; | 50 | use crate::builtin_type::BuiltinType; |
51 | 51 | ||
52 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 52 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
53 | pub struct LocalImportId(RawId); | 53 | pub struct LocalImportId(RawId); |
@@ -65,63 +65,6 @@ pub struct ModuleId { | |||
65 | pub struct LocalModuleId(RawId); | 65 | pub struct LocalModuleId(RawId); |
66 | impl_arena_id!(LocalModuleId); | 66 | impl_arena_id!(LocalModuleId); |
67 | 67 | ||
68 | #[derive(Debug)] | ||
69 | pub struct ItemLoc<N: AstNode> { | ||
70 | pub(crate) module: ModuleId, | ||
71 | ast_id: AstId<N>, | ||
72 | } | ||
73 | |||
74 | impl<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 | } | ||
79 | impl<N: AstNode> Eq for ItemLoc<N> {} | ||
80 | impl<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 | |||
87 | impl<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)] | ||
94 | pub struct LocationCtx<DB> { | ||
95 | db: DB, | ||
96 | module: ModuleId, | ||
97 | file_id: HirFileId, | ||
98 | } | ||
99 | |||
100 | impl<'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 | |||
106 | pub 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)] |
126 | pub struct FunctionId(salsa::InternId); | 69 | pub struct FunctionId(salsa::InternId); |
127 | impl_intern_key!(FunctionId); | 70 | impl_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)] |
150 | pub struct StructId(salsa::InternId); | 93 | pub struct StructId(salsa::InternId); |
151 | impl_intern_key!(StructId); | 94 | impl_intern_key!(StructId); |
152 | impl 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) | 97 | pub struct StructLoc { |
98 | pub container: ModuleId, | ||
99 | pub ast_id: AstId<ast::StructDef>, | ||
100 | } | ||
101 | |||
102 | impl 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 | |
109 | impl 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)] |
162 | pub struct UnionId(salsa::InternId); | 117 | pub struct UnionId(salsa::InternId); |
163 | impl_intern_key!(UnionId); | 118 | impl_intern_key!(UnionId); |
164 | impl 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) | 121 | pub struct UnionLoc { |
122 | pub container: ModuleId, | ||
123 | pub ast_id: AstId<ast::UnionDef>, | ||
124 | } | ||
125 | |||
126 | impl 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 | |
133 | impl 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)] |
174 | pub struct EnumId(salsa::InternId); | 141 | pub struct EnumId(salsa::InternId); |
175 | impl_intern_key!(EnumId); | 142 | impl_intern_key!(EnumId); |
176 | impl 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) | 145 | pub struct EnumLoc { |
146 | pub container: ModuleId, | ||
147 | pub ast_id: AstId<ast::EnumDef>, | ||
148 | } | ||
149 | |||
150 | impl 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 | |
157 | impl 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)] |
254 | pub struct TraitId(salsa::InternId); | 233 | pub struct TraitId(salsa::InternId); |
255 | impl_intern_key!(TraitId); | 234 | impl_intern_key!(TraitId); |
256 | impl 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) | 237 | pub struct TraitLoc { |
238 | pub container: ModuleId, | ||
239 | pub ast_id: AstId<ast::TraitDef>, | ||
240 | } | ||
241 | |||
242 | impl 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 | |
249 | impl 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 | ||
@@ -289,12 +280,24 @@ impl Lookup for TypeAliasId { | |||
289 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 280 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
290 | pub struct ImplId(salsa::InternId); | 281 | pub struct ImplId(salsa::InternId); |
291 | impl_intern_key!(ImplId); | 282 | impl_intern_key!(ImplId); |
292 | impl AstItemDef<ast::ImplBlock> for ImplId { | 283 | |
293 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ImplBlock>) -> Self { | 284 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
294 | db.intern_impl(loc) | 285 | pub struct ImplLoc { |
286 | pub container: ModuleId, | ||
287 | pub ast_id: AstId<ast::ImplBlock>, | ||
288 | } | ||
289 | |||
290 | impl Intern for ImplLoc { | ||
291 | type ID = ImplId; | ||
292 | fn intern(self, db: &impl db::DefDatabase) -> ImplId { | ||
293 | db.intern_impl(self) | ||
295 | } | 294 | } |
296 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ImplBlock> { | 295 | } |
297 | db.lookup_intern_impl(self) | 296 | |
297 | impl Lookup for ImplId { | ||
298 | type Data = ImplLoc; | ||
299 | fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { | ||
300 | db.lookup_intern_impl(*self) | ||
298 | } | 301 | } |
299 | } | 302 | } |
300 | 303 | ||
@@ -479,8 +482,8 @@ impl HasModule for FunctionLoc { | |||
479 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 482 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
480 | match self.container { | 483 | match self.container { |
481 | ContainerId::ModuleId(it) => it, | 484 | ContainerId::ModuleId(it) => it, |
482 | ContainerId::ImplId(it) => it.module(db), | 485 | ContainerId::ImplId(it) => it.lookup(db).container, |
483 | ContainerId::TraitId(it) => it.module(db), | 486 | ContainerId::TraitId(it) => it.lookup(db).container, |
484 | } | 487 | } |
485 | } | 488 | } |
486 | } | 489 | } |
@@ -489,8 +492,8 @@ impl HasModule for TypeAliasLoc { | |||
489 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 492 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
490 | match self.container { | 493 | match self.container { |
491 | ContainerId::ModuleId(it) => it, | 494 | ContainerId::ModuleId(it) => it, |
492 | ContainerId::ImplId(it) => it.module(db), | 495 | ContainerId::ImplId(it) => it.lookup(db).container, |
493 | ContainerId::TraitId(it) => it.module(db), | 496 | ContainerId::TraitId(it) => it.lookup(db).container, |
494 | } | 497 | } |
495 | } | 498 | } |
496 | } | 499 | } |
@@ -499,8 +502,8 @@ impl HasModule for ConstLoc { | |||
499 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 502 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
500 | match self.container { | 503 | match self.container { |
501 | ContainerId::ModuleId(it) => it, | 504 | ContainerId::ModuleId(it) => it, |
502 | ContainerId::ImplId(it) => it.module(db), | 505 | ContainerId::ImplId(it) => it.lookup(db).container, |
503 | ContainerId::TraitId(it) => it.module(db), | 506 | ContainerId::TraitId(it) => it.lookup(db).container, |
504 | } | 507 | } |
505 | } | 508 | } |
506 | } | 509 | } |
@@ -508,9 +511,9 @@ impl HasModule for ConstLoc { | |||
508 | impl HasModule for AdtId { | 511 | impl HasModule for AdtId { |
509 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 512 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
510 | match self { | 513 | match self { |
511 | AdtId::StructId(it) => it.module(db), | 514 | AdtId::StructId(it) => it.lookup(db).container, |
512 | AdtId::UnionId(it) => it.module(db), | 515 | AdtId::UnionId(it) => it.lookup(db).container, |
513 | AdtId::EnumId(it) => it.module(db), | 516 | AdtId::EnumId(it) => it.lookup(db).container, |
514 | } | 517 | } |
515 | } | 518 | } |
516 | } | 519 | } |
@@ -530,10 +533,10 @@ impl HasModule for GenericDefId { | |||
530 | match self { | 533 | match self { |
531 | GenericDefId::FunctionId(it) => it.lookup(db).module(db), | 534 | GenericDefId::FunctionId(it) => it.lookup(db).module(db), |
532 | GenericDefId::AdtId(it) => it.module(db), | 535 | GenericDefId::AdtId(it) => it.module(db), |
533 | GenericDefId::TraitId(it) => it.module(db), | 536 | GenericDefId::TraitId(it) => it.lookup(db).container, |
534 | GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), | 537 | GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), |
535 | GenericDefId::ImplId(it) => it.module(db), | 538 | GenericDefId::ImplId(it) => it.lookup(db).container, |
536 | GenericDefId::EnumVariantId(it) => it.parent.module(db), | 539 | GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, |
537 | GenericDefId::ConstId(it) => it.lookup(db).module(db), | 540 | GenericDefId::ConstId(it) => it.lookup(db).module(db), |
538 | } | 541 | } |
539 | } | 542 | } |