Moved removeFile() and getUniqueFilename() into FileUtilities.
[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 "llvm/Support/InstIterator.h"
13 #include "llvm/Instruction.h"
14 #include "Support/iterator"
15 class Constant;
16
17 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
18   const_inst_iterator InstI;                // Method instruction iterator
19   unsigned OpIdx;                           // Operand index
20
21   typedef constant_iterator _Self;
22
23   inline bool isAtConstant() const {
24     assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
25            "isAtConstant called with invalid arguments!");
26     return isa<Constant>(InstI->getOperand(OpIdx));
27   }
28
29 public:
30   inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
31     // Advance to first constant... if we are not already at constant or end
32     if (InstI != inst_end(F) &&                            // InstI is valid?
33         (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
34       operator++();
35   }
36
37   inline constant_iterator(const Function *F, bool)   // end ctor
38     : InstI(inst_end(F)), OpIdx(0) {
39   }
40
41   inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx && 
42                                                         InstI == x.InstI; }
43   inline bool operator!=(const _Self& x) const { return !operator==(x); }
44
45   inline pointer operator*() const {
46     assert(isAtConstant() && "Dereferenced an iterator at the end!");
47     return cast<Constant>(InstI->getOperand(OpIdx));
48   }
49   inline pointer operator->() const { return operator*(); }
50
51   inline _Self& operator++() {   // Preincrement implementation
52     ++OpIdx;
53     do {
54       unsigned NumOperands = InstI->getNumOperands();
55       while (OpIdx < NumOperands && !isAtConstant()) {
56         ++OpIdx;
57       }
58
59       if (OpIdx < NumOperands) return *this;  // Found a constant!
60       ++InstI;
61       OpIdx = 0;
62     } while (!InstI.atEnd());
63
64     return *this;  // At the end of the method
65   }
66
67   inline _Self operator++(int) { // Postincrement
68     _Self tmp = *this; ++*this; return tmp; 
69   }
70
71   inline bool atEnd() const { return InstI.atEnd(); }
72 };
73
74 inline constant_iterator constant_begin(const Function *F) {
75   return constant_iterator(F);
76 }
77
78 inline constant_iterator constant_end(const Function *F) {
79   return constant_iterator(F, true);
80 }
81
82 #endif