LLVM API Documentation

GetElementPtrTypeIterator.h

Go to the documentation of this file.
00001 //===- llvm/Support/GetElementPtrTypeIterator.h -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements an iterator for walking through the types indexed by
00011 // getelementptr instructions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_GETELEMENTPTRTYPE_H
00016 #define LLVM_SUPPORT_GETELEMENTPTRTYPE_H
00017 
00018 #include "llvm/User.h"
00019 #include "llvm/DerivedTypes.h"
00020 
00021 namespace llvm {
00022   template<typename ItTy = User::const_op_iterator>
00023   class generic_gep_type_iterator
00024     : public forward_iterator<const Type *, ptrdiff_t> {
00025     typedef forward_iterator<const Type*, ptrdiff_t> super;
00026 
00027     ItTy OpIt;
00028     const Type *CurTy;
00029     generic_gep_type_iterator() {}
00030   public:
00031 
00032     static generic_gep_type_iterator begin(const Type *Ty, ItTy It) {
00033       generic_gep_type_iterator I;
00034       I.CurTy = Ty;
00035       I.OpIt = It;
00036       return I;
00037     }
00038     static generic_gep_type_iterator end(ItTy It) {
00039       generic_gep_type_iterator I;
00040       I.CurTy = 0;
00041       I.OpIt = It;
00042       return I;
00043     }
00044 
00045     bool operator==(const generic_gep_type_iterator& x) const {
00046       return OpIt == x.OpIt;
00047     }
00048     bool operator!=(const generic_gep_type_iterator& x) const {
00049       return !operator==(x);
00050     }
00051 
00052     const Type *operator*() const {
00053       return CurTy;
00054     }
00055 
00056     const Type *getIndexedType() const {
00057       const CompositeType *CT = cast<CompositeType>(CurTy);
00058       return CT->getTypeAtIndex(getOperand());
00059     }
00060 
00061     // This is a non-standard operator->.  It allows you to call methods on the
00062     // current type directly.
00063     const Type *operator->() const { return operator*(); }
00064 
00065     Value *getOperand() const { return *OpIt; }
00066 
00067     generic_gep_type_iterator& operator++() {   // Preincrement
00068       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
00069         CurTy = CT->getTypeAtIndex(getOperand());
00070       } else {
00071         CurTy = 0;
00072       }
00073       ++OpIt;
00074       return *this;
00075     }
00076 
00077     generic_gep_type_iterator operator++(int) { // Postincrement
00078       generic_gep_type_iterator tmp = *this; ++*this; return tmp;
00079     }
00080   };
00081 
00082   typedef generic_gep_type_iterator<> gep_type_iterator;
00083 
00084   inline gep_type_iterator gep_type_begin(const User *GEP) {
00085     return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
00086                                       GEP->op_begin()+1);
00087   }
00088   inline gep_type_iterator gep_type_end(const User *GEP) {
00089     return gep_type_iterator::end(GEP->op_end());
00090   }
00091   inline gep_type_iterator gep_type_begin(const User &GEP) {
00092     return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
00093                                     GEP.op_begin()+1);
00094   }
00095   inline gep_type_iterator gep_type_end(const User &GEP) {
00096     return gep_type_iterator::end(GEP.op_end());
00097   }
00098 
00099   template<typename ItTy>
00100   inline generic_gep_type_iterator<ItTy>
00101   gep_type_begin(const Type *Op0, ItTy I, ItTy E) {
00102     return generic_gep_type_iterator<ItTy>::begin(Op0, I);
00103   }
00104 
00105   template<typename ItTy>
00106   inline generic_gep_type_iterator<ItTy>
00107   gep_type_end(const Type *Op0, ItTy I, ItTy E) {
00108     return generic_gep_type_iterator<ItTy>::end(E);
00109   }
00110 } // end namespace llvm
00111 
00112 #endif