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
550
551
552
553
554
555
556
557
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// 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.

//!
//
// Code relating to taking, dropping, etc as well as type descriptors.


use back::abi;
use back::link::*;
use lib::llvm::{llvm, ValueRef, True};
use lib;
use middle::lang_items::{FreeFnLangItem, ExchangeFreeFnLangItem};
use middle::subst;
use middle::trans::adt;
use middle::trans::base::*;
use middle::trans::build::*;
use middle::trans::callee;
use middle::trans::cleanup;
use middle::trans::cleanup::CleanupMethods;
use middle::trans::common::*;
use middle::trans::expr;
use middle::trans::machine::*;
use middle::trans::reflect;
use middle::trans::tvec;
use middle::trans::type_::Type;
use middle::trans::type_of::{type_of, sizing_type_of};
use middle::ty;
use util::ppaux::ty_to_short_str;
use util::ppaux;

use arena::TypedArena;
use std::c_str::ToCStr;
use std::cell::Cell;
use libc::c_uint;
use syntax::ast;
use syntax::parse::token;

pub fn trans_free<'a>(cx: &'a Block<'a>, v: ValueRef) -> &'a Block<'a> {
    let _icx = push_ctxt("trans_free");
    callee::trans_lang_call(cx,
        langcall(cx, None, "", FreeFnLangItem),
        [PointerCast(cx, v, Type::i8p(cx.ccx()))],
        Some(expr::Ignore)).bcx
}

fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef, size: u64,
                               align: u64) -> &'a Block<'a> {
    let _icx = push_ctxt("trans_exchange_free");
    let ccx = cx.ccx();
    callee::trans_lang_call(cx,
        langcall(cx, None, "", ExchangeFreeFnLangItem),
        [PointerCast(cx, v, Type::i8p(ccx)), C_uint(ccx, size as uint), C_uint(ccx, align as uint)],
        Some(expr::Ignore)).bcx
}

pub fn trans_exchange_free_ty<'a>(bcx: &'a Block<'a>, ptr: ValueRef,
                                  content_ty: ty::t) -> &'a Block<'a> {
    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 {
        let content_align = llalign_of_min(bcx.ccx(), sizing_type);
        trans_exchange_free(bcx, ptr, content_size, content_align)
    } else {
        bcx
    }
}

pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
               -> &'a Block<'a> {
    // NB: v is an *alias* of type t here, not a direct value.
    let _icx = push_ctxt("take_ty");
    match ty::get(t).sty {
        ty::ty_box(_) => incr_refcnt_of_boxed(bcx, v),
        _ if ty::type_is_structural(t)
          && ty::type_needs_drop(bcx.tcx(), t) => {
            iter_structural_ty(bcx, v, t, take_ty)
        }
        _ => bcx
    }
}

pub fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {
    let tcx = ccx.tcx();
    if !ty::type_needs_drop(tcx, t) {
        return ty::mk_i8();
    }
    match ty::get(t).sty {
        ty::ty_box(typ) if !ty::type_needs_drop(tcx, typ) =>
            ty::mk_box(tcx, ty::mk_i8()),

        ty::ty_uniq(typ) if !ty::type_needs_drop(tcx, typ) => {
            match ty::get(typ).sty {
                ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => t,
                _ => {
                    let llty = sizing_type_of(ccx, typ);
                    // `Box<ZeroSizeType>` does not allocate.
                    if llsize_of_alloc(ccx, llty) == 0 {
                        ty::mk_i8()
                    } else {
                        ty::mk_uniq(tcx, ty::mk_i8())
                    }
                }
            }
        }
        _ => t
    }
}

pub fn drop_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
               -> &'a Block<'a> {
    // NB: v is an *alias* of type t here, not a direct value.
    let _icx = push_ctxt("drop_ty");
    let ccx = bcx.ccx();
    if ty::type_needs_drop(bcx.tcx(), t) {
        let glue = get_drop_glue(ccx, t);
        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
        };
        Call(bcx, glue, [ptr], []);
    }
    bcx
}

