Skip to content

Remove IteratorRange in favor of llvm::iterator_range #27574

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
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/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ class DeclAttributes {
public:
template <typename ATTR, bool AllowInvalid>
using AttributeKindRange =
OptionalTransformRange<llvm::iterator_range<const_iterator>,
OptionalTransformRange<iterator_range<const_iterator>,
ToAttributeKind<ATTR, AllowInvalid>,
const_iterator>;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ class DeclIterator {

/// The range of declarations stored within an iterable declaration
/// context.
typedef IteratorRange<DeclIterator> DeclRange;
using DeclRange = iterator_range<DeclIterator>;

/// The kind of an \c IterableDeclContext.
enum class IterableDeclContextKind : uint8_t {
Expand Down
12 changes: 4 additions & 8 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3965,13 +3965,11 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
};
using IndirectFormalResultIter =
llvm::filter_iterator<const SILResultInfo *, IndirectFormalResultFilter>;
using IndirectFormalResultRange = IteratorRange<IndirectFormalResultIter>;
using IndirectFormalResultRange = iterator_range<IndirectFormalResultIter>;

/// A range of SILResultInfo for all formally indirect results.
IndirectFormalResultRange getIndirectFormalResults() const {
auto filter =
llvm::make_filter_range(getResults(), IndirectFormalResultFilter());
return makeIteratorRange(filter.begin(), filter.end());
return llvm::make_filter_range(getResults(), IndirectFormalResultFilter());
}

struct DirectFormalResultFilter {
Expand All @@ -3981,13 +3979,11 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
};
using DirectFormalResultIter =
llvm::filter_iterator<const SILResultInfo *, DirectFormalResultFilter>;
using DirectFormalResultRange = IteratorRange<DirectFormalResultIter>;
using DirectFormalResultRange = iterator_range<DirectFormalResultIter>;

/// A range of SILResultInfo for all formally direct results.
DirectFormalResultRange getDirectFormalResults() const {
auto filter =
llvm::make_filter_range(getResults(), DirectFormalResultFilter());
return makeIteratorRange(filter.begin(), filter.end());
return llvm::make_filter_range(getResults(), DirectFormalResultFilter());
}

/// Get a single non-address SILType that represents all formal direct
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Basic/BlotSetVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ class BlotSetVector {

ArrayRef<Optional<ValueT>> getArray() const { return vector; }

llvm::iterator_range<const_iterator> getRange() const {
iterator_range<const_iterator> getRange() const {
return {begin(), end()};
}

using const_reverse_iterator = typename VectorT::const_reverse_iterator;
const_reverse_iterator rbegin() const { return vector.rbegin(); }
const_reverse_iterator rend() const { return vector.rend(); }
llvm::iterator_range<const_reverse_iterator> getReverseRange() const {
iterator_range<const_reverse_iterator> getReverseRange() const {
return {rbegin(), rend()};
}

Expand Down
2 changes: 2 additions & 0 deletions include/swift/Basic/LLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace llvm {
template<typename T> class TinyPtrVector;
template<typename T> class Optional;
template <typename ...PTs> class PointerUnion;
template <typename IteratorT> class iterator_range;
class SmallBitVector;

// Other common classes.
Expand All @@ -63,6 +64,7 @@ namespace swift {

// Containers.
using llvm::ArrayRef;
using llvm::iterator_range;
using llvm::MutableArrayRef;
using llvm::None;
using llvm::Optional;
Expand Down
1 change: 0 additions & 1 deletion include/swift/Basic/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

namespace swift {
using llvm::make_range;
using llvm::iterator_range;

template<typename T>
inline auto reversed(T &&container)
Expand Down
30 changes: 3 additions & 27 deletions include/swift/Basic/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,37 +251,13 @@ inline Iterator prev_or_begin(Iterator it, Iterator begin) {

/// @}

/// A range of iterators.
/// TODO: Add `llvm::iterator_range::empty()`, then remove this helper, along
/// with the superfluous TransformIterator.
template<typename Iterator>
class IteratorRange {
Iterator First, Last;

public:
using iterator = Iterator;

IteratorRange(Iterator first, Iterator last) : First(first), Last(last) { }
iterator begin() const { return First; }
iterator end() const { return Last; }
bool empty() const { return First == Last; }

typename std::iterator_traits<iterator>::value_type front() const {
assert(!empty() && "Front of empty range");
return *begin();
}
};

/// Create a new iterator range.
template<typename Iterator>
inline IteratorRange<Iterator>
makeIteratorRange(Iterator first, Iterator last) {
return IteratorRange<Iterator>(first, last);
}

/// An iterator that transforms the result of an underlying bidirectional
/// iterator with a given operation.
///
/// Slightly different semantics from llvm::map_iterator, but we should
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the different semantics here? Just seems out of place with the rest of the PR. Can you elaborate in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's something about whether the underlying iterator returns a reference or not. I don't actually remember the details, but I tried to remove it and failed and I want to leave a warning for others to come.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not the purpose of the PR, as you note, so if you object to it going in in this form I'll just drop it.

/// probably figure out how to merge them eventually.
///
/// \tparam Iterator the underlying iterator.
///
/// \tparam Operation A function object that transforms the underlying
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class DeserializationNotificationHandlerSet final
using iterator = llvm::mapped_iterator<
decltype(handlerSet)::const_iterator,
decltype(&DeserializationNotificationHandlerSet::getUnderlyingHandler)>;
using range = llvm::iterator_range<iterator>;
using range = iterator_range<iterator>;

/// Returns an iterator to the first element of the handler set.
///
Expand Down
43 changes: 13 additions & 30 deletions include/swift/SIL/SILFunctionConventions.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ class SILFunctionConventions {
if (silConv.loweredAddresses)
return funcTy->getIndirectFormalResults();

auto filter = llvm::make_filter_range(
makeIteratorRange((const SILResultInfo *)0, (const SILResultInfo *)0),
return llvm::make_filter_range(
llvm::make_range((const SILResultInfo *)0, (const SILResultInfo *)0),
SILFunctionType::IndirectFormalResultFilter());
return makeIteratorRange(filter.begin(), filter.end());
}

struct SILResultTypeFunc {
Expand All @@ -210,16 +209,12 @@ class SILFunctionConventions {

using IndirectSILResultTypeIter =
llvm::mapped_iterator<IndirectSILResultIter, SILResultTypeFunc>;
using IndirectSILResultTypeRange = IteratorRange<IndirectSILResultTypeIter>;
using IndirectSILResultTypeRange = iterator_range<IndirectSILResultTypeIter>;

/// Return a range of SILTypes for each result passed as an address-typed SIL
/// argument.
IndirectSILResultTypeRange getIndirectSILResultTypes() const {
return makeIteratorRange(
IndirectSILResultTypeIter(getIndirectSILResults().begin(),
SILResultTypeFunc(silConv)),
IndirectSILResultTypeIter(getIndirectSILResults().end(),
SILResultTypeFunc(silConv)));
return llvm::map_range(getIndirectSILResults(), SILResultTypeFunc(silConv));
}

/// Get the number of SIL results directly returned by SIL value.
Expand All @@ -238,28 +233,23 @@ class SILFunctionConventions {
};
using DirectSILResultIter =
llvm::filter_iterator<const SILResultInfo *, DirectSILResultFilter>;
using DirectSILResultRange = IteratorRange<DirectSILResultIter>;
using DirectSILResultRange = iterator_range<DirectSILResultIter>;

/// Return a range of direct result information for results directly returned
/// by SIL value.
DirectSILResultRange getDirectSILResults() const {
auto filter = llvm::make_filter_range(
return llvm::make_filter_range(
funcTy->getResults(), DirectSILResultFilter(silConv.loweredAddresses));
return makeIteratorRange(filter.begin(), filter.end());
}

using DirectSILResultTypeIter =
llvm::mapped_iterator<DirectSILResultIter, SILResultTypeFunc>;
using DirectSILResultTypeRange = IteratorRange<DirectSILResultTypeIter>;
using DirectSILResultTypeRange = iterator_range<DirectSILResultTypeIter>;

/// Return a range of SILTypes for each result directly returned
/// by SIL value.
DirectSILResultTypeRange getDirectSILResultTypes() const {
return makeIteratorRange(
DirectSILResultTypeIter(getDirectSILResults().begin(),
SILResultTypeFunc(silConv)),
DirectSILResultTypeIter(getDirectSILResults().end(),
SILResultTypeFunc(silConv)));
return llvm::map_range(getDirectSILResults(), SILResultTypeFunc(silConv));
}

//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -288,16 +278,13 @@ class SILFunctionConventions {

using SILParameterTypeIter =
llvm::mapped_iterator<const SILParameterInfo *, SILParameterTypeFunc>;
using SILParameterTypeRange = IteratorRange<SILParameterTypeIter>;
using SILParameterTypeRange = iterator_range<SILParameterTypeIter>;

/// Return a range of SILTypes for each function parameter, not including
/// indirect results.
SILParameterTypeRange getParameterSILTypes() const {
return makeIteratorRange(
SILParameterTypeIter(funcTy->getParameters().begin(),
SILParameterTypeFunc(silConv)),
SILParameterTypeIter(funcTy->getParameters().end(),
SILParameterTypeFunc(silConv)));
return llvm::map_range(funcTy->getParameters(),
SILParameterTypeFunc(silConv));
}

//===--------------------------------------------------------------------===//
Expand All @@ -312,14 +299,10 @@ class SILFunctionConventions {

using SILYieldTypeIter =
llvm::mapped_iterator<const SILYieldInfo *, SILParameterTypeFunc>;
using SILYieldTypeRange = IteratorRange<SILYieldTypeIter>;
using SILYieldTypeRange = iterator_range<SILYieldTypeIter>;

SILYieldTypeRange getYieldSILTypes() const {
return makeIteratorRange(
SILYieldTypeIter(funcTy->getYields().begin(),
SILParameterTypeFunc(silConv)),
SILYieldTypeIter(funcTy->getYields().end(),
SILParameterTypeFunc(silConv)));
return llvm::map_range(funcTy->getYields(), SILParameterTypeFunc(silConv));
}

SILYieldInfo getYieldInfoForOperandIndex(unsigned opIndex) const {
Expand Down
6 changes: 3 additions & 3 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ class SILInstructionResultArray {
reverse_iterator rbegin() const;
reverse_iterator rend() const;

using range = llvm::iterator_range<iterator>;
using range = iterator_range<iterator>;
range getValues() const;
using reverse_range = llvm::iterator_range<reverse_iterator>;
using reverse_range = iterator_range<reverse_iterator>;
reverse_range getReversedValues() const;

using type_range = llvm::iterator_range<
using type_range = iterator_range<
llvm::mapped_iterator<iterator, SILType(*)(SILValue), SILType>>;
type_range getTypes() const;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class ValueBase : public SILNode, public SILAllocated<ValueBase> {
template <typename Subclass>
using DowncastUserFilterRange =
DowncastFilterRange<Subclass,
llvm::iterator_range<llvm::mapped_iterator<
iterator_range<llvm::mapped_iterator<
use_iterator, UseToUser, SILInstruction *>>>;

/// Iterate over the use list of this ValueBase visiting all users that are of
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Analysis/ClosureScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ClosureScopeAnalysis : public SILAnalysis {
return None;
}
};
using IndexRange = IteratorRange<int *>;
using IndexRange = iterator_range<int *>;

public:
// A range of SILFunction scopes converted from their scope indices and
Expand Down
8 changes: 4 additions & 4 deletions include/swift/SILOptimizer/Analysis/LoopRegionAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,11 @@ class LoopRegion {
return getSubregionData().rend();
}

llvm::iterator_range<subregion_iterator> getSubregions() const {
iterator_range<subregion_iterator> getSubregions() const {
return {subregion_begin(), subregion_end()};
}

llvm::iterator_range<subregion_reverse_iterator>
iterator_range<subregion_reverse_iterator>
getReverseSubregions() const {
return {subregion_rbegin(), subregion_rend()};
}
Expand Down Expand Up @@ -674,7 +674,7 @@ class LoopRegion {
unsigned succ_size() const { return Succs.size(); }

private:
using InnerSuccRange = IteratorRange<decltype(Succs)::const_iterator>;
using InnerSuccRange = iterator_range<decltype(Succs)::const_iterator>;

public:
using SuccRange =
Expand Down Expand Up @@ -964,7 +964,7 @@ class LoopRegionFunctionInfo {
const_iterator end() const { return IDToRegionMap.end(); }
unsigned size() const { return IDToRegionMap.size(); }
bool empty() const { return IDToRegionMap.empty(); }
llvm::iterator_range<const_iterator> getRegions() const {
iterator_range<const_iterator> getRegions() const {
return {begin(), end()};
}

Expand Down
5 changes: 2 additions & 3 deletions include/swift/SILOptimizer/Utils/InstOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ template <class T> class NullablePtr;
/// Transform a Use Range (Operand*) into a User Range (SILInstruction*)
using UserTransform = std::function<SILInstruction *(Operand *)>;
using ValueBaseUserRange =
TransformRange<IteratorRange<ValueBase::use_iterator>, UserTransform>;
TransformRange<iterator_range<ValueBase::use_iterator>, UserTransform>;

inline ValueBaseUserRange
makeUserRange(iterator_range<ValueBase::use_iterator> range) {
auto toUser = [](Operand *operand) { return operand->getUser(); };
return makeTransformRange(makeIteratorRange(range.begin(), range.end()),
UserTransform(toUser));
return makeTransformRange(range, UserTransform(toUser));
}

using DeadInstructionSet = llvm::SmallSetVector<SILInstruction *, 8>;
Expand Down
3 changes: 1 addition & 2 deletions lib/FrontendTool/ImportedModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ static void findAllClangImports(const clang::Module *module,
modules.insert(getTopLevelName(imported));
}

for (auto sub :
makeIteratorRange(module->submodule_begin(), module->submodule_end())) {
for (auto sub : module->submodules()) {
findAllClangImports(sub, modules);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsObjC/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ void DeclAndTypePrinter::print(Type ty) {
}

void DeclAndTypePrinter::printAdHocCategory(
llvm::iterator_range<const ValueDecl * const *> members) {
iterator_range<const ValueDecl * const *> members) {
getImpl().printAdHocCategory(members);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsObjC/DeclAndTypePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DeclAndTypePrinter {
///
/// All members must have the same parent type. The list must not be empty.
void
printAdHocCategory(llvm::iterator_range<const ValueDecl * const *> members);
printAdHocCategory(iterator_range<const ValueDecl * const *> members);

/// Returns the name of an <os/object.h> type minus the leading "OS_",
/// or an empty string if \p decl is not an <os/object.h> type.
Expand Down
5 changes: 3 additions & 2 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,13 +2027,14 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
require(initConv.getNumDirectSILResults() == 1,
"wrong number of init function results");
require(Dest->getType().getObjectType() ==
initConv.getDirectSILResultTypes().front(),
*initConv.getDirectSILResultTypes().begin(),
"wrong init function result type");
break;
case 1:
require(initConv.getNumDirectSILResults() == 0,
"wrong number of init function results");
require(Dest->getType() == initConv.getIndirectSILResultTypes().front(),
require(Dest->getType() ==
*initConv.getIndirectSILResultTypes().begin(),
"wrong indirect init function result type");
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Transforms/CopyPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class LivenessInfo {
originalDestroyBlocks.insert(use->getUser()->getParent());
}

llvm::iterator_range<BlockSetVec::const_iterator>
iterator_range<BlockSetVec::const_iterator>
getOriginalDestroyBlocks() const {
return originalDestroyBlocks;
}
Expand Down