Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit db16b4f

Browse files
authored
Merge pull request #31 from tfiala/for-lldb
Changes for LLDB upstream merge
2 parents 6540a7a + f5d747c commit db16b4f

File tree

12 files changed

+956
-39
lines changed

12 files changed

+956
-39
lines changed

include/llvm/ADT/ArrayRef.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ namespace llvm {
195195
return slice(0, size() - N);
196196
}
197197

198+
/// \brief Return a copy of *this with only the first \p N elements.
199+
LLVM_ATTRIBUTE_UNUSED_RESULT
200+
ArrayRef<T> take_front(size_t N = 1) const {
201+
if (N >= size())
202+
return *this;
203+
return drop_back(size() - N);
204+
}
205+
206+
/// \brief Return a copy of *this with only the last \p N elements.
207+
LLVM_ATTRIBUTE_UNUSED_RESULT
208+
ArrayRef<T> take_back(size_t N = 1) const {
209+
if (N >= size())
210+
return *this;
211+
return drop_front(size() - N);
212+
}
213+
198214
/// @}
199215
/// @name Operator Overloads
200216
/// @{
@@ -203,6 +219,22 @@ namespace llvm {
203219
return Data[Index];
204220
}
205221

222+
/// Disallow accidental assignment from a temporary.
223+
///
224+
/// The declaration here is extra complicated so that "arrayRef = {}"
225+
/// continues to select the move assignment operator.
226+
template <typename U>
227+
typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
228+
operator=(U &&Temporary) = delete;
229+
230+
/// Disallow accidental assignment from a temporary.
231+
///
232+
/// The declaration here is extra complicated so that "arrayRef = {}"
233+
/// continues to select the move assignment operator.
234+
template <typename U>
235+
typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
236+
operator=(std::initializer_list<U>) = delete;
237+
206238
/// @}
207239
/// @name Expensive Operations
208240
/// @{
@@ -321,6 +353,22 @@ namespace llvm {
321353
return slice(0, this->size() - N);
322354
}
323355

356+
/// \brief Return a copy of *this with only the first \p N elements.
357+
LLVM_ATTRIBUTE_UNUSED_RESULT
358+
MutableArrayRef<T> take_front(size_t N = 1) const {
359+
if (N >= this->size())
360+
return *this;
361+
return drop_back(this->size() - N);
362+
}
363+
364+
/// \brief Return a copy of *this with only the last \p N elements.
365+
LLVM_ATTRIBUTE_UNUSED_RESULT
366+
MutableArrayRef<T> take_back(size_t N = 1) const {
367+
if (N >= this->size())
368+
return *this;
369+
return drop_front(this->size() - N);
370+
}
371+
324372
/// @}
325373
/// @name Operator Overloads
326374
/// @{

include/llvm/ADT/STLExtras.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace llvm {
3535
namespace detail {
3636

3737
template <typename RangeT>
38-
using IterOfRange = decltype(std::begin(std::declval<RangeT>()));
38+
using IterOfRange = decltype(std::begin(std::declval<RangeT &>()));
3939

4040
} // End detail namespace
4141

@@ -369,6 +369,11 @@ struct build_index_impl<0, I...> : index_sequence<I...> {};
369369
template <class... Ts>
370370
struct index_sequence_for : build_index_impl<sizeof...(Ts)> {};
371371

372+
/// Utility type to build an inheritance chain that makes it easy to rank
373+
/// overload candidates.
374+
template <int N> struct rank : rank<N - 1> {};
375+
template <> struct rank<0> {};
376+
372377
//===----------------------------------------------------------------------===//
373378
// Extra additions for arrays
374379
//===----------------------------------------------------------------------===//
@@ -608,6 +613,70 @@ template <typename T> struct deref {
608613
}
609614
};
610615

