2017-06-14 00:11:49 +02:00
|
|
|
//===- llvm/ADT/ilist_iterator.h - Intrusive List Iterator ------*- C++ -*-===//
|
2016-08-30 03:37:58 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-30 03:37:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_ILIST_ITERATOR_H
|
|
|
|
#define LLVM_ADT_ILIST_ITERATOR_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ilist_node.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
namespace ilist_detail {
|
|
|
|
|
2016-09-11 18:20:53 +02:00
|
|
|
/// Find const-correct node types.
|
|
|
|
template <class OptionsT, bool IsConst> struct IteratorTraits;
|
|
|
|
template <class OptionsT> struct IteratorTraits<OptionsT, false> {
|
2017-06-14 00:11:49 +02:00
|
|
|
using value_type = typename OptionsT::value_type;
|
|
|
|
using pointer = typename OptionsT::pointer;
|
|
|
|
using reference = typename OptionsT::reference;
|
|
|
|
using node_pointer = ilist_node_impl<OptionsT> *;
|
|
|
|
using node_reference = ilist_node_impl<OptionsT> &;
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
2016-09-11 18:20:53 +02:00
|
|
|
template <class OptionsT> struct IteratorTraits<OptionsT, true> {
|
2017-06-14 00:11:49 +02:00
|
|
|
using value_type = const typename OptionsT::value_type;
|
|
|
|
using pointer = typename OptionsT::const_pointer;
|
|
|
|
using reference = typename OptionsT::const_reference;
|
|
|
|
using node_pointer = const ilist_node_impl<OptionsT> *;
|
|
|
|
using node_reference = const ilist_node_impl<OptionsT> &;
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
|
|
|
|
2016-09-10 18:55:06 +02:00
|
|
|
template <bool IsReverse> struct IteratorHelper;
|
|
|
|
template <> struct IteratorHelper<false> : ilist_detail::NodeAccess {
|
2017-06-14 00:11:49 +02:00
|
|
|
using Access = ilist_detail::NodeAccess;
|
|
|
|
|
2016-09-10 18:55:06 +02:00
|
|
|
template <class T> static void increment(T *&I) { I = Access::getNext(*I); }
|
|
|
|
template <class T> static void decrement(T *&I) { I = Access::getPrev(*I); }
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
2016-09-10 18:55:06 +02:00
|
|
|
template <> struct IteratorHelper<true> : ilist_detail::NodeAccess {
|
2017-06-14 00:11:49 +02:00
|
|
|
using Access = ilist_detail::NodeAccess;
|
|
|
|
|
2016-09-10 18:55:06 +02:00
|
|
|
template <class T> static void increment(T *&I) { I = Access::getPrev(*I); }
|
|
|
|
template <class T> static void decrement(T *&I) { I = Access::getNext(*I); }
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace ilist_detail
|
|
|
|
|
|
|
|
/// Iterator for intrusive lists based on ilist_node.
|
2016-09-11 18:20:53 +02:00
|
|
|
template <class OptionsT, bool IsReverse, bool IsConst>
|
|
|
|
class ilist_iterator : ilist_detail::SpecificNodeAccess<OptionsT> {
|
|
|
|
friend ilist_iterator<OptionsT, IsReverse, !IsConst>;
|
|
|
|
friend ilist_iterator<OptionsT, !IsReverse, IsConst>;
|
|
|
|
friend ilist_iterator<OptionsT, !IsReverse, !IsConst>;
|
|
|
|
|
2017-06-14 00:11:49 +02:00
|
|
|
using Traits = ilist_detail::IteratorTraits<OptionsT, IsConst>;
|
|
|
|
using Access = ilist_detail::SpecificNodeAccess<OptionsT>;
|
2016-09-10 18:55:06 +02:00
|
|
|
|
2016-08-30 03:37:58 +02:00
|
|
|
public:
|
2017-06-14 00:11:49 +02:00
|
|
|
using value_type = typename Traits::value_type;
|
|
|
|
using pointer = typename Traits::pointer;
|
|
|
|
using reference = typename Traits::reference;
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
using const_pointer = typename OptionsT::const_pointer;
|
|
|
|
using const_reference = typename OptionsT::const_reference;
|
2016-08-30 03:37:58 +02:00
|
|
|
|
|
|
|
private:
|
2017-06-14 00:11:49 +02:00
|
|
|
using node_pointer = typename Traits::node_pointer;
|
|
|
|
using node_reference = typename Traits::node_reference;
|
2016-09-11 18:20:53 +02:00
|
|
|
|
2017-06-14 00:11:49 +02:00
|
|
|
node_pointer NodePtr = nullptr;
|
2016-08-30 03:37:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create from an ilist_node.
|
|
|
|
explicit ilist_iterator(node_reference N) : NodePtr(&N) {}
|
|
|
|
|
2016-09-10 18:55:06 +02:00
|
|
|
explicit ilist_iterator(pointer NP) : NodePtr(Access::getNodePtr(NP)) {}
|
|
|
|
explicit ilist_iterator(reference NR) : NodePtr(Access::getNodePtr(&NR)) {}
|
2017-06-14 00:11:49 +02:00
|
|
|
ilist_iterator() = default;
|
2016-08-30 03:37:58 +02:00
|
|
|
|
|
|
|
// This is templated so that we can allow constructing a const iterator from
|
|
|
|
// a nonconst iterator...
|
2016-09-11 18:20:53 +02:00
|
|
|
template <bool RHSIsConst>
|
Use std::foo_t rather than std::foo in LLVM.
Summary: C++14 migration. No functional change.
Reviewers: bkramer, JDevlieghere, lebedev.ri
Subscribers: MatzeB, hiraditya, jkorous, dexonsmith, arphaman, kadircet, lebedev.ri, usaxena95, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D74384
2020-02-11 05:33:08 +01:00
|
|
|
ilist_iterator(const ilist_iterator<OptionsT, IsReverse, RHSIsConst> &RHS,
|
|
|
|
std::enable_if_t<IsConst || !RHSIsConst, void *> = nullptr)
|
2016-09-11 18:20:53 +02:00
|
|
|
: NodePtr(RHS.NodePtr) {}
|
2016-08-30 03:37:58 +02:00
|
|
|
|
|
|
|
// This is templated so that we can allow assigning to a const iterator from
|
|
|
|
// a nonconst iterator...
|
2016-09-11 18:20:53 +02:00
|
|
|
template <bool RHSIsConst>
|
Use std::foo_t rather than std::foo in LLVM.
Summary: C++14 migration. No functional change.
Reviewers: bkramer, JDevlieghere, lebedev.ri
Subscribers: MatzeB, hiraditya, jkorous, dexonsmith, arphaman, kadircet, lebedev.ri, usaxena95, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D74384
2020-02-11 05:33:08 +01:00
|
|
|
std::enable_if_t<IsConst || !RHSIsConst, ilist_iterator &>
|
2016-09-11 18:20:53 +02:00
|
|
|
operator=(const ilist_iterator<OptionsT, IsReverse, RHSIsConst> &RHS) {
|
|
|
|
NodePtr = RHS.NodePtr;
|
2016-08-30 03:37:58 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-02-07 22:03:50 +01:00
|
|
|
/// Explicit conversion between forward/reverse iterators.
|
2016-08-30 03:37:58 +02:00
|
|
|
///
|
2017-02-07 22:03:50 +01:00
|
|
|
/// Translate between forward and reverse iterators without changing range
|
|
|
|
/// boundaries. The resulting iterator will dereference (and have a handle)
|
|
|
|
/// to the previous node, which is somewhat unexpected; but converting the
|
|
|
|
/// two endpoints in a range will give the same range in reverse.
|
|
|
|
///
|
|
|
|
/// This matches std::reverse_iterator conversions.
|
|
|
|
explicit ilist_iterator(
|
|
|
|
const ilist_iterator<OptionsT, !IsReverse, IsConst> &RHS)
|
|
|
|
: ilist_iterator(++RHS.getReverse()) {}
|
|
|
|
|
|
|
|
/// Get a reverse iterator to the same node.
|
|
|
|
///
|
|
|
|
/// Gives a reverse iterator that will dereference (and have a handle) to the
|
|
|
|
/// same node. Converting the endpoint iterators in a range will give a
|
|
|
|
/// different range; for range operations, use the explicit conversions.
|
2016-09-11 18:20:53 +02:00
|
|
|
ilist_iterator<OptionsT, !IsReverse, IsConst> getReverse() const {
|
|
|
|
if (NodePtr)
|
|
|
|
return ilist_iterator<OptionsT, !IsReverse, IsConst>(*NodePtr);
|
|
|
|
return ilist_iterator<OptionsT, !IsReverse, IsConst>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Const-cast.
|
|
|
|
ilist_iterator<OptionsT, IsReverse, false> getNonConst() const {
|
2016-08-30 03:37:58 +02:00
|
|
|
if (NodePtr)
|
2016-09-11 18:20:53 +02:00
|
|
|
return ilist_iterator<OptionsT, IsReverse, false>(
|
|
|
|
const_cast<typename ilist_iterator<OptionsT, IsReverse,
|
|
|
|
false>::node_reference>(*NodePtr));
|
|
|
|
return ilist_iterator<OptionsT, IsReverse, false>();
|
2016-08-30 03:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors...
|
|
|
|
reference operator*() const {
|
|
|
|
assert(!NodePtr->isKnownSentinel());
|
2016-09-10 18:55:06 +02:00
|
|
|
return *Access::getValuePtr(NodePtr);
|
2016-08-30 03:37:58 +02:00
|
|
|
}
|
|
|
|
pointer operator->() const { return &operator*(); }
|
|
|
|
|
|
|
|
// Comparison operators
|
|
|
|
friend bool operator==(const ilist_iterator &LHS, const ilist_iterator &RHS) {
|
|
|
|
return LHS.NodePtr == RHS.NodePtr;
|
|
|
|
}
|
|
|
|
friend bool operator!=(const ilist_iterator &LHS, const ilist_iterator &RHS) {
|
|
|
|
return LHS.NodePtr != RHS.NodePtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment and decrement operators...
|
|
|
|
ilist_iterator &operator--() {
|
2016-09-11 18:20:53 +02:00
|
|
|
NodePtr = IsReverse ? NodePtr->getNext() : NodePtr->getPrev();
|
2016-08-30 03:37:58 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ilist_iterator &operator++() {
|
2016-09-11 18:20:53 +02:00
|
|
|
NodePtr = IsReverse ? NodePtr->getPrev() : NodePtr->getNext();
|
2016-08-30 03:37:58 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ilist_iterator operator--(int) {
|
|
|
|
ilist_iterator tmp = *this;
|
|
|
|
--*this;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
ilist_iterator operator++(int) {
|
|
|
|
ilist_iterator tmp = *this;
|
|
|
|
++*this;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the underlying ilist_node.
|
|
|
|
node_pointer getNodePtr() const { return static_cast<node_pointer>(NodePtr); }
|
2016-09-11 19:12:28 +02:00
|
|
|
|
|
|
|
/// Check for end. Only valid if ilist_sentinel_tracking<true>.
|
|
|
|
bool isEnd() const { return NodePtr ? NodePtr->isSentinel() : false; }
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename From> struct simplify_type;
|
|
|
|
|
|
|
|
/// Allow ilist_iterators to convert into pointers to a node automatically when
|
|
|
|
/// used by the dyn_cast, cast, isa mechanisms...
|
|
|
|
///
|
|
|
|
/// FIXME: remove this, since there is no implicit conversion to NodeTy.
|
2016-09-11 18:20:53 +02:00
|
|
|
template <class OptionsT, bool IsConst>
|
|
|
|
struct simplify_type<ilist_iterator<OptionsT, false, IsConst>> {
|
2017-06-14 00:11:49 +02:00
|
|
|
using iterator = ilist_iterator<OptionsT, false, IsConst>;
|
|
|
|
using SimpleType = typename iterator::pointer;
|
2016-08-30 03:37:58 +02:00
|
|
|
|
2016-09-11 18:20:53 +02:00
|
|
|
static SimpleType getSimplifiedValue(const iterator &Node) { return &*Node; }
|
2016-08-30 03:37:58 +02:00
|
|
|
};
|
2016-09-11 18:20:53 +02:00
|
|
|
template <class OptionsT, bool IsConst>
|
|
|
|
struct simplify_type<const ilist_iterator<OptionsT, false, IsConst>>
|
|
|
|
: simplify_type<ilist_iterator<OptionsT, false, IsConst>> {};
|
2016-08-30 03:37:58 +02:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_ADT_ILIST_ITERATOR_H
|