public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: waffl3x <waffl3x@protonmail.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v5 1/1] c++: Initial support for P0847R7 (Deducing This) [PR102609]
Date: Tue, 21 Nov 2023 22:22:33 -0500	[thread overview]
Message-ID: <ffef1807-b7be-43e1-82e3-210dbf66470e@redhat.com> (raw)
In-Reply-To: <e1dWYu-AKeY4Tx7EvAMCbTpi8vcvr8Xl_o3ZRez4jYtClI9TD5vOf8Qk41sEh6gBbyjlvCOQHPI1woLPVnHY9g9JJu8FRm6eqUtE2L0hsNc=@protonmail.com>

On 11/21/23 08:04, waffl3x wrote:
> Bootstrapped and tested on x86_64-linux with no regressions.
> 
> Hopefully this patch is legible enough for reviewing purposes, I've not
> been feeling the greatest so it was a task to get this finished.
> Tomorrow I will look at putting the diagnostics in
> start_preparsed_function and also fixing up anything else.
> 
> To reiterate in case it wasn't abundantly clear by the barren changelog
> and commit message, this version is not intended as the final revision.
> 
> Handling re-declarations was kind of nightmarish, so the comments in
> there are lengthy, but I am fairly certain I implemented them correctly.
> 
> I am going to get some sleep now, hopefully I will feel better tomorrow
> and be ready to polish off the patch. Thanks for the patience.

Great!

> I stared at start_preparsed_function for a long while and couldn't
> figure out where to start off at. So for now the error handling is
> split up between instantiate_body and cp_parser_lambda_declarator_opt.
> The latter is super not correct but I've been stuck on this for a long
> time now though so I wanted to actually get something that works and
> then try to make it better.

I see what you mean, instantiate body isn't prepared for 
start_preparsed_function to fail.  It's ok to handle this in two places.
Though actually, instantiate_body is too late for it to fail; I think 
for the template case it should fail in tsubst_lambda_expr, before we 
even start to consider the body.

Incidentally, I notice this code in tsubst_function_decl seems to need 
adjusting for xobj:

   tree parms = DECL_ARGUMENTS (t);
   if (closure && !DECL_STATIC_FUNCTION_P (t))
     parms = DECL_CHAIN (parms);
   parms = tsubst (parms, args, complain, t);
   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
     DECL_CONTEXT (parm) = r;
   if (closure && !DECL_STATIC_FUNCTION_P (t))
...

and this in tsubst_lambda_expr that assumes iobj:

       /* Fix the type of 'this'.  */
       fntype = build_memfn_type (fntype, type,
                                  type_memfn_quals (fntype),
                                  type_memfn_rqual (fntype));

This also seems like the place to check for unrelated type.

>  /* Nonzero for FUNCTION_DECL means that this decl is a non-static
> -   member function.  */
> +   member function, use DECL_IOBJ_MEMBER_FUNC_P instead.  */
>  #define DECL_NONSTATIC_MEMBER_FUNCTION_P(NODE) \
>    (TREE_CODE (TREE_TYPE (NODE)) == METHOD_TYPE)
>  
> +/* Nonzero for FUNCTION_DECL means that this decl is an implicit object
> +   member function.  */
> +#define DECL_IOBJ_MEMBER_FUNC_P(NODE) \
> +  (TREE_CODE (TREE_TYPE (NODE)) == METHOD_TYPE)

I was thinking to rename DECL_NONSTATIC_MEMBER_FUNCTION_P rather than 
add a new, equivalent one.  And then go through all the current uses of 
the old macro to decide whether they mean IOBJ or OBJECT.

> -   (static or non-static).  */
> +   (static or object).  */

Let's leave this comment as it was.

> +  auto handle_arg = [fn, flags, complain](tree type,
> +                                         tree arg,
> +                                         int const param_index,
> +                                         conversion *conv,
> +                                         bool const conversion_warning)

Let's move the conversion_warning logic into the handle_arg lambda 
rather than have it as a parameter.  Yes, we don't need it for the xobj 
parm, but I think it's cleaner to have less in the loop.

Also, let's move handle_arg after the iobj 'this' handling so it's 
closer to the uses.  For which the 'else xobj' needs to drop the 'else', 
or change to 'if (first_arg)'.

