clang  3.7.0
StmtIterator.h
Go to the documentation of this file.
1 //===--- StmtIterator.h - Iterators for Statements --------------*- 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 defines the StmtIterator and ConstStmtIterator classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_STMTITERATOR_H
15 #define LLVM_CLANG_AST_STMTITERATOR_H
16 
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
19 #include <cassert>
20 #include <cstddef>
21 #include <iterator>
22 #include <utility>
23 
24 namespace clang {
25 
26 class Stmt;
27 class Decl;
28 class VariableArrayType;
29 
31 protected:
32  enum { StmtMode = 0x0, SizeOfTypeVAMode = 0x1, DeclGroupMode = 0x2,
33  Flags = 0x3 };
34 
35  union {
37  Decl **DGI;
38  };
39  uintptr_t RawVAPtr;
40  Decl **DGE;
41 
42  bool inDeclGroup() const {
43  return (RawVAPtr & Flags) == DeclGroupMode;
44  }
45 
46  bool inSizeOfTypeVA() const {
47  return (RawVAPtr & Flags) == SizeOfTypeVAMode;
48  }
49 
50  bool inStmt() const {
51  return (RawVAPtr & Flags) == StmtMode;
52  }
53 
54  const VariableArrayType *getVAPtr() const {
55  return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags);
56  }
57 
58  void setVAPtr(const VariableArrayType *P) {
59  assert (inDeclGroup() || inSizeOfTypeVA());
60  RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags);
61  }
62 
63  void NextDecl(bool ImmediateAdvance = true);
64  bool HandleDecl(Decl* D);
65  void NextVA();
66 
67  Stmt*& GetDeclExpr() const;
68 
71  StmtIteratorBase(Decl **dgi, Decl **dge);
72  StmtIteratorBase() : stmt(nullptr), RawVAPtr(0) {}
73 };
74 
75 
76 template <typename DERIVED, typename REFERENCE>
78  public std::iterator<std::forward_iterator_tag,
79  REFERENCE, ptrdiff_t,
80  REFERENCE, REFERENCE> {
81 protected:
83 public:
86  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
88 
89  DERIVED& operator++() {
90  if (inStmt())
91  ++stmt;
92  else if (getVAPtr())
93  NextVA();
94  else
95  NextDecl();
96 
97  return static_cast<DERIVED&>(*this);
98  }
99 
100  DERIVED operator++(int) {
101  DERIVED tmp = static_cast<DERIVED&>(*this);
102  operator++();
103  return tmp;
104  }
105 
106  bool operator==(const DERIVED& RHS) const {
107  return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr;
108  }
109 
110  bool operator!=(const DERIVED& RHS) const {
111  return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr;
112  }
113 
114  REFERENCE operator*() const {
115  return inStmt() ? *stmt : GetDeclExpr();
116  }
117 
118  REFERENCE operator->() const { return operator*(); }
119 };
120 
121 struct StmtIterator : public StmtIteratorImpl<StmtIterator,Stmt*&> {
123 
125 
126  StmtIterator(Decl** dgi, Decl** dge)
127  : StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {}
128 
131 };
132 
133 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
134  const Stmt*> {
135  explicit ConstStmtIterator() :
137 
139  StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {}
140 };
141 
142 /// A range of statement iterators.
143 ///
144 /// This class provides some extra functionality beyond std::pair
145 /// in order to allow the following idiom:
146 /// for (StmtRange range = stmt->children(); range; ++range)
147 struct StmtRange : std::pair<StmtIterator,StmtIterator> {
150  : std::pair<StmtIterator,StmtIterator>(begin, end) {}
151 
152  bool empty() const { return first == second; }
153  explicit operator bool() const { return !empty(); }
154 
155  Stmt *operator->() const { return first.operator->(); }
156  Stmt *&operator*() const { return first.operator*(); }
157 
159  assert(!empty() && "incrementing on empty range");
160  ++first;
161  return *this;
162  }
163 
165  assert(!empty() && "incrementing on empty range");
166  StmtRange copy = *this;
167  ++first;
168  return copy;
169  }
170 
171  friend const StmtIterator &begin(const StmtRange &range) {
172  return range.first;
173  }
174  friend const StmtIterator &end(const StmtRange &range) {
175  return range.second;
176  }
177 };
178 
179 /// A range of const statement iterators.
180 ///
181 /// This class provides some extra functionality beyond std::pair
182 /// in order to allow the following idiom:
183 /// for (ConstStmtRange range = stmt->children(); range; ++range)
184 struct ConstStmtRange : std::pair<ConstStmtIterator,ConstStmtIterator> {
187  const ConstStmtIterator &end)
188  : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {}
189  ConstStmtRange(const StmtRange &range)
190  : std::pair<ConstStmtIterator,ConstStmtIterator>(range.first, range.second)
191  {}
193  : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {}
194 
195  bool empty() const { return first == second; }
196  explicit operator bool() const { return !empty(); }
197 
198  const Stmt *operator->() const { return first.operator->(); }
199  const Stmt *operator*() const { return first.operator*(); }
200 
202  assert(!empty() && "incrementing on empty range");
203  ++first;
204  return *this;
205  }
206 
208  assert(!empty() && "incrementing on empty range");
209  ConstStmtRange copy = *this;
210  ++first;
211  return copy;
212  }
213 
214  friend const ConstStmtIterator &begin(const ConstStmtRange &range) {
215  return range.first;
216  }
217  friend const ConstStmtIterator &end(const ConstStmtRange &range) {
218  return range.second;
219  }
220 };
221 
222 } // end namespace clang
223 
224 #endif
bool empty() const
Definition: StmtIterator.h:195
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:138
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:46
Stmt * operator->() const
Definition: StmtIterator.h:155
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:87
const Stmt * operator->() const
Definition: StmtIterator.h:198
friend const StmtIterator & begin(const StmtRange &range)
Definition: StmtIterator.h:171
const Stmt * operator*() const
Definition: StmtIterator.h:199
Stmt *& operator*() const
Definition: StmtIterator.h:156
void NextDecl(bool ImmediateAdvance=true)
friend const StmtIterator & end(const StmtRange &range)
Definition: StmtIterator.h:174
ConstStmtRange & operator++()
Definition: StmtIterator.h:201
AnnotatingParser & P
StmtRange & operator++()
Definition: StmtIterator.h:158
StmtRange operator++(int)
Definition: StmtIterator.h:164
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:82
REFERENCE operator->() const
Definition: StmtIterator.h:118
#define bool
Definition: stdbool.h:31
ConstStmtRange(const StmtRange &range)
Definition: StmtIterator.h:189
bool inDeclGroup() const
Definition: StmtIterator.h:42
bool operator==(const DERIVED &RHS) const
Definition: StmtIterator.h:106
StmtIterator(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:126
bool empty() const
Definition: StmtIterator.h:152
Stmt *& GetDeclExpr() const
bool inStmt() const
Definition: StmtIterator.h:50
ConstStmtRange(const StmtIterator &begin, const StmtIterator &end)
Definition: StmtIterator.h:192
bool HandleDecl(Decl *D)
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:129
REFERENCE operator*() const
Definition: StmtIterator.h:114
friend const ConstStmtIterator & begin(const ConstStmtRange &range)
Definition: StmtIterator.h:214
friend const ConstStmtIterator & end(const ConstStmtRange &range)
Definition: StmtIterator.h:217
ConstStmtRange(const ConstStmtIterator &begin, const ConstStmtIterator &end)
Definition: StmtIterator.h:186
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:86
bool operator!=(const DERIVED &RHS) const
Definition: StmtIterator.h:110
const VariableArrayType * getVAPtr() const
Definition: StmtIterator.h:54
StmtRange(const StmtIterator &begin, const StmtIterator &end)
Definition: StmtIterator.h:149
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:58
StmtIterator(Stmt **S)
Definition: StmtIterator.h:124
ConstStmtRange operator++(int)
Definition: StmtIterator.h:207