r227764 - Reimplement iterator wrappers on top of llvm::iterator_adaptor_base.
Benjamin Kramer
benny.kra at googlemail.com
Sun Feb 1 12:31:36 PST 2015
Author: d0k
Date: Sun Feb 1 14:31:36 2015
New Revision: 227764
URL: http://llvm.org/viewvc/llvm-project?rev=227764&view=rev
Log:
Reimplement iterator wrappers on top of llvm::iterator_adaptor_base.
Eliminates a ton of boilerplate proxying the iterator methods. NFC.
Modified:
cfe/trunk/include/clang/AST/ASTUnresolvedSet.h
cfe/trunk/include/clang/AST/CanonicalType.h
cfe/trunk/include/clang/AST/DeclTemplate.h
cfe/trunk/include/clang/AST/UnresolvedSet.h
cfe/trunk/include/clang/Sema/Lookup.h
cfe/trunk/lib/AST/ExprCXX.cpp
Modified: cfe/trunk/include/clang/AST/ASTUnresolvedSet.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTUnresolvedSet.h?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTUnresolvedSet.h (original)
+++ cfe/trunk/include/clang/AST/ASTUnresolvedSet.h Sun Feb 1 14:31:36 2015
@@ -76,7 +76,7 @@ public:
}
void append(ASTContext &C, iterator I, iterator E) {
- Decls.append(C, I.ir, E.ir);
+ Decls.append(C, I.I, E.I);
}
DeclAccessPair &operator[](unsigned I) { return Decls[I]; }
Modified: cfe/trunk/include/clang/AST/CanonicalType.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/CanonicalType.h?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/CanonicalType.h (original)
+++ cfe/trunk/include/clang/AST/CanonicalType.h Sun Feb 1 14:31:36 2015
@@ -16,8 +16,8 @@
#define LLVM_CLANG_AST_CANONICALTYPE_H
#include "clang/AST/Type.h"
+#include "llvm/ADT/iterator.h"
#include "llvm/Support/Casting.h"
-#include <iterator>
namespace clang {
@@ -381,93 +381,22 @@ namespace clang {
/// \brief Iterator adaptor that turns an iterator over canonical QualTypes
/// into an iterator over CanQualTypes.
-template<typename InputIterator>
-class CanTypeIterator {
- InputIterator Iter;
+template <typename InputIterator>
+class CanTypeIterator
+ : public llvm::iterator_adaptor_base<
+ CanTypeIterator<InputIterator>, InputIterator,
+ typename std::iterator_traits<InputIterator>::iterator_category,
+ CanQualType,
+ typename std::iterator_traits<InputIterator>::difference_type,
+ CanProxy<Type>, CanQualType> {
+ typedef typename CanTypeIterator::iterator_adaptor_base BaseT;
public:
- typedef CanQualType value_type;
- typedef value_type reference;
- typedef CanProxy<Type> pointer;
- typedef typename std::iterator_traits<InputIterator>::difference_type
- difference_type;
- typedef typename std::iterator_traits<InputIterator>::iterator_category
- iterator_category;
-
- CanTypeIterator() : Iter() { }
- explicit CanTypeIterator(InputIterator Iter) : Iter(Iter) { }
-
- // Input iterator
- reference operator*() const {
- return CanQualType::CreateUnsafe(*Iter);
- }
-
- pointer operator->() const;
-
- CanTypeIterator &operator++() {
- ++Iter;
- return *this;
- }
-
- CanTypeIterator operator++(int) {
- CanTypeIterator Tmp(*this);
- ++Iter;
- return Tmp;
- }
-
- friend bool operator==(const CanTypeIterator& X, const CanTypeIterator &Y) {
- return X.Iter == Y.Iter;
- }
- friend bool operator!=(const CanTypeIterator& X, const CanTypeIterator &Y) {
- return X.Iter != Y.Iter;
- }
-
- // Bidirectional iterator
- CanTypeIterator &operator--() {
- --Iter;
- return *this;
- }
-
- CanTypeIterator operator--(int) {
- CanTypeIterator Tmp(*this);
- --Iter;
- return Tmp;
- }
+ CanTypeIterator() {}
+ explicit CanTypeIterator(InputIterator Iter) : BaseT(std::move(Iter)) {}
- // Random access iterator
- reference operator[](difference_type n) const {
- return CanQualType::CreateUnsafe(Iter[n]);
- }
-
- CanTypeIterator &operator+=(difference_type n) {
- Iter += n;
- return *this;
- }
-
- CanTypeIterator &operator-=(difference_type n) {
- Iter -= n;
- return *this;
- }
-
- friend CanTypeIterator operator+(CanTypeIterator X, difference_type n) {
- X += n;
- return X;
- }
-
- friend CanTypeIterator operator+(difference_type n, CanTypeIterator X) {
- X += n;
- return X;
- }
-
- friend CanTypeIterator operator-(CanTypeIterator X, difference_type n) {
- X -= n;
- return X;
- }
-
- friend difference_type operator-(const CanTypeIterator &X,
- const CanTypeIterator &Y) {
- return X - Y;
- }
+ CanQualType operator*() const { return CanQualType::CreateUnsafe(*BaseT::I); }
+ CanProxy<Type> operator->() const;
};
template<>
@@ -727,9 +656,8 @@ CanProxy<T> CanQual<T>::operator->() con
return CanProxy<T>(*this);
}
-template<typename InputIterator>
-typename CanTypeIterator<InputIterator>::pointer
-CanTypeIterator<InputIterator>::operator->() const {
+template <typename InputIterator>
+CanProxy<Type> CanTypeIterator<InputIterator>::operator->() const {
return CanProxy<Type>(*this);
}
Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Sun Feb 1 14:31:36 2015
@@ -550,42 +550,27 @@ protected:
}
};
- template <typename EntryType,
- typename _SETraits = SpecEntryTraits<EntryType>,
- typename _DeclType = typename _SETraits::DeclType>
- class SpecIterator : public std::iterator<std::forward_iterator_tag,
- _DeclType*, ptrdiff_t,
- _DeclType*, _DeclType*> {
- typedef _SETraits SETraits;
- typedef _DeclType DeclType;
-
- typedef typename llvm::FoldingSetVector<EntryType>::iterator
- SetIteratorType;
-
- SetIteratorType SetIter;
+ template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>,
+ typename DeclType = typename SETraits::DeclType>
+ class SpecIterator
+ : public llvm::iterator_adaptor_base<
+ SpecIterator<EntryType, SETraits, DeclType>,
+ typename llvm::FoldingSetVector<EntryType>::iterator,
+ typename std::iterator_traits<typename llvm::FoldingSetVector<
+ EntryType>::iterator>::iterator_category,
+ DeclType *, ptrdiff_t, DeclType *, DeclType *> {
+ typedef typename SpecIterator::iterator_adaptor_base BaseT;
public:
- SpecIterator() : SetIter() {}
- SpecIterator(SetIteratorType SetIter) : SetIter(SetIter) {}
+ SpecIterator() {}
+ explicit SpecIterator(
+ typename llvm::FoldingSetVector<EntryType>::iterator SetIter)
+ : BaseT(std::move(SetIter)) {}
DeclType *operator*() const {
- return SETraits::getMostRecentDecl(&*SetIter);
+ return SETraits::getMostRecentDecl(&*BaseT::I);
}
DeclType *operator->() const { return **this; }
-
- SpecIterator &operator++() { ++SetIter; return *this; }
- SpecIterator operator++(int) {
- SpecIterator tmp(*this);
- ++(*this);
- return tmp;
- }
-
- bool operator==(SpecIterator Other) const {
- return SetIter == Other.SetIter;
- }
- bool operator!=(SpecIterator Other) const {
- return SetIter != Other.SetIter;
- }
};
template <typename EntryType>
Modified: cfe/trunk/include/clang/AST/UnresolvedSet.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/UnresolvedSet.h?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/UnresolvedSet.h (original)
+++ cfe/trunk/include/clang/AST/UnresolvedSet.h Sun Feb 1 14:31:36 2015
@@ -19,74 +19,36 @@
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
-#include <iterator>
+#include "llvm/ADT/iterator.h"
namespace clang {
/// The iterator over UnresolvedSets. Serves as both the const and
/// non-const iterator.
-class UnresolvedSetIterator {
-private:
- typedef MutableArrayRef<DeclAccessPair> DeclsTy;
- typedef DeclsTy::iterator IteratorTy;
-
- IteratorTy ir;
-
+class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
+ UnresolvedSetIterator, DeclAccessPair *,
+ std::random_access_iterator_tag, NamedDecl *,
+ std::ptrdiff_t, NamedDecl *, NamedDecl *> {
friend class UnresolvedSetImpl;
friend class ASTUnresolvedSet;
friend class OverloadExpr;
- explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
- explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
- ir(const_cast<DeclsTy::iterator>(ir)) {}
-
- IteratorTy getIterator() const { return ir; }
-
+
+ explicit UnresolvedSetIterator(DeclAccessPair *Iter)
+ : iterator_adaptor_base(Iter) {}
+ explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
+ : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
+
public:
UnresolvedSetIterator() {}
- typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
- typedef NamedDecl *value_type;
- typedef NamedDecl **pointer;
- typedef NamedDecl *reference;
- typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
-
- NamedDecl *getDecl() const { return ir->getDecl(); }
- void setDecl(NamedDecl *ND) const { return ir->setDecl(ND); }
- AccessSpecifier getAccess() const { return ir->getAccess(); }
- void setAccess(AccessSpecifier AS) { ir->setAccess(AS); }
- DeclAccessPair getPair() const { return *ir; }
+ NamedDecl *getDecl() const { return I->getDecl(); }
+ void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
+ AccessSpecifier getAccess() const { return I->getAccess(); }
+ void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
+ const DeclAccessPair &getPair() const { return *I; }
NamedDecl *operator*() const { return getDecl(); }
-
- UnresolvedSetIterator &operator++() { ++ir; return *this; }
- UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
- UnresolvedSetIterator &operator--() { --ir; return *this; }
- UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
-
- UnresolvedSetIterator &operator+=(difference_type d) {
- ir += d; return *this;
- }
- UnresolvedSetIterator operator+(difference_type d) const {
- return UnresolvedSetIterator(ir + d);
- }
- UnresolvedSetIterator &operator-=(difference_type d) {
- ir -= d; return *this;
- }
- UnresolvedSetIterator operator-(difference_type d) const {
- return UnresolvedSetIterator(ir - d);
- }
- value_type operator[](difference_type d) const { return *(*this + d); }
-
- difference_type operator-(const UnresolvedSetIterator &o) const {
- return ir - o.ir;
- }
-
- bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
- bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
- bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
- bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
- bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
- bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
+ NamedDecl *operator->() const { return **this; }
};
/// \brief A set of unresolved declarations.
@@ -132,21 +94,17 @@ public:
/// Replaces the declaration at the given iterator with the new one,
/// preserving the original access bits.
- void replace(iterator I, NamedDecl *New) {
- I.ir->setDecl(New);
- }
+ void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
- I.ir->set(New, AS);
+ I.I->set(New, AS);
}
void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
- void erase(iterator I) { *I.ir = decls().pop_back_val(); }
+ void erase(iterator I) { *I.I = decls().pop_back_val(); }
- void setAccess(iterator I, AccessSpecifier AS) {
- I.ir->setAccess(AS);
- }
+ void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
void clear() { decls().clear(); }
void set_size(unsigned N) { decls().set_size(N); }
@@ -154,9 +112,7 @@ public:
bool empty() const { return decls().empty(); }
unsigned size() const { return decls().size(); }
- void append(iterator I, iterator E) {
- decls().append(I.ir, E.ir);
- }
+ void append(iterator I, iterator E) { decls().append(I.I, E.I); }
DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
Modified: cfe/trunk/include/clang/Sema/Lookup.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Lookup.h?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Lookup.h (original)
+++ cfe/trunk/include/clang/Sema/Lookup.h Sun Feb 1 14:31:36 2015
@@ -735,22 +735,18 @@ public:
}
class iterator
- : public std::iterator<std::forward_iterator_tag, NamedDecl *> {
- typedef llvm::DenseMap<NamedDecl*,NamedDecl*>::iterator inner_iterator;
- inner_iterator iter;
-
+ : public llvm::iterator_adaptor_base<iterator, decltype(Decls)::iterator,
+ std::forward_iterator_tag,
+ NamedDecl *> {
friend class ADLResult;
- iterator(const inner_iterator &iter) : iter(iter) {}
- public:
- iterator() {}
- iterator &operator++() { ++iter; return *this; }
- iterator operator++(int) { return iterator(iter++); }
+ iterator(decltype(Decls)::iterator Iter)
+ : iterator_adaptor_base(std::move(Iter)) {}
- value_type operator*() const { return iter->second; }
+ public:
+ iterator() {}
- bool operator==(const iterator &other) const { return iter == other.iter; }
- bool operator!=(const iterator &other) const { return iter != other.iter; }
+ value_type operator*() const { return I->second; }
};
iterator begin() { return iterator(Decls.begin()); }
Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=227764&r1=227763&r2=227764&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Sun Feb 1 14:31:36 2015
@@ -359,8 +359,7 @@ OverloadExpr::OverloadExpr(StmtClass K,
Results = static_cast<DeclAccessPair *>(
C.Allocate(sizeof(DeclAccessPair) * NumResults,
llvm::alignOf<DeclAccessPair>()));
- memcpy(Results, &*Begin.getIterator(),
- NumResults * sizeof(DeclAccessPair));
+ memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
}
// If we have explicit template arguments, check for dependent
@@ -401,8 +400,7 @@ void OverloadExpr::initializeResults(con
C.Allocate(sizeof(DeclAccessPair) * NumResults,
llvm::alignOf<DeclAccessPair>()));
- memcpy(Results, &*Begin.getIterator(),
- NumResults * sizeof(DeclAccessPair));
+ memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
}
}
More information about the cfe-commits
mailing list