aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/parser.c
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-08-13 14:56:13 -0400
committerMarek Polacek <polacek@redhat.com>2020-08-31 16:09:10 -0400
commit73a2b8dd17dbc02c0c7e6286e90f17833aa50906 (patch)
tree9585c05c0faaa35dc21c0c1331a7681cd20ad9bd /gcc/cp/parser.c
parent0d1b4edc5fff834e8f924b20dd021ded7a21d2d2 (diff)
downloadgcc-73a2b8dd17dbc02c0c7e6286e90f17833aa50906.zip
gcc-73a2b8dd17dbc02c0c7e6286e90f17833aa50906.tar.gz
gcc-73a2b8dd17dbc02c0c7e6286e90f17833aa50906.tar.bz2
c++: Implement P1009: Array size deduction in new-expressions.
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>
Diffstat (limited to 'gcc/cp/parser.c')
-rw-r--r--gcc/cp/parser.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 147d537..9849e59 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;