diff options
author | John McCall <rjmccall@apple.com> | 2011-02-13 04:07:26 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2011-02-13 04:07:26 +0000 |
commit | 7502c1d3ce8bb97bcc4f7bebef507040bd93b26f (patch) | |
tree | a8483d8d96d4c459027291b90fad493b985313cb /include/clang/AST/StmtIterator.h | |
parent | 0d70d71ccbc4f7f59cadb759f61b7172a149676c (diff) |
Give some convenient idiomatic accessors to Stmt::child_range and
Stmt::const_child_range, then make a bunch of places use them instead
of the individual iterator accessors.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125450 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang/AST/StmtIterator.h')
-rw-r--r-- | include/clang/AST/StmtIterator.h | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/include/clang/AST/StmtIterator.h b/include/clang/AST/StmtIterator.h index 0d4657bdcc..851c001adc 100644 --- a/include/clang/AST/StmtIterator.h +++ b/include/clang/AST/StmtIterator.h @@ -146,14 +146,85 @@ struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator, StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {} }; -typedef std::pair<StmtIterator,StmtIterator> StmtRange; -typedef std::pair<ConstStmtIterator,ConstStmtIterator> ConstStmtRange; +/// A range of statement iterators. +/// +/// This class provides some extra functionality beyond std::pair +/// in order to allow the following idiom: +/// for (StmtRange range = stmt->children(); range; ++range) +struct StmtRange : std::pair<StmtIterator,StmtIterator> { + StmtRange() {} + StmtRange(const StmtIterator &begin, const StmtIterator &end) + : std::pair<StmtIterator,StmtIterator>(begin, end) {} + + bool empty() const { return first == second; } + operator bool() const { return !empty(); } + + Stmt *operator->() const { return first.operator->(); } + Stmt *&operator*() const { return first.operator*(); } + + StmtRange &operator++() { + assert(!empty() && "incrementing on empty range"); + ++first; + return *this; + } -inline StmtIterator begin(StmtRange range) { return range.first; } -inline StmtIterator end(StmtRange range) { return range.second; } + StmtRange operator++(int) { + assert(!empty() && "incrementing on empty range"); + StmtRange copy = *this; + ++first; + return copy; + } -inline ConstStmtIterator begin(ConstStmtRange range) { return range.first; } -inline ConstStmtIterator end(ConstStmtRange range) { return range.second; } + friend const StmtIterator &begin(const StmtRange &range) { + return range.first; + } + friend const StmtIterator &end(const StmtRange &range) { + return range.second; + } +}; + +/// A range of const statement iterators. +/// +/// This class provides some extra functionality beyond std::pair +/// in order to allow the following idiom: +/// for (ConstStmtRange range = stmt->children(); range; ++range) +struct ConstStmtRange : std::pair<ConstStmtIterator,ConstStmtIterator> { + ConstStmtRange() {} + ConstStmtRange(const ConstStmtIterator &begin, + const ConstStmtIterator &end) + : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {} + ConstStmtRange(const StmtRange &range) + : std::pair<ConstStmtIterator,ConstStmtIterator>(range.first, range.second) + {} + ConstStmtRange(const StmtIterator &begin, const StmtIterator &end) + : std::pair<ConstStmtIterator,ConstStmtIterator>(begin, end) {} + + bool empty() const { return first == second; } + operator bool() const { return !empty(); } + + const Stmt *operator->() const { return first.operator->(); } + const Stmt *operator*() const { return first.operator*(); } + + ConstStmtRange &operator++() { + assert(!empty() && "incrementing on empty range"); + ++first; + return *this; + } + + ConstStmtRange operator++(int) { + assert(!empty() && "incrementing on empty range"); + ConstStmtRange copy = *this; + ++first; + return copy; + } + + friend const ConstStmtIterator &begin(const ConstStmtRange &range) { + return range.first; + } + friend const ConstStmtIterator &end(const ConstStmtRange &range) { + return range.second; + } +}; } // end namespace clang |