]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/mod.rs
Auto merge of #54813 - petrochenkov:uilocale, r=alexcrichton
[rust.git] / src / librustc_mir / build / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 use build;
13 use build::scope::{CachedBlock, DropKind};
14 use hair::cx::Cx;
15 use hair::{LintLevel, BindingMode, PatternKind};
16 use rustc::hir;
17 use rustc::hir::Node;
18 use rustc::hir::def_id::{DefId, LocalDefId};
19 use rustc::middle::region;
20 use rustc::mir::*;
21 use rustc::mir::visit::{MutVisitor, TyContext};
22 use rustc::ty::{self, Ty, TyCtxt};
23 use rustc::ty::subst::Substs;
24 use rustc::util::nodemap::NodeMap;
25 use rustc_target::spec::PanicStrategy;
26 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
27 use shim;
28 use std::mem;
29 use std::u32;
30 use rustc_target::spec::abi::Abi;
31 use syntax::ast;
32 use syntax::attr::{self, UnwindAttr};
33 use syntax::symbol::keywords;
34 use syntax_pos::Span;
35 use transform::MirSource;
36 use util as mir_util;
37
38 /// Construct the MIR for a given def-id.
39 pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'tcx> {
40     let id = tcx.hir.as_local_node_id(def_id).unwrap();
41
42     // Figure out what primary body this item has.
43     let (body_id, return_ty_span) = match tcx.hir.get(id) {
44         Node::Variant(variant) =>
45             return create_constructor_shim(tcx, id, &variant.node.data),
46         Node::StructCtor(ctor) =>
47             return create_constructor_shim(tcx, id, ctor),
48
49         Node::Expr(hir::Expr { node: hir::ExprKind::Closure(_, decl, body_id, _, _), .. })
50         | Node::Item(hir::Item { node: hir::ItemKind::Fn(decl, _, _, body_id), .. })
51         | Node::ImplItem(
52             hir::ImplItem {
53                 node: hir::ImplItemKind::Method(hir::MethodSig { decl, .. }, body_id),
54                 ..
55             }
56         )
57         | Node::TraitItem(
58             hir::TraitItem {
59                 node: hir::TraitItemKind::Method(
60                     hir::MethodSig { decl, .. },
61                     hir::TraitMethod::Provided(body_id),
62                 ),
63                 ..
64             }
65         ) => {
66             (*body_id, decl.output.span())
67         }
68         Node::Item(hir::Item { node: hir::ItemKind::Static(ty, _, body_id), .. })
69         | Node::Item(hir::Item { node: hir::ItemKind::Const(ty, body_id), .. })
70         | Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Const(ty, body_id), .. })
71         | Node::TraitItem(
72             hir::TraitItem { node: hir::TraitItemKind::Const(ty, Some(body_id)), .. }
73         ) => {
74             (*body_id, ty.span)
75         }
76         Node::AnonConst(hir::AnonConst { body, id, .. }) => {
77             (*body, tcx.hir.span(*id))
78         }
79
80         _ => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
81     };
82
83     tcx.infer_ctxt().enter(|infcx| {
84         let cx = Cx::new(&infcx, id);
85         let mut mir = if cx.tables().tainted_by_errors {
86             build::construct_error(cx, body_id)
87         } else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
88             // fetch the fully liberated fn signature (that is, all bound
89             // types/lifetimes replaced)
90             let fn_hir_id = tcx.hir.node_to_hir_id(id);
91             let fn_sig = cx.tables().liberated_fn_sigs()[fn_hir_id].clone();
92
93             let ty = tcx.type_of(tcx.hir.local_def_id(id));
94             let mut abi = fn_sig.abi;
95             let implicit_argument = match ty.sty {
96                 ty::Closure(..) => {
97                     // HACK(eddyb) Avoid having RustCall on closures,
98                     // as it adds unnecessary (and wrong) auto-tupling.
99                     abi = Abi::Rust;
100                     Some(ArgInfo(liberated_closure_env_ty(tcx, id, body_id), None, None, None))
101                 }
102                 ty::Generator(..) => {
103                     let gen_ty = tcx.body_tables(body_id).node_id_to_type(fn_hir_id);
104                     Some(ArgInfo(gen_ty, None, None, None))
105                 }
106                 _ => None,
107             };
108
109             // FIXME: safety in closures
110             let safety = match fn_sig.unsafety {
111                 hir::Unsafety::Normal => Safety::Safe,
112                 hir::Unsafety::Unsafe => Safety::FnUnsafe,
113             };
114
115             let body = tcx.hir.body(body_id);
116             let explicit_arguments =
117                 body.arguments
118                     .iter()
119                     .enumerate()
120                     .map(|(index, arg)| {
121                         let owner_id = tcx.hir.body_owner(body_id);
122                         let opt_ty_info;
123                         let self_arg;
124                         if let Some(ref fn_decl) = tcx.hir.fn_decl(owner_id) {
125                             let ty_hir_id = fn_decl.inputs[index].hir_id;
126                             let ty_span = tcx.hir.span(tcx.hir.hir_to_node_id(ty_hir_id));
127                             opt_ty_info = Some(ty_span);
128                             self_arg = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
129                                 match fn_decl.implicit_self {
130                                     hir::ImplicitSelfKind::Imm => Some(ImplicitSelfKind::Imm),
131                                     hir::ImplicitSelfKind::Mut => Some(ImplicitSelfKind::Mut),
132                                     hir::ImplicitSelfKind::ImmRef => Some(ImplicitSelfKind::ImmRef),
133                                     hir::ImplicitSelfKind::MutRef => Some(ImplicitSelfKind::MutRef),
134                                     _ => None,
135                                 }
136                             } else {
137                                 None
138                             };
139                         } else {
140                             opt_ty_info = None;
141                             self_arg = None;
142                         }
143                         ArgInfo(fn_sig.inputs()[index], opt_ty_info, Some(&*arg.pat), self_arg)
144                     });
145
146             let arguments = implicit_argument.into_iter().chain(explicit_arguments);
147
148             let (yield_ty, return_ty) = if body.is_generator {
149                 let gen_sig = match ty.sty {
150                     ty::Generator(gen_def_id, gen_substs, ..) =>
151                         gen_substs.sig(gen_def_id, tcx),
152                     _ =>
153                         span_bug!(tcx.hir.span(id), "generator w/o generator type: {:?}", ty),
154                 };
155                 (Some(gen_sig.yield_ty), gen_sig.return_ty)
156             } else {
157                 (None, fn_sig.output())
158             };
159
160             build::construct_fn(cx, id, arguments, safety, abi,
161                                 return_ty, yield_ty, return_ty_span, body)
162         } else {
163             build::construct_const(cx, body_id, return_ty_span)
164         };
165
166         // Convert the Mir to global types.
167         let mut globalizer = GlobalizeMir {
168             tcx,
169             span: mir.span
170         };
171         globalizer.visit_mir(&mut mir);
172         let mir = unsafe {
173             mem::transmute::<Mir, Mir<'tcx>>(mir)
174         };
175
176         mir_util::dump_mir(tcx, None, "mir_map", &0,
177                            MirSource::item(def_id), &mir, |_, _| Ok(()) );
178
179         mir
180     })
181 }
182
183 /// A pass to lift all the types and substitutions in a Mir
184 /// to the global tcx. Sadly, we don't have a "folder" that
185 /// can change 'tcx so we have to transmute afterwards.
186 struct GlobalizeMir<'a, 'gcx: 'a> {
187     tcx: TyCtxt<'a, 'gcx, 'gcx>,
188     span: Span
189 }
190
191 impl<'a, 'gcx: 'tcx, 'tcx> MutVisitor<'tcx> for GlobalizeMir<'a, 'gcx> {
192     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
193         if let Some(lifted) = self.tcx.lift(ty) {
194             *ty = lifted;
195         } else {
196             span_bug!(self.span,
197                       "found type `{:?}` with inference types/regions in MIR",
198                       ty);
199         }
200     }
201
202     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
203         if let Some(lifted) = self.tcx.lift(region) {
204             *region = lifted;
205         } else {
206             span_bug!(self.span,
207                       "found region `{:?}` with inference types/regions in MIR",
208                       region);
209         }
210     }
211
212     fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
213         if let Some(lifted) = self.tcx.lift(constant) {
214             *constant = lifted;
215         } else {
216             span_bug!(self.span,
217                       "found constant `{:?}` with inference types/regions in MIR",
218                       constant);
219         }
220     }
221
222     fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) {
223         if let Some(lifted) = self.tcx.lift(substs) {
224             *substs = lifted;
225         } else {
226             span_bug!(self.span,
227                       "found substs `{:?}` with inference types/regions in MIR",
228                       substs);
229         }
230     }
231 }
232
233 fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
234                                      ctor_id: ast::NodeId,
235                                      v: &'tcx hir::VariantData)
236                                      -> Mir<'tcx>
237 {
238     let span = tcx.hir.span(ctor_id);
239     if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
240         tcx.infer_ctxt().enter(|infcx| {
241             let mut mir = shim::build_adt_ctor(&infcx, ctor_id, fields, span);
242
243             // Convert the Mir to global types.
244             let tcx = infcx.tcx.global_tcx();
245             let mut globalizer = GlobalizeMir {
246                 tcx,
247                 span: mir.span
248             };
249             globalizer.visit_mir(&mut mir);
250             let mir = unsafe {
251                 mem::transmute::<Mir, Mir<'tcx>>(mir)
252             };
253
254             mir_util::dump_mir(tcx, None, "mir_map", &0,
255                                MirSource::item(tcx.hir.local_def_id(ctor_id)),
256                                &mir, |_, _| Ok(()) );
257
258             mir
259         })
260     } else {
261         span_bug!(span, "attempting to create MIR for non-tuple variant {:?}", v);
262     }
263 }
264
265 ///////////////////////////////////////////////////////////////////////////
266 // BuildMir -- walks a crate, looking for fn items and methods to build MIR from
267
268 fn liberated_closure_env_ty<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
269                                             closure_expr_id: ast::NodeId,
270                                             body_id: hir::BodyId)
271                                             -> Ty<'tcx> {
272     let closure_expr_hir_id = tcx.hir.node_to_hir_id(closure_expr_id);
273     let closure_ty = tcx.body_tables(body_id).node_id_to_type(closure_expr_hir_id);
274
275     let (closure_def_id, closure_substs) = match closure_ty.sty {
276         ty::Closure(closure_def_id, closure_substs) => (closure_def_id, closure_substs),
277         _ => bug!("closure expr does not have closure type: {:?}", closure_ty)
278     };
279
280     let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs).unwrap();
281     tcx.liberate_late_bound_regions(closure_def_id, &closure_env_ty)
282 }
283
284 #[derive(Debug, PartialEq, Eq)]
285 pub enum BlockFrame {
286     /// Evaluation is currently within a statement.
287     ///
288     /// Examples include:
289     ///  1. `EXPR;`
290     ///  2. `let _ = EXPR;`
291     ///  3. `let x = EXPR;`
292     Statement {
293         /// If true, then statement discards result from evaluating
294         /// the expression (such as examples 1 and 2 above).
295         ignores_expr_result: bool
296     },
297
298     /// Evaluation is currently within the tail expression of a block.
299     ///
300     /// Example: `{ STMT_1; STMT_2; EXPR }`
301     TailExpr {
302         /// If true, then the surrounding context of the block ignores
303         /// the result of evaluating the block's tail expression.
304         ///
305         /// Example: `let _ = { STMT_1; EXPR };`
306         tail_result_is_ignored: bool
307     },
308
309     /// Generic mark meaning that the block occurred as a subexpression
310     /// where the result might be used.
311     ///
312     /// Examples: `foo(EXPR)`, `match EXPR { ... }`
313     SubExpr,
314 }
315
316 impl BlockFrame {
317     fn is_tail_expr(&self) -> bool {
318         match *self {
319             BlockFrame::TailExpr { .. } => true,
320
321             BlockFrame::Statement { .. } |
322             BlockFrame::SubExpr => false,
323         }
324     }
325     fn is_statement(&self) -> bool {
326         match *self {
327             BlockFrame::Statement { .. } => true,
328
329             BlockFrame::TailExpr { .. } |
330             BlockFrame::SubExpr => false,
331         }
332     }
333  }
334
335 struct Builder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
336     hir: Cx<'a, 'gcx, 'tcx>,
337     cfg: CFG<'tcx>,
338
339     fn_span: Span,
340     arg_count: usize,
341
342     /// the current set of scopes, updated as we traverse;
343     /// see the `scope` module for more details
344     scopes: Vec<scope::Scope<'tcx>>,
345
346     /// the block-context: each time we build the code within an hair::Block,
347     /// we push a frame here tracking whether we are building a statement or
348     /// if we are pushing the tail expression of the block. This is used to
349     /// embed information in generated temps about whether they were created
350     /// for a block tail expression or not.
351     ///
352     /// It would be great if we could fold this into `self.scopes`
353     /// somehow; but right now I think that is very tightly tied to
354     /// the code generation in ways that we cannot (or should not)
355     /// start just throwing new entries onto that vector in order to
356     /// distinguish the context of EXPR1 from the context of EXPR2 in
357     /// `{ STMTS; EXPR1 } + EXPR2`
358     block_context: Vec<BlockFrame>,
359
360     /// The current unsafe block in scope, even if it is hidden by
361     /// a PushUnsafeBlock
362     unpushed_unsafe: Safety,
363
364     /// The number of `push_unsafe_block` levels in scope
365     push_unsafe_count: usize,
366
367     /// the current set of breakables; see the `scope` module for more
368     /// details
369     breakable_scopes: Vec<scope::BreakableScope<'tcx>>,
370
371     /// the vector of all scopes that we have created thus far;
372     /// we track this for debuginfo later
373     source_scopes: IndexVec<SourceScope, SourceScopeData>,
374     source_scope_local_data: IndexVec<SourceScope, SourceScopeLocalData>,
375     source_scope: SourceScope,
376
377     /// the guard-context: each time we build the guard expression for
378     /// a match arm, we push onto this stack, and then pop when we
379     /// finish building it.
380     guard_context: Vec<GuardFrame>,
381
382     /// Maps node ids of variable bindings to the `Local`s created for them.
383     /// (A match binding can have two locals; the 2nd is for the arm's guard.)
384     var_indices: NodeMap<LocalsForNode>,
385     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
386     upvar_decls: Vec<UpvarDecl>,
387     unit_temp: Option<Place<'tcx>>,
388
389     /// cached block with the RESUME terminator; this is created
390     /// when first set of cleanups are built.
391     cached_resume_block: Option<BasicBlock>,
392     /// cached block with the RETURN terminator
393     cached_return_block: Option<BasicBlock>,
394     /// cached block with the UNREACHABLE terminator
395     cached_unreachable_block: Option<BasicBlock>,
396 }
397
398 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
399     fn is_bound_var_in_guard(&self, id: ast::NodeId) -> bool {
400         self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
401     }
402
403     fn var_local_id(&self, id: ast::NodeId, for_guard: ForGuard) -> Local {
404         self.var_indices[&id].local_id(for_guard)
405     }
406 }
407
408 #[derive(Debug)]
409 enum LocalsForNode {
410     /// In the usual case, a node-id for an identifier maps to at most
411     /// one Local declaration.
412     One(Local),
413
414     /// The exceptional case is identifiers in a match arm's pattern
415     /// that are referenced in a guard of that match arm. For these,
416     /// we can have `2+k` Locals, where `k` is the number of candidate
417     /// patterns (separated by `|`) in the arm.
418     ///
419     /// * `for_arm_body` is the Local used in the arm body (which is
420     ///   just like the `One` case above),
421     ///
422     /// * `ref_for_guard` is the Local used in the arm's guard (which
423     ///   is a reference to a temp that is an alias of
424     ///   `for_arm_body`).
425     ///
426     /// * `vals_for_guard` is the `k` Locals; at most one of them will
427     ///   get initialized by the arm's execution, and after it is
428     ///   initialized, `ref_for_guard` will be assigned a reference to
429     ///   it.
430     ///
431     /// There reason we have `k` Locals rather than just 1 is to
432     /// accommodate some restrictions imposed by two-phase borrows,
433     /// which apply when we have a `ref mut` pattern.
434     ForGuard { vals_for_guard: Vec<Local>, ref_for_guard: Local, for_arm_body: Local },
435 }
436
437 #[derive(Debug)]
438 struct GuardFrameLocal {
439     id: ast::NodeId,
440 }
441
442 impl GuardFrameLocal {
443     fn new(id: ast::NodeId, _binding_mode: BindingMode) -> Self {
444         GuardFrameLocal {
445             id: id,
446         }
447     }
448 }
449
450 #[derive(Debug)]
451 struct GuardFrame {
452     /// These are the id's of names that are bound by patterns of the
453     /// arm of *this* guard.
454     ///
455     /// (Frames higher up the stack will have the id's bound in arms
456     /// further out, such as in a case like:
457     ///
458     /// match E1 {
459     ///      P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
460     /// }
461     ///
462     /// here, when building for FIXME
463     locals: Vec<GuardFrameLocal>,
464 }
465
466 /// ForGuard indicates whether we are talking about:
467 ///   1. the temp for a local binding used solely within guard expressions,
468 ///   2. the temp that holds reference to (1.), which is actually what the
469 ///      guard expressions see, or
470 ///   3. the temp for use outside of guard expressions.
471 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
472 enum ForGuard {
473     /// The `usize` identifies for which candidate pattern we want the
474     /// local binding. We keep a temp per-candidate to accommodate
475     /// two-phase borrows (see `LocalsForNode` documentation).
476     ValWithinGuard(usize),
477     RefWithinGuard,
478     OutsideGuard,
479 }
480
481 impl LocalsForNode {
482     fn local_id(&self, for_guard: ForGuard) -> Local {
483         match (self, for_guard) {
484             (&LocalsForNode::One(local_id), ForGuard::OutsideGuard) |
485             (&LocalsForNode::ForGuard { ref_for_guard: local_id, .. }, ForGuard::RefWithinGuard) |
486             (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) =>
487                 local_id,
488
489             (&LocalsForNode::ForGuard { ref vals_for_guard, .. },
490              ForGuard::ValWithinGuard(pat_idx)) =>
491                 vals_for_guard[pat_idx],
492
493             (&LocalsForNode::One(_), ForGuard::ValWithinGuard(_)) |
494             (&LocalsForNode::One(_), ForGuard::RefWithinGuard) =>
495                 bug!("anything with one local should never be within a guard."),
496         }
497     }
498 }
499
500 struct CFG<'tcx> {
501     basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
502 }
503
504 newtype_index! {
505     pub struct ScopeId { .. }
506 }
507
508 ///////////////////////////////////////////////////////////////////////////
509 /// The `BlockAnd` "monad" packages up the new basic block along with a
510 /// produced value (sometimes just unit, of course). The `unpack!`
511 /// macro (and methods below) makes working with `BlockAnd` much more
512 /// convenient.
513
514 #[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
515 struct BlockAnd<T>(BasicBlock, T);
516
517 trait BlockAndExtension {
518     fn and<T>(self, v: T) -> BlockAnd<T>;
519     fn unit(self) -> BlockAnd<()>;
520 }
521
522 impl BlockAndExtension for BasicBlock {
523     fn and<T>(self, v: T) -> BlockAnd<T> {
524         BlockAnd(self, v)
525     }
526
527     fn unit(self) -> BlockAnd<()> {
528         BlockAnd(self, ())
529     }
530 }
531
532 /// Update a block pointer and return the value.
533 /// Use it like `let x = unpack!(block = self.foo(block, foo))`.
534 macro_rules! unpack {
535     ($x:ident = $c:expr) => {
536         {
537             let BlockAnd(b, v) = $c;
538             $x = b;
539             v
540         }
541     };
542
543     ($c:expr) => {
544         {
545             let BlockAnd(b, ()) = $c;
546             b
547         }
548     };
549 }
550
551 fn should_abort_on_panic<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
552                                          fn_def_id: DefId,
553                                          abi: Abi)
554                                          -> bool {
555     // Not callable from C, so we can safely unwind through these
556     if abi == Abi::Rust || abi == Abi::RustCall { return false; }
557
558     // We never unwind, so it's not relevant to stop an unwind
559     if tcx.sess.panic_strategy() != PanicStrategy::Unwind { return false; }
560
561     // We cannot add landing pads, so don't add one
562     if tcx.sess.no_landing_pads() { return false; }
563
564     // This is a special case: some functions have a C abi but are meant to
565     // unwind anyway. Don't stop them.
566     let attrs = &tcx.get_attrs(fn_def_id);
567     match attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs) {
568         None => {
569             // FIXME(rust-lang/rust#48251) -- Had to disable
570             // abort-on-panic for backwards compatibility reasons.
571             false
572         }
573
574         Some(UnwindAttr::Allowed) => false,
575         Some(UnwindAttr::Aborts) => true,
576     }
577 }
578
579 ///////////////////////////////////////////////////////////////////////////
580 /// the main entry point for building MIR for a function
581
582 struct ArgInfo<'gcx>(Ty<'gcx>,
583                      Option<Span>,
584                      Option<&'gcx hir::Pat>,
585                      Option<ImplicitSelfKind>);
586
587 fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
588                                    fn_id: ast::NodeId,
589                                    arguments: A,
590                                    safety: Safety,
591                                    abi: Abi,
592                                    return_ty: Ty<'gcx>,
593                                    yield_ty: Option<Ty<'gcx>>,
594                                    return_ty_span: Span,
595                                    body: &'gcx hir::Body)
596                                    -> Mir<'tcx>
597     where A: Iterator<Item=ArgInfo<'gcx>>
598 {
599     let arguments: Vec<_> = arguments.collect();
600
601     let tcx = hir.tcx();
602     let span = tcx.hir.span(fn_id);
603
604     // Gather the upvars of a closure, if any.
605     let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
606         freevars.iter().map(|fv| {
607             let var_id = fv.var_id();
608             let var_hir_id = tcx.hir.node_to_hir_id(var_id);
609             let closure_expr_id = tcx.hir.local_def_id(fn_id);
610             let capture = hir.tables().upvar_capture(ty::UpvarId {
611                 var_id: var_hir_id,
612                 closure_expr_id: LocalDefId::from_def_id(closure_expr_id),
613             });
614             let by_ref = match capture {
615                 ty::UpvarCapture::ByValue => false,
616                 ty::UpvarCapture::ByRef(..) => true
617             };
618             let mut decl = UpvarDecl {
619                 debug_name: keywords::Invalid.name(),
620                 var_hir_id: ClearCrossCrate::Set(var_hir_id),
621                 by_ref,
622                 mutability: Mutability::Not,
623             };
624             if let Some(Node::Binding(pat)) = tcx.hir.find(var_id) {
625                 if let hir::PatKind::Binding(_, _, ident, _) = pat.node {
626                     decl.debug_name = ident.name;
627
628                     if let Some(&bm) = hir.tables.pat_binding_modes().get(pat.hir_id) {
629                         if bm == ty::BindByValue(hir::MutMutable) {
630                             decl.mutability = Mutability::Mut;
631                         } else {
632                             decl.mutability = Mutability::Not;
633                         }
634                     } else {
635                         tcx.sess.delay_span_bug(pat.span, "missing binding mode");
636                     }
637                 }
638             }
639             decl
640         }).collect()
641     });
642
643     let mut builder = Builder::new(hir.clone(),
644         span,
645         arguments.len(),
646         safety,
647         return_ty,
648         return_ty_span,
649         upvar_decls);
650
651     let fn_def_id = tcx.hir.local_def_id(fn_id);
652     let call_site_scope = region::Scope {
653         id: body.value.hir_id.local_id,
654         data: region::ScopeData::CallSite
655     };
656     let arg_scope = region::Scope {
657         id: body.value.hir_id.local_id,
658         data: region::ScopeData::Arguments
659     };
660     let mut block = START_BLOCK;
661     let source_info = builder.source_info(span);
662     let call_site_s = (call_site_scope, source_info);
663     unpack!(block = builder.in_scope(call_site_s, LintLevel::Inherited, block, |builder| {
664         if should_abort_on_panic(tcx, fn_def_id, abi) {
665             builder.schedule_abort();
666         }
667
668         let arg_scope_s = (arg_scope, source_info);
669         unpack!(block = builder.in_scope(arg_scope_s, LintLevel::Inherited, block, |builder| {
670             builder.args_and_body(block, &arguments, arg_scope, &body.value)
671         }));
672         // Attribute epilogue to function's closing brace
673         let fn_end = span.shrink_to_hi();
674         let source_info = builder.source_info(fn_end);
675         let return_block = builder.return_block();
676         builder.cfg.terminate(block, source_info,
677                               TerminatorKind::Goto { target: return_block });
678         builder.cfg.terminate(return_block, source_info,
679                               TerminatorKind::Return);
680         // Attribute any unreachable codepaths to the function's closing brace
681         if let Some(unreachable_block) = builder.cached_unreachable_block {
682             builder.cfg.terminate(unreachable_block, source_info,
683                                   TerminatorKind::Unreachable);
684         }
685         return_block.unit()
686     }));
687     assert_eq!(block, builder.return_block());
688
689     let mut spread_arg = None;
690     if abi == Abi::RustCall {
691         // RustCall pseudo-ABI untuples the last argument.
692         spread_arg = Some(Local::new(arguments.len()));
693     }
694     let closure_expr_id = tcx.hir.local_def_id(fn_id);
695     info!("fn_id {:?} has attrs {:?}", closure_expr_id,
696           tcx.get_attrs(closure_expr_id));
697
698     let mut mir = builder.finish(yield_ty);
699     mir.spread_arg = spread_arg;
700     mir
701 }
702
703 fn construct_const<'a, 'gcx, 'tcx>(
704     hir: Cx<'a, 'gcx, 'tcx>,
705     body_id: hir::BodyId,
706     ty_span: Span,
707 ) -> Mir<'tcx> {
708     let tcx = hir.tcx();
709     let ast_expr = &tcx.hir.body(body_id).value;
710     let ty = hir.tables().expr_ty_adjusted(ast_expr);
711     let owner_id = tcx.hir.body_owner(body_id);
712     let span = tcx.hir.span(owner_id);
713     let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, ty_span,vec![]);
714
715     let mut block = START_BLOCK;
716     let expr = builder.hir.mirror(ast_expr);
717     unpack!(block = builder.into_expr(&Place::Local(RETURN_PLACE), block, expr));
718
719     let source_info = builder.source_info(span);
720     builder.cfg.terminate(block, source_info, TerminatorKind::Return);
721
722     // Constants can't `return` so a return block should not be created.
723     assert_eq!(builder.cached_return_block, None);
724
725     // Constants may be match expressions in which case an unreachable block may
726     // be created, so terminate it properly.
727     if let Some(unreachable_block) = builder.cached_unreachable_block {
728         builder.cfg.terminate(unreachable_block, source_info,
729                               TerminatorKind::Unreachable);
730     }
731
732     builder.finish(None)
733 }
734
735 fn construct_error<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
736                                    body_id: hir::BodyId)
737                                    -> Mir<'tcx> {
738     let owner_id = hir.tcx().hir.body_owner(body_id);
739     let span = hir.tcx().hir.span(owner_id);
740     let ty = hir.tcx().types.err;
741     let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, span, vec![]);
742     let source_info = builder.source_info(span);
743     builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
744     builder.finish(None)
745 }
746
747 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
748     fn new(hir: Cx<'a, 'gcx, 'tcx>,
749            span: Span,
750            arg_count: usize,
751            safety: Safety,
752            return_ty: Ty<'tcx>,
753            return_span: Span,
754            upvar_decls: Vec<UpvarDecl>)
755            -> Builder<'a, 'gcx, 'tcx> {
756         let lint_level = LintLevel::Explicit(hir.root_lint_level);
757         let mut builder = Builder {
758             hir,
759             cfg: CFG { basic_blocks: IndexVec::new() },
760             fn_span: span,
761             arg_count,
762             scopes: vec![],
763             block_context: vec![],
764             source_scopes: IndexVec::new(),
765             source_scope: OUTERMOST_SOURCE_SCOPE,
766             source_scope_local_data: IndexVec::new(),
767             guard_context: vec![],
768             push_unsafe_count: 0,
769             unpushed_unsafe: safety,
770             breakable_scopes: vec![],
771             local_decls: IndexVec::from_elem_n(
772                 LocalDecl::new_return_place(return_ty, return_span),
773                 1,
774             ),
775             upvar_decls,
776             var_indices: NodeMap(),
777             unit_temp: None,
778             cached_resume_block: None,
779             cached_return_block: None,
780             cached_unreachable_block: None,
781         };
782
783         assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
784         assert_eq!(
785             builder.new_source_scope(span, lint_level, Some(safety)),
786             OUTERMOST_SOURCE_SCOPE);
787         builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
788
789         builder
790     }
791
792     fn finish(self,
793               yield_ty: Option<Ty<'tcx>>)
794               -> Mir<'tcx> {
795         for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
796             if block.terminator.is_none() {
797                 span_bug!(self.fn_span, "no terminator on block {:?}", index);
798             }
799         }
800
801         Mir::new(self.cfg.basic_blocks,
802                  self.source_scopes,
803                  ClearCrossCrate::Set(self.source_scope_local_data),
804                  IndexVec::new(),
805                  yield_ty,
806                  self.local_decls,
807                  self.arg_count,
808                  self.upvar_decls,
809                  self.fn_span
810         )
811     }
812
813     fn args_and_body(&mut self,
814                      mut block: BasicBlock,
815                      arguments: &[ArgInfo<'gcx>],
816                      argument_scope: region::Scope,
817                      ast_body: &'gcx hir::Expr)
818                      -> BlockAnd<()>
819     {
820         // Allocate locals for the function arguments
821         for &ArgInfo(ty, _, pattern, _) in arguments.iter() {
822             // If this is a simple binding pattern, give the local a name for
823             // debuginfo and so that error reporting knows that this is a user
824             // variable. For any other pattern the pattern introduces new
825             // variables which will be named instead.
826             let mut name = None;
827             if let Some(pat) = pattern {
828                 match pat.node {
829                     hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, _)
830                     | hir::PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, _) => {
831                         name = Some(ident.name);
832                     }
833                     _ => (),
834                 }
835             }
836
837             let source_info = SourceInfo {
838                 scope: OUTERMOST_SOURCE_SCOPE,
839                 span: pattern.map_or(self.fn_span, |pat| pat.span)
840             };
841             self.local_decls.push(LocalDecl {
842                 mutability: Mutability::Mut,
843                 ty,
844                 user_ty: None,
845                 source_info,
846                 visibility_scope: source_info.scope,
847                 name,
848                 internal: false,
849                 is_user_variable: None,
850                 is_block_tail: None,
851             });
852         }
853
854         let mut scope = None;
855         // Bind the argument patterns
856         for (index, arg_info) in arguments.iter().enumerate() {
857             // Function arguments always get the first Local indices after the return place
858             let local = Local::new(index + 1);
859             let place = Place::Local(local);
860             let &ArgInfo(ty, opt_ty_info, pattern, ref self_binding) = arg_info;
861
862             if let Some(pattern) = pattern {
863                 let pattern = self.hir.pattern_from_hir(pattern);
864                 let span = pattern.span;
865
866                 match *pattern.kind {
867                     // Don't introduce extra copies for simple bindings
868                     PatternKind::Binding { mutability, var, mode: BindingMode::ByValue, .. } => {
869                         self.local_decls[local].mutability = mutability;
870                         self.local_decls[local].is_user_variable =
871                             if let Some(kind) = self_binding {
872                                 Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf(*kind)))
873                             } else {
874                                 let binding_mode = ty::BindingMode::BindByValue(mutability.into());
875                                 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
876                                     binding_mode,
877                                     opt_ty_info,
878                                     opt_match_place: Some((Some(place.clone()), span)),
879                                     pat_span: span,
880                                 })))
881                             };
882                         self.var_indices.insert(var, LocalsForNode::One(local));
883                     }
884                     _ => {
885                         scope = self.declare_bindings(scope, ast_body.span,
886                                                       LintLevel::Inherited, &[pattern.clone()],
887                                                       matches::ArmHasGuard(false),
888                                                       Some((Some(&place), span)));
889                         unpack!(block = self.place_into_pattern(block, pattern, &place, false));
890                     }
891                 }
892             }
893
894             // Make sure we drop (parts of) the argument even when not matched on.
895             self.schedule_drop(
896                 pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
897                 argument_scope, &place, ty,
898                 DropKind::Value { cached_block: CachedBlock::default() },
899             );
900         }
901
902         // Enter the argument pattern bindings source scope, if it exists.
903         if let Some(source_scope) = scope {
904             self.source_scope = source_scope;
905         }
906
907         let body = self.hir.mirror(ast_body);
908         self.into(&Place::Local(RETURN_PLACE), block, body)
909     }
910
911     fn get_unit_temp(&mut self) -> Place<'tcx> {
912         match self.unit_temp {
913             Some(ref tmp) => tmp.clone(),
914             None => {
915                 let ty = self.hir.unit_ty();
916                 let fn_span = self.fn_span;
917                 let tmp = self.temp(ty, fn_span);
918                 self.unit_temp = Some(tmp.clone());
919                 tmp
920             }
921         }
922     }
923
924     fn return_block(&mut self) -> BasicBlock {
925         match self.cached_return_block {
926             Some(rb) => rb,
927             None => {
928                 let rb = self.cfg.start_new_block();
929                 self.cached_return_block = Some(rb);
930                 rb
931             }
932         }
933     }
934
935     fn unreachable_block(&mut self) -> BasicBlock {
936         match self.cached_unreachable_block {
937             Some(ub) => ub,
938             None => {
939                 let ub = self.cfg.start_new_block();
940                 self.cached_unreachable_block = Some(ub);
941                 ub
942             }
943         }
944     }
945 }
946
947 ///////////////////////////////////////////////////////////////////////////
948 // Builder methods are broken up into modules, depending on what kind
949 // of thing is being lowered. Note that they use the `unpack` macro
950 // above extensively.
951
952 mod block;
953 mod cfg;
954 mod expr;
955 mod into;
956 mod matches;
957 mod misc;
958 mod scope;