aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/decl.c
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-08-26 08:27:33 -0400
committerMarek Polacek <polacek@redhat.com>2020-09-03 14:30:06 -0400
commit753b4679bc46f6806cf45d9afc3783c6d3b63589 (patch)
tree73c4f9354bfad6c843c92c04369b8ea5d6668a8c /gcc/cp/decl.c
parent6641d6d3fe79113f8d9f3ced355aea79bffda822 (diff)
downloadgcc-753b4679bc46f6806cf45d9afc3783c6d3b63589.zip
gcc-753b4679bc46f6806cf45d9afc3783c6d3b63589.tar.gz
gcc-753b4679bc46f6806cf45d9afc3783c6d3b63589.tar.bz2
c++: Fix P0960 in member init list and array [PR92812]
This patch nails down the remaining P0960 case in PR92812: struct A { int ar[2]; A(): ar(1, 2) {} // doesn't work without this patch }; Note that when the target object is not of array type, this already works: struct S { int x, y; }; struct A { S s; A(): s(1, 2) { } // OK in C++20 }; because build_new_method_call_1 takes care of the P0960 magic. It proved to be quite hairy. When the ()-list has more than one element, we can always create a CONSTRUCTOR, because the code was previously invalid. But when the ()-list has just one element, it gets all kinds of difficult. As usual, we have to handle a("foo") so as not to wrap the STRING_CST in a CONSTRUCTOR. Always turning x(e) into x{e} would run into trouble as in c++/93790. Another issue was what to do about x({e}): previously, this would trigger "list-initializer for non-class type must not be parenthesized". I figured I'd make this work in C++20, so that given struct S { int x, y; }; you can do S a[2]; [...] A(): a({1, 2}) // initialize a[0] with {1, 2} and a[1] with {} It also turned out that, as an extension, we support compound literals: F (): m((S[1]) { 1, 2 }) so this has to keep working as before. Moreover, make sure not to trigger in compiler-generated code, like =default, where array assignment is allowed. I've factored out a function that turns a TREE_LIST into a CONSTRUCTOR to simplify handling of P0960. paren-init35.C also tests this with vector types. gcc/cp/ChangeLog: PR c++/92812 * cp-tree.h (do_aggregate_paren_init): Declare. * decl.c (do_aggregate_paren_init): New. (grok_reference_init): Use it. (check_initializer): Likewise. * init.c (perform_member_init): Handle initializing an array from a ()-list. Use do_aggregate_paren_init. gcc/testsuite/ChangeLog: PR c++/92812 * g++.dg/cpp0x/constexpr-array23.C: Adjust dg-error. * g++.dg/cpp0x/initlist69.C: Likewise. * g++.dg/diagnostic/mem-init1.C: Likewise. * g++.dg/init/array28.C: Likewise. * g++.dg/cpp2a/paren-init33.C: New test. * g++.dg/cpp2a/paren-init34.C: New test. * g++.dg/cpp2a/paren-init35.C: New test. * g++.old-deja/g++.brendan/crash60.C: Adjust dg-error. * g++.old-deja/g++.law/init10.C: Likewise. * g++.old-deja/g++.other/array3.C: Likewise.
Diffstat (limited to 'gcc/cp/decl.c')
-rw-r--r--gcc/cp/decl.c62
1 files changed, 33 insertions, 29 deletions
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 4c84f2d..31d6874 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5557,6 +5557,37 @@ start_decl_1 (tree decl, bool initialized)
maybe_push_cleanup_level (type);
}
+/* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
+ C++20 P0960. TYPE is the type of the object we're initializing. */
+
+tree
+do_aggregate_paren_init (tree init, tree type)
+{
+ tree val = TREE_VALUE (init);
+
+ if (TREE_CHAIN (init) == NULL_TREE)
+ {
+ /* If the list has a single element and it's a string literal,
+ then it's the initializer for the array as a whole. */
+ if (TREE_CODE (type) == ARRAY_TYPE
+ && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
+ && TREE_CODE (tree_strip_any_location_wrapper (val))
+ == STRING_CST)
+ return val;
+ /* Handle non-standard extensions like compound literals. This also
+ prevents triggering aggregate parenthesized-initialization in
+ compiler-generated code for =default. */
+ else if (same_type_ignoring_top_level_qualifiers_p (type,
+ TREE_TYPE (val)))
+ return val;
+ }
+
+ init = build_constructor_from_list (init_list_type_node, init);
+ CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
+ CONSTRUCTOR_IS_PAREN_INIT (init) = true;
+ return init;
+}
+
/* Handle initialization of references. DECL, TYPE, and INIT have the
same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
but will be set to a new CLEANUP_STMT if a temporary is created
@@ -5604,11 +5635,7 @@ grok_reference_init (tree decl, tree type, tree init, int flags)
/* If the list had more than one element, the code is ill-formed
pre-C++20, so we can build a constructor right away. */
else
- {
- init = build_constructor_from_list (init_list_type_node, init);
- CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
- CONSTRUCTOR_IS_PAREN_INIT (init) = true;
- }
+ init = do_aggregate_paren_init (init, ttype);
}
else
init = build_x_compound_expr_from_list (init, ELK_INIT,
@@ -6794,30 +6821,7 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
&& TREE_CODE (type) == ARRAY_TYPE
&& !DECL_DECOMPOSITION_P (decl)
&& (cxx_dialect >= cxx20))
- {
- /* [dcl.init.string] "An array of ordinary character type [...]
- can be initialized by an ordinary string literal [...] by an
- appropriately-typed string literal enclosed in braces" only
- talks about braces, but GCC has always accepted
-
- char a[]("foobar");
-
- so we continue to do so. */
- tree val = TREE_VALUE (init);
- if (TREE_CHAIN (init) == NULL_TREE
- && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
- && TREE_CODE (tree_strip_any_location_wrapper (val))
- == STRING_CST)
- /* If the list has a single element and it's a string literal,
- then it's the initializer for the array as a whole. */
- init = val;
- else
- {
- init = build_constructor_from_list (init_list_type_node, init);
- CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
- CONSTRUCTOR_IS_PAREN_INIT (init) = true;
- }
- }
+ init = do_aggregate_paren_init (init, type);
else if (TREE_CODE (init) == TREE_LIST
&& TREE_TYPE (init) != unknown_type_node
&& !MAYBE_CLASS_TYPE_P (type))