]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/ty_fold.rs
Ignore tests broken by failing on ICE
[rust.git] / src / librustc / middle / ty_fold.rs
1 // Copyright 2012-2013 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 // Generalized type folding mechanism.
12
13 use middle::ty;
14 use util::ppaux::Repr;
15
16 pub trait TypeFolder {
17     fn tcx<'a>(&'a self) -> &'a ty::ctxt;
18
19     fn fold_ty(&mut self, t: ty::t) -> ty::t {
20         super_fold_ty(self, t)
21     }
22
23     fn fold_mt(&mut self, t: &ty::mt) -> ty::mt {
24         super_fold_mt(self, t)
25     }
26
27     fn fold_trait_ref(&mut self, t: &ty::TraitRef) -> ty::TraitRef {
28         super_fold_trait_ref(self, t)
29     }
30
31     fn fold_sty(&mut self, sty: &ty::sty) -> ty::sty {
32         super_fold_sty(self, sty)
33     }
34
35     fn fold_substs(&mut self,
36                    substs: &ty::substs)
37                    -> ty::substs {
38         super_fold_substs(self, substs)
39     }
40
41     fn fold_sig(&mut self,
42                 sig: &ty::FnSig)
43                 -> ty::FnSig {
44         super_fold_sig(self, sig)
45     }
46
47     fn fold_bare_fn_ty(&mut self,
48                        fty: &ty::BareFnTy)
49                        -> ty::BareFnTy {
50         ty::BareFnTy { sig: self.fold_sig(&fty.sig),
51                        abi: fty.abi,
52                        fn_style: fty.fn_style }
53     }
54
55     fn fold_closure_ty(&mut self,
56                        fty: &ty::ClosureTy)
57                        -> ty::ClosureTy {
58         ty::ClosureTy {
59             store: self.fold_trait_store(fty.store),
60             sig: self.fold_sig(&fty.sig),
61             fn_style: fty.fn_style,
62             onceness: fty.onceness,
63             bounds: fty.bounds,
64         }
65     }
66
67     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
68         r
69     }
70
71     fn fold_trait_store(&mut self, s: ty::TraitStore) -> ty::TraitStore {
72         super_fold_trait_store(self, s)
73     }
74 }
75
76 pub fn fold_opt_ty<T:TypeFolder>(this: &mut T,
77                                  t: Option<ty::t>)
78                                  -> Option<ty::t> {
79     t.map(|t| this.fold_ty(t))
80 }
81
82 pub fn fold_ty_vec<T:TypeFolder>(this: &mut T, tys: &[ty::t]) -> Vec<ty::t> {
83     tys.iter().map(|t| this.fold_ty(*t)).collect()
84 }
85
86 pub fn super_fold_ty<T:TypeFolder>(this: &mut T,
87                                    t: ty::t)
88                                    -> ty::t {
89     let sty = this.fold_sty(&ty::get(t).sty);
90     ty::mk_t(this.tcx(), sty)
91 }
92
93 pub fn super_fold_substs<T:TypeFolder>(this: &mut T,
94                                        substs: &ty::substs)
95                                        -> ty::substs {
96     let regions = match substs.regions {
97         ty::ErasedRegions => {
98             ty::ErasedRegions
99         }
100         ty::NonerasedRegions(ref regions) => {
101             ty::NonerasedRegions(regions.map(|r| this.fold_region(*r)))
102         }
103     };
104
105     ty::substs { regions: regions,
106                  self_ty: fold_opt_ty(this, substs.self_ty),
107                  tps: fold_ty_vec(this, substs.tps.as_slice()), }
108 }
109
110 pub fn super_fold_sig<T:TypeFolder>(this: &mut T,
111                                     sig: &ty::FnSig)
112                                     -> ty::FnSig {
113     ty::FnSig { binder_id: sig.binder_id,
114                 inputs: fold_ty_vec(this, sig.inputs.as_slice()),
115                 output: this.fold_ty(sig.output),
116                 variadic: sig.variadic }
117 }
118
119 pub fn super_fold_trait_ref<T:TypeFolder>(this: &mut T,
120                                           t: &ty::TraitRef)
121                                           -> ty::TraitRef {
122     ty::TraitRef {
123         def_id: t.def_id,
124         substs: this.fold_substs(&t.substs)
125     }
126 }
127
128 pub fn super_fold_mt<T:TypeFolder>(this: &mut T,
129                                    mt: &ty::mt) -> ty::mt {
130     ty::mt {ty: this.fold_ty(mt.ty),
131             mutbl: mt.mutbl}
132 }
133
134 pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
135                                     sty: &ty::sty) -> ty::sty {
136     match *sty {
137         ty::ty_box(typ) => {
138             ty::ty_box(this.fold_ty(typ))
139         }
140         ty::ty_uniq(typ) => {
141             ty::ty_uniq(this.fold_ty(typ))
142         }
143         ty::ty_ptr(ref tm) => {
144             ty::ty_ptr(this.fold_mt(tm))
145         }
146         ty::ty_vec(ref tm, sz) => {
147             ty::ty_vec(this.fold_mt(tm), sz)
148         }
149         ty::ty_enum(tid, ref substs) => {
150             ty::ty_enum(tid, this.fold_substs(substs))
151         }
152         ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, bounds }) => {
153             ty::ty_trait(box ty::TyTrait{
154                 def_id: def_id,
155                 substs: this.fold_substs(substs),
156                 store: this.fold_trait_store(store),
157                 bounds: bounds
158             })
159         }
160         ty::ty_tup(ref ts) => {
161             ty::ty_tup(fold_ty_vec(this, ts.as_slice()))
162         }
163         ty::ty_bare_fn(ref f) => {
164             ty::ty_bare_fn(this.fold_bare_fn_ty(f))
165         }
166         ty::ty_closure(ref f) => {
167             ty::ty_closure(box this.fold_closure_ty(*f))
168         }
169         ty::ty_rptr(r, ref tm) => {
170             ty::ty_rptr(this.fold_region(r),
171                         ty::mt {ty: this.fold_ty(tm.ty),
172                                 mutbl: tm.mutbl})
173         }
174         ty::ty_struct(did, ref substs) => {
175             ty::ty_struct(did,
176                           this.fold_substs(substs))
177         }
178         ty::ty_nil | ty::ty_bot | ty::ty_bool | ty::ty_char | ty::ty_str |
179         ty::ty_int(_) | ty::ty_uint(_) | ty::ty_float(_) |
180         ty::ty_err | ty::ty_infer(_) |
181         ty::ty_param(..) | ty::ty_self(_) => {
182             (*sty).clone()
183         }
184     }
185 }
186
187 pub fn super_fold_trait_store<T:TypeFolder>(this: &mut T,
188                                             trait_store: ty::TraitStore)
189                                             -> ty::TraitStore {
190     match trait_store {
191         ty::UniqTraitStore => ty::UniqTraitStore,
192         ty::RegionTraitStore(r, m) => {
193             ty::RegionTraitStore(this.fold_region(r), m)
194         }
195     }
196 }
197
198 ///////////////////////////////////////////////////////////////////////////
199 // Some sample folders
200
201 pub struct BottomUpFolder<'a> {
202     pub tcx: &'a ty::ctxt,
203     pub fldop: |ty::t|: 'a -> ty::t,
204 }
205
206 impl<'a> TypeFolder for BottomUpFolder<'a> {
207     fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
208
209     fn fold_ty(&mut self, ty: ty::t) -> ty::t {
210         let t1 = super_fold_ty(self, ty);
211         (self.fldop)(t1)
212     }
213 }
214
215 ///////////////////////////////////////////////////////////////////////////
216 // Region folder
217
218 pub struct RegionFolder<'a> {
219     tcx: &'a ty::ctxt,
220     fld_t: |ty::t|: 'a -> ty::t,
221     fld_r: |ty::Region|: 'a -> ty::Region,
222 }
223
224 impl<'a> RegionFolder<'a> {
225     pub fn general(tcx: &'a ty::ctxt,
226                    fld_r: |ty::Region|: 'a -> ty::Region,
227                    fld_t: |ty::t|: 'a -> ty::t)
228                    -> RegionFolder<'a> {
229         RegionFolder {
230             tcx: tcx,
231             fld_t: fld_t,
232             fld_r: fld_r
233         }
234     }
235
236     pub fn regions(tcx: &'a ty::ctxt, fld_r: |ty::Region|: 'a -> ty::Region)
237                    -> RegionFolder<'a> {
238         fn noop(t: ty::t) -> ty::t { t }
239
240         RegionFolder {
241             tcx: tcx,
242             fld_t: noop,
243             fld_r: fld_r
244         }
245     }
246 }
247
248 impl<'a> TypeFolder for RegionFolder<'a> {
249     fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
250
251     fn fold_ty(&mut self, ty: ty::t) -> ty::t {
252         debug!("RegionFolder.fold_ty({})", ty.repr(self.tcx()));
253         let t1 = super_fold_ty(self, ty);
254         (self.fld_t)(t1)
255     }
256
257     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
258         debug!("RegionFolder.fold_region({})", r.repr(self.tcx()));
259         (self.fld_r)(r)
260     }
261 }