Skip to content

Commit 219c3c7

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 <[email protected]>
1 parent 50c2b3d commit 219c3c7

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
@@ -1954,6 +1954,8 @@ struct GTY(()) saved_scope {
19541954
int x_processing_contract_condition;
19551955
int x_processing_contract_postcondition;
19561956
int suppress_location_wrappers;
1957+
BOOL_BITFIELD x_processing_postcondition : 1;
1958+
BOOL_BITFIELD x_should_constify_contract : 1;
19571959
BOOL_BITFIELD x_processing_explicit_instantiation : 1;
19581960
BOOL_BITFIELD need_pop_function_context : 1;
19591961
BOOL_BITFIELD x_processing_omp_trait_property_expr : 1;
@@ -2038,6 +2040,9 @@ extern GTY(()) struct saved_scope *scope_chain;
20382040

20392041
#define processing_contract_postcondition scope_chain->x_processing_contract_postcondition
20402042

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

gcc/cp/parser.cc

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

1314013140
/* Parse the condition. */
13141+
begin_scope (sk_contract, current_function_decl);
1314113142
++processing_contract_condition;
13143+
processing_postcondition = false;
13144+
should_constify_contract = should_constify;
1314213145
cp_expr condition = cp_parser_conditional_expression (parser);
1314313146
--processing_contract_condition;
13147+
pop_bindings_and_leave_scope ();
1314413148

1314513149
/* Revert (any) constification of the current class object. */
1314613150
current_class_ref = current_class_ref_copy;
@@ -31393,16 +31397,20 @@ cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute,
3139331397

3139431398
/* Parse the condition, ensuring that parameters or the return variable
3139531399
aren't flagged for use outside the body of a function. */
31400+
begin_scope (sk_contract, current_function_decl);
3139631401
++processing_contract_condition;
3139731402
if (postcondition_p)
3139831403
++processing_contract_postcondition;
31404+
processing_postcondition = postcondition_p;
31405+
should_constify_contract = should_constify;
3139931406
cp_expr condition = cp_parser_conditional_expression (parser);
3140031407
if (postcondition_p)
3140131408
--processing_contract_postcondition;
31409+
--processing_contract_condition;
31410+
pop_bindings_and_leave_scope ();
3140231411
/* Revert (any) constification of the current class object. */
3140331412
current_class_ref = current_class_ref_copy;
3140431413
flag_contracts_nonattr_noconst = old_flag_contracts_nonattr_noconst;
31405-
--processing_contract_condition;
3140631414

3140731415
/* For natural syntax, we eat the parens here. For the attribute
3140831416
syntax, it will be done one level up, we just need to skip to it. */
@@ -31512,13 +31520,15 @@ void cp_parser_late_contract_condition (cp_parser *parser,
3151231520

3151331521
/* Parse the condition, ensuring that parameters or the return variable
3151431522
aren't flagged for use outside the body of a function. */
31523+
begin_scope (sk_contract, fn);
3151531524
++processing_contract_condition;
3151631525
if (POSTCONDITION_P (contract))
3151731526
++processing_contract_postcondition;
3151831527
condition = cp_parser_conditional_expression (parser);
3151931528
if (POSTCONDITION_P (contract))
3152031529
--processing_contract_postcondition;
3152131530
--processing_contract_condition;
31531+
pop_bindings_and_leave_scope ();
3152231532

3152331533
if (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
3152431534
error_at (input_location,

0 commit comments

Comments
 (0)