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 rustc_data_structures
::fx
::FxHashMap
;
14 use rustc_data_structures
::indexed_vec
::Idx
;
16 use build
::expr
::category
::{Category, RvalueFunc}
;
17 use build
::{BlockAnd, BlockAndExtension, Builder}
;
19 use rustc
::middle
::region
;
20 use rustc
::mir
::interpret
::EvalErrorKind
;
22 use rustc
::ty
::{self, Ty, UpvarSubsts}
;
25 impl<'a
, 'gcx
, 'tcx
> Builder
<'a
, 'gcx
, 'tcx
> {
26 /// See comment on `as_local_operand`
27 pub fn as_local_rvalue
<M
>(&mut self, block
: BasicBlock
, expr
: M
) -> BlockAnd
<Rvalue
<'tcx
>>
29 M
: Mirror
<'tcx
, Output
= Expr
<'tcx
>>,
31 let local_scope
= self.local_scope();
32 self.as_rvalue(block
, local_scope
, expr
)
35 /// Compile `expr`, yielding an rvalue.
39 scope
: Option
<region
::Scope
>,
41 ) -> BlockAnd
<Rvalue
<'tcx
>>
43 M
: Mirror
<'tcx
, Output
= Expr
<'tcx
>>,
45 let expr
= self.hir
.mirror(expr
);
46 self.expr_as_rvalue(block
, scope
, expr
)
51 mut block
: BasicBlock
,
52 scope
: Option
<region
::Scope
>,
54 ) -> BlockAnd
<Rvalue
<'tcx
>> {
56 "expr_as_rvalue(block={:?}, scope={:?}, expr={:?})",
61 let expr_span
= expr
.span
;
62 let source_info
= this
.source_info(expr_span
);
70 let region_scope
= (region_scope
, source_info
);
71 this
.in_scope(region_scope
, lint_level
, block
, |this
| {
72 this
.as_rvalue(block
, scope
, value
)
75 ExprKind
::Repeat { value, count }
=> {
76 let value_operand
= unpack
!(block
= this
.as_operand(block
, scope
, value
));
77 block
.and(Rvalue
::Repeat(value_operand
, count
))
84 let arg_place
= match borrow_kind
{
85 BorrowKind
::Shared
=> unpack
!(block
= this
.as_read_only_place(block
, arg
)),
86 _
=> unpack
!(block
= this
.as_place(block
, arg
)),
88 block
.and(Rvalue
::Ref(region
, borrow_kind
, arg_place
))
90 ExprKind
::Binary { op, lhs, rhs }
=> {
91 let lhs
= unpack
!(block
= this
.as_operand(block
, scope
, lhs
));
92 let rhs
= unpack
!(block
= this
.as_operand(block
, scope
, rhs
));
93 this
.build_binary_op(block
, op
, expr_span
, expr
.ty
, lhs
, rhs
)
95 ExprKind
::Unary { op, arg }
=> {
96 let arg
= unpack
!(block
= this
.as_operand(block
, scope
, arg
));
97 // Check for -MIN on signed integers
98 if this
.hir
.check_overflow() && op
== UnOp
::Neg
&& expr
.ty
.is_signed() {
99 let bool_ty
= this
.hir
.bool_ty();
101 let minval
= this
.minval_literal(expr_span
, expr
.ty
);
102 let is_min
= this
.temp(bool_ty
, expr_span
);
104 this
.cfg
.push_assign(
108 Rvalue
::BinaryOp(BinOp
::Eq
, arg
.to_copy(), minval
),
113 Operand
::Move(is_min
),
115 EvalErrorKind
::OverflowNeg
,
119 block
.and(Rvalue
::UnaryOp(op
, arg
))
121 ExprKind
::Box { value }
=> {
122 let value
= this
.hir
.mirror(value
);
123 // The `Box<T>` temporary created here is not a part of the HIR,
124 // and therefore is not considered during generator OIBIT
125 // determination. See the comment about `box` at `yield_in_scope`.
128 .push(LocalDecl
::new_internal(expr
.ty
, expr_span
));
133 kind
: StatementKind
::StorageLive(result
),
136 if let Some(scope
) = scope
{
137 // schedule a shallow free of that memory, lest we unwind:
138 this
.schedule_drop_storage_and_value(
141 &Place
::Local(result
),
146 // malloc some memory of suitable type (thus far, uninitialized):
147 let box_
= Rvalue
::NullaryOp(NullOp
::Box
, value
.ty
);
149 .push_assign(block
, source_info
, &Place
::Local(result
), box_
);
151 // initialize the box contents:
152 unpack
!(block
= this
.into(&Place
::Local(result
).deref(), block
, value
));
153 block
.and(Rvalue
::Use(Operand
::Move(Place
::Local(result
))))
155 ExprKind
::Cast { source }
=> {
156 let source
= this
.hir
.mirror(source
);
158 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
159 block
.and(Rvalue
::Cast(CastKind
::Misc
, source
, expr
.ty
))
161 ExprKind
::Use { source }
=> {
162 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
163 block
.and(Rvalue
::Use(source
))
165 ExprKind
::ReifyFnPointer { source }
=> {
166 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
167 block
.and(Rvalue
::Cast(CastKind
::ReifyFnPointer
, source
, expr
.ty
))
169 ExprKind
::UnsafeFnPointer { source }
=> {
170 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
171 block
.and(Rvalue
::Cast(CastKind
::UnsafeFnPointer
, source
, expr
.ty
))
173 ExprKind
::ClosureFnPointer { source }
=> {
174 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
175 block
.and(Rvalue
::Cast(CastKind
::ClosureFnPointer
, source
, expr
.ty
))
177 ExprKind
::Unsize { source }
=> {
178 let source
= unpack
!(block
= this
.as_operand(block
, scope
, source
));
179 block
.and(Rvalue
::Cast(CastKind
::Unsize
, source
, expr
.ty
))
181 ExprKind
::Array { fields }
=> {
182 // (*) We would (maybe) be closer to codegen if we
183 // handled this and other aggregate cases via
184 // `into()`, not `as_rvalue` -- in that case, instead
189 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
191 // we could just generate
196 // The problem is that then we would need to:
198 // (a) have a more complex mechanism for handling
200 // (b) distinguish the case where the type `Foo` has a
201 // destructor, in which case creating an instance
202 // as a whole "arms" the destructor, and you can't
203 // write individual fields; and,
204 // (c) handle the case where the type Foo has no
205 // fields. We don't want `let x: ();` to compile
206 // to the same MIR as `let x = ();`.
208 // first process the set of fields
209 let el_ty
= expr
.ty
.sequence_element_type(this
.hir
.tcx());
210 let fields
: Vec
<_
> = fields
212 .map(|f
| unpack
!(block
= this
.as_operand(block
, scope
, f
)))
215 block
.and(Rvalue
::Aggregate(box AggregateKind
::Array(el_ty
), fields
))
217 ExprKind
::Tuple { fields }
=> {
219 // first process the set of fields
220 let fields
: Vec
<_
> = fields
222 .map(|f
| unpack
!(block
= this
.as_operand(block
, scope
, f
)))
225 block
.and(Rvalue
::Aggregate(box AggregateKind
::Tuple
, fields
))
234 let mut operands
: Vec
<_
> = upvars
237 let upvar
= this
.hir
.mirror(upvar
);
238 match Category
::of(&upvar
.kind
) {
239 // Use as_place to avoid creating a temporary when
240 // moving a variable into a closure, so that
241 // borrowck knows which variables to mark as being
242 // used as mut. This is OK here because the upvar
243 // expressions have no side effects and act on
245 // This occurs when capturing by copy/move, while
246 // by reference captures use as_operand
247 Some(Category
::Place
) => {
248 let place
= unpack
!(block
= this
.as_place(block
, upvar
));
249 this
.consume_by_copy_or_move(place
)
252 // Turn mutable borrow captures into unique
253 // borrow captures when capturing an immutable
254 // variable. This is sound because the mutation
255 // that caused the capture will cause an error.
260 allow_two_phase_borrow
: false,
265 block
= this
.limit_capture_mutability(
266 upvar
.span
, upvar
.ty
, scope
, block
, arg
, region
,
269 _
=> unpack
!(block
= this
.as_operand(block
, scope
, upvar
)),
274 let result
= match substs
{
275 UpvarSubsts
::Generator(substs
) => {
276 let movability
= movability
.unwrap();
277 // Add the state operand since it follows the upvars in the generator
278 // struct. See librustc_mir/transform/generator.rs for more details.
279 operands
.push(Operand
::Constant(box Constant
{
281 ty
: this
.hir
.tcx().types
.u32,
283 literal
: ty
::Const
::from_bits(
286 ty
::ParamEnv
::empty().and(this
.hir
.tcx().types
.u32),
289 box AggregateKind
::Generator(closure_id
, substs
, movability
)
291 UpvarSubsts
::Closure(substs
) => box AggregateKind
::Closure(closure_id
, substs
),
293 block
.and(Rvalue
::Aggregate(result
, operands
))
304 let is_union
= adt_def
.is_union();
305 let active_field_index
= if is_union
{
306 Some(fields
[0].name
.index())
311 // first process the set of fields that were provided
312 // (evaluating them in order given by user)
313 let fields_map
: FxHashMap
<_
, _
> = fields
318 unpack
!(block
= this
.as_operand(block
, scope
, f
.expr
)),
322 let field_names
= this
.hir
.all_fields(adt_def
, variant_index
);
324 let fields
= if let Some(FruInfo { base, field_types }
) = base
{
325 let base
= unpack
!(block
= this
.as_place(block
, base
));
327 // MIR does not natively support FRU, so for each
328 // base-supplied field, generate an operand that
329 // reads it from the base.
332 .zip(field_types
.into_iter())
333 .map(|(n
, ty
)| match fields_map
.get(&n
) {
334 Some(v
) => v
.clone(),
335 None
=> this
.consume_by_copy_or_move(base
.clone().field(n
, ty
)),
340 .filter_map(|n
| fields_map
.get(n
).cloned())
344 let adt
= box AggregateKind
::Adt(
351 block
.and(Rvalue
::Aggregate(adt
, fields
))
353 ExprKind
::Assign { .. }
| ExprKind
::AssignOp { .. }
=> {
354 block
= unpack
!(this
.stmt_expr(block
, expr
, None
));
355 block
.and(this
.unit_rvalue())
357 ExprKind
::Yield { value }
=> {
358 let value
= unpack
!(block
= this
.as_operand(block
, scope
, value
));
359 let resume
= this
.cfg
.start_new_block();
360 let cleanup
= this
.generator_drop_cleanup();
364 TerminatorKind
::Yield
{
370 resume
.and(this
.unit_rvalue())
372 ExprKind
::Literal { .. }
373 | ExprKind
::Block { .. }
374 | ExprKind
::Match { .. }
375 | ExprKind
::If { .. }
376 | ExprKind
::NeverToAny { .. }
377 | ExprKind
::Loop { .. }
378 | ExprKind
::LogicalOp { .. }
379 | ExprKind
::Call { .. }
380 | ExprKind
::Field { .. }
381 | ExprKind
::Deref { .. }
382 | ExprKind
::Index { .. }
383 | ExprKind
::VarRef { .. }
385 | ExprKind
::Break { .. }
386 | ExprKind
::Continue { .. }
387 | ExprKind
::Return { .. }
388 | ExprKind
::InlineAsm { .. }
389 | ExprKind
::StaticRef { .. }
390 | ExprKind
::PlaceTypeAscription { .. }
391 | ExprKind
::ValueTypeAscription { .. }
=> {
392 // these do not have corresponding `Rvalue` variants,
393 // so make an operand and then return that
394 debug_assert
!(match Category
::of(&expr
.kind
) {
395 Some(Category
::Rvalue(RvalueFunc
::AsRvalue
)) => false,
398 let operand
= unpack
!(block
= this
.as_operand(block
, scope
, expr
));
399 block
.and(Rvalue
::Use(operand
))
404 pub fn build_binary_op(
406 mut block
: BasicBlock
,
412 ) -> BlockAnd
<Rvalue
<'tcx
>> {
413 let source_info
= self.source_info(span
);
414 let bool_ty
= self.hir
.bool_ty();
415 if self.hir
.check_overflow() && op
.is_checkable() && ty
.is_integral() {
416 let result_tup
= self.hir
.tcx().intern_tup(&[ty
, bool_ty
]);
417 let result_value
= self.temp(result_tup
, span
);
419 self.cfg
.push_assign(
423 Rvalue
::CheckedBinaryOp(op
, lhs
, rhs
),
425 let val_fld
= Field
::new(0);
426 let of_fld
= Field
::new(1);
428 let val
= result_value
.clone().field(val_fld
, ty
);
429 let of
= result_value
.field(of_fld
, bool_ty
);
431 let err
= EvalErrorKind
::Overflow(op
);
433 block
= self.assert(block
, Operand
::Move(of
), false, err
, span
);
435 block
.and(Rvalue
::Use(Operand
::Move(val
)))
437 if ty
.is_integral() && (op
== BinOp
::Div
|| op
== BinOp
::Rem
) {
438 // Checking division and remainder is more complex, since we 1. always check
439 // and 2. there are two possible failure cases, divide-by-zero and overflow.
441 let (zero_err
, overflow_err
) = if op
== BinOp
::Div
{
442 (EvalErrorKind
::DivisionByZero
, EvalErrorKind
::Overflow(op
))
444 (EvalErrorKind
::RemainderByZero
, EvalErrorKind
::Overflow(op
))
448 let is_zero
= self.temp(bool_ty
, span
);
449 let zero
= self.zero_literal(span
, ty
);
450 self.cfg
.push_assign(
454 Rvalue
::BinaryOp(BinOp
::Eq
, rhs
.to_copy(), zero
),
457 block
= self.assert(block
, Operand
::Move(is_zero
), false, zero_err
, span
);
459 // We only need to check for the overflow in one case:
460 // MIN / -1, and only for signed values.
462 let neg_1
= self.neg_1_literal(span
, ty
);
463 let min
= self.minval_literal(span
, ty
);
465 let is_neg_1
= self.temp(bool_ty
, span
);
466 let is_min
= self.temp(bool_ty
, span
);
467 let of
= self.temp(bool_ty
, span
);
469 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
471 self.cfg
.push_assign(
475 Rvalue
::BinaryOp(BinOp
::Eq
, rhs
.to_copy(), neg_1
),
477 self.cfg
.push_assign(
481 Rvalue
::BinaryOp(BinOp
::Eq
, lhs
.to_copy(), min
),
484 let is_neg_1
= Operand
::Move(is_neg_1
);
485 let is_min
= Operand
::Move(is_min
);
486 self.cfg
.push_assign(
490 Rvalue
::BinaryOp(BinOp
::BitAnd
, is_neg_1
, is_min
),
493 block
= self.assert(block
, Operand
::Move(of
), false, overflow_err
, span
);
497 block
.and(Rvalue
::BinaryOp(op
, lhs
, rhs
))
501 fn limit_capture_mutability(
505 temp_lifetime
: Option
<region
::Scope
>,
506 mut block
: BasicBlock
,
508 region
: &'tcx ty
::RegionKind
,
509 ) -> BlockAnd
<Operand
<'tcx
>> {
512 let source_info
= this
.source_info(upvar_span
);
515 .push(LocalDecl
::new_temp(upvar_ty
, upvar_span
));
521 kind
: StatementKind
::StorageLive(temp
),
525 let arg_place
= unpack
!(block
= this
.as_place(block
, arg
));
527 let mutability
= match arg_place
{
528 Place
::Local(local
) => this
.local_decls
[local
].mutability
,
529 Place
::Projection(box Projection
{
530 base
: Place
::Local(local
),
531 elem
: ProjectionElem
::Deref
,
534 if let Some(ClearCrossCrate
::Set(BindingForm
::RefForGuard
)) =
535 this
.local_decls
[local
].is_user_variable
541 "Unexpected capture place",
543 this
.local_decls
[local
].mutability
545 Place
::Projection(box Projection
{
547 elem
: ProjectionElem
::Field(upvar_index
, _
),
549 | Place
::Projection(box Projection
{
551 Place
::Projection(box Projection
{
553 elem
: ProjectionElem
::Field(upvar_index
, _
),
555 elem
: ProjectionElem
::Deref
,
557 // Not projected from the implicit `self` in a closure.
560 Place
::Local(local
) => local
== Local
::new(1),
561 Place
::Projection(box Projection
{
563 elem
: ProjectionElem
::Deref
,
564 }) => *base
== Place
::Local(Local
::new(1)),
567 "Unexpected capture place"
571 this
.upvar_decls
.len() > upvar_index
.index(),
572 "Unexpected capture place"
574 this
.upvar_decls
[upvar_index
.index()].mutability
576 _
=> bug
!("Unexpected capture place"),
579 let borrow_kind
= match mutability
{
580 Mutability
::Not
=> BorrowKind
::Unique
,
581 Mutability
::Mut
=> BorrowKind
::Mut
{
582 allow_two_phase_borrow
: false,
586 this
.cfg
.push_assign(
590 Rvalue
::Ref(region
, borrow_kind
, arg_place
),
593 // In constants, temp_lifetime is None. We should not need to drop
594 // anything because no values with a destructor can be created in
595 // a constant at this time, even if the type may need dropping.
596 if let Some(temp_lifetime
) = temp_lifetime
{
597 this
.schedule_drop_storage_and_value(
605 block
.and(Operand
::Move(Place
::Local(temp
)))
608 // Helper to get a `-1` value of the appropriate type
609 fn neg_1_literal(&mut self, span
: Span
, ty
: Ty
<'tcx
>) -> Operand
<'tcx
> {
610 let param_ty
= ty
::ParamEnv
::empty().and(self.hir
.tcx().lift_to_global(&ty
).unwrap());
611 let bits
= self.hir
.tcx().layout_of(param_ty
).unwrap().size
.bits();
612 let n
= (!0u128) >> (128 - bits
);
613 let literal
= ty
::Const
::from_bits(self.hir
.tcx(), n
, param_ty
);
615 self.literal_operand(span
, ty
, literal
)
618 // Helper to get the minimum value of the appropriate type
619 fn minval_literal(&mut self, span
: Span
, ty
: Ty
<'tcx
>) -> Operand
<'tcx
> {
620 assert
!(ty
.is_signed());
621 let param_ty
= ty
::ParamEnv
::empty().and(self.hir
.tcx().lift_to_global(&ty
).unwrap());
622 let bits
= self.hir
.tcx().layout_of(param_ty
).unwrap().size
.bits();
623 let n
= 1 << (bits
- 1);
624 let literal
= ty
::Const
::from_bits(self.hir
.tcx(), n
, param_ty
);
626 self.literal_operand(span
, ty
, literal
)