From: Ted Kremenek Date: Thu, 18 Oct 2007 18:19:31 +0000 (+0000) Subject: Refactored StmtIterator into classes StmtIteratorBase (non-templated) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c325e7f51b4c92efd711b8ad289ec16da8cd64f0;p=clang Refactored StmtIterator into classes StmtIteratorBase (non-templated) and StmtIteratorImpl (templated), which StmtIterator and ConstStmtIterator now succintly subclass. Implemented iteration over the initializers in DeclStmts. This is not thoroughly tested, so there may be bugs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43138 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/StmtIterator.cpp b/AST/StmtIterator.cpp index 16d8223a06..d618db8147 100644 --- a/AST/StmtIterator.cpp +++ b/AST/StmtIterator.cpp @@ -12,39 +12,53 @@ //===----------------------------------------------------------------------===// #include "clang/AST/StmtIterator.h" -#include "clang/AST/Stmt.h" +#include "clang/AST/Expr.h" #include "clang/AST/Decl.h" using namespace clang; -void StmtIterator::NextDecl() { - assert (D); - do D = D->getNextDeclarator(); - while (D != NULL && !isa(D)); - - if (!D) S = NULL; -} +void StmtIteratorBase::NextDecl() { + assert (FirstDecl && Ptr.D); -void StmtIterator::PrevDecl() { - assert (isa(*S)); - DeclStmt* DS = cast(*S); + do Ptr.D = Ptr.D->getNextDeclarator(); + while (Ptr.D != NULL && !isa(Ptr.D)); +} - ScopedDecl* d = DS->getDecl(); +StmtIteratorBase::StmtIteratorBase(ScopedDecl* d) { assert (d); - if (d == D) { assert(false) ; return; } + while (d != NULL) { + if (VarDecl* V = dyn_cast(d)) + if (V->getInit()) break; + + d = d->getNextDeclarator(); + } + + FirstDecl = d; + Ptr.D = d; +} + +void StmtIteratorBase::PrevDecl() { + assert (FirstDecl); + assert (Ptr.D != FirstDecl); // March through the list of decls until we find the decl just before // the one we currently point - while (d->getNextDeclarator() != D) + ScopedDecl* d = FirstDecl; + ScopedDecl* lastVD = d; + + while (d->getNextDeclarator() != Ptr.D) { + if (VarDecl* V = dyn_cast(d)) + if (V->getInit()) + lastVD = d; + d = d->getNextDeclarator(); + } - D = d; + Ptr.D = lastVD; } -Stmt*& StmtIterator::GetInitializer() const { - assert (D && isa(D)); - assert (cast(D)->Init); - return reinterpret_cast(cast(D)->Init); +Stmt* StmtIteratorBase::GetInitializer() const { + return cast(Ptr.D)->getInit(); } diff --git a/include/clang/AST/StmtIterator.h b/include/clang/AST/StmtIterator.h index 5281ccfcb9..33b26eac81 100644 --- a/include/clang/AST/StmtIterator.h +++ b/include/clang/AST/StmtIterator.h @@ -20,82 +20,86 @@ namespace clang { class Stmt; class ScopedDecl; - -class StmtIterator : public bidirectional_iterator { - Stmt** S; - ScopedDecl* D; + +class StmtIteratorBase { +protected: + union { Stmt** S; ScopedDecl* D; } Ptr; + ScopedDecl* FirstDecl; void NextDecl(); void PrevDecl(); - Stmt*& GetInitializer() const; -public: - StmtIterator(Stmt** s, ScopedDecl* d = NULL) : S(s), D(d) {} + Stmt* GetInitializer() const; + + StmtIteratorBase(Stmt** s) : FirstDecl(NULL) { Ptr.S = s; } + StmtIteratorBase(ScopedDecl* d); + StmtIteratorBase() : FirstDecl(NULL) { Ptr.S = NULL; } +}; - StmtIterator& operator++() { - if (D) NextDecl(); - else ++S; + +template +class StmtIteratorImpl : public StmtIteratorBase, + public std::iterator { +protected: + StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {} +public: + StmtIteratorImpl() {} + StmtIteratorImpl(Stmt** s) : StmtIteratorBase(s) {} + StmtIteratorImpl(ScopedDecl* d) : StmtIteratorBase(d) {} + + + DERIVED& operator++() { + if (FirstDecl) NextDecl(); + else ++Ptr.S; - return *this; + return static_cast(*this); } - StmtIterator operator++(int) { - StmtIterator tmp = *this; + DERIVED operator++(int) { + DERIVED tmp = static_cast(*this); operator++(); return tmp; } - StmtIterator& operator--() { - if (D) PrevDecl(); - else --S; + DERIVED& operator--() { + if (FirstDecl) PrevDecl(); + else --Ptr.S; - return *this; + return static_cast(*this); } - StmtIterator operator--(int) { - StmtIterator tmp = *this; + DERIVED operator--(int) { + DERIVED tmp = static_cast(*this); operator--(); return tmp; } - - reference operator*() const { return D ? GetInitializer() : *S; } - pointer operator->() const { return D ? &GetInitializer() : S; } - bool operator==(const StmtIterator& RHS) const { - return D == RHS.D && S == RHS.S; - } - - bool operator!=(const StmtIterator& RHS) const { - return D != RHS.D || S != RHS.S; + bool operator==(const DERIVED& RHS) const { + return FirstDecl == RHS.FirstDecl && Ptr.S == RHS.Ptr.S; } -}; -class ConstStmtIterator: public bidirectional_iterator { - StmtIterator I; -public: - explicit ConstStmtIterator(const StmtIterator& i) : I(i) {} - - ConstStmtIterator& operator++() { ++I; return *this; } - ConstStmtIterator& operator--() { --I; return *this; } - - ConstStmtIterator operator++(int) { - ConstStmtIterator tmp = *this; - operator++(); - return tmp; + bool operator!=(const DERIVED& RHS) const { + return FirstDecl != RHS.FirstDecl || Ptr.S != RHS.Ptr.S; } - ConstStmtIterator operator--(int) { - ConstStmtIterator tmp = *this; - operator--(); - return tmp; + STMT_PTR operator*() const { + return (STMT_PTR) (FirstDecl ? GetInitializer() : *Ptr.S); } - reference operator*() const { return const_cast(*I); } - pointer operator->() const { return const_cast(I.operator->()); } - - bool operator==(const ConstStmtIterator& RHS) const { return I == RHS.I; } - bool operator!=(const ConstStmtIterator& RHS) const { return I != RHS.I; } + STMT_PTR operator->() const { return operator*(); } }; - + +struct StmtIterator : public StmtIteratorImpl { + StmtIterator(Stmt** S) : StmtIteratorImpl(S) {} +}; + +struct ConstStmtIterator : public StmtIteratorImpl { + ConstStmtIterator(const StmtIterator& RHS) : + StmtIteratorImpl(RHS) {} +}; + } // end namespace clang #endif