8f0538bcf6e7e7ea5c0cd09e7061f4745a44346d
[oota-llvm.git] / include / llvm / ADT / ilist_node.h
1 //==-- llvm/ADT/ilist_node.h - Intrusive Linked List Helper ------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_ILIST_NODE_H
16 #define LLVM_ADT_ILIST_NODE_H
17
18 namespace llvm {
19
20 template<typename NodeTy>
21 struct ilist_traits;
22 template <typename NodeTy> struct ilist_embedded_sentinel_traits;
23 template <typename NodeTy> struct ilist_half_embedded_sentinel_traits;
24
25 /// ilist_half_node - Base class that provides prev services for sentinels.
26 ///
27 template<typename NodeTy>
28 class ilist_half_node {
29   friend struct ilist_traits<NodeTy>;
30   friend struct ilist_half_embedded_sentinel_traits<NodeTy>;
31   NodeTy *Prev;
32 protected:
33   NodeTy *getPrev() { return Prev; }
34   const NodeTy *getPrev() const { return Prev; }
35   void setPrev(NodeTy *P) { Prev = P; }
36   ilist_half_node() : Prev(nullptr) {}
37 };
38
39 template<typename NodeTy>
40 struct ilist_nextprev_traits;
41
42 template <typename NodeTy> class ilist_iterator;
43
44 /// ilist_node - Base class that provides next/prev services for nodes
45 /// that use ilist_nextprev_traits or ilist_default_traits.
46 ///
47 template<typename NodeTy>
48 class ilist_node : private ilist_half_node<NodeTy> {
49   friend struct ilist_nextprev_traits<NodeTy>;
50   friend struct ilist_traits<NodeTy>;
51   friend struct ilist_half_embedded_sentinel_traits<NodeTy>;
52   friend struct ilist_embedded_sentinel_traits<NodeTy>;
53   NodeTy *Next;
54   NodeTy *getNext() { return Next; }
55   const NodeTy *getNext() const { return Next; }
56   void setNext(NodeTy *N) { Next = N; }
57 protected:
58   ilist_node() : Next(nullptr) {}
59
60 public:
61   ilist_iterator<NodeTy> getIterator() {
62     // FIXME: Stop downcasting to create the iterator (potential UB).
63     return ilist_iterator<NodeTy>(static_cast<NodeTy *>(this));
64   }
65   ilist_iterator<const NodeTy> getIterator() const {
66     // FIXME: Stop downcasting to create the iterator (potential UB).
67     return ilist_iterator<const NodeTy>(static_cast<const NodeTy *>(this));
68   }
69
70   /// @name Adjacent Node Accessors
71   /// @{
72
73   /// \brief Get the previous node, or 0 for the list head.
74   NodeTy *getPrevNode() {
75     NodeTy *Prev = this->getPrev();
76
77     // Check for sentinel.
78     if (!Prev->getNext())
79       return nullptr;
80
81     return Prev;
82   }
83
84   /// \brief Get the previous node, or 0 for the list head.
85   const NodeTy *getPrevNode() const {
86     const NodeTy *Prev = this->getPrev();
87
88     // Check for sentinel.
89     if (!Prev->getNext())
90       return nullptr;
91
92     return Prev;
93   }
94
95   /// \brief Get the next node, or 0 for the list tail.
96   NodeTy *getNextNode() {
97     NodeTy *Next = getNext();
98
99     // Check for sentinel.
100     if (!Next->getNext())
101       return nullptr;
102
103     return Next;
104   }
105
106   /// \brief Get the next node, or 0 for the list tail.
107   const NodeTy *getNextNode() const {
108     const NodeTy *Next = getNext();
109
110     // Check for sentinel.
111     if (!Next->getNext())
112       return nullptr;
113
114     return Next;
115   }
116
117   /// @}
118 };
119
120 } // End llvm namespace
121
122 #endif