pub fn drop_ty_immediate<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
                         -> &'a Block<'a> {
    let _icx = push_ctxt("drop_ty_immediate");
    let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
    Store(bcx, v, vp);
    drop_ty(bcx, vp, t)
}

pub fn get_drop_glue(ccx: &CrateContext, t: ty::t) -> ValueRef {
    let t = get_drop_glue_type(ccx, t);
    match ccx.drop_glues.borrow().find(&t) {
        Some(&glue) => return glue,
        _ => { }
    }

    let llfnty = Type::glue_fn(ccx, type_of(ccx, t).ptr_to());
    let glue = declare_generic_glue(ccx, t, llfnty, "drop");

    ccx.drop_glues.borrow_mut().insert(t, glue);

    make_generic_glue(ccx, t, glue, make_drop_glue, "drop");

    glue
}

pub fn lazily_emit_visit_glue(ccx: &CrateContext, ti: &tydesc_info) -> ValueRef {
    let _icx = push_ctxt("lazily_emit_visit_glue");

    let llfnty = Type::glue_fn(ccx, type_of(ccx, ti.ty).ptr_to());

    match ti.visit_glue.get() {
        Some(visit_glue) => visit_glue,
        None => {
            debug!("+++ lazily_emit_tydesc_glue VISIT {}", ppaux::ty_to_str(ccx.tcx(), ti.ty));
            let glue_fn = declare_generic_glue(ccx, ti.ty, llfnty, "visit");
            ti.visit_glue.set(Some(glue_fn));
            make_generic_glue(ccx, ti.ty, glue_fn, make_visit_glue, "visit");
            debug!("--- lazily_emit_tydesc_glue VISIT {}", ppaux::ty_to_str(ccx.tcx(), ti.ty));
            glue_fn
        }
    }
}

// See [Note-arg-mode]
pub fn call_visit_glue(bcx: &Block, v: ValueRef, tydesc: ValueRef,
                       static_ti: Option<&tydesc_info>) {
    let _icx = push_ctxt("call_tydesc_glue_full");
    let ccx = bcx.ccx();
    // NB: Don't short-circuit even if this block is unreachable because
    // GC-based cleanup needs to the see that the roots are live.
    if bcx.unreachable.get() && !ccx.sess().no_landing_pads() { return; }

    let static_glue_fn = static_ti.map(|sti| lazily_emit_visit_glue(ccx, sti));

    // When static type info is available, avoid casting to a generic pointer.
    let llrawptr = if static_glue_fn.is_none() {
        PointerCast(bcx, v, Type::i8p(ccx))
    } else {
        v
    };

    let llfn = {
        match static_glue_fn {
            None => {
                // Select out the glue function to call from the tydesc
                let llfnptr = GEPi(bcx, tydesc, [0u, abi::tydesc_field_visit_glue]);
                Load(bcx, llfnptr)
            }
            Some(sgf) => sgf
        }
    };

    Call(bcx, llfn, [llrawptr], []);
}

fn make_visit_glue<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
                   -> &'a Block<'a> {
    let _icx = push_ctxt("make_visit_glue");
    let mut bcx = bcx;
    let (visitor_trait, object_ty) = match ty::visitor_object_ty(bcx.tcx(),
                                                                 ty::ReStatic) {
        Ok(pair) => pair,
        Err(s) => {
            bcx.tcx().sess.fatal(s.as_slice());
        }
    };
    let v = PointerCast(bcx, v, type_of(bcx.ccx(), object_ty).ptr_to());
    bcx = reflect::emit_calls_to_trait_visit_ty(bcx, t, v, visitor_trait.def_id);
    bcx
}

fn trans_struct_drop_flag<'a>(bcx: &'a Block<'a>,
                              t: ty::t,
                              v0: ValueRef,
                              dtor_did: ast::DefId,
                              class_did: ast::DefId,
                              substs: &subst::Substs)
                              -> &'a Block<'a> {
    let repr = adt::represent_type(bcx.ccx(), t);
    let drop_flag = adt::trans_drop_flag_ptr(bcx, &*repr, v0);
    with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag)), |cx| {
        trans_struct_drop(cx, t, v0, dtor_did, class_did, substs)
    })
}

