diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 6fbd27afd50..e8aee0f9927 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2014-06-27 Paolo Carlini + + * parser.c (cp_parser_compound_literal_p): New. + (cp_parser_postfix_expression, cp_parser_sizeof_operand): Use it. + 2014-06-26 Jason Merrill * parser.c (cp_parser_for_init_statement): Change range-for error diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index a7edd4181ae..057b6ca14cc 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -2480,7 +2480,9 @@ static bool cp_parser_is_keyword static tree cp_parser_make_typename_type (cp_parser *, tree, tree, location_t location); static cp_declarator * cp_parser_make_indirect_declarator - (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree); + (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree); +static bool cp_parser_compound_literal_p + (cp_parser *); /* Returns nonzero if we are parsing tentatively. */ @@ -5609,6 +5611,30 @@ cp_parser_qualifying_entity (cp_parser *parser, return scope; } +/* Return true if we are looking at a compound-literal, false otherwise. */ + +static bool +cp_parser_compound_literal_p (cp_parser *parser) +{ + /* Consume the `('. */ + cp_lexer_consume_token (parser->lexer); + + cp_lexer_save_tokens (parser->lexer); + + /* Skip tokens until the next token is a closing parenthesis. + If we find the closing `)', and the next token is a `{', then + we are looking at a compound-literal. */ + bool compound_literal_p + = (cp_parser_skip_to_closing_parenthesis (parser, false, false, + /*consume_paren=*/true) + && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); + + /* Roll back the tokens we skipped. */ + cp_lexer_rollback_tokens (parser->lexer); + + return compound_literal_p; +} + /* Parse a postfix-expression. postfix-expression: @@ -5917,25 +5943,12 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p, && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) { tree initializer = NULL_TREE; - bool compound_literal_p; cp_parser_parse_tentatively (parser); - /* Consume the `('. */ - cp_lexer_consume_token (parser->lexer); /* Avoid calling cp_parser_type_id pointlessly, see comment in cp_parser_cast_expression about c++/29234. */ - cp_lexer_save_tokens (parser->lexer); - - compound_literal_p - = (cp_parser_skip_to_closing_parenthesis (parser, false, false, - /*consume_paren=*/true) - && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); - - /* Roll back the tokens we skipped. */ - cp_lexer_rollback_tokens (parser->lexer); - - if (!compound_literal_p) + if (!cp_parser_compound_literal_p (parser)) cp_parser_simulate_error (parser); else { @@ -23966,31 +23979,15 @@ cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) { tree type = NULL_TREE; - bool compound_literal_p; /* We can't be sure yet whether we're looking at a type-id or an expression. */ cp_parser_parse_tentatively (parser); - /* Consume the `('. */ - cp_lexer_consume_token (parser->lexer); /* Note: as a GNU Extension, compound literals are considered postfix-expressions as they are in C99, so they are valid arguments to sizeof. See comment in cp_parser_cast_expression for details. */ - cp_lexer_save_tokens (parser->lexer); - /* Skip tokens until the next token is a closing parenthesis. - If we find the closing `)', and the next token is a `{', then - we are looking at a compound-literal. */ - compound_literal_p - = (cp_parser_skip_to_closing_parenthesis (parser, false, false, - /*consume_paren=*/true) - && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); - /* Roll back the tokens we skipped. */ - cp_lexer_rollback_tokens (parser->lexer); - /* If we were looking at a compound-literal, simulate an error - so that the call to cp_parser_parse_definitely below will - fail. */ - if (compound_literal_p) + if (cp_parser_compound_literal_p (parser)) cp_parser_simulate_error (parser); else {