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<'a>(&'a self) -> &'a 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),
52 fn_style: fty.fn_style }
55 fn fold_closure_ty(&mut self,
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,
67 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
71 fn fold_trait_store(&mut self, s: ty::TraitStore) -> ty::TraitStore {
72 super_fold_trait_store(self, s)
76 pub fn fold_opt_ty<T:TypeFolder>(this: &mut T,
79 t.map(|t| this.fold_ty(t))
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()
86 pub fn super_fold_ty<T:TypeFolder>(this: &mut T,
89 let sty = this.fold_sty(&ty::get(t).sty);
90 ty::mk_t(this.tcx(), sty)
93 pub fn super_fold_substs<T:TypeFolder>(this: &mut T,
96 let regions = match substs.regions {
97 ty::ErasedRegions => {
100 ty::NonerasedRegions(ref regions) => {
101 ty::NonerasedRegions(regions.map(|r| this.fold_region(*r)))
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()), }
110 pub fn super_fold_sig<T:TypeFolder>(this: &mut T,
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 }
119 pub fn super_fold_trait_ref<T:TypeFolder>(this: &mut T,
124 substs: this.fold_substs(&t.substs)
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),
134 pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
135 sty: &ty::sty) -> ty::sty {
138 ty::ty_box(this.fold_ty(typ))
140 ty::ty_uniq(typ) => {
141 ty::ty_uniq(this.fold_ty(typ))
143 ty::ty_ptr(ref tm) => {
144 ty::ty_ptr(this.fold_mt(tm))
146 ty::ty_vec(ref tm, sz) => {
147 ty::ty_vec(this.fold_mt(tm), sz)
149 ty::ty_enum(tid, ref substs) => {
150 ty::ty_enum(tid, this.fold_substs(substs))
152 ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, bounds }) => {
153 ty::ty_trait(box ty::TyTrait{
155 substs: this.fold_substs(substs),
156 store: this.fold_trait_store(store),
160 ty::ty_tup(ref ts) => {
161 ty::ty_tup(fold_ty_vec(this, ts.as_slice()))
163 ty::ty_bare_fn(ref f) => {
164 ty::ty_bare_fn(this.fold_bare_fn_ty(f))
166 ty::ty_closure(ref f) => {
167 ty::ty_closure(box this.fold_closure_ty(*f))
169 ty::ty_rptr(r, ref tm) => {
170 ty::ty_rptr(this.fold_region(r),
171 ty::mt {ty: this.fold_ty(tm.ty),
174 ty::ty_struct(did, ref substs) => {
176 this.fold_substs(substs))
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(_) => {
187 pub fn super_fold_trait_store<T:TypeFolder>(this: &mut T,
188 trait_store: ty::TraitStore)
191 ty::UniqTraitStore => ty::UniqTraitStore,
192 ty::RegionTraitStore(r, m) => {
193 ty::RegionTraitStore(this.fold_region(r), m)
198 ///////////////////////////////////////////////////////////////////////////
199 // Some sample folders
201 pub struct BottomUpFolder<'a> {
202 pub tcx: &'a ty::ctxt,
203 pub fldop: |ty::t|: 'a -> ty::t,
206 impl<'a> TypeFolder for BottomUpFolder<'a> {
207 fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
209 fn fold_ty(&mut self, ty: ty::t) -> ty::t {
210 let t1 = super_fold_ty(self, ty);
215 ///////////////////////////////////////////////////////////////////////////
218 pub struct RegionFolder<'a> {
220 fld_t: |ty::t|: 'a -> ty::t,
221 fld_r: |ty::Region|: 'a -> ty::Region,
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> {
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 }
248 impl<'a> TypeFolder for RegionFolder<'a> {
249 fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
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);
257 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
258 debug!("RegionFolder.fold_region({})", r.repr(self.tcx()));