Added LLVM copyright header (for lack of a better term).
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 Bytecode & 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 "Support/iterator"
22 class Constant;
23
24 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
25   const_inst_iterator InstI;                // Method instruction iterator
26   unsigned OpIdx;                           // Operand index
27
28   typedef constant_iterator _Self;
29
30   inline bool isAtConstant() const {
31     assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
32            "isAtConstant called with invalid arguments!");
33     return isa<Constant>(InstI->getOperand(OpIdx));
34   }
35
36 public:
37   inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
38     // Advance to first constant... if we are not already at constant or end
39     if (InstI != inst_end(F) &&                            // InstI is valid?
40         (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
41       operator++();
42   }
43
44   inline constant_iterator(const Function *F, bool)   // end ctor
45     : InstI(inst_end(F)), OpIdx(0) {
46   }
47
48   inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx && 
49                                                         InstI == x.InstI; }
50   inline bool operator!=(const _Self& x) const { return !operator==(x); }
51
52   inline pointer operator*() const {
53     assert(isAtConstant() && "Dereferenced an iterator at the end!");
54     return cast<Constant>(InstI->getOperand(OpIdx));
55   }
56   inline pointer operator->() const { return operator*(); }
57
58   inline _Self& operator++() {   // Preincrement implementation
59     ++OpIdx;
60     do {
61       unsigned NumOperands = InstI->getNumOperands();
62       while (OpIdx < NumOperands && !isAtConstant()) {
63         ++OpIdx;
64       }
65
66       if (OpIdx < NumOperands) return *this;  // Found a constant!
67       ++InstI;
68       OpIdx = 0;
69     } while (!InstI.atEnd());
70
71     return *this;  // At the end of the method
72   }
73
74   inline _Self operator++(int) { // Postincrement
75     _Self tmp = *this; ++*this; return tmp; 
76   }
77
78   inline bool atEnd() const { return InstI.atEnd(); }
79 };
80
81 inline constant_iterator constant_begin(const Function *F) {
82   return constant_iterator(F);
83 }
84
85 inline constant_iterator constant_end(const Function *F) {
86   return constant_iterator(F, true);
87 }
88
89 #endif