1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std;
use std::iter;
use llvm;
use llvm::{ValueRef, get_param};
use middle::lang_items::BoxFreeFnLangItem;
use rustc::ty::subst::{Substs};
use rustc::traits;
use rustc::ty::{self, layout, AdtDef, AdtKind, Ty, TypeFoldable};
use rustc::ty::subst::Kind;
use rustc::mir::tcx::LvalueTy;
use mir::lvalue::LvalueRef;
use adt;
use base::*;
use callee::Callee;
use cleanup::CleanupScope;
use common::*;
use machine::*;
use monomorphize;
use trans_item::TransItem;
use tvec;
use type_of::{type_of, sizing_type_of, align_of};
use type_::Type;
use value::Value;
use Disr;
use builder::Builder;
use syntax_pos::DUMMY_SP;
use mir::lvalue::Alignment;
pub fn trans_exchange_free_ty<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, ptr: LvalueRef<'tcx>) {
let content_ty = ptr.ty.to_ty(bcx.tcx());
let def_id = langcall(bcx.tcx(), None, "", BoxFreeFnLangItem);
let substs = bcx.tcx().mk_substs(iter::once(Kind::from(content_ty)));
let callee = Callee::def(bcx.ccx, def_id, substs);
let fn_ty = callee.direct_fn_type(bcx.ccx, &[]);
let llret = bcx.call(callee.reify(bcx.ccx),
&[ptr.llval, ptr.llextra][..1 + ptr.has_extra() as usize], None);
fn_ty.apply_attrs_callsite(llret);
}
pub fn get_drop_glue_type<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {
assert!(t.is_normalized_for_trans());
let t = scx.tcx().erase_regions(&t);
if !scx.type_is_sized(t) {
return t;
}
if !scx.type_needs_drop(t) {
return scx.tcx().types.i8;
}
match t.sty {
ty::TyAdt(def, _) if def.is_box() => {
let typ = t.boxed_ty();
if !scx.type_needs_drop(typ) && scx.type_is_sized(typ) {
scx.tcx().infer_ctxt((), traits::Reveal::All).enter(|infcx| {
let layout = t.layout(&infcx).unwrap();
if layout.size(&scx.tcx().data_layout).bytes() == 0 {
scx.tcx().types.i8
} else {
t
}
})
} else {
t
}
}
_ => t
}
}
fn drop_ty<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, args: LvalueRef<'tcx>) {
call_drop_glue(bcx, args, false, None)
}
pub fn call_drop_glue<'a, 'tcx>(
bcx: &Builder<'a, 'tcx>,
mut args: LvalueRef<'tcx>,
skip_dtor: bool,
funclet: Option<&'a Funclet>,
) {
let t = args.ty.to_ty(bcx.tcx());
debug!("call_drop_glue(t={:?}, skip_dtor={})", t, skip_dtor);
if bcx.ccx.shared().type_needs_drop(t) {
let ccx = bcx.ccx;
let g = if skip_dtor {
DropGlueKind::TyContents(t)
} else {
DropGlueKind::Ty(t)
};
let glue = get_drop_glue_core(ccx, g);
let glue_type = get_drop_glue_type(ccx.shared(), t);
if glue_type != t {
args.llval = bcx.pointercast(args.llval, type_of(ccx, glue_type).ptr_to());
}
bcx.call(glue, &[args.llval, args.llextra][..1 + args.has_extra() as usize],
funclet.map(|b| b.bundle()));
}
}
pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ValueRef {
get_drop_glue_core(ccx, DropGlueKind::Ty(t))
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum DropGlueKind<'tcx> {
Ty(Ty<'tcx>),
TyContents(Ty<'tcx>),
}
impl<'tcx> DropGlueKind<'tcx> {
pub fn ty(&self) -> Ty<'tcx> {
match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
}
pub 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)),
}
}
}
fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKind<'tcx>) -> ValueRef {
let g = g.map_ty(|t| get_drop_glue_type(ccx.shared(), t));
match ccx.drop_glues().borrow().get(&g) {
Some(&(glue, _)) => glue,
None => {
bug!("Could not find drop glue for {:?} -- {} -- {}.",
g,
TransItem::DropGlue(g).to_raw_string(),
ccx.codegen_unit().name());
}
}
}
pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKind<'tcx>) {
assert_eq!(g.ty(), get_drop_glue_type(ccx.shared(), g.ty()));
let (llfn, _) = ccx.drop_glues().borrow().get(&g).unwrap().clone();
let mut bcx = Builder::new_block(ccx, llfn, "entry-block");
ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1);
let t = g.ty();
let value = get_param(llfn, 0);
let ptr = if ccx.shared().type_is_sized(t) {
LvalueRef::new_sized_ty(value, t, Alignment::AbiAligned)
} else {
LvalueRef::new_unsized_ty(value, get_param(llfn, 1), t, Alignment::AbiAligned)
};
let skip_dtor = match g {
DropGlueKind::Ty(_) => false,
DropGlueKind::TyContents(_) => true
};
let bcx = match t.sty {
ty::TyAdt(def, _) if def.is_box() => {
assert!(!skip_dtor);
let content_ty = t.boxed_ty();
let ptr = if !bcx.ccx.shared().type_is_sized(content_ty) {
let llbox = bcx.load(get_dataptr(&bcx, ptr.llval), None);
let info = bcx.load(get_meta(&bcx, ptr.llval), None);
LvalueRef::new_unsized_ty(llbox, info, content_ty, Alignment::AbiAligned)
} else {
LvalueRef::new_sized_ty(
bcx.load(ptr.llval, None),
content_ty, Alignment::AbiAligned)
};
drop_ty(&bcx, ptr);
trans_exchange_free_ty(&bcx, ptr);
bcx
}
ty::TyDynamic(..) => {
assert!(!skip_dtor);
let dtor = bcx.load(ptr.llextra, None);
bcx.call(dtor, &[ptr.llval], None);
bcx
}
ty::TyAdt(def, ..) if def.has_dtor(bcx.tcx()) && !skip_dtor => {
let shallow_drop = def.is_union();
let tcx = bcx.tcx();
let def = t.ty_adt_def().unwrap();
let contents_scope = if !shallow_drop {
CleanupScope::schedule_drop_adt_contents(&bcx, ptr)
} else {
CleanupScope::noop()
};
let trait_ref = ty::Binder(ty::TraitRef {
def_id: tcx.lang_items.drop_trait().unwrap(),
substs: tcx.mk_substs_trait(t, &[])
});
let vtbl = match fulfill_obligation(bcx.ccx.shared(), DUMMY_SP, trait_ref) {
traits::VtableImpl(data) => data,
_ => bug!("dtor for {:?} is not an impl???", t)
};
let dtor_did = def.destructor(tcx).unwrap().did;
let callee = Callee::def(bcx.ccx, dtor_did, vtbl.substs);
let fn_ty = callee.direct_fn_type(bcx.ccx, &[]);
let llret;
let args = &[ptr.llval, ptr.llextra][..1 + ptr.has_extra() as usize];
if let Some(landing_pad) = contents_scope.landing_pad {
let normal_bcx = bcx.build_sibling_block("normal-return");
llret = bcx.invoke(callee.reify(ccx), args, normal_bcx.llbb(), landing_pad, None);
bcx = normal_bcx;
} else {
llret = bcx.call(callee.reify(bcx.ccx), args, None);
}
fn_ty.apply_attrs_callsite(llret);
contents_scope.trans(&bcx);
bcx
}
ty::TyAdt(def, ..) if def.is_union() => {
bcx
}
_ => {
if bcx.ccx.shared().type_needs_drop(t) {
drop_structural_ty(bcx, ptr)
} else {
bcx
}
}
};
bcx.ret_void();
}
pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
-> (ValueRef, ValueRef) {
debug!("calculate size of DST: {}; with lost info: {:?}",
t, Value(info));
if bcx.ccx.shared().type_is_sized(t) {
let sizing_type = sizing_type_of(bcx.ccx, t);
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, Value(info), size, align);
let size = C_uint(bcx.ccx, size);
let align = C_uint(bcx.ccx, align);
return (size, align);
}
match t.sty {
ty::TyAdt(def, substs) => {
let ccx = bcx.ccx;
assert!(!t.is_simd());
let layout = ccx.layout_of(t);
debug!("DST {} layout: {:?}", t, layout);
let (sized_size, sized_align) = match *layout {
ty::layout::Layout::Univariant { ref variant, .. } => {
(variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
}
_ => {
bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
t, layout);
}
};
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);
let last_field = def.struct_variant().fields.last().unwrap();
let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
let size = bcx.add(sized_size, unsized_size);
let align = match (const_to_opt_u128(sized_align, false),
const_to_opt_u128(unsized_align, false)) {
(Some(sized_align), Some(unsized_align)) => {
C_uint(ccx, std::cmp::max(sized_align, unsized_align) as u64)
}
_ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
sized_align,
unsized_align)
};
let addend = bcx.sub(align, C_uint(bcx.ccx, 1_u64));
let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
(size, align)
}
ty::TyDynamic(..) => {
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]);
let size = bcx.load(size_ptr, None);
let align = bcx.load(align_ptr, None);
bcx.set_invariant_load(size);
bcx.set_invariant_load(align);
(size, align)
}
ty::TySlice(_) | ty::TyStr => {
let unit_ty = t.sequence_element_type(bcx.tcx());
let llunit_ty = sizing_type_of(bcx.ccx, unit_ty);
let unit_align = llalign_of_min(bcx.ccx, llunit_ty);
let unit_size = llsize_of_alloc(bcx.ccx, llunit_ty);
(bcx.mul(info, C_uint(bcx.ccx, unit_size)),
C_uint(bcx.ccx, unit_align))
}
_ => bug!("Unexpected unsized type, found {}", t)
}
}
fn drop_structural_ty<'a, 'tcx>(
cx: Builder<'a, 'tcx>,
mut ptr: LvalueRef<'tcx>
) -> Builder<'a, 'tcx> {
fn iter_variant_fields<'a, 'tcx>(
cx: &'a Builder<'a, 'tcx>,
av: LvalueRef<'tcx>,
adt_def: &'tcx AdtDef,
variant_index: usize,
substs: &'tcx Substs<'tcx>
) {
let variant = &adt_def.variants[variant_index];
let tcx = cx.tcx();
for (i, field) in variant.fields.iter().enumerate() {
let arg = monomorphize::field_ty(tcx, substs, field);
let (field_ptr, align) = av.trans_field_ptr(&cx, i);
drop_ty(&cx, LvalueRef::new_sized_ty(field_ptr, arg, align));
}
}
let mut cx = cx;
let t = ptr.ty.to_ty(cx.tcx());
match t.sty {
ty::TyClosure(def_id, substs) => {
for (i, upvar_ty) in substs.upvar_tys(def_id, cx.tcx()).enumerate() {
let (llupvar, align) = ptr.trans_field_ptr(&cx, i);
drop_ty(&cx, LvalueRef::new_sized_ty(llupvar, upvar_ty, align));
}
}
ty::TyArray(_, n) => {
let base = get_dataptr(&cx, ptr.llval);
let len = C_uint(cx.ccx, n);
let unit_ty = t.sequence_element_type(cx.tcx());
cx = tvec::slice_for_each(&cx, base, unit_ty, len,
|bb, vv| drop_ty(bb, LvalueRef::new_sized_ty(vv, unit_ty, ptr.alignment)));
}
ty::TySlice(_) | ty::TyStr => {
let unit_ty = t.sequence_element_type(cx.tcx());
cx = tvec::slice_for_each(&cx, ptr.llval, unit_ty, ptr.llextra,
|bb, vv| drop_ty(bb, LvalueRef::new_sized_ty(vv, unit_ty, ptr.alignment)));
}
ty::TyTuple(ref args, _) => {
for (i, arg) in args.iter().enumerate() {
let (llfld_a, align) = ptr.trans_field_ptr(&cx, i);
drop_ty(&cx, LvalueRef::new_sized_ty(llfld_a, *arg, align));
}
}
ty::TyAdt(adt, substs) => match adt.adt_kind() {
AdtKind::Struct => {
for (i, field) in adt.variants[0].fields.iter().enumerate() {
let field_ty = monomorphize::field_ty(cx.tcx(), substs, field);
let (llval, align) = ptr.trans_field_ptr(&cx, i);
let field_ptr = if cx.ccx.shared().type_is_sized(field_ty) {
LvalueRef::new_sized_ty(llval, field_ty, align)
} else {
LvalueRef::new_unsized_ty(llval, ptr.llextra, field_ty, align)
};
drop_ty(&cx, field_ptr);
}
}
AdtKind::Union => {
bug!("Union in `glue::drop_structural_ty`");
}
AdtKind::Enum => {
let n_variants = adt.variants.len();
let l = cx.ccx.layout_of(t);
match *l {
layout::Univariant { .. } |
layout::UntaggedUnion { .. } => {
if n_variants != 0 {
assert!(n_variants == 1);
ptr.ty = LvalueTy::Downcast {
adt_def: adt,
substs: substs,
variant_index: 0,
};
iter_variant_fields(&cx, ptr, &adt, 0, substs);
}
}
layout::CEnum { .. } |
layout::General { .. } |
layout::RawNullablePointer { .. } |
layout::StructWrappedNullablePointer { .. } => {
let lldiscrim_a = adt::trans_get_discr(
&cx, t, ptr.llval, ptr.alignment, None, false);
let ret_void_cx = cx.build_sibling_block("enum-iter-ret-void");
ret_void_cx.ret_void();
let llswitch = cx.switch(lldiscrim_a, ret_void_cx.llbb(), n_variants);
let next_cx = cx.build_sibling_block("enum-iter-next");
for (i, discr) in adt.discriminants(cx.tcx()).enumerate() {
let variant_cx_name = format!("enum-iter-variant-{}", i);
let variant_cx = cx.build_sibling_block(&variant_cx_name);
let case_val = adt::trans_case(&cx, t, Disr::from(discr));
variant_cx.add_case(llswitch, case_val, variant_cx.llbb());
ptr.ty = LvalueTy::Downcast {
adt_def: adt,
substs: substs,
variant_index: i,
};
iter_variant_fields(&variant_cx, ptr, &adt, i, substs);
variant_cx.br(next_cx.llbb());
}
cx = next_cx;
}
_ => bug!("{} is not an enum.", t),
}
}
},
_ => {
cx.sess().unimpl(&format!("type in drop_structural_ty: {}", t))
}
}
return cx;
}