2014-12-26 03:20:01 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! An iterator over the type substructure.
|
2015-06-30 00:32:39 +03:00
|
|
|
//! WARNING: this does not keep track of the region depth.
|
2014-12-26 03:20:01 -05:00
|
|
|
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{self, Ty};
|
2016-10-18 08:23:09 +11:00
|
|
|
use rustc_data_structures::small_vec::SmallVec;
|
|
|
|
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
|
|
|
|
|
|
|
|
// The TypeWalker's stack is hot enough that it's worth going to some effort to
|
|
|
|
// avoid heap allocations.
|
|
|
|
pub type TypeWalkerArray<'tcx> = [Ty<'tcx>; 8];
|
|
|
|
pub type TypeWalkerStack<'tcx> = SmallVec<TypeWalkerArray<'tcx>>;
|
2014-12-26 03:20:01 -05:00
|
|
|
|
|
|
|
pub struct TypeWalker<'tcx> {
|
2016-10-18 08:23:09 +11:00
|
|
|
stack: TypeWalkerStack<'tcx>,
|
2015-03-25 17:06:52 -07:00
|
|
|
last_subtree: usize,
|
2014-12-26 03:20:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeWalker<'tcx> {
|
|
|
|
pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> {
|
2016-10-18 08:23:09 +11:00
|
|
|
TypeWalker { stack: SmallVec::one(ty), last_subtree: 1, }
|
2014-12-26 03:20:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Skips the subtree of types corresponding to the last type
|
|
|
|
/// returned by `next()`.
|
|
|
|
///
|
2015-03-25 17:06:52 -07:00
|
|
|
/// Example: Imagine you are walking `Foo<Bar<int>, usize>`.
|
2014-12-26 03:20:01 -05:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2014-12-26 03:20:01 -05:00
|
|
|
/// let mut iter: TypeWalker = ...;
|
|
|
|
/// iter.next(); // yields Foo
|
|
|
|
/// iter.next(); // yields Bar<int>
|
|
|
|
/// iter.skip_current_subtree(); // skips int
|
2015-03-25 17:06:52 -07:00
|
|
|
/// iter.next(); // yields usize
|
2014-12-26 03:20:01 -05:00
|
|
|
/// ```
|
|
|
|
pub fn skip_current_subtree(&mut self) {
|
|
|
|
self.stack.truncate(self.last_subtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
impl<'tcx> Iterator for TypeWalker<'tcx> {
|
|
|
|
type Item = Ty<'tcx>;
|
|
|
|
|
2014-12-26 03:20:01 -05:00
|
|
|
fn next(&mut self) -> Option<Ty<'tcx>> {
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("next(): stack={:?}", self.stack);
|
2014-12-26 03:20:01 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
None => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty) => {
|
|
|
|
self.last_subtree = self.stack.len();
|
2015-03-30 17:46:34 -04:00
|
|
|
push_subtypes(&mut self.stack, ty);
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("next: stack={:?}", self.stack);
|
2014-12-26 03:20:01 -05:00
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 17:46:34 -04:00
|
|
|
|
2016-10-18 08:23:09 +11:00
|
|
|
pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> AccIntoIter<TypeWalkerArray<'tcx>> {
|
|
|
|
let mut stack = SmallVec::new();
|
2015-03-30 17:46:34 -04:00
|
|
|
push_subtypes(&mut stack, ty);
|
|
|
|
stack.into_iter()
|
|
|
|
}
|
|
|
|
|
2016-08-27 01:13:48 +03:00
|
|
|
// We push types on the stack in reverse order so as to
|
|
|
|
// maintain a pre-order traversal. As of the time of this
|
|
|
|
// writing, the fact that the traversal is pre-order is not
|
|
|
|
// known to be significant to any code, but it seems like the
|
|
|
|
// natural order one would expect (basically, the order of the
|
|
|
|
// types as they are written).
|
2016-10-18 08:23:09 +11:00
|
|
|
fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
|
2015-03-30 17:46:34 -04:00
|
|
|
match parent_ty.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
|
2016-08-02 15:56:20 +08:00
|
|
|
ty::TyStr | ty::TyInfer(_) | ty::TyParam(_) | ty::TyNever | ty::TyError => {
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
2017-01-21 17:40:31 +03:00
|
|
|
ty::TyArray(ty, _) | ty::TySlice(ty) => {
|
2015-03-30 17:46:34 -04:00
|
|
|
stack.push(ty);
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyRawPtr(ref mt) | ty::TyRef(_, ref mt) => {
|
2015-03-30 17:46:34 -04:00
|
|
|
stack.push(mt.ty);
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyProjection(ref data) => {
|
2016-08-27 01:13:48 +03:00
|
|
|
stack.extend(data.trait_ref.substs.types().rev());
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
2016-11-16 09:21:49 -07:00
|
|
|
ty::TyDynamic(ref obj, ..) => {
|
|
|
|
stack.extend(obj.iter().rev().flat_map(|predicate| {
|
|
|
|
let (substs, opt_ty) = match *predicate.skip_binder() {
|
|
|
|
ty::ExistentialPredicate::Trait(tr) => (tr.substs, None),
|
|
|
|
ty::ExistentialPredicate::Projection(p) =>
|
|
|
|
(p.trait_ref.substs, Some(p.ty)),
|
|
|
|
ty::ExistentialPredicate::AutoTrait(_) =>
|
|
|
|
// Empty iterator
|
|
|
|
(ty::Substs::empty(), None),
|
|
|
|
};
|
|
|
|
|
|
|
|
substs.types().rev().chain(opt_ty)
|
|
|
|
}));
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(_, substs) | ty::TyAnon(_, substs) => {
|
2016-08-27 01:13:48 +03:00
|
|
|
stack.extend(substs.types().rev());
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
2015-07-16 09:46:35 -04:00
|
|
|
ty::TyClosure(_, ref substs) => {
|
2016-11-03 22:19:33 +02:00
|
|
|
stack.extend(substs.substs.types().rev());
|
2015-07-16 05:32:45 -04:00
|
|
|
}
|
2017-01-11 15:58:37 +08:00
|
|
|
ty::TyTuple(ts, _) => {
|
2016-08-27 01:13:48 +03:00
|
|
|
stack.extend(ts.iter().cloned().rev());
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
2016-02-16 18:36:41 +02:00
|
|
|
ty::TyFnDef(_, substs, ref ft) => {
|
2016-08-27 01:13:48 +03:00
|
|
|
stack.extend(substs.types().rev());
|
2016-02-16 18:36:41 +02:00
|
|
|
push_sig_subtypes(stack, &ft.sig);
|
|
|
|
}
|
|
|
|
ty::TyFnPtr(ref ft) => {
|
2015-03-30 17:46:34 -04:00
|
|
|
push_sig_subtypes(stack, &ft.sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 08:23:09 +11:00
|
|
|
fn push_sig_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, sig: &ty::PolyFnSig<'tcx>) {
|
2016-11-28 19:35:38 -07:00
|
|
|
stack.push(sig.skip_binder().output());
|
|
|
|
stack.extend(sig.skip_binder().inputs().iter().cloned().rev());
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|