]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/build/block.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / librustc_mir / build / block.rs
1 // Copyright 2015 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 use build::{BlockAnd, BlockAndExtension, Builder};
12 use hair::*;
13 use rustc::mir::*;
14 use rustc::hir;
15
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17 pub fn ast_block(&mut self,
18 destination: &Lvalue<'tcx>,
19 mut block: BasicBlock,
20 ast_block: &'tcx hir::Block)
21 -> BlockAnd<()> {
22 let Block { extent, span, stmts, expr } = self.hir.mirror(ast_block);
23 self.in_scope(extent, block, move |this| {
24 // This convoluted structure is to avoid using recursion as we walk down a list
25 // of statements. Basically, the structure we get back is something like:
26 //
27 // let x = <init> in {
28 // expr1;
29 // let y = <init> in {
30 // expr2;
31 // expr3;
32 // ...
33 // }
34 // }
35 //
36 // The let bindings are valid till the end of block so all we have to do is to pop all
37 // the let-scopes at the end.
38 //
39 // First we build all the statements in the block.
40 let mut let_extent_stack = Vec::with_capacity(8);
41 let outer_visibility_scope = this.visibility_scope;
42 for stmt in stmts {
43 let Stmt { span: _, kind } = this.hir.mirror(stmt);
44 match kind {
45 StmtKind::Expr { scope, expr } => {
46 unpack!(block = this.in_scope(scope, block, |this| {
47 let expr = this.hir.mirror(expr);
48 this.stmt_expr(block, expr)
49 }));
50 }
51 StmtKind::Let { remainder_scope, init_scope, pattern, initializer } => {
52 let tcx = this.hir.tcx();
53
54 // Enter the remainder scope, i.e. the bindings' destruction scope.
55 this.push_scope(remainder_scope);
56 let_extent_stack.push(remainder_scope);
57
58 // Declare the bindings, which may create a visibility scope.
59 let remainder_span = remainder_scope.span(&tcx.region_maps, &tcx.hir);
60 let remainder_span = remainder_span.unwrap_or(span);
61 let scope = this.declare_bindings(None, remainder_span, &pattern);
62
63 // Evaluate the initializer, if present.
64 if let Some(init) = initializer {
65 unpack!(block = this.in_scope(init_scope, block, move |this| {
66 // FIXME #30046 ^~~~
67 this.expr_into_pattern(block, pattern, init)
68 }));
69 } else {
70 this.visit_bindings(&pattern, &mut |this, _, _, node, span, _| {
71 this.storage_live_binding(block, node, span);
72 this.schedule_drop_for_binding(node, span);
73 })
74 }
75
76 // Enter the visibility scope, after evaluating the initializer.
77 if let Some(visibility_scope) = scope {
78 this.visibility_scope = visibility_scope;
79 }
80 }
81 }
82 }
83 // Then, the block may have an optional trailing expression which is a “return” value
84 // of the block.
85 if let Some(expr) = expr {
86 unpack!(block = this.into(destination, block, expr));
87 } else {
88 let source_info = this.source_info(span);
89 this.cfg.push_assign_unit(block, source_info, destination);
90 }
91 // Finally, we pop all the let scopes before exiting out from the scope of block
92 // itself.
93 for extent in let_extent_stack.into_iter().rev() {
94 unpack!(block = this.pop_scope(extent, block));
95 }
96 // Restore the original visibility scope.
97 this.visibility_scope = outer_visibility_scope;
98 block.unit()
99 })
100 }
101 }