616+
namespace detail {
617+
template <typename R> class enumerator_impl {
618+
public:
619+
template <typename X> struct result_pair {
620+
result_pair(std::size_t Index, X Value) : Index(Index), Value(Value) {}
621+
622+
const std::size_t Index;
623+
X Value;
624+
};
625+
626+
class iterator {
627+
typedef
628+
typename std::iterator_traits<IterOfRange<R>>::reference iter_reference;
629+
typedef result_pair<iter_reference> result_type;
630+
631+
public:
632+
iterator(IterOfRange<R> &&Iter, std::size_t Index)
633+
: Iter(Iter), Index(Index) {}
634+
635+
result_type operator*() const { return result_type(Index, *Iter); }
636+
637+
iterator &operator++() {
638+
++Iter;
639+
++Index;
640+
return *this;
641+
}
642+
643+
bool operator!=(const iterator &RHS) const { return Iter != RHS.Iter; }
644+
645+
private:
646+
IterOfRange<R> Iter;
647+
std::size_t Index;
648+
};
649+
650+
public:
651+
explicit enumerator_impl(R &&Range) : Range(std::forward<R>(Range)) {}
652+
653+
iterator begin() { return iterator(std::begin(Range), 0); }
654+
iterator end() { return iterator(std::end(Range), std::size_t(-1)); }
655+
656+
private:
657+
R Range;
658+
};
659+
}
660+
661+
/// Given an input range, returns a new range whose values are are pair (A,B)
662+
/// such that A is the 0-based index of the item in the sequence, and B is
663+
/// the value from the original sequence. Example:
664+
///
665+
/// std::vector<char> Items = {'A', 'B', 'C', 'D'};
666+
/// for (auto X : enumerate(Items)) {
667+
/// printf("Item %d - %c\n", X.Item, X.Value);
668+
/// }
669+
///
670+
/// Output:
671+
/// Item 0 - A
672+
/// Item 1 - B
673+
/// Item 2 - C
674+
/// Item 3 - D
675+
///
676+
template <typename R> detail::enumerator_impl<R> enumerate(R &&Range) {
677+
return detail::enumerator_impl<R>(std::forward<R>(Range));
678+
}
679+
611680
} // End llvm namespace
612681

613682
#endif

include/llvm/ADT/StringExtras.h

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iterator>
2020

2121
namespace llvm {
22+
class raw_ostream;
2223
template<typename T> class SmallVectorImpl;
2324

2425
/// hexdigit - Return the hexadecimal character for the
@@ -150,6 +151,12 @@ static inline StringRef getOrdinalSuffix(unsigned Val) {
150151
}
151152
}
152153

154+
/// PrintEscapedString - Print each character of the specified string, escaping
155+
/// it if it is not printable or if it is an escape char.
156+
void PrintEscapedString(StringRef Name, raw_ostream &Out);
157+
158+
namespace detail {
159+
153160
template <typename IteratorT>
154161
inline std::string join_impl(IteratorT Begin, IteratorT End,
155162
StringRef Separator, std::input_iterator_tag) {
@@ -184,12 +191,64 @@ inline std::string join_impl(IteratorT Begin, IteratorT End,
184191
return S;
185192
}
186193

194+
template <typename Sep>
195+
inline void join_items_impl(std::string &Result, Sep Separator) {}
196+
197+
template <typename Sep, typename Arg>
198+
inline void join_items_impl(std::string &Result, Sep Separator,
199+
const Arg &Item) {
200+
Result += Item;
201+
}
202+
203+
template <typename Sep, typename Arg1, typename... Args>
204+
inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1,
205+
Args &&... Items) {
206+
Result += A1;
207+
Result += Separator;
208+
join_items_impl(Result, Separator, std::forward<Args>(Items)...);
209+
}
210+
211+
inline size_t join_one_item_size(char C) { return 1; }
212+
inline size_t join_one_item_size(const char *S) { return S ? ::strlen(S) : 0; }
213+
214+
template <typename T> inline size_t join_one_item_size(const T &Str) {
215+
return Str.size();
216+
}
217+
218+
inline size_t join_items_size() { return 0; }
219+
220+
template <typename A1> inline size_t join_items_size(const A1 &A) {
221+
return join_one_item_size(A);
222+
}
223+
template <typename A1, typename... Args>
224+
inline size_t join_items_size(const A1 &A, Args &&... Items) {
225+
return join_one_item_size(A) + join_items_size(std::forward<Args>(Items)...);
226+
}
227+
}
228+
187229
/// Joins the strings in the range [Begin, End), adding Separator between
188230
/// the elements.
189231
template <typename IteratorT>
190232
inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
191233
typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
192-
return join_impl(Begin, End, Separator, tag());
234+
return detail::join_impl(Begin, End, Separator, tag());
235+
}
236+
237+
/// Joins the strings in the parameter pack \p Items, adding \p Separator
238+
/// between the elements. All arguments must be implicitly convertible to
239+
/// std::string, or there should be an overload of std::string::operator+=()
240+
/// that accepts the argument explicitly.
241+
template <typename Sep, typename... Args>
242+
inline std::string join_items(Sep Separator, Args &&... Items) {
243+
std::string Result;
244+
if (sizeof...(Items) == 0)
245+
return Result;
246+
247+
size_t NS = detail::join_one_item_size(Separator);
248+
size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
249+
Result.reserve(NI + (sizeof...(Items) - 1) * NS + 1);
250+
detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
251+
return Result;
193252
}
194253

195254
} // End llvm namespace

0 commit comments

Comments
 (0)