1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 // Code relating to drop glue.
18 use llvm::{ValueRef, get_param};
19 use middle::lang_items::ExchangeFreeFnLangItem;
20 use rustc::ty::subst::{Substs};
22 use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
24 use adt::GetDtorType; // for tcx.dtor_type()
27 use callee::{Callee, ArgVals};
29 use cleanup::CleanupMethods;
31 use debuginfo::DebugLoc;
35 use trans_item::TransItem;
36 use type_of::{type_of, sizing_type_of, align_of};
40 use arena::TypedArena;
41 use syntax_pos::DUMMY_SP;
43 pub fn trans_exchange_free_dyn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
48 -> Block<'blk, 'tcx> {
49 let _icx = push_ctxt("trans_exchange_free");
51 let def_id = langcall(bcx.tcx(), None, "", ExchangeFreeFnLangItem);
52 let args = [PointerCast(bcx, v, Type::i8p(bcx.ccx())), size, align];
53 Callee::def(bcx.ccx(), def_id, bcx.tcx().mk_substs(Substs::empty()))
54 .call(bcx, debug_loc, ArgVals(&args), None).bcx
57 pub fn trans_exchange_free<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
62 -> Block<'blk, 'tcx> {
63 trans_exchange_free_dyn(cx,
65 C_uint(cx.ccx(), size),
66 C_uint(cx.ccx(), align),
70 pub fn trans_exchange_free_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
74 -> Block<'blk, 'tcx> {
75 assert!(type_is_sized(bcx.ccx().tcx(), content_ty));
76 let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
77 let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
79 // `Box<ZeroSizeType>` does not allocate.
80 if content_size != 0 {
81 let content_align = align_of(bcx.ccx(), content_ty);
82 trans_exchange_free(bcx, ptr, content_size, content_align, debug_loc)
88 pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
89 ty: Ty<'tcx>) -> bool {
90 tcx.type_needs_drop_given_env(ty, &tcx.empty_parameter_environment())
93 pub fn get_drop_glue_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
94 t: Ty<'tcx>) -> Ty<'tcx> {
95 assert!(t.is_normalized_for_trans());
97 // Even if there is no dtor for t, there might be one deeper down and we
98 // might need to pass in the vtable ptr.
99 if !type_is_sized(tcx, t) {
103 // FIXME (#22815): note that type_needs_drop conservatively
104 // approximates in some cases and may say a type expression
105 // requires drop glue when it actually does not.
107 // (In this case it is not clear whether any harm is done, i.e.
108 // erroneously returning `t` in some cases where we could have
109 // returned `tcx.types.i8` does not appear unsound. The impact on
110 // code quality is unknown at this time.)
112 if !type_needs_drop(tcx, t) {
116 ty::TyBox(typ) if !type_needs_drop(tcx, typ)
117 && type_is_sized(tcx, typ) => {
118 tcx.normalizing_infer_ctxt(traits::ProjectionMode::Any).enter(|infcx| {
119 let layout = t.layout(&infcx).unwrap();
120 if layout.size(&tcx.data_layout).bytes() == 0 {
121 // `Box<ZeroSizeType>` does not allocate.
132 pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
135 debug_loc: DebugLoc) -> Block<'blk, 'tcx> {
136 drop_ty_core(bcx, v, t, debug_loc, false, None)
139 pub fn drop_ty_core<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
144 drop_hint: Option<cleanup::DropHintValue>)
145 -> Block<'blk, 'tcx> {
146 // NB: v is an *alias* of type t here, not a direct value.
147 debug!("drop_ty_core(t={:?}, skip_dtor={} drop_hint={:?})", t, skip_dtor, drop_hint);
148 let _icx = push_ctxt("drop_ty");
150 if bcx.fcx.type_needs_drop(t) {
152 let g = if skip_dtor {
153 DropGlueKind::TyContents(t)
157 let glue = get_drop_glue_core(ccx, g);
158 let glue_type = get_drop_glue_type(ccx.tcx(), t);
159 let ptr = if glue_type != t {
160 PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
167 let hint_val = load_ty(bcx, drop_hint.value(), bcx.tcx().types.u8);
169 C_integral(Type::i8(bcx.ccx()), adt::DTOR_MOVED_HINT as u64, false);
171 ICmp(bcx, llvm::IntNE, hint_val, moved_val, DebugLoc::None);
172 bcx = with_cond(bcx, may_need_drop, |cx| {
173 Call(cx, glue, &[ptr], debug_loc);
178 // No drop-hint ==> call standard drop glue
179 Call(bcx, glue, &[ptr], debug_loc);
186 pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
191 -> Block<'blk, 'tcx> {
192 let _icx = push_ctxt("drop_ty_immediate");
193 let vp = alloc_ty(bcx, t, "");
194 call_lifetime_start(bcx, vp);
195 store_ty(bcx, v, vp, t);
196 let bcx = drop_ty_core(bcx, vp, t, debug_loc, skip_dtor, None);
197 call_lifetime_end(bcx, vp);
201 pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ValueRef {
202 get_drop_glue_core(ccx, DropGlueKind::Ty(t))
205 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
206 pub enum DropGlueKind<'tcx> {
207 /// The normal path; runs the dtor, and then recurs on the contents
209 /// Skips the dtor, if any, for ty; drops the contents directly.
210 /// Note that the dtor is only skipped at the most *shallow*
211 /// level, namely, an `impl Drop for Ty` itself. So, for example,
212 /// if Ty is Newtype(S) then only the Drop impl for Newtype itself
213 /// will be skipped, while the Drop impl for S, if any, will be
215 TyContents(Ty<'tcx>),
218 impl<'tcx> DropGlueKind<'tcx> {
219 pub fn ty(&self) -> Ty<'tcx> {
220 match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
223 pub fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
226 DropGlueKind::Ty(t) => DropGlueKind::Ty(f(t)),
227 DropGlueKind::TyContents(t) => DropGlueKind::TyContents(f(t)),
232 fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
233 g: DropGlueKind<'tcx>) -> ValueRef {
234 let g = g.map_ty(|t| get_drop_glue_type(ccx.tcx(), t));
235 match ccx.drop_glues().borrow().get(&g) {
236 Some(&(glue, _)) => return glue,
238 debug!("Could not find drop glue for {:?} -- {} -- {}. \
239 Falling back to on-demand instantiation.",
241 TransItem::DropGlue(g).to_raw_string(),
242 ccx.codegen_unit().name());
244 ccx.stats().n_fallback_instantiations.set(ccx.stats()
245 .n_fallback_instantiations
251 // Normally, getting here would indicate a bug in trans::collector,
252 // since it seems to have missed a translation item. When we are
253 // translating with non-MIR-based trans, however, the results of the
254 // collector are not entirely reliable since it bases its analysis
255 // on MIR. Thus, we'll instantiate the missing function on demand in
256 // this codegen unit, so that things keep working.
258 TransItem::DropGlue(g).predefine(ccx, llvm::InternalLinkage);
259 TransItem::DropGlue(g).define(ccx);
261 // Now that we made sure that the glue function is in ccx.drop_glues,
262 // give it another try
263 get_drop_glue_core(ccx, g)
266 pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
267 g: DropGlueKind<'tcx>) {
269 assert_eq!(g.ty(), get_drop_glue_type(tcx, g.ty()));
270 let (llfn, fn_ty) = ccx.drop_glues().borrow().get(&g).unwrap().clone();
272 let (arena, fcx): (TypedArena<_>, FunctionContext);
273 arena = TypedArena::new();
274 fcx = FunctionContext::new(ccx, llfn, fn_ty, None, &arena);
276 let bcx = fcx.init(false, None);
278 ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1);
279 // All glue functions take values passed *by alias*; this is a
280 // requirement since in many contexts glue is invoked indirectly and
281 // the caller has no idea if it's dealing with something that can be
284 // llfn is expected be declared to take a parameter of the appropriate
285 // type, so we don't need to explicitly cast the function parameter.
287 let bcx = make_drop_glue(bcx, get_param(llfn, 0), g);
288 fcx.finish(bcx, DebugLoc::None);
292 fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
294 struct_data: ValueRef)
295 -> Block<'blk, 'tcx> {
296 assert!(type_is_sized(bcx.tcx(), t), "Precondition: caller must ensure t is sized");
298 let repr = adt::represent_type(bcx.ccx(), t);
299 let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &repr, struct_data));
300 let loaded = load_ty(bcx, drop_flag.val, bcx.tcx().dtor_type());
301 let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
302 let init_val = C_integral(drop_flag_llty, adt::DTOR_NEEDED as u64, false);
304 let bcx = if !bcx.ccx().check_drop_flag_for_sanity() {
307 let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
308 let done_val = C_integral(drop_flag_llty, adt::DTOR_DONE as u64, false);
309 let not_init = ICmp(bcx, llvm::IntNE, loaded, init_val, DebugLoc::None);
310 let not_done = ICmp(bcx, llvm::IntNE, loaded, done_val, DebugLoc::None);
311 let drop_flag_neither_initialized_nor_cleared =
312 And(bcx, not_init, not_done, DebugLoc::None);
313 with_cond(bcx, drop_flag_neither_initialized_nor_cleared, |cx| {
314 let llfn = cx.ccx().get_intrinsic(&("llvm.debugtrap"));
315 Call(cx, llfn, &[], DebugLoc::None);
320 let drop_flag_dtor_needed = ICmp(bcx, llvm::IntEQ, loaded, init_val, DebugLoc::None);
321 with_cond(bcx, drop_flag_dtor_needed, |cx| {
322 trans_struct_drop(cx, t, struct_data)
325 fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
330 debug!("trans_struct_drop t: {}", t);
334 let def = t.ty_adt_def().unwrap();
336 // Be sure to put the contents into a scope so we can use an invoke
337 // instruction to call the user destructor but still call the field
338 // destructors if the user destructor panics.
340 // FIXME (#14875) panic-in-drop semantics might be unsupported; we
341 // might well consider changing below to more direct code.
342 let contents_scope = bcx.fcx.push_custom_cleanup_scope();
344 // Issue #23611: schedule cleanup of contents, re-inspecting the
345 // discriminant (if any) in case of variant swap in drop code.
346 bcx.fcx.schedule_drop_adt_contents(cleanup::CustomScope(contents_scope), v0, t);
348 let (sized_args, unsized_args);
349 let args: &[ValueRef] = if type_is_sized(tcx, t) {
353 unsized_args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
357 let trait_ref = ty::Binder(ty::TraitRef {
358 def_id: tcx.lang_items.drop_trait().unwrap(),
359 substs: tcx.mk_substs(Substs::empty().with_self_ty(t))
361 let vtbl = match fulfill_obligation(bcx.ccx().shared(), DUMMY_SP, trait_ref) {
362 traits::VtableImpl(data) => data,
363 _ => bug!("dtor for {:?} is not an impl???", t)
365 let dtor_did = def.destructor().unwrap();
366 bcx = Callee::def(bcx.ccx(), dtor_did, vtbl.substs)
367 .call(bcx, DebugLoc::None, ArgVals(args), None).bcx;
369 bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, contents_scope)
372 pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>,
373 t: Ty<'tcx>, info: ValueRef)
374 -> (ValueRef, ValueRef) {
375 debug!("calculate size of DST: {}; with lost info: {:?}",
377 if type_is_sized(bcx.tcx(), t) {
378 let sizing_type = sizing_type_of(bcx.ccx(), t);
379 let size = llsize_of_alloc(bcx.ccx(), sizing_type);
380 let align = align_of(bcx.ccx(), t);
381 debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
382 t, Value(info), size, align);
383 let size = C_uint(bcx.ccx(), size);
384 let align = C_uint(bcx.ccx(), align);
385 return (size, align);
387 if bcx.is_unreachable() {
388 let llty = Type::int(bcx.ccx());
389 return (C_undef(llty), C_undef(llty));
392 ty::TyStruct(def, substs) => {
394 // First get the size of all statically known fields.
395 // Don't use type_of::sizing_type_of because that expects t to be sized.
396 assert!(!t.is_simd());
397 let repr = adt::represent_type(ccx, t);
398 let sizing_type = adt::sizing_type_context_of(ccx, &repr, true);
399 debug!("DST {} sizing_type: {:?}", t, sizing_type);
400 let sized_size = llsize_of_alloc(ccx, sizing_type.prefix());
401 let sized_align = llalign_of_min(ccx, sizing_type.prefix());
402 debug!("DST {} statically sized prefix size: {} align: {}",
403 t, sized_size, sized_align);
404 let sized_size = C_uint(ccx, sized_size);
405 let sized_align = C_uint(ccx, sized_align);
407 // Recurse to get the size of the dynamically sized field (must be
409 let last_field = def.struct_variant().fields.last().unwrap();
410 let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
411 let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
413 // FIXME (#26403, #27023): We should be adding padding
414 // to `sized_size` (to accommodate the `unsized_align`
415 // required of the unsized field that follows) before
416 // summing it with `sized_size`. (Note that since #26403
417 // is unfixed, we do not yet add the necessary padding
418 // here. But this is where the add would go.)
420 // Return the sum of sizes and max of aligns.
421 let mut size = bcx.add(sized_size, unsized_size);
423 // Issue #27023: If there is a drop flag, *now* we add 1
424 // to the size. (We can do this without adding any
425 // padding because drop flags do not have any alignment
427 if sizing_type.needs_drop_flag() {
428 size = bcx.add(size, C_uint(bcx.ccx(), 1_u64));
431 // Choose max of two known alignments (combined value must
432 // be aligned according to more restrictive of the two).
433 let align = match (const_to_opt_uint(sized_align), const_to_opt_uint(unsized_align)) {
434 (Some(sized_align), Some(unsized_align)) => {
435 // If both alignments are constant, (the sized_align should always be), then
436 // pick the correct alignment statically.
437 C_uint(ccx, std::cmp::max(sized_align, unsized_align))
439 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
444 // Issue #27023: must add any necessary padding to `size`
445 // (to make it a multiple of `align`) before returning it.
447 // Namely, the returned size should be, in C notation:
449 // `size + ((size & (align-1)) ? align : 0)`
451 // emulated via the semi-standard fast bit trick:
453 // `(size + (align-1)) & -align`
455 let addend = bcx.sub(align, C_uint(bcx.ccx(), 1_u64));
456 let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
461 // info points to the vtable and the second entry in the vtable is the
462 // dynamic size of the object.
463 let info = bcx.pointercast(info, Type::int(bcx.ccx()).ptr_to());
464 let size_ptr = bcx.gepi(info, &[1]);
465 let align_ptr = bcx.gepi(info, &[2]);
466 (bcx.load(size_ptr), bcx.load(align_ptr))
468 ty::TySlice(_) | ty::TyStr => {
469 let unit_ty = t.sequence_element_type(bcx.tcx());
470 // The info in this case is the length of the str, so the size is that
471 // times the unit size.
472 let llunit_ty = sizing_type_of(bcx.ccx(), unit_ty);
473 let unit_align = llalign_of_min(bcx.ccx(), llunit_ty);
474 let unit_size = llsize_of_alloc(bcx.ccx(), llunit_ty);
475 (bcx.mul(info, C_uint(bcx.ccx(), unit_size)),
476 C_uint(bcx.ccx(), unit_align))
478 _ => bug!("Unexpected unsized type, found {}", t)
482 fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueKind<'tcx>)
483 -> Block<'blk, 'tcx> {
486 let skip_dtor = match g { DropGlueKind::Ty(_) => false, DropGlueKind::TyContents(_) => true };
487 // NB: v0 is an *alias* of type t here, not a direct value.
488 let _icx = push_ctxt("make_drop_glue");
490 // Only drop the value when it ... well, we used to check for
491 // non-null, (and maybe we need to continue doing so), but we now
492 // must definitely check for special bit-patterns corresponding to
493 // the special dtor markings.
495 let inttype = Type::int(bcx.ccx());
496 let dropped_pattern = C_integral(inttype, adt::DTOR_DONE_U64, false);
499 ty::TyBox(content_ty) => {
500 // Support for TyBox is built-in and its drop glue is
501 // special. It may move to library and have Drop impl. As
502 // a safe-guard, assert TyBox not used with TyContents.
504 if !type_is_sized(bcx.tcx(), content_ty) {
505 let llval = expr::get_dataptr(bcx, v0);
506 let llbox = Load(bcx, llval);
507 let llbox_as_usize = PtrToInt(bcx, llbox, Type::int(bcx.ccx()));
508 let drop_flag_not_dropped_already =
509 ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
510 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
511 let bcx = drop_ty(bcx, v0, content_ty, DebugLoc::None);
512 let info = expr::get_meta(bcx, v0);
513 let info = Load(bcx, info);
514 let (llsize, llalign) =
515 size_and_align_of_dst(&bcx.build(), content_ty, info);
517 // `Box<ZeroSizeType>` does not allocate.
518 let needs_free = ICmp(bcx,
521 C_uint(bcx.ccx(), 0u64),
523 with_cond(bcx, needs_free, |bcx| {
524 trans_exchange_free_dyn(bcx, llbox, llsize, llalign, DebugLoc::None)
529 let llbox = Load(bcx, llval);
530 let llbox_as_usize = PtrToInt(bcx, llbox, inttype);
531 let drop_flag_not_dropped_already =
532 ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
533 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
534 let bcx = drop_ty(bcx, llbox, content_ty, DebugLoc::None);
535 trans_exchange_free_ty(bcx, llbox, content_ty, DebugLoc::None)
539 ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
540 match (def.dtor_kind(), skip_dtor) {
541 (ty::TraitDtor(true), false) => {
542 // FIXME(16758) Since the struct is unsized, it is hard to
543 // find the drop flag (which is at the end of the struct).
544 // Lets just ignore the flag and pretend everything will be
546 if type_is_sized(bcx.tcx(), t) {
547 trans_struct_drop_flag(bcx, t, v0)
549 // Give the user a heads up that we are doing something
550 // stupid and dangerous.
551 bcx.sess().warn(&format!("Ignoring drop flag in destructor for {} \
552 because the struct is unsized. See issue \
554 trans_struct_drop(bcx, t, v0)
557 (ty::TraitDtor(false), false) => {
558 trans_struct_drop(bcx, t, v0)
560 (ty::NoDtor, _) | (_, true) => {
561 // No dtor? Just the default case
562 iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
567 // No support in vtable for distinguishing destroying with
568 // versus without calling Drop::drop. Assert caller is
569 // okay with always calling the Drop impl, if any.
571 let data_ptr = expr::get_dataptr(bcx, v0);
572 let vtable_ptr = Load(bcx, expr::get_meta(bcx, v0));
573 let dtor = Load(bcx, vtable_ptr);
576 &[PointerCast(bcx, Load(bcx, data_ptr), Type::i8p(bcx.ccx()))],
581 if bcx.fcx.type_needs_drop(t) {
582 iter_structural_ty(bcx,
585 |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))