comparison gcc/vec.h @ 132:d34655255c78

update gcc-8.2
author mir3636
date Thu, 25 Oct 2018 10:21:07 +0900
parents 84e7813d76e9
children 1830386684a0
comparison
equal deleted inserted replaced
130:e108057fa461 132:d34655255c78
1 /* Vector API for GNU compiler. 1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2017 Free Software Foundation, Inc. 2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com> 3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com> 4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
405 typename L = typename A::default_layout> 405 typename L = typename A::default_layout>
406 struct GTY((user)) vec 406 struct GTY((user)) vec
407 { 407 {
408 }; 408 };
409 409
410 /* Generic vec<> debug helpers.
411
412 These need to be instantiated for each vec<TYPE> used throughout
413 the compiler like this:
414
415 DEFINE_DEBUG_VEC (TYPE)
416
417 The reason we have a debug_helper() is because GDB can't
418 disambiguate a plain call to debug(some_vec), and it must be called
419 like debug<TYPE>(some_vec). */
420
421 template<typename T>
422 void
423 debug_helper (vec<T> &ref)
424 {
425 unsigned i;
426 for (i = 0; i < ref.length (); ++i)
427 {
428 fprintf (stderr, "[%d] = ", i);
429 debug_slim (ref[i]);
430 fputc ('\n', stderr);
431 }
432 }
433
434 /* We need a separate va_gc variant here because default template
435 argument for functions cannot be used in c++-98. Once this
436 restriction is removed, those variant should be folded with the
437 above debug_helper. */
438
439 template<typename T>
440 void
441 debug_helper (vec<T, va_gc> &ref)
442 {
443 unsigned i;
444 for (i = 0; i < ref.length (); ++i)
445 {
446 fprintf (stderr, "[%d] = ", i);
447 debug_slim (ref[i]);
448 fputc ('\n', stderr);
449 }
450 }
451
452 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
453 functions for a type T. */
454
455 #define DEFINE_DEBUG_VEC(T) \
456 template void debug_helper (vec<T> &); \
457 template void debug_helper (vec<T, va_gc> &); \
458 /* Define the vec<T> debug functions. */ \
459 DEBUG_FUNCTION void \
460 debug (vec<T> &ref) \
461 { \
462 debug_helper <T> (ref); \
463 } \
464 DEBUG_FUNCTION void \
465 debug (vec<T> *ptr) \
466 { \
467 if (ptr) \
468 debug (*ptr); \
469 else \
470 fprintf (stderr, "<nil>\n"); \
471 } \
472 /* Define the vec<T, va_gc> debug functions. */ \
473 DEBUG_FUNCTION void \
474 debug (vec<T, va_gc> &ref) \
475 { \
476 debug_helper <T> (ref); \
477 } \
478 DEBUG_FUNCTION void \
479 debug (vec<T, va_gc> *ptr) \
480 { \
481 if (ptr) \
482 debug (*ptr); \
483 else \
484 fprintf (stderr, "<nil>\n"); \
485 }
486
410 /* Default-construct N elements in DST. */ 487 /* Default-construct N elements in DST. */
411 488
412 template <typename T> 489 template <typename T>
413 inline void 490 inline void
414 vec_default_construct (T *dst, unsigned n) 491 vec_default_construct (T *dst, unsigned n)
415 { 492 {
493 #ifdef BROKEN_VALUE_INITIALIZATION
494 /* Versions of GCC before 4.4 sometimes leave certain objects
495 uninitialized when value initialized, though if the type has
496 user defined default ctor, that ctor is invoked. As a workaround
497 perform clearing first and then the value initialization, which
498 fixes the case when value initialization doesn't initialize due to
499 the bugs and should initialize to all zeros, but still allows
500 vectors for types with user defined default ctor that initializes
501 some or all elements to non-zero. If T has no user defined
502 default ctor and some non-static data members have user defined
503 default ctors that initialize to non-zero the workaround will
504 still not work properly; in that case we just need to provide
505 user defined default ctor. */
506 memset (dst, '\0', sizeof (T) * n);
507 #endif
416 for ( ; n; ++dst, --n) 508 for ( ; n; ++dst, --n)
417 ::new (static_cast<void*>(dst)) T (); 509 ::new (static_cast<void*>(dst)) T ();
418 } 510 }
419 511
420 /* Copy-construct N elements in DST from *SRC. */ 512 /* Copy-construct N elements in DST from *SRC. */
934 T *slot = &m_vecdata[ix]; 1026 T *slot = &m_vecdata[ix];
935 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T)); 1027 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
936 } 1028 }
937 1029
938 1030
1031 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
1032 remaining elements is preserved. This is an O(N) operation. */
1033
1034 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
1035 elem_ptr, start, end, cond) \
1036 { \
1037 gcc_assert ((end) <= (vec).length ()); \
1038 for (read_index = write_index = (start); read_index < (end); \
1039 ++read_index) \
1040 { \
1041 elem_ptr = &(vec)[read_index]; \
1042 bool remove_p = (cond); \
1043 if (remove_p) \
1044 continue; \
1045 \
1046 if (read_index != write_index) \
1047 (vec)[write_index] = (vec)[read_index]; \
1048 \
1049 write_index++; \
1050 } \
1051 \
1052 if (read_index - write_index > 0) \
1053 (vec).block_remove (write_index, read_index - write_index); \
1054 }
1055
1056
1057 /* Remove elements from VEC for which COND holds. Ordering of remaining
1058 elements is preserved. This is an O(N) operation. */
1059
1060 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
1061 cond) \
1062 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
1063 elem_ptr, 0, (vec).length (), (cond))
1064
939 /* Remove an element from the IXth position of this vector. Ordering of 1065 /* Remove an element from the IXth position of this vector. Ordering of
940 remaining elements is destroyed. This is an O(1) operation. */ 1066 remaining elements is destroyed. This is an O(1) operation. */
941 1067
942 template<typename T, typename A> 1068 template<typename T, typename A>
943 inline void 1069 inline void
1261 void block_remove (unsigned, unsigned); 1387 void block_remove (unsigned, unsigned);
1262 void qsort (int (*) (const void *, const void *)); 1388 void qsort (int (*) (const void *, const void *));
1263 T *bsearch (const void *key, int (*compar)(const void *, const void *)); 1389 T *bsearch (const void *key, int (*compar)(const void *, const void *));
1264 unsigned lower_bound (T, bool (*)(const T &, const T &)) const; 1390 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1265 bool contains (const T &search) const; 1391 bool contains (const T &search) const;
1392 void reverse (void);
1266 1393
1267 bool using_auto_storage () const; 1394 bool using_auto_storage () const;
1268 1395
1269 /* FIXME - This field should be private, but we need to cater to 1396 /* FIXME - This field should be private, but we need to cater to
1270 compilers that have stricter notions of PODness for types. */ 1397 compilers that have stricter notions of PODness for types. */
1333 v = new vec<T>; 1460 v = new vec<T>;
1334 v->create (nelems PASS_MEM_STAT); 1461 v->create (nelems PASS_MEM_STAT);
1335 } 1462 }
1336 1463
1337 1464
1465 /* A subclass of auto_vec <char *> that frees all of its elements on
1466 deletion. */
1467
1468 class auto_string_vec : public auto_vec <char *>
1469 {
1470 public:
1471 ~auto_string_vec ();
1472 };
1473
1338 /* Conditionally allocate heap memory for VEC and its internal vector. */ 1474 /* Conditionally allocate heap memory for VEC and its internal vector. */
1339 1475
1340 template<typename T> 1476 template<typename T>
1341 inline void 1477 inline void
1342 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO) 1478 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1424 1560
1425 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \ 1561 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1426 for (I = vec_safe_length (V) - 1; \ 1562 for (I = vec_safe_length (V) - 1; \
1427 vec_safe_iterate ((V), (I), &(P)); \ 1563 vec_safe_iterate ((V), (I), &(P)); \
1428 (I)--) 1564 (I)--)
1565
1566 /* auto_string_vec's dtor, freeing all contained strings, automatically
1567 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
1568
1569 inline
1570 auto_string_vec::~auto_string_vec ()
1571 {
1572 int i;
1573 char *str;
1574 FOR_EACH_VEC_ELT (*this, i, str)
1575 free (str);
1576 }
1429 1577
1430 1578
1431 /* Return a copy of this vector. */ 1579 /* Return a copy of this vector. */
1432 1580
1433 template<typename T> 1581 template<typename T>
1772 vec<T, va_heap, vl_ptr>::contains (const T &search) const 1920 vec<T, va_heap, vl_ptr>::contains (const T &search) const
1773 { 1921 {
1774 return m_vec ? m_vec->contains (search) : false; 1922 return m_vec ? m_vec->contains (search) : false;
1775 } 1923 }
1776 1924
1925 /* Reverse content of the vector. */
1926
1927 template<typename T>
1928 inline void
1929 vec<T, va_heap, vl_ptr>::reverse (void)
1930 {
1931 unsigned l = length ();
1932 T *ptr = address ();
1933
1934 for (unsigned i = 0; i < l / 2; i++)
1935 std::swap (ptr[i], ptr[l - i - 1]);
1936 }
1937
1777 template<typename T> 1938 template<typename T>
1778 inline bool 1939 inline bool
1779 vec<T, va_heap, vl_ptr>::using_auto_storage () const 1940 vec<T, va_heap, vl_ptr>::using_auto_storage () const
1780 { 1941 {
1781 return m_vec->m_vecpfx.m_using_auto_storage; 1942 return m_vec->m_vecpfx.m_using_auto_storage;