summaryrefslogtreecommitdiff
path: root/include/llvm/Support/GetElementPtrTypeIterator.h
blob: 95f02a8b6a3b4af5a19d7872565a0afefff66a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===- 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 implements an iterator for walking through the types indexed by
// getelementptr instructions.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_GETELEMENTPTRTYPE_H
#define LLVM_SUPPORT_GETELEMENTPTRTYPE_H

#include "Support/iterator"
#include "llvm/iMemory.h"
#include "llvm/DerivedTypes.h"

namespace llvm {
  class gep_type_iterator
    : public forward_iterator<const Type *, ptrdiff_t> {
    typedef forward_iterator<const Type*, ptrdiff_t> super;

    GetElementPtrInst *TheGEP;
    const Type *CurTy;
    unsigned Operand;
    
    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;
      return I;
    }
    static gep_type_iterator end(GetElementPtrInst *gep) {
      gep_type_iterator I;
      I.TheGEP = gep;
      I.CurTy = 0;
      I.Operand = gep->getNumOperands();
      return I;
    }

    bool operator==(const gep_type_iterator& x) const { 
      return Operand == x.Operand;
    }
    bool operator!=(const gep_type_iterator& x) const {
      return !operator==(x);
    }

    const Type *operator*() const { 
      return CurTy;
    }

    // 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); }

    gep_type_iterator& operator++() {   // Preincrement
      if (const CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
        CurTy = CT->getTypeAtIndex(getOperand());
      } else {
        CurTy = 0;
      }
      ++Operand;
      return *this; 
    }

    gep_type_iterator operator++(int) { // Postincrement
      gep_type_iterator tmp = *this; ++*this; return tmp; 
    }
  };

  inline gep_type_iterator gep_type_begin(GetElementPtrInst *GEP) {
    return gep_type_iterator::begin(GEP);
  }

  inline gep_type_iterator gep_type_end(GetElementPtrInst *GEP) {
    return gep_type_iterator::end(GEP);
  }
} // end namespace llvm

#endif