aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT/alist_node.h
blob: fcdf22c45c120968e03d1fb66983446b0535e9a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//==- llvm/ADT/alist_node.h - Next/Prev helper class for alist ---*- C++ -*-==//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the alist_node class template, which is used by the alist
// class template to provide next/prev pointers for arbitrary objects.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_ALIST_NODE_H
#define LLVM_ADT_ALIST_NODE_H

#include "llvm/ADT/ilist.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/DataTypes.h"
#include <cassert>

namespace llvm {

/// alist_node - This is a utility class used by alist. It holds prev and next
/// pointers for use with ilists, as well as storage for objects as large as
/// LargestT, that are in T's inheritance tree.
///
template<class T, class LargestT = T>
class alist_node {
  alist_node *Prev, *Next;

public:
  alist_node() : Prev(0), Next(0) {}

  alist_node *getPrev() const { return Prev; }
  alist_node *getNext() const { return Next; }
  void setPrev(alist_node *N) { Prev = N; }
  void setNext(alist_node *N) { Next = N; }

  union {
    char Bytes[sizeof(LargestT)];
    long long L;
    void *P;
  } Storage;

  template<class SubClass>
  SubClass *getElement(SubClass *) {
    assert(sizeof(SubClass) <= sizeof(LargestT));
    return reinterpret_cast<SubClass*>(&Storage.Bytes);
  }

  template<class SubClass>
  const SubClass *getElement(SubClass *) const {
    assert(sizeof(SubClass) <= sizeof(LargestT));
    return reinterpret_cast<const SubClass*>(&Storage.Bytes);
  }

  // This code essentially does offsetof, but actual offsetof hits an ICE in
  // GCC 4.0 relating to offsetof being used inside a template.
  static alist_node* getNode(T *D) {
    return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
                                                (uintptr_t)&getNull()->Storage);
  }
  static const alist_node* getNode(const T *D) {
    return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
                                                (uintptr_t)&getNull()->Storage);
  }
private:
  static alist_node* getNull() { return 0; }
};

// A specialization of ilist_traits for alist_nodes.
template<class T, class LargestT>
class ilist_traits<alist_node<T, LargestT> > {
public:
  typedef alist_node<T, LargestT> NodeTy;

protected:
  // Allocate a sentinel inside the traits class. This works
  // because iplist carries an instance of the traits class.
  NodeTy Sentinel;

public:
  static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
  static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
  static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
  static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }

  static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
  static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }

  NodeTy *createSentinel() const {
    assert(Sentinel.getPrev() == 0);
    assert(Sentinel.getNext() == 0);
    return const_cast<NodeTy*>(&Sentinel);
  }

  void destroySentinel(NodeTy *N) {
    assert(N == &Sentinel); N = N;
    Sentinel.setPrev(0);
    Sentinel.setNext(0);
  }

  void addNodeToList(NodeTy *) {}
  void removeNodeFromList(NodeTy *) {}
  void transferNodesFromList(iplist<NodeTy, ilist_traits> &,
                             ilist_iterator<NodeTy> /*first*/,
                             ilist_iterator<NodeTy> /*last*/) {}

  // Ideally we wouldn't implement this, but ilist's clear calls it,
  // which is called from ilist's destructor. We won't ever call
  // either of those with a non-empty list, but statically this
  // method needs to exist.
  void deleteNode(NodeTy *) { assert(0); }

private:
  static NodeTy *createNode(const NodeTy &V); // do not implement
};

}

#endif