From 1fea6f82489006cfec3171f77bde8b5ed3527616 Mon Sep 17 00:00:00 2001
From: Marek Polacek <polacek@redhat.com>
Date: Tue, 24 Sep 2024 17:54:04 -0400
Subject: [PATCH] c++: use TARGET_EXPR accessors

While futzing around with PR116416 I noticed that we can use
the _SLOT and _INITIAL macros to make the code more readable.

gcc/c-family/ChangeLog:

	* c-pretty-print.cc (c_pretty_printer::primary_expression): Use
	TARGET_EXPR accessors.
	(c_pretty_printer::expression): Likewise.

gcc/cp/ChangeLog:

	* coroutines.cc (build_co_await): Use TARGET_EXPR accessors.
	(finish_co_yield_expr): Likewise.
	(register_awaits): Likewise.
	(tmp_target_expr_p): Likewise.
	(flatten_await_stmt): Likewise.
	* error.cc (dump_expr): Likewise.
	* semantics.cc (finish_omp_target_clauses): Likewise.
	* tree.cc (bot_manip): Likewise.
	(cp_tree_equal): Likewise.
	* typeck.cc (cxx_mark_addressable): Likewise.
	(cp_build_compound_expr): Likewise.
	(cp_build_modify_expr): Likewise.
	(check_return_expr): Likewise.

Reviewed-by: Jason Merrill <jason@redhat.com>
---
 gcc/c-family/c-pretty-print.cc | 10 +++++-----
 gcc/cp/coroutines.cc           | 18 +++++++++---------
 gcc/cp/error.cc                |  4 ++--
 gcc/cp/semantics.cc            |  2 +-
 gcc/cp/tree.cc                 | 27 ++++++++++++++-------------
 gcc/cp/typeck.cc               | 12 ++++++------
 6 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/gcc/c-family/c-pretty-print.cc b/gcc/c-family/c-pretty-print.cc
index 26038818d759..0c764aecc3de 100644
--- a/gcc/c-family/c-pretty-print.cc
+++ b/gcc/c-family/c-pretty-print.cc
@@ -1376,14 +1376,14 @@ c_pretty_printer::primary_expression (tree e)
       pp_c_ws_string (this, "__builtin_memcpy");
       pp_c_left_paren (this);
       pp_ampersand (this);
-      primary_expression (TREE_OPERAND (e, 0));
+      primary_expression (TARGET_EXPR_SLOT (e));
       pp_separate_with (this, ',');
       pp_ampersand (this);
-      initializer (TREE_OPERAND (e, 1));
-      if (TREE_OPERAND (e, 2))
+      initializer (TARGET_EXPR_INITIAL (e));
+      if (TARGET_EXPR_CLEANUP (e))
 	{
 	  pp_separate_with (this, ',');
-	  expression (TREE_OPERAND (e, 2));
+	  expression (TARGET_EXPR_CLEANUP (e));
 	}
       pp_c_right_paren (this);
       break;
@@ -2838,7 +2838,7 @@ c_pretty_printer::expression (tree e)
       break;
 
     case TARGET_EXPR:
-      postfix_expression (TREE_OPERAND (e, 1));
+      postfix_expression (TARGET_EXPR_INITIAL (e));
       break;
 
     case BIND_EXPR:
diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index 70c9094d1d19..4c7ea1dd3216 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -1258,7 +1258,7 @@ build_co_await (location_t loc, tree a, suspend_point_kind suspend_kind,
   if (TREE_CODE (awrs_call) == TARGET_EXPR)
     {
       te = awrs_call;
-      awrs_call = TREE_OPERAND (awrs_call, 1);
+      awrs_call = TARGET_EXPR_INITIAL (awrs_call);
     }
   TREE_VEC_ELT (awaiter_calls, 2) = awrs_call; /* await_resume().  */
 
@@ -1447,9 +1447,9 @@ finish_co_yield_expr (location_t kw, tree expr)
 	 its contained await.  Otherwise, just build the CO_YIELD_EXPR.  */
       if (TREE_CODE (op) == TARGET_EXPR)
 	{
-	  tree t = TREE_OPERAND (op, 1);
+	  tree t = TARGET_EXPR_INITIAL (op);
 	  t = build2_loc (kw, CO_YIELD_EXPR, TREE_TYPE (t), expr, t);
-	  TREE_OPERAND (op, 1) = t;
+	  TARGET_EXPR_INITIAL (op) = t;
 	}
       else
 	op = build2_loc (kw, CO_YIELD_EXPR, TREE_TYPE (op), expr, op);
@@ -2711,7 +2711,7 @@ register_awaits (tree *stmt, int *, void *d)
   tree v = TREE_OPERAND (aw_expr, 3);
   tree o = TREE_VEC_ELT (v, 1);
   if (TREE_CODE (o) == TARGET_EXPR)
-    TREE_VEC_ELT (v, 1) = get_target_expr (TREE_OPERAND (o, 1));
+    TREE_VEC_ELT (v, 1) = get_target_expr (TARGET_EXPR_INITIAL (o));
   return NULL_TREE;
 }
 
