1 //===- llvm/Support/GetElementPtrTypeIterator.h -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements an iterator for walking through the types indexed by
11 // getelementptr instructions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_GETELEMENTPTRTYPE_H
16 #define LLVM_SUPPORT_GETELEMENTPTRTYPE_H
18 #include "Support/iterator"
19 #include "llvm/iMemory.h"
20 #include "llvm/DerivedTypes.h"
23 class GetElementPtrTypeIterator
24 : public forward_iterator<const Type *, ptrdiff_t> {
25 typedef forward_iterator<const Type*, ptrdiff_t> super;
27 GetElementPtrInst *TheGEP;
31 GetElementPtrTypeIterator() {}
34 static GetElementPtrTypeIterator begin(GetElementPtrInst *gep) {
35 GetElementPtrTypeIterator I;
37 I.CurTy = gep->getOperand(0)->getType();
41 static GetElementPtrTypeIterator end(GetElementPtrInst *gep) {
42 GetElementPtrTypeIterator I;
45 I.Operand = gep->getNumOperands();
49 bool operator==(const GetElementPtrTypeIterator& x) const {
50 return Operand == x.Operand;
52 bool operator!=(const GetElementPtrTypeIterator& x) const {
53 return !operator==(x);
56 const Type *operator*() const {
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*(); }
64 unsigned getOperandNum() const { return Operand; }
66 Value *getOperand() const { return TheGEP->getOperand(Operand); }
68 GetElementPtrTypeIterator& operator++() { // Preincrement
69 if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
70 CurTy = CT->getTypeAtIndex(getOperand());
78 GetElementPtrTypeIterator operator++(int) { // Postincrement
79 GetElementPtrTypeIterator tmp = *this; ++*this; return tmp;
83 inline GetElementPtrTypeIterator gep_type_begin(GetElementPtrInst *GEP) {
84 return GetElementPtrTypeIterator::begin(GEP);
87 inline GetElementPtrTypeIterator gep_type_end(GetElementPtrInst *GEP) {
88 return GetElementPtrTypeIterator::end(GEP);
90 } // end namespace llvm