]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/monomorphize.rs
translate tuple-variant constructors using MIR
[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::ppaux;
19 use rustc::util::common::MemoizationMap;
20
21 use syntax::codemap::DUMMY_SP;
22
23 use std::fmt;
24
25 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
26 pub struct Instance<'tcx> {
27     pub def: DefId,
28     pub substs: &'tcx Substs<'tcx>,
29 }
30
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, &[])
34     }
35 }
36
37 impl<'a, 'tcx> Instance<'tcx> {
38     pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
39                -> Instance<'tcx> {
40         assert!(substs.regions().all(|&r| r == ty::ReErased));
41         Instance { def: def_id, substs: substs }
42     }
43
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))
46     }
47
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);
60                 }
61             }
62         }
63
64         *self
65     }
66 }
67
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>,
72                                        value: &T)
73                                        -> T
74     where T: TransNormalize<'tcx>
75 {
76     let tcx = scx.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)
81 }
82
83
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)
88                           -> Ty<'tcx>
89 {
90     tcx.normalize_associated_type(&f.ty(tcx, param_substs))
91 }
92
93 struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
94     shared: &'a SharedCrateContext<'b, 'gcx>,
95 }
96
97 impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
98     fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
99         AssociatedTypeNormalizer {
100             shared: shared,
101         }
102     }
103
104     fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
105         if !value.has_projection_types() {
106             value.clone()
107         } else {
108             value.fold_with(self)
109         }
110     }
111 }
112
113 impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
114     fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
115         self.shared.tcx()
116     }
117
118     fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
119         if !ty.has_projection_types() {
120             ty
121         } else {
122             self.shared.project_cache().memoize(ty, || {
123                 debug!("AssociatedTypeNormalizer: ty={:?}", ty);
124                 self.shared.tcx().normalize_associated_type(&ty)
125             })
126         }
127     }
128 }