From 2f15787f2e1a3afe2c2ad93d4eb0d3c1f73c8fbd Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 2 May 2024 09:34:31 +0200 Subject: c++: Implement C++26 P2573R2 - = delete("should have a reason"); [PR114458] The following patch implements the C++26 P2573R2 = delete("should have a reason"); paper. I've tried to avoid increasing compile time memory for it when it isn't used (e.g. by adding a new lang_decl tree member), so the reason is represented as STRING_CST in DECL_INITIAL (which normally is for DECL_DELETED_FN error_mark_node) and to differentiate this delete("reason") initializer from some bogus attempt to initialize a function with "reason" using the RID_DELETE identifier as TREE_TYPE of the STRING_CST, as nothing needs to care about the type of the reason. If preferred it could be say TREE_LIST with the reason STRING_CST and RID_DELETE identifier or something similar instead, but that would need more compile time memory when it is used. 2024-05-02 Jakub Jelinek PR c++/114458 gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Predefine __cpp_deleted_function=202403L for C++26. gcc/cp/ChangeLog * parser.cc (cp_parser_pure_specifier): Implement C++26 P2573R2 - = delete("should have a reason");. Parse deleted-function-body. * decl.cc (duplicate_decls): Copy DECL_INITIAL from DECL_DELETED_FN olddecl to newdecl if it is a STRING_CST. (cp_finish_decl): Handle deleted init with a reason. * decl2.cc: Include "escaped_string.h". (grokfield): Handle deleted init with a reason. (mark_used): Emit DECL_DELETED_FN reason in the message if any. * cp-tree.h (DECL_DELETED_FN): Document representation of = delete("reason") on a DECL. gcc/testsuite/ * g++.dg/cpp26/feat-cxx26.C (__cpp_deleted_function): Add test. * g++.dg/cpp26/delete-reason1.C: New test. * g++.dg/cpp26/delete-reason2.C: New test. * g++.dg/parse/error65.C (f1): Adjust expected diagnostics. --- gcc/cp/parser.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'gcc/cp/parser.cc') diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index a2bc6f6..7c3cfcf 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -28634,6 +28634,27 @@ cp_parser_pure_specifier (cp_parser* parser) || token->keyword == RID_DELETE) { maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED); + if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) + { + if (cxx_dialect >= cxx11 && cxx_dialect < cxx26) + pedwarn (cp_lexer_peek_token (parser->lexer)->location, + OPT_Wc__26_extensions, + "% reason only available with " + "%<-std=c++2c%> or %<-std=gnu++2c%>"); + + /* Consume the `('. */ + matching_parens parens; + parens.consume_open (parser); + tree reason = cp_parser_unevaluated_string_literal (parser); + /* Consume the `)'. */ + parens.require_close (parser); + if (TREE_CODE (reason) == STRING_CST) + { + TREE_TYPE (reason) = token->u.value; + return reason; + } + } + return token->u.value; } -- cgit v1.1