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, RvalueFunc};
14 use build::{BlockAnd, BlockAndExtension, Builder};
19 use rustc_target::spec::abi::Abi;
21 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
22 /// Compile `expr`, storing the result into `destination`, which
23 /// is assumed to be uninitialized.
26 destination: &Place<'tcx>,
27 mut block: BasicBlock,
31 "into_expr(destination={:?}, block={:?}, expr={:?})",
32 destination, block, expr
35 // since we frequently have to reference `self` from within a
36 // closure, where `self` would be shadowed, it's easier to
37 // just use the name `this` uniformly
39 let expr_span = expr.span;
40 let source_info = this.source_info(expr_span);
48 let region_scope = (region_scope, source_info);
49 this.in_scope(region_scope, lint_level, block, |this| {
50 this.into(destination, block, value)
53 ExprKind::Block { body: ast_block } => {
54 this.ast_block(destination, block, ast_block, source_info)
56 ExprKind::Match { discriminant, arms } => {
57 this.match_expr(destination, expr_span, block, discriminant, arms)
59 ExprKind::NeverToAny { source } => {
60 let source = this.hir.mirror(source);
61 let is_call = match source.kind {
62 ExprKind::Call { .. } => true,
66 unpack!(block = this.as_local_rvalue(block, source));
68 // This is an optimization. If the expression was a call then we already have an
69 // unreachable block. Don't bother to terminate it and create a new one.
74 .terminate(block, source_info, TerminatorKind::Unreachable);
75 let end_block = this.cfg.start_new_block();
84 let operand = unpack!(block = this.as_local_operand(block, cond_expr));
86 let mut then_block = this.cfg.start_new_block();
87 let mut else_block = this.cfg.start_new_block();
88 let term = TerminatorKind::if_(this.hir.tcx(), operand, then_block, else_block);
89 this.cfg.terminate(block, source_info, term);
91 unpack!(then_block = this.into(destination, then_block, then_expr));
92 else_block = if let Some(else_expr) = else_expr {
93 unpack!(this.into(destination, else_block, else_expr))
95 // Body of the `if` expression without an `else` clause must return `()`, thus
96 // we implicitly generate a `else {}` if it is not specified.
98 .push_assign_unit(else_block, source_info, destination);
102 let join_block = this.cfg.start_new_block();
106 TerminatorKind::Goto { target: join_block },
111 TerminatorKind::Goto { target: join_block },
116 ExprKind::LogicalOp { op, lhs, rhs } => {
119 // [block: If(lhs)] -true-> [else_block: If(rhs)] -true-> [true_block]
121 // +----------false-----------+------------------> [false_block]
125 // [block: If(lhs)] -false-> [else_block: If(rhs)] -true-> [true_block]
126 // | (true) | (false)
127 // [true_block] [false_block]
129 let (true_block, false_block, mut else_block, join_block) = (
130 this.cfg.start_new_block(),
131 this.cfg.start_new_block(),
132 this.cfg.start_new_block(),
133 this.cfg.start_new_block(),
136 let lhs = unpack!(block = this.as_local_operand(block, lhs));
137 let blocks = match op {
138 LogicalOp::And => (else_block, false_block),
139 LogicalOp::Or => (true_block, else_block),
141 let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
142 this.cfg.terminate(block, source_info, term);
144 let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
145 let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
146 this.cfg.terminate(else_block, source_info, term);
148 this.cfg.push_assign_constant(
154 ty: this.hir.bool_ty(),
156 literal: this.hir.true_literal(),
160 this.cfg.push_assign_constant(
166 ty: this.hir.bool_ty(),
168 literal: this.hir.false_literal(),
175 TerminatorKind::Goto { target: join_block },
180 TerminatorKind::Goto { target: join_block },
186 condition: opt_cond_expr,
189 // [block] --> [loop_block] -/eval. cond./-> [loop_block_end] -1-> [exit_block]
194 // [body_block_end] <-/eval. body/-- [body_block]
196 // If `opt_cond_expr` is `None`, then the graph is somewhat simplified:
200 // [loop_block] -> [body_block] -/eval. body/-> [body_block_end]
203 // | +-----------------------------------------+
204 // +-> [diverge_cleanup]
205 // The false link is required to make sure borrowck considers unwinds through the
206 // body, even when the exact code in the body cannot unwind
208 let loop_block = this.cfg.start_new_block();
209 let exit_block = this.cfg.start_new_block();
215 TerminatorKind::Goto { target: loop_block },
218 this.in_breakable_scope(
223 // conduct the test, if necessary
225 if let Some(cond_expr) = opt_cond_expr {
228 loop_block_end = this.as_local_operand(loop_block, cond_expr)
230 body_block = this.cfg.start_new_block();
232 TerminatorKind::if_(this.hir.tcx(), cond, body_block, exit_block);
233 this.cfg.terminate(loop_block_end, source_info, term);
235 // if the test is false, there's no `break` to assign `destination`, so
236 // we have to do it; this overwrites any `break`-assigned value but it's
237 // always `()` anyway
239 .push_assign_unit(exit_block, source_info, destination);
241 body_block = this.cfg.start_new_block();
242 let diverge_cleanup = this.diverge_cleanup();
246 TerminatorKind::FalseUnwind {
247 real_target: body_block,
248 unwind: Some(diverge_cleanup),
253 // The “return” value of the loop body must always be an unit. We therefore
254 // introduce a unit temporary as the destination for the loop body.
255 let tmp = this.get_unit_temp();
256 // Execute the body, branching back to the test.
257 let body_block_end = unpack!(this.into(&tmp, body_block, body));
261 TerminatorKind::Goto { target: loop_block },
267 ExprKind::Call { ty, fun, args } => {
268 // FIXME(canndrew): This is_never should probably be an is_uninhabited
269 let diverges = expr.ty.is_never();
270 let intrinsic = match ty.sty {
271 ty::FnDef(def_id, _) => {
272 let f = ty.fn_sig(this.hir.tcx());
273 if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {
274 Some(this.hir.tcx().item_name(def_id).as_str())
281 let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
282 let fun = unpack!(block = this.as_local_operand(block, fun));
283 if intrinsic == Some("move_val_init") {
284 // `move_val_init` has "magic" semantics - the second argument is
285 // always evaluated "directly" into the first one.
287 let mut args = args.into_iter();
288 let ptr = args.next().expect("0 arguments to `move_val_init`");
289 let val = args.next().expect("1 argument to `move_val_init`");
290 assert!(args.next().is_none(), ">2 arguments to `move_val_init`");
292 let ptr = this.hir.mirror(ptr);
294 // Create an *internal* temp for the pointer, so that unsafety
295 // checking won't complain about the raw pointer assignment.
296 let ptr_temp = this.local_decls.push(LocalDecl {
297 mutability: Mutability::Mut,
302 visibility_scope: source_info.scope,
304 is_user_variable: None,
306 let ptr_temp = Place::Local(ptr_temp);
307 let block = unpack!(this.into(&ptr_temp, block, ptr));
308 this.into(&ptr_temp.deref(), block, val)
310 let args: Vec<_> = args
312 .map(|arg| unpack!(block = this.as_local_operand(block, arg)))
315 let success = this.cfg.start_new_block();
316 let cleanup = this.diverge_cleanup();
320 TerminatorKind::Call {
323 cleanup: Some(cleanup),
324 destination: if diverges {
327 Some((destination.clone(), success))
335 // These cases don't actually need a destination
336 ExprKind::Assign { .. }
337 | ExprKind::AssignOp { .. }
338 | ExprKind::Continue { .. }
339 | ExprKind::Break { .. }
340 | ExprKind::InlineAsm { .. }
341 | ExprKind::Return { .. } => {
342 unpack!(block = this.stmt_expr(block, expr));
343 this.cfg.push_assign_unit(block, source_info, destination);
347 // Avoid creating a temporary
348 ExprKind::VarRef { .. } |
350 ExprKind::StaticRef { .. } |
351 ExprKind::PlaceTypeAscription { .. } |
352 ExprKind::ValueTypeAscription { .. } => {
353 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
355 let place = unpack!(block = this.as_place(block, expr));
356 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
358 .push_assign(block, source_info, destination, rvalue);
361 ExprKind::Index { .. } | ExprKind::Deref { .. } | ExprKind::Field { .. } => {
362 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
364 // Create a "fake" temporary variable so that we check that the
365 // value is Sized. Usually, this is caught in type checking, but
366 // in the case of box expr there is no such check.
367 if let Place::Projection(..) = destination {
369 .push(LocalDecl::new_temp(expr.ty, expr.span));
372 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
374 let place = unpack!(block = this.as_place(block, expr));
375 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
377 .push_assign(block, source_info, destination, rvalue);
381 // these are the cases that are more naturally handled by some other mode
382 ExprKind::Unary { .. }
383 | ExprKind::Binary { .. }
384 | ExprKind::Box { .. }
385 | ExprKind::Cast { .. }
386 | ExprKind::Use { .. }
387 | ExprKind::ReifyFnPointer { .. }
388 | ExprKind::ClosureFnPointer { .. }
389 | ExprKind::UnsafeFnPointer { .. }
390 | ExprKind::Unsize { .. }
391 | ExprKind::Repeat { .. }
392 | ExprKind::Borrow { .. }
393 | ExprKind::Array { .. }
394 | ExprKind::Tuple { .. }
395 | ExprKind::Adt { .. }
396 | ExprKind::Closure { .. }
397 | ExprKind::Literal { .. }
398 | ExprKind::Yield { .. } => {
399 debug_assert!(match Category::of(&expr.kind).unwrap() {
400 // should be handled above
401 Category::Rvalue(RvalueFunc::Into) => false,
403 // must be handled above or else we get an
404 // infinite loop in the builder; see
405 // e.g. `ExprKind::VarRef` above
406 Category::Place => false,
411 let rvalue = unpack!(block = this.as_local_rvalue(block, expr));
413 .push_assign(block, source_info, destination, rvalue);