]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/monomorphize.rs
move Instance to rustc and use it in the collector
[rust.git] / src / librustc_trans / monomorphize.rs
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 common::*;
12 use rustc::hir::def_id::DefId;
13 use rustc::infer::TransNormalize;
14 use rustc::traits;
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;
19
20 use syntax::codemap::DUMMY_SP;
21
22 pub use rustc::ty::Instance;
23
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>
27 ) -> Instance<'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);
38             }
39         }
40     }
41
42     Instance::new(def_id, substs)
43 }
44
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>,
49                                        value: &T)
50                                        -> T
51     where T: TransNormalize<'tcx>
52 {
53     let tcx = scx.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)
58 }
59
60
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)
65                           -> Ty<'tcx>
66 {
67     tcx.normalize_associated_type(&f.ty(tcx, param_substs))
68 }
69
70 struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
71     shared: &'a SharedCrateContext<'b, 'gcx>,
72 }
73
74 impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
75     fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
76         AssociatedTypeNormalizer {
77             shared: shared,
78         }
79     }
80
81     fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
82         if !value.has_projection_types() {
83             value.clone()
84         } else {
85             value.fold_with(self)
86         }
87     }
88 }
89
90 impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
91     fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
92         self.shared.tcx()
93     }
94
95     fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
96         if !ty.has_projection_types() {
97             ty
98         } else {
99             self.shared.project_cache().memoize(ty, || {
100                 debug!("AssociatedTypeNormalizer: ty={:?}", ty);
101                 self.shared.tcx().normalize_associated_type(&ty)
102             })
103         }
104     }
105 }