diff --git a/include/swift/AST/Module.h b/include/swift/AST/Module.h index 126a0e9e401..133a2dc1ed2 100644 --- a/include/swift/AST/Module.h +++ b/include/swift/AST/Module.h @@ -28,6 +28,7 @@ #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Support/ErrorHandling.h" @@ -369,11 +370,11 @@ public: /// due to the callback. bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool includePrivateTopLevelImports, - std::function fn); + llvm::function_ref fn); bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool includePrivateTopLevelImports, - std::function fn) { + llvm::function_ref fn) { return forAllVisibleModules(topLevelAccessPath, includePrivateTopLevelImports, [=](const ImportedModule &import) -> bool { @@ -385,38 +386,28 @@ public: template bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool includePrivateTopLevelImports, - const Fn &fn) { - using RetTy = typename as_function::type::result_type; - std::function wrapped = std::cref(fn); + Fn &&fn) { + using RetTy = typename std::result_of::type; + llvm::function_ref wrapped{std::forward(fn)}; return forAllVisibleModules(topLevelAccessPath, includePrivateTopLevelImports, wrapped); } template - bool forAllVisibleModules(AccessPathTy topLevelAccessPath, - const Fn &fn) { - return forAllVisibleModules(topLevelAccessPath, false, fn); + bool forAllVisibleModules(AccessPathTy topLevelAccessPath, Fn &&fn) { + return forAllVisibleModules(topLevelAccessPath, false, + std::forward(fn)); } /// @} - using LinkLibraryCallback = std::function; - - /// @{ + using LinkLibraryCallback = llvm::function_ref; /// Generate the list of libraries needed to link this module, based on its /// imports. void collectLinkLibraries(LinkLibraryCallback callback); - template - void collectLinkLibraries(const Fn &fn) { - LinkLibraryCallback wrapped = std::cref(fn); - collectLinkLibraries(wrapped); - } - - /// @} - /// Returns true if the two access paths contain the same chain of /// identifiers. /// @@ -578,20 +569,23 @@ public: /// /// \return True if the traversal ran to completion, false if it ended early /// due to the callback. - bool forAllVisibleModules(std::function fn); + bool + forAllVisibleModules(llvm::function_ref fn); - bool forAllVisibleModules(std::function fn) { - forAllVisibleModules([=](const Module::ImportedModule &import) -> bool { + bool + forAllVisibleModules(llvm::function_ref fn) { + return forAllVisibleModules([=](Module::ImportedModule import) -> bool { fn(import); return true; }); - return true; } template - bool forAllVisibleModules(const Fn &fn) { - using RetTy = typename as_function::type::result_type; - std::function wrapped = std::cref(fn); + bool forAllVisibleModules(Fn &&fn) { + using RetTy = typename std::result_of::type; + llvm::function_ref wrapped{ + std::forward(fn) + }; return forAllVisibleModules(wrapped); } diff --git a/include/swift/Basic/STLExtras.h b/include/swift/Basic/STLExtras.h index 2e5ab425116..c14d3cd52f5 100644 --- a/include/swift/Basic/STLExtras.h +++ b/include/swift/Basic/STLExtras.h @@ -102,28 +102,6 @@ inline void for_each3(const Container1 &c1, const Container2 &c2, /// @} -/// @{ - -/// Declares a member \c type that is a std::function compatible with the given -/// callable type. -/// -/// \tparam T A callable type, such as a lambda. -template -struct as_function : public as_function {}; - -template -struct as_function { - using type = std::function; -}; - -/// @} - -/// Returns a std::function that can call a lambda without copying it. -template -static typename as_function::type makeStackLambda(const L &theLambda) { - return std::cref(theLambda); -} - /// A range of iterators. template class IteratorRange { diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index be6060fdd0e..ce1c5441045 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -1328,13 +1328,13 @@ static bool forAllImportedModules(Module *topLevel, bool Module::forAllVisibleModules(AccessPathTy thisPath, bool includePrivateTopLevelImports, - std::function fn) { + llvm::function_ref fn) { return forAllImportedModules(this, thisPath, includePrivateTopLevelImports, fn); } -bool -FileUnit::forAllVisibleModules(std::function fn) { +bool FileUnit::forAllVisibleModules( + llvm::function_ref fn) { if (!getParentModule()->forAllVisibleModules(Module::AccessPathTy(), fn)) return false; diff --git a/lib/ClangImporter/ClangDiagnosticConsumer.cpp b/lib/ClangImporter/ClangDiagnosticConsumer.cpp index c6d4ed686c3..e4e1d19b313 100644 --- a/lib/ClangImporter/ClangDiagnosticConsumer.cpp +++ b/lib/ClangImporter/ClangDiagnosticConsumer.cpp @@ -18,18 +18,18 @@ #include "clang/AST/ASTContext.h" #include "clang/Frontend/DiagnosticRenderer.h" #include "clang/Lex/LexDiagnostic.h" +#include "llvm/ADT/STLExtras.h" using namespace swift; namespace { class ClangDiagRenderer final : public clang::DiagnosticNoteRenderer { - const std::function callback; + const llvm::function_ref callback; public: - template - ClangDiagRenderer(const clang::ASTContext &ctx, const Fn &fn) + ClangDiagRenderer(const clang::ASTContext &ctx, decltype(callback) fn) : DiagnosticNoteRenderer(ctx.getLangOpts(), &ctx.getDiagnostics().getDiagnosticOptions()), callback(fn) {} @@ -219,8 +219,7 @@ void ClangDiagnosticConsumer::HandleDiagnostic( } else { assert(clangDiag.hasSourceManager()); - ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), - std::cref(emitDiag)); + ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), emitDiag); renderer.emitDiagnostic(clangDiag.getLocation(), clangDiagLevel, message, clangDiag.getRanges(), clangDiag.getFixItHints(), &clangDiag.getSourceManager()); diff --git a/lib/Driver/DependencyGraph.cpp b/lib/Driver/DependencyGraph.cpp index d5c5f0214f3..6b344492f20 100644 --- a/lib/Driver/DependencyGraph.cpp +++ b/lib/Driver/DependencyGraph.cpp @@ -13,6 +13,7 @@ #include "swift/Driver/DependencyGraph.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SourceMgr.h" @@ -34,9 +35,9 @@ namespace { } // end anonymous namespace static bool -parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly, - std::function callback) { +parseDependencyFile(llvm::MemoryBuffer &buffer, bool providesOnly, + llvm::function_ref callback) { using namespace llvm; // FIXME: Switch to a format other than YAML. @@ -81,12 +82,6 @@ parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly, return false; } -template -static bool parseDependencyFile(llvm::MemoryBuffer &buffer, bool providesOnly, - Fn fn) { - return parseDependencyFileImpl(buffer, providesOnly, std::cref(fn)); -} - bool DependencyGraphImpl::loadFromPath(const void *node, StringRef path) { auto buffer = llvm::MemoryBuffer::getFile(path); if (!buffer) diff --git a/lib/PrintAsObjC/PrintAsObjC.cpp b/lib/PrintAsObjC/PrintAsObjC.cpp index 0a4b9365095..d565391aa7c 100644 --- a/lib/PrintAsObjC/PrintAsObjC.cpp +++ b/lib/PrintAsObjC/PrintAsObjC.cpp @@ -25,8 +25,9 @@ #include "clang/AST/DeclObjC.h" #include "clang/Basic/Module.h" #include "llvm/ADT/SetVector.h" -#include "llvm/Support/Path.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" using namespace swift; @@ -723,9 +724,9 @@ private: class ReferencedTypeFinder : private TypeVisitor { friend TypeVisitor; - std::function Callback; + llvm::function_ref Callback; - ReferencedTypeFinder(decltype(Callback) &&callback) : Callback(callback) {} + ReferencedTypeFinder(decltype(Callback) callback) : Callback(callback) {} void visitType(TypeBase *base) { assert(base->getDesugaredType() == base && "unhandled sugared type"); @@ -795,9 +796,8 @@ class ReferencedTypeFinder : private TypeVisitor { public: using TypeVisitor::visit; - template - static void walk(Type ty, const Fn &callback) { - ReferencedTypeFinder(std::cref(callback)).visit(ty); + static void walk(Type ty, decltype(Callback) callback) { + ReferencedTypeFinder(callback).visit(ty); } };