]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++, contracts: Add a contract scope per basic.scope.contract.
authorIain Sandoe <iain@sandoe.co.uk>
Fri, 31 Oct 2025 11:02:13 +0000 (11:02 +0000)
committerIain Sandoe <iain@sandoe.co.uk>
Wed, 28 Jan 2026 01:23:18 +0000 (01:23 +0000)
This adds a scope for contract assertions as per the standard section
referenced.  Split out here because it is the only part of the implementation
that touches the name lookup files.

gcc/cp/ChangeLog:

* cp-tree.h (processing_contract_condition): New.
* name-lookup.cc (cp_binding_level_descriptor): Add contract scope.
(begin_scope): Likewise.
* name-lookup.h (enum scope_kind): Add sk_contract.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
gcc/cp/cp-tree.h
gcc/cp/name-lookup.cc
gcc/cp/name-lookup.h

index 0f6df4b0ca74449a9876dea840fa4d826cef9fa5..2046818301f866c2c5423fc6f343bdb4150fd739 100644 (file)
@@ -2154,6 +2154,13 @@ extern GTY(()) struct saved_scope *scope_chain;
 #define processing_explicit_instantiation scope_chain->x_processing_explicit_instantiation
 #define processing_omp_trait_property_expr scope_chain->x_processing_omp_trait_property_expr
 
+/* Nonzero if we are parsing the expression of a contract condition. These
+   expressions appear outside the parameter list (like a trailing return
+   type), but are potentially evaluated.  */
+
+#define processing_contract_condition \
+  (scope_chain->bindings->kind == sk_contract)
+
 #define in_discarded_stmt scope_chain->discarded_stmt
 #define in_consteval_if_p scope_chain->consteval_if_p
 #define in_expansion_stmt scope_chain->expansion_stmt
index 16aae34ee287b41a453365b3b4d28ab0d9019573..823e28bef76f5627915c25593981241458c0e5c7 100644 (file)
@@ -4747,7 +4747,8 @@ cp_binding_level_descriptor (cp_binding_level *scope)
     "template-explicit-spec-scope",
     "transaction-scope",
     "openmp-scope",
-    "lambda-scope"
+    "lambda-scope",
+    "contract-check-scope"
   };
   static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
                 "must keep names aligned with scope_kind enum");
@@ -4838,6 +4839,7 @@ begin_scope (scope_kind kind, tree entity)
     case sk_scoped_enum:
     case sk_transaction:
     case sk_omp:
+    case sk_contract:
     case sk_stmt_expr:
     case sk_lambda:
       scope->keep = keep_next_level_flag;
index 4c0ca435762b06e3eab905dfca128cde7e5a7db1..4c167cd135c4b5c8fbe233ce4cb1517315cc22d3 100644 (file)
@@ -229,6 +229,8 @@ enum scope_kind {
   sk_transaction,    /* A synchronized or atomic statement.  */
   sk_omp,           /* An OpenMP structured block.  */
   sk_lambda,        /* A lambda scope.  */
+  sk_contract,      /* A C++26 contract-assertion scope.
+                       [basic.scope.contract] */
   sk_count          /* Number of scope_kind enumerations.  */
 };