mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
dont store body inside source map
This commit is contained in:
parent
eaf1df26e9
commit
f4c5383103
5 changed files with 54 additions and 61 deletions
|
@ -48,13 +48,13 @@ pub struct Body {
|
|||
/// expression containing it; but for type inference etc., we want to operate on
|
||||
/// a structure that is agnostic to the actual positions of expressions in the
|
||||
/// file, so that we don't recompute types whenever some whitespace is typed.
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Default, Debug, Eq, PartialEq)]
|
||||
pub struct BodySourceMap {
|
||||
body: Arc<Body>,
|
||||
expr_syntax_mapping: FxHashMap<SyntaxNodePtr, ExprId>,
|
||||
expr_syntax_mapping_back: ArenaMap<ExprId, SyntaxNodePtr>,
|
||||
pat_syntax_mapping: FxHashMap<SyntaxNodePtr, PatId>,
|
||||
pat_syntax_mapping_back: ArenaMap<PatId, SyntaxNodePtr>,
|
||||
// body: Arc<Body>,
|
||||
expr_map: FxHashMap<SyntaxNodePtr, ExprId>,
|
||||
expr_map_back: ArenaMap<ExprId, SyntaxNodePtr>,
|
||||
pat_map: FxHashMap<SyntaxNodePtr, PatId>,
|
||||
pat_map_back: ArenaMap<PatId, SyntaxNodePtr>,
|
||||
}
|
||||
|
||||
impl Body {
|
||||
|
@ -79,7 +79,7 @@ impl Body {
|
|||
}
|
||||
|
||||
pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
|
||||
db.body_source_map(self.owner)
|
||||
db.body_with_source_map(self.owner).1
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,31 +121,27 @@ impl Index<PatId> for Body {
|
|||
|
||||
impl BodySourceMap {
|
||||
pub fn expr_syntax(&self, expr: ExprId) -> Option<SyntaxNodePtr> {
|
||||
self.expr_syntax_mapping_back.get(expr).cloned()
|
||||
self.expr_map_back.get(expr).cloned()
|
||||
}
|
||||
|
||||
pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option<ExprId> {
|
||||
self.expr_syntax_mapping.get(&ptr).cloned()
|
||||
self.expr_map.get(&ptr).cloned()
|
||||
}
|
||||
|
||||
pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> {
|
||||
self.expr_syntax_mapping.get(&SyntaxNodePtr::new(node.syntax())).cloned()
|
||||
self.expr_map.get(&SyntaxNodePtr::new(node.syntax())).cloned()
|
||||
}
|
||||
|
||||
pub fn pat_syntax(&self, pat: PatId) -> Option<SyntaxNodePtr> {
|
||||
self.pat_syntax_mapping_back.get(pat).cloned()
|
||||
self.pat_map_back.get(pat).cloned()
|
||||
}
|
||||
|
||||
pub fn syntax_pat(&self, ptr: SyntaxNodePtr) -> Option<PatId> {
|
||||
self.pat_syntax_mapping.get(&ptr).cloned()
|
||||
self.pat_map.get(&ptr).cloned()
|
||||
}
|
||||
|
||||
pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> {
|
||||
self.pat_syntax_mapping.get(&SyntaxNodePtr::new(node.syntax())).cloned()
|
||||
}
|
||||
|
||||
pub fn body(&self) -> &Arc<Body> {
|
||||
&self.body
|
||||
self.pat_map.get(&SyntaxNodePtr::new(node.syntax())).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -467,18 +463,11 @@ impl Pat {
|
|||
|
||||
// Queries
|
||||
|
||||
pub(crate) fn body_hir(db: &impl HirDatabase, func: Function) -> Arc<Body> {
|
||||
Arc::clone(&body_source_map(db, func).body)
|
||||
}
|
||||
|
||||
struct ExprCollector {
|
||||
owner: Function,
|
||||
exprs: Arena<ExprId, Expr>,
|
||||
pats: Arena<PatId, Pat>,
|
||||
expr_syntax_mapping: FxHashMap<SyntaxNodePtr, ExprId>,
|
||||
expr_syntax_mapping_back: ArenaMap<ExprId, SyntaxNodePtr>,
|
||||
pat_syntax_mapping: FxHashMap<SyntaxNodePtr, PatId>,
|
||||
pat_syntax_mapping_back: ArenaMap<PatId, SyntaxNodePtr>,
|
||||
source_map: BodySourceMap,
|
||||
params: Vec<PatId>,
|
||||
body_expr: Option<ExprId>,
|
||||
}
|
||||
|
@ -489,10 +478,7 @@ impl ExprCollector {
|
|||
owner,
|
||||
exprs: Arena::default(),
|
||||
pats: Arena::default(),
|
||||
expr_syntax_mapping: FxHashMap::default(),
|
||||
expr_syntax_mapping_back: ArenaMap::default(),
|
||||
pat_syntax_mapping: FxHashMap::default(),
|
||||
pat_syntax_mapping_back: ArenaMap::default(),
|
||||
source_map: BodySourceMap::default(),
|
||||
params: Vec::new(),
|
||||
body_expr: None,
|
||||
}
|
||||
|
@ -500,15 +486,15 @@ impl ExprCollector {
|
|||
|
||||
fn alloc_expr(&mut self, expr: Expr, syntax_ptr: SyntaxNodePtr) -> ExprId {
|
||||
let id = self.exprs.alloc(expr);
|
||||
self.expr_syntax_mapping.insert(syntax_ptr, id);
|
||||
self.expr_syntax_mapping_back.insert(id, syntax_ptr);
|
||||
self.source_map.expr_map.insert(syntax_ptr, id);
|
||||
self.source_map.expr_map_back.insert(id, syntax_ptr);
|
||||
id
|
||||
}
|
||||
|
||||
fn alloc_pat(&mut self, pat: Pat, syntax_ptr: SyntaxNodePtr) -> PatId {
|
||||
let id = self.pats.alloc(pat);
|
||||
self.pat_syntax_mapping.insert(syntax_ptr, id);
|
||||
self.pat_syntax_mapping_back.insert(id, syntax_ptr);
|
||||
self.source_map.pat_map.insert(syntax_ptr, id);
|
||||
self.source_map.pat_map_back.insert(id, syntax_ptr);
|
||||
id
|
||||
}
|
||||
|
||||
|
@ -639,7 +625,7 @@ impl ExprCollector {
|
|||
ast::ExprKind::ParenExpr(e) => {
|
||||
let inner = self.collect_expr_opt(e.expr());
|
||||
// make the paren expr point to the inner expression as well
|
||||
self.expr_syntax_mapping.insert(syntax_ptr, inner);
|
||||
self.source_map.expr_map.insert(syntax_ptr, inner);
|
||||
inner
|
||||
}
|
||||
ast::ExprKind::ReturnExpr(e) => {
|
||||
|
@ -660,9 +646,11 @@ impl ExprCollector {
|
|||
} else if let Some(nr) = field.name_ref() {
|
||||
// field shorthand
|
||||
let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr)));
|
||||
self.expr_syntax_mapping
|
||||
self.source_map
|
||||
.expr_map
|
||||
.insert(SyntaxNodePtr::new(nr.syntax()), id);
|
||||
self.expr_syntax_mapping_back
|
||||
self.source_map
|
||||
.expr_map_back
|
||||
.insert(id, SyntaxNodePtr::new(nr.syntax()));
|
||||
id
|
||||
} else {
|
||||
|
@ -910,7 +898,7 @@ impl ExprCollector {
|
|||
self.body_expr = Some(body);
|
||||
}
|
||||
|
||||
fn into_body_source_map(self) -> BodySourceMap {
|
||||
fn finish(self) -> (Body, BodySourceMap) {
|
||||
let body = Body {
|
||||
owner: self.owner,
|
||||
exprs: self.exprs,
|
||||
|
@ -918,28 +906,30 @@ impl ExprCollector {
|
|||
params: self.params,
|
||||
body_expr: self.body_expr.expect("A body should have been collected"),
|
||||
};
|
||||
BodySourceMap {
|
||||
body: Arc::new(body),
|
||||
expr_syntax_mapping: self.expr_syntax_mapping,
|
||||
expr_syntax_mapping_back: self.expr_syntax_mapping_back,
|
||||
pat_syntax_mapping: self.pat_syntax_mapping,
|
||||
pat_syntax_mapping_back: self.pat_syntax_mapping_back,
|
||||
}
|
||||
(body, self.source_map)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn body_source_map(db: &impl HirDatabase, func: Function) -> Arc<BodySourceMap> {
|
||||
pub(crate) fn body_with_source_map_query(
|
||||
db: &impl HirDatabase,
|
||||
func: Function,
|
||||
) -> (Arc<Body>, Arc<BodySourceMap>) {
|
||||
let mut collector = ExprCollector::new(func);
|
||||
|
||||
// TODO: consts, etc.
|
||||
collector.collect_fn_body(&func.source(db).1);
|
||||
|
||||
Arc::new(collector.into_body_source_map())
|
||||
let (body, source_map) = collector.finish();
|
||||
(Arc::new(body), Arc::new(source_map))
|
||||
}
|
||||
|
||||
pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc<Body> {
|
||||
db.body_with_source_map(func).0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> BodySourceMap {
|
||||
fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) {
|
||||
let mut collector = ExprCollector::new(function);
|
||||
collector.collect_fn_body(node);
|
||||
collector.into_body_source_map()
|
||||
collector.finish()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue