1 //===- llvm/ADT/ilist_node.h - Intrusive Linked List Helper -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ilist_node class template, which is a convenient
11 // base class for creating classes that can be used with ilists.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_ILIST_NODE_H
16 #define LLVM_ADT_ILIST_NODE_H
18 #include "llvm/ADT/ilist_node_base.h"
19 #include "llvm/ADT/ilist_node_options.h"
23 namespace ilist_detail {
27 } // end namespace ilist_detail
29 template <class OptionsT, bool IsReverse, bool IsConst> class ilist_iterator;
30 template <class OptionsT> class ilist_sentinel;
32 /// Implementation for an ilist node.
34 /// Templated on an appropriate \a ilist_detail::node_options, usually computed
35 /// by \a ilist_detail::compute_node_options.
37 /// This is a wrapper around \a ilist_node_base whose main purpose is to
38 /// provide type safety: you can't insert nodes of \a ilist_node_impl into the
39 /// wrong \a simple_ilist or \a iplist.
40 template <class OptionsT> class ilist_node_impl : OptionsT::node_base_type {
41 using value_type = typename OptionsT::value_type;
42 using node_base_type = typename OptionsT::node_base_type;
43 using list_base_type = typename OptionsT::list_base_type;
45 friend typename OptionsT::list_base_type;
46 friend struct ilist_detail::NodeAccess;
47 friend class ilist_sentinel<OptionsT>;
48 friend class ilist_iterator<OptionsT, false, false>;
49 friend class ilist_iterator<OptionsT, false, true>;
50 friend class ilist_iterator<OptionsT, true, false>;
51 friend class ilist_iterator<OptionsT, true, true>;
54 using self_iterator = ilist_iterator<OptionsT, false, false>;
55 using const_self_iterator = ilist_iterator<OptionsT, false, true>;
56 using reverse_self_iterator = ilist_iterator<OptionsT, true, false>;
57 using const_reverse_self_iterator = ilist_iterator<OptionsT, true, true>;
59 ilist_node_impl() = default;
62 ilist_node_impl *getPrev() {
63 return static_cast<ilist_node_impl *>(node_base_type::getPrev());
66 ilist_node_impl *getNext() {
67 return static_cast<ilist_node_impl *>(node_base_type::getNext());
70 const ilist_node_impl *getPrev() const {
71 return static_cast<ilist_node_impl *>(node_base_type::getPrev());
74 const ilist_node_impl *getNext() const {
75 return static_cast<ilist_node_impl *>(node_base_type::getNext());
78 void setPrev(ilist_node_impl *N) { node_base_type::setPrev(N); }
79 void setNext(ilist_node_impl *N) { node_base_type::setNext(N); }
82 self_iterator getIterator() { return self_iterator(*this); }
83 const_self_iterator getIterator() const { return const_self_iterator(*this); }
85 reverse_self_iterator getReverseIterator() {
86 return reverse_self_iterator(*this);
89 const_reverse_self_iterator getReverseIterator() const {
90 return const_reverse_self_iterator(*this);
93 // Under-approximation, but always available for assertions.
94 using node_base_type::isKnownSentinel;
96 /// Check whether this is the sentinel node.
98 /// This requires sentinel tracking to be explicitly enabled. Use the
99 /// ilist_sentinel_tracking<true> option to get this API.
100 bool isSentinel() const {
101 static_assert(OptionsT::is_sentinel_tracking_explicit,
102 "Use ilist_sentinel_tracking<true> to enable isSentinel()");
103 return node_base_type::isSentinel();
107 /// An intrusive list node.
109 /// A base class to enable membership in intrusive lists, including \a
110 /// simple_ilist, \a iplist, and \a ilist. The first template parameter is the
111 /// \a value_type for the list.
113 /// An ilist node can be configured with compile-time options to change
114 /// behaviour and/or add API.
116 /// By default, an \a ilist_node knows whether it is the list sentinel (an
117 /// instance of \a ilist_sentinel) if and only if
118 /// LLVM_ENABLE_ABI_BREAKING_CHECKS. The function \a isKnownSentinel() always
119 /// returns \c false tracking is off. Sentinel tracking steals a bit from the
120 /// "prev" link, which adds a mask operation when decrementing an iterator, but
121 /// enables bug-finding assertions in \a ilist_iterator.
123 /// To turn sentinel tracking on all the time, pass in the
124 /// ilist_sentinel_tracking<true> template parameter. This also enables the \a
125 /// isSentinel() function. The same option must be passed to the intrusive
126 /// list. (ilist_sentinel_tracking<false> turns sentinel tracking off all the
129 /// A type can inherit from ilist_node multiple times by passing in different
130 /// \a ilist_tag options. This allows a single instance to be inserted into
131 /// multiple lists simultaneously, where each list is given the same tag.
136 /// struct N : ilist_node<N, ilist_tag<A>>, ilist_node<N, ilist_tag<B>> {};
139 /// simple_ilist<N, ilist_tag<A>> ListA;
140 /// simple_ilist<N, ilist_tag<B>> ListB;
142 /// ListA.push_back(N1);
143 /// ListB.push_back(N1);
147 /// See \a is_valid_option for steps on adding a new option.
148 template <class T, class... Options>
150 : public ilist_node_impl<
151 typename ilist_detail::compute_node_options<T, Options...>::type> {
152 static_assert(ilist_detail::check_options<Options...>::value,
153 "Unrecognized node option!");
156 namespace ilist_detail {
158 /// An access class for ilist_node private API.
160 /// This gives access to the private parts of ilist nodes. Nodes for an ilist
161 /// should friend this class if they inherit privately from ilist_node.
163 /// Using this class outside of the ilist implementation is unsupported.
166 template <class OptionsT>
167 static ilist_node_impl<OptionsT> *getNodePtr(typename OptionsT::pointer N) {
171 template <class OptionsT>
172 static const ilist_node_impl<OptionsT> *
173 getNodePtr(typename OptionsT::const_pointer N) {
177 template <class OptionsT>
178 static typename OptionsT::pointer getValuePtr(ilist_node_impl<OptionsT> *N) {
179 return static_cast<typename OptionsT::pointer>(N);
182 template <class OptionsT>
183 static typename OptionsT::const_pointer
184 getValuePtr(const ilist_node_impl<OptionsT> *N) {
185 return static_cast<typename OptionsT::const_pointer>(N);
188 template <class OptionsT>
189 static ilist_node_impl<OptionsT> *getPrev(ilist_node_impl<OptionsT> &N) {
193 template <class OptionsT>
194 static ilist_node_impl<OptionsT> *getNext(ilist_node_impl<OptionsT> &N) {
198 template <class OptionsT>
199 static const ilist_node_impl<OptionsT> *
200 getPrev(const ilist_node_impl<OptionsT> &N) {
204 template <class OptionsT>
205 static const ilist_node_impl<OptionsT> *
206 getNext(const ilist_node_impl<OptionsT> &N) {
211 template <class OptionsT> struct SpecificNodeAccess : NodeAccess {
213 using pointer = typename OptionsT::pointer;
214 using const_pointer = typename OptionsT::const_pointer;
215 using node_type = ilist_node_impl<OptionsT>;
217 static node_type *getNodePtr(pointer N) {
218 return NodeAccess::getNodePtr<OptionsT>(N);
221 static const node_type *getNodePtr(const_pointer N) {
222 return NodeAccess::getNodePtr<OptionsT>(N);
225 static pointer getValuePtr(node_type *N) {
226 return NodeAccess::getValuePtr<OptionsT>(N);
229 static const_pointer getValuePtr(const node_type *N) {
230 return NodeAccess::getValuePtr<OptionsT>(N);
234 } // end namespace ilist_detail
236 template <class OptionsT>
237 class ilist_sentinel : public ilist_node_impl<OptionsT> {
240 this->initializeSentinel();
249 bool empty() const { return this == this->getPrev(); }
252 /// An ilist node that can access its parent list.
254 /// Requires \c NodeTy to have \a getParent() to find the parent node, and the
255 /// \c ParentTy to have \a getSublistAccess() to get a reference to the list.
256 template <typename NodeTy, typename ParentTy, class... Options>
257 class ilist_node_with_parent : public ilist_node<NodeTy, Options...> {
259 ilist_node_with_parent() = default;
262 /// Forward to NodeTy::getParent().
264 /// Note: do not use the name "getParent()". We want a compile error
265 /// (instead of recursion) when the subclass fails to implement \a
267 const ParentTy *getNodeParent() const {
268 return static_cast<const NodeTy *>(this)->getParent();
272 /// @name Adjacent Node Accessors
274 /// \brief Get the previous node, or \c nullptr for the list head.
275 NodeTy *getPrevNode() {
276 // Should be separated to a reused function, but then we couldn't use auto
277 // (and would need the type of the list).
279 getNodeParent()->*(ParentTy::getSublistAccess((NodeTy *)nullptr));
280 return List.getPrevNode(*static_cast<NodeTy *>(this));
283 /// \brief Get the previous node, or \c nullptr for the list head.
284 const NodeTy *getPrevNode() const {
285 return const_cast<ilist_node_with_parent *>(this)->getPrevNode();
288 /// \brief Get the next node, or \c nullptr for the list tail.
289 NodeTy *getNextNode() {
290 // Should be separated to a reused function, but then we couldn't use auto
291 // (and would need the type of the list).
293 getNodeParent()->*(ParentTy::getSublistAccess((NodeTy *)nullptr));
294 return List.getNextNode(*static_cast<NodeTy *>(this));
297 /// \brief Get the next node, or \c nullptr for the list tail.
298 const NodeTy *getNextNode() const {
299 return const_cast<ilist_node_with_parent *>(this)->getNextNode();
304 } // end namespace llvm
306 #endif // LLVM_ADT_ILIST_NODE_H