Add a method
[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   template<typename ItTy = User::const_op_iterator>
23   class generic_gep_type_iterator
24     : public forward_iterator<const Type *, ptrdiff_t> {
25     typedef forward_iterator<const Type*, ptrdiff_t> super;
26
27     ItTy OpIt;
28     const Type *CurTy;
29     generic_gep_type_iterator() {}
30   public:
31
32     static generic_gep_type_iterator begin(const Type *Ty, ItTy It) {
33       generic_gep_type_iterator I;
34       I.CurTy = Ty;
35       I.OpIt = It;
36       return I;
37     }
38     static generic_gep_type_iterator end(ItTy It) {
39       generic_gep_type_iterator I;
40       I.CurTy = 0;
41       I.OpIt = It;
42       return I;
43     }
44
45     bool operator==(const generic_gep_type_iterator& x) const { 
46       return OpIt == x.OpIt;
47     }
48     bool operator!=(const generic_gep_type_iterator& x) const {
49       return !operator==(x);
50     }
51
52     const Type *operator*() const { 
53       return CurTy;
54     }
55
56     const Type *getIndexedType() const {
57       const CompositeType *CT = cast<CompositeType>(CurTy);
58       return CT->getTypeAtIndex(getOperand());
59     }
60
61     // This is a non-standard operator->.  It allows you to call methods on the
62     // current type directly.
63     const Type *operator->() const { return operator*(); }
64     
65     Value *getOperand() const { return *OpIt; }
66
67     generic_gep_type_iterator& operator++() {   // Preincrement
68       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
69         CurTy = CT->getTypeAtIndex(getOperand());
70       } else {
71         CurTy = 0;
72       }
73       ++OpIt;
74       return *this; 
75     }
76
77     generic_gep_type_iterator operator++(int) { // Postincrement
78       generic_gep_type_iterator tmp = *this; ++*this; return tmp; 
79     }
80   };
81
82   typedef generic_gep_type_iterator<> gep_type_iterator;
83
84   inline gep_type_iterator gep_type_begin(const User *GEP) {
85     return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
86                                       GEP->op_begin()+1);
87   }
88   inline gep_type_iterator gep_type_end(const User *GEP) {
89     return gep_type_iterator::end(GEP->op_end());
90   }
91   inline gep_type_iterator gep_type_begin(const User &GEP) {
92     return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
93                                     GEP.op_begin()+1);
94   }
95   inline gep_type_iterator gep_type_end(const User &GEP) {
96     return gep_type_iterator::end(GEP.op_end());
97   }
98
99   template<typename ItTy>
100   inline generic_gep_type_iterator<ItTy>
101   gep_type_begin(const Type *Op0, ItTy I, ItTy E) {
102     return generic_gep_type_iterator<ItTy>::begin(Op0, I);
103   }
104
105   template<typename ItTy>
106   inline generic_gep_type_iterator<ItTy>
107   gep_type_end(const Type *Op0, ItTy I, ItTy E) {
108     return generic_gep_type_iterator<ItTy>::end(E);
109   }
110 } // end namespace llvm
111
112 #endif