2019-10-29 03:15:51 -05:00
|
|
|
//! `AstIdMap` allows to create stable IDs for "large" syntax nodes like items
|
|
|
|
//! and macro calls.
|
|
|
|
//!
|
|
|
|
//! Specifically, it enumerates all items in a file and uses position of a an
|
|
|
|
//! item as an ID. That way, id's don't change unless the set of items itself
|
|
|
|
//! changes.
|
|
|
|
|
|
|
|
use std::{
|
2020-06-22 08:07:06 -05:00
|
|
|
any::type_name,
|
|
|
|
fmt,
|
2019-10-29 03:15:51 -05:00
|
|
|
hash::{Hash, Hasher},
|
|
|
|
marker::PhantomData,
|
|
|
|
};
|
|
|
|
|
2021-01-14 09:47:42 -06:00
|
|
|
use la_arena::{Arena, Idx};
|
2021-01-27 03:16:24 -06:00
|
|
|
use profile::Count;
|
2021-11-27 11:50:07 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-01-19 12:49:19 -06:00
|
|
|
use syntax::{ast, match_ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
|
2019-10-29 03:15:51 -05:00
|
|
|
|
|
|
|
/// `AstId` points to an AST node in a specific file.
|
|
|
|
pub struct FileAstId<N: AstNode> {
|
|
|
|
raw: ErasedFileAstId,
|
|
|
|
_ty: PhantomData<fn() -> N>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: AstNode> Clone for FileAstId<N> {
|
|
|
|
fn clone(&self) -> FileAstId<N> {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<N: AstNode> Copy for FileAstId<N> {}
|
|
|
|
|
|
|
|
impl<N: AstNode> PartialEq for FileAstId<N> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.raw == other.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<N: AstNode> Eq for FileAstId<N> {}
|
|
|
|
impl<N: AstNode> Hash for FileAstId<N> {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
self.raw.hash(hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 08:07:06 -05:00
|
|
|
impl<N: AstNode> fmt::Debug for FileAstId<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "FileAstId::<{}>({})", type_name::<N>(), self.raw.into_raw())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
impl<N: AstNode> FileAstId<N> {
|
|
|
|
// Can't make this a From implementation because of coherence
|
|
|
|
pub fn upcast<M: AstNode>(self) -> FileAstId<M>
|
|
|
|
where
|
2020-06-22 08:07:06 -05:00
|
|
|
N: Into<M>,
|
2019-12-05 08:10:33 -06:00
|
|
|
{
|
|
|
|
FileAstId { raw: self.raw, _ty: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
type ErasedFileAstId = Idx<SyntaxNodePtr>;
|
2019-10-29 03:15:51 -05:00
|
|
|
|
|
|
|
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Default)]
|
|
|
|
pub struct AstIdMap {
|
2020-03-19 10:00:11 -05:00
|
|
|
arena: Arena<SyntaxNodePtr>,
|
2021-11-27 11:50:07 -06:00
|
|
|
map: FxHashMap<SyntaxNodePtr, ErasedFileAstId>,
|
2021-01-27 03:16:24 -06:00
|
|
|
_c: Count<Self>,
|
2019-10-29 03:15:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AstIdMap {
|
2019-10-29 08:08:06 -05:00
|
|
|
pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap {
|
2019-10-29 03:15:51 -05:00
|
|
|
assert!(node.parent().is_none());
|
2021-01-27 03:16:24 -06:00
|
|
|
let mut res = AstIdMap::default();
|
2019-12-05 08:10:33 -06:00
|
|
|
// By walking the tree in breadth-first order we make sure that parents
|
2019-10-29 03:15:51 -05:00
|
|
|
// get lower ids then children. That is, adding a new child does not
|
|
|
|
// change parent's id. This means that, say, adding a new function to a
|
|
|
|
// trait does not change ids of top-level items, which helps caching.
|
2021-01-19 12:49:19 -06:00
|
|
|
bdfs(node, |it| {
|
|
|
|
match_ast! {
|
|
|
|
match it {
|
|
|
|
ast::Item(module_item) => {
|
|
|
|
res.alloc(module_item.syntax());
|
|
|
|
true
|
|
|
|
},
|
|
|
|
ast::BlockExpr(block) => {
|
|
|
|
res.alloc(block.syntax());
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
2019-10-29 03:15:51 -05:00
|
|
|
}
|
|
|
|
});
|
2021-11-27 11:50:07 -06:00
|
|
|
res.map.extend(res.arena.iter().map(|(idx, ptr)| (ptr.clone(), idx)));
|
2019-10-29 03:15:51 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
|
2019-10-29 07:20:08 -05:00
|
|
|
let raw = self.erased_ast_id(item.syntax());
|
|
|
|
FileAstId { raw, _ty: PhantomData }
|
|
|
|
}
|
2021-11-27 11:50:07 -06:00
|
|
|
|
2019-10-29 07:20:08 -05:00
|
|
|
fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
|
|
|
|
let ptr = SyntaxNodePtr::new(item);
|
2021-11-27 11:50:07 -06:00
|
|
|
*self.map.get(&ptr).unwrap_or_else(|| {
|
|
|
|
panic!(
|
2019-10-29 03:15:51 -05:00
|
|
|
"Can't find {:?} in AstIdMap:\n{:?}",
|
2019-10-29 07:20:08 -05:00
|
|
|
item,
|
2019-10-29 03:15:51 -05:00
|
|
|
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
|
2021-11-27 11:50:07 -06:00
|
|
|
)
|
|
|
|
})
|
2019-10-29 03:15:51 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 08:07:06 -05:00
|
|
|
pub fn get<N: AstNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
|
2020-04-10 17:27:00 -05:00
|
|
|
self.arena[id.raw].clone().cast::<N>().unwrap()
|
2019-10-29 03:15:51 -05:00
|
|
|
}
|
|
|
|
|
2019-10-29 07:25:46 -05:00
|
|
|
fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
|
|
|
|
self.arena.alloc(SyntaxNodePtr::new(item))
|
2019-10-29 03:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 13:38:22 -06:00
|
|
|
/// Walks the subtree in bdfs order, calling `f` for each node. What is bdfs
|
|
|
|
/// order? It is a mix of breadth-first and depth first orders. Nodes for which
|
|
|
|
/// `f` returns true are visited breadth-first, all the other nodes are explored
|
|
|
|
/// depth-first.
|
|
|
|
///
|
|
|
|
/// In other words, the size of the bfs queue is bound by the number of "true"
|
|
|
|
/// nodes.
|
|
|
|
fn bdfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode) -> bool) {
|
2019-10-29 03:15:51 -05:00
|
|
|
let mut curr_layer = vec![node.clone()];
|
|
|
|
let mut next_layer = vec![];
|
|
|
|
while !curr_layer.is_empty() {
|
|
|
|
curr_layer.drain(..).for_each(|node| {
|
2021-01-16 13:38:22 -06:00
|
|
|
let mut preorder = node.preorder();
|
|
|
|
while let Some(event) = preorder.next() {
|
|
|
|
match event {
|
|
|
|
syntax::WalkEvent::Enter(node) => {
|
|
|
|
if f(node.clone()) {
|
|
|
|
next_layer.extend(node.children());
|
|
|
|
preorder.skip_subtree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syntax::WalkEvent::Leave(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 03:15:51 -05:00
|
|
|
});
|
|
|
|
std::mem::swap(&mut curr_layer, &mut next_layer);
|
|
|
|
}
|
|
|
|
}
|