===================================================================
@@ -373,7 +373,7 @@
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);
===================================================================
@@ -2902,6 +2902,8 @@
break;
}
+ convert_vector_to_pointer_for_subscript (loc, &array, idx);
+
if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
{
tree rval, type;
===================================================================
@@ -10831,4 +10831,29 @@
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"
===================================================================
@@ -1119,4 +1119,6 @@
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 */
===================================================================
@@ -2,4 +2,7 @@
/* 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 "" } */
===================================================================
@@ -6,7 +6,7 @@
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" } */
}
===================================================================
@@ -6822,7 +6822,7 @@
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
===================================================================
@@ -2340,27 +2340,8 @@
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;
+ convert_vector_to_pointer_for_subscript (loc, &array, index);
- 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);
- }
-
if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
{
tree rval, type;