public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-5751] vec: Simplify use with C++11 range-based 'for'.
@ 2020-12-04 19:46 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2020-12-04 19:46 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:df933e307b1950ce12472660dcac1765b8eb431d

commit r11-5751-gdf933e307b1950ce12472660dcac1765b8eb431d
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Dec 2 17:11:33 2020 -0500

    vec: Simplify use with C++11 range-based 'for'.
    
    It looks cleaner if we can use a vec* directly as a range for the C++11
    range-based 'for' loop, without needing to indirect from it, and also works
    with null pointers.
    
    The change in cp_parser_late_parsing_default_args is an example of how this
    can be used to simplify a simple loop over a vector.  Reverse or subset
    iteration will require adding range adaptors.
    
    I deliberately didn't format the new overloads for etags since they are
    trivial.
    
    gcc/ChangeLog:
    
            * vec.h (begin, end): Add overloads for vec*.
            * tree.c (build_constructor_from_vec): Remove *.
    
    gcc/cp/ChangeLog:
    
            * decl2.c (clear_consteval_vfns): Remove *.
            * pt.c (do_auto_deduction): Remove *.
            * parser.c (cp_parser_late_parsing_default_args): Change loop
            to use range 'for'.

Diff:
---
 gcc/cp/decl2.c  |  2 +-
 gcc/cp/parser.c |  6 +-----
 gcc/cp/pt.c     |  2 +-
 gcc/tree.c      |  2 +-
 gcc/vec.h       | 10 ++++++++++
 5 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 1bc7b7e0197..46069cb66a6 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -1928,7 +1928,7 @@ static void
 clear_consteval_vfns (vec<tree> &consteval_vtables)
 {
   for (tree vtable : consteval_vtables)
-    for (constructor_elt &elt : *CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
+    for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
       {
 	tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
 	if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 103567cd004..cc3da155032 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -30611,9 +30611,6 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
     {
       tree default_arg = TREE_PURPOSE (parm);
       tree parsed_arg;
-      vec<tree, va_gc> *insts;
-      tree copy;
-      unsigned ix;
 
       tree parmdecl = parms[i];
       pushdecl (parmdecl);
@@ -30633,8 +30630,7 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
       TREE_PURPOSE (parm) = parsed_arg;
 
       /* Update any instantiations we've already created.  */
-      for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
-	   vec_safe_iterate (insts, ix, &copy); ix++)
+      for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
 	TREE_PURPOSE (copy) = parsed_arg;
     }
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 9e8113d51a3..e991a323de8 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -29280,7 +29280,7 @@ do_auto_deduction (tree type, tree init, tree auto_node,
       /* We don't recurse here because we can't deduce from a nested
 	 initializer_list.  */
       if (CONSTRUCTOR_ELTS (init))
-	for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
+	for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
 	  elt.value = resolve_nondeduced_context (elt.value, complain);
     }
   else
diff --git a/gcc/tree.c b/gcc/tree.c
index 72311005f57..02ce5ddcd49 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -2185,7 +2185,7 @@ build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
 {
   vec<constructor_elt, va_gc> *v = NULL;
 
-  for (tree t : *vals)
+  for (tree t : vals)
     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
 
   return build_constructor (type, v);
diff --git a/gcc/vec.h b/gcc/vec.h
index 90904515ea0..09166f1bce6 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -419,6 +419,16 @@ struct GTY((user)) vec
 {
 };
 
+/* Allow C++11 range-based 'for' to work directly on vec<T>*.  */
+template<typename T, typename A, typename L>
+T* begin (vec<T,A,L> *v) { return v ? v->begin () : nullptr; }
+template<typename T, typename A, typename L>
+T* end (vec<T,A,L> *v) { return v ? v->end () : nullptr; }
+template<typename T, typename A, typename L>
+const T* begin (const vec<T,A,L> *v) { return v ? v->begin () : nullptr; }
+template<typename T, typename A, typename L>
+const T* end (const vec<T,A,L> *v) { return v ? v->end () : nullptr; }
+
 /* Generic vec<> debug helpers.
 
    These need to be instantiated for each vec<TYPE> used throughout


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-04 19:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-04 19:46 [gcc r11-5751] vec: Simplify use with C++11 range-based 'for' Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).