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 /* auto specifier: storage-class-specifier in C++,
543 simple-type-specifier in C++0x. */
545 /* Storage classes. */
551 /* Elaborated type specifiers. */
557 /* Simple type specifiers. */
571 /* GNU extensions. */
574 /* C++0x extensions. */
583 /* Return a pointer to the Nth token in the token stream. If N is 1,
584 then this is precisely equivalent to cp_lexer_peek_token (except
585 that it is not inline). One would like to disallow that case, but
586 there is one case (cp_parser_nth_token_starts_template_id) where
587 the caller passes a variable for N and it might be 1. */
590 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 /* N is 1-based, not zero-based. */
597 if (cp_lexer_debugging_p (lexer))
598 fprintf (cp_lexer_debug_stream,
599 "cp_lexer: peeking ahead %ld at token: ", (long)n);
602 token = lexer->next_token;
603 gcc_assert (!n || token != &eof_token);
607 if (token == lexer->last_token)
613 if (token->type != CPP_PURGED)
617 if (cp_lexer_debugging_p (lexer))
619 cp_lexer_print_token (cp_lexer_debug_stream, token);
620 putc ('\n', cp_lexer_debug_stream);
626 /* Return the next token, and advance the lexer's next_token pointer
627 to point to the next non-purged token. */
630 cp_lexer_consume_token (cp_lexer* lexer)
632 cp_token *token = lexer->next_token;
634 gcc_assert (token != &eof_token);
635 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640 if (lexer->next_token == lexer->last_token)
642 lexer->next_token = &eof_token;
647 while (lexer->next_token->type == CPP_PURGED);
649 cp_lexer_set_source_position_from_token (token);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
655 cp_lexer_print_token (cp_lexer_debug_stream, token);
656 putc ('\n', cp_lexer_debug_stream);
662 /* Permanently remove the next token from the token stream, and
663 advance the next_token pointer to refer to the next non-purged
667 cp_lexer_purge_token (cp_lexer *lexer)
669 cp_token *tok = lexer->next_token;
671 gcc_assert (tok != &eof_token);
672 tok->type = CPP_PURGED;
673 tok->location = UNKNOWN_LOCATION;
674 tok->u.value = NULL_TREE;
675 tok->keyword = RID_MAX;
680 if (tok == lexer->last_token)
686 while (tok->type == CPP_PURGED);
687 lexer->next_token = tok;
690 /* Permanently remove all tokens after TOK, up to, but not
691 including, the token that will be returned next by
692 cp_lexer_peek_token. */
695 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 cp_token *peek = lexer->next_token;
699 if (peek == &eof_token)
700 peek = lexer->last_token;
702 gcc_assert (tok < peek);
704 for ( tok += 1; tok != peek; tok += 1)
706 tok->type = CPP_PURGED;
707 tok->location = UNKNOWN_LOCATION;
708 tok->u.value = NULL_TREE;
709 tok->keyword = RID_MAX;
713 /* Begin saving tokens. All tokens consumed after this point will be
717 cp_lexer_save_tokens (cp_lexer* lexer)
719 /* Provide debugging output. */
720 if (cp_lexer_debugging_p (lexer))
721 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723 VEC_safe_push (cp_token_position, heap,
724 lexer->saved_tokens, lexer->next_token);
727 /* Commit to the portion of the token stream most recently saved. */
730 cp_lexer_commit_tokens (cp_lexer* lexer)
732 /* Provide debugging output. */
733 if (cp_lexer_debugging_p (lexer))
734 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736 VEC_pop (cp_token_position, lexer->saved_tokens);
739 /* Return all tokens saved since the last call to cp_lexer_save_tokens
740 to the token stream. Stop saving tokens. */
743 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 /* Provide debugging output. */
746 if (cp_lexer_debugging_p (lexer))
747 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
752 /* Print a representation of the TOKEN on the STREAM. */
754 #ifdef ENABLE_CHECKING
757 cp_lexer_print_token (FILE * stream, cp_token *token)
759 /* We don't use cpp_type2name here because the parser defines
760 a few tokens of its own. */
761 static const char *const token_names[] = {
762 /* cpplib-defined token types */
768 /* C++ parser token types - see "Manifest constants", above. */
771 "NESTED_NAME_SPECIFIER",
775 /* If we have a name for the token, print it out. Otherwise, we
776 simply give the numeric code. */
777 gcc_assert (token->type < ARRAY_SIZE(token_names));
778 fputs (token_names[token->type], stream);
780 /* For some tokens, print the associated data. */
784 /* Some keywords have a value that is not an IDENTIFIER_NODE.
785 For example, `struct' is mapped to an INTEGER_CST. */
786 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
788 /* else fall through */
790 fputs (IDENTIFIER_POINTER (token->u.value), stream);
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
805 /* Start emitting debugging information. */
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
953 declarator->parameter_pack_p = false;
958 /* Like make_pointer_declarator -- but for references. */
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
976 declarator->parameter_pack_p = false;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1001 declarator->parameter_pack_p = false;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1012 make_call_declarator (cp_declarator *target,
1013 cp_parameter_declarator *parms,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1030 declarator->parameter_pack_p = false;
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1039 make_array_declarator (cp_declarator *element, tree bounds)
1041 cp_declarator *declarator;
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1052 declarator->parameter_pack_p = false;
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1067 while (declarator && !found)
1069 switch ((int)declarator->kind)
1080 declarator = declarator->declarator;
1088 cp_parameter_declarator *no_parameters;
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1098 cp_parameter_declarator *parameter;
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1106 clear_decl_specs (¶meter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1117 function_declarator_p (const cp_declarator *declarator)
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1127 declarator = declarator->declarator;
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1190 typedef enum cp_parser_flags
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 /* The different kinds of declarators we want to parse. */
1203 typedef enum cp_parser_declarator_kind
1205 /* We want an abstract declarator. */
1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
1209 /* We don't mind, but the name must be an unqualified-id. */
1210 CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1213 /* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
1225 PREC_EQUALITY_EXPRESSION,
1226 PREC_RELATIONAL_EXPRESSION,
1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
1237 typedef struct cp_parser_binary_operations_map_node
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1247 /* The status of a tentative parse. */
1249 typedef enum cp_parser_status_kind
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1260 typedef struct cp_parser_expression_stack_entry
1262 /* Left hand side of the binary operation we are currently
1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
1269 enum tree_code tree_type;
1270 /* Precedence of the binary operation we are parsing. */
1272 } cp_parser_expression_stack_entry;
1274 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1277 typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
1280 /* Context that is saved and restored when parsing tentatively. */
1281 typedef struct cp_parser_context GTY (())
1283 /* If this is a tentative parsing context, the status of the
1285 enum cp_parser_status_kind status;
1286 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1287 that are looked up in this context must be looked up both in the
1288 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1289 the context of the containing expression. */
1292 /* The next parsing context in the stack. */
1293 struct cp_parser_context *next;
1294 } cp_parser_context;
1298 /* Constructors and destructors. */
1300 static cp_parser_context *cp_parser_context_new
1301 (cp_parser_context *);
1303 /* Class variables. */
1305 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1307 /* The operator-precedence table used by cp_parser_binary_expression.
1308 Transformed into an associative array (binops_by_token) by
1311 static const cp_parser_binary_operations_map_node binops[] = {
1312 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1313 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1315 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1331 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1333 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1335 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1337 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1339 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1341 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1344 /* The same as binops, but initialized by cp_parser_new so that
1345 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1347 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1349 /* Constructors and destructors. */
1351 /* Construct a new context. The context below this one on the stack
1352 is given by NEXT. */
1354 static cp_parser_context *
1355 cp_parser_context_new (cp_parser_context* next)
1357 cp_parser_context *context;
1359 /* Allocate the storage. */
1360 if (cp_parser_context_free_list != NULL)
1362 /* Pull the first entry from the free list. */
1363 context = cp_parser_context_free_list;
1364 cp_parser_context_free_list = context->next;
1365 memset (context, 0, sizeof (*context));
1368 context = GGC_CNEW (cp_parser_context);
1370 /* No errors have occurred yet in this context. */
1371 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1372 /* If this is not the bottomost context, copy information that we
1373 need from the previous context. */
1376 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1377 expression, then we are parsing one in this context, too. */
1378 context->object_type = next->object_type;
1379 /* Thread the stack. */
1380 context->next = next;
1386 /* The cp_parser structure represents the C++ parser. */
1388 typedef struct cp_parser GTY(())
1390 /* The lexer from which we are obtaining tokens. */
1393 /* The scope in which names should be looked up. If NULL_TREE, then
1394 we look up names in the scope that is currently open in the
1395 source program. If non-NULL, this is either a TYPE or
1396 NAMESPACE_DECL for the scope in which we should look. It can
1397 also be ERROR_MARK, when we've parsed a bogus scope.
1399 This value is not cleared automatically after a name is looked
1400 up, so we must be careful to clear it before starting a new look
1401 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1402 will look up `Z' in the scope of `X', rather than the current
1403 scope.) Unfortunately, it is difficult to tell when name lookup
1404 is complete, because we sometimes peek at a token, look it up,
1405 and then decide not to consume it. */
1408 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1409 last lookup took place. OBJECT_SCOPE is used if an expression
1410 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1411 respectively. QUALIFYING_SCOPE is used for an expression of the
1412 form "X::Y"; it refers to X. */
1414 tree qualifying_scope;
1416 /* A stack of parsing contexts. All but the bottom entry on the
1417 stack will be tentative contexts.
1419 We parse tentatively in order to determine which construct is in
1420 use in some situations. For example, in order to determine
1421 whether a statement is an expression-statement or a
1422 declaration-statement we parse it tentatively as a
1423 declaration-statement. If that fails, we then reparse the same
1424 token stream as an expression-statement. */
1425 cp_parser_context *context;
1427 /* True if we are parsing GNU C++. If this flag is not set, then
1428 GNU extensions are not recognized. */
1429 bool allow_gnu_extensions_p;
1431 /* TRUE if the `>' token should be interpreted as the greater-than
1432 operator. FALSE if it is the end of a template-id or
1433 template-parameter-list. In C++0x mode, this flag also applies to
1434 `>>' tokens, which are viewed as two consecutive `>' tokens when
1435 this flag is FALSE. */
1436 bool greater_than_is_operator_p;
1438 /* TRUE if default arguments are allowed within a parameter list
1439 that starts at this point. FALSE if only a gnu extension makes
1440 them permissible. */
1441 bool default_arg_ok_p;
1443 /* TRUE if we are parsing an integral constant-expression. See
1444 [expr.const] for a precise definition. */
1445 bool integral_constant_expression_p;
1447 /* TRUE if we are parsing an integral constant-expression -- but a
1448 non-constant expression should be permitted as well. This flag
1449 is used when parsing an array bound so that GNU variable-length
1450 arrays are tolerated. */
1451 bool allow_non_integral_constant_expression_p;
1453 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1454 been seen that makes the expression non-constant. */
1455 bool non_integral_constant_expression_p;
1457 /* TRUE if local variable names and `this' are forbidden in the
1459 bool local_variables_forbidden_p;
1461 /* TRUE if the declaration we are parsing is part of a
1462 linkage-specification of the form `extern string-literal
1464 bool in_unbraced_linkage_specification_p;
1466 /* TRUE if we are presently parsing a declarator, after the
1467 direct-declarator. */
1468 bool in_declarator_p;
1470 /* TRUE if we are presently parsing a template-argument-list. */
1471 bool in_template_argument_list_p;
1473 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1474 to IN_OMP_BLOCK if parsing OpenMP structured block and
1475 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1476 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1477 iteration-statement, OpenMP block or loop within that switch. */
1478 #define IN_SWITCH_STMT 1
1479 #define IN_ITERATION_STMT 2
1480 #define IN_OMP_BLOCK 4
1481 #define IN_OMP_FOR 8
1482 #define IN_IF_STMT 16
1483 unsigned char in_statement;
1485 /* TRUE if we are presently parsing the body of a switch statement.
1486 Note that this doesn't quite overlap with in_statement above.
1487 The difference relates to giving the right sets of error messages:
1488 "case not in switch" vs "break statement used with OpenMP...". */
1489 bool in_switch_statement_p;
1491 /* TRUE if we are parsing a type-id in an expression context. In
1492 such a situation, both "type (expr)" and "type (type)" are valid
1494 bool in_type_id_in_expr_p;
1496 /* TRUE if we are currently in a header file where declarations are
1497 implicitly extern "C". */
1498 bool implicit_extern_c;
1500 /* TRUE if strings in expressions should be translated to the execution
1502 bool translate_strings_p;
1504 /* TRUE if we are presently parsing the body of a function, but not
1506 bool in_function_body;
1508 /* If non-NULL, then we are parsing a construct where new type
1509 definitions are not permitted. The string stored here will be
1510 issued as an error message if a type is defined. */
1511 const char *type_definition_forbidden_message;
1513 /* A list of lists. The outer list is a stack, used for member
1514 functions of local classes. At each level there are two sub-list,
1515 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1516 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1517 TREE_VALUE's. The functions are chained in reverse declaration
1520 The TREE_PURPOSE sublist contains those functions with default
1521 arguments that need post processing, and the TREE_VALUE sublist
1522 contains those functions with definitions that need post
1525 These lists can only be processed once the outermost class being
1526 defined is complete. */
1527 tree unparsed_functions_queues;
1529 /* The number of classes whose definitions are currently in
1531 unsigned num_classes_being_defined;
1533 /* The number of template parameter lists that apply directly to the
1534 current declaration. */
1535 unsigned num_template_parameter_lists;
1540 /* Constructors and destructors. */
1542 static cp_parser *cp_parser_new
1545 /* Routines to parse various constructs.
1547 Those that return `tree' will return the error_mark_node (rather
1548 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1549 Sometimes, they will return an ordinary node if error-recovery was
1550 attempted, even though a parse error occurred. So, to check
1551 whether or not a parse error occurred, you should always use
1552 cp_parser_error_occurred. If the construct is optional (indicated
1553 either by an `_opt' in the name of the function that does the
1554 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1555 the construct is not present. */
1557 /* Lexical conventions [gram.lex] */
1559 static tree cp_parser_identifier
1561 static tree cp_parser_string_literal
1562 (cp_parser *, bool, bool);
1564 /* Basic concepts [gram.basic] */
1566 static bool cp_parser_translation_unit
1569 /* Expressions [gram.expr] */
1571 static tree cp_parser_primary_expression
1572 (cp_parser *, bool, bool, bool, cp_id_kind *);
1573 static tree cp_parser_id_expression
1574 (cp_parser *, bool, bool, bool *, bool, bool);
1575 static tree cp_parser_unqualified_id
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier_opt
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_class_or_namespace_name
1582 (cp_parser *, bool, bool, bool, bool, bool);
1583 static tree cp_parser_postfix_expression
1584 (cp_parser *, bool, bool, bool);
1585 static tree cp_parser_postfix_open_square_expression
1586 (cp_parser *, tree, bool);
1587 static tree cp_parser_postfix_dot_deref_expression
1588 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1589 static tree cp_parser_parenthesized_expression_list
1590 (cp_parser *, bool, bool, bool, bool *);
1591 static void cp_parser_pseudo_destructor_name
1592 (cp_parser *, tree *, tree *);
1593 static tree cp_parser_unary_expression
1594 (cp_parser *, bool, bool);
1595 static enum tree_code cp_parser_unary_operator
1597 static tree cp_parser_new_expression
1599 static tree cp_parser_new_placement
1601 static tree cp_parser_new_type_id
1602 (cp_parser *, tree *);
1603 static cp_declarator *cp_parser_new_declarator_opt
1605 static cp_declarator *cp_parser_direct_new_declarator
1607 static tree cp_parser_new_initializer
1609 static tree cp_parser_delete_expression
1611 static tree cp_parser_cast_expression
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_binary_expression
1614 (cp_parser *, bool, enum cp_parser_prec);
1615 static tree cp_parser_question_colon_clause
1616 (cp_parser *, tree);
1617 static tree cp_parser_assignment_expression
1618 (cp_parser *, bool);
1619 static enum tree_code cp_parser_assignment_operator_opt
1621 static tree cp_parser_expression
1622 (cp_parser *, bool);
1623 static tree cp_parser_constant_expression
1624 (cp_parser *, bool, bool *);
1625 static tree cp_parser_builtin_offsetof
1628 /* Statements [gram.stmt.stmt] */
1630 static void cp_parser_statement
1631 (cp_parser *, tree, bool, bool *);
1632 static void cp_parser_label_for_labeled_statement
1634 static tree cp_parser_expression_statement
1635 (cp_parser *, tree);
1636 static tree cp_parser_compound_statement
1637 (cp_parser *, tree, bool);
1638 static void cp_parser_statement_seq_opt
1639 (cp_parser *, tree);
1640 static tree cp_parser_selection_statement
1641 (cp_parser *, bool *);
1642 static tree cp_parser_condition
1644 static tree cp_parser_iteration_statement
1646 static void cp_parser_for_init_statement
1648 static tree cp_parser_jump_statement
1650 static void cp_parser_declaration_statement
1653 static tree cp_parser_implicitly_scoped_statement
1654 (cp_parser *, bool *);
1655 static void cp_parser_already_scoped_statement
1658 /* Declarations [gram.dcl.dcl] */
1660 static void cp_parser_declaration_seq_opt
1662 static void cp_parser_declaration
1664 static void cp_parser_block_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_simple_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_decl_specifier_seq
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1670 static tree cp_parser_storage_class_specifier_opt
1672 static tree cp_parser_function_specifier_opt
1673 (cp_parser *, cp_decl_specifier_seq *);
1674 static tree cp_parser_type_specifier
1675 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1677 static tree cp_parser_simple_type_specifier
1678 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1679 static tree cp_parser_type_name
1681 static tree cp_parser_nonclass_name
1682 (cp_parser* parser);
1683 static tree cp_parser_elaborated_type_specifier
1684 (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1687 static void cp_parser_enumerator_list
1688 (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690 (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1693 static void cp_parser_namespace_definition
1695 static void cp_parser_namespace_body
1697 static tree cp_parser_qualified_namespace_specifier
1699 static void cp_parser_namespace_alias_definition
1701 static bool cp_parser_using_declaration
1702 (cp_parser *, bool);
1703 static void cp_parser_using_directive
1705 static void cp_parser_asm_definition
1707 static void cp_parser_linkage_specification
1709 static void cp_parser_static_assert
1710 (cp_parser *, bool);
1711 static tree cp_parser_decltype
1714 /* Declarators [gram.dcl.decl] */
1716 static tree cp_parser_init_declarator
1717 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723 (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1730 static void cp_parser_type_specifier_seq
1731 (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735 (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737 (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument
1739 (cp_parser *, bool);
1740 static void cp_parser_function_body
1742 static tree cp_parser_initializer
1743 (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745 (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747 (cp_parser *, bool *);
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1752 /* Classes [gram.class] */
1754 static tree cp_parser_class_name
1755 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1758 static tree cp_parser_class_head
1759 (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1762 static void cp_parser_member_specification_opt
1764 static void cp_parser_member_declaration
1766 static tree cp_parser_pure_specifier
1768 static tree cp_parser_constant_initializer
1771 /* Derived classes [gram.class.derived] */
1773 static tree cp_parser_base_clause
1775 static tree cp_parser_base_specifier
1778 /* Special member functions [gram.special] */
1780 static tree cp_parser_conversion_function_id
1782 static tree cp_parser_conversion_type_id
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1786 static bool cp_parser_ctor_initializer_opt
1788 static void cp_parser_mem_initializer_list
1790 static tree cp_parser_mem_initializer
1792 static tree cp_parser_mem_initializer_id
1795 /* Overloading [gram.over] */
1797 static tree cp_parser_operator_function_id
1799 static tree cp_parser_operator
1802 /* Templates [gram.temp] */
1804 static void cp_parser_template_declaration
1805 (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1808 static tree cp_parser_template_parameter
1809 (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811 (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813 (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815 (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1818 static tree cp_parser_template_argument
1820 static void cp_parser_explicit_instantiation
1822 static void cp_parser_explicit_specialization
1825 /* Exception handling [gram.exception] */
1827 static tree cp_parser_try_block
1829 static bool cp_parser_function_try_block
1831 static void cp_parser_handler_seq
1833 static void cp_parser_handler
1835 static tree cp_parser_exception_declaration
1837 static tree cp_parser_throw_expression
1839 static tree cp_parser_exception_specification_opt
1841 static tree cp_parser_type_id_list
1844 /* GNU Extensions */
1846 static tree cp_parser_asm_specification_opt
1848 static tree cp_parser_asm_operand_list
1850 static tree cp_parser_asm_clobber_list
1852 static tree cp_parser_attributes_opt
1854 static tree cp_parser_attribute_list
1856 static bool cp_parser_extension_opt
1857 (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863 (cp_parser *, enum pragma_context);
1865 /* Objective-C++ Productions */
1867 static tree cp_parser_objc_message_receiver
1869 static tree cp_parser_objc_message_args
1871 static tree cp_parser_objc_message_expression
1873 static tree cp_parser_objc_encode_expression
1875 static tree cp_parser_objc_defs_expression
1877 static tree cp_parser_objc_protocol_expression
1879 static tree cp_parser_objc_selector_expression
1881 static tree cp_parser_objc_expression
1883 static bool cp_parser_objc_selector_p
1885 static tree cp_parser_objc_selector
1887 static tree cp_parser_objc_protocol_refs_opt
1889 static void cp_parser_objc_declaration
1891 static tree cp_parser_objc_statement
1894 /* Utility Routines */
1896 static tree cp_parser_lookup_name
1897 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899 (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1902 static bool cp_parser_check_declarator_template_parameters
1903 (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905 (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1908 static tree cp_parser_global_scope_opt
1909 (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911 (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915 (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917 (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919 (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923 (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1928 static void cp_parser_save_default_args
1929 (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931 (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933 (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935 (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937 (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1940 static void cp_parser_set_storage_class
1941 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943 (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945 (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947 (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949 (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1952 static bool cp_parser_next_token_starts_class_definition_p
1954 static bool cp_parser_next_token_ends_template_argument_p
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957 (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1960 static void cp_parser_check_class_key
1961 (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1964 static bool cp_parser_optional_template_keyword
1966 static void cp_parser_pre_parsed_nested_name_specifier
1968 static void cp_parser_cache_group
1969 (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1972 static void cp_parser_commit_to_tentative_parse
1974 static void cp_parser_abort_tentative_parse
1976 static bool cp_parser_parse_definitely
1978 static inline bool cp_parser_parsing_tentatively
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1982 static void cp_parser_error
1983 (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985 (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1988 static bool cp_parser_check_type_definition
1990 static void cp_parser_check_for_definition_in_return_type
1991 (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993 (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995 (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997 (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
2000 static int cp_parser_skip_to_closing_parenthesis
2001 (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2006 static void cp_parser_skip_to_end_of_block_or_statement
2008 static bool cp_parser_skip_to_closing_brace
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2012 static void cp_parser_skip_to_pragma_eol
2013 (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2016 static bool cp_parser_allow_gnu_extensions_p
2018 static bool cp_parser_is_string_literal
2020 static bool cp_parser_is_keyword
2021 (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023 (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2027 /* Returns nonzero if we are parsing tentatively. */
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2032 return parser->context->next != NULL;
2035 /* Returns nonzero if TOKEN is a string literal. */
2038 cp_parser_is_string_literal (cp_token* token)
2040 return (token->type == CPP_STRING ||
2041 token->type == CPP_STRING16 ||
2042 token->type == CPP_STRING32 ||
2043 token->type == CPP_WSTRING);
2046 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2049 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2051 return token->keyword == keyword;
2054 /* If not parsing tentatively, issue a diagnostic of the form
2055 FILE:LINE: MESSAGE before TOKEN
2056 where TOKEN is the next token in the input stream. MESSAGE
2057 (specified by the caller) is usually of the form "expected
2061 cp_parser_error (cp_parser* parser, const char* message)
2063 if (!cp_parser_simulate_error (parser))
2065 cp_token *token = cp_lexer_peek_token (parser->lexer);
2066 /* This diagnostic makes more sense if it is tagged to the line
2067 of the token we just peeked at. */
2068 cp_lexer_set_source_position_from_token (token);
2070 if (token->type == CPP_PRAGMA)
2072 error ("%<#pragma%> is not allowed here");
2073 cp_parser_skip_to_pragma_eol (parser, token);
2077 c_parse_error (message,
2078 /* Because c_parser_error does not understand
2079 CPP_KEYWORD, keywords are treated like
2081 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2086 /* Issue an error about name-lookup failing. NAME is the
2087 IDENTIFIER_NODE DECL is the result of
2088 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2089 the thing that we hoped to find. */
2092 cp_parser_name_lookup_error (cp_parser* parser,
2095 const char* desired)
2097 /* If name lookup completely failed, tell the user that NAME was not
2099 if (decl == error_mark_node)
2101 if (parser->scope && parser->scope != global_namespace)
2102 error ("%<%E::%E%> has not been declared",
2103 parser->scope, name);
2104 else if (parser->scope == global_namespace)
2105 error ("%<::%E%> has not been declared", name);
2106 else if (parser->object_scope
2107 && !CLASS_TYPE_P (parser->object_scope))
2108 error ("request for member %qE in non-class type %qT",
2109 name, parser->object_scope);
2110 else if (parser->object_scope)
2111 error ("%<%T::%E%> has not been declared",
2112 parser->object_scope, name);
2114 error ("%qE has not been declared", name);
2116 else if (parser->scope && parser->scope != global_namespace)
2117 error ("%<%E::%E%> %s", parser->scope, name, desired);
2118 else if (parser->scope == global_namespace)
2119 error ("%<::%E%> %s", name, desired);
2121 error ("%qE %s", name, desired);
2124 /* If we are parsing tentatively, remember that an error has occurred
2125 during this tentative parse. Returns true if the error was
2126 simulated; false if a message should be issued by the caller. */
2129 cp_parser_simulate_error (cp_parser* parser)
2131 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2133 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2139 /* Check for repeated decl-specifiers. */
2142 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2146 for (ds = ds_first; ds != ds_last; ++ds)
2148 unsigned count = decl_specs->specs[(int)ds];
2151 /* The "long" specifier is a special case because of "long long". */
2155 error ("%<long long long%> is too long for GCC");
2156 else if (pedantic && !in_system_header && warn_long_long
2157 && cxx_dialect == cxx98)
2158 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2162 static const char *const decl_spec_names[] = {
2178 error ("duplicate %qs", decl_spec_names[(int)ds]);
2183 /* This function is called when a type is defined. If type
2184 definitions are forbidden at this point, an error message is
2188 cp_parser_check_type_definition (cp_parser* parser)
2190 /* If types are forbidden here, issue a message. */
2191 if (parser->type_definition_forbidden_message)
2193 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2194 in the message need to be interpreted. */
2195 error (parser->type_definition_forbidden_message);
2201 /* This function is called when the DECLARATOR is processed. The TYPE
2202 was a type defined in the decl-specifiers. If it is invalid to
2203 define a type in the decl-specifiers for DECLARATOR, an error is
2207 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2210 /* [dcl.fct] forbids type definitions in return types.
2211 Unfortunately, it's not easy to know whether or not we are
2212 processing a return type until after the fact. */
2214 && (declarator->kind == cdk_pointer
2215 || declarator->kind == cdk_reference
2216 || declarator->kind == cdk_ptrmem))
2217 declarator = declarator->declarator;
2219 && declarator->kind == cdk_function)
2221 error ("new types may not be defined in a return type");
2222 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2227 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2228 "<" in any valid C++ program. If the next token is indeed "<",
2229 issue a message warning the user about what appears to be an
2230 invalid attempt to form a template-id. */
2233 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2236 cp_token_position start = 0;
2238 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2241 error ("%qT is not a template", type);
2242 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2243 error ("%qE is not a template", type);
2245 error ("invalid template-id");
2246 /* Remember the location of the invalid "<". */
2247 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2248 start = cp_lexer_token_position (parser->lexer, true);
2249 /* Consume the "<". */
2250 cp_lexer_consume_token (parser->lexer);
2251 /* Parse the template arguments. */
2252 cp_parser_enclosed_template_argument_list (parser);
2253 /* Permanently remove the invalid template arguments so that
2254 this error message is not issued again. */
2256 cp_lexer_purge_tokens_after (parser->lexer, start);
2260 /* If parsing an integral constant-expression, issue an error message
2261 about the fact that THING appeared and return true. Otherwise,
2262 return false. In either case, set
2263 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2266 cp_parser_non_integral_constant_expression (cp_parser *parser,
2269 parser->non_integral_constant_expression_p = true;
2270 if (parser->integral_constant_expression_p)
2272 if (!parser->allow_non_integral_constant_expression_p)
2274 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2275 in the message need to be interpreted. */
2276 char *message = concat (thing,
2277 " cannot appear in a constant-expression",
2287 /* Emit a diagnostic for an invalid type name. SCOPE is the
2288 qualifying scope (or NULL, if none) for ID. This function commits
2289 to the current active tentative parse, if any. (Otherwise, the
2290 problematic construct might be encountered again later, resulting
2291 in duplicate error messages.) */
2294 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2296 tree decl, old_scope;
2297 /* Try to lookup the identifier. */
2298 old_scope = parser->scope;
2299 parser->scope = scope;
2300 decl = cp_parser_lookup_name_simple (parser, id);
2301 parser->scope = old_scope;
2302 /* If the lookup found a template-name, it means that the user forgot
2303 to specify an argument list. Emit a useful error message. */
2304 if (TREE_CODE (decl) == TEMPLATE_DECL)
2305 error ("invalid use of template-name %qE without an argument list", decl);
2306 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2307 error ("invalid use of destructor %qD as a type", id);
2308 else if (TREE_CODE (decl) == TYPE_DECL)
2309 /* Something like 'unsigned A a;' */
2310 error ("invalid combination of multiple type-specifiers");
2311 else if (!parser->scope)
2313 /* Issue an error message. */
2314 error ("%qE does not name a type", id);
2315 /* If we're in a template class, it's possible that the user was
2316 referring to a type from a base class. For example:
2318 template <typename T> struct A { typedef T X; };
2319 template <typename T> struct B : public A<T> { X x; };
2321 The user should have said "typename A<T>::X". */
2322 if (processing_template_decl && current_class_type
2323 && TYPE_BINFO (current_class_type))
2327 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2331 tree base_type = BINFO_TYPE (b);
2332 if (CLASS_TYPE_P (base_type)
2333 && dependent_type_p (base_type))
2336 /* Go from a particular instantiation of the
2337 template (which will have an empty TYPE_FIELDs),
2338 to the main version. */
2339 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2340 for (field = TYPE_FIELDS (base_type);
2342 field = TREE_CHAIN (field))
2343 if (TREE_CODE (field) == TYPE_DECL
2344 && DECL_NAME (field) == id)
2346 inform ("(perhaps %<typename %T::%E%> was intended)",
2347 BINFO_TYPE (b), id);
2356 /* Here we diagnose qualified-ids where the scope is actually correct,
2357 but the identifier does not resolve to a valid type name. */
2358 else if (parser->scope != error_mark_node)
2360 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2361 error ("%qE in namespace %qE does not name a type",
2363 else if (TYPE_P (parser->scope))
2364 error ("%qE in class %qT does not name a type", id, parser->scope);
2368 cp_parser_commit_to_tentative_parse (parser);
2371 /* Check for a common situation where a type-name should be present,
2372 but is not, and issue a sensible error message. Returns true if an
2373 invalid type-name was detected.
2375 The situation handled by this function are variable declarations of the
2376 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2377 Usually, `ID' should name a type, but if we got here it means that it
2378 does not. We try to emit the best possible error message depending on
2379 how exactly the id-expression looks like. */
2382 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2386 cp_parser_parse_tentatively (parser);
2387 id = cp_parser_id_expression (parser,
2388 /*template_keyword_p=*/false,
2389 /*check_dependency_p=*/true,
2390 /*template_p=*/NULL,
2391 /*declarator_p=*/true,
2392 /*optional_p=*/false);
2393 /* After the id-expression, there should be a plain identifier,
2394 otherwise this is not a simple variable declaration. Also, if
2395 the scope is dependent, we cannot do much. */
2396 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2397 || (parser->scope && TYPE_P (parser->scope)
2398 && dependent_type_p (parser->scope))
2399 || TREE_CODE (id) == TYPE_DECL)
2401 cp_parser_abort_tentative_parse (parser);
2404 if (!cp_parser_parse_definitely (parser))
2407 /* Emit a diagnostic for the invalid type. */
2408 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2409 /* Skip to the end of the declaration; there's no point in
2410 trying to process it. */
2411 cp_parser_skip_to_end_of_block_or_statement (parser);
2415 /* Consume tokens up to, and including, the next non-nested closing `)'.
2416 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2417 are doing error recovery. Returns -1 if OR_COMMA is true and we
2418 found an unnested comma. */
2421 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2426 unsigned paren_depth = 0;
2427 unsigned brace_depth = 0;
2429 if (recovering && !or_comma
2430 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2435 cp_token * token = cp_lexer_peek_token (parser->lexer);
2437 switch (token->type)
2440 case CPP_PRAGMA_EOL:
2441 /* If we've run out of tokens, then there is no closing `)'. */
2445 /* This matches the processing in skip_to_end_of_statement. */
2450 case CPP_OPEN_BRACE:
2453 case CPP_CLOSE_BRACE:
2459 if (recovering && or_comma && !brace_depth && !paren_depth)
2463 case CPP_OPEN_PAREN:
2468 case CPP_CLOSE_PAREN:
2469 if (!brace_depth && !paren_depth--)
2472 cp_lexer_consume_token (parser->lexer);
2481 /* Consume the token. */
2482 cp_lexer_consume_token (parser->lexer);
2486 /* Consume tokens until we reach the end of the current statement.
2487 Normally, that will be just before consuming a `;'. However, if a
2488 non-nested `}' comes first, then we stop before consuming that. */
2491 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2493 unsigned nesting_depth = 0;
2497 cp_token *token = cp_lexer_peek_token (parser->lexer);
2499 switch (token->type)
2502 case CPP_PRAGMA_EOL:
2503 /* If we've run out of tokens, stop. */
2507 /* If the next token is a `;', we have reached the end of the
2513 case CPP_CLOSE_BRACE:
2514 /* If this is a non-nested '}', stop before consuming it.
2515 That way, when confronted with something like:
2519 we stop before consuming the closing '}', even though we
2520 have not yet reached a `;'. */
2521 if (nesting_depth == 0)
2524 /* If it is the closing '}' for a block that we have
2525 scanned, stop -- but only after consuming the token.
2531 we will stop after the body of the erroneously declared
2532 function, but before consuming the following `typedef'
2534 if (--nesting_depth == 0)
2536 cp_lexer_consume_token (parser->lexer);
2540 case CPP_OPEN_BRACE:
2548 /* Consume the token. */
2549 cp_lexer_consume_token (parser->lexer);
2553 /* This function is called at the end of a statement or declaration.
2554 If the next token is a semicolon, it is consumed; otherwise, error
2555 recovery is attempted. */
2558 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2560 /* Look for the trailing `;'. */
2561 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2563 /* If there is additional (erroneous) input, skip to the end of
2565 cp_parser_skip_to_end_of_statement (parser);
2566 /* If the next token is now a `;', consume it. */
2567 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2568 cp_lexer_consume_token (parser->lexer);
2572 /* Skip tokens until we have consumed an entire block, or until we
2573 have consumed a non-nested `;'. */
2576 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2578 int nesting_depth = 0;
2580 while (nesting_depth >= 0)
2582 cp_token *token = cp_lexer_peek_token (parser->lexer);
2584 switch (token->type)
2587 case CPP_PRAGMA_EOL:
2588 /* If we've run out of tokens, stop. */
2592 /* Stop if this is an unnested ';'. */
2597 case CPP_CLOSE_BRACE:
2598 /* Stop if this is an unnested '}', or closes the outermost
2605 case CPP_OPEN_BRACE:
2614 /* Consume the token. */
2615 cp_lexer_consume_token (parser->lexer);
2619 /* Skip tokens until a non-nested closing curly brace is the next
2620 token, or there are no more tokens. Return true in the first case,
2624 cp_parser_skip_to_closing_brace (cp_parser *parser)
2626 unsigned nesting_depth = 0;
2630 cp_token *token = cp_lexer_peek_token (parser->lexer);
2632 switch (token->type)
2635 case CPP_PRAGMA_EOL:
2636 /* If we've run out of tokens, stop. */
2639 case CPP_CLOSE_BRACE:
2640 /* If the next token is a non-nested `}', then we have reached
2641 the end of the current block. */
2642 if (nesting_depth-- == 0)
2646 case CPP_OPEN_BRACE:
2647 /* If it the next token is a `{', then we are entering a new
2648 block. Consume the entire block. */
2656 /* Consume the token. */
2657 cp_lexer_consume_token (parser->lexer);
2661 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2662 parameter is the PRAGMA token, allowing us to purge the entire pragma
2666 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2670 parser->lexer->in_pragma = false;
2673 token = cp_lexer_consume_token (parser->lexer);
2674 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2676 /* Ensure that the pragma is not parsed again. */
2677 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2680 /* Require pragma end of line, resyncing with it as necessary. The
2681 arguments are as for cp_parser_skip_to_pragma_eol. */
2684 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2686 parser->lexer->in_pragma = false;
2687 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2688 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2691 /* This is a simple wrapper around make_typename_type. When the id is
2692 an unresolved identifier node, we can provide a superior diagnostic
2693 using cp_parser_diagnose_invalid_type_name. */
2696 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2699 if (TREE_CODE (id) == IDENTIFIER_NODE)
2701 result = make_typename_type (scope, id, typename_type,
2702 /*complain=*/tf_none);
2703 if (result == error_mark_node)
2704 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2707 return make_typename_type (scope, id, typename_type, tf_error);
2710 /* This is a wrapper around the
2711 make_{pointer,ptrmem,reference}_declarator functions that decides
2712 which one to call based on the CODE and CLASS_TYPE arguments. The
2713 CODE argument should be one of the values returned by
2714 cp_parser_ptr_operator. */
2715 static cp_declarator *
2716 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2717 cp_cv_quals cv_qualifiers,
2718 cp_declarator *target)
2720 if (code == ERROR_MARK)
2721 return cp_error_declarator;
2723 if (code == INDIRECT_REF)
2724 if (class_type == NULL_TREE)
2725 return make_pointer_declarator (cv_qualifiers, target);
2727 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2728 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2729 return make_reference_declarator (cv_qualifiers, target, false);
2730 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2731 return make_reference_declarator (cv_qualifiers, target, true);
2735 /* Create a new C++ parser. */
2738 cp_parser_new (void)
2744 /* cp_lexer_new_main is called before calling ggc_alloc because
2745 cp_lexer_new_main might load a PCH file. */
2746 lexer = cp_lexer_new_main ();
2748 /* Initialize the binops_by_token so that we can get the tree
2749 directly from the token. */
2750 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2751 binops_by_token[binops[i].token_type] = binops[i];
2753 parser = GGC_CNEW (cp_parser);
2754 parser->lexer = lexer;
2755 parser->context = cp_parser_context_new (NULL);
2757 /* For now, we always accept GNU extensions. */
2758 parser->allow_gnu_extensions_p = 1;
2760 /* The `>' token is a greater-than operator, not the end of a
2762 parser->greater_than_is_operator_p = true;
2764 parser->default_arg_ok_p = true;
2766 /* We are not parsing a constant-expression. */
2767 parser->integral_constant_expression_p = false;
2768 parser->allow_non_integral_constant_expression_p = false;
2769 parser->non_integral_constant_expression_p = false;
2771 /* Local variable names are not forbidden. */
2772 parser->local_variables_forbidden_p = false;
2774 /* We are not processing an `extern "C"' declaration. */
2775 parser->in_unbraced_linkage_specification_p = false;
2777 /* We are not processing a declarator. */
2778 parser->in_declarator_p = false;
2780 /* We are not processing a template-argument-list. */
2781 parser->in_template_argument_list_p = false;
2783 /* We are not in an iteration statement. */
2784 parser->in_statement = 0;
2786 /* We are not in a switch statement. */
2787 parser->in_switch_statement_p = false;
2789 /* We are not parsing a type-id inside an expression. */
2790 parser->in_type_id_in_expr_p = false;
2792 /* Declarations aren't implicitly extern "C". */
2793 parser->implicit_extern_c = false;
2795 /* String literals should be translated to the execution character set. */
2796 parser->translate_strings_p = true;
2798 /* We are not parsing a function body. */
2799 parser->in_function_body = false;
2801 /* The unparsed function queue is empty. */
2802 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2804 /* There are no classes being defined. */
2805 parser->num_classes_being_defined = 0;
2807 /* No template parameters apply. */
2808 parser->num_template_parameter_lists = 0;
2813 /* Create a cp_lexer structure which will emit the tokens in CACHE
2814 and push it onto the parser's lexer stack. This is used for delayed
2815 parsing of in-class method bodies and default arguments, and should
2816 not be confused with tentative parsing. */
2818 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2820 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2821 lexer->next = parser->lexer;
2822 parser->lexer = lexer;
2824 /* Move the current source position to that of the first token in the
2826 cp_lexer_set_source_position_from_token (lexer->next_token);
2829 /* Pop the top lexer off the parser stack. This is never used for the
2830 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2832 cp_parser_pop_lexer (cp_parser *parser)
2834 cp_lexer *lexer = parser->lexer;
2835 parser->lexer = lexer->next;
2836 cp_lexer_destroy (lexer);
2838 /* Put the current source position back where it was before this
2839 lexer was pushed. */
2840 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2843 /* Lexical conventions [gram.lex] */
2845 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2849 cp_parser_identifier (cp_parser* parser)
2853 /* Look for the identifier. */
2854 token = cp_parser_require (parser, CPP_NAME, "identifier");
2855 /* Return the value. */
2856 return token ? token->u.value : error_mark_node;
2859 /* Parse a sequence of adjacent string constants. Returns a
2860 TREE_STRING representing the combined, nul-terminated string
2861 constant. If TRANSLATE is true, translate the string to the
2862 execution character set. If WIDE_OK is true, a wide string is
2865 C++98 [lex.string] says that if a narrow string literal token is
2866 adjacent to a wide string literal token, the behavior is undefined.
2867 However, C99 6.4.5p4 says that this results in a wide string literal.
2868 We follow C99 here, for consistency with the C front end.
2870 This code is largely lifted from lex_string() in c-lex.c.
2872 FUTURE: ObjC++ will need to handle @-strings here. */
2874 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2878 struct obstack str_ob;
2879 cpp_string str, istr, *strs;
2881 enum cpp_ttype type;
2883 tok = cp_lexer_peek_token (parser->lexer);
2884 if (!cp_parser_is_string_literal (tok))
2886 cp_parser_error (parser, "expected string-literal");
2887 return error_mark_node;
2892 /* Try to avoid the overhead of creating and destroying an obstack
2893 for the common case of just one string. */
2894 if (!cp_parser_is_string_literal
2895 (cp_lexer_peek_nth_token (parser->lexer, 2)))
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);
2907 gcc_obstack_init (&str_ob);
2912 cp_lexer_consume_token (parser->lexer);
2914 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2915 str.len = TREE_STRING_LENGTH (tok->u.value);
2917 if (type != tok->type)
2919 if (type == CPP_STRING)
2921 else if (tok->type != CPP_STRING)
2922 error ("unsupported non-standard concatenation of string literals");
2925 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2927 tok = cp_lexer_peek_token (parser->lexer);
2929 while (cp_parser_is_string_literal (tok));
2931 strs = (cpp_string *) obstack_finish (&str_ob);
2934 if (type != CPP_STRING && !wide_ok)
2936 cp_parser_error (parser, "a wide string is invalid in this context");
2940 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2941 (parse_in, strs, count, &istr, type))
2943 value = build_string (istr.len, (const char *)istr.text);
2944 free (CONST_CAST (unsigned char *, istr.text));
2950 TREE_TYPE (value) = char_array_type_node;
2953 TREE_TYPE (value) = char16_array_type_node;
2956 TREE_TYPE (value) = char32_array_type_node;
2959 TREE_TYPE (value) = wchar_array_type_node;
2963 value = fix_string_type (value);
2966 /* cpp_interpret_string has issued an error. */
2967 value = error_mark_node;
2970 obstack_free (&str_ob, 0);
2976 /* Basic concepts [gram.basic] */
2978 /* Parse a translation-unit.
2981 declaration-seq [opt]
2983 Returns TRUE if all went well. */
2986 cp_parser_translation_unit (cp_parser* parser)
2988 /* The address of the first non-permanent object on the declarator
2990 static void *declarator_obstack_base;
2994 /* Create the declarator obstack, if necessary. */
2995 if (!cp_error_declarator)
2997 gcc_obstack_init (&declarator_obstack);
2998 /* Create the error declarator. */
2999 cp_error_declarator = make_declarator (cdk_error);
3000 /* Create the empty parameter list. */
3001 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3002 /* Remember where the base of the declarator obstack lies. */
3003 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3006 cp_parser_declaration_seq_opt (parser);
3008 /* If there are no tokens left then all went well. */
3009 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3011 /* Get rid of the token array; we don't need it any more. */
3012 cp_lexer_destroy (parser->lexer);
3013 parser->lexer = NULL;
3015 /* This file might have been a context that's implicitly extern
3016 "C". If so, pop the lang context. (Only relevant for PCH.) */
3017 if (parser->implicit_extern_c)
3019 pop_lang_context ();
3020 parser->implicit_extern_c = false;
3024 finish_translation_unit ();
3030 cp_parser_error (parser, "expected declaration");
3034 /* Make sure the declarator obstack was fully cleaned up. */
3035 gcc_assert (obstack_next_free (&declarator_obstack)
3036 == declarator_obstack_base);
3038 /* All went well. */
3042 /* Expressions [gram.expr] */
3044 /* Parse a primary-expression.
3055 ( compound-statement )
3056 __builtin_va_arg ( assignment-expression , type-id )
3057 __builtin_offsetof ( type-id , offsetof-expression )
3060 __has_nothrow_assign ( type-id )
3061 __has_nothrow_constructor ( type-id )
3062 __has_nothrow_copy ( type-id )
3063 __has_trivial_assign ( type-id )
3064 __has_trivial_constructor ( type-id )
3065 __has_trivial_copy ( type-id )
3066 __has_trivial_destructor ( type-id )
3067 __has_virtual_destructor ( type-id )
3068 __is_abstract ( type-id )
3069 __is_base_of ( type-id , type-id )
3070 __is_class ( type-id )
3071 __is_convertible_to ( type-id , type-id )
3072 __is_empty ( type-id )
3073 __is_enum ( type-id )
3074 __is_pod ( type-id )
3075 __is_polymorphic ( type-id )
3076 __is_union ( type-id )
3078 Objective-C++ Extension:
3086 ADDRESS_P is true iff this expression was immediately preceded by
3087 "&" and therefore might denote a pointer-to-member. CAST_P is true
3088 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3089 true iff this expression is a template argument.
3091 Returns a representation of the expression. Upon return, *IDK
3092 indicates what kind of id-expression (if any) was present. */
3095 cp_parser_primary_expression (cp_parser *parser,
3098 bool template_arg_p,
3103 /* Assume the primary expression is not an id-expression. */
3104 *idk = CP_ID_KIND_NONE;
3106 /* Peek at the next token. */
3107 token = cp_lexer_peek_token (parser->lexer);
3108 switch (token->type)
3121 token = cp_lexer_consume_token (parser->lexer);
3122 /* Floating-point literals are only allowed in an integral
3123 constant expression if they are cast to an integral or
3124 enumeration type. */
3125 if (TREE_CODE (token->u.value) == REAL_CST
3126 && parser->integral_constant_expression_p
3129 /* CAST_P will be set even in invalid code like "int(2.7 +
3130 ...)". Therefore, we have to check that the next token
3131 is sure to end the cast. */
3134 cp_token *next_token;
3136 next_token = cp_lexer_peek_token (parser->lexer);
3137 if (/* The comma at the end of an
3138 enumerator-definition. */
3139 next_token->type != CPP_COMMA
3140 /* The curly brace at the end of an enum-specifier. */
3141 && next_token->type != CPP_CLOSE_BRACE
3142 /* The end of a statement. */
3143 && next_token->type != CPP_SEMICOLON
3144 /* The end of the cast-expression. */
3145 && next_token->type != CPP_CLOSE_PAREN
3146 /* The end of an array bound. */
3147 && next_token->type != CPP_CLOSE_SQUARE
3148 /* The closing ">" in a template-argument-list. */
3149 && (next_token->type != CPP_GREATER
3150 || parser->greater_than_is_operator_p)
3151 /* C++0x only: A ">>" treated like two ">" tokens,
3152 in a template-argument-list. */
3153 && (next_token->type != CPP_RSHIFT
3154 || (cxx_dialect == cxx98)
3155 || parser->greater_than_is_operator_p))
3159 /* If we are within a cast, then the constraint that the
3160 cast is to an integral or enumeration type will be
3161 checked at that point. If we are not within a cast, then
3162 this code is invalid. */
3164 cp_parser_non_integral_constant_expression
3165 (parser, "floating-point literal");
3167 return token->u.value;
3173 /* ??? Should wide strings be allowed when parser->translate_strings_p
3174 is false (i.e. in attributes)? If not, we can kill the third
3175 argument to cp_parser_string_literal. */
3176 return cp_parser_string_literal (parser,
3177 parser->translate_strings_p,
3180 case CPP_OPEN_PAREN:
3183 bool saved_greater_than_is_operator_p;
3185 /* Consume the `('. */
3186 cp_lexer_consume_token (parser->lexer);
3187 /* Within a parenthesized expression, a `>' token is always
3188 the greater-than operator. */
3189 saved_greater_than_is_operator_p
3190 = parser->greater_than_is_operator_p;
3191 parser->greater_than_is_operator_p = true;
3192 /* If we see `( { ' then we are looking at the beginning of
3193 a GNU statement-expression. */
3194 if (cp_parser_allow_gnu_extensions_p (parser)
3195 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3197 /* Statement-expressions are not allowed by the standard. */
3199 pedwarn ("ISO C++ forbids braced-groups within expressions");
3201 /* And they're not allowed outside of a function-body; you
3202 cannot, for example, write:
3204 int i = ({ int j = 3; j + 1; });
3206 at class or namespace scope. */
3207 if (!parser->in_function_body
3208 || parser->in_template_argument_list_p)
3210 error ("statement-expressions are not allowed outside "
3211 "functions nor in template-argument lists");
3212 cp_parser_skip_to_end_of_block_or_statement (parser);
3213 expr = error_mark_node;
3217 /* Start the statement-expression. */
3218 expr = begin_stmt_expr ();
3219 /* Parse the compound-statement. */
3220 cp_parser_compound_statement (parser, expr, false);
3222 expr = finish_stmt_expr (expr, false);
3227 /* Parse the parenthesized expression. */
3228 expr = cp_parser_expression (parser, cast_p);
3229 /* Let the front end know that this expression was
3230 enclosed in parentheses. This matters in case, for
3231 example, the expression is of the form `A::B', since
3232 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3234 finish_parenthesized_expr (expr);
3236 /* The `>' token might be the end of a template-id or
3237 template-parameter-list now. */
3238 parser->greater_than_is_operator_p
3239 = saved_greater_than_is_operator_p;
3240 /* Consume the `)'. */
3241 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3242 cp_parser_skip_to_end_of_statement (parser);
3248 switch (token->keyword)
3250 /* These two are the boolean literals. */
3252 cp_lexer_consume_token (parser->lexer);
3253 return boolean_true_node;
3255 cp_lexer_consume_token (parser->lexer);
3256 return boolean_false_node;
3258 /* The `__null' literal. */
3260 cp_lexer_consume_token (parser->lexer);
3263 /* Recognize the `this' keyword. */
3265 cp_lexer_consume_token (parser->lexer);
3266 if (parser->local_variables_forbidden_p)
3268 error ("%<this%> may not be used in this context");
3269 return error_mark_node;
3271 /* Pointers cannot appear in constant-expressions. */
3272 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3273 return error_mark_node;
3274 return finish_this_expr ();
3276 /* The `operator' keyword can be the beginning of an
3281 case RID_FUNCTION_NAME:
3282 case RID_PRETTY_FUNCTION_NAME:
3283 case RID_C99_FUNCTION_NAME:
3284 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3285 __func__ are the names of variables -- but they are
3286 treated specially. Therefore, they are handled here,
3287 rather than relying on the generic id-expression logic
3288 below. Grammatically, these names are id-expressions.
3290 Consume the token. */
3291 token = cp_lexer_consume_token (parser->lexer);
3292 /* Look up the name. */
3293 return finish_fname (token->u.value);
3300 /* The `__builtin_va_arg' construct is used to handle
3301 `va_arg'. Consume the `__builtin_va_arg' token. */
3302 cp_lexer_consume_token (parser->lexer);
3303 /* Look for the opening `('. */
3304 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3305 /* Now, parse the assignment-expression. */
3306 expression = cp_parser_assignment_expression (parser,
3308 /* Look for the `,'. */
3309 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3310 /* Parse the type-id. */
3311 type = cp_parser_type_id (parser);
3312 /* Look for the closing `)'. */
3313 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3314 /* Using `va_arg' in a constant-expression is not
3316 if (cp_parser_non_integral_constant_expression (parser,
3318 return error_mark_node;
3319 return build_x_va_arg (expression, type);
3323 return cp_parser_builtin_offsetof (parser);
3325 case RID_HAS_NOTHROW_ASSIGN:
3326 case RID_HAS_NOTHROW_CONSTRUCTOR:
3327 case RID_HAS_NOTHROW_COPY:
3328 case RID_HAS_TRIVIAL_ASSIGN:
3329 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3330 case RID_HAS_TRIVIAL_COPY:
3331 case RID_HAS_TRIVIAL_DESTRUCTOR:
3332 case RID_HAS_VIRTUAL_DESTRUCTOR:
3333 case RID_IS_ABSTRACT:
3334 case RID_IS_BASE_OF:
3336 case RID_IS_CONVERTIBLE_TO:
3340 case RID_IS_POLYMORPHIC:
3342 return cp_parser_trait_expr (parser, token->keyword);
3344 /* Objective-C++ expressions. */
3346 case RID_AT_PROTOCOL:
3347 case RID_AT_SELECTOR:
3348 return cp_parser_objc_expression (parser);
3351 cp_parser_error (parser, "expected primary-expression");
3352 return error_mark_node;
3355 /* An id-expression can start with either an identifier, a
3356 `::' as the beginning of a qualified-id, or the "operator"
3360 case CPP_TEMPLATE_ID:
3361 case CPP_NESTED_NAME_SPECIFIER:
3365 const char *error_msg;
3370 /* Parse the id-expression. */
3372 = cp_parser_id_expression (parser,
3373 /*template_keyword_p=*/false,
3374 /*check_dependency_p=*/true,
3376 /*declarator_p=*/false,
3377 /*optional_p=*/false);
3378 if (id_expression == error_mark_node)
3379 return error_mark_node;
3380 token = cp_lexer_peek_token (parser->lexer);
3381 done = (token->type != CPP_OPEN_SQUARE
3382 && token->type != CPP_OPEN_PAREN
3383 && token->type != CPP_DOT
3384 && token->type != CPP_DEREF
3385 && token->type != CPP_PLUS_PLUS
3386 && token->type != CPP_MINUS_MINUS);
3387 /* If we have a template-id, then no further lookup is
3388 required. If the template-id was for a template-class, we
3389 will sometimes have a TYPE_DECL at this point. */
3390 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3391 || TREE_CODE (id_expression) == TYPE_DECL)
3392 decl = id_expression;
3393 /* Look up the name. */
3396 tree ambiguous_decls;
3398 decl = cp_parser_lookup_name (parser, id_expression,
3401 /*is_namespace=*/false,
3402 /*check_dependency=*/true,
3404 /* If the lookup was ambiguous, an error will already have
3406 if (ambiguous_decls)
3407 return error_mark_node;
3409 /* In Objective-C++, an instance variable (ivar) may be preferred
3410 to whatever cp_parser_lookup_name() found. */
3411 decl = objc_lookup_ivar (decl, id_expression);
3413 /* If name lookup gives us a SCOPE_REF, then the
3414 qualifying scope was dependent. */
3415 if (TREE_CODE (decl) == SCOPE_REF)
3417 /* At this point, we do not know if DECL is a valid
3418 integral constant expression. We assume that it is
3419 in fact such an expression, so that code like:
3421 template <int N> struct A {
3425 is accepted. At template-instantiation time, we
3426 will check that B<N>::i is actually a constant. */
3429 /* Check to see if DECL is a local variable in a context
3430 where that is forbidden. */
3431 if (parser->local_variables_forbidden_p
3432 && local_variable_p (decl))
3434 /* It might be that we only found DECL because we are
3435 trying to be generous with pre-ISO scoping rules.
3436 For example, consider:
3440 for (int i = 0; i < 10; ++i) {}
3441 extern void f(int j = i);
3444 Here, name look up will originally find the out
3445 of scope `i'. We need to issue a warning message,
3446 but then use the global `i'. */
3447 decl = check_for_out_of_scope_variable (decl);
3448 if (local_variable_p (decl))
3450 error ("local variable %qD may not appear in this context",
3452 return error_mark_node;
3457 decl = (finish_id_expression
3458 (id_expression, decl, parser->scope,
3460 parser->integral_constant_expression_p,
3461 parser->allow_non_integral_constant_expression_p,
3462 &parser->non_integral_constant_expression_p,
3463 template_p, done, address_p,
3467 cp_parser_error (parser, error_msg);
3471 /* Anything else is an error. */
3473 /* ...unless we have an Objective-C++ message or string literal,
3475 if (c_dialect_objc ()
3476 && (token->type == CPP_OPEN_SQUARE
3477 || token->type == CPP_OBJC_STRING))
3478 return cp_parser_objc_expression (parser);
3480 cp_parser_error (parser, "expected primary-expression");
3481 return error_mark_node;
3485 /* Parse an id-expression.
3492 :: [opt] nested-name-specifier template [opt] unqualified-id
3494 :: operator-function-id
3497 Return a representation of the unqualified portion of the
3498 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3499 a `::' or nested-name-specifier.
3501 Often, if the id-expression was a qualified-id, the caller will
3502 want to make a SCOPE_REF to represent the qualified-id. This
3503 function does not do this in order to avoid wastefully creating
3504 SCOPE_REFs when they are not required.
3506 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3509 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3510 uninstantiated templates.
3512 If *TEMPLATE_P is non-NULL, it is set to true iff the
3513 `template' keyword is used to explicitly indicate that the entity
3514 named is a template.
3516 If DECLARATOR_P is true, the id-expression is appearing as part of
3517 a declarator, rather than as part of an expression. */
3520 cp_parser_id_expression (cp_parser *parser,
3521 bool template_keyword_p,
3522 bool check_dependency_p,
3527 bool global_scope_p;
3528 bool nested_name_specifier_p;
3530 /* Assume the `template' keyword was not used. */
3532 *template_p = template_keyword_p;
3534 /* Look for the optional `::' operator. */
3536 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3538 /* Look for the optional nested-name-specifier. */
3539 nested_name_specifier_p
3540 = (cp_parser_nested_name_specifier_opt (parser,
3541 /*typename_keyword_p=*/false,
3546 /* If there is a nested-name-specifier, then we are looking at
3547 the first qualified-id production. */
3548 if (nested_name_specifier_p)
3551 tree saved_object_scope;
3552 tree saved_qualifying_scope;
3553 tree unqualified_id;
3556 /* See if the next token is the `template' keyword. */
3558 template_p = &is_template;
3559 *template_p = cp_parser_optional_template_keyword (parser);
3560 /* Name lookup we do during the processing of the
3561 unqualified-id might obliterate SCOPE. */
3562 saved_scope = parser->scope;
3563 saved_object_scope = parser->object_scope;
3564 saved_qualifying_scope = parser->qualifying_scope;
3565 /* Process the final unqualified-id. */
3566 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3569 /*optional_p=*/false);
3570 /* Restore the SAVED_SCOPE for our caller. */
3571 parser->scope = saved_scope;
3572 parser->object_scope = saved_object_scope;
3573 parser->qualifying_scope = saved_qualifying_scope;
3575 return unqualified_id;
3577 /* Otherwise, if we are in global scope, then we are looking at one
3578 of the other qualified-id productions. */
3579 else if (global_scope_p)
3584 /* Peek at the next token. */
3585 token = cp_lexer_peek_token (parser->lexer);
3587 /* If it's an identifier, and the next token is not a "<", then
3588 we can avoid the template-id case. This is an optimization
3589 for this common case. */
3590 if (token->type == CPP_NAME
3591 && !cp_parser_nth_token_starts_template_argument_list_p
3593 return cp_parser_identifier (parser);
3595 cp_parser_parse_tentatively (parser);
3596 /* Try a template-id. */
3597 id = cp_parser_template_id (parser,
3598 /*template_keyword_p=*/false,
3599 /*check_dependency_p=*/true,
3601 /* If that worked, we're done. */
3602 if (cp_parser_parse_definitely (parser))
3605 /* Peek at the next token. (Changes in the token buffer may
3606 have invalidated the pointer obtained above.) */
3607 token = cp_lexer_peek_token (parser->lexer);
3609 switch (token->type)
3612 return cp_parser_identifier (parser);
3615 if (token->keyword == RID_OPERATOR)
3616 return cp_parser_operator_function_id (parser);
3620 cp_parser_error (parser, "expected id-expression");
3621 return error_mark_node;
3625 return cp_parser_unqualified_id (parser, template_keyword_p,
3626 /*check_dependency_p=*/true,
3631 /* Parse an unqualified-id.
3635 operator-function-id
3636 conversion-function-id
3640 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3641 keyword, in a construct like `A::template ...'.
3643 Returns a representation of unqualified-id. For the `identifier'
3644 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3645 production a BIT_NOT_EXPR is returned; the operand of the
3646 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3647 other productions, see the documentation accompanying the
3648 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3649 names are looked up in uninstantiated templates. If DECLARATOR_P
3650 is true, the unqualified-id is appearing as part of a declarator,
3651 rather than as part of an expression. */
3654 cp_parser_unqualified_id (cp_parser* parser,
3655 bool template_keyword_p,
3656 bool check_dependency_p,
3662 /* Peek at the next token. */
3663 token = cp_lexer_peek_token (parser->lexer);
3665 switch (token->type)
3671 /* We don't know yet whether or not this will be a
3673 cp_parser_parse_tentatively (parser);
3674 /* Try a template-id. */
3675 id = cp_parser_template_id (parser, template_keyword_p,
3678 /* If it worked, we're done. */
3679 if (cp_parser_parse_definitely (parser))
3681 /* Otherwise, it's an ordinary identifier. */
3682 return cp_parser_identifier (parser);
3685 case CPP_TEMPLATE_ID:
3686 return cp_parser_template_id (parser, template_keyword_p,
3693 tree qualifying_scope;
3698 /* Consume the `~' token. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the class-name. The standard, as written, seems to
3703 template <typename T> struct S { ~S (); };
3704 template <typename T> S<T>::~S() {}
3706 is invalid, since `~' must be followed by a class-name, but
3707 `S<T>' is dependent, and so not known to be a class.
3708 That's not right; we need to look in uninstantiated
3709 templates. A further complication arises from:
3711 template <typename T> void f(T t) {
3715 Here, it is not possible to look up `T' in the scope of `T'
3716 itself. We must look in both the current scope, and the
3717 scope of the containing complete expression.
3719 Yet another issue is:
3728 The standard does not seem to say that the `S' in `~S'
3729 should refer to the type `S' and not the data member
3732 /* DR 244 says that we look up the name after the "~" in the
3733 same scope as we looked up the qualifying name. That idea
3734 isn't fully worked out; it's more complicated than that. */
3735 scope = parser->scope;
3736 object_scope = parser->object_scope;
3737 qualifying_scope = parser->qualifying_scope;
3739 /* Check for invalid scopes. */
3740 if (scope == error_mark_node)
3742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3743 cp_lexer_consume_token (parser->lexer);
3744 return error_mark_node;
3746 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3748 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3749 error ("scope %qT before %<~%> is not a class-name", scope);
3750 cp_parser_simulate_error (parser);
3751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3752 cp_lexer_consume_token (parser->lexer);
3753 return error_mark_node;
3755 gcc_assert (!scope || TYPE_P (scope));
3757 /* If the name is of the form "X::~X" it's OK. */
3758 token = cp_lexer_peek_token (parser->lexer);
3760 && token->type == CPP_NAME
3761 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3763 && constructor_name_p (token->u.value, scope))
3765 cp_lexer_consume_token (parser->lexer);
3766 return build_nt (BIT_NOT_EXPR, scope);
3769 /* If there was an explicit qualification (S::~T), first look
3770 in the scope given by the qualification (i.e., S). */
3772 type_decl = NULL_TREE;
3775 cp_parser_parse_tentatively (parser);
3776 type_decl = cp_parser_class_name (parser,
3777 /*typename_keyword_p=*/false,
3778 /*template_keyword_p=*/false,
3780 /*check_dependency=*/false,
3781 /*class_head_p=*/false,
3783 if (cp_parser_parse_definitely (parser))
3786 /* In "N::S::~S", look in "N" as well. */
3787 if (!done && scope && qualifying_scope)
3789 cp_parser_parse_tentatively (parser);
3790 parser->scope = qualifying_scope;
3791 parser->object_scope = NULL_TREE;
3792 parser->qualifying_scope = NULL_TREE;
3794 = cp_parser_class_name (parser,
3795 /*typename_keyword_p=*/false,
3796 /*template_keyword_p=*/false,
3798 /*check_dependency=*/false,
3799 /*class_head_p=*/false,
3801 if (cp_parser_parse_definitely (parser))
3804 /* In "p->S::~T", look in the scope given by "*p" as well. */
3805 else if (!done && object_scope)
3807 cp_parser_parse_tentatively (parser);
3808 parser->scope = object_scope;
3809 parser->object_scope = NULL_TREE;
3810 parser->qualifying_scope = NULL_TREE;
3812 = cp_parser_class_name (parser,
3813 /*typename_keyword_p=*/false,
3814 /*template_keyword_p=*/false,
3816 /*check_dependency=*/false,
3817 /*class_head_p=*/false,
3819 if (cp_parser_parse_definitely (parser))
3822 /* Look in the surrounding context. */
3825 parser->scope = NULL_TREE;
3826 parser->object_scope = NULL_TREE;
3827 parser->qualifying_scope = NULL_TREE;
3829 = cp_parser_class_name (parser,
3830 /*typename_keyword_p=*/false,
3831 /*template_keyword_p=*/false,
3833 /*check_dependency=*/false,
3834 /*class_head_p=*/false,
3837 /* If an error occurred, assume that the name of the
3838 destructor is the same as the name of the qualifying
3839 class. That allows us to keep parsing after running
3840 into ill-formed destructor names. */
3841 if (type_decl == error_mark_node && scope)
3842 return build_nt (BIT_NOT_EXPR, scope);
3843 else if (type_decl == error_mark_node)
3844 return error_mark_node;
3846 /* Check that destructor name and scope match. */
3847 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3849 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3850 error ("declaration of %<~%T%> as member of %qT",
3852 cp_parser_simulate_error (parser);
3853 return error_mark_node;
3858 A typedef-name that names a class shall not be used as the
3859 identifier in the declarator for a destructor declaration. */
3861 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3862 && !DECL_SELF_REFERENCE_P (type_decl)
3863 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3864 error ("typedef-name %qD used as destructor declarator",
3867 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3871 if (token->keyword == RID_OPERATOR)
3875 /* This could be a template-id, so we try that first. */
3876 cp_parser_parse_tentatively (parser);
3877 /* Try a template-id. */
3878 id = cp_parser_template_id (parser, template_keyword_p,
3879 /*check_dependency_p=*/true,
3881 /* If that worked, we're done. */
3882 if (cp_parser_parse_definitely (parser))
3884 /* We still don't know whether we're looking at an
3885 operator-function-id or a conversion-function-id. */
3886 cp_parser_parse_tentatively (parser);
3887 /* Try an operator-function-id. */
3888 id = cp_parser_operator_function_id (parser);
3889 /* If that didn't work, try a conversion-function-id. */
3890 if (!cp_parser_parse_definitely (parser))
3891 id = cp_parser_conversion_function_id (parser);
3900 cp_parser_error (parser, "expected unqualified-id");
3901 return error_mark_node;
3905 /* Parse an (optional) nested-name-specifier.
3907 nested-name-specifier:
3908 class-or-namespace-name :: nested-name-specifier [opt]
3909 class-or-namespace-name :: template nested-name-specifier [opt]
3911 PARSER->SCOPE should be set appropriately before this function is
3912 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3913 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3916 Sets PARSER->SCOPE to the class (TYPE) or namespace
3917 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3918 it unchanged if there is no nested-name-specifier. Returns the new
3919 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3921 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3922 part of a declaration and/or decl-specifier. */
3925 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3926 bool typename_keyword_p,
3927 bool check_dependency_p,
3929 bool is_declaration)
3931 bool success = false;
3932 cp_token_position start = 0;
3935 /* Remember where the nested-name-specifier starts. */
3936 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3938 start = cp_lexer_token_position (parser->lexer, false);
3939 push_deferring_access_checks (dk_deferred);
3946 tree saved_qualifying_scope;
3947 bool template_keyword_p;
3949 /* Spot cases that cannot be the beginning of a
3950 nested-name-specifier. */
3951 token = cp_lexer_peek_token (parser->lexer);
3953 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3954 the already parsed nested-name-specifier. */
3955 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3957 /* Grab the nested-name-specifier and continue the loop. */
3958 cp_parser_pre_parsed_nested_name_specifier (parser);
3959 /* If we originally encountered this nested-name-specifier
3960 with IS_DECLARATION set to false, we will not have
3961 resolved TYPENAME_TYPEs, so we must do so here. */
3963 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3965 new_scope = resolve_typename_type (parser->scope,
3966 /*only_current_p=*/false);
3967 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3968 parser->scope = new_scope;
3974 /* Spot cases that cannot be the beginning of a
3975 nested-name-specifier. On the second and subsequent times
3976 through the loop, we look for the `template' keyword. */
3977 if (success && token->keyword == RID_TEMPLATE)
3979 /* A template-id can start a nested-name-specifier. */
3980 else if (token->type == CPP_TEMPLATE_ID)
3984 /* If the next token is not an identifier, then it is
3985 definitely not a class-or-namespace-name. */
3986 if (token->type != CPP_NAME)
3988 /* If the following token is neither a `<' (to begin a
3989 template-id), nor a `::', then we are not looking at a
3990 nested-name-specifier. */
3991 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3992 if (token->type != CPP_SCOPE
3993 && !cp_parser_nth_token_starts_template_argument_list_p
3998 /* The nested-name-specifier is optional, so we parse
4000 cp_parser_parse_tentatively (parser);
4002 /* Look for the optional `template' keyword, if this isn't the
4003 first time through the loop. */
4005 template_keyword_p = cp_parser_optional_template_keyword (parser);
4007 template_keyword_p = false;
4009 /* Save the old scope since the name lookup we are about to do
4010 might destroy it. */
4011 old_scope = parser->scope;
4012 saved_qualifying_scope = parser->qualifying_scope;
4013 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4014 look up names in "X<T>::I" in order to determine that "Y" is
4015 a template. So, if we have a typename at this point, we make
4016 an effort to look through it. */
4018 && !typename_keyword_p
4020 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4021 parser->scope = resolve_typename_type (parser->scope,
4022 /*only_current_p=*/false);
4023 /* Parse the qualifying entity. */
4025 = cp_parser_class_or_namespace_name (parser,
4031 /* Look for the `::' token. */
4032 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4034 /* If we found what we wanted, we keep going; otherwise, we're
4036 if (!cp_parser_parse_definitely (parser))
4038 bool error_p = false;
4040 /* Restore the OLD_SCOPE since it was valid before the
4041 failed attempt at finding the last
4042 class-or-namespace-name. */
4043 parser->scope = old_scope;
4044 parser->qualifying_scope = saved_qualifying_scope;
4045 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4047 /* If the next token is an identifier, and the one after
4048 that is a `::', then any valid interpretation would have
4049 found a class-or-namespace-name. */
4050 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4051 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4053 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4056 token = cp_lexer_consume_token (parser->lexer);
4059 if (!token->ambiguous_p)
4062 tree ambiguous_decls;
4064 decl = cp_parser_lookup_name (parser, token->u.value,
4066 /*is_template=*/false,
4067 /*is_namespace=*/false,
4068 /*check_dependency=*/true,
4070 if (TREE_CODE (decl) == TEMPLATE_DECL)
4071 error ("%qD used without template parameters", decl);
4072 else if (ambiguous_decls)
4074 error ("reference to %qD is ambiguous",
4076 print_candidates (ambiguous_decls);
4077 decl = error_mark_node;
4080 cp_parser_name_lookup_error
4081 (parser, token->u.value, decl,
4082 "is not a class or namespace");
4084 parser->scope = error_mark_node;
4086 /* Treat this as a successful nested-name-specifier
4091 If the name found is not a class-name (clause
4092 _class_) or namespace-name (_namespace.def_), the
4093 program is ill-formed. */
4096 cp_lexer_consume_token (parser->lexer);
4100 /* We've found one valid nested-name-specifier. */
4102 /* Name lookup always gives us a DECL. */
4103 if (TREE_CODE (new_scope) == TYPE_DECL)
4104 new_scope = TREE_TYPE (new_scope);
4105 /* Uses of "template" must be followed by actual templates. */
4106 if (template_keyword_p
4107 && !(CLASS_TYPE_P (new_scope)
4108 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4109 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4110 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4111 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4112 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4113 == TEMPLATE_ID_EXPR)))
4114 pedwarn (TYPE_P (new_scope)
4115 ? "%qT is not a template"
4116 : "%qD is not a template",
4118 /* If it is a class scope, try to complete it; we are about to
4119 be looking up names inside the class. */
4120 if (TYPE_P (new_scope)
4121 /* Since checking types for dependency can be expensive,
4122 avoid doing it if the type is already complete. */
4123 && !COMPLETE_TYPE_P (new_scope)
4124 /* Do not try to complete dependent types. */
4125 && !dependent_type_p (new_scope))
4127 new_scope = complete_type (new_scope);
4128 /* If it is a typedef to current class, use the current
4129 class instead, as the typedef won't have any names inside
4131 if (!COMPLETE_TYPE_P (new_scope)
4132 && currently_open_class (new_scope))
4133 new_scope = TYPE_MAIN_VARIANT (new_scope);
4135 /* Make sure we look in the right scope the next time through
4137 parser->scope = new_scope;
4140 /* If parsing tentatively, replace the sequence of tokens that makes
4141 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4142 token. That way, should we re-parse the token stream, we will
4143 not have to repeat the effort required to do the parse, nor will
4144 we issue duplicate error messages. */
4145 if (success && start)
4149 token = cp_lexer_token_at (parser->lexer, start);
4150 /* Reset the contents of the START token. */
4151 token->type = CPP_NESTED_NAME_SPECIFIER;
4152 /* Retrieve any deferred checks. Do not pop this access checks yet
4153 so the memory will not be reclaimed during token replacing below. */
4154 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4155 token->u.tree_check_value->value = parser->scope;
4156 token->u.tree_check_value->checks = get_deferred_access_checks ();
4157 token->u.tree_check_value->qualifying_scope =
4158 parser->qualifying_scope;
4159 token->keyword = RID_MAX;
4161 /* Purge all subsequent tokens. */
4162 cp_lexer_purge_tokens_after (parser->lexer, start);
4166 pop_to_parent_deferring_access_checks ();
4168 return success ? parser->scope : NULL_TREE;
4171 /* Parse a nested-name-specifier. See
4172 cp_parser_nested_name_specifier_opt for details. This function
4173 behaves identically, except that it will an issue an error if no
4174 nested-name-specifier is present. */