[PATCH] D49985: [ADT] ImmutableList no longer requires elements to be copy constructible

Umann Kristóf via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 13 10:14:29 PDT 2018


Szelethus updated this revision to Diff 160385.
Szelethus edited the summary of this revision.
Szelethus added a comment.

- Moved tests to https://reviews.llvm.org/D50646
- Changed ordering back to how it was
- Added a `static_assert`, so only trivially destructible types can be stored in `ImmutableList`.


https://reviews.llvm.org/D49985

Files:
  include/llvm/ADT/ImmutableList.h


Index: include/llvm/ADT/ImmutableList.h
===================================================================
--- include/llvm/ADT/ImmutableList.h
+++ include/llvm/ADT/ImmutableList.h
@@ -31,8 +31,8 @@
   T Head;
   const ImmutableListImpl* Tail;
 
-  ImmutableListImpl(const T& head, const ImmutableListImpl* tail = nullptr)
-    : Head(head), Tail(tail) {}
+  ImmutableListImpl(T &&head, const ImmutableListImpl *tail = nullptr)
+    : Head(std::move(head)), Tail(tail) {}
 
 public:
   ImmutableListImpl(const ImmutableListImpl &) = delete;
@@ -66,6 +66,9 @@
   using value_type = T;
   using Factory = ImmutableListFactory<T>;
 
+  static_assert(std::is_trivially_destructible<T>::value,
+                "T must be trivially destructible!");
+
 private:
   const ImmutableListImpl<T>* X;
 
@@ -166,7 +169,7 @@
     if (ownsAllocator()) delete &getAllocator();
   }
 
-  LLVM_NODISCARD ImmutableList<T> concat(const T &Head, ImmutableList<T> Tail) {
+  LLVM_NODISCARD ImmutableList<T> concat(T Head, ImmutableList<T>  Tail) {
     // Profile the new list to see if it already exists in our cache.
     FoldingSetNodeID ID;
     void* InsertPos;
@@ -179,25 +182,31 @@
       // The list does not exist in our cache.  Create it.
       BumpPtrAllocator& A = getAllocator();
       L = (ListTy*) A.Allocate<ListTy>();
-      new (L) ListTy(Head, TailImpl);
+      new (L) ListTy(std::move(Head), TailImpl);
 
       // Insert the new list into the cache.
       Cache.InsertNode(L, InsertPos);
     }
 
     return L;
   }
 
-  LLVM_NODISCARD ImmutableList<T> add(const T& D, ImmutableList<T> L) {
-    return concat(D, L);
+  LLVM_NODISCARD ImmutableList<T> add(T Data, ImmutableList<T> L) {
+    return concat(std::move(Data), L);
+  }
+
+  template <typename ...CtorArgs>
+  LLVM_NODISCARD ImmutableList<T> emplace(ImmutableList<T> Tail,
+                                          CtorArgs &&...Args) {
+    return concat(T(std::forward<CtorArgs>(Args)...), Tail);
   }
 
   ImmutableList<T> getEmptyList() const {
     return ImmutableList<T>(nullptr);
   }
 
-  ImmutableList<T> create(const T& X) {
-    return concat(X, getEmptyList());
+  ImmutableList<T> create(T Data) {
+    return concat(std::move(Data), getEmptyList());
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49985.160385.patch
Type: text/x-patch
Size: 2244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180813/3a43a389/attachment.bin>


More information about the llvm-commits mailing list