Skip to content

Commit

Permalink
rustfmt src/librustc_mir/build/expr
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-m1 committed Sep 6, 2018
1 parent 527a29a commit d0c1e5a
Show file tree
Hide file tree
Showing 9 changed files with 656 additions and 446 deletions.
32 changes: 20 additions & 12 deletions src/librustc_mir/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,34 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// Compile `expr`, yielding a compile-time constant. Assumes that
/// `expr` is a valid compile-time constant!
pub fn as_constant<M>(&mut self, expr: M) -> Constant<'tcx>
where M: Mirror<'tcx, Output=Expr<'tcx>>
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
{
let expr = self.hir.mirror(expr);
self.expr_as_constant(expr)
}

fn expr_as_constant(&mut self, expr: Expr<'tcx>) -> Constant<'tcx> {
let this = self;
let Expr { ty, temp_lifetime: _, span, kind }
= expr;
let Expr {
ty,
temp_lifetime: _,
span,
kind,
} = expr;
match kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } =>
this.as_constant(value),
ExprKind::Literal { literal, user_ty } =>
Constant { span, ty, user_ty, literal },
_ =>
span_bug!(
span,
"expression is not a valid constant {:?}",
kind),
ExprKind::Scope {
region_scope: _,
lint_level: _,
value,
} => this.as_constant(value),
ExprKind::Literal { literal, user_ty } => Constant {
span,
ty,
user_ty,
literal,
},
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
}
}
}
50 changes: 30 additions & 20 deletions src/librustc_mir/build/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

