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::common::MemoizationMap;
20 use syntax::codemap::DUMMY_SP;
22 pub use rustc::ty::Instance;
24 /// For associated constants from traits, return the impl definition.
25 pub fn resolve_const<'a, 'tcx>(
26 scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>
28 if let Some(trait_id) = scx.tcx().trait_of_item(def_id) {
29 let trait_ref = ty::TraitRef::new(trait_id, substs);
30 let trait_ref = ty::Binder(trait_ref);
31 let vtable = fulfill_obligation(scx, DUMMY_SP, trait_ref);
32 if let traits::VtableImpl(vtable_impl) = vtable {
33 let name = scx.tcx().item_name(def_id);
34 let ac = scx.tcx().associated_items(vtable_impl.impl_def_id)
35 .find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
36 if let Some(ac) = ac {
37 return Instance::new(ac.def_id, vtable_impl.substs);
42 Instance::new(def_id, substs)
45 /// Monomorphizes a type from the AST by first applying the in-scope
46 /// substitutions and then normalizing any associated types.
47 pub fn apply_param_substs<'a, 'tcx, T>(scx: &SharedCrateContext<'a, 'tcx>,
48 param_substs: &Substs<'tcx>,
51 where T: TransNormalize<'tcx>
54 debug!("apply_param_substs(param_substs={:?}, value={:?})", param_substs, value);
55 let substituted = value.subst(tcx, param_substs);
56 let substituted = scx.tcx().erase_regions(&substituted);
57 AssociatedTypeNormalizer::new(scx).fold(&substituted)
61 /// Returns the normalized type of a struct field
62 pub fn field_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
63 param_substs: &Substs<'tcx>,
64 f: &'tcx ty::FieldDef)
67 tcx.normalize_associated_type(&f.ty(tcx, param_substs))
70 struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
71 shared: &'a SharedCrateContext<'b, 'gcx>,
74 impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
75 fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
76 AssociatedTypeNormalizer {
81 fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
82 if !value.has_projection_types() {
90 impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
91 fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
95 fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
96 if !ty.has_projection_types() {
99 self.shared.project_cache().memoize(ty, || {
100 debug!("AssociatedTypeNormalizer: ty={:?}", ty);
101 self.shared.tcx().normalize_associated_type(&ty)