1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- 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_TINYPTRVECTOR_H
#define LLVM_ADT_TINYPTRVECTOR_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallVector.h"
#include <cassert>
#include <cstddef>
#include <iterator>
#include <type_traits>
namespace llvm {
/// TinyPtrVector - This class is specialized for cases where there are
/// normally 0 or 1 element in a vector, but is general enough to go beyond that
/// when required.
///
/// NOTE: This container doesn't allow you to store a null pointer into it.
///
template <typename EltTy>
class TinyPtrVector {
public:
using VecTy = SmallVector<EltTy, 4>;
using value_type = typename VecTy::value_type;
// EltTy must be the first pointer type so that is<EltTy> is true for the
// default-constructed PtrUnion. This allows an empty TinyPtrVector to
// naturally vend a begin/end iterator of type EltTy* without an additional
// check for the empty state.
using PtrUnion = PointerUnion<EltTy, VecTy *>;
private:
PtrUnion Val;
public:
TinyPtrVector() = default;
~TinyPtrVector() {
if (VecTy *V = dyn_cast_if_present<VecTy *>(Val))
delete V;
}
TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
if (VecTy *V = dyn_cast_if_present<VecTy *>(Val))
Val = new VecTy(*V);
}
TinyPtrVector &operator=(const TinyPtrVector &RHS) {
if (this == &RHS)
return *this;
if (RHS.empty()) {
this->clear();
return *this;
}
// Try to squeeze into the single slot. If it won't fit, allocate a copied
// vector.
if (isa<EltTy>(Val)) {
if (RHS.size() == 1)
Val = RHS.front();
else
Val = new VecTy(*cast<VecTy *>(RHS.Val));
return *this;
}
// If we have a full vector allocated, try to re-use it.
if (isa<EltTy>(RHS.Val)) {
cast<VecTy *>(Val)->clear();
cast<VecTy *>(Val)->push_back(RHS.front());
} else {
*cast<VecTy *>(Val) = *cast<VecTy *>(RHS.Val);
}
return *this;
}
TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
RHS.Val = (EltTy)nullptr;
}
TinyPtrVector &operator=(TinyPtrVector &&RHS) {
if (this == &RHS)
return *this;
if (RHS.empty()) {
this->clear();
return *this;
}
// If this vector has been allocated on the heap, re-use it if cheap. If it
// would require more copying, just delete it and we'll steal the other
// side.
if (VecTy *V = dyn_cast_if_present<VecTy *>(Val)) {
if (isa<EltTy>(RHS.Val)) {
V->clear();
V->push_back(RHS.front());
RHS.Val = EltTy();
return *this;
}
delete V;
}
Val = RHS.Val;
RHS.Val = EltTy();
return *this;
}
TinyPtrVector(std::initializer_list<EltTy> IL)
: Val(IL.size() == 0
? PtrUnion()
: IL.size() == 1 ? PtrUnion(*IL.begin())
: PtrUnion(new VecTy(IL.begin(), IL.end()))) {}
/// Constructor from an ArrayRef.
///
/// This also is a constructor for individual array elements due to the single
/// element constructor for ArrayRef.
explicit TinyPtrVector(ArrayRef<EltTy> Elts)
: Val(Elts.empty()
? PtrUnion()
: Elts.size() == 1
? PtrUnion(Elts[0])
: PtrUnion(new VecTy(Elts.begin(), Elts.end()))) {}
TinyPtrVector(size_t Count, EltTy Value)
: Val(Count == 0 ? PtrUnion()
: Count == 1 ? PtrUnion(Value)
: PtrUnion(new VecTy(Count, Value))) {}
// implicit conversion operator to ArrayRef.
operator ArrayRef<EltTy>() const {
if (Val.isNull())
return std::nullopt;
if (isa<EltTy>(Val))
return *Val.getAddrOfPtr1();
return *cast<VecTy *>(Val);
}
// implicit conversion operator to MutableArrayRef.
operator MutableArrayRef<EltTy>() {
if (Val.isNull())
return std::nullopt;
if (isa<EltTy>(Val))
return *Val.getAddrOfPtr1();
return *cast<VecTy *>(Val);
}
// Implicit conversion to ArrayRef<U> if EltTy* implicitly converts to U*.
template <
typename U,
std::enable_if_t<std::is_convertible<ArrayRef<EltTy>, ArrayRef<U>>::value,
bool> = false>
operator ArrayRef<U>() const {
return operator ArrayRef<EltTy>();
}
bool empty() const {
// This vector can be empty if it contains no element, or if it
// contains a pointer to an empty vector.
if (Val.isNull()) return true;
if (VecTy *Vec = dyn_cast_if_present<VecTy *>(Val))
return Vec->empty();
return false;
}
unsigned size() const {
if (empty())
return 0;
if (isa<EltTy>(Val))
return 1;
return cast<VecTy *>(Val)->size();
}
using iterator = EltTy *;
using const_iterator = const EltTy *;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
iterator begin() {
if (isa<EltTy>(Val))
return Val.getAddrOfPtr1();
return cast<VecTy *>(Val)->begin();
}
iterator end() {
if (isa<EltTy>(Val))
return begin() + (Val.isNull() ? 0 : 1);
return cast<VecTy *>(Val)->end();
}
const_iterator begin() const {
return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
}
const_iterator end() const {
return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
}
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
EltTy operator[](unsigned i) const {
assert(!Val.isNull() && "can't index into an empty vector");
if (isa<EltTy>(Val)) {
assert(i == 0 && "tinyvector index out of range");
return cast<EltTy>(Val);
}
assert(i < cast<VecTy *>(Val)->size() && "tinyvector index out of range");
return (*cast<VecTy *>(Val))[i];
}
EltTy front() const {
assert(!empty() && "vector empty");
if (isa<EltTy>(Val))
return cast<EltTy>(Val);
return cast<VecTy *>(Val)->front();
}
EltTy back() const {
assert(!empty() && "vector empty");
if (isa<EltTy>(Val))
return cast<EltTy>(Val);
return cast<VecTy *>(Val)->back();
}
void push_back(EltTy NewVal) {
// If we have nothing, add something.
if (Val.isNull()) {
Val = NewVal;
assert(!Val.isNull() && "Can't add a null value");
return;
}
// If we have a single value, convert to a vector.
if (isa<EltTy>(Val)) {
EltTy V = cast<EltTy>(Val);
Val = new VecTy();
cast<VecTy *>(Val)->push_back(V);
}
// Add the new value, we know we have a vector.
cast<VecTy *>(Val)->push_back(NewVal);
}
void pop_back() {
// If we have a single value, convert to empty.
if (isa<EltTy>(Val))
Val = (EltTy)nullptr;
else if (VecTy *Vec = cast<VecTy *>(Val))
Vec->pop_back();
}
void clear() {
// If we have a single value, convert to empty.
if (isa<EltTy>(Val)) {
Val = EltTy();
} else if (VecTy *Vec = dyn_cast_if_present<VecTy *>(Val)) {
// If we have a vector form, just clear it.
Vec->clear();
}
// Otherwise, we're already empty.
}
iterator erase(iterator I) {
assert(I >= begin() && "Iterator to erase is out of bounds.");
assert(I < end() && "Erasing at past-the-end iterator.");
// If we have a single value, convert to empty.
if (isa<EltTy>(Val)) {
if (I == begin())
Val = EltTy();
} else if (VecTy *Vec = dyn_cast_if_present<VecTy *>(Val)) {
// multiple items in a vector; just do the erase, there is no
// benefit to collapsing back to a pointer
return Vec->erase(I);
}
return end();
}
iterator erase(iterator S, iterator E) {
assert(S >= begin() && "Range to erase is out of bounds.");
assert(S <= E && "Trying to erase invalid range.");
assert(E <= end() && "Trying to erase past the end.");
if (isa<EltTy>(Val)) {
if (S == begin() && S != E)
Val = EltTy();
} else if (VecTy *Vec = dyn_cast_if_present<VecTy *>(Val)) {
return Vec->erase(S, E);
}
return end();
}
iterator insert(iterator I, const EltTy &Elt) {
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
assert(I <= this->end() && "Inserting past the end of the vector.");
if (I == end()) {
push_back(Elt);
return std::prev(end());
}
assert(!Val.isNull() && "Null value with non-end insert iterator.");
if (isa<EltTy>(Val)) {
EltTy V = cast<EltTy>(Val);
assert(I == begin());
Val = Elt;
push_back(V);
return begin();
}
return cast<VecTy *>(Val)->insert(I, Elt);
}
template<typename ItTy>
iterator insert(iterator I, ItTy From, ItTy To) {
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
assert(I <= this->end() && "Inserting past the end of the vector.");
if (From == To)
return I;
// If we have a single value, convert to a vector.
ptrdiff_t Offset = I - begin();
if (Val.isNull()) {
if (std::next(From) == To) {
Val = *From;
return begin();
}
Val = new VecTy();
} else if (isa<EltTy>(Val)) {
EltTy V = cast<EltTy>(Val);
Val = new VecTy();
cast<VecTy *>(Val)->push_back(V);
}
return cast<VecTy *>(Val)->insert(begin() + Offset, From, To);
}
};
} // end namespace llvm
#endif // LLVM_ADT_TINYPTRVECTOR_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|