diff options
Diffstat (limited to 'crates/ra_hir_def/src/body.rs')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index dfb79a30a..d77ccb272 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! Defines `Body`: a lowered representation of bodies of functions, statics and |
2 | //! consts. | ||
2 | mod lower; | 3 | mod lower; |
3 | pub mod scope; | 4 | pub mod scope; |
4 | 5 | ||
@@ -13,14 +14,14 @@ use ra_syntax::{ast, AstNode, AstPtr}; | |||
13 | use rustc_hash::FxHashMap; | 14 | use rustc_hash::FxHashMap; |
14 | 15 | ||
15 | use crate::{ | 16 | use crate::{ |
16 | db::DefDatabase2, | 17 | db::DefDatabase, |
17 | expr::{Expr, ExprId, Pat, PatId}, | 18 | expr::{Expr, ExprId, Pat, PatId}, |
18 | nameres::CrateDefMap, | 19 | nameres::CrateDefMap, |
19 | path::Path, | 20 | path::Path, |
20 | AstItemDef, DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, | 21 | DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, |
21 | }; | 22 | }; |
22 | 23 | ||
23 | pub struct Expander { | 24 | struct Expander { |
24 | crate_def_map: Arc<CrateDefMap>, | 25 | crate_def_map: Arc<CrateDefMap>, |
25 | current_file_id: HirFileId, | 26 | current_file_id: HirFileId, |
26 | hygiene: Hygiene, | 27 | hygiene: Hygiene, |
@@ -28,7 +29,7 @@ pub struct Expander { | |||
28 | } | 29 | } |
29 | 30 | ||
30 | impl Expander { | 31 | impl Expander { |
31 | pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander { | 32 | fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander { |
32 | let crate_def_map = db.crate_def_map(module.krate); | 33 | let crate_def_map = db.crate_def_map(module.krate); |
33 | let hygiene = Hygiene::new(db, current_file_id); | 34 | let hygiene = Hygiene::new(db, current_file_id); |
34 | Expander { crate_def_map, current_file_id, hygiene, module } | 35 | Expander { crate_def_map, current_file_id, hygiene, module } |
@@ -36,7 +37,7 @@ impl Expander { | |||
36 | 37 | ||
37 | fn enter_expand( | 38 | fn enter_expand( |
38 | &mut self, | 39 | &mut self, |
39 | db: &impl DefDatabase2, | 40 | db: &impl DefDatabase, |
40 | macro_call: ast::MacroCall, | 41 | macro_call: ast::MacroCall, |
41 | ) -> Option<(Mark, ast::Expr)> { | 42 | ) -> Option<(Mark, ast::Expr)> { |
42 | let ast_id = AstId::new( | 43 | let ast_id = AstId::new( |
@@ -67,7 +68,7 @@ impl Expander { | |||
67 | None | 68 | None |
68 | } | 69 | } |
69 | 70 | ||
70 | fn exit(&mut self, db: &impl DefDatabase2, mark: Mark) { | 71 | fn exit(&mut self, db: &impl DefDatabase, mark: Mark) { |
71 | self.hygiene = Hygiene::new(db, mark.file_id); | 72 | self.hygiene = Hygiene::new(db, mark.file_id); |
72 | self.current_file_id = mark.file_id; | 73 | self.current_file_id = mark.file_id; |
73 | std::mem::forget(mark); | 74 | std::mem::forget(mark); |
@@ -81,8 +82,8 @@ impl Expander { | |||
81 | Path::from_src(path, &self.hygiene) | 82 | Path::from_src(path, &self.hygiene) |
82 | } | 83 | } |
83 | 84 | ||
84 | fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> { | 85 | fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &Path) -> Option<MacroDefId> { |
85 | self.crate_def_map.resolve_path(db, self.module.module_id, path).0.get_macros() | 86 | self.crate_def_map.resolve_path(db, self.module.module_id, path).0.take_macros() |
86 | } | 87 | } |
87 | } | 88 | } |
88 | 89 | ||
@@ -101,17 +102,17 @@ impl Drop for Mark { | |||
101 | /// The body of an item (function, const etc.). | 102 | /// The body of an item (function, const etc.). |
102 | #[derive(Debug, Eq, PartialEq)] | 103 | #[derive(Debug, Eq, PartialEq)] |
103 | pub struct Body { | 104 | pub struct Body { |
104 | exprs: Arena<ExprId, Expr>, | 105 | pub exprs: Arena<ExprId, Expr>, |
105 | pats: Arena<PatId, Pat>, | 106 | pub pats: Arena<PatId, Pat>, |
106 | /// The patterns for the function's parameters. While the parameter types are | 107 | /// The patterns for the function's parameters. While the parameter types are |
107 | /// part of the function signature, the patterns are not (they don't change | 108 | /// part of the function signature, the patterns are not (they don't change |
108 | /// the external type of the function). | 109 | /// the external type of the function). |
109 | /// | 110 | /// |
110 | /// If this `Body` is for the body of a constant, this will just be | 111 | /// If this `Body` is for the body of a constant, this will just be |
111 | /// empty. | 112 | /// empty. |
112 | params: Vec<PatId>, | 113 | pub params: Vec<PatId>, |
113 | /// The `ExprId` of the actual body expression. | 114 | /// The `ExprId` of the actual body expression. |
114 | body_expr: ExprId, | 115 | pub body_expr: ExprId, |
115 | } | 116 | } |
116 | 117 | ||
117 | pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; | 118 | pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; |
@@ -142,7 +143,7 @@ pub struct BodySourceMap { | |||
142 | 143 | ||
143 | impl Body { | 144 | impl Body { |
144 | pub(crate) fn body_with_source_map_query( | 145 | pub(crate) fn body_with_source_map_query( |
145 | db: &impl DefDatabase2, | 146 | db: &impl DefDatabase, |
146 | def: DefWithBodyId, | 147 | def: DefWithBodyId, |
147 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | 148 | ) -> (Arc<Body>, Arc<BodySourceMap>) { |
148 | let mut params = None; | 149 | let mut params = None; |
@@ -160,6 +161,7 @@ impl Body { | |||
160 | (src.file_id, c.module(db), src.value.body()) | 161 | (src.file_id, c.module(db), src.value.body()) |
161 | } | 162 | } |
162 | DefWithBodyId::StaticId(s) => { | 163 | DefWithBodyId::StaticId(s) => { |
164 | let s = s.lookup(db); | ||
163 | let src = s.source(db); | 165 | let src = s.source(db); |
164 | (src.file_id, s.module(db), src.value.body()) | 166 | (src.file_id, s.module(db), src.value.body()) |
165 | } | 167 | } |
@@ -169,34 +171,18 @@ impl Body { | |||
169 | (Arc::new(body), Arc::new(source_map)) | 171 | (Arc::new(body), Arc::new(source_map)) |
170 | } | 172 | } |
171 | 173 | ||
172 | pub(crate) fn body_query(db: &impl DefDatabase2, def: DefWithBodyId) -> Arc<Body> { | 174 | pub(crate) fn body_query(db: &impl DefDatabase, def: DefWithBodyId) -> Arc<Body> { |
173 | db.body_with_source_map(def).0 | 175 | db.body_with_source_map(def).0 |
174 | } | 176 | } |
175 | 177 | ||
176 | fn new( | 178 | fn new( |
177 | db: &impl DefDatabase2, | 179 | db: &impl DefDatabase, |
178 | expander: Expander, | 180 | expander: Expander, |
179 | params: Option<ast::ParamList>, | 181 | params: Option<ast::ParamList>, |
180 | body: Option<ast::Expr>, | 182 | body: Option<ast::Expr>, |
181 | ) -> (Body, BodySourceMap) { | 183 | ) -> (Body, BodySourceMap) { |
182 | lower::lower(db, expander, params, body) | 184 | lower::lower(db, expander, params, body) |
183 | } | 185 | } |
184 | |||
185 | pub fn params(&self) -> &[PatId] { | ||
186 | &self.params | ||
187 | } | ||
188 | |||
189 | pub fn body_expr(&self) -> ExprId { | ||
190 | self.body_expr | ||
191 | } | ||
192 | |||
193 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | ||
194 | self.exprs.iter() | ||
195 | } | ||
196 | |||
197 | pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> { | ||
198 | self.pats.iter() | ||
199 | } | ||
200 | } | 186 | } |
201 | 187 | ||
202 | impl Index<ExprId> for Body { | 188 | impl Index<ExprId> for Body { |