Adopt llvm::function_ref for callbacks that aren't persisted.

...removing a few other constructs that were doing the same thing
(mostly from me).

No functionality change.

Swift SVN r23294
This commit is contained in:
Jordan Rose
2014-11-13 00:19:03 +00:00
parent 1bbe784d51
commit 5ad871ecd6
6 changed files with 39 additions and 73 deletions

View File

@@ -28,6 +28,7 @@
#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/TinyPtrVector.h" #include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
@@ -369,11 +370,11 @@ public:
/// due to the callback. /// due to the callback.
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
bool includePrivateTopLevelImports, bool includePrivateTopLevelImports,
std::function<bool(ImportedModule)> fn); llvm::function_ref<bool(ImportedModule)> fn);
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
bool includePrivateTopLevelImports, bool includePrivateTopLevelImports,
std::function<void(ImportedModule)> fn) { llvm::function_ref<void(ImportedModule)> fn) {
return forAllVisibleModules(topLevelAccessPath, return forAllVisibleModules(topLevelAccessPath,
includePrivateTopLevelImports, includePrivateTopLevelImports,
[=](const ImportedModule &import) -> bool { [=](const ImportedModule &import) -> bool {
@@ -385,38 +386,28 @@ public:
template <typename Fn> template <typename Fn>
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
bool includePrivateTopLevelImports, bool includePrivateTopLevelImports,
const Fn &fn) { Fn &&fn) {
using RetTy = typename as_function<Fn>::type::result_type; using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
std::function<RetTy(ImportedModule)> wrapped = std::cref(fn); llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
return forAllVisibleModules(topLevelAccessPath, return forAllVisibleModules(topLevelAccessPath,
includePrivateTopLevelImports, includePrivateTopLevelImports,
wrapped); wrapped);
} }
template <typename Fn> template <typename Fn>
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, bool forAllVisibleModules(AccessPathTy topLevelAccessPath, Fn &&fn) {
const Fn &fn) { return forAllVisibleModules(topLevelAccessPath, false,
return forAllVisibleModules(topLevelAccessPath, false, fn); std::forward<Fn>(fn));
} }
/// @} /// @}
using LinkLibraryCallback = std::function<void(LinkLibrary)>; using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;
/// @{
/// Generate the list of libraries needed to link this module, based on its /// Generate the list of libraries needed to link this module, based on its
/// imports. /// imports.
void collectLinkLibraries(LinkLibraryCallback callback); void collectLinkLibraries(LinkLibraryCallback callback);
template <typename Fn>
void collectLinkLibraries(const Fn &fn) {
LinkLibraryCallback wrapped = std::cref(fn);
collectLinkLibraries(wrapped);
}
/// @}
/// Returns true if the two access paths contain the same chain of /// Returns true if the two access paths contain the same chain of
/// identifiers. /// identifiers.
/// ///
@@ -578,20 +569,23 @@ public:
/// ///
/// \return True if the traversal ran to completion, false if it ended early /// \return True if the traversal ran to completion, false if it ended early
/// due to the callback. /// due to the callback.
bool forAllVisibleModules(std::function<bool(Module::ImportedModule)> fn); bool
forAllVisibleModules(llvm::function_ref<bool(Module::ImportedModule)> fn);
bool forAllVisibleModules(std::function<void(Module::ImportedModule)> fn) { bool
forAllVisibleModules([=](const Module::ImportedModule &import) -> bool { forAllVisibleModules(llvm::function_ref<void(Module::ImportedModule)> fn) {
return forAllVisibleModules([=](Module::ImportedModule import) -> bool {
fn(import); fn(import);
return true; return true;
}); });
return true;
} }
template <typename Fn> template <typename Fn>
bool forAllVisibleModules(const Fn &fn) { bool forAllVisibleModules(Fn &&fn) {
using RetTy = typename as_function<Fn>::type::result_type; using RetTy = typename std::result_of<Fn(Module::ImportedModule)>::type;
std::function<RetTy(Module::ImportedModule)> wrapped = std::cref(fn); llvm::function_ref<RetTy(Module::ImportedModule)> wrapped{
std::forward<Fn>(fn)
};
return forAllVisibleModules(wrapped); return forAllVisibleModules(wrapped);
} }

View File

@@ -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 <typename T>
struct as_function : public as_function<decltype(&T::operator())> {};
template <typename L, typename R, typename... Args>
struct as_function<R(L::*)(Args...) const> {
using type = std::function<R(Args...)>;
};
/// @}
/// Returns a std::function that can call a lambda without copying it.
template <typename L>
static typename as_function<L>::type makeStackLambda(const L &theLambda) {
return std::cref(theLambda);
}
/// A range of iterators. /// A range of iterators.
template<typename Iterator> template<typename Iterator>
class IteratorRange { class IteratorRange {

View File

@@ -1328,13 +1328,13 @@ static bool forAllImportedModules(Module *topLevel,
bool Module::forAllVisibleModules(AccessPathTy thisPath, bool Module::forAllVisibleModules(AccessPathTy thisPath,
bool includePrivateTopLevelImports, bool includePrivateTopLevelImports,
std::function<bool(ImportedModule)> fn) { llvm::function_ref<bool(ImportedModule)> fn) {
return forAllImportedModules<true>(this, thisPath, return forAllImportedModules<true>(this, thisPath,
includePrivateTopLevelImports, fn); includePrivateTopLevelImports, fn);
} }
bool bool FileUnit::forAllVisibleModules(
FileUnit::forAllVisibleModules(std::function<bool(Module::ImportedModule)> fn) { llvm::function_ref<bool(Module::ImportedModule)> fn) {
if (!getParentModule()->forAllVisibleModules(Module::AccessPathTy(), fn)) if (!getParentModule()->forAllVisibleModules(Module::AccessPathTy(), fn))
return false; return false;

View File

@@ -18,18 +18,18 @@
#include "clang/AST/ASTContext.h" #include "clang/AST/ASTContext.h"
#include "clang/Frontend/DiagnosticRenderer.h" #include "clang/Frontend/DiagnosticRenderer.h"
#include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/LexDiagnostic.h"
#include "llvm/ADT/STLExtras.h"
using namespace swift; using namespace swift;
namespace { namespace {
class ClangDiagRenderer final : public clang::DiagnosticNoteRenderer { class ClangDiagRenderer final : public clang::DiagnosticNoteRenderer {
const std::function<void(clang::FullSourceLoc, const llvm::function_ref<void(clang::FullSourceLoc,
clang::DiagnosticsEngine::Level, clang::DiagnosticsEngine::Level,
StringRef)> callback; StringRef)> callback;
public: public:
template <typename Fn> ClangDiagRenderer(const clang::ASTContext &ctx, decltype(callback) fn)
ClangDiagRenderer(const clang::ASTContext &ctx, const Fn &fn)
: DiagnosticNoteRenderer(ctx.getLangOpts(), : DiagnosticNoteRenderer(ctx.getLangOpts(),
&ctx.getDiagnostics().getDiagnosticOptions()), &ctx.getDiagnostics().getDiagnosticOptions()),
callback(fn) {} callback(fn) {}
@@ -219,8 +219,7 @@ void ClangDiagnosticConsumer::HandleDiagnostic(
} else { } else {
assert(clangDiag.hasSourceManager()); assert(clangDiag.hasSourceManager());
ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), emitDiag);
std::cref(emitDiag));
renderer.emitDiagnostic(clangDiag.getLocation(), clangDiagLevel, message, renderer.emitDiagnostic(clangDiag.getLocation(), clangDiagLevel, message,
clangDiag.getRanges(), clangDiag.getFixItHints(), clangDiag.getRanges(), clangDiag.getFixItHints(),
&clangDiag.getSourceManager()); &clangDiag.getSourceManager());

View File

@@ -13,6 +13,7 @@
#include "swift/Driver/DependencyGraph.h" #include "swift/Driver/DependencyGraph.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SourceMgr.h"
@@ -34,9 +35,9 @@ namespace {
} // end anonymous namespace } // end anonymous namespace
static bool static bool
parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly, parseDependencyFile(llvm::MemoryBuffer &buffer, bool providesOnly,
std::function<void(StringRef, DependencyKind, llvm::function_ref<void(StringRef, DependencyKind,
DependencyDirection)> callback) { DependencyDirection)> callback) {
using namespace llvm; using namespace llvm;
// FIXME: Switch to a format other than YAML. // FIXME: Switch to a format other than YAML.
@@ -81,12 +82,6 @@ parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly,
return false; return false;
} }
template <typename Fn>
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) { bool DependencyGraphImpl::loadFromPath(const void *node, StringRef path) {
auto buffer = llvm::MemoryBuffer::getFile(path); auto buffer = llvm::MemoryBuffer::getFile(path);
if (!buffer) if (!buffer)

View File

@@ -25,8 +25,9 @@
#include "clang/AST/DeclObjC.h" #include "clang/AST/DeclObjC.h"
#include "clang/Basic/Module.h" #include "clang/Basic/Module.h"
#include "llvm/ADT/SetVector.h" #include "llvm/ADT/SetVector.h"
#include "llvm/Support/Path.h"
#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
using namespace swift; using namespace swift;
@@ -723,9 +724,9 @@ private:
class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> { class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> {
friend TypeVisitor; friend TypeVisitor;
std::function<void(ReferencedTypeFinder &, const TypeDecl *)> Callback; llvm::function_ref<void(ReferencedTypeFinder &, const TypeDecl *)> Callback;
ReferencedTypeFinder(decltype(Callback) &&callback) : Callback(callback) {} ReferencedTypeFinder(decltype(Callback) callback) : Callback(callback) {}
void visitType(TypeBase *base) { void visitType(TypeBase *base) {
assert(base->getDesugaredType() == base && "unhandled sugared type"); assert(base->getDesugaredType() == base && "unhandled sugared type");
@@ -795,9 +796,8 @@ class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> {
public: public:
using TypeVisitor::visit; using TypeVisitor::visit;
template<typename Fn> static void walk(Type ty, decltype(Callback) callback) {
static void walk(Type ty, const Fn &callback) { ReferencedTypeFinder(callback).visit(ty);
ReferencedTypeFinder(std::cref(callback)).visit(ty);
} }
}; };