//===- StmtIterator.h - Iterators for Statements ----------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the StmtIterator and ConstStmtIterator classes. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_AST_STMTITERATOR_H #define LLVM_CLANG_AST_STMTITERATOR_H #include #include #include #include namespace clang { class Decl; class Stmt; class VariableArrayType; class StmtIteratorBase { protected: enum { StmtMode = 0x0, SizeOfTypeVAMode = 0x1, DeclGroupMode = 0x2, Flags = 0x3 }; union { Stmt **stmt; Decl **DGI; }; uintptr_t RawVAPtr = 0; Decl **DGE; StmtIteratorBase(Stmt **s) : stmt(s) {} StmtIteratorBase(const VariableArrayType *t); StmtIteratorBase(Decl **dgi, Decl **dge); StmtIteratorBase() : stmt(nullptr) {} bool inDeclGroup() const { return (RawVAPtr & Flags) == DeclGroupMode; } bool inSizeOfTypeVA() const { return (RawVAPtr & Flags) == SizeOfTypeVAMode; } bool inStmt() const { return (RawVAPtr & Flags) == StmtMode; } const VariableArrayType *getVAPtr() const { return reinterpret_cast(RawVAPtr & ~Flags); } void setVAPtr(const VariableArrayType *P) { assert(inDeclGroup() || inSizeOfTypeVA()); RawVAPtr = reinterpret_cast(P) | (RawVAPtr & Flags); } void NextDecl(bool ImmediateAdvance = true); bool HandleDecl(Decl* D); void NextVA(); Stmt*& GetDeclExpr() const; }; template class StmtIteratorImpl : public StmtIteratorBase, public std::iterator { protected: StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {} public: StmtIteratorImpl() = default; StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {} StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {} StmtIteratorImpl(const VariableArrayType *t) : StmtIteratorBase(t) {} DERIVED& operator++() { if (inStmt()) ++stmt; else if (getVAPtr()) NextVA(); else NextDecl(); return static_cast(*this); } DERIVED operator++(int) { DERIVED tmp = static_cast(*this); operator++(); return tmp; } bool operator==(const DERIVED& RHS) const { return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr; } bool operator!=(const DERIVED& RHS) const { return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr; } REFERENCE operator*() const { return inStmt() ? *stmt : GetDeclExpr(); } REFERENCE operator->() const { return operator*(); } }; struct ConstStmtIterator; struct StmtIterator : public StmtIteratorImpl { explicit StmtIterator() = default; StmtIterator(Stmt** S) : StmtIteratorImpl(S) {} StmtIterator(Decl** dgi, Decl** dge) : StmtIteratorImpl(dgi, dge) {} StmtIterator(const VariableArrayType *t) : StmtIteratorImpl(t) {} private: StmtIterator(const StmtIteratorBase &RHS) : StmtIteratorImpl(RHS) {} inline friend StmtIterator cast_away_const(const ConstStmtIterator &RHS); }; struct ConstStmtIterator : public StmtIteratorImpl { explicit ConstStmtIterator() = default; ConstStmtIterator(const StmtIterator& RHS) : StmtIteratorImpl(RHS) {} ConstStmtIterator(Stmt * const *S) : StmtIteratorImpl( const_cast(S)) {} }; inline StmtIterator cast_away_const(const ConstStmtIterator &RHS) { return RHS; } } // namespace clang #endif // LLVM_CLANG_AST_STMTITERATOR_H