]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/build/expr/as_operand.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_mir / build / expr / as_operand.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 //! See docs in build/expr/mod.rs
12
13 use build::{BlockAnd, BlockAndExtension, Builder};
14 use build::expr::category::Category;
15 use hair::*;
16 use rustc::mir::repr::*;
17
18 impl<'a,'tcx> Builder<'a,'tcx> {
19 /// Compile `expr` into a value that can be used as an operand.
20 /// If `expr` is an lvalue like `x`, this will introduce a
21 /// temporary `tmp = x`, so that we capture the value of `x` at
22 /// this time.
23 pub fn as_operand<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Operand<'tcx>>
24 where M: Mirror<'tcx, Output = Expr<'tcx>>
25 {
26 let expr = self.hir.mirror(expr);
27 self.expr_as_operand(block, expr)
28 }
29
30 fn expr_as_operand(&mut self,
31 mut block: BasicBlock,
32 expr: Expr<'tcx>)
33 -> BlockAnd<Operand<'tcx>> {
34 debug!("expr_as_operand(block={:?}, expr={:?})", block, expr);
35 let this = self;
36
37 if let ExprKind::Scope { extent, value } = expr.kind {
38 return this.in_scope(extent, block, |this, _| this.as_operand(block, value));
39 }
40
41 let category = Category::of(&expr.kind).unwrap();
42 debug!("expr_as_operand: category={:?} for={:?}", category, expr.kind);
43 match category {
44 Category::Constant => {
45 let constant = this.as_constant(expr);
46 block.and(Operand::Constant(constant))
47 }
48 Category::Lvalue |
49 Category::Rvalue(..) => {
50 let operand = unpack!(block = this.as_temp(block, expr));
51 block.and(Operand::Consume(operand))
52 }
53 }
54 }
55 }