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, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
50 typedef struct cp_token GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The value associated with this token, if any. */
71 /* The location at which this token was found. */
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
80 static const cp_token eof_token =
82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
95 typedef struct cp_lexer GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
102 size_t buffer_length;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position GTY ((skip)) last_token;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position GTY ((skip)) next_token;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
121 /* True if we should output debugging information. */
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
135 typedef struct cp_token_cache GTY(())
137 /* The beginning of the token range. */
138 cp_token * GTY((skip)) first;
140 /* Points immediately after the last token in the range. */
141 cp_token * GTY ((skip)) last;
146 static cp_lexer *cp_lexer_new_main
148 static cp_lexer *cp_lexer_new_from_tokens
149 (cp_token_cache *tokens);
150 static void cp_lexer_destroy
152 static int cp_lexer_saving_tokens
154 static cp_token_position cp_lexer_token_position
156 static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
162 static cp_token *cp_lexer_peek_nth_token
163 (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
172 static void cp_lexer_purge_token
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
178 static void cp_lexer_commit_tokens
180 static void cp_lexer_rollback_tokens
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
187 static void cp_lexer_start_debugging
188 (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
204 static void cp_parser_initial_pragma
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
246 cp_lexer_new_main (void)
248 cp_token first_token;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings = true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer = GGC_CNEW (cp_lexer);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
277 buffer = GGC_NEWVEC (cp_token, alloc);
279 /* Put the first token in the buffer. */
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos->type != CPP_EOF)
292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293 pos = buffer + space;
295 cp_lexer_get_preprocessor_token (lexer, pos);
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
307 gcc_assert (lexer->next_token->type != CPP_PURGED);
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
321 /* We do not own the buffer. */
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325 lexer->last_token = last;
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer->debugging_p = false;
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
339 /* Frees all resources associated with LEXER. */
342 cp_lexer_destroy (cp_lexer *lexer)
345 ggc_free (lexer->buffer);
346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
355 cp_lexer_debugging_p (cp_lexer *lexer)
357 return lexer->debugging_p;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
367 return lexer->next_token - previous_p;
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
376 /* nonzero if we are presently saving tokens. */
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 static int is_extern_c = 0;
393 /* Get a new token from the preprocessor. */
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else if (token->type == CPP_PRAGMA)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
453 /* Update the globals input_location and in_system_header from TOKEN. */
455 cp_lexer_set_source_position_from_token (cp_token *token)
457 if (token->type != CPP_EOF)
459 input_location = token->location;
460 in_system_header = token->in_system_header;
464 /* Return a pointer to the next token in the token stream, but do not
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
470 if (cp_lexer_debugging_p (lexer))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
476 return lexer->next_token;
479 /* Return true if the next token has the indicated TYPE. */
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
484 return cp_lexer_peek_token (lexer)->type == type;
487 /* Return true if the next token does not have the indicated TYPE. */
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
492 return !cp_lexer_next_token_is (lexer, type);
495 /* Return true if the next token is the indicated KEYWORD. */
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
500 return cp_lexer_peek_token (lexer)->keyword == keyword;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
514 /* N is 1-based, not zero-based. */
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
522 token = lexer->next_token;
523 gcc_assert (!n || token != &eof_token);
527 if (token == lexer->last_token)
529 token = (cp_token *)&eof_token;
533 if (token->type != CPP_PURGED)
537 if (cp_lexer_debugging_p (lexer))
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
550 cp_lexer_consume_token (cp_lexer* lexer)
552 cp_token *token = lexer->next_token;
554 gcc_assert (token != &eof_token);
555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Begin saving tokens. All tokens consumed after this point will be
637 cp_lexer_save_tokens (cp_lexer* lexer)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
647 /* Commit to the portion of the token stream most recently saved. */
650 cp_lexer_commit_tokens (cp_lexer* lexer)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position, lexer->saved_tokens);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
677 cp_lexer_print_token (FILE * stream, cp_token *token)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
688 /* C++ parser token types - see "Manifest constants", above. */
691 "NESTED_NAME_SPECIFIER",
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
700 /* For some tokens, print the associated data. */
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
708 /* else fall through */
710 fputs (IDENTIFIER_POINTER (token->value), stream);
715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
723 /* Start emitting debugging information. */
726 cp_lexer_start_debugging (cp_lexer* lexer)
728 lexer->debugging_p = true;
731 /* Stop emitting debugging information. */
734 cp_lexer_stop_debugging (cp_lexer* lexer)
736 lexer->debugging_p = false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator *make_call_declarator
771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773 (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775 (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777 (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779 (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781 (cp_cv_quals, tree, cp_declarator *);
783 cp_declarator *cp_error_declarator;
785 /* The obstack on which declarators and related data structures are
787 static struct obstack declarator_obstack;
789 /* Alloc BYTES from the declarator memory pool. */
792 alloc_declarator (size_t bytes)
794 return obstack_alloc (&declarator_obstack, bytes);
797 /* Allocate a declarator of the indicated KIND. Clear fields that are
798 common to all declarators. */
800 static cp_declarator *
801 make_declarator (cp_declarator_kind kind)
803 cp_declarator *declarator;
805 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
806 declarator->kind = kind;
807 declarator->attributes = NULL_TREE;
808 declarator->declarator = NULL;
813 /* Make a declarator for a generalized identifier. If
814 QUALIFYING_SCOPE is non-NULL, the identifier is
815 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
816 UNQUALIFIED_NAME. SFK indicates the kind of special function this
819 static cp_declarator *
820 make_id_declarator (tree qualifying_scope, tree unqualified_name,
821 special_function_kind sfk)
823 cp_declarator *declarator;
825 /* It is valid to write:
827 class C { void f(); };
831 The standard is not clear about whether `typedef const C D' is
832 legal; as of 2002-09-15 the committee is considering that
833 question. EDG 3.0 allows that syntax. Therefore, we do as
835 if (qualifying_scope && TYPE_P (qualifying_scope))
836 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
839 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
840 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842 declarator = make_declarator (cdk_id);
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
845 declarator->u.id.sfk = sfk;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 cp_declarator *declarator;
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
867 /* Like make_pointer_declarator -- but for references. */
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 cp_declarator *declarator;
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887 cp_declarator *pointee)
889 cp_declarator *declarator;
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
905 make_call_declarator (cp_declarator *target,
906 cp_parameter_declarator *parms,
907 cp_cv_quals cv_qualifiers,
908 tree exception_specification)
910 cp_declarator *declarator;
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
925 make_array_declarator (cp_declarator *element, tree bounds)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
936 cp_parameter_declarator *no_parameters;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943 cp_declarator *declarator,
944 tree default_argument)
946 cp_parameter_declarator *parameter;
948 parameter = ((cp_parameter_declarator *)
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
952 parameter->decl_specifiers = *decl_specifiers;
954 clear_decl_specs (¶meter->decl_specifiers);
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
1055 PREC_EQUALITY_EXPRESSION,
1056 PREC_RELATIONAL_EXPRESSION,
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1090 typedef struct cp_parser_expression_stack_entry
1093 enum tree_code tree_type;
1095 } cp_parser_expression_stack_entry;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117 } cp_parser_context;
1121 /* Constructors and destructors. */
1123 static cp_parser_context *cp_parser_context_new
1124 (cp_parser_context *);
1126 /* Class variables. */
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1182 cp_parser_context *context;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list != NULL)
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
1190 memset (context, 0, sizeof (*context));
1193 context = GGC_CNEW (cp_parser_context);
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
1204 /* Thread the stack. */
1205 context->next = next;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p;
1300 /* TRUE if we are presently parsing the body of a switch
1302 bool in_switch_statement_p;
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1307 bool in_type_id_in_expr_p;
1309 /* TRUE if we are currently in a header file where declarations are
1310 implicitly extern "C". */
1311 bool implicit_extern_c;
1313 /* TRUE if strings in expressions should be translated to the execution
1315 bool translate_strings_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1334 These lists can only be processed once the outermost class being
1335 defined is complete. */
1336 tree unparsed_functions_queues;
1338 /* The number of classes whose definitions are currently in
1340 unsigned num_classes_being_defined;
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists;
1349 /* Constructors and destructors. */
1351 static cp_parser *cp_parser_new
1354 /* Routines to parse various constructs.
1356 Those that return `tree' will return the error_mark_node (rather
1357 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1358 Sometimes, they will return an ordinary node if error-recovery was
1359 attempted, even though a parse error occurred. So, to check
1360 whether or not a parse error occurred, you should always use
1361 cp_parser_error_occurred. If the construct is optional (indicated
1362 either by an `_opt' in the name of the function that does the
1363 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1364 the construct is not present. */
1366 /* Lexical conventions [gram.lex] */
1368 static tree cp_parser_identifier
1370 static tree cp_parser_string_literal
1371 (cp_parser *, bool, bool);
1373 /* Basic concepts [gram.basic] */
1375 static bool cp_parser_translation_unit
1378 /* Expressions [gram.expr] */
1380 static tree cp_parser_primary_expression
1381 (cp_parser *, bool, bool, bool, cp_id_kind *);
1382 static tree cp_parser_id_expression
1383 (cp_parser *, bool, bool, bool *, bool);
1384 static tree cp_parser_unqualified_id
1385 (cp_parser *, bool, bool, bool);
1386 static tree cp_parser_nested_name_specifier_opt
1387 (cp_parser *, bool, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier
1389 (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_class_or_namespace_name
1391 (cp_parser *, bool, bool, bool, bool, bool);
1392 static tree cp_parser_postfix_expression
1393 (cp_parser *, bool, bool);
1394 static tree cp_parser_postfix_open_square_expression
1395 (cp_parser *, tree, bool);
1396 static tree cp_parser_postfix_dot_deref_expression
1397 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1398 static tree cp_parser_parenthesized_expression_list
1399 (cp_parser *, bool, bool, bool *);
1400 static void cp_parser_pseudo_destructor_name
1401 (cp_parser *, tree *, tree *);
1402 static tree cp_parser_unary_expression
1403 (cp_parser *, bool, bool);
1404 static enum tree_code cp_parser_unary_operator
1406 static tree cp_parser_new_expression
1408 static tree cp_parser_new_placement
1410 static tree cp_parser_new_type_id
1411 (cp_parser *, tree *);
1412 static cp_declarator *cp_parser_new_declarator_opt
1414 static cp_declarator *cp_parser_direct_new_declarator
1416 static tree cp_parser_new_initializer
1418 static tree cp_parser_delete_expression
1420 static tree cp_parser_cast_expression
1421 (cp_parser *, bool, bool);
1422 static tree cp_parser_binary_expression
1423 (cp_parser *, bool);
1424 static tree cp_parser_question_colon_clause
1425 (cp_parser *, tree);
1426 static tree cp_parser_assignment_expression
1427 (cp_parser *, bool);
1428 static enum tree_code cp_parser_assignment_operator_opt
1430 static tree cp_parser_expression
1431 (cp_parser *, bool);
1432 static tree cp_parser_constant_expression
1433 (cp_parser *, bool, bool *);
1434 static tree cp_parser_builtin_offsetof
1437 /* Statements [gram.stmt.stmt] */
1439 static void cp_parser_statement
1440 (cp_parser *, tree, bool);
1441 static tree cp_parser_labeled_statement
1442 (cp_parser *, tree, bool);
1443 static tree cp_parser_expression_statement
1444 (cp_parser *, tree);
1445 static tree cp_parser_compound_statement
1446 (cp_parser *, tree, bool);
1447 static void cp_parser_statement_seq_opt
1448 (cp_parser *, tree);
1449 static tree cp_parser_selection_statement
1451 static tree cp_parser_condition
1453 static tree cp_parser_iteration_statement
1455 static void cp_parser_for_init_statement
1457 static tree cp_parser_jump_statement
1459 static void cp_parser_declaration_statement
1462 static tree cp_parser_implicitly_scoped_statement
1464 static void cp_parser_already_scoped_statement
1467 /* Declarations [gram.dcl.dcl] */
1469 static void cp_parser_declaration_seq_opt
1471 static void cp_parser_declaration
1473 static void cp_parser_block_declaration
1474 (cp_parser *, bool);
1475 static void cp_parser_simple_declaration
1476 (cp_parser *, bool);
1477 static void cp_parser_decl_specifier_seq
1478 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1479 static tree cp_parser_storage_class_specifier_opt
1481 static tree cp_parser_function_specifier_opt
1482 (cp_parser *, cp_decl_specifier_seq *);
1483 static tree cp_parser_type_specifier
1484 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1486 static tree cp_parser_simple_type_specifier
1487 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1488 static tree cp_parser_type_name
1490 static tree cp_parser_elaborated_type_specifier
1491 (cp_parser *, bool, bool);
1492 static tree cp_parser_enum_specifier
1494 static void cp_parser_enumerator_list
1495 (cp_parser *, tree);
1496 static void cp_parser_enumerator_definition
1497 (cp_parser *, tree);
1498 static tree cp_parser_namespace_name
1500 static void cp_parser_namespace_definition
1502 static void cp_parser_namespace_body
1504 static tree cp_parser_qualified_namespace_specifier
1506 static void cp_parser_namespace_alias_definition
1508 static void cp_parser_using_declaration
1510 static void cp_parser_using_directive
1512 static void cp_parser_asm_definition
1514 static void cp_parser_linkage_specification
1517 /* Declarators [gram.dcl.decl] */
1519 static tree cp_parser_init_declarator
1520 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1521 static cp_declarator *cp_parser_declarator
1522 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1523 static cp_declarator *cp_parser_direct_declarator
1524 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1525 static enum tree_code cp_parser_ptr_operator
1526 (cp_parser *, tree *, cp_cv_quals *);
1527 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1529 static tree cp_parser_declarator_id
1531 static tree cp_parser_type_id
1533 static void cp_parser_type_specifier_seq
1534 (cp_parser *, bool, cp_decl_specifier_seq *);
1535 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1538 (cp_parser *, bool *);
1539 static cp_parameter_declarator *cp_parser_parameter_declaration
1540 (cp_parser *, bool, bool *);
1541 static void cp_parser_function_body
1543 static tree cp_parser_initializer
1544 (cp_parser *, bool *, bool *);
1545 static tree cp_parser_initializer_clause
1546 (cp_parser *, bool *);
1547 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1548 (cp_parser *, bool *);
1550 static bool cp_parser_ctor_initializer_opt_and_function_body
1553 /* Classes [gram.class] */
1555 static tree cp_parser_class_name
1556 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1557 static tree cp_parser_class_specifier
1559 static tree cp_parser_class_head
1560 (cp_parser *, bool *, tree *);
1561 static enum tag_types cp_parser_class_key
1563 static void cp_parser_member_specification_opt
1565 static void cp_parser_member_declaration
1567 static tree cp_parser_pure_specifier
1569 static tree cp_parser_constant_initializer
1572 /* Derived classes [gram.class.derived] */
1574 static tree cp_parser_base_clause
1576 static tree cp_parser_base_specifier
1579 /* Special member functions [gram.special] */
1581 static tree cp_parser_conversion_function_id
1583 static tree cp_parser_conversion_type_id
1585 static cp_declarator *cp_parser_conversion_declarator_opt
1587 static bool cp_parser_ctor_initializer_opt
1589 static void cp_parser_mem_initializer_list
1591 static tree cp_parser_mem_initializer
1593 static tree cp_parser_mem_initializer_id
1596 /* Overloading [gram.over] */
1598 static tree cp_parser_operator_function_id
1600 static tree cp_parser_operator
1603 /* Templates [gram.temp] */
1605 static void cp_parser_template_declaration
1606 (cp_parser *, bool);
1607 static tree cp_parser_template_parameter_list
1609 static tree cp_parser_template_parameter
1610 (cp_parser *, bool *);
1611 static tree cp_parser_type_parameter
1613 static tree cp_parser_template_id
1614 (cp_parser *, bool, bool, bool);
1615 static tree cp_parser_template_name
1616 (cp_parser *, bool, bool, bool, bool *);
1617 static tree cp_parser_template_argument_list
1619 static tree cp_parser_template_argument
1621 static void cp_parser_explicit_instantiation
1623 static void cp_parser_explicit_specialization
1626 /* Exception handling [gram.exception] */
1628 static tree cp_parser_try_block
1630 static bool cp_parser_function_try_block
1632 static void cp_parser_handler_seq
1634 static void cp_parser_handler
1636 static tree cp_parser_exception_declaration
1638 static tree cp_parser_throw_expression
1640 static tree cp_parser_exception_specification_opt
1642 static tree cp_parser_type_id_list
1645 /* GNU Extensions */
1647 static tree cp_parser_asm_specification_opt
1649 static tree cp_parser_asm_operand_list
1651 static tree cp_parser_asm_clobber_list
1653 static tree cp_parser_attributes_opt
1655 static tree cp_parser_attribute_list
1657 static bool cp_parser_extension_opt
1658 (cp_parser *, int *);
1659 static void cp_parser_label_declaration
1662 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1663 static bool cp_parser_pragma
1664 (cp_parser *, enum pragma_context);
1666 /* Objective-C++ Productions */
1668 static tree cp_parser_objc_message_receiver
1670 static tree cp_parser_objc_message_args
1672 static tree cp_parser_objc_message_expression
1674 static tree cp_parser_objc_encode_expression
1676 static tree cp_parser_objc_defs_expression
1678 static tree cp_parser_objc_protocol_expression
1680 static tree cp_parser_objc_selector_expression
1682 static tree cp_parser_objc_expression
1684 static bool cp_parser_objc_selector_p
1686 static tree cp_parser_objc_selector
1688 static tree cp_parser_objc_protocol_refs_opt
1690 static void cp_parser_objc_declaration
1692 static tree cp_parser_objc_statement
1695 /* Utility Routines */
1697 static tree cp_parser_lookup_name
1698 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1699 static tree cp_parser_lookup_name_simple
1700 (cp_parser *, tree);
1701 static tree cp_parser_maybe_treat_template_as_class
1703 static bool cp_parser_check_declarator_template_parameters
1704 (cp_parser *, cp_declarator *);
1705 static bool cp_parser_check_template_parameters
1706 (cp_parser *, unsigned);
1707 static tree cp_parser_simple_cast_expression
1709 static tree cp_parser_global_scope_opt
1710 (cp_parser *, bool);
1711 static bool cp_parser_constructor_declarator_p
1712 (cp_parser *, bool);
1713 static tree cp_parser_function_definition_from_specifiers_and_declarator
1714 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1715 static tree cp_parser_function_definition_after_declarator
1716 (cp_parser *, bool);
1717 static void cp_parser_template_declaration_after_export
1718 (cp_parser *, bool);
1719 static tree cp_parser_single_declaration
1720 (cp_parser *, bool, bool *);
1721 static tree cp_parser_functional_cast
1722 (cp_parser *, tree);
1723 static tree cp_parser_save_member_function_body
1724 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1725 static tree cp_parser_enclosed_template_argument_list
1727 static void cp_parser_save_default_args
1728 (cp_parser *, tree);
1729 static void cp_parser_late_parsing_for_member
1730 (cp_parser *, tree);
1731 static void cp_parser_late_parsing_default_args
1732 (cp_parser *, tree);
1733 static tree cp_parser_sizeof_operand
1734 (cp_parser *, enum rid);
1735 static bool cp_parser_declares_only_class_p
1737 static void cp_parser_set_storage_class
1738 (cp_decl_specifier_seq *, cp_storage_class);
1739 static void cp_parser_set_decl_spec_type
1740 (cp_decl_specifier_seq *, tree, bool);
1741 static bool cp_parser_friend_p
1742 (const cp_decl_specifier_seq *);
1743 static cp_token *cp_parser_require
1744 (cp_parser *, enum cpp_ttype, const char *);
1745 static cp_token *cp_parser_require_keyword
1746 (cp_parser *, enum rid, const char *);
1747 static bool cp_parser_token_starts_function_definition_p
1749 static bool cp_parser_next_token_starts_class_definition_p
1751 static bool cp_parser_next_token_ends_template_argument_p
1753 static bool cp_parser_nth_token_starts_template_argument_list_p
1754 (cp_parser *, size_t);
1755 static enum tag_types cp_parser_token_is_class_key
1757 static void cp_parser_check_class_key
1758 (enum tag_types, tree type);
1759 static void cp_parser_check_access_in_redeclaration
1761 static bool cp_parser_optional_template_keyword
1763 static void cp_parser_pre_parsed_nested_name_specifier
1765 static void cp_parser_cache_group
1766 (cp_parser *, enum cpp_ttype, unsigned);
1767 static void cp_parser_parse_tentatively
1769 static void cp_parser_commit_to_tentative_parse
1771 static void cp_parser_abort_tentative_parse
1773 static bool cp_parser_parse_definitely
1775 static inline bool cp_parser_parsing_tentatively
1777 static bool cp_parser_uncommitted_to_tentative_parse_p
1779 static void cp_parser_error
1780 (cp_parser *, const char *);
1781 static void cp_parser_name_lookup_error
1782 (cp_parser *, tree, tree, const char *);
1783 static bool cp_parser_simulate_error
1785 static void cp_parser_check_type_definition
1787 static void cp_parser_check_for_definition_in_return_type
1788 (cp_declarator *, tree);
1789 static void cp_parser_check_for_invalid_template_id
1790 (cp_parser *, tree);
1791 static bool cp_parser_non_integral_constant_expression
1792 (cp_parser *, const char *);
1793 static void cp_parser_diagnose_invalid_type_name
1794 (cp_parser *, tree, tree);
1795 static bool cp_parser_parse_and_diagnose_invalid_type_name
1797 static int cp_parser_skip_to_closing_parenthesis
1798 (cp_parser *, bool, bool, bool);
1799 static void cp_parser_skip_to_end_of_statement
1801 static void cp_parser_consume_semicolon_at_end_of_statement
1803 static void cp_parser_skip_to_end_of_block_or_statement
1805 static void cp_parser_skip_to_closing_brace
1807 static void cp_parser_skip_until_found
1808 (cp_parser *, enum cpp_ttype, const char *);
1809 static void cp_parser_skip_to_pragma_eol
1810 (cp_parser*, cp_token *);
1811 static bool cp_parser_error_occurred
1813 static bool cp_parser_allow_gnu_extensions_p
1815 static bool cp_parser_is_string_literal
1817 static bool cp_parser_is_keyword
1818 (cp_token *, enum rid);
1819 static tree cp_parser_make_typename_type
1820 (cp_parser *, tree, tree);
1822 /* Returns nonzero if we are parsing tentatively. */
1825 cp_parser_parsing_tentatively (cp_parser* parser)
1827 return parser->context->next != NULL;
1830 /* Returns nonzero if TOKEN is a string literal. */
1833 cp_parser_is_string_literal (cp_token* token)
1835 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1838 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1841 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1843 return token->keyword == keyword;
1846 /* A minimum or maximum operator has been seen. As these are
1847 deprecated, issue a warning. */
1850 cp_parser_warn_min_max (void)
1852 if (warn_deprecated && !in_system_header)
1853 warning (0, "minimum/maximum operators are deprecated");
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857 FILE:LINE: MESSAGE before TOKEN
1858 where TOKEN is the next token in the input stream. MESSAGE
1859 (specified by the caller) is usually of the form "expected
1863 cp_parser_error (cp_parser* parser, const char* message)
1865 if (!cp_parser_simulate_error (parser))
1867 cp_token *token = cp_lexer_peek_token (parser->lexer);
1868 /* This diagnostic makes more sense if it is tagged to the line
1869 of the token we just peeked at. */
1870 cp_lexer_set_source_position_from_token (token);
1872 if (token->type == CPP_PRAGMA)
1874 error ("%<#pragma%> is not allowed here");
1875 cp_parser_skip_to_pragma_eol (parser, token);
1879 c_parse_error (message,
1880 /* Because c_parser_error does not understand
1881 CPP_KEYWORD, keywords are treated like
1883 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1888 /* Issue an error about name-lookup failing. NAME is the
1889 IDENTIFIER_NODE DECL is the result of
1890 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1891 the thing that we hoped to find. */
1894 cp_parser_name_lookup_error (cp_parser* parser,
1897 const char* desired)
1899 /* If name lookup completely failed, tell the user that NAME was not
1901 if (decl == error_mark_node)
1903 if (parser->scope && parser->scope != global_namespace)
1904 error ("%<%D::%D%> has not been declared",
1905 parser->scope, name);
1906 else if (parser->scope == global_namespace)
1907 error ("%<::%D%> has not been declared", name);
1908 else if (parser->object_scope
1909 && !CLASS_TYPE_P (parser->object_scope))
1910 error ("request for member %qD in non-class type %qT",
1911 name, parser->object_scope);
1912 else if (parser->object_scope)
1913 error ("%<%T::%D%> has not been declared",
1914 parser->object_scope, name);
1916 error ("%qD has not been declared", name);
1918 else if (parser->scope && parser->scope != global_namespace)
1919 error ("%<%D::%D%> %s", parser->scope, name, desired);
1920 else if (parser->scope == global_namespace)
1921 error ("%<::%D%> %s", name, desired);
1923 error ("%qD %s", name, desired);
1926 /* If we are parsing tentatively, remember that an error has occurred
1927 during this tentative parse. Returns true if the error was
1928 simulated; false if a message should be issued by the caller. */
1931 cp_parser_simulate_error (cp_parser* parser)
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1935 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941 /* This function is called when a type is defined. If type
1942 definitions are forbidden at this point, an error message is
1946 cp_parser_check_type_definition (cp_parser* parser)
1948 /* If types are forbidden here, issue a message. */
1949 if (parser->type_definition_forbidden_message)
1950 /* Use `%s' to print the string in case there are any escape
1951 characters in the message. */
1952 error ("%s", parser->type_definition_forbidden_message);
1955 /* This function is called when the DECLARATOR is processed. The TYPE
1956 was a type defined in the decl-specifiers. If it is invalid to
1957 define a type in the decl-specifiers for DECLARATOR, an error is
1961 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1964 /* [dcl.fct] forbids type definitions in return types.
1965 Unfortunately, it's not easy to know whether or not we are
1966 processing a return type until after the fact. */
1968 && (declarator->kind == cdk_pointer
1969 || declarator->kind == cdk_reference
1970 || declarator->kind == cdk_ptrmem))
1971 declarator = declarator->declarator;
1973 && declarator->kind == cdk_function)
1975 error ("new types may not be defined in a return type");
1976 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1981 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1982 "<" in any valid C++ program. If the next token is indeed "<",
1983 issue a message warning the user about what appears to be an
1984 invalid attempt to form a template-id. */
1987 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1990 cp_token_position start = 0;
1992 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1995 error ("%qT is not a template", type);
1996 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1997 error ("%qE is not a template", type);
1999 error ("invalid template-id");
2000 /* Remember the location of the invalid "<". */
2001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2002 start = cp_lexer_token_position (parser->lexer, true);
2003 /* Consume the "<". */
2004 cp_lexer_consume_token (parser->lexer);
2005 /* Parse the template arguments. */
2006 cp_parser_enclosed_template_argument_list (parser);
2007 /* Permanently remove the invalid template arguments so that
2008 this error message is not issued again. */
2010 cp_lexer_purge_tokens_after (parser->lexer, start);
2014 /* If parsing an integral constant-expression, issue an error message
2015 about the fact that THING appeared and return true. Otherwise,
2016 return false. In either case, set
2017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2020 cp_parser_non_integral_constant_expression (cp_parser *parser,
2023 parser->non_integral_constant_expression_p = true;
2024 if (parser->integral_constant_expression_p)
2026 if (!parser->allow_non_integral_constant_expression_p)
2028 error ("%s cannot appear in a constant-expression", thing);
2035 /* Emit a diagnostic for an invalid type name. SCOPE is the
2036 qualifying scope (or NULL, if none) for ID. This function commits
2037 to the current active tentative parse, if any. (Otherwise, the
2038 problematic construct might be encountered again later, resulting
2039 in duplicate error messages.) */
2042 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2044 tree decl, old_scope;
2045 /* Try to lookup the identifier. */
2046 old_scope = parser->scope;
2047 parser->scope = scope;
2048 decl = cp_parser_lookup_name_simple (parser, id);
2049 parser->scope = old_scope;
2050 /* If the lookup found a template-name, it means that the user forgot
2051 to specify an argument list. Emit a useful error message. */
2052 if (TREE_CODE (decl) == TEMPLATE_DECL)
2053 error ("invalid use of template-name %qE without an argument list",
2055 else if (!parser->scope)
2057 /* Issue an error message. */
2058 error ("%qE does not name a type", id);
2059 /* If we're in a template class, it's possible that the user was
2060 referring to a type from a base class. For example:
2062 template <typename T> struct A { typedef T X; };
2063 template <typename T> struct B : public A<T> { X x; };
2065 The user should have said "typename A<T>::X". */
2066 if (processing_template_decl && current_class_type
2067 && TYPE_BINFO (current_class_type))
2071 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2075 tree base_type = BINFO_TYPE (b);
2076 if (CLASS_TYPE_P (base_type)
2077 && dependent_type_p (base_type))
2080 /* Go from a particular instantiation of the
2081 template (which will have an empty TYPE_FIELDs),
2082 to the main version. */
2083 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2084 for (field = TYPE_FIELDS (base_type);
2086 field = TREE_CHAIN (field))
2087 if (TREE_CODE (field) == TYPE_DECL
2088 && DECL_NAME (field) == id)
2090 inform ("(perhaps %<typename %T::%E%> was intended)",
2091 BINFO_TYPE (b), id);
2100 /* Here we diagnose qualified-ids where the scope is actually correct,
2101 but the identifier does not resolve to a valid type name. */
2102 else if (parser->scope != error_mark_node)
2104 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2105 error ("%qE in namespace %qE does not name a type",
2107 else if (TYPE_P (parser->scope))
2108 error ("%qE in class %qT does not name a type", id, parser->scope);
2112 cp_parser_commit_to_tentative_parse (parser);
2115 /* Check for a common situation where a type-name should be present,
2116 but is not, and issue a sensible error message. Returns true if an
2117 invalid type-name was detected.
2119 The situation handled by this function are variable declarations of the
2120 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2121 Usually, `ID' should name a type, but if we got here it means that it
2122 does not. We try to emit the best possible error message depending on
2123 how exactly the id-expression looks like.
2127 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2131 cp_parser_parse_tentatively (parser);
2132 id = cp_parser_id_expression (parser,
2133 /*template_keyword_p=*/false,
2134 /*check_dependency_p=*/true,
2135 /*template_p=*/NULL,
2136 /*declarator_p=*/true);
2137 /* After the id-expression, there should be a plain identifier,
2138 otherwise this is not a simple variable declaration. Also, if
2139 the scope is dependent, we cannot do much. */
2140 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2141 || (parser->scope && TYPE_P (parser->scope)
2142 && dependent_type_p (parser->scope)))
2144 cp_parser_abort_tentative_parse (parser);
2147 if (!cp_parser_parse_definitely (parser)
2148 || TREE_CODE (id) != IDENTIFIER_NODE)
2151 /* Emit a diagnostic for the invalid type. */
2152 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2153 /* Skip to the end of the declaration; there's no point in
2154 trying to process it. */
2155 cp_parser_skip_to_end_of_block_or_statement (parser);
2159 /* Consume tokens up to, and including, the next non-nested closing `)'.
2160 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2161 are doing error recovery. Returns -1 if OR_COMMA is true and we
2162 found an unnested comma. */
2165 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2170 unsigned paren_depth = 0;
2171 unsigned brace_depth = 0;
2173 if (recovering && !or_comma
2174 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2179 cp_token * token = cp_lexer_peek_token (parser->lexer);
2181 switch (token->type)
2184 case CPP_PRAGMA_EOL:
2185 /* If we've run out of tokens, then there is no closing `)'. */
2189 /* This matches the processing in skip_to_end_of_statement. */
2194 case CPP_OPEN_BRACE:
2197 case CPP_CLOSE_BRACE:
2203 if (recovering && or_comma && !brace_depth && !paren_depth)
2207 case CPP_OPEN_PAREN:
2212 case CPP_CLOSE_PAREN:
2213 if (!brace_depth && !paren_depth--)
2216 cp_lexer_consume_token (parser->lexer);
2225 /* Consume the token. */
2226 cp_lexer_consume_token (parser->lexer);
2230 /* Consume tokens until we reach the end of the current statement.
2231 Normally, that will be just before consuming a `;'. However, if a
2232 non-nested `}' comes first, then we stop before consuming that. */
2235 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2237 unsigned nesting_depth = 0;
2241 cp_token *token = cp_lexer_peek_token (parser->lexer);
2243 switch (token->type)
2246 case CPP_PRAGMA_EOL:
2247 /* If we've run out of tokens, stop. */
2251 /* If the next token is a `;', we have reached the end of the
2257 case CPP_CLOSE_BRACE:
2258 /* If this is a non-nested '}', stop before consuming it.
2259 That way, when confronted with something like:
2263 we stop before consuming the closing '}', even though we
2264 have not yet reached a `;'. */
2265 if (nesting_depth == 0)
2268 /* If it is the closing '}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2278 if (--nesting_depth == 0)
2280 cp_lexer_consume_token (parser->lexer);
2284 case CPP_OPEN_BRACE:
2292 /* Consume the token. */
2293 cp_lexer_consume_token (parser->lexer);
2297 /* This function is called at the end of a statement or declaration.
2298 If the next token is a semicolon, it is consumed; otherwise, error
2299 recovery is attempted. */
2302 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2304 /* Look for the trailing `;'. */
2305 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2307 /* If there is additional (erroneous) input, skip to the end of
2309 cp_parser_skip_to_end_of_statement (parser);
2310 /* If the next token is now a `;', consume it. */
2311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2312 cp_lexer_consume_token (parser->lexer);
2316 /* Skip tokens until we have consumed an entire block, or until we
2317 have consumed a non-nested `;'. */
2320 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2322 int nesting_depth = 0;
2324 while (nesting_depth >= 0)
2326 cp_token *token = cp_lexer_peek_token (parser->lexer);
2328 switch (token->type)
2331 case CPP_PRAGMA_EOL:
2332 /* If we've run out of tokens, stop. */
2336 /* Stop if this is an unnested ';'. */
2341 case CPP_CLOSE_BRACE:
2342 /* Stop if this is an unnested '}', or closes the outermost
2349 case CPP_OPEN_BRACE:
2358 /* Consume the token. */
2359 cp_lexer_consume_token (parser->lexer);
2363 /* Skip tokens until a non-nested closing curly brace is the next
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2369 unsigned nesting_depth = 0;
2373 cp_token *token = cp_lexer_peek_token (parser->lexer);
2375 switch (token->type)
2378 case CPP_PRAGMA_EOL:
2379 /* If we've run out of tokens, stop. */
2382 case CPP_CLOSE_BRACE:
2383 /* If the next token is a non-nested `}', then we have reached
2384 the end of the current block. */
2385 if (nesting_depth-- == 0)
2389 case CPP_OPEN_BRACE:
2390 /* If it the next token is a `{', then we are entering a new
2391 block. Consume the entire block. */
2399 /* Consume the token. */
2400 cp_lexer_consume_token (parser->lexer);
2404 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2405 parameter is the PRAGMA token, allowing us to purge the entire pragma
2409 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2413 parser->lexer->in_pragma = false;
2416 token = cp_lexer_consume_token (parser->lexer);
2417 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2419 /* Ensure that the pragma is not parsed again. */
2420 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2423 /* This is a simple wrapper around make_typename_type. When the id is
2424 an unresolved identifier node, we can provide a superior diagnostic
2425 using cp_parser_diagnose_invalid_type_name. */
2428 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2431 if (TREE_CODE (id) == IDENTIFIER_NODE)
2433 result = make_typename_type (scope, id, typename_type,
2434 /*complain=*/tf_none);
2435 if (result == error_mark_node)
2436 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2439 return make_typename_type (scope, id, typename_type, tf_error);
2443 /* Create a new C++ parser. */
2446 cp_parser_new (void)
2452 /* cp_lexer_new_main is called before calling ggc_alloc because
2453 cp_lexer_new_main might load a PCH file. */
2454 lexer = cp_lexer_new_main ();
2456 /* Initialize the binops_by_token so that we can get the tree
2457 directly from the token. */
2458 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2459 binops_by_token[binops[i].token_type] = binops[i];
2461 parser = GGC_CNEW (cp_parser);
2462 parser->lexer = lexer;
2463 parser->context = cp_parser_context_new (NULL);
2465 /* For now, we always accept GNU extensions. */
2466 parser->allow_gnu_extensions_p = 1;
2468 /* The `>' token is a greater-than operator, not the end of a
2470 parser->greater_than_is_operator_p = true;
2472 parser->default_arg_ok_p = true;
2474 /* We are not parsing a constant-expression. */
2475 parser->integral_constant_expression_p = false;
2476 parser->allow_non_integral_constant_expression_p = false;
2477 parser->non_integral_constant_expression_p = false;
2479 /* Local variable names are not forbidden. */
2480 parser->local_variables_forbidden_p = false;
2482 /* We are not processing an `extern "C"' declaration. */
2483 parser->in_unbraced_linkage_specification_p = false;
2485 /* We are not processing a declarator. */
2486 parser->in_declarator_p = false;
2488 /* We are not processing a template-argument-list. */
2489 parser->in_template_argument_list_p = false;
2491 /* We are not in an iteration statement. */
2492 parser->in_iteration_statement_p = false;
2494 /* We are not in a switch statement. */
2495 parser->in_switch_statement_p = false;
2497 /* We are not parsing a type-id inside an expression. */
2498 parser->in_type_id_in_expr_p = false;
2500 /* Declarations aren't implicitly extern "C". */
2501 parser->implicit_extern_c = false;
2503 /* String literals should be translated to the execution character set. */
2504 parser->translate_strings_p = true;
2506 /* The unparsed function queue is empty. */
2507 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2509 /* There are no classes being defined. */
2510 parser->num_classes_being_defined = 0;
2512 /* No template parameters apply. */
2513 parser->num_template_parameter_lists = 0;
2518 /* Create a cp_lexer structure which will emit the tokens in CACHE
2519 and push it onto the parser's lexer stack. This is used for delayed
2520 parsing of in-class method bodies and default arguments, and should
2521 not be confused with tentative parsing. */
2523 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2525 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2526 lexer->next = parser->lexer;
2527 parser->lexer = lexer;
2529 /* Move the current source position to that of the first token in the
2531 cp_lexer_set_source_position_from_token (lexer->next_token);
2534 /* Pop the top lexer off the parser stack. This is never used for the
2535 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2537 cp_parser_pop_lexer (cp_parser *parser)
2539 cp_lexer *lexer = parser->lexer;
2540 parser->lexer = lexer->next;
2541 cp_lexer_destroy (lexer);
2543 /* Put the current source position back where it was before this
2544 lexer was pushed. */
2545 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2548 /* Lexical conventions [gram.lex] */
2550 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2554 cp_parser_identifier (cp_parser* parser)
2558 /* Look for the identifier. */
2559 token = cp_parser_require (parser, CPP_NAME, "identifier");
2560 /* Return the value. */
2561 return token ? token->value : error_mark_node;
2564 /* Parse a sequence of adjacent string constants. Returns a
2565 TREE_STRING representing the combined, nul-terminated string
2566 constant. If TRANSLATE is true, translate the string to the
2567 execution character set. If WIDE_OK is true, a wide string is
2570 C++98 [lex.string] says that if a narrow string literal token is
2571 adjacent to a wide string literal token, the behavior is undefined.
2572 However, C99 6.4.5p4 says that this results in a wide string literal.
2573 We follow C99 here, for consistency with the C front end.
2575 This code is largely lifted from lex_string() in c-lex.c.
2577 FUTURE: ObjC++ will need to handle @-strings here. */
2579 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2584 struct obstack str_ob;
2585 cpp_string str, istr, *strs;
2588 tok = cp_lexer_peek_token (parser->lexer);
2589 if (!cp_parser_is_string_literal (tok))
2591 cp_parser_error (parser, "expected string-literal");
2592 return error_mark_node;
2595 /* Try to avoid the overhead of creating and destroying an obstack
2596 for the common case of just one string. */
2597 if (!cp_parser_is_string_literal
2598 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2600 cp_lexer_consume_token (parser->lexer);
2602 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2603 str.len = TREE_STRING_LENGTH (tok->value);
2605 if (tok->type == CPP_WSTRING)
2612 gcc_obstack_init (&str_ob);
2617 cp_lexer_consume_token (parser->lexer);
2619 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2620 str.len = TREE_STRING_LENGTH (tok->value);
2621 if (tok->type == CPP_WSTRING)
2624 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2626 tok = cp_lexer_peek_token (parser->lexer);
2628 while (cp_parser_is_string_literal (tok));
2630 strs = (cpp_string *) obstack_finish (&str_ob);
2633 if (wide && !wide_ok)
2635 cp_parser_error (parser, "a wide string is invalid in this context");
2639 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2640 (parse_in, strs, count, &istr, wide))
2642 value = build_string (istr.len, (char *)istr.text);
2643 free ((void *)istr.text);
2645 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2646 value = fix_string_type (value);
2649 /* cpp_interpret_string has issued an error. */
2650 value = error_mark_node;
2653 obstack_free (&str_ob, 0);
2659 /* Basic concepts [gram.basic] */
2661 /* Parse a translation-unit.
2664 declaration-seq [opt]
2666 Returns TRUE if all went well. */
2669 cp_parser_translation_unit (cp_parser* parser)
2671 /* The address of the first non-permanent object on the declarator
2673 static void *declarator_obstack_base;
2677 /* Create the declarator obstack, if necessary. */
2678 if (!cp_error_declarator)
2680 gcc_obstack_init (&declarator_obstack);
2681 /* Create the error declarator. */
2682 cp_error_declarator = make_declarator (cdk_error);
2683 /* Create the empty parameter list. */
2684 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2685 /* Remember where the base of the declarator obstack lies. */
2686 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2689 cp_parser_declaration_seq_opt (parser);
2691 /* If there are no tokens left then all went well. */
2692 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2694 /* Get rid of the token array; we don't need it any more. */
2695 cp_lexer_destroy (parser->lexer);
2696 parser->lexer = NULL;
2698 /* This file might have been a context that's implicitly extern
2699 "C". If so, pop the lang context. (Only relevant for PCH.) */
2700 if (parser->implicit_extern_c)
2702 pop_lang_context ();
2703 parser->implicit_extern_c = false;
2707 finish_translation_unit ();
2713 cp_parser_error (parser, "expected declaration");
2717 /* Make sure the declarator obstack was fully cleaned up. */
2718 gcc_assert (obstack_next_free (&declarator_obstack)
2719 == declarator_obstack_base);
2721 /* All went well. */
2725 /* Expressions [gram.expr] */
2727 /* Parse a primary-expression.
2738 ( compound-statement )
2739 __builtin_va_arg ( assignment-expression , type-id )
2740 __builtin_offsetof ( type-id , offsetof-expression )
2742 Objective-C++ Extension:
2750 ADDRESS_P is true iff this expression was immediately preceded by
2751 "&" and therefore might denote a pointer-to-member. CAST_P is true
2752 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2753 true iff this expression is a template argument.
2755 Returns a representation of the expression. Upon return, *IDK
2756 indicates what kind of id-expression (if any) was present. */
2759 cp_parser_primary_expression (cp_parser *parser,
2762 bool template_arg_p,
2767 /* Assume the primary expression is not an id-expression. */
2768 *idk = CP_ID_KIND_NONE;
2770 /* Peek at the next token. */
2771 token = cp_lexer_peek_token (parser->lexer);
2772 switch (token->type)
2783 token = cp_lexer_consume_token (parser->lexer);
2784 /* Floating-point literals are only allowed in an integral
2785 constant expression if they are cast to an integral or
2786 enumeration type. */
2787 if (TREE_CODE (token->value) == REAL_CST
2788 && parser->integral_constant_expression_p
2791 /* CAST_P will be set even in invalid code like "int(2.7 +
2792 ...)". Therefore, we have to check that the next token
2793 is sure to end the cast. */
2796 cp_token *next_token;
2798 next_token = cp_lexer_peek_token (parser->lexer);
2799 if (/* The comma at the end of an
2800 enumerator-definition. */
2801 next_token->type != CPP_COMMA
2802 /* The curly brace at the end of an enum-specifier. */
2803 && next_token->type != CPP_CLOSE_BRACE
2804 /* The end of a statement. */
2805 && next_token->type != CPP_SEMICOLON
2806 /* The end of the cast-expression. */
2807 && next_token->type != CPP_CLOSE_PAREN
2808 /* The end of an array bound. */
2809 && next_token->type != CPP_CLOSE_SQUARE
2810 /* The closing ">" in a template-argument-list. */
2811 && (next_token->type != CPP_GREATER
2812 || parser->greater_than_is_operator_p))
2816 /* If we are within a cast, then the constraint that the
2817 cast is to an integral or enumeration type will be
2818 checked at that point. If we are not within a cast, then
2819 this code is invalid. */
2821 cp_parser_non_integral_constant_expression
2822 (parser, "floating-point literal");
2824 return token->value;
2828 /* ??? Should wide strings be allowed when parser->translate_strings_p
2829 is false (i.e. in attributes)? If not, we can kill the third
2830 argument to cp_parser_string_literal. */
2831 return cp_parser_string_literal (parser,
2832 parser->translate_strings_p,
2835 case CPP_OPEN_PAREN:
2838 bool saved_greater_than_is_operator_p;
2840 /* Consume the `('. */
2841 cp_lexer_consume_token (parser->lexer);
2842 /* Within a parenthesized expression, a `>' token is always
2843 the greater-than operator. */
2844 saved_greater_than_is_operator_p
2845 = parser->greater_than_is_operator_p;
2846 parser->greater_than_is_operator_p = true;
2847 /* If we see `( { ' then we are looking at the beginning of
2848 a GNU statement-expression. */
2849 if (cp_parser_allow_gnu_extensions_p (parser)
2850 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2852 /* Statement-expressions are not allowed by the standard. */
2854 pedwarn ("ISO C++ forbids braced-groups within expressions");
2856 /* And they're not allowed outside of a function-body; you
2857 cannot, for example, write:
2859 int i = ({ int j = 3; j + 1; });
2861 at class or namespace scope. */
2862 if (!at_function_scope_p ())
2863 error ("statement-expressions are allowed only inside functions");
2864 /* Start the statement-expression. */
2865 expr = begin_stmt_expr ();
2866 /* Parse the compound-statement. */
2867 cp_parser_compound_statement (parser, expr, false);
2869 expr = finish_stmt_expr (expr, false);
2873 /* Parse the parenthesized expression. */
2874 expr = cp_parser_expression (parser, cast_p);
2875 /* Let the front end know that this expression was
2876 enclosed in parentheses. This matters in case, for
2877 example, the expression is of the form `A::B', since
2878 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2880 finish_parenthesized_expr (expr);
2882 /* The `>' token might be the end of a template-id or
2883 template-parameter-list now. */
2884 parser->greater_than_is_operator_p
2885 = saved_greater_than_is_operator_p;
2886 /* Consume the `)'. */
2887 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2888 cp_parser_skip_to_end_of_statement (parser);
2894 switch (token->keyword)
2896 /* These two are the boolean literals. */
2898 cp_lexer_consume_token (parser->lexer);
2899 return boolean_true_node;
2901 cp_lexer_consume_token (parser->lexer);
2902 return boolean_false_node;
2904 /* The `__null' literal. */
2906 cp_lexer_consume_token (parser->lexer);
2909 /* Recognize the `this' keyword. */
2911 cp_lexer_consume_token (parser->lexer);
2912 if (parser->local_variables_forbidden_p)
2914 error ("%<this%> may not be used in this context");
2915 return error_mark_node;
2917 /* Pointers cannot appear in constant-expressions. */
2918 if (cp_parser_non_integral_constant_expression (parser,
2920 return error_mark_node;
2921 return finish_this_expr ();
2923 /* The `operator' keyword can be the beginning of an
2928 case RID_FUNCTION_NAME:
2929 case RID_PRETTY_FUNCTION_NAME:
2930 case RID_C99_FUNCTION_NAME:
2931 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2932 __func__ are the names of variables -- but they are
2933 treated specially. Therefore, they are handled here,
2934 rather than relying on the generic id-expression logic
2935 below. Grammatically, these names are id-expressions.
2937 Consume the token. */
2938 token = cp_lexer_consume_token (parser->lexer);
2939 /* Look up the name. */
2940 return finish_fname (token->value);
2947 /* The `__builtin_va_arg' construct is used to handle
2948 `va_arg'. Consume the `__builtin_va_arg' token. */
2949 cp_lexer_consume_token (parser->lexer);
2950 /* Look for the opening `('. */
2951 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2952 /* Now, parse the assignment-expression. */
2953 expression = cp_parser_assignment_expression (parser,
2955 /* Look for the `,'. */
2956 cp_parser_require (parser, CPP_COMMA, "`,'");
2957 /* Parse the type-id. */
2958 type = cp_parser_type_id (parser);
2959 /* Look for the closing `)'. */
2960 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2961 /* Using `va_arg' in a constant-expression is not
2963 if (cp_parser_non_integral_constant_expression (parser,
2965 return error_mark_node;
2966 return build_x_va_arg (expression, type);
2970 return cp_parser_builtin_offsetof (parser);
2972 /* Objective-C++ expressions. */
2974 case RID_AT_PROTOCOL:
2975 case RID_AT_SELECTOR:
2976 return cp_parser_objc_expression (parser);
2979 cp_parser_error (parser, "expected primary-expression");
2980 return error_mark_node;
2983 /* An id-expression can start with either an identifier, a
2984 `::' as the beginning of a qualified-id, or the "operator"
2988 case CPP_TEMPLATE_ID:
2989 case CPP_NESTED_NAME_SPECIFIER:
2993 const char *error_msg;
2998 /* Parse the id-expression. */
3000 = cp_parser_id_expression (parser,
3001 /*template_keyword_p=*/false,
3002 /*check_dependency_p=*/true,
3004 /*declarator_p=*/false);
3005 if (id_expression == error_mark_node)
3006 return error_mark_node;
3007 token = cp_lexer_peek_token (parser->lexer);
3008 done = (token->type != CPP_OPEN_SQUARE
3009 && token->type != CPP_OPEN_PAREN
3010 && token->type != CPP_DOT
3011 && token->type != CPP_DEREF
3012 && token->type != CPP_PLUS_PLUS
3013 && token->type != CPP_MINUS_MINUS);
3014 /* If we have a template-id, then no further lookup is
3015 required. If the template-id was for a template-class, we
3016 will sometimes have a TYPE_DECL at this point. */
3017 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3018 || TREE_CODE (id_expression) == TYPE_DECL)
3019 decl = id_expression;
3020 /* Look up the name. */
3023 tree ambiguous_decls;
3025 decl = cp_parser_lookup_name (parser, id_expression,
3028 /*is_namespace=*/false,
3029 /*check_dependency=*/true,
3031 /* If the lookup was ambiguous, an error will already have
3033 if (ambiguous_decls)
3034 return error_mark_node;
3036 /* In Objective-C++, an instance variable (ivar) may be preferred
3037 to whatever cp_parser_lookup_name() found. */
3038 decl = objc_lookup_ivar (decl, id_expression);
3040 /* If name lookup gives us a SCOPE_REF, then the
3041 qualifying scope was dependent. */
3042 if (TREE_CODE (decl) == SCOPE_REF)
3044 /* Check to see if DECL is a local variable in a context
3045 where that is forbidden. */
3046 if (parser->local_variables_forbidden_p
3047 && local_variable_p (decl))
3049 /* It might be that we only found DECL because we are
3050 trying to be generous with pre-ISO scoping rules.
3051 For example, consider:
3055 for (int i = 0; i < 10; ++i) {}
3056 extern void f(int j = i);
3059 Here, name look up will originally find the out
3060 of scope `i'. We need to issue a warning message,
3061 but then use the global `i'. */
3062 decl = check_for_out_of_scope_variable (decl);
3063 if (local_variable_p (decl))
3065 error ("local variable %qD may not appear in this context",
3067 return error_mark_node;
3072 decl = (finish_id_expression
3073 (id_expression, decl, parser->scope,
3075 parser->integral_constant_expression_p,
3076 parser->allow_non_integral_constant_expression_p,
3077 &parser->non_integral_constant_expression_p,
3078 template_p, done, address_p,
3082 cp_parser_error (parser, error_msg);
3086 /* Anything else is an error. */
3088 /* ...unless we have an Objective-C++ message or string literal, that is. */
3089 if (c_dialect_objc ()
3090 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3091 return cp_parser_objc_expression (parser);
3093 cp_parser_error (parser, "expected primary-expression");
3094 return error_mark_node;
3098 /* Parse an id-expression.
3105 :: [opt] nested-name-specifier template [opt] unqualified-id
3107 :: operator-function-id
3110 Return a representation of the unqualified portion of the
3111 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3112 a `::' or nested-name-specifier.
3114 Often, if the id-expression was a qualified-id, the caller will
3115 want to make a SCOPE_REF to represent the qualified-id. This
3116 function does not do this in order to avoid wastefully creating
3117 SCOPE_REFs when they are not required.
3119 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3122 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3123 uninstantiated templates.
3125 If *TEMPLATE_P is non-NULL, it is set to true iff the
3126 `template' keyword is used to explicitly indicate that the entity
3127 named is a template.
3129 If DECLARATOR_P is true, the id-expression is appearing as part of
3130 a declarator, rather than as part of an expression. */
3133 cp_parser_id_expression (cp_parser *parser,
3134 bool template_keyword_p,
3135 bool check_dependency_p,
3139 bool global_scope_p;
3140 bool nested_name_specifier_p;
3142 /* Assume the `template' keyword was not used. */
3144 *template_p = template_keyword_p;
3146 /* Look for the optional `::' operator. */
3148 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3150 /* Look for the optional nested-name-specifier. */
3151 nested_name_specifier_p
3152 = (cp_parser_nested_name_specifier_opt (parser,
3153 /*typename_keyword_p=*/false,
3158 /* If there is a nested-name-specifier, then we are looking at
3159 the first qualified-id production. */
3160 if (nested_name_specifier_p)
3163 tree saved_object_scope;
3164 tree saved_qualifying_scope;
3165 tree unqualified_id;
3168 /* See if the next token is the `template' keyword. */
3170 template_p = &is_template;
3171 *template_p = cp_parser_optional_template_keyword (parser);
3172 /* Name lookup we do during the processing of the
3173 unqualified-id might obliterate SCOPE. */
3174 saved_scope = parser->scope;
3175 saved_object_scope = parser->object_scope;
3176 saved_qualifying_scope = parser->qualifying_scope;
3177 /* Process the final unqualified-id. */
3178 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3181 /* Restore the SAVED_SCOPE for our caller. */
3182 parser->scope = saved_scope;
3183 parser->object_scope = saved_object_scope;
3184 parser->qualifying_scope = saved_qualifying_scope;
3186 return unqualified_id;
3188 /* Otherwise, if we are in global scope, then we are looking at one
3189 of the other qualified-id productions. */
3190 else if (global_scope_p)
3195 /* Peek at the next token. */
3196 token = cp_lexer_peek_token (parser->lexer);
3198 /* If it's an identifier, and the next token is not a "<", then
3199 we can avoid the template-id case. This is an optimization
3200 for this common case. */
3201 if (token->type == CPP_NAME
3202 && !cp_parser_nth_token_starts_template_argument_list_p
3204 return cp_parser_identifier (parser);
3206 cp_parser_parse_tentatively (parser);
3207 /* Try a template-id. */
3208 id = cp_parser_template_id (parser,
3209 /*template_keyword_p=*/false,
3210 /*check_dependency_p=*/true,
3212 /* If that worked, we're done. */
3213 if (cp_parser_parse_definitely (parser))
3216 /* Peek at the next token. (Changes in the token buffer may
3217 have invalidated the pointer obtained above.) */
3218 token = cp_lexer_peek_token (parser->lexer);
3220 switch (token->type)
3223 return cp_parser_identifier (parser);
3226 if (token->keyword == RID_OPERATOR)
3227 return cp_parser_operator_function_id (parser);
3231 cp_parser_error (parser, "expected id-expression");
3232 return error_mark_node;
3236 return cp_parser_unqualified_id (parser, template_keyword_p,
3237 /*check_dependency_p=*/true,
3241 /* Parse an unqualified-id.
3245 operator-function-id
3246 conversion-function-id
3250 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3251 keyword, in a construct like `A::template ...'.
3253 Returns a representation of unqualified-id. For the `identifier'
3254 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3255 production a BIT_NOT_EXPR is returned; the operand of the
3256 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3257 other productions, see the documentation accompanying the
3258 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3259 names are looked up in uninstantiated templates. If DECLARATOR_P
3260 is true, the unqualified-id is appearing as part of a declarator,
3261 rather than as part of an expression. */
3264 cp_parser_unqualified_id (cp_parser* parser,
3265 bool template_keyword_p,
3266 bool check_dependency_p,
3271 /* Peek at the next token. */
3272 token = cp_lexer_peek_token (parser->lexer);
3274 switch (token->type)
3280 /* We don't know yet whether or not this will be a
3282 cp_parser_parse_tentatively (parser);
3283 /* Try a template-id. */
3284 id = cp_parser_template_id (parser, template_keyword_p,
3287 /* If it worked, we're done. */
3288 if (cp_parser_parse_definitely (parser))
3290 /* Otherwise, it's an ordinary identifier. */
3291 return cp_parser_identifier (parser);
3294 case CPP_TEMPLATE_ID:
3295 return cp_parser_template_id (parser, template_keyword_p,
3302 tree qualifying_scope;
3307 /* Consume the `~' token. */
3308 cp_lexer_consume_token (parser->lexer);
3309 /* Parse the class-name. The standard, as written, seems to
3312 template <typename T> struct S { ~S (); };
3313 template <typename T> S<T>::~S() {}
3315 is invalid, since `~' must be followed by a class-name, but
3316 `S<T>' is dependent, and so not known to be a class.
3317 That's not right; we need to look in uninstantiated
3318 templates. A further complication arises from:
3320 template <typename T> void f(T t) {
3324 Here, it is not possible to look up `T' in the scope of `T'
3325 itself. We must look in both the current scope, and the
3326 scope of the containing complete expression.
3328 Yet another issue is:
3337 The standard does not seem to say that the `S' in `~S'
3338 should refer to the type `S' and not the data member
3341 /* DR 244 says that we look up the name after the "~" in the
3342 same scope as we looked up the qualifying name. That idea
3343 isn't fully worked out; it's more complicated than that. */
3344 scope = parser->scope;
3345 object_scope = parser->object_scope;
3346 qualifying_scope = parser->qualifying_scope;
3348 /* If the name is of the form "X::~X" it's OK. */
3349 if (scope && TYPE_P (scope)
3350 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3351 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3353 && (cp_lexer_peek_token (parser->lexer)->value
3354 == TYPE_IDENTIFIER (scope)))
3356 cp_lexer_consume_token (parser->lexer);
3357 return build_nt (BIT_NOT_EXPR, scope);
3360 /* If there was an explicit qualification (S::~T), first look
3361 in the scope given by the qualification (i.e., S). */
3363 type_decl = NULL_TREE;
3366 cp_parser_parse_tentatively (parser);
3367 type_decl = cp_parser_class_name (parser,
3368 /*typename_keyword_p=*/false,
3369 /*template_keyword_p=*/false,
3371 /*check_dependency=*/false,
3372 /*class_head_p=*/false,
3374 if (cp_parser_parse_definitely (parser))
3377 /* In "N::S::~S", look in "N" as well. */
3378 if (!done && scope && qualifying_scope)
3380 cp_parser_parse_tentatively (parser);
3381 parser->scope = qualifying_scope;
3382 parser->object_scope = NULL_TREE;
3383 parser->qualifying_scope = NULL_TREE;
3385 = cp_parser_class_name (parser,
3386 /*typename_keyword_p=*/false,
3387 /*template_keyword_p=*/false,
3389 /*check_dependency=*/false,
3390 /*class_head_p=*/false,
3392 if (cp_parser_parse_definitely (parser))
3395 /* In "p->S::~T", look in the scope given by "*p" as well. */
3396 else if (!done && object_scope)
3398 cp_parser_parse_tentatively (parser);
3399 parser->scope = object_scope;
3400 parser->object_scope = NULL_TREE;
3401 parser->qualifying_scope = NULL_TREE;
3403 = cp_parser_class_name (parser,
3404 /*typename_keyword_p=*/false,
3405 /*template_keyword_p=*/false,
3407 /*check_dependency=*/false,
3408 /*class_head_p=*/false,
3410 if (cp_parser_parse_definitely (parser))
3413 /* Look in the surrounding context. */
3416 parser->scope = NULL_TREE;
3417 parser->object_scope = NULL_TREE;
3418 parser->qualifying_scope = NULL_TREE;
3420 = cp_parser_class_name (parser,
3421 /*typename_keyword_p=*/false,
3422 /*template_keyword_p=*/false,
3424 /*check_dependency=*/false,
3425 /*class_head_p=*/false,
3428 /* If an error occurred, assume that the name of the
3429 destructor is the same as the name of the qualifying
3430 class. That allows us to keep parsing after running
3431 into ill-formed destructor names. */
3432 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3433 return build_nt (BIT_NOT_EXPR, scope);
3434 else if (type_decl == error_mark_node)
3435 return error_mark_node;
3437 /* Check that destructor name and scope match. */
3438 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3440 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3441 error ("declaration of %<~%T%> as member of %qT",
3443 return error_mark_node;
3448 A typedef-name that names a class shall not be used as the
3449 identifier in the declarator for a destructor declaration. */
3451 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3452 && !DECL_SELF_REFERENCE_P (type_decl)
3453 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3454 error ("typedef-name %qD used as destructor declarator",
3457 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3461 if (token->keyword == RID_OPERATOR)
3465 /* This could be a template-id, so we try that first. */
3466 cp_parser_parse_tentatively (parser);
3467 /* Try a template-id. */
3468 id = cp_parser_template_id (parser, template_keyword_p,
3469 /*check_dependency_p=*/true,
3471 /* If that worked, we're done. */
3472 if (cp_parser_parse_definitely (parser))
3474 /* We still don't know whether we're looking at an
3475 operator-function-id or a conversion-function-id. */
3476 cp_parser_parse_tentatively (parser);
3477 /* Try an operator-function-id. */
3478 id = cp_parser_operator_function_id (parser);
3479 /* If that didn't work, try a conversion-function-id. */
3480 if (!cp_parser_parse_definitely (parser))
3481 id = cp_parser_conversion_function_id (parser);
3488 cp_parser_error (parser, "expected unqualified-id");
3489 return error_mark_node;
3493 /* Parse an (optional) nested-name-specifier.
3495 nested-name-specifier:
3496 class-or-namespace-name :: nested-name-specifier [opt]
3497 class-or-namespace-name :: template nested-name-specifier [opt]
3499 PARSER->SCOPE should be set appropriately before this function is
3500 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3501 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3504 Sets PARSER->SCOPE to the class (TYPE) or namespace
3505 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3506 it unchanged if there is no nested-name-specifier. Returns the new
3507 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3509 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3510 part of a declaration and/or decl-specifier. */
3513 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3514 bool typename_keyword_p,
3515 bool check_dependency_p,
3517 bool is_declaration)
3519 bool success = false;
3520 cp_token_position start = 0;
3523 /* If the next token corresponds to a nested name specifier, there
3524 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3525 false, it may have been true before, in which case something
3526 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3527 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3528 CHECK_DEPENDENCY_P is false, we have to fall through into the
3530 if (check_dependency_p
3531 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3533 cp_parser_pre_parsed_nested_name_specifier (parser);
3534 return parser->scope;
3537 /* Remember where the nested-name-specifier starts. */
3538 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 start = cp_lexer_token_position (parser->lexer, false);
3541 push_deferring_access_checks (dk_deferred);
3548 tree saved_qualifying_scope;
3549 bool template_keyword_p;
3551 /* Spot cases that cannot be the beginning of a
3552 nested-name-specifier. */
3553 token = cp_lexer_peek_token (parser->lexer);
3555 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556 the already parsed nested-name-specifier. */
3557 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3559 /* Grab the nested-name-specifier and continue the loop. */
3560 cp_parser_pre_parsed_nested_name_specifier (parser);
3565 /* Spot cases that cannot be the beginning of a
3566 nested-name-specifier. On the second and subsequent times
3567 through the loop, we look for the `template' keyword. */
3568 if (success && token->keyword == RID_TEMPLATE)
3570 /* A template-id can start a nested-name-specifier. */
3571 else if (token->type == CPP_TEMPLATE_ID)
3575 /* If the next token is not an identifier, then it is
3576 definitely not a class-or-namespace-name. */
3577 if (token->type != CPP_NAME)
3579 /* If the following token is neither a `<' (to begin a
3580 template-id), nor a `::', then we are not looking at a
3581 nested-name-specifier. */
3582 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583 if (token->type != CPP_SCOPE
3584 && !cp_parser_nth_token_starts_template_argument_list_p
3589 /* The nested-name-specifier is optional, so we parse
3591 cp_parser_parse_tentatively (parser);