2013-05-30 03:16:33 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-08-28 15:54:45 -07:00
|
|
|
//!
|
|
|
|
//
|
2015-03-15 04:01:57 +02:00
|
|
|
// Code relating to drop glue.
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2015-12-07 02:38:29 +13:00
|
|
|
use std;
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm;
|
2015-02-28 23:55:50 +02:00
|
|
|
use llvm::{ValueRef, get_param};
|
2014-10-01 01:26:04 +03:00
|
|
|
use middle::lang_items::ExchangeFreeFnLangItem;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::subst::{Substs};
|
2016-03-11 02:31:38 +02:00
|
|
|
use rustc::traits;
|
2016-05-22 00:47:21 -04:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-03-22 19:23:36 +02:00
|
|
|
use adt;
|
|
|
|
use base::*;
|
|
|
|
use build::*;
|
2016-08-16 17:41:38 +03:00
|
|
|
use callee::{Callee};
|
2016-03-22 19:23:36 +02:00
|
|
|
use common::*;
|
|
|
|
use debuginfo::DebugLoc;
|
|
|
|
use machine::*;
|
|
|
|
use monomorphize;
|
2016-05-09 13:37:14 -04:00
|
|
|
use trans_item::TransItem;
|
2016-03-22 19:23:36 +02:00
|
|
|
use type_of::{type_of, sizing_type_of, align_of};
|
|
|
|
use type_::Type;
|
|
|
|
use value::Value;
|
2013-06-16 22:52:44 +12:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
use arena::TypedArena;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 21:21:50 +02:00
|
|
|
pub fn trans_exchange_free_dyn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
2015-02-04 17:16:59 +01:00
|
|
|
v: ValueRef,
|
|
|
|
size: ValueRef,
|
|
|
|
align: ValueRef,
|
|
|
|
debug_loc: DebugLoc)
|
2014-09-06 19:13:04 +03:00
|
|
|
-> Block<'blk, 'tcx> {
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("trans_exchange_free");
|
2016-02-23 21:21:50 +02:00
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
let def_id = langcall(bcx.tcx(), None, "", ExchangeFreeFnLangItem);
|
2016-02-23 21:21:50 +02:00
|
|
|
let args = [PointerCast(bcx, v, Type::i8p(bcx.ccx())), size, align];
|
2016-08-08 23:39:49 +03:00
|
|
|
Callee::def(bcx.ccx(), def_id, Substs::empty(bcx.tcx()))
|
2016-08-16 17:41:38 +03:00
|
|
|
.call(bcx, debug_loc, &args, None).bcx
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2015-02-04 17:16:59 +01:00
|
|
|
pub fn trans_exchange_free<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
|
|
|
|
v: ValueRef,
|
|
|
|
size: u64,
|
|
|
|
align: u32,
|
|
|
|
debug_loc: DebugLoc)
|
|
|
|
-> Block<'blk, 'tcx> {
|
|
|
|
trans_exchange_free_dyn(cx,
|
|
|
|
v,
|
|
|
|
C_uint(cx.ccx(), size),
|
|
|
|
C_uint(cx.ccx(), align),
|
|
|
|
debug_loc)
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
|
|
|
|
2015-02-04 17:16:59 +01:00
|
|
|
pub fn trans_exchange_free_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
ptr: ValueRef,
|
|
|
|
content_ty: Ty<'tcx>,
|
|
|
|
debug_loc: DebugLoc)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-12-18 09:26:10 -05:00
|
|
|
assert!(type_is_sized(bcx.ccx().tcx(), content_ty));
|
2014-05-21 00:18:10 -04:00
|
|
|
let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
|
|
|
|
let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
|
|
|
|
|
|
|
|
// `Box<ZeroSizeType>` does not allocate.
|
|
|
|
if content_size != 0 {
|
2014-08-06 11:59:40 +02:00
|
|
|
let content_align = align_of(bcx.ccx(), content_ty);
|
2015-02-04 17:16:59 +01:00
|
|
|
trans_exchange_free(bcx, ptr, content_size, content_align, debug_loc)
|
2014-05-21 00:18:10 -04:00
|
|
|
} else {
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> bool {
|
2015-12-31 16:56:40 -05:00
|
|
|
tcx.type_needs_drop_given_env(ty, &tcx.empty_parameter_environment())
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn get_drop_glue_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
t: Ty<'tcx>) -> Ty<'tcx> {
|
2016-05-22 00:47:21 -04:00
|
|
|
assert!(t.is_normalized_for_trans());
|
|
|
|
|
2014-08-06 11:59:40 +02:00
|
|
|
// Even if there is no dtor for t, there might be one deeper down and we
|
|
|
|
// might need to pass in the vtable ptr.
|
2014-12-18 09:26:10 -05:00
|
|
|
if !type_is_sized(tcx, t) {
|
2016-05-22 00:47:21 -04:00
|
|
|
return t;
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
2015-02-25 23:09:58 +01:00
|
|
|
|
|
|
|
// FIXME (#22815): note that type_needs_drop conservatively
|
|
|
|
// approximates in some cases and may say a type expression
|
|
|
|
// requires drop glue when it actually does not.
|
|
|
|
//
|
|
|
|
// (In this case it is not clear whether any harm is done, i.e.
|
|
|
|
// erroneously returning `t` in some cases where we could have
|
|
|
|
// returned `tcx.types.i8` does not appear unsound. The impact on
|
|
|
|
// code quality is unknown at this time.)
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
if !type_needs_drop(tcx, t) {
|
2014-12-25 07:20:48 -05:00
|
|
|
return tcx.types.i8;
|
2014-02-06 01:07:06 -05:00
|
|
|
}
|
2014-10-31 10:51:16 +02:00
|
|
|
match t.sty {
|
2016-05-03 04:56:42 +03:00
|
|
|
ty::TyBox(typ) if !type_needs_drop(tcx, typ)
|
2014-12-18 09:26:10 -05:00
|
|
|
&& type_is_sized(tcx, typ) => {
|
2016-06-30 21:22:47 +03:00
|
|
|
tcx.normalizing_infer_ctxt(traits::Reveal::All).enter(|infcx| {
|
2016-05-11 04:14:41 +03:00
|
|
|
let layout = t.layout(&infcx).unwrap();
|
|
|
|
if layout.size(&tcx.data_layout).bytes() == 0 {
|
|
|
|
// `Box<ZeroSizeType>` does not allocate.
|
|
|
|
tcx.types.i8
|
|
|
|
} else {
|
2016-05-22 00:47:21 -04:00
|
|
|
t
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
})
|
2014-05-21 00:18:10 -04:00
|
|
|
}
|
2016-05-22 00:47:21 -04:00
|
|
|
_ => t
|
2014-02-06 01:07:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
debuginfo: Make sure that all calls to drop glue are associated with debug locations.
This commit makes rustc emit debug locations for all call
and invoke statements in LLVM IR, if they are contained
within a function that debuginfo is enabled for. This is
important because LLVM does not handle the case where a
function body containing debuginfo is inlined into another
function with debuginfo, but the inlined call statement
does not have a debug location. In this case, LLVM will
not know where (in terms of source code coordinates) the
function was inlined to and we end up with some statements
still linked to the source locations in there original,
non-inlined function without any indication that they are
indeed an inline-copy. Later, when generating DWARF from
the IR, LLVM will interpret this as corrupt IR and abort.
Unfortunately, the undesirable case described above can
still occur when using LTO. If there is a crate compiled
without debuginfo calling into a crate compiled with
debuginfo, we again end up with the conditions triggering
the error. This is why some LTO tests still fail with the
dreaded assertion, if the standard library was built with
debuginfo enabled.
That is, `RUSTFLAGS_STAGE2=-g make rustc-stage2` will
succeed but `RUSTFLAGS_STAGE2=-g make check` will still
fail after this commit has been merged. This is a problem
that has to be dealt with separately.
Fixes #17201
Fixes #15816
Fixes #15156
2014-09-24 08:49:38 +02:00
|
|
|
pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
v: ValueRef,
|
2014-09-29 22:11:30 +03:00
|
|
|
t: Ty<'tcx>,
|
2015-04-22 11:52:08 +02:00
|
|
|
debug_loc: DebugLoc) -> Block<'blk, 'tcx> {
|
2016-08-16 17:41:38 +03:00
|
|
|
drop_ty_core(bcx, v, t, debug_loc, false)
|
2015-04-22 11:52:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn drop_ty_core<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
v: ValueRef,
|
|
|
|
t: Ty<'tcx>,
|
|
|
|
debug_loc: DebugLoc,
|
2016-08-16 17:41:38 +03:00
|
|
|
skip_dtor: bool)
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 09:25:14 +02:00
|
|
|
-> Block<'blk, 'tcx> {
|
2012-08-28 15:54:45 -07:00
|
|
|
// NB: v is an *alias* of type t here, not a direct value.
|
2016-08-16 17:41:38 +03:00
|
|
|
debug!("drop_ty_core(t={:?}, skip_dtor={})", t, skip_dtor);
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("drop_ty");
|
2015-02-25 23:09:58 +01:00
|
|
|
if bcx.fcx.type_needs_drop(t) {
|
2014-04-25 15:14:52 +12:00
|
|
|
let ccx = bcx.ccx();
|
2015-04-22 11:52:08 +02:00
|
|
|
let g = if skip_dtor {
|
|
|
|
DropGlueKind::TyContents(t)
|
|
|
|
} else {
|
|
|
|
DropGlueKind::Ty(t)
|
|
|
|
};
|
|
|
|
let glue = get_drop_glue_core(ccx, g);
|
2016-05-06 01:23:44 -04:00
|
|
|
let glue_type = get_drop_glue_type(ccx.tcx(), t);
|
2014-02-06 01:07:06 -05:00
|
|
|
let ptr = if glue_type != t {
|
|
|
|
PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
};
|
debuginfo: Make sure that all calls to drop glue are associated with debug locations.
This commit makes rustc emit debug locations for all call
and invoke statements in LLVM IR, if they are contained
within a function that debuginfo is enabled for. This is
important because LLVM does not handle the case where a
function body containing debuginfo is inlined into another
function with debuginfo, but the inlined call statement
does not have a debug location. In this case, LLVM will
not know where (in terms of source code coordinates) the
function was inlined to and we end up with some statements
still linked to the source locations in there original,
non-inlined function without any indication that they are
indeed an inline-copy. Later, when generating DWARF from
the IR, LLVM will interpret this as corrupt IR and abort.
Unfortunately, the undesirable case described above can
still occur when using LTO. If there is a crate compiled
without debuginfo calling into a crate compiled with
debuginfo, we again end up with the conditions triggering
the error. This is why some LTO tests still fail with the
dreaded assertion, if the standard library was built with
debuginfo enabled.
That is, `RUSTFLAGS_STAGE2=-g make rustc-stage2` will
succeed but `RUSTFLAGS_STAGE2=-g make check` will still
fail after this commit has been merged. This is a problem
that has to be dealt with separately.
Fixes #17201
Fixes #15816
Fixes #15156
2014-09-24 08:49:38 +02:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
// No drop-hint ==> call standard drop glue
|
|
|
|
Call(bcx, glue, &[ptr], debug_loc);
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2014-02-06 01:07:06 -05:00
|
|
|
bcx
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
debuginfo: Make sure that all calls to drop glue are associated with debug locations.
This commit makes rustc emit debug locations for all call
and invoke statements in LLVM IR, if they are contained
within a function that debuginfo is enabled for. This is
important because LLVM does not handle the case where a
function body containing debuginfo is inlined into another
function with debuginfo, but the inlined call statement
does not have a debug location. In this case, LLVM will
not know where (in terms of source code coordinates) the
function was inlined to and we end up with some statements
still linked to the source locations in there original,
non-inlined function without any indication that they are
indeed an inline-copy. Later, when generating DWARF from
the IR, LLVM will interpret this as corrupt IR and abort.
Unfortunately, the undesirable case described above can
still occur when using LTO. If there is a crate compiled
without debuginfo calling into a crate compiled with
debuginfo, we again end up with the conditions triggering
the error. This is why some LTO tests still fail with the
dreaded assertion, if the standard library was built with
debuginfo enabled.
That is, `RUSTFLAGS_STAGE2=-g make rustc-stage2` will
succeed but `RUSTFLAGS_STAGE2=-g make check` will still
fail after this commit has been merged. This is a problem
that has to be dealt with separately.
Fixes #17201
Fixes #15816
Fixes #15156
2014-09-24 08:49:38 +02:00
|
|
|
pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
v: ValueRef,
|
2014-09-29 22:11:30 +03:00
|
|
|
t: Ty<'tcx>,
|
2015-04-22 11:52:08 +02:00
|
|
|
debug_loc: DebugLoc,
|
|
|
|
skip_dtor: bool)
|
2014-09-06 19:13:04 +03:00
|
|
|
-> Block<'blk, 'tcx> {
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("drop_ty_immediate");
|
2015-08-25 18:29:24 +02:00
|
|
|
let vp = alloc_ty(bcx, t, "");
|
2015-08-25 18:24:16 +02:00
|
|
|
call_lifetime_start(bcx, vp);
|
2015-01-08 16:28:07 +01:00
|
|
|
store_ty(bcx, v, vp, t);
|
2016-08-16 17:41:38 +03:00
|
|
|
let bcx = drop_ty_core(bcx, vp, t, debug_loc, skip_dtor);
|
2015-08-25 18:27:44 +02:00
|
|
|
call_lifetime_end(bcx, vp);
|
|
|
|
bcx
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ValueRef {
|
2015-04-22 11:52:08 +02:00
|
|
|
get_drop_glue_core(ccx, DropGlueKind::Ty(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum DropGlueKind<'tcx> {
|
|
|
|
/// The normal path; runs the dtor, and then recurs on the contents
|
|
|
|
Ty(Ty<'tcx>),
|
|
|
|
/// Skips the dtor, if any, for ty; drops the contents directly.
|
|
|
|
/// Note that the dtor is only skipped at the most *shallow*
|
|
|
|
/// level, namely, an `impl Drop for Ty` itself. So, for example,
|
2015-10-07 23:11:25 +01:00
|
|
|
/// if Ty is Newtype(S) then only the Drop impl for Newtype itself
|
|
|
|
/// will be skipped, while the Drop impl for S, if any, will be
|
|
|
|
/// invoked.
|
2015-04-22 11:52:08 +02:00
|
|
|
TyContents(Ty<'tcx>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> DropGlueKind<'tcx> {
|
2016-04-21 15:36:57 -04:00
|
|
|
pub fn ty(&self) -> Ty<'tcx> {
|
2015-04-22 11:52:08 +02:00
|
|
|
match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:36:57 -04:00
|
|
|
pub fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
|
2015-04-22 11:52:08 +02:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
DropGlueKind::Ty(t) => DropGlueKind::Ty(f(t)),
|
|
|
|
DropGlueKind::TyContents(t) => DropGlueKind::TyContents(f(t)),
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 01:39:20 +03:00
|
|
|
}
|
2015-04-22 11:52:08 +02:00
|
|
|
|
|
|
|
fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
g: DropGlueKind<'tcx>) -> ValueRef {
|
2016-05-06 01:23:44 -04:00
|
|
|
let g = g.map_ty(|t| get_drop_glue_type(ccx.tcx(), t));
|
2015-04-22 11:52:08 +02:00
|
|
|
match ccx.drop_glues().borrow().get(&g) {
|
2016-06-07 21:14:51 -04:00
|
|
|
Some(&(glue, _)) => return glue,
|
|
|
|
None => {
|
|
|
|
debug!("Could not find drop glue for {:?} -- {} -- {}. \
|
|
|
|
Falling back to on-demand instantiation.",
|
|
|
|
g,
|
|
|
|
TransItem::DropGlue(g).to_raw_string(),
|
2016-07-21 12:49:59 -04:00
|
|
|
ccx.codegen_unit().name());
|
2016-06-07 21:14:51 -04:00
|
|
|
|
|
|
|
ccx.stats().n_fallback_instantiations.set(ccx.stats()
|
|
|
|
.n_fallback_instantiations
|
|
|
|
.get() + 1);
|
|
|
|
}
|
2012-09-12 14:48:13 -07:00
|
|
|
}
|
2016-06-07 21:14:51 -04:00
|
|
|
|
|
|
|
// FIXME: #34151
|
|
|
|
// Normally, getting here would indicate a bug in trans::collector,
|
|
|
|
// since it seems to have missed a translation item. When we are
|
|
|
|
// translating with non-MIR-based trans, however, the results of the
|
|
|
|
// collector are not entirely reliable since it bases its analysis
|
|
|
|
// on MIR. Thus, we'll instantiate the missing function on demand in
|
|
|
|
// this codegen unit, so that things keep working.
|
|
|
|
|
2016-06-10 19:06:21 -04:00
|
|
|
TransItem::DropGlue(g).predefine(ccx, llvm::InternalLinkage);
|
2016-06-07 21:14:51 -04:00
|
|
|
TransItem::DropGlue(g).define(ccx);
|
|
|
|
|
|
|
|
// Now that we made sure that the glue function is in ccx.drop_glues,
|
|
|
|
// give it another try
|
|
|
|
get_drop_glue_core(ccx, g)
|
2016-05-09 23:56:49 -04:00
|
|
|
}
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2016-05-09 23:56:49 -04:00
|
|
|
pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
g: DropGlueKind<'tcx>) {
|
2016-03-06 16:30:21 +02:00
|
|
|
let tcx = ccx.tcx();
|
2016-05-09 23:56:49 -04:00
|
|
|
assert_eq!(g.ty(), get_drop_glue_type(tcx, g.ty()));
|
|
|
|
let (llfn, fn_ty) = ccx.drop_glues().borrow().get(&g).unwrap().clone();
|
2015-03-15 04:01:57 +02:00
|
|
|
|
|
|
|
let (arena, fcx): (TypedArena<_>, FunctionContext);
|
|
|
|
arena = TypedArena::new();
|
2016-04-06 08:34:03 +03:00
|
|
|
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, &arena);
|
2015-03-15 04:01:57 +02:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
let bcx = fcx.init(false);
|
2015-03-15 04:01:57 +02:00
|
|
|
|
|
|
|
ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1);
|
|
|
|
// All glue functions take values passed *by alias*; this is a
|
|
|
|
// requirement since in many contexts glue is invoked indirectly and
|
|
|
|
// the caller has no idea if it's dealing with something that can be
|
|
|
|
// passed by value.
|
|
|
|
//
|
|
|
|
// llfn is expected be declared to take a parameter of the appropriate
|
|
|
|
// type, so we don't need to explicitly cast the function parameter.
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
let bcx = make_drop_glue(bcx, get_param(llfn, 0), g);
|
|
|
|
fcx.finish(bcx, DebugLoc::None);
|
2014-02-06 01:07:06 -05:00
|
|
|
}
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2014-09-06 19:13:04 +03:00
|
|
|
fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
t: Ty<'tcx>,
|
2015-08-25 20:50:30 +03:00
|
|
|
v0: ValueRef)
|
2015-01-06 05:03:42 -05:00
|
|
|
-> Block<'blk, 'tcx>
|
|
|
|
{
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("trans_struct_drop t: {}", t);
|
2015-08-25 20:50:30 +03:00
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let mut bcx = bcx;
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2015-08-25 20:50:30 +03:00
|
|
|
let def = t.ty_adt_def().unwrap();
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2015-04-24 09:29:56 +02:00
|
|
|
// Be sure to put the contents into a scope so we can use an invoke
|
|
|
|
// instruction to call the user destructor but still call the field
|
|
|
|
// destructors if the user destructor panics.
|
|
|
|
//
|
|
|
|
// FIXME (#14875) panic-in-drop semantics might be unsupported; we
|
|
|
|
// might well consider changing below to more direct code.
|
|
|
|
let contents_scope = bcx.fcx.push_custom_cleanup_scope();
|
2014-08-06 11:59:40 +02:00
|
|
|
|
2015-04-24 09:29:56 +02:00
|
|
|
// Issue #23611: schedule cleanup of contents, re-inspecting the
|
|
|
|
// discriminant (if any) in case of variant swap in drop code.
|
2016-08-16 17:41:38 +03:00
|
|
|
bcx.fcx.schedule_drop_adt_contents(contents_scope, v0, t);
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2015-08-25 20:50:30 +03:00
|
|
|
let (sized_args, unsized_args);
|
|
|
|
let args: &[ValueRef] = if type_is_sized(tcx, t) {
|
|
|
|
sized_args = [v0];
|
|
|
|
&sized_args
|
2015-07-03 12:19:36 +02:00
|
|
|
} else {
|
2016-08-16 17:41:38 +03:00
|
|
|
unsized_args = [
|
|
|
|
Load(bcx, get_dataptr(bcx, v0)),
|
|
|
|
Load(bcx, get_meta(bcx, v0))
|
|
|
|
];
|
2015-08-25 20:50:30 +03:00
|
|
|
&unsized_args
|
2015-07-03 12:19:36 +02:00
|
|
|
};
|
2014-01-15 14:39:08 -05:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
let trait_ref = ty::Binder(ty::TraitRef {
|
|
|
|
def_id: tcx.lang_items.drop_trait().unwrap(),
|
2016-08-08 23:39:49 +03:00
|
|
|
substs: Substs::new_trait(tcx, vec![], vec![], t)
|
2016-03-06 17:32:47 +02:00
|
|
|
});
|
2016-05-06 00:47:28 -04:00
|
|
|
let vtbl = match fulfill_obligation(bcx.ccx().shared(), DUMMY_SP, trait_ref) {
|
2016-03-06 17:32:47 +02:00
|
|
|
traits::VtableImpl(data) => data,
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("dtor for {:?} is not an impl???", t)
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
|
|
|
let dtor_did = def.destructor().unwrap();
|
2016-02-23 21:21:50 +02:00
|
|
|
bcx = Callee::def(bcx.ccx(), dtor_did, vtbl.substs)
|
2016-08-16 17:41:38 +03:00
|
|
|
.call(bcx, DebugLoc::None, args, None).bcx;
|
2015-08-25 20:50:30 +03:00
|
|
|
|
2015-04-24 09:29:56 +02:00
|
|
|
bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, contents_scope)
|
2013-06-23 14:41:45 +12:00
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-09 14:20:22 +02:00
|
|
|
pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>,
|
|
|
|
t: Ty<'tcx>, info: ValueRef)
|
2015-04-15 11:57:29 +12:00
|
|
|
-> (ValueRef, ValueRef) {
|
2016-02-18 19:49:45 +02:00
|
|
|
debug!("calculate size of DST: {}; with lost info: {:?}",
|
|
|
|
t, Value(info));
|
2014-12-18 09:26:10 -05:00
|
|
|
if type_is_sized(bcx.tcx(), t) {
|
2014-08-06 11:59:40 +02:00
|
|
|
let sizing_type = sizing_type_of(bcx.ccx(), t);
|
2015-07-27 14:45:20 +02:00
|
|
|
let size = llsize_of_alloc(bcx.ccx(), sizing_type);
|
|
|
|
let align = align_of(bcx.ccx(), t);
|
2016-02-18 19:49:45 +02:00
|
|
|
debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
|
|
|
|
t, Value(info), size, align);
|
2015-07-27 14:45:20 +02:00
|
|
|
let size = C_uint(bcx.ccx(), size);
|
|
|
|
let align = C_uint(bcx.ccx(), align);
|
2014-08-06 11:59:40 +02:00
|
|
|
return (size, align);
|
|
|
|
}
|
2016-03-09 14:20:22 +02:00
|
|
|
if bcx.is_unreachable() {
|
|
|
|
let llty = Type::int(bcx.ccx());
|
|
|
|
return (C_undef(llty), C_undef(llty));
|
|
|
|
}
|
2014-10-31 10:51:16 +02:00
|
|
|
match t.sty {
|
2015-07-20 22:13:36 +03:00
|
|
|
ty::TyStruct(def, substs) => {
|
2014-08-06 11:59:40 +02:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
// First get the size of all statically known fields.
|
|
|
|
// Don't use type_of::sizing_type_of because that expects t to be sized.
|
2015-08-06 18:25:15 +03:00
|
|
|
assert!(!t.is_simd());
|
2014-08-06 11:59:40 +02:00
|
|
|
let repr = adt::represent_type(ccx, t);
|
2016-02-09 21:24:11 +01:00
|
|
|
let sizing_type = adt::sizing_type_context_of(ccx, &repr, true);
|
2016-02-18 19:49:45 +02:00
|
|
|
debug!("DST {} sizing_type: {:?}", t, sizing_type);
|
2015-07-27 14:45:20 +02:00
|
|
|
let sized_size = llsize_of_alloc(ccx, sizing_type.prefix());
|
|
|
|
let sized_align = llalign_of_min(ccx, sizing_type.prefix());
|
|
|
|
debug!("DST {} statically sized prefix size: {} align: {}",
|
|
|
|
t, sized_size, sized_align);
|
|
|
|
let sized_size = C_uint(ccx, sized_size);
|
|
|
|
let sized_align = C_uint(ccx, sized_align);
|
2014-08-06 11:59:40 +02:00
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
|
|
|
// the last field).
|
2015-08-02 22:52:50 +03:00
|
|
|
let last_field = def.struct_variant().fields.last().unwrap();
|
|
|
|
let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
|
2014-08-06 11:59:40 +02:00
|
|
|
let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
|
|
|
|
|
2015-07-29 19:43:26 +02:00
|
|
|
// FIXME (#26403, #27023): We should be adding padding
|
2015-07-27 14:45:20 +02:00
|
|
|
// to `sized_size` (to accommodate the `unsized_align`
|
|
|
|
// required of the unsized field that follows) before
|
2015-07-29 19:43:26 +02:00
|
|
|
// summing it with `sized_size`. (Note that since #26403
|
|
|
|
// is unfixed, we do not yet add the necessary padding
|
|
|
|
// here. But this is where the add would go.)
|
2015-07-27 14:45:20 +02:00
|
|
|
|
2014-08-06 11:59:40 +02:00
|
|
|
// Return the sum of sizes and max of aligns.
|
2016-03-09 14:20:22 +02:00
|
|
|
let mut size = bcx.add(sized_size, unsized_size);
|
2015-07-27 14:45:20 +02:00
|
|
|
|
|
|
|
// Issue #27023: If there is a drop flag, *now* we add 1
|
|
|
|
// to the size. (We can do this without adding any
|
|
|
|
// padding because drop flags do not have any alignment
|
|
|
|
// constraints.)
|
|
|
|
if sizing_type.needs_drop_flag() {
|
2016-03-09 14:20:22 +02:00
|
|
|
size = bcx.add(size, C_uint(bcx.ccx(), 1_u64));
|
2015-07-27 14:45:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 16:53:57 +02:00
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
2015-12-07 02:38:29 +13:00
|
|
|
let align = match (const_to_opt_uint(sized_align), const_to_opt_uint(unsized_align)) {
|
|
|
|
(Some(sized_align), Some(unsized_align)) => {
|
|
|
|
// If both alignments are constant, (the sized_align should always be), then
|
|
|
|
// pick the correct alignment statically.
|
|
|
|
C_uint(ccx, std::cmp::max(sized_align, unsized_align))
|
|
|
|
}
|
2016-03-09 14:20:22 +02:00
|
|
|
_ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
|
|
|
|
sized_align,
|
|
|
|
unsized_align)
|
2015-12-07 02:38:29 +13:00
|
|
|
};
|
2015-07-27 14:45:20 +02:00
|
|
|
|
2015-07-27 16:53:57 +02:00
|
|
|
// Issue #27023: must add any necessary padding to `size`
|
|
|
|
// (to make it a multiple of `align`) before returning it.
|
|
|
|
//
|
|
|
|
// Namely, the returned size should be, in C notation:
|
|
|
|
//
|
|
|
|
// `size + ((size & (align-1)) ? align : 0)`
|
|
|
|
//
|
2015-07-29 22:18:39 +02:00
|
|
|
// emulated via the semi-standard fast bit trick:
|
2015-07-27 16:53:57 +02:00
|
|
|
//
|
2015-12-07 02:38:29 +13:00
|
|
|
// `(size + (align-1)) & -align`
|
2015-07-29 22:18:39 +02:00
|
|
|
|
2016-03-09 14:20:22 +02:00
|
|
|
let addend = bcx.sub(align, C_uint(bcx.ccx(), 1_u64));
|
|
|
|
let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
|
2015-07-27 14:45:20 +02:00
|
|
|
|
2014-08-06 11:59:40 +02:00
|
|
|
(size, align)
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyTrait(..) => {
|
2014-08-06 11:59:40 +02:00
|
|
|
// info points to the vtable and the second entry in the vtable is the
|
|
|
|
// dynamic size of the object.
|
2016-03-09 14:20:22 +02:00
|
|
|
let info = bcx.pointercast(info, Type::int(bcx.ccx()).ptr_to());
|
|
|
|
let size_ptr = bcx.gepi(info, &[1]);
|
|
|
|
let align_ptr = bcx.gepi(info, &[2]);
|
|
|
|
(bcx.load(size_ptr), bcx.load(align_ptr))
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
2015-06-12 16:50:13 -07:00
|
|
|
ty::TySlice(_) | ty::TyStr => {
|
2015-06-24 08:24:13 +03:00
|
|
|
let unit_ty = t.sequence_element_type(bcx.tcx());
|
2015-01-15 06:54:51 +00:00
|
|
|
// The info in this case is the length of the str, so the size is that
|
2014-08-06 11:59:40 +02:00
|
|
|
// times the unit size.
|
|
|
|
let llunit_ty = sizing_type_of(bcx.ccx(), unit_ty);
|
2015-01-15 06:54:51 +00:00
|
|
|
let unit_align = llalign_of_min(bcx.ccx(), llunit_ty);
|
2014-08-06 11:59:40 +02:00
|
|
|
let unit_size = llsize_of_alloc(bcx.ccx(), llunit_ty);
|
2016-03-09 14:20:22 +02:00
|
|
|
(bcx.mul(info, C_uint(bcx.ccx(), unit_size)),
|
2014-12-11 13:53:30 +01:00
|
|
|
C_uint(bcx.ccx(), unit_align))
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("Unexpected unsized type, found {}", t)
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:52:08 +02:00
|
|
|
fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueKind<'tcx>)
|
2014-09-06 19:13:04 +03:00
|
|
|
-> Block<'blk, 'tcx> {
|
2016-04-21 15:36:57 -04:00
|
|
|
let t = g.ty();
|
|
|
|
|
2015-04-22 11:52:08 +02:00
|
|
|
let skip_dtor = match g { DropGlueKind::Ty(_) => false, DropGlueKind::TyContents(_) => true };
|
2012-08-28 15:54:45 -07:00
|
|
|
// NB: v0 is an *alias* of type t here, not a direct value.
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("make_drop_glue");
|
2015-02-10 10:04:39 +01:00
|
|
|
|
|
|
|
// Only drop the value when it ... well, we used to check for
|
|
|
|
// non-null, (and maybe we need to continue doing so), but we now
|
|
|
|
// must definitely check for special bit-patterns corresponding to
|
|
|
|
// the special dtor markings.
|
|
|
|
|
2014-10-31 10:51:16 +02:00
|
|
|
match t.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBox(content_ty) => {
|
|
|
|
// Support for TyBox is built-in and its drop glue is
|
2015-04-22 11:52:08 +02:00
|
|
|
// special. It may move to library and have Drop impl. As
|
2015-06-11 16:21:46 -07:00
|
|
|
// a safe-guard, assert TyBox not used with TyContents.
|
2015-04-22 11:52:08 +02:00
|
|
|
assert!(!skip_dtor);
|
2015-03-14 02:36:41 +02:00
|
|
|
if !type_is_sized(bcx.tcx(), content_ty) {
|
2016-08-16 17:41:38 +03:00
|
|
|
let llval = get_dataptr(bcx, v0);
|
2015-03-14 02:36:41 +02:00
|
|
|
let llbox = Load(bcx, llval);
|
2016-08-16 17:41:38 +03:00
|
|
|
let bcx = drop_ty(bcx, v0, content_ty, DebugLoc::None);
|
|
|
|
let info = get_meta(bcx, v0);
|
|
|
|
let info = Load(bcx, info);
|
|
|
|
let (llsize, llalign) =
|
|
|
|
size_and_align_of_dst(&bcx.build(), content_ty, info);
|
|
|
|
|
|
|
|
// `Box<ZeroSizeType>` does not allocate.
|
|
|
|
let needs_free = ICmp(bcx,
|
|
|
|
llvm::IntNE,
|
|
|
|
llsize,
|
|
|
|
C_uint(bcx.ccx(), 0u64),
|
|
|
|
DebugLoc::None);
|
|
|
|
with_cond(bcx, needs_free, |bcx| {
|
|
|
|
trans_exchange_free_dyn(bcx, llbox, llsize, llalign, DebugLoc::None)
|
2015-03-14 02:36:41 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let llval = v0;
|
|
|
|
let llbox = Load(bcx, llval);
|
2016-08-16 17:41:38 +03:00
|
|
|
let bcx = drop_ty(bcx, llbox, content_ty, DebugLoc::None);
|
|
|
|
trans_exchange_free_ty(bcx, llbox, content_ty, DebugLoc::None)
|
2014-04-09 19:15:31 +12:00
|
|
|
}
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
2015-08-25 20:50:30 +03:00
|
|
|
ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
|
2015-08-25 21:52:15 +03:00
|
|
|
match (def.dtor_kind(), skip_dtor) {
|
2016-08-16 17:41:38 +03:00
|
|
|
(ty::TraitDtor(_), false) => {
|
2015-08-25 20:50:30 +03:00
|
|
|
trans_struct_drop(bcx, t, v0)
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
2015-04-22 11:52:08 +02:00
|
|
|
(ty::NoDtor, _) | (_, true) => {
|
2014-01-27 14:18:36 +02:00
|
|
|
// No dtor? Just the default case
|
2014-12-11 13:53:30 +01:00
|
|
|
iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyTrait(..) => {
|
2015-04-22 11:52:08 +02:00
|
|
|
// No support in vtable for distinguishing destroying with
|
|
|
|
// versus without calling Drop::drop. Assert caller is
|
|
|
|
// okay with always calling the Drop impl, if any.
|
|
|
|
assert!(!skip_dtor);
|
2016-08-16 17:41:38 +03:00
|
|
|
let data_ptr = get_dataptr(bcx, v0);
|
|
|
|
let vtable_ptr = Load(bcx, get_meta(bcx, v0));
|
2015-03-14 02:36:41 +02:00
|
|
|
let dtor = Load(bcx, vtable_ptr);
|
2014-08-06 11:59:40 +02:00
|
|
|
Call(bcx,
|
|
|
|
dtor,
|
2015-03-14 02:36:41 +02:00
|
|
|
&[PointerCast(bcx, Load(bcx, data_ptr), Type::i8p(bcx.ccx()))],
|
2014-12-11 13:53:30 +01:00
|
|
|
DebugLoc::None);
|
2014-08-06 11:59:40 +02:00
|
|
|
bcx
|
2015-03-14 02:36:41 +02:00
|
|
|
}
|
2014-01-27 14:18:36 +02:00
|
|
|
_ => {
|
2015-03-14 02:36:41 +02:00
|
|
|
if bcx.fcx.type_needs_drop(t) {
|
2014-12-11 13:53:30 +01:00
|
|
|
iter_structural_ty(bcx,
|
|
|
|
v0,
|
|
|
|
t,
|
|
|
|
|bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
|
2014-01-27 14:18:36 +02:00
|
|
|
} else {
|
|
|
|
bcx
|
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2013-07-13 03:25:46 +02:00
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|