diff options
Diffstat (limited to 'crates/ra_hir/src/expr')
-rw-r--r-- | crates/ra_hir/src/expr/lower.rs | 653 |
1 files changed, 653 insertions, 0 deletions
diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs new file mode 100644 index 000000000..01b770149 --- /dev/null +++ b/crates/ra_hir/src/expr/lower.rs | |||
@@ -0,0 +1,653 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use ra_arena::Arena; | ||
4 | use ra_syntax::{ | ||
5 | ast::{ | ||
6 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, | ||
7 | TypeAscriptionOwner, | ||
8 | }, | ||
9 | AstNode, AstPtr, | ||
10 | }; | ||
11 | use test_utils::tested_by; | ||
12 | |||
13 | use crate::{ | ||
14 | name::{AsName, Name, SELF_PARAM}, | ||
15 | path::GenericArgs, | ||
16 | ty::primitive::{FloatTy, IntTy, UncertainFloatTy, UncertainIntTy}, | ||
17 | type_ref::TypeRef, | ||
18 | DefWithBody, Either, HasSource, HirDatabase, HirFileId, MacroCallLoc, MacroFileKind, | ||
19 | Mutability, Path, Resolver, | ||
20 | }; | ||
21 | |||
22 | use super::{ | ||
23 | ArithOp, Array, BinaryOp, BindingAnnotation, Body, BodySourceMap, CmpOp, Expr, ExprId, | ||
24 | Literal, LogicOp, MatchArm, Ordering, Pat, PatId, PatPtr, RecordFieldPat, RecordLitField, | ||
25 | Statement, | ||
26 | }; | ||
27 | |||
28 | pub(crate) struct ExprCollector<DB> { | ||
29 | db: DB, | ||
30 | owner: DefWithBody, | ||
31 | exprs: Arena<ExprId, Expr>, | ||
32 | pats: Arena<PatId, Pat>, | ||
33 | source_map: BodySourceMap, | ||
34 | params: Vec<PatId>, | ||
35 | body_expr: Option<ExprId>, | ||
36 | resolver: Resolver, | ||
37 | // Expr collector expands macros along the way. original points to the file | ||
38 | // we started with, current points to the current macro expansion. source | ||
39 | // maps don't support macros yet, so we only record info into source map if | ||
40 | // current == original (see #1196) | ||
41 | original_file_id: HirFileId, | ||
42 | current_file_id: HirFileId, | ||
43 | } | ||
44 | |||
45 | impl<'a, DB> ExprCollector<&'a DB> | ||
46 | where | ||
47 | DB: HirDatabase, | ||
48 | { | ||
49 | fn new(owner: DefWithBody, file_id: HirFileId, resolver: Resolver, db: &'a DB) -> Self { | ||
50 | ExprCollector { | ||
51 | owner, | ||
52 | resolver, | ||
53 | db, | ||
54 | exprs: Arena::default(), | ||
55 | pats: Arena::default(), | ||
56 | source_map: BodySourceMap::default(), | ||
57 | params: Vec::new(), | ||
58 | body_expr: None, | ||
59 | original_file_id: file_id, | ||
60 | current_file_id: file_id, | ||
61 | } | ||
62 | } | ||
63 | fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { | ||
64 | let ptr = Either::A(ptr); | ||
65 | let id = self.exprs.alloc(expr); | ||
66 | if self.current_file_id == self.original_file_id { | ||
67 | self.source_map.expr_map.insert(ptr, id); | ||
68 | self.source_map.expr_map_back.insert(id, ptr); | ||
69 | } | ||
70 | id | ||
71 | } | ||
72 | |||
73 | fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { | ||
74 | let id = self.pats.alloc(pat); | ||
75 | |||
76 | if self.current_file_id == self.original_file_id { | ||
77 | self.source_map.pat_map.insert(ptr, id); | ||
78 | self.source_map.pat_map_back.insert(id, ptr); | ||
79 | } | ||
80 | |||
81 | id | ||
82 | } | ||
83 | |||
84 | fn empty_block(&mut self) -> ExprId { | ||
85 | let block = Expr::Block { statements: Vec::new(), tail: None }; | ||
86 | self.exprs.alloc(block) | ||
87 | } | ||
88 | |||
89 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { | ||
90 | let syntax_ptr = AstPtr::new(&expr); | ||
91 | match expr { | ||
92 | ast::Expr::IfExpr(e) => { | ||
93 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
94 | |||
95 | let else_branch = e.else_branch().map(|b| match b { | ||
96 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
97 | ast::ElseBranch::IfExpr(elif) => { | ||
98 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
99 | self.collect_expr(expr) | ||
100 | } | ||
101 | }); | ||
102 | |||
103 | let condition = match e.condition() { | ||
104 | None => self.exprs.alloc(Expr::Missing), | ||
105 | Some(condition) => match condition.pat() { | ||
106 | None => self.collect_expr_opt(condition.expr()), | ||
107 | // if let -- desugar to match | ||
108 | Some(pat) => { | ||
109 | let pat = self.collect_pat(pat); | ||
110 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
111 | let placeholder_pat = self.pats.alloc(Pat::Missing); | ||
112 | let arms = vec![ | ||
113 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, | ||
114 | MatchArm { | ||
115 | pats: vec![placeholder_pat], | ||
116 | expr: else_branch.unwrap_or_else(|| self.empty_block()), | ||
117 | guard: None, | ||
118 | }, | ||
119 | ]; | ||
120 | return self | ||
121 | .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr); | ||
122 | } | ||
123 | }, | ||
124 | }; | ||
125 | |||
126 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) | ||
127 | } | ||
128 | ast::Expr::TryBlockExpr(e) => { | ||
129 | let body = self.collect_block_opt(e.body()); | ||
130 | self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) | ||
131 | } | ||
132 | ast::Expr::BlockExpr(e) => self.collect_block(e), | ||
133 | ast::Expr::LoopExpr(e) => { | ||
134 | let body = self.collect_block_opt(e.loop_body()); | ||
135 | self.alloc_expr(Expr::Loop { body }, syntax_ptr) | ||
136 | } | ||
137 | ast::Expr::WhileExpr(e) => { | ||
138 | let body = self.collect_block_opt(e.loop_body()); | ||
139 | |||
140 | let condition = match e.condition() { | ||
141 | None => self.exprs.alloc(Expr::Missing), | ||
142 | Some(condition) => match condition.pat() { | ||
143 | None => self.collect_expr_opt(condition.expr()), | ||
144 | // if let -- desugar to match | ||
145 | Some(pat) => { | ||
146 | tested_by!(infer_while_let); | ||
147 | let pat = self.collect_pat(pat); | ||
148 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
149 | let placeholder_pat = self.pats.alloc(Pat::Missing); | ||
150 | let break_ = self.exprs.alloc(Expr::Break { expr: None }); | ||
151 | let arms = vec![ | ||
152 | MatchArm { pats: vec![pat], expr: body, guard: None }, | ||
153 | MatchArm { pats: vec![placeholder_pat], expr: break_, guard: None }, | ||
154 | ]; | ||
155 | let match_expr = | ||
156 | self.exprs.alloc(Expr::Match { expr: match_expr, arms }); | ||
157 | return self.alloc_expr(Expr::Loop { body: match_expr }, syntax_ptr); | ||
158 | } | ||
159 | }, | ||
160 | }; | ||
161 | |||
162 | self.alloc_expr(Expr::While { condition, body }, syntax_ptr) | ||
163 | } | ||
164 | ast::Expr::ForExpr(e) => { | ||
165 | let iterable = self.collect_expr_opt(e.iterable()); | ||
166 | let pat = self.collect_pat_opt(e.pat()); | ||
167 | let body = self.collect_block_opt(e.loop_body()); | ||
168 | self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) | ||
169 | } | ||
170 | ast::Expr::CallExpr(e) => { | ||
171 | let callee = self.collect_expr_opt(e.expr()); | ||
172 | let args = if let Some(arg_list) = e.arg_list() { | ||
173 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
174 | } else { | ||
175 | Vec::new() | ||
176 | }; | ||
177 | self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) | ||
178 | } | ||
179 | ast::Expr::MethodCallExpr(e) => { | ||
180 | let receiver = self.collect_expr_opt(e.expr()); | ||
181 | let args = if let Some(arg_list) = e.arg_list() { | ||
182 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
183 | } else { | ||
184 | Vec::new() | ||
185 | }; | ||
186 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
187 | let generic_args = e.type_arg_list().and_then(GenericArgs::from_ast); | ||
188 | self.alloc_expr( | ||
189 | Expr::MethodCall { receiver, method_name, args, generic_args }, | ||
190 | syntax_ptr, | ||
191 | ) | ||
192 | } | ||
193 | ast::Expr::MatchExpr(e) => { | ||
194 | let expr = self.collect_expr_opt(e.expr()); | ||
195 | let arms = if let Some(match_arm_list) = e.match_arm_list() { | ||
196 | match_arm_list | ||
197 | .arms() | ||
198 | .map(|arm| MatchArm { | ||
199 | pats: arm.pats().map(|p| self.collect_pat(p)).collect(), | ||
200 | expr: self.collect_expr_opt(arm.expr()), | ||
201 | guard: arm | ||
202 | .guard() | ||
203 | .and_then(|guard| guard.expr()) | ||
204 | .map(|e| self.collect_expr(e)), | ||
205 | }) | ||
206 | .collect() | ||
207 | } else { | ||
208 | Vec::new() | ||
209 | }; | ||
210 | self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) | ||
211 | } | ||
212 | ast::Expr::PathExpr(e) => { | ||
213 | let path = | ||
214 | e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing); | ||
215 | self.alloc_expr(path, syntax_ptr) | ||
216 | } | ||
217 | ast::Expr::ContinueExpr(_e) => { | ||
218 | // FIXME: labels | ||
219 | self.alloc_expr(Expr::Continue, syntax_ptr) | ||
220 | } | ||
221 | ast::Expr::BreakExpr(e) => { | ||
222 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
223 | self.alloc_expr(Expr::Break { expr }, syntax_ptr) | ||
224 | } | ||
225 | ast::Expr::ParenExpr(e) => { | ||
226 | let inner = self.collect_expr_opt(e.expr()); | ||
227 | // make the paren expr point to the inner expression as well | ||
228 | self.source_map.expr_map.insert(Either::A(syntax_ptr), inner); | ||
229 | inner | ||
230 | } | ||
231 | ast::Expr::ReturnExpr(e) => { | ||
232 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
233 | self.alloc_expr(Expr::Return { expr }, syntax_ptr) | ||
234 | } | ||
235 | ast::Expr::RecordLit(e) => { | ||
236 | let path = e.path().and_then(Path::from_ast); | ||
237 | let mut field_ptrs = Vec::new(); | ||
238 | let record_lit = if let Some(nfl) = e.record_field_list() { | ||
239 | let fields = nfl | ||
240 | .fields() | ||
241 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) | ||
242 | .map(|field| RecordLitField { | ||
243 | name: field | ||
244 | .name_ref() | ||
245 | .map(|nr| nr.as_name()) | ||
246 | .unwrap_or_else(Name::missing), | ||
247 | expr: if let Some(e) = field.expr() { | ||
248 | self.collect_expr(e) | ||
249 | } else if let Some(nr) = field.name_ref() { | ||
250 | // field shorthand | ||
251 | let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(&nr))); | ||
252 | let ptr = Either::B(AstPtr::new(&field)); | ||
253 | self.source_map.expr_map.insert(ptr, id); | ||
254 | self.source_map.expr_map_back.insert(id, ptr); | ||
255 | id | ||
256 | } else { | ||
257 | self.exprs.alloc(Expr::Missing) | ||
258 | }, | ||
259 | }) | ||
260 | .collect(); | ||
261 | let spread = nfl.spread().map(|s| self.collect_expr(s)); | ||
262 | Expr::RecordLit { path, fields, spread } | ||
263 | } else { | ||
264 | Expr::RecordLit { path, fields: Vec::new(), spread: None } | ||
265 | }; | ||
266 | |||
267 | let res = self.alloc_expr(record_lit, syntax_ptr); | ||
268 | for (i, ptr) in field_ptrs.into_iter().enumerate() { | ||
269 | self.source_map.field_map.insert((res, i), ptr); | ||
270 | } | ||
271 | res | ||
272 | } | ||
273 | ast::Expr::FieldExpr(e) => { | ||
274 | let expr = self.collect_expr_opt(e.expr()); | ||
275 | let name = match e.field_access() { | ||
276 | Some(kind) => kind.as_name(), | ||
277 | _ => Name::missing(), | ||
278 | }; | ||
279 | self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) | ||
280 | } | ||
281 | ast::Expr::AwaitExpr(e) => { | ||
282 | let expr = self.collect_expr_opt(e.expr()); | ||
283 | self.alloc_expr(Expr::Await { expr }, syntax_ptr) | ||
284 | } | ||
285 | ast::Expr::TryExpr(e) => { | ||
286 | let expr = self.collect_expr_opt(e.expr()); | ||
287 | self.alloc_expr(Expr::Try { expr }, syntax_ptr) | ||
288 | } | ||
289 | ast::Expr::CastExpr(e) => { | ||
290 | let expr = self.collect_expr_opt(e.expr()); | ||
291 | let type_ref = TypeRef::from_ast_opt(e.type_ref()); | ||
292 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) | ||
293 | } | ||
294 | ast::Expr::RefExpr(e) => { | ||
295 | let expr = self.collect_expr_opt(e.expr()); | ||
296 | let mutability = Mutability::from_mutable(e.is_mut()); | ||
297 | self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) | ||
298 | } | ||
299 | ast::Expr::PrefixExpr(e) => { | ||
300 | let expr = self.collect_expr_opt(e.expr()); | ||
301 | if let Some(op) = e.op_kind() { | ||
302 | self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) | ||
303 | } else { | ||
304 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
305 | } | ||
306 | } | ||
307 | ast::Expr::LambdaExpr(e) => { | ||
308 | let mut args = Vec::new(); | ||
309 | let mut arg_types = Vec::new(); | ||
310 | if let Some(pl) = e.param_list() { | ||
311 | for param in pl.params() { | ||
312 | let pat = self.collect_pat_opt(param.pat()); | ||
313 | let type_ref = param.ascribed_type().map(TypeRef::from_ast); | ||
314 | args.push(pat); | ||
315 | arg_types.push(type_ref); | ||
316 | } | ||
317 | } | ||
318 | let body = self.collect_expr_opt(e.body()); | ||
319 | self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) | ||
320 | } | ||
321 | ast::Expr::BinExpr(e) => { | ||
322 | let lhs = self.collect_expr_opt(e.lhs()); | ||
323 | let rhs = self.collect_expr_opt(e.rhs()); | ||
324 | let op = e.op_kind().map(BinaryOp::from); | ||
325 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) | ||
326 | } | ||
327 | ast::Expr::TupleExpr(e) => { | ||
328 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | ||
329 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | ||
330 | } | ||
331 | |||
332 | ast::Expr::ArrayExpr(e) => { | ||
333 | let kind = e.kind(); | ||
334 | |||
335 | match kind { | ||
336 | ArrayExprKind::ElementList(e) => { | ||
337 | let exprs = e.map(|expr| self.collect_expr(expr)).collect(); | ||
338 | self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr) | ||
339 | } | ||
340 | ArrayExprKind::Repeat { initializer, repeat } => { | ||
341 | let initializer = self.collect_expr_opt(initializer); | ||
342 | let repeat = self.collect_expr_opt(repeat); | ||
343 | self.alloc_expr( | ||
344 | Expr::Array(Array::Repeat { initializer, repeat }), | ||
345 | syntax_ptr, | ||
346 | ) | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | |||
351 | ast::Expr::Literal(e) => { | ||
352 | let lit = match e.kind() { | ||
353 | LiteralKind::IntNumber { suffix } => { | ||
354 | let known_name = suffix | ||
355 | .and_then(|it| IntTy::from_suffix(&it).map(UncertainIntTy::Known)); | ||
356 | |||
357 | Literal::Int( | ||
358 | Default::default(), | ||
359 | known_name.unwrap_or(UncertainIntTy::Unknown), | ||
360 | ) | ||
361 | } | ||
362 | LiteralKind::FloatNumber { suffix } => { | ||
363 | let known_name = suffix | ||
364 | .and_then(|it| FloatTy::from_suffix(&it).map(UncertainFloatTy::Known)); | ||
365 | |||
366 | Literal::Float( | ||
367 | Default::default(), | ||
368 | known_name.unwrap_or(UncertainFloatTy::Unknown), | ||
369 | ) | ||
370 | } | ||
371 | LiteralKind::ByteString => Literal::ByteString(Default::default()), | ||
372 | LiteralKind::String => Literal::String(Default::default()), | ||
373 | LiteralKind::Byte => { | ||
374 | Literal::Int(Default::default(), UncertainIntTy::Known(IntTy::u8())) | ||
375 | } | ||
376 | LiteralKind::Bool => Literal::Bool(Default::default()), | ||
377 | LiteralKind::Char => Literal::Char(Default::default()), | ||
378 | }; | ||
379 | self.alloc_expr(Expr::Literal(lit), syntax_ptr) | ||
380 | } | ||
381 | ast::Expr::IndexExpr(e) => { | ||
382 | let base = self.collect_expr_opt(e.base()); | ||
383 | let index = self.collect_expr_opt(e.index()); | ||
384 | self.alloc_expr(Expr::Index { base, index }, syntax_ptr) | ||
385 | } | ||
386 | |||
387 | // FIXME implement HIR for these: | ||
388 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
389 | ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
390 | ast::Expr::MacroCall(e) => { | ||
391 | let ast_id = self | ||
392 | .db | ||
393 | .ast_id_map(self.current_file_id) | ||
394 | .ast_id(&e) | ||
395 | .with_file_id(self.current_file_id); | ||
396 | |||
397 | if let Some(path) = e.path().and_then(Path::from_ast) { | ||
398 | if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &path) { | ||
399 | let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db); | ||
400 | let file_id = call_id.as_file(MacroFileKind::Expr); | ||
401 | if let Some(node) = self.db.parse_or_expand(file_id) { | ||
402 | if let Some(expr) = ast::Expr::cast(node) { | ||
403 | log::debug!("macro expansion {:#?}", expr.syntax()); | ||
404 | let old_file_id = | ||
405 | std::mem::replace(&mut self.current_file_id, file_id); | ||
406 | let id = self.collect_expr(expr); | ||
407 | self.current_file_id = old_file_id; | ||
408 | return id; | ||
409 | } | ||
410 | } | ||
411 | } | ||
412 | } | ||
413 | // FIXME: Instead of just dropping the error from expansion | ||
414 | // report it | ||
415 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
416 | } | ||
417 | } | ||
418 | } | ||
419 | |||
420 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | ||
421 | if let Some(expr) = expr { | ||
422 | self.collect_expr(expr) | ||
423 | } else { | ||
424 | self.exprs.alloc(Expr::Missing) | ||
425 | } | ||
426 | } | ||
427 | |||
428 | fn collect_block(&mut self, expr: ast::BlockExpr) -> ExprId { | ||
429 | let syntax_node_ptr = AstPtr::new(&expr.clone().into()); | ||
430 | let block = match expr.block() { | ||
431 | Some(block) => block, | ||
432 | None => return self.alloc_expr(Expr::Missing, syntax_node_ptr), | ||
433 | }; | ||
434 | let statements = block | ||
435 | .statements() | ||
436 | .map(|s| match s { | ||
437 | ast::Stmt::LetStmt(stmt) => { | ||
438 | let pat = self.collect_pat_opt(stmt.pat()); | ||
439 | let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); | ||
440 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | ||
441 | Statement::Let { pat, type_ref, initializer } | ||
442 | } | ||
443 | ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), | ||
444 | }) | ||
445 | .collect(); | ||
446 | let tail = block.expr().map(|e| self.collect_expr(e)); | ||
447 | self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr) | ||
448 | } | ||
449 | |||
450 | fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { | ||
451 | if let Some(block) = expr { | ||
452 | self.collect_block(block) | ||
453 | } else { | ||
454 | self.exprs.alloc(Expr::Missing) | ||
455 | } | ||
456 | } | ||
457 | |||
458 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { | ||
459 | let pattern = match &pat { | ||
460 | ast::Pat::BindPat(bp) => { | ||
461 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
462 | let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); | ||
463 | let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); | ||
464 | Pat::Bind { name, mode: annotation, subpat } | ||
465 | } | ||
466 | ast::Pat::TupleStructPat(p) => { | ||
467 | let path = p.path().and_then(Path::from_ast); | ||
468 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
469 | Pat::TupleStruct { path, args } | ||
470 | } | ||
471 | ast::Pat::RefPat(p) => { | ||
472 | let pat = self.collect_pat_opt(p.pat()); | ||
473 | let mutability = Mutability::from_mutable(p.is_mut()); | ||
474 | Pat::Ref { pat, mutability } | ||
475 | } | ||
476 | ast::Pat::PathPat(p) => { | ||
477 | let path = p.path().and_then(Path::from_ast); | ||
478 | path.map(Pat::Path).unwrap_or(Pat::Missing) | ||
479 | } | ||
480 | ast::Pat::TuplePat(p) => { | ||
481 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
482 | Pat::Tuple(args) | ||
483 | } | ||
484 | ast::Pat::PlaceholderPat(_) => Pat::Wild, | ||
485 | ast::Pat::RecordPat(p) => { | ||
486 | let path = p.path().and_then(Path::from_ast); | ||
487 | let record_field_pat_list = | ||
488 | p.record_field_pat_list().expect("every struct should have a field list"); | ||
489 | let mut fields: Vec<_> = record_field_pat_list | ||
490 | .bind_pats() | ||
491 | .filter_map(|bind_pat| { | ||
492 | let ast_pat = | ||
493 | ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat"); | ||
494 | let pat = self.collect_pat(ast_pat); | ||
495 | let name = bind_pat.name()?.as_name(); | ||
496 | Some(RecordFieldPat { name, pat }) | ||
497 | }) | ||
498 | .collect(); | ||
499 | let iter = record_field_pat_list.record_field_pats().filter_map(|f| { | ||
500 | let ast_pat = f.pat()?; | ||
501 | let pat = self.collect_pat(ast_pat); | ||
502 | let name = f.name()?.as_name(); | ||
503 | Some(RecordFieldPat { name, pat }) | ||
504 | }); | ||
505 | fields.extend(iter); | ||
506 | |||
507 | Pat::Struct { path, args: fields } | ||
508 | } | ||
509 | |||
510 | // FIXME: implement | ||
511 | ast::Pat::BoxPat(_) => Pat::Missing, | ||
512 | ast::Pat::LiteralPat(_) => Pat::Missing, | ||
513 | ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing, | ||
514 | }; | ||
515 | let ptr = AstPtr::new(&pat); | ||
516 | self.alloc_pat(pattern, Either::A(ptr)) | ||
517 | } | ||
518 | |||
519 | fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { | ||
520 | if let Some(pat) = pat { | ||
521 | self.collect_pat(pat) | ||
522 | } else { | ||
523 | self.pats.alloc(Pat::Missing) | ||
524 | } | ||
525 | } | ||
526 | |||
527 | fn collect_const_body(&mut self, node: ast::ConstDef) { | ||
528 | let body = self.collect_expr_opt(node.body()); | ||
529 | self.body_expr = Some(body); | ||
530 | } | ||
531 | |||
532 | fn collect_static_body(&mut self, node: ast::StaticDef) { | ||
533 | let body = self.collect_expr_opt(node.body()); | ||
534 | self.body_expr = Some(body); | ||
535 | } | ||
536 | |||
537 | fn collect_fn_body(&mut self, node: ast::FnDef) { | ||
538 | if let Some(param_list) = node.param_list() { | ||
539 | if let Some(self_param) = param_list.self_param() { | ||
540 | let ptr = AstPtr::new(&self_param); | ||
541 | let param_pat = self.alloc_pat( | ||
542 | Pat::Bind { | ||
543 | name: SELF_PARAM, | ||
544 | mode: BindingAnnotation::Unannotated, | ||
545 | subpat: None, | ||
546 | }, | ||
547 | Either::B(ptr), | ||
548 | ); | ||
549 | self.params.push(param_pat); | ||
550 | } | ||
551 | |||
552 | for param in param_list.params() { | ||
553 | let pat = if let Some(pat) = param.pat() { | ||
554 | pat | ||
555 | } else { | ||
556 | continue; | ||
557 | }; | ||
558 | let param_pat = self.collect_pat(pat); | ||
559 | self.params.push(param_pat); | ||
560 | } | ||
561 | }; | ||
562 | |||
563 | let body = self.collect_block_opt(node.body()); | ||
564 | self.body_expr = Some(body); | ||
565 | } | ||
566 | |||
567 | fn finish(self) -> (Body, BodySourceMap) { | ||
568 | let body = Body { | ||
569 | owner: self.owner, | ||
570 | exprs: self.exprs, | ||
571 | pats: self.pats, | ||
572 | params: self.params, | ||
573 | body_expr: self.body_expr.expect("A body should have been collected"), | ||
574 | }; | ||
575 | (body, self.source_map) | ||
576 | } | ||
577 | } | ||
578 | |||
579 | impl From<ast::BinOp> for BinaryOp { | ||
580 | fn from(ast_op: ast::BinOp) -> Self { | ||
581 | match ast_op { | ||
582 | ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or), | ||
583 | ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And), | ||
584 | ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }), | ||
585 | ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }), | ||
586 | ast::BinOp::LesserEqualTest => { | ||
587 | BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false }) | ||
588 | } | ||
589 | ast::BinOp::GreaterEqualTest => { | ||
590 | BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false }) | ||
591 | } | ||
592 | ast::BinOp::LesserTest => { | ||
593 | BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true }) | ||
594 | } | ||
595 | ast::BinOp::GreaterTest => { | ||
596 | BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true }) | ||
597 | } | ||
598 | ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add), | ||
599 | ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul), | ||
600 | ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub), | ||
601 | ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div), | ||
602 | ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem), | ||
603 | ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl), | ||
604 | ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr), | ||
605 | ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor), | ||
606 | ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr), | ||
607 | ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd), | ||
608 | ast::BinOp::Assignment => BinaryOp::Assignment { op: None }, | ||
609 | ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) }, | ||
610 | ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) }, | ||
611 | ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) }, | ||
612 | ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) }, | ||
613 | ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) }, | ||
614 | ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) }, | ||
615 | ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) }, | ||
616 | ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) }, | ||
617 | ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) }, | ||
618 | ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) }, | ||
619 | } | ||
620 | } | ||
621 | } | ||
622 | |||
623 | pub(crate) fn body_with_source_map_query( | ||
624 | db: &impl HirDatabase, | ||
625 | def: DefWithBody, | ||
626 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | ||
627 | let mut collector; | ||
628 | |||
629 | match def { | ||
630 | DefWithBody::Const(ref c) => { | ||
631 | let src = c.source(db); | ||
632 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | ||
633 | collector.collect_const_body(src.ast) | ||
634 | } | ||
635 | DefWithBody::Function(ref f) => { | ||
636 | let src = f.source(db); | ||
637 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | ||
638 | collector.collect_fn_body(src.ast) | ||
639 | } | ||
640 | DefWithBody::Static(ref s) => { | ||
641 | let src = s.source(db); | ||
642 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | ||
643 | collector.collect_static_body(src.ast) | ||
644 | } | ||
645 | } | ||
646 | |||
647 | let (body, source_map) = collector.finish(); | ||
648 | (Arc::new(body), Arc::new(source_map)) | ||
649 | } | ||
650 | |||
651 | pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { | ||
652 | db.body_with_source_map(def).0 | ||
653 | } | ||