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.
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.
13 use arena::TypedArena;
15 use llvm::{ValueRef, get_params};
16 use middle::def_id::DefId;
18 use middle::subst::{Subst, Substs};
23 use trans::callee::{Callee, Virtual, ArgVals,
24 trans_fn_pointer_shim, trans_fn_ref_with_substs};
29 use trans::debuginfo::DebugLoc;
34 use trans::type_::Type;
35 use trans::type_of::*;
36 use middle::ty::{self, Ty, TyCtxt, TypeFoldable};
37 use middle::ty::MethodCall;
39 use syntax::ast::{self, Name};
41 use syntax::codemap::DUMMY_SP;
45 // drop_glue pointer, size, align.
46 const VTABLE_OFFSET: usize = 3;
48 /// The main "translation" pass for methods. Generates code
49 /// for non-monomorphized methods only. Other methods will
50 /// be generated once they are invoked with specific type parameters,
51 /// see `trans::base::lval_static_fn()` or `trans::base::monomorphic_fn()`.
52 pub fn trans_impl(ccx: &CrateContext,
54 impl_items: &[hir::ImplItem],
55 generics: &hir::Generics,
57 let _icx = push_ctxt("meth::trans_impl");
60 debug!("trans_impl(name={}, id={})", name, id);
62 // Both here and below with generic methods, be sure to recurse and look for
63 // items that we need to translate.
64 if !generics.ty_params.is_empty() {
68 for impl_item in impl_items {
69 match impl_item.node {
70 hir::ImplItemKind::Method(ref sig, ref body) => {
71 if sig.generics.ty_params.is_empty() {
72 let trans_everywhere = attr::requests_inline(&impl_item.attrs);
73 for (ref ccx, is_origin) in ccx.maybe_iter(trans_everywhere) {
74 let llfn = get_item_val(ccx, impl_item.id);
75 let empty_substs = tcx.mk_substs(Substs::trans_empty());
86 if is_origin { OriginalTranslation } else { InlinedCopy });
95 /// Compute the appropriate callee, give na method's ID, trait ID,
96 /// substitutions and a Vtable for that trait.
97 pub fn callee_for_trait_impl<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
99 substs: &'tcx subst::Substs<'tcx>,
102 vtable: traits::Vtable<'tcx, ()>)
104 let _icx = push_ctxt("meth::callee_for_trait_impl");
106 traits::VtableImpl(vtable_impl) => {
107 let impl_did = vtable_impl.impl_def_id;
108 let mname = ccx.tcx().item_name(method_id);
109 // create a concatenated set of substitutions which includes
110 // those from the impl and those from the method:
111 let impl_substs = vtable_impl.substs.with_method_from(&substs);
112 let substs = ccx.tcx().mk_substs(impl_substs);
113 let mth = get_impl_method(ccx.tcx(), impl_did, impl_substs, mname);
115 // Translate the function, bypassing Callee::def.
116 // That is because default methods have the same ID as the
117 // trait method used to look up the impl method that ended
118 // up here, so calling Callee::def would infinitely recurse.
119 Callee::ptr(trans_fn_ref_with_substs(ccx, mth.method.def_id,
120 Some(method_ty), mth.substs))
122 traits::VtableClosure(vtable_closure) => {
123 // The substitutions should have no type parameters remaining
124 // after passing through fulfill_obligation
125 let trait_closure_kind = ccx.tcx().lang_items.fn_trait_kind(trait_id).unwrap();
126 let llfn = closure::trans_closure_method(ccx,
127 vtable_closure.closure_def_id,
128 vtable_closure.substs,
130 let fn_ptr_ty = match method_ty.sty {
131 ty::TyFnDef(_, _, fty) => ccx.tcx().mk_ty(ty::TyFnPtr(fty)),
132 _ => unreachable!("expected fn item type, found {}",
135 Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
137 traits::VtableFnPointer(fn_ty) => {
138 let trait_closure_kind = ccx.tcx().lang_items.fn_trait_kind(trait_id).unwrap();
139 let llfn = trans_fn_pointer_shim(ccx, trait_closure_kind, fn_ty);
140 let fn_ptr_ty = match method_ty.sty {
141 ty::TyFnDef(_, _, fty) => ccx.tcx().mk_ty(ty::TyFnPtr(fty)),
142 _ => unreachable!("expected fn item type, found {}",
145 Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
147 traits::VtableObject(ref data) => {
149 data: Virtual(traits::get_vtable_index_of_object_method(
150 ccx.tcx(), data, method_id)),
154 traits::VtableBuiltin(..) |
155 traits::VtableDefaultImpl(..) |
156 traits::VtableParam(..) => {
158 &format!("resolved vtable bad vtable {:?} in trans",
164 /// Extracts a method from a trait object's vtable, at the
165 /// specified index, and casts it to the given type.
166 pub fn get_virtual_method<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
170 -> Datum<'tcx, Rvalue> {
171 let _icx = push_ctxt("meth::get_virtual_method");
174 // Load the data pointer from the object.
175 debug!("get_virtual_method(callee_ty={}, vtable_index={}, llvtable={})",
178 bcx.val_to_string(llvtable));
180 let mptr = Load(bcx, GEPi(bcx, llvtable, &[vtable_index + VTABLE_OFFSET]));
182 // Replace the self type (&Self or Box<Self>) with an opaque pointer.
183 if let ty::TyFnDef(_, _, fty) = method_ty.sty {
184 let opaque_ty = opaque_method_ty(ccx.tcx(), fty);
185 immediate_rvalue(PointerCast(bcx, mptr, type_of(ccx, opaque_ty)), opaque_ty)
187 immediate_rvalue(mptr, method_ty)
191 /// Generate a shim function that allows an object type like `SomeTrait` to
192 /// implement the type `SomeTrait`. Imagine a trait definition:
194 /// trait SomeTrait { fn get(&self) -> i32; ... }
196 /// And a generic bit of code:
198 /// fn foo<T:SomeTrait>(t: &T) {
199 /// let x = SomeTrait::get;
203 /// What is the value of `x` when `foo` is invoked with `T=SomeTrait`?
204 /// The answer is that it is a shim function generated by this routine:
206 /// fn shim(t: &SomeTrait) -> i32 {
207 /// // ... call t.get() virtually ...
210 /// In fact, all virtual calls can be thought of as normal trait calls
211 /// that go through this shim function.
212 pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
215 -> Datum<'tcx, Rvalue> {
216 let _icx = push_ctxt("trans_object_shim");
219 debug!("trans_object_shim(vtable_index={}, method_ty={:?})",
223 let ret_ty = tcx.erase_late_bound_regions(&method_ty.fn_ret());
224 let ret_ty = infer::normalize_associated_type(tcx, &ret_ty);
226 let shim_fn_ty = match method_ty.sty {
227 ty::TyFnDef(_, _, fty) => tcx.mk_ty(ty::TyFnPtr(fty)),
228 _ => unreachable!("expected fn item type, found {}", method_ty)
232 let function_name = link::mangle_internal_name_by_type_and_seq(ccx, shim_fn_ty, "object_shim");
233 let llfn = declare::define_internal_rust_fn(ccx, &function_name, shim_fn_ty);
235 let empty_substs = tcx.mk_substs(Substs::trans_empty());
236 let (block_arena, fcx): (TypedArena<_>, FunctionContext);
237 block_arena = TypedArena::new();
238 fcx = new_fn_ctxt(ccx,
246 let mut bcx = init_function(&fcx, false, ret_ty);
248 let llargs = get_params(fcx.llfn);
250 let self_idx = fcx.arg_offset();
251 let llself = llargs[self_idx];
252 let llvtable = llargs[self_idx + 1];
254 debug!("trans_object_shim: llself={}, llvtable={}",
255 bcx.val_to_string(llself), bcx.val_to_string(llvtable));
257 assert!(!fcx.needs_ret_allocas);
260 fcx.llretslotptr.get().map(
261 |_| expr::SaveIn(fcx.get_ret_slot(bcx, ret_ty, "ret_slot")));
263 debug!("trans_object_shim: method_offset_in_vtable={}",
266 let callee = Callee {
267 data: Virtual(vtable_index),
270 bcx = callee.call(bcx, DebugLoc::None, ArgVals(&llargs[self_idx..]), dest).bcx;
272 finish_fn(&fcx, bcx, ret_ty, DebugLoc::None);
274 immediate_rvalue(llfn, shim_fn_ty)
277 /// Creates a returns a dynamic vtable for the given type and vtable origin.
278 /// This is used only for objects.
280 /// The `trait_ref` encodes the erased self type. Hence if we are
281 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
282 /// `trait_ref` would map `T:Trait`.
283 pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
284 trait_ref: ty::PolyTraitRef<'tcx>)
288 let _icx = push_ctxt("meth::get_vtable");
290 debug!("get_vtable(trait_ref={:?})", trait_ref);
293 match ccx.vtables().borrow().get(&trait_ref) {
294 Some(&val) => { return val }
298 // Not in the cache. Build it.
299 let methods = traits::supertraits(tcx, trait_ref.clone()).flat_map(|trait_ref| {
300 let vtable = fulfill_obligation(ccx, DUMMY_SP, trait_ref.clone());
302 // Should default trait error here?
303 traits::VtableDefaultImpl(_) |
304 traits::VtableBuiltin(_) => {
305 Vec::new().into_iter()
308 traits::VtableImplData {
312 let nullptr = C_null(Type::nil(ccx).ptr_to());
313 get_vtable_methods(ccx, id, substs)
318 trans_fn_ref_with_substs(ccx,
329 traits::VtableClosure(
330 traits::VtableClosureData {
334 let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_ref.def_id()).unwrap();
335 let llfn = closure::trans_closure_method(ccx,
339 vec![llfn].into_iter()
341 traits::VtableFnPointer(bare_fn_ty) => {
342 let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_ref.def_id()).unwrap();
343 vec![trans_fn_pointer_shim(ccx, trait_closure_kind, bare_fn_ty)].into_iter()
345 traits::VtableObject(ref data) => {
346 // this would imply that the Self type being erased is
347 // an object type; this cannot happen because we
348 // cannot cast an unsized type into a trait object
350 &format!("cannot get vtable for an object type: {:?}",
353 traits::VtableParam(..) => {
355 &format!("resolved vtable for {:?} to bad vtable {:?} in trans",
362 let size_ty = sizing_type_of(ccx, trait_ref.self_ty());
363 let size = machine::llsize_of_alloc(ccx, size_ty);
364 let align = align_of(ccx, trait_ref.self_ty());
366 let components: Vec<_> = vec![
367 // Generate a destructor for the vtable.
368 glue::get_drop_glue(ccx, trait_ref.self_ty()),
371 ].into_iter().chain(methods).collect();
373 let vtable_const = C_struct(ccx, &components, false);
374 let align = machine::llalign_of_pref(ccx, val_ty(vtable_const));
375 let vtable = consts::addr_of(ccx, vtable_const, align, "vtable");
377 ccx.vtables().borrow_mut().insert(trait_ref, vtable);
381 pub fn get_vtable_methods<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
383 substs: &'tcx subst::Substs<'tcx>)
384 -> Vec<Option<ImplMethod<'tcx>>>
388 debug!("get_vtable_methods(impl_id={:?}, substs={:?}", impl_id, substs);
390 let trt_id = match tcx.impl_trait_ref(impl_id) {
391 Some(t_id) => t_id.def_id,
392 None => ccx.sess().bug("make_impl_vtable: don't know how to \
393 make a vtable for a type impl!")
396 tcx.populate_implementations_for_trait_if_necessary(trt_id);
398 let trait_item_def_ids = tcx.trait_item_def_ids(trt_id);
402 // Filter out non-method items.
403 .filter_map(|item_def_id| {
405 ty::MethodTraitItemId(def_id) => Some(def_id),
410 // Now produce pointers for each remaining method. If the
411 // method could never be called from this object, just supply
413 .map(|trait_method_def_id| {
414 debug!("get_vtable_methods: trait_method_def_id={:?}",
415 trait_method_def_id);
417 let trait_method_type = match tcx.impl_or_trait_item(trait_method_def_id) {
418 ty::MethodTraitItem(m) => m,
419 _ => ccx.sess().bug("should be a method, not other assoc item"),
421 let name = trait_method_type.name;
423 // Some methods cannot be called on an object; skip those.
424 if !traits::is_vtable_safe_method(tcx, trt_id, &trait_method_type) {
425 debug!("get_vtable_methods: not vtable safe");
429 debug!("get_vtable_methods: trait_method_type={:?}",
432 // The substitutions we have are on the impl, so we grab
433 // the method type from the impl to substitute into.
434 let mth = get_impl_method(tcx, impl_id, substs.clone(), name);
436 debug!("get_vtable_methods: mth={:?}", mth);
438 // If this is a default method, it's possible that it
439 // relies on where clauses that do not hold for this
440 // particular set of type parameters. Note that this
441 // method could then never be called, so we do not want to
442 // try and trans it, in that case. Issue #23435.
444 let predicates = mth.method.predicates.predicates.subst(tcx, mth.substs);
445 if !normalize_and_test_predicates(ccx, predicates.into_vec()) {
446 debug!("get_vtable_methods: predicates do not hold");
456 /// Replace the self type (&Self or Box<Self>) with an opaque pointer.
457 fn opaque_method_ty<'tcx>(tcx: &TyCtxt<'tcx>, method_ty: &ty::BareFnTy<'tcx>)
459 let mut inputs = method_ty.sig.0.inputs.clone();
460 inputs[0] = tcx.mk_mut_ptr(tcx.mk_mach_int(ast::IntTy::I8));
462 tcx.mk_fn_ptr(ty::BareFnTy {
463 unsafety: method_ty.unsafety,
465 sig: ty::Binder(ty::FnSig {
467 output: method_ty.sig.0.output,
468 variadic: method_ty.sig.0.variadic,
474 pub struct ImplMethod<'tcx> {
475 pub method: Rc<ty::Method<'tcx>>,
476 pub substs: Substs<'tcx>,
477 pub is_provided: bool
480 /// Locates the applicable definition of a method, given its name.
481 pub fn get_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>,
483 substs: Substs<'tcx>,
487 assert!(!substs.types.needs_infer());
489 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
490 let trait_def = tcx.lookup_trait_def(trait_def_id);
491 let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables);
493 match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
496 method: node_item.item,
497 substs: traits::translate_substs(&infcx, impl_def_id, substs, node_item.node),
498 is_provided: node_item.node.is_from_trait(),
502 tcx.sess.bug(&format!("method {:?} not found in {:?}", name, impl_def_id))