From f4c5383103e2b79b05e2669889d44e51988932a6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 Mar 2019 16:18:40 +0300 Subject: dont store body inside source map --- crates/ra_hir/src/code_model_api.rs | 4 +- crates/ra_hir/src/db.rs | 11 +++-- crates/ra_hir/src/expr.rs | 86 ++++++++++++++++--------------------- crates/ra_hir/src/expr/scope.rs | 12 +++--- 4 files changed, 53 insertions(+), 60 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index f8521e895..c77b8acc7 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -484,7 +484,7 @@ impl Function { } pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { - db.body_source_map(*self) + db.body_with_source_map(*self).1 } pub fn body(&self, db: &impl HirDatabase) -> Arc { @@ -497,7 +497,7 @@ impl Function { pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping { let scopes = db.expr_scopes(*self); - let syntax_mapping = db.body_source_map(*self); + let syntax_mapping = db.body_with_source_map(*self).1; ScopesWithSyntaxMapping { scopes, syntax_mapping } } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index cb1c24561..a669dc9b0 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -105,11 +105,14 @@ pub trait HirDatabase: PersistentHirDatabase { #[salsa::invoke(crate::ty::type_for_field)] fn type_for_field(&self, field: StructField) -> Ty; - #[salsa::invoke(crate::expr::body_hir)] - fn body_hir(&self, func: Function) -> Arc; + #[salsa::invoke(crate::expr::body_with_source_map_query)] + fn body_with_source_map( + &self, + func: Function, + ) -> (Arc, Arc); - #[salsa::invoke(crate::expr::body_source_map)] - fn body_source_map(&self, func: Function) -> Arc; + #[salsa::invoke(crate::expr::body_hir_query)] + fn body_hir(&self, func: Function) -> Arc; #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] fn impls_in_crate(&self, krate: Crate) -> Arc; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index b1398411b..4e399ec6d 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -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, - expr_syntax_mapping: FxHashMap, - expr_syntax_mapping_back: ArenaMap, - pat_syntax_mapping: FxHashMap, - pat_syntax_mapping_back: ArenaMap, + // body: Arc, + expr_map: FxHashMap, + expr_map_back: ArenaMap, + pat_map: FxHashMap, + pat_map_back: ArenaMap, } impl Body { @@ -79,7 +79,7 @@ impl Body { } pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc { - db.body_source_map(self.owner) + db.body_with_source_map(self.owner).1 } } @@ -121,31 +121,27 @@ impl Index for Body { impl BodySourceMap { pub fn expr_syntax(&self, expr: ExprId) -> Option { - self.expr_syntax_mapping_back.get(expr).cloned() + self.expr_map_back.get(expr).cloned() } pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { - self.expr_syntax_mapping.get(&ptr).cloned() + self.expr_map.get(&ptr).cloned() } pub fn node_expr(&self, node: &ast::Expr) -> Option { - 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 { - self.pat_syntax_mapping_back.get(pat).cloned() + self.pat_map_back.get(pat).cloned() } pub fn syntax_pat(&self, ptr: SyntaxNodePtr) -> Option { - self.pat_syntax_mapping.get(&ptr).cloned() + self.pat_map.get(&ptr).cloned() } pub fn node_pat(&self, node: &ast::Pat) -> Option { - self.pat_syntax_mapping.get(&SyntaxNodePtr::new(node.syntax())).cloned() - } - - pub fn body(&self) -> &Arc { - &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 { - Arc::clone(&body_source_map(db, func).body) -} - struct ExprCollector { owner: Function, exprs: Arena, pats: Arena, - expr_syntax_mapping: FxHashMap, - expr_syntax_mapping_back: ArenaMap, - pat_syntax_mapping: FxHashMap, - pat_syntax_mapping_back: ArenaMap, + source_map: BodySourceMap, params: Vec, body_expr: Option, } @@ -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 { +pub(crate) fn body_with_source_map_query( + db: &impl HirDatabase, + func: Function, +) -> (Arc, Arc) { 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 { + 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() } diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index bb919dcfa..d5824eb81 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -316,11 +316,11 @@ mod tests { let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; - let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); - let scopes = ExprScopes::new(Arc::clone(body_hir.body())); + let (body, syntax_mapping) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); + let scopes = ExprScopes::new(Arc::new(body)); let scopes = ScopesWithSyntaxMapping { scopes: Arc::new(scopes), - syntax_mapping: Arc::new(body_hir), + syntax_mapping: Arc::new(syntax_mapping), }; let actual = scopes .scope_chain(marker.syntax()) @@ -417,11 +417,11 @@ mod tests { let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; - let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); - let scopes = ExprScopes::new(Arc::clone(body_hir.body())); + let (body, syntax_mapping) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); + let scopes = ExprScopes::new(Arc::new(body)); let scopes = ScopesWithSyntaxMapping { scopes: Arc::new(scopes), - syntax_mapping: Arc::new(body_hir), + syntax_mapping: Arc::new(syntax_mapping), }; let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); let local_name = local_name_entry.ptr(); -- cgit v1.2.3