Skip to content

Commit baa0105

Browse files
committed
copy resolve() over to librustc
1 parent b7041bf commit baa0105

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

src/librustc/ty/instance.rs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
use hir::def_id::DefId;
1212
use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
13+
use ty::subst::{Kind, Subst};
14+
use traits;
15+
use syntax::abi::Abi;
16+
use syntax::codemap::DUMMY_SP;
1317
use util::ppaux;
1418

1519
use std::fmt;
@@ -111,4 +115,191 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
111115
pub fn def_id(&self) -> DefId {
112116
self.def.def_id()
113117
}
118+
119+
/// The point where linking happens. Resolve a (def_id, substs)
120+
/// pair to an instance.
121+
pub fn resolve(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Instance<'tcx>> {
122+
let def_id = self.def_id();
123+
debug!("resolve(def_id={:?}, substs={:?})", def_id, self.substs);
124+
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
125+
debug!(" => associated item, attempting to find impl");
126+
let item = tcx.associated_item(def_id);
127+
resolve_associated_item(tcx, &item, trait_def_id, self.substs)
128+
} else {
129+
let ty = tcx.type_of(def_id);
130+
let item_type = tcx.trans_apply_param_substs(self.substs, &ty);
131+
132+
let def = match item_type.sty {
133+
ty::TyFnDef(..) if {
134+
let f = item_type.fn_sig(tcx);
135+
f.abi() == Abi::RustIntrinsic ||
136+
f.abi() == Abi::PlatformIntrinsic
137+
} =>
138+
{
139+
debug!(" => intrinsic");
140+
ty::InstanceDef::Intrinsic(def_id)
141+
}
142+
_ => {
143+
if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
144+
let ty = self.substs.type_at(0);
145+
if ty.needs_drop(tcx, ty::ParamEnv::empty(traits::Reveal::All)) {
146+
debug!(" => nontrivial drop glue");
147+
ty::InstanceDef::DropGlue(def_id, Some(ty))
148+
} else {
149+
debug!(" => trivial drop glue");
150+
ty::InstanceDef::DropGlue(def_id, None)
151+
}
152+
} else {
153+
debug!(" => free item");
154+
ty::InstanceDef::Item(def_id)
155+
}
156+
}
157+
};
158+
Some(Instance {
159+
def: def,
160+
substs: self.substs
161+
})
162+
};
163+
debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, self.substs, result);
164+
result
165+
}
166+
167+
168+
}
169+
170+
fn resolve_closure<'a, 'tcx>(
171+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
172+
def_id: DefId,
173+
substs: ty::ClosureSubsts<'tcx>,
174+
requested_kind: ty::ClosureKind)
175+
-> Instance<'tcx>
176+
{
177+
let actual_kind = tcx.closure_kind(def_id);
178+
179+
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
180+
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
181+
_ => Instance::new(def_id, substs.substs)
182+
}
183+
}
184+
185+
fn resolve_associated_item<'a, 'tcx>(
186+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
187+
trait_item: &ty::AssociatedItem,
188+
trait_id: DefId,
189+
rcvr_substs: &'tcx Substs<'tcx>
190+
) -> Option<Instance<'tcx>> {
191+
let def_id = trait_item.def_id;
192+
debug!("resolve_associated_item(trait_item={:?}, \
193+
trait_id={:?}, \
194+
rcvr_substs={:?})",
195+
def_id, trait_id, rcvr_substs);
196+
197+
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
198+
let vtbl = tcx.trans_fulfill_obligation(DUMMY_SP, ty::Binder(trait_ref));
199+
200+
// Now that we know which impl is being used, we can dispatch to
201+
// the actual function:
202+
match vtbl {
203+
traits::VtableImpl(impl_data) => {
204+
let (def_id, substs) = traits::find_associated_item(
205+
tcx, trait_item, rcvr_substs, &impl_data);
206+
let substs = tcx.erase_regions(&substs);
207+
Some(ty::Instance::new(def_id, substs))
208+
}
209+
traits::VtableGenerator(closure_data) => {
210+
Some(Instance {
211+
def: ty::InstanceDef::Item(closure_data.closure_def_id),
212+
substs: closure_data.substs.substs
213+
})
214+
}
215+
traits::VtableClosure(closure_data) => {
216+
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
217+
Some(resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
218+
trait_closure_kind))
219+
}
220+
traits::VtableFnPointer(ref data) => {
221+
Some(Instance {
222+
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
223+
substs: rcvr_substs
224+
})
225+
}
226+
traits::VtableObject(ref data) => {
227+
let index = tcx.get_vtable_index_of_object_method(data, def_id);
228+
Some(Instance {
229+
def: ty::InstanceDef::Virtual(def_id, index),
230+
substs: rcvr_substs
231+
})
232+
}
233+
traits::VtableBuiltin(..) if Some(trait_id) == tcx.lang_items().clone_trait() => {
234+
Some(Instance {
235+
def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
236+
substs: rcvr_substs
237+
})
238+
}
239+
_ => {
240+
None
241+
}
242+
}
243+
}
244+
245+
fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
246+
trait_closure_kind: ty::ClosureKind)
247+
-> Result<bool, ()>
248+
{
249+
match (actual_closure_kind, trait_closure_kind) {
250+
(ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
251+
(ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
252+
(ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
253+
// No adapter needed.
254+
Ok(false)
255+
}
256+
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
257+
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
258+
// `fn(&mut self, ...)`. In fact, at trans time, these are
259+
// basically the same thing, so we can just return llfn.
260+
Ok(false)
261+
}
262+
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
263+
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
264+
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
265+
// self, ...)`. We want a `fn(self, ...)`. We can produce
266+
// this by doing something like:
267+
//
268+
// fn call_once(self, ...) { call_mut(&self, ...) }
269+
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
270+
//
271+
// These are both the same at trans time.
272+
Ok(true)
273+
}
274+
_ => Err(()),
275+
}
276+
}
277+
278+
fn fn_once_adapter_instance<'a, 'tcx>(
279+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
280+
closure_did: DefId,
281+
substs: ty::ClosureSubsts<'tcx>,
282+
) -> Instance<'tcx> {
283+
debug!("fn_once_adapter_shim({:?}, {:?})",
284+
closure_did,
285+
substs);
286+
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
287+
let call_once = tcx.associated_items(fn_once)
288+
.find(|it| it.kind == ty::AssociatedKind::Method)
289+
.unwrap().def_id;
290+
let def = ty::InstanceDef::ClosureOnceShim { call_once };
291+
292+
let self_ty = tcx.mk_closure_from_closure_substs(
293+
closure_did, substs);
294+
295+
let sig = tcx.fn_sig(closure_did).subst(tcx, substs.substs);
296+
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
297+
assert_eq!(sig.inputs().len(), 1);
298+
let substs = tcx.mk_substs([
299+
Kind::from(self_ty),
300+
Kind::from(sig.inputs()[0]),
301+
].iter().cloned());
302+
303+
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
304+
Instance { def, substs }
114305
}

0 commit comments

Comments
 (0)