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 std::iterator<std::forward_iterator_tag, const Type *, ptrdiff_t> {
25 typedef std::iterator<std::forward_iterator_tag,
26 const Type *, ptrdiff_t> super;
30 generic_gep_type_iterator() {}
33 static generic_gep_type_iterator begin(const Type *Ty, ItTy It) {
34 generic_gep_type_iterator I;
39 static generic_gep_type_iterator end(ItTy It) {
40 generic_gep_type_iterator I;
46 bool operator==(const generic_gep_type_iterator& x) const {
47 return OpIt == x.OpIt;
49 bool operator!=(const generic_gep_type_iterator& x) const {
50 return !operator==(x);
53 const Type *operator*() const {
57 const Type *getIndexedType() const {
58 const CompositeType *CT = cast<CompositeType>(CurTy);
59 return CT->getTypeAtIndex(getOperand());
62 // This is a non-standard operator->. It allows you to call methods on the
63 // current type directly.
64 const Type *operator->() const { return operator*(); }
66 Value *getOperand() const { return *OpIt; }
68 generic_gep_type_iterator& operator++() { // Preincrement
69 if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
70 CurTy = CT->getTypeAtIndex(getOperand());
78 generic_gep_type_iterator operator++(int) { // Postincrement
79 generic_gep_type_iterator tmp = *this; ++*this; return tmp;
83 typedef generic_gep_type_iterator<> gep_type_iterator;
85 inline gep_type_iterator gep_type_begin(const User *GEP) {
86 return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
89 inline gep_type_iterator gep_type_end(const User *GEP) {
90 return gep_type_iterator::end(GEP->op_end());
92 inline gep_type_iterator gep_type_begin(const User &GEP) {
93 return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
96 inline gep_type_iterator gep_type_end(const User &GEP) {
97 return gep_type_iterator::end(GEP.op_end());
100 template<typename ItTy>
101 inline generic_gep_type_iterator<ItTy>
102 gep_type_begin(const Type *Op0, ItTy I, ItTy E) {
103 return generic_gep_type_iterator<ItTy>::begin(Op0, I);
106 template<typename ItTy>
107 inline generic_gep_type_iterator<ItTy>
108 gep_type_end(const Type *Op0, ItTy I, ItTy E) {
109 return generic_gep_type_iterator<ItTy>::end(E);
111 } // end namespace llvm