2015-08-18 17:59:21 -04:00
|
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-01-29 19:06:23 +02:00
|
|
|
|
use graphviz::IntoCow;
|
2016-03-30 13:43:36 +02:00
|
|
|
|
use middle::const_val::ConstVal;
|
2016-05-25 08:39:32 +03:00
|
|
|
|
use rustc_const_math::{ConstUsize, ConstInt, ConstMathErr};
|
2016-06-07 17:28:36 +03:00
|
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
2016-06-09 15:49:07 -07:00
|
|
|
|
use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
|
|
|
|
|
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
|
|
|
|
|
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
|
2016-09-15 00:51:46 +03:00
|
|
|
|
use hir::def::CtorKind;
|
2016-03-29 12:54:26 +03:00
|
|
|
|
use hir::def_id::DefId;
|
2016-03-22 17:30:57 +02:00
|
|
|
|
use ty::subst::Substs;
|
2016-07-31 22:33:41 +08:00
|
|
|
|
use ty::{self, AdtDef, ClosureSubsts, Region, Ty};
|
2016-02-23 22:04:27 +02:00
|
|
|
|
use util::ppaux;
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 16:28:45 -07:00
|
|
|
|
use rustc_back::slice;
|
2016-03-29 08:50:44 +03:00
|
|
|
|
use hir::InlineAsm;
|
2016-01-05 23:06:33 -06:00
|
|
|
|
use std::ascii;
|
2016-01-29 19:06:23 +02:00
|
|
|
|
use std::borrow::{Cow};
|
2016-06-07 22:02:08 +03:00
|
|
|
|
use std::cell::Ref;
|
2015-12-31 20:11:25 -06:00
|
|
|
|
use std::fmt::{self, Debug, Formatter, Write};
|
2015-12-18 19:29:03 -06:00
|
|
|
|
use std::{iter, u32};
|
2016-01-07 05:49:46 -05:00
|
|
|
|
use std::ops::{Index, IndexMut};
|
2016-06-09 15:49:07 -07:00
|
|
|
|
use std::vec::IntoIter;
|
2017-01-06 21:54:24 +02:00
|
|
|
|
use syntax::ast::Name;
|
2016-06-21 18:08:13 -04:00
|
|
|
|
use syntax_pos::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-09-19 23:50:00 +03:00
|
|
|
|
mod cache;
|
|
|
|
|
pub mod tcx;
|
|
|
|
|
pub mod visit;
|
|
|
|
|
pub mod transform;
|
|
|
|
|
pub mod traversal;
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
macro_rules! newtype_index {
|
|
|
|
|
($name:ident, $debug_name:expr) => (
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
|
|
|
|
|
RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct $name(u32);
|
|
|
|
|
|
|
|
|
|
impl Idx for $name {
|
|
|
|
|
fn new(value: usize) -> Self {
|
|
|
|
|
assert!(value < (u32::MAX) as usize);
|
|
|
|
|
$name(value as u32)
|
|
|
|
|
}
|
|
|
|
|
fn index(self) -> usize {
|
|
|
|
|
self.0 as usize
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Debug for $name {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
|
write!(fmt, "{}{}", $debug_name, self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// Lowered representation of a single function.
|
2017-02-06 10:12:30 +08:00
|
|
|
|
// Do not implement clone for Mir, which can be accidently done and kind of expensive.
|
2016-10-31 02:16:21 +02:00
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, Debug)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Mir<'tcx> {
|
2015-11-12 14:29:23 -05:00
|
|
|
|
/// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
|
|
|
|
|
/// that indexes into this vector.
|
2016-06-07 21:20:50 +03:00
|
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
|
/// List of visibility (lexical) scopes; these are referenced by statements
|
|
|
|
|
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
|
2016-06-07 17:28:36 +03:00
|
|
|
|
pub visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
2016-05-03 00:26:41 +03:00
|
|
|
|
/// Rvalues promoted from this function, such as borrows of constants.
|
|
|
|
|
/// Each of them is the Mir of a constant with the fn's type parameters
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// in scope, but a separate set of locals.
|
2016-06-07 17:28:36 +03:00
|
|
|
|
pub promoted: IndexVec<Promoted, Mir<'tcx>>,
|
2016-05-03 00:26:41 +03:00
|
|
|
|
|
2015-11-12 14:29:23 -05:00
|
|
|
|
/// Return type of the function.
|
2016-07-31 22:33:41 +08:00
|
|
|
|
pub return_ty: Ty<'tcx>,
|
2015-10-21 17:14:25 -04:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Declarations of locals.
|
|
|
|
|
///
|
|
|
|
|
/// The first local is the return value pointer, followed by `arg_count`
|
|
|
|
|
/// locals for the function arguments, followed by any user-declared
|
|
|
|
|
/// variables and temporaries.
|
|
|
|
|
pub local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
2015-11-12 14:29:23 -05:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Number of arguments this function takes.
|
|
|
|
|
///
|
2016-09-29 01:11:54 +02:00
|
|
|
|
/// Starting at local 1, `arg_count` locals will be provided by the caller
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// and can be assumed to be initialized.
|
|
|
|
|
///
|
|
|
|
|
/// If this MIR was built for a constant, this will be 0.
|
|
|
|
|
pub arg_count: usize,
|
2016-02-07 21:13:00 +02:00
|
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
|
/// Names and capture modes of all the closure upvars, assuming
|
|
|
|
|
/// the first argument is either the closure or a reference to it.
|
|
|
|
|
pub upvar_decls: Vec<UpvarDecl>,
|
|
|
|
|
|
2016-09-26 22:44:01 +02:00
|
|
|
|
/// Mark an argument local (which must be a tuple) as getting passed as
|
|
|
|
|
/// its individual components at the LLVM level.
|
2016-09-21 00:45:30 +02:00
|
|
|
|
///
|
|
|
|
|
/// This is used for the "rust-call" ABI.
|
2016-09-26 22:44:01 +02:00
|
|
|
|
pub spread_arg: Option<Local>,
|
2016-09-21 00:45:30 +02:00
|
|
|
|
|
2016-02-07 21:13:00 +02:00
|
|
|
|
/// A span representing this MIR, for error reporting
|
|
|
|
|
pub span: Span,
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
|
|
|
|
/// A cache for various calculations
|
2016-09-19 23:50:00 +03:00
|
|
|
|
cache: cache::Cache
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// where execution begins
|
|
|
|
|
pub const START_BLOCK: BasicBlock = BasicBlock(0);
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Mir<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
|
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
|
|
|
|
|
promoted: IndexVec<Promoted, Mir<'tcx>>,
|
2016-07-31 22:33:41 +08:00
|
|
|
|
return_ty: Ty<'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
|
|
|
|
arg_count: usize,
|
2016-06-07 21:20:50 +03:00
|
|
|
|
upvar_decls: Vec<UpvarDecl>,
|
|
|
|
|
span: Span) -> Self
|
|
|
|
|
{
|
2016-09-25 01:38:27 +02:00
|
|
|
|
// We need `arg_count` locals, and one for the return pointer
|
|
|
|
|
assert!(local_decls.len() >= arg_count + 1,
|
|
|
|
|
"expected at least {} locals, got {}", arg_count + 1, local_decls.len());
|
|
|
|
|
assert_eq!(local_decls[RETURN_POINTER].ty, return_ty);
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
Mir {
|
|
|
|
|
basic_blocks: basic_blocks,
|
|
|
|
|
visibility_scopes: visibility_scopes,
|
|
|
|
|
promoted: promoted,
|
|
|
|
|
return_ty: return_ty,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
local_decls: local_decls,
|
|
|
|
|
arg_count: arg_count,
|
2016-06-07 21:20:50 +03:00
|
|
|
|
upvar_decls: upvar_decls,
|
2016-09-26 22:44:01 +02:00
|
|
|
|
spread_arg: None,
|
2016-06-07 22:02:08 +03:00
|
|
|
|
span: span,
|
2016-09-19 23:50:00 +03:00
|
|
|
|
cache: cache::Cache::new()
|
2016-06-07 21:20:50 +03:00
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
|
|
|
|
&self.basic_blocks
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
2016-06-07 22:02:08 +03:00
|
|
|
|
self.cache.invalidate();
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&mut self.basic_blocks
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn predecessors(&self) -> Ref<IndexVec<BasicBlock, Vec<BasicBlock>>> {
|
|
|
|
|
self.cache.predecessors(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn predecessors_for(&self, bb: BasicBlock) -> Ref<Vec<BasicBlock>> {
|
|
|
|
|
Ref::map(self.predecessors(), |p| &p[bb])
|
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
|
|
2016-06-09 15:49:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn dominators(&self) -> Dominators<BasicBlock> {
|
|
|
|
|
dominators(self)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn local_kind(&self, local: Local) -> LocalKind {
|
|
|
|
|
let index = local.0 as usize;
|
|
|
|
|
if index == 0 {
|
|
|
|
|
debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
|
|
|
|
|
"return pointer should be mutable");
|
|
|
|
|
|
|
|
|
|
LocalKind::ReturnPointer
|
|
|
|
|
} else if index < self.arg_count + 1 {
|
|
|
|
|
LocalKind::Arg
|
|
|
|
|
} else if self.local_decls[local].name.is_some() {
|
|
|
|
|
LocalKind::Var
|
|
|
|
|
} else {
|
|
|
|
|
debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
|
|
|
|
|
"temp should be mutable");
|
|
|
|
|
|
|
|
|
|
LocalKind::Temp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all temporaries.
|
|
|
|
|
#[inline]
|
2016-09-26 22:50:03 +02:00
|
|
|
|
pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
|
let local = Local::new(index);
|
|
|
|
|
if self.local_decls[local].source_info.is_none() {
|
|
|
|
|
Some(local)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2016-06-20 23:55:14 +03:00
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all user-declared locals.
|
|
|
|
|
#[inline]
|
2016-09-26 22:50:03 +02:00
|
|
|
|
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
|
let local = Local::new(index);
|
|
|
|
|
if self.local_decls[local].source_info.is_none() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(local)
|
2016-06-20 23:55:14 +03:00
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all function arguments.
|
|
|
|
|
#[inline]
|
2016-09-26 22:51:51 +02:00
|
|
|
|
pub fn args_iter(&self) -> impl Iterator<Item=Local> {
|
|
|
|
|
let arg_count = self.arg_count;
|
|
|
|
|
(1..arg_count+1).map(Local::new)
|
2016-09-25 01:38:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
|
|
|
|
|
/// locals that are neither arguments nor the return pointer).
|
|
|
|
|
#[inline]
|
2016-09-27 02:03:35 +02:00
|
|
|
|
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item=Local> {
|
|
|
|
|
let arg_count = self.arg_count;
|
|
|
|
|
let local_count = self.local_decls.len();
|
|
|
|
|
(arg_count+1..local_count).map(Local::new)
|
2016-09-15 18:18:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
|
pub fn make_statement_nop(&mut self, location: Location) {
|
|
|
|
|
let block = &mut self[location.block];
|
|
|
|
|
debug_assert!(location.statement_index < block.statements.len());
|
|
|
|
|
block.statements[location.statement_index].make_nop()
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 05:49:46 -05:00
|
|
|
|
impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
|
|
|
|
|
type Output = BasicBlockData<'tcx>;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&self.basic_blocks()[index]
|
2016-01-07 05:49:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&mut self.basic_blocks_mut()[index]
|
2016-01-07 05:49:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
|
/// Grouped information about the source code origin of a MIR entity.
|
|
|
|
|
/// Intended to be inspected by diagnostics and debuginfo.
|
|
|
|
|
/// Most passes can work with it as a whole, within a single function.
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct SourceInfo {
|
|
|
|
|
/// Source span for the AST pertaining to this MIR entity.
|
|
|
|
|
pub span: Span,
|
|
|
|
|
|
|
|
|
|
/// The lexical visibility scope, i.e. which bindings can be seen.
|
|
|
|
|
pub scope: VisibilityScope
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Mutability and borrow kinds
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum Mutability {
|
|
|
|
|
Mut,
|
|
|
|
|
Not,
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum BorrowKind {
|
|
|
|
|
/// Data must be immutable and is aliasable.
|
|
|
|
|
Shared,
|
|
|
|
|
|
|
|
|
|
/// Data must be immutable but not aliasable. This kind of borrow
|
|
|
|
|
/// cannot currently be expressed by the user and is used only in
|
|
|
|
|
/// implicit closure bindings. It is needed when you the closure
|
|
|
|
|
/// is borrowing or mutating a mutable referent, e.g.:
|
|
|
|
|
///
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = || *x += 5;
|
|
|
|
|
///
|
|
|
|
|
/// If we were to try to translate this closure into a more explicit
|
|
|
|
|
/// form, we'd encounter an error with the code as written:
|
|
|
|
|
///
|
|
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
|
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
|
///
|
|
|
|
|
/// This is then illegal because you cannot mutate a `&mut` found
|
|
|
|
|
/// in an aliasable location. To solve, you'd have to translate with
|
|
|
|
|
/// an `&mut` borrow:
|
|
|
|
|
///
|
|
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
|
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
|
///
|
|
|
|
|
/// Now the assignment to `**env.x` is legal, but creating a
|
|
|
|
|
/// mutable pointer to `x` is not because `x` is not mutable. We
|
|
|
|
|
/// could fix this by declaring `x` as `let mut x`. This is ok in
|
|
|
|
|
/// user code, if awkward, but extra weird for closures, since the
|
|
|
|
|
/// borrow is hidden.
|
|
|
|
|
///
|
|
|
|
|
/// So we introduce a "unique imm" borrow -- the referent is
|
|
|
|
|
/// immutable, but not aliasable. This solves the problem. For
|
|
|
|
|
/// simplicity, we don't give users the way to express this
|
|
|
|
|
/// borrow, it's just used when translating closures.
|
|
|
|
|
Unique,
|
|
|
|
|
|
|
|
|
|
/// Data is mutable and not aliasable.
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Mut,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Variables and temps
|
|
|
|
|
|
2016-09-29 01:11:54 +02:00
|
|
|
|
newtype_index!(Local, "_");
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
|
|
|
|
pub const RETURN_POINTER: Local = Local(0);
|
|
|
|
|
|
|
|
|
|
/// Classifies locals into categories. See `Mir::local_kind`.
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
|
pub enum LocalKind {
|
|
|
|
|
/// User-declared variable binding
|
|
|
|
|
Var,
|
|
|
|
|
/// Compiler-introduced temporary
|
|
|
|
|
Temp,
|
|
|
|
|
/// Function argument
|
|
|
|
|
Arg,
|
|
|
|
|
/// Location of function's return value
|
|
|
|
|
ReturnPointer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A MIR local.
|
|
|
|
|
///
|
|
|
|
|
/// This can be a binding declared by the user, a temporary inserted by the compiler, a function
|
|
|
|
|
/// argument, or the return pointer.
|
2016-02-07 21:13:00 +02:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
|
pub struct LocalDecl<'tcx> {
|
|
|
|
|
/// `let mut x` vs `let x`.
|
|
|
|
|
///
|
|
|
|
|
/// Temporaries and the return pointer are always mutable.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub mutability: Mutability,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Type of this local.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub ty: Ty<'tcx>,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Name of the local, used in debuginfo and pretty-printing.
|
|
|
|
|
///
|
|
|
|
|
/// Note that function arguments can also have this set to `Some(_)`
|
|
|
|
|
/// to generate better debuginfo.
|
|
|
|
|
pub name: Option<Name>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// For user-declared variables, stores their source information.
|
|
|
|
|
///
|
|
|
|
|
/// For temporaries, this is `None`.
|
|
|
|
|
///
|
|
|
|
|
/// This is the primary way to differentiate between user-declared
|
|
|
|
|
/// variables and compiler-generated temporaries.
|
|
|
|
|
pub source_info: Option<SourceInfo>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
impl<'tcx> LocalDecl<'tcx> {
|
|
|
|
|
/// Create a new `LocalDecl` for a temporary.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_temp(ty: Ty<'tcx>) -> Self {
|
|
|
|
|
LocalDecl {
|
|
|
|
|
mutability: Mutability::Mut,
|
|
|
|
|
ty: ty,
|
|
|
|
|
name: None,
|
|
|
|
|
source_info: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-08 14:24:44 +02:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Builds a `LocalDecl` for the return pointer.
|
|
|
|
|
///
|
|
|
|
|
/// This must be inserted into the `local_decls` list as the first local.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_return_pointer(return_ty: Ty) -> LocalDecl {
|
|
|
|
|
LocalDecl {
|
|
|
|
|
mutability: Mutability::Mut,
|
|
|
|
|
ty: return_ty,
|
|
|
|
|
source_info: None,
|
|
|
|
|
name: None, // FIXME maybe we do want some name here?
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-16 21:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A closure capture, with its name and mode.
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct UpvarDecl {
|
|
|
|
|
pub debug_name: Name,
|
|
|
|
|
|
|
|
|
|
/// If true, the capture is behind a reference.
|
|
|
|
|
pub by_ref: bool
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// BasicBlock
|
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
newtype_index!(BasicBlock, "bb");
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-26 18:05:50 +02:00
|
|
|
|
// BasicBlockData and Terminator
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct BasicBlockData<'tcx> {
|
2016-03-24 06:12:19 -04:00
|
|
|
|
/// List of statements in this block.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub statements: Vec<Statement<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
|
|
/// Terminator for this block.
|
|
|
|
|
///
|
|
|
|
|
/// NB. This should generally ONLY be `None` during construction.
|
|
|
|
|
/// Therefore, you should generally access it via the
|
|
|
|
|
/// `terminator()` or `terminator_mut()` methods. The only
|
|
|
|
|
/// exception is that certain passes, such as `simplify_cfg`, swap
|
|
|
|
|
/// out the terminator temporarily with `None` while they continue
|
|
|
|
|
/// to recurse over the set of basic blocks.
|
2015-12-19 00:44:32 +02:00
|
|
|
|
pub terminator: Option<Terminator<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
|
|
/// If true, this block lies on an unwind path. This is used
|
|
|
|
|
/// during trans where distinct kinds of basic blocks may be
|
|
|
|
|
/// generated (particularly for MSVC cleanup). Unwind blocks must
|
|
|
|
|
/// only branch to other unwind blocks.
|
2015-12-20 15:30:09 +02:00
|
|
|
|
pub is_cleanup: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct Terminator<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub kind: TerminatorKind<'tcx>
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub enum TerminatorKind<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// block should have one successor in the graph; we jump there
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Goto {
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-26 14:35:18 -04:00
|
|
|
|
/// operand evaluates to an integer; jump depending on its value
|
|
|
|
|
/// to one of the targets, and otherwise fallback to `otherwise`
|
|
|
|
|
SwitchInt {
|
|
|
|
|
/// discriminant value being tested
|
2017-01-31 05:32:08 +02:00
|
|
|
|
discr: Operand<'tcx>,
|
2015-10-26 14:35:18 -04:00
|
|
|
|
|
|
|
|
|
/// type of value being tested
|
|
|
|
|
switch_ty: Ty<'tcx>,
|
|
|
|
|
|
|
|
|
|
/// Possible values. The locations to branch to in each case
|
|
|
|
|
/// are found in the corresponding indices from the `targets` vector.
|
2017-02-02 06:44:30 +02:00
|
|
|
|
values: Cow<'tcx, [ConstInt]>,
|
2015-10-26 14:35:18 -04:00
|
|
|
|
|
2017-02-02 20:35:54 +02:00
|
|
|
|
/// Possible branch sites. The last element of this vector is used
|
|
|
|
|
/// for the otherwise branch, so values.len() == targets.len() + 1
|
|
|
|
|
/// should hold.
|
|
|
|
|
// This invariant is quite non-obvious and also could be improved.
|
|
|
|
|
// One way to make this invariant is to have something like this instead:
|
|
|
|
|
//
|
|
|
|
|
// branches: Vec<(ConstInt, BasicBlock)>,
|
|
|
|
|
// otherwise: Option<BasicBlock> // exhaustive if None
|
|
|
|
|
//
|
|
|
|
|
// However we’ve decided to keep this as-is until we figure a case
|
|
|
|
|
// where some other approach seems to be strictly better than other.
|
2015-10-26 14:35:18 -04:00
|
|
|
|
targets: Vec<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-15 20:46:39 +02:00
|
|
|
|
/// Indicates that the landing pad is finished and unwinding should
|
|
|
|
|
/// continue. Emitted by build::scope::diverge_cleanup.
|
|
|
|
|
Resume,
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Indicates a normal return. The return pointer lvalue should
|
MIR: Do not require END_BLOCK to always exist
Once upon a time, along with START_BLOCK and END_BLOCK in the castle of important blocks also lived
a RESUME_BLOCK (or was it UNWIND_BLOCK? Either works, I don’t remember anymore). This trinity of
important blocks were required to always exist from the birth to death of the MIR-land they
belonged to.
Some time later, it was discovered that RESUME_BLOCK was just a lazy goon enjoying comfortable life
in the light of fame of the other two. Needless to say, once found out, the RESUME_BLOCK was
quickly slain and disposed of.
Now, the all-seeing eye of ours discovers that END_BLOCK is actually the more evil and better
disguised twin of the slain RESUME_BLOCK. Thus END_BLOCK gets slain and quickly disposed
of. Glory to the START_BLOCK, one and only lord of the important blocks’ castle!
---
Basically, all this does, is removing restriction for END_BLOCK to exist past the first invocation
of RemoveDeadBlocks pass. This way for functions whose CFG does not reach the `END_BLOCK` end up
not containing the block.
As far as the implementation goes, I’m not entirely satisfied with the `BasicBlock::end_block`, I
had hoped to make `new` a `const fn` and then just have a `const END_BLOCK` private to mir::build,
but it turns out that constant functions don’t yet support conditionals nor a way to assert.
2016-04-16 18:09:51 +03:00
|
|
|
|
/// have been filled in by now. This should occur at most once.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Return,
|
|
|
|
|
|
2016-06-08 19:26:19 +03:00
|
|
|
|
/// Indicates a terminator that can never be reached.
|
|
|
|
|
Unreachable,
|
|
|
|
|
|
2016-01-30 00:18:47 +02:00
|
|
|
|
/// Drop the Lvalue
|
|
|
|
|
Drop {
|
2016-05-17 01:06:52 +03:00
|
|
|
|
location: Lvalue<'tcx>,
|
2016-01-30 00:18:47 +02:00
|
|
|
|
target: BasicBlock,
|
|
|
|
|
unwind: Option<BasicBlock>
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-17 01:06:52 +03:00
|
|
|
|
/// Drop the Lvalue and assign the new value over it
|
|
|
|
|
DropAndReplace {
|
|
|
|
|
location: Lvalue<'tcx>,
|
|
|
|
|
value: Operand<'tcx>,
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
unwind: Option<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-14 23:27:58 +02:00
|
|
|
|
/// Block ends with a call of a converging function
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Call {
|
2015-12-14 23:27:58 +02:00
|
|
|
|
/// The function that’s being called
|
|
|
|
|
func: Operand<'tcx>,
|
|
|
|
|
/// Arguments the function is called with
|
|
|
|
|
args: Vec<Operand<'tcx>>,
|
2016-01-29 20:42:02 +02:00
|
|
|
|
/// Destination for the return value. If some, the call is converging.
|
|
|
|
|
destination: Option<(Lvalue<'tcx>, BasicBlock)>,
|
|
|
|
|
/// Cleanups to be done if the call unwinds.
|
|
|
|
|
cleanup: Option<BasicBlock>
|
2015-10-07 14:37:42 +02:00
|
|
|
|
},
|
2016-05-25 08:39:32 +03:00
|
|
|
|
|
|
|
|
|
/// Jump to the target if the condition has the expected value,
|
|
|
|
|
/// otherwise panic with a message and a cleanup target.
|
|
|
|
|
Assert {
|
|
|
|
|
cond: Operand<'tcx>,
|
|
|
|
|
expected: bool,
|
|
|
|
|
msg: AssertMessage<'tcx>,
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
cleanup: Option<BasicBlock>
|
|
|
|
|
}
|
2015-12-14 23:27:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Terminator<'tcx> {
|
2016-01-29 19:06:23 +02:00
|
|
|
|
pub fn successors(&self) -> Cow<[BasicBlock]> {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
self.kind.successors()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn successors_mut(&mut self) -> Vec<&mut BasicBlock> {
|
|
|
|
|
self.kind.successors_mut()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TerminatorKind<'tcx> {
|
2017-02-03 03:36:32 +02:00
|
|
|
|
pub fn if_<'a, 'gcx>(tcx: ty::TyCtxt<'a, 'gcx, 'tcx>, cond: Operand<'tcx>,
|
|
|
|
|
t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
|
2017-02-15 15:00:20 +02:00
|
|
|
|
static BOOL_SWITCH_FALSE: &'static [ConstInt] = &[ConstInt::U8(0)];
|
2017-02-03 03:36:32 +02:00
|
|
|
|
TerminatorKind::SwitchInt {
|
|
|
|
|
discr: cond,
|
|
|
|
|
switch_ty: tcx.types.bool,
|
|
|
|
|
values: From::from(BOOL_SWITCH_FALSE),
|
|
|
|
|
targets: vec![f, t],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub fn successors(&self) -> Cow<[BasicBlock]> {
|
|
|
|
|
use self::TerminatorKind::*;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
match *self {
|
2016-01-29 19:06:23 +02:00
|
|
|
|
Goto { target: ref b } => slice::ref_slice(b).into_cow(),
|
|
|
|
|
SwitchInt { targets: ref b, .. } => b[..].into_cow(),
|
|
|
|
|
Resume => (&[]).into_cow(),
|
|
|
|
|
Return => (&[]).into_cow(),
|
2016-06-08 19:26:19 +03:00
|
|
|
|
Unreachable => (&[]).into_cow(),
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { destination: Some((_, t)), cleanup: Some(c), .. } => vec![t, c].into_cow(),
|
|
|
|
|
Call { destination: Some((_, ref t)), cleanup: None, .. } =>
|
|
|
|
|
slice::ref_slice(t).into_cow(),
|
|
|
|
|
Call { destination: None, cleanup: Some(ref c), .. } => slice::ref_slice(c).into_cow(),
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => (&[]).into_cow(),
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { target, unwind: Some(unwind), .. } |
|
|
|
|
|
Drop { target, unwind: Some(unwind), .. } => {
|
|
|
|
|
vec![target, unwind].into_cow()
|
|
|
|
|
}
|
|
|
|
|
DropAndReplace { ref target, unwind: None, .. } |
|
|
|
|
|
Drop { ref target, unwind: None, .. } => {
|
|
|
|
|
slice::ref_slice(target).into_cow()
|
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { target, cleanup: Some(unwind), .. } => vec![target, unwind].into_cow(),
|
|
|
|
|
Assert { ref target, .. } => slice::ref_slice(target).into_cow(),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 21:38:36 +01:00
|
|
|
|
|
2016-01-29 19:06:23 +02:00
|
|
|
|
// FIXME: no mootable cow. I’m honestly not sure what a “cow” between `&mut [BasicBlock]` and
|
|
|
|
|
// `Vec<&mut BasicBlock>` would look like in the first place.
|
|
|
|
|
pub fn successors_mut(&mut self) -> Vec<&mut BasicBlock> {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-11-10 21:38:36 +01:00
|
|
|
|
match *self {
|
2016-01-29 19:06:23 +02:00
|
|
|
|
Goto { target: ref mut b } => vec![b],
|
|
|
|
|
SwitchInt { targets: ref mut b, .. } => b.iter_mut().collect(),
|
|
|
|
|
Resume => Vec::new(),
|
|
|
|
|
Return => Vec::new(),
|
2016-06-08 19:26:19 +03:00
|
|
|
|
Unreachable => Vec::new(),
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut c), .. } => vec![t, c],
|
|
|
|
|
Call { destination: Some((_, ref mut t)), cleanup: None, .. } => vec![t],
|
|
|
|
|
Call { destination: None, cleanup: Some(ref mut c), .. } => vec![c],
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => vec![],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { ref mut target, unwind: Some(ref mut unwind), .. } |
|
2016-01-30 00:18:47 +02:00
|
|
|
|
Drop { ref mut target, unwind: Some(ref mut unwind), .. } => vec![target, unwind],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { ref mut target, unwind: None, .. } |
|
|
|
|
|
Drop { ref mut target, unwind: None, .. } => {
|
|
|
|
|
vec![target]
|
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { ref mut target, cleanup: Some(ref mut unwind), .. } => vec![target, unwind],
|
|
|
|
|
Assert { ref mut target, .. } => vec![target]
|
2015-11-10 21:38:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> BasicBlockData<'tcx> {
|
2015-12-19 00:44:32 +02:00
|
|
|
|
pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
BasicBlockData {
|
|
|
|
|
statements: vec![],
|
|
|
|
|
terminator: terminator,
|
2015-12-20 15:30:09 +02:00
|
|
|
|
is_cleanup: false,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-19 00:44:32 +02:00
|
|
|
|
|
|
|
|
|
/// Accessor for terminator.
|
|
|
|
|
///
|
|
|
|
|
/// Terminator may not be None after construction of the basic block is complete. This accessor
|
|
|
|
|
/// provides a convenience way to reach the terminator.
|
|
|
|
|
pub fn terminator(&self) -> &Terminator<'tcx> {
|
|
|
|
|
self.terminator.as_ref().expect("invalid terminator state")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
|
|
|
|
|
self.terminator.as_mut().expect("invalid terminator state")
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
impl<'tcx> Debug for TerminatorKind<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
self.fmt_head(fmt)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
let successors = self.successors();
|
|
|
|
|
let labels = self.fmt_successor_labels();
|
|
|
|
|
assert_eq!(successors.len(), labels.len());
|
|
|
|
|
|
|
|
|
|
match successors.len() {
|
|
|
|
|
0 => Ok(()),
|
|
|
|
|
|
|
|
|
|
1 => write!(fmt, " -> {:?}", successors[0]),
|
|
|
|
|
|
|
|
|
|
_ => {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, " -> [")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
for (i, target) in successors.iter().enumerate() {
|
|
|
|
|
if i > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, ", ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{}: {:?}", labels[i], target)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
write!(fmt, "]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
impl<'tcx> TerminatorKind<'tcx> {
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Write the "head" part of the terminator; that is, its name and the data it uses to pick the
|
|
|
|
|
/// successor basic block, if any. The only information not inlcuded is the list of possible
|
|
|
|
|
/// successors, which may be rendered differently between the text and the graphviz format.
|
2015-12-31 20:11:25 -06:00
|
|
|
|
pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
match *self {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Goto { .. } => write!(fmt, "goto"),
|
|
|
|
|
SwitchInt { discr: ref lv, .. } => write!(fmt, "switchInt({:?})", lv),
|
|
|
|
|
Return => write!(fmt, "return"),
|
2015-12-15 20:46:39 +02:00
|
|
|
|
Resume => write!(fmt, "resume"),
|
2016-06-08 19:26:19 +03:00
|
|
|
|
Unreachable => write!(fmt, "unreachable"),
|
2016-05-17 01:06:52 +03:00
|
|
|
|
Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
|
|
|
|
|
DropAndReplace { ref location, ref value, .. } =>
|
|
|
|
|
write!(fmt, "replace({:?} <- {:?})", location, value),
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
|
|
|
|
if let Some((ref destination, _)) = *destination {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?} = ", destination)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?}(", func)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
for (index, arg) in args.iter().enumerate() {
|
|
|
|
|
if index > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, ", ")?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?}", arg)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
|
|
|
|
write!(fmt, ")")
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { ref cond, expected, ref msg, .. } => {
|
|
|
|
|
write!(fmt, "assert(")?;
|
|
|
|
|
if !expected {
|
|
|
|
|
write!(fmt, "!")?;
|
|
|
|
|
}
|
|
|
|
|
write!(fmt, "{:?}, ", cond)?;
|
|
|
|
|
|
|
|
|
|
match *msg {
|
|
|
|
|
AssertMessage::BoundsCheck { ref len, ref index } => {
|
|
|
|
|
write!(fmt, "{:?}, {:?}, {:?}",
|
|
|
|
|
"index out of bounds: the len is {} but the index is {}",
|
|
|
|
|
len, index)?;
|
|
|
|
|
}
|
|
|
|
|
AssertMessage::Math(ref err) => {
|
|
|
|
|
write!(fmt, "{:?}", err.description())?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write!(fmt, ")")
|
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Return the list of labels for the edges to the successor basic blocks.
|
2015-12-18 19:29:03 -06:00
|
|
|
|
pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
match *self {
|
2016-06-08 19:26:19 +03:00
|
|
|
|
Return | Resume | Unreachable => vec![],
|
2016-01-29 19:06:23 +02:00
|
|
|
|
Goto { .. } => vec!["".into()],
|
2015-12-18 19:29:03 -06:00
|
|
|
|
SwitchInt { ref values, .. } => {
|
|
|
|
|
values.iter()
|
|
|
|
|
.map(|const_val| {
|
|
|
|
|
let mut buf = String::new();
|
2017-02-02 06:44:30 +02:00
|
|
|
|
fmt_const_val(&mut buf, &ConstVal::Integral(*const_val)).unwrap();
|
2016-01-29 19:06:23 +02:00
|
|
|
|
buf.into()
|
2015-12-18 19:29:03 -06:00
|
|
|
|
})
|
2016-01-29 19:06:23 +02:00
|
|
|
|
.chain(iter::once(String::from("otherwise").into()))
|
2015-12-18 19:29:03 -06:00
|
|
|
|
.collect()
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { destination: Some(_), cleanup: Some(_), .. } =>
|
|
|
|
|
vec!["return".into_cow(), "unwind".into_cow()],
|
|
|
|
|
Call { destination: Some(_), cleanup: None, .. } => vec!["return".into_cow()],
|
|
|
|
|
Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into_cow()],
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => vec![],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { unwind: None, .. } |
|
2016-01-30 00:18:47 +02:00
|
|
|
|
Drop { unwind: None, .. } => vec!["return".into_cow()],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { unwind: Some(_), .. } |
|
|
|
|
|
Drop { unwind: Some(_), .. } => {
|
|
|
|
|
vec!["return".into_cow(), "unwind".into_cow()]
|
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { cleanup: None, .. } => vec!["".into()],
|
|
|
|
|
Assert { .. } =>
|
|
|
|
|
vec!["success".into_cow(), "unwind".into_cow()]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub enum AssertMessage<'tcx> {
|
|
|
|
|
BoundsCheck {
|
|
|
|
|
len: Operand<'tcx>,
|
|
|
|
|
index: Operand<'tcx>
|
|
|
|
|
},
|
|
|
|
|
Math(ConstMathErr)
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Statements
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Statement<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub kind: StatementKind<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
impl<'tcx> Statement<'tcx> {
|
|
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
|
pub fn make_nop(&mut self) {
|
|
|
|
|
self.kind = StatementKind::Nop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum StatementKind<'tcx> {
|
2016-08-14 06:34:14 +03:00
|
|
|
|
/// Write the RHS Rvalue to the LHS Lvalue.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Assign(Lvalue<'tcx>, Rvalue<'tcx>),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
|
|
|
|
|
/// Write the discriminant for a variant to the enum Lvalue.
|
|
|
|
|
SetDiscriminant { lvalue: Lvalue<'tcx>, variant_index: usize },
|
|
|
|
|
|
|
|
|
|
/// Start a live range for the storage of the local.
|
|
|
|
|
StorageLive(Lvalue<'tcx>),
|
|
|
|
|
|
|
|
|
|
/// End the current live range for the storage of the local.
|
|
|
|
|
StorageDead(Lvalue<'tcx>),
|
2016-09-15 18:17:58 -07:00
|
|
|
|
|
2017-02-15 21:21:36 +02:00
|
|
|
|
InlineAsm {
|
|
|
|
|
asm: InlineAsm,
|
|
|
|
|
outputs: Vec<Lvalue<'tcx>>,
|
|
|
|
|
inputs: Vec<Operand<'tcx>>
|
|
|
|
|
},
|
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
/// No-op. Useful for deleting instructions without affecting statement indices.
|
|
|
|
|
Nop,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Statement<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::StatementKind::*;
|
|
|
|
|
match self.kind {
|
2016-08-04 16:14:33 -07:00
|
|
|
|
Assign(ref lv, ref rv) => write!(fmt, "{:?} = {:?}", lv, rv),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
StorageLive(ref lv) => write!(fmt, "StorageLive({:?})", lv),
|
|
|
|
|
StorageDead(ref lv) => write!(fmt, "StorageDead({:?})", lv),
|
2016-08-04 16:14:33 -07:00
|
|
|
|
SetDiscriminant{lvalue: ref lv, variant_index: index} => {
|
|
|
|
|
write!(fmt, "discriminant({:?}) = {:?}", lv, index)
|
2017-02-15 21:21:36 +02:00
|
|
|
|
},
|
|
|
|
|
InlineAsm { ref asm, ref outputs, ref inputs } => {
|
|
|
|
|
write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
|
|
|
|
|
},
|
2016-09-15 18:17:58 -07:00
|
|
|
|
Nop => write!(fmt, "nop"),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-09 13:36:04 -05:00
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Lvalues
|
|
|
|
|
|
|
|
|
|
/// A path to a value; something that can be evaluated without
|
|
|
|
|
/// changing or disturbing program state.
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Lvalue<'tcx> {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// local variable
|
|
|
|
|
Local(Local),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
/// static or static mut variable
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Static(DefId),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
/// projection out of an lvalue (access a field, deref a pointer, etc)
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Projection(Box<LvalueProjection<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The `Projection` data structure defines things of the form `B.x`
|
|
|
|
|
/// or `*B` or `B[index]`. Note that it is parameterized because it is
|
|
|
|
|
/// shared between `Constant` and `Lvalue`. See the aliases
|
|
|
|
|
/// `LvalueProjection` etc below.
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-07 14:37:42 +02:00
|
|
|
|
pub struct Projection<'tcx, B, V> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub base: B,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
pub elem: ProjectionElem<'tcx, V>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-07 14:37:42 +02:00
|
|
|
|
pub enum ProjectionElem<'tcx, V> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Deref,
|
2016-02-11 18:31:42 +02:00
|
|
|
|
Field(Field, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Index(V),
|
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// These indices are generated by slice patterns. Easiest to explain
|
|
|
|
|
/// by example:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
|
|
|
|
|
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
|
|
|
|
|
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
|
|
|
|
|
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
|
|
|
|
|
/// ```
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ConstantIndex {
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// index or -index (in Python terms), depending on from_end
|
|
|
|
|
offset: u32,
|
|
|
|
|
/// thing being indexed must be at least this long
|
|
|
|
|
min_length: u32,
|
|
|
|
|
/// counting backwards from end?
|
|
|
|
|
from_end: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2016-03-11 12:54:59 +02:00
|
|
|
|
/// These indices are generated by slice patterns.
|
|
|
|
|
///
|
|
|
|
|
/// slice[from:-to] in Python terms.
|
|
|
|
|
Subslice {
|
|
|
|
|
from: u32,
|
|
|
|
|
to: u32,
|
|
|
|
|
},
|
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// "Downcast" to a variant of an ADT. Currently, we only introduce
|
|
|
|
|
/// this for ADTs with more than one variant. It may be better to
|
|
|
|
|
/// just introduce it always, or always for enums.
|
2016-11-25 01:33:29 +02:00
|
|
|
|
Downcast(&'tcx AdtDef, usize),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Alias for projections as they appear in lvalues, where the base is an lvalue
|
|
|
|
|
/// and the index is an operand.
|
2016-01-05 17:26:22 -06:00
|
|
|
|
pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Operand<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
/// Alias for projections as they appear in lvalues, where the base is an lvalue
|
|
|
|
|
/// and the index is an operand.
|
2016-01-05 17:26:22 -06:00
|
|
|
|
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Operand<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
newtype_index!(Field, "field");
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Lvalue<'tcx> {
|
2016-02-11 18:31:42 +02:00
|
|
|
|
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
|
|
|
|
|
self.elem(ProjectionElem::Field(f, ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub fn deref(self) -> Lvalue<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
self.elem(ProjectionElem::Deref)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 12:56:20 +08:00
|
|
|
|
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Lvalue<'tcx> {
|
2016-12-01 01:12:03 +08:00
|
|
|
|
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub fn index(self, index: Operand<'tcx>) -> Lvalue<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
self.elem(ProjectionElem::Index(index))
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub fn elem(self, elem: LvalueElem<'tcx>) -> Lvalue<'tcx> {
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Lvalue::Projection(Box::new(LvalueProjection {
|
|
|
|
|
base: self,
|
|
|
|
|
elem: elem,
|
|
|
|
|
}))
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Lvalue<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::Lvalue::*;
|
|
|
|
|
|
|
|
|
|
match *self {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Local(id) => write!(fmt, "{:?}", id),
|
2016-01-01 21:01:17 -06:00
|
|
|
|
Static(def_id) =>
|
|
|
|
|
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Projection(ref data) =>
|
|
|
|
|
match data.elem {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
ProjectionElem::Downcast(ref adt_def, index) =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::Deref =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "(*{:?})", data.base),
|
2016-02-11 18:31:42 +02:00
|
|
|
|
ProjectionElem::Field(field, ty) =>
|
|
|
|
|
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::Index(ref index) =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[{:?}]", data.base, index),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
|
2016-03-11 12:54:59 +02:00
|
|
|
|
ProjectionElem::Subslice { from, to } if to == 0 =>
|
2016-11-10 17:54:31 +00:00
|
|
|
|
write!(fmt, "{:?}[{:?}:]", data.base, from),
|
2016-03-11 12:54:59 +02:00
|
|
|
|
ProjectionElem::Subslice { from, to } if from == 0 =>
|
|
|
|
|
write!(fmt, "{:?}[:-{:?}]", data.base, to),
|
|
|
|
|
ProjectionElem::Subslice { from, to } =>
|
|
|
|
|
write!(fmt, "{:?}[{:?}:-{:?}]", data.base,
|
|
|
|
|
from, to),
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 11:04:26 -05:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Scopes
|
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
newtype_index!(VisibilityScope, "scope");
|
|
|
|
|
pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope(0);
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2016-05-31 20:27:36 +03:00
|
|
|
|
pub struct VisibilityScopeData {
|
2016-04-06 17:17:12 +03:00
|
|
|
|
pub span: Span,
|
2016-05-31 20:27:36 +03:00
|
|
|
|
pub parent_scope: Option<VisibilityScope>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Operands
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// These are values that can appear inside an rvalue (or an index
|
|
|
|
|
/// lvalue). They are intentionally limited to prevent rvalues from
|
|
|
|
|
/// being nested in one another.
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Operand<'tcx> {
|
|
|
|
|
Consume(Lvalue<'tcx>),
|
|
|
|
|
Constant(Constant<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Operand<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::Operand::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Constant(ref a) => write!(fmt, "{:?}", a),
|
|
|
|
|
Consume(ref lv) => write!(fmt, "{:?}", lv),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Rvalues
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Rvalue<'tcx> {
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// x (either a move or copy, depending on type of x)
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Use(Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// [x; 32]
|
2016-01-20 23:14:00 +01:00
|
|
|
|
Repeat(Operand<'tcx>, TypedConstVal<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// &x or &mut x
|
2016-08-25 23:58:52 +03:00
|
|
|
|
Ref(&'tcx Region, BorrowKind, Lvalue<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// length of a [X] or [X;n] value
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Len(Lvalue<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
|
2016-03-31 18:50:07 +13:00
|
|
|
|
CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
UnaryOp(UnOp, Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-01-31 01:10:54 +02:00
|
|
|
|
/// Read the discriminant of an ADT.
|
|
|
|
|
///
|
|
|
|
|
/// Undefined (i.e. no effort is made to make it defined, but there’s no reason why it cannot
|
|
|
|
|
/// be defined to return, say, a 0) if ADT is not an enum.
|
|
|
|
|
Discriminant(Lvalue<'tcx>),
|
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Creates an *uninitialized* Box
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Box(Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Create an aggregate value, like a tuple or struct. This is
|
|
|
|
|
/// only needed because we want to distinguish `dest = Foo { x:
|
|
|
|
|
/// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
|
|
|
|
|
/// that `Foo` has a destructor. These rvalues can be optimized
|
|
|
|
|
/// away after type-checking and before lowering.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Aggregate(AggregateKind<'tcx>, Vec<Operand<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 14:46:39 +01:00
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum CastKind {
|
|
|
|
|
Misc,
|
|
|
|
|
|
|
|
|
|
/// Convert unique, zero-sized type for a fn to fn()
|
|
|
|
|
ReifyFnPointer,
|
|
|
|
|
|
2017-02-22 01:24:16 +01:00
|
|
|
|
/// Convert non capturing closure to fn()
|
|
|
|
|
ClosureFnPointer,
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// Convert safe fn() to unsafe fn()
|
|
|
|
|
UnsafeFnPointer,
|
|
|
|
|
|
|
|
|
|
/// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
|
|
|
|
|
/// trans must figure out the details once full monomorphization
|
|
|
|
|
/// is known. For example, this could be used to cast from a
|
|
|
|
|
/// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<Trait>`
|
|
|
|
|
/// (presuming `T: Trait`).
|
|
|
|
|
Unsize,
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum AggregateKind<'tcx> {
|
2016-09-20 02:14:46 +02:00
|
|
|
|
Array,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Tuple,
|
2016-08-19 19:20:30 +03:00
|
|
|
|
/// The second field is variant number (discriminant), it's equal to 0
|
|
|
|
|
/// for struct and union expressions. The fourth field is active field
|
|
|
|
|
/// number and is present only for union expressions.
|
2016-11-25 01:33:29 +02:00
|
|
|
|
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
|
2016-04-29 08:30:54 +03:00
|
|
|
|
Closure(DefId, ClosureSubsts<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum BinOp {
|
|
|
|
|
/// The `+` operator (addition)
|
|
|
|
|
Add,
|
|
|
|
|
/// The `-` operator (subtraction)
|
|
|
|
|
Sub,
|
|
|
|
|
/// The `*` operator (multiplication)
|
|
|
|
|
Mul,
|
|
|
|
|
/// The `/` operator (division)
|
|
|
|
|
Div,
|
|
|
|
|
/// The `%` operator (modulus)
|
|
|
|
|
Rem,
|
|
|
|
|
/// The `^` operator (bitwise xor)
|
|
|
|
|
BitXor,
|
|
|
|
|
/// The `&` operator (bitwise and)
|
|
|
|
|
BitAnd,
|
|
|
|
|
/// The `|` operator (bitwise or)
|
|
|
|
|
BitOr,
|
|
|
|
|
/// The `<<` operator (shift left)
|
|
|
|
|
Shl,
|
|
|
|
|
/// The `>>` operator (shift right)
|
|
|
|
|
Shr,
|
|
|
|
|
/// The `==` operator (equality)
|
|
|
|
|
Eq,
|
|
|
|
|
/// The `<` operator (less than)
|
|
|
|
|
Lt,
|
|
|
|
|
/// The `<=` operator (less than or equal to)
|
|
|
|
|
Le,
|
|
|
|
|
/// The `!=` operator (not equal to)
|
|
|
|
|
Ne,
|
|
|
|
|
/// The `>=` operator (greater than or equal to)
|
|
|
|
|
Ge,
|
|
|
|
|
/// The `>` operator (greater than)
|
|
|
|
|
Gt,
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
|
impl BinOp {
|
|
|
|
|
pub fn is_checkable(self) -> bool {
|
|
|
|
|
use self::BinOp::*;
|
|
|
|
|
match self {
|
|
|
|
|
Add | Sub | Mul | Shl | Shr => true,
|
|
|
|
|
_ => false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum UnOp {
|
|
|
|
|
/// The `!` operator for logical inversion
|
|
|
|
|
Not,
|
|
|
|
|
/// The `-` operator for negation
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Neg,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Rvalue<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::Rvalue::*;
|
|
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
|
Use(ref lvalue) => write!(fmt, "{:?}", lvalue),
|
|
|
|
|
Repeat(ref a, ref b) => write!(fmt, "[{:?}; {:?}]", a, b),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Len(ref a) => write!(fmt, "Len({:?})", a),
|
|
|
|
|
Cast(ref kind, ref lv, ref ty) => write!(fmt, "{:?} as {:?} ({:?})", lv, ty, kind),
|
|
|
|
|
BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
|
2016-03-31 18:50:07 +13:00
|
|
|
|
CheckedBinaryOp(ref op, ref a, ref b) => {
|
|
|
|
|
write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
|
2017-01-31 01:10:54 +02:00
|
|
|
|
Discriminant(ref lval) => write!(fmt, "discriminant({:?})", lval),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Box(ref t) => write!(fmt, "Box({:?})", t),
|
2016-01-01 00:39:02 -06:00
|
|
|
|
Ref(_, borrow_kind, ref lv) => {
|
|
|
|
|
let kind_str = match borrow_kind {
|
|
|
|
|
BorrowKind::Shared => "",
|
|
|
|
|
BorrowKind::Mut | BorrowKind::Unique => "mut ",
|
|
|
|
|
};
|
|
|
|
|
write!(fmt, "&{}{:?}", kind_str, lv)
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 21:38:44 -06:00
|
|
|
|
Aggregate(ref kind, ref lvs) => {
|
2016-02-23 22:04:27 +02:00
|
|
|
|
fn fmt_tuple(fmt: &mut Formatter, lvs: &[Operand]) -> fmt::Result {
|
|
|
|
|
let mut tuple_fmt = fmt.debug_tuple("");
|
2015-12-31 21:38:44 -06:00
|
|
|
|
for lv in lvs {
|
|
|
|
|
tuple_fmt.field(lv);
|
|
|
|
|
}
|
|
|
|
|
tuple_fmt.finish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match *kind {
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Array => write!(fmt, "{:?}", lvs),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Tuple => {
|
2016-01-06 14:17:38 -06:00
|
|
|
|
match lvs.len() {
|
|
|
|
|
0 => write!(fmt, "()"),
|
|
|
|
|
1 => write!(fmt, "({:?},)", lvs[0]),
|
2016-02-23 22:04:27 +02:00
|
|
|
|
_ => fmt_tuple(fmt, lvs),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Adt(adt_def, variant, substs, _) => {
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let variant_def = &adt_def.variants[variant];
|
2016-02-23 22:04:27 +02:00
|
|
|
|
|
2016-10-28 16:26:47 -06:00
|
|
|
|
ppaux::parameterized(fmt, substs, variant_def.did, &[])?;
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
match variant_def.ctor_kind {
|
|
|
|
|
CtorKind::Const => Ok(()),
|
|
|
|
|
CtorKind::Fn => fmt_tuple(fmt, lvs),
|
|
|
|
|
CtorKind::Fictive => {
|
2016-02-23 22:04:27 +02:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct("");
|
2015-12-31 21:38:44 -06:00
|
|
|
|
for (field, lv) in variant_def.fields.iter().zip(lvs) {
|
|
|
|
|
struct_fmt.field(&field.name.as_str(), lv);
|
|
|
|
|
}
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Closure(def_id, _) => ty::tls::with(|tcx| {
|
2017-01-26 02:41:06 +02:00
|
|
|
|
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
|
|
|
|
|
let name = format!("[closure@{:?}]", tcx.hir.span(node_id));
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
|
|
|
|
|
tcx.with_freevars(node_id, |freevars| {
|
|
|
|
|
for (freevar, lv) in freevars.iter().zip(lvs) {
|
2016-08-31 14:08:22 +03:00
|
|
|
|
let def_id = freevar.def.def_id();
|
2017-01-26 02:41:06 +02:00
|
|
|
|
let var_id = tcx.hir.as_local_node_id(def_id).unwrap();
|
2016-08-31 14:08:22 +03:00
|
|
|
|
let var_name = tcx.local_var_name_str(var_id);
|
2015-12-31 21:38:44 -06:00
|
|
|
|
struct_fmt.field(&var_name, lv);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
|
} else {
|
|
|
|
|
write!(fmt, "[closure]")
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Constants
|
|
|
|
|
///
|
|
|
|
|
/// Two constants are equal if they are the same constant. Note that
|
|
|
|
|
/// this does not necessarily mean that they are "==" in Rust -- in
|
|
|
|
|
/// particular one must be wary of `NaN`!
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Constant<'tcx> {
|
|
|
|
|
pub span: Span,
|
|
|
|
|
pub ty: Ty<'tcx>,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
pub literal: Literal<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 22:49:26 +01:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2016-01-20 23:14:00 +01:00
|
|
|
|
pub struct TypedConstVal<'tcx> {
|
|
|
|
|
pub ty: Ty<'tcx>,
|
|
|
|
|
pub span: Span,
|
2015-12-16 18:44:15 +01:00
|
|
|
|
pub value: ConstUsize,
|
2016-01-20 23:14:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 22:49:26 +01:00
|
|
|
|
impl<'tcx> Debug for TypedConstVal<'tcx> {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-12-16 18:44:15 +01:00
|
|
|
|
write!(fmt, "const {}", ConstInt::Usize(self.value))
|
2016-01-21 22:49:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
newtype_index!(Promoted, "promoted");
|
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Literal<'tcx> {
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Item {
|
|
|
|
|
def_id: DefId,
|
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
|
},
|
|
|
|
|
Value {
|
2017-02-15 15:00:20 +02:00
|
|
|
|
value: ConstVal<'tcx>,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
},
|
2016-05-03 00:26:41 +03:00
|
|
|
|
Promoted {
|
|
|
|
|
// Index into the `promoted` vector of `Mir`.
|
2016-06-07 17:28:36 +03:00
|
|
|
|
index: Promoted
|
2016-05-03 00:26:41 +03:00
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
|
|
|
|
|
impl<'tcx> Debug for Constant<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
write!(fmt, "{:?}", self.literal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> Debug for Literal<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
use self::Literal::*;
|
|
|
|
|
match *self {
|
2016-02-23 22:04:27 +02:00
|
|
|
|
Item { def_id, substs } => {
|
2016-10-28 16:26:47 -06:00
|
|
|
|
ppaux::parameterized(fmt, substs, def_id, &[])
|
2016-02-23 22:04:27 +02:00
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
Value { ref value } => {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "const ")?;
|
2016-01-05 23:06:33 -06:00
|
|
|
|
fmt_const_val(fmt, value)
|
|
|
|
|
}
|
2016-05-03 00:26:41 +03:00
|
|
|
|
Promoted { index } => {
|
2016-06-07 17:28:36 +03:00
|
|
|
|
write!(fmt, "{:?}", index)
|
2016-05-03 00:26:41 +03:00
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
|
2016-01-05 23:06:33 -06:00
|
|
|
|
fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
|
2016-03-30 13:43:36 +02:00
|
|
|
|
use middle::const_val::ConstVal::*;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
match *const_val {
|
|
|
|
|
Float(f) => write!(fmt, "{:?}", f),
|
2015-12-16 18:44:15 +01:00
|
|
|
|
Integral(n) => write!(fmt, "{}", n),
|
2016-01-05 23:06:33 -06:00
|
|
|
|
Str(ref s) => write!(fmt, "{:?}", s),
|
|
|
|
|
ByteStr(ref bytes) => {
|
|
|
|
|
let escaped: String = bytes
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|&ch| ascii::escape_default(ch).map(|c| c as char))
|
|
|
|
|
.collect();
|
|
|
|
|
write!(fmt, "b\"{}\"", escaped)
|
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Bool(b) => write!(fmt, "{:?}", b),
|
2017-02-15 15:00:20 +02:00
|
|
|
|
Function(def_id, _) => write!(fmt, "{}", item_path_str(def_id)),
|
2017-01-06 21:54:24 +02:00
|
|
|
|
Struct(_) | Tuple(_) | Array(_) | Repeat(..) =>
|
|
|
|
|
bug!("ConstVal `{:?}` should not be in MIR", const_val),
|
2015-12-16 18:44:15 +01:00
|
|
|
|
Char(c) => write!(fmt, "{:?}", c),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
|
|
|
|
|
fn item_path_str(def_id: DefId) -> String {
|
|
|
|
|
ty::tls::with(|tcx| tcx.item_path_str(def_id))
|
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
|
impl<'tcx> ControlFlowGraph for Mir<'tcx> {
|
|
|
|
|
|
|
|
|
|
type Node = BasicBlock;
|
|
|
|
|
|
|
|
|
|
fn num_nodes(&self) -> usize { self.basic_blocks.len() }
|
|
|
|
|
|
|
|
|
|
fn start_node(&self) -> Self::Node { START_BLOCK }
|
|
|
|
|
|
|
|
|
|
fn predecessors<'graph>(&'graph self, node: Self::Node)
|
|
|
|
|
-> <Self as GraphPredecessors<'graph>>::Iter
|
|
|
|
|
{
|
|
|
|
|
self.predecessors_for(node).clone().into_iter()
|
|
|
|
|
}
|
|
|
|
|
fn successors<'graph>(&'graph self, node: Self::Node)
|
|
|
|
|
-> <Self as GraphSuccessors<'graph>>::Iter
|
|
|
|
|
{
|
|
|
|
|
self.basic_blocks[node].terminator().successors().into_owned().into_iter()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
|
|
|
|
|
type Item = BasicBlock;
|
|
|
|
|
type Iter = IntoIter<BasicBlock>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
|
|
|
|
|
type Item = BasicBlock;
|
|
|
|
|
type Iter = IntoIter<BasicBlock>;
|
|
|
|
|
}
|
2016-08-08 18:46:06 -07:00
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
2016-08-08 18:46:06 -07:00
|
|
|
|
pub struct Location {
|
|
|
|
|
/// the location is within this block
|
|
|
|
|
pub block: BasicBlock,
|
|
|
|
|
|
|
|
|
|
/// the location is the start of the this statement; or, if `statement_index`
|
|
|
|
|
/// == num-statements, then the start of the terminator.
|
|
|
|
|
pub statement_index: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
|
impl fmt::Debug for Location {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(fmt, "{:?}[{}]", self.block, self.statement_index)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 18:18:40 -07:00
|
|
|
|
|
|
|
|
|
impl Location {
|
|
|
|
|
pub fn dominates(&self, other: &Location, dominators: &Dominators<BasicBlock>) -> bool {
|
|
|
|
|
if self.block == other.block {
|
|
|
|
|
self.statement_index <= other.statement_index
|
|
|
|
|
} else {
|
|
|
|
|
dominators.is_dominated_by(other.block, self.block)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|