2015-08-18 17:59:21 -04:00
|
|
|
// Copyright 2012-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.
|
|
|
|
|
2017-05-02 14:56:26 -04:00
|
|
|
|
|
|
|
use build;
|
2015-11-09 20:54:17 -05:00
|
|
|
use hair::cx::Cx;
|
2016-09-24 17:45:24 +03:00
|
|
|
use hair::Pattern;
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::def_id::DefId;
|
2017-05-07 16:22:33 +03:00
|
|
|
use rustc::middle::region::CodeExtent;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc::mir::transform::MirSource;
|
2017-07-27 19:43:05 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, Lookup};
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
|
|
|
use rustc::ty::subst::Substs;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc::util::nodemap::NodeMap;
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
|
|
|
use shim;
|
|
|
|
use std::mem;
|
|
|
|
use std::u32;
|
2016-04-15 17:11:24 +03:00
|
|
|
use syntax::abi::Abi;
|
2015-10-05 12:31:48 -04:00
|
|
|
use syntax::ast;
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::keywords;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2017-05-02 14:56:26 -04:00
|
|
|
use util as mir_util;
|
2015-10-05 12:31:48 -04:00
|
|
|
|
2017-05-01 14:39:48 -04:00
|
|
|
/// Construct the MIR for a given def-id.
|
|
|
|
pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'tcx> {
|
2017-05-02 14:56:26 -04:00
|
|
|
let id = tcx.hir.as_local_node_id(def_id).unwrap();
|
|
|
|
let unsupported = || {
|
|
|
|
span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id);
|
|
|
|
};
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2017-05-02 14:56:26 -04:00
|
|
|
// Figure out what primary body this item has.
|
|
|
|
let body_id = match tcx.hir.get(id) {
|
|
|
|
hir::map::NodeItem(item) => {
|
|
|
|
match item.node {
|
|
|
|
hir::ItemConst(_, body) |
|
|
|
|
hir::ItemStatic(_, _, body) |
|
|
|
|
hir::ItemFn(.., body) => body,
|
|
|
|
_ => unsupported()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::map::NodeTraitItem(item) => {
|
|
|
|
match item.node {
|
|
|
|
hir::TraitItemKind::Const(_, Some(body)) |
|
|
|
|
hir::TraitItemKind::Method(_,
|
|
|
|
hir::TraitMethod::Provided(body)) => body,
|
|
|
|
_ => unsupported()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::map::NodeImplItem(item) => {
|
|
|
|
match item.node {
|
|
|
|
hir::ImplItemKind::Const(_, body) |
|
|
|
|
hir::ImplItemKind::Method(_, body) => body,
|
|
|
|
_ => unsupported()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::map::NodeExpr(expr) => {
|
|
|
|
// FIXME(eddyb) Closures should have separate
|
|
|
|
// function definition IDs and expression IDs.
|
|
|
|
// Type-checking should not let closures get
|
|
|
|
// this far in a constant position.
|
|
|
|
// Assume that everything other than closures
|
|
|
|
// is a constant "initializer" expression.
|
|
|
|
match expr.node {
|
2016-12-26 14:34:03 +01:00
|
|
|
hir::ExprClosure(_, _, body, _, _) => body,
|
2017-05-02 14:56:26 -04:00
|
|
|
_ => hir::BodyId { node_id: expr.id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::map::NodeVariant(variant) =>
|
|
|
|
return create_constructor_shim(tcx, id, &variant.node.data),
|
|
|
|
hir::map::NodeStructCtor(ctor) =>
|
|
|
|
return create_constructor_shim(tcx, id, ctor),
|
|
|
|
_ => unsupported()
|
|
|
|
};
|
|
|
|
|
|
|
|
let src = MirSource::from_node(tcx, id);
|
2017-06-09 10:55:16 +03:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-05-02 14:56:26 -04:00
|
|
|
let cx = Cx::new(&infcx, src);
|
|
|
|
let mut mir = if cx.tables().tainted_by_errors {
|
|
|
|
build::construct_error(cx, body_id)
|
|
|
|
} else if let MirSource::Fn(id) = src {
|
|
|
|
// fetch the fully liberated fn signature (that is, all bound
|
|
|
|
// types/lifetimes replaced)
|
2017-08-07 17:45:06 +02:00
|
|
|
let fn_hir_id = tcx.hir.node_to_hir_id(id);
|
2017-08-10 16:10:04 +02:00
|
|
|
let fn_sig = cx.tables().liberated_fn_sigs()[fn_hir_id].clone();
|
2017-05-02 14:56:26 -04:00
|
|
|
|
|
|
|
let ty = tcx.type_of(tcx.hir.local_def_id(id));
|
|
|
|
let mut abi = fn_sig.abi;
|
2016-12-26 14:34:03 +01:00
|
|
|
let implicit_argument = match ty.sty {
|
|
|
|
ty::TyClosure(..) => {
|
|
|
|
// HACK(eddyb) Avoid having RustCall on closures,
|
|
|
|
// as it adds unnecessary (and wrong) auto-tupling.
|
|
|
|
abi = Abi::Rust;
|
|
|
|
Some((closure_self_ty(tcx, id, body_id), None))
|
|
|
|
}
|
|
|
|
ty::TyGenerator(..) => {
|
2017-08-14 20:12:13 -07:00
|
|
|
let gen_ty = tcx.body_tables(body_id).node_id_to_type(fn_hir_id);
|
2016-12-26 14:34:03 +01:00
|
|
|
Some((gen_ty, None))
|
|
|
|
}
|
|
|
|
_ => None,
|
2017-05-02 14:56:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let body = tcx.hir.body(body_id);
|
|
|
|
let explicit_arguments =
|
|
|
|
body.arguments
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, arg)| {
|
|
|
|
(fn_sig.inputs()[index], Some(&*arg.pat))
|
|
|
|
});
|
|
|
|
|
|
|
|
let arguments = implicit_argument.into_iter().chain(explicit_arguments);
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2017-07-11 12:57:05 -07:00
|
|
|
let (yield_ty, return_ty) = if body.is_generator {
|
2017-08-14 20:12:13 -07:00
|
|
|
let gen_sig = cx.tables().generator_sigs()[fn_hir_id].clone().unwrap();
|
2017-07-11 12:57:05 -07:00
|
|
|
(Some(gen_sig.yield_ty), gen_sig.return_ty)
|
2016-12-26 14:34:03 +01:00
|
|
|
} else {
|
2017-07-11 12:57:05 -07:00
|
|
|
(None, fn_sig.output())
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2017-07-11 12:57:05 -07:00
|
|
|
build::construct_fn(cx, id, arguments, abi, return_ty, yield_ty, body)
|
2017-05-02 14:56:26 -04:00
|
|
|
} else {
|
|
|
|
build::construct_const(cx, body_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert the Mir to global types.
|
|
|
|
let mut globalizer = GlobalizeMir {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-05-02 14:56:26 -04:00
|
|
|
span: mir.span
|
|
|
|
};
|
|
|
|
globalizer.visit_mir(&mut mir);
|
|
|
|
let mir = unsafe {
|
|
|
|
mem::transmute::<Mir, Mir<'tcx>>(mir)
|
|
|
|
};
|
|
|
|
|
|
|
|
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
|
|
|
|
|
2017-05-01 14:39:48 -04:00
|
|
|
mir
|
2017-05-02 14:56:26 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A pass to lift all the types and substitutions in a Mir
|
|
|
|
/// to the global tcx. Sadly, we don't have a "folder" that
|
|
|
|
/// can change 'tcx so we have to transmute afterwards.
|
|
|
|
struct GlobalizeMir<'a, 'gcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'gcx>,
|
|
|
|
span: Span
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx: 'tcx, 'tcx> MutVisitor<'tcx> for GlobalizeMir<'a, 'gcx> {
|
2017-07-27 19:43:05 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) {
|
2017-05-02 14:56:26 -04:00
|
|
|
if let Some(lifted) = self.tcx.lift(ty) {
|
|
|
|
*ty = lifted;
|
|
|
|
} else {
|
|
|
|
span_bug!(self.span,
|
|
|
|
"found type `{:?}` with inference types/regions in MIR",
|
|
|
|
ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 22:31:38 -04:00
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) {
|
2017-05-02 14:56:26 -04:00
|
|
|
if let Some(lifted) = self.tcx.lift(substs) {
|
|
|
|
*substs = lifted;
|
|
|
|
} else {
|
|
|
|
span_bug!(self.span,
|
|
|
|
"found substs `{:?}` with inference types/regions in MIR",
|
|
|
|
substs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ctor_id: ast::NodeId,
|
|
|
|
v: &'tcx hir::VariantData)
|
2017-05-01 14:39:48 -04:00
|
|
|
-> Mir<'tcx>
|
2017-05-02 14:56:26 -04:00
|
|
|
{
|
|
|
|
let span = tcx.hir.span(ctor_id);
|
|
|
|
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
|
2017-06-09 10:55:16 +03:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-05-02 14:56:26 -04:00
|
|
|
let (mut mir, src) =
|
|
|
|
shim::build_adt_ctor(&infcx, ctor_id, fields, span);
|
|
|
|
|
|
|
|
// Convert the Mir to global types.
|
|
|
|
let tcx = infcx.tcx.global_tcx();
|
|
|
|
let mut globalizer = GlobalizeMir {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-05-02 14:56:26 -04:00
|
|
|
span: mir.span
|
|
|
|
};
|
|
|
|
globalizer.visit_mir(&mut mir);
|
|
|
|
let mir = unsafe {
|
|
|
|
mem::transmute::<Mir, Mir<'tcx>>(mir)
|
|
|
|
};
|
|
|
|
|
|
|
|
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
|
|
|
|
|
2017-05-01 14:39:48 -04:00
|
|
|
mir
|
2017-05-02 14:56:26 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "attempting to create MIR for non-tuple variant {:?}", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// BuildMir -- walks a crate, looking for fn items and methods to build MIR from
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
pub fn closure_self_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-02 14:56:26 -04:00
|
|
|
closure_expr_id: ast::NodeId,
|
|
|
|
body_id: hir::BodyId)
|
|
|
|
-> Ty<'tcx> {
|
2017-08-07 14:43:43 +02:00
|
|
|
let closure_expr_hir_id = tcx.hir.node_to_hir_id(closure_expr_id);
|
|
|
|
let closure_ty = tcx.body_tables(body_id).node_id_to_type(closure_expr_hir_id);
|
2017-05-02 14:56:26 -04:00
|
|
|
|
2017-05-07 19:57:51 +03:00
|
|
|
let closure_def_id = tcx.hir.local_def_id(closure_expr_id);
|
2017-05-02 14:56:26 -04:00
|
|
|
let region = ty::ReFree(ty::FreeRegion {
|
2017-05-07 19:57:51 +03:00
|
|
|
scope: closure_def_id,
|
2017-05-02 14:56:26 -04:00
|
|
|
bound_region: ty::BoundRegion::BrEnv,
|
|
|
|
});
|
|
|
|
let region = tcx.mk_region(region);
|
|
|
|
|
2017-05-07 19:57:51 +03:00
|
|
|
match tcx.closure_kind(closure_def_id) {
|
2017-05-02 14:56:26 -04:00
|
|
|
ty::ClosureKind::Fn =>
|
|
|
|
tcx.mk_ref(region,
|
|
|
|
ty::TypeAndMut { ty: closure_ty,
|
|
|
|
mutbl: hir::MutImmutable }),
|
|
|
|
ty::ClosureKind::FnMut =>
|
|
|
|
tcx.mk_ref(region,
|
|
|
|
ty::TypeAndMut { ty: closure_ty,
|
|
|
|
mutbl: hir::MutMutable }),
|
|
|
|
ty::ClosureKind::FnOnce =>
|
|
|
|
closure_ty
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2017-05-01 11:58:05 -04:00
|
|
|
struct Builder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
2016-05-11 04:14:41 +03:00
|
|
|
hir: Cx<'a, 'gcx, 'tcx>,
|
2015-10-05 12:31:48 -04:00
|
|
|
cfg: CFG<'tcx>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2016-03-22 20:41:07 -04:00
|
|
|
fn_span: Span,
|
2016-09-25 01:38:27 +02:00
|
|
|
arg_count: usize,
|
2016-03-22 20:41:07 -04:00
|
|
|
|
2016-04-20 00:13:30 +03:00
|
|
|
/// the current set of scopes, updated as we traverse;
|
|
|
|
/// see the `scope` module for more details
|
2015-10-05 12:31:48 -04:00
|
|
|
scopes: Vec<scope::Scope<'tcx>>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2017-02-28 11:05:03 -08:00
|
|
|
/// the current set of breakables; see the `scope` module for more
|
2016-04-20 00:13:30 +03:00
|
|
|
/// details
|
2017-02-28 11:05:03 -08:00
|
|
|
breakable_scopes: Vec<scope::BreakableScope<'tcx>>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2016-04-20 00:13:30 +03:00
|
|
|
/// the vector of all scopes that we have created thus far;
|
|
|
|
/// we track this for debuginfo later
|
2016-06-07 17:28:36 +03:00
|
|
|
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
|
2016-05-31 20:27:36 +03:00
|
|
|
visibility_scope: VisibilityScope,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
/// Maps node ids of variable bindings to the `Local`s created for them.
|
|
|
|
var_indices: NodeMap<Local>,
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
2016-01-16 00:36:32 +02:00
|
|
|
unit_temp: Option<Lvalue<'tcx>>,
|
2016-03-23 20:46:38 -04:00
|
|
|
|
2016-04-20 00:13:30 +03:00
|
|
|
/// cached block with the RESUME terminator; this is created
|
|
|
|
/// when first set of cleanups are built.
|
2016-03-23 20:46:38 -04:00
|
|
|
cached_resume_block: Option<BasicBlock>,
|
2016-04-20 00:13:30 +03:00
|
|
|
/// cached block with the RETURN terminator
|
|
|
|
cached_return_block: Option<BasicBlock>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
struct CFG<'tcx> {
|
2016-06-07 17:28:36 +03:00
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct ScopeId(u32);
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
impl Idx for ScopeId {
|
|
|
|
fn new(index: usize) -> ScopeId {
|
2016-05-31 20:27:36 +03:00
|
|
|
assert!(index < (u32::MAX as usize));
|
|
|
|
ScopeId(index as u32)
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
fn index(self) -> usize {
|
2016-05-31 20:27:36 +03:00
|
|
|
self.0 as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
/// The `BlockAnd` "monad" packages up the new basic block along with a
|
|
|
|
/// produced value (sometimes just unit, of course). The `unpack!`
|
|
|
|
/// macro (and methods below) makes working with `BlockAnd` much more
|
|
|
|
/// convenient.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
#[must_use] // if you don't use one of these results, you're leaving a dangling edge
|
2017-05-01 11:58:05 -04:00
|
|
|
struct BlockAnd<T>(BasicBlock, T);
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-11-19 16:37:34 +01:00
|
|
|
trait BlockAndExtension {
|
|
|
|
fn and<T>(self, v: T) -> BlockAnd<T>;
|
|
|
|
fn unit(self) -> BlockAnd<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockAndExtension for BasicBlock {
|
2015-08-18 17:59:21 -04:00
|
|
|
fn and<T>(self, v: T) -> BlockAnd<T> {
|
|
|
|
BlockAnd(self, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unit(self) -> BlockAnd<()> {
|
|
|
|
BlockAnd(self, ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update a block pointer and return the value.
|
|
|
|
/// Use it like `let x = unpack!(block = self.foo(block, foo))`.
|
|
|
|
macro_rules! unpack {
|
|
|
|
($x:ident = $c:expr) => {
|
|
|
|
{
|
|
|
|
let BlockAnd(b, v) = $c;
|
|
|
|
$x = b;
|
|
|
|
v
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($c:expr) => {
|
|
|
|
{
|
|
|
|
let BlockAnd(b, ()) = $c;
|
|
|
|
b
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
/// the main entry point for building MIR for a function
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2017-05-02 14:56:26 -04:00
|
|
|
fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
|
|
|
|
fn_id: ast::NodeId,
|
|
|
|
arguments: A,
|
|
|
|
abi: Abi,
|
|
|
|
return_ty: Ty<'gcx>,
|
2017-07-10 21:11:31 +02:00
|
|
|
yield_ty: Option<Ty<'gcx>>,
|
2017-05-02 14:56:26 -04:00
|
|
|
body: &'gcx hir::Body)
|
|
|
|
-> Mir<'tcx>
|
2016-05-11 04:14:41 +03:00
|
|
|
where A: Iterator<Item=(Ty<'gcx>, Option<&'gcx hir::Pat>)>
|
2016-04-15 17:11:24 +03:00
|
|
|
{
|
2016-09-25 01:38:27 +02:00
|
|
|
let arguments: Vec<_> = arguments.collect();
|
|
|
|
|
2016-03-23 12:26:37 -04:00
|
|
|
let tcx = hir.tcx();
|
2017-01-26 02:41:06 +02:00
|
|
|
let span = tcx.hir.span(fn_id);
|
2016-12-26 14:34:03 +01:00
|
|
|
let mut builder = Builder::new(hir.clone(),
|
|
|
|
span,
|
2017-07-11 12:57:05 -07:00
|
|
|
arguments.len(),
|
2016-12-26 14:34:03 +01:00
|
|
|
return_ty);
|
2016-03-18 08:52:13 -04:00
|
|
|
|
2017-08-29 19:24:49 +03:00
|
|
|
let call_site_extent = CodeExtent::CallSiteScope(body.value.hir_id.local_id);
|
|
|
|
let arg_extent = CodeExtent::ParameterScope(body.value.hir_id.local_id);
|
2016-04-15 17:11:24 +03:00
|
|
|
let mut block = START_BLOCK;
|
2017-05-26 18:21:03 +02:00
|
|
|
let source_info = builder.source_info(span);
|
|
|
|
unpack!(block = builder.in_scope((call_site_extent, source_info), block, |builder| {
|
|
|
|
unpack!(block = builder.in_scope((arg_extent, source_info), block, |builder| {
|
2017-07-11 12:57:05 -07:00
|
|
|
builder.args_and_body(block, &arguments, arg_extent, &body.value)
|
2016-03-23 12:26:37 -04:00
|
|
|
}));
|
2016-10-19 00:25:03 -07:00
|
|
|
// Attribute epilogue to function's closing brace
|
2017-07-31 23:04:34 +03:00
|
|
|
let fn_end = span.with_lo(span.hi());
|
2016-10-19 00:25:03 -07:00
|
|
|
let source_info = builder.source_info(fn_end);
|
2016-04-20 00:13:30 +03:00
|
|
|
let return_block = builder.return_block();
|
2016-06-07 19:21:56 +03:00
|
|
|
builder.cfg.terminate(block, source_info,
|
2016-04-20 00:13:30 +03:00
|
|
|
TerminatorKind::Goto { target: return_block });
|
2016-06-07 19:21:56 +03:00
|
|
|
builder.cfg.terminate(return_block, source_info,
|
2016-03-23 12:26:37 -04:00
|
|
|
TerminatorKind::Return);
|
2016-09-25 01:38:27 +02:00
|
|
|
return_block.unit()
|
2016-04-15 17:11:24 +03:00
|
|
|
}));
|
|
|
|
assert_eq!(block, builder.return_block());
|
|
|
|
|
2016-09-26 22:44:01 +02:00
|
|
|
let mut spread_arg = None;
|
2016-11-10 16:49:53 +02:00
|
|
|
if abi == Abi::RustCall {
|
|
|
|
// RustCall pseudo-ABI untuples the last argument.
|
|
|
|
spread_arg = Some(Local::new(arguments.len()));
|
2016-04-15 17:11:24 +03:00
|
|
|
}
|
2016-03-22 16:05:28 -04:00
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
// Gather the upvars of a closure, if any.
|
|
|
|
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
|
|
|
|
freevars.iter().map(|fv| {
|
2017-08-08 14:34:37 +02:00
|
|
|
let var_def_id = fv.def.def_id();
|
|
|
|
let var_node_id = tcx.hir.as_local_node_id(var_def_id).unwrap();
|
|
|
|
let closure_expr_id = tcx.hir.local_def_id(fn_id).index;
|
2017-06-08 13:38:30 +03:00
|
|
|
let capture = hir.tables().upvar_capture(ty::UpvarId {
|
2017-08-08 14:34:37 +02:00
|
|
|
var_id: var_def_id.index,
|
|
|
|
closure_expr_id,
|
2017-06-08 13:38:30 +03:00
|
|
|
});
|
|
|
|
let by_ref = match capture {
|
2016-04-16 21:51:26 +03:00
|
|
|
ty::UpvarCapture::ByValue => false,
|
|
|
|
ty::UpvarCapture::ByRef(..) => true
|
2017-06-08 13:38:30 +03:00
|
|
|
};
|
2016-04-16 21:51:26 +03:00
|
|
|
let mut decl = UpvarDecl {
|
2016-04-18 22:53:50 +03:00
|
|
|
debug_name: keywords::Invalid.name(),
|
2017-08-06 22:54:09 -07:00
|
|
|
by_ref,
|
2016-04-16 21:51:26 +03:00
|
|
|
};
|
2017-08-17 23:00:57 -07:00
|
|
|
if let Some(hir::map::NodeBinding(pat)) = tcx.hir.find(var_node_id) {
|
2016-11-25 13:21:19 +02:00
|
|
|
if let hir::PatKind::Binding(_, _, ref ident, _) = pat.node {
|
2016-03-06 15:54:44 +03:00
|
|
|
decl.debug_name = ident.node;
|
2016-04-16 21:51:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
decl
|
|
|
|
}).collect()
|
|
|
|
});
|
|
|
|
|
2017-07-10 21:11:31 +02:00
|
|
|
let mut mir = builder.finish(upvar_decls, return_ty, yield_ty);
|
2016-09-26 22:44:01 +02:00
|
|
|
mir.spread_arg = spread_arg;
|
2016-11-03 14:22:57 +11:00
|
|
|
mir
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 21:09:01 -04:00
|
|
|
fn construct_const<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
|
|
|
|
body_id: hir::BodyId)
|
|
|
|
-> Mir<'tcx> {
|
2016-05-02 23:11:19 +03:00
|
|
|
let tcx = hir.tcx();
|
2017-01-26 02:41:06 +02:00
|
|
|
let ast_expr = &tcx.hir.body(body_id).value;
|
2017-01-06 21:54:24 +02:00
|
|
|
let ty = hir.tables().expr_ty_adjusted(ast_expr);
|
2017-04-19 16:45:07 -04:00
|
|
|
let owner_id = tcx.hir.body_owner(body_id);
|
|
|
|
let span = tcx.hir.span(owner_id);
|
2017-07-11 12:57:05 -07:00
|
|
|
let mut builder = Builder::new(hir.clone(), span, 0, ty);
|
2016-05-02 23:11:19 +03:00
|
|
|
|
|
|
|
let mut block = START_BLOCK;
|
2017-05-07 17:30:55 +03:00
|
|
|
let expr = builder.hir.mirror(ast_expr);
|
|
|
|
unpack!(block = builder.into_expr(&Lvalue::Local(RETURN_POINTER), block, expr));
|
2016-05-02 23:11:19 +03:00
|
|
|
|
2017-05-07 17:30:55 +03:00
|
|
|
let source_info = builder.source_info(span);
|
|
|
|
builder.cfg.terminate(block, source_info, TerminatorKind::Return);
|
2016-05-02 23:11:19 +03:00
|
|
|
|
2017-05-07 17:30:55 +03:00
|
|
|
// Constants can't `return` so a return block should not be created.
|
|
|
|
assert_eq!(builder.cached_return_block, None);
|
2016-05-02 23:11:19 +03:00
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
builder.finish(vec![], ty, None)
|
2016-05-02 23:11:19 +03:00
|
|
|
}
|
|
|
|
|
2017-05-15 21:09:01 -04:00
|
|
|
fn construct_error<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
|
2017-02-15 15:00:20 +02:00
|
|
|
body_id: hir::BodyId)
|
|
|
|
-> Mir<'tcx> {
|
|
|
|
let span = hir.tcx().hir.span(hir.tcx().hir.body_owner(body_id));
|
|
|
|
let ty = hir.tcx().types.err;
|
2017-07-11 12:57:05 -07:00
|
|
|
let mut builder = Builder::new(hir, span, 0, ty);
|
2017-02-15 15:00:20 +02:00
|
|
|
let source_info = builder.source_info(span);
|
|
|
|
builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
|
2016-12-26 14:34:03 +01:00
|
|
|
builder.finish(vec![], ty, None)
|
2017-02-15 15:00:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
2016-09-25 01:38:27 +02:00
|
|
|
fn new(hir: Cx<'a, 'gcx, 'tcx>,
|
|
|
|
span: Span,
|
|
|
|
arg_count: usize,
|
|
|
|
return_ty: Ty<'tcx>)
|
|
|
|
-> Builder<'a, 'gcx, 'tcx> {
|
2016-04-15 17:11:24 +03:00
|
|
|
let mut builder = Builder {
|
2017-08-06 22:54:09 -07:00
|
|
|
hir,
|
2016-06-07 17:28:36 +03:00
|
|
|
cfg: CFG { basic_blocks: IndexVec::new() },
|
2016-04-15 17:11:24 +03:00
|
|
|
fn_span: span,
|
2017-08-06 22:54:09 -07:00
|
|
|
arg_count,
|
2016-04-15 17:11:24 +03:00
|
|
|
scopes: vec![],
|
2016-06-07 17:28:36 +03:00
|
|
|
visibility_scopes: IndexVec::new(),
|
2016-05-31 20:27:36 +03:00
|
|
|
visibility_scope: ARGUMENT_VISIBILITY_SCOPE,
|
2017-02-28 11:05:03 -08:00
|
|
|
breakable_scopes: vec![],
|
2017-04-11 23:52:51 +03:00
|
|
|
local_decls: IndexVec::from_elem_n(LocalDecl::new_return_pointer(return_ty,
|
|
|
|
span), 1),
|
2016-06-07 17:28:36 +03:00
|
|
|
var_indices: NodeMap(),
|
2016-04-15 17:11:24 +03:00
|
|
|
unit_temp: None,
|
|
|
|
cached_resume_block: None,
|
|
|
|
cached_return_block: None
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
|
2016-05-31 20:27:36 +03:00
|
|
|
assert_eq!(builder.new_visibility_scope(span), ARGUMENT_VISIBILITY_SCOPE);
|
|
|
|
builder.visibility_scopes[ARGUMENT_VISIBILITY_SCOPE].parent_scope = None;
|
2016-04-15 17:11:24 +03:00
|
|
|
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self,
|
|
|
|
upvar_decls: Vec<UpvarDecl>,
|
2016-12-26 14:34:03 +01:00
|
|
|
return_ty: Ty<'tcx>,
|
2017-07-10 21:11:31 +02:00
|
|
|
yield_ty: Option<Ty<'tcx>>)
|
2016-11-03 14:22:57 +11:00
|
|
|
-> Mir<'tcx> {
|
2016-04-15 17:11:24 +03:00
|
|
|
for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
|
|
|
|
if block.terminator.is_none() {
|
|
|
|
span_bug!(self.fn_span, "no terminator on block {:?}", index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:22:57 +11:00
|
|
|
Mir::new(self.cfg.basic_blocks,
|
|
|
|
self.visibility_scopes,
|
|
|
|
IndexVec::new(),
|
|
|
|
return_ty,
|
2017-07-10 21:11:31 +02:00
|
|
|
yield_ty,
|
2016-11-03 14:22:57 +11:00
|
|
|
self.local_decls,
|
|
|
|
self.arg_count,
|
|
|
|
upvar_decls,
|
|
|
|
self.fn_span
|
|
|
|
)
|
2016-04-15 17:11:24 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
fn args_and_body(&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
arguments: &[(Ty<'gcx>, Option<&'gcx hir::Pat>)],
|
2017-05-11 16:10:47 +03:00
|
|
|
argument_extent: CodeExtent,
|
2016-10-26 02:27:14 +03:00
|
|
|
ast_body: &'gcx hir::Expr)
|
2016-09-25 01:38:27 +02:00
|
|
|
-> BlockAnd<()>
|
2015-08-18 17:59:21 -04:00
|
|
|
{
|
2016-09-25 01:38:27 +02:00
|
|
|
// Allocate locals for the function arguments
|
|
|
|
for &(ty, pattern) in arguments.iter() {
|
|
|
|
// If this is a simple binding pattern, give the local a nice name for debuginfo.
|
|
|
|
let mut name = None;
|
|
|
|
if let Some(pat) = pattern {
|
2016-11-25 13:21:19 +02:00
|
|
|
if let hir::PatKind::Binding(_, _, ref ident, _) = pat.node {
|
2016-09-25 01:38:27 +02:00
|
|
|
name = Some(ident.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.local_decls.push(LocalDecl {
|
|
|
|
mutability: Mutability::Not,
|
2017-08-06 22:54:09 -07:00
|
|
|
ty,
|
2017-04-11 23:52:51 +03:00
|
|
|
source_info: SourceInfo {
|
|
|
|
scope: ARGUMENT_VISIBILITY_SCOPE,
|
|
|
|
span: pattern.map_or(self.fn_span, |pat| pat.span)
|
|
|
|
},
|
2017-08-06 22:54:09 -07:00
|
|
|
name,
|
2017-07-15 22:41:33 +02:00
|
|
|
internal: false,
|
2017-04-11 23:52:51 +03:00
|
|
|
is_user_variable: false,
|
2016-09-25 01:38:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
let mut scope = None;
|
2016-09-25 01:38:27 +02:00
|
|
|
// Bind the argument patterns
|
|
|
|
for (index, &(ty, pattern)) in arguments.iter().enumerate() {
|
|
|
|
// Function arguments always get the first Local indices after the return pointer
|
2017-07-11 12:57:05 -07:00
|
|
|
let lvalue = Lvalue::Local(Local::new(index + 1));
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2016-04-15 17:11:24 +03:00
|
|
|
if let Some(pattern) = pattern {
|
2017-07-26 15:54:44 +03:00
|
|
|
let pattern = Pattern::from_hir(self.hir.tcx().global_tcx(),
|
2017-07-26 15:51:53 +03:00
|
|
|
self.hir.param_env.and(self.hir.identity_substs),
|
2017-07-26 15:54:44 +03:00
|
|
|
self.hir.tables(),
|
|
|
|
pattern);
|
2016-10-26 02:27:14 +03:00
|
|
|
scope = self.declare_bindings(scope, ast_body.span, &pattern);
|
2016-05-31 20:27:36 +03:00
|
|
|
unpack!(block = self.lvalue_into_pattern(block, pattern, &lvalue));
|
2016-04-15 17:11:24 +03:00
|
|
|
}
|
2016-03-23 12:26:37 -04:00
|
|
|
|
2016-04-15 17:11:24 +03:00
|
|
|
// Make sure we drop (parts of) the argument even when not matched on.
|
2016-10-26 02:27:14 +03:00
|
|
|
self.schedule_drop(pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
|
2017-07-11 12:57:05 -07:00
|
|
|
argument_extent, &lvalue, ty);
|
2016-04-15 17:11:24 +03:00
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
2016-03-23 12:26:37 -04:00
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
// Enter the argument pattern bindings visibility scope, if it exists.
|
|
|
|
if let Some(visibility_scope) = scope {
|
|
|
|
self.visibility_scope = visibility_scope;
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:27:14 +03:00
|
|
|
let body = self.hir.mirror(ast_body);
|
|
|
|
self.into(&Lvalue::Local(RETURN_POINTER), block, body)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2016-01-16 00:36:32 +02:00
|
|
|
|
|
|
|
fn get_unit_temp(&mut self) -> Lvalue<'tcx> {
|
|
|
|
match self.unit_temp {
|
|
|
|
Some(ref tmp) => tmp.clone(),
|
|
|
|
None => {
|
|
|
|
let ty = self.hir.unit_ty();
|
2017-04-11 23:52:51 +03:00
|
|
|
let fn_span = self.fn_span;
|
|
|
|
let tmp = self.temp(ty, fn_span);
|
2016-01-16 00:36:32 +02:00
|
|
|
self.unit_temp = Some(tmp.clone());
|
|
|
|
tmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 00:13:30 +03:00
|
|
|
|
|
|
|
fn return_block(&mut self) -> BasicBlock {
|
|
|
|
match self.cached_return_block {
|
|
|
|
Some(rb) => rb,
|
|
|
|
None => {
|
|
|
|
let rb = self.cfg.start_new_block();
|
|
|
|
self.cached_return_block = Some(rb);
|
|
|
|
rb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Builder methods are broken up into modules, depending on what kind
|
|
|
|
// of thing is being translated. Note that they use the `unpack` macro
|
|
|
|
// above extensively.
|
|
|
|
|
|
|
|
mod block;
|
|
|
|
mod cfg;
|
|
|
|
mod expr;
|
|
|
|
mod into;
|
|
|
|
mod matches;
|
|
|
|
mod misc;
|
|
|
|
mod scope;
|