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. 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:
17 //! T.fold_with(F) --calls--> F.fold_T(T) --calls--> super_fold_T(F, T)
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.)
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()`.
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.
38 use middle::subst::VecPerParamSpace;
39 use middle::ty::{self, Ty, HasTypeFlags, RegionEscape};
46 use syntax::owned_slice::OwnedSlice;
47 use util::nodemap::{FnvHashMap, FnvHashSet};
49 ///////////////////////////////////////////////////////////////////////////
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;
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
63 pub trait TypeFolder<'tcx> : Sized {
64 fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx>;
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) { }
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) { }
77 fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
78 where T : TypeFoldable<'tcx>
80 // FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
81 super_fold_binder(self, t)
84 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
85 super_fold_ty(self, t)
88 fn fold_mt(&mut self, t: &ty::TypeAndMut<'tcx>) -> ty::TypeAndMut<'tcx> {
89 super_fold_mt(self, t)
92 fn fold_trait_ref(&mut self, t: &ty::TraitRef<'tcx>) -> ty::TraitRef<'tcx> {
93 super_fold_trait_ref(self, t)
96 fn fold_substs(&mut self,
97 substs: &subst::Substs<'tcx>)
98 -> subst::Substs<'tcx> {
99 super_fold_substs(self, substs)
102 fn fold_fn_sig(&mut self,
103 sig: &ty::FnSig<'tcx>)
105 super_fold_fn_sig(self, sig)
108 fn fold_output(&mut self,
109 output: &ty::FnOutput<'tcx>)
110 -> ty::FnOutput<'tcx> {
111 super_fold_output(self, output)
114 fn fold_bare_fn_ty(&mut self,
115 fty: &ty::BareFnTy<'tcx>)
116 -> ty::BareFnTy<'tcx>
118 super_fold_bare_fn_ty(self, fty)
121 fn fold_closure_ty(&mut self,
122 fty: &ty::ClosureTy<'tcx>)
123 -> ty::ClosureTy<'tcx> {
124 super_fold_closure_ty(self, fty)
127 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
131 fn fold_existential_bounds(&mut self, s: &ty::ExistentialBounds<'tcx>)
132 -> ty::ExistentialBounds<'tcx> {
133 super_fold_existential_bounds(self, s)
136 fn fold_autoref(&mut self, ar: &ty::AutoRef<'tcx>) -> ty::AutoRef<'tcx> {
137 super_fold_autoref(self, ar)
140 fn fold_item_substs(&mut self, i: ty::ItemSubsts<'tcx>) -> ty::ItemSubsts<'tcx> {
141 super_fold_item_substs(self, i)
145 ///////////////////////////////////////////////////////////////////////////
146 // TypeFoldable implementations.
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
156 macro_rules! CopyImpls {
159 impl<'tcx> TypeFoldable<'tcx> for $ty {
160 fn fold_with<F:TypeFolder<'tcx>>(&self, _: &mut F) -> $ty {
168 CopyImpls! { (), hir::Unsafety, abi::Abi }
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))
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))
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))
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);
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()
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)
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()
213 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
214 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> VecPerParamSpace<T> {
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.
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();
226 elem.fold_with(folder)
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();
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)
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)
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)
254 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
255 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TypeAndMut<'tcx> {
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)
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)
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)
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)
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)
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);
294 func_substs: folder.tcx().mk_substs(func_substs),
295 upvar_tys: self.upvar_tys.fold_with(folder),
300 impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
301 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ItemSubsts<'tcx> {
303 substs: self.substs.fold_with(folder),
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)
314 impl<'tcx> TypeFoldable<'tcx> for ty::BuiltinBounds {
315 fn fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> ty::BuiltinBounds {
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)
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 {
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),
340 impl<'tcx> TypeFoldable<'tcx> for ty::ObjectLifetimeDefault {
341 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ObjectLifetimeDefault {
343 ty::ObjectLifetimeDefault::Ambiguous =>
344 ty::ObjectLifetimeDefault::Ambiguous,
346 ty::ObjectLifetimeDefault::BaseDefault =>
347 ty::ObjectLifetimeDefault::BaseDefault,
349 ty::ObjectLifetimeDefault::Specific(r) =>
350 ty::ObjectLifetimeDefault::Specific(r.fold_with(folder)),
355 impl<'tcx> TypeFoldable<'tcx> for ty::RegionParameterDef {
356 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::RegionParameterDef {
357 ty::RegionParameterDef {
362 bounds: self.bounds.fold_with(folder)
367 impl<'tcx> TypeFoldable<'tcx> for ty::Generics<'tcx> {
368 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Generics<'tcx> {
370 types: self.types.fold_with(folder),
371 regions: self.regions.fold_with(folder),
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),
384 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
385 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Predicate<'tcx> {
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),
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),
414 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
415 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ProjectionTy<'tcx> {
417 trait_ref: self.trait_ref.fold_with(folder),
418 item_name: self.item_name,
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),
431 impl<'tcx,O> TypeFoldable<'tcx> for traits::Obligation<'tcx,O>
432 where O : TypeFoldable<'tcx>
434 fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::Obligation<'tcx, O> {
436 cause: self.cause.clone(),
437 recursion_depth: self.recursion_depth,
438 predicate: self.predicate.fold_with(folder),
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),
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),
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),
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),
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> {
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))
488 traits::VtableFnPointer(ref d) => {
489 traits::VtableFnPointer(d.fold_with(folder))
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)),
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
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))
514 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
515 fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TraitPredicate<'tcx> {
517 trait_ref: self.trait_ref.fold_with(folder)
522 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
523 where T : TypeFoldable<'tcx>,
524 U : TypeFoldable<'tcx>,
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))
532 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
533 fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureUpvar<'tcx> {
537 ty: self.ty.fold_with(folder),
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 {
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,
555 ///////////////////////////////////////////////////////////////////////////
556 // "super" routines: these are the default implementations for TypeFolder.
558 // They should invoke `foo.fold_with()` to do recursive folding.
560 pub fn super_fold_binder<'tcx, T, U>(this: &mut T,
561 binder: &ty::Binder<U>)
563 where T : TypeFolder<'tcx>, U : TypeFoldable<'tcx>
565 this.enter_region_binder();
566 let result = ty::Binder(binder.0.fold_with(this));
567 this.exit_region_binder();
571 pub fn super_fold_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
574 let sty = match ty.sty {
576 ty::TyBox(typ.fold_with(this))
578 ty::TyRawPtr(ref tm) => {
579 ty::TyRawPtr(tm.fold_with(this))
581 ty::TyArray(typ, sz) => {
582 ty::TyArray(typ.fold_with(this), sz)
584 ty::TySlice(typ) => {
585 ty::TySlice(typ.fold_with(this))
587 ty::TyEnum(tid, ref substs) => {
588 let substs = substs.fold_with(this);
589 ty::TyEnum(tid, this.tcx().mk_substs(substs))
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),
597 ty::TyTuple(ref ts) => {
598 ty::TyTuple(ts.fold_with(this))
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))
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))
608 ty::TyStruct(did, ref substs) => {
609 let substs = substs.fold_with(this);
610 ty::TyStruct(did, this.tcx().mk_substs(substs))
612 ty::TyClosure(did, ref substs) => {
613 let s = substs.fold_with(this);
614 ty::TyClosure(did, s)
616 ty::TyProjection(ref data) => {
617 ty::TyProjection(data.fold_with(this))
619 ty::TyBool | ty::TyChar | ty::TyStr |
620 ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
621 ty::TyError | ty::TyInfer(_) |
626 this.tcx().mk_ty(sty)
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 => {
636 subst::NonerasedRegions(ref regions) => {
637 subst::NonerasedRegions(regions.fold_with(this))
641 subst::Substs { regions: regions,
642 types: substs.types.fold_with(this) }
645 pub fn super_fold_fn_sig<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
646 sig: &ty::FnSig<'tcx>)
649 ty::FnSig { inputs: sig.inputs.fold_with(this),
650 output: sig.output.fold_with(this),
651 variadic: sig.variadic }
654 pub fn super_fold_output<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
655 output: &ty::FnOutput<'tcx>)
656 -> ty::FnOutput<'tcx> {
658 ty::FnConverging(ref ty) => ty::FnConverging(ty.fold_with(this)),
659 ty::FnDiverging => ty::FnDiverging
663 pub fn super_fold_bare_fn_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
664 fty: &ty::BareFnTy<'tcx>)
665 -> ty::BareFnTy<'tcx>
667 ty::BareFnTy { sig: fty.sig.fold_with(this),
669 unsafety: fty.unsafety }
672 pub fn super_fold_closure_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
673 fty: &ty::ClosureTy<'tcx>)
674 -> ty::ClosureTy<'tcx>
677 sig: fty.sig.fold_with(this),
678 unsafety: fty.unsafety,
683 pub fn super_fold_trait_ref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
684 t: &ty::TraitRef<'tcx>)
685 -> ty::TraitRef<'tcx>
687 let substs = t.substs.fold_with(this);
690 substs: this.tcx().mk_substs(substs),
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),
701 pub fn super_fold_existential_bounds<'tcx, T: TypeFolder<'tcx>>(
703 bounds: &ty::ExistentialBounds<'tcx>)
704 -> ty::ExistentialBounds<'tcx>
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),
713 pub fn super_fold_autoref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
714 autoref: &ty::AutoRef<'tcx>)
718 ty::AutoPtr(r, m) => {
719 let r = r.fold_with(this);
720 ty::AutoPtr(this.tcx().mk_region(r), m)
722 ty::AutoUnsafe(m) => ty::AutoUnsafe(m)
726 pub fn super_fold_item_substs<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
727 substs: ty::ItemSubsts<'tcx>)
728 -> ty::ItemSubsts<'tcx>
731 substs: substs.substs.fold_with(this),
735 ///////////////////////////////////////////////////////////////////////////
736 // Some sample folders
738 pub struct BottomUpFolder<'a, 'tcx: 'a, F> where F: FnMut(Ty<'tcx>) -> Ty<'tcx> {
739 pub tcx: &'a ty::ctxt<'tcx>,
743 impl<'a, 'tcx, F> TypeFolder<'tcx> for BottomUpFolder<'a, 'tcx, F> where
744 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
746 fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
748 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
749 let t1 = super_fold_ty(self, ty);
754 ///////////////////////////////////////////////////////////////////////////
757 /// Folds over the substructure of a type, visiting its component
758 /// types and all regions that occur *free* within it.
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`.
766 pub struct RegionFolder<'a, 'tcx: 'a> {
767 tcx: &'a ty::ctxt<'tcx>,
768 skipped_regions: &'a mut bool,
770 fld_r: &'a mut (FnMut(ty::Region, u32) -> ty::Region + 'a),
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
781 skipped_regions: skipped_regions,
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>,
792 region_set: &mut FnvHashSet<ty::Region>) -> bool
793 where T : TypeFoldable<'tcx>
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 });
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
804 pub fn fold_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
806 skipped_regions: &mut bool,
809 where F : FnMut(ty::Region, u32) -> ty::Region,
810 T : TypeFoldable<'tcx>,
812 value.fold_with(&mut RegionFolder::new(tcx, skipped_regions, &mut f))
815 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
817 fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
819 fn enter_region_binder(&mut self) {
820 self.current_depth += 1;
823 fn exit_region_binder(&mut self) {
824 self.current_depth -= 1;
827 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
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;
836 debug!("RegionFolder.fold_region({:?}) folding free region (current_depth={})",
837 r, self.current_depth);
838 (self.fld_r)(r, self.current_depth)
844 ///////////////////////////////////////////////////////////////////////////
845 // Late-bound region replacer
847 // Replaces the escaping regions in a type.
849 struct RegionReplacer<'a, 'tcx: 'a> {
850 tcx: &'a ty::ctxt<'tcx>,
852 fld_r: &'a mut (FnMut(ty::BoundRegion) -> ty::Region + 'a),
853 map: FnvHashMap<ty::BoundRegion, ty::Region>
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
869 pub fn replace_late_bound_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
870 value: &ty::Binder<T>,
872 -> (T, FnvHashMap<ty::BoundRegion, ty::Region>)
873 where F : FnMut(ty::BoundRegion) -> ty::Region,
874 T : TypeFoldable<'tcx>,
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)
882 impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
884 fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
886 fn enter_region_binder(&mut self) {
887 self.current_depth += 1;
890 fn exit_region_binder(&mut self) {
891 self.current_depth -= 1;
894 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
895 if !t.has_regions_escaping_depth(self.current_depth-1) {
899 super_fold_ty(self, t)
902 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
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)
924 ///////////////////////////////////////////////////////////////////////////
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.
932 pub struct RegionEraser<'a, 'tcx: 'a> {
933 tcx: &'a ty::ctxt<'tcx>,
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)
941 impl<'a, 'tcx> TypeFolder<'tcx> for RegionEraser<'a, 'tcx> {
942 fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
944 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
945 if !t.has_erasable_regions() {
949 super_fold_ty(self, t)
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
957 ty::ReLateBound(..) | ty::ReEarlyBound(..) => r,
963 ///////////////////////////////////////////////////////////////////////////
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.
972 pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
974 ty::ReLateBound(debruijn, br) => {
975 ty::ReLateBound(debruijn.shifted(amount), br)
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={})",
988 value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
989 shift_region(region, amount)