Mercurial > hg > CbC > CbC_gcc
diff gcc/vec.h @ 131:84e7813d76e9
gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 07:37:49 +0900 |
parents | 04ced10e8804 |
children | 1830386684a0 |
line wrap: on
line diff
--- a/gcc/vec.h Fri Oct 27 22:46:09 2017 +0900 +++ b/gcc/vec.h Thu Oct 25 07:37:49 2018 +0900 @@ -1,5 +1,5 @@ /* Vector API for GNU compiler. - Copyright (C) 2004-2017 Free Software Foundation, Inc. + Copyright (C) 2004-2018 Free Software Foundation, Inc. Contributed by Nathan Sidwell <nathan@codesourcery.com> Re-implemented in C++ by Diego Novillo <dnovillo@google.com> @@ -407,12 +407,104 @@ { }; +/* Generic vec<> debug helpers. + + These need to be instantiated for each vec<TYPE> used throughout + the compiler like this: + + DEFINE_DEBUG_VEC (TYPE) + + The reason we have a debug_helper() is because GDB can't + disambiguate a plain call to debug(some_vec), and it must be called + like debug<TYPE>(some_vec). */ + +template<typename T> +void +debug_helper (vec<T> &ref) +{ + unsigned i; + for (i = 0; i < ref.length (); ++i) + { + fprintf (stderr, "[%d] = ", i); + debug_slim (ref[i]); + fputc ('\n', stderr); + } +} + +/* We need a separate va_gc variant here because default template + argument for functions cannot be used in c++-98. Once this + restriction is removed, those variant should be folded with the + above debug_helper. */ + +template<typename T> +void +debug_helper (vec<T, va_gc> &ref) +{ + unsigned i; + for (i = 0; i < ref.length (); ++i) + { + fprintf (stderr, "[%d] = ", i); + debug_slim (ref[i]); + fputc ('\n', stderr); + } +} + +/* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper + functions for a type T. */ + +#define DEFINE_DEBUG_VEC(T) \ + template void debug_helper (vec<T> &); \ + template void debug_helper (vec<T, va_gc> &); \ + /* Define the vec<T> debug functions. */ \ + DEBUG_FUNCTION void \ + debug (vec<T> &ref) \ + { \ + debug_helper <T> (ref); \ + } \ + DEBUG_FUNCTION void \ + debug (vec<T> *ptr) \ + { \ + if (ptr) \ + debug (*ptr); \ + else \ + fprintf (stderr, "<nil>\n"); \ + } \ + /* Define the vec<T, va_gc> debug functions. */ \ + DEBUG_FUNCTION void \ + debug (vec<T, va_gc> &ref) \ + { \ + debug_helper <T> (ref); \ + } \ + DEBUG_FUNCTION void \ + debug (vec<T, va_gc> *ptr) \ + { \ + if (ptr) \ + debug (*ptr); \ + else \ + fprintf (stderr, "<nil>\n"); \ + } + /* Default-construct N elements in DST. */ template <typename T> inline void vec_default_construct (T *dst, unsigned n) { +#ifdef BROKEN_VALUE_INITIALIZATION + /* Versions of GCC before 4.4 sometimes leave certain objects + uninitialized when value initialized, though if the type has + user defined default ctor, that ctor is invoked. As a workaround + perform clearing first and then the value initialization, which + fixes the case when value initialization doesn't initialize due to + the bugs and should initialize to all zeros, but still allows + vectors for types with user defined default ctor that initializes + some or all elements to non-zero. If T has no user defined + default ctor and some non-static data members have user defined + default ctors that initialize to non-zero the workaround will + still not work properly; in that case we just need to provide + user defined default ctor. */ + memset (dst, '\0', sizeof (T) * n); +#endif for ( ; n; ++dst, --n) ::new (static_cast<void*>(dst)) T (); } @@ -936,6 +1028,40 @@ } +/* Remove elements in [START, END) from VEC for which COND holds. Ordering of + remaining elements is preserved. This is an O(N) operation. */ + +#define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \ + elem_ptr, start, end, cond) \ + { \ + gcc_assert ((end) <= (vec).length ()); \ + for (read_index = write_index = (start); read_index < (end); \ + ++read_index) \ + { \ + elem_ptr = &(vec)[read_index]; \ + bool remove_p = (cond); \ + if (remove_p) \ + continue; \ + \ + if (read_index != write_index) \ + (vec)[write_index] = (vec)[read_index]; \ + \ + write_index++; \ + } \ + \ + if (read_index - write_index > 0) \ + (vec).block_remove (write_index, read_index - write_index); \ + } + + +/* Remove elements from VEC for which COND holds. Ordering of remaining + elements is preserved. This is an O(N) operation. */ + +#define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \ + cond) \ + VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \ + elem_ptr, 0, (vec).length (), (cond)) + /* Remove an element from the IXth position of this vector. Ordering of remaining elements is destroyed. This is an O(1) operation. */ @@ -1263,6 +1389,7 @@ T *bsearch (const void *key, int (*compar)(const void *, const void *)); unsigned lower_bound (T, bool (*)(const T &, const T &)) const; bool contains (const T &search) const; + void reverse (void); bool using_auto_storage () const; @@ -1335,6 +1462,15 @@ } +/* A subclass of auto_vec <char *> that frees all of its elements on + deletion. */ + +class auto_string_vec : public auto_vec <char *> +{ + public: + ~auto_string_vec (); +}; + /* Conditionally allocate heap memory for VEC and its internal vector. */ template<typename T> @@ -1427,6 +1563,18 @@ vec_safe_iterate ((V), (I), &(P)); \ (I)--) +/* auto_string_vec's dtor, freeing all contained strings, automatically + chaining up to ~auto_vec <char *>, which frees the internal buffer. */ + +inline +auto_string_vec::~auto_string_vec () +{ + int i; + char *str; + FOR_EACH_VEC_ELT (*this, i, str) + free (str); +} + /* Return a copy of this vector. */ @@ -1774,6 +1922,19 @@ return m_vec ? m_vec->contains (search) : false; } +/* Reverse content of the vector. */ + +template<typename T> +inline void +vec<T, va_heap, vl_ptr>::reverse (void) +{ + unsigned l = length (); + T *ptr = address (); + + for (unsigned i = 0; i < l / 2; i++) + std::swap (ptr[i], ptr[l - i - 1]); +} + template<typename T> inline bool vec<T, va_heap, vl_ptr>::using_auto_storage () const