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/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<bool(ImportedModule)> fn);
llvm::function_ref<bool(ImportedModule)> fn);
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
bool includePrivateTopLevelImports,
std::function<void(ImportedModule)> fn) {
llvm::function_ref<void(ImportedModule)> fn) {
return forAllVisibleModules(topLevelAccessPath,
includePrivateTopLevelImports,
[=](const ImportedModule &import) -> bool {
@@ -385,38 +386,28 @@ public:
template <typename Fn>
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
bool includePrivateTopLevelImports,
const Fn &fn) {
using RetTy = typename as_function<Fn>::type::result_type;
std::function<RetTy(ImportedModule)> wrapped = std::cref(fn);
Fn &&fn) {
using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
return forAllVisibleModules(topLevelAccessPath,
includePrivateTopLevelImports,
wrapped);
}
template <typename Fn>
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>(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
/// imports.
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
/// 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<bool(Module::ImportedModule)> fn);
bool
forAllVisibleModules(llvm::function_ref<bool(Module::ImportedModule)> fn);
bool forAllVisibleModules(std::function<void(Module::ImportedModule)> fn) {
forAllVisibleModules([=](const Module::ImportedModule &import) -> bool {
bool
forAllVisibleModules(llvm::function_ref<void(Module::ImportedModule)> fn) {
return forAllVisibleModules([=](Module::ImportedModule import) -> bool {
fn(import);
return true;
});
return true;
}
template <typename Fn>
bool forAllVisibleModules(const Fn &fn) {
using RetTy = typename as_function<Fn>::type::result_type;
std::function<RetTy(Module::ImportedModule)> wrapped = std::cref(fn);
bool forAllVisibleModules(Fn &&fn) {
using RetTy = typename std::result_of<Fn(Module::ImportedModule)>::type;
llvm::function_ref<RetTy(Module::ImportedModule)> wrapped{
std::forward<Fn>(fn)
};
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.
template<typename Iterator>
class IteratorRange {

View File

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

View File

@@ -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<void(clang::FullSourceLoc,
const llvm::function_ref<void(clang::FullSourceLoc,
clang::DiagnosticsEngine::Level,
StringRef)> callback;
public:
template <typename Fn>
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());

View File

@@ -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,8 +35,8 @@ namespace {
} // end anonymous namespace
static bool
parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly,
std::function<void(StringRef, DependencyKind,
parseDependencyFile(llvm::MemoryBuffer &buffer, bool providesOnly,
llvm::function_ref<void(StringRef, DependencyKind,
DependencyDirection)> callback) {
using namespace llvm;
@@ -81,12 +82,6 @@ parseDependencyFileImpl(llvm::MemoryBuffer &buffer, bool providesOnly,
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) {
auto buffer = llvm::MemoryBuffer::getFile(path);
if (!buffer)

View File

@@ -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<ReferencedTypeFinder> {
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) {
assert(base->getDesugaredType() == base && "unhandled sugared type");
@@ -795,9 +796,8 @@ class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> {
public:
using TypeVisitor::visit;
template<typename Fn>
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);
}
};