[RFC] c++/reflection: add null_reflection_p

Message ID 20260506182006.1867406-1-jason@redhat.com
State New
Headers
Series [RFC] c++/reflection: add null_reflection_p |

Commit Message

Jason Merrill May 6, 2026, 6:18 p.m. UTC
  Tested x86_64-pc-linux-gnu.  Does the spelling in dump_expr make sense to you,
or would you prefer something else?

-- 8< --

I wanted to improve %E of info{}, and it seemed desirable to have a
shorter way to spell this test.

gcc/cp/ChangeLog:

	* reflect.cc (null_reflection_p): New.
	(splice): Use it.
	* error.cc (dump_expr): Use it.
	* cp-tree.h (null_reflection_p): Declare.
---
 gcc/cp/cp-tree.h  |  1 +
 gcc/cp/error.cc   |  5 +++++
 gcc/cp/reflect.cc | 10 +++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)


base-commit: b1cd9bd9908b0f05430e74c5cc82927b39997c42
  

Comments

Jakub Jelinek May 6, 2026, 6:28 p.m. UTC | #1
On Wed, May 06, 2026 at 02:18:06PM -0400, Jason Merrill wrote:
> --- a/gcc/cp/error.cc
> +++ b/gcc/cp/error.cc
> @@ -3331,6 +3331,11 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags)
>  
>      case REFLECT_EXPR:
>        {
> +	if (null_reflection_p (t))
> +	  {
> +	    pp_cxx_ws_string (pp, "std::info{}");

"std::meta::info{}"

Otherwise LGTM.

	Jakub
  
Marek Polacek May 6, 2026, 7:08 p.m. UTC | #2
On Wed, May 06, 2026 at 08:28:05PM +0200, Jakub Jelinek wrote:
> On Wed, May 06, 2026 at 02:18:06PM -0400, Jason Merrill wrote:
> > --- a/gcc/cp/error.cc
> > +++ b/gcc/cp/error.cc
> > @@ -3331,6 +3331,11 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags)
> >  
> >      case REFLECT_EXPR:
> >        {
> > +	if (null_reflection_p (t))
> > +	  {
> > +	    pp_cxx_ws_string (pp, "std::info{}");
> 
> "std::meta::info{}"
> 
> Otherwise LGTM.

Yeah, looks good with that fixed.

Marek
  
Jason Merrill May 11, 2026, 9:53 p.m. UTC | #3
On 5/6/26 3:08 PM, Marek Polacek wrote:
> On Wed, May 06, 2026 at 08:28:05PM +0200, Jakub Jelinek wrote:
>> On Wed, May 06, 2026 at 02:18:06PM -0400, Jason Merrill wrote:
>>> --- a/gcc/cp/error.cc
>>> +++ b/gcc/cp/error.cc
>>> @@ -3331,6 +3331,11 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags)
>>>   
>>>       case REFLECT_EXPR:
>>>         {
>>> +	if (null_reflection_p (t))
>>> +	  {
>>> +	    pp_cxx_ws_string (pp, "std::info{}");
>>
>> "std::meta::info{}"
>>
>> Otherwise LGTM.
> 
> Yeah, looks good with that fixed.

Thanks, pushing the attached.
  

Patch

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b94c7a8ca99..4e166c621b0 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -9454,6 +9454,7 @@  extern tree process_metafunction (const constexpr_ctx *, tree, tree,
 				  bool *, bool *, tree *);
 extern tree get_reflection (location_t, tree, reflect_kind = REFLECT_UNDEF);
 extern tree get_null_reflection () ATTRIBUTE_PURE;
+extern bool null_reflection_p (const_tree) ATTRIBUTE_PURE;
 extern tree splice (tree);
 extern bool check_out_of_consteval_use (tree, bool = true);
 extern bool consteval_only_p (tree) ATTRIBUTE_PURE;
diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 5f4cb477230..97988dbdd49 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -3331,6 +3331,11 @@  dump_expr (cxx_pretty_printer *pp, tree t, int flags)
 
     case REFLECT_EXPR:
       {
+	if (null_reflection_p (t))
+	  {
+	    pp_cxx_ws_string (pp, "std::info{}");
+	    break;
+	  }
 	pp_string (pp, "^^");
 	tree h = REFLECT_EXPR_HANDLE (t);
 	if (DECL_P (h))
diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index beaa7150007..a168d823a4c 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -267,6 +267,14 @@  get_null_reflection ()
   return null_reflection;
 }
 
+/* True iff T is a null reflection.  */
+
+bool
+null_reflection_p (const_tree t)
+{
+  return REFLECT_EXPR_HANDLE (t) == unknown_type_node;
+}
+
 /* Do strip_typedefs on T, but only for types.  */
 
 static tree
@@ -8525,7 +8533,7 @@  splice (tree refl)
       return error_mark_node;
     }
 
-  if (compare_reflections (refl, get_null_reflection ()))
+  if (null_reflection_p (refl))
     {
       error_at (loc, "cannot splice a null reflection");
       return error_mark_node;