• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! `AstIdMap` allows to create stable IDs for "large" syntax nodes like items
2 //! and macro calls.
3 //!
4 //! Specifically, it enumerates all items in a file and uses position of a an
5 //! item as an ID. That way, id's don't change unless the set of items itself
6 //! changes.
7 
8 use std::{
9     any::type_name,
10     fmt,
11     hash::{BuildHasher, BuildHasherDefault, Hash, Hasher},
12     marker::PhantomData,
13 };
14 
15 use la_arena::{Arena, Idx};
16 use profile::Count;
17 use rustc_hash::FxHasher;
18 use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
19 
20 /// `AstId` points to an AST node in a specific file.
21 pub struct FileAstId<N: AstNode> {
22     raw: ErasedFileAstId,
23     covariant: PhantomData<fn() -> N>,
24 }
25 
26 impl<N: AstNode> Clone for FileAstId<N> {
clone(&self) -> FileAstId<N>27     fn clone(&self) -> FileAstId<N> {
28         *self
29     }
30 }
31 impl<N: AstNode> Copy for FileAstId<N> {}
32 
33 impl<N: AstNode> PartialEq for FileAstId<N> {
eq(&self, other: &Self) -> bool34     fn eq(&self, other: &Self) -> bool {
35         self.raw == other.raw
36     }
37 }
38 impl<N: AstNode> Eq for FileAstId<N> {}
39 impl<N: AstNode> Hash for FileAstId<N> {
hash<H: Hasher>(&self, hasher: &mut H)40     fn hash<H: Hasher>(&self, hasher: &mut H) {
41         self.raw.hash(hasher);
42     }
43 }
44 
45 impl<N: AstNode> fmt::Debug for FileAstId<N> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result46     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47         write!(f, "FileAstId::<{}>({})", type_name::<N>(), self.raw.into_raw())
48     }
49 }
50 
51 impl<N: AstNode> FileAstId<N> {
52     // Can't make this a From implementation because of coherence
upcast<M: AstNode>(self) -> FileAstId<M> where N: Into<M>,53     pub fn upcast<M: AstNode>(self) -> FileAstId<M>
54     where
55         N: Into<M>,
56     {
57         FileAstId { raw: self.raw, covariant: PhantomData }
58     }
59 }
60 
61 type ErasedFileAstId = Idx<SyntaxNodePtr>;
62 
63 /// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
64 #[derive(Default)]
65 pub struct AstIdMap {
66     /// Maps stable id to unstable ptr.
67     arena: Arena<SyntaxNodePtr>,
68     /// Reverse: map ptr to id.
69     map: hashbrown::HashMap<Idx<SyntaxNodePtr>, (), ()>,
70     _c: Count<Self>,
71 }
72 
73 impl fmt::Debug for AstIdMap {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result74     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75         f.debug_struct("AstIdMap").field("arena", &self.arena).finish()
76     }
77 }
78 
79 impl PartialEq for AstIdMap {
eq(&self, other: &Self) -> bool80     fn eq(&self, other: &Self) -> bool {
81         self.arena == other.arena
82     }
83 }
84 impl Eq for AstIdMap {}
85 
86 impl AstIdMap {
from_source(node: &SyntaxNode) -> AstIdMap87     pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap {
88         assert!(node.parent().is_none());
89         let mut res = AstIdMap::default();
90         // By walking the tree in breadth-first order we make sure that parents
91         // get lower ids then children. That is, adding a new child does not
92         // change parent's id. This means that, say, adding a new function to a
93         // trait does not change ids of top-level items, which helps caching.
94         bdfs(node, |it| {
95             let kind = it.kind();
96             if ast::Item::can_cast(kind)
97                 || ast::BlockExpr::can_cast(kind)
98                 || ast::Variant::can_cast(kind)
99                 || ast::RecordField::can_cast(kind)
100                 || ast::TupleField::can_cast(kind)
101                 || ast::ConstArg::can_cast(kind)
102             {
103                 res.alloc(&it);
104                 true
105             } else {
106                 false
107             }
108         });
109         res.map = hashbrown::HashMap::with_capacity_and_hasher(res.arena.len(), ());
110         for (idx, ptr) in res.arena.iter() {
111             let hash = hash_ptr(ptr);
112             match res.map.raw_entry_mut().from_hash(hash, |idx2| *idx2 == idx) {
113                 hashbrown::hash_map::RawEntryMut::Occupied(_) => unreachable!(),
114                 hashbrown::hash_map::RawEntryMut::Vacant(entry) => {
115                     entry.insert_with_hasher(hash, idx, (), |&idx| hash_ptr(&res.arena[idx]));
116                 }
117             }
118         }
119         res.arena.shrink_to_fit();
120         res
121     }
122 
ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N>123     pub fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
124         let raw = self.erased_ast_id(item.syntax());
125         FileAstId { raw, covariant: PhantomData }
126     }
127 
get<N: AstNode>(&self, id: FileAstId<N>) -> AstPtr<N>128     pub fn get<N: AstNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
129         AstPtr::try_from_raw(self.arena[id.raw].clone()).unwrap()
130     }
131 
erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId132     fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
133         let ptr = SyntaxNodePtr::new(item);
134         let hash = hash_ptr(&ptr);
135         match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
136             Some((&idx, &())) => idx,
137             None => panic!(
138                 "Can't find {:?} in AstIdMap:\n{:?}",
139                 item,
140                 self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
141             ),
142         }
143     }
144 
alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId145     fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
146         self.arena.alloc(SyntaxNodePtr::new(item))
147     }
148 }
149 
hash_ptr(ptr: &SyntaxNodePtr) -> u64150 fn hash_ptr(ptr: &SyntaxNodePtr) -> u64 {
151     let mut hasher = BuildHasherDefault::<FxHasher>::default().build_hasher();
152     ptr.hash(&mut hasher);
153     hasher.finish()
154 }
155 
156 /// Walks the subtree in bdfs order, calling `f` for each node. What is bdfs
157 /// order? It is a mix of breadth-first and depth first orders. Nodes for which
158 /// `f` returns true are visited breadth-first, all the other nodes are explored
159 /// depth-first.
160 ///
161 /// In other words, the size of the bfs queue is bound by the number of "true"
162 /// nodes.
bdfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode) -> bool)163 fn bdfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode) -> bool) {
164     let mut curr_layer = vec![node.clone()];
165     let mut next_layer = vec![];
166     while !curr_layer.is_empty() {
167         curr_layer.drain(..).for_each(|node| {
168             let mut preorder = node.preorder();
169             while let Some(event) = preorder.next() {
170                 match event {
171                     syntax::WalkEvent::Enter(node) => {
172                         if f(node.clone()) {
173                             next_layer.extend(node.children());
174                             preorder.skip_subtree();
175                         }
176                     }
177                     syntax::WalkEvent::Leave(_) => {}
178                 }
179             }
180         });
181         std::mem::swap(&mut curr_layer, &mut next_layer);
182     }
183 }
184