From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00641c01.pphosted.com (mx0b-00641c01.pphosted.com [205.220.177.146]) by sourceware.org (Postfix) with ESMTPS id 51359385E83E; Thu, 2 May 2024 20:16:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51359385E83E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=gcc.gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 51359385E83E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.177.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714680992; cv=none; b=ce40TXPGsuaqYgPhYLKPJcO/CrcIkowu2Dk0sjVzXiQ3mzSfY/QIFTf/bVlCsgPvP8xUe81LlWYZ+Z1RSxcBMkrvUwshmPXX3vChZEbVKYoDfZcQ9T4aX4iGBCbHOhhTZGkZgKxoiNPGeLifejXm+lJlEfyNqNsSb8GeNf/zFaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714680992; c=relaxed/simple; bh=Q14vAr/HWZqqjWJF9EO6b4j2A+CMCdTdIWw/oWXuZyg=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=AP4zhsR9j2ovf3mUhnFzuhPtVEHdGm37qfe+ads8YTylx1XXgkfCK9vLzokXw8zR2ZD7uHMhWRlJz8ShH4vnKHVuCMvLLtIHdXDqIYseRqDSRpY519Vc2DUW3o1V0X4aK2jI9U6PHEvad1k1rRfRRnPDt4uSDSmPPedIO5PW2YU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247480.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 442JtKh8027800; Thu, 2 May 2024 20:16:04 GMT Received: from mxout22.s.uw.edu (mxout22.s.uw.edu [128.95.242.222]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3xvf559kng-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 May 2024 20:16:03 +0000 (GMT) Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout22.s.uw.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 442KFvIr013914 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 2 May 2024 13:15:57 -0700 X-Auth-Received: from ken-20xw00qgus.dhcp4.washington.edu ([10.154.75.239]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 442KFqmk009550 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 2 May 2024 13:15:57 -0700 X-UW-Orig-Sender: kmatsui@smtp.washington.edu From: Ken Matsui To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, jason@redhat.com, Ken Matsui Subject: [PATCH v18 23/26] c++: Implement __is_invocable built-in trait Date: Thu, 2 May 2024 13:12:24 -0700 Message-ID: <20240502201549.459244-24-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240502201549.459244-1-kmatsui@gcc.gnu.org> References: <20240502184529.97337-1-kmatsui@gcc.gnu.org> <20240502201549.459244-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: wOD5-iAGWiCn5I0dpPlk02ZdVFVKhDzc X-Proofpoint-ORIG-GUID: wOD5-iAGWiCn5I0dpPlk02ZdVFVKhDzc X-outbound-smtp: 1 x-header: uw-relay-non-domains X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-02_12,2024-05-02_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 mlxscore=0 priorityscore=1501 clxscore=1034 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2405020132 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NEUTRAL,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This patch implements built-in trait for std::is_invocable. gcc/cp/ChangeLog: * cp-trait.def: Define __is_invocable. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. * cp-tree.h (build_invoke): New function. * method.cc (build_invoke): New function. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __is_invocable. * g++.dg/ext/is_invocable1.C: New test. * g++.dg/ext/is_invocable2.C: New test. * g++.dg/ext/is_invocable3.C: New test. * g++.dg/ext/is_invocable4.C: New test. Signed-off-by: Ken Matsui --- gcc/cp/constraint.cc | 6 + gcc/cp/cp-trait.def | 1 + gcc/cp/cp-tree.h | 2 + gcc/cp/method.cc | 134 +++++++++ gcc/cp/semantics.cc | 5 + gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 + gcc/testsuite/g++.dg/ext/is_invocable1.C | 349 +++++++++++++++++++++++ gcc/testsuite/g++.dg/ext/is_invocable2.C | 139 +++++++++ gcc/testsuite/g++.dg/ext/is_invocable3.C | 51 ++++ gcc/testsuite/g++.dg/ext/is_invocable4.C | 33 +++ 10 files changed, 723 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable1.C create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable2.C create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable3.C create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable4.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index c28d7bf428e..6d14ef7dcc7 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3792,6 +3792,12 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_FUNCTION: inform (loc, " %qT is not a function", t1); break; + case CPTK_IS_INVOCABLE: + if (!t2) + inform (loc, " %qT is not invocable", t1); + else + inform (loc, " %qT is not invocable by %qE", t1, t2); + break; case CPTK_IS_LAYOUT_COMPATIBLE: inform (loc, " %qT is not layout compatible with %qT", t1, t2); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index b1c875a6e7d..4e420d5390a 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -75,6 +75,7 @@ DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1) DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1) DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1) DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1) +DEFTRAIT_EXPR (IS_INVOCABLE, "__is_invocable", -1) DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2) DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1) DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1938ada0268..83dc20e1130 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7338,6 +7338,8 @@ extern tree get_copy_assign (tree); extern tree get_default_ctor (tree); extern tree get_dtor (tree, tsubst_flags_t); extern tree build_stub_object (tree); +extern tree build_invoke (tree, const_tree, + tsubst_flags_t); extern tree strip_inheriting_ctors (tree); extern tree inherited_ctor_binfo (tree); extern bool base_ctor_omit_inherited_parms (tree); diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 08a3d34fb01..faf932258e6 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -1928,6 +1928,140 @@ build_trait_object (tree type) return build_stub_object (type); } +/* [func.require] Build an expression of INVOKE(FN_TYPE, ARG_TYPES...). If the + given is not invocable, returns error_mark_node. */ + +tree +build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain) +{ + if (error_operand_p (fn_type) || error_operand_p (arg_types)) + return error_mark_node; + + gcc_assert (TYPE_P (fn_type)); + gcc_assert (TREE_CODE (arg_types) == TREE_VEC); + + /* Access check is required to determine if the given is invocable. */ + deferring_access_check_sentinel acs (dk_no_deferred); + + /* INVOKE is an unevaluated context. */ + cp_unevaluated cp_uneval_guard; + + bool is_ptrdatamem; + bool is_ptrmemfunc; + if (TREE_CODE (fn_type) == REFERENCE_TYPE) + { + tree deref_fn_type = TREE_TYPE (fn_type); + is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type); + is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type); + + /* Dereference fn_type if it is a pointer to member. */ + if (is_ptrdatamem || is_ptrmemfunc) + fn_type = deref_fn_type; + } + else + { + is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type); + is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type); + } + + if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1) + { + if (complain & tf_error) + error ("pointer to data member type %qT can only be invoked with " + "one argument", fn_type); + return error_mark_node; + } + + if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0) + { + if (complain & tf_error) + error ("pointer to member function type %qT must be invoked with " + "at least one argument", fn_type); + return error_mark_node; + } + + /* Construct an expression of a pointer to member. */ + tree ptrmem_expr; + if (is_ptrdatamem || is_ptrmemfunc) + { + tree datum_type = TREE_VEC_ELT (arg_types, 0); + + /* datum must be a class type or a reference/pointer to a class type. */ + if (!(CLASS_TYPE_P (datum_type) + || ((TYPE_REF_P (datum_type) || POINTER_TYPE_P (datum_type)) + && CLASS_TYPE_P (TREE_TYPE (datum_type))))) + { + if (complain & tf_error) + error ("first argument type %qT of a pointer to member must be" + "a class type or a reference/pointer to a class type", + datum_type); + return error_mark_node; + } + + bool is_refwrap = false; + if (CLASS_TYPE_P (datum_type)) + { + /* 1.2 & 1.5: Handle std::reference_wrapper. */ + tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type)); + if (decl_in_std_namespace_p (datum_decl)) + { + const_tree name = DECL_NAME (datum_decl); + if (name && (id_equal (name, "reference_wrapper"))) + { + /* Retrieve T from std::reference_wrapper, + i.e., decltype(datum.get()). */ + datum_type = TREE_VEC_ELT (TYPE_TI_ARGS (datum_type), 0); + is_refwrap = true; + } + } + } + + tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type); + const bool ptrmem_is_base_of_datum = + (NON_UNION_CLASS_TYPE_P (ptrmem_class_type) + && NON_UNION_CLASS_TYPE_P (datum_type) + && (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type, + datum_type) + || DERIVED_FROM_P (ptrmem_class_type, datum_type))); + + tree datum_expr = build_trait_object (datum_type); + if (!ptrmem_is_base_of_datum && !is_refwrap && !TYPE_REF_P (datum_type)) + /* 1.3 & 1.6: Try to dereference datum_expr. */ + datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr, + RO_UNARY_STAR, NULL_TREE, complain); + /* 1.1, 1.2, 1.4, & 1.5: Otherwise. */ + + tree fn_expr = build_trait_object (fn_type); + ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain); + + if (error_operand_p (ptrmem_expr)) + return error_mark_node; + + if (is_ptrdatamem) + return ptrmem_expr; + } + + /* Construct expressions for arguments to INVOKE. For a pointer to member + function, the first argument, which is the object, is not arguments to + the function. */ + releasing_vec args; + for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i) + { + tree arg_type = TREE_VEC_ELT (arg_types, i); + tree arg = build_trait_object (arg_type); + vec_safe_push (args, arg); + } + + tree invoke_expr; + if (is_ptrmemfunc) + invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args, + complain); + else /* 1.7. */ + invoke_expr = finish_call_expr (build_trait_object (fn_type), &args, false, + false, complain); + return invoke_expr; +} + /* Determine which function will be called when looking up NAME in TYPE, called with a single ARGTYPE argument, or no argument if ARGTYPE is null. FLAGS and COMPLAIN are as for build_new_method_call. diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index a293e0fc23e..55c2ec4b5b1 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12561,6 +12561,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_FUNCTION: return type_code1 == FUNCTION_TYPE; + case CPTK_IS_INVOCABLE: + return !error_operand_p (build_invoke (type1, type2, tf_none)); + case CPTK_IS_LAYOUT_COMPATIBLE: return layout_compatible_type_p (type1, type2); @@ -12719,6 +12722,7 @@ same_type_ref_bind_p (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_CONSTRUCTIBLE: case CPTK_IS_NOTHROW_CONSTRUCTIBLE: case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE: + case CPTK_IS_INVOCABLE: case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY: case CPTK_REF_CONVERTS_FROM_TEMPORARY: to = type1; @@ -12821,6 +12825,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_CONSTRUCTIBLE: case CPTK_IS_CONVERTIBLE: + case CPTK_IS_INVOCABLE: case CPTK_IS_NOTHROW_CONSTRUCTIBLE: case CPTK_IS_NOTHROW_CONVERTIBLE: case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE: diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index bcaa56a7bc5..d1c66267855 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -107,6 +107,9 @@ #if !__has_builtin (__is_function) # error "__has_builtin (__is_function) failed" #endif +#if !__has_builtin (__is_invocable) +# error "__has_builtin (__is_invocable) failed" +#endif #if !__has_builtin (__is_layout_compatible) # error "__has_builtin (__is_layout_compatible) failed" #endif diff --git a/gcc/testsuite/g++.dg/ext/is_invocable1.C b/gcc/testsuite/g++.dg/ext/is_invocable1.C new file mode 100644 index 00000000000..d21ae1d1958 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_invocable1.C @@ -0,0 +1,349 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +using func_type_v0 = void(*)(); + +SA( __is_invocable( func_type_v0 ) ); +SA( ! __is_invocable( func_type_v0, int ) ); + +using func_type_i0 = int(*)(); + +SA( __is_invocable( func_type_i0 ) ); +SA( ! __is_invocable( func_type_i0, int ) ); + +using func_type_l0 = int&(*)(); + +SA( __is_invocable( func_type_l0 ) ); +SA( ! __is_invocable( func_type_l0(int) ) ); + +using func_type_ii = int(*)(int); + +SA( ! __is_invocable( func_type_ii ) ); +SA( __is_invocable( func_type_ii, int ) ); + +using func_type_il = int(*)(int&); + +SA( ! __is_invocable( func_type_il ) ); +SA( ! __is_invocable( func_type_il, int ) ); +SA( __is_invocable( func_type_il, int& ) ); + +using func_type_ir = int(*)(int&&); + +SA( ! __is_invocable( func_type_ir ) ); +SA( ! __is_invocable( func_type_ir, int& ) ); +SA( __is_invocable( func_type_ir, int ) ); +SA( __is_invocable( func_type_ir, int&& ) ); + +struct A { }; + +using mem_type_i = int A::*; + +SA( ! __is_invocable( mem_type_i ) ); +SA( ! __is_invocable( mem_type_i, int ) ); +SA( ! __is_invocable( mem_type_i, int* ) ); +SA( ! __is_invocable( mem_type_i, int& ) ); +SA( ! __is_invocable( mem_type_i, int&& ) ); +SA( __is_invocable( mem_type_i, A ) ); +SA( __is_invocable( mem_type_i, A* ) ); +SA( __is_invocable( mem_type_i, A& ) ); +SA( __is_invocable( mem_type_i, A&& ) ); +SA( __is_invocable( mem_type_i, const A& ) ); +SA( ! __is_invocable( mem_type_i, A&, int ) ); + +using memfun_type_i = int (A::*)(); + +SA( ! __is_invocable( memfun_type_i ) ); +SA( ! __is_invocable( memfun_type_i, int ) ); +SA( ! __is_invocable( memfun_type_i, int* ) ); +SA( ! __is_invocable( memfun_type_i, int& ) ); +SA( ! __is_invocable( memfun_type_i, int&& ) ); +SA( __is_invocable( memfun_type_i, A ) ); +SA( __is_invocable( memfun_type_i, A* ) ); +SA( __is_invocable( memfun_type_i, A& ) ); +SA( __is_invocable( memfun_type_i, A&& ) ); +SA( ! __is_invocable( memfun_type_i, const A& ) ); +SA( ! __is_invocable( memfun_type_i, A&, int ) ); + +using memfun_type_ic = int (A::*)() const; + +SA( ! __is_invocable( memfun_type_ic ) ); +SA( ! __is_invocable( memfun_type_ic, int ) ); +SA( ! __is_invocable( memfun_type_ic, int& ) ); +SA( __is_invocable( memfun_type_ic, A& ) ); +SA( __is_invocable( memfun_type_ic, A* ) ); +SA( ! __is_invocable( memfun_type_ic, A&, int ) ); +SA( ! __is_invocable( memfun_type_ic, A*, int& ) ); +SA( __is_invocable( memfun_type_ic, const A& ) ); +SA( __is_invocable( memfun_type_ic, const A* ) ); +SA( ! __is_invocable( memfun_type_ic, const A&, int& ) ); +SA( ! __is_invocable( memfun_type_ic, const A*, int ) ); + +using memfun_type_iic = int& (A::*)(int&) const; + +SA( ! __is_invocable( memfun_type_iic ) ); +SA( ! __is_invocable( memfun_type_iic, int ) ); +SA( ! __is_invocable( memfun_type_iic, int& ) ); +SA( ! __is_invocable( memfun_type_iic, A&, int ) ); +SA( __is_invocable( memfun_type_iic, A&, int& ) ); +SA( ! __is_invocable( memfun_type_iic, A*, int ) ); +SA( __is_invocable( memfun_type_iic, A*, int& ) ); +SA( ! __is_invocable( memfun_type_iic, const A&, int ) ); +SA( ! __is_invocable( memfun_type_iic, const A&, int&, int ) ); +SA( __is_invocable( memfun_type_iic, const A&, int& ) ); +SA( __is_invocable( memfun_type_iic, const A*, int& ) ); + +struct B { + int& operator()(); + long& operator()() const; + bool& operator()(int); +private: + void operator()(int, int); +}; +using CB = const B; + +SA( __is_invocable( B ) ); +SA( __is_invocable( B& ) ); +SA( __is_invocable( B&& ) ); +SA( ! __is_invocable( B* ) ); +SA( __is_invocable( CB ) ); +SA( __is_invocable( CB& ) ); +SA( ! __is_invocable( CB* ) ); + +SA( __is_invocable( B, int ) ); +SA( __is_invocable( B&, int ) ); +SA( __is_invocable( B&&, int ) ); +SA( ! __is_invocable( B*, int ) ); +SA( ! __is_invocable( CB, int ) ); +SA( ! __is_invocable( CB&, int ) ); +SA( ! __is_invocable( CB*, int ) ); + +SA( ! __is_invocable( B, int, int ) ); +SA( ! __is_invocable( B&, int, int ) ); +SA( ! __is_invocable( B&&, int, int ) ); +SA( ! __is_invocable( B*, int, int ) ); +SA( ! __is_invocable( CB, int, int ) ); +SA( ! __is_invocable( CB&, int, int ) ); +SA( ! __is_invocable( CB*, int, int ) ); + +struct C : B { int& operator()() = delete; }; +using CC = const C; + +SA( ! __is_invocable( C ) ); +SA( ! __is_invocable( C& ) ); +SA( ! __is_invocable( C&& ) ); +SA( ! __is_invocable( C* ) ); +SA( ! __is_invocable( CC ) ); +SA( ! __is_invocable( CC& ) ); +SA( ! __is_invocable( CC* ) ); + +struct D { B operator*(); }; +using CD = const D; + +SA( ! __is_invocable( D ) ); + +struct E { void v(); }; +using CE = const E; + +SA( ! __is_invocable( E ) ); +SA( ! __is_invocable( void (E::*)() ) ); +SA( __is_invocable( void (E::*)(), E ) ); +SA( __is_invocable( void (E::*)(), E* ) ); +SA( ! __is_invocable( void (E::*)(), CE ) ); + +struct F : E {}; +using CF = const F; + +SA( ! __is_invocable( F ) ); +SA( __is_invocable( void (E::*)(), F ) ); +SA( __is_invocable( void (E::*)(), F* ) ); +SA( ! __is_invocable( void (E::*)(), CF ) ); + +struct G { E operator*(); }; +using CG = const G; + +SA( ! __is_invocable( G ) ); +SA( __is_invocable( void (E::*)(), G ) ); +SA( ! __is_invocable( void (E::*)(), G* ) ); +SA( ! __is_invocable( void (E::*)(), CG ) ); + +struct H { E& operator*(); }; +using CH = const H; + +SA( ! __is_invocable( H ) ); +SA( __is_invocable( void (E::*)(), H ) ); +SA( ! __is_invocable( void (E::*)(), H* ) ); +SA( ! __is_invocable( void (E::*)(), CH ) ); + +struct I { E&& operator*(); }; +using CI = const I; + +SA( ! __is_invocable( I ) ); +SA( __is_invocable( void (E::*)(), I ) ); +SA( ! __is_invocable( void (E::*)(), I* ) ); +SA( ! __is_invocable( void (E::*)(), CI ) ); + +struct K { E* operator*(); }; +using CK = const K; + +SA( ! __is_invocable( K ) ); +SA( ! __is_invocable( void (E::*)(), K ) ); +SA( ! __is_invocable( void (E::*)(), K* ) ); +SA( ! __is_invocable( void (E::*)(), CK ) ); + +struct L { CE operator*(); }; +using CL = const L; + +SA( ! __is_invocable( L ) ); +SA( ! __is_invocable( void (E::*)(), L ) ); +SA( ! __is_invocable( void (E::*)(), L* ) ); +SA( ! __is_invocable( void (E::*)(), CL ) ); + +struct M { + int i; +private: + long l; +}; +using CM = const M; + +SA( ! __is_invocable( M ) ); +SA( ! __is_invocable( M& ) ); +SA( ! __is_invocable( M&& ) ); +SA( ! __is_invocable( M* ) ); +SA( ! __is_invocable( CM ) ); +SA( ! __is_invocable( CM& ) ); +SA( ! __is_invocable( CM* ) ); + +SA( ! __is_invocable( int M::* ) ); +SA( __is_invocable( int M::*, M ) ); +SA( __is_invocable( int M::*, M& ) ); +SA( __is_invocable( int M::*, M&& ) ); +SA( __is_invocable( int M::*, M* ) ); +SA( __is_invocable( int M::*, CM ) ); +SA( __is_invocable( int M::*, CM& ) ); +SA( __is_invocable( int M::*, CM* ) ); +SA( ! __is_invocable( int M::*, int ) ); + +SA( ! __is_invocable( int CM::* ) ); +SA( __is_invocable( int CM::*, M ) ); +SA( __is_invocable( int CM::*, M& ) ); +SA( __is_invocable( int CM::*, M&& ) ); +SA( __is_invocable( int CM::*, M* ) ); +SA( __is_invocable( int CM::*, CM ) ); +SA( __is_invocable( int CM::*, CM& ) ); +SA( __is_invocable( int CM::*, CM* ) ); +SA( ! __is_invocable( int CM::*, int ) ); + +SA( ! __is_invocable( long M::* ) ); +SA( __is_invocable( long M::*, M ) ); +SA( __is_invocable( long M::*, M& ) ); +SA( __is_invocable( long M::*, M&& ) ); +SA( __is_invocable( long M::*, M* ) ); +SA( __is_invocable( long M::*, CM ) ); +SA( __is_invocable( long M::*, CM& ) ); +SA( __is_invocable( long M::*, CM* ) ); +SA( ! __is_invocable( long M::*, long ) ); + +SA( ! __is_invocable( long CM::* ) ); +SA( __is_invocable( long CM::*, M ) ); +SA( __is_invocable( long CM::*, M& ) ); +SA( __is_invocable( long CM::*, M&& ) ); +SA( __is_invocable( long CM::*, M* ) ); +SA( __is_invocable( long CM::*, CM ) ); +SA( __is_invocable( long CM::*, CM& ) ); +SA( __is_invocable( long CM::*, CM* ) ); +SA( ! __is_invocable( long CM::*, long ) ); + +SA( ! __is_invocable( short M::* ) ); +SA( __is_invocable( short M::*, M ) ); +SA( __is_invocable( short M::*, M& ) ); +SA( __is_invocable( short M::*, M&& ) ); +SA( __is_invocable( short M::*, M* ) ); +SA( __is_invocable( short M::*, CM ) ); +SA( __is_invocable( short M::*, CM& ) ); +SA( __is_invocable( short M::*, CM* ) ); +SA( ! __is_invocable( short M::*, short ) ); + +SA( ! __is_invocable( short CM::* ) ); +SA( __is_invocable( short CM::*, M ) ); +SA( __is_invocable( short CM::*, M& ) ); +SA( __is_invocable( short CM::*, M&& ) ); +SA( __is_invocable( short CM::*, M* ) ); +SA( __is_invocable( short CM::*, CM ) ); +SA( __is_invocable( short CM::*, CM& ) ); +SA( __is_invocable( short CM::*, CM* ) ); +SA( ! __is_invocable( short CM::*, short ) ); + +struct N { M operator*(); }; +SA( __is_invocable( int M::*, N ) ); +SA( ! __is_invocable( int M::*, N* ) ); + +struct O { M& operator*(); }; +SA( __is_invocable( int M::*, O ) ); +SA( ! __is_invocable( int M::*, O* ) ); + +struct P { M&& operator*(); }; +SA( __is_invocable( int M::*, P ) ); +SA( ! __is_invocable( int M::*, P* ) ); + +struct Q { M* operator*(); }; +SA( ! __is_invocable( int M::*, Q ) ); +SA( ! __is_invocable( int M::*, Q* ) ); + +struct R { void operator()(int = 0); }; + +SA( __is_invocable( R ) ); +SA( __is_invocable( R, int ) ); +SA( ! __is_invocable( R, int, int ) ); + +struct S { void operator()(int, ...); }; + +SA( ! __is_invocable( S ) ); +SA( __is_invocable( S, int ) ); +SA( __is_invocable( S, int, int ) ); +SA( __is_invocable( S, int, int, int ) ); + +void fn1() {} + +SA( __is_invocable( decltype(fn1) ) ); + +void fn2(int arr[10]); + +SA( __is_invocable( decltype(fn2), int[10] ) ); +SA( __is_invocable( decltype(fn2), int(&)[10] ) ); +SA( __is_invocable( decltype(fn2), int(&&)[10] ) ); +SA( ! __is_invocable( decltype(fn2), int(*)[10] ) ); +SA( ! __is_invocable( decltype(fn2), int(*&)[10] ) ); +SA( ! __is_invocable( decltype(fn2), int(*&&)[10] ) ); +SA( __is_invocable( decltype(fn2), int[] ) ); + +auto lambda = []() {}; + +SA( __is_invocable( decltype(lambda) ) ); + +template +struct can_invoke { + static constexpr bool value = __is_invocable( Func, Args... ); +}; + +SA( can_invoke::value ); + +struct T { + void func() const {} + int data; +}; + +SA( __is_invocable( decltype(&T::func)&, T& ) ); +SA( __is_invocable( decltype(&T::data)&, T& ) ); + +struct U { }; +struct V : U { U& operator*() = delete; }; +SA( __is_invocable( int U::*, V ) ); + +struct W : private U { U& operator*(); }; +SA( ! __is_invocable( int U::*, W ) ); + +struct X { int m; }; +struct Y { X& operator*(); }; +struct Z : Y { }; +SA( __is_invocable(int X::*, Z) ); diff --git a/gcc/testsuite/g++.dg/ext/is_invocable2.C b/gcc/testsuite/g++.dg/ext/is_invocable2.C new file mode 100644 index 00000000000..a68aefd3e13 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_invocable2.C @@ -0,0 +1,139 @@ +// { dg-do compile { target c++11 } } +// __is_invocable should handle std::reference_wrapper correctly. + +#include + +#define SA(X) static_assert((X),#X) + +using std::reference_wrapper; + +using func_type_v0 = void(*)(); + +SA( __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); + +using func_type_i0 = int(*)(); + +SA( __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); + +using func_type_l0 = int&(*)(); + +SA( __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper ) ); + +using func_type_ii = int(*)(int); + +SA( ! __is_invocable( reference_wrapper ) ); +SA( __is_invocable( reference_wrapper, int ) ); + +using func_type_il = int(*)(int&); + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); +SA( __is_invocable( reference_wrapper, int& ) ); + +using func_type_ir = int(*)(int&&); + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int& ) ); +SA( __is_invocable( reference_wrapper, int ) ); +SA( __is_invocable( reference_wrapper, int&& ) ); + +struct A { }; + +using mem_type_i = int A::*; + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); +SA( ! __is_invocable( reference_wrapper, int* ) ); +SA( ! __is_invocable( reference_wrapper, int& ) ); +SA( ! __is_invocable( reference_wrapper, int&& ) ); +SA( __is_invocable( reference_wrapper, A ) ); +SA( __is_invocable( reference_wrapper, A* ) ); +SA( __is_invocable( reference_wrapper, A& ) ); +SA( __is_invocable( reference_wrapper, A&& ) ); + +using memfun_type_i = int (A::*)(); + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); +SA( ! __is_invocable( reference_wrapper, int* ) ); +SA( ! __is_invocable( reference_wrapper, int& ) ); +SA( ! __is_invocable( reference_wrapper, int&& ) ); +SA( __is_invocable( reference_wrapper, A ) ); +SA( __is_invocable( reference_wrapper, A* ) ); +SA( __is_invocable( reference_wrapper, A& ) ); +SA( __is_invocable( reference_wrapper, A&& ) ); +SA( ! __is_invocable( reference_wrapper, const A& ) ); +SA( ! __is_invocable( reference_wrapper, A&, int ) ); + +using memfun_type_ic = int (A::*)() const; + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); +SA( ! __is_invocable( reference_wrapper, int& ) ); +SA( __is_invocable( reference_wrapper, A& ) ); +SA( __is_invocable( reference_wrapper, A* ) ); +SA( ! __is_invocable( reference_wrapper, A&, int ) ); +SA( ! __is_invocable( reference_wrapper, A*, int& ) ); +SA( __is_invocable( reference_wrapper, const A& ) ); +SA( __is_invocable( reference_wrapper, const A* ) ); +SA( ! __is_invocable( reference_wrapper, const A&, int& ) ); +SA( ! __is_invocable( reference_wrapper, const A*, int ) ); + +using memfun_type_iic = int& (A::*)(int&) const; + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper, int ) ); +SA( ! __is_invocable( reference_wrapper, int& ) ); +SA( ! __is_invocable( reference_wrapper, A&, int ) ); +SA( __is_invocable( reference_wrapper, A&, int& ) ); +SA( ! __is_invocable( reference_wrapper, A*, int ) ); +SA( __is_invocable( reference_wrapper, A*, int& ) ); +SA( ! __is_invocable( reference_wrapper, const A&, int ) ); +SA( ! __is_invocable( reference_wrapper, const A&, int&, int ) ); +SA( __is_invocable( reference_wrapper, const A&, int& ) ); +SA( __is_invocable( reference_wrapper, const A*, int& ) ); + +struct B { + int& operator()(); + long& operator()() const; + bool& operator()(int); +private: + void operator()(int, int); +}; +using CB = const B; + +SA( __is_invocable( reference_wrapper ) ); +SA( __is_invocable( reference_wrapper& ) ); +SA( __is_invocable( reference_wrapper&& ) ); +SA( __is_invocable( reference_wrapper ) ); +SA( __is_invocable( reference_wrapper& ) ); +SA( __is_invocable( reference_wrapper, int ) ); +SA( ! __is_invocable( reference_wrapper&, int, int ) ); + +struct C : B { int& operator()() = delete; }; +using CC = const C; + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper& ) ); +SA( ! __is_invocable( reference_wrapper&& ) ); +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper& ) ); + +struct D { B operator*(); }; +using CD = const D; + +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper& ) ); +SA( ! __is_invocable( reference_wrapper&& ) ); +SA( ! __is_invocable( reference_wrapper* ) ); +SA( ! __is_invocable( reference_wrapper ) ); +SA( ! __is_invocable( reference_wrapper* ) ); + +std::function fn = []() {}; +auto refwrap = std::ref(fn); + +SA( __is_invocable( decltype(fn) ) ); +SA( __is_invocable( decltype(refwrap) ) ); diff --git a/gcc/testsuite/g++.dg/ext/is_invocable3.C b/gcc/testsuite/g++.dg/ext/is_invocable3.C new file mode 100644 index 00000000000..8699b0a53ca --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_invocable3.C @@ -0,0 +1,51 @@ +// { dg-do compile { target c++11 } } +// __is_invocable should handle incomplete class correctly. + +#define SA(X) static_assert((X),#X) + +struct Incomplete; + +SA( ! __is_invocable( Incomplete ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( Incomplete, int ) ); // { dg-error "incomplete type" } + +SA( ! __is_invocable( int, Incomplete, int ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( int, Incomplete ) ); // { dg-error "incomplete type" } + +SA( ! __is_invocable( Incomplete, Incomplete() ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( Incomplete, Incomplete(int), int ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( Incomplete, Incomplete(int, int), int, int ) ); // { dg-error "incomplete type" } + +SA( ! __is_invocable( Incomplete, Incomplete(), int, int ) ); // { dg-error "incomplete type" } + +SA( ! __is_invocable( int(Incomplete), Incomplete ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( int(int, Incomplete), int, Incomplete ) ); // { dg-error "incomplete type" } +SA( ! __is_invocable( int(int, Incomplete), Incomplete, int ) ); // { dg-error "incomplete type" } + +SA( __is_invocable( int(Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( int(int, Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" } + +SA( __is_invocable( int(Incomplete&&), Incomplete&& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( int(int, Incomplete&&), int, Incomplete&& ) ); // { dg-bogus "incomplete type" } + +SA( __is_invocable( int(const Incomplete&&), const Incomplete&& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( int(int, const Incomplete&&), int, const Incomplete&& ) ); // { dg-bogus "incomplete type" } + +SA( __is_invocable( int(const Incomplete&), const Incomplete& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( int(int, const Incomplete&), int, const Incomplete& ) ); // { dg-bogus "incomplete type" } + +SA( __is_invocable( int(const Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( int(int, const Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" } + +SA( __is_invocable( int Incomplete::*, const Incomplete& ) ); // { dg-bogus "incomplete type" } +SA( ! __is_invocable( void (Incomplete::*)(long&), const Incomplete*, long& ) ); // { dg-bogus "incomplete type" } +SA( __is_invocable( void (Incomplete::*)(long&) const, Incomplete*, long& ) ); // { dg-bogus "incomplete type" } + +template +struct Holder { T t; }; + +SA( __is_invocable( int(Holder&), Holder& ) ); // { dg-bogus "incomplete type" } + +// Define Incomplete, which is now not incomplete. +struct Incomplete { void operator()(); }; + +SA( __is_invocable( Incomplete ) ); // { dg-bogus "incomplete type" } diff --git a/gcc/testsuite/g++.dg/ext/is_invocable4.C b/gcc/testsuite/g++.dg/ext/is_invocable4.C new file mode 100644 index 00000000000..d1efccf08f8 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_invocable4.C @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } +// Failed access check should be a substitution failure, not an error. + +#define SA(X) static_assert((X),#X) + +template +struct bool_constant { static constexpr bool value = B; }; + +template +struct is_invocable +: public bool_constant<__is_invocable(_Fn, _ArgTypes...)> +{ }; + +#if __cpp_variable_templates +template +constexpr bool is_invocable_v = __is_invocable(_Fn, _ArgTypes...); +#endif + +class Private +{ + void operator()() const + { + SA( ! is_invocable::value ); +#if __cpp_variable_templates + SA( ! is_invocable_v ); +#endif + } +}; + +SA( ! is_invocable::value ); +#if __cpp_variable_templates +SA( ! is_invocable_v ); +#endif -- 2.44.0