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