Skip to content

Move interleave(...) to the llvm namespace #31091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/AutoDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ struct SILAutoDiffIndices {

std::string mangle() const {
std::string result = "src_" + llvm::utostr(source) + "_wrt_";
interleave(
llvm::interleave(
parameters->getIndices(),
[&](unsigned idx) { result += llvm::utostr(idx); },
[&] { result += '_'; });
Expand Down
9 changes: 9 additions & 0 deletions include/swift/Basic/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ struct function_traits<R (T::*)(Args...) const> {
using argument_types = std::tuple<Args...>;
};

} // end namespace swift

namespace llvm {

/// @{

/// An STL-style algorithm similar to std::for_each that applies a second
Expand Down Expand Up @@ -103,6 +107,11 @@ inline void interleave(const Container &c, UnaryFunctor each_fn,
}

/// @}

} // end namespace llvm

namespace swift {

/// @{

/// The equivalent of std::for_each, but for two lists at once.
Expand Down
47 changes: 24 additions & 23 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1477,25 +1477,26 @@ void PrintAST::printSingleDepthOfGenericSignature(
if (printParams) {
// Print the generic parameters.
Printer << "<";
interleave(genericParams,
[&](GenericTypeParamType *param) {
if (!subMap.empty()) {
if (auto argTy = substParam(param))
printType(argTy);
else
printType(param);
} else if (auto *GP = param->getDecl()) {
Printer.callPrintStructurePre(
PrintStructureKind::GenericParameter, GP);
Printer.printName(GP->getName(),
PrintNameContext::GenericParameter);
Printer.printStructurePost(
PrintStructureKind::GenericParameter, GP);
} else {
printType(param);
}
},
[&] { Printer << ", "; });
llvm::interleave(
genericParams,
[&](GenericTypeParamType *param) {
if (!subMap.empty()) {
if (auto argTy = substParam(param))
printType(argTy);
else
printType(param);
} else if (auto *GP = param->getDecl()) {
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter,
GP);
Printer.printName(GP->getName(),
PrintNameContext::GenericParameter);
Printer.printStructurePost(PrintStructureKind::GenericParameter,
GP);
} else {
printType(param);
}
},
[&] { Printer << ", "; });
}

if (printRequirements || printInherited) {
Expand Down Expand Up @@ -3025,7 +3026,7 @@ void PrintAST::visitEnumCaseDecl(EnumCaseDecl *decl) {
}
Printer << tok::kw_case << " ";

interleave(elems.begin(), elems.end(),
llvm::interleave(elems.begin(), elems.end(),
[&](EnumElementDecl *elt) {
printEnumElement(elt);
},
Expand Down Expand Up @@ -4438,9 +4439,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
if (!T->getSubstitutions().empty()) {
Printer << '<';
auto replacements = T->getSubstitutions().getReplacementTypes();
interleave(replacements.begin(), replacements.end(),
[&](Type t) { visit(t); },
[&] { Printer << ", "; });
llvm::interleave(
replacements.begin(), replacements.end(), [&](Type t) { visit(t); },
[&] { Printer << ", "; });
Printer << '>';
}
return;
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/ASTScopePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ void ASTScopeImpl::dumpOneScopeMapLocation(
locScope->lookupLocalsOrMembers({this}, gatherer);
if (!gatherer.getDecls().empty()) {
llvm::errs() << "Local bindings: ";
interleave(gatherer.getDecls().begin(), gatherer.getDecls().end(),
[&](ValueDecl *value) { llvm::errs() << value->getFullName(); },
[&]() { llvm::errs() << " "; });
llvm::interleave(
gatherer.getDecls().begin(), gatherer.getDecls().end(),
[&](ValueDecl *value) { llvm::errs() << value->getFullName(); },
[&]() { llvm::errs() << " "; });
llvm::errs() << "\n";
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
using namespace swift;

void ConformanceAccessPath::print(raw_ostream &out) const {
interleave(begin(), end(),
[&](const Entry &entry) {
entry.first.print(out);
out << ": " << entry.second->getName();
}, [&] {
out << " -> ";
});
llvm::interleave(
begin(), end(),
[&](const Entry &entry) {
entry.first.print(out);
out << ": " << entry.second->getName();
},
[&] { out << " -> "; });
}

void ConformanceAccessPath::dump() const {
Expand Down
38 changes: 19 additions & 19 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,16 +2139,16 @@ void EquivalenceClass::dump(llvm::raw_ostream &out,
},
[&] { out << ", "; });
out << "\nSame-type constraints:";
interleave(sameTypeConstraints,
[&](const Constraint<Type> &constraint) {
out << "\n " << constraint.getSubjectDependentType({ })
<< " == " << constraint.value;

if (constraint.source->isDerivedRequirement())
out << " [derived]";
}, [&] {
out << ", ";
});
llvm::interleave(
sameTypeConstraints,
[&](const Constraint<Type> &constraint) {
out << "\n " << constraint.getSubjectDependentType({})
<< " == " << constraint.value;

if (constraint.source->isDerivedRequirement())
out << " [derived]";
},
[&] { out << ", "; });
if (concreteType)
out << "\nConcrete type: " << concreteType.getString();
if (superclass)
Expand Down Expand Up @@ -2922,15 +2922,15 @@ void RewritePath::print(llvm::raw_ostream &out) const {
if (!getPath().empty()) out << " -> ";
}

interleave(getPath().begin(), getPath().end(),
[&](AssociatedTypeDecl *assocType) {
out.changeColor(raw_ostream::BLUE);
out << assocType->getProtocol()->getName() << "."
<< assocType->getName();
out.resetColor();
}, [&] {
out << " -> ";
});
llvm::interleave(
getPath().begin(), getPath().end(),
[&](AssociatedTypeDecl *assocType) {
out.changeColor(raw_ostream::BLUE);
out << assocType->getProtocol()->getName() << "."
<< assocType->getName();
out.resetColor();
},
[&] { out << " -> "; });
out << "]";
}

Expand Down
5 changes: 3 additions & 2 deletions lib/AST/IndexSubset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ IndexSubset *IndexSubset::extendingCapacity(

void IndexSubset::print(llvm::raw_ostream &s) const {
s << '{';
interleave(range(capacity), [this, &s](unsigned i) { s << contains(i); },
[&s] { s << ", "; });
llvm::interleave(
range(capacity), [this, &s](unsigned i) { s << contains(i); },
[&s] { s << ", "; });
s << '}';
}

Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,9 +1479,9 @@ void
ModuleDecl::ReverseFullNameIterator::printForward(raw_ostream &out,
StringRef delim) const {
SmallVector<StringRef, 8> elements(*this, {});
swift::interleave(llvm::reverse(elements),
[&out](StringRef next) { out << next; },
[&out, delim] { out << delim; });
llvm::interleave(
llvm::reverse(elements), [&out](StringRef next) { out << next; },
[&out, delim] { out << delim; });
}

void
Expand Down
6 changes: 3 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,9 @@ ClangImporter::create(ASTContext &ctx, const ClangImporterOptions &importerOpts,

if (importerOpts.DumpClangDiagnostics) {
llvm::errs() << "'";
interleave(invocationArgStrs,
[](StringRef arg) { llvm::errs() << arg; },
[] { llvm::errs() << "' '"; });
llvm::interleave(
invocationArgStrs, [](StringRef arg) { llvm::errs() << arg; },
[] { llvm::errs() << "' '"; });
llvm::errs() << "'\n";
}

Expand Down
50 changes: 21 additions & 29 deletions lib/ClangImporter/SwiftLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,37 +973,32 @@ void SwiftLookupTable::dump(raw_ostream &os) const {
printStoredContext(entry.Context, os);
os << ": ";

interleave(entry.DeclsOrMacros.begin(), entry.DeclsOrMacros.end(),
[this, &os](uint64_t entry) {
printStoredEntry(this, entry, os);
},
[&os] {
os << ", ";
});
llvm::interleave(
entry.DeclsOrMacros.begin(), entry.DeclsOrMacros.end(),
[this, &os](uint64_t entry) { printStoredEntry(this, entry, os); },
[&os] { os << ", "; });
os << "\n";
}
}

if (!Categories.empty()) {
os << "Categories: ";
interleave(Categories.begin(), Categories.end(),
[&os](clang::ObjCCategoryDecl *category) {
os << category->getClassInterface()->getName()
<< "(" << category->getName() << ")";
},
[&os] {
os << ", ";
});
llvm::interleave(
Categories.begin(), Categories.end(),
[&os](clang::ObjCCategoryDecl *category) {
os << category->getClassInterface()->getName() << "("
<< category->getName() << ")";
},
[&os] { os << ", "; });
os << "\n";
} else if (Reader && !Reader->categories().empty()) {
os << "Categories: ";
interleave(Reader->categories().begin(), Reader->categories().end(),
[&os](clang::serialization::DeclID declID) {
os << "decl ID #" << declID;
},
[&os] {
os << ", ";
});
llvm::interleave(
Reader->categories().begin(), Reader->categories().end(),
[&os](clang::serialization::DeclID declID) {
os << "decl ID #" << declID;
},
[&os] { os << ", "; });
os << "\n";
}

Expand All @@ -1020,13 +1015,10 @@ void SwiftLookupTable::dump(raw_ostream &os) const {
os << ": ";

const auto &entries = GlobalsAsMembersIndex.find(context)->second;
interleave(entries.begin(), entries.end(),
[this, &os](uint64_t entry) {
printStoredEntry(this, entry, os);
},
[&os] {
os << ", ";
});
llvm::interleave(
entries.begin(), entries.end(),
[this, &os](uint64_t entry) { printStoredEntry(this, entry, os); },
[&os] { os << ", "; });
os << "\n";
}
}
Expand Down
46 changes: 23 additions & 23 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,29 +706,29 @@ class NodePrinter {
bool hasLabels = LabelList && LabelList->getNumChildren() > 0;

Printer << '(';
interleave(Parameters->begin(), Parameters->end(),
[&](NodePointer Param) {
assert(Param->getKind() == Node::Kind::TupleElement);

if (hasLabels) {
Printer << getLabelFor(Param, ParamIndex) << ':';
} else if (!showTypes) {
if (auto Label = getChildIf(Param,
Node::Kind::TupleElementName))
Printer << Label->getText() << ":";
else
Printer << "_:";
}

if (hasLabels && showTypes)
Printer << ' ';

++ParamIndex;

if (showTypes)
print(Param);
},
[&]() { Printer << (showTypes ? ", " : ""); });
llvm::interleave(
Parameters->begin(), Parameters->end(),
[&](NodePointer Param) {
assert(Param->getKind() == Node::Kind::TupleElement);

if (hasLabels) {
Printer << getLabelFor(Param, ParamIndex) << ':';
} else if (!showTypes) {
if (auto Label = getChildIf(Param, Node::Kind::TupleElementName))
Printer << Label->getText() << ":";
else
Printer << "_:";
}

if (hasLabels && showTypes)
Printer << ' ';

++ParamIndex;

if (showTypes)
print(Param);
},
[&]() { Printer << (showTypes ? ", " : ""); });
Printer << ')';
}

Expand Down
22 changes: 12 additions & 10 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2844,15 +2844,16 @@ Job *Driver::buildJobsForAction(Compilation &C, const JobAction *JA,
<< "\" - \"" << llvm::sys::path::filename(J->getExecutable())
<< "\", inputs: [";

interleave(InputActions.begin(), InputActions.end(),
[](const Action *A) {
auto Input = cast<InputAction>(A);
llvm::outs() << '"' << Input->getInputArg().getValue() << '"';
},
[] { llvm::outs() << ", "; });
llvm::interleave(
InputActions.begin(), InputActions.end(),
[](const Action *A) {
auto Input = cast<InputAction>(A);
llvm::outs() << '"' << Input->getInputArg().getValue() << '"';
},
[] { llvm::outs() << ", "; });
if (!InputActions.empty() && !J->getInputs().empty())
llvm::outs() << ", ";
interleave(
llvm::interleave(
J->getInputs().begin(), J->getInputs().end(),
[](const Job *Input) {
auto FileNames = Input->getOutput().getPrimaryOutputFilenames();
Expand Down Expand Up @@ -3305,9 +3306,10 @@ static unsigned printActions(const Action *A,
os << "\"" << IA->getInputArg().getValue() << "\"";
} else {
os << "{";
interleave(*cast<JobAction>(A),
[&](const Action *Input) { os << printActions(Input, Ids); },
[&] { os << ", "; });
llvm::interleave(
*cast<JobAction>(A),
[&](const Action *Input) { os << printActions(Input, Ids); },
[&] { os << ", "; });
os << "}";
}

Expand Down
Loading