]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/ty/fold.rs
c6d213583af20173cd367bee82715a715bad4a82
[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. The setup is a bit convoluted
12 //! but allows for convenient usage. Let T be an instance of some
13 //! "foldable type" (one which implements `TypeFoldable`) and F be an
14 //! instance of a "folder" (a type which implements `TypeFolder`). Then
15 //! the setup is intended to be:
16 //!
17 //!     T.fold_with(F) --calls--> F.fold_T(T) --calls--> super_fold_T(F, T)
18 //!
19 //! This way, when you define a new folder F, you can override
20 //! `fold_T()` to customize the behavior, and invoke `super_fold_T()`
21 //! to get the original behavior. Meanwhile, to actually fold
22 //! something, you can just write `T.fold_with(F)`, which is
23 //! convenient. (Note that `fold_with` will also transparently handle
24 //! things like a `Vec<T>` where T is foldable and so on.)
25 //!
26 //! In this ideal setup, the only function that actually *does*
27 //! anything is `super_fold_T`, which traverses the type `T`. Moreover,
28 //! `super_fold_T` should only ever call `T.fold_with()`.
29 //!
30 //! In some cases, we follow a degenerate pattern where we do not have
31 //! a `fold_T` nor `super_fold_T` method. Instead, `T.fold_with`
32 //! traverses the structure directly. This is suboptimal because the
33 //! behavior cannot be overridden, but it's much less work to implement.
34 //! If you ever *do* need an override that doesn't exist, it's not hard
35 //! to convert the degenerate pattern into the proper thing.
36
37 use middle::subst;
38 use middle::subst::VecPerParamSpace;
39 use middle::ty::{self, Ty, HasTypeFlags, RegionEscape};
40 use middle::traits;
41
42 use std::fmt;
43 use std::rc::Rc;
44 use syntax::abi;
45 use rustc_front::hir;
46 use syntax::owned_slice::OwnedSlice;
47 use util::nodemap::{FnvHashMap, FnvHashSet};
48
49 ///////////////////////////////////////////////////////////////////////////
50 // Two generic traits
51
52 /// The TypeFoldable trait is implemented for every type that can be folded.
53 /// Basically, every type that has a corresponding method in TypeFolder.
54 pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
55     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self;
56 }
57
58 /// The TypeFolder trait defines the actual *folding*. There is a
59 /// method defined for every foldable type. Each of these has a
60 /// default implementation that does an "identity" fold. Within each
61 /// identity fold, it should invoke `foo.fold_with(self)` to fold each
62 /// sub-item.
63 pub trait TypeFolder<'tcx> : Sized {
64     fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx>;
65
66     /// Invoked by the `super_*` routines when we enter a region
67     /// binding level (for example, when entering a function
68     /// signature). This is used by clients that want to track the
69     /// Debruijn index nesting level.
70     fn enter_region_binder(&mut self) { }
71
72     /// Invoked by the `super_*` routines when we exit a region
73     /// binding level. This is used by clients that want to
74     /// track the Debruijn index nesting level.
75     fn exit_region_binder(&mut self) { }
76
77     fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
78         where T : TypeFoldable<'tcx>
79     {
80         // FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
81         super_fold_binder(self, t)
82     }
83
84     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
85         super_fold_ty(self, t)
86     }
87
88     fn fold_mt(&mut self, t: &ty::TypeAndMut<'tcx>) -> ty::TypeAndMut<'tcx> {
89         super_fold_mt(self, t)
90     }
91
92     fn fold_trait_ref(&mut self, t: &ty::TraitRef<'tcx>) -> ty::TraitRef<'tcx> {
93         super_fold_trait_ref(self, t)
94     }
95
96     fn fold_substs(&mut self,
97                    substs: &subst::Substs<'tcx>)
98                    -> subst::Substs<'tcx> {
99         super_fold_substs(self, substs)
100     }
101
102     fn fold_fn_sig(&mut self,
103                    sig: &ty::FnSig<'tcx>)
104                    -> ty::FnSig<'tcx> {
105         super_fold_fn_sig(self, sig)
106     }
107
108     fn fold_output(&mut self,
109                       output: &ty::FnOutput<'tcx>)
110                       -> ty::FnOutput<'tcx> {
111         super_fold_output(self, output)
112     }
113
114     fn fold_bare_fn_ty(&mut self,
115                        fty: &ty::BareFnTy<'tcx>)
116                        -> ty::BareFnTy<'tcx>
117     {
118         super_fold_bare_fn_ty(self, fty)
119     }
120
121     fn fold_closure_ty(&mut self,
122                        fty: &ty::ClosureTy<'tcx>)
123                        -> ty::ClosureTy<'tcx> {
124         super_fold_closure_ty(self, fty)
125     }
126
127     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
128         r
129     }
130
131     fn fold_existential_bounds(&mut self, s: &ty::ExistentialBounds<'tcx>)
132                                -> ty::ExistentialBounds<'tcx> {
133         super_fold_existential_bounds(self, s)
134     }
135
136     fn fold_autoref(&mut self, ar: &ty::AutoRef<'tcx>) -> ty::AutoRef<'tcx> {
137         super_fold_autoref(self, ar)
138     }
139
140     fn fold_item_substs(&mut self, i: ty::ItemSubsts<'tcx>) -> ty::ItemSubsts<'tcx> {
141         super_fold_item_substs(self, i)
142     }
143 }
144
145 ///////////////////////////////////////////////////////////////////////////
146 // TypeFoldable implementations.
147 //
148 // Ideally, each type should invoke `folder.fold_foo(self)` and
149 // nothing else. In some cases, though, we haven't gotten around to
150 // adding methods on the `folder` yet, and thus the folding is
151 // hard-coded here. This is less-flexible, because folders cannot
152 // override the behavior, but there are a lot of random types and one
153 // can easily refactor the folding into the TypeFolder trait as
154 // needed.
155
156 macro_rules! CopyImpls {
157     ($($ty:ty),+) => {
158         $(
159             impl<'tcx> TypeFoldable<'tcx> for $ty {
160                 fn fold_with<F:TypeFolder<'tcx>>(&self, _: &mut F) -> $ty {
161                     *self
162                 }
163             }
164         )+
165     }
166 }
167
168 CopyImpls! { (), hir::Unsafety, abi::Abi }
169
170 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
171     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
172         (self.0.fold_with(folder), self.1.fold_with(folder))
173     }
174 }
175
176 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Option<T> {
177     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Option<T> {
178         self.as_ref().map(|t| t.fold_with(folder))
179     }
180 }
181
182 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
183     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Rc<T> {
184         Rc::new((**self).fold_with(folder))
185     }
186 }
187
188 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
189     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Box<T> {
190         let content: T = (**self).fold_with(folder);
191         box content
192     }
193 }
194
195 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
196     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Vec<T> {
197         self.iter().map(|t| t.fold_with(folder)).collect()
198     }
199 }
200
201 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
202     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Binder<T> {
203         folder.fold_binder(self)
204     }
205 }
206
207 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for OwnedSlice<T> {
208     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> OwnedSlice<T> {
209         self.iter().map(|t| t.fold_with(folder)).collect()
210     }
211 }
212
213 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
214     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> VecPerParamSpace<T> {
215
216         // Things in the Fn space take place under an additional level
217         // of region binding relative to the other spaces. This is
218         // because those entries are attached to a method, and methods
219         // always introduce a level of region binding.
220
221         let result = self.map_enumerated(|(space, index, elem)| {
222             if space == subst::FnSpace && index == 0 {
223                 // enter new level when/if we reach the first thing in fn space
224                 folder.enter_region_binder();
225             }
226             elem.fold_with(folder)
227         });
228         if result.len(subst::FnSpace) > 0 {
229             // if there was anything in fn space, exit the region binding level
230             folder.exit_region_binder();
231         }
232         result
233     }
234 }
235
236 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
237     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Ty<'tcx> {
238         folder.fold_ty(*self)
239     }
240 }
241
242 impl<'tcx> TypeFoldable<'tcx> for ty::BareFnTy<'tcx> {
243     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::BareFnTy<'tcx> {
244         folder.fold_bare_fn_ty(self)
245     }
246 }
247
248 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureTy<'tcx> {
249     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureTy<'tcx> {
250         folder.fold_closure_ty(self)
251     }
252 }
253
254 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
255     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TypeAndMut<'tcx> {
256         folder.fold_mt(self)
257     }
258 }
259
260 impl<'tcx> TypeFoldable<'tcx> for ty::FnOutput<'tcx> {
261     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::FnOutput<'tcx> {
262         folder.fold_output(self)
263     }
264 }
265
266 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
267     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::FnSig<'tcx> {
268         folder.fold_fn_sig(self)
269     }
270 }
271
272 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
273     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TraitRef<'tcx> {
274         folder.fold_trait_ref(self)
275     }
276 }
277
278 impl<'tcx> TypeFoldable<'tcx> for ty::Region {
279     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Region {
280         folder.fold_region(*self)
281     }
282 }
283
284 impl<'tcx> TypeFoldable<'tcx> for subst::Substs<'tcx> {
285     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> subst::Substs<'tcx> {
286         folder.fold_substs(self)
287     }
288 }
289
290 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
291     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureSubsts<'tcx> {
292         let func_substs = self.func_substs.fold_with(folder);
293         ty::ClosureSubsts {
294             func_substs: folder.tcx().mk_substs(func_substs),
295             upvar_tys: self.upvar_tys.fold_with(folder),
296         }
297     }
298 }
299
300 impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
301     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ItemSubsts<'tcx> {
302         ty::ItemSubsts {
303             substs: self.substs.fold_with(folder),
304         }
305     }
306 }
307
308 impl<'tcx> TypeFoldable<'tcx> for ty::AutoRef<'tcx> {
309     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::AutoRef<'tcx> {
310         folder.fold_autoref(self)
311     }
312 }
313
314 impl<'tcx> TypeFoldable<'tcx> for ty::BuiltinBounds {
315     fn fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> ty::BuiltinBounds {
316         *self
317     }
318 }
319
320 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialBounds<'tcx> {
321     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ExistentialBounds<'tcx> {
322         folder.fold_existential_bounds(self)
323     }
324 }
325
326 impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
327     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TypeParameterDef<'tcx> {
328         ty::TypeParameterDef {
329             name: self.name,
330             def_id: self.def_id,
331             space: self.space,
332             index: self.index,
333             default: self.default.fold_with(folder),
334             default_def_id: self.default_def_id,
335             object_lifetime_default: self.object_lifetime_default.fold_with(folder),
336         }
337     }
338 }
339
340 impl<'tcx> TypeFoldable<'tcx> for ty::ObjectLifetimeDefault {
341     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ObjectLifetimeDefault {
342         match *self {
343             ty::ObjectLifetimeDefault::Ambiguous =>
344                 ty::ObjectLifetimeDefault::Ambiguous,
345
346             ty::ObjectLifetimeDefault::BaseDefault =>
347                 ty::ObjectLifetimeDefault::BaseDefault,
348
349             ty::ObjectLifetimeDefault::Specific(r) =>
350                 ty::ObjectLifetimeDefault::Specific(r.fold_with(folder)),
351         }
352     }
353 }
354
355 impl<'tcx> TypeFoldable<'tcx> for ty::RegionParameterDef {
356     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::RegionParameterDef {
357         ty::RegionParameterDef {
358             name: self.name,
359             def_id: self.def_id,
360             space: self.space,
361             index: self.index,
362             bounds: self.bounds.fold_with(folder)
363         }
364     }
365 }
366
367 impl<'tcx> TypeFoldable<'tcx> for ty::Generics<'tcx> {
368     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Generics<'tcx> {
369         ty::Generics {
370             types: self.types.fold_with(folder),
371             regions: self.regions.fold_with(folder),
372         }
373     }
374 }
375
376 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
377     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::GenericPredicates<'tcx> {
378         ty::GenericPredicates {
379             predicates: self.predicates.fold_with(folder),
380         }
381     }
382 }
383
384 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
385     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Predicate<'tcx> {
386         match *self {
387             ty::Predicate::Trait(ref a) =>
388                 ty::Predicate::Trait(a.fold_with(folder)),
389             ty::Predicate::Equate(ref binder) =>
390                 ty::Predicate::Equate(binder.fold_with(folder)),
391             ty::Predicate::RegionOutlives(ref binder) =>
392                 ty::Predicate::RegionOutlives(binder.fold_with(folder)),
393             ty::Predicate::TypeOutlives(ref binder) =>
394                 ty::Predicate::TypeOutlives(binder.fold_with(folder)),
395             ty::Predicate::Projection(ref binder) =>
396                 ty::Predicate::Projection(binder.fold_with(folder)),
397             ty::Predicate::WellFormed(data) =>
398                 ty::Predicate::WellFormed(data.fold_with(folder)),
399             ty::Predicate::ObjectSafe(trait_def_id) =>
400                 ty::Predicate::ObjectSafe(trait_def_id),
401         }
402     }
403 }
404
405 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
406     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ProjectionPredicate<'tcx> {
407         ty::ProjectionPredicate {
408             projection_ty: self.projection_ty.fold_with(folder),
409             ty: self.ty.fold_with(folder),
410         }
411     }
412 }
413
414 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
415     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ProjectionTy<'tcx> {
416         ty::ProjectionTy {
417             trait_ref: self.trait_ref.fold_with(folder),
418             item_name: self.item_name,
419         }
420     }
421 }
422
423 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
424     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::InstantiatedPredicates<'tcx> {
425         ty::InstantiatedPredicates {
426             predicates: self.predicates.fold_with(folder),
427         }
428     }
429 }
430
431 impl<'tcx,O> TypeFoldable<'tcx> for traits::Obligation<'tcx,O>
432     where O : TypeFoldable<'tcx>
433 {
434     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::Obligation<'tcx, O> {
435         traits::Obligation {
436             cause: self.cause.clone(),
437             recursion_depth: self.recursion_depth,
438             predicate: self.predicate.fold_with(folder),
439         }
440     }
441 }
442
443 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableImplData<'tcx, N> {
444     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableImplData<'tcx, N> {
445         traits::VtableImplData {
446             impl_def_id: self.impl_def_id,
447             substs: self.substs.fold_with(folder),
448             nested: self.nested.fold_with(folder),
449         }
450     }
451 }
452
453 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableClosureData<'tcx, N> {
454     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableClosureData<'tcx, N> {
455         traits::VtableClosureData {
456             closure_def_id: self.closure_def_id,
457             substs: self.substs.fold_with(folder),
458             nested: self.nested.fold_with(folder),
459         }
460     }
461 }
462
463 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableDefaultImplData<N> {
464     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableDefaultImplData<N> {
465         traits::VtableDefaultImplData {
466             trait_def_id: self.trait_def_id,
467             nested: self.nested.fold_with(folder),
468         }
469     }
470 }
471
472 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableBuiltinData<N> {
473     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableBuiltinData<N> {
474         traits::VtableBuiltinData {
475             nested: self.nested.fold_with(folder),
476         }
477     }
478 }
479
480 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Vtable<'tcx, N> {
481     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::Vtable<'tcx, N> {
482         match *self {
483             traits::VtableImpl(ref v) => traits::VtableImpl(v.fold_with(folder)),
484             traits::VtableDefaultImpl(ref t) => traits::VtableDefaultImpl(t.fold_with(folder)),
485             traits::VtableClosure(ref d) => {
486                 traits::VtableClosure(d.fold_with(folder))
487             }
488             traits::VtableFnPointer(ref d) => {
489                 traits::VtableFnPointer(d.fold_with(folder))
490             }
491             traits::VtableParam(ref n) => traits::VtableParam(n.fold_with(folder)),
492             traits::VtableBuiltin(ref d) => traits::VtableBuiltin(d.fold_with(folder)),
493             traits::VtableObject(ref d) => traits::VtableObject(d.fold_with(folder)),
494         }
495     }
496 }
497
498 impl<'tcx> TypeFoldable<'tcx> for traits::VtableObjectData<'tcx> {
499     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableObjectData<'tcx> {
500         traits::VtableObjectData {
501             upcast_trait_ref: self.upcast_trait_ref.fold_with(folder),
502             vtable_base: self.vtable_base
503         }
504     }
505 }
506
507 impl<'tcx> TypeFoldable<'tcx> for ty::EquatePredicate<'tcx> {
508     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::EquatePredicate<'tcx> {
509         ty::EquatePredicate(self.0.fold_with(folder),
510                             self.1.fold_with(folder))
511     }
512 }
513
514 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
515     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TraitPredicate<'tcx> {
516         ty::TraitPredicate {
517             trait_ref: self.trait_ref.fold_with(folder)
518         }
519     }
520 }
521
522 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
523     where T : TypeFoldable<'tcx>,
524           U : TypeFoldable<'tcx>,
525 {
526     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::OutlivesPredicate<T,U> {
527         ty::OutlivesPredicate(self.0.fold_with(folder),
528                               self.1.fold_with(folder))
529     }
530 }
531
532 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
533     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureUpvar<'tcx> {
534         ty::ClosureUpvar {
535             def: self.def,
536             span: self.span,
537             ty: self.ty.fold_with(folder),
538         }
539     }
540 }
541
542 impl<'a, 'tcx> TypeFoldable<'tcx> for ty::ParameterEnvironment<'a, 'tcx> where 'tcx: 'a {
543     fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ParameterEnvironment<'a, 'tcx> {
544         ty::ParameterEnvironment {
545             tcx: self.tcx,
546             free_substs: self.free_substs.fold_with(folder),
547             implicit_region_bound: self.implicit_region_bound.fold_with(folder),
548             caller_bounds: self.caller_bounds.fold_with(folder),
549             selection_cache: traits::SelectionCache::new(),
550             free_id: self.free_id,
551         }
552     }
553 }
554
555 ///////////////////////////////////////////////////////////////////////////
556 // "super" routines: these are the default implementations for TypeFolder.
557 //
558 // They should invoke `foo.fold_with()` to do recursive folding.
559
560 pub fn super_fold_binder<'tcx, T, U>(this: &mut T,
561                                      binder: &ty::Binder<U>)
562                                      -> ty::Binder<U>
563     where T : TypeFolder<'tcx>, U : TypeFoldable<'tcx>
564 {
565     this.enter_region_binder();
566     let result = ty::Binder(binder.0.fold_with(this));
567     this.exit_region_binder();
568     result
569 }
570
571 pub fn super_fold_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
572                                                 ty: Ty<'tcx>)
573                                                 -> Ty<'tcx> {
574     let sty = match ty.sty {
575         ty::TyBox(typ) => {
576             ty::TyBox(typ.fold_with(this))
577         }
578         ty::TyRawPtr(ref tm) => {
579             ty::TyRawPtr(tm.fold_with(this))
580         }
581         ty::TyArray(typ, sz) => {
582             ty::TyArray(typ.fold_with(this), sz)
583         }
584         ty::TySlice(typ) => {
585             ty::TySlice(typ.fold_with(this))
586         }
587         ty::TyEnum(tid, ref substs) => {
588             let substs = substs.fold_with(this);
589             ty::TyEnum(tid, this.tcx().mk_substs(substs))
590         }
591         ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
592             ty::TyTrait(box ty::TraitTy {
593                 principal: principal.fold_with(this),
594                 bounds: bounds.fold_with(this),
595             })
596         }
597         ty::TyTuple(ref ts) => {
598             ty::TyTuple(ts.fold_with(this))
599         }
600         ty::TyBareFn(opt_def_id, ref f) => {
601             let bfn = f.fold_with(this);
602             ty::TyBareFn(opt_def_id, this.tcx().mk_bare_fn(bfn))
603         }
604         ty::TyRef(r, ref tm) => {
605             let r = r.fold_with(this);
606             ty::TyRef(this.tcx().mk_region(r), tm.fold_with(this))
607         }
608         ty::TyStruct(did, ref substs) => {
609             let substs = substs.fold_with(this);
610             ty::TyStruct(did, this.tcx().mk_substs(substs))
611         }
612         ty::TyClosure(did, ref substs) => {
613             let s = substs.fold_with(this);
614             ty::TyClosure(did, s)
615         }
616         ty::TyProjection(ref data) => {
617             ty::TyProjection(data.fold_with(this))
618         }
619         ty::TyBool | ty::TyChar | ty::TyStr |
620         ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
621         ty::TyError | ty::TyInfer(_) |
622         ty::TyParam(..) => {
623             ty.sty.clone()
624         }
625     };
626     this.tcx().mk_ty(sty)
627 }
628
629 pub fn super_fold_substs<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
630                                                     substs: &subst::Substs<'tcx>)
631                                                     -> subst::Substs<'tcx> {
632     let regions = match substs.regions {
633         subst::ErasedRegions => {
634             subst::ErasedRegions
635         }
636         subst::NonerasedRegions(ref regions) => {
637             subst::NonerasedRegions(regions.fold_with(this))
638         }
639     };
640
641     subst::Substs { regions: regions,
642                     types: substs.types.fold_with(this) }
643 }
644
645 pub fn super_fold_fn_sig<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
646                                                     sig: &ty::FnSig<'tcx>)
647                                                     -> ty::FnSig<'tcx>
648 {
649     ty::FnSig { inputs: sig.inputs.fold_with(this),
650                 output: sig.output.fold_with(this),
651                 variadic: sig.variadic }
652 }
653
654 pub fn super_fold_output<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
655                                                     output: &ty::FnOutput<'tcx>)
656                                                     -> ty::FnOutput<'tcx> {
657     match *output {
658         ty::FnConverging(ref ty) => ty::FnConverging(ty.fold_with(this)),
659         ty::FnDiverging => ty::FnDiverging
660     }
661 }
662
663 pub fn super_fold_bare_fn_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
664                                                         fty: &ty::BareFnTy<'tcx>)
665                                                         -> ty::BareFnTy<'tcx>
666 {
667     ty::BareFnTy { sig: fty.sig.fold_with(this),
668                    abi: fty.abi,
669                    unsafety: fty.unsafety }
670 }
671
672 pub fn super_fold_closure_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
673                                                         fty: &ty::ClosureTy<'tcx>)
674                                                         -> ty::ClosureTy<'tcx>
675 {
676     ty::ClosureTy {
677         sig: fty.sig.fold_with(this),
678         unsafety: fty.unsafety,
679         abi: fty.abi,
680     }
681 }
682
683 pub fn super_fold_trait_ref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
684                                                        t: &ty::TraitRef<'tcx>)
685                                                        -> ty::TraitRef<'tcx>
686 {
687     let substs = t.substs.fold_with(this);
688     ty::TraitRef {
689         def_id: t.def_id,
690         substs: this.tcx().mk_substs(substs),
691     }
692 }
693
694 pub fn super_fold_mt<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
695                                                 mt: &ty::TypeAndMut<'tcx>)
696                                                 -> ty::TypeAndMut<'tcx> {
697     ty::TypeAndMut {ty: mt.ty.fold_with(this),
698             mutbl: mt.mutbl}
699 }
700
701 pub fn super_fold_existential_bounds<'tcx, T: TypeFolder<'tcx>>(
702     this: &mut T,
703     bounds: &ty::ExistentialBounds<'tcx>)
704     -> ty::ExistentialBounds<'tcx>
705 {
706     ty::ExistentialBounds {
707         region_bound: bounds.region_bound.fold_with(this),
708         builtin_bounds: bounds.builtin_bounds,
709         projection_bounds: bounds.projection_bounds.fold_with(this),
710     }
711 }
712
713 pub fn super_fold_autoref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
714                                                      autoref: &ty::AutoRef<'tcx>)
715                                                      -> ty::AutoRef<'tcx>
716 {
717     match *autoref {
718         ty::AutoPtr(r, m) => {
719             let r = r.fold_with(this);
720             ty::AutoPtr(this.tcx().mk_region(r), m)
721         }
722         ty::AutoUnsafe(m) => ty::AutoUnsafe(m)
723     }
724 }
725
726 pub fn super_fold_item_substs<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
727                                                          substs: ty::ItemSubsts<'tcx>)
728                                                          -> ty::ItemSubsts<'tcx>
729 {
730     ty::ItemSubsts {
731         substs: substs.substs.fold_with(this),
732     }
733 }
734
735 ///////////////////////////////////////////////////////////////////////////
736 // Some sample folders
737
738 pub struct BottomUpFolder<'a, 'tcx: 'a, F> where F: FnMut(Ty<'tcx>) -> Ty<'tcx> {
739     pub tcx: &'a ty::ctxt<'tcx>,
740     pub fldop: F,
741 }
742
743 impl<'a, 'tcx, F> TypeFolder<'tcx> for BottomUpFolder<'a, 'tcx, F> where
744     F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
745 {
746     fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
747
748     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
749         let t1 = super_fold_ty(self, ty);
750         (self.fldop)(t1)
751     }
752 }
753
754 ///////////////////////////////////////////////////////////////////////////
755 // Region folder
756
757 /// Folds over the substructure of a type, visiting its component
758 /// types and all regions that occur *free* within it.
759 ///
760 /// That is, `Ty` can contain function or method types that bind
761 /// regions at the call site (`ReLateBound`), and occurrences of
762 /// regions (aka "lifetimes") that are bound within a type are not
763 /// visited by this folder; only regions that occur free will be
764 /// visited by `fld_r`.
765
766 pub struct RegionFolder<'a, 'tcx: 'a> {
767     tcx: &'a ty::ctxt<'tcx>,
768     skipped_regions: &'a mut bool,
769     current_depth: u32,
770     fld_r: &'a mut (FnMut(ty::Region, u32) -> ty::Region + 'a),
771 }
772
773 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
774     pub fn new<F>(tcx: &'a ty::ctxt<'tcx>,
775                   skipped_regions: &'a mut bool,
776                   fld_r: &'a mut F) -> RegionFolder<'a, 'tcx>
777         where F : FnMut(ty::Region, u32) -> ty::Region
778     {
779         RegionFolder {
780             tcx: tcx,
781             skipped_regions: skipped_regions,
782             current_depth: 1,
783             fld_r: fld_r,
784         }
785     }
786 }
787
788 /// Collects the free and escaping regions in `value` into `region_set`. Returns
789 /// whether any late-bound regions were skipped
790 pub fn collect_regions<'tcx,T>(tcx: &ty::ctxt<'tcx>,
791                                value: &T,
792                                region_set: &mut FnvHashSet<ty::Region>) -> bool
793     where T : TypeFoldable<'tcx>
794 {
795     let mut have_bound_regions = false;
796     fold_regions(tcx, value, &mut have_bound_regions,
797                  |r, d| { region_set.insert(r.from_depth(d)); r });
798     have_bound_regions
799 }
800
801 /// Folds the escaping and free regions in `value` using `f`, and
802 /// sets `skipped_regions` to true if any late-bound region was found
803 /// and skipped.
804 pub fn fold_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
805                               value: &T,
806                               skipped_regions: &mut bool,
807                               mut f: F)
808                               -> T
809     where F : FnMut(ty::Region, u32) -> ty::Region,
810           T : TypeFoldable<'tcx>,
811 {
812     value.fold_with(&mut RegionFolder::new(tcx, skipped_regions, &mut f))
813 }
814
815 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
816 {
817     fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
818
819     fn enter_region_binder(&mut self) {
820         self.current_depth += 1;
821     }
822
823     fn exit_region_binder(&mut self) {
824         self.current_depth -= 1;
825     }
826
827     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
828         match r {
829             ty::ReLateBound(debruijn, _) if debruijn.depth < self.current_depth => {
830                 debug!("RegionFolder.fold_region({:?}) skipped bound region (current depth={})",
831                        r, self.current_depth);
832                 *self.skipped_regions = true;
833                 r
834             }
835             _ => {
836                 debug!("RegionFolder.fold_region({:?}) folding free region (current_depth={})",
837                        r, self.current_depth);
838                 (self.fld_r)(r, self.current_depth)
839             }
840         }
841     }
842 }
843
844 ///////////////////////////////////////////////////////////////////////////
845 // Late-bound region replacer
846
847 // Replaces the escaping regions in a type.
848
849 struct RegionReplacer<'a, 'tcx: 'a> {
850     tcx: &'a ty::ctxt<'tcx>,
851     current_depth: u32,
852     fld_r: &'a mut (FnMut(ty::BoundRegion) -> ty::Region + 'a),
853     map: FnvHashMap<ty::BoundRegion, ty::Region>
854 }
855
856 impl<'a, 'tcx> RegionReplacer<'a, 'tcx> {
857     fn new<F>(tcx: &'a ty::ctxt<'tcx>, fld_r: &'a mut F) -> RegionReplacer<'a, 'tcx>
858         where F : FnMut(ty::BoundRegion) -> ty::Region
859     {
860         RegionReplacer {
861             tcx: tcx,
862             current_depth: 1,
863             fld_r: fld_r,
864             map: FnvHashMap()
865         }
866     }
867 }
868
869 pub fn replace_late_bound_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
870                                             value: &ty::Binder<T>,
871                                             mut f: F)
872                                             -> (T, FnvHashMap<ty::BoundRegion, ty::Region>)
873     where F : FnMut(ty::BoundRegion) -> ty::Region,
874           T : TypeFoldable<'tcx>,
875 {
876     debug!("replace_late_bound_regions({:?})", value);
877     let mut replacer = RegionReplacer::new(tcx, &mut f);
878     let result = value.skip_binder().fold_with(&mut replacer);
879     (result, replacer.map)
880 }
881
882 impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
883 {
884     fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
885
886     fn enter_region_binder(&mut self) {
887         self.current_depth += 1;
888     }
889
890     fn exit_region_binder(&mut self) {
891         self.current_depth -= 1;
892     }
893
894     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
895         if !t.has_regions_escaping_depth(self.current_depth-1) {
896             return t;
897         }
898
899         super_fold_ty(self, t)
900     }
901
902     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
903         match r {
904             ty::ReLateBound(debruijn, br) if debruijn.depth == self.current_depth => {
905                 debug!("RegionReplacer.fold_region({:?}) folding region (current_depth={})",
906                        r, self.current_depth);
907                 let fld_r = &mut self.fld_r;
908                 let region = *self.map.entry(br).or_insert_with(|| fld_r(br));
909                 if let ty::ReLateBound(debruijn1, br) = region {
910                     // If the callback returns a late-bound region,
911                     // that region should always use depth 1. Then we
912                     // adjust it to the correct depth.
913                     assert_eq!(debruijn1.depth, 1);
914                     ty::ReLateBound(debruijn, br)
915                 } else {
916                     region
917                 }
918             }
919             r => r
920         }
921     }
922 }
923
924 ///////////////////////////////////////////////////////////////////////////
925 // Region eraser
926 //
927 // Replaces all free regions with 'static. Useful in contexts, such as
928 // method probing, where precise region relationships are not
929 // important. Note that in trans you should use
930 // `common::erase_regions` instead.
931
932 pub struct RegionEraser<'a, 'tcx: 'a> {
933     tcx: &'a ty::ctxt<'tcx>,
934 }
935
936 pub fn erase_regions<'tcx, T: TypeFoldable<'tcx>>(tcx: &ty::ctxt<'tcx>, t: T) -> T {
937     let mut eraser = RegionEraser { tcx: tcx };
938     t.fold_with(&mut eraser)
939 }
940
941 impl<'a, 'tcx> TypeFolder<'tcx> for RegionEraser<'a, 'tcx> {
942     fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
943
944     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
945         if !t.has_erasable_regions() {
946             return t;
947         }
948
949         super_fold_ty(self, t)
950     }
951
952     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
953         // because whether or not a region is bound affects subtyping,
954         // we can't erase the bound/free distinction, but we can
955         // replace all free regions with 'static
956         match r {
957             ty::ReLateBound(..) | ty::ReEarlyBound(..) => r,
958             _ => ty::ReStatic
959         }
960     }
961 }
962
963 ///////////////////////////////////////////////////////////////////////////
964 // Region shifter
965 //
966 // Shifts the De Bruijn indices on all escaping bound regions by a
967 // fixed amount. Useful in substitution or when otherwise introducing
968 // a binding level that is not intended to capture the existing bound
969 // regions. See comment on `shift_regions_through_binders` method in
970 // `subst.rs` for more details.
971
972 pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
973     match region {
974         ty::ReLateBound(debruijn, br) => {
975             ty::ReLateBound(debruijn.shifted(amount), br)
976         }
977         _ => {
978             region
979         }
980     }
981 }
982
983 pub fn shift_regions<'tcx, T:TypeFoldable<'tcx>>(tcx: &ty::ctxt<'tcx>,
984                                                  amount: u32, value: &T) -> T {
985     debug!("shift_regions(value={:?}, amount={})",
986            value, amount);
987
988     value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
989         shift_region(region, amount)
990     }))
991 }