1 //===- llvm/Support/GetElementPtrTypeIterator.h -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "llvm/User.h"
19 #include "llvm/DerivedTypes.h"
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;
29 generic_gep_type_iterator() {}
32 static generic_gep_type_iterator begin(const Type *Ty, ItTy It) {
33 generic_gep_type_iterator I;
38 static generic_gep_type_iterator end(ItTy It) {
39 generic_gep_type_iterator I;
45 bool operator==(const generic_gep_type_iterator& x) const {
46 return OpIt == x.OpIt;
48 bool operator!=(const generic_gep_type_iterator& x) const {
49 return !operator==(x);
52 const Type *operator*() const {
56 const Type *getIndexedType() const {
57 const CompositeType *CT = cast<CompositeType>(CurTy);
58 return CT->getTypeAtIndex(getOperand());
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*(); }
65 Value *getOperand() const { return *OpIt; }
67 generic_gep_type_iterator& operator++() { // Preincrement
68 if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
69 CurTy = CT->getTypeAtIndex(getOperand());
77 generic_gep_type_iterator operator++(int) { // Postincrement
78 generic_gep_type_iterator tmp = *this; ++*this; return tmp;
82 typedef generic_gep_type_iterator<> gep_type_iterator;
84 inline gep_type_iterator gep_type_begin(const User *GEP) {
85 return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
88 inline gep_type_iterator gep_type_end(const User *GEP) {
89 return gep_type_iterator::end(GEP->op_end());
91 inline gep_type_iterator gep_type_begin(const User &GEP) {
92 return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
95 inline gep_type_iterator gep_type_end(const User &GEP) {
96 return gep_type_iterator::end(GEP.op_end());
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);
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);
110 } // end namespace llvm