]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/glue.rs
librustc: Fix merge fallout.
[rust.git] / src / librustc / middle / 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 taking, dropping, etc as well as type descriptors.
14
15
16 use back::abi;
17 use back::link::*;
18 use lib::llvm::{llvm, ValueRef, True};
19 use lib;
20 use middle::lang_items::{FreeFnLangItem, ExchangeFreeFnLangItem};
21 use middle::trans::adt;
22 use middle::trans::base::*;
23 use middle::trans::build::*;
24 use middle::trans::callee;
25 use middle::trans::cleanup;
26 use middle::trans::cleanup::CleanupMethods;
27 use middle::trans::common::*;
28 use middle::trans::expr;
29 use middle::trans::machine::*;
30 use middle::trans::reflect;
31 use middle::trans::tvec;
32 use middle::trans::type_::Type;
33 use middle::trans::type_of::type_of;
34 use middle::ty;
35 use util::ppaux::ty_to_short_str;
36 use util::ppaux;
37
38 use arena::TypedArena;
39 use std::c_str::ToCStr;
40 use std::cell::Cell;
41 use std::libc::c_uint;
42 use syntax::ast;
43 use syntax::parse::token;
44
45 pub fn trans_free<'a>(cx: &'a Block<'a>, v: ValueRef) -> &'a Block<'a> {
46     let _icx = push_ctxt("trans_free");
47     callee::trans_lang_call(cx,
48         langcall(cx, None, "", FreeFnLangItem),
49         [PointerCast(cx, v, Type::i8p())],
50         Some(expr::Ignore)).bcx
51 }
52
53 pub fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef)
54                            -> &'a Block<'a> {
55     let _icx = push_ctxt("trans_exchange_free");
56     callee::trans_lang_call(cx,
57         langcall(cx, None, "", ExchangeFreeFnLangItem),
58         [PointerCast(cx, v, Type::i8p())],
59         Some(expr::Ignore)).bcx
60 }
61
62 pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
63                -> &'a Block<'a> {
64     // NB: v is an *alias* of type t here, not a direct value.
65     let _icx = push_ctxt("take_ty");
66     match ty::get(t).sty {
67         ty::ty_box(_) |
68         ty::ty_vec(_, ty::vstore_box) | ty::ty_str(ty::vstore_box) => {
69             incr_refcnt_of_boxed(bcx, v)
70         }
71         ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
72             incr_refcnt_of_boxed(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]))
73         }
74         _ if ty::type_is_structural(t)
75           && ty::type_needs_drop(bcx.tcx(), t) => {
76             iter_structural_ty(bcx, v, t, take_ty)
77         }
78         _ => bcx
79     }
80 }
81
82 pub fn drop_ty<'a>(cx: &'a Block<'a>, v: ValueRef, t: ty::t)
83                -> &'a Block<'a> {
84     // NB: v is an *alias* of type t here, not a direct value.
85     let _icx = push_ctxt("drop_ty");
86     if ty::type_needs_drop(cx.tcx(), t) {
87         return call_tydesc_glue(cx, v, t, abi::tydesc_field_drop_glue);
88     }
89     return cx;
90 }
91
92 pub fn drop_ty_immediate<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
93                          -> &'a Block<'a> {
94     let _icx = push_ctxt("drop_ty_immediate");
95     let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
96     Store(bcx, v, vp);
97     drop_ty(bcx, vp, t)
98 }
99
100 pub fn lazily_emit_all_tydesc_glue(ccx: @CrateContext,
101                                    static_ti: @tydesc_info) {
102     lazily_emit_tydesc_glue(ccx, abi::tydesc_field_drop_glue, static_ti);
103     lazily_emit_tydesc_glue(ccx, abi::tydesc_field_visit_glue, static_ti);
104 }
105
106 fn simplified_glue_type(tcx: ty::ctxt, field: uint, t: ty::t) -> ty::t {
107     if field == abi::tydesc_field_drop_glue {
108         if !ty::type_needs_drop(tcx, t) {
109             return ty::mk_nil();
110         }
111         match ty::get(t).sty {
112             ty::ty_box(typ)
113                 if !ty::type_needs_drop(tcx, typ) =>
114             return ty::mk_box(tcx, ty::mk_nil()),
115
116             ty::ty_vec(mt, ty::vstore_box)
117                 if !ty::type_needs_drop(tcx, mt.ty) =>
118             return ty::mk_box(tcx, ty::mk_nil()),
119
120             ty::ty_uniq(typ)
121                 if !ty::type_needs_drop(tcx, typ) =>
122             return ty::mk_uniq(tcx, ty::mk_nil()),
123
124             ty::ty_vec(mt, ty::vstore_uniq)
125                 if !ty::type_needs_drop(tcx, mt.ty) =>
126             return ty::mk_uniq(tcx, ty::mk_nil()),
127
128             _ => {}
129         }
130     }
131
132     t
133 }
134
135 pub fn lazily_emit_tydesc_glue(ccx: @CrateContext, field: uint, ti: @tydesc_info) {
136     let _icx = push_ctxt("lazily_emit_tydesc_glue");
137
138     let simpl = simplified_glue_type(ccx.tcx, field, ti.ty);
139     if simpl != ti.ty {
140         let _icx = push_ctxt("lazily_emit_simplified_tydesc_glue");
141         let simpl_ti = get_tydesc(ccx, simpl);
142         lazily_emit_tydesc_glue(ccx, field, simpl_ti);
143
144         if field == abi::tydesc_field_drop_glue {
145             ti.drop_glue.set(simpl_ti.drop_glue.get());
146         } else if field == abi::tydesc_field_visit_glue {
147             ti.visit_glue.set(simpl_ti.visit_glue.get());
148         }
149
150         return;
151     }
152
153     let llfnty = Type::glue_fn(type_of(ccx, ti.ty).ptr_to());
154
155     if field == abi::tydesc_field_drop_glue {
156         match ti.drop_glue.get() {
157           Some(_) => (),
158           None => {
159             debug!("+++ lazily_emit_tydesc_glue DROP {}",
160                    ppaux::ty_to_str(ccx.tcx, ti.ty));
161             let glue_fn = declare_generic_glue(ccx, ti.ty, llfnty, "drop");
162             ti.drop_glue.set(Some(glue_fn));
163             make_generic_glue(ccx, ti.ty, glue_fn, make_drop_glue, "drop");
164             debug!("--- lazily_emit_tydesc_glue DROP {}",
165                    ppaux::ty_to_str(ccx.tcx, ti.ty));
166           }
167         }
168     } else if field == abi::tydesc_field_visit_glue {
169         match ti.visit_glue.get() {
170           Some(_) => (),
171           None => {
172             debug!("+++ lazily_emit_tydesc_glue VISIT {}",
173                    ppaux::ty_to_str(ccx.tcx, ti.ty));
174             let glue_fn = declare_generic_glue(ccx, ti.ty, llfnty, "visit");
175             ti.visit_glue.set(Some(glue_fn));
176             make_generic_glue(ccx, ti.ty, glue_fn, make_visit_glue, "visit");
177             debug!("--- lazily_emit_tydesc_glue VISIT {}",
178                    ppaux::ty_to_str(ccx.tcx, ti.ty));
179           }
180         }
181     }
182 }
183
184 // See [Note-arg-mode]
185 pub fn call_tydesc_glue_full(bcx: &Block, v: ValueRef, tydesc: ValueRef,
186                              field: uint, static_ti: Option<@tydesc_info>) {
187     let _icx = push_ctxt("call_tydesc_glue_full");
188     let ccx = bcx.ccx();
189     // NB: Don't short-circuit even if this block is unreachable because
190     // GC-based cleanup needs to the see that the roots are live.
191     if bcx.unreachable.get() && !ccx.sess.no_landing_pads() { return; }
192
193     let static_glue_fn = match static_ti {
194         None => None,
195         Some(sti) => {
196             lazily_emit_tydesc_glue(ccx, field, sti);
197             if field == abi::tydesc_field_drop_glue {
198                 sti.drop_glue.get()
199             } else if field == abi::tydesc_field_visit_glue {
200                 sti.visit_glue.get()
201             } else {
202                 None
203             }
204         }
205     };
206
207     // When static type info is available, avoid casting parameter unless the
208     // glue is using a simplified type, because the function already has the
209     // right type. Otherwise cast to generic pointer.
210     let llrawptr = if static_glue_fn.is_none() {
211         PointerCast(bcx, v, Type::i8p())
212     } else {
213         let ty = static_ti.unwrap().ty;
214         let simpl = simplified_glue_type(ccx.tcx, field, ty);
215         if simpl != ty {
216             PointerCast(bcx, v, type_of(ccx, simpl).ptr_to())
217         } else {
218             v
219         }
220     };
221
222     let llfn = {
223         match static_glue_fn {
224             None => {
225                 // Select out the glue function to call from the tydesc
226                 let llfnptr = GEPi(bcx, tydesc, [0u, field]);
227                 Load(bcx, llfnptr)
228             }
229             Some(sgf) => sgf
230         }
231     };
232
233     Call(bcx, llfn, [llrawptr], []);
234 }
235
236 // See [Note-arg-mode]
237 fn call_tydesc_glue<'a>(cx: &'a Block<'a>, v: ValueRef, t: ty::t, field: uint)
238                     -> &'a Block<'a> {
239     let _icx = push_ctxt("call_tydesc_glue");
240     let ti = get_tydesc(cx.ccx(), t);
241     call_tydesc_glue_full(cx, v, ti.tydesc, field, Some(ti));
242     cx
243 }
244
245 fn make_visit_glue<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
246                    -> &'a Block<'a> {
247     let _icx = push_ctxt("make_visit_glue");
248     let mut bcx = bcx;
249     let (visitor_trait, object_ty) = match ty::visitor_object_ty(bcx.tcx(),
250                                                                  ty::ReStatic) {
251         Ok(pair) => pair,
252         Err(s) => {
253             bcx.tcx().sess.fatal(s);
254         }
255     };
256     let v = PointerCast(bcx, v, type_of(bcx.ccx(), object_ty).ptr_to());
257     bcx = reflect::emit_calls_to_trait_visit_ty(bcx, t, v, visitor_trait.def_id);
258     bcx
259 }
260
261 fn trans_struct_drop_flag<'a>(bcx: &'a Block<'a>,
262                               t: ty::t,
263                               v0: ValueRef,
264                               dtor_did: ast::DefId,
265                               class_did: ast::DefId,
266                               substs: &ty::substs)
267                               -> &'a Block<'a> {
268     let repr = adt::represent_type(bcx.ccx(), t);
269     let drop_flag = adt::trans_drop_flag_ptr(bcx, repr, v0);
270     with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag)), |cx| {
271         trans_struct_drop(cx, t, v0, dtor_did, class_did, substs)
272     })
273 }
274
275 fn trans_struct_drop<'a>(bcx: &'a Block<'a>,
276                          t: ty::t,
277                          v0: ValueRef,
278                          dtor_did: ast::DefId,
279                          class_did: ast::DefId,
280                          substs: &ty::substs)
281                          -> &'a Block<'a> {
282     let repr = adt::represent_type(bcx.ccx(), t);
283
284     // Find and call the actual destructor
285     let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did,
286                                  class_did, substs.tps.clone());
287
288     // The second argument is the "self" argument for drop
289     let params = unsafe {
290         let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
291         ty.element_type().func_params()
292     };
293
294     // Class dtors have no explicit args, so the params should
295     // just consist of the environment (self)
296     assert_eq!(params.len(), 1);
297
298     // Be sure to put all of the fields into a scope so we can use an invoke
299     // instruction to call the user destructor but still call the field
300     // destructors if the user destructor fails.
301     let field_scope = bcx.fcx.push_custom_cleanup_scope();
302
303     let self_arg = PointerCast(bcx, v0, params[0]);
304     let args = ~[self_arg];
305
306     // Add all the fields as a value which needs to be cleaned at the end of
307     // this scope.
308     let field_tys = ty::struct_fields(bcx.tcx(), class_did, substs);
309     for (i, fld) in field_tys.iter().enumerate() {
310         let llfld_a = adt::trans_field_ptr(bcx, repr, v0, 0, i);
311         bcx.fcx.schedule_drop_mem(cleanup::CustomScope(field_scope),
312                                   llfld_a,
313                                   fld.mt.ty);
314     }
315
316     let (_, bcx) = invoke(bcx, dtor_addr, args, [], None);
317
318     bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, field_scope)
319 }
320
321 fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'a> {
322     // NB: v0 is an *alias* of type t here, not a direct value.
323     let _icx = push_ctxt("make_drop_glue");
324     let ccx = bcx.ccx();
325     match ty::get(t).sty {
326         ty::ty_box(body_ty) => {
327             decr_refcnt_maybe_free(bcx, v0, Some(body_ty))
328         }
329         ty::ty_str(ty::vstore_box) | ty::ty_vec(_, ty::vstore_box) => {
330             let unit_ty = ty::sequence_element_type(ccx.tcx, t);
331             let unboxed_vec_ty = ty::mk_mut_unboxed_vec(ccx.tcx, unit_ty);
332             decr_refcnt_maybe_free(bcx, v0, Some(unboxed_vec_ty))
333         }
334         ty::ty_uniq(content_ty) => {
335             let llbox = Load(bcx, v0);
336             let not_null = IsNotNull(bcx, llbox);
337             with_cond(bcx, not_null, |bcx| {
338                 let bcx = drop_ty(bcx, llbox, content_ty);
339                 trans_exchange_free(bcx, llbox)
340             })
341         }
342         ty::ty_vec(_, ty::vstore_uniq) | ty::ty_str(ty::vstore_uniq) => {
343             make_drop_glue(bcx, v0, tvec::expand_boxed_vec_ty(bcx.tcx(), t))
344         }
345         ty::ty_unboxed_vec(_) => {
346             tvec::make_drop_glue_unboxed(bcx, v0, t)
347         }
348         ty::ty_struct(did, ref substs) => {
349             let tcx = bcx.tcx();
350             match ty::ty_dtor(tcx, did) {
351                 ty::TraitDtor(dtor, true) => {
352                     trans_struct_drop_flag(bcx, t, v0, dtor, did, substs)
353                 }
354                 ty::TraitDtor(dtor, false) => {
355                     trans_struct_drop(bcx, t, v0, dtor, did, substs)
356                 }
357                 ty::NoDtor => {
358                     // No dtor? Just the default case
359                     iter_structural_ty(bcx, v0, t, drop_ty)
360                 }
361             }
362         }
363         ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
364             let llbox_ptr = GEPi(bcx, v0, [0u, abi::trt_field_box]);
365             decr_refcnt_maybe_free(bcx, llbox_ptr, None)
366         }
367         ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
368             let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
369             // Only drop the value when it is non-null
370             with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
371                 let lldtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
372                 let lldtor = Load(bcx, lldtor_ptr);
373                 Call(bcx, lldtor, [PointerCast(bcx, lluniquevalue, Type::i8p())], []);
374                 bcx
375             })
376         }
377         ty::ty_closure(ref f) if f.sigil == ast::OwnedSigil => {
378             let box_cell_v = GEPi(bcx, v0, [0u, abi::fn_field_box]);
379             let env = Load(bcx, box_cell_v);
380             let env_ptr_ty = Type::at_box(ccx, Type::i8()).ptr_to();
381             let env = PointerCast(bcx, env, env_ptr_ty);
382             with_cond(bcx, IsNotNull(bcx, env), |bcx| {
383                 // Load the type descr found in the env
384                 let lltydescty = ccx.tydesc_type.ptr_to();
385                 let tydescptr = GEPi(bcx, env, [0u, abi::box_field_tydesc]);
386                 let tydesc = Load(bcx, tydescptr);
387                 let tydesc = PointerCast(bcx, tydesc, lltydescty);
388
389                 // Drop the tuple data then free the descriptor
390                 let cdata = GEPi(bcx, env, [0u, abi::box_field_body]);
391                 call_tydesc_glue_full(bcx, cdata, tydesc,
392                                       abi::tydesc_field_drop_glue, None);
393
394                 // Free the ty descr (if necc) and the env itself
395                 trans_exchange_free(bcx, env)
396             })
397         }
398         _ => {
399             if ty::type_needs_drop(ccx.tcx, t) &&
400                 ty::type_is_structural(t) {
401                 iter_structural_ty(bcx, v0, t, drop_ty)
402             } else {
403                 bcx
404             }
405         }
406     }
407 }
408
409 fn decr_refcnt_maybe_free<'a>(bcx: &'a Block<'a>, box_ptr_ptr: ValueRef,
410                               t: Option<ty::t>) -> &'a Block<'a> {
411     let _icx = push_ctxt("decr_refcnt_maybe_free");
412     let fcx = bcx.fcx;
413     let ccx = bcx.ccx();
414
415     let decr_bcx = fcx.new_temp_block("decr");
416     let free_bcx = fcx.new_temp_block("free");
417     let next_bcx = fcx.new_temp_block("next");
418
419     let box_ptr = Load(bcx, box_ptr_ptr);
420     let llnotnull = IsNotNull(bcx, box_ptr);
421     CondBr(bcx, llnotnull, decr_bcx.llbb, next_bcx.llbb);
422
423     let rc_ptr = GEPi(decr_bcx, box_ptr, [0u, abi::box_field_refcnt]);
424     let rc = Sub(decr_bcx, Load(decr_bcx, rc_ptr), C_int(ccx, 1));
425     Store(decr_bcx, rc, rc_ptr);
426     CondBr(decr_bcx, IsNull(decr_bcx, rc), free_bcx.llbb, next_bcx.llbb);
427
428     let v = Load(free_bcx, box_ptr_ptr);
429     let body = GEPi(free_bcx, v, [0u, abi::box_field_body]);
430     let free_bcx = match t {
431         Some(t) => drop_ty(free_bcx, body, t),
432         None => {
433             // Generate code that, dynamically, indexes into the
434             // tydesc and calls the drop glue that got set dynamically
435             let td = Load(free_bcx, GEPi(free_bcx, v, [0u, abi::box_field_tydesc]));
436             call_tydesc_glue_full(free_bcx, body, td, abi::tydesc_field_drop_glue, None);
437             free_bcx
438         }
439     };
440     let free_bcx = trans_free(free_bcx, v);
441     Br(free_bcx, next_bcx.llbb);
442
443     next_bcx
444 }
445
446 fn incr_refcnt_of_boxed<'a>(bcx: &'a Block<'a>,
447                             box_ptr_ptr: ValueRef) -> &'a Block<'a> {
448     let _icx = push_ctxt("incr_refcnt_of_boxed");
449     let ccx = bcx.ccx();
450     let box_ptr = Load(bcx, box_ptr_ptr);
451     let rc_ptr = GEPi(bcx, box_ptr, [0u, abi::box_field_refcnt]);
452     let rc = Load(bcx, rc_ptr);
453     let rc = Add(bcx, rc, C_int(ccx, 1));
454     Store(bcx, rc, rc_ptr);
455     bcx
456 }
457
458
459 // Generates the declaration for (but doesn't emit) a type descriptor.
460 pub fn declare_tydesc(ccx: &CrateContext, t: ty::t) -> @tydesc_info {
461     // If emit_tydescs already ran, then we shouldn't be creating any new
462     // tydescs.
463     assert!(!ccx.finished_tydescs.get());
464
465     let llty = type_of(ccx, t);
466
467     if ccx.sess.count_type_sizes() {
468         println!("{}\t{}", llsize_of_real(ccx, llty),
469                  ppaux::ty_to_str(ccx.tcx, t));
470     }
471
472     let llsize = llsize_of(ccx, llty);
473     let llalign = llalign_of(ccx, llty);
474     let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc").to_managed();
475     note_unique_llvm_symbol(ccx, name);
476     debug!("+++ declare_tydesc {} {}", ppaux::ty_to_str(ccx.tcx, t), name);
477     let gvar = name.with_c_str(|buf| {
478         unsafe {
479             llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type.to_ref(), buf)
480         }
481     });
482
483     let ty_name = token::intern_and_get_ident(ppaux::ty_to_str(ccx.tcx, t));
484     let ty_name = C_str_slice(ccx, ty_name);
485
486     let inf = @tydesc_info {
487         ty: t,
488         tydesc: gvar,
489         size: llsize,
490         align: llalign,
491         name: ty_name,
492         drop_glue: Cell::new(None),
493         visit_glue: Cell::new(None),
494     };
495     debug!("--- declare_tydesc {}", ppaux::ty_to_str(ccx.tcx, t));
496     return inf;
497 }
498
499 fn declare_generic_glue(ccx: &CrateContext, t: ty::t, llfnty: Type,
500                         name: &str) -> ValueRef {
501     let _icx = push_ctxt("declare_generic_glue");
502     let fn_nm = mangle_internal_name_by_type_and_seq(ccx, t, (~"glue_" + name)).to_managed();
503     debug!("{} is for type {}", fn_nm, ppaux::ty_to_str(ccx.tcx, t));
504     note_unique_llvm_symbol(ccx, fn_nm);
505     let llfn = decl_cdecl_fn(ccx.llmod, fn_nm, llfnty, ty::mk_nil());
506     return llfn;
507 }
508
509 fn make_generic_glue(ccx: @CrateContext,
510                      t: ty::t,
511                      llfn: ValueRef,
512                      helper: <'a> |&'a Block<'a>, ValueRef, ty::t|
513                                   -> &'a Block<'a>,
514                      name: &str)
515                      -> ValueRef {
516     let _icx = push_ctxt("make_generic_glue");
517     let glue_name = format!("glue {} {}", name, ty_to_short_str(ccx.tcx, t));
518     let _s = StatRecorder::new(ccx, glue_name);
519
520     let arena = TypedArena::new();
521     let fcx = new_fn_ctxt(ccx, ~[], llfn, -1, false, ty::mk_nil(), None, None,
522                           &arena);
523
524     init_function(&fcx, false, ty::mk_nil(), None);
525
526     lib::llvm::SetLinkage(llfn, lib::llvm::InternalLinkage);
527     ccx.stats.n_glues_created.set(ccx.stats.n_glues_created.get() + 1u);
528     // All glue functions take values passed *by alias*; this is a
529     // requirement since in many contexts glue is invoked indirectly and
530     // the caller has no idea if it's dealing with something that can be
531     // passed by value.
532     //
533     // llfn is expected be declared to take a parameter of the appropriate
534     // type, so we don't need to explicitly cast the function parameter.
535
536     let bcx = fcx.entry_bcx.get().unwrap();
537     let llrawptr0 = unsafe { llvm::LLVMGetParam(llfn, fcx.arg_pos(0) as c_uint) };
538     let bcx = helper(bcx, llrawptr0, t);
539     finish_fn(&fcx, bcx);
540
541     llfn
542 }
543
544 pub fn emit_tydescs(ccx: &CrateContext) {
545     let _icx = push_ctxt("emit_tydescs");
546     // As of this point, allow no more tydescs to be created.
547     ccx.finished_tydescs.set(true);
548     let glue_fn_ty = Type::generic_glue_fn(ccx).ptr_to();
549     let mut tyds = ccx.tydescs.borrow_mut();
550     for (_, &val) in tyds.get().iter() {
551         let ti = val;
552
553         // Each of the glue functions needs to be cast to a generic type
554         // before being put into the tydesc because we only have a singleton
555         // tydesc type. Then we'll recast each function to its real type when
556         // calling it.
557         let drop_glue =
558             match ti.drop_glue.get() {
559               None => {
560                   ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
561                                              1u);
562                   C_null(glue_fn_ty)
563               }
564               Some(v) => {
565                 unsafe {
566                     ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
567                                                1);
568                     llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
569                 }
570               }
571             };
572         let visit_glue =
573             match ti.visit_glue.get() {
574               None => {
575                   ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
576                                              1u);
577                   C_null(glue_fn_ty)
578               }
579               Some(v) => {
580                 unsafe {
581                     ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
582                                                1);
583                     llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
584                 }
585               }
586             };
587
588         let tydesc = C_named_struct(ccx.tydesc_type,
589                                     [ti.size, // size
590                                      ti.align, // align
591                                      drop_glue, // drop_glue
592                                      visit_glue, // visit_glue
593                                      ti.name]); // name
594
595         unsafe {
596             let gvar = ti.tydesc;
597             llvm::LLVMSetInitializer(gvar, tydesc);
598             llvm::LLVMSetGlobalConstant(gvar, True);
599             lib::llvm::SetLinkage(gvar, lib::llvm::InternalLinkage);
600         }
601     };
602 }