From 04ec7e316b1a8d3d89377644c1bd2fe71d59ea17 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 1 Feb 2015 20:31:36 +0000 Subject: [PATCH] Reimplement iterator wrappers on top of llvm::iterator_adaptor_base. Eliminates a ton of boilerplate proxying the iterator methods. NFC. llvm-svn: 227764 --- clang/include/clang/AST/ASTUnresolvedSet.h | 2 +- clang/include/clang/AST/CanonicalType.h | 104 +++++------------------------ clang/include/clang/AST/DeclTemplate.h | 45 +++++-------- clang/include/clang/AST/UnresolvedSet.h | 88 ++++++------------------ clang/include/clang/Sema/Lookup.h | 20 +++--- clang/lib/AST/ExprCXX.cpp | 6 +- 6 files changed, 64 insertions(+), 201 deletions(-) diff --git a/clang/include/clang/AST/ASTUnresolvedSet.h b/clang/include/clang/AST/ASTUnresolvedSet.h index 84b0842..9078a0e 100644 --- a/clang/include/clang/AST/ASTUnresolvedSet.h +++ b/clang/include/clang/AST/ASTUnresolvedSet.h @@ -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]; } diff --git a/clang/include/clang/AST/CanonicalType.h b/clang/include/clang/AST/CanonicalType.h index aa3c846..d73daaa 100644 --- a/clang/include/clang/AST/CanonicalType.h +++ b/clang/include/clang/AST/CanonicalType.h @@ -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 namespace clang { @@ -381,93 +381,22 @@ namespace clang { /// \brief Iterator adaptor that turns an iterator over canonical QualTypes /// into an iterator over CanQualTypes. -template -class CanTypeIterator { - InputIterator Iter; +template +class CanTypeIterator + : public llvm::iterator_adaptor_base< + CanTypeIterator, InputIterator, + typename std::iterator_traits::iterator_category, + CanQualType, + typename std::iterator_traits::difference_type, + CanProxy, CanQualType> { + typedef typename CanTypeIterator::iterator_adaptor_base BaseT; public: - typedef CanQualType value_type; - typedef value_type reference; - typedef CanProxy pointer; - typedef typename std::iterator_traits::difference_type - difference_type; - typedef typename std::iterator_traits::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 operator->() const; }; template<> @@ -727,9 +656,8 @@ CanProxy CanQual::operator->() const { return CanProxy(*this); } -template -typename CanTypeIterator::pointer -CanTypeIterator::operator->() const { +template +CanProxy CanTypeIterator::operator->() const { return CanProxy(*this); } diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index 58f24d9..2c1d8f4 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -550,42 +550,27 @@ protected: } }; - template , - typename _DeclType = typename _SETraits::DeclType> - class SpecIterator : public std::iterator { - typedef _SETraits SETraits; - typedef _DeclType DeclType; - - typedef typename llvm::FoldingSetVector::iterator - SetIteratorType; - - SetIteratorType SetIter; + template , + typename DeclType = typename SETraits::DeclType> + class SpecIterator + : public llvm::iterator_adaptor_base< + SpecIterator, + typename llvm::FoldingSetVector::iterator, + typename std::iterator_traits::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::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 diff --git a/clang/include/clang/AST/UnresolvedSet.h b/clang/include/clang/AST/UnresolvedSet.h index a11f22d..26ee1cf 100644 --- a/clang/include/clang/AST/UnresolvedSet.h +++ b/clang/include/clang/AST/UnresolvedSet.h @@ -19,74 +19,36 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" -#include +#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 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(ir)) {} - - IteratorTy getIterator() const { return ir; } - + + explicit UnresolvedSetIterator(DeclAccessPair *Iter) + : iterator_adaptor_base(Iter) {} + explicit UnresolvedSetIterator(const DeclAccessPair *Iter) + : iterator_adaptor_base(const_cast(Iter)) {} + public: UnresolvedSetIterator() {} - typedef std::iterator_traits::difference_type difference_type; - typedef NamedDecl *value_type; - typedef NamedDecl **pointer; - typedef NamedDecl *reference; - typedef std::iterator_traits::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]; } diff --git a/clang/include/clang/Sema/Lookup.h b/clang/include/clang/Sema/Lookup.h index 1c6c7bb..a9892d2 100644 --- a/clang/include/clang/Sema/Lookup.h +++ b/clang/include/clang/Sema/Lookup.h @@ -735,22 +735,18 @@ public: } class iterator - : public std::iterator { - typedef llvm::DenseMap::iterator inner_iterator; - inner_iterator iter; - + : public llvm::iterator_adaptor_base { 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()); } diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 9336166..fd25b58 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -359,8 +359,7 @@ OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C, Results = static_cast( C.Allocate(sizeof(DeclAccessPair) * NumResults, llvm::alignOf())); - 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(const ASTContext &C, C.Allocate(sizeof(DeclAccessPair) * NumResults, llvm::alignOf())); - memcpy(Results, &*Begin.getIterator(), - NumResults * sizeof(DeclAccessPair)); + memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair)); } } -- 2.7.4