|
| 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. |
| 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 | +use rustc::hir::def_id::DefId; |
| 12 | +use rustc::middle::lang_items::DropInPlaceFnLangItem; |
| 13 | +use rustc::traits; |
| 14 | +use rustc::ty::adjustment::CustomCoerceUnsized; |
| 15 | +use rustc::ty::subst::Kind; |
| 16 | +use rustc::ty::{self, Ty, TyCtxt}; |
| 17 | + |
| 18 | +pub use rustc::ty::Instance; |
| 19 | + |
1 | 20 | pub mod collector;
|
| 21 | +pub mod mono_item; |
| 22 | + |
| 23 | +fn fn_once_adapter_instance<'a, 'tcx>( |
| 24 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 25 | + closure_did: DefId, |
| 26 | + substs: ty::ClosureSubsts<'tcx>, |
| 27 | + ) -> Instance<'tcx> { |
| 28 | + debug!("fn_once_adapter_shim({:?}, {:?})", |
| 29 | + closure_did, |
| 30 | + substs); |
| 31 | + let fn_once = tcx.lang_items().fn_once_trait().unwrap(); |
| 32 | + let call_once = tcx.associated_items(fn_once) |
| 33 | + .find(|it| it.kind == ty::AssociatedKind::Method) |
| 34 | + .unwrap().def_id; |
| 35 | + let def = ty::InstanceDef::ClosureOnceShim { call_once }; |
| 36 | + |
| 37 | + let self_ty = tcx.mk_closure_from_closure_substs( |
| 38 | + closure_did, substs); |
| 39 | + |
| 40 | + let sig = substs.closure_sig(closure_did, tcx); |
| 41 | + let sig = tcx.erase_late_bound_regions_and_normalize(&sig); |
| 42 | + assert_eq!(sig.inputs().len(), 1); |
| 43 | + let substs = tcx.mk_substs([ |
| 44 | + Kind::from(self_ty), |
| 45 | + Kind::from(sig.inputs()[0]), |
| 46 | + ].iter().cloned()); |
| 47 | + |
| 48 | + debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig); |
| 49 | + Instance { def, substs } |
| 50 | +} |
| 51 | + |
| 52 | +fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind, |
| 53 | + trait_closure_kind: ty::ClosureKind) |
| 54 | + -> Result<bool, ()> |
| 55 | +{ |
| 56 | + match (actual_closure_kind, trait_closure_kind) { |
| 57 | + (ty::ClosureKind::Fn, ty::ClosureKind::Fn) | |
| 58 | + (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | |
| 59 | + (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { |
| 60 | + // No adapter needed. |
| 61 | + Ok(false) |
| 62 | + } |
| 63 | + (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { |
| 64 | + // The closure fn `llfn` is a `fn(&self, ...)`. We want a |
| 65 | + // `fn(&mut self, ...)`. In fact, at trans time, these are |
| 66 | + // basically the same thing, so we can just return llfn. |
| 67 | + Ok(false) |
| 68 | + } |
| 69 | + (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) | |
| 70 | + (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { |
| 71 | + // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut |
| 72 | + // self, ...)`. We want a `fn(self, ...)`. We can produce |
| 73 | + // this by doing something like: |
| 74 | + // |
| 75 | + // fn call_once(self, ...) { call_mut(&self, ...) } |
| 76 | + // fn call_once(mut self, ...) { call_mut(&mut self, ...) } |
| 77 | + // |
| 78 | + // These are both the same at trans time. |
| 79 | + Ok(true) |
| 80 | + } |
| 81 | + _ => Err(()), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub fn resolve_closure<'a, 'tcx> ( |
| 86 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 87 | + def_id: DefId, |
| 88 | + substs: ty::ClosureSubsts<'tcx>, |
| 89 | + requested_kind: ty::ClosureKind) |
| 90 | + -> Instance<'tcx> |
| 91 | +{ |
| 92 | + let actual_kind = substs.closure_kind(def_id, tcx); |
| 93 | + |
| 94 | + match needs_fn_once_adapter_shim(actual_kind, requested_kind) { |
| 95 | + Ok(true) => fn_once_adapter_instance(tcx, def_id, substs), |
| 96 | + _ => Instance::new(def_id, substs.substs) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +pub fn resolve_drop_in_place<'a, 'tcx>( |
| 101 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 102 | + ty: Ty<'tcx>) |
| 103 | + -> ty::Instance<'tcx> |
| 104 | +{ |
| 105 | + let def_id = tcx.require_lang_item(DropInPlaceFnLangItem); |
| 106 | + let substs = tcx.intern_substs(&[Kind::from(ty)]); |
| 107 | + Instance::resolve(tcx, ty::ParamEnv::empty(traits::Reveal::All), def_id, substs).unwrap() |
| 108 | +} |
| 109 | + |
| 110 | +pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 111 | + source_ty: Ty<'tcx>, |
| 112 | + target_ty: Ty<'tcx>) |
| 113 | + -> CustomCoerceUnsized { |
| 114 | + let def_id = tcx.lang_items().coerce_unsized_trait().unwrap(); |
| 115 | + |
| 116 | + let trait_ref = ty::Binder(ty::TraitRef { |
| 117 | + def_id: def_id, |
| 118 | + substs: tcx.mk_substs_trait(source_ty, &[target_ty]) |
| 119 | + }); |
| 120 | + |
| 121 | + match tcx.trans_fulfill_obligation( (ty::ParamEnv::empty(traits::Reveal::All), trait_ref)) { |
| 122 | + traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => { |
| 123 | + tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap() |
| 124 | + } |
| 125 | + vtable => { |
| 126 | + bug!("invalid CoerceUnsized vtable: {:?}", vtable); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments