]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/body/lower.rs
Use block_def_map in body lowering
[rust.git] / crates / hir_def / src / body / lower.rs
1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2 //! representation.
3
4 use std::{any::type_name, mem, sync::Arc};
5
6 use either::Either;
7 use hir_expand::{
8     hygiene::Hygiene,
9     name::{name, AsName, Name},
10     ExpandError, HirFileId, MacroDefId, MacroDefKind,
11 };
12 use la_arena::Arena;
13 use profile::Count;
14 use rustc_hash::FxHashMap;
15 use syntax::{
16     ast::{
17         self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner,
18         SlicePatComponents,
19     },
20     AstNode, AstPtr, SyntaxNodePtr,
21 };
22 use test_utils::mark;
23
24 use crate::{
25     adt::StructKind,
26     body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax},
27     builtin_type::{BuiltinFloat, BuiltinInt},
28     db::DefDatabase,
29     diagnostics::{InactiveCode, MacroError, UnresolvedProcMacro},
30     expr::{
31         dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label,
32         LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField,
33         Statement,
34     },
35     item_scope::BuiltinShadowMode,
36     item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
37     path::{GenericArgs, Path},
38     type_ref::{Mutability, Rawness, TypeRef},
39     AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern,
40     ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
41 };
42
43 use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
44
45 pub(crate) struct LowerCtx {
46     hygiene: Hygiene,
47 }
48
49 impl LowerCtx {
50     pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
51         LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
52     }
53     pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self {
54         LowerCtx { hygiene: hygiene.clone() }
55     }
56
57     pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
58         Path::from_src(ast, &self.hygiene)
59     }
60 }
61
62 pub(super) fn lower(
63     db: &dyn DefDatabase,
64     def: DefWithBodyId,
65     expander: Expander,
66     params: Option<ast::ParamList>,
67     body: Option<ast::Expr>,
68 ) -> (Body, BodySourceMap) {
69     let item_tree = db.item_tree(expander.current_file_id);
70     ExprCollector {
71         db,
72         def,
73         source_map: BodySourceMap::default(),
74         body: Body {
75             exprs: Arena::default(),
76             pats: Arena::default(),
77             labels: Arena::default(),
78             params: Vec::new(),
79             body_expr: dummy_expr_id(),
80             item_scope: Default::default(),
81             _c: Count::new(),
82         },
83         item_trees: {
84             let mut map = FxHashMap::default();
85             map.insert(expander.current_file_id, item_tree);
86             map
87         },
88         expander,
89     }
90     .collect(params, body)
91 }
92
93 struct ExprCollector<'a> {
94     db: &'a dyn DefDatabase,
95     def: DefWithBodyId,
96     expander: Expander,
97     body: Body,
98     source_map: BodySourceMap,
99
100     item_trees: FxHashMap<HirFileId, Arc<ItemTree>>,
101 }
102
103 impl ExprCollector<'_> {
104     fn collect(
105         mut self,
106         param_list: Option<ast::ParamList>,
107         body: Option<ast::Expr>,
108     ) -> (Body, BodySourceMap) {
109         if let Some(param_list) = param_list {
110             if let Some(self_param) = param_list.self_param() {
111                 let ptr = AstPtr::new(&self_param);
112                 let param_pat = self.alloc_pat(
113                     Pat::Bind {
114                         name: name![self],
115                         mode: BindingAnnotation::new(
116                             self_param.mut_token().is_some() && self_param.amp_token().is_none(),
117                             false,
118                         ),
119                         subpat: None,
120                     },
121                     Either::Right(ptr),
122                 );
123                 self.body.params.push(param_pat);
124             }
125
126             for param in param_list.params() {
127                 let pat = match param.pat() {
128                     None => continue,
129                     Some(pat) => pat,
130                 };
131                 let param_pat = self.collect_pat(pat);
132                 self.body.params.push(param_pat);
133             }
134         };
135
136         self.body.body_expr = self.collect_expr_opt(body);
137         (self.body, self.source_map)
138     }
139
140     fn ctx(&self) -> LowerCtx {
141         LowerCtx::new(self.db, self.expander.current_file_id)
142     }
143
144     fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
145         let src = self.expander.to_source(ptr);
146         let id = self.make_expr(expr, Ok(src.clone()));
147         self.source_map.expr_map.insert(src, id);
148         id
149     }
150     // desugared exprs don't have ptr, that's wrong and should be fixed
151     // somehow.
152     fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
153         self.make_expr(expr, Err(SyntheticSyntax))
154     }
155     fn unit(&mut self) -> ExprId {
156         self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
157     }
158     fn missing_expr(&mut self) -> ExprId {
159         self.alloc_expr_desugared(Expr::Missing)
160     }
161     fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
162         let id = self.body.exprs.alloc(expr);
163         self.source_map.expr_map_back.insert(id, src);
164         id
165     }
166
167     fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
168         let src = self.expander.to_source(ptr);
169         let id = self.make_pat(pat, Ok(src.clone()));
170         self.source_map.pat_map.insert(src, id);
171         id
172     }
173     fn missing_pat(&mut self) -> PatId {
174         self.make_pat(Pat::Missing, Err(SyntheticSyntax))
175     }
176     fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
177         let id = self.body.pats.alloc(pat);
178         self.source_map.pat_map_back.insert(id, src);
179         id
180     }
181
182     fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
183         let src = self.expander.to_source(ptr);
184         let id = self.make_label(label, src.clone());
185         self.source_map.label_map.insert(src, id);
186         id
187     }
188     fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId {
189         let id = self.body.labels.alloc(label);
190         self.source_map.label_map_back.insert(id, src);
191         id
192     }
193
194     fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
195         let syntax_ptr = AstPtr::new(&expr);
196         if self.check_cfg(&expr).is_none() {
197             return self.missing_expr();
198         }
199
200         match expr {
201             ast::Expr::IfExpr(e) => {
202                 let then_branch = self.collect_block_opt(e.then_branch());
203
204                 let else_branch = e.else_branch().map(|b| match b {
205                     ast::ElseBranch::Block(it) => self.collect_block(it),
206                     ast::ElseBranch::IfExpr(elif) => {
207                         let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
208                         self.collect_expr(expr)
209                     }
210                 });
211
212                 let condition = match e.condition() {
213                     None => self.missing_expr(),
214                     Some(condition) => match condition.pat() {
215                         None => self.collect_expr_opt(condition.expr()),
216                         // if let -- desugar to match
217                         Some(pat) => {
218                             let pat = self.collect_pat(pat);
219                             let match_expr = self.collect_expr_opt(condition.expr());
220                             let placeholder_pat = self.missing_pat();
221                             let arms = vec![
222                                 MatchArm { pat, expr: then_branch, guard: None },
223                                 MatchArm {
224                                     pat: placeholder_pat,
225                                     expr: else_branch.unwrap_or_else(|| self.unit()),
226                                     guard: None,
227                                 },
228                             ];
229                             return self
230                                 .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
231                         }
232                     },
233                 };
234
235                 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
236             }
237             ast::Expr::EffectExpr(e) => match e.effect() {
238                 ast::Effect::Try(_) => {
239                     let body = self.collect_block_opt(e.block_expr());
240                     self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
241                 }
242                 ast::Effect::Unsafe(_) => {
243                     let body = self.collect_block_opt(e.block_expr());
244                     self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
245                 }
246                 // FIXME: we need to record these effects somewhere...
247                 ast::Effect::Label(label) => {
248                     let label = self.collect_label(label);
249                     match e.block_expr() {
250                         Some(block) => {
251                             let res = self.collect_block(block);
252                             match &mut self.body.exprs[res] {
253                                 Expr::Block { label: block_label, .. } => {
254                                     *block_label = Some(label);
255                                 }
256                                 _ => unreachable!(),
257                             }
258                             res
259                         }
260                         None => self.missing_expr(),
261                     }
262                 }
263                 // FIXME: we need to record these effects somewhere...
264                 ast::Effect::Async(_) => {
265                     let body = self.collect_block_opt(e.block_expr());
266                     self.alloc_expr(Expr::Async { body }, syntax_ptr)
267                 }
268                 ast::Effect::Const(_) => {
269                     let body = self.collect_block_opt(e.block_expr());
270                     self.alloc_expr(Expr::Const { body }, syntax_ptr)
271                 }
272             },
273             ast::Expr::BlockExpr(e) => self.collect_block(e),
274             ast::Expr::LoopExpr(e) => {
275                 let label = e.label().map(|label| self.collect_label(label));
276                 let body = self.collect_block_opt(e.loop_body());
277                 self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
278             }
279             ast::Expr::WhileExpr(e) => {
280                 let label = e.label().map(|label| self.collect_label(label));
281                 let body = self.collect_block_opt(e.loop_body());
282
283                 let condition = match e.condition() {
284                     None => self.missing_expr(),
285                     Some(condition) => match condition.pat() {
286                         None => self.collect_expr_opt(condition.expr()),
287                         // if let -- desugar to match
288                         Some(pat) => {
289                             mark::hit!(infer_resolve_while_let);
290                             let pat = self.collect_pat(pat);
291                             let match_expr = self.collect_expr_opt(condition.expr());
292                             let placeholder_pat = self.missing_pat();
293                             let break_ =
294                                 self.alloc_expr_desugared(Expr::Break { expr: None, label: None });
295                             let arms = vec![
296                                 MatchArm { pat, expr: body, guard: None },
297                                 MatchArm { pat: placeholder_pat, expr: break_, guard: None },
298                             ];
299                             let match_expr =
300                                 self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
301                             return self
302                                 .alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr);
303                         }
304                     },
305                 };
306
307                 self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
308             }
309             ast::Expr::ForExpr(e) => {
310                 let label = e.label().map(|label| self.collect_label(label));
311                 let iterable = self.collect_expr_opt(e.iterable());
312                 let pat = self.collect_pat_opt(e.pat());
313                 let body = self.collect_block_opt(e.loop_body());
314                 self.alloc_expr(Expr::For { iterable, pat, body, label }, syntax_ptr)
315             }
316             ast::Expr::CallExpr(e) => {
317                 let callee = self.collect_expr_opt(e.expr());
318                 let args = if let Some(arg_list) = e.arg_list() {
319                     arg_list.args().map(|e| self.collect_expr(e)).collect()
320                 } else {
321                     Vec::new()
322                 };
323                 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
324             }
325             ast::Expr::MethodCallExpr(e) => {
326                 let receiver = self.collect_expr_opt(e.receiver());
327                 let args = if let Some(arg_list) = e.arg_list() {
328                     arg_list.args().map(|e| self.collect_expr(e)).collect()
329                 } else {
330                     Vec::new()
331                 };
332                 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
333                 let generic_args =
334                     e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it));
335                 self.alloc_expr(
336                     Expr::MethodCall { receiver, method_name, args, generic_args },
337                     syntax_ptr,
338                 )
339             }
340             ast::Expr::MatchExpr(e) => {
341                 let expr = self.collect_expr_opt(e.expr());
342                 let arms = if let Some(match_arm_list) = e.match_arm_list() {
343                     match_arm_list
344                         .arms()
345                         .filter_map(|arm| {
346                             self.check_cfg(&arm).map(|()| MatchArm {
347                                 pat: self.collect_pat_opt(arm.pat()),
348                                 expr: self.collect_expr_opt(arm.expr()),
349                                 guard: arm
350                                     .guard()
351                                     .and_then(|guard| guard.expr())
352                                     .map(|e| self.collect_expr(e)),
353                             })
354                         })
355                         .collect()
356                 } else {
357                     Vec::new()
358                 };
359                 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
360             }
361             ast::Expr::PathExpr(e) => {
362                 let path = e
363                     .path()
364                     .and_then(|path| self.expander.parse_path(path))
365                     .map(Expr::Path)
366                     .unwrap_or(Expr::Missing);
367                 self.alloc_expr(path, syntax_ptr)
368             }
369             ast::Expr::ContinueExpr(e) => self.alloc_expr(
370                 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
371                 syntax_ptr,
372             ),
373             ast::Expr::BreakExpr(e) => {
374                 let expr = e.expr().map(|e| self.collect_expr(e));
375                 self.alloc_expr(
376                     Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
377                     syntax_ptr,
378                 )
379             }
380             ast::Expr::ParenExpr(e) => {
381                 let inner = self.collect_expr_opt(e.expr());
382                 // make the paren expr point to the inner expression as well
383                 let src = self.expander.to_source(syntax_ptr);
384                 self.source_map.expr_map.insert(src, inner);
385                 inner
386             }
387             ast::Expr::ReturnExpr(e) => {
388                 let expr = e.expr().map(|e| self.collect_expr(e));
389                 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
390             }
391             ast::Expr::YieldExpr(e) => {
392                 let expr = e.expr().map(|e| self.collect_expr(e));
393                 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
394             }
395             ast::Expr::RecordExpr(e) => {
396                 let path = e.path().and_then(|path| self.expander.parse_path(path));
397                 let mut field_ptrs = Vec::new();
398                 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
399                     let fields = nfl
400                         .fields()
401                         .inspect(|field| field_ptrs.push(AstPtr::new(field)))
402                         .filter_map(|field| {
403                             self.check_cfg(&field)?;
404
405                             let name = field.field_name()?.as_name();
406
407                             Some(RecordLitField {
408                                 name,
409                                 expr: match field.expr() {
410                                     Some(e) => self.collect_expr(e),
411                                     None => self.missing_expr(),
412                                 },
413                             })
414                         })
415                         .collect();
416                     let spread = nfl.spread().map(|s| self.collect_expr(s));
417                     Expr::RecordLit { path, fields, spread }
418                 } else {
419                     Expr::RecordLit { path, fields: Vec::new(), spread: None }
420                 };
421
422                 let res = self.alloc_expr(record_lit, syntax_ptr);
423                 for (i, ptr) in field_ptrs.into_iter().enumerate() {
424                     let src = self.expander.to_source(ptr);
425                     self.source_map.field_map.insert((res, i), src);
426                 }
427                 res
428             }
429             ast::Expr::FieldExpr(e) => {
430                 let expr = self.collect_expr_opt(e.expr());
431                 let name = match e.field_access() {
432                     Some(kind) => kind.as_name(),
433                     _ => Name::missing(),
434                 };
435                 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
436             }
437             ast::Expr::AwaitExpr(e) => {
438                 let expr = self.collect_expr_opt(e.expr());
439                 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
440             }
441             ast::Expr::TryExpr(e) => {
442                 let expr = self.collect_expr_opt(e.expr());
443                 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
444             }
445             ast::Expr::CastExpr(e) => {
446                 let expr = self.collect_expr_opt(e.expr());
447                 let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
448                 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
449             }
450             ast::Expr::RefExpr(e) => {
451                 let expr = self.collect_expr_opt(e.expr());
452                 let raw_tok = e.raw_token().is_some();
453                 let mutability = if raw_tok {
454                     if e.mut_token().is_some() {
455                         Mutability::Mut
456                     } else if e.const_token().is_some() {
457                         Mutability::Shared
458                     } else {
459                         unreachable!("parser only remaps to raw_token() if matching mutability token follows")
460                     }
461                 } else {
462                     Mutability::from_mutable(e.mut_token().is_some())
463                 };
464                 let rawness = Rawness::from_raw(raw_tok);
465                 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
466             }
467             ast::Expr::PrefixExpr(e) => {
468                 let expr = self.collect_expr_opt(e.expr());
469                 if let Some(op) = e.op_kind() {
470                     self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
471                 } else {
472                     self.alloc_expr(Expr::Missing, syntax_ptr)
473                 }
474             }
475             ast::Expr::ClosureExpr(e) => {
476                 let mut args = Vec::new();
477                 let mut arg_types = Vec::new();
478                 if let Some(pl) = e.param_list() {
479                     for param in pl.params() {
480                         let pat = self.collect_pat_opt(param.pat());
481                         let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
482                         args.push(pat);
483                         arg_types.push(type_ref);
484                     }
485                 }
486                 let ret_type =
487                     e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it));
488                 let body = self.collect_expr_opt(e.body());
489                 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
490             }
491             ast::Expr::BinExpr(e) => {
492                 let lhs = self.collect_expr_opt(e.lhs());
493                 let rhs = self.collect_expr_opt(e.rhs());
494                 let op = e.op_kind().map(BinaryOp::from);
495                 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
496             }
497             ast::Expr::TupleExpr(e) => {
498                 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
499                 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
500             }
501             ast::Expr::BoxExpr(e) => {
502                 let expr = self.collect_expr_opt(e.expr());
503                 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
504             }
505
506             ast::Expr::ArrayExpr(e) => {
507                 let kind = e.kind();
508
509                 match kind {
510                     ArrayExprKind::ElementList(e) => {
511                         let exprs = e.map(|expr| self.collect_expr(expr)).collect();
512                         self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
513                     }
514                     ArrayExprKind::Repeat { initializer, repeat } => {
515                         let initializer = self.collect_expr_opt(initializer);
516                         let repeat = self.collect_expr_opt(repeat);
517                         self.alloc_expr(
518                             Expr::Array(Array::Repeat { initializer, repeat }),
519                             syntax_ptr,
520                         )
521                     }
522                 }
523             }
524
525             ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
526             ast::Expr::IndexExpr(e) => {
527                 let base = self.collect_expr_opt(e.base());
528                 let index = self.collect_expr_opt(e.index());
529                 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
530             }
531             ast::Expr::RangeExpr(e) => {
532                 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
533                 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
534                 match e.op_kind() {
535                     Some(range_type) => {
536                         self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
537                     }
538                     None => self.alloc_expr(Expr::Missing, syntax_ptr),
539                 }
540             }
541             ast::Expr::MacroCall(e) => {
542                 let mut ids = vec![];
543                 self.collect_macro_call(e, syntax_ptr.clone(), |this, expansion| {
544                     ids.push(match expansion {
545                         Some(it) => this.collect_expr(it),
546                         None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
547                     })
548                 });
549                 ids[0]
550             }
551         }
552     }
553
554     fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
555         &mut self,
556         e: ast::MacroCall,
557         syntax_ptr: AstPtr<ast::Expr>,
558         mut collector: F,
559     ) {
560         // File containing the macro call. Expansion errors will be attached here.
561         let outer_file = self.expander.current_file_id;
562
563         let macro_call = self.expander.to_source(AstPtr::new(&e));
564         let res = self.expander.enter_expand(self.db, e);
565
566         match &res.err {
567             Some(ExpandError::UnresolvedProcMacro) => {
568                 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro(
569                     UnresolvedProcMacro {
570                         file: outer_file,
571                         node: syntax_ptr.into(),
572                         precise_location: None,
573                         macro_name: None,
574                     },
575                 ));
576             }
577             Some(err) => {
578                 self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError {
579                     file: outer_file,
580                     node: syntax_ptr.into(),
581                     message: err.to_string(),
582                 }));
583             }
584             None => {}
585         }
586
587         match res.value {
588             Some((mark, expansion)) => {
589                 // FIXME: Statements are too complicated to recover from error for now.
590                 // It is because we don't have any hygiene for local variable expansion right now.
591                 if T::can_cast(syntax::SyntaxKind::MACRO_STMTS) && res.err.is_some() {
592                     self.expander.exit(self.db, mark);
593                     collector(self, None);
594                 } else {
595                     self.source_map.expansions.insert(macro_call, self.expander.current_file_id);
596
597                     let item_tree = self.db.item_tree(self.expander.current_file_id);
598                     self.item_trees.insert(self.expander.current_file_id, item_tree);
599
600                     let id = collector(self, Some(expansion));
601                     self.expander.exit(self.db, mark);
602                     id
603                 }
604             }
605             None => collector(self, None),
606         }
607     }
608
609     fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> Option<ItemTreeId<N>> {
610         let id = self.expander.ast_id(ast);
611         let tree = &self.item_trees[&id.file_id];
612
613         // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes
614
615         // Root file (non-macro).
616         let item_tree_id = tree
617             .all_inner_items()
618             .chain(tree.top_level_items().iter().copied())
619             .filter_map(|mod_item| mod_item.downcast::<N>())
620             .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast())
621             .or_else(|| {
622                 log::debug!(
623                     "couldn't find inner {} item for {:?} (AST: `{}` - {:?})",
624                     type_name::<N>(),
625                     id,
626                     ast.syntax(),
627                     ast.syntax(),
628                 );
629                 None
630             })?;
631
632         Some(ItemTreeId::new(id.file_id, item_tree_id))
633     }
634
635     fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
636         if let Some(expr) = expr {
637             self.collect_expr(expr)
638         } else {
639             self.missing_expr()
640         }
641     }
642
643     fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
644         let stmt =
645             match s {
646                 ast::Stmt::LetStmt(stmt) => {
647                     self.check_cfg(&stmt)?;
648
649                     let pat = self.collect_pat_opt(stmt.pat());
650                     let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
651                     let initializer = stmt.initializer().map(|e| self.collect_expr(e));
652                     vec![Statement::Let { pat, type_ref, initializer }]
653                 }
654                 ast::Stmt::ExprStmt(stmt) => {
655                     self.check_cfg(&stmt)?;
656
657                     // Note that macro could be expended to multiple statements
658                     if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
659                         let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());
660                         let mut stmts = vec![];
661
662                         self.collect_macro_call(m, syntax_ptr.clone(), |this, expansion| {
663                             match expansion {
664                                 Some(expansion) => {
665                                     let statements: ast::MacroStmts = expansion;
666                                     this.collect_stmts_items(statements.statements());
667
668                                     statements.statements().for_each(|stmt| {
669                                         if let Some(mut r) = this.collect_stmt(stmt) {
670                                             stmts.append(&mut r);
671                                         }
672                                     });
673                                     if let Some(expr) = statements.expr() {
674                                         stmts.push(Statement::Expr(this.collect_expr(expr)));
675                                     }
676                                 }
677                                 None => {
678                                     stmts.push(Statement::Expr(
679                                         this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
680                                     ));
681                                 }
682                             }
683                         });
684                         stmts
685                     } else {
686                         vec![Statement::Expr(self.collect_expr_opt(stmt.expr()))]
687                     }
688                 }
689                 ast::Stmt::Item(item) => {
690                     self.check_cfg(&item)?;
691
692                     return None;
693                 }
694             };
695
696         Some(stmt)
697     }
698
699     fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
700         let ast_id = self.expander.ast_id(&block);
701         let block_loc = BlockLoc { ast_id, module: self.expander.module };
702         let block_id = self.db.intern_block(block_loc);
703         let def_map = self.db.block_def_map(block_id);
704         let root = def_map.module_id(def_map.root());
705         let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
706         let prev_module = mem::replace(&mut self.expander.module, root);
707
708         self.collect_stmts_items(block.statements());
709         let statements =
710             block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
711         let tail = block.tail_expr().map(|e| self.collect_expr(e));
712         let syntax_node_ptr = AstPtr::new(&block.clone().into());
713         let expr_id = self.alloc_expr(
714             Expr::Block { id: block_id, statements, tail, label: None },
715             syntax_node_ptr,
716         );
717
718         self.expander.def_map = prev_def_map;
719         self.expander.module = prev_module;
720         expr_id
721     }
722
723     fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
724         let container = ContainerId::DefWithBodyId(self.def);
725
726         let items = stmts
727             .filter_map(|stmt| match stmt {
728                 ast::Stmt::Item(it) => Some(it),
729                 ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None,
730             })
731             .filter_map(|item| {
732                 let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
733                     ast::Item::Fn(def) => {
734                         let id = self.find_inner_item(&def)?;
735                         (
736                             FunctionLoc { container: container.into(), id }.intern(self.db).into(),
737                             def.name(),
738                         )
739                     }
740                     ast::Item::TypeAlias(def) => {
741                         let id = self.find_inner_item(&def)?;
742                         (
743                             TypeAliasLoc { container: container.into(), id }.intern(self.db).into(),
744                             def.name(),
745                         )
746                     }
747                     ast::Item::Const(def) => {
748                         let id = self.find_inner_item(&def)?;
749                         (
750                             ConstLoc { container: container.into(), id }.intern(self.db).into(),
751                             def.name(),
752                         )
753                     }
754                     ast::Item::Static(def) => {
755                         let id = self.find_inner_item(&def)?;
756                         (StaticLoc { container, id }.intern(self.db).into(), def.name())
757                     }
758                     ast::Item::Struct(def) => {
759                         let id = self.find_inner_item(&def)?;
760                         (StructLoc { container, id }.intern(self.db).into(), def.name())
761                     }
762                     ast::Item::Enum(def) => {
763                         let id = self.find_inner_item(&def)?;
764                         (EnumLoc { container, id }.intern(self.db).into(), def.name())
765                     }
766                     ast::Item::Union(def) => {
767                         let id = self.find_inner_item(&def)?;
768                         (UnionLoc { container, id }.intern(self.db).into(), def.name())
769                     }
770                     ast::Item::Trait(def) => {
771                         let id = self.find_inner_item(&def)?;
772                         (TraitLoc { container, id }.intern(self.db).into(), def.name())
773                     }
774                     ast::Item::ExternBlock(_) => return None, // FIXME: collect from extern blocks
775                     ast::Item::Impl(_)
776                     | ast::Item::Use(_)
777                     | ast::Item::ExternCrate(_)
778                     | ast::Item::Module(_)
779                     | ast::Item::MacroCall(_) => return None,
780                     ast::Item::MacroRules(def) => {
781                         return Some(Either::Right(ast::Macro::from(def)));
782                     }
783                     ast::Item::MacroDef(def) => {
784                         return Some(Either::Right(ast::Macro::from(def)));
785                     }
786                 };
787
788                 Some(Either::Left((def, name)))
789             })
790             .collect::<Vec<_>>();
791
792         for either in items {
793             match either {
794                 Either::Left((def, name)) => {
795                     self.body.item_scope.define_def(def);
796                     if let Some(name) = name {
797                         let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
798                         let has_constructor = match def {
799                             ModuleDefId::AdtId(AdtId::StructId(s)) => {
800                                 self.db.struct_data(s).variant_data.kind() != StructKind::Record
801                             }
802                             _ => true,
803                         };
804                         self.body.item_scope.push_res(
805                             name.as_name(),
806                             crate::per_ns::PerNs::from_def(def, vis, has_constructor),
807                         );
808                     }
809                 }
810                 Either::Right(e) => {
811                     let mac = MacroDefId {
812                         krate: self.expander.module.krate,
813                         ast_id: Some(self.expander.ast_id(&e)),
814                         kind: MacroDefKind::Declarative,
815                         local_inner: false,
816                     };
817                     if let Some(name) = e.name() {
818                         self.body.item_scope.define_legacy_macro(name.as_name(), mac);
819                     }
820                 }
821             }
822         }
823     }
824
825     fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
826         if let Some(block) = expr {
827             self.collect_block(block)
828         } else {
829             self.missing_expr()
830         }
831     }
832
833     fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
834         let label = Label {
835             name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
836         };
837         self.alloc_label(label, AstPtr::new(&ast_label))
838     }
839
840     fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
841         let pattern = match &pat {
842             ast::Pat::IdentPat(bp) => {
843                 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
844                 let annotation =
845                     BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
846                 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
847                 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
848                     // This could also be a single-segment path pattern. To
849                     // decide that, we need to try resolving the name.
850                     let (resolved, _) = self.expander.def_map.resolve_path(
851                         self.db,
852                         self.expander.module.local_id,
853                         &name.clone().into(),
854                         BuiltinShadowMode::Other,
855                     );
856                     match resolved.take_values() {
857                         Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
858                         Some(ModuleDefId::EnumVariantId(_)) => {
859                             // this is only really valid for unit variants, but
860                             // shadowing other enum variants with a pattern is
861                             // an error anyway
862                             Pat::Path(name.into())
863                         }
864                         Some(ModuleDefId::AdtId(AdtId::StructId(s)))
865                             if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
866                         {
867                             // Funnily enough, record structs *can* be shadowed
868                             // by pattern bindings (but unit or tuple structs
869                             // can't).
870                             Pat::Path(name.into())
871                         }
872                         // shadowing statics is an error as well, so we just ignore that case here
873                         _ => Pat::Bind { name, mode: annotation, subpat },
874                     }
875                 } else {
876                     Pat::Bind { name, mode: annotation, subpat }
877                 }
878             }
879             ast::Pat::TupleStructPat(p) => {
880                 let path = p.path().and_then(|path| self.expander.parse_path(path));
881                 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
882                 Pat::TupleStruct { path, args, ellipsis }
883             }
884             ast::Pat::RefPat(p) => {
885                 let pat = self.collect_pat_opt(p.pat());
886                 let mutability = Mutability::from_mutable(p.mut_token().is_some());
887                 Pat::Ref { pat, mutability }
888             }
889             ast::Pat::PathPat(p) => {
890                 let path = p.path().and_then(|path| self.expander.parse_path(path));
891                 path.map(Pat::Path).unwrap_or(Pat::Missing)
892             }
893             ast::Pat::OrPat(p) => {
894                 let pats = p.pats().map(|p| self.collect_pat(p)).collect();
895                 Pat::Or(pats)
896             }
897             ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
898             ast::Pat::TuplePat(p) => {
899                 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
900                 Pat::Tuple { args, ellipsis }
901             }
902             ast::Pat::WildcardPat(_) => Pat::Wild,
903             ast::Pat::RecordPat(p) => {
904                 let path = p.path().and_then(|path| self.expander.parse_path(path));
905                 let args: Vec<_> = p
906                     .record_pat_field_list()
907                     .expect("every struct should have a field list")
908                     .fields()
909                     .filter_map(|f| {
910                         let ast_pat = f.pat()?;
911                         let pat = self.collect_pat(ast_pat);
912                         let name = f.field_name()?.as_name();
913                         Some(RecordFieldPat { name, pat })
914                     })
915                     .collect();
916
917                 let ellipsis = p
918                     .record_pat_field_list()
919                     .expect("every struct should have a field list")
920                     .dotdot_token()
921                     .is_some();
922
923                 Pat::Record { path, args, ellipsis }
924             }
925             ast::Pat::SlicePat(p) => {
926                 let SlicePatComponents { prefix, slice, suffix } = p.components();
927
928                 // FIXME properly handle `RestPat`
929                 Pat::Slice {
930                     prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
931                     slice: slice.map(|p| self.collect_pat(p)),
932                     suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
933                 }
934             }
935             ast::Pat::LiteralPat(lit) => {
936                 if let Some(ast_lit) = lit.literal() {
937                     let expr = Expr::Literal(ast_lit.kind().into());
938                     let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
939                     let expr_id = self.alloc_expr(expr, expr_ptr);
940                     Pat::Lit(expr_id)
941                 } else {
942                     Pat::Missing
943                 }
944             }
945             ast::Pat::RestPat(_) => {
946                 // `RestPat` requires special handling and should not be mapped
947                 // to a Pat. Here we are using `Pat::Missing` as a fallback for
948                 // when `RestPat` is mapped to `Pat`, which can easily happen
949                 // when the source code being analyzed has a malformed pattern
950                 // which includes `..` in a place where it isn't valid.
951
952                 Pat::Missing
953             }
954             ast::Pat::BoxPat(boxpat) => {
955                 let inner = self.collect_pat_opt(boxpat.pat());
956                 Pat::Box { inner }
957             }
958             ast::Pat::ConstBlockPat(const_block_pat) => {
959                 if let Some(expr) = const_block_pat.block_expr() {
960                     let expr_id = self.collect_block(expr);
961                     Pat::ConstBlock(expr_id)
962                 } else {
963                     Pat::Missing
964                 }
965             }
966             // FIXME: implement
967             ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
968         };
969         let ptr = AstPtr::new(&pat);
970         self.alloc_pat(pattern, Either::Left(ptr))
971     }
972
973     fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
974         if let Some(pat) = pat {
975             self.collect_pat(pat)
976         } else {
977             self.missing_pat()
978         }
979     }
980
981     fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
982         // Find the location of the `..`, if there is one. Note that we do not
983         // consider the possibility of there being multiple `..` here.
984         let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
985         // We want to skip the `..` pattern here, since we account for it above.
986         let args = args
987             .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
988             .map(|p| self.collect_pat(p))
989             .collect();
990
991         (args, ellipsis)
992     }
993
994     /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
995     /// not.
996     fn check_cfg(&mut self, owner: &dyn ast::AttrsOwner) -> Option<()> {
997         match self.expander.parse_attrs(self.db, owner).cfg() {
998             Some(cfg) => {
999                 if self.expander.cfg_options().check(&cfg) != Some(false) {
1000                     return Some(());
1001                 }
1002
1003                 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode {
1004                     file: self.expander.current_file_id,
1005                     node: SyntaxNodePtr::new(owner.syntax()),
1006                     cfg,
1007                     opts: self.expander.cfg_options().clone(),
1008                 }));
1009
1010                 None
1011             }
1012             None => Some(()),
1013         }
1014     }
1015 }
1016
1017 impl From<ast::BinOp> for BinaryOp {
1018     fn from(ast_op: ast::BinOp) -> Self {
1019         match ast_op {
1020             ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
1021             ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
1022             ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
1023             ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
1024             ast::BinOp::LesserEqualTest => {
1025                 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
1026             }
1027             ast::BinOp::GreaterEqualTest => {
1028                 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
1029             }
1030             ast::BinOp::LesserTest => {
1031                 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
1032             }
1033             ast::BinOp::GreaterTest => {
1034                 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
1035             }
1036             ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
1037             ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
1038             ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
1039             ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
1040             ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
1041             ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
1042             ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
1043             ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
1044             ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
1045             ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
1046             ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
1047             ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
1048             ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
1049             ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
1050             ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
1051             ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
1052             ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
1053             ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
1054             ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
1055             ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
1056             ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
1057         }
1058     }
1059 }
1060
1061 impl From<ast::LiteralKind> for Literal {
1062     fn from(ast_lit_kind: ast::LiteralKind) -> Self {
1063         match ast_lit_kind {
1064             LiteralKind::IntNumber(lit) => {
1065                 if let Some(float_suffix) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
1066                     return Literal::Float(Default::default(), Some(float_suffix));
1067                 }
1068                 let ty = lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it));
1069                 Literal::Int(Default::default(), ty)
1070             }
1071             LiteralKind::FloatNumber(lit) => {
1072                 let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
1073                 Literal::Float(Default::default(), ty)
1074             }
1075             LiteralKind::ByteString(_) => Literal::ByteString(Default::default()),
1076             LiteralKind::String(_) => Literal::String(Default::default()),
1077             LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
1078             LiteralKind::Bool(val) => Literal::Bool(val),
1079             LiteralKind::Char => Literal::Char(Default::default()),
1080         }
1081     }
1082 }