X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=gcc%2Fcp%2Fparser.c;h=3ba92852a77ba24233f01ee7b5a16ba87292eca0;hb=a32f68f58d919e02f885b817337f5ad369dfd156;hp=db3fa63164b40056b7a6ffc17cb16243fdde9746;hpb=653e540576c50568687d95e09f5f3322fc15e227;p=pf3gnuchains%2Fgcc-fork.git diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index db3fa63164b..3ba92852a77 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License along with GCC; see the file COPYING. If not, write to the Free - Software Foundation, 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include "config.h" #include "system.h" @@ -36,6 +36,7 @@ #include "toplev.h" #include "output.h" #include "target.h" +#include "cgraph.h" #include "c-common.h" @@ -55,10 +56,16 @@ typedef struct cp_token GTY (()) ENUM_BITFIELD (rid) keyword : 8; /* Token flags. */ unsigned char flags; + /* Identifier for the pragma. */ + ENUM_BITFIELD (pragma_kind) pragma_kind : 6; /* True if this token is from a system header. */ BOOL_BITFIELD in_system_header : 1; /* True if this token is from a context where it is implicitly extern "C" */ BOOL_BITFIELD implicit_extern_c : 1; + /* True for a CPP_NAME token that is not a keyword (i.e., for which + KEYWORD is RID_MAX) iff this name was looked up and found to be + ambiguous. An error has already been reported. */ + BOOL_BITFIELD ambiguous_p : 1; /* The value associated with this token, if any. */ tree value; /* The location at which this token was found. */ @@ -72,7 +79,7 @@ DEF_VEC_ALLOC_P (cp_token_position,heap); static const cp_token eof_token = { - CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE, + CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE, #if USE_MAPPED_LOCATION 0 #else @@ -108,11 +115,15 @@ typedef struct cp_lexer GTY (()) tokens. */ VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens; + /* The next lexer in a linked list of lexers. */ + struct cp_lexer *next; + /* True if we should output debugging information. */ bool debugging_p; - /* The next lexer in a linked list of lexers. */ - struct cp_lexer *next; + /* True if we're in the context of parsing a pragma, and should not + increment past the end-of-line marker. */ + bool in_pragma; } cp_lexer; /* cp_token_cache is a range of tokens. There is no need to represent @@ -162,8 +173,6 @@ static void cp_lexer_purge_token (cp_lexer *); static void cp_lexer_purge_tokens_after (cp_lexer *, cp_token_position); -static void cp_lexer_handle_pragma - (cp_lexer *); static void cp_lexer_save_tokens (cp_lexer *); static void cp_lexer_commit_tokens @@ -192,6 +201,9 @@ static void cp_lexer_stop_debugging static cp_token_cache *cp_token_cache_new (cp_token *, cp_token *); +static void cp_parser_initial_pragma + (cp_token *); + /* Manifest constants. */ #define CP_LEXER_BUFFER_SIZE 10000 #define CP_SAVED_TOKEN_STACK 5 @@ -240,17 +252,12 @@ cp_lexer_new_main (void) size_t space; cp_token *buffer; - /* It's possible that lexing the first token will load a PCH file, - which is a GC collection point. So we have to grab the first - token before allocating any memory. Pragmas must not be deferred - as -fpch-preprocess can generate a pragma to load the PCH file in - the preprocessed output used by -save-temps. */ - cp_lexer_get_preprocessor_token (NULL, &first_token); - - /* Tell cpplib we want CPP_PRAGMA tokens. */ - cpp_get_options (parse_in)->defer_pragmas = true; + /* It's possible that parsing the first pragma will load a PCH file, + which is a GC collection point. So we have to do that before + allocating any memory. */ + cp_parser_initial_pragma (&first_token); - /* Tell c_lex not to merge string constants. */ + /* Tell c_lex_with_flags not to merge string constants. */ c_lex_return_raw_strings = true; c_common_no_more_pch (); @@ -267,7 +274,7 @@ cp_lexer_new_main (void) /* Create the buffer. */ alloc = CP_LEXER_BUFFER_SIZE; - buffer = ggc_alloc (alloc * sizeof (cp_token)); + buffer = GGC_NEWVEC (cp_token, alloc); /* Put the first token in the buffer. */ space = alloc; @@ -282,7 +289,7 @@ cp_lexer_new_main (void) { space = alloc; alloc *= 2; - buffer = ggc_realloc (buffer, alloc * sizeof (cp_token)); + buffer = GGC_RESIZEVEC (cp_token, buffer, alloc); pos = buffer + space; } cp_lexer_get_preprocessor_token (lexer, pos); @@ -292,10 +299,10 @@ cp_lexer_new_main (void) lexer->last_token = pos; lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token; - /* Pragma processing (via cpp_handle_deferred_pragma) may result in - direct calls to c_lex. Those callers all expect c_lex to do - string constant concatenation. */ - c_lex_return_raw_strings = false; + /* Subsequent preprocessor diagnostics should use compiler + diagnostic functions to get the compiler source location. */ + cpp_get_options (parse_in)->client_diagnostic = true; + cpp_get_callbacks (parse_in)->error = cp_cpp_error; gcc_assert (lexer->next_token->type != CPP_PURGED); return lexer; @@ -386,6 +393,8 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , /* Get a new token from the preprocessor. */ token->type = c_lex_with_flags (&token->value, &token->location, &token->flags); + token->keyword = RID_MAX; + token->pragma_kind = PRAGMA_NONE; token->in_system_header = in_system_header; /* On some systems, some header files are surrounded by an @@ -396,18 +405,25 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , token->implicit_extern_c = is_extern_c > 0; /* Check to see if this token is a keyword. */ - if (token->type == CPP_NAME - && C_IS_RESERVED_WORD (token->value)) + if (token->type == CPP_NAME) { - /* Mark this token as a keyword. */ - token->type = CPP_KEYWORD; - /* Record which keyword. */ - token->keyword = C_RID_CODE (token->value); - /* Update the value. Some keywords are mapped to particular - entities, rather than simply having the value of the - corresponding IDENTIFIER_NODE. For example, `__const' is - mapped to `const'. */ - token->value = ridpointers[token->keyword]; + if (C_IS_RESERVED_WORD (token->value)) + { + /* Mark this token as a keyword. */ + token->type = CPP_KEYWORD; + /* Record which keyword. */ + token->keyword = C_RID_CODE (token->value); + /* Update the value. Some keywords are mapped to particular + entities, rather than simply having the value of the + corresponding IDENTIFIER_NODE. For example, `__const' is + mapped to `const'. */ + token->value = ridpointers[token->keyword]; + } + else + { + token->ambiguous_p = false; + token->keyword = RID_MAX; + } } /* Handle Objective-C++ keywords. */ else if (token->type == CPP_AT_NAME) @@ -426,8 +442,12 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , default: token->keyword = C_RID_CODE (token->value); } } - else - token->keyword = RID_MAX; + else if (token->type == CPP_PRAGMA) + { + /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */ + token->pragma_kind = TREE_INT_CST_LOW (token->value); + token->value = NULL; + } } /* Update the globals input_location and in_system_header from TOKEN. */ @@ -477,12 +497,7 @@ cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) static inline bool cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) { - cp_token *token; - - /* Peek at the next token. */ - token = cp_lexer_peek_token (lexer); - /* Check to see if it is the indicated keyword. */ - return token->keyword == keyword; + return cp_lexer_peek_token (lexer)->keyword == keyword; } /* Return a pointer to the Nth token in the token stream. If N is 1, @@ -497,14 +512,15 @@ cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) cp_token *token; /* N is 1-based, not zero-based. */ - gcc_assert (n > 0 && lexer->next_token != &eof_token); - + gcc_assert (n > 0); + if (cp_lexer_debugging_p (lexer)) fprintf (cp_lexer_debug_stream, "cp_lexer: peeking ahead %ld at token: ", (long)n); --n; token = lexer->next_token; + gcc_assert (!n || token != &eof_token); while (n != 0) { ++token; @@ -536,6 +552,7 @@ cp_lexer_consume_token (cp_lexer* lexer) cp_token *token = lexer->next_token; gcc_assert (token != &eof_token); + gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL); do { @@ -613,25 +630,6 @@ cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok) } } -/* Consume and handle a pragma token. */ -static void -cp_lexer_handle_pragma (cp_lexer *lexer) -{ - cpp_string s; - cp_token *token = cp_lexer_consume_token (lexer); - gcc_assert (token->type == CPP_PRAGMA); - gcc_assert (token->value); - - s.len = TREE_STRING_LENGTH (token->value); - s.text = (const unsigned char *) TREE_STRING_POINTER (token->value); - - cpp_handle_deferred_pragma (parse_in, &s); - - /* Clearing token->value here means that we will get an ICE if we - try to process this #pragma again (which should be impossible). */ - token->value = NULL; -} - /* Begin saving tokens. All tokens consumed after this point will be preserved. */ @@ -714,7 +712,6 @@ cp_lexer_print_token (FILE * stream, cp_token *token) case CPP_STRING: case CPP_WSTRING: - case CPP_PRAGMA: fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value)); break; @@ -755,9 +752,6 @@ cp_token_cache_new (cp_token *first, cp_token *last) /* Decl-specifiers. */ -static void clear_decl_specs - (cp_decl_specifier_seq *); - /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */ static void @@ -816,12 +810,15 @@ make_declarator (cp_declarator_kind kind) return declarator; } -/* Make a declarator for a generalized identifier. If non-NULL, the - identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is - just UNQUALIFIED_NAME. */ +/* Make a declarator for a generalized identifier. If + QUALIFYING_SCOPE is non-NULL, the identifier is + QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just + UNQUALIFIED_NAME. SFK indicates the kind of special function this + is, if any. */ static cp_declarator * -make_id_declarator (tree qualifying_scope, tree unqualified_name) +make_id_declarator (tree qualifying_scope, tree unqualified_name, + special_function_kind sfk) { cp_declarator *declarator; @@ -838,10 +835,14 @@ make_id_declarator (tree qualifying_scope, tree unqualified_name) if (qualifying_scope && TYPE_P (qualifying_scope)) qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope); + gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE + || TREE_CODE (unqualified_name) == BIT_NOT_EXPR + || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR); + declarator = make_declarator (cdk_id); declarator->u.id.qualifying_scope = qualifying_scope; declarator->u.id.unqualified_name = unqualified_name; - declarator->u.id.sfk = sfk_none; + declarator->u.id.sfk = sfk; return declarator; } @@ -1343,9 +1344,6 @@ typedef struct cp_parser GTY(()) unsigned num_template_parameter_lists; } cp_parser; -/* The type of a function that parses some kind of expression. */ -typedef tree (*cp_parser_expression_fn) (cp_parser *); - /* Prototypes. */ /* Constructors and destructors. */ @@ -1380,7 +1378,7 @@ static bool cp_parser_translation_unit /* Expressions [gram.expr] */ static tree cp_parser_primary_expression - (cp_parser *, bool, cp_id_kind *, tree *); + (cp_parser *, bool, bool, bool, cp_id_kind *); static tree cp_parser_id_expression (cp_parser *, bool, bool, bool *, bool); static tree cp_parser_unqualified_id @@ -1439,9 +1437,9 @@ static tree cp_parser_builtin_offsetof /* Statements [gram.stmt.stmt] */ static void cp_parser_statement - (cp_parser *, tree); + (cp_parser *, tree, bool); static tree cp_parser_labeled_statement - (cp_parser *, tree); + (cp_parser *, tree, bool); static tree cp_parser_expression_statement (cp_parser *, tree); static tree cp_parser_compound_statement @@ -1546,7 +1544,7 @@ static tree cp_parser_initializer (cp_parser *, bool *, bool *); static tree cp_parser_initializer_clause (cp_parser *, bool *); -static tree cp_parser_initializer_list +static VEC(constructor_elt,gc) *cp_parser_initializer_list (cp_parser *, bool *); static bool cp_parser_ctor_initializer_opt_and_function_body @@ -1661,6 +1659,10 @@ static bool cp_parser_extension_opt static void cp_parser_label_declaration (cp_parser *); +enum pragma_context { pragma_external, pragma_stmt, pragma_compound }; +static bool cp_parser_pragma + (cp_parser *, enum pragma_context); + /* Objective-C++ Productions */ static tree cp_parser_objc_message_receiver @@ -1693,7 +1695,7 @@ static tree cp_parser_objc_statement /* Utility Routines */ static tree cp_parser_lookup_name - (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *); + (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *); static tree cp_parser_lookup_name_simple (cp_parser *, tree); static tree cp_parser_maybe_treat_template_as_class @@ -1804,6 +1806,8 @@ static void cp_parser_skip_to_closing_brace (cp_parser *); static void cp_parser_skip_until_found (cp_parser *, enum cpp_ttype, const char *); +static void cp_parser_skip_to_pragma_eol + (cp_parser*, cp_token *); static bool cp_parser_error_occurred (cp_parser *); static bool cp_parser_allow_gnu_extensions_p @@ -1864,12 +1868,14 @@ cp_parser_error (cp_parser* parser, const char* message) /* This diagnostic makes more sense if it is tagged to the line of the token we just peeked at. */ cp_lexer_set_source_position_from_token (token); + if (token->type == CPP_PRAGMA) { error ("%<#pragma%> is not allowed here"); - cp_lexer_purge_token (parser->lexer); + cp_parser_skip_to_pragma_eol (parser, token); return; } + c_parse_error (message, /* Because c_parser_error does not understand CPP_KEYWORD, keywords are treated like @@ -2042,11 +2048,11 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id) decl = cp_parser_lookup_name_simple (parser, id); parser->scope = old_scope; /* If the lookup found a template-name, it means that the user forgot - to specify an argument list. Emit an useful error message. */ + to specify an argument list. Emit a useful error message. */ if (TREE_CODE (decl) == TEMPLATE_DECL) error ("invalid use of template-name %qE without an argument list", decl); - else if (!parser->scope || parser->scope == error_mark_node) + else if (!parser->scope) { /* Issue an error message. */ error ("%qE does not name a type", id); @@ -2093,7 +2099,7 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id) } /* Here we diagnose qualified-ids where the scope is actually correct, but the identifier does not resolve to a valid type name. */ - else + else if (parser->scope != error_mark_node) { if (TREE_CODE (parser->scope) == NAMESPACE_DECL) error ("%qE in namespace %qE does not name a type", @@ -2163,7 +2169,6 @@ cp_parser_skip_to_closing_parenthesis (cp_parser *parser, { unsigned paren_depth = 0; unsigned brace_depth = 0; - int result; if (recovering && !or_comma && cp_parser_uncommitted_to_tentative_parse_p (parser)) @@ -2171,62 +2176,55 @@ cp_parser_skip_to_closing_parenthesis (cp_parser *parser, while (true) { - cp_token *token; + cp_token * token = cp_lexer_peek_token (parser->lexer); - /* If we've run out of tokens, then there is no closing `)'. */ - if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) + switch (token->type) { - result = 0; - break; - } + case CPP_EOF: + case CPP_PRAGMA_EOL: + /* If we've run out of tokens, then there is no closing `)'. */ + return 0; - token = cp_lexer_peek_token (parser->lexer); + case CPP_SEMICOLON: + /* This matches the processing in skip_to_end_of_statement. */ + if (!brace_depth) + return 0; + break; - /* This matches the processing in skip_to_end_of_statement. */ - if (token->type == CPP_SEMICOLON && !brace_depth) - { - result = 0; + case CPP_OPEN_BRACE: + ++brace_depth; break; - } - if (token->type == CPP_OPEN_BRACE) - ++brace_depth; - if (token->type == CPP_CLOSE_BRACE) - { + case CPP_CLOSE_BRACE: if (!brace_depth--) - { - result = 0; - break; - } - } - if (recovering && or_comma && token->type == CPP_COMMA - && !brace_depth && !paren_depth) - { - result = -1; + return 0; break; - } - if (!brace_depth) - { - /* If it is an `(', we have entered another level of nesting. */ - if (token->type == CPP_OPEN_PAREN) + case CPP_COMMA: + if (recovering && or_comma && !brace_depth && !paren_depth) + return -1; + break; + + case CPP_OPEN_PAREN: + if (!brace_depth) ++paren_depth; - /* If it is a `)', then we might be done. */ - else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) + break; + + case CPP_CLOSE_PAREN: + if (!brace_depth && !paren_depth--) { if (consume_paren) cp_lexer_consume_token (parser->lexer); - { - result = 1; - break; - } + return 1; } + break; + + default: + break; } /* Consume the token. */ cp_lexer_consume_token (parser->lexer); } - - return result; } /* Consume tokens until we reach the end of the current statement. @@ -2240,31 +2238,34 @@ cp_parser_skip_to_end_of_statement (cp_parser* parser) while (true) { - cp_token *token; + cp_token *token = cp_lexer_peek_token (parser->lexer); - /* Peek at the next token. */ - token = cp_lexer_peek_token (parser->lexer); - /* If we've run out of tokens, stop. */ - if (token->type == CPP_EOF) - break; - /* If the next token is a `;', we have reached the end of the - statement. */ - if (token->type == CPP_SEMICOLON && !nesting_depth) - break; - /* If the next token is a non-nested `}', then we have reached - the end of the current block. */ - if (token->type == CPP_CLOSE_BRACE) + switch (token->type) { - /* If this is a non-nested `}', stop before consuming it. + case CPP_EOF: + case CPP_PRAGMA_EOL: + /* If we've run out of tokens, stop. */ + return; + + case CPP_SEMICOLON: + /* If the next token is a `;', we have reached the end of the + statement. */ + if (!nesting_depth) + return; + break; + + case CPP_CLOSE_BRACE: + /* If this is a non-nested '}', stop before consuming it. That way, when confronted with something like: { 3 + } - we stop before consuming the closing `}', even though we + we stop before consuming the closing '}', even though we have not yet reached a `;'. */ if (nesting_depth == 0) - break; - /* If it is the closing `}' for a block that we have + return; + + /* If it is the closing '}' for a block that we have scanned, stop -- but only after consuming the token. That way given: @@ -2277,13 +2278,17 @@ cp_parser_skip_to_end_of_statement (cp_parser* parser) if (--nesting_depth == 0) { cp_lexer_consume_token (parser->lexer); - break; + return; } + + case CPP_OPEN_BRACE: + ++nesting_depth; + break; + + default: + break; } - /* If it the next token is a `{', then we are entering a new - block. Consume the entire block. */ - else if (token->type == CPP_OPEN_BRACE) - ++nesting_depth; + /* Consume the token. */ cp_lexer_consume_token (parser->lexer); } @@ -2320,15 +2325,12 @@ cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) { cp_token *token = cp_lexer_peek_token (parser->lexer); - if (token->type == CPP_EOF) - break; - switch (token->type) { case CPP_EOF: + case CPP_PRAGMA_EOL: /* If we've run out of tokens, stop. */ - nesting_depth = -1; - continue; + return; case CPP_SEMICOLON: /* Stop if this is an unnested ';'. */ @@ -2355,7 +2357,6 @@ cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) /* Consume the token. */ cp_lexer_consume_token (parser->lexer); - } } @@ -2369,26 +2370,56 @@ cp_parser_skip_to_closing_brace (cp_parser *parser) while (true) { - cp_token *token; + cp_token *token = cp_lexer_peek_token (parser->lexer); + + switch (token->type) + { + case CPP_EOF: + case CPP_PRAGMA_EOL: + /* If we've run out of tokens, stop. */ + return; + + case CPP_CLOSE_BRACE: + /* If the next token is a non-nested `}', then we have reached + the end of the current block. */ + if (nesting_depth-- == 0) + return; + break; + + case CPP_OPEN_BRACE: + /* If it the next token is a `{', then we are entering a new + block. Consume the entire block. */ + ++nesting_depth; + break; + + default: + break; + } - /* Peek at the next token. */ - token = cp_lexer_peek_token (parser->lexer); - /* If we've run out of tokens, stop. */ - if (token->type == CPP_EOF) - break; - /* If the next token is a non-nested `}', then we have reached - the end of the current block. */ - if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) - break; - /* If it the next token is a `{', then we are entering a new - block. Consume the entire block. */ - else if (token->type == CPP_OPEN_BRACE) - ++nesting_depth; /* Consume the token. */ cp_lexer_consume_token (parser->lexer); } } +/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK + parameter is the PRAGMA token, allowing us to purge the entire pragma + sequence. */ + +static void +cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok) +{ + cp_token *token; + + parser->lexer->in_pragma = false; + + do + token = cp_lexer_consume_token (parser->lexer); + while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF); + + /* Ensure that the pragma is not parsed again. */ + cp_lexer_purge_tokens_after (parser->lexer, pragma_tok); +} + /* This is a simple wrapper around make_typename_type. When the id is an unresolved identifier node, we can provide a superior diagnostic using cp_parser_diagnose_invalid_type_name. */ @@ -2400,7 +2431,7 @@ cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id) if (TREE_CODE (id) == IDENTIFIER_NODE) { result = make_typename_type (scope, id, typename_type, - /*complain=*/0); + /*complain=*/tf_none); if (result == error_mark_node) cp_parser_diagnose_invalid_type_name (parser, scope, id); return result; @@ -2655,39 +2686,34 @@ cp_parser_translation_unit (cp_parser* parser) declarator_obstack_base = obstack_next_free (&declarator_obstack); } - while (true) + cp_parser_declaration_seq_opt (parser); + + /* If there are no tokens left then all went well. */ + if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) + { + /* Get rid of the token array; we don't need it any more. */ + cp_lexer_destroy (parser->lexer); + parser->lexer = NULL; + + /* This file might have been a context that's implicitly extern + "C". If so, pop the lang context. (Only relevant for PCH.) */ + if (parser->implicit_extern_c) + { + pop_lang_context (); + parser->implicit_extern_c = false; + } + + /* Finish up. */ + finish_translation_unit (); + + success = true; + } + else { - cp_parser_declaration_seq_opt (parser); - - /* If there are no tokens left then all went well. */ - if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) - { - /* Get rid of the token array; we don't need it any more. */ - cp_lexer_destroy (parser->lexer); - parser->lexer = NULL; - - /* This file might have been a context that's implicitly extern - "C". If so, pop the lang context. (Only relevant for PCH.) */ - if (parser->implicit_extern_c) - { - pop_lang_context (); - parser->implicit_extern_c = false; - } - - /* Finish up. */ - finish_translation_unit (); - - success = true; - break; - } - else - { - cp_parser_error (parser, "expected declaration"); - success = false; - break; - } + cp_parser_error (parser, "expected declaration"); + success = false; } - + /* Make sure the declarator obstack was fully cleaned up. */ gcc_assert (obstack_next_free (&declarator_obstack) == declarator_obstack_base); @@ -2711,6 +2737,7 @@ cp_parser_translation_unit (cp_parser* parser) primary-expression: ( compound-statement ) __builtin_va_arg ( assignment-expression , type-id ) + __builtin_offsetof ( type-id , offsetof-expression ) Objective-C++ Extension: @@ -2720,29 +2747,25 @@ cp_parser_translation_unit (cp_parser* parser) literal: __null - CAST_P is true if this primary expression is the target of a cast. + ADDRESS_P is true iff this expression was immediately preceded by + "&" and therefore might denote a pointer-to-member. CAST_P is true + iff this expression is the target of a cast. TEMPLATE_ARG_P is + true iff this expression is a template argument. - Returns a representation of the expression. - - *IDK indicates what kind of id-expression (if any) was present. - - *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be - used as the operand of a pointer-to-member. In that case, - *QUALIFYING_CLASS gives the class that is used as the qualifying - class in the pointer-to-member. */ + Returns a representation of the expression. Upon return, *IDK + indicates what kind of id-expression (if any) was present. */ static tree cp_parser_primary_expression (cp_parser *parser, + bool address_p, bool cast_p, - cp_id_kind *idk, - tree *qualifying_class) + bool template_arg_p, + cp_id_kind *idk) { cp_token *token; /* Assume the primary expression is not an id-expression. */ *idk = CP_ID_KIND_NONE; - /* And that it cannot be used as pointer-to-member. */ - *qualifying_class = NULL_TREE; /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); @@ -2783,7 +2806,10 @@ cp_parser_primary_expression (cp_parser *parser, /* The end of the cast-expression. */ && next_token->type != CPP_CLOSE_PAREN /* The end of an array bound. */ - && next_token->type != CPP_CLOSE_SQUARE) + && next_token->type != CPP_CLOSE_SQUARE + /* The closing ">" in a template-argument-list. */ + && (next_token->type != CPP_GREATER + || parser->greater_than_is_operator_p)) cast_p = false; } @@ -2965,6 +2991,8 @@ cp_parser_primary_expression (cp_parser *parser, tree id_expression; tree decl; const char *error_msg; + bool template_p; + bool done; id_expression: /* Parse the id-expression. */ @@ -2972,30 +3000,37 @@ cp_parser_primary_expression (cp_parser *parser, = cp_parser_id_expression (parser, /*template_keyword_p=*/false, /*check_dependency_p=*/true, - /*template_p=*/NULL, + &template_p, /*declarator_p=*/false); if (id_expression == error_mark_node) return error_mark_node; + token = cp_lexer_peek_token (parser->lexer); + done = (token->type != CPP_OPEN_SQUARE + && token->type != CPP_OPEN_PAREN + && token->type != CPP_DOT + && token->type != CPP_DEREF + && token->type != CPP_PLUS_PLUS + && token->type != CPP_MINUS_MINUS); /* If we have a template-id, then no further lookup is required. If the template-id was for a template-class, we will sometimes have a TYPE_DECL at this point. */ - else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR - || TREE_CODE (id_expression) == TYPE_DECL) + if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR + || TREE_CODE (id_expression) == TYPE_DECL) decl = id_expression; /* Look up the name. */ else { - bool ambiguous_p; + tree ambiguous_decls; decl = cp_parser_lookup_name (parser, id_expression, none_type, - /*is_template=*/false, + template_p, /*is_namespace=*/false, /*check_dependency=*/true, - &ambiguous_p); + &ambiguous_decls); /* If the lookup was ambiguous, an error will already have been issued. */ - if (ambiguous_p) + if (ambiguous_decls) return error_mark_node; /* In Objective-C++, an instance variable (ivar) may be preferred @@ -3003,14 +3038,9 @@ cp_parser_primary_expression (cp_parser *parser, decl = objc_lookup_ivar (decl, id_expression); /* If name lookup gives us a SCOPE_REF, then the - qualifying scope was dependent. Just propagate the - name. */ + qualifying scope was dependent. */ if (TREE_CODE (decl) == SCOPE_REF) - { - if (TYPE_P (TREE_OPERAND (decl, 0))) - *qualifying_class = TREE_OPERAND (decl, 0); - return decl; - } + return decl; /* Check to see if DECL is a local variable in a context where that is forbidden. */ if (parser->local_variables_forbidden_p @@ -3039,12 +3069,15 @@ cp_parser_primary_expression (cp_parser *parser, } } - decl = finish_id_expression (id_expression, decl, parser->scope, - idk, qualifying_class, - parser->integral_constant_expression_p, - parser->allow_non_integral_constant_expression_p, - &parser->non_integral_constant_expression_p, - &error_msg); + decl = (finish_id_expression + (id_expression, decl, parser->scope, + idk, + parser->integral_constant_expression_p, + parser->allow_non_integral_constant_expression_p, + &parser->non_integral_constant_expression_p, + template_p, done, address_p, + template_arg_p, + &error_msg)); if (error_msg) cp_parser_error (parser, error_msg); return decl; @@ -3108,7 +3141,7 @@ cp_parser_id_expression (cp_parser *parser, /* Assume the `template' keyword was not used. */ if (template_p) - *template_p = false; + *template_p = template_keyword_p; /* Look for the optional `::' operator. */ global_scope_p @@ -3401,6 +3434,15 @@ cp_parser_unqualified_id (cp_parser* parser, else if (type_decl == error_mark_node) return error_mark_node; + /* Check that destructor name and scope match. */ + if (declarator_p && scope && !check_dtor_name (scope, type_decl)) + { + if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) + error ("declaration of %<~%T%> as member of %qT", + type_decl, scope); + return error_mark_node; + } + /* [class.dtor] A typedef-name that names a class shall not be used as the @@ -3475,7 +3517,6 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, bool is_declaration) { bool success = false; - tree access_check = NULL_TREE; cp_token_position start = 0; cp_token *token; @@ -3495,9 +3536,10 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, /* Remember where the nested-name-specifier starts. */ if (cp_parser_uncommitted_to_tentative_parse_p (parser)) - start = cp_lexer_token_position (parser->lexer, false); - - push_deferring_access_checks (dk_deferred); + { + start = cp_lexer_token_position (parser->lexer, false); + push_deferring_access_checks (dk_deferred); + } while (true) { @@ -3603,16 +3645,32 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, token = cp_lexer_consume_token (parser->lexer); if (!error_p) { - tree decl; - - decl = cp_parser_lookup_name_simple (parser, token->value); - if (TREE_CODE (decl) == TEMPLATE_DECL) - error ("%qD used without template parameters", decl); - else - cp_parser_name_lookup_error - (parser, token->value, decl, - "is not a class or namespace"); - parser->scope = NULL_TREE; + if (!token->ambiguous_p) + { + tree decl; + tree ambiguous_decls; + + decl = cp_parser_lookup_name (parser, token->value, + none_type, + /*is_template=*/false, + /*is_namespace=*/false, + /*check_dependency=*/true, + &ambiguous_decls); + if (TREE_CODE (decl) == TEMPLATE_DECL) + error ("%qD used without template parameters", decl); + else if (ambiguous_decls) + { + error ("reference to %qD is ambiguous", + token->value); + print_candidates (ambiguous_decls); + decl = error_mark_node; + } + else + cp_parser_name_lookup_error + (parser, token->value, decl, + "is not a class or namespace"); + } + parser->scope = error_mark_node; error_p = true; /* Treat this as a successful nested-name-specifier due to: @@ -3628,29 +3686,38 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, } break; } - /* We've found one valid nested-name-specifier. */ success = true; - /* Make sure we look in the right scope the next time through - the loop. */ - parser->scope = (TREE_CODE (new_scope) == TYPE_DECL - ? TREE_TYPE (new_scope) - : new_scope); + /* Name lookup always gives us a DECL. */ + if (TREE_CODE (new_scope) == TYPE_DECL) + new_scope = TREE_TYPE (new_scope); + /* Uses of "template" must be followed by actual templates. */ + if (template_keyword_p + && !(CLASS_TYPE_P (new_scope) + && ((CLASSTYPE_USE_TEMPLATE (new_scope) + && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope))) + || CLASSTYPE_IS_TEMPLATE (new_scope))) + && !(TREE_CODE (new_scope) == TYPENAME_TYPE + && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope)) + == TEMPLATE_ID_EXPR))) + pedwarn (TYPE_P (new_scope) + ? "%qT is not a template" + : "%qD is not a template", + new_scope); /* If it is a class scope, try to complete it; we are about to be looking up names inside the class. */ - if (TYPE_P (parser->scope) + if (TYPE_P (new_scope) /* Since checking types for dependency can be expensive, avoid doing it if the type is already complete. */ - && !COMPLETE_TYPE_P (parser->scope) + && !COMPLETE_TYPE_P (new_scope) /* Do not try to complete dependent types. */ - && !dependent_type_p (parser->scope)) - complete_type (parser->scope); + && !dependent_type_p (new_scope)) + new_scope = complete_type (new_scope); + /* Make sure we look in the right scope the next time through + the loop. */ + parser->scope = new_scope; } - /* Retrieve any deferred checks. Do not pop this access checks yet - so the memory will not be reclaimed during token replacing below. */ - access_check = get_deferred_access_checks (); - /* If parsing tentatively, replace the sequence of tokens that makes up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER token. That way, should we re-parse the token stream, we will @@ -3658,19 +3725,27 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, we issue duplicate error messages. */ if (success && start) { - cp_token *token = cp_lexer_token_at (parser->lexer, start); + cp_token *token; + tree access_checks; + token = cp_lexer_token_at (parser->lexer, start); /* Reset the contents of the START token. */ token->type = CPP_NESTED_NAME_SPECIFIER; - token->value = build_tree_list (access_check, parser->scope); + /* Retrieve any deferred checks. Do not pop this access checks yet + so the memory will not be reclaimed during token replacing below. */ + access_checks = get_deferred_access_checks (); + token->value = build_tree_list (copy_list (access_checks), + parser->scope); TREE_TYPE (token->value) = parser->qualifying_scope; token->keyword = RID_MAX; /* Purge all subsequent tokens. */ cp_lexer_purge_tokens_after (parser->lexer, start); } + + if (start) + pop_to_parent_deferring_access_checks (); - pop_deferring_access_checks (); return success ? parser->scope : NULL_TREE; } @@ -3819,10 +3894,6 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) enum rid keyword; cp_id_kind idk = CP_ID_KIND_NONE; tree postfix_expression = NULL_TREE; - /* Non-NULL only if the current postfix-expression can be used to - form a pointer-to-member. In that case, QUALIFYING_CLASS is the - class used to qualify the member. */ - tree qualifying_class = NULL_TREE; /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); @@ -3950,53 +4021,12 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) case RID_TYPENAME: { - bool template_p = false; - tree id; tree type; - tree scope; - - /* Consume the `typename' token. */ - cp_lexer_consume_token (parser->lexer); - /* Look for the optional `::' operator. */ - cp_parser_global_scope_opt (parser, - /*current_scope_valid_p=*/false); - /* Look for the nested-name-specifier. In case of error here, - consume the trailing id to avoid subsequent error messages - for usual cases. */ - scope = cp_parser_nested_name_specifier (parser, - /*typename_keyword_p=*/true, - /*check_dependency_p=*/true, - /*type_p=*/true, - /*is_declaration=*/true); - - /* Look for the optional `template' keyword. */ - template_p = cp_parser_optional_template_keyword (parser); - /* We don't know whether we're looking at a template-id or an - identifier. */ - cp_parser_parse_tentatively (parser); - /* Try a template-id. */ - id = cp_parser_template_id (parser, template_p, - /*check_dependency_p=*/true, - /*is_declaration=*/true); - /* If that didn't work, try an identifier. */ - if (!cp_parser_parse_definitely (parser)) - id = cp_parser_identifier (parser); - - /* Don't process id if nested name specifier is invalid. */ - if (!scope || scope == error_mark_node) - return error_mark_node; - /* If we look up a template-id in a non-dependent qualifying - scope, there's no need to create a dependent type. */ - else if (TREE_CODE (id) == TYPE_DECL - && !dependent_type_p (parser->scope)) - type = TREE_TYPE (id); - /* Create a TYPENAME_TYPE to represent the type to which the - functional cast is being performed. */ - else - type = make_typename_type (parser->scope, id, - typename_type, - /*complain=*/1); - + /* The syntax permitted here is the same permitted for an + elaborated-type-specifier. */ + type = cp_parser_elaborated_type_specifier (parser, + /*is_friend=*/false, + /*is_declaration=*/false); postfix_expression = cp_parser_functional_cast (parser, type); } break; @@ -4027,7 +4057,7 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) if (cp_parser_allow_gnu_extensions_p (parser) && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) { - tree initializer_list = NULL_TREE; + VEC(constructor_elt,gc) *initializer_list = NULL; bool saved_in_type_id_in_expr_p; cp_parser_parse_tentatively (parser); @@ -4072,38 +4102,14 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) } /* It must be a primary-expression. */ - postfix_expression = cp_parser_primary_expression (parser, - cast_p, - &idk, - &qualifying_class); + postfix_expression + = cp_parser_primary_expression (parser, address_p, cast_p, + /*template_arg_p=*/false, + &idk); } break; } - /* If we were avoiding committing to the processing of a - qualified-id until we knew whether or not we had a - pointer-to-member, we now know. */ - if (qualifying_class) - { - bool done; - - /* Peek at the next token. */ - token = cp_lexer_peek_token (parser->lexer); - done = (token->type != CPP_OPEN_SQUARE - && token->type != CPP_OPEN_PAREN - && token->type != CPP_DOT - && token->type != CPP_DEREF - && token->type != CPP_PLUS_PLUS - && token->type != CPP_MINUS_MINUS); - - postfix_expression = finish_qualified_id_expr (qualifying_class, - postfix_expression, - done, - address_p); - if (done) - return postfix_expression; - } - /* Keep looping until the postfix-expression is complete. */ while (true) { @@ -4391,7 +4397,6 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, { tree name; bool dependent_p; - bool template_p; bool pseudo_destructor_p; tree scope = NULL_TREE; @@ -4418,7 +4423,13 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, underlying type here. */ scope = non_reference (scope); /* The type of the POSTFIX_EXPRESSION must be complete. */ - scope = complete_type_or_else (scope, NULL_TREE); + if (scope == unknown_type_node) + { + error ("%qE does not have class type", postfix_expression); + scope = NULL_TREE; + } + else + scope = complete_type_or_else (scope, NULL_TREE); /* Let the name lookup machinery know that we are processing a class member access expression. */ parser->context->object_type = scope; @@ -4462,12 +4473,14 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, /* If the SCOPE is not a scalar type, we are looking at an ordinary class member access expression, rather than a pseudo-destructor-name. */ - template_p = cp_parser_optional_template_keyword (parser); + bool template_p; /* Parse the id-expression. */ - name = cp_parser_id_expression (parser, template_p, - /*check_dependency_p=*/true, - /*template_p=*/NULL, - /*declarator_p=*/false); + name = (cp_parser_id_expression + (parser, + cp_parser_optional_template_keyword (parser), + /*check_dependency_p=*/true, + &template_p, + /*declarator_p=*/false)); /* In general, build a SCOPE_REF if the member name is qualified. However, if the name was not dependent and has already been resolved; there is no need to build the SCOPE_REF. For example; @@ -4494,7 +4507,10 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, { if (name != error_mark_node && !BASELINK_P (name) && parser->scope) { - name = build_nt (SCOPE_REF, parser->scope, name); + name = build_qualified_name (/*type=*/NULL_TREE, + parser->scope, + name, + template_p); parser->scope = NULL_TREE; parser->qualifying_scope = NULL_TREE; parser->object_scope = NULL_TREE; @@ -4503,7 +4519,8 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, adjust_result_of_qualified_name_lookup (name, BINFO_TYPE (BASELINK_BINFO (name)), scope); postfix_expression - = finish_class_member_access_expr (postfix_expression, name); + = finish_class_member_access_expr (postfix_expression, name, + template_p); } } @@ -6009,15 +6026,20 @@ cp_parser_builtin_offsetof (cp_parser *parser) iteration-statement jump-statement declaration-statement - try-block */ + try-block + + IN_COMPOUND is true when the statement is nested inside a + cp_parser_compound_statement; this matters for certain pragmas. */ static void -cp_parser_statement (cp_parser* parser, tree in_statement_expr) +cp_parser_statement (cp_parser* parser, tree in_statement_expr, + bool in_compound) { tree statement; cp_token *token; location_t statement_location; + restart: /* There is no statement yet. */ statement = NULL_TREE; /* Peek at the next token. */ @@ -6034,8 +6056,8 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr) { case RID_CASE: case RID_DEFAULT: - statement = cp_parser_labeled_statement (parser, - in_statement_expr); + statement = cp_parser_labeled_statement (parser, in_statement_expr, + in_compound); break; case RID_IF: @@ -6081,7 +6103,8 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr) labeled-statement. */ token = cp_lexer_peek_nth_token (parser->lexer, 2); if (token->type == CPP_COLON) - statement = cp_parser_labeled_statement (parser, in_statement_expr); + statement = cp_parser_labeled_statement (parser, in_statement_expr, + in_compound); } /* Anything that starts with a `{' must be a compound-statement. */ else if (token->type == CPP_OPEN_BRACE) @@ -6090,7 +6113,20 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr) a statement all its own. */ else if (token->type == CPP_PRAGMA) { - cp_lexer_handle_pragma (parser->lexer); + /* Only certain OpenMP pragmas are attached to statements, and thus + are considered statements themselves. All others are not. In + the context of a compound, accept the pragma as a "statement" and + return so that we can check for a close brace. Otherwise we + require a real statement and must go back and read one. */ + if (in_compound) + cp_parser_pragma (parser, pragma_compound); + else if (!cp_parser_pragma (parser, pragma_stmt)) + goto restart; + return; + } + else if (token->type == CPP_EOF) + { + cp_parser_error (parser, "expected statement"); return; } @@ -6131,10 +6167,14 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr) case constant-expression ... constant-expression : statement Returns the new CASE_LABEL_EXPR, for a `case' or `default' label. - For an ordinary label, returns a LABEL_EXPR. */ + For an ordinary label, returns a LABEL_EXPR. + + IN_COMPOUND is as for cp_parser_statement: true when we're nested + inside a compound. */ static tree -cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr) +cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr, + bool in_compound) { cp_token *token; tree statement = error_mark_node; @@ -6177,20 +6217,21 @@ cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr) else expr_hi = NULL_TREE; - if (!parser->in_switch_statement_p) - error ("case label %qE not within a switch statement", expr); - else + if (parser->in_switch_statement_p) statement = finish_case_label (expr, expr_hi); + else + error ("case label %qE not within a switch statement", expr); } break; case RID_DEFAULT: /* Consume the `default' token. */ cp_lexer_consume_token (parser->lexer); - if (!parser->in_switch_statement_p) - error ("case label not within a switch statement"); - else + + if (parser->in_switch_statement_p) statement = finish_case_label (NULL_TREE, NULL_TREE); + else + error ("case label not within a switch statement"); break; default: @@ -6202,7 +6243,7 @@ cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr) /* Require the `:' token. */ cp_parser_require (parser, CPP_COLON, "`:'"); /* Parse the labeled statement. */ - cp_parser_statement (parser, in_statement_expr); + cp_parser_statement (parser, in_statement_expr, in_compound); /* Return the label, in the case of a `case' or `default' label. */ return statement; @@ -6284,13 +6325,16 @@ cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) /* Scan statements until there aren't any more. */ while (true) { + cp_token *token = cp_lexer_peek_token (parser->lexer); + /* If we're looking at a `}', then we've run out of statements. */ - if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) - || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) + if (token->type == CPP_CLOSE_BRACE + || token->type == CPP_EOF + || token->type == CPP_PRAGMA_EOL) break; /* Parse the statement. */ - cp_parser_statement (parser, in_statement_expr); + cp_parser_statement (parser, in_statement_expr, true); } } @@ -6781,19 +6825,25 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser) { tree statement; + /* Mark if () ; with a special NOP_EXPR. */ + if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) + { + cp_lexer_consume_token (parser->lexer); + statement = add_stmt (build_empty_stmt ()); + } + /* if a compound is opened, we simply parse the statement directly. */ + else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) + statement = cp_parser_compound_statement (parser, NULL, false); /* If the token is not a `{', then we must take special action. */ - if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) + else { /* Create a compound-statement. */ statement = begin_compound_stmt (0); /* Parse the dependent-statement. */ - cp_parser_statement (parser, false); + cp_parser_statement (parser, NULL_TREE, false); /* Finish the dummy compound-statement. */ finish_compound_stmt (statement); } - /* Otherwise, we simply parse the statement directly. */ - else - statement = cp_parser_compound_statement (parser, NULL, false); /* Return the statement. */ return statement; @@ -6809,13 +6859,13 @@ cp_parser_already_scoped_statement (cp_parser* parser) { /* If the token is a `{', then we must take special action. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) - cp_parser_statement (parser, false); + cp_parser_statement (parser, NULL_TREE, false); else { /* Avoid calling cp_parser_compound_statement, so that we don't create a new scope. Do everything else by hand. */ cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); - cp_parser_statement_seq_opt (parser, false); + cp_parser_statement_seq_opt (parser, NULL_TREE); cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); } } @@ -6838,7 +6888,8 @@ cp_parser_declaration_seq_opt (cp_parser* parser) token = cp_lexer_peek_token (parser->lexer); if (token->type == CPP_CLOSE_BRACE - || token->type == CPP_EOF) + || token->type == CPP_EOF + || token->type == CPP_PRAGMA_EOL) break; if (token->type == CPP_SEMICOLON) @@ -6870,7 +6921,7 @@ cp_parser_declaration_seq_opt (cp_parser* parser) A nested declaration cannot, so this is done here and not in cp_parser_declaration. (A #pragma at block scope is handled in cp_parser_statement.) */ - cp_lexer_handle_pragma (parser->lexer); + cp_parser_pragma (parser, pragma_external); continue; } @@ -6919,6 +6970,11 @@ cp_parser_declaration (cp_parser* parser) if (token1.type != CPP_EOF) token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); + else + { + token2.type = CPP_EOF; + token2.keyword = RID_MAX; + } /* Get the high-water mark for the DECLARATOR_OBSTACK. */ p = obstack_alloc (&declarator_obstack, 0); @@ -7146,7 +7202,16 @@ cp_parser_simple_declaration (cp_parser* parser, bool function_definition_p; tree decl; - saw_declarator = true; + if (saw_declarator) + { + /* If we are processing next declarator, coma is expected */ + token = cp_lexer_peek_token (parser->lexer); + gcc_assert (token->type == CPP_COMMA); + cp_lexer_consume_token (parser->lexer); + } + else + saw_declarator = true; + /* Parse the init-declarator. */ decl = cp_parser_init_declarator (parser, &decl_specifiers, function_definition_allowed_p, @@ -7181,7 +7246,7 @@ cp_parser_simple_declaration (cp_parser* parser, token = cp_lexer_peek_token (parser->lexer); /* If it's a `,', there are more declarators to come. */ if (token->type == CPP_COMMA) - cp_lexer_consume_token (parser->lexer); + /* will be consumed next time around */; /* If it's a `;', we are done. */ else if (token->type == CPP_SEMICOLON) break; @@ -7793,7 +7858,7 @@ cp_parser_mem_initializer_list (cp_parser* parser) /* Parse the mem-initializer. */ mem_initializer = cp_parser_mem_initializer (parser); /* Add it to the list, unless it was erroneous. */ - if (mem_initializer) + if (mem_initializer != error_mark_node) { TREE_CHAIN (mem_initializer) = mem_initializer_list; mem_initializer_list = mem_initializer; @@ -7822,7 +7887,8 @@ cp_parser_mem_initializer_list (cp_parser* parser) Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base class) or FIELD_DECL (for a non-static data member) to initialize; - the TREE_VALUE is the expression-list. */ + the TREE_VALUE is the expression-list. An empty initialization + list is represented by void_list_node. */ static tree cp_parser_mem_initializer (cp_parser* parser) @@ -7847,12 +7913,14 @@ cp_parser_mem_initializer (cp_parser* parser) = cp_parser_parenthesized_expression_list (parser, false, /*cast_p=*/false, /*non_constant_p=*/NULL); + if (expression_list == error_mark_node) + return error_mark_node; if (!expression_list) expression_list = void_type_node; in_base_initializer = 0; - return member ? build_tree_list (member, expression_list) : NULL_TREE; + return member ? build_tree_list (member, expression_list) : error_mark_node; } /* Parse a mem-initializer-id. @@ -8262,6 +8330,7 @@ cp_parser_template_parameter_list (cp_parser* parser) { tree parameter_list = NULL_TREE; + begin_template_parm_list (); while (true) { tree parameter; @@ -8284,7 +8353,7 @@ cp_parser_template_parameter_list (cp_parser* parser) cp_lexer_consume_token (parser->lexer); } - return parameter_list; + return end_template_parm_list (parameter_list); } /* Parse a template-parameter. @@ -8432,10 +8501,7 @@ cp_parser_type_parameter (cp_parser* parser) /* Look for the `<'. */ cp_parser_require (parser, CPP_LESS, "`<'"); /* Parse the template-parameter-list. */ - begin_template_parm_list (); - parameter_list - = cp_parser_template_parameter_list (parser); - parameter_list = end_template_parm_list (parameter_list); + parameter_list = cp_parser_template_parameter_list (parser); /* Look for the `>'. */ cp_parser_require (parser, CPP_GREATER, "`>'"); /* Look for the `class' keyword. */ @@ -8488,7 +8554,7 @@ cp_parser_type_parameter (cp_parser* parser) /*is_template=*/is_template, /*is_namespace=*/false, /*check_dependency=*/true, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); /* See if the default argument is valid. */ default_argument = check_template_template_default_arg (default_argument); @@ -8844,7 +8910,7 @@ cp_parser_template_name (cp_parser* parser, /*is_template=*/false, /*is_namespace=*/false, check_dependency_p, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); decl = maybe_get_template_decl_from_type_decl (decl); /* If DECL is a template, then the name was a template-name. */ @@ -8902,9 +8968,19 @@ cp_parser_template_argument_list (cp_parser* parser) tree *arg_ary = fixed_args; tree vec; bool saved_in_template_argument_list_p; + bool saved_ice_p; + bool saved_non_ice_p; saved_in_template_argument_list_p = parser->in_template_argument_list_p; parser->in_template_argument_list_p = true; + /* Even if the template-id appears in an integral + constant-expression, the contents of the argument list do + not. */ + saved_ice_p = parser->integral_constant_expression_p; + parser->integral_constant_expression_p = false; + saved_non_ice_p = parser->non_integral_constant_expression_p; + parser->non_integral_constant_expression_p = false; + /* Parse the arguments. */ do { tree argument; @@ -8921,11 +8997,11 @@ cp_parser_template_argument_list (cp_parser* parser) if (arg_ary == fixed_args) { - arg_ary = xmalloc (sizeof (tree) * alloced); + arg_ary = XNEWVEC (tree, alloced); memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); } else - arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); + arg_ary = XRESIZEVEC (tree, arg_ary, alloced); } arg_ary[n_args++] = argument; } @@ -8938,6 +9014,8 @@ cp_parser_template_argument_list (cp_parser* parser) if (arg_ary != fixed_args) free (arg_ary); + parser->non_integral_constant_expression_p = saved_non_ice_p; + parser->integral_constant_expression_p = saved_ice_p; parser->in_template_argument_list_p = saved_in_template_argument_list_p; return vec; } @@ -8967,7 +9045,6 @@ cp_parser_template_argument (cp_parser* parser) bool maybe_type_id = false; cp_token *token; cp_id_kind idk; - tree qualifying_class; /* There's really no way to know what we're looking at, so we just try each alternative in order. @@ -9034,7 +9111,7 @@ cp_parser_template_argument (cp_parser* parser) /*is_template=*/template_p, /*is_namespace=*/false, /*check_dependency=*/true, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); if (TREE_CODE (argument) != TEMPLATE_DECL && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) cp_parser_error (parser, "expected template-name"); @@ -9059,9 +9136,10 @@ cp_parser_template_argument (cp_parser* parser) { cp_parser_parse_tentatively (parser); argument = cp_parser_primary_expression (parser, + /*adress_p=*/false, /*cast_p=*/false, - &idk, - &qualifying_class); + /*template_arg_p=*/true, + &idk); if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX || !cp_parser_next_token_ends_template_argument_p (parser)) cp_parser_simulate_error (parser); @@ -9084,9 +9162,10 @@ cp_parser_template_argument (cp_parser* parser) { cp_parser_parse_tentatively (parser); argument = cp_parser_primary_expression (parser, + address_p, /*cast_p=*/false, - &idk, - &qualifying_class); + /*template_arg_p=*/true, + &idk); if (cp_parser_error_occurred (parser) || !cp_parser_next_token_ends_template_argument_p (parser)) cp_parser_abort_tentative_parse (parser); @@ -9098,11 +9177,11 @@ cp_parser_template_argument (cp_parser* parser) argument = TREE_OPERAND (argument, 0); } - if (qualifying_class) - argument = finish_qualified_id_expr (qualifying_class, - argument, - /*done=*/true, - address_p); + if (TREE_CODE (argument) == BASELINK) + /* We don't need the information about what class was used + to name the overloaded functions. */ + argument = BASELINK_FUNCTIONS (argument); + if (TREE_CODE (argument) == VAR_DECL) { /* A variable without external linkage might still be a @@ -9231,7 +9310,8 @@ cp_parser_explicit_instantiation (cp_parser* parser) template instantiation. */ pop_deferring_access_checks (); if (type) - do_type_instantiation (type, extension_specifier, /*complain=*/1); + do_type_instantiation (type, extension_specifier, + /*complain=*/tf_error); } else { @@ -9286,6 +9366,7 @@ cp_parser_explicit_instantiation (cp_parser* parser) static void cp_parser_explicit_specialization (cp_parser* parser) { + bool need_lang_pop; /* Look for the `template' keyword. */ cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); /* Look for the `<'. */ @@ -9294,9 +9375,22 @@ cp_parser_explicit_specialization (cp_parser* parser) cp_parser_require (parser, CPP_GREATER, "`>'"); /* We have processed another parameter list. */ ++parser->num_template_parameter_lists; + /* [temp] + + A template ... explicit specialization ... shall not have C + linkage. */ + if (current_lang_name == lang_name_c) + { + error ("template specialization with C linkage"); + /* Give it C++ linkage to avoid confusing other parts of the + front end. */ + push_lang_context (lang_name_cplusplus); + need_lang_pop = true; + } + else + need_lang_pop = false; /* Let the front end know that we are beginning a specialization. */ begin_specialization (); - /* If the next keyword is `template', we need to figure out whether or not we're looking a template-declaration. */ if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) @@ -9313,9 +9407,12 @@ cp_parser_explicit_specialization (cp_parser* parser) cp_parser_single_declaration (parser, /*member_p=*/false, /*friend_p=*/NULL); - /* We're done with the specialization. */ end_specialization (); + /* For the erroneous case of a template with C linkage, we pushed an + implicit C++ linkage scope; exit that scope now. */ + if (need_lang_pop) + pop_lang_context (); /* We're done with this parameter list. */ --parser->num_template_parameter_lists; } @@ -9924,7 +10021,7 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, && tag_type == typename_type) type = make_typename_type (parser->scope, decl, typename_type, - /*complain=*/1); + /*complain=*/tf_error); else type = TREE_TYPE (decl); } @@ -9955,7 +10052,7 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, /*is_template=*/false, /*is_namespace=*/false, /*check_dependency=*/true, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); /* If we are parsing friend declaration, DECL may be a TEMPLATE_DECL tree node here. However, we need to check @@ -10044,6 +10141,8 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, declaration context. */ tag_scope ts; + bool template_p; + if (is_friend) /* Friends have special name lookup rules. */ ts = ts_within_enclosing_non_class; @@ -10060,8 +10159,16 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, warning (OPT_Wattributes, "type attributes are honored only at type definition"); - type = xref_tag (tag_type, identifier, ts, - parser->num_template_parameter_lists); + template_p = + (parser->num_template_parameter_lists + && (cp_parser_next_token_starts_class_definition_p (parser) + || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))); + /* An unqualified name was used to reference this type, so + there were no qualifying templates. */ + if (!cp_parser_check_template_parameters (parser, + /*num_templates=*/0)) + return error_mark_node; + type = xref_tag (tag_type, identifier, ts, template_p); } } if (tag_type != enum_type) @@ -10246,11 +10353,13 @@ cp_parser_namespace_name (cp_parser* parser) /*is_template=*/false, /*is_namespace=*/true, /*check_dependency=*/true, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); /* If it's not a namespace, issue an error. */ if (namespace_decl == error_mark_node || TREE_CODE (namespace_decl) != NAMESPACE_DECL) { + if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) + error ("%qD is not a namespace-name", identifier); cp_parser_error (parser, "expected namespace-name"); namespace_decl = error_mark_node; } @@ -10433,7 +10542,7 @@ cp_parser_using_declaration (cp_parser* parser) /* The function we call to handle a using-declaration is different depending on what scope we are in. */ - if (identifier == error_mark_node) + if (qscope == error_mark_node || identifier == error_mark_node) ; else if (TREE_CODE (identifier) != IDENTIFIER_NODE && TREE_CODE (identifier) != BIT_NOT_EXPR) @@ -10637,7 +10746,7 @@ cp_parser_asm_definition (cp_parser* parser) } } else - assemble_asm (string); + cgraph_add_asm_node (string); } /* Declarators [gram.dcl.decl] */ @@ -10917,7 +11026,7 @@ cp_parser_init_declarator (cp_parser* parser, } decl = grokfield (declarator, decl_specifiers, initializer, /*asmspec=*/NULL_TREE, - /*attributes=*/NULL_TREE); + prefix_attributes); if (decl && TREE_CODE (decl) == FUNCTION_DECL) cp_parser_save_default_args (parser, decl); } @@ -11308,6 +11417,7 @@ cp_parser_direct_declarator (cp_parser* parser, { tree qualifying_scope; tree unqualified_name; + special_function_kind sfk; /* Parse a declarator-id */ if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) @@ -11365,9 +11475,7 @@ cp_parser_direct_declarator (cp_parser* parser, qualifying_scope = type; } - declarator = make_id_declarator (qualifying_scope, - unqualified_name); - declarator->id_loc = token->location; + sfk = sfk_none; if (unqualified_name) { tree class_type; @@ -11378,39 +11486,63 @@ cp_parser_direct_declarator (cp_parser* parser, else class_type = current_class_type; + if (TREE_CODE (unqualified_name) == TYPE_DECL) + { + tree name_type = TREE_TYPE (unqualified_name); + if (class_type && same_type_p (name_type, class_type)) + { + if (qualifying_scope + && CLASSTYPE_USE_TEMPLATE (name_type)) + { + error ("invalid use of constructor as a template"); + inform ("use %<%T::%D%> instead of %<%T::%D%> to " + "name the constructor in a qualified name", + class_type, + DECL_NAME (TYPE_TI_TEMPLATE (class_type)), + class_type, name_type); + declarator = cp_error_declarator; + break; + } + else + unqualified_name = constructor_name (class_type); + } + else + { + /* We do not attempt to print the declarator + here because we do not have enough + information about its original syntactic + form. */ + cp_parser_error (parser, "invalid declarator"); + declarator = cp_error_declarator; + break; + } + } + if (class_type) { if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) - declarator->u.id.sfk = sfk_destructor; + sfk = sfk_destructor; else if (IDENTIFIER_TYPENAME_P (unqualified_name)) - declarator->u.id.sfk = sfk_conversion; + sfk = sfk_conversion; else if (/* There's no way to declare a constructor for an anonymous type, even if the type got a name for linkage purposes. */ !TYPE_WAS_ANONYMOUS (class_type) - && (constructor_name_p (unqualified_name, - class_type) - || (TREE_CODE (unqualified_name) == TYPE_DECL - && (same_type_p - (TREE_TYPE (unqualified_name), - class_type))))) - declarator->u.id.sfk = sfk_constructor; - - if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none) - *ctor_dtor_or_conv_p = -1; - if (qualifying_scope - && TREE_CODE (unqualified_name) == TYPE_DECL - && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name))) + && constructor_name_p (unqualified_name, + class_type)) { - error ("invalid use of constructor as a template"); - inform ("use %<%T::%D%> instead of %<%T::%T%> to name " - "the constructor in a qualified name", - class_type, - DECL_NAME (TYPE_TI_TEMPLATE (class_type)), - class_type, class_type); + unqualified_name = constructor_name (class_type); + sfk = sfk_constructor; } + + if (ctor_dtor_or_conv_p && sfk != sfk_none) + *ctor_dtor_or_conv_p = -1; } } + declarator = make_id_declarator (qualifying_scope, + unqualified_name, + sfk); + declarator->id_loc = token->location; handle_declarator:; scope = get_scope_of_declarator (declarator); @@ -11620,6 +11752,7 @@ cp_parser_cv_qualifier_seq_opt (cp_parser* parser) static tree cp_parser_declarator_id (cp_parser* parser) { + tree id; /* The expression must be an id-expression. Assume that qualified names are the names of types so that: @@ -11634,11 +11767,14 @@ cp_parser_declarator_id (cp_parser* parser) int S::R::i = 3; will work, too. */ - return cp_parser_id_expression (parser, - /*template_keyword_p=*/false, - /*check_dependency_p=*/false, - /*template_p=*/NULL, - /*declarator_p=*/true); + id = cp_parser_id_expression (parser, + /*template_keyword_p=*/false, + /*check_dependency_p=*/false, + /*template_p=*/NULL, + /*declarator_p=*/true); + if (BASELINK_P (id)) + id = BASELINK_FUNCTIONS (id); + return id; } /* Parse a type-id. @@ -11757,8 +11893,6 @@ cp_parser_type_specifier_seq (cp_parser* parser, if (is_condition && !is_cv_qualifier) flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; } - - return; } /* Parse a parameter-declaration-clause. @@ -12123,6 +12257,7 @@ cp_parser_parameter_declaration (cp_parser *parser, /* If we run out of tokens, issue an error message. */ case CPP_EOF: + case CPP_PRAGMA_EOL: error ("file ends in default argument"); done = true; break; @@ -12239,7 +12374,7 @@ cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) = initializer-clause ( expression-list ) - Returns a expression representing the initializer. If no + Returns an expression representing the initializer. If no initializer is present, NULL_TREE is returned. *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' @@ -12298,7 +12433,7 @@ cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, returned is simply a representation for the expression. Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be - the elements of the initializer-list (or NULL_TREE, if the last + the elements of the initializer-list (or NULL, if the last production is used). The TREE_TYPE for the CONSTRUCTOR will be NULL_TREE. There is no way to detect whether or not the optional trailing `,' was provided. NON_CONSTANT_P is as for @@ -12358,15 +12493,15 @@ cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) identifier : initializer-clause initializer-list, identifier : initializer-clause - Returns a TREE_LIST. The TREE_VALUE of each node is an expression - for the initializer. If the TREE_PURPOSE is non-NULL, it is the + Returns a VEC of constructor_elt. The VALUE of each elt is an expression + for the initializer. If the INDEX of the elt is non-NULL, it is the IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is as for cp_parser_initializer. */ -static tree +static VEC(constructor_elt,gc) * cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) { - tree initializers = NULL_TREE; + VEC(constructor_elt,gc) *v = NULL; /* Assume all of the expressions are constant. */ *non_constant_p = false; @@ -12400,8 +12535,9 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) /* If any clause is non-constant, so is the entire initializer. */ if (clause_non_constant_p) *non_constant_p = true; - /* Add it to the list. */ - initializers = tree_cons (identifier, initializer, initializers); + + /* Add it to the vector. */ + CONSTRUCTOR_APPEND_ELT(v, identifier, initializer); /* If the next token is not a comma, we have reached the end of the list. */ @@ -12420,9 +12556,7 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) cp_lexer_consume_token (parser->lexer); } - /* The initializers were built up in reverse order, so we need to - reverse them now. */ - return nreverse (initializers); + return v; } /* Classes [gram.class] */ @@ -12481,9 +12615,13 @@ cp_parser_class_name (cp_parser *parser, if (token->type == CPP_NAME && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) { + cp_token *identifier_token; tree identifier; + bool ambiguous_p; /* Look for the identifier. */ + identifier_token = cp_lexer_peek_token (parser->lexer); + ambiguous_p = identifier_token->ambiguous_p; identifier = cp_parser_identifier (parser); /* If the next token isn't an identifier, we are certainly not looking at a class-name. */ @@ -12495,6 +12633,15 @@ cp_parser_class_name (cp_parser *parser, decl = identifier; else { + tree ambiguous_decls; + /* If we already know that this lookup is ambiguous, then + we've already issued an error message; there's no reason + to check again. */ + if (ambiguous_p) + { + cp_parser_simulate_error (parser); + return error_mark_node; + } /* If the next token is a `::', then the name must be a type name. @@ -12511,7 +12658,18 @@ cp_parser_class_name (cp_parser *parser, /*is_template=*/false, /*is_namespace=*/false, check_dependency_p, - /*ambiguous_p=*/NULL); + &ambiguous_decls); + if (ambiguous_decls) + { + error ("reference to %qD is ambiguous", identifier); + print_candidates (ambiguous_decls); + if (cp_parser_parsing_tentatively (parser)) + { + identifier_token->ambiguous_p = true; + cp_parser_simulate_error (parser); + } + return error_mark_node; + } } } else @@ -12529,7 +12687,8 @@ cp_parser_class_name (cp_parser *parser, /* If this is a typename, create a TYPENAME_TYPE. */ if (typename_p && decl != error_mark_node) { - decl = make_typename_type (scope, decl, typename_type, /*complain=*/1); + decl = make_typename_type (scope, decl, typename_type, + /*complain=*/tf_error); if (decl != error_mark_node) decl = TYPE_NAME (decl); } @@ -12669,7 +12828,7 @@ cp_parser_class_specifier (cp_parser* parser) tree fn; tree class_type = NULL_TREE; tree pushed_scope = NULL_TREE; - + /* In a first pass, parse default arguments to the functions. Then, in a second pass, parse the bodies of the functions. This two-phased approach handles cases like: @@ -12714,13 +12873,8 @@ cp_parser_class_specifier (cp_parser* parser) { /* Figure out which function we need to process. */ fn = TREE_VALUE (queue_entry); - - /* A hack to prevent garbage collection. */ - function_depth++; - /* Parse the function. */ cp_parser_late_parsing_for_member (parser, fn); - function_depth--; } } @@ -13058,7 +13212,7 @@ cp_parser_class_head (cp_parser* parser, if (type != error_mark_node && COMPLETE_TYPE_P (type)) { error ("redefinition of %q#T", type); - cp_error_at ("previous definition of %q#T", type); + error ("previous definition of %q+#T", type); type = NULL_TREE; goto done; } @@ -13139,7 +13293,9 @@ cp_parser_member_specification_opt (cp_parser* parser) /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); /* If it's a `}', or EOF then we've seen all the members. */ - if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) + if (token->type == CPP_CLOSE_BRACE + || token->type == CPP_EOF + || token->type == CPP_PRAGMA_EOL) break; /* See if this token is a keyword. */ @@ -13161,7 +13317,7 @@ cp_parser_member_specification_opt (cp_parser* parser) /* Accept #pragmas at class scope. */ if (token->type == CPP_PRAGMA) { - cp_lexer_handle_pragma (parser->lexer); + cp_parser_pragma (parser, pragma_external); break; } @@ -13225,8 +13381,13 @@ cp_parser_member_declaration (cp_parser* parser) /* Check for a template-declaration. */ if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) { - /* Parse the template-declaration. */ - cp_parser_template_declaration (parser, /*member_p=*/true); + /* An explicit specialization here is an error condition, and we + expect the specialization handler to detect and report this. */ + if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS + && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) + cp_parser_explicit_specialization (parser); + else + cp_parser_template_declaration (parser, /*member_p=*/true); return; } @@ -13394,7 +13555,8 @@ cp_parser_member_declaration (cp_parser* parser) /* Create the bitfield declaration. */ decl = grokbitfield (identifier ? make_id_declarator (NULL_TREE, - identifier) + identifier, + sfk_none) : NULL, &decl_specifiers, width); @@ -13578,18 +13740,13 @@ cp_parser_pure_specifier (cp_parser* parser) return error_mark_node; /* Look for the `0' token. */ token = cp_lexer_consume_token (parser->lexer); - if (token->type != CPP_NUMBER || !integer_zerop (token->value)) - { - cp_parser_error (parser, - "invalid pure specifier (only `= 0' is allowed)"); - cp_parser_skip_to_end_of_statement (parser); - return error_mark_node; - } + /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */ + if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO)) + return integer_zero_node; - /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well. - We need to get information from the lexer about how the number - was spelled in order to fix this problem. */ - return integer_zero_node; + cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)"); + cp_parser_skip_to_end_of_statement (parser); + return error_mark_node; } /* Parse a constant-initializer. @@ -14450,22 +14607,28 @@ cp_parser_label_declaration (cp_parser* parser) If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent types. - If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup - results in an ambiguity, and false otherwise. */ + If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a + TREE_LIST of candidates if name-lookup results in an ambiguity, and + NULL_TREE otherwise. */ static tree cp_parser_lookup_name (cp_parser *parser, tree name, enum tag_types tag_type, - bool is_template, bool is_namespace, + bool is_template, + bool is_namespace, bool check_dependency, - bool *ambiguous_p) + tree *ambiguous_decls) { + int flags = 0; tree decl; tree object_type = parser->context->object_type; + if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) + flags |= LOOKUP_COMPLAIN; + /* Assume that the lookup will be unambiguous. */ - if (ambiguous_p) - *ambiguous_p = false; + if (ambiguous_decls) + *ambiguous_decls = NULL_TREE; /* Now that we have looked up the name, the OBJECT_TYPE (if any) is no longer valid. Note that if we are parsing tentatively, and @@ -14544,15 +14707,20 @@ cp_parser_lookup_name (cp_parser *parser, tree name, A::B' should be considered a type-name, even if `A' is dependent. */ type = make_typename_type (parser->scope, name, tag_type, - /*complain=*/1); + /*complain=*/tf_error); decl = TYPE_NAME (type); } - else if (is_template) + else if (is_template + && (cp_parser_next_token_ends_template_argument_p (parser) + || cp_lexer_next_token_is (parser->lexer, + CPP_CLOSE_PAREN))) decl = make_unbound_class_template (parser->scope, name, NULL_TREE, - /*complain=*/1); + /*complain=*/tf_error); else - decl = build_nt (SCOPE_REF, parser->scope, name); + decl = build_qualified_name (/*type=*/NULL_TREE, + parser->scope, name, + is_template); } else { @@ -14595,8 +14763,7 @@ cp_parser_lookup_name (cp_parser *parser, tree name, /* Look it up in the enclosing context, too. */ decl = lookup_name_real (name, tag_type != none_type, /*nonclass=*/0, - /*block_p=*/true, is_namespace, - /*flags=*/0); + /*block_p=*/true, is_namespace, flags); parser->object_scope = object_type; parser->qualifying_scope = NULL_TREE; if (object_decl) @@ -14606,8 +14773,7 @@ cp_parser_lookup_name (cp_parser *parser, tree name, { decl = lookup_name_real (name, tag_type != none_type, /*nonclass=*/0, - /*block_p=*/true, is_namespace, - /*flags=*/0); + /*block_p=*/true, is_namespace, flags); parser->qualifying_scope = NULL_TREE; parser->object_scope = NULL_TREE; } @@ -14619,8 +14785,8 @@ cp_parser_lookup_name (cp_parser *parser, tree name, /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ if (TREE_CODE (decl) == TREE_LIST) { - if (ambiguous_p) - *ambiguous_p = true; + if (ambiguous_decls) + *ambiguous_decls = decl; /* The error message we have to print is too complicated for cp_parser_error, so we incorporate its actions directly. */ if (!cp_parser_simulate_error (parser)) @@ -14662,7 +14828,7 @@ cp_parser_lookup_name_simple (cp_parser* parser, tree name) /*is_template=*/false, /*is_namespace=*/false, /*check_dependency=*/true, - /*ambiguous_p=*/NULL); + /*ambiguous_decls=*/NULL); } /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in @@ -14684,7 +14850,7 @@ cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) template struct A::B {}; - Similarly, in a elaborated-type-specifier: + Similarly, in an elaborated-type-specifier: namespace N { struct X{}; } @@ -15072,9 +15238,15 @@ cp_parser_function_definition_after_declarator (cp_parser* parser, /* Issue an error message. */ error ("named return values are no longer supported"); /* Skip tokens until we reach the start of the function body. */ - while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) - && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) - cp_lexer_consume_token (parser->lexer); + while (true) + { + cp_token *token = cp_lexer_peek_token (parser->lexer); + if (token->type == CPP_OPEN_BRACE + || token->type == CPP_EOF + || token->type == CPP_PRAGMA_EOL) + break; + cp_lexer_consume_token (parser->lexer); + } } /* The `extern' in `extern "C" void f () { ... }' does not apply to anything declared inside `f'. */ @@ -15120,6 +15292,7 @@ cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) tree decl = NULL_TREE; tree parameter_list; bool friend_p = false; + bool need_lang_pop; /* Look for the `template' keyword. */ if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) @@ -15128,7 +15301,19 @@ cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) /* And the `<'. */ if (!cp_parser_require (parser, CPP_LESS, "`<'")) return; - + /* [temp] + + A template ... shall not have C linkage. */ + if (current_lang_name == lang_name_c) + { + error ("template with C linkage"); + /* Give it C++ linkage to avoid confusing other parts of the + front end. */ + push_lang_context (lang_name_cplusplus); + need_lang_pop = true; + } + else + need_lang_pop = false; /* If the next token is `>', then we have an invalid specialization. Rather than complain about an invalid template parameter, issue an error message here. */ @@ -15139,12 +15324,8 @@ cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) parameter_list = NULL_TREE; } else - { - /* Parse the template parameters. */ - begin_template_parm_list (); - parameter_list = cp_parser_template_parameter_list (parser); - parameter_list = end_template_parm_list (parameter_list); - } + /* Parse the template parameters. */ + parameter_list = cp_parser_template_parameter_list (parser); /* Look for the `>'. */ cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); @@ -15189,7 +15370,10 @@ cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) /* Register member declarations. */ if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) finish_member_declaration (decl); - + /* For the erroneous case of a template with C linkage, we pushed an + implicit C++ linkage scope; exit that scope now. */ + if (need_lang_pop) + pop_lang_context (); /* If DECL is a function template, we must return to parse it later. (Even though there is no definition, there might be default arguments that need handling.) */ @@ -15330,8 +15514,10 @@ cp_parser_functional_cast (cp_parser* parser, tree type) cast = build_functional_cast (type, expression_list); /* [expr.const]/1: In an integral constant expression "only type conversions to integral or enumeration type can be used". */ - if (cast != error_mark_node && !type_dependent_expression_p (type) - && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type))) + if (TREE_CODE (type) == TYPE_DECL) + type = TREE_TYPE (type); + if (cast != error_mark_node && !dependent_type_p (type) + && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) { if (cp_parser_non_integral_constant_expression (parser, "a call to a constructor")) @@ -15412,6 +15598,7 @@ cp_parser_enclosed_template_argument_list (cp_parser* parser) tree saved_qualifying_scope; tree saved_object_scope; bool saved_greater_than_is_operator_p; + bool saved_skip_evaluation; /* [temp.names] @@ -15426,6 +15613,10 @@ cp_parser_enclosed_template_argument_list (cp_parser* parser) saved_scope = parser->scope; saved_qualifying_scope = parser->qualifying_scope; saved_object_scope = parser->object_scope; + /* We need to evaluate the template arguments, even though this + template-id may be nested within a "sizeof". */ + saved_skip_evaluation = skip_evaluation; + skip_evaluation = false; /* Parse the template-argument-list itself. */ if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) arguments = NULL_TREE; @@ -15463,11 +15654,8 @@ cp_parser_enclosed_template_argument_list (cp_parser* parser) "a template argument list"); } } - else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) - error ("missing %<>%> to terminate the template argument list"); else - /* It's what we want, a '>'; consume it. */ - cp_lexer_consume_token (parser->lexer); + cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); /* The `>' token might be a greater-than operator again now. */ parser->greater_than_is_operator_p = saved_greater_than_is_operator_p; @@ -15475,6 +15663,7 @@ cp_parser_enclosed_template_argument_list (cp_parser* parser) parser->scope = saved_scope; parser->qualifying_scope = saved_qualifying_scope; parser->object_scope = saved_object_scope; + skip_evaluation = saved_skip_evaluation; return arguments; } @@ -15575,7 +15764,6 @@ cp_parser_save_default_args (cp_parser* parser, tree decl) TREE_PURPOSE (parser->unparsed_functions_queues)); break; } - return; } /* FN is a FUNCTION_DECL which may contains a parameter with an @@ -15628,6 +15816,9 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) /* Parse the assignment-expression. */ parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false); + if (!processing_template_decl) + parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg); + TREE_PURPOSE (parm) = parsed_arg; /* Update any instantiations we've already created. */ @@ -15645,6 +15836,9 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) cp_parser_pop_lexer (parser); } + /* Make sure no default arg is missing. */ + check_default_args (fn); + /* Restore the state of local_variables_forbidden_p. */ parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; @@ -15676,7 +15870,7 @@ cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) saved_message = parser->type_definition_forbidden_message; /* And create the new one. */ parser->type_definition_forbidden_message - = xmalloc (strlen (format) + = XNEWVEC (const char, strlen (format) + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) + 1 /* `\0' */); sprintf ((char *) parser->type_definition_forbidden_message, @@ -15869,27 +16063,38 @@ cp_parser_skip_until_found (cp_parser* parser, { /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); - /* If we've reached the token we want, consume it and - stop. */ + + /* If we've reached the token we want, consume it and stop. */ if (token->type == type && !nesting_depth) { cp_lexer_consume_token (parser->lexer); return; } - /* If we've run out of tokens, stop. */ - if (token->type == CPP_EOF) - return; - if (token->type == CPP_OPEN_BRACE - || token->type == CPP_OPEN_PAREN - || token->type == CPP_OPEN_SQUARE) - ++nesting_depth; - else if (token->type == CPP_CLOSE_BRACE - || token->type == CPP_CLOSE_PAREN - || token->type == CPP_CLOSE_SQUARE) + + switch (token->type) { + case CPP_EOF: + case CPP_PRAGMA_EOL: + /* If we've run out of tokens, stop. */ + return; + + case CPP_OPEN_BRACE: + case CPP_OPEN_PAREN: + case CPP_OPEN_SQUARE: + ++nesting_depth; + break; + + case CPP_CLOSE_BRACE: + case CPP_CLOSE_PAREN: + case CPP_CLOSE_SQUARE: if (nesting_depth-- == 0) return; + break; + + default: + break; } + /* Consume this token. */ cp_lexer_consume_token (parser->lexer); } @@ -15964,7 +16169,7 @@ cp_parser_next_token_ends_template_argument_p (cp_parser *parser) return (token->type == CPP_COMMA || token->type == CPP_GREATER); } -/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the +/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ static bool @@ -16108,7 +16313,9 @@ cp_parser_cache_group (cp_parser *parser, && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) return; /* If we've reached the end of the file, stop. */ - if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) + if (cp_lexer_next_token_is (parser->lexer, CPP_EOF) + || (end != CPP_PRAGMA_EOL + && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))) return; /* Consume the next token. */ token = cp_lexer_consume_token (parser->lexer); @@ -16121,6 +16328,8 @@ cp_parser_cache_group (cp_parser *parser, } else if (token->type == CPP_OPEN_PAREN) cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); + else if (token->type == CPP_PRAGMA) + cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1); else if (token->type == end) return; } @@ -16290,7 +16499,7 @@ cp_parser_objc_expression (cp_parser* parser) break; } default: - error ("misplaced `@%D' Objective-C++ construct", kwd->value); + error ("misplaced %<@%D%> Objective-C++ construct", kwd->value); cp_parser_skip_to_end_of_block_or_statement (parser); } @@ -16431,7 +16640,7 @@ cp_parser_objc_encode_expression (cp_parser* parser) if (!type) { - error ("`@encode' must specify a type as an argument"); + error ("%<@encode%> must specify a type as an argument"); return error_mark_node; } @@ -16499,27 +16708,45 @@ cp_parser_objc_selector_expression (cp_parser* parser) cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); token = cp_lexer_peek_token (parser->lexer); - while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) + while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON + || token->type == CPP_SCOPE) { tree selector = NULL_TREE; - if (token->type != CPP_COLON) + if (token->type != CPP_COLON + || token->type == CPP_SCOPE) selector = cp_parser_objc_selector (parser); - /* Detect if we have a unary selector. */ - if (maybe_unary_selector_p - && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) + if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON) + && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)) { - sel_seq = selector; - goto finish_selector; + /* Detect if we have a unary selector. */ + if (maybe_unary_selector_p) + { + sel_seq = selector; + goto finish_selector; + } + else + { + cp_parser_error (parser, "expected %<:%>"); + } } - maybe_unary_selector_p = false; - cp_parser_require (parser, CPP_COLON, "`:'"); - - sel_seq - = chainon (sel_seq, - build_tree_list (selector, NULL_TREE)); + token = cp_lexer_consume_token (parser->lexer); + + if (token->type == CPP_SCOPE) + { + sel_seq + = chainon (sel_seq, + build_tree_list (selector, NULL_TREE)); + sel_seq + = chainon (sel_seq, + build_tree_list (NULL_TREE, NULL_TREE)); + } + else + sel_seq + = chainon (sel_seq, + build_tree_list (selector, NULL_TREE)); token = cp_lexer_peek_token (parser->lexer); } @@ -16839,7 +17066,7 @@ cp_parser_objc_interstitial_code (cp_parser* parser) cp_parser_linkage_specification (parser); /* Handle #pragma, if any. */ else if (token->type == CPP_PRAGMA) - cp_lexer_handle_pragma (parser->lexer); + cp_parser_pragma (parser, pragma_external); /* Allow stray semicolons. */ else if (token->type == CPP_SEMICOLON) cp_lexer_consume_token (parser->lexer); @@ -16979,7 +17206,8 @@ cp_parser_objc_class_ivars (cp_parser* parser) { /* Get the name of the bitfield. */ declarator = make_id_declarator (NULL_TREE, - cp_parser_identifier (parser)); + cp_parser_identifier (parser), + sfk_none); eat_colon: cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ @@ -17057,7 +17285,7 @@ cp_parser_objc_protocol_declaration (cp_parser* parser) cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)) { - error ("identifier expected after `@protocol'"); + error ("identifier expected after %<@protocol%>"); goto finish; } @@ -17193,7 +17421,7 @@ cp_parser_objc_declaration (cp_parser* parser) cp_parser_objc_end_implementation (parser); break; default: - error ("misplaced `@%D' Objective-C++ construct", kwd->value); + error ("misplaced %<@%D%> Objective-C++ construct", kwd->value); cp_parser_skip_to_end_of_block_or_statement (parser); } } @@ -17324,17 +17552,117 @@ cp_parser_objc_statement (cp_parser * parser) { case RID_AT_THROW: return cp_parser_objc_throw_statement (parser); default: - error ("misplaced `@%D' Objective-C++ construct", kwd->value); + error ("misplaced %<@%D%> Objective-C++ construct", kwd->value); cp_parser_skip_to_end_of_block_or_statement (parser); } return error_mark_node; } - /* The parser. */ static GTY (()) cp_parser *the_parser; + +/* Special handling for the first token or line in the file. The first + thing in the file might be #pragma GCC pch_preprocess, which loads a + PCH file, which is a GC collection point. So we need to handle this + first pragma without benefit of an existing lexer structure. + + Always returns one token to the caller in *FIRST_TOKEN. This is + either the true first token of the file, or the first token after + the initial pragma. */ + +static void +cp_parser_initial_pragma (cp_token *first_token) +{ + tree name = NULL; + + cp_lexer_get_preprocessor_token (NULL, first_token); + if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS) + return; + + cp_lexer_get_preprocessor_token (NULL, first_token); + if (first_token->type == CPP_STRING) + { + name = first_token->value; + + cp_lexer_get_preprocessor_token (NULL, first_token); + if (first_token->type != CPP_PRAGMA_EOL) + error ("junk at end of %<#pragma GCC pch_preprocess%>"); + } + else + error ("expected string literal"); + + /* Skip to the end of the pragma. */ + while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF) + cp_lexer_get_preprocessor_token (NULL, first_token); + + /* Read one more token to return to our caller. */ + cp_lexer_get_preprocessor_token (NULL, first_token); + + /* Now actually load the PCH file. */ + if (name) + c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); +} + +/* Normal parsing of a pragma token. Here we can (and must) use the + regular lexer. */ + +static bool +cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED) +{ + cp_token *pragma_tok; + unsigned int id; + + pragma_tok = cp_lexer_consume_token (parser->lexer); + gcc_assert (pragma_tok->type == CPP_PRAGMA); + parser->lexer->in_pragma = true; + + id = pragma_tok->pragma_kind; + switch (id) + { + case PRAGMA_GCC_PCH_PREPROCESS: + error ("%<#pragma GCC pch_preprocess%> must be first"); + break; + + default: + gcc_assert (id >= PRAGMA_FIRST_EXTERNAL); + c_invoke_pragma_handler (id); + break; + } + + cp_parser_skip_to_pragma_eol (parser, pragma_tok); + return false; +} + +/* The interface the pragma parsers have to the lexer. */ + +enum cpp_ttype +pragma_lex (tree *value) +{ + cp_token *tok; + enum cpp_ttype ret; + + tok = cp_lexer_peek_token (the_parser->lexer); + + ret = tok->type; + *value = tok->value; + + if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) + ret = CPP_EOF; + else if (ret == CPP_STRING) + *value = cp_parser_string_literal (the_parser, false, false); + else + { + cp_lexer_consume_token (the_parser->lexer); + if (ret == CPP_KEYWORD) + ret = CPP_NAME; + } + + return ret; +} + + /* External interface. */ /* Parse one entire translation unit. */