]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/ty_fold.rs
c0977d3c43fef22b5853c1529bcc3436b52441a7
[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(&self) -> 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                        abis: fty.abis,
52                        purity: fty.purity }
53     }
54
55     fn fold_closure_ty(&mut self,
56                        fty: &ty::ClosureTy)
57                        -> ty::ClosureTy {
58         ty::ClosureTy {
59             region: self.fold_region(fty.region),
60             sig: self.fold_sig(&fty.sig),
61             purity: fty.purity,
62             sigil: fty.sigil,
63             onceness: fty.onceness,
64             bounds: fty.bounds,
65         }
66     }
67
68     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
69         r
70     }
71
72     fn fold_vstore(&mut self, vstore: ty::vstore) -> ty::vstore {
73         super_fold_vstore(self, vstore)
74     }
75
76     fn fold_trait_store(&mut self, s: ty::TraitStore) -> ty::TraitStore {
77         super_fold_trait_store(self, s)
78     }
79 }
80
81 pub fn fold_opt_ty<T:TypeFolder>(this: &mut T,
82                                  t: Option<ty::t>)
83                                  -> Option<ty::t> {
84     t.map(|t| this.fold_ty(t))
85 }
86
87 pub fn fold_ty_vec<T:TypeFolder>(this: &mut T,
88                                  tys: &[ty::t])
89                                  -> ~[ty::t] {
90     tys.map(|t| this.fold_ty(*t))
91 }
92
93 pub fn super_fold_ty<T:TypeFolder>(this: &mut T,
94                                    t: ty::t)
95                                    -> ty::t {
96     ty::mk_t(this.tcx(), this.fold_sty(&ty::get(t).sty))
97 }
98
99 pub fn super_fold_substs<T:TypeFolder>(this: &mut T,
100                                        substs: &ty::substs)
101                                        -> ty::substs {
102     let regions = match substs.regions {
103         ty::ErasedRegions => {
104             ty::ErasedRegions
105         }
106         ty::NonerasedRegions(ref regions) => {
107             ty::NonerasedRegions(regions.map(|r| this.fold_region(*r)))
108         }
109     };
110
111     ty::substs { regions: regions,
112                  self_ty: fold_opt_ty(this, substs.self_ty),
113                  tps: fold_ty_vec(this, substs.tps), }
114 }
115
116 pub fn super_fold_sig<T:TypeFolder>(this: &mut T,
117                                     sig: &ty::FnSig)
118                                     -> ty::FnSig {
119     ty::FnSig { binder_id: sig.binder_id,
120                 inputs: fold_ty_vec(this, sig.inputs),
121                 output: this.fold_ty(sig.output),
122                 variadic: sig.variadic }
123 }
124
125 pub fn super_fold_trait_ref<T:TypeFolder>(this: &mut T,
126                                           t: &ty::TraitRef)
127                                           -> ty::TraitRef {
128     ty::TraitRef {
129         def_id: t.def_id,
130         substs: this.fold_substs(&t.substs)
131     }
132 }
133
134 pub fn super_fold_mt<T:TypeFolder>(this: &mut T,
135                                    mt: &ty::mt) -> ty::mt {
136     ty::mt {ty: this.fold_ty(mt.ty),
137             mutbl: mt.mutbl}
138 }
139
140 pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
141                                     sty: &ty::sty) -> ty::sty {
142     match *sty {
143         ty::ty_box(typ) => {
144             ty::ty_box(this.fold_ty(typ))
145         }
146         ty::ty_uniq(typ) => {
147             ty::ty_uniq(this.fold_ty(typ))
148         }
149         ty::ty_ptr(ref tm) => {
150             ty::ty_ptr(this.fold_mt(tm))
151         }
152         ty::ty_unboxed_vec(ref tm) => {
153             ty::ty_unboxed_vec(this.fold_mt(tm))
154         }
155         ty::ty_vec(ref tm, vst) => {
156             ty::ty_vec(this.fold_mt(tm), this.fold_vstore(vst))
157         }
158         ty::ty_enum(tid, ref substs) => {
159             ty::ty_enum(tid, this.fold_substs(substs))
160         }
161         ty::ty_trait(did, ref substs, st, mutbl, bounds) => {
162             ty::ty_trait(did,
163                      this.fold_substs(substs),
164                      this.fold_trait_store(st),
165                      mutbl,
166                      bounds)
167         }
168         ty::ty_tup(ref ts) => {
169             ty::ty_tup(fold_ty_vec(this, *ts))
170         }
171         ty::ty_bare_fn(ref f) => {
172             ty::ty_bare_fn(this.fold_bare_fn_ty(f))
173         }
174         ty::ty_closure(ref f) => {
175             ty::ty_closure(this.fold_closure_ty(f))
176         }
177         ty::ty_rptr(r, ref tm) => {
178             ty::ty_rptr(this.fold_region(r),
179                         ty::mt {ty: this.fold_ty(tm.ty),
180                                 mutbl: tm.mutbl})
181         }
182         ty::ty_struct(did, ref substs) => {
183             ty::ty_struct(did,
184                           this.fold_substs(substs))
185         }
186         ty::ty_str(vst) => {
187             ty::ty_str(this.fold_vstore(vst))
188         }
189         ty::ty_nil | ty::ty_bot | ty::ty_bool | ty::ty_char |
190         ty::ty_int(_) | ty::ty_uint(_) | ty::ty_float(_) |
191         ty::ty_err | ty::ty_infer(_) |
192         ty::ty_param(..) | ty::ty_self(_) => {
193             (*sty).clone()
194         }
195     }
196 }
197
198 pub fn super_fold_vstore<T:TypeFolder>(this: &mut T,
199                                        vstore: ty::vstore)
200                                        -> ty::vstore {
201     match vstore {
202         ty::vstore_fixed(i) => ty::vstore_fixed(i),
203         ty::vstore_uniq => ty::vstore_uniq,
204         ty::vstore_slice(r) => ty::vstore_slice(this.fold_region(r)),
205     }
206 }
207
208 pub fn super_fold_trait_store<T:TypeFolder>(this: &mut T,
209                                             trait_store: ty::TraitStore)
210                                             -> ty::TraitStore {
211     match trait_store {
212         ty::UniqTraitStore      => ty::UniqTraitStore,
213         ty::RegionTraitStore(r) => ty::RegionTraitStore(this.fold_region(r)),
214     }
215 }
216
217 ///////////////////////////////////////////////////////////////////////////
218 // Some sample folders
219
220 pub struct BottomUpFolder<'a> {
221     tcx: ty::ctxt,
222     fldop: 'a |ty::t| -> ty::t,
223 }
224
225 impl<'a> TypeFolder for BottomUpFolder<'a> {
226     fn tcx(&self) -> ty::ctxt { self.tcx }
227
228     fn fold_ty(&mut self, ty: ty::t) -> ty::t {
229         let t1 = super_fold_ty(self, ty);
230         (self.fldop)(t1)
231     }
232 }
233
234 ///////////////////////////////////////////////////////////////////////////
235 // Region folder
236
237 pub struct RegionFolder<'a> {
238     tcx: ty::ctxt,
239     fld_t: 'a |ty::t| -> ty::t,
240     fld_r: 'a |ty::Region| -> ty::Region,
241 }
242
243 impl<'a> RegionFolder<'a> {
244     pub fn general(tcx: ty::ctxt,
245                    fld_r: 'a |ty::Region| -> ty::Region,
246                    fld_t: 'a |ty::t| -> ty::t)
247                    -> RegionFolder<'a> {
248         RegionFolder {
249             tcx: tcx,
250             fld_t: fld_t,
251             fld_r: fld_r
252         }
253     }
254
255     pub fn regions(tcx: ty::ctxt, fld_r: 'a |ty::Region| -> ty::Region)
256                    -> RegionFolder<'a> {
257         fn noop(t: ty::t) -> ty::t { t }
258
259         RegionFolder {
260             tcx: tcx,
261             fld_t: noop,
262             fld_r: fld_r
263         }
264     }
265 }
266
267 impl<'a> TypeFolder for RegionFolder<'a> {
268     fn tcx(&self) -> ty::ctxt { self.tcx }
269
270     fn fold_ty(&mut self, ty: ty::t) -> ty::t {
271         debug!("RegionFolder.fold_ty({})", ty.repr(self.tcx()));
272         let t1 = super_fold_ty(self, ty);
273         (self.fld_t)(t1)
274     }
275
276     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
277         debug!("RegionFolder.fold_region({})", r.repr(self.tcx()));
278         (self.fld_r)(r)
279     }
280 }