aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ids.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r--crates/ra_hir/src/ids.rs32
1 files changed, 12 insertions, 20 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index b503e0ee5..c2df5ce00 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -1,5 +1,4 @@
1use std::{ 1use std::{
2 marker::PhantomData,
3 hash::{Hash, Hasher}, 2 hash::{Hash, Hasher},
4 sync::Arc, 3 sync::Arc,
5}; 4};
@@ -10,7 +9,7 @@ use ra_arena::{RawId, ArenaId, impl_arena_id};
10use mbe::MacroRules; 9use mbe::MacroRules;
11 10
12use crate::{ 11use crate::{
13 Module, DefDatabase, SourceItemId, SourceFileItemId, AstId, 12 Module, DefDatabase, AstId, FileAstId,
14}; 13};
15 14
16#[derive(Debug, Default)] 15#[derive(Debug, Default)]
@@ -123,6 +122,7 @@ impl From<MacroCallId> for HirFileId {
123} 122}
124 123
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 124#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
125
126pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); 126pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
127 127
128pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { 128pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
@@ -161,26 +161,25 @@ impl MacroCallLoc {
161#[derive(Debug)] 161#[derive(Debug)]
162pub struct ItemLoc<N: AstNode> { 162pub struct ItemLoc<N: AstNode> {
163 pub(crate) module: Module, 163 pub(crate) module: Module,
164 raw: SourceItemId, 164 ast_id: AstId<N>,
165 _ty: PhantomData<N>,
166} 165}
167 166
168impl<N: AstNode> PartialEq for ItemLoc<N> { 167impl<N: AstNode> PartialEq for ItemLoc<N> {
169 fn eq(&self, other: &Self) -> bool { 168 fn eq(&self, other: &Self) -> bool {
170 self.module == other.module && self.raw == other.raw 169 self.module == other.module && self.ast_id == other.ast_id
171 } 170 }
172} 171}
173impl<N: AstNode> Eq for ItemLoc<N> {} 172impl<N: AstNode> Eq for ItemLoc<N> {}
174impl<N: AstNode> Hash for ItemLoc<N> { 173impl<N: AstNode> Hash for ItemLoc<N> {
175 fn hash<H: Hasher>(&self, hasher: &mut H) { 174 fn hash<H: Hasher>(&self, hasher: &mut H) {
176 self.module.hash(hasher); 175 self.module.hash(hasher);
177 self.raw.hash(hasher); 176 self.ast_id.hash(hasher);
178 } 177 }
179} 178}
180 179
181impl<N: AstNode> Clone for ItemLoc<N> { 180impl<N: AstNode> Clone for ItemLoc<N> {
182 fn clone(&self) -> ItemLoc<N> { 181 fn clone(&self) -> ItemLoc<N> {
183 ItemLoc { module: self.module, raw: self.raw, _ty: PhantomData } 182 ItemLoc { module: self.module, ast_id: self.ast_id }
184 } 183 }
185} 184}
186 185
@@ -208,25 +207,18 @@ pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
208 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>; 207 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
209 fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { 208 fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self {
210 let items = ctx.db.file_items(ctx.file_id); 209 let items = ctx.db.file_items(ctx.file_id);
211 let item_id = items.id_of(ctx.file_id, ast.syntax()); 210 let item_id = items.ast_id(ast);
212 Self::from_source_item_id_unchecked(ctx, item_id) 211 Self::from_ast_id(ctx, item_id)
213 } 212 }
214 fn from_source_item_id_unchecked( 213 fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self {
215 ctx: LocationCtx<&impl DefDatabase>, 214 let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
216 item_id: SourceFileItemId,
217 ) -> Self {
218 let raw = SourceItemId { file_id: ctx.file_id, item_id };
219 let loc = ItemLoc { module: ctx.module, raw, _ty: PhantomData };
220
221 Self::interner(ctx.db.as_ref()).loc2id(&loc) 215 Self::interner(ctx.db.as_ref()).loc2id(&loc)
222 } 216 }
223 fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) { 217 fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) {
224 let int = Self::interner(db.as_ref()); 218 let int = Self::interner(db.as_ref());
225 let loc = int.id2loc(self); 219 let loc = int.id2loc(self);
226 let syntax = db.file_item(loc.raw); 220 let ast = loc.ast_id.to_node(db);
227 let ast = 221 (loc.ast_id.file_id(), ast)
228 N::cast(&syntax).unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", loc.raw)).to_owned();
229 (loc.raw.file_id, ast)
230 } 222 }
231 fn module(self, db: &impl DefDatabase) -> Module { 223 fn module(self, db: &impl DefDatabase) -> Module {
232 let int = Self::interner(db.as_ref()); 224 let int = Self::interner(db.as_ref());