Skip to content

Commit 11c0183

Browse files
iainsjicama
andcommitted
c++, contracts: omit unnecessary const-wrappers
We need to view class pointers as const, which we do by wrapping them in a view convert expression. However, we do not need to do this if the original is already const-qualified. gcc/cp/ChangeLog: * contracts.cc (view_as_const): Check for const-qualified class pointer before wrapping it. * parser.cc (cp_parser_late_contract_condition): Use revised handling of const-ification of class pointers. (cp_parser_contract_assert): Likewise. (cp_parser_function_contract_specifier): Likewise. Signed-off-by: Iain Sandoe <iain@sandoe.co.uk> Co-authored-by: Jason Merrill <jason@redhat.com>
1 parent f3c25c5 commit 11c0183

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

gcc/cp/contracts.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ finish_contract_condition (cp_expr condition)
482482
tree
483483
view_as_const (tree decl)
484484
{
485-
if (!contract_const_wrapper_p (decl))
485+
if (decl
486+
&& !CP_TYPE_CONST_P (TREE_TYPE (decl)))
486487
{
488+
gcc_checking_assert (!contract_const_wrapper_p (decl));
487489
tree ctype = TREE_TYPE (decl);
488490
location_t loc =
489491
EXPR_P (decl) ? EXPR_LOCATION (decl) : DECL_SOURCE_LOCATION (decl);

gcc/cp/parser.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33582,9 +33582,7 @@ cp_parser_late_contract_condition (cp_parser *parser, tree fn, tree contract)
3358233582

3358333583
/* If we have a current class object, we need to consider
3358433584
it const when processing the contract condition. */
33585-
tree current_class_ref_copy = current_class_ref;
33586-
if (flag_contracts && current_class_ref_copy)
33587-
current_class_ref = view_as_const (current_class_ref_copy);
33585+
current_class_ref = view_as_const (current_class_ref);
3358833586

3358933587
/* Parse the condition, ensuring that parameters or the return variable
3359033588
aren't flagged for use outside the body of a function. */
@@ -33691,8 +33689,7 @@ cp_parser_contract_assert (cp_parser *parser, cp_token *token)
3369133689
/* If we have a current class object, see if we need to consider
3369233690
it const when processing the contract condition. */
3369333691
tree current_class_ref_copy = current_class_ref;
33694-
if (current_class_ref_copy)
33695-
current_class_ref = view_as_const (current_class_ref_copy);
33692+
current_class_ref = view_as_const (current_class_ref);
3369633693

3369733694
/* Parse the condition. */
3369833695
begin_scope (sk_contract, current_function_decl);
@@ -33857,8 +33854,7 @@ cp_parser_function_contract_specifier (cp_parser *parser)
3385733854
/* If we have a current class object, see if we need to consider
3385833855
it const when processing the contract condition. */
3385933856
tree current_class_ref_copy = current_class_ref;
33860-
if (current_class_ref_copy)
33861-
current_class_ref = view_as_const (current_class_ref_copy);
33857+
current_class_ref = view_as_const (current_class_ref);
3386233858

3386333859
/* Parse the condition, ensuring that parameters or the return variable
3386433860
aren't flagged for use outside the body of a function. */

0 commit comments

Comments
 (0)