[llvm] r278467 - ADT: Add ilist_iterator conversions to/from ilist_node

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 11 20:35:33 PDT 2016


Author: dexonsmith
Date: Thu Aug 11 22:35:33 2016
New Revision: 278467

URL: http://llvm.org/viewvc/llvm-project?rev=278467&view=rev
Log:
ADT: Add ilist_iterator conversions to/from ilist_node

Allow an ilist_iterator to be constructed from an ilist_node, and give
access to the underlying ilist_node as well.

This will be used immediately in lld to support a type-erasure use case.
Longer term, they'll stick around once the iterator is using
ilist_node<NodeTy>* instead of NodeTy*.

Modified:
    llvm/trunk/include/llvm/ADT/ilist.h

Modified: llvm/trunk/include/llvm/ADT/ilist.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ilist.h?rev=278467&r1=278466&r2=278467&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ilist.h (original)
+++ llvm/trunk/include/llvm/ADT/ilist.h Thu Aug 11 22:35:33 2016
@@ -185,6 +185,15 @@ struct ilist_traits : public ilist_defau
 template<typename Ty>
 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
 
+namespace ilist_detail {
+template <class NodeTy> struct ConstCorrectNodeType {
+  typedef ilist_node<NodeTy> type;
+};
+template <class NodeTy> struct ConstCorrectNodeType<const NodeTy> {
+  typedef const ilist_node<NodeTy> type;
+};
+} // end namespace ilist_detail
+
 //===----------------------------------------------------------------------===//
 // Iterator for intrusive list.
 //
@@ -201,10 +210,18 @@ public:
   typedef typename super::pointer pointer;
   typedef typename super::reference reference;
 
+  typedef typename ilist_detail::ConstCorrectNodeType<NodeTy>::type node_type;
+  typedef node_type *node_pointer;
+  typedef node_type &node_reference;
+
 private:
   pointer NodePtr;
 
 public:
+  /// Create from an ilist_node.
+  explicit ilist_iterator(node_reference N)
+      : NodePtr(static_cast<NodeTy *>(&N)) {}
+
   explicit ilist_iterator(pointer NP) : NodePtr(NP) {}
   explicit ilist_iterator(reference NR) : NodePtr(&NR) {}
   ilist_iterator() : NodePtr(nullptr) {}
@@ -259,6 +276,9 @@ public:
     return tmp;
   }
 
+  /// Get the underlying ilist_node.
+  node_pointer getNodePtr() const { return static_cast<node_pointer>(NodePtr); }
+
   // Internal interface, do not use...
   pointer getNodePtrUnchecked() const { return NodePtr; }
 };




More information about the llvm-commits mailing list