fn trans_struct_drop<'a>(bcx: &'a Block<'a>,
                         t: ty::t,
                         v0: ValueRef,
                         dtor_did: ast::DefId,
                         class_did: ast::DefId,
                         substs: &subst::Substs)
                         -> &'a Block<'a> {
    let repr = adt::represent_type(bcx.ccx(), t);

    // Find and call the actual destructor
    let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, t,
                                 class_did, substs);

    // The second argument is the "self" argument for drop
    let params = unsafe {
        let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
        ty.element_type().func_params()
    };

    // Class dtors have no explicit args, so the params should
    // just consist of the environment (self)
    assert_eq!(params.len(), 1);

    // Be sure to put all of the fields 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 fails.
    let field_scope = bcx.fcx.push_custom_cleanup_scope();

    let self_arg = PointerCast(bcx, v0, *params.get(0));
    let args = vec!(self_arg);

    // Add all the fields as a value which needs to be cleaned at the end of
    // this scope.
    let field_tys = ty::struct_fields(bcx.tcx(), class_did, substs);
    for (i, fld) in field_tys.iter().enumerate() {
        let llfld_a = adt::trans_field_ptr(bcx, &*repr, v0, 0, i);
        bcx.fcx.schedule_drop_mem(cleanup::CustomScope(field_scope),
                                  llfld_a,
                                  fld.mt.ty);
    }

    let dtor_ty = ty::mk_ctor_fn(bcx.tcx(), ast::DUMMY_NODE_ID,
                                 [get_drop_glue_type(bcx.ccx(), t)], ty::mk_nil());
    let (_, bcx) = invoke(bcx, dtor_addr, args, dtor_ty, None);

    bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, field_scope)
}

fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'a> {
    // NB: v0 is an *alias* of type t here, not a direct value.
    let _icx = push_ctxt("make_drop_glue");
    match ty::get(t).sty {
        ty::ty_box(body_ty) => {
            decr_refcnt_maybe_free(bcx, v0, body_ty)
        }
        ty::ty_uniq(content_ty) => {
            match ty::get(content_ty).sty {
                ty::ty_vec(mt, None) => {
                    let llbox = Load(bcx, v0);
                    let not_null = IsNotNull(bcx, llbox);
                    with_cond(bcx, not_null, |bcx| {
                        let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, mt.ty);
                        // FIXME: #13994: the old `Box<[T]>` will not support sized deallocation
                        trans_exchange_free(bcx, llbox, 0, 8)
                    })
                }
                ty::ty_str => {
                    let llbox = Load(bcx, v0);
                    let not_null = IsNotNull(bcx, llbox);
                    with_cond(bcx, not_null, |bcx| {
                        let unit_ty = ty::sequence_element_type(bcx.tcx(), t);
                        let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, unit_ty);
                        // FIXME: #13994: the old `Box<str>` will not support sized deallocation
                        trans_exchange_free(bcx, llbox, 0, 8)
                    })
                }
                ty::ty_trait(..) => {
                    let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
                    // Only drop the value when it is non-null
                    with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
                        let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
                        let dtor = Load(bcx, dtor_ptr);
                        Call(bcx,
                             dtor,
                             [PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
                             []);
                        bcx
                    })
                }
                _ => {
                    let llbox = Load(bcx, v0);
                    let not_null = IsNotNull(bcx, llbox);
                    with_cond(bcx, not_null, |bcx| {
                        let bcx = drop_ty(bcx, llbox, content_ty);
                        trans_exchange_free_ty(bcx, llbox, content_ty)
                    })
                }
            }
        }
        ty::ty_struct(did, ref substs) => {
            let tcx = bcx.tcx();
            match ty::ty_dtor(tcx, did) {
                ty::TraitDtor(dtor, true) => {
                    trans_struct_drop_flag(bcx, t, v0, dtor, did, substs)
                }
                ty::TraitDtor(dtor, false) => {
                    trans_struct_drop(bcx, t, v0, dtor, did, substs)
                }
                ty::NoDtor => {
                    // No dtor? Just the default case
                    iter_structural_ty(bcx, v0, t, drop_ty)
                }
            }
        }
        ty::ty_closure(ref f) if f.store == ty::UniqTraitStore => {
            let box_cell_v = GEPi(bcx, v0, [0u, abi::fn_field_box]);
            let env = Load(bcx, box_cell_v);
            let env_ptr_ty = Type::at_box(bcx.ccx(), Type::i8(bcx.ccx())).ptr_to();
            let env = PointerCast(bcx, env, env_ptr_ty);
            with_cond(bcx, IsNotNull(bcx, env), |bcx| {
                let dtor_ptr = GEPi(bcx, env, [0u, abi::box_field_tydesc]);
                let dtor = Load(bcx, dtor_ptr);
                let cdata = GEPi(bcx, env, [0u, abi::box_field_body]);
                Call(bcx, dtor, [PointerCast(bcx, cdata, Type::i8p(bcx.ccx()))], []);

                // Free the environment itself
                // FIXME: #13994: pass align and size here
                trans_exchange_free(bcx, env, 0, 8)
            })
        }
        _ => {
            if ty::type_needs_drop(bcx.tcx(), t) &&
                ty::type_is_structural(t) {
                iter_structural_ty(bcx, v0, t, drop_ty)
            } else {
                bcx
            }
        }
    }
}

