]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/build/expr/as_lvalue.rs
Merge tag 'upstream/1.5.0+dfsg1'
[rustc.git] / src / librustc_mir / build / expr / as_lvalue.rs
CommitLineData
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
13use build::{BlockAnd, Builder};
14use build::expr::category::Category;
15use hair::*;
16use repr::*;
17
b039eaaf 18impl<'a,'tcx> Builder<'a,'tcx> {
e9174d1e
SL
19 /// Compile `expr`, yielding an lvalue that we can move from etc.
20 pub fn as_lvalue<M>(&mut self,
21 block: BasicBlock,
22 expr: M)
b039eaaf
SL
23 -> BlockAnd<Lvalue<'tcx>>
24 where M: Mirror<'tcx, Output=Expr<'tcx>>
e9174d1e
SL
25 {
26 let expr = self.hir.mirror(expr);
27 self.expr_as_lvalue(block, expr)
28 }
29
30 fn expr_as_lvalue(&mut self,
31 mut block: BasicBlock,
b039eaaf
SL
32 expr: Expr<'tcx>)
33 -> BlockAnd<Lvalue<'tcx>> {
34 debug!("expr_as_lvalue(block={:?}, expr={:?})", block, expr);
e9174d1e
SL
35
36 let this = self;
37 let expr_span = expr.span;
38 match expr.kind {
39 ExprKind::Scope { extent, value } => {
b039eaaf 40 this.in_scope(extent, block, |this| this.as_lvalue(block, value))
e9174d1e
SL
41 }
42 ExprKind::Field { lhs, name } => {
43 let lvalue = unpack!(block = this.as_lvalue(block, lhs));
44 let lvalue = lvalue.field(name);
45 block.and(lvalue)
46 }
47 ExprKind::Deref { arg } => {
48 let lvalue = unpack!(block = this.as_lvalue(block, arg));
49 let lvalue = lvalue.deref();
50 block.and(lvalue)
51 }
52 ExprKind::Index { lhs, index } => {
53 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
54
55 let slice = unpack!(block = this.as_lvalue(block, lhs));
56
57 let idx = unpack!(block = this.as_operand(block, index));
58
59 // bounds check:
60 let (len, lt) = (this.temp(usize_ty.clone()), this.temp(bool_ty));
61 this.cfg.push_assign(block, expr_span, // len = len(slice)
62 &len, Rvalue::Len(slice.clone()));
63 this.cfg.push_assign(block, expr_span, // lt = idx < len
64 &lt, Rvalue::BinaryOp(BinOp::Lt,
65 idx.clone(),
66 Operand::Consume(len)));
67
b039eaaf 68 let (success, failure) = (this.cfg.start_new_block(), this.cfg.start_new_block());
e9174d1e
SL
69 this.cfg.terminate(block,
70 Terminator::If {
71 cond: Operand::Consume(lt),
b039eaaf 72 targets: [success, failure],
e9174d1e
SL
73 });
74 this.panic(failure);
75 success.and(slice.index(idx))
76 }
77 ExprKind::SelfRef => {
78 block.and(Lvalue::Arg(0))
79 }
80 ExprKind::VarRef { id } => {
81 let index = this.var_indices[&id];
82 block.and(Lvalue::Var(index))
83 }
84 ExprKind::StaticRef { id } => {
85 block.and(Lvalue::Static(id))
86 }
87
88 ExprKind::Vec { .. } |
89 ExprKind::Tuple { .. } |
90 ExprKind::Adt { .. } |
91 ExprKind::Closure { .. } |
92 ExprKind::Unary { .. } |
93 ExprKind::Binary { .. } |
94 ExprKind::LogicalOp { .. } |
95 ExprKind::Box { .. } |
96 ExprKind::Cast { .. } |
97 ExprKind::ReifyFnPointer { .. } |
98 ExprKind::UnsafeFnPointer { .. } |
99 ExprKind::Unsize { .. } |
100 ExprKind::Repeat { .. } |
101 ExprKind::Borrow { .. } |
102 ExprKind::If { .. } |
103 ExprKind::Match { .. } |
104 ExprKind::Loop { .. } |
105 ExprKind::Block { .. } |
106 ExprKind::Assign { .. } |
107 ExprKind::AssignOp { .. } |
108 ExprKind::Break { .. } |
109 ExprKind::Continue { .. } |
110 ExprKind::Return { .. } |
111 ExprKind::Literal { .. } |
112 ExprKind::InlineAsm { .. } |
113 ExprKind::Call { .. } => {
114 // these are not lvalues, so we need to make a temporary.
115 debug_assert!(match Category::of(&expr.kind) {
116 Some(Category::Lvalue) => false,
117 _ => true,
118 });
119 this.as_temp(block, expr)
120 }
121 }
122 }
123}