]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_rvalue.rs
Various minor/cosmetic improvements to code
[rust.git] / src / librustc_mir / build / expr / as_rvalue.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 rustc_data_structures::fx::FxHashMap;
14 use rustc_data_structures::indexed_vec::Idx;
15
16 use build::expr::category::{Category, RvalueFunc};
17 use build::{BlockAnd, BlockAndExtension, Builder};
18 use hair::*;
19 use rustc::middle::region;
20 use rustc::mir::interpret::EvalErrorKind;
21 use rustc::mir::*;
22 use rustc::ty::{self, Ty, UpvarSubsts};
23 use syntax_pos::Span;
24
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>>
28     where
29         M: Mirror<'tcx, Output = Expr<'tcx>>,
30     {
31         let local_scope = self.local_scope();
32         self.as_rvalue(block, local_scope, expr)
33     }
34
35     /// Compile `expr`, yielding an rvalue.
36     pub fn as_rvalue<M>(
37         &mut self,
38         block: BasicBlock,
39         scope: Option<region::Scope>,
40         expr: M,
41     ) -> BlockAnd<Rvalue<'tcx>>
42     where
43         M: Mirror<'tcx, Output = Expr<'tcx>>,
44     {
45         let expr = self.hir.mirror(expr);
46         self.expr_as_rvalue(block, scope, expr)
47     }
48
49     fn expr_as_rvalue(
50         &mut self,
51         mut block: BasicBlock,
52         scope: Option<region::Scope>,
53         expr: Expr<'tcx>,
54     ) -> BlockAnd<Rvalue<'tcx>> {
55         debug!(
56             "expr_as_rvalue(block={:?}, scope={:?}, expr={:?})",
57             block, scope, expr
58         );
59
60         let this = self;
61         let expr_span = expr.span;
62         let source_info = this.source_info(expr_span);
63
64         match expr.kind {
65             ExprKind::Scope {
66                 region_scope,
67                 lint_level,
68                 value,
69             } => {
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)
73                 })
74             }
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))
78             }
79             ExprKind::Borrow {
80                 region,
81                 borrow_kind,
82                 arg,
83             } => {
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)),
87                 };
88                 block.and(Rvalue::Ref(region, borrow_kind, arg_place))
89             }
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)
94             }
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();
100
101                     let minval = this.minval_literal(expr_span, expr.ty);
102                     let is_min = this.temp(bool_ty, expr_span);
103
104                     this.cfg.push_assign(
105                         block,
106                         source_info,
107                         &is_min,
108                         Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
109                     );
110
111                     block = this.assert(
112                         block,
113                         Operand::Move(is_min),
114                         false,
115                         EvalErrorKind::OverflowNeg,
116                         expr_span,
117                     );
118                 }
119                 block.and(Rvalue::UnaryOp(op, arg))
120             }
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`.
126                 let result = this
127                     .local_decls
128                     .push(LocalDecl::new_internal(expr.ty, expr_span));
129                 this.cfg.push(
130                     block,
131                     Statement {
132                         source_info,
133                         kind: StatementKind::StorageLive(result),
134                     },
135                 );
136                 if let Some(scope) = scope {
137                     // schedule a shallow free of that memory, lest we unwind:
138                     this.schedule_drop_storage_and_value(
139                         expr_span,
140                         scope,
141                         &Place::Local(result),
142                         value.ty,
143                     );
144                 }
145
146                 // malloc some memory of suitable type (thus far, uninitialized):
147                 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
148                 this.cfg
149                     .push_assign(block, source_info, &Place::Local(result), box_);
150
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))))
154             }
155             ExprKind::Cast { source } => {
156                 let source = this.hir.mirror(source);
157
158                 let source = unpack!(block = this.as_operand(block, scope, source));
159                 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
160             }
161             ExprKind::Use { source } => {
162                 let source = unpack!(block = this.as_operand(block, scope, source));
163                 block.and(Rvalue::Use(source))
164             }
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))
168             }
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))
172             }
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))
176             }
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))
180             }
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
185                 // of generating
186                 //
187                 //     let tmp1 = ...1;
188                 //     let tmp2 = ...2;
189                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
190                 //
191                 // we could just generate
192                 //
193                 //     dest.f = ...1;
194                 //     dest.g = ...2;
195                 //
196                 // The problem is that then we would need to:
197                 //
198                 // (a) have a more complex mechanism for handling
199                 //     partial cleanup;
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 = ();`.
207
208                 // first process the set of fields
209                 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
210                 let fields: Vec<_> = fields
211                     .into_iter()
212                     .map(|f| unpack!(block = this.as_operand(block, scope, f)))
213                     .collect();
214
215                 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
216             }
217             ExprKind::Tuple { fields } => {
218                 // see (*) above
219                 // first process the set of fields
220                 let fields: Vec<_> = fields
221                     .into_iter()
222                     .map(|f| unpack!(block = this.as_operand(block, scope, f)))
223                     .collect();
224
225                 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
226             }
227             ExprKind::Closure {
228                 closure_id,
229                 substs,
230                 upvars,
231                 movability,
232             } => {
233                 // see (*) above
234                 let mut operands: Vec<_> = upvars
235                     .into_iter()
236                     .map(|upvar| {
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
244                             // disjoint places.
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)
250                             }
251                             _ => {
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.
256                                 match upvar.kind {
257                                     ExprKind::Borrow {
258                                         borrow_kind:
259                                             BorrowKind::Mut {
260                                                 allow_two_phase_borrow: false,
261                                             },
262                                         region,
263                                         arg,
264                                     } => unpack!(
265                                         block = this.limit_capture_mutability(
266                                             upvar.span, upvar.ty, scope, block, arg, region,
267                                         )
268                                     ),
269                                     _ => unpack!(block = this.as_operand(block, scope, upvar)),
270                                 }
271                             }
272                         }
273                     }).collect();
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 {
280                             span: expr_span,
281                             ty: this.hir.tcx().types.u32,
282                             user_ty: None,
283                             literal: ty::Const::from_bits(
284                                 this.hir.tcx(),
285                                 0,
286                                 ty::ParamEnv::empty().and(this.hir.tcx().types.u32),
287                             ),
288                         }));
289                         box AggregateKind::Generator(closure_id, substs, movability)
290                     }
291                     UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
292                 };
293                 block.and(Rvalue::Aggregate(result, operands))
294             }
295             ExprKind::Adt {
296                 adt_def,
297                 variant_index,
298                 substs,
299                 user_ty,
300                 fields,
301                 base,
302             } => {
303                 // see (*) above
304                 let is_union = adt_def.is_union();
305                 let active_field_index = if is_union {
306                     Some(fields[0].name.index())
307                 } else {
308                     None
309                 };
310
311                 // first process the set of fields that were provided
312                 // (evaluating them in order given by user)
313                 let fields_map: FxHashMap<_, _> = fields
314                     .into_iter()
315                     .map(|f| {
316                         (
317                             f.name,
318                             unpack!(block = this.as_operand(block, scope, f.expr)),
319                         )
320                     }).collect();
321
322                 let field_names = this.hir.all_fields(adt_def, variant_index);
323
324                 let fields = if let Some(FruInfo { base, field_types }) = base {
325                     let base = unpack!(block = this.as_place(block, base));
326
327                     // MIR does not natively support FRU, so for each
328                     // base-supplied field, generate an operand that
329                     // reads it from the base.
330                     field_names
331                         .into_iter()
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)),
336                         }).collect()
337                 } else {
338                     field_names
339                         .iter()
340                         .filter_map(|n| fields_map.get(n).cloned())
341                         .collect()
342                 };
343
344                 let adt = box AggregateKind::Adt(
345                     adt_def,
346                     variant_index,
347                     substs,
348                     user_ty,
349                     active_field_index,
350                 );
351                 block.and(Rvalue::Aggregate(adt, fields))
352             }
353             ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
354                 block = unpack!(this.stmt_expr(block, expr, None));
355                 block.and(this.unit_rvalue())
356             }
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();
361                 this.cfg.terminate(
362                     block,
363                     source_info,
364                     TerminatorKind::Yield {
365                         value: value,
366                         resume: resume,
367                         drop: cleanup,
368                     },
369                 );
370                 resume.and(this.unit_rvalue())
371             }
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 { .. }
384             | ExprKind::SelfRef
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,
396                     _ => true,
397                 });
398                 let operand = unpack!(block = this.as_operand(block, scope, expr));
399                 block.and(Rvalue::Use(operand))
400             }
401         }
402     }
403
404     pub fn build_binary_op(
405         &mut self,
406         mut block: BasicBlock,
407         op: BinOp,
408         span: Span,
409         ty: Ty<'tcx>,
410         lhs: Operand<'tcx>,
411         rhs: Operand<'tcx>,
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);
418
419             self.cfg.push_assign(
420                 block,
421                 source_info,
422                 &result_value,
423                 Rvalue::CheckedBinaryOp(op, lhs, rhs),
424             );
425             let val_fld = Field::new(0);
426             let of_fld = Field::new(1);
427
428             let val = result_value.clone().field(val_fld, ty);
429             let of = result_value.field(of_fld, bool_ty);
430
431             let err = EvalErrorKind::Overflow(op);
432
433             block = self.assert(block, Operand::Move(of), false, err, span);
434
435             block.and(Rvalue::Use(Operand::Move(val)))
436         } else {
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.
440
441                 let (zero_err, overflow_err) = if op == BinOp::Div {
442                     (EvalErrorKind::DivisionByZero, EvalErrorKind::Overflow(op))
443                 } else {
444                     (EvalErrorKind::RemainderByZero, EvalErrorKind::Overflow(op))
445                 };
446
447                 // Check for / 0
448                 let is_zero = self.temp(bool_ty, span);
449                 let zero = self.zero_literal(span, ty);
450                 self.cfg.push_assign(
451                     block,
452                     source_info,
453                     &is_zero,
454                     Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
455                 );
456
457                 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
458
459                 // We only need to check for the overflow in one case:
460                 // MIN / -1, and only for signed values.
461                 if ty.is_signed() {
462                     let neg_1 = self.neg_1_literal(span, ty);
463                     let min = self.minval_literal(span, ty);
464
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);
468
469                     // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
470
471                     self.cfg.push_assign(
472                         block,
473                         source_info,
474                         &is_neg_1,
475                         Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
476                     );
477                     self.cfg.push_assign(
478                         block,
479                         source_info,
480                         &is_min,
481                         Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
482                     );
483
484                     let is_neg_1 = Operand::Move(is_neg_1);
485                     let is_min = Operand::Move(is_min);
486                     self.cfg.push_assign(
487                         block,
488                         source_info,
489                         &of,
490                         Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
491                     );
492
493                     block = self.assert(block, Operand::Move(of), false, overflow_err, span);
494                 }
495             }
496
497             block.and(Rvalue::BinaryOp(op, lhs, rhs))
498         }
499     }
500
501     fn limit_capture_mutability(
502         &mut self,
503         upvar_span: Span,
504         upvar_ty: Ty<'tcx>,
505         temp_lifetime: Option<region::Scope>,
506         mut block: BasicBlock,
507         arg: ExprRef<'tcx>,
508         region: &'tcx ty::RegionKind,
509     ) -> BlockAnd<Operand<'tcx>> {
510         let this = self;
511
512         let source_info = this.source_info(upvar_span);
513         let temp = this
514             .local_decls
515             .push(LocalDecl::new_temp(upvar_ty, upvar_span));
516
517         this.cfg.push(
518             block,
519             Statement {
520                 source_info,
521                 kind: StatementKind::StorageLive(temp),
522             },
523         );
524
525         let arg_place = unpack!(block = this.as_place(block, arg));
526
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,
532             }) => {
533                 debug_assert!(
534                     if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
535                         this.local_decls[local].is_user_variable
536                     {
537                         true
538                     } else {
539                         false
540                     },
541                     "Unexpected capture place",
542                 );
543                 this.local_decls[local].mutability
544             }
545             Place::Projection(box Projection {
546                 ref base,
547                 elem: ProjectionElem::Field(upvar_index, _),
548             })
549             | Place::Projection(box Projection {
550                 base:
551                     Place::Projection(box Projection {
552                         ref base,
553                         elem: ProjectionElem::Field(upvar_index, _),
554                     }),
555                 elem: ProjectionElem::Deref,
556             }) => {
557                 // Not projected from the implicit `self` in a closure.
558                 debug_assert!(
559                     match *base {
560                         Place::Local(local) => local == Local::new(1),
561                         Place::Projection(box Projection {
562                             ref base,
563                             elem: ProjectionElem::Deref,
564                         }) => *base == Place::Local(Local::new(1)),
565                         _ => false,
566                     },
567                     "Unexpected capture place"
568                 );
569                 // Not in a closure
570                 debug_assert!(
571                     this.upvar_decls.len() > upvar_index.index(),
572                     "Unexpected capture place"
573                 );
574                 this.upvar_decls[upvar_index.index()].mutability
575             }
576             _ => bug!("Unexpected capture place"),
577         };
578
579         let borrow_kind = match mutability {
580             Mutability::Not => BorrowKind::Unique,
581             Mutability::Mut => BorrowKind::Mut {
582                 allow_two_phase_borrow: false,
583             },
584         };
585
586         this.cfg.push_assign(
587             block,
588             source_info,
589             &Place::Local(temp),
590             Rvalue::Ref(region, borrow_kind, arg_place),
591         );
592
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(
598                 upvar_span,
599                 temp_lifetime,
600                 &Place::Local(temp),
601                 upvar_ty,
602             );
603         }
604
605         block.and(Operand::Move(Place::Local(temp)))
606     }
607
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);
614
615         self.literal_operand(span, ty, literal)
616     }
617
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);
625
626         self.literal_operand(span, ty, literal)
627     }
628 }