Re: [PATCH] c++: use conversion_obstack_sentinel throughout

2023-08-28 Thread Jason Merrill via Gcc-patches

On 8/25/23 12:33, Patrick Palka wrote:

Boostrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?


OK.


-- >8 --

This replaces manual memory management via conversion_obstack_alloc(0)
and obstack_free with the recently added conversion_obstack_sentinel,
and also uses the latter in build_user_type_conversion and
build_operator_new_call.

gcc/cp/ChangeLog:

* call.cc (build_user_type_conversion): Free allocated
conversions.
(build_converted_constant_expr_internal): Use
conversion_obstack_sentinel instead.
(perform_dguide_overload_resolution): Likewise.
(build_new_function_call): Likewise.
(build_operator_new_call): Free allocated conversions.
(build_op_call): Use conversion_obstack_sentinel instead.
(build_conditional_expr): Use conversion_obstack_sentinel
instead, and hoist it out to the outermost scope.
(build_new_op): Use conversion_obstack_sentinel instead
and set it up before the first goto.  Remove second unneeded goto.
(build_op_subscript): Use conversion_obstack_sentinel instead.
(ref_conv_binds_to_temporary): Likewise.
(build_new_method_call): Likewise.
(can_convert_arg): Likewise.
(can_convert_arg_bad): Likewise.
(perform_implicit_conversion_flags): Likewise.
(perform_direct_initialization_if_possible): Likewise.
(initialize_reference): Likewise.
---
  gcc/cp/call.cc | 107 ++---
  1 file changed, 22 insertions(+), 85 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 673ec91d60e..432ac99b4bb 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4646,6 +4646,9 @@ build_user_type_conversion (tree totype, tree expr, int 
flags,
tree ret;
  
auto_cond_timevar tv (TV_OVERLOAD);

+
+  conversion_obstack_sentinel cos;
+
cand = build_user_type_conversion_1 (totype, expr, flags, complain);
  
if (cand)

@@ -4698,15 +4701,13 @@ build_converted_constant_expr_internal (tree type, tree 
expr,
int flags, tsubst_flags_t complain)
  {
conversion *conv;
-  void *p;
tree t;
location_t loc = cp_expr_loc_or_input_loc (expr);
  
if (error_operand_p (expr))

  return error_mark_node;
  
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */

-  p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
  
conv = implicit_conversion (type, TREE_TYPE (expr), expr,

  /*c_cast_p=*/false, flags, complain);
@@ -4815,9 +4816,6 @@ build_converted_constant_expr_internal (tree type, tree 
expr,
expr = error_mark_node;
  }
  
-  /* Free all the conversions we allocated.  */

-  obstack_free (&conversion_obstack, p);
-
return expr;
  }
  
@@ -4985,8 +4983,7 @@ perform_dguide_overload_resolution (tree dguides, const vec *args,
  
gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
  
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */

-  void *p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
  
z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,

   &any_viable_p, complain);
@@ -4999,9 +4996,6 @@ perform_dguide_overload_resolution (tree dguides, const 
vec *args,
else
  result = cand->fn;
  
-  /* Free all the conversions we allocated.  */

-  obstack_free (&conversion_obstack, p);
-
return result;
  }
  
@@ -5015,7 +5009,6 @@ build_new_function_call (tree fn, vec **args,

  {
struct z_candidate *candidates, *cand;
bool any_viable_p;
-  void *p;
tree result;
  
if (args != NULL && *args != NULL)

@@ -5028,8 +5021,7 @@ build_new_function_call (tree fn, vec **args,
if (flag_tm)
  tm_malloc_replacement (fn);
  
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */

-  p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
  
cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,

  complain);
@@ -5061,9 +5053,6 @@ build_new_function_call (tree fn, vec **args,
  == BUILT_IN_NORMAL)
 result = coro_validate_builtin_call (result);
  
-  /* Free all the conversions we allocated.  */

-  obstack_free (&conversion_obstack, p);
-
return result;
  }
  
@@ -5108,6 +5097,8 @@ build_operator_new_call (tree fnname, vec **args,

if (*args == NULL)
  return error_mark_node;
  
+  conversion_obstack_sentinel cos;

+
/* Based on:
  
 [expr.new]

@@ -5234,7 +5225,6 @@ build_op_call (tree obj, vec **args, 
tsubst_flags_t complain)
tree fns, convs, first_mem_arg = NULL_TREE;
bool any_viable_p;
tree result = NULL_TREE;
-  void *p;
  
auto_cond_timevar tv (TV_OVERLOAD);
  
@@ -5273,8 +5263,7 @@ build_op_call (tree obj, vec **args, tsubst_flags_t complain)

return error_mark_node;
  }
  

Re: [PATCH] c++: use conversion_obstack_sentinel throughout

2023-08-25 Thread Marek Polacek via Gcc-patches
On Fri, Aug 25, 2023 at 12:33:31PM -0400, Patrick Palka via Gcc-patches wrote:
> Boostrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

Very nice.  LGTM.
 
> -- >8 --
> 
> This replaces manual memory management via conversion_obstack_alloc(0)
> and obstack_free with the recently added conversion_obstack_sentinel,
> and also uses the latter in build_user_type_conversion and
> build_operator_new_call.
> 
> gcc/cp/ChangeLog:
> 
>   * call.cc (build_user_type_conversion): Free allocated
>   conversions.
>   (build_converted_constant_expr_internal): Use
>   conversion_obstack_sentinel instead.
>   (perform_dguide_overload_resolution): Likewise.
>   (build_new_function_call): Likewise.
>   (build_operator_new_call): Free allocated conversions.
>   (build_op_call): Use conversion_obstack_sentinel instead.
>   (build_conditional_expr): Use conversion_obstack_sentinel
>   instead, and hoist it out to the outermost scope.
>   (build_new_op): Use conversion_obstack_sentinel instead
>   and set it up before the first goto.  Remove second unneeded goto.
>   (build_op_subscript): Use conversion_obstack_sentinel instead.
>   (ref_conv_binds_to_temporary): Likewise.
>   (build_new_method_call): Likewise.
>   (can_convert_arg): Likewise.
>   (can_convert_arg_bad): Likewise.
>   (perform_implicit_conversion_flags): Likewise.
>   (perform_direct_initialization_if_possible): Likewise.
>   (initialize_reference): Likewise.
> ---
>  gcc/cp/call.cc | 107 ++---
>  1 file changed, 22 insertions(+), 85 deletions(-)
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 673ec91d60e..432ac99b4bb 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -4646,6 +4646,9 @@ build_user_type_conversion (tree totype, tree expr, int 
> flags,
>tree ret;
>  
>auto_cond_timevar tv (TV_OVERLOAD);
> +
> +  conversion_obstack_sentinel cos;
> +
>cand = build_user_type_conversion_1 (totype, expr, flags, complain);
>  
>if (cand)
> @@ -4698,15 +4701,13 @@ build_converted_constant_expr_internal (tree type, 
> tree expr,
>   int flags, tsubst_flags_t complain)
>  {
>conversion *conv;
> -  void *p;
>tree t;
>location_t loc = cp_expr_loc_or_input_loc (expr);
>  
>if (error_operand_p (expr))
>  return error_mark_node;
>  
> -  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
> -  p = conversion_obstack_alloc (0);
> +  conversion_obstack_sentinel cos;
>  
>conv = implicit_conversion (type, TREE_TYPE (expr), expr,
> /*c_cast_p=*/false, flags, complain);
> @@ -4815,9 +4816,6 @@ build_converted_constant_expr_internal (tree type, tree 
> expr,
>expr = error_mark_node;
>  }
>  
> -  /* Free all the conversions we allocated.  */
> -  obstack_free (&conversion_obstack, p);
> -
>return expr;
>  }
>  
> @@ -4985,8 +4983,7 @@ perform_dguide_overload_resolution (tree dguides, const 
> vec *args,
>  
>gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
>  
> -  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
> -  void *p = conversion_obstack_alloc (0);
> +  conversion_obstack_sentinel cos;
>  
>z_candidate *cand = perform_overload_resolution (dguides, args, 
> &candidates,
>  &any_viable_p, complain);
> @@ -4999,9 +4996,6 @@ perform_dguide_overload_resolution (tree dguides, const 
> vec *args,
>else
>  result = cand->fn;
>  
> -  /* Free all the conversions we allocated.  */
> -  obstack_free (&conversion_obstack, p);
> -
>return result;
>  }
>  
> @@ -5015,7 +5009,6 @@ build_new_function_call (tree fn, vec 
> **args,
>  {
>struct z_candidate *candidates, *cand;
>bool any_viable_p;
> -  void *p;
>tree result;
>  
>if (args != NULL && *args != NULL)
> @@ -5028,8 +5021,7 @@ build_new_function_call (tree fn, vec 
> **args,
>if (flag_tm)
>  tm_malloc_replacement (fn);
>  
> -  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
> -  p = conversion_obstack_alloc (0);
> +  conversion_obstack_sentinel cos;
>  
>cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
> complain);
> @@ -5061,9 +5053,6 @@ build_new_function_call (tree fn, vec 
> **args,
> == BUILT_IN_NORMAL)
> result = coro_validate_builtin_call (result);
>  
> -  /* Free all the conversions we allocated.  */
> -  obstack_free (&conversion_obstack, p);
> -
>return result;
>  }
>  
> @@ -5108,6 +5097,8 @@ build_operator_new_call (tree fnname, vec 
> **args,
>if (*args == NULL)
>  return error_mark_node;
>  
> +  conversion_obstack_sentinel cos;
> +
>/* Based on:
>  
> [expr.new]
> @@ -5234,7 +5225,6 @@ build_op_call (tree obj, vec **args, 
> tsubst_flags_t complain)
>tree fns, convs, first_mem_arg = NULL_TREE;
>bool any_

[PATCH] c++: use conversion_obstack_sentinel throughout

2023-08-25 Thread Patrick Palka via Gcc-patches
Boostrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

This replaces manual memory management via conversion_obstack_alloc(0)
and obstack_free with the recently added conversion_obstack_sentinel,
and also uses the latter in build_user_type_conversion and
build_operator_new_call.

gcc/cp/ChangeLog:

* call.cc (build_user_type_conversion): Free allocated
conversions.
(build_converted_constant_expr_internal): Use
conversion_obstack_sentinel instead.
(perform_dguide_overload_resolution): Likewise.
(build_new_function_call): Likewise.
(build_operator_new_call): Free allocated conversions.
(build_op_call): Use conversion_obstack_sentinel instead.
(build_conditional_expr): Use conversion_obstack_sentinel
instead, and hoist it out to the outermost scope.
(build_new_op): Use conversion_obstack_sentinel instead
and set it up before the first goto.  Remove second unneeded goto.
(build_op_subscript): Use conversion_obstack_sentinel instead.
(ref_conv_binds_to_temporary): Likewise.
(build_new_method_call): Likewise.
(can_convert_arg): Likewise.
(can_convert_arg_bad): Likewise.
(perform_implicit_conversion_flags): Likewise.
(perform_direct_initialization_if_possible): Likewise.
(initialize_reference): Likewise.
---
 gcc/cp/call.cc | 107 ++---
 1 file changed, 22 insertions(+), 85 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 673ec91d60e..432ac99b4bb 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4646,6 +4646,9 @@ build_user_type_conversion (tree totype, tree expr, int 
flags,
   tree ret;
 
   auto_cond_timevar tv (TV_OVERLOAD);
+
+  conversion_obstack_sentinel cos;
+
   cand = build_user_type_conversion_1 (totype, expr, flags, complain);
 
   if (cand)
@@ -4698,15 +4701,13 @@ build_converted_constant_expr_internal (tree type, tree 
expr,
int flags, tsubst_flags_t complain)
 {
   conversion *conv;
-  void *p;
   tree t;
   location_t loc = cp_expr_loc_or_input_loc (expr);
 
   if (error_operand_p (expr))
 return error_mark_node;
 
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
-  p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
 
   conv = implicit_conversion (type, TREE_TYPE (expr), expr,
  /*c_cast_p=*/false, flags, complain);
@@ -4815,9 +4816,6 @@ build_converted_constant_expr_internal (tree type, tree 
expr,
   expr = error_mark_node;
 }
 
-  /* Free all the conversions we allocated.  */
-  obstack_free (&conversion_obstack, p);
-
   return expr;
 }
 
@@ -4985,8 +4983,7 @@ perform_dguide_overload_resolution (tree dguides, const 
vec *args,
 
   gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
 
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
-  void *p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
 
   z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
   &any_viable_p, complain);
@@ -4999,9 +4996,6 @@ perform_dguide_overload_resolution (tree dguides, const 
vec *args,
   else
 result = cand->fn;
 
-  /* Free all the conversions we allocated.  */
-  obstack_free (&conversion_obstack, p);
-
   return result;
 }
 
@@ -5015,7 +5009,6 @@ build_new_function_call (tree fn, vec **args,
 {
   struct z_candidate *candidates, *cand;
   bool any_viable_p;
-  void *p;
   tree result;
 
   if (args != NULL && *args != NULL)
@@ -5028,8 +5021,7 @@ build_new_function_call (tree fn, vec **args,
   if (flag_tm)
 tm_malloc_replacement (fn);
 
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
-  p = conversion_obstack_alloc (0);
+  conversion_obstack_sentinel cos;
 
   cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
  complain);
@@ -5061,9 +5053,6 @@ build_new_function_call (tree fn, vec **args,
  == BUILT_IN_NORMAL)
result = coro_validate_builtin_call (result);
 
-  /* Free all the conversions we allocated.  */
-  obstack_free (&conversion_obstack, p);
-
   return result;
 }
 
@@ -5108,6 +5097,8 @@ build_operator_new_call (tree fnname, vec 
**args,
   if (*args == NULL)
 return error_mark_node;
 
+  conversion_obstack_sentinel cos;
+
   /* Based on:
 
[expr.new]
@@ -5234,7 +5225,6 @@ build_op_call (tree obj, vec **args, 
tsubst_flags_t complain)
   tree fns, convs, first_mem_arg = NULL_TREE;
   bool any_viable_p;
   tree result = NULL_TREE;
-  void *p;
 
   auto_cond_timevar tv (TV_OVERLOAD);
 
@@ -5273,8 +5263,7 @@ build_op_call (tree obj, vec **args, 
tsubst_flags_t complain)
return error_mark_node;
 }
 
-  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
-  p = conversion_obstack_alloc (0);
+  conversion_obstack_sentine