1 //===- 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_IR_GETELEMENTPTRTYPEITERATOR_H
16 #define LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Operator.h"
20 #include "llvm/IR/User.h"
21 #include "llvm/ADT/PointerIntPair.h"
24 template<typename ItTy = User::const_op_iterator>
25 class generic_gep_type_iterator
26 : public std::iterator<std::forward_iterator_tag, Type *, ptrdiff_t> {
27 typedef std::iterator<std::forward_iterator_tag,
28 Type *, ptrdiff_t> super;
31 PointerIntPair<Type *, 1> CurTy;
33 generic_gep_type_iterator() {}
36 static generic_gep_type_iterator begin(Type *Ty, ItTy It) {
37 generic_gep_type_iterator I;
38 I.CurTy.setPointer(Ty);
42 static generic_gep_type_iterator begin(Type *Ty, unsigned AddrSpace,
44 generic_gep_type_iterator I;
45 I.CurTy.setPointer(Ty);
47 I.AddrSpace = AddrSpace;
51 static generic_gep_type_iterator end(ItTy It) {
52 generic_gep_type_iterator I;
57 bool operator==(const generic_gep_type_iterator& x) const {
58 return OpIt == x.OpIt;
60 bool operator!=(const generic_gep_type_iterator& x) const {
61 return !operator==(x);
64 Type *operator*() const {
66 return CurTy.getPointer()->getPointerTo(AddrSpace);
67 return CurTy.getPointer();
70 Type *getIndexedType() const {
72 return CurTy.getPointer();
73 CompositeType *CT = cast<CompositeType>(CurTy.getPointer());
74 return CT->getTypeAtIndex(getOperand());
77 // This is a non-standard operator->. It allows you to call methods on the
78 // current type directly.
79 Type *operator->() const { return operator*(); }
81 Value *getOperand() const { return const_cast<Value *>(&**OpIt); }
83 generic_gep_type_iterator& operator++() { // Preincrement
86 } else if (CompositeType *CT =
87 dyn_cast<CompositeType>(CurTy.getPointer())) {
88 CurTy.setPointer(CT->getTypeAtIndex(getOperand()));
90 CurTy.setPointer(nullptr);
96 generic_gep_type_iterator operator++(int) { // Postincrement
97 generic_gep_type_iterator tmp = *this; ++*this; return tmp;
101 typedef generic_gep_type_iterator<> gep_type_iterator;
103 inline gep_type_iterator gep_type_begin(const User *GEP) {
104 auto *GEPOp = cast<GEPOperator>(GEP);
105 return gep_type_iterator::begin(
106 GEPOp->getSourceElementType(),
107 cast<PointerType>(GEPOp->getPointerOperandType()->getScalarType())
109 GEP->op_begin() + 1);
111 inline gep_type_iterator gep_type_end(const User *GEP) {
112 return gep_type_iterator::end(GEP->op_end());
114 inline gep_type_iterator gep_type_begin(const User &GEP) {
115 auto &GEPOp = cast<GEPOperator>(GEP);
116 return gep_type_iterator::begin(
117 GEPOp.getSourceElementType(),
118 cast<PointerType>(GEPOp.getPointerOperandType()->getScalarType())
122 inline gep_type_iterator gep_type_end(const User &GEP) {
123 return gep_type_iterator::end(GEP.op_end());
127 inline generic_gep_type_iterator<const T *>
128 gep_type_begin(Type *Op0, ArrayRef<T> A) {
129 return generic_gep_type_iterator<const T *>::begin(Op0, A.begin());
133 inline generic_gep_type_iterator<const T *>
134 gep_type_end(Type * /*Op0*/, ArrayRef<T> A) {
135 return generic_gep_type_iterator<const T *>::end(A.end());
137 } // end namespace llvm