Skip to content

Commit 11cbfcb

Browse files
committed
c++, contracts: Use contract scope for each contract condition.
This just wraps each contract condition check in a new scope (as required by P2900). I've actually just let it happen for c++2a contracts as well, since we are going to deprecate them (and it apparently does not alter their behaviour). Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
1 parent 3b66c50 commit 11cbfcb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

gcc/cp/cp-tree.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,8 @@ struct GTY(()) saved_scope {
19551955
int x_processing_contract_condition;
19561956
int x_processing_contract_postcondition;
19571957
int suppress_location_wrappers;
1958+
BOOL_BITFIELD x_processing_postcondition : 1;
1959+
BOOL_BITFIELD x_should_constify_contract : 1;
19581960
BOOL_BITFIELD x_processing_explicit_instantiation : 1;
19591961
BOOL_BITFIELD need_pop_function_context : 1;
19601962
BOOL_BITFIELD x_processing_omp_trait_property_expr : 1;
@@ -2039,6 +2041,9 @@ extern GTY(()) struct saved_scope *scope_chain;
20392041

20402042
#define processing_contract_postcondition scope_chain->x_processing_contract_postcondition
20412043

2044+
#define processing_postcondition scope_chain->x_processing_postcondition
2045+
#define should_constify_contract scope_chain->x_should_constify_contract
2046+
20422047
#define in_discarded_stmt scope_chain->discarded_stmt
20432048
#define in_consteval_if_p scope_chain->consteval_if_p
20442049

gcc/cp/parser.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13237,9 +13237,13 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
1323713237
current_class_ref = view_as_const (current_class_ref_copy);
1323813238

1323913239
/* Parse the condition. */
13240+
begin_scope (sk_contract, current_function_decl);
1324013241
++processing_contract_condition;
13242+
processing_postcondition = false;
13243+
should_constify_contract = should_constify;
1324113244
cp_expr condition = cp_parser_conditional_expression (parser);
1324213245
--processing_contract_condition;
13246+
pop_bindings_and_leave_scope ();
1324313247

1324413248
/* Revert (any) constification of the current class object. */
1324513249
current_class_ref = current_class_ref_copy;
@@ -31524,16 +31528,20 @@ cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute,
3152431528

3152531529
/* Parse the condition, ensuring that parameters or the return variable
3152631530
aren't flagged for use outside the body of a function. */
31531+
begin_scope (sk_contract, current_function_decl);
3152731532
++processing_contract_condition;
3152831533
if (postcondition_p)
3152931534
++processing_contract_postcondition;
31535+
processing_postcondition = postcondition_p;
31536+
should_constify_contract = should_constify;
3153031537
cp_expr condition = cp_parser_conditional_expression (parser);
3153131538
if (postcondition_p)
3153231539
--processing_contract_postcondition;
31540+
--processing_contract_condition;
31541+
pop_bindings_and_leave_scope ();
3153331542
/* Revert (any) constification of the current class object. */
3153431543
current_class_ref = current_class_ref_copy;
3153531544
flag_contracts_nonattr_noconst = old_flag_contracts_nonattr_noconst;
31536-
--processing_contract_condition;
3153731545

3153831546
/* For natural syntax, we eat the parens here. For the attribute
3153931547
syntax, it will be done one level up, we just need to skip to it. */
@@ -31643,13 +31651,15 @@ void cp_parser_late_contract_condition (cp_parser *parser,
3164331651

3164431652
/* Parse the condition, ensuring that parameters or the return variable
3164531653
aren't flagged for use outside the body of a function. */
31654+
begin_scope (sk_contract, fn);
3164631655
++processing_contract_condition;
3164731656
if (POSTCONDITION_P (contract))
3164831657
++processing_contract_postcondition;
3164931658
condition = cp_parser_conditional_expression (parser);
3165031659
if (POSTCONDITION_P (contract))
3165131660
--processing_contract_postcondition;
3165231661
--processing_contract_condition;
31662+
pop_bindings_and_leave_scope ();
3165331663

3165431664
if (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
3165531665
error_at (input_location,

0 commit comments

Comments
 (0)