]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/build/expr/as_place.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc_mir / build / expr / as_place.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::expr::category::Category;
14 use build::ForGuard::{OutsideGuard, RefWithinGuard};
15 use build::{BlockAnd, BlockAndExtension, Builder};
16 use hair::*;
17 use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
18 use rustc::mir::*;
19 use rustc::ty::Variance;
20
21 use rustc_data_structures::indexed_vec::Idx;
22
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>>
26 where
27 M: Mirror<'tcx, Output = Expr<'tcx>>,
28 {
29 let expr = self.hir.mirror(expr);
30 self.expr_as_place(block, expr, Mutability::Mut)
31 }
32
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>>
39 where
40 M: Mirror<'tcx, Output = Expr<'tcx>>,
41 {
42 let expr = self.hir.mirror(expr);
43 self.expr_as_place(block, expr, Mutability::Not)
44 }
45
46 fn expr_as_place(
47 &mut self,
48 mut block: BasicBlock,
49 expr: Expr<'tcx>,
50 mutability: Mutability,
51 ) -> BlockAnd<Place<'tcx>> {
52 debug!(
53 "expr_as_place(block={:?}, expr={:?}, mutability={:?})",
54 block, expr, mutability
55 );
56
57 let this = self;
58 let expr_span = expr.span;
59 let source_info = this.source_info(expr_span);
60 match expr.kind {
61 ExprKind::Scope {
62 region_scope,
63 lint_level,
64 value,
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)
68 } else {
69 this.as_place(block, value)
70 }
71 }),
72 ExprKind::Field { lhs, name } => {
73 let place = unpack!(block = this.as_place(block, lhs));
74 let place = place.field(name, expr.ty);
75 block.and(place)
76 }
77 ExprKind::Deref { arg } => {
78 let place = unpack!(block = this.as_place(block, arg));
79 let place = place.deref();
80 block.and(place)
81 }
82 ExprKind::Index { lhs, index } => {
83 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
84
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
88 // places.
89 let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));
90
91 // bounds check:
92 let (len, lt) = (
93 this.temp(usize_ty.clone(), expr_span),
94 this.temp(bool_ty, expr_span),
95 );
96 this.cfg.push_assign(
97 block,
98 source_info, // len = len(slice)
99 &len,
100 Rvalue::Len(slice.clone()),
101 );
102 this.cfg.push_assign(
103 block,
104 source_info, // lt = idx < len
105 &lt,
106 Rvalue::BinaryOp(
107 BinOp::Lt,
108 Operand::Copy(Place::Local(idx)),
109 Operand::Copy(len.clone()),
110 ),
111 );
112
113 let msg = BoundsCheck {
114 len: Operand::Move(len),
115 index: Operand::Copy(Place::Local(idx)),
116 };
117 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
118 success.and(slice.index(idx))
119 }
120 ExprKind::SelfRef => block.and(Place::Local(Local::new(1))),
121 ExprKind::VarRef { id } => {
122 let place = if this.is_bound_var_in_guard(id) && this
123 .hir
124 .tcx()
125 .all_pat_vars_are_implicit_refs_within_guards()
126 {
127 let index = this.var_local_id(id, RefWithinGuard);
128 Place::Local(index).deref()
129 } else {
130 let index = this.var_local_id(id, OutsideGuard);
131 Place::Local(index)
132 };
133 block.and(place)
134 }
135 ExprKind::StaticRef { id } => block.and(Place::Static(Box::new(Static {
136 def_id: id,
137 ty: expr.ty,
138 }))),
139
140 ExprKind::PlaceTypeAscription { source, user_ty } => {
141 let place = unpack!(block = this.as_place(block, source));
142 if let Some(user_ty) = user_ty {
143 this.cfg.push(
144 block,
145 Statement {
146 source_info,
147 kind: StatementKind::AscribeUserType(
148 place.clone(),
149 Variance::Invariant,
150 box UserTypeProjection { base: user_ty, projs: vec![], },
151 ),
152 },
153 );
154 }
155 block.and(place)
156 }
157 ExprKind::ValueTypeAscription { source, user_ty } => {
158 let source = this.hir.mirror(source);
159 let temp = unpack!(
160 block = this.as_temp(block, source.temp_lifetime, source, mutability)
161 );
162 if let Some(user_ty) = user_ty {
163 this.cfg.push(
164 block,
165 Statement {
166 source_info,
167 kind: StatementKind::AscribeUserType(
168 Place::Local(temp.clone()),
169 Variance::Invariant,
170 box UserTypeProjection { base: user_ty, projs: vec![], },
171 ),
172 },
173 );
174 }
175 block.and(Place::Local(temp))
176 }
177
178 ExprKind::Array { .. }
179 | ExprKind::Tuple { .. }
180 | ExprKind::Adt { .. }
181 | ExprKind::Closure { .. }
182 | ExprKind::Unary { .. }
183 | ExprKind::Binary { .. }
184 | ExprKind::LogicalOp { .. }
185 | ExprKind::Box { .. }
186 | ExprKind::Cast { .. }
187 | ExprKind::Use { .. }
188 | ExprKind::NeverToAny { .. }
189 | ExprKind::ReifyFnPointer { .. }
190 | ExprKind::ClosureFnPointer { .. }
191 | ExprKind::UnsafeFnPointer { .. }
192 | ExprKind::Unsize { .. }
193 | ExprKind::Repeat { .. }
194 | ExprKind::Borrow { .. }
195 | ExprKind::If { .. }
196 | ExprKind::Match { .. }
197 | ExprKind::Loop { .. }
198 | ExprKind::Block { .. }
199 | ExprKind::Assign { .. }
200 | ExprKind::AssignOp { .. }
201 | ExprKind::Break { .. }
202 | ExprKind::Continue { .. }
203 | ExprKind::Return { .. }
204 | ExprKind::Literal { .. }
205 | ExprKind::InlineAsm { .. }
206 | ExprKind::Yield { .. }
207 | ExprKind::Call { .. } => {
208 // these are not places, so we need to make a temporary.
209 debug_assert!(match Category::of(&expr.kind) {
210 Some(Category::Place) => false,
211 _ => true,
212 });
213 let temp =
214 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
215 block.and(Place::Local(temp))
216 }
217 }
218 }
219 }