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.gli...@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.gli...@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.gli...@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.gli...@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. -- Marc Glisse
Index: cp/decl2.c =================================================================== --- cp/decl2.c (revision 186523) +++ cp/decl2.c (working copy) @@ -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); Index: cp/typeck.c =================================================================== --- cp/typeck.c (revision 186523) +++ cp/typeck.c (working copy) @@ -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; Index: c-family/c-common.c =================================================================== --- c-family/c-common.c (revision 186523) +++ c-family/c-common.c (working copy) @@ -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" Index: c-family/c-common.h =================================================================== --- c-family/c-common.h (revision 186523) +++ c-family/c-common.h (working copy) @@ -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 */ Index: testsuite/c-c++-common/vector-3.c =================================================================== --- testsuite/c-c++-common/vector-3.c (revision 186523) +++ testsuite/c-c++-common/vector-3.c (working copy) @@ -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 "" } */ Index: testsuite/c-c++-common/vector-subscript-1.c =================================================================== --- testsuite/c-c++-common/vector-subscript-1.c (revision 186523) +++ testsuite/c-c++-common/vector-subscript-1.c (working copy) @@ -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" } */ } Index: doc/extend.texi =================================================================== --- doc/extend.texi (revision 186523) +++ doc/extend.texi (working copy) @@ -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 Index: c-typeck.c =================================================================== --- c-typeck.c (revision 186523) +++ c-typeck.c (working copy) @@ -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;