From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 879B4384474B for ; Fri, 10 May 2024 14:20:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 879B4384474B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 879B4384474B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715350822; cv=none; b=KDotlm8zSnKJqehzV1mnobaaw2MIgcSiPwhEZOpcgMhL6lSOTeuRQ7L8LCscpwfvzbX+1irYZM3e5/ogtOjDKKf2w+28GN7cCQ/xIsvVgGQy1xJfEjlXH7WGdyXZnd6oMcZZVHiElyKdZS/UYz2eTH82Jtx1cIhISHc8jUCU5oU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715350822; c=relaxed/simple; bh=VArHUCKwnldk15nZsdI0tEjxgmZCGzI2CEejOdm/5kU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=Tt5O8kC990N5RDv5xkmK67lir1GHLt4SONpGpO0RBuiNYzSO1jAz2CvFZZgaXFg1Q2DkmxJjzQNsmZbfdYnfXcY+WbiiAEIF7oSOM/pWVc/nQ5DJiLcgWuN2/GadVK1ubTOXe48U6weXFzEbasRFi154aEARL+TJ8PBirhRuEKY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715350819; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ApLSN4hrcfN6wlwWor90STec8suwjLJLtHVgA0qmoCM=; b=V7RL7ulseGmHmxruncaOrRdTYBepavObEQeiEHkPKXfRPsc/m69oZUuBx5fzcc9dU3UlP/ TH8TrXaTYGrSWTN85unSvqlualjSx0DL5BN3WnE7F15Dq2shHzL/zBCSQaHHx2U/2ErdkB +3cKPFXlasL25+nRGK5TCkRRgqM+Jm8= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-272-4eI1IpaqNBCE6q-l0XWk5Q-1; Fri, 10 May 2024 10:20:12 -0400 X-MC-Unique: 4eI1IpaqNBCE6q-l0XWk5Q-1 Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-1ee0caec57fso19381075ad.3 for ; Fri, 10 May 2024 07:20:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715350812; x=1715955612; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ApLSN4hrcfN6wlwWor90STec8suwjLJLtHVgA0qmoCM=; b=dV74uclzibp7y40dKo16HApr9PGfJDGOsMiYyWc2biVcWSTC3x4Qps96GkdpJDsW++ 8iTr6TQ59Nbk+SXlHItQ2zOjC6QwtnAOzSsZMil7RQLnE6n3RXBmctQpBfbuddUmOxCx xR7ZSFZOriKHTs62XkTTmSJgxfTS5Ur9yOuFjmBFPooChXeX8JuSHAo8aIKrn0/6LgMS V89C9aCUeXWaaBT3AKQb1SVPgLWMPNiizkE71lzJQm15fU1JAmZibIFR1WptBZVSAveA 1NWaAUUEbNDYYSabLdRT+uHr7yqvA+/jk+U8a3h45NnGnal/lCSbd5cgX9eQGk630J8q p2nA== X-Gm-Message-State: AOJu0YzplDR9xYYxKjUG1dkmtWzrohGhzbl+bzzHQqrCqwHH2rfkI4ds /u7LfXkUKaRmgYZXXerDK/KL4BjfQILuoqJtBEQSYso8dXPfrT4NwYfuOYUyBdrebf99TkS3M9n wDqsPm6k57UhrZKO4lBTFAoJRyuyPTJHOyaSwTcxgRmJ45Xt9MayB X-Received: by 2002:a17:902:c204:b0:1eb:7832:8d93 with SMTP id d9443c01a7336-1ef43d18438mr27941905ad.23.1715350811757; Fri, 10 May 2024 07:20:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+rdGiNQCkywwTPAWZWtfXr2YcQ7zAU6KY5sdztGAdZOYh5YVuDCg0OkM9ae00UrufJtQL5w== X-Received: by 2002:a17:902:c204:b0:1eb:7832:8d93 with SMTP id d9443c01a7336-1ef43d18438mr27941605ad.23.1715350811132; Fri, 10 May 2024 07:20:11 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0b9d187fsm32562805ad.27.2024.05.10.07.20.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 May 2024 07:20:10 -0700 (PDT) Message-ID: <177fdb76-499c-4427-bfab-9e32019bbb25@redhat.com> Date: Fri, 10 May 2024 10:20:08 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v23 20/23] c++: Implement __is_invocable built-in trait To: Ken Matsui , gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org References: <20240508050455.365029-21-kmatsui@gcc.gnu.org> <20240510112417.8927-21-kmatsui@gcc.gnu.org> From: Jason Merrill In-Reply-To: <20240510112417.8927-21-kmatsui@gcc.gnu.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable 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 5/10/24 07:24, Ken Matsui wrote: > Fixed the reference to pointer to class case. Ok for trunk? > > -- >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_pointer", 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_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..7e66d67f5d9 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 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 non_ref_fn_type = TREE_TYPE (fn_type); > + is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type); > + is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type); > + > + /* Dereference fn_type if it is a pointer to member. */ > + if (is_ptrdatamem || is_ptrmemfunc) > + fn_type = non_ref_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); > + tree non_ref_datum_type = datum_type; > + if (TYPE_REF_P (datum_type)) > + non_ref_datum_type = 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)))) This logic looks like it will now only reject a pointer to non-class, not any non-pointer, non-class type. Such types will still fail when we get to build_x_indirect_ref, but I think you meant them to get this diagnostic (if we get here with tf_error). Jason