diff options
author | Aleksey Kladov <[email protected]> | 2019-11-20 13:03:59 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-20 13:22:58 +0000 |
commit | cebeedc66fc40097eae20bf1767a285d00269966 (patch) | |
tree | e54c306ac0512e3610430574dc8bea39e4b50218 /crates/ra_hir_def/src/lib.rs | |
parent | 06fa3d8389c833b01f482bf35b0f850e627612b9 (diff) |
Next gen IDs for functions
The current system with AstIds has two primaraly drawbacks:
* It is possible to manufacture IDs out of thin air.
For example, it's possible to create IDs for items which are not
considered in CrateDefMap due to cfg. Or it is possible to mixup
structs and unions, because they share ID space.
* Getting the ID of a parent requires a secondary index.
Instead, the plan is to pursue the more traditional approach, where
each items stores the id of the parent declaration. This makes
`FromSource` more awkward, but also more correct: now, to get from an
AST to HIR, we first do this recursively for the parent item, and the
just search the children of the parent for the matching def
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 | } | ||