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.rs75
1 files changed, 31 insertions, 44 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index e73dd5d21..eb9939df7 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, 12 Module, DefDatabase, AstId, FileAstId,
14}; 13};
15 14
16#[derive(Debug, Default)] 15#[derive(Debug, Default)]
@@ -22,7 +21,7 @@ pub struct HirInterner {
22 consts: LocationInterner<ItemLoc<ast::ConstDef>, ConstId>, 21 consts: LocationInterner<ItemLoc<ast::ConstDef>, ConstId>,
23 statics: LocationInterner<ItemLoc<ast::StaticDef>, StaticId>, 22 statics: LocationInterner<ItemLoc<ast::StaticDef>, StaticId>,
24 traits: LocationInterner<ItemLoc<ast::TraitDef>, TraitId>, 23 traits: LocationInterner<ItemLoc<ast::TraitDef>, TraitId>,
25 types: LocationInterner<ItemLoc<ast::TypeAliasDef>, TypeId>, 24 types: LocationInterner<ItemLoc<ast::TypeAliasDef>, TypeAliasId>,
26} 25}
27 26
28impl HirInterner { 27impl HirInterner {
@@ -68,7 +67,7 @@ impl HirFileId {
68 HirFileIdRepr::File(file_id) => file_id, 67 HirFileIdRepr::File(file_id) => file_id,
69 HirFileIdRepr::Macro(macro_call_id) => { 68 HirFileIdRepr::Macro(macro_call_id) => {
70 let loc = macro_call_id.loc(db); 69 let loc = macro_call_id.loc(db);
71 loc.source_item_id.file_id.original_file(db) 70 loc.ast_id.file_id().original_file(db)
72 } 71 }
73 } 72 }
74 } 73 }
@@ -83,7 +82,10 @@ impl HirFileId {
83 } 82 }
84 } 83 }
85 84
86 pub(crate) fn hir_parse(db: &impl DefDatabase, file_id: HirFileId) -> TreeArc<SourceFile> { 85 pub(crate) fn hir_parse_query(
86 db: &impl DefDatabase,
87 file_id: HirFileId,
88 ) -> TreeArc<SourceFile> {
87 match file_id.0 { 89 match file_id.0 {
88 HirFileIdRepr::File(file_id) => db.parse(file_id), 90 HirFileIdRepr::File(file_id) => db.parse(file_id),
89 HirFileIdRepr::Macro(macro_call_id) => { 91 HirFileIdRepr::Macro(macro_call_id) => {
@@ -96,8 +98,7 @@ impl HirFileId {
96 98
97fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option<TreeArc<SourceFile>> { 99fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option<TreeArc<SourceFile>> {
98 let loc = macro_call_id.loc(db); 100 let loc = macro_call_id.loc(db);
99 let syntax = db.file_item(loc.source_item_id); 101 let macro_call = loc.ast_id.to_node(db);
100 let macro_call = ast::MacroCall::cast(&syntax).unwrap();
101 let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?; 102 let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?;
102 103
103 let macro_rules = db.macro_def(loc.def)?; 104 let macro_rules = db.macro_def(loc.def)?;
@@ -124,15 +125,10 @@ impl From<MacroCallId> for HirFileId {
124} 125}
125 126
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
127pub enum MacroDefId { 128pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
128 MacroByExample { source_item_id: SourceItemId },
129}
130 129
131pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { 130pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
132 let syntax_node = match id { 131 let macro_call = id.0.to_node(db);
133 MacroDefId::MacroByExample { source_item_id } => db.file_item(source_item_id),
134 };
135 let macro_call = ast::MacroCall::cast(&syntax_node).unwrap();
136 let arg = macro_call.token_tree()?; 132 let arg = macro_call.token_tree()?;
137 let (tt, _) = mbe::ast_to_token_tree(arg)?; 133 let (tt, _) = mbe::ast_to_token_tree(arg)?;
138 let rules = MacroRules::parse(&tt).ok()?; 134 let rules = MacroRules::parse(&tt).ok()?;
@@ -148,7 +144,7 @@ impl_arena_id!(MacroCallId);
148#[derive(Debug, Clone, PartialEq, Eq, Hash)] 144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
149pub struct MacroCallLoc { 145pub struct MacroCallLoc {
150 pub(crate) def: MacroDefId, 146 pub(crate) def: MacroDefId,
151 pub(crate) source_item_id: SourceItemId, 147 pub(crate) ast_id: AstId<ast::MacroCall>,
152} 148}
153 149
154impl MacroCallId { 150impl MacroCallId {
@@ -158,7 +154,6 @@ impl MacroCallId {
158} 154}
159 155
160impl MacroCallLoc { 156impl MacroCallLoc {
161 #[allow(unused)]
162 pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId { 157 pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId {
163 db.as_ref().macros.loc2id(&self) 158 db.as_ref().macros.loc2id(&self)
164 } 159 }
@@ -167,26 +162,25 @@ impl MacroCallLoc {
167#[derive(Debug)] 162#[derive(Debug)]
168pub struct ItemLoc<N: AstNode> { 163pub struct ItemLoc<N: AstNode> {
169 pub(crate) module: Module, 164 pub(crate) module: Module,
170 raw: SourceItemId, 165 ast_id: AstId<N>,
171 _ty: PhantomData<N>,
172} 166}
173 167
174impl<N: AstNode> PartialEq for ItemLoc<N> { 168impl<N: AstNode> PartialEq for ItemLoc<N> {
175 fn eq(&self, other: &Self) -> bool { 169 fn eq(&self, other: &Self) -> bool {
176 self.module == other.module && self.raw == other.raw 170 self.module == other.module && self.ast_id == other.ast_id
177 } 171 }
178} 172}
179impl<N: AstNode> Eq for ItemLoc<N> {} 173impl<N: AstNode> Eq for ItemLoc<N> {}
180impl<N: AstNode> Hash for ItemLoc<N> { 174impl<N: AstNode> Hash for ItemLoc<N> {
181 fn hash<H: Hasher>(&self, hasher: &mut H) { 175 fn hash<H: Hasher>(&self, hasher: &mut H) {
182 self.module.hash(hasher); 176 self.module.hash(hasher);
183 self.raw.hash(hasher); 177 self.ast_id.hash(hasher);
184 } 178 }
185} 179}
186 180
187impl<N: AstNode> Clone for ItemLoc<N> { 181impl<N: AstNode> Clone for ItemLoc<N> {
188 fn clone(&self) -> ItemLoc<N> { 182 fn clone(&self) -> ItemLoc<N> {
189 ItemLoc { module: self.module, raw: self.raw, _ty: PhantomData } 183 ItemLoc { module: self.module, ast_id: self.ast_id }
190 } 184 }
191} 185}
192 186
@@ -213,26 +207,19 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> {
213pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone { 207pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
214 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>; 208 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
215 fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { 209 fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self {
216 let items = ctx.db.file_items(ctx.file_id); 210 let items = ctx.db.ast_id_map(ctx.file_id);
217 let item_id = items.id_of(ctx.file_id, ast.syntax()); 211 let item_id = items.ast_id(ast);
218 Self::from_source_item_id_unchecked(ctx, item_id) 212 Self::from_ast_id(ctx, item_id)
219 } 213 }
220 fn from_source_item_id_unchecked( 214 fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self {
221 ctx: LocationCtx<&impl DefDatabase>, 215 let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
222 item_id: SourceFileItemId,
223 ) -> Self {
224 let raw = SourceItemId { file_id: ctx.file_id, item_id };
225 let loc = ItemLoc { module: ctx.module, raw, _ty: PhantomData };
226
227 Self::interner(ctx.db.as_ref()).loc2id(&loc) 216 Self::interner(ctx.db.as_ref()).loc2id(&loc)
228 } 217 }
229 fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) { 218 fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) {
230 let int = Self::interner(db.as_ref()); 219 let int = Self::interner(db.as_ref());
231 let loc = int.id2loc(self); 220 let loc = int.id2loc(self);
232 let syntax = db.file_item(loc.raw); 221 let ast = loc.ast_id.to_node(db);
233 let ast = 222 (loc.ast_id.file_id(), ast)
234 N::cast(&syntax).unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", loc.raw)).to_owned();
235 (loc.raw.file_id, ast)
236 } 223 }
237 fn module(self, db: &impl DefDatabase) -> Module { 224 fn module(self, db: &impl DefDatabase) -> Module {
238 let int = Self::interner(db.as_ref()); 225 let int = Self::interner(db.as_ref());
@@ -242,7 +229,7 @@ pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
242} 229}
243 230
244#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
245pub struct FunctionId(RawId); 232pub(crate) struct FunctionId(RawId);
246impl_arena_id!(FunctionId); 233impl_arena_id!(FunctionId);
247impl AstItemDef<ast::FnDef> for FunctionId { 234impl AstItemDef<ast::FnDef> for FunctionId {
248 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::FnDef>, Self> { 235 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::FnDef>, Self> {
@@ -251,7 +238,7 @@ impl AstItemDef<ast::FnDef> for FunctionId {
251} 238}
252 239
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 240#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254pub struct StructId(RawId); 241pub(crate) struct StructId(RawId);
255impl_arena_id!(StructId); 242impl_arena_id!(StructId);
256impl AstItemDef<ast::StructDef> for StructId { 243impl AstItemDef<ast::StructDef> for StructId {
257 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StructDef>, Self> { 244 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StructDef>, Self> {
@@ -260,7 +247,7 @@ impl AstItemDef<ast::StructDef> for StructId {
260} 247}
261 248
262#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 249#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
263pub struct EnumId(RawId); 250pub(crate) struct EnumId(RawId);
264impl_arena_id!(EnumId); 251impl_arena_id!(EnumId);
265impl AstItemDef<ast::EnumDef> for EnumId { 252impl AstItemDef<ast::EnumDef> for EnumId {
266 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::EnumDef>, Self> { 253 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::EnumDef>, Self> {
@@ -269,7 +256,7 @@ impl AstItemDef<ast::EnumDef> for EnumId {
269} 256}
270 257
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 258#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
272pub struct ConstId(RawId); 259pub(crate) struct ConstId(RawId);
273impl_arena_id!(ConstId); 260impl_arena_id!(ConstId);
274impl AstItemDef<ast::ConstDef> for ConstId { 261impl AstItemDef<ast::ConstDef> for ConstId {
275 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::ConstDef>, Self> { 262 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::ConstDef>, Self> {
@@ -278,7 +265,7 @@ impl AstItemDef<ast::ConstDef> for ConstId {
278} 265}
279 266
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 267#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
281pub struct StaticId(RawId); 268pub(crate) struct StaticId(RawId);
282impl_arena_id!(StaticId); 269impl_arena_id!(StaticId);
283impl AstItemDef<ast::StaticDef> for StaticId { 270impl AstItemDef<ast::StaticDef> for StaticId {
284 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StaticDef>, Self> { 271 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StaticDef>, Self> {
@@ -287,7 +274,7 @@ impl AstItemDef<ast::StaticDef> for StaticId {
287} 274}
288 275
289#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 276#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
290pub struct TraitId(RawId); 277pub(crate) struct TraitId(RawId);
291impl_arena_id!(TraitId); 278impl_arena_id!(TraitId);
292impl AstItemDef<ast::TraitDef> for TraitId { 279impl AstItemDef<ast::TraitDef> for TraitId {
293 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TraitDef>, Self> { 280 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TraitDef>, Self> {
@@ -296,9 +283,9 @@ impl AstItemDef<ast::TraitDef> for TraitId {
296} 283}
297 284
298#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 285#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
299pub struct TypeId(RawId); 286pub(crate) struct TypeAliasId(RawId);
300impl_arena_id!(TypeId); 287impl_arena_id!(TypeAliasId);
301impl AstItemDef<ast::TypeAliasDef> for TypeId { 288impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
302 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TypeAliasDef>, Self> { 289 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TypeAliasDef>, Self> {
303 &interner.types 290 &interner.types
304 } 291 }