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::{BlockAnd, BlockAndExtension, Builder};
12 use build::scope::BreakableScope;
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
18 pub fn stmt_expr(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<()> {
20 let expr_span = expr.span;
21 let source_info = this.source_info(expr.span);
22 // Handle a number of expressions that don't need a destination at all. This
23 // avoids needing a mountain of temporary `()` variables.
25 ExprKind::Scope { extent, value } => {
26 let value = this.hir.mirror(value);
27 this.in_scope(extent, block, |this| this.stmt_expr(block, value))
29 ExprKind::Assign { lhs, rhs } => {
30 let lhs = this.hir.mirror(lhs);
31 let rhs = this.hir.mirror(rhs);
32 let lhs_span = lhs.span;
34 // Note: we evaluate assignments right-to-left. This
35 // is better for borrowck interaction with overloaded
36 // operators like x[j] = x[i].
38 // Generate better code for things that don't need to be
40 if this.hir.needs_drop(lhs.ty) {
41 let rhs = unpack!(block = this.as_local_operand(block, rhs));
42 let lhs = unpack!(block = this.as_lvalue(block, lhs));
43 unpack!(block = this.build_drop_and_replace(
44 block, lhs_span, lhs, rhs
48 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
49 let lhs = unpack!(block = this.as_lvalue(block, lhs));
50 this.cfg.push_assign(block, source_info, &lhs, rhs);
54 ExprKind::AssignOp { op, lhs, rhs } => {
55 // FIXME(#28160) there is an interesting semantics
56 // question raised here -- should we "freeze" the
57 // value of the lhs here? I'm inclined to think not,
58 // since it seems closer to the semantics of the
59 // overloaded version, which takes `&mut self`. This
60 // only affects weird things like `x += {x += 1; x}`
61 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
63 let lhs = this.hir.mirror(lhs);
67 let rhs = unpack!(block = this.as_local_operand(block, rhs));
68 let lhs = unpack!(block = this.as_lvalue(block, lhs));
70 // we don't have to drop prior contents or anything
71 // because AssignOp is only legal for Copy types
72 // (overloaded ops should be desugared into a call).
73 let result = unpack!(block = this.build_binary_op(block, op, expr_span, lhs_ty,
74 Operand::Consume(lhs.clone()), rhs));
75 this.cfg.push_assign(block, source_info, &lhs, result);
79 ExprKind::Continue { label } => {
80 let BreakableScope { continue_block, extent, .. } =
81 *this.find_breakable_scope(expr_span, label);
82 let continue_block = continue_block.expect(
83 "Attempted to continue in non-continuable breakable block");
84 this.exit_scope(expr_span, extent, block, continue_block);
85 this.cfg.start_new_block().unit()
87 ExprKind::Break { label, value } => {
88 let (break_block, extent, destination) = {
92 ref break_destination,
94 } = *this.find_breakable_scope(expr_span, label);
95 (break_block, extent, break_destination.clone())
97 if let Some(value) = value {
98 unpack!(block = this.into(&destination, block, value))
100 this.cfg.push_assign_unit(block, source_info, &destination)
102 this.exit_scope(expr_span, extent, block, break_block);
103 this.cfg.start_new_block().unit()
105 ExprKind::Return { value } => {
106 block = match value {
108 unpack!(this.into(&Lvalue::Local(RETURN_POINTER), block, value))
111 this.cfg.push_assign_unit(block,
113 &Lvalue::Local(RETURN_POINTER));
117 let extent = this.extent_of_return_scope();
118 let return_block = this.return_block();
119 this.exit_scope(expr_span, extent, block, return_block);
120 this.cfg.start_new_block().unit()
122 ExprKind::InlineAsm { asm, outputs, inputs } => {
123 let outputs = outputs.into_iter().map(|output| {
124 unpack!(block = this.as_lvalue(block, output))
126 let inputs = inputs.into_iter().map(|input| {
127 unpack!(block = this.as_local_operand(block, input))
129 this.cfg.push(block, Statement {
130 source_info: source_info,
131 kind: StatementKind::InlineAsm {
140 let expr_ty = expr.ty;
141 let temp = this.temp(expr.ty.clone());
142 unpack!(block = this.into(&temp, block, expr));
143 unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));