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