@@ -2734,7 +2734,7 @@ tmp_target_expr_p (tree t)
 {
   if (TREE_CODE (t) != TARGET_EXPR)
     return false;
-  tree v = TREE_OPERAND (t, 0);
+  tree v = TARGET_EXPR_SLOT (t);
   if (!DECL_ARTIFICIAL (v))
     return false;
   if (DECL_NAME (v))
@@ -2941,7 +2941,7 @@ flatten_await_stmt (var_nest_node *n, hash_set<tree> *promoted,
 		    {
 		      temps_used->add (inner);
 		      gcc_checking_assert
-			(TREE_CODE (TREE_OPERAND (inner, 1)) != COND_EXPR);
+			(TREE_CODE (TARGET_EXPR_INITIAL (inner)) != COND_EXPR);
 		    }
 		}
 		break;
@@ -2972,7 +2972,7 @@ flatten_await_stmt (var_nest_node *n, hash_set<tree> *promoted,
 	  free (buf);
 	  bool already_present = promoted->add (var);
 	  gcc_checking_assert (!already_present);
-	  tree inner = TREE_OPERAND (init, 1);
+	  tree inner = TARGET_EXPR_INITIAL (init);
 	  gcc_checking_assert (TREE_CODE (inner) != COND_EXPR);
 	  init = cp_build_modify_expr (input_location, var, INIT_EXPR, init,
 				       tf_warning_or_error);
@@ -2981,7 +2981,7 @@ flatten_await_stmt (var_nest_node *n, hash_set<tree> *promoted,
 	  if (t == n->init && n->var == NULL_TREE)
 	    {
 	      n->var = var;
-	      proxy_replace pr = {TREE_OPERAND (t, 0), var};
+	      proxy_replace pr = {TARGET_EXPR_SLOT (t), var};
 	      cp_walk_tree (&init, replace_proxy, &pr, NULL);
 	      n->init = init;
 	      if (replace_in)
@@ -2995,7 +2995,7 @@ flatten_await_stmt (var_nest_node *n, hash_set<tree> *promoted,
 	      /* We have to replace the target expr... */
 	      *v.entry = var;
 	      /* ... and any uses of its var.  */
-	      proxy_replace pr = {TREE_OPERAND (t, 0), var};
+	      proxy_replace pr = {TARGET_EXPR_SLOT (t), var};
 	      cp_walk_tree (&n->init, replace_proxy, &pr, NULL);
 	      /* Compiler-generated temporaries can also have uses in
 		 following arms of compound expressions, which will be listed
diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 90e3630e3aa6..65f70c595cf8 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -2525,8 +2525,8 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags)
 	 it initializes anything other that the parameter slot for the
 	 default argument.  Note we may have cleared out the first
 	 operand in expand_expr, so don't go killing ourselves.  */
-      if (TREE_OPERAND (t, 1))
-	dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
+      if (TARGET_EXPR_INITIAL (t))
+	dump_expr (pp, TARGET_EXPR_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
       break;
 
     case POINTER_PLUS_EXPR:
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index a061704e1a27..0cb46c1986ca 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -10446,7 +10446,7 @@ finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
     {
       tree lobj = *i;
       if (TREE_CODE (lobj) == TARGET_EXPR)
-	lobj = TREE_OPERAND (lobj, 0);
+	lobj = TARGET_EXPR_SLOT (lobj);
 
       tree lt = TREE_TYPE (lobj);
       gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index f43febed124c..e8d8b2ab6f70 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3138,17 +3138,17 @@ bot_manip (tree* tp, int* walk_subtrees, void* data_)
     {
       tree u;
 
-      if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
+      if (TREE_CODE (TARGET_EXPR_INITIAL (t)) == AGGR_INIT_EXPR)
 	{
-	  u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
+	  u = build_cplus_new (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
 			       tf_warning_or_error);
 	  if (u == error_mark_node)
 	    return u;
-	  if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
-	    AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
+	  if (AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (t)))
+	    AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (u)) = true;
 	}
       else
-	u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
+	u = force_target_expr (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
 			       tf_warning_or_error);
 
       TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
@@ -3158,12 +3158,12 @@ bot_manip (tree* tp, int* walk_subtrees, void* data_)
 
       /* Map the old variable to the new one.  */
       splay_tree_insert (target_remap,
-			 (splay_tree_key) TREE_OPERAND (t, 0),
-			 (splay_tree_value) TREE_OPERAND (u, 0));
+			 (splay_tree_key) TARGET_EXPR_SLOT (t),
+			 (splay_tree_value) TARGET_EXPR_SLOT (u));
 
-      TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
-						    data.clear_location);
-      if (TREE_OPERAND (u, 1) == error_mark_node)
+      TARGET_EXPR_INITIAL (u) = break_out_target_exprs (TARGET_EXPR_INITIAL (u),
+							data.clear_location);
+      if (TARGET_EXPR_INITIAL (u) == error_mark_node)
 	return error_mark_node;
 
       if (data.clear_location)
@@ -4034,8 +4034,8 @@ cp_tree_equal (tree t1, tree t2)
 
     case TARGET_EXPR:
       {
-	tree o1 = TREE_OPERAND (t1, 0);
-	tree o2 = TREE_OPERAND (t2, 0);
+	tree o1 = TARGET_EXPR_SLOT (t1);
+	tree o2 = TARGET_EXPR_SLOT (t2);
 
 	/* Special case: if either target is an unallocated VAR_DECL,
 	   it means that it's going to be unified with whatever the
@@ -4050,7 +4050,8 @@ cp_tree_equal (tree t1, tree t2)
 	else if (!cp_tree_equal (o1, o2))
 	  return false;
 
-	return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
+	return cp_tree_equal (TARGET_EXPR_INITIAL (t1),
+			      TARGET_EXPR_INITIAL (t2));
       }
 
     case PARM_DECL:
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index b6835286cff3..d1204d922bbd 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -7995,7 +7995,7 @@ cxx_mark_addressable (tree exp, bool array_ref_p)
 
       case TARGET_EXPR:
 	TREE_ADDRESSABLE (x) = 1;
-	cxx_mark_addressable (TREE_OPERAND (x, 0));
+	cxx_mark_addressable (TARGET_EXPR_SLOT (x));
 	return true;
 
       default:
@@ -8195,10 +8195,10 @@ cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
       /* If the rhs is a TARGET_EXPR, then build the compound
 	 expression inside the target_expr's initializer. This
 	 helps the compiler to eliminate unnecessary temporaries.  */
-      tree init = TREE_OPERAND (rhs, 1);
+      tree init = TARGET_EXPR_INITIAL (rhs);
 
       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
-      TREE_OPERAND (rhs, 1) = init;
+      TARGET_EXPR_INITIAL (rhs) = init;
 
       if (eptype)
 	rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
@@ -9861,7 +9861,7 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
 	 expanded without a target.  */
       if (TREE_CODE (newrhs) == TARGET_EXPR)
 	newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
-			 TREE_OPERAND (newrhs, 0));
+			 TARGET_EXPR_SLOT (newrhs));
     }
 
   if (newrhs == error_mark_node)
@@ -11448,9 +11448,9 @@ check_return_expr (tree retval, bool *no_warning, bool *dangling)
       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
       else if (! cfun->returns_struct
 	       && TREE_CODE (retval) == TARGET_EXPR
-	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
+	       && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
-			 TREE_OPERAND (retval, 0));
+			 TARGET_EXPR_SLOT (retval));
       else if (!processing_template_decl
 	       && maybe_warn_about_returning_address_of_local (retval, loc)
 	       && INDIRECT_TYPE_P (valtype))
-- 
GitLab