1 // Copyright 2016 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.
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.
11 use build::scope::BreakableScope;
12 use build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17 pub fn stmt_expr(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<()> {
19 let expr_span = expr.span;
20 let source_info = this.source_info(expr.span);
21 // Handle a number of expressions that don't need a destination at all. This
22 // avoids needing a mountain of temporary `()` variables.
23 let expr2 = expr.clone();
30 let value = this.hir.mirror(value);
31 this.in_scope((region_scope, source_info), lint_level, block, |this| {
32 this.stmt_expr(block, value)
35 ExprKind::Assign { lhs, rhs } => {
36 let lhs = this.hir.mirror(lhs);
37 let rhs = this.hir.mirror(rhs);
38 let lhs_span = lhs.span;
40 // Note: we evaluate assignments right-to-left. This
41 // is better for borrowck interaction with overloaded
42 // operators like x[j] = x[i].
44 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
45 this.block_context.push(BlockFrame::SubExpr);
47 // Generate better code for things that don't need to be
49 if this.hir.needs_drop(lhs.ty) {
50 let rhs = unpack!(block = this.as_local_operand(block, rhs));
51 let lhs = unpack!(block = this.as_place(block, lhs));
52 unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
54 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
55 let lhs = unpack!(block = this.as_place(block, lhs));
56 this.cfg.push_assign(block, source_info, &lhs, rhs);
59 this.block_context.pop();
62 ExprKind::AssignOp { op, lhs, rhs } => {
63 // FIXME(#28160) there is an interesting semantics
64 // question raised here -- should we "freeze" the
65 // value of the lhs here? I'm inclined to think not,
66 // since it seems closer to the semantics of the
67 // overloaded version, which takes `&mut self`. This
68 // only affects weird things like `x += {x += 1; x}`
69 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
71 let lhs = this.hir.mirror(lhs);
74 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
75 this.block_context.push(BlockFrame::SubExpr);
78 let rhs = unpack!(block = this.as_local_operand(block, rhs));
79 let lhs = unpack!(block = this.as_place(block, lhs));
81 // we don't have to drop prior contents or anything
82 // because AssignOp is only legal for Copy types
83 // (overloaded ops should be desugared into a call).
85 block = this.build_binary_op(
90 Operand::Copy(lhs.clone()),
94 this.cfg.push_assign(block, source_info, &lhs, result);
96 this.block_context.pop();
99 ExprKind::Continue { label } => {
104 } = *this.find_breakable_scope(expr_span, label);
105 let continue_block = continue_block
106 .expect("Attempted to continue in non-continuable breakable block");
109 (region_scope, source_info),
113 this.cfg.start_new_block().unit()
115 ExprKind::Break { label, value } => {
116 let (break_block, region_scope, destination) = {
120 ref break_destination,
122 } = *this.find_breakable_scope(expr_span, label);
123 (break_block, region_scope, break_destination.clone())
125 if let Some(value) = value {
126 debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
127 this.block_context.push(BlockFrame::SubExpr);
128 unpack!(block = this.into(&destination, block, value));
129 this.block_context.pop();
131 this.cfg.push_assign_unit(block, source_info, &destination)
133 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
134 this.cfg.start_new_block().unit()
136 ExprKind::Return { value } => {
137 block = match value {
139 debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
140 this.block_context.push(BlockFrame::SubExpr);
141 let result = unpack!(this.into(&Place::Local(RETURN_PLACE), block, value));
142 this.block_context.pop();
147 .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
151 let region_scope = this.region_scope_of_return_scope();
152 let return_block = this.return_block();
153 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
154 this.cfg.start_new_block().unit()
156 ExprKind::InlineAsm {
161 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
162 this.block_context.push(BlockFrame::SubExpr);
163 let outputs = outputs
165 .map(|output| unpack!(block = this.as_place(block, output)))
173 unpack!(block = this.as_local_operand(block, input)),
175 }).collect::<Vec<_>>()
181 kind: StatementKind::InlineAsm {
182 asm: box asm.clone(),
188 this.block_context.pop();
192 let expr_ty = expr.ty;
193 let temp = this.temp(expr.ty.clone(), expr_span);
194 unpack!(block = this.into(&temp, block, expr));
195 unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));