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