clang API Documentation
00001 //===--- StmtIterator.h - Iterators for Statements --------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the StmtIterator and ConstStmtIterator classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_STMT_ITR_H 00015 #define LLVM_CLANG_AST_STMT_ITR_H 00016 00017 #include "llvm/Support/DataTypes.h" 00018 #include <cassert> 00019 #include <cstddef> 00020 #include <iterator> 00021 #include <utility> 00022 00023 namespace clang { 00024 00025 class Stmt; 00026 class Decl; 00027 class VariableArrayType; 00028 00029 class StmtIteratorBase { 00030 protected: 00031 enum { DeclMode = 0x1, SizeOfTypeVAMode = 0x2, DeclGroupMode = 0x3, 00032 Flags = 0x3 }; 00033 00034 Stmt **stmt; 00035 union { Decl *decl; Decl **DGI; }; 00036 uintptr_t RawVAPtr; 00037 Decl **DGE; 00038 00039 bool inDecl() const { 00040 return (RawVAPtr & Flags) == DeclMode; 00041 } 00042 00043 bool inDeclGroup() const { 00044 return (RawVAPtr & Flags) == DeclGroupMode; 00045 } 00046 00047 bool inSizeOfTypeVA() const { 00048 return (RawVAPtr & Flags) == SizeOfTypeVAMode; 00049 } 00050 00051 bool inStmt() const { 00052 return (RawVAPtr & Flags) == 0; 00053 } 00054 00055 const VariableArrayType *getVAPtr() const { 00056 return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags); 00057 } 00058 00059 void setVAPtr(const VariableArrayType *P) { 00060 assert (inDecl() || inDeclGroup() || inSizeOfTypeVA()); 00061 RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags); 00062 } 00063 00064 void NextDecl(bool ImmediateAdvance = true); 00065 bool HandleDecl(Decl* D); 00066 void NextVA(); 00067 00068 Stmt*& GetDeclExpr() const; 00069 00070 StmtIteratorBase(Stmt **s) : stmt(s), decl(0), RawVAPtr(0) {} 00071 StmtIteratorBase(Decl *d, Stmt **s); 00072 StmtIteratorBase(const VariableArrayType *t); 00073 StmtIteratorBase(Decl **dgi, Decl **dge); 00074 StmtIteratorBase() : stmt(0), decl(0), RawVAPtr(0) {} 00075 }; 00076 00077 00078 template <typename DERIVED, typename REFERENCE> 00079 class StmtIteratorImpl : public StmtIteratorBase, 00080 public std::iterator<std::forward_iterator_tag, 00081 REFERENCE, ptrdiff_t, 00082 REFERENCE, REFERENCE> { 00083 protected: 00084 StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {} 00085 public: 00086 StmtIteratorImpl() {} 00087 StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {} 00088 StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {} 00089 StmtIteratorImpl(Decl *d, Stmt **s) : StmtIteratorBase(d, s) {} 00090 StmtIteratorImpl(const VariableArrayType *t) : StmtIteratorBase(t) {} 00091 00092 DERIVED& operator++() { 00093 if (inStmt()) 00094 ++stmt; 00095 else if (getVAPtr()) 00096 NextVA(); 00097 else 00098 NextDecl(); 00099 00100 return static_cast<DERIVED&>(*this); 00101 } 00102 00103 DERIVED operator++(int) { 00104 DERIVED tmp = static_cast<DERIVED&>(*this); 00105 operator++(); 00106 return tmp; 00107 } 00108 00109 bool operator==(const DERIVED& RHS) const { 00110 return stmt == RHS.stmt && decl == RHS.decl && RawVAPtr == RHS.RawVAPtr; 00111 } 00112 00113 bool operator!=(const DERIVED& RHS) const { 00114 return stmt != RHS.stmt || decl != RHS.decl || RawVAPtr != RHS.RawVAPtr; 00115 } 00116 00117 REFERENCE operator*() const { 00118 return (REFERENCE) (inStmt() ? *stmt : GetDeclExpr()); 00119 } 00120 00121 REFERENCE operator->() const { return operator*(); } 00122 }; 00123 00124 struct StmtIterator : public StmtIteratorImpl<StmtIterator,Stmt*&> { 00125 explicit StmtIterator() : StmtIteratorImpl<StmtIterator,Stmt*&>() {} 00126 00127 StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*&>(S) {} 00128 00129 StmtIterator(Decl** dgi, Decl** dge) 00130 : StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {} 00131 00132 StmtIterator(const VariableArrayType *t) 00133 : StmtIteratorImpl<StmtIterator,Stmt*&>(t) {} 00134 00135 StmtIterator(Decl* D, Stmt **s = 0) 00136 : StmtIteratorImpl<StmtIterator,Stmt*&>(D, s) {} 00137 }; 00138 00139 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator, 00140 const Stmt*> { 00141 explicit ConstStmtIterator() : 00142 StmtIteratorImpl<ConstStmtIterator,const Stmt*>() {} 00143 00144 ConstStmtIterator(const StmtIterator& RHS) : 00145 StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {} 00146 }; 00147 00148 /// A range of statement iterators. 00149 /// 00150 /// This class provides some extra functionality beyond std::pair 00151 /// in order to allow the following idiom: 00152 /// for (StmtRange range = stmt->children(); range; ++range) 00153 struct StmtRange : std::pair<StmtIterator,StmtIterator> { 00154 StmtRange() {} 00155 StmtRange(const StmtIterator &begin, const StmtIterator &end) 00156 : std::pair<StmtIterator,StmtIterator>(begin, end) {} 00157 00158 bool empty() const { return first == second; } 00159 operator bool() const { return !empty(); } 00160 00161 Stmt *operator->() const { return first.operator->(); } 00162 Stmt *&operator*() const { return first.operator*(); } 00163 00164 StmtRange &operator++() { 00165 assert(!empty() && "incrementing on empty range"); 00166 ++first; 00167 return *this; 00168 } 00169 00170 StmtRange operator++(int) { 00171 assert(!empty() && "incrementing on empty range"); 00172 StmtRange copy = *this; 00173 ++first; 00174 return copy; 00175 } 00176 00177 friend const StmtIterator &begin(const StmtRange &range) { 00178 return range.first; 00179 } 00180 friend const StmtIterator &end(const StmtRange &range) { 00181 return range.second; 00182 } 00183 }; 00184 00185 /// A range of const statement iterators. 00186 /// 00187 /// This class provides some extra functionality beyond std::pair 00188 /// in order to allow the following idiom: 00189 /// for (ConstStmtRange range = stmt->children(); range; ++range) 00190 struct ConstStmtRange : std::pair<ConstStmtIterator,ConstStmtIterator> { 00191 ConstStmtRange() {} 00192 ConstStmtRange(const ConstStmtIterator &begin, 00193 const ConstStmtIterator &end) 00194 : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {} 00195 ConstStmtRange(const StmtRange &range) 00196 : std::pair<ConstStmtIterator,ConstStmtIterator>(range.first, range.second) 00197 {} 00198 ConstStmtRange(const StmtIterator &begin, const StmtIterator &end) 00199 : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {} 00200 00201 bool empty() const { return first == second; } 00202 operator bool() const { return !empty(); } 00203 00204 const Stmt *operator->() const { return first.operator->(); } 00205 const Stmt *operator*() const { return first.operator*(); } 00206 00207 ConstStmtRange &operator++() { 00208 assert(!empty() && "incrementing on empty range"); 00209 ++first; 00210 return *this; 00211 } 00212 00213 ConstStmtRange operator++(int) { 00214 assert(!empty() && "incrementing on empty range"); 00215 ConstStmtRange copy = *this; 00216 ++first; 00217 return copy; 00218 } 00219 00220 friend const ConstStmtIterator &begin(const ConstStmtRange &range) { 00221 return range.first; 00222 } 00223 friend const ConstStmtIterator &end(const ConstStmtRange &range) { 00224 return range.second; 00225 } 00226 }; 00227 00228 } // end namespace clang 00229 00230 #endif