]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/glue.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc_trans / trans / glue.rs
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.
4 //
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.
10
11 //!
12 //
13 // Code relating to drop glue.
14
15 use std;
16
17 use back::link::*;
18 use llvm;
19 use llvm::{ValueRef, get_param};
20 use middle::lang_items::ExchangeFreeFnLangItem;
21 use middle::subst::{Substs};
22 use middle::traits;
23 use middle::ty::{self, Ty};
24 use trans::adt;
25 use trans::adt::GetDtorType; // for tcx.dtor_type()
26 use trans::base::*;
27 use trans::build::*;
28 use trans::callee;
29 use trans::cleanup;
30 use trans::cleanup::CleanupMethods;
31 use trans::collector::{self, TransItem};
32 use trans::common::*;
33 use trans::debuginfo::DebugLoc;
34 use trans::declare;
35 use trans::expr;
36 use trans::machine::*;
37 use trans::monomorphize;
38 use trans::type_of::{type_of, sizing_type_of, align_of};
39 use trans::type_::Type;
40
41 use arena::TypedArena;
42 use libc::c_uint;
43 use syntax::ast;
44 use syntax::codemap::DUMMY_SP;
45
46 pub fn trans_exchange_free_dyn<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
47 v: ValueRef,
48 size: ValueRef,
49 align: ValueRef,
50 debug_loc: DebugLoc)
51 -> Block<'blk, 'tcx> {
52 let _icx = push_ctxt("trans_exchange_free");
53 let ccx = cx.ccx();
54 callee::trans_lang_call(cx,
55 langcall(cx, None, "", ExchangeFreeFnLangItem),
56 &[PointerCast(cx, v, Type::i8p(ccx)), size, align],
57 Some(expr::Ignore),
58 debug_loc).bcx
59 }
60
61 pub fn trans_exchange_free<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
62 v: ValueRef,
63 size: u64,
64 align: u32,
65 debug_loc: DebugLoc)
66 -> Block<'blk, 'tcx> {
67 trans_exchange_free_dyn(cx,
68 v,
69 C_uint(cx.ccx(), size),
70 C_uint(cx.ccx(), align),
71 debug_loc)
72 }
73
74 pub fn trans_exchange_free_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
75 ptr: ValueRef,
76 content_ty: Ty<'tcx>,
77 debug_loc: DebugLoc)
78 -> Block<'blk, 'tcx> {
79 assert!(type_is_sized(bcx.ccx().tcx(), content_ty));
80 let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
81 let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
82
83 // `Box<ZeroSizeType>` does not allocate.
84 if content_size != 0 {
85 let content_align = align_of(bcx.ccx(), content_ty);
86 trans_exchange_free(bcx, ptr, content_size, content_align, debug_loc)
87 } else {
88 bcx
89 }
90 }
91
92 pub fn type_needs_drop<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
93 tcx.type_needs_drop_given_env(ty, &tcx.empty_parameter_environment())
94 }
95
96 pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
97 t: Ty<'tcx>) -> Ty<'tcx> {
98 let tcx = ccx.tcx();
99 // Even if there is no dtor for t, there might be one deeper down and we
100 // might need to pass in the vtable ptr.
101 if !type_is_sized(tcx, t) {
102 return t
103 }
104
105 // FIXME (#22815): note that type_needs_drop conservatively
106 // approximates in some cases and may say a type expression
107 // requires drop glue when it actually does not.
108 //
109 // (In this case it is not clear whether any harm is done, i.e.
110 // erroneously returning `t` in some cases where we could have
111 // returned `tcx.types.i8` does not appear unsound. The impact on
112 // code quality is unknown at this time.)
113
114 if !type_needs_drop(&tcx, t) {
115 return tcx.types.i8;
116 }
117 match t.sty {
118 ty::TyBox(typ) if !type_needs_drop(&tcx, typ)
119 && type_is_sized(tcx, typ) => {
120 let llty = sizing_type_of(ccx, typ);
121 // `Box<ZeroSizeType>` does not allocate.
122 if llsize_of_alloc(ccx, llty) == 0 {
123 tcx.types.i8
124 } else {
125 t
126 }
127 }
128 _ => t
129 }
130 }
131
132 pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
133 v: ValueRef,
134 t: Ty<'tcx>,
135 debug_loc: DebugLoc) -> Block<'blk, 'tcx> {
136 drop_ty_core(bcx, v, t, debug_loc, false, None)
137 }
138
139 pub fn drop_ty_core<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
140 v: ValueRef,
141 t: Ty<'tcx>,
142 debug_loc: DebugLoc,
143 skip_dtor: bool,
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");
149 let mut bcx = bcx;
150 if bcx.fcx.type_needs_drop(t) {
151 let ccx = bcx.ccx();
152 let g = if skip_dtor {
153 DropGlueKind::TyContents(t)
154 } else {
155 DropGlueKind::Ty(t)
156 };
157 let glue = get_drop_glue_core(ccx, g);
158 let glue_type = get_drop_glue_type(ccx, t);
159 let ptr = if glue_type != t {
160 PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
161 } else {
162 v
163 };
164
165 match drop_hint {
166 Some(drop_hint) => {
167 let hint_val = load_ty(bcx, drop_hint.value(), bcx.tcx().types.u8);
168 let moved_val =
169 C_integral(Type::i8(bcx.ccx()), adt::DTOR_MOVED_HINT as u64, false);
170 let may_need_drop =
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], None, debug_loc);
174 cx
175 })
176 }
177 None => {
178 // No drop-hint ==> call standard drop glue
179 Call(bcx, glue, &[ptr], None, debug_loc);
180 }
181 }
182 }
183 bcx
184 }
185
186 pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
187 v: ValueRef,
188 t: Ty<'tcx>,
189 debug_loc: DebugLoc,
190 skip_dtor: bool)
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);
198 bcx
199 }
200
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))
203 }
204
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
208 Ty(Ty<'tcx>),
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
214 /// invoked.
215 TyContents(Ty<'tcx>),
216 }
217
218 impl<'tcx> DropGlueKind<'tcx> {
219 fn ty(&self) -> Ty<'tcx> {
220 match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
221 }
222
223 fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
224 {
225 match *self {
226 DropGlueKind::Ty(t) => DropGlueKind::Ty(f(t)),
227 DropGlueKind::TyContents(t) => DropGlueKind::TyContents(f(t)),
228 }
229 }
230 }
231
232 fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
233 g: DropGlueKind<'tcx>) -> ValueRef {
234 debug!("make drop glue for {:?}", g);
235 let g = g.map_ty(|t| get_drop_glue_type(ccx, t));
236 debug!("drop glue type {:?}", g);
237 match ccx.drop_glues().borrow().get(&g) {
238 Some(&glue) => return glue,
239 _ => { }
240 }
241 let t = g.ty();
242
243 let llty = if type_is_sized(ccx.tcx(), t) {
244 type_of(ccx, t).ptr_to()
245 } else {
246 type_of(ccx, ccx.tcx().mk_box(t)).ptr_to()
247 };
248
249 let llfnty = Type::glue_fn(ccx, llty);
250
251 // To avoid infinite recursion, don't `make_drop_glue` until after we've
252 // added the entry to the `drop_glues` cache.
253 if let Some(old_sym) = ccx.available_drop_glues().borrow().get(&g) {
254 let llfn = declare::declare_cfn(ccx, &old_sym, llfnty, ccx.tcx().mk_nil());
255 ccx.drop_glues().borrow_mut().insert(g, llfn);
256 return llfn;
257 };
258
259 let fn_nm = mangle_internal_name_by_type_and_seq(ccx, t, "drop");
260 let llfn = declare::define_cfn(ccx, &fn_nm, llfnty, ccx.tcx().mk_nil()).unwrap_or_else(||{
261 ccx.sess().bug(&format!("symbol `{}` already defined", fn_nm));
262 });
263 ccx.available_drop_glues().borrow_mut().insert(g, fn_nm);
264
265 let _s = StatRecorder::new(ccx, format!("drop {:?}", t));
266
267 let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
268 let (arena, fcx): (TypedArena<_>, FunctionContext);
269 arena = TypedArena::new();
270 fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, false,
271 ty::FnConverging(ccx.tcx().mk_nil()),
272 empty_substs, None, &arena);
273
274 let bcx = init_function(&fcx, false, ty::FnConverging(ccx.tcx().mk_nil()));
275
276 update_linkage(ccx, llfn, None, OriginalTranslation);
277
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
282 // passed by value.
283 //
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.
286
287 let llrawptr0 = get_param(llfn, fcx.arg_offset() as c_uint);
288 let bcx = make_drop_glue(bcx, llrawptr0, g);
289 finish_fn(&fcx, bcx, ty::FnConverging(ccx.tcx().mk_nil()), DebugLoc::None);
290
291 llfn
292 }
293
294 fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
295 t: Ty<'tcx>,
296 struct_data: ValueRef)
297 -> Block<'blk, 'tcx> {
298 assert!(type_is_sized(bcx.tcx(), t), "Precondition: caller must ensure t is sized");
299
300 let repr = adt::represent_type(bcx.ccx(), t);
301 let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &repr, struct_data));
302 let loaded = load_ty(bcx, drop_flag.val, bcx.tcx().dtor_type());
303 let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
304 let init_val = C_integral(drop_flag_llty, adt::DTOR_NEEDED as u64, false);
305
306 let bcx = if !bcx.ccx().check_drop_flag_for_sanity() {
307 bcx
308 } else {
309 let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
310 let done_val = C_integral(drop_flag_llty, adt::DTOR_DONE as u64, false);
311 let not_init = ICmp(bcx, llvm::IntNE, loaded, init_val, DebugLoc::None);
312 let not_done = ICmp(bcx, llvm::IntNE, loaded, done_val, DebugLoc::None);
313 let drop_flag_neither_initialized_nor_cleared =
314 And(bcx, not_init, not_done, DebugLoc::None);
315 with_cond(bcx, drop_flag_neither_initialized_nor_cleared, |cx| {
316 let llfn = cx.ccx().get_intrinsic(&("llvm.debugtrap"));
317 Call(cx, llfn, &[], None, DebugLoc::None);
318 cx
319 })
320 };
321
322 let drop_flag_dtor_needed = ICmp(bcx, llvm::IntEQ, loaded, init_val, DebugLoc::None);
323 with_cond(bcx, drop_flag_dtor_needed, |cx| {
324 trans_struct_drop(cx, t, struct_data)
325 })
326 }
327 fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
328 t: Ty<'tcx>,
329 v0: ValueRef)
330 -> Block<'blk, 'tcx>
331 {
332 debug!("trans_struct_drop t: {}", t);
333 let tcx = bcx.tcx();
334 let mut bcx = bcx;
335
336 let def = t.ty_adt_def().unwrap();
337
338 // Be sure to put the contents into a scope so we can use an invoke
339 // instruction to call the user destructor but still call the field
340 // destructors if the user destructor panics.
341 //
342 // FIXME (#14875) panic-in-drop semantics might be unsupported; we
343 // might well consider changing below to more direct code.
344 let contents_scope = bcx.fcx.push_custom_cleanup_scope();
345
346 // Issue #23611: schedule cleanup of contents, re-inspecting the
347 // discriminant (if any) in case of variant swap in drop code.
348 bcx.fcx.schedule_drop_adt_contents(cleanup::CustomScope(contents_scope), v0, t);
349
350 let (sized_args, unsized_args);
351 let args: &[ValueRef] = if type_is_sized(tcx, t) {
352 sized_args = [v0];
353 &sized_args
354 } else {
355 unsized_args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
356 &unsized_args
357 };
358
359 bcx = callee::trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
360 let trait_ref = ty::Binder(ty::TraitRef {
361 def_id: tcx.lang_items.drop_trait().unwrap(),
362 substs: tcx.mk_substs(Substs::trans_empty().with_self_ty(t))
363 });
364 let vtbl = match fulfill_obligation(bcx.ccx(), DUMMY_SP, trait_ref) {
365 traits::VtableImpl(data) => data,
366 _ => tcx.sess.bug(&format!("dtor for {:?} is not an impl???", t))
367 };
368 let dtor_did = def.destructor().unwrap();
369 let datum = callee::trans_fn_ref_with_substs(bcx.ccx(),
370 dtor_did,
371 ExprId(0),
372 bcx.fcx.param_substs,
373 vtbl.substs);
374 callee::Callee {
375 bcx: bcx,
376 data: callee::Fn(datum.val),
377 ty: datum.ty
378 }
379 }, callee::ArgVals(args), Some(expr::Ignore)).bcx;
380
381 bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, contents_scope)
382 }
383
384 pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, info: ValueRef)
385 -> (ValueRef, ValueRef) {
386 debug!("calculate size of DST: {}; with lost info: {}",
387 t, bcx.val_to_string(info));
388 if type_is_sized(bcx.tcx(), t) {
389 let sizing_type = sizing_type_of(bcx.ccx(), t);
390 let size = llsize_of_alloc(bcx.ccx(), sizing_type);
391 let align = align_of(bcx.ccx(), t);
392 debug!("size_and_align_of_dst t={} info={} size: {} align: {}",
393 t, bcx.val_to_string(info), size, align);
394 let size = C_uint(bcx.ccx(), size);
395 let align = C_uint(bcx.ccx(), align);
396 return (size, align);
397 }
398 match t.sty {
399 ty::TyStruct(def, substs) => {
400 let ccx = bcx.ccx();
401 // First get the size of all statically known fields.
402 // Don't use type_of::sizing_type_of because that expects t to be sized.
403 assert!(!t.is_simd());
404 let repr = adt::represent_type(ccx, t);
405 let sizing_type = adt::sizing_type_context_of(ccx, &repr, true);
406 debug!("DST {} sizing_type: {}", t, sizing_type.to_string());
407 let sized_size = llsize_of_alloc(ccx, sizing_type.prefix());
408 let sized_align = llalign_of_min(ccx, sizing_type.prefix());
409 debug!("DST {} statically sized prefix size: {} align: {}",
410 t, sized_size, sized_align);
411 let sized_size = C_uint(ccx, sized_size);
412 let sized_align = C_uint(ccx, sized_align);
413
414 // Recurse to get the size of the dynamically sized field (must be
415 // the last field).
416 let last_field = def.struct_variant().fields.last().unwrap();
417 let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
418 let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
419
420 let dbloc = DebugLoc::None;
421
422 // FIXME (#26403, #27023): We should be adding padding
423 // to `sized_size` (to accommodate the `unsized_align`
424 // required of the unsized field that follows) before
425 // summing it with `sized_size`. (Note that since #26403
426 // is unfixed, we do not yet add the necessary padding
427 // here. But this is where the add would go.)
428
429 // Return the sum of sizes and max of aligns.
430 let mut size = Add(bcx, sized_size, unsized_size, dbloc);
431
432 // Issue #27023: If there is a drop flag, *now* we add 1
433 // to the size. (We can do this without adding any
434 // padding because drop flags do not have any alignment
435 // constraints.)
436 if sizing_type.needs_drop_flag() {
437 size = Add(bcx, size, C_uint(bcx.ccx(), 1_u64), dbloc);
438 }
439
440 // Choose max of two known alignments (combined value must
441 // be aligned according to more restrictive of the two).
442 let align = match (const_to_opt_uint(sized_align), const_to_opt_uint(unsized_align)) {
443 (Some(sized_align), Some(unsized_align)) => {
444 // If both alignments are constant, (the sized_align should always be), then
445 // pick the correct alignment statically.
446 C_uint(ccx, std::cmp::max(sized_align, unsized_align))
447 }
448 _ => Select(bcx,
449 ICmp(bcx,
450 llvm::IntUGT,
451 sized_align,
452 unsized_align,
453 dbloc),
454 sized_align,
455 unsized_align)
456 };
457
458 // Issue #27023: must add any necessary padding to `size`
459 // (to make it a multiple of `align`) before returning it.
460 //
461 // Namely, the returned size should be, in C notation:
462 //
463 // `size + ((size & (align-1)) ? align : 0)`
464 //
465 // emulated via the semi-standard fast bit trick:
466 //
467 // `(size + (align-1)) & -align`
468
469 let addend = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
470 let size = And(
471 bcx, Add(bcx, size, addend, dbloc), Neg(bcx, align, dbloc), dbloc);
472
473 (size, align)
474 }
475 ty::TyTrait(..) => {
476 // info points to the vtable and the second entry in the vtable is the
477 // dynamic size of the object.
478 let info = PointerCast(bcx, info, Type::int(bcx.ccx()).ptr_to());
479 let size_ptr = GEPi(bcx, info, &[1]);
480 let align_ptr = GEPi(bcx, info, &[2]);
481 (Load(bcx, size_ptr), Load(bcx, align_ptr))
482 }
483 ty::TySlice(_) | ty::TyStr => {
484 let unit_ty = t.sequence_element_type(bcx.tcx());
485 // The info in this case is the length of the str, so the size is that
486 // times the unit size.
487 let llunit_ty = sizing_type_of(bcx.ccx(), unit_ty);
488 let unit_align = llalign_of_min(bcx.ccx(), llunit_ty);
489 let unit_size = llsize_of_alloc(bcx.ccx(), llunit_ty);
490 (Mul(bcx, info, C_uint(bcx.ccx(), unit_size), DebugLoc::None),
491 C_uint(bcx.ccx(), unit_align))
492 }
493 _ => bcx.sess().bug(&format!("Unexpected unsized type, found {}", t))
494 }
495 }
496
497 fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueKind<'tcx>)
498 -> Block<'blk, 'tcx> {
499 let t = g.ty();
500
501 if collector::collecting_debug_information(bcx.ccx()) {
502 bcx.ccx()
503 .record_translation_item_as_generated(TransItem::DropGlue(bcx.tcx()
504 .erase_regions(&t)));
505 }
506
507 let skip_dtor = match g { DropGlueKind::Ty(_) => false, DropGlueKind::TyContents(_) => true };
508 // NB: v0 is an *alias* of type t here, not a direct value.
509 let _icx = push_ctxt("make_drop_glue");
510
511 // Only drop the value when it ... well, we used to check for
512 // non-null, (and maybe we need to continue doing so), but we now
513 // must definitely check for special bit-patterns corresponding to
514 // the special dtor markings.
515
516 let inttype = Type::int(bcx.ccx());
517 let dropped_pattern = C_integral(inttype, adt::DTOR_DONE_U64, false);
518
519 match t.sty {
520 ty::TyBox(content_ty) => {
521 // Support for TyBox is built-in and its drop glue is
522 // special. It may move to library and have Drop impl. As
523 // a safe-guard, assert TyBox not used with TyContents.
524 assert!(!skip_dtor);
525 if !type_is_sized(bcx.tcx(), content_ty) {
526 let llval = expr::get_dataptr(bcx, v0);
527 let llbox = Load(bcx, llval);
528 let llbox_as_usize = PtrToInt(bcx, llbox, Type::int(bcx.ccx()));
529 let drop_flag_not_dropped_already =
530 ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
531 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
532 let bcx = drop_ty(bcx, v0, content_ty, DebugLoc::None);
533 let info = expr::get_meta(bcx, v0);
534 let info = Load(bcx, info);
535 let (llsize, llalign) = size_and_align_of_dst(bcx, content_ty, info);
536
537 // `Box<ZeroSizeType>` does not allocate.
538 let needs_free = ICmp(bcx,
539 llvm::IntNE,
540 llsize,
541 C_uint(bcx.ccx(), 0u64),
542 DebugLoc::None);
543 with_cond(bcx, needs_free, |bcx| {
544 trans_exchange_free_dyn(bcx, llbox, llsize, llalign, DebugLoc::None)
545 })
546 })
547 } else {
548 let llval = v0;
549 let llbox = Load(bcx, llval);
550 let llbox_as_usize = PtrToInt(bcx, llbox, inttype);
551 let drop_flag_not_dropped_already =
552 ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
553 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
554 let bcx = drop_ty(bcx, llbox, content_ty, DebugLoc::None);
555 trans_exchange_free_ty(bcx, llbox, content_ty, DebugLoc::None)
556 })
557 }
558 }
559 ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
560 match (def.dtor_kind(), skip_dtor) {
561 (ty::TraitDtor(true), false) => {
562 // FIXME(16758) Since the struct is unsized, it is hard to
563 // find the drop flag (which is at the end of the struct).
564 // Lets just ignore the flag and pretend everything will be
565 // OK.
566 if type_is_sized(bcx.tcx(), t) {
567 trans_struct_drop_flag(bcx, t, v0)
568 } else {
569 // Give the user a heads up that we are doing something
570 // stupid and dangerous.
571 bcx.sess().warn(&format!("Ignoring drop flag in destructor for {}\
572 because the struct is unsized. See issue\
573 #16758", t));
574 trans_struct_drop(bcx, t, v0)
575 }
576 }
577 (ty::TraitDtor(false), false) => {
578 trans_struct_drop(bcx, t, v0)
579 }
580 (ty::NoDtor, _) | (_, true) => {
581 // No dtor? Just the default case
582 iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
583 }
584 }
585 }
586 ty::TyTrait(..) => {
587 // No support in vtable for distinguishing destroying with
588 // versus without calling Drop::drop. Assert caller is
589 // okay with always calling the Drop impl, if any.
590 assert!(!skip_dtor);
591 let data_ptr = expr::get_dataptr(bcx, v0);
592 let vtable_ptr = Load(bcx, expr::get_meta(bcx, v0));
593 let dtor = Load(bcx, vtable_ptr);
594 Call(bcx,
595 dtor,
596 &[PointerCast(bcx, Load(bcx, data_ptr), Type::i8p(bcx.ccx()))],
597 None,
598 DebugLoc::None);
599 bcx
600 }
601 _ => {
602 if bcx.fcx.type_needs_drop(t) {
603 iter_structural_ty(bcx,
604 v0,
605 t,
606 |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
607 } else {
608 bcx
609 }
610 }
611 }
612 }