diff --git a/llvm/include/llvm/IR/ValueMap.h b/llvm/include/llvm/IR/ValueMap.h index 97653c2282aba..9ab7d8ba17a79 100644 --- a/llvm/include/llvm/IR/ValueMap.h +++ b/llvm/include/llvm/IR/ValueMap.h @@ -44,8 +44,8 @@ namespace llvm { template class ValueMapCallbackVH; -template class ValueMapIterator; -template class ValueMapConstIterator; +template +class ValueMapIteratorImpl; /// This class defines the default behavior for configurable aspects of /// ValueMap<>. User Configs should inherit from this class to be as compatible @@ -132,8 +132,8 @@ class ValueMap { return Where->second.get(); } - using iterator = ValueMapIterator; - using const_iterator = ValueMapConstIterator; + using iterator = ValueMapIteratorImpl; + using const_iterator = ValueMapIteratorImpl; inline iterator begin() { return iterator(Map.begin()); } inline iterator end() { return iterator(Map.end()); } @@ -318,8 +318,10 @@ struct DenseMapInfo> { } }; -template class ValueMapIterator { - using BaseT = typename DenseMapT::iterator; +template +class ValueMapIteratorImpl { + using BaseT = std::conditional_t; using ValueT = typename DenseMapT::mapped_type; BaseT I; @@ -331,14 +333,20 @@ template class ValueMapIterator { using pointer = value_type *; using reference = value_type &; - ValueMapIterator() : I() {} - ValueMapIterator(BaseT I) : I(I) {} + ValueMapIteratorImpl() = default; + ValueMapIteratorImpl(BaseT I) : I(I) {} + + // Allow conversion from iterator to const_iterator. + template > + ValueMapIteratorImpl( + const ValueMapIteratorImpl &Other) + : I(Other.base()) {} BaseT base() const { return I; } struct ValueTypeProxy { const KeyT first; - ValueT &second; + std::conditional_t second; ValueTypeProxy *operator->() { return this; } @@ -354,69 +362,25 @@ template class ValueMapIterator { ValueTypeProxy operator->() const { return operator*(); } - bool operator==(const ValueMapIterator &RHS) const { return I == RHS.I; } - bool operator!=(const ValueMapIterator &RHS) const { return I != RHS.I; } + bool operator==(const ValueMapIteratorImpl &RHS) const { return I == RHS.I; } + bool operator!=(const ValueMapIteratorImpl &RHS) const { return I != RHS.I; } - inline ValueMapIterator &operator++() { // Preincrement + inline ValueMapIteratorImpl &operator++() { // Preincrement ++I; return *this; } - ValueMapIterator operator++(int) { // Postincrement - ValueMapIterator tmp = *this; + ValueMapIteratorImpl operator++(int) { // Postincrement + ValueMapIteratorImpl tmp = *this; ++*this; return tmp; } }; -template class ValueMapConstIterator { - using BaseT = typename DenseMapT::const_iterator; - using ValueT = typename DenseMapT::mapped_type; - - BaseT I; - -public: - using iterator_category = std::forward_iterator_tag; - using value_type = std::pair; - using difference_type = std::ptrdiff_t; - using pointer = value_type *; - using reference = value_type &; - - ValueMapConstIterator() : I() {} - ValueMapConstIterator(BaseT I) : I(I) {} - ValueMapConstIterator(ValueMapIterator Other) - : I(Other.base()) {} - - BaseT base() const { return I; } +template +using ValueMapIterator = ValueMapIteratorImpl; - struct ValueTypeProxy { - const KeyT first; - const ValueT &second; - ValueTypeProxy *operator->() { return this; } - operator std::pair() const { - return std::make_pair(first, second); - } - }; - - ValueTypeProxy operator*() const { - ValueTypeProxy Result = {I->first.Unwrap(), I->second}; - return Result; - } - - ValueTypeProxy operator->() const { return operator*(); } - - bool operator==(const ValueMapConstIterator &RHS) const { return I == RHS.I; } - bool operator!=(const ValueMapConstIterator &RHS) const { return I != RHS.I; } - - inline ValueMapConstIterator &operator++() { // Preincrement - ++I; - return *this; - } - ValueMapConstIterator operator++(int) { // Postincrement - ValueMapConstIterator tmp = *this; - ++*this; - return tmp; - } -}; +template +using ValueMapConstIterator = ValueMapIteratorImpl; } // end namespace llvm