Included assert.h so that the code compiles under newer versions of GCC.
[oota-llvm.git] / include / llvm / Analysis / ConstantsScanner.h
1 //==-- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-==//
2 //
3 // This class implements an iterator to walk through the constants referenced by
4 // a method.  This is used by the Bytecode & Assembly writers to build constant
5 // pools.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
10 #define LLVM_ANALYSIS_CONSTANTSSCANNER_H
11
12 #include <assert.h>
13
14 #include "llvm/Support/InstIterator.h"
15 #include "llvm/Instruction.h"
16 #include "Support/iterator"
17 class Constant;
18
19 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
20   const_inst_iterator InstI;                // Method instruction iterator
21   unsigned OpIdx;                           // Operand index
22
23   typedef constant_iterator _Self;
24
25   inline bool isAtConstant() const {
26     assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
27            "isAtConstant called with invalid arguments!");
28     return isa<Constant>(InstI->getOperand(OpIdx));
29   }
30
31 public:
32   inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
33     // Advance to first constant... if we are not already at constant or end
34     if (InstI != inst_end(F) &&                            // InstI is valid?
35         (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
36       operator++();
37   }
38
39   inline constant_iterator(const Function *F, bool)   // end ctor
40     : InstI(inst_end(F)), OpIdx(0) {
41   }
42
43   inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx && 
44                                                         InstI == x.InstI; }
45   inline bool operator!=(const _Self& x) const { return !operator==(x); }
46
47   inline pointer operator*() const {
48     assert(isAtConstant() && "Dereferenced an iterator at the end!");
49     return cast<Constant>(InstI->getOperand(OpIdx));
50   }
51   inline pointer operator->() const { return operator*(); }
52
53   inline _Self& operator++() {   // Preincrement implementation
54     ++OpIdx;
55     do {
56       unsigned NumOperands = InstI->getNumOperands();
57       while (OpIdx < NumOperands && !isAtConstant()) {
58         ++OpIdx;
59       }
60
61       if (OpIdx < NumOperands) return *this;  // Found a constant!
62       ++InstI;
63       OpIdx = 0;
64     } while (!InstI.atEnd());
65
66     return *this;  // At the end of the method
67   }
68
69   inline _Self operator++(int) { // Postincrement
70     _Self tmp = *this; ++*this; return tmp; 
71   }
72
73   inline bool atEnd() const { return InstI.atEnd(); }
74 };
75
76 inline constant_iterator constant_begin(const Function *F) {
77   return constant_iterator(F);
78 }
79
80 inline constant_iterator constant_end(const Function *F) {
81   return constant_iterator(F, true);
82 }
83
84 #endif