Significantly simplify gep_type_iterator, and make its interface more general/powerful
[oota-llvm.git] / include / llvm / Support / GetElementPtrTypeIterator.h
1 //===- llvm/Support/GetElementPtrTypeIterator.h -----------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements an iterator for walking through the types indexed by
11 // getelementptr instructions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_GETELEMENTPTRTYPE_H
16 #define LLVM_SUPPORT_GETELEMENTPTRTYPE_H
17
18 #include "llvm/User.h"
19 #include "llvm/DerivedTypes.h"
20
21 namespace llvm {
22   class gep_type_iterator
23     : public forward_iterator<const Type *, ptrdiff_t> {
24     typedef forward_iterator<const Type*, ptrdiff_t> super;
25
26     User::op_iterator OpIt;
27     const Type *CurTy;
28     gep_type_iterator() {}
29   public:
30
31     static gep_type_iterator begin(const Type *Ty, User::op_iterator It) {
32       gep_type_iterator I;
33       I.CurTy = Ty;
34       I.OpIt = It;
35       return I;
36     }
37     static gep_type_iterator end(User::op_iterator It) {
38       gep_type_iterator I;
39       I.CurTy = 0;
40       I.OpIt = It;
41       return I;
42     }
43
44     bool operator==(const gep_type_iterator& x) const { 
45       return OpIt == x.OpIt;
46     }
47     bool operator!=(const gep_type_iterator& x) const {
48       return !operator==(x);
49     }
50
51     const Type *operator*() const { 
52       return CurTy;
53     }
54
55     // This is a non-standard operator->.  It allows you to call methods on the
56     // current type directly.
57     const Type *operator->() const { return operator*(); }
58     
59     Value *getOperand() const { return *OpIt; }
60
61     gep_type_iterator& operator++() {   // Preincrement
62       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
63         CurTy = CT->getTypeAtIndex(getOperand());
64       } else {
65         CurTy = 0;
66       }
67       ++OpIt;
68       return *this; 
69     }
70
71     gep_type_iterator operator++(int) { // Postincrement
72       gep_type_iterator tmp = *this; ++*this; return tmp; 
73     }
74   };
75
76   inline gep_type_iterator gep_type_begin(User *GEP) {
77     return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
78                                     GEP->op_begin()+1);
79   }
80   inline gep_type_iterator gep_type_end(User *GEP) {
81     return gep_type_iterator::end(GEP->op_end());
82   }
83   inline gep_type_iterator gep_type_begin(User &GEP) {
84     return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
85                                     GEP.op_begin()+1);
86   }
87   inline gep_type_iterator gep_type_end(User &GEP) {
88     return gep_type_iterator::end(GEP.op_end());
89   }
90   inline gep_type_iterator gep_type_begin(const Type *Op0, User::op_iterator I,
91                                           User::op_iterator E) {
92     return gep_type_iterator::begin(Op0, I);
93   }
94   inline gep_type_iterator gep_type_end(const Type *Op0, User::op_iterator I,
95                                         User::op_iterator E) {
96     return gep_type_iterator::end(E);
97   }
98 } // end namespace llvm
99
100 #endif