* [PATCH] c++: Implement P1009: Array size deduction in new-expressions. @ 2020-08-20 20:22 Marek Polacek 2020-08-21 21:04 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2020-08-20 20:22 UTC (permalink / raw) To: Jason Merrill, GCC Patches This patch implements C++20 P1009, allowing code like new double[]{1,2,3}; // array bound will be deduced Since this proposal makes the initialization rules more consistent, it is applied to all previous versions of C++ (thus, effectively, all the way back to C++11). My patch is based on Jason's patch that handled the basic case. I've extended it to work with ()-init and also the string literal case. Further testing revealed that to handle stuff like new int[]{t...}; in a template, we have to consider such a NEW_EXPR type-dependent. Obviously, we first have to expand the pack to be able to deduce the number of elements in the array. Curiously, while implementing this proposal, I noticed that we fail to accept new char[4]{"abc"}; so I've assigned 77841 to self. I think the fix will depend on the build_new_1 hunk in this patch. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: PR c++/93529 * init.c (build_new_1): Handle new char[]{"foo"}. (build_new): Deduce the array size in new-expression if not present. Handle ()-init. Handle initializing an array from a string literal. * parser.c (cp_parser_new_type_id): Leave [] alone. (cp_parser_direct_new_declarator): Allow []. * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider array types whose dimension has to be deduced type-dependent. gcc/testsuite/ChangeLog: PR c++/93529 * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. * g++.dg/cpp2a/new-array1.C: New test. * g++.dg/cpp2a/new-array2.C: New test. * g++.dg/cpp2a/new-array3.C: New test. Co-authored-by: Jason Merrill <jason@redhat.com> --- gcc/cp/init.c | 54 ++++++++++++++++++- gcc/cp/parser.c | 11 ++-- gcc/cp/pt.c | 4 ++ gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ 7 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 872c23453fd..ae1177079e4 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, else if (array_p) { tree vecinit = NULL_TREE; - if (vec_safe_length (*init) == 1 - && DIRECT_LIST_INIT_P ((**init)[0])) + const size_t len = vec_safe_length (*init); + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) { vecinit = (**init)[0]; if (CONSTRUCTOR_NELTS (vecinit) == 0) @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, vecinit = digest_init (arraytype, vecinit, complain); } } + /* This handles code like new char[]{"foo"}. */ + else if (len == 1 + && char_type_p (TYPE_MAIN_VARIANT (type)) + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) + == STRING_CST) + { + vecinit = (**init)[0]; + STRIP_ANY_LOCATION_WRAPPER (vecinit); + } else if (*init) { if (complain & tf_error) @@ -3917,6 +3926,47 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, return error_mark_node; } + /* P1009: Array size deduction in new-expressions. */ + if (TREE_CODE (type) == ARRAY_TYPE + && !TYPE_DOMAIN (type) + && *init) + { + /* This means we have 'new T[]()'. */ + if ((*init)->is_empty ()) + { + tree ctor = build_constructor (init_list_type_node, NULL); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + vec_safe_push (*init, ctor); + } + tree &elt = (**init)[0]; + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) + { + /* Handle new char[]("foo"). */ + if (vec_safe_length (*init) == 1 + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) + && TREE_CODE (tree_strip_any_location_wrapper (elt)) + == STRING_CST) + /* Leave it alone: the string should not be wrapped in {}. */; + else + { + /* Create a CONSTRUCTOR from the vector INIT. */ + tree list = build_tree_list_vec (*init); + tree ctor = build_constructor_from_list (init_list_type_node, list); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; + elt = ctor; + /* We've squashed all the vector elements into the first one; + truncate the rest. */ + (*init)->truncate (1); + } + } + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) + elt = reshape_init (type, elt, complain); + cp_complete_array_type (&type, elt, /*do_default*/false); + } + /* The type allocated must be complete. If the new-type-id was "T[N]" then we are just checking that "T" is complete here, but that is equivalent, since the value of "N" doesn't matter. */ diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 7cc2dbed5fe..dc1f2ec45ad 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) if (*nelts == error_mark_node) *nelts = integer_one_node; - if (outer_declarator) + if (*nelts == NULL_TREE) + /* Leave [] in the declarator. */; + else if (outer_declarator) outer_declarator->declarator = declarator->declarator; else new_declarator = NULL; @@ -9082,14 +9084,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); token = cp_lexer_peek_token (parser->lexer); - expression = cp_parser_expression (parser); + if (token->type == CPP_CLOSE_SQUARE) + expression = NULL_TREE; + else + expression = cp_parser_expression (parser); /* The standard requires that the expression have integral type. DR 74 adds enumeration types. We believe that the real intent is that these expressions be handled like the expression in a `switch' condition, which also allows classes with a single conversion to integral or enumeration type. */ - if (!processing_template_decl) + if (expression && !processing_template_decl) { expression = build_expr_type_conversion (WANT_INT | WANT_ENUM, diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 5dbdd37f6e3..fe318b84385 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) || value_dependent_expression_p (TREE_OPERAND (TREE_VALUE (type), 1)); + /* Array type whose dimension has to be deduced. */ + else if (TREE_CODE (type) == ARRAY_TYPE + && TREE_OPERAND (expression, 2) == NULL_TREE) + return true; else return dependent_type_p (type); } diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C index 1b24966e051..f271cf1df6a 100644 --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; }; -static_assert( !is_constructible_mini<int[], int>::value, ""); +// int[](...) will work with P0960 and P1009. +#if __cpp_aggregate_paren_init +constexpr bool r = true; +#else +constexpr bool r = false; +#endif +static_assert( is_constructible_mini<int[], int>::value == r, ""); static_assert( !is_constructible_mini<void, int>::value, ""); diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C new file mode 100644 index 00000000000..2353c384359 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C @@ -0,0 +1,70 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do run { target c++11 } } + +// When the array bound is deduced to 0, malloc(0) returns +// a non-dereferenceable pointer. +int *p0 = new int[]{}; +int *p1 = new int[]{ 1 }; +int *p2 = new int[]{ 1, 2, 3 }; +char *c1 = new char[]{"foo"}; +#if __cpp_aggregate_paren_init +int *q0 = new int[](); +int *q1 = new int[](1); +int *q2 = new int[](1, 2, 3); +char *d1 = new char[]("foo"); +char *d2 = new char[4]("foo"); +char *d3 = new char[]((("foo"))); +#endif + +struct Aggr { int a; int b; int c; }; +Aggr *a1 = new Aggr[]{}; +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; +#if __cpp_designated_initializers +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; +#endif +#if __cpp_aggregate_paren_init +Aggr *a6 = new Aggr[](); +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); +#endif + +int +main () +{ + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (c1, "foo")) + __builtin_abort (); + if (a2->a != 1 || a2->b != 2 || a2->c != 3) + __builtin_abort (); + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) + __builtin_abort (); + if (a4->a != 1 || a4->b != 2 || a4->c != 3) + __builtin_abort (); + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) + __builtin_abort (); +#if __cpp_designated_initializers + if (a9->a != 1 || a9->b != 2 || a9->c != 3) + __builtin_abort (); +#endif +#if __cpp_aggregate_paren_init + if (q1[0] != 1) + __builtin_abort (); + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") + || __builtin_strcmp (d3, "foo")) + __builtin_abort (); + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) + __builtin_abort (); + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) + __builtin_abort (); +#endif +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C new file mode 100644 index 00000000000..9fa3f9ea7a3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C @@ -0,0 +1,22 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +// Test error cases. +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } +struct Aggr { int a; int b; int c; }; +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; // { dg-error "invalid conversion" } +} + +void +g () +{ + int *p = fn ("a"); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C new file mode 100644 index 00000000000..f35124e159d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C @@ -0,0 +1,17 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; +} + +int +main () +{ + int *p0 = fn (); + int *p1 = fn (1); + int *p3 = fn (1, 2, 3); +} base-commit: 5e9ad288eb6fb366142b166e7985d16727b398e1 -- 2.26.2 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: Implement P1009: Array size deduction in new-expressions. 2020-08-20 20:22 [PATCH] c++: Implement P1009: Array size deduction in new-expressions Marek Polacek @ 2020-08-21 21:04 ` Jason Merrill 2020-08-24 21:44 ` [PATCH v2] " Marek Polacek 0 siblings, 1 reply; 6+ messages in thread From: Jason Merrill @ 2020-08-21 21:04 UTC (permalink / raw) To: Marek Polacek, GCC Patches On 8/20/20 4:22 PM, Marek Polacek wrote: > This patch implements C++20 P1009, allowing code like > > new double[]{1,2,3}; // array bound will be deduced > > Since this proposal makes the initialization rules more consistent, it is > applied to all previous versions of C++ (thus, effectively, all the way back > to C++11). > > My patch is based on Jason's patch that handled the basic case. I've > extended it to work with ()-init and also the string literal case. > Further testing revealed that to handle stuff like > > new int[]{t...}; > > in a template, we have to consider such a NEW_EXPR type-dependent. > Obviously, we first have to expand the pack to be able to deduce the > number of elements in the array. > > Curiously, while implementing this proposal, I noticed that we fail > to accept > > new char[4]{"abc"}; > > so I've assigned 77841 to self. I think the fix will depend on the > build_new_1 hunk in this patch. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > gcc/cp/ChangeLog: > > PR c++/93529 > * init.c (build_new_1): Handle new char[]{"foo"}. > (build_new): Deduce the array size in new-expression if not > present. Handle ()-init. Handle initializing an array from > a string literal. > * parser.c (cp_parser_new_type_id): Leave [] alone. > (cp_parser_direct_new_declarator): Allow []. > * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider > array types whose dimension has to be deduced type-dependent. > > gcc/testsuite/ChangeLog: > > PR c++/93529 > * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. > * g++.dg/cpp2a/new-array1.C: New test. > * g++.dg/cpp2a/new-array2.C: New test. > * g++.dg/cpp2a/new-array3.C: New test. > > Co-authored-by: Jason Merrill <jason@redhat.com> > --- > gcc/cp/init.c | 54 ++++++++++++++++++- > gcc/cp/parser.c | 11 ++-- > gcc/cp/pt.c | 4 ++ > gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- > gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ > 7 files changed, 180 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C > > diff --git a/gcc/cp/init.c b/gcc/cp/init.c > index 872c23453fd..ae1177079e4 100644 > --- a/gcc/cp/init.c > +++ b/gcc/cp/init.c > @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > else if (array_p) > { > tree vecinit = NULL_TREE; > - if (vec_safe_length (*init) == 1 > - && DIRECT_LIST_INIT_P ((**init)[0])) > + const size_t len = vec_safe_length (*init); > + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) > { > vecinit = (**init)[0]; > if (CONSTRUCTOR_NELTS (vecinit) == 0) > @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > vecinit = digest_init (arraytype, vecinit, complain); > } > } > + /* This handles code like new char[]{"foo"}. */ > + else if (len == 1 > + && char_type_p (TYPE_MAIN_VARIANT (type)) > + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) > + == STRING_CST) > + { > + vecinit = (**init)[0]; > + STRIP_ANY_LOCATION_WRAPPER (vecinit); > + } > else if (*init) > { > if (complain & tf_error) > @@ -3917,6 +3926,47 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, > return error_mark_node; > } > > + /* P1009: Array size deduction in new-expressions. */ > + if (TREE_CODE (type) == ARRAY_TYPE > + && !TYPE_DOMAIN (type) > + && *init) > + { > + /* This means we have 'new T[]()'. */ > + if ((*init)->is_empty ()) > + { > + tree ctor = build_constructor (init_list_type_node, NULL); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + vec_safe_push (*init, ctor); > + } > + tree &elt = (**init)[0]; > + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ > + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) > + { > + /* Handle new char[]("foo"). */ > + if (vec_safe_length (*init) == 1 > + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) > + && TREE_CODE (tree_strip_any_location_wrapper (elt)) > + == STRING_CST) > + /* Leave it alone: the string should not be wrapped in {}. */; > + else > + { > + /* Create a CONSTRUCTOR from the vector INIT. */ > + tree list = build_tree_list_vec (*init); > + tree ctor = build_constructor_from_list (init_list_type_node, list); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; It seems wasteful to build a TREE_LIST only to throw it away; let's add a function to build a CONSTRUCTOR directly from a vec<tree>*. And use it in build_new_method_call_1 as well. > + elt = ctor; > + /* We've squashed all the vector elements into the first one; > + truncate the rest. */ > + (*init)->truncate (1); > + } > + } > + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ > + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) > + elt = reshape_init (type, elt, complain); > + cp_complete_array_type (&type, elt, /*do_default*/false); > + } > + > /* The type allocated must be complete. If the new-type-id was > "T[N]" then we are just checking that "T" is complete here, but > that is equivalent, since the value of "N" doesn't matter. */ > diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > index 7cc2dbed5fe..dc1f2ec45ad 100644 > --- a/gcc/cp/parser.c > +++ b/gcc/cp/parser.c > @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) > if (*nelts == error_mark_node) > *nelts = integer_one_node; > > - if (outer_declarator) > + if (*nelts == NULL_TREE) > + /* Leave [] in the declarator. */; > + else if (outer_declarator) > outer_declarator->declarator = declarator->declarator; > else > new_declarator = NULL; > @@ -9082,14 +9084,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) > cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); > > token = cp_lexer_peek_token (parser->lexer); > - expression = cp_parser_expression (parser); > + if (token->type == CPP_CLOSE_SQUARE) > + expression = NULL_TREE; Only the first bound is optional; we need to require the expression for subsequent bounds. > + else > + expression = cp_parser_expression (parser); > /* The standard requires that the expression have integral > type. DR 74 adds enumeration types. We believe that the > real intent is that these expressions be handled like the > expression in a `switch' condition, which also allows > classes with a single conversion to integral or > enumeration type. */ > - if (!processing_template_decl) > + if (expression && !processing_template_decl) > { > expression > = build_expr_type_conversion (WANT_INT | WANT_ENUM, > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index 5dbdd37f6e3..fe318b84385 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) > return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) > || value_dependent_expression_p > (TREE_OPERAND (TREE_VALUE (type), 1)); > + /* Array type whose dimension has to be deduced. */ > + else if (TREE_CODE (type) == ARRAY_TYPE > + && TREE_OPERAND (expression, 2) == NULL_TREE) > + return true; > else > return dependent_type_p (type); > } > diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > index 1b24966e051..f271cf1df6a 100644 > --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> > static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; > }; > > -static_assert( !is_constructible_mini<int[], int>::value, ""); > +// int[](...) will work with P0960 and P1009. > +#if __cpp_aggregate_paren_init > +constexpr bool r = true; > +#else > +constexpr bool r = false; > +#endif > +static_assert( is_constructible_mini<int[], int>::value == r, ""); > static_assert( !is_constructible_mini<void, int>::value, ""); > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > new file mode 100644 > index 00000000000..2353c384359 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > @@ -0,0 +1,70 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do run { target c++11 } } > + > +// When the array bound is deduced to 0, malloc(0) returns > +// a non-dereferenceable pointer. > +int *p0 = new int[]{}; > +int *p1 = new int[]{ 1 }; > +int *p2 = new int[]{ 1, 2, 3 }; > +char *c1 = new char[]{"foo"}; > +#if __cpp_aggregate_paren_init > +int *q0 = new int[](); > +int *q1 = new int[](1); > +int *q2 = new int[](1, 2, 3); > +char *d1 = new char[]("foo"); > +char *d2 = new char[4]("foo"); > +char *d3 = new char[]((("foo"))); > +#endif > + > +struct Aggr { int a; int b; int c; }; > +Aggr *a1 = new Aggr[]{}; > +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; > +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; > +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; > +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; > +#if __cpp_designated_initializers > +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; > +#endif > +#if __cpp_aggregate_paren_init > +Aggr *a6 = new Aggr[](); > +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); > +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); > +#endif > + > +int > +main () > +{ > + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (c1, "foo")) > + __builtin_abort (); > + if (a2->a != 1 || a2->b != 2 || a2->c != 3) > + __builtin_abort (); > + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 > + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) > + __builtin_abort (); > + if (a4->a != 1 || a4->b != 2 || a4->c != 3) > + __builtin_abort (); > + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 > + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) > + __builtin_abort (); > +#if __cpp_designated_initializers > + if (a9->a != 1 || a9->b != 2 || a9->c != 3) > + __builtin_abort (); > +#endif > +#if __cpp_aggregate_paren_init > + if (q1[0] != 1) > + __builtin_abort (); > + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") > + || __builtin_strcmp (d3, "foo")) > + __builtin_abort (); > + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) > + __builtin_abort (); > + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 > + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) > + __builtin_abort (); > +#endif > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > new file mode 100644 > index 00000000000..9fa3f9ea7a3 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > @@ -0,0 +1,22 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +// Test error cases. > +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } > +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } > +struct Aggr { int a; int b; int c; }; > +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } > +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; // { dg-error "invalid conversion" } > +} > + > +void > +g () > +{ > + int *p = fn ("a"); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > new file mode 100644 > index 00000000000..f35124e159d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > @@ -0,0 +1,17 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; > +} > + > +int > +main () > +{ > + int *p0 = fn (); > + int *p1 = fn (1); > + int *p3 = fn (1, 2, 3); > +} > > base-commit: 5e9ad288eb6fb366142b166e7985d16727b398e1 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] c++: Implement P1009: Array size deduction in new-expressions. 2020-08-21 21:04 ` Jason Merrill @ 2020-08-24 21:44 ` Marek Polacek 2020-08-25 2:49 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2020-08-24 21:44 UTC (permalink / raw) To: Jason Merrill; +Cc: GCC Patches On Fri, Aug 21, 2020 at 05:04:43PM -0400, Jason Merrill wrote: > On 8/20/20 4:22 PM, Marek Polacek wrote: > > @@ -3917,6 +3926,47 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, > > return error_mark_node; > > } > > + /* P1009: Array size deduction in new-expressions. */ > > + if (TREE_CODE (type) == ARRAY_TYPE > > + && !TYPE_DOMAIN (type) > > + && *init) > > + { > > + /* This means we have 'new T[]()'. */ > > + if ((*init)->is_empty ()) > > + { > > + tree ctor = build_constructor (init_list_type_node, NULL); > > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > > + vec_safe_push (*init, ctor); > > + } > > + tree &elt = (**init)[0]; > > + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ > > + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) > > + { > > + /* Handle new char[]("foo"). */ > > + if (vec_safe_length (*init) == 1 > > + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) > > + && TREE_CODE (tree_strip_any_location_wrapper (elt)) > > + == STRING_CST) > > + /* Leave it alone: the string should not be wrapped in {}. */; > > + else > > + { > > + /* Create a CONSTRUCTOR from the vector INIT. */ > > + tree list = build_tree_list_vec (*init); > > + tree ctor = build_constructor_from_list (init_list_type_node, list); > > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > > + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; > > It seems wasteful to build a TREE_LIST only to throw it away; let's add a > function to build a CONSTRUCTOR directly from a vec<tree>*. And use it in > build_new_method_call_1 as well. Good point. Done. > > @@ -9082,14 +9084,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) > > cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); > > token = cp_lexer_peek_token (parser->lexer); > > - expression = cp_parser_expression (parser); > > + if (token->type == CPP_CLOSE_SQUARE) > > + expression = NULL_TREE; > > Only the first bound is optional; we need to require the expression for > subsequent bounds. Fixed (though the diagnostic we gave was pretty good). New test added. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This patch implements C++20 P1009, allowing code like new double[]{1,2,3}; // array bound will be deduced Since this proposal makes the initialization rules more consistent, it is applied to all previous versions of C++ (thus, effectively, all the way back to C++11). My patch is based on Jason's patch that handled the basic case. I've extended it to work with ()-init and also the string literal case. Further testing revealed that to handle stuff like new int[]{t...}; in a template, we have to consider such a NEW_EXPR type-dependent. Obviously, we first have to expand the pack to be able to deduce the number of elements in the array. Curiously, while implementing this proposal, I noticed that we fail to accept new char[4]{"abc"}; so I've assigned 77841 to self. I think the fix will depend on the build_new_1 hunk in this patch. The new tree.c function build_constructor_from_vec helps us morph a vector into a CONSTRUCTOR more efficiently. gcc/cp/ChangeLog: PR c++/93529 * call.c (build_new_method_call_1): Use build_constructor_from_vec instead of build_tree_list_vec + build_constructor_from_list. * init.c (build_new_1): Handle new char[]{"foo"}. Use build_constructor_from_vec instead of build_tree_list_vec + build_constructor_from_list. (build_new): Deduce the array size in new-expression if not present. Handle ()-init. Handle initializing an array from a string literal. * parser.c (cp_parser_new_type_id): Leave [] alone. (cp_parser_direct_new_declarator): Allow []. * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider array types whose dimension has to be deduced type-dependent. gcc/ChangeLog: PR c++/93529 * tree.c (build_constructor_from_vec): New. * tree.h (build_constructor_from_vec): Declare. gcc/testsuite/ChangeLog: PR c++/93529 * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. * g++.dg/cpp2a/new-array1.C: New test. * g++.dg/cpp2a/new-array2.C: New test. * g++.dg/cpp2a/new-array3.C: New test. * g++.dg/cpp2a/new-array4.C: New test. Co-authored-by: Jason Merrill <jason@redhat.com> --- gcc/cp/call.c | 4 +- gcc/cp/init.c | 55 +++++++++++++++++-- gcc/cp/parser.c | 13 +++-- gcc/cp/pt.c | 4 ++ gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ gcc/testsuite/g++.dg/cpp2a/new-array4.C | 10 ++++ gcc/tree.c | 17 ++++++ gcc/tree.h | 1 + 11 files changed, 211 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array4.C diff --git a/gcc/cp/call.c b/gcc/cp/call.c index c62d9e227d3..5ebb6ba81ab 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -10294,8 +10294,8 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args, && !vec_safe_is_empty (user_args)) { /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ - tree list = build_tree_list_vec (user_args); - tree ctor = build_constructor_from_list (init_list_type_node, list); + tree ctor = build_constructor_from_vec (init_list_type_node, + user_args); CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; if (is_dummy_object (instance)) diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 872c23453fd..360ab8c0b52 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, else if (array_p) { tree vecinit = NULL_TREE; - if (vec_safe_length (*init) == 1 - && DIRECT_LIST_INIT_P ((**init)[0])) + const size_t len = vec_safe_length (*init); + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) { vecinit = (**init)[0]; if (CONSTRUCTOR_NELTS (vecinit) == 0) @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, vecinit = digest_init (arraytype, vecinit, complain); } } + /* This handles code like new char[]{"foo"}. */ + else if (len == 1 + && char_type_p (TYPE_MAIN_VARIANT (type)) + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) + == STRING_CST) + { + vecinit = (**init)[0]; + STRIP_ANY_LOCATION_WRAPPER (vecinit); + } else if (*init) { if (complain & tf_error) @@ -3634,8 +3643,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, && AGGREGATE_TYPE_P (type) && (*init)->length () > 1) { - ie = build_tree_list_vec (*init); - ie = build_constructor_from_list (init_list_type_node, ie); + ie = build_constructor_from_vec (init_list_type_node, *init); CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; CONSTRUCTOR_IS_PAREN_INIT (ie) = true; ie = digest_init (type, ie, complain); @@ -3917,6 +3925,45 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, return error_mark_node; } + /* P1009: Array size deduction in new-expressions. */ + if (TREE_CODE (type) == ARRAY_TYPE + && !TYPE_DOMAIN (type) + && *init) + { + /* This means we have 'new T[]()'. */ + if ((*init)->is_empty ()) + { + tree ctor = build_constructor (init_list_type_node, NULL); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + vec_safe_push (*init, ctor); + } + tree &elt = (**init)[0]; + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) + { + /* Handle new char[]("foo"). */ + if (vec_safe_length (*init) == 1 + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) + && TREE_CODE (tree_strip_any_location_wrapper (elt)) + == STRING_CST) + /* Leave it alone: the string should not be wrapped in {}. */; + else + { + tree ctor = build_constructor_from_vec (init_list_type_node, *init); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; + elt = ctor; + /* We've squashed all the vector elements into the first one; + truncate the rest. */ + (*init)->truncate (1); + } + } + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) + elt = reshape_init (type, elt, complain); + cp_complete_array_type (&type, elt, /*do_default*/false); + } + /* The type allocated must be complete. If the new-type-id was "T[N]" then we are just checking that "T" is complete here, but that is equivalent, since the value of "N" doesn't matter. */ diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 7cc2dbed5fe..edc34a808fd 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) if (*nelts == error_mark_node) *nelts = integer_one_node; - if (outer_declarator) + if (*nelts == NULL_TREE) + /* Leave [] in the declarator. */; + else if (outer_declarator) outer_declarator->declarator = declarator->declarator; else new_declarator = NULL; @@ -9072,6 +9074,7 @@ static cp_declarator * cp_parser_direct_new_declarator (cp_parser* parser) { cp_declarator *declarator = NULL; + bool first_p = true; while (true) { @@ -9082,14 +9085,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); token = cp_lexer_peek_token (parser->lexer); - expression = cp_parser_expression (parser); + if (token->type == CPP_CLOSE_SQUARE && first_p) + expression = NULL_TREE; + else + expression = cp_parser_expression (parser); /* The standard requires that the expression have integral type. DR 74 adds enumeration types. We believe that the real intent is that these expressions be handled like the expression in a `switch' condition, which also allows classes with a single conversion to integral or enumeration type. */ - if (!processing_template_decl) + if (expression && !processing_template_decl) { expression = build_expr_type_conversion (WANT_INT | WANT_ENUM, @@ -9114,6 +9120,7 @@ cp_parser_direct_new_declarator (cp_parser* parser) bounds. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) break; + first_p = false; } return declarator; diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 5dbdd37f6e3..fe318b84385 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) || value_dependent_expression_p (TREE_OPERAND (TREE_VALUE (type), 1)); + /* Array type whose dimension has to be deduced. */ + else if (TREE_CODE (type) == ARRAY_TYPE + && TREE_OPERAND (expression, 2) == NULL_TREE) + return true; else return dependent_type_p (type); } diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C index 1b24966e051..f271cf1df6a 100644 --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; }; -static_assert( !is_constructible_mini<int[], int>::value, ""); +// int[](...) will work with P0960 and P1009. +#if __cpp_aggregate_paren_init +constexpr bool r = true; +#else +constexpr bool r = false; +#endif +static_assert( is_constructible_mini<int[], int>::value == r, ""); static_assert( !is_constructible_mini<void, int>::value, ""); diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C new file mode 100644 index 00000000000..2353c384359 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C @@ -0,0 +1,70 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do run { target c++11 } } + +// When the array bound is deduced to 0, malloc(0) returns +// a non-dereferenceable pointer. +int *p0 = new int[]{}; +int *p1 = new int[]{ 1 }; +int *p2 = new int[]{ 1, 2, 3 }; +char *c1 = new char[]{"foo"}; +#if __cpp_aggregate_paren_init +int *q0 = new int[](); +int *q1 = new int[](1); +int *q2 = new int[](1, 2, 3); +char *d1 = new char[]("foo"); +char *d2 = new char[4]("foo"); +char *d3 = new char[]((("foo"))); +#endif + +struct Aggr { int a; int b; int c; }; +Aggr *a1 = new Aggr[]{}; +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; +#if __cpp_designated_initializers +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; +#endif +#if __cpp_aggregate_paren_init +Aggr *a6 = new Aggr[](); +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); +#endif + +int +main () +{ + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (c1, "foo")) + __builtin_abort (); + if (a2->a != 1 || a2->b != 2 || a2->c != 3) + __builtin_abort (); + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) + __builtin_abort (); + if (a4->a != 1 || a4->b != 2 || a4->c != 3) + __builtin_abort (); + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) + __builtin_abort (); +#if __cpp_designated_initializers + if (a9->a != 1 || a9->b != 2 || a9->c != 3) + __builtin_abort (); +#endif +#if __cpp_aggregate_paren_init + if (q1[0] != 1) + __builtin_abort (); + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") + || __builtin_strcmp (d3, "foo")) + __builtin_abort (); + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) + __builtin_abort (); + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) + __builtin_abort (); +#endif +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C new file mode 100644 index 00000000000..9fa3f9ea7a3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C @@ -0,0 +1,22 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +// Test error cases. +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } +struct Aggr { int a; int b; int c; }; +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; // { dg-error "invalid conversion" } +} + +void +g () +{ + int *p = fn ("a"); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C new file mode 100644 index 00000000000..f35124e159d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C @@ -0,0 +1,17 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; +} + +int +main () +{ + int *p0 = fn (); + int *p1 = fn (1); + int *p3 = fn (1, 2, 3); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array4.C b/gcc/testsuite/g++.dg/cpp2a/new-array4.C new file mode 100644 index 00000000000..2c327ebc853 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array4.C @@ -0,0 +1,10 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +void +fn () +{ + new int[][3]{ { 1, 2, 3 } }; + new int[][]{ { 1, 2, 3 } }; // { dg-error "expected primary-expression" } +} diff --git a/gcc/tree.c b/gcc/tree.c index d0202c3f785..1ff13f218ce 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -2123,6 +2123,23 @@ build_constructor_from_list (tree type, tree vals) return build_constructor (type, v); } +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values + are in a vector pointed to by VALS. Note that the TREE_PURPOSE + fields in the constructor remain null. */ + +tree +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) +{ + tree ctor = build_constructor (type, NULL); + + unsigned int ix; + tree t; + FOR_EACH_VEC_SAFE_ELT (vals, ix, t) + CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), NULL_TREE, t); + + return ctor; +} + /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number of elements, provided as index/value pairs. */ diff --git a/gcc/tree.h b/gcc/tree.h index 22dd4ac0f3c..4fd51aca723 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4407,6 +4407,7 @@ extern void verify_constructor_flags (tree); extern tree build_constructor (tree, vec<constructor_elt, va_gc> * CXX_MEM_STAT_INFO); extern tree build_constructor_single (tree, tree, tree); extern tree build_constructor_from_list (tree, tree); +extern tree build_constructor_from_vec (tree, const vec<tree, va_gc> *); extern tree build_constructor_va (tree, int, ...); extern tree build_clobber (tree); extern tree build_real_from_int_cst (tree, const_tree); base-commit: 0d166f4a8773a43d925be006e713b7d81626ddb9 -- 2.26.2 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] c++: Implement P1009: Array size deduction in new-expressions. 2020-08-24 21:44 ` [PATCH v2] " Marek Polacek @ 2020-08-25 2:49 ` Jason Merrill 2020-08-25 12:26 ` [PATCH v3] " Marek Polacek 0 siblings, 1 reply; 6+ messages in thread From: Jason Merrill @ 2020-08-25 2:49 UTC (permalink / raw) To: Marek Polacek; +Cc: GCC Patches On 8/24/20 5:44 PM, Marek Polacek wrote: > On Fri, Aug 21, 2020 at 05:04:43PM -0400, Jason Merrill wrote: >> On 8/20/20 4:22 PM, Marek Polacek wrote: >>> @@ -3917,6 +3926,47 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, >>> return error_mark_node; >>> } >>> + /* P1009: Array size deduction in new-expressions. */ >>> + if (TREE_CODE (type) == ARRAY_TYPE >>> + && !TYPE_DOMAIN (type) >>> + && *init) >>> + { >>> + /* This means we have 'new T[]()'. */ >>> + if ((*init)->is_empty ()) >>> + { >>> + tree ctor = build_constructor (init_list_type_node, NULL); >>> + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; >>> + vec_safe_push (*init, ctor); >>> + } >>> + tree &elt = (**init)[0]; >>> + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ >>> + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) >>> + { >>> + /* Handle new char[]("foo"). */ >>> + if (vec_safe_length (*init) == 1 >>> + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) >>> + && TREE_CODE (tree_strip_any_location_wrapper (elt)) >>> + == STRING_CST) >>> + /* Leave it alone: the string should not be wrapped in {}. */; >>> + else >>> + { >>> + /* Create a CONSTRUCTOR from the vector INIT. */ >>> + tree list = build_tree_list_vec (*init); >>> + tree ctor = build_constructor_from_list (init_list_type_node, list); >>> + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; >>> + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; >> >> It seems wasteful to build a TREE_LIST only to throw it away; let's add a >> function to build a CONSTRUCTOR directly from a vec<tree>*. And use it in >> build_new_method_call_1 as well. > > Good point. Done. > >>> @@ -9082,14 +9084,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) >>> cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); >>> token = cp_lexer_peek_token (parser->lexer); >>> - expression = cp_parser_expression (parser); >>> + if (token->type == CPP_CLOSE_SQUARE) >>> + expression = NULL_TREE; >> >> Only the first bound is optional; we need to require the expression for >> subsequent bounds. > > Fixed (though the diagnostic we gave was pretty good). New test added. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > This patch implements C++20 P1009, allowing code like > > new double[]{1,2,3}; // array bound will be deduced > > Since this proposal makes the initialization rules more consistent, it is > applied to all previous versions of C++ (thus, effectively, all the way back > to C++11). > > My patch is based on Jason's patch that handled the basic case. I've > extended it to work with ()-init and also the string literal case. > Further testing revealed that to handle stuff like > > new int[]{t...}; > > in a template, we have to consider such a NEW_EXPR type-dependent. > Obviously, we first have to expand the pack to be able to deduce the > number of elements in the array. > > Curiously, while implementing this proposal, I noticed that we fail > to accept > > new char[4]{"abc"}; > > so I've assigned 77841 to self. I think the fix will depend on the > build_new_1 hunk in this patch. > > The new tree.c function build_constructor_from_vec helps us morph > a vector into a CONSTRUCTOR more efficiently. > > gcc/cp/ChangeLog: > > PR c++/93529 > * call.c (build_new_method_call_1): Use build_constructor_from_vec > instead of build_tree_list_vec + build_constructor_from_list. > * init.c (build_new_1): Handle new char[]{"foo"}. Use > build_constructor_from_vec instead of build_tree_list_vec + > build_constructor_from_list. > (build_new): Deduce the array size in new-expression if not > present. Handle ()-init. Handle initializing an array from > a string literal. > * parser.c (cp_parser_new_type_id): Leave [] alone. > (cp_parser_direct_new_declarator): Allow []. > * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider > array types whose dimension has to be deduced type-dependent. > > gcc/ChangeLog: > > PR c++/93529 > * tree.c (build_constructor_from_vec): New. > * tree.h (build_constructor_from_vec): Declare. > > gcc/testsuite/ChangeLog: > > PR c++/93529 > * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. > * g++.dg/cpp2a/new-array1.C: New test. > * g++.dg/cpp2a/new-array2.C: New test. > * g++.dg/cpp2a/new-array3.C: New test. > * g++.dg/cpp2a/new-array4.C: New test. > > Co-authored-by: Jason Merrill <jason@redhat.com> > --- > gcc/cp/call.c | 4 +- > gcc/cp/init.c | 55 +++++++++++++++++-- > gcc/cp/parser.c | 13 +++-- > gcc/cp/pt.c | 4 ++ > gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- > gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ > gcc/testsuite/g++.dg/cpp2a/new-array4.C | 10 ++++ > gcc/tree.c | 17 ++++++ > gcc/tree.h | 1 + > 11 files changed, 211 insertions(+), 10 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array4.C > > diff --git a/gcc/cp/call.c b/gcc/cp/call.c > index c62d9e227d3..5ebb6ba81ab 100644 > --- a/gcc/cp/call.c > +++ b/gcc/cp/call.c > @@ -10294,8 +10294,8 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args, > && !vec_safe_is_empty (user_args)) > { > /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ > - tree list = build_tree_list_vec (user_args); > - tree ctor = build_constructor_from_list (init_list_type_node, list); > + tree ctor = build_constructor_from_vec (init_list_type_node, > + user_args); > CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; > if (is_dummy_object (instance)) > diff --git a/gcc/cp/init.c b/gcc/cp/init.c > index 872c23453fd..360ab8c0b52 100644 > --- a/gcc/cp/init.c > +++ b/gcc/cp/init.c > @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > else if (array_p) > { > tree vecinit = NULL_TREE; > - if (vec_safe_length (*init) == 1 > - && DIRECT_LIST_INIT_P ((**init)[0])) > + const size_t len = vec_safe_length (*init); > + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) > { > vecinit = (**init)[0]; > if (CONSTRUCTOR_NELTS (vecinit) == 0) > @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > vecinit = digest_init (arraytype, vecinit, complain); > } > } > + /* This handles code like new char[]{"foo"}. */ > + else if (len == 1 > + && char_type_p (TYPE_MAIN_VARIANT (type)) > + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) > + == STRING_CST) > + { > + vecinit = (**init)[0]; > + STRIP_ANY_LOCATION_WRAPPER (vecinit); > + } > else if (*init) > { > if (complain & tf_error) > @@ -3634,8 +3643,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > && AGGREGATE_TYPE_P (type) > && (*init)->length () > 1) > { > - ie = build_tree_list_vec (*init); > - ie = build_constructor_from_list (init_list_type_node, ie); > + ie = build_constructor_from_vec (init_list_type_node, *init); > CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; > CONSTRUCTOR_IS_PAREN_INIT (ie) = true; > ie = digest_init (type, ie, complain); > @@ -3917,6 +3925,45 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, > return error_mark_node; > } > > + /* P1009: Array size deduction in new-expressions. */ > + if (TREE_CODE (type) == ARRAY_TYPE > + && !TYPE_DOMAIN (type) > + && *init) > + { > + /* This means we have 'new T[]()'. */ > + if ((*init)->is_empty ()) > + { > + tree ctor = build_constructor (init_list_type_node, NULL); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + vec_safe_push (*init, ctor); > + } > + tree &elt = (**init)[0]; > + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ > + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) > + { > + /* Handle new char[]("foo"). */ > + if (vec_safe_length (*init) == 1 > + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) > + && TREE_CODE (tree_strip_any_location_wrapper (elt)) > + == STRING_CST) > + /* Leave it alone: the string should not be wrapped in {}. */; > + else > + { > + tree ctor = build_constructor_from_vec (init_list_type_node, *init); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; > + elt = ctor; > + /* We've squashed all the vector elements into the first one; > + truncate the rest. */ > + (*init)->truncate (1); > + } > + } > + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ > + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) > + elt = reshape_init (type, elt, complain); > + cp_complete_array_type (&type, elt, /*do_default*/false); > + } > + > /* The type allocated must be complete. If the new-type-id was > "T[N]" then we are just checking that "T" is complete here, but > that is equivalent, since the value of "N" doesn't matter. */ > diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > index 7cc2dbed5fe..edc34a808fd 100644 > --- a/gcc/cp/parser.c > +++ b/gcc/cp/parser.c > @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) > if (*nelts == error_mark_node) > *nelts = integer_one_node; > > - if (outer_declarator) > + if (*nelts == NULL_TREE) > + /* Leave [] in the declarator. */; > + else if (outer_declarator) > outer_declarator->declarator = declarator->declarator; > else > new_declarator = NULL; > @@ -9072,6 +9074,7 @@ static cp_declarator * > cp_parser_direct_new_declarator (cp_parser* parser) > { > cp_declarator *declarator = NULL; > + bool first_p = true; > > while (true) > { > @@ -9082,14 +9085,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) > cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); > > token = cp_lexer_peek_token (parser->lexer); > - expression = cp_parser_expression (parser); > + if (token->type == CPP_CLOSE_SQUARE && first_p) > + expression = NULL_TREE; > + else > + expression = cp_parser_expression (parser); > /* The standard requires that the expression have integral > type. DR 74 adds enumeration types. We believe that the > real intent is that these expressions be handled like the > expression in a `switch' condition, which also allows > classes with a single conversion to integral or > enumeration type. */ > - if (!processing_template_decl) > + if (expression && !processing_template_decl) > { > expression > = build_expr_type_conversion (WANT_INT | WANT_ENUM, > @@ -9114,6 +9120,7 @@ cp_parser_direct_new_declarator (cp_parser* parser) > bounds. */ > if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) > break; > + first_p = false; > } > > return declarator; > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index 5dbdd37f6e3..fe318b84385 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) > return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) > || value_dependent_expression_p > (TREE_OPERAND (TREE_VALUE (type), 1)); > + /* Array type whose dimension has to be deduced. */ > + else if (TREE_CODE (type) == ARRAY_TYPE > + && TREE_OPERAND (expression, 2) == NULL_TREE) > + return true; > else > return dependent_type_p (type); > } > diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > index 1b24966e051..f271cf1df6a 100644 > --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> > static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; > }; > > -static_assert( !is_constructible_mini<int[], int>::value, ""); > +// int[](...) will work with P0960 and P1009. > +#if __cpp_aggregate_paren_init > +constexpr bool r = true; > +#else > +constexpr bool r = false; > +#endif > +static_assert( is_constructible_mini<int[], int>::value == r, ""); > static_assert( !is_constructible_mini<void, int>::value, ""); > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > new file mode 100644 > index 00000000000..2353c384359 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > @@ -0,0 +1,70 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do run { target c++11 } } > + > +// When the array bound is deduced to 0, malloc(0) returns > +// a non-dereferenceable pointer. > +int *p0 = new int[]{}; > +int *p1 = new int[]{ 1 }; > +int *p2 = new int[]{ 1, 2, 3 }; > +char *c1 = new char[]{"foo"}; > +#if __cpp_aggregate_paren_init > +int *q0 = new int[](); > +int *q1 = new int[](1); > +int *q2 = new int[](1, 2, 3); > +char *d1 = new char[]("foo"); > +char *d2 = new char[4]("foo"); > +char *d3 = new char[]((("foo"))); > +#endif > + > +struct Aggr { int a; int b; int c; }; > +Aggr *a1 = new Aggr[]{}; > +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; > +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; > +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; > +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; > +#if __cpp_designated_initializers > +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; > +#endif > +#if __cpp_aggregate_paren_init > +Aggr *a6 = new Aggr[](); > +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); > +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); > +#endif > + > +int > +main () > +{ > + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (c1, "foo")) > + __builtin_abort (); > + if (a2->a != 1 || a2->b != 2 || a2->c != 3) > + __builtin_abort (); > + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 > + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) > + __builtin_abort (); > + if (a4->a != 1 || a4->b != 2 || a4->c != 3) > + __builtin_abort (); > + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 > + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) > + __builtin_abort (); > +#if __cpp_designated_initializers > + if (a9->a != 1 || a9->b != 2 || a9->c != 3) > + __builtin_abort (); > +#endif > +#if __cpp_aggregate_paren_init > + if (q1[0] != 1) > + __builtin_abort (); > + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") > + || __builtin_strcmp (d3, "foo")) > + __builtin_abort (); > + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) > + __builtin_abort (); > + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 > + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) > + __builtin_abort (); > +#endif > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > new file mode 100644 > index 00000000000..9fa3f9ea7a3 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > @@ -0,0 +1,22 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +// Test error cases. > +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } > +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } > +struct Aggr { int a; int b; int c; }; > +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } > +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; // { dg-error "invalid conversion" } > +} > + > +void > +g () > +{ > + int *p = fn ("a"); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > new file mode 100644 > index 00000000000..f35124e159d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > @@ -0,0 +1,17 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; > +} > + > +int > +main () > +{ > + int *p0 = fn (); > + int *p1 = fn (1); > + int *p3 = fn (1, 2, 3); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array4.C b/gcc/testsuite/g++.dg/cpp2a/new-array4.C > new file mode 100644 > index 00000000000..2c327ebc853 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array4.C > @@ -0,0 +1,10 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +void > +fn () > +{ > + new int[][3]{ { 1, 2, 3 } }; > + new int[][]{ { 1, 2, 3 } }; // { dg-error "expected primary-expression" } > +} > diff --git a/gcc/tree.c b/gcc/tree.c > index d0202c3f785..1ff13f218ce 100644 > --- a/gcc/tree.c > +++ b/gcc/tree.c > @@ -2123,6 +2123,23 @@ build_constructor_from_list (tree type, tree vals) > return build_constructor (type, v); > } > > +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values > + are in a vector pointed to by VALS. Note that the TREE_PURPOSE > + fields in the constructor remain null. */ > + > +tree > +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) > +{ > + tree ctor = build_constructor (type, NULL); > + > + unsigned int ix; > + tree t; > + FOR_EACH_VEC_SAFE_ELT (vals, ix, t) > + CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), NULL_TREE, t); Either you need to build up the constructor_elt vec first and then pass it to build_constructor, or call recompute_constructor_flags after you add all the elements. And perhaps for (tree t : *vals) Jason > + return ctor; > +} > + > /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number > of elements, provided as index/value pairs. */ > > diff --git a/gcc/tree.h b/gcc/tree.h > index 22dd4ac0f3c..4fd51aca723 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -4407,6 +4407,7 @@ extern void verify_constructor_flags (tree); > extern tree build_constructor (tree, vec<constructor_elt, va_gc> * CXX_MEM_STAT_INFO); > extern tree build_constructor_single (tree, tree, tree); > extern tree build_constructor_from_list (tree, tree); > +extern tree build_constructor_from_vec (tree, const vec<tree, va_gc> *); > extern tree build_constructor_va (tree, int, ...); > extern tree build_clobber (tree); > extern tree build_real_from_int_cst (tree, const_tree); > > base-commit: 0d166f4a8773a43d925be006e713b7d81626ddb9 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3] c++: Implement P1009: Array size deduction in new-expressions. 2020-08-25 2:49 ` Jason Merrill @ 2020-08-25 12:26 ` Marek Polacek 2020-08-31 19:43 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2020-08-25 12:26 UTC (permalink / raw) To: Jason Merrill; +Cc: GCC Patches On Mon, Aug 24, 2020 at 10:49:38PM -0400, Jason Merrill wrote: > On 8/24/20 5:44 PM, Marek Polacek wrote: > > --- a/gcc/tree.c > > +++ b/gcc/tree.c > > @@ -2123,6 +2123,23 @@ build_constructor_from_list (tree type, tree vals) > > return build_constructor (type, v); > > } > > +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values > > + are in a vector pointed to by VALS. Note that the TREE_PURPOSE > > + fields in the constructor remain null. */ > > + > > +tree > > +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) > > +{ > > + tree ctor = build_constructor (type, NULL); > > + > > + unsigned int ix; > > + tree t; > > + FOR_EACH_VEC_SAFE_ELT (vals, ix, t) > > + CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), NULL_TREE, t); > > Either you need to build up the constructor_elt vec first and then pass it > to build_constructor, or call recompute_constructor_flags after you add all > the elements. Ug. I wanted to get rid of the vec<constructor_elt, va_gc> temporary, moved build_constructor, and forgot about recompute. Fixed. > And perhaps > > for (tree t : *vals) Nice! So nice to get rid of the ix temp, too. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This patch implements C++20 P1009, allowing code like new double[]{1,2,3}; // array bound will be deduced Since this proposal makes the initialization rules more consistent, it is applied to all previous versions of C++ (thus, effectively, all the way back to C++11). My patch is based on Jason's patch that handled the basic case. I've extended it to work with ()-init and also the string literal case. Further testing revealed that to handle stuff like new int[]{t...}; in a template, we have to consider such a NEW_EXPR type-dependent. Obviously, we first have to expand the pack to be able to deduce the number of elements in the array. Curiously, while implementing this proposal, I noticed that we fail to accept new char[4]{"abc"}; so I've assigned 77841 to self. I think the fix will depend on the build_new_1 hunk in this patch. The new tree.c function build_constructor_from_vec helps us morph a vector into a CONSTRUCTOR more efficiently. gcc/cp/ChangeLog: PR c++/93529 * call.c (build_new_method_call_1): Use build_constructor_from_vec instead of build_tree_list_vec + build_constructor_from_list. * init.c (build_new_1): Handle new char[]{"foo"}. Use build_constructor_from_vec instead of build_tree_list_vec + build_constructor_from_list. (build_new): Deduce the array size in new-expression if not present. Handle ()-init. Handle initializing an array from a string literal. * parser.c (cp_parser_new_type_id): Leave [] alone. (cp_parser_direct_new_declarator): Allow []. * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider array types whose dimension has to be deduced type-dependent. gcc/ChangeLog: PR c++/93529 * tree.c (build_constructor_from_vec): New. * tree.h (build_constructor_from_vec): Declare. gcc/testsuite/ChangeLog: PR c++/93529 * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. * g++.dg/cpp2a/new-array1.C: New test. * g++.dg/cpp2a/new-array2.C: New test. * g++.dg/cpp2a/new-array3.C: New test. * g++.dg/cpp2a/new-array4.C: New test. Co-authored-by: Jason Merrill <jason@redhat.com> --- gcc/cp/call.c | 4 +- gcc/cp/init.c | 55 +++++++++++++++++-- gcc/cp/parser.c | 13 +++-- gcc/cp/pt.c | 4 ++ gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ gcc/testsuite/g++.dg/cpp2a/new-array4.C | 10 ++++ gcc/tree.c | 15 ++++++ gcc/tree.h | 1 + 11 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array4.C diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 4726e57a30d..61bbb38bd2b 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -10297,8 +10297,8 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args, && !vec_safe_is_empty (user_args)) { /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ - tree list = build_tree_list_vec (user_args); - tree ctor = build_constructor_from_list (init_list_type_node, list); + tree ctor = build_constructor_from_vec (init_list_type_node, + user_args); CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; if (is_dummy_object (instance)) diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 872c23453fd..360ab8c0b52 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, else if (array_p) { tree vecinit = NULL_TREE; - if (vec_safe_length (*init) == 1 - && DIRECT_LIST_INIT_P ((**init)[0])) + const size_t len = vec_safe_length (*init); + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) { vecinit = (**init)[0]; if (CONSTRUCTOR_NELTS (vecinit) == 0) @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, vecinit = digest_init (arraytype, vecinit, complain); } } + /* This handles code like new char[]{"foo"}. */ + else if (len == 1 + && char_type_p (TYPE_MAIN_VARIANT (type)) + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) + == STRING_CST) + { + vecinit = (**init)[0]; + STRIP_ANY_LOCATION_WRAPPER (vecinit); + } else if (*init) { if (complain & tf_error) @@ -3634,8 +3643,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, && AGGREGATE_TYPE_P (type) && (*init)->length () > 1) { - ie = build_tree_list_vec (*init); - ie = build_constructor_from_list (init_list_type_node, ie); + ie = build_constructor_from_vec (init_list_type_node, *init); CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; CONSTRUCTOR_IS_PAREN_INIT (ie) = true; ie = digest_init (type, ie, complain); @@ -3917,6 +3925,45 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, return error_mark_node; } + /* P1009: Array size deduction in new-expressions. */ + if (TREE_CODE (type) == ARRAY_TYPE + && !TYPE_DOMAIN (type) + && *init) + { + /* This means we have 'new T[]()'. */ + if ((*init)->is_empty ()) + { + tree ctor = build_constructor (init_list_type_node, NULL); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + vec_safe_push (*init, ctor); + } + tree &elt = (**init)[0]; + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) + { + /* Handle new char[]("foo"). */ + if (vec_safe_length (*init) == 1 + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) + && TREE_CODE (tree_strip_any_location_wrapper (elt)) + == STRING_CST) + /* Leave it alone: the string should not be wrapped in {}. */; + else + { + tree ctor = build_constructor_from_vec (init_list_type_node, *init); + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; + elt = ctor; + /* We've squashed all the vector elements into the first one; + truncate the rest. */ + (*init)->truncate (1); + } + } + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) + elt = reshape_init (type, elt, complain); + cp_complete_array_type (&type, elt, /*do_default*/false); + } + /* The type allocated must be complete. If the new-type-id was "T[N]" then we are just checking that "T" is complete here, but that is equivalent, since the value of "N" doesn't matter. */ diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 7cc2dbed5fe..edc34a808fd 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) if (*nelts == error_mark_node) *nelts = integer_one_node; - if (outer_declarator) + if (*nelts == NULL_TREE) + /* Leave [] in the declarator. */; + else if (outer_declarator) outer_declarator->declarator = declarator->declarator; else new_declarator = NULL; @@ -9072,6 +9074,7 @@ static cp_declarator * cp_parser_direct_new_declarator (cp_parser* parser) { cp_declarator *declarator = NULL; + bool first_p = true; while (true) { @@ -9082,14 +9085,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); token = cp_lexer_peek_token (parser->lexer); - expression = cp_parser_expression (parser); + if (token->type == CPP_CLOSE_SQUARE && first_p) + expression = NULL_TREE; + else + expression = cp_parser_expression (parser); /* The standard requires that the expression have integral type. DR 74 adds enumeration types. We believe that the real intent is that these expressions be handled like the expression in a `switch' condition, which also allows classes with a single conversion to integral or enumeration type. */ - if (!processing_template_decl) + if (expression && !processing_template_decl) { expression = build_expr_type_conversion (WANT_INT | WANT_ENUM, @@ -9114,6 +9120,7 @@ cp_parser_direct_new_declarator (cp_parser* parser) bounds. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) break; + first_p = false; } return declarator; diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 5dbdd37f6e3..fe318b84385 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) || value_dependent_expression_p (TREE_OPERAND (TREE_VALUE (type), 1)); + /* Array type whose dimension has to be deduced. */ + else if (TREE_CODE (type) == ARRAY_TYPE + && TREE_OPERAND (expression, 2) == NULL_TREE) + return true; else return dependent_type_p (type); } diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C index 1b24966e051..f271cf1df6a 100644 --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; }; -static_assert( !is_constructible_mini<int[], int>::value, ""); +// int[](...) will work with P0960 and P1009. +#if __cpp_aggregate_paren_init +constexpr bool r = true; +#else +constexpr bool r = false; +#endif +static_assert( is_constructible_mini<int[], int>::value == r, ""); static_assert( !is_constructible_mini<void, int>::value, ""); diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C new file mode 100644 index 00000000000..2353c384359 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C @@ -0,0 +1,70 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do run { target c++11 } } + +// When the array bound is deduced to 0, malloc(0) returns +// a non-dereferenceable pointer. +int *p0 = new int[]{}; +int *p1 = new int[]{ 1 }; +int *p2 = new int[]{ 1, 2, 3 }; +char *c1 = new char[]{"foo"}; +#if __cpp_aggregate_paren_init +int *q0 = new int[](); +int *q1 = new int[](1); +int *q2 = new int[](1, 2, 3); +char *d1 = new char[]("foo"); +char *d2 = new char[4]("foo"); +char *d3 = new char[]((("foo"))); +#endif + +struct Aggr { int a; int b; int c; }; +Aggr *a1 = new Aggr[]{}; +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; +#if __cpp_designated_initializers +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; +#endif +#if __cpp_aggregate_paren_init +Aggr *a6 = new Aggr[](); +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); +#endif + +int +main () +{ + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (c1, "foo")) + __builtin_abort (); + if (a2->a != 1 || a2->b != 2 || a2->c != 3) + __builtin_abort (); + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) + __builtin_abort (); + if (a4->a != 1 || a4->b != 2 || a4->c != 3) + __builtin_abort (); + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) + __builtin_abort (); +#if __cpp_designated_initializers + if (a9->a != 1 || a9->b != 2 || a9->c != 3) + __builtin_abort (); +#endif +#if __cpp_aggregate_paren_init + if (q1[0] != 1) + __builtin_abort (); + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) + __builtin_abort (); + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") + || __builtin_strcmp (d3, "foo")) + __builtin_abort (); + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) + __builtin_abort (); + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) + __builtin_abort (); +#endif +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C new file mode 100644 index 00000000000..9fa3f9ea7a3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C @@ -0,0 +1,22 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +// Test error cases. +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } +struct Aggr { int a; int b; int c; }; +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; // { dg-error "invalid conversion" } +} + +void +g () +{ + int *p = fn ("a"); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C new file mode 100644 index 00000000000..f35124e159d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C @@ -0,0 +1,17 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +template<typename... T> +int *fn(T... t) +{ + return new int[]{t...}; +} + +int +main () +{ + int *p0 = fn (); + int *p1 = fn (1); + int *p3 = fn (1, 2, 3); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array4.C b/gcc/testsuite/g++.dg/cpp2a/new-array4.C new file mode 100644 index 00000000000..2c327ebc853 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/new-array4.C @@ -0,0 +1,10 @@ +// PR c++/93529 +// P1009: Array size deduction in new-expressions +// { dg-do compile { target c++11 } } + +void +fn () +{ + new int[][3]{ { 1, 2, 3 } }; + new int[][]{ { 1, 2, 3 } }; // { dg-error "expected primary-expression" } +} diff --git a/gcc/tree.c b/gcc/tree.c index d0202c3f785..45aacadbe2d 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -2123,6 +2123,21 @@ build_constructor_from_list (tree type, tree vals) return build_constructor (type, v); } +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values + are in a vector pointed to by VALS. Note that the TREE_PURPOSE + fields in the constructor remain null. */ + +tree +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) +{ + vec<constructor_elt, va_gc> *v = NULL; + + for (tree t : *vals) + CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t); + + return build_constructor (type, v); +} + /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number of elements, provided as index/value pairs. */ diff --git a/gcc/tree.h b/gcc/tree.h index 22dd4ac0f3c..4fd51aca723 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4407,6 +4407,7 @@ extern void verify_constructor_flags (tree); extern tree build_constructor (tree, vec<constructor_elt, va_gc> * CXX_MEM_STAT_INFO); extern tree build_constructor_single (tree, tree, tree); extern tree build_constructor_from_list (tree, tree); +extern tree build_constructor_from_vec (tree, const vec<tree, va_gc> *); extern tree build_constructor_va (tree, int, ...); extern tree build_clobber (tree); extern tree build_real_from_int_cst (tree, const_tree); base-commit: 26ea069ec02f15593ff35cf1d5b6056c1c17e4f4 -- 2.26.2 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3] c++: Implement P1009: Array size deduction in new-expressions. 2020-08-25 12:26 ` [PATCH v3] " Marek Polacek @ 2020-08-31 19:43 ` Jason Merrill 0 siblings, 0 replies; 6+ messages in thread From: Jason Merrill @ 2020-08-31 19:43 UTC (permalink / raw) To: Marek Polacek; +Cc: GCC Patches On 8/25/20 8:26 AM, Marek Polacek wrote: > On Mon, Aug 24, 2020 at 10:49:38PM -0400, Jason Merrill wrote: >> On 8/24/20 5:44 PM, Marek Polacek wrote: >>> --- a/gcc/tree.c >>> +++ b/gcc/tree.c >>> @@ -2123,6 +2123,23 @@ build_constructor_from_list (tree type, tree vals) >>> return build_constructor (type, v); >>> } >>> +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values >>> + are in a vector pointed to by VALS. Note that the TREE_PURPOSE >>> + fields in the constructor remain null. */ >>> + >>> +tree >>> +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) >>> +{ >>> + tree ctor = build_constructor (type, NULL); >>> + >>> + unsigned int ix; >>> + tree t; >>> + FOR_EACH_VEC_SAFE_ELT (vals, ix, t) >>> + CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), NULL_TREE, t); >> >> Either you need to build up the constructor_elt vec first and then pass it >> to build_constructor, or call recompute_constructor_flags after you add all >> the elements. > > Ug. I wanted to get rid of the vec<constructor_elt, va_gc> temporary, moved > build_constructor, and forgot about recompute. Fixed. > >> And perhaps >> >> for (tree t : *vals) > > Nice! So nice to get rid of the ix temp, too. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > This patch implements C++20 P1009, allowing code like > > new double[]{1,2,3}; // array bound will be deduced > > Since this proposal makes the initialization rules more consistent, it is > applied to all previous versions of C++ (thus, effectively, all the way back > to C++11). > > My patch is based on Jason's patch that handled the basic case. I've > extended it to work with ()-init and also the string literal case. > Further testing revealed that to handle stuff like > > new int[]{t...}; > > in a template, we have to consider such a NEW_EXPR type-dependent. > Obviously, we first have to expand the pack to be able to deduce the > number of elements in the array. > > Curiously, while implementing this proposal, I noticed that we fail > to accept > > new char[4]{"abc"}; > > so I've assigned 77841 to self. I think the fix will depend on the > build_new_1 hunk in this patch. > > The new tree.c function build_constructor_from_vec helps us morph > a vector into a CONSTRUCTOR more efficiently. > > gcc/cp/ChangeLog: > > PR c++/93529 > * call.c (build_new_method_call_1): Use build_constructor_from_vec > instead of build_tree_list_vec + build_constructor_from_list. > * init.c (build_new_1): Handle new char[]{"foo"}. Use > build_constructor_from_vec instead of build_tree_list_vec + > build_constructor_from_list. > (build_new): Deduce the array size in new-expression if not > present. Handle ()-init. Handle initializing an array from > a string literal. > * parser.c (cp_parser_new_type_id): Leave [] alone. > (cp_parser_direct_new_declarator): Allow []. > * pt.c (type_dependent_expression_p): In a NEW_EXPR, consider > array types whose dimension has to be deduced type-dependent. > > gcc/ChangeLog: > > PR c++/93529 > * tree.c (build_constructor_from_vec): New. > * tree.h (build_constructor_from_vec): Declare. > > gcc/testsuite/ChangeLog: > > PR c++/93529 > * g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009. > * g++.dg/cpp2a/new-array1.C: New test. > * g++.dg/cpp2a/new-array2.C: New test. > * g++.dg/cpp2a/new-array3.C: New test. > * g++.dg/cpp2a/new-array4.C: New test. > > Co-authored-by: Jason Merrill <jason@redhat.com> > --- > gcc/cp/call.c | 4 +- > gcc/cp/init.c | 55 +++++++++++++++++-- > gcc/cp/parser.c | 13 +++-- > gcc/cp/pt.c | 4 ++ > gcc/testsuite/g++.dg/cpp0x/sfinae4.C | 8 ++- > gcc/testsuite/g++.dg/cpp2a/new-array1.C | 70 +++++++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array2.C | 22 ++++++++ > gcc/testsuite/g++.dg/cpp2a/new-array3.C | 17 ++++++ > gcc/testsuite/g++.dg/cpp2a/new-array4.C | 10 ++++ > gcc/tree.c | 15 ++++++ > gcc/tree.h | 1 + > 11 files changed, 209 insertions(+), 10 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array1.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array2.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array3.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/new-array4.C > > diff --git a/gcc/cp/call.c b/gcc/cp/call.c > index 4726e57a30d..61bbb38bd2b 100644 > --- a/gcc/cp/call.c > +++ b/gcc/cp/call.c > @@ -10297,8 +10297,8 @@ build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args, > && !vec_safe_is_empty (user_args)) > { > /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ > - tree list = build_tree_list_vec (user_args); > - tree ctor = build_constructor_from_list (init_list_type_node, list); > + tree ctor = build_constructor_from_vec (init_list_type_node, > + user_args); > CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; > if (is_dummy_object (instance)) > diff --git a/gcc/cp/init.c b/gcc/cp/init.c > index 872c23453fd..360ab8c0b52 100644 > --- a/gcc/cp/init.c > +++ b/gcc/cp/init.c > @@ -3559,8 +3559,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > else if (array_p) > { > tree vecinit = NULL_TREE; > - if (vec_safe_length (*init) == 1 > - && DIRECT_LIST_INIT_P ((**init)[0])) > + const size_t len = vec_safe_length (*init); > + if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) > { > vecinit = (**init)[0]; > if (CONSTRUCTOR_NELTS (vecinit) == 0) > @@ -3578,6 +3578,15 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > vecinit = digest_init (arraytype, vecinit, complain); > } > } > + /* This handles code like new char[]{"foo"}. */ > + else if (len == 1 > + && char_type_p (TYPE_MAIN_VARIANT (type)) > + && TREE_CODE (tree_strip_any_location_wrapper ((**init)[0])) > + == STRING_CST) > + { > + vecinit = (**init)[0]; > + STRIP_ANY_LOCATION_WRAPPER (vecinit); > + } > else if (*init) > { > if (complain & tf_error) > @@ -3634,8 +3643,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, > && AGGREGATE_TYPE_P (type) > && (*init)->length () > 1) > { > - ie = build_tree_list_vec (*init); > - ie = build_constructor_from_list (init_list_type_node, ie); > + ie = build_constructor_from_vec (init_list_type_node, *init); > CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; > CONSTRUCTOR_IS_PAREN_INIT (ie) = true; > ie = digest_init (type, ie, complain); > @@ -3917,6 +3925,45 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, > return error_mark_node; > } > > + /* P1009: Array size deduction in new-expressions. */ > + if (TREE_CODE (type) == ARRAY_TYPE > + && !TYPE_DOMAIN (type) > + && *init) > + { > + /* This means we have 'new T[]()'. */ > + if ((*init)->is_empty ()) > + { > + tree ctor = build_constructor (init_list_type_node, NULL); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + vec_safe_push (*init, ctor); > + } > + tree &elt = (**init)[0]; > + /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ > + if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) > + { > + /* Handle new char[]("foo"). */ > + if (vec_safe_length (*init) == 1 > + && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) > + && TREE_CODE (tree_strip_any_location_wrapper (elt)) > + == STRING_CST) > + /* Leave it alone: the string should not be wrapped in {}. */; > + else > + { > + tree ctor = build_constructor_from_vec (init_list_type_node, *init); > + CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; > + CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; > + elt = ctor; > + /* We've squashed all the vector elements into the first one; > + truncate the rest. */ > + (*init)->truncate (1); > + } > + } > + /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ > + if (BRACE_ENCLOSED_INITIALIZER_P (elt)) > + elt = reshape_init (type, elt, complain); > + cp_complete_array_type (&type, elt, /*do_default*/false); > + } > + > /* The type allocated must be complete. If the new-type-id was > "T[N]" then we are just checking that "T" is complete here, but > that is equivalent, since the value of "N" doesn't matter. */ > diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > index 7cc2dbed5fe..edc34a808fd 100644 > --- a/gcc/cp/parser.c > +++ b/gcc/cp/parser.c > @@ -9011,7 +9011,9 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts) > if (*nelts == error_mark_node) > *nelts = integer_one_node; > > - if (outer_declarator) > + if (*nelts == NULL_TREE) > + /* Leave [] in the declarator. */; > + else if (outer_declarator) > outer_declarator->declarator = declarator->declarator; > else > new_declarator = NULL; > @@ -9072,6 +9074,7 @@ static cp_declarator * > cp_parser_direct_new_declarator (cp_parser* parser) > { > cp_declarator *declarator = NULL; > + bool first_p = true; > > while (true) > { > @@ -9082,14 +9085,17 @@ cp_parser_direct_new_declarator (cp_parser* parser) > cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); > > token = cp_lexer_peek_token (parser->lexer); > - expression = cp_parser_expression (parser); > + if (token->type == CPP_CLOSE_SQUARE && first_p) > + expression = NULL_TREE; > + else > + expression = cp_parser_expression (parser); > /* The standard requires that the expression have integral > type. DR 74 adds enumeration types. We believe that the > real intent is that these expressions be handled like the > expression in a `switch' condition, which also allows > classes with a single conversion to integral or > enumeration type. */ > - if (!processing_template_decl) > + if (expression && !processing_template_decl) > { > expression > = build_expr_type_conversion (WANT_INT | WANT_ENUM, > @@ -9114,6 +9120,7 @@ cp_parser_direct_new_declarator (cp_parser* parser) > bounds. */ > if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) > break; > + first_p = false; > } > > return declarator; > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index 5dbdd37f6e3..fe318b84385 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -26872,6 +26872,10 @@ type_dependent_expression_p (tree expression) > return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) > || value_dependent_expression_p > (TREE_OPERAND (TREE_VALUE (type), 1)); > + /* Array type whose dimension has to be deduced. */ > + else if (TREE_CODE (type) == ARRAY_TYPE > + && TREE_OPERAND (expression, 2) == NULL_TREE) > + return true; > else > return dependent_type_p (type); > } > diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > index 1b24966e051..f271cf1df6a 100644 > --- a/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae4.C > @@ -19,5 +19,11 @@ template<typename _Tp, typename... _Args> > static const bool value = sizeof(__test<_Tp, _Args...>(0)) == 1; > }; > > -static_assert( !is_constructible_mini<int[], int>::value, ""); > +// int[](...) will work with P0960 and P1009. > +#if __cpp_aggregate_paren_init > +constexpr bool r = true; > +#else > +constexpr bool r = false; > +#endif > +static_assert( is_constructible_mini<int[], int>::value == r, ""); > static_assert( !is_constructible_mini<void, int>::value, ""); > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array1.C b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > new file mode 100644 > index 00000000000..2353c384359 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array1.C > @@ -0,0 +1,70 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do run { target c++11 } } > + > +// When the array bound is deduced to 0, malloc(0) returns > +// a non-dereferenceable pointer. > +int *p0 = new int[]{}; > +int *p1 = new int[]{ 1 }; > +int *p2 = new int[]{ 1, 2, 3 }; > +char *c1 = new char[]{"foo"}; > +#if __cpp_aggregate_paren_init > +int *q0 = new int[](); > +int *q1 = new int[](1); > +int *q2 = new int[](1, 2, 3); > +char *d1 = new char[]("foo"); > +char *d2 = new char[4]("foo"); > +char *d3 = new char[]((("foo"))); > +#endif > + > +struct Aggr { int a; int b; int c; }; > +Aggr *a1 = new Aggr[]{}; > +Aggr *a2 = new Aggr[]{ 1, 2, 3 }; > +Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 }; > +Aggr *a4 = new Aggr[]{ { 1, 2, 3 } }; > +Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } }; > +#if __cpp_designated_initializers > +Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } }; > +#endif > +#if __cpp_aggregate_paren_init > +Aggr *a6 = new Aggr[](); > +Aggr *a7 = new Aggr[]({ 1, 2, 3 }); > +Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 }); > +#endif > + > +int > +main () > +{ > + if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (c1, "foo")) > + __builtin_abort (); > + if (a2->a != 1 || a2->b != 2 || a2->c != 3) > + __builtin_abort (); > + if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3 > + || a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0) > + __builtin_abort (); > + if (a4->a != 1 || a4->b != 2 || a4->c != 3) > + __builtin_abort (); > + if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0 > + || a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0) > + __builtin_abort (); > +#if __cpp_designated_initializers > + if (a9->a != 1 || a9->b != 2 || a9->c != 3) > + __builtin_abort (); > +#endif > +#if __cpp_aggregate_paren_init > + if (q1[0] != 1) > + __builtin_abort (); > + if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3) > + __builtin_abort (); > + if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo") > + || __builtin_strcmp (d3, "foo")) > + __builtin_abort (); > + if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3) > + __builtin_abort (); > + if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0 > + || a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0) > + __builtin_abort (); > +#endif > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array2.C b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > new file mode 100644 > index 00000000000..9fa3f9ea7a3 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array2.C > @@ -0,0 +1,22 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +// Test error cases. > +int *p = new double[] = { 1, 2, 3}; // { dg-error "invalid use of array with unspecified bounds" } > +int *p2 = new double[] = (1, 2, 3); // { dg-error "invalid use of array with unspecified bounds" } > +struct Aggr { int a; int b; int c; }; > +Aggr *p3 = new Aggr[]( 1, 2, 3 ); // { dg-error "could not convert|parenthesized initializer" } > +char *p4 = new char[]("foo", "a"); // { dg-error "invalid conversion|parenthesized initializer" } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; // { dg-error "invalid conversion" } > +} > + > +void > +g () > +{ > + int *p = fn ("a"); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array3.C b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > new file mode 100644 > index 00000000000..f35124e159d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array3.C > @@ -0,0 +1,17 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +template<typename... T> > +int *fn(T... t) > +{ > + return new int[]{t...}; > +} > + > +int > +main () > +{ > + int *p0 = fn (); > + int *p1 = fn (1); > + int *p3 = fn (1, 2, 3); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/new-array4.C b/gcc/testsuite/g++.dg/cpp2a/new-array4.C > new file mode 100644 > index 00000000000..2c327ebc853 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/new-array4.C > @@ -0,0 +1,10 @@ > +// PR c++/93529 > +// P1009: Array size deduction in new-expressions > +// { dg-do compile { target c++11 } } > + > +void > +fn () > +{ > + new int[][3]{ { 1, 2, 3 } }; > + new int[][]{ { 1, 2, 3 } }; // { dg-error "expected primary-expression" } > +} > diff --git a/gcc/tree.c b/gcc/tree.c > index d0202c3f785..45aacadbe2d 100644 > --- a/gcc/tree.c > +++ b/gcc/tree.c > @@ -2123,6 +2123,21 @@ build_constructor_from_list (tree type, tree vals) > return build_constructor (type, v); > } > > +/* Return a new CONSTRUCTOR node whose type is TYPE and whose values > + are in a vector pointed to by VALS. Note that the TREE_PURPOSE > + fields in the constructor remain null. */ > + > +tree > +build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals) > +{ > + vec<constructor_elt, va_gc> *v = NULL; > + > + for (tree t : *vals) > + CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t); > + > + return build_constructor (type, v); > +} > + > /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number > of elements, provided as index/value pairs. */ > > diff --git a/gcc/tree.h b/gcc/tree.h > index 22dd4ac0f3c..4fd51aca723 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -4407,6 +4407,7 @@ extern void verify_constructor_flags (tree); > extern tree build_constructor (tree, vec<constructor_elt, va_gc> * CXX_MEM_STAT_INFO); > extern tree build_constructor_single (tree, tree, tree); > extern tree build_constructor_from_list (tree, tree); > +extern tree build_constructor_from_vec (tree, const vec<tree, va_gc> *); > extern tree build_constructor_va (tree, int, ...); > extern tree build_clobber (tree); > extern tree build_real_from_int_cst (tree, const_tree); > > base-commit: 26ea069ec02f15593ff35cf1d5b6056c1c17e4f4 > ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-08-31 19:43 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-08-20 20:22 [PATCH] c++: Implement P1009: Array size deduction in new-expressions Marek Polacek 2020-08-21 21:04 ` Jason Merrill 2020-08-24 21:44 ` [PATCH v2] " Marek Polacek 2020-08-25 2:49 ` Jason Merrill 2020-08-25 12:26 ` [PATCH v3] " Marek Polacek 2020-08-31 19:43 ` 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).