Implemented 90% functionality of new child_iterator for Stmt objects

that will (soon) allow iteration over the initializers in
declarations.  This new iterator mechanism is implemented by the
classes StmtIterator and ConstStmtIterator.

Patched a few files to use "operator++" instead of "operator+" on
child_iterators.

Friendship added in VarDecl to StmtIterator to allow returning a
reference to the initializer within the VarDecl.  We may not wish this
as a permanent solution.

llvm-svn: 43105
This commit is contained in:
Ted Kremenek 2007-10-18 00:24:38 +00:00
parent 9c0f9fe151
commit 336886a344
7 changed files with 142 additions and 11 deletions

View File

@ -25,7 +25,7 @@ Stmt::child_iterator CXXCastExpr::child_begin() {
}
Stmt::child_iterator CXXCastExpr::child_end() {
return child_begin()+1;
return ++child_begin();
}
// CXXBoolLiteralExpr

View File

@ -168,7 +168,7 @@ Stmt::child_iterator IndirectGotoStmt::child_begin() {
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; }

View File

@ -0,0 +1,27 @@
//===--- 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);
}

View File

@ -268,7 +268,8 @@ protected:
: 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.

View File

@ -15,6 +15,7 @@
#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>
@ -95,23 +96,24 @@ public:
/// 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() {

View File

@ -60,12 +60,12 @@ template <> struct GraphTraits<const clang::Stmt*> {
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) {

View File

@ -0,0 +1,101 @@
//===--- 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