]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
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 | ||
92a42be0 | 13 | use build::{BlockAnd, BlockAndExtension, Builder}; |
e9174d1e SL |
14 | use build::expr::category::Category; |
15 | use hair::*; | |
92a42be0 | 16 | use rustc::mir::repr::*; |
e9174d1e | 17 | |
b039eaaf | 18 | impl<'a,'tcx> Builder<'a,'tcx> { |
e9174d1e SL |
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. | |
b039eaaf SL |
23 | pub fn as_operand<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Operand<'tcx>> |
24 | where M: Mirror<'tcx, Output = Expr<'tcx>> | |
e9174d1e SL |
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, | |
b039eaaf SL |
32 | expr: Expr<'tcx>) |
33 | -> BlockAnd<Operand<'tcx>> { | |
34 | debug!("expr_as_operand(block={:?}, expr={:?})", block, expr); | |
e9174d1e SL |
35 | let this = self; |
36 | ||
b039eaaf | 37 | if let ExprKind::Scope { extent, value } = expr.kind { |
54a0048b | 38 | return this.in_scope(extent, block, |this, _| this.as_operand(block, value)); |
e9174d1e SL |
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 | } |