c++, contracts: Abstract interfaces to constexpr [NFC].

Message ID 20250917153544.54911-1-iain@sandoe.co.uk
State Committed
Commit f2d9f6635e33ddcc1999fceb1b025635e1b646f9
Headers
Series c++, contracts: Abstract interfaces to constexpr [NFC]. |

Commit Message

Iain Sandoe Sept. 17, 2025, 3:35 p.m. UTC
  Another small preparation patch for C++26 upstreaming.
Tested on x86_64-darwin, powerpc64le-linux,
OK for trunk?
thanks
Iain

--- 8< ---

We want to move to having different representations of the contract
semantic for C++26, since that wants values outside the range that
can be accommodated using the cxx2a mechanisms.  First part, abstract
the interfaces to constexpr, so that they do not see the underlying
source of data.

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_eval_constant_expression): Use revised
	interfaces to determine if contracts are ignored and, if not,
	whether they are evaluated.
	* contracts.h (contract_ignored_p, contract_evaluated_p): New.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
---
 gcc/cp/constexpr.cc |  5 ++---
 gcc/cp/contracts.h  | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)
  

Comments

Jason Merrill Sept. 20, 2025, 1:05 p.m. UTC | #1
On 9/17/25 5:35 PM, Iain Sandoe wrote:
> Another small preparation patch for C++26 upstreaming.
> Tested on x86_64-darwin, powerpc64le-linux,
> OK for trunk?

OK.

> --- 8< ---
> 
> We want to move to having different representations of the contract
> semantic for C++26, since that wants values outside the range that
> can be accommodated using the cxx2a mechanisms.  First part, abstract
> the interfaces to constexpr, so that they do not see the underlying
> source of data.
> 
> gcc/cp/ChangeLog:
> 
> 	* constexpr.cc (cxx_eval_constant_expression): Use revised
> 	interfaces to determine if contracts are ignored and, if not,
> 	whether they are evaluated.
> 	* contracts.h (contract_ignored_p, contract_evaluated_p): New.
> 
> Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
> ---
>   gcc/cp/constexpr.cc |  5 ++---
>   gcc/cp/contracts.h  | 15 +++++++++++++++
>   2 files changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index db363dc2760..b0e55712cbe 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -10112,14 +10112,13 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
>       case PRECONDITION_STMT:
>       case POSTCONDITION_STMT:
>         {
> -	contract_semantic semantic = get_contract_semantic (t);
> -	if (semantic == CCS_IGNORE)
> +	if (contract_ignored_p (t))
>   	  break;
>   
>   	if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
>   			      G_("contract predicate is false in "
>   				 "constant expression"),
> -			      EXPR_LOCATION (t), checked_contract_p (semantic),
> +			      EXPR_LOCATION (t), contract_evaluated_p (t),
>   			      non_constant_p, overflow_p))
>   	  *non_constant_p = true;
>   	r = void_node;
> diff --git a/gcc/cp/contracts.h b/gcc/cp/contracts.h
> index ead07d19fb7..54eacd9a4c5 100644
> --- a/gcc/cp/contracts.h
> +++ b/gcc/cp/contracts.h
> @@ -334,4 +334,19 @@ set_contract_semantic (tree t, contract_semantic semantic)
>   }
>   
>   
> +/* Will this contract be ignored.  */
> +
> +inline bool
> +contract_ignored_p (const_tree contract)
> +{
> +  return (get_contract_semantic (contract) <= CCS_IGNORE);
> +}
> +
> +/* Will this contract be evaluated?  */
> +
> +inline bool
> +contract_evaluated_p (const_tree contract)
> +{
> +  return (get_contract_semantic (contract) >= CCS_NEVER);
> +}
>   #endif /* ! GCC_CP_CONTRACT_H */
  

Patch

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index db363dc2760..b0e55712cbe 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -10112,14 +10112,13 @@  cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
     case PRECONDITION_STMT:
     case POSTCONDITION_STMT:
       {
-	contract_semantic semantic = get_contract_semantic (t);
-	if (semantic == CCS_IGNORE)
+	if (contract_ignored_p (t))
 	  break;
 
 	if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
 			      G_("contract predicate is false in "
 				 "constant expression"),
-			      EXPR_LOCATION (t), checked_contract_p (semantic),
+			      EXPR_LOCATION (t), contract_evaluated_p (t),
 			      non_constant_p, overflow_p))
 	  *non_constant_p = true;
 	r = void_node;
diff --git a/gcc/cp/contracts.h b/gcc/cp/contracts.h
index ead07d19fb7..54eacd9a4c5 100644
--- a/gcc/cp/contracts.h
+++ b/gcc/cp/contracts.h
@@ -334,4 +334,19 @@  set_contract_semantic (tree t, contract_semantic semantic)
 }
 
 
+/* Will this contract be ignored.  */
+
+inline bool
+contract_ignored_p (const_tree contract)
+{
+  return (get_contract_semantic (contract) <= CCS_IGNORE);
+}
+
+/* Will this contract be evaluated?  */
+
+inline bool
+contract_evaluated_p (const_tree contract)
+{
+  return (get_contract_semantic (contract) >= CCS_NEVER);
+}
 #endif /* ! GCC_CP_CONTRACT_H */