2017-02-08 18:31:03 +01:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use hir::def_id::DefId;
|
2017-09-07 22:22:08 -04:00
|
|
|
use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
|
2017-02-08 18:31:03 +01:00
|
|
|
use util::ppaux;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct Instance<'tcx> {
|
|
|
|
pub def: InstanceDef<'tcx>,
|
|
|
|
pub substs: &'tcx Substs<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum InstanceDef<'tcx> {
|
|
|
|
Item(DefId),
|
2017-03-08 01:41:26 +02:00
|
|
|
Intrinsic(DefId),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
|
|
|
/// <fn() as FnTrait>::call_*
|
|
|
|
/// def-id is FnTrait::call_*
|
2017-02-08 18:31:03 +01:00
|
|
|
FnPtrShim(DefId, Ty<'tcx>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
|
|
|
/// <Trait as Trait>::fn
|
2017-03-08 01:41:26 +02:00
|
|
|
Virtual(DefId, usize),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
|
|
|
/// <[mut closure] as FnOnce>::call_once
|
2017-03-08 23:19:09 +02:00
|
|
|
ClosureOnceShim { call_once: DefId },
|
2017-08-04 14:44:12 +02:00
|
|
|
|
|
|
|
/// drop_in_place::<T>; None for empty drop glue.
|
2017-03-14 01:08:21 +02:00
|
|
|
DropGlue(DefId, Option<Ty<'tcx>>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
|
|
|
/// Builtin method implementation, e.g. `Clone::clone`.
|
2017-08-07 16:21:08 +02:00
|
|
|
CloneShim(DefId, Ty<'tcx>),
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> InstanceDef<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
match *self {
|
|
|
|
InstanceDef::Item(def_id) |
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::FnPtrShim(def_id, _) |
|
|
|
|
InstanceDef::Virtual(def_id, _) |
|
|
|
|
InstanceDef::Intrinsic(def_id, ) |
|
2017-08-04 14:44:12 +02:00
|
|
|
InstanceDef::ClosureOnceShim { call_once: def_id } |
|
|
|
|
InstanceDef::DropGlue(def_id, _) |
|
2017-08-07 16:21:08 +02:00
|
|
|
InstanceDef::CloneShim(def_id, _) => def_id
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-09-07 22:22:08 -04:00
|
|
|
pub fn def_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
|
2017-04-24 15:20:46 +03:00
|
|
|
tcx.type_of(self.def_id())
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-09-07 22:22:08 -04:00
|
|
|
pub fn attrs<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::Attributes<'tcx> {
|
2017-02-08 18:31:03 +01:00
|
|
|
tcx.get_attrs(self.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-03-08 01:41:26 +02:00
|
|
|
ppaux::parameterized(f, self.substs, self.def_id(), &[])?;
|
2017-02-08 18:31:03 +01:00
|
|
|
match self.def {
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Item(_) => Ok(()),
|
|
|
|
InstanceDef::Intrinsic(_) => {
|
|
|
|
write!(f, " - intrinsic")
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Virtual(_, num) => {
|
|
|
|
write!(f, " - shim(#{})", num)
|
|
|
|
}
|
|
|
|
InstanceDef::FnPtrShim(_, ty) => {
|
2017-02-08 18:31:03 +01:00
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-03-08 23:19:09 +02:00
|
|
|
InstanceDef::ClosureOnceShim { .. } => {
|
|
|
|
write!(f, " - shim")
|
2017-03-08 01:41:26 +02:00
|
|
|
}
|
2017-03-14 01:08:21 +02:00
|
|
|
InstanceDef::DropGlue(_, ty) => {
|
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-08-07 16:21:08 +02:00
|
|
|
InstanceDef::CloneShim(_, ty) => {
|
2017-08-04 14:44:12 +02:00
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'tcx> Instance<'tcx> {
|
|
|
|
pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
|
|
|
|
-> Instance<'tcx> {
|
|
|
|
assert!(substs.is_normalized_for_trans() && !substs.has_escaping_regions(),
|
|
|
|
"substs of instance {:?} not normalized for trans: {:?}",
|
|
|
|
def_id, substs);
|
|
|
|
Instance { def: InstanceDef::Item(def_id), substs: substs }
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:22:08 -04:00
|
|
|
pub fn mono(tcx: TyCtxt<'a, 'tcx, 'b>, def_id: DefId) -> Instance<'tcx> {
|
2017-02-08 18:31:03 +01:00
|
|
|
Instance::new(def_id, tcx.global_tcx().empty_substs_for_def_id(def_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def.def_id()
|
|
|
|
}
|
|
|
|
}
|