fn decr_refcnt_maybe_free<'a>(bcx: &'a Block<'a>,
                              box_ptr_ptr: ValueRef,
                              t: ty::t) -> &'a Block<'a> {
    let _icx = push_ctxt("decr_refcnt_maybe_free");
    let fcx = bcx.fcx;
    let ccx = bcx.ccx();

    let decr_bcx = fcx.new_temp_block("decr");
    let free_bcx = fcx.new_temp_block("free");
    let next_bcx = fcx.new_temp_block("next");

    let box_ptr = Load(bcx, box_ptr_ptr);
    let llnotnull = IsNotNull(bcx, box_ptr);
    CondBr(bcx, llnotnull, decr_bcx.llbb, next_bcx.llbb);

    let rc_ptr = GEPi(decr_bcx, box_ptr, [0u, abi::box_field_refcnt]);
    let rc = Sub(decr_bcx, Load(decr_bcx, rc_ptr), C_int(ccx, 1));
    Store(decr_bcx, rc, rc_ptr);
    CondBr(decr_bcx, IsNull(decr_bcx, rc), free_bcx.llbb, next_bcx.llbb);

    let v = Load(free_bcx, box_ptr_ptr);
    let body = GEPi(free_bcx, v, [0u, abi::box_field_body]);
    let free_bcx = drop_ty(free_bcx, body, t);
    let free_bcx = trans_free(free_bcx, v);
    Br(free_bcx, next_bcx.llbb);

    next_bcx
}

fn incr_refcnt_of_boxed<'a>(bcx: &'a Block<'a>,
                            box_ptr_ptr: ValueRef) -> &'a Block<'a> {
    let _icx = push_ctxt("incr_refcnt_of_boxed");
    let ccx = bcx.ccx();
    let box_ptr = Load(bcx, box_ptr_ptr);
    let rc_ptr = GEPi(bcx, box_ptr, [0u, abi::box_field_refcnt]);
    let rc = Load(bcx, rc_ptr);
    let rc = Add(bcx, rc, C_int(ccx, 1));
    Store(bcx, rc, rc_ptr);
    bcx
}


// Generates the declaration for (but doesn't emit) a type descriptor.
pub fn declare_tydesc(ccx: &CrateContext, t: ty::t) -> tydesc_info {
    // If emit_tydescs already ran, then we shouldn't be creating any new
    // tydescs.
    assert!(!ccx.finished_tydescs.get());

    let llty = type_of(ccx, t);

    if ccx.sess().count_type_sizes() {
        println!("{}\t{}", llsize_of_real(ccx, llty),
                 ppaux::ty_to_str(ccx.tcx(), t));
    }

    let llsize = llsize_of(ccx, llty);
    let llalign = llalign_of(ccx, llty);
    let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc");
    debug!("+++ declare_tydesc {} {}", ppaux::ty_to_str(ccx.tcx(), t), name);
    let gvar = name.as_slice().with_c_str(|buf| {
        unsafe {
            llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type().to_ref(), buf)
        }
    });
    note_unique_llvm_symbol(ccx, name);

    let ty_name = token::intern_and_get_ident(
        ppaux::ty_to_str(ccx.tcx(), t).as_slice());
    let ty_name = C_str_slice(ccx, ty_name);

    debug!("--- declare_tydesc {}", ppaux::ty_to_str(ccx.tcx(), t));
    tydesc_info {
        ty: t,
        tydesc: gvar,
        size: llsize,
        align: llalign,
        name: ty_name,
        visit_glue: Cell::new(None),
    }
}

