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.129.124]) by sourceware.org (Postfix) with ESMTPS id 635BA384AB4C for ; Thu, 2 May 2024 20:45:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 635BA384AB4C 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 635BA384AB4C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714682751; cv=none; b=wqQp6PeS744jGahh1fjr+ZdjaJTGjsPLKKcGUq4qagBaOfZsZagwqsZDOtmvfGhH6R/Y+xV1dyZGIjsKkM8npqX6zITndMKubUXgjdZWwU+K6KtpVnaJx/JZRlVvuaC3C8IagL62cYXwzLMzJ5gJFJuMmFyLVe7P9Q+zfE04urA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714682751; c=relaxed/simple; bh=HStYOI6JrOvKK4RaTRNK+TVhTCC/TS6y/dRwuKfmGDw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=mO6NwPQaBt+jeJhbRxSunlL1nFK9fwQd03dNjLkrSYnVw18POyohnkcIP5G0KW8pcPm2RrIpwiAob5k+HB+XLuFmGyHsZApRedxeI5tiqJxNE9HspDs4imH/PjauYWmQtd+QZYO3hAb0mstO9ViqSwOElBx37AwJeP+rpRRFKjY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714682749; 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: in-reply-to:in-reply-to:references:references; bh=jfWHUgddcqVmX09WB+2BZFpO4RLysT355ElF0LwE9Ew=; b=NF9z+Ikz/EJy0xSec5BAytQyj9VZ9ZEYF6e9nTkfEzukh0HZaSqLvXbbUkTMp5c/qsXvgS x5ur3lNEudzurjaOfg+v6flsz3ZUMG/xvSNIiVOQs1VS7YJTVV+HAvvu+d7YXRFmf1iSOc 2ItKZ1upFU6i0RFBapDGPxZlnmZvpo8= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627-uMcOjsJTPMCbkz-FOR-vfA-1; Thu, 02 May 2024 16:45:47 -0400 X-MC-Unique: uMcOjsJTPMCbkz-FOR-vfA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6a0dd95cbc6so38616066d6.1 for ; Thu, 02 May 2024 13:45:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714682747; x=1715287547; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jfWHUgddcqVmX09WB+2BZFpO4RLysT355ElF0LwE9Ew=; b=jIMWsN/5n3ee31Qa9/5Xu5W6Sab/8ttnP+4ldAySY+tyWqFypZ03lTcAQA0buNvQUO qpALP84eODR9x4YI6PJE+VuvbEETFuntuWwkaI39qhaWUklzJogadj//k6ZngTVE6ck8 WssDy0qfFXEtTWCY9+4C6XAtVRJ8tCWDDtsGda2mRlYR1RrQv9fBwNjWqj/gEegF+U9r BjJAFLWiRz845V2vfA6kcQ+EKVeSGZSIqrQUTJrKadNpMSkke+hwOtc8MCPC+8oStKLO KkeRLWsNGOOTSBfrxq2WIKC8ANaqXyGSVvwtoImT4Iis16Z5YApj3is/8QHR6ZNz608p CRgg== X-Forwarded-Encrypted: i=1; AJvYcCUl+qk9C9gvqQuCyQU2cXSTJGaNqBbNFfNo1HsXjHwopyXU5SYflpGhjO78hXoZbkxceprohwHoj9O029h7irfr5NBiLuM= X-Gm-Message-State: AOJu0YxwuSd2HHaK3G3rR5k3LhkBlnGWdgaWh1zXLHNffWVEAwfJJcev dBjYAGnz6rLZDQ+vQcfdmvptXsVKyM/DhaPyv6QJTR2x1MSgQi82c02cxZVtH1JhbwHNnG3gaWF D33URM6Npry/h4Eim8sgE08E45rsW+To7HfJyTg7+SXspoa8A85YgGgDS7tGi X-Received: by 2002:a05:6214:dcb:b0:6a0:c945:335 with SMTP id 11-20020a0562140dcb00b006a0c9450335mr930412qvt.12.1714682746873; Thu, 02 May 2024 13:45:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9t9kZH03dydv90F63wLi1gt4f3K3tDlo/zAMASIQZqmUoFIszp8eBI2QH1p8nXDc8FmT6Yg== X-Received: by 2002:a05:6214:dcb:b0:6a0:c945:335 with SMTP id 11-20020a0562140dcb00b006a0c9450335mr930389qvt.12.1714682746425; Thu, 02 May 2024 13:45:46 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id s16-20020a0cf650000000b006a103d66c7esm633395qvm.14.2024.05.02.13.45.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 13:45:46 -0700 (PDT) Date: Thu, 2 May 2024 16:45:44 -0400 From: Marek Polacek To: Jason Merrill Cc: Ken Matsui , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH v18 23/26] c++: Implement __is_invocable built-in trait Message-ID: References: <20240502184529.97337-1-kmatsui@gcc.gnu.org> <20240502201549.459244-1-kmatsui@gcc.gnu.org> <20240502201549.459244-24-kmatsui@gcc.gnu.org> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.3 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=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 02, 2024 at 04:38:16PM -0400, 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_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", There's a space missing before "a class". But we'll never see these errors anyway, because build_invoke will only ever be called with complain=tf_none, yes? Functions like constructible_expr / assignable_expr don't have a complain parameter, so do we need it here? > > + 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) > > Why NON_UNION? That doesn't seem to be based on anything in the standard. > > This check and the one for reference_wrapper need to ignore REFERENCE_TYPE; > INVOKE is defined in terms of expressions, and the type of an expression is > never a reference. > > > + && (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)) > > ...so we shouldn't need to check TYPE_REF_P here. > > Jason > > > + /* 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. > Marek