From 6887ff2c70f4afba4e521f8779d041e6d02df464 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 27 Jun 2013 20:39:04 +0000 Subject: [PATCH] Simplify StmtIterator. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185098 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/StmtIterator.h | 27 +++++--------- lib/AST/StmtIterator.cpp | 63 ++++++-------------------------- 2 files changed, 20 insertions(+), 70 deletions(-) diff --git a/include/clang/AST/StmtIterator.h b/include/clang/AST/StmtIterator.h index fc25fa9a6e..fbc8e5d4ea 100644 --- a/include/clang/AST/StmtIterator.h +++ b/include/clang/AST/StmtIterator.h @@ -29,18 +29,14 @@ class VariableArrayType; class StmtIteratorBase { protected: - enum { DeclMode = 0x1, SizeOfTypeVAMode = 0x2, DeclGroupMode = 0x3, + enum { StmtMode = 0x0, SizeOfTypeVAMode = 0x1, DeclGroupMode = 0x2, Flags = 0x3 }; Stmt **stmt; - union { Decl *decl; Decl **DGI; }; + Decl **DGI; uintptr_t RawVAPtr; Decl **DGE; - bool inDecl() const { - return (RawVAPtr & Flags) == DeclMode; - } - bool inDeclGroup() const { return (RawVAPtr & Flags) == DeclGroupMode; } @@ -50,7 +46,7 @@ protected: } bool inStmt() const { - return (RawVAPtr & Flags) == 0; + return (RawVAPtr & Flags) == StmtMode; } const VariableArrayType *getVAPtr() const { @@ -58,7 +54,7 @@ protected: } void setVAPtr(const VariableArrayType *P) { - assert (inDecl() || inDeclGroup() || inSizeOfTypeVA()); + assert (inDeclGroup() || inSizeOfTypeVA()); RawVAPtr = reinterpret_cast(P) | (RawVAPtr & Flags); } @@ -68,11 +64,10 @@ protected: Stmt*& GetDeclExpr() const; - StmtIteratorBase(Stmt **s) : stmt(s), decl(0), RawVAPtr(0) {} - StmtIteratorBase(Decl *d, Stmt **s); + StmtIteratorBase(Stmt **s) : stmt(s), DGI(0), RawVAPtr(0) {} StmtIteratorBase(const VariableArrayType *t); StmtIteratorBase(Decl **dgi, Decl **dge); - StmtIteratorBase() : stmt(0), decl(0), RawVAPtr(0) {} + StmtIteratorBase() : stmt(0), DGI(0), RawVAPtr(0) {} }; @@ -87,7 +82,6 @@ public: StmtIteratorImpl() {} StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {} StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {} - StmtIteratorImpl(Decl *d, Stmt **s) : StmtIteratorBase(d, s) {} StmtIteratorImpl(const VariableArrayType *t) : StmtIteratorBase(t) {} DERIVED& operator++() { @@ -108,15 +102,15 @@ public: } bool operator==(const DERIVED& RHS) const { - return stmt == RHS.stmt && decl == RHS.decl && RawVAPtr == RHS.RawVAPtr; + return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr; } bool operator!=(const DERIVED& RHS) const { - return stmt != RHS.stmt || decl != RHS.decl || RawVAPtr != RHS.RawVAPtr; + return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr; } REFERENCE operator*() const { - return (REFERENCE) (inStmt() ? *stmt : GetDeclExpr()); + return inStmt() ? *stmt : GetDeclExpr(); } REFERENCE operator->() const { return operator*(); } @@ -132,9 +126,6 @@ struct StmtIterator : public StmtIteratorImpl { StmtIterator(const VariableArrayType *t) : StmtIteratorImpl(t) {} - - StmtIterator(Decl* D, Stmt **s = 0) - : StmtIteratorImpl(D, s) {} }; struct ConstStmtIterator : public StmtIteratorImpl(decl)) - if (VD->Init) - return; - - NextDecl(); - } - else if (inDeclGroup()) { + if (inDeclGroup()) { if (VarDecl* VD = dyn_cast(*DGI)) if (VD->Init) return; @@ -55,40 +48,26 @@ void StmtIteratorBase::NextVA() { NextDecl(); } else { - assert (inSizeOfTypeVA()); - assert(!decl); + assert(inSizeOfTypeVA()); RawVAPtr = 0; } } void StmtIteratorBase::NextDecl(bool ImmediateAdvance) { assert (getVAPtr() == NULL); + assert(inDeclGroup()); - if (inDecl()) { - assert(decl); + if (ImmediateAdvance) + ++DGI; - // FIXME: SIMPLIFY AWAY. - if (ImmediateAdvance) - decl = 0; - else if (HandleDecl(decl)) + for ( ; DGI != DGE; ++DGI) + if (HandleDecl(*DGI)) return; - } - else { - assert(inDeclGroup()); - - if (ImmediateAdvance) - ++DGI; - - for ( ; DGI != DGE; ++DGI) - if (HandleDecl(*DGI)) - return; - } RawVAPtr = 0; } bool StmtIteratorBase::HandleDecl(Decl* D) { - if (VarDecl* VD = dyn_cast(D)) { if (const VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) { setVAPtr(VAPtr); @@ -113,43 +92,23 @@ bool StmtIteratorBase::HandleDecl(Decl* D) { return false; } -StmtIteratorBase::StmtIteratorBase(Decl *d, Stmt **s) - : stmt(s), decl(d), RawVAPtr(d ? DeclMode : 0) { - if (decl) - NextDecl(false); -} - StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge) : stmt(0), DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) { NextDecl(false); } StmtIteratorBase::StmtIteratorBase(const VariableArrayType* t) - : stmt(0), decl(0), RawVAPtr(SizeOfTypeVAMode) { + : stmt(0), DGI(0), RawVAPtr(SizeOfTypeVAMode) { RawVAPtr |= reinterpret_cast(t); } Stmt*& StmtIteratorBase::GetDeclExpr() const { - if (const VariableArrayType* VAPtr = getVAPtr()) { assert (VAPtr->SizeExpr); return const_cast(VAPtr->SizeExpr); } - assert (inDecl() || inDeclGroup()); - - if (inDeclGroup()) { - VarDecl* VD = cast(*DGI); - return *VD->getInitAddress(); - } - - assert (inDecl()); - - if (VarDecl* VD = dyn_cast(decl)) { - assert (VD->Init); - return *VD->getInitAddress(); - } - - EnumConstantDecl* ECD = cast(decl); - return ECD->Init; + assert (inDeclGroup()); + VarDecl* VD = cast(*DGI); + return *VD->getInitAddress(); } -- 2.40.0