Revert r240137 (Fixed/added namespace ending comments using clang-tidy. NFC)
[oota-llvm.git] / include / llvm / IR / InstIterator.h
1 //===- InstIterator.h - Classes for inst iteration --------------*- 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 file contains definitions of two iterators for iterating over the
11 // instructions in a function.  This is effectively a wrapper around a two level
12 // iterator that can probably be genericized later.
13 //
14 // Note that this iterator gets invalidated any time that basic blocks or
15 // instructions are moved around.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_IR_INSTITERATOR_H
20 #define LLVM_IR_INSTITERATOR_H
21
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
24
25 namespace llvm {
26
27 // This class implements inst_begin() & inst_end() for
28 // inst_iterator and const_inst_iterator's.
29 //
30 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
31   typedef BB_t BBty;
32   typedef BB_i_t BBIty;
33   typedef BI_t BIty;
34   typedef II_t IIty;
35   BB_t *BBs; // BasicBlocksType
36   BB_i_t BB; // BasicBlocksType::iterator
37   BI_t BI;   // BasicBlock::iterator
38 public:
39   typedef std::bidirectional_iterator_tag iterator_category;
40   typedef IIty                            value_type;
41   typedef signed                        difference_type;
42   typedef IIty*                           pointer;
43   typedef IIty&                           reference;
44
45   // Default constructor
46   InstIterator() {}
47
48   // Copy constructor...
49   template<typename A, typename B, typename C, typename D>
50   InstIterator(const InstIterator<A,B,C,D> &II)
51     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
52
53   template<typename A, typename B, typename C, typename D>
54   InstIterator(InstIterator<A,B,C,D> &II)
55     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
56
57   template<class M> InstIterator(M &m)
58     : BBs(&m.getBasicBlockList()), BB(BBs->begin()) {    // begin ctor
59     if (BB != BBs->end()) {
60       BI = BB->begin();
61       advanceToNextBB();
62     }
63   }
64
65   template<class M> InstIterator(M &m, bool)
66     : BBs(&m.getBasicBlockList()), BB(BBs->end()) {    // end ctor
67   }
68
69   // Accessors to get at the underlying iterators...
70   inline BBIty &getBasicBlockIterator()  { return BB; }
71   inline BIty  &getInstructionIterator() { return BI; }
72
73   inline reference operator*()  const { return *BI; }
74   inline pointer operator->() const { return &operator*(); }
75
76   inline bool operator==(const InstIterator &y) const {
77     return BB == y.BB && (BB == BBs->end() || BI == y.BI);
78   }
79   inline bool operator!=(const InstIterator& y) const {
80     return !operator==(y);
81   }
82
83   InstIterator& operator++() {
84     ++BI;
85     advanceToNextBB();
86     return *this;
87   }
88   inline InstIterator operator++(int) {
89     InstIterator tmp = *this; ++*this; return tmp;
90   }
91
92   InstIterator& operator--() {
93     while (BB == BBs->end() || BI == BB->begin()) {
94       --BB;
95       BI = BB->end();
96     }
97     --BI;
98     return *this;
99   }
100   inline InstIterator  operator--(int) {
101     InstIterator tmp = *this; --*this; return tmp;
102   }
103
104   inline bool atEnd() const { return BB == BBs->end(); }
105
106 private:
107   inline void advanceToNextBB() {
108     // The only way that the II could be broken is if it is now pointing to
109     // the end() of the current BasicBlock and there are successor BBs.
110     while (BI == BB->end()) {
111       ++BB;
112       if (BB == BBs->end()) break;
113       BI = BB->begin();
114     }
115   }
116 };
117
118
119 typedef InstIterator<iplist<BasicBlock>,
120                      Function::iterator, BasicBlock::iterator,
121                      Instruction> inst_iterator;
122 typedef InstIterator<const iplist<BasicBlock>,
123                      Function::const_iterator,
124                      BasicBlock::const_iterator,
125                      const Instruction> const_inst_iterator;
126
127 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
128 inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
129 inline iterator_range<inst_iterator> inst_range(Function *F) {
130   return iterator_range<inst_iterator>(inst_begin(F), inst_end(F));
131 }
132 inline const_inst_iterator inst_begin(const Function *F) {
133   return const_inst_iterator(*F);
134 }
135 inline const_inst_iterator inst_end(const Function *F) {
136   return const_inst_iterator(*F, true);
137 }
138 inline iterator_range<const_inst_iterator> inst_range(const Function *F) {
139   return iterator_range<const_inst_iterator>(inst_begin(F), inst_end(F));
140 }
141 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
142 inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
143 inline iterator_range<inst_iterator> inst_range(Function &F) {
144   return iterator_range<inst_iterator>(inst_begin(F), inst_end(F));
145 }
146 inline const_inst_iterator inst_begin(const Function &F) {
147   return const_inst_iterator(F);
148 }
149 inline const_inst_iterator inst_end(const Function &F) {
150   return const_inst_iterator(F, true);
151 }
152 inline iterator_range<const_inst_iterator> inst_range(const Function &F) {
153   return iterator_range<const_inst_iterator>(inst_begin(F), inst_end(F));
154 }
155
156 } // End llvm namespace
157
158 #endif