]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/block.rs
Specify output filenames for compatibility with Windows
[rust.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 use syntax_pos::Span;
16
17 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
18     pub fn ast_block(&mut self,
19                      destination: &Place<'tcx>,
20                      block: BasicBlock,
21                      ast_block: &'tcx hir::Block,
22                      source_info: SourceInfo)
23                      -> BlockAnd<()> {
24         let Block {
25             region_scope,
26             opt_destruction_scope,
27             span,
28             stmts,
29             expr,
30             targeted_by_break,
31             safety_mode
32         } =
33             self.hir.mirror(ast_block);
34         self.in_opt_scope(opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
35             this.in_scope((region_scope, source_info), LintLevel::Inherited, block, move |this| {
36                 if targeted_by_break {
37                     // This is a `break`-able block (currently only `catch { ... }`)
38                     let exit_block = this.cfg.start_new_block();
39                     let block_exit = this.in_breakable_scope(
40                         None, exit_block, destination.clone(), |this| {
41                             this.ast_block_stmts(destination, block, span, stmts, expr,
42                                                  safety_mode)
43                         });
44                     this.cfg.terminate(unpack!(block_exit), source_info,
45                                        TerminatorKind::Goto { target: exit_block });
46                     exit_block.unit()
47                 } else {
48                     this.ast_block_stmts(destination, block, span, stmts, expr,
49                                          safety_mode)
50                 }
51             })
52         })
53     }
54
55     fn ast_block_stmts(&mut self,
56                        destination: &Place<'tcx>,
57                        mut block: BasicBlock,
58                        span: Span,
59                        stmts: Vec<StmtRef<'tcx>>,
60                        expr: Option<ExprRef<'tcx>>,
61                        safety_mode: BlockSafety)
62                        -> BlockAnd<()> {
63         let this = self;
64
65         // This convoluted structure is to avoid using recursion as we walk down a list
66         // of statements. Basically, the structure we get back is something like:
67         //
68         //    let x = <init> in {
69         //       expr1;
70         //       let y = <init> in {
71         //           expr2;
72         //           expr3;
73         //           ...
74         //       }
75         //    }
76         //
77         // The let bindings are valid till the end of block so all we have to do is to pop all
78         // the let-scopes at the end.
79         //
80         // First we build all the statements in the block.
81         let mut let_scope_stack = Vec::with_capacity(8);
82         let outer_visibility_scope = this.visibility_scope;
83         let outer_push_unsafe_count = this.push_unsafe_count;
84         let outer_unpushed_unsafe = this.unpushed_unsafe;
85         this.update_visibility_scope_for_safety_mode(span, safety_mode);
86
87         let source_info = this.source_info(span);
88         for stmt in stmts {
89             let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
90             match kind {
91                 StmtKind::Expr { scope, expr } => {
92                     unpack!(block = this.in_opt_scope(
93                         opt_destruction_scope.map(|de|(de, source_info)), block, |this| {
94                             let si = (scope, source_info);
95                             this.in_scope(si, LintLevel::Inherited, block, |this| {
96                                 let expr = this.hir.mirror(expr);
97                                 this.stmt_expr(block, expr)
98                             })
99                         }));
100                 }
101                 StmtKind::Let {
102                     remainder_scope,
103                     init_scope,
104                     pattern,
105                     initializer,
106                     lint_level
107                 } => {
108                     // Enter the remainder scope, i.e. the bindings' destruction scope.
109                     this.push_scope((remainder_scope, source_info));
110                     let_scope_stack.push(remainder_scope);
111
112                     // Declare the bindings, which may create a visibility scope.
113                     let remainder_span = remainder_scope.span(this.hir.tcx(),
114                                                               &this.hir.region_scope_tree);
115                     let scope = this.declare_bindings(None, remainder_span, lint_level, &pattern);
116
117                     // Evaluate the initializer, if present.
118                     if let Some(init) = initializer {
119                         unpack!(block = this.in_opt_scope(
120                             opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
121                                 let scope = (init_scope, source_info);
122                                 this.in_scope(scope, lint_level, block, move |this| {
123                                     // FIXME #30046                             ^~~~
124                                     this.expr_into_pattern(block, pattern, init)
125                                 })
126                             }));
127                     } else {
128                         this.visit_bindings(&pattern, &mut |this, _, _, node, span, _| {
129                             this.storage_live_binding(block, node, span);
130                             this.schedule_drop_for_binding(node, span);
131                         })
132                     }
133
134                     // Enter the visibility scope, after evaluating the initializer.
135                     if let Some(visibility_scope) = scope {
136                         this.visibility_scope = visibility_scope;
137                     }
138                 }
139             }
140         }
141         // Then, the block may have an optional trailing expression which is a “return” value
142         // of the block.
143         if let Some(expr) = expr {
144             unpack!(block = this.into(destination, block, expr));
145         } else {
146             this.cfg.push_assign_unit(block, source_info, destination);
147         }
148         // Finally, we pop all the let scopes before exiting out from the scope of block
149         // itself.
150         for scope in let_scope_stack.into_iter().rev() {
151             unpack!(block = this.pop_scope((scope, source_info), block));
152         }
153         // Restore the original visibility scope.
154         this.visibility_scope = outer_visibility_scope;
155         this.push_unsafe_count = outer_push_unsafe_count;
156         this.unpushed_unsafe = outer_unpushed_unsafe;
157         block.unit()
158     }
159
160     /// If we are changing the safety mode, create a new visibility scope
161     fn update_visibility_scope_for_safety_mode(&mut self,
162                                                span: Span,
163                                                safety_mode: BlockSafety)
164     {
165         debug!("update_visibility_scope_for({:?}, {:?})", span, safety_mode);
166         let new_unsafety = match safety_mode {
167             BlockSafety::Safe => None,
168             BlockSafety::ExplicitUnsafe(node_id) => {
169                 assert_eq!(self.push_unsafe_count, 0);
170                 match self.unpushed_unsafe {
171                     Safety::Safe => {}
172                     _ => return
173                 }
174                 self.unpushed_unsafe = Safety::ExplicitUnsafe(node_id);
175                 Some(Safety::ExplicitUnsafe(node_id))
176             }
177             BlockSafety::PushUnsafe => {
178                 self.push_unsafe_count += 1;
179                 Some(Safety::BuiltinUnsafe)
180             }
181             BlockSafety::PopUnsafe => {
182                 self.push_unsafe_count =
183                     self.push_unsafe_count.checked_sub(1).unwrap_or_else(|| {
184                         span_bug!(span, "unsafe count underflow")
185                     });
186                 if self.push_unsafe_count == 0 {
187                     Some(self.unpushed_unsafe)
188                 } else {
189                     None
190                 }
191             }
192         };
193
194         if let Some(unsafety) = new_unsafety {
195             self.visibility_scope = self.new_visibility_scope(
196                 span, LintLevel::Inherited, Some(unsafety));
197         }
198     }
199 }