]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/instance.rs
rustc: move Instance::fn_sig to ty::layout and privatize it.
[rust.git] / src / librustc / ty / instance.rs
1 use crate::hir::CodegenFnAttrFlags;
2 use crate::hir::def::Namespace;
3 use crate::hir::def_id::DefId;
4 use crate::ty::{self, Ty, TypeFoldable, SubstsRef, TyCtxt};
5 use crate::ty::print::{FmtPrinter, Printer};
6 use crate::traits;
7 use crate::middle::lang_items::DropInPlaceFnLangItem;
8 use rustc_target::spec::abi::Abi;
9 use rustc_macros::HashStable;
10
11 use std::fmt;
12
13 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
14 #[derive(HashStable, Lift)]
15 pub struct Instance<'tcx> {
16     pub def: InstanceDef<'tcx>,
17     pub substs: SubstsRef<'tcx>,
18 }
19
20 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
21 pub enum InstanceDef<'tcx> {
22     Item(DefId),
23     Intrinsic(DefId),
24
25     /// `<T as Trait>::method` where `method` receives unsizeable `self: Self`.
26     VtableShim(DefId),
27
28     /// `fn()` pointer where the function itself cannot be turned into a pointer.
29     ///
30     /// One example is `<dyn Trait as Trait>::fn`, where the shim contains
31     /// a virtual call, which codegen supports only via a direct call to the
32     /// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
33     ///
34     /// Another example is functions annotated with `#[track_caller]`, which
35     /// must have their implicit caller location argument populated for a call.
36     /// Because this is a required part of the function's ABI but can't be tracked
37     /// as a property of the function pointer, we use a single "caller location"
38     /// (the definition of the function itself).
39     ReifyShim(DefId),
40
41     /// `<fn() as FnTrait>::call_*`
42     /// `DefId` is `FnTrait::call_*`.
43     FnPtrShim(DefId, Ty<'tcx>),
44
45     /// `<dyn Trait as Trait>::fn`
46     Virtual(DefId, usize),
47
48     /// `<[mut closure] as FnOnce>::call_once`
49     ClosureOnceShim { call_once: DefId },
50
51     /// `drop_in_place::<T>; None` for empty drop glue.
52     DropGlue(DefId, Option<Ty<'tcx>>),
53
54     ///`<T as Clone>::clone` shim.
55     CloneShim(DefId, Ty<'tcx>),
56 }
57
58 impl<'tcx> Instance<'tcx> {
59     pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
60         let ty = tcx.type_of(self.def.def_id());
61         tcx.subst_and_normalize_erasing_regions(
62             self.substs,
63             ty::ParamEnv::reveal_all(),
64             &ty,
65         )
66     }
67 }
68
69 impl<'tcx> InstanceDef<'tcx> {
70     #[inline]
71     pub fn def_id(&self) -> DefId {
72         match *self {
73             InstanceDef::Item(def_id) |
74             InstanceDef::VtableShim(def_id) |
75             InstanceDef::ReifyShim(def_id) |
76             InstanceDef::FnPtrShim(def_id, _) |
77             InstanceDef::Virtual(def_id, _) |
78             InstanceDef::Intrinsic(def_id, ) |
79             InstanceDef::ClosureOnceShim { call_once: def_id } |
80             InstanceDef::DropGlue(def_id, _) |
81             InstanceDef::CloneShim(def_id, _) => def_id
82         }
83     }
84
85     #[inline]
86     pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
87         tcx.get_attrs(self.def_id())
88     }
89
90     pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
91         use crate::hir::map::DefPathData;
92         let def_id = match *self {
93             ty::InstanceDef::Item(def_id) => def_id,
94             ty::InstanceDef::DropGlue(_, Some(_)) => return false,
95             _ => return true
96         };
97         match tcx.def_key(def_id).disambiguated_data.data {
98             DefPathData::Ctor | DefPathData::ClosureExpr => true,
99             _ => false
100         }
101     }
102
103     pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
104         if self.is_inline(tcx) {
105             return true
106         }
107         if let ty::InstanceDef::DropGlue(..) = *self {
108             // Drop glue wants to be instantiated at every codegen
109             // unit, but without an #[inline] hint. We should make this
110             // available to normal end-users.
111             return true
112         }
113         tcx.codegen_fn_attrs(self.def_id()).requests_inline()
114     }
115 }
116
117 impl<'tcx> fmt::Display for Instance<'tcx> {
118     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119         ty::tls::with(|tcx| {
120             let substs = tcx.lift(&self.substs).expect("could not lift for printing");
121             FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
122                 .print_def_path(self.def_id(), substs)?;
123             Ok(())
124         })?;
125
126         match self.def {
127             InstanceDef::Item(_) => Ok(()),
128             InstanceDef::VtableShim(_) => {
129                 write!(f, " - shim(vtable)")
130             }
131             InstanceDef::ReifyShim(_) => {
132                 write!(f, " - shim(reify)")
133             }
134             InstanceDef::Intrinsic(_) => {
135                 write!(f, " - intrinsic")
136             }
137             InstanceDef::Virtual(_, num) => {
138                 write!(f, " - virtual#{}", num)
139             }
140             InstanceDef::FnPtrShim(_, ty) => {
141                 write!(f, " - shim({:?})", ty)
142             }
143             InstanceDef::ClosureOnceShim { .. } => {
144                 write!(f, " - shim")
145             }
146             InstanceDef::DropGlue(_, ty) => {
147                 write!(f, " - shim({:?})", ty)
148             }
149             InstanceDef::CloneShim(_, ty) => {
150                 write!(f, " - shim({:?})", ty)
151             }
152         }
153     }
154 }
155
156 impl<'tcx> Instance<'tcx> {
157     pub fn new(def_id: DefId, substs: SubstsRef<'tcx>)
158                -> Instance<'tcx> {
159         assert!(!substs.has_escaping_bound_vars(),
160                 "substs of instance {:?} not normalized for codegen: {:?}",
161                 def_id, substs);
162         Instance { def: InstanceDef::Item(def_id), substs: substs }
163     }
164
165     pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
166         Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
167     }
168
169     #[inline]
170     pub fn def_id(&self) -> DefId {
171         self.def.def_id()
172     }
173
174     /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
175     /// this is used to find the precise code that will run for a trait method invocation,
176     /// if known.
177     ///
178     /// Returns `None` if we cannot resolve `Instance` to a specific instance.
179     /// For example, in a context like this,
180     ///
181     /// ```
182     /// fn foo<T: Debug>(t: T) { ... }
183     /// ```
184     ///
185     /// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not
186     /// know what code ought to run. (Note that this setting is also affected by the
187     /// `RevealMode` in the parameter environment.)
188     ///
189     /// Presuming that coherence and type-check have succeeded, if this method is invoked
190     /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
191     /// `Some`.
192     pub fn resolve(
193         tcx: TyCtxt<'tcx>,
194         param_env: ty::ParamEnv<'tcx>,
195         def_id: DefId,
196         substs: SubstsRef<'tcx>,
197     ) -> Option<Instance<'tcx>> {
198         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
199         let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
200             debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
201             let item = tcx.associated_item(def_id);
202             resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
203         } else {
204             let ty = tcx.type_of(def_id);
205             let item_type = tcx.subst_and_normalize_erasing_regions(
206                 substs,
207                 param_env,
208                 &ty,
209             );
210
211             let def = match item_type.kind {
212                 ty::FnDef(..) if {
213                     let f = item_type.fn_sig(tcx);
214                     f.abi() == Abi::RustIntrinsic ||
215                         f.abi() == Abi::PlatformIntrinsic
216                 } =>
217                 {
218                     debug!(" => intrinsic");
219                     ty::InstanceDef::Intrinsic(def_id)
220                 }
221                 _ => {
222                     if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
223                         let ty = substs.type_at(0);
224                         if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) {
225                             debug!(" => nontrivial drop glue");
226                             ty::InstanceDef::DropGlue(def_id, Some(ty))
227                         } else {
228                             debug!(" => trivial drop glue");
229                             ty::InstanceDef::DropGlue(def_id, None)
230                         }
231                     } else {
232                         debug!(" => free item");
233                         ty::InstanceDef::Item(def_id)
234                     }
235                 }
236             };
237             Some(Instance {
238                 def: def,
239                 substs: substs
240             })
241         };
242         debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
243         result
244     }
245
246     pub fn resolve_for_fn_ptr(
247         tcx: TyCtxt<'tcx>,
248         param_env: ty::ParamEnv<'tcx>,
249         def_id: DefId,
250         substs: SubstsRef<'tcx>,
251     ) -> Option<Instance<'tcx>> {
252         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
253         Instance::resolve(tcx, param_env, def_id, substs).map(|mut resolved| {
254             let has_track_caller = |def| tcx.codegen_fn_attrs(def).flags
255                 .contains(CodegenFnAttrFlags::TRACK_CALLER);
256
257             match resolved.def {
258                 InstanceDef::Item(def_id) if has_track_caller(def_id) => {
259                     debug!(" => fn pointer created for function with #[track_caller]");
260                     resolved.def = InstanceDef::ReifyShim(def_id);
261                 }
262                 InstanceDef::Virtual(def_id, _) => {
263                     debug!(" => fn pointer created for virtual call");
264                     resolved.def = InstanceDef::ReifyShim(def_id);
265                 }
266                 _ => {}
267             }
268
269             resolved
270         })
271     }
272
273     pub fn resolve_for_vtable(
274         tcx: TyCtxt<'tcx>,
275         param_env: ty::ParamEnv<'tcx>,
276         def_id: DefId,
277         substs: SubstsRef<'tcx>,
278     ) -> Option<Instance<'tcx>> {
279         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
280         let fn_sig = tcx.fn_sig(def_id);
281         let is_vtable_shim = fn_sig.inputs().skip_binder().len() > 0
282             && fn_sig.input(0).skip_binder().is_param(0)
283             && tcx.generics_of(def_id).has_self;
284         if is_vtable_shim {
285             debug!(" => associated item with unsizeable self: Self");
286             Some(Instance {
287                 def: InstanceDef::VtableShim(def_id),
288                 substs,
289             })
290         } else {
291             Instance::resolve(tcx, param_env, def_id, substs)
292         }
293     }
294
295     pub fn resolve_closure(
296         tcx: TyCtxt<'tcx>,
297         def_id: DefId,
298         substs: ty::SubstsRef<'tcx>,
299         requested_kind: ty::ClosureKind,
300     ) -> Instance<'tcx> {
301         let actual_kind = substs.as_closure().kind(def_id, tcx);
302
303         match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
304             Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
305             _ => Instance::new(def_id, substs)
306         }
307     }
308
309     pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
310         let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None);
311         let substs = tcx.intern_substs(&[ty.into()]);
312         Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
313     }
314
315     pub fn fn_once_adapter_instance(
316         tcx: TyCtxt<'tcx>,
317         closure_did: DefId,
318         substs: ty::SubstsRef<'tcx>,
319     ) -> Instance<'tcx> {
320         debug!("fn_once_adapter_shim({:?}, {:?})",
321                closure_did,
322                substs);
323         let fn_once = tcx.lang_items().fn_once_trait().unwrap();
324         let call_once = tcx.associated_items(fn_once)
325             .find(|it| it.kind == ty::AssocKind::Method)
326             .unwrap().def_id;
327         let def = ty::InstanceDef::ClosureOnceShim { call_once };
328
329         let self_ty = tcx.mk_closure(closure_did, substs);
330
331         let sig = substs.as_closure().sig(closure_did, tcx);
332         let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
333         assert_eq!(sig.inputs().len(), 1);
334         let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
335
336         debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
337         Instance { def, substs }
338     }
339
340     pub fn is_vtable_shim(&self) -> bool {
341         if let InstanceDef::VtableShim(..) = self.def {
342             true
343         } else {
344             false
345         }
346     }
347 }
348
349 fn resolve_associated_item<'tcx>(
350     tcx: TyCtxt<'tcx>,
351     trait_item: &ty::AssocItem,
352     param_env: ty::ParamEnv<'tcx>,
353     trait_id: DefId,
354     rcvr_substs: SubstsRef<'tcx>,
355 ) -> Option<Instance<'tcx>> {
356     let def_id = trait_item.def_id;
357     debug!("resolve_associated_item(trait_item={:?}, \
358             param_env={:?}, \
359             trait_id={:?}, \
360             rcvr_substs={:?})",
361             def_id, param_env, trait_id, rcvr_substs);
362
363     let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
364     let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)));
365
366     // Now that we know which impl is being used, we can dispatch to
367     // the actual function:
368     match vtbl {
369         traits::VtableImpl(impl_data) => {
370             let (def_id, substs) = traits::find_associated_item(
371                 tcx, param_env, trait_item, rcvr_substs, &impl_data);
372             let substs = tcx.erase_regions(&substs);
373             Some(ty::Instance::new(def_id, substs))
374         }
375         traits::VtableGenerator(generator_data) => {
376             Some(Instance {
377                 def: ty::InstanceDef::Item(generator_data.generator_def_id),
378                 substs: generator_data.substs
379             })
380         }
381         traits::VtableClosure(closure_data) => {
382             let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
383             Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
384                                            trait_closure_kind))
385         }
386         traits::VtableFnPointer(ref data) => {
387             Some(Instance {
388                 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
389                 substs: rcvr_substs
390             })
391         }
392         traits::VtableObject(ref data) => {
393             let index = tcx.get_vtable_index_of_object_method(data, def_id);
394             Some(Instance {
395                 def: ty::InstanceDef::Virtual(def_id, index),
396                 substs: rcvr_substs
397             })
398         }
399         traits::VtableBuiltin(..) => {
400             if tcx.lang_items().clone_trait().is_some() {
401                 Some(Instance {
402                     def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
403                     substs: rcvr_substs
404                 })
405             } else {
406                 None
407             }
408         }
409         traits::VtableAutoImpl(..) |
410         traits::VtableParam(..) |
411         traits::VtableTraitAlias(..) => None
412     }
413 }
414
415 fn needs_fn_once_adapter_shim(
416     actual_closure_kind: ty::ClosureKind,
417     trait_closure_kind: ty::ClosureKind,
418 ) -> Result<bool, ()> {
419     match (actual_closure_kind, trait_closure_kind) {
420         (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
421             (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
422             (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
423                 // No adapter needed.
424                 Ok(false)
425             }
426         (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
427             // The closure fn `llfn` is a `fn(&self, ...)`.  We want a
428             // `fn(&mut self, ...)`. In fact, at codegen time, these are
429             // basically the same thing, so we can just return llfn.
430             Ok(false)
431         }
432         (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
433             (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
434                 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
435                 // self, ...)`.  We want a `fn(self, ...)`. We can produce
436                 // this by doing something like:
437                 //
438                 //     fn call_once(self, ...) { call_mut(&self, ...) }
439                 //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
440                 //
441                 // These are both the same at codegen time.
442                 Ok(true)
443         }
444         (ty::ClosureKind::FnMut, _) |
445         (ty::ClosureKind::FnOnce, _) => Err(())
446     }
447 }