diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-09 21:04:42 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-09 21:04:42 +0100 |
commit | b863272899a1bae63c7d9411d0ebff74652bae8e (patch) | |
tree | f5b95ae6af1ee37545eff70813c8940979e99daf /crates/ra_hir/src/ids.rs | |
parent | cc284dad30de4990516eeccf60f24e613fd78a2a (diff) | |
parent | 88189c428242d2d65b749d0980eb447e72766e77 (diff) |
Merge #1126
1126: Swithc to native salsa interning r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r-- | crates/ra_hir/src/ids.rs | 145 |
1 files changed, 77 insertions, 68 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index eb9939df7..141c9072f 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -3,40 +3,14 @@ use std::{ | |||
3 | sync::Arc, | 3 | sync::Arc, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use ra_db::{LocationInterner, FileId}; | 6 | use ra_db::{FileId, salsa}; |
7 | use ra_syntax::{TreeArc, SourceFile, AstNode, ast}; | 7 | use ra_syntax::{TreeArc, SourceFile, AstNode, ast}; |
8 | use ra_arena::{RawId, ArenaId, impl_arena_id}; | ||
9 | use mbe::MacroRules; | 8 | use mbe::MacroRules; |
10 | 9 | ||
11 | use crate::{ | 10 | use crate::{ |
12 | Module, DefDatabase, AstId, FileAstId, | 11 | Module, DefDatabase, AstId, FileAstId, |
13 | }; | 12 | }; |
14 | 13 | ||
15 | #[derive(Debug, Default)] | ||
16 | pub struct HirInterner { | ||
17 | macros: LocationInterner<MacroCallLoc, MacroCallId>, | ||
18 | fns: LocationInterner<ItemLoc<ast::FnDef>, FunctionId>, | ||
19 | structs: LocationInterner<ItemLoc<ast::StructDef>, StructId>, | ||
20 | enums: LocationInterner<ItemLoc<ast::EnumDef>, EnumId>, | ||
21 | consts: LocationInterner<ItemLoc<ast::ConstDef>, ConstId>, | ||
22 | statics: LocationInterner<ItemLoc<ast::StaticDef>, StaticId>, | ||
23 | traits: LocationInterner<ItemLoc<ast::TraitDef>, TraitId>, | ||
24 | types: LocationInterner<ItemLoc<ast::TypeAliasDef>, TypeAliasId>, | ||
25 | } | ||
26 | |||
27 | impl HirInterner { | ||
28 | pub fn len(&self) -> usize { | ||
29 | self.macros.len() | ||
30 | + self.fns.len() | ||
31 | + self.structs.len() | ||
32 | + self.enums.len() | ||
33 | + self.consts.len() | ||
34 | + self.statics.len() | ||
35 | + self.traits.len() | ||
36 | + self.types.len() | ||
37 | } | ||
38 | } | ||
39 | |||
40 | /// hir makes heavy use of ids: integer (u32) handlers to various things. You | 14 | /// hir makes heavy use of ids: integer (u32) handlers to various things. You |
41 | /// can think of id as a pointer (but without a lifetime) or a file descriptor | 15 | /// can think of id as a pointer (but without a lifetime) or a file descriptor |
42 | /// (but for hir objects). | 16 | /// (but for hir objects). |
@@ -135,11 +109,24 @@ pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<A | |||
135 | Some(Arc::new(rules)) | 109 | Some(Arc::new(rules)) |
136 | } | 110 | } |
137 | 111 | ||
112 | macro_rules! impl_intern_key { | ||
113 | ($name:ident) => { | ||
114 | impl salsa::InternKey for $name { | ||
115 | fn from_intern_id(v: salsa::InternId) -> Self { | ||
116 | $name(v) | ||
117 | } | ||
118 | fn as_intern_id(&self) -> salsa::InternId { | ||
119 | self.0 | ||
120 | } | ||
121 | } | ||
122 | }; | ||
123 | } | ||
124 | |||
138 | /// `MacroCallId` identifies a particular macro invocation, like | 125 | /// `MacroCallId` identifies a particular macro invocation, like |
139 | /// `println!("Hello, {}", world)`. | 126 | /// `println!("Hello, {}", world)`. |
140 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 127 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
141 | pub struct MacroCallId(RawId); | 128 | pub struct MacroCallId(salsa::InternId); |
142 | impl_arena_id!(MacroCallId); | 129 | impl_intern_key!(MacroCallId); |
143 | 130 | ||
144 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 131 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
145 | pub struct MacroCallLoc { | 132 | pub struct MacroCallLoc { |
@@ -148,14 +135,14 @@ pub struct MacroCallLoc { | |||
148 | } | 135 | } |
149 | 136 | ||
150 | impl MacroCallId { | 137 | impl MacroCallId { |
151 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> MacroCallLoc { | 138 | pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc { |
152 | db.as_ref().macros.id2loc(self) | 139 | db.lookup_intern_macro(self) |
153 | } | 140 | } |
154 | } | 141 | } |
155 | 142 | ||
156 | impl MacroCallLoc { | 143 | impl MacroCallLoc { |
157 | pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId { | 144 | pub(crate) fn id(self, db: &impl DefDatabase) -> MacroCallId { |
158 | db.as_ref().macros.loc2id(&self) | 145 | db.intern_macro(self) |
159 | } | 146 | } |
160 | } | 147 | } |
161 | 148 | ||
@@ -204,8 +191,10 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> { | |||
204 | } | 191 | } |
205 | } | 192 | } |
206 | 193 | ||
207 | pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone { | 194 | pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone { |
208 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>; | 195 | fn intern(db: &impl DefDatabase, loc: ItemLoc<N>) -> Self; |
196 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<N>; | ||
197 | |||
209 | fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { | 198 | fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { |
210 | let items = ctx.db.ast_id_map(ctx.file_id); | 199 | let items = ctx.db.ast_id_map(ctx.file_id); |
211 | let item_id = items.ast_id(ast); | 200 | let item_id = items.ast_id(ast); |
@@ -213,80 +202,100 @@ pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone { | |||
213 | } | 202 | } |
214 | fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self { | 203 | fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self { |
215 | let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; | 204 | let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; |
216 | Self::interner(ctx.db.as_ref()).loc2id(&loc) | 205 | Self::intern(ctx.db, loc) |
217 | } | 206 | } |
218 | fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) { | 207 | fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) { |
219 | let int = Self::interner(db.as_ref()); | 208 | let loc = self.lookup_intern(db); |
220 | let loc = int.id2loc(self); | ||
221 | let ast = loc.ast_id.to_node(db); | 209 | let ast = loc.ast_id.to_node(db); |
222 | (loc.ast_id.file_id(), ast) | 210 | (loc.ast_id.file_id(), ast) |
223 | } | 211 | } |
224 | fn module(self, db: &impl DefDatabase) -> Module { | 212 | fn module(self, db: &impl DefDatabase) -> Module { |
225 | let int = Self::interner(db.as_ref()); | 213 | let loc = self.lookup_intern(db); |
226 | let loc = int.id2loc(self); | ||
227 | loc.module | 214 | loc.module |
228 | } | 215 | } |
229 | } | 216 | } |
230 | 217 | ||
231 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 218 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
232 | pub(crate) struct FunctionId(RawId); | 219 | pub struct FunctionId(salsa::InternId); |
233 | impl_arena_id!(FunctionId); | 220 | impl_intern_key!(FunctionId); |
221 | |||
234 | impl AstItemDef<ast::FnDef> for FunctionId { | 222 | impl AstItemDef<ast::FnDef> for FunctionId { |
235 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::FnDef>, Self> { | 223 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::FnDef>) -> Self { |
236 | &interner.fns | 224 | db.intern_function(loc) |
225 | } | ||
226 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::FnDef> { | ||
227 | db.lookup_intern_function(self) | ||
237 | } | 228 | } |
238 | } | 229 | } |
239 | 230 | ||
240 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 231 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
241 | pub(crate) struct StructId(RawId); | 232 | pub struct StructId(salsa::InternId); |
242 | impl_arena_id!(StructId); | 233 | impl_intern_key!(StructId); |
243 | impl AstItemDef<ast::StructDef> for StructId { | 234 | impl AstItemDef<ast::StructDef> for StructId { |
244 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StructDef>, Self> { | 235 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::StructDef>) -> Self { |
245 | &interner.structs | 236 | db.intern_struct(loc) |
237 | } | ||
238 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::StructDef> { | ||
239 | db.lookup_intern_struct(self) | ||
246 | } | 240 | } |
247 | } | 241 | } |
248 | 242 | ||
249 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 243 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
250 | pub(crate) struct EnumId(RawId); | 244 | pub struct EnumId(salsa::InternId); |
251 | impl_arena_id!(EnumId); | 245 | impl_intern_key!(EnumId); |
252 | impl AstItemDef<ast::EnumDef> for EnumId { | 246 | impl AstItemDef<ast::EnumDef> for EnumId { |
253 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::EnumDef>, Self> { | 247 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::EnumDef>) -> Self { |
254 | &interner.enums | 248 | db.intern_enum(loc) |
249 | } | ||
250 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::EnumDef> { | ||
251 | db.lookup_intern_enum(self) | ||
255 | } | 252 | } |
256 | } | 253 | } |
257 | 254 | ||
258 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 255 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
259 | pub(crate) struct ConstId(RawId); | 256 | pub struct ConstId(salsa::InternId); |
260 | impl_arena_id!(ConstId); | 257 | impl_intern_key!(ConstId); |
261 | impl AstItemDef<ast::ConstDef> for ConstId { | 258 | impl AstItemDef<ast::ConstDef> for ConstId { |
262 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::ConstDef>, Self> { | 259 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::ConstDef>) -> Self { |
263 | &interner.consts | 260 | db.intern_const(loc) |
261 | } | ||
262 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::ConstDef> { | ||
263 | db.lookup_intern_const(self) | ||
264 | } | 264 | } |
265 | } | 265 | } |
266 | 266 | ||
267 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 267 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
268 | pub(crate) struct StaticId(RawId); | 268 | pub struct StaticId(salsa::InternId); |
269 | impl_arena_id!(StaticId); | 269 | impl_intern_key!(StaticId); |
270 | impl AstItemDef<ast::StaticDef> for StaticId { | 270 | impl AstItemDef<ast::StaticDef> for StaticId { |
271 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StaticDef>, Self> { | 271 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::StaticDef>) -> Self { |
272 | &interner.statics | 272 | db.intern_static(loc) |
273 | } | ||
274 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::StaticDef> { | ||
275 | db.lookup_intern_static(self) | ||
273 | } | 276 | } |
274 | } | 277 | } |
275 | 278 | ||
276 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 279 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
277 | pub(crate) struct TraitId(RawId); | 280 | pub struct TraitId(salsa::InternId); |
278 | impl_arena_id!(TraitId); | 281 | impl_intern_key!(TraitId); |
279 | impl AstItemDef<ast::TraitDef> for TraitId { | 282 | impl AstItemDef<ast::TraitDef> for TraitId { |
280 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TraitDef>, Self> { | 283 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::TraitDef>) -> Self { |
281 | &interner.traits | 284 | db.intern_trait(loc) |
285 | } | ||
286 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::TraitDef> { | ||
287 | db.lookup_intern_trait(self) | ||
282 | } | 288 | } |
283 | } | 289 | } |
284 | 290 | ||
285 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 291 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
286 | pub(crate) struct TypeAliasId(RawId); | 292 | pub struct TypeAliasId(salsa::InternId); |
287 | impl_arena_id!(TypeAliasId); | 293 | impl_intern_key!(TypeAliasId); |
288 | impl AstItemDef<ast::TypeAliasDef> for TypeAliasId { | 294 | impl AstItemDef<ast::TypeAliasDef> for TypeAliasId { |
289 | fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TypeAliasDef>, Self> { | 295 | fn intern(db: &impl DefDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self { |
290 | &interner.types | 296 | db.intern_type_alias(loc) |
297 | } | ||
298 | fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<ast::TypeAliasDef> { | ||
299 | db.lookup_intern_type_alias(self) | ||
291 | } | 300 | } |
292 | } | 301 | } |