]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/glue.rs
Fix commented graphs in src/doc/trpl/ownership.md
[rust.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 taking, dropping, etc as well as type descriptors.
14
15
16 use back::abi;
17 use back::link::*;
18 use llvm::{ValueRef, True, get_param};
19 use llvm;
20 use middle::lang_items::ExchangeFreeFnLangItem;
21 use middle::subst;
22 use middle::subst::{Subst, Substs};
23 use trans::adt;
24 use trans::base::*;
25 use trans::build::*;
26 use trans::callee;
27 use trans::cleanup;
28 use trans::cleanup::CleanupMethods;
29 use trans::consts;
30 use trans::common::*;
31 use trans::datum;
32 use trans::debuginfo;
33 use trans::expr;
34 use trans::machine::*;
35 use trans::tvec;
36 use trans::type_::Type;
37 use trans::type_of::{type_of, sizing_type_of, align_of};
38 use middle::ty::{self, Ty};
39 use util::ppaux::{ty_to_short_str, Repr};
40 use util::ppaux;
41
42 use arena::TypedArena;
43 use libc::c_uint;
44 use std::ffi::CString;
45 use syntax::ast;
46 use syntax::parse::token;
47
48 pub fn trans_exchange_free_dyn<'blk, 'tcx>(cx: Block<'blk, 'tcx>, v: ValueRef,
49                                            size: ValueRef, align: ValueRef)
50                                            -> Block<'blk, 'tcx> {
51     let _icx = push_ctxt("trans_exchange_free");
52     let ccx = cx.ccx();
53     callee::trans_lang_call(cx,
54         langcall(cx, None, "", ExchangeFreeFnLangItem),
55         &[PointerCast(cx, v, Type::i8p(ccx)), size, align],
56         Some(expr::Ignore)).bcx
57 }
58
59 pub fn trans_exchange_free<'blk, 'tcx>(cx: Block<'blk, 'tcx>, v: ValueRef,
60                                        size: u64, align: u32) -> Block<'blk, 'tcx> {
61     trans_exchange_free_dyn(cx, v, C_uint(cx.ccx(), size),
62                                    C_uint(cx.ccx(), align))
63 }
64
65 pub fn trans_exchange_free_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ptr: ValueRef,
66                                           content_ty: Ty<'tcx>) -> Block<'blk, 'tcx> {
67     assert!(type_is_sized(bcx.ccx().tcx(), content_ty));
68     let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
69     let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
70
71     // `Box<ZeroSizeType>` does not allocate.
72     if content_size != 0 {
73         let content_align = align_of(bcx.ccx(), content_ty);
74         trans_exchange_free(bcx, ptr, content_size, content_align)
75     } else {
76         bcx
77     }
78 }
79
80 pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
81                                     t: Ty<'tcx>) -> Ty<'tcx> {
82     let tcx = ccx.tcx();
83     // Even if there is no dtor for t, there might be one deeper down and we
84     // might need to pass in the vtable ptr.
85     if !type_is_sized(tcx, t) {
86         return t
87     }
88     if !type_needs_drop(tcx, t) {
89         return tcx.types.i8;
90     }
91     match t.sty {
92         ty::ty_uniq(typ) if !type_needs_drop(tcx, typ)
93                          && type_is_sized(tcx, typ) => {
94             let llty = sizing_type_of(ccx, typ);
95             // `Box<ZeroSizeType>` does not allocate.
96             if llsize_of_alloc(ccx, llty) == 0 {
97                 tcx.types.i8
98             } else {
99                 t
100             }
101         }
102         _ => t
103     }
104 }
105
106 pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
107                            v: ValueRef,
108                            t: Ty<'tcx>,
109                            source_location: Option<NodeInfo>)
110                            -> Block<'blk, 'tcx> {
111     // NB: v is an *alias* of type t here, not a direct value.
112     debug!("drop_ty(t={})", t.repr(bcx.tcx()));
113     let _icx = push_ctxt("drop_ty");
114     if type_needs_drop(bcx.tcx(), t) {
115         let ccx = bcx.ccx();
116         let glue = get_drop_glue(ccx, t);
117         let glue_type = get_drop_glue_type(ccx, t);
118         let ptr = if glue_type != t {
119             PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
120         } else {
121             v
122         };
123
124         match source_location {
125             Some(sl) => debuginfo::set_source_location(bcx.fcx, sl.id, sl.span),
126             None => debuginfo::clear_source_location(bcx.fcx)
127         };
128
129         Call(bcx, glue, &[ptr], None);
130     }
131     bcx
132 }
133
134 pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
135                                      v: ValueRef,
136                                      t: Ty<'tcx>,
137                                      source_location: Option<NodeInfo>)
138                                      -> Block<'blk, 'tcx> {
139     let _icx = push_ctxt("drop_ty_immediate");
140     let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
141     store_ty(bcx, v, vp, t);
142     drop_ty(bcx, vp, t, source_location)
143 }
144
145 pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ValueRef {
146     debug!("make drop glue for {}", ppaux::ty_to_string(ccx.tcx(), t));
147     let t = get_drop_glue_type(ccx, t);
148     debug!("drop glue type {}", ppaux::ty_to_string(ccx.tcx(), t));
149     match ccx.drop_glues().borrow().get(&t) {
150         Some(&glue) => return glue,
151         _ => { }
152     }
153
154     let llty = if type_is_sized(ccx.tcx(), t) {
155         type_of(ccx, t).ptr_to()
156     } else {
157         type_of(ccx, ty::mk_uniq(ccx.tcx(), t)).ptr_to()
158     };
159
160     let llfnty = Type::glue_fn(ccx, llty);
161
162     let (glue, new_sym) = match ccx.available_drop_glues().borrow().get(&t) {
163         Some(old_sym) => {
164             let glue = decl_cdecl_fn(ccx, &old_sym[], llfnty, ty::mk_nil(ccx.tcx()));
165             (glue, None)
166         },
167         None => {
168             let (sym, glue) = declare_generic_glue(ccx, t, llfnty, "drop");
169             (glue, Some(sym))
170         },
171     };
172
173     ccx.drop_glues().borrow_mut().insert(t, glue);
174
175     // To avoid infinite recursion, don't `make_drop_glue` until after we've
176     // added the entry to the `drop_glues` cache.
177     match new_sym {
178         Some(sym) => {
179             ccx.available_drop_glues().borrow_mut().insert(t, sym);
180             // We're creating a new drop glue, so also generate a body.
181             make_generic_glue(ccx, t, glue, make_drop_glue, "drop");
182         },
183         None => {},
184     }
185
186     glue
187 }
188
189 fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
190                                       t: Ty<'tcx>,
191                                       v0: ValueRef,
192                                       dtor_did: ast::DefId,
193                                       class_did: ast::DefId,
194                                       substs: &subst::Substs<'tcx>)
195                                       -> Block<'blk, 'tcx> {
196     let repr = adt::represent_type(bcx.ccx(), t);
197     let struct_data = if type_is_sized(bcx.tcx(), t) {
198         v0
199     } else {
200         let llval = GEPi(bcx, v0, &[0, abi::FAT_PTR_ADDR]);
201         Load(bcx, llval)
202     };
203     let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &*repr, struct_data));
204     with_cond(bcx, load_ty(bcx, drop_flag.val, bcx.tcx().types.bool), |cx| {
205         trans_struct_drop(cx, t, v0, dtor_did, class_did, substs)
206     })
207 }
208
209 fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
210                                  t: Ty<'tcx>,
211                                  v0: ValueRef,
212                                  dtor_did: ast::DefId,
213                                  class_did: ast::DefId,
214                                  substs: &subst::Substs<'tcx>)
215                                  -> Block<'blk, 'tcx>
216 {
217     let repr = adt::represent_type(bcx.ccx(), t);
218
219     // Find and call the actual destructor
220     let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, t,
221                                  class_did, substs);
222
223     // The first argument is the "self" argument for drop
224     let params = unsafe {
225         let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
226         ty.element_type().func_params()
227     };
228
229     let fty = ty::lookup_item_type(bcx.tcx(), dtor_did).ty.subst(bcx.tcx(), substs);
230     let self_ty = match fty.sty {
231         ty::ty_bare_fn(_, ref f) => {
232             let sig = ty::erase_late_bound_regions(bcx.tcx(), &f.sig);
233             assert!(sig.inputs.len() == 1);
234             sig.inputs[0]
235         }
236         _ => bcx.sess().bug(&format!("Expected function type, found {}",
237                                     bcx.ty_to_string(fty))[])
238     };
239
240     let (struct_data, info) = if type_is_sized(bcx.tcx(), t) {
241         (v0, None)
242     } else {
243         let data = GEPi(bcx, v0, &[0, abi::FAT_PTR_ADDR]);
244         let info = GEPi(bcx, v0, &[0, abi::FAT_PTR_EXTRA]);
245         (Load(bcx, data), Some(Load(bcx, info)))
246     };
247
248     adt::fold_variants(bcx, &*repr, struct_data, |variant_cx, st, value| {
249         // Be sure to put all of the fields into a scope so we can use an invoke
250         // instruction to call the user destructor but still call the field
251         // destructors if the user destructor panics.
252         let field_scope = variant_cx.fcx.push_custom_cleanup_scope();
253
254         // Class dtors have no explicit args, so the params should
255         // just consist of the environment (self).
256         assert_eq!(params.len(), 1);
257         let self_arg = if type_is_fat_ptr(bcx.tcx(), self_ty) {
258             // The dtor expects a fat pointer, so make one, even if we have to fake it.
259             let boxed_ty = ty::mk_open(bcx.tcx(), t);
260             let scratch = datum::rvalue_scratch_datum(bcx, boxed_ty, "__fat_ptr_drop_self");
261             Store(bcx, value, GEPi(bcx, scratch.val, &[0, abi::FAT_PTR_ADDR]));
262             Store(bcx,
263                   // If we just had a thin pointer, make a fat pointer by sticking
264                   // null where we put the unsizing info. This works because t
265                   // is a sized type, so we will only unpack the fat pointer, never
266                   // use the fake info.
267                   info.unwrap_or(C_null(Type::i8p(bcx.ccx()))),
268                   GEPi(bcx, scratch.val, &[0, abi::FAT_PTR_EXTRA]));
269             PointerCast(variant_cx, scratch.val, params[0])
270         } else {
271             PointerCast(variant_cx, value, params[0])
272         };
273         let args = vec!(self_arg);
274
275         // Add all the fields as a value which needs to be cleaned at the end of
276         // this scope. Iterate in reverse order so a Drop impl doesn't reverse
277         // the order in which fields get dropped.
278         for (i, ty) in st.fields.iter().enumerate().rev() {
279             let llfld_a = adt::struct_field_ptr(variant_cx, &*st, value, i, false);
280
281             let val = if type_is_sized(bcx.tcx(), *ty) {
282                 llfld_a
283             } else {
284                 let boxed_ty = ty::mk_open(bcx.tcx(), *ty);
285                 let scratch = datum::rvalue_scratch_datum(bcx, boxed_ty, "__fat_ptr_drop_field");
286                 Store(bcx, llfld_a, GEPi(bcx, scratch.val, &[0, abi::FAT_PTR_ADDR]));
287                 Store(bcx, info.unwrap(), GEPi(bcx, scratch.val, &[0, abi::FAT_PTR_EXTRA]));
288                 scratch.val
289             };
290             variant_cx.fcx.schedule_drop_mem(cleanup::CustomScope(field_scope),
291                                              val, *ty);
292         }
293
294         let dtor_ty = ty::mk_ctor_fn(bcx.tcx(),
295                                      class_did,
296                                      &[get_drop_glue_type(bcx.ccx(), t)],
297                                      ty::mk_nil(bcx.tcx()));
298         let (_, variant_cx) = invoke(variant_cx, dtor_addr, &args[], dtor_ty, None);
299
300         variant_cx.fcx.pop_and_trans_custom_cleanup_scope(variant_cx, field_scope);
301         variant_cx
302     })
303 }
304
305 fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, info: ValueRef)
306                                      -> (ValueRef, ValueRef) {
307     debug!("calculate size of DST: {}; with lost info: {}",
308            bcx.ty_to_string(t), bcx.val_to_string(info));
309     if type_is_sized(bcx.tcx(), t) {
310         let sizing_type = sizing_type_of(bcx.ccx(), t);
311         let size = C_uint(bcx.ccx(), llsize_of_alloc(bcx.ccx(), sizing_type));
312         let align = C_uint(bcx.ccx(), align_of(bcx.ccx(), t));
313         return (size, align);
314     }
315     match t.sty {
316         ty::ty_struct(id, substs) => {
317             let ccx = bcx.ccx();
318             // First get the size of all statically known fields.
319             // Don't use type_of::sizing_type_of because that expects t to be sized.
320             assert!(!ty::type_is_simd(bcx.tcx(), t));
321             let repr = adt::represent_type(ccx, t);
322             let sizing_type = adt::sizing_type_of(ccx, &*repr, true);
323             let sized_size = C_uint(ccx, llsize_of_alloc(ccx, sizing_type));
324             let sized_align = C_uint(ccx, llalign_of_min(ccx, sizing_type));
325
326             // Recurse to get the size of the dynamically sized field (must be
327             // the last field).
328             let fields = ty::struct_fields(bcx.tcx(), id, substs);
329             let last_field = fields[fields.len()-1];
330             let field_ty = last_field.mt.ty;
331             let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
332
333             // Return the sum of sizes and max of aligns.
334             let size = Add(bcx, sized_size, unsized_size);
335             let align = Select(bcx,
336                                ICmp(bcx, llvm::IntULT, sized_align, unsized_align),
337                                sized_align,
338                                unsized_align);
339             (size, align)
340         }
341         ty::ty_trait(..) => {
342             // info points to the vtable and the second entry in the vtable is the
343             // dynamic size of the object.
344             let info = PointerCast(bcx, info, Type::int(bcx.ccx()).ptr_to());
345             let size_ptr = GEPi(bcx, info, &[1u]);
346             let align_ptr = GEPi(bcx, info, &[2u]);
347             (Load(bcx, size_ptr), Load(bcx, align_ptr))
348         }
349         ty::ty_vec(unit_ty, None) => {
350             // The info in this case is the length of the vec, so the size is that
351             // times the unit size.
352             let llunit_ty = sizing_type_of(bcx.ccx(), unit_ty);
353             let unit_size = llsize_of_alloc(bcx.ccx(), llunit_ty);
354             (Mul(bcx, info, C_uint(bcx.ccx(), unit_size)), C_uint(bcx.ccx(), 8u))
355         }
356         _ => bcx.sess().bug(&format!("Unexpected unsized type, found {}",
357                                     bcx.ty_to_string(t))[])
358     }
359 }
360
361 fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, t: Ty<'tcx>)
362                               -> Block<'blk, 'tcx> {
363     // NB: v0 is an *alias* of type t here, not a direct value.
364     let _icx = push_ctxt("make_drop_glue");
365     match t.sty {
366         ty::ty_uniq(content_ty) => {
367             match content_ty.sty {
368                 ty::ty_vec(ty, None) => {
369                     tvec::make_drop_glue_unboxed(bcx, v0, ty, true)
370                 }
371                 ty::ty_str => {
372                     let unit_ty = ty::sequence_element_type(bcx.tcx(), content_ty);
373                     tvec::make_drop_glue_unboxed(bcx, v0, unit_ty, true)
374                 }
375                 ty::ty_trait(..) => {
376                     let lluniquevalue = GEPi(bcx, v0, &[0, abi::FAT_PTR_ADDR]);
377                     // Only drop the value when it is non-null
378                     let concrete_ptr = Load(bcx, lluniquevalue);
379                     with_cond(bcx, IsNotNull(bcx, concrete_ptr), |bcx| {
380                         let dtor_ptr = Load(bcx, GEPi(bcx, v0, &[0, abi::FAT_PTR_EXTRA]));
381                         let dtor = Load(bcx, dtor_ptr);
382                         Call(bcx,
383                              dtor,
384                              &[PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
385                              None);
386                         bcx
387                     })
388                 }
389                 ty::ty_struct(..) if !type_is_sized(bcx.tcx(), content_ty) => {
390                     let llval = GEPi(bcx, v0, &[0, abi::FAT_PTR_ADDR]);
391                     let llbox = Load(bcx, llval);
392                     let not_null = IsNotNull(bcx, llbox);
393                     with_cond(bcx, not_null, |bcx| {
394                         let bcx = drop_ty(bcx, v0, content_ty, None);
395                         let info = GEPi(bcx, v0, &[0, abi::FAT_PTR_EXTRA]);
396                         let info = Load(bcx, info);
397                         let (llsize, llalign) = size_and_align_of_dst(bcx, content_ty, info);
398                         trans_exchange_free_dyn(bcx, llbox, llsize, llalign)
399                     })
400                 }
401                 _ => {
402                     assert!(type_is_sized(bcx.tcx(), content_ty));
403                     let llval = v0;
404                     let llbox = Load(bcx, llval);
405                     let not_null = IsNotNull(bcx, llbox);
406                     with_cond(bcx, not_null, |bcx| {
407                         let bcx = drop_ty(bcx, llbox, content_ty, None);
408                         trans_exchange_free_ty(bcx, llbox, content_ty)
409                     })
410                 }
411             }
412         }
413         ty::ty_struct(did, substs) | ty::ty_enum(did, substs) => {
414             let tcx = bcx.tcx();
415             match ty::ty_dtor(tcx, did) {
416                 ty::TraitDtor(dtor, true) => {
417                     // FIXME(16758) Since the struct is unsized, it is hard to
418                     // find the drop flag (which is at the end of the struct).
419                     // Lets just ignore the flag and pretend everything will be
420                     // OK.
421                     if type_is_sized(bcx.tcx(), t) {
422                         trans_struct_drop_flag(bcx, t, v0, dtor, did, substs)
423                     } else {
424                         // Give the user a heads up that we are doing something
425                         // stupid and dangerous.
426                         bcx.sess().warn(&format!("Ignoring drop flag in destructor for {}\
427                                                  because the struct is unsized. See issue\
428                                                  #16758",
429                                                 bcx.ty_to_string(t))[]);
430                         trans_struct_drop(bcx, t, v0, dtor, did, substs)
431                     }
432                 }
433                 ty::TraitDtor(dtor, false) => {
434                     trans_struct_drop(bcx, t, v0, dtor, did, substs)
435                 }
436                 ty::NoDtor => {
437                     // No dtor? Just the default case
438                     iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, None))
439                 }
440             }
441         }
442         ty::ty_unboxed_closure(..) => iter_structural_ty(bcx,
443                                                          v0,
444                                                          t,
445                                                          |bb, vv, tt| drop_ty(bb, vv, tt, None)),
446         ty::ty_trait(..) => {
447             // No need to do a null check here (as opposed to the Box<trait case
448             // above), because this happens for a trait field in an unsized
449             // struct. If anything is null, it is the whole struct and we won't
450             // get here.
451             let lluniquevalue = GEPi(bcx, v0, &[0, abi::FAT_PTR_ADDR]);
452             let dtor_ptr = Load(bcx, GEPi(bcx, v0, &[0, abi::FAT_PTR_EXTRA]));
453             let dtor = Load(bcx, dtor_ptr);
454             Call(bcx,
455                  dtor,
456                  &[PointerCast(bcx, Load(bcx, lluniquevalue), Type::i8p(bcx.ccx()))],
457                  None);
458             bcx
459         }
460         ty::ty_vec(ty, None) => tvec::make_drop_glue_unboxed(bcx, v0, ty, false),
461         _ => {
462             assert!(type_is_sized(bcx.tcx(), t));
463             if type_needs_drop(bcx.tcx(), t) &&
464                 ty::type_is_structural(t) {
465                 iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, None))
466             } else {
467                 bcx
468             }
469         }
470     }
471 }
472
473 // Generates the declaration for (but doesn't emit) a type descriptor.
474 pub fn declare_tydesc<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>)
475                                 -> tydesc_info<'tcx> {
476     // If emit_tydescs already ran, then we shouldn't be creating any new
477     // tydescs.
478     assert!(!ccx.finished_tydescs().get());
479
480     let llty = type_of(ccx, t);
481
482     if ccx.sess().count_type_sizes() {
483         println!("{}\t{}", llsize_of_real(ccx, llty),
484                  ppaux::ty_to_string(ccx.tcx(), t));
485     }
486
487     let llsize = llsize_of(ccx, llty);
488     let llalign = llalign_of(ccx, llty);
489     let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc");
490     debug!("+++ declare_tydesc {} {}", ppaux::ty_to_string(ccx.tcx(), t), name);
491     let buf = CString::from_slice(name.as_bytes());
492     let gvar = unsafe {
493         llvm::LLVMAddGlobal(ccx.llmod(), ccx.tydesc_type().to_ref(),
494                             buf.as_ptr())
495     };
496     note_unique_llvm_symbol(ccx, name);
497
498     let ty_name = token::intern_and_get_ident(
499         &ppaux::ty_to_string(ccx.tcx(), t)[]);
500     let ty_name = C_str_slice(ccx, ty_name);
501
502     debug!("--- declare_tydesc {}", ppaux::ty_to_string(ccx.tcx(), t));
503     tydesc_info {
504         ty: t,
505         tydesc: gvar,
506         size: llsize,
507         align: llalign,
508         name: ty_name,
509     }
510 }
511
512 fn declare_generic_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>,
513                                   llfnty: Type, name: &str) -> (String, ValueRef) {
514     let _icx = push_ctxt("declare_generic_glue");
515     let fn_nm = mangle_internal_name_by_type_and_seq(
516         ccx,
517         t,
518         &format!("glue_{}", name)[]);
519     let llfn = decl_cdecl_fn(ccx, &fn_nm[], llfnty, ty::mk_nil(ccx.tcx()));
520     note_unique_llvm_symbol(ccx, fn_nm.clone());
521     return (fn_nm, llfn);
522 }
523
524 fn make_generic_glue<'a, 'tcx, F>(ccx: &CrateContext<'a, 'tcx>,
525                                   t: Ty<'tcx>,
526                                   llfn: ValueRef,
527                                   helper: F,
528                                   name: &str)
529                                   -> ValueRef where
530     F: for<'blk> FnOnce(Block<'blk, 'tcx>, ValueRef, Ty<'tcx>) -> Block<'blk, 'tcx>,
531 {
532     let _icx = push_ctxt("make_generic_glue");
533     let glue_name = format!("glue {} {}", name, ty_to_short_str(ccx.tcx(), t));
534     let _s = StatRecorder::new(ccx, glue_name);
535
536     let arena = TypedArena::new();
537     let empty_param_substs = Substs::trans_empty();
538     let fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, false,
539                           ty::FnConverging(ty::mk_nil(ccx.tcx())),
540                           &empty_param_substs, None, &arena);
541
542     let bcx = init_function(&fcx, false, ty::FnConverging(ty::mk_nil(ccx.tcx())));
543
544     update_linkage(ccx, llfn, None, OriginalTranslation);
545
546     ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1u);
547     // All glue functions take values passed *by alias*; this is a
548     // requirement since in many contexts glue is invoked indirectly and
549     // the caller has no idea if it's dealing with something that can be
550     // passed by value.
551     //
552     // llfn is expected be declared to take a parameter of the appropriate
553     // type, so we don't need to explicitly cast the function parameter.
554
555     let llrawptr0 = get_param(llfn, fcx.arg_pos(0) as c_uint);
556     let bcx = helper(bcx, llrawptr0, t);
557     finish_fn(&fcx, bcx, ty::FnConverging(ty::mk_nil(ccx.tcx())));
558
559     llfn
560 }
561
562 pub fn emit_tydescs(ccx: &CrateContext) {
563     let _icx = push_ctxt("emit_tydescs");
564     // As of this point, allow no more tydescs to be created.
565     ccx.finished_tydescs().set(true);
566     let glue_fn_ty = Type::generic_glue_fn(ccx).ptr_to();
567     for (_, ti) in ccx.tydescs().borrow().iter() {
568         // Each of the glue functions needs to be cast to a generic type
569         // before being put into the tydesc because we only have a singleton
570         // tydesc type. Then we'll recast each function to its real type when
571         // calling it.
572         let drop_glue = consts::ptrcast(get_drop_glue(ccx, ti.ty), glue_fn_ty);
573         ccx.stats().n_real_glues.set(ccx.stats().n_real_glues.get() + 1);
574
575         let tydesc = C_named_struct(ccx.tydesc_type(),
576                                     &[ti.size, // size
577                                       ti.align, // align
578                                       drop_glue, // drop_glue
579                                       ti.name]); // name
580
581         unsafe {
582             let gvar = ti.tydesc;
583             llvm::LLVMSetInitializer(gvar, tydesc);
584             llvm::LLVMSetGlobalConstant(gvar, True);
585             llvm::SetLinkage(gvar, llvm::InternalLinkage);
586         }
587     };
588 }