Force postdom to be linked into opt and bugpoint, even though it is no longer used...
[oota-llvm.git] / include / llvm / ADT / ilist
index 09c951c2572d57e9818cd5d4f47595815a898347..d465cbfc7152d30719c16cc515f499dc23cfe1d3 100644 (file)
@@ -1,7 +1,14 @@
-//===-- <Support/ilist> - Intrusive Linked List Template ---------*- C++ -*--=//
+//===-- llvm/ADT/ilist - Intrusive Linked List Template ---------*- 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 classes to implement an intrusive doubly linked list class
-// (ie each node of the list must contain a next and previous field for the
+// (i.e. each node of the list must contain a next and previous field for the
 // list.
 //
 // The ilist_traits trait class is used to gain access to the next and previous
 //
 // The ilist class itself, should be a plug in replacement for list, assuming
 // that the nodes contain next/prev pointers.  This list replacement does not
-// provides a constant time size() method, so be careful to use empty() when you
+// provide a constant time size() method, so be careful to use empty() when you
 // really want to know if it's empty.
 //
 // The ilist class is implemented by allocating a 'tail' node when the list is
-// created (using ilist_traits<>::createEndMarker()).  This tail node is
+// created (using ilist_traits<>::createSentinel()).  This tail node is
 // absolutely required because the user must be able to compute end()-1. Because
 // of this, users of the direct next/prev links will see an extra link on the
 // end of the list, which should be ignored.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef INCLUDED_SUPPORT_ILIST
-#define INCLUDED_SUPPORT_ILIST
+#ifndef LLVM_ADT_ILIST
+#define LLVM_ADT_ILIST
+
+#include "llvm/ADT/iterator"
+#include <cassert>
+#include <cstdlib>
 
