aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/keys.rs4
-rw-r--r--crates/ra_hir_def/src/lib.rs65
2 files changed, 6 insertions, 63 deletions
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
index ada145379..4611c6e38 100644
--- a/crates/ra_hir_def/src/keys.rs
+++ b/crates/ra_hir_def/src/keys.rs
@@ -8,8 +8,8 @@ use rustc_hash::FxHashMap;
8 8
9use crate::{ 9use crate::{
10 dyn_map::{DynMap, Policy}, 10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, 11 ConstId, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId,
12 TypeAliasId, TypeParamId, EnumId, UnionId, 12 TypeAliasId, TypeParamId, UnionId,
13}; 13};
14 14
15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; 15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
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)]
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);