diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 65 |
1 files changed, 4 insertions, 61 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index c9e4e6a0f..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); |