//! See docs in build/expr/mod.rs
use build::{BlockAnd, BlockAndExtension, Builder};
use build::expr::category::Category;
use build::{BlockAnd, BlockAndExtension, Builder};
use hair::*;
use rustc::middle::region;
use rustc::mir::*;
Expand All @@ -23,9 +23,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// The operand returned from this function will *not be valid* after
/// an ExprKind::Scope is passed, so please do *not* return it from
/// functions to avoid bad miscompiles.
pub fn as_local_operand<M>(&mut self, block: BasicBlock, expr: M)
-> BlockAnd<Operand<'tcx>>
where M: Mirror<'tcx, Output = Expr<'tcx>>
pub fn as_local_operand<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Operand<'tcx>>
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
{
let local_scope = self.local_scope();
self.as_operand(block, local_scope, expr)
Expand All @@ -37,25 +37,34 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// this time.
///
/// The operand is known to be live until the end of `scope`.
pub fn as_operand<M>(&mut self,
block: BasicBlock,
scope: Option<region::Scope>,
expr: M) -> BlockAnd<Operand<'tcx>>
where M: Mirror<'tcx, Output = Expr<'tcx>>
pub fn as_operand<M>(
&mut self,
block: BasicBlock,
scope: Option<region::Scope>,
expr: M,
) -> BlockAnd<Operand<'tcx>>
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
{
let expr = self.hir.mirror(expr);
self.expr_as_operand(block, scope, expr)
}

fn expr_as_operand(&mut self,
mut block: BasicBlock,
scope: Option<region::Scope>,
expr: Expr<'tcx>)
-> BlockAnd<Operand<'tcx>> {
fn expr_as_operand(
&mut self,
mut block: BasicBlock,
scope: Option<region::Scope>,
expr: Expr<'tcx>,
) -> BlockAnd<Operand<'tcx>> {
debug!("expr_as_operand(block={:?}, expr={:?})", block, expr);
let this = self;

if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
if let ExprKind::Scope {
region_scope,
lint_level,
value,
} = expr.kind
{
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, block, |this| {
Expand All @@ -64,16 +73,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
}

let category = Category::of(&expr.kind).unwrap();
debug!("expr_as_operand: category={:?} for={:?}", category, expr.kind);
debug!(
"expr_as_operand: category={:?} for={:?}",
category, expr.kind
);
match category {
Category::Constant => {
let constant = this.as_constant(expr);
block.and(Operand::Constant(box constant))
}
Category::Place |
Category::Rvalue(..) => {
let operand =
unpack!(block = this.as_temp(block, scope, expr, Mutability::Mut));
Category::Place | Category::Rvalue(..) => {
let operand = unpack!(block = this.as_temp(block, scope, expr, Mutability::Mut));
block.and(Operand::Move(Place::Local(operand)))
}
}
Expand Down
172 changes: 93 additions & 79 deletions src/librustc_mir/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@

//! See docs in build/expr/mod.rs
use build::{BlockAnd, BlockAndExtension, Builder};
use build::ForGuard::{OutsideGuard, RefWithinGuard};
use build::expr::category::Category;
use build::ForGuard::{OutsideGuard, RefWithinGuard};
use build::{BlockAnd, BlockAndExtension, Builder};
use hair::*;
use rustc::mir::*;
use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
use rustc::mir::*;

use rustc_data_structures::indexed_vec::Idx;

impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// Compile `expr`, yielding a place that we can move from etc.
pub fn as_place<M>(&mut self,
block: BasicBlock,
expr: M)
-> BlockAnd<Place<'tcx>>
where M: Mirror<'tcx, Output=Expr<'tcx>>
pub fn as_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
{
let expr = self.hir.mirror(expr);
self.expr_as_place(block, expr, Mutability::Mut)
Expand All @@ -36,36 +34,40 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// place. The place itself may or may not be mutable:
/// * If this expr is a place expr like a.b, then we will return that place.
/// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
pub fn as_read_only_place<M>(&mut self,
block: BasicBlock,
expr: M)
-> BlockAnd<Place<'tcx>>
where M: Mirror<'tcx, Output=Expr<'tcx>>
pub fn as_read_only_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
{
let expr = self.hir.mirror(expr);
self.expr_as_place(block, expr, Mutability::Not)
}

fn expr_as_place(&mut self,
mut block: BasicBlock,
expr: Expr<'tcx>,
mutability: Mutability)
-> BlockAnd<Place<'tcx>> {
debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
fn expr_as_place(
&mut self,
mut block: BasicBlock,
expr: Expr<'tcx>,
mutability: Mutability,
) -> BlockAnd<Place<'tcx>> {
debug!(
"expr_as_place(block={:?}, expr={:?}, mutability={:?})",
block, expr, mutability
);

let this = self;
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, block, |this| {
if mutability == Mutability::Not {
this.as_read_only_place(block, value)
} else {
this.as_place(block, value)
}
})
}
ExprKind::Scope {
region_scope,
lint_level,
value,
} => this.in_scope((region_scope, source_info), lint_level, block, |this| {
if mutability == Mutability::Not {
this.as_read_only_place(block, value)
} else {
this.as_place(block, value)
}
}),
ExprKind::Field { lhs, name } => {
let place = unpack!(block = this.as_place(block, lhs));
let place = place.field(name, expr.ty);
Expand All @@ -86,29 +88,40 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));

// bounds check:
let (len, lt) = (this.temp(usize_ty.clone(), expr_span),
this.temp(bool_ty, expr_span));
this.cfg.push_assign(block, source_info, // len = len(slice)
&len, Rvalue::Len(slice.clone()));
this.cfg.push_assign(block, source_info, // lt = idx < len
&lt, Rvalue::BinaryOp(BinOp::Lt,
Operand::Copy(Place::Local(idx)),
Operand::Copy(len.clone())));
let (len, lt) = (
this.temp(usize_ty.clone(), expr_span),
this.temp(bool_ty, expr_span),
);
this.cfg.push_assign(
block,
source_info, // len = len(slice)
&len,
Rvalue::Len(slice.clone()),
);
this.cfg.push_assign(
block,
source_info, // lt = idx < len
&lt,
Rvalue::BinaryOp(
BinOp::Lt,
Operand::Copy(Place::Local(idx)),
Operand::Copy(len.clone()),
),
);

let msg = BoundsCheck {
len: Operand::Move(len),
index: Operand::Copy(Place::Local(idx))
index: Operand::Copy(Place::Local(idx)),
};
let success = this.assert(block, Operand::Move(lt), true,
msg, expr_span);
let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
success.and(slice.index(idx))
}
ExprKind::SelfRef => {
block.and(Place::Local(Local::new(1)))
}
ExprKind::SelfRef => block.and(Place::Local(Local::new(1))),
ExprKind::VarRef { id } => {
let place = if this.is_bound_var_in_guard(id) &&
this.hir.tcx().all_pat_vars_are_implicit_refs_within_guards()
let place = if this.is_bound_var_in_guard(id) && this
.hir
.tcx()
.all_pat_vars_are_implicit_refs_within_guards()
{
let index = this.var_local_id(id, RefWithinGuard);
Place::Local(index).deref()
Expand All @@ -118,47 +131,48 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
};
block.and(place)
}
ExprKind::StaticRef { id } => {
block.and(Place::Static(Box::new(Static { def_id: id, ty: expr.ty })))
}
ExprKind::StaticRef { id } => block.and(Place::Static(Box::new(Static {
def_id: id,
ty: expr.ty,
}))),

ExprKind::Array { .. } |
ExprKind::Tuple { .. } |
ExprKind::Adt { .. } |
ExprKind::Closure { .. } |
ExprKind::Unary { .. } |
ExprKind::Binary { .. } |
ExprKind::LogicalOp { .. } |
ExprKind::Box { .. } |
ExprKind::Cast { .. } |
ExprKind::Use { .. } |
ExprKind::NeverToAny { .. } |
ExprKind::ReifyFnPointer { .. } |
ExprKind::ClosureFnPointer { .. } |
ExprKind::UnsafeFnPointer { .. } |
ExprKind::Unsize { .. } |
ExprKind::Repeat { .. } |
ExprKind::Borrow { .. } |
ExprKind::If { .. } |
ExprKind::Match { .. } |
ExprKind::Loop { .. } |
ExprKind::Block { .. } |
ExprKind::Assign { .. } |
ExprKind::AssignOp { .. } |
ExprKind::Break { .. } |
ExprKind::Continue { .. } |
ExprKind::Return { .. } |
ExprKind::Literal { .. } |
ExprKind::InlineAsm { .. } |
ExprKind::Yield { .. } |
ExprKind::Call { .. } => {
ExprKind::Array { .. }
| ExprKind::Tuple { .. }
| ExprKind::Adt { .. }
| ExprKind::Closure { .. }
| ExprKind::Unary { .. }
| ExprKind::Binary { .. }
| ExprKind::LogicalOp { .. }
| ExprKind::Box { .. }
| ExprKind::Cast { .. }
| ExprKind::Use { .. }
| ExprKind::NeverToAny { .. }
| ExprKind::ReifyFnPointer { .. }
| ExprKind::ClosureFnPointer { .. }
| ExprKind::UnsafeFnPointer { .. }
| ExprKind::Unsize { .. }
| ExprKind::Repeat { .. }
| ExprKind::Borrow { .. }
| ExprKind::If { .. }
| ExprKind::Match { .. }
| ExprKind::Loop { .. }
| ExprKind::Block { .. }
| ExprKind::Assign { .. }
| ExprKind::AssignOp { .. }
| ExprKind::Break { .. }
| ExprKind::Continue { .. }
| ExprKind::Return { .. }
| ExprKind::Literal { .. }
| ExprKind::InlineAsm { .. }
| ExprKind::Yield { .. }
| ExprKind::Call { .. } => {
// these are not places, so we need to make a temporary.
debug_assert!(match Category::of(&expr.kind) {
Some(Category::Place) => false,
_ => true,
});
let temp = unpack!(
block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
let temp =
unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
block.and(Place::Local(temp))
}
}
Expand Down
Loading

0 comments on commit d0c1e5a

Please sign in to comment.