Initial checkin of gep_type_begin/end which will be used to address PR82
[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 "Support/iterator"
19 #include "llvm/iMemory.h"
20 #include "llvm/DerivedTypes.h"
21
22 namespace llvm {
23   class GetElementPtrTypeIterator
24     : public forward_iterator<const Type *, ptrdiff_t> {
25     typedef forward_iterator<const Type*, ptrdiff_t> super;
26
27     GetElementPtrInst *TheGEP;
28     const Type *CurTy;
29     unsigned Operand;
30     
31     GetElementPtrTypeIterator() {}
32   public:
33
34     static GetElementPtrTypeIterator begin(GetElementPtrInst *gep) {
35       GetElementPtrTypeIterator I;
36       I.TheGEP = gep;
37       I.CurTy = gep->getOperand(0)->getType();
38       I.Operand = 1;
39       return I;
40     }
41     static GetElementPtrTypeIterator end(GetElementPtrInst *gep) {
42       GetElementPtrTypeIterator I;
43       I.TheGEP = gep;
44       I.CurTy = 0;
45       I.Operand = gep->getNumOperands();
46       return I;
47     }
48
49     bool operator==(const GetElementPtrTypeIterator& x) const { 
50       return Operand == x.Operand;
51     }
52     bool operator!=(const GetElementPtrTypeIterator& x) const {
53       return !operator==(x);
54     }
55
56     const Type *operator*() const { 
57       return CurTy;
58     }
59
60     // This is a non-standard operator->.  It allows you to call methods on the
61     // current type directly.
62     const Type *operator->() const { return operator*(); }
63     
64     unsigned getOperandNum() const { return Operand; }
65
66     Value *getOperand() const { return TheGEP->getOperand(Operand); }
67
68     GetElementPtrTypeIterator& operator++() {   // Preincrement
69       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
70         CurTy = CT->getTypeAtIndex(getOperand());
71       } else {
72         CurTy = 0;
73       }
74       ++Operand;
75       return *this; 
76     }
77
78     GetElementPtrTypeIterator operator++(int) { // Postincrement
79       GetElementPtrTypeIterator tmp = *this; ++*this; return tmp; 
80     }
81   };
82
83   inline GetElementPtrTypeIterator gep_type_begin(GetElementPtrInst *GEP) {
84     return GetElementPtrTypeIterator::begin(GEP);
85   }
86
87   inline GetElementPtrTypeIterator gep_type_end(GetElementPtrInst *GEP) {
88     return GetElementPtrTypeIterator::end(GEP);
89   }
90 } // end namespace llvm
91
92 #endif