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 16d8223..d618db8 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<VarDecl>(D));
-
- if (!D) S = NULL;
+void StmtIteratorBase::NextDecl() {
+ assert (FirstDecl && Ptr.D);
+
+ do Ptr.D = Ptr.D->getNextDeclarator();
+ while (Ptr.D != NULL && !isa<VarDecl>(Ptr.D));
}
-void StmtIterator::PrevDecl() {
- assert (isa<DeclStmt>(*S));
- DeclStmt* DS = cast<DeclStmt>(*S);
-
- 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)
- d = d->getNextDeclarator();
+ ScopedDecl* d = FirstDecl;
+ ScopedDecl* lastVD = d;
- D = d;
+ while (d->getNextDeclarator() != Ptr.D) {
+ if (VarDecl* V = dyn_cast<VarDecl>(d))
+ if (V->getInit())
+ lastVD = d;
+
+ d = d->getNextDeclarator();
+ }
+
+ 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();
}