re PR c++/51033 (generic vector subscript and shuffle support was not added to C++)
authorMarc Glisse <marc.glisse@inria.fr>
Mon, 30 Apr 2012 17:23:28 +0000 (19:23 +0200)
committerJason Merrill <jason@gcc.gnu.org>
Mon, 30 Apr 2012 17:23:28 +0000 (13:23 -0400)
PR c++/51033
gcc/
* c-typeck.c (build_array_ref): Call
convert_vector_to_pointer_for_subscript.
gcc/c-family
* c-common.c (convert_vector_to_pointer_for_subscript): New function.
* c-common.h (convert_vector_to_pointer_for_subscript): Declare it.
gcc/cp/
* typeck.c (cp_build_array_ref): Handle VECTOR_TYPE.
* decl2.c (grok_array_decl): Likewise.

From-SVN: r186994

29 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c-family/c-common.h
gcc/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/decl2.c
gcc/cp/typeck.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/vector-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-3.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-4.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-init-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-init-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-subscript-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-subscript-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/vector-subscript-3.c [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/vt-51314.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/vector-1.c [deleted file]
gcc/testsuite/gcc.dg/vector-2.c [deleted file]
gcc/testsuite/gcc.dg/vector-3.c [deleted file]
gcc/testsuite/gcc.dg/vector-4.c [deleted file]
gcc/testsuite/gcc.dg/vector-init-1.c [deleted file]
gcc/testsuite/gcc.dg/vector-init-2.c [deleted file]
gcc/testsuite/gcc.dg/vector-subscript-1.c [deleted file]
gcc/testsuite/gcc.dg/vector-subscript-2.c [deleted file]
gcc/testsuite/gcc.dg/vector-subscript-3.c [deleted file]

index 5e7dadfbfc4c2e6297d44b729cd7632ca5b4782d..461f16208e67d03da99e612361ac07d50319ad74 100644 (file)
@@ -1,3 +1,10 @@
+2012-04-30  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.
+
 2012-04-30  Uros Bizjak  <ubizjak@gmail.com>
 
        * config/i386/i386.md (and<mode>3): Change runtime operand mode checks
index 9f62db21b6d333a8e04358a852c6fb28622579d1..7bd6ad7afa649219eed024d64328302d09fb69e5 100644 (file)
@@ -1,3 +1,9 @@
+2012-04-30  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.
+
 2012-04-30  Dodji Seketeli  <dodji@redhat.com>
 
        Add -Wvarargs option
index 089c7576ddfa4cff70d01eb37336352233d5bc46..dce390260cda7085673f878ea4a9d0f32f35bfc4 100644 (file)
@@ -10833,4 +10833,30 @@ build_userdef_literal (tree suffix_id, tree value, tree 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 dd411032272e2c3ff54444ee0548cc5a21c9e497..c3d679adcbcffede62c80fb38981f243e788384e 100644 (file)
@@ -1119,4 +1119,6 @@ struct GTY(()) tree_userdef_literal {
 
 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 5e18a98d942115051c6499c0a16ae227bc9c1edf..f45d1dcc342996fbf55f7bc35fc60a679b53609d 100644 (file)
@@ -2340,26 +2340,7 @@ build_array_ref (location_t loc, tree array, tree 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)
     {
index 282912a6a6ba7bf2725bb441f9f93d2826308367..e52149f023d45df9e3df55122aeadfceaae885ad 100644 (file)
@@ -1,5 +1,9 @@
 2012-04-30  Marc Glisse  <marc.glisse@inria.fr>
 
+       PR c++/51033
+       * typeck.c (cp_build_array_ref): Handle VECTOR_TYPE.
+       * decl2.c (grok_array_decl): Likewise.
+
        PR c++/51314
        * parser.c (cp_parser_sizeof_operand): Require parentheses for
        sizeof...
index 34c969c31fcce83ec5afb9bb66600813a82ee85a..7088c675914f11b44f46ccdade1be821befd7ebc 100644 (file)
@@ -373,7 +373,7 @@ grok_array_decl (tree array_expr, tree index_exp)
         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);
index 4f1e965c65ae2d8c382c2cc9f942689677043c8a..b59741c6471023a4b31b7aa7980a245e0fd02bb6 100644 (file)
@@ -2915,6 +2915,8 @@ cp_build_array_ref (location_t loc, tree array, tree idx,
       break;
     }
 
+  convert_vector_to_pointer_for_subscript (loc, &array, idx);
+
   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
     {
       tree rval, type;
index 473339ec736c15daa57b3a5a88d972af874d96c5..95cea834407d7d2f61986c91f66727e2786fe6e7 100644 (file)
@@ -6823,7 +6823,7 @@ 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
index 6613970d23c185efc1d6eb536290b7cd13660f30..0a22ae4f05f9754e3d41d39bc9b281b6da11fa05 100644 (file)
@@ -1,5 +1,25 @@
 2012-04-30  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.
+
        PR c++/51314
        * g++.dg/cpp0x/vt-51314.C: New test.
        * g++.dg/cpp0x/variadic76.C: Fix.
diff --git a/gcc/testsuite/c-c++-common/vector-1.c b/gcc/testsuite/c-c++-common/vector-1.c
new file mode 100644 (file)
index 0000000..288dd1e
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+/* Check for application of ~ on vector types.  */
+
+#define vector __attribute__((vector_size(16) ))
+
+vector float a;
+vector int a1;
+
+int f(void)
+{
+ a =  ~a; /* { dg-error "" } */
+ a1 =  ~a1;
+}
diff --git a/gcc/testsuite/c-c++-common/vector-2.c b/gcc/testsuite/c-c++-common/vector-2.c
new file mode 100644 (file)
index 0000000..e9f40a3
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+/* Check for application of |, ^, and & on vector types.  */
+#define vector __attribute__((vector_size(16) ))
+
+vector float a;
+vector int a1;
+vector float b;
+vector int b1;
+
+int f(void)
+{
+ a =  a | b; /* { dg-error "" } */
+ a =  a & b; /* { dg-error "" } */
+ a =  a ^ b; /* { dg-error "" } */
+ a1 =  a1 | b1;
+ a1 =  a1 & b1;
+ a1 =  a1 ^ b1;
+}
diff --git a/gcc/testsuite/c-c++-common/vector-3.c b/gcc/testsuite/c-c++-common/vector-3.c
new file mode 100644 (file)
index 0000000..0f5d3c4
--- /dev/null
@@ -0,0 +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 "" } */
diff --git a/gcc/testsuite/c-c++-common/vector-4.c b/gcc/testsuite/c-c++-common/vector-4.c
new file mode 100644 (file)
index 0000000..cc4d504
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fno-common" { target { { hppa*-*-hpux* } && { ! lp64 } } } } */
+#define vector __attribute__((vector_size(4*sizeof(int)) ))
+
+vector int a, b, c;
+
+
+/* Test that remainder works for vectors. */
+void f(void)
+{
+  a = b % c;
+}
diff --git a/gcc/testsuite/c-c++-common/vector-init-1.c b/gcc/testsuite/c-c++-common/vector-init-1.c
new file mode 100644 (file)
index 0000000..5baf956
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+
+/* PR C/31499, test that the C front-end treats vectors like an array. */
+
+#define vector __attribute__((__vector_size__(4*sizeof(int)) ))
+vector signed int v1[]={0,1,2,3,4,5,6,7};
diff --git a/gcc/testsuite/c-c++-common/vector-init-2.c b/gcc/testsuite/c-c++-common/vector-init-2.c
new file mode 100644 (file)
index 0000000..6527f49
--- /dev/null
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+
+/* PR C/31499, test that the C front-end treats vectors like an array
+   and that it works at runtime. */
+
+#define vector __attribute__((__vector_size__(4*sizeof(int)) ))
+vector signed int v1[]={0,1,2,3,4,5,6,7};
+
+
+int main(void)
+{
+  int i;
+  for (i = 0; i < sizeof(v1)/sizeof(v1[0]); i++)
+  {
+    vector int t = v1[i];
+    int *d = (int*)&t;
+    int j;
+    for (j = 0; j < 4; j++)
+      {
+        if (d[j] != i * 4 + j)
+         __builtin_abort ();
+      }
+  }
+  return 0;
+}
\ No newline at end of file
diff --git a/gcc/testsuite/c-c++-common/vector-subscript-1.c b/gcc/testsuite/c-c++-common/vector-subscript-1.c
new file mode 100644 (file)
index 0000000..c18b7b6
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-w" } */
+
+#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|invalid types .* for array subscript" } */
+}
+
+
+float fv(vector float a)
+{
+  return a[0];
+}
diff --git a/gcc/testsuite/c-c++-common/vector-subscript-2.c b/gcc/testsuite/c-c++-common/vector-subscript-2.c
new file mode 100644 (file)
index 0000000..84d55b9
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+
+/* Check that subscripting of vectors work with register storage class decls.  */
+
+#define vector __attribute__((vector_size(16) ))
+
+
+float vf(int i)
+{
+  register vector float a;
+  return a[0];
+}
diff --git a/gcc/testsuite/c-c++-common/vector-subscript-3.c b/gcc/testsuite/c-c++-common/vector-subscript-3.c
new file mode 100644 (file)
index 0000000..22cd089
--- /dev/null
@@ -0,0 +1,18 @@
+/* Check the case when index is out of bound */
+/* { dg-do compile } */
+/* { dg-options "-Warray-bounds" } */
+
+#define vector __attribute__((vector_size(16) ))
+
+
+int test0(void)
+{
+  vector int a;
+  return a[10]; /* { dg-warning "index value is out of bound" } */
+}
+
+int test1(void)
+{
+  vector int a;
+  return a[-1]; /* { dg-warning "index value is out of bound" } */
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/vt-51314.C b/gcc/testsuite/g++.dg/cpp0x/vt-51314.C
new file mode 100644 (file)
index 0000000..9f8c646
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-options "-std=c++11 -pedantic-errors" }
+// { dg-prune-output "invalid" }
+
+template<int>struct A{};
+template<class...U>void f(U...){
+    A<sizeof...U> x; // { dg-error "surrounded by parentheses" }
+}
+
+
+template<int...> struct Indices;
+template<class> struct Next_increasing_indices;
+template<int...I> struct Next_increasing_indices<Indices<I...> > {
+    typedef Indices<I...,sizeof...I> type; // { dg-error "surrounded by parentheses" }
+};
diff --git a/gcc/testsuite/gcc.dg/vector-1.c b/gcc/testsuite/gcc.dg/vector-1.c
deleted file mode 100644 (file)
index 288dd1e..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "" } */
-
-/* Check for application of ~ on vector types.  */
-
-#define vector __attribute__((vector_size(16) ))
-
-vector float a;
-vector int a1;
-
-int f(void)
-{
- a =  ~a; /* { dg-error "" } */
- a1 =  ~a1;
-}
diff --git a/gcc/testsuite/gcc.dg/vector-2.c b/gcc/testsuite/gcc.dg/vector-2.c
deleted file mode 100644 (file)
index 5f9f956..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "" } */
-
-/* Check for application of |, ^, and & on vector types.  */
-#define vector __attribute__((vector_size(16) ))
-
-vector float a;
-vector int a1;
-vector float b;
-vector int b1;
-
-int f(void)
-{
- a =  a | b; /* { dg-error "" } */
- a =  a & b; /* { dg-error "" } */
- a =  a ^ b; /* { dg-error "" } */
- a1 =  a1 | b1;
- a1 =  a1 & b1;
- a1 =  a1 ^ b1;
-}
-
diff --git a/gcc/testsuite/gcc.dg/vector-3.c b/gcc/testsuite/gcc.dg/vector-3.c
deleted file mode 100644 (file)
index 3f86698..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-/* { dg-do compile } */
-
-/* Check that we error out when using vector_size on the bool type. */
-
-__attribute__((vector_size(16) )) _Bool a; /* { dg-error "" } */
diff --git a/gcc/testsuite/gcc.dg/vector-4.c b/gcc/testsuite/gcc.dg/vector-4.c
deleted file mode 100644 (file)
index cc4d504..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "-fno-common" { target { { hppa*-*-hpux* } && { ! lp64 } } } } */
-#define vector __attribute__((vector_size(4*sizeof(int)) ))
-
-vector int a, b, c;
-
-
-/* Test that remainder works for vectors. */
-void f(void)
-{
-  a = b % c;
-}
diff --git a/gcc/testsuite/gcc.dg/vector-init-1.c b/gcc/testsuite/gcc.dg/vector-init-1.c
deleted file mode 100644 (file)
index 5baf956..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-/* { dg-do compile } */
-
-/* PR C/31499, test that the C front-end treats vectors like an array. */
-
-#define vector __attribute__((__vector_size__(4*sizeof(int)) ))
-vector signed int v1[]={0,1,2,3,4,5,6,7};
diff --git a/gcc/testsuite/gcc.dg/vector-init-2.c b/gcc/testsuite/gcc.dg/vector-init-2.c
deleted file mode 100644 (file)
index 6527f49..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/* { dg-do run } */
-
-/* PR C/31499, test that the C front-end treats vectors like an array
-   and that it works at runtime. */
-
-#define vector __attribute__((__vector_size__(4*sizeof(int)) ))
-vector signed int v1[]={0,1,2,3,4,5,6,7};
-
-
-int main(void)
-{
-  int i;
-  for (i = 0; i < sizeof(v1)/sizeof(v1[0]); i++)
-  {
-    vector int t = v1[i];
-    int *d = (int*)&t;
-    int j;
-    for (j = 0; j < 4; j++)
-      {
-        if (d[j] != i * 4 + j)
-         __builtin_abort ();
-      }
-  }
-  return 0;
-}
\ No newline at end of file
diff --git a/gcc/testsuite/gcc.dg/vector-subscript-1.c b/gcc/testsuite/gcc.dg/vector-subscript-1.c
deleted file mode 100644 (file)
index 7cc50af..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "-w" } */
-
-#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" } */
-}
-
-
-float fv(vector float a)
-{
-  return a[0];
-}
-
diff --git a/gcc/testsuite/gcc.dg/vector-subscript-2.c b/gcc/testsuite/gcc.dg/vector-subscript-2.c
deleted file mode 100644 (file)
index 3a8d522..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-/* { dg-do compile } */
-
-/* Check that subscripting of vectors work with register storage class decls.  */
-
-#define vector __attribute__((vector_size(16) ))
-
-
-float vf(int i)
-{
-  register vector float a;
-  return a[0];
-}
-
diff --git a/gcc/testsuite/gcc.dg/vector-subscript-3.c b/gcc/testsuite/gcc.dg/vector-subscript-3.c
deleted file mode 100644 (file)
index 55ed2b3..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/* Check the case when index is out of bound */
-/* { dg-do compile } */
-/* { dg-options "-Warray-bounds" } */
-
-#define vector __attribute__((vector_size(16) ))
-
-
-int test0(void)
-{
-  vector int a;
-  return a[10]; /* { dg-warning "index value is out of bound" } */
-}
-
-int test1(void)
-{
-  vector int a;
-  return a[-1]; /* { dg-warning "index value is out of bound" } */
-}
-