From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vk1-xa31.google.com (mail-vk1-xa31.google.com [IPv6:2607:f8b0:4864:20::a31]) by sourceware.org (Postfix) with ESMTPS id 206A6384AB58 for ; Fri, 3 May 2024 02:47:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 206A6384AB58 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 206A6384AB58 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::a31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714704444; cv=none; b=lhDQNDLymFn9dsj6cQ4jdLWGpU7XurSivT7LoRUhLMdyZMlpDWkgKmvKgD1UT0tcvwcA7GOjOMA1fD4C1vAhzMxHcW680+Z+8vEEK/4cvIdxOfPW8WNXoNP3bM3+4AeQW2oIzFb+FMWC0+cO1LxKRsj5qq6N/z0EVpeDzsHPD5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714704444; c=relaxed/simple; bh=qWg5JeSLGH/1OQt1xdlrLDVTR/QsoUtzQzW1KJcpjPw=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=n9YZywuaRmWvPKIdV7h0QCNM8rTv1OJHj+zkuH98odVWvltgc+eEVWKgDFPeRUXnMHYHEtnxCkkdW4/x1uVaVDcl57/jxdIVURDfkWTiytzs55foDz3gEdowjed+aTJ+gOLomZq4mVPbROG2aQ5vii4lkgKurQrIC/R8xJinlDk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-vk1-xa31.google.com with SMTP id 71dfb90a1353d-4dac112e192so2466426e0c.1 for ; Thu, 02 May 2024 19:47:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1714704441; x=1715309241; 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=mrZPauxhU3gixuOOwc6Dhf12iq1Ht00Fnog4iW36WoA=; b=V7cFT8hIFZstOsiWeK5VH2RpYS2ycMVOLfLf9yVdUQ4fiA1quacQ+c5xWbf5a59ZJK GtV4Dd0kNEocpO9oTCpKTFpX5tZ6pDgD1RP82wlI1qYoUfx8ucLcgM9CH/r5im9q4M3O y8oBL/NSxNOAp88lZ7Cc2jezsl4XrxPkMNJ5c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714704441; x=1715309241; 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=mrZPauxhU3gixuOOwc6Dhf12iq1Ht00Fnog4iW36WoA=; b=fjAmPYEFibdEl+C9vcFcgW3i4zrA+6JIFre2sGYOkC7cKqB5JgC1JwyLaBO7YzshIr OukPuaZiyxguiZDWR7/pYVQBleqi4eHBZBguPAaB4LJToo3/6BEf+hpe3tmKUuCccmNy fnZV9fncDgiudiFT0r26j3FeqUE16MmyCluSkvSYOoRRGRBgC+3PrRd/CFpd9LdP5h2C +Sn0oqjzZhwvV56P4rJ4dpqP5QMYJ3HdtPQH2lMD/pe5wkZyV4AB6CqBRiLmtWdA4kH6 shiGwjysI0QA+Is0nzlfe3H8SKHEW/V8ED9iO/BM1WOTZIdlCyHSTDT8PQtef4+tTPtf fhjA== X-Forwarded-Encrypted: i=1; AJvYcCVV00BXPanC5zsoHX0CC6nW28S9Pm9ZhJHhNmol/07J4c+SN48xqByMoJxzep5w/3ip0Z+arPG2ZgZ957PZrrmQj39jwGw= X-Gm-Message-State: AOJu0YzC7nEsaNseHgv7OMaxB5VQsZVn1bhlGl49R/grMgXPf+xDBxvM sWaSAfjADtWCGCdAE92DtEH3Lz/owEvPiVpYAB2kTA8v+wBBOdWBhJcESwMxZVffAyKVN+cc4Dj L9THGvnGCKGF644F7qEH0A2V14MdbLR3ARhge X-Google-Smtp-Source: AGHT+IGKvMejsj12QaTPC3NQxQ8NeSDRJjvxcZiQ+YN+hK/Wo4gc5hZoT/ZqttHoD27dVi7aiIdMtSPf3VX0uC+JXT0= X-Received: by 2002:a05:6122:221b:b0:4de:bdbf:6351 with SMTP id bb27-20020a056122221b00b004debdbf6351mr1678224vkb.7.1714704441238; Thu, 02 May 2024 19:47:21 -0700 (PDT) MIME-Version: 1.0 References: <20240502184529.97337-1-kmatsui@gcc.gnu.org> <20240502201549.459244-1-kmatsui@gcc.gnu.org> <20240502201549.459244-24-kmatsui@gcc.gnu.org> In-Reply-To: From: Ken Matsui Date: Thu, 2 May 2024 19:46:45 -0700 Message-ID: Subject: Re: [PATCH v18 23/26] 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.8 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 Thu, May 2, 2024 at 7:43=E2=80=AFPM Jason Merrill wro= te: > > On 5/2/24 16:47, Ken Matsui wrote: > > On Thu, May 2, 2024 at 1:38=E2=80=AFPM Jason Merrill = wrote: > >> > >> On 5/2/24 16:12, Ken Matsui wrote: > >>> 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_INVOCABL= E. > >>> * 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_p= ointer", 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 com= plain) > >>> +{ > >>> + 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 deref_fn_type =3D TREE_TYPE (fn_type); > >>> + is_ptrdatamem =3D TYPE_PTRDATAMEM_P (deref_fn_type); > >>> + is_ptrmemfunc =3D TYPE_PTRMEMFUNC_P (deref_fn_type); > >>> + > >>> + /* Dereference fn_type if it is a pointer to member. */ > >>> + if (is_ptrdatamem || is_ptrmemfunc) > >>> + fn_type =3D deref_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 wit= h " > >>> + "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 wit= h " > >>> + "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); > >>> + > >>> + /* datum must be a class type or a reference/pointer to a clas= s 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 =3D false; > >>> + if (CLASS_TYPE_P (datum_type)) > >>> + { > >>> + /* 1.2 & 1.5: Handle std::reference_wrapper. */ > >>> + tree datum_decl =3D TYPE_NAME (TYPE_MAIN_VARIANT (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 (datum_type= ), 0); > >>> + is_refwrap =3D true; > >>> + } > >>> + } > >>> + } > >>> + > >>> + tree ptrmem_class_type =3D TYPE_PTRMEM_CLASS_TYPE (fn_type); > >>> + const bool ptrmem_is_base_of_datum =3D > >>> + (NON_UNION_CLASS_TYPE_P (ptrmem_class_type) > >>> + && NON_UNION_CLASS_TYPE_P (datum_type) > >> > >> Why NON_UNION? That doesn't seem to be based on anything in the stand= ard. > > > > This comes from the __is_base_of implementation in semantics.cc: > > > > case CPTK_IS_BASE_OF: > > return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P= (type2) > > && (same_type_ignoring_top_level_qualifiers_p (type1, type2) > > || DERIVED_FROM_P (type1, type2))); > > OK, but non-union isn't a requirement for is_same, but you're applying > that requirement for the same-type case as well as for the base case. > > >> This check and the one for reference_wrapper need to ignore > >> REFERENCE_TYPE; INVOKE is defined in terms of expressions, and the typ= e > >> of an expression is never a reference. > > > I think ptrmem_is_base_of_datum and is_refwrap checks are ignoring > > REFERENCE_TYPE, leading to false, ... > > I mean they need to look through REFERENCE_TYPE; if the first argument > is reference to reference_wrapper, we need to use the reference_wrapper > handling. Likewise if it's a reference to T or a base of T. > > >>> + && (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_ty= pe, > >>> + datum_type) > >>> + || DERIVED_FROM_P (ptrmem_class_type, datum_type))); > >>> + > >>> + tree datum_expr =3D build_trait_object (datum_type); > >>> + if (!ptrmem_is_base_of_datum && !is_refwrap && !TYPE_REF_P (da= tum_type)) > >> > >> ...so we shouldn't need to check TYPE_REF_P here. > > > ... which is why I think we need to check TYPE_REF_P here. > > The standard says that if t1 isn't either same, base, or > reference_wrapper, we dereference it. There's no exception for > references because they shouldn't get this far. Oh, that makes sense! Thank you so much for your review! > > Jason >