diff mbox

Vector subscripts in C++

Message ID alpine.DEB.2.02.1204301112360.2550@laptop-mg.saclay.inria.fr
State New
Headers show

Commit Message

Marc Glisse April 30, 2012, 9:21 a.m. UTC
Ping?

Also added Richard in Cc: as the author of the C front-end code I am 
sharing with the C++ front-end.

Looks like I forgot to say it here (was in bugzilla), but the patch was 
tested in a c,c++ bootstrap + make -k check on linux x86_64.

Reattaching the patch (now using the recommended diff options).

On Tue, 17 Apr 2012, Marc Glisse wrote:

> Hello,
>
> this patch adds vector subscripting to C++ by reusing the C code. 
> build_array_ref and cp_build_array_ref could probably share more, but I don't 
> understand them enough to do it.
>
> (note that I can't commit, so if you like the patch...)
>
> gcc/cp/ChangeLog
> 2012-04-17  Marc Glisse  <marc.glisse@inria.fr>
>
> 	PR c++/51033
> 	* typeck.c (cp_build_array_ref): Handle VECTOR_TYPE.
> 	* decl2.c (grok_array_decl): Likewise.
>
> gcc/c-family/ChangeLog
> 2012-04-17  Marc Glisse  <marc.glisse@inria.fr>
>
> 	PR c++/51033
> 	* c-common.c (convert_vector_to_pointer_for_subscript): New function.
> 	* c-common.h (convert_vector_to_pointer_for_subscript): Declare it.
>
> gcc/ChangeLog
> 2012-04-17  Marc Glisse  <marc.glisse@inria.fr>
>
> 	PR c++/51033
> 	* c-typeck.c (build_array_ref): Call
> 	convert_vector_to_pointer_for_subscript.
> 	* doc/extend.texi (Vector Extensions): Subscripting not just for C.
>
> gcc/testsuite/ChangeLog
> 2012-04-17  Marc Glisse  <marc.glisse@inria.fr>
>
> 	PR c++/51033
> 	* gcc.dg/vector-1.c: Move to ...
> 	* c-c++-common/vector-1.c: ... here.
> 	* gcc.dg/vector-2.c: Move to ...
> 	* c-c++-common/vector-2.c: ... here.
> 	* gcc.dg/vector-3.c: Move to ...
> 	* c-c++-common/vector-3.c: ... here. Adapt to C++.
> 	* gcc.dg/vector-4.c: Move to ...
> 	* c-c++-common/vector-4.c: ... here.
> 	* gcc.dg/vector-init-1.c: Move to ...
> 	* c-c++-common/vector-init-1.c: ... here.
> 	* gcc.dg/vector-init-2.c: Move to ...
> 	* c-c++-common/vector-init-2.c: ... here.
> 	* gcc.dg/vector-subscript-1.c: Move to ... Adapt to C++.
> 	* c-c++-common/vector-subscript-1.c: ... here.
> 	* gcc.dg/vector-subscript-2.c: Move to ...
> 	* c-c++-common/vector-subscript-2.c: ... here.
> 	* gcc.dg/vector-subscript-3.c: Move to ...
> 	* c-c++-common/vector-subscript-3.c: ... here.
diff mbox

Patch

Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 186961)
+++ c-typeck.c	(working copy)
@@ -2338,30 +2338,11 @@  build_array_ref (location_t loc, tree ar
   /* Apply default promotions *after* noticing character types.  */
   index = default_conversion (index);
 
   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
 
-  /* For vector[index], convert the vector to a
-     pointer of the underlying type.  */
-  if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
-    {
-      tree type = TREE_TYPE (array);
-      tree type1;
-
-      if (TREE_CODE (index) == INTEGER_CST)
-        if (!host_integerp (index, 1)
-            || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
-               >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
-          warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
-
-      c_common_mark_addressable_vec (array);
-      type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
-      type = build_pointer_type (type);
-      type1 = build_pointer_type (TREE_TYPE (array));
-      array = build1 (ADDR_EXPR, type1, array);
-      array = convert (type, array);
-    }
+  convert_vector_to_pointer_for_subscript (loc, &array, index);
 
   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
     {
       tree rval, type;
 
Index: c-family/c-common.c
===================================================================
--- c-family/c-common.c	(revision 186961)
+++ c-family/c-common.c	(working copy)
@@ -10831,6 +10831,31 @@  build_userdef_literal (tree suffix_id, t
   USERDEF_LITERAL_VALUE (literal) = value;
   USERDEF_LITERAL_NUM_STRING (literal) = num_string;
   return literal;
 }
 
+/* For vector[index], convert the vector to a
+   pointer of the underlying type.  */
+void
+convert_vector_to_pointer_for_subscript (location_t loc, tree* vecp, tree index)
+{
+  if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
+    {
+      tree type = TREE_TYPE (*vecp);
+      tree type1;
+
+      if (TREE_CODE (index) == INTEGER_CST)
+        if (!host_integerp (index, 1)
+            || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
+               >= TYPE_VECTOR_SUBPARTS (type)))
+          warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
+
+      c_common_mark_addressable_vec (*vecp);
+      type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
+      type = build_pointer_type (type);
+      type1 = build_pointer_type (TREE_TYPE (*vecp));
+      *vecp = build1 (ADDR_EXPR, type1, *vecp);
+      *vecp = convert (type, *vecp);
+    }
+}
+
 #include "gt-c-family-c-common.h"
Index: c-family/c-common.h
===================================================================
--- c-family/c-common.h	(revision 186961)
+++ c-family/c-common.h	(working copy)
@@ -1117,6 +1117,8 @@  struct GTY(()) tree_userdef_literal {
 #define USERDEF_LITERAL_TYPE(NODE) \
   (TREE_TYPE (USERDEF_LITERAL_VALUE (NODE)))
 
 extern tree build_userdef_literal (tree suffix_id, tree value, tree num_string);
 
+extern void convert_vector_to_pointer_for_subscript (location_t, tree*, tree);
+
 #endif /* ! GCC_C_COMMON_H */
Index: doc/extend.texi
===================================================================
--- doc/extend.texi	(revision 186961)
+++ doc/extend.texi	(working copy)
@@ -6821,11 +6821,11 @@  a = b + 1;    /* a = b + @{1,1,1,1@}; */
 a = 2 * b;    /* a = @{2,2,2,2@} * b; */
 
 a = l + a;    /* Error, cannot convert long to int. */
 @end smallexample
 
-In C vectors can be subscripted as if the vector were an array with
+Vectors can be subscripted as if the vector were an array with
 the same number of elements and base type.  Out of bound accesses
 invoke undefined behavior at runtime.  Warnings for out of bound
 accesses for vector subscription can be enabled with
 @option{-Warray-bounds}.
 
Index: cp/decl2.c
===================================================================
--- cp/decl2.c	(revision 186961)
+++ cp/decl2.c	(working copy)
@@ -371,11 +371,11 @@  grok_array_decl (tree array_expr, tree i
 
       /* Otherwise, create an ARRAY_REF for a pointer or array type.
 	 It is a little-known fact that, if `a' is an array and `i' is
 	 an int, you can write `i[a]', which means the same thing as
 	 `a[i]'.  */
-      if (TREE_CODE (type) == ARRAY_TYPE)
+      if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
 	p1 = array_expr;
       else
 	p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
 
       if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 186961)
+++ cp/typeck.c	(working copy)
@@ -2913,10 +2913,12 @@  cp_build_array_ref (location_t loc, tree
 
     default:
       break;
     }
 
+  convert_vector_to_pointer_for_subscript (loc, &array, idx);
+
   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
     {
       tree rval, type;
 
       warn_array_subscript_with_type_char (idx);
Index: testsuite/c-c++-common/vector-3.c
===================================================================
--- testsuite/c-c++-common/vector-3.c	(revision 186961)
+++ testsuite/c-c++-common/vector-3.c	(working copy)
@@ -1,5 +1,8 @@ 
 /* { dg-do compile } */
 
 /* Check that we error out when using vector_size on the bool type. */
 
+#ifdef __cplusplus
+#define _Bool bool
+#endif
 __attribute__((vector_size(16) )) _Bool a; /* { dg-error "" } */
Index: testsuite/c-c++-common/vector-subscript-1.c
===================================================================
--- testsuite/c-c++-common/vector-subscript-1.c	(revision 186961)
+++ testsuite/c-c++-common/vector-subscript-1.c	(working copy)
@@ -4,11 +4,11 @@ 
 #define vector __attribute__((vector_size(16) ))
 /* Check that vector[index] works and index[vector] is rejected.  */
 
 float vf(vector float a)
 {
-  return 0[a]; /* { dg-error "subscripted value is neither array nor pointer nor vector" } */
+  return 0[a]; /* { dg-error "subscripted value is neither array nor pointer nor vector|invalid types .* for array subscript" } */
 }
 
 
 float fv(vector float a)
 {