aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/cp-tree.h
AgeCommit message (Collapse)AuthorFilesLines
2022-10-07c++: track whether we expect a TARGET_EXPR to be elidedJason Merrill1-0/+11
A discussion at Cauldron made me think that with the formalization of copy elision in C++17, we should be able to determine before optimization which TARGET_EXPRs will become temporaries and which are initializers. This patch implements that: we set TARGET_EXPR_ELIDING_P if it's used as an initializer, and later check that we were right. There's an exception in the cp_gimplify_expr check to allow extra temporaries of non-addressable type: this is used by gimplify_init_ctor_preeval to materialize subobjects of a CONSTRUCTOR on the rhs of a MODIFY_EXPR rather than materializing the whole object. If the type isn't addressable, there's no way for a program to tell the difference, so this is a valid optimization. I considered changing replace_placeholders_for_class_temp_r to check TARGET_EXPR_ELIDING_P instead of potential_prvalue_result_of, but decided that would be wrong: if we have an eliding TARGET_EXPR inside a non-eliding one, we would miss replacing its placeholders. gcc/cp/ChangeLog: * cp-tree.h (TARGET_EXPR_ELIDING_P): New. (unsafe_copy_elision_p, set_target_expr_eliding) (cp_build_init_expr): Declare. * call.cc (unsafe_copy_elision_p): No longer static. (build_over_call, build_special_member_call) (build_new_method_call): Use cp_build_init_expr. * coroutines.cc (expand_one_await_expression) (build_actor_fn, flatten_await_stmt, handle_nested_conditionals) (await_statement_walker, morph_fn_to_coro): Use cp_build_init_expr. * cp-gimplify.cc (cp_gimplify_init_expr) (cp_gimplify_expr): Check TARGET_EXPR_ELIDING_P. (cp_fold_r): Propagate it. (cp_fold): Use cp_build_init_expr. * decl.cc (check_initializer): Use cp_build_init_expr. * except.cc (build_throw): Use cp_build_init_expr. * init.cc (get_nsdmi): Call set_target_expr_eliding. (perform_member_init, expand_default_init, expand_aggr_init_1) (build_new_1, build_vec_init): Use cp_build_init_expr. * method.cc (do_build_copy_constructor): Use cp_build_init_expr. * semantics.cc (simplify_aggr_init_expr, finalize_nrv_r) (finish_omp_reduction_clause): Use cp_build_init_expr. * tree.cc (build_target_expr): Call set_target_expr_eliding. (bot_manip): Copy TARGET_EXPR_ELIDING_P. * typeck.cc (cp_build_modify_expr): Call set_target_expr_eliding. (check_return_expr): Use cp_build_modify_expr. * typeck2.cc (split_nonconstant_init_1) (split_nonconstant_init): Use cp_build_init_expr. (massage_init_elt): Call set_target_expr_eliding. (process_init_constructor_record): Clear TARGET_EXPR_ELIDING_P on unsafe copy elision. (set_target_expr_eliding, cp_build_init_expr): New.
2022-10-07c++: fixes for derived-to-base reference binding [PR107085]Marek Polacek1-1/+1
This PR reports that struct Base {}; struct Derived : Base {}; static_assert(__reference_constructs_from_temporary(Base const&, Derived)); doesn't pass, which it should: it's just like const Base& b(Derived{}); where we bind 'b' to the Base subobject of a temporary object of type Derived. The ck_base conversion didn't have ->need_temporary_p set because we didn't need to create a temporary object just for the base, but the whole object is a temporary so we're still binding to a temporary. Since the Base subobject is an xvalue, a new function is introduced. PR c++/107085 gcc/cp/ChangeLog: * call.cc (conv_binds_ref_to_temporary): New. (ref_conv_binds_directly): Rename to... (ref_conv_binds_to_temporary): ...this. Use conv_binds_ref_to_temporary. * cp-tree.h (ref_conv_binds_directly): Rename to... (ref_conv_binds_to_temporary): ...this. * method.cc (ref_xes_from_temporary): Use ref_conv_binds_to_temporary. * parser.cc (warn_for_range_copy): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/reference_constructs_from_temporary1.C: Adjust expected result. * g++.dg/ext/reference_converts_from_temporary1.C: Likewise. * g++.dg/cpp0x/elision4.C: New test.
2022-10-06c++, c: Implement C++23 P1774R8 - Portable assumptions [PR106654]Jakub Jelinek1-0/+4
The following patch implements C++23 P1774R8 - Portable assumptions paper, by introducing support for [[assume (cond)]]; attribute for C++. In addition to that the patch adds [[gnu::assume (cond)]]; and __attribute__((assume (cond))); support to both C and C++. As described in C++23, the attribute argument is conditional-expression rather than the usual assignment-expression for attribute arguments, the condition is contextually converted to bool (for C truthvalue conversion is done on it) and is never evaluated at runtime. For C++ constant expression evaluation, I only check the simplest conditions for undefined behavior, because otherwise I'd need to undo changes to *ctx->global which happened during the evaluation (but I believe the spec allows that and we can further improve later). The patch uses a new internal function, .ASSUME, to hold the condition in the FEs. At gimplification time, if the condition is simple/without side-effects, it is gimplified as if (cond) ; else __builtin_unreachable (); and otherwise for now dropped on the floor. The intent is to incrementally outline the conditions into separate artificial functions and use .ASSUME further to tell the ranger and perhaps other optimization passes about the assumptions, as detailed in the PR. When implementing it, I found that assume entry hasn't been added to https://eel.is/c++draft/cpp.cond#6 Jonathan said he'll file a NB comment about it, this patch assumes it has been added into the table as 202207L when the paper has been voted in. With the attributes for both C/C++, I'd say we don't need to add __builtin_assume with similar purpose, especially when __builtin_assume in LLVM is just weird. It is strange for side-effects in function call's argument not to be evaluated, and LLVM in that case (annoyingly) warns and ignores the side-effects (but doesn't do then anything with it), if there are no side-effects, it will work like our if (!cond) __builtin_unreachable (); 2022-10-06 Jakub Jelinek <jakub@redhat.com> PR c++/106654 gcc/ * internal-fn.def (ASSUME): New internal function. * internal-fn.h (expand_ASSUME): Declare. * internal-fn.cc (expand_ASSUME): Define. * gimplify.cc (gimplify_call_expr): Gimplify IFN_ASSUME. * fold-const.h (simple_condition_p): Declare. * fold-const.cc (simple_operand_p_2): Rename to ... (simple_condition_p): ... this. Remove forward declaration. No longer static. Adjust function comment and fix a typo in it. Adjust recursive call. (simple_operand_p): Adjust function comment. (fold_truth_andor): Adjust simple_operand_p_2 callers to call simple_condition_p. * doc/extend.texi: Document assume attribute. Move fallthrough attribute example to its section. gcc/c-family/ * c-attribs.cc (handle_assume_attribute): New function. (c_common_attribute_table): Add entry for assume attribute. * c-lex.cc (c_common_has_attribute): Handle __have_cpp_attribute (assume). gcc/c/ * c-parser.cc (handle_assume_attribute): New function. (c_parser_declaration_or_fndef): Handle assume attribute. (c_parser_attribute_arguments): Add assume_attr argument, if true, parse first argument as conditional expression. (c_parser_gnu_attribute, c_parser_std_attribute): Adjust c_parser_attribute_arguments callers. (c_parser_statement_after_labels) <case RID_ATTRIBUTE>: Handle assume attribute. gcc/cp/ * cp-tree.h (process_stmt_assume_attribute): Implement C++23 P1774R8 - Portable assumptions. Declare. (diagnose_failing_condition): Declare. (find_failing_clause): Likewise. * parser.cc (assume_attr): New enumerator. (cp_parser_parenthesized_expression_list): Handle assume_attr. Remove identifier variable, for id_attr push the identifier into expression_list right away instead of inserting it before all the others at the end. (cp_parser_conditional_expression): New function. (cp_parser_constant_expression): Use it. (cp_parser_statement): Handle assume attribute. (cp_parser_expression_statement): Likewise. (cp_parser_gnu_attribute_list): Use assume_attr for assume attribute. (cp_parser_std_attribute): Likewise. Handle standard assume attribute like gnu::assume. * cp-gimplify.cc (process_stmt_assume_attribute): New function. * constexpr.cc: Include fold-const.h. (find_failing_clause_r, find_failing_clause): New functions, moved from semantics.cc with ctx argument added and if non-NULL, call cxx_eval_constant_expression rather than fold_non_dependent_expr. (cxx_eval_internal_function): Handle IFN_ASSUME. (potential_constant_expression_1): Likewise. * pt.cc (tsubst_copy_and_build): Likewise. * semantics.cc (diagnose_failing_condition): New function. (find_failing_clause_r, find_failing_clause): Moved to constexpr.cc. (finish_static_assert): Use it. Add auto_diagnostic_group. gcc/testsuite/ * gcc.dg/attr-assume-1.c: New test. * gcc.dg/attr-assume-2.c: New test. * gcc.dg/attr-assume-3.c: New test. * g++.dg/cpp2a/feat-cxx2a.C: Add colon to C++20 features comment, add C++20 attributes comment and move C++20 new features after the attributes before them. * g++.dg/cpp23/feat-cxx2b.C: Likewise. Test __has_cpp_attribute(assume). * g++.dg/cpp23/attr-assume1.C: New test. * g++.dg/cpp23/attr-assume2.C: New test. * g++.dg/cpp23/attr-assume3.C: New test. * g++.dg/cpp23/attr-assume4.C: New test.
2022-10-04openmp: Add begin declare target supportJakub Jelinek1-4/+5
The following patch adds support for the begin declare target construct, which is another spelling for declare target construct without clauses (where it needs paired end declare target), but unlike that one accepts clauses. This is an OpenMP 5.1 feature, implemented with 5.2 clarification because in 5.1 we had a restriction in the declare target chapter shared by declare target and begin declare target that if there are any clauses specified at least one of them needs to be to or link. But that was of course meant just for declare target and not begin declare target, because begin declare target doesn't even allow to/link/enter clauses. In addition to that, the patch also makes device_type clause duplication an error (as stated in 5.1) and similarly makes declare target with just device_type clause an error rather than warning. What this patch doesn't do is: 1) OpenMP 5.1 also added an indirect clause, we don't support that neither on declare target nor begin declare target and I couldn't find it in our features pages (neither libgomp.texi nor web) 2) I think device_type(nohost)/device_type(host) support can't work for variables (in 5.0 it only talked about procedures so this could be also thought as 5.1 feature that we should just add to the list and implement) 3) I don't see any use of the "omp declare target nohost" attribute, so I'm not sure if device_type(nohost) works at all 2022-10-04 Jakub Jelinek <jakub@redhat.com> gcc/c-family/ * c-omp.cc (c_omp_directives): Uncomment begin declare target entry. gcc/c/ * c-lang.h (struct c_omp_declare_target_attr): New type. (current_omp_declare_target_attribute): Change type from int to vec<c_omp_declare_target_attr, va_gc> *. * c-parser.cc (c_parser_translation_unit): Adjust for that change. If last pushed directive was begin declare target, use different wording and simplify format strings for easier translations. (c_parser_omp_clause_device_type): Uncomment check_no_duplicate_clause call. (c_parser_omp_declare_target): Adjust for the current_omp_declare_target_attribute type change, push { -1 }. Use error_at rather than warning_at for declare target with only device_type clauses. (OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK): Define. (c_parser_omp_begin): Add begin declare target support. (c_parser_omp_end): Adjust for the current_omp_declare_target_attribute type change, adjust diagnostics wording and simplify format strings for easier translations. * c-decl.cc (current_omp_declare_target_attribute): Change type from int to vec<c_omp_declare_target_attr, va_gc> *. (c_decl_attributes): Adjust for the current_omp_declare_target_attribute type change. If device_type was present on begin declare target, add "omp declare target host" and/or "omp declare target nohost" attributes. gcc/cp/ * cp-tree.h (struct omp_declare_target_attr): Rename to ... (cp_omp_declare_target_attr): ... this. Add device_type member. (omp_begin_assumes_data): Rename to ... (cp_omp_begin_assumes_data): ... this. (struct saved_scope): Change types of omp_declare_target_attribute and omp_begin_assumes. * parser.cc (cp_parser_omp_clause_device_type): Uncomment check_no_duplicate_clause call. (cp_parser_omp_all_clauses): Fix up pasto, c_name for OMP_CLAUSE_LINK should be "link" rather than "to". (cp_parser_omp_declare_target): Adjust for omp_declare_target_attr to cp_omp_declare_target_attr changes, push -1 as device_type. Use error_at rather than warning_at for declare target with only device_type clauses. (OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK): Define. (cp_parser_omp_begin): Add begin declare target support. Adjust for omp_begin_assumes_data to cp_omp_begin_assumes_data change. (cp_parser_omp_end): Adjust for the omp_declare_target_attr to cp_omp_declare_target_attr and omp_begin_assumes_data to cp_omp_begin_assumes_data type changes, adjust diagnostics wording and simplify format strings for easier translations. * semantics.cc (finish_translation_unit): Likewise. * decl2.cc (cplus_decl_attributes): If device_type was present on begin declare target, add "omp declare target host" and/or "omp declare target nohost" attributes. gcc/testsuite/ * c-c++-common/gomp/declare-target-4.c: Move tests that are now rejected into declare-target-7.c. * c-c++-common/gomp/declare-target-6.c: Adjust expected diagnostics. * c-c++-common/gomp/declare-target-7.c: New test. * c-c++-common/gomp/begin-declare-target-1.c: New test. * c-c++-common/gomp/begin-declare-target-2.c: New test. * c-c++-common/gomp/begin-declare-target-3.c: New test. * c-c++-common/gomp/begin-declare-target-4.c: New test. * g++.dg/gomp/attrs-9.C: Add begin declare target tests. * g++.dg/gomp/attrs-18.C: New test. libgomp/ * libgomp.texi (Support begin/end declare target syntax in C/C++): Mark as implemented.
2022-10-03c++: Disallow jumps into statement expressionsJakub Jelinek1-1/+2
On Fri, Sep 30, 2022 at 04:39:25PM -0400, Jason Merrill wrote: > > --- gcc/cp/decl.cc.jj 2022-09-22 00:14:55.478599363 +0200 > > +++ gcc/cp/decl.cc 2022-09-22 00:24:01.121178256 +0200 > > @@ -223,6 +223,7 @@ struct GTY((for_user)) named_label_entry > > bool in_transaction_scope; > > bool in_constexpr_if; > > bool in_consteval_if; > > + bool in_assume; > > I think it would be better to reject jumps into statement-expressions like > the C front-end. Ok, here is a self-contained patch that does that. 2022-10-03 Jakub Jelinek <jakub@redhat.com> * cp-tree.h (BCS_STMT_EXPR): New enumerator. * name-lookup.h (enum scope_kind): Add sk_stmt_expr. * name-lookup.cc (begin_scope): Handle sk_stmt_expr like sk_block. * semantics.cc (begin_compound_stmt): For BCS_STMT_EXPR use sk_stmt_expr. * parser.cc (cp_parser_statement_expr): Use BCS_STMT_EXPR instead of BCS_NORMAL. * decl.cc (struct named_label_entry): Add in_stmt_expr. (poplevel_named_label_1): Handle sk_stmt_expr. (check_previous_goto_1): Diagnose entering of statement expression. (check_goto): Likewise. * g++.dg/ext/stmtexpr24.C: New test.
2022-09-30c++: streamline built-in trait addition processPatrick Palka1-42/+4
Adding a new built-in trait currently involves manual boilerplate consisting of defining an rid enumerator for the identifier as well as a corresponding cp_trait_kind enumerator and handling them in various switch statements, the exact set of which depends on whether the proposed trait yields (and thus is recognized as) a type or an expression. To streamline the process, this patch adds a central cp-trait.def file that tabulates the essential details about each built-in trait (whether it yields a type or an expression, its code, its spelling and its arity) and uses this file to automate away the manual boilerplate. It also migrates all the existing C++-specific built-in traits to use this approach. After this change, adding a new built-in trait just entails declaring it in cp-trait.def and defining its behavior in finish_trait_expr/type (and handling it in diagnose_trait_expr, if it's an expression-yielding trait). gcc/c-family/ChangeLog: * c-common.cc (c_common_reswords): Use cp/cp-trait.def to handle C++ traits. * c-common.h (enum rid): Likewise. gcc/cp/ChangeLog: * constraint.cc (diagnose_trait_expr): Likewise. * cp-objcp-common.cc (names_builtin_p): Likewise. * cp-tree.h (enum cp_trait_kind): Likewise. * cxx-pretty-print.cc (pp_cxx_trait): Likewise. * parser.cc (cp_keyword_starts_decl_specifier_p): Likewise. (cp_parser_primary_expression): Likewise. (cp_parser_trait): Likewise. (cp_parser_simple_type_specifier): Likewise. * cp-trait.def: New file.
2022-09-29c++: reduce redundant TARGET_EXPRJason Merrill1-1/+2
An experiment led me to notice that in some cases we were ending up with TARGET_EXPR initialized by TARGET_EXPR, which isn't useful. The target_expr_needs_replace change won't make a difference in most cases, since cp_genericize_init will have already expanded VEC_INIT_EXPR by the time we consider it, but it is correct. gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold_r) [TARGET_EXPR]: Collapse TARGET_EXPR within TARGET_EXPR. * constexpr.cc (cxx_eval_outermost_constant_expr): Avoid adding redundant TARGET_EXPR. * cp-tree.h (target_expr_needs_replace): VEC_INIT_EXPR doesn't.
2022-09-29c++: implement __remove_cv, __remove_reference and __remove_cvrefPatrick Palka1-1/+4
This implements builtins for std::remove_cv, std::remove_reference and std::remove_cvref using TRAIT_TYPE from the previous patch. gcc/c-family/ChangeLog: * c-common.cc (c_common_reswords): Add __remove_cv, __remove_reference and __remove_cvref. * c-common.h (enum rid): Add RID_REMOVE_CV, RID_REMOVE_REFERENCE and RID_REMOVE_CVREF. gcc/cp/ChangeLog: * constraint.cc (diagnose_trait_expr): Handle CPTK_REMOVE_CV, CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF. * cp-objcp-common.cc (names_builtin_p): Likewise. * cp-tree.h (enum cp_trait_kind): Add CPTK_REMOVE_CV, CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF. * cxx-pretty-print.cc (pp_cxx_trait): Handle CPTK_REMOVE_CV, CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF. * parser.cc (cp_keyword_starts_decl_specifier_p): Return true for RID_REMOVE_CV, RID_REMOVE_REFERENCE and RID_REMOVE_CVREF. (cp_parser_trait): Handle RID_REMOVE_CV, RID_REMOVE_REFERENCE and RID_REMOVE_CVREF. (cp_parser_simple_type_specifier): Likewise. * semantics.cc (finish_trait_type): Likewise. libstdc++-v3/ChangeLog: * include/bits/unique_ptr.h (unique_ptr<_Tp[], _Dp>): Remove __remove_cv and use __remove_cv_t instead. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __remove_cv, __remove_reference and __remove_cvref. * g++.dg/ext/remove_cv.C: New test. * g++.dg/ext/remove_reference.C: New test. * g++.dg/ext/remove_cvref.C: New test.
2022-09-29c++: introduce TRAIT_TYPE alongside TRAIT_EXPRPatrick Palka1-0/+18
We already have generic support for predicate-like traits that yield a boolean value via TRAIT_EXPR, but we lack the same support for traits that yield a type instead of a value. Such support would streamline implementing efficient builtins for the standard library type traits. To that end this patch implements a generic TRAIT_TYPE type alongside TRAIT_EXPR, and reimplements the existing UNDERLYING_TYPE builtin trait using this new TRAIT_TYPE. gcc/cp/ChangeLog: * cp-objcp-common.cc (cp_common_init_ts): Replace UNDERLYING_TYPE with TRAIT_TYPE. * cp-tree.def (TRAIT_TYPE): Define. (UNDERLYING_TYPE): Remove. * cp-tree.h (TRAIT_TYPE_KIND_RAW): Define. (TRAIT_TYPE_KIND): Define. (TRAIT_TYPE_TYPE1): Define. (TRAIT_TYPE_TYPE2): Define. (WILDCARD_TYPE_P): Return true for TRAIT_TYPE. (finish_trait_type): Declare. * cxx-pretty-print.cc (cxx_pretty_printer::primary_expression): Adjust after renaming pp_cxx_trait_expression. (cxx_pretty_printer::simple_type_specifier) <case TRAIT_TYPE>: New. (cxx_pretty_printer::type_id): Replace UNDERLYING_TYPE with TRAIT_TYPE. (pp_cxx_trait_expression): Rename to ... (pp_cxx_trait): ... this. Handle TRAIT_TYPE as well. Correct pretty printing of the trailing arguments. * cxx-pretty-print.h (pp_cxx_trait_expression): Rename to ... (pp_cxx_trait_type): ... this. * error.cc (dump_type) <case UNDERLYING_TYPE>: Remove. <case TRAIT_TYPE>: New. (dump_type_prefix): Replace UNDERLYING_WITH with TRAIT_TYPE. (dump_type_suffix): Likewise. * mangle.cc (write_type) <case UNDERLYING_TYPE>: Remove. <case TRAIT_TYPE>: New. * module.cc (trees_out::type_node) <case UNDERLYING_TYPE>: Remove. <case TRAIT_TYPE>: New. (trees_in::tree_node): Likewise. * parser.cc (cp_parser_primary_expression): Adjust after renaming cp_parser_trait_expr. (cp_parser_trait_expr): Rename to ... (cp_parser_trait): ... this. Call finish_trait_type for traits that yield a type. (cp_parser_simple_type_specifier): Adjust after renaming cp_parser_trait_expr. * pt.cc (for_each_template_parm_r) <case UNDERLYING_TYPE>: Remove. <case TRAIT_TYPE>: New. (tsubst): Likewise. (unify): Replace UNDERLYING_TYPE with TRAIT_TYPE. (dependent_type_p_r): Likewise. * semantics.cc (finish_underlying_type): Don't return UNDERLYING_TYPE anymore when processing_template_decl. (finish_trait_type): Define. * tree.cc (strip_typedefs) <case UNDERLYING_TYPE>: Remove. <case TRAIT_TYPE>: New. (cp_walk_subtrees): Likewise. * typeck.cc (structural_comptypes): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-59.C: Adjust expected error message. * g++.dg/ext/underlying_type7.C: Likewise. * g++.dg/ext/underlying_type13.C: New test. * g++.dg/ext/underlying_type14.C: New test.
2022-09-28c++: Add DECL_NTTP_OBJECT_P lang flagNathan Sidwell1-1/+6
VAR_DECLs for NTTPs need to be handled specially by module streaming, in the same manner to type info decls. This reworks their handling to allow that work to drop in. We use DECL_LANG_FLAG_5 to indicate such decls (I didn't notice template_parm_object_p, which looks at the mangled name -- anyway a bit flag on the node is better, IMHO). We break apart the creation routine, so there's now an entry point the module machinery can use directly. gcc/cp/ * cp-tree.h (DECL_NTTP_OBJECT_P): New. (template_parm_object_p): Delete. (build_template_parm_object): Declare. * cxx-pretty-print.cc (pp_cx_template_argument_list): Use DECL_NTTP_OBJECT_P. * error.cc (dump_simple_decl): Likewise. * mangle.cc (write_template_arg): Likewise. * pt.cc (template_parm_object_p): Delete. (create_template_parm_object): Separated out checking from ... (get_template_parm_object): ... this, new external entry point.
2022-09-27c++: Implement C++23 P2266R1, Simpler implicit move [PR101165]Marek Polacek1-0/+1
This patch implements https://wg21.link/p2266, which, once again, changes the implicit move rules. Here's a brief summary of various changes in this area: r125211: Introduced moving from certain lvalues when returning them r171071: CWG 1148, enable move from value parameter on return r212099: CWG 1579, it's OK to call a converting ctor taking an rvalue r251035: CWG 1579, do maybe-rvalue overload resolution twice r11-2411: Avoid calling const copy ctor on implicit move r11-2412: C++20 implicit move changes, remove the fallback overload resolution, allow move on throw of parameters and implicit move of rvalue references P2266 enables the implicit move even for functions that return references. That is, we will now perform a move in X&& foo (X&& x) { return x; } P2266 also removes the fallback overload resolution, but this was resolved by r11-2412: we only do convert_for_initialization with LOOKUP_PREFER_RVALUE in C++17 and older. P2266 also says that a returned move-eligible id-expression is always an xvalue. This required some further short, but nontrivial changes, especially when it comes to deduction, because we have to pay attention to whether we have auto, auto&& (which is like T&&), or decltype(auto) with (un)parenthesized argument. In C++23, decltype(auto) f(int&& x) { return (x); } auto&& f(int x) { return x; } both should deduce to 'int&&' but decltype(auto) f(int x) { return x; } should deduce to 'int'. A cornucopia of tests attached. I've also verified that we behave like clang++. xvalue_p seemed to be broken: since the introduction of clk_implicit_rval, it cannot use '==' when checking for clk_rvalueref. Since this change breaks code, it's only enabled in C++23. In particular, this code will not compile in C++23: int& g(int&& x) { return x; } because x is now treated as an rvalue, and you can't bind a non-const lvalue reference to an rvalue. This patch also fixes PR106882 (the check_return_expr changes). PR c++/101165 PR c++/106882 gcc/c-family/ChangeLog: * c-cppbuiltin.cc (c_cpp_builtins): Define __cpp_implicit_move. gcc/cp/ChangeLog: * call.cc (reference_binding): Check clk_implicit_rval in C++20 only. * cp-tree.h (unparenthesized_id_or_class_member_access_p): Declare. * pt.cc (unparenthesized_id_or_class_member_access_p): New function, broken out of... (do_auto_deduction): ...here. Use it. In C++23, maybe call treat_lvalue_as_rvalue_p. * tree.cc (xvalue_p): Check & clk_rvalueref, not == clk_rvalueref. * typeck.cc (check_return_expr): Allow implicit move for functions returning a reference as well, or when the return value type is not a scalar type. gcc/testsuite/ChangeLog: * g++.dg/conversion/pr41426.C: Add dg-error for C++23. * g++.dg/cpp0x/elision_weak.C: Likewise. * g++.dg/cpp0x/move-return3.C: Only link in c++20_down. * g++.dg/cpp1y/decltype-auto2.C: Add dg-error for C++23. * g++.dg/cpp1y/lambda-generic-89419.C: Likewise. * g++.dg/cpp23/feat-cxx2b.C: Test __cpp_implicit_move. * g++.dg/gomp/pr56217.C: Only compile in c++20_down. * g++.dg/warn/Wno-return-local-addr.C: Add dg-error for C++23. * g++.dg/warn/Wreturn-local-addr.C: Adjust dg-error. * g++.old-deja/g++.brendan/crash55.C: Add dg-error for C++23. * g++.old-deja/g++.jason/temporary2.C: Likewise. * g++.old-deja/g++.mike/p2846b.C: Adjust. * g++.dg/cpp1y/decltype-auto6.C: New test. * g++.dg/cpp23/decltype1.C: New test. * g++.dg/cpp23/decltype2.C: New test. * g++.dg/cpp23/elision1.C: New test. * g++.dg/cpp23/elision2.C: New test. * g++.dg/cpp23/elision3.C: New test. * g++.dg/cpp23/elision4.C: New test. * g++.dg/cpp23/elision5.C: New test. * g++.dg/cpp23/elision6.C: New test. * g++.dg/cpp23/elision7.C: New test.
2022-09-27c++: Implement C++23 P1169R4 - static operator() [PR106651]Jakub Jelinek1-0/+5
The following patch attempts to implement C++23 P1169R4 - static operator() paper's compiler side (there is some small library side too not implemented yet). This allows static members as user operator() declarations and static specifier on lambdas without lambda capture. The synthetized conversion operator changes for static lambdas as it can just return the operator() static method address, doesn't need to create a thunk for it. The change in call.cc (joust) is to avoid ICEs because we assumed that len could be different only if both candidates are direct calls but it can be one direct and one indirect call, and to implement the [over.match.best.general]/1 and [over.best.ics.general] changes from the paper (implemented always as Jason is sure it doesn't make a difference in C++20 and earlier unless static member function operator() or static lambda which we accept with pedwarn in earlier standards too appears and my testing confirmed that). 2022-09-27 Jakub Jelinek <jakub@redhat.com> PR c++/106651 gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Predefine __cpp_static_call_operator=202207L for C++23. gcc/cp/ * cp-tree.h (LAMBDA_EXPR_STATIC_P): Implement C++23 P1169R4 - static operator(). Define. * parser.cc (CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR): Document that it also allows static. (cp_parser_lambda_declarator_opt): Handle static lambda specifier. (cp_parser_decl_specifier_seq): Allow RID_STATIC for CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR. * decl.cc (grok_op_properties): If operator() isn't a method, use a different error wording, if it is static member function, allow it (for C++20 and older with a pedwarn unless it is a lambda function or template instantiation). * call.cc (joust): Don't ICE if one candidate is static member function and the other is an indirect call. If the parameter conversion on the other candidate is user defined conversion, ellipsis or bad conversion, make static member function candidate a winner for that parameter. * lambda.cc (maybe_add_lambda_conv_op): Handle static lambdas. * error.cc (dump_lambda_function): Print static for static lambdas. gcc/testsuite/ * g++.dg/template/error30.C: Adjust expected diagnostics. * g++.dg/cpp1z/constexpr-lambda13.C: Likewise. * g++.dg/cpp23/feat-cxx2b.C: Test __cpp_static_call_operator. * g++.dg/cpp23/static-operator-call1.C: New test. * g++.dg/cpp23/static-operator-call2.C: New test. * g++.old-deja/g++.jason/operator.C: Adjust expected diagnostics.
2022-09-27openmp: Add OpenMP assume, assumes and begin/end assumes supportJakub Jelinek1-0/+5
The following patch implements OpenMP 5.1 #pragma omp assume #pragma omp assumes and #pragma omp begin assumes #pragma omp end assumes directive support for C and C++. Currently it doesn't remember anything from the assumption clauses for later, so is mainly to support the directives and diagnose errors in their use. If the recently posted C++23 [[assume (cond)]]; support makes it in, the intent is that this can be easily adjusted at least for the #pragma omp assume directive with holds clause(s) to use the same infrastructure. Now, C++23 portable assumptions are slightly different from OpenMP 5.1 assumptions' holds clause in that C++23 assumption holds just where it appears, while OpenMP 5.1 assumptions hold everywhere in the scope of the directive. For assumes directive which can appear at file or namespace scope it is the whole TU and everything that functions from there call at runtime, for begin assumes/end assumes pair all the functions in between those directives and everything they call and for assume directive the associated (currently structured) block. I have no idea how to represents such holds to be usable for optimizers, except to make #pragma omp assume holds (cond) block; expand essentially to [[assume (cond)]]; block; or [[assume (cond)]]; block; [[assume (cond)]]; for now. Except for holds clause, the other assumptions are OpenMP related, I'd say we should brainstorm where it would be useful to optimize based on such information (I guess e.g. in target regions it easily could) and only when we come up with something like that think about how to propagate the assumptions to the optimizers. 2022-09-27 Jakub Jelinek <jakub@redhat.com> gcc/c-family/ * c-pragma.h (enum pragma_kind): Add PRAGMA_OMP_ASSUME, PRAGMA_OMP_ASSUMES and PRAGMA_OMP_BEGIN. Rename PRAGMA_OMP_END_DECLARE_TARGET to PRAGMA_OMP_END. * c-pragma.cc (omp_pragmas): Add assumes and begin. For end rename PRAGMA_OMP_END_DECLARE_TARGET to PRAGMA_OMP_END. (omp_pragmas_simd): Add assume. * c-common.h (c_omp_directives): Declare. * c-omp.cc (omp_directives): Rename to ... (c_omp_directives): ... this. No longer static. Uncomment assume, assumes, begin assumes and end assumes entries. In end declare target entry rename PRAGMA_OMP_END_DECLARE_TARGET to PRAGMA_OMP_END. (c_omp_categorize_directive): Adjust for omp_directives to c_omp_directives renaming. gcc/c/ * c-lang.h (current_omp_begin_assumes): Declare. * c-parser.cc: Include bitmap.h. (c_parser_omp_end_declare_target): Rename to ... (c_parser_omp_end): ... this. Handle also end assumes. (c_parser_omp_begin, c_parser_omp_assumption_clauses, c_parser_omp_assumes, c_parser_omp_assume): New functions. (c_parser_translation_unit): Also diagnose #pragma omp begin assumes without corresponding #pragma omp end assumes. (c_parser_pragma): Use %s in may only be used at file scope diagnostics to decrease number of translatable messages. Handle PRAGMA_OMP_BEGIN and PRAGMA_OMP_ASSUMES. Handle PRAGMA_OMP_END rather than PRAGMA_OMP_END_DECLARE_TARGET and call c_parser_omp_end for it rather than c_parser_omp_end_declare_target. (c_parser_omp_construct): Handle PRAGMA_OMP_ASSUME. * c-decl.cc (current_omp_begin_assumes): Define. gcc/cp/ * cp-tree.h (struct omp_begin_assumes_data): New type. (struct saved_scope): Add omp_begin_assumes member. * parser.cc: Include bitmap.h. (cp_parser_omp_assumption_clauses, cp_parser_omp_assume, cp_parser_omp_assumes, cp_parser_omp_begin): New functions. (cp_parser_omp_end_declare_target): Rename to ... (cp_parser_omp_end): ... this. Handle also end assumes. (cp_parser_omp_construct): Handle PRAGMA_OMP_ASSUME. (cp_parser_pragma): Handle PRAGMA_OMP_ASSUME, PRAGMA_OMP_ASSUMES and PRAGMA_OMP_BEGIN. Handle PRAGMA_OMP_END rather than PRAGMA_OMP_END_DECLARE_TARGET and call cp_parser_omp_end for it rather than cp_parser_omp_end_declare_target. * pt.cc (apply_late_template_attributes): Also temporarily clear omp_begin_assumes. * semantics.cc (finish_translation_unit): Also diagnose #pragma omp begin assumes without corresponding #pragma omp end assumes. gcc/testsuite/ * c-c++-common/gomp/assume-1.c: New test. * c-c++-common/gomp/assume-2.c: New test. * c-c++-common/gomp/assume-3.c: New test. * c-c++-common/gomp/assumes-1.c: New test. * c-c++-common/gomp/assumes-2.c: New test. * c-c++-common/gomp/assumes-3.c: New test. * c-c++-common/gomp/assumes-4.c: New test. * c-c++-common/gomp/begin-assumes-1.c: New test. * c-c++-common/gomp/begin-assumes-2.c: New test. * c-c++-common/gomp/begin-assumes-3.c: New test. * c-c++-common/gomp/begin-assumes-4.c: New test. * c-c++-common/gomp/declare-target-6.c: New test. * g++.dg/gomp/attrs-1.C (bar): Add n1 and n2 arguments, add tests for assume directive. * g++.dg/gomp/attrs-2.C (bar): Likewise. * g++.dg/gomp/attrs-9.C: Add n1 and n2 variables, add tests for begin assumes directive. * g++.dg/gomp/attrs-15.C: New test. * g++.dg/gomp/attrs-16.C: New test. * g++.dg/gomp/attrs-17.C: New test.
2022-09-27c++: Implement P1467R9 - Extended floating-point types and standard names ↵Jakub Jelinek1-0/+13
compiler part except for bfloat16 [PR106652] The following patch implements the compiler part of C++23 P1467R9 - Extended floating-point types and standard names compiler part by introducing _Float{16,32,64,128} as keywords and builtin types like they are implemented for C already since GCC 7, with DF{16,32,64,128}_ mangling. It also introduces _Float{32,64,128}x for C++ with the https://github.com/itanium-cxx-abi/cxx-abi/pull/147 proposed mangling of DF{32,64,128}x. The patch doesn't add anything for bfloat16_t support, as right now __bf16 type refuses all conversions and arithmetic operations. The patch wants to keep backwards compatibility with how __float128 has been handled in C++ before, both for mangling and behavior in binary operations, overload resolution etc. So, there are some backend changes where for C __float128 and _Float128 are the same type (float128_type_node and float128t_type_node are the same pointer), but for C++ they are distinct types which mangle differently and _Float128 is treated as extended floating-point type while __float128 is treated as non-standard floating point type. The various C++23 changes about how floating-point types are changed are actually implemented as written in the spec only if at least one of the types involved is _Float{16,32,64,128,32x,64x,128x} (_FloatNx are also treated as extended floating-point types) and kept previous behavior otherwise. For float/double/long double the rules are actually written that they behave the same as before. There is some backwards incompatibility at least on x86 regarding _Float16, because that type was already used by that name and with the DF16_ mangling (but only since GCC 12 and I think it isn't that widely used in the wild yet). E.g. config/i386/avx512fp16intrin.h shows the issues, where in C or in GCC 12 in C++ one could pass 0.0f to a builtin taking _Float16 argument, but with the changes that is not possible anymore, one needs to either use 0.0f16 or (_Float16) 0.0f. We have also a problem with glibc headers, where since glibc 2.27 math.h and complex.h aren't compilable with these changes. One gets errors like: In file included from /usr/include/math.h:43, from abc.c:1: /usr/include/bits/floatn.h:86:9: error: multiple types in one declaration 86 | typedef __float128 _Float128; | ^~~~~~~~~~ /usr/include/bits/floatn.h:86:20: error: declaration does not declare anything [-fpermissive] 86 | typedef __float128 _Float128; | ^~~~~~~~~ In file included from /usr/include/bits/floatn.h:119: /usr/include/bits/floatn-common.h:214:9: error: multiple types in one declaration 214 | typedef float _Float32; | ^~~~~ /usr/include/bits/floatn-common.h:214:15: error: declaration does not declare anything [-fpermissive] 214 | typedef float _Float32; | ^~~~~~~~ /usr/include/bits/floatn-common.h:251:9: error: multiple types in one declaration 251 | typedef double _Float64; | ^~~~~~ /usr/include/bits/floatn-common.h:251:16: error: declaration does not declare anything [-fpermissive] 251 | typedef double _Float64; | ^~~~~~~~ This is from snippets like: /* The remaining of this file provides support for older compilers. */ # if __HAVE_FLOAT128 /* The type _Float128 exists only since GCC 7.0. */ # if !__GNUC_PREREQ (7, 0) || defined __cplusplus typedef __float128 _Float128; # endif where it hardcodes that C++ doesn't have _Float{16,32,64,128,32x,64x,128x} support nor {f,F}{16,32,64,128}{,x} literal suffixes nor _Complex _Float{16,32,64,128,32x,64x,128x}. The patch fixincludes this for now and hopefully if this is committed, then glibc can change those. The patch changes those # if !__GNUC_PREREQ (7, 0) || defined __cplusplus conditions to # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0)) Another thing is mangling, as said above, Itanium C++ ABI specifies DF <number> _ as _Float{16,32,64,128} mangling, but GCC was implementing a mangling incompatible with that starting with DF for fixed point types. Fixed point was never supported in C++ though, I believe the reason why the mangling has been added was that due to a bug it would leak into the C++ FE through decltype (0.0r) etc. But that has been shortly after the mangling was added fixed (I think in the same GCC release cycle), so we now reject 0.0r etc. in C++. If we ever need the fixed point mangling, I think it can be readded but better with a different prefix so that it doesn't conflict with the published standard manglings. So, this patch also kills the fixed point mangling and implements the DF <number> _ demangling. The patch predefines __STDCPP_FLOAT{16,32,64,128}_T__ macros when those types are available, but only for C++23, while the underlying types are available in C++98 and later including the {f,F}{16,32,64,128} literal suffixes (but those with a pedwarn for C++20 and earlier). My understanding is that it needs to be predefined by the compiler, on the other side predefining even for older modes when <stdfloat> is a new C++23 header would be weird. One can find out if _Float{16,32,64,128,32x,64x,128x} is supported in C++ by __GNUC__ >= 13 && defined(__FLT{16,32,64,128,32X,64X,128X}_MANT_DIG__) (but that doesn't work well with older G++ 13 snapshots). As for std::bfloat16_t, three targets (aarch64, arm and x86) apparently "support" __bf16 type which has the bfloat16 format, but isn't really usable, e.g. {aarch64,arm,ix86}_invalid_conversion disallow any conversions from or to type with BFmode, {aarch64,arm,ix86}_invalid_unary_op disallows any unary operations on those except for ADDR_EXPR and {aarch64,arm,ix86}_invalid_binary_op disallows any binary operation on those. So, I think we satisfy: "If the implementation supports an extended floating-point type with the properties, as specified by ISO/IEC/IEEE 60559, of radix (b) of 2, storage width in bits (k) of 16, precision in bits (p) of 8, maximum exponent (emax) of 127, and exponent field width in bits (w) of 8, then the typedef-name std::bfloat16_t is defined in the header <stdfloat> and names such a type, the macro __STDCPP_BFLOAT16_T__ is defined, and the floating-point literal suffixes bf16 and BF16 are supported." because we don't really support those right now. 2022-09-27 Jakub Jelinek <jakub@redhat.com> PR c++/106652 PR c++/85518 gcc/ * tree-core.h (enum tree_index): Add TI_FLOAT128T_TYPE enumerator. * tree.h (float128t_type_node): Define. * tree.cc (build_common_tree_nodes): Initialize float128t_type_node. * builtins.def (DEF_FLOATN_BUILTIN): Adjust comment now that _Float<N> is supported in C++ too. * config/i386/i386.cc (ix86_mangle_type): Only mangle as "g" float128t_type_node. * config/i386/i386-builtins.cc (ix86_init_builtin_types): Use float128t_type_node for __float128 instead of float128_type_node and create it if NULL. * config/i386/avx512fp16intrin.h (_mm_setzero_ph, _mm256_setzero_ph, _mm512_setzero_ph, _mm_set_sh, _mm_load_sh): Use 0.0f16 instead of 0.0f. * config/ia64/ia64.cc (ia64_init_builtins): Use float128t_type_node for __float128 instead of float128_type_node and create it if NULL. * config/rs6000/rs6000-c.cc (is_float128_p): Also return true for float128t_type_node if non-NULL. * config/rs6000/rs6000.cc (rs6000_mangle_type): Don't mangle float128_type_node as "u9__ieee128". * config/rs6000/rs6000-builtin.cc (rs6000_init_builtins): Use float128t_type_node for __float128 instead of float128_type_node and create it if NULL. gcc/c-family/ * c-common.cc (c_common_reswords): Change _Float{16,32,64,128} and _Float{32,64,128}x flags from D_CONLY to 0. (shorten_binary_op): Punt if common_type returns error_mark_node. (shorten_compare): Likewise. (c_common_nodes_and_builtins): For C++ record _Float{16,32,64,128} and _Float{32,64,128}x builtin types if available. For C++ clear float128t_type_node. * c-cppbuiltin.cc (c_cpp_builtins): Predefine __STDCPP_FLOAT{16,32,64,128}_T__ for C++23 if supported. * c-lex.cc (interpret_float): For q/Q suffixes prefer float128t_type_node over float128_type_node. Allow {f,F}{16,32,64,128} suffixes for C++ if supported with pedwarn for C++20 and older. Allow {f,F}{32,64,128}x suffixes for C++ with pedwarn. Don't call excess_precision_type for C++. gcc/cp/ * cp-tree.h (cp_compare_floating_point_conversion_ranks): Implement P1467R9 - Extended floating-point types and standard names except for std::bfloat16_t for now. Declare. (extended_float_type_p): New inline function. * mangle.cc (write_builtin_type): Mangle float{16,32,64,128}_type_node as DF{16,32,64,128}_. Mangle float{32,64,128}x_type_node as DF{32,64,128}x. Remove FIXED_POINT_TYPE mangling that conflicts with that. * typeck2.cc (check_narrowing): If one of ftype or type is extended floating-point type, compare floating-point conversion ranks. * parser.cc (cp_keyword_starts_decl_specifier_p): Handle CASE_RID_FLOATN_NX. (cp_parser_simple_type_specifier): Likewise and diagnose missing _Float<N> or _Float<N>x support if not supported by target. * typeck.cc (cp_compare_floating_point_conversion_ranks): New function. (cp_common_type): If both types are REAL_TYPE and one or both are extended floating-point types, select common type based on comparison of floating-point conversion ranks and subranks. (cp_build_binary_op): Diagnose operation with floating point arguments with unordered conversion ranks. * call.cc (standard_conversion): For floating-point conversion, if either from or to are extended floating-point types, set conv->bad_p for implicit conversion from larger to smaller conversion rank or with unordered conversion ranks. (convert_like_internal): Emit a pedwarn on such conversions. (build_conditional_expr): Diagnose operation with floating point arguments with unordered conversion ranks. (convert_arg_to_ellipsis): Don't promote extended floating-point types narrower than double to double. (compare_ics): Implement P1467R9 [over.ics.rank]/4 changes. gcc/testsuite/ * g++.dg/cpp23/ext-floating1.C: New test. * g++.dg/cpp23/ext-floating2.C: New test. * g++.dg/cpp23/ext-floating3.C: New test. * g++.dg/cpp23/ext-floating4.C: New test. * g++.dg/cpp23/ext-floating5.C: New test. * g++.dg/cpp23/ext-floating6.C: New test. * g++.dg/cpp23/ext-floating7.C: New test. * g++.dg/cpp23/ext-floating8.C: New test. * g++.dg/cpp23/ext-floating9.C: New test. * g++.dg/cpp23/ext-floating10.C: New test. * g++.dg/cpp23/ext-floating.h: New file. * g++.target/i386/float16-1.C: Adjust expected diagnostics. libcpp/ * expr.cc (interpret_float_suffix): Allow {f,F}{16,32,64,128} and {f,F}{32,64,128}x suffixes for C++. include/ * demangle.h (enum demangle_component_type): Add DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE. (struct demangle_component): Add u.s_extended_builtin member. libiberty/ * cp-demangle.c (d_dump): Handle DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE. Don't handle DEMANGLE_COMPONENT_FIXED_TYPE. (d_make_extended_builtin_type): New function. (cplus_demangle_builtin_types): Add _Float entry. (cplus_demangle_type): For DF demangle it as _Float<N> or _Float<N>x rather than fixed point which conflicts with it. (d_count_templates_scopes): Handle DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE. Just break; for DEMANGLE_COMPONENT_FIXED_TYPE. (d_find_pack): Handle DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE. Don't handle DEMANGLE_COMPONENT_FIXED_TYPE. (d_print_comp_inner): Likewise. * cp-demangle.h (D_BUILTIN_TYPE_COUNT): Bump. * testsuite/demangle-expected: Replace _Z3xxxDFyuVb test with _Z3xxxDF16_DF32_DF64_DF128_CDF16_Vb. Add _Z3xxxDF32xDF64xDF128xCDF32xVb test. fixincludes/ * inclhack.def (glibc_cxx_floatn_1, glibc_cxx_floatn_2, glibc_cxx_floatn_3): New fixes. * tests/base/bits/floatn.h: New file. * fixincl.x: Regenerated.
2022-09-23c++: Implement __is_{nothrow_,}convertible [PR106784]Marek Polacek1-0/+4
To improve compile times, the C++ library could use compiler built-ins rather than implementing std::is_convertible (and _nothrow) as class templates. This patch adds the built-ins. We already have __is_constructible and __is_assignable, and the nothrow forms of those. Microsoft (and clang, for compatibility) also provide an alias called __is_convertible_to. I did not add it, but it would be trivial to do so. I noticed that our __is_assignable doesn't implement the "Access checks are performed as if from a context unrelated to either type" requirement, therefore std::is_assignable / __is_assignable give two different results here: class S { operator int(); friend void g(); // #1 }; void g () { // #1 doesn't matter static_assert(std::is_assignable<int&, S>::value, ""); static_assert(__is_assignable(int&, S), ""); } This is not a problem if __is_assignable is not meant to be used by the users. This patch doesn't make libstdc++ use the new built-ins, but I had to rename a class otherwise its name would clash with the new built-in. PR c++/106784 gcc/c-family/ChangeLog: * c-common.cc (c_common_reswords): Add __is_convertible and __is_nothrow_convertible. * c-common.h (enum rid): Add RID_IS_CONVERTIBLE and RID_IS_NOTHROW_CONVERTIBLE. gcc/cp/ChangeLog: * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_CONVERTIBLE and CPTK_IS_NOTHROW_CONVERTIBLE. * cp-objcp-common.cc (names_builtin_p): Handle RID_IS_CONVERTIBLE RID_IS_NOTHROW_CONVERTIBLE. * cp-tree.h (enum cp_trait_kind): Add CPTK_IS_CONVERTIBLE and CPTK_IS_NOTHROW_CONVERTIBLE. (is_convertible): Declare. (is_nothrow_convertible): Likewise. * cxx-pretty-print.cc (pp_cxx_trait_expression): Handle CPTK_IS_CONVERTIBLE and CPTK_IS_NOTHROW_CONVERTIBLE. * method.cc (is_convertible): New. (is_nothrow_convertible): Likewise. * parser.cc (cp_parser_primary_expression): Handle RID_IS_CONVERTIBLE and RID_IS_NOTHROW_CONVERTIBLE. (cp_parser_trait_expr): Likewise. * semantics.cc (trait_expr_value): Handle CPTK_IS_CONVERTIBLE and CPTK_IS_NOTHROW_CONVERTIBLE. (finish_trait_expr): Likewise. libstdc++-v3/ChangeLog: * include/std/type_traits: Rename __is_nothrow_convertible to __is_nothrow_convertible_lib. * testsuite/20_util/is_nothrow_convertible/value_ext.cc: Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Enhance to test __is_convertible and __is_nothrow_convertible. * g++.dg/ext/is_convertible1.C: New test. * g++.dg/ext/is_convertible2.C: New test. * g++.dg/ext/is_nothrow_convertible1.C: New test. * g++.dg/ext/is_nothrow_convertible2.C: New test.
2022-09-13c++: remove single-parameter version of mark_usedPatrick Palka1-2/+2
gcc/cp/ChangeLog: * cp-tree.h (mark_used): Remove single-parameter overload. Add default argument to the two-parameter overload. * decl2.cc (mark_used): Likewise.
2022-09-13c++: two-parameter version of cxx_constant_valuePatrick Palka1-0/+2
Since some callers need the complain parameter but not the object parameter, let's introduce and use an overload of cxx_constant_value that omits the latter. gcc/cp/ChangeLog: * cp-tree.h (cxx_constant_value): Define two-parameter version that omits the object parameter. * decl.cc (build_explicit_specifier): Omit NULL_TREE object argument to cxx_constant_value. * except.cc (build_noexcept_spec): Likewise. * pt.cc (expand_integer_pack): Likewise. (fold_targs_r): Likewise. * semantics.cc (finish_if_stmt_cond): Likewise.
2022-09-12c++: remove '_sfinae' suffix from functionsPatrick Palka1-12/+11
The functions abstract_virtuals_error cxx_constant_value get_target_expr instantiate_non_dependent_expr require_complete_type are each just a non-SFINAE-enabled wrapper for the corresponding SFINAE-enabled version that's suffixed by '_sfinae'. But this suffix is at best redundant since a 'complain' parameter already broadly conveys that a function is SFINAE-enabled, and having two such versions of a function is less concise than just using a default argument for 'complain' (and arguably no less mistake prone). So this patch squashes the two versions of each of the above functions by adding a default 'complain' argument to the SFINAE-enabled version whose '_sfinae' suffix we then remove. gcc/cp/ChangeLog: * call.cc (build_conditional_expr): Adjust calls to '_sfinae'-suffixed functions. (build_temp): Likewise. (convert_like_internal): Likewise. (convert_arg_to_ellipsis): Likewise. (build_over_call): Likewise. (build_cxx_call): Likewise. (build_new_method_call): Likewise. * constexpr.cc (cxx_eval_outermost_constant_expr): Likewise. (cxx_constant_value_sfinae): Rename to ... (cxx_constant_value): ... this. Document its default arguments. (fold_non_dependent_expr): Adjust function comment. * cp-tree.h (instantiate_non_dependent_expr_sfinae): Rename to ... (instantiate_non_dependent_expr): ... this. Give its 'complain' parameter a default argument. (get_target_expr_sfinae, get_target_expr): Likewise. (require_complete_type_sfinae, require_complete_type): Likewise. (abstract_virtuals_error_sfinae, abstract_virtuals_error): Likewise. (cxx_constant_value_sfinae, cxx_constant_value): Likewise. * cvt.cc (build_up_reference): Adjust calls to '_sfinae'-suffixed functions. (ocp_convert): Likewise. * decl.cc (build_explicit_specifier): Likewise. * except.cc (build_noexcept_spec): Likewise. * init.cc (build_new_1): Likewise. * pt.cc (expand_integer_pack): Likewise. (instantiate_non_dependent_expr_internal): Adjust function comment. (instantiate_non_dependent_expr): Rename to ... (instantiate_non_dependent_expr_sfinae): ... this. Document its default argument. (tsubst_init): Adjust calls to '_sfinae'-suffixed functions. (fold_targs_r): Likewise. * semantics.cc (finish_compound_literal): Likewise. (finish_decltype_type): Likewise. (cp_build_bit_cast): Likewise. * tree.cc (build_cplus_new): Likewise. (get_target_expr): Rename to ... (get_target_expr_sfinae): ... this. Document its default argument. * typeck.cc (require_complete_type): Rename to ... (require_complete_type_sfinae): ... this. Document its default argument. (cp_build_array_ref): Adjust calls to '_sfinae'-suffixed functions. (convert_arguments): Likewise. (cp_build_binary_op): Likewise. (build_static_cast_1): Likewise. (cp_build_modify_expr): Likewise. (convert_for_initialization): Likewise. * typeck2.cc (abstract_virtuals_error): Rename to ... (abstract_virtuals_error_sfinae): ... this. Document its default argument. (build_functional_cast_1): Adjust calls to '_sfinae'-suffixed functions.
2022-09-12c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]Jonathan Wakely1-1/+2
Since C++11 relaxed the requirement for template arguments to have external linkage, it's possible to get -Wsubobject-linkage warnings without using any anonymous namespaces. This confuses users when they get diagnostics that refer to an anonymous namespace that doesn't exist in their code. This changes the diagnostic to say "has internal linkage" for C++11 and later, if the type isn't actually a member of the anonymous namespace. Making that distinction involved renaming the current decl_anon_ns_mem_p to something that better expresses its semantics. For C++98 template arguments declared with 'static' are ill-formed anyway, so the only way this warning can arise is via anonymous namespaces. That means the existing wording is accurate for C++98 and so we can keep it. PR c++/86491 gcc/cp/ChangeLog: * decl2.cc (constrain_class_visibility): Adjust wording of -Wsubobject-linkage for cases where anonymous namespaces aren't used. * tree.cc (decl_anon_ns_mem_p): Now only true for actual anonymous namespace members, rename old semantics to... (decl_internal_context_p): ...this. * cp-tree.h, name-lookup.cc, pt.cc: Adjust. gcc/testsuite/ChangeLog: * g++.dg/warn/anonymous-namespace-3.C: Use separate dg-warning directives for C++98 and everything else. * g++.dg/warn/Wsubobject-linkage-5.C: New test.
2022-09-02c/c++: new warning: -Wxor-used-as-pow [PR90885]David Malcolm1-3/+16
PR c/90885 notes various places in real-world code where people have written C/C++ code that uses ^ (exclusive or) where presumbably they meant exponentiation. For example https://codesearch.isocpp.org/cgi-bin/cgi_ppsearch?q=2%5E32&search=Search currently finds 11 places using "2^32", and all of them appear to be places where the user means 2 to the power of 32, rather than 2 exclusive-orred with 32 (which is 34). This patch adds a new -Wxor-used-as-pow warning to the C and C++ frontends to complain about ^ when the left-hand side is the decimal constant 2 or the decimal constant 10. This is the same name as the corresponding clang warning: https://clang.llvm.org/docs/DiagnosticsReference.html#wxor-used-as-pow As per the clang warning, the warning suggests converting the left-hand side to a hexadecimal constant if you really mean xor, which suppresses the warning (though this patch implements a fix-it hint for that, whereas the clang implementation only has a fix-it hint for the initial suggestion of exponentiation). I initially tried implementing this without checking for decimals, but this version had lots of false positives. Checking for decimals requires extending the lexer to capture whether or not a CPP_NUMBER token was decimal. I added a new DECIMAL_INT flag to cpplib.h for this. Unfortunately, c_token and cp_tokens both have only an unsigned char for their flags (as captured by c_lex_with_flags), whereas this would add the 12th flag to cpp_tokens. Of the first 8 flags, all but BOL are used in the C or C++ frontends, but BOL is not, so I moved that to a higher position, using its old value for the new DECIMAL_INT flag, so that it is representable within an unsigned char. Example output: demo.c:5:13: warning: result of '2^8' is 10; did you mean '1 << 8' (256)? [-Wxor-used-as-pow] 5 | int t2_8 = 2^8; | ^ | -- | 1<< demo.c:5:12: note: you can silence this warning by using a hexadecimal constant (0x2 rather than 2) 5 | int t2_8 = 2^8; | ^ | 0x2 demo.c:21:15: warning: result of '10^6' is 12; did you mean '1e6'? [-Wxor-used-as-pow] 21 | int t10_6 = 10^6; | ^ | --- | 1e demo.c:21:13: note: you can silence this warning by using a hexadecimal constant (0xa rather than 10) 21 | int t10_6 = 10^6; | ^~ | 0xa gcc/c-family/ChangeLog: PR c/90885 * c-common.h (check_for_xor_used_as_pow): New decl. * c-lex.cc (c_lex_with_flags): Add DECIMAL_INT to flags as appropriate. * c-warn.cc (check_for_xor_used_as_pow): New. * c.opt (Wxor-used-as-pow): New. gcc/c/ChangeLog: PR c/90885 * c-parser.cc (c_parser_string_literal): Clear ret.m_decimal. (c_parser_expr_no_commas): Likewise. (c_parser_conditional_expression): Likewise. (c_parser_binary_expression): Clear m_decimal when popping the stack. (c_parser_unary_expression): Clear ret.m_decimal. (c_parser_has_attribute_expression): Likewise for result. (c_parser_predefined_identifier): Likewise for expr. (c_parser_postfix_expression): Likewise for expr. Set expr.m_decimal when handling a CPP_NUMBER that was a decimal token. * c-tree.h (c_expr::m_decimal): New bitfield. * c-typeck.cc (parser_build_binary_op): Clear result.m_decimal. (parser_build_binary_op): Call check_for_xor_used_as_pow. gcc/cp/ChangeLog: PR c/90885 * cp-tree.h (class cp_expr): Add bitfield m_decimal. Clear it in existing ctors. Add ctor that allows specifying its value. (cp_expr::decimal_p): New accessor. * parser.cc (cp_parser_expression_stack_entry::flags): New field. (cp_parser_primary_expression): Set m_decimal of cp_expr when handling numbers. (cp_parser_binary_expression): Extract flags from token when populating stack. Call check_for_xor_used_as_pow. gcc/ChangeLog: PR c/90885 * doc/invoke.texi (Warning Options): Add -Wxor-used-as-pow. gcc/testsuite/ChangeLog: PR c/90885 * c-c++-common/Wxor-used-as-pow-1.c: New test. * c-c++-common/Wxor-used-as-pow-fixits.c: New test. * g++.dg/parse/expr3.C: Convert 2 to 0x2 to suppress -Wxor-used-as-pow. * g++.dg/warn/Wparentheses-10.C: Likewise. * g++.dg/warn/Wparentheses-18.C: Likewise. * g++.dg/warn/Wparentheses-19.C: Likewise. * g++.dg/warn/Wparentheses-9.C: Likewise. * g++.dg/warn/Wxor-used-as-pow-named-op.C: New test. * gcc.dg/Wparentheses-6.c: Convert 2 to 0x2 to suppress -Wxor-used-as-pow. * gcc.dg/Wparentheses-7.c: Likewise. * gcc.dg/precedence-1.c: Likewise. libcpp/ChangeLog: PR c/90885 * include/cpplib.h (BOL): Move macro to 1 << 12 since it is not used by C/C++'s unsigned char token flags. (DECIMAL_INT): New, using 1 << 6, so that it is visible as part of C/C++'s 8 bits of token flags. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2022-09-01c++: Remove unused declarationMarek Polacek1-1/+0
This declaration was added in r260905 but the function was never defined. gcc/cp/ChangeLog: * cp-tree.h (maybe_strip_ref_conversion): Remove.
2022-08-25c: Implement C23 nullptr (N3042)Marek Polacek1-8/+0
This patch implements the C23 nullptr literal: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3042.htm> (with wording fixes from N3047), which is intended to replace the problematic definition of NULL which might be either of integer type or void*. Since C++ has had nullptr for over a decade now, it was relatively easy to just move the built-in node definitions from the C++ FE to the C/C++ common code. Also, our DWARF emitter already handles NULLPTR_TYPE by emitting DW_TAG_unspecified_type. However, I had to handle a lot of contexts such as ?:, comparison, conversion, etc. There are some minor differences, e.g. in C you can do bool b = nullptr; but in C++ you have to use direct-initialization: bool b{nullptr}; And I think that nullptr_t n = 0; is only valid in C++. Of course, C doesn't have to handle mangling, RTTI, substitution, overloading, ... This patch also defines nullptr_t in <stddef.h>. However, it does not define __STDC_VERSION_STDDEF_H__ yet, because we don't know yet what value it should be defined to. gcc/c-family/ChangeLog: * c-common.cc (c_common_reswords): Enable nullptr in C2X. (c_common_nodes_and_builtins): Create the built-in node for nullptr. * c-common.h (enum c_tree_index): Add CTI_NULLPTR, CTI_NULLPTR_TYPE. (struct c_common_resword): Resize the disable member. (D_C2X): Add. (nullptr_node): Define. (nullptr_type_node): Define. (NULLPTR_TYPE_P): Define. * c-pretty-print.cc (c_pretty_printer::simple_type_specifier): Handle NULLPTR_TYPE. (c_pretty_printer::direct_abstract_declarator): Likewise. (c_pretty_printer::constant): Likewise. gcc/c/ChangeLog: * c-convert.cc (c_convert) <case POINTER_TYPE>: Handle NULLPTR_TYPE. Give a better diagnostic when converting to nullptr_t. * c-decl.cc (c_init_decl_processing): Perform C-specific nullptr initialization. * c-parser.cc (c_parse_init): Maybe OR D_C2X into mask. (c_parser_postfix_expression): Handle RID_NULLPTR. * c-typeck.cc (null_pointer_constant_p): Return true when expr is nullptr_node. (build_unary_op) <case TRUTH_NOT_EXPR>: Handle NULLPTR_TYPE. (build_conditional_expr): Handle the case when the second/third operand is NULLPTR_TYPE and third/second operand is POINTER_TYPE. (convert_for_assignment): Handle converting an expression of type nullptr_t to pointer/bool. (build_binary_op) <case TRUTH_XOR_EXPR>: Handle NULLPTR_TYPE. <case EQ_EXPR>: Handle comparing operands of type nullptr_t. gcc/cp/ChangeLog: * cp-tree.h (enum cp_tree_index): Remove CTI_NULLPTR, CTI_NULLPTR_TYPE. Move it to c_tree_index. (nullptr_node): No longer define here. (nullptr_type_node): Likewise. (NULLPTR_TYPE_P): Likewise. * decl.cc (cxx_init_decl_processing): Only keep C++-specific nullptr initialization; move the shared code to c_common_nodes_and_builtins. gcc/ChangeLog: * ginclude/stddef.h: Define nullptr_t. gcc/testsuite/ChangeLog: * gcc.dg/c11-nullptr-1.c: New test. * gcc.dg/c17-nullptr-1.c: New test. * gcc.dg/c17-nullptr-2.c: New test. * gcc.dg/c2x-nullptr-1.c: New test. * gcc.dg/c2x-nullptr-2.c: New test. * gcc.dg/c2x-nullptr-3.c: New test. * gcc.dg/c2x-nullptr-4.c: New test. * gcc.dg/c2x-nullptr-5.c: New test.
2022-08-17c++: Extend -Wpessimizing-move to other contextsMarek Polacek1-0/+1
In my recent patch which enhanced -Wpessimizing-move so that it warns about class prvalues too I said that I'd like to extend it so that it warns in more contexts where a std::move can prevent copy elision, such as: T t = std::move(T()); T t(std::move(T())); T t{std::move(T())}; T t = {std::move(T())}; void foo (T); foo (std::move(T())); This patch does that by adding two maybe_warn_pessimizing_move calls. These must happen before we've converted the initializers otherwise the std::move will be buried in a TARGET_EXPR. PR c++/106276 gcc/cp/ChangeLog: * call.cc (build_over_call): Call maybe_warn_pessimizing_move. * cp-tree.h (maybe_warn_pessimizing_move): Declare. * decl.cc (build_aggr_init_full_exprs): Call maybe_warn_pessimizing_move. * typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and CONSTRUCTOR. Add a bool parameter and use it. Adjust a diagnostic message. (check_return_expr): Adjust the call to maybe_warn_pessimizing_move. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning. * g++.dg/cpp0x/Wpessimizing-move8.C: New test.
2022-08-17OpenMP/C++: Allow classes with static members to be mappable [PR104493]Tobias Burnus1-2/+0
As this is the last lang-specific user of the omp_mappable_type hook, the hook is removed, keeping only a generic omp_mappable_type for incomplete types (or error_node). PR c++/104493 gcc/c/ChangeLog: * c-decl.cc (c_decl_attributes, finish_decl): Call omp_mappable_type instead of removed langhook. * c-typeck.cc (c_finish_omp_clauses): Likewise. gcc/cp/ChangeLog: * cp-objcp-common.h (LANG_HOOKS_OMP_MAPPABLE_TYPE): Remove. * cp-tree.h (cp_omp_mappable_type, cp_omp_emit_unmappable_type_notes): Remove. * decl2.cc (cp_omp_mappable_type_1, cp_omp_mappable_type, cp_omp_emit_unmappable_type_notes): Remove. (cplus_decl_attributes): Call omp_mappable_type instead of removed langhook. * decl.cc (cp_finish_decl): Likewise; call cxx_incomplete_type_inform in lieu of cp_omp_emit_unmappable_type_notes. * semantics.cc (finish_omp_clauses): Likewise. gcc/ChangeLog: * gimplify.cc (omp_notice_variable): Call omp_mappable_type instead of removed langhook. * omp-general.h (omp_mappable_type): New prototype. * omp-general.cc (omp_mappable_type): New; moved from ... * langhooks.cc (lhd_omp_mappable_type): ... here. * langhooks-def.h (lhd_omp_mappable_type, LANG_HOOKS_OMP_MAPPABLE_TYPE): Remove. (LANG_HOOKS_FOR_TYPES_INITIALIZER): Remote the latter. * langhooks.h (struct lang_hooks_for_types): Remove omp_mappable_type. gcc/testsuite/ChangeLog: * g++.dg/gomp/unmappable-1.C: Remove dg-error; remove dg-note no longer shown as TYPE_MAIN_DECL is NULL. * c-c++-common/gomp/map-incomplete-type.c: New test. Co-authored-by: Chung-Lin Tang <cltang@codesourcery.com>
2022-07-19c++: shortcut bad reference binding [PR94894]Patrick Palka1-0/+5
In case of l/rvalue or cv-qual mismatch during reference binding, we try to give more helpful diagnostics by computing a bad conversion that allows the mismatch. But in doing so, we may end up considering and instantiating a conversion function that could induce a hard error and in turn cause us to reject otherwise valid code. We could just give up on producing a better diagnostic here, but ideally we'd preserve the better diagnostics for invalid code while avoiding unnecessary template instantiations for valid code. To that end, this patch adapts the bad conversion shortcutting mechanism from r12-3346-g47543e5f9d1fc5 to additionally handle this situation. The main observation from there is that during overload resolution, if we know we have a strictly viable candidate then we don't need to distinguish between an unviable and non-strictly viable candidate. Thus we don't need to distinguish between an invalid and bad conversion either, which is what this patch exploits. Of course, we don't know whether we have a strictly viable candidate until after the fact, so we still need to remember when we deferred distinguishing between an invalid and bad conversion. This patch adds a special conversion kind ck_deferred_bad for this purpose. PR c++/94894 PR c++/105766 PR c++/106201 gcc/cp/ChangeLog: * call.cc (enum conversion_kind): Add ck_deferred_bad enumerator. (has_next): Return false for it. (reference_binding): Return a ck_deferred_bad conversion instead of an actual bad conversion when LOOKUP_SHORTCUT_BAD_CONVS is set. Remove now obsolete early exit for the incomplete TO case. (implicit_conversion_1): Don't mask out LOOKUP_SHORTCUT_BAD_CONVS. (add_function_candidate): Set LOOKUP_SHORTCUT_BAD_CONVS iff shortcut_bad_convs. (missing_conversion_p): Also return true for a ck_deferred_bad conversion. * cp-tree.h (LOOKUP_SHORTCUT_BAD_CONVS): Define. gcc/testsuite/ChangeLog: * g++.dg/conversion/ref8.C: New test. * g++.dg/conversion/ref9.C: New test.
2022-07-15c++: Add __reference_con{struc,ver}ts_from_temporary [PR104477]Marek Polacek1-2/+6
This patch implements C++23 P2255R2, which adds two new type traits to detect reference binding to a temporary. They can be used to detect code like std::tuple<const std::string&> t("meow"); which is incorrect because it always creates a dangling reference, because the std::string temporary is created inside the selected constructor of std::tuple, and not outside it. There are two new compiler builtins, __reference_constructs_from_temporary and __reference_converts_from_temporary. The former is used to simulate direct- and the latter copy-initialization context. But I had a hard time finding a test where there's actually a difference. Under DR 2267, both of these are invalid: struct A { } a; struct B { explicit B(const A&); }; const B &b1{a}; const B &b2(a); so I had to peruse [over.match.ref], and eventually realized that the difference can be seen here: struct G { operator int(); // #1 explicit operator int&&(); // #2 }; int&& r1(G{}); // use #2 (no temporary) int&& r2 = G{}; // use #1 (a temporary is created to be bound to int&&) The implementation itself was rather straightforward because we already have the conv_binds_ref_to_prvalue function. The main function here is ref_xes_from_temporary. I've changed the return type of ref_conv_binds_directly to tristate, because previously the function didn't distinguish between an invalid conversion and one that binds to a prvalue. Since it no longer returns a bool, I removed the _p suffix. The patch also adds the relevant class and variable templates to <type_traits>. PR c++/104477 gcc/c-family/ChangeLog: * c-common.cc (c_common_reswords): Add __reference_constructs_from_temporary and __reference_converts_from_temporary. * c-common.h (enum rid): Add RID_REF_CONSTRUCTS_FROM_TEMPORARY and RID_REF_CONVERTS_FROM_TEMPORARY. gcc/cp/ChangeLog: * call.cc (ref_conv_binds_directly_p): Rename to ... (ref_conv_binds_directly): ... this. Add a new bool parameter. Change the return type to tristate. * constraint.cc (diagnose_trait_expr): Handle CPTK_REF_CONSTRUCTS_FROM_TEMPORARY and CPTK_REF_CONVERTS_FROM_TEMPORARY. * cp-tree.h: Include "tristate.h". (enum cp_trait_kind): Add CPTK_REF_CONSTRUCTS_FROM_TEMPORARY and CPTK_REF_CONVERTS_FROM_TEMPORARY. (ref_conv_binds_directly_p): Rename to ... (ref_conv_binds_directly): ... this. (ref_xes_from_temporary): Declare. * cxx-pretty-print.cc (pp_cxx_trait_expression): Handle CPTK_REF_CONSTRUCTS_FROM_TEMPORARY and CPTK_REF_CONVERTS_FROM_TEMPORARY. * method.cc (ref_xes_from_temporary): New. * parser.cc (cp_parser_primary_expression): Handle RID_REF_CONSTRUCTS_FROM_TEMPORARY and RID_REF_CONVERTS_FROM_TEMPORARY. (cp_parser_trait_expr): Likewise. (warn_for_range_copy): Adjust to call ref_conv_binds_directly. * semantics.cc (trait_expr_value): Handle CPTK_REF_CONSTRUCTS_FROM_TEMPORARY and CPTK_REF_CONVERTS_FROM_TEMPORARY. (finish_trait_expr): Likewise. libstdc++-v3/ChangeLog: * include/std/type_traits (reference_constructs_from_temporary, reference_converts_from_temporary): New class templates. (reference_constructs_from_temporary_v, reference_converts_from_temporary_v): New variable templates. (__cpp_lib_reference_from_temporary): Define for C++23. * include/std/version (__cpp_lib_reference_from_temporary): Define for C++23. * testsuite/20_util/variable_templates_for_traits.cc: Test reference_constructs_from_temporary_v and reference_converts_from_temporary_v. * testsuite/20_util/reference_from_temporary/value.cc: New test. * testsuite/20_util/reference_from_temporary/value2.cc: New test. * testsuite/20_util/reference_from_temporary/version.cc: New test. gcc/testsuite/ChangeLog: * g++.dg/ext/reference_constructs_from_temporary1.C: New test. * g++.dg/ext/reference_converts_from_temporary1.C: New test.
2022-07-13c++: dependence of constrained memfn from current inst [PR105842]Patrick Palka1-0/+1
Here we incorrectly deem the calls to func1, func2 and tmpl2 as ambiguous ahead of time ultimately because we mishandle dependence of a constrained member function from the current instantiation. In type_dependent_expression_p, we already consider dependence of a TEMPLATE_DECL's constraints (via uses_outer_template_parms), but neglect to do the same for a FUNCTION_DECL (such as that for func1). And in satisfy_declaration_constraints, we give up if _any_ template argument is dependent, but for non-dependent member functions from the current instantiation (such as func2 and tmpl2), we can and must check constraints as long as the innermost arguments aren't dependent. PR c++/105842 gcc/cp/ChangeLog: * constraint.cc (satisfy_declaration_constraints): Refine early exit test for argument dependence. * cp-tree.h (uses_outer_template_parms_in_constraints): Declare. * pt.cc (template_class_depth): Handle TI_TEMPLATE being a FIELD_DECL. (usse_outer_template_parms): Factor out constraint dependence test into ... (uses_outer_template_parms_in_constraints): ... here. (type_dependent_expression_p): Use it for FUNCTION_DECL. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-memtmpl6.C: New test.
2022-06-14c++: Elide calls to NOP module initializersNathan Sidwell1-1/+1
gcc/cp * cp-tree.h (fini_modules): Add has_inits parm. * decl2.cc (c_parse_final_cleanups): Check for inits, adjust fini_modules flags. * module.cc (module_state): Rename call_init_p to active_init_p. (module_state::write_config): Write active_init. (module_state::read_config): Read it. (module_determine_import_inits): Clear active_init_p of covered inits. (late_finish_module): Add has_init parm. Record it. (fini_modules): Adjust. gcc/testsuite/ * g++.dg/modules/init-2_a.C: Adjust. * g++.dg/modules/init-2_c.C: Adjust. * g++.dg/modules/init-2_d.C: New.
2022-06-10c++: Add a late-writing step for modulesNathan Sidwell1-2/+2
To add a module initializer optimization, we need to defer finishing writing out the module file until the end of determining the dynamic initializers. This is achieved by passing some saved-state from the main module writing to a new function that completes it. This patch merely adds the skeleton of that state and move things around, allowing the finalization of the ELF file to be postponed. None of the contents writing is moved, or the init optimization added. gcc/cp/ * cp-tree.h (fini_modules): Add some parameters. (finish_module_processing): Return an opaque pointer. * decl2.cc (c_parse_final_cleanups): Propagate a cookie from finish_module_processing to fini_modules. * module.cc (struct module_processing_cookie): New. (finish_module_processing): Return a heap-allocated cookie. (late_finish_module): New. Finish out the module writing. (fini_modules): Adjust.
2022-06-10c++: Adjust module initializer calling emissionNathan Sidwell1-1/+1
We special-case emitting the calls of module initializer functions. It's simpler to just emit a static fn do do that, and add it onto the front of the global init fn chain. We can also move the calculation of the set of initializers to call to the point of use. gcc/cp/ * cp-tree.h (module_has_import_init): Rename to ... (module_determined_import_inits): ... here. * decl2.cc (start_objects): Do not handle module initializers here. (c_parse_final_cleanups): Generate a separate module initializer calling function and add it to the list. Shrink the c-lang region. * module.cc (num_init_calls_needed): Delete. (module_has_import_init): Rename to ... (module_determined_import_inits): ... here. Do the calculation here ... (finish_module_processing): ... rather than here. (module_add_import_initializers): Reformat. gcc/testsuite/ * g++.dg/modules/init-3_a.C: New. * g++.dg/modules/init-3_b.C: New. * g++.dg/modules/init-3_c.C: New.
2022-06-09c++: Better module initializer codeNathan Sidwell1-1/+2
Every module interface needs to emit a global initializer, but it might have nothing to init. In those cases, there's no need for any idempotency boolean to be emitted. gcc/cp * cp-tree.h (module_initializer_kind): Replace with ... (module_global_init_needed, module_has_import_inits): ... these. * decl2.cc (start_objects): Add has_body parm. Reorganize module initializer creation. (generate_ctor_or_dtor_function): Adjust. (c_parse_final_cleanups): Adjust. (vtv_start_verification_constructor_init_function): Adjust. * module.cc (module_initializer_kind): Replace with ... (module_global_init_needed, module_has_import_inits): ... these. gcc/testsuite/ * g++.dg/modules/init-2_a.C: Check no idempotency. * g++.dg/modules/init-2_b.C: Check idempotency.
2022-06-08c++: redeclared hidden friend take 2 [PR105852]Jason Merrill1-0/+1
My previous patch for 105761 avoided copying DECL_TEMPLATE_INFO from a friend to a later definition, but in this testcase we have first a non-friend declaration and then a definition, and we need to avoid copying in that case as well. But we do still want to set new_template_info to avoid GC trouble. With this change, the modules dump correctly identifies ::foo as a non-template function in tpl-friend-2_a.C. Along the way I noticed that the duplicate_decls handling of DECL_UNIQUE_FRIEND_P was backwards for templates, where we don't clobber DECL_LANG_SPECIFIC (olddecl) with DECL_LANG_SPECIFIC (newdecl) like we do for non-templates. PR c++/105852 PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Avoid copying template info from non-templated friend even if newdecl isn't a definition. Correct handling of DECL_UNIQUE_FRIEND_P on templates. * pt.cc (non_templated_friend_p): New. * cp-tree.h (non_templated_friend_p): Declare it. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-2_a.C: Adjust expected dump. * g++.dg/template/friend74.C: New test.
2022-05-31c++: squash cp_build_qualified_type/_realPatrick Palka1-3/+2
This combines the two differently named versions of the same function into a single function utilizing a default argument. gcc/cp/ChangeLog: * cp-tree.h (cp_build_qualified_type_real): Rename to ... (cp_build_qualified_type): ... this. Give its last parameter a default argument. Remove macro of the same name. * decl.cc (grokdeclarator): Adjust accordingly. * pt.cc (tsubst_aggr_type): Likewise. (rebuild_function_or_method_type): Likewise. (tsubst): Likewise. (maybe_dependent_member_ref): Likewise. (unify): Likewise. * tree.cc (cp_build_qualified_type_real): Rename to ... (cp_build_qualified_type): ... this. Adjust accordingly.
2022-05-31c++: document comp_template_args's default argsPatrick Palka1-1/+1
In passing, use bool for its return type. gcc/cp/ChangeLog: * cp-tree.h (comp_template_args): Change return type to bool. * pt.cc (comp_template_args): Document default arguments. Change return type to bool and adjust returns accordingly.
2022-05-25c++: fix ICE on invalid attributes [PR96637]Marek Polacek1-0/+1
When chaining attributes, attr_chainon should be used rather than plain chainon, so that we don't end up with a TREE_LIST where one of the elements is error_mark_node, which causes problems. parser.cc has already been fixed to use attr_chainon, but decl.cc has not. Until now. PR c++/96637 gcc/cp/ChangeLog: * cp-tree.h (attr_chainon): Declare. * decl.cc (start_decl): Use attr_chainon. (grokdeclarator): Likewise. * parser.cc (cp_parser_statement): No longer static. gcc/testsuite/ChangeLog: * g++.dg/parse/error64.C: New test.
2022-05-24c++: set TYPE_CANONICAL for more template typesPatrick Palka1-0/+1
When forming a class template specialization, lookup_template_class uses structural equality for the specialized type whenever one of its template arguments uses structural equality. This is the sensible thing to do in a vacuum, but given that we already effectively deduplicate class specializations via the type_specializations table, we ought to be able to safely assume that each class specialization is unique and therefore canonical, regardless of the canonicity of the template arguments. To that end this patch makes us use the canonical type machinery for all type specializations, except for the case where a PARM_DECL appears in the template arguments (this special case was recently added by r12-3766-g72394d38d929c7). Additionally, this patch makes us use the canonical type machinery for TEMPLATE_TEMPLATE_PARMs and BOUND_TEMPLATE_TEMPLATE_PARMs, by extending canonical_type_parameter appropriately. A comment in tsubst says it's unsafe to set TYPE_CANONICAL for a lowered TEMPLATE_TEMPLATE_PARM, but I'm not sure this is true anymore. According to Jason, this comment (from r120341) became obsolete when later that year r129844 started to substitute the template parms of ttps. Note that r10-7817-ga6f400239d792d recently changed process_template_parm to clear TYPE_CANONICAL for TEMPLATE_TEMPLATE_PARM consistent with the tsubst comment; this patch changes both functions to set instead of clear TYPE_CANONICAL for ttps. These changes improve compile time of template-heavy code by around 10% for me (with a release compiler). For instance, compile time for the libstdc++ test std/ranges/adaptors/all.cc drops from 1.45s to 1.25s, and for the range-v3 test test/view/zip.cpp from 5.38s to 4.88s. The total number of calls to structural_comptypes for the latter test drops from 10.5M to 1.8M. Memory use is unaffected (as expected). The new testcase verifies we check the r12-3766 PARM_DECL special case in bind_template_template_parm too. gcc/cp/ChangeLog: * cp-tree.h (any_template_arguments_need_structural_equality_p): Declare. * pt.cc (struct ctp_hasher): Define. (ctp_table): Define. (canonical_type_parameter): Use it. (process_template_parm): Set TYPE_CANONICAL for TEMPLATE_TEMPLATE_PARM too. (lookup_template_class_1): Remove now outdated comment for the any_template_arguments_need_structural_equality_p test. (tsubst) <case TEMPLATE_TEMPLATE_PARM, etc>: Don't specifically clear TYPE_CANONICAL for ttps. Set TYPE_CANONICAL on the substituted type later. (any_template_arguments_need_structural_equality_p): Return true for any_targ_node. Don't return true just because a template argument uses structural equality. Add comment for the PARM_DECL special case. (rewrite_template_parm): Set TYPE_CANONICAL on the rewritten parm's type later. * tree.cc (bind_template_template_parm): Set TYPE_CANONICAL when safe to do so. * typeck.cc (structural_comptypes) [check_alias]: Increment processing_template_decl before checking dependent_alias_template_spec_p. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-52830a.C: New test.
2022-05-12c++: tighten TMPL_ARGS_LEVEL macroPatrick Palka1-1/+2
This patch makes TMPL_ARGS_LEVEL verify the level argument is valid when the one-dimensional vector case. Doing so uncovered a couple of latent issues: in try_class_unification, we weren't correctly copying targs when it's two-dimensional, and in unify_pack_expansion it seems an inequality test needs to be reversed. This patch fixes both issues, and in passing makes the former function free the temporary copy of targs. gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. * pt.cc (try_class_unification): Correctly copy multidimensional targs. Free the copy of targs. (unify_pack_expansion): Fix level comparison.
2022-05-11c++: lambda template in requires [PR105541]Jason Merrill1-5/+7
Since the patch for PR103408, the template parameters for the lambda in this test have level 1 instead of 2, and we were treating null template args as 1 level of arguments, so tsubst_template_parms decided it had nothing to do. Fixed by distinguishing between <> and no args at all, which is what we have in our "substitution" in a requires-expression. PR c++/105541 gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_DEPTH): 0 for null args. * parser.cc (cp_parser_enclosed_template_argument_list): Use 0-length TREE_VEC for <>. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-requires1.C: New test.
2022-05-11[c++] Add module attachmentNathan Sidwell1-7/+7
This adds module attachment as a distinct flag to 'in module purview'. A declaration may have neither or both (as before), but can also have just the 'in [named-module] purview', which was previously not representable. This new state allows some cleanup of redeclarations (particularly the builtins), which was a little warty. Some other internal APIs get similarly clarified. gcc/cp/ * cp-tree.h (DECL_MODULE_ATTACH_P): New. (struct lang_decl_base): Add module_attach_p flag. * decl.cc (duplicate_decls): Rework module redeclaration checking. * module.cc (trees_out::lang_decl_bools): Write attach flag. (trees_in::lang_decl_bools): ... and read it back. (trees_out::decl_value): Rework module attachment handling. (trees_in::decl_value): Rename local var to reflect meaning. (trees_in::key_mergeable): Likewise. (get_originating_module): Use DECL_MODULE_ATTACH_P. No need to special-case mangling. (module_may_redeclare): Reimplement. (set_originating_module): Deal with attachment. * name-lookup.cc (maybe_record_mergeable_decl): Deal with attachment. (mergeable_namespace_slots): Likewise. (do_nonmember_using_decl): Likewise. * name-lookup.h (mergeable_namespace_slots): Adjust parm meaning. * ptree.cc (cxx_print_decl): Adjust purview & attach printing.
2022-05-10c++: Remove SET_PACK_EXPANSION_PATTERN / SET_ARGUMENT_PACK_ARGSPatrick Palka1-16/+0
Unlike in C, in C++ the conditional operator yields an lvalue if both branches are lvalues, so these setter macros are unnecessary; we can just assign to PACK_EXPANSION_PATTERN and ARGUMENT_PACK_ARGS directly. gcc/cp/ChangeLog: * coroutines.cc (instantiate_coro_traits): Adjust accordingly. * cp-tree.def: Remove mention of SET_PACK_EXPANSION_PATTERN. * cp-tree.h (SET_PACK_EXPANSION_PATTERN): Remove. (SET_ARGUMENT_PACK_ARGS): Remove. * module.cc (trees_in::tree_node): Adjust accordingly. * parser.cc (make_char_string_pack): Likewise. (make_string_pack): Likewise. * pt.cc (make_pack_expansion): Likewise. (template_parm_to_arg): Likewise. (coerce_template_parameter_pack): Likewise. (extract_fnparm_pack): Likewise. (extract_locals_r): Likewise. (make_argument_pack): Likewise. (tsubst_argument_pack): Likewise. (lookup_init_capture_pack): Likewise. (type_unification_real): Likewise. (unify_pack_expansion): Likewise. (tsubst_initializer_list): Likewise.
2022-05-10c++: Harden *_PACK_EXPANSION and *_ARGUMENT_PACK macrosPatrick Palka1-12/+22
This makes the accessor macros for TYPE_/EXPR_PACK_EXPANSION and TYPE_/NONTYPE_ARGUMENT_PACK check the tree code of the argument. gcc/cp/ChangeLog: * cp-tree.h (PACK_EXPANSION_CHECK): Define. (PACK_EXPANSION_PATTERN): Use PACK_EXPANSION_CHECK. (SET_PACK_EXPANSION_PATTERN): Likewise. (PACK_EXPANSION_PARAMETER_PACKS): Likewise. (PACK_EXPANSION_EXTRA_ARGS): Likewise. (PACK_EXPANSION_LOCAL_P): Likewise. (PACK_EXPANSION_SIZEOF_P): Likewise. (PACK_EXPANSION_AUTO_P): Likewise. (PACK_EXPANSION_FORCE_EXTRA_ARGS_P): Likewise. (ARGUMENT_PACK_CHECK): Define. (ARGUMENT_PACK_ARGS): Use ARGUMENT_PACK_CHECK. (SET_ARGUMENT_PACK_ARGS): Likewise. * parser.cc (cp_parser_sizeof_pack): Check for error_mark_node before setting PACK_EXPANSION_SIZEOF_P.
2022-05-10[c++] Disambiguate ModuleKind flagsNathan Sidwell1-28/+23
In modules, 'attached to global module' nearly always means 'not in module purview'. Also the implementation treats, 'in global module && in module purview' as meaning 'header unit'. The ModuleKind flags reflected that. The 'nearly always' means there are cases that the first condition is not invariant, and that of course invalidates the second equivalence. This disambiguates the ModuleKind flags to allow that 'not quite', and separate out header-unitness from the GMF & purview flags combination. 1) Separate out named-module vs header-unit from the MODULE/GLOBAL flags. 2) Replace the MODULE/GLOBAL flags with PURVIEW & ATTACH flags. 3) Adjust the parser state handling. Lays ground-work for language-declaration changes. gcc/cp/ * cp-tree.h (enum module_kind_bits): Disambiguate purview, attach, named module vs header-unit. (global_purview_p, not_module_p): Delete. (named_module_p): New. (header_module_p, module_purview_p): Adjust. (module_attach_p, named_module_purview_p): New. * decl.cc (duplicate_decls): Adjust. * module.cc (declare_module, preprocessed_module): Adjust. * name-lookup.cc (init_global_partition): Adjust. (get_fixed_binding_slot, pushdecl): Adjust. * parser.cc (cp_parser_module_declaration): Adjust. (cp_parser_import_declaration, cp_parser_declaration): Adjust.
2022-05-10c++: fix arm-eabi crash building libstdc++ [PR105529]Jason Merrill1-0/+1
My recent change to cxx_eval_store_expression asserts that the target and value can only end up having different types in the case of an empty base; this was crashing arm-eabi compilers because in that ABI [cd]tors return *this, and weren't converting it to void* first. This also shares the 'return this' code between the three places it occurs. Thanks to Marek for the tests. PR c++/105529 gcc/cp/ChangeLog: * decl.cc (maybe_return_this): Replace... (finish_constructor_body, finish_destructor_body): ...these. (finish_function_body): Call it. * optimize.cc (build_delete_destructor_body): Call it. * cp-tree.h (maybe_return_this): Declare. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-dtor13.C: New test. * g++.dg/cpp2a/constexpr-dtor14.C: New test.
2022-05-09c++: constexpr init of union sub-aggr w/ base [PR105491]Patrick Palka1-1/+2
Here ever since r10-7313-gb599bf9d6d1e18, reduced_constant_expression_p in C++11/14 is rejecting the marked sub-aggregate initializer (of type S) W w = {.D.2445={.s={.D.2387={.m=0}, .b=0}}}; ^ ultimately because said initializer has CONSTRUCTOR_NO_CLEARING set, hence the function must verify that all fields of S are initialized. And before C++17 it doesn't expect to see base class fields (since next_initializable_field skips over them), so the presence thereof causes r_c_e_p to return false. The reason r10-7313-gb599bf9d6d1e18 causes this is because in that commit we began using CONSTRUCTOR_NO_CLEARING to precisely track whether we're in middle of activating a union member. This ends up affecting clear_no_implicit_zero, which recurses into sub-aggregate initializers only if the outer initializer has CONSTRUCTOR_NO_CLEARING set. After that commit, the outer union initializer above no longer has the flag set at this point and so clear_no_implicit_zero no longer recurses into the marked inner initializer. But arguably r_c_e_p should be able to accept the marked initializer regardless of whether CONSTRUCTOR_NO_CLEARING is set. The primary bug therefore seems to be that r_c_e_p relies on next_initializable_field which skips over base class fields in C++11/14. To fix this, this patch introduces a new helper function next_subobject_field which is like next_initializable_field except that it never skips base class fields, and makes r_c_e_p use it. This patch then renames next_initializable_field to next_aggregate_field (and makes it skip over vptr fields again). PR c++/105491 gcc/cp/ChangeLog: * call.cc (field_in_pset): Adjust after next_initializable_field renaming. (build_aggr_conv): Likewise. (convert_like_internal): Likewise. (type_has_extended_temps): Likewise. * class.cc (default_init_uninitialized_part): Likewise. (finish_struct): Likewise. * constexpr.cc (cx_check_missing_mem_inits): Likewise. (reduced_constant_expression_p): Use next_subobject_field instead. * cp-gimplify.cc (get_source_location_impl_type): Adjust after next_initializable_field renaming. (fold_builtin_source_location): Likewise. * cp-tree.h (next_initializable_field): Rename to ... (next_aggregate_field): ... this. (next_subobject_field): Declare. * decl.cc (next_aggregate_field): Renamed from ... (next_initializable_field): ... this. Skip over vptr fields again. (next_subobject_field): Define. (reshape_init_class): Adjust after next_initializable_field renaming. * init.cc (build_value_init_noctor): Likewise. (emit_mem_initializers): Likewise. * lambda.cc (build_capture_proxy): Likewise. * method.cc (build_comparison_op): Likewise. * pt.cc (maybe_aggr_guide): Likewise. * tree.cc (structural_type_p): Likewise. * typeck2.cc (split_nonconstant_init_1): Likewise. (digest_init_r): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-union7.C: New test. * g++.dg/cpp0x/constexpr-union7a.C: New test. * g++.dg/cpp2a/constinit17.C: New test.
2022-05-09[c++][NFC] Rename Attached to KeyedNathan Sidwell1-5/+5
With modules, certain decls are 'scoped' with another decl. I chose the name 'attached', but that has become something specific in the modules context, so is no longer a good name. The alternative name I considered was 'keyed', but we already had the concept of a key virtual function (from the ABI), which is why I went with 'attached'. However, I think 'keyed' is the less worse name. I think there's less chance of confusion. gcc/cp/ * cp-tree.h (DECL_MODULE_KEYED_DECLS_P): Renamed from DECL_MODULE_ATTACHMENTS_P. (struct lane_decl_base): Rename module_attached_p to module_keyed_decls_p. (maybe_key_decl): Renamed from maybe_attach_decl. * lambda.cc (record_lambda_scope): Adjust. * lex.cc (cxx_dup_lang_specific_decl): Adjust. * module.cc (keyed_map_t, keyed_table): Renamed from attached_map_t, attached_table. (enum merge_kind): Rename MK_attached to MK_keyed. (trees_out::lang_decl_bools): Adjust. (trees_in::lang_decl_bools): Adjust. (trees_in::decl_value): Adjust. (trees_out::get_merge_kind): Adjust. (trees_out::key_mergeable): Adjust. (trees_in::key_mergeable): Adjust. (maybe_key_decl): Rename from maybe_attach_decl. (direct_import): Adjust. (fini_modules): Adjust.
2022-05-04c++: Remove cdtor_labelJason Merrill1-14/+0
Jakub pointed out that cdtor_label is unnecessary, we should get all the desired semantics with a normal return. gcc/cp/ChangeLog: * cp-tree.h (struct language_function): Remove x_cdtor_label. (cdtor_label, LABEL_DECL_CDTOR): Remove. * constexpr.cc (returns): Don't check LABEL_DECL_CDTOR. (cxx_eval_constant_expression): Don't call returns. * decl.cc (check_goto): Don't check cdtor_label. (start_preparsed_function): And don't set it. (finish_constructor_body, finish_destructor_body): Remove. (finish_function_body): Don't call them. * typeck.cc (check_return_expr): Handle cdtor_returns_this here. * semantics.cc (finish_return_stmt): Not here.
2022-05-03c++: make finish_non_static_data_member SFINAE enabled [PR105351]Patrick Palka1-1/+2
Here since finish_non_static_data_member isn't SFINAE enabled, we incorrectly emit an error when considering the first overload rather than silently discarding it: sfinae33.C: In substitution of ‘template<class T> A<T::value> f() [with T = B]’: sfinae33.C:11:7: required from here sfinae33.C:5:31: error: invalid use of non-static data member ‘B::value’ 5 | template<class T> A<T::value> f(); | ^ This patch makes the function SFINAE enabled in the usual way: give it a complain parameter, check it before emitting an error, and pass it through appropriately. PR c++/105351 gcc/cp/ChangeLog: * cp-tree.h (finish_non_static_data_member): Add defaulted complain parameter. * pt.cc (tsubst_copy_and_build): Pass complain to finish_non_static_data_member. * semantics.cc (finish_non_static_data_member): Respect complain parameter. (finish_qualified_id_expr): Pass complain to finish_non_static_data_member. gcc/testsuite/ChangeLog: * g++.dg/template/sfinae33.C: New test.
2022-05-02c++: uses_template_parms cleanupsMarek Polacek1-1/+1
gcc/cp/ChangeLog: * cp-tree.h (uses_template_parms): Adjust declaration. * pt.cc (uses_template_parms): Return bool. Use a RAII sentinel.
2022-04-29c++: alias CTAD and member alias templates [PR104470]Jason Merrill1-0/+1
In this testcase, we were trying to substitute into variant<Foo<T>>::__accepted_type, but failed to look it up because variant<Foo<T>> doesn't exist. In other cases we already rewrite such things into a dependent reference; we need to do that for alias templates as well. This caused some testsuite regressions on alias uses outside of deduction guides, so I've made all of this rewriting conditional on a new tf_dguide tsubst flag. PR c++/104470 gcc/cp/ChangeLog: * cp-tree.h (enum tsubst_flags): Add tf_dguide. * pt.cc (tsubst_aggr_type): Check it. (tsubst_baselink, tsubst_copy): Check it. (maybe_dependent_member_ref): Check it. (instantiate_alias_template): Handle it. (build_deduction_guide): Set it. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/explicit11.C: Second example also ill-formed. * g++.dg/cpp2a/class-deduction-alias12.C: New test.
2022-04-13c++: local fn and generic lambda [PR97219]Jason Merrill1-0/+1
When instantiating the op() for a generic lambda, we can no longer do name lookup inside function scopes enclosing the lambda, so we need to remember the lookup result from processing the definition of the lambda. So the code in finish_call_expr to throw away the lookup result and instead look it up again at instantiation time needs to be adjusted. The approach I take is to only discard the result if the local extern comes from dependent scope; once the enclosing function template is instantiated and we're regenerating the lambda, then we can remember the result of lookup. We also need any default arguments to be instantiated at that point. PR c++/97219 gcc/cp/ChangeLog: * name-lookup.cc (dependent_local_decl_p): New. * cp-tree.h (dependent_local_decl_p): Declare. * semantics.cc (finish_call_expr): Use it. * pt.cc (tsubst_arg_types): Also substitute default args for local externs. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/lambda-generic-local-fn1.C: New test.