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 218B1385C017 for ; Mon, 23 Oct 2023 21:23:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 218B1385C017 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 218B1385C017 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=1698096208; cv=none; b=socEZOvYxocG689Grp/bL/XMrpeone+5mNfN7fLn6KptArwKoLcGs82KN3IEpq5D/P6GOM9lnAetk1S+rvSCNglKH9YxuhDoZfHsZmlYQIMQr41UZR0Bi52E97k0Af3kle1xs+AQCt4v8BghPbuWAnuwLuwQJ8Ec0FhCKMeUdEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698096208; c=relaxed/simple; bh=RTD9tdRltaBXYJwA61wv8Xd7HUkVVfTZ9bzZ/8I8QDo=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=nN0lFBd36RZrgenjLAOFPyhyPE4dg/NZUFlgdhS1Up9/zlCM3wLygGlKGBBRlWRn7fjwQnIC7yc8H+n1hVVQpiTpYaWHPDRtSGfYcegvw2A4nMvTyHl1Xix5gSW2TpDGZsyAX65K8rgAM5bfOJwkM5z5WqCYbfvMzbiC7w9RaEY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698096206; 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=bulul56DZuPOsBJoADzzNxK4aFMUaUnKKO4as/dPEbI=; b=Cd4kWxlk5HNC4+qtw7oE66WRpWMGU/GzVaIODmG3CYJ32q/9DDgPBaXlLiYewzRrTTGJ8z SKnX2XvUDQn49XHRHfvMBYnofPvHYm76XCtiXJF2MtGvEqqDoVu4KUi5sHzc9vxtC97xqS +GXxkfPPjELOiKKCoLKPiescSQMEJoc= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-124-i574fmKlMRShC5RmU48yhQ-1; Mon, 23 Oct 2023 17:23:25 -0400 X-MC-Unique: i574fmKlMRShC5RmU48yhQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-779e001ffa5so113781285a.3 for ; Mon, 23 Oct 2023 14:23:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698096205; x=1698701005; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bulul56DZuPOsBJoADzzNxK4aFMUaUnKKO4as/dPEbI=; b=e7tJ/Ti2U5pcCVELDUcWBDCpx8SNWicvZX4nkzV7Lh/kRcQtI9WGXW9QWYDGxptH7v KOrR/i5kOrTjfoSEK/vKh7DBvFuJsNhR9KzB1qLnzHi3rxQ7u7PUxv8Xcv4vuzbhTJuj GsriIv8hQiuhhZkqcZMwH6+HqKFhZoECQqYBjnBbmQM1kjAO7emzl7tA8rwvRFZ58nQ1 S6Fpdlu/tvFV5ztt15BqGtEBO79bwn9mB7HwDYl/yt1mTrcolBpdQDkb7kSqHef7ZhsG RBRxL4+0Xt/oeOPmNvhrteZprZH7QR8j3G0s2bRk+hlzPNMYxsOYxxmg8NkwGFwMTy5p SQ5g== X-Gm-Message-State: AOJu0YzvPg6eQphSkdnqEfg9y1pZViR2ICoZ2w0y8im8CAxbALCmpKjC 9EpLNiNbN7y3ffd6lvFjULog6Ip2lj7zr9icYzpgUpIokYbwFchWEmpU5842/k/9TbI4EBW0SSK 0Hed+jG/VlfxHhGXYGe0VRRY= X-Received: by 2002:a05:620a:8892:b0:775:6d22:d1f5 with SMTP id qk18-20020a05620a889200b007756d22d1f5mr8898902qkn.51.1698096204928; Mon, 23 Oct 2023 14:23:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEaxOH1Ssx5MWCSldJwtaU5QXzo5JrYc0TuTH7wGTMMVu28wWoVU9Q2KxaMVSO0kUxhvpcPww== X-Received: by 2002:a05:620a:8892:b0:775:6d22:d1f5 with SMTP id qk18-20020a05620a889200b007756d22d1f5mr8898891qkn.51.1698096204539; Mon, 23 Oct 2023 14:23:24 -0700 (PDT) Received: from [192.168.1.108] (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 o22-20020a05620a131600b007777521dca4sm2989378qkj.21.2023.10.23.14.23.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 Oct 2023 14:23:23 -0700 (PDT) Message-ID: Date: Mon, 23 Oct 2023 17:23:22 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v23 32/33] c++: Implement __is_invocable built-in trait To: Ken Matsui Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org, Patrick Palka References: <20231017113822.677344-1-kmatsui@gcc.gnu.org> <20231020135748.1846670-1-kmatsui@gcc.gnu.org> <20231020135748.1846670-33-kmatsui@gcc.gnu.org> <024758c0-21f1-ef6e-4bc7-e615a7084c86@idea> From: Jason Merrill In-Reply-To: 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=-13.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 10/20/23 17:37, Patrick Palka wrote: > On Fri, 20 Oct 2023, Patrick Palka wrote: > >> On Fri, 20 Oct 2023, Patrick Palka wrote: >> >>> On Fri, 20 Oct 2023, Ken Matsui wrote: >>> >>>> This patch implements built-in trait for std::is_invocable. >>> >>> Nice! My email client unfortunately ate my first review attempt, so >>> apologies for my brevity this time around. >>> >>>> 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. >>>> (is_invocable_p): New function. >>>> * method.h: New file to export build_trait_object in method.cc. Given how much larger semantics.cc is than method.cc, maybe let's put is_invocable_p in method.cc instead? And in general declarations can go in cp-tree.h. >>>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc >>>> index 7cccbae5287..cc2e400531a 100644 >>>> --- a/gcc/cp/semantics.cc >>>> +++ b/gcc/cp/semantics.cc >>>> @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3. If not see >>>> #include "gomp-constants.h" >>>> #include "predict.h" >>>> #include "memmodel.h" >>>> +#include "method.h" >>>> + >>>> +#include "print-tree.h" >>>> +#include "tree-pretty-print.h" >>>> >>>> /* There routines provide a modular interface to perform many parsing >>>> operations. They may therefore be used during actual parsing, or >>>> @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p) >>>> return saw_copy; >>>> } >>>> >>>> +/* Return true if FN_TYPE is invocable with the given ARG_TYPES. */ >>>> + >>>> +static bool >>>> +is_invocable_p (tree fn_type, tree arg_types) > > (Sorry for the spam) We'll eventually want to implement a built-in for > invoke_result, so perhaps we should preemptively factor out the bulk > of this function into a 'build_INVOKE' helper function that returns the > built tree? > >>>> +{ >>>> + /* ARG_TYPES must be a TREE_VEC. */ >>>> + 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); >>>> + >>>> + /* std::is_invocable 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) >>>> + /* A pointer to data member with non-one argument is not invocable. */ >>>> + return false; >>>> + >>>> + if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0) >>>> + /* A pointer to member function with no arguments is not invocable. */ >>>> + return false; >>>> + >>>> + /* Construct an expression of a pointer to member. */ >>>> + tree datum; >>>> + if (is_ptrdatamem || is_ptrmemfunc) >>>> + { >>>> + tree datum_type = TREE_VEC_ELT (arg_types, 0); >>>> + >>>> + /* Dereference datum. */ >>>> + if (CLASS_TYPE_P (datum_type)) >>>> + { >>>> + bool is_refwrap = false; >>>> + >>>> + tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type)); >>>> + if (decl_in_std_namespace_p (datum_decl)) >>>> + { >>>> + tree name = DECL_NAME (datum_decl); >>>> + if (name && (id_equal (name, "reference_wrapper"))) >>>> + { >>>> + /* Handle std::reference_wrapper. */ >>>> + is_refwrap = true; >>>> + datum_type = cp_build_reference_type (datum_type, false); Why do you change datum_type from std::reference_wrapper<...> to std::reference_wrapper<...>&? >>>> + } >>>> + } >>>> + >>>> + datum = build_trait_object (datum_type); >>>> + >>>> + /* If datum_type was not std::reference_wrapper, check if it has >>>> + operator*() overload. If datum_type was std::reference_wrapper, >>>> + avoid dereferencing the datum twice. */ >>>> + if (!is_refwrap) >>>> + if (get_class_binding (datum_type, get_identifier ("operator*"))) >>> >>> We probably should use lookup_member instead of get_class_binding since >>> IIUC the latter doesn't look into bases: >>> >>> struct A { int m; }; >>> struct B { A& operator*(): }; >>> struct C : B { }; >>> static_assert(std::is_invocable_v); >>> >>> However, I notice that the specification of INVOKE >>> (https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name >>> lookup at all so it strikes me as suspicious that we'd perform name >>> lookup here. Agreed. It seems that whether or not to build_x_indirect_ref should depend instead on whether f is a pointer to a member of decltype(t1) (as well as is_refwrap). >>> I think this would misbehave for: >>> >>> struct A { }; >>> struct B : A { A& operator*() = delete; }; >>> static_assert(std::is_invocable_v); >>> >>> struct C : private A { A& operator*(); }; >>> static_assert(std::is_invocable_v); >> >> Oops, this static_assert is missing a ! >> >>> >>> ultimately because we end up choosing the dereference form of INVOKE, >>> but according to 1.1/1.4 we should choose the non-dereference form? >>> >>>> + /* Handle operator*(). */ >>>> + datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum, >>>> + RO_UNARY_STAR, NULL_TREE, >>>> + tf_none); >>>> + } >>>> + else if (POINTER_TYPE_P (datum_type)) >>>> + datum = build_trait_object (TREE_TYPE (datum_type)); >>>> + else >>>> + datum = build_trait_object (datum_type); >>>> + } >>>> + >>>> + /* Build a function expression. */ >>>> + tree fn; >>>> + if (is_ptrdatamem) >>>> + fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none); >>> >>> Maybe exit early for the is_ptrdatamem case here (and simplify the rest >>> of the function accordingly)? >>> >>>> + else if (is_ptrmemfunc) >>>> + fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type)); Why not use build_m_component_ref and build_offset_ref_call_from_tree like it would if you wrote (t1.*f)() directly? Jason