diff options
author | Marek Polacek <polacek@redhat.com> | 2019-12-05 20:13:03 +0000 |
---|---|---|
committer | Marek Polacek <mpolacek@gcc.gnu.org> | 2019-12-05 20:13:03 +0000 |
commit | 7906797ebec6881d7d90165340f51efcf447d716 (patch) | |
tree | ed7a31a3d6aa39957a07685ac63429cd13bede4a /gcc/cp | |
parent | 5c04da88731961636d08c0fd06f2aa291410d5b9 (diff) | |
download | gcc-7906797ebec6881d7d90165340f51efcf447d716.zip gcc-7906797ebec6881d7d90165340f51efcf447d716.tar.gz gcc-7906797ebec6881d7d90165340f51efcf447d716.tar.bz2 |
PR c++/91353 - P1331R2: Allow trivial default init in constexpr contexts.
This patch implements C++20 P1331, allowing trivial default initialization in
constexpr contexts.
* c-cppbuiltin.c (c_cpp_builtins): Adjust the value of __cpp_constexpr.
* class.c (trivial_default_constructor_is_constexpr): Return true in
C++20.
* constexpr.c (cx_check_missing_mem_inits): Allow missing field
initializers in C++20.
(cxx_eval_call_expression): Don't clear CONSTRUCTOR_NO_CLEARING for
constexpr constructors in C++20.
(reduced_constant_expression_p): Don't set FIELD for union and array
types. Skip empty class fields without initializers.
* decl.c (check_for_uninitialized_const_var): Permit trivial default
initialization in constexpr.
(next_initializable_field): Don't skip vptr fields.
* method.c (walk_field_subobs): Still consider a constructor that
doesn't initialize all the members constexpr.
* g++.dg/cpp0x/constexpr-array6.C: Adjust dg-error.
* g++.dg/cpp0x/constexpr-ctor.C: Likewise.
* g++.dg/cpp0x/constexpr-diag3.C: Likewise.
* g++.dg/cpp0x/constexpr-diag4.C: Likewise.
* g++.dg/cpp0x/constexpr-ex3.C: Likewise.
* g++.dg/cpp0x/constexpr-template2.C: Likewise.
* g++.dg/cpp0x/constexpr-union2.C: Likewise.
* g++.dg/cpp0x/lambda/lambda-mangle.C: Rip out a piece of code ...
* g++.dg/cpp0x/lambda/lambda-mangle6.C: ... and put it here.
* g++.dg/cpp0x/pr79118.C: Adjust dg-error.
* g++.dg/cpp1y/constexpr-83921-3.C: Likewise.
* g++.dg/cpp1y/constexpr-neg1.C: Likewise.
* g++.dg/cpp1z/constexpr-lambda12.C: Likewise.
* g++.dg/cpp1z/feat-cxx1z.C: Use -std=c++17.
* g++.dg/cpp2a/constexpr-init1.C: New test.
* g++.dg/cpp2a/constexpr-init2.C: New test.
* g++.dg/cpp2a/constexpr-init3.C: New test.
* g++.dg/cpp2a/constexpr-init4.C: New test.
* g++.dg/cpp2a/constexpr-init5.C: New test.
* g++.dg/cpp2a/constexpr-init6.C: New test.
* g++.dg/cpp2a/constexpr-init7.C: New test.
* g++.dg/cpp2a/constexpr-init8.C: New test.
* g++.dg/cpp2a/constexpr-init9.C: New test.
* g++.dg/cpp2a/constexpr-init10.C: New test.
* g++.dg/cpp2a/constexpr-init11.C: New test.
* g++.dg/cpp2a/constexpr-init12.C: New test.
* g++.dg/cpp2a/constexpr-init13.C: New test.
* g++.dg/cpp2a/constexpr-init14.C: New test.
* g++.dg/cpp2a/constexpr-init15.C: New test.
* g++.dg/cpp2a/constexpr-try5.C: Adjust dg-error.
* g++.dg/cpp2a/feat-cxx2a.C: Test __cpp_constexpr.
* g++.dg/cpp2a/lambda-mangle.C: New test.
* g++.dg/debug/dwarf2/pr44641.C: Skip for c++2a.
* g++.dg/ext/stmtexpr21.C: Adjust dg-error.
Co-Authored-By: Jakub Jelinek <jakub@redhat.com>
From-SVN: r279019
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/ChangeLog | 18 | ||||
-rw-r--r-- | gcc/cp/class.c | 10 | ||||
-rw-r--r-- | gcc/cp/constexpr.c | 45 | ||||
-rw-r--r-- | gcc/cp/decl.c | 16 | ||||
-rw-r--r-- | gcc/cp/method.c | 10 |
5 files changed, 80 insertions, 19 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4e88ed8..b5c2d1b 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,4 +1,22 @@ 2019-12-05 Marek Polacek <polacek@redhat.com> + Jakub Jelinek <jakub@redhat.com> + + PR c++/91353 - P1331R2: Allow trivial default init in constexpr contexts. + * class.c (trivial_default_constructor_is_constexpr): Return true in + C++20. + * constexpr.c (cx_check_missing_mem_inits): Allow missing field + initializers in C++20. + (cxx_eval_call_expression): Don't clear CONSTRUCTOR_NO_CLEARING for + constexpr constructors in C++20. + (reduced_constant_expression_p): Don't set FIELD for union and array + types. Skip empty class fields without initializers. + * decl.c (check_for_uninitialized_const_var): Permit trivial default + initialization in constexpr. + (next_initializable_field): Don't skip vptr fields. + * method.c (walk_field_subobs): Still consider a constructor that + doesn't initialize all the members constexpr. + +2019-12-05 Marek Polacek <polacek@redhat.com> PR c++/92271 - make __is_same alias for __is_same_as. * cxx-pretty-print.c (pp_cxx_trait_expression) <case CPTK_IS_SAME_AS>: diff --git a/gcc/cp/class.c b/gcc/cp/class.c index f36f75f..d8bb449 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -5288,8 +5288,14 @@ trivial_default_constructor_is_constexpr (tree t) /* A defaulted trivial default constructor is constexpr if there is nothing to initialize. */ gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t)); - /* A class with a vptr doesn't have a trivial default ctor. */ - return is_really_empty_class (t, /*ignore_vptr*/true); + /* A class with a vptr doesn't have a trivial default ctor. + In C++20, a class can have transient uninitialized members, e.g.: + + struct S { int i; constexpr S() = default; }; + + should work. */ + return (cxx_dialect >= cxx2a + || is_really_empty_class (t, /*ignore_vptr*/true)); } /* Returns true iff class T has a constexpr default constructor. */ diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 3706334..19e09c7 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -767,6 +767,10 @@ massage_constexpr_body (tree fun, tree body) static bool cx_check_missing_mem_inits (tree ctype, tree body, bool complain) { + /* We allow uninitialized bases/fields in C++20. */ + if (cxx_dialect >= cxx2a) + return false; + unsigned nelts = 0; if (body) @@ -815,7 +819,7 @@ cx_check_missing_mem_inits (tree ctype, tree body, bool complain) continue; if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) { - /* Recurse to check the anonummous aggregate member. */ + /* Recurse to check the anonymous aggregate member. */ bad |= cx_check_missing_mem_inits (TREE_TYPE (field), NULL_TREE, complain); if (bad && !complain) @@ -2179,15 +2183,26 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, entry->result = result; } - /* The result of a constexpr function must be completely initialized. */ - if (TREE_CODE (result) == CONSTRUCTOR) + /* The result of a constexpr function must be completely initialized. + + However, in C++20, a constexpr constructor doesn't necessarily have + to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING + in order to detect reading an unitialized object in constexpr instead + of value-initializing it. (reduced_constant_expression_p is expected to + take care of clearing the flag.) */ + if (TREE_CODE (result) == CONSTRUCTOR + && (cxx_dialect < cxx2a + || !DECL_CONSTRUCTOR_P (fun))) clear_no_implicit_zero (result); pop_cx_call_context (); return result; } -/* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ +/* Return true if T is a valid constant initializer. If a CONSTRUCTOR + initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be + cleared. + FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ bool reduced_constant_expression_p (tree t) @@ -2209,6 +2224,12 @@ reduced_constant_expression_p (tree t) if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) /* An initialized vector would have a VECTOR_CST. */ return false; + else if (cxx_dialect >= cxx2a + /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */ + && (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE + /* A union only initializes one member. */ + || TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)) + field = NULL_TREE; else field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); } @@ -2222,14 +2243,20 @@ reduced_constant_expression_p (tree t) return false; if (field) { - if (idx != field) - return false; + /* Empty class field may or may not have an initializer. */ + for (; idx != field; + field = next_initializable_field (DECL_CHAIN (field))) + if (!is_really_empty_class (TREE_TYPE (field), + /*ignore_vptr*/false)) + return false; field = next_initializable_field (DECL_CHAIN (field)); } } - if (field) - return false; - else if (CONSTRUCTOR_NO_CLEARING (t)) + /* There could be a non-empty field at the end. */ + for (; field; field = next_initializable_field (DECL_CHAIN (field))) + if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) + return false; + if (CONSTRUCTOR_NO_CLEARING (t)) /* All the fields are initialized. */ CONSTRUCTOR_NO_CLEARING (t) = false; return true; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 7897327..a44f172 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -5858,8 +5858,12 @@ check_for_uninitialized_const_var (tree decl, bool constexpr_context_p, 7.1.6 */ if (VAR_P (decl) && !TYPE_REF_P (type) - && (constexpr_context_p - || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl)) + && (CP_TYPE_CONST_P (type) + /* C++20 permits trivial default initialization in constexpr + context (P1331R2). */ + || (cxx_dialect < cxx2a + && (constexpr_context_p + || var_in_constexpr_fn (decl)))) && !DECL_NONTRIVIALLY_INITIALIZED_P (decl)) { tree field = default_init_uninitialized_part (type); @@ -5868,7 +5872,7 @@ check_for_uninitialized_const_var (tree decl, bool constexpr_context_p, bool show_notes = true; - if (!constexpr_context_p) + if (!constexpr_context_p || cxx_dialect >= cxx2a) { if (CP_TYPE_CONST_P (type)) { @@ -5938,7 +5942,11 @@ next_initializable_field (tree field) && (TREE_CODE (field) != FIELD_DECL || DECL_UNNAMED_BIT_FIELD (field) || (DECL_ARTIFICIAL (field) - && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))) + /* In C++17, don't skip base class fields. */ + && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)) + /* Don't skip vptr fields. We might see them when we're + called from reduced_constant_expression_p. */ + && !DECL_VIRTUAL_P (field)))) field = DECL_CHAIN (field); return field; diff --git a/gcc/cp/method.c b/gcc/cp/method.c index a707940..d2aed47 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1985,10 +1985,12 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname, if (bad && deleted_p) *deleted_p = true; - /* For an implicitly-defined default constructor to be constexpr, - every member must have a user-provided default constructor or - an explicit initializer. */ - if (constexpr_p && !CLASS_TYPE_P (mem_type) + /* Before C++20, for an implicitly-defined default constructor to + be constexpr, every member must have a user-provided default + constructor or an explicit initializer. */ + if (constexpr_p + && cxx_dialect < cxx2a + && !CLASS_TYPE_P (mem_type) && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) { *constexpr_p = false; |