1 //===- ASTVector.h - Vector that uses ASTContext for allocation --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides ASTVector, a vector ADT whose contents are
11 // allocated using the allocator associated with an ASTContext..
13 //===----------------------------------------------------------------------===//
15 // FIXME: Most of this is copy-and-paste from BumpVector.h and SmallVector.h.
16 // We can refactor this core logic into something common.
18 #ifndef LLVM_CLANG_AST_VECTOR
19 #define LLVM_CLANG_AST_VECTOR
21 #include "llvm/Support/type_traits.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/ADT/PointerIntPair.h"
31 // Work around flawed VC++ implementation of std::uninitialized_copy. Define
32 // additional overloads so that elements with pointer types are recognized as
33 // scalars and not objects, causing bizarre type conversion errors.
34 template<class T1, class T2>
35 inline _Scalar_ptr_iterator_tag _Ptr_cat(T1 **, T2 **) {
36 _Scalar_ptr_iterator_tag _Cat;
40 template<class T1, class T2>
41 inline _Scalar_ptr_iterator_tag _Ptr_cat(T1* const *, T2 **) {
42 _Scalar_ptr_iterator_tag _Cat;
46 // FIXME: It is not clear if the problem is fixed in VS 2005. What is clear
47 // is that the above hack won't work if it wasn't fixed.
56 T *Begin, *End, *Capacity;
58 void setEnd(T *P) { this->End = P; }
61 // Default ctor - Initialize to empty.
62 explicit ASTVector(ASTContext &C, unsigned N = 0)
63 : Begin(NULL), End(NULL), Capacity(NULL) {
68 if (llvm::is_class<T>::value) {
69 // Destroy the constructed elements in the vector.
70 destroy_range(Begin, End);
74 typedef size_t size_type;
75 typedef ptrdiff_t difference_type;
78 typedef const T* const_iterator;
80 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
81 typedef std::reverse_iterator<iterator> reverse_iterator;
84 typedef const T& const_reference;
86 typedef const T* const_pointer;
88 // forward iterator creation methods.
89 iterator begin() { return Begin; }
90 const_iterator begin() const { return Begin; }
91 iterator end() { return End; }
92 const_iterator end() const { return End; }
94 // reverse iterator creation methods.
95 reverse_iterator rbegin() { return reverse_iterator(end()); }
96 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
97 reverse_iterator rend() { return reverse_iterator(begin()); }
98 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
100 bool empty() const { return Begin == End; }
101 size_type size() const { return End-Begin; }
103 reference operator[](unsigned idx) {
104 assert(Begin + idx < End);
107 const_reference operator[](unsigned idx) const {
108 assert(Begin + idx < End);
115 const_reference front() const {
122 const_reference back() const {
138 if (llvm::is_class<T>::value) {
139 destroy_range(Begin, End);
144 /// data - Return a pointer to the vector's buffer, even if empty().
146 return pointer(Begin);
149 /// data - Return a pointer to the vector's buffer, even if empty().
150 const_pointer data() const {
151 return const_pointer(Begin);
154 void push_back(const_reference Elt, ASTContext &C) {
155 if (End < Capacity) {
165 void reserve(ASTContext &C, unsigned N) {
166 if (unsigned(Capacity-Begin) < N)
170 /// capacity - Return the total number of elements in the currently allocated
172 size_t capacity() const { return Capacity - Begin; }
174 /// append - Add the specified range to the end of the SmallVector.
176 template<typename in_iter>
177 void append(ASTContext &C, in_iter in_start, in_iter in_end) {
178 size_type NumInputs = std::distance(in_start, in_end);
183 // Grow allocated space if needed.
184 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
185 this->grow(C, this->size()+NumInputs);
187 // Copy the new elements over.
188 // TODO: NEED To compile time dispatch on whether in_iter is a random access
189 // iterator to use the fast uninitialized_copy.
190 std::uninitialized_copy(in_start, in_end, this->end());
191 this->setEnd(this->end() + NumInputs);
194 /// append - Add the specified range to the end of the SmallVector.
196 void append(ASTContext &C, size_type NumInputs, const T &Elt) {
197 // Grow allocated space if needed.
198 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
199 this->grow(C, this->size()+NumInputs);
201 // Copy the new elements over.
202 std::uninitialized_fill_n(this->end(), NumInputs, Elt);
203 this->setEnd(this->end() + NumInputs);
206 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
207 /// starting with "Dest", constructing elements into it as needed.
208 template<typename It1, typename It2>
209 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
210 std::uninitialized_copy(I, E, Dest);
213 iterator insert(ASTContext &C, iterator I, const T &Elt) {
214 if (I == this->end()) { // Important special case for empty vector.
216 return this->end()-1;
219 if (this->EndX < this->CapacityX) {
221 new (this->end()) T(this->back());
222 this->setEnd(this->end()+1);
223 // Push everything else over.
224 std::copy_backward(I, this->end()-1, this->end());
228 size_t EltNo = I-this->begin();
230 I = this->begin()+EltNo;
234 iterator insert(ASTContext &C, iterator I, size_type NumToInsert,
236 if (I == this->end()) { // Important special case for empty vector.
237 append(C, NumToInsert, Elt);
238 return this->end()-1;
241 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
242 size_t InsertElt = I - this->begin();
244 // Ensure there is enough space.
245 reserve(C, static_cast<unsigned>(this->size() + NumToInsert));
247 // Uninvalidate the iterator.
248 I = this->begin()+InsertElt;
250 // If there are more elements between the insertion point and the end of the
251 // range than there are being inserted, we can use a simple approach to
252 // insertion. Since we already reserved space, we know that this won't
253 // reallocate the vector.
254 if (size_t(this->end()-I) >= NumToInsert) {
255 T *OldEnd = this->end();
256 append(C, this->end()-NumToInsert, this->end());
258 // Copy the existing elements that get replaced.
259 std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
261 std::fill_n(I, NumToInsert, Elt);
265 // Otherwise, we're inserting more elements than exist already, and we're
266 // not inserting at the end.
268 // Copy over the elements that we're about to overwrite.
269 T *OldEnd = this->end();
270 this->setEnd(this->end() + NumToInsert);
271 size_t NumOverwritten = OldEnd-I;
272 this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
274 // Replace the overwritten part.
275 std::fill_n(I, NumOverwritten, Elt);
277 // Insert the non-overwritten middle part.
278 std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
282 template<typename ItTy>
283 iterator insert(ASTContext &C, iterator I, ItTy From, ItTy To) {
284 if (I == this->end()) { // Important special case for empty vector.
286 return this->end()-1;
289 size_t NumToInsert = std::distance(From, To);
290 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
291 size_t InsertElt = I - this->begin();
293 // Ensure there is enough space.
294 reserve(C, static_cast<unsigned>(this->size() + NumToInsert));
296 // Uninvalidate the iterator.
297 I = this->begin()+InsertElt;
299 // If there are more elements between the insertion point and the end of the
300 // range than there are being inserted, we can use a simple approach to
301 // insertion. Since we already reserved space, we know that this won't
302 // reallocate the vector.
303 if (size_t(this->end()-I) >= NumToInsert) {
304 T *OldEnd = this->end();
305 append(C, this->end()-NumToInsert, this->end());
307 // Copy the existing elements that get replaced.
308 std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
310 std::copy(From, To, I);
314 // Otherwise, we're inserting more elements than exist already, and we're
315 // not inserting at the end.
317 // Copy over the elements that we're about to overwrite.
318 T *OldEnd = this->end();
319 this->setEnd(this->end() + NumToInsert);
320 size_t NumOverwritten = OldEnd-I;
321 this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
323 // Replace the overwritten part.
324 for (; NumOverwritten > 0; --NumOverwritten) {
329 // Insert the non-overwritten middle part.
330 this->uninitialized_copy(From, To, OldEnd);
334 void resize(ASTContext &C, unsigned N, const T &NV) {
335 if (N < this->size()) {
336 this->destroy_range(this->begin()+N, this->end());
337 this->setEnd(this->begin()+N);
338 } else if (N > this->size()) {
339 if (this->capacity() < N)
341 construct_range(this->end(), this->begin()+N, NV);
342 this->setEnd(this->begin()+N);
347 /// grow - double the size of the allocated memory, guaranteeing space for at
348 /// least one more element or MinSize if specified.
349 void grow(ASTContext &C, size_type MinSize = 1);
351 void construct_range(T *S, T *E, const T &Elt) {
356 void destroy_range(T *S, T *E) {
364 iterator capacity_ptr() { return (iterator)this->Capacity; }
367 // Define this out-of-line to dissuade the C++ compiler from inlining it.
368 template <typename T>
369 void ASTVector<T>::grow(ASTContext &C, size_t MinSize) {
370 size_t CurCapacity = Capacity-Begin;
371 size_t CurSize = size();
372 size_t NewCapacity = 2*CurCapacity;
373 if (NewCapacity < MinSize)
374 NewCapacity = MinSize;
376 // Allocate the memory from the ASTContext.
377 T *NewElts = new (C) T[NewCapacity];
379 // Copy the elements over.
380 if (llvm::is_class<T>::value) {
381 std::uninitialized_copy(Begin, End, NewElts);
382 // Destroy the original elements.
383 destroy_range(Begin, End);
386 // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
387 memcpy(NewElts, Begin, CurSize * sizeof(T));
392 End = NewElts+CurSize;
393 Capacity = Begin+NewCapacity;
396 } // end: clang namespace