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.
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.
11 // Generalized type folding mechanism.
14 use util::ppaux::Repr;
16 pub trait TypeFolder {
17 fn tcx(&self) -> ty::ctxt;
19 fn fold_ty(&mut self, t: ty::t) -> ty::t {
20 super_fold_ty(self, t)
23 fn fold_mt(&mut self, t: &ty::mt) -> ty::mt {
24 super_fold_mt(self, t)
27 fn fold_trait_ref(&mut self, t: &ty::TraitRef) -> ty::TraitRef {
28 super_fold_trait_ref(self, t)
31 fn fold_sty(&mut self, sty: &ty::sty) -> ty::sty {
32 super_fold_sty(self, sty)
35 fn fold_substs(&mut self,
38 super_fold_substs(self, substs)
41 fn fold_sig(&mut self,
44 super_fold_sig(self, sig)
47 fn fold_bare_fn_ty(&mut self,
50 ty::BareFnTy { sig: self.fold_sig(&fty.sig),
55 fn fold_closure_ty(&mut self,
59 region: self.fold_region(fty.region),
60 sig: self.fold_sig(&fty.sig),
63 onceness: fty.onceness,
68 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
72 fn fold_vstore(&mut self, vstore: ty::vstore) -> ty::vstore {
73 super_fold_vstore(self, vstore)
76 fn fold_trait_store(&mut self, s: ty::TraitStore) -> ty::TraitStore {
77 super_fold_trait_store(self, s)
81 pub fn fold_opt_ty<T:TypeFolder>(this: &mut T,
84 t.map(|t| this.fold_ty(t))
87 pub fn fold_ty_vec<T:TypeFolder>(this: &mut T,
90 tys.map(|t| this.fold_ty(*t))
93 pub fn super_fold_ty<T:TypeFolder>(this: &mut T,
96 ty::mk_t(this.tcx(), this.fold_sty(&ty::get(t).sty))
99 pub fn super_fold_substs<T:TypeFolder>(this: &mut T,
102 let regions = match substs.regions {
103 ty::ErasedRegions => {
106 ty::NonerasedRegions(ref regions) => {
107 ty::NonerasedRegions(regions.map(|r| this.fold_region(*r)))
111 ty::substs { regions: regions,
112 self_ty: fold_opt_ty(this, substs.self_ty),
113 tps: fold_ty_vec(this, substs.tps), }
116 pub fn super_fold_sig<T:TypeFolder>(this: &mut T,
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 }
125 pub fn super_fold_trait_ref<T:TypeFolder>(this: &mut T,
130 substs: this.fold_substs(&t.substs)
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),
140 pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
141 sty: &ty::sty) -> ty::sty {
144 ty::ty_box(this.fold_ty(typ))
146 ty::ty_uniq(typ) => {
147 ty::ty_uniq(this.fold_ty(typ))
149 ty::ty_ptr(ref tm) => {
150 ty::ty_ptr(this.fold_mt(tm))
152 ty::ty_unboxed_vec(ref tm) => {
153 ty::ty_unboxed_vec(this.fold_mt(tm))
155 ty::ty_vec(ref tm, vst) => {
156 ty::ty_vec(this.fold_mt(tm), this.fold_vstore(vst))
158 ty::ty_enum(tid, ref substs) => {
159 ty::ty_enum(tid, this.fold_substs(substs))
161 ty::ty_trait(did, ref substs, st, mutbl, bounds) => {
163 this.fold_substs(substs),
164 this.fold_trait_store(st),
168 ty::ty_tup(ref ts) => {
169 ty::ty_tup(fold_ty_vec(this, *ts))
171 ty::ty_bare_fn(ref f) => {
172 ty::ty_bare_fn(this.fold_bare_fn_ty(f))
174 ty::ty_closure(ref f) => {
175 ty::ty_closure(this.fold_closure_ty(f))
177 ty::ty_rptr(r, ref tm) => {
178 ty::ty_rptr(this.fold_region(r),
179 ty::mt {ty: this.fold_ty(tm.ty),
182 ty::ty_struct(did, ref substs) => {
184 this.fold_substs(substs))
187 ty::ty_str(this.fold_vstore(vst))
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(_) => {
198 pub fn super_fold_vstore<T:TypeFolder>(this: &mut T,
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)),
208 pub fn super_fold_trait_store<T:TypeFolder>(this: &mut T,
209 trait_store: ty::TraitStore)
212 ty::UniqTraitStore => ty::UniqTraitStore,
213 ty::RegionTraitStore(r) => ty::RegionTraitStore(this.fold_region(r)),
217 ///////////////////////////////////////////////////////////////////////////
218 // Some sample folders
220 pub struct BottomUpFolder<'a> {
222 fldop: 'a |ty::t| -> ty::t,
225 impl<'a> TypeFolder for BottomUpFolder<'a> {
226 fn tcx(&self) -> ty::ctxt { self.tcx }
228 fn fold_ty(&mut self, ty: ty::t) -> ty::t {
229 let t1 = super_fold_ty(self, ty);
234 ///////////////////////////////////////////////////////////////////////////
237 pub struct RegionFolder<'a> {
239 fld_t: 'a |ty::t| -> ty::t,
240 fld_r: 'a |ty::Region| -> ty::Region,
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> {
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 }
267 impl<'a> TypeFolder for RegionFolder<'a> {
268 fn tcx(&self) -> ty::ctxt { self.tcx }
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);
276 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
277 debug!("RegionFolder.fold_region({})", r.repr(self.tcx()));