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
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2013-02-25 14:11:21 -05:00
|
|
|
use back::link::*;
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm;
|
2015-02-28 23:55:50 +02:00
|
|
|
use llvm::{ValueRef, get_param};
|
|
|
|
use metadata::csearch;
|
2015-08-16 06:32:28 -04:00
|
|
|
use middle::def_id::{DefId, LOCAL_CRATE};
|
2014-10-01 01:26:04 +03:00
|
|
|
use middle::lang_items::ExchangeFreeFnLangItem;
|
2014-05-13 11:35:42 -04:00
|
|
|
use middle::subst;
|
2014-11-06 09:24:44 +02:00
|
|
|
use middle::subst::{Subst, Substs};
|
2015-02-28 23:55:50 +02:00
|
|
|
use middle::ty::{self, Ty};
|
2015-06-29 16:07:45 -07:00
|
|
|
use trans::adt;
|
2015-07-09 09:50:08 -07:00
|
|
|
use trans::adt::GetDtorType; // for tcx.dtor_type()
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::base::*;
|
|
|
|
use trans::build::*;
|
|
|
|
use trans::callee;
|
|
|
|
use trans::cleanup;
|
|
|
|
use trans::cleanup::CleanupMethods;
|
|
|
|
use trans::common::*;
|
2014-12-11 13:53:30 +01:00
|
|
|
use trans::debuginfo::DebugLoc;
|
2015-03-04 01:08:06 +02:00
|
|
|
use trans::declare;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::expr;
|
2015-02-28 23:55:50 +02:00
|
|
|
use trans::foreign;
|
|
|
|
use trans::inline;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::machine::*;
|
2015-02-28 23:55:50 +02:00
|
|
|
use trans::monomorphize;
|
2015-03-20 15:09:00 +02:00
|
|
|
use trans::type_of::{type_of, type_of_dtor, sizing_type_of, align_of};
|
2015-02-28 23:55:50 +02:00
|
|
|
use trans::type_::Type;
|
2013-06-16 22:52:44 +12:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
use arena::TypedArena;
|
2014-02-26 12:58:41 -05:00
|
|
|
use libc::c_uint;
|
2013-02-25 14:11:21 -05:00
|
|
|
use syntax::ast;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2015-02-04 17:16:59 +01:00
|
|
|
pub fn trans_exchange_free_dyn<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
|
|
|
|
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");
|
2014-05-21 00:18:10 -04:00
|
|
|
let ccx = cx.ccx();
|
2013-06-16 15:45:48 +12:00
|
|
|
callee::trans_lang_call(cx,
|
2013-07-15 20:42:13 -07:00
|
|
|
langcall(cx, None, "", ExchangeFreeFnLangItem),
|
2014-11-17 21:39:01 +13:00
|
|
|
&[PointerCast(cx, v, Type::i8p(ccx)), size, align],
|
2015-02-04 17:16:59 +01:00
|
|
|
Some(expr::Ignore),
|
|
|
|
debug_loc).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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
t: Ty<'tcx>) -> Ty<'tcx> {
|
2014-03-15 22:29:34 +02:00
|
|
|
let tcx = ccx.tcx();
|
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) {
|
2014-08-06 11:59:40 +02:00
|
|
|
return t
|
|
|
|
}
|
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.)
|
|
|
|
|
2014-12-16 15:00:05 -05: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 {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBox(typ) if !type_needs_drop(tcx, typ)
|
2014-12-18 09:26:10 -05:00
|
|
|
&& type_is_sized(tcx, typ) => {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 14:20:11 +02:00
|
|
|
let llty = sizing_type_of(ccx, typ);
|
|
|
|
// `Box<ZeroSizeType>` does not allocate.
|
|
|
|
if llsize_of_alloc(ccx, llty) == 0 {
|
2014-12-25 07:20:48 -05:00
|
|
|
tcx.types.i8
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 14:20:11 +02:00
|
|
|
} else {
|
2014-09-04 23:58:39 -04:00
|
|
|
t
|
2014-05-21 00:18:10 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 01:07:06 -05:00
|
|
|
_ => t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
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
|
|
|
drop_ty_core(bcx, v, t, debug_loc, false, None)
|
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,
|
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
|
|
|
skip_dtor: bool,
|
|
|
|
drop_hint: Option<cleanup::DropHintValue>)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2012-08-28 15:54:45 -07:00
|
|
|
// NB: v is an *alias* of type t here, not a direct value.
|
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
|
|
|
debug!("drop_ty_core(t={:?}, skip_dtor={} drop_hint={:?})", t, skip_dtor, drop_hint);
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("drop_ty");
|
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
|
|
|
let mut bcx = bcx;
|
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);
|
2014-02-06 01:07:06 -05:00
|
|
|
let glue_type = get_drop_glue_type(ccx, t);
|
|
|
|
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
|
|
|
|
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
|
|
|
match drop_hint {
|
|
|
|
Some(drop_hint) => {
|
|
|
|
let hint_val = load_ty(bcx, drop_hint.value(), bcx.tcx().types.u8);
|
|
|
|
let moved_val =
|
|
|
|
C_integral(Type::i8(bcx.ccx()), adt::DTOR_MOVED_HINT as u64, false);
|
|
|
|
let may_need_drop =
|
|
|
|
ICmp(bcx, llvm::IntNE, hint_val, moved_val, DebugLoc::None);
|
|
|
|
bcx = with_cond(bcx, may_need_drop, |cx| {
|
|
|
|
Call(cx, glue, &[ptr], None, debug_loc);
|
|
|
|
cx
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// No drop-hint ==> call standard drop glue
|
|
|
|
Call(bcx, glue, &[ptr], None, 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");
|
2013-09-30 22:40:44 -04:00
|
|
|
let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
|
2015-01-08 16:28:07 +01:00
|
|
|
store_ty(bcx, v, vp, t);
|
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
|
|
|
drop_ty_core(bcx, vp, t, debug_loc, skip_dtor, None)
|
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,
|
|
|
|
/// if Ty is Newtype(S) then only the Drop impl for for Newtype
|
|
|
|
/// itself will be skipped, while the Drop impl for S, if any,
|
|
|
|
/// will be invoked.
|
|
|
|
TyContents(Ty<'tcx>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> DropGlueKind<'tcx> {
|
|
|
|
fn ty(&self) -> Ty<'tcx> {
|
|
|
|
match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
|
|
|
|
{
|
|
|
|
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 {
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("make drop glue for {:?}", g);
|
2015-04-22 11:52:08 +02:00
|
|
|
let g = g.map_ty(|t| get_drop_glue_type(ccx, t));
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("drop glue type {:?}", g);
|
2015-04-22 11:52:08 +02:00
|
|
|
match ccx.drop_glues().borrow().get(&g) {
|
2014-03-15 22:29:34 +02:00
|
|
|
Some(&glue) => return glue,
|
|
|
|
_ => { }
|
2012-09-12 14:48:13 -07:00
|
|
|
}
|
2015-04-22 11:52:08 +02:00
|
|
|
let t = g.ty();
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2014-12-18 09:26:10 -05:00
|
|
|
let llty = if type_is_sized(ccx.tcx(), t) {
|
2014-08-06 11:59:40 +02:00
|
|
|
type_of(ccx, t).ptr_to()
|
|
|
|
} else {
|
2015-06-25 04:09:46 +03:00
|
|
|
type_of(ccx, ccx.tcx().mk_box(t)).ptr_to()
|
2014-08-06 11:59:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let llfnty = Type::glue_fn(ccx, llty);
|
2014-07-31 16:45:29 -07:00
|
|
|
|
2015-03-15 04:01:57 +02:00
|
|
|
// To avoid infinite recursion, don't `make_drop_glue` until after we've
|
|
|
|
// added the entry to the `drop_glues` cache.
|
2015-04-22 11:52:08 +02:00
|
|
|
if let Some(old_sym) = ccx.available_drop_glues().borrow().get(&g) {
|
2015-06-25 04:09:46 +03:00
|
|
|
let llfn = declare::declare_cfn(ccx, &old_sym, llfnty, ccx.tcx().mk_nil());
|
2015-04-22 11:52:08 +02:00
|
|
|
ccx.drop_glues().borrow_mut().insert(g, llfn);
|
2015-03-15 04:01:57 +02:00
|
|
|
return llfn;
|
2014-07-31 16:45:29 -07:00
|
|
|
};
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2015-03-15 04:01:57 +02:00
|
|
|
let fn_nm = mangle_internal_name_by_type_and_seq(ccx, t, "drop");
|
2015-06-25 04:09:46 +03:00
|
|
|
let llfn = declare::define_cfn(ccx, &fn_nm, llfnty, ccx.tcx().mk_nil()).unwrap_or_else(||{
|
2015-03-20 15:09:00 +02:00
|
|
|
ccx.sess().bug(&format!("symbol `{}` already defined", fn_nm));
|
|
|
|
});
|
2015-04-22 11:52:08 +02:00
|
|
|
ccx.available_drop_glues().borrow_mut().insert(g, fn_nm);
|
2014-01-11 16:39:32 +02:00
|
|
|
|
2015-06-18 20:25:05 +03:00
|
|
|
let _s = StatRecorder::new(ccx, format!("drop {:?}", t));
|
2015-03-15 04:01:57 +02:00
|
|
|
|
|
|
|
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
|
|
|
|
let (arena, fcx): (TypedArena<_>, FunctionContext);
|
|
|
|
arena = TypedArena::new();
|
|
|
|
fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, false,
|
2015-06-25 04:09:46 +03:00
|
|
|
ty::FnConverging(ccx.tcx().mk_nil()),
|
2015-03-15 04:01:57 +02:00
|
|
|
empty_substs, None, &arena);
|
|
|
|
|
2015-06-25 04:09:46 +03:00
|
|
|
let bcx = init_function(&fcx, false, ty::FnConverging(ccx.tcx().mk_nil()));
|
2015-03-15 04:01:57 +02:00
|
|
|
|
|
|
|
update_linkage(ccx, llfn, None, OriginalTranslation);
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 23:57:40 +02:00
|
|
|
let llrawptr0 = get_param(llfn, fcx.arg_offset() as c_uint);
|
2015-04-22 11:52:08 +02:00
|
|
|
let bcx = make_drop_glue(bcx, llrawptr0, g);
|
2015-06-25 04:09:46 +03:00
|
|
|
finish_fn(&fcx, bcx, ty::FnConverging(ccx.tcx().mk_nil()), DebugLoc::None);
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2015-03-15 04:01:57 +02:00
|
|
|
llfn
|
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_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
t: Ty<'tcx>,
|
2015-04-20 10:52:26 +12:00
|
|
|
struct_data: ValueRef,
|
2015-08-16 06:32:28 -04:00
|
|
|
dtor_did: DefId,
|
|
|
|
class_did: DefId,
|
2014-09-29 22:11:30 +03:00
|
|
|
substs: &subst::Substs<'tcx>)
|
2014-09-06 19:13:04 +03:00
|
|
|
-> Block<'blk, 'tcx> {
|
2015-04-20 10:52:26 +12:00
|
|
|
assert!(type_is_sized(bcx.tcx(), t), "Precondition: caller must ensure t is sized");
|
|
|
|
|
2013-02-24 23:20:08 -08:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), t);
|
2014-08-06 11:59:40 +02:00
|
|
|
let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &*repr, struct_data));
|
2015-02-10 10:04:39 +01:00
|
|
|
let loaded = load_ty(bcx, drop_flag.val, bcx.tcx().dtor_type());
|
|
|
|
let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
|
|
|
|
let init_val = C_integral(drop_flag_llty, adt::DTOR_NEEDED as u64, false);
|
|
|
|
|
2015-03-25 11:57:55 +01:00
|
|
|
let bcx = if !bcx.ccx().check_drop_flag_for_sanity() {
|
2015-02-10 10:04:39 +01:00
|
|
|
bcx
|
|
|
|
} else {
|
|
|
|
let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
|
|
|
|
let done_val = C_integral(drop_flag_llty, adt::DTOR_DONE as u64, false);
|
|
|
|
let not_init = ICmp(bcx, llvm::IntNE, loaded, init_val, DebugLoc::None);
|
|
|
|
let not_done = ICmp(bcx, llvm::IntNE, loaded, done_val, DebugLoc::None);
|
|
|
|
let drop_flag_neither_initialized_nor_cleared =
|
|
|
|
And(bcx, not_init, not_done, DebugLoc::None);
|
|
|
|
with_cond(bcx, drop_flag_neither_initialized_nor_cleared, |cx| {
|
|
|
|
let llfn = cx.ccx().get_intrinsic(&("llvm.debugtrap"));
|
|
|
|
Call(cx, llfn, &[], None, DebugLoc::None);
|
|
|
|
cx
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let drop_flag_dtor_needed = ICmp(bcx, llvm::IntEQ, loaded, init_val, DebugLoc::None);
|
|
|
|
with_cond(bcx, drop_flag_dtor_needed, |cx| {
|
2015-04-20 10:52:26 +12:00
|
|
|
trans_struct_drop(cx, t, struct_data, dtor_did, class_did, substs)
|
2013-11-21 15:42:55 -08:00
|
|
|
})
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2015-02-28 23:55:50 +02:00
|
|
|
pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId,
|
|
|
|
parent_id: DefId,
|
2015-02-28 23:55:50 +02:00
|
|
|
substs: &Substs<'tcx>)
|
|
|
|
-> ValueRef {
|
|
|
|
let _icx = push_ctxt("trans_res_dtor");
|
|
|
|
let did = inline::maybe_instantiate_inline(ccx, did);
|
|
|
|
|
|
|
|
if !substs.types.is_empty() {
|
2015-08-16 06:32:28 -04:00
|
|
|
assert_eq!(did.krate, LOCAL_CRATE);
|
2015-02-28 23:55:50 +02:00
|
|
|
|
|
|
|
// Since we're in trans we don't care for any region parameters
|
|
|
|
let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone()));
|
|
|
|
|
|
|
|
let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None);
|
|
|
|
|
|
|
|
val
|
2015-08-16 09:06:23 -04:00
|
|
|
} else if did.is_local() {
|
2015-02-28 23:55:50 +02:00
|
|
|
get_item_val(ccx, did.node)
|
|
|
|
} else {
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
let name = csearch::get_symbol(&ccx.sess().cstore, did);
|
2015-06-25 23:42:17 +03:00
|
|
|
let class_ty = tcx.lookup_item_type(parent_id).ty.subst(tcx, substs);
|
2015-02-28 23:55:50 +02:00
|
|
|
let llty = type_of_dtor(ccx, class_ty);
|
2015-03-04 01:08:06 +02:00
|
|
|
foreign::get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), &name[..], llvm::CCallConv,
|
2015-08-08 15:32:35 +02:00
|
|
|
llty, ccx.tcx().mk_nil())
|
2015-02-28 23:55:50 +02: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>,
|
2014-09-06 19:13:04 +03:00
|
|
|
v0: ValueRef,
|
2015-08-16 06:32:28 -04:00
|
|
|
dtor_did: DefId,
|
|
|
|
class_did: DefId,
|
2014-09-29 22:11:30 +03:00
|
|
|
substs: &subst::Substs<'tcx>)
|
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);
|
2013-06-23 14:41:45 +12:00
|
|
|
|
|
|
|
// Find and call the actual destructor
|
2015-08-08 15:32:35 +02:00
|
|
|
let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, class_did, substs);
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2015-04-24 09:29:56 +02:00
|
|
|
// Class dtors have no explicit args, so the params should
|
|
|
|
// just consist of the environment (self).
|
2013-06-23 14:41:45 +12:00
|
|
|
let params = unsafe {
|
|
|
|
let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
|
|
|
|
ty.element_type().func_params()
|
|
|
|
};
|
2015-07-03 12:19:36 +02:00
|
|
|
assert_eq!(params.len(), if type_is_sized(bcx.tcx(), t) { 1 } else { 2 });
|
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.
|
2015-04-28 16:40:02 +02:00
|
|
|
bcx.fcx.schedule_drop_adt_contents(cleanup::CustomScope(contents_scope), v0, t);
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2015-04-24 09:29:56 +02:00
|
|
|
let glue_type = get_drop_glue_type(bcx.ccx(), t);
|
2015-06-25 04:09:46 +03:00
|
|
|
let dtor_ty = bcx.tcx().mk_ctor_fn(class_did, &[glue_type], bcx.tcx().mk_nil());
|
2015-07-03 12:19:36 +02:00
|
|
|
let (_, bcx) = if type_is_sized(bcx.tcx(), t) {
|
|
|
|
invoke(bcx, dtor_addr, &[v0], dtor_ty, DebugLoc::None)
|
|
|
|
} else {
|
2015-08-24 21:50:50 +02:00
|
|
|
let args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
|
2015-07-03 12:19:36 +02:00
|
|
|
invoke(bcx, dtor_addr, &args, dtor_ty, DebugLoc::None)
|
|
|
|
};
|
2014-01-15 14:39:08 -05: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
|
|
|
|
2015-04-15 11:57:29 +12:00
|
|
|
pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, info: ValueRef)
|
|
|
|
-> (ValueRef, ValueRef) {
|
2014-08-06 11:59:40 +02:00
|
|
|
debug!("calculate size of DST: {}; with lost info: {}",
|
2015-06-18 20:25:05 +03:00
|
|
|
t, bcx.val_to_string(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);
|
|
|
|
debug!("size_and_align_of_dst t={} info={} size: {} align: {}",
|
|
|
|
t, bcx.val_to_string(info), size, align);
|
|
|
|
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);
|
|
|
|
}
|
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);
|
2015-07-27 14:45:20 +02:00
|
|
|
let sizing_type = adt::sizing_type_context_of(ccx, &*repr, true);
|
|
|
|
debug!("DST {} sizing_type: {}", t, sizing_type.to_string());
|
|
|
|
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-27 16:53:57 +02:00
|
|
|
let dbloc = DebugLoc::None;
|
|
|
|
|
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.
|
2015-07-27 16:53:57 +02:00
|
|
|
let mut size = Add(bcx, sized_size, unsized_size, dbloc);
|
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() {
|
2015-07-27 16:53:57 +02:00
|
|
|
size = Add(bcx, size, C_uint(bcx.ccx(), 1_u64), dbloc);
|
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).
|
2014-08-06 11:59:40 +02:00
|
|
|
let align = Select(bcx,
|
2015-02-04 17:42:32 +01:00
|
|
|
ICmp(bcx,
|
2015-07-27 16:53:57 +02:00
|
|
|
llvm::IntUGT,
|
2015-02-04 17:42:32 +01:00
|
|
|
sized_align,
|
|
|
|
unsized_align,
|
2015-07-27 16:53:57 +02:00
|
|
|
dbloc),
|
2014-08-06 11:59:40 +02:00
|
|
|
sized_align,
|
|
|
|
unsized_align);
|
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-07-29 22:18:39 +02:00
|
|
|
// `(size + (align-1)) & !align`
|
|
|
|
|
|
|
|
let addend = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
|
|
|
|
let size = And(
|
|
|
|
bcx, Add(bcx, size, addend, dbloc), Neg(bcx, align, dbloc), dbloc);
|
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.
|
|
|
|
let info = PointerCast(bcx, info, Type::int(bcx.ccx()).ptr_to());
|
2015-01-25 10:58:43 +00:00
|
|
|
let size_ptr = GEPi(bcx, info, &[1]);
|
|
|
|
let align_ptr = GEPi(bcx, info, &[2]);
|
2014-08-06 11:59:40 +02:00
|
|
|
(Load(bcx, size_ptr), Load(bcx, align_ptr))
|
|
|
|
}
|
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);
|
2014-12-11 13:53:30 +01:00
|
|
|
(Mul(bcx, info, C_uint(bcx.ccx(), unit_size), DebugLoc::None),
|
|
|
|
C_uint(bcx.ccx(), unit_align))
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
2015-06-18 20:25:05 +03:00
|
|
|
_ => bcx.sess().bug(&format!("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> {
|
2015-04-22 11:52:08 +02:00
|
|
|
let t = g.ty();
|
|
|
|
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.
|
|
|
|
|
|
|
|
let inttype = Type::int(bcx.ccx());
|
|
|
|
let dropped_pattern = C_integral(inttype, adt::dtor_done_usize(bcx.fcx.ccx) as u64, false);
|
|
|
|
|
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) {
|
2015-08-24 22:05:20 +02:00
|
|
|
let llval = expr::get_dataptr(bcx, v0);
|
2015-03-14 02:36:41 +02:00
|
|
|
let llbox = Load(bcx, llval);
|
2015-02-10 10:04:39 +01:00
|
|
|
let llbox_as_usize = PtrToInt(bcx, llbox, Type::int(bcx.ccx()));
|
|
|
|
let drop_flag_not_dropped_already =
|
|
|
|
ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
|
|
|
|
with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
|
2015-03-14 02:36:41 +02:00
|
|
|
let bcx = drop_ty(bcx, v0, content_ty, DebugLoc::None);
|
2015-08-24 22:05:20 +02:00
|
|
|
let info = expr::get_meta(bcx, v0);
|
2015-03-14 02:36:41 +02:00
|
|
|
let info = Load(bcx, info);
|
|
|
|
let (llsize, llalign) = size_and_align_of_dst(bcx, 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| {
|
2015-02-04 17:16:59 +01:00
|
|
|
trans_exchange_free_dyn(bcx, llbox, llsize, llalign, DebugLoc::None)
|
2014-08-06 11:59:40 +02:00
|
|
|
})
|
2015-03-14 02:36:41 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let llval = v0;
|
|
|
|
let llbox = Load(bcx, llval);
|
2015-02-10 10:04:39 +01:00
|
|
|
let llbox_as_usize = PtrToInt(bcx, llbox, inttype);
|
|
|
|
let drop_flag_not_dropped_already =
|
|
|
|
ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
|
|
|
|
with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
|
2015-03-14 02:36:41 +02: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-07-20 22:13:36 +03:00
|
|
|
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
|
2014-01-27 14:18:36 +02:00
|
|
|
let tcx = bcx.tcx();
|
2015-07-20 22:13:36 +03:00
|
|
|
match (tcx.ty_dtor(def.did), skip_dtor) {
|
2015-04-22 11:52:08 +02:00
|
|
|
(ty::TraitDtor(dtor, true), false) => {
|
2014-08-06 11:59:40 +02:00
|
|
|
// FIXME(16758) Since the struct is unsized, it is hard to
|
|
|
|
// find the drop flag (which is at the end of the struct).
|
|
|
|
// Lets just ignore the flag and pretend everything will be
|
|
|
|
// OK.
|
2014-12-18 09:26:10 -05:00
|
|
|
if type_is_sized(bcx.tcx(), t) {
|
2015-07-20 22:13:36 +03:00
|
|
|
trans_struct_drop_flag(bcx, t, v0, dtor, def.did, substs)
|
2014-08-06 11:59:40 +02:00
|
|
|
} else {
|
|
|
|
// Give the user a heads up that we are doing something
|
|
|
|
// stupid and dangerous.
|
2015-01-07 11:58:31 -05:00
|
|
|
bcx.sess().warn(&format!("Ignoring drop flag in destructor for {}\
|
2014-08-06 11:59:40 +02:00
|
|
|
because the struct is unsized. See issue\
|
2015-06-18 20:25:05 +03:00
|
|
|
#16758", t));
|
2015-07-20 22:13:36 +03:00
|
|
|
trans_struct_drop(bcx, t, v0, dtor, def.did, substs)
|
2014-08-06 11:59:40 +02:00
|
|
|
}
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
2015-04-22 11:52:08 +02:00
|
|
|
(ty::TraitDtor(dtor, false), false) => {
|
2015-07-20 22:13:36 +03:00
|
|
|
trans_struct_drop(bcx, t, v0, dtor, def.did, substs)
|
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);
|
2015-08-24 22:05:20 +02:00
|
|
|
let data_ptr = expr::get_dataptr(bcx, v0);
|
|
|
|
let vtable_ptr = Load(bcx, expr::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
|
|
|
None,
|
|
|
|
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
|
|
|
}
|