1 // Copyright 2012-2014 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 //! An iterator over the type substructure.
13 use middle::ty::{mod, Ty};
14 use std::iter::Iterator;
16 pub struct TypeWalker<'tcx> {
21 impl<'tcx> TypeWalker<'tcx> {
22 pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> {
23 TypeWalker { stack: vec!(ty), last_subtree: 1, }
26 fn push_subtypes(&mut self, parent_ty: Ty<'tcx>) {
28 ty::ty_bool | ty::ty_char | ty::ty_int(_) | ty::ty_uint(_) | ty::ty_float(_) |
29 ty::ty_str | ty::ty_infer(_) | ty::ty_param(_) | ty::ty_err => {
31 ty::ty_uniq(ty) | ty::ty_vec(ty, _) | ty::ty_open(ty) => {
34 ty::ty_ptr(ref mt) | ty::ty_rptr(_, ref mt) => {
35 self.stack.push(mt.ty);
37 ty::ty_projection(ref data) => {
38 self.push_reversed(data.trait_ref.substs.types.as_slice());
40 ty::ty_trait(box ty::TyTrait { ref principal, .. }) => {
41 self.push_reversed(principal.substs().types.as_slice());
43 ty::ty_enum(_, ref substs) |
44 ty::ty_struct(_, ref substs) |
45 ty::ty_unboxed_closure(_, _, ref substs) => {
46 self.push_reversed(substs.types.as_slice());
48 ty::ty_tup(ref ts) => {
49 self.push_reversed(ts.as_slice());
51 ty::ty_bare_fn(_, ref ft) => {
52 self.push_sig_subtypes(&ft.sig);
54 ty::ty_closure(ref ft) => {
55 self.push_sig_subtypes(&ft.sig);
60 fn push_sig_subtypes(&mut self, sig: &ty::PolyFnSig<'tcx>) {
62 ty::FnConverging(output) => { self.stack.push(output); }
63 ty::FnDiverging => { }
65 self.push_reversed(sig.0.inputs.as_slice());
68 fn push_reversed(&mut self, tys: &[Ty<'tcx>]) {
69 // We push slices on the stack in reverse order so as to
70 // maintain a pre-order traversal. As of the time of this
71 // writing, the fact that the traversal is pre-order is not
72 // known to be significant to any code, but it seems like the
73 // natural order one would expect (basically, the order of the
74 // types as they are written).
75 for &ty in tys.iter().rev() {
80 /// Skips the subtree of types corresponding to the last type
81 /// returned by `next()`.
83 /// Example: Imagine you are walking `Foo<Bar<int>, uint>`.
86 /// let mut iter: TypeWalker = ...;
87 /// iter.next(); // yields Foo
88 /// iter.next(); // yields Bar<int>
89 /// iter.skip_current_subtree(); // skips int
90 /// iter.next(); // yields uint
92 pub fn skip_current_subtree(&mut self) {
93 self.stack.truncate(self.last_subtree);
97 impl<'tcx> Iterator for TypeWalker<'tcx> {
100 fn next(&mut self) -> Option<Ty<'tcx>> {
101 debug!("next(): stack={}", self.stack);
102 match self.stack.pop() {
107 self.last_subtree = self.stack.len();
108 self.push_subtypes(ty);
109 debug!("next: stack={}", self.stack);