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.
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.
12 use rustc::hir::def_id::DefId;
13 use rustc::infer::TransNormalize;
15 use rustc::ty::fold::{TypeFolder, TypeFoldable};
16 use rustc::ty::subst::{Subst, Substs};
17 use rustc::ty::{self, Ty, TyCtxt};
18 use rustc::util::ppaux;
19 use rustc::util::common::MemoizationMap;
21 use syntax::codemap::DUMMY_SP;
25 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
26 pub struct Instance<'tcx> {
28 pub substs: &'tcx Substs<'tcx>,
31 impl<'tcx> fmt::Display for Instance<'tcx> {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 ppaux::parameterized(f, &self.substs, self.def, &[])
37 impl<'a, 'tcx> Instance<'tcx> {
38 pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
40 assert!(substs.regions().all(|&r| r == ty::ReErased));
41 Instance { def: def_id, substs: substs }
44 pub fn mono(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {
45 Instance::new(def_id, scx.empty_substs_for_def_id(def_id))
48 /// For associated constants from traits, return the impl definition.
49 pub fn resolve_const(&self, scx: &SharedCrateContext<'a, 'tcx>) -> Self {
50 if let Some(trait_id) = scx.tcx().trait_of_item(self.def) {
51 let trait_ref = ty::TraitRef::new(trait_id, self.substs);
52 let trait_ref = ty::Binder(trait_ref);
53 let vtable = fulfill_obligation(scx, DUMMY_SP, trait_ref);
54 if let traits::VtableImpl(vtable_impl) = vtable {
55 let name = scx.tcx().item_name(self.def);
56 let ac = scx.tcx().associated_items(vtable_impl.impl_def_id)
57 .find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
58 if let Some(ac) = ac {
59 return Instance::new(ac.def_id, vtable_impl.substs);
68 /// Monomorphizes a type from the AST by first applying the in-scope
69 /// substitutions and then normalizing any associated types.
70 pub fn apply_param_substs<'a, 'tcx, T>(scx: &SharedCrateContext<'a, 'tcx>,
71 param_substs: &Substs<'tcx>,
74 where T: TransNormalize<'tcx>
77 debug!("apply_param_substs(param_substs={:?}, value={:?})", param_substs, value);
78 let substituted = value.subst(tcx, param_substs);
79 let substituted = scx.tcx().erase_regions(&substituted);
80 AssociatedTypeNormalizer::new(scx).fold(&substituted)
84 /// Returns the normalized type of a struct field
85 pub fn field_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
86 param_substs: &Substs<'tcx>,
87 f: &'tcx ty::FieldDef)
90 tcx.normalize_associated_type(&f.ty(tcx, param_substs))
93 struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
94 shared: &'a SharedCrateContext<'b, 'gcx>,
97 impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
98 fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
99 AssociatedTypeNormalizer {
104 fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
105 if !value.has_projection_types() {
108 value.fold_with(self)
113 impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
114 fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
118 fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
119 if !ty.has_projection_types() {
122 self.shared.project_cache().memoize(ty, || {
123 debug!("AssociatedTypeNormalizer: ty={:?}", ty);
124 self.shared.tcx().normalize_associated_type(&ty)