}
Stmt::child_iterator CXXCastExpr::child_end() {
- return child_begin()+1;
+ return ++child_begin();
}
// CXXBoolLiteralExpr
return reinterpret_cast<Stmt**>(&Target);
}
-Stmt::child_iterator IndirectGotoStmt::child_end() { return child_begin()+1; }
+Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
// ContinueStmt
Stmt::child_iterator ContinueStmt::child_begin() { return NULL; }
--- /dev/null
+//===--- StmtIterator.cpp - Iterators for Statements ------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Ted Kremenek and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines internal methods for StmtIterator.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/StmtIterator.h"
+#include "clang/AST/Stmt.h"
+#include "clang/AST/Decl.h"
+
+using namespace clang;
+
+void StmtIterator::NextDecl() { assert(false); }
+void StmtIterator::PrevDecl() { assert(false); }
+
+Stmt*& StmtIterator::GetInitializer() const {
+ assert (D && isa<VarDecl>(D));
+ assert (cast<VarDecl>(D)->Init);
+ return reinterpret_cast<Stmt*&>(cast<VarDecl>(D)->Init);
+}
: ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
private:
StorageClass SClass;
- Expr *Init;
+ Expr *Init;
+ friend class StmtIterator;
};
/// BlockVarDecl - Represent a local variable declaration.
#define LLVM_CLANG_AST_STMT_H
#include "clang/Basic/SourceLocation.h"
+#include "clang/AST/StmtIterator.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator"
#include <iosfwd>
/// Child Iterators: All subclasses must implement child_begin and child_end
/// to permit easy iteration over the substatements/subexpessions of an
/// AST node. This permits easy iteration over all nodes in the AST.
- typedef Stmt** child_iterator;
- typedef Stmt* const * const_child_iterator;
+ typedef StmtIterator child_iterator;
+ typedef ConstStmtIterator const_child_iterator;
typedef std::reverse_iterator<child_iterator>
- reverse_child_iterator;
+ reverse_child_iterator;
+
typedef std::reverse_iterator<const_child_iterator>
- const_reverse_child_iterator;
+ const_reverse_child_iterator;
virtual child_iterator child_begin() = 0;
virtual child_iterator child_end() = 0;
const_child_iterator child_begin() const {
- return (child_iterator) const_cast<Stmt*>(this)->child_begin();
+ return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
}
const_child_iterator child_end() const {
- return (child_iterator) const_cast<Stmt*>(this)->child_end();
+ return const_child_iterator(const_cast<Stmt*>(this)->child_end());
}
reverse_child_iterator child_rbegin() {
static inline ChildIteratorType child_begin(NodeType* N) {
if (N) return N->child_begin();
- else return NULL;
+ else return ChildIteratorType(NULL);
}
static inline ChildIteratorType child_end(NodeType* N) {
if (N) return N->child_end();
- else return NULL;
+ else return ChildIteratorType(NULL);
}
static nodes_iterator nodes_begin(const clang::Stmt* S) {
--- /dev/null
+//===--- StmtIterator.h - Iterators for Statements ------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Ted Kremenek and 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_STMT_ITR_H
+#define LLVM_CLANG_AST_STMT_ITR_H
+
+#include "llvm/ADT/iterator"
+
+namespace clang {
+
+class Stmt;
+class ScopedDecl;
+
+class StmtIterator : public bidirectional_iterator<Stmt*, ptrdiff_t> {
+ Stmt** S;
+ ScopedDecl* D;
+
+ 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;
+
+ return *this;
+ }
+
+ StmtIterator operator++(int) {
+ StmtIterator tmp = *this;
+ operator++();
+ return tmp;
+ }
+
+ StmtIterator& operator--() {
+ if (D) PrevDecl();
+ else --S;
+
+ return *this;
+ }
+
+ StmtIterator operator--(int) {
+ StmtIterator tmp = *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;
+ }
+};
+
+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;
+ }
+
+ 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; }
+};
+
+} // end namespace clang
+
+#endif