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 let idx
= unpack
!(block
= this
.as_temp(block
, None
, index
, Mutability
::Mut
));
93 this
.temp(usize_ty
.clone(), expr_span
),
94 this
.temp(bool_ty
, expr_span
),
98 source_info
, // len = len(slice)
100 Rvalue
::Len(slice
.clone()),
102 this
.cfg
.push_assign(
104 source_info
, // lt = idx < len
108 Operand
::Copy(Place
::Local(idx
)),
109 Operand
::Copy(len
.clone()),
113 let msg
= BoundsCheck
{
114 len
: Operand
::Move(len
),
115 index
: Operand
::Copy(Place
::Local(idx
)),
117 let success
= this
.assert(block
, Operand
::Move(lt
), true, msg
, expr_span
);
118 success
.and(slice
.index(idx
))
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
125 .all_pat_vars_are_implicit_refs_within_guards()
127 let index
= this
.var_local_id(id
, RefWithinGuard
);
128 Place
::Local(index
).deref()
130 let index
= this
.var_local_id(id
, OutsideGuard
);
135 ExprKind
::StaticRef { id }
=> block
.and(Place
::Static(Box
::new(Static
{
140 ExprKind
::PlaceTypeAscription { source, user_ty }
=> {
141 let place
= unpack
!(block
= this
.as_place(block
, source
));
142 if let Some(user_ty
) = user_ty
{
147 kind
: StatementKind
::AscribeUserType(
150 box UserTypeProjection { base: user_ty, projs: vec![], }
,
157 ExprKind
::ValueTypeAscription { source, user_ty }
=> {
158 let source
= this
.hir
.mirror(source
);
160 block
= this
.as_temp(block
, source
.temp_lifetime
, source
, mutability
)
162 if let Some(user_ty
) = user_ty
{
167 kind
: StatementKind
::AscribeUserType(
168 Place
::Local(temp
.clone()),
170 box UserTypeProjection { base: user_ty, projs: vec![], }
,
175 block
.and(Place
::Local(temp
))
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,
214 unpack
!(block
= this
.as_temp(block
, expr
.temp_lifetime
, expr
, mutability
));
215 block
.and(Place
::Local(temp
))