2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* The value associated with this token, if any. */
64 /* The location at which this token was found. */
68 /* We use a stack of token pointer for saving token sets. */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
73 static const cp_token eof_token =
75 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
83 /* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
85 it to the parser. Tokens are never added to the cp_lexer after
88 typedef struct cp_lexer GTY (())
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
92 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93 /* If the lexer owns the buffer, this is the number of tokens in the
97 /* A pointer just past the last available token. The tokens
98 in this lexer are [buffer, last_token). */
99 cp_token_position GTY ((skip)) last_token;
101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
102 no more available tokens. */
103 cp_token_position GTY ((skip)) next_token;
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
107 began saving tokens. If the stack is non-empty, we are saving
109 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
111 /* True if we should output debugging information. */
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer *next;
118 /* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
124 typedef struct cp_token_cache GTY(())
126 /* The beginning of the token range. */
127 cp_token * GTY((skip)) first;
129 /* Points immediately after the last token in the range. */
130 cp_token * GTY ((skip)) last;
135 static cp_lexer *cp_lexer_new_main
137 static cp_lexer *cp_lexer_new_from_tokens
138 (cp_token_cache *tokens);
139 static void cp_lexer_destroy
141 static int cp_lexer_saving_tokens
143 static cp_token_position cp_lexer_token_position
145 static cp_token *cp_lexer_token_at
146 (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148 (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
151 static cp_token *cp_lexer_peek_nth_token
152 (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156 (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158 (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
161 static void cp_lexer_purge_token
163 static void cp_lexer_purge_tokens_after
164 (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
167 static void cp_lexer_save_tokens
169 static void cp_lexer_commit_tokens
171 static void cp_lexer_rollback_tokens
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175 (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
178 static void cp_lexer_start_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181 (cp_lexer *) ATTRIBUTE_UNUSED;
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
192 static cp_token_cache *cp_token_cache_new
193 (cp_token *, cp_token *);
195 /* Manifest constants. */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
199 /* A token type for keywords, as opposed to ordinary identifiers. */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
202 /* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
208 /* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
215 /* A token type for tokens that are not tokens at all; these are used
216 to represent slots in the array where there used to be a token
217 that has now been deleted. */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
220 /* The number of token types, including C++-specific ones. */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written. */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
234 cp_lexer_new_main (void)
236 cp_token first_token;
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 /* Tell cpplib we want CPP_PRAGMA tokens. */
251 cpp_get_options (parse_in)->defer_pragmas = true;
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings = true;
256 c_common_no_more_pch ();
258 /* Allocate the memory. */
259 lexer = GGC_CNEW (cp_lexer);
261 #ifdef ENABLE_CHECKING
262 /* Initially we are not debugging. */
263 lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266 CP_SAVED_TOKEN_STACK);
268 /* Create the buffer. */
269 alloc = CP_LEXER_BUFFER_SIZE;
270 buffer = ggc_alloc (alloc * sizeof (cp_token));
272 /* Put the first token in the buffer. */
277 /* Get the remaining tokens from the preprocessor. */
278 while (pos->type != CPP_EOF)
285 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286 pos = buffer + space;
288 cp_lexer_get_preprocessor_token (lexer, pos);
290 lexer->buffer = buffer;
291 lexer->buffer_length = alloc - space;
292 lexer->last_token = pos;
293 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings = false;
300 gcc_assert (lexer->next_token->type != CPP_PURGED);
304 /* Create a new lexer whose token stream is primed with the tokens in
305 CACHE. When these tokens are exhausted, no new tokens will be read. */
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
310 cp_token *first = cache->first;
311 cp_token *last = cache->last;
312 cp_lexer *lexer = GGC_CNEW (cp_lexer);
314 /* We do not own the buffer. */
315 lexer->buffer = NULL;
316 lexer->buffer_length = 0;
317 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318 lexer->last_token = last;
320 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321 CP_SAVED_TOKEN_STACK);
323 #ifdef ENABLE_CHECKING
324 /* Initially we are not debugging. */
325 lexer->debugging_p = false;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
332 /* Frees all resources associated with LEXER. */
335 cp_lexer_destroy (cp_lexer *lexer)
338 ggc_free (lexer->buffer);
339 VEC_free (cp_token_position, heap, lexer->saved_tokens);
343 /* Returns nonzero if debugging information should be output. */
345 #ifdef ENABLE_CHECKING
348 cp_lexer_debugging_p (cp_lexer *lexer)
350 return lexer->debugging_p;
353 #endif /* ENABLE_CHECKING */
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
358 gcc_assert (!previous_p || lexer->next_token != &eof_token);
360 return lexer->next_token - previous_p;
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
369 /* nonzero if we are presently saving tokens. */
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
374 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
377 /* Store the next token from the preprocessor in *TOKEN. Return true
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
384 static int is_extern_c = 0;
386 /* Get a new token from the preprocessor. */
388 = c_lex_with_flags (&token->value, &token->location, &token->flags);
389 token->in_system_header = in_system_header;
391 /* On some systems, some header files are surrounded by an
392 implicit extern "C" block. Set a flag in the token if it
393 comes from such a header. */
394 is_extern_c += pending_lang_change;
395 pending_lang_change = 0;
396 token->implicit_extern_c = is_extern_c > 0;
398 /* Check to see if this token is a keyword. */
399 if (token->type == CPP_NAME
400 && C_IS_RESERVED_WORD (token->value))
402 /* Mark this token as a keyword. */
403 token->type = CPP_KEYWORD;
404 /* Record which keyword. */
405 token->keyword = C_RID_CODE (token->value);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token->value = ridpointers[token->keyword];
412 /* Handle Objective-C++ keywords. */
413 else if (token->type == CPP_AT_NAME)
415 token->type = CPP_KEYWORD;
416 switch (C_RID_CODE (token->value))
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423 case RID_THROW: token->keyword = RID_AT_THROW; break;
424 case RID_TRY: token->keyword = RID_AT_TRY; break;
425 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426 default: token->keyword = C_RID_CODE (token->value);
430 token->keyword = RID_MAX;
433 /* Update the globals input_location and in_system_header from TOKEN. */
435 cp_lexer_set_source_position_from_token (cp_token *token)
437 if (token->type != CPP_EOF)
439 input_location = token->location;
440 in_system_header = token->in_system_header;
444 /* Return a pointer to the next token in the token stream, but do not
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
450 if (cp_lexer_debugging_p (lexer))
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454 putc ('\n', cp_lexer_debug_stream);
456 return lexer->next_token;
459 /* Return true if the next token has the indicated TYPE. */
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
464 return cp_lexer_peek_token (lexer)->type == type;
467 /* Return true if the next token does not have the indicated TYPE. */
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
472 return !cp_lexer_next_token_is (lexer, type);
475 /* Return true if the next token is the indicated KEYWORD. */
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
482 /* Peek at the next token. */
483 token = cp_lexer_peek_token (lexer);
484 /* Check to see if it is the indicated keyword. */
485 return token->keyword == keyword;
488 /* Return a pointer to the Nth token in the token stream. If N is 1,
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
499 /* N is 1-based, not zero-based. */
500 gcc_assert (n > 0 && lexer->next_token != &eof_token);
502 if (cp_lexer_debugging_p (lexer))
503 fprintf (cp_lexer_debug_stream,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n);
507 token = lexer->next_token;
511 if (token == lexer->last_token)
513 token = (cp_token *)&eof_token;
517 if (token->type != CPP_PURGED)
521 if (cp_lexer_debugging_p (lexer))
523 cp_lexer_print_token (cp_lexer_debug_stream, token);
524 putc ('\n', cp_lexer_debug_stream);
530 /* Return the next token, and advance the lexer's next_token pointer
531 to point to the next non-purged token. */
534 cp_lexer_consume_token (cp_lexer* lexer)
536 cp_token *token = lexer->next_token;
538 gcc_assert (token != &eof_token);
543 if (lexer->next_token == lexer->last_token)
545 lexer->next_token = (cp_token *)&eof_token;
550 while (lexer->next_token->type == CPP_PURGED);
552 cp_lexer_set_source_position_from_token (token);
554 /* Provide debugging output. */
555 if (cp_lexer_debugging_p (lexer))
557 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558 cp_lexer_print_token (cp_lexer_debug_stream, token);
559 putc ('\n', cp_lexer_debug_stream);
565 /* Permanently remove the next token from the token stream, and
566 advance the next_token pointer to refer to the next non-purged
570 cp_lexer_purge_token (cp_lexer *lexer)
572 cp_token *tok = lexer->next_token;
574 gcc_assert (tok != &eof_token);
575 tok->type = CPP_PURGED;
576 tok->location = UNKNOWN_LOCATION;
577 tok->value = NULL_TREE;
578 tok->keyword = RID_MAX;
583 if (tok == lexer->last_token)
585 tok = (cp_token *)&eof_token;
589 while (tok->type == CPP_PURGED);
590 lexer->next_token = tok;
593 /* Permanently remove all tokens after TOK, up to, but not
594 including, the token that will be returned next by
595 cp_lexer_peek_token. */
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
600 cp_token *peek = lexer->next_token;
602 if (peek == &eof_token)
603 peek = lexer->last_token;
605 gcc_assert (tok < peek);
607 for ( tok += 1; tok != peek; tok += 1)
609 tok->type = CPP_PURGED;
610 tok->location = UNKNOWN_LOCATION;
611 tok->value = NULL_TREE;
612 tok->keyword = RID_MAX;
616 /* Consume and handle a pragma token. */
618 cp_lexer_handle_pragma (cp_lexer *lexer)
621 cp_token *token = cp_lexer_consume_token (lexer);
622 gcc_assert (token->type == CPP_PRAGMA);
623 gcc_assert (token->value);
625 s.len = TREE_STRING_LENGTH (token->value);
626 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
628 cpp_handle_deferred_pragma (parse_in, &s);
630 /* Clearing token->value here means that we will get an ICE if we
631 try to process this #pragma again (which should be impossible). */
635 /* Begin saving tokens. All tokens consumed after this point will be
639 cp_lexer_save_tokens (cp_lexer* lexer)
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer))
643 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
645 VEC_safe_push (cp_token_position, heap,
646 lexer->saved_tokens, lexer->next_token);
649 /* Commit to the portion of the token stream most recently saved. */
652 cp_lexer_commit_tokens (cp_lexer* lexer)
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
656 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
658 VEC_pop (cp_token_position, lexer->saved_tokens);
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662 to the token stream. Stop saving tokens. */
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
667 /* Provide debugging output. */
668 if (cp_lexer_debugging_p (lexer))
669 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
671 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
674 /* Print a representation of the TOKEN on the STREAM. */
676 #ifdef ENABLE_CHECKING
679 cp_lexer_print_token (FILE * stream, cp_token *token)
681 /* We don't use cpp_type2name here because the parser defines
682 a few tokens of its own. */
683 static const char *const token_names[] = {
684 /* cpplib-defined token types */
690 /* C++ parser token types - see "Manifest constants", above. */
693 "NESTED_NAME_SPECIFIER",
697 /* If we have a name for the token, print it out. Otherwise, we
698 simply give the numeric code. */
699 gcc_assert (token->type < ARRAY_SIZE(token_names));
700 fputs (token_names[token->type], stream);
702 /* For some tokens, print the associated data. */
706 /* Some keywords have a value that is not an IDENTIFIER_NODE.
707 For example, `struct' is mapped to an INTEGER_CST. */
708 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710 /* else fall through */
712 fputs (IDENTIFIER_POINTER (token->value), stream);
718 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
726 /* Start emitting debugging information. */
729 cp_lexer_start_debugging (cp_lexer* lexer)
731 lexer->debugging_p = true;
734 /* Stop emitting debugging information. */
737 cp_lexer_stop_debugging (cp_lexer* lexer)
739 lexer->debugging_p = false;
742 #endif /* ENABLE_CHECKING */
744 /* Create a new cp_token_cache, representing a range of tokens. */
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
749 cp_token_cache *cache = GGC_NEW (cp_token_cache);
750 cache->first = first;
756 /* Decl-specifiers. */
758 static void clear_decl_specs
759 (cp_decl_specifier_seq *);
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
771 /* Nothing other than the parser should be creating declarators;
772 declarators are a semi-syntactic representation of C++ entities.
773 Other parts of the front end that need to create entities (like
774 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
776 static cp_declarator *make_call_declarator
777 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779 (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781 (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783 (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785 (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787 (cp_cv_quals, tree, cp_declarator *);
789 cp_declarator *cp_error_declarator;
791 /* The obstack on which declarators and related data structures are
793 static struct obstack declarator_obstack;
795 /* Alloc BYTES from the declarator memory pool. */
798 alloc_declarator (size_t bytes)
800 return obstack_alloc (&declarator_obstack, bytes);
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
809 cp_declarator *declarator;
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
819 /* Make a declarator for a generalized identifier. If non-NULL, the
820 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821 just UNQUALIFIED_NAME. */
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
826 cp_declarator *declarator;
828 /* It is valid to write:
830 class C { void f(); };
834 The standard is not clear about whether `typedef const C D' is
835 legal; as of 2002-09-15 the committee is considering that
836 question. EDG 3.0 allows that syntax. Therefore, we do as
838 if (qualifying_scope && TYPE_P (qualifying_scope))
839 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
841 declarator = make_declarator (cdk_id);
842 declarator->u.id.qualifying_scope = qualifying_scope;
843 declarator->u.id.unqualified_name = unqualified_name;
844 declarator->u.id.sfk = sfk_none;
849 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
850 of modifiers such as const or volatile to apply to the pointer
851 type, represented as identifiers. */
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
856 cp_declarator *declarator;
858 declarator = make_declarator (cdk_pointer);
859 declarator->declarator = target;
860 declarator->u.pointer.qualifiers = cv_qualifiers;
861 declarator->u.pointer.class_type = NULL_TREE;
866 /* Like make_pointer_declarator -- but for references. */
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
871 cp_declarator *declarator;
873 declarator = make_declarator (cdk_reference);
874 declarator->declarator = target;
875 declarator->u.pointer.qualifiers = cv_qualifiers;
876 declarator->u.pointer.class_type = NULL_TREE;
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882 member of CLASS_TYPE. */
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886 cp_declarator *pointee)
888 cp_declarator *declarator;
890 declarator = make_declarator (cdk_ptrmem);
891 declarator->declarator = pointee;
892 declarator->u.pointer.qualifiers = cv_qualifiers;
893 declarator->u.pointer.class_type = class_type;
898 /* Make a declarator for the function given by TARGET, with the
899 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
900 "const"-qualified member function. The EXCEPTION_SPECIFICATION
901 indicates what exceptions can be thrown. */
904 make_call_declarator (cp_declarator *target,
905 cp_parameter_declarator *parms,
906 cp_cv_quals cv_qualifiers,
907 tree exception_specification)
909 cp_declarator *declarator;
911 declarator = make_declarator (cdk_function);
912 declarator->declarator = target;
913 declarator->u.function.parameters = parms;
914 declarator->u.function.qualifiers = cv_qualifiers;
915 declarator->u.function.exception_specification = exception_specification;
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921 defined by ELEMENT. */
924 make_array_declarator (cp_declarator *element, tree bounds)
926 cp_declarator *declarator;
928 declarator = make_declarator (cdk_array);
929 declarator->declarator = element;
930 declarator->u.array.bounds = bounds;
935 cp_parameter_declarator *no_parameters;
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938 DECLARATOR and DEFAULT_ARGUMENT. */
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942 cp_declarator *declarator,
943 tree default_argument)
945 cp_parameter_declarator *parameter;
947 parameter = ((cp_parameter_declarator *)
948 alloc_declarator (sizeof (cp_parameter_declarator)));
949 parameter->next = NULL;
951 parameter->decl_specifiers = *decl_specifiers;
953 clear_decl_specs (¶meter->decl_specifiers);
954 parameter->declarator = declarator;
955 parameter->default_argument = default_argument;
956 parameter->ellipsis_p = false;
966 A cp_parser parses the token stream as specified by the C++
967 grammar. Its job is purely parsing, not semantic analysis. For
968 example, the parser breaks the token stream into declarators,
969 expressions, statements, and other similar syntactic constructs.
970 It does not check that the types of the expressions on either side
971 of an assignment-statement are compatible, or that a function is
972 not declared with a parameter of type `void'.
974 The parser invokes routines elsewhere in the compiler to perform
975 semantic analysis and to build up the abstract syntax tree for the
978 The parser (and the template instantiation code, which is, in a
979 way, a close relative of parsing) are the only parts of the
980 compiler that should be calling push_scope and pop_scope, or
981 related functions. The parser (and template instantiation code)
982 keeps track of what scope is presently active; everything else
983 should simply honor that. (The code that generates static
984 initializers may also need to set the scope, in order to check
985 access control correctly when emitting the initializers.)
990 The parser is of the standard recursive-descent variety. Upcoming
991 tokens in the token stream are examined in order to determine which
992 production to use when parsing a non-terminal. Some C++ constructs
993 require arbitrary look ahead to disambiguate. For example, it is
994 impossible, in the general case, to tell whether a statement is an
995 expression or declaration without scanning the entire statement.
996 Therefore, the parser is capable of "parsing tentatively." When the
997 parser is not sure what construct comes next, it enters this mode.
998 Then, while we attempt to parse the construct, the parser queues up
999 error messages, rather than issuing them immediately, and saves the
1000 tokens it consumes. If the construct is parsed successfully, the
1001 parser "commits", i.e., it issues any queued error messages and
1002 the tokens that were being preserved are permanently discarded.
1003 If, however, the construct is not parsed successfully, the parser
1004 rolls back its state completely so that it can resume parsing using
1005 a different alternative.
1010 The performance of the parser could probably be improved substantially.
1011 We could often eliminate the need to parse tentatively by looking ahead
1012 a little bit. In some places, this approach might not entirely eliminate
1013 the need to parse tentatively, but it might still speed up the average
1016 /* Flags that are passed to some parsing functions. These values can
1017 be bitwise-ored together. */
1019 typedef enum cp_parser_flags
1022 CP_PARSER_FLAGS_NONE = 0x0,
1023 /* The construct is optional. If it is not present, then no error
1024 should be issued. */
1025 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026 /* When parsing a type-specifier, do not allow user-defined types. */
1027 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030 /* The different kinds of declarators we want to parse. */
1032 typedef enum cp_parser_declarator_kind
1034 /* We want an abstract declarator. */
1035 CP_PARSER_DECLARATOR_ABSTRACT,
1036 /* We want a named declarator. */
1037 CP_PARSER_DECLARATOR_NAMED,
1038 /* We don't mind, but the name must be an unqualified-id. */
1039 CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1042 /* The precedence values used to parse binary expressions. The minimum value
1043 of PREC must be 1, because zero is reserved to quickly discriminate
1044 binary operators from other tokens. */
1049 PREC_LOGICAL_OR_EXPRESSION,
1050 PREC_LOGICAL_AND_EXPRESSION,
1051 PREC_INCLUSIVE_OR_EXPRESSION,
1052 PREC_EXCLUSIVE_OR_EXPRESSION,
1053 PREC_AND_EXPRESSION,
1054 PREC_EQUALITY_EXPRESSION,
1055 PREC_RELATIONAL_EXPRESSION,
1056 PREC_SHIFT_EXPRESSION,
1057 PREC_ADDITIVE_EXPRESSION,
1058 PREC_MULTIPLICATIVE_EXPRESSION,
1060 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064 precedence value. */
1066 typedef struct cp_parser_binary_operations_map_node
1068 /* The token type. */
1069 enum cpp_ttype token_type;
1070 /* The corresponding tree code. */
1071 enum tree_code tree_type;
1072 /* The precedence of this operator. */
1073 enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1076 /* The status of a tentative parse. */
1078 typedef enum cp_parser_status_kind
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR,
1084 /* We are committed to this tentative parse, whether or not an error
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1089 typedef struct cp_parser_expression_stack_entry
1092 enum tree_code tree_type;
1094 } cp_parser_expression_stack_entry;
1096 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1097 entries because precedence levels on the stack are monotonically
1099 typedef struct cp_parser_expression_stack_entry
1100 cp_parser_expression_stack[NUM_PREC_VALUES];
1102 /* Context that is saved and restored when parsing tentatively. */
1103 typedef struct cp_parser_context GTY (())
1105 /* If this is a tentative parsing context, the status of the
1107 enum cp_parser_status_kind status;
1108 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1109 that are looked up in this context must be looked up both in the
1110 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111 the context of the containing expression. */
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116 } cp_parser_context;
1120 /* Constructors and destructors. */
1122 static cp_parser_context *cp_parser_context_new
1123 (cp_parser_context *);
1125 /* Class variables. */
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130 Transformed into an associative array (binops_by_token) by
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 /* The same as binops, but initialized by cp_parser_new so that
1169 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173 /* Constructors and destructors. */
1175 /* Construct a new context. The context below this one on the stack
1176 is given by NEXT. */
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1181 cp_parser_context *context;
1183 /* Allocate the storage. */
1184 if (cp_parser_context_free_list != NULL)
1186 /* Pull the first entry from the free list. */
1187 context = cp_parser_context_free_list;
1188 cp_parser_context_free_list = context->next;
1189 memset (context, 0, sizeof (*context));
1192 context = GGC_CNEW (cp_parser_context);
1194 /* No errors have occurred yet in this context. */
1195 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196 /* If this is not the bottomost context, copy information that we
1197 need from the previous context. */
1200 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201 expression, then we are parsing one in this context, too. */
1202 context->object_type = next->object_type;
1203 /* Thread the stack. */
1204 context->next = next;
1210 /* The cp_parser structure represents the C++ parser. */
1212 typedef struct cp_parser GTY(())
1214 /* The lexer from which we are obtaining tokens. */
1217 /* The scope in which names should be looked up. If NULL_TREE, then
1218 we look up names in the scope that is currently open in the
1219 source program. If non-NULL, this is either a TYPE or
1220 NAMESPACE_DECL for the scope in which we should look.
1222 This value is not cleared automatically after a name is looked
1223 up, so we must be careful to clear it before starting a new look
1224 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1225 will look up `Z' in the scope of `X', rather than the current
1226 scope.) Unfortunately, it is difficult to tell when name lookup
1227 is complete, because we sometimes peek at a token, look it up,
1228 and then decide not to consume it. */
1231 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1232 last lookup took place. OBJECT_SCOPE is used if an expression
1233 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1234 respectively. QUALIFYING_SCOPE is used for an expression of the
1235 form "X::Y"; it refers to X. */
1237 tree qualifying_scope;
1239 /* A stack of parsing contexts. All but the bottom entry on the
1240 stack will be tentative contexts.
1242 We parse tentatively in order to determine which construct is in
1243 use in some situations. For example, in order to determine
1244 whether a statement is an expression-statement or a
1245 declaration-statement we parse it tentatively as a
1246 declaration-statement. If that fails, we then reparse the same
1247 token stream as an expression-statement. */
1248 cp_parser_context *context;
1250 /* True if we are parsing GNU C++. If this flag is not set, then
1251 GNU extensions are not recognized. */
1252 bool allow_gnu_extensions_p;
1254 /* TRUE if the `>' token should be interpreted as the greater-than
1255 operator. FALSE if it is the end of a template-id or
1256 template-parameter-list. */
1257 bool greater_than_is_operator_p;
1259 /* TRUE if default arguments are allowed within a parameter list
1260 that starts at this point. FALSE if only a gnu extension makes
1261 them permissible. */
1262 bool default_arg_ok_p;
1264 /* TRUE if we are parsing an integral constant-expression. See
1265 [expr.const] for a precise definition. */
1266 bool integral_constant_expression_p;
1268 /* TRUE if we are parsing an integral constant-expression -- but a
1269 non-constant expression should be permitted as well. This flag
1270 is used when parsing an array bound so that GNU variable-length
1271 arrays are tolerated. */
1272 bool allow_non_integral_constant_expression_p;
1274 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1275 been seen that makes the expression non-constant. */
1276 bool non_integral_constant_expression_p;
1278 /* TRUE if local variable names and `this' are forbidden in the
1280 bool local_variables_forbidden_p;
1282 /* TRUE if the declaration we are parsing is part of a
1283 linkage-specification of the form `extern string-literal
1285 bool in_unbraced_linkage_specification_p;
1287 /* TRUE if we are presently parsing a declarator, after the
1288 direct-declarator. */
1289 bool in_declarator_p;
1291 /* TRUE if we are presently parsing a template-argument-list. */
1292 bool in_template_argument_list_p;
1294 /* TRUE if we are presently parsing the body of an
1295 iteration-statement. */
1296 bool in_iteration_statement_p;
1298 /* TRUE if we are presently parsing the body of a switch
1300 bool in_switch_statement_p;
1302 /* TRUE if we are parsing a type-id in an expression context. In
1303 such a situation, both "type (expr)" and "type (type)" are valid
1305 bool in_type_id_in_expr_p;
1307 /* TRUE if we are currently in a header file where declarations are
1308 implicitly extern "C". */
1309 bool implicit_extern_c;
1311 /* TRUE if strings in expressions should be translated to the execution
1313 bool translate_strings_p;
1315 /* If non-NULL, then we are parsing a construct where new type
1316 definitions are not permitted. The string stored here will be
1317 issued as an error message if a type is defined. */
1318 const char *type_definition_forbidden_message;
1320 /* A list of lists. The outer list is a stack, used for member
1321 functions of local classes. At each level there are two sub-list,
1322 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1323 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1324 TREE_VALUE's. The functions are chained in reverse declaration
1327 The TREE_PURPOSE sublist contains those functions with default
1328 arguments that need post processing, and the TREE_VALUE sublist
1329 contains those functions with definitions that need post
1332 These lists can only be processed once the outermost class being
1333 defined is complete. */
1334 tree unparsed_functions_queues;
1336 /* The number of classes whose definitions are currently in
1338 unsigned num_classes_being_defined;
1340 /* The number of template parameter lists that apply directly to the
1341 current declaration. */
1342 unsigned num_template_parameter_lists;
1345 /* The type of a function that parses some kind of expression. */
1346 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1350 /* Constructors and destructors. */
1352 static cp_parser *cp_parser_new
1355 /* Routines to parse various constructs.
1357 Those that return `tree' will return the error_mark_node (rather
1358 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1359 Sometimes, they will return an ordinary node if error-recovery was
1360 attempted, even though a parse error occurred. So, to check
1361 whether or not a parse error occurred, you should always use
1362 cp_parser_error_occurred. If the construct is optional (indicated
1363 either by an `_opt' in the name of the function that does the
1364 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1365 the construct is not present. */
1367 /* Lexical conventions [gram.lex] */
1369 static tree cp_parser_identifier
1371 static tree cp_parser_string_literal
1372 (cp_parser *, bool, bool);
1374 /* Basic concepts [gram.basic] */
1376 static bool cp_parser_translation_unit
1379 /* Expressions [gram.expr] */
1381 static tree cp_parser_primary_expression
1382 (cp_parser *, bool, cp_id_kind *, tree *);
1383 static tree cp_parser_id_expression
1384 (cp_parser *, bool, bool, bool *, bool);
1385 static tree cp_parser_unqualified_id
1386 (cp_parser *, bool, bool, bool);
1387 static tree cp_parser_nested_name_specifier_opt
1388 (cp_parser *, bool, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier
1390 (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_class_or_namespace_name
1392 (cp_parser *, bool, bool, bool, bool, bool);
1393 static tree cp_parser_postfix_expression
1394 (cp_parser *, bool, bool);
1395 static tree cp_parser_postfix_open_square_expression
1396 (cp_parser *, tree, bool);
1397 static tree cp_parser_postfix_dot_deref_expression
1398 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1399 static tree cp_parser_parenthesized_expression_list
1400 (cp_parser *, bool, bool, bool *);
1401 static void cp_parser_pseudo_destructor_name
1402 (cp_parser *, tree *, tree *);
1403 static tree cp_parser_unary_expression
1404 (cp_parser *, bool, bool);
1405 static enum tree_code cp_parser_unary_operator
1407 static tree cp_parser_new_expression
1409 static tree cp_parser_new_placement
1411 static tree cp_parser_new_type_id
1412 (cp_parser *, tree *);
1413 static cp_declarator *cp_parser_new_declarator_opt
1415 static cp_declarator *cp_parser_direct_new_declarator
1417 static tree cp_parser_new_initializer
1419 static tree cp_parser_delete_expression
1421 static tree cp_parser_cast_expression
1422 (cp_parser *, bool, bool);
1423 static tree cp_parser_binary_expression
1424 (cp_parser *, bool);
1425 static tree cp_parser_question_colon_clause
1426 (cp_parser *, tree);
1427 static tree cp_parser_assignment_expression
1428 (cp_parser *, bool);
1429 static enum tree_code cp_parser_assignment_operator_opt
1431 static tree cp_parser_expression
1432 (cp_parser *, bool);
1433 static tree cp_parser_constant_expression
1434 (cp_parser *, bool, bool *);
1435 static tree cp_parser_builtin_offsetof
1438 /* Statements [gram.stmt.stmt] */
1440 static void cp_parser_statement
1441 (cp_parser *, tree);
1442 static tree cp_parser_labeled_statement
1443 (cp_parser *, tree);
1444 static tree cp_parser_expression_statement
1445 (cp_parser *, tree);
1446 static tree cp_parser_compound_statement
1447 (cp_parser *, tree, bool);
1448 static void cp_parser_statement_seq_opt
1449 (cp_parser *, tree);
1450 static tree cp_parser_selection_statement
1452 static tree cp_parser_condition
1454 static tree cp_parser_iteration_statement
1456 static void cp_parser_for_init_statement
1458 static tree cp_parser_jump_statement
1460 static void cp_parser_declaration_statement
1463 static tree cp_parser_implicitly_scoped_statement
1465 static void cp_parser_already_scoped_statement
1468 /* Declarations [gram.dcl.dcl] */
1470 static void cp_parser_declaration_seq_opt
1472 static void cp_parser_declaration
1474 static void cp_parser_block_declaration
1475 (cp_parser *, bool);
1476 static void cp_parser_simple_declaration
1477 (cp_parser *, bool);
1478 static void cp_parser_decl_specifier_seq
1479 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1480 static tree cp_parser_storage_class_specifier_opt
1482 static tree cp_parser_function_specifier_opt
1483 (cp_parser *, cp_decl_specifier_seq *);
1484 static tree cp_parser_type_specifier
1485 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1487 static tree cp_parser_simple_type_specifier
1488 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1489 static tree cp_parser_type_name
1491 static tree cp_parser_elaborated_type_specifier
1492 (cp_parser *, bool, bool);
1493 static tree cp_parser_enum_specifier
1495 static void cp_parser_enumerator_list
1496 (cp_parser *, tree);
1497 static void cp_parser_enumerator_definition
1498 (cp_parser *, tree);
1499 static tree cp_parser_namespace_name
1501 static void cp_parser_namespace_definition
1503 static void cp_parser_namespace_body
1505 static tree cp_parser_qualified_namespace_specifier
1507 static void cp_parser_namespace_alias_definition
1509 static void cp_parser_using_declaration
1511 static void cp_parser_using_directive
1513 static void cp_parser_asm_definition
1515 static void cp_parser_linkage_specification
1518 /* Declarators [gram.dcl.decl] */
1520 static tree cp_parser_init_declarator
1521 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1522 static cp_declarator *cp_parser_declarator
1523 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1524 static cp_declarator *cp_parser_direct_declarator
1525 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1526 static enum tree_code cp_parser_ptr_operator
1527 (cp_parser *, tree *, cp_cv_quals *);
1528 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1530 static tree cp_parser_declarator_id
1532 static tree cp_parser_type_id
1534 static void cp_parser_type_specifier_seq
1535 (cp_parser *, bool, cp_decl_specifier_seq *);
1536 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1539 (cp_parser *, bool *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration
1541 (cp_parser *, bool, bool *);
1542 static void cp_parser_function_body
1544 static tree cp_parser_initializer
1545 (cp_parser *, bool *, bool *);
1546 static tree cp_parser_initializer_clause
1547 (cp_parser *, bool *);
1548 static tree cp_parser_initializer_list
1549 (cp_parser *, bool *);
1551 static bool cp_parser_ctor_initializer_opt_and_function_body
1554 /* Classes [gram.class] */
1556 static tree cp_parser_class_name
1557 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1558 static tree cp_parser_class_specifier
1560 static tree cp_parser_class_head
1561 (cp_parser *, bool *, tree *);
1562 static enum tag_types cp_parser_class_key
1564 static void cp_parser_member_specification_opt
1566 static void cp_parser_member_declaration
1568 static tree cp_parser_pure_specifier
1570 static tree cp_parser_constant_initializer
1573 /* Derived classes [gram.class.derived] */
1575 static tree cp_parser_base_clause
1577 static tree cp_parser_base_specifier
1580 /* Special member functions [gram.special] */
1582 static tree cp_parser_conversion_function_id
1584 static tree cp_parser_conversion_type_id
1586 static cp_declarator *cp_parser_conversion_declarator_opt
1588 static bool cp_parser_ctor_initializer_opt
1590 static void cp_parser_mem_initializer_list
1592 static tree cp_parser_mem_initializer
1594 static tree cp_parser_mem_initializer_id
1597 /* Overloading [gram.over] */
1599 static tree cp_parser_operator_function_id
1601 static tree cp_parser_operator
1604 /* Templates [gram.temp] */
1606 static void cp_parser_template_declaration
1607 (cp_parser *, bool);
1608 static tree cp_parser_template_parameter_list
1610 static tree cp_parser_template_parameter
1611 (cp_parser *, bool *);
1612 static tree cp_parser_type_parameter
1614 static tree cp_parser_template_id
1615 (cp_parser *, bool, bool, bool);
1616 static tree cp_parser_template_name
1617 (cp_parser *, bool, bool, bool, bool *);
1618 static tree cp_parser_template_argument_list
1620 static tree cp_parser_template_argument
1622 static void cp_parser_explicit_instantiation
1624 static void cp_parser_explicit_specialization
1627 /* Exception handling [gram.exception] */
1629 static tree cp_parser_try_block
1631 static bool cp_parser_function_try_block
1633 static void cp_parser_handler_seq
1635 static void cp_parser_handler
1637 static tree cp_parser_exception_declaration
1639 static tree cp_parser_throw_expression
1641 static tree cp_parser_exception_specification_opt
1643 static tree cp_parser_type_id_list
1646 /* GNU Extensions */
1648 static tree cp_parser_asm_specification_opt
1650 static tree cp_parser_asm_operand_list
1652 static tree cp_parser_asm_clobber_list
1654 static tree cp_parser_attributes_opt
1656 static tree cp_parser_attribute_list
1658 static bool cp_parser_extension_opt
1659 (cp_parser *, int *);
1660 static void cp_parser_label_declaration
1663 /* Objective-C++ Productions */
1665 static tree cp_parser_objc_message_receiver
1667 static tree cp_parser_objc_message_args
1669 static tree cp_parser_objc_message_expression
1671 static tree cp_parser_objc_encode_expression
1673 static tree cp_parser_objc_defs_expression
1675 static tree cp_parser_objc_protocol_expression
1677 static tree cp_parser_objc_selector_expression
1679 static tree cp_parser_objc_expression
1681 static bool cp_parser_objc_selector_p
1683 static tree cp_parser_objc_selector
1685 static tree cp_parser_objc_protocol_refs_opt
1687 static void cp_parser_objc_declaration
1689 static tree cp_parser_objc_statement
1692 /* Utility Routines */
1694 static tree cp_parser_lookup_name
1695 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1696 static tree cp_parser_lookup_name_simple
1697 (cp_parser *, tree);
1698 static tree cp_parser_maybe_treat_template_as_class
1700 static bool cp_parser_check_declarator_template_parameters
1701 (cp_parser *, cp_declarator *);
1702 static bool cp_parser_check_template_parameters
1703 (cp_parser *, unsigned);
1704 static tree cp_parser_simple_cast_expression
1706 static tree cp_parser_global_scope_opt
1707 (cp_parser *, bool);
1708 static bool cp_parser_constructor_declarator_p
1709 (cp_parser *, bool);
1710 static tree cp_parser_function_definition_from_specifiers_and_declarator
1711 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1712 static tree cp_parser_function_definition_after_declarator
1713 (cp_parser *, bool);
1714 static void cp_parser_template_declaration_after_export
1715 (cp_parser *, bool);
1716 static tree cp_parser_single_declaration
1717 (cp_parser *, bool, bool *);
1718 static tree cp_parser_functional_cast
1719 (cp_parser *, tree);
1720 static tree cp_parser_save_member_function_body
1721 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1722 static tree cp_parser_enclosed_template_argument_list
1724 static void cp_parser_save_default_args
1725 (cp_parser *, tree);
1726 static void cp_parser_late_parsing_for_member
1727 (cp_parser *, tree);
1728 static void cp_parser_late_parsing_default_args
1729 (cp_parser *, tree);
1730 static tree cp_parser_sizeof_operand
1731 (cp_parser *, enum rid);
1732 static bool cp_parser_declares_only_class_p
1734 static void cp_parser_set_storage_class
1735 (cp_decl_specifier_seq *, cp_storage_class);
1736 static void cp_parser_set_decl_spec_type
1737 (cp_decl_specifier_seq *, tree, bool);
1738 static bool cp_parser_friend_p
1739 (const cp_decl_specifier_seq *);
1740 static cp_token *cp_parser_require
1741 (cp_parser *, enum cpp_ttype, const char *);
1742 static cp_token *cp_parser_require_keyword
1743 (cp_parser *, enum rid, const char *);
1744 static bool cp_parser_token_starts_function_definition_p
1746 static bool cp_parser_next_token_starts_class_definition_p
1748 static bool cp_parser_next_token_ends_template_argument_p
1750 static bool cp_parser_nth_token_starts_template_argument_list_p
1751 (cp_parser *, size_t);
1752 static enum tag_types cp_parser_token_is_class_key
1754 static void cp_parser_check_class_key
1755 (enum tag_types, tree type);
1756 static void cp_parser_check_access_in_redeclaration
1758 static bool cp_parser_optional_template_keyword
1760 static void cp_parser_pre_parsed_nested_name_specifier
1762 static void cp_parser_cache_group
1763 (cp_parser *, enum cpp_ttype, unsigned);
1764 static void cp_parser_parse_tentatively
1766 static void cp_parser_commit_to_tentative_parse
1768 static void cp_parser_abort_tentative_parse
1770 static bool cp_parser_parse_definitely
1772 static inline bool cp_parser_parsing_tentatively
1774 static bool cp_parser_uncommitted_to_tentative_parse_p
1776 static void cp_parser_error
1777 (cp_parser *, const char *);
1778 static void cp_parser_name_lookup_error
1779 (cp_parser *, tree, tree, const char *);
1780 static bool cp_parser_simulate_error
1782 static void cp_parser_check_type_definition
1784 static void cp_parser_check_for_definition_in_return_type
1785 (cp_declarator *, tree);
1786 static void cp_parser_check_for_invalid_template_id
1787 (cp_parser *, tree);
1788 static bool cp_parser_non_integral_constant_expression
1789 (cp_parser *, const char *);
1790 static void cp_parser_diagnose_invalid_type_name
1791 (cp_parser *, tree, tree);
1792 static bool cp_parser_parse_and_diagnose_invalid_type_name
1794 static int cp_parser_skip_to_closing_parenthesis
1795 (cp_parser *, bool, bool, bool);
1796 static void cp_parser_skip_to_end_of_statement
1798 static void cp_parser_consume_semicolon_at_end_of_statement
1800 static void cp_parser_skip_to_end_of_block_or_statement
1802 static void cp_parser_skip_to_closing_brace
1804 static void cp_parser_skip_until_found
1805 (cp_parser *, enum cpp_ttype, const char *);
1806 static bool cp_parser_error_occurred
1808 static bool cp_parser_allow_gnu_extensions_p
1810 static bool cp_parser_is_string_literal
1812 static bool cp_parser_is_keyword
1813 (cp_token *, enum rid);
1814 static tree cp_parser_make_typename_type
1815 (cp_parser *, tree, tree);
1817 /* Returns nonzero if we are parsing tentatively. */
1820 cp_parser_parsing_tentatively (cp_parser* parser)
1822 return parser->context->next != NULL;
1825 /* Returns nonzero if TOKEN is a string literal. */
1828 cp_parser_is_string_literal (cp_token* token)
1830 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1833 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1836 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1838 return token->keyword == keyword;
1841 /* A minimum or maximum operator has been seen. As these are
1842 deprecated, issue a warning. */
1845 cp_parser_warn_min_max (void)
1847 if (warn_deprecated && !in_system_header)
1848 warning (0, "minimum/maximum operators are deprecated");
1851 /* If not parsing tentatively, issue a diagnostic of the form
1852 FILE:LINE: MESSAGE before TOKEN
1853 where TOKEN is the next token in the input stream. MESSAGE
1854 (specified by the caller) is usually of the form "expected
1858 cp_parser_error (cp_parser* parser, const char* message)
1860 if (!cp_parser_simulate_error (parser))
1862 cp_token *token = cp_lexer_peek_token (parser->lexer);
1863 /* This diagnostic makes more sense if it is tagged to the line
1864 of the token we just peeked at. */
1865 cp_lexer_set_source_position_from_token (token);
1866 if (token->type == CPP_PRAGMA)
1868 error ("%<#pragma%> is not allowed here");
1869 cp_lexer_purge_token (parser->lexer);
1872 c_parse_error (message,
1873 /* Because c_parser_error does not understand
1874 CPP_KEYWORD, keywords are treated like
1876 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1881 /* Issue an error about name-lookup failing. NAME is the
1882 IDENTIFIER_NODE DECL is the result of
1883 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1884 the thing that we hoped to find. */
1887 cp_parser_name_lookup_error (cp_parser* parser,
1890 const char* desired)
1892 /* If name lookup completely failed, tell the user that NAME was not
1894 if (decl == error_mark_node)
1896 if (parser->scope && parser->scope != global_namespace)
1897 error ("%<%D::%D%> has not been declared",
1898 parser->scope, name);
1899 else if (parser->scope == global_namespace)
1900 error ("%<::%D%> has not been declared", name);
1901 else if (parser->object_scope
1902 && !CLASS_TYPE_P (parser->object_scope))
1903 error ("request for member %qD in non-class type %qT",
1904 name, parser->object_scope);
1905 else if (parser->object_scope)
1906 error ("%<%T::%D%> has not been declared",
1907 parser->object_scope, name);
1909 error ("%qD has not been declared", name);
1911 else if (parser->scope && parser->scope != global_namespace)
1912 error ("%<%D::%D%> %s", parser->scope, name, desired);
1913 else if (parser->scope == global_namespace)
1914 error ("%<::%D%> %s", name, desired);
1916 error ("%qD %s", name, desired);
1919 /* If we are parsing tentatively, remember that an error has occurred
1920 during this tentative parse. Returns true if the error was
1921 simulated; false if a message should be issued by the caller. */
1924 cp_parser_simulate_error (cp_parser* parser)
1926 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1928 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1934 /* This function is called when a type is defined. If type
1935 definitions are forbidden at this point, an error message is
1939 cp_parser_check_type_definition (cp_parser* parser)
1941 /* If types are forbidden here, issue a message. */
1942 if (parser->type_definition_forbidden_message)
1943 /* Use `%s' to print the string in case there are any escape
1944 characters in the message. */
1945 error ("%s", parser->type_definition_forbidden_message);
1948 /* This function is called when the DECLARATOR is processed. The TYPE
1949 was a type defined in the decl-specifiers. If it is invalid to
1950 define a type in the decl-specifiers for DECLARATOR, an error is
1954 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1957 /* [dcl.fct] forbids type definitions in return types.
1958 Unfortunately, it's not easy to know whether or not we are
1959 processing a return type until after the fact. */
1961 && (declarator->kind == cdk_pointer
1962 || declarator->kind == cdk_reference
1963 || declarator->kind == cdk_ptrmem))
1964 declarator = declarator->declarator;
1966 && declarator->kind == cdk_function)
1968 error ("new types may not be defined in a return type");
1969 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1974 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1975 "<" in any valid C++ program. If the next token is indeed "<",
1976 issue a message warning the user about what appears to be an
1977 invalid attempt to form a template-id. */
1980 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1983 cp_token_position start = 0;
1985 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1988 error ("%qT is not a template", type);
1989 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1990 error ("%qE is not a template", type);
1992 error ("invalid template-id");
1993 /* Remember the location of the invalid "<". */
1994 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1995 start = cp_lexer_token_position (parser->lexer, true);
1996 /* Consume the "<". */
1997 cp_lexer_consume_token (parser->lexer);
1998 /* Parse the template arguments. */
1999 cp_parser_enclosed_template_argument_list (parser);
2000 /* Permanently remove the invalid template arguments so that
2001 this error message is not issued again. */
2003 cp_lexer_purge_tokens_after (parser->lexer, start);
2007 /* If parsing an integral constant-expression, issue an error message
2008 about the fact that THING appeared and return true. Otherwise,
2009 return false. In either case, set
2010 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2013 cp_parser_non_integral_constant_expression (cp_parser *parser,
2016 parser->non_integral_constant_expression_p = true;
2017 if (parser->integral_constant_expression_p)
2019 if (!parser->allow_non_integral_constant_expression_p)
2021 error ("%s cannot appear in a constant-expression", thing);
2028 /* Emit a diagnostic for an invalid type name. SCOPE is the
2029 qualifying scope (or NULL, if none) for ID. This function commits
2030 to the current active tentative parse, if any. (Otherwise, the
2031 problematic construct might be encountered again later, resulting
2032 in duplicate error messages.) */
2035 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2037 tree decl, old_scope;
2038 /* Try to lookup the identifier. */
2039 old_scope = parser->scope;
2040 parser->scope = scope;
2041 decl = cp_parser_lookup_name_simple (parser, id);
2042 parser->scope = old_scope;
2043 /* If the lookup found a template-name, it means that the user forgot
2044 to specify an argument list. Emit an useful error message. */
2045 if (TREE_CODE (decl) == TEMPLATE_DECL)
2046 error ("invalid use of template-name %qE without an argument list",
2048 else if (!parser->scope)
2050 /* Issue an error message. */
2051 error ("%qE does not name a type", id);
2052 /* If we're in a template class, it's possible that the user was
2053 referring to a type from a base class. For example:
2055 template <typename T> struct A { typedef T X; };
2056 template <typename T> struct B : public A<T> { X x; };
2058 The user should have said "typename A<T>::X". */
2059 if (processing_template_decl && current_class_type
2060 && TYPE_BINFO (current_class_type))
2064 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2068 tree base_type = BINFO_TYPE (b);
2069 if (CLASS_TYPE_P (base_type)
2070 && dependent_type_p (base_type))
2073 /* Go from a particular instantiation of the
2074 template (which will have an empty TYPE_FIELDs),
2075 to the main version. */
2076 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2077 for (field = TYPE_FIELDS (base_type);
2079 field = TREE_CHAIN (field))
2080 if (TREE_CODE (field) == TYPE_DECL
2081 && DECL_NAME (field) == id)
2083 inform ("(perhaps %<typename %T::%E%> was intended)",
2084 BINFO_TYPE (b), id);
2093 /* Here we diagnose qualified-ids where the scope is actually correct,
2094 but the identifier does not resolve to a valid type name. */
2097 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2098 error ("%qE in namespace %qE does not name a type",
2100 else if (TYPE_P (parser->scope))
2101 error ("%qE in class %qT does not name a type", id, parser->scope);
2105 cp_parser_commit_to_tentative_parse (parser);
2108 /* Check for a common situation where a type-name should be present,
2109 but is not, and issue a sensible error message. Returns true if an
2110 invalid type-name was detected.
2112 The situation handled by this function are variable declarations of the
2113 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2114 Usually, `ID' should name a type, but if we got here it means that it
2115 does not. We try to emit the best possible error message depending on
2116 how exactly the id-expression looks like.
2120 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2124 cp_parser_parse_tentatively (parser);
2125 id = cp_parser_id_expression (parser,
2126 /*template_keyword_p=*/false,
2127 /*check_dependency_p=*/true,
2128 /*template_p=*/NULL,
2129 /*declarator_p=*/true);
2130 /* After the id-expression, there should be a plain identifier,
2131 otherwise this is not a simple variable declaration. Also, if
2132 the scope is dependent, we cannot do much. */
2133 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2134 || (parser->scope && TYPE_P (parser->scope)
2135 && dependent_type_p (parser->scope)))
2137 cp_parser_abort_tentative_parse (parser);
2140 if (!cp_parser_parse_definitely (parser)
2141 || TREE_CODE (id) != IDENTIFIER_NODE)
2144 /* Emit a diagnostic for the invalid type. */
2145 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2146 /* Skip to the end of the declaration; there's no point in
2147 trying to process it. */
2148 cp_parser_skip_to_end_of_block_or_statement (parser);
2152 /* Consume tokens up to, and including, the next non-nested closing `)'.
2153 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2154 are doing error recovery. Returns -1 if OR_COMMA is true and we
2155 found an unnested comma. */
2158 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2163 unsigned paren_depth = 0;
2164 unsigned brace_depth = 0;
2167 if (recovering && !or_comma
2168 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2175 /* If we've run out of tokens, then there is no closing `)'. */
2176 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2182 token = cp_lexer_peek_token (parser->lexer);
2184 /* This matches the processing in skip_to_end_of_statement. */
2185 if (token->type == CPP_SEMICOLON && !brace_depth)
2190 if (token->type == CPP_OPEN_BRACE)
2192 if (token->type == CPP_CLOSE_BRACE)
2200 if (recovering && or_comma && token->type == CPP_COMMA
2201 && !brace_depth && !paren_depth)
2209 /* If it is an `(', we have entered another level of nesting. */
2210 if (token->type == CPP_OPEN_PAREN)
2212 /* If it is a `)', then we might be done. */
2213 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2216 cp_lexer_consume_token (parser->lexer);
2224 /* Consume the token. */
2225 cp_lexer_consume_token (parser->lexer);
2231 /* Consume tokens until we reach the end of the current statement.
2232 Normally, that will be just before consuming a `;'. However, if a
2233 non-nested `}' comes first, then we stop before consuming that. */
2236 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2238 unsigned nesting_depth = 0;
2244 /* Peek at the next token. */
2245 token = cp_lexer_peek_token (parser->lexer);
2246 /* If we've run out of tokens, stop. */
2247 if (token->type == CPP_EOF)
2249 /* If the next token is a `;', we have reached the end of the
2251 if (token->type == CPP_SEMICOLON && !nesting_depth)
2253 /* If the next token is a non-nested `}', then we have reached
2254 the end of the current block. */
2255 if (token->type == CPP_CLOSE_BRACE)
2257 /* If this is a non-nested `}', stop before consuming it.
2258 That way, when confronted with something like:
2262 we stop before consuming the closing `}', even though we
2263 have not yet reached a `;'. */
2264 if (nesting_depth == 0)
2266 /* If it is the closing `}' for a block that we have
2267 scanned, stop -- but only after consuming the token.
2273 we will stop after the body of the erroneously declared
2274 function, but before consuming the following `typedef'
2276 if (--nesting_depth == 0)
2278 cp_lexer_consume_token (parser->lexer);
2282 /* If it the next token is a `{', then we are entering a new
2283 block. Consume the entire block. */
2284 else if (token->type == CPP_OPEN_BRACE)
2286 /* Consume the token. */
2287 cp_lexer_consume_token (parser->lexer);
2291 /* This function is called at the end of a statement or declaration.
2292 If the next token is a semicolon, it is consumed; otherwise, error
2293 recovery is attempted. */
2296 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2298 /* Look for the trailing `;'. */
2299 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2301 /* If there is additional (erroneous) input, skip to the end of
2303 cp_parser_skip_to_end_of_statement (parser);
2304 /* If the next token is now a `;', consume it. */
2305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2306 cp_lexer_consume_token (parser->lexer);
2310 /* Skip tokens until we have consumed an entire block, or until we
2311 have consumed a non-nested `;'. */
2314 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2316 unsigned nesting_depth = 0;
2322 /* Peek at the next token. */
2323 token = cp_lexer_peek_token (parser->lexer);
2324 /* If we've run out of tokens, stop. */
2325 if (token->type == CPP_EOF)
2327 /* If the next token is a `;', we have reached the end of the
2329 if (token->type == CPP_SEMICOLON && !nesting_depth)
2331 /* Consume the `;'. */
2332 cp_lexer_consume_token (parser->lexer);
2335 /* Consume the token. */
2336 token = cp_lexer_consume_token (parser->lexer);
2337 /* If the next token is a non-nested `}', then we have reached
2338 the end of the current block. */
2339 if (token->type == CPP_CLOSE_BRACE
2340 && (nesting_depth == 0 || --nesting_depth == 0))
2342 /* If it the next token is a `{', then we are entering a new
2343 block. Consume the entire block. */
2344 if (token->type == CPP_OPEN_BRACE)
2349 /* Skip tokens until a non-nested closing curly brace is the next
2353 cp_parser_skip_to_closing_brace (cp_parser *parser)
2355 unsigned nesting_depth = 0;
2361 /* Peek at the next token. */
2362 token = cp_lexer_peek_token (parser->lexer);
2363 /* If we've run out of tokens, stop. */
2364 if (token->type == CPP_EOF)
2366 /* If the next token is a non-nested `}', then we have reached
2367 the end of the current block. */
2368 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2370 /* If it the next token is a `{', then we are entering a new
2371 block. Consume the entire block. */
2372 else if (token->type == CPP_OPEN_BRACE)
2374 /* Consume the token. */
2375 cp_lexer_consume_token (parser->lexer);
2379 /* This is a simple wrapper around make_typename_type. When the id is
2380 an unresolved identifier node, we can provide a superior diagnostic
2381 using cp_parser_diagnose_invalid_type_name. */
2384 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2387 if (TREE_CODE (id) == IDENTIFIER_NODE)
2389 result = make_typename_type (scope, id, typename_type,
2391 if (result == error_mark_node)
2392 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2395 return make_typename_type (scope, id, typename_type, tf_error);
2399 /* Create a new C++ parser. */
2402 cp_parser_new (void)
2408 /* cp_lexer_new_main is called before calling ggc_alloc because
2409 cp_lexer_new_main might load a PCH file. */
2410 lexer = cp_lexer_new_main ();
2412 /* Initialize the binops_by_token so that we can get the tree
2413 directly from the token. */
2414 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2415 binops_by_token[binops[i].token_type] = binops[i];
2417 parser = GGC_CNEW (cp_parser);
2418 parser->lexer = lexer;
2419 parser->context = cp_parser_context_new (NULL);
2421 /* For now, we always accept GNU extensions. */
2422 parser->allow_gnu_extensions_p = 1;
2424 /* The `>' token is a greater-than operator, not the end of a
2426 parser->greater_than_is_operator_p = true;
2428 parser->default_arg_ok_p = true;
2430 /* We are not parsing a constant-expression. */
2431 parser->integral_constant_expression_p = false;
2432 parser->allow_non_integral_constant_expression_p = false;
2433 parser->non_integral_constant_expression_p = false;
2435 /* Local variable names are not forbidden. */
2436 parser->local_variables_forbidden_p = false;
2438 /* We are not processing an `extern "C"' declaration. */
2439 parser->in_unbraced_linkage_specification_p = false;
2441 /* We are not processing a declarator. */
2442 parser->in_declarator_p = false;
2444 /* We are not processing a template-argument-list. */
2445 parser->in_template_argument_list_p = false;
2447 /* We are not in an iteration statement. */
2448 parser->in_iteration_statement_p = false;
2450 /* We are not in a switch statement. */
2451 parser->in_switch_statement_p = false;
2453 /* We are not parsing a type-id inside an expression. */
2454 parser->in_type_id_in_expr_p = false;
2456 /* Declarations aren't implicitly extern "C". */
2457 parser->implicit_extern_c = false;
2459 /* String literals should be translated to the execution character set. */
2460 parser->translate_strings_p = true;
2462 /* The unparsed function queue is empty. */
2463 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2465 /* There are no classes being defined. */
2466 parser->num_classes_being_defined = 0;
2468 /* No template parameters apply. */
2469 parser->num_template_parameter_lists = 0;
2474 /* Create a cp_lexer structure which will emit the tokens in CACHE
2475 and push it onto the parser's lexer stack. This is used for delayed
2476 parsing of in-class method bodies and default arguments, and should
2477 not be confused with tentative parsing. */
2479 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2481 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2482 lexer->next = parser->lexer;
2483 parser->lexer = lexer;
2485 /* Move the current source position to that of the first token in the
2487 cp_lexer_set_source_position_from_token (lexer->next_token);
2490 /* Pop the top lexer off the parser stack. This is never used for the
2491 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2493 cp_parser_pop_lexer (cp_parser *parser)
2495 cp_lexer *lexer = parser->lexer;
2496 parser->lexer = lexer->next;
2497 cp_lexer_destroy (lexer);
2499 /* Put the current source position back where it was before this
2500 lexer was pushed. */
2501 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2504 /* Lexical conventions [gram.lex] */
2506 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2510 cp_parser_identifier (cp_parser* parser)
2514 /* Look for the identifier. */
2515 token = cp_parser_require (parser, CPP_NAME, "identifier");
2516 /* Return the value. */
2517 return token ? token->value : error_mark_node;
2520 /* Parse a sequence of adjacent string constants. Returns a
2521 TREE_STRING representing the combined, nul-terminated string
2522 constant. If TRANSLATE is true, translate the string to the
2523 execution character set. If WIDE_OK is true, a wide string is
2526 C++98 [lex.string] says that if a narrow string literal token is
2527 adjacent to a wide string literal token, the behavior is undefined.
2528 However, C99 6.4.5p4 says that this results in a wide string literal.
2529 We follow C99 here, for consistency with the C front end.
2531 This code is largely lifted from lex_string() in c-lex.c.
2533 FUTURE: ObjC++ will need to handle @-strings here. */
2535 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2540 struct obstack str_ob;
2541 cpp_string str, istr, *strs;
2544 tok = cp_lexer_peek_token (parser->lexer);
2545 if (!cp_parser_is_string_literal (tok))
2547 cp_parser_error (parser, "expected string-literal");
2548 return error_mark_node;
2551 /* Try to avoid the overhead of creating and destroying an obstack
2552 for the common case of just one string. */
2553 if (!cp_parser_is_string_literal
2554 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2556 cp_lexer_consume_token (parser->lexer);
2558 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2559 str.len = TREE_STRING_LENGTH (tok->value);
2561 if (tok->type == CPP_WSTRING)
2568 gcc_obstack_init (&str_ob);
2573 cp_lexer_consume_token (parser->lexer);
2575 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2576 str.len = TREE_STRING_LENGTH (tok->value);
2577 if (tok->type == CPP_WSTRING)
2580 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2582 tok = cp_lexer_peek_token (parser->lexer);
2584 while (cp_parser_is_string_literal (tok));
2586 strs = (cpp_string *) obstack_finish (&str_ob);
2589 if (wide && !wide_ok)
2591 cp_parser_error (parser, "a wide string is invalid in this context");
2595 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2596 (parse_in, strs, count, &istr, wide))
2598 value = build_string (istr.len, (char *)istr.text);
2599 free ((void *)istr.text);
2601 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2602 value = fix_string_type (value);
2605 /* cpp_interpret_string has issued an error. */
2606 value = error_mark_node;
2609 obstack_free (&str_ob, 0);
2615 /* Basic concepts [gram.basic] */
2617 /* Parse a translation-unit.
2620 declaration-seq [opt]
2622 Returns TRUE if all went well. */
2625 cp_parser_translation_unit (cp_parser* parser)
2627 /* The address of the first non-permanent object on the declarator
2629 static void *declarator_obstack_base;
2633 /* Create the declarator obstack, if necessary. */
2634 if (!cp_error_declarator)
2636 gcc_obstack_init (&declarator_obstack);
2637 /* Create the error declarator. */
2638 cp_error_declarator = make_declarator (cdk_error);
2639 /* Create the empty parameter list. */
2640 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2641 /* Remember where the base of the declarator obstack lies. */
2642 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2647 cp_parser_declaration_seq_opt (parser);
2649 /* If there are no tokens left then all went well. */
2650 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2652 /* Get rid of the token array; we don't need it any more. */
2653 cp_lexer_destroy (parser->lexer);
2654 parser->lexer = NULL;
2656 /* This file might have been a context that's implicitly extern
2657 "C". If so, pop the lang context. (Only relevant for PCH.) */
2658 if (parser->implicit_extern_c)
2660 pop_lang_context ();
2661 parser->implicit_extern_c = false;
2665 finish_translation_unit ();
2672 cp_parser_error (parser, "expected declaration");
2678 /* Make sure the declarator obstack was fully cleaned up. */
2679 gcc_assert (obstack_next_free (&declarator_obstack)
2680 == declarator_obstack_base);
2682 /* All went well. */
2686 /* Expressions [gram.expr] */
2688 /* Parse a primary-expression.
2699 ( compound-statement )
2700 __builtin_va_arg ( assignment-expression , type-id )
2702 Objective-C++ Extension:
2710 CAST_P is true if this primary expression is the target of a cast.
2712 Returns a representation of the expression.
2714 *IDK indicates what kind of id-expression (if any) was present.
2716 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2717 used as the operand of a pointer-to-member. In that case,
2718 *QUALIFYING_CLASS gives the class that is used as the qualifying
2719 class in the pointer-to-member. */
2722 cp_parser_primary_expression (cp_parser *parser,
2725 tree *qualifying_class)
2729 /* Assume the primary expression is not an id-expression. */
2730 *idk = CP_ID_KIND_NONE;
2731 /* And that it cannot be used as pointer-to-member. */
2732 *qualifying_class = NULL_TREE;
2734 /* Peek at the next token. */
2735 token = cp_lexer_peek_token (parser->lexer);
2736 switch (token->type)
2747 token = cp_lexer_consume_token (parser->lexer);
2748 /* Floating-point literals are only allowed in an integral
2749 constant expression if they are cast to an integral or
2750 enumeration type. */
2751 if (TREE_CODE (token->value) == REAL_CST
2752 && parser->integral_constant_expression_p
2755 /* CAST_P will be set even in invalid code like "int(2.7 +
2756 ...)". Therefore, we have to check that the next token
2757 is sure to end the cast. */
2760 cp_token *next_token;
2762 next_token = cp_lexer_peek_token (parser->lexer);
2763 if (/* The comma at the end of an
2764 enumerator-definition. */
2765 next_token->type != CPP_COMMA
2766 /* The curly brace at the end of an enum-specifier. */
2767 && next_token->type != CPP_CLOSE_BRACE
2768 /* The end of a statement. */
2769 && next_token->type != CPP_SEMICOLON
2770 /* The end of the cast-expression. */
2771 && next_token->type != CPP_CLOSE_PAREN
2772 /* The end of an array bound. */
2773 && next_token->type != CPP_CLOSE_SQUARE)
2777 /* If we are within a cast, then the constraint that the
2778 cast is to an integral or enumeration type will be
2779 checked at that point. If we are not within a cast, then
2780 this code is invalid. */
2782 cp_parser_non_integral_constant_expression
2783 (parser, "floating-point literal");
2785 return token->value;
2789 /* ??? Should wide strings be allowed when parser->translate_strings_p
2790 is false (i.e. in attributes)? If not, we can kill the third
2791 argument to cp_parser_string_literal. */
2792 return cp_parser_string_literal (parser,
2793 parser->translate_strings_p,
2796 case CPP_OPEN_PAREN:
2799 bool saved_greater_than_is_operator_p;
2801 /* Consume the `('. */
2802 cp_lexer_consume_token (parser->lexer);
2803 /* Within a parenthesized expression, a `>' token is always
2804 the greater-than operator. */
2805 saved_greater_than_is_operator_p
2806 = parser->greater_than_is_operator_p;
2807 parser->greater_than_is_operator_p = true;
2808 /* If we see `( { ' then we are looking at the beginning of
2809 a GNU statement-expression. */
2810 if (cp_parser_allow_gnu_extensions_p (parser)
2811 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2813 /* Statement-expressions are not allowed by the standard. */
2815 pedwarn ("ISO C++ forbids braced-groups within expressions");
2817 /* And they're not allowed outside of a function-body; you
2818 cannot, for example, write:
2820 int i = ({ int j = 3; j + 1; });
2822 at class or namespace scope. */
2823 if (!at_function_scope_p ())
2824 error ("statement-expressions are allowed only inside functions");
2825 /* Start the statement-expression. */
2826 expr = begin_stmt_expr ();
2827 /* Parse the compound-statement. */
2828 cp_parser_compound_statement (parser, expr, false);
2830 expr = finish_stmt_expr (expr, false);
2834 /* Parse the parenthesized expression. */
2835 expr = cp_parser_expression (parser, cast_p);
2836 /* Let the front end know that this expression was
2837 enclosed in parentheses. This matters in case, for
2838 example, the expression is of the form `A::B', since
2839 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2841 finish_parenthesized_expr (expr);
2843 /* The `>' token might be the end of a template-id or
2844 template-parameter-list now. */
2845 parser->greater_than_is_operator_p
2846 = saved_greater_than_is_operator_p;
2847 /* Consume the `)'. */
2848 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2849 cp_parser_skip_to_end_of_statement (parser);
2855 switch (token->keyword)
2857 /* These two are the boolean literals. */
2859 cp_lexer_consume_token (parser->lexer);
2860 return boolean_true_node;
2862 cp_lexer_consume_token (parser->lexer);
2863 return boolean_false_node;
2865 /* The `__null' literal. */
2867 cp_lexer_consume_token (parser->lexer);
2870 /* Recognize the `this' keyword. */
2872 cp_lexer_consume_token (parser->lexer);
2873 if (parser->local_variables_forbidden_p)
2875 error ("%<this%> may not be used in this context");
2876 return error_mark_node;
2878 /* Pointers cannot appear in constant-expressions. */
2879 if (cp_parser_non_integral_constant_expression (parser,
2881 return error_mark_node;
2882 return finish_this_expr ();
2884 /* The `operator' keyword can be the beginning of an
2889 case RID_FUNCTION_NAME:
2890 case RID_PRETTY_FUNCTION_NAME:
2891 case RID_C99_FUNCTION_NAME:
2892 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2893 __func__ are the names of variables -- but they are
2894 treated specially. Therefore, they are handled here,
2895 rather than relying on the generic id-expression logic
2896 below. Grammatically, these names are id-expressions.
2898 Consume the token. */
2899 token = cp_lexer_consume_token (parser->lexer);
2900 /* Look up the name. */
2901 return finish_fname (token->value);
2908 /* The `__builtin_va_arg' construct is used to handle
2909 `va_arg'. Consume the `__builtin_va_arg' token. */
2910 cp_lexer_consume_token (parser->lexer);
2911 /* Look for the opening `('. */
2912 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2913 /* Now, parse the assignment-expression. */
2914 expression = cp_parser_assignment_expression (parser,
2916 /* Look for the `,'. */
2917 cp_parser_require (parser, CPP_COMMA, "`,'");
2918 /* Parse the type-id. */
2919 type = cp_parser_type_id (parser);
2920 /* Look for the closing `)'. */
2921 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2922 /* Using `va_arg' in a constant-expression is not
2924 if (cp_parser_non_integral_constant_expression (parser,
2926 return error_mark_node;
2927 return build_x_va_arg (expression, type);
2931 return cp_parser_builtin_offsetof (parser);
2933 /* Objective-C++ expressions. */
2935 case RID_AT_PROTOCOL:
2936 case RID_AT_SELECTOR:
2937 return cp_parser_objc_expression (parser);
2940 cp_parser_error (parser, "expected primary-expression");
2941 return error_mark_node;
2944 /* An id-expression can start with either an identifier, a
2945 `::' as the beginning of a qualified-id, or the "operator"
2949 case CPP_TEMPLATE_ID:
2950 case CPP_NESTED_NAME_SPECIFIER:
2954 const char *error_msg;
2957 /* Parse the id-expression. */
2959 = cp_parser_id_expression (parser,
2960 /*template_keyword_p=*/false,
2961 /*check_dependency_p=*/true,
2962 /*template_p=*/NULL,
2963 /*declarator_p=*/false);
2964 if (id_expression == error_mark_node)
2965 return error_mark_node;
2966 /* If we have a template-id, then no further lookup is
2967 required. If the template-id was for a template-class, we
2968 will sometimes have a TYPE_DECL at this point. */
2969 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2970 || TREE_CODE (id_expression) == TYPE_DECL)
2971 decl = id_expression;
2972 /* Look up the name. */
2977 decl = cp_parser_lookup_name (parser, id_expression,
2979 /*is_template=*/false,
2980 /*is_namespace=*/false,
2981 /*check_dependency=*/true,
2983 /* If the lookup was ambiguous, an error will already have
2986 return error_mark_node;
2988 /* In Objective-C++, an instance variable (ivar) may be preferred
2989 to whatever cp_parser_lookup_name() found. */
2990 decl = objc_lookup_ivar (decl, id_expression);
2992 /* If name lookup gives us a SCOPE_REF, then the
2993 qualifying scope was dependent. Just propagate the
2995 if (TREE_CODE (decl) == SCOPE_REF)
2997 if (TYPE_P (TREE_OPERAND (decl, 0)))
2998 *qualifying_class = TREE_OPERAND (decl, 0);
3001 /* Check to see if DECL is a local variable in a context
3002 where that is forbidden. */
3003 if (parser->local_variables_forbidden_p
3004 && local_variable_p (decl))
3006 /* It might be that we only found DECL because we are
3007 trying to be generous with pre-ISO scoping rules.
3008 For example, consider:
3012 for (int i = 0; i < 10; ++i) {}
3013 extern void f(int j = i);
3016 Here, name look up will originally find the out
3017 of scope `i'. We need to issue a warning message,
3018 but then use the global `i'. */
3019 decl = check_for_out_of_scope_variable (decl);
3020 if (local_variable_p (decl))
3022 error ("local variable %qD may not appear in this context",
3024 return error_mark_node;
3029 decl = finish_id_expression (id_expression, decl, parser->scope,
3030 idk, qualifying_class,
3031 parser->integral_constant_expression_p,
3032 parser->allow_non_integral_constant_expression_p,
3033 &parser->non_integral_constant_expression_p,
3036 cp_parser_error (parser, error_msg);
3040 /* Anything else is an error. */
3042 /* ...unless we have an Objective-C++ message or string literal, that is. */
3043 if (c_dialect_objc ()
3044 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3045 return cp_parser_objc_expression (parser);
3047 cp_parser_error (parser, "expected primary-expression");
3048 return error_mark_node;
3052 /* Parse an id-expression.
3059 :: [opt] nested-name-specifier template [opt] unqualified-id
3061 :: operator-function-id
3064 Return a representation of the unqualified portion of the
3065 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3066 a `::' or nested-name-specifier.
3068 Often, if the id-expression was a qualified-id, the caller will
3069 want to make a SCOPE_REF to represent the qualified-id. This
3070 function does not do this in order to avoid wastefully creating
3071 SCOPE_REFs when they are not required.
3073 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3076 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3077 uninstantiated templates.
3079 If *TEMPLATE_P is non-NULL, it is set to true iff the
3080 `template' keyword is used to explicitly indicate that the entity
3081 named is a template.
3083 If DECLARATOR_P is true, the id-expression is appearing as part of
3084 a declarator, rather than as part of an expression. */
3087 cp_parser_id_expression (cp_parser *parser,
3088 bool template_keyword_p,
3089 bool check_dependency_p,
3093 bool global_scope_p;
3094 bool nested_name_specifier_p;
3096 /* Assume the `template' keyword was not used. */
3098 *template_p = false;
3100 /* Look for the optional `::' operator. */
3102 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3104 /* Look for the optional nested-name-specifier. */
3105 nested_name_specifier_p
3106 = (cp_parser_nested_name_specifier_opt (parser,
3107 /*typename_keyword_p=*/false,
3112 /* If there is a nested-name-specifier, then we are looking at
3113 the first qualified-id production. */
3114 if (nested_name_specifier_p)
3117 tree saved_object_scope;
3118 tree saved_qualifying_scope;
3119 tree unqualified_id;
3122 /* See if the next token is the `template' keyword. */
3124 template_p = &is_template;
3125 *template_p = cp_parser_optional_template_keyword (parser);
3126 /* Name lookup we do during the processing of the
3127 unqualified-id might obliterate SCOPE. */
3128 saved_scope = parser->scope;
3129 saved_object_scope = parser->object_scope;
3130 saved_qualifying_scope = parser->qualifying_scope;
3131 /* Process the final unqualified-id. */
3132 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3135 /* Restore the SAVED_SCOPE for our caller. */
3136 parser->scope = saved_scope;
3137 parser->object_scope = saved_object_scope;
3138 parser->qualifying_scope = saved_qualifying_scope;
3140 return unqualified_id;
3142 /* Otherwise, if we are in global scope, then we are looking at one
3143 of the other qualified-id productions. */
3144 else if (global_scope_p)
3149 /* Peek at the next token. */
3150 token = cp_lexer_peek_token (parser->lexer);
3152 /* If it's an identifier, and the next token is not a "<", then
3153 we can avoid the template-id case. This is an optimization
3154 for this common case. */
3155 if (token->type == CPP_NAME
3156 && !cp_parser_nth_token_starts_template_argument_list_p
3158 return cp_parser_identifier (parser);
3160 cp_parser_parse_tentatively (parser);
3161 /* Try a template-id. */
3162 id = cp_parser_template_id (parser,
3163 /*template_keyword_p=*/false,
3164 /*check_dependency_p=*/true,
3166 /* If that worked, we're done. */
3167 if (cp_parser_parse_definitely (parser))
3170 /* Peek at the next token. (Changes in the token buffer may
3171 have invalidated the pointer obtained above.) */
3172 token = cp_lexer_peek_token (parser->lexer);
3174 switch (token->type)
3177 return cp_parser_identifier (parser);
3180 if (token->keyword == RID_OPERATOR)
3181 return cp_parser_operator_function_id (parser);
3185 cp_parser_error (parser, "expected id-expression");
3186 return error_mark_node;
3190 return cp_parser_unqualified_id (parser, template_keyword_p,
3191 /*check_dependency_p=*/true,
3195 /* Parse an unqualified-id.
3199 operator-function-id
3200 conversion-function-id
3204 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3205 keyword, in a construct like `A::template ...'.
3207 Returns a representation of unqualified-id. For the `identifier'
3208 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3209 production a BIT_NOT_EXPR is returned; the operand of the
3210 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3211 other productions, see the documentation accompanying the
3212 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3213 names are looked up in uninstantiated templates. If DECLARATOR_P
3214 is true, the unqualified-id is appearing as part of a declarator,
3215 rather than as part of an expression. */
3218 cp_parser_unqualified_id (cp_parser* parser,
3219 bool template_keyword_p,
3220 bool check_dependency_p,
3225 /* Peek at the next token. */
3226 token = cp_lexer_peek_token (parser->lexer);
3228 switch (token->type)
3234 /* We don't know yet whether or not this will be a
3236 cp_parser_parse_tentatively (parser);
3237 /* Try a template-id. */
3238 id = cp_parser_template_id (parser, template_keyword_p,
3241 /* If it worked, we're done. */
3242 if (cp_parser_parse_definitely (parser))
3244 /* Otherwise, it's an ordinary identifier. */
3245 return cp_parser_identifier (parser);
3248 case CPP_TEMPLATE_ID:
3249 return cp_parser_template_id (parser, template_keyword_p,
3256 tree qualifying_scope;
3261 /* Consume the `~' token. */
3262 cp_lexer_consume_token (parser->lexer);
3263 /* Parse the class-name. The standard, as written, seems to
3266 template <typename T> struct S { ~S (); };
3267 template <typename T> S<T>::~S() {}
3269 is invalid, since `~' must be followed by a class-name, but
3270 `S<T>' is dependent, and so not known to be a class.
3271 That's not right; we need to look in uninstantiated
3272 templates. A further complication arises from:
3274 template <typename T> void f(T t) {
3278 Here, it is not possible to look up `T' in the scope of `T'
3279 itself. We must look in both the current scope, and the
3280 scope of the containing complete expression.
3282 Yet another issue is:
3291 The standard does not seem to say that the `S' in `~S'
3292 should refer to the type `S' and not the data member
3295 /* DR 244 says that we look up the name after the "~" in the
3296 same scope as we looked up the qualifying name. That idea
3297 isn't fully worked out; it's more complicated than that. */
3298 scope = parser->scope;
3299 object_scope = parser->object_scope;
3300 qualifying_scope = parser->qualifying_scope;
3302 /* If the name is of the form "X::~X" it's OK. */
3303 if (scope && TYPE_P (scope)
3304 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3305 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3307 && (cp_lexer_peek_token (parser->lexer)->value
3308 == TYPE_IDENTIFIER (scope)))
3310 cp_lexer_consume_token (parser->lexer);
3311 return build_nt (BIT_NOT_EXPR, scope);
3314 /* If there was an explicit qualification (S::~T), first look
3315 in the scope given by the qualification (i.e., S). */
3317 type_decl = NULL_TREE;
3320 cp_parser_parse_tentatively (parser);
3321 type_decl = cp_parser_class_name (parser,
3322 /*typename_keyword_p=*/false,
3323 /*template_keyword_p=*/false,
3325 /*check_dependency=*/false,
3326 /*class_head_p=*/false,
3328 if (cp_parser_parse_definitely (parser))
3331 /* In "N::S::~S", look in "N" as well. */
3332 if (!done && scope && qualifying_scope)
3334 cp_parser_parse_tentatively (parser);
3335 parser->scope = qualifying_scope;
3336 parser->object_scope = NULL_TREE;
3337 parser->qualifying_scope = NULL_TREE;
3339 = cp_parser_class_name (parser,
3340 /*typename_keyword_p=*/false,
3341 /*template_keyword_p=*/false,
3343 /*check_dependency=*/false,
3344 /*class_head_p=*/false,
3346 if (cp_parser_parse_definitely (parser))
3349 /* In "p->S::~T", look in the scope given by "*p" as well. */
3350 else if (!done && object_scope)
3352 cp_parser_parse_tentatively (parser);
3353 parser->scope = object_scope;
3354 parser->object_scope = NULL_TREE;
3355 parser->qualifying_scope = NULL_TREE;
3357 = cp_parser_class_name (parser,
3358 /*typename_keyword_p=*/false,
3359 /*template_keyword_p=*/false,
3361 /*check_dependency=*/false,
3362 /*class_head_p=*/false,
3364 if (cp_parser_parse_definitely (parser))
3367 /* Look in the surrounding context. */
3370 parser->scope = NULL_TREE;
3371 parser->object_scope = NULL_TREE;
3372 parser->qualifying_scope = NULL_TREE;
3374 = cp_parser_class_name (parser,
3375 /*typename_keyword_p=*/false,
3376 /*template_keyword_p=*/false,
3378 /*check_dependency=*/false,
3379 /*class_head_p=*/false,
3382 /* If an error occurred, assume that the name of the
3383 destructor is the same as the name of the qualifying
3384 class. That allows us to keep parsing after running
3385 into ill-formed destructor names. */
3386 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3387 return build_nt (BIT_NOT_EXPR, scope);
3388 else if (type_decl == error_mark_node)
3389 return error_mark_node;
3393 A typedef-name that names a class shall not be used as the
3394 identifier in the declarator for a destructor declaration. */
3396 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3397 && !DECL_SELF_REFERENCE_P (type_decl)
3398 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3399 error ("typedef-name %qD used as destructor declarator",
3402 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3406 if (token->keyword == RID_OPERATOR)
3410 /* This could be a template-id, so we try that first. */
3411 cp_parser_parse_tentatively (parser);
3412 /* Try a template-id. */
3413 id = cp_parser_template_id (parser, template_keyword_p,
3414 /*check_dependency_p=*/true,
3416 /* If that worked, we're done. */
3417 if (cp_parser_parse_definitely (parser))
3419 /* We still don't know whether we're looking at an
3420 operator-function-id or a conversion-function-id. */
3421 cp_parser_parse_tentatively (parser);
3422 /* Try an operator-function-id. */
3423 id = cp_parser_operator_function_id (parser);
3424 /* If that didn't work, try a conversion-function-id. */
3425 if (!cp_parser_parse_definitely (parser))
3426 id = cp_parser_conversion_function_id (parser);
3433 cp_parser_error (parser, "expected unqualified-id");
3434 return error_mark_node;
3438 /* Parse an (optional) nested-name-specifier.
3440 nested-name-specifier:
3441 class-or-namespace-name :: nested-name-specifier [opt]
3442 class-or-namespace-name :: template nested-name-specifier [opt]
3444 PARSER->SCOPE should be set appropriately before this function is
3445 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3446 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3449 Sets PARSER->SCOPE to the class (TYPE) or namespace
3450 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3451 it unchanged if there is no nested-name-specifier. Returns the new
3452 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3454 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3455 part of a declaration and/or decl-specifier. */
3458 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3459 bool typename_keyword_p,
3460 bool check_dependency_p,
3462 bool is_declaration)
3464 bool success = false;
3465 tree access_check = NULL_TREE;
3466 cp_token_position start = 0;
3469 /* If the next token corresponds to a nested name specifier, there
3470 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3471 false, it may have been true before, in which case something
3472 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3473 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3474 CHECK_DEPENDENCY_P is false, we have to fall through into the
3476 if (check_dependency_p
3477 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3479 cp_parser_pre_parsed_nested_name_specifier (parser);
3480 return parser->scope;
3483 /* Remember where the nested-name-specifier starts. */
3484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3485 start = cp_lexer_token_position (parser->lexer, false);
3487 push_deferring_access_checks (dk_deferred);
3493 tree saved_qualifying_scope;
3494 bool template_keyword_p;
3496 /* Spot cases that cannot be the beginning of a
3497 nested-name-specifier. */
3498 token = cp_lexer_peek_token (parser->lexer);
3500 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3501 the already parsed nested-name-specifier. */
3502 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3504 /* Grab the nested-name-specifier and continue the loop. */
3505 cp_parser_pre_parsed_nested_name_specifier (parser);
3510 /* Spot cases that cannot be the beginning of a
3511 nested-name-specifier. On the second and subsequent times
3512 through the loop, we look for the `template' keyword. */
3513 if (success && token->keyword == RID_TEMPLATE)
3515 /* A template-id can start a nested-name-specifier. */
3516 else if (token->type == CPP_TEMPLATE_ID)
3520 /* If the next token is not an identifier, then it is
3521 definitely not a class-or-namespace-name. */
3522 if (token->type != CPP_NAME)
3524 /* If the following token is neither a `<' (to begin a
3525 template-id), nor a `::', then we are not looking at a
3526 nested-name-specifier. */
3527 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3528 if (token->type != CPP_SCOPE
3529 && !cp_parser_nth_token_starts_template_argument_list_p
3534 /* The nested-name-specifier is optional, so we parse
3536 cp_parser_parse_tentatively (parser);
3538 /* Look for the optional `template' keyword, if this isn't the
3539 first time through the loop. */
3541 template_keyword_p = cp_parser_optional_template_keyword (parser);
3543 template_keyword_p = false;
3545 /* Save the old scope since the name lookup we are about to do
3546 might destroy it. */
3547 old_scope = parser->scope;
3548 saved_qualifying_scope = parser->qualifying_scope;
3549 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3550 look up names in "X<T>::I" in order to determine that "Y" is
3551 a template. So, if we have a typename at this point, we make
3552 an effort to look through it. */
3554 && !typename_keyword_p
3556 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3557 parser->scope = resolve_typename_type (parser->scope,
3558 /*only_current_p=*/false);
3559 /* Parse the qualifying entity. */
3561 = cp_parser_class_or_namespace_name (parser,
3567 /* Look for the `::' token. */
3568 cp_parser_require (parser, CPP_SCOPE, "`::'");
3570 /* If we found what we wanted, we keep going; otherwise, we're
3572 if (!cp_parser_parse_definitely (parser))
3574 bool error_p = false;
3576 /* Restore the OLD_SCOPE since it was valid before the
3577 failed attempt at finding the last
3578 class-or-namespace-name. */