ilist_node.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/ilist_node.h - Intrusive Linked List Helper -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the ilist_node class template, which is a convenient
  16. /// base class for creating classes that can be used with ilists.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_ILIST_NODE_H
  20. #define LLVM_ADT_ILIST_NODE_H
  21. #include "llvm/ADT/ilist_node_base.h"
  22. #include "llvm/ADT/ilist_node_options.h"
  23. namespace llvm {
  24. namespace ilist_detail {
  25. struct NodeAccess;
  26. } // end namespace ilist_detail
  27. template <class OptionsT, bool IsReverse, bool IsConst> class ilist_iterator;
  28. template <class OptionsT> class ilist_sentinel;
  29. /// Implementation for an ilist node.
  30. ///
  31. /// Templated on an appropriate \a ilist_detail::node_options, usually computed
  32. /// by \a ilist_detail::compute_node_options.
  33. ///
  34. /// This is a wrapper around \a ilist_node_base whose main purpose is to
  35. /// provide type safety: you can't insert nodes of \a ilist_node_impl into the
  36. /// wrong \a simple_ilist or \a iplist.
  37. template <class OptionsT> class ilist_node_impl : OptionsT::node_base_type {
  38. using value_type = typename OptionsT::value_type;
  39. using node_base_type = typename OptionsT::node_base_type;
  40. using list_base_type = typename OptionsT::list_base_type;
  41. friend typename OptionsT::list_base_type;
  42. friend struct ilist_detail::NodeAccess;
  43. friend class ilist_sentinel<OptionsT>;
  44. friend class ilist_iterator<OptionsT, false, false>;
  45. friend class ilist_iterator<OptionsT, false, true>;
  46. friend class ilist_iterator<OptionsT, true, false>;
  47. friend class ilist_iterator<OptionsT, true, true>;
  48. protected:
  49. using self_iterator = ilist_iterator<OptionsT, false, false>;
  50. using const_self_iterator = ilist_iterator<OptionsT, false, true>;
  51. using reverse_self_iterator = ilist_iterator<OptionsT, true, false>;
  52. using const_reverse_self_iterator = ilist_iterator<OptionsT, true, true>;
  53. ilist_node_impl() = default;
  54. private:
  55. ilist_node_impl *getPrev() {
  56. return static_cast<ilist_node_impl *>(node_base_type::getPrev());
  57. }
  58. ilist_node_impl *getNext() {
  59. return static_cast<ilist_node_impl *>(node_base_type::getNext());
  60. }
  61. const ilist_node_impl *getPrev() const {
  62. return static_cast<ilist_node_impl *>(node_base_type::getPrev());
  63. }
  64. const ilist_node_impl *getNext() const {
  65. return static_cast<ilist_node_impl *>(node_base_type::getNext());
  66. }
  67. void setPrev(ilist_node_impl *N) { node_base_type::setPrev(N); }
  68. void setNext(ilist_node_impl *N) { node_base_type::setNext(N); }
  69. public:
  70. self_iterator getIterator() { return self_iterator(*this); }
  71. const_self_iterator getIterator() const { return const_self_iterator(*this); }
  72. reverse_self_iterator getReverseIterator() {
  73. return reverse_self_iterator(*this);
  74. }
  75. const_reverse_self_iterator getReverseIterator() const {
  76. return const_reverse_self_iterator(*this);
  77. }
  78. // Under-approximation, but always available for assertions.
  79. using node_base_type::isKnownSentinel;
  80. /// Check whether this is the sentinel node.
  81. ///
  82. /// This requires sentinel tracking to be explicitly enabled. Use the
  83. /// ilist_sentinel_tracking<true> option to get this API.
  84. bool isSentinel() const {
  85. static_assert(OptionsT::is_sentinel_tracking_explicit,
  86. "Use ilist_sentinel_tracking<true> to enable isSentinel()");
  87. return node_base_type::isSentinel();
  88. }
  89. };
  90. /// An intrusive list node.
  91. ///
  92. /// A base class to enable membership in intrusive lists, including \a
  93. /// simple_ilist, \a iplist, and \a ilist. The first template parameter is the
  94. /// \a value_type for the list.
  95. ///
  96. /// An ilist node can be configured with compile-time options to change
  97. /// behaviour and/or add API.
  98. ///
  99. /// By default, an \a ilist_node knows whether it is the list sentinel (an
  100. /// instance of \a ilist_sentinel) if and only if
  101. /// LLVM_ENABLE_ABI_BREAKING_CHECKS. The function \a isKnownSentinel() always
  102. /// returns \c false tracking is off. Sentinel tracking steals a bit from the
  103. /// "prev" link, which adds a mask operation when decrementing an iterator, but
  104. /// enables bug-finding assertions in \a ilist_iterator.
  105. ///
  106. /// To turn sentinel tracking on all the time, pass in the
  107. /// ilist_sentinel_tracking<true> template parameter. This also enables the \a
  108. /// isSentinel() function. The same option must be passed to the intrusive
  109. /// list. (ilist_sentinel_tracking<false> turns sentinel tracking off all the
  110. /// time.)
  111. ///
  112. /// A type can inherit from ilist_node multiple times by passing in different
  113. /// \a ilist_tag options. This allows a single instance to be inserted into
  114. /// multiple lists simultaneously, where each list is given the same tag.
  115. ///
  116. /// \example
  117. /// struct A {};
  118. /// struct B {};
  119. /// struct N : ilist_node<N, ilist_tag<A>>, ilist_node<N, ilist_tag<B>> {};
  120. ///
  121. /// void foo() {
  122. /// simple_ilist<N, ilist_tag<A>> ListA;
  123. /// simple_ilist<N, ilist_tag<B>> ListB;
  124. /// N N1;
  125. /// ListA.push_back(N1);
  126. /// ListB.push_back(N1);
  127. /// }
  128. /// \endexample
  129. ///
  130. /// See \a is_valid_option for steps on adding a new option.
  131. template <class T, class... Options>
  132. class ilist_node
  133. : public ilist_node_impl<
  134. typename ilist_detail::compute_node_options<T, Options...>::type> {
  135. static_assert(ilist_detail::check_options<Options...>::value,
  136. "Unrecognized node option!");
  137. };
  138. namespace ilist_detail {
  139. /// An access class for ilist_node private API.
  140. ///
  141. /// This gives access to the private parts of ilist nodes. Nodes for an ilist
  142. /// should friend this class if they inherit privately from ilist_node.
  143. ///
  144. /// Using this class outside of the ilist implementation is unsupported.
  145. struct NodeAccess {
  146. protected:
  147. template <class OptionsT>
  148. static ilist_node_impl<OptionsT> *getNodePtr(typename OptionsT::pointer N) {
  149. return N;
  150. }
  151. template <class OptionsT>
  152. static const ilist_node_impl<OptionsT> *
  153. getNodePtr(typename OptionsT::const_pointer N) {
  154. return N;
  155. }
  156. template <class OptionsT>
  157. static typename OptionsT::pointer getValuePtr(ilist_node_impl<OptionsT> *N) {
  158. return static_cast<typename OptionsT::pointer>(N);
  159. }
  160. template <class OptionsT>
  161. static typename OptionsT::const_pointer
  162. getValuePtr(const ilist_node_impl<OptionsT> *N) {
  163. return static_cast<typename OptionsT::const_pointer>(N);
  164. }
  165. template <class OptionsT>
  166. static ilist_node_impl<OptionsT> *getPrev(ilist_node_impl<OptionsT> &N) {
  167. return N.getPrev();
  168. }
  169. template <class OptionsT>
  170. static ilist_node_impl<OptionsT> *getNext(ilist_node_impl<OptionsT> &N) {
  171. return N.getNext();
  172. }
  173. template <class OptionsT>
  174. static const ilist_node_impl<OptionsT> *
  175. getPrev(const ilist_node_impl<OptionsT> &N) {
  176. return N.getPrev();
  177. }
  178. template <class OptionsT>
  179. static const ilist_node_impl<OptionsT> *
  180. getNext(const ilist_node_impl<OptionsT> &N) {
  181. return N.getNext();
  182. }
  183. };
  184. template <class OptionsT> struct SpecificNodeAccess : NodeAccess {
  185. protected:
  186. using pointer = typename OptionsT::pointer;
  187. using const_pointer = typename OptionsT::const_pointer;
  188. using node_type = ilist_node_impl<OptionsT>;
  189. static node_type *getNodePtr(pointer N) {
  190. return NodeAccess::getNodePtr<OptionsT>(N);
  191. }
  192. static const node_type *getNodePtr(const_pointer N) {
  193. return NodeAccess::getNodePtr<OptionsT>(N);
  194. }
  195. static pointer getValuePtr(node_type *N) {
  196. return NodeAccess::getValuePtr<OptionsT>(N);
  197. }
  198. static const_pointer getValuePtr(const node_type *N) {
  199. return NodeAccess::getValuePtr<OptionsT>(N);
  200. }
  201. };
  202. } // end namespace ilist_detail
  203. template <class OptionsT>
  204. class ilist_sentinel : public ilist_node_impl<OptionsT> {
  205. public:
  206. ilist_sentinel() {
  207. this->initializeSentinel();
  208. reset();
  209. }
  210. void reset() {
  211. this->setPrev(this);
  212. this->setNext(this);
  213. }
  214. bool empty() const { return this == this->getPrev(); }
  215. };
  216. /// An ilist node that can access its parent list.
  217. ///
  218. /// Requires \c NodeTy to have \a getParent() to find the parent node, and the
  219. /// \c ParentTy to have \a getSublistAccess() to get a reference to the list.
  220. template <typename NodeTy, typename ParentTy, class... Options>
  221. class ilist_node_with_parent : public ilist_node<NodeTy, Options...> {
  222. protected:
  223. ilist_node_with_parent() = default;
  224. private:
  225. /// Forward to NodeTy::getParent().
  226. ///
  227. /// Note: do not use the name "getParent()". We want a compile error
  228. /// (instead of recursion) when the subclass fails to implement \a
  229. /// getParent().
  230. const ParentTy *getNodeParent() const {
  231. return static_cast<const NodeTy *>(this)->getParent();
  232. }
  233. public:
  234. /// @name Adjacent Node Accessors
  235. /// @{
  236. /// Get the previous node, or \c nullptr for the list head.
  237. NodeTy *getPrevNode() {
  238. // Should be separated to a reused function, but then we couldn't use auto
  239. // (and would need the type of the list).
  240. const auto &List =
  241. getNodeParent()->*(ParentTy::getSublistAccess((NodeTy *)nullptr));
  242. return List.getPrevNode(*static_cast<NodeTy *>(this));
  243. }
  244. /// Get the previous node, or \c nullptr for the list head.
  245. const NodeTy *getPrevNode() const {
  246. return const_cast<ilist_node_with_parent *>(this)->getPrevNode();
  247. }
  248. /// Get the next node, or \c nullptr for the list tail.
  249. NodeTy *getNextNode() {
  250. // Should be separated to a reused function, but then we couldn't use auto
  251. // (and would need the type of the list).
  252. const auto &List =
  253. getNodeParent()->*(ParentTy::getSublistAccess((NodeTy *)nullptr));
  254. return List.getNextNode(*static_cast<NodeTy *>(this));
  255. }
  256. /// Get the next node, or \c nullptr for the list tail.
  257. const NodeTy *getNextNode() const {
  258. return const_cast<ilist_node_with_parent *>(this)->getNextNode();
  259. }
  260. /// @}
  261. };
  262. } // end namespace llvm
  263. #endif // LLVM_ADT_ILIST_NODE_H
  264. #ifdef __GNUC__
  265. #pragma GCC diagnostic pop
  266. #endif