-#include <assert.h>
-#include <iterator>
-#include <algorithm>
+namespace llvm {
 
 template<typename NodeTy, typename Traits> class iplist;
 template<typename NodeTy> class ilist_iterator;
@@ -50,9 +59,10 @@ struct ilist_traits {
   static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
   static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
 
-  static NodeTy *createNode() { return new NodeTy(); }
   static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
 
+  static NodeTy *createSentinel() { return new NodeTy(); }
+  static void destroySentinel(NodeTy *N) { delete N; }
 
   void addNodeToList(NodeTy *NTy) {}
   void removeNodeFromList(NodeTy *NTy) {}
@@ -71,22 +81,20 @@ struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
 //
 template<typename NodeTy>
 class ilist_iterator
-#if __GNUC__ == 3
-  : public std::iterator<std::bidirectional_iterator_tag, NodeTy> {
-  typedef std::iterator<std::bidirectional_iterator_tag, NodeTy> super;
-#else
-  : public std::bidirectional_iterator<NodeTy, ptrdiff_t> {
-  typedef std::bidirectional_iterator<NodeTy, ptrdiff_t> super;
-#endif
+  : public bidirectional_iterator<NodeTy, ptrdiff_t> {
   typedef ilist_traits<NodeTy> Traits;
+  typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
 
+public:
+  typedef size_t size_type;
   typedef typename super::pointer pointer;
   typedef typename super::reference reference;
+private:
   pointer NodePtr;
 public:
-  typedef size_t size_type;
 
   ilist_iterator(pointer NP) : NodePtr(NP) {}
+  ilist_iterator(reference NR) : NodePtr(&NR) {}
   ilist_iterator() : NodePtr(0) {}
 
   // This is templated so that we can allow constructing a const iterator from
@@ -127,7 +135,7 @@ public:
   // Increment and decrement operators...
   ilist_iterator &operator--() {      // predecrement - Back up
     NodePtr = Traits::getPrev(NodePtr);
-    assert(NodePtr && "--'d off the beginning of an ilist!");
+    assert(Traits::getNext(NodePtr) && "--'d off the beginning of an ilist!");
     return *this;
   }
   ilist_iterator &operator++() {      // preincrement - Advance
@@ -146,25 +154,105 @@ public:
     return tmp;
   }
 
-
-  // Dummy operators to make errors apparent...
-  template<class X> void operator+(X Val) {}
-  template<class X> void operator-(X Val) {}
-
   // Internal interface, do not use...
   pointer getNodePtrUnchecked() const { return NodePtr; }
 };
 
+// do not implement. this is to catch errors when people try to use
+// them as random access iterators
+template<typename T>
+void operator-(int, ilist_iterator<T>);
+template<typename T>
+void operator-(ilist_iterator<T>,int);
+
+template<typename T>
+void operator+(int, ilist_iterator<T>);
+template<typename T>
+void operator+(ilist_iterator<T>,int);
+
+// operator!=/operator== - Allow mixed comparisons without dereferencing
+// the iterator, which could very likely be pointing to end().
+template<typename T>
+bool operator!=(const T* LHS, const ilist_iterator<const T> &RHS) {
+  return LHS != RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator==(const T* LHS, const ilist_iterator<const T> &RHS) {
+  return LHS == RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator!=(T* LHS, const ilist_iterator<T> &RHS) {
+  return LHS != RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator==(T* LHS, const ilist_iterator<T> &RHS) {
+  return LHS == RHS.getNodePtrUnchecked();
+}
+
+
+// Allow ilist_iterators to convert into pointers to a node automatically when
+// used by the dyn_cast, cast, isa mechanisms...
+
+template<typename From> struct simplify_type;
+
+template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
+  typedef NodeTy* SimpleType;
+  
+  static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
+    return &*Node;
+  }
+};
+template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
+  typedef NodeTy* SimpleType;
+  
+  static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
+    return &*Node;
+  }
+};
+
 
 //===----------------------------------------------------------------------===//
 //
-// iplist - The subset of list functionality that can safely be used on nodes of
-// polymorphic types, ie a heterogeneus list with a common base class that holds
-// the next/prev pointers...
-//
+/// iplist - The subset of list functionality that can safely be used on nodes
+/// of polymorphic types, i.e. a heterogenous list with a common base class that
+/// holds the next/prev pointers.  The only state of the list itself is a single
+/// pointer to the head of the list.
+///
+/// This list can be in one of three interesting states:
+/// 1. The list may be completely unconstructed.  In this case, the head
+///    pointer is null.  When in this form, any query for an iterator (e.g.
+///    begin() or end()) causes the list to transparently change to state #2.
+/// 2. The list may be empty, but contain a sentinal for the end iterator. This
+///    sentinal is created by the Traits::createSentinel method and is a link
+///    in the list.  When the list is empty, the pointer in the iplist points
+///    to the sentinal.  Once the sentinal is constructed, it
+///    is not destroyed until the list is.
+/// 3. The list may contain actual objects in it, which are stored as a doubly
+///    linked list of nodes.  One invariant of the list is that the predecessor
+///    of the first node in the list always points to the last node in the list,
+///    and the successor pointer for the sentinal (which always stays at the
+///    end of the list) is always null.  
+///
 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
 class iplist : public Traits {
-  NodeTy *Head, *Tail;
+  mutable NodeTy *Head;
+
+  // Use the prev node pointer of 'head' as the tail pointer.  This is really a
+  // circularly linked list where we snip the 'next' link from the sentinel node
+  // back to the first node in the list (to preserve assertions about going off
+  // the end of the list).
+  NodeTy *getTail() { return getPrev(Head); }
+  const NodeTy *getTail() const { return getPrev(Head); }
+  void setTail(NodeTy *N) const { setPrev(Head, N); }
+  
+  /// CreateLazySentinal - This method verifies whether the sentinal for the
+  /// list has been created and lazily makes it if not.
+  void CreateLazySentinal() const {
+    if (Head != 0) return;
+    Head = Traits::createSentinel();
+    setNext(Head, 0);
+    setTail(Head);
+  }
 
   static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
   static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
@@ -181,27 +269,41 @@ public:
   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
   typedef std::reverse_iterator<iterator>  reverse_iterator;
 
-  iplist() : Head(createNode()), Tail(Head) {
-    setNext(Head, 0);
-    setPrev(Head, 0);
+  iplist() : Head(0) {}
+  ~iplist() {
+    if (!Head) return;
+    clear();
+    Traits::destroySentinel(getTail());
   }
-  ~iplist() { clear(); delete Tail; }
 
-  // Iterator creation methods...
-  iterator begin()             { return iterator(Head); }
-  const_iterator begin() const { return const_iterator(Head); }
-  iterator end()               { return iterator(Tail); }
-  const_iterator end() const   { return const_iterator(Tail); }
+  // Iterator creation methods.
+  iterator begin() {
+    CreateLazySentinal(); 
+    return iterator(Head); 
+  }
+  const_iterator begin() const {
+    CreateLazySentinal();
+    return const_iterator(Head);
+  }
+  iterator end() {
+    CreateLazySentinal();
+    return iterator(getTail());
+  }
+  const_iterator end() const {
+    CreateLazySentinal();
+    return const_iterator(getTail());
+  }
 
-  // reverse iterator creation methods...
+  // reverse iterator creation methods.
   reverse_iterator rbegin()            { return reverse_iterator(end()); }
   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
   reverse_iterator rend()              { return reverse_iterator(begin()); }
-  const_reverse_iterator rend() const  {return const_reverse_iterator(begin());}
+  const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
+
 
-  // Miscellaneous inspection routines...
+  // Miscellaneous inspection routines.
   size_type max_size() const { return size_type(-1); }
-  bool empty() const { return Head == Tail; }
+  bool empty() const { return Head == 0 || Head == getTail(); }
 
   // Front and back accessor functions...
   reference front() {
@@ -214,17 +316,16 @@ public:
   }
   reference back() {
     assert(!empty() && "Called back() on empty list!");
-    return *getPrev(Tail);
+    return *getPrev(getTail());
   }
   const_reference back() const {
     assert(!empty() && "Called back() on empty list!");
-    return *getPrev(Tail);
+    return *getPrev(getTail());
   }
 
   void swap(iplist &RHS) {
     abort();     // Swap does not use list traits callback correctly yet!
     std::swap(Head, RHS.Head);
-    std::swap(Tail, RHS.Tail);
   }
 
   iterator insert(iterator where, NodeTy *New) {
@@ -232,7 +333,7 @@ public:
     setNext(New, CurNode);
     setPrev(New, PrevNode);
 
-    if (PrevNode)
+    if (CurNode != Head)  // Is PrevNode off the beginning of the list?
       setNext(PrevNode, New);
     else
       Head = New;
@@ -248,13 +349,21 @@ public:
     NodeTy *NextNode = getNext(Node);
     NodeTy *PrevNode = getPrev(Node);
 
-    if (PrevNode)
+    if (Node != Head)  // Is PrevNode off the beginning of the list?
       setNext(PrevNode, NextNode);
     else
       Head = NextNode;
     setPrev(NextNode, PrevNode);
     IT = NextNode;
-    removeNodeFromList(Node);  // Notify traits that we added a node...
+    removeNodeFromList(Node);  // Notify traits that we removed a node...
+    
+    // Set the next/prev pointers of the current node to null.  This isn't
+    // strictly required, but this catches errors where a node is removed from
+    // an ilist (and potentially deleted) with iterators still pointing at it.
+    // When those iterators are incremented or decremented, they will assert on
+    // the null next/prev pointer instead of "usually working".
+    setNext(Node, 0);
+    setPrev(Node, 0);
     return Node;
   }
 
@@ -276,7 +385,15 @@ private:
   //
   void transfer(iterator position, iplist &L2, iterator first, iterator last) {
     assert(first != last && "Should be checked by callers");
+
     if (position != last) {
+      // Note: we have to be careful about the case when we move the first node
+      // in the list.  This node is the list sentinel node and we can't move it.
+      NodeTy *ThisSentinel = getTail();
+      setTail(0);
+      NodeTy *L2Sentinel = L2.getTail();
+      L2.setTail(0);
+
       // Remove [first, last) from its old position.
       NodeTy *First = &*first, *Prev = getPrev(First);
       NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
@@ -302,6 +419,10 @@ private:
       setPrev(PosNext, Last);
 
       transferNodesFromList(L2, First, PosNext);
+
+      // Now that everything is set, restore the pointers to the list sentinals.
+      L2.setTail(L2Sentinel);
+      setTail(ThisSentinel);
     }
   }
 
@@ -312,13 +433,15 @@ public:
   //
 
   size_type size() const {
-#if __GNUC__ == 3
-    size_type Result = std::distance(begin(), end());
-#else
+    if (Head == 0) return 0; // Don't require construction of sentinal if empty.
+#if __GNUC__ == 2
+    // GCC 2.95 has a broken std::distance
     size_type Result = 0;
     std::distance(begin(), end(), Result);
-#endif
     return Result;
+#else
+    return std::distance(begin(), end());
+#endif
   }
 
   iterator erase(iterator first, iterator last) {
@@ -327,7 +450,7 @@ public:
     return last;
   }
 
-  void clear() { erase(begin(), end()); }
+  void clear() { if (Head) erase(begin(), end()); }
 
   // Front and back inserters...
   void push_front(NodeTy *val) { insert(begin(), val); }
@@ -424,16 +547,16 @@ struct ilist : public iplist<NodeTy> {
 
   ilist() {}
   ilist(const ilist &right) {
-    insert(begin(), right.begin(), right.end());
+    insert(this->begin(), right.begin(), right.end());
   }
   explicit ilist(size_type count) {
-    insert(begin(), count, NodeTy());
+    insert(this->begin(), count, NodeTy());
   } 
   ilist(size_type count, const NodeTy &val) {
-    insert(begin(), count, val);
+    insert(this->begin(), count, val);
   }
   template<class InIt> ilist(InIt first, InIt last) {
-    insert(begin(), first, last);
+    insert(this->begin(), first, last);
   }
 
 
@@ -452,8 +575,8 @@ struct ilist : public iplist<NodeTy> {
 
 
   // Front and back inserters...
-  void push_front(const NodeTy &val) { insert(begin(), val); }
-  void push_back(const NodeTy &val) { insert(end(), val); }
+  void push_front(const NodeTy &val) { insert(this->begin(), val); }
+  void push_back(const NodeTy &val) { insert(this->end(), val); }
 
   // Special forms of insert...
   template<class InIt> void insert(iterator where, InIt first, InIt last) {
@@ -465,16 +588,16 @@ struct ilist : public iplist<NodeTy> {
 
   // Assign special forms...
   void assign(size_type count, const NodeTy &val) {
-    iterator I = begin();
-    for (; I != end() && count != 0; ++I, --count)
+    iterator I = this->begin();
+    for (; I != this->end() && count != 0; ++I, --count)
       *I = val;
     if (count != 0)
-      insert(end(), n, val);
+      insert(this->end(), val, val);
     else
-      erase(I, end());
+      erase(I, this->end());
   }
-  template<class InIt> void assign(InIt first, InIt last) {
-    iterator first1 = begin(), last1 = end();
+  template<class InIt> void assign(InIt first1, InIt last1) {
+    iterator first2 = this->begin(), last2 = this->end();
     for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
       *first1 = *first2;
     if (first2 == last2)
@@ -486,22 +609,24 @@ struct ilist : public iplist<NodeTy> {
 
   // Resize members...
   void resize(size_type newsize, NodeTy val) {
-    iterator i = begin();
+    iterator i = this->begin();
     size_type len = 0;
-    for ( ; i != end() && len < newsize; ++i, ++len) /* empty*/ ;
+    for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ;
 
     if (len == newsize)
-      erase(i, end());
+      erase(i, this->end());
     else                                          // i == end()
-      insert(end(), newsize - len, val);
+      insert(this->end(), newsize - len, val);
   }
   void resize(size_type newsize) { resize(newsize, NodeTy()); }
 };
 
+} // End llvm namespace
+
 namespace std {
   // Ensure that swap uses the fast list swap...
   template<class Ty>
-  void swap(iplist<Ty> &Left, iplist<Ty> &Right) {
+  void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
     Left.swap(Right);
   }
 }  // End 'std' extensions...