Move EVER MORE stuff over to LLVMContext.
[oota-llvm.git] / include / llvm / Support / GetElementPtrTypeIterator.h
index 95f02a8b6a3b4af5a19d7872565a0afefff66a3c..e1cda75c5f6a101c05d6f59e02b0e093ba61ee6e 100644 (file)
@@ -1,10 +1,10 @@
 //===- llvm/Support/GetElementPtrTypeIterator.h -----------------*- C++ -*-===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
 //===----------------------------------------------------------------------===//
 //
 // This file implements an iterator for walking through the types indexed by
 #ifndef LLVM_SUPPORT_GETELEMENTPTRTYPE_H
 #define LLVM_SUPPORT_GETELEMENTPTRTYPE_H
 
-#include "Support/iterator"
-#include "llvm/iMemory.h"
+#include "llvm/User.h"
 #include "llvm/DerivedTypes.h"
 
 namespace llvm {
-  class gep_type_iterator
+  template<typename ItTy = User::const_op_iterator>
+  class generic_gep_type_iterator
     : public forward_iterator<const Type *, ptrdiff_t> {
     typedef forward_iterator<const Type*, ptrdiff_t> super;
 
-    GetElementPtrInst *TheGEP;
+    ItTy OpIt;
     const Type *CurTy;
-    unsigned Operand;
-    
-    gep_type_iterator() {}
+    generic_gep_type_iterator() {}
   public:
 
-    static gep_type_iterator begin(GetElementPtrInst *gep) {
-      gep_type_iterator I;
-      I.TheGEP = gep;
-      I.CurTy = gep->getOperand(0)->getType();
-      I.Operand = 1;
+    static generic_gep_type_iterator begin(const Type *Ty, ItTy It) {
+      generic_gep_type_iterator I;
+      I.CurTy = Ty;
+      I.OpIt = It;
       return I;
     }
-    static gep_type_iterator end(GetElementPtrInst *gep) {
-      gep_type_iterator I;
-      I.TheGEP = gep;
+    static generic_gep_type_iterator end(ItTy It) {
+      generic_gep_type_iterator I;
       I.CurTy = 0;
-      I.Operand = gep->getNumOperands();
+      I.OpIt = It;
       return I;
     }
 
-    bool operator==(const gep_type_iterator& x) const { 
-      return Operand == x.Operand;
+    bool operator==(const generic_gep_type_iterator& x) const {
+      return OpIt == x.OpIt;
     }
-    bool operator!=(const gep_type_iterator& x) const {
+    bool operator!=(const generic_gep_type_iterator& x) const {
       return !operator==(x);
     }
 
-    const Type *operator*() const { 
+    const Type *operator*() const {
       return CurTy;
     }
 
+    const Type *getIndexedType() const {
+      const CompositeType *CT = cast<CompositeType>(CurTy);
+      return CT->getTypeAtIndex(getOperand());
+    }
+
     // This is a non-standard operator->.  It allows you to call methods on the
     // current type directly.
     const Type *operator->() const { return operator*(); }
-    
-    unsigned getOperandNum() const { return Operand; }
 
-    Value *getOperand() const { return TheGEP->getOperand(Operand); }
+    Value *getOperand() const { return *OpIt; }
 
-    gep_type_iterator& operator++() {   // Preincrement
+    generic_gep_type_iterator& operator++() {   // Preincrement
       if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
         CurTy = CT->getTypeAtIndex(getOperand());
       } else {
         CurTy = 0;
       }
-      ++Operand;
-      return *this; 
+      ++OpIt;
+      return *this;
     }
 
-    gep_type_iterator operator++(int) { // Postincrement
-      gep_type_iterator tmp = *this; ++*this; return tmp; 
+    generic_gep_type_iterator operator++(int) { // Postincrement
+      generic_gep_type_iterator tmp = *this; ++*this; return tmp;
     }
   };
 
-  inline gep_type_iterator gep_type_begin(GetElementPtrInst *GEP) {
-    return gep_type_iterator::begin(GEP);
+  typedef generic_gep_type_iterator<> gep_type_iterator;
+
+  inline gep_type_iterator gep_type_begin(const User *GEP) {
+    return gep_type_iterator::begin(GEP->getOperand(0)->getType(),
+                                      GEP->op_begin()+1);
+  }
+  inline gep_type_iterator gep_type_end(const User *GEP) {
+    return gep_type_iterator::end(GEP->op_end());
+  }
+  inline gep_type_iterator gep_type_begin(const User &GEP) {
+    return gep_type_iterator::begin(GEP.getOperand(0)->getType(),
+                                    GEP.op_begin()+1);
+  }
+  inline gep_type_iterator gep_type_end(const User &GEP) {
+    return gep_type_iterator::end(GEP.op_end());
+  }
+
+  template<typename ItTy>
+  inline generic_gep_type_iterator<ItTy>
+  gep_type_begin(const Type *Op0, ItTy I, ItTy E) {
+    return generic_gep_type_iterator<ItTy>::begin(Op0, I);
   }
 
-  inline gep_type_iterator gep_type_end(GetElementPtrInst *GEP) {
-    return gep_type_iterator::end(GEP);
+  template<typename ItTy>
+  inline generic_gep_type_iterator<ItTy>
+  gep_type_end(const Type *Op0, ItTy I, ItTy E) {
+    return generic_gep_type_iterator<ItTy>::end(E);
   }
 } // end namespace llvm