diff options
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r-- | crates/ra_hir/src/ids.rs | 75 |
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 @@ | |||
1 | use std::{ | 1 | use 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}; | |||
10 | use mbe::MacroRules; | 9 | use mbe::MacroRules; |
11 | 10 | ||
12 | use crate::{ | 11 | use 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 | ||
28 | impl HirInterner { | 27 | impl 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 | ||
97 | fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option<TreeArc<SourceFile>> { | 99 | fn 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)] |
127 | pub enum MacroDefId { | 128 | pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); |
128 | MacroByExample { source_item_id: SourceItemId }, | ||
129 | } | ||
130 | 129 | ||
131 | pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { | 130 | pub(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)] |
149 | pub struct MacroCallLoc { | 145 | pub 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 | ||
154 | impl MacroCallId { | 150 | impl MacroCallId { |
@@ -158,7 +154,6 @@ impl MacroCallId { | |||
158 | } | 154 | } |
159 | 155 | ||
160 | impl MacroCallLoc { | 156 | impl 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)] |
168 | pub struct ItemLoc<N: AstNode> { | 163 | pub 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 | ||
174 | impl<N: AstNode> PartialEq for ItemLoc<N> { | 168 | impl<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 | } |
179 | impl<N: AstNode> Eq for ItemLoc<N> {} | 173 | impl<N: AstNode> Eq for ItemLoc<N> {} |
180 | impl<N: AstNode> Hash for ItemLoc<N> { | 174 | impl<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 | ||
187 | impl<N: AstNode> Clone for ItemLoc<N> { | 181 | impl<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> { | |||
213 | pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone { | 207 | pub(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)] |
245 | pub struct FunctionId(RawId); | 232 | pub(crate) struct FunctionId(RawId); |
246 | impl_arena_id!(FunctionId); | 233 | impl_arena_id!(FunctionId); |
247 | impl AstItemDef<ast::FnDef> for FunctionId { | 234 | impl 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)] |
254 | pub struct StructId(RawId); | 241 | pub(crate) struct StructId(RawId); |
255 | impl_arena_id!(StructId); | 242 | impl_arena_id!(StructId); |
256 | impl AstItemDef<ast::StructDef> for StructId { | 243 | impl 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)] |
263 | pub struct EnumId(RawId); | 250 | pub(crate) struct EnumId(RawId); |
264 | impl_arena_id!(EnumId); | 251 | impl_arena_id!(EnumId); |
265 | impl AstItemDef<ast::EnumDef> for EnumId { | 252 | impl 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)] |
272 | pub struct ConstId(RawId); | 259 | pub(crate) struct ConstId(RawId); |
273 | impl_arena_id!(ConstId); | 260 | impl_arena_id!(ConstId); |
274 | impl AstItemDef<ast::ConstDef> for ConstId { | 261 | impl 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)] |
281 | pub struct StaticId(RawId); | 268 | pub(crate) struct StaticId(RawId); |
282 | impl_arena_id!(StaticId); | 269 | impl_arena_id!(StaticId); |
283 | impl AstItemDef<ast::StaticDef> for StaticId { | 270 | impl 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)] |
290 | pub struct TraitId(RawId); | 277 | pub(crate) struct TraitId(RawId); |
291 | impl_arena_id!(TraitId); | 278 | impl_arena_id!(TraitId); |
292 | impl AstItemDef<ast::TraitDef> for TraitId { | 279 | impl 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)] |
299 | pub struct TypeId(RawId); | 286 | pub(crate) struct TypeAliasId(RawId); |
300 | impl_arena_id!(TypeId); | 287 | impl_arena_id!(TypeAliasId); |
301 | impl AstItemDef<ast::TypeAliasDef> for TypeId { | 288 | impl 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 | } |