From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id BBB0D3846024 for ; Fri, 10 May 2024 16:12:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBB0D3846024 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.washington.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.washington.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BBB0D3846024 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715357527; cv=none; b=aDtp6UorQEecoiCE1lBH12PeZr0jz44sZccXAnguJaY84202cBaN8cn6HsCV/+hwMDsS34N0Wca24hh7R7aRj3v/WHflv+S/zk4wfQZPwlLeUlTHyat912dowAtqfBBd/MN8w9N66kXDp2vL8vXf+Vj2Qwsl10wueef5QtRTeX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715357527; c=relaxed/simple; bh=iojyjatqhgaE1sLicjd1SJPs6ts+qwS0sK5af+RAJLY=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=t8dg4eGAkm4/3g9edVnoMmB08sHh/vw2/ZisAMncheDSFWauzLcCwcpKd8h3zfQ0TypCLjT76OwhjpWlqOUNr2HpsIoI+0PvX/OFsw5jgepf2BRrc2TbtC366maQkNpOKnz/eygWxWPflbPGHgaarMAQYQBZuAeEN22284b+eac= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3c9995562fcso367281b6e.2 for ; Fri, 10 May 2024 09:12:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1715357524; x=1715962324; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=43durUZGJrXJc/IpZf/b1jvO7ZJHw5ZoHnHZ9yZ2qis=; b=eDpD9NW2bxLZM4z/GUs0zJCo+FTYw0B8W9k2Cglj76fbmb5iDgs+Z2jGBHap6zeKou ssanU5B79gWdVF04c3Fz0Ejzjq3r0DJ68vJRWu5JiPRibA4PXLEKmW6XI4TKB0rfGWOT ZnenllvF3uzPx1ixlNcO6NEniIEH3TDMJ5Hc8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715357524; x=1715962324; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=43durUZGJrXJc/IpZf/b1jvO7ZJHw5ZoHnHZ9yZ2qis=; b=TwTwTjxKHbBszijbxm29zcUqg1mcgRO8Sy7Sl5s53h8GYsgmmSSk3MXDKdB7a24XFd ZmvNWFsO2mZMOpdQRuhziDNsVtts4k9DF5W/fj26N8apBxTtgznOpfm9P6xDCIuD8FZ+ 23LJho4eP3tEGgqbr56RAtA2W5sDcVc4o9LqO8h5QhtWARF7o1yTp/H54SwLRjPgPvew MLdiEwI25u/CS8qS5yMcAlMoloBsiQRYqkKtndqCUTl+1mUncb9FpPY/k7hGWGMkcPRH eLFVApqGQb8XdPa0vC/hAR+TRIzkSYygvloCC0Uov8wlAxYC3CdnqGkHFEQC4UA764D4 RLhg== X-Forwarded-Encrypted: i=1; AJvYcCXIsosNnfht5arM5moyTCITKkYQKWe3uv3m2i3k1ahaY0yuZLxlvsV/uVxWciim+KVUSKH+7V0ACOrsYcjT8NchFRHSros= X-Gm-Message-State: AOJu0YxXVkNilvMJnMpdUB346kGMbGHXOSXxgRAdGLPlJL+TNm2px004 lqIcSPFaK6iysqW5cu3B19qGNgL/HqnyYUCBrsUWSenI8LxxJRqA3+RycRE3Y3Opp/L5zFfnq3D 8vh/5oJuIrAVQElYPw/I7VyXYjqmaBEao5nr0 X-Google-Smtp-Source: AGHT+IHR2VWnkEsafsREH0rtIN2F9t5KPdnm0yGVxv05Z/lBf+TrD28L34Nzcnl3D9ml+Uwezo3MWTBUHJC+obLCtnc= X-Received: by 2002:aca:100f:0:b0:3c9:7066:a49b with SMTP id 5614622812f47-3c99706a089mr3028856b6e.30.1715357519439; Fri, 10 May 2024 09:11:59 -0700 (PDT) MIME-Version: 1.0 References: <20240510112417.8927-21-kmatsui@gcc.gnu.org> <20240510144547.90831-21-kmatsui@gcc.gnu.org> <5a99fa9f-48d8-46cf-b4db-d84a48d5075e@redhat.com> In-Reply-To: <5a99fa9f-48d8-46cf-b4db-d84a48d5075e@redhat.com> From: Ken Matsui Date: Fri, 10 May 2024 09:11:22 -0700 Message-ID: Subject: Re: [PATCH v24 20/23] c++: Implement __is_invocable built-in trait To: Jason Merrill Cc: Ken Matsui , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: On Fri, May 10, 2024 at 8:27=E2=80=AFAM Jason Merrill wr= ote: > > On 5/10/24 10:45, Ken Matsui wrote: > > Fixed the incorrect if condition. Could you please review this again? > > > > -- >8 -- > > > > 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 | 139 +++++++++ > > 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, 728 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_poin= ter", 1) > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index 52d6841559c..8aa41f7147f 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -7340,6 +7340,8 @@ extern tree get_copy_assign (= tree); > > extern tree get_default_ctor (tree); > > extern tree get_dtor (tree, tsubst_fla= gs_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..bbe91a276e1 100644 > > --- a/gcc/cp/method.cc > > +++ b/gcc/cp/method.cc > > @@ -1928,6 +1928,145 @@ 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 compl= ain) > > +{ > > + 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) =3D=3D 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) =3D=3D REFERENCE_TYPE) > > + { > > + tree non_ref_fn_type =3D TREE_TYPE (fn_type); > > + is_ptrdatamem =3D TYPE_PTRDATAMEM_P (non_ref_fn_type); > > + is_ptrmemfunc =3D TYPE_PTRMEMFUNC_P (non_ref_fn_type); > > + > > + /* Dereference fn_type if it is a pointer to member. */ > > + if (is_ptrdatamem || is_ptrmemfunc) > > + fn_type =3D non_ref_fn_type; > > + } > > + else > > + { > > + is_ptrdatamem =3D TYPE_PTRDATAMEM_P (fn_type); > > + is_ptrmemfunc =3D TYPE_PTRMEMFUNC_P (fn_type); > > + } > > + > > + if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) !=3D 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) =3D=3D 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 =3D TREE_VEC_ELT (arg_types, 0); > > + tree non_ref_datum_type =3D datum_type; > > + if (TYPE_REF_P (datum_type)) > > + non_ref_datum_type =3D TREE_TYPE (datum_type); > > + > > + /* datum must be a class type or a pointer to a class type. */ > > + if (!CLASS_TYPE_P (non_ref_datum_type) > > + && !(POINTER_TYPE_P (non_ref_datum_type) > > + && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type)))) > > + { > > + if (complain & tf_error) > > + error ("first argument type %qT of a pointer to member must b= e a " > > + "class type or a pointer to a class type", datum_type)= ; > > + return error_mark_node; > > + } > > + > > + bool datum_is_refwrap =3D false; > > + if (CLASS_TYPE_P (non_ref_datum_type)) > > + { > > + /* 1.2 & 1.5: Handle std::reference_wrapper. */ > > + tree datum_decl =3D TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum= _type)); > > + if (decl_in_std_namespace_p (datum_decl)) > > + { > > + const_tree name =3D DECL_NAME (datum_decl); > > + if (name && (id_equal (name, "reference_wrapper"))) > > + { > > + /* Retrieve T from std::reference_wrapper, i.e., > > + decltype(datum.get()). */ > > + datum_type =3D > > + TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0); > > + datum_is_refwrap =3D true; > > + } > > + } > > + } > > + > > + tree datum_expr =3D build_trait_object (datum_type); > > + non_ref_datum_type =3D TREE_TYPE (datum_expr); > > + > > + tree ptrmem_class_type =3D TYPE_PTRMEM_CLASS_TYPE (fn_type); > > + const bool ptrmem_is_same_or_base_of_datum =3D > > + (same_type_p (ptrmem_class_type, non_ref_datum_type) > > This should be _ignoring_top_level_qualifiers_p... > > > + || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type) > > + && NON_UNION_CLASS_TYPE_P (non_ref_datum_type) > > + && (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_= type, > > + non_ref_datum= _type) > > ...so you don't need to check it again here. > > And from [func.require] it seems we should check same-or-base first, and > not check for reference_wrapper if same-or-base is satisfied. Unlikely > to actually matter (who would check is_invocable with a pointer to > member of reference_wrapper?) but we might as well be precise. I see. Let me update the patch. Thank you! > > Jason >