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