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;
|
2018-07-03 18:09:00 -07:00
|
|
|
use build::scope::{CachedBlock, DropKind};
|
2015-11-09 20:54:17 -05:00
|
|
|
use hair::cx::Cx;
|
2018-01-26 16:12:07 +01:00
|
|
|
use hair::{LintLevel, BindingMode, PatternKind};
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc::hir;
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::Node;
|
2017-11-16 14:04:01 +01:00
|
|
|
use rustc::hir::def_id::{DefId, LocalDefId};
|
2017-08-31 21:37:38 +03:00
|
|
|
use rustc::middle::region;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2017-10-26 19:53:31 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
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-12-08 21:18:21 +02:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2017-05-02 14:56:26 -04:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
|
|
|
use shim;
|
|
|
|
use std::mem;
|
|
|
|
use std::u32;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2015-10-05 12:31:48 -04:00
|
|
|
use syntax::ast;
|
2018-02-20 13:49:54 -05:00
|
|
|
use syntax::attr::{self, UnwindAttr};
|
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-11-10 19:20:35 +02:00
|
|
|
use transform::MirSource;
|
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();
|
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) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Variant(variant) =>
|
2017-05-02 14:56:26 -04:00
|
|
|
return create_constructor_shim(tcx, id, &variant.node.data),
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::StructCtor(ctor) =>
|
2017-05-02 14:56:26 -04:00
|
|
|
return create_constructor_shim(tcx, id, ctor),
|
2018-05-17 21:28:50 +03:00
|
|
|
|
|
|
|
_ => match tcx.hir.maybe_body_owned_by(id) {
|
|
|
|
Some(body) => body,
|
2018-08-24 11:20:30 +01:00
|
|
|
None => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
|
2018-05-17 21:28:50 +03:00
|
|
|
},
|
2017-05-02 14:56:26 -04:00
|
|
|
};
|
|
|
|
|
2017-06-09 10:55:16 +03:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-11-10 19:20:35 +02:00
|
|
|
let cx = Cx::new(&infcx, id);
|
2017-05-02 14:56:26 -04:00
|
|
|
let mut mir = if cx.tables().tainted_by_errors {
|
|
|
|
build::construct_error(cx, body_id)
|
2017-11-10 19:20:35 +02:00
|
|
|
} else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
|
2017-05-02 14:56:26 -04:00
|
|
|
// 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 {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(..) => {
|
2016-12-26 14:34:03 +01:00
|
|
|
// HACK(eddyb) Avoid having RustCall on closures,
|
|
|
|
// as it adds unnecessary (and wrong) auto-tupling.
|
|
|
|
abi = Abi::Rust;
|
2018-06-07 17:05:58 +02:00
|
|
|
Some(ArgInfo(liberated_closure_env_ty(tcx, id, body_id), None, None, None))
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Generator(..) => {
|
2017-11-18 11:16:25 -05:00
|
|
|
let gen_ty = tcx.body_tables(body_id).node_id_to_type(fn_hir_id);
|
2018-06-07 17:05:58 +02:00
|
|
|
Some(ArgInfo(gen_ty, None, None, None))
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
_ => None,
|
2017-05-02 14:56:26 -04:00
|
|
|
};
|
|
|
|
|
2017-09-19 16:20:02 +03:00
|
|
|
// FIXME: safety in closures
|
|
|
|
let safety = match fn_sig.unsafety {
|
|
|
|
hir::Unsafety::Normal => Safety::Safe,
|
|
|
|
hir::Unsafety::Unsafe => Safety::FnUnsafe,
|
|
|
|
};
|
|
|
|
|
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)| {
|
2018-06-07 17:05:58 +02:00
|
|
|
let owner_id = tcx.hir.body_owner(body_id);
|
|
|
|
let opt_ty_info;
|
|
|
|
let self_arg;
|
|
|
|
if let Some(ref fn_decl) = tcx.hir.fn_decl(owner_id) {
|
|
|
|
let ty_hir_id = fn_decl.inputs[index].hir_id;
|
|
|
|
let ty_span = tcx.hir.span(tcx.hir.hir_to_node_id(ty_hir_id));
|
|
|
|
opt_ty_info = Some(ty_span);
|
|
|
|
self_arg = if index == 0 && fn_decl.has_implicit_self {
|
|
|
|
Some(ImplicitSelfBinding)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
opt_ty_info = None;
|
|
|
|
self_arg = None;
|
|
|
|
}
|
|
|
|
ArgInfo(fn_sig.inputs()[index], opt_ty_info, Some(&*arg.pat), self_arg)
|
2017-05-02 14:56:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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-11-18 11:16:25 -05:00
|
|
|
let gen_sig = match ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Generator(gen_def_id, gen_substs, ..) =>
|
2018-05-02 13:14:30 +02:00
|
|
|
gen_substs.sig(gen_def_id, tcx),
|
2017-11-18 11:16:25 -05:00
|
|
|
_ =>
|
|
|
|
span_bug!(tcx.hir.span(id), "generator w/o generator type: {:?}", ty),
|
|
|
|
};
|
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-09-19 16:20:02 +03:00
|
|
|
build::construct_fn(cx, id, arguments, safety, 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)
|
|
|
|
};
|
|
|
|
|
2017-11-10 19:20:35 +02:00
|
|
|
mir_util::dump_mir(tcx, None, "mir_map", &0,
|
|
|
|
MirSource::item(def_id), &mir, |_, _| Ok(()) );
|
2017-05-02 14:56:26 -04:00
|
|
|
|
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-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
|
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-08-04 11:25:13 +03:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
|
|
|
|
if let Some(lifted) = self.tcx.lift(region) {
|
|
|
|
*region = lifted;
|
|
|
|
} else {
|
|
|
|
span_bug!(self.span,
|
|
|
|
"found region `{:?}` with inference types/regions in MIR",
|
|
|
|
region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
|
|
|
|
if let Some(lifted) = self.tcx.lift(constant) {
|
|
|
|
*constant = lifted;
|
|
|
|
} else {
|
|
|
|
span_bug!(self.span,
|
|
|
|
"found constant `{:?}` with inference types/regions in MIR",
|
|
|
|
constant);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-10 19:20:35 +02:00
|
|
|
let mut mir = shim::build_adt_ctor(&infcx, ctor_id, fields, span);
|
2017-05-02 14:56:26 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
};
|
|
|
|
|
2017-11-10 19:20:35 +02:00
|
|
|
mir_util::dump_mir(tcx, None, "mir_map", &0,
|
|
|
|
MirSource::item(tcx.hir.local_def_id(ctor_id)),
|
|
|
|
&mir, |_, _| Ok(()) );
|
2017-05-02 14:56:26 -04:00
|
|
|
|
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
|
|
|
|
|
2017-11-21 11:18:40 -05:00
|
|
|
fn liberated_closure_env_ty<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
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-11-08 09:45:48 -05:00
|
|
|
let (closure_def_id, closure_substs) = match closure_ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(closure_def_id, closure_substs) => (closure_def_id, closure_substs),
|
2017-11-08 09:45:48 -05:00
|
|
|
_ => bug!("closure expr does not have closure type: {:?}", closure_ty)
|
|
|
|
};
|
|
|
|
|
2017-11-21 11:18:40 -05:00
|
|
|
let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs).unwrap();
|
|
|
|
tcx.liberate_late_bound_regions(closure_def_id, &closure_env_ty)
|
2017-05-02 14:56:26 -04:00
|
|
|
}
|
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-09-19 16:20:02 +03:00
|
|
|
/// The current unsafe block in scope, even if it is hidden by
|
|
|
|
/// a PushUnsafeBlock
|
|
|
|
unpushed_unsafe: Safety,
|
|
|
|
|
|
|
|
/// The number of `push_unsafe_block` levels in scope
|
|
|
|
push_unsafe_count: usize,
|
|
|
|
|
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
|
2018-05-28 14:16:09 +03:00
|
|
|
source_scopes: IndexVec<SourceScope, SourceScopeData>,
|
2018-05-28 17:37:48 +03:00
|
|
|
source_scope_local_data: IndexVec<SourceScope, SourceScopeLocalData>,
|
2018-05-28 14:16:09 +03:00
|
|
|
source_scope: SourceScope,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2018-02-26 15:45:13 +01:00
|
|
|
/// the guard-context: each time we build the guard expression for
|
|
|
|
/// a match arm, we push onto this stack, and then pop when we
|
|
|
|
/// finish building it.
|
|
|
|
guard_context: Vec<GuardFrame>,
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
/// Maps node ids of variable bindings to the `Local`s created for them.
|
2018-02-26 15:45:13 +01:00
|
|
|
/// (A match binding can have two locals; the 2nd is for the arm's guard.)
|
|
|
|
var_indices: NodeMap<LocalsForNode>,
|
2016-09-25 01:38:27 +02:00
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
2018-07-15 15:00:58 +01:00
|
|
|
upvar_decls: Vec<UpvarDecl>,
|
2017-12-01 14:31:47 +02:00
|
|
|
unit_temp: Option<Place<'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>,
|
2017-11-13 23:08:12 +00:00
|
|
|
/// cached block with the UNREACHABLE terminator
|
|
|
|
cached_unreachable_block: Option<BasicBlock>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:45:13 +01:00
|
|
|
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
|
|
|
fn is_bound_var_in_guard(&self, id: ast::NodeId) -> bool {
|
|
|
|
self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn var_local_id(&self, id: ast::NodeId, for_guard: ForGuard) -> Local {
|
|
|
|
self.var_indices[&id].local_id(for_guard)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum LocalsForNode {
|
2018-07-26 13:14:12 +02:00
|
|
|
/// In the usual case, a node-id for an identifier maps to at most
|
|
|
|
/// one Local declaration.
|
2018-02-26 15:45:13 +01:00
|
|
|
One(Local),
|
2018-07-26 13:14:12 +02:00
|
|
|
|
|
|
|
/// The exceptional case is identifiers in a match arm's pattern
|
|
|
|
/// that are referenced in a guard of that match arm. For these,
|
|
|
|
/// we can have `2+k` Locals, where `k` is the number of candidate
|
|
|
|
/// patterns (separated by `|`) in the arm.
|
|
|
|
///
|
|
|
|
/// * `for_arm_body` is the Local used in the arm body (which is
|
|
|
|
/// just like the `One` case above),
|
|
|
|
///
|
|
|
|
/// * `ref_for_guard` is the Local used in the arm's guard (which
|
|
|
|
/// is a reference to a temp that is an alias of
|
|
|
|
/// `for_arm_body`).
|
|
|
|
///
|
|
|
|
/// * `vals_for_guard` is the `k` Locals; at most one of them will
|
|
|
|
/// get initialized by the arm's execution, and after it is
|
|
|
|
/// initialized, `ref_for_guard` will be assigned a reference to
|
|
|
|
/// it.
|
|
|
|
///
|
|
|
|
/// There reason we have `k` Locals rather than just 1 is to
|
|
|
|
/// accommodate some restrictions imposed by two-phase borrows,
|
|
|
|
/// which apply when we have a `ref mut` pattern.
|
|
|
|
ForGuard { vals_for_guard: Vec<Local>, ref_for_guard: Local, for_arm_body: Local },
|
2018-02-26 15:45:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct GuardFrameLocal {
|
|
|
|
id: ast::NodeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GuardFrameLocal {
|
|
|
|
fn new(id: ast::NodeId, _binding_mode: BindingMode) -> Self {
|
|
|
|
GuardFrameLocal {
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct GuardFrame {
|
|
|
|
/// These are the id's of names that are bound by patterns of the
|
|
|
|
/// arm of *this* guard.
|
|
|
|
///
|
|
|
|
/// (Frames higher up the stack will have the id's bound in arms
|
|
|
|
/// further out, such as in a case like:
|
|
|
|
///
|
|
|
|
/// match E1 {
|
|
|
|
/// P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// here, when building for FIXME
|
|
|
|
locals: Vec<GuardFrameLocal>,
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:13 +02:00
|
|
|
/// ForGuard indicates whether we are talking about:
|
|
|
|
/// 1. the temp for a local binding used solely within guard expressions,
|
|
|
|
/// 2. the temp that holds reference to (1.), which is actually what the
|
|
|
|
/// guard expressions see, or
|
|
|
|
/// 3. the temp for use outside of guard expressions.
|
2018-02-26 15:45:13 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
enum ForGuard {
|
2018-07-26 13:14:12 +02:00
|
|
|
/// The `usize` identifies for which candidate pattern we want the
|
|
|
|
/// local binding. We keep a temp per-candidate to accommodate
|
|
|
|
/// two-phase borrows (see `LocalsForNode` documentation).
|
|
|
|
ValWithinGuard(usize),
|
2018-05-11 23:32:13 +02:00
|
|
|
RefWithinGuard,
|
2018-02-26 15:45:13 +01:00
|
|
|
OutsideGuard,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalsForNode {
|
|
|
|
fn local_id(&self, for_guard: ForGuard) -> Local {
|
|
|
|
match (self, for_guard) {
|
|
|
|
(&LocalsForNode::One(local_id), ForGuard::OutsideGuard) |
|
2018-07-26 13:14:12 +02:00
|
|
|
(&LocalsForNode::ForGuard { ref_for_guard: local_id, .. }, ForGuard::RefWithinGuard) |
|
|
|
|
(&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) =>
|
2018-02-26 15:45:13 +01:00
|
|
|
local_id,
|
|
|
|
|
2018-07-26 13:14:12 +02:00
|
|
|
(&LocalsForNode::ForGuard { ref vals_for_guard, .. },
|
|
|
|
ForGuard::ValWithinGuard(pat_idx)) =>
|
|
|
|
vals_for_guard[pat_idx],
|
|
|
|
|
|
|
|
(&LocalsForNode::One(_), ForGuard::ValWithinGuard(_)) |
|
2018-05-11 23:32:13 +02:00
|
|
|
(&LocalsForNode::One(_), ForGuard::RefWithinGuard) =>
|
2018-02-26 15:45:13 +01:00
|
|
|
bug!("anything with one local should never be within a guard."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-25 13:41:32 +03:00
|
|
|
newtype_index! {
|
|
|
|
pub struct ScopeId { .. }
|
|
|
|
}
|
2016-05-31 20:27:36 +03:00
|
|
|
|
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
|
|
|
|
2018-03-30 23:06:05 -07: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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-21 19:32:26 +01:00
|
|
|
fn should_abort_on_panic<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2018-02-20 13:49:54 -05:00
|
|
|
fn_def_id: DefId,
|
2017-12-21 19:32:26 +01:00
|
|
|
abi: Abi)
|
|
|
|
-> bool {
|
2017-12-19 01:59:29 +01:00
|
|
|
// Not callable from C, so we can safely unwind through these
|
|
|
|
if abi == Abi::Rust || abi == Abi::RustCall { return false; }
|
|
|
|
|
|
|
|
// We never unwind, so it's not relevant to stop an unwind
|
|
|
|
if tcx.sess.panic_strategy() != PanicStrategy::Unwind { return false; }
|
|
|
|
|
|
|
|
// We cannot add landing pads, so don't add one
|
|
|
|
if tcx.sess.no_landing_pads() { return false; }
|
|
|
|
|
|
|
|
// This is a special case: some functions have a C abi but are meant to
|
|
|
|
// unwind anyway. Don't stop them.
|
2018-02-20 13:49:54 -05:00
|
|
|
let attrs = &tcx.get_attrs(fn_def_id);
|
|
|
|
match attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs) {
|
|
|
|
None => {
|
|
|
|
// FIXME(rust-lang/rust#48251) -- Had to disable
|
|
|
|
// abort-on-panic for backwards compatibility reasons.
|
|
|
|
false
|
|
|
|
}
|
2017-12-19 01:59:29 +01:00
|
|
|
|
2018-02-20 13:49:54 -05:00
|
|
|
Some(UnwindAttr::Allowed) => false,
|
|
|
|
Some(UnwindAttr::Aborts) => true,
|
|
|
|
}
|
2017-12-19 01:59:29 +01:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
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
|
|
|
|
2018-06-07 17:05:58 +02:00
|
|
|
struct ImplicitSelfBinding;
|
|
|
|
|
|
|
|
struct ArgInfo<'gcx>(Ty<'gcx>,
|
|
|
|
Option<Span>,
|
|
|
|
Option<&'gcx hir::Pat>,
|
|
|
|
Option<ImplicitSelfBinding>);
|
2018-06-07 16:32:07 +02: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,
|
2017-09-19 16:20:02 +03:00
|
|
|
safety: Safety,
|
2017-05-02 14:56:26 -04:00
|
|
|
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>
|
2018-06-07 16:32:07 +02:00
|
|
|
where A: Iterator<Item=ArgInfo<'gcx>>
|
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);
|
2018-07-15 15:00:58 +01:00
|
|
|
|
|
|
|
// Gather the upvars of a closure, if any.
|
|
|
|
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
|
|
|
|
freevars.iter().map(|fv| {
|
|
|
|
let var_id = fv.var_id();
|
|
|
|
let var_hir_id = tcx.hir.node_to_hir_id(var_id);
|
|
|
|
let closure_expr_id = tcx.hir.local_def_id(fn_id);
|
|
|
|
let capture = hir.tables().upvar_capture(ty::UpvarId {
|
|
|
|
var_id: var_hir_id,
|
|
|
|
closure_expr_id: LocalDefId::from_def_id(closure_expr_id),
|
|
|
|
});
|
|
|
|
let by_ref = match capture {
|
|
|
|
ty::UpvarCapture::ByValue => false,
|
|
|
|
ty::UpvarCapture::ByRef(..) => true
|
|
|
|
};
|
|
|
|
let mut decl = UpvarDecl {
|
|
|
|
debug_name: keywords::Invalid.name(),
|
|
|
|
var_hir_id: ClearCrossCrate::Set(var_hir_id),
|
|
|
|
by_ref,
|
|
|
|
mutability: Mutability::Not,
|
|
|
|
};
|
2018-08-25 15:56:16 +01:00
|
|
|
if let Some(Node::Binding(pat)) = tcx.hir.find(var_id) {
|
2018-07-15 15:00:58 +01:00
|
|
|
if let hir::PatKind::Binding(_, _, ident, _) = pat.node {
|
|
|
|
decl.debug_name = ident.name;
|
|
|
|
|
|
|
|
if let Some(&bm) = hir.tables.pat_binding_modes().get(pat.hir_id) {
|
|
|
|
if bm == ty::BindByValue(hir::MutMutable) {
|
|
|
|
decl.mutability = Mutability::Mut;
|
|
|
|
} else {
|
|
|
|
decl.mutability = Mutability::Not;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decl
|
|
|
|
}).collect()
|
|
|
|
});
|
|
|
|
|
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(),
|
2017-09-19 16:20:02 +03:00
|
|
|
safety,
|
2018-07-15 15:00:58 +01:00
|
|
|
return_ty,
|
|
|
|
upvar_decls);
|
2016-03-18 08:52:13 -04:00
|
|
|
|
2018-02-20 13:49:54 -05:00
|
|
|
let fn_def_id = tcx.hir.local_def_id(fn_id);
|
2017-08-31 21:37:38 +03:00
|
|
|
let call_site_scope = region::Scope::CallSite(body.value.hir_id.local_id);
|
|
|
|
let arg_scope = region::Scope::Arguments(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);
|
2017-09-13 22:33:07 +03:00
|
|
|
let call_site_s = (call_site_scope, source_info);
|
|
|
|
unpack!(block = builder.in_scope(call_site_s, LintLevel::Inherited, block, |builder| {
|
2018-02-20 13:49:54 -05:00
|
|
|
if should_abort_on_panic(tcx, fn_def_id, abi) {
|
2017-12-19 01:59:29 +01:00
|
|
|
builder.schedule_abort();
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:33:07 +03:00
|
|
|
let arg_scope_s = (arg_scope, source_info);
|
|
|
|
unpack!(block = builder.in_scope(arg_scope_s, LintLevel::Inherited, block, |builder| {
|
2017-08-31 21:37:38 +03:00
|
|
|
builder.args_and_body(block, &arguments, arg_scope, &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
|
2018-03-10 17:45:47 +03:00
|
|
|
let fn_end = span.shrink_to_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);
|
2017-11-13 23:08:12 +00:00
|
|
|
// Attribute any unreachable codepaths to the function's closing brace
|
|
|
|
if let Some(unreachable_block) = builder.cached_unreachable_block {
|
|
|
|
builder.cfg.terminate(unreachable_block, source_info,
|
|
|
|
TerminatorKind::Unreachable);
|
|
|
|
}
|
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
|
|
|
}
|
2017-12-21 00:35:19 +02:00
|
|
|
let closure_expr_id = tcx.hir.local_def_id(fn_id);
|
|
|
|
info!("fn_id {:?} has attrs {:?}", closure_expr_id,
|
|
|
|
tcx.get_attrs(closure_expr_id));
|
2016-03-22 16:05:28 -04:00
|
|
|
|
2018-07-15 15:00:58 +01:00
|
|
|
let mut mir = builder.finish(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);
|
2018-07-15 15:00:58 +01:00
|
|
|
let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, vec![]);
|
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);
|
2017-12-01 14:39:51 +02:00
|
|
|
unpack!(block = builder.into_expr(&Place::Local(RETURN_PLACE), 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
|
|
|
|
2017-12-20 14:54:50 +02:00
|
|
|
// Constants may be match expressions in which case an unreachable block may
|
|
|
|
// be created, so terminate it properly.
|
|
|
|
if let Some(unreachable_block) = builder.cached_unreachable_block {
|
|
|
|
builder.cfg.terminate(unreachable_block, source_info,
|
|
|
|
TerminatorKind::Unreachable);
|
|
|
|
}
|
|
|
|
|
2018-07-15 15:00:58 +01:00
|
|
|
builder.finish(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-09-13 22:33:07 +03:00
|
|
|
body_id: hir::BodyId)
|
|
|
|
-> Mir<'tcx> {
|
|
|
|
let owner_id = hir.tcx().hir.body_owner(body_id);
|
|
|
|
let span = hir.tcx().hir.span(owner_id);
|
2017-02-15 15:00:20 +02:00
|
|
|
let ty = hir.tcx().types.err;
|
2018-07-15 15:00:58 +01:00
|
|
|
let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, vec![]);
|
2017-02-15 15:00:20 +02:00
|
|
|
let source_info = builder.source_info(span);
|
|
|
|
builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
|
2018-07-15 15:00:58 +01:00
|
|
|
builder.finish(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,
|
2017-09-19 16:20:02 +03:00
|
|
|
safety: Safety,
|
2018-07-15 15:00:58 +01:00
|
|
|
return_ty: Ty<'tcx>,
|
|
|
|
upvar_decls: Vec<UpvarDecl>)
|
2016-09-25 01:38:27 +02:00
|
|
|
-> Builder<'a, 'gcx, 'tcx> {
|
2017-09-13 22:33:07 +03:00
|
|
|
let lint_level = LintLevel::Explicit(hir.root_lint_level);
|
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![],
|
2018-05-28 14:16:09 +03:00
|
|
|
source_scopes: IndexVec::new(),
|
|
|
|
source_scope: OUTERMOST_SOURCE_SCOPE,
|
2018-05-28 17:37:48 +03:00
|
|
|
source_scope_local_data: IndexVec::new(),
|
2018-02-26 15:45:13 +01:00
|
|
|
guard_context: vec![],
|
2017-09-19 16:20:02 +03:00
|
|
|
push_unsafe_count: 0,
|
|
|
|
unpushed_unsafe: safety,
|
2017-02-28 11:05:03 -08:00
|
|
|
breakable_scopes: vec![],
|
2017-12-01 14:39:51 +02:00
|
|
|
local_decls: IndexVec::from_elem_n(LocalDecl::new_return_place(return_ty,
|
2017-04-11 23:52:51 +03:00
|
|
|
span), 1),
|
2018-07-15 15:00:58 +01:00
|
|
|
upvar_decls,
|
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,
|
2017-11-13 23:08:12 +00:00
|
|
|
cached_return_block: None,
|
|
|
|
cached_unreachable_block: None,
|
2016-04-15 17:11:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
|
2017-09-13 22:33:07 +03:00
|
|
|
assert_eq!(
|
2018-05-28 14:16:09 +03:00
|
|
|
builder.new_source_scope(span, lint_level, Some(safety)),
|
|
|
|
OUTERMOST_SOURCE_SCOPE);
|
|
|
|
builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
|
2016-04-15 17:11:24 +03:00
|
|
|
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self,
|
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,
|
2018-05-28 14:16:09 +03:00
|
|
|
self.source_scopes,
|
2018-05-28 17:37:48 +03:00
|
|
|
ClearCrossCrate::Set(self.source_scope_local_data),
|
2016-11-03 14:22:57 +11:00
|
|
|
IndexVec::new(),
|
2017-07-10 21:11:31 +02:00
|
|
|
yield_ty,
|
2016-11-03 14:22:57 +11:00
|
|
|
self.local_decls,
|
|
|
|
self.arg_count,
|
2018-07-15 15:00:58 +01:00
|
|
|
self.upvar_decls,
|
2016-11-03 14:22:57 +11:00
|
|
|
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,
|
2018-06-07 16:32:07 +02:00
|
|
|
arguments: &[ArgInfo<'gcx>],
|
2017-08-31 21:37:38 +03:00
|
|
|
argument_scope: region::Scope,
|
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
|
2018-06-07 17:05:58 +02:00
|
|
|
for &ArgInfo(ty, _, pattern, _) in arguments.iter() {
|
2018-07-03 20:12:09 +01:00
|
|
|
// If this is a simple binding pattern, give the local a name for
|
|
|
|
// debuginfo and so that error reporting knows that this is a user
|
|
|
|
// variable. For any other pattern the pattern introduces new
|
|
|
|
// variables which will be named instead.
|
2016-09-25 01:38:27 +02:00
|
|
|
let mut name = None;
|
|
|
|
if let Some(pat) = pattern {
|
2018-07-03 20:12:09 +01:00
|
|
|
match pat.node {
|
|
|
|
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, _)
|
|
|
|
| hir::PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, _) => {
|
|
|
|
name = Some(ident.name);
|
|
|
|
}
|
|
|
|
_ => (),
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:24:53 +03:00
|
|
|
let source_info = SourceInfo {
|
|
|
|
scope: OUTERMOST_SOURCE_SCOPE,
|
|
|
|
span: pattern.map_or(self.fn_span, |pat| pat.span)
|
|
|
|
};
|
2016-09-25 01:38:27 +02:00
|
|
|
self.local_decls.push(LocalDecl {
|
2017-11-30 23:19:06 +00:00
|
|
|
mutability: Mutability::Mut,
|
2017-08-06 22:54:09 -07:00
|
|
|
ty,
|
2018-09-10 10:54:31 -04:00
|
|
|
user_ty: None,
|
2018-05-29 21:31:33 +03:00
|
|
|
source_info,
|
2018-05-29 13:55:21 +03:00
|
|
|
visibility_scope: source_info.scope,
|
2017-08-06 22:54:09 -07:00
|
|
|
name,
|
2017-07-15 22:41:33 +02:00
|
|
|
internal: false,
|
2018-06-07 15:25:08 +02:00
|
|
|
is_user_variable: None,
|
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
|
2018-06-07 17:05:58 +02:00
|
|
|
for (index, arg_info) in arguments.iter().enumerate() {
|
2017-12-01 14:39:51 +02:00
|
|
|
// Function arguments always get the first Local indices after the return place
|
Avoid unnecessary copies of arguments that are simple bindings
Initially MIR differentiated between arguments and locals, which
introduced a need to add extra copies assigning the argument to a
local, even for simple bindings. This differentiation no longer exists,
but we're still creating those copies, bloating the MIR and LLVM IR we
emit.
Additionally, the current approach means that we create debug info for
both the incoming argument (marking it as an argument), and then
immediately shadow it a local that goes by the same name. This can be
confusing when using e.g. "info args" in gdb, or when e.g. a debugger
with a GUI displays the function arguments separately from the local
variables, especially when the binding is mutable, because the argument
doesn't change, while the local variable does.
2017-10-11 20:49:36 +02:00
|
|
|
let local = Local::new(index + 1);
|
2017-12-01 14:39:51 +02:00
|
|
|
let place = Place::Local(local);
|
2018-06-07 17:05:58 +02:00
|
|
|
let &ArgInfo(ty, opt_ty_info, pattern, ref self_binding) = arg_info;
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2016-04-15 17:11:24 +03:00
|
|
|
if let Some(pattern) = pattern {
|
2017-08-04 00:41:44 +03:00
|
|
|
let pattern = self.hir.pattern_from_hir(pattern);
|
2018-06-27 22:06:54 +01:00
|
|
|
let span = pattern.span;
|
Avoid unnecessary copies of arguments that are simple bindings
Initially MIR differentiated between arguments and locals, which
introduced a need to add extra copies assigning the argument to a
local, even for simple bindings. This differentiation no longer exists,
but we're still creating those copies, bloating the MIR and LLVM IR we
emit.
Additionally, the current approach means that we create debug info for
both the incoming argument (marking it as an argument), and then
immediately shadow it a local that goes by the same name. This can be
confusing when using e.g. "info args" in gdb, or when e.g. a debugger
with a GUI displays the function arguments separately from the local
variables, especially when the binding is mutable, because the argument
doesn't change, while the local variable does.
2017-10-11 20:49:36 +02:00
|
|
|
|
|
|
|
match *pattern.kind {
|
|
|
|
// Don't introduce extra copies for simple bindings
|
|
|
|
PatternKind::Binding { mutability, var, mode: BindingMode::ByValue, .. } => {
|
|
|
|
self.local_decls[local].mutability = mutability;
|
2018-06-07 17:05:58 +02:00
|
|
|
self.local_decls[local].is_user_variable =
|
|
|
|
if let Some(ImplicitSelfBinding) = self_binding {
|
|
|
|
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf))
|
|
|
|
} else {
|
|
|
|
let binding_mode = ty::BindingMode::BindByValue(mutability.into());
|
|
|
|
Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
2018-06-27 22:06:54 +01:00
|
|
|
binding_mode,
|
|
|
|
opt_ty_info,
|
|
|
|
opt_match_place: Some((Some(place.clone()), span)),
|
2018-08-07 01:02:39 -07:00
|
|
|
pat_span: span,
|
2018-06-27 22:06:54 +01:00
|
|
|
})))
|
2018-06-07 17:05:58 +02:00
|
|
|
};
|
2018-02-26 15:45:13 +01:00
|
|
|
self.var_indices.insert(var, LocalsForNode::One(local));
|
Avoid unnecessary copies of arguments that are simple bindings
Initially MIR differentiated between arguments and locals, which
introduced a need to add extra copies assigning the argument to a
local, even for simple bindings. This differentiation no longer exists,
but we're still creating those copies, bloating the MIR and LLVM IR we
emit.
Additionally, the current approach means that we create debug info for
both the incoming argument (marking it as an argument), and then
immediately shadow it a local that goes by the same name. This can be
confusing when using e.g. "info args" in gdb, or when e.g. a debugger
with a GUI displays the function arguments separately from the local
variables, especially when the binding is mutable, because the argument
doesn't change, while the local variable does.
2017-10-11 20:49:36 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
scope = self.declare_bindings(scope, ast_body.span,
|
2018-07-26 13:14:12 +02:00
|
|
|
LintLevel::Inherited, &[pattern.clone()],
|
2018-06-27 22:06:54 +01:00
|
|
|
matches::ArmHasGuard(false),
|
|
|
|
Some((Some(&place), span)));
|
|
|
|
unpack!(block = self.place_into_pattern(block, pattern, &place, false));
|
Avoid unnecessary copies of arguments that are simple bindings
Initially MIR differentiated between arguments and locals, which
introduced a need to add extra copies assigning the argument to a
local, even for simple bindings. This differentiation no longer exists,
but we're still creating those copies, bloating the MIR and LLVM IR we
emit.
Additionally, the current approach means that we create debug info for
both the incoming argument (marking it as an argument), and then
immediately shadow it a local that goes by the same name. This can be
confusing when using e.g. "info args" in gdb, or when e.g. a debugger
with a GUI displays the function arguments separately from the local
variables, especially when the binding is mutable, because the argument
doesn't change, while the local variable does.
2017-10-11 20:49:36 +02:00
|
|
|
}
|
|
|
|
}
|
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.
|
2018-07-03 18:09:00 -07:00
|
|
|
self.schedule_drop(
|
|
|
|
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
|
|
|
|
argument_scope, &place, ty,
|
|
|
|
DropKind::Value { cached_block: CachedBlock::default() },
|
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
2016-03-23 12:26:37 -04:00
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
// Enter the argument pattern bindings source scope, if it exists.
|
|
|
|
if let Some(source_scope) = scope {
|
|
|
|
self.source_scope = source_scope;
|
2016-05-31 20:27:36 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 02:27:14 +03:00
|
|
|
let body = self.hir.mirror(ast_body);
|
2017-12-01 14:39:51 +02:00
|
|
|
self.into(&Place::Local(RETURN_PLACE), block, body)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2016-01-16 00:36:32 +02:00
|
|
|
|
2017-12-01 14:31:47 +02:00
|
|
|
fn get_unit_temp(&mut self) -> Place<'tcx> {
|
2016-01-16 00:36:32 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 23:08:12 +00:00
|
|
|
|
|
|
|
fn unreachable_block(&mut self) -> BasicBlock {
|
|
|
|
match self.cached_unreachable_block {
|
|
|
|
Some(ub) => ub,
|
|
|
|
None => {
|
|
|
|
let ub = self.cfg.start_new_block();
|
|
|
|
self.cached_unreachable_block = Some(ub);
|
|
|
|
ub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Builder methods are broken up into modules, depending on what kind
|
2018-05-08 16:10:16 +03:00
|
|
|
// of thing is being lowered. Note that they use the `unpack` macro
|
2015-08-18 17:59:21 -04:00
|
|
|
// above extensively.
|
|
|
|
|
|
|
|
mod block;
|
|
|
|
mod cfg;
|
|
|
|
mod expr;
|
|
|
|
mod into;
|
|
|
|
mod matches;
|
|
|
|
mod misc;
|
|
|
|
mod scope;
|