2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* The location at which this token was found. */
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
98 static cp_token eof_token =
100 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
109 typedef struct cp_lexer GTY (())
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114 /* If the lexer owns the buffer, this is the number of tokens in the
116 size_t buffer_length;
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position GTY ((skip)) last_token;
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position GTY ((skip)) next_token;
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
130 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer *next;
135 /* True if we should output debugging information. */
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
149 typedef struct cp_token_cache GTY(())
151 /* The beginning of the token range. */
152 cp_token * GTY((skip)) first;
154 /* Points immediately after the last token in the range. */
155 cp_token * GTY ((skip)) last;
160 static cp_lexer *cp_lexer_new_main
162 static cp_lexer *cp_lexer_new_from_tokens
163 (cp_token_cache *tokens);
164 static void cp_lexer_destroy
166 static int cp_lexer_saving_tokens
168 static cp_token_position cp_lexer_token_position
170 static cp_token *cp_lexer_token_at
171 (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
176 static cp_token *cp_lexer_peek_nth_token
177 (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
186 static void cp_lexer_purge_token
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
192 static void cp_lexer_commit_tokens
194 static void cp_lexer_rollback_tokens
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
201 static void cp_lexer_start_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
215 static cp_token_cache *cp_token_cache_new
216 (cp_token *, cp_token *);
218 static void cp_parser_initial_pragma
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
260 cp_lexer_new_main (void)
262 cp_token first_token;
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token);
274 c_common_no_more_pch ();
276 /* Allocate the memory. */
277 lexer = GGC_CNEW (cp_lexer);
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284 CP_SAVED_TOKEN_STACK);
286 /* Create the buffer. */
287 alloc = CP_LEXER_BUFFER_SIZE;
288 buffer = GGC_NEWVEC (cp_token, alloc);
290 /* Put the first token in the buffer. */
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos->type != CPP_EOF)
303 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304 pos = buffer + space;
306 cp_lexer_get_preprocessor_token (lexer, pos);
308 lexer->buffer = buffer;
309 lexer->buffer_length = alloc - space;
310 lexer->last_token = pos;
311 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in)->client_diagnostic = true;
316 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
318 gcc_assert (lexer->next_token->type != CPP_PURGED);
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
328 cp_token *first = cache->first;
329 cp_token *last = cache->last;
330 cp_lexer *lexer = GGC_CNEW (cp_lexer);
332 /* We do not own the buffer. */
333 lexer->buffer = NULL;
334 lexer->buffer_length = 0;
335 lexer->next_token = first == last ? &eof_token : first;
336 lexer->last_token = last;
338 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339 CP_SAVED_TOKEN_STACK);
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer->debugging_p = false;
346 gcc_assert (lexer->next_token->type != CPP_PURGED);
350 /* Frees all resources associated with LEXER. */
353 cp_lexer_destroy (cp_lexer *lexer)
356 ggc_free (lexer->buffer);
357 VEC_free (cp_token_position, heap, lexer->saved_tokens);
361 /* Returns nonzero if debugging information should be output. */
363 #ifdef ENABLE_CHECKING
366 cp_lexer_debugging_p (cp_lexer *lexer)
368 return lexer->debugging_p;
371 #endif /* ENABLE_CHECKING */
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
376 gcc_assert (!previous_p || lexer->next_token != &eof_token);
378 return lexer->next_token - previous_p;
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
387 /* nonzero if we are presently saving tokens. */
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
392 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
403 static int is_extern_c = 0;
405 /* Get a new token from the preprocessor. */
407 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409 token->keyword = RID_MAX;
410 token->pragma_kind = PRAGMA_NONE;
411 token->in_system_header = in_system_header;
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c += pending_lang_change;
417 pending_lang_change = 0;
418 token->implicit_extern_c = is_extern_c > 0;
420 /* Check to see if this token is a keyword. */
421 if (token->type == CPP_NAME)
423 if (C_IS_RESERVED_WORD (token->u.value))
425 /* Mark this token as a keyword. */
426 token->type = CPP_KEYWORD;
427 /* Record which keyword. */
428 token->keyword = C_RID_CODE (token->u.value);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token->u.value = ridpointers[token->keyword];
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
441 /* Warn about the C++0x keyword (but still treat it as
443 warning (OPT_Wc__0x_compat,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token->u.value));
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_RID_CODE (token->u.value) = RID_MAX;
452 token->ambiguous_p = false;
453 token->keyword = RID_MAX;
456 /* Handle Objective-C++ keywords. */
457 else if (token->type == CPP_AT_NAME)
459 token->type = CPP_KEYWORD;
460 switch (C_RID_CODE (token->u.value))
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467 case RID_THROW: token->keyword = RID_AT_THROW; break;
468 case RID_TRY: token->keyword = RID_AT_TRY; break;
469 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470 default: token->keyword = C_RID_CODE (token->u.value);
473 else if (token->type == CPP_PRAGMA)
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477 token->u.value = NULL_TREE;
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
484 cp_lexer_set_source_position_from_token (cp_token *token)
486 if (token->type != CPP_EOF)
488 input_location = token->location;
489 in_system_header = token->in_system_header;
493 /* Return a pointer to the next token in the token stream, but do not
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
499 if (cp_lexer_debugging_p (lexer))
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503 putc ('\n', cp_lexer_debug_stream);
505 return lexer->next_token;
508 /* Return true if the next token has the indicated TYPE. */
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
513 return cp_lexer_peek_token (lexer)->type == type;
516 /* Return true if the next token does not have the indicated TYPE. */
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
521 return !cp_lexer_next_token_is (lexer, type);
524 /* Return true if the next token is the indicated KEYWORD. */
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
529 return cp_lexer_peek_token (lexer)->keyword == keyword;
532 /* Return true if the next token is a keyword for a decl-specifier. */
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 token = cp_lexer_peek_token (lexer);
540 switch (token->keyword)
542 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
567 /* GNU extensions. */
570 /* C++0x extensions. */
579 /* Return a pointer to the Nth token in the token stream. If N is 1,
580 then this is precisely equivalent to cp_lexer_peek_token (except
581 that it is not inline). One would like to disallow that case, but
582 there is one case (cp_parser_nth_token_starts_template_id) where
583 the caller passes a variable for N and it might be 1. */
586 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 /* N is 1-based, not zero-based. */
593 if (cp_lexer_debugging_p (lexer))
594 fprintf (cp_lexer_debug_stream,
595 "cp_lexer: peeking ahead %ld at token: ", (long)n);
598 token = lexer->next_token;
599 gcc_assert (!n || token != &eof_token);
603 if (token == lexer->last_token)
609 if (token->type != CPP_PURGED)
613 if (cp_lexer_debugging_p (lexer))
615 cp_lexer_print_token (cp_lexer_debug_stream, token);
616 putc ('\n', cp_lexer_debug_stream);
622 /* Return the next token, and advance the lexer's next_token pointer
623 to point to the next non-purged token. */
626 cp_lexer_consume_token (cp_lexer* lexer)
628 cp_token *token = lexer->next_token;
630 gcc_assert (token != &eof_token);
631 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
636 if (lexer->next_token == lexer->last_token)
638 lexer->next_token = &eof_token;
643 while (lexer->next_token->type == CPP_PURGED);
645 cp_lexer_set_source_position_from_token (token);
647 /* Provide debugging output. */
648 if (cp_lexer_debugging_p (lexer))
650 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
651 cp_lexer_print_token (cp_lexer_debug_stream, token);
652 putc ('\n', cp_lexer_debug_stream);
658 /* Permanently remove the next token from the token stream, and
659 advance the next_token pointer to refer to the next non-purged
663 cp_lexer_purge_token (cp_lexer *lexer)
665 cp_token *tok = lexer->next_token;
667 gcc_assert (tok != &eof_token);
668 tok->type = CPP_PURGED;
669 tok->location = UNKNOWN_LOCATION;
670 tok->u.value = NULL_TREE;
671 tok->keyword = RID_MAX;
676 if (tok == lexer->last_token)
682 while (tok->type == CPP_PURGED);
683 lexer->next_token = tok;
686 /* Permanently remove all tokens after TOK, up to, but not
687 including, the token that will be returned next by
688 cp_lexer_peek_token. */
691 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
693 cp_token *peek = lexer->next_token;
695 if (peek == &eof_token)
696 peek = lexer->last_token;
698 gcc_assert (tok < peek);
700 for ( tok += 1; tok != peek; tok += 1)
702 tok->type = CPP_PURGED;
703 tok->location = UNKNOWN_LOCATION;
704 tok->u.value = NULL_TREE;
705 tok->keyword = RID_MAX;
709 /* Begin saving tokens. All tokens consumed after this point will be
713 cp_lexer_save_tokens (cp_lexer* lexer)
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
719 VEC_safe_push (cp_token_position, heap,
720 lexer->saved_tokens, lexer->next_token);
723 /* Commit to the portion of the token stream most recently saved. */
726 cp_lexer_commit_tokens (cp_lexer* lexer)
728 /* Provide debugging output. */
729 if (cp_lexer_debugging_p (lexer))
730 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
732 VEC_pop (cp_token_position, lexer->saved_tokens);
735 /* Return all tokens saved since the last call to cp_lexer_save_tokens
736 to the token stream. Stop saving tokens. */
739 cp_lexer_rollback_tokens (cp_lexer* lexer)
741 /* Provide debugging output. */
742 if (cp_lexer_debugging_p (lexer))
743 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
745 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 /* Print a representation of the TOKEN on the STREAM. */
750 #ifdef ENABLE_CHECKING
753 cp_lexer_print_token (FILE * stream, cp_token *token)
755 /* We don't use cpp_type2name here because the parser defines
756 a few tokens of its own. */
757 static const char *const token_names[] = {
758 /* cpplib-defined token types */
764 /* C++ parser token types - see "Manifest constants", above. */
767 "NESTED_NAME_SPECIFIER",
771 /* If we have a name for the token, print it out. Otherwise, we
772 simply give the numeric code. */
773 gcc_assert (token->type < ARRAY_SIZE(token_names));
774 fputs (token_names[token->type], stream);
776 /* For some tokens, print the associated data. */
780 /* Some keywords have a value that is not an IDENTIFIER_NODE.
781 For example, `struct' is mapped to an INTEGER_CST. */
782 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
784 /* else fall through */
786 fputs (IDENTIFIER_POINTER (token->u.value), stream);
791 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799 /* Start emitting debugging information. */
802 cp_lexer_start_debugging (cp_lexer* lexer)
804 lexer->debugging_p = true;
807 /* Stop emitting debugging information. */
810 cp_lexer_stop_debugging (cp_lexer* lexer)
812 lexer->debugging_p = false;
815 #endif /* ENABLE_CHECKING */
817 /* Create a new cp_token_cache, representing a range of tokens. */
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
822 cp_token_cache *cache = GGC_NEW (cp_token_cache);
823 cache->first = first;
829 /* Decl-specifiers. */
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
836 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 /* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
846 static cp_declarator *make_call_declarator
847 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
848 static cp_declarator *make_array_declarator
849 (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851 (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853 (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855 (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857 (cp_cv_quals, tree, cp_declarator *);
859 /* An erroneous declarator. */
860 static cp_declarator *cp_error_declarator;
862 /* The obstack on which declarators and related data structures are
864 static struct obstack declarator_obstack;
866 /* Alloc BYTES from the declarator memory pool. */
869 alloc_declarator (size_t bytes)
871 return obstack_alloc (&declarator_obstack, bytes);
874 /* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
880 cp_declarator *declarator;
882 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883 declarator->kind = kind;
884 declarator->attributes = NULL_TREE;
885 declarator->declarator = NULL;
886 declarator->parameter_pack_p = false;
891 /* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899 special_function_kind sfk)
901 cp_declarator *declarator;
903 /* It is valid to write:
905 class C { void f(); };
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
913 if (qualifying_scope && TYPE_P (qualifying_scope))
914 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
916 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
920 declarator = make_declarator (cdk_id);
921 declarator->u.id.qualifying_scope = qualifying_scope;
922 declarator->u.id.unqualified_name = unqualified_name;
923 declarator->u.id.sfk = sfk;
928 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
935 cp_declarator *declarator;
937 declarator = make_declarator (cdk_pointer);
938 declarator->declarator = target;
939 declarator->u.pointer.qualifiers = cv_qualifiers;
940 declarator->u.pointer.class_type = NULL_TREE;
943 declarator->parameter_pack_p = target->parameter_pack_p;
944 target->parameter_pack_p = false;
947 declarator->parameter_pack_p = false;
952 /* Like make_pointer_declarator -- but for references. */
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
958 cp_declarator *declarator;
960 declarator = make_declarator (cdk_reference);
961 declarator->declarator = target;
962 declarator->u.reference.qualifiers = cv_qualifiers;
963 declarator->u.reference.rvalue_ref = rvalue_ref;
966 declarator->parameter_pack_p = target->parameter_pack_p;
967 target->parameter_pack_p = false;
970 declarator->parameter_pack_p = false;
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980 cp_declarator *pointee)
982 cp_declarator *declarator;
984 declarator = make_declarator (cdk_ptrmem);
985 declarator->declarator = pointee;
986 declarator->u.pointer.qualifiers = cv_qualifiers;
987 declarator->u.pointer.class_type = class_type;
991 declarator->parameter_pack_p = pointee->parameter_pack_p;
992 pointee->parameter_pack_p = false;
995 declarator->parameter_pack_p = false;
1000 /* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1006 make_call_declarator (cp_declarator *target,
1007 cp_parameter_declarator *parms,
1008 cp_cv_quals cv_qualifiers,
1009 tree exception_specification)
1011 cp_declarator *declarator;
1013 declarator = make_declarator (cdk_function);
1014 declarator->declarator = target;
1015 declarator->u.function.parameters = parms;
1016 declarator->u.function.qualifiers = cv_qualifiers;
1017 declarator->u.function.exception_specification = exception_specification;
1020 declarator->parameter_pack_p = target->parameter_pack_p;
1021 target->parameter_pack_p = false;
1024 declarator->parameter_pack_p = false;
1029 /* Make a declarator for an array of BOUNDS elements, each of which is
1030 defined by ELEMENT. */
1033 make_array_declarator (cp_declarator *element, tree bounds)
1035 cp_declarator *declarator;
1037 declarator = make_declarator (cdk_array);
1038 declarator->declarator = element;
1039 declarator->u.array.bounds = bounds;
1042 declarator->parameter_pack_p = element->parameter_pack_p;
1043 element->parameter_pack_p = false;
1046 declarator->parameter_pack_p = false;
1051 /* Determine whether the declarator we've seen so far can be a
1052 parameter pack, when followed by an ellipsis. */
1054 declarator_can_be_parameter_pack (cp_declarator *declarator)
1056 /* Search for a declarator name, or any other declarator that goes
1057 after the point where the ellipsis could appear in a parameter
1058 pack. If we find any of these, then this declarator can not be
1059 made into a parameter pack. */
1061 while (declarator && !found)
1063 switch ((int)declarator->kind)
1074 declarator = declarator->declarator;
1082 cp_parameter_declarator *no_parameters;
1084 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1085 DECLARATOR and DEFAULT_ARGUMENT. */
1087 cp_parameter_declarator *
1088 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1089 cp_declarator *declarator,
1090 tree default_argument)
1092 cp_parameter_declarator *parameter;
1094 parameter = ((cp_parameter_declarator *)
1095 alloc_declarator (sizeof (cp_parameter_declarator)));
1096 parameter->next = NULL;
1097 if (decl_specifiers)
1098 parameter->decl_specifiers = *decl_specifiers;
1100 clear_decl_specs (¶meter->decl_specifiers);
1101 parameter->declarator = declarator;
1102 parameter->default_argument = default_argument;
1103 parameter->ellipsis_p = false;
1108 /* Returns true iff DECLARATOR is a declaration for a function. */
1111 function_declarator_p (const cp_declarator *declarator)
1115 if (declarator->kind == cdk_function
1116 && declarator->declarator->kind == cdk_id)
1118 if (declarator->kind == cdk_id
1119 || declarator->kind == cdk_error)
1121 declarator = declarator->declarator;
1131 A cp_parser parses the token stream as specified by the C++
1132 grammar. Its job is purely parsing, not semantic analysis. For
1133 example, the parser breaks the token stream into declarators,
1134 expressions, statements, and other similar syntactic constructs.
1135 It does not check that the types of the expressions on either side
1136 of an assignment-statement are compatible, or that a function is
1137 not declared with a parameter of type `void'.
1139 The parser invokes routines elsewhere in the compiler to perform
1140 semantic analysis and to build up the abstract syntax tree for the
1143 The parser (and the template instantiation code, which is, in a
1144 way, a close relative of parsing) are the only parts of the
1145 compiler that should be calling push_scope and pop_scope, or
1146 related functions. The parser (and template instantiation code)
1147 keeps track of what scope is presently active; everything else
1148 should simply honor that. (The code that generates static
1149 initializers may also need to set the scope, in order to check
1150 access control correctly when emitting the initializers.)
1155 The parser is of the standard recursive-descent variety. Upcoming
1156 tokens in the token stream are examined in order to determine which
1157 production to use when parsing a non-terminal. Some C++ constructs
1158 require arbitrary look ahead to disambiguate. For example, it is
1159 impossible, in the general case, to tell whether a statement is an
1160 expression or declaration without scanning the entire statement.
1161 Therefore, the parser is capable of "parsing tentatively." When the
1162 parser is not sure what construct comes next, it enters this mode.
1163 Then, while we attempt to parse the construct, the parser queues up
1164 error messages, rather than issuing them immediately, and saves the
1165 tokens it consumes. If the construct is parsed successfully, the
1166 parser "commits", i.e., it issues any queued error messages and
1167 the tokens that were being preserved are permanently discarded.
1168 If, however, the construct is not parsed successfully, the parser
1169 rolls back its state completely so that it can resume parsing using
1170 a different alternative.
1175 The performance of the parser could probably be improved substantially.
1176 We could often eliminate the need to parse tentatively by looking ahead
1177 a little bit. In some places, this approach might not entirely eliminate
1178 the need to parse tentatively, but it might still speed up the average
1181 /* Flags that are passed to some parsing functions. These values can
1182 be bitwise-ored together. */
1184 typedef enum cp_parser_flags
1187 CP_PARSER_FLAGS_NONE = 0x0,
1188 /* The construct is optional. If it is not present, then no error
1189 should be issued. */
1190 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1191 /* When parsing a type-specifier, do not allow user-defined types. */
1192 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1195 /* The different kinds of declarators we want to parse. */
1197 typedef enum cp_parser_declarator_kind
1199 /* We want an abstract declarator. */
1200 CP_PARSER_DECLARATOR_ABSTRACT,
1201 /* We want a named declarator. */
1202 CP_PARSER_DECLARATOR_NAMED,
1203 /* We don't mind, but the name must be an unqualified-id. */
1204 CP_PARSER_DECLARATOR_EITHER
1205 } cp_parser_declarator_kind;
1207 /* The precedence values used to parse binary expressions. The minimum value
1208 of PREC must be 1, because zero is reserved to quickly discriminate
1209 binary operators from other tokens. */
1214 PREC_LOGICAL_OR_EXPRESSION,
1215 PREC_LOGICAL_AND_EXPRESSION,
1216 PREC_INCLUSIVE_OR_EXPRESSION,
1217 PREC_EXCLUSIVE_OR_EXPRESSION,
1218 PREC_AND_EXPRESSION,
1219 PREC_EQUALITY_EXPRESSION,
1220 PREC_RELATIONAL_EXPRESSION,
1221 PREC_SHIFT_EXPRESSION,
1222 PREC_ADDITIVE_EXPRESSION,
1223 PREC_MULTIPLICATIVE_EXPRESSION,
1225 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1228 /* A mapping from a token type to a corresponding tree node type, with a
1229 precedence value. */
1231 typedef struct cp_parser_binary_operations_map_node
1233 /* The token type. */
1234 enum cpp_ttype token_type;
1235 /* The corresponding tree code. */
1236 enum tree_code tree_type;
1237 /* The precedence of this operator. */
1238 enum cp_parser_prec prec;
1239 } cp_parser_binary_operations_map_node;
1241 /* The status of a tentative parse. */
1243 typedef enum cp_parser_status_kind
1245 /* No errors have occurred. */
1246 CP_PARSER_STATUS_KIND_NO_ERROR,
1247 /* An error has occurred. */
1248 CP_PARSER_STATUS_KIND_ERROR,
1249 /* We are committed to this tentative parse, whether or not an error
1251 CP_PARSER_STATUS_KIND_COMMITTED
1252 } cp_parser_status_kind;
1254 typedef struct cp_parser_expression_stack_entry
1256 /* Left hand side of the binary operation we are currently
1259 /* Original tree code for left hand side, if it was a binary
1260 expression itself (used for -Wparentheses). */
1261 enum tree_code lhs_type;
1262 /* Tree code for the binary operation we are parsing. */
1263 enum tree_code tree_type;
1264 /* Precedence of the binary operation we are parsing. */
1266 } cp_parser_expression_stack_entry;
1268 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1269 entries because precedence levels on the stack are monotonically
1271 typedef struct cp_parser_expression_stack_entry
1272 cp_parser_expression_stack[NUM_PREC_VALUES];
1274 /* Context that is saved and restored when parsing tentatively. */
1275 typedef struct cp_parser_context GTY (())
1277 /* If this is a tentative parsing context, the status of the
1279 enum cp_parser_status_kind status;
1280 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1281 that are looked up in this context must be looked up both in the
1282 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1283 the context of the containing expression. */
1286 /* The next parsing context in the stack. */
1287 struct cp_parser_context *next;
1288 } cp_parser_context;
1292 /* Constructors and destructors. */
1294 static cp_parser_context *cp_parser_context_new
1295 (cp_parser_context *);
1297 /* Class variables. */
1299 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1301 /* The operator-precedence table used by cp_parser_binary_expression.
1302 Transformed into an associative array (binops_by_token) by
1305 static const cp_parser_binary_operations_map_node binops[] = {
1306 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1307 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1309 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1310 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1311 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1313 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1314 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1316 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1317 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1319 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1320 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1321 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1322 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1324 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1325 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1327 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1329 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1331 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1333 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1335 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1338 /* The same as binops, but initialized by cp_parser_new so that
1339 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1341 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1343 /* Constructors and destructors. */
1345 /* Construct a new context. The context below this one on the stack
1346 is given by NEXT. */
1348 static cp_parser_context *
1349 cp_parser_context_new (cp_parser_context* next)
1351 cp_parser_context *context;
1353 /* Allocate the storage. */
1354 if (cp_parser_context_free_list != NULL)
1356 /* Pull the first entry from the free list. */
1357 context = cp_parser_context_free_list;
1358 cp_parser_context_free_list = context->next;
1359 memset (context, 0, sizeof (*context));
1362 context = GGC_CNEW (cp_parser_context);
1364 /* No errors have occurred yet in this context. */
1365 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1366 /* If this is not the bottomost context, copy information that we
1367 need from the previous context. */
1370 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1371 expression, then we are parsing one in this context, too. */
1372 context->object_type = next->object_type;
1373 /* Thread the stack. */
1374 context->next = next;
1380 /* The cp_parser structure represents the C++ parser. */
1382 typedef struct cp_parser GTY(())
1384 /* The lexer from which we are obtaining tokens. */
1387 /* The scope in which names should be looked up. If NULL_TREE, then
1388 we look up names in the scope that is currently open in the
1389 source program. If non-NULL, this is either a TYPE or
1390 NAMESPACE_DECL for the scope in which we should look. It can
1391 also be ERROR_MARK, when we've parsed a bogus scope.
1393 This value is not cleared automatically after a name is looked
1394 up, so we must be careful to clear it before starting a new look
1395 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1396 will look up `Z' in the scope of `X', rather than the current
1397 scope.) Unfortunately, it is difficult to tell when name lookup
1398 is complete, because we sometimes peek at a token, look it up,
1399 and then decide not to consume it. */
1402 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1403 last lookup took place. OBJECT_SCOPE is used if an expression
1404 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1405 respectively. QUALIFYING_SCOPE is used for an expression of the
1406 form "X::Y"; it refers to X. */
1408 tree qualifying_scope;
1410 /* A stack of parsing contexts. All but the bottom entry on the
1411 stack will be tentative contexts.
1413 We parse tentatively in order to determine which construct is in
1414 use in some situations. For example, in order to determine
1415 whether a statement is an expression-statement or a
1416 declaration-statement we parse it tentatively as a
1417 declaration-statement. If that fails, we then reparse the same
1418 token stream as an expression-statement. */
1419 cp_parser_context *context;
1421 /* True if we are parsing GNU C++. If this flag is not set, then
1422 GNU extensions are not recognized. */
1423 bool allow_gnu_extensions_p;
1425 /* TRUE if the `>' token should be interpreted as the greater-than
1426 operator. FALSE if it is the end of a template-id or
1427 template-parameter-list. In C++0x mode, this flag also applies to
1428 `>>' tokens, which are viewed as two consecutive `>' tokens when
1429 this flag is FALSE. */
1430 bool greater_than_is_operator_p;
1432 /* TRUE if default arguments are allowed within a parameter list
1433 that starts at this point. FALSE if only a gnu extension makes
1434 them permissible. */
1435 bool default_arg_ok_p;
1437 /* TRUE if we are parsing an integral constant-expression. See
1438 [expr.const] for a precise definition. */
1439 bool integral_constant_expression_p;
1441 /* TRUE if we are parsing an integral constant-expression -- but a
1442 non-constant expression should be permitted as well. This flag
1443 is used when parsing an array bound so that GNU variable-length
1444 arrays are tolerated. */
1445 bool allow_non_integral_constant_expression_p;
1447 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1448 been seen that makes the expression non-constant. */
1449 bool non_integral_constant_expression_p;
1451 /* TRUE if local variable names and `this' are forbidden in the
1453 bool local_variables_forbidden_p;
1455 /* TRUE if the declaration we are parsing is part of a
1456 linkage-specification of the form `extern string-literal
1458 bool in_unbraced_linkage_specification_p;
1460 /* TRUE if we are presently parsing a declarator, after the
1461 direct-declarator. */
1462 bool in_declarator_p;
1464 /* TRUE if we are presently parsing a template-argument-list. */
1465 bool in_template_argument_list_p;
1467 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1468 to IN_OMP_BLOCK if parsing OpenMP structured block and
1469 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1470 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1471 iteration-statement, OpenMP block or loop within that switch. */
1472 #define IN_SWITCH_STMT 1
1473 #define IN_ITERATION_STMT 2
1474 #define IN_OMP_BLOCK 4
1475 #define IN_OMP_FOR 8
1476 #define IN_IF_STMT 16
1477 unsigned char in_statement;
1479 /* TRUE if we are presently parsing the body of a switch statement.
1480 Note that this doesn't quite overlap with in_statement above.
1481 The difference relates to giving the right sets of error messages:
1482 "case not in switch" vs "break statement used with OpenMP...". */
1483 bool in_switch_statement_p;
1485 /* TRUE if we are parsing a type-id in an expression context. In
1486 such a situation, both "type (expr)" and "type (type)" are valid
1488 bool in_type_id_in_expr_p;
1490 /* TRUE if we are currently in a header file where declarations are
1491 implicitly extern "C". */
1492 bool implicit_extern_c;
1494 /* TRUE if strings in expressions should be translated to the execution
1496 bool translate_strings_p;
1498 /* TRUE if we are presently parsing the body of a function, but not
1500 bool in_function_body;
1502 /* If non-NULL, then we are parsing a construct where new type
1503 definitions are not permitted. The string stored here will be
1504 issued as an error message if a type is defined. */
1505 const char *type_definition_forbidden_message;
1507 /* A list of lists. The outer list is a stack, used for member
1508 functions of local classes. At each level there are two sub-list,
1509 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1510 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1511 TREE_VALUE's. The functions are chained in reverse declaration
1514 The TREE_PURPOSE sublist contains those functions with default
1515 arguments that need post processing, and the TREE_VALUE sublist
1516 contains those functions with definitions that need post
1519 These lists can only be processed once the outermost class being
1520 defined is complete. */
1521 tree unparsed_functions_queues;
1523 /* The number of classes whose definitions are currently in
1525 unsigned num_classes_being_defined;
1527 /* The number of template parameter lists that apply directly to the
1528 current declaration. */
1529 unsigned num_template_parameter_lists;
1534 /* Constructors and destructors. */
1536 static cp_parser *cp_parser_new
1539 /* Routines to parse various constructs.
1541 Those that return `tree' will return the error_mark_node (rather
1542 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1543 Sometimes, they will return an ordinary node if error-recovery was
1544 attempted, even though a parse error occurred. So, to check
1545 whether or not a parse error occurred, you should always use
1546 cp_parser_error_occurred. If the construct is optional (indicated
1547 either by an `_opt' in the name of the function that does the
1548 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1549 the construct is not present. */
1551 /* Lexical conventions [gram.lex] */
1553 static tree cp_parser_identifier
1555 static tree cp_parser_string_literal
1556 (cp_parser *, bool, bool);
1558 /* Basic concepts [gram.basic] */
1560 static bool cp_parser_translation_unit
1563 /* Expressions [gram.expr] */
1565 static tree cp_parser_primary_expression
1566 (cp_parser *, bool, bool, bool, cp_id_kind *);
1567 static tree cp_parser_id_expression
1568 (cp_parser *, bool, bool, bool *, bool, bool);
1569 static tree cp_parser_unqualified_id
1570 (cp_parser *, bool, bool, bool, bool);
1571 static tree cp_parser_nested_name_specifier_opt
1572 (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_class_or_namespace_name
1576 (cp_parser *, bool, bool, bool, bool, bool);
1577 static tree cp_parser_postfix_expression
1578 (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_postfix_open_square_expression
1580 (cp_parser *, tree, bool);
1581 static tree cp_parser_postfix_dot_deref_expression
1582 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1583 static tree cp_parser_parenthesized_expression_list
1584 (cp_parser *, bool, bool, bool, bool *);
1585 static void cp_parser_pseudo_destructor_name
1586 (cp_parser *, tree *, tree *);
1587 static tree cp_parser_unary_expression
1588 (cp_parser *, bool, bool);
1589 static enum tree_code cp_parser_unary_operator
1591 static tree cp_parser_new_expression
1593 static tree cp_parser_new_placement
1595 static tree cp_parser_new_type_id
1596 (cp_parser *, tree *);
1597 static cp_declarator *cp_parser_new_declarator_opt
1599 static cp_declarator *cp_parser_direct_new_declarator
1601 static tree cp_parser_new_initializer
1603 static tree cp_parser_delete_expression
1605 static tree cp_parser_cast_expression
1606 (cp_parser *, bool, bool);
1607 static tree cp_parser_binary_expression
1608 (cp_parser *, bool);
1609 static tree cp_parser_question_colon_clause
1610 (cp_parser *, tree);
1611 static tree cp_parser_assignment_expression
1612 (cp_parser *, bool);
1613 static enum tree_code cp_parser_assignment_operator_opt
1615 static tree cp_parser_expression
1616 (cp_parser *, bool);
1617 static tree cp_parser_constant_expression
1618 (cp_parser *, bool, bool *);
1619 static tree cp_parser_builtin_offsetof
1622 /* Statements [gram.stmt.stmt] */
1624 static void cp_parser_statement
1625 (cp_parser *, tree, bool, bool *);
1626 static void cp_parser_label_for_labeled_statement
1628 static tree cp_parser_expression_statement
1629 (cp_parser *, tree);
1630 static tree cp_parser_compound_statement
1631 (cp_parser *, tree, bool);
1632 static void cp_parser_statement_seq_opt
1633 (cp_parser *, tree);
1634 static tree cp_parser_selection_statement
1635 (cp_parser *, bool *);
1636 static tree cp_parser_condition
1638 static tree cp_parser_iteration_statement
1640 static void cp_parser_for_init_statement
1642 static tree cp_parser_jump_statement
1644 static void cp_parser_declaration_statement
1647 static tree cp_parser_implicitly_scoped_statement
1648 (cp_parser *, bool *);
1649 static void cp_parser_already_scoped_statement
1652 /* Declarations [gram.dcl.dcl] */
1654 static void cp_parser_declaration_seq_opt
1656 static void cp_parser_declaration
1658 static void cp_parser_block_declaration
1659 (cp_parser *, bool);
1660 static void cp_parser_simple_declaration
1661 (cp_parser *, bool);
1662 static void cp_parser_decl_specifier_seq
1663 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1664 static tree cp_parser_storage_class_specifier_opt
1666 static tree cp_parser_function_specifier_opt
1667 (cp_parser *, cp_decl_specifier_seq *);
1668 static tree cp_parser_type_specifier
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1671 static tree cp_parser_simple_type_specifier
1672 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1673 static tree cp_parser_type_name
1675 static tree cp_parser_nonclass_name
1676 (cp_parser* parser);
1677 static tree cp_parser_elaborated_type_specifier
1678 (cp_parser *, bool, bool);
1679 static tree cp_parser_enum_specifier
1681 static void cp_parser_enumerator_list
1682 (cp_parser *, tree);
1683 static void cp_parser_enumerator_definition
1684 (cp_parser *, tree);
1685 static tree cp_parser_namespace_name
1687 static void cp_parser_namespace_definition
1689 static void cp_parser_namespace_body
1691 static tree cp_parser_qualified_namespace_specifier
1693 static void cp_parser_namespace_alias_definition
1695 static bool cp_parser_using_declaration
1696 (cp_parser *, bool);
1697 static void cp_parser_using_directive
1699 static void cp_parser_asm_definition
1701 static void cp_parser_linkage_specification
1703 static void cp_parser_static_assert
1704 (cp_parser *, bool);
1705 static tree cp_parser_decltype
1708 /* Declarators [gram.dcl.decl] */
1710 static tree cp_parser_init_declarator
1711 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1712 static cp_declarator *cp_parser_declarator
1713 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1714 static cp_declarator *cp_parser_direct_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1716 static enum tree_code cp_parser_ptr_operator
1717 (cp_parser *, tree *, cp_cv_quals *);
1718 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1720 static tree cp_parser_declarator_id
1721 (cp_parser *, bool);
1722 static tree cp_parser_type_id
1724 static void cp_parser_type_specifier_seq
1725 (cp_parser *, bool, cp_decl_specifier_seq *);
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1729 (cp_parser *, bool *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration
1731 (cp_parser *, bool, bool *);
1732 static tree cp_parser_default_argument
1733 (cp_parser *, bool);
1734 static void cp_parser_function_body
1736 static tree cp_parser_initializer
1737 (cp_parser *, bool *, bool *);
1738 static tree cp_parser_initializer_clause
1739 (cp_parser *, bool *);
1740 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1741 (cp_parser *, bool *);
1743 static bool cp_parser_ctor_initializer_opt_and_function_body
1746 /* Classes [gram.class] */
1748 static tree cp_parser_class_name
1749 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1750 static tree cp_parser_class_specifier
1752 static tree cp_parser_class_head
1753 (cp_parser *, bool *, tree *, tree *);
1754 static enum tag_types cp_parser_class_key
1756 static void cp_parser_member_specification_opt
1758 static void cp_parser_member_declaration
1760 static tree cp_parser_pure_specifier
1762 static tree cp_parser_constant_initializer
1765 /* Derived classes [gram.class.derived] */
1767 static tree cp_parser_base_clause
1769 static tree cp_parser_base_specifier
1772 /* Special member functions [gram.special] */
1774 static tree cp_parser_conversion_function_id
1776 static tree cp_parser_conversion_type_id
1778 static cp_declarator *cp_parser_conversion_declarator_opt
1780 static bool cp_parser_ctor_initializer_opt
1782 static void cp_parser_mem_initializer_list
1784 static tree cp_parser_mem_initializer
1786 static tree cp_parser_mem_initializer_id
1789 /* Overloading [gram.over] */
1791 static tree cp_parser_operator_function_id
1793 static tree cp_parser_operator
1796 /* Templates [gram.temp] */
1798 static void cp_parser_template_declaration
1799 (cp_parser *, bool);
1800 static tree cp_parser_template_parameter_list
1802 static tree cp_parser_template_parameter
1803 (cp_parser *, bool *, bool *);
1804 static tree cp_parser_type_parameter
1805 (cp_parser *, bool *);
1806 static tree cp_parser_template_id
1807 (cp_parser *, bool, bool, bool);
1808 static tree cp_parser_template_name
1809 (cp_parser *, bool, bool, bool, bool *);
1810 static tree cp_parser_template_argument_list
1812 static tree cp_parser_template_argument
1814 static void cp_parser_explicit_instantiation
1816 static void cp_parser_explicit_specialization
1819 /* Exception handling [gram.exception] */
1821 static tree cp_parser_try_block
1823 static bool cp_parser_function_try_block
1825 static void cp_parser_handler_seq
1827 static void cp_parser_handler
1829 static tree cp_parser_exception_declaration
1831 static tree cp_parser_throw_expression
1833 static tree cp_parser_exception_specification_opt
1835 static tree cp_parser_type_id_list
1838 /* GNU Extensions */
1840 static tree cp_parser_asm_specification_opt
1842 static tree cp_parser_asm_operand_list
1844 static tree cp_parser_asm_clobber_list
1846 static tree cp_parser_attributes_opt
1848 static tree cp_parser_attribute_list
1850 static bool cp_parser_extension_opt
1851 (cp_parser *, int *);
1852 static void cp_parser_label_declaration
1855 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1856 static bool cp_parser_pragma
1857 (cp_parser *, enum pragma_context);
1859 /* Objective-C++ Productions */
1861 static tree cp_parser_objc_message_receiver
1863 static tree cp_parser_objc_message_args
1865 static tree cp_parser_objc_message_expression
1867 static tree cp_parser_objc_encode_expression
1869 static tree cp_parser_objc_defs_expression
1871 static tree cp_parser_objc_protocol_expression
1873 static tree cp_parser_objc_selector_expression
1875 static tree cp_parser_objc_expression
1877 static bool cp_parser_objc_selector_p
1879 static tree cp_parser_objc_selector
1881 static tree cp_parser_objc_protocol_refs_opt
1883 static void cp_parser_objc_declaration
1885 static tree cp_parser_objc_statement
1888 /* Utility Routines */
1890 static tree cp_parser_lookup_name
1891 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1892 static tree cp_parser_lookup_name_simple
1893 (cp_parser *, tree);
1894 static tree cp_parser_maybe_treat_template_as_class
1896 static bool cp_parser_check_declarator_template_parameters
1897 (cp_parser *, cp_declarator *);
1898 static bool cp_parser_check_template_parameters
1899 (cp_parser *, unsigned);
1900 static tree cp_parser_simple_cast_expression
1902 static tree cp_parser_global_scope_opt
1903 (cp_parser *, bool);
1904 static bool cp_parser_constructor_declarator_p
1905 (cp_parser *, bool);
1906 static tree cp_parser_function_definition_from_specifiers_and_declarator
1907 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1908 static tree cp_parser_function_definition_after_declarator
1909 (cp_parser *, bool);
1910 static void cp_parser_template_declaration_after_export
1911 (cp_parser *, bool);
1912 static void cp_parser_perform_template_parameter_access_checks
1913 (VEC (deferred_access_check,gc)*);
1914 static tree cp_parser_single_declaration
1915 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1916 static tree cp_parser_functional_cast
1917 (cp_parser *, tree);
1918 static tree cp_parser_save_member_function_body
1919 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1920 static tree cp_parser_enclosed_template_argument_list
1922 static void cp_parser_save_default_args
1923 (cp_parser *, tree);
1924 static void cp_parser_late_parsing_for_member
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_default_args
1927 (cp_parser *, tree);
1928 static tree cp_parser_sizeof_operand
1929 (cp_parser *, enum rid);
1930 static tree cp_parser_trait_expr
1931 (cp_parser *, enum rid);
1932 static bool cp_parser_declares_only_class_p
1934 static void cp_parser_set_storage_class
1935 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1936 static void cp_parser_set_decl_spec_type
1937 (cp_decl_specifier_seq *, tree, bool);
1938 static bool cp_parser_friend_p
1939 (const cp_decl_specifier_seq *);
1940 static cp_token *cp_parser_require
1941 (cp_parser *, enum cpp_ttype, const char *);
1942 static cp_token *cp_parser_require_keyword
1943 (cp_parser *, enum rid, const char *);
1944 static bool cp_parser_token_starts_function_definition_p
1946 static bool cp_parser_next_token_starts_class_definition_p
1948 static bool cp_parser_next_token_ends_template_argument_p
1950 static bool cp_parser_nth_token_starts_template_argument_list_p
1951 (cp_parser *, size_t);
1952 static enum tag_types cp_parser_token_is_class_key
1954 static void cp_parser_check_class_key
1955 (enum tag_types, tree type);
1956 static void cp_parser_check_access_in_redeclaration
1958 static bool cp_parser_optional_template_keyword
1960 static void cp_parser_pre_parsed_nested_name_specifier
1962 static void cp_parser_cache_group
1963 (cp_parser *, enum cpp_ttype, unsigned);
1964 static void cp_parser_parse_tentatively
1966 static void cp_parser_commit_to_tentative_parse
1968 static void cp_parser_abort_tentative_parse
1970 static bool cp_parser_parse_definitely
1972 static inline bool cp_parser_parsing_tentatively
1974 static bool cp_parser_uncommitted_to_tentative_parse_p
1976 static void cp_parser_error
1977 (cp_parser *, const char *);
1978 static void cp_parser_name_lookup_error
1979 (cp_parser *, tree, tree, const char *);
1980 static bool cp_parser_simulate_error
1982 static bool cp_parser_check_type_definition
1984 static void cp_parser_check_for_definition_in_return_type
1985 (cp_declarator *, tree);
1986 static void cp_parser_check_for_invalid_template_id
1987 (cp_parser *, tree);
1988 static bool cp_parser_non_integral_constant_expression
1989 (cp_parser *, const char *);
1990 static void cp_parser_diagnose_invalid_type_name
1991 (cp_parser *, tree, tree);
1992 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994 static int cp_parser_skip_to_closing_parenthesis
1995 (cp_parser *, bool, bool, bool);
1996 static void cp_parser_skip_to_end_of_statement
1998 static void cp_parser_consume_semicolon_at_end_of_statement
2000 static void cp_parser_skip_to_end_of_block_or_statement
2002 static bool cp_parser_skip_to_closing_brace
2004 static void cp_parser_skip_to_end_of_template_parameter_list
2006 static void cp_parser_skip_to_pragma_eol
2007 (cp_parser*, cp_token *);
2008 static bool cp_parser_error_occurred
2010 static bool cp_parser_allow_gnu_extensions_p
2012 static bool cp_parser_is_string_literal
2014 static bool cp_parser_is_keyword
2015 (cp_token *, enum rid);
2016 static tree cp_parser_make_typename_type
2017 (cp_parser *, tree, tree);
2018 static cp_declarator * cp_parser_make_indirect_declarator
2019 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021 /* Returns nonzero if we are parsing tentatively. */
2024 cp_parser_parsing_tentatively (cp_parser* parser)
2026 return parser->context->next != NULL;
2029 /* Returns nonzero if TOKEN is a string literal. */
2032 cp_parser_is_string_literal (cp_token* token)
2034 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2037 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2040 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2042 return token->keyword == keyword;
2045 /* If not parsing tentatively, issue a diagnostic of the form
2046 FILE:LINE: MESSAGE before TOKEN
2047 where TOKEN is the next token in the input stream. MESSAGE
2048 (specified by the caller) is usually of the form "expected
2052 cp_parser_error (cp_parser* parser, const char* message)
2054 if (!cp_parser_simulate_error (parser))
2056 cp_token *token = cp_lexer_peek_token (parser->lexer);
2057 /* This diagnostic makes more sense if it is tagged to the line
2058 of the token we just peeked at. */
2059 cp_lexer_set_source_position_from_token (token);
2061 if (token->type == CPP_PRAGMA)
2063 error ("%<#pragma%> is not allowed here");
2064 cp_parser_skip_to_pragma_eol (parser, token);
2068 c_parse_error (message,
2069 /* Because c_parser_error does not understand
2070 CPP_KEYWORD, keywords are treated like
2072 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2077 /* Issue an error about name-lookup failing. NAME is the
2078 IDENTIFIER_NODE DECL is the result of
2079 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2080 the thing that we hoped to find. */
2083 cp_parser_name_lookup_error (cp_parser* parser,
2086 const char* desired)
2088 /* If name lookup completely failed, tell the user that NAME was not
2090 if (decl == error_mark_node)
2092 if (parser->scope && parser->scope != global_namespace)
2093 error ("%<%E::%E%> has not been declared",
2094 parser->scope, name);
2095 else if (parser->scope == global_namespace)
2096 error ("%<::%E%> has not been declared", name);
2097 else if (parser->object_scope
2098 && !CLASS_TYPE_P (parser->object_scope))
2099 error ("request for member %qE in non-class type %qT",
2100 name, parser->object_scope);
2101 else if (parser->object_scope)
2102 error ("%<%T::%E%> has not been declared",
2103 parser->object_scope, name);
2105 error ("%qE has not been declared", name);
2107 else if (parser->scope && parser->scope != global_namespace)
2108 error ("%<%E::%E%> %s", parser->scope, name, desired);
2109 else if (parser->scope == global_namespace)
2110 error ("%<::%E%> %s", name, desired);
2112 error ("%qE %s", name, desired);
2115 /* If we are parsing tentatively, remember that an error has occurred
2116 during this tentative parse. Returns true if the error was
2117 simulated; false if a message should be issued by the caller. */
2120 cp_parser_simulate_error (cp_parser* parser)
2122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2124 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2130 /* Check for repeated decl-specifiers. */
2133 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2137 for (ds = ds_first; ds != ds_last; ++ds)
2139 unsigned count = decl_specs->specs[(int)ds];
2142 /* The "long" specifier is a special case because of "long long". */
2146 error ("%<long long long%> is too long for GCC");
2147 else if (pedantic && !in_system_header && warn_long_long
2148 && cxx_dialect == cxx98)
2149 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2153 static const char *const decl_spec_names[] = {
2169 error ("duplicate %qs", decl_spec_names[(int)ds]);
2174 /* This function is called when a type is defined. If type
2175 definitions are forbidden at this point, an error message is
2179 cp_parser_check_type_definition (cp_parser* parser)
2181 /* If types are forbidden here, issue a message. */
2182 if (parser->type_definition_forbidden_message)
2184 /* Use `%s' to print the string in case there are any escape
2185 characters in the message. */
2186 error ("%s", parser->type_definition_forbidden_message);
2192 /* This function is called when the DECLARATOR is processed. The TYPE
2193 was a type defined in the decl-specifiers. If it is invalid to
2194 define a type in the decl-specifiers for DECLARATOR, an error is
2198 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201 /* [dcl.fct] forbids type definitions in return types.
2202 Unfortunately, it's not easy to know whether or not we are
2203 processing a return type until after the fact. */
2205 && (declarator->kind == cdk_pointer
2206 || declarator->kind == cdk_reference
2207 || declarator->kind == cdk_ptrmem))
2208 declarator = declarator->declarator;
2210 && declarator->kind == cdk_function)
2212 error ("new types may not be defined in a return type");
2213 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2218 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2219 "<" in any valid C++ program. If the next token is indeed "<",
2220 issue a message warning the user about what appears to be an
2221 invalid attempt to form a template-id. */
2224 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227 cp_token_position start = 0;
2229 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232 error ("%qT is not a template", type);
2233 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2234 error ("%qE is not a template", type);
2236 error ("invalid template-id");
2237 /* Remember the location of the invalid "<". */
2238 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2239 start = cp_lexer_token_position (parser->lexer, true);
2240 /* Consume the "<". */
2241 cp_lexer_consume_token (parser->lexer);
2242 /* Parse the template arguments. */
2243 cp_parser_enclosed_template_argument_list (parser);
2244 /* Permanently remove the invalid template arguments so that
2245 this error message is not issued again. */
2247 cp_lexer_purge_tokens_after (parser->lexer, start);
2251 /* If parsing an integral constant-expression, issue an error message
2252 about the fact that THING appeared and return true. Otherwise,
2253 return false. In either case, set
2254 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2257 cp_parser_non_integral_constant_expression (cp_parser *parser,
2260 parser->non_integral_constant_expression_p = true;
2261 if (parser->integral_constant_expression_p)
2263 if (!parser->allow_non_integral_constant_expression_p)
2265 error ("%s cannot appear in a constant-expression", thing);
2272 /* Emit a diagnostic for an invalid type name. SCOPE is the
2273 qualifying scope (or NULL, if none) for ID. This function commits
2274 to the current active tentative parse, if any. (Otherwise, the
2275 problematic construct might be encountered again later, resulting
2276 in duplicate error messages.) */
2279 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2281 tree decl, old_scope;
2282 /* Try to lookup the identifier. */
2283 old_scope = parser->scope;
2284 parser->scope = scope;
2285 decl = cp_parser_lookup_name_simple (parser, id);
2286 parser->scope = old_scope;
2287 /* If the lookup found a template-name, it means that the user forgot
2288 to specify an argument list. Emit a useful error message. */
2289 if (TREE_CODE (decl) == TEMPLATE_DECL)
2290 error ("invalid use of template-name %qE without an argument list", decl);
2291 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2292 error ("invalid use of destructor %qD as a type", id);
2293 else if (TREE_CODE (decl) == TYPE_DECL)
2294 /* Something like 'unsigned A a;' */
2295 error ("invalid combination of multiple type-specifiers");
2296 else if (!parser->scope)
2298 /* Issue an error message. */
2299 error ("%qE does not name a type", id);
2300 /* If we're in a template class, it's possible that the user was
2301 referring to a type from a base class. For example:
2303 template <typename T> struct A { typedef T X; };
2304 template <typename T> struct B : public A<T> { X x; };
2306 The user should have said "typename A<T>::X". */
2307 if (processing_template_decl && current_class_type
2308 && TYPE_BINFO (current_class_type))
2312 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2316 tree base_type = BINFO_TYPE (b);
2317 if (CLASS_TYPE_P (base_type)
2318 && dependent_type_p (base_type))
2321 /* Go from a particular instantiation of the
2322 template (which will have an empty TYPE_FIELDs),
2323 to the main version. */
2324 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2325 for (field = TYPE_FIELDS (base_type);
2327 field = TREE_CHAIN (field))
2328 if (TREE_CODE (field) == TYPE_DECL
2329 && DECL_NAME (field) == id)
2331 inform ("(perhaps %<typename %T::%E%> was intended)",
2332 BINFO_TYPE (b), id);
2341 /* Here we diagnose qualified-ids where the scope is actually correct,
2342 but the identifier does not resolve to a valid type name. */
2343 else if (parser->scope != error_mark_node)
2345 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2346 error ("%qE in namespace %qE does not name a type",
2348 else if (TYPE_P (parser->scope))
2349 error ("%qE in class %qT does not name a type", id, parser->scope);
2353 cp_parser_commit_to_tentative_parse (parser);
2356 /* Check for a common situation where a type-name should be present,
2357 but is not, and issue a sensible error message. Returns true if an
2358 invalid type-name was detected.
2360 The situation handled by this function are variable declarations of the
2361 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2362 Usually, `ID' should name a type, but if we got here it means that it
2363 does not. We try to emit the best possible error message depending on
2364 how exactly the id-expression looks like. */
2367 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2371 cp_parser_parse_tentatively (parser);
2372 id = cp_parser_id_expression (parser,
2373 /*template_keyword_p=*/false,
2374 /*check_dependency_p=*/true,
2375 /*template_p=*/NULL,
2376 /*declarator_p=*/true,
2377 /*optional_p=*/false);
2378 /* After the id-expression, there should be a plain identifier,
2379 otherwise this is not a simple variable declaration. Also, if
2380 the scope is dependent, we cannot do much. */
2381 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2382 || (parser->scope && TYPE_P (parser->scope)
2383 && dependent_type_p (parser->scope))
2384 || TREE_CODE (id) == TYPE_DECL)
2386 cp_parser_abort_tentative_parse (parser);
2389 if (!cp_parser_parse_definitely (parser))
2392 /* Emit a diagnostic for the invalid type. */
2393 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2394 /* Skip to the end of the declaration; there's no point in
2395 trying to process it. */
2396 cp_parser_skip_to_end_of_block_or_statement (parser);
2400 /* Consume tokens up to, and including, the next non-nested closing `)'.
2401 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2402 are doing error recovery. Returns -1 if OR_COMMA is true and we
2403 found an unnested comma. */
2406 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2411 unsigned paren_depth = 0;
2412 unsigned brace_depth = 0;
2414 if (recovering && !or_comma
2415 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2420 cp_token * token = cp_lexer_peek_token (parser->lexer);
2422 switch (token->type)
2425 case CPP_PRAGMA_EOL:
2426 /* If we've run out of tokens, then there is no closing `)'. */
2430 /* This matches the processing in skip_to_end_of_statement. */
2435 case CPP_OPEN_BRACE:
2438 case CPP_CLOSE_BRACE:
2444 if (recovering && or_comma && !brace_depth && !paren_depth)
2448 case CPP_OPEN_PAREN:
2453 case CPP_CLOSE_PAREN:
2454 if (!brace_depth && !paren_depth--)
2457 cp_lexer_consume_token (parser->lexer);
2466 /* Consume the token. */
2467 cp_lexer_consume_token (parser->lexer);
2471 /* Consume tokens until we reach the end of the current statement.
2472 Normally, that will be just before consuming a `;'. However, if a
2473 non-nested `}' comes first, then we stop before consuming that. */
2476 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2478 unsigned nesting_depth = 0;
2482 cp_token *token = cp_lexer_peek_token (parser->lexer);
2484 switch (token->type)
2487 case CPP_PRAGMA_EOL:
2488 /* If we've run out of tokens, stop. */
2492 /* If the next token is a `;', we have reached the end of the
2498 case CPP_CLOSE_BRACE:
2499 /* If this is a non-nested '}', stop before consuming it.
2500 That way, when confronted with something like:
2504 we stop before consuming the closing '}', even though we
2505 have not yet reached a `;'. */
2506 if (nesting_depth == 0)
2509 /* If it is the closing '}' for a block that we have
2510 scanned, stop -- but only after consuming the token.
2516 we will stop after the body of the erroneously declared
2517 function, but before consuming the following `typedef'
2519 if (--nesting_depth == 0)
2521 cp_lexer_consume_token (parser->lexer);
2525 case CPP_OPEN_BRACE:
2533 /* Consume the token. */
2534 cp_lexer_consume_token (parser->lexer);
2538 /* This function is called at the end of a statement or declaration.
2539 If the next token is a semicolon, it is consumed; otherwise, error
2540 recovery is attempted. */
2543 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2545 /* Look for the trailing `;'. */
2546 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2548 /* If there is additional (erroneous) input, skip to the end of
2550 cp_parser_skip_to_end_of_statement (parser);
2551 /* If the next token is now a `;', consume it. */
2552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2553 cp_lexer_consume_token (parser->lexer);
2557 /* Skip tokens until we have consumed an entire block, or until we
2558 have consumed a non-nested `;'. */
2561 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2563 int nesting_depth = 0;
2565 while (nesting_depth >= 0)
2567 cp_token *token = cp_lexer_peek_token (parser->lexer);
2569 switch (token->type)
2572 case CPP_PRAGMA_EOL:
2573 /* If we've run out of tokens, stop. */
2577 /* Stop if this is an unnested ';'. */
2582 case CPP_CLOSE_BRACE:
2583 /* Stop if this is an unnested '}', or closes the outermost
2590 case CPP_OPEN_BRACE:
2599 /* Consume the token. */
2600 cp_lexer_consume_token (parser->lexer);
2604 /* Skip tokens until a non-nested closing curly brace is the next
2605 token, or there are no more tokens. Return true in the first case,
2609 cp_parser_skip_to_closing_brace (cp_parser *parser)
2611 unsigned nesting_depth = 0;
2615 cp_token *token = cp_lexer_peek_token (parser->lexer);
2617 switch (token->type)
2620 case CPP_PRAGMA_EOL:
2621 /* If we've run out of tokens, stop. */
2624 case CPP_CLOSE_BRACE:
2625 /* If the next token is a non-nested `}', then we have reached
2626 the end of the current block. */
2627 if (nesting_depth-- == 0)
2631 case CPP_OPEN_BRACE:
2632 /* If it the next token is a `{', then we are entering a new
2633 block. Consume the entire block. */
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser->lexer);
2646 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2647 parameter is the PRAGMA token, allowing us to purge the entire pragma
2651 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2655 parser->lexer->in_pragma = false;
2658 token = cp_lexer_consume_token (parser->lexer);
2659 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2661 /* Ensure that the pragma is not parsed again. */
2662 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 /* Require pragma end of line, resyncing with it as necessary. The
2666 arguments are as for cp_parser_skip_to_pragma_eol. */
2669 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2671 parser->lexer->in_pragma = false;
2672 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 /* This is a simple wrapper around make_typename_type. When the id is
2677 an unresolved identifier node, we can provide a superior diagnostic
2678 using cp_parser_diagnose_invalid_type_name. */
2681 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 if (TREE_CODE (id) == IDENTIFIER_NODE)
2686 result = make_typename_type (scope, id, typename_type,
2687 /*complain=*/tf_none);
2688 if (result == error_mark_node)
2689 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692 return make_typename_type (scope, id, typename_type, tf_error);
2695 /* This is a wrapper around the
2696 make_{pointer,ptrmem,reference}_declarator functions that decides
2697 which one to call based on the CODE and CLASS_TYPE arguments. The
2698 CODE argument should be one of the values returned by
2699 cp_parser_ptr_operator. */
2700 static cp_declarator *
2701 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2702 cp_cv_quals cv_qualifiers,
2703 cp_declarator *target)
2705 if (code == ERROR_MARK)
2706 return cp_error_declarator;
2708 if (code == INDIRECT_REF)
2709 if (class_type == NULL_TREE)
2710 return make_pointer_declarator (cv_qualifiers, target);
2712 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2713 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2714 return make_reference_declarator (cv_qualifiers, target, false);
2715 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, true);
2720 /* Create a new C++ parser. */
2723 cp_parser_new (void)
2729 /* cp_lexer_new_main is called before calling ggc_alloc because
2730 cp_lexer_new_main might load a PCH file. */
2731 lexer = cp_lexer_new_main ();
2733 /* Initialize the binops_by_token so that we can get the tree
2734 directly from the token. */
2735 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2736 binops_by_token[binops[i].token_type] = binops[i];
2738 parser = GGC_CNEW (cp_parser);
2739 parser->lexer = lexer;
2740 parser->context = cp_parser_context_new (NULL);
2742 /* For now, we always accept GNU extensions. */
2743 parser->allow_gnu_extensions_p = 1;
2745 /* The `>' token is a greater-than operator, not the end of a
2747 parser->greater_than_is_operator_p = true;
2749 parser->default_arg_ok_p = true;
2751 /* We are not parsing a constant-expression. */
2752 parser->integral_constant_expression_p = false;
2753 parser->allow_non_integral_constant_expression_p = false;
2754 parser->non_integral_constant_expression_p = false;
2756 /* Local variable names are not forbidden. */
2757 parser->local_variables_forbidden_p = false;
2759 /* We are not processing an `extern "C"' declaration. */
2760 parser->in_unbraced_linkage_specification_p = false;
2762 /* We are not processing a declarator. */
2763 parser->in_declarator_p = false;
2765 /* We are not processing a template-argument-list. */
2766 parser->in_template_argument_list_p = false;
2768 /* We are not in an iteration statement. */
2769 parser->in_statement = 0;
2771 /* We are not in a switch statement. */
2772 parser->in_switch_statement_p = false;
2774 /* We are not parsing a type-id inside an expression. */
2775 parser->in_type_id_in_expr_p = false;
2777 /* Declarations aren't implicitly extern "C". */
2778 parser->implicit_extern_c = false;
2780 /* String literals should be translated to the execution character set. */
2781 parser->translate_strings_p = true;
2783 /* We are not parsing a function body. */
2784 parser->in_function_body = false;
2786 /* The unparsed function queue is empty. */
2787 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2789 /* There are no classes being defined. */
2790 parser->num_classes_being_defined = 0;
2792 /* No template parameters apply. */
2793 parser->num_template_parameter_lists = 0;
2798 /* Create a cp_lexer structure which will emit the tokens in CACHE
2799 and push it onto the parser's lexer stack. This is used for delayed
2800 parsing of in-class method bodies and default arguments, and should
2801 not be confused with tentative parsing. */
2803 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2805 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2806 lexer->next = parser->lexer;
2807 parser->lexer = lexer;
2809 /* Move the current source position to that of the first token in the
2811 cp_lexer_set_source_position_from_token (lexer->next_token);
2814 /* Pop the top lexer off the parser stack. This is never used for the
2815 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2817 cp_parser_pop_lexer (cp_parser *parser)
2819 cp_lexer *lexer = parser->lexer;
2820 parser->lexer = lexer->next;
2821 cp_lexer_destroy (lexer);
2823 /* Put the current source position back where it was before this
2824 lexer was pushed. */
2825 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 /* Lexical conventions [gram.lex] */
2830 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2834 cp_parser_identifier (cp_parser* parser)
2838 /* Look for the identifier. */
2839 token = cp_parser_require (parser, CPP_NAME, "identifier");
2840 /* Return the value. */
2841 return token ? token->u.value : error_mark_node;
2844 /* Parse a sequence of adjacent string constants. Returns a
2845 TREE_STRING representing the combined, nul-terminated string
2846 constant. If TRANSLATE is true, translate the string to the
2847 execution character set. If WIDE_OK is true, a wide string is
2850 C++98 [lex.string] says that if a narrow string literal token is
2851 adjacent to a wide string literal token, the behavior is undefined.
2852 However, C99 6.4.5p4 says that this results in a wide string literal.
2853 We follow C99 here, for consistency with the C front end.
2855 This code is largely lifted from lex_string() in c-lex.c.
2857 FUTURE: ObjC++ will need to handle @-strings here. */
2859 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2864 struct obstack str_ob;
2865 cpp_string str, istr, *strs;
2868 tok = cp_lexer_peek_token (parser->lexer);
2869 if (!cp_parser_is_string_literal (tok))
2871 cp_parser_error (parser, "expected string-literal");
2872 return error_mark_node;
2875 /* Try to avoid the overhead of creating and destroying an obstack
2876 for the common case of just one string. */
2877 if (!cp_parser_is_string_literal
2878 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2880 cp_lexer_consume_token (parser->lexer);
2882 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2883 str.len = TREE_STRING_LENGTH (tok->u.value);
2885 if (tok->type == CPP_WSTRING)
2892 gcc_obstack_init (&str_ob);
2897 cp_lexer_consume_token (parser->lexer);
2899 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900 str.len = TREE_STRING_LENGTH (tok->u.value);
2901 if (tok->type == CPP_WSTRING)
2904 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2906 tok = cp_lexer_peek_token (parser->lexer);
2908 while (cp_parser_is_string_literal (tok));
2910 strs = (cpp_string *) obstack_finish (&str_ob);
2913 if (wide && !wide_ok)
2915 cp_parser_error (parser, "a wide string is invalid in this context");
2919 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2920 (parse_in, strs, count, &istr, wide))
2922 value = build_string (istr.len, (const char *)istr.text);
2923 free (CONST_CAST (unsigned char *, istr.text));
2925 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2926 value = fix_string_type (value);
2929 /* cpp_interpret_string has issued an error. */
2930 value = error_mark_node;
2933 obstack_free (&str_ob, 0);
2939 /* Basic concepts [gram.basic] */
2941 /* Parse a translation-unit.
2944 declaration-seq [opt]
2946 Returns TRUE if all went well. */
2949 cp_parser_translation_unit (cp_parser* parser)
2951 /* The address of the first non-permanent object on the declarator
2953 static void *declarator_obstack_base;
2957 /* Create the declarator obstack, if necessary. */
2958 if (!cp_error_declarator)
2960 gcc_obstack_init (&declarator_obstack);
2961 /* Create the error declarator. */
2962 cp_error_declarator = make_declarator (cdk_error);
2963 /* Create the empty parameter list. */
2964 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2965 /* Remember where the base of the declarator obstack lies. */
2966 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969 cp_parser_declaration_seq_opt (parser);
2971 /* If there are no tokens left then all went well. */
2972 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2974 /* Get rid of the token array; we don't need it any more. */
2975 cp_lexer_destroy (parser->lexer);
2976 parser->lexer = NULL;
2978 /* This file might have been a context that's implicitly extern
2979 "C". If so, pop the lang context. (Only relevant for PCH.) */
2980 if (parser->implicit_extern_c)
2982 pop_lang_context ();
2983 parser->implicit_extern_c = false;
2987 finish_translation_unit ();
2993 cp_parser_error (parser, "expected declaration");
2997 /* Make sure the declarator obstack was fully cleaned up. */
2998 gcc_assert (obstack_next_free (&declarator_obstack)
2999 == declarator_obstack_base);
3001 /* All went well. */
3005 /* Expressions [gram.expr] */
3007 /* Parse a primary-expression.
3018 ( compound-statement )
3019 __builtin_va_arg ( assignment-expression , type-id )
3020 __builtin_offsetof ( type-id , offsetof-expression )
3023 __has_nothrow_assign ( type-id )
3024 __has_nothrow_constructor ( type-id )
3025 __has_nothrow_copy ( type-id )
3026 __has_trivial_assign ( type-id )
3027 __has_trivial_constructor ( type-id )
3028 __has_trivial_copy ( type-id )
3029 __has_trivial_destructor ( type-id )
3030 __has_virtual_destructor ( type-id )
3031 __is_abstract ( type-id )
3032 __is_base_of ( type-id , type-id )
3033 __is_class ( type-id )
3034 __is_convertible_to ( type-id , type-id )
3035 __is_empty ( type-id )
3036 __is_enum ( type-id )
3037 __is_pod ( type-id )
3038 __is_polymorphic ( type-id )
3039 __is_union ( type-id )
3041 Objective-C++ Extension:
3049 ADDRESS_P is true iff this expression was immediately preceded by
3050 "&" and therefore might denote a pointer-to-member. CAST_P is true
3051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3052 true iff this expression is a template argument.
3054 Returns a representation of the expression. Upon return, *IDK
3055 indicates what kind of id-expression (if any) was present. */
3058 cp_parser_primary_expression (cp_parser *parser,
3061 bool template_arg_p,
3066 /* Assume the primary expression is not an id-expression. */
3067 *idk = CP_ID_KIND_NONE;
3069 /* Peek at the next token. */
3070 token = cp_lexer_peek_token (parser->lexer);
3071 switch (token->type)
3082 token = cp_lexer_consume_token (parser->lexer);
3083 /* Floating-point literals are only allowed in an integral
3084 constant expression if they are cast to an integral or
3085 enumeration type. */
3086 if (TREE_CODE (token->u.value) == REAL_CST
3087 && parser->integral_constant_expression_p
3090 /* CAST_P will be set even in invalid code like "int(2.7 +
3091 ...)". Therefore, we have to check that the next token
3092 is sure to end the cast. */
3095 cp_token *next_token;
3097 next_token = cp_lexer_peek_token (parser->lexer);
3098 if (/* The comma at the end of an
3099 enumerator-definition. */
3100 next_token->type != CPP_COMMA
3101 /* The curly brace at the end of an enum-specifier. */
3102 && next_token->type != CPP_CLOSE_BRACE
3103 /* The end of a statement. */
3104 && next_token->type != CPP_SEMICOLON
3105 /* The end of the cast-expression. */
3106 && next_token->type != CPP_CLOSE_PAREN
3107 /* The end of an array bound. */
3108 && next_token->type != CPP_CLOSE_SQUARE
3109 /* The closing ">" in a template-argument-list. */
3110 && (next_token->type != CPP_GREATER
3111 || parser->greater_than_is_operator_p)
3112 /* C++0x only: A ">>" treated like two ">" tokens,
3113 in a template-argument-list. */
3114 && (next_token->type != CPP_RSHIFT
3115 || (cxx_dialect == cxx98)
3116 || parser->greater_than_is_operator_p))
3120 /* If we are within a cast, then the constraint that the
3121 cast is to an integral or enumeration type will be
3122 checked at that point. If we are not within a cast, then
3123 this code is invalid. */
3125 cp_parser_non_integral_constant_expression
3126 (parser, "floating-point literal");
3128 return token->u.value;
3132 /* ??? Should wide strings be allowed when parser->translate_strings_p
3133 is false (i.e. in attributes)? If not, we can kill the third
3134 argument to cp_parser_string_literal. */
3135 return cp_parser_string_literal (parser,
3136 parser->translate_strings_p,
3139 case CPP_OPEN_PAREN:
3142 bool saved_greater_than_is_operator_p;
3144 /* Consume the `('. */
3145 cp_lexer_consume_token (parser->lexer);
3146 /* Within a parenthesized expression, a `>' token is always
3147 the greater-than operator. */
3148 saved_greater_than_is_operator_p
3149 = parser->greater_than_is_operator_p;
3150 parser->greater_than_is_operator_p = true;
3151 /* If we see `( { ' then we are looking at the beginning of
3152 a GNU statement-expression. */
3153 if (cp_parser_allow_gnu_extensions_p (parser)
3154 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3156 /* Statement-expressions are not allowed by the standard. */
3158 pedwarn ("ISO C++ forbids braced-groups within expressions");
3160 /* And they're not allowed outside of a function-body; you
3161 cannot, for example, write:
3163 int i = ({ int j = 3; j + 1; });
3165 at class or namespace scope. */
3166 if (!parser->in_function_body
3167 || parser->in_template_argument_list_p)
3169 error ("statement-expressions are not allowed outside "
3170 "functions nor in template-argument lists");
3171 cp_parser_skip_to_end_of_block_or_statement (parser);
3172 expr = error_mark_node;
3176 /* Start the statement-expression. */
3177 expr = begin_stmt_expr ();
3178 /* Parse the compound-statement. */
3179 cp_parser_compound_statement (parser, expr, false);
3181 expr = finish_stmt_expr (expr, false);
3186 /* Parse the parenthesized expression. */
3187 expr = cp_parser_expression (parser, cast_p);
3188 /* Let the front end know that this expression was
3189 enclosed in parentheses. This matters in case, for
3190 example, the expression is of the form `A::B', since
3191 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3193 finish_parenthesized_expr (expr);
3195 /* The `>' token might be the end of a template-id or
3196 template-parameter-list now. */
3197 parser->greater_than_is_operator_p
3198 = saved_greater_than_is_operator_p;
3199 /* Consume the `)'. */
3200 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3201 cp_parser_skip_to_end_of_statement (parser);
3207 switch (token->keyword)
3209 /* These two are the boolean literals. */
3211 cp_lexer_consume_token (parser->lexer);
3212 return boolean_true_node;
3214 cp_lexer_consume_token (parser->lexer);
3215 return boolean_false_node;
3217 /* The `__null' literal. */
3219 cp_lexer_consume_token (parser->lexer);
3222 /* Recognize the `this' keyword. */
3224 cp_lexer_consume_token (parser->lexer);
3225 if (parser->local_variables_forbidden_p)
3227 error ("%<this%> may not be used in this context");
3228 return error_mark_node;
3230 /* Pointers cannot appear in constant-expressions. */
3231 if (cp_parser_non_integral_constant_expression (parser,
3233 return error_mark_node;
3234 return finish_this_expr ();
3236 /* The `operator' keyword can be the beginning of an
3241 case RID_FUNCTION_NAME:
3242 case RID_PRETTY_FUNCTION_NAME:
3243 case RID_C99_FUNCTION_NAME:
3244 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3245 __func__ are the names of variables -- but they are
3246 treated specially. Therefore, they are handled here,
3247 rather than relying on the generic id-expression logic
3248 below. Grammatically, these names are id-expressions.
3250 Consume the token. */
3251 token = cp_lexer_consume_token (parser->lexer);
3252 /* Look up the name. */
3253 return finish_fname (token->u.value);
3260 /* The `__builtin_va_arg' construct is used to handle
3261 `va_arg'. Consume the `__builtin_va_arg' token. */
3262 cp_lexer_consume_token (parser->lexer);
3263 /* Look for the opening `('. */
3264 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3265 /* Now, parse the assignment-expression. */
3266 expression = cp_parser_assignment_expression (parser,
3268 /* Look for the `,'. */
3269 cp_parser_require (parser, CPP_COMMA, "`,'");
3270 /* Parse the type-id. */
3271 type = cp_parser_type_id (parser);
3272 /* Look for the closing `)'. */
3273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3274 /* Using `va_arg' in a constant-expression is not
3276 if (cp_parser_non_integral_constant_expression (parser,
3278 return error_mark_node;
3279 return build_x_va_arg (expression, type);
3283 return cp_parser_builtin_offsetof (parser);
3285 case RID_HAS_NOTHROW_ASSIGN:
3286 case RID_HAS_NOTHROW_CONSTRUCTOR:
3287 case RID_HAS_NOTHROW_COPY:
3288 case RID_HAS_TRIVIAL_ASSIGN:
3289 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3290 case RID_HAS_TRIVIAL_COPY:
3291 case RID_HAS_TRIVIAL_DESTRUCTOR:
3292 case RID_HAS_VIRTUAL_DESTRUCTOR:
3293 case RID_IS_ABSTRACT:
3294 case RID_IS_BASE_OF:
3296 case RID_IS_CONVERTIBLE_TO:
3300 case RID_IS_POLYMORPHIC:
3302 return cp_parser_trait_expr (parser, token->keyword);
3304 /* Objective-C++ expressions. */
3306 case RID_AT_PROTOCOL:
3307 case RID_AT_SELECTOR:
3308 return cp_parser_objc_expression (parser);
3311 cp_parser_error (parser, "expected primary-expression");
3312 return error_mark_node;
3315 /* An id-expression can start with either an identifier, a
3316 `::' as the beginning of a qualified-id, or the "operator"
3320 case CPP_TEMPLATE_ID:
3321 case CPP_NESTED_NAME_SPECIFIER:
3325 const char *error_msg;
3330 /* Parse the id-expression. */
3332 = cp_parser_id_expression (parser,
3333 /*template_keyword_p=*/false,
3334 /*check_dependency_p=*/true,
3336 /*declarator_p=*/false,
3337 /*optional_p=*/false);
3338 if (id_expression == error_mark_node)
3339 return error_mark_node;
3340 token = cp_lexer_peek_token (parser->lexer);
3341 done = (token->type != CPP_OPEN_SQUARE
3342 && token->type != CPP_OPEN_PAREN
3343 && token->type != CPP_DOT
3344 && token->type != CPP_DEREF
3345 && token->type != CPP_PLUS_PLUS
3346 && token->type != CPP_MINUS_MINUS);
3347 /* If we have a template-id, then no further lookup is
3348 required. If the template-id was for a template-class, we
3349 will sometimes have a TYPE_DECL at this point. */
3350 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3351 || TREE_CODE (id_expression) == TYPE_DECL)
3352 decl = id_expression;
3353 /* Look up the name. */
3356 tree ambiguous_decls;
3358 decl = cp_parser_lookup_name (parser, id_expression,
3361 /*is_namespace=*/false,
3362 /*check_dependency=*/true,
3364 /* If the lookup was ambiguous, an error will already have
3366 if (ambiguous_decls)
3367 return error_mark_node;
3369 /* In Objective-C++, an instance variable (ivar) may be preferred
3370 to whatever cp_parser_lookup_name() found. */
3371 decl = objc_lookup_ivar (decl, id_expression);
3373 /* If name lookup gives us a SCOPE_REF, then the
3374 qualifying scope was dependent. */
3375 if (TREE_CODE (decl) == SCOPE_REF)
3377 /* At this point, we do not know if DECL is a valid
3378 integral constant expression. We assume that it is
3379 in fact such an expression, so that code like:
3381 template <int N> struct A {
3385 is accepted. At template-instantiation time, we
3386 will check that B<N>::i is actually a constant. */
3389 /* Check to see if DECL is a local variable in a context
3390 where that is forbidden. */
3391 if (parser->local_variables_forbidden_p
3392 && local_variable_p (decl))
3394 /* It might be that we only found DECL because we are
3395 trying to be generous with pre-ISO scoping rules.
3396 For example, consider:
3400 for (int i = 0; i < 10; ++i) {}
3401 extern void f(int j = i);
3404 Here, name look up will originally find the out
3405 of scope `i'. We need to issue a warning message,
3406 but then use the global `i'. */
3407 decl = check_for_out_of_scope_variable (decl);
3408 if (local_variable_p (decl))
3410 error ("local variable %qD may not appear in this context",
3412 return error_mark_node;
3417 decl = (finish_id_expression
3418 (id_expression, decl, parser->scope,
3420 parser->integral_constant_expression_p,
3421 parser->allow_non_integral_constant_expression_p,
3422 &parser->non_integral_constant_expression_p,
3423 template_p, done, address_p,
3427 cp_parser_error (parser, error_msg);
3431 /* Anything else is an error. */
3433 /* ...unless we have an Objective-C++ message or string literal,
3435 if (c_dialect_objc ()
3436 && (token->type == CPP_OPEN_SQUARE
3437 || token->type == CPP_OBJC_STRING))
3438 return cp_parser_objc_expression (parser);
3440 cp_parser_error (parser, "expected primary-expression");
3441 return error_mark_node;
3445 /* Parse an id-expression.
3452 :: [opt] nested-name-specifier template [opt] unqualified-id
3454 :: operator-function-id
3457 Return a representation of the unqualified portion of the
3458 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3459 a `::' or nested-name-specifier.
3461 Often, if the id-expression was a qualified-id, the caller will
3462 want to make a SCOPE_REF to represent the qualified-id. This
3463 function does not do this in order to avoid wastefully creating
3464 SCOPE_REFs when they are not required.
3466 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3470 uninstantiated templates.
3472 If *TEMPLATE_P is non-NULL, it is set to true iff the
3473 `template' keyword is used to explicitly indicate that the entity
3474 named is a template.
3476 If DECLARATOR_P is true, the id-expression is appearing as part of
3477 a declarator, rather than as part of an expression. */
3480 cp_parser_id_expression (cp_parser *parser,
3481 bool template_keyword_p,
3482 bool check_dependency_p,
3487 bool global_scope_p;
3488 bool nested_name_specifier_p;
3490 /* Assume the `template' keyword was not used. */
3492 *template_p = template_keyword_p;
3494 /* Look for the optional `::' operator. */
3496 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3498 /* Look for the optional nested-name-specifier. */
3499 nested_name_specifier_p
3500 = (cp_parser_nested_name_specifier_opt (parser,
3501 /*typename_keyword_p=*/false,
3506 /* If there is a nested-name-specifier, then we are looking at
3507 the first qualified-id production. */
3508 if (nested_name_specifier_p)
3511 tree saved_object_scope;
3512 tree saved_qualifying_scope;
3513 tree unqualified_id;
3516 /* See if the next token is the `template' keyword. */
3518 template_p = &is_template;
3519 *template_p = cp_parser_optional_template_keyword (parser);
3520 /* Name lookup we do during the processing of the
3521 unqualified-id might obliterate SCOPE. */
3522 saved_scope = parser->scope;
3523 saved_object_scope = parser->object_scope;
3524 saved_qualifying_scope = parser->qualifying_scope;
3525 /* Process the final unqualified-id. */
3526 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3529 /*optional_p=*/false);
3530 /* Restore the SAVED_SCOPE for our caller. */
3531 parser->scope = saved_scope;
3532 parser->object_scope = saved_object_scope;
3533 parser->qualifying_scope = saved_qualifying_scope;
3535 return unqualified_id;
3537 /* Otherwise, if we are in global scope, then we are looking at one
3538 of the other qualified-id productions. */
3539 else if (global_scope_p)
3544 /* Peek at the next token. */
3545 token = cp_lexer_peek_token (parser->lexer);
3547 /* If it's an identifier, and the next token is not a "<", then
3548 we can avoid the template-id case. This is an optimization
3549 for this common case. */
3550 if (token->type == CPP_NAME
3551 && !cp_parser_nth_token_starts_template_argument_list_p
3553 return cp_parser_identifier (parser);
3555 cp_parser_parse_tentatively (parser);
3556 /* Try a template-id. */
3557 id = cp_parser_template_id (parser,
3558 /*template_keyword_p=*/false,
3559 /*check_dependency_p=*/true,
3561 /* If that worked, we're done. */
3562 if (cp_parser_parse_definitely (parser))
3565 /* Peek at the next token. (Changes in the token buffer may
3566 have invalidated the pointer obtained above.) */
3567 token = cp_lexer_peek_token (parser->lexer);
3569 switch (token->type)
3572 return cp_parser_identifier (parser);
3575 if (token->keyword == RID_OPERATOR)
3576 return cp_parser_operator_function_id (parser);
3580 cp_parser_error (parser, "expected id-expression");
3581 return error_mark_node;
3585 return cp_parser_unqualified_id (parser, template_keyword_p,
3586 /*check_dependency_p=*/true,
3591 /* Parse an unqualified-id.
3595 operator-function-id
3596 conversion-function-id
3600 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3601 keyword, in a construct like `A::template ...'.
3603 Returns a representation of unqualified-id. For the `identifier'
3604 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3605 production a BIT_NOT_EXPR is returned; the operand of the
3606 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3607 other productions, see the documentation accompanying the
3608 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3609 names are looked up in uninstantiated templates. If DECLARATOR_P
3610 is true, the unqualified-id is appearing as part of a declarator,
3611 rather than as part of an expression. */
3614 cp_parser_unqualified_id (cp_parser* parser,
3615 bool template_keyword_p,
3616 bool check_dependency_p,
3622 /* Peek at the next token. */
3623 token = cp_lexer_peek_token (parser->lexer);
3625 switch (token->type)
3631 /* We don't know yet whether or not this will be a
3633 cp_parser_parse_tentatively (parser);
3634 /* Try a template-id. */
3635 id = cp_parser_template_id (parser, template_keyword_p,
3638 /* If it worked, we're done. */
3639 if (cp_parser_parse_definitely (parser))
3641 /* Otherwise, it's an ordinary identifier. */
3642 return cp_parser_identifier (parser);
3645 case CPP_TEMPLATE_ID:
3646 return cp_parser_template_id (parser, template_keyword_p,
3653 tree qualifying_scope;
3658 /* Consume the `~' token. */
3659 cp_lexer_consume_token (parser->lexer);
3660 /* Parse the class-name. The standard, as written, seems to
3663 template <typename T> struct S { ~S (); };
3664 template <typename T> S<T>::~S() {}
3666 is invalid, since `~' must be followed by a class-name, but
3667 `S<T>' is dependent, and so not known to be a class.
3668 That's not right; we need to look in uninstantiated
3669 templates. A further complication arises from:
3671 template <typename T> void f(T t) {
3675 Here, it is not possible to look up `T' in the scope of `T'
3676 itself. We must look in both the current scope, and the
3677 scope of the containing complete expression.
3679 Yet another issue is:
3688 The standard does not seem to say that the `S' in `~S'
3689 should refer to the type `S' and not the data member
3692 /* DR 244 says that we look up the name after the "~" in the
3693 same scope as we looked up the qualifying name. That idea
3694 isn't fully worked out; it's more complicated than that. */
3695 scope = parser->scope;
3696 object_scope = parser->object_scope;
3697 qualifying_scope = parser->qualifying_scope;
3699 /* Check for invalid scopes. */
3700 if (scope == error_mark_node)
3702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3703 cp_lexer_consume_token (parser->lexer);
3704 return error_mark_node;
3706 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3709 error ("scope %qT before %<~%> is not a class-name", scope);
3710 cp_parser_simulate_error (parser);
3711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3712 cp_lexer_consume_token (parser->lexer);
3713 return error_mark_node;
3715 gcc_assert (!scope || TYPE_P (scope));
3717 /* If the name is of the form "X::~X" it's OK. */
3718 token = cp_lexer_peek_token (parser->lexer);
3720 && token->type == CPP_NAME
3721 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3723 && constructor_name_p (token->u.value, scope))
3725 cp_lexer_consume_token (parser->lexer);
3726 return build_nt (BIT_NOT_EXPR, scope);
3729 /* If there was an explicit qualification (S::~T), first look
3730 in the scope given by the qualification (i.e., S). */
3732 type_decl = NULL_TREE;
3735 cp_parser_parse_tentatively (parser);
3736 type_decl = cp_parser_class_name (parser,
3737 /*typename_keyword_p=*/false,
3738 /*template_keyword_p=*/false,
3740 /*check_dependency=*/false,
3741 /*class_head_p=*/false,
3743 if (cp_parser_parse_definitely (parser))
3746 /* In "N::S::~S", look in "N" as well. */
3747 if (!done && scope && qualifying_scope)
3749 cp_parser_parse_tentatively (parser);
3750 parser->scope = qualifying_scope;
3751 parser->object_scope = NULL_TREE;
3752 parser->qualifying_scope = NULL_TREE;
3754 = cp_parser_class_name (parser,
3755 /*typename_keyword_p=*/false,
3756 /*template_keyword_p=*/false,
3758 /*check_dependency=*/false,
3759 /*class_head_p=*/false,
3761 if (cp_parser_parse_definitely (parser))
3764 /* In "p->S::~T", look in the scope given by "*p" as well. */
3765 else if (!done && object_scope)
3767 cp_parser_parse_tentatively (parser);
3768 parser->scope = object_scope;
3769 parser->object_scope = NULL_TREE;
3770 parser->qualifying_scope = NULL_TREE;
3772 = cp_parser_class_name (parser,
3773 /*typename_keyword_p=*/false,
3774 /*template_keyword_p=*/false,
3776 /*check_dependency=*/false,
3777 /*class_head_p=*/false,
3779 if (cp_parser_parse_definitely (parser))
3782 /* Look in the surrounding context. */
3785 parser->scope = NULL_TREE;
3786 parser->object_scope = NULL_TREE;
3787 parser->qualifying_scope = NULL_TREE;
3789 = cp_parser_class_name (parser,
3790 /*typename_keyword_p=*/false,
3791 /*template_keyword_p=*/false,
3793 /*check_dependency=*/false,
3794 /*class_head_p=*/false,
3797 /* If an error occurred, assume that the name of the
3798 destructor is the same as the name of the qualifying
3799 class. That allows us to keep parsing after running
3800 into ill-formed destructor names. */
3801 if (type_decl == error_mark_node && scope)
3802 return build_nt (BIT_NOT_EXPR, scope);
3803 else if (type_decl == error_mark_node)
3804 return error_mark_node;
3806 /* Check that destructor name and scope match. */
3807 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3810 error ("declaration of %<~%T%> as member of %qT",
3812 cp_parser_simulate_error (parser);
3813 return error_mark_node;
3818 A typedef-name that names a class shall not be used as the
3819 identifier in the declarator for a destructor declaration. */
3821 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3822 && !DECL_SELF_REFERENCE_P (type_decl)
3823 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3824 error ("typedef-name %qD used as destructor declarator",
3827 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3831 if (token->keyword == RID_OPERATOR)
3835 /* This could be a template-id, so we try that first. */
3836 cp_parser_parse_tentatively (parser);
3837 /* Try a template-id. */
3838 id = cp_parser_template_id (parser, template_keyword_p,
3839 /*check_dependency_p=*/true,
3841 /* If that worked, we're done. */
3842 if (cp_parser_parse_definitely (parser))
3844 /* We still don't know whether we're looking at an
3845 operator-function-id or a conversion-function-id. */
3846 cp_parser_parse_tentatively (parser);
3847 /* Try an operator-function-id. */
3848 id = cp_parser_operator_function_id (parser);
3849 /* If that didn't work, try a conversion-function-id. */
3850 if (!cp_parser_parse_definitely (parser))
3851 id = cp_parser_conversion_function_id (parser);
3860 cp_parser_error (parser, "expected unqualified-id");
3861 return error_mark_node;
3865 /* Parse an (optional) nested-name-specifier.
3867 nested-name-specifier:
3868 class-or-namespace-name :: nested-name-specifier [opt]
3869 class-or-namespace-name :: template nested-name-specifier [opt]
3871 PARSER->SCOPE should be set appropriately before this function is
3872 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3873 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3876 Sets PARSER->SCOPE to the class (TYPE) or namespace
3877 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3878 it unchanged if there is no nested-name-specifier. Returns the new
3879 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3881 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3882 part of a declaration and/or decl-specifier. */
3885 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3886 bool typename_keyword_p,
3887 bool check_dependency_p,
3889 bool is_declaration)
3891 bool success = false;
3892 cp_token_position start = 0;
3895 /* Remember where the nested-name-specifier starts. */
3896 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3898 start = cp_lexer_token_position (parser->lexer, false);
3899 push_deferring_access_checks (dk_deferred);
3906 tree saved_qualifying_scope;
3907 bool template_keyword_p;
3909 /* Spot cases that cannot be the beginning of a
3910 nested-name-specifier. */
3911 token = cp_lexer_peek_token (parser->lexer);
3913 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3914 the already parsed nested-name-specifier. */
3915 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3917 /* Grab the nested-name-specifier and continue the loop. */
3918 cp_parser_pre_parsed_nested_name_specifier (parser);
3919 /* If we originally encountered this nested-name-specifier
3920 with IS_DECLARATION set to false, we will not have
3921 resolved TYPENAME_TYPEs, so we must do so here. */
3923 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3925 new_scope = resolve_typename_type (parser->scope,
3926 /*only_current_p=*/false);
3927 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3928 parser->scope = new_scope;
3934 /* Spot cases that cannot be the beginning of a
3935 nested-name-specifier. On the second and subsequent times
3936 through the loop, we look for the `template' keyword. */
3937 if (success && token->keyword == RID_TEMPLATE)
3939 /* A template-id can start a nested-name-specifier. */
3940 else if (token->type == CPP_TEMPLATE_ID)
3944 /* If the next token is not an identifier, then it is
3945 definitely not a class-or-namespace-name. */
3946 if (token->type != CPP_NAME)
3948 /* If the following token is neither a `<' (to begin a
3949 template-id), nor a `::', then we are not looking at a
3950 nested-name-specifier. */
3951 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3952 if (token->type != CPP_SCOPE
3953 && !cp_parser_nth_token_starts_template_argument_list_p
3958 /* The nested-name-specifier is optional, so we parse
3960 cp_parser_parse_tentatively (parser);
3962 /* Look for the optional `template' keyword, if this isn't the
3963 first time through the loop. */
3965 template_keyword_p = cp_parser_optional_template_keyword (parser);
3967 template_keyword_p = false;
3969 /* Save the old scope since the name lookup we are about to do
3970 might destroy it. */
3971 old_scope = parser->scope;
3972 saved_qualifying_scope = parser->qualifying_scope;
3973 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3974 look up names in "X<T>::I" in order to determine that "Y" is
3975 a template. So, if we have a typename at this point, we make
3976 an effort to look through it. */
3978 && !typename_keyword_p
3980 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3981 parser->scope = resolve_typename_type (parser->scope,
3982 /*only_current_p=*/false);
3983 /* Parse the qualifying entity. */
3985 = cp_parser_class_or_namespace_name (parser,
3991 /* Look for the `::' token. */
3992 cp_parser_require (parser, CPP_SCOPE, "`::'");
3994 /* If we found what we wanted, we keep going; otherwise, we're
3996 if (!cp_parser_parse_definitely (parser))
3998 bool error_p = false;
4000 /* Restore the OLD_SCOPE since it was valid before the
4001 failed attempt at finding the last
4002 class-or-namespace-name. */
4003 parser->scope = old_scope;
4004 parser->qualifying_scope = saved_qualifying_scope;
4005 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4007 /* If the next token is an identifier, and the one after
4008 that is a `::', then any valid interpretation would have
4009 found a class-or-namespace-name. */
4010 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4011 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4013 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4016 token = cp_lexer_consume_token (parser->lexer);
4019 if (!token->ambiguous_p)
4022 tree ambiguous_decls;
4024 decl = cp_parser_lookup_name (parser, token->u.value,
4026 /*is_template=*/false,
4027 /*is_namespace=*/false,
4028 /*check_dependency=*/true,
4030 if (TREE_CODE (decl) == TEMPLATE_DECL)
4031 error ("%qD used without template parameters", decl);
4032 else if (ambiguous_decls)
4034 error ("reference to %qD is ambiguous",
4036 print_candidates (ambiguous_decls);
4037 decl = error_mark_node;
4040 cp_parser_name_lookup_error
4041 (parser, token->u.value, decl,
4042 "is not a class or namespace");
4044 parser->scope = error_mark_node;
4046 /* Treat this as a successful nested-name-specifier
4051 If the name found is not a class-name (clause
4052 _class_) or namespace-name (_namespace.def_), the
4053 program is ill-formed. */
4056 cp_lexer_consume_token (parser->lexer);
4060 /* We've found one valid nested-name-specifier. */
4062 /* Name lookup always gives us a DECL. */
4063 if (TREE_CODE (new_scope) == TYPE_DECL)
4064 new_scope = TREE_TYPE (new_scope);
4065 /* Uses of "template" must be followed by actual templates. */
4066 if (template_keyword_p
4067 && !(CLASS_TYPE_P (new_scope)
4068 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4069 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4070 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4071 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4072 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4073 == TEMPLATE_ID_EXPR)))
4074 pedwarn (TYPE_P (new_scope)
4075 ? "%qT is not a template"
4076 : "%qD is not a template",
4078 /* If it is a class scope, try to complete it; we are about to
4079 be looking up names inside the class. */
4080 if (TYPE_P (new_scope)
4081 /* Since checking types for dependency can be expensive,
4082 avoid doing it if the type is already complete. */
4083 && !COMPLETE_TYPE_P (new_scope)
4084 /* Do not try to complete dependent types. */
4085 && !dependent_type_p (new_scope))
4087 new_scope = complete_type (new_scope);
4088 /* If it is a typedef to current class, use the current
4089 class instead, as the typedef won't have any names inside
4091 if (!COMPLETE_TYPE_P (new_scope)
4092 && currently_open_class (new_scope))
4093 new_scope = TYPE_MAIN_VARIANT (new_scope);
4095 /* Make sure we look in the right scope the next time through
4097 parser->scope = new_scope;
4100 /* If parsing tentatively, replace the sequence of tokens that makes
4101 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4102 token. That way, should we re-parse the token stream, we will
4103 not have to repeat the effort required to do the parse, nor will
4104 we issue duplicate error messages. */
4105 if (success && start)
4109 token = cp_lexer_token_at (parser->lexer, start);
4110 /* Reset the contents of the START token. */
4111 token->type = CPP_NESTED_NAME_SPECIFIER;
4112 /* Retrieve any deferred checks. Do not pop this access checks yet
4113 so the memory will not be reclaimed during token replacing below. */
4114 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4115 token->u.tree_check_value->value = parser->scope;
4116 token->u.tree_check_value->checks = get_deferred_access_checks ();
4117 token->u.tree_check_value->qualifying_scope =
4118 parser->qualifying_scope;
4119 token->keyword = RID_MAX;
4121 /* Purge all subsequent tokens. */
4122 cp_lexer_purge_tokens_after (parser->lexer, start);
4126 pop_to_parent_deferring_access_checks ();
4128 return success ? parser->scope : NULL_TREE;
4131 /* Parse a nested-name-specifier. See
4132 cp_parser_nested_name_specifier_opt for details. This function
4133 behaves identically, except that it will an issue an error if no
4134 nested-name-specifier is present. */
4137 cp_parser_nested_name_specifier (cp_parser *parser,
4138 bool typename_keyword_p,
4139 bool check_dependency_p,
4141 bool is_declaration)
4145 /* Look for the nested-name-specifier. */
4146 scope = cp_parser_nested_name_specifier_opt (parser,
4151 /* If it was not present, issue an error message. */
4154 cp_parser_error (parser, "expected nested-name-specifier");
4155 parser->scope = NULL_TREE;
4161 /* Parse a class-or-namespace-name.
4163 class-or-namespace-name:
4167 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4168 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4169 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4170 TYPE_P is TRUE iff the next name should be taken as a class-name,
4171 even the same name is declared to be another entity in the same