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