Support iteration over constant instructions
[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     // This is a non-standard operator->.  It allows you to call methods on the
57     // current type directly.
58     const Type *operator->() const { return operator*(); }
59     
60     Value *getOperand() const { return *OpIt; }
61
62     generic_gep_type_iterator& operator++() {   // Preincrement
63       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
64         CurTy = CT->getTypeAtIndex(getOperand());
65       } else {
66         CurTy = 0;
67       }
68       ++OpIt;
69       return *this; 
70     }
71
72     generic_gep_type_iterator operator++(int) { // Postincrement
73       generic_gep_type_iterator tmp = *this; ++*this; return tmp; 
74     }
75   };
76
77   typedef generic_gep_type_iterator<> gep_type_iterator;
78
79   inline gep_type_iterator gep_type_begin(const User *GEP) {
80     return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
81                                       GEP->op_begin()+1);
82   }
83   inline gep_type_iterator gep_type_end(const User *GEP) {
84     return gep_type_iterator::end(GEP->op_end());
85   }
86   inline gep_type_iterator gep_type_begin(const User &GEP) {
87     return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
88                                     GEP.op_begin()+1);
89   }
90   inline gep_type_iterator gep_type_end(const User &GEP) {
91     return gep_type_iterator::end(GEP.op_end());
92   }
93
94   template<typename ItTy>
95   inline generic_gep_type_iterator<ItTy>
96   gep_type_begin(const Type *Op0, ItTy I, ItTy E) {
97     return generic_gep_type_iterator<ItTy>::begin(Op0, I);
98   }
99
100   template<typename ItTy>
101   inline generic_gep_type_iterator<ItTy>
102   gep_type_end(const Type *Op0, ItTy I, ItTy E) {
103     return generic_gep_type_iterator<ItTy>::end(E);
104   }
105 } // end namespace llvm
106
107 #endif