[cfe-commits] r43138 - in /cfe/trunk: AST/StmtIterator.cpp include/clang/AST/StmtIterator.h

Ted Kremenek kremenek at apple.com
Thu Oct 18 11:19:31 PDT 2007


Author: kremenek
Date: Thu Oct 18 13:19:31 2007
New Revision: 43138

URL: http://llvm.org/viewvc/llvm-project?rev=43138&view=rev
Log:
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.

Modified:
    cfe/trunk/AST/StmtIterator.cpp
    cfe/trunk/include/clang/AST/StmtIterator.h

Modified: cfe/trunk/AST/StmtIterator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtIterator.cpp?rev=43138&r1=43137&r2=43138&view=diff

==============================================================================
--- cfe/trunk/AST/StmtIterator.cpp (original)
+++ cfe/trunk/AST/StmtIterator.cpp Thu Oct 18 13:19:31 2007
@@ -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<VarDecl>(D));
-  
-  if (!D) S = NULL;
-}
+void StmtIteratorBase::NextDecl() {
+  assert (FirstDecl && Ptr.D);
 
-void StmtIterator::PrevDecl() {
-  assert (isa<DeclStmt>(*S));
-  DeclStmt* DS = cast<DeclStmt>(*S);
+  do Ptr.D = Ptr.D->getNextDeclarator();
+  while (Ptr.D != NULL && !isa<VarDecl>(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<VarDecl>(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<VarDecl>(d))
+      if (V->getInit())
+        lastVD = d;
+
     d = d->getNextDeclarator();
+  }
   
-  D = d;
+  Ptr.D = lastVD;
 }
 
-Stmt*& StmtIterator::GetInitializer() const {
-  assert (D && isa<VarDecl>(D));
-  assert (cast<VarDecl>(D)->Init);
-  return reinterpret_cast<Stmt*&>(cast<VarDecl>(D)->Init);
+Stmt* StmtIteratorBase::GetInitializer() const {
+  return cast<VarDecl>(Ptr.D)->getInit();
 }

Modified: cfe/trunk/include/clang/AST/StmtIterator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtIterator.h?rev=43138&r1=43137&r2=43138&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtIterator.h (original)
+++ cfe/trunk/include/clang/AST/StmtIterator.h Thu Oct 18 13:19:31 2007
@@ -20,82 +20,86 @@
 
 class Stmt;
 class ScopedDecl;
-  
-class StmtIterator : public bidirectional_iterator<Stmt*, ptrdiff_t> {
-  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) {}
-  
-  StmtIterator& operator++() { 
-    if (D) NextDecl();
-    else ++S;
+  Stmt* GetInitializer() const;
+
+  StmtIteratorBase(Stmt** s) : FirstDecl(NULL) { Ptr.S = s; }
+  StmtIteratorBase(ScopedDecl* d);
+  StmtIteratorBase() : FirstDecl(NULL) { Ptr.S = NULL; }
+};
+  
+  
+template <typename DERIVED, typename STMT_PTR>
+class StmtIteratorImpl : public StmtIteratorBase, 
+                         public std::iterator<std::bidirectional_iterator_tag,
+                                              STMT_PTR, ptrdiff_t, 
+                                              STMT_PTR, STMT_PTR> {  
+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<DERIVED&>(*this);
   }
     
-  StmtIterator operator++(int) {
-    StmtIterator tmp = *this;
+  DERIVED operator++(int) {
+    DERIVED tmp = static_cast<DERIVED&>(*this);
     operator++();
     return tmp;
   }
   
-  StmtIterator& operator--() {
-    if (D) PrevDecl();
-    else --S;
+  DERIVED& operator--() {
+    if (FirstDecl) PrevDecl();
+    else --Ptr.S;
     
-    return *this;
+    return static_cast<DERIVED&>(*this);
   }
   
-  StmtIterator operator--(int) {
-    StmtIterator tmp = *this;
+  DERIVED operator--(int) {
+    DERIVED tmp = static_cast<DERIVED&>(*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 DERIVED& RHS) const {
+    return FirstDecl == RHS.FirstDecl && Ptr.S == RHS.Ptr.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<const Stmt*, ptrdiff_t> {
-  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;
+  STMT_PTR operator*() const { 
+    return (STMT_PTR) (FirstDecl ? GetInitializer() : *Ptr.S);
   }
   
-  ConstStmtIterator operator--(int) {
-    ConstStmtIterator tmp = *this;
-    operator--();
-    return tmp;
-  }
-  
-  reference operator*() const { return const_cast<reference>(*I); }
-  pointer operator->() const { return const_cast<pointer>(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*> {
+  StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*>(S) {}
+};
+
+struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
+                                                   const Stmt*> {    
+  ConstStmtIterator(const StmtIterator& RHS) : 
+    StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {}
+};
+
 } // end namespace clang
 
 #endif





More information about the cfe-commits mailing list