LLVM API Documentation
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 // This is a non-standard operator->. It allows you to call methods on the 00057 // current type directly. 00058 const Type *operator->() const { return operator*(); } 00059 00060 Value *getOperand() const { return *OpIt; } 00061 00062 generic_gep_type_iterator& operator++() { // Preincrement 00063 if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) { 00064 CurTy = CT->getTypeAtIndex(getOperand()); 00065 } else { 00066 CurTy = 0; 00067 } 00068 ++OpIt; 00069 return *this; 00070 } 00071 00072 generic_gep_type_iterator operator++(int) { // Postincrement 00073 generic_gep_type_iterator tmp = *this; ++*this; return tmp; 00074 } 00075 }; 00076 00077 typedef generic_gep_type_iterator<> gep_type_iterator; 00078 00079 inline gep_type_iterator gep_type_begin(const User *GEP) { 00080 return gep_type_iterator::begin(GEP->getOperand(0)->getType(), 00081 GEP->op_begin()+1); 00082 } 00083 inline gep_type_iterator gep_type_end(const User *GEP) { 00084 return gep_type_iterator::end(GEP->op_end()); 00085 } 00086 inline gep_type_iterator gep_type_begin(const User &GEP) { 00087 return gep_type_iterator::begin(GEP.getOperand(0)->getType(), 00088 GEP.op_begin()+1); 00089 } 00090 inline gep_type_iterator gep_type_end(const User &GEP) { 00091 return gep_type_iterator::end(GEP.op_end()); 00092 } 00093 00094 template<typename ItTy> 00095 inline generic_gep_type_iterator<ItTy> 00096 gep_type_begin(const Type *Op0, ItTy I, ItTy E) { 00097 return generic_gep_type_iterator<ItTy>::begin(Op0, I); 00098 } 00099 00100 template<typename ItTy> 00101 inline generic_gep_type_iterator<ItTy> 00102 gep_type_end(const Type *Op0, ItTy I, ItTy E) { 00103 return generic_gep_type_iterator<ItTy>::end(E); 00104 } 00105 } // end namespace llvm 00106 00107 #endif