> +      /* We currently handle for the case where first_arg is NULL_TREE
> +	 in the future this should be changed and the assert reactivated.  */
> +      #if 0
> +      gcc_assert (first_arg);
> +      #endif

Let's leave this out.

> +      val = handle_arg(TREE_VALUE (parm),

Missing space before (.

> -      if (null_node_p (arg)
> -          && DECL_TEMPLATE_INFO (fn)
> -          && cand->template_decl
> -	  && !cand->explicit_targs)
> -        conversion_warning = false;
> -
> +      auto determine_conversion_warning = [&]()
> +	{
> +	  return !(null_node_p (current_arg)
> +	    && DECL_TEMPLATE_INFO (fn)
> +	    && cand->template_decl
> +	    && !cand->explicit_targs);
> +	};

I don't think this needs to be a lambda.

> +	     declaration, then the type of it's object parameter is still
...
> +		 but due to [basic.scope.scope.3] we need to ignore it's
...
> +	    /* Since a valid xobj parm has it's purpose cleared in find_xobj_parm

"its"

> -  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
> -      && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
> +  if (DECL_OBJECT_MEMBER_FUNC_P (fn)
> +      && !(complain & tf_ptrmem_ok))

The following code (not quoted) could share more code between the cases 
if we check

!(flag_ms_extensions && DECL_IOBJ_MEMBER_FUNC_P (fn))

here?

> +		else if (declarator->declarator->kind == cdk_pointer)
> +		  error_at (DECL_SOURCE_LOCATION (xobj_parm),
> +			 /* "a pointer to function type cannot "?  */
> +			    "a function pointer type cannot "
> +			    "have an explicit object parameter");

"pointer to function type", yes.

> +		/* The locations being used here are probably not correct.  */

Why not?

Let's clear xobj_parm after giving an error in the TYPENAME case

>   if ((!methodp && !DECL_XOBJ_MEMBER_FUNC_P (decl))
>       || DECL_STATIC_FUNCTION_P (decl))

I think this can just be if (DECL_OBJECT_MEMBER_FUNC_P (decl)).

>    if (TREE_CODE (fntype) == METHOD_TYPE)
>      ctype = TYPE_METHOD_BASETYPE (fntype);
> +  else if (DECL_XOBJ_MEMBER_FUNC_P (decl1))
> +    ctype = DECL_CONTEXT (decl1);

All of this can be

if (DECL_CLASS_SCOPE_P (decl1))
   ctype = DECL_CONTEXT (decl1);

I think I'm going to go ahead and clean that up now.

> +  /* We don't need deal with 'this' or vtable for xobj member functions.  */
> +  if (ctype && !doing_friend &&
> +      !(DECL_STATIC_FUNCTION_P (decl1) || DECL_XOBJ_MEMBER_FUNC_P (decl1)))

And this can be

if (DECL_IOBJ_MEMBER_FUNC_P (decl1))

> +  if (INDIRECT_TYPE_P (TREE_TYPE (object)))
> +    /* The proxy variable forwards to the capture field.  */

This comment should stay outside the if.

> +      /* This might not be the most appropriate place for this, but I'm going
> +	 to hold back on changing it for the time being since we are short on
> +	 time. It's not really a parser error, it's more a semantic error.
> +	 But this is where the other errors were so...  */

It's OK to give semantic errors in the parser if they are always 
detectable at parse time (before template instantiation).

Though maybe we could contain the xobj vs mutable and xobj vs static 
diagnostics in this block:

> +  if (xobj_param)
> +    {
> +      quals = TYPE_UNQUALIFIED;
> +      if (TYPE_REF_P (xobj_param)
> +	  && !(cp_type_quals (TREE_TYPE (xobj_param)) & TYPE_QUAL_CONST))
> +        LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
> +    }

rather than separately in the mutable/static blocks?  But the way you 
have it is fine, too.

> +	  inform (DECL_SOURCE_LOCATION(xobj_param),

missing space before (xobj_param), repeated several times in the rest of 
the function.

> +	  /* Error reporting here is a little awkward, if the type of the
> +	     object parameter is deduced, we should tell them the lambda
> +	     is effectively already const, or to make the param const if it is
> +	     not, but if it is deduced and taken by value shouldn't we say
> +	     that it's taken by copy and won't mutate?
> +	     Seems right to me, but it's a little strange.  */

I think just omit the inform if dependent_type_p.

> -	if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
> +	if (!LAMBDA_EXPR_STATIC_P (lambda_expr)
> +	    && !DECL_XOBJ_MEMBER_FUNC_P (fco))

if (DECL_IOBJ_MEMBER_FUNC_P (fco))

> +      /* Since a lambda's type is anonymous, we can assume an explicit object
> +	 parameter is unrelated... at least I believe so. I can think of a
> +	 paradoxical case that almost works, where a class is forward declared
> +	 and then defined deriving from a lambda that has an explicit object
> +	 parameter of that class type, but the only way that can work is with
> +	 decltype... okay yeah I think it can work for a struct defined in
> +	 block scope, but I'm leaving this as is until I can confirm my
> +	 hypothesis.  */

Like this?

template <class T> struct A: T { };
template <class T> T f()
{
   int x = 42;
   auto f1 = [x]<typename U>(this A<U>&& self) { return x; };
   return A<decltype(f1)>{f1}();
}
int main() { return f<int>(); }

When I disable the error this crashes in register_parameter_specializations

> +      if (!TEMPLATE_PARM_P (non_reference (TREE_TYPE (xobj_param))))

...so let's just check dependent_type_p here.

> +      error ("%<this%> is unavailable for explicit object member "
> +	     "functions");
> +      /* I can imagine doing a fixit here, suggesting replacing
> +	 this / *this / this-> with &name / name / "name." but it would be
> +	 very difficult to get it perfect and I've been advised against
> +	 making imperfect fixits.
> +	 Perhaps it would be as simple as the replacements listed,
> +	 even if one is move'ing/forward'ing, if the replacement is just
> +	 done in the same place, it will be exactly what the user wants?
> +	 Even if this is true though, there's still a problem of getting the
> +	 context of the expression to find which tokens to replace.
> +	 I would really like for this to be possible though.
> +	 I will decide whether or not to persue this after review.  */

I still think (&self) will always be a correct replacement for 'this', 
but unlikely to be the way they actually want their code to look.  To 
detect this-> or *this should be doable in the parser (where you can 
look forward and back in the token stream) but we don't have access to 
that here in finish_this_expr.

> +	/* Pull out the function_decl for a single xobj member function,
> +	   similar to the BASELINK case above. If we did the same optimization
> +	   as we do for single static member functions (passing in a BASELINK)
> +	   then it could be handled the same too.  */

This comment seems out of date given that the BASELINK handling just 
gives an error.  We can't use the same handling as static member 
functions because we need the SCOPE_REF to tell us that it was properly 
qualified.

Jason


  reply	other threads:[~2023-11-22  3:22 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31  6:02 [PATCH 1/2] " waffl3x
2023-08-31  8:33 ` Jakub Jelinek
2023-09-02  8:43   ` waffl3x
2023-09-11 13:49     ` [PATCH v2 " waffl3x
2023-09-19 20:14       ` Jason Merrill
2023-09-20  0:30         ` waffl3x
2023-09-20 21:19           ` Jason Merrill
2023-09-21 11:28             ` waffl3x
2023-09-22 11:30               ` Jason Merrill
2023-09-26  1:56                 ` [PATCH v3 " waffl3x
2023-09-27 22:43                   ` Hans-Peter Nilsson
2023-09-27 23:35                     ` Waffl3x
2023-10-17 20:53                   ` Jason Merrill
2023-10-17 21:11                   ` Jason Merrill
2023-10-18 11:46                     ` waffl3x
2023-10-18 14:17                       ` Jason Merrill
2023-10-18 17:28                         ` waffl3x
2023-10-18 17:45                           ` Jakub Jelinek
2023-10-18 18:12                           ` Jason Merrill
2023-10-19 21:05                             ` waffl3x
2023-10-19 21:11                               ` Jakub Jelinek
2023-10-19 21:31                                 ` waffl3x
2023-10-19 21:53                                   ` Jakub Jelinek
2023-10-19 22:18                               ` Jason Merrill
     [not found]                                 ` <Q2xXS2RkwtzDslDUAsi4YWupkb9s3QKvecqsxLNUr=5FL-MdSmzIJcZ96S3B9Avk30GneMm8R67JsQ4D-Uj1JB6N8dhTw6LAlNsrpLKHuLP2o=3D@protonmail.com>
2023-10-19 22:51                                 ` waffl3x
2023-10-19 23:35                                   ` waffl3x
2023-10-20  2:39                                     ` Jason Merrill
2023-10-20  4:34                                       ` waffl3x
2023-10-20 16:01                                         ` Jason Merrill
     [not found]                                           ` <zXWkSXEO=5FH62WXyNUeV1zNAx9wSVGQ5ooxKAfpN2InCP4X25uOC0yTZlnMqbDMoIa4lGwj0hP-KEP5UIcMs1S1zkhz=5FZx4oM3oz09DY2BRg=3D@protonmail.com>
     [not found]                                             ` <9YnRZJPkB8KCk8R86UDwWBoNnmOEAir4IU4enb1qIGgVdkB6dwy76ClgAzwpPqpToQ9sLTBs50EIRwhivBJU6RAFfLt-fjJxdYTZ35YVFWA=3D@protonmail.com>
2023-10-28  4:07                                           ` waffl3x
2023-10-28 23:21                                             ` waffl3x
2023-11-01 23:15                                               ` waffl3x
2023-11-02  7:01                                                 ` waffl3x
2023-11-02  7:01                                                 ` Jakub Jelinek
2023-11-03  3:04                                             ` Jason Merrill
2023-11-03  4:44                                               ` waffl3x
2023-11-03 18:05                                                 ` Jason Merrill
2023-11-04  6:40                                                   ` waffl3x
2023-11-09 18:44                                                     ` Jason Merrill
2023-11-10  4:24                                                       ` waffl3x
2023-11-10 23:12                                                         ` Jason Merrill
2023-11-11 10:43                                                           ` waffl3x
2023-11-11 11:24                                                             ` waffl3x
2023-11-14  3:48                                                             ` Jason Merrill
2023-11-14  4:36                                                               ` waffl3x
2023-11-18  6:43                                                                 ` waffl3x
2023-11-19  6:22                                                                   ` Jason Merrill
2023-11-19 13:39                                                                     ` waffl3x
2023-11-19 16:31                                                                       ` Jason Merrill
2023-11-19 18:36                                                                         ` waffl3x
2023-11-19 20:34                                                                           ` Jason Merrill
2023-11-19 21:44                                                                             ` waffl3x
2023-11-20 14:35                                                                               ` Jason Merrill
     [not found]                                                                                 ` <1MdaTybBd=5Fo4uw-Gb23fYyd5GNz7qFqoSe=5Ff5h90LY=5FBdzM2ge2qPSyCuiCLYoYcZSjmVv13fw1LmjQC=5FM2L8raS1fydY5pEJ=5Fvwvv5Z-0k=3D@protonmail.com>
2023-11-21 10:02                                                                                 ` waffl3x
2023-11-21 13:04                                                                                   ` [PATCH v5 1/1] " waffl3x
2023-11-22  3:22                                                                                     ` Jason Merrill [this message]
2023-11-22 20:46                                                                                       ` waffl3x
2023-11-22 21:38                                                                                         ` Jason Merrill
     [not found]                                                                                           ` <kltTuyDDwoyOmhBWostMKm5zF3sQCGz3HjMBrBUK6LOZp1-AbGMl5ijKKMlOncwR2yiWippyp89sFPZykNF3OVyz4yknnCVwn=5FiHJPUl25k=3D@protonmail.com>
     [not found]                                                                                             ` <dHEpSeuiljMbH0YhwLULApd3yO3LNaVkamGW2KJBYBl0EgMrtpJZ41GeTVOc77siD1kh2vkF4zwInWWGxYXfcnW4XV7sfDPX7cY028JiORE=3D@protonmail.com>
2023-11-22 21:56                                                                                           ` waffl3x
2023-11-22 22:44                                                                                           ` waffl3x
2023-11-24  6:49                                                                                             ` waffl3x
2023-11-24 23:26                                                                                               ` waffl3x
2023-11-25  1:14                                                                                                 ` waffl3x
2023-11-26 21:30                                                                                                   ` Jason Merrill
2023-11-26 23:10                                                                                                     ` waffl3x
2023-11-27  1:16                                                                                                       ` Jason Merrill
     [not found]                                                                                                         ` <BEI8PD7nktTuX7dimb22uDnR0b8Bc8ozi4xx9KbiEFj8TjgUCxMfEPpcIPL0bkdThBBab97T1uEJ9rUM3va1eiE1TyRw=5FiLrxwKgg30ZaW0=3D@protonmail.com>
2023-11-27  1:30                                                                                                         ` waffl3x
2023-11-27  1:44                                                                                                         ` waffl3x
2023-11-27  2:40                                                                                                           ` Jason Merrill
2023-11-27  5:35                                                                                                           ` [PATCH v6 " waffl3x
2023-11-28  3:31                                                                                                             ` waffl3x
2023-11-28 10:00                                                                                                               ` waffl3x
2023-11-30  5:00                                                                                                             ` Jason Merrill
2023-11-30  6:36                                                                                                               ` waffl3x
2023-11-30 14:55                                                                                                                 ` Jason Merrill
2023-12-01  6:02                                                                                                                   ` waffl3x
2023-12-01 16:52                                                                                                                     ` Jason Merrill
2023-12-02  1:31                                                                                                                       ` waffl3x
2023-12-02 15:02                                                                                                                         ` Jason Merrill
     [not found]                                                                                                                           ` <KQegse=5FguOyql4Ok1lrAgS7gasZJd1pOoPbCTdGxcHh-G4A9Tlf5zCnGJmqtshMt72edmcXdIapaZNPp4VJp5Ar9PHZbUrbwDsPjTSUrnOI=3D@protonmail.com>
     [not found]                                                                                                                             ` <59LofhYhxl7MLEuElXwZcESRB6MpjdG-iq-89B63siDRo5k0j-y6z-PVa6Y3iE1xE5LkJwpwTFi82bd0RZjB1yZbSJptFdPTBWfvOGj1W78=3D@protonmail.com>
2023-12-05  4:35                                                                                                                           ` waffl3x
2023-12-05  4:39                                                                                                                             ` waffl3x
2023-12-05  5:54                                                                                                                               ` waffl3x
2023-12-06  7:33                                                                                                                                 ` [PATCH v7 " waffl3x
2023-12-06  8:48                                                                                                                                   ` Jakub Jelinek
2023-12-06  9:31                                                                                                                                     ` waffl3x
2023-12-06 11:08                                                                                                                                   ` waffl3x
2023-12-08 19:25                                                                                                                                   ` Jason Merrill
2023-12-10 15:22                                                                                                                                     ` waffl3x
2023-12-10 18:59                                                                                                                                       ` Jason Merrill
2023-12-22  9:01                                                                                                                                         ` waffl3x
2023-12-22 17:26                                                                                                                                           ` Jason Merrill
2023-12-23  7:10                                                                                                                                             ` waffl3x
2023-12-26 16:37                                                                                                                                               ` Jason Merrill
2024-01-01 15:17                                                                                                                                                 ` waffl3x
2024-01-01 15:34                                                                                                                                                   ` waffl3x
2024-01-01 17:12                                                                                                                                                     ` waffl3x
2024-01-06 12:37                                                                                                                                                       ` waffl3x
2023-11-25 17:32                                                                                               ` [PATCH v5 " Jason Merrill
2023-11-25 22:59                                                                                                 ` waffl3x
2023-09-19 20:24   ` [PATCH 1/2] " Jason Merrill
     [not found] <b=5FiIXwWwO63ZE1ZSZHUIAdWyA2sqGsE3FM7eXfsInWogDyBZRsw8CwNsvFSDmEVmBtdq0pqb4zJ55HN2JCR7boDNramlEfne-R5PWdUXjbA=3D@protonmail.com>

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ffef1807-b7be-43e1-82e3-210dbf66470e@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=waffl3x@protonmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).