[clangd] Replace UniqueFunction with llvm::unique_function.

One implementation of this ought to be enough for everyone.

llvm-svn: 336228
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index 95f6dae..cc61673 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -280,7 +280,7 @@
 }
 
 void ClangdServer::dumpAST(PathRef File,
-                           UniqueFunction<void(std::string)> Callback) {
+                           llvm::unique_function<void(std::string)> Callback) {
   auto Action = [](decltype(Callback) Callback,
                    llvm::Expected<InputsAndAST> InpAST) {
     if (!InpAST) {
diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index 7550c28..5d14f09 100644
--- a/clang-tools-extra/clangd/ClangdServer.h
+++ b/clang-tools-extra/clangd/ClangdServer.h
@@ -188,7 +188,7 @@
   /// Only for testing purposes.
   /// Waits until all requests to worker thread are finished and dumps AST for
   /// \p File. \p File must be in the list of added documents.
-  void dumpAST(PathRef File, UniqueFunction<void(std::string)> Callback);
+  void dumpAST(PathRef File, llvm::unique_function<void(std::string)> Callback);
   /// Called when an event occurs for a watched file in the workspace.
   void onFileEvent(const DidChangeWatchedFilesParams &Params);
 
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 7e07c3f..a1298fd 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -566,7 +566,7 @@
 // within the callback.
 struct CompletionRecorder : public CodeCompleteConsumer {
   CompletionRecorder(const CodeCompleteOptions &Opts,
-                     UniqueFunction<void()> ResultsCallback)
+                     llvm::unique_function<void()> ResultsCallback)
       : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
                              /*OutputIsBinary=*/false),
         CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
@@ -657,7 +657,7 @@
   CodeCompleteOptions Opts;
   std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
   CodeCompletionTUInfo CCTUInfo;
-  UniqueFunction<void()> ResultsCallback;
+  llvm::unique_function<void()> ResultsCallback;
 };
 
 class SignatureHelpCollector final : public CodeCompleteConsumer {
diff --git a/clang-tools-extra/clangd/Function.h b/clang-tools-extra/clangd/Function.h
index 975134b..88a5bc0 100644
--- a/clang-tools-extra/clangd/Function.h
+++ b/clang-tools-extra/clangd/Function.h
@@ -7,83 +7,25 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file provides an analogue to std::function that supports move semantics.
+// This file provides utilities for callable objects.
 //
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUNCTION_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUNCTION_H
 
-#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/FunctionExtras.h"
 #include "llvm/Support/Error.h"
-#include <cassert>
-#include <memory>
 #include <tuple>
-#include <type_traits>
 #include <utility>
 
 namespace clang {
 namespace clangd {
 
-/// A move-only type-erasing function wrapper. Similar to `std::function`, but
-/// allows to store move-only callables.
-template <class> class UniqueFunction;
 /// A Callback<T> is a void function that accepts Expected<T>.
 /// This is accepted by ClangdServer functions that logically return T.
-template <typename T> using Callback = UniqueFunction<void(llvm::Expected<T>)>;
-
-template <class Ret, class... Args> class UniqueFunction<Ret(Args...)> {
-public:
-  UniqueFunction() = default;
-  UniqueFunction(std::nullptr_t) : UniqueFunction(){};
-
-  UniqueFunction(UniqueFunction const &) = delete;
-  UniqueFunction &operator=(UniqueFunction const &) = delete;
-
-  UniqueFunction(UniqueFunction &&) noexcept = default;
-  UniqueFunction &operator=(UniqueFunction &&) noexcept = default;
-
-  template <class Callable,
-            /// A sfinae-check that Callable can be called with Args... and
-            class = typename std::enable_if<std::is_convertible<
-                decltype(std::declval<Callable>()(std::declval<Args>()...)),
-                Ret>::value>::type>
-  UniqueFunction(Callable &&Func)
-      : CallablePtr(llvm::make_unique<
-                    FunctionCallImpl<typename std::decay<Callable>::type>>(
-            std::forward<Callable>(Func))) {}
-
-  explicit operator bool() { return bool(CallablePtr); }
-
-  Ret operator()(Args... As) {
-    assert(CallablePtr);
-    return CallablePtr->Call(std::forward<Args>(As)...);
-  }
-
-private:
-  class FunctionCallBase {
-  public:
-    virtual ~FunctionCallBase() = default;
-    virtual Ret Call(Args... As) = 0;
-  };
-
-  template <class Callable>
-  class FunctionCallImpl final : public FunctionCallBase {
-    static_assert(
-        std::is_same<Callable, typename std::decay<Callable>::type>::value,
-        "FunctionCallImpl must be instanstiated with std::decay'ed types");
-
-  public:
-    FunctionCallImpl(Callable Func) : Func(std::move(Func)) {}
-
-    Ret Call(Args... As) override { return Func(std::forward<Args>(As)...); }
-
-  private:
-    Callable Func;
-  };
-
-  std::unique_ptr<FunctionCallBase> CallablePtr;
-};
+template <typename T>
+using Callback = llvm::unique_function<void(llvm::Expected<T>)>;
 
 /// Stores a callable object (Func) and arguments (Args) and allows to call the
 /// callable with provided arguments later using `operator ()`. The arguments
diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp
index c0598c6..6c2918c 100644
--- a/clang-tools-extra/clangd/TUScheduler.cpp
+++ b/clang-tools-extra/clangd/TUScheduler.cpp
@@ -169,9 +169,10 @@
   ~ASTWorker();
 
   void update(ParseInputs Inputs, WantDiagnostics,
-              UniqueFunction<void(std::vector<Diag>)> OnUpdated);
-  void runWithAST(llvm::StringRef Name,
-                  UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action);
+              llvm::unique_function<void(std::vector<Diag>)> OnUpdated);
+  void
+  runWithAST(llvm::StringRef Name,
+             llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action);
   bool blockUntilIdle(Deadline Timeout) const;
 
   std::shared_ptr<const PreambleData> getPossiblyStalePreamble() const;
@@ -186,7 +187,7 @@
   /// Signal that run() should finish processing pending requests and exit.
   void stop();
   /// Adds a new task to the end of the request queue.
-  void startTask(llvm::StringRef Name, UniqueFunction<void()> Task,
+  void startTask(llvm::StringRef Name, llvm::unique_function<void()> Task,
                  llvm::Optional<WantDiagnostics> UpdateType);
   /// Determines the next action to perform.
   /// All actions that should never run are disarded.
@@ -197,7 +198,7 @@
   bool shouldSkipHeadLocked() const;
 
   struct Request {
-    UniqueFunction<void()> Action;
+    llvm::unique_function<void()> Action;
     std::string Name;
     steady_clock::time_point AddTime;
     Context Ctx;
@@ -311,8 +312,9 @@
 #endif
 }
 
-void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags,
-                       UniqueFunction<void(std::vector<Diag>)> OnUpdated) {
+void ASTWorker::update(
+    ParseInputs Inputs, WantDiagnostics WantDiags,
+    llvm::unique_function<void(std::vector<Diag>)> OnUpdated) {
   auto Task = [=](decltype(OnUpdated) OnUpdated) mutable {
     tooling::CompileCommand OldCommand = std::move(FileInputs.CompileCommand);
     FileInputs = Inputs;
@@ -356,7 +358,7 @@
 
 void ASTWorker::runWithAST(
     llvm::StringRef Name,
-    UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action) {
+    llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
   auto Task = [=](decltype(Action) Action) {
     llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
     if (!AST) {
@@ -411,7 +413,8 @@
   RequestsCV.notify_all();
 }
 
-void ASTWorker::startTask(llvm::StringRef Name, UniqueFunction<void()> Task,
+void ASTWorker::startTask(llvm::StringRef Name,
+                          llvm::unique_function<void()> Task,
                           llvm::Optional<WantDiagnostics> UpdateType) {
   if (RunSync) {
     assert(!Done && "running a task after stop()");
@@ -586,9 +589,9 @@
   return true;
 }
 
-void TUScheduler::update(PathRef File, ParseInputs Inputs,
-                         WantDiagnostics WantDiags,
-                         UniqueFunction<void(std::vector<Diag>)> OnUpdated) {
+void TUScheduler::update(
+    PathRef File, ParseInputs Inputs, WantDiagnostics WantDiags,
+    llvm::unique_function<void(std::vector<Diag>)> OnUpdated) {
   std::unique_ptr<FileData> &FD = Files[File];
   if (!FD) {
     // Create a new worker to process the AST-related tasks.
@@ -614,7 +617,7 @@
 
 void TUScheduler::runWithAST(
     llvm::StringRef Name, PathRef File,
-    UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action) {
+    llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
   auto It = Files.find(File);
   if (It == Files.end()) {
     Action(llvm::make_error<llvm::StringError>(
@@ -628,7 +631,7 @@
 
 void TUScheduler::runWithPreamble(
     llvm::StringRef Name, PathRef File,
-    UniqueFunction<void(llvm::Expected<InputsAndPreamble>)> Action) {
+    llvm::unique_function<void(llvm::Expected<InputsAndPreamble>)> Action) {
   auto It = Files.find(File);
   if (It == Files.end()) {
     Action(llvm::make_error<llvm::StringError>(
diff --git a/clang-tools-extra/clangd/TUScheduler.h b/clang-tools-extra/clangd/TUScheduler.h
index dfd4bfd..a0c67b5 100644
--- a/clang-tools-extra/clangd/TUScheduler.h
+++ b/clang-tools-extra/clangd/TUScheduler.h
@@ -79,7 +79,7 @@
   /// \p File was not part of it before.
   /// FIXME(ibiryukov): remove the callback from this function.
   void update(PathRef File, ParseInputs Inputs, WantDiagnostics WD,
-              UniqueFunction<void(std::vector<Diag>)> OnUpdated);
+              llvm::unique_function<void(std::vector<Diag>)> OnUpdated);
 
   /// Remove \p File from the list of tracked files and schedule removal of its
   /// resources.
diff --git a/clang-tools-extra/clangd/Threading.cpp b/clang-tools-extra/clangd/Threading.cpp
index dfd0d2e..bd2c12e 100644
--- a/clang-tools-extra/clangd/Threading.cpp
+++ b/clang-tools-extra/clangd/Threading.cpp
@@ -51,7 +51,7 @@
 }
 
 void AsyncTaskRunner::runAsync(llvm::Twine Name,
-                               UniqueFunction<void()> Action) {
+                               llvm::unique_function<void()> Action) {
   {
     std::lock_guard<std::mutex> Lock(Mutex);
     ++InFlightTasks;
diff --git a/clang-tools-extra/clangd/Threading.h b/clang-tools-extra/clangd/Threading.h
index 3f5e5fc..61e02e6 100644
--- a/clang-tools-extra/clangd/Threading.h
+++ b/clang-tools-extra/clangd/Threading.h
@@ -108,7 +108,7 @@
   void wait() const { (void)wait(Deadline::infinity()); }
   LLVM_NODISCARD bool wait(Deadline D) const;
   // The name is used for tracing and debugging (e.g. to name a spawned thread).
-  void runAsync(llvm::Twine Name, UniqueFunction<void()> Action);
+  void runAsync(llvm::Twine Name, llvm::unique_function<void()> Action);
 
 private:
   mutable std::mutex Mutex;
diff --git a/clang-tools-extra/unittests/clangd/SyncAPI.cpp b/clang-tools-extra/unittests/clangd/SyncAPI.cpp
index 6b773fa..fa4f376 100644
--- a/clang-tools-extra/unittests/clangd/SyncAPI.cpp
+++ b/clang-tools-extra/unittests/clangd/SyncAPI.cpp
@@ -36,7 +36,7 @@
   }
   CaptureProxy &operator=(CaptureProxy &&) = delete;
 
-  operator UniqueFunction<void(T)>() && {
+  operator llvm::unique_function<void(T)>() && {
     assert(!Future.valid() && "conversion to callback called multiple times");
     Future = Promise.get_future();
     return Bind(