mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
* Enforce using headers from Swift's LLVMSupport fork and not llvm-project when building stdlib * [LLVMSupport] Re-import LLVMSupport .cpp and .h files from 9ff3a9759b7c2f146e7f46e4aebc60453c577c5a from apple/llvm-project Done via the following commands, while having llvm-project checked out at 9ff3a9759b7c2f146e7f46e4aebc60453c577c5a, a commit on the stable/20210726 branch of apple/llvm-project, <9ff3a9759b>: for i in swift/stdlib/public/LLVMSupport/*.cpp ; do cp llvm-project/llvm/lib/Support/$(basename $i) $i ; done for i in swift/stdlib/include/llvm/ADT/*.h; do cp llvm-project/llvm/include/llvm/ADT/$(basename $i) $i ; done for i in swift/stdlib/include/llvm/Support/*.h; do cp llvm-project/llvm/include/llvm/Support/$(basename $i) $i ; done cp llvm-project/llvm/include/llvm/ADT/ScopeExit.h swift/stdlib/include/llvm/ADT/ScopeExit.h cp llvm-project/llvm/include/llvm/ADT/Twine.h swift/stdlib/include/llvm/ADT/Twine.h cp llvm-project/llvm/include/llvm/Support/raw_ostream.h swift/stdlib/include/llvm/Support/raw_ostream.h * [LLVMSupport] Re-namespace the LLVMSupport fork after re-forking by re-applyingb72788c27aMore precisely: 1) git cherry-pickb72788c27a2) manually resolve the conflict in AlignOf.h by keeping the HEAD's version of the chunk and discarding the cherry-pick's change 3) git add AlignOf.h 4) git status | grep "deleted by us" | awk '{print($4)}' | xargs git rm 5) git cherry-pick --continue Original namespacing commit message: > This adds the `__swift::__runtime` inline namespace to the LLVMSupport > interfaces. This avoids an ODR violation when LLVM and Swift are in the > same address space. It also will aid in the process of pruning the > LLVMSupport library by ensuring that accidental leakage of the llvm > namespace does not allow us to remove symbols which we rely on. * [LLVMSupport] Re-apply "pruning" on re-forked LLVMSupport frombb102707edThis re-applies the "pruning" commit frombb102707ed, which did the following: - Remove many whole files, - Remove "epoch tracking" and "reverse iteration" support from ADT containers - Remove "ABI break checking" support from STLExtras - Remove float parsing functions from StringExtras.h - Remove APInt/APSInt dependencies from StringRef.h + StringRef.cpp (edit distance, int parsing) - Remove some variants of error handling and dependency of dbgs() from ErrorHandling.h and ErrorHandling.cpp We don't need to do the whole-file-removal step, because that's already done, but the rest is re-applied by doing: 1) git cherry-pickbb102707ed2) manually resolving conflict in ADT/DenseMap.h by keeping HEAD's version of the chunk and removing epoch tracking from it 3) manually resolving conflict in ADT/STLExtras.h by keeping HEAD's version of the chunk and removing ABI check checking from it 4) manually resolving conflict in ADT/StringExtras.h by deleting the whole chunk (removing APInt/APSInt dependent functions) 5) manually resolving conflict in ErrorHandling.cpp by force-applying the cherry-pick's version (removing write() calls and OOM callback) 6) manually resolving the three conflicts in CMakeLists.txt files by keeping HEAD's version completely 7) git add stdlib/include/llvm/{ADT/StringSwitch.h,ADT/Twine.h,Support/raw_ostream.h} Original commit description: > Reduce LLVMSupport to the subset required for the runtime. This reduces > the TCB and the overheads of the runtime. The inline namespace's > preservation ensures that ODR violations do not occur. * [LLVMSupport] Re-apply all post-import modifications on LLVMSupport that the Swift's fork has Since the previous commits re-imported "vanilla" versions of LLVMSupport, we need to re-apply all modifications that the Swift's fork has made since the last import. More precisely: 1) git diff 7b70120440cd39d67a595a7d0ea4e828ecc6ee44..origin/main -- stdlib/include/llvm stdlib/public/LLVMSupport | git apply -3 --exclude "stdlib/include/llvm/Support/DataTypes.h" --exclude "stdlib/include/llvm/Config/llvm-config.h.cmake" 2) manually resolve conflict in STLExtras.h by applying the "__swift::__runtime" prefix to HEAD's version 3) manually resolve conflicts in StringSwitch.h by keeping HEAD's version (removing the Unicode BOM marker at the beginning of the file, keeping LLVM's version of the string functions) 4) manually resolve conflict in SwapByteOrder.h by adding the `defined(__wasi__)` part into the #if * [LLVMSupport] Drop remaining dependencies on APSInt.h, Error.h, DataTypes.h and STLForwardCompat.h Most cases can drop the #includes without any changes, in some cases there are straighforward replacements (climits, cstdint). For STLForwardCompat.h, we need to bring in parts of STLForwardCompat.h from llvm-project. * [LLVMSupport] Remove raw_ostream.h and drop dependencies to it from the runtime * [LLVMSupport] Simplify error reporting in SmallVector and avoid using std::string when producing fatal errors messages Co-authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
358 lines
13 KiB
C++
358 lines
13 KiB
C++
//===- iterator.h - Utilities for using and defining iterators --*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_ADT_ITERATOR_H
|
|
#define LLVM_ADT_ITERATOR_H
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
#include <cstddef>
|
|
#include <iterator>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
inline namespace __swift { inline namespace __runtime {
|
|
namespace llvm {
|
|
|
|
/// CRTP base class which implements the entire standard iterator facade
|
|
/// in terms of a minimal subset of the interface.
|
|
///
|
|
/// Use this when it is reasonable to implement most of the iterator
|
|
/// functionality in terms of a core subset. If you need special behavior or
|
|
/// there are performance implications for this, you may want to override the
|
|
/// relevant members instead.
|
|
///
|
|
/// Note, one abstraction that this does *not* provide is implementing
|
|
/// subtraction in terms of addition by negating the difference. Negation isn't
|
|
/// always information preserving, and I can see very reasonable iterator
|
|
/// designs where this doesn't work well. It doesn't really force much added
|
|
/// boilerplate anyways.
|
|
///
|
|
/// Another abstraction that this doesn't provide is implementing increment in
|
|
/// terms of addition of one. These aren't equivalent for all iterator
|
|
/// categories, and respecting that adds a lot of complexity for little gain.
|
|
///
|
|
/// Classes wishing to use `iterator_facade_base` should implement the following
|
|
/// methods:
|
|
///
|
|
/// Forward Iterators:
|
|
/// (All of the following methods)
|
|
/// - DerivedT &operator=(const DerivedT &R);
|
|
/// - bool operator==(const DerivedT &R) const;
|
|
/// - const T &operator*() const;
|
|
/// - T &operator*();
|
|
/// - DerivedT &operator++();
|
|
///
|
|
/// Bidirectional Iterators:
|
|
/// (All methods of forward iterators, plus the following)
|
|
/// - DerivedT &operator--();
|
|
///
|
|
/// Random-access Iterators:
|
|
/// (All methods of bidirectional iterators excluding the following)
|
|
/// - DerivedT &operator++();
|
|
/// - DerivedT &operator--();
|
|
/// (and plus the following)
|
|
/// - bool operator<(const DerivedT &RHS) const;
|
|
/// - DifferenceTypeT operator-(const DerivedT &R) const;
|
|
/// - DerivedT &operator+=(DifferenceTypeT N);
|
|
/// - DerivedT &operator-=(DifferenceTypeT N);
|
|
///
|
|
template <typename DerivedT, typename IteratorCategoryT, typename T,
|
|
typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
|
|
typename ReferenceT = T &>
|
|
class iterator_facade_base {
|
|
public:
|
|
using iterator_category = IteratorCategoryT;
|
|
using value_type = T;
|
|
using difference_type = DifferenceTypeT;
|
|
using pointer = PointerT;
|
|
using reference = ReferenceT;
|
|
|
|
protected:
|
|
enum {
|
|
IsRandomAccess = std::is_base_of<std::random_access_iterator_tag,
|
|
IteratorCategoryT>::value,
|
|
IsBidirectional = std::is_base_of<std::bidirectional_iterator_tag,
|
|
IteratorCategoryT>::value,
|
|
};
|
|
|
|
/// A proxy object for computing a reference via indirecting a copy of an
|
|
/// iterator. This is used in APIs which need to produce a reference via
|
|
/// indirection but for which the iterator object might be a temporary. The
|
|
/// proxy preserves the iterator internally and exposes the indirected
|
|
/// reference via a conversion operator.
|
|
class ReferenceProxy {
|
|
friend iterator_facade_base;
|
|
|
|
DerivedT I;
|
|
|
|
ReferenceProxy(DerivedT I) : I(std::move(I)) {}
|
|
|
|
public:
|
|
operator ReferenceT() const { return *I; }
|
|
};
|
|
|
|
public:
|
|
DerivedT operator+(DifferenceTypeT n) const {
|
|
static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
|
|
"Must pass the derived type to this template!");
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"The '+' operator is only defined for random access iterators.");
|
|
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
|
tmp += n;
|
|
return tmp;
|
|
}
|
|
friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"The '+' operator is only defined for random access iterators.");
|
|
return i + n;
|
|
}
|
|
DerivedT operator-(DifferenceTypeT n) const {
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"The '-' operator is only defined for random access iterators.");
|
|
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
|
tmp -= n;
|
|
return tmp;
|
|
}
|
|
|
|
DerivedT &operator++() {
|
|
static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
|
|
"Must pass the derived type to this template!");
|
|
return static_cast<DerivedT *>(this)->operator+=(1);
|
|
}
|
|
DerivedT operator++(int) {
|
|
DerivedT tmp = *static_cast<DerivedT *>(this);
|
|
++*static_cast<DerivedT *>(this);
|
|
return tmp;
|
|
}
|
|
DerivedT &operator--() {
|
|
static_assert(
|
|
IsBidirectional,
|
|
"The decrement operator is only defined for bidirectional iterators.");
|
|
return static_cast<DerivedT *>(this)->operator-=(1);
|
|
}
|
|
DerivedT operator--(int) {
|
|
static_assert(
|
|
IsBidirectional,
|
|
"The decrement operator is only defined for bidirectional iterators.");
|
|
DerivedT tmp = *static_cast<DerivedT *>(this);
|
|
--*static_cast<DerivedT *>(this);
|
|
return tmp;
|
|
}
|
|
|
|
#ifndef __cpp_impl_three_way_comparison
|
|
bool operator!=(const DerivedT &RHS) const {
|
|
return !(static_cast<const DerivedT &>(*this) == RHS);
|
|
}
|
|
#endif
|
|
|
|
bool operator>(const DerivedT &RHS) const {
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"Relational operators are only defined for random access iterators.");
|
|
return !(static_cast<const DerivedT &>(*this) < RHS) &&
|
|
!(static_cast<const DerivedT &>(*this) == RHS);
|
|
}
|
|
bool operator<=(const DerivedT &RHS) const {
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"Relational operators are only defined for random access iterators.");
|
|
return !(static_cast<const DerivedT &>(*this) > RHS);
|
|
}
|
|
bool operator>=(const DerivedT &RHS) const {
|
|
static_assert(
|
|
IsRandomAccess,
|
|
"Relational operators are only defined for random access iterators.");
|
|
return !(static_cast<const DerivedT &>(*this) < RHS);
|
|
}
|
|
|
|
PointerT operator->() { return &static_cast<DerivedT *>(this)->operator*(); }
|
|
PointerT operator->() const {
|
|
return &static_cast<const DerivedT *>(this)->operator*();
|
|
}
|
|
ReferenceProxy operator[](DifferenceTypeT n) {
|
|
static_assert(IsRandomAccess,
|
|
"Subscripting is only defined for random access iterators.");
|
|
return ReferenceProxy(static_cast<DerivedT *>(this)->operator+(n));
|
|
}
|
|
ReferenceProxy operator[](DifferenceTypeT n) const {
|
|
static_assert(IsRandomAccess,
|
|
"Subscripting is only defined for random access iterators.");
|
|
return ReferenceProxy(static_cast<const DerivedT *>(this)->operator+(n));
|
|
}
|
|
};
|
|
|
|
/// CRTP base class for adapting an iterator to a different type.
|
|
///
|
|
/// This class can be used through CRTP to adapt one iterator into another.
|
|
/// Typically this is done through providing in the derived class a custom \c
|
|
/// operator* implementation. Other methods can be overridden as well.
|
|
template <
|
|
typename DerivedT, typename WrappedIteratorT,
|
|
typename IteratorCategoryT =
|
|
typename std::iterator_traits<WrappedIteratorT>::iterator_category,
|
|
typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
|
|
typename DifferenceTypeT =
|
|
typename std::iterator_traits<WrappedIteratorT>::difference_type,
|
|
typename PointerT = std::conditional_t<
|
|
std::is_same<T, typename std::iterator_traits<
|
|
WrappedIteratorT>::value_type>::value,
|
|
typename std::iterator_traits<WrappedIteratorT>::pointer, T *>,
|
|
typename ReferenceT = std::conditional_t<
|
|
std::is_same<T, typename std::iterator_traits<
|
|
WrappedIteratorT>::value_type>::value,
|
|
typename std::iterator_traits<WrappedIteratorT>::reference, T &>>
|
|
class iterator_adaptor_base
|
|
: public iterator_facade_base<DerivedT, IteratorCategoryT, T,
|
|
DifferenceTypeT, PointerT, ReferenceT> {
|
|
using BaseT = typename iterator_adaptor_base::iterator_facade_base;
|
|
|
|
protected:
|
|
WrappedIteratorT I;
|
|
|
|
iterator_adaptor_base() = default;
|
|
|
|
explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {
|
|
static_assert(std::is_base_of<iterator_adaptor_base, DerivedT>::value,
|
|
"Must pass the derived type to this template!");
|
|
}
|
|
|
|
const WrappedIteratorT &wrapped() const { return I; }
|
|
|
|
public:
|
|
using difference_type = DifferenceTypeT;
|
|
|
|
DerivedT &operator+=(difference_type n) {
|
|
static_assert(
|
|
BaseT::IsRandomAccess,
|
|
"The '+=' operator is only defined for random access iterators.");
|
|
I += n;
|
|
return *static_cast<DerivedT *>(this);
|
|
}
|
|
DerivedT &operator-=(difference_type n) {
|
|
static_assert(
|
|
BaseT::IsRandomAccess,
|
|
"The '-=' operator is only defined for random access iterators.");
|
|
I -= n;
|
|
return *static_cast<DerivedT *>(this);
|
|
}
|
|
using BaseT::operator-;
|
|
difference_type operator-(const DerivedT &RHS) const {
|
|
static_assert(
|
|
BaseT::IsRandomAccess,
|
|
"The '-' operator is only defined for random access iterators.");
|
|
return I - RHS.I;
|
|
}
|
|
|
|
// We have to explicitly provide ++ and -- rather than letting the facade
|
|
// forward to += because WrappedIteratorT might not support +=.
|
|
using BaseT::operator++;
|
|
DerivedT &operator++() {
|
|
++I;
|
|
return *static_cast<DerivedT *>(this);
|
|
}
|
|
using BaseT::operator--;
|
|
DerivedT &operator--() {
|
|
static_assert(
|
|
BaseT::IsBidirectional,
|
|
"The decrement operator is only defined for bidirectional iterators.");
|
|
--I;
|
|
return *static_cast<DerivedT *>(this);
|
|
}
|
|
|
|
friend bool operator==(const iterator_adaptor_base &LHS,
|
|
const iterator_adaptor_base &RHS) {
|
|
return LHS.I == RHS.I;
|
|
}
|
|
friend bool operator<(const iterator_adaptor_base &LHS,
|
|
const iterator_adaptor_base &RHS) {
|
|
static_assert(
|
|
BaseT::IsRandomAccess,
|
|
"Relational operators are only defined for random access iterators.");
|
|
return LHS.I < RHS.I;
|
|
}
|
|
|
|
ReferenceT operator*() const { return *I; }
|
|
};
|
|
|
|
/// An iterator type that allows iterating over the pointees via some
|
|
/// other iterator.
|
|
///
|
|
/// The typical usage of this is to expose a type that iterates over Ts, but
|
|
/// which is implemented with some iterator over T*s:
|
|
///
|
|
/// \code
|
|
/// using iterator = pointee_iterator<SmallVectorImpl<T *>::iterator>;
|
|
/// \endcode
|
|
template <typename WrappedIteratorT,
|
|
typename T = std::remove_reference_t<decltype(
|
|
**std::declval<WrappedIteratorT>())>>
|
|
struct pointee_iterator
|
|
: iterator_adaptor_base<
|
|
pointee_iterator<WrappedIteratorT, T>, WrappedIteratorT,
|
|
typename std::iterator_traits<WrappedIteratorT>::iterator_category,
|
|
T> {
|
|
pointee_iterator() = default;
|
|
template <typename U>
|
|
pointee_iterator(U &&u)
|
|
: pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
|
|
|
|
T &operator*() const { return **this->I; }
|
|
};
|
|
|
|
template <typename RangeT, typename WrappedIteratorT =
|
|
decltype(std::begin(std::declval<RangeT>()))>
|
|
iterator_range<pointee_iterator<WrappedIteratorT>>
|
|
make_pointee_range(RangeT &&Range) {
|
|
using PointeeIteratorT = pointee_iterator<WrappedIteratorT>;
|
|
return make_range(PointeeIteratorT(std::begin(std::forward<RangeT>(Range))),
|
|
PointeeIteratorT(std::end(std::forward<RangeT>(Range))));
|
|
}
|
|
|
|
template <typename WrappedIteratorT,
|
|
typename T = decltype(&*std::declval<WrappedIteratorT>())>
|
|
class pointer_iterator
|
|
: public iterator_adaptor_base<
|
|
pointer_iterator<WrappedIteratorT, T>, WrappedIteratorT,
|
|
typename std::iterator_traits<WrappedIteratorT>::iterator_category,
|
|
T> {
|
|
mutable T Ptr;
|
|
|
|
public:
|
|
pointer_iterator() = default;
|
|
|
|
explicit pointer_iterator(WrappedIteratorT u)
|
|
: pointer_iterator::iterator_adaptor_base(std::move(u)) {}
|
|
|
|
T &operator*() { return Ptr = &*this->I; }
|
|
const T &operator*() const { return Ptr = &*this->I; }
|
|
};
|
|
|
|
template <typename RangeT, typename WrappedIteratorT =
|
|
decltype(std::begin(std::declval<RangeT>()))>
|
|
iterator_range<pointer_iterator<WrappedIteratorT>>
|
|
make_pointer_range(RangeT &&Range) {
|
|
using PointerIteratorT = pointer_iterator<WrappedIteratorT>;
|
|
return make_range(PointerIteratorT(std::begin(std::forward<RangeT>(Range))),
|
|
PointerIteratorT(std::end(std::forward<RangeT>(Range))));
|
|
}
|
|
|
|
template <typename WrappedIteratorT,
|
|
typename T1 = std::remove_reference_t<decltype(
|
|
**std::declval<WrappedIteratorT>())>,
|
|
typename T2 = std::add_pointer_t<T1>>
|
|
using raw_pointer_iterator =
|
|
pointer_iterator<pointee_iterator<WrappedIteratorT, T1>, T2>;
|
|
|
|
} // end namespace llvm
|
|
}} // namespace swift::runtime
|
|
|
|
#endif // LLVM_ADT_ITERATOR_H
|