]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/meth.rs
auto merge of #15772 : treeman/rust/hashset-doc, r=alexcrichton
[rust.git] / src / librustc / middle / trans / meth.rs
1 // Copyright 2012 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 use back::abi;
13 use llvm;
14 use llvm::ValueRef;
15 use metadata::csearch;
16 use middle::subst::VecPerParamSpace;
17 use middle::subst;
18 use middle::trans::base::*;
19 use middle::trans::build::*;
20 use middle::trans::callee::*;
21 use middle::trans::callee;
22 use middle::trans::cleanup;
23 use middle::trans::common::*;
24 use middle::trans::datum::*;
25 use middle::trans::expr::{SaveIn, Ignore};
26 use middle::trans::expr;
27 use middle::trans::glue;
28 use middle::trans::monomorphize;
29 use middle::trans::type_::Type;
30 use middle::trans::type_of::*;
31 use middle::ty;
32 use middle::typeck;
33 use middle::typeck::MethodCall;
34 use util::common::indenter;
35 use util::ppaux::Repr;
36
37 use std::c_str::ToCStr;
38 use std::gc::Gc;
39 use syntax::abi::{Rust, RustCall};
40 use syntax::parse::token;
41 use syntax::{ast, ast_map, visit};
42 use syntax::ast_util::PostExpansionMethod;
43
44 /**
45 The main "translation" pass for methods.  Generates code
46 for non-monomorphized methods only.  Other methods will
47 be generated once they are invoked with specific type parameters,
48 see `trans::base::lval_static_fn()` or `trans::base::monomorphic_fn()`.
49 */
50 pub fn trans_impl(ccx: &CrateContext,
51                   name: ast::Ident,
52                   methods: &[Gc<ast::Method>],
53                   generics: &ast::Generics,
54                   id: ast::NodeId) {
55     let _icx = push_ctxt("meth::trans_impl");
56     let tcx = ccx.tcx();
57
58     debug!("trans_impl(name={}, id={:?})", name.repr(tcx), id);
59
60     // Both here and below with generic methods, be sure to recurse and look for
61     // items that we need to translate.
62     if !generics.ty_params.is_empty() {
63         let mut v = TransItemVisitor{ ccx: ccx };
64         for method in methods.iter() {
65             visit::walk_method_helper(&mut v, &**method, ());
66         }
67         return;
68     }
69     for method in methods.iter() {
70         if method.pe_generics().ty_params.len() == 0u {
71             let llfn = get_item_val(ccx, method.id);
72             trans_fn(ccx,
73                      &*method.pe_fn_decl(),
74                      &*method.pe_body(),
75                      llfn,
76                      &param_substs::empty(),
77                      method.id,
78                      []);
79         } else {
80             let mut v = TransItemVisitor{ ccx: ccx };
81             visit::walk_method_helper(&mut v, &**method, ());
82         }
83     }
84 }
85
86 pub fn trans_method_callee<'a>(
87                            bcx: &'a Block<'a>,
88                            method_call: MethodCall,
89                            self_expr: Option<&ast::Expr>,
90                            arg_cleanup_scope: cleanup::ScopeId)
91                            -> Callee<'a> {
92     let _icx = push_ctxt("meth::trans_method_callee");
93
94     let (origin, method_ty) = match bcx.tcx().method_map
95                                        .borrow().find(&method_call) {
96         Some(method) => {
97             debug!("trans_method_callee({:?}, method={})",
98                    method_call, method.repr(bcx.tcx()));
99             (method.origin, method.ty)
100         }
101         None => {
102             bcx.sess().span_bug(bcx.tcx().map.span(method_call.expr_id),
103                                 "method call expr wasn't in method map")
104         }
105     };
106
107     match origin {
108         typeck::MethodStatic(did) |
109         typeck::MethodStaticUnboxedClosure(did) => {
110             Callee {
111                 bcx: bcx,
112                 data: Fn(callee::trans_fn_ref(bcx,
113                                               did,
114                                               MethodCall(method_call))),
115             }
116         }
117         typeck::MethodParam(typeck::MethodParam {
118             trait_id: trait_id,
119             method_num: off,
120             param_num: p,
121             bound_num: b
122         }) => {
123             ty::populate_implementations_for_trait_if_necessary(
124                 bcx.tcx(),
125                 trait_id);
126
127             let vtbl = find_vtable(bcx.tcx(), bcx.fcx.param_substs, p, b);
128             trans_monomorphized_callee(bcx, method_call,
129                                        trait_id, off, vtbl)
130         }
131
132         typeck::MethodObject(ref mt) => {
133             let self_expr = match self_expr {
134                 Some(self_expr) => self_expr,
135                 None => {
136                     bcx.sess().span_bug(bcx.tcx().map.span(method_call.expr_id),
137                                         "self expr wasn't provided for trait object \
138                                          callee (trying to call overloaded op?)")
139                 }
140             };
141             trans_trait_callee(bcx,
142                                monomorphize_type(bcx, method_ty),
143                                mt.real_index,
144                                self_expr,
145                                arg_cleanup_scope)
146         }
147     }
148 }
149
150 pub fn trans_static_method_callee(bcx: &Block,
151                                   method_id: ast::DefId,
152                                   trait_id: ast::DefId,
153                                   expr_id: ast::NodeId)
154                                   -> ValueRef {
155     let _icx = push_ctxt("meth::trans_static_method_callee");
156     let ccx = bcx.ccx();
157
158     debug!("trans_static_method_callee(method_id={:?}, trait_id={}, \
159             expr_id={:?})",
160            method_id,
161            ty::item_path_str(bcx.tcx(), trait_id),
162            expr_id);
163     let _indenter = indenter();
164
165     ty::populate_implementations_for_trait_if_necessary(bcx.tcx(), trait_id);
166
167     let mname = if method_id.krate == ast::LOCAL_CRATE {
168         match bcx.tcx().map.get(method_id.node) {
169             ast_map::NodeTraitMethod(method) => {
170                 let ident = match *method {
171                     ast::Required(ref m) => m.ident,
172                     ast::Provided(ref m) => m.pe_ident()
173                 };
174                 ident.name
175             }
176             _ => fail!("callee is not a trait method")
177         }
178     } else {
179         csearch::get_item_path(bcx.tcx(), method_id).last().unwrap().name()
180     };
181     debug!("trans_static_method_callee: method_id={:?}, expr_id={:?}, \
182             name={}", method_id, expr_id, token::get_name(mname));
183
184     let vtable_key = MethodCall::expr(expr_id);
185     let vtbls = resolve_vtables_in_fn_ctxt(
186         bcx.fcx,
187         ccx.tcx.vtable_map.borrow().get(&vtable_key));
188
189     match *vtbls.get_self().unwrap().get(0) {
190         typeck::vtable_static(impl_did, ref rcvr_substs, ref rcvr_origins) => {
191             assert!(rcvr_substs.types.all(|t| !ty::type_needs_infer(*t)));
192
193             let mth_id = method_with_name(ccx, impl_did, mname);
194             let (callee_substs, callee_origins) =
195                 combine_impl_and_methods_tps(
196                     bcx, ExprId(expr_id),
197                     (*rcvr_substs).clone(), (*rcvr_origins).clone());
198
199             let llfn = trans_fn_ref_with_vtables(bcx, mth_id, ExprId(expr_id),
200                                                  callee_substs,
201                                                  callee_origins);
202
203             let callee_ty = node_id_type(bcx, expr_id);
204             let llty = type_of_fn_from_ty(ccx, callee_ty).ptr_to();
205             PointerCast(bcx, llfn, llty)
206         }
207         typeck::vtable_unboxed_closure(_) => {
208             bcx.tcx().sess.bug("can't call a closure vtable in a static way");
209         }
210         _ => {
211             fail!("vtable_param left in monomorphized \
212                    function's vtable substs");
213         }
214     }
215 }
216
217 fn method_with_name(ccx: &CrateContext,
218                     impl_id: ast::DefId,
219                     name: ast::Name) -> ast::DefId {
220     match ccx.impl_method_cache.borrow().find_copy(&(impl_id, name)) {
221         Some(m) => return m,
222         None => {}
223     }
224
225     let methods = ccx.tcx.impl_methods.borrow();
226     let methods = methods.find(&impl_id)
227                          .expect("could not find impl while translating");
228     let meth_did = methods.iter().find(|&did| ty::method(&ccx.tcx, *did).ident.name == name)
229                                  .expect("could not find method while translating");
230
231     ccx.impl_method_cache.borrow_mut().insert((impl_id, name), *meth_did);
232     *meth_did
233 }
234
235 fn trans_monomorphized_callee<'a>(
236                               bcx: &'a Block<'a>,
237                               method_call: MethodCall,
238                               trait_id: ast::DefId,
239                               n_method: uint,
240                               vtbl: typeck::vtable_origin)
241                               -> Callee<'a> {
242     let _icx = push_ctxt("meth::trans_monomorphized_callee");
243     match vtbl {
244       typeck::vtable_static(impl_did, rcvr_substs, rcvr_origins) => {
245           let ccx = bcx.ccx();
246           let mname = ty::trait_method(ccx.tcx(), trait_id, n_method).ident;
247           let mth_id = method_with_name(bcx.ccx(), impl_did, mname.name);
248
249           // create a concatenated set of substitutions which includes
250           // those from the impl and those from the method:
251           let (callee_substs, callee_origins) =
252               combine_impl_and_methods_tps(
253                   bcx, MethodCall(method_call), rcvr_substs, rcvr_origins);
254
255           // translate the function
256           let llfn = trans_fn_ref_with_vtables(bcx,
257                                                mth_id,
258                                                MethodCall(method_call),
259                                                callee_substs,
260                                                callee_origins);
261
262           Callee { bcx: bcx, data: Fn(llfn) }
263       }
264       typeck::vtable_unboxed_closure(closure_def_id) => {
265           // The static region and type parameters are lies, but we're in
266           // trans so it doesn't matter.
267           //
268           // FIXME(pcwalton): Is this true in the case of type parameters?
269           let callee_substs = get_callee_substitutions_for_unboxed_closure(
270                 bcx,
271                 closure_def_id);
272
273           let llfn = trans_fn_ref_with_vtables(bcx,
274                                                closure_def_id,
275                                                MethodCall(method_call),
276                                                callee_substs,
277                                                VecPerParamSpace::empty());
278
279           Callee {
280               bcx: bcx,
281               data: Fn(llfn),
282           }
283       }
284       typeck::vtable_param(..) => {
285           bcx.tcx().sess.bug(
286               "vtable_param left in monomorphized function's vtable substs");
287       }
288       typeck::vtable_error => {
289           bcx.tcx().sess.bug(
290               "vtable_error left in monomorphized function's vtable substs");
291       }
292     }
293 }
294
295 fn combine_impl_and_methods_tps(bcx: &Block,
296                                 node: ExprOrMethodCall,
297                                 rcvr_substs: subst::Substs,
298                                 rcvr_origins: typeck::vtable_res)
299                                 -> (subst::Substs, typeck::vtable_res)
300 {
301     /*!
302      * Creates a concatenated set of substitutions which includes
303      * those from the impl and those from the method.  This are
304      * some subtle complications here.  Statically, we have a list
305      * of type parameters like `[T0, T1, T2, M1, M2, M3]` where
306      * `Tn` are type parameters that appear on the receiver.  For
307      * example, if the receiver is a method parameter `A` with a
308      * bound like `trait<B,C,D>` then `Tn` would be `[B,C,D]`.
309      *
310      * The weird part is that the type `A` might now be bound to
311      * any other type, such as `foo<X>`.  In that case, the vector
312      * we want is: `[X, M1, M2, M3]`.  Therefore, what we do now is
313      * to slice off the method type parameters and append them to
314      * the type parameters from the type that the receiver is
315      * mapped to.
316      */
317
318     let ccx = bcx.ccx();
319
320     let vtable_key = match node {
321         ExprId(id) => MethodCall::expr(id),
322         MethodCall(method_call) => method_call
323     };
324     let node_substs = node_id_substs(bcx, node);
325     let node_vtables = node_vtables(bcx, vtable_key);
326
327     debug!("rcvr_substs={:?}", rcvr_substs.repr(ccx.tcx()));
328     debug!("node_substs={:?}", node_substs.repr(ccx.tcx()));
329
330     // Break apart the type parameters from the node and type
331     // parameters from the receiver.
332     let (_, _, node_method) = node_substs.types.split();
333     let (rcvr_type, rcvr_self, rcvr_method) = rcvr_substs.types.clone().split();
334     assert!(rcvr_method.is_empty());
335     let ty_substs = subst::Substs {
336         regions: subst::ErasedRegions,
337         types: subst::VecPerParamSpace::new(rcvr_type, rcvr_self, node_method)
338     };
339
340     // Now do the same work for the vtables.
341     let (rcvr_type, rcvr_self, rcvr_method) = rcvr_origins.split();
342     let (_, _, node_method) = node_vtables.split();
343     assert!(rcvr_method.is_empty());
344     let vtables = subst::VecPerParamSpace::new(rcvr_type, rcvr_self, node_method);
345
346     (ty_substs, vtables)
347 }
348
349 fn trans_trait_callee<'a>(bcx: &'a Block<'a>,
350                           method_ty: ty::t,
351                           n_method: uint,
352                           self_expr: &ast::Expr,
353                           arg_cleanup_scope: cleanup::ScopeId)
354                           -> Callee<'a> {
355     /*!
356      * Create a method callee where the method is coming from a trait
357      * object (e.g., Box<Trait> type).  In this case, we must pull the fn
358      * pointer out of the vtable that is packaged up with the object.
359      * Objects are represented as a pair, so we first evaluate the self
360      * expression and then extract the self data and vtable out of the
361      * pair.
362      */
363
364     let _icx = push_ctxt("meth::trans_trait_callee");
365     let mut bcx = bcx;
366
367     // Translate self_datum and take ownership of the value by
368     // converting to an rvalue.
369     let self_datum = unpack_datum!(
370         bcx, expr::trans(bcx, self_expr));
371
372     let llval = if ty::type_needs_drop(bcx.tcx(), self_datum.ty) {
373         let self_datum = unpack_datum!(
374             bcx, self_datum.to_rvalue_datum(bcx, "trait_callee"));
375
376         // Convert to by-ref since `trans_trait_callee_from_llval` wants it
377         // that way.
378         let self_datum = unpack_datum!(
379             bcx, self_datum.to_ref_datum(bcx));
380
381         // Arrange cleanup in case something should go wrong before the
382         // actual call occurs.
383         self_datum.add_clean(bcx.fcx, arg_cleanup_scope)
384     } else {
385         // We don't have to do anything about cleanups for &Trait and &mut Trait.
386         assert!(self_datum.kind.is_by_ref());
387         self_datum.val
388     };
389
390     trans_trait_callee_from_llval(bcx, method_ty, n_method, llval)
391 }
392
393 pub fn trans_trait_callee_from_llval<'a>(bcx: &'a Block<'a>,
394                                          callee_ty: ty::t,
395                                          n_method: uint,
396                                          llpair: ValueRef)
397                                          -> Callee<'a> {
398     /*!
399      * Same as `trans_trait_callee()` above, except that it is given
400      * a by-ref pointer to the object pair.
401      */
402
403     let _icx = push_ctxt("meth::trans_trait_callee");
404     let ccx = bcx.ccx();
405
406     // Load the data pointer from the object.
407     debug!("(translating trait callee) loading second index from pair");
408     let llboxptr = GEPi(bcx, llpair, [0u, abi::trt_field_box]);
409     let llbox = Load(bcx, llboxptr);
410     let llself = PointerCast(bcx, llbox, Type::i8p(ccx));
411
412     // Load the function from the vtable and cast it to the expected type.
413     debug!("(translating trait callee) loading method");
414     // Replace the self type (&Self or Box<Self>) with an opaque pointer.
415     let llcallee_ty = match ty::get(callee_ty).sty {
416         ty::ty_bare_fn(ref f) if f.abi == Rust || f.abi == RustCall => {
417             type_of_rust_fn(ccx,
418                             Some(Type::i8p(ccx)),
419                             f.sig.inputs.slice_from(1),
420                             f.sig.output,
421                             f.abi)
422         }
423         _ => {
424             ccx.sess().bug("meth::trans_trait_callee given non-bare-rust-fn");
425         }
426     };
427     let llvtable = Load(bcx,
428                         PointerCast(bcx,
429                                     GEPi(bcx, llpair,
430                                          [0u, abi::trt_field_vtable]),
431                                     Type::vtable(ccx).ptr_to().ptr_to()));
432     let mptr = Load(bcx, GEPi(bcx, llvtable, [0u, n_method + 1]));
433     let mptr = PointerCast(bcx, mptr, llcallee_ty.ptr_to());
434
435     return Callee {
436         bcx: bcx,
437         data: TraitMethod(MethodData {
438             llfn: mptr,
439             llself: llself,
440         })
441     };
442 }
443
444 /// Creates the self type and (fake) callee substitutions for an unboxed
445 /// closure with the given def ID. The static region and type parameters are
446 /// lies, but we're in trans so it doesn't matter.
447 fn get_callee_substitutions_for_unboxed_closure(bcx: &Block,
448                                                 def_id: ast::DefId)
449                                                 -> subst::Substs {
450     let self_ty = ty::mk_unboxed_closure(bcx.tcx(), def_id);
451     subst::Substs::erased(
452         VecPerParamSpace::new(Vec::new(),
453                               vec![
454                                   ty::mk_rptr(bcx.tcx(),
455                                               ty::ReStatic,
456                                               ty::mt {
457                                                 ty: self_ty,
458                                                 mutbl: ast::MutMutable,
459                                               })
460                               ],
461                               Vec::new()))
462 }
463
464 /// Creates a returns a dynamic vtable for the given type and vtable origin.
465 /// This is used only for objects.
466 fn get_vtable(bcx: &Block,
467               self_ty: ty::t,
468               origins: typeck::vtable_param_res)
469               -> ValueRef
470 {
471     debug!("get_vtable(self_ty={}, origins={})",
472            self_ty.repr(bcx.tcx()),
473            origins.repr(bcx.tcx()));
474
475     let ccx = bcx.ccx();
476     let _icx = push_ctxt("meth::get_vtable");
477
478     // Check the cache.
479     let hash_id = (self_ty, monomorphize::make_vtable_id(ccx, origins.get(0)));
480     match ccx.vtables.borrow().find(&hash_id) {
481         Some(&val) => { return val }
482         None => { }
483     }
484
485     // Not in the cache. Actually build it.
486     let methods = origins.move_iter().flat_map(|origin| {
487         match origin {
488             typeck::vtable_static(id, substs, sub_vtables) => {
489                 emit_vtable_methods(bcx, id, substs, sub_vtables).move_iter()
490             }
491             typeck::vtable_unboxed_closure(closure_def_id) => {
492                 let callee_substs =
493                     get_callee_substitutions_for_unboxed_closure(
494                         bcx,
495                         closure_def_id);
496
497                 let llfn = trans_fn_ref_with_vtables(
498                     bcx,
499                     closure_def_id,
500                     ExprId(0),
501                     callee_substs,
502                     VecPerParamSpace::empty());
503
504                 (vec!(llfn)).move_iter()
505             }
506             _ => ccx.sess().bug("get_vtable: expected a static origin"),
507         }
508     });
509
510     // Generate a destructor for the vtable.
511     let drop_glue = glue::get_drop_glue(ccx, self_ty);
512     let vtable = make_vtable(ccx, drop_glue, methods);
513
514     ccx.vtables.borrow_mut().insert(hash_id, vtable);
515     vtable
516 }
517
518 /// Helper function to declare and initialize the vtable.
519 pub fn make_vtable<I: Iterator<ValueRef>>(ccx: &CrateContext,
520                                           drop_glue: ValueRef,
521                                           ptrs: I)
522                                           -> ValueRef {
523     let _icx = push_ctxt("meth::make_vtable");
524
525     let components: Vec<_> = Some(drop_glue).move_iter().chain(ptrs).collect();
526
527     unsafe {
528         let tbl = C_struct(ccx, components.as_slice(), false);
529         let sym = token::gensym("vtable");
530         let vt_gvar = format!("vtable{}", sym).with_c_str(|buf| {
531             llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl).to_ref(), buf)
532         });
533         llvm::LLVMSetInitializer(vt_gvar, tbl);
534         llvm::LLVMSetGlobalConstant(vt_gvar, llvm::True);
535         llvm::SetLinkage(vt_gvar, llvm::InternalLinkage);
536         vt_gvar
537     }
538 }
539
540 fn emit_vtable_methods(bcx: &Block,
541                        impl_id: ast::DefId,
542                        substs: subst::Substs,
543                        vtables: typeck::vtable_res)
544                        -> Vec<ValueRef> {
545     let ccx = bcx.ccx();
546     let tcx = ccx.tcx();
547
548     let trt_id = match ty::impl_trait_ref(tcx, impl_id) {
549         Some(t_id) => t_id.def_id,
550         None       => ccx.sess().bug("make_impl_vtable: don't know how to \
551                                       make a vtable for a type impl!")
552     };
553
554     ty::populate_implementations_for_trait_if_necessary(bcx.tcx(), trt_id);
555
556     let trait_method_def_ids = ty::trait_method_def_ids(tcx, trt_id);
557     trait_method_def_ids.iter().map(|method_def_id| {
558         let ident = ty::method(tcx, *method_def_id).ident;
559         // The substitutions we have are on the impl, so we grab
560         // the method type from the impl to substitute into.
561         let m_id = method_with_name(ccx, impl_id, ident.name);
562         let m = ty::method(tcx, m_id);
563         debug!("(making impl vtable) emitting method {} at subst {}",
564                m.repr(tcx),
565                substs.repr(tcx));
566         if m.generics.has_type_params(subst::FnSpace) ||
567            ty::type_has_self(ty::mk_bare_fn(tcx, m.fty.clone())) {
568             debug!("(making impl vtable) method has self or type params: {}",
569                    token::get_ident(ident));
570             C_null(Type::nil(ccx).ptr_to())
571         } else {
572             let mut fn_ref = trans_fn_ref_with_vtables(bcx,
573                                                        m_id,
574                                                        ExprId(0),
575                                                        substs.clone(),
576                                                        vtables.clone());
577             if m.explicit_self == ty::ByValueExplicitSelfCategory {
578                 fn_ref = trans_unboxing_shim(bcx,
579                                              fn_ref,
580                                              &*m,
581                                              m_id,
582                                              substs.clone());
583             }
584             fn_ref
585         }
586     }).collect()
587 }
588
589 pub fn trans_trait_cast<'a>(bcx: &'a Block<'a>,
590                             datum: Datum<Expr>,
591                             id: ast::NodeId,
592                             dest: expr::Dest)
593                             -> &'a Block<'a> {
594     /*!
595      * Generates the code to convert from a pointer (`Box<T>`, `&T`, etc)
596      * into an object (`Box<Trait>`, `&Trait`, etc). This means creating a
597      * pair where the first word is the vtable and the second word is
598      * the pointer.
599      */
600
601     let mut bcx = bcx;
602     let _icx = push_ctxt("meth::trans_cast");
603
604     let lldest = match dest {
605         Ignore => {
606             return datum.clean(bcx, "trait_cast", id);
607         }
608         SaveIn(dest) => dest
609     };
610
611     let ccx = bcx.ccx();
612     let v_ty = datum.ty;
613     let llbox_ty = type_of(bcx.ccx(), datum.ty);
614
615     // Store the pointer into the first half of pair.
616     let mut llboxdest = GEPi(bcx, lldest, [0u, abi::trt_field_box]);
617     llboxdest = PointerCast(bcx, llboxdest, llbox_ty.ptr_to());
618     bcx = datum.store_to(bcx, llboxdest);
619
620     // Store the vtable into the second half of pair.
621     let origins = {
622         let vtable_map = ccx.tcx.vtable_map.borrow();
623         // This trait cast might be because of implicit coercion
624         let method_call = match ccx.tcx.adjustments.borrow().find(&id) {
625             Some(&ty::AutoObject(..)) => MethodCall::autoobject(id),
626             _ => MethodCall::expr(id)
627         };
628         let vres = vtable_map.get(&method_call).get_self().unwrap();
629         resolve_param_vtables_under_param_substs(ccx.tcx(), bcx.fcx.param_substs, vres)
630     };
631     let vtable = get_vtable(bcx, v_ty, origins);
632     let llvtabledest = GEPi(bcx, lldest, [0u, abi::trt_field_vtable]);
633     let llvtabledest = PointerCast(bcx, llvtabledest, val_ty(vtable).ptr_to());
634     Store(bcx, vtable, llvtabledest);
635
636     bcx
637 }