ilist_node.h 10 KB

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