diff options
Diffstat (limited to 'crates/ra_hir_def/src/trace.rs')
-rw-r--r-- | crates/ra_hir_def/src/trace.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs new file mode 100644 index 000000000..2bcd707bc --- /dev/null +++ b/crates/ra_hir_def/src/trace.rs | |||
@@ -0,0 +1,59 @@ | |||
1 | //! Trace is a pretty niche data structure which is used when lowering a CST | ||
2 | //! into HIR. | ||
3 | //! | ||
4 | //! Lowering process calculates two bits of information: | ||
5 | //! * the lowered syntax itself | ||
6 | //! * a mapping between lowered syntax and original syntax | ||
7 | //! | ||
8 | //! Due to the way salsa works, the mapping is usually hot lava, as it contains | ||
9 | //! absolute offsets. The `Trace` structure (inspired, at least in name, by | ||
10 | //! Kotlin's `BindingTrace`) allows use the same code to compute both | ||
11 | //! projections. | ||
12 | use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId}; | ||
13 | |||
14 | pub(crate) struct Trace<ID: ArenaId, T, V> { | ||
15 | arena: Option<Arena<ID, T>>, | ||
16 | map: Option<ArenaMap<ID, V>>, | ||
17 | len: u32, | ||
18 | } | ||
19 | |||
20 | impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> { | ||
21 | pub(crate) fn new() -> Trace<ID, T, V> { | ||
22 | Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 } | ||
23 | } | ||
24 | |||
25 | pub(crate) fn new_for_arena() -> Trace<ID, T, V> { | ||
26 | Trace { arena: Some(Arena::default()), map: None, len: 0 } | ||
27 | } | ||
28 | |||
29 | pub(crate) fn new_for_map() -> Trace<ID, T, V> { | ||
30 | Trace { arena: None, map: Some(ArenaMap::default()), len: 0 } | ||
31 | } | ||
32 | |||
33 | pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> ID { | ||
34 | let id = if let Some(arena) = &mut self.arena { | ||
35 | arena.alloc(data()) | ||
36 | } else { | ||
37 | let id = ID::from_raw(RawId::from(self.len)); | ||
38 | self.len += 1; | ||
39 | id | ||
40 | }; | ||
41 | |||
42 | if let Some(map) = &mut self.map { | ||
43 | map.insert(id, value()); | ||
44 | } | ||
45 | id | ||
46 | } | ||
47 | |||
48 | pub(crate) fn into_arena(mut self) -> Arena<ID, T> { | ||
49 | self.arena.take().unwrap() | ||
50 | } | ||
51 | |||
52 | pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> { | ||
53 | self.map.take().unwrap() | ||
54 | } | ||
55 | |||
56 | pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) { | ||
57 | (self.arena.take().unwrap(), self.map.take().unwrap()) | ||
58 | } | ||
59 | } | ||