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.
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 //! See docs in build/expr/mod.rs
13 use build::expr::category::Category;
14 use build::ForGuard::{OutsideGuard, RefWithinGuard};
15 use build::{BlockAnd, BlockAndExtension, Builder};
17 use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
19 use rustc::ty::Variance;
21 use rustc_data_structures::indexed_vec::Idx;
23 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
24 /// Compile `expr`, yielding a place that we can move from etc.
25 pub fn as_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
27 M: Mirror<'tcx, Output = Expr<'tcx>>,
29 let expr = self.hir.mirror(expr);
30 self.expr_as_place(block, expr, Mutability::Mut)
33 /// Compile `expr`, yielding a place that we can move from etc.
34 /// Mutability note: The caller of this method promises only to read from the resulting
35 /// place. The place itself may or may not be mutable:
36 /// * If this expr is a place expr like a.b, then we will return that place.
37 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
38 pub fn as_read_only_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
40 M: Mirror<'tcx, Output = Expr<'tcx>>,
42 let expr = self.hir.mirror(expr);
43 self.expr_as_place(block, expr, Mutability::Not)
48 mut block: BasicBlock,
50 mutability: Mutability,
51 ) -> BlockAnd<Place<'tcx>> {
53 "expr_as_place(block={:?}, expr={:?}, mutability={:?})",
54 block, expr, mutability
58 let expr_span = expr.span;
59 let source_info = this.source_info(expr_span);
65 } => this.in_scope((region_scope, source_info), lint_level, block, |this| {
66 if mutability == Mutability::Not {
67 this.as_read_only_place(block, value)
69 this.as_place(block, value)
72 ExprKind::Field { lhs, name } => {
73 let place = unpack!(block = this.as_place(block, lhs));
74 let place = place.field(name, expr.ty);
77 ExprKind::Deref { arg } => {
78 let place = unpack!(block = this.as_place(block, arg));
79 let place = place.deref();
82 ExprKind::Index { lhs, index } => {
83 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
85 let slice = unpack!(block = this.as_place(block, lhs));
86 // region_scope=None so place indexes live forever. They are scalars so they
87 // do not need storage annotations, and they are often copied between
89 // Making this a *fresh* temporary also means we do not have to worry about
90 // the index changing later: Nothing will ever change this temporary.
91 // The "retagging" transformation (for Stacked Borrows) relies on this.
92 let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));
96 this.temp(usize_ty.clone(), expr_span),
97 this.temp(bool_ty, expr_span),
101 source_info, // len = len(slice)
103 Rvalue::Len(slice.clone()),
105 this.cfg.push_assign(
107 source_info, // lt = idx < len
111 Operand::Copy(Place::Local(idx)),
112 Operand::Copy(len.clone()),
116 let msg = BoundsCheck {
117 len: Operand::Move(len),
118 index: Operand::Copy(Place::Local(idx)),
120 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
121 success.and(slice.index(idx))
123 ExprKind::SelfRef => block.and(Place::Local(Local::new(1))),
124 ExprKind::VarRef { id } => {
125 let place = if this.is_bound_var_in_guard(id) && this
128 .all_pat_vars_are_implicit_refs_within_guards()
130 let index = this.var_local_id(id, RefWithinGuard);
131 Place::Local(index).deref()
133 let index = this.var_local_id(id, OutsideGuard);
138 ExprKind::StaticRef { id } => block.and(Place::Static(Box::new(Static {
143 ExprKind::PlaceTypeAscription { source, user_ty } => {
144 let place = unpack!(block = this.as_place(block, source));
145 if let Some(user_ty) = user_ty {
150 kind: StatementKind::AscribeUserType(
153 box UserTypeProjection { base: user_ty, projs: vec![], },
160 ExprKind::ValueTypeAscription { source, user_ty } => {
161 let source = this.hir.mirror(source);
163 block = this.as_temp(block, source.temp_lifetime, source, mutability)
165 if let Some(user_ty) = user_ty {
170 kind: StatementKind::AscribeUserType(
171 Place::Local(temp.clone()),
173 box UserTypeProjection { base: user_ty, projs: vec![], },
178 block.and(Place::Local(temp))
181 ExprKind::Array { .. }
182 | ExprKind::Tuple { .. }
183 | ExprKind::Adt { .. }
184 | ExprKind::Closure { .. }
185 | ExprKind::Unary { .. }
186 | ExprKind::Binary { .. }
187 | ExprKind::LogicalOp { .. }
188 | ExprKind::Box { .. }
189 | ExprKind::Cast { .. }
190 | ExprKind::Use { .. }
191 | ExprKind::NeverToAny { .. }
192 | ExprKind::ReifyFnPointer { .. }
193 | ExprKind::ClosureFnPointer { .. }
194 | ExprKind::UnsafeFnPointer { .. }
195 | ExprKind::Unsize { .. }
196 | ExprKind::Repeat { .. }
197 | ExprKind::Borrow { .. }
198 | ExprKind::If { .. }
199 | ExprKind::Match { .. }
200 | ExprKind::Loop { .. }
201 | ExprKind::Block { .. }
202 | ExprKind::Assign { .. }
203 | ExprKind::AssignOp { .. }
204 | ExprKind::Break { .. }
205 | ExprKind::Continue { .. }
206 | ExprKind::Return { .. }
207 | ExprKind::Literal { .. }
208 | ExprKind::InlineAsm { .. }
209 | ExprKind::Yield { .. }
210 | ExprKind::Call { .. } => {
211 // these are not places, so we need to make a temporary.
212 debug_assert!(match Category::of(&expr.kind) {
213 Some(Category::Place) => false,
217 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
218 block.and(Place::Local(temp))