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; @@ -166,12 +166,14 @@ if (ownsAllocator()) delete &getAllocator(); } - LLVM_NODISCARD ImmutableList concat(const T &Head, ImmutableList Tail) { + template + LLVM_NODISCARD ImmutableList concat(ImmutableList Tail, CtorArgs &&...Args) { // Profile the new list to see if it already exists in our cache. FoldingSetNodeID ID; void* InsertPos; const ListTy* TailImpl = Tail.getInternalPointer(); + T Head(std::forward(Args)...); ListTy::Profile(ID, Head, TailImpl); ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos); @@ -179,7 +181,7 @@ // The list does not exist in our cache. Create it. BumpPtrAllocator& A = getAllocator(); L = (ListTy*) A.Allocate(); - new (L) ListTy(Head, TailImpl); + new (L) ListTy(std::move(Head), TailImpl); // Insert the new list into the cache. Cache.InsertNode(L, InsertPos); @@ -188,16 +190,18 @@ return L; } - LLVM_NODISCARD ImmutableList add(const T& D, ImmutableList L) { - return concat(D, L); + template + LLVM_NODISCARD ImmutableList add(ImmutableList Tail, CtorArgs &&...Args) { + return concat(Tail, std::forward(Args)...); } ImmutableList getEmptyList() const { return ImmutableList(nullptr); } - ImmutableList create(const T& X) { - return Concat(X, getEmptyList()); + template + ImmutableList create(CtorArgs &&...Args) { + return concat(getEmptyList(), std::forward(Args)...); } };