1 // Copyright 2012-2014 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.
14 use rustc::hir::def_id::DefId;
15 use rustc::middle::lang_items::DropInPlaceFnLangItem;
17 use rustc::ty::adjustment::CustomCoerceUnsized;
18 use rustc::ty::subst::{Kind, Subst, Substs};
19 use rustc::ty::{self, Ty, TyCtxt};
21 use syntax::codemap::DUMMY_SP;
23 pub use rustc::ty::Instance;
25 fn fn_once_adapter_instance<'a, 'tcx>(
26 tcx: TyCtxt<'a, 'tcx, 'tcx>,
28 substs: ty::ClosureSubsts<'tcx>,
30 debug!("fn_once_adapter_shim({:?}, {:?})",
33 let fn_once = tcx.lang_items().fn_once_trait().unwrap();
34 let call_once = tcx.associated_items(fn_once)
35 .find(|it| it.kind == ty::AssociatedKind::Method)
37 let def = ty::InstanceDef::ClosureOnceShim { call_once };
39 let self_ty = tcx.mk_closure_from_closure_substs(
42 let sig = tcx.fn_sig(closure_did).subst(tcx, substs.substs);
43 let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
44 assert_eq!(sig.inputs().len(), 1);
45 let substs = tcx.mk_substs([
47 Kind::from(sig.inputs()[0]),
50 debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
51 Instance { def, substs }
54 fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
55 trait_closure_kind: ty::ClosureKind)
58 match (actual_closure_kind, trait_closure_kind) {
59 (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
60 (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
61 (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
65 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
66 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
67 // `fn(&mut self, ...)`. In fact, at trans time, these are
68 // basically the same thing, so we can just return llfn.
71 (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
72 (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
73 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
74 // self, ...)`. We want a `fn(self, ...)`. We can produce
75 // this by doing something like:
77 // fn call_once(self, ...) { call_mut(&self, ...) }
78 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
80 // These are both the same at trans time.
87 pub fn resolve_closure<'a, 'tcx> (
88 scx: &SharedCrateContext<'a, 'tcx>,
90 substs: ty::ClosureSubsts<'tcx>,
91 requested_kind: ty::ClosureKind)
94 let actual_kind = scx.tcx().closure_kind(def_id);
96 match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
97 Ok(true) => fn_once_adapter_instance(scx.tcx(), def_id, substs),
98 _ => Instance::new(def_id, substs.substs)
102 fn resolve_associated_item<'a, 'tcx>(
103 scx: &SharedCrateContext<'a, 'tcx>,
104 trait_item: &ty::AssociatedItem,
106 rcvr_substs: &'tcx Substs<'tcx>
107 ) -> Instance<'tcx> {
109 let def_id = trait_item.def_id;
110 debug!("resolve_associated_item(trait_item={:?}, \
113 def_id, trait_id, rcvr_substs);
115 let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
116 let vtbl = tcx.trans_fulfill_obligation(DUMMY_SP, ty::Binder(trait_ref));
118 // Now that we know which impl is being used, we can dispatch to
119 // the actual function:
121 traits::VtableImpl(impl_data) => {
122 let (def_id, substs) = traits::find_associated_item(
123 tcx, trait_item, rcvr_substs, &impl_data);
124 let substs = tcx.erase_regions(&substs);
125 ty::Instance::new(def_id, substs)
127 traits::VtableGenerator(closure_data) => {
129 def: ty::InstanceDef::Item(closure_data.closure_def_id),
130 substs: closure_data.substs.substs
133 traits::VtableClosure(closure_data) => {
134 let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
135 resolve_closure(scx, closure_data.closure_def_id, closure_data.substs,
138 traits::VtableFnPointer(ref data) => {
140 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
144 traits::VtableObject(ref data) => {
145 let index = tcx.get_vtable_index_of_object_method(data, def_id);
147 def: ty::InstanceDef::Virtual(def_id, index),
151 traits::VtableBuiltin(..) if Some(trait_id) == tcx.lang_items().clone_trait() => {
153 def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
158 bug!("static call to invalid vtable: {:?}", vtbl)
163 /// The point where linking happens. Resolve a (def_id, substs)
164 /// pair to an instance.
165 pub fn resolve<'a, 'tcx>(
166 scx: &SharedCrateContext<'a, 'tcx>,
168 substs: &'tcx Substs<'tcx>
169 ) -> Instance<'tcx> {
170 debug!("resolve(def_id={:?}, substs={:?})",
172 let result = if let Some(trait_def_id) = scx.tcx().trait_of_item(def_id) {
173 debug!(" => associated item, attempting to find impl");
174 let item = scx.tcx().associated_item(def_id);
175 resolve_associated_item(scx, &item, trait_def_id, substs)
177 let item_type = def_ty(scx, def_id, substs);
178 let def = match item_type.sty {
180 let f = item_type.fn_sig(scx.tcx());
181 f.abi() == Abi::RustIntrinsic ||
182 f.abi() == Abi::PlatformIntrinsic
185 debug!(" => intrinsic");
186 ty::InstanceDef::Intrinsic(def_id)
189 if Some(def_id) == scx.tcx().lang_items().drop_in_place_fn() {
190 let ty = substs.type_at(0);
191 if scx.type_needs_drop(ty) {
192 debug!(" => nontrivial drop glue");
193 ty::InstanceDef::DropGlue(def_id, Some(ty))
195 debug!(" => trivial drop glue");
196 ty::InstanceDef::DropGlue(def_id, None)
199 debug!(" => free item");
200 ty::InstanceDef::Item(def_id)
204 Instance { def, substs }
206 debug!("resolve(def_id={:?}, substs={:?}) = {}",
207 def_id, substs, result);
211 pub fn resolve_drop_in_place<'a, 'tcx>(
212 scx: &SharedCrateContext<'a, 'tcx>,
214 -> ty::Instance<'tcx>
216 let def_id = scx.tcx().require_lang_item(DropInPlaceFnLangItem);
217 let substs = scx.tcx().intern_substs(&[Kind::from(ty)]);
218 resolve(scx, def_id, substs)
221 pub fn custom_coerce_unsize_info<'scx, 'tcx>(scx: &SharedCrateContext<'scx, 'tcx>,
224 -> CustomCoerceUnsized {
225 let trait_ref = ty::Binder(ty::TraitRef {
226 def_id: scx.tcx().lang_items().coerce_unsized_trait().unwrap(),
227 substs: scx.tcx().mk_substs_trait(source_ty, &[target_ty])
230 match scx.tcx().trans_fulfill_obligation(DUMMY_SP, trait_ref) {
231 traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
232 scx.tcx().coerce_unsized_info(impl_def_id).custom_kind.unwrap()
235 bug!("invalid CoerceUnsized vtable: {:?}", vtable);
240 /// Returns the normalized type of a struct field
241 pub fn field_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
242 param_substs: &Substs<'tcx>,
243 f: &'tcx ty::FieldDef)
246 tcx.normalize_associated_type(&f.ty(tcx, param_substs))