diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 14:37:57 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 14:37:57 +0000 |
commit | a583070b7d47852094b498c3191b4b5d87520fc3 (patch) | |
tree | dfe8364efeeaca6f8f32e1d922fb615119b8012b /crates/ra_hir/src/expr.rs | |
parent | 81fcfc55d247bfe6090741f2e4ae9aa89947bf32 (diff) | |
parent | 7b901f86cd1d0198994e5a2ab7eea18f444dd148 (diff) |
Merge #610
610: move SyntaxPtr to ra_syntax r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 51dae8e25..1a3821692 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -4,8 +4,10 @@ use std::sync::Arc; | |||
4 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | 5 | ||
6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
7 | use ra_db::LocalSyntaxPtr; | 7 | use ra_syntax::{ |
8 | use ra_syntax::ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor}; | 8 | SyntaxNodePtr, AstNode, |
9 | ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor} | ||
10 | }; | ||
9 | 11 | ||
10 | use crate::{Path, type_ref::{Mutability, TypeRef}, Name, HirDatabase, DefId, Def, name::AsName}; | 12 | use crate::{Path, type_ref::{Mutability, TypeRef}, Name, HirDatabase, DefId, Def, name::AsName}; |
11 | use crate::ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy}; | 13 | use crate::ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy}; |
@@ -38,10 +40,10 @@ pub struct Body { | |||
38 | #[derive(Debug, Eq, PartialEq)] | 40 | #[derive(Debug, Eq, PartialEq)] |
39 | pub struct BodySyntaxMapping { | 41 | pub struct BodySyntaxMapping { |
40 | body: Arc<Body>, | 42 | body: Arc<Body>, |
41 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, | 43 | expr_syntax_mapping: FxHashMap<SyntaxNodePtr, ExprId>, |
42 | expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>, | 44 | expr_syntax_mapping_back: ArenaMap<ExprId, SyntaxNodePtr>, |
43 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, | 45 | pat_syntax_mapping: FxHashMap<SyntaxNodePtr, PatId>, |
44 | pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>, | 46 | pat_syntax_mapping_back: ArenaMap<PatId, SyntaxNodePtr>, |
45 | } | 47 | } |
46 | 48 | ||
47 | impl Body { | 49 | impl Body { |
@@ -71,31 +73,31 @@ impl Index<PatId> for Body { | |||
71 | } | 73 | } |
72 | 74 | ||
73 | impl BodySyntaxMapping { | 75 | impl BodySyntaxMapping { |
74 | pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { | 76 | pub fn expr_syntax(&self, expr: ExprId) -> Option<SyntaxNodePtr> { |
75 | self.expr_syntax_mapping_back.get(expr).cloned() | 77 | self.expr_syntax_mapping_back.get(expr).cloned() |
76 | } | 78 | } |
77 | 79 | ||
78 | pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> { | 80 | pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option<ExprId> { |
79 | self.expr_syntax_mapping.get(&ptr).cloned() | 81 | self.expr_syntax_mapping.get(&ptr).cloned() |
80 | } | 82 | } |
81 | 83 | ||
82 | pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { | 84 | pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { |
83 | self.expr_syntax_mapping | 85 | self.expr_syntax_mapping |
84 | .get(&LocalSyntaxPtr::new(node.syntax())) | 86 | .get(&SyntaxNodePtr::new(node.syntax())) |
85 | .cloned() | 87 | .cloned() |
86 | } | 88 | } |
87 | 89 | ||
88 | pub fn pat_syntax(&self, pat: PatId) -> Option<LocalSyntaxPtr> { | 90 | pub fn pat_syntax(&self, pat: PatId) -> Option<SyntaxNodePtr> { |
89 | self.pat_syntax_mapping_back.get(pat).cloned() | 91 | self.pat_syntax_mapping_back.get(pat).cloned() |
90 | } | 92 | } |
91 | 93 | ||
92 | pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> { | 94 | pub fn syntax_pat(&self, ptr: SyntaxNodePtr) -> Option<PatId> { |
93 | self.pat_syntax_mapping.get(&ptr).cloned() | 95 | self.pat_syntax_mapping.get(&ptr).cloned() |
94 | } | 96 | } |
95 | 97 | ||
96 | pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { | 98 | pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { |
97 | self.pat_syntax_mapping | 99 | self.pat_syntax_mapping |
98 | .get(&LocalSyntaxPtr::new(node.syntax())) | 100 | .get(&SyntaxNodePtr::new(node.syntax())) |
99 | .cloned() | 101 | .cloned() |
100 | } | 102 | } |
101 | 103 | ||
@@ -440,10 +442,10 @@ pub(crate) fn body_hir(db: &impl HirDatabase, def_id: DefId) -> Arc<Body> { | |||
440 | struct ExprCollector { | 442 | struct ExprCollector { |
441 | exprs: Arena<ExprId, Expr>, | 443 | exprs: Arena<ExprId, Expr>, |
442 | pats: Arena<PatId, Pat>, | 444 | pats: Arena<PatId, Pat>, |
443 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, | 445 | expr_syntax_mapping: FxHashMap<SyntaxNodePtr, ExprId>, |
444 | expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>, | 446 | expr_syntax_mapping_back: ArenaMap<ExprId, SyntaxNodePtr>, |
445 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, | 447 | pat_syntax_mapping: FxHashMap<SyntaxNodePtr, PatId>, |
446 | pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>, | 448 | pat_syntax_mapping_back: ArenaMap<PatId, SyntaxNodePtr>, |
447 | } | 449 | } |
448 | 450 | ||
449 | impl ExprCollector { | 451 | impl ExprCollector { |
@@ -458,14 +460,14 @@ impl ExprCollector { | |||
458 | } | 460 | } |
459 | } | 461 | } |
460 | 462 | ||
461 | fn alloc_expr(&mut self, expr: Expr, syntax_ptr: LocalSyntaxPtr) -> ExprId { | 463 | fn alloc_expr(&mut self, expr: Expr, syntax_ptr: SyntaxNodePtr) -> ExprId { |
462 | let id = self.exprs.alloc(expr); | 464 | let id = self.exprs.alloc(expr); |
463 | self.expr_syntax_mapping.insert(syntax_ptr, id); | 465 | self.expr_syntax_mapping.insert(syntax_ptr, id); |
464 | self.expr_syntax_mapping_back.insert(id, syntax_ptr); | 466 | self.expr_syntax_mapping_back.insert(id, syntax_ptr); |
465 | id | 467 | id |
466 | } | 468 | } |
467 | 469 | ||
468 | fn alloc_pat(&mut self, pat: Pat, syntax_ptr: LocalSyntaxPtr) -> PatId { | 470 | fn alloc_pat(&mut self, pat: Pat, syntax_ptr: SyntaxNodePtr) -> PatId { |
469 | let id = self.pats.alloc(pat); | 471 | let id = self.pats.alloc(pat); |
470 | self.pat_syntax_mapping.insert(syntax_ptr, id); | 472 | self.pat_syntax_mapping.insert(syntax_ptr, id); |
471 | self.pat_syntax_mapping_back.insert(id, syntax_ptr); | 473 | self.pat_syntax_mapping_back.insert(id, syntax_ptr); |
@@ -481,7 +483,7 @@ impl ExprCollector { | |||
481 | } | 483 | } |
482 | 484 | ||
483 | fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { | 485 | fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { |
484 | let syntax_ptr = LocalSyntaxPtr::new(expr.syntax()); | 486 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); |
485 | match expr.kind() { | 487 | match expr.kind() { |
486 | ast::ExprKind::IfExpr(e) => { | 488 | ast::ExprKind::IfExpr(e) => { |
487 | if let Some(pat) = e.condition().and_then(|c| c.pat()) { | 489 | if let Some(pat) = e.condition().and_then(|c| c.pat()) { |
@@ -643,9 +645,9 @@ impl ExprCollector { | |||
643 | // field shorthand | 645 | // field shorthand |
644 | let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr))); | 646 | let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr))); |
645 | self.expr_syntax_mapping | 647 | self.expr_syntax_mapping |
646 | .insert(LocalSyntaxPtr::new(nr.syntax()), id); | 648 | .insert(SyntaxNodePtr::new(nr.syntax()), id); |
647 | self.expr_syntax_mapping_back | 649 | self.expr_syntax_mapping_back |
648 | .insert(id, LocalSyntaxPtr::new(nr.syntax())); | 650 | .insert(id, SyntaxNodePtr::new(nr.syntax())); |
649 | id | 651 | id |
650 | } else { | 652 | } else { |
651 | self.exprs.alloc(Expr::Missing) | 653 | self.exprs.alloc(Expr::Missing) |
@@ -806,7 +808,7 @@ impl ExprCollector { | |||
806 | let tail = block.expr().map(|e| self.collect_expr(e)); | 808 | let tail = block.expr().map(|e| self.collect_expr(e)); |
807 | self.alloc_expr( | 809 | self.alloc_expr( |
808 | Expr::Block { statements, tail }, | 810 | Expr::Block { statements, tail }, |
809 | LocalSyntaxPtr::new(block.syntax()), | 811 | SyntaxNodePtr::new(block.syntax()), |
810 | ) | 812 | ) |
811 | } | 813 | } |
812 | 814 | ||
@@ -883,7 +885,7 @@ impl ExprCollector { | |||
883 | // TODO: implement | 885 | // TODO: implement |
884 | ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, | 886 | ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, |
885 | }; | 887 | }; |
886 | let syntax_ptr = LocalSyntaxPtr::new(pat.syntax()); | 888 | let syntax_ptr = SyntaxNodePtr::new(pat.syntax()); |
887 | self.alloc_pat(pattern, syntax_ptr) | 889 | self.alloc_pat(pattern, syntax_ptr) |
888 | } | 890 | } |
889 | 891 | ||
@@ -919,7 +921,7 @@ pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping { | |||
919 | let mut params = Vec::new(); | 921 | let mut params = Vec::new(); |
920 | 922 | ||
921 | if let Some(self_param) = param_list.self_param() { | 923 | if let Some(self_param) = param_list.self_param() { |
922 | let self_param = LocalSyntaxPtr::new( | 924 | let self_param = SyntaxNodePtr::new( |
923 | self_param | 925 | self_param |
924 | .self_kw() | 926 | .self_kw() |
925 | .expect("self param without self keyword") | 927 | .expect("self param without self keyword") |