1 // Copyright 2016 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.
11 use hir::def_id::DefId;
12 use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
14 use rustc_target::spec::abi::Abi;
19 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
20 pub struct Instance<'tcx> {
21 pub def: InstanceDef<'tcx>,
22 pub substs: &'tcx Substs<'tcx>,
25 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
26 pub enum InstanceDef<'tcx> {
30 /// \<fn() as FnTrait>::call_*
31 /// def-id is FnTrait::call_*
32 FnPtrShim(DefId, Ty<'tcx>),
34 /// <Trait as Trait>::fn
35 Virtual(DefId, usize),
37 /// <[mut closure] as FnOnce>::call_once
38 ClosureOnceShim { call_once: DefId },
40 /// drop_in_place::<T>; None for empty drop glue.
41 DropGlue(DefId, Option<Ty<'tcx>>),
43 ///`<T as Clone>::clone` shim.
44 CloneShim(DefId, Ty<'tcx>),
47 impl<'a, 'tcx> Instance<'tcx> {
49 tcx: TyCtxt<'a, 'tcx, 'tcx>)
52 let ty = tcx.type_of(self.def.def_id());
53 tcx.subst_and_normalize_erasing_regions(
55 ty::ParamEnv::reveal_all(),
61 impl<'tcx> InstanceDef<'tcx> {
63 pub fn def_id(&self) -> DefId {
65 InstanceDef::Item(def_id) |
66 InstanceDef::FnPtrShim(def_id, _) |
67 InstanceDef::Virtual(def_id, _) |
68 InstanceDef::Intrinsic(def_id, ) |
69 InstanceDef::ClosureOnceShim { call_once: def_id } |
70 InstanceDef::DropGlue(def_id, _) |
71 InstanceDef::CloneShim(def_id, _) => def_id
76 pub fn attrs<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::Attributes<'tcx> {
77 tcx.get_attrs(self.def_id())
82 tcx: TyCtxt<'a, 'tcx, 'tcx>
84 use hir::map::DefPathData;
85 let def_id = match *self {
86 ty::InstanceDef::Item(def_id) => def_id,
87 ty::InstanceDef::DropGlue(_, Some(_)) => return false,
90 match tcx.def_key(def_id).disambiguated_data.data {
91 DefPathData::StructCtor |
92 DefPathData::EnumVariant(..) |
93 DefPathData::ClosureExpr => true,
98 pub fn requires_local<'a>(
100 tcx: TyCtxt<'a, 'tcx, 'tcx>
102 if self.is_inline(tcx) {
105 if let ty::InstanceDef::DropGlue(..) = *self {
106 // Drop glue wants to be instantiated at every codegen
107 // unit, but without an #[inline] hint. We should make this
108 // available to normal end-users.
111 let codegen_fn_attrs = tcx.codegen_fn_attrs(self.def_id());
112 codegen_fn_attrs.requests_inline() || tcx.is_const_fn(self.def_id())
116 impl<'tcx> fmt::Display for Instance<'tcx> {
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 ppaux::parameterized(f, self.substs, self.def_id(), &[])?;
120 InstanceDef::Item(_) => Ok(()),
121 InstanceDef::Intrinsic(_) => {
122 write!(f, " - intrinsic")
124 InstanceDef::Virtual(_, num) => {
125 write!(f, " - shim(#{})", num)
127 InstanceDef::FnPtrShim(_, ty) => {
128 write!(f, " - shim({:?})", ty)
130 InstanceDef::ClosureOnceShim { .. } => {
133 InstanceDef::DropGlue(_, ty) => {
134 write!(f, " - shim({:?})", ty)
136 InstanceDef::CloneShim(_, ty) => {
137 write!(f, " - shim({:?})", ty)
143 impl<'a, 'b, 'tcx> Instance<'tcx> {
144 pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
146 assert!(!substs.has_escaping_regions(),
147 "substs of instance {:?} not normalized for codegen: {:?}",
149 Instance { def: InstanceDef::Item(def_id), substs: substs }
152 pub fn mono(tcx: TyCtxt<'a, 'tcx, 'b>, def_id: DefId) -> Instance<'tcx> {
153 Instance::new(def_id, tcx.global_tcx().empty_substs_for_def_id(def_id))
157 pub fn def_id(&self) -> DefId {
161 /// Resolve a (def_id, substs) pair to an (optional) instance -- most commonly,
162 /// this is used to find the precise code that will run for a trait method invocation,
165 /// Returns `None` if we cannot resolve `Instance` to a specific instance.
166 /// For example, in a context like this,
169 /// fn foo<T: Debug>(t: T) { ... }
172 /// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not
173 /// know what code ought to run. (Note that this setting is also affected by the
174 /// `RevealMode` in the parameter environment.)
176 /// Presuming that coherence and type-check have succeeded, if this method is invoked
177 /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
179 pub fn resolve(tcx: TyCtxt<'a, 'tcx, 'tcx>,
180 param_env: ty::ParamEnv<'tcx>,
182 substs: &'tcx Substs<'tcx>) -> Option<Instance<'tcx>> {
183 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
184 let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
185 debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
186 let item = tcx.associated_item(def_id);
187 resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
189 let ty = tcx.type_of(def_id);
190 let item_type = tcx.subst_and_normalize_erasing_regions(
196 let def = match item_type.sty {
198 let f = item_type.fn_sig(tcx);
199 f.abi() == Abi::RustIntrinsic ||
200 f.abi() == Abi::PlatformIntrinsic
203 debug!(" => intrinsic");
204 ty::InstanceDef::Intrinsic(def_id)
207 if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
208 let ty = substs.type_at(0);
209 if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) {
210 debug!(" => nontrivial drop glue");
211 ty::InstanceDef::DropGlue(def_id, Some(ty))
213 debug!(" => trivial drop glue");
214 ty::InstanceDef::DropGlue(def_id, None)
217 debug!(" => free item");
218 ty::InstanceDef::Item(def_id)
227 debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
231 pub fn resolve_closure(
232 tcx: TyCtxt<'a, 'tcx, 'tcx>,
234 substs: ty::ClosureSubsts<'tcx>,
235 requested_kind: ty::ClosureKind)
238 let actual_kind = substs.closure_kind(def_id, tcx);
240 match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
241 Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
242 _ => Instance::new(def_id, substs.substs)
247 fn resolve_associated_item<'a, 'tcx>(
248 tcx: TyCtxt<'a, 'tcx, 'tcx>,
249 trait_item: &ty::AssociatedItem,
250 param_env: ty::ParamEnv<'tcx>,
252 rcvr_substs: &'tcx Substs<'tcx>,
253 ) -> Option<Instance<'tcx>> {
254 let def_id = trait_item.def_id;
255 debug!("resolve_associated_item(trait_item={:?}, \
258 def_id, trait_id, rcvr_substs);
260 let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
261 let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)));
263 // Now that we know which impl is being used, we can dispatch to
264 // the actual function:
266 traits::VtableImpl(impl_data) => {
267 let (def_id, substs) = traits::find_associated_item(
268 tcx, trait_item, rcvr_substs, &impl_data);
269 let substs = tcx.erase_regions(&substs);
270 Some(ty::Instance::new(def_id, substs))
272 traits::VtableGenerator(generator_data) => {
274 def: ty::InstanceDef::Item(generator_data.generator_def_id),
275 substs: generator_data.substs.substs
278 traits::VtableClosure(closure_data) => {
279 let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
280 Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
283 traits::VtableFnPointer(ref data) => {
285 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
289 traits::VtableObject(ref data) => {
290 let index = tcx.get_vtable_index_of_object_method(data, def_id);
292 def: ty::InstanceDef::Virtual(def_id, index),
296 traits::VtableBuiltin(..) => {
297 if let Some(_) = tcx.lang_items().clone_trait() {
299 def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
306 traits::VtableAutoImpl(..) | traits::VtableParam(..) => None
310 fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
311 trait_closure_kind: ty::ClosureKind)
314 match (actual_closure_kind, trait_closure_kind) {
315 (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
316 (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
317 (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
318 // No adapter needed.
321 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
322 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
323 // `fn(&mut self, ...)`. In fact, at codegen time, these are
324 // basically the same thing, so we can just return llfn.
327 (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
328 (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
329 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
330 // self, ...)`. We want a `fn(self, ...)`. We can produce
331 // this by doing something like:
333 // fn call_once(self, ...) { call_mut(&self, ...) }
334 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
336 // These are both the same at codegen time.
339 (ty::ClosureKind::FnMut, _) |
340 (ty::ClosureKind::FnOnce, _) => Err(())
344 fn fn_once_adapter_instance<'a, 'tcx>(
345 tcx: TyCtxt<'a, 'tcx, 'tcx>,
347 substs: ty::ClosureSubsts<'tcx>,
348 ) -> Instance<'tcx> {
349 debug!("fn_once_adapter_shim({:?}, {:?})",
352 let fn_once = tcx.lang_items().fn_once_trait().unwrap();
353 let call_once = tcx.associated_items(fn_once)
354 .find(|it| it.kind == ty::AssociatedKind::Method)
356 let def = ty::InstanceDef::ClosureOnceShim { call_once };
358 let self_ty = tcx.mk_closure(closure_did, substs);
360 let sig = substs.closure_sig(closure_did, tcx);
361 let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
362 assert_eq!(sig.inputs().len(), 1);
363 let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
365 debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
366 Instance { def, substs }