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 gep_type_iterator
24 : public forward_iterator<const Type *, ptrdiff_t> {
25 typedef forward_iterator<const Type*, ptrdiff_t> super;
27 User *TheGEP; // Either GetElementPtrInst or ConstantExpr
31 gep_type_iterator() {}
34 static gep_type_iterator begin(User *gep) {
37 I.CurTy = gep->getOperand(0)->getType();
41 static gep_type_iterator end(User *gep) {
45 I.Operand = gep->getNumOperands();
49 bool operator==(const gep_type_iterator& x) const {
50 return Operand == x.Operand;
52 bool operator!=(const gep_type_iterator& 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 gep_type_iterator& operator++() { // Preincrement
69 if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
70 CurTy = CT->getTypeAtIndex(getOperand());
78 gep_type_iterator operator++(int) { // Postincrement
79 gep_type_iterator tmp = *this; ++*this; return tmp;
83 inline gep_type_iterator gep_type_begin(User *GEP) {
84 return gep_type_iterator::begin(GEP);
87 inline gep_type_iterator gep_type_end(User *GEP) {
88 return gep_type_iterator::end(GEP);
90 inline gep_type_iterator gep_type_begin(User &GEP) {
91 return gep_type_iterator::begin(&GEP);
94 inline gep_type_iterator gep_type_end(User &GEP) {
95 return gep_type_iterator::end(&GEP);
97 } // end namespace llvm