Tidy up #includes.
[oota-llvm.git] / include / llvm / ADT / alist_node.h
1 //==- llvm/ADT/alist_node.h - Next/Prev helper class for alist ---*- 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 alist_node class template, which is used by the alist
11 // class template to provide next/prev pointers for arbitrary objects.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_ALIST_NODE_H
16 #define LLVM_ADT_ALIST_NODE_H
17
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/Support/AlignOf.h"
20 #include "llvm/Support/DataTypes.h"
21 #include <cassert>
22
23 namespace llvm {
24
25 /// alist_node - This is a utility class used by alist. It holds prev and next
26 /// pointers for use with ilists, as well as storage for objects as large as
27 /// LargestT, that are in T's inheritance tree.
28 ///
29 template<class T, class LargestT = T>
30 class alist_node {
31   alist_node *Prev, *Next;
32
33 public:
34   alist_node() : Prev(0), Next(0) {}
35
36   alist_node *getPrev() const { return Prev; }
37   alist_node *getNext() const { return Next; }
38   void setPrev(alist_node *N) { Prev = N; }
39   void setNext(alist_node *N) { Next = N; }
40
41   union {
42     char Bytes[sizeof(LargestT)];
43     long long L;
44     void *P;
45   } Storage;
46
47   template<class SubClass>
48   SubClass *getElement(SubClass *) {
49     assert(sizeof(SubClass) <= sizeof(LargestT));
50     assert(unsigned(AlignOf<SubClass>::Alignment) <=
51            unsigned(AlignOf<LargestT>::Alignment));
52     return reinterpret_cast<SubClass*>(&Storage.Bytes);
53   }
54
55   template<class SubClass>
56   const SubClass *getElement(SubClass *) const {
57     assert(sizeof(SubClass) <= sizeof(LargestT));
58     assert(unsigned(AlignOf<SubClass>::Alignment) <=
59            unsigned(AlignOf<LargestT>::Alignment));
60     return reinterpret_cast<const SubClass*>(&Storage.Bytes);
61   }
62
63   // This code essentially does offsetof, but actual offsetof hits an ICE in
64   // GCC 4.0 relating to offsetof being used inside a template.
65   static alist_node* getNode(T *D) {
66     return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
67                                                 (uintptr_t)&getNull()->Storage);
68   }
69   static const alist_node* getNode(const T *D) {
70     return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
71                                                 (uintptr_t)&getNull()->Storage);
72   }
73 private:
74   static alist_node* getNull() { return 0; }
75 };
76
77 // A specialization of ilist_traits for alist_nodes.
78 template<class T, class LargestT>
79 class ilist_traits<alist_node<T, LargestT> > {
80   typedef alist_node<T, LargestT> NodeTy;
81
82 protected:
83   // Allocate a sentinel inside the traits class. This works
84   // because iplist carries an instance of the traits class.
85   NodeTy Sentinel;
86
87 public:
88   static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
89   static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
90   static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
91   static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
92
93   static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
94   static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
95
96   NodeTy *createSentinel() const {
97     assert(Sentinel.getPrev() == 0);
98     assert(Sentinel.getNext() == 0);
99     return const_cast<NodeTy*>(&Sentinel);
100   }
101
102   void destroySentinel(NodeTy *N) {
103     assert(N == &Sentinel);
104     Sentinel.setPrev(0);
105     Sentinel.setNext(0);
106   }
107
108   void addNodeToList(NodeTy *N) {}
109   void removeNodeFromList(NodeTy *N) {}
110   void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
111                              ilist_iterator<NodeTy> first,
112                              ilist_iterator<NodeTy> last) {}
113
114   // Ideally we wouldn't implement this, but ilist's clear calls it,
115   // which is called from ilist's destructor. We won't ever call
116   // either of those with a non-empty list, but statically this
117   // method needs to exist.
118   void deleteNode(NodeTy *N) { assert(0); }
119
120 private:
121   static NodeTy *createNode(const NodeTy &V); // do not implement
122 };
123
124 }
125
126 #endif