fn declare_generic_glue(ccx: &CrateContext, t: ty::t, llfnty: Type,
                        name: &str) -> ValueRef {
    let _icx = push_ctxt("declare_generic_glue");
    let fn_nm = mangle_internal_name_by_type_and_seq(
        ccx,
        t,
        format!("glue_{}", name).as_slice());
    debug!("{} is for type {}", fn_nm, ppaux::ty_to_str(ccx.tcx(), t));
    let llfn = decl_cdecl_fn(ccx, fn_nm.as_slice(), llfnty, ty::mk_nil());
    note_unique_llvm_symbol(ccx, fn_nm);
    return llfn;
}

fn make_generic_glue(ccx: &CrateContext,
                     t: ty::t,
                     llfn: ValueRef,
                     helper: <'a> |&'a Block<'a>, ValueRef, ty::t|
                                  -> &'a Block<'a>,
                     name: &str)
                     -> ValueRef {
    let _icx = push_ctxt("make_generic_glue");
    let glue_name = format!("glue {} {}", name, ty_to_short_str(ccx.tcx(), t));
    let _s = StatRecorder::new(ccx, glue_name);

    let arena = TypedArena::new();
    let empty_param_substs = param_substs::empty();
    let fcx = new_fn_ctxt(ccx, llfn, -1, false, ty::mk_nil(),
                          &empty_param_substs, None, &arena);

    init_function(&fcx, false, ty::mk_nil());

    lib::llvm::SetLinkage(llfn, lib::llvm::InternalLinkage);
    ccx.stats.n_glues_created.set(ccx.stats.n_glues_created.get() + 1u);
    // 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.

    let bcx = fcx.entry_bcx.borrow().clone().unwrap();
    let llrawptr0 = unsafe { llvm::LLVMGetParam(llfn, fcx.arg_pos(0) as c_uint) };
    let bcx = helper(bcx, llrawptr0, t);
    finish_fn(&fcx, bcx);

    llfn
}

pub fn emit_tydescs(ccx: &CrateContext) {
    let _icx = push_ctxt("emit_tydescs");
    // As of this point, allow no more tydescs to be created.
    ccx.finished_tydescs.set(true);
    let glue_fn_ty = Type::generic_glue_fn(ccx).ptr_to();
    for (_, ti) in ccx.tydescs.borrow().iter() {
        // Each of the glue functions needs to be cast to a generic type
        // before being put into the tydesc because we only have a singleton
        // tydesc type. Then we'll recast each function to its real type when
        // calling it.
        let drop_glue = unsafe {
            llvm::LLVMConstPointerCast(get_drop_glue(ccx, ti.ty), glue_fn_ty.to_ref())
        };
        ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() + 1);
        let visit_glue =
            match ti.visit_glue.get() {
              None => {
                  ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
                                             1u);
                  C_null(glue_fn_ty)
              }
              Some(v) => {
                unsafe {
                    ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
                                               1);
                    llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
                }
              }
            };

        let tydesc = C_named_struct(ccx.tydesc_type(),
                                    [ti.size, // size
                                     ti.align, // align
                                     drop_glue, // drop_glue
                                     visit_glue, // visit_glue
                                     ti.name]); // name

        unsafe {
            let gvar = ti.tydesc;
            llvm::LLVMSetInitializer(gvar, tydesc);
            llvm::LLVMSetGlobalConstant(gvar, True);
            lib::llvm::SetLinkage(gvar, lib::llvm::InternalLinkage);
        }
    };
}