2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype) type : 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid) keyword : 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header : 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* We use a stack of token pointer for saving token sets. */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
70 static cp_token eof_token = {CPP_EOF, 0, 0, 0, 0, NULL_TREE, {0, 0}};
72 /* The cp_lexer structure represents the C++ lexer. It is responsible
73 for managing the token stream from the preprocessor and supplying
74 it to the parser. Tokens are never added to the cp_lexer after
77 typedef struct cp_lexer GTY (())
79 /* The memory allocated for the buffer. NULL if this lexer does not
80 own the token buffer. */
81 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
82 /* If non-null, a pointer just past the end of the memory allocated
84 cp_token * GTY ((skip)) buffer_end;
86 /* A pointer just past the last available token. The tokens
87 in this lexer are [buffer, last_token). */
88 cp_token_position GTY ((skip)) last_token;
90 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
91 no more available tokens. */
92 cp_token_position GTY ((skip)) next_token;
94 /* A stack indicating positions at which cp_lexer_save_tokens was
95 called. The top entry is the most recent position at which we
96 began saving tokens. If the stack is non-empty, we are saving
98 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
100 /* True if we should output debugging information. */
103 /* The next lexer in a linked list of lexers. */
104 struct cp_lexer *next;
107 /* cp_token_cache is a range of tokens. There is no need to represent
108 allocate heap memory for it, since tokens are never removed from the
109 lexer's array. There is also no need for the GC to walk through
110 a cp_token_cache, since everything in here is referenced through
113 typedef struct cp_token_cache GTY(())
115 /* The beginning of the token range. */
116 cp_token * GTY((skip)) first;
118 /* Points immediately after the last token in the range. */
119 cp_token * GTY ((skip)) last;
124 static cp_lexer *cp_lexer_new_main
126 static cp_lexer *cp_lexer_new_from_tokens
127 (cp_token_cache *tokens);
128 static void cp_lexer_destroy
130 static int cp_lexer_saving_tokens
132 static cp_token_position cp_lexer_token_position
134 static cp_token *cp_lexer_token_at
135 (cp_lexer *, cp_token_position);
136 static void cp_lexer_grow_buffer
138 static void cp_lexer_get_preprocessor_token
139 (cp_lexer *, cp_token *);
140 static inline cp_token *cp_lexer_peek_token
142 static cp_token *cp_lexer_peek_nth_token
143 (cp_lexer *, size_t);
144 static inline bool cp_lexer_next_token_is
145 (cp_lexer *, enum cpp_ttype);
146 static bool cp_lexer_next_token_is_not
147 (cp_lexer *, enum cpp_ttype);
148 static bool cp_lexer_next_token_is_keyword
149 (cp_lexer *, enum rid);
150 static cp_token *cp_lexer_consume_token
152 static void cp_lexer_purge_token
154 static void cp_lexer_purge_tokens_after
155 (cp_lexer *, cp_token_position);
156 static void cp_lexer_handle_pragma
158 static void cp_lexer_save_tokens
160 static void cp_lexer_commit_tokens
162 static void cp_lexer_rollback_tokens
164 #ifdef ENABLE_CHECKING
165 static void cp_lexer_print_token
166 (FILE *, cp_token *);
167 static inline bool cp_lexer_debugging_p
169 static void cp_lexer_start_debugging
170 (cp_lexer *) ATTRIBUTE_UNUSED;
171 static void cp_lexer_stop_debugging
172 (cp_lexer *) ATTRIBUTE_UNUSED;
174 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
175 about passing NULL to functions that require non-NULL arguments
176 (fputs, fprintf). It will never be used, so all we need is a value
177 of the right type that's guaranteed not to be NULL. */
178 #define cp_lexer_debug_stream stdout
179 #define cp_lexer_print_token(str, tok) (void) 0
180 #define cp_lexer_debugging_p(lexer) 0
181 #endif /* ENABLE_CHECKING */
183 static cp_token_cache *cp_token_cache_new
184 (cp_token *, cp_token *);
186 /* Manifest constants. */
187 #define CP_LEXER_BUFFER_SIZE 10000
188 #define CP_SAVED_TOKEN_STACK 5
190 /* A token type for keywords, as opposed to ordinary identifiers. */
191 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
193 /* A token type for template-ids. If a template-id is processed while
194 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
195 the value of the CPP_TEMPLATE_ID is whatever was returned by
196 cp_parser_template_id. */
197 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
199 /* A token type for nested-name-specifiers. If a
200 nested-name-specifier is processed while parsing tentatively, it is
201 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
202 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
203 cp_parser_nested_name_specifier_opt. */
204 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
206 /* A token type for tokens that are not tokens at all; these are used
207 to represent slots in the array where there used to be a token
208 that has now been deleted. */
209 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
211 /* The number of token types, including C++-specific ones. */
212 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
216 #ifdef ENABLE_CHECKING
217 /* The stream to which debugging output should be written. */
218 static FILE *cp_lexer_debug_stream;
219 #endif /* ENABLE_CHECKING */
221 /* Create a new main C++ lexer, the lexer that gets tokens from the
225 cp_lexer_new_main (void)
228 cp_token first_token;
230 /* Tell cpplib we want CPP_PRAGMA tokens. */
231 cpp_get_options (parse_in)->defer_pragmas = true;
233 /* Tell c_lex not to merge string constants. */
234 c_lex_return_raw_strings = true;
236 /* It's possible that lexing the first token will load a PCH file,
237 which is a GC collection point. So we have to grab the first
238 token before allocating any memory. */
239 cp_lexer_get_preprocessor_token (NULL, &first_token);
240 c_common_no_more_pch ();
242 /* Allocate the memory. */
243 lexer = GGC_CNEW (cp_lexer);
245 /* Create the buffer. */
246 lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
247 lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
249 /* There is one token in the buffer. */
250 lexer->last_token = lexer->buffer;
251 lexer->next_token = lexer->buffer;
252 *lexer->next_token = first_token;
254 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
256 #ifdef ENABLE_CHECKING
257 /* Initially we are not debugging. */
258 lexer->debugging_p = false;
259 #endif /* ENABLE_CHECKING */
261 /* Get the rest of the tokens from the preprocessor. */
262 while (lexer->last_token->type != CPP_EOF)
265 if (lexer->last_token == lexer->buffer_end)
266 cp_lexer_grow_buffer (lexer);
267 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
270 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
271 direct calls to c_lex. Those callers all expect c_lex to do
272 string constant concatenation. */
273 c_lex_return_raw_strings = false;
275 gcc_assert (lexer->next_token->type != CPP_PURGED);
279 /* Create a new lexer whose token stream is primed with the tokens in
280 CACHE. When these tokens are exhausted, no new tokens will be read. */
283 cp_lexer_new_from_tokens (cp_token_cache *cache)
285 cp_token *first = cache->first;
286 cp_token *last = cache->last;
287 cp_lexer *lexer = GGC_CNEW (cp_lexer);
289 /* We do not own the buffer. */
290 lexer->buffer = lexer->buffer_end = NULL;
291 lexer->next_token = first == last ? &eof_token : first;
292 lexer->last_token = last;
294 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
296 #ifdef ENABLE_CHECKING
297 /* Initially we are not debugging. */
298 lexer->debugging_p = false;
301 gcc_assert (lexer->next_token->type != CPP_PURGED);
305 /* Frees all resources associated with LEXER. */
308 cp_lexer_destroy (cp_lexer *lexer)
311 ggc_free (lexer->buffer);
312 VEC_free (cp_token_position, lexer->saved_tokens);
316 /* Returns nonzero if debugging information should be output. */
318 #ifdef ENABLE_CHECKING
321 cp_lexer_debugging_p (cp_lexer *lexer)
323 return lexer->debugging_p;
326 #endif /* ENABLE_CHECKING */
328 static inline cp_token_position
329 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
331 gcc_assert (!previous_p || lexer->next_token != &eof_token);
333 return lexer->next_token - previous_p;
336 static inline cp_token *
337 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
342 /* nonzero if we are presently saving tokens. */
345 cp_lexer_saving_tokens (const cp_lexer* lexer)
347 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
350 /* If the buffer is full, make it bigger. */
352 cp_lexer_grow_buffer (cp_lexer* lexer)
354 cp_token *old_buffer;
355 cp_token *new_buffer;
356 ptrdiff_t buffer_length;
358 /* This function should only be called when buffer is full. */
359 gcc_assert (lexer->last_token == lexer->buffer_end);
361 /* Remember the current buffer pointer. It will become invalid,
362 but we will need to do pointer arithmetic involving this
364 old_buffer = lexer->buffer;
365 /* Compute the current buffer size. */
366 buffer_length = lexer->buffer_end - lexer->buffer;
367 /* Allocate a buffer twice as big. */
368 new_buffer = ggc_realloc (lexer->buffer,
369 2 * buffer_length * sizeof (cp_token));
371 /* Recompute buffer positions. */
372 lexer->buffer = new_buffer;
373 lexer->buffer_end = new_buffer + 2 * buffer_length;
374 lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
375 lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
377 /* Clear the rest of the buffer. We never look at this storage,
378 but the garbage collector may. */
379 memset (lexer->last_token, 0,
380 (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
383 /* Store the next token from the preprocessor in *TOKEN. */
386 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389 static int is_extern_c = 0;
393 /* Keep going until we get a token we like. */
396 /* Get a new token from the preprocessor. */
397 token->type = c_lex_with_flags (&token->value, &token->flags);
398 /* Issue messages about tokens we cannot process. */
404 error ("invalid token");
408 /* This is a good token, so we exit the loop. */
413 /* Now we've got our token. */
414 token->location = input_location;
415 token->in_system_header = in_system_header;
417 /* On some systems, some header files are surrounded by an
418 implicit extern "C" block. Set a flag in the token if it
419 comes from such a header. */
420 is_extern_c += pending_lang_change;
421 pending_lang_change = 0;
422 token->implicit_extern_c = is_extern_c > 0;
424 /* Check to see if this token is a keyword. */
425 if (token->type == CPP_NAME
426 && C_IS_RESERVED_WORD (token->value))
428 /* Mark this token as a keyword. */
429 token->type = CPP_KEYWORD;
430 /* Record which keyword. */
431 token->keyword = C_RID_CODE (token->value);
432 /* Update the value. Some keywords are mapped to particular
433 entities, rather than simply having the value of the
434 corresponding IDENTIFIER_NODE. For example, `__const' is
435 mapped to `const'. */
436 token->value = ridpointers[token->keyword];
439 token->keyword = RID_MAX;
442 /* Update the globals input_location and in_system_header from TOKEN. */
444 cp_lexer_set_source_position_from_token (cp_token *token)
446 if (token->type != CPP_EOF)
448 input_location = token->location;
449 in_system_header = token->in_system_header;
453 /* Return a pointer to the next token in the token stream, but do not
456 static inline cp_token *
457 cp_lexer_peek_token (cp_lexer *lexer)
459 if (cp_lexer_debugging_p (lexer))
461 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
462 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
463 putc ('\n', cp_lexer_debug_stream);
465 return lexer->next_token;
468 /* Return true if the next token has the indicated TYPE. */
471 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
473 return cp_lexer_peek_token (lexer)->type == type;
476 /* Return true if the next token does not have the indicated TYPE. */
479 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
481 return !cp_lexer_next_token_is (lexer, type);
484 /* Return true if the next token is the indicated KEYWORD. */
487 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
491 /* Peek at the next token. */
492 token = cp_lexer_peek_token (lexer);
493 /* Check to see if it is the indicated keyword. */
494 return token->keyword == keyword;
497 /* Return a pointer to the Nth token in the token stream. If N is 1,
498 then this is precisely equivalent to cp_lexer_peek_token (except
499 that it is not inline). One would like to disallow that case, but
500 there is one case (cp_parser_nth_token_starts_template_id) where
501 the caller passes a variable for N and it might be 1. */
504 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
508 /* N is 1-based, not zero-based. */
509 gcc_assert (n > 0 && lexer->next_token != &eof_token);
511 if (cp_lexer_debugging_p (lexer))
512 fprintf (cp_lexer_debug_stream,
513 "cp_lexer: peeking ahead %ld at token: ", (long)n);
516 token = lexer->next_token;
520 if (token == lexer->last_token)
526 if (token->type != CPP_PURGED)
530 if (cp_lexer_debugging_p (lexer))
532 cp_lexer_print_token (cp_lexer_debug_stream, token);
533 putc ('\n', cp_lexer_debug_stream);
539 /* Return the next token, and advance the lexer's next_token pointer
540 to point to the next non-purged token. */
543 cp_lexer_consume_token (cp_lexer* lexer)
545 cp_token *token = lexer->next_token;
547 gcc_assert (token != &eof_token);
552 if (lexer->next_token == lexer->last_token)
554 lexer->next_token = &eof_token;
559 while (lexer->next_token->type == CPP_PURGED);
561 cp_lexer_set_source_position_from_token (token);
563 /* Provide debugging output. */
564 if (cp_lexer_debugging_p (lexer))
566 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
567 cp_lexer_print_token (cp_lexer_debug_stream, token);
568 putc ('\n', cp_lexer_debug_stream);
574 /* Permanently remove the next token from the token stream, and
575 advance the next_token pointer to refer to the next non-purged
579 cp_lexer_purge_token (cp_lexer *lexer)
581 cp_token *tok = lexer->next_token;
583 gcc_assert (tok != &eof_token);
584 tok->type = CPP_PURGED;
585 tok->location = UNKNOWN_LOCATION;
586 tok->value = NULL_TREE;
587 tok->keyword = RID_MAX;
592 if (tok == lexer->last_token)
598 while (tok->type == CPP_PURGED);
599 lexer->next_token = tok;
602 /* Permanently remove all tokens after TOK, up to, but not
603 including, the token that will be returned next by
604 cp_lexer_peek_token. */
607 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
609 cp_token *peek = lexer->next_token;
611 if (peek == &eof_token)
612 peek = lexer->last_token;
614 gcc_assert (tok < peek);
616 for ( tok += 1; tok != peek; tok += 1)
618 tok->type = CPP_PURGED;
619 tok->location = UNKNOWN_LOCATION;
620 tok->value = NULL_TREE;
621 tok->keyword = RID_MAX;
625 /* Consume and handle a pragma token. */
627 cp_lexer_handle_pragma (cp_lexer *lexer)
630 cp_token *token = cp_lexer_consume_token (lexer);
631 gcc_assert (token->type == CPP_PRAGMA);
632 gcc_assert (token->value);
634 s.len = TREE_STRING_LENGTH (token->value);
635 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
637 cpp_handle_deferred_pragma (parse_in, &s);
639 /* Clearing token->value here means that we will get an ICE if we
640 try to process this #pragma again (which should be impossible). */
644 /* Begin saving tokens. All tokens consumed after this point will be
648 cp_lexer_save_tokens (cp_lexer* lexer)
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
652 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
654 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
657 /* Commit to the portion of the token stream most recently saved. */
660 cp_lexer_commit_tokens (cp_lexer* lexer)
662 /* Provide debugging output. */
663 if (cp_lexer_debugging_p (lexer))
664 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
666 VEC_pop (cp_token_position, lexer->saved_tokens);
669 /* Return all tokens saved since the last call to cp_lexer_save_tokens
670 to the token stream. Stop saving tokens. */
673 cp_lexer_rollback_tokens (cp_lexer* lexer)
675 /* Provide debugging output. */
676 if (cp_lexer_debugging_p (lexer))
677 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
679 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
682 /* Print a representation of the TOKEN on the STREAM. */
684 #ifdef ENABLE_CHECKING
687 cp_lexer_print_token (FILE * stream, cp_token *token)
689 /* We don't use cpp_type2name here because the parser defines
690 a few tokens of its own. */
691 static const char *const token_names[] = {
692 /* cpplib-defined token types */
698 /* C++ parser token types - see "Manifest constants", above. */
701 "NESTED_NAME_SPECIFIER",
705 /* If we have a name for the token, print it out. Otherwise, we
706 simply give the numeric code. */
707 gcc_assert (token->type < ARRAY_SIZE(token_names));
708 fputs (token_names[token->type], stream);
710 /* For some tokens, print the associated data. */
714 /* Some keywords have a value that is not an IDENTIFIER_NODE.
715 For example, `struct' is mapped to an INTEGER_CST. */
716 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
718 /* else fall through */
720 fputs (IDENTIFIER_POINTER (token->value), stream);
726 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
734 /* Start emitting debugging information. */
737 cp_lexer_start_debugging (cp_lexer* lexer)
739 ++lexer->debugging_p;
742 /* Stop emitting debugging information. */
745 cp_lexer_stop_debugging (cp_lexer* lexer)
747 --lexer->debugging_p;
750 #endif /* ENABLE_CHECKING */
752 /* Create a new cp_token_cache, representing a range of tokens. */
754 static cp_token_cache *
755 cp_token_cache_new (cp_token *first, cp_token *last)
757 cp_token_cache *cache = GGC_NEW (cp_token_cache);
758 cache->first = first;
764 /* Decl-specifiers. */
766 static void clear_decl_specs
767 (cp_decl_specifier_seq *);
769 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
772 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
774 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
779 /* Nothing other than the parser should be creating declarators;
780 declarators are a semi-syntactic representation of C++ entities.
781 Other parts of the front end that need to create entities (like
782 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
784 static cp_declarator *make_id_declarator
786 static cp_declarator *make_call_declarator
787 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
788 static cp_declarator *make_array_declarator
789 (cp_declarator *, tree);
790 static cp_declarator *make_pointer_declarator
791 (cp_cv_quals, cp_declarator *);
792 static cp_declarator *make_reference_declarator
793 (cp_cv_quals, cp_declarator *);
794 static cp_parameter_declarator *make_parameter_declarator
795 (cp_decl_specifier_seq *, cp_declarator *, tree);
796 static cp_declarator *make_ptrmem_declarator
797 (cp_cv_quals, tree, cp_declarator *);
799 cp_declarator *cp_error_declarator;
801 /* The obstack on which declarators and related data structures are
803 static struct obstack declarator_obstack;
805 /* Alloc BYTES from the declarator memory pool. */
808 alloc_declarator (size_t bytes)
810 return obstack_alloc (&declarator_obstack, bytes);
813 /* Allocate a declarator of the indicated KIND. Clear fields that are
814 common to all declarators. */
816 static cp_declarator *
817 make_declarator (cp_declarator_kind kind)
819 cp_declarator *declarator;
821 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
822 declarator->kind = kind;
823 declarator->attributes = NULL_TREE;
824 declarator->declarator = NULL;
829 /* Make a declarator for a generalized identifier. */
832 make_id_declarator (tree id)
834 cp_declarator *declarator;
836 declarator = make_declarator (cdk_id);
837 declarator->u.id.name = id;
838 declarator->u.id.sfk = sfk_none;
843 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
844 of modifiers such as const or volatile to apply to the pointer
845 type, represented as identifiers. */
848 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
850 cp_declarator *declarator;
852 declarator = make_declarator (cdk_pointer);
853 declarator->declarator = target;
854 declarator->u.pointer.qualifiers = cv_qualifiers;
855 declarator->u.pointer.class_type = NULL_TREE;
860 /* Like make_pointer_declarator -- but for references. */
863 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
865 cp_declarator *declarator;
867 declarator = make_declarator (cdk_reference);
868 declarator->declarator = target;
869 declarator->u.pointer.qualifiers = cv_qualifiers;
870 declarator->u.pointer.class_type = NULL_TREE;
875 /* Like make_pointer_declarator -- but for a pointer to a non-static
876 member of CLASS_TYPE. */
879 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
880 cp_declarator *pointee)
882 cp_declarator *declarator;
884 declarator = make_declarator (cdk_ptrmem);
885 declarator->declarator = pointee;
886 declarator->u.pointer.qualifiers = cv_qualifiers;
887 declarator->u.pointer.class_type = class_type;
892 /* Make a declarator for the function given by TARGET, with the
893 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
894 "const"-qualified member function. The EXCEPTION_SPECIFICATION
895 indicates what exceptions can be thrown. */
898 make_call_declarator (cp_declarator *target,
899 cp_parameter_declarator *parms,
900 cp_cv_quals cv_qualifiers,
901 tree exception_specification)
903 cp_declarator *declarator;
905 declarator = make_declarator (cdk_function);
906 declarator->declarator = target;
907 declarator->u.function.parameters = parms;
908 declarator->u.function.qualifiers = cv_qualifiers;
909 declarator->u.function.exception_specification = exception_specification;
914 /* Make a declarator for an array of BOUNDS elements, each of which is
915 defined by ELEMENT. */
918 make_array_declarator (cp_declarator *element, tree bounds)
920 cp_declarator *declarator;
922 declarator = make_declarator (cdk_array);
923 declarator->declarator = element;
924 declarator->u.array.bounds = bounds;
929 cp_parameter_declarator *no_parameters;
931 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
932 DECLARATOR and DEFAULT_ARGUMENT. */
934 cp_parameter_declarator *
935 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
936 cp_declarator *declarator,
937 tree default_argument)
939 cp_parameter_declarator *parameter;
941 parameter = ((cp_parameter_declarator *)
942 alloc_declarator (sizeof (cp_parameter_declarator)));
943 parameter->next = NULL;
945 parameter->decl_specifiers = *decl_specifiers;
947 clear_decl_specs (¶meter->decl_specifiers);
948 parameter->declarator = declarator;
949 parameter->default_argument = default_argument;
950 parameter->ellipsis_p = false;
960 A cp_parser parses the token stream as specified by the C++
961 grammar. Its job is purely parsing, not semantic analysis. For
962 example, the parser breaks the token stream into declarators,
963 expressions, statements, and other similar syntactic constructs.
964 It does not check that the types of the expressions on either side
965 of an assignment-statement are compatible, or that a function is
966 not declared with a parameter of type `void'.
968 The parser invokes routines elsewhere in the compiler to perform
969 semantic analysis and to build up the abstract syntax tree for the
972 The parser (and the template instantiation code, which is, in a
973 way, a close relative of parsing) are the only parts of the
974 compiler that should be calling push_scope and pop_scope, or
975 related functions. The parser (and template instantiation code)
976 keeps track of what scope is presently active; everything else
977 should simply honor that. (The code that generates static
978 initializers may also need to set the scope, in order to check
979 access control correctly when emitting the initializers.)
984 The parser is of the standard recursive-descent variety. Upcoming
985 tokens in the token stream are examined in order to determine which
986 production to use when parsing a non-terminal. Some C++ constructs
987 require arbitrary look ahead to disambiguate. For example, it is
988 impossible, in the general case, to tell whether a statement is an
989 expression or declaration without scanning the entire statement.
990 Therefore, the parser is capable of "parsing tentatively." When the
991 parser is not sure what construct comes next, it enters this mode.
992 Then, while we attempt to parse the construct, the parser queues up
993 error messages, rather than issuing them immediately, and saves the
994 tokens it consumes. If the construct is parsed successfully, the
995 parser "commits", i.e., it issues any queued error messages and
996 the tokens that were being preserved are permanently discarded.
997 If, however, the construct is not parsed successfully, the parser
998 rolls back its state completely so that it can resume parsing using
999 a different alternative.
1004 The performance of the parser could probably be improved substantially.
1005 We could often eliminate the need to parse tentatively by looking ahead
1006 a little bit. In some places, this approach might not entirely eliminate
1007 the need to parse tentatively, but it might still speed up the average
1010 /* Flags that are passed to some parsing functions. These values can
1011 be bitwise-ored together. */
1013 typedef enum cp_parser_flags
1016 CP_PARSER_FLAGS_NONE = 0x0,
1017 /* The construct is optional. If it is not present, then no error
1018 should be issued. */
1019 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1020 /* When parsing a type-specifier, do not allow user-defined types. */
1021 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1024 /* The different kinds of declarators we want to parse. */
1026 typedef enum cp_parser_declarator_kind
1028 /* We want an abstract declarator. */
1029 CP_PARSER_DECLARATOR_ABSTRACT,
1030 /* We want a named declarator. */
1031 CP_PARSER_DECLARATOR_NAMED,
1032 /* We don't mind, but the name must be an unqualified-id. */
1033 CP_PARSER_DECLARATOR_EITHER
1034 } cp_parser_declarator_kind;
1036 /* The precedence values used to parse binary expressions. The minimum value
1037 of PREC must be 1, because zero is reserved to quickly discriminate
1038 binary operators from other tokens. */
1043 PREC_LOGICAL_OR_EXPRESSION,
1044 PREC_LOGICAL_AND_EXPRESSION,
1045 PREC_INCLUSIVE_OR_EXPRESSION,
1046 PREC_EXCLUSIVE_OR_EXPRESSION,
1047 PREC_AND_EXPRESSION,
1048 PREC_EQUALITY_EXPRESSION,
1049 PREC_RELATIONAL_EXPRESSION,
1050 PREC_SHIFT_EXPRESSION,
1051 PREC_ADDITIVE_EXPRESSION,
1052 PREC_MULTIPLICATIVE_EXPRESSION,
1054 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1057 /* A mapping from a token type to a corresponding tree node type, with a
1058 precedence value. */
1060 typedef struct cp_parser_binary_operations_map_node
1062 /* The token type. */
1063 enum cpp_ttype token_type;
1064 /* The corresponding tree code. */
1065 enum tree_code tree_type;
1066 /* The precedence of this operator. */
1067 enum cp_parser_prec prec;
1068 } cp_parser_binary_operations_map_node;
1070 /* The status of a tentative parse. */
1072 typedef enum cp_parser_status_kind
1074 /* No errors have occurred. */
1075 CP_PARSER_STATUS_KIND_NO_ERROR,
1076 /* An error has occurred. */
1077 CP_PARSER_STATUS_KIND_ERROR,
1078 /* We are committed to this tentative parse, whether or not an error
1080 CP_PARSER_STATUS_KIND_COMMITTED
1081 } cp_parser_status_kind;
1083 typedef struct cp_parser_expression_stack_entry
1086 enum tree_code tree_type;
1088 } cp_parser_expression_stack_entry;
1090 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1091 entries because precedence levels on the stack are monotonically
1093 typedef struct cp_parser_expression_stack_entry
1094 cp_parser_expression_stack[NUM_PREC_VALUES];
1096 /* Context that is saved and restored when parsing tentatively. */
1097 typedef struct cp_parser_context GTY (())
1099 /* If this is a tentative parsing context, the status of the
1101 enum cp_parser_status_kind status;
1102 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1103 that are looked up in this context must be looked up both in the
1104 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1105 the context of the containing expression. */
1108 /* The next parsing context in the stack. */
1109 struct cp_parser_context *next;
1110 } cp_parser_context;
1114 /* Constructors and destructors. */
1116 static cp_parser_context *cp_parser_context_new
1117 (cp_parser_context *);
1119 /* Class variables. */
1121 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1123 /* The operator-precedence table used by cp_parser_binary_expression.
1124 Transformed into an associative array (binops_by_token) by
1127 static const cp_parser_binary_operations_map_node binops[] = {
1128 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1129 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1131 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1132 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1133 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1135 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1136 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1138 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1139 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1141 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1142 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1143 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1144 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1145 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1146 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1148 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1149 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1151 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1153 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1155 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1157 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1159 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1162 /* The same as binops, but initialized by cp_parser_new so that
1163 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1165 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1167 /* Constructors and destructors. */
1169 /* Construct a new context. The context below this one on the stack
1170 is given by NEXT. */
1172 static cp_parser_context *
1173 cp_parser_context_new (cp_parser_context* next)
1175 cp_parser_context *context;
1177 /* Allocate the storage. */
1178 if (cp_parser_context_free_list != NULL)
1180 /* Pull the first entry from the free list. */
1181 context = cp_parser_context_free_list;
1182 cp_parser_context_free_list = context->next;
1183 memset (context, 0, sizeof (*context));
1186 context = GGC_CNEW (cp_parser_context);
1188 /* No errors have occurred yet in this context. */
1189 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1190 /* If this is not the bottomost context, copy information that we
1191 need from the previous context. */
1194 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1195 expression, then we are parsing one in this context, too. */
1196 context->object_type = next->object_type;
1197 /* Thread the stack. */
1198 context->next = next;
1204 /* The cp_parser structure represents the C++ parser. */
1206 typedef struct cp_parser GTY(())
1208 /* The lexer from which we are obtaining tokens. */
1211 /* The scope in which names should be looked up. If NULL_TREE, then
1212 we look up names in the scope that is currently open in the
1213 source program. If non-NULL, this is either a TYPE or
1214 NAMESPACE_DECL for the scope in which we should look.
1216 This value is not cleared automatically after a name is looked
1217 up, so we must be careful to clear it before starting a new look
1218 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1219 will look up `Z' in the scope of `X', rather than the current
1220 scope.) Unfortunately, it is difficult to tell when name lookup
1221 is complete, because we sometimes peek at a token, look it up,
1222 and then decide not to consume it. */
1225 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1226 last lookup took place. OBJECT_SCOPE is used if an expression
1227 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1228 respectively. QUALIFYING_SCOPE is used for an expression of the
1229 form "X::Y"; it refers to X. */
1231 tree qualifying_scope;
1233 /* A stack of parsing contexts. All but the bottom entry on the
1234 stack will be tentative contexts.
1236 We parse tentatively in order to determine which construct is in
1237 use in some situations. For example, in order to determine
1238 whether a statement is an expression-statement or a
1239 declaration-statement we parse it tentatively as a
1240 declaration-statement. If that fails, we then reparse the same
1241 token stream as an expression-statement. */
1242 cp_parser_context *context;
1244 /* True if we are parsing GNU C++. If this flag is not set, then
1245 GNU extensions are not recognized. */
1246 bool allow_gnu_extensions_p;
1248 /* TRUE if the `>' token should be interpreted as the greater-than
1249 operator. FALSE if it is the end of a template-id or
1250 template-parameter-list. */
1251 bool greater_than_is_operator_p;
1253 /* TRUE if default arguments are allowed within a parameter list
1254 that starts at this point. FALSE if only a gnu extension makes
1255 them permissible. */
1256 bool default_arg_ok_p;
1258 /* TRUE if we are parsing an integral constant-expression. See
1259 [expr.const] for a precise definition. */
1260 bool integral_constant_expression_p;
1262 /* TRUE if we are parsing an integral constant-expression -- but a
1263 non-constant expression should be permitted as well. This flag
1264 is used when parsing an array bound so that GNU variable-length
1265 arrays are tolerated. */
1266 bool allow_non_integral_constant_expression_p;
1268 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1269 been seen that makes the expression non-constant. */
1270 bool non_integral_constant_expression_p;
1272 /* TRUE if local variable names and `this' are forbidden in the
1274 bool local_variables_forbidden_p;
1276 /* TRUE if the declaration we are parsing is part of a
1277 linkage-specification of the form `extern string-literal
1279 bool in_unbraced_linkage_specification_p;
1281 /* TRUE if we are presently parsing a declarator, after the
1282 direct-declarator. */
1283 bool in_declarator_p;
1285 /* TRUE if we are presently parsing a template-argument-list. */
1286 bool in_template_argument_list_p;
1288 /* TRUE if we are presently parsing the body of an
1289 iteration-statement. */
1290 bool in_iteration_statement_p;
1292 /* TRUE if we are presently parsing the body of a switch
1294 bool in_switch_statement_p;
1296 /* TRUE if we are parsing a type-id in an expression context. In
1297 such a situation, both "type (expr)" and "type (type)" are valid
1299 bool in_type_id_in_expr_p;
1301 /* TRUE if we are currently in a header file where declarations are
1302 implicitly extern "C". */
1303 bool implicit_extern_c;
1305 /* TRUE if strings in expressions should be translated to the execution
1307 bool translate_strings_p;
1309 /* If non-NULL, then we are parsing a construct where new type
1310 definitions are not permitted. The string stored here will be
1311 issued as an error message if a type is defined. */
1312 const char *type_definition_forbidden_message;
1314 /* A list of lists. The outer list is a stack, used for member
1315 functions of local classes. At each level there are two sub-list,
1316 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1317 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1318 TREE_VALUE's. The functions are chained in reverse declaration
1321 The TREE_PURPOSE sublist contains those functions with default
1322 arguments that need post processing, and the TREE_VALUE sublist
1323 contains those functions with definitions that need post
1326 These lists can only be processed once the outermost class being
1327 defined is complete. */
1328 tree unparsed_functions_queues;
1330 /* The number of classes whose definitions are currently in
1332 unsigned num_classes_being_defined;
1334 /* The number of template parameter lists that apply directly to the
1335 current declaration. */
1336 unsigned num_template_parameter_lists;
1339 /* The type of a function that parses some kind of expression. */
1340 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1344 /* Constructors and destructors. */
1346 static cp_parser *cp_parser_new
1349 /* Routines to parse various constructs.
1351 Those that return `tree' will return the error_mark_node (rather
1352 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1353 Sometimes, they will return an ordinary node if error-recovery was
1354 attempted, even though a parse error occurred. So, to check
1355 whether or not a parse error occurred, you should always use
1356 cp_parser_error_occurred. If the construct is optional (indicated
1357 either by an `_opt' in the name of the function that does the
1358 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1359 the construct is not present. */
1361 /* Lexical conventions [gram.lex] */
1363 static tree cp_parser_identifier
1365 static tree cp_parser_string_literal
1366 (cp_parser *, bool, bool);
1368 /* Basic concepts [gram.basic] */
1370 static bool cp_parser_translation_unit
1373 /* Expressions [gram.expr] */
1375 static tree cp_parser_primary_expression
1376 (cp_parser *, cp_id_kind *, tree *);
1377 static tree cp_parser_id_expression
1378 (cp_parser *, bool, bool, bool *, bool);
1379 static tree cp_parser_unqualified_id
1380 (cp_parser *, bool, bool, bool);
1381 static tree cp_parser_nested_name_specifier_opt
1382 (cp_parser *, bool, bool, bool, bool);
1383 static tree cp_parser_nested_name_specifier
1384 (cp_parser *, bool, bool, bool, bool);
1385 static tree cp_parser_class_or_namespace_name
1386 (cp_parser *, bool, bool, bool, bool, bool);
1387 static tree cp_parser_postfix_expression
1388 (cp_parser *, bool);
1389 static tree cp_parser_postfix_open_square_expression
1390 (cp_parser *, tree, bool);
1391 static tree cp_parser_postfix_dot_deref_expression
1392 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1393 static tree cp_parser_parenthesized_expression_list
1394 (cp_parser *, bool, bool *);
1395 static void cp_parser_pseudo_destructor_name
1396 (cp_parser *, tree *, tree *);
1397 static tree cp_parser_unary_expression
1398 (cp_parser *, bool);
1399 static enum tree_code cp_parser_unary_operator
1401 static tree cp_parser_new_expression
1403 static tree cp_parser_new_placement
1405 static tree cp_parser_new_type_id
1406 (cp_parser *, tree *);
1407 static cp_declarator *cp_parser_new_declarator_opt
1409 static cp_declarator *cp_parser_direct_new_declarator
1411 static tree cp_parser_new_initializer
1413 static tree cp_parser_delete_expression
1415 static tree cp_parser_cast_expression
1416 (cp_parser *, bool);
1417 static tree cp_parser_binary_expression
1419 static tree cp_parser_question_colon_clause
1420 (cp_parser *, tree);
1421 static tree cp_parser_assignment_expression
1423 static enum tree_code cp_parser_assignment_operator_opt
1425 static tree cp_parser_expression
1427 static tree cp_parser_constant_expression
1428 (cp_parser *, bool, bool *);
1429 static tree cp_parser_builtin_offsetof
1432 /* Statements [gram.stmt.stmt] */
1434 static void cp_parser_statement
1435 (cp_parser *, tree);
1436 static tree cp_parser_labeled_statement
1437 (cp_parser *, tree);
1438 static tree cp_parser_expression_statement
1439 (cp_parser *, tree);
1440 static tree cp_parser_compound_statement
1441 (cp_parser *, tree, bool);
1442 static void cp_parser_statement_seq_opt
1443 (cp_parser *, tree);
1444 static tree cp_parser_selection_statement
1446 static tree cp_parser_condition
1448 static tree cp_parser_iteration_statement
1450 static void cp_parser_for_init_statement
1452 static tree cp_parser_jump_statement
1454 static void cp_parser_declaration_statement
1457 static tree cp_parser_implicitly_scoped_statement
1459 static void cp_parser_already_scoped_statement
1462 /* Declarations [gram.dcl.dcl] */
1464 static void cp_parser_declaration_seq_opt
1466 static void cp_parser_declaration
1468 static void cp_parser_block_declaration
1469 (cp_parser *, bool);
1470 static void cp_parser_simple_declaration
1471 (cp_parser *, bool);
1472 static void cp_parser_decl_specifier_seq
1473 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1474 static tree cp_parser_storage_class_specifier_opt
1476 static tree cp_parser_function_specifier_opt
1477 (cp_parser *, cp_decl_specifier_seq *);
1478 static tree cp_parser_type_specifier
1479 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1481 static tree cp_parser_simple_type_specifier
1482 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1483 static tree cp_parser_type_name
1485 static tree cp_parser_elaborated_type_specifier
1486 (cp_parser *, bool, bool);
1487 static tree cp_parser_enum_specifier
1489 static void cp_parser_enumerator_list
1490 (cp_parser *, tree);
1491 static void cp_parser_enumerator_definition
1492 (cp_parser *, tree);
1493 static tree cp_parser_namespace_name
1495 static void cp_parser_namespace_definition
1497 static void cp_parser_namespace_body
1499 static tree cp_parser_qualified_namespace_specifier
1501 static void cp_parser_namespace_alias_definition
1503 static void cp_parser_using_declaration
1505 static void cp_parser_using_directive
1507 static void cp_parser_asm_definition
1509 static void cp_parser_linkage_specification
1512 /* Declarators [gram.dcl.decl] */
1514 static tree cp_parser_init_declarator
1515 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1516 static cp_declarator *cp_parser_declarator
1517 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1518 static cp_declarator *cp_parser_direct_declarator
1519 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1520 static enum tree_code cp_parser_ptr_operator
1521 (cp_parser *, tree *, cp_cv_quals *);
1522 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1524 static tree cp_parser_declarator_id
1526 static tree cp_parser_type_id
1528 static void cp_parser_type_specifier_seq
1529 (cp_parser *, cp_decl_specifier_seq *);
1530 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1532 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1533 (cp_parser *, bool *);
1534 static cp_parameter_declarator *cp_parser_parameter_declaration
1535 (cp_parser *, bool, bool *);
1536 static void cp_parser_function_body
1538 static tree cp_parser_initializer
1539 (cp_parser *, bool *, bool *);
1540 static tree cp_parser_initializer_clause
1541 (cp_parser *, bool *);
1542 static tree cp_parser_initializer_list
1543 (cp_parser *, bool *);
1545 static bool cp_parser_ctor_initializer_opt_and_function_body
1548 /* Classes [gram.class] */
1550 static tree cp_parser_class_name
1551 (cp_parser *, bool, bool, bool, bool, bool, bool);
1552 static tree cp_parser_class_specifier
1554 static tree cp_parser_class_head
1555 (cp_parser *, bool *, tree *);
1556 static enum tag_types cp_parser_class_key
1558 static void cp_parser_member_specification_opt
1560 static void cp_parser_member_declaration
1562 static tree cp_parser_pure_specifier
1564 static tree cp_parser_constant_initializer
1567 /* Derived classes [gram.class.derived] */
1569 static tree cp_parser_base_clause
1571 static tree cp_parser_base_specifier
1574 /* Special member functions [gram.special] */
1576 static tree cp_parser_conversion_function_id
1578 static tree cp_parser_conversion_type_id
1580 static cp_declarator *cp_parser_conversion_declarator_opt
1582 static bool cp_parser_ctor_initializer_opt
1584 static void cp_parser_mem_initializer_list
1586 static tree cp_parser_mem_initializer
1588 static tree cp_parser_mem_initializer_id
1591 /* Overloading [gram.over] */
1593 static tree cp_parser_operator_function_id
1595 static tree cp_parser_operator
1598 /* Templates [gram.temp] */
1600 static void cp_parser_template_declaration
1601 (cp_parser *, bool);
1602 static tree cp_parser_template_parameter_list
1604 static tree cp_parser_template_parameter
1605 (cp_parser *, bool *);
1606 static tree cp_parser_type_parameter
1608 static tree cp_parser_template_id
1609 (cp_parser *, bool, bool, bool);
1610 static tree cp_parser_template_name
1611 (cp_parser *, bool, bool, bool, bool *);
1612 static tree cp_parser_template_argument_list
1614 static tree cp_parser_template_argument
1616 static void cp_parser_explicit_instantiation
1618 static void cp_parser_explicit_specialization
1621 /* Exception handling [gram.exception] */
1623 static tree cp_parser_try_block
1625 static bool cp_parser_function_try_block
1627 static void cp_parser_handler_seq
1629 static void cp_parser_handler
1631 static tree cp_parser_exception_declaration
1633 static tree cp_parser_throw_expression
1635 static tree cp_parser_exception_specification_opt
1637 static tree cp_parser_type_id_list
1640 /* GNU Extensions */
1642 static tree cp_parser_asm_specification_opt
1644 static tree cp_parser_asm_operand_list
1646 static tree cp_parser_asm_clobber_list
1648 static tree cp_parser_attributes_opt
1650 static tree cp_parser_attribute_list
1652 static bool cp_parser_extension_opt
1653 (cp_parser *, int *);
1654 static void cp_parser_label_declaration
1657 /* Utility Routines */
1659 static tree cp_parser_lookup_name
1660 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1661 static tree cp_parser_lookup_name_simple
1662 (cp_parser *, tree);
1663 static tree cp_parser_maybe_treat_template_as_class
1665 static bool cp_parser_check_declarator_template_parameters
1666 (cp_parser *, cp_declarator *);
1667 static bool cp_parser_check_template_parameters
1668 (cp_parser *, unsigned);
1669 static tree cp_parser_simple_cast_expression
1671 static tree cp_parser_global_scope_opt
1672 (cp_parser *, bool);
1673 static bool cp_parser_constructor_declarator_p
1674 (cp_parser *, bool);
1675 static tree cp_parser_function_definition_from_specifiers_and_declarator
1676 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1677 static tree cp_parser_function_definition_after_declarator
1678 (cp_parser *, bool);
1679 static void cp_parser_template_declaration_after_export
1680 (cp_parser *, bool);
1681 static tree cp_parser_single_declaration
1682 (cp_parser *, bool, bool *);
1683 static tree cp_parser_functional_cast
1684 (cp_parser *, tree);
1685 static tree cp_parser_save_member_function_body
1686 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1687 static tree cp_parser_enclosed_template_argument_list
1689 static void cp_parser_save_default_args
1690 (cp_parser *, tree);
1691 static void cp_parser_late_parsing_for_member
1692 (cp_parser *, tree);
1693 static void cp_parser_late_parsing_default_args
1694 (cp_parser *, tree);
1695 static tree cp_parser_sizeof_operand
1696 (cp_parser *, enum rid);
1697 static bool cp_parser_declares_only_class_p
1699 static void cp_parser_set_storage_class
1700 (cp_decl_specifier_seq *, cp_storage_class);
1701 static void cp_parser_set_decl_spec_type
1702 (cp_decl_specifier_seq *, tree, bool);
1703 static bool cp_parser_friend_p
1704 (const cp_decl_specifier_seq *);
1705 static cp_token *cp_parser_require
1706 (cp_parser *, enum cpp_ttype, const char *);
1707 static cp_token *cp_parser_require_keyword
1708 (cp_parser *, enum rid, const char *);
1709 static bool cp_parser_token_starts_function_definition_p
1711 static bool cp_parser_next_token_starts_class_definition_p
1713 static bool cp_parser_next_token_ends_template_argument_p
1715 static bool cp_parser_nth_token_starts_template_argument_list_p
1716 (cp_parser *, size_t);
1717 static enum tag_types cp_parser_token_is_class_key
1719 static void cp_parser_check_class_key
1720 (enum tag_types, tree type);
1721 static void cp_parser_check_access_in_redeclaration
1723 static bool cp_parser_optional_template_keyword
1725 static void cp_parser_pre_parsed_nested_name_specifier
1727 static void cp_parser_cache_group
1728 (cp_parser *, enum cpp_ttype, unsigned);
1729 static void cp_parser_parse_tentatively
1731 static void cp_parser_commit_to_tentative_parse
1733 static void cp_parser_abort_tentative_parse
1735 static bool cp_parser_parse_definitely
1737 static inline bool cp_parser_parsing_tentatively
1739 static bool cp_parser_committed_to_tentative_parse
1741 static void cp_parser_error
1742 (cp_parser *, const char *);
1743 static void cp_parser_name_lookup_error
1744 (cp_parser *, tree, tree, const char *);
1745 static bool cp_parser_simulate_error
1747 static void cp_parser_check_type_definition
1749 static void cp_parser_check_for_definition_in_return_type
1750 (cp_declarator *, int);
1751 static void cp_parser_check_for_invalid_template_id
1752 (cp_parser *, tree);
1753 static bool cp_parser_non_integral_constant_expression
1754 (cp_parser *, const char *);
1755 static void cp_parser_diagnose_invalid_type_name
1756 (cp_parser *, tree, tree);
1757 static bool cp_parser_parse_and_diagnose_invalid_type_name
1759 static int cp_parser_skip_to_closing_parenthesis
1760 (cp_parser *, bool, bool, bool);
1761 static void cp_parser_skip_to_end_of_statement
1763 static void cp_parser_consume_semicolon_at_end_of_statement
1765 static void cp_parser_skip_to_end_of_block_or_statement
1767 static void cp_parser_skip_to_closing_brace
1769 static void cp_parser_skip_until_found
1770 (cp_parser *, enum cpp_ttype, const char *);
1771 static bool cp_parser_error_occurred
1773 static bool cp_parser_allow_gnu_extensions_p
1775 static bool cp_parser_is_string_literal
1777 static bool cp_parser_is_keyword
1778 (cp_token *, enum rid);
1779 static tree cp_parser_make_typename_type
1780 (cp_parser *, tree, tree);
1782 /* Returns nonzero if we are parsing tentatively. */
1785 cp_parser_parsing_tentatively (cp_parser* parser)
1787 return parser->context->next != NULL;
1790 /* Returns nonzero if TOKEN is a string literal. */
1793 cp_parser_is_string_literal (cp_token* token)
1795 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1798 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1801 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1803 return token->keyword == keyword;
1806 /* If not parsing tentatively, issue a diagnostic of the form
1807 FILE:LINE: MESSAGE before TOKEN
1808 where TOKEN is the next token in the input stream. MESSAGE
1809 (specified by the caller) is usually of the form "expected
1813 cp_parser_error (cp_parser* parser, const char* message)
1815 if (!cp_parser_simulate_error (parser))
1817 cp_token *token = cp_lexer_peek_token (parser->lexer);
1818 /* This diagnostic makes more sense if it is tagged to the line
1819 of the token we just peeked at. */
1820 cp_lexer_set_source_position_from_token (token);
1821 c_parse_error (message,
1822 /* Because c_parser_error does not understand
1823 CPP_KEYWORD, keywords are treated like
1825 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1830 /* Issue an error about name-lookup failing. NAME is the
1831 IDENTIFIER_NODE DECL is the result of
1832 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1833 the thing that we hoped to find. */
1836 cp_parser_name_lookup_error (cp_parser* parser,
1839 const char* desired)
1841 /* If name lookup completely failed, tell the user that NAME was not
1843 if (decl == error_mark_node)
1845 if (parser->scope && parser->scope != global_namespace)
1846 error ("%<%D::%D%> has not been declared",
1847 parser->scope, name);
1848 else if (parser->scope == global_namespace)
1849 error ("%<::%D%> has not been declared", name);
1850 else if (parser->object_scope
1851 && !CLASS_TYPE_P (parser->object_scope))
1852 error ("request for member %qD in non-class type %qT",
1853 name, parser->object_scope);
1854 else if (parser->object_scope)
1855 error ("%<%T::%D%> has not been declared",
1856 parser->object_scope, name);
1858 error ("`%D' has not been declared", name);
1860 else if (parser->scope && parser->scope != global_namespace)
1861 error ("%<%D::%D%> %s", parser->scope, name, desired);
1862 else if (parser->scope == global_namespace)
1863 error ("%<::%D%> %s", name, desired);
1865 error ("%qD %s", name, desired);
1868 /* If we are parsing tentatively, remember that an error has occurred
1869 during this tentative parse. Returns true if the error was
1870 simulated; false if a message should be issued by the caller. */
1873 cp_parser_simulate_error (cp_parser* parser)
1875 if (cp_parser_parsing_tentatively (parser)
1876 && !cp_parser_committed_to_tentative_parse (parser))
1878 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1884 /* This function is called when a type is defined. If type
1885 definitions are forbidden at this point, an error message is
1889 cp_parser_check_type_definition (cp_parser* parser)
1891 /* If types are forbidden here, issue a message. */
1892 if (parser->type_definition_forbidden_message)
1893 /* Use `%s' to print the string in case there are any escape
1894 characters in the message. */
1895 error ("%s", parser->type_definition_forbidden_message);
1898 /* This function is called when a declaration is parsed. If
1899 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1900 indicates that a type was defined in the decl-specifiers for DECL,
1901 then an error is issued. */
1904 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1905 int declares_class_or_enum)
1907 /* [dcl.fct] forbids type definitions in return types.
1908 Unfortunately, it's not easy to know whether or not we are
1909 processing a return type until after the fact. */
1911 && (declarator->kind == cdk_pointer
1912 || declarator->kind == cdk_reference
1913 || declarator->kind == cdk_ptrmem))
1914 declarator = declarator->declarator;
1916 && declarator->kind == cdk_function
1917 && declares_class_or_enum & 2)
1918 error ("new types may not be defined in a return type");
1921 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1922 "<" in any valid C++ program. If the next token is indeed "<",
1923 issue a message warning the user about what appears to be an
1924 invalid attempt to form a template-id. */
1927 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1930 cp_token_position start = 0;
1932 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1935 error ("%qT is not a template", type);
1936 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1937 error ("%qE is not a template", type);
1939 error ("invalid template-id");
1940 /* Remember the location of the invalid "<". */
1941 if (cp_parser_parsing_tentatively (parser)
1942 && !cp_parser_committed_to_tentative_parse (parser))
1943 start = cp_lexer_token_position (parser->lexer, true);
1944 /* Consume the "<". */
1945 cp_lexer_consume_token (parser->lexer);
1946 /* Parse the template arguments. */
1947 cp_parser_enclosed_template_argument_list (parser);
1948 /* Permanently remove the invalid template arguments so that
1949 this error message is not issued again. */
1951 cp_lexer_purge_tokens_after (parser->lexer, start);
1955 /* If parsing an integral constant-expression, issue an error message
1956 about the fact that THING appeared and return true. Otherwise,
1957 return false, marking the current expression as non-constant. */
1960 cp_parser_non_integral_constant_expression (cp_parser *parser,
1963 if (parser->integral_constant_expression_p)
1965 if (!parser->allow_non_integral_constant_expression_p)
1967 error ("%s cannot appear in a constant-expression", thing);
1970 parser->non_integral_constant_expression_p = true;
1975 /* Emit a diagnostic for an invalid type name. Consider also if it is
1976 qualified or not and the result of a lookup, to provide a better
1980 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1982 tree decl, old_scope;
1983 /* Try to lookup the identifier. */
1984 old_scope = parser->scope;
1985 parser->scope = scope;
1986 decl = cp_parser_lookup_name_simple (parser, id);
1987 parser->scope = old_scope;
1988 /* If the lookup found a template-name, it means that the user forgot
1989 to specify an argument list. Emit an useful error message. */
1990 if (TREE_CODE (decl) == TEMPLATE_DECL)
1991 error ("invalid use of template-name %qE without an argument list",
1993 else if (!parser->scope)
1995 /* Issue an error message. */
1996 error ("%qE does not name a type", id);
1997 /* If we're in a template class, it's possible that the user was
1998 referring to a type from a base class. For example:
2000 template <typename T> struct A { typedef T X; };
2001 template <typename T> struct B : public A<T> { X x; };
2003 The user should have said "typename A<T>::X". */
2004 if (processing_template_decl && current_class_type)
2008 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2012 tree base_type = BINFO_TYPE (b);
2013 if (CLASS_TYPE_P (base_type)
2014 && dependent_type_p (base_type))
2017 /* Go from a particular instantiation of the
2018 template (which will have an empty TYPE_FIELDs),
2019 to the main version. */
2020 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2021 for (field = TYPE_FIELDS (base_type);
2023 field = TREE_CHAIN (field))
2024 if (TREE_CODE (field) == TYPE_DECL
2025 && DECL_NAME (field) == id)
2027 inform ("(perhaps `typename %T::%E' was intended)",
2028 BINFO_TYPE (b), id);
2037 /* Here we diagnose qualified-ids where the scope is actually correct,
2038 but the identifier does not resolve to a valid type name. */
2041 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2042 error ("%qE in namespace %qE does not name a type",
2044 else if (TYPE_P (parser->scope))
2045 error ("q%E in class %qT does not name a type", id, parser->scope);
2051 /* Check for a common situation where a type-name should be present,
2052 but is not, and issue a sensible error message. Returns true if an
2053 invalid type-name was detected.
2055 The situation handled by this function are variable declarations of the
2056 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2057 Usually, `ID' should name a type, but if we got here it means that it
2058 does not. We try to emit the best possible error message depending on
2059 how exactly the id-expression looks like.
2063 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2067 cp_parser_parse_tentatively (parser);
2068 id = cp_parser_id_expression (parser,
2069 /*template_keyword_p=*/false,
2070 /*check_dependency_p=*/true,
2071 /*template_p=*/NULL,
2072 /*declarator_p=*/true);
2073 /* After the id-expression, there should be a plain identifier,
2074 otherwise this is not a simple variable declaration. Also, if
2075 the scope is dependent, we cannot do much. */
2076 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2077 || (parser->scope && TYPE_P (parser->scope)
2078 && dependent_type_p (parser->scope)))
2080 cp_parser_abort_tentative_parse (parser);
2083 if (!cp_parser_parse_definitely (parser)
2084 || TREE_CODE (id) != IDENTIFIER_NODE)
2087 /* Emit a diagnostic for the invalid type. */
2088 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2089 /* Skip to the end of the declaration; there's no point in
2090 trying to process it. */
2091 cp_parser_skip_to_end_of_block_or_statement (parser);
2095 /* Consume tokens up to, and including, the next non-nested closing `)'.
2096 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2097 are doing error recovery. Returns -1 if OR_COMMA is true and we
2098 found an unnested comma. */
2101 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2106 unsigned paren_depth = 0;
2107 unsigned brace_depth = 0;
2110 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2111 && !cp_parser_committed_to_tentative_parse (parser))
2118 /* If we've run out of tokens, then there is no closing `)'. */
2119 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2125 token = cp_lexer_peek_token (parser->lexer);
2127 /* This matches the processing in skip_to_end_of_statement. */
2128 if (token->type == CPP_SEMICOLON && !brace_depth)
2133 if (token->type == CPP_OPEN_BRACE)
2135 if (token->type == CPP_CLOSE_BRACE)
2143 if (recovering && or_comma && token->type == CPP_COMMA
2144 && !brace_depth && !paren_depth)
2152 /* If it is an `(', we have entered another level of nesting. */
2153 if (token->type == CPP_OPEN_PAREN)
2155 /* If it is a `)', then we might be done. */
2156 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2159 cp_lexer_consume_token (parser->lexer);
2167 /* Consume the token. */
2168 cp_lexer_consume_token (parser->lexer);
2174 /* Consume tokens until we reach the end of the current statement.
2175 Normally, that will be just before consuming a `;'. However, if a
2176 non-nested `}' comes first, then we stop before consuming that. */
2179 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2181 unsigned nesting_depth = 0;
2187 /* Peek at the next token. */
2188 token = cp_lexer_peek_token (parser->lexer);
2189 /* If we've run out of tokens, stop. */
2190 if (token->type == CPP_EOF)
2192 /* If the next token is a `;', we have reached the end of the
2194 if (token->type == CPP_SEMICOLON && !nesting_depth)
2196 /* If the next token is a non-nested `}', then we have reached
2197 the end of the current block. */
2198 if (token->type == CPP_CLOSE_BRACE)
2200 /* If this is a non-nested `}', stop before consuming it.
2201 That way, when confronted with something like:
2205 we stop before consuming the closing `}', even though we
2206 have not yet reached a `;'. */
2207 if (nesting_depth == 0)
2209 /* If it is the closing `}' for a block that we have
2210 scanned, stop -- but only after consuming the token.
2216 we will stop after the body of the erroneously declared
2217 function, but before consuming the following `typedef'
2219 if (--nesting_depth == 0)
2221 cp_lexer_consume_token (parser->lexer);
2225 /* If it the next token is a `{', then we are entering a new
2226 block. Consume the entire block. */
2227 else if (token->type == CPP_OPEN_BRACE)
2229 /* Consume the token. */
2230 cp_lexer_consume_token (parser->lexer);
2234 /* This function is called at the end of a statement or declaration.
2235 If the next token is a semicolon, it is consumed; otherwise, error
2236 recovery is attempted. */
2239 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2241 /* Look for the trailing `;'. */
2242 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2244 /* If there is additional (erroneous) input, skip to the end of
2246 cp_parser_skip_to_end_of_statement (parser);
2247 /* If the next token is now a `;', consume it. */
2248 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2249 cp_lexer_consume_token (parser->lexer);
2253 /* Skip tokens until we have consumed an entire block, or until we
2254 have consumed a non-nested `;'. */
2257 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2259 unsigned nesting_depth = 0;
2265 /* Peek at the next token. */
2266 token = cp_lexer_peek_token (parser->lexer);
2267 /* If we've run out of tokens, stop. */
2268 if (token->type == CPP_EOF)
2270 /* If the next token is a `;', we have reached the end of the
2272 if (token->type == CPP_SEMICOLON && !nesting_depth)
2274 /* Consume the `;'. */
2275 cp_lexer_consume_token (parser->lexer);
2278 /* Consume the token. */
2279 token = cp_lexer_consume_token (parser->lexer);
2280 /* If the next token is a non-nested `}', then we have reached
2281 the end of the current block. */
2282 if (token->type == CPP_CLOSE_BRACE
2283 && (nesting_depth == 0 || --nesting_depth == 0))
2285 /* If it the next token is a `{', then we are entering a new
2286 block. Consume the entire block. */
2287 if (token->type == CPP_OPEN_BRACE)
2292 /* Skip tokens until a non-nested closing curly brace is the next
2296 cp_parser_skip_to_closing_brace (cp_parser *parser)
2298 unsigned nesting_depth = 0;
2304 /* Peek at the next token. */
2305 token = cp_lexer_peek_token (parser->lexer);
2306 /* If we've run out of tokens, stop. */
2307 if (token->type == CPP_EOF)
2309 /* If the next token is a non-nested `}', then we have reached
2310 the end of the current block. */
2311 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2313 /* If it the next token is a `{', then we are entering a new
2314 block. Consume the entire block. */
2315 else if (token->type == CPP_OPEN_BRACE)
2317 /* Consume the token. */
2318 cp_lexer_consume_token (parser->lexer);
2322 /* This is a simple wrapper around make_typename_type. When the id is
2323 an unresolved identifier node, we can provide a superior diagnostic
2324 using cp_parser_diagnose_invalid_type_name. */
2327 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2330 if (TREE_CODE (id) == IDENTIFIER_NODE)
2332 result = make_typename_type (scope, id, /*complain=*/0);
2333 if (result == error_mark_node)
2334 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2337 return make_typename_type (scope, id, tf_error);
2341 /* Create a new C++ parser. */
2344 cp_parser_new (void)
2350 /* cp_lexer_new_main is called before calling ggc_alloc because
2351 cp_lexer_new_main might load a PCH file. */
2352 lexer = cp_lexer_new_main ();
2354 /* Initialize the binops_by_token so that we can get the tree
2355 directly from the token. */
2356 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2357 binops_by_token[binops[i].token_type] = binops[i];
2359 parser = GGC_CNEW (cp_parser);
2360 parser->lexer = lexer;
2361 parser->context = cp_parser_context_new (NULL);
2363 /* For now, we always accept GNU extensions. */
2364 parser->allow_gnu_extensions_p = 1;
2366 /* The `>' token is a greater-than operator, not the end of a
2368 parser->greater_than_is_operator_p = true;
2370 parser->default_arg_ok_p = true;
2372 /* We are not parsing a constant-expression. */
2373 parser->integral_constant_expression_p = false;
2374 parser->allow_non_integral_constant_expression_p = false;
2375 parser->non_integral_constant_expression_p = false;
2377 /* Local variable names are not forbidden. */
2378 parser->local_variables_forbidden_p = false;
2380 /* We are not processing an `extern "C"' declaration. */
2381 parser->in_unbraced_linkage_specification_p = false;
2383 /* We are not processing a declarator. */
2384 parser->in_declarator_p = false;
2386 /* We are not processing a template-argument-list. */
2387 parser->in_template_argument_list_p = false;
2389 /* We are not in an iteration statement. */
2390 parser->in_iteration_statement_p = false;
2392 /* We are not in a switch statement. */
2393 parser->in_switch_statement_p = false;
2395 /* We are not parsing a type-id inside an expression. */
2396 parser->in_type_id_in_expr_p = false;
2398 /* Declarations aren't implicitly extern "C". */
2399 parser->implicit_extern_c = false;
2401 /* String literals should be translated to the execution character set. */
2402 parser->translate_strings_p = true;
2404 /* The unparsed function queue is empty. */
2405 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2407 /* There are no classes being defined. */
2408 parser->num_classes_being_defined = 0;
2410 /* No template parameters apply. */
2411 parser->num_template_parameter_lists = 0;
2416 /* Create a cp_lexer structure which will emit the tokens in CACHE
2417 and push it onto the parser's lexer stack. This is used for delayed
2418 parsing of in-class method bodies and default arguments, and should
2419 not be confused with tentative parsing. */
2421 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2423 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2424 lexer->next = parser->lexer;
2425 parser->lexer = lexer;
2427 /* Move the current source position to that of the first token in the
2429 cp_lexer_set_source_position_from_token (lexer->next_token);
2432 /* Pop the top lexer off the parser stack. This is never used for the
2433 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2435 cp_parser_pop_lexer (cp_parser *parser)
2437 cp_lexer *lexer = parser->lexer;
2438 parser->lexer = lexer->next;
2439 cp_lexer_destroy (lexer);
2441 /* Put the current source position back where it was before this
2442 lexer was pushed. */
2443 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2446 /* Lexical conventions [gram.lex] */
2448 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2452 cp_parser_identifier (cp_parser* parser)
2456 /* Look for the identifier. */
2457 token = cp_parser_require (parser, CPP_NAME, "identifier");
2458 /* Return the value. */
2459 return token ? token->value : error_mark_node;
2462 /* Parse a sequence of adjacent string constants. Returns a
2463 TREE_STRING representing the combined, nul-terminated string
2464 constant. If TRANSLATE is true, translate the string to the
2465 execution character set. If WIDE_OK is true, a wide string is
2468 C++98 [lex.string] says that if a narrow string literal token is
2469 adjacent to a wide string literal token, the behavior is undefined.
2470 However, C99 6.4.5p4 says that this results in a wide string literal.
2471 We follow C99 here, for consistency with the C front end.
2473 This code is largely lifted from lex_string() in c-lex.c.
2475 FUTURE: ObjC++ will need to handle @-strings here. */
2477 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2482 struct obstack str_ob;
2483 cpp_string str, istr, *strs;
2486 tok = cp_lexer_peek_token (parser->lexer);
2487 if (!cp_parser_is_string_literal (tok))
2489 cp_parser_error (parser, "expected string-literal");
2490 return error_mark_node;
2493 /* Try to avoid the overhead of creating and destroying an obstack
2494 for the common case of just one string. */
2495 if (!cp_parser_is_string_literal
2496 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2498 cp_lexer_consume_token (parser->lexer);
2500 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2501 str.len = TREE_STRING_LENGTH (tok->value);
2503 if (tok->type == CPP_WSTRING)
2510 gcc_obstack_init (&str_ob);
2515 cp_lexer_consume_token (parser->lexer);
2517 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2518 str.len = TREE_STRING_LENGTH (tok->value);
2519 if (tok->type == CPP_WSTRING)
2522 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2524 tok = cp_lexer_peek_token (parser->lexer);
2526 while (cp_parser_is_string_literal (tok));
2528 strs = (cpp_string *) obstack_finish (&str_ob);
2531 if (wide && !wide_ok)
2533 cp_parser_error (parser, "a wide string is invalid in this context");
2537 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2538 (parse_in, strs, count, &istr, wide))
2540 value = build_string (istr.len, (char *)istr.text);
2541 free ((void *)istr.text);
2543 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2544 value = fix_string_type (value);
2547 /* cpp_interpret_string has issued an error. */
2548 value = error_mark_node;
2551 obstack_free (&str_ob, 0);
2557 /* Basic concepts [gram.basic] */
2559 /* Parse a translation-unit.
2562 declaration-seq [opt]
2564 Returns TRUE if all went well. */
2567 cp_parser_translation_unit (cp_parser* parser)
2569 /* The address of the first non-permanent object on the declarator
2571 static void *declarator_obstack_base;
2575 /* Create the declarator obstack, if necessary. */
2576 if (!cp_error_declarator)
2578 gcc_obstack_init (&declarator_obstack);
2579 /* Create the error declarator. */
2580 cp_error_declarator = make_declarator (cdk_error);
2581 /* Create the empty parameter list. */
2582 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2583 /* Remember where the base of the declarator obstack lies. */
2584 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2589 cp_parser_declaration_seq_opt (parser);
2591 /* If there are no tokens left then all went well. */
2592 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2594 /* Get rid of the token array; we don't need it any more. */
2595 cp_lexer_destroy (parser->lexer);
2596 parser->lexer = NULL;
2598 /* This file might have been a context that's implicitly extern
2599 "C". If so, pop the lang context. (Only relevant for PCH.) */
2600 if (parser->implicit_extern_c)
2602 pop_lang_context ();
2603 parser->implicit_extern_c = false;
2607 finish_translation_unit ();
2614 cp_parser_error (parser, "expected declaration");
2620 /* Make sure the declarator obstack was fully cleaned up. */
2621 gcc_assert (obstack_next_free (&declarator_obstack)
2622 == declarator_obstack_base);
2624 /* All went well. */
2628 /* Expressions [gram.expr] */
2630 /* Parse a primary-expression.
2641 ( compound-statement )
2642 __builtin_va_arg ( assignment-expression , type-id )
2647 Returns a representation of the expression.
2649 *IDK indicates what kind of id-expression (if any) was present.
2651 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2652 used as the operand of a pointer-to-member. In that case,
2653 *QUALIFYING_CLASS gives the class that is used as the qualifying
2654 class in the pointer-to-member. */
2657 cp_parser_primary_expression (cp_parser *parser,
2659 tree *qualifying_class)
2663 /* Assume the primary expression is not an id-expression. */
2664 *idk = CP_ID_KIND_NONE;
2665 /* And that it cannot be used as pointer-to-member. */
2666 *qualifying_class = NULL_TREE;
2668 /* Peek at the next token. */
2669 token = cp_lexer_peek_token (parser->lexer);
2670 switch (token->type)
2681 token = cp_lexer_consume_token (parser->lexer);
2682 return token->value;
2686 /* ??? Should wide strings be allowed when parser->translate_strings_p
2687 is false (i.e. in attributes)? If not, we can kill the third
2688 argument to cp_parser_string_literal. */
2689 return cp_parser_string_literal (parser,
2690 parser->translate_strings_p,
2693 case CPP_OPEN_PAREN:
2696 bool saved_greater_than_is_operator_p;
2698 /* Consume the `('. */
2699 cp_lexer_consume_token (parser->lexer);
2700 /* Within a parenthesized expression, a `>' token is always
2701 the greater-than operator. */
2702 saved_greater_than_is_operator_p
2703 = parser->greater_than_is_operator_p;
2704 parser->greater_than_is_operator_p = true;
2705 /* If we see `( { ' then we are looking at the beginning of
2706 a GNU statement-expression. */
2707 if (cp_parser_allow_gnu_extensions_p (parser)
2708 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2710 /* Statement-expressions are not allowed by the standard. */
2712 pedwarn ("ISO C++ forbids braced-groups within expressions");
2714 /* And they're not allowed outside of a function-body; you
2715 cannot, for example, write:
2717 int i = ({ int j = 3; j + 1; });
2719 at class or namespace scope. */
2720 if (!at_function_scope_p ())
2721 error ("statement-expressions are allowed only inside functions");
2722 /* Start the statement-expression. */
2723 expr = begin_stmt_expr ();
2724 /* Parse the compound-statement. */
2725 cp_parser_compound_statement (parser, expr, false);
2727 expr = finish_stmt_expr (expr, false);
2731 /* Parse the parenthesized expression. */
2732 expr = cp_parser_expression (parser);
2733 /* Let the front end know that this expression was
2734 enclosed in parentheses. This matters in case, for
2735 example, the expression is of the form `A::B', since
2736 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2738 finish_parenthesized_expr (expr);
2740 /* The `>' token might be the end of a template-id or
2741 template-parameter-list now. */
2742 parser->greater_than_is_operator_p
2743 = saved_greater_than_is_operator_p;
2744 /* Consume the `)'. */
2745 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2746 cp_parser_skip_to_end_of_statement (parser);
2752 switch (token->keyword)
2754 /* These two are the boolean literals. */
2756 cp_lexer_consume_token (parser->lexer);
2757 return boolean_true_node;
2759 cp_lexer_consume_token (parser->lexer);
2760 return boolean_false_node;
2762 /* The `__null' literal. */
2764 cp_lexer_consume_token (parser->lexer);
2767 /* Recognize the `this' keyword. */
2769 cp_lexer_consume_token (parser->lexer);
2770 if (parser->local_variables_forbidden_p)
2772 error ("%<this%> may not be used in this context");
2773 return error_mark_node;
2775 /* Pointers cannot appear in constant-expressions. */
2776 if (cp_parser_non_integral_constant_expression (parser,
2778 return error_mark_node;
2779 return finish_this_expr ();
2781 /* The `operator' keyword can be the beginning of an
2786 case RID_FUNCTION_NAME:
2787 case RID_PRETTY_FUNCTION_NAME:
2788 case RID_C99_FUNCTION_NAME:
2789 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2790 __func__ are the names of variables -- but they are
2791 treated specially. Therefore, they are handled here,
2792 rather than relying on the generic id-expression logic
2793 below. Grammatically, these names are id-expressions.
2795 Consume the token. */
2796 token = cp_lexer_consume_token (parser->lexer);
2797 /* Look up the name. */
2798 return finish_fname (token->value);
2805 /* The `__builtin_va_arg' construct is used to handle
2806 `va_arg'. Consume the `__builtin_va_arg' token. */
2807 cp_lexer_consume_token (parser->lexer);
2808 /* Look for the opening `('. */
2809 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2810 /* Now, parse the assignment-expression. */
2811 expression = cp_parser_assignment_expression (parser);
2812 /* Look for the `,'. */
2813 cp_parser_require (parser, CPP_COMMA, "`,'");
2814 /* Parse the type-id. */
2815 type = cp_parser_type_id (parser);
2816 /* Look for the closing `)'. */
2817 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2818 /* Using `va_arg' in a constant-expression is not
2820 if (cp_parser_non_integral_constant_expression (parser,
2822 return error_mark_node;
2823 return build_x_va_arg (expression, type);
2827 return cp_parser_builtin_offsetof (parser);
2830 cp_parser_error (parser, "expected primary-expression");
2831 return error_mark_node;
2834 /* An id-expression can start with either an identifier, a
2835 `::' as the beginning of a qualified-id, or the "operator"
2839 case CPP_TEMPLATE_ID:
2840 case CPP_NESTED_NAME_SPECIFIER:
2844 const char *error_msg;
2847 /* Parse the id-expression. */
2849 = cp_parser_id_expression (parser,
2850 /*template_keyword_p=*/false,
2851 /*check_dependency_p=*/true,
2852 /*template_p=*/NULL,
2853 /*declarator_p=*/false);
2854 if (id_expression == error_mark_node)
2855 return error_mark_node;
2856 /* If we have a template-id, then no further lookup is
2857 required. If the template-id was for a template-class, we
2858 will sometimes have a TYPE_DECL at this point. */
2859 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2860 || TREE_CODE (id_expression) == TYPE_DECL)
2861 decl = id_expression;
2862 /* Look up the name. */
2867 decl = cp_parser_lookup_name (parser, id_expression,
2869 /*is_template=*/false,
2870 /*is_namespace=*/false,
2871 /*check_dependency=*/true,
2873 /* If the lookup was ambiguous, an error will already have
2876 return error_mark_node;
2877 /* If name lookup gives us a SCOPE_REF, then the
2878 qualifying scope was dependent. Just propagate the
2880 if (TREE_CODE (decl) == SCOPE_REF)
2882 if (TYPE_P (TREE_OPERAND (decl, 0)))
2883 *qualifying_class = TREE_OPERAND (decl, 0);
2886 /* Check to see if DECL is a local variable in a context
2887 where that is forbidden. */
2888 if (parser->local_variables_forbidden_p
2889 && local_variable_p (decl))
2891 /* It might be that we only found DECL because we are
2892 trying to be generous with pre-ISO scoping rules.
2893 For example, consider:
2897 for (int i = 0; i < 10; ++i) {}
2898 extern void f(int j = i);
2901 Here, name look up will originally find the out
2902 of scope `i'. We need to issue a warning message,
2903 but then use the global `i'. */
2904 decl = check_for_out_of_scope_variable (decl);
2905 if (local_variable_p (decl))
2907 error ("local variable %qD may not appear in this context",
2909 return error_mark_node;
2914 decl = finish_id_expression (id_expression, decl, parser->scope,
2915 idk, qualifying_class,
2916 parser->integral_constant_expression_p,
2917 parser->allow_non_integral_constant_expression_p,
2918 &parser->non_integral_constant_expression_p,
2921 cp_parser_error (parser, error_msg);
2925 /* Anything else is an error. */
2927 cp_parser_error (parser, "expected primary-expression");
2928 return error_mark_node;
2932 /* Parse an id-expression.
2939 :: [opt] nested-name-specifier template [opt] unqualified-id
2941 :: operator-function-id
2944 Return a representation of the unqualified portion of the
2945 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2946 a `::' or nested-name-specifier.
2948 Often, if the id-expression was a qualified-id, the caller will
2949 want to make a SCOPE_REF to represent the qualified-id. This
2950 function does not do this in order to avoid wastefully creating
2951 SCOPE_REFs when they are not required.
2953 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2956 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2957 uninstantiated templates.
2959 If *TEMPLATE_P is non-NULL, it is set to true iff the
2960 `template' keyword is used to explicitly indicate that the entity
2961 named is a template.
2963 If DECLARATOR_P is true, the id-expression is appearing as part of
2964 a declarator, rather than as part of an expression. */
2967 cp_parser_id_expression (cp_parser *parser,
2968 bool template_keyword_p,
2969 bool check_dependency_p,
2973 bool global_scope_p;
2974 bool nested_name_specifier_p;
2976 /* Assume the `template' keyword was not used. */
2978 *template_p = false;
2980 /* Look for the optional `::' operator. */
2982 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2984 /* Look for the optional nested-name-specifier. */
2985 nested_name_specifier_p
2986 = (cp_parser_nested_name_specifier_opt (parser,
2987 /*typename_keyword_p=*/false,
2992 /* If there is a nested-name-specifier, then we are looking at
2993 the first qualified-id production. */
2994 if (nested_name_specifier_p)
2997 tree saved_object_scope;
2998 tree saved_qualifying_scope;
2999 tree unqualified_id;
3002 /* See if the next token is the `template' keyword. */
3004 template_p = &is_template;
3005 *template_p = cp_parser_optional_template_keyword (parser);
3006 /* Name lookup we do during the processing of the
3007 unqualified-id might obliterate SCOPE. */
3008 saved_scope = parser->scope;
3009 saved_object_scope = parser->object_scope;
3010 saved_qualifying_scope = parser->qualifying_scope;
3011 /* Process the final unqualified-id. */
3012 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3015 /* Restore the SAVED_SCOPE for our caller. */
3016 parser->scope = saved_scope;
3017 parser->object_scope = saved_object_scope;
3018 parser->qualifying_scope = saved_qualifying_scope;
3020 return unqualified_id;
3022 /* Otherwise, if we are in global scope, then we are looking at one
3023 of the other qualified-id productions. */
3024 else if (global_scope_p)
3029 /* Peek at the next token. */
3030 token = cp_lexer_peek_token (parser->lexer);
3032 /* If it's an identifier, and the next token is not a "<", then
3033 we can avoid the template-id case. This is an optimization
3034 for this common case. */
3035 if (token->type == CPP_NAME
3036 && !cp_parser_nth_token_starts_template_argument_list_p
3038 return cp_parser_identifier (parser);
3040 cp_parser_parse_tentatively (parser);
3041 /* Try a template-id. */
3042 id = cp_parser_template_id (parser,
3043 /*template_keyword_p=*/false,
3044 /*check_dependency_p=*/true,
3046 /* If that worked, we're done. */
3047 if (cp_parser_parse_definitely (parser))
3050 /* Peek at the next token. (Changes in the token buffer may
3051 have invalidated the pointer obtained above.) */
3052 token = cp_lexer_peek_token (parser->lexer);
3054 switch (token->type)
3057 return cp_parser_identifier (parser);
3060 if (token->keyword == RID_OPERATOR)
3061 return cp_parser_operator_function_id (parser);
3065 cp_parser_error (parser, "expected id-expression");
3066 return error_mark_node;
3070 return cp_parser_unqualified_id (parser, template_keyword_p,
3071 /*check_dependency_p=*/true,
3075 /* Parse an unqualified-id.
3079 operator-function-id
3080 conversion-function-id
3084 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3085 keyword, in a construct like `A::template ...'.
3087 Returns a representation of unqualified-id. For the `identifier'
3088 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3089 production a BIT_NOT_EXPR is returned; the operand of the
3090 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3091 other productions, see the documentation accompanying the
3092 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3093 names are looked up in uninstantiated templates. If DECLARATOR_P
3094 is true, the unqualified-id is appearing as part of a declarator,
3095 rather than as part of an expression. */
3098 cp_parser_unqualified_id (cp_parser* parser,
3099 bool template_keyword_p,
3100 bool check_dependency_p,
3105 /* Peek at the next token. */
3106 token = cp_lexer_peek_token (parser->lexer);
3108 switch (token->type)
3114 /* We don't know yet whether or not this will be a
3116 cp_parser_parse_tentatively (parser);
3117 /* Try a template-id. */
3118 id = cp_parser_template_id (parser, template_keyword_p,
3121 /* If it worked, we're done. */
3122 if (cp_parser_parse_definitely (parser))
3124 /* Otherwise, it's an ordinary identifier. */
3125 return cp_parser_identifier (parser);
3128 case CPP_TEMPLATE_ID:
3129 return cp_parser_template_id (parser, template_keyword_p,
3136 tree qualifying_scope;
3140 /* Consume the `~' token. */
3141 cp_lexer_consume_token (parser->lexer);
3142 /* Parse the class-name. The standard, as written, seems to
3145 template <typename T> struct S { ~S (); };
3146 template <typename T> S<T>::~S() {}
3148 is invalid, since `~' must be followed by a class-name, but
3149 `S<T>' is dependent, and so not known to be a class.
3150 That's not right; we need to look in uninstantiated
3151 templates. A further complication arises from:
3153 template <typename T> void f(T t) {
3157 Here, it is not possible to look up `T' in the scope of `T'
3158 itself. We must look in both the current scope, and the
3159 scope of the containing complete expression.
3161 Yet another issue is:
3170 The standard does not seem to say that the `S' in `~S'
3171 should refer to the type `S' and not the data member
3174 /* DR 244 says that we look up the name after the "~" in the
3175 same scope as we looked up the qualifying name. That idea
3176 isn't fully worked out; it's more complicated than that. */
3177 scope = parser->scope;
3178 object_scope = parser->object_scope;
3179 qualifying_scope = parser->qualifying_scope;
3181 /* If the name is of the form "X::~X" it's OK. */
3182 if (scope && TYPE_P (scope)
3183 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3184 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3186 && (cp_lexer_peek_token (parser->lexer)->value
3187 == TYPE_IDENTIFIER (scope)))
3189 cp_lexer_consume_token (parser->lexer);
3190 return build_nt (BIT_NOT_EXPR, scope);
3193 /* If there was an explicit qualification (S::~T), first look
3194 in the scope given by the qualification (i.e., S). */
3197 cp_parser_parse_tentatively (parser);
3198 type_decl = cp_parser_class_name (parser,
3199 /*typename_keyword_p=*/false,
3200 /*template_keyword_p=*/false,
3202 /*check_dependency=*/false,
3203 /*class_head_p=*/false,
3205 if (cp_parser_parse_definitely (parser))
3206 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3208 /* In "N::S::~S", look in "N" as well. */
3209 if (scope && qualifying_scope)
3211 cp_parser_parse_tentatively (parser);
3212 parser->scope = qualifying_scope;
3213 parser->object_scope = NULL_TREE;
3214 parser->qualifying_scope = NULL_TREE;
3216 = cp_parser_class_name (parser,
3217 /*typename_keyword_p=*/false,
3218 /*template_keyword_p=*/false,
3220 /*check_dependency=*/false,
3221 /*class_head_p=*/false,
3223 if (cp_parser_parse_definitely (parser))
3224 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3226 /* In "p->S::~T", look in the scope given by "*p" as well. */
3227 else if (object_scope)
3229 cp_parser_parse_tentatively (parser);
3230 parser->scope = object_scope;
3231 parser->object_scope = NULL_TREE;
3232 parser->qualifying_scope = NULL_TREE;
3234 = cp_parser_class_name (parser,
3235 /*typename_keyword_p=*/false,
3236 /*template_keyword_p=*/false,
3238 /*check_dependency=*/false,
3239 /*class_head_p=*/false,
3241 if (cp_parser_parse_definitely (parser))
3242 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3244 /* Look in the surrounding context. */
3245 parser->scope = NULL_TREE;
3246 parser->object_scope = NULL_TREE;
3247 parser->qualifying_scope = NULL_TREE;
3249 = cp_parser_class_name (parser,
3250 /*typename_keyword_p=*/false,
3251 /*template_keyword_p=*/false,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false,
3256 /* If an error occurred, assume that the name of the
3257 destructor is the same as the name of the qualifying
3258 class. That allows us to keep parsing after running
3259 into ill-formed destructor names. */
3260 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3261 return build_nt (BIT_NOT_EXPR, scope);
3262 else if (type_decl == error_mark_node)
3263 return error_mark_node;
3267 A typedef-name that names a class shall not be used as the
3268 identifier in the declarator for a destructor declaration. */
3270 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3271 && !DECL_SELF_REFERENCE_P (type_decl))
3272 error ("typedef-name %qD used as destructor declarator",
3275 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3279 if (token->keyword == RID_OPERATOR)
3283 /* This could be a template-id, so we try that first. */
3284 cp_parser_parse_tentatively (parser);
3285 /* Try a template-id. */
3286 id = cp_parser_template_id (parser, template_keyword_p,
3287 /*check_dependency_p=*/true,
3289 /* If that worked, we're done. */
3290 if (cp_parser_parse_definitely (parser))
3292 /* We still don't know whether we're looking at an
3293 operator-function-id or a conversion-function-id. */
3294 cp_parser_parse_tentatively (parser);
3295 /* Try an operator-function-id. */
3296 id = cp_parser_operator_function_id (parser);
3297 /* If that didn't work, try a conversion-function-id. */
3298 if (!cp_parser_parse_definitely (parser))
3299 id = cp_parser_conversion_function_id (parser);
3306 cp_parser_error (parser, "expected unqualified-id");
3307 return error_mark_node;
3311 /* Parse an (optional) nested-name-specifier.
3313 nested-name-specifier:
3314 class-or-namespace-name :: nested-name-specifier [opt]
3315 class-or-namespace-name :: template nested-name-specifier [opt]
3317 PARSER->SCOPE should be set appropriately before this function is
3318 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3319 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3322 Sets PARSER->SCOPE to the class (TYPE) or namespace
3323 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3324 it unchanged if there is no nested-name-specifier. Returns the new
3325 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3327 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3328 part of a declaration and/or decl-specifier. */
3331 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3332 bool typename_keyword_p,
3333 bool check_dependency_p,
3335 bool is_declaration)
3337 bool success = false;
3338 tree access_check = NULL_TREE;
3339 cp_token_position start = 0;
3342 /* If the next token corresponds to a nested name specifier, there
3343 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3344 false, it may have been true before, in which case something
3345 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3346 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3347 CHECK_DEPENDENCY_P is false, we have to fall through into the
3349 if (check_dependency_p
3350 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3352 cp_parser_pre_parsed_nested_name_specifier (parser);
3353 return parser->scope;
3356 /* Remember where the nested-name-specifier starts. */
3357 if (cp_parser_parsing_tentatively (parser)
3358 && !cp_parser_committed_to_tentative_parse (parser))
3359 start = cp_lexer_token_position (parser->lexer, false);
3361 push_deferring_access_checks (dk_deferred);
3367 tree saved_qualifying_scope;
3368 bool template_keyword_p;
3370 /* Spot cases that cannot be the beginning of a
3371 nested-name-specifier. */
3372 token = cp_lexer_peek_token (parser->lexer);
3374 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3375 the already parsed nested-name-specifier. */
3376 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3378 /* Grab the nested-name-specifier and continue the loop. */
3379 cp_parser_pre_parsed_nested_name_specifier (parser);
3384 /* Spot cases that cannot be the beginning of a
3385 nested-name-specifier. On the second and subsequent times
3386 through the loop, we look for the `template' keyword. */
3387 if (success && token->keyword == RID_TEMPLATE)
3389 /* A template-id can start a nested-name-specifier. */
3390 else if (token->type == CPP_TEMPLATE_ID)
3394 /* If the next token is not an identifier, then it is
3395 definitely not a class-or-namespace-name. */
3396 if (token->type != CPP_NAME)
3398 /* If the following token is neither a `<' (to begin a
3399 template-id), nor a `::', then we are not looking at a
3400 nested-name-specifier. */
3401 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3402 if (token->type != CPP_SCOPE
3403 && !cp_parser_nth_token_starts_template_argument_list_p
3408 /* The nested-name-specifier is optional, so we parse
3410 cp_parser_parse_tentatively (parser);
3412 /* Look for the optional `template' keyword, if this isn't the
3413 first time through the loop. */
3415 template_keyword_p = cp_parser_optional_template_keyword (parser);
3417 template_keyword_p = false;
3419 /* Save the old scope since the name lookup we are about to do
3420 might destroy it. */
3421 old_scope = parser->scope;
3422 saved_qualifying_scope = parser->qualifying_scope;
3423 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3424 look up names in "X<T>::I" in order to determine that "Y" is
3425 a template. So, if we have a typename at this point, we make
3426 an effort to look through it. */
3428 && !typename_keyword_p
3430 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3431 parser->scope = resolve_typename_type (parser->scope,
3432 /*only_current_p=*/false);
3433 /* Parse the qualifying entity. */
3435 = cp_parser_class_or_namespace_name (parser,
3441 /* Look for the `::' token. */
3442 cp_parser_require (parser, CPP_SCOPE, "`::'");
3444 /* If we found what we wanted, we keep going; otherwise, we're
3446 if (!cp_parser_parse_definitely (parser))
3448 bool error_p = false;
3450 /* Restore the OLD_SCOPE since it was valid before the
3451 failed attempt at finding the last
3452 class-or-namespace-name. */
3453 parser->scope = old_scope;
3454 parser->qualifying_scope = saved_qualifying_scope;
3455 /* If the next token is an identifier, and the one after
3456 that is a `::', then any valid interpretation would have
3457 found a class-or-namespace-name. */
3458 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3459 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3461 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3464 token = cp_lexer_consume_token (parser->lexer);
3469 decl = cp_parser_lookup_name_simple (parser, token->value);
3470 if (TREE_CODE (decl) == TEMPLATE_DECL)
3471 error ("%qD used without template parameters", decl);
3473 cp_parser_name_lookup_error
3474 (parser, token->value, decl,
3475 "is not a class or namespace");
3476 parser->scope = NULL_TREE;
3478 /* Treat this as a successful nested-name-specifier
3483 If the name found is not a class-name (clause
3484 _class_) or namespace-name (_namespace.def_), the
3485 program is ill-formed. */
3488 cp_lexer_consume_token (parser->lexer);
3493 /* We've found one valid nested-name-specifier. */
3495 /* Make sure we look in the right scope the next time through
3497 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3498 ? TREE_TYPE (new_scope)
3500 /* If it is a class scope, try to complete it; we are about to
3501 be looking up names inside the class. */
3502 if (TYPE_P (parser->scope)
3503 /* Since checking types for dependency can be expensive,
3504 avoid doing it if the type is already complete. */
3505 && !COMPLETE_TYPE_P (parser->scope)
3506 /* Do not try to complete dependent types. */
3507 && !dependent_type_p (parser->scope))
3508 complete_type (parser->scope);
3511 /* Retrieve any deferred checks. Do not pop this access checks yet
3512 so the memory will not be reclaimed during token replacing below. */
3513 access_check = get_deferred_access_checks ();
3515 /* If parsing tentatively, replace the sequence of tokens that makes
3516 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3517 token. That way, should we re-parse the token stream, we will
3518 not have to repeat the effort required to do the parse, nor will
3519 we issue duplicate error messages. */
3520 if (success && start)
3522 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3524 /* Reset the contents of the START token. */
3525 token->type = CPP_NESTED_NAME_SPECIFIER;
3526 token->value = build_tree_list (access_check, parser->scope);
3527 TREE_TYPE (token->value) = parser->qualifying_scope;
3528 token->keyword = RID_MAX;
3530 /* Purge all subsequent tokens. */
3531 cp_lexer_purge_tokens_after (parser->lexer, start);
3534 pop_deferring_access_checks ();
3535 return success ? parser->scope : NULL_TREE;
3538 /* Parse a nested-name-specifier. See
3539 cp_parser_nested_name_specifier_opt for details. This function
3540 behaves identically, except that it will an issue an error if no
3541 nested-name-specifier is present, and it will return
3542 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3546 cp_parser_nested_name_specifier (cp_parser *parser,
3547 bool typename_keyword_p,
3548 bool check_dependency_p,
3550 bool is_declaration)
3554 /* Look for the nested-name-specifier. */
3555 scope = cp_parser_nested_name_specifier_opt (parser,
3560 /* If it was not present, issue an error message. */
3563 cp_parser_error (parser, "expected nested-name-specifier");
3564 parser->scope = NULL_TREE;
3565 return error_mark_node;
3571 /* Parse a class-or-namespace-name.
3573 class-or-namespace-name:
3577 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3578 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3579 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3580 TYPE_P is TRUE iff the next name should be taken as a class-name,
3581 even the same name is declared to be another entity in the same
3584 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3585 specified by the class-or-namespace-name. If neither is found the
3586 ERROR_MARK_NODE is returned. */
3589 cp_parser_class_or_namespace_name (cp_parser *parser,
3590 bool typename_keyword_p,
3591 bool template_keyword_p,
3592 bool check_dependency_p,
3594 bool is_declaration)
3597 tree saved_qualifying_scope;
3598 tree saved_object_scope;
3602 /* Before we try to parse the class-name, we must save away the
3603 current PARSER->SCOPE since cp_parser_class_name will destroy
3605 saved_scope = parser->scope;
3606 saved_qualifying_scope = parser->qualifying_scope;
3607 saved_object_scope = parser->object_scope;
3608 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3609 there is no need to look for a namespace-name. */
3610 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3612 cp_parser_parse_tentatively (parser);
3613 scope = cp_parser_class_name (parser,
3618 /*class_head_p=*/false,
3620 /* If that didn't work, try for a namespace-name. */
3621 if (!only_class_p && !cp_parser_parse_definitely (parser))
3623 /* Restore the saved scope. */
3624 parser->scope = saved_scope;
3625 parser->qualifying_scope = saved_qualifying_scope;
3626 parser->object_scope = saved_object_scope;
3627 /* If we are not looking at an identifier followed by the scope
3628 resolution operator, then this is not part of a
3629 nested-name-specifier. (Note that this function is only used
3630 to parse the components of a nested-name-specifier.) */
3631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3632 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3633 return error_mark_node;
3634 scope = cp_parser_namespace_name (parser);
3640 /* Parse a postfix-expression.
3644 postfix-expression [ expression ]
3645 postfix-expression ( expression-list [opt] )
3646 simple-type-specifier ( expression-list [opt] )
3647 typename :: [opt] nested-name-specifier identifier
3648 ( expression-list [opt] )
3649 typename :: [opt] nested-name-specifier template [opt] template-id
3650 ( expression-list [opt] )
3651 postfix-expression . template [opt] id-expression
3652 postfix-expression -> template [opt] id-expression
3653 postfix-expression . pseudo-destructor-name
3654 postfix-expression -> pseudo-destructor-name
3655 postfix-expression ++
3656 postfix-expression --
3657 dynamic_cast < type-id > ( expression )
3658 static_cast < type-id > ( expression )
3659 reinterpret_cast < type-id > ( expression )
3660 const_cast < type-id > ( expression )
3661 typeid ( expression )
3667 ( type-id ) { initializer-list , [opt] }
3669 This extension is a GNU version of the C99 compound-literal
3670 construct. (The C99 grammar uses `type-name' instead of `type-id',
3671 but they are essentially the same concept.)
3673 If ADDRESS_P is true, the postfix expression is the operand of the
3676 Returns a representation of the expression. */
3679 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3683 cp_id_kind idk = CP_ID_KIND_NONE;
3684 tree postfix_expression = NULL_TREE;
3685 /* Non-NULL only if the current postfix-expression can be used to
3686 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3687 class used to qualify the member. */
3688 tree qualifying_class = NULL_TREE;
3690 /* Peek at the next token. */
3691 token = cp_lexer_peek_token (parser->lexer);
3692 /* Some of the productions are determined by keywords. */
3693 keyword = token->keyword;
3703 const char *saved_message;
3705 /* All of these can be handled in the same way from the point
3706 of view of parsing. Begin by consuming the token
3707 identifying the cast. */
3708 cp_lexer_consume_token (parser->lexer);
3710 /* New types cannot be defined in the cast. */
3711 saved_message = parser->type_definition_forbidden_message;
3712 parser->type_definition_forbidden_message
3713 = "types may not be defined in casts";
3715 /* Look for the opening `<'. */
3716 cp_parser_require (parser, CPP_LESS, "`<'");
3717 /* Parse the type to which we are casting. */
3718 type = cp_parser_type_id (parser);
3719 /* Look for the closing `>'. */
3720 cp_parser_require (parser, CPP_GREATER, "`>'");
3721 /* Restore the old message. */
3722 parser->type_definition_forbidden_message = saved_message;
3724 /* And the expression which is being cast. */
3725 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3726 expression = cp_parser_expression (parser);
3727 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3729 /* Only type conversions to integral or enumeration types
3730 can be used in constant-expressions. */
3731 if (parser->integral_constant_expression_p
3732 && !dependent_type_p (type)
3733 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3734 && (cp_parser_non_integral_constant_expression
3736 "a cast to a type other than an integral or "
3737 "enumeration type")))
3738 return error_mark_node;
3744 = build_dynamic_cast (type, expression);
3748 = build_static_cast (type, expression);
3752 = build_reinterpret_cast (type, expression);
3756 = build_const_cast (type, expression);
3767 const char *saved_message;
3768 bool saved_in_type_id_in_expr_p;
3770 /* Consume the `typeid' token. */
3771 cp_lexer_consume_token (parser->lexer);
3772 /* Look for the `(' token. */
3773 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3774 /* Types cannot be defined in a `typeid' expression. */
3775 saved_message = parser->type_definition_forbidden_message;
3776 parser->type_definition_forbidden_message
3777 = "types may not be defined in a `typeid\' expression";
3778 /* We can't be sure yet whether we're looking at a type-id or an
3780 cp_parser_parse_tentatively (parser);
3781 /* Try a type-id first. */
3782 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3783 parser->in_type_id_in_expr_p = true;
3784 type = cp_parser_type_id (parser);
3785 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3786 /* Look for the `)' token. Otherwise, we can't be sure that
3787 we're not looking at an expression: consider `typeid (int
3788 (3))', for example. */
3789 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3790 /* If all went well, simply lookup the type-id. */
3791 if (cp_parser_parse_definitely (parser))
3792 postfix_expression = get_typeid (type);
3793 /* Otherwise, fall back to the expression variant. */
3798 /* Look for an expression. */
3799 expression = cp_parser_expression (parser);
3800 /* Compute its typeid. */
3801 postfix_expression = build_typeid (expression);
3802 /* Look for the `)' token. */
3803 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3805 /* `typeid' may not appear in an integral constant expression. */
3806 if (cp_parser_non_integral_constant_expression(parser,
3807 "`typeid' operator"))
3808 return error_mark_node;
3809 /* Restore the saved message. */
3810 parser->type_definition_forbidden_message = saved_message;
3816 bool template_p = false;
3820 /* Consume the `typename' token. */
3821 cp_lexer_consume_token (parser->lexer);
3822 /* Look for the optional `::' operator. */
3823 cp_parser_global_scope_opt (parser,
3824 /*current_scope_valid_p=*/false);
3825 /* Look for the nested-name-specifier. */
3826 cp_parser_nested_name_specifier (parser,
3827 /*typename_keyword_p=*/true,
3828 /*check_dependency_p=*/true,
3830 /*is_declaration=*/true);
3831 /* Look for the optional `template' keyword. */
3832 template_p = cp_parser_optional_template_keyword (parser);
3833 /* We don't know whether we're looking at a template-id or an
3835 cp_parser_parse_tentatively (parser);
3836 /* Try a template-id. */
3837 id = cp_parser_template_id (parser, template_p,
3838 /*check_dependency_p=*/true,
3839 /*is_declaration=*/true);
3840 /* If that didn't work, try an identifier. */
3841 if (!cp_parser_parse_definitely (parser))
3842 id = cp_parser_identifier (parser);
3843 /* If we look up a template-id in a non-dependent qualifying
3844 scope, there's no need to create a dependent type. */
3845 if (TREE_CODE (id) == TYPE_DECL
3846 && !dependent_type_p (parser->scope))
3847 type = TREE_TYPE (id);
3848 /* Create a TYPENAME_TYPE to represent the type to which the
3849 functional cast is being performed. */
3851 type = make_typename_type (parser->scope, id,
3854 postfix_expression = cp_parser_functional_cast (parser, type);
3862 /* If the next thing is a simple-type-specifier, we may be
3863 looking at a functional cast. We could also be looking at
3864 an id-expression. So, we try the functional cast, and if
3865 that doesn't work we fall back to the primary-expression. */
3866 cp_parser_parse_tentatively (parser);
3867 /* Look for the simple-type-specifier. */
3868 type = cp_parser_simple_type_specifier (parser,
3869 /*decl_specs=*/NULL,
3870 CP_PARSER_FLAGS_NONE);
3871 /* Parse the cast itself. */
3872 if (!cp_parser_error_occurred (parser))
3874 = cp_parser_functional_cast (parser, type);
3875 /* If that worked, we're done. */
3876 if (cp_parser_parse_definitely (parser))
3879 /* If the functional-cast didn't work out, try a
3880 compound-literal. */
3881 if (cp_parser_allow_gnu_extensions_p (parser)
3882 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3884 tree initializer_list = NULL_TREE;
3885 bool saved_in_type_id_in_expr_p;
3887 cp_parser_parse_tentatively (parser);
3888 /* Consume the `('. */
3889 cp_lexer_consume_token (parser->lexer);
3890 /* Parse the type. */
3891 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3892 parser->in_type_id_in_expr_p = true;
3893 type = cp_parser_type_id (parser);
3894 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3895 /* Look for the `)'. */
3896 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3897 /* Look for the `{'. */
3898 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3899 /* If things aren't going well, there's no need to
3901 if (!cp_parser_error_occurred (parser))
3903 bool non_constant_p;
3904 /* Parse the initializer-list. */
3906 = cp_parser_initializer_list (parser, &non_constant_p);
3907 /* Allow a trailing `,'. */
3908 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3909 cp_lexer_consume_token (parser->lexer);
3910 /* Look for the final `}'. */
3911 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3913 /* If that worked, we're definitely looking at a
3914 compound-literal expression. */
3915 if (cp_parser_parse_definitely (parser))
3917 /* Warn the user that a compound literal is not
3918 allowed in standard C++. */
3920 pedwarn ("ISO C++ forbids compound-literals");
3921 /* Form the representation of the compound-literal. */
3923 = finish_compound_literal (type, initializer_list);
3928 /* It must be a primary-expression. */
3929 postfix_expression = cp_parser_primary_expression (parser,
3936 /* If we were avoiding committing to the processing of a
3937 qualified-id until we knew whether or not we had a
3938 pointer-to-member, we now know. */
3939 if (qualifying_class)
3943 /* Peek at the next token. */
3944 token = cp_lexer_peek_token (parser->lexer);
3945 done = (token->type != CPP_OPEN_SQUARE
3946 && token->type != CPP_OPEN_PAREN
3947 && token->type != CPP_DOT
3948 && token->type != CPP_DEREF
3949 && token->type != CPP_PLUS_PLUS
3950 && token->type != CPP_MINUS_MINUS);
3952 postfix_expression = finish_qualified_id_expr (qualifying_class,
3957 return postfix_expression;
3960 /* Keep looping until the postfix-expression is complete. */
3963 if (idk == CP_ID_KIND_UNQUALIFIED
3964 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3965 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3966 /* It is not a Koenig lookup function call. */
3968 = unqualified_name_lookup_error (postfix_expression);
3970 /* Peek at the next token. */
3971 token = cp_lexer_peek_token (parser->lexer);
3973 switch (token->type)
3975 case CPP_OPEN_SQUARE:
3977 = cp_parser_postfix_open_square_expression (parser,
3980 idk = CP_ID_KIND_NONE;
3983 case CPP_OPEN_PAREN:
3984 /* postfix-expression ( expression-list [opt] ) */
3987 tree args = (cp_parser_parenthesized_expression_list
3988 (parser, false, /*non_constant_p=*/NULL));
3990 if (args == error_mark_node)
3992 postfix_expression = error_mark_node;
3996 /* Function calls are not permitted in
3997 constant-expressions. */
3998 if (cp_parser_non_integral_constant_expression (parser,
4001 postfix_expression = error_mark_node;
4006 if (idk == CP_ID_KIND_UNQUALIFIED)
4008 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4014 = perform_koenig_lookup (postfix_expression, args);
4018 = unqualified_fn_lookup_error (postfix_expression);
4020 /* We do not perform argument-dependent lookup if
4021 normal lookup finds a non-function, in accordance
4022 with the expected resolution of DR 218. */
4023 else if (args && is_overloaded_fn (postfix_expression))
4025 tree fn = get_first_fn (postfix_expression);
4027 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4028 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4030 /* Only do argument dependent lookup if regular
4031 lookup does not find a set of member functions.
4032 [basic.lookup.koenig]/2a */
4033 if (!DECL_FUNCTION_MEMBER_P (fn))
4037 = perform_koenig_lookup (postfix_expression, args);
4042 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4044 tree instance = TREE_OPERAND (postfix_expression, 0);
4045 tree fn = TREE_OPERAND (postfix_expression, 1);
4047 if (processing_template_decl
4048 && (type_dependent_expression_p (instance)
4049 || (!BASELINK_P (fn)
4050 && TREE_CODE (fn) != FIELD_DECL)
4051 || type_dependent_expression_p (fn)
4052 || any_type_dependent_arguments_p (args)))
4055 = build_min_nt (CALL_EXPR, postfix_expression,
4060 if (BASELINK_P (fn))
4062 = (build_new_method_call
4063 (instance, fn, args, NULL_TREE,
4064 (idk == CP_ID_KIND_QUALIFIED
4065 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4068 = finish_call_expr (postfix_expression, args,
4069 /*disallow_virtual=*/false,
4070 /*koenig_p=*/false);
4072 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4073 || TREE_CODE (postfix_expression) == MEMBER_REF
4074 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4075 postfix_expression = (build_offset_ref_call_from_tree
4076 (postfix_expression, args));
4077 else if (idk == CP_ID_KIND_QUALIFIED)
4078 /* A call to a static class member, or a namespace-scope
4081 = finish_call_expr (postfix_expression, args,
4082 /*disallow_virtual=*/true,
4085 /* All other function calls. */
4087 = finish_call_expr (postfix_expression, args,
4088 /*disallow_virtual=*/false,
4091 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4092 idk = CP_ID_KIND_NONE;
4098 /* postfix-expression . template [opt] id-expression
4099 postfix-expression . pseudo-destructor-name
4100 postfix-expression -> template [opt] id-expression
4101 postfix-expression -> pseudo-destructor-name */
4103 /* Consume the `.' or `->' operator. */
4104 cp_lexer_consume_token (parser->lexer);
4107 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4113 /* postfix-expression ++ */
4114 /* Consume the `++' token. */
4115 cp_lexer_consume_token (parser->lexer);
4116 /* Generate a representation for the complete expression. */
4118 = finish_increment_expr (postfix_expression,
4119 POSTINCREMENT_EXPR);
4120 /* Increments may not appear in constant-expressions. */
4121 if (cp_parser_non_integral_constant_expression (parser,
4123 postfix_expression = error_mark_node;
4124 idk = CP_ID_KIND_NONE;
4127 case CPP_MINUS_MINUS:
4128 /* postfix-expression -- */
4129 /* Consume the `--' token. */
4130 cp_lexer_consume_token (parser->lexer);
4131 /* Generate a representation for the complete expression. */
4133 = finish_increment_expr (postfix_expression,
4134 POSTDECREMENT_EXPR);
4135 /* Decrements may not appear in constant-expressions. */
4136 if (cp_parser_non_integral_constant_expression (parser,
4138 postfix_expression = error_mark_node;
4139 idk = CP_ID_KIND_NONE;
4143 return postfix_expression;
4147 /* We should never get here. */
4149 return error_mark_node;
4152 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4153 by cp_parser_builtin_offsetof. We're looking for
4155 postfix-expression [ expression ]
4157 FOR_OFFSETOF is set if we're being called in that context, which
4158 changes how we deal with integer constant expressions. */
4161 cp_parser_postfix_open_square_expression (cp_parser *parser,
4162 tree postfix_expression,
4167 /* Consume the `[' token. */
4168 cp_lexer_consume_token (parser->lexer);
4170 /* Parse the index expression. */
4171 /* ??? For offsetof, there is a question of what to allow here. If
4172 offsetof is not being used in an integral constant expression context,
4173 then we *could* get the right answer by computing the value at runtime.
4174 If we are in an integral constant expression context, then we might
4175 could accept any constant expression; hard to say without analysis.
4176 Rather than open the barn door too wide right away, allow only integer
4177 constant expressions here. */
4179 index = cp_parser_constant_expression (parser, false, NULL);
4181 index = cp_parser_expression (parser);
4183 /* Look for the closing `]'. */
4184 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4186 /* Build the ARRAY_REF. */
4187 postfix_expression = grok_array_decl (postfix_expression, index);
4189 /* When not doing offsetof, array references are not permitted in
4190 constant-expressions. */
4192 && (cp_parser_non_integral_constant_expression
4193 (parser, "an array reference")))
4194 postfix_expression = error_mark_node;
4196 return postfix_expression;
4199 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4200 by cp_parser_builtin_offsetof. We're looking for
4202 postfix-expression . template [opt] id-expression
4203 postfix-expression . pseudo-destructor-name
4204 postfix-expression -> template [opt] id-expression
4205 postfix-expression -> pseudo-destructor-name
4207 FOR_OFFSETOF is set if we're being called in that context. That sorta
4208 limits what of the above we'll actually accept, but nevermind.
4209 TOKEN_TYPE is the "." or "->" token, which will already have been
4210 removed from the stream. */
4213 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4214 enum cpp_ttype token_type,
4215 tree postfix_expression,
4216 bool for_offsetof, cp_id_kind *idk)
4221 bool pseudo_destructor_p;
4222 tree scope = NULL_TREE;
4224 /* If this is a `->' operator, dereference the pointer. */
4225 if (token_type == CPP_DEREF)
4226 postfix_expression = build_x_arrow (postfix_expression);
4227 /* Check to see whether or not the expression is type-dependent. */
4228 dependent_p = type_dependent_expression_p (postfix_expression);
4229 /* The identifier following the `->' or `.' is not qualified. */
4230 parser->scope = NULL_TREE;
4231 parser->qualifying_scope = NULL_TREE;
4232 parser->object_scope = NULL_TREE;
4233 *idk = CP_ID_KIND_NONE;
4234 /* Enter the scope corresponding to the type of the object
4235 given by the POSTFIX_EXPRESSION. */
4236 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4238 scope = TREE_TYPE (postfix_expression);
4239 /* According to the standard, no expression should ever have
4240 reference type. Unfortunately, we do not currently match
4241 the standard in this respect in that our internal representation
4242 of an expression may have reference type even when the standard
4243 says it does not. Therefore, we have to manually obtain the
4244 underlying type here. */
4245 scope = non_reference (scope);
4246 /* The type of the POSTFIX_EXPRESSION must be complete. */
4247 scope = complete_type_or_else (scope, NULL_TREE);
4248 /* Let the name lookup machinery know that we are processing a
4249 class member access expression. */
4250 parser->context->object_type = scope;
4251 /* If something went wrong, we want to be able to discern that case,
4252 as opposed to the case where there was no SCOPE due to the type
4253 of expression being dependent. */
4255 scope = error_mark_node;
4256 /* If the SCOPE was erroneous, make the various semantic analysis
4257 functions exit quickly -- and without issuing additional error
4259 if (scope == error_mark_node)
4260 postfix_expression = error_mark_node;
4263 /* Assume this expression is not a pseudo-destructor access. */
4264 pseudo_destructor_p = false;
4266 /* If the SCOPE is a scalar type, then, if this is a valid program,
4267 we must be looking at a pseudo-destructor-name. */
4268 if (scope && SCALAR_TYPE_P (scope))
4273 cp_parser_parse_tentatively (parser);
4274 /* Parse the pseudo-destructor-name. */
4276 cp_parser_pseudo_destructor_name (parser, &s, &type);
4277 if (cp_parser_parse_definitely (parser))
4279 pseudo_destructor_p = true;
4281 = finish_pseudo_destructor_expr (postfix_expression,
4282 s, TREE_TYPE (type));
4286 if (!pseudo_destructor_p)
4288 /* If the SCOPE is not a scalar type, we are looking at an
4289 ordinary class member access expression, rather than a
4290 pseudo-destructor-name. */
4291 template_p = cp_parser_optional_template_keyword (parser);
4292 /* Parse the id-expression. */
4293 name = cp_parser_id_expression (parser, template_p,
4294 /*check_dependency_p=*/true,
4295 /*template_p=*/NULL,
4296 /*declarator_p=*/false);
4297 /* In general, build a SCOPE_REF if the member name is qualified.
4298 However, if the name was not dependent and has already been
4299 resolved; there is no need to build the SCOPE_REF. For example;
4301 struct X { void f(); };
4302 template <typename T> void f(T* t) { t->X::f(); }
4304 Even though "t" is dependent, "X::f" is not and has been resolved
4305 to a BASELINK; there is no need to include scope information. */
4307 /* But we do need to remember that there was an explicit scope for
4308 virtual function calls. */
4310 *idk = CP_ID_KIND_QUALIFIED;
4312 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4314 name = build_nt (SCOPE_REF, parser->scope, name);
4315 parser->scope = NULL_TREE;
4316 parser->qualifying_scope = NULL_TREE;
4317 parser->object_scope = NULL_TREE;
4319 if (scope && name && BASELINK_P (name))
4320 adjust_result_of_qualified_name_lookup
4321 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4323 = finish_class_member_access_expr (postfix_expression, name);
4326 /* We no longer need to look up names in the scope of the object on
4327 the left-hand side of the `.' or `->' operator. */
4328 parser->context->object_type = NULL_TREE;
4330 /* Outside of offsetof, these operators may not appear in
4331 constant-expressions. */
4333 && (cp_parser_non_integral_constant_expression
4334 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4335 postfix_expression = error_mark_node;
4337 return postfix_expression;
4340 /* Parse a parenthesized expression-list.
4343 assignment-expression
4344 expression-list, assignment-expression
4349 identifier, expression-list
4351 Returns a TREE_LIST. The TREE_VALUE of each node is a
4352 representation of an assignment-expression. Note that a TREE_LIST
4353 is returned even if there is only a single expression in the list.
4354 error_mark_node is returned if the ( and or ) are
4355 missing. NULL_TREE is returned on no expressions. The parentheses
4356 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4357 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4358 indicates whether or not all of the expressions in the list were
4362 cp_parser_parenthesized_expression_list (cp_parser* parser,
4363 bool is_attribute_list,
4364 bool *non_constant_p)
4366 tree expression_list = NULL_TREE;
4367 bool fold_expr_p = is_attribute_list;
4368 tree identifier = NULL_TREE;
4370 /* Assume all the expressions will be constant. */
4372 *non_constant_p = false;
4374 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4375 return error_mark_node;
4377 /* Consume expressions until there are no more. */
4378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4383 /* At the beginning of attribute lists, check to see if the
4384 next token is an identifier. */
4385 if (is_attribute_list
4386 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4390 /* Consume the identifier. */
4391 token = cp_lexer_consume_token (parser->lexer);
4392 /* Save the identifier. */
4393 identifier = token->value;
4397 /* Parse the next assignment-expression. */
4400 bool expr_non_constant_p;
4401 expr = (cp_parser_constant_expression
4402 (parser, /*allow_non_constant_p=*/true,
4403 &expr_non_constant_p));
4404 if (expr_non_constant_p)
4405 *non_constant_p = true;
4408 expr = cp_parser_assignment_expression (parser);
4411 expr = fold_non_dependent_expr (expr);
4413 /* Add it to the list. We add error_mark_node
4414 expressions to the list, so that we can still tell if
4415 the correct form for a parenthesized expression-list
4416 is found. That gives better errors. */
4417 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4419 if (expr == error_mark_node)
4423 /* After the first item, attribute lists look the same as
4424 expression lists. */
4425 is_attribute_list = false;
4428 /* If the next token isn't a `,', then we are done. */
4429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4432 /* Otherwise, consume the `,' and keep going. */
4433 cp_lexer_consume_token (parser->lexer);
4436 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4441 /* We try and resync to an unnested comma, as that will give the
4442 user better diagnostics. */
4443 ending = cp_parser_skip_to_closing_parenthesis (parser,
4444 /*recovering=*/true,
4446 /*consume_paren=*/true);
4450 return error_mark_node;
4453 /* We built up the list in reverse order so we must reverse it now. */
4454 expression_list = nreverse (expression_list);
4456 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4458 return expression_list;
4461 /* Parse a pseudo-destructor-name.
4463 pseudo-destructor-name:
4464 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4465 :: [opt] nested-name-specifier template template-id :: ~ type-name
4466 :: [opt] nested-name-specifier [opt] ~ type-name
4468 If either of the first two productions is used, sets *SCOPE to the
4469 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4470 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4471 or ERROR_MARK_NODE if the parse fails. */
4474 cp_parser_pseudo_destructor_name (cp_parser* parser,
4478 bool nested_name_specifier_p;
4480 /* Assume that things will not work out. */
4481 *type = error_mark_node;
4483 /* Look for the optional `::' operator. */
4484 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4485 /* Look for the optional nested-name-specifier. */
4486 nested_name_specifier_p
4487 = (cp_parser_nested_name_specifier_opt (parser,
4488 /*typename_keyword_p=*/false,
4489 /*check_dependency_p=*/true,
4491 /*is_declaration=*/true)
4493 /* Now, if we saw a nested-name-specifier, we might be doing the
4494 second production. */
4495 if (nested_name_specifier_p
4496 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4498 /* Consume the `template' keyword. */
4499 cp_lexer_consume_token (parser->lexer);
4500 /* Parse the template-id. */
4501 cp_parser_template_id (parser,
4502 /*template_keyword_p=*/true,
4503 /*check_dependency_p=*/false,
4504 /*is_declaration=*/true);
4505 /* Look for the `::' token. */
4506 cp_parser_require (parser, CPP_SCOPE, "`::'");
4508 /* If the next token is not a `~', then there might be some
4509 additional qualification. */
4510 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4512 /* Look for the type-name. */
4513 *scope = TREE_TYPE (cp_parser_type_name (parser));
4515 if (*scope == error_mark_node)
4518 /* If we don't have ::~, then something has gone wrong. Since
4519 the only caller of this function is looking for something
4520 after `.' or `->' after a scalar type, most likely the
4521 program is trying to get a member of a non-aggregate
4523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4524 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4526 cp_parser_error (parser, "request for member of non-aggregate type");
4530 /* Look for the `::' token. */
4531 cp_parser_require (parser, CPP_SCOPE, "`::'");
4536 /* Look for the `~'. */
4537 cp_parser_require (parser, CPP_COMPL, "`~'");
4538 /* Look for the type-name again. We are not responsible for
4539 checking that it matches the first type-name. */
4540 *type = cp_parser_type_name (parser);
4543 /* Parse a unary-expression.
4549 unary-operator cast-expression
4550 sizeof unary-expression
4558 __extension__ cast-expression
4559 __alignof__ unary-expression
4560 __alignof__ ( type-id )
4561 __real__ cast-expression
4562 __imag__ cast-expression
4565 ADDRESS_P is true iff the unary-expression is appearing as the
4566 operand of the `&' operator.
4568 Returns a representation of the expression. */
4571 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4574 enum tree_code unary_operator;
4576 /* Peek at the next token. */
4577 token = cp_lexer_peek_token (parser->lexer);
4578 /* Some keywords give away the kind of expression. */
4579 if (token->type == CPP_KEYWORD)
4581 enum rid keyword = token->keyword;
4591 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4592 /* Consume the token. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Parse the operand. */
4595 operand = cp_parser_sizeof_operand (parser, keyword);
4597 if (TYPE_P (operand))
4598 return cxx_sizeof_or_alignof_type (operand, op, true);
4600 return cxx_sizeof_or_alignof_expr (operand, op);
4604 return cp_parser_new_expression (parser);
4607 return cp_parser_delete_expression (parser);
4611 /* The saved value of the PEDANTIC flag. */
4615 /* Save away the PEDANTIC flag. */
4616 cp_parser_extension_opt (parser, &saved_pedantic);
4617 /* Parse the cast-expression. */
4618 expr = cp_parser_simple_cast_expression (parser);
4619 /* Restore the PEDANTIC flag. */
4620 pedantic = saved_pedantic;
4630 /* Consume the `__real__' or `__imag__' token. */
4631 cp_lexer_consume_token (parser->lexer);
4632 /* Parse the cast-expression. */
4633 expression = cp_parser_simple_cast_expression (parser);
4634 /* Create the complete representation. */
4635 return build_x_unary_op ((keyword == RID_REALPART
4636 ? REALPART_EXPR : IMAGPART_EXPR),
4646 /* Look for the `:: new' and `:: delete', which also signal the
4647 beginning of a new-expression, or delete-expression,
4648 respectively. If the next token is `::', then it might be one of
4650 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4654 /* See if the token after the `::' is one of the keywords in
4655 which we're interested. */
4656 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4657 /* If it's `new', we have a new-expression. */
4658 if (keyword == RID_NEW)
4659 return cp_parser_new_expression (parser);
4660 /* Similarly, for `delete'. */
4661 else if (keyword == RID_DELETE)
4662 return cp_parser_delete_expression (parser);
4665 /* Look for a unary operator. */
4666 unary_operator = cp_parser_unary_operator (token);
4667 /* The `++' and `--' operators can be handled similarly, even though
4668 they are not technically unary-operators in the grammar. */
4669 if (unary_operator == ERROR_MARK)
4671 if (token->type == CPP_PLUS_PLUS)
4672 unary_operator = PREINCREMENT_EXPR;
4673 else if (token->type == CPP_MINUS_MINUS)
4674 unary_operator = PREDECREMENT_EXPR;
4675 /* Handle the GNU address-of-label extension. */
4676 else if (cp_parser_allow_gnu_extensions_p (parser)
4677 && token->type == CPP_AND_AND)
4681 /* Consume the '&&' token. */
4682 cp_lexer_consume_token (parser->lexer);
4683 /* Look for the identifier. */
4684 identifier = cp_parser_identifier (parser);
4685 /* Create an expression representing the address. */
4686 return finish_label_address_expr (identifier);
4689 if (unary_operator != ERROR_MARK)
4691 tree cast_expression;
4692 tree expression = error_mark_node;
4693 const char *non_constant_p = NULL;
4695 /* Consume the operator token. */
4696 token = cp_lexer_consume_token (parser->lexer);
4697 /* Parse the cast-expression. */
4699 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4700 /* Now, build an appropriate representation. */
4701 switch (unary_operator)
4704 non_constant_p = "`*'";
4705 expression = build_x_indirect_ref (cast_expression, "unary *");
4709 non_constant_p = "`&'";
4712 expression = build_x_unary_op (unary_operator, cast_expression);
4715 case PREINCREMENT_EXPR:
4716 case PREDECREMENT_EXPR:
4717 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4722 case TRUTH_NOT_EXPR:
4723 expression = finish_unary_op_expr (unary_operator, cast_expression);
4731 && cp_parser_non_integral_constant_expression (parser,
4733 expression = error_mark_node;
4738 return cp_parser_postfix_expression (parser, address_p);
4741 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4742 unary-operator, the corresponding tree code is returned. */
4744 static enum tree_code
4745 cp_parser_unary_operator (cp_token* token)
4747 switch (token->type)
4750 return INDIRECT_REF;
4756 return CONVERT_EXPR;
4762 return TRUTH_NOT_EXPR;
4765 return BIT_NOT_EXPR;
4772 /* Parse a new-expression.
4775 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4776 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4778 Returns a representation of the expression. */
4781 cp_parser_new_expression (cp_parser* parser)
4783 bool global_scope_p;
4789 /* Look for the optional `::' operator. */
4791 = (cp_parser_global_scope_opt (parser,
4792 /*current_scope_valid_p=*/false)
4794 /* Look for the `new' operator. */
4795 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4796 /* There's no easy way to tell a new-placement from the
4797 `( type-id )' construct. */
4798 cp_parser_parse_tentatively (parser);
4799 /* Look for a new-placement. */
4800 placement = cp_parser_new_placement (parser);
4801 /* If that didn't work out, there's no new-placement. */
4802 if (!cp_parser_parse_definitely (parser))
4803 placement = NULL_TREE;
4805 /* If the next token is a `(', then we have a parenthesized
4807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4809 /* Consume the `('. */
4810 cp_lexer_consume_token (parser->lexer);
4811 /* Parse the type-id. */
4812 type = cp_parser_type_id (parser);
4813 /* Look for the closing `)'. */
4814 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4815 /* There should not be a direct-new-declarator in this production,
4816 but GCC used to allowed this, so we check and emit a sensible error
4817 message for this case. */
4818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4820 error ("array bound forbidden after parenthesized type-id");
4821 inform ("try removing the parentheses around the type-id");
4822 cp_parser_direct_new_declarator (parser);
4826 /* Otherwise, there must be a new-type-id. */
4828 type = cp_parser_new_type_id (parser, &nelts);
4830 /* If the next token is a `(', then we have a new-initializer. */
4831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4832 initializer = cp_parser_new_initializer (parser);
4834 initializer = NULL_TREE;
4836 /* A new-expression may not appear in an integral constant
4838 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4839 return error_mark_node;
4841 /* Create a representation of the new-expression. */
4842 return build_new (placement, type, nelts, initializer, global_scope_p);
4845 /* Parse a new-placement.
4850 Returns the same representation as for an expression-list. */
4853 cp_parser_new_placement (cp_parser* parser)
4855 tree expression_list;
4857 /* Parse the expression-list. */
4858 expression_list = (cp_parser_parenthesized_expression_list
4859 (parser, false, /*non_constant_p=*/NULL));
4861 return expression_list;
4864 /* Parse a new-type-id.
4867 type-specifier-seq new-declarator [opt]
4869 Returns the TYPE allocated. If the new-type-id indicates an array
4870 type, *NELTS is set to the number of elements in the last array
4871 bound; the TYPE will not include the last array bound. */
4874 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4876 cp_decl_specifier_seq type_specifier_seq;
4877 cp_declarator *new_declarator;
4878 cp_declarator *declarator;
4879 cp_declarator *outer_declarator;
4880 const char *saved_message;
4883 /* The type-specifier sequence must not contain type definitions.
4884 (It cannot contain declarations of new types either, but if they
4885 are not definitions we will catch that because they are not
4887 saved_message = parser->type_definition_forbidden_message;
4888 parser->type_definition_forbidden_message
4889 = "types may not be defined in a new-type-id";
4890 /* Parse the type-specifier-seq. */
4891 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4892 /* Restore the old message. */
4893 parser->type_definition_forbidden_message = saved_message;
4894 /* Parse the new-declarator. */
4895 new_declarator = cp_parser_new_declarator_opt (parser);
4897 /* Determine the number of elements in the last array dimension, if
4900 /* Skip down to the last array dimension. */
4901 declarator = new_declarator;
4902 outer_declarator = NULL;
4903 while (declarator && (declarator->kind == cdk_pointer
4904 || declarator->kind == cdk_ptrmem))
4906 outer_declarator = declarator;
4907 declarator = declarator->declarator;
4910 && declarator->kind == cdk_array
4911 && declarator->declarator
4912 && declarator->declarator->kind == cdk_array)
4914 outer_declarator = declarator;
4915 declarator = declarator->declarator;
4918 if (declarator && declarator->kind == cdk_array)
4920 *nelts = declarator->u.array.bounds;
4921 if (*nelts == error_mark_node)
4922 *nelts = integer_one_node;
4923 else if (!processing_template_decl)
4925 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4927 pedwarn ("size in array new must have integral type");
4928 *nelts = save_expr (cp_convert (sizetype, *nelts));
4929 if (*nelts == integer_zero_node)
4930 warning ("zero size array reserves no space");
4932 if (outer_declarator)
4933 outer_declarator->declarator = declarator->declarator;
4935 new_declarator = NULL;
4938 type = groktypename (&type_specifier_seq, new_declarator);
4939 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4941 *nelts = array_type_nelts_top (type);
4942 type = TREE_TYPE (type);
4947 /* Parse an (optional) new-declarator.
4950 ptr-operator new-declarator [opt]
4951 direct-new-declarator
4953 Returns the declarator. */
4955 static cp_declarator *
4956 cp_parser_new_declarator_opt (cp_parser* parser)
4958 enum tree_code code;
4960 cp_cv_quals cv_quals;
4962 /* We don't know if there's a ptr-operator next, or not. */
4963 cp_parser_parse_tentatively (parser);
4964 /* Look for a ptr-operator. */
4965 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4966 /* If that worked, look for more new-declarators. */
4967 if (cp_parser_parse_definitely (parser))
4969 cp_declarator *declarator;
4971 /* Parse another optional declarator. */
4972 declarator = cp_parser_new_declarator_opt (parser);
4974 /* Create the representation of the declarator. */
4976 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4977 else if (code == INDIRECT_REF)
4978 declarator = make_pointer_declarator (cv_quals, declarator);
4980 declarator = make_reference_declarator (cv_quals, declarator);
4985 /* If the next token is a `[', there is a direct-new-declarator. */
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4987 return cp_parser_direct_new_declarator (parser);
4992 /* Parse a direct-new-declarator.
4994 direct-new-declarator:
4996 direct-new-declarator [constant-expression]
5000 static cp_declarator *
5001 cp_parser_direct_new_declarator (cp_parser* parser)
5003 cp_declarator *declarator = NULL;
5009 /* Look for the opening `['. */
5010 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5011 /* The first expression is not required to be constant. */
5014 expression = cp_parser_expression (parser);
5015 /* The standard requires that the expression have integral
5016 type. DR 74 adds enumeration types. We believe that the
5017 real intent is that these expressions be handled like the
5018 expression in a `switch' condition, which also allows
5019 classes with a single conversion to integral or
5020 enumeration type. */
5021 if (!processing_template_decl)
5024 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5029 error ("expression in new-declarator must have integral "
5030 "or enumeration type");
5031 expression = error_mark_node;
5035 /* But all the other expressions must be. */
5038 = cp_parser_constant_expression (parser,
5039 /*allow_non_constant=*/false,
5041 /* Look for the closing `]'. */
5042 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5044 /* Add this bound to the declarator. */
5045 declarator = make_array_declarator (declarator, expression);
5047 /* If the next token is not a `[', then there are no more
5049 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5056 /* Parse a new-initializer.
5059 ( expression-list [opt] )
5061 Returns a representation of the expression-list. If there is no
5062 expression-list, VOID_ZERO_NODE is returned. */
5065 cp_parser_new_initializer (cp_parser* parser)
5067 tree expression_list;
5069 expression_list = (cp_parser_parenthesized_expression_list
5070 (parser, false, /*non_constant_p=*/NULL));
5071 if (!expression_list)
5072 expression_list = void_zero_node;
5074 return expression_list;
5077 /* Parse a delete-expression.
5080 :: [opt] delete cast-expression
5081 :: [opt] delete [ ] cast-expression
5083 Returns a representation of the expression. */
5086 cp_parser_delete_expression (cp_parser* parser)
5088 bool global_scope_p;
5092 /* Look for the optional `::' operator. */
5094 = (cp_parser_global_scope_opt (parser,
5095 /*current_scope_valid_p=*/false)
5097 /* Look for the `delete' keyword. */
5098 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5099 /* See if the array syntax is in use. */
5100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5102 /* Consume the `[' token. */
5103 cp_lexer_consume_token (parser->lexer);
5104 /* Look for the `]' token. */
5105 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5106 /* Remember that this is the `[]' construct. */
5112 /* Parse the cast-expression. */
5113 expression = cp_parser_simple_cast_expression (parser);
5115 /* A delete-expression may not appear in an integral constant
5117 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5118 return error_mark_node;
5120 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5123 /* Parse a cast-expression.
5127 ( type-id ) cast-expression
5129 Returns a representation of the expression. */
5132 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5134 /* If it's a `(', then we might be looking at a cast. */
5135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5137 tree type = NULL_TREE;
5138 tree expr = NULL_TREE;
5139 bool compound_literal_p;
5140 const char *saved_message;
5142 /* There's no way to know yet whether or not this is a cast.
5143 For example, `(int (3))' is a unary-expression, while `(int)
5144 3' is a cast. So, we resort to parsing tentatively. */
5145 cp_parser_parse_tentatively (parser);
5146 /* Types may not be defined in a cast. */
5147 saved_message = parser->type_definition_forbidden_message;
5148 parser->type_definition_forbidden_message
5149 = "types may not be defined in casts";
5150 /* Consume the `('. */
5151 cp_lexer_consume_token (parser->lexer);
5152 /* A very tricky bit is that `(struct S) { 3 }' is a
5153 compound-literal (which we permit in C++ as an extension).
5154 But, that construct is not a cast-expression -- it is a
5155 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5156 is legal; if the compound-literal were a cast-expression,
5157 you'd need an extra set of parentheses.) But, if we parse
5158 the type-id, and it happens to be a class-specifier, then we
5159 will commit to the parse at that point, because we cannot
5160 undo the action that is done when creating a new class. So,
5161 then we cannot back up and do a postfix-expression.
5163 Therefore, we scan ahead to the closing `)', and check to see
5164 if the token after the `)' is a `{'. If so, we are not
5165 looking at a cast-expression.
5167 Save tokens so that we can put them back. */
5168 cp_lexer_save_tokens (parser->lexer);
5169 /* Skip tokens until the next token is a closing parenthesis.
5170 If we find the closing `)', and the next token is a `{', then
5171 we are looking at a compound-literal. */
5173 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5174 /*consume_paren=*/true)
5175 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5176 /* Roll back the tokens we skipped. */
5177 cp_lexer_rollback_tokens (parser->lexer);
5178 /* If we were looking at a compound-literal, simulate an error
5179 so that the call to cp_parser_parse_definitely below will
5181 if (compound_literal_p)
5182 cp_parser_simulate_error (parser);
5185 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5186 parser->in_type_id_in_expr_p = true;
5187 /* Look for the type-id. */
5188 type = cp_parser_type_id (parser);
5189 /* Look for the closing `)'. */
5190 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5191 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5194 /* Restore the saved message. */
5195 parser->type_definition_forbidden_message = saved_message;
5197 /* If ok so far, parse the dependent expression. We cannot be
5198 sure it is a cast. Consider `(T ())'. It is a parenthesized
5199 ctor of T, but looks like a cast to function returning T
5200 without a dependent expression. */
5201 if (!cp_parser_error_occurred (parser))
5202 expr = cp_parser_simple_cast_expression (parser);
5204 if (cp_parser_parse_definitely (parser))
5206 /* Warn about old-style casts, if so requested. */
5207 if (warn_old_style_cast
5208 && !in_system_header
5209 && !VOID_TYPE_P (type)
5210 && current_lang_name != lang_name_c)
5211 warning ("use of old-style cast");
5213 /* Only type conversions to integral or enumeration types
5214 can be used in constant-expressions. */
5215 if (parser->integral_constant_expression_p
5216 && !dependent_type_p (type)
5217 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5218 && (cp_parser_non_integral_constant_expression
5220 "a cast to a type other than an integral or "
5221 "enumeration type")))
5222 return error_mark_node;
5224 /* Perform the cast. */
5225 expr = build_c_cast (type, expr);
5230 /* If we get here, then it's not a cast, so it must be a
5231 unary-expression. */
5232 return cp_parser_unary_expression (parser, address_p);
5235 /* Parse a binary expression of the general form:
5239 pm-expression .* cast-expression
5240 pm-expression ->* cast-expression
5242 multiplicative-expression:
5244 multiplicative-expression * pm-expression
5245 multiplicative-expression / pm-expression
5246 multiplicative-expression % pm-expression
5248 additive-expression:
5249 multiplicative-expression
5250 additive-expression + multiplicative-expression
5251 additive-expression - multiplicative-expression
5255 shift-expression << additive-expression
5256 shift-expression >> additive-expression
5258 relational-expression:
5260 relational-expression < shift-expression
5261 relational-expression > shift-expression
5262 relational-expression <= shift-expression
5263 relational-expression >= shift-expression
5267 relational-expression:
5268 relational-expression <? shift-expression
5269 relational-expression >? shift-expression
5271 equality-expression:
5272 relational-expression
5273 equality-expression == relational-expression
5274 equality-expression != relational-expression
5278 and-expression & equality-expression
5280 exclusive-or-expression:
5282 exclusive-or-expression ^ and-expression
5284 inclusive-or-expression:
5285 exclusive-or-expression
5286 inclusive-or-expression | exclusive-or-expression
5288 logical-and-expression:
5289 inclusive-or-expression
5290 logical-and-expression && inclusive-or-expression
5292 logical-or-expression:
5293 logical-and-expression
5294 logical-or-expression || logical-and-expression
5296 All these are implemented with a single function like:
5299 simple-cast-expression
5300 binary-expression <token> binary-expression
5302 The binops_by_token map is used to get the tree codes for each <token> type.
5303 binary-expressions are associated according to a precedence table. */
5305 #define TOKEN_PRECEDENCE(token) \
5306 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5307 ? PREC_NOT_OPERATOR \
5308 : binops_by_token[token->type].prec)
5311 cp_parser_binary_expression (cp_parser* parser)
5313 cp_parser_expression_stack stack;
5314 cp_parser_expression_stack_entry *sp = &stack[0];
5317 enum tree_code tree_type;
5318 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5321 /* Parse the first expression. */
5322 lhs = cp_parser_simple_cast_expression (parser);
5326 /* Get an operator token. */
5327 token = cp_lexer_peek_token (parser->lexer);
5328 new_prec = TOKEN_PRECEDENCE (token);
5330 /* Popping an entry off the stack means we completed a subexpression:
5331 - either we found a token which is not an operator (`>' where it is not
5332 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5333 will happen repeatedly;
5334 - or, we found an operator which has lower priority. This is the case
5335 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5337 if (new_prec <= prec)
5346 tree_type = binops_by_token[token->type].tree_type;
5348 /* We used the operator token. */
5349 cp_lexer_consume_token (parser->lexer);
5351 /* Extract another operand. It may be the RHS of this expression
5352 or the LHS of a new, higher priority expression. */
5353 rhs = cp_parser_simple_cast_expression (parser);
5355 /* Get another operator token. Look up its precedence to avoid
5356 building a useless (immediately popped) stack entry for common
5357 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5358 token = cp_lexer_peek_token (parser->lexer);
5359 lookahead_prec = TOKEN_PRECEDENCE (token);
5360 if (lookahead_prec > new_prec)
5362 /* ... and prepare to parse the RHS of the new, higher priority
5363 expression. Since precedence levels on the stack are
5364 monotonically increasing, we do not have to care about
5367 sp->tree_type = tree_type;
5372 new_prec = lookahead_prec;
5376 /* If the stack is not empty, we have parsed into LHS the right side
5377 (`4' in the example above) of an expression we had suspended.
5378 We can use the information on the stack to recover the LHS (`3')
5379 from the stack together with the tree code (`MULT_EXPR'), and
5380 the precedence of the higher level subexpression
5381 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5382 which will be used to actually build the additive expression. */
5385 tree_type = sp->tree_type;
5390 overloaded_p = false;
5391 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5393 /* If the binary operator required the use of an overloaded operator,
5394 then this expression cannot be an integral constant-expression.
5395 An overloaded operator can be used even if both operands are
5396 otherwise permissible in an integral constant-expression if at
5397 least one of the operands is of enumeration type. */
5400 && (cp_parser_non_integral_constant_expression
5401 (parser, "calls to overloaded operators")))
5402 return error_mark_node;
5409 /* Parse the `? expression : assignment-expression' part of a
5410 conditional-expression. The LOGICAL_OR_EXPR is the
5411 logical-or-expression that started the conditional-expression.
5412 Returns a representation of the entire conditional-expression.
5414 This routine is used by cp_parser_assignment_expression.
5416 ? expression : assignment-expression
5420 ? : assignment-expression */
5423 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5426 tree assignment_expr;
5428 /* Consume the `?' token. */
5429 cp_lexer_consume_token (parser->lexer);
5430 if (cp_parser_allow_gnu_extensions_p (parser)
5431 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5432 /* Implicit true clause. */
5435 /* Parse the expression. */
5436 expr = cp_parser_expression (parser);
5438 /* The next token should be a `:'. */
5439 cp_parser_require (parser, CPP_COLON, "`:'");
5440 /* Parse the assignment-expression. */
5441 assignment_expr = cp_parser_assignment_expression (parser);
5443 /* Build the conditional-expression. */
5444 return build_x_conditional_expr (logical_or_expr,
5449 /* Parse an assignment-expression.
5451 assignment-expression:
5452 conditional-expression
5453 logical-or-expression assignment-operator assignment_expression
5456 Returns a representation for the expression. */
5459 cp_parser_assignment_expression (cp_parser* parser)
5463 /* If the next token is the `throw' keyword, then we're looking at
5464 a throw-expression. */
5465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5466 expr = cp_parser_throw_expression (parser);
5467 /* Otherwise, it must be that we are looking at a
5468 logical-or-expression. */
5471 /* Parse the binary expressions (logical-or-expression). */
5472 expr = cp_parser_binary_expression (parser);
5473 /* If the next token is a `?' then we're actually looking at a
5474 conditional-expression. */
5475 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5476 return cp_parser_question_colon_clause (parser, expr);
5479 enum tree_code assignment_operator;
5481 /* If it's an assignment-operator, we're using the second
5484 = cp_parser_assignment_operator_opt (parser);
5485 if (assignment_operator != ERROR_MARK)
5489 /* Parse the right-hand side of the assignment. */
5490 rhs = cp_parser_assignment_expression (parser);
5491 /* An assignment may not appear in a
5492 constant-expression. */
5493 if (cp_parser_non_integral_constant_expression (parser,
5495 return error_mark_node;
5496 /* Build the assignment expression. */
5497 expr = build_x_modify_expr (expr,
5498 assignment_operator,
5507 /* Parse an (optional) assignment-operator.
5509 assignment-operator: one of
5510 = *= /= %= += -= >>= <<= &= ^= |=
5514 assignment-operator: one of
5517 If the next token is an assignment operator, the corresponding tree
5518 code is returned, and the token is consumed. For example, for
5519 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5520 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5521 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5522 operator, ERROR_MARK is returned. */
5524 static enum tree_code
5525 cp_parser_assignment_operator_opt (cp_parser* parser)
5530 /* Peek at the next toen. */
5531 token = cp_lexer_peek_token (parser->lexer);
5533 switch (token->type)
5544 op = TRUNC_DIV_EXPR;
5548 op = TRUNC_MOD_EXPR;
5588 /* Nothing else is an assignment operator. */
5592 /* If it was an assignment operator, consume it. */
5593 if (op != ERROR_MARK)
5594 cp_lexer_consume_token (parser->lexer);
5599 /* Parse an expression.
5602 assignment-expression
5603 expression , assignment-expression
5605 Returns a representation of the expression. */
5608 cp_parser_expression (cp_parser* parser)
5610 tree expression = NULL_TREE;
5614 tree assignment_expression;
5616 /* Parse the next assignment-expression. */
5617 assignment_expression
5618 = cp_parser_assignment_expression (parser);
5619 /* If this is the first assignment-expression, we can just
5622 expression = assignment_expression;
5624 expression = build_x_compound_expr (expression,
5625 assignment_expression);
5626 /* If the next token is not a comma, then we are done with the
5628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5630 /* Consume the `,'. */
5631 cp_lexer_consume_token (parser->lexer);
5632 /* A comma operator cannot appear in a constant-expression. */
5633 if (cp_parser_non_integral_constant_expression (parser,
5634 "a comma operator"))
5635 expression = error_mark_node;
5641 /* Parse a constant-expression.
5643 constant-expression:
5644 conditional-expression
5646 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5647 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5648 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5649 is false, NON_CONSTANT_P should be NULL. */
5652 cp_parser_constant_expression (cp_parser* parser,
5653 bool allow_non_constant_p,
5654 bool *non_constant_p)
5656 bool saved_integral_constant_expression_p;
5657 bool saved_allow_non_integral_constant_expression_p;
5658 bool saved_non_integral_constant_expression_p;
5661 /* It might seem that we could simply parse the
5662 conditional-expression, and then check to see if it were
5663 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5664 one that the compiler can figure out is constant, possibly after
5665 doing some simplifications or optimizations. The standard has a
5666 precise definition of constant-expression, and we must honor
5667 that, even though it is somewhat more restrictive.
5673 is not a legal declaration, because `(2, 3)' is not a
5674 constant-expression. The `,' operator is forbidden in a
5675 constant-expression. However, GCC's constant-folding machinery
5676 will fold this operation to an INTEGER_CST for `3'. */
5678 /* Save the old settings. */
5679 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5680 saved_allow_non_integral_constant_expression_p
5681 = parser->allow_non_integral_constant_expression_p;
5682 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5683 /* We are now parsing a constant-expression. */
5684 parser->integral_constant_expression_p = true;
5685 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5686 parser->non_integral_constant_expression_p = false;
5687 /* Although the grammar says "conditional-expression", we parse an
5688 "assignment-expression", which also permits "throw-expression"
5689 and the use of assignment operators. In the case that
5690 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5691 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5692 actually essential that we look for an assignment-expression.
5693 For example, cp_parser_initializer_clauses uses this function to
5694 determine whether a particular assignment-expression is in fact
5696 expression = cp_parser_assignment_expression (parser);
5697 /* Restore the old settings. */
5698 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5699 parser->allow_non_integral_constant_expression_p
5700 = saved_allow_non_integral_constant_expression_p;
5701 if (allow_non_constant_p)
5702 *non_constant_p = parser->non_integral_constant_expression_p;
5703 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5708 /* Parse __builtin_offsetof.
5710 offsetof-expression:
5711 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5713 offsetof-member-designator:
5715 | offsetof-member-designator "." id-expression
5716 | offsetof-member-designator "[" expression "]"
5720 cp_parser_builtin_offsetof (cp_parser *parser)
5722 int save_ice_p, save_non_ice_p;
5726 /* We're about to accept non-integral-constant things, but will
5727 definitely yield an integral constant expression. Save and
5728 restore these values around our local parsing. */
5729 save_ice_p = parser->integral_constant_expression_p;
5730 save_non_ice_p = parser->non_integral_constant_expression_p;
5732 /* Consume the "__builtin_offsetof" token. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Consume the opening `('. */
5735 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5736 /* Parse the type-id. */
5737 type = cp_parser_type_id (parser);
5738 /* Look for the `,'. */
5739 cp_parser_require (parser, CPP_COMMA, "`,'");
5741 /* Build the (type *)null that begins the traditional offsetof macro. */
5742 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5744 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5745 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5749 cp_token *token = cp_lexer_peek_token (parser->lexer);
5750 switch (token->type)
5752 case CPP_OPEN_SQUARE:
5753 /* offsetof-member-designator "[" expression "]" */
5754 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5758 /* offsetof-member-designator "." identifier */
5759 cp_lexer_consume_token (parser->lexer);
5760 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5764 case CPP_CLOSE_PAREN:
5765 /* Consume the ")" token. */
5766 cp_lexer_consume_token (parser->lexer);
5770 /* Error. We know the following require will fail, but
5771 that gives the proper error message. */
5772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5773 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5774 expr = error_mark_node;
5780 /* If we're processing a template, we can't finish the semantics yet.
5781 Otherwise we can fold the entire expression now. */
5782 if (processing_template_decl)
5783 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5785 expr = fold_offsetof (expr);
5788 parser->integral_constant_expression_p = save_ice_p;
5789 parser->non_integral_constant_expression_p = save_non_ice_p;
5794 /* Statements [gram.stmt.stmt] */
5796 /* Parse a statement.
5800 expression-statement
5805 declaration-statement
5809 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5813 location_t statement_location;
5815 /* There is no statement yet. */
5816 statement = NULL_TREE;
5817 /* Peek at the next token. */
5818 token = cp_lexer_peek_token (parser->lexer);
5819 /* Remember the location of the first token in the statement. */
5820 statement_location = token->location;
5821 /* If this is a keyword, then that will often determine what kind of
5822 statement we have. */
5823 if (token->type == CPP_KEYWORD)
5825 enum rid keyword = token->keyword;
5831 statement = cp_parser_labeled_statement (parser,
5837 statement = cp_parser_selection_statement (parser);
5843 statement = cp_parser_iteration_statement (parser);
5850 statement = cp_parser_jump_statement (parser);
5854 statement = cp_parser_try_block (parser);
5858 /* It might be a keyword like `int' that can start a
5859 declaration-statement. */
5863 else if (token->type == CPP_NAME)
5865 /* If the next token is a `:', then we are looking at a
5866 labeled-statement. */
5867 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5868 if (token->type == CPP_COLON)
5869 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5871 /* Anything that starts with a `{' must be a compound-statement. */
5872 else if (token->type == CPP_OPEN_BRACE)
5873 statement = cp_parser_compound_statement (parser, NULL, false);
5874 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5875 a statement all its own. */
5876 else if (token->type == CPP_PRAGMA)
5878 cp_lexer_handle_pragma (parser->lexer);
5882 /* Everything else must be a declaration-statement or an
5883 expression-statement. Try for the declaration-statement
5884 first, unless we are looking at a `;', in which case we know that
5885 we have an expression-statement. */
5888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5890 cp_parser_parse_tentatively (parser);
5891 /* Try to parse the declaration-statement. */
5892 cp_parser_declaration_statement (parser);
5893 /* If that worked, we're done. */
5894 if (cp_parser_parse_definitely (parser))
5897 /* Look for an expression-statement instead. */
5898 statement = cp_parser_expression_statement (parser, in_statement_expr);
5901 /* Set the line number for the statement. */
5902 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5903 SET_EXPR_LOCATION (statement, statement_location);
5906 /* Parse a labeled-statement.
5909 identifier : statement
5910 case constant-expression : statement
5916 case constant-expression ... constant-expression : statement
5918 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5919 For an ordinary label, returns a LABEL_EXPR. */
5922 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5925 tree statement = error_mark_node;
5927 /* The next token should be an identifier. */
5928 token = cp_lexer_peek_token (parser->lexer);
5929 if (token->type != CPP_NAME
5930 && token->type != CPP_KEYWORD)
5932 cp_parser_error (parser, "expected labeled-statement");
5933 return error_mark_node;
5936 switch (token->keyword)
5943 /* Consume the `case' token. */
5944 cp_lexer_consume_token (parser->lexer);
5945 /* Parse the constant-expression. */
5946 expr = cp_parser_constant_expression (parser,
5947 /*allow_non_constant_p=*/false,
5950 ellipsis = cp_lexer_peek_token (parser->lexer);
5951 if (ellipsis->type == CPP_ELLIPSIS)
5953 /* Consume the `...' token. */
5954 cp_lexer_consume_token (parser->lexer);
5956 cp_parser_constant_expression (parser,
5957 /*allow_non_constant_p=*/false,
5959 /* We don't need to emit warnings here, as the common code
5960 will do this for us. */
5963 expr_hi = NULL_TREE;
5965 if (!parser->in_switch_statement_p)
5966 error ("case label %qE not within a switch statement", expr);
5968 statement = finish_case_label (expr, expr_hi);
5973 /* Consume the `default' token. */
5974 cp_lexer_consume_token (parser->lexer);
5975 if (!parser->in_switch_statement_p)
5976 error ("case label not within a switch statement");
5978 statement = finish_case_label (NULL_TREE, NULL_TREE);
5982 /* Anything else must be an ordinary label. */
5983 statement = finish_label_stmt (cp_parser_identifier (parser));
5987 /* Require the `:' token. */
5988 cp_parser_require (parser, CPP_COLON, "`:'");
5989 /* Parse the labeled statement. */
5990 cp_parser_statement (parser, in_statement_expr);
5992 /* Return the label, in the case of a `case' or `default' label. */
5996 /* Parse an expression-statement.
5998 expression-statement:
6001 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6002 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6003 indicates whether this expression-statement is part of an
6004 expression statement. */
6007 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6009 tree statement = NULL_TREE;
6011 /* If the next token is a ';', then there is no expression
6013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6014 statement = cp_parser_expression (parser);
6016 /* Consume the final `;'. */
6017 cp_parser_consume_semicolon_at_end_of_statement (parser);
6019 if (in_statement_expr
6020 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6022 /* This is the final expression statement of a statement
6024 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6027 statement = finish_expr_stmt (statement);
6034 /* Parse a compound-statement.
6037 { statement-seq [opt] }
6039 Returns a tree representing the statement. */
6042 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6047 /* Consume the `{'. */
6048 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6049 return error_mark_node;
6050 /* Begin the compound-statement. */
6051 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6052 /* Parse an (optional) statement-seq. */
6053 cp_parser_statement_seq_opt (parser, in_statement_expr);
6054 /* Finish the compound-statement. */
6055 finish_compound_stmt (compound_stmt);
6056 /* Consume the `}'. */
6057 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6059 return compound_stmt;
6062 /* Parse an (optional) statement-seq.
6066 statement-seq [opt] statement */
6069 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6071 /* Scan statements until there aren't any more. */
6074 /* If we're looking at a `}', then we've run out of statements. */
6075 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6076 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6079 /* Parse the statement. */
6080 cp_parser_statement (parser, in_statement_expr);
6084 /* Parse a selection-statement.
6086 selection-statement:
6087 if ( condition ) statement
6088 if ( condition ) statement else statement
6089 switch ( condition ) statement
6091 Returns the new IF_STMT or SWITCH_STMT. */
6094 cp_parser_selection_statement (cp_parser* parser)
6099 /* Peek at the next token. */
6100 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6102 /* See what kind of keyword it is. */
6103 keyword = token->keyword;
6112 /* Look for the `('. */
6113 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6115 cp_parser_skip_to_end_of_statement (parser);
6116 return error_mark_node;
6119 /* Begin the selection-statement. */
6120 if (keyword == RID_IF)
6121 statement = begin_if_stmt ();
6123 statement = begin_switch_stmt ();
6125 /* Parse the condition. */
6126 condition = cp_parser_condition (parser);
6127 /* Look for the `)'. */
6128 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6129 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6130 /*consume_paren=*/true);
6132 if (keyword == RID_IF)
6134 /* Add the condition. */
6135 finish_if_stmt_cond (condition, statement);
6137 /* Parse the then-clause. */
6138 cp_parser_implicitly_scoped_statement (parser);
6139 finish_then_clause (statement);
6141 /* If the next token is `else', parse the else-clause. */
6142 if (cp_lexer_next_token_is_keyword (parser->lexer,
6145 /* Consume the `else' keyword. */
6146 cp_lexer_consume_token (parser->lexer);
6147 begin_else_clause (statement);
6148 /* Parse the else-clause. */
6149 cp_parser_implicitly_scoped_statement (parser);
6150 finish_else_clause (statement);
6153 /* Now we're all done with the if-statement. */
6154 finish_if_stmt (statement);
6158 bool in_switch_statement_p;
6160 /* Add the condition. */
6161 finish_switch_cond (condition, statement);
6163 /* Parse the body of the switch-statement. */
6164 in_switch_statement_p = parser->in_switch_statement_p;
6165 parser->in_switch_statement_p = true;
6166 cp_parser_implicitly_scoped_statement (parser);
6167 parser->in_switch_statement_p = in_switch_statement_p;
6169 /* Now we're all done with the switch-statement. */
6170 finish_switch_stmt (statement);
6178 cp_parser_error (parser, "expected selection-statement");
6179 return error_mark_node;
6183 /* Parse a condition.
6187 type-specifier-seq declarator = assignment-expression
6192 type-specifier-seq declarator asm-specification [opt]
6193 attributes [opt] = assignment-expression
6195 Returns the expression that should be tested. */
6198 cp_parser_condition (cp_parser* parser)
6200 cp_decl_specifier_seq type_specifiers;
6201 const char *saved_message;
6203 /* Try the declaration first. */
6204 cp_parser_parse_tentatively (parser);
6205 /* New types are not allowed in the type-specifier-seq for a
6207 saved_message = parser->type_definition_forbidden_message;
6208 parser->type_definition_forbidden_message
6209 = "types may not be defined in conditions";
6210 /* Parse the type-specifier-seq. */
6211 cp_parser_type_specifier_seq (parser, &type_specifiers);
6212 /* Restore the saved message. */
6213 parser->type_definition_forbidden_message = saved_message;
6214 /* If all is well, we might be looking at a declaration. */
6215 if (!cp_parser_error_occurred (parser))
6218 tree asm_specification;
6220 cp_declarator *declarator;
6221 tree initializer = NULL_TREE;
6223 /* Parse the declarator. */
6224 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6225 /*ctor_dtor_or_conv_p=*/NULL,
6226 /*parenthesized_p=*/NULL,
6227 /*member_p=*/false);
6228 /* Parse the attributes. */
6229 attributes = cp_parser_attributes_opt (parser);
6230 /* Parse the asm-specification. */
6231 asm_specification = cp_parser_asm_specification_opt (parser);
6232 /* If the next token is not an `=', then we might still be
6233 looking at an expression. For example:
6237 looks like a decl-specifier-seq and a declarator -- but then
6238 there is no `=', so this is an expression. */
6239 cp_parser_require (parser, CPP_EQ, "`='");
6240 /* If we did see an `=', then we are looking at a declaration
6242 if (cp_parser_parse_definitely (parser))
6246 /* Create the declaration. */
6247 decl = start_decl (declarator, &type_specifiers,
6248 /*initialized_p=*/true,
6249 attributes, /*prefix_attributes=*/NULL_TREE,
6251 /* Parse the assignment-expression. */
6252 initializer = cp_parser_assignment_expression (parser);
6254 /* Process the initializer. */
6255 cp_finish_decl (decl,
6258 LOOKUP_ONLYCONVERTING);
6261 pop_scope (DECL_CONTEXT (decl));
6263 return convert_from_reference (decl);
6266 /* If we didn't even get past the declarator successfully, we are
6267 definitely not looking at a declaration. */
6269 cp_parser_abort_tentative_parse (parser);
6271 /* Otherwise, we are looking at an expression. */
6272 return cp_parser_expression (parser);
6275 /* Parse an iteration-statement.
6277 iteration-statement:
6278 while ( condition ) statement
6279 do statement while ( expression ) ;
6280 for ( for-init-statement condition [opt] ; expression [opt] )
6283 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6286 cp_parser_iteration_statement (cp_parser* parser)
6291 bool in_iteration_statement_p;
6294 /* Peek at the next token. */
6295 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6297 return error_mark_node;
6299 /* Remember whether or not we are already within an iteration
6301 in_iteration_statement_p = parser->in_iteration_statement_p;
6303 /* See what kind of keyword it is. */
6304 keyword = token->keyword;
6311 /* Begin the while-statement. */
6312 statement = begin_while_stmt ();
6313 /* Look for the `('. */
6314 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6315 /* Parse the condition. */
6316 condition = cp_parser_condition (parser);
6317 finish_while_stmt_cond (condition, statement);
6318 /* Look for the `)'. */
6319 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6320 /* Parse the dependent statement. */
6321 parser->in_iteration_statement_p = true;
6322 cp_parser_already_scoped_statement (parser);
6323 parser->in_iteration_statement_p = in_iteration_statement_p;
6324 /* We're done with the while-statement. */
6325 finish_while_stmt (statement);
6333 /* Begin the do-statement. */
6334 statement = begin_do_stmt ();
6335 /* Parse the body of the do-statement. */
6336 parser->in_iteration_statement_p = true;
6337 cp_parser_implicitly_scoped_statement (parser);
6338 parser->in_iteration_statement_p = in_iteration_statement_p;
6339 finish_do_body (statement);
6340 /* Look for the `while' keyword. */
6341 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6342 /* Look for the `('. */
6343 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6344 /* Parse the expression. */
6345 expression = cp_parser_expression (parser);
6346 /* We're done with the do-statement. */
6347 finish_do_stmt (expression, statement);
6348 /* Look for the `)'. */
6349 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6350 /* Look for the `;'. */
6351 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6357 tree condition = NULL_TREE;
6358 tree expression = NULL_TREE;
6360 /* Begin the for-statement. */
6361 statement = begin_for_stmt ();
6362 /* Look for the `('. */
6363 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6364 /* Parse the initialization. */
6365 cp_parser_for_init_statement (parser);
6366 finish_for_init_stmt (statement);
6368 /* If there's a condition, process it. */
6369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6370 condition = cp_parser_condition (parser);
6371 finish_for_cond (condition, statement);
6372 /* Look for the `;'. */
6373 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6375 /* If there's an expression, process it. */
6376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6377 expression = cp_parser_expression (parser);
6378 finish_for_expr (expression, statement);
6379 /* Look for the `)'. */
6380 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6382 /* Parse the body of the for-statement. */
6383 parser->in_iteration_statement_p = true;
6384 cp_parser_already_scoped_statement (parser);
6385 parser->in_iteration_statement_p = in_iteration_statement_p;
6387 /* We're done with the for-statement. */
6388 finish_for_stmt (statement);
6393 cp_parser_error (parser, "expected iteration-statement");
6394 statement = error_mark_node;
6401 /* Parse a for-init-statement.
6404 expression-statement
6405 simple-declaration */
6408 cp_parser_for_init_statement (cp_parser* parser)
6410 /* If the next token is a `;', then we have an empty
6411 expression-statement. Grammatically, this is also a
6412 simple-declaration, but an invalid one, because it does not
6413 declare anything. Therefore, if we did not handle this case
6414 specially, we would issue an error message about an invalid
6416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6418 /* We're going to speculatively look for a declaration, falling back
6419 to an expression, if necessary. */
6420 cp_parser_parse_tentatively (parser);
6421 /* Parse the declaration. */
6422 cp_parser_simple_declaration (parser,
6423 /*function_definition_allowed_p=*/false);
6424 /* If the tentative parse failed, then we shall need to look for an
6425 expression-statement. */
6426 if (cp_parser_parse_definitely (parser))
6430 cp_parser_expression_statement (parser, false);
6433 /* Parse a jump-statement.
6438 return expression [opt] ;
6446 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6449 cp_parser_jump_statement (cp_parser* parser)
6451 tree statement = error_mark_node;
6455 /* Peek at the next token. */
6456 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6458 return error_mark_node;
6460 /* See what kind of keyword it is. */
6461 keyword = token->keyword;
6465 if (!parser->in_switch_statement_p
6466 && !parser->in_iteration_statement_p)
6468 error ("break statement not within loop or switch");
6469 statement = error_mark_node;
6472 statement = finish_break_stmt ();
6473 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6477 if (!parser->in_iteration_statement_p)
6479 error ("continue statement not within a loop");
6480 statement = error_mark_node;
6483 statement = finish_continue_stmt ();
6484 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6491 /* If the next token is a `;', then there is no
6493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6494 expr = cp_parser_expression (parser);
6497 /* Build the return-statement. */
6498 statement = finish_return_stmt (expr);
6499 /* Look for the final `;'. */
6500 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6505 /* Create the goto-statement. */
6506 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6508 /* Issue a warning about this use of a GNU extension. */
6510 pedwarn ("ISO C++ forbids computed gotos");
6511 /* Consume the '*' token. */
6512 cp_lexer_consume_token (parser->lexer);
6513 /* Parse the dependent expression. */
6514 finish_goto_stmt (cp_parser_expression (parser));
6517 finish_goto_stmt (cp_parser_identifier (parser));
6518 /* Look for the final `;'. */
6519 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6523 cp_parser_error (parser, "expected jump-statement");
6530 /* Parse a declaration-statement.
6532 declaration-statement:
6533 block-declaration */
6536 cp_parser_declaration_statement (cp_parser* parser)
6540 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6541 p = obstack_alloc (&declarator_obstack, 0);
6543 /* Parse the block-declaration. */
6544 cp_parser_block_declaration (parser, /*statement_p=*/true);
6546 /* Free any declarators allocated. */
6547 obstack_free (&declarator_obstack, p);
6549 /* Finish off the statement. */
6553 /* Some dependent statements (like `if (cond) statement'), are
6554 implicitly in their own scope. In other words, if the statement is
6555 a single statement (as opposed to a compound-statement), it is
6556 none-the-less treated as if it were enclosed in braces. Any
6557 declarations appearing in the dependent statement are out of scope
6558 after control passes that point. This function parses a statement,
6559 but ensures that is in its own scope, even if it is not a
6562 Returns the new statement. */
6565 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6569 /* If the token is not a `{', then we must take special action. */
6570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6572 /* Create a compound-statement. */
6573 statement = begin_compound_stmt (0);
6574 /* Parse the dependent-statement. */
6575 cp_parser_statement (parser, false);
6576 /* Finish the dummy compound-statement. */
6577 finish_compound_stmt (statement);
6579 /* Otherwise, we simply parse the statement directly. */
6581 statement = cp_parser_compound_statement (parser, NULL, false);
6583 /* Return the statement. */
6587 /* For some dependent statements (like `while (cond) statement'), we
6588 have already created a scope. Therefore, even if the dependent
6589 statement is a compound-statement, we do not want to create another
6593 cp_parser_already_scoped_statement (cp_parser* parser)
6595 /* If the token is a `{', then we must take special action. */
6596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6597 cp_parser_statement (parser, false);
6600 /* Avoid calling cp_parser_compound_statement, so that we
6601 don't create a new scope. Do everything else by hand. */
6602 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6603 cp_parser_statement_seq_opt (parser, false);
6604 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6608 /* Declarations [gram.dcl.dcl] */
6610 /* Parse an optional declaration-sequence.
6614 declaration-seq declaration */
6617 cp_parser_declaration_seq_opt (cp_parser* parser)
6623 token = cp_lexer_peek_token (parser->lexer);
6625 if (token->type == CPP_CLOSE_BRACE
6626 || token->type == CPP_EOF)
6629 if (token->type == CPP_SEMICOLON)
6631 /* A declaration consisting of a single semicolon is
6632 invalid. Allow it unless we're being pedantic. */
6633 cp_lexer_consume_token (parser->lexer);
6634 if (pedantic && !in_system_header)
6635 pedwarn ("extra %<;%>");
6639 /* If we're entering or exiting a region that's implicitly
6640 extern "C", modify the lang context appropriately. */
6641 if (!parser->implicit_extern_c && token->implicit_extern_c)
6643 push_lang_context (lang_name_c);
6644 parser->implicit_extern_c = true;
6646 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6648 pop_lang_context ();
6649 parser->implicit_extern_c = false;
6652 if (token->type == CPP_PRAGMA)
6654 /* A top-level declaration can consist solely of a #pragma.
6655 A nested declaration cannot, so this is done here and not
6656 in cp_parser_declaration. (A #pragma at block scope is
6657 handled in cp_parser_statement.) */
6658 cp_lexer_handle_pragma (parser->lexer);
6662 /* Parse the declaration itself. */
6663 cp_parser_declaration (parser);
6667 /* Parse a declaration.
6672 template-declaration
6673 explicit-instantiation
6674 explicit-specialization
6675 linkage-specification
6676 namespace-definition
6681 __extension__ declaration */
6684 cp_parser_declaration (cp_parser* parser)
6691 /* Check for the `__extension__' keyword. */
6692 if (cp_parser_extension_opt (parser, &saved_pedantic))
6694 /* Parse the qualified declaration. */
6695 cp_parser_declaration (parser);
6696 /* Restore the PEDANTIC flag. */
6697 pedantic = saved_pedantic;
6702 /* Try to figure out what kind of declaration is present. */
6703 token1 = *cp_lexer_peek_token (parser->lexer);
6705 if (token1.type != CPP_EOF)
6706 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6708 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6709 p = obstack_alloc (&declarator_obstack, 0);
6711 /* If the next token is `extern' and the following token is a string
6712 literal, then we have a linkage specification. */
6713 if (token1.keyword == RID_EXTERN
6714 && cp_parser_is_string_literal (&token2))
6715 cp_parser_linkage_specification (parser);
6716 /* If the next token is `template', then we have either a template
6717 declaration, an explicit instantiation, or an explicit
6719 else if (token1.keyword == RID_TEMPLATE)
6721 /* `template <>' indicates a template specialization. */
6722 if (token2.type == CPP_LESS
6723 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6724 cp_parser_explicit_specialization (parser);
6725 /* `template <' indicates a template declaration. */
6726 else if (token2.type == CPP_LESS)
6727 cp_parser_template_declaration (parser, /*member_p=*/false);
6728 /* Anything else must be an explicit instantiation. */
6730 cp_parser_explicit_instantiation (parser);
6732 /* If the next token is `export', then we have a template
6734 else if (token1.keyword == RID_EXPORT)
6735 cp_parser_template_declaration (parser, /*member_p=*/false);
6736 /* If the next token is `extern', 'static' or 'inline' and the one
6737 after that is `template', we have a GNU extended explicit
6738 instantiation directive. */
6739 else if (cp_parser_allow_gnu_extensions_p (parser)
6740 && (token1.keyword == RID_EXTERN
6741 || token1.keyword == RID_STATIC
6742 || token1.keyword == RID_INLINE)
6743 && token2.keyword == RID_TEMPLATE)
6744 cp_parser_explicit_instantiation (parser);
6745 /* If the next token is `namespace', check for a named or unnamed
6746 namespace definition. */
6747 else if (token1.keyword == RID_NAMESPACE
6748 && (/* A named namespace definition. */
6749 (token2.type == CPP_NAME
6750 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6752 /* An unnamed namespace definition. */
6753 || token2.type == CPP_OPEN_BRACE))
6754 cp_parser_namespace_definition (parser);
6755 /* We must have either a block declaration or a function
6758 /* Try to parse a block-declaration, or a function-definition. */
6759 cp_parser_block_declaration (parser, /*statement_p=*/false);
6761 /* Free any declarators allocated. */
6762 obstack_free (&declarator_obstack, p);
6765 /* Parse a block-declaration.
6770 namespace-alias-definition
6777 __extension__ block-declaration
6780 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6781 part of a declaration-statement. */
6784 cp_parser_block_declaration (cp_parser *parser,
6790 /* Check for the `__extension__' keyword. */
6791 if (cp_parser_extension_opt (parser, &saved_pedantic))
6793 /* Parse the qualified declaration. */
6794 cp_parser_block_declaration (parser, statement_p);
6795 /* Restore the PEDANTIC flag. */
6796 pedantic = saved_pedantic;
6801 /* Peek at the next token to figure out which kind of declaration is
6803 token1 = cp_lexer_peek_token (parser->lexer);
6805 /* If the next keyword is `asm', we have an asm-definition. */
6806 if (token1->keyword == RID_ASM)
6809 cp_parser_commit_to_tentative_parse (parser);
6810 cp_parser_asm_definition (parser);
6812 /* If the next keyword is `namespace', we have a
6813 namespace-alias-definition. */
6814 else if (token1->keyword == RID_NAMESPACE)
6815 cp_parser_namespace_alias_definition (parser);
6816 /* If the next keyword is `using', we have either a
6817 using-declaration or a using-directive. */
6818 else if (token1->keyword == RID_USING)
6823 cp_parser_commit_to_tentative_parse (parser);
6824 /* If the token after `using' is `namespace', then we have a
6826 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6827 if (token2->keyword == RID_NAMESPACE)
6828 cp_parser_using_directive (parser);
6829 /* Otherwise, it's a using-declaration. */
6831 cp_parser_using_declaration (parser);
6833 /* If the next keyword is `__label__' we have a label declaration. */
6834 else if (token1->keyword == RID_LABEL)
6837 cp_parser_commit_to_tentative_parse (parser);
6838 cp_parser_label_declaration (parser);
6840 /* Anything else must be a simple-declaration. */
6842 cp_parser_simple_declaration (parser, !statement_p);
6845 /* Parse a simple-declaration.
6848 decl-specifier-seq [opt] init-declarator-list [opt] ;
6850 init-declarator-list:
6852 init-declarator-list , init-declarator
6854 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6855 function-definition as a simple-declaration. */
6858 cp_parser_simple_declaration (cp_parser* parser,
6859 bool function_definition_allowed_p)
6861 cp_decl_specifier_seq decl_specifiers;
6862 int declares_class_or_enum;
6863 bool saw_declarator;
6865 /* Defer access checks until we know what is being declared; the
6866 checks for names appearing in the decl-specifier-seq should be
6867 done as if we were in the scope of the thing being declared. */
6868 push_deferring_access_checks (dk_deferred);
6870 /* Parse the decl-specifier-seq. We have to keep track of whether
6871 or not the decl-specifier-seq declares a named class or
6872 enumeration type, since that is the only case in which the
6873 init-declarator-list is allowed to be empty.
6877 In a simple-declaration, the optional init-declarator-list can be
6878 omitted only when declaring a class or enumeration, that is when
6879 the decl-specifier-seq contains either a class-specifier, an
6880 elaborated-type-specifier, or an enum-specifier. */
6881 cp_parser_decl_specifier_seq (parser,
6882 CP_PARSER_FLAGS_OPTIONAL,
6884 &declares_class_or_enum);
6885 /* We no longer need to defer access checks. */
6886 stop_deferring_access_checks ();
6888 /* In a block scope, a valid declaration must always have a
6889 decl-specifier-seq. By not trying to parse declarators, we can
6890 resolve the declaration/expression ambiguity more quickly. */
6891 if (!function_definition_allowed_p
6892 && !decl_specifiers.any_specifiers_p)
6894 cp_parser_error (parser, "expected declaration");
6898 /* If the next two tokens are both identifiers, the code is
6899 erroneous. The usual cause of this situation is code like:
6903 where "T" should name a type -- but does not. */
6904 if (!decl_specifiers.type
6905 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6907 /* If parsing tentatively, we should commit; we really are
6908 looking at a declaration. */
6909 cp_parser_commit_to_tentative_parse (parser);
6914 /* If we have seen at least one decl-specifier, and the next token
6915 is not a parenthesis, then we must be looking at a declaration.
6916 (After "int (" we might be looking at a functional cast.) */
6917 if (decl_specifiers.any_specifiers_p
6918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6919 cp_parser_commit_to_tentative_parse (parser);
6921 /* Keep going until we hit the `;' at the end of the simple
6923 saw_declarator = false;
6924 while (cp_lexer_next_token_is_not (parser->lexer,
6928 bool function_definition_p;
6931 saw_declarator = true;
6932 /* Parse the init-declarator. */
6933 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6934 function_definition_allowed_p,
6936 declares_class_or_enum,
6937 &function_definition_p);
6938 /* If an error occurred while parsing tentatively, exit quickly.
6939 (That usually happens when in the body of a function; each
6940 statement is treated as a declaration-statement until proven
6942 if (cp_parser_error_occurred (parser))
6944 /* Handle function definitions specially. */
6945 if (function_definition_p)
6947 /* If the next token is a `,', then we are probably
6948 processing something like:
6952 which is erroneous. */
6953 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6954 error ("mixing declarations and function-definitions is forbidden");
6955 /* Otherwise, we're done with the list of declarators. */
6958 pop_deferring_access_checks ();
6962 /* The next token should be either a `,' or a `;'. */
6963 token = cp_lexer_peek_token (parser->lexer);
6964 /* If it's a `,', there are more declarators to come. */
6965 if (token->type == CPP_COMMA)
6966 cp_lexer_consume_token (parser->lexer);
6967 /* If it's a `;', we are done. */
6968 else if (token->type == CPP_SEMICOLON)
6970 /* Anything else is an error. */
6973 /* If we have already issued an error message we don't need
6974 to issue another one. */
6975 if (decl != error_mark_node
6976 || (cp_parser_parsing_tentatively (parser)
6977 && !cp_parser_committed_to_tentative_parse (parser)))
6978 cp_parser_error (parser, "expected %<,%> or %<;%>");
6979 /* Skip tokens until we reach the end of the statement. */
6980 cp_parser_skip_to_end_of_statement (parser);
6981 /* If the next token is now a `;', consume it. */
6982 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6983 cp_lexer_consume_token (parser->lexer);
6986 /* After the first time around, a function-definition is not
6987 allowed -- even if it was OK at first. For example:
6992 function_definition_allowed_p = false;
6995 /* Issue an error message if no declarators are present, and the
6996 decl-specifier-seq does not itself declare a class or
6998 if (!saw_declarator)
7000 if (cp_parser_declares_only_class_p (parser))
7001 shadow_tag (&decl_specifiers);
7002 /* Perform any deferred access checks. */
7003 perform_deferred_access_checks ();
7006 /* Consume the `;'. */
7007 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7010 pop_deferring_access_checks ();
7013 /* Parse a decl-specifier-seq.
7016 decl-specifier-seq [opt] decl-specifier
7019 storage-class-specifier
7030 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7032 The parser flags FLAGS is used to control type-specifier parsing.
7034 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7037 1: one of the decl-specifiers is an elaborated-type-specifier
7038 (i.e., a type declaration)
7039 2: one of the decl-specifiers is an enum-specifier or a
7040 class-specifier (i.e., a type definition)
7045 cp_parser_decl_specifier_seq (cp_parser* parser,
7046 cp_parser_flags flags,
7047 cp_decl_specifier_seq *decl_specs,
7048 int* declares_class_or_enum)
7050 bool constructor_possible_p = !parser->in_declarator_p;
7052 /* Clear DECL_SPECS. */
7053 clear_decl_specs (decl_specs);
7055 /* Assume no class or enumeration type is declared. */
7056 *declares_class_or_enum = 0;
7058 /* Keep reading specifiers until there are no more to read. */
7062 bool found_decl_spec;
7065 /* Peek at the next token. */
7066 token = cp_lexer_peek_token (parser->lexer);
7067 /* Handle attributes. */
7068 if (token->keyword == RID_ATTRIBUTE)
7070 /* Parse the attributes. */
7071 decl_specs->attributes
7072 = chainon (decl_specs->attributes,
7073 cp_parser_attributes_opt (parser));
7076 /* Assume we will find a decl-specifier keyword. */
7077 found_decl_spec = true;
7078 /* If the next token is an appropriate keyword, we can simply
7079 add it to the list. */
7080 switch (token->keyword)
7085 if (decl_specs->specs[(int) ds_friend]++)
7086 error ("duplicate %<friend%>");
7087 /* Consume the token. */
7088 cp_lexer_consume_token (parser->lexer);
7091 /* function-specifier:
7098 cp_parser_function_specifier_opt (parser, decl_specs);
7104 ++decl_specs->specs[(int) ds_typedef];
7105 /* Consume the token. */
7106 cp_lexer_consume_token (parser->lexer);
7107 /* A constructor declarator cannot appear in a typedef. */
7108 constructor_possible_p = false;
7109 /* The "typedef" keyword can only occur in a declaration; we
7110 may as well commit at this point. */
7111 cp_parser_commit_to_tentative_parse (parser);
7114 /* storage-class-specifier:
7124 /* Consume the token. */
7125 cp_lexer_consume_token (parser->lexer);
7126 cp_parser_set_storage_class (decl_specs, sc_auto);
7129 /* Consume the token. */
7130 cp_lexer_consume_token (parser->lexer);
7131 cp_parser_set_storage_class (decl_specs, sc_register);
7134 /* Consume the token. */
7135 cp_lexer_consume_token (parser->lexer);
7136 if (decl_specs->specs[(int) ds_thread])
7138 error ("%<__thread%> before %<static%>");
7139 decl_specs->specs[(int) ds_thread] = 0;
7141 cp_parser_set_storage_class (decl_specs, sc_static);
7144 /* Consume the token. */
7145 cp_lexer_consume_token (parser->lexer);
7146 if (decl_specs->specs[(int) ds_thread])
7148 error ("%<__thread%> before %<extern%>");
7149 decl_specs->specs[(int) ds_thread] = 0;
7151 cp_parser_set_storage_class (decl_specs, sc_extern);
7154 /* Consume the token. */
7155 cp_lexer_consume_token (parser->lexer);
7156 cp_parser_set_storage_class (decl_specs, sc_mutable);
7159 /* Consume the token. */
7160 cp_lexer_consume_token (parser->lexer);
7161 ++decl_specs->specs[(int) ds_thread];
7165 /* We did not yet find a decl-specifier yet. */
7166 found_decl_spec = false;
7170 /* Constructors are a special case. The `S' in `S()' is not a
7171 decl-specifier; it is the beginning of the declarator. */
7174 && constructor_possible_p
7175 && (cp_parser_constructor_declarator_p
7176 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7178 /* If we don't have a DECL_SPEC yet, then we must be looking at
7179 a type-specifier. */
7180 if (!found_decl_spec && !constructor_p)
7182 int decl_spec_declares_class_or_enum;
7183 bool is_cv_qualifier;
7187 = cp_parser_type_specifier (parser, flags,
7189 /*is_declaration=*/true,
7190 &decl_spec_declares_class_or_enum,
7193 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7195 /* If this type-specifier referenced a user-defined type
7196 (a typedef, class-name, etc.), then we can't allow any
7197 more such type-specifiers henceforth.
7201 The longest sequence of decl-specifiers that could
7202 possibly be a type name is taken as the
7203 decl-specifier-seq of a declaration. The sequence shall
7204 be self-consistent as described below.
7208 As a general rule, at most one type-specifier is allowed
7209 in the complete decl-specifier-seq of a declaration. The
7210 only exceptions are the following:
7212 -- const or volatile can be combined with any other
7215 -- signed or unsigned can be combined with char, long,
7223 void g (const int Pc);
7225 Here, Pc is *not* part of the decl-specifier seq; it's
7226 the declarator. Therefore, once we see a type-specifier
7227 (other than a cv-qualifier), we forbid any additional
7228 user-defined types. We *do* still allow things like `int
7229 int' to be considered a decl-specifier-seq, and issue the
7230 error message later. */
7231 if (type_spec && !is_cv_qualifier)
7232 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7233 /* A constructor declarator cannot follow a type-specifier. */
7236 constructor_possible_p = false;
7237 found_decl_spec = true;
7241 /* If we still do not have a DECL_SPEC, then there are no more
7243 if (!found_decl_spec)
7246 decl_specs->any_specifiers_p = true;
7247 /* After we see one decl-specifier, further decl-specifiers are
7249 flags |= CP_PARSER_FLAGS_OPTIONAL;
7252 /* Don't allow a friend specifier with a class definition. */
7253 if (decl_specs->specs[(int) ds_friend] != 0
7254 && (*declares_class_or_enum & 2))
7255 error ("class definition may not be declared a friend");
7258 /* Parse an (optional) storage-class-specifier.
7260 storage-class-specifier:
7269 storage-class-specifier:
7272 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7275 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7277 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7285 /* Consume the token. */
7286 return cp_lexer_consume_token (parser->lexer)->value;
7293 /* Parse an (optional) function-specifier.
7300 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7301 Updates DECL_SPECS, if it is non-NULL. */
7304 cp_parser_function_specifier_opt (cp_parser* parser,
7305 cp_decl_specifier_seq *decl_specs)
7307 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7311 ++decl_specs->specs[(int) ds_inline];
7316 ++decl_specs->specs[(int) ds_virtual];
7321 ++decl_specs->specs[(int) ds_explicit];
7328 /* Consume the token. */
7329 return cp_lexer_consume_token (parser->lexer)->value;
7332 /* Parse a linkage-specification.
7334 linkage-specification:
7335 extern string-literal { declaration-seq [opt] }
7336 extern string-literal declaration */
7339 cp_parser_linkage_specification (cp_parser* parser)
7343 /* Look for the `extern' keyword. */
7344 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7346 /* Look for the string-literal. */
7347 linkage = cp_parser_string_literal (parser, false, false);
7349 /* Transform the literal into an identifier. If the literal is a
7350 wide-character string, or contains embedded NULs, then we can't
7351 handle it as the user wants. */
7352 if (strlen (TREE_STRING_POINTER (linkage))
7353 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7355 cp_parser_error (parser, "invalid linkage-specification");
7356 /* Assume C++ linkage. */
7357 linkage = lang_name_cplusplus;
7360 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7362 /* We're now using the new linkage. */
7363 push_lang_context (linkage);
7365 /* If the next token is a `{', then we're using the first
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7369 /* Consume the `{' token. */
7370 cp_lexer_consume_token (parser->lexer);
7371 /* Parse the declarations. */
7372 cp_parser_declaration_seq_opt (parser);
7373 /* Look for the closing `}'. */
7374 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7376 /* Otherwise, there's just one declaration. */
7379 bool saved_in_unbraced_linkage_specification_p;
7381 saved_in_unbraced_linkage_specification_p
7382 = parser->in_unbraced_linkage_specification_p;
7383 parser->in_unbraced_linkage_specification_p = true;
7384 have_extern_spec = true;
7385 cp_parser_declaration (parser);
7386 have_extern_spec = false;
7387 parser->in_unbraced_linkage_specification_p
7388 = saved_in_unbraced_linkage_specification_p;
7391 /* We're done with the linkage-specification. */
7392 pop_lang_context ();
7395 /* Special member functions [gram.special] */
7397 /* Parse a conversion-function-id.
7399 conversion-function-id:
7400 operator conversion-type-id
7402 Returns an IDENTIFIER_NODE representing the operator. */
7405 cp_parser_conversion_function_id (cp_parser* parser)
7409 tree saved_qualifying_scope;
7410 tree saved_object_scope;
7413 /* Look for the `operator' token. */
7414 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7415 return error_mark_node;
7416 /* When we parse the conversion-type-id, the current scope will be
7417 reset. However, we need that information in able to look up the
7418 conversion function later, so we save it here. */
7419 saved_scope = parser->scope;
7420 saved_qualifying_scope = parser->qualifying_scope;
7421 saved_object_scope = parser->object_scope;
7422 /* We must enter the scope of the class so that the names of
7423 entities declared within the class are available in the
7424 conversion-type-id. For example, consider:
7431 S::operator I() { ... }
7433 In order to see that `I' is a type-name in the definition, we
7434 must be in the scope of `S'. */
7436 pop_p = push_scope (saved_scope);
7437 /* Parse the conversion-type-id. */
7438 type = cp_parser_conversion_type_id (parser);
7439 /* Leave the scope of the class, if any. */
7441 pop_scope (saved_scope);
7442 /* Restore the saved scope. */
7443 parser->scope = saved_scope;
7444 parser->qualifying_scope = saved_qualifying_scope;
7445 parser->object_scope = saved_object_scope;
7446 /* If the TYPE is invalid, indicate failure. */
7447 if (type == error_mark_node)
7448 return error_mark_node;
7449 return mangle_conv_op_name_for_type (type);
7452 /* Parse a conversion-type-id:
7455 type-specifier-seq conversion-declarator [opt]
7457 Returns the TYPE specified. */
7460 cp_parser_conversion_type_id (cp_parser* parser)
7463 cp_decl_specifier_seq type_specifiers;
7464 cp_declarator *declarator;
7465 tree type_specified;
7467 /* Parse the attributes. */
7468 attributes = cp_parser_attributes_opt (parser);
7469 /* Parse the type-specifiers. */
7470 cp_parser_type_specifier_seq (parser, &type_specifiers);
7471 /* If that didn't work, stop. */
7472 if (type_specifiers.type == error_mark_node)
7473 return error_mark_node;
7474 /* Parse the conversion-declarator. */
7475 declarator = cp_parser_conversion_declarator_opt (parser);
7477 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7478 /*initialized=*/0, &attributes);
7480 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7481 return type_specified;
7484 /* Parse an (optional) conversion-declarator.
7486 conversion-declarator:
7487 ptr-operator conversion-declarator [opt]
7491 static cp_declarator *
7492 cp_parser_conversion_declarator_opt (cp_parser* parser)
7494 enum tree_code code;
7496 cp_cv_quals cv_quals;
7498 /* We don't know if there's a ptr-operator next, or not. */
7499 cp_parser_parse_tentatively (parser);
7500 /* Try the ptr-operator. */
7501 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7502 /* If it worked, look for more conversion-declarators. */
7503 if (cp_parser_parse_definitely (parser))
7505 cp_declarator *declarator;
7507 /* Parse another optional declarator. */
7508 declarator = cp_parser_conversion_declarator_opt (parser);
7510 /* Create the representation of the declarator. */
7512 declarator = make_ptrmem_declarator (cv_quals, class_type,
7514 else if (code == INDIRECT_REF)
7515 declarator = make_pointer_declarator (cv_quals, declarator);
7517 declarator = make_reference_declarator (cv_quals, declarator);
7525 /* Parse an (optional) ctor-initializer.
7528 : mem-initializer-list
7530 Returns TRUE iff the ctor-initializer was actually present. */
7533 cp_parser_ctor_initializer_opt (cp_parser* parser)
7535 /* If the next token is not a `:', then there is no
7536 ctor-initializer. */
7537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7539 /* Do default initialization of any bases and members. */
7540 if (DECL_CONSTRUCTOR_P (current_function_decl))
7541 finish_mem_initializers (NULL_TREE);
7546 /* Consume the `:' token. */
7547 cp_lexer_consume_token (parser->lexer);
7548 /* And the mem-initializer-list. */
7549 cp_parser_mem_initializer_list (parser);
7554 /* Parse a mem-initializer-list.
7556 mem-initializer-list:
7558 mem-initializer , mem-initializer-list */
7561 cp_parser_mem_initializer_list (cp_parser* parser)
7563 tree mem_initializer_list = NULL_TREE;
7565 /* Let the semantic analysis code know that we are starting the
7566 mem-initializer-list. */
7567 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7568 error ("only constructors take base initializers");
7570 /* Loop through the list. */
7573 tree mem_initializer;
7575 /* Parse the mem-initializer. */
7576 mem_initializer = cp_parser_mem_initializer (parser);
7577 /* Add it to the list, unless it was erroneous. */
7578 if (mem_initializer)
7580 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7581 mem_initializer_list = mem_initializer;
7583 /* If the next token is not a `,', we're done. */
7584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7586 /* Consume the `,' token. */
7587 cp_lexer_consume_token (parser->lexer);
7590 /* Perform semantic analysis. */
7591 if (DECL_CONSTRUCTOR_P (current_function_decl))
7592 finish_mem_initializers (mem_initializer_list);
7595 /* Parse a mem-initializer.
7598 mem-initializer-id ( expression-list [opt] )
7603 ( expression-list [opt] )
7605 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7606 class) or FIELD_DECL (for a non-static data member) to initialize;
7607 the TREE_VALUE is the expression-list. */
7610 cp_parser_mem_initializer (cp_parser* parser)
7612 tree mem_initializer_id;
7613 tree expression_list;
7616 /* Find out what is being initialized. */
7617 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7619 pedwarn ("anachronistic old-style base class initializer");
7620 mem_initializer_id = NULL_TREE;
7623 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7624 member = expand_member_init (mem_initializer_id);
7625 if (member && !DECL_P (member))
7626 in_base_initializer = 1;
7629 = cp_parser_parenthesized_expression_list (parser, false,
7630 /*non_constant_p=*/NULL);
7631 if (!expression_list)
7632 expression_list = void_type_node;
7634 in_base_initializer = 0;
7636 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7639 /* Parse a mem-initializer-id.
7642 :: [opt] nested-name-specifier [opt] class-name
7645 Returns a TYPE indicating the class to be initializer for the first
7646 production. Returns an IDENTIFIER_NODE indicating the data member
7647 to be initialized for the second production. */
7650 cp_parser_mem_initializer_id (cp_parser* parser)
7652 bool global_scope_p;
7653 bool nested_name_specifier_p;
7654 bool template_p = false;
7657 /* `typename' is not allowed in this context ([temp.res]). */
7658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7660 error ("keyword %<typename%> not allowed in this context (a qualified "
7661 "member initializer is implicitly a type)");
7662 cp_lexer_consume_token (parser->lexer);
7664 /* Look for the optional `::' operator. */
7666 = (cp_parser_global_scope_opt (parser,
7667 /*current_scope_valid_p=*/false)
7669 /* Look for the optional nested-name-specifier. The simplest way to
7674 The keyword `typename' is not permitted in a base-specifier or
7675 mem-initializer; in these contexts a qualified name that
7676 depends on a template-parameter is implicitly assumed to be a
7679 is to assume that we have seen the `typename' keyword at this
7681 nested_name_specifier_p
7682 = (cp_parser_nested_name_specifier_opt (parser,
7683 /*typename_keyword_p=*/true,
7684 /*check_dependency_p=*/true,
7686 /*is_declaration=*/true)
7688 if (nested_name_specifier_p)
7689 template_p = cp_parser_optional_template_keyword (parser);
7690 /* If there is a `::' operator or a nested-name-specifier, then we
7691 are definitely looking for a class-name. */
7692 if (global_scope_p || nested_name_specifier_p)
7693 return cp_parser_class_name (parser,
7694 /*typename_keyword_p=*/true,
7695 /*template_keyword_p=*/template_p,
7697 /*check_dependency_p=*/true,
7698 /*class_head_p=*/false,
7699 /*is_declaration=*/true);
7700 /* Otherwise, we could also be looking for an ordinary identifier. */
7701 cp_parser_parse_tentatively (parser);
7702 /* Try a class-name. */
7703 id = cp_parser_class_name (parser,
7704 /*typename_keyword_p=*/true,
7705 /*template_keyword_p=*/false,
7707 /*check_dependency_p=*/true,
7708 /*class_head_p=*/false,
7709 /*is_declaration=*/true);
7710 /* If we found one, we're done. */
7711 if (cp_parser_parse_definitely (parser))
7713 /* Otherwise, look for an ordinary identifier. */
7714 return cp_parser_identifier (parser);
7717 /* Overloading [gram.over] */
7719 /* Parse an operator-function-id.
7721 operator-function-id:
7724 Returns an IDENTIFIER_NODE for the operator which is a
7725 human-readable spelling of the identifier, e.g., `operator +'. */
7728 cp_parser_operator_function_id (cp_parser* parser)
7730 /* Look for the `operator' keyword. */
7731 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7732 return error_mark_node;
7733 /* And then the name of the operator itself. */
7734 return cp_parser_operator (parser);
7737 /* Parse an operator.
7740 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7741 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7742 || ++ -- , ->* -> () []
7749 Returns an IDENTIFIER_NODE for the operator which is a
7750 human-readable spelling of the identifier, e.g., `operator +'. */
7753 cp_parser_operator (cp_parser* parser)
7755 tree id = NULL_TREE;
7758 /* Peek at the next token. */
7759 token = cp_lexer_peek_token (parser->lexer);
7760 /* Figure out which operator we have. */
7761 switch (token->type)
7767 /* The keyword should be either `new' or `delete'. */
7768 if (token->keyword == RID_NEW)
7770 else if (token->keyword == RID_DELETE)
7775 /* Consume the `new' or `delete' token. */
7776 cp_lexer_consume_token (parser->lexer);
7778 /* Peek at the next token. */
7779 token = cp_lexer_peek_token (parser->lexer);
7780 /* If it's a `[' token then this is the array variant of the
7782 if (token->type == CPP_OPEN_SQUARE)
7784 /* Consume the `[' token. */
7785 cp_lexer_consume_token (parser->lexer);
7786 /* Look for the `]' token. */
7787 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7788 id = ansi_opname (op == NEW_EXPR
7789 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7791 /* Otherwise, we have the non-array variant. */
7793 id = ansi_opname (op);
7799 id = ansi_opname (PLUS_EXPR);
7803 id = ansi_opname (MINUS_EXPR);
7807 id = ansi_opname (MULT_EXPR);
7811 id = ansi_opname (TRUNC_DIV_EXPR);
7815 id = ansi_opname (TRUNC_MOD_EXPR);
7819 id = ansi_opname (BIT_XOR_EXPR);
7823 id = ansi_opname (BIT_AND_EXPR);
7827 id = ansi_opname (BIT_IOR_EXPR);
7831 id = ansi_opname (BIT_NOT_EXPR);
7835 id = ansi_opname (TRUTH_NOT_EXPR);
7839 id = ansi_assopname (NOP_EXPR);
7843 id = ansi_opname (LT_EXPR);
7847 id = ansi_opname (GT_EXPR);
7851 id = ansi_assopname (PLUS_EXPR);
7855 id = ansi_assopname (MINUS_EXPR);
7859 id = ansi_assopname (MULT_EXPR);
7863 id = ansi_assopname (TRUNC_DIV_EXPR);
7867 id = ansi_assopname (TRUNC_MOD_EXPR);
7871 id = ansi_assopname (BIT_XOR_EXPR);
7875 id = ansi_assopname (BIT_AND_EXPR);
7879 id = ansi_assopname (BIT_IOR_EXPR);
7883 id = ansi_opname (LSHIFT_EXPR);
7887 id = ansi_opname (RSHIFT_EXPR);
7891 id = ansi_assopname (LSHIFT_EXPR);
7895 id = ansi_assopname (RSHIFT_EXPR);
7899 id = ansi_opname (EQ_EXPR);
7903 id = ansi_opname (NE_EXPR);
7907 id = ansi_opname (LE_EXPR);
7910 case CPP_GREATER_EQ:
7911 id = ansi_opname (GE_EXPR);
7915 id = ansi_opname (TRUTH_ANDIF_EXPR);
7919 id = ansi_opname (TRUTH_ORIF_EXPR);
7923 id = ansi_opname (POSTINCREMENT_EXPR);
7926 case CPP_MINUS_MINUS:
7927 id = ansi_opname (PREDECREMENT_EXPR);
7931 id = ansi_opname (COMPOUND_EXPR);
7934 case CPP_DEREF_STAR:
7935 id = ansi_opname (MEMBER_REF);
7939 id = ansi_opname (COMPONENT_REF);
7942 case CPP_OPEN_PAREN:
7943 /* Consume the `('. */
7944 cp_lexer_consume_token (parser->lexer);
7945 /* Look for the matching `)'. */
7946 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7947 return ansi_opname (CALL_EXPR);
7949 case CPP_OPEN_SQUARE:
7950 /* Consume the `['. */
7951 cp_lexer_consume_token (parser->lexer);
7952 /* Look for the matching `]'. */
7953 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7954 return ansi_opname (ARRAY_REF);
7958 id = ansi_opname (MIN_EXPR);
7962 id = ansi_opname (MAX_EXPR);
7966 id = ansi_assopname (MIN_EXPR);
7970 id = ansi_assopname (MAX_EXPR);
7974 /* Anything else is an error. */
7978 /* If we have selected an identifier, we need to consume the
7981 cp_lexer_consume_token (parser->lexer);
7982 /* Otherwise, no valid operator name was present. */
7985 cp_parser_error (parser, "expected operator");
7986 id = error_mark_node;
7992 /* Parse a template-declaration.
7994 template-declaration:
7995 export [opt] template < template-parameter-list > declaration
7997 If MEMBER_P is TRUE, this template-declaration occurs within a
8000 The grammar rule given by the standard isn't correct. What
8003 template-declaration:
8004 export [opt] template-parameter-list-seq
8005 decl-specifier-seq [opt] init-declarator [opt] ;
8006 export [opt] template-parameter-list-seq
8009 template-parameter-list-seq:
8010 template-parameter-list-seq [opt]
8011 template < template-parameter-list > */
8014 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8016 /* Check for `export'. */
8017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8019 /* Consume the `export' token. */
8020 cp_lexer_consume_token (parser->lexer);
8021 /* Warn that we do not support `export'. */
8022 warning ("keyword %<export%> not implemented, and will be ignored");
8025 cp_parser_template_declaration_after_export (parser, member_p);
8028 /* Parse a template-parameter-list.
8030 template-parameter-list:
8032 template-parameter-list , template-parameter
8034 Returns a TREE_LIST. Each node represents a template parameter.
8035 The nodes are connected via their TREE_CHAINs. */
8038 cp_parser_template_parameter_list (cp_parser* parser)
8040 tree parameter_list = NULL_TREE;
8048 /* Parse the template-parameter. */
8049 parameter = cp_parser_template_parameter (parser, &is_non_type);
8050 /* Add it to the list. */
8051 parameter_list = process_template_parm (parameter_list,
8054 /* Peek at the next token. */
8055 token = cp_lexer_peek_token (parser->lexer);
8056 /* If it's not a `,', we're done. */
8057 if (token->type != CPP_COMMA)
8059 /* Otherwise, consume the `,' token. */
8060 cp_lexer_consume_token (parser->lexer);
8063 return parameter_list;
8066 /* Parse a template-parameter.
8070 parameter-declaration
8072 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8073 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8074 true iff this parameter is a non-type parameter. */
8077 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8080 cp_parameter_declarator *parameter_declarator;
8082 /* Assume it is a type parameter or a template parameter. */
8083 *is_non_type = false;
8084 /* Peek at the next token. */
8085 token = cp_lexer_peek_token (parser->lexer);
8086 /* If it is `class' or `template', we have a type-parameter. */
8087 if (token->keyword == RID_TEMPLATE)
8088 return cp_parser_type_parameter (parser);
8089 /* If it is `class' or `typename' we do not know yet whether it is a
8090 type parameter or a non-type parameter. Consider:
8092 template <typename T, typename T::X X> ...
8096 template <class C, class D*> ...
8098 Here, the first parameter is a type parameter, and the second is
8099 a non-type parameter. We can tell by looking at the token after
8100 the identifier -- if it is a `,', `=', or `>' then we have a type
8102 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8104 /* Peek at the token after `class' or `typename'. */
8105 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8106 /* If it's an identifier, skip it. */
8107 if (token->type == CPP_NAME)
8108 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8109 /* Now, see if the token looks like the end of a template
8111 if (token->type == CPP_COMMA
8112 || token->type == CPP_EQ
8113 || token->type == CPP_GREATER)
8114 return cp_parser_type_parameter (parser);
8117 /* Otherwise, it is a non-type parameter.
8121 When parsing a default template-argument for a non-type
8122 template-parameter, the first non-nested `>' is taken as the end
8123 of the template parameter-list rather than a greater-than
8125 *is_non_type = true;
8126 parameter_declarator
8127 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8128 /*parenthesized_p=*/NULL);
8129 return (build_tree_list
8130 (parameter_declarator->default_argument,
8131 grokdeclarator (parameter_declarator->declarator,
8132 ¶meter_declarator->decl_specifiers,
8133 PARM, /*initialized=*/0,
8134 /*attrlist=*/NULL)));
8137 /* Parse a type-parameter.
8140 class identifier [opt]
8141 class identifier [opt] = type-id
8142 typename identifier [opt]
8143 typename identifier [opt] = type-id
8144 template < template-parameter-list > class identifier [opt]
8145 template < template-parameter-list > class identifier [opt]
8148 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8149 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8150 the declaration of the parameter. */
8153 cp_parser_type_parameter (cp_parser* parser)
8158 /* Look for a keyword to tell us what kind of parameter this is. */
8159 token = cp_parser_require (parser, CPP_KEYWORD,
8160 "`class', `typename', or `template'");
8162 return error_mark_node;
8164 switch (token->keyword)
8170 tree default_argument;
8172 /* If the next token is an identifier, then it names the
8174 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8175 identifier = cp_parser_identifier (parser);
8177 identifier = NULL_TREE;
8179 /* Create the parameter. */
8180 parameter = finish_template_type_parm (class_type_node, identifier);
8182 /* If the next token is an `=', we have a default argument. */
8183 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8185 /* Consume the `=' token. */
8186 cp_lexer_consume_token (parser->lexer);
8187 /* Parse the default-argument. */
8188 default_argument = cp_parser_type_id (parser);
8191 default_argument = NULL_TREE;
8193 /* Create the combined representation of the parameter and the
8194 default argument. */
8195 parameter = build_tree_list (default_argument, parameter);
8201 tree parameter_list;
8203 tree default_argument;
8205 /* Look for the `<'. */
8206 cp_parser_require (parser, CPP_LESS, "`<'");
8207 /* Parse the template-parameter-list. */
8208 begin_template_parm_list ();
8210 = cp_parser_template_parameter_list (parser);
8211 parameter_list = end_template_parm_list (parameter_list);
8212 /* Look for the `>'. */
8213 cp_parser_require (parser, CPP_GREATER, "`>'");
8214 /* Look for the `class' keyword. */
8215 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8216 /* If the next token is an `=', then there is a
8217 default-argument. If the next token is a `>', we are at
8218 the end of the parameter-list. If the next token is a `,',
8219 then we are at the end of this parameter. */
8220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8221 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8222 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8223 identifier = cp_parser_identifier (parser);
8225 identifier = NULL_TREE;
8226 /* Create the template parameter. */
8227 parameter = finish_template_template_parm (class_type_node,
8230 /* If the next token is an `=', then there is a
8231 default-argument. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8236 /* Consume the `='. */
8237 cp_lexer_consume_token (parser->lexer);
8238 /* Parse the id-expression. */
8240 = cp_parser_id_expression (parser,
8241 /*template_keyword_p=*/false,
8242 /*check_dependency_p=*/true,
8243 /*template_p=*/&is_template,
8244 /*declarator_p=*/false);
8245 if (TREE_CODE (default_argument) == TYPE_DECL)
8246 /* If the id-expression was a template-id that refers to
8247 a template-class, we already have the declaration here,
8248 so no further lookup is needed. */
8251 /* Look up the name. */
8253 = cp_parser_lookup_name (parser, default_argument,
8255 /*is_template=*/is_template,
8256 /*is_namespace=*/false,
8257 /*check_dependency=*/true,
8258 /*ambiguous_p=*/NULL);
8259 /* See if the default argument is valid. */
8261 = check_template_template_default_arg (default_argument);
8264 default_argument = NULL_TREE;
8266 /* Create the combined representation of the parameter and the
8267 default argument. */
8268 parameter = build_tree_list (default_argument, parameter);
8273 /* Anything else is an error. */
8274 cp_parser_error (parser,
8275 "expected %<class%>, %<typename%>, or %<template%>");
8276 parameter = error_mark_node;
8282 /* Parse a template-id.
8285 template-name < template-argument-list [opt] >
8287 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8288 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8289 returned. Otherwise, if the template-name names a function, or set
8290 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8291 names a class, returns a TYPE_DECL for the specialization.
8293 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8294 uninstantiated templates. */
8297 cp_parser_template_id (cp_parser *parser,
8298 bool template_keyword_p,
8299 bool check_dependency_p,
8300 bool is_declaration)
8305 cp_token_position start_of_id = 0;
8306 tree access_check = NULL_TREE;
8307 cp_token *next_token, *next_token_2;
8310 /* If the next token corresponds to a template-id, there is no need
8312 next_token = cp_lexer_peek_token (parser->lexer);
8313 if (next_token->type == CPP_TEMPLATE_ID)
8318 /* Get the stored value. */
8319 value = cp_lexer_consume_token (parser->lexer)->value;
8320 /* Perform any access checks that were deferred. */
8321 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8322 perform_or_defer_access_check (TREE_PURPOSE (check),
8323 TREE_VALUE (check));
8324 /* Return the stored value. */
8325 return TREE_VALUE (value);
8328 /* Avoid performing name lookup if there is no possibility of
8329 finding a template-id. */
8330 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8331 || (next_token->type == CPP_NAME
8332 && !cp_parser_nth_token_starts_template_argument_list_p
8335 cp_parser_error (parser, "expected template-id");
8336 return error_mark_node;
8339 /* Remember where the template-id starts. */
8340 if (cp_parser_parsing_tentatively (parser)
8341 && !cp_parser_committed_to_tentative_parse (parser))
8342 start_of_id = cp_lexer_token_position (parser->lexer, false);
8344 push_deferring_access_checks (dk_deferred);
8346 /* Parse the template-name. */
8347 is_identifier = false;
8348 template = cp_parser_template_name (parser, template_keyword_p,
8352 if (template == error_mark_node || is_identifier)
8354 pop_deferring_access_checks ();
8358 /* If we find the sequence `[:' after a template-name, it's probably
8359 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8360 parse correctly the argument list. */
8361 next_token = cp_lexer_peek_token (parser->lexer);
8362 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8363 if (next_token->type == CPP_OPEN_SQUARE
8364 && next_token->flags & DIGRAPH
8365 && next_token_2->type == CPP_COLON
8366 && !(next_token_2->flags & PREV_WHITE))
8368 cp_parser_parse_tentatively (parser);
8369 /* Change `:' into `::'. */
8370 next_token_2->type = CPP_SCOPE;
8371 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8373 cp_lexer_consume_token (parser->lexer);
8374 /* Parse the arguments. */
8375 arguments = cp_parser_enclosed_template_argument_list (parser);
8376 if (!cp_parser_parse_definitely (parser))
8378 /* If we couldn't parse an argument list, then we revert our changes
8379 and return simply an error. Maybe this is not a template-id
8381 next_token_2->type = CPP_COLON;
8382 cp_parser_error (parser, "expected %<<%>");
8383 pop_deferring_access_checks ();
8384 return error_mark_node;
8386 /* Otherwise, emit an error about the invalid digraph, but continue
8387 parsing because we got our argument list. */
8388 pedwarn ("%<<::%> cannot begin a template-argument list");
8389 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8390 "between %<<%> and %<::%>");
8391 if (!flag_permissive)
8396 inform ("(if you use -fpermissive G++ will accept your code)");
8403 /* Look for the `<' that starts the template-argument-list. */
8404 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8406 pop_deferring_access_checks ();
8407 return error_mark_node;
8409 /* Parse the arguments. */
8410 arguments = cp_parser_enclosed_template_argument_list (parser);
8413 /* Build a representation of the specialization. */
8414 if (TREE_CODE (template) == IDENTIFIER_NODE)
8415 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8416 else if (DECL_CLASS_TEMPLATE_P (template)
8417 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8419 = finish_template_type (template, arguments,
8420 cp_lexer_next_token_is (parser->lexer,
8424 /* If it's not a class-template or a template-template, it should be
8425 a function-template. */
8426 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8427 || TREE_CODE (template) == OVERLOAD
8428 || BASELINK_P (template)));
8430 template_id = lookup_template_function (template, arguments);
8433 /* Retrieve any deferred checks. Do not pop this access checks yet
8434 so the memory will not be reclaimed during token replacing below. */
8435 access_check = get_deferred_access_checks ();
8437 /* If parsing tentatively, replace the sequence of tokens that makes
8438 up the template-id with a CPP_TEMPLATE_ID token. That way,
8439 should we re-parse the token stream, we will not have to repeat
8440 the effort required to do the parse, nor will we issue duplicate
8441 error messages about problems during instantiation of the
8445 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8447 /* Reset the contents of the START_OF_ID token. */
8448 token->type = CPP_TEMPLATE_ID;
8449 token->value = build_tree_list (access_check, template_id);
8450 token->keyword = RID_MAX;
8452 /* Purge all subsequent tokens. */
8453 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8456 pop_deferring_access_checks ();
8460 /* Parse a template-name.
8465 The standard should actually say:
8469 operator-function-id
8471 A defect report has been filed about this issue.
8473 A conversion-function-id cannot be a template name because they cannot
8474 be part of a template-id. In fact, looking at this code:
8478 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8479 It is impossible to call a templated conversion-function-id with an
8480 explicit argument list, since the only allowed template parameter is
8481 the type to which it is converting.
8483 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8484 `template' keyword, in a construction like:
8488 In that case `f' is taken to be a template-name, even though there
8489 is no way of knowing for sure.
8491 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8492 name refers to a set of overloaded functions, at least one of which
8493 is a template, or an IDENTIFIER_NODE with the name of the template,
8494 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8495 names are looked up inside uninstantiated templates. */
8498 cp_parser_template_name (cp_parser* parser,
8499 bool template_keyword_p,
8500 bool check_dependency_p,
8501 bool is_declaration,
8502 bool *is_identifier)
8508 /* If the next token is `operator', then we have either an
8509 operator-function-id or a conversion-function-id. */
8510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8512 /* We don't know whether we're looking at an
8513 operator-function-id or a conversion-function-id. */
8514 cp_parser_parse_tentatively (parser);
8515 /* Try an operator-function-id. */
8516 identifier = cp_parser_operator_function_id (parser);
8517 /* If that didn't work, try a conversion-function-id. */
8518 if (!cp_parser_parse_definitely (parser))
8520 cp_parser_error (parser, "expected template-name");
8521 return error_mark_node;
8524 /* Look for the identifier. */
8526 identifier = cp_parser_identifier (parser);
8528 /* If we didn't find an identifier, we don't have a template-id. */
8529 if (identifier == error_mark_node)
8530 return error_mark_node;
8532 /* If the name immediately followed the `template' keyword, then it
8533 is a template-name. However, if the next token is not `<', then
8534 we do not treat it as a template-name, since it is not being used
8535 as part of a template-id. This enables us to handle constructs
8538 template <typename T> struct S { S(); };
8539 template <typename T> S<T>::S();
8541 correctly. We would treat `S' as a template -- if it were `S<T>'
8542 -- but we do not if there is no `<'. */
8544 if (processing_template_decl
8545 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8547 /* In a declaration, in a dependent context, we pretend that the
8548 "template" keyword was present in order to improve error
8549 recovery. For example, given:
8551 template <typename T> void f(T::X<int>);
8553 we want to treat "X<int>" as a template-id. */
8555 && !template_keyword_p
8556 && parser->scope && TYPE_P (parser->scope)
8557 && check_dependency_p
8558 && dependent_type_p (parser->scope)
8559 /* Do not do this for dtors (or ctors), since they never
8560 need the template keyword before their name. */
8561 && !constructor_name_p (identifier, parser->scope))
8563 cp_token_position start = 0;
8565 /* Explain what went wrong. */
8566 error ("non-template %qD used as template", identifier);
8567 inform ("use %<%T::template %D%> to indicate that it is a template",
8568 parser->scope, identifier);
8569 /* If parsing tentatively, find the location of the "<"
8571 if (cp_parser_parsing_tentatively (parser)
8572 && !cp_parser_committed_to_tentative_parse (parser))
8574 cp_parser_simulate_error (parser);
8575 start = cp_lexer_token_position (parser->lexer, true);
8577 /* Parse the template arguments so that we can issue error
8578 messages about them. */
8579 cp_lexer_consume_token (parser->lexer);
8580 cp_parser_enclosed_template_argument_list (parser);
8581 /* Skip tokens until we find a good place from which to
8582 continue parsing. */
8583 cp_parser_skip_to_closing_parenthesis (parser,
8584 /*recovering=*/true,
8586 /*consume_paren=*/false);
8587 /* If parsing tentatively, permanently remove the
8588 template argument list. That will prevent duplicate
8589 error messages from being issued about the missing
8590 "template" keyword. */
8592 cp_lexer_purge_tokens_after (parser->lexer, start);
8594 *is_identifier = true;
8598 /* If the "template" keyword is present, then there is generally
8599 no point in doing name-lookup, so we just return IDENTIFIER.
8600 But, if the qualifying scope is non-dependent then we can
8601 (and must) do name-lookup normally. */
8602 if (template_keyword_p
8604 || (TYPE_P (parser->scope)
8605 && dependent_type_p (parser->scope))))
8609 /* Look up the name. */
8610 decl = cp_parser_lookup_name (parser, identifier,
8612 /*is_template=*/false,
8613 /*is_namespace=*/false,
8615 /*ambiguous_p=*/NULL);
8616 decl = maybe_get_template_decl_from_type_decl (decl);
8618 /* If DECL is a template, then the name was a template-name. */
8619 if (TREE_CODE (decl) == TEMPLATE_DECL)
8623 /* The standard does not explicitly indicate whether a name that
8624 names a set of overloaded declarations, some of which are
8625 templates, is a template-name. However, such a name should
8626 be a template-name; otherwise, there is no way to form a
8627 template-id for the overloaded templates. */
8628 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8629 if (TREE_CODE (fns) == OVERLOAD)
8633 for (fn = fns; fn; fn = OVL_NEXT (fn))
8634 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8639 /* Otherwise, the name does not name a template. */
8640 cp_parser_error (parser, "expected template-name");
8641 return error_mark_node;
8645 /* If DECL is dependent, and refers to a function, then just return
8646 its name; we will look it up again during template instantiation. */
8647 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8649 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8650 if (TYPE_P (scope) && dependent_type_p (scope))
8657 /* Parse a template-argument-list.
8659 template-argument-list:
8661 template-argument-list , template-argument
8663 Returns a TREE_VEC containing the arguments. */
8666 cp_parser_template_argument_list (cp_parser* parser)
8668 tree fixed_args[10];
8669 unsigned n_args = 0;
8670 unsigned alloced = 10;
8671 tree *arg_ary = fixed_args;
8673 bool saved_in_template_argument_list_p;
8675 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8676 parser->in_template_argument_list_p = true;
8682 /* Consume the comma. */
8683 cp_lexer_consume_token (parser->lexer);
8685 /* Parse the template-argument. */
8686 argument = cp_parser_template_argument (parser);
8687 if (n_args == alloced)
8691 if (arg_ary == fixed_args)
8693 arg_ary = xmalloc (sizeof (tree) * alloced);
8694 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8697 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8699 arg_ary[n_args++] = argument;
8701 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8703 vec = make_tree_vec (n_args);
8706 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8708 if (arg_ary != fixed_args)
8710 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8714 /* Parse a template-argument.
8717 assignment-expression
8721 The representation is that of an assignment-expression, type-id, or
8722 id-expression -- except that the qualified id-expression is
8723 evaluated, so that the value returned is either a DECL or an
8726 Although the standard says "assignment-expression", it forbids
8727 throw-expressions or assignments in the template argument.
8728 Therefore, we use "conditional-expression" instead. */
8731 cp_parser_template_argument (cp_parser* parser)
8736 bool maybe_type_id = false;
8739 tree qualifying_class;
8741 /* There's really no way to know what we're looking at, so we just
8742 try each alternative in order.
8746 In a template-argument, an ambiguity between a type-id and an
8747 expression is resolved to a type-id, regardless of the form of
8748 the corresponding template-parameter.
8750 Therefore, we try a type-id first. */
8751 cp_parser_parse_tentatively (parser);
8752 argument = cp_parser_type_id (parser);
8753 /* If there was no error parsing the type-id but the next token is a '>>',
8754 we probably found a typo for '> >'. But there are type-id which are
8755 also valid expressions. For instance:
8757 struct X { int operator >> (int); };
8758 template <int V> struct Foo {};
8761 Here 'X()' is a valid type-id of a function type, but the user just
8762 wanted to write the expression "X() >> 5". Thus, we remember that we
8763 found a valid type-id, but we still try to parse the argument as an
8764 expression to see what happens. */
8765 if (!cp_parser_error_occurred (parser)
8766 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8768 maybe_type_id = true;
8769 cp_parser_abort_tentative_parse (parser);
8773 /* If the next token isn't a `,' or a `>', then this argument wasn't
8774 really finished. This means that the argument is not a valid
8776 if (!cp_parser_next_token_ends_template_argument_p (parser))
8777 cp_parser_error (parser, "expected template-argument");
8778 /* If that worked, we're done. */
8779 if (cp_parser_parse_definitely (parser))
8782 /* We're still not sure what the argument will be. */
8783 cp_parser_parse_tentatively (parser);
8784 /* Try a template. */
8785 argument = cp_parser_id_expression (parser,
8786 /*template_keyword_p=*/false,
8787 /*check_dependency_p=*/true,
8789 /*declarator_p=*/false);
8790 /* If the next token isn't a `,' or a `>', then this argument wasn't
8792 if (!cp_parser_next_token_ends_template_argument_p (parser))
8793 cp_parser_error (parser, "expected template-argument");
8794 if (!cp_parser_error_occurred (parser))
8796 /* Figure out what is being referred to. If the id-expression
8797 was for a class template specialization, then we will have a
8798 TYPE_DECL at this point. There is no need to do name lookup
8799 at this point in that case. */
8800 if (TREE_CODE (argument) != TYPE_DECL)
8801 argument = cp_parser_lookup_name (parser, argument,
8803 /*is_template=*/template_p,
8804 /*is_namespace=*/false,
8805 /*check_dependency=*/true,
8806 /*ambiguous_p=*/NULL);
8807 if (TREE_CODE (argument) != TEMPLATE_DECL
8808 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8809 cp_parser_error (parser, "expected template-name");
8811 if (cp_parser_parse_definitely (parser))
8813 /* It must be a non-type argument. There permitted cases are given
8814 in [temp.arg.nontype]:
8816 -- an integral constant-expression of integral or enumeration
8819 -- the name of a non-type template-parameter; or
8821 -- the name of an object or function with external linkage...
8823 -- the address of an object or function with external linkage...
8825 -- a pointer to member... */
8826 /* Look for a non-type template parameter. */
8827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8829 cp_parser_parse_tentatively (parser);
8830 argument = cp_parser_primary_expression (parser,
8833 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8834 || !cp_parser_next_token_ends_template_argument_p (parser))
8835 cp_parser_simulate_error (parser);
8836 if (cp_parser_parse_definitely (parser))
8839 /* If the next token is "&", the argument must be the address of an
8840 object or function with external linkage. */
8841 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8843 cp_lexer_consume_token (parser->lexer);
8844 /* See if we might have an id-expression. */
8845 token = cp_lexer_peek_token (parser->lexer);
8846 if (token->type == CPP_NAME
8847 || token->keyword == RID_OPERATOR
8848 || token->type == CPP_SCOPE
8849 || token->type == CPP_TEMPLATE_ID
8850 || token->type == CPP_NESTED_NAME_SPECIFIER)
8852 cp_parser_parse_tentatively (parser);
8853 argument = cp_parser_primary_expression (parser,
8856 if (cp_parser_error_occurred (parser)
8857 || !cp_parser_next_token_ends_template_argument_p (parser))
8858 cp_parser_abort_tentative_parse (parser);
8861 if (qualifying_class)
8862 argument = finish_qualified_id_expr (qualifying_class,
8866 if (TREE_CODE (argument) == VAR_DECL)
8868 /* A variable without external linkage might still be a
8869 valid constant-expression, so no error is issued here
8870 if the external-linkage check fails. */
8871 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8872 cp_parser_simulate_error (parser);
8874 else if (is_overloaded_fn (argument))
8875 /* All overloaded functions are allowed; if the external
8876 linkage test does not pass, an error will be issued
8880 && (TREE_CODE (argument) == OFFSET_REF
8881 || TREE_CODE (argument) == SCOPE_REF))
8882 /* A pointer-to-member. */
8885 cp_parser_simulate_error (parser);
8887 if (cp_parser_parse_definitely (parser))
8890 argument = build_x_unary_op (ADDR_EXPR, argument);
8895 /* If the argument started with "&", there are no other valid
8896 alternatives at this point. */
8899 cp_parser_error (parser, "invalid non-type template argument");
8900 return error_mark_node;
8902 /* If the argument wasn't successfully parsed as a type-id followed
8903 by '>>', the argument can only be a constant expression now.
8904 Otherwise, we try parsing the constant-expression tentatively,
8905 because the argument could really be a type-id. */
8907 cp_parser_parse_tentatively (parser);
8908 argument = cp_parser_constant_expression (parser,
8909 /*allow_non_constant_p=*/false,
8910 /*non_constant_p=*/NULL);
8911 argument = fold_non_dependent_expr (argument);
8914 if (!cp_parser_next_token_ends_template_argument_p (parser))
8915 cp_parser_error (parser, "expected template-argument");
8916 if (cp_parser_parse_definitely (parser))
8918 /* We did our best to parse the argument as a non type-id, but that
8919 was the only alternative that matched (albeit with a '>' after
8920 it). We can assume it's just a typo from the user, and a
8921 diagnostic will then be issued. */
8922 return cp_parser_type_id (parser);
8925 /* Parse an explicit-instantiation.
8927 explicit-instantiation:
8928 template declaration
8930 Although the standard says `declaration', what it really means is:
8932 explicit-instantiation:
8933 template decl-specifier-seq [opt] declarator [opt] ;
8935 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8936 supposed to be allowed. A defect report has been filed about this
8941 explicit-instantiation:
8942 storage-class-specifier template
8943 decl-specifier-seq [opt] declarator [opt] ;
8944 function-specifier template
8945 decl-specifier-seq [opt] declarator [opt] ; */
8948 cp_parser_explicit_instantiation (cp_parser* parser)
8950 int declares_class_or_enum;
8951 cp_decl_specifier_seq decl_specifiers;
8952 tree extension_specifier = NULL_TREE;
8954 /* Look for an (optional) storage-class-specifier or
8955 function-specifier. */
8956 if (cp_parser_allow_gnu_extensions_p (parser))
8959 = cp_parser_storage_class_specifier_opt (parser);
8960 if (!extension_specifier)
8962 = cp_parser_function_specifier_opt (parser,
8963 /*decl_specs=*/NULL);
8966 /* Look for the `template' keyword. */
8967 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8968 /* Let the front end know that we are processing an explicit
8970 begin_explicit_instantiation ();
8971 /* [temp.explicit] says that we are supposed to ignore access
8972 control while processing explicit instantiation directives. */
8973 push_deferring_access_checks (dk_no_check);
8974 /* Parse a decl-specifier-seq. */
8975 cp_parser_decl_specifier_seq (parser,
8976 CP_PARSER_FLAGS_OPTIONAL,
8978 &declares_class_or_enum);
8979 /* If there was exactly one decl-specifier, and it declared a class,
8980 and there's no declarator, then we have an explicit type
8982 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8986 type = check_tag_decl (&decl_specifiers);
8987 /* Turn access control back on for names used during
8988 template instantiation. */
8989 pop_deferring_access_checks ();
8991 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8995 cp_declarator *declarator;
8998 /* Parse the declarator. */
9000 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9001 /*ctor_dtor_or_conv_p=*/NULL,
9002 /*parenthesized_p=*/NULL,
9003 /*member_p=*/false);
9004 cp_parser_check_for_definition_in_return_type (declarator,
9005 declares_class_or_enum);
9006 if (declarator != cp_error_declarator)
9008 decl = grokdeclarator (declarator, &decl_specifiers,
9010 /* Turn access control back on for names used during
9011 template instantiation. */
9012 pop_deferring_access_checks ();
9013 /* Do the explicit instantiation. */
9014 do_decl_instantiation (decl, extension_specifier);
9018 pop_deferring_access_checks ();
9019 /* Skip the body of the explicit instantiation. */
9020 cp_parser_skip_to_end_of_statement (parser);
9023 /* We're done with the instantiation. */
9024 end_explicit_instantiation ();
9026 cp_parser_consume_semicolon_at_end_of_statement (parser);
9029 /* Parse an explicit-specialization.
9031 explicit-specialization:
9032 template < > declaration
9034 Although the standard says `declaration', what it really means is:
9036 explicit-specialization:
9037 template <> decl-specifier [opt] init-declarator [opt] ;
9038 template <> function-definition
9039 template <> explicit-specialization
9040 template <> template-declaration */
9043 cp_parser_explicit_specialization (cp_parser* parser)
9045 /* Look for the `template' keyword. */
9046 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9047 /* Look for the `<'. */
9048 cp_parser_require (parser, CPP_LESS, "`<'");
9049 /* Look for the `>'. */
9050 cp_parser_require (parser, CPP_GREATER, "`>'");
9051 /* We have processed another parameter list. */
9052 ++parser->num_template_parameter_lists;
9053 /* Let the front end know that we are beginning a specialization. */
9054 begin_specialization ();
9056 /* If the next keyword is `template', we need to figure out whether
9057 or not we're looking a template-declaration. */
9058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9060 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9061 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9062 cp_parser_template_declaration_after_export (parser,
9063 /*member_p=*/false);
9065 cp_parser_explicit_specialization (parser);
9068 /* Parse the dependent declaration. */
9069 cp_parser_single_declaration (parser,
9073 /* We're done with the specialization. */
9074 end_specialization ();
9075 /* We're done with this parameter list. */
9076 --parser->num_template_parameter_lists;
9079 /* Parse a type-specifier.
9082 simple-type-specifier
9085 elaborated-type-specifier
9093 Returns a representation of the type-specifier. For a
9094 class-specifier, enum-specifier, or elaborated-type-specifier, a
9095 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9097 The parser flags FLAGS is used to control type-specifier parsing.
9099 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9100 in a decl-specifier-seq.
9102 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9103 class-specifier, enum-specifier, or elaborated-type-specifier, then
9104 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9105 if a type is declared; 2 if it is defined. Otherwise, it is set to
9108 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9109 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9113 cp_parser_type_specifier (cp_parser* parser,
9114 cp_parser_flags flags,
9115 cp_decl_specifier_seq *decl_specs,
9116 bool is_declaration,
9117 int* declares_class_or_enum,
9118 bool* is_cv_qualifier)
9120 tree type_spec = NULL_TREE;
9123 cp_decl_spec ds = ds_last;
9125 /* Assume this type-specifier does not declare a new type. */
9126 if (declares_class_or_enum)
9127 *declares_class_or_enum = 0;
9128 /* And that it does not specify a cv-qualifier. */
9129 if (is_cv_qualifier)
9130 *is_cv_qualifier = false;
9131 /* Peek at the next token. */
9132 token = cp_lexer_peek_token (parser->lexer);
9134 /* If we're looking at a keyword, we can use that to guide the
9135 production we choose. */
9136 keyword = token->keyword;
9140 /* 'enum' [identifier] '{' introduces an enum-specifier;
9141 'enum' <anything else> introduces an elaborated-type-specifier. */
9142 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9143 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9144 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9147 type_spec = cp_parser_enum_specifier (parser);
9148 if (declares_class_or_enum)
9149 *declares_class_or_enum = 2;
9151 cp_parser_set_decl_spec_type (decl_specs,
9153 /*user_defined_p=*/true);
9157 goto elaborated_type_specifier;
9159 /* Any of these indicate either a class-specifier, or an
9160 elaborated-type-specifier. */
9164 /* Parse tentatively so that we can back up if we don't find a
9166 cp_parser_parse_tentatively (parser);
9167 /* Look for the class-specifier. */
9168 type_spec = cp_parser_class_specifier (parser);
9169 /* If that worked, we're done. */
9170 if (cp_parser_parse_definitely (parser))
9172 if (declares_class_or_enum)
9173 *declares_class_or_enum = 2;
9175 cp_parser_set_decl_spec_type (decl_specs,
9177 /*user_defined_p=*/true);
9182 elaborated_type_specifier:
9183 /* We're declaring (not defining) a class or enum. */
9184 if (declares_class_or_enum)
9185 *declares_class_or_enum = 1;
9189 /* Look for an elaborated-type-specifier. */
9191 = (cp_parser_elaborated_type_specifier
9193 decl_specs && decl_specs->specs[(int) ds_friend],
9196 cp_parser_set_decl_spec_type (decl_specs,
9198 /*user_defined_p=*/true);
9203 if (is_cv_qualifier)
9204 *is_cv_qualifier = true;
9209 if (is_cv_qualifier)
9210 *is_cv_qualifier = true;
9215 if (is_cv_qualifier)
9216 *is_cv_qualifier = true;
9220 /* The `__complex__' keyword is a GNU extension. */
9228 /* Handle simple keywords. */
9233 ++decl_specs->specs[(int)ds];
9234 decl_specs->any_specifiers_p = true;
9236 return cp_lexer_consume_token (parser->lexer)->value;
9239 /* If we do not already have a type-specifier, assume we are looking
9240 at a simple-type-specifier. */
9241 type_spec = cp_parser_simple_type_specifier (parser,
9245 /* If we didn't find a type-specifier, and a type-specifier was not
9246 optional in this context, issue an error message. */
9247 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9249 cp_parser_error (parser, "expected type specifier");
9250 return error_mark_node;
9256 /* Parse a simple-type-specifier.
9258 simple-type-specifier:
9259 :: [opt] nested-name-specifier [opt] type-name
9260 :: [opt] nested-name-specifier template template-id
9275 simple-type-specifier:
9276 __typeof__ unary-expression
9277 __typeof__ ( type-id )
9279 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9280 appropriately updated. */
9283 cp_parser_simple_type_specifier (cp_parser* parser,
9284 cp_decl_specifier_seq *decl_specs,
9285 cp_parser_flags flags)
9287 tree type = NULL_TREE;
9290 /* Peek at the next token. */
9291 token = cp_lexer_peek_token (parser->lexer);
9293 /* If we're looking at a keyword, things are easy. */
9294 switch (token->keyword)
9298 decl_specs->explicit_char_p = true;
9299 type = char_type_node;
9302 type = wchar_type_node;
9305 type = boolean_type_node;
9309 ++decl_specs->specs[(int) ds_short];
9310 type = short_integer_type_node;
9314 decl_specs->explicit_int_p = true;
9315 type = integer_type_node;
9319 ++decl_specs->specs[(int) ds_long];
9320 type = long_integer_type_node;
9324 ++decl_specs->specs[(int) ds_signed];
9325 type = integer_type_node;
9329 ++decl_specs->specs[(int) ds_unsigned];
9330 type = unsigned_type_node;
9333 type = float_type_node;
9336 type = double_type_node;
9339 type = void_type_node;
9343 /* Consume the `typeof' token. */
9344 cp_lexer_consume_token (parser->lexer);
9345 /* Parse the operand to `typeof'. */
9346 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9347 /* If it is not already a TYPE, take its type. */
9349 type = finish_typeof (type);
9352 cp_parser_set_decl_spec_type (decl_specs, type,
9353 /*user_defined_p=*/true);
9361 /* If the type-specifier was for a built-in type, we're done. */
9366 /* Record the type. */
9368 && (token->keyword != RID_SIGNED
9369 && token->keyword != RID_UNSIGNED
9370 && token->keyword != RID_SHORT
9371 && token->keyword != RID_LONG))
9372 cp_parser_set_decl_spec_type (decl_specs,
9374 /*user_defined=*/false);
9376 decl_specs->any_specifiers_p = true;
9378 /* Consume the token. */
9379 id = cp_lexer_consume_token (parser->lexer)->value;
9381 /* There is no valid C++ program where a non-template type is
9382 followed by a "<". That usually indicates that the user thought
9383 that the type was a template. */
9384 cp_parser_check_for_invalid_template_id (parser, type);
9386 return TYPE_NAME (type);
9389 /* The type-specifier must be a user-defined type. */
9390 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9395 /* Don't gobble tokens or issue error messages if this is an
9396 optional type-specifier. */
9397 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9398 cp_parser_parse_tentatively (parser);
9400 /* Look for the optional `::' operator. */
9402 = (cp_parser_global_scope_opt (parser,
9403 /*current_scope_valid_p=*/false)
9405 /* Look for the nested-name specifier. */
9407 = (cp_parser_nested_name_specifier_opt (parser,
9408 /*typename_keyword_p=*/false,
9409 /*check_dependency_p=*/true,
9411 /*is_declaration=*/false)
9413 /* If we have seen a nested-name-specifier, and the next token
9414 is `template', then we are using the template-id production. */
9416 && cp_parser_optional_template_keyword (parser))
9418 /* Look for the template-id. */
9419 type = cp_parser_template_id (parser,
9420 /*template_keyword_p=*/true,
9421 /*check_dependency_p=*/true,
9422 /*is_declaration=*/false);
9423 /* If the template-id did not name a type, we are out of
9425 if (TREE_CODE (type) != TYPE_DECL)
9427 cp_parser_error (parser, "expected template-id for type");
9431 /* Otherwise, look for a type-name. */
9433 type = cp_parser_type_name (parser);
9434 /* Keep track of all name-lookups performed in class scopes. */
9438 && TREE_CODE (type) == TYPE_DECL
9439 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9440 maybe_note_name_used_in_class (DECL_NAME (type), type);
9441 /* If it didn't work out, we don't have a TYPE. */
9442 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9443 && !cp_parser_parse_definitely (parser))
9445 if (type && decl_specs)
9446 cp_parser_set_decl_spec_type (decl_specs, type,
9447 /*user_defined=*/true);
9450 /* If we didn't get a type-name, issue an error message. */
9451 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9453 cp_parser_error (parser, "expected type-name");
9454 return error_mark_node;
9457 /* There is no valid C++ program where a non-template type is
9458 followed by a "<". That usually indicates that the user thought
9459 that the type was a template. */
9460 if (type && type != error_mark_node)
9461 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9466 /* Parse a type-name.
9479 Returns a TYPE_DECL for the the type. */
9482 cp_parser_type_name (cp_parser* parser)
9487 /* We can't know yet whether it is a class-name or not. */
9488 cp_parser_parse_tentatively (parser);
9489 /* Try a class-name. */
9490 type_decl = cp_parser_class_name (parser,
9491 /*typename_keyword_p=*/false,
9492 /*template_keyword_p=*/false,
9494 /*check_dependency_p=*/true,
9495 /*class_head_p=*/false,
9496 /*is_declaration=*/false);
9497 /* If it's not a class-name, keep looking. */
9498 if (!cp_parser_parse_definitely (parser))
9500 /* It must be a typedef-name or an enum-name. */
9501 identifier = cp_parser_identifier (parser);
9502 if (identifier == error_mark_node)
9503 return error_mark_node;
9505 /* Look up the type-name. */
9506 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9507 /* Issue an error if we did not find a type-name. */
9508 if (TREE_CODE (type_decl) != TYPE_DECL)
9510 if (!cp_parser_simulate_error (parser))
9511 cp_parser_name_lookup_error (parser, identifier, type_decl,
9513 type_decl = error_mark_node;
9515 /* Remember that the name was used in the definition of the
9516 current class so that we can check later to see if the
9517 meaning would have been different after the class was
9518 entirely defined. */
9519 else if (type_decl != error_mark_node
9521 maybe_note_name_used_in_class (identifier, type_decl);
9528 /* Parse an elaborated-type-specifier. Note that the grammar given
9529 here incorporates the resolution to DR68.
9531 elaborated-type-specifier:
9532 class-key :: [opt] nested-name-specifier [opt] identifier
9533 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9534 enum :: [opt] nested-name-specifier [opt] identifier
9535 typename :: [opt] nested-name-specifier identifier
9536 typename :: [opt] nested-name-specifier template [opt]
9541 elaborated-type-specifier:
9542 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9543 class-key attributes :: [opt] nested-name-specifier [opt]
9544 template [opt] template-id
9545 enum attributes :: [opt] nested-name-specifier [opt] identifier
9547 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9548 declared `friend'. If IS_DECLARATION is TRUE, then this
9549 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9550 something is being declared.
9552 Returns the TYPE specified. */
9555 cp_parser_elaborated_type_specifier (cp_parser* parser,
9557 bool is_declaration)
9559 enum tag_types tag_type;
9561 tree type = NULL_TREE;
9562 tree attributes = NULL_TREE;
9564 /* See if we're looking at the `enum' keyword. */
9565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9567 /* Consume the `enum' token. */
9568 cp_lexer_consume_token (parser->lexer);
9569 /* Remember that it's an enumeration type. */
9570 tag_type = enum_type;
9571 /* Parse the attributes. */
9572 attributes = cp_parser_attributes_opt (parser);
9574 /* Or, it might be `typename'. */
9575 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9578 /* Consume the `typename' token. */
9579 cp_lexer_consume_token (parser->lexer);
9580 /* Remember that it's a `typename' type. */
9581 tag_type = typename_type;
9582 /* The `typename' keyword is only allowed in templates. */
9583 if (!processing_template_decl)
9584 pedwarn ("using %<typename%> outside of template");
9586 /* Otherwise it must be a class-key. */
9589 tag_type = cp_parser_class_key (parser);
9590 if (tag_type == none_type)
9591 return error_mark_node;
9592 /* Parse the attributes. */
9593 attributes = cp_parser_attributes_opt (parser);
9596 /* Look for the `::' operator. */
9597 cp_parser_global_scope_opt (parser,
9598 /*current_scope_valid_p=*/false);
9599 /* Look for the nested-name-specifier. */
9600 if (tag_type == typename_type)
9602 if (cp_parser_nested_name_specifier (parser,
9603 /*typename_keyword_p=*/true,
9604 /*check_dependency_p=*/true,
9608 return error_mark_node;
9611 /* Even though `typename' is not present, the proposed resolution
9612 to Core Issue 180 says that in `class A<T>::B', `B' should be
9613 considered a type-name, even if `A<T>' is dependent. */
9614 cp_parser_nested_name_specifier_opt (parser,
9615 /*typename_keyword_p=*/true,
9616 /*check_dependency_p=*/true,
9619 /* For everything but enumeration types, consider a template-id. */
9620 if (tag_type != enum_type)
9622 bool template_p = false;
9625 /* Allow the `template' keyword. */
9626 template_p = cp_parser_optional_template_keyword (parser);
9627 /* If we didn't see `template', we don't know if there's a
9628 template-id or not. */
9630 cp_parser_parse_tentatively (parser);
9631 /* Parse the template-id. */
9632 decl = cp_parser_template_id (parser, template_p,
9633 /*check_dependency_p=*/true,
9635 /* If we didn't find a template-id, look for an ordinary
9637 if (!template_p && !cp_parser_parse_definitely (parser))
9639 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9640 in effect, then we must assume that, upon instantiation, the
9641 template will correspond to a class. */
9642 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9643 && tag_type == typename_type)
9644 type = make_typename_type (parser->scope, decl,
9647 type = TREE_TYPE (decl);
9650 /* For an enumeration type, consider only a plain identifier. */
9653 identifier = cp_parser_identifier (parser);
9655 if (identifier == error_mark_node)
9657 parser->scope = NULL_TREE;
9658 return error_mark_node;
9661 /* For a `typename', we needn't call xref_tag. */
9662 if (tag_type == typename_type)
9663 return cp_parser_make_typename_type (parser, parser->scope,
9665 /* Look up a qualified name in the usual way. */
9670 /* In an elaborated-type-specifier, names are assumed to name
9671 types, so we set IS_TYPE to TRUE when calling
9672 cp_parser_lookup_name. */
9673 decl = cp_parser_lookup_name (parser, identifier,
9675 /*is_template=*/false,
9676 /*is_namespace=*/false,
9677 /*check_dependency=*/true,
9678 /*ambiguous_p=*/NULL);
9680 /* If we are parsing friend declaration, DECL may be a
9681 TEMPLATE_DECL tree node here. However, we need to check
9682 whether this TEMPLATE_DECL results in valid code. Consider
9683 the following example:
9686 template <class T> class C {};
9689 template <class T> friend class N::C; // #1, valid code
9691 template <class T> class Y {
9692 friend class N::C; // #2, invalid code
9695 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9696 name lookup of `N::C'. We see that friend declaration must
9697 be template for the code to be valid. Note that
9698 processing_template_decl does not work here since it is
9699 always 1 for the above two cases. */
9701 decl = (cp_parser_maybe_treat_template_as_class
9702 (decl, /*tag_name_p=*/is_friend
9703 && parser->num_template_parameter_lists));
9705 if (TREE_CODE (decl) != TYPE_DECL)
9707 error ("expected type-name");
9708 return error_mark_node;
9711 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9712 check_elaborated_type_specifier
9714 (parser->num_template_parameter_lists
9715 || DECL_SELF_REFERENCE_P (decl)));
9717 type = TREE_TYPE (decl);
9721 /* An elaborated-type-specifier sometimes introduces a new type and
9722 sometimes names an existing type. Normally, the rule is that it
9723 introduces a new type only if there is not an existing type of
9724 the same name already in scope. For example, given:
9727 void f() { struct S s; }
9729 the `struct S' in the body of `f' is the same `struct S' as in
9730 the global scope; the existing definition is used. However, if
9731 there were no global declaration, this would introduce a new
9732 local class named `S'.
9734 An exception to this rule applies to the following code:
9736 namespace N { struct S; }
9738 Here, the elaborated-type-specifier names a new type
9739 unconditionally; even if there is already an `S' in the
9740 containing scope this declaration names a new type.
9741 This exception only applies if the elaborated-type-specifier
9742 forms the complete declaration:
9746 A declaration consisting solely of `class-key identifier ;' is
9747 either a redeclaration of the name in the current scope or a
9748 forward declaration of the identifier as a class name. It
9749 introduces the name into the current scope.
9751 We are in this situation precisely when the next token is a `;'.
9753 An exception to the exception is that a `friend' declaration does
9754 *not* name a new type; i.e., given:
9756 struct S { friend struct T; };
9758 `T' is not a new type in the scope of `S'.
9760 Also, `new struct S' or `sizeof (struct S)' never results in the
9761 definition of a new type; a new type can only be declared in a
9762 declaration context. */
9764 /* Warn about attributes. They are ignored. */
9766 warning ("type attributes are honored only at type definition");
9768 type = xref_tag (tag_type, identifier,
9771 || cp_lexer_next_token_is_not (parser->lexer,
9773 parser->num_template_parameter_lists);
9776 if (tag_type != enum_type)
9777 cp_parser_check_class_key (tag_type, type);
9779 /* A "<" cannot follow an elaborated type specifier. If that
9780 happens, the user was probably trying to form a template-id. */
9781 cp_parser_check_for_invalid_template_id (parser, type);
9786 /* Parse an enum-specifier.
9789 enum identifier [opt] { enumerator-list [opt] }
9791 Returns an ENUM_TYPE representing the enumeration. */
9794 cp_parser_enum_specifier (cp_parser* parser)
9799 /* Caller guarantees that the current token is 'enum', an identifier
9800 possibly follows, and the token after that is an opening brace.
9801 If we don't have an identifier, fabricate an anonymous name for
9802 the enumeration being defined. */
9803 cp_lexer_consume_token (parser->lexer);
9805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9806 identifier = cp_parser_identifier (parser);
9808 identifier = make_anon_name ();
9810 /* Issue an error message if type-definitions are forbidden here. */
9811 cp_parser_check_type_definition (parser);
9813 /* Create the new type. We do this before consuming the opening brace
9814 so the enum will be recorded as being on the line of its tag (or the
9815 'enum' keyword, if there is no tag). */
9816 type = start_enum (identifier);
9818 /* Consume the opening brace. */
9819 cp_lexer_consume_token (parser->lexer);
9821 /* If the next token is not '}', then there are some enumerators. */
9822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9823 cp_parser_enumerator_list (parser, type);
9825 /* Consume the final '}'. */
9826 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9828 /* Finish up the enumeration. */
9834 /* Parse an enumerator-list. The enumerators all have the indicated
9838 enumerator-definition
9839 enumerator-list , enumerator-definition */
9842 cp_parser_enumerator_list (cp_parser* parser, tree type)
9846 /* Parse an enumerator-definition. */
9847 cp_parser_enumerator_definition (parser, type);
9849 /* If the next token is not a ',', we've reached the end of
9851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9853 /* Otherwise, consume the `,' and keep going. */
9854 cp_lexer_consume_token (parser->lexer);
9855 /* If the next token is a `}', there is a trailing comma. */
9856 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9858 if (pedantic && !in_system_header)
9859 pedwarn ("comma at end of enumerator list");
9865 /* Parse an enumerator-definition. The enumerator has the indicated
9868 enumerator-definition:
9870 enumerator = constant-expression
9876 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9881 /* Look for the identifier. */
9882 identifier = cp_parser_identifier (parser);
9883 if (identifier == error_mark_node)
9886 /* If the next token is an '=', then there is an explicit value. */
9887 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9889 /* Consume the `=' token. */
9890 cp_lexer_consume_token (parser->lexer);
9891 /* Parse the value. */
9892 value = cp_parser_constant_expression (parser,
9893 /*allow_non_constant_p=*/false,
9899 /* Create the enumerator. */
9900 build_enumerator (identifier, value, type);
9903 /* Parse a namespace-name.
9906 original-namespace-name
9909 Returns the NAMESPACE_DECL for the namespace. */
9912 cp_parser_namespace_name (cp_parser* parser)
9915 tree namespace_decl;
9917 /* Get the name of the namespace. */
9918 identifier = cp_parser_identifier (parser);
9919 if (identifier == error_mark_node)
9920 return error_mark_node;
9922 /* Look up the identifier in the currently active scope. Look only
9923 for namespaces, due to:
9927 When looking up a namespace-name in a using-directive or alias
9928 definition, only namespace names are considered.
9934 During the lookup of a name preceding the :: scope resolution
9935 operator, object, function, and enumerator names are ignored.
9937 (Note that cp_parser_class_or_namespace_name only calls this
9938 function if the token after the name is the scope resolution
9940 namespace_decl = cp_parser_lookup_name (parser, identifier,
9942 /*is_template=*/false,
9943 /*is_namespace=*/true,
9944 /*check_dependency=*/true,
9945 /*ambiguous_p=*/NULL);
9946 /* If it's not a namespace, issue an error. */
9947 if (namespace_decl == error_mark_node
9948 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9950 cp_parser_error (parser, "expected namespace-name");
9951 namespace_decl = error_mark_node;
9954 return namespace_decl;
9957 /* Parse a namespace-definition.
9959 namespace-definition:
9960 named-namespace-definition
9961 unnamed-namespace-definition
9963 named-namespace-definition:
9964 original-namespace-definition
9965 extension-namespace-definition
9967 original-namespace-definition:
9968 namespace identifier { namespace-body }
9970 extension-namespace-definition:
9971 namespace original-namespace-name { namespace-body }
9973 unnamed-namespace-definition:
9974 namespace { namespace-body } */
9977 cp_parser_namespace_definition (cp_parser* parser)
9981 /* Look for the `namespace' keyword. */
9982 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9984 /* Get the name of the namespace. We do not attempt to distinguish
9985 between an original-namespace-definition and an
9986 extension-namespace-definition at this point. The semantic
9987 analysis routines are responsible for that. */
9988 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9989 identifier = cp_parser_identifier (parser);
9991 identifier = NULL_TREE;
9993 /* Look for the `{' to start the namespace. */
9994 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9995 /* Start the namespace. */
9996 push_namespace (identifier);
9997 /* Parse the body of the namespace. */
9998 cp_parser_namespace_body (parser);
9999 /* Finish the namespace. */
10001 /* Look for the final `}'. */
10002 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10005 /* Parse a namespace-body.
10008 declaration-seq [opt] */
10011 cp_parser_namespace_body (cp_parser* parser)
10013 cp_parser_declaration_seq_opt (parser);
10016 /* Parse a namespace-alias-definition.
10018 namespace-alias-definition:
10019 namespace identifier = qualified-namespace-specifier ; */
10022 cp_parser_namespace_alias_definition (cp_parser* parser)
10025 tree namespace_specifier;
10027 /* Look for the `namespace' keyword. */
10028 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10029 /* Look for the identifier. */
10030 identifier = cp_parser_identifier (parser);
10031 if (identifier == error_mark_node)
10033 /* Look for the `=' token. */
10034 cp_parser_require (parser, CPP_EQ, "`='");
10035 /* Look for the qualified-namespace-specifier. */
10036 namespace_specifier
10037 = cp_parser_qualified_namespace_specifier (parser);
10038 /* Look for the `;' token. */
10039 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10041 /* Register the alias in the symbol table. */
10042 do_namespace_alias (identifier, namespace_specifier);
10045 /* Parse a qualified-namespace-specifier.
10047 qualified-namespace-specifier:
10048 :: [opt] nested-name-specifier [opt] namespace-name
10050 Returns a NAMESPACE_DECL corresponding to the specified
10054 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10056 /* Look for the optional `::'. */
10057 cp_parser_global_scope_opt (parser,
10058 /*current_scope_valid_p=*/false);
10060 /* Look for the optional nested-name-specifier. */
10061 cp_parser_nested_name_specifier_opt (parser,
10062 /*typename_keyword_p=*/false,
10063 /*check_dependency_p=*/true,
10065 /*is_declaration=*/true);
10067 return cp_parser_namespace_name (parser);
10070 /* Parse a using-declaration.
10073 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10074 using :: unqualified-id ; */
10077 cp_parser_using_declaration (cp_parser* parser)
10080 bool typename_p = false;
10081 bool global_scope_p;
10087 /* Look for the `using' keyword. */
10088 cp_parser_require_keyword (parser, RID_USING, "`using'");
10090 /* Peek at the next token. */
10091 token = cp_lexer_peek_token (parser->lexer);
10092 /* See if it's `typename'. */
10093 if (token->keyword == RID_TYPENAME)
10095 /* Remember that we've seen it. */
10097 /* Consume the `typename' token. */
10098 cp_lexer_consume_token (parser->lexer);
10101 /* Look for the optional global scope qualification. */
10103 = (cp_parser_global_scope_opt (parser,
10104 /*current_scope_valid_p=*/false)
10107 /* If we saw `typename', or didn't see `::', then there must be a
10108 nested-name-specifier present. */
10109 if (typename_p || !global_scope_p)
10110 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10111 /*check_dependency_p=*/true,
10113 /*is_declaration=*/true);
10114 /* Otherwise, we could be in either of the two productions. In that
10115 case, treat the nested-name-specifier as optional. */
10117 qscope = cp_parser_nested_name_specifier_opt (parser,
10118 /*typename_keyword_p=*/false,
10119 /*check_dependency_p=*/true,
10121 /*is_declaration=*/true);
10123 qscope = global_namespace;
10125 /* Parse the unqualified-id. */
10126 identifier = cp_parser_unqualified_id (parser,
10127 /*template_keyword_p=*/false,
10128 /*check_dependency_p=*/true,
10129 /*declarator_p=*/true);
10131 /* The function we call to handle a using-declaration is different
10132 depending on what scope we are in. */
10133 if (identifier == error_mark_node)
10135 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10136 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10137 /* [namespace.udecl]
10139 A using declaration shall not name a template-id. */
10140 error ("a template-id may not appear in a using-declaration");
10143 scope = current_scope ();
10144 if (scope && TYPE_P (scope))
10146 /* Create the USING_DECL. */
10147 decl = do_class_using_decl (build_nt (SCOPE_REF,
10150 /* Add it to the list of members in this class. */
10151 finish_member_declaration (decl);
10155 decl = cp_parser_lookup_name_simple (parser, identifier);
10156 if (decl == error_mark_node)
10157 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10159 do_local_using_decl (decl, qscope, identifier);
10161 do_toplevel_using_decl (decl, qscope, identifier);
10165 /* Look for the final `;'. */
10166 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10169 /* Parse a using-directive.
10172 using namespace :: [opt] nested-name-specifier [opt]
10173 namespace-name ; */
10176 cp_parser_using_directive (cp_parser* parser)
10178 tree namespace_decl;
10181 /* Look for the `using' keyword. */
10182 cp_parser_require_keyword (parser, RID_USING, "`using'");
10183 /* And the `namespace' keyword. */
10184 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10185 /* Look for the optional `::' operator. */
10186 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10187 /* And the optional nested-name-specifier. */
10188 cp_parser_nested_name_specifier_opt (parser,
10189 /*typename_keyword_p=*/false,
10190 /*check_dependency_p=*/true,
10192 /*is_declaration=*/true);
10193 /* Get the namespace being used. */
10194 namespace_decl = cp_parser_namespace_name (parser);
10195 /* And any specified attributes. */
10196 attribs = cp_parser_attributes_opt (parser);
10197 /* Update the symbol table. */
10198 parse_using_directive (namespace_decl, attribs);
10199 /* Look for the final `;'. */
10200 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10203 /* Parse an asm-definition.
10206 asm ( string-literal ) ;
10211 asm volatile [opt] ( string-literal ) ;
10212 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10213 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10214 : asm-operand-list [opt] ) ;
10215 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10216 : asm-operand-list [opt]
10217 : asm-operand-list [opt] ) ; */
10220 cp_parser_asm_definition (cp_parser* parser)
10223 tree outputs = NULL_TREE;
10224 tree inputs = NULL_TREE;
10225 tree clobbers = NULL_TREE;
10227 bool volatile_p = false;
10228 bool extended_p = false;
10230 /* Look for the `asm' keyword. */
10231 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10232 /* See if the next token is `volatile'. */
10233 if (cp_parser_allow_gnu_extensions_p (parser)
10234 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10236 /* Remember that we saw the `volatile' keyword. */
10238 /* Consume the token. */
10239 cp_lexer_consume_token (parser->lexer);
10241 /* Look for the opening `('. */
10242 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10244 /* Look for the string. */
10245 string = cp_parser_string_literal (parser, false, false);
10246 if (string == error_mark_node)
10248 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10249 /*consume_paren=*/true);
10253 /* If we're allowing GNU extensions, check for the extended assembly
10254 syntax. Unfortunately, the `:' tokens need not be separated by
10255 a space in C, and so, for compatibility, we tolerate that here
10256 too. Doing that means that we have to treat the `::' operator as
10258 if (cp_parser_allow_gnu_extensions_p (parser)
10259 && at_function_scope_p ()
10260 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10261 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10263 bool inputs_p = false;
10264 bool clobbers_p = false;
10266 /* The extended syntax was used. */
10269 /* Look for outputs. */
10270 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10272 /* Consume the `:'. */
10273 cp_lexer_consume_token (parser->lexer);
10274 /* Parse the output-operands. */
10275 if (cp_lexer_next_token_is_not (parser->lexer,
10277 && cp_lexer_next_token_is_not (parser->lexer,
10279 && cp_lexer_next_token_is_not (parser->lexer,
10281 outputs = cp_parser_asm_operand_list (parser);
10283 /* If the next token is `::', there are no outputs, and the
10284 next token is the beginning of the inputs. */
10285 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10286 /* The inputs are coming next. */
10289 /* Look for inputs. */
10291 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10293 /* Consume the `:' or `::'. */
10294 cp_lexer_consume_token (parser->lexer);
10295 /* Parse the output-operands. */
10296 if (cp_lexer_next_token_is_not (parser->lexer,
10298 && cp_lexer_next_token_is_not (parser->lexer,
10300 inputs = cp_parser_asm_operand_list (parser);
10302 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10303 /* The clobbers are coming next. */
10306 /* Look for clobbers. */
10308 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10310 /* Consume the `:' or `::'. */
10311 cp_lexer_consume_token (parser->lexer);
10312 /* Parse the clobbers. */
10313 if (cp_lexer_next_token_is_not (parser->lexer,
10315 clobbers = cp_parser_asm_clobber_list (parser);
10318 /* Look for the closing `)'. */
10319 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10320 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10321 /*consume_paren=*/true);
10322 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10324 /* Create the ASM_EXPR. */
10325 if (at_function_scope_p ())
10327 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10329 /* If the extended syntax was not used, mark the ASM_EXPR. */
10332 tree temp = asm_stmt;
10333 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10334 temp = TREE_OPERAND (temp, 0);
10336 ASM_INPUT_P (temp) = 1;
10340 assemble_asm (string);
10343 /* Declarators [gram.dcl.decl] */
10345 /* Parse an init-declarator.
10348 declarator initializer [opt]
10353 declarator asm-specification [opt] attributes [opt] initializer [opt]
10355 function-definition:
10356 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10358 decl-specifier-seq [opt] declarator function-try-block
10362 function-definition:
10363 __extension__ function-definition
10365 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10366 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10367 then this declarator appears in a class scope. The new DECL created
10368 by this declarator is returned.
10370 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10371 for a function-definition here as well. If the declarator is a
10372 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10373 be TRUE upon return. By that point, the function-definition will
10374 have been completely parsed.
10376 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10380 cp_parser_init_declarator (cp_parser* parser,
10381 cp_decl_specifier_seq *decl_specifiers,
10382 bool function_definition_allowed_p,
10384 int declares_class_or_enum,
10385 bool* function_definition_p)
10388 cp_declarator *declarator;
10389 tree prefix_attributes;
10391 tree asm_specification;
10393 tree decl = NULL_TREE;
10395 bool is_initialized;
10396 bool is_parenthesized_init;
10397 bool is_non_constant_init;
10398 int ctor_dtor_or_conv_p;
10400 bool pop_p = false;
10402 /* Gather the attributes that were provided with the
10403 decl-specifiers. */
10404 prefix_attributes = decl_specifiers->attributes;
10406 /* Assume that this is not the declarator for a function
10408 if (function_definition_p)
10409 *function_definition_p = false;
10411 /* Defer access checks while parsing the declarator; we cannot know
10412 what names are accessible until we know what is being
10414 resume_deferring_access_checks ();
10416 /* Parse the declarator. */
10418 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10419 &ctor_dtor_or_conv_p,
10420 /*parenthesized_p=*/NULL,
10421 /*member_p=*/false);
10422 /* Gather up the deferred checks. */
10423 stop_deferring_access_checks ();
10425 /* If the DECLARATOR was erroneous, there's no need to go
10427 if (declarator == cp_error_declarator)
10428 return error_mark_node;
10430 cp_parser_check_for_definition_in_return_type (declarator,
10431 declares_class_or_enum);
10433 /* Figure out what scope the entity declared by the DECLARATOR is
10434 located in. `grokdeclarator' sometimes changes the scope, so
10435 we compute it now. */
10436 scope = get_scope_of_declarator (declarator);
10438 /* If we're allowing GNU extensions, look for an asm-specification
10440 if (cp_parser_allow_gnu_extensions_p (parser))
10442 /* Look for an asm-specification. */
10443 asm_specification = cp_parser_asm_specification_opt (parser);
10444 /* And attributes. */
10445 attributes = cp_parser_attributes_opt (parser);
10449 asm_specification = NULL_TREE;
10450 attributes = NULL_TREE;
10453 /* Peek at the next token. */
10454 token = cp_lexer_peek_token (parser->lexer);
10455 /* Check to see if the token indicates the start of a
10456 function-definition. */
10457 if (cp_parser_token_starts_function_definition_p (token))
10459 if (!function_definition_allowed_p)
10461 /* If a function-definition should not appear here, issue an
10463 cp_parser_error (parser,
10464 "a function-definition is not allowed here");
10465 return error_mark_node;
10469 /* Neither attributes nor an asm-specification are allowed
10470 on a function-definition. */
10471 if (asm_specification)
10472 error ("an asm-specification is not allowed on a function-definition");
10474 error ("attributes are not allowed on a function-definition");
10475 /* This is a function-definition. */
10476 *function_definition_p = true;
10478 /* Parse the function definition. */
10480 decl = cp_parser_save_member_function_body (parser,
10483 prefix_attributes);
10486 = (cp_parser_function_definition_from_specifiers_and_declarator
10487 (parser, decl_specifiers, prefix_attributes, declarator));
10495 Only in function declarations for constructors, destructors, and
10496 type conversions can the decl-specifier-seq be omitted.
10498 We explicitly postpone this check past the point where we handle
10499 function-definitions because we tolerate function-definitions
10500 that are missing their return types in some modes. */
10501 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10503 cp_parser_error (parser,
10504 "expected constructor, destructor, or type conversion");
10505 return error_mark_node;
10508 /* An `=' or an `(' indicates an initializer. */
10509 is_initialized = (token->type == CPP_EQ
10510 || token->type == CPP_OPEN_PAREN);
10511 /* If the init-declarator isn't initialized and isn't followed by a
10512 `,' or `;', it's not a valid init-declarator. */
10513 if (!is_initialized
10514 && token->type != CPP_COMMA
10515 && token->type != CPP_SEMICOLON)
10517 cp_parser_error (parser, "expected initializer");
10518 return error_mark_node;
10521 /* Because start_decl has side-effects, we should only call it if we
10522 know we're going ahead. By this point, we know that we cannot
10523 possibly be looking at any other construct. */
10524 cp_parser_commit_to_tentative_parse (parser);
10526 /* If the decl specifiers were bad, issue an error now that we're
10527 sure this was intended to be a declarator. Then continue
10528 declaring the variable(s), as int, to try to cut down on further
10530 if (decl_specifiers->any_specifiers_p
10531 && decl_specifiers->type == error_mark_node)
10533 cp_parser_error (parser, "invalid type in declaration");
10534 decl_specifiers->type = integer_type_node;
10537 /* Check to see whether or not this declaration is a friend. */
10538 friend_p = cp_parser_friend_p (decl_specifiers);
10540 /* Check that the number of template-parameter-lists is OK. */
10541 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10542 return error_mark_node;
10544 /* Enter the newly declared entry in the symbol table. If we're
10545 processing a declaration in a class-specifier, we wait until
10546 after processing the initializer. */
10549 if (parser->in_unbraced_linkage_specification_p)
10551 decl_specifiers->storage_class = sc_extern;
10552 have_extern_spec = false;
10554 decl = start_decl (declarator, decl_specifiers,
10555 is_initialized, attributes, prefix_attributes,
10559 /* Enter the SCOPE. That way unqualified names appearing in the
10560 initializer will be looked up in SCOPE. */
10561 pop_p = push_scope (scope);
10563 /* Perform deferred access control checks, now that we know in which
10564 SCOPE the declared entity resides. */
10565 if (!member_p && decl)
10567 tree saved_current_function_decl = NULL_TREE;
10569 /* If the entity being declared is a function, pretend that we
10570 are in its scope. If it is a `friend', it may have access to
10571 things that would not otherwise be accessible. */
10572 if (TREE_CODE (decl) == FUNCTION_DECL)
10574 saved_current_function_decl = current_function_decl;
10575 current_function_decl = decl;
10578 /* Perform the access control checks for the declarator and the
10579 the decl-specifiers. */
10580 perform_deferred_access_checks ();
10582 /* Restore the saved value. */
10583 if (TREE_CODE (decl) == FUNCTION_DECL)
10584 current_function_decl = saved_current_function_decl;
10587 /* Parse the initializer. */
10588 if (is_initialized)
10589 initializer = cp_parser_initializer (parser,
10590 &is_parenthesized_init,
10591 &is_non_constant_init);
10594 initializer = NULL_TREE;
10595 is_parenthesized_init = false;
10596 is_non_constant_init = true;
10599 /* The old parser allows attributes to appear after a parenthesized
10600 initializer. Mark Mitchell proposed removing this functionality
10601 on the GCC mailing lists on 2002-08-13. This parser accepts the
10602 attributes -- but ignores them. */
10603 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10604 if (cp_parser_attributes_opt (parser))
10605 warning ("attributes after parenthesized initializer ignored");
10607 /* For an in-class declaration, use `grokfield' to create the
10613 decl = grokfield (declarator, decl_specifiers,
10614 initializer, /*asmspec=*/NULL_TREE,
10615 /*attributes=*/NULL_TREE);
10616 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10617 cp_parser_save_default_args (parser, decl);
10620 /* Finish processing the declaration. But, skip friend
10622 if (!friend_p && decl && decl != error_mark_node)
10624 cp_finish_decl (decl,
10627 /* If the initializer is in parentheses, then this is
10628 a direct-initialization, which means that an
10629 `explicit' constructor is OK. Otherwise, an
10630 `explicit' constructor cannot be used. */
10631 ((is_parenthesized_init || !is_initialized)
10632 ? 0 : LOOKUP_ONLYCONVERTING));
10634 pop_scope (DECL_CONTEXT (decl));
10637 /* Remember whether or not variables were initialized by
10638 constant-expressions. */
10639 if (decl && TREE_CODE (decl) == VAR_DECL
10640 && is_initialized && !is_non_constant_init)
10641 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10646 /* Parse a declarator.
10650 ptr-operator declarator
10652 abstract-declarator:
10653 ptr-operator abstract-declarator [opt]
10654 direct-abstract-declarator
10659 attributes [opt] direct-declarator
10660 attributes [opt] ptr-operator declarator
10662 abstract-declarator:
10663 attributes [opt] ptr-operator abstract-declarator [opt]
10664 attributes [opt] direct-abstract-declarator
10666 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10667 detect constructor, destructor or conversion operators. It is set
10668 to -1 if the declarator is a name, and +1 if it is a
10669 function. Otherwise it is set to zero. Usually you just want to
10670 test for >0, but internally the negative value is used.
10672 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10673 a decl-specifier-seq unless it declares a constructor, destructor,
10674 or conversion. It might seem that we could check this condition in
10675 semantic analysis, rather than parsing, but that makes it difficult
10676 to handle something like `f()'. We want to notice that there are
10677 no decl-specifiers, and therefore realize that this is an
10678 expression, not a declaration.)
10680 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10681 the declarator is a direct-declarator of the form "(...)".
10683 MEMBER_P is true iff this declarator is a member-declarator. */
10685 static cp_declarator *
10686 cp_parser_declarator (cp_parser* parser,
10687 cp_parser_declarator_kind dcl_kind,
10688 int* ctor_dtor_or_conv_p,
10689 bool* parenthesized_p,
10693 cp_declarator *declarator;
10694 enum tree_code code;
10695 cp_cv_quals cv_quals;
10697 tree attributes = NULL_TREE;
10699 /* Assume this is not a constructor, destructor, or type-conversion
10701 if (ctor_dtor_or_conv_p)
10702 *ctor_dtor_or_conv_p = 0;
10704 if (cp_parser_allow_gnu_extensions_p (parser))
10705 attributes = cp_parser_attributes_opt (parser);
10707 /* Peek at the next token. */
10708 token = cp_lexer_peek_token (parser->lexer);
10710 /* Check for the ptr-operator production. */
10711 cp_parser_parse_tentatively (parser);
10712 /* Parse the ptr-operator. */
10713 code = cp_parser_ptr_operator (parser,
10716 /* If that worked, then we have a ptr-operator. */
10717 if (cp_parser_parse_definitely (parser))
10719 /* If a ptr-operator was found, then this declarator was not
10721 if (parenthesized_p)
10722 *parenthesized_p = true;
10723 /* The dependent declarator is optional if we are parsing an
10724 abstract-declarator. */
10725 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10726 cp_parser_parse_tentatively (parser);
10728 /* Parse the dependent declarator. */
10729 declarator = cp_parser_declarator (parser, dcl_kind,
10730 /*ctor_dtor_or_conv_p=*/NULL,
10731 /*parenthesized_p=*/NULL,
10732 /*member_p=*/false);
10734 /* If we are parsing an abstract-declarator, we must handle the
10735 case where the dependent declarator is absent. */
10736 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10737 && !cp_parser_parse_definitely (parser))
10740 /* Build the representation of the ptr-operator. */
10742 declarator = make_ptrmem_declarator (cv_quals,
10745 else if (code == INDIRECT_REF)
10746 declarator = make_pointer_declarator (cv_quals, declarator);
10748 declarator = make_reference_declarator (cv_quals, declarator);
10750 /* Everything else is a direct-declarator. */
10753 if (parenthesized_p)
10754 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10756 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10757 ctor_dtor_or_conv_p,
10761 if (attributes && declarator != cp_error_declarator)
10762 declarator->attributes = attributes;
10767 /* Parse a direct-declarator or direct-abstract-declarator.
10771 direct-declarator ( parameter-declaration-clause )
10772 cv-qualifier-seq [opt]
10773 exception-specification [opt]
10774 direct-declarator [ constant-expression [opt] ]
10777 direct-abstract-declarator:
10778 direct-abstract-declarator [opt]
10779 ( parameter-declaration-clause )
10780 cv-qualifier-seq [opt]
10781 exception-specification [opt]
10782 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10783 ( abstract-declarator )
10785 Returns a representation of the declarator. DCL_KIND is
10786 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10787 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10788 we are parsing a direct-declarator. It is
10789 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10790 of ambiguity we prefer an abstract declarator, as per
10791 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10792 cp_parser_declarator. */
10794 static cp_declarator *
10795 cp_parser_direct_declarator (cp_parser* parser,
10796 cp_parser_declarator_kind dcl_kind,
10797 int* ctor_dtor_or_conv_p,
10801 cp_declarator *declarator = NULL;
10802 tree scope = NULL_TREE;
10803 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10804 bool saved_in_declarator_p = parser->in_declarator_p;
10806 bool pop_p = false;
10810 /* Peek at the next token. */
10811 token = cp_lexer_peek_token (parser->lexer);
10812 if (token->type == CPP_OPEN_PAREN)
10814 /* This is either a parameter-declaration-clause, or a
10815 parenthesized declarator. When we know we are parsing a
10816 named declarator, it must be a parenthesized declarator
10817 if FIRST is true. For instance, `(int)' is a
10818 parameter-declaration-clause, with an omitted
10819 direct-abstract-declarator. But `((*))', is a
10820 parenthesized abstract declarator. Finally, when T is a
10821 template parameter `(T)' is a
10822 parameter-declaration-clause, and not a parenthesized
10825 We first try and parse a parameter-declaration-clause,
10826 and then try a nested declarator (if FIRST is true).
10828 It is not an error for it not to be a
10829 parameter-declaration-clause, even when FIRST is
10835 The first is the declaration of a function while the
10836 second is a the definition of a variable, including its
10839 Having seen only the parenthesis, we cannot know which of
10840 these two alternatives should be selected. Even more
10841 complex are examples like:
10846 The former is a function-declaration; the latter is a
10847 variable initialization.
10849 Thus again, we try a parameter-declaration-clause, and if
10850 that fails, we back out and return. */
10852 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10854 cp_parameter_declarator *params;
10855 unsigned saved_num_template_parameter_lists;
10857 /* In a member-declarator, the only valid interpretation
10858 of a parenthesis is the start of a
10859 parameter-declaration-clause. (It is invalid to
10860 initialize a static data member with a parenthesized
10861 initializer; only the "=" form of initialization is
10864 cp_parser_parse_tentatively (parser);
10866 /* Consume the `('. */
10867 cp_lexer_consume_token (parser->lexer);
10870 /* If this is going to be an abstract declarator, we're
10871 in a declarator and we can't have default args. */
10872 parser->default_arg_ok_p = false;
10873 parser->in_declarator_p = true;
10876 /* Inside the function parameter list, surrounding
10877 template-parameter-lists do not apply. */
10878 saved_num_template_parameter_lists
10879 = parser->num_template_parameter_lists;
10880 parser->num_template_parameter_lists = 0;
10882 /* Parse the parameter-declaration-clause. */
10883 params = cp_parser_parameter_declaration_clause (parser);
10885 parser->num_template_parameter_lists
10886 = saved_num_template_parameter_lists;
10888 /* If all went well, parse the cv-qualifier-seq and the
10889 exception-specification. */
10890 if (member_p || cp_parser_parse_definitely (parser))
10892 cp_cv_quals cv_quals;
10893 tree exception_specification;
10895 if (ctor_dtor_or_conv_p)
10896 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10898 /* Consume the `)'. */
10899 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10901 /* Parse the cv-qualifier-seq. */
10902 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10903 /* And the exception-specification. */
10904 exception_specification
10905 = cp_parser_exception_specification_opt (parser);
10907 /* Create the function-declarator. */
10908 declarator = make_call_declarator (declarator,
10911 exception_specification);
10912 /* Any subsequent parameter lists are to do with
10913 return type, so are not those of the declared
10915 parser->default_arg_ok_p = false;
10917 /* Repeat the main loop. */
10922 /* If this is the first, we can try a parenthesized
10926 bool saved_in_type_id_in_expr_p;
10928 parser->default_arg_ok_p = saved_default_arg_ok_p;
10929 parser->in_declarator_p = saved_in_declarator_p;
10931 /* Consume the `('. */
10932 cp_lexer_consume_token (parser->lexer);
10933 /* Parse the nested declarator. */
10934 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10935 parser->in_type_id_in_expr_p = true;
10937 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10938 /*parenthesized_p=*/NULL,
10940 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10942 /* Expect a `)'. */
10943 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10944 declarator = cp_error_declarator;
10945 if (declarator == cp_error_declarator)
10948 goto handle_declarator;
10950 /* Otherwise, we must be done. */
10954 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10955 && token->type == CPP_OPEN_SQUARE)
10957 /* Parse an array-declarator. */
10960 if (ctor_dtor_or_conv_p)
10961 *ctor_dtor_or_conv_p = 0;
10964 parser->default_arg_ok_p = false;
10965 parser->in_declarator_p = true;
10966 /* Consume the `['. */
10967 cp_lexer_consume_token (parser->lexer);
10968 /* Peek at the next token. */
10969 token = cp_lexer_peek_token (parser->lexer);
10970 /* If the next token is `]', then there is no
10971 constant-expression. */
10972 if (token->type != CPP_CLOSE_SQUARE)
10974 bool non_constant_p;
10977 = cp_parser_constant_expression (parser,
10978 /*allow_non_constant=*/true,
10980 if (!non_constant_p)
10981 bounds = fold_non_dependent_expr (bounds);
10984 bounds = NULL_TREE;
10985 /* Look for the closing `]'. */
10986 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10988 declarator = cp_error_declarator;
10992 declarator = make_array_declarator (declarator, bounds);
10994 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10998 /* Parse a declarator-id */
10999 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11000 cp_parser_parse_tentatively (parser);
11001 id = cp_parser_declarator_id (parser);
11002 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11004 if (!cp_parser_parse_definitely (parser))
11005 id = error_mark_node;
11006 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11008 cp_parser_error (parser, "expected unqualified-id");
11009 id = error_mark_node;
11013 if (id == error_mark_node)
11015 declarator = cp_error_declarator;
11019 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11021 tree scope = TREE_OPERAND (id, 0);
11023 /* In the declaration of a member of a template class
11024 outside of the class itself, the SCOPE will sometimes
11025 be a TYPENAME_TYPE. For example, given:
11027 template <typename T>
11028 int S<T>::R::i = 3;
11030 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11031 this context, we must resolve S<T>::R to an ordinary
11032 type, rather than a typename type.
11034 The reason we normally avoid resolving TYPENAME_TYPEs
11035 is that a specialization of `S' might render
11036 `S<T>::R' not a type. However, if `S' is
11037 specialized, then this `i' will not be used, so there
11038 is no harm in resolving the types here. */
11039 if (TREE_CODE (scope) == TYPENAME_TYPE)
11043 /* Resolve the TYPENAME_TYPE. */
11044 type = resolve_typename_type (scope,
11045 /*only_current_p=*/false);
11046 /* If that failed, the declarator is invalid. */
11047 if (type == error_mark_node)
11048 error ("%<%T::%D%> is not a type",
11049 TYPE_CONTEXT (scope),
11050 TYPE_IDENTIFIER (scope));
11051 /* Build a new DECLARATOR. */
11052 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11056 declarator = make_id_declarator (id);
11060 tree unqualified_name;
11062 if (TREE_CODE (id) == SCOPE_REF
11063 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11065 class_type = TREE_OPERAND (id, 0);
11066 unqualified_name = TREE_OPERAND (id, 1);
11070 class_type = current_class_type;
11071 unqualified_name = id;
11076 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11077 declarator->u.id.sfk = sfk_destructor;
11078 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11079 declarator->u.id.sfk = sfk_conversion;
11080 else if (constructor_name_p (unqualified_name,
11082 || (TREE_CODE (unqualified_name) == TYPE_DECL
11083 && same_type_p (TREE_TYPE (unqualified_name),
11085 declarator->u.id.sfk = sfk_constructor;
11087 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11088 *ctor_dtor_or_conv_p = -1;
11089 if (TREE_CODE (id) == SCOPE_REF
11090 && TREE_CODE (unqualified_name) == TYPE_DECL
11091 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11093 error ("invalid use of constructor as a template");
11094 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11095 "the constructor in a qualified name",
11097 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11098 class_type, class_type);
11103 handle_declarator:;
11104 scope = get_scope_of_declarator (declarator);
11106 /* Any names that appear after the declarator-id for a
11107 member are looked up in the containing scope. */
11108 pop_p = push_scope (scope);
11109 parser->in_declarator_p = true;
11110 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11111 || (declarator && declarator->kind == cdk_id))
11112 /* Default args are only allowed on function
11114 parser->default_arg_ok_p = saved_default_arg_ok_p;
11116 parser->default_arg_ok_p = false;
11125 /* For an abstract declarator, we might wind up with nothing at this
11126 point. That's an error; the declarator is not optional. */
11128 cp_parser_error (parser, "expected declarator");
11130 /* If we entered a scope, we must exit it now. */
11134 parser->default_arg_ok_p = saved_default_arg_ok_p;
11135 parser->in_declarator_p = saved_in_declarator_p;
11140 /* Parse a ptr-operator.
11143 * cv-qualifier-seq [opt]
11145 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11150 & cv-qualifier-seq [opt]
11152 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11153 Returns ADDR_EXPR if a reference was used. In the case of a
11154 pointer-to-member, *TYPE is filled in with the TYPE containing the
11155 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11156 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11157 ERROR_MARK if an error occurred. */
11159 static enum tree_code
11160 cp_parser_ptr_operator (cp_parser* parser,
11162 cp_cv_quals *cv_quals)
11164 enum tree_code code = ERROR_MARK;
11167 /* Assume that it's not a pointer-to-member. */
11169 /* And that there are no cv-qualifiers. */
11170 *cv_quals = TYPE_UNQUALIFIED;
11172 /* Peek at the next token. */
11173 token = cp_lexer_peek_token (parser->lexer);
11174 /* If it's a `*' or `&' we have a pointer or reference. */
11175 if (token->type == CPP_MULT || token->type == CPP_AND)
11177 /* Remember which ptr-operator we were processing. */
11178 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11180 /* Consume the `*' or `&'. */
11181 cp_lexer_consume_token (parser->lexer);
11183 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11184 `&', if we are allowing GNU extensions. (The only qualifier
11185 that can legally appear after `&' is `restrict', but that is
11186 enforced during semantic analysis. */
11187 if (code == INDIRECT_REF
11188 || cp_parser_allow_gnu_extensions_p (parser))
11189 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11193 /* Try the pointer-to-member case. */
11194 cp_parser_parse_tentatively (parser);
11195 /* Look for the optional `::' operator. */
11196 cp_parser_global_scope_opt (parser,
11197 /*current_scope_valid_p=*/false);
11198 /* Look for the nested-name specifier. */
11199 cp_parser_nested_name_specifier (parser,
11200 /*typename_keyword_p=*/false,
11201 /*check_dependency_p=*/true,
11203 /*is_declaration=*/false);
11204 /* If we found it, and the next token is a `*', then we are
11205 indeed looking at a pointer-to-member operator. */
11206 if (!cp_parser_error_occurred (parser)
11207 && cp_parser_require (parser, CPP_MULT, "`*'"))
11209 /* The type of which the member is a member is given by the
11211 *type = parser->scope;
11212 /* The next name will not be qualified. */
11213 parser->scope = NULL_TREE;
11214 parser->qualifying_scope = NULL_TREE;
11215 parser->object_scope = NULL_TREE;
11216 /* Indicate that the `*' operator was used. */
11217 code = INDIRECT_REF;
11218 /* Look for the optional cv-qualifier-seq. */
11219 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11221 /* If that didn't work we don't have a ptr-operator. */
11222 if (!cp_parser_parse_definitely (parser))
11223 cp_parser_error (parser, "expected ptr-operator");
11229 /* Parse an (optional) cv-qualifier-seq.
11232 cv-qualifier cv-qualifier-seq [opt]
11243 Returns a bitmask representing the cv-qualifiers. */
11246 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11248 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11253 cp_cv_quals cv_qualifier;
11255 /* Peek at the next token. */
11256 token = cp_lexer_peek_token (parser->lexer);
11257 /* See if it's a cv-qualifier. */
11258 switch (token->keyword)
11261 cv_qualifier = TYPE_QUAL_CONST;
11265 cv_qualifier = TYPE_QUAL_VOLATILE;
11269 cv_qualifier = TYPE_QUAL_RESTRICT;
11273 cv_qualifier = TYPE_UNQUALIFIED;
11280 if (cv_quals & cv_qualifier)
11282 error ("duplicate cv-qualifier");
11283 cp_lexer_purge_token (parser->lexer);
11287 cp_lexer_consume_token (parser->lexer);
11288 cv_quals |= cv_qualifier;
11295 /* Parse a declarator-id.
11299 :: [opt] nested-name-specifier [opt] type-name
11301 In the `id-expression' case, the value returned is as for
11302 cp_parser_id_expression if the id-expression was an unqualified-id.
11303 If the id-expression was a qualified-id, then a SCOPE_REF is
11304 returned. The first operand is the scope (either a NAMESPACE_DECL
11305 or TREE_TYPE), but the second is still just a representation of an
11309 cp_parser_declarator_id (cp_parser* parser)
11311 tree id_expression;
11313 /* The expression must be an id-expression. Assume that qualified
11314 names are the names of types so that:
11317 int S<T>::R::i = 3;
11319 will work; we must treat `S<T>::R' as the name of a type.
11320 Similarly, assume that qualified names are templates, where
11324 int S<T>::R<T>::i = 3;
11327 id_expression = cp_parser_id_expression (parser,
11328 /*template_keyword_p=*/false,
11329 /*check_dependency_p=*/false,
11330 /*template_p=*/NULL,
11331 /*declarator_p=*/true);
11332 /* If the name was qualified, create a SCOPE_REF to represent
11336 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11337 parser->scope = NULL_TREE;
11340 return id_expression;
11343 /* Parse a type-id.
11346 type-specifier-seq abstract-declarator [opt]
11348 Returns the TYPE specified. */
11351 cp_parser_type_id (cp_parser* parser)
11353 cp_decl_specifier_seq type_specifier_seq;
11354 cp_declarator *abstract_declarator;
11356 /* Parse the type-specifier-seq. */
11357 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11358 if (type_specifier_seq.type == error_mark_node)
11359 return error_mark_node;
11361 /* There might or might not be an abstract declarator. */
11362 cp_parser_parse_tentatively (parser);
11363 /* Look for the declarator. */
11364 abstract_declarator
11365 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11366 /*parenthesized_p=*/NULL,
11367 /*member_p=*/false);
11368 /* Check to see if there really was a declarator. */
11369 if (!cp_parser_parse_definitely (parser))
11370 abstract_declarator = NULL;
11372 return groktypename (&type_specifier_seq, abstract_declarator);
11375 /* Parse a type-specifier-seq.
11377 type-specifier-seq:
11378 type-specifier type-specifier-seq [opt]
11382 type-specifier-seq:
11383 attributes type-specifier-seq [opt]
11385 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11388 cp_parser_type_specifier_seq (cp_parser* parser,
11389 cp_decl_specifier_seq *type_specifier_seq)
11391 bool seen_type_specifier = false;
11393 /* Clear the TYPE_SPECIFIER_SEQ. */
11394 clear_decl_specs (type_specifier_seq);
11396 /* Parse the type-specifiers and attributes. */
11399 tree type_specifier;
11401 /* Check for attributes first. */
11402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11404 type_specifier_seq->attributes =
11405 chainon (type_specifier_seq->attributes,
11406 cp_parser_attributes_opt (parser));
11410 /* Look for the type-specifier. */
11411 type_specifier = cp_parser_type_specifier (parser,
11412 CP_PARSER_FLAGS_OPTIONAL,
11413 type_specifier_seq,
11414 /*is_declaration=*/false,
11417 /* If the first type-specifier could not be found, this is not a
11418 type-specifier-seq at all. */
11419 if (!seen_type_specifier && !type_specifier)
11421 cp_parser_error (parser, "expected type-specifier");
11422 type_specifier_seq->type = error_mark_node;
11425 /* If subsequent type-specifiers could not be found, the
11426 type-specifier-seq is complete. */
11427 else if (seen_type_specifier && !type_specifier)
11430 seen_type_specifier = true;
11436 /* Parse a parameter-declaration-clause.
11438 parameter-declaration-clause:
11439 parameter-declaration-list [opt] ... [opt]
11440 parameter-declaration-list , ...
11442 Returns a representation for the parameter declarations. A return
11443 value of NULL indicates a parameter-declaration-clause consisting
11444 only of an ellipsis. */
11446 static cp_parameter_declarator *
11447 cp_parser_parameter_declaration_clause (cp_parser* parser)
11449 cp_parameter_declarator *parameters;
11454 /* Peek at the next token. */
11455 token = cp_lexer_peek_token (parser->lexer);
11456 /* Check for trivial parameter-declaration-clauses. */
11457 if (token->type == CPP_ELLIPSIS)
11459 /* Consume the `...' token. */
11460 cp_lexer_consume_token (parser->lexer);
11463 else if (token->type == CPP_CLOSE_PAREN)
11464 /* There are no parameters. */
11466 #ifndef NO_IMPLICIT_EXTERN_C
11467 if (in_system_header && current_class_type == NULL
11468 && current_lang_name == lang_name_c)
11472 return no_parameters;
11474 /* Check for `(void)', too, which is a special case. */
11475 else if (token->keyword == RID_VOID
11476 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11477 == CPP_CLOSE_PAREN))
11479 /* Consume the `void' token. */
11480 cp_lexer_consume_token (parser->lexer);
11481 /* There are no parameters. */
11482 return no_parameters;
11485 /* Parse the parameter-declaration-list. */
11486 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11487 /* If a parse error occurred while parsing the
11488 parameter-declaration-list, then the entire
11489 parameter-declaration-clause is erroneous. */
11493 /* Peek at the next token. */
11494 token = cp_lexer_peek_token (parser->lexer);
11495 /* If it's a `,', the clause should terminate with an ellipsis. */
11496 if (token->type == CPP_COMMA)
11498 /* Consume the `,'. */
11499 cp_lexer_consume_token (parser->lexer);
11500 /* Expect an ellipsis. */
11502 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11504 /* It might also be `...' if the optional trailing `,' was
11506 else if (token->type == CPP_ELLIPSIS)
11508 /* Consume the `...' token. */
11509 cp_lexer_consume_token (parser->lexer);
11510 /* And remember that we saw it. */
11514 ellipsis_p = false;
11516 /* Finish the parameter list. */
11517 if (parameters && ellipsis_p)
11518 parameters->ellipsis_p = true;
11523 /* Parse a parameter-declaration-list.
11525 parameter-declaration-list:
11526 parameter-declaration
11527 parameter-declaration-list , parameter-declaration
11529 Returns a representation of the parameter-declaration-list, as for
11530 cp_parser_parameter_declaration_clause. However, the
11531 `void_list_node' is never appended to the list. Upon return,
11532 *IS_ERROR will be true iff an error occurred. */
11534 static cp_parameter_declarator *
11535 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11537 cp_parameter_declarator *parameters = NULL;
11538 cp_parameter_declarator **tail = ¶meters;
11540 /* Assume all will go well. */
11543 /* Look for more parameters. */
11546 cp_parameter_declarator *parameter;
11547 bool parenthesized_p;
11548 /* Parse the parameter. */
11550 = cp_parser_parameter_declaration (parser,
11551 /*template_parm_p=*/false,
11554 /* If a parse error occurred parsing the parameter declaration,
11555 then the entire parameter-declaration-list is erroneous. */
11562 /* Add the new parameter to the list. */
11564 tail = ¶meter->next;
11566 /* Peek at the next token. */
11567 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11568 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11569 /* The parameter-declaration-list is complete. */
11571 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11575 /* Peek at the next token. */
11576 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11577 /* If it's an ellipsis, then the list is complete. */
11578 if (token->type == CPP_ELLIPSIS)
11580 /* Otherwise, there must be more parameters. Consume the
11582 cp_lexer_consume_token (parser->lexer);
11583 /* When parsing something like:
11585 int i(float f, double d)
11587 we can tell after seeing the declaration for "f" that we
11588 are not looking at an initialization of a variable "i",
11589 but rather at the declaration of a function "i".
11591 Due to the fact that the parsing of template arguments
11592 (as specified to a template-id) requires backtracking we
11593 cannot use this technique when inside a template argument
11595 if (!parser->in_template_argument_list_p
11596 && !parser->in_type_id_in_expr_p
11597 && cp_parser_parsing_tentatively (parser)
11598 && !cp_parser_committed_to_tentative_parse (parser)
11599 /* However, a parameter-declaration of the form
11600 "foat(f)" (which is a valid declaration of a
11601 parameter "f") can also be interpreted as an
11602 expression (the conversion of "f" to "float"). */
11603 && !parenthesized_p)
11604 cp_parser_commit_to_tentative_parse (parser);
11608 cp_parser_error (parser, "expected %<,%> or %<...%>");
11609 if (!cp_parser_parsing_tentatively (parser)
11610 || cp_parser_committed_to_tentative_parse (parser))
11611 cp_parser_skip_to_closing_parenthesis (parser,
11612 /*recovering=*/true,
11613 /*or_comma=*/false,
11614 /*consume_paren=*/false);
11622 /* Parse a parameter declaration.
11624 parameter-declaration:
11625 decl-specifier-seq declarator
11626 decl-specifier-seq declarator = assignment-expression
11627 decl-specifier-seq abstract-declarator [opt]
11628 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11630 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11631 declares a template parameter. (In that case, a non-nested `>'
11632 token encountered during the parsing of the assignment-expression
11633 is not interpreted as a greater-than operator.)
11635 Returns a representation of the parameter, or NULL if an error
11636 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11637 true iff the declarator is of the form "(p)". */
11639 static cp_parameter_declarator *
11640 cp_parser_parameter_declaration (cp_parser *parser,
11641 bool template_parm_p,
11642 bool *parenthesized_p)
11644 int declares_class_or_enum;
11645 bool greater_than_is_operator_p;
11646 cp_decl_specifier_seq decl_specifiers;
11647 cp_declarator *declarator;
11648 tree default_argument;
11650 const char *saved_message;
11652 /* In a template parameter, `>' is not an operator.
11656 When parsing a default template-argument for a non-type
11657 template-parameter, the first non-nested `>' is taken as the end
11658 of the template parameter-list rather than a greater-than
11660 greater_than_is_operator_p = !template_parm_p;
11662 /* Type definitions may not appear in parameter types. */
11663 saved_message = parser->type_definition_forbidden_message;
11664 parser->type_definition_forbidden_message
11665 = "types may not be defined in parameter types";
11667 /* Parse the declaration-specifiers. */
11668 cp_parser_decl_specifier_seq (parser,
11669 CP_PARSER_FLAGS_NONE,
11671 &declares_class_or_enum);
11672 /* If an error occurred, there's no reason to attempt to parse the
11673 rest of the declaration. */
11674 if (cp_parser_error_occurred (parser))
11676 parser->type_definition_forbidden_message = saved_message;
11680 /* Peek at the next token. */
11681 token = cp_lexer_peek_token (parser->lexer);
11682 /* If the next token is a `)', `,', `=', `>', or `...', then there
11683 is no declarator. */
11684 if (token->type == CPP_CLOSE_PAREN
11685 || token->type == CPP_COMMA
11686 || token->type == CPP_EQ
11687 || token->type == CPP_ELLIPSIS
11688 || token->type == CPP_GREATER)
11691 if (parenthesized_p)
11692 *parenthesized_p = false;
11694 /* Otherwise, there should be a declarator. */
11697 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11698 parser->default_arg_ok_p = false;
11700 /* After seeing a decl-specifier-seq, if the next token is not a
11701 "(", there is no possibility that the code is a valid
11702 expression. Therefore, if parsing tentatively, we commit at
11704 if (!parser->in_template_argument_list_p
11705 /* In an expression context, having seen:
11709 we cannot be sure whether we are looking at a
11710 function-type (taking a "char" as a parameter) or a cast
11711 of some object of type "char" to "int". */
11712 && !parser->in_type_id_in_expr_p
11713 && cp_parser_parsing_tentatively (parser)
11714 && !cp_parser_committed_to_tentative_parse (parser)
11715 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11716 cp_parser_commit_to_tentative_parse (parser);
11717 /* Parse the declarator. */
11718 declarator = cp_parser_declarator (parser,
11719 CP_PARSER_DECLARATOR_EITHER,
11720 /*ctor_dtor_or_conv_p=*/NULL,
11722 /*member_p=*/false);
11723 parser->default_arg_ok_p = saved_default_arg_ok_p;
11724 /* After the declarator, allow more attributes. */
11725 decl_specifiers.attributes
11726 = chainon (decl_specifiers.attributes,
11727 cp_parser_attributes_opt (parser));
11730 /* The restriction on defining new types applies only to the type
11731 of the parameter, not to the default argument. */
11732 parser->type_definition_forbidden_message = saved_message;
11734 /* If the next token is `=', then process a default argument. */
11735 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11737 bool saved_greater_than_is_operator_p;
11738 /* Consume the `='. */
11739 cp_lexer_consume_token (parser->lexer);
11741 /* If we are defining a class, then the tokens that make up the
11742 default argument must be saved and processed later. */
11743 if (!template_parm_p && at_class_scope_p ()
11744 && TYPE_BEING_DEFINED (current_class_type))
11746 unsigned depth = 0;
11747 cp_token *first_token;
11750 /* Add tokens until we have processed the entire default
11751 argument. We add the range [first_token, token). */
11752 first_token = cp_lexer_peek_token (parser->lexer);
11757 /* Peek at the next token. */
11758 token = cp_lexer_peek_token (parser->lexer);
11759 /* What we do depends on what token we have. */
11760 switch (token->type)
11762 /* In valid code, a default argument must be
11763 immediately followed by a `,' `)', or `...'. */
11765 case CPP_CLOSE_PAREN:
11767 /* If we run into a non-nested `;', `}', or `]',
11768 then the code is invalid -- but the default
11769 argument is certainly over. */
11770 case CPP_SEMICOLON:
11771 case CPP_CLOSE_BRACE:
11772 case CPP_CLOSE_SQUARE:
11775 /* Update DEPTH, if necessary. */
11776 else if (token->type == CPP_CLOSE_PAREN
11777 || token->type == CPP_CLOSE_BRACE
11778 || token->type == CPP_CLOSE_SQUARE)
11782 case CPP_OPEN_PAREN:
11783 case CPP_OPEN_SQUARE:
11784 case CPP_OPEN_BRACE:
11789 /* If we see a non-nested `>', and `>' is not an
11790 operator, then it marks the end of the default
11792 if (!depth && !greater_than_is_operator_p)
11796 /* If we run out of tokens, issue an error message. */
11798 error ("file ends in default argument");
11804 /* In these cases, we should look for template-ids.
11805 For example, if the default argument is
11806 `X<int, double>()', we need to do name lookup to
11807 figure out whether or not `X' is a template; if
11808 so, the `,' does not end the default argument.
11810 That is not yet done. */
11817 /* If we've reached the end, stop. */
11821 /* Add the token to the token block. */
11822 token = cp_lexer_consume_token (parser->lexer);
11825 /* Create a DEFAULT_ARG to represented the unparsed default
11827 default_argument = make_node (DEFAULT_ARG);
11828 DEFARG_TOKENS (default_argument)
11829 = cp_token_cache_new (first_token, token);
11831 /* Outside of a class definition, we can just parse the
11832 assignment-expression. */
11835 bool saved_local_variables_forbidden_p;
11837 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11839 saved_greater_than_is_operator_p
11840 = parser->greater_than_is_operator_p;
11841 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11842 /* Local variable names (and the `this' keyword) may not
11843 appear in a default argument. */
11844 saved_local_variables_forbidden_p
11845 = parser->local_variables_forbidden_p;
11846 parser->local_variables_forbidden_p = true;
11847 /* Parse the assignment-expression. */
11848 default_argument = cp_parser_assignment_expression (parser);
11849 /* Restore saved state. */
11850 parser->greater_than_is_operator_p
11851 = saved_greater_than_is_operator_p;
11852 parser->local_variables_forbidden_p
11853 = saved_local_variables_forbidden_p;
11855 if (!parser->default_arg_ok_p)
11857 if (!flag_pedantic_errors)
11858 warning ("deprecated use of default argument for parameter of non-function");
11861 error ("default arguments are only permitted for function parameters");
11862 default_argument = NULL_TREE;
11867 default_argument = NULL_TREE;
11869 return make_parameter_declarator (&decl_specifiers,
11874 /* Parse a function-body.
11877 compound_statement */
11880 cp_parser_function_body (cp_parser *parser)
11882 cp_parser_compound_statement (parser, NULL, false);
11885 /* Parse a ctor-initializer-opt followed by a function-body. Return
11886 true if a ctor-initializer was present. */
11889 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11892 bool ctor_initializer_p;
11894 /* Begin the function body. */
11895 body = begin_function_body ();
11896 /* Parse the optional ctor-initializer. */
11897 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11898 /* Parse the function-body. */
11899 cp_parser_function_body (parser);
11900 /* Finish the function body. */
11901 finish_function_body (body);
11903 return ctor_initializer_p;
11906 /* Parse an initializer.
11909 = initializer-clause
11910 ( expression-list )
11912 Returns a expression representing the initializer. If no
11913 initializer is present, NULL_TREE is returned.
11915 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11916 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11917 set to FALSE if there is no initializer present. If there is an
11918 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11919 is set to true; otherwise it is set to false. */
11922 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11923 bool* non_constant_p)
11928 /* Peek at the next token. */
11929 token = cp_lexer_peek_token (parser->lexer);
11931 /* Let our caller know whether or not this initializer was
11933 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11934 /* Assume that the initializer is constant. */
11935 *non_constant_p = false;
11937 if (token->type == CPP_EQ)
11939 /* Consume the `='. */
11940 cp_lexer_consume_token (parser->lexer);
11941 /* Parse the initializer-clause. */
11942 init = cp_parser_initializer_clause (parser, non_constant_p);
11944 else if (token->type == CPP_OPEN_PAREN)
11945 init = cp_parser_parenthesized_expression_list (parser, false,
11949 /* Anything else is an error. */
11950 cp_parser_error (parser, "expected initializer");
11951 init = error_mark_node;
11957 /* Parse an initializer-clause.
11959 initializer-clause:
11960 assignment-expression
11961 { initializer-list , [opt] }
11964 Returns an expression representing the initializer.
11966 If the `assignment-expression' production is used the value
11967 returned is simply a representation for the expression.
11969 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11970 the elements of the initializer-list (or NULL_TREE, if the last
11971 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11972 NULL_TREE. There is no way to detect whether or not the optional
11973 trailing `,' was provided. NON_CONSTANT_P is as for
11974 cp_parser_initializer. */
11977 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11981 /* If it is not a `{', then we are looking at an
11982 assignment-expression. */
11983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11986 = cp_parser_constant_expression (parser,
11987 /*allow_non_constant_p=*/true,
11989 if (!*non_constant_p)
11990 initializer = fold_non_dependent_expr (initializer);
11994 /* Consume the `{' token. */
11995 cp_lexer_consume_token (parser->lexer);
11996 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11997 initializer = make_node (CONSTRUCTOR);
11998 /* If it's not a `}', then there is a non-trivial initializer. */
11999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12001 /* Parse the initializer list. */
12002 CONSTRUCTOR_ELTS (initializer)
12003 = cp_parser_initializer_list (parser, non_constant_p);
12004 /* A trailing `,' token is allowed. */
12005 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12006 cp_lexer_consume_token (parser->lexer);
12008 /* Now, there should be a trailing `}'. */
12009 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12012 return initializer;
12015 /* Parse an initializer-list.
12019 initializer-list , initializer-clause
12024 identifier : initializer-clause
12025 initializer-list, identifier : initializer-clause
12027 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12028 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12029 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12030 as for cp_parser_initializer. */
12033 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12035 tree initializers = NULL_TREE;
12037 /* Assume all of the expressions are constant. */
12038 *non_constant_p = false;
12040 /* Parse the rest of the list. */
12046 bool clause_non_constant_p;
12048 /* If the next token is an identifier and the following one is a
12049 colon, we are looking at the GNU designated-initializer
12051 if (cp_parser_allow_gnu_extensions_p (parser)
12052 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12053 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12055 /* Consume the identifier. */
12056 identifier = cp_lexer_consume_token (parser->lexer)->value;
12057 /* Consume the `:'. */
12058 cp_lexer_consume_token (parser->lexer);
12061 identifier = NULL_TREE;
12063 /* Parse the initializer. */
12064 initializer = cp_parser_initializer_clause (parser,
12065 &clause_non_constant_p);
12066 /* If any clause is non-constant, so is the entire initializer. */
12067 if (clause_non_constant_p)
12068 *non_constant_p = true;
12069 /* Add it to the list. */
12070 initializers = tree_cons (identifier, initializer, initializers);
12072 /* If the next token is not a comma, we have reached the end of
12074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12077 /* Peek at the next token. */
12078 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12079 /* If the next token is a `}', then we're still done. An
12080 initializer-clause can have a trailing `,' after the
12081 initializer-list and before the closing `}'. */
12082 if (token->type == CPP_CLOSE_BRACE)
12085 /* Consume the `,' token. */
12086 cp_lexer_consume_token (parser->lexer);
12089 /* The initializers were built up in reverse order, so we need to
12090 reverse them now. */
12091 return nreverse (initializers);
12094 /* Classes [gram.class] */
12096 /* Parse a class-name.
12102 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12103 to indicate that names looked up in dependent types should be
12104 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12105 keyword has been used to indicate that the name that appears next
12106 is a template. TYPE_P is true iff the next name should be treated
12107 as class-name, even if it is declared to be some other kind of name
12108 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12109 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12110 being defined in a class-head.
12112 Returns the TYPE_DECL representing the class. */
12115 cp_parser_class_name (cp_parser *parser,
12116 bool typename_keyword_p,
12117 bool template_keyword_p,
12119 bool check_dependency_p,
12121 bool is_declaration)
12128 /* All class-names start with an identifier. */
12129 token = cp_lexer_peek_token (parser->lexer);
12130 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12132 cp_parser_error (parser, "expected class-name");
12133 return error_mark_node;
12136 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12137 to a template-id, so we save it here. */
12138 scope = parser->scope;
12139 if (scope == error_mark_node)
12140 return error_mark_node;
12142 /* Any name names a type if we're following the `typename' keyword
12143 in a qualified name where the enclosing scope is type-dependent. */
12144 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12145 && dependent_type_p (scope));
12146 /* Handle the common case (an identifier, but not a template-id)
12148 if (token->type == CPP_NAME
12149 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12153 /* Look for the identifier. */
12154 identifier = cp_parser_identifier (parser);
12155 /* If the next token isn't an identifier, we are certainly not
12156 looking at a class-name. */
12157 if (identifier == error_mark_node)
12158 decl = error_mark_node;
12159 /* If we know this is a type-name, there's no need to look it
12161 else if (typename_p)
12165 /* If the next token is a `::', then the name must be a type
12168 [basic.lookup.qual]
12170 During the lookup for a name preceding the :: scope
12171 resolution operator, object, function, and enumerator
12172 names are ignored. */
12173 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12175 /* Look up the name. */
12176 decl = cp_parser_lookup_name (parser, identifier,
12178 /*is_template=*/false,
12179 /*is_namespace=*/false,
12180 check_dependency_p,
12181 /*ambiguous_p=*/NULL);
12186 /* Try a template-id. */
12187 decl = cp_parser_template_id (parser, template_keyword_p,
12188 check_dependency_p,
12190 if (decl == error_mark_node)
12191 return error_mark_node;
12194 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12196 /* If this is a typename, create a TYPENAME_TYPE. */
12197 if (typename_p && decl != error_mark_node)
12199 decl = make_typename_type (scope, decl, /*complain=*/1);
12200 if (decl != error_mark_node)
12201 decl = TYPE_NAME (decl);
12204 /* Check to see that it is really the name of a class. */
12205 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12206 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12207 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12208 /* Situations like this:
12210 template <typename T> struct A {
12211 typename T::template X<int>::I i;
12214 are problematic. Is `T::template X<int>' a class-name? The
12215 standard does not seem to be definitive, but there is no other
12216 valid interpretation of the following `::'. Therefore, those
12217 names are considered class-names. */
12218 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12219 else if (decl == error_mark_node
12220 || TREE_CODE (decl) != TYPE_DECL
12221 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12223 cp_parser_error (parser, "expected class-name");
12224 return error_mark_node;
12230 /* Parse a class-specifier.
12233 class-head { member-specification [opt] }
12235 Returns the TREE_TYPE representing the class. */
12238 cp_parser_class_specifier (cp_parser* parser)
12242 tree attributes = NULL_TREE;
12243 int has_trailing_semicolon;
12244 bool nested_name_specifier_p;
12245 unsigned saved_num_template_parameter_lists;
12246 bool pop_p = false;
12247 tree scope = NULL_TREE;
12249 push_deferring_access_checks (dk_no_deferred);
12251 /* Parse the class-head. */
12252 type = cp_parser_class_head (parser,
12253 &nested_name_specifier_p,
12255 /* If the class-head was a semantic disaster, skip the entire body
12259 cp_parser_skip_to_end_of_block_or_statement (parser);
12260 pop_deferring_access_checks ();
12261 return error_mark_node;
12264 /* Look for the `{'. */
12265 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12267 pop_deferring_access_checks ();
12268 return error_mark_node;
12271 /* Issue an error message if type-definitions are forbidden here. */
12272 cp_parser_check_type_definition (parser);
12273 /* Remember that we are defining one more class. */
12274 ++parser->num_classes_being_defined;
12275 /* Inside the class, surrounding template-parameter-lists do not
12277 saved_num_template_parameter_lists
12278 = parser->num_template_parameter_lists;
12279 parser->num_template_parameter_lists = 0;
12281 /* Start the class. */
12282 if (nested_name_specifier_p)
12284 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12285 pop_p = push_scope (scope);
12287 type = begin_class_definition (type);
12289 if (type == error_mark_node)
12290 /* If the type is erroneous, skip the entire body of the class. */
12291 cp_parser_skip_to_closing_brace (parser);
12293 /* Parse the member-specification. */
12294 cp_parser_member_specification_opt (parser);
12296 /* Look for the trailing `}'. */
12297 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12298 /* We get better error messages by noticing a common problem: a
12299 missing trailing `;'. */
12300 token = cp_lexer_peek_token (parser->lexer);
12301 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12302 /* Look for trailing attributes to apply to this class. */
12303 if (cp_parser_allow_gnu_extensions_p (parser))
12305 tree sub_attr = cp_parser_attributes_opt (parser);
12306 attributes = chainon (attributes, sub_attr);
12308 if (type != error_mark_node)
12309 type = finish_struct (type, attributes);
12312 /* If this class is not itself within the scope of another class,
12313 then we need to parse the bodies of all of the queued function
12314 definitions. Note that the queued functions defined in a class
12315 are not always processed immediately following the
12316 class-specifier for that class. Consider:
12319 struct B { void f() { sizeof (A); } };
12322 If `f' were processed before the processing of `A' were
12323 completed, there would be no way to compute the size of `A'.
12324 Note that the nesting we are interested in here is lexical --
12325 not the semantic nesting given by TYPE_CONTEXT. In particular,
12328 struct A { struct B; };
12329 struct A::B { void f() { } };
12331 there is no need to delay the parsing of `A::B::f'. */
12332 if (--parser->num_classes_being_defined == 0)
12339 /* In a first pass, parse default arguments to the functions.
12340 Then, in a second pass, parse the bodies of the functions.
12341 This two-phased approach handles cases like:
12349 class_type = NULL_TREE;
12351 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12352 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12353 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12354 TREE_PURPOSE (parser->unparsed_functions_queues)
12355 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12357 fn = TREE_VALUE (queue_entry);
12358 /* If there are default arguments that have not yet been processed,
12359 take care of them now. */
12360 if (class_type != TREE_PURPOSE (queue_entry))
12363 pop_scope (class_type);
12364 class_type = TREE_PURPOSE (queue_entry);
12365 pop_p = push_scope (class_type);
12367 /* Make sure that any template parameters are in scope. */
12368 maybe_begin_member_template_processing (fn);
12369 /* Parse the default argument expressions. */
12370 cp_parser_late_parsing_default_args (parser, fn);
12371 /* Remove any template parameters from the symbol table. */
12372 maybe_end_member_template_processing ();
12375 pop_scope (class_type);
12376 /* Now parse the body of the functions. */
12377 for (TREE_VALUE (parser->unparsed_functions_queues)
12378 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12379 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12380 TREE_VALUE (parser->unparsed_functions_queues)
12381 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12383 /* Figure out which function we need to process. */
12384 fn = TREE_VALUE (queue_entry);
12386 /* A hack to prevent garbage collection. */
12389 /* Parse the function. */
12390 cp_parser_late_parsing_for_member (parser, fn);
12395 /* Put back any saved access checks. */
12396 pop_deferring_access_checks ();
12398 /* Restore the count of active template-parameter-lists. */
12399 parser->num_template_parameter_lists
12400 = saved_num_template_parameter_lists;
12405 /* Parse a class-head.
12408 class-key identifier [opt] base-clause [opt]
12409 class-key nested-name-specifier identifier base-clause [opt]
12410 class-key nested-name-specifier [opt] template-id
12414 class-key attributes identifier [opt] base-clause [opt]
12415 class-key attributes nested-name-specifier identifier base-clause [opt]
12416 class-key attributes nested-name-specifier [opt] template-id
12419 Returns the TYPE of the indicated class. Sets
12420 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12421 involving a nested-name-specifier was used, and FALSE otherwise.
12423 Returns NULL_TREE if the class-head is syntactically valid, but
12424 semantically invalid in a way that means we should skip the entire
12425 body of the class. */
12428 cp_parser_class_head (cp_parser* parser,
12429 bool* nested_name_specifier_p,
12430 tree *attributes_p)
12432 tree nested_name_specifier;
12433 enum tag_types class_key;
12434 tree id = NULL_TREE;
12435 tree type = NULL_TREE;
12437 bool template_id_p = false;
12438 bool qualified_p = false;
12439 bool invalid_nested_name_p = false;
12440 bool invalid_explicit_specialization_p = false;
12441 bool pop_p = false;
12442 unsigned num_templates;
12445 /* Assume no nested-name-specifier will be present. */
12446 *nested_name_specifier_p = false;
12447 /* Assume no template parameter lists will be used in defining the
12451 /* Look for the class-key. */
12452 class_key = cp_parser_class_key (parser);
12453 if (class_key == none_type)
12454 return error_mark_node;
12456 /* Parse the attributes. */
12457 attributes = cp_parser_attributes_opt (parser);
12459 /* If the next token is `::', that is invalid -- but sometimes
12460 people do try to write:
12464 Handle this gracefully by accepting the extra qualifier, and then
12465 issuing an error about it later if this really is a
12466 class-head. If it turns out just to be an elaborated type
12467 specifier, remain silent. */
12468 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12469 qualified_p = true;
12471 push_deferring_access_checks (dk_no_check);
12473 /* Determine the name of the class. Begin by looking for an
12474 optional nested-name-specifier. */
12475 nested_name_specifier
12476 = cp_parser_nested_name_specifier_opt (parser,
12477 /*typename_keyword_p=*/false,
12478 /*check_dependency_p=*/false,
12480 /*is_declaration=*/false);
12481 /* If there was a nested-name-specifier, then there *must* be an
12483 if (nested_name_specifier)
12485 /* Although the grammar says `identifier', it really means
12486 `class-name' or `template-name'. You are only allowed to
12487 define a class that has already been declared with this
12490 The proposed resolution for Core Issue 180 says that whever
12491 you see `class T::X' you should treat `X' as a type-name.
12493 It is OK to define an inaccessible class; for example:
12495 class A { class B; };
12498 We do not know if we will see a class-name, or a
12499 template-name. We look for a class-name first, in case the
12500 class-name is a template-id; if we looked for the
12501 template-name first we would stop after the template-name. */
12502 cp_parser_parse_tentatively (parser);
12503 type = cp_parser_class_name (parser,
12504 /*typename_keyword_p=*/false,
12505 /*template_keyword_p=*/false,
12507 /*check_dependency_p=*/false,
12508 /*class_head_p=*/true,
12509 /*is_declaration=*/false);
12510 /* If that didn't work, ignore the nested-name-specifier. */
12511 if (!cp_parser_parse_definitely (parser))
12513 invalid_nested_name_p = true;
12514 id = cp_parser_identifier (parser);
12515 if (id == error_mark_node)
12518 /* If we could not find a corresponding TYPE, treat this
12519 declaration like an unqualified declaration. */
12520 if (type == error_mark_node)
12521 nested_name_specifier = NULL_TREE;
12522 /* Otherwise, count the number of templates used in TYPE and its
12523 containing scopes. */
12528 for (scope = TREE_TYPE (type);
12529 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12530 scope = (TYPE_P (scope)
12531 ? TYPE_CONTEXT (scope)
12532 : DECL_CONTEXT (scope)))
12534 && CLASS_TYPE_P (scope)
12535 && CLASSTYPE_TEMPLATE_INFO (scope)
12536 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12537 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12541 /* Otherwise, the identifier is optional. */
12544 /* We don't know whether what comes next is a template-id,
12545 an identifier, or nothing at all. */
12546 cp_parser_parse_tentatively (parser);
12547 /* Check for a template-id. */
12548 id = cp_parser_template_id (parser,
12549 /*template_keyword_p=*/false,
12550 /*check_dependency_p=*/true,
12551 /*is_declaration=*/true);
12552 /* If that didn't work, it could still be an identifier. */
12553 if (!cp_parser_parse_definitely (parser))
12555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12556 id = cp_parser_identifier (parser);
12562 template_id_p = true;
12567 pop_deferring_access_checks ();
12570 cp_parser_check_for_invalid_template_id (parser, id);
12572 /* If it's not a `:' or a `{' then we can't really be looking at a
12573 class-head, since a class-head only appears as part of a
12574 class-specifier. We have to detect this situation before calling
12575 xref_tag, since that has irreversible side-effects. */
12576 if (!cp_parser_next_token_starts_class_definition_p (parser))
12578 cp_parser_error (parser, "expected %<{%> or %<:%>");
12579 return error_mark_node;
12582 /* At this point, we're going ahead with the class-specifier, even
12583 if some other problem occurs. */
12584 cp_parser_commit_to_tentative_parse (parser);
12585 /* Issue the error about the overly-qualified name now. */
12587 cp_parser_error (parser,
12588 "global qualification of class name is invalid");
12589 else if (invalid_nested_name_p)
12590 cp_parser_error (parser,
12591 "qualified name does not name a class");
12592 else if (nested_name_specifier)
12595 /* Figure out in what scope the declaration is being placed. */
12596 scope = current_scope ();
12598 scope = current_namespace;
12599 /* If that scope does not contain the scope in which the
12600 class was originally declared, the program is invalid. */
12601 if (scope && !is_ancestor (scope, nested_name_specifier))
12603 error ("declaration of %qD in %qD which does not enclose %qD",
12604 type, scope, nested_name_specifier);
12610 A declarator-id shall not be qualified exception of the
12611 definition of a ... nested class outside of its class
12612 ... [or] a the definition or explicit instantiation of a
12613 class member of a namespace outside of its namespace. */
12614 if (scope == nested_name_specifier)
12616 pedwarn ("extra qualification ignored");
12617 nested_name_specifier = NULL_TREE;
12621 /* An explicit-specialization must be preceded by "template <>". If
12622 it is not, try to recover gracefully. */
12623 if (at_namespace_scope_p ()
12624 && parser->num_template_parameter_lists == 0
12627 error ("an explicit specialization must be preceded by %<template <>%>");
12628 invalid_explicit_specialization_p = true;
12629 /* Take the same action that would have been taken by
12630 cp_parser_explicit_specialization. */
12631 ++parser->num_template_parameter_lists;
12632 begin_specialization ();
12634 /* There must be no "return" statements between this point and the
12635 end of this function; set "type "to the correct return value and
12636 use "goto done;" to return. */
12637 /* Make sure that the right number of template parameters were
12639 if (!cp_parser_check_template_parameters (parser, num_templates))
12641 /* If something went wrong, there is no point in even trying to
12642 process the class-definition. */
12647 /* Look up the type. */
12650 type = TREE_TYPE (id);
12651 maybe_process_partial_specialization (type);
12653 else if (!nested_name_specifier)
12655 /* If the class was unnamed, create a dummy name. */
12657 id = make_anon_name ();
12658 type = xref_tag (class_key, id, /*globalize=*/false,
12659 parser->num_template_parameter_lists);
12664 bool pop_p = false;
12668 template <typename T> struct S { struct T };
12669 template <typename T> struct S<T>::T { };
12671 we will get a TYPENAME_TYPE when processing the definition of
12672 `S::T'. We need to resolve it to the actual type before we
12673 try to define it. */
12674 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12676 class_type = resolve_typename_type (TREE_TYPE (type),
12677 /*only_current_p=*/false);
12678 if (class_type != error_mark_node)
12679 type = TYPE_NAME (class_type);
12682 cp_parser_error (parser, "could not resolve typename type");
12683 type = error_mark_node;
12687 maybe_process_partial_specialization (TREE_TYPE (type));
12688 class_type = current_class_type;
12689 /* Enter the scope indicated by the nested-name-specifier. */
12690 if (nested_name_specifier)
12691 pop_p = push_scope (nested_name_specifier);
12692 /* Get the canonical version of this type. */
12693 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12694 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12695 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12696 type = push_template_decl (type);
12697 type = TREE_TYPE (type);
12698 if (nested_name_specifier)
12700 *nested_name_specifier_p = true;
12702 pop_scope (nested_name_specifier);
12705 /* Indicate whether this class was declared as a `class' or as a
12707 if (TREE_CODE (type) == RECORD_TYPE)
12708 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12709 cp_parser_check_class_key (class_key, type);
12711 /* Enter the scope containing the class; the names of base classes
12712 should be looked up in that context. For example, given:
12714 struct A { struct B {}; struct C; };
12715 struct A::C : B {};
12718 if (nested_name_specifier)
12719 pop_p = push_scope (nested_name_specifier);
12723 /* Get the list of base-classes, if there is one. */
12724 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12725 bases = cp_parser_base_clause (parser);
12727 /* Process the base classes. */
12728 xref_basetypes (type, bases);
12730 /* Leave the scope given by the nested-name-specifier. We will
12731 enter the class scope itself while processing the members. */
12733 pop_scope (nested_name_specifier);
12736 if (invalid_explicit_specialization_p)
12738 end_specialization ();
12739 --parser->num_template_parameter_lists;
12741 *attributes_p = attributes;
12745 /* Parse a class-key.
12752 Returns the kind of class-key specified, or none_type to indicate
12755 static enum tag_types
12756 cp_parser_class_key (cp_parser* parser)
12759 enum tag_types tag_type;
12761 /* Look for the class-key. */
12762 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12766 /* Check to see if the TOKEN is a class-key. */
12767 tag_type = cp_parser_token_is_class_key (token);
12769 cp_parser_error (parser, "expected class-key");
12773 /* Parse an (optional) member-specification.
12775 member-specification:
12776 member-declaration member-specification [opt]
12777 access-specifier : member-specification [opt] */
12780 cp_parser_member_specification_opt (cp_parser* parser)
12787 /* Peek at the next token. */
12788 token = cp_lexer_peek_token (parser->lexer);
12789 /* If it's a `}', or EOF then we've seen all the members. */
12790 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12793 /* See if this token is a keyword. */
12794 keyword = token->keyword;
12798 case RID_PROTECTED:
12800 /* Consume the access-specifier. */
12801 cp_lexer_consume_token (parser->lexer);
12802 /* Remember which access-specifier is active. */
12803 current_access_specifier = token->value;
12804 /* Look for the `:'. */
12805 cp_parser_require (parser, CPP_COLON, "`:'");
12809 /* Accept #pragmas at class scope. */
12810 if (token->type == CPP_PRAGMA)
12812 cp_lexer_handle_pragma (parser->lexer);
12816 /* Otherwise, the next construction must be a
12817 member-declaration. */
12818 cp_parser_member_declaration (parser);
12823 /* Parse a member-declaration.
12825 member-declaration:
12826 decl-specifier-seq [opt] member-declarator-list [opt] ;
12827 function-definition ; [opt]
12828 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12830 template-declaration
12832 member-declarator-list:
12834 member-declarator-list , member-declarator
12837 declarator pure-specifier [opt]
12838 declarator constant-initializer [opt]
12839 identifier [opt] : constant-expression
12843 member-declaration:
12844 __extension__ member-declaration
12847 declarator attributes [opt] pure-specifier [opt]
12848 declarator attributes [opt] constant-initializer [opt]
12849 identifier [opt] attributes [opt] : constant-expression */
12852 cp_parser_member_declaration (cp_parser* parser)
12854 cp_decl_specifier_seq decl_specifiers;
12855 tree prefix_attributes;
12857 int declares_class_or_enum;
12860 int saved_pedantic;
12862 /* Check for the `__extension__' keyword. */
12863 if (cp_parser_extension_opt (parser, &saved_pedantic))
12866 cp_parser_member_declaration (parser);
12867 /* Restore the old value of the PEDANTIC flag. */
12868 pedantic = saved_pedantic;
12873 /* Check for a template-declaration. */
12874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12876 /* Parse the template-declaration. */
12877 cp_parser_template_declaration (parser, /*member_p=*/true);
12882 /* Check for a using-declaration. */
12883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12885 /* Parse the using-declaration. */
12886 cp_parser_using_declaration (parser);
12891 /* Parse the decl-specifier-seq. */
12892 cp_parser_decl_specifier_seq (parser,
12893 CP_PARSER_FLAGS_OPTIONAL,
12895 &declares_class_or_enum);
12896 prefix_attributes = decl_specifiers.attributes;
12897 decl_specifiers.attributes = NULL_TREE;
12898 /* Check for an invalid type-name. */
12899 if (!decl_specifiers.type
12900 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12902 /* If there is no declarator, then the decl-specifier-seq should
12904 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12906 /* If there was no decl-specifier-seq, and the next token is a
12907 `;', then we have something like:
12913 Each member-declaration shall declare at least one member
12914 name of the class. */
12915 if (!decl_specifiers.any_specifiers_p)
12917 cp_token *token = cp_lexer_peek_token (parser->lexer);
12918 if (pedantic && !token->in_system_header)
12919 pedwarn ("%Hextra %<;%>", &token->location);
12925 /* See if this declaration is a friend. */
12926 friend_p = cp_parser_friend_p (&decl_specifiers);
12927 /* If there were decl-specifiers, check to see if there was
12928 a class-declaration. */
12929 type = check_tag_decl (&decl_specifiers);
12930 /* Nested classes have already been added to the class, but
12931 a `friend' needs to be explicitly registered. */
12934 /* If the `friend' keyword was present, the friend must
12935 be introduced with a class-key. */
12936 if (!declares_class_or_enum)
12937 error ("a class-key must be used when declaring a friend");
12940 template <typename T> struct A {
12941 friend struct A<T>::B;
12944 A<T>::B will be represented by a TYPENAME_TYPE, and
12945 therefore not recognized by check_tag_decl. */
12947 && decl_specifiers.type
12948 && TYPE_P (decl_specifiers.type))
12949 type = decl_specifiers.type;
12950 if (!type || !TYPE_P (type))
12951 error ("friend declaration does not name a class or "
12954 make_friend_class (current_class_type, type,
12955 /*complain=*/true);
12957 /* If there is no TYPE, an error message will already have
12959 else if (!type || type == error_mark_node)
12961 /* An anonymous aggregate has to be handled specially; such
12962 a declaration really declares a data member (with a
12963 particular type), as opposed to a nested class. */
12964 else if (ANON_AGGR_TYPE_P (type))
12966 /* Remove constructors and such from TYPE, now that we
12967 know it is an anonymous aggregate. */
12968 fixup_anonymous_aggr (type);
12969 /* And make the corresponding data member. */
12970 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12971 /* Add it to the class. */
12972 finish_member_declaration (decl);
12975 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12980 /* See if these declarations will be friends. */
12981 friend_p = cp_parser_friend_p (&decl_specifiers);
12983 /* Keep going until we hit the `;' at the end of the
12985 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12987 tree attributes = NULL_TREE;
12988 tree first_attribute;
12990 /* Peek at the next token. */
12991 token = cp_lexer_peek_token (parser->lexer);
12993 /* Check for a bitfield declaration. */
12994 if (token->type == CPP_COLON
12995 || (token->type == CPP_NAME
12996 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13002 /* Get the name of the bitfield. Note that we cannot just
13003 check TOKEN here because it may have been invalidated by
13004 the call to cp_lexer_peek_nth_token above. */
13005 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13006 identifier = cp_parser_identifier (parser);
13008 identifier = NULL_TREE;
13010 /* Consume the `:' token. */
13011 cp_lexer_consume_token (parser->lexer);
13012 /* Get the width of the bitfield. */
13014 = cp_parser_constant_expression (parser,
13015 /*allow_non_constant=*/false,
13018 /* Look for attributes that apply to the bitfield. */
13019 attributes = cp_parser_attributes_opt (parser);
13020 /* Remember which attributes are prefix attributes and
13022 first_attribute = attributes;
13023 /* Combine the attributes. */
13024 attributes = chainon (prefix_attributes, attributes);
13026 /* Create the bitfield declaration. */
13027 decl = grokbitfield (identifier
13028 ? make_id_declarator (identifier)
13032 /* Apply the attributes. */
13033 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13037 cp_declarator *declarator;
13039 tree asm_specification;
13040 int ctor_dtor_or_conv_p;
13042 /* Parse the declarator. */
13044 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13045 &ctor_dtor_or_conv_p,
13046 /*parenthesized_p=*/NULL,
13047 /*member_p=*/true);
13049 /* If something went wrong parsing the declarator, make sure
13050 that we at least consume some tokens. */
13051 if (declarator == cp_error_declarator)
13053 /* Skip to the end of the statement. */
13054 cp_parser_skip_to_end_of_statement (parser);
13055 /* If the next token is not a semicolon, that is
13056 probably because we just skipped over the body of
13057 a function. So, we consume a semicolon if
13058 present, but do not issue an error message if it
13060 if (cp_lexer_next_token_is (parser->lexer,
13062 cp_lexer_consume_token (parser->lexer);
13066 cp_parser_check_for_definition_in_return_type
13067 (declarator, declares_class_or_enum);
13069 /* Look for an asm-specification. */
13070 asm_specification = cp_parser_asm_specification_opt (parser);
13071 /* Look for attributes that apply to the declaration. */
13072 attributes = cp_parser_attributes_opt (parser);
13073 /* Remember which attributes are prefix attributes and
13075 first_attribute = attributes;
13076 /* Combine the attributes. */
13077 attributes = chainon (prefix_attributes, attributes);
13079 /* If it's an `=', then we have a constant-initializer or a
13080 pure-specifier. It is not correct to parse the
13081 initializer before registering the member declaration
13082 since the member declaration should be in scope while
13083 its initializer is processed. However, the rest of the
13084 front end does not yet provide an interface that allows
13085 us to handle this correctly. */
13086 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13090 A pure-specifier shall be used only in the declaration of
13091 a virtual function.
13093 A member-declarator can contain a constant-initializer
13094 only if it declares a static member of integral or
13097 Therefore, if the DECLARATOR is for a function, we look
13098 for a pure-specifier; otherwise, we look for a
13099 constant-initializer. When we call `grokfield', it will
13100 perform more stringent semantics checks. */
13101 if (declarator->kind == cdk_function)
13102 initializer = cp_parser_pure_specifier (parser);
13104 /* Parse the initializer. */
13105 initializer = cp_parser_constant_initializer (parser);
13107 /* Otherwise, there is no initializer. */
13109 initializer = NULL_TREE;
13111 /* See if we are probably looking at a function
13112 definition. We are certainly not looking at at a
13113 member-declarator. Calling `grokfield' has
13114 side-effects, so we must not do it unless we are sure
13115 that we are looking at a member-declarator. */
13116 if (cp_parser_token_starts_function_definition_p
13117 (cp_lexer_peek_token (parser->lexer)))
13119 /* The grammar does not allow a pure-specifier to be
13120 used when a member function is defined. (It is
13121 possible that this fact is an oversight in the
13122 standard, since a pure function may be defined
13123 outside of the class-specifier. */
13125 error ("pure-specifier on function-definition");
13126 decl = cp_parser_save_member_function_body (parser,
13130 /* If the member was not a friend, declare it here. */
13132 finish_member_declaration (decl);
13133 /* Peek at the next token. */
13134 token = cp_lexer_peek_token (parser->lexer);
13135 /* If the next token is a semicolon, consume it. */
13136 if (token->type == CPP_SEMICOLON)
13137 cp_lexer_consume_token (parser->lexer);
13142 /* Create the declaration. */
13143 decl = grokfield (declarator, &decl_specifiers,
13144 initializer, asm_specification,
13146 /* Any initialization must have been from a
13147 constant-expression. */
13148 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13149 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13153 /* Reset PREFIX_ATTRIBUTES. */
13154 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13155 attributes = TREE_CHAIN (attributes);
13157 TREE_CHAIN (attributes) = NULL_TREE;
13159 /* If there is any qualification still in effect, clear it
13160 now; we will be starting fresh with the next declarator. */
13161 parser->scope = NULL_TREE;
13162 parser->qualifying_scope = NULL_TREE;
13163 parser->object_scope = NULL_TREE;
13164 /* If it's a `,', then there are more declarators. */
13165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13166 cp_lexer_consume_token (parser->lexer);
13167 /* If the next token isn't a `;', then we have a parse error. */
13168 else if (cp_lexer_next_token_is_not (parser->lexer,
13171 cp_parser_error (parser, "expected %<;%>");
13172 /* Skip tokens until we find a `;'. */
13173 cp_parser_skip_to_end_of_statement (parser);
13180 /* Add DECL to the list of members. */
13182 finish_member_declaration (decl);
13184 if (TREE_CODE (decl) == FUNCTION_DECL)
13185 cp_parser_save_default_args (parser, decl);
13190 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13193 /* Parse a pure-specifier.
13198 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13199 Otherwise, ERROR_MARK_NODE is returned. */
13202 cp_parser_pure_specifier (cp_parser* parser)
13206 /* Look for the `=' token. */
13207 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13208 return error_mark_node;
13209 /* Look for the `0' token. */
13210 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13211 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13212 to get information from the lexer about how the number was
13213 spelled in order to fix this problem. */
13214 if (!token || !integer_zerop (token->value))
13215 return error_mark_node;
13217 return integer_zero_node;
13220 /* Parse a constant-initializer.
13222 constant-initializer:
13223 = constant-expression
13225 Returns a representation of the constant-expression. */
13228 cp_parser_constant_initializer (cp_parser* parser)
13230 /* Look for the `=' token. */
13231 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13232 return error_mark_node;
13234 /* It is invalid to write:
13236 struct S { static const int i = { 7 }; };
13239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13241 cp_parser_error (parser,
13242 "a brace-enclosed initializer is not allowed here");
13243 /* Consume the opening brace. */
13244 cp_lexer_consume_token (parser->lexer);
13245 /* Skip the initializer. */
13246 cp_parser_skip_to_closing_brace (parser);
13247 /* Look for the trailing `}'. */
13248 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13250 return error_mark_node;
13253 return cp_parser_constant_expression (parser,
13254 /*allow_non_constant=*/false,
13258 /* Derived classes [gram.class.derived] */
13260 /* Parse a base-clause.
13263 : base-specifier-list
13265 base-specifier-list:
13267 base-specifier-list , base-specifier
13269 Returns a TREE_LIST representing the base-classes, in the order in
13270 which they were declared. The representation of each node is as
13271 described by cp_parser_base_specifier.
13273 In the case that no bases are specified, this function will return
13274 NULL_TREE, not ERROR_MARK_NODE. */
13277 cp_parser_base_clause (cp_parser* parser)
13279 tree bases = NULL_TREE;
13281 /* Look for the `:' that begins the list. */
13282 cp_parser_require (parser, CPP_COLON, "`:'");
13284 /* Scan the base-specifier-list. */
13290 /* Look for the base-specifier. */
13291 base = cp_parser_base_specifier (parser);
13292 /* Add BASE to the front of the list. */
13293 if (base != error_mark_node)
13295 TREE_CHAIN (base) = bases;
13298 /* Peek at the next token. */
13299 token = cp_lexer_peek_token (parser->lexer);
13300 /* If it's not a comma, then the list is complete. */
13301 if (token->type != CPP_COMMA)
13303 /* Consume the `,'. */
13304 cp_lexer_consume_token (parser->lexer);
13307 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13308 base class had a qualified name. However, the next name that
13309 appears is certainly not qualified. */
13310 parser->scope = NULL_TREE;
13311 parser->qualifying_scope = NULL_TREE;
13312 parser->object_scope = NULL_TREE;
13314 return nreverse (bases);
13317 /* Parse a base-specifier.
13320 :: [opt] nested-name-specifier [opt] class-name
13321 virtual access-specifier [opt] :: [opt] nested-name-specifier
13323 access-specifier virtual [opt] :: [opt] nested-name-specifier
13326 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13327 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13328 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13329 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13332 cp_parser_base_specifier (cp_parser* parser)
13336 bool virtual_p = false;
13337 bool duplicate_virtual_error_issued_p = false;
13338 bool duplicate_access_error_issued_p = false;
13339 bool class_scope_p, template_p;
13340 tree access = access_default_node;
13343 /* Process the optional `virtual' and `access-specifier'. */
13346 /* Peek at the next token. */
13347 token = cp_lexer_peek_token (parser->lexer);
13348 /* Process `virtual'. */
13349 switch (token->keyword)
13352 /* If `virtual' appears more than once, issue an error. */
13353 if (virtual_p && !duplicate_virtual_error_issued_p)
13355 cp_parser_error (parser,
13356 "%<virtual%> specified more than once in base-specified");
13357 duplicate_virtual_error_issued_p = true;
13362 /* Consume the `virtual' token. */
13363 cp_lexer_consume_token (parser->lexer);
13368 case RID_PROTECTED:
13370 /* If more than one access specifier appears, issue an
13372 if (access != access_default_node
13373 && !duplicate_access_error_issued_p)
13375 cp_parser_error (parser,
13376 "more than one access specifier in base-specified");
13377 duplicate_access_error_issued_p = true;
13380 access = ridpointers[(int) token->keyword];
13382 /* Consume the access-specifier. */
13383 cp_lexer_consume_token (parser->lexer);
13392 /* It is not uncommon to see programs mechanically, erroneously, use
13393 the 'typename' keyword to denote (dependent) qualified types
13394 as base classes. */
13395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13397 if (!processing_template_decl)
13398 error ("keyword %<typename%> not allowed outside of templates");
13400 error ("keyword %<typename%> not allowed in this context "
13401 "(the base class is implicitly a type)");
13402 cp_lexer_consume_token (parser->lexer);
13405 /* Look for the optional `::' operator. */
13406 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13407 /* Look for the nested-name-specifier. The simplest way to
13412 The keyword `typename' is not permitted in a base-specifier or
13413 mem-initializer; in these contexts a qualified name that
13414 depends on a template-parameter is implicitly assumed to be a
13417 is to pretend that we have seen the `typename' keyword at this
13419 cp_parser_nested_name_specifier_opt (parser,
13420 /*typename_keyword_p=*/true,
13421 /*check_dependency_p=*/true,
13423 /*is_declaration=*/true);
13424 /* If the base class is given by a qualified name, assume that names
13425 we see are type names or templates, as appropriate. */
13426 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13427 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13429 /* Finally, look for the class-name. */
13430 type = cp_parser_class_name (parser,
13434 /*check_dependency_p=*/true,
13435 /*class_head_p=*/false,
13436 /*is_declaration=*/true);
13438 if (type == error_mark_node)
13439 return error_mark_node;
13441 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13444 /* Exception handling [gram.exception] */
13446 /* Parse an (optional) exception-specification.
13448 exception-specification:
13449 throw ( type-id-list [opt] )
13451 Returns a TREE_LIST representing the exception-specification. The
13452 TREE_VALUE of each node is a type. */
13455 cp_parser_exception_specification_opt (cp_parser* parser)
13460 /* Peek at the next token. */
13461 token = cp_lexer_peek_token (parser->lexer);
13462 /* If it's not `throw', then there's no exception-specification. */
13463 if (!cp_parser_is_keyword (token, RID_THROW))
13466 /* Consume the `throw'. */
13467 cp_lexer_consume_token (parser->lexer);
13469 /* Look for the `('. */
13470 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13472 /* Peek at the next token. */
13473 token = cp_lexer_peek_token (parser->lexer);
13474 /* If it's not a `)', then there is a type-id-list. */
13475 if (token->type != CPP_CLOSE_PAREN)
13477 const char *saved_message;
13479 /* Types may not be defined in an exception-specification. */
13480 saved_message = parser->type_definition_forbidden_message;
13481 parser->type_definition_forbidden_message
13482 = "types may not be defined in an exception-specification";
13483 /* Parse the type-id-list. */
13484 type_id_list = cp_parser_type_id_list (parser);
13485 /* Restore the saved message. */
13486 parser->type_definition_forbidden_message = saved_message;
13489 type_id_list = empty_except_spec;
13491 /* Look for the `)'. */
13492 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13494 return type_id_list;
13497 /* Parse an (optional) type-id-list.
13501 type-id-list , type-id
13503 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13504 in the order that the types were presented. */
13507 cp_parser_type_id_list (cp_parser* parser)
13509 tree types = NULL_TREE;
13516 /* Get the next type-id. */
13517 type = cp_parser_type_id (parser);
13518 /* Add it to the list. */
13519 types = add_exception_specifier (types, type, /*complain=*/1);
13520 /* Peek at the next token. */
13521 token = cp_lexer_peek_token (parser->lexer);
13522 /* If it is not a `,', we are done. */
13523 if (token->type != CPP_COMMA)
13525 /* Consume the `,'. */
13526 cp_lexer_consume_token (parser->lexer);
13529 return nreverse (types);
13532 /* Parse a try-block.
13535 try compound-statement handler-seq */
13538 cp_parser_try_block (cp_parser* parser)
13542 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13543 try_block = begin_try_block ();
13544 cp_parser_compound_statement (parser, NULL, true);
13545 finish_try_block (try_block);
13546 cp_parser_handler_seq (parser);
13547 finish_handler_sequence (try_block);
13552 /* Parse a function-try-block.
13554 function-try-block:
13555 try ctor-initializer [opt] function-body handler-seq */
13558 cp_parser_function_try_block (cp_parser* parser)
13561 bool ctor_initializer_p;
13563 /* Look for the `try' keyword. */
13564 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13566 /* Let the rest of the front-end know where we are. */
13567 try_block = begin_function_try_block ();
13568 /* Parse the function-body. */
13570 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13571 /* We're done with the `try' part. */
13572 finish_function_try_block (try_block);
13573 /* Parse the handlers. */
13574 cp_parser_handler_seq (parser);
13575 /* We're done with the handlers. */
13576 finish_function_handler_sequence (try_block);
13578 return ctor_initializer_p;
13581 /* Parse a handler-seq.
13584 handler handler-seq [opt] */
13587 cp_parser_handler_seq (cp_parser* parser)
13593 /* Parse the handler. */
13594 cp_parser_handler (parser);
13595 /* Peek at the next token. */
13596 token = cp_lexer_peek_token (parser->lexer);
13597 /* If it's not `catch' then there are no more handlers. */
13598 if (!cp_parser_is_keyword (token, RID_CATCH))
13603 /* Parse a handler.
13606 catch ( exception-declaration ) compound-statement */
13609 cp_parser_handler (cp_parser* parser)
13614 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13615 handler = begin_handler ();
13616 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13617 declaration = cp_parser_exception_declaration (parser);
13618 finish_handler_parms (declaration, handler);
13619 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13620 cp_parser_compound_statement (parser, NULL, false);
13621 finish_handler (handler);
13624 /* Parse an exception-declaration.
13626 exception-declaration:
13627 type-specifier-seq declarator
13628 type-specifier-seq abstract-declarator
13632 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13633 ellipsis variant is used. */
13636 cp_parser_exception_declaration (cp_parser* parser)
13639 cp_decl_specifier_seq type_specifiers;
13640 cp_declarator *declarator;
13641 const char *saved_message;
13643 /* If it's an ellipsis, it's easy to handle. */
13644 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13646 /* Consume the `...' token. */
13647 cp_lexer_consume_token (parser->lexer);
13651 /* Types may not be defined in exception-declarations. */
13652 saved_message = parser->type_definition_forbidden_message;
13653 parser->type_definition_forbidden_message
13654 = "types may not be defined in exception-declarations";
13656 /* Parse the type-specifier-seq. */
13657 cp_parser_type_specifier_seq (parser, &type_specifiers);
13658 /* If it's a `)', then there is no declarator. */
13659 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13662 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13663 /*ctor_dtor_or_conv_p=*/NULL,
13664 /*parenthesized_p=*/NULL,
13665 /*member_p=*/false);
13667 /* Restore the saved message. */
13668 parser->type_definition_forbidden_message = saved_message;
13670 if (type_specifiers.any_specifiers_p)
13672 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13673 if (decl == NULL_TREE)
13674 error ("invalid catch parameter");
13682 /* Parse a throw-expression.
13685 throw assignment-expression [opt]
13687 Returns a THROW_EXPR representing the throw-expression. */
13690 cp_parser_throw_expression (cp_parser* parser)
13695 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13696 token = cp_lexer_peek_token (parser->lexer);
13697 /* Figure out whether or not there is an assignment-expression
13698 following the "throw" keyword. */
13699 if (token->type == CPP_COMMA
13700 || token->type == CPP_SEMICOLON
13701 || token->type == CPP_CLOSE_PAREN
13702 || token->type == CPP_CLOSE_SQUARE
13703 || token->type == CPP_CLOSE_BRACE
13704 || token->type == CPP_COLON)
13705 expression = NULL_TREE;
13707 expression = cp_parser_assignment_expression (parser);
13709 return build_throw (expression);
13712 /* GNU Extensions */
13714 /* Parse an (optional) asm-specification.
13717 asm ( string-literal )
13719 If the asm-specification is present, returns a STRING_CST
13720 corresponding to the string-literal. Otherwise, returns
13724 cp_parser_asm_specification_opt (cp_parser* parser)
13727 tree asm_specification;
13729 /* Peek at the next token. */
13730 token = cp_lexer_peek_token (parser->lexer);
13731 /* If the next token isn't the `asm' keyword, then there's no
13732 asm-specification. */
13733 if (!cp_parser_is_keyword (token, RID_ASM))
13736 /* Consume the `asm' token. */
13737 cp_lexer_consume_token (parser->lexer);
13738 /* Look for the `('. */
13739 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13741 /* Look for the string-literal. */
13742 asm_specification = cp_parser_string_literal (parser, false, false);
13744 /* Look for the `)'. */
13745 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13747 return asm_specification;
13750 /* Parse an asm-operand-list.
13754 asm-operand-list , asm-operand
13757 string-literal ( expression )
13758 [ string-literal ] string-literal ( expression )
13760 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13761 each node is the expression. The TREE_PURPOSE is itself a
13762 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13763 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13764 is a STRING_CST for the string literal before the parenthesis. */
13767 cp_parser_asm_operand_list (cp_parser* parser)
13769 tree asm_operands = NULL_TREE;
13773 tree string_literal;
13777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13779 /* Consume the `[' token. */
13780 cp_lexer_consume_token (parser->lexer);
13781 /* Read the operand name. */
13782 name = cp_parser_identifier (parser);
13783 if (name != error_mark_node)
13784 name = build_string (IDENTIFIER_LENGTH (name),
13785 IDENTIFIER_POINTER (name));
13786 /* Look for the closing `]'. */
13787 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13791 /* Look for the string-literal. */
13792 string_literal = cp_parser_string_literal (parser, false, false);
13794 /* Look for the `('. */
13795 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13796 /* Parse the expression. */
13797 expression = cp_parser_expression (parser);
13798 /* Look for the `)'. */
13799 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13801 /* Add this operand to the list. */
13802 asm_operands = tree_cons (build_tree_list (name, string_literal),
13805 /* If the next token is not a `,', there are no more
13807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13809 /* Consume the `,'. */
13810 cp_lexer_consume_token (parser->lexer);
13813 return nreverse (asm_operands);
13816 /* Parse an asm-clobber-list.
13820 asm-clobber-list , string-literal
13822 Returns a TREE_LIST, indicating the clobbers in the order that they
13823 appeared. The TREE_VALUE of each node is a STRING_CST. */
13826 cp_parser_asm_clobber_list (cp_parser* parser)
13828 tree clobbers = NULL_TREE;
13832 tree string_literal;
13834 /* Look for the string literal. */
13835 string_literal = cp_parser_string_literal (parser, false, false);
13836 /* Add it to the list. */
13837 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13838 /* If the next token is not a `,', then the list is
13840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13842 /* Consume the `,' token. */
13843 cp_lexer_consume_token (parser->lexer);
13849 /* Parse an (optional) series of attributes.
13852 attributes attribute
13855 __attribute__ (( attribute-list [opt] ))
13857 The return value is as for cp_parser_attribute_list. */
13860 cp_parser_attributes_opt (cp_parser* parser)
13862 tree attributes = NULL_TREE;
13867 tree attribute_list;
13869 /* Peek at the next token. */
13870 token = cp_lexer_peek_token (parser->lexer);
13871 /* If it's not `__attribute__', then we're done. */
13872 if (token->keyword != RID_ATTRIBUTE)
13875 /* Consume the `__attribute__' keyword. */
13876 cp_lexer_consume_token (parser->lexer);
13877 /* Look for the two `(' tokens. */
13878 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13879 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13881 /* Peek at the next token. */
13882 token = cp_lexer_peek_token (parser->lexer);
13883 if (token->type != CPP_CLOSE_PAREN)
13884 /* Parse the attribute-list. */
13885 attribute_list = cp_parser_attribute_list (parser);
13887 /* If the next token is a `)', then there is no attribute
13889 attribute_list = NULL;
13891 /* Look for the two `)' tokens. */
13892 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13893 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13895 /* Add these new attributes to the list. */
13896 attributes = chainon (attributes, attribute_list);
13902 /* Parse an attribute-list.
13906 attribute-list , attribute
13910 identifier ( identifier )
13911 identifier ( identifier , expression-list )
13912 identifier ( expression-list )
13914 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13915 TREE_PURPOSE of each node is the identifier indicating which
13916 attribute is in use. The TREE_VALUE represents the arguments, if
13920 cp_parser_attribute_list (cp_parser* parser)
13922 tree attribute_list = NULL_TREE;
13923 bool save_translate_strings_p = parser->translate_strings_p;
13925 parser->translate_strings_p = false;
13932 /* Look for the identifier. We also allow keywords here; for
13933 example `__attribute__ ((const))' is legal. */
13934 token = cp_lexer_peek_token (parser->lexer);
13935 if (token->type != CPP_NAME
13936 && token->type != CPP_KEYWORD)
13937 return error_mark_node;
13938 /* Consume the token. */
13939 token = cp_lexer_consume_token (parser->lexer);
13941 /* Save away the identifier that indicates which attribute this is. */
13942 identifier = token->value;
13943 attribute = build_tree_list (identifier, NULL_TREE);
13945 /* Peek at the next token. */
13946 token = cp_lexer_peek_token (parser->lexer);
13947 /* If it's an `(', then parse the attribute arguments. */
13948 if (token->type == CPP_OPEN_PAREN)
13952 arguments = (cp_parser_parenthesized_expression_list
13953 (parser, true, /*non_constant_p=*/NULL));
13954 /* Save the identifier and arguments away. */
13955 TREE_VALUE (attribute) = arguments;
13958 /* Add this attribute to the list. */
13959 TREE_CHAIN (attribute) = attribute_list;
13960 attribute_list = attribute;
13962 /* Now, look for more attributes. */
13963 token = cp_lexer_peek_token (parser->lexer);
13964 /* If the next token isn't a `,', we're done. */
13965 if (token->type != CPP_COMMA)
13968 /* Consume the comma and keep going. */
13969 cp_lexer_consume_token (parser->lexer);
13971 parser->translate_strings_p = save_translate_strings_p;
13973 /* We built up the list in reverse order. */
13974 return nreverse (attribute_list);
13977 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13978 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13979 current value of the PEDANTIC flag, regardless of whether or not
13980 the `__extension__' keyword is present. The caller is responsible
13981 for restoring the value of the PEDANTIC flag. */
13984 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13986 /* Save the old value of the PEDANTIC flag. */
13987 *saved_pedantic = pedantic;
13989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13991 /* Consume the `__extension__' token. */
13992 cp_lexer_consume_token (parser->lexer);
13993 /* We're not being pedantic while the `__extension__' keyword is
14003 /* Parse a label declaration.
14006 __label__ label-declarator-seq ;
14008 label-declarator-seq:
14009 identifier , label-declarator-seq
14013 cp_parser_label_declaration (cp_parser* parser)
14015 /* Look for the `__label__' keyword. */
14016 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14022 /* Look for an identifier. */
14023 identifier = cp_parser_identifier (parser);
14024 /* Declare it as a lobel. */
14025 finish_label_decl (identifier);
14026 /* If the next token is a `;', stop. */
14027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14029 /* Look for the `,' separating the label declarations. */
14030 cp_parser_require (parser, CPP_COMMA, "`,'");
14033 /* Look for the final `;'. */
14034 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14037 /* Support Functions */
14039 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14040 NAME should have one of the representations used for an
14041 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14042 is returned. If PARSER->SCOPE is a dependent type, then a
14043 SCOPE_REF is returned.
14045 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14046 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14047 was formed. Abstractly, such entities should not be passed to this
14048 function, because they do not need to be looked up, but it is
14049 simpler to check for this special case here, rather than at the
14052 In cases not explicitly covered above, this function returns a
14053 DECL, OVERLOAD, or baselink representing the result of the lookup.
14054 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14057 If IS_TYPE is TRUE, bindings that do not refer to types are
14060 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14063 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14066 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14069 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14070 results in an ambiguity, and false otherwise. */
14073 cp_parser_lookup_name (cp_parser *parser, tree name,
14074 bool is_type, bool is_template, bool is_namespace,
14075 bool check_dependency,
14079 tree object_type = parser->context->object_type;
14081 /* Assume that the lookup will be unambiguous. */
14083 *ambiguous_p = false;
14085 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14086 no longer valid. Note that if we are parsing tentatively, and
14087 the parse fails, OBJECT_TYPE will be automatically restored. */
14088 parser->context->object_type = NULL_TREE;
14090 if (name == error_mark_node)
14091 return error_mark_node;
14093 /* A template-id has already been resolved; there is no lookup to
14095 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14097 if (BASELINK_P (name))
14099 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14100 == TEMPLATE_ID_EXPR);
14104 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14105 it should already have been checked to make sure that the name
14106 used matches the type being destroyed. */
14107 if (TREE_CODE (name) == BIT_NOT_EXPR)
14111 /* Figure out to which type this destructor applies. */
14113 type = parser->scope;
14114 else if (object_type)
14115 type = object_type;
14117 type = current_class_type;
14118 /* If that's not a class type, there is no destructor. */
14119 if (!type || !CLASS_TYPE_P (type))
14120 return error_mark_node;
14121 if (!CLASSTYPE_DESTRUCTORS (type))
14122 return error_mark_node;
14123 /* If it was a class type, return the destructor. */
14124 return CLASSTYPE_DESTRUCTORS (type);
14127 /* By this point, the NAME should be an ordinary identifier. If
14128 the id-expression was a qualified name, the qualifying scope is
14129 stored in PARSER->SCOPE at this point. */
14130 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14132 /* Perform the lookup. */
14137 if (parser->scope == error_mark_node)
14138 return error_mark_node;
14140 /* If the SCOPE is dependent, the lookup must be deferred until
14141 the template is instantiated -- unless we are explicitly
14142 looking up names in uninstantiated templates. Even then, we
14143 cannot look up the name if the scope is not a class type; it
14144 might, for example, be a template type parameter. */
14145 dependent_p = (TYPE_P (parser->scope)
14146 && !(parser->in_declarator_p
14147 && currently_open_class (parser->scope))
14148 && dependent_type_p (parser->scope));
14149 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14153 /* The resolution to Core Issue 180 says that `struct A::B'
14154 should be considered a type-name, even if `A' is
14156 decl = TYPE_NAME (make_typename_type (parser->scope,
14159 else if (is_template)
14160 decl = make_unbound_class_template (parser->scope,
14164 decl = build_nt (SCOPE_REF, parser->scope, name);
14168 bool pop_p = false;
14170 /* If PARSER->SCOPE is a dependent type, then it must be a
14171 class type, and we must not be checking dependencies;
14172 otherwise, we would have processed this lookup above. So
14173 that PARSER->SCOPE is not considered a dependent base by
14174 lookup_member, we must enter the scope here. */
14176 pop_p = push_scope (parser->scope);
14177 /* If the PARSER->SCOPE is a a template specialization, it
14178 may be instantiated during name lookup. In that case,
14179 errors may be issued. Even if we rollback the current
14180 tentative parse, those errors are valid. */
14181 decl = lookup_qualified_name (parser->scope, name, is_type,
14182 /*complain=*/true);
14184 pop_scope (parser->scope);
14186 parser->qualifying_scope = parser->scope;
14187 parser->object_scope = NULL_TREE;
14189 else if (object_type)
14191 tree object_decl = NULL_TREE;
14192 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14193 OBJECT_TYPE is not a class. */
14194 if (CLASS_TYPE_P (object_type))
14195 /* If the OBJECT_TYPE is a template specialization, it may
14196 be instantiated during name lookup. In that case, errors
14197 may be issued. Even if we rollback the current tentative
14198 parse, those errors are valid. */
14199 object_decl = lookup_member (object_type,
14201 /*protect=*/0, is_type);
14202 /* Look it up in the enclosing context, too. */
14203 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14204 /*block_p=*/true, is_namespace,
14206 parser->object_scope = object_type;
14207 parser->qualifying_scope = NULL_TREE;
14209 decl = object_decl;
14213 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14214 /*block_p=*/true, is_namespace,
14216 parser->qualifying_scope = NULL_TREE;
14217 parser->object_scope = NULL_TREE;
14220 /* If the lookup failed, let our caller know. */
14222 || decl == error_mark_node
14223 || (TREE_CODE (decl) == FUNCTION_DECL
14224 && DECL_ANTICIPATED (decl)))
14225 return error_mark_node;
14227 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14228 if (TREE_CODE (decl) == TREE_LIST)
14231 *ambiguous_p = true;
14232 /* The error message we have to print is too complicated for
14233 cp_parser_error, so we incorporate its actions directly. */
14234 if (!cp_parser_simulate_error (parser))
14236 error ("reference to %qD is ambiguous", name);
14237 print_candidates (decl);
14239 return error_mark_node;
14242 gcc_assert (DECL_P (decl)
14243 || TREE_CODE (decl) == OVERLOAD
14244 || TREE_CODE (decl) == SCOPE_REF
14245 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14246 || BASELINK_P (decl));
14248 /* If we have resolved the name of a member declaration, check to
14249 see if the declaration is accessible. When the name resolves to
14250 set of overloaded functions, accessibility is checked when
14251 overload resolution is done.
14253 During an explicit instantiation, access is not checked at all,
14254 as per [temp.explicit]. */
14256 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14261 /* Like cp_parser_lookup_name, but for use in the typical case where
14262 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14263 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14266 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14268 return cp_parser_lookup_name (parser, name,
14270 /*is_template=*/false,
14271 /*is_namespace=*/false,
14272 /*check_dependency=*/true,
14273 /*ambiguous_p=*/NULL);
14276 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14277 the current context, return the TYPE_DECL. If TAG_NAME_P is
14278 true, the DECL indicates the class being defined in a class-head,
14279 or declared in an elaborated-type-specifier.
14281 Otherwise, return DECL. */
14284 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14286 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14287 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14290 template <typename T> struct B;
14293 template <typename T> struct A::B {};
14295 Similarly, in a elaborated-type-specifier:
14297 namespace N { struct X{}; }
14300 template <typename T> friend struct N::X;
14303 However, if the DECL refers to a class type, and we are in
14304 the scope of the class, then the name lookup automatically
14305 finds the TYPE_DECL created by build_self_reference rather
14306 than a TEMPLATE_DECL. For example, in:
14308 template <class T> struct S {
14312 there is no need to handle such case. */
14314 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14315 return DECL_TEMPLATE_RESULT (decl);
14320 /* If too many, or too few, template-parameter lists apply to the
14321 declarator, issue an error message. Returns TRUE if all went well,
14322 and FALSE otherwise. */
14325 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14326 cp_declarator *declarator)
14328 unsigned num_templates;
14330 /* We haven't seen any classes that involve template parameters yet. */
14333 switch (declarator->kind)
14336 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14341 scope = TREE_OPERAND (declarator->u.id.name, 0);
14342 member = TREE_OPERAND (declarator->u.id.name, 1);
14344 while (scope && CLASS_TYPE_P (scope))
14346 /* You're supposed to have one `template <...>'
14347 for every template class, but you don't need one
14348 for a full specialization. For example:
14350 template <class T> struct S{};
14351 template <> struct S<int> { void f(); };
14352 void S<int>::f () {}
14354 is correct; there shouldn't be a `template <>' for
14355 the definition of `S<int>::f'. */
14356 if (CLASSTYPE_TEMPLATE_INFO (scope)
14357 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14358 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14359 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14362 scope = TYPE_CONTEXT (scope);
14366 /* If the DECLARATOR has the form `X<y>' then it uses one
14367 additional level of template parameters. */
14368 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14371 return cp_parser_check_template_parameters (parser,
14377 case cdk_reference:
14379 return (cp_parser_check_declarator_template_parameters
14380 (parser, declarator->declarator));
14386 gcc_unreachable ();
14391 /* NUM_TEMPLATES were used in the current declaration. If that is
14392 invalid, return FALSE and issue an error messages. Otherwise,
14396 cp_parser_check_template_parameters (cp_parser* parser,
14397 unsigned num_templates)
14399 /* If there are more template classes than parameter lists, we have
14402 template <class T> void S<T>::R<T>::f (); */
14403 if (parser->num_template_parameter_lists < num_templates)
14405 error ("too few template-parameter-lists");
14408 /* If there are the same number of template classes and parameter
14409 lists, that's OK. */
14410 if (parser->num_template_parameter_lists == num_templates)
14412 /* If there are more, but only one more, then we are referring to a
14413 member template. That's OK too. */
14414 if (parser->num_template_parameter_lists == num_templates + 1)
14416 /* Otherwise, there are too many template parameter lists. We have
14419 template <class T> template <class U> void S::f(); */
14420 error ("too many template-parameter-lists");
14424 /* Parse an optional `::' token indicating that the following name is
14425 from the global namespace. If so, PARSER->SCOPE is set to the
14426 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14427 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14428 Returns the new value of PARSER->SCOPE, if the `::' token is
14429 present, and NULL_TREE otherwise. */
14432 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14436 /* Peek at the next token. */
14437 token = cp_lexer_peek_token (parser->lexer);
14438 /* If we're looking at a `::' token then we're starting from the
14439 global namespace, not our current location. */
14440 if (token->type == CPP_SCOPE)
14442 /* Consume the `::' token. */
14443 cp_lexer_consume_token (parser->lexer);
14444 /* Set the SCOPE so that we know where to start the lookup. */
14445 parser->scope = global_namespace;
14446 parser->qualifying_scope = global_namespace;
14447 parser->object_scope = NULL_TREE;
14449 return parser->scope;
14451 else if (!current_scope_valid_p)
14453 parser->scope = NULL_TREE;
14454 parser->qualifying_scope = NULL_TREE;
14455 parser->object_scope = NULL_TREE;
14461 /* Returns TRUE if the upcoming token sequence is the start of a
14462 constructor declarator. If FRIEND_P is true, the declarator is
14463 preceded by the `friend' specifier. */
14466 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14468 bool constructor_p;
14469 tree type_decl = NULL_TREE;
14470 bool nested_name_p;
14471 cp_token *next_token;
14473 /* The common case is that this is not a constructor declarator, so
14474 try to avoid doing lots of work if at all possible. It's not
14475 valid declare a constructor at function scope. */
14476 if (at_function_scope_p ())
14478 /* And only certain tokens can begin a constructor declarator. */
14479 next_token = cp_lexer_peek_token (parser->lexer);
14480 if (next_token->type != CPP_NAME
14481 && next_token->type != CPP_SCOPE
14482 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14483 && next_token->type != CPP_TEMPLATE_ID)
14486 /* Parse tentatively; we are going to roll back all of the tokens
14488 cp_parser_parse_tentatively (parser);
14489 /* Assume that we are looking at a constructor declarator. */
14490 constructor_p = true;
14492 /* Look for the optional `::' operator. */
14493 cp_parser_global_scope_opt (parser,
14494 /*current_scope_valid_p=*/false);
14495 /* Look for the nested-name-specifier. */
14497 = (cp_parser_nested_name_specifier_opt (parser,
14498 /*typename_keyword_p=*/false,
14499 /*check_dependency_p=*/false,
14501 /*is_declaration=*/false)
14503 /* Outside of a class-specifier, there must be a
14504 nested-name-specifier. */
14505 if (!nested_name_p &&
14506 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14508 constructor_p = false;
14509 /* If we still think that this might be a constructor-declarator,
14510 look for a class-name. */
14515 template <typename T> struct S { S(); };
14516 template <typename T> S<T>::S ();
14518 we must recognize that the nested `S' names a class.
14521 template <typename T> S<T>::S<T> ();
14523 we must recognize that the nested `S' names a template. */
14524 type_decl = cp_parser_class_name (parser,
14525 /*typename_keyword_p=*/false,
14526 /*template_keyword_p=*/false,
14528 /*check_dependency_p=*/false,
14529 /*class_head_p=*/false,
14530 /*is_declaration=*/false);
14531 /* If there was no class-name, then this is not a constructor. */
14532 constructor_p = !cp_parser_error_occurred (parser);
14535 /* If we're still considering a constructor, we have to see a `(',
14536 to begin the parameter-declaration-clause, followed by either a
14537 `)', an `...', or a decl-specifier. We need to check for a
14538 type-specifier to avoid being fooled into thinking that:
14542 is a constructor. (It is actually a function named `f' that
14543 takes one parameter (of type `int') and returns a value of type
14546 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14549 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14550 /* A parameter declaration begins with a decl-specifier,
14551 which is either the "attribute" keyword, a storage class
14552 specifier, or (usually) a type-specifier. */
14553 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14554 && !cp_parser_storage_class_specifier_opt (parser))
14557 bool pop_p = false;
14558 unsigned saved_num_template_parameter_lists;
14560 /* Names appearing in the type-specifier should be looked up
14561 in the scope of the class. */
14562 if (current_class_type)
14566 type = TREE_TYPE (type_decl);
14567 if (TREE_CODE (type) == TYPENAME_TYPE)
14569 type = resolve_typename_type (type,
14570 /*only_current_p=*/false);
14571 if (type == error_mark_node)
14573 cp_parser_abort_tentative_parse (parser);
14577 pop_p = push_scope (type);
14580 /* Inside the constructor parameter list, surrounding
14581 template-parameter-lists do not apply. */
14582 saved_num_template_parameter_lists
14583 = parser->num_template_parameter_lists;
14584 parser->num_template_parameter_lists = 0;
14586 /* Look for the type-specifier. */
14587 cp_parser_type_specifier (parser,
14588 CP_PARSER_FLAGS_NONE,
14589 /*decl_specs=*/NULL,
14590 /*is_declarator=*/true,
14591 /*declares_class_or_enum=*/NULL,
14592 /*is_cv_qualifier=*/NULL);
14594 parser->num_template_parameter_lists
14595 = saved_num_template_parameter_lists;
14597 /* Leave the scope of the class. */
14601 constructor_p = !cp_parser_error_occurred (parser);
14605 constructor_p = false;
14606 /* We did not really want to consume any tokens. */
14607 cp_parser_abort_tentative_parse (parser);
14609 return constructor_p;
14612 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14613 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14614 they must be performed once we are in the scope of the function.
14616 Returns the function defined. */
14619 cp_parser_function_definition_from_specifiers_and_declarator
14620 (cp_parser* parser,
14621 cp_decl_specifier_seq *decl_specifiers,
14623 const cp_declarator *declarator)
14628 /* Begin the function-definition. */
14629 success_p = start_function (decl_specifiers, declarator, attributes);
14631 /* The things we're about to see are not directly qualified by any
14632 template headers we've seen thus far. */
14633 reset_specialization ();
14635 /* If there were names looked up in the decl-specifier-seq that we
14636 did not check, check them now. We must wait until we are in the
14637 scope of the function to perform the checks, since the function
14638 might be a friend. */
14639 perform_deferred_access_checks ();
14643 /* Skip the entire function. */
14644 error ("invalid function declaration");
14645 cp_parser_skip_to_end_of_block_or_statement (parser);
14646 fn = error_mark_node;
14649 fn = cp_parser_function_definition_after_declarator (parser,
14650 /*inline_p=*/false);
14655 /* Parse the part of a function-definition that follows the
14656 declarator. INLINE_P is TRUE iff this function is an inline
14657 function defined with a class-specifier.
14659 Returns the function defined. */
14662 cp_parser_function_definition_after_declarator (cp_parser* parser,
14666 bool ctor_initializer_p = false;
14667 bool saved_in_unbraced_linkage_specification_p;
14668 unsigned saved_num_template_parameter_lists;
14670 /* If the next token is `return', then the code may be trying to
14671 make use of the "named return value" extension that G++ used to
14673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14675 /* Consume the `return' keyword. */
14676 cp_lexer_consume_token (parser->lexer);
14677 /* Look for the identifier that indicates what value is to be
14679 cp_parser_identifier (parser);
14680 /* Issue an error message. */
14681 error ("named return values are no longer supported");
14682 /* Skip tokens until we reach the start of the function body. */
14683 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14684 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14685 cp_lexer_consume_token (parser->lexer);
14687 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14688 anything declared inside `f'. */
14689 saved_in_unbraced_linkage_specification_p
14690 = parser->in_unbraced_linkage_specification_p;
14691 parser->in_unbraced_linkage_specification_p = false;
14692 /* Inside the function, surrounding template-parameter-lists do not
14694 saved_num_template_parameter_lists
14695 = parser->num_template_parameter_lists;
14696 parser->num_template_parameter_lists = 0;
14697 /* If the next token is `try', then we are looking at a
14698 function-try-block. */
14699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14700 ctor_initializer_p = cp_parser_function_try_block (parser);
14701 /* A function-try-block includes the function-body, so we only do
14702 this next part if we're not processing a function-try-block. */
14705 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14707 /* Finish the function. */
14708 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14709 (inline_p ? 2 : 0));
14710 /* Generate code for it, if necessary. */
14711 expand_or_defer_fn (fn);
14712 /* Restore the saved values. */
14713 parser->in_unbraced_linkage_specification_p
14714 = saved_in_unbraced_linkage_specification_p;
14715 parser->num_template_parameter_lists
14716 = saved_num_template_parameter_lists;
14721 /* Parse a template-declaration, assuming that the `export' (and
14722 `extern') keywords, if present, has already been scanned. MEMBER_P
14723 is as for cp_parser_template_declaration. */
14726 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14728 tree decl = NULL_TREE;
14729 tree parameter_list;
14730 bool friend_p = false;
14732 /* Look for the `template' keyword. */
14733 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14737 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14740 /* If the next token is `>', then we have an invalid
14741 specialization. Rather than complain about an invalid template
14742 parameter, issue an error message here. */
14743 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14745 cp_parser_error (parser, "invalid explicit specialization");
14746 begin_specialization ();
14747 parameter_list = NULL_TREE;
14751 /* Parse the template parameters. */
14752 begin_template_parm_list ();
14753 parameter_list = cp_parser_template_parameter_list (parser);
14754 parameter_list = end_template_parm_list (parameter_list);
14757 /* Look for the `>'. */
14758 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14759 /* We just processed one more parameter list. */
14760 ++parser->num_template_parameter_lists;
14761 /* If the next token is `template', there are more template
14763 if (cp_lexer_next_token_is_keyword (parser->lexer,
14765 cp_parser_template_declaration_after_export (parser, member_p);
14768 /* There are no access checks when parsing a template, as we do not
14769 know if a specialization will be a friend. */
14770 push_deferring_access_checks (dk_no_check);
14772 decl = cp_parser_single_declaration (parser,
14776 pop_deferring_access_checks ();
14778 /* If this is a member template declaration, let the front
14780 if (member_p && !friend_p && decl)
14782 if (TREE_CODE (decl) == TYPE_DECL)
14783 cp_parser_check_access_in_redeclaration (decl);
14785 decl = finish_member_template_decl (decl);
14787 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14788 make_friend_class (current_class_type, TREE_TYPE (decl),
14789 /*complain=*/true);
14791 /* We are done with the current parameter list. */
14792 --parser->num_template_parameter_lists;
14795 finish_template_decl (parameter_list);
14797 /* Register member declarations. */
14798 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14799 finish_member_declaration (decl);
14801 /* If DECL is a function template, we must return to parse it later.
14802 (Even though there is no definition, there might be default
14803 arguments that need handling.) */
14804 if (member_p && decl
14805 && (TREE_CODE (decl) == FUNCTION_DECL
14806 || DECL_FUNCTION_TEMPLATE_P (decl)))
14807 TREE_VALUE (parser->unparsed_functions_queues)
14808 = tree_cons (NULL_TREE, decl,
14809 TREE_VALUE (parser->unparsed_functions_queues));
14812 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14813 `function-definition' sequence. MEMBER_P is true, this declaration
14814 appears in a class scope.
14816 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14817 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14820 cp_parser_single_declaration (cp_parser* parser,
14824 int declares_class_or_enum;
14825 tree decl = NULL_TREE;
14826 cp_decl_specifier_seq decl_specifiers;
14827 bool function_definition_p = false;
14829 /* Defer access checks until we know what is being declared. */
14830 push_deferring_access_checks (dk_deferred);
14832 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14834 cp_parser_decl_specifier_seq (parser,
14835 CP_PARSER_FLAGS_OPTIONAL,
14837 &declares_class_or_enum);
14839 *friend_p = cp_parser_friend_p (&decl_specifiers);
14840 /* Gather up the access checks that occurred the
14841 decl-specifier-seq. */
14842 stop_deferring_access_checks ();
14844 /* Check for the declaration of a template class. */
14845 if (declares_class_or_enum)
14847 if (cp_parser_declares_only_class_p (parser))
14849 decl = shadow_tag (&decl_specifiers);
14850 if (decl && decl != error_mark_node)
14851 decl = TYPE_NAME (decl);
14853 decl = error_mark_node;
14858 /* If it's not a template class, try for a template function. If
14859 the next token is a `;', then this declaration does not declare
14860 anything. But, if there were errors in the decl-specifiers, then
14861 the error might well have come from an attempted class-specifier.
14862 In that case, there's no need to warn about a missing declarator. */
14864 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14865 || decl_specifiers.type != error_mark_node))
14866 decl = cp_parser_init_declarator (parser,
14868 /*function_definition_allowed_p=*/true,
14870 declares_class_or_enum,
14871 &function_definition_p);
14873 pop_deferring_access_checks ();
14875 /* Clear any current qualification; whatever comes next is the start
14876 of something new. */
14877 parser->scope = NULL_TREE;
14878 parser->qualifying_scope = NULL_TREE;
14879 parser->object_scope = NULL_TREE;
14880 /* Look for a trailing `;' after the declaration. */
14881 if (!function_definition_p
14882 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14883 cp_parser_skip_to_end_of_block_or_statement (parser);
14888 /* Parse a cast-expression that is not the operand of a unary "&". */
14891 cp_parser_simple_cast_expression (cp_parser *parser)
14893 return cp_parser_cast_expression (parser, /*address_p=*/false);
14896 /* Parse a functional cast to TYPE. Returns an expression
14897 representing the cast. */
14900 cp_parser_functional_cast (cp_parser* parser, tree type)
14902 tree expression_list;
14906 = cp_parser_parenthesized_expression_list (parser, false,
14907 /*non_constant_p=*/NULL);
14909 cast = build_functional_cast (type, expression_list);
14910 /* [expr.const]/1: In an integral constant expression "only type
14911 conversions to integral or enumeration type can be used". */
14912 if (cast != error_mark_node && !type_dependent_expression_p (type)
14913 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14915 if (cp_parser_non_integral_constant_expression
14916 (parser, "a call to a constructor"))
14917 return error_mark_node;
14922 /* Save the tokens that make up the body of a member function defined
14923 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14924 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14925 specifiers applied to the declaration. Returns the FUNCTION_DECL
14926 for the member function. */
14929 cp_parser_save_member_function_body (cp_parser* parser,
14930 cp_decl_specifier_seq *decl_specifiers,
14931 cp_declarator *declarator,
14938 /* Create the function-declaration. */
14939 fn = start_method (decl_specifiers, declarator, attributes);
14940 /* If something went badly wrong, bail out now. */
14941 if (fn == error_mark_node)
14943 /* If there's a function-body, skip it. */
14944 if (cp_parser_token_starts_function_definition_p
14945 (cp_lexer_peek_token (parser->lexer)))
14946 cp_parser_skip_to_end_of_block_or_statement (parser);
14947 return error_mark_node;
14950 /* Remember it, if there default args to post process. */
14951 cp_parser_save_default_args (parser, fn);
14953 /* Save away the tokens that make up the body of the
14955 first = parser->lexer->next_token;
14956 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14957 /* Handle function try blocks. */
14958 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14959 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14960 last = parser->lexer->next_token;
14962 /* Save away the inline definition; we will process it when the
14963 class is complete. */
14964 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14965 DECL_PENDING_INLINE_P (fn) = 1;
14967 /* We need to know that this was defined in the class, so that
14968 friend templates are handled correctly. */
14969 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14971 /* We're done with the inline definition. */
14972 finish_method (fn);
14974 /* Add FN to the queue of functions to be parsed later. */
14975 TREE_VALUE (parser->unparsed_functions_queues)
14976 = tree_cons (NULL_TREE, fn,
14977 TREE_VALUE (parser->unparsed_functions_queues));
14982 /* Parse a template-argument-list, as well as the trailing ">" (but
14983 not the opening ">"). See cp_parser_template_argument_list for the
14987 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14991 tree saved_qualifying_scope;
14992 tree saved_object_scope;
14993 bool saved_greater_than_is_operator_p;
14997 When parsing a template-id, the first non-nested `>' is taken as
14998 the end of the template-argument-list rather than a greater-than
15000 saved_greater_than_is_operator_p
15001 = parser->greater_than_is_operator_p;
15002 parser->greater_than_is_operator_p = false;
15003 /* Parsing the argument list may modify SCOPE, so we save it
15005 saved_scope = parser->scope;
15006 saved_qualifying_scope = parser->qualifying_scope;
15007 saved_object_scope = parser->object_scope;
15008 /* Parse the template-argument-list itself. */
15009 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15010 arguments = NULL_TREE;
15012 arguments = cp_parser_template_argument_list (parser);
15013 /* Look for the `>' that ends the template-argument-list. If we find
15014 a '>>' instead, it's probably just a typo. */
15015 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15017 if (!saved_greater_than_is_operator_p)
15019 /* If we're in a nested template argument list, the '>>' has
15020 to be a typo for '> >'. We emit the error message, but we
15021 continue parsing and we push a '>' as next token, so that
15022 the argument list will be parsed correctly. Note that the
15023 global source location is still on the token before the
15024 '>>', so we need to say explicitly where we want it. */
15025 cp_token *token = cp_lexer_peek_token (parser->lexer);
15026 error ("%H%<>>%> should be %<> >%> "
15027 "within a nested template argument list",
15030 /* ??? Proper recovery should terminate two levels of
15031 template argument list here. */
15032 token->type = CPP_GREATER;
15036 /* If this is not a nested template argument list, the '>>'
15037 is a typo for '>'. Emit an error message and continue.
15038 Same deal about the token location, but here we can get it
15039 right by consuming the '>>' before issuing the diagnostic. */
15040 cp_lexer_consume_token (parser->lexer);
15041 error ("spurious %<>>%>, use %<>%> to terminate "
15042 "a template argument list");
15045 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15046 error ("missing %<>%> to terminate the template argument list");
15048 /* It's what we want, a '>'; consume it. */
15049 cp_lexer_consume_token (parser->lexer);
15050 /* The `>' token might be a greater-than operator again now. */
15051 parser->greater_than_is_operator_p
15052 = saved_greater_than_is_operator_p;
15053 /* Restore the SAVED_SCOPE. */
15054 parser->scope = saved_scope;
15055 parser->qualifying_scope = saved_qualifying_scope;
15056 parser->object_scope = saved_object_scope;
15061 /* MEMBER_FUNCTION is a member function, or a friend. If default
15062 arguments, or the body of the function have not yet been parsed,
15066 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15068 /* If this member is a template, get the underlying
15070 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15071 member_function = DECL_TEMPLATE_RESULT (member_function);
15073 /* There should not be any class definitions in progress at this
15074 point; the bodies of members are only parsed outside of all class
15076 gcc_assert (parser->num_classes_being_defined == 0);
15077 /* While we're parsing the member functions we might encounter more
15078 classes. We want to handle them right away, but we don't want
15079 them getting mixed up with functions that are currently in the
15081 parser->unparsed_functions_queues
15082 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15084 /* Make sure that any template parameters are in scope. */
15085 maybe_begin_member_template_processing (member_function);
15087 /* If the body of the function has not yet been parsed, parse it
15089 if (DECL_PENDING_INLINE_P (member_function))
15091 tree function_scope;
15092 cp_token_cache *tokens;
15094 /* The function is no longer pending; we are processing it. */
15095 tokens = DECL_PENDING_INLINE_INFO (member_function);
15096 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15097 DECL_PENDING_INLINE_P (member_function) = 0;
15098 /* If this was an inline function in a local class, enter the scope
15099 of the containing function. */
15100 function_scope = decl_function_context (member_function);
15101 if (function_scope)
15102 push_function_context_to (function_scope);
15104 /* Push the body of the function onto the lexer stack. */
15105 cp_parser_push_lexer_for_tokens (parser, tokens);
15107 /* Let the front end know that we going to be defining this
15109 start_preparsed_function (member_function, NULL_TREE,
15110 SF_PRE_PARSED | SF_INCLASS_INLINE);
15112 /* Now, parse the body of the function. */
15113 cp_parser_function_definition_after_declarator (parser,
15114 /*inline_p=*/true);
15116 /* Leave the scope of the containing function. */
15117 if (function_scope)
15118 pop_function_context_from (function_scope);
15119 cp_parser_pop_lexer (parser);
15122 /* Remove any template parameters from the symbol table. */
15123 maybe_end_member_template_processing ();
15125 /* Restore the queue. */
15126 parser->unparsed_functions_queues
15127 = TREE_CHAIN (parser->unparsed_functions_queues);
15130 /* If DECL contains any default args, remember it on the unparsed
15131 functions queue. */
15134 cp_parser_save_default_args (cp_parser* parser, tree decl)
15138 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15140 probe = TREE_CHAIN (probe))
15141 if (TREE_PURPOSE (probe))
15143 TREE_PURPOSE (parser->unparsed_functions_queues)
15144 = tree_cons (current_class_type, decl,
15145 TREE_PURPOSE (parser->unparsed_functions_queues));
15151 /* FN is a FUNCTION_DECL which may contains a parameter with an
15152 unparsed DEFAULT_ARG. Parse the default args now. This function
15153 assumes that the current scope is the scope in which the default
15154 argument should be processed. */
15157 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15159 bool saved_local_variables_forbidden_p;
15162 /* While we're parsing the default args, we might (due to the
15163 statement expression extension) encounter more classes. We want
15164 to handle them right away, but we don't want them getting mixed
15165 up with default args that are currently in the queue. */
15166 parser->unparsed_functions_queues
15167 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15169 /* Local variable names (and the `this' keyword) may not appear
15170 in a default argument. */
15171 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15172 parser->local_variables_forbidden_p = true;
15174 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15176 parm = TREE_CHAIN (parm))
15178 cp_token_cache *tokens;
15180 if (!TREE_PURPOSE (parm)
15181 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15184 /* Push the saved tokens for the default argument onto the parser's
15186 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15187 cp_parser_push_lexer_for_tokens (parser, tokens);
15189 /* Parse the assignment-expression. */
15190 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15192 /* If the token stream has not been completely used up, then
15193 there was extra junk after the end of the default
15195 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15196 cp_parser_error (parser, "expected %<,%>");
15198 /* Revert to the main lexer. */
15199 cp_parser_pop_lexer (parser);
15202 /* Restore the state of local_variables_forbidden_p. */
15203 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15205 /* Restore the queue. */
15206 parser->unparsed_functions_queues
15207 = TREE_CHAIN (parser->unparsed_functions_queues);
15210 /* Parse the operand of `sizeof' (or a similar operator). Returns
15211 either a TYPE or an expression, depending on the form of the
15212 input. The KEYWORD indicates which kind of expression we have
15216 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15218 static const char *format;
15219 tree expr = NULL_TREE;
15220 const char *saved_message;
15221 bool saved_integral_constant_expression_p;
15223 /* Initialize FORMAT the first time we get here. */
15225 format = "types may not be defined in `%s' expressions";
15227 /* Types cannot be defined in a `sizeof' expression. Save away the
15229 saved_message = parser->type_definition_forbidden_message;
15230 /* And create the new one. */
15231 parser->type_definition_forbidden_message
15232 = xmalloc (strlen (format)
15233 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15235 sprintf ((char *) parser->type_definition_forbidden_message,
15236 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15238 /* The restrictions on constant-expressions do not apply inside
15239 sizeof expressions. */
15240 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15241 parser->integral_constant_expression_p = false;
15243 /* Do not actually evaluate the expression. */
15245 /* If it's a `(', then we might be looking at the type-id
15247 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15250 bool saved_in_type_id_in_expr_p;
15252 /* We can't be sure yet whether we're looking at a type-id or an
15254 cp_parser_parse_tentatively (parser);
15255 /* Consume the `('. */
15256 cp_lexer_consume_token (parser->lexer);
15257 /* Parse the type-id. */
15258 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15259 parser->in_type_id_in_expr_p = true;
15260 type = cp_parser_type_id (parser);
15261 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15262 /* Now, look for the trailing `)'. */
15263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15264 /* If all went well, then we're done. */
15265 if (cp_parser_parse_definitely (parser))
15267 cp_decl_specifier_seq decl_specs;
15269 /* Build a trivial decl-specifier-seq. */
15270 clear_decl_specs (&decl_specs);
15271 decl_specs.type = type;
15273 /* Call grokdeclarator to figure out what type this is. */
15274 expr = grokdeclarator (NULL,
15278 /*attrlist=*/NULL);
15282 /* If the type-id production did not work out, then we must be
15283 looking at the unary-expression production. */
15285 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15286 /* Go back to evaluating expressions. */
15289 /* Free the message we created. */
15290 free ((char *) parser->type_definition_forbidden_message);
15291 /* And restore the old one. */
15292 parser->type_definition_forbidden_message = saved_message;
15293 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15298 /* If the current declaration has no declarator, return true. */
15301 cp_parser_declares_only_class_p (cp_parser *parser)
15303 /* If the next token is a `;' or a `,' then there is no
15305 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15306 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15309 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15312 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15313 cp_storage_class storage_class)
15315 if (decl_specs->storage_class != sc_none)
15316 decl_specs->multiple_storage_classes_p = true;
15318 decl_specs->storage_class = storage_class;
15321 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15322 is true, the type is a user-defined type; otherwise it is a
15323 built-in type specified by a keyword. */
15326 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15328 bool user_defined_p)
15330 decl_specs->any_specifiers_p = true;
15332 /* If the user tries to redeclare a built-in type (with, for example,
15333 in "typedef int wchar_t;") we remember that this is what
15334 happened. In system headers, we ignore these declarations so
15335 that G++ can work with system headers that are not C++-safe. */
15336 if (decl_specs->specs[(int) ds_typedef]
15338 && (decl_specs->type
15339 || decl_specs->specs[(int) ds_long]
15340 || decl_specs->specs[(int) ds_short]
15341 || decl_specs->specs[(int) ds_unsigned]
15342 || decl_specs->specs[(int) ds_signed]))
15344 decl_specs->redefined_builtin_type = type_spec;
15345 if (!decl_specs->type)
15347 decl_specs->type = type_spec;
15348 decl_specs->user_defined_type_p = false;
15351 else if (decl_specs->type)
15352 decl_specs->multiple_types_p = true;
15355 decl_specs->type = type_spec;
15356 decl_specs->user_defined_type_p = user_defined_p;
15357 decl_specs->redefined_builtin_type = NULL_TREE;
15361 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15362 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15365 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15367 return decl_specifiers->specs[(int) ds_friend] != 0;
15370 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15371 issue an error message indicating that TOKEN_DESC was expected.
15373 Returns the token consumed, if the token had the appropriate type.
15374 Otherwise, returns NULL. */
15377 cp_parser_require (cp_parser* parser,
15378 enum cpp_ttype type,
15379 const char* token_desc)
15381 if (cp_lexer_next_token_is (parser->lexer, type))
15382 return cp_lexer_consume_token (parser->lexer);
15385 /* Output the MESSAGE -- unless we're parsing tentatively. */
15386 if (!cp_parser_simulate_error (parser))
15388 char *message = concat ("expected ", token_desc, NULL);
15389 cp_parser_error (parser, message);
15396 /* Like cp_parser_require, except that tokens will be skipped until
15397 the desired token is found. An error message is still produced if
15398 the next token is not as expected. */
15401 cp_parser_skip_until_found (cp_parser* parser,
15402 enum cpp_ttype type,
15403 const char* token_desc)
15406 unsigned nesting_depth = 0;
15408 if (cp_parser_require (parser, type, token_desc))
15411 /* Skip tokens until the desired token is found. */
15414 /* Peek at the next token. */
15415 token = cp_lexer_peek_token (parser->lexer);
15416 /* If we've reached the token we want, consume it and
15418 if (token->type == type && !nesting_depth)
15420 cp_lexer_consume_token (parser->lexer);
15423 /* If we've run out of tokens, stop. */
15424 if (token->type == CPP_EOF)
15426 if (token->type == CPP_OPEN_BRACE
15427 || token->type == CPP_OPEN_PAREN
15428 || token->type == CPP_OPEN_SQUARE)
15430 else if (token->type == CPP_CLOSE_BRACE
15431 || token->type == CPP_CLOSE_PAREN
15432 || token->type == CPP_CLOSE_SQUARE)
15434 if (nesting_depth-- == 0)
15437 /* Consume this token. */
15438 cp_lexer_consume_token (parser->lexer);
15442 /* If the next token is the indicated keyword, consume it. Otherwise,
15443 issue an error message indicating that TOKEN_DESC was expected.
15445 Returns the token consumed, if the token had the appropriate type.
15446 Otherwise, returns NULL. */
15449 cp_parser_require_keyword (cp_parser* parser,
15451 const char* token_desc)
15453 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15455 if (token && token->keyword != keyword)
15457 dyn_string_t error_msg;
15459 /* Format the error message. */
15460 error_msg = dyn_string_new (0);
15461 dyn_string_append_cstr (error_msg, "expected ");
15462 dyn_string_append_cstr (error_msg, token_desc);
15463 cp_parser_error (parser, error_msg->s);
15464 dyn_string_delete (error_msg);
15471 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15472 function-definition. */
15475 cp_parser_token_starts_function_definition_p (cp_token* token)
15477 return (/* An ordinary function-body begins with an `{'. */
15478 token->type == CPP_OPEN_BRACE
15479 /* A ctor-initializer begins with a `:'. */
15480 || token->type == CPP_COLON
15481 /* A function-try-block begins with `try'. */
15482 || token->keyword == RID_TRY
15483 /* The named return value extension begins with `return'. */
15484 || token->keyword == RID_RETURN);
15487 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15491 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15495 token = cp_lexer_peek_token (parser->lexer);
15496 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15499 /* Returns TRUE iff the next token is the "," or ">" ending a
15500 template-argument. ">>" is also accepted (after the full
15501 argument was parsed) because it's probably a typo for "> >",
15502 and there is a specific diagnostic for this. */
15505 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15509 token = cp_lexer_peek_token (parser->lexer);
15510 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15511 || token->type == CPP_RSHIFT);
15514 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15515 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15518 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15523 token = cp_lexer_peek_nth_token (parser->lexer, n);
15524 if (token->type == CPP_LESS)
15526 /* Check for the sequence `<::' in the original code. It would be lexed as
15527 `[:', where `[' is a digraph, and there is no whitespace before
15529 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15532 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15533 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15539 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15540 or none_type otherwise. */
15542 static enum tag_types
15543 cp_parser_token_is_class_key (cp_token* token)
15545 switch (token->keyword)
15550 return record_type;
15559 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15562 cp_parser_check_class_key (enum tag_types class_key, tree type)
15564 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15565 pedwarn ("%qs tag used in naming %q#T",
15566 class_key == union_type ? "union"
15567 : class_key == record_type ? "struct" : "class",
15571 /* Issue an error message if DECL is redeclared with different
15572 access than its original declaration [class.access.spec/3].
15573 This applies to nested classes and nested class templates.
15577 cp_parser_check_access_in_redeclaration (tree decl)
15579 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15582 if ((TREE_PRIVATE (decl)
15583 != (current_access_specifier == access_private_node))
15584 || (TREE_PROTECTED (decl)
15585 != (current_access_specifier == access_protected_node)))
15586 error ("%qD redeclared with different access", decl);
15589 /* Look for the `template' keyword, as a syntactic disambiguator.
15590 Return TRUE iff it is present, in which case it will be
15594 cp_parser_optional_template_keyword (cp_parser *parser)
15596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15598 /* The `template' keyword can only be used within templates;
15599 outside templates the parser can always figure out what is a
15600 template and what is not. */
15601 if (!processing_template_decl)
15603 error ("%<template%> (as a disambiguator) is only allowed "
15604 "within templates");
15605 /* If this part of the token stream is rescanned, the same
15606 error message would be generated. So, we purge the token
15607 from the stream. */
15608 cp_lexer_purge_token (parser->lexer);
15613 /* Consume the `template' keyword. */
15614 cp_lexer_consume_token (parser->lexer);
15622 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15623 set PARSER->SCOPE, and perform other related actions. */
15626 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15631 /* Get the stored value. */
15632 value = cp_lexer_consume_token (parser->lexer)->value;
15633 /* Perform any access checks that were deferred. */
15634 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15635 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15636 /* Set the scope from the stored value. */
15637 parser->scope = TREE_VALUE (value);
15638 parser->qualifying_scope = TREE_TYPE (value);
15639 parser->object_scope = NULL_TREE;
15642 /* Consume tokens up through a non-nested END token. */
15645 cp_parser_cache_group (cp_parser *parser,
15646 enum cpp_ttype end,
15653 /* Abort a parenthesized expression if we encounter a brace. */
15654 if ((end == CPP_CLOSE_PAREN || depth == 0)
15655 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15657 /* If we've reached the end of the file, stop. */
15658 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15660 /* Consume the next token. */
15661 token = cp_lexer_consume_token (parser->lexer);
15662 /* See if it starts a new group. */
15663 if (token->type == CPP_OPEN_BRACE)
15665 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15669 else if (token->type == CPP_OPEN_PAREN)
15670 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15671 else if (token->type == end)
15676 /* Begin parsing tentatively. We always save tokens while parsing
15677 tentatively so that if the tentative parsing fails we can restore the
15681 cp_parser_parse_tentatively (cp_parser* parser)
15683 /* Enter a new parsing context. */
15684 parser->context = cp_parser_context_new (parser->context);
15685 /* Begin saving tokens. */
15686 cp_lexer_save_tokens (parser->lexer);
15687 /* In order to avoid repetitive access control error messages,
15688 access checks are queued up until we are no longer parsing
15690 push_deferring_access_checks (dk_deferred);
15693 /* Commit to the currently active tentative parse. */
15696 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15698 cp_parser_context *context;
15701 /* Mark all of the levels as committed. */
15702 lexer = parser->lexer;
15703 for (context = parser->context; context->next; context = context->next)
15705 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15707 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15708 while (!cp_lexer_saving_tokens (lexer))
15709 lexer = lexer->next;
15710 cp_lexer_commit_tokens (lexer);
15714 /* Abort the currently active tentative parse. All consumed tokens
15715 will be rolled back, and no diagnostics will be issued. */
15718 cp_parser_abort_tentative_parse (cp_parser* parser)
15720 cp_parser_simulate_error (parser);
15721 /* Now, pretend that we want to see if the construct was
15722 successfully parsed. */
15723 cp_parser_parse_definitely (parser);
15726 /* Stop parsing tentatively. If a parse error has occurred, restore the
15727 token stream. Otherwise, commit to the tokens we have consumed.
15728 Returns true if no error occurred; false otherwise. */
15731 cp_parser_parse_definitely (cp_parser* parser)
15733 bool error_occurred;
15734 cp_parser_context *context;
15736 /* Remember whether or not an error occurred, since we are about to
15737 destroy that information. */
15738 error_occurred = cp_parser_error_occurred (parser);
15739 /* Remove the topmost context from the stack. */
15740 context = parser->context;
15741 parser->context = context->next;
15742 /* If no parse errors occurred, commit to the tentative parse. */
15743 if (!error_occurred)
15745 /* Commit to the tokens read tentatively, unless that was
15747 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15748 cp_lexer_commit_tokens (parser->lexer);
15750 pop_to_parent_deferring_access_checks ();
15752 /* Otherwise, if errors occurred, roll back our state so that things
15753 are just as they were before we began the tentative parse. */
15756 cp_lexer_rollback_tokens (parser->lexer);
15757 pop_deferring_access_checks ();
15759 /* Add the context to the front of the free list. */
15760 context->next = cp_parser_context_free_list;
15761 cp_parser_context_free_list = context;
15763 return !error_occurred;
15766 /* Returns true if we are parsing tentatively -- but have decided that
15767 we will stick with this tentative parse, even if errors occur. */
15770 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15772 return (cp_parser_parsing_tentatively (parser)
15773 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15776 /* Returns nonzero iff an error has occurred during the most recent
15777 tentative parse. */
15780 cp_parser_error_occurred (cp_parser* parser)
15782 return (cp_parser_parsing_tentatively (parser)
15783 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15786 /* Returns nonzero if GNU extensions are allowed. */
15789 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15791 return parser->allow_gnu_extensions_p;
15797 static GTY (()) cp_parser *the_parser;
15799 /* External interface. */
15801 /* Parse one entire translation unit. */
15804 c_parse_file (void)
15806 bool error_occurred;
15807 static bool already_called = false;
15809 if (already_called)
15811 sorry ("inter-module optimizations not implemented for C++");
15814 already_called = true;
15816 the_parser = cp_parser_new ();
15817 push_deferring_access_checks (flag_access_control
15818 ? dk_no_deferred : dk_no_check);
15819 error_occurred = cp_parser_translation_unit (the_parser);
15823 /* This variable must be provided by every front end. */
15827 #include "gt-cp-parser.h"