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"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype) type : 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid) keyword : 8;
78 /* The value associated with this token, if any. */
80 /* The location at which this token was found. */
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block GTY (())
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
119 cp_token_block *last;
124 static cp_token_cache *cp_token_cache_new
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return GGC_CNEW (cp_token_cache);
137 /* Add *TOKEN to *CACHE. */
140 cp_token_cache_push_token (cp_token_cache *cache,
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = GGC_CNEW (cp_token_block);
149 b->prev = cache->last;
152 cache->last->next = b;
156 cache->first = cache->last = b;
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
166 typedef struct cp_lexer GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token * GTY ((skip)) buffer_end;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token * GTY ((skip)) first_token;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token * GTY ((skip)) next_token;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token * GTY ((skip)) last_token;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
200 /* The STRING_CST tokens encountered while processing the current
202 varray_type string_tokens;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
208 /* True if we should output debugging information. */
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
217 static cp_lexer *cp_lexer_new_main
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
223 static cp_token *cp_lexer_next_token
224 (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
231 static void cp_lexer_maybe_grow_buffer
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
237 static cp_token *cp_lexer_peek_nth_token
238 (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
247 static void cp_lexer_purge_token
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
253 static void cp_lexer_commit_tokens
255 static void cp_lexer_rollback_tokens
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260 (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
263 static void cp_lexer_start_debugging
264 (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266 (cp_lexer *) ATTRIBUTE_UNUSED;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
302 cp_lexer_new_main (void)
305 cp_token first_token;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer = GGC_CNEW (cp_lexer);
316 /* Create the circular buffer. */
317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer->main_lexer_p = true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
352 /* Allocate the memory. */
353 lexer = GGC_CNEW (cp_lexer);
355 /* Create a new buffer, appropriately sized. */
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
359 lexer->buffer = GGC_NEWVEC (cp_token, num_tokens);
360 lexer->buffer_end = lexer->buffer + num_tokens;
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
392 /* Returns nonzero if debugging information should be output. */
395 cp_lexer_debugging_p (cp_lexer *lexer)
397 return lexer->debugging_p;
400 /* Set the current source position from the information stored in
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
412 input_location = token->location;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
438 /* nonzero if we are presently saving tokens. */
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
465 return finish - start;
467 return ((lexer->buffer_end - lexer->buffer)
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
475 cp_lexer_read_token (cp_lexer* lexer)
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
505 if (c_lex_string_translate)
507 tree value = token->value;
510 /* We might as well go ahead and release the chained
511 translated string such that we can reuse its memory. */
512 if (TREE_CHAIN (value))
513 value = TREE_CHAIN (token->value);
515 /* Get the current type. It will be an ARRAY_TYPE. */
516 type = TREE_TYPE (value);
517 /* Use build_cplus_array_type to rebuild the array, thereby
518 getting the right type. */
519 type = build_cplus_array_type (TREE_TYPE (type),
521 /* Reset the type of the token. */
522 TREE_TYPE (value) = type;
529 /* If the circular buffer is full, make it bigger. */
532 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
534 /* If the buffer is full, enlarge it. */
535 if (lexer->last_token == lexer->first_token)
537 cp_token *new_buffer;
538 cp_token *old_buffer;
539 cp_token *new_first_token;
540 ptrdiff_t buffer_length;
541 size_t num_tokens_to_copy;
543 /* Remember the current buffer pointer. It will become invalid,
544 but we will need to do pointer arithmetic involving this
546 old_buffer = lexer->buffer;
547 /* Compute the current buffer size. */
548 buffer_length = lexer->buffer_end - lexer->buffer;
549 /* Allocate a buffer twice as big. */
550 new_buffer = ggc_realloc (lexer->buffer,
551 2 * buffer_length * sizeof (cp_token));
553 /* Because the buffer is circular, logically consecutive tokens
554 are not necessarily placed consecutively in memory.
555 Therefore, we must keep move the tokens that were before
556 FIRST_TOKEN to the second half of the newly allocated
558 num_tokens_to_copy = (lexer->first_token - old_buffer);
559 memcpy (new_buffer + buffer_length,
561 num_tokens_to_copy * sizeof (cp_token));
562 /* Clear the rest of the buffer. We never look at this storage,
563 but the garbage collector may. */
564 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
565 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
567 /* Now recompute all of the buffer pointers. */
569 = new_buffer + (lexer->first_token - old_buffer);
570 if (lexer->next_token != NULL)
572 ptrdiff_t next_token_delta;
574 if (lexer->next_token > lexer->first_token)
575 next_token_delta = lexer->next_token - lexer->first_token;
578 buffer_length - (lexer->first_token - lexer->next_token);
579 lexer->next_token = new_first_token + next_token_delta;
581 lexer->last_token = new_first_token + buffer_length;
582 lexer->buffer = new_buffer;
583 lexer->buffer_end = new_buffer + buffer_length * 2;
584 lexer->first_token = new_first_token;
588 /* Store the next token from the preprocessor in *TOKEN. */
591 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
596 /* If this not the main lexer, return a terminating CPP_EOF token. */
597 if (lexer != NULL && !lexer->main_lexer_p)
599 token->type = CPP_EOF;
600 token->location = UNKNOWN_LOCATION;
601 token->value = NULL_TREE;
602 token->keyword = RID_MAX;
608 /* Keep going until we get a token we like. */
611 /* Get a new token from the preprocessor. */
612 token->type = c_lex_with_flags (&token->value, &token->flags);
613 /* Issue messages about tokens we cannot process. */
619 error ("invalid token");
623 /* This is a good token, so we exit the loop. */
628 /* Now we've got our token. */
629 token->location = input_location;
631 /* Check to see if this token is a keyword. */
632 if (token->type == CPP_NAME
633 && C_IS_RESERVED_WORD (token->value))
635 /* Mark this token as a keyword. */
636 token->type = CPP_KEYWORD;
637 /* Record which keyword. */
638 token->keyword = C_RID_CODE (token->value);
639 /* Update the value. Some keywords are mapped to particular
640 entities, rather than simply having the value of the
641 corresponding IDENTIFIER_NODE. For example, `__const' is
642 mapped to `const'. */
643 token->value = ridpointers[token->keyword];
646 token->keyword = RID_MAX;
649 /* Return a pointer to the next token in the token stream, but do not
653 cp_lexer_peek_token (cp_lexer* lexer)
657 /* If there are no tokens, read one now. */
658 if (!lexer->next_token)
659 cp_lexer_read_token (lexer);
661 /* Provide debugging output. */
662 if (cp_lexer_debugging_p (lexer))
664 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
665 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
666 fprintf (cp_lexer_debug_stream, "\n");
669 token = lexer->next_token;
670 cp_lexer_set_source_position_from_token (lexer, token);
674 /* Return true if the next token has the indicated TYPE. */
677 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
681 /* Peek at the next token. */
682 token = cp_lexer_peek_token (lexer);
683 /* Check to see if it has the indicated TYPE. */
684 return token->type == type;
687 /* Return true if the next token does not have the indicated TYPE. */
690 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
692 return !cp_lexer_next_token_is (lexer, type);
695 /* Return true if the next token is the indicated KEYWORD. */
698 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
702 /* Peek at the next token. */
703 token = cp_lexer_peek_token (lexer);
704 /* Check to see if it is the indicated keyword. */
705 return token->keyword == keyword;
708 /* Return a pointer to the Nth token in the token stream. If N is 1,
709 then this is precisely equivalent to cp_lexer_peek_token. */
712 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
716 /* N is 1-based, not zero-based. */
719 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
720 token = lexer->next_token;
721 /* If there are no tokens in the buffer, get one now. */
724 cp_lexer_read_token (lexer);
725 token = lexer->next_token;
728 /* Now, read tokens until we have enough. */
731 /* Advance to the next token. */
732 token = cp_lexer_next_token (lexer, token);
733 /* If that's all the tokens we have, read a new one. */
734 if (token == lexer->last_token)
735 token = cp_lexer_read_token (lexer);
741 /* Consume the next token. The pointer returned is valid only until
742 another token is read. Callers should preserve copy the token
743 explicitly if they will need its value for a longer period of
747 cp_lexer_consume_token (cp_lexer* lexer)
751 /* If there are no tokens, read one now. */
752 if (!lexer->next_token)
753 cp_lexer_read_token (lexer);
755 /* Remember the token we'll be returning. */
756 token = lexer->next_token;
758 /* Increment NEXT_TOKEN. */
759 lexer->next_token = cp_lexer_next_token (lexer,
761 /* Check to see if we're all out of tokens. */
762 if (lexer->next_token == lexer->last_token)
763 lexer->next_token = NULL;
765 /* If we're not saving tokens, then move FIRST_TOKEN too. */
766 if (!cp_lexer_saving_tokens (lexer))
768 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
769 if (!lexer->next_token)
770 lexer->first_token = NULL;
772 lexer->first_token = lexer->next_token;
775 /* Provide debugging output. */
776 if (cp_lexer_debugging_p (lexer))
778 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
779 cp_lexer_print_token (cp_lexer_debug_stream, token);
780 fprintf (cp_lexer_debug_stream, "\n");
786 /* Permanently remove the next token from the token stream. There
787 must be a valid next token already; this token never reads
788 additional tokens from the preprocessor. */
791 cp_lexer_purge_token (cp_lexer *lexer)
794 cp_token *next_token;
796 token = lexer->next_token;
799 next_token = cp_lexer_next_token (lexer, token);
800 if (next_token == lexer->last_token)
802 *token = *next_token;
806 lexer->last_token = token;
807 /* The token purged may have been the only token remaining; if so,
809 if (lexer->next_token == token)
810 lexer->next_token = NULL;
813 /* Permanently remove all tokens after TOKEN, up to, but not
814 including, the token that will be returned next by
815 cp_lexer_peek_token. */
818 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
824 if (lexer->next_token)
826 /* Copy the tokens that have not yet been read to the location
827 immediately following TOKEN. */
828 t1 = cp_lexer_next_token (lexer, token);
829 t2 = peek = cp_lexer_peek_token (lexer);
830 /* Move tokens into the vacant area between TOKEN and PEEK. */
831 while (t2 != lexer->last_token)
834 t1 = cp_lexer_next_token (lexer, t1);
835 t2 = cp_lexer_next_token (lexer, t2);
837 /* Now, the next available token is right after TOKEN. */
838 lexer->next_token = cp_lexer_next_token (lexer, token);
839 /* And the last token is wherever we ended up. */
840 lexer->last_token = t1;
844 /* There are no tokens in the buffer, so there is nothing to
845 copy. The last token in the buffer is TOKEN itself. */
846 lexer->last_token = cp_lexer_next_token (lexer, token);
850 /* Begin saving tokens. All tokens consumed after this point will be
854 cp_lexer_save_tokens (cp_lexer* lexer)
856 /* Provide debugging output. */
857 if (cp_lexer_debugging_p (lexer))
858 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
860 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
861 restore the tokens if required. */
862 if (!lexer->next_token)
863 cp_lexer_read_token (lexer);
865 VARRAY_PUSH_INT (lexer->saved_tokens,
866 cp_lexer_token_difference (lexer,
871 /* Commit to the portion of the token stream most recently saved. */
874 cp_lexer_commit_tokens (cp_lexer* lexer)
876 /* Provide debugging output. */
877 if (cp_lexer_debugging_p (lexer))
878 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
880 VARRAY_POP (lexer->saved_tokens);
883 /* Return all tokens saved since the last call to cp_lexer_save_tokens
884 to the token stream. Stop saving tokens. */
887 cp_lexer_rollback_tokens (cp_lexer* lexer)
891 /* Provide debugging output. */
892 if (cp_lexer_debugging_p (lexer))
893 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
895 /* Find the token that was the NEXT_TOKEN when we started saving
897 delta = VARRAY_TOP_INT(lexer->saved_tokens);
898 /* Make it the next token again now. */
899 lexer->next_token = cp_lexer_advance_token (lexer,
902 /* It might be the case that there were no tokens when we started
903 saving tokens, but that there are some tokens now. */
904 if (!lexer->next_token && lexer->first_token)
905 lexer->next_token = lexer->first_token;
907 /* Stop saving tokens. */
908 VARRAY_POP (lexer->saved_tokens);
911 /* Print a representation of the TOKEN on the STREAM. */
914 cp_lexer_print_token (FILE * stream, cp_token* token)
916 const char *token_type = NULL;
918 /* Figure out what kind of token this is. */
926 token_type = "COMMA";
930 token_type = "OPEN_PAREN";
933 case CPP_CLOSE_PAREN:
934 token_type = "CLOSE_PAREN";
938 token_type = "OPEN_BRACE";
941 case CPP_CLOSE_BRACE:
942 token_type = "CLOSE_BRACE";
946 token_type = "SEMICOLON";
958 token_type = "keyword";
961 /* This is not a token that we know how to handle yet. */
966 /* If we have a name for the token, print it out. Otherwise, we
967 simply give the numeric code. */
969 fprintf (stream, "%s", token_type);
971 fprintf (stream, "%d", token->type);
972 /* And, for an identifier, print the identifier name. */
973 if (token->type == CPP_NAME
974 /* Some keywords have a value that is not an IDENTIFIER_NODE.
975 For example, `struct' is mapped to an INTEGER_CST. */
976 || (token->type == CPP_KEYWORD
977 && TREE_CODE (token->value) == IDENTIFIER_NODE))
978 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
981 /* Start emitting debugging information. */
984 cp_lexer_start_debugging (cp_lexer* lexer)
986 ++lexer->debugging_p;
989 /* Stop emitting debugging information. */
992 cp_lexer_stop_debugging (cp_lexer* lexer)
994 --lexer->debugging_p;
998 /* Decl-specifiers. */
1000 static void clear_decl_specs
1001 (cp_decl_specifier_seq *);
1003 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1006 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1008 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1013 /* Nothing other than the parser should be creating declarators;
1014 declarators are a semi-syntactic representation of C++ entities.
1015 Other parts of the front end that need to create entities (like
1016 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1018 static cp_declarator *make_id_declarator
1020 static cp_declarator *make_call_declarator
1021 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
1022 static cp_declarator *make_array_declarator
1023 (cp_declarator *, tree);
1024 static cp_declarator *make_pointer_declarator
1025 (cp_cv_quals, cp_declarator *);
1026 static cp_declarator *make_reference_declarator
1027 (cp_cv_quals, cp_declarator *);
1028 static cp_parameter_declarator *make_parameter_declarator
1029 (cp_decl_specifier_seq *, cp_declarator *, tree);
1030 static cp_declarator *make_ptrmem_declarator
1031 (cp_cv_quals, tree, cp_declarator *);
1033 cp_declarator *cp_error_declarator;
1035 /* The obstack on which declarators and related data structures are
1037 static struct obstack declarator_obstack;
1039 /* Alloc BYTES from the declarator memory pool. */
1041 static inline void *
1042 alloc_declarator (size_t bytes)
1044 return obstack_alloc (&declarator_obstack, bytes);
1047 /* Allocate a declarator of the indicated KIND. Clear fields that are
1048 common to all declarators. */
1050 static cp_declarator *
1051 make_declarator (cp_declarator_kind kind)
1053 cp_declarator *declarator;
1055 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1056 declarator->kind = kind;
1057 declarator->attributes = NULL_TREE;
1058 declarator->declarator = NULL;
1063 /* Make a declarator for a generalized identifier. */
1066 make_id_declarator (tree id)
1068 cp_declarator *declarator;
1070 declarator = make_declarator (cdk_id);
1071 declarator->u.id.name = id;
1072 declarator->u.id.sfk = sfk_none;
1077 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1078 of modifiers such as const or volatile to apply to the pointer
1079 type, represented as identifiers. */
1082 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1084 cp_declarator *declarator;
1086 declarator = make_declarator (cdk_pointer);
1087 declarator->declarator = target;
1088 declarator->u.pointer.qualifiers = cv_qualifiers;
1089 declarator->u.pointer.class_type = NULL_TREE;
1094 /* Like make_pointer_declarator -- but for references. */
1097 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1099 cp_declarator *declarator;
1101 declarator = make_declarator (cdk_reference);
1102 declarator->declarator = target;
1103 declarator->u.pointer.qualifiers = cv_qualifiers;
1104 declarator->u.pointer.class_type = NULL_TREE;
1109 /* Like make_pointer_declarator -- but for a pointer to a non-static
1110 member of CLASS_TYPE. */
1113 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1114 cp_declarator *pointee)
1116 cp_declarator *declarator;
1118 declarator = make_declarator (cdk_ptrmem);
1119 declarator->declarator = pointee;
1120 declarator->u.pointer.qualifiers = cv_qualifiers;
1121 declarator->u.pointer.class_type = class_type;
1126 /* Make a declarator for the function given by TARGET, with the
1127 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1128 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1129 indicates what exceptions can be thrown. */
1132 make_call_declarator (cp_declarator *target,
1133 cp_parameter_declarator *parms,
1134 cp_cv_quals cv_qualifiers,
1135 tree exception_specification)
1137 cp_declarator *declarator;
1139 declarator = make_declarator (cdk_function);
1140 declarator->declarator = target;
1141 declarator->u.function.parameters = parms;
1142 declarator->u.function.qualifiers = cv_qualifiers;
1143 declarator->u.function.exception_specification = exception_specification;
1148 /* Make a declarator for an array of BOUNDS elements, each of which is
1149 defined by ELEMENT. */
1152 make_array_declarator (cp_declarator *element, tree bounds)
1154 cp_declarator *declarator;
1156 declarator = make_declarator (cdk_array);
1157 declarator->declarator = element;
1158 declarator->u.array.bounds = bounds;
1163 cp_parameter_declarator *no_parameters;
1165 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1166 DECLARATOR and DEFAULT_ARGUMENT. */
1168 cp_parameter_declarator *
1169 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1170 cp_declarator *declarator,
1171 tree default_argument)
1173 cp_parameter_declarator *parameter;
1175 parameter = ((cp_parameter_declarator *)
1176 alloc_declarator (sizeof (cp_parameter_declarator)));
1177 parameter->next = NULL;
1178 if (decl_specifiers)
1179 parameter->decl_specifiers = *decl_specifiers;
1181 clear_decl_specs (¶meter->decl_specifiers);
1182 parameter->declarator = declarator;
1183 parameter->default_argument = default_argument;
1184 parameter->ellipsis_p = false;
1194 A cp_parser parses the token stream as specified by the C++
1195 grammar. Its job is purely parsing, not semantic analysis. For
1196 example, the parser breaks the token stream into declarators,
1197 expressions, statements, and other similar syntactic constructs.
1198 It does not check that the types of the expressions on either side
1199 of an assignment-statement are compatible, or that a function is
1200 not declared with a parameter of type `void'.
1202 The parser invokes routines elsewhere in the compiler to perform
1203 semantic analysis and to build up the abstract syntax tree for the
1206 The parser (and the template instantiation code, which is, in a
1207 way, a close relative of parsing) are the only parts of the
1208 compiler that should be calling push_scope and pop_scope, or
1209 related functions. The parser (and template instantiation code)
1210 keeps track of what scope is presently active; everything else
1211 should simply honor that. (The code that generates static
1212 initializers may also need to set the scope, in order to check
1213 access control correctly when emitting the initializers.)
1218 The parser is of the standard recursive-descent variety. Upcoming
1219 tokens in the token stream are examined in order to determine which
1220 production to use when parsing a non-terminal. Some C++ constructs
1221 require arbitrary look ahead to disambiguate. For example, it is
1222 impossible, in the general case, to tell whether a statement is an
1223 expression or declaration without scanning the entire statement.
1224 Therefore, the parser is capable of "parsing tentatively." When the
1225 parser is not sure what construct comes next, it enters this mode.
1226 Then, while we attempt to parse the construct, the parser queues up
1227 error messages, rather than issuing them immediately, and saves the
1228 tokens it consumes. If the construct is parsed successfully, the
1229 parser "commits", i.e., it issues any queued error messages and
1230 the tokens that were being preserved are permanently discarded.
1231 If, however, the construct is not parsed successfully, the parser
1232 rolls back its state completely so that it can resume parsing using
1233 a different alternative.
1238 The performance of the parser could probably be improved
1239 substantially. Some possible improvements include:
1241 - The expression parser recurses through the various levels of
1242 precedence as specified in the grammar, rather than using an
1243 operator-precedence technique. Therefore, parsing a simple
1244 identifier requires multiple recursive calls.
1246 - We could often eliminate the need to parse tentatively by
1247 looking ahead a little bit. In some places, this approach
1248 might not entirely eliminate the need to parse tentatively, but
1249 it might still speed up the average case. */
1251 /* Flags that are passed to some parsing functions. These values can
1252 be bitwise-ored together. */
1254 typedef enum cp_parser_flags
1257 CP_PARSER_FLAGS_NONE = 0x0,
1258 /* The construct is optional. If it is not present, then no error
1259 should be issued. */
1260 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1261 /* When parsing a type-specifier, do not allow user-defined types. */
1262 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1265 /* The different kinds of declarators we want to parse. */
1267 typedef enum cp_parser_declarator_kind
1269 /* We want an abstract declarator. */
1270 CP_PARSER_DECLARATOR_ABSTRACT,
1271 /* We want a named declarator. */
1272 CP_PARSER_DECLARATOR_NAMED,
1273 /* We don't mind, but the name must be an unqualified-id. */
1274 CP_PARSER_DECLARATOR_EITHER
1275 } cp_parser_declarator_kind;
1277 /* A mapping from a token type to a corresponding tree node type. */
1279 typedef struct cp_parser_token_tree_map_node
1281 /* The token type. */
1282 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1283 /* The corresponding tree code. */
1284 ENUM_BITFIELD (tree_code) tree_type : 8;
1285 } cp_parser_token_tree_map_node;
1287 /* A complete map consists of several ordinary entries, followed by a
1288 terminator. The terminating entry has a token_type of CPP_EOF. */
1290 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1292 /* The status of a tentative parse. */
1294 typedef enum cp_parser_status_kind
1296 /* No errors have occurred. */
1297 CP_PARSER_STATUS_KIND_NO_ERROR,
1298 /* An error has occurred. */
1299 CP_PARSER_STATUS_KIND_ERROR,
1300 /* We are committed to this tentative parse, whether or not an error
1302 CP_PARSER_STATUS_KIND_COMMITTED
1303 } cp_parser_status_kind;
1305 /* Context that is saved and restored when parsing tentatively. */
1307 typedef struct cp_parser_context GTY (())
1309 /* If this is a tentative parsing context, the status of the
1311 enum cp_parser_status_kind status;
1312 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1313 that are looked up in this context must be looked up both in the
1314 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1315 the context of the containing expression. */
1317 /* The next parsing context in the stack. */
1318 struct cp_parser_context *next;
1319 } cp_parser_context;
1323 /* Constructors and destructors. */
1325 static cp_parser_context *cp_parser_context_new
1326 (cp_parser_context *);
1328 /* Class variables. */
1330 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1332 /* Constructors and destructors. */
1334 /* Construct a new context. The context below this one on the stack
1335 is given by NEXT. */
1337 static cp_parser_context *
1338 cp_parser_context_new (cp_parser_context* next)
1340 cp_parser_context *context;
1342 /* Allocate the storage. */
1343 if (cp_parser_context_free_list != NULL)
1345 /* Pull the first entry from the free list. */
1346 context = cp_parser_context_free_list;
1347 cp_parser_context_free_list = context->next;
1348 memset (context, 0, sizeof (*context));
1351 context = GGC_CNEW (cp_parser_context);
1352 /* No errors have occurred yet in this context. */
1353 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1354 /* If this is not the bottomost context, copy information that we
1355 need from the previous context. */
1358 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1359 expression, then we are parsing one in this context, too. */
1360 context->object_type = next->object_type;
1361 /* Thread the stack. */
1362 context->next = next;
1368 /* The cp_parser structure represents the C++ parser. */
1370 typedef struct cp_parser GTY(())
1372 /* The lexer from which we are obtaining tokens. */
1375 /* The scope in which names should be looked up. If NULL_TREE, then
1376 we look up names in the scope that is currently open in the
1377 source program. If non-NULL, this is either a TYPE or
1378 NAMESPACE_DECL for the scope in which we should look.
1380 This value is not cleared automatically after a name is looked
1381 up, so we must be careful to clear it before starting a new look
1382 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1383 will look up `Z' in the scope of `X', rather than the current
1384 scope.) Unfortunately, it is difficult to tell when name lookup
1385 is complete, because we sometimes peek at a token, look it up,
1386 and then decide not to consume it. */
1389 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1390 last lookup took place. OBJECT_SCOPE is used if an expression
1391 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1392 respectively. QUALIFYING_SCOPE is used for an expression of the
1393 form "X::Y"; it refers to X. */
1395 tree qualifying_scope;
1397 /* A stack of parsing contexts. All but the bottom entry on the
1398 stack will be tentative contexts.
1400 We parse tentatively in order to determine which construct is in
1401 use in some situations. For example, in order to determine
1402 whether a statement is an expression-statement or a
1403 declaration-statement we parse it tentatively as a
1404 declaration-statement. If that fails, we then reparse the same
1405 token stream as an expression-statement. */
1406 cp_parser_context *context;
1408 /* True if we are parsing GNU C++. If this flag is not set, then
1409 GNU extensions are not recognized. */
1410 bool allow_gnu_extensions_p;
1412 /* TRUE if the `>' token should be interpreted as the greater-than
1413 operator. FALSE if it is the end of a template-id or
1414 template-parameter-list. */
1415 bool greater_than_is_operator_p;
1417 /* TRUE if default arguments are allowed within a parameter list
1418 that starts at this point. FALSE if only a gnu extension makes
1419 them permissible. */
1420 bool default_arg_ok_p;
1422 /* TRUE if we are parsing an integral constant-expression. See
1423 [expr.const] for a precise definition. */
1424 bool integral_constant_expression_p;
1426 /* TRUE if we are parsing an integral constant-expression -- but a
1427 non-constant expression should be permitted as well. This flag
1428 is used when parsing an array bound so that GNU variable-length
1429 arrays are tolerated. */
1430 bool allow_non_integral_constant_expression_p;
1432 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1433 been seen that makes the expression non-constant. */
1434 bool non_integral_constant_expression_p;
1436 /* TRUE if local variable names and `this' are forbidden in the
1438 bool local_variables_forbidden_p;
1440 /* TRUE if the declaration we are parsing is part of a
1441 linkage-specification of the form `extern string-literal
1443 bool in_unbraced_linkage_specification_p;
1445 /* TRUE if we are presently parsing a declarator, after the
1446 direct-declarator. */
1447 bool in_declarator_p;
1449 /* TRUE if we are presently parsing a template-argument-list. */
1450 bool in_template_argument_list_p;
1452 /* TRUE if we are presently parsing the body of an
1453 iteration-statement. */
1454 bool in_iteration_statement_p;
1456 /* TRUE if we are presently parsing the body of a switch
1458 bool in_switch_statement_p;
1460 /* TRUE if we are parsing a type-id in an expression context. In
1461 such a situation, both "type (expr)" and "type (type)" are valid
1463 bool in_type_id_in_expr_p;
1465 /* If non-NULL, then we are parsing a construct where new type
1466 definitions are not permitted. The string stored here will be
1467 issued as an error message if a type is defined. */
1468 const char *type_definition_forbidden_message;
1470 /* A list of lists. The outer list is a stack, used for member
1471 functions of local classes. At each level there are two sub-list,
1472 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1473 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1474 TREE_VALUE's. The functions are chained in reverse declaration
1477 The TREE_PURPOSE sublist contains those functions with default
1478 arguments that need post processing, and the TREE_VALUE sublist
1479 contains those functions with definitions that need post
1482 These lists can only be processed once the outermost class being
1483 defined is complete. */
1484 tree unparsed_functions_queues;
1486 /* The number of classes whose definitions are currently in
1488 unsigned num_classes_being_defined;
1490 /* The number of template parameter lists that apply directly to the
1491 current declaration. */
1492 unsigned num_template_parameter_lists;
1495 /* The type of a function that parses some kind of expression. */
1496 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1500 /* Constructors and destructors. */
1502 static cp_parser *cp_parser_new
1505 /* Routines to parse various constructs.
1507 Those that return `tree' will return the error_mark_node (rather
1508 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1509 Sometimes, they will return an ordinary node if error-recovery was
1510 attempted, even though a parse error occurred. So, to check
1511 whether or not a parse error occurred, you should always use
1512 cp_parser_error_occurred. If the construct is optional (indicated
1513 either by an `_opt' in the name of the function that does the
1514 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1515 the construct is not present. */
1517 /* Lexical conventions [gram.lex] */
1519 static tree cp_parser_identifier
1522 /* Basic concepts [gram.basic] */
1524 static bool cp_parser_translation_unit
1527 /* Expressions [gram.expr] */
1529 static tree cp_parser_primary_expression
1530 (cp_parser *, cp_id_kind *, tree *);
1531 static tree cp_parser_id_expression
1532 (cp_parser *, bool, bool, bool *, bool);
1533 static tree cp_parser_unqualified_id
1534 (cp_parser *, bool, bool, bool);
1535 static tree cp_parser_nested_name_specifier_opt
1536 (cp_parser *, bool, bool, bool, bool);
1537 static tree cp_parser_nested_name_specifier
1538 (cp_parser *, bool, bool, bool, bool);
1539 static tree cp_parser_class_or_namespace_name
1540 (cp_parser *, bool, bool, bool, bool, bool);
1541 static tree cp_parser_postfix_expression
1542 (cp_parser *, bool);
1543 static tree cp_parser_postfix_open_square_expression
1544 (cp_parser *, tree, bool);
1545 static tree cp_parser_postfix_dot_deref_expression
1546 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1547 static tree cp_parser_parenthesized_expression_list
1548 (cp_parser *, bool, bool *);
1549 static void cp_parser_pseudo_destructor_name
1550 (cp_parser *, tree *, tree *);
1551 static tree cp_parser_unary_expression
1552 (cp_parser *, bool);
1553 static enum tree_code cp_parser_unary_operator
1555 static tree cp_parser_new_expression
1557 static tree cp_parser_new_placement
1559 static tree cp_parser_new_type_id
1560 (cp_parser *, tree *);
1561 static cp_declarator *cp_parser_new_declarator_opt
1563 static cp_declarator *cp_parser_direct_new_declarator
1565 static tree cp_parser_new_initializer
1567 static tree cp_parser_delete_expression
1569 static tree cp_parser_cast_expression
1570 (cp_parser *, bool);
1571 static tree cp_parser_pm_expression
1573 static tree cp_parser_multiplicative_expression
1575 static tree cp_parser_additive_expression
1577 static tree cp_parser_shift_expression
1579 static tree cp_parser_relational_expression
1581 static tree cp_parser_equality_expression
1583 static tree cp_parser_and_expression
1585 static tree cp_parser_exclusive_or_expression
1587 static tree cp_parser_inclusive_or_expression
1589 static tree cp_parser_logical_and_expression
1591 static tree cp_parser_logical_or_expression
1593 static tree cp_parser_question_colon_clause
1594 (cp_parser *, tree);
1595 static tree cp_parser_assignment_expression
1597 static enum tree_code cp_parser_assignment_operator_opt
1599 static tree cp_parser_expression
1601 static tree cp_parser_constant_expression
1602 (cp_parser *, bool, bool *);
1603 static tree cp_parser_builtin_offsetof
1606 /* Statements [gram.stmt.stmt] */
1608 static void cp_parser_statement
1609 (cp_parser *, tree);
1610 static tree cp_parser_labeled_statement
1611 (cp_parser *, tree);
1612 static tree cp_parser_expression_statement
1613 (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615 (cp_parser *, tree, bool);
1616 static void cp_parser_statement_seq_opt
1617 (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1620 static tree cp_parser_condition
1622 static tree cp_parser_iteration_statement
1624 static void cp_parser_for_init_statement
1626 static tree cp_parser_jump_statement
1628 static void cp_parser_declaration_statement
1631 static tree cp_parser_implicitly_scoped_statement
1633 static void cp_parser_already_scoped_statement
1636 /* Declarations [gram.dcl.dcl] */
1638 static void cp_parser_declaration_seq_opt
1640 static void cp_parser_declaration
1642 static void cp_parser_block_declaration
1643 (cp_parser *, bool);
1644 static void cp_parser_simple_declaration
1645 (cp_parser *, bool);
1646 static void cp_parser_decl_specifier_seq
1647 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1648 static tree cp_parser_storage_class_specifier_opt
1650 static tree cp_parser_function_specifier_opt
1651 (cp_parser *, cp_decl_specifier_seq *);
1652 static tree cp_parser_type_specifier
1653 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1655 static tree cp_parser_simple_type_specifier
1656 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1657 static tree cp_parser_type_name
1659 static tree cp_parser_elaborated_type_specifier
1660 (cp_parser *, bool, bool);
1661 static tree cp_parser_enum_specifier
1663 static void cp_parser_enumerator_list
1664 (cp_parser *, tree);
1665 static void cp_parser_enumerator_definition
1666 (cp_parser *, tree);
1667 static tree cp_parser_namespace_name
1669 static void cp_parser_namespace_definition
1671 static void cp_parser_namespace_body
1673 static tree cp_parser_qualified_namespace_specifier
1675 static void cp_parser_namespace_alias_definition
1677 static void cp_parser_using_declaration
1679 static void cp_parser_using_directive
1681 static void cp_parser_asm_definition
1683 static void cp_parser_linkage_specification
1686 /* Declarators [gram.dcl.decl] */
1688 static tree cp_parser_init_declarator
1689 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1690 static cp_declarator *cp_parser_declarator
1691 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1692 static cp_declarator *cp_parser_direct_declarator
1693 (cp_parser *, cp_parser_declarator_kind, int *);
1694 static enum tree_code cp_parser_ptr_operator
1695 (cp_parser *, tree *, cp_cv_quals *);
1696 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1698 static tree cp_parser_declarator_id
1700 static tree cp_parser_type_id
1702 static void cp_parser_type_specifier_seq
1703 (cp_parser *, cp_decl_specifier_seq *);
1704 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1706 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1707 (cp_parser *, bool *);
1708 static cp_parameter_declarator *cp_parser_parameter_declaration
1709 (cp_parser *, bool, bool *);
1710 static void cp_parser_function_body
1712 static tree cp_parser_initializer
1713 (cp_parser *, bool *, bool *);
1714 static tree cp_parser_initializer_clause
1715 (cp_parser *, bool *);
1716 static tree cp_parser_initializer_list
1717 (cp_parser *, bool *);
1719 static bool cp_parser_ctor_initializer_opt_and_function_body
1722 /* Classes [gram.class] */
1724 static tree cp_parser_class_name
1725 (cp_parser *, bool, bool, bool, bool, bool, bool);
1726 static tree cp_parser_class_specifier
1728 static tree cp_parser_class_head
1729 (cp_parser *, bool *, tree *);
1730 static enum tag_types cp_parser_class_key
1732 static void cp_parser_member_specification_opt
1734 static void cp_parser_member_declaration
1736 static tree cp_parser_pure_specifier
1738 static tree cp_parser_constant_initializer
1741 /* Derived classes [gram.class.derived] */
1743 static tree cp_parser_base_clause
1745 static tree cp_parser_base_specifier
1748 /* Special member functions [gram.special] */
1750 static tree cp_parser_conversion_function_id
1752 static tree cp_parser_conversion_type_id
1754 static cp_declarator *cp_parser_conversion_declarator_opt
1756 static bool cp_parser_ctor_initializer_opt
1758 static void cp_parser_mem_initializer_list
1760 static tree cp_parser_mem_initializer
1762 static tree cp_parser_mem_initializer_id
1765 /* Overloading [gram.over] */
1767 static tree cp_parser_operator_function_id
1769 static tree cp_parser_operator
1772 /* Templates [gram.temp] */
1774 static void cp_parser_template_declaration
1775 (cp_parser *, bool);
1776 static tree cp_parser_template_parameter_list
1778 static tree cp_parser_template_parameter
1779 (cp_parser *, bool *);
1780 static tree cp_parser_type_parameter
1782 static tree cp_parser_template_id
1783 (cp_parser *, bool, bool, bool);
1784 static tree cp_parser_template_name
1785 (cp_parser *, bool, bool, bool, bool *);
1786 static tree cp_parser_template_argument_list
1788 static tree cp_parser_template_argument
1790 static void cp_parser_explicit_instantiation
1792 static void cp_parser_explicit_specialization
1795 /* Exception handling [gram.exception] */
1797 static tree cp_parser_try_block
1799 static bool cp_parser_function_try_block
1801 static void cp_parser_handler_seq
1803 static void cp_parser_handler
1805 static tree cp_parser_exception_declaration
1807 static tree cp_parser_throw_expression
1809 static tree cp_parser_exception_specification_opt
1811 static tree cp_parser_type_id_list
1814 /* GNU Extensions */
1816 static tree cp_parser_asm_specification_opt
1818 static tree cp_parser_asm_operand_list
1820 static tree cp_parser_asm_clobber_list
1822 static tree cp_parser_attributes_opt
1824 static tree cp_parser_attribute_list
1826 static bool cp_parser_extension_opt
1827 (cp_parser *, int *);
1828 static void cp_parser_label_declaration
1831 /* Utility Routines */
1833 static tree cp_parser_lookup_name
1834 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1835 static tree cp_parser_lookup_name_simple
1836 (cp_parser *, tree);
1837 static tree cp_parser_maybe_treat_template_as_class
1839 static bool cp_parser_check_declarator_template_parameters
1840 (cp_parser *, cp_declarator *);
1841 static bool cp_parser_check_template_parameters
1842 (cp_parser *, unsigned);
1843 static tree cp_parser_simple_cast_expression
1845 static tree cp_parser_binary_expression
1846 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1847 static tree cp_parser_global_scope_opt
1848 (cp_parser *, bool);
1849 static bool cp_parser_constructor_declarator_p
1850 (cp_parser *, bool);
1851 static tree cp_parser_function_definition_from_specifiers_and_declarator
1852 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1853 static tree cp_parser_function_definition_after_declarator
1854 (cp_parser *, bool);
1855 static void cp_parser_template_declaration_after_export
1856 (cp_parser *, bool);
1857 static tree cp_parser_single_declaration
1858 (cp_parser *, bool, bool *);
1859 static tree cp_parser_functional_cast
1860 (cp_parser *, tree);
1861 static tree cp_parser_save_member_function_body
1862 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1863 static tree cp_parser_enclosed_template_argument_list
1865 static void cp_parser_save_default_args
1866 (cp_parser *, tree);
1867 static void cp_parser_late_parsing_for_member
1868 (cp_parser *, tree);
1869 static void cp_parser_late_parsing_default_args
1870 (cp_parser *, tree);
1871 static tree cp_parser_sizeof_operand
1872 (cp_parser *, enum rid);
1873 static bool cp_parser_declares_only_class_p
1875 static void cp_parser_set_storage_class
1876 (cp_decl_specifier_seq *, cp_storage_class);
1877 static void cp_parser_set_decl_spec_type
1878 (cp_decl_specifier_seq *, tree, bool);
1879 static bool cp_parser_friend_p
1880 (const cp_decl_specifier_seq *);
1881 static cp_token *cp_parser_require
1882 (cp_parser *, enum cpp_ttype, const char *);
1883 static cp_token *cp_parser_require_keyword
1884 (cp_parser *, enum rid, const char *);
1885 static bool cp_parser_token_starts_function_definition_p
1887 static bool cp_parser_next_token_starts_class_definition_p
1889 static bool cp_parser_next_token_ends_template_argument_p
1891 static bool cp_parser_nth_token_starts_template_argument_list_p
1892 (cp_parser *, size_t);
1893 static enum tag_types cp_parser_token_is_class_key
1895 static void cp_parser_check_class_key
1896 (enum tag_types, tree type);
1897 static void cp_parser_check_access_in_redeclaration
1899 static bool cp_parser_optional_template_keyword
1901 static void cp_parser_pre_parsed_nested_name_specifier
1903 static void cp_parser_cache_group
1904 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1905 static void cp_parser_parse_tentatively
1907 static void cp_parser_commit_to_tentative_parse
1909 static void cp_parser_abort_tentative_parse
1911 static bool cp_parser_parse_definitely
1913 static inline bool cp_parser_parsing_tentatively
1915 static bool cp_parser_committed_to_tentative_parse
1917 static void cp_parser_error
1918 (cp_parser *, const char *);
1919 static void cp_parser_name_lookup_error
1920 (cp_parser *, tree, tree, const char *);
1921 static bool cp_parser_simulate_error
1923 static void cp_parser_check_type_definition
1925 static void cp_parser_check_for_definition_in_return_type
1926 (cp_declarator *, int);
1927 static void cp_parser_check_for_invalid_template_id
1928 (cp_parser *, tree);
1929 static bool cp_parser_non_integral_constant_expression
1930 (cp_parser *, const char *);
1931 static void cp_parser_diagnose_invalid_type_name
1932 (cp_parser *, tree, tree);
1933 static bool cp_parser_parse_and_diagnose_invalid_type_name
1935 static int cp_parser_skip_to_closing_parenthesis
1936 (cp_parser *, bool, bool, bool);
1937 static void cp_parser_skip_to_end_of_statement
1939 static void cp_parser_consume_semicolon_at_end_of_statement
1941 static void cp_parser_skip_to_end_of_block_or_statement
1943 static void cp_parser_skip_to_closing_brace
1945 static void cp_parser_skip_until_found
1946 (cp_parser *, enum cpp_ttype, const char *);
1947 static bool cp_parser_error_occurred
1949 static bool cp_parser_allow_gnu_extensions_p
1951 static bool cp_parser_is_string_literal
1953 static bool cp_parser_is_keyword
1954 (cp_token *, enum rid);
1955 static tree cp_parser_make_typename_type
1956 (cp_parser *, tree, tree);
1958 /* Returns nonzero if we are parsing tentatively. */
1961 cp_parser_parsing_tentatively (cp_parser* parser)
1963 return parser->context->next != NULL;
1966 /* Returns nonzero if TOKEN is a string literal. */
1969 cp_parser_is_string_literal (cp_token* token)
1971 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1974 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1977 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1979 return token->keyword == keyword;
1982 /* Issue the indicated error MESSAGE. */
1985 cp_parser_error (cp_parser* parser, const char* message)
1987 /* Output the MESSAGE -- unless we're parsing tentatively. */
1988 if (!cp_parser_simulate_error (parser))
1991 token = cp_lexer_peek_token (parser->lexer);
1992 c_parse_error (message,
1993 /* Because c_parser_error does not understand
1994 CPP_KEYWORD, keywords are treated like
1996 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2001 /* Issue an error about name-lookup failing. NAME is the
2002 IDENTIFIER_NODE DECL is the result of
2003 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2004 the thing that we hoped to find. */
2007 cp_parser_name_lookup_error (cp_parser* parser,
2010 const char* desired)
2012 /* If name lookup completely failed, tell the user that NAME was not
2014 if (decl == error_mark_node)
2016 if (parser->scope && parser->scope != global_namespace)
2017 error ("`%D::%D' has not been declared",
2018 parser->scope, name);
2019 else if (parser->scope == global_namespace)
2020 error ("`::%D' has not been declared", name);
2021 else if (parser->object_scope
2022 && !CLASS_TYPE_P (parser->object_scope))
2023 error ("request for member `%D' in non-class type `%T'",
2024 name, parser->object_scope);
2025 else if (parser->object_scope)
2026 error ("`%T::%D' has not been declared",
2027 parser->object_scope, name);
2029 error ("`%D' has not been declared", name);
2031 else if (parser->scope && parser->scope != global_namespace)
2032 error ("`%D::%D' %s", parser->scope, name, desired);
2033 else if (parser->scope == global_namespace)
2034 error ("`::%D' %s", name, desired);
2036 error ("`%D' %s", name, desired);
2039 /* If we are parsing tentatively, remember that an error has occurred
2040 during this tentative parse. Returns true if the error was
2041 simulated; false if a message should be issued by the caller. */
2044 cp_parser_simulate_error (cp_parser* parser)
2046 if (cp_parser_parsing_tentatively (parser)
2047 && !cp_parser_committed_to_tentative_parse (parser))
2049 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2055 /* This function is called when a type is defined. If type
2056 definitions are forbidden at this point, an error message is
2060 cp_parser_check_type_definition (cp_parser* parser)
2062 /* If types are forbidden here, issue a message. */
2063 if (parser->type_definition_forbidden_message)
2064 /* Use `%s' to print the string in case there are any escape
2065 characters in the message. */
2066 error ("%s", parser->type_definition_forbidden_message);
2069 /* This function is called when a declaration is parsed. If
2070 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2071 indicates that a type was defined in the decl-specifiers for DECL,
2072 then an error is issued. */
2075 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2076 int declares_class_or_enum)
2078 /* [dcl.fct] forbids type definitions in return types.
2079 Unfortunately, it's not easy to know whether or not we are
2080 processing a return type until after the fact. */
2082 && (declarator->kind == cdk_pointer
2083 || declarator->kind == cdk_reference
2084 || declarator->kind == cdk_ptrmem))
2085 declarator = declarator->declarator;
2087 && declarator->kind == cdk_function
2088 && declares_class_or_enum & 2)
2089 error ("new types may not be defined in a return type");
2092 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2093 "<" in any valid C++ program. If the next token is indeed "<",
2094 issue a message warning the user about what appears to be an
2095 invalid attempt to form a template-id. */
2098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2104 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2107 error ("`%T' is not a template", type);
2108 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2109 error ("`%E' is not a template", type);
2111 error ("invalid template-id");
2112 /* Remember the location of the invalid "<". */
2113 if (cp_parser_parsing_tentatively (parser)
2114 && !cp_parser_committed_to_tentative_parse (parser))
2116 token = cp_lexer_peek_token (parser->lexer);
2117 token = cp_lexer_prev_token (parser->lexer, token);
2118 start = cp_lexer_token_difference (parser->lexer,
2119 parser->lexer->first_token,
2124 /* Consume the "<". */
2125 cp_lexer_consume_token (parser->lexer);
2126 /* Parse the template arguments. */
2127 cp_parser_enclosed_template_argument_list (parser);
2128 /* Permanently remove the invalid template arguments so that
2129 this error message is not issued again. */
2132 token = cp_lexer_advance_token (parser->lexer,
2133 parser->lexer->first_token,
2135 cp_lexer_purge_tokens_after (parser->lexer, token);
2140 /* If parsing an integral constant-expression, issue an error message
2141 about the fact that THING appeared and return true. Otherwise,
2142 return false, marking the current expression as non-constant. */
2145 cp_parser_non_integral_constant_expression (cp_parser *parser,
2148 if (parser->integral_constant_expression_p)
2150 if (!parser->allow_non_integral_constant_expression_p)
2152 error ("%s cannot appear in a constant-expression", thing);
2155 parser->non_integral_constant_expression_p = true;
2160 /* Emit a diagnostic for an invalid type name. Consider also if it is
2161 qualified or not and the result of a lookup, to provide a better
2165 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2167 tree decl, old_scope;
2168 /* Try to lookup the identifier. */
2169 old_scope = parser->scope;
2170 parser->scope = scope;
2171 decl = cp_parser_lookup_name_simple (parser, id);
2172 parser->scope = old_scope;
2173 /* If the lookup found a template-name, it means that the user forgot
2174 to specify an argument list. Emit an useful error message. */
2175 if (TREE_CODE (decl) == TEMPLATE_DECL)
2176 error ("invalid use of template-name `%E' without an argument list",
2178 else if (!parser->scope)
2180 /* Issue an error message. */
2181 error ("`%E' does not name a type", id);
2182 /* If we're in a template class, it's possible that the user was
2183 referring to a type from a base class. For example:
2185 template <typename T> struct A { typedef T X; };
2186 template <typename T> struct B : public A<T> { X x; };
2188 The user should have said "typename A<T>::X". */
2189 if (processing_template_decl && current_class_type)
2193 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2197 tree base_type = BINFO_TYPE (b);
2198 if (CLASS_TYPE_P (base_type)
2199 && dependent_type_p (base_type))
2202 /* Go from a particular instantiation of the
2203 template (which will have an empty TYPE_FIELDs),
2204 to the main version. */
2205 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2206 for (field = TYPE_FIELDS (base_type);
2208 field = TREE_CHAIN (field))
2209 if (TREE_CODE (field) == TYPE_DECL
2210 && DECL_NAME (field) == id)
2212 inform ("(perhaps `typename %T::%E' was intended)",
2213 BINFO_TYPE (b), id);
2222 /* Here we diagnose qualified-ids where the scope is actually correct,
2223 but the identifier does not resolve to a valid type name. */
2226 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2227 error ("`%E' in namespace `%E' does not name a type",
2229 else if (TYPE_P (parser->scope))
2230 error ("`%E' in class `%T' does not name a type",
2237 /* Check for a common situation where a type-name should be present,
2238 but is not, and issue a sensible error message. Returns true if an
2239 invalid type-name was detected.
2241 The situation handled by this function are variable declarations of the
2242 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2243 Usually, `ID' should name a type, but if we got here it means that it
2244 does not. We try to emit the best possible error message depending on
2245 how exactly the id-expression looks like.
2249 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2253 cp_parser_parse_tentatively (parser);
2254 id = cp_parser_id_expression (parser,
2255 /*template_keyword_p=*/false,
2256 /*check_dependency_p=*/true,
2257 /*template_p=*/NULL,
2258 /*declarator_p=*/true);
2259 /* After the id-expression, there should be a plain identifier,
2260 otherwise this is not a simple variable declaration. Also, if
2261 the scope is dependent, we cannot do much. */
2262 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2263 || (parser->scope && TYPE_P (parser->scope)
2264 && dependent_type_p (parser->scope)))
2266 cp_parser_abort_tentative_parse (parser);
2269 if (!cp_parser_parse_definitely (parser))
2272 /* If we got here, this cannot be a valid variable declaration, thus
2273 the cp_parser_id_expression must have resolved to a plain identifier
2274 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2275 gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2276 /* Emit a diagnostic for the invalid type. */
2277 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2278 /* Skip to the end of the declaration; there's no point in
2279 trying to process it. */
2280 cp_parser_skip_to_end_of_block_or_statement (parser);
2284 /* Consume tokens up to, and including, the next non-nested closing `)'.
2285 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2286 are doing error recovery. Returns -1 if OR_COMMA is true and we
2287 found an unnested comma. */
2290 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2295 unsigned paren_depth = 0;
2296 unsigned brace_depth = 0;
2297 int saved_c_lex_string_translate = c_lex_string_translate;
2300 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2301 && !cp_parser_committed_to_tentative_parse (parser))
2305 /* If we're looking ahead, keep both translated and untranslated
2307 c_lex_string_translate = -1;
2313 /* If we've run out of tokens, then there is no closing `)'. */
2314 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2320 token = cp_lexer_peek_token (parser->lexer);
2322 /* This matches the processing in skip_to_end_of_statement. */
2323 if (token->type == CPP_SEMICOLON && !brace_depth)
2328 if (token->type == CPP_OPEN_BRACE)
2330 if (token->type == CPP_CLOSE_BRACE)
2338 if (recovering && or_comma && token->type == CPP_COMMA
2339 && !brace_depth && !paren_depth)
2347 /* If it is an `(', we have entered another level of nesting. */
2348 if (token->type == CPP_OPEN_PAREN)
2350 /* If it is a `)', then we might be done. */
2351 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2354 cp_lexer_consume_token (parser->lexer);
2362 /* Consume the token. */
2363 cp_lexer_consume_token (parser->lexer);
2366 c_lex_string_translate = saved_c_lex_string_translate;
2370 /* Consume tokens until we reach the end of the current statement.
2371 Normally, that will be just before consuming a `;'. However, if a
2372 non-nested `}' comes first, then we stop before consuming that. */
2375 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2377 unsigned nesting_depth = 0;
2383 /* Peek at the next token. */
2384 token = cp_lexer_peek_token (parser->lexer);
2385 /* If we've run out of tokens, stop. */
2386 if (token->type == CPP_EOF)
2388 /* If the next token is a `;', we have reached the end of the
2390 if (token->type == CPP_SEMICOLON && !nesting_depth)
2392 /* If the next token is a non-nested `}', then we have reached
2393 the end of the current block. */
2394 if (token->type == CPP_CLOSE_BRACE)
2396 /* If this is a non-nested `}', stop before consuming it.
2397 That way, when confronted with something like:
2401 we stop before consuming the closing `}', even though we
2402 have not yet reached a `;'. */
2403 if (nesting_depth == 0)
2405 /* If it is the closing `}' for a block that we have
2406 scanned, stop -- but only after consuming the token.
2412 we will stop after the body of the erroneously declared
2413 function, but before consuming the following `typedef'
2415 if (--nesting_depth == 0)
2417 cp_lexer_consume_token (parser->lexer);
2421 /* If it the next token is a `{', then we are entering a new
2422 block. Consume the entire block. */
2423 else if (token->type == CPP_OPEN_BRACE)
2425 /* Consume the token. */
2426 cp_lexer_consume_token (parser->lexer);
2430 /* This function is called at the end of a statement or declaration.
2431 If the next token is a semicolon, it is consumed; otherwise, error
2432 recovery is attempted. */
2435 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2437 /* Look for the trailing `;'. */
2438 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2440 /* If there is additional (erroneous) input, skip to the end of
2442 cp_parser_skip_to_end_of_statement (parser);
2443 /* If the next token is now a `;', consume it. */
2444 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2445 cp_lexer_consume_token (parser->lexer);
2449 /* Skip tokens until we have consumed an entire block, or until we
2450 have consumed a non-nested `;'. */
2453 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2455 unsigned nesting_depth = 0;
2461 /* Peek at the next token. */
2462 token = cp_lexer_peek_token (parser->lexer);
2463 /* If we've run out of tokens, stop. */
2464 if (token->type == CPP_EOF)
2466 /* If the next token is a `;', we have reached the end of the
2468 if (token->type == CPP_SEMICOLON && !nesting_depth)
2470 /* Consume the `;'. */
2471 cp_lexer_consume_token (parser->lexer);
2474 /* Consume the token. */
2475 token = cp_lexer_consume_token (parser->lexer);
2476 /* If the next token is a non-nested `}', then we have reached
2477 the end of the current block. */
2478 if (token->type == CPP_CLOSE_BRACE
2479 && (nesting_depth == 0 || --nesting_depth == 0))
2481 /* If it the next token is a `{', then we are entering a new
2482 block. Consume the entire block. */
2483 if (token->type == CPP_OPEN_BRACE)
2488 /* Skip tokens until a non-nested closing curly brace is the next
2492 cp_parser_skip_to_closing_brace (cp_parser *parser)
2494 unsigned nesting_depth = 0;
2500 /* Peek at the next token. */
2501 token = cp_lexer_peek_token (parser->lexer);
2502 /* If we've run out of tokens, stop. */
2503 if (token->type == CPP_EOF)
2505 /* If the next token is a non-nested `}', then we have reached
2506 the end of the current block. */
2507 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2509 /* If it the next token is a `{', then we are entering a new
2510 block. Consume the entire block. */
2511 else if (token->type == CPP_OPEN_BRACE)
2513 /* Consume the token. */
2514 cp_lexer_consume_token (parser->lexer);
2518 /* This is a simple wrapper around make_typename_type. When the id is
2519 an unresolved identifier node, we can provide a superior diagnostic
2520 using cp_parser_diagnose_invalid_type_name. */
2523 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2526 if (TREE_CODE (id) == IDENTIFIER_NODE)
2528 result = make_typename_type (scope, id, /*complain=*/0);
2529 if (result == error_mark_node)
2530 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2533 return make_typename_type (scope, id, tf_error);
2537 /* Create a new C++ parser. */
2540 cp_parser_new (void)
2545 /* cp_lexer_new_main is called before calling ggc_alloc because
2546 cp_lexer_new_main might load a PCH file. */
2547 lexer = cp_lexer_new_main ();
2549 parser = GGC_CNEW (cp_parser);
2550 parser->lexer = lexer;
2551 parser->context = cp_parser_context_new (NULL);
2553 /* For now, we always accept GNU extensions. */
2554 parser->allow_gnu_extensions_p = 1;
2556 /* The `>' token is a greater-than operator, not the end of a
2558 parser->greater_than_is_operator_p = true;
2560 parser->default_arg_ok_p = true;
2562 /* We are not parsing a constant-expression. */
2563 parser->integral_constant_expression_p = false;
2564 parser->allow_non_integral_constant_expression_p = false;
2565 parser->non_integral_constant_expression_p = false;
2567 /* Local variable names are not forbidden. */
2568 parser->local_variables_forbidden_p = false;
2570 /* We are not processing an `extern "C"' declaration. */
2571 parser->in_unbraced_linkage_specification_p = false;
2573 /* We are not processing a declarator. */
2574 parser->in_declarator_p = false;
2576 /* We are not processing a template-argument-list. */
2577 parser->in_template_argument_list_p = false;
2579 /* We are not in an iteration statement. */
2580 parser->in_iteration_statement_p = false;
2582 /* We are not in a switch statement. */
2583 parser->in_switch_statement_p = false;
2585 /* We are not parsing a type-id inside an expression. */
2586 parser->in_type_id_in_expr_p = false;
2588 /* The unparsed function queue is empty. */
2589 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2591 /* There are no classes being defined. */
2592 parser->num_classes_being_defined = 0;
2594 /* No template parameters apply. */
2595 parser->num_template_parameter_lists = 0;
2600 /* Lexical conventions [gram.lex] */
2602 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2606 cp_parser_identifier (cp_parser* parser)
2610 /* Look for the identifier. */
2611 token = cp_parser_require (parser, CPP_NAME, "identifier");
2612 /* Return the value. */
2613 return token ? token->value : error_mark_node;
2616 /* Basic concepts [gram.basic] */
2618 /* Parse a translation-unit.
2621 declaration-seq [opt]
2623 Returns TRUE if all went well. */
2626 cp_parser_translation_unit (cp_parser* parser)
2628 /* The address of the first non-permanent object on the declarator
2630 static void *declarator_obstack_base;
2634 /* Create the declarator obstack, if necessary. */
2635 if (!cp_error_declarator)
2637 gcc_obstack_init (&declarator_obstack);
2638 /* Create the error declarator. */
2639 cp_error_declarator = make_declarator (cdk_error);
2640 /* Create the empty parameter list. */
2641 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2642 /* Remember where the base of the declarator obstack lies. */
2643 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2648 cp_parser_declaration_seq_opt (parser);
2650 /* If there are no tokens left then all went well. */
2651 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2653 /* Consume the EOF token. */
2654 cp_parser_require (parser, CPP_EOF, "end-of-file");
2657 finish_translation_unit ();
2664 cp_parser_error (parser, "expected declaration");
2670 /* Make sure the declarator obstack was fully cleaned up. */
2671 gcc_assert (obstack_next_free (&declarator_obstack)
2672 == declarator_obstack_base);
2674 /* All went well. */
2678 /* Expressions [gram.expr] */
2680 /* Parse a primary-expression.
2691 ( compound-statement )
2692 __builtin_va_arg ( assignment-expression , type-id )
2697 Returns a representation of the expression.
2699 *IDK indicates what kind of id-expression (if any) was present.
2701 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2702 used as the operand of a pointer-to-member. In that case,
2703 *QUALIFYING_CLASS gives the class that is used as the qualifying
2704 class in the pointer-to-member. */
2707 cp_parser_primary_expression (cp_parser *parser,
2709 tree *qualifying_class)
2713 /* Assume the primary expression is not an id-expression. */
2714 *idk = CP_ID_KIND_NONE;
2715 /* And that it cannot be used as pointer-to-member. */
2716 *qualifying_class = NULL_TREE;
2718 /* Peek at the next token. */
2719 token = cp_lexer_peek_token (parser->lexer);
2720 switch (token->type)
2731 token = cp_lexer_consume_token (parser->lexer);
2732 return token->value;
2736 token = cp_lexer_consume_token (parser->lexer);
2737 if (TREE_CHAIN (token->value))
2738 return TREE_CHAIN (token->value);
2740 return token->value;
2742 case CPP_OPEN_PAREN:
2745 bool saved_greater_than_is_operator_p;
2747 /* Consume the `('. */
2748 cp_lexer_consume_token (parser->lexer);
2749 /* Within a parenthesized expression, a `>' token is always
2750 the greater-than operator. */
2751 saved_greater_than_is_operator_p
2752 = parser->greater_than_is_operator_p;
2753 parser->greater_than_is_operator_p = true;
2754 /* If we see `( { ' then we are looking at the beginning of
2755 a GNU statement-expression. */
2756 if (cp_parser_allow_gnu_extensions_p (parser)
2757 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2759 /* Statement-expressions are not allowed by the standard. */
2761 pedwarn ("ISO C++ forbids braced-groups within expressions");
2763 /* And they're not allowed outside of a function-body; you
2764 cannot, for example, write:
2766 int i = ({ int j = 3; j + 1; });
2768 at class or namespace scope. */
2769 if (!at_function_scope_p ())
2770 error ("statement-expressions are allowed only inside functions");
2771 /* Start the statement-expression. */
2772 expr = begin_stmt_expr ();
2773 /* Parse the compound-statement. */
2774 cp_parser_compound_statement (parser, expr, false);
2776 expr = finish_stmt_expr (expr, false);
2780 /* Parse the parenthesized expression. */
2781 expr = cp_parser_expression (parser);
2782 /* Let the front end know that this expression was
2783 enclosed in parentheses. This matters in case, for
2784 example, the expression is of the form `A::B', since
2785 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2787 finish_parenthesized_expr (expr);
2789 /* The `>' token might be the end of a template-id or
2790 template-parameter-list now. */
2791 parser->greater_than_is_operator_p
2792 = saved_greater_than_is_operator_p;
2793 /* Consume the `)'. */
2794 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2795 cp_parser_skip_to_end_of_statement (parser);
2801 switch (token->keyword)
2803 /* These two are the boolean literals. */
2805 cp_lexer_consume_token (parser->lexer);
2806 return boolean_true_node;
2808 cp_lexer_consume_token (parser->lexer);
2809 return boolean_false_node;
2811 /* The `__null' literal. */
2813 cp_lexer_consume_token (parser->lexer);
2816 /* Recognize the `this' keyword. */
2818 cp_lexer_consume_token (parser->lexer);
2819 if (parser->local_variables_forbidden_p)
2821 error ("`this' may not be used in this context");
2822 return error_mark_node;
2824 /* Pointers cannot appear in constant-expressions. */
2825 if (cp_parser_non_integral_constant_expression (parser,
2827 return error_mark_node;
2828 return finish_this_expr ();
2830 /* The `operator' keyword can be the beginning of an
2835 case RID_FUNCTION_NAME:
2836 case RID_PRETTY_FUNCTION_NAME:
2837 case RID_C99_FUNCTION_NAME:
2838 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2839 __func__ are the names of variables -- but they are
2840 treated specially. Therefore, they are handled here,
2841 rather than relying on the generic id-expression logic
2842 below. Grammatically, these names are id-expressions.
2844 Consume the token. */
2845 token = cp_lexer_consume_token (parser->lexer);
2846 /* Look up the name. */
2847 return finish_fname (token->value);
2854 /* The `__builtin_va_arg' construct is used to handle
2855 `va_arg'. Consume the `__builtin_va_arg' token. */
2856 cp_lexer_consume_token (parser->lexer);
2857 /* Look for the opening `('. */
2858 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2859 /* Now, parse the assignment-expression. */
2860 expression = cp_parser_assignment_expression (parser);
2861 /* Look for the `,'. */
2862 cp_parser_require (parser, CPP_COMMA, "`,'");
2863 /* Parse the type-id. */
2864 type = cp_parser_type_id (parser);
2865 /* Look for the closing `)'. */
2866 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2867 /* Using `va_arg' in a constant-expression is not
2869 if (cp_parser_non_integral_constant_expression (parser,
2871 return error_mark_node;
2872 return build_x_va_arg (expression, type);
2876 return cp_parser_builtin_offsetof (parser);
2879 cp_parser_error (parser, "expected primary-expression");
2880 return error_mark_node;
2883 /* An id-expression can start with either an identifier, a
2884 `::' as the beginning of a qualified-id, or the "operator"
2888 case CPP_TEMPLATE_ID:
2889 case CPP_NESTED_NAME_SPECIFIER:
2893 const char *error_msg;
2896 /* Parse the id-expression. */
2898 = cp_parser_id_expression (parser,
2899 /*template_keyword_p=*/false,
2900 /*check_dependency_p=*/true,
2901 /*template_p=*/NULL,
2902 /*declarator_p=*/false);
2903 if (id_expression == error_mark_node)
2904 return error_mark_node;
2905 /* If we have a template-id, then no further lookup is
2906 required. If the template-id was for a template-class, we
2907 will sometimes have a TYPE_DECL at this point. */
2908 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2909 || TREE_CODE (id_expression) == TYPE_DECL)
2910 decl = id_expression;
2911 /* Look up the name. */
2916 decl = cp_parser_lookup_name (parser, id_expression,
2918 /*is_template=*/false,
2919 /*is_namespace=*/false,
2920 /*check_dependency=*/true,
2922 /* If the lookup was ambiguous, an error will already have
2925 return error_mark_node;
2926 /* If name lookup gives us a SCOPE_REF, then the
2927 qualifying scope was dependent. Just propagate the
2929 if (TREE_CODE (decl) == SCOPE_REF)
2931 if (TYPE_P (TREE_OPERAND (decl, 0)))
2932 *qualifying_class = TREE_OPERAND (decl, 0);
2935 /* Check to see if DECL is a local variable in a context
2936 where that is forbidden. */
2937 if (parser->local_variables_forbidden_p
2938 && local_variable_p (decl))
2940 /* It might be that we only found DECL because we are
2941 trying to be generous with pre-ISO scoping rules.
2942 For example, consider:
2946 for (int i = 0; i < 10; ++i) {}
2947 extern void f(int j = i);
2950 Here, name look up will originally find the out
2951 of scope `i'. We need to issue a warning message,
2952 but then use the global `i'. */
2953 decl = check_for_out_of_scope_variable (decl);
2954 if (local_variable_p (decl))
2956 error ("local variable `%D' may not appear in this context",
2958 return error_mark_node;
2963 decl = finish_id_expression (id_expression, decl, parser->scope,
2964 idk, qualifying_class,
2965 parser->integral_constant_expression_p,
2966 parser->allow_non_integral_constant_expression_p,
2967 &parser->non_integral_constant_expression_p,
2970 cp_parser_error (parser, error_msg);
2974 /* Anything else is an error. */
2976 cp_parser_error (parser, "expected primary-expression");
2977 return error_mark_node;
2981 /* Parse an id-expression.
2988 :: [opt] nested-name-specifier template [opt] unqualified-id
2990 :: operator-function-id
2993 Return a representation of the unqualified portion of the
2994 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2995 a `::' or nested-name-specifier.
2997 Often, if the id-expression was a qualified-id, the caller will
2998 want to make a SCOPE_REF to represent the qualified-id. This
2999 function does not do this in order to avoid wastefully creating
3000 SCOPE_REFs when they are not required.
3002 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3005 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3006 uninstantiated templates.
3008 If *TEMPLATE_P is non-NULL, it is set to true iff the
3009 `template' keyword is used to explicitly indicate that the entity
3010 named is a template.
3012 If DECLARATOR_P is true, the id-expression is appearing as part of
3013 a declarator, rather than as part of an expression. */
3016 cp_parser_id_expression (cp_parser *parser,
3017 bool template_keyword_p,
3018 bool check_dependency_p,
3022 bool global_scope_p;
3023 bool nested_name_specifier_p;
3025 /* Assume the `template' keyword was not used. */
3027 *template_p = false;
3029 /* Look for the optional `::' operator. */
3031 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3033 /* Look for the optional nested-name-specifier. */
3034 nested_name_specifier_p
3035 = (cp_parser_nested_name_specifier_opt (parser,
3036 /*typename_keyword_p=*/false,
3039 /*is_declarator=*/false)
3041 /* If there is a nested-name-specifier, then we are looking at
3042 the first qualified-id production. */
3043 if (nested_name_specifier_p)
3046 tree saved_object_scope;
3047 tree saved_qualifying_scope;
3048 tree unqualified_id;
3051 /* See if the next token is the `template' keyword. */
3053 template_p = &is_template;
3054 *template_p = cp_parser_optional_template_keyword (parser);
3055 /* Name lookup we do during the processing of the
3056 unqualified-id might obliterate SCOPE. */
3057 saved_scope = parser->scope;
3058 saved_object_scope = parser->object_scope;
3059 saved_qualifying_scope = parser->qualifying_scope;
3060 /* Process the final unqualified-id. */
3061 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3064 /* Restore the SAVED_SCOPE for our caller. */
3065 parser->scope = saved_scope;
3066 parser->object_scope = saved_object_scope;
3067 parser->qualifying_scope = saved_qualifying_scope;
3069 return unqualified_id;
3071 /* Otherwise, if we are in global scope, then we are looking at one
3072 of the other qualified-id productions. */
3073 else if (global_scope_p)
3078 /* Peek at the next token. */
3079 token = cp_lexer_peek_token (parser->lexer);
3081 /* If it's an identifier, and the next token is not a "<", then
3082 we can avoid the template-id case. This is an optimization
3083 for this common case. */
3084 if (token->type == CPP_NAME
3085 && !cp_parser_nth_token_starts_template_argument_list_p
3087 return cp_parser_identifier (parser);
3089 cp_parser_parse_tentatively (parser);
3090 /* Try a template-id. */
3091 id = cp_parser_template_id (parser,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3095 /* If that worked, we're done. */
3096 if (cp_parser_parse_definitely (parser))
3099 /* Peek at the next token. (Changes in the token buffer may
3100 have invalidated the pointer obtained above.) */
3101 token = cp_lexer_peek_token (parser->lexer);
3103 switch (token->type)
3106 return cp_parser_identifier (parser);
3109 if (token->keyword == RID_OPERATOR)
3110 return cp_parser_operator_function_id (parser);
3114 cp_parser_error (parser, "expected id-expression");
3115 return error_mark_node;
3119 return cp_parser_unqualified_id (parser, template_keyword_p,
3120 /*check_dependency_p=*/true,
3124 /* Parse an unqualified-id.
3128 operator-function-id
3129 conversion-function-id
3133 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3134 keyword, in a construct like `A::template ...'.
3136 Returns a representation of unqualified-id. For the `identifier'
3137 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3138 production a BIT_NOT_EXPR is returned; the operand of the
3139 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3140 other productions, see the documentation accompanying the
3141 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3142 names are looked up in uninstantiated templates. If DECLARATOR_P
3143 is true, the unqualified-id is appearing as part of a declarator,
3144 rather than as part of an expression. */
3147 cp_parser_unqualified_id (cp_parser* parser,
3148 bool template_keyword_p,
3149 bool check_dependency_p,
3154 /* Peek at the next token. */
3155 token = cp_lexer_peek_token (parser->lexer);
3157 switch (token->type)
3163 /* We don't know yet whether or not this will be a
3165 cp_parser_parse_tentatively (parser);
3166 /* Try a template-id. */
3167 id = cp_parser_template_id (parser, template_keyword_p,
3170 /* If it worked, we're done. */
3171 if (cp_parser_parse_definitely (parser))
3173 /* Otherwise, it's an ordinary identifier. */
3174 return cp_parser_identifier (parser);
3177 case CPP_TEMPLATE_ID:
3178 return cp_parser_template_id (parser, template_keyword_p,
3185 tree qualifying_scope;
3189 /* Consume the `~' token. */
3190 cp_lexer_consume_token (parser->lexer);
3191 /* Parse the class-name. The standard, as written, seems to
3194 template <typename T> struct S { ~S (); };
3195 template <typename T> S<T>::~S() {}
3197 is invalid, since `~' must be followed by a class-name, but
3198 `S<T>' is dependent, and so not known to be a class.
3199 That's not right; we need to look in uninstantiated
3200 templates. A further complication arises from:
3202 template <typename T> void f(T t) {
3206 Here, it is not possible to look up `T' in the scope of `T'
3207 itself. We must look in both the current scope, and the
3208 scope of the containing complete expression.
3210 Yet another issue is:
3219 The standard does not seem to say that the `S' in `~S'
3220 should refer to the type `S' and not the data member
3223 /* DR 244 says that we look up the name after the "~" in the
3224 same scope as we looked up the qualifying name. That idea
3225 isn't fully worked out; it's more complicated than that. */
3226 scope = parser->scope;
3227 object_scope = parser->object_scope;
3228 qualifying_scope = parser->qualifying_scope;
3230 /* If the name is of the form "X::~X" it's OK. */
3231 if (scope && TYPE_P (scope)
3232 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3233 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3235 && (cp_lexer_peek_token (parser->lexer)->value
3236 == TYPE_IDENTIFIER (scope)))
3238 cp_lexer_consume_token (parser->lexer);
3239 return build_nt (BIT_NOT_EXPR, scope);
3242 /* If there was an explicit qualification (S::~T), first look
3243 in the scope given by the qualification (i.e., S). */
3246 cp_parser_parse_tentatively (parser);
3247 type_decl = cp_parser_class_name (parser,
3248 /*typename_keyword_p=*/false,
3249 /*template_keyword_p=*/false,
3251 /*check_dependency=*/false,
3252 /*class_head_p=*/false,
3254 if (cp_parser_parse_definitely (parser))
3255 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3257 /* In "N::S::~S", look in "N" as well. */
3258 if (scope && qualifying_scope)
3260 cp_parser_parse_tentatively (parser);
3261 parser->scope = qualifying_scope;
3262 parser->object_scope = NULL_TREE;
3263 parser->qualifying_scope = NULL_TREE;
3265 = cp_parser_class_name (parser,
3266 /*typename_keyword_p=*/false,
3267 /*template_keyword_p=*/false,
3269 /*check_dependency=*/false,
3270 /*class_head_p=*/false,
3272 if (cp_parser_parse_definitely (parser))
3273 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3275 /* In "p->S::~T", look in the scope given by "*p" as well. */
3276 else if (object_scope)
3278 cp_parser_parse_tentatively (parser);
3279 parser->scope = object_scope;
3280 parser->object_scope = NULL_TREE;
3281 parser->qualifying_scope = NULL_TREE;
3283 = cp_parser_class_name (parser,
3284 /*typename_keyword_p=*/false,
3285 /*template_keyword_p=*/false,
3287 /*check_dependency=*/false,
3288 /*class_head_p=*/false,
3290 if (cp_parser_parse_definitely (parser))
3291 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3293 /* Look in the surrounding context. */
3294 parser->scope = NULL_TREE;
3295 parser->object_scope = NULL_TREE;
3296 parser->qualifying_scope = NULL_TREE;
3298 = cp_parser_class_name (parser,
3299 /*typename_keyword_p=*/false,
3300 /*template_keyword_p=*/false,
3302 /*check_dependency=*/false,
3303 /*class_head_p=*/false,
3305 /* If an error occurred, assume that the name of the
3306 destructor is the same as the name of the qualifying
3307 class. That allows us to keep parsing after running
3308 into ill-formed destructor names. */
3309 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3310 return build_nt (BIT_NOT_EXPR, scope);
3311 else if (type_decl == error_mark_node)
3312 return error_mark_node;
3316 A typedef-name that names a class shall not be used as the
3317 identifier in the declarator for a destructor declaration. */
3319 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3320 && !DECL_SELF_REFERENCE_P (type_decl))
3321 error ("typedef-name `%D' used as destructor declarator",
3324 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3328 if (token->keyword == RID_OPERATOR)
3332 /* This could be a template-id, so we try that first. */
3333 cp_parser_parse_tentatively (parser);
3334 /* Try a template-id. */
3335 id = cp_parser_template_id (parser, template_keyword_p,
3336 /*check_dependency_p=*/true,
3338 /* If that worked, we're done. */
3339 if (cp_parser_parse_definitely (parser))
3341 /* We still don't know whether we're looking at an
3342 operator-function-id or a conversion-function-id. */
3343 cp_parser_parse_tentatively (parser);
3344 /* Try an operator-function-id. */
3345 id = cp_parser_operator_function_id (parser);
3346 /* If that didn't work, try a conversion-function-id. */
3347 if (!cp_parser_parse_definitely (parser))
3348 id = cp_parser_conversion_function_id (parser);
3355 cp_parser_error (parser, "expected unqualified-id");
3356 return error_mark_node;
3360 /* Parse an (optional) nested-name-specifier.
3362 nested-name-specifier:
3363 class-or-namespace-name :: nested-name-specifier [opt]
3364 class-or-namespace-name :: template nested-name-specifier [opt]
3366 PARSER->SCOPE should be set appropriately before this function is
3367 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3368 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3371 Sets PARSER->SCOPE to the class (TYPE) or namespace
3372 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3373 it unchanged if there is no nested-name-specifier. Returns the new
3374 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3376 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3377 part of a declaration and/or decl-specifier. */
3380 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3381 bool typename_keyword_p,
3382 bool check_dependency_p,
3384 bool is_declaration)
3386 bool success = false;
3387 tree access_check = NULL_TREE;
3391 /* If the next token corresponds to a nested name specifier, there
3392 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3393 false, it may have been true before, in which case something
3394 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3395 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3396 CHECK_DEPENDENCY_P is false, we have to fall through into the
3398 if (check_dependency_p
3399 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3401 cp_parser_pre_parsed_nested_name_specifier (parser);
3402 return parser->scope;
3405 /* Remember where the nested-name-specifier starts. */
3406 if (cp_parser_parsing_tentatively (parser)
3407 && !cp_parser_committed_to_tentative_parse (parser))
3409 token = cp_lexer_peek_token (parser->lexer);
3410 start = cp_lexer_token_difference (parser->lexer,
3411 parser->lexer->first_token,
3417 push_deferring_access_checks (dk_deferred);
3423 tree saved_qualifying_scope;
3424 bool template_keyword_p;
3426 /* Spot cases that cannot be the beginning of a
3427 nested-name-specifier. */
3428 token = cp_lexer_peek_token (parser->lexer);
3430 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3431 the already parsed nested-name-specifier. */
3432 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3434 /* Grab the nested-name-specifier and continue the loop. */
3435 cp_parser_pre_parsed_nested_name_specifier (parser);
3440 /* Spot cases that cannot be the beginning of a
3441 nested-name-specifier. On the second and subsequent times
3442 through the loop, we look for the `template' keyword. */
3443 if (success && token->keyword == RID_TEMPLATE)
3445 /* A template-id can start a nested-name-specifier. */
3446 else if (token->type == CPP_TEMPLATE_ID)
3450 /* If the next token is not an identifier, then it is
3451 definitely not a class-or-namespace-name. */
3452 if (token->type != CPP_NAME)
3454 /* If the following token is neither a `<' (to begin a
3455 template-id), nor a `::', then we are not looking at a
3456 nested-name-specifier. */
3457 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3458 if (token->type != CPP_SCOPE
3459 && !cp_parser_nth_token_starts_template_argument_list_p
3464 /* The nested-name-specifier is optional, so we parse
3466 cp_parser_parse_tentatively (parser);
3468 /* Look for the optional `template' keyword, if this isn't the
3469 first time through the loop. */
3471 template_keyword_p = cp_parser_optional_template_keyword (parser);
3473 template_keyword_p = false;
3475 /* Save the old scope since the name lookup we are about to do
3476 might destroy it. */
3477 old_scope = parser->scope;
3478 saved_qualifying_scope = parser->qualifying_scope;
3479 /* Parse the qualifying entity. */
3481 = cp_parser_class_or_namespace_name (parser,
3487 /* Look for the `::' token. */
3488 cp_parser_require (parser, CPP_SCOPE, "`::'");
3490 /* If we found what we wanted, we keep going; otherwise, we're
3492 if (!cp_parser_parse_definitely (parser))
3494 bool error_p = false;
3496 /* Restore the OLD_SCOPE since it was valid before the
3497 failed attempt at finding the last
3498 class-or-namespace-name. */
3499 parser->scope = old_scope;
3500 parser->qualifying_scope = saved_qualifying_scope;
3501 /* If the next token is an identifier, and the one after
3502 that is a `::', then any valid interpretation would have
3503 found a class-or-namespace-name. */
3504 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3505 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3507 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3510 token = cp_lexer_consume_token (parser->lexer);
3515 decl = cp_parser_lookup_name_simple (parser, token->value);
3516 if (TREE_CODE (decl) == TEMPLATE_DECL)
3517 error ("`%D' used without template parameters",
3520 cp_parser_name_lookup_error
3521 (parser, token->value, decl,
3522 "is not a class or namespace");
3523 parser->scope = NULL_TREE;
3525 /* Treat this as a successful nested-name-specifier
3530 If the name found is not a class-name (clause
3531 _class_) or namespace-name (_namespace.def_), the
3532 program is ill-formed. */
3535 cp_lexer_consume_token (parser->lexer);
3540 /* We've found one valid nested-name-specifier. */
3542 /* Make sure we look in the right scope the next time through
3544 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3545 ? TREE_TYPE (new_scope)
3547 /* If it is a class scope, try to complete it; we are about to
3548 be looking up names inside the class. */
3549 if (TYPE_P (parser->scope)
3550 /* Since checking types for dependency can be expensive,
3551 avoid doing it if the type is already complete. */
3552 && !COMPLETE_TYPE_P (parser->scope)
3553 /* Do not try to complete dependent types. */
3554 && !dependent_type_p (parser->scope))
3555 complete_type (parser->scope);
3558 /* Retrieve any deferred checks. Do not pop this access checks yet
3559 so the memory will not be reclaimed during token replacing below. */
3560 access_check = get_deferred_access_checks ();
3562 /* If parsing tentatively, replace the sequence of tokens that makes
3563 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3564 token. That way, should we re-parse the token stream, we will
3565 not have to repeat the effort required to do the parse, nor will
3566 we issue duplicate error messages. */
3567 if (success && start >= 0)
3569 /* Find the token that corresponds to the start of the
3571 token = cp_lexer_advance_token (parser->lexer,
3572 parser->lexer->first_token,
3575 /* Reset the contents of the START token. */
3576 token->type = CPP_NESTED_NAME_SPECIFIER;
3577 token->value = build_tree_list (access_check, parser->scope);
3578 TREE_TYPE (token->value) = parser->qualifying_scope;
3579 token->keyword = RID_MAX;
3580 /* Purge all subsequent tokens. */
3581 cp_lexer_purge_tokens_after (parser->lexer, token);
3584 pop_deferring_access_checks ();
3585 return success ? parser->scope : NULL_TREE;
3588 /* Parse a nested-name-specifier. See
3589 cp_parser_nested_name_specifier_opt for details. This function
3590 behaves identically, except that it will an issue an error if no
3591 nested-name-specifier is present, and it will return
3592 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3596 cp_parser_nested_name_specifier (cp_parser *parser,
3597 bool typename_keyword_p,
3598 bool check_dependency_p,
3600 bool is_declaration)
3604 /* Look for the nested-name-specifier. */
3605 scope = cp_parser_nested_name_specifier_opt (parser,
3610 /* If it was not present, issue an error message. */
3613 cp_parser_error (parser, "expected nested-name-specifier");
3614 parser->scope = NULL_TREE;
3615 return error_mark_node;
3621 /* Parse a class-or-namespace-name.
3623 class-or-namespace-name:
3627 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3628 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3629 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3630 TYPE_P is TRUE iff the next name should be taken as a class-name,
3631 even the same name is declared to be another entity in the same
3634 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3635 specified by the class-or-namespace-name. If neither is found the
3636 ERROR_MARK_NODE is returned. */
3639 cp_parser_class_or_namespace_name (cp_parser *parser,
3640 bool typename_keyword_p,
3641 bool template_keyword_p,
3642 bool check_dependency_p,
3644 bool is_declaration)
3647 tree saved_qualifying_scope;
3648 tree saved_object_scope;
3652 /* Before we try to parse the class-name, we must save away the
3653 current PARSER->SCOPE since cp_parser_class_name will destroy
3655 saved_scope = parser->scope;
3656 saved_qualifying_scope = parser->qualifying_scope;
3657 saved_object_scope = parser->object_scope;
3658 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3659 there is no need to look for a namespace-name. */
3660 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3662 cp_parser_parse_tentatively (parser);
3663 scope = cp_parser_class_name (parser,
3668 /*class_head_p=*/false,
3670 /* If that didn't work, try for a namespace-name. */
3671 if (!only_class_p && !cp_parser_parse_definitely (parser))
3673 /* Restore the saved scope. */
3674 parser->scope = saved_scope;
3675 parser->qualifying_scope = saved_qualifying_scope;
3676 parser->object_scope = saved_object_scope;
3677 /* If we are not looking at an identifier followed by the scope
3678 resolution operator, then this is not part of a
3679 nested-name-specifier. (Note that this function is only used
3680 to parse the components of a nested-name-specifier.) */
3681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3682 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3683 return error_mark_node;
3684 scope = cp_parser_namespace_name (parser);
3690 /* Parse a postfix-expression.
3694 postfix-expression [ expression ]
3695 postfix-expression ( expression-list [opt] )
3696 simple-type-specifier ( expression-list [opt] )
3697 typename :: [opt] nested-name-specifier identifier
3698 ( expression-list [opt] )
3699 typename :: [opt] nested-name-specifier template [opt] template-id
3700 ( expression-list [opt] )
3701 postfix-expression . template [opt] id-expression
3702 postfix-expression -> template [opt] id-expression
3703 postfix-expression . pseudo-destructor-name
3704 postfix-expression -> pseudo-destructor-name
3705 postfix-expression ++
3706 postfix-expression --
3707 dynamic_cast < type-id > ( expression )
3708 static_cast < type-id > ( expression )
3709 reinterpret_cast < type-id > ( expression )
3710 const_cast < type-id > ( expression )
3711 typeid ( expression )
3717 ( type-id ) { initializer-list , [opt] }
3719 This extension is a GNU version of the C99 compound-literal
3720 construct. (The C99 grammar uses `type-name' instead of `type-id',
3721 but they are essentially the same concept.)
3723 If ADDRESS_P is true, the postfix expression is the operand of the
3726 Returns a representation of the expression. */
3729 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3733 cp_id_kind idk = CP_ID_KIND_NONE;
3734 tree postfix_expression = NULL_TREE;
3735 /* Non-NULL only if the current postfix-expression can be used to
3736 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3737 class used to qualify the member. */
3738 tree qualifying_class = NULL_TREE;
3740 /* Peek at the next token. */
3741 token = cp_lexer_peek_token (parser->lexer);
3742 /* Some of the productions are determined by keywords. */
3743 keyword = token->keyword;
3753 const char *saved_message;
3755 /* All of these can be handled in the same way from the point
3756 of view of parsing. Begin by consuming the token
3757 identifying the cast. */
3758 cp_lexer_consume_token (parser->lexer);
3760 /* New types cannot be defined in the cast. */
3761 saved_message = parser->type_definition_forbidden_message;
3762 parser->type_definition_forbidden_message
3763 = "types may not be defined in casts";
3765 /* Look for the opening `<'. */
3766 cp_parser_require (parser, CPP_LESS, "`<'");
3767 /* Parse the type to which we are casting. */
3768 type = cp_parser_type_id (parser);
3769 /* Look for the closing `>'. */
3770 cp_parser_require (parser, CPP_GREATER, "`>'");
3771 /* Restore the old message. */
3772 parser->type_definition_forbidden_message = saved_message;
3774 /* And the expression which is being cast. */
3775 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3776 expression = cp_parser_expression (parser);
3777 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3779 /* Only type conversions to integral or enumeration types
3780 can be used in constant-expressions. */
3781 if (parser->integral_constant_expression_p
3782 && !dependent_type_p (type)
3783 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3784 && (cp_parser_non_integral_constant_expression
3786 "a cast to a type other than an integral or "
3787 "enumeration type")))
3788 return error_mark_node;
3794 = build_dynamic_cast (type, expression);
3798 = build_static_cast (type, expression);
3802 = build_reinterpret_cast (type, expression);
3806 = build_const_cast (type, expression);
3817 const char *saved_message;
3818 bool saved_in_type_id_in_expr_p;
3820 /* Consume the `typeid' token. */
3821 cp_lexer_consume_token (parser->lexer);
3822 /* Look for the `(' token. */
3823 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3824 /* Types cannot be defined in a `typeid' expression. */
3825 saved_message = parser->type_definition_forbidden_message;
3826 parser->type_definition_forbidden_message
3827 = "types may not be defined in a `typeid\' expression";
3828 /* We can't be sure yet whether we're looking at a type-id or an
3830 cp_parser_parse_tentatively (parser);
3831 /* Try a type-id first. */
3832 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3833 parser->in_type_id_in_expr_p = true;
3834 type = cp_parser_type_id (parser);
3835 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3836 /* Look for the `)' token. Otherwise, we can't be sure that
3837 we're not looking at an expression: consider `typeid (int
3838 (3))', for example. */
3839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3840 /* If all went well, simply lookup the type-id. */
3841 if (cp_parser_parse_definitely (parser))
3842 postfix_expression = get_typeid (type);
3843 /* Otherwise, fall back to the expression variant. */
3848 /* Look for an expression. */
3849 expression = cp_parser_expression (parser);
3850 /* Compute its typeid. */
3851 postfix_expression = build_typeid (expression);
3852 /* Look for the `)' token. */
3853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3855 /* `typeid' may not appear in an integral constant expression. */
3856 if (cp_parser_non_integral_constant_expression(parser,
3857 "`typeid' operator"))
3858 return error_mark_node;
3859 /* Restore the saved message. */
3860 parser->type_definition_forbidden_message = saved_message;
3866 bool template_p = false;
3870 /* Consume the `typename' token. */
3871 cp_lexer_consume_token (parser->lexer);
3872 /* Look for the optional `::' operator. */
3873 cp_parser_global_scope_opt (parser,
3874 /*current_scope_valid_p=*/false);
3875 /* Look for the nested-name-specifier. */
3876 cp_parser_nested_name_specifier (parser,
3877 /*typename_keyword_p=*/true,
3878 /*check_dependency_p=*/true,
3880 /*is_declaration=*/true);
3881 /* Look for the optional `template' keyword. */
3882 template_p = cp_parser_optional_template_keyword (parser);
3883 /* We don't know whether we're looking at a template-id or an
3885 cp_parser_parse_tentatively (parser);
3886 /* Try a template-id. */
3887 id = cp_parser_template_id (parser, template_p,
3888 /*check_dependency_p=*/true,
3889 /*is_declaration=*/true);
3890 /* If that didn't work, try an identifier. */
3891 if (!cp_parser_parse_definitely (parser))
3892 id = cp_parser_identifier (parser);
3893 /* If we look up a template-id in a non-dependent qualifying
3894 scope, there's no need to create a dependent type. */
3895 if (TREE_CODE (id) == TYPE_DECL
3896 && !dependent_type_p (parser->scope))
3897 type = TREE_TYPE (id);
3898 /* Create a TYPENAME_TYPE to represent the type to which the
3899 functional cast is being performed. */
3901 type = make_typename_type (parser->scope, id,
3904 postfix_expression = cp_parser_functional_cast (parser, type);
3912 /* If the next thing is a simple-type-specifier, we may be
3913 looking at a functional cast. We could also be looking at
3914 an id-expression. So, we try the functional cast, and if
3915 that doesn't work we fall back to the primary-expression. */
3916 cp_parser_parse_tentatively (parser);
3917 /* Look for the simple-type-specifier. */
3918 type = cp_parser_simple_type_specifier (parser,
3919 /*decl_specs=*/NULL,
3920 CP_PARSER_FLAGS_NONE);
3921 /* Parse the cast itself. */
3922 if (!cp_parser_error_occurred (parser))
3924 = cp_parser_functional_cast (parser, type);
3925 /* If that worked, we're done. */
3926 if (cp_parser_parse_definitely (parser))
3929 /* If the functional-cast didn't work out, try a
3930 compound-literal. */
3931 if (cp_parser_allow_gnu_extensions_p (parser)
3932 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3934 tree initializer_list = NULL_TREE;
3935 bool saved_in_type_id_in_expr_p;
3937 cp_parser_parse_tentatively (parser);
3938 /* Consume the `('. */
3939 cp_lexer_consume_token (parser->lexer);
3940 /* Parse the type. */
3941 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3942 parser->in_type_id_in_expr_p = true;
3943 type = cp_parser_type_id (parser);
3944 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3945 /* Look for the `)'. */
3946 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3947 /* Look for the `{'. */
3948 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3949 /* If things aren't going well, there's no need to
3951 if (!cp_parser_error_occurred (parser))
3953 bool non_constant_p;
3954 /* Parse the initializer-list. */
3956 = cp_parser_initializer_list (parser, &non_constant_p);
3957 /* Allow a trailing `,'. */
3958 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3959 cp_lexer_consume_token (parser->lexer);
3960 /* Look for the final `}'. */
3961 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3963 /* If that worked, we're definitely looking at a
3964 compound-literal expression. */
3965 if (cp_parser_parse_definitely (parser))
3967 /* Warn the user that a compound literal is not
3968 allowed in standard C++. */
3970 pedwarn ("ISO C++ forbids compound-literals");
3971 /* Form the representation of the compound-literal. */
3973 = finish_compound_literal (type, initializer_list);
3978 /* It must be a primary-expression. */
3979 postfix_expression = cp_parser_primary_expression (parser,
3986 /* If we were avoiding committing to the processing of a
3987 qualified-id until we knew whether or not we had a
3988 pointer-to-member, we now know. */
3989 if (qualifying_class)
3993 /* Peek at the next token. */
3994 token = cp_lexer_peek_token (parser->lexer);
3995 done = (token->type != CPP_OPEN_SQUARE
3996 && token->type != CPP_OPEN_PAREN
3997 && token->type != CPP_DOT
3998 && token->type != CPP_DEREF
3999 && token->type != CPP_PLUS_PLUS
4000 && token->type != CPP_MINUS_MINUS);
4002 postfix_expression = finish_qualified_id_expr (qualifying_class,
4007 return postfix_expression;
4010 /* Keep looping until the postfix-expression is complete. */
4013 if (idk == CP_ID_KIND_UNQUALIFIED
4014 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4015 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4016 /* It is not a Koenig lookup function call. */
4018 = unqualified_name_lookup_error (postfix_expression);
4020 /* Peek at the next token. */
4021 token = cp_lexer_peek_token (parser->lexer);
4023 switch (token->type)
4025 case CPP_OPEN_SQUARE:
4027 = cp_parser_postfix_open_square_expression (parser,
4030 idk = CP_ID_KIND_NONE;
4033 case CPP_OPEN_PAREN:
4034 /* postfix-expression ( expression-list [opt] ) */
4037 tree args = (cp_parser_parenthesized_expression_list
4038 (parser, false, /*non_constant_p=*/NULL));
4040 if (args == error_mark_node)
4042 postfix_expression = error_mark_node;
4046 /* Function calls are not permitted in
4047 constant-expressions. */
4048 if (cp_parser_non_integral_constant_expression (parser,
4051 postfix_expression = error_mark_node;
4056 if (idk == CP_ID_KIND_UNQUALIFIED)
4058 /* We do not perform argument-dependent lookup if
4059 normal lookup finds a non-function, in accordance
4060 with the expected resolution of DR 218. */
4062 && (is_overloaded_fn (postfix_expression)
4063 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4067 = perform_koenig_lookup (postfix_expression, args);
4069 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4071 = unqualified_fn_lookup_error (postfix_expression);
4074 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4076 tree instance = TREE_OPERAND (postfix_expression, 0);
4077 tree fn = TREE_OPERAND (postfix_expression, 1);
4079 if (processing_template_decl
4080 && (type_dependent_expression_p (instance)
4081 || (!BASELINK_P (fn)
4082 && TREE_CODE (fn) != FIELD_DECL)
4083 || type_dependent_expression_p (fn)
4084 || any_type_dependent_arguments_p (args)))
4087 = build_min_nt (CALL_EXPR, postfix_expression,
4092 if (BASELINK_P (fn))
4094 = (build_new_method_call
4095 (instance, fn, args, NULL_TREE,
4096 (idk == CP_ID_KIND_QUALIFIED
4097 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4100 = finish_call_expr (postfix_expression, args,
4101 /*disallow_virtual=*/false,
4102 /*koenig_p=*/false);
4104 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4105 || TREE_CODE (postfix_expression) == MEMBER_REF
4106 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4107 postfix_expression = (build_offset_ref_call_from_tree
4108 (postfix_expression, args));
4109 else if (idk == CP_ID_KIND_QUALIFIED)
4110 /* A call to a static class member, or a namespace-scope
4113 = finish_call_expr (postfix_expression, args,
4114 /*disallow_virtual=*/true,
4117 /* All other function calls. */
4119 = finish_call_expr (postfix_expression, args,
4120 /*disallow_virtual=*/false,
4123 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4124 idk = CP_ID_KIND_NONE;
4130 /* postfix-expression . template [opt] id-expression
4131 postfix-expression . pseudo-destructor-name
4132 postfix-expression -> template [opt] id-expression
4133 postfix-expression -> pseudo-destructor-name */
4135 /* Consume the `.' or `->' operator. */
4136 cp_lexer_consume_token (parser->lexer);
4139 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4145 /* postfix-expression ++ */
4146 /* Consume the `++' token. */
4147 cp_lexer_consume_token (parser->lexer);
4148 /* Generate a representation for the complete expression. */
4150 = finish_increment_expr (postfix_expression,
4151 POSTINCREMENT_EXPR);
4152 /* Increments may not appear in constant-expressions. */
4153 if (cp_parser_non_integral_constant_expression (parser,
4155 postfix_expression = error_mark_node;
4156 idk = CP_ID_KIND_NONE;
4159 case CPP_MINUS_MINUS:
4160 /* postfix-expression -- */
4161 /* Consume the `--' token. */
4162 cp_lexer_consume_token (parser->lexer);
4163 /* Generate a representation for the complete expression. */
4165 = finish_increment_expr (postfix_expression,
4166 POSTDECREMENT_EXPR);
4167 /* Decrements may not appear in constant-expressions. */
4168 if (cp_parser_non_integral_constant_expression (parser,
4170 postfix_expression = error_mark_node;
4171 idk = CP_ID_KIND_NONE;
4175 return postfix_expression;
4179 /* We should never get here. */
4181 return error_mark_node;
4184 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4185 by cp_parser_builtin_offsetof. We're looking for
4187 postfix-expression [ expression ]
4189 FOR_OFFSETOF is set if we're being called in that context, which
4190 changes how we deal with integer constant expressions. */
4193 cp_parser_postfix_open_square_expression (cp_parser *parser,
4194 tree postfix_expression,
4199 /* Consume the `[' token. */
4200 cp_lexer_consume_token (parser->lexer);
4202 /* Parse the index expression. */
4203 /* ??? For offsetof, there is a question of what to allow here. If
4204 offsetof is not being used in an integral constant expression context,
4205 then we *could* get the right answer by computing the value at runtime.
4206 If we are in an integral constant expression context, then we might
4207 could accept any constant expression; hard to say without analysis.
4208 Rather than open the barn door too wide right away, allow only integer
4209 constant expresions here. */
4211 index = cp_parser_constant_expression (parser, false, NULL);
4213 index = cp_parser_expression (parser);
4215 /* Look for the closing `]'. */
4216 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4218 /* Build the ARRAY_REF. */
4219 postfix_expression = grok_array_decl (postfix_expression, index);
4221 /* When not doing offsetof, array references are not permitted in
4222 constant-expressions. */
4224 && (cp_parser_non_integral_constant_expression
4225 (parser, "an array reference")))
4226 postfix_expression = error_mark_node;
4228 return postfix_expression;
4231 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4232 by cp_parser_builtin_offsetof. We're looking for
4234 postfix-expression . template [opt] id-expression
4235 postfix-expression . pseudo-destructor-name
4236 postfix-expression -> template [opt] id-expression
4237 postfix-expression -> pseudo-destructor-name
4239 FOR_OFFSETOF is set if we're being called in that context. That sorta
4240 limits what of the above we'll actually accept, but nevermind.
4241 TOKEN_TYPE is the "." or "->" token, which will already have been
4242 removed from the stream. */
4245 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4246 enum cpp_ttype token_type,
4247 tree postfix_expression,
4248 bool for_offsetof, cp_id_kind *idk)
4253 tree scope = NULL_TREE;
4255 /* If this is a `->' operator, dereference the pointer. */
4256 if (token_type == CPP_DEREF)
4257 postfix_expression = build_x_arrow (postfix_expression);
4258 /* Check to see whether or not the expression is type-dependent. */
4259 dependent_p = type_dependent_expression_p (postfix_expression);
4260 /* The identifier following the `->' or `.' is not qualified. */
4261 parser->scope = NULL_TREE;
4262 parser->qualifying_scope = NULL_TREE;
4263 parser->object_scope = NULL_TREE;
4264 *idk = CP_ID_KIND_NONE;
4265 /* Enter the scope corresponding to the type of the object
4266 given by the POSTFIX_EXPRESSION. */
4267 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4269 scope = TREE_TYPE (postfix_expression);
4270 /* According to the standard, no expression should ever have
4271 reference type. Unfortunately, we do not currently match
4272 the standard in this respect in that our internal representation
4273 of an expression may have reference type even when the standard
4274 says it does not. Therefore, we have to manually obtain the
4275 underlying type here. */
4276 scope = non_reference (scope);
4277 /* The type of the POSTFIX_EXPRESSION must be complete. */
4278 scope = complete_type_or_else (scope, NULL_TREE);
4279 /* Let the name lookup machinery know that we are processing a
4280 class member access expression. */
4281 parser->context->object_type = scope;
4282 /* If something went wrong, we want to be able to discern that case,
4283 as opposed to the case where there was no SCOPE due to the type
4284 of expression being dependent. */
4286 scope = error_mark_node;
4287 /* If the SCOPE was erroneous, make the various semantic analysis
4288 functions exit quickly -- and without issuing additional error
4290 if (scope == error_mark_node)
4291 postfix_expression = error_mark_node;
4294 /* If the SCOPE is not a scalar type, we are looking at an
4295 ordinary class member access expression, rather than a
4296 pseudo-destructor-name. */
4297 if (!scope || !SCALAR_TYPE_P (scope))
4299 template_p = cp_parser_optional_template_keyword (parser);
4300 /* Parse the id-expression. */
4301 name = cp_parser_id_expression (parser, template_p,
4302 /*check_dependency_p=*/true,
4303 /*template_p=*/NULL,
4304 /*declarator_p=*/false);
4305 /* In general, build a SCOPE_REF if the member name is qualified.
4306 However, if the name was not dependent and has already been
4307 resolved; there is no need to build the SCOPE_REF. For example;
4309 struct X { void f(); };
4310 template <typename T> void f(T* t) { t->X::f(); }
4312 Even though "t" is dependent, "X::f" is not and has been resolved
4313 to a BASELINK; there is no need to include scope information. */
4315 /* But we do need to remember that there was an explicit scope for
4316 virtual function calls. */
4318 *idk = CP_ID_KIND_QUALIFIED;
4320 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4322 name = build_nt (SCOPE_REF, parser->scope, name);
4323 parser->scope = NULL_TREE;
4324 parser->qualifying_scope = NULL_TREE;
4325 parser->object_scope = NULL_TREE;
4327 if (scope && name && BASELINK_P (name))
4328 adjust_result_of_qualified_name_lookup
4329 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4331 = finish_class_member_access_expr (postfix_expression, name);
4333 /* Otherwise, try the pseudo-destructor-name production. */
4339 /* Parse the pseudo-destructor-name. */
4340 cp_parser_pseudo_destructor_name (parser, &s, &type);
4341 /* Form the call. */
4343 = finish_pseudo_destructor_expr (postfix_expression,
4344 s, TREE_TYPE (type));
4347 /* We no longer need to look up names in the scope of the object on
4348 the left-hand side of the `.' or `->' operator. */
4349 parser->context->object_type = NULL_TREE;
4351 /* Outside of offsetof, these operators may not appear in
4352 constant-expressions. */
4354 && (cp_parser_non_integral_constant_expression
4355 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4356 postfix_expression = error_mark_node;
4358 return postfix_expression;
4361 /* Parse a parenthesized expression-list.
4364 assignment-expression
4365 expression-list, assignment-expression
4370 identifier, expression-list
4372 Returns a TREE_LIST. The TREE_VALUE of each node is a
4373 representation of an assignment-expression. Note that a TREE_LIST
4374 is returned even if there is only a single expression in the list.
4375 error_mark_node is returned if the ( and or ) are
4376 missing. NULL_TREE is returned on no expressions. The parentheses
4377 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4378 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4379 indicates whether or not all of the expressions in the list were
4383 cp_parser_parenthesized_expression_list (cp_parser* parser,
4384 bool is_attribute_list,
4385 bool *non_constant_p)
4387 tree expression_list = NULL_TREE;
4388 tree identifier = NULL_TREE;
4390 /* Assume all the expressions will be constant. */
4392 *non_constant_p = false;
4394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4395 return error_mark_node;
4397 /* Consume expressions until there are no more. */
4398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4403 /* At the beginning of attribute lists, check to see if the
4404 next token is an identifier. */
4405 if (is_attribute_list
4406 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4410 /* Consume the identifier. */
4411 token = cp_lexer_consume_token (parser->lexer);
4412 /* Save the identifier. */
4413 identifier = token->value;
4417 /* Parse the next assignment-expression. */
4420 bool expr_non_constant_p;
4421 expr = (cp_parser_constant_expression
4422 (parser, /*allow_non_constant_p=*/true,
4423 &expr_non_constant_p));
4424 if (expr_non_constant_p)
4425 *non_constant_p = true;
4428 expr = cp_parser_assignment_expression (parser);
4430 /* Add it to the list. We add error_mark_node
4431 expressions to the list, so that we can still tell if
4432 the correct form for a parenthesized expression-list
4433 is found. That gives better errors. */
4434 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4436 if (expr == error_mark_node)
4440 /* After the first item, attribute lists look the same as
4441 expression lists. */
4442 is_attribute_list = false;
4445 /* If the next token isn't a `,', then we are done. */
4446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4449 /* Otherwise, consume the `,' and keep going. */
4450 cp_lexer_consume_token (parser->lexer);
4453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4458 /* We try and resync to an unnested comma, as that will give the
4459 user better diagnostics. */
4460 ending = cp_parser_skip_to_closing_parenthesis (parser,
4461 /*recovering=*/true,
4463 /*consume_paren=*/true);
4467 return error_mark_node;
4470 /* We built up the list in reverse order so we must reverse it now. */
4471 expression_list = nreverse (expression_list);
4473 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4475 return expression_list;
4478 /* Parse a pseudo-destructor-name.
4480 pseudo-destructor-name:
4481 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4482 :: [opt] nested-name-specifier template template-id :: ~ type-name
4483 :: [opt] nested-name-specifier [opt] ~ type-name
4485 If either of the first two productions is used, sets *SCOPE to the
4486 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4487 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4488 or ERROR_MARK_NODE if the parse fails. */
4491 cp_parser_pseudo_destructor_name (cp_parser* parser,
4495 bool nested_name_specifier_p;
4497 /* Assume that things will not work out. */
4498 *type = error_mark_node;
4500 /* Look for the optional `::' operator. */
4501 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4502 /* Look for the optional nested-name-specifier. */
4503 nested_name_specifier_p
4504 = (cp_parser_nested_name_specifier_opt (parser,
4505 /*typename_keyword_p=*/false,
4506 /*check_dependency_p=*/true,
4508 /*is_declaration=*/true)
4510 /* Now, if we saw a nested-name-specifier, we might be doing the
4511 second production. */
4512 if (nested_name_specifier_p
4513 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4515 /* Consume the `template' keyword. */
4516 cp_lexer_consume_token (parser->lexer);
4517 /* Parse the template-id. */
4518 cp_parser_template_id (parser,
4519 /*template_keyword_p=*/true,
4520 /*check_dependency_p=*/false,
4521 /*is_declaration=*/true);
4522 /* Look for the `::' token. */
4523 cp_parser_require (parser, CPP_SCOPE, "`::'");
4525 /* If the next token is not a `~', then there might be some
4526 additional qualification. */
4527 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4529 /* Look for the type-name. */
4530 *scope = TREE_TYPE (cp_parser_type_name (parser));
4532 if (*scope == error_mark_node)
4535 /* If we don't have ::~, then something has gone wrong. Since
4536 the only caller of this function is looking for something
4537 after `.' or `->' after a scalar type, most likely the
4538 program is trying to get a member of a non-aggregate
4540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4541 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4543 cp_parser_error (parser, "request for member of non-aggregate type");
4547 /* Look for the `::' token. */
4548 cp_parser_require (parser, CPP_SCOPE, "`::'");
4553 /* Look for the `~'. */
4554 cp_parser_require (parser, CPP_COMPL, "`~'");
4555 /* Look for the type-name again. We are not responsible for
4556 checking that it matches the first type-name. */
4557 *type = cp_parser_type_name (parser);
4560 /* Parse a unary-expression.
4566 unary-operator cast-expression
4567 sizeof unary-expression
4575 __extension__ cast-expression
4576 __alignof__ unary-expression
4577 __alignof__ ( type-id )
4578 __real__ cast-expression
4579 __imag__ cast-expression
4582 ADDRESS_P is true iff the unary-expression is appearing as the
4583 operand of the `&' operator.
4585 Returns a representation of the expression. */
4588 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4591 enum tree_code unary_operator;
4593 /* Peek at the next token. */
4594 token = cp_lexer_peek_token (parser->lexer);
4595 /* Some keywords give away the kind of expression. */
4596 if (token->type == CPP_KEYWORD)
4598 enum rid keyword = token->keyword;
4608 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4609 /* Consume the token. */
4610 cp_lexer_consume_token (parser->lexer);
4611 /* Parse the operand. */
4612 operand = cp_parser_sizeof_operand (parser, keyword);
4614 if (TYPE_P (operand))
4615 return cxx_sizeof_or_alignof_type (operand, op, true);
4617 return cxx_sizeof_or_alignof_expr (operand, op);
4621 return cp_parser_new_expression (parser);
4624 return cp_parser_delete_expression (parser);
4628 /* The saved value of the PEDANTIC flag. */
4632 /* Save away the PEDANTIC flag. */
4633 cp_parser_extension_opt (parser, &saved_pedantic);
4634 /* Parse the cast-expression. */
4635 expr = cp_parser_simple_cast_expression (parser);
4636 /* Restore the PEDANTIC flag. */
4637 pedantic = saved_pedantic;
4647 /* Consume the `__real__' or `__imag__' token. */
4648 cp_lexer_consume_token (parser->lexer);
4649 /* Parse the cast-expression. */
4650 expression = cp_parser_simple_cast_expression (parser);
4651 /* Create the complete representation. */
4652 return build_x_unary_op ((keyword == RID_REALPART
4653 ? REALPART_EXPR : IMAGPART_EXPR),
4663 /* Look for the `:: new' and `:: delete', which also signal the
4664 beginning of a new-expression, or delete-expression,
4665 respectively. If the next token is `::', then it might be one of
4667 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4671 /* See if the token after the `::' is one of the keywords in
4672 which we're interested. */
4673 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4674 /* If it's `new', we have a new-expression. */
4675 if (keyword == RID_NEW)
4676 return cp_parser_new_expression (parser);
4677 /* Similarly, for `delete'. */
4678 else if (keyword == RID_DELETE)
4679 return cp_parser_delete_expression (parser);
4682 /* Look for a unary operator. */
4683 unary_operator = cp_parser_unary_operator (token);
4684 /* The `++' and `--' operators can be handled similarly, even though
4685 they are not technically unary-operators in the grammar. */
4686 if (unary_operator == ERROR_MARK)
4688 if (token->type == CPP_PLUS_PLUS)
4689 unary_operator = PREINCREMENT_EXPR;
4690 else if (token->type == CPP_MINUS_MINUS)
4691 unary_operator = PREDECREMENT_EXPR;
4692 /* Handle the GNU address-of-label extension. */
4693 else if (cp_parser_allow_gnu_extensions_p (parser)
4694 && token->type == CPP_AND_AND)
4698 /* Consume the '&&' token. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Look for the identifier. */
4701 identifier = cp_parser_identifier (parser);
4702 /* Create an expression representing the address. */
4703 return finish_label_address_expr (identifier);
4706 if (unary_operator != ERROR_MARK)
4708 tree cast_expression;
4709 tree expression = error_mark_node;
4710 const char *non_constant_p = NULL;
4712 /* Consume the operator token. */
4713 token = cp_lexer_consume_token (parser->lexer);
4714 /* Parse the cast-expression. */
4716 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4717 /* Now, build an appropriate representation. */
4718 switch (unary_operator)
4721 non_constant_p = "`*'";
4722 expression = build_x_indirect_ref (cast_expression, "unary *");
4726 non_constant_p = "`&'";
4729 expression = build_x_unary_op (unary_operator, cast_expression);
4732 case PREINCREMENT_EXPR:
4733 case PREDECREMENT_EXPR:
4734 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4739 case TRUTH_NOT_EXPR:
4740 expression = finish_unary_op_expr (unary_operator, cast_expression);
4748 && cp_parser_non_integral_constant_expression (parser,
4750 expression = error_mark_node;
4755 return cp_parser_postfix_expression (parser, address_p);
4758 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4759 unary-operator, the corresponding tree code is returned. */
4761 static enum tree_code
4762 cp_parser_unary_operator (cp_token* token)
4764 switch (token->type)
4767 return INDIRECT_REF;
4773 return CONVERT_EXPR;
4779 return TRUTH_NOT_EXPR;
4782 return BIT_NOT_EXPR;
4789 /* Parse a new-expression.
4792 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4793 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4795 Returns a representation of the expression. */
4798 cp_parser_new_expression (cp_parser* parser)
4800 bool global_scope_p;
4806 /* Look for the optional `::' operator. */
4808 = (cp_parser_global_scope_opt (parser,
4809 /*current_scope_valid_p=*/false)
4811 /* Look for the `new' operator. */
4812 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4813 /* There's no easy way to tell a new-placement from the
4814 `( type-id )' construct. */
4815 cp_parser_parse_tentatively (parser);
4816 /* Look for a new-placement. */
4817 placement = cp_parser_new_placement (parser);
4818 /* If that didn't work out, there's no new-placement. */
4819 if (!cp_parser_parse_definitely (parser))
4820 placement = NULL_TREE;
4822 /* If the next token is a `(', then we have a parenthesized
4824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4826 /* Consume the `('. */
4827 cp_lexer_consume_token (parser->lexer);
4828 /* Parse the type-id. */
4829 type = cp_parser_type_id (parser);
4830 /* Look for the closing `)'. */
4831 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4832 /* There should not be a direct-new-declarator in this production,
4833 but GCC used to allowed this, so we check and emit a sensible error
4834 message for this case. */
4835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4837 error ("array bound forbidden after parenthesized type-id");
4838 inform ("try removing the parentheses around the type-id");
4839 cp_parser_direct_new_declarator (parser);
4841 nelts = integer_one_node;
4843 /* Otherwise, there must be a new-type-id. */
4845 type = cp_parser_new_type_id (parser, &nelts);
4847 /* If the next token is a `(', then we have a new-initializer. */
4848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4849 initializer = cp_parser_new_initializer (parser);
4851 initializer = NULL_TREE;
4853 /* A new-expression may not appear in an integral constant
4855 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4856 return error_mark_node;
4858 /* Create a representation of the new-expression. */
4859 return build_new (placement, type, nelts, initializer, global_scope_p);
4862 /* Parse a new-placement.
4867 Returns the same representation as for an expression-list. */
4870 cp_parser_new_placement (cp_parser* parser)
4872 tree expression_list;
4874 /* Parse the expression-list. */
4875 expression_list = (cp_parser_parenthesized_expression_list
4876 (parser, false, /*non_constant_p=*/NULL));
4878 return expression_list;
4881 /* Parse a new-type-id.
4884 type-specifier-seq new-declarator [opt]
4886 Returns the TYPE allocated. If the new-type-id indicates an array
4887 type, *NELTS is set to the number of elements in the last array
4888 bound; the TYPE will not include the last array bound. */
4891 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4893 cp_decl_specifier_seq type_specifier_seq;
4894 cp_declarator *new_declarator;
4895 cp_declarator *declarator;
4896 cp_declarator *outer_declarator;
4897 const char *saved_message;
4900 /* The type-specifier sequence must not contain type definitions.
4901 (It cannot contain declarations of new types either, but if they
4902 are not definitions we will catch that because they are not
4904 saved_message = parser->type_definition_forbidden_message;
4905 parser->type_definition_forbidden_message
4906 = "types may not be defined in a new-type-id";
4907 /* Parse the type-specifier-seq. */
4908 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4909 /* Restore the old message. */
4910 parser->type_definition_forbidden_message = saved_message;
4911 /* Parse the new-declarator. */
4912 new_declarator = cp_parser_new_declarator_opt (parser);
4914 /* Determine the number of elements in the last array dimension, if
4917 /* Skip down to the last array dimension. */
4918 declarator = new_declarator;
4919 outer_declarator = NULL;
4920 while (declarator && (declarator->kind == cdk_pointer
4921 || declarator->kind == cdk_ptrmem))
4923 outer_declarator = declarator;
4924 declarator = declarator->declarator;
4927 && declarator->kind == cdk_array
4928 && declarator->declarator
4929 && declarator->declarator->kind == cdk_array)
4931 outer_declarator = declarator;
4932 declarator = declarator->declarator;
4935 if (declarator && declarator->kind == cdk_array)
4937 *nelts = declarator->u.array.bounds;
4938 if (*nelts == error_mark_node)
4939 *nelts = integer_one_node;
4940 else if (!processing_template_decl)
4942 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4944 pedwarn ("size in array new must have integral type");
4945 *nelts = save_expr (cp_convert (sizetype, *nelts));
4946 if (*nelts == integer_zero_node)
4947 warning ("zero size array reserves no space");
4949 if (outer_declarator)
4950 outer_declarator->declarator = declarator->declarator;
4952 new_declarator = NULL;
4955 type = groktypename (&type_specifier_seq, new_declarator);
4956 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4958 *nelts = array_type_nelts_top (type);
4959 type = TREE_TYPE (type);
4964 /* Parse an (optional) new-declarator.
4967 ptr-operator new-declarator [opt]
4968 direct-new-declarator
4970 Returns the declarator. */
4972 static cp_declarator *
4973 cp_parser_new_declarator_opt (cp_parser* parser)
4975 enum tree_code code;
4977 cp_cv_quals cv_quals;
4979 /* We don't know if there's a ptr-operator next, or not. */
4980 cp_parser_parse_tentatively (parser);
4981 /* Look for a ptr-operator. */
4982 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4983 /* If that worked, look for more new-declarators. */
4984 if (cp_parser_parse_definitely (parser))
4986 cp_declarator *declarator;
4988 /* Parse another optional declarator. */
4989 declarator = cp_parser_new_declarator_opt (parser);
4991 /* Create the representation of the declarator. */
4993 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4994 else if (code == INDIRECT_REF)
4995 declarator = make_pointer_declarator (cv_quals, declarator);
4997 declarator = make_reference_declarator (cv_quals, declarator);
5002 /* If the next token is a `[', there is a direct-new-declarator. */
5003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5004 return cp_parser_direct_new_declarator (parser);
5009 /* Parse a direct-new-declarator.
5011 direct-new-declarator:
5013 direct-new-declarator [constant-expression]
5017 static cp_declarator *
5018 cp_parser_direct_new_declarator (cp_parser* parser)
5020 cp_declarator *declarator = NULL;
5026 /* Look for the opening `['. */
5027 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5028 /* The first expression is not required to be constant. */
5031 expression = cp_parser_expression (parser);
5032 /* The standard requires that the expression have integral
5033 type. DR 74 adds enumeration types. We believe that the
5034 real intent is that these expressions be handled like the
5035 expression in a `switch' condition, which also allows
5036 classes with a single conversion to integral or
5037 enumeration type. */
5038 if (!processing_template_decl)
5041 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5046 error ("expression in new-declarator must have integral or enumeration type");
5047 expression = error_mark_node;
5051 /* But all the other expressions must be. */
5054 = cp_parser_constant_expression (parser,
5055 /*allow_non_constant=*/false,
5057 /* Look for the closing `]'. */
5058 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5060 /* Add this bound to the declarator. */
5061 declarator = make_array_declarator (declarator, expression);
5063 /* If the next token is not a `[', then there are no more
5065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5072 /* Parse a new-initializer.
5075 ( expression-list [opt] )
5077 Returns a representation of the expression-list. If there is no
5078 expression-list, VOID_ZERO_NODE is returned. */
5081 cp_parser_new_initializer (cp_parser* parser)
5083 tree expression_list;
5085 expression_list = (cp_parser_parenthesized_expression_list
5086 (parser, false, /*non_constant_p=*/NULL));
5087 if (!expression_list)
5088 expression_list = void_zero_node;
5090 return expression_list;
5093 /* Parse a delete-expression.
5096 :: [opt] delete cast-expression
5097 :: [opt] delete [ ] cast-expression
5099 Returns a representation of the expression. */
5102 cp_parser_delete_expression (cp_parser* parser)
5104 bool global_scope_p;
5108 /* Look for the optional `::' operator. */
5110 = (cp_parser_global_scope_opt (parser,
5111 /*current_scope_valid_p=*/false)
5113 /* Look for the `delete' keyword. */
5114 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5115 /* See if the array syntax is in use. */
5116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5118 /* Consume the `[' token. */
5119 cp_lexer_consume_token (parser->lexer);
5120 /* Look for the `]' token. */
5121 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5122 /* Remember that this is the `[]' construct. */
5128 /* Parse the cast-expression. */
5129 expression = cp_parser_simple_cast_expression (parser);
5131 /* A delete-expression may not appear in an integral constant
5133 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5134 return error_mark_node;
5136 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5139 /* Parse a cast-expression.
5143 ( type-id ) cast-expression
5145 Returns a representation of the expression. */
5148 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5150 /* If it's a `(', then we might be looking at a cast. */
5151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5153 tree type = NULL_TREE;
5154 tree expr = NULL_TREE;
5155 bool compound_literal_p;
5156 const char *saved_message;
5158 /* There's no way to know yet whether or not this is a cast.
5159 For example, `(int (3))' is a unary-expression, while `(int)
5160 3' is a cast. So, we resort to parsing tentatively. */
5161 cp_parser_parse_tentatively (parser);
5162 /* Types may not be defined in a cast. */
5163 saved_message = parser->type_definition_forbidden_message;
5164 parser->type_definition_forbidden_message
5165 = "types may not be defined in casts";
5166 /* Consume the `('. */
5167 cp_lexer_consume_token (parser->lexer);
5168 /* A very tricky bit is that `(struct S) { 3 }' is a
5169 compound-literal (which we permit in C++ as an extension).
5170 But, that construct is not a cast-expression -- it is a
5171 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5172 is legal; if the compound-literal were a cast-expression,
5173 you'd need an extra set of parentheses.) But, if we parse
5174 the type-id, and it happens to be a class-specifier, then we
5175 will commit to the parse at that point, because we cannot
5176 undo the action that is done when creating a new class. So,
5177 then we cannot back up and do a postfix-expression.
5179 Therefore, we scan ahead to the closing `)', and check to see
5180 if the token after the `)' is a `{'. If so, we are not
5181 looking at a cast-expression.
5183 Save tokens so that we can put them back. */
5184 cp_lexer_save_tokens (parser->lexer);
5185 /* Skip tokens until the next token is a closing parenthesis.
5186 If we find the closing `)', and the next token is a `{', then
5187 we are looking at a compound-literal. */
5189 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5190 /*consume_paren=*/true)
5191 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5192 /* Roll back the tokens we skipped. */
5193 cp_lexer_rollback_tokens (parser->lexer);
5194 /* If we were looking at a compound-literal, simulate an error
5195 so that the call to cp_parser_parse_definitely below will
5197 if (compound_literal_p)
5198 cp_parser_simulate_error (parser);
5201 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5202 parser->in_type_id_in_expr_p = true;
5203 /* Look for the type-id. */
5204 type = cp_parser_type_id (parser);
5205 /* Look for the closing `)'. */
5206 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5207 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5210 /* Restore the saved message. */
5211 parser->type_definition_forbidden_message = saved_message;
5213 /* If ok so far, parse the dependent expression. We cannot be
5214 sure it is a cast. Consider `(T ())'. It is a parenthesized
5215 ctor of T, but looks like a cast to function returning T
5216 without a dependent expression. */
5217 if (!cp_parser_error_occurred (parser))
5218 expr = cp_parser_simple_cast_expression (parser);
5220 if (cp_parser_parse_definitely (parser))
5222 /* Warn about old-style casts, if so requested. */
5223 if (warn_old_style_cast
5224 && !in_system_header
5225 && !VOID_TYPE_P (type)
5226 && current_lang_name != lang_name_c)
5227 warning ("use of old-style cast");
5229 /* Only type conversions to integral or enumeration types
5230 can be used in constant-expressions. */
5231 if (parser->integral_constant_expression_p
5232 && !dependent_type_p (type)
5233 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5234 && (cp_parser_non_integral_constant_expression
5236 "a cast to a type other than an integral or "
5237 "enumeration type")))
5238 return error_mark_node;
5240 /* Perform the cast. */
5241 expr = build_c_cast (type, expr);
5246 /* If we get here, then it's not a cast, so it must be a
5247 unary-expression. */
5248 return cp_parser_unary_expression (parser, address_p);
5251 /* Parse a pm-expression.
5255 pm-expression .* cast-expression
5256 pm-expression ->* cast-expression
5258 Returns a representation of the expression. */
5261 cp_parser_pm_expression (cp_parser* parser)
5263 static const cp_parser_token_tree_map map = {
5264 { CPP_DEREF_STAR, MEMBER_REF },
5265 { CPP_DOT_STAR, DOTSTAR_EXPR },
5266 { CPP_EOF, ERROR_MARK }
5269 return cp_parser_binary_expression (parser, map,
5270 cp_parser_simple_cast_expression);
5273 /* Parse a multiplicative-expression.
5275 multiplicative-expression:
5277 multiplicative-expression * pm-expression
5278 multiplicative-expression / pm-expression
5279 multiplicative-expression % pm-expression
5281 Returns a representation of the expression. */
5284 cp_parser_multiplicative_expression (cp_parser* parser)
5286 static const cp_parser_token_tree_map map = {
5287 { CPP_MULT, MULT_EXPR },
5288 { CPP_DIV, TRUNC_DIV_EXPR },
5289 { CPP_MOD, TRUNC_MOD_EXPR },
5290 { CPP_EOF, ERROR_MARK }
5293 return cp_parser_binary_expression (parser,
5295 cp_parser_pm_expression);
5298 /* Parse an additive-expression.
5300 additive-expression:
5301 multiplicative-expression
5302 additive-expression + multiplicative-expression
5303 additive-expression - multiplicative-expression
5305 Returns a representation of the expression. */
5308 cp_parser_additive_expression (cp_parser* parser)
5310 static const cp_parser_token_tree_map map = {
5311 { CPP_PLUS, PLUS_EXPR },
5312 { CPP_MINUS, MINUS_EXPR },
5313 { CPP_EOF, ERROR_MARK }
5316 return cp_parser_binary_expression (parser,
5318 cp_parser_multiplicative_expression);
5321 /* Parse a shift-expression.
5325 shift-expression << additive-expression
5326 shift-expression >> additive-expression
5328 Returns a representation of the expression. */
5331 cp_parser_shift_expression (cp_parser* parser)
5333 static const cp_parser_token_tree_map map = {
5334 { CPP_LSHIFT, LSHIFT_EXPR },
5335 { CPP_RSHIFT, RSHIFT_EXPR },
5336 { CPP_EOF, ERROR_MARK }
5339 return cp_parser_binary_expression (parser,
5341 cp_parser_additive_expression);
5344 /* Parse a relational-expression.
5346 relational-expression:
5348 relational-expression < shift-expression
5349 relational-expression > shift-expression
5350 relational-expression <= shift-expression
5351 relational-expression >= shift-expression
5355 relational-expression:
5356 relational-expression <? shift-expression
5357 relational-expression >? shift-expression
5359 Returns a representation of the expression. */
5362 cp_parser_relational_expression (cp_parser* parser)
5364 static const cp_parser_token_tree_map map = {
5365 { CPP_LESS, LT_EXPR },
5366 { CPP_GREATER, GT_EXPR },
5367 { CPP_LESS_EQ, LE_EXPR },
5368 { CPP_GREATER_EQ, GE_EXPR },
5369 { CPP_MIN, MIN_EXPR },
5370 { CPP_MAX, MAX_EXPR },
5371 { CPP_EOF, ERROR_MARK }
5374 return cp_parser_binary_expression (parser,
5376 cp_parser_shift_expression);
5379 /* Parse an equality-expression.
5381 equality-expression:
5382 relational-expression
5383 equality-expression == relational-expression
5384 equality-expression != relational-expression
5386 Returns a representation of the expression. */
5389 cp_parser_equality_expression (cp_parser* parser)
5391 static const cp_parser_token_tree_map map = {
5392 { CPP_EQ_EQ, EQ_EXPR },
5393 { CPP_NOT_EQ, NE_EXPR },
5394 { CPP_EOF, ERROR_MARK }
5397 return cp_parser_binary_expression (parser,
5399 cp_parser_relational_expression);
5402 /* Parse an and-expression.
5406 and-expression & equality-expression
5408 Returns a representation of the expression. */
5411 cp_parser_and_expression (cp_parser* parser)
5413 static const cp_parser_token_tree_map map = {
5414 { CPP_AND, BIT_AND_EXPR },
5415 { CPP_EOF, ERROR_MARK }
5418 return cp_parser_binary_expression (parser,
5420 cp_parser_equality_expression);
5423 /* Parse an exclusive-or-expression.
5425 exclusive-or-expression:
5427 exclusive-or-expression ^ and-expression
5429 Returns a representation of the expression. */
5432 cp_parser_exclusive_or_expression (cp_parser* parser)
5434 static const cp_parser_token_tree_map map = {
5435 { CPP_XOR, BIT_XOR_EXPR },
5436 { CPP_EOF, ERROR_MARK }
5439 return cp_parser_binary_expression (parser,
5441 cp_parser_and_expression);
5445 /* Parse an inclusive-or-expression.
5447 inclusive-or-expression:
5448 exclusive-or-expression
5449 inclusive-or-expression | exclusive-or-expression
5451 Returns a representation of the expression. */
5454 cp_parser_inclusive_or_expression (cp_parser* parser)
5456 static const cp_parser_token_tree_map map = {
5457 { CPP_OR, BIT_IOR_EXPR },
5458 { CPP_EOF, ERROR_MARK }
5461 return cp_parser_binary_expression (parser,
5463 cp_parser_exclusive_or_expression);
5466 /* Parse a logical-and-expression.
5468 logical-and-expression:
5469 inclusive-or-expression
5470 logical-and-expression && inclusive-or-expression
5472 Returns a representation of the expression. */
5475 cp_parser_logical_and_expression (cp_parser* parser)
5477 static const cp_parser_token_tree_map map = {
5478 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5479 { CPP_EOF, ERROR_MARK }
5482 return cp_parser_binary_expression (parser,
5484 cp_parser_inclusive_or_expression);
5487 /* Parse a logical-or-expression.
5489 logical-or-expression:
5490 logical-and-expression
5491 logical-or-expression || logical-and-expression
5493 Returns a representation of the expression. */
5496 cp_parser_logical_or_expression (cp_parser* parser)
5498 static const cp_parser_token_tree_map map = {
5499 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5500 { CPP_EOF, ERROR_MARK }
5503 return cp_parser_binary_expression (parser,
5505 cp_parser_logical_and_expression);
5508 /* Parse the `? expression : assignment-expression' part of a
5509 conditional-expression. The LOGICAL_OR_EXPR is the
5510 logical-or-expression that started the conditional-expression.
5511 Returns a representation of the entire conditional-expression.
5513 This routine is used by cp_parser_assignment_expression.
5515 ? expression : assignment-expression
5519 ? : assignment-expression */
5522 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5525 tree assignment_expr;
5527 /* Consume the `?' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 if (cp_parser_allow_gnu_extensions_p (parser)
5530 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5531 /* Implicit true clause. */
5534 /* Parse the expression. */
5535 expr = cp_parser_expression (parser);
5537 /* The next token should be a `:'. */
5538 cp_parser_require (parser, CPP_COLON, "`:'");
5539 /* Parse the assignment-expression. */
5540 assignment_expr = cp_parser_assignment_expression (parser);
5542 /* Build the conditional-expression. */
5543 return build_x_conditional_expr (logical_or_expr,
5548 /* Parse an assignment-expression.
5550 assignment-expression:
5551 conditional-expression
5552 logical-or-expression assignment-operator assignment_expression
5555 Returns a representation for the expression. */
5558 cp_parser_assignment_expression (cp_parser* parser)
5562 /* If the next token is the `throw' keyword, then we're looking at
5563 a throw-expression. */
5564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5565 expr = cp_parser_throw_expression (parser);
5566 /* Otherwise, it must be that we are looking at a
5567 logical-or-expression. */
5570 /* Parse the logical-or-expression. */
5571 expr = cp_parser_logical_or_expression (parser);
5572 /* If the next token is a `?' then we're actually looking at a
5573 conditional-expression. */
5574 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5575 return cp_parser_question_colon_clause (parser, expr);
5578 enum tree_code assignment_operator;
5580 /* If it's an assignment-operator, we're using the second
5583 = cp_parser_assignment_operator_opt (parser);
5584 if (assignment_operator != ERROR_MARK)
5588 /* Parse the right-hand side of the assignment. */
5589 rhs = cp_parser_assignment_expression (parser);
5590 /* An assignment may not appear in a
5591 constant-expression. */
5592 if (cp_parser_non_integral_constant_expression (parser,
5594 return error_mark_node;
5595 /* Build the assignment expression. */
5596 expr = build_x_modify_expr (expr,
5597 assignment_operator,
5606 /* Parse an (optional) assignment-operator.
5608 assignment-operator: one of
5609 = *= /= %= += -= >>= <<= &= ^= |=
5613 assignment-operator: one of
5616 If the next token is an assignment operator, the corresponding tree
5617 code is returned, and the token is consumed. For example, for
5618 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5619 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5620 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5621 operator, ERROR_MARK is returned. */
5623 static enum tree_code
5624 cp_parser_assignment_operator_opt (cp_parser* parser)
5629 /* Peek at the next toen. */
5630 token = cp_lexer_peek_token (parser->lexer);
5632 switch (token->type)
5643 op = TRUNC_DIV_EXPR;
5647 op = TRUNC_MOD_EXPR;
5687 /* Nothing else is an assignment operator. */
5691 /* If it was an assignment operator, consume it. */
5692 if (op != ERROR_MARK)
5693 cp_lexer_consume_token (parser->lexer);
5698 /* Parse an expression.
5701 assignment-expression
5702 expression , assignment-expression
5704 Returns a representation of the expression. */
5707 cp_parser_expression (cp_parser* parser)
5709 tree expression = NULL_TREE;
5713 tree assignment_expression;
5715 /* Parse the next assignment-expression. */
5716 assignment_expression
5717 = cp_parser_assignment_expression (parser);
5718 /* If this is the first assignment-expression, we can just
5721 expression = assignment_expression;
5723 expression = build_x_compound_expr (expression,
5724 assignment_expression);
5725 /* If the next token is not a comma, then we are done with the
5727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5729 /* Consume the `,'. */
5730 cp_lexer_consume_token (parser->lexer);
5731 /* A comma operator cannot appear in a constant-expression. */
5732 if (cp_parser_non_integral_constant_expression (parser,
5733 "a comma operator"))
5734 expression = error_mark_node;
5740 /* Parse a constant-expression.
5742 constant-expression:
5743 conditional-expression
5745 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5746 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5747 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5748 is false, NON_CONSTANT_P should be NULL. */
5751 cp_parser_constant_expression (cp_parser* parser,
5752 bool allow_non_constant_p,
5753 bool *non_constant_p)
5755 bool saved_integral_constant_expression_p;
5756 bool saved_allow_non_integral_constant_expression_p;
5757 bool saved_non_integral_constant_expression_p;
5760 /* It might seem that we could simply parse the
5761 conditional-expression, and then check to see if it were
5762 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5763 one that the compiler can figure out is constant, possibly after
5764 doing some simplifications or optimizations. The standard has a
5765 precise definition of constant-expression, and we must honor
5766 that, even though it is somewhat more restrictive.
5772 is not a legal declaration, because `(2, 3)' is not a
5773 constant-expression. The `,' operator is forbidden in a
5774 constant-expression. However, GCC's constant-folding machinery
5775 will fold this operation to an INTEGER_CST for `3'. */
5777 /* Save the old settings. */
5778 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5779 saved_allow_non_integral_constant_expression_p
5780 = parser->allow_non_integral_constant_expression_p;
5781 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5782 /* We are now parsing a constant-expression. */
5783 parser->integral_constant_expression_p = true;
5784 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5785 parser->non_integral_constant_expression_p = false;
5786 /* Although the grammar says "conditional-expression", we parse an
5787 "assignment-expression", which also permits "throw-expression"
5788 and the use of assignment operators. In the case that
5789 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5790 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5791 actually essential that we look for an assignment-expression.
5792 For example, cp_parser_initializer_clauses uses this function to
5793 determine whether a particular assignment-expression is in fact
5795 expression = cp_parser_assignment_expression (parser);
5796 /* Restore the old settings. */
5797 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5798 parser->allow_non_integral_constant_expression_p
5799 = saved_allow_non_integral_constant_expression_p;
5800 if (allow_non_constant_p)
5801 *non_constant_p = parser->non_integral_constant_expression_p;
5802 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5807 /* Parse __builtin_offsetof.
5809 offsetof-expression:
5810 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5812 offsetof-member-designator:
5814 | offsetof-member-designator "." id-expression
5815 | offsetof-member-designator "[" expression "]"
5819 cp_parser_builtin_offsetof (cp_parser *parser)
5821 int save_ice_p, save_non_ice_p;
5825 /* We're about to accept non-integral-constant things, but will
5826 definitely yield an integral constant expression. Save and
5827 restore these values around our local parsing. */
5828 save_ice_p = parser->integral_constant_expression_p;
5829 save_non_ice_p = parser->non_integral_constant_expression_p;
5831 /* Consume the "__builtin_offsetof" token. */
5832 cp_lexer_consume_token (parser->lexer);
5833 /* Consume the opening `('. */
5834 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5835 /* Parse the type-id. */
5836 type = cp_parser_type_id (parser);
5837 /* Look for the `,'. */
5838 cp_parser_require (parser, CPP_COMMA, "`,'");
5840 /* Build the (type *)null that begins the traditional offsetof macro. */
5841 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5843 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5844 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5848 cp_token *token = cp_lexer_peek_token (parser->lexer);
5849 switch (token->type)
5851 case CPP_OPEN_SQUARE:
5852 /* offsetof-member-designator "[" expression "]" */
5853 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5857 /* offsetof-member-designator "." identifier */
5858 cp_lexer_consume_token (parser->lexer);
5859 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5863 case CPP_CLOSE_PAREN:
5864 /* Consume the ")" token. */
5865 cp_lexer_consume_token (parser->lexer);
5869 /* Error. We know the following require will fail, but
5870 that gives the proper error message. */
5871 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5872 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5873 expr = error_mark_node;
5879 /* If we're processing a template, we can't finish the semantics yet.
5880 Otherwise we can fold the entire expression now. */
5881 if (processing_template_decl)
5882 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5884 expr = fold_offsetof (expr);
5887 parser->integral_constant_expression_p = save_ice_p;
5888 parser->non_integral_constant_expression_p = save_non_ice_p;
5893 /* Statements [gram.stmt.stmt] */
5895 /* Parse a statement.
5899 expression-statement
5904 declaration-statement
5908 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5912 location_t statement_location;
5914 /* There is no statement yet. */
5915 statement = NULL_TREE;
5916 /* Peek at the next token. */
5917 token = cp_lexer_peek_token (parser->lexer);
5918 /* Remember the location of the first token in the statement. */
5919 statement_location = token->location;
5920 /* If this is a keyword, then that will often determine what kind of
5921 statement we have. */
5922 if (token->type == CPP_KEYWORD)
5924 enum rid keyword = token->keyword;
5930 statement = cp_parser_labeled_statement (parser,
5936 statement = cp_parser_selection_statement (parser);
5942 statement = cp_parser_iteration_statement (parser);
5949 statement = cp_parser_jump_statement (parser);
5953 statement = cp_parser_try_block (parser);
5957 /* It might be a keyword like `int' that can start a
5958 declaration-statement. */
5962 else if (token->type == CPP_NAME)
5964 /* If the next token is a `:', then we are looking at a
5965 labeled-statement. */
5966 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5967 if (token->type == CPP_COLON)
5968 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5970 /* Anything that starts with a `{' must be a compound-statement. */
5971 else if (token->type == CPP_OPEN_BRACE)
5972 statement = cp_parser_compound_statement (parser, NULL, false);
5974 /* Everything else must be a declaration-statement or an
5975 expression-statement. Try for the declaration-statement
5976 first, unless we are looking at a `;', in which case we know that
5977 we have an expression-statement. */
5980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5982 cp_parser_parse_tentatively (parser);
5983 /* Try to parse the declaration-statement. */
5984 cp_parser_declaration_statement (parser);
5985 /* If that worked, we're done. */
5986 if (cp_parser_parse_definitely (parser))
5989 /* Look for an expression-statement instead. */
5990 statement = cp_parser_expression_statement (parser, in_statement_expr);
5993 /* Set the line number for the statement. */
5994 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5995 SET_EXPR_LOCATION (statement, statement_location);
5998 /* Parse a labeled-statement.
6001 identifier : statement
6002 case constant-expression : statement
6008 case constant-expression ... constant-expression : statement
6010 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6011 For an ordinary label, returns a LABEL_EXPR. */
6014 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6017 tree statement = error_mark_node;
6019 /* The next token should be an identifier. */
6020 token = cp_lexer_peek_token (parser->lexer);
6021 if (token->type != CPP_NAME
6022 && token->type != CPP_KEYWORD)
6024 cp_parser_error (parser, "expected labeled-statement");
6025 return error_mark_node;
6028 switch (token->keyword)
6035 /* Consume the `case' token. */
6036 cp_lexer_consume_token (parser->lexer);
6037 /* Parse the constant-expression. */
6038 expr = cp_parser_constant_expression (parser,
6039 /*allow_non_constant_p=*/false,
6042 ellipsis = cp_lexer_peek_token (parser->lexer);
6043 if (ellipsis->type == CPP_ELLIPSIS)
6045 /* Consume the `...' token. */
6046 cp_lexer_consume_token (parser->lexer);
6048 cp_parser_constant_expression (parser,
6049 /*allow_non_constant_p=*/false,
6051 /* We don't need to emit warnings here, as the common code
6052 will do this for us. */
6055 expr_hi = NULL_TREE;
6057 if (!parser->in_switch_statement_p)
6058 error ("case label `%E' not within a switch statement", expr);
6060 statement = finish_case_label (expr, expr_hi);
6065 /* Consume the `default' token. */
6066 cp_lexer_consume_token (parser->lexer);
6067 if (!parser->in_switch_statement_p)
6068 error ("case label not within a switch statement");
6070 statement = finish_case_label (NULL_TREE, NULL_TREE);
6074 /* Anything else must be an ordinary label. */
6075 statement = finish_label_stmt (cp_parser_identifier (parser));
6079 /* Require the `:' token. */
6080 cp_parser_require (parser, CPP_COLON, "`:'");
6081 /* Parse the labeled statement. */
6082 cp_parser_statement (parser, in_statement_expr);
6084 /* Return the label, in the case of a `case' or `default' label. */
6088 /* Parse an expression-statement.
6090 expression-statement:
6093 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6094 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6095 indicates whether this expression-statement is part of an
6096 expression statement. */
6099 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6101 tree statement = NULL_TREE;
6103 /* If the next token is a ';', then there is no expression
6105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6106 statement = cp_parser_expression (parser);
6108 /* Consume the final `;'. */
6109 cp_parser_consume_semicolon_at_end_of_statement (parser);
6111 if (in_statement_expr
6112 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6114 /* This is the final expression statement of a statement
6116 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6119 statement = finish_expr_stmt (statement);
6126 /* Parse a compound-statement.
6129 { statement-seq [opt] }
6131 Returns a tree representing the statement. */
6134 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6139 /* Consume the `{'. */
6140 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6141 return error_mark_node;
6142 /* Begin the compound-statement. */
6143 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6144 /* Parse an (optional) statement-seq. */
6145 cp_parser_statement_seq_opt (parser, in_statement_expr);
6146 /* Finish the compound-statement. */
6147 finish_compound_stmt (compound_stmt);
6148 /* Consume the `}'. */
6149 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6151 return compound_stmt;
6154 /* Parse an (optional) statement-seq.
6158 statement-seq [opt] statement */
6161 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6163 /* Scan statements until there aren't any more. */
6166 /* If we're looking at a `}', then we've run out of statements. */
6167 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6168 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6171 /* Parse the statement. */
6172 cp_parser_statement (parser, in_statement_expr);
6176 /* Parse a selection-statement.
6178 selection-statement:
6179 if ( condition ) statement
6180 if ( condition ) statement else statement
6181 switch ( condition ) statement
6183 Returns the new IF_STMT or SWITCH_STMT. */
6186 cp_parser_selection_statement (cp_parser* parser)
6191 /* Peek at the next token. */
6192 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6194 /* See what kind of keyword it is. */
6195 keyword = token->keyword;
6204 /* Look for the `('. */
6205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6207 cp_parser_skip_to_end_of_statement (parser);
6208 return error_mark_node;
6211 /* Begin the selection-statement. */
6212 if (keyword == RID_IF)
6213 statement = begin_if_stmt ();
6215 statement = begin_switch_stmt ();
6217 /* Parse the condition. */
6218 condition = cp_parser_condition (parser);
6219 /* Look for the `)'. */
6220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6221 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6222 /*consume_paren=*/true);
6224 if (keyword == RID_IF)
6226 /* Add the condition. */
6227 finish_if_stmt_cond (condition, statement);
6229 /* Parse the then-clause. */
6230 cp_parser_implicitly_scoped_statement (parser);
6231 finish_then_clause (statement);
6233 /* If the next token is `else', parse the else-clause. */
6234 if (cp_lexer_next_token_is_keyword (parser->lexer,
6237 /* Consume the `else' keyword. */
6238 cp_lexer_consume_token (parser->lexer);
6239 begin_else_clause (statement);
6240 /* Parse the else-clause. */
6241 cp_parser_implicitly_scoped_statement (parser);
6242 finish_else_clause (statement);
6245 /* Now we're all done with the if-statement. */
6246 finish_if_stmt (statement);
6250 bool in_switch_statement_p;
6252 /* Add the condition. */
6253 finish_switch_cond (condition, statement);
6255 /* Parse the body of the switch-statement. */
6256 in_switch_statement_p = parser->in_switch_statement_p;
6257 parser->in_switch_statement_p = true;
6258 cp_parser_implicitly_scoped_statement (parser);
6259 parser->in_switch_statement_p = in_switch_statement_p;
6261 /* Now we're all done with the switch-statement. */
6262 finish_switch_stmt (statement);
6270 cp_parser_error (parser, "expected selection-statement");
6271 return error_mark_node;
6275 /* Parse a condition.
6279 type-specifier-seq declarator = assignment-expression
6284 type-specifier-seq declarator asm-specification [opt]
6285 attributes [opt] = assignment-expression
6287 Returns the expression that should be tested. */
6290 cp_parser_condition (cp_parser* parser)
6292 cp_decl_specifier_seq type_specifiers;
6293 const char *saved_message;
6295 /* Try the declaration first. */
6296 cp_parser_parse_tentatively (parser);
6297 /* New types are not allowed in the type-specifier-seq for a
6299 saved_message = parser->type_definition_forbidden_message;
6300 parser->type_definition_forbidden_message
6301 = "types may not be defined in conditions";
6302 /* Parse the type-specifier-seq. */
6303 cp_parser_type_specifier_seq (parser, &type_specifiers);
6304 /* Restore the saved message. */
6305 parser->type_definition_forbidden_message = saved_message;
6306 /* If all is well, we might be looking at a declaration. */
6307 if (!cp_parser_error_occurred (parser))
6310 tree asm_specification;
6312 cp_declarator *declarator;
6313 tree initializer = NULL_TREE;
6315 /* Parse the declarator. */
6316 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6317 /*ctor_dtor_or_conv_p=*/NULL,
6318 /*parenthesized_p=*/NULL);
6319 /* Parse the attributes. */
6320 attributes = cp_parser_attributes_opt (parser);
6321 /* Parse the asm-specification. */
6322 asm_specification = cp_parser_asm_specification_opt (parser);
6323 /* If the next token is not an `=', then we might still be
6324 looking at an expression. For example:
6328 looks like a decl-specifier-seq and a declarator -- but then
6329 there is no `=', so this is an expression. */
6330 cp_parser_require (parser, CPP_EQ, "`='");
6331 /* If we did see an `=', then we are looking at a declaration
6333 if (cp_parser_parse_definitely (parser))
6337 /* Create the declaration. */
6338 decl = start_decl (declarator, &type_specifiers,
6339 /*initialized_p=*/true,
6340 attributes, /*prefix_attributes=*/NULL_TREE,
6342 /* Parse the assignment-expression. */
6343 initializer = cp_parser_assignment_expression (parser);
6345 /* Process the initializer. */
6346 cp_finish_decl (decl,
6349 LOOKUP_ONLYCONVERTING);
6351 pop_scope (DECL_CONTEXT (decl));
6353 return convert_from_reference (decl);
6356 /* If we didn't even get past the declarator successfully, we are
6357 definitely not looking at a declaration. */
6359 cp_parser_abort_tentative_parse (parser);
6361 /* Otherwise, we are looking at an expression. */
6362 return cp_parser_expression (parser);
6365 /* Parse an iteration-statement.
6367 iteration-statement:
6368 while ( condition ) statement
6369 do statement while ( expression ) ;
6370 for ( for-init-statement condition [opt] ; expression [opt] )
6373 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6376 cp_parser_iteration_statement (cp_parser* parser)
6381 bool in_iteration_statement_p;
6384 /* Peek at the next token. */
6385 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6387 return error_mark_node;
6389 /* Remember whether or not we are already within an iteration
6391 in_iteration_statement_p = parser->in_iteration_statement_p;
6393 /* See what kind of keyword it is. */
6394 keyword = token->keyword;
6401 /* Begin the while-statement. */
6402 statement = begin_while_stmt ();
6403 /* Look for the `('. */
6404 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6405 /* Parse the condition. */
6406 condition = cp_parser_condition (parser);
6407 finish_while_stmt_cond (condition, statement);
6408 /* Look for the `)'. */
6409 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6410 /* Parse the dependent statement. */
6411 parser->in_iteration_statement_p = true;
6412 cp_parser_already_scoped_statement (parser);
6413 parser->in_iteration_statement_p = in_iteration_statement_p;
6414 /* We're done with the while-statement. */
6415 finish_while_stmt (statement);
6423 /* Begin the do-statement. */
6424 statement = begin_do_stmt ();
6425 /* Parse the body of the do-statement. */
6426 parser->in_iteration_statement_p = true;
6427 cp_parser_implicitly_scoped_statement (parser);
6428 parser->in_iteration_statement_p = in_iteration_statement_p;
6429 finish_do_body (statement);
6430 /* Look for the `while' keyword. */
6431 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6432 /* Look for the `('. */
6433 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6434 /* Parse the expression. */
6435 expression = cp_parser_expression (parser);
6436 /* We're done with the do-statement. */
6437 finish_do_stmt (expression, statement);
6438 /* Look for the `)'. */
6439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6440 /* Look for the `;'. */
6441 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6447 tree condition = NULL_TREE;
6448 tree expression = NULL_TREE;
6450 /* Begin the for-statement. */
6451 statement = begin_for_stmt ();
6452 /* Look for the `('. */
6453 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6454 /* Parse the initialization. */
6455 cp_parser_for_init_statement (parser);
6456 finish_for_init_stmt (statement);
6458 /* If there's a condition, process it. */
6459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6460 condition = cp_parser_condition (parser);
6461 finish_for_cond (condition, statement);
6462 /* Look for the `;'. */
6463 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6465 /* If there's an expression, process it. */
6466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6467 expression = cp_parser_expression (parser);
6468 finish_for_expr (expression, statement);
6469 /* Look for the `)'. */
6470 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6472 /* Parse the body of the for-statement. */
6473 parser->in_iteration_statement_p = true;
6474 cp_parser_already_scoped_statement (parser);
6475 parser->in_iteration_statement_p = in_iteration_statement_p;
6477 /* We're done with the for-statement. */
6478 finish_for_stmt (statement);
6483 cp_parser_error (parser, "expected iteration-statement");
6484 statement = error_mark_node;
6491 /* Parse a for-init-statement.
6494 expression-statement
6495 simple-declaration */
6498 cp_parser_for_init_statement (cp_parser* parser)
6500 /* If the next token is a `;', then we have an empty
6501 expression-statement. Grammatically, this is also a
6502 simple-declaration, but an invalid one, because it does not
6503 declare anything. Therefore, if we did not handle this case
6504 specially, we would issue an error message about an invalid
6506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6508 /* We're going to speculatively look for a declaration, falling back
6509 to an expression, if necessary. */
6510 cp_parser_parse_tentatively (parser);
6511 /* Parse the declaration. */
6512 cp_parser_simple_declaration (parser,
6513 /*function_definition_allowed_p=*/false);
6514 /* If the tentative parse failed, then we shall need to look for an
6515 expression-statement. */
6516 if (cp_parser_parse_definitely (parser))
6520 cp_parser_expression_statement (parser, false);
6523 /* Parse a jump-statement.
6528 return expression [opt] ;
6536 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6539 cp_parser_jump_statement (cp_parser* parser)
6541 tree statement = error_mark_node;
6545 /* Peek at the next token. */
6546 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6548 return error_mark_node;
6550 /* See what kind of keyword it is. */
6551 keyword = token->keyword;
6555 if (!parser->in_switch_statement_p
6556 && !parser->in_iteration_statement_p)
6558 error ("break statement not within loop or switch");
6559 statement = error_mark_node;
6562 statement = finish_break_stmt ();
6563 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6567 if (!parser->in_iteration_statement_p)
6569 error ("continue statement not within a loop");
6570 statement = error_mark_node;
6573 statement = finish_continue_stmt ();
6574 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6581 /* If the next token is a `;', then there is no
6583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6584 expr = cp_parser_expression (parser);
6587 /* Build the return-statement. */
6588 statement = finish_return_stmt (expr);
6589 /* Look for the final `;'. */
6590 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595 /* Create the goto-statement. */
6596 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6598 /* Issue a warning about this use of a GNU extension. */
6600 pedwarn ("ISO C++ forbids computed gotos");
6601 /* Consume the '*' token. */
6602 cp_lexer_consume_token (parser->lexer);
6603 /* Parse the dependent expression. */
6604 finish_goto_stmt (cp_parser_expression (parser));
6607 finish_goto_stmt (cp_parser_identifier (parser));
6608 /* Look for the final `;'. */
6609 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6613 cp_parser_error (parser, "expected jump-statement");
6620 /* Parse a declaration-statement.
6622 declaration-statement:
6623 block-declaration */
6626 cp_parser_declaration_statement (cp_parser* parser)
6630 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6631 p = obstack_alloc (&declarator_obstack, 0);
6633 /* Parse the block-declaration. */
6634 cp_parser_block_declaration (parser, /*statement_p=*/true);
6636 /* Free any declarators allocated. */
6637 obstack_free (&declarator_obstack, p);
6639 /* Finish off the statement. */
6643 /* Some dependent statements (like `if (cond) statement'), are
6644 implicitly in their own scope. In other words, if the statement is
6645 a single statement (as opposed to a compound-statement), it is
6646 none-the-less treated as if it were enclosed in braces. Any
6647 declarations appearing in the dependent statement are out of scope
6648 after control passes that point. This function parses a statement,
6649 but ensures that is in its own scope, even if it is not a
6652 Returns the new statement. */
6655 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6659 /* If the token is not a `{', then we must take special action. */
6660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6662 /* Create a compound-statement. */
6663 statement = begin_compound_stmt (0);
6664 /* Parse the dependent-statement. */
6665 cp_parser_statement (parser, false);
6666 /* Finish the dummy compound-statement. */
6667 finish_compound_stmt (statement);
6669 /* Otherwise, we simply parse the statement directly. */
6671 statement = cp_parser_compound_statement (parser, NULL, false);
6673 /* Return the statement. */
6677 /* For some dependent statements (like `while (cond) statement'), we
6678 have already created a scope. Therefore, even if the dependent
6679 statement is a compound-statement, we do not want to create another
6683 cp_parser_already_scoped_statement (cp_parser* parser)
6685 /* If the token is a `{', then we must take special action. */
6686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6687 cp_parser_statement (parser, false);
6690 /* Avoid calling cp_parser_compound_statement, so that we
6691 don't create a new scope. Do everything else by hand. */
6692 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6693 cp_parser_statement_seq_opt (parser, false);
6694 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6698 /* Declarations [gram.dcl.dcl] */
6700 /* Parse an optional declaration-sequence.
6704 declaration-seq declaration */
6707 cp_parser_declaration_seq_opt (cp_parser* parser)
6713 token = cp_lexer_peek_token (parser->lexer);
6715 if (token->type == CPP_CLOSE_BRACE
6716 || token->type == CPP_EOF)
6719 if (token->type == CPP_SEMICOLON)
6721 /* A declaration consisting of a single semicolon is
6722 invalid. Allow it unless we're being pedantic. */
6723 if (pedantic && !in_system_header)
6724 pedwarn ("extra `;'");
6725 cp_lexer_consume_token (parser->lexer);
6729 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6730 parser to enter or exit implicit `extern "C"' blocks. */
6731 while (pending_lang_change > 0)
6733 push_lang_context (lang_name_c);
6734 --pending_lang_change;
6736 while (pending_lang_change < 0)
6738 pop_lang_context ();
6739 ++pending_lang_change;
6742 /* Parse the declaration itself. */
6743 cp_parser_declaration (parser);
6747 /* Parse a declaration.
6752 template-declaration
6753 explicit-instantiation
6754 explicit-specialization
6755 linkage-specification
6756 namespace-definition
6761 __extension__ declaration */
6764 cp_parser_declaration (cp_parser* parser)
6771 /* Set this here since we can be called after
6772 pushing the linkage specification. */
6773 c_lex_string_translate = 1;
6775 /* Check for the `__extension__' keyword. */
6776 if (cp_parser_extension_opt (parser, &saved_pedantic))
6778 /* Parse the qualified declaration. */
6779 cp_parser_declaration (parser);
6780 /* Restore the PEDANTIC flag. */
6781 pedantic = saved_pedantic;
6786 /* Try to figure out what kind of declaration is present. */
6787 token1 = *cp_lexer_peek_token (parser->lexer);
6789 /* Don't translate the CPP_STRING in extern "C". */
6790 if (token1.keyword == RID_EXTERN)
6791 c_lex_string_translate = 0;
6793 if (token1.type != CPP_EOF)
6794 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6796 c_lex_string_translate = 1;
6798 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6799 p = obstack_alloc (&declarator_obstack, 0);
6801 /* If the next token is `extern' and the following token is a string
6802 literal, then we have a linkage specification. */
6803 if (token1.keyword == RID_EXTERN
6804 && cp_parser_is_string_literal (&token2))
6805 cp_parser_linkage_specification (parser);
6806 /* If the next token is `template', then we have either a template
6807 declaration, an explicit instantiation, or an explicit
6809 else if (token1.keyword == RID_TEMPLATE)
6811 /* `template <>' indicates a template specialization. */
6812 if (token2.type == CPP_LESS
6813 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6814 cp_parser_explicit_specialization (parser);
6815 /* `template <' indicates a template declaration. */
6816 else if (token2.type == CPP_LESS)
6817 cp_parser_template_declaration (parser, /*member_p=*/false);
6818 /* Anything else must be an explicit instantiation. */
6820 cp_parser_explicit_instantiation (parser);
6822 /* If the next token is `export', then we have a template
6824 else if (token1.keyword == RID_EXPORT)
6825 cp_parser_template_declaration (parser, /*member_p=*/false);
6826 /* If the next token is `extern', 'static' or 'inline' and the one
6827 after that is `template', we have a GNU extended explicit
6828 instantiation directive. */
6829 else if (cp_parser_allow_gnu_extensions_p (parser)
6830 && (token1.keyword == RID_EXTERN
6831 || token1.keyword == RID_STATIC
6832 || token1.keyword == RID_INLINE)
6833 && token2.keyword == RID_TEMPLATE)
6834 cp_parser_explicit_instantiation (parser);
6835 /* If the next token is `namespace', check for a named or unnamed
6836 namespace definition. */
6837 else if (token1.keyword == RID_NAMESPACE
6838 && (/* A named namespace definition. */
6839 (token2.type == CPP_NAME
6840 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6842 /* An unnamed namespace definition. */
6843 || token2.type == CPP_OPEN_BRACE))
6844 cp_parser_namespace_definition (parser);
6845 /* We must have either a block declaration or a function
6848 /* Try to parse a block-declaration, or a function-definition. */
6849 cp_parser_block_declaration (parser, /*statement_p=*/false);
6851 /* Free any declarators allocated. */
6852 obstack_free (&declarator_obstack, p);
6855 /* Parse a block-declaration.
6860 namespace-alias-definition
6867 __extension__ block-declaration
6870 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6871 part of a declaration-statement. */
6874 cp_parser_block_declaration (cp_parser *parser,
6880 /* Check for the `__extension__' keyword. */
6881 if (cp_parser_extension_opt (parser, &saved_pedantic))
6883 /* Parse the qualified declaration. */
6884 cp_parser_block_declaration (parser, statement_p);
6885 /* Restore the PEDANTIC flag. */
6886 pedantic = saved_pedantic;
6891 /* Peek at the next token to figure out which kind of declaration is
6893 token1 = cp_lexer_peek_token (parser->lexer);
6895 /* If the next keyword is `asm', we have an asm-definition. */
6896 if (token1->keyword == RID_ASM)
6899 cp_parser_commit_to_tentative_parse (parser);
6900 cp_parser_asm_definition (parser);
6902 /* If the next keyword is `namespace', we have a
6903 namespace-alias-definition. */
6904 else if (token1->keyword == RID_NAMESPACE)
6905 cp_parser_namespace_alias_definition (parser);
6906 /* If the next keyword is `using', we have either a
6907 using-declaration or a using-directive. */
6908 else if (token1->keyword == RID_USING)
6913 cp_parser_commit_to_tentative_parse (parser);
6914 /* If the token after `using' is `namespace', then we have a
6916 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6917 if (token2->keyword == RID_NAMESPACE)
6918 cp_parser_using_directive (parser);
6919 /* Otherwise, it's a using-declaration. */
6921 cp_parser_using_declaration (parser);
6923 /* If the next keyword is `__label__' we have a label declaration. */
6924 else if (token1->keyword == RID_LABEL)
6927 cp_parser_commit_to_tentative_parse (parser);
6928 cp_parser_label_declaration (parser);
6930 /* Anything else must be a simple-declaration. */
6932 cp_parser_simple_declaration (parser, !statement_p);
6935 /* Parse a simple-declaration.
6938 decl-specifier-seq [opt] init-declarator-list [opt] ;
6940 init-declarator-list:
6942 init-declarator-list , init-declarator
6944 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6945 function-definition as a simple-declaration. */
6948 cp_parser_simple_declaration (cp_parser* parser,
6949 bool function_definition_allowed_p)
6951 cp_decl_specifier_seq decl_specifiers;
6952 int declares_class_or_enum;
6953 bool saw_declarator;
6955 /* Defer access checks until we know what is being declared; the
6956 checks for names appearing in the decl-specifier-seq should be
6957 done as if we were in the scope of the thing being declared. */
6958 push_deferring_access_checks (dk_deferred);
6960 /* Parse the decl-specifier-seq. We have to keep track of whether
6961 or not the decl-specifier-seq declares a named class or
6962 enumeration type, since that is the only case in which the
6963 init-declarator-list is allowed to be empty.
6967 In a simple-declaration, the optional init-declarator-list can be
6968 omitted only when declaring a class or enumeration, that is when
6969 the decl-specifier-seq contains either a class-specifier, an
6970 elaborated-type-specifier, or an enum-specifier. */
6971 cp_parser_decl_specifier_seq (parser,
6972 CP_PARSER_FLAGS_OPTIONAL,
6974 &declares_class_or_enum);
6975 /* We no longer need to defer access checks. */
6976 stop_deferring_access_checks ();
6978 /* In a block scope, a valid declaration must always have a
6979 decl-specifier-seq. By not trying to parse declarators, we can
6980 resolve the declaration/expression ambiguity more quickly. */
6981 if (!function_definition_allowed_p
6982 && !decl_specifiers.any_specifiers_p)
6984 cp_parser_error (parser, "expected declaration");
6988 /* If the next two tokens are both identifiers, the code is
6989 erroneous. The usual cause of this situation is code like:
6993 where "T" should name a type -- but does not. */
6994 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6996 /* If parsing tentatively, we should commit; we really are
6997 looking at a declaration. */
6998 cp_parser_commit_to_tentative_parse (parser);
7003 /* Keep going until we hit the `;' at the end of the simple
7005 saw_declarator = false;
7006 while (cp_lexer_next_token_is_not (parser->lexer,
7010 bool function_definition_p;
7013 saw_declarator = true;
7014 /* Parse the init-declarator. */
7015 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7016 function_definition_allowed_p,
7018 declares_class_or_enum,
7019 &function_definition_p);
7020 /* If an error occurred while parsing tentatively, exit quickly.
7021 (That usually happens when in the body of a function; each
7022 statement is treated as a declaration-statement until proven
7024 if (cp_parser_error_occurred (parser))
7026 /* Handle function definitions specially. */
7027 if (function_definition_p)
7029 /* If the next token is a `,', then we are probably
7030 processing something like:
7034 which is erroneous. */
7035 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7036 error ("mixing declarations and function-definitions is forbidden");
7037 /* Otherwise, we're done with the list of declarators. */
7040 pop_deferring_access_checks ();
7044 /* The next token should be either a `,' or a `;'. */
7045 token = cp_lexer_peek_token (parser->lexer);
7046 /* If it's a `,', there are more declarators to come. */
7047 if (token->type == CPP_COMMA)
7048 cp_lexer_consume_token (parser->lexer);
7049 /* If it's a `;', we are done. */
7050 else if (token->type == CPP_SEMICOLON)
7052 /* Anything else is an error. */
7055 cp_parser_error (parser, "expected `,' or `;'");
7056 /* Skip tokens until we reach the end of the statement. */
7057 cp_parser_skip_to_end_of_statement (parser);
7058 /* If the next token is now a `;', consume it. */
7059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7060 cp_lexer_consume_token (parser->lexer);
7063 /* After the first time around, a function-definition is not
7064 allowed -- even if it was OK at first. For example:
7069 function_definition_allowed_p = false;
7072 /* Issue an error message if no declarators are present, and the
7073 decl-specifier-seq does not itself declare a class or
7075 if (!saw_declarator)
7077 if (cp_parser_declares_only_class_p (parser))
7078 shadow_tag (&decl_specifiers);
7079 /* Perform any deferred access checks. */
7080 perform_deferred_access_checks ();
7083 /* Consume the `;'. */
7084 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7087 pop_deferring_access_checks ();
7090 /* Parse a decl-specifier-seq.
7093 decl-specifier-seq [opt] decl-specifier
7096 storage-class-specifier
7107 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7109 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7110 appears, and the entity that will be a friend is not going to be a
7111 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
7112 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7113 friendship is granted might not be a class.
7115 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7118 1: one of the decl-specifiers is an elaborated-type-specifier
7119 (i.e., a type declaration)
7120 2: one of the decl-specifiers is an enum-specifier or a
7121 class-specifier (i.e., a type definition)
7126 cp_parser_decl_specifier_seq (cp_parser* parser,
7127 cp_parser_flags flags,
7128 cp_decl_specifier_seq *decl_specs,
7129 int* declares_class_or_enum)
7131 bool constructor_possible_p = !parser->in_declarator_p;
7133 /* Clear DECL_SPECS. */
7134 clear_decl_specs (decl_specs);
7136 /* Assume no class or enumeration type is declared. */
7137 *declares_class_or_enum = 0;
7139 /* Keep reading specifiers until there are no more to read. */
7143 bool found_decl_spec;
7146 /* Peek at the next token. */
7147 token = cp_lexer_peek_token (parser->lexer);
7148 /* Handle attributes. */
7149 if (token->keyword == RID_ATTRIBUTE)
7151 /* Parse the attributes. */
7152 decl_specs->attributes
7153 = chainon (decl_specs->attributes,
7154 cp_parser_attributes_opt (parser));
7157 /* Assume we will find a decl-specifier keyword. */
7158 found_decl_spec = true;
7159 /* If the next token is an appropriate keyword, we can simply
7160 add it to the list. */
7161 switch (token->keyword)
7166 if (decl_specs->specs[(int) ds_friend]++)
7167 error ("duplicate `friend'");
7168 /* Consume the token. */
7169 cp_lexer_consume_token (parser->lexer);
7172 /* function-specifier:
7179 cp_parser_function_specifier_opt (parser, decl_specs);
7185 ++decl_specs->specs[(int) ds_typedef];
7186 /* Consume the token. */
7187 cp_lexer_consume_token (parser->lexer);
7188 /* A constructor declarator cannot appear in a typedef. */
7189 constructor_possible_p = false;
7190 /* The "typedef" keyword can only occur in a declaration; we
7191 may as well commit at this point. */
7192 cp_parser_commit_to_tentative_parse (parser);
7195 /* storage-class-specifier:
7205 /* Consume the token. */
7206 cp_lexer_consume_token (parser->lexer);
7207 cp_parser_set_storage_class (decl_specs, sc_auto);
7210 /* Consume the token. */
7211 cp_lexer_consume_token (parser->lexer);
7212 cp_parser_set_storage_class (decl_specs, sc_register);
7215 /* Consume the token. */
7216 cp_lexer_consume_token (parser->lexer);
7217 if (decl_specs->specs[(int) ds_thread])
7219 error ("`__thread' before `static'");
7220 decl_specs->specs[(int) ds_thread] = 0;
7222 cp_parser_set_storage_class (decl_specs, sc_static);
7225 /* Consume the token. */
7226 cp_lexer_consume_token (parser->lexer);
7227 if (decl_specs->specs[(int) ds_thread])
7229 error ("`__thread' before `extern'");
7230 decl_specs->specs[(int) ds_thread] = 0;
7232 cp_parser_set_storage_class (decl_specs, sc_extern);
7235 /* Consume the token. */
7236 cp_lexer_consume_token (parser->lexer);
7237 cp_parser_set_storage_class (decl_specs, sc_mutable);
7240 /* Consume the token. */
7241 cp_lexer_consume_token (parser->lexer);
7242 ++decl_specs->specs[(int) ds_thread];
7246 /* We did not yet find a decl-specifier yet. */
7247 found_decl_spec = false;
7251 /* Constructors are a special case. The `S' in `S()' is not a
7252 decl-specifier; it is the beginning of the declarator. */
7255 && constructor_possible_p
7256 && (cp_parser_constructor_declarator_p
7257 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7259 /* If we don't have a DECL_SPEC yet, then we must be looking at
7260 a type-specifier. */
7261 if (!found_decl_spec && !constructor_p)
7263 int decl_spec_declares_class_or_enum;
7264 bool is_cv_qualifier;
7268 = cp_parser_type_specifier (parser, flags,
7270 /*is_declaration=*/true,
7271 &decl_spec_declares_class_or_enum,
7274 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7276 /* If this type-specifier referenced a user-defined type
7277 (a typedef, class-name, etc.), then we can't allow any
7278 more such type-specifiers henceforth.
7282 The longest sequence of decl-specifiers that could
7283 possibly be a type name is taken as the
7284 decl-specifier-seq of a declaration. The sequence shall
7285 be self-consistent as described below.
7289 As a general rule, at most one type-specifier is allowed
7290 in the complete decl-specifier-seq of a declaration. The
7291 only exceptions are the following:
7293 -- const or volatile can be combined with any other
7296 -- signed or unsigned can be combined with char, long,
7304 void g (const int Pc);
7306 Here, Pc is *not* part of the decl-specifier seq; it's
7307 the declarator. Therefore, once we see a type-specifier
7308 (other than a cv-qualifier), we forbid any additional
7309 user-defined types. We *do* still allow things like `int
7310 int' to be considered a decl-specifier-seq, and issue the
7311 error message later. */
7312 if (type_spec && !is_cv_qualifier)
7313 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7314 /* A constructor declarator cannot follow a type-specifier. */
7317 constructor_possible_p = false;
7318 found_decl_spec = true;
7322 /* If we still do not have a DECL_SPEC, then there are no more
7324 if (!found_decl_spec)
7327 decl_specs->any_specifiers_p = true;
7328 /* After we see one decl-specifier, further decl-specifiers are
7330 flags |= CP_PARSER_FLAGS_OPTIONAL;
7333 /* Don't allow a friend specifier with a class definition. */
7334 if (decl_specs->specs[(int) ds_friend] != 0
7335 && (*declares_class_or_enum & 2))
7336 error ("class definition may not be declared a friend");
7339 /* Parse an (optional) storage-class-specifier.
7341 storage-class-specifier:
7350 storage-class-specifier:
7353 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7356 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7358 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7366 /* Consume the token. */
7367 return cp_lexer_consume_token (parser->lexer)->value;
7374 /* Parse an (optional) function-specifier.
7381 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7382 Updates DECL_SPECS, if it is non-NULL. */
7385 cp_parser_function_specifier_opt (cp_parser* parser,
7386 cp_decl_specifier_seq *decl_specs)
7388 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7392 ++decl_specs->specs[(int) ds_inline];
7397 ++decl_specs->specs[(int) ds_virtual];
7402 ++decl_specs->specs[(int) ds_explicit];
7409 /* Consume the token. */
7410 return cp_lexer_consume_token (parser->lexer)->value;
7413 /* Parse a linkage-specification.
7415 linkage-specification:
7416 extern string-literal { declaration-seq [opt] }
7417 extern string-literal declaration */
7420 cp_parser_linkage_specification (cp_parser* parser)
7425 /* Look for the `extern' keyword. */
7426 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7428 /* Peek at the next token. */
7429 token = cp_lexer_peek_token (parser->lexer);
7430 /* If it's not a string-literal, then there's a problem. */
7431 if (!cp_parser_is_string_literal (token))
7433 cp_parser_error (parser, "expected language-name");
7436 /* Consume the token. */
7437 cp_lexer_consume_token (parser->lexer);
7439 /* Transform the literal into an identifier. If the literal is a
7440 wide-character string, or contains embedded NULs, then we can't
7441 handle it as the user wants. */
7442 if (token->type == CPP_WSTRING
7443 || (strlen (TREE_STRING_POINTER (token->value))
7444 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7446 cp_parser_error (parser, "invalid linkage-specification");
7447 /* Assume C++ linkage. */
7448 linkage = get_identifier ("c++");
7450 /* If the string is chained to another string, take the latter,
7451 that's the untranslated string. */
7452 else if (TREE_CHAIN (token->value))
7453 linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7454 /* If it's a simple string constant, things are easier. */
7456 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7458 /* We're now using the new linkage. */
7459 push_lang_context (linkage);
7461 /* If the next token is a `{', then we're using the first
7463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7465 /* Consume the `{' token. */
7466 cp_lexer_consume_token (parser->lexer);
7467 /* Parse the declarations. */
7468 cp_parser_declaration_seq_opt (parser);
7469 /* Look for the closing `}'. */
7470 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7472 /* Otherwise, there's just one declaration. */
7475 bool saved_in_unbraced_linkage_specification_p;
7477 saved_in_unbraced_linkage_specification_p
7478 = parser->in_unbraced_linkage_specification_p;
7479 parser->in_unbraced_linkage_specification_p = true;
7480 have_extern_spec = true;
7481 cp_parser_declaration (parser);
7482 have_extern_spec = false;
7483 parser->in_unbraced_linkage_specification_p
7484 = saved_in_unbraced_linkage_specification_p;
7487 /* We're done with the linkage-specification. */
7488 pop_lang_context ();
7491 /* Special member functions [gram.special] */
7493 /* Parse a conversion-function-id.
7495 conversion-function-id:
7496 operator conversion-type-id
7498 Returns an IDENTIFIER_NODE representing the operator. */
7501 cp_parser_conversion_function_id (cp_parser* parser)
7505 tree saved_qualifying_scope;
7506 tree saved_object_scope;
7509 /* Look for the `operator' token. */
7510 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7511 return error_mark_node;
7512 /* When we parse the conversion-type-id, the current scope will be
7513 reset. However, we need that information in able to look up the
7514 conversion function later, so we save it here. */
7515 saved_scope = parser->scope;
7516 saved_qualifying_scope = parser->qualifying_scope;
7517 saved_object_scope = parser->object_scope;
7518 /* We must enter the scope of the class so that the names of
7519 entities declared within the class are available in the
7520 conversion-type-id. For example, consider:
7527 S::operator I() { ... }
7529 In order to see that `I' is a type-name in the definition, we
7530 must be in the scope of `S'. */
7532 pop_p = push_scope (saved_scope);
7533 /* Parse the conversion-type-id. */
7534 type = cp_parser_conversion_type_id (parser);
7535 /* Leave the scope of the class, if any. */
7537 pop_scope (saved_scope);
7538 /* Restore the saved scope. */
7539 parser->scope = saved_scope;
7540 parser->qualifying_scope = saved_qualifying_scope;
7541 parser->object_scope = saved_object_scope;
7542 /* If the TYPE is invalid, indicate failure. */
7543 if (type == error_mark_node)
7544 return error_mark_node;
7545 return mangle_conv_op_name_for_type (type);
7548 /* Parse a conversion-type-id:
7551 type-specifier-seq conversion-declarator [opt]
7553 Returns the TYPE specified. */
7556 cp_parser_conversion_type_id (cp_parser* parser)
7559 cp_decl_specifier_seq type_specifiers;
7560 cp_declarator *declarator;
7562 /* Parse the attributes. */
7563 attributes = cp_parser_attributes_opt (parser);
7564 /* Parse the type-specifiers. */
7565 cp_parser_type_specifier_seq (parser, &type_specifiers);
7566 /* If that didn't work, stop. */
7567 if (type_specifiers.type == error_mark_node)
7568 return error_mark_node;
7569 /* Parse the conversion-declarator. */
7570 declarator = cp_parser_conversion_declarator_opt (parser);
7572 return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7573 /*initialized=*/0, &attributes);
7576 /* Parse an (optional) conversion-declarator.
7578 conversion-declarator:
7579 ptr-operator conversion-declarator [opt]
7583 static cp_declarator *
7584 cp_parser_conversion_declarator_opt (cp_parser* parser)
7586 enum tree_code code;
7588 cp_cv_quals cv_quals;
7590 /* We don't know if there's a ptr-operator next, or not. */
7591 cp_parser_parse_tentatively (parser);
7592 /* Try the ptr-operator. */
7593 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7594 /* If it worked, look for more conversion-declarators. */
7595 if (cp_parser_parse_definitely (parser))
7597 cp_declarator *declarator;
7599 /* Parse another optional declarator. */
7600 declarator = cp_parser_conversion_declarator_opt (parser);
7602 /* Create the representation of the declarator. */
7604 declarator = make_ptrmem_declarator (cv_quals, class_type,
7606 else if (code == INDIRECT_REF)
7607 declarator = make_pointer_declarator (cv_quals, declarator);
7609 declarator = make_reference_declarator (cv_quals, declarator);
7617 /* Parse an (optional) ctor-initializer.
7620 : mem-initializer-list
7622 Returns TRUE iff the ctor-initializer was actually present. */
7625 cp_parser_ctor_initializer_opt (cp_parser* parser)
7627 /* If the next token is not a `:', then there is no
7628 ctor-initializer. */
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7631 /* Do default initialization of any bases and members. */
7632 if (DECL_CONSTRUCTOR_P (current_function_decl))
7633 finish_mem_initializers (NULL_TREE);
7638 /* Consume the `:' token. */
7639 cp_lexer_consume_token (parser->lexer);
7640 /* And the mem-initializer-list. */
7641 cp_parser_mem_initializer_list (parser);
7646 /* Parse a mem-initializer-list.
7648 mem-initializer-list:
7650 mem-initializer , mem-initializer-list */
7653 cp_parser_mem_initializer_list (cp_parser* parser)
7655 tree mem_initializer_list = NULL_TREE;
7657 /* Let the semantic analysis code know that we are starting the
7658 mem-initializer-list. */
7659 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7660 error ("only constructors take base initializers");
7662 /* Loop through the list. */
7665 tree mem_initializer;
7667 /* Parse the mem-initializer. */
7668 mem_initializer = cp_parser_mem_initializer (parser);
7669 /* Add it to the list, unless it was erroneous. */
7670 if (mem_initializer)
7672 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7673 mem_initializer_list = mem_initializer;
7675 /* If the next token is not a `,', we're done. */
7676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7678 /* Consume the `,' token. */
7679 cp_lexer_consume_token (parser->lexer);
7682 /* Perform semantic analysis. */
7683 if (DECL_CONSTRUCTOR_P (current_function_decl))
7684 finish_mem_initializers (mem_initializer_list);
7687 /* Parse a mem-initializer.
7690 mem-initializer-id ( expression-list [opt] )
7695 ( expression-list [opt] )
7697 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7698 class) or FIELD_DECL (for a non-static data member) to initialize;
7699 the TREE_VALUE is the expression-list. */
7702 cp_parser_mem_initializer (cp_parser* parser)
7704 tree mem_initializer_id;
7705 tree expression_list;
7708 /* Find out what is being initialized. */
7709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7711 pedwarn ("anachronistic old-style base class initializer");
7712 mem_initializer_id = NULL_TREE;
7715 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7716 member = expand_member_init (mem_initializer_id);
7717 if (member && !DECL_P (member))
7718 in_base_initializer = 1;
7721 = cp_parser_parenthesized_expression_list (parser, false,
7722 /*non_constant_p=*/NULL);
7723 if (!expression_list)
7724 expression_list = void_type_node;
7726 in_base_initializer = 0;
7728 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7731 /* Parse a mem-initializer-id.
7734 :: [opt] nested-name-specifier [opt] class-name
7737 Returns a TYPE indicating the class to be initializer for the first
7738 production. Returns an IDENTIFIER_NODE indicating the data member
7739 to be initialized for the second production. */
7742 cp_parser_mem_initializer_id (cp_parser* parser)
7744 bool global_scope_p;
7745 bool nested_name_specifier_p;
7746 bool template_p = false;
7749 /* `typename' is not allowed in this context ([temp.res]). */
7750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7752 error ("keyword `typename' not allowed in this context (a qualified "
7753 "member initializer is implicitly a type)");
7754 cp_lexer_consume_token (parser->lexer);
7756 /* Look for the optional `::' operator. */
7758 = (cp_parser_global_scope_opt (parser,
7759 /*current_scope_valid_p=*/false)
7761 /* Look for the optional nested-name-specifier. The simplest way to
7766 The keyword `typename' is not permitted in a base-specifier or
7767 mem-initializer; in these contexts a qualified name that
7768 depends on a template-parameter is implicitly assumed to be a
7771 is to assume that we have seen the `typename' keyword at this
7773 nested_name_specifier_p
7774 = (cp_parser_nested_name_specifier_opt (parser,
7775 /*typename_keyword_p=*/true,
7776 /*check_dependency_p=*/true,
7778 /*is_declaration=*/true)
7780 if (nested_name_specifier_p)
7781 template_p = cp_parser_optional_template_keyword (parser);
7782 /* If there is a `::' operator or a nested-name-specifier, then we
7783 are definitely looking for a class-name. */
7784 if (global_scope_p || nested_name_specifier_p)
7785 return cp_parser_class_name (parser,
7786 /*typename_keyword_p=*/true,
7787 /*template_keyword_p=*/template_p,
7789 /*check_dependency_p=*/true,
7790 /*class_head_p=*/false,
7791 /*is_declaration=*/true);
7792 /* Otherwise, we could also be looking for an ordinary identifier. */
7793 cp_parser_parse_tentatively (parser);
7794 /* Try a class-name. */
7795 id = cp_parser_class_name (parser,
7796 /*typename_keyword_p=*/true,
7797 /*template_keyword_p=*/false,
7799 /*check_dependency_p=*/true,
7800 /*class_head_p=*/false,
7801 /*is_declaration=*/true);
7802 /* If we found one, we're done. */
7803 if (cp_parser_parse_definitely (parser))
7805 /* Otherwise, look for an ordinary identifier. */
7806 return cp_parser_identifier (parser);
7809 /* Overloading [gram.over] */
7811 /* Parse an operator-function-id.
7813 operator-function-id:
7816 Returns an IDENTIFIER_NODE for the operator which is a
7817 human-readable spelling of the identifier, e.g., `operator +'. */
7820 cp_parser_operator_function_id (cp_parser* parser)
7822 /* Look for the `operator' keyword. */
7823 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7824 return error_mark_node;
7825 /* And then the name of the operator itself. */
7826 return cp_parser_operator (parser);
7829 /* Parse an operator.
7832 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7833 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7834 || ++ -- , ->* -> () []
7841 Returns an IDENTIFIER_NODE for the operator which is a
7842 human-readable spelling of the identifier, e.g., `operator +'. */
7845 cp_parser_operator (cp_parser* parser)
7847 tree id = NULL_TREE;
7850 /* Peek at the next token. */
7851 token = cp_lexer_peek_token (parser->lexer);
7852 /* Figure out which operator we have. */
7853 switch (token->type)
7859 /* The keyword should be either `new' or `delete'. */
7860 if (token->keyword == RID_NEW)
7862 else if (token->keyword == RID_DELETE)
7867 /* Consume the `new' or `delete' token. */
7868 cp_lexer_consume_token (parser->lexer);
7870 /* Peek at the next token. */
7871 token = cp_lexer_peek_token (parser->lexer);
7872 /* If it's a `[' token then this is the array variant of the
7874 if (token->type == CPP_OPEN_SQUARE)
7876 /* Consume the `[' token. */
7877 cp_lexer_consume_token (parser->lexer);
7878 /* Look for the `]' token. */
7879 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7880 id = ansi_opname (op == NEW_EXPR
7881 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7883 /* Otherwise, we have the non-array variant. */
7885 id = ansi_opname (op);
7891 id = ansi_opname (PLUS_EXPR);
7895 id = ansi_opname (MINUS_EXPR);
7899 id = ansi_opname (MULT_EXPR);
7903 id = ansi_opname (TRUNC_DIV_EXPR);
7907 id = ansi_opname (TRUNC_MOD_EXPR);
7911 id = ansi_opname (BIT_XOR_EXPR);
7915 id = ansi_opname (BIT_AND_EXPR);
7919 id = ansi_opname (BIT_IOR_EXPR);
7923 id = ansi_opname (BIT_NOT_EXPR);
7927 id = ansi_opname (TRUTH_NOT_EXPR);
7931 id = ansi_assopname (NOP_EXPR);
7935 id = ansi_opname (LT_EXPR);
7939 id = ansi_opname (GT_EXPR);
7943 id = ansi_assopname (PLUS_EXPR);
7947 id = ansi_assopname (MINUS_EXPR);
7951 id = ansi_assopname (MULT_EXPR);
7955 id = ansi_assopname (TRUNC_DIV_EXPR);
7959 id = ansi_assopname (TRUNC_MOD_EXPR);
7963 id = ansi_assopname (BIT_XOR_EXPR);
7967 id = ansi_assopname (BIT_AND_EXPR);
7971 id = ansi_assopname (BIT_IOR_EXPR);
7975 id = ansi_opname (LSHIFT_EXPR);
7979 id = ansi_opname (RSHIFT_EXPR);
7983 id = ansi_assopname (LSHIFT_EXPR);
7987 id = ansi_assopname (RSHIFT_EXPR);
7991 id = ansi_opname (EQ_EXPR);
7995 id = ansi_opname (NE_EXPR);
7999 id = ansi_opname (LE_EXPR);
8002 case CPP_GREATER_EQ:
8003 id = ansi_opname (GE_EXPR);
8007 id = ansi_opname (TRUTH_ANDIF_EXPR);
8011 id = ansi_opname (TRUTH_ORIF_EXPR);
8015 id = ansi_opname (POSTINCREMENT_EXPR);
8018 case CPP_MINUS_MINUS:
8019 id = ansi_opname (PREDECREMENT_EXPR);
8023 id = ansi_opname (COMPOUND_EXPR);
8026 case CPP_DEREF_STAR:
8027 id = ansi_opname (MEMBER_REF);
8031 id = ansi_opname (COMPONENT_REF);
8034 case CPP_OPEN_PAREN:
8035 /* Consume the `('. */
8036 cp_lexer_consume_token (parser->lexer);
8037 /* Look for the matching `)'. */
8038 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8039 return ansi_opname (CALL_EXPR);
8041 case CPP_OPEN_SQUARE:
8042 /* Consume the `['. */
8043 cp_lexer_consume_token (parser->lexer);
8044 /* Look for the matching `]'. */
8045 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8046 return ansi_opname (ARRAY_REF);
8050 id = ansi_opname (MIN_EXPR);
8054 id = ansi_opname (MAX_EXPR);
8058 id = ansi_assopname (MIN_EXPR);
8062 id = ansi_assopname (MAX_EXPR);
8066 /* Anything else is an error. */
8070 /* If we have selected an identifier, we need to consume the
8073 cp_lexer_consume_token (parser->lexer);
8074 /* Otherwise, no valid operator name was present. */
8077 cp_parser_error (parser, "expected operator");
8078 id = error_mark_node;
8084 /* Parse a template-declaration.
8086 template-declaration:
8087 export [opt] template < template-parameter-list > declaration
8089 If MEMBER_P is TRUE, this template-declaration occurs within a
8092 The grammar rule given by the standard isn't correct. What
8095 template-declaration:
8096 export [opt] template-parameter-list-seq
8097 decl-specifier-seq [opt] init-declarator [opt] ;
8098 export [opt] template-parameter-list-seq
8101 template-parameter-list-seq:
8102 template-parameter-list-seq [opt]
8103 template < template-parameter-list > */
8106 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8108 /* Check for `export'. */
8109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8111 /* Consume the `export' token. */
8112 cp_lexer_consume_token (parser->lexer);
8113 /* Warn that we do not support `export'. */
8114 warning ("keyword `export' not implemented, and will be ignored");
8117 cp_parser_template_declaration_after_export (parser, member_p);
8120 /* Parse a template-parameter-list.
8122 template-parameter-list:
8124 template-parameter-list , template-parameter
8126 Returns a TREE_LIST. Each node represents a template parameter.
8127 The nodes are connected via their TREE_CHAINs. */
8130 cp_parser_template_parameter_list (cp_parser* parser)
8132 tree parameter_list = NULL_TREE;
8140 /* Parse the template-parameter. */
8141 parameter = cp_parser_template_parameter (parser, &is_non_type);
8142 /* Add it to the list. */
8143 parameter_list = process_template_parm (parameter_list,
8146 /* Peek at the next token. */
8147 token = cp_lexer_peek_token (parser->lexer);
8148 /* If it's not a `,', we're done. */
8149 if (token->type != CPP_COMMA)
8151 /* Otherwise, consume the `,' token. */
8152 cp_lexer_consume_token (parser->lexer);
8155 return parameter_list;
8158 /* Parse a template-parameter.
8162 parameter-declaration
8164 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8165 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8166 true iff this parameter is a non-type parameter. */
8169 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8172 cp_parameter_declarator *parameter_declarator;
8174 /* Assume it is a type parameter or a template parameter. */
8175 *is_non_type = false;
8176 /* Peek at the next token. */
8177 token = cp_lexer_peek_token (parser->lexer);
8178 /* If it is `class' or `template', we have a type-parameter. */
8179 if (token->keyword == RID_TEMPLATE)
8180 return cp_parser_type_parameter (parser);
8181 /* If it is `class' or `typename' we do not know yet whether it is a
8182 type parameter or a non-type parameter. Consider:
8184 template <typename T, typename T::X X> ...
8188 template <class C, class D*> ...
8190 Here, the first parameter is a type parameter, and the second is
8191 a non-type parameter. We can tell by looking at the token after
8192 the identifier -- if it is a `,', `=', or `>' then we have a type
8194 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8196 /* Peek at the token after `class' or `typename'. */
8197 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8198 /* If it's an identifier, skip it. */
8199 if (token->type == CPP_NAME)
8200 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8201 /* Now, see if the token looks like the end of a template
8203 if (token->type == CPP_COMMA
8204 || token->type == CPP_EQ
8205 || token->type == CPP_GREATER)
8206 return cp_parser_type_parameter (parser);
8209 /* Otherwise, it is a non-type parameter.
8213 When parsing a default template-argument for a non-type
8214 template-parameter, the first non-nested `>' is taken as the end
8215 of the template parameter-list rather than a greater-than
8217 *is_non_type = true;
8218 parameter_declarator
8219 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8220 /*parenthesized_p=*/NULL);
8221 return (build_tree_list
8222 (parameter_declarator->default_argument,
8223 grokdeclarator (parameter_declarator->declarator,
8224 ¶meter_declarator->decl_specifiers,
8225 PARM, /*initialized=*/0,
8226 /*attrlist=*/NULL)));
8229 /* Parse a type-parameter.
8232 class identifier [opt]
8233 class identifier [opt] = type-id
8234 typename identifier [opt]
8235 typename identifier [opt] = type-id
8236 template < template-parameter-list > class identifier [opt]
8237 template < template-parameter-list > class identifier [opt]
8240 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8241 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8242 the declaration of the parameter. */
8245 cp_parser_type_parameter (cp_parser* parser)
8250 /* Look for a keyword to tell us what kind of parameter this is. */
8251 token = cp_parser_require (parser, CPP_KEYWORD,
8252 "`class', `typename', or `template'");
8254 return error_mark_node;
8256 switch (token->keyword)
8262 tree default_argument;
8264 /* If the next token is an identifier, then it names the
8266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8267 identifier = cp_parser_identifier (parser);
8269 identifier = NULL_TREE;
8271 /* Create the parameter. */
8272 parameter = finish_template_type_parm (class_type_node, identifier);
8274 /* If the next token is an `=', we have a default argument. */
8275 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8277 /* Consume the `=' token. */
8278 cp_lexer_consume_token (parser->lexer);
8279 /* Parse the default-argument. */
8280 default_argument = cp_parser_type_id (parser);
8283 default_argument = NULL_TREE;
8285 /* Create the combined representation of the parameter and the
8286 default argument. */
8287 parameter = build_tree_list (default_argument, parameter);
8293 tree parameter_list;
8295 tree default_argument;
8297 /* Look for the `<'. */
8298 cp_parser_require (parser, CPP_LESS, "`<'");
8299 /* Parse the template-parameter-list. */
8300 begin_template_parm_list ();
8302 = cp_parser_template_parameter_list (parser);
8303 parameter_list = end_template_parm_list (parameter_list);
8304 /* Look for the `>'. */
8305 cp_parser_require (parser, CPP_GREATER, "`>'");
8306 /* Look for the `class' keyword. */
8307 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8308 /* If the next token is an `=', then there is a
8309 default-argument. If the next token is a `>', we are at
8310 the end of the parameter-list. If the next token is a `,',
8311 then we are at the end of this parameter. */
8312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8313 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8314 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8315 identifier = cp_parser_identifier (parser);
8317 identifier = NULL_TREE;
8318 /* Create the template parameter. */
8319 parameter = finish_template_template_parm (class_type_node,
8322 /* If the next token is an `=', then there is a
8323 default-argument. */
8324 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8328 /* Consume the `='. */
8329 cp_lexer_consume_token (parser->lexer);
8330 /* Parse the id-expression. */
8332 = cp_parser_id_expression (parser,
8333 /*template_keyword_p=*/false,
8334 /*check_dependency_p=*/true,
8335 /*template_p=*/&is_template,
8336 /*declarator_p=*/false);
8337 if (TREE_CODE (default_argument) == TYPE_DECL)
8338 /* If the id-expression was a template-id that refers to
8339 a template-class, we already have the declaration here,
8340 so no further lookup is needed. */
8343 /* Look up the name. */
8345 = cp_parser_lookup_name (parser, default_argument,
8347 /*is_template=*/is_template,
8348 /*is_namespace=*/false,
8349 /*check_dependency=*/true,
8350 /*ambiguous_p=*/NULL);
8351 /* See if the default argument is valid. */
8353 = check_template_template_default_arg (default_argument);
8356 default_argument = NULL_TREE;
8358 /* Create the combined representation of the parameter and the
8359 default argument. */
8360 parameter = build_tree_list (default_argument, parameter);
8365 /* Anything else is an error. */
8366 cp_parser_error (parser,
8367 "expected `class', `typename', or `template'");
8368 parameter = error_mark_node;
8374 /* Parse a template-id.
8377 template-name < template-argument-list [opt] >
8379 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8380 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8381 returned. Otherwise, if the template-name names a function, or set
8382 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8383 names a class, returns a TYPE_DECL for the specialization.
8385 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8386 uninstantiated templates. */
8389 cp_parser_template_id (cp_parser *parser,
8390 bool template_keyword_p,
8391 bool check_dependency_p,
8392 bool is_declaration)
8397 ptrdiff_t start_of_id;
8398 tree access_check = NULL_TREE;
8399 cp_token *next_token, *next_token_2;
8402 /* If the next token corresponds to a template-id, there is no need
8404 next_token = cp_lexer_peek_token (parser->lexer);
8405 if (next_token->type == CPP_TEMPLATE_ID)
8410 /* Get the stored value. */
8411 value = cp_lexer_consume_token (parser->lexer)->value;
8412 /* Perform any access checks that were deferred. */
8413 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8414 perform_or_defer_access_check (TREE_PURPOSE (check),
8415 TREE_VALUE (check));
8416 /* Return the stored value. */
8417 return TREE_VALUE (value);
8420 /* Avoid performing name lookup if there is no possibility of
8421 finding a template-id. */
8422 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8423 || (next_token->type == CPP_NAME
8424 && !cp_parser_nth_token_starts_template_argument_list_p
8427 cp_parser_error (parser, "expected template-id");
8428 return error_mark_node;
8431 /* Remember where the template-id starts. */
8432 if (cp_parser_parsing_tentatively (parser)
8433 && !cp_parser_committed_to_tentative_parse (parser))
8435 next_token = cp_lexer_peek_token (parser->lexer);
8436 start_of_id = cp_lexer_token_difference (parser->lexer,
8437 parser->lexer->first_token,
8443 push_deferring_access_checks (dk_deferred);
8445 /* Parse the template-name. */
8446 is_identifier = false;
8447 template = cp_parser_template_name (parser, template_keyword_p,
8451 if (template == error_mark_node || is_identifier)
8453 pop_deferring_access_checks ();
8457 /* If we find the sequence `[:' after a template-name, it's probably
8458 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8459 parse correctly the argument list. */
8460 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8461 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8462 if (next_token->type == CPP_OPEN_SQUARE
8463 && next_token->flags & DIGRAPH
8464 && next_token_2->type == CPP_COLON
8465 && !(next_token_2->flags & PREV_WHITE))
8467 cp_parser_parse_tentatively (parser);
8468 /* Change `:' into `::'. */
8469 next_token_2->type = CPP_SCOPE;
8470 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8472 cp_lexer_consume_token (parser->lexer);
8473 /* Parse the arguments. */
8474 arguments = cp_parser_enclosed_template_argument_list (parser);
8475 if (!cp_parser_parse_definitely (parser))
8477 /* If we couldn't parse an argument list, then we revert our changes
8478 and return simply an error. Maybe this is not a template-id
8480 next_token_2->type = CPP_COLON;
8481 cp_parser_error (parser, "expected `<'");
8482 pop_deferring_access_checks ();
8483 return error_mark_node;
8485 /* Otherwise, emit an error about the invalid digraph, but continue
8486 parsing because we got our argument list. */
8487 pedwarn ("`<::' cannot begin a template-argument list");
8488 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8489 "between `<' and `::'");
8490 if (!flag_permissive)
8495 inform ("(if you use `-fpermissive' G++ will accept your code)");
8502 /* Look for the `<' that starts the template-argument-list. */
8503 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8505 pop_deferring_access_checks ();
8506 return error_mark_node;
8508 /* Parse the arguments. */
8509 arguments = cp_parser_enclosed_template_argument_list (parser);
8512 /* Build a representation of the specialization. */
8513 if (TREE_CODE (template) == IDENTIFIER_NODE)
8514 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8515 else if (DECL_CLASS_TEMPLATE_P (template)
8516 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8518 = finish_template_type (template, arguments,
8519 cp_lexer_next_token_is (parser->lexer,
8523 /* If it's not a class-template or a template-template, it should be
8524 a function-template. */
8525 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8526 || TREE_CODE (template) == OVERLOAD
8527 || BASELINK_P (template)));
8529 template_id = lookup_template_function (template, arguments);
8532 /* Retrieve any deferred checks. Do not pop this access checks yet
8533 so the memory will not be reclaimed during token replacing below. */
8534 access_check = get_deferred_access_checks ();
8536 /* If parsing tentatively, replace the sequence of tokens that makes
8537 up the template-id with a CPP_TEMPLATE_ID token. That way,
8538 should we re-parse the token stream, we will not have to repeat
8539 the effort required to do the parse, nor will we issue duplicate
8540 error messages about problems during instantiation of the
8542 if (start_of_id >= 0)
8546 /* Find the token that corresponds to the start of the
8548 token = cp_lexer_advance_token (parser->lexer,
8549 parser->lexer->first_token,
8552 /* Reset the contents of the START_OF_ID token. */
8553 token->type = CPP_TEMPLATE_ID;
8554 token->value = build_tree_list (access_check, template_id);
8555 token->keyword = RID_MAX;
8556 /* Purge all subsequent tokens. */
8557 cp_lexer_purge_tokens_after (parser->lexer, token);
8560 pop_deferring_access_checks ();
8564 /* Parse a template-name.
8569 The standard should actually say:
8573 operator-function-id
8575 A defect report has been filed about this issue.
8577 A conversion-function-id cannot be a template name because they cannot
8578 be part of a template-id. In fact, looking at this code:
8582 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8583 It is impossible to call a templated conversion-function-id with an
8584 explicit argument list, since the only allowed template parameter is
8585 the type to which it is converting.
8587 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8588 `template' keyword, in a construction like:
8592 In that case `f' is taken to be a template-name, even though there
8593 is no way of knowing for sure.
8595 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8596 name refers to a set of overloaded functions, at least one of which
8597 is a template, or an IDENTIFIER_NODE with the name of the template,
8598 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8599 names are looked up inside uninstantiated templates. */
8602 cp_parser_template_name (cp_parser* parser,
8603 bool template_keyword_p,
8604 bool check_dependency_p,
8605 bool is_declaration,
8606 bool *is_identifier)
8612 /* If the next token is `operator', then we have either an
8613 operator-function-id or a conversion-function-id. */
8614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8616 /* We don't know whether we're looking at an
8617 operator-function-id or a conversion-function-id. */
8618 cp_parser_parse_tentatively (parser);
8619 /* Try an operator-function-id. */
8620 identifier = cp_parser_operator_function_id (parser);
8621 /* If that didn't work, try a conversion-function-id. */
8622 if (!cp_parser_parse_definitely (parser))
8624 cp_parser_error (parser, "expected template-name");
8625 return error_mark_node;
8628 /* Look for the identifier. */
8630 identifier = cp_parser_identifier (parser);
8632 /* If we didn't find an identifier, we don't have a template-id. */
8633 if (identifier == error_mark_node)
8634 return error_mark_node;
8636 /* If the name immediately followed the `template' keyword, then it
8637 is a template-name. However, if the next token is not `<', then
8638 we do not treat it as a template-name, since it is not being used
8639 as part of a template-id. This enables us to handle constructs
8642 template <typename T> struct S { S(); };
8643 template <typename T> S<T>::S();
8645 correctly. We would treat `S' as a template -- if it were `S<T>'
8646 -- but we do not if there is no `<'. */
8648 if (processing_template_decl
8649 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8651 /* In a declaration, in a dependent context, we pretend that the
8652 "template" keyword was present in order to improve error
8653 recovery. For example, given:
8655 template <typename T> void f(T::X<int>);
8657 we want to treat "X<int>" as a template-id. */
8659 && !template_keyword_p
8660 && parser->scope && TYPE_P (parser->scope)
8661 && dependent_type_p (parser->scope)
8662 /* Do not do this for dtors (or ctors), since they never
8663 need the template keyword before their name. */
8664 && !constructor_name_p (identifier, parser->scope))
8668 /* Explain what went wrong. */
8669 error ("non-template `%D' used as template", identifier);
8670 inform ("use `%T::template %D' to indicate that it is a template",
8671 parser->scope, identifier);
8672 /* If parsing tentatively, find the location of the "<"
8674 if (cp_parser_parsing_tentatively (parser)
8675 && !cp_parser_committed_to_tentative_parse (parser))
8677 cp_parser_simulate_error (parser);
8678 token = cp_lexer_peek_token (parser->lexer);
8679 token = cp_lexer_prev_token (parser->lexer, token);
8680 start = cp_lexer_token_difference (parser->lexer,
8681 parser->lexer->first_token,
8686 /* Parse the template arguments so that we can issue error
8687 messages about them. */
8688 cp_lexer_consume_token (parser->lexer);
8689 cp_parser_enclosed_template_argument_list (parser);
8690 /* Skip tokens until we find a good place from which to
8691 continue parsing. */
8692 cp_parser_skip_to_closing_parenthesis (parser,
8693 /*recovering=*/true,
8695 /*consume_paren=*/false);
8696 /* If parsing tentatively, permanently remove the
8697 template argument list. That will prevent duplicate
8698 error messages from being issued about the missing
8699 "template" keyword. */
8702 token = cp_lexer_advance_token (parser->lexer,
8703 parser->lexer->first_token,
8705 cp_lexer_purge_tokens_after (parser->lexer, token);
8708 *is_identifier = true;
8712 /* If the "template" keyword is present, then there is generally
8713 no point in doing name-lookup, so we just return IDENTIFIER.
8714 But, if the qualifying scope is non-dependent then we can
8715 (and must) do name-lookup normally. */
8716 if (template_keyword_p
8718 || (TYPE_P (parser->scope)
8719 && dependent_type_p (parser->scope))))
8723 /* Look up the name. */
8724 decl = cp_parser_lookup_name (parser, identifier,
8726 /*is_template=*/false,
8727 /*is_namespace=*/false,
8729 /*ambiguous_p=*/NULL);
8730 decl = maybe_get_template_decl_from_type_decl (decl);
8732 /* If DECL is a template, then the name was a template-name. */
8733 if (TREE_CODE (decl) == TEMPLATE_DECL)
8737 /* The standard does not explicitly indicate whether a name that
8738 names a set of overloaded declarations, some of which are
8739 templates, is a template-name. However, such a name should
8740 be a template-name; otherwise, there is no way to form a
8741 template-id for the overloaded templates. */
8742 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8743 if (TREE_CODE (fns) == OVERLOAD)
8747 for (fn = fns; fn; fn = OVL_NEXT (fn))
8748 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8753 /* Otherwise, the name does not name a template. */
8754 cp_parser_error (parser, "expected template-name");
8755 return error_mark_node;
8759 /* If DECL is dependent, and refers to a function, then just return
8760 its name; we will look it up again during template instantiation. */
8761 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8763 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8764 if (TYPE_P (scope) && dependent_type_p (scope))
8771 /* Parse a template-argument-list.
8773 template-argument-list:
8775 template-argument-list , template-argument
8777 Returns a TREE_VEC containing the arguments. */
8780 cp_parser_template_argument_list (cp_parser* parser)
8782 tree fixed_args[10];
8783 unsigned n_args = 0;
8784 unsigned alloced = 10;
8785 tree *arg_ary = fixed_args;
8787 bool saved_in_template_argument_list_p;
8789 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8790 parser->in_template_argument_list_p = true;
8796 /* Consume the comma. */
8797 cp_lexer_consume_token (parser->lexer);
8799 /* Parse the template-argument. */
8800 argument = cp_parser_template_argument (parser);
8801 if (n_args == alloced)
8805 if (arg_ary == fixed_args)
8807 arg_ary = xmalloc (sizeof (tree) * alloced);
8808 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8811 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8813 arg_ary[n_args++] = argument;
8815 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8817 vec = make_tree_vec (n_args);
8820 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8822 if (arg_ary != fixed_args)
8824 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8828 /* Parse a template-argument.
8831 assignment-expression
8835 The representation is that of an assignment-expression, type-id, or
8836 id-expression -- except that the qualified id-expression is
8837 evaluated, so that the value returned is either a DECL or an
8840 Although the standard says "assignment-expression", it forbids
8841 throw-expressions or assignments in the template argument.
8842 Therefore, we use "conditional-expression" instead. */
8845 cp_parser_template_argument (cp_parser* parser)
8850 bool maybe_type_id = false;
8853 tree qualifying_class;
8855 /* There's really no way to know what we're looking at, so we just
8856 try each alternative in order.
8860 In a template-argument, an ambiguity between a type-id and an
8861 expression is resolved to a type-id, regardless of the form of
8862 the corresponding template-parameter.
8864 Therefore, we try a type-id first. */
8865 cp_parser_parse_tentatively (parser);
8866 argument = cp_parser_type_id (parser);
8867 /* If there was no error parsing the type-id but the next token is a '>>',
8868 we probably found a typo for '> >'. But there are type-id which are
8869 also valid expressions. For instance:
8871 struct X { int operator >> (int); };
8872 template <int V> struct Foo {};
8875 Here 'X()' is a valid type-id of a function type, but the user just
8876 wanted to write the expression "X() >> 5". Thus, we remember that we
8877 found a valid type-id, but we still try to parse the argument as an
8878 expression to see what happens. */
8879 if (!cp_parser_error_occurred (parser)
8880 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8882 maybe_type_id = true;
8883 cp_parser_abort_tentative_parse (parser);
8887 /* If the next token isn't a `,' or a `>', then this argument wasn't
8888 really finished. This means that the argument is not a valid
8890 if (!cp_parser_next_token_ends_template_argument_p (parser))
8891 cp_parser_error (parser, "expected template-argument");
8892 /* If that worked, we're done. */
8893 if (cp_parser_parse_definitely (parser))
8896 /* We're still not sure what the argument will be. */
8897 cp_parser_parse_tentatively (parser);
8898 /* Try a template. */
8899 argument = cp_parser_id_expression (parser,
8900 /*template_keyword_p=*/false,
8901 /*check_dependency_p=*/true,
8903 /*declarator_p=*/false);
8904 /* If the next token isn't a `,' or a `>', then this argument wasn't
8906 if (!cp_parser_next_token_ends_template_argument_p (parser))
8907 cp_parser_error (parser, "expected template-argument");
8908 if (!cp_parser_error_occurred (parser))
8910 /* Figure out what is being referred to. If the id-expression
8911 was for a class template specialization, then we will have a
8912 TYPE_DECL at this point. There is no need to do name lookup
8913 at this point in that case. */
8914 if (TREE_CODE (argument) != TYPE_DECL)
8915 argument = cp_parser_lookup_name (parser, argument,
8917 /*is_template=*/template_p,
8918 /*is_namespace=*/false,
8919 /*check_dependency=*/true,
8920 /*ambiguous_p=*/NULL);
8921 if (TREE_CODE (argument) != TEMPLATE_DECL
8922 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8923 cp_parser_error (parser, "expected template-name");
8925 if (cp_parser_parse_definitely (parser))
8927 /* It must be a non-type argument. There permitted cases are given
8928 in [temp.arg.nontype]:
8930 -- an integral constant-expression of integral or enumeration
8933 -- the name of a non-type template-parameter; or
8935 -- the name of an object or function with external linkage...
8937 -- the address of an object or function with external linkage...
8939 -- a pointer to member... */
8940 /* Look for a non-type template parameter. */
8941 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8943 cp_parser_parse_tentatively (parser);
8944 argument = cp_parser_primary_expression (parser,
8947 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8948 || !cp_parser_next_token_ends_template_argument_p (parser))
8949 cp_parser_simulate_error (parser);
8950 if (cp_parser_parse_definitely (parser))
8953 /* If the next token is "&", the argument must be the address of an
8954 object or function with external linkage. */
8955 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8957 cp_lexer_consume_token (parser->lexer);
8958 /* See if we might have an id-expression. */
8959 token = cp_lexer_peek_token (parser->lexer);
8960 if (token->type == CPP_NAME
8961 || token->keyword == RID_OPERATOR
8962 || token->type == CPP_SCOPE
8963 || token->type == CPP_TEMPLATE_ID
8964 || token->type == CPP_NESTED_NAME_SPECIFIER)
8966 cp_parser_parse_tentatively (parser);
8967 argument = cp_parser_primary_expression (parser,
8970 if (cp_parser_error_occurred (parser)
8971 || !cp_parser_next_token_ends_template_argument_p (parser))
8972 cp_parser_abort_tentative_parse (parser);
8975 if (qualifying_class)
8976 argument = finish_qualified_id_expr (qualifying_class,
8980 if (TREE_CODE (argument) == VAR_DECL)
8982 /* A variable without external linkage might still be a
8983 valid constant-expression, so no error is issued here
8984 if the external-linkage check fails. */
8985 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8986 cp_parser_simulate_error (parser);
8988 else if (is_overloaded_fn (argument))
8989 /* All overloaded functions are allowed; if the external
8990 linkage test does not pass, an error will be issued
8994 && (TREE_CODE (argument) == OFFSET_REF
8995 || TREE_CODE (argument) == SCOPE_REF))
8996 /* A pointer-to-member. */
8999 cp_parser_simulate_error (parser);
9001 if (cp_parser_parse_definitely (parser))
9004 argument = build_x_unary_op (ADDR_EXPR, argument);
9009 /* If the argument started with "&", there are no other valid
9010 alternatives at this point. */
9013 cp_parser_error (parser, "invalid non-type template argument");
9014 return error_mark_node;
9016 /* If the argument wasn't successfully parsed as a type-id followed
9017 by '>>', the argument can only be a constant expression now.
9018 Otherwise, we try parsing the constant-expression tentatively,
9019 because the argument could really be a type-id. */
9021 cp_parser_parse_tentatively (parser);
9022 argument = cp_parser_constant_expression (parser,
9023 /*allow_non_constant_p=*/false,
9024 /*non_constant_p=*/NULL);
9025 argument = fold_non_dependent_expr (argument);
9028 if (!cp_parser_next_token_ends_template_argument_p (parser))
9029 cp_parser_error (parser, "expected template-argument");
9030 if (cp_parser_parse_definitely (parser))
9032 /* We did our best to parse the argument as a non type-id, but that
9033 was the only alternative that matched (albeit with a '>' after
9034 it). We can assume it's just a typo from the user, and a
9035 diagnostic will then be issued. */
9036 return cp_parser_type_id (parser);
9039 /* Parse an explicit-instantiation.
9041 explicit-instantiation:
9042 template declaration
9044 Although the standard says `declaration', what it really means is:
9046 explicit-instantiation:
9047 template decl-specifier-seq [opt] declarator [opt] ;
9049 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9050 supposed to be allowed. A defect report has been filed about this
9055 explicit-instantiation:
9056 storage-class-specifier template
9057 decl-specifier-seq [opt] declarator [opt] ;
9058 function-specifier template
9059 decl-specifier-seq [opt] declarator [opt] ; */
9062 cp_parser_explicit_instantiation (cp_parser* parser)
9064 int declares_class_or_enum;
9065 cp_decl_specifier_seq decl_specifiers;
9066 tree extension_specifier = NULL_TREE;
9068 /* Look for an (optional) storage-class-specifier or
9069 function-specifier. */
9070 if (cp_parser_allow_gnu_extensions_p (parser))
9073 = cp_parser_storage_class_specifier_opt (parser);
9074 if (!extension_specifier)
9076 = cp_parser_function_specifier_opt (parser,
9077 /*decl_specs=*/NULL);
9080 /* Look for the `template' keyword. */
9081 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9082 /* Let the front end know that we are processing an explicit
9084 begin_explicit_instantiation ();
9085 /* [temp.explicit] says that we are supposed to ignore access
9086 control while processing explicit instantiation directives. */
9087 push_deferring_access_checks (dk_no_check);
9088 /* Parse a decl-specifier-seq. */
9089 cp_parser_decl_specifier_seq (parser,
9090 CP_PARSER_FLAGS_OPTIONAL,
9092 &declares_class_or_enum);
9093 /* If there was exactly one decl-specifier, and it declared a class,
9094 and there's no declarator, then we have an explicit type
9096 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9100 type = check_tag_decl (&decl_specifiers);
9101 /* Turn access control back on for names used during
9102 template instantiation. */
9103 pop_deferring_access_checks ();
9105 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9109 cp_declarator *declarator;
9112 /* Parse the declarator. */
9114 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9115 /*ctor_dtor_or_conv_p=*/NULL,
9116 /*parenthesized_p=*/NULL);
9117 cp_parser_check_for_definition_in_return_type (declarator,
9118 declares_class_or_enum);
9119 if (declarator != cp_error_declarator)
9121 decl = grokdeclarator (declarator, &decl_specifiers,
9123 /* Turn access control back on for names used during
9124 template instantiation. */
9125 pop_deferring_access_checks ();
9126 /* Do the explicit instantiation. */
9127 do_decl_instantiation (decl, extension_specifier);
9131 pop_deferring_access_checks ();
9132 /* Skip the body of the explicit instantiation. */
9133 cp_parser_skip_to_end_of_statement (parser);
9136 /* We're done with the instantiation. */
9137 end_explicit_instantiation ();
9139 cp_parser_consume_semicolon_at_end_of_statement (parser);
9142 /* Parse an explicit-specialization.
9144 explicit-specialization:
9145 template < > declaration
9147 Although the standard says `declaration', what it really means is:
9149 explicit-specialization:
9150 template <> decl-specifier [opt] init-declarator [opt] ;
9151 template <> function-definition
9152 template <> explicit-specialization
9153 template <> template-declaration */
9156 cp_parser_explicit_specialization (cp_parser* parser)
9158 /* Look for the `template' keyword. */
9159 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9160 /* Look for the `<'. */
9161 cp_parser_require (parser, CPP_LESS, "`<'");
9162 /* Look for the `>'. */
9163 cp_parser_require (parser, CPP_GREATER, "`>'");
9164 /* We have processed another parameter list. */
9165 ++parser->num_template_parameter_lists;
9166 /* Let the front end know that we are beginning a specialization. */
9167 begin_specialization ();
9169 /* If the next keyword is `template', we need to figure out whether
9170 or not we're looking a template-declaration. */
9171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9173 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9174 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9175 cp_parser_template_declaration_after_export (parser,
9176 /*member_p=*/false);
9178 cp_parser_explicit_specialization (parser);
9181 /* Parse the dependent declaration. */
9182 cp_parser_single_declaration (parser,
9186 /* We're done with the specialization. */
9187 end_specialization ();
9188 /* We're done with this parameter list. */
9189 --parser->num_template_parameter_lists;
9192 /* Parse a type-specifier.
9195 simple-type-specifier
9198 elaborated-type-specifier
9206 Returns a representation of the type-specifier. For a
9207 class-specifier, enum-specifier, or elaborated-type-specifier, a
9208 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9210 If IS_FRIEND is TRUE then this type-specifier is being declared a
9211 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
9212 appearing in a decl-specifier-seq.
9214 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9215 class-specifier, enum-specifier, or elaborated-type-specifier, then
9216 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9217 if a type is declared; 2 if it is defined. Otherwise, it is set to
9220 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9221 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9225 cp_parser_type_specifier (cp_parser* parser,
9226 cp_parser_flags flags,
9227 cp_decl_specifier_seq *decl_specs,
9228 bool is_declaration,
9229 int* declares_class_or_enum,
9230 bool* is_cv_qualifier)
9232 tree type_spec = NULL_TREE;
9235 cp_decl_spec ds = ds_last;
9237 /* Assume this type-specifier does not declare a new type. */
9238 if (declares_class_or_enum)
9239 *declares_class_or_enum = 0;
9240 /* And that it does not specify a cv-qualifier. */
9241 if (is_cv_qualifier)
9242 *is_cv_qualifier = false;
9243 /* Peek at the next token. */
9244 token = cp_lexer_peek_token (parser->lexer);
9246 /* If we're looking at a keyword, we can use that to guide the
9247 production we choose. */
9248 keyword = token->keyword;
9251 /* Any of these indicate either a class-specifier, or an
9252 elaborated-type-specifier. */
9257 /* Parse tentatively so that we can back up if we don't find a
9258 class-specifier or enum-specifier. */
9259 cp_parser_parse_tentatively (parser);
9260 /* Look for the class-specifier or enum-specifier. */
9261 if (keyword == RID_ENUM)
9262 type_spec = cp_parser_enum_specifier (parser);
9264 type_spec = cp_parser_class_specifier (parser);
9266 /* If that worked, we're done. */
9267 if (cp_parser_parse_definitely (parser))
9269 if (declares_class_or_enum)
9270 *declares_class_or_enum = 2;
9272 cp_parser_set_decl_spec_type (decl_specs,
9274 /*user_defined_p=*/true);
9281 /* Look for an elaborated-type-specifier. */
9283 = (cp_parser_elaborated_type_specifier
9285 decl_specs && decl_specs->specs[(int) ds_friend],
9287 /* We're declaring a class or enum -- unless we're using
9289 if (declares_class_or_enum && keyword != RID_TYPENAME)
9290 *declares_class_or_enum = 1;
9292 cp_parser_set_decl_spec_type (decl_specs,
9294 /*user_defined_p=*/true);
9299 if (is_cv_qualifier)
9300 *is_cv_qualifier = true;
9305 if (is_cv_qualifier)
9306 *is_cv_qualifier = true;
9311 if (is_cv_qualifier)
9312 *is_cv_qualifier = true;
9316 /* The `__complex__' keyword is a GNU extension. */
9324 /* Handle simple keywords. */
9329 ++decl_specs->specs[(int)ds];
9330 decl_specs->any_specifiers_p = true;
9332 return cp_lexer_consume_token (parser->lexer)->value;
9335 /* If we do not already have a type-specifier, assume we are looking
9336 at a simple-type-specifier. */
9337 type_spec = cp_parser_simple_type_specifier (parser,
9341 /* If we didn't find a type-specifier, and a type-specifier was not
9342 optional in this context, issue an error message. */
9343 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9345 cp_parser_error (parser, "expected type specifier");
9346 return error_mark_node;
9352 /* Parse a simple-type-specifier.
9354 simple-type-specifier:
9355 :: [opt] nested-name-specifier [opt] type-name
9356 :: [opt] nested-name-specifier template template-id
9371 simple-type-specifier:
9372 __typeof__ unary-expression
9373 __typeof__ ( type-id )
9375 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9376 appropriately updated. */
9379 cp_parser_simple_type_specifier (cp_parser* parser,
9380 cp_decl_specifier_seq *decl_specs,
9381 cp_parser_flags flags)
9383 tree type = NULL_TREE;
9386 /* Peek at the next token. */
9387 token = cp_lexer_peek_token (parser->lexer);
9389 /* If we're looking at a keyword, things are easy. */
9390 switch (token->keyword)
9394 decl_specs->explicit_char_p = true;
9395 type = char_type_node;
9398 type = wchar_type_node;
9401 type = boolean_type_node;
9405 ++decl_specs->specs[(int) ds_short];
9406 type = short_integer_type_node;
9410 decl_specs->explicit_int_p = true;
9411 type = integer_type_node;
9415 ++decl_specs->specs[(int) ds_long];
9416 type = long_integer_type_node;
9420 ++decl_specs->specs[(int) ds_signed];
9421 type = integer_type_node;
9425 ++decl_specs->specs[(int) ds_unsigned];
9426 type = unsigned_type_node;
9429 type = float_type_node;
9432 type = double_type_node;
9435 type = void_type_node;
9439 /* Consume the `typeof' token. */
9440 cp_lexer_consume_token (parser->lexer);
9441 /* Parse the operand to `typeof'. */
9442 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9443 /* If it is not already a TYPE, take its type. */
9445 type = finish_typeof (type);
9448 cp_parser_set_decl_spec_type (decl_specs, type,
9449 /*user_defined_p=*/true);
9457 /* If the type-specifier was for a built-in type, we're done. */
9462 /* Record the type. */
9464 && (token->keyword != RID_SIGNED
9465 && token->keyword != RID_UNSIGNED
9466 && token->keyword != RID_SHORT
9467 && token->keyword != RID_LONG))
9468 cp_parser_set_decl_spec_type (decl_specs,
9470 /*user_defined=*/false);
9472 decl_specs->any_specifiers_p = true;
9474 /* Consume the token. */
9475 id = cp_lexer_consume_token (parser->lexer)->value;
9477 /* There is no valid C++ program where a non-template type is
9478 followed by a "<". That usually indicates that the user thought
9479 that the type was a template. */
9480 cp_parser_check_for_invalid_template_id (parser, type);
9482 return TYPE_NAME (type);
9485 /* The type-specifier must be a user-defined type. */
9486 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9491 /* Don't gobble tokens or issue error messages if this is an
9492 optional type-specifier. */
9493 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9494 cp_parser_parse_tentatively (parser);
9496 /* Look for the optional `::' operator. */
9498 = (cp_parser_global_scope_opt (parser,
9499 /*current_scope_valid_p=*/false)
9501 /* Look for the nested-name specifier. */
9503 = (cp_parser_nested_name_specifier_opt (parser,
9504 /*typename_keyword_p=*/false,
9505 /*check_dependency_p=*/true,
9507 /*is_declaration=*/false)
9509 /* If we have seen a nested-name-specifier, and the next token
9510 is `template', then we are using the template-id production. */
9512 && cp_parser_optional_template_keyword (parser))
9514 /* Look for the template-id. */
9515 type = cp_parser_template_id (parser,
9516 /*template_keyword_p=*/true,
9517 /*check_dependency_p=*/true,
9518 /*is_declaration=*/false);
9519 /* If the template-id did not name a type, we are out of
9521 if (TREE_CODE (type) != TYPE_DECL)
9523 cp_parser_error (parser, "expected template-id for type");
9527 /* Otherwise, look for a type-name. */
9529 type = cp_parser_type_name (parser);
9530 /* Keep track of all name-lookups performed in class scopes. */
9534 && TREE_CODE (type) == TYPE_DECL
9535 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9536 maybe_note_name_used_in_class (DECL_NAME (type), type);
9537 /* If it didn't work out, we don't have a TYPE. */
9538 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9539 && !cp_parser_parse_definitely (parser))
9541 if (type && decl_specs)
9542 cp_parser_set_decl_spec_type (decl_specs, type,
9543 /*user_defined=*/true);
9546 /* If we didn't get a type-name, issue an error message. */
9547 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9549 cp_parser_error (parser, "expected type-name");
9550 return error_mark_node;
9553 /* There is no valid C++ program where a non-template type is
9554 followed by a "<". That usually indicates that the user thought
9555 that the type was a template. */
9556 if (type && type != error_mark_node)
9557 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9562 /* Parse a type-name.
9575 Returns a TYPE_DECL for the the type. */
9578 cp_parser_type_name (cp_parser* parser)
9583 /* We can't know yet whether it is a class-name or not. */
9584 cp_parser_parse_tentatively (parser);
9585 /* Try a class-name. */
9586 type_decl = cp_parser_class_name (parser,
9587 /*typename_keyword_p=*/false,
9588 /*template_keyword_p=*/false,
9590 /*check_dependency_p=*/true,
9591 /*class_head_p=*/false,
9592 /*is_declaration=*/false);
9593 /* If it's not a class-name, keep looking. */
9594 if (!cp_parser_parse_definitely (parser))
9596 /* It must be a typedef-name or an enum-name. */
9597 identifier = cp_parser_identifier (parser);
9598 if (identifier == error_mark_node)
9599 return error_mark_node;
9601 /* Look up the type-name. */
9602 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9603 /* Issue an error if we did not find a type-name. */
9604 if (TREE_CODE (type_decl) != TYPE_DECL)
9606 if (!cp_parser_simulate_error (parser))
9607 cp_parser_name_lookup_error (parser, identifier, type_decl,
9609 type_decl = error_mark_node;
9611 /* Remember that the name was used in the definition of the
9612 current class so that we can check later to see if the
9613 meaning would have been different after the class was
9614 entirely defined. */
9615 else if (type_decl != error_mark_node
9617 maybe_note_name_used_in_class (identifier, type_decl);
9624 /* Parse an elaborated-type-specifier. Note that the grammar given
9625 here incorporates the resolution to DR68.
9627 elaborated-type-specifier:
9628 class-key :: [opt] nested-name-specifier [opt] identifier
9629 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9630 enum :: [opt] nested-name-specifier [opt] identifier
9631 typename :: [opt] nested-name-specifier identifier
9632 typename :: [opt] nested-name-specifier template [opt]
9637 elaborated-type-specifier:
9638 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9639 class-key attributes :: [opt] nested-name-specifier [opt]
9640 template [opt] template-id
9641 enum attributes :: [opt] nested-name-specifier [opt] identifier
9643 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9644 declared `friend'. If IS_DECLARATION is TRUE, then this
9645 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9646 something is being declared.
9648 Returns the TYPE specified. */
9651 cp_parser_elaborated_type_specifier (cp_parser* parser,
9653 bool is_declaration)
9655 enum tag_types tag_type;
9657 tree type = NULL_TREE;
9658 tree attributes = NULL_TREE;
9660 /* See if we're looking at the `enum' keyword. */
9661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9663 /* Consume the `enum' token. */
9664 cp_lexer_consume_token (parser->lexer);
9665 /* Remember that it's an enumeration type. */
9666 tag_type = enum_type;
9667 /* Parse the attributes. */
9668 attributes = cp_parser_attributes_opt (parser);
9670 /* Or, it might be `typename'. */
9671 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9674 /* Consume the `typename' token. */
9675 cp_lexer_consume_token (parser->lexer);
9676 /* Remember that it's a `typename' type. */
9677 tag_type = typename_type;
9678 /* The `typename' keyword is only allowed in templates. */
9679 if (!processing_template_decl)
9680 pedwarn ("using `typename' outside of template");
9682 /* Otherwise it must be a class-key. */
9685 tag_type = cp_parser_class_key (parser);
9686 if (tag_type == none_type)
9687 return error_mark_node;
9688 /* Parse the attributes. */
9689 attributes = cp_parser_attributes_opt (parser);
9692 /* Look for the `::' operator. */
9693 cp_parser_global_scope_opt (parser,
9694 /*current_scope_valid_p=*/false);
9695 /* Look for the nested-name-specifier. */
9696 if (tag_type == typename_type)
9698 if (cp_parser_nested_name_specifier (parser,
9699 /*typename_keyword_p=*/true,
9700 /*check_dependency_p=*/true,
9704 return error_mark_node;
9707 /* Even though `typename' is not present, the proposed resolution
9708 to Core Issue 180 says that in `class A<T>::B', `B' should be
9709 considered a type-name, even if `A<T>' is dependent. */
9710 cp_parser_nested_name_specifier_opt (parser,
9711 /*typename_keyword_p=*/true,
9712 /*check_dependency_p=*/true,
9715 /* For everything but enumeration types, consider a template-id. */
9716 if (tag_type != enum_type)
9718 bool template_p = false;
9721 /* Allow the `template' keyword. */
9722 template_p = cp_parser_optional_template_keyword (parser);
9723 /* If we didn't see `template', we don't know if there's a
9724 template-id or not. */
9726 cp_parser_parse_tentatively (parser);
9727 /* Parse the template-id. */
9728 decl = cp_parser_template_id (parser, template_p,
9729 /*check_dependency_p=*/true,
9731 /* If we didn't find a template-id, look for an ordinary
9733 if (!template_p && !cp_parser_parse_definitely (parser))
9735 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9736 in effect, then we must assume that, upon instantiation, the
9737 template will correspond to a class. */
9738 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9739 && tag_type == typename_type)
9740 type = make_typename_type (parser->scope, decl,
9743 type = TREE_TYPE (decl);
9746 /* For an enumeration type, consider only a plain identifier. */
9749 identifier = cp_parser_identifier (parser);
9751 if (identifier == error_mark_node)
9753 parser->scope = NULL_TREE;
9754 return error_mark_node;
9757 /* For a `typename', we needn't call xref_tag. */
9758 if (tag_type == typename_type)
9759 return cp_parser_make_typename_type (parser, parser->scope,
9761 /* Look up a qualified name in the usual way. */
9766 /* In an elaborated-type-specifier, names are assumed to name
9767 types, so we set IS_TYPE to TRUE when calling
9768 cp_parser_lookup_name. */
9769 decl = cp_parser_lookup_name (parser, identifier,
9771 /*is_template=*/false,
9772 /*is_namespace=*/false,
9773 /*check_dependency=*/true,
9774 /*ambiguous_p=*/NULL);
9776 /* If we are parsing friend declaration, DECL may be a
9777 TEMPLATE_DECL tree node here. However, we need to check
9778 whether this TEMPLATE_DECL results in valid code. Consider
9779 the following example:
9782 template <class T> class C {};
9785 template <class T> friend class N::C; // #1, valid code
9787 template <class T> class Y {
9788 friend class N::C; // #2, invalid code
9791 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9792 name lookup of `N::C'. We see that friend declaration must
9793 be template for the code to be valid. Note that
9794 processing_template_decl does not work here since it is
9795 always 1 for the above two cases. */
9797 decl = (cp_parser_maybe_treat_template_as_class
9798 (decl, /*tag_name_p=*/is_friend
9799 && parser->num_template_parameter_lists));
9801 if (TREE_CODE (decl) != TYPE_DECL)
9803 error ("expected type-name");
9804 return error_mark_node;
9807 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9808 check_elaborated_type_specifier
9810 (parser->num_template_parameter_lists
9811 || DECL_SELF_REFERENCE_P (decl)));
9813 type = TREE_TYPE (decl);
9817 /* An elaborated-type-specifier sometimes introduces a new type and
9818 sometimes names an existing type. Normally, the rule is that it
9819 introduces a new type only if there is not an existing type of
9820 the same name already in scope. For example, given:
9823 void f() { struct S s; }
9825 the `struct S' in the body of `f' is the same `struct S' as in
9826 the global scope; the existing definition is used. However, if
9827 there were no global declaration, this would introduce a new
9828 local class named `S'.
9830 An exception to this rule applies to the following code:
9832 namespace N { struct S; }
9834 Here, the elaborated-type-specifier names a new type
9835 unconditionally; even if there is already an `S' in the
9836 containing scope this declaration names a new type.
9837 This exception only applies if the elaborated-type-specifier
9838 forms the complete declaration:
9842 A declaration consisting solely of `class-key identifier ;' is
9843 either a redeclaration of the name in the current scope or a
9844 forward declaration of the identifier as a class name. It
9845 introduces the name into the current scope.
9847 We are in this situation precisely when the next token is a `;'.
9849 An exception to the exception is that a `friend' declaration does
9850 *not* name a new type; i.e., given:
9852 struct S { friend struct T; };
9854 `T' is not a new type in the scope of `S'.
9856 Also, `new struct S' or `sizeof (struct S)' never results in the
9857 definition of a new type; a new type can only be declared in a
9858 declaration context. */
9860 /* Warn about attributes. They are ignored. */
9862 warning ("type attributes are honored only at type definition");
9864 type = xref_tag (tag_type, identifier,
9867 || cp_lexer_next_token_is_not (parser->lexer,
9869 parser->num_template_parameter_lists);
9872 if (tag_type != enum_type)
9873 cp_parser_check_class_key (tag_type, type);
9875 /* A "<" cannot follow an elaborated type specifier. If that
9876 happens, the user was probably trying to form a template-id. */
9877 cp_parser_check_for_invalid_template_id (parser, type);
9882 /* Parse an enum-specifier.
9885 enum identifier [opt] { enumerator-list [opt] }
9887 Returns an ENUM_TYPE representing the enumeration. */
9890 cp_parser_enum_specifier (cp_parser* parser)
9893 tree identifier = NULL_TREE;
9896 /* Look for the `enum' keyword. */
9897 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9898 return error_mark_node;
9899 /* Peek at the next token. */
9900 token = cp_lexer_peek_token (parser->lexer);
9902 /* See if it is an identifier. */
9903 if (token->type == CPP_NAME)
9904 identifier = cp_parser_identifier (parser);
9906 /* Look for the `{'. */
9907 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9908 return error_mark_node;
9910 /* At this point, we're going ahead with the enum-specifier, even
9911 if some other problem occurs. */
9912 cp_parser_commit_to_tentative_parse (parser);
9914 /* Issue an error message if type-definitions are forbidden here. */
9915 cp_parser_check_type_definition (parser);
9917 /* Create the new type. */
9918 type = start_enum (identifier ? identifier : make_anon_name ());
9920 /* Peek at the next token. */
9921 token = cp_lexer_peek_token (parser->lexer);
9922 /* If it's not a `}', then there are some enumerators. */
9923 if (token->type != CPP_CLOSE_BRACE)
9924 cp_parser_enumerator_list (parser, type);
9925 /* Look for the `}'. */
9926 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9928 /* Finish up the enumeration. */
9934 /* Parse an enumerator-list. The enumerators all have the indicated
9938 enumerator-definition
9939 enumerator-list , enumerator-definition */
9942 cp_parser_enumerator_list (cp_parser* parser, tree type)
9948 /* Parse an enumerator-definition. */
9949 cp_parser_enumerator_definition (parser, type);
9950 /* Peek at the next token. */
9951 token = cp_lexer_peek_token (parser->lexer);
9952 /* If it's not a `,', then we've reached the end of the
9954 if (token->type != CPP_COMMA)
9956 /* Otherwise, consume the `,' and keep going. */
9957 cp_lexer_consume_token (parser->lexer);
9958 /* If the next token is a `}', there is a trailing comma. */
9959 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9961 if (pedantic && !in_system_header)
9962 pedwarn ("comma at end of enumerator list");
9968 /* Parse an enumerator-definition. The enumerator has the indicated
9971 enumerator-definition:
9973 enumerator = constant-expression
9979 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9985 /* Look for the identifier. */
9986 identifier = cp_parser_identifier (parser);
9987 if (identifier == error_mark_node)
9990 /* Peek at the next token. */
9991 token = cp_lexer_peek_token (parser->lexer);
9992 /* If it's an `=', then there's an explicit value. */
9993 if (token->type == CPP_EQ)
9995 /* Consume the `=' token. */
9996 cp_lexer_consume_token (parser->lexer);
9997 /* Parse the value. */
9998 value = cp_parser_constant_expression (parser,
9999 /*allow_non_constant_p=*/false,
10005 /* Create the enumerator. */
10006 build_enumerator (identifier, value, type);
10009 /* Parse a namespace-name.
10012 original-namespace-name
10015 Returns the NAMESPACE_DECL for the namespace. */
10018 cp_parser_namespace_name (cp_parser* parser)
10021 tree namespace_decl;
10023 /* Get the name of the namespace. */
10024 identifier = cp_parser_identifier (parser);
10025 if (identifier == error_mark_node)
10026 return error_mark_node;
10028 /* Look up the identifier in the currently active scope. Look only
10029 for namespaces, due to:
10031 [basic.lookup.udir]
10033 When looking up a namespace-name in a using-directive or alias
10034 definition, only namespace names are considered.
10038 [basic.lookup.qual]
10040 During the lookup of a name preceding the :: scope resolution
10041 operator, object, function, and enumerator names are ignored.
10043 (Note that cp_parser_class_or_namespace_name only calls this
10044 function if the token after the name is the scope resolution
10046 namespace_decl = cp_parser_lookup_name (parser, identifier,
10048 /*is_template=*/false,
10049 /*is_namespace=*/true,
10050 /*check_dependency=*/true,
10051 /*ambiguous_p=*/NULL);
10052 /* If it's not a namespace, issue an error. */
10053 if (namespace_decl == error_mark_node
10054 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10056 cp_parser_error (parser, "expected namespace-name");
10057 namespace_decl = error_mark_node;
10060 return namespace_decl;
10063 /* Parse a namespace-definition.
10065 namespace-definition:
10066 named-namespace-definition
10067 unnamed-namespace-definition
10069 named-namespace-definition:
10070 original-namespace-definition
10071 extension-namespace-definition
10073 original-namespace-definition:
10074 namespace identifier { namespace-body }
10076 extension-namespace-definition:
10077 namespace original-namespace-name { namespace-body }
10079 unnamed-namespace-definition:
10080 namespace { namespace-body } */
10083 cp_parser_namespace_definition (cp_parser* parser)
10087 /* Look for the `namespace' keyword. */
10088 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10090 /* Get the name of the namespace. We do not attempt to distinguish
10091 between an original-namespace-definition and an
10092 extension-namespace-definition at this point. The semantic
10093 analysis routines are responsible for that. */
10094 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10095 identifier = cp_parser_identifier (parser);
10097 identifier = NULL_TREE;
10099 /* Look for the `{' to start the namespace. */
10100 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10101 /* Start the namespace. */
10102 push_namespace (identifier);
10103 /* Parse the body of the namespace. */
10104 cp_parser_namespace_body (parser);
10105 /* Finish the namespace. */
10107 /* Look for the final `}'. */
10108 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10111 /* Parse a namespace-body.
10114 declaration-seq [opt] */
10117 cp_parser_namespace_body (cp_parser* parser)
10119 cp_parser_declaration_seq_opt (parser);
10122 /* Parse a namespace-alias-definition.
10124 namespace-alias-definition:
10125 namespace identifier = qualified-namespace-specifier ; */
10128 cp_parser_namespace_alias_definition (cp_parser* parser)
10131 tree namespace_specifier;
10133 /* Look for the `namespace' keyword. */
10134 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10135 /* Look for the identifier. */
10136 identifier = cp_parser_identifier (parser);
10137 if (identifier == error_mark_node)
10139 /* Look for the `=' token. */
10140 cp_parser_require (parser, CPP_EQ, "`='");
10141 /* Look for the qualified-namespace-specifier. */
10142 namespace_specifier
10143 = cp_parser_qualified_namespace_specifier (parser);
10144 /* Look for the `;' token. */
10145 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10147 /* Register the alias in the symbol table. */
10148 do_namespace_alias (identifier, namespace_specifier);
10151 /* Parse a qualified-namespace-specifier.
10153 qualified-namespace-specifier:
10154 :: [opt] nested-name-specifier [opt] namespace-name
10156 Returns a NAMESPACE_DECL corresponding to the specified
10160 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10162 /* Look for the optional `::'. */
10163 cp_parser_global_scope_opt (parser,
10164 /*current_scope_valid_p=*/false);
10166 /* Look for the optional nested-name-specifier. */
10167 cp_parser_nested_name_specifier_opt (parser,
10168 /*typename_keyword_p=*/false,
10169 /*check_dependency_p=*/true,
10171 /*is_declaration=*/true);
10173 return cp_parser_namespace_name (parser);
10176 /* Parse a using-declaration.
10179 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10180 using :: unqualified-id ; */
10183 cp_parser_using_declaration (cp_parser* parser)
10186 bool typename_p = false;
10187 bool global_scope_p;
10193 /* Look for the `using' keyword. */
10194 cp_parser_require_keyword (parser, RID_USING, "`using'");
10196 /* Peek at the next token. */
10197 token = cp_lexer_peek_token (parser->lexer);
10198 /* See if it's `typename'. */
10199 if (token->keyword == RID_TYPENAME)
10201 /* Remember that we've seen it. */
10203 /* Consume the `typename' token. */
10204 cp_lexer_consume_token (parser->lexer);
10207 /* Look for the optional global scope qualification. */
10209 = (cp_parser_global_scope_opt (parser,
10210 /*current_scope_valid_p=*/false)
10213 /* If we saw `typename', or didn't see `::', then there must be a
10214 nested-name-specifier present. */
10215 if (typename_p || !global_scope_p)
10216 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10217 /*check_dependency_p=*/true,
10219 /*is_declaration=*/true);
10220 /* Otherwise, we could be in either of the two productions. In that
10221 case, treat the nested-name-specifier as optional. */
10223 qscope = cp_parser_nested_name_specifier_opt (parser,
10224 /*typename_keyword_p=*/false,
10225 /*check_dependency_p=*/true,
10227 /*is_declaration=*/true);
10229 qscope = global_namespace;
10231 /* Parse the unqualified-id. */
10232 identifier = cp_parser_unqualified_id (parser,
10233 /*template_keyword_p=*/false,
10234 /*check_dependency_p=*/true,
10235 /*declarator_p=*/true);
10237 /* The function we call to handle a using-declaration is different
10238 depending on what scope we are in. */
10239 if (identifier == error_mark_node)
10241 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10242 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10243 /* [namespace.udecl]
10245 A using declaration shall not name a template-id. */
10246 error ("a template-id may not appear in a using-declaration");
10249 scope = current_scope ();
10250 if (scope && TYPE_P (scope))
10252 /* Create the USING_DECL. */
10253 decl = do_class_using_decl (build_nt (SCOPE_REF,
10256 /* Add it to the list of members in this class. */
10257 finish_member_declaration (decl);
10261 decl = cp_parser_lookup_name_simple (parser, identifier);
10262 if (decl == error_mark_node)
10263 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10265 do_local_using_decl (decl, qscope, identifier);
10267 do_toplevel_using_decl (decl, qscope, identifier);
10271 /* Look for the final `;'. */
10272 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10275 /* Parse a using-directive.
10278 using namespace :: [opt] nested-name-specifier [opt]
10279 namespace-name ; */
10282 cp_parser_using_directive (cp_parser* parser)
10284 tree namespace_decl;
10287 /* Look for the `using' keyword. */
10288 cp_parser_require_keyword (parser, RID_USING, "`using'");
10289 /* And the `namespace' keyword. */
10290 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10291 /* Look for the optional `::' operator. */
10292 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10293 /* And the optional nested-name-specifier. */
10294 cp_parser_nested_name_specifier_opt (parser,
10295 /*typename_keyword_p=*/false,
10296 /*check_dependency_p=*/true,
10298 /*is_declaration=*/true);
10299 /* Get the namespace being used. */
10300 namespace_decl = cp_parser_namespace_name (parser);
10301 /* And any specified attributes. */
10302 attribs = cp_parser_attributes_opt (parser);
10303 /* Update the symbol table. */
10304 parse_using_directive (namespace_decl, attribs);
10305 /* Look for the final `;'. */
10306 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10309 /* Parse an asm-definition.
10312 asm ( string-literal ) ;
10317 asm volatile [opt] ( string-literal ) ;
10318 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10319 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10320 : asm-operand-list [opt] ) ;
10321 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10322 : asm-operand-list [opt]
10323 : asm-operand-list [opt] ) ; */
10326 cp_parser_asm_definition (cp_parser* parser)
10330 tree outputs = NULL_TREE;
10331 tree inputs = NULL_TREE;
10332 tree clobbers = NULL_TREE;
10334 bool volatile_p = false;
10335 bool extended_p = false;
10337 /* Look for the `asm' keyword. */
10338 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10339 /* See if the next token is `volatile'. */
10340 if (cp_parser_allow_gnu_extensions_p (parser)
10341 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10343 /* Remember that we saw the `volatile' keyword. */
10345 /* Consume the token. */
10346 cp_lexer_consume_token (parser->lexer);
10348 /* Look for the opening `('. */
10349 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10350 /* Look for the string. */
10351 c_lex_string_translate = 0;
10352 token = cp_parser_require (parser, CPP_STRING, "asm body");
10355 string = token->value;
10356 /* If we're allowing GNU extensions, check for the extended assembly
10357 syntax. Unfortunately, the `:' tokens need not be separated by
10358 a space in C, and so, for compatibility, we tolerate that here
10359 too. Doing that means that we have to treat the `::' operator as
10361 if (cp_parser_allow_gnu_extensions_p (parser)
10362 && at_function_scope_p ()
10363 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10364 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10366 bool inputs_p = false;
10367 bool clobbers_p = false;
10369 /* The extended syntax was used. */
10372 /* Look for outputs. */
10373 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10375 /* Consume the `:'. */
10376 cp_lexer_consume_token (parser->lexer);
10377 /* Parse the output-operands. */
10378 if (cp_lexer_next_token_is_not (parser->lexer,
10380 && cp_lexer_next_token_is_not (parser->lexer,
10382 && cp_lexer_next_token_is_not (parser->lexer,
10384 outputs = cp_parser_asm_operand_list (parser);
10386 /* If the next token is `::', there are no outputs, and the
10387 next token is the beginning of the inputs. */
10388 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10389 /* The inputs are coming next. */
10392 /* Look for inputs. */
10394 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10396 /* Consume the `:' or `::'. */
10397 cp_lexer_consume_token (parser->lexer);
10398 /* Parse the output-operands. */
10399 if (cp_lexer_next_token_is_not (parser->lexer,
10401 && cp_lexer_next_token_is_not (parser->lexer,
10403 inputs = cp_parser_asm_operand_list (parser);
10405 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10406 /* The clobbers are coming next. */
10409 /* Look for clobbers. */
10411 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10413 /* Consume the `:' or `::'. */
10414 cp_lexer_consume_token (parser->lexer);
10415 /* Parse the clobbers. */
10416 if (cp_lexer_next_token_is_not (parser->lexer,
10418 clobbers = cp_parser_asm_clobber_list (parser);
10421 /* Look for the closing `)'. */
10422 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10423 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10424 /*consume_paren=*/true);
10425 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10427 /* Create the ASM_EXPR. */
10428 if (at_function_scope_p ())
10430 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10432 /* If the extended syntax was not used, mark the ASM_EXPR. */
10434 ASM_INPUT_P (asm_stmt) = 1;
10437 assemble_asm (string);
10440 c_lex_string_translate = 1;
10443 /* Declarators [gram.dcl.decl] */
10445 /* Parse an init-declarator.
10448 declarator initializer [opt]
10453 declarator asm-specification [opt] attributes [opt] initializer [opt]
10455 function-definition:
10456 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10458 decl-specifier-seq [opt] declarator function-try-block
10462 function-definition:
10463 __extension__ function-definition
10465 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10466 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10467 then this declarator appears in a class scope. The new DECL created
10468 by this declarator is returned.
10470 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10471 for a function-definition here as well. If the declarator is a
10472 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10473 be TRUE upon return. By that point, the function-definition will
10474 have been completely parsed.
10476 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10480 cp_parser_init_declarator (cp_parser* parser,
10481 cp_decl_specifier_seq *decl_specifiers,
10482 bool function_definition_allowed_p,
10484 int declares_class_or_enum,
10485 bool* function_definition_p)
10488 cp_declarator *declarator;
10489 tree prefix_attributes;
10491 tree asm_specification;
10493 tree decl = NULL_TREE;
10495 bool is_initialized;
10496 bool is_parenthesized_init;
10497 bool is_non_constant_init;
10498 int ctor_dtor_or_conv_p;
10500 bool pop_p = false;
10502 /* Gather the attributes that were provided with the
10503 decl-specifiers. */
10504 prefix_attributes = decl_specifiers->attributes;
10506 /* Assume that this is not the declarator for a function
10508 if (function_definition_p)
10509 *function_definition_p = false;
10511 /* Defer access checks while parsing the declarator; we cannot know
10512 what names are accessible until we know what is being
10514 resume_deferring_access_checks ();
10516 /* Parse the declarator. */
10518 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10519 &ctor_dtor_or_conv_p,
10520 /*parenthesized_p=*/NULL);
10521 /* Gather up the deferred checks. */
10522 stop_deferring_access_checks ();
10524 /* If the DECLARATOR was erroneous, there's no need to go
10526 if (declarator == cp_error_declarator)
10527 return error_mark_node;
10529 cp_parser_check_for_definition_in_return_type (declarator,
10530 declares_class_or_enum);
10532 /* Figure out what scope the entity declared by the DECLARATOR is
10533 located in. `grokdeclarator' sometimes changes the scope, so
10534 we compute it now. */
10535 scope = get_scope_of_declarator (declarator);
10537 /* If we're allowing GNU extensions, look for an asm-specification
10539 if (cp_parser_allow_gnu_extensions_p (parser))
10541 /* Look for an asm-specification. */
10542 asm_specification = cp_parser_asm_specification_opt (parser);
10543 /* And attributes. */
10544 attributes = cp_parser_attributes_opt (parser);
10548 asm_specification = NULL_TREE;
10549 attributes = NULL_TREE;
10552 /* Peek at the next token. */
10553 token = cp_lexer_peek_token (parser->lexer);
10554 /* Check to see if the token indicates the start of a
10555 function-definition. */
10556 if (cp_parser_token_starts_function_definition_p (token))
10558 if (!function_definition_allowed_p)
10560 /* If a function-definition should not appear here, issue an
10562 cp_parser_error (parser,
10563 "a function-definition is not allowed here");
10564 return error_mark_node;
10568 /* Neither attributes nor an asm-specification are allowed
10569 on a function-definition. */
10570 if (asm_specification)
10571 error ("an asm-specification is not allowed on a function-definition");
10573 error ("attributes are not allowed on a function-definition");
10574 /* This is a function-definition. */
10575 *function_definition_p = true;
10577 /* Parse the function definition. */
10579 decl = cp_parser_save_member_function_body (parser,
10582 prefix_attributes);
10585 = (cp_parser_function_definition_from_specifiers_and_declarator
10586 (parser, decl_specifiers, prefix_attributes, declarator));
10594 Only in function declarations for constructors, destructors, and
10595 type conversions can the decl-specifier-seq be omitted.
10597 We explicitly postpone this check past the point where we handle
10598 function-definitions because we tolerate function-definitions
10599 that are missing their return types in some modes. */
10600 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10602 cp_parser_error (parser,
10603 "expected constructor, destructor, or type conversion");
10604 return error_mark_node;
10607 /* An `=' or an `(' indicates an initializer. */
10608 is_initialized = (token->type == CPP_EQ
10609 || token->type == CPP_OPEN_PAREN);
10610 /* If the init-declarator isn't initialized and isn't followed by a
10611 `,' or `;', it's not a valid init-declarator. */
10612 if (!is_initialized
10613 && token->type != CPP_COMMA
10614 && token->type != CPP_SEMICOLON)
10616 cp_parser_error (parser, "expected init-declarator");
10617 return error_mark_node;
10620 /* Because start_decl has side-effects, we should only call it if we
10621 know we're going ahead. By this point, we know that we cannot
10622 possibly be looking at any other construct. */
10623 cp_parser_commit_to_tentative_parse (parser);
10625 /* If the decl specifiers were bad, issue an error now that we're
10626 sure this was intended to be a declarator. Then continue
10627 declaring the variable(s), as int, to try to cut down on further
10629 if (decl_specifiers->any_specifiers_p
10630 && decl_specifiers->type == error_mark_node)
10632 cp_parser_error (parser, "invalid type in declaration");
10633 decl_specifiers->type = integer_type_node;
10636 /* Check to see whether or not this declaration is a friend. */
10637 friend_p = cp_parser_friend_p (decl_specifiers);
10639 /* Check that the number of template-parameter-lists is OK. */
10640 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10641 return error_mark_node;
10643 /* Enter the newly declared entry in the symbol table. If we're
10644 processing a declaration in a class-specifier, we wait until
10645 after processing the initializer. */
10648 if (parser->in_unbraced_linkage_specification_p)
10650 decl_specifiers->storage_class = sc_extern;
10651 have_extern_spec = false;
10653 decl = start_decl (declarator, decl_specifiers,
10654 is_initialized, attributes, prefix_attributes,
10658 /* Enter the SCOPE. That way unqualified names appearing in the
10659 initializer will be looked up in SCOPE. */
10660 pop_p = push_scope (scope);
10662 /* Perform deferred access control checks, now that we know in which
10663 SCOPE the declared entity resides. */
10664 if (!member_p && decl)
10666 tree saved_current_function_decl = NULL_TREE;
10668 /* If the entity being declared is a function, pretend that we
10669 are in its scope. If it is a `friend', it may have access to
10670 things that would not otherwise be accessible. */
10671 if (TREE_CODE (decl) == FUNCTION_DECL)
10673 saved_current_function_decl = current_function_decl;
10674 current_function_decl = decl;
10677 /* Perform the access control checks for the declarator and the
10678 the decl-specifiers. */
10679 perform_deferred_access_checks ();
10681 /* Restore the saved value. */
10682 if (TREE_CODE (decl) == FUNCTION_DECL)
10683 current_function_decl = saved_current_function_decl;
10686 /* Parse the initializer. */
10687 if (is_initialized)
10688 initializer = cp_parser_initializer (parser,
10689 &is_parenthesized_init,
10690 &is_non_constant_init);
10693 initializer = NULL_TREE;
10694 is_parenthesized_init = false;
10695 is_non_constant_init = true;
10698 /* The old parser allows attributes to appear after a parenthesized
10699 initializer. Mark Mitchell proposed removing this functionality
10700 on the GCC mailing lists on 2002-08-13. This parser accepts the
10701 attributes -- but ignores them. */
10702 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10703 if (cp_parser_attributes_opt (parser))
10704 warning ("attributes after parenthesized initializer ignored");
10706 /* For an in-class declaration, use `grokfield' to create the
10712 decl = grokfield (declarator, decl_specifiers,
10713 initializer, /*asmspec=*/NULL_TREE,
10714 /*attributes=*/NULL_TREE);
10715 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10716 cp_parser_save_default_args (parser, decl);
10719 /* Finish processing the declaration. But, skip friend
10721 if (!friend_p && decl && decl != error_mark_node)
10723 cp_finish_decl (decl,
10726 /* If the initializer is in parentheses, then this is
10727 a direct-initialization, which means that an
10728 `explicit' constructor is OK. Otherwise, an
10729 `explicit' constructor cannot be used. */
10730 ((is_parenthesized_init || !is_initialized)
10731 ? 0 : LOOKUP_ONLYCONVERTING));
10733 pop_scope (DECL_CONTEXT (decl));
10736 /* Remember whether or not variables were initialized by
10737 constant-expressions. */
10738 if (decl && TREE_CODE (decl) == VAR_DECL
10739 && is_initialized && !is_non_constant_init)
10740 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10745 /* Parse a declarator.
10749 ptr-operator declarator
10751 abstract-declarator:
10752 ptr-operator abstract-declarator [opt]
10753 direct-abstract-declarator
10758 attributes [opt] direct-declarator
10759 attributes [opt] ptr-operator declarator
10761 abstract-declarator:
10762 attributes [opt] ptr-operator abstract-declarator [opt]
10763 attributes [opt] direct-abstract-declarator
10765 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10766 detect constructor, destructor or conversion operators. It is set
10767 to -1 if the declarator is a name, and +1 if it is a
10768 function. Otherwise it is set to zero. Usually you just want to
10769 test for >0, but internally the negative value is used.
10771 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10772 a decl-specifier-seq unless it declares a constructor, destructor,
10773 or conversion. It might seem that we could check this condition in
10774 semantic analysis, rather than parsing, but that makes it difficult
10775 to handle something like `f()'. We want to notice that there are
10776 no decl-specifiers, and therefore realize that this is an
10777 expression, not a declaration.)
10779 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10780 the declarator is a direct-declarator of the form "(...)". */
10782 static cp_declarator *
10783 cp_parser_declarator (cp_parser* parser,
10784 cp_parser_declarator_kind dcl_kind,
10785 int* ctor_dtor_or_conv_p,
10786 bool* parenthesized_p)
10789 cp_declarator *declarator;
10790 enum tree_code code;
10791 cp_cv_quals cv_quals;
10793 tree attributes = NULL_TREE;
10795 /* Assume this is not a constructor, destructor, or type-conversion
10797 if (ctor_dtor_or_conv_p)
10798 *ctor_dtor_or_conv_p = 0;
10800 if (cp_parser_allow_gnu_extensions_p (parser))
10801 attributes = cp_parser_attributes_opt (parser);
10803 /* Peek at the next token. */
10804 token = cp_lexer_peek_token (parser->lexer);
10806 /* Check for the ptr-operator production. */
10807 cp_parser_parse_tentatively (parser);
10808 /* Parse the ptr-operator. */
10809 code = cp_parser_ptr_operator (parser,
10812 /* If that worked, then we have a ptr-operator. */
10813 if (cp_parser_parse_definitely (parser))
10815 /* If a ptr-operator was found, then this declarator was not
10817 if (parenthesized_p)
10818 *parenthesized_p = true;
10819 /* The dependent declarator is optional if we are parsing an
10820 abstract-declarator. */
10821 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10822 cp_parser_parse_tentatively (parser);
10824 /* Parse the dependent declarator. */
10825 declarator = cp_parser_declarator (parser, dcl_kind,
10826 /*ctor_dtor_or_conv_p=*/NULL,
10827 /*parenthesized_p=*/NULL);
10829 /* If we are parsing an abstract-declarator, we must handle the
10830 case where the dependent declarator is absent. */
10831 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10832 && !cp_parser_parse_definitely (parser))
10835 /* Build the representation of the ptr-operator. */
10837 declarator = make_ptrmem_declarator (cv_quals,
10840 else if (code == INDIRECT_REF)
10841 declarator = make_pointer_declarator (cv_quals, declarator);
10843 declarator = make_reference_declarator (cv_quals, declarator);
10845 /* Everything else is a direct-declarator. */
10848 if (parenthesized_p)
10849 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10851 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10852 ctor_dtor_or_conv_p);
10855 if (attributes && declarator != cp_error_declarator)
10856 declarator->attributes = attributes;
10861 /* Parse a direct-declarator or direct-abstract-declarator.
10865 direct-declarator ( parameter-declaration-clause )
10866 cv-qualifier-seq [opt]
10867 exception-specification [opt]
10868 direct-declarator [ constant-expression [opt] ]
10871 direct-abstract-declarator:
10872 direct-abstract-declarator [opt]
10873 ( parameter-declaration-clause )
10874 cv-qualifier-seq [opt]
10875 exception-specification [opt]
10876 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10877 ( abstract-declarator )
10879 Returns a representation of the declarator. DCL_KIND is
10880 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10881 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10882 we are parsing a direct-declarator. It is
10883 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10884 of ambiguity we prefer an abstract declarator, as per
10885 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10886 cp_parser_declarator. */
10888 static cp_declarator *
10889 cp_parser_direct_declarator (cp_parser* parser,
10890 cp_parser_declarator_kind dcl_kind,
10891 int* ctor_dtor_or_conv_p)
10894 cp_declarator *declarator = NULL;
10895 tree scope = NULL_TREE;
10896 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10897 bool saved_in_declarator_p = parser->in_declarator_p;
10899 bool pop_p = false;
10903 /* Peek at the next token. */
10904 token = cp_lexer_peek_token (parser->lexer);
10905 if (token->type == CPP_OPEN_PAREN)
10907 /* This is either a parameter-declaration-clause, or a
10908 parenthesized declarator. When we know we are parsing a
10909 named declarator, it must be a parenthesized declarator
10910 if FIRST is true. For instance, `(int)' is a
10911 parameter-declaration-clause, with an omitted
10912 direct-abstract-declarator. But `((*))', is a
10913 parenthesized abstract declarator. Finally, when T is a
10914 template parameter `(T)' is a
10915 parameter-declaration-clause, and not a parenthesized
10918 We first try and parse a parameter-declaration-clause,
10919 and then try a nested declarator (if FIRST is true).
10921 It is not an error for it not to be a
10922 parameter-declaration-clause, even when FIRST is
10928 The first is the declaration of a function while the
10929 second is a the definition of a variable, including its
10932 Having seen only the parenthesis, we cannot know which of
10933 these two alternatives should be selected. Even more
10934 complex are examples like:
10939 The former is a function-declaration; the latter is a
10940 variable initialization.
10942 Thus again, we try a parameter-declaration-clause, and if
10943 that fails, we back out and return. */
10945 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10947 cp_parameter_declarator *params;
10948 unsigned saved_num_template_parameter_lists;
10950 cp_parser_parse_tentatively (parser);
10952 /* Consume the `('. */
10953 cp_lexer_consume_token (parser->lexer);
10956 /* If this is going to be an abstract declarator, we're
10957 in a declarator and we can't have default args. */
10958 parser->default_arg_ok_p = false;
10959 parser->in_declarator_p = true;
10962 /* Inside the function parameter list, surrounding
10963 template-parameter-lists do not apply. */
10964 saved_num_template_parameter_lists
10965 = parser->num_template_parameter_lists;
10966 parser->num_template_parameter_lists = 0;
10968 /* Parse the parameter-declaration-clause. */
10969 params = cp_parser_parameter_declaration_clause (parser);
10971 parser->num_template_parameter_lists
10972 = saved_num_template_parameter_lists;
10974 /* If all went well, parse the cv-qualifier-seq and the
10975 exception-specification. */
10976 if (cp_parser_parse_definitely (parser))
10978 cp_cv_quals cv_quals;
10979 tree exception_specification;
10981 if (ctor_dtor_or_conv_p)
10982 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10984 /* Consume the `)'. */
10985 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10987 /* Parse the cv-qualifier-seq. */
10988 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10989 /* And the exception-specification. */
10990 exception_specification
10991 = cp_parser_exception_specification_opt (parser);
10993 /* Create the function-declarator. */
10994 declarator = make_call_declarator (declarator,
10997 exception_specification);
10998 /* Any subsequent parameter lists are to do with
10999 return type, so are not those of the declared
11001 parser->default_arg_ok_p = false;
11003 /* Repeat the main loop. */
11008 /* If this is the first, we can try a parenthesized
11012 bool saved_in_type_id_in_expr_p;
11014 parser->default_arg_ok_p = saved_default_arg_ok_p;
11015 parser->in_declarator_p = saved_in_declarator_p;
11017 /* Consume the `('. */
11018 cp_lexer_consume_token (parser->lexer);
11019 /* Parse the nested declarator. */
11020 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11021 parser->in_type_id_in_expr_p = true;
11023 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11024 /*parenthesized_p=*/NULL);
11025 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11027 /* Expect a `)'. */
11028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11029 declarator = cp_error_declarator;
11030 if (declarator == cp_error_declarator)
11033 goto handle_declarator;
11035 /* Otherwise, we must be done. */
11039 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11040 && token->type == CPP_OPEN_SQUARE)
11042 /* Parse an array-declarator. */
11045 if (ctor_dtor_or_conv_p)
11046 *ctor_dtor_or_conv_p = 0;
11049 parser->default_arg_ok_p = false;
11050 parser->in_declarator_p = true;
11051 /* Consume the `['. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Peek at the next token. */
11054 token = cp_lexer_peek_token (parser->lexer);
11055 /* If the next token is `]', then there is no
11056 constant-expression. */
11057 if (token->type != CPP_CLOSE_SQUARE)
11059 bool non_constant_p;
11062 = cp_parser_constant_expression (parser,
11063 /*allow_non_constant=*/true,
11065 if (!non_constant_p)
11066 bounds = fold_non_dependent_expr (bounds);
11069 bounds = NULL_TREE;
11070 /* Look for the closing `]'. */
11071 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11073 declarator = cp_error_declarator;
11077 declarator = make_array_declarator (declarator, bounds);
11079 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11083 /* Parse a declarator-id */
11084 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11085 cp_parser_parse_tentatively (parser);
11086 id = cp_parser_declarator_id (parser);
11087 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11089 if (!cp_parser_parse_definitely (parser))
11090 id = error_mark_node;
11091 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11093 cp_parser_error (parser, "expected unqualified-id");
11094 id = error_mark_node;
11098 if (id == error_mark_node)
11100 declarator = cp_error_declarator;
11104 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11106 tree scope = TREE_OPERAND (id, 0);
11108 /* In the declaration of a member of a template class
11109 outside of the class itself, the SCOPE will sometimes
11110 be a TYPENAME_TYPE. For example, given:
11112 template <typename T>
11113 int S<T>::R::i = 3;
11115 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11116 this context, we must resolve S<T>::R to an ordinary
11117 type, rather than a typename type.
11119 The reason we normally avoid resolving TYPENAME_TYPEs
11120 is that a specialization of `S' might render
11121 `S<T>::R' not a type. However, if `S' is
11122 specialized, then this `i' will not be used, so there
11123 is no harm in resolving the types here. */
11124 if (TREE_CODE (scope) == TYPENAME_TYPE)
11128 /* Resolve the TYPENAME_TYPE. */
11129 type = resolve_typename_type (scope,
11130 /*only_current_p=*/false);
11131 /* If that failed, the declarator is invalid. */
11132 if (type == error_mark_node)
11133 error ("`%T::%D' is not a type",
11134 TYPE_CONTEXT (scope),
11135 TYPE_IDENTIFIER (scope));
11136 /* Build a new DECLARATOR. */
11137 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11141 declarator = make_id_declarator (id);
11145 tree unqualified_name;
11147 if (TREE_CODE (id) == SCOPE_REF
11148 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11150 class_type = TREE_OPERAND (id, 0);
11151 unqualified_name = TREE_OPERAND (id, 1);
11155 class_type = current_class_type;
11156 unqualified_name = id;
11161 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11162 declarator->u.id.sfk = sfk_destructor;
11163 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11164 declarator->u.id.sfk = sfk_conversion;
11165 else if (constructor_name_p (unqualified_name,
11167 || (TREE_CODE (unqualified_name) == TYPE_DECL
11168 && same_type_p (TREE_TYPE (unqualified_name),
11170 declarator->u.id.sfk = sfk_constructor;
11172 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11173 *ctor_dtor_or_conv_p = -1;
11174 if (TREE_CODE (id) == SCOPE_REF
11175 && TREE_CODE (unqualified_name) == TYPE_DECL
11176 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11178 error ("invalid use of constructor as a template");
11179 inform ("use `%T::%D' instead of `%T::%T' to name the "
11180 "constructor in a qualified name", class_type,
11181 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11182 class_type, class_type);
11187 handle_declarator:;
11188 scope = get_scope_of_declarator (declarator);
11190 /* Any names that appear after the declarator-id for a
11191 member are looked up in the containing scope. */
11192 pop_p = push_scope (scope);
11193 parser->in_declarator_p = true;
11194 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11195 || (declarator && declarator->kind == cdk_id))
11196 /* Default args are only allowed on function
11198 parser->default_arg_ok_p = saved_default_arg_ok_p;
11200 parser->default_arg_ok_p = false;
11209 /* For an abstract declarator, we might wind up with nothing at this
11210 point. That's an error; the declarator is not optional. */
11212 cp_parser_error (parser, "expected declarator");
11214 /* If we entered a scope, we must exit it now. */
11218 parser->default_arg_ok_p = saved_default_arg_ok_p;
11219 parser->in_declarator_p = saved_in_declarator_p;
11224 /* Parse a ptr-operator.
11227 * cv-qualifier-seq [opt]
11229 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11234 & cv-qualifier-seq [opt]
11236 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11237 Returns ADDR_EXPR if a reference was used. In the case of a
11238 pointer-to-member, *TYPE is filled in with the TYPE containing the
11239 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11240 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11241 ERROR_MARK if an error occurred. */
11243 static enum tree_code
11244 cp_parser_ptr_operator (cp_parser* parser,
11246 cp_cv_quals *cv_quals)
11248 enum tree_code code = ERROR_MARK;
11251 /* Assume that it's not a pointer-to-member. */
11253 /* And that there are no cv-qualifiers. */
11254 *cv_quals = TYPE_UNQUALIFIED;
11256 /* Peek at the next token. */
11257 token = cp_lexer_peek_token (parser->lexer);
11258 /* If it's a `*' or `&' we have a pointer or reference. */
11259 if (token->type == CPP_MULT || token->type == CPP_AND)
11261 /* Remember which ptr-operator we were processing. */
11262 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11264 /* Consume the `*' or `&'. */
11265 cp_lexer_consume_token (parser->lexer);
11267 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11268 `&', if we are allowing GNU extensions. (The only qualifier
11269 that can legally appear after `&' is `restrict', but that is
11270 enforced during semantic analysis. */
11271 if (code == INDIRECT_REF
11272 || cp_parser_allow_gnu_extensions_p (parser))
11273 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11277 /* Try the pointer-to-member case. */
11278 cp_parser_parse_tentatively (parser);
11279 /* Look for the optional `::' operator. */
11280 cp_parser_global_scope_opt (parser,
11281 /*current_scope_valid_p=*/false);
11282 /* Look for the nested-name specifier. */
11283 cp_parser_nested_name_specifier (parser,
11284 /*typename_keyword_p=*/false,
11285 /*check_dependency_p=*/true,
11287 /*is_declaration=*/false);
11288 /* If we found it, and the next token is a `*', then we are
11289 indeed looking at a pointer-to-member operator. */
11290 if (!cp_parser_error_occurred (parser)
11291 && cp_parser_require (parser, CPP_MULT, "`*'"))
11293 /* The type of which the member is a member is given by the
11295 *type = parser->scope;
11296 /* The next name will not be qualified. */
11297 parser->scope = NULL_TREE;
11298 parser->qualifying_scope = NULL_TREE;
11299 parser->object_scope = NULL_TREE;
11300 /* Indicate that the `*' operator was used. */
11301 code = INDIRECT_REF;
11302 /* Look for the optional cv-qualifier-seq. */
11303 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11305 /* If that didn't work we don't have a ptr-operator. */
11306 if (!cp_parser_parse_definitely (parser))
11307 cp_parser_error (parser, "expected ptr-operator");
11313 /* Parse an (optional) cv-qualifier-seq.
11316 cv-qualifier cv-qualifier-seq [opt]
11327 Returns a bitmask representing the cv-qualifiers. */
11330 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11332 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11337 cp_cv_quals cv_qualifier;
11339 /* Peek at the next token. */
11340 token = cp_lexer_peek_token (parser->lexer);
11341 /* See if it's a cv-qualifier. */
11342 switch (token->keyword)
11345 cv_qualifier = TYPE_QUAL_CONST;
11349 cv_qualifier = TYPE_QUAL_VOLATILE;
11353 cv_qualifier = TYPE_QUAL_RESTRICT;
11357 cv_qualifier = TYPE_UNQUALIFIED;
11364 if (cv_quals & cv_qualifier)
11366 error ("duplicate cv-qualifier");
11367 cp_lexer_purge_token (parser->lexer);
11371 cp_lexer_consume_token (parser->lexer);
11372 cv_quals |= cv_qualifier;
11379 /* Parse a declarator-id.
11383 :: [opt] nested-name-specifier [opt] type-name
11385 In the `id-expression' case, the value returned is as for
11386 cp_parser_id_expression if the id-expression was an unqualified-id.
11387 If the id-expression was a qualified-id, then a SCOPE_REF is
11388 returned. The first operand is the scope (either a NAMESPACE_DECL
11389 or TREE_TYPE), but the second is still just a representation of an
11393 cp_parser_declarator_id (cp_parser* parser)
11395 tree id_expression;
11397 /* The expression must be an id-expression. Assume that qualified
11398 names are the names of types so that:
11401 int S<T>::R::i = 3;
11403 will work; we must treat `S<T>::R' as the name of a type.
11404 Similarly, assume that qualified names are templates, where
11408 int S<T>::R<T>::i = 3;
11411 id_expression = cp_parser_id_expression (parser,
11412 /*template_keyword_p=*/false,
11413 /*check_dependency_p=*/false,
11414 /*template_p=*/NULL,
11415 /*declarator_p=*/true);
11416 /* If the name was qualified, create a SCOPE_REF to represent
11420 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11421 parser->scope = NULL_TREE;
11424 return id_expression;
11427 /* Parse a type-id.
11430 type-specifier-seq abstract-declarator [opt]
11432 Returns the TYPE specified. */
11435 cp_parser_type_id (cp_parser* parser)
11437 cp_decl_specifier_seq type_specifier_seq;
11438 cp_declarator *abstract_declarator;
11440 /* Parse the type-specifier-seq. */
11441 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11442 if (type_specifier_seq.type == error_mark_node)
11443 return error_mark_node;
11445 /* There might or might not be an abstract declarator. */
11446 cp_parser_parse_tentatively (parser);
11447 /* Look for the declarator. */
11448 abstract_declarator
11449 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11450 /*parenthesized_p=*/NULL);
11451 /* Check to see if there really was a declarator. */
11452 if (!cp_parser_parse_definitely (parser))
11453 abstract_declarator = NULL;
11455 return groktypename (&type_specifier_seq, abstract_declarator);
11458 /* Parse a type-specifier-seq.
11460 type-specifier-seq:
11461 type-specifier type-specifier-seq [opt]
11465 type-specifier-seq:
11466 attributes type-specifier-seq [opt]
11468 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11471 cp_parser_type_specifier_seq (cp_parser* parser,
11472 cp_decl_specifier_seq *type_specifier_seq)
11474 bool seen_type_specifier = false;
11476 /* Clear the TYPE_SPECIFIER_SEQ. */
11477 clear_decl_specs (type_specifier_seq);
11479 /* Parse the type-specifiers and attributes. */
11482 tree type_specifier;
11484 /* Check for attributes first. */
11485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11487 type_specifier_seq->attributes =
11488 chainon (type_specifier_seq->attributes,
11489 cp_parser_attributes_opt (parser));
11493 /* Look for the type-specifier. */
11494 type_specifier = cp_parser_type_specifier (parser,
11495 CP_PARSER_FLAGS_OPTIONAL,
11496 type_specifier_seq,
11497 /*is_declaration=*/false,
11500 /* If the first type-specifier could not be found, this is not a
11501 type-specifier-seq at all. */
11502 if (!seen_type_specifier && !type_specifier)
11504 cp_parser_error (parser, "expected type-specifier");
11505 type_specifier_seq->type = error_mark_node;
11508 /* If subsequent type-specifiers could not be found, the
11509 type-specifier-seq is complete. */
11510 else if (seen_type_specifier && !type_specifier)
11513 seen_type_specifier = true;
11519 /* Parse a parameter-declaration-clause.
11521 parameter-declaration-clause:
11522 parameter-declaration-list [opt] ... [opt]
11523 parameter-declaration-list , ...
11525 Returns a representation for the parameter declarations. A return
11526 value of NULL indicates a parameter-declaration-clause consisting
11527 only of an ellipsis. */
11529 static cp_parameter_declarator *
11530 cp_parser_parameter_declaration_clause (cp_parser* parser)
11532 cp_parameter_declarator *parameters;
11537 /* Peek at the next token. */
11538 token = cp_lexer_peek_token (parser->lexer);
11539 /* Check for trivial parameter-declaration-clauses. */
11540 if (token->type == CPP_ELLIPSIS)
11542 /* Consume the `...' token. */
11543 cp_lexer_consume_token (parser->lexer);
11546 else if (token->type == CPP_CLOSE_PAREN)
11547 /* There are no parameters. */
11549 #ifndef NO_IMPLICIT_EXTERN_C
11550 if (in_system_header && current_class_type == NULL
11551 && current_lang_name == lang_name_c)
11555 return no_parameters;
11557 /* Check for `(void)', too, which is a special case. */
11558 else if (token->keyword == RID_VOID
11559 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11560 == CPP_CLOSE_PAREN))
11562 /* Consume the `void' token. */
11563 cp_lexer_consume_token (parser->lexer);
11564 /* There are no parameters. */
11565 return no_parameters;
11568 /* Parse the parameter-declaration-list. */
11569 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11570 /* If a parse error occurred while parsing the
11571 parameter-declaration-list, then the entire
11572 parameter-declaration-clause is erroneous. */
11576 /* Peek at the next token. */
11577 token = cp_lexer_peek_token (parser->lexer);
11578 /* If it's a `,', the clause should terminate with an ellipsis. */
11579 if (token->type == CPP_COMMA)
11581 /* Consume the `,'. */
11582 cp_lexer_consume_token (parser->lexer);
11583 /* Expect an ellipsis. */
11585 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11587 /* It might also be `...' if the optional trailing `,' was
11589 else if (token->type == CPP_ELLIPSIS)
11591 /* Consume the `...' token. */
11592 cp_lexer_consume_token (parser->lexer);
11593 /* And remember that we saw it. */
11597 ellipsis_p = false;
11599 /* Finish the parameter list. */
11600 if (parameters && ellipsis_p)
11601 parameters->ellipsis_p = true;
11606 /* Parse a parameter-declaration-list.
11608 parameter-declaration-list:
11609 parameter-declaration
11610 parameter-declaration-list , parameter-declaration
11612 Returns a representation of the parameter-declaration-list, as for
11613 cp_parser_parameter_declaration_clause. However, the
11614 `void_list_node' is never appended to the list. Upon return,
11615 *IS_ERROR will be true iff an error occurred. */
11617 static cp_parameter_declarator *
11618 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11620 cp_parameter_declarator *parameters = NULL;
11621 cp_parameter_declarator **tail = ¶meters;
11623 /* Assume all will go well. */
11626 /* Look for more parameters. */
11629 cp_parameter_declarator *parameter;
11630 bool parenthesized_p;
11631 /* Parse the parameter. */
11633 = cp_parser_parameter_declaration (parser,
11634 /*template_parm_p=*/false,
11637 /* If a parse error occurred parsing the parameter declaration,
11638 then the entire parameter-declaration-list is erroneous. */
11645 /* Add the new parameter to the list. */
11647 tail = ¶meter->next;
11649 /* Peek at the next token. */
11650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11651 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11652 /* The parameter-declaration-list is complete. */
11654 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11658 /* Peek at the next token. */
11659 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11660 /* If it's an ellipsis, then the list is complete. */
11661 if (token->type == CPP_ELLIPSIS)
11663 /* Otherwise, there must be more parameters. Consume the
11665 cp_lexer_consume_token (parser->lexer);
11666 /* When parsing something like:
11668 int i(float f, double d)
11670 we can tell after seeing the declaration for "f" that we
11671 are not looking at an initialization of a variable "i",
11672 but rather at the declaration of a function "i".
11674 Due to the fact that the parsing of template arguments
11675 (as specified to a template-id) requires backtracking we
11676 cannot use this technique when inside a template argument
11678 if (!parser->in_template_argument_list_p
11679 && !parser->in_type_id_in_expr_p
11680 && cp_parser_parsing_tentatively (parser)
11681 && !cp_parser_committed_to_tentative_parse (parser)
11682 /* However, a parameter-declaration of the form
11683 "foat(f)" (which is a valid declaration of a
11684 parameter "f") can also be interpreted as an
11685 expression (the conversion of "f" to "float"). */
11686 && !parenthesized_p)
11687 cp_parser_commit_to_tentative_parse (parser);
11691 cp_parser_error (parser, "expected `,' or `...'");
11692 if (!cp_parser_parsing_tentatively (parser)
11693 || cp_parser_committed_to_tentative_parse (parser))
11694 cp_parser_skip_to_closing_parenthesis (parser,
11695 /*recovering=*/true,
11696 /*or_comma=*/false,
11697 /*consume_paren=*/false);
11705 /* Parse a parameter declaration.
11707 parameter-declaration:
11708 decl-specifier-seq declarator
11709 decl-specifier-seq declarator = assignment-expression
11710 decl-specifier-seq abstract-declarator [opt]
11711 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11713 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11714 declares a template parameter. (In that case, a non-nested `>'
11715 token encountered during the parsing of the assignment-expression
11716 is not interpreted as a greater-than operator.)
11718 Returns a representation of the parameter, or NULL if an error
11719 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11720 true iff the declarator is of the form "(p)". */
11722 static cp_parameter_declarator *
11723 cp_parser_parameter_declaration (cp_parser *parser,
11724 bool template_parm_p,
11725 bool *parenthesized_p)
11727 int declares_class_or_enum;
11728 bool greater_than_is_operator_p;
11729 cp_decl_specifier_seq decl_specifiers;
11730 cp_declarator *declarator;
11731 tree default_argument;
11733 const char *saved_message;
11735 /* In a template parameter, `>' is not an operator.
11739 When parsing a default template-argument for a non-type
11740 template-parameter, the first non-nested `>' is taken as the end
11741 of the template parameter-list rather than a greater-than
11743 greater_than_is_operator_p = !template_parm_p;
11745 /* Type definitions may not appear in parameter types. */
11746 saved_message = parser->type_definition_forbidden_message;
11747 parser->type_definition_forbidden_message
11748 = "types may not be defined in parameter types";
11750 /* Parse the declaration-specifiers. */
11751 cp_parser_decl_specifier_seq (parser,
11752 CP_PARSER_FLAGS_NONE,
11754 &declares_class_or_enum);
11755 /* If an error occurred, there's no reason to attempt to parse the
11756 rest of the declaration. */
11757 if (cp_parser_error_occurred (parser))
11759 parser->type_definition_forbidden_message = saved_message;
11763 /* Peek at the next token. */
11764 token = cp_lexer_peek_token (parser->lexer);
11765 /* If the next token is a `)', `,', `=', `>', or `...', then there
11766 is no declarator. */
11767 if (token->type == CPP_CLOSE_PAREN
11768 || token->type == CPP_COMMA
11769 || token->type == CPP_EQ
11770 || token->type == CPP_ELLIPSIS
11771 || token->type == CPP_GREATER)
11774 if (parenthesized_p)
11775 *parenthesized_p = false;
11777 /* Otherwise, there should be a declarator. */
11780 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11781 parser->default_arg_ok_p = false;
11783 /* After seeing a decl-specifier-seq, if the next token is not a
11784 "(", there is no possibility that the code is a valid
11785 expression. Therefore, if parsing tentatively, we commit at
11787 if (!parser->in_template_argument_list_p
11788 /* In an expression context, having seen:
11792 we cannot be sure whether we are looking at a
11793 function-type (taking a "char" as a parameter) or a cast
11794 of some object of type "char" to "int". */
11795 && !parser->in_type_id_in_expr_p
11796 && cp_parser_parsing_tentatively (parser)
11797 && !cp_parser_committed_to_tentative_parse (parser)
11798 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11799 cp_parser_commit_to_tentative_parse (parser);
11800 /* Parse the declarator. */
11801 declarator = cp_parser_declarator (parser,
11802 CP_PARSER_DECLARATOR_EITHER,
11803 /*ctor_dtor_or_conv_p=*/NULL,
11805 parser->default_arg_ok_p = saved_default_arg_ok_p;
11806 /* After the declarator, allow more attributes. */
11807 decl_specifiers.attributes
11808 = chainon (decl_specifiers.attributes,
11809 cp_parser_attributes_opt (parser));
11812 /* The restriction on defining new types applies only to the type
11813 of the parameter, not to the default argument. */
11814 parser->type_definition_forbidden_message = saved_message;
11816 /* If the next token is `=', then process a default argument. */
11817 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11819 bool saved_greater_than_is_operator_p;
11820 /* Consume the `='. */
11821 cp_lexer_consume_token (parser->lexer);
11823 /* If we are defining a class, then the tokens that make up the
11824 default argument must be saved and processed later. */
11825 if (!template_parm_p && at_class_scope_p ()
11826 && TYPE_BEING_DEFINED (current_class_type))
11828 unsigned depth = 0;
11830 /* Create a DEFAULT_ARG to represented the unparsed default
11832 default_argument = make_node (DEFAULT_ARG);
11833 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11835 /* Add tokens until we have processed the entire default
11842 /* Peek at the next token. */
11843 token = cp_lexer_peek_token (parser->lexer);
11844 /* What we do depends on what token we have. */
11845 switch (token->type)
11847 /* In valid code, a default argument must be
11848 immediately followed by a `,' `)', or `...'. */
11850 case CPP_CLOSE_PAREN:
11852 /* If we run into a non-nested `;', `}', or `]',
11853 then the code is invalid -- but the default
11854 argument is certainly over. */
11855 case CPP_SEMICOLON:
11856 case CPP_CLOSE_BRACE:
11857 case CPP_CLOSE_SQUARE:
11860 /* Update DEPTH, if necessary. */
11861 else if (token->type == CPP_CLOSE_PAREN
11862 || token->type == CPP_CLOSE_BRACE
11863 || token->type == CPP_CLOSE_SQUARE)
11867 case CPP_OPEN_PAREN:
11868 case CPP_OPEN_SQUARE:
11869 case CPP_OPEN_BRACE:
11874 /* If we see a non-nested `>', and `>' is not an
11875 operator, then it marks the end of the default
11877 if (!depth && !greater_than_is_operator_p)
11881 /* If we run out of tokens, issue an error message. */
11883 error ("file ends in default argument");
11889 /* In these cases, we should look for template-ids.
11890 For example, if the default argument is
11891 `X<int, double>()', we need to do name lookup to
11892 figure out whether or not `X' is a template; if
11893 so, the `,' does not end the default argument.
11895 That is not yet done. */
11902 /* If we've reached the end, stop. */
11906 /* Add the token to the token block. */
11907 token = cp_lexer_consume_token (parser->lexer);
11908 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11912 /* Outside of a class definition, we can just parse the
11913 assignment-expression. */
11916 bool saved_local_variables_forbidden_p;
11918 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11920 saved_greater_than_is_operator_p
11921 = parser->greater_than_is_operator_p;
11922 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11923 /* Local variable names (and the `this' keyword) may not
11924 appear in a default argument. */
11925 saved_local_variables_forbidden_p
11926 = parser->local_variables_forbidden_p;
11927 parser->local_variables_forbidden_p = true;
11928 /* Parse the assignment-expression. */
11929 default_argument = cp_parser_assignment_expression (parser);
11930 /* Restore saved state. */
11931 parser->greater_than_is_operator_p
11932 = saved_greater_than_is_operator_p;
11933 parser->local_variables_forbidden_p
11934 = saved_local_variables_forbidden_p;
11936 if (!parser->default_arg_ok_p)
11938 if (!flag_pedantic_errors)
11939 warning ("deprecated use of default argument for parameter of non-function");
11942 error ("default arguments are only permitted for function parameters");
11943 default_argument = NULL_TREE;
11948 default_argument = NULL_TREE;
11950 return make_parameter_declarator (&decl_specifiers,
11955 /* Parse a function-body.
11958 compound_statement */
11961 cp_parser_function_body (cp_parser *parser)
11963 cp_parser_compound_statement (parser, NULL, false);
11966 /* Parse a ctor-initializer-opt followed by a function-body. Return
11967 true if a ctor-initializer was present. */
11970 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11973 bool ctor_initializer_p;
11975 /* Begin the function body. */
11976 body = begin_function_body ();
11977 /* Parse the optional ctor-initializer. */
11978 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11979 /* Parse the function-body. */
11980 cp_parser_function_body (parser);
11981 /* Finish the function body. */
11982 finish_function_body (body);
11984 return ctor_initializer_p;
11987 /* Parse an initializer.
11990 = initializer-clause
11991 ( expression-list )
11993 Returns a expression representing the initializer. If no
11994 initializer is present, NULL_TREE is returned.
11996 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11997 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11998 set to FALSE if there is no initializer present. If there is an
11999 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12000 is set to true; otherwise it is set to false. */
12003 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12004 bool* non_constant_p)
12009 /* Peek at the next token. */
12010 token = cp_lexer_peek_token (parser->lexer);
12012 /* Let our caller know whether or not this initializer was
12014 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12015 /* Assume that the initializer is constant. */
12016 *non_constant_p = false;
12018 if (token->type == CPP_EQ)
12020 /* Consume the `='. */
12021 cp_lexer_consume_token (parser->lexer);
12022 /* Parse the initializer-clause. */
12023 init = cp_parser_initializer_clause (parser, non_constant_p);
12025 else if (token->type == CPP_OPEN_PAREN)
12026 init = cp_parser_parenthesized_expression_list (parser, false,
12030 /* Anything else is an error. */
12031 cp_parser_error (parser, "expected initializer");
12032 init = error_mark_node;
12038 /* Parse an initializer-clause.
12040 initializer-clause:
12041 assignment-expression
12042 { initializer-list , [opt] }
12045 Returns an expression representing the initializer.
12047 If the `assignment-expression' production is used the value
12048 returned is simply a representation for the expression.
12050 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12051 the elements of the initializer-list (or NULL_TREE, if the last
12052 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12053 NULL_TREE. There is no way to detect whether or not the optional
12054 trailing `,' was provided. NON_CONSTANT_P is as for
12055 cp_parser_initializer. */
12058 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12062 /* If it is not a `{', then we are looking at an
12063 assignment-expression. */
12064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12067 = cp_parser_constant_expression (parser,
12068 /*allow_non_constant_p=*/true,
12070 if (!*non_constant_p)
12071 initializer = fold_non_dependent_expr (initializer);
12075 /* Consume the `{' token. */
12076 cp_lexer_consume_token (parser->lexer);
12077 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12078 initializer = make_node (CONSTRUCTOR);
12079 /* If it's not a `}', then there is a non-trivial initializer. */
12080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12082 /* Parse the initializer list. */
12083 CONSTRUCTOR_ELTS (initializer)
12084 = cp_parser_initializer_list (parser, non_constant_p);
12085 /* A trailing `,' token is allowed. */
12086 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12087 cp_lexer_consume_token (parser->lexer);
12089 /* Now, there should be a trailing `}'. */
12090 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12093 return initializer;
12096 /* Parse an initializer-list.
12100 initializer-list , initializer-clause
12105 identifier : initializer-clause
12106 initializer-list, identifier : initializer-clause
12108 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12109 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12110 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12111 as for cp_parser_initializer. */
12114 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12116 tree initializers = NULL_TREE;
12118 /* Assume all of the expressions are constant. */
12119 *non_constant_p = false;
12121 /* Parse the rest of the list. */
12127 bool clause_non_constant_p;
12129 /* If the next token is an identifier and the following one is a
12130 colon, we are looking at the GNU designated-initializer
12132 if (cp_parser_allow_gnu_extensions_p (parser)
12133 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12134 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12136 /* Consume the identifier. */
12137 identifier = cp_lexer_consume_token (parser->lexer)->value;
12138 /* Consume the `:'. */
12139 cp_lexer_consume_token (parser->lexer);
12142 identifier = NULL_TREE;
12144 /* Parse the initializer. */
12145 initializer = cp_parser_initializer_clause (parser,
12146 &clause_non_constant_p);
12147 /* If any clause is non-constant, so is the entire initializer. */
12148 if (clause_non_constant_p)
12149 *non_constant_p = true;
12150 /* Add it to the list. */
12151 initializers = tree_cons (identifier, initializer, initializers);
12153 /* If the next token is not a comma, we have reached the end of
12155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12158 /* Peek at the next token. */
12159 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12160 /* If the next token is a `}', then we're still done. An
12161 initializer-clause can have a trailing `,' after the
12162 initializer-list and before the closing `}'. */
12163 if (token->type == CPP_CLOSE_BRACE)
12166 /* Consume the `,' token. */
12167 cp_lexer_consume_token (parser->lexer);
12170 /* The initializers were built up in reverse order, so we need to
12171 reverse them now. */
12172 return nreverse (initializers);
12175 /* Classes [gram.class] */
12177 /* Parse a class-name.
12183 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12184 to indicate that names looked up in dependent types should be
12185 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12186 keyword has been used to indicate that the name that appears next
12187 is a template. TYPE_P is true iff the next name should be treated
12188 as class-name, even if it is declared to be some other kind of name
12189 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12190 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12191 being defined in a class-head.
12193 Returns the TYPE_DECL representing the class. */
12196 cp_parser_class_name (cp_parser *parser,
12197 bool typename_keyword_p,
12198 bool template_keyword_p,
12200 bool check_dependency_p,
12202 bool is_declaration)
12209 /* All class-names start with an identifier. */
12210 token = cp_lexer_peek_token (parser->lexer);
12211 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12213 cp_parser_error (parser, "expected class-name");
12214 return error_mark_node;
12217 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12218 to a template-id, so we save it here. */
12219 scope = parser->scope;
12220 if (scope == error_mark_node)
12221 return error_mark_node;
12223 /* Any name names a type if we're following the `typename' keyword
12224 in a qualified name where the enclosing scope is type-dependent. */
12225 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12226 && dependent_type_p (scope));
12227 /* Handle the common case (an identifier, but not a template-id)
12229 if (token->type == CPP_NAME
12230 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12234 /* Look for the identifier. */
12235 identifier = cp_parser_identifier (parser);
12236 /* If the next token isn't an identifier, we are certainly not
12237 looking at a class-name. */
12238 if (identifier == error_mark_node)
12239 decl = error_mark_node;
12240 /* If we know this is a type-name, there's no need to look it
12242 else if (typename_p)
12246 /* If the next token is a `::', then the name must be a type
12249 [basic.lookup.qual]
12251 During the lookup for a name preceding the :: scope
12252 resolution operator, object, function, and enumerator
12253 names are ignored. */
12254 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12256 /* Look up the name. */
12257 decl = cp_parser_lookup_name (parser, identifier,
12259 /*is_template=*/false,
12260 /*is_namespace=*/false,
12261 check_dependency_p,
12262 /*ambiguous_p=*/NULL);
12267 /* Try a template-id. */
12268 decl = cp_parser_template_id (parser, template_keyword_p,
12269 check_dependency_p,
12271 if (decl == error_mark_node)
12272 return error_mark_node;
12275 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12277 /* If this is a typename, create a TYPENAME_TYPE. */
12278 if (typename_p && decl != error_mark_node)
12280 decl = make_typename_type (scope, decl, /*complain=*/1);
12281 if (decl != error_mark_node)
12282 decl = TYPE_NAME (decl);
12285 /* Check to see that it is really the name of a class. */
12286 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12287 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12288 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12289 /* Situations like this:
12291 template <typename T> struct A {
12292 typename T::template X<int>::I i;
12295 are problematic. Is `T::template X<int>' a class-name? The
12296 standard does not seem to be definitive, but there is no other
12297 valid interpretation of the following `::'. Therefore, those
12298 names are considered class-names. */
12299 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12300 else if (decl == error_mark_node
12301 || TREE_CODE (decl) != TYPE_DECL
12302 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12304 cp_parser_error (parser, "expected class-name");
12305 return error_mark_node;
12311 /* Parse a class-specifier.
12314 class-head { member-specification [opt] }
12316 Returns the TREE_TYPE representing the class. */
12319 cp_parser_class_specifier (cp_parser* parser)
12323 tree attributes = NULL_TREE;
12324 int has_trailing_semicolon;
12325 bool nested_name_specifier_p;
12326 unsigned saved_num_template_parameter_lists;
12327 bool pop_p = false;
12328 tree scope = NULL_TREE;
12330 push_deferring_access_checks (dk_no_deferred);
12332 /* Parse the class-head. */
12333 type = cp_parser_class_head (parser,
12334 &nested_name_specifier_p,
12336 /* If the class-head was a semantic disaster, skip the entire body
12340 cp_parser_skip_to_end_of_block_or_statement (parser);
12341 pop_deferring_access_checks ();
12342 return error_mark_node;
12345 /* Look for the `{'. */
12346 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12348 pop_deferring_access_checks ();
12349 return error_mark_node;
12352 /* Issue an error message if type-definitions are forbidden here. */
12353 cp_parser_check_type_definition (parser);
12354 /* Remember that we are defining one more class. */
12355 ++parser->num_classes_being_defined;
12356 /* Inside the class, surrounding template-parameter-lists do not
12358 saved_num_template_parameter_lists
12359 = parser->num_template_parameter_lists;
12360 parser->num_template_parameter_lists = 0;
12362 /* Start the class. */
12363 if (nested_name_specifier_p)
12365 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12366 pop_p = push_scope (scope);
12368 type = begin_class_definition (type);
12370 if (type == error_mark_node)
12371 /* If the type is erroneous, skip the entire body of the class. */
12372 cp_parser_skip_to_closing_brace (parser);
12374 /* Parse the member-specification. */
12375 cp_parser_member_specification_opt (parser);
12377 /* Look for the trailing `}'. */
12378 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12379 /* We get better error messages by noticing a common problem: a
12380 missing trailing `;'. */
12381 token = cp_lexer_peek_token (parser->lexer);
12382 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12383 /* Look for trailing attributes to apply to this class. */
12384 if (cp_parser_allow_gnu_extensions_p (parser))
12386 tree sub_attr = cp_parser_attributes_opt (parser);
12387 attributes = chainon (attributes, sub_attr);
12389 if (type != error_mark_node)
12390 type = finish_struct (type, attributes);
12393 /* If this class is not itself within the scope of another class,
12394 then we need to parse the bodies of all of the queued function
12395 definitions. Note that the queued functions defined in a class
12396 are not always processed immediately following the
12397 class-specifier for that class. Consider:
12400 struct B { void f() { sizeof (A); } };
12403 If `f' were processed before the processing of `A' were
12404 completed, there would be no way to compute the size of `A'.
12405 Note that the nesting we are interested in here is lexical --
12406 not the semantic nesting given by TYPE_CONTEXT. In particular,
12409 struct A { struct B; };
12410 struct A::B { void f() { } };
12412 there is no need to delay the parsing of `A::B::f'. */
12413 if (--parser->num_classes_being_defined == 0)
12420 /* In a first pass, parse default arguments to the functions.
12421 Then, in a second pass, parse the bodies of the functions.
12422 This two-phased approach handles cases like:
12430 class_type = NULL_TREE;
12432 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12433 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12434 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12435 TREE_PURPOSE (parser->unparsed_functions_queues)
12436 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12438 fn = TREE_VALUE (queue_entry);
12439 /* If there are default arguments that have not yet been processed,
12440 take care of them now. */
12441 if (class_type != TREE_PURPOSE (queue_entry))
12444 pop_scope (class_type);
12445 class_type = TREE_PURPOSE (queue_entry);
12446 pop_p = push_scope (class_type);
12448 /* Make sure that any template parameters are in scope. */
12449 maybe_begin_member_template_processing (fn);
12450 /* Parse the default argument expressions. */
12451 cp_parser_late_parsing_default_args (parser, fn);
12452 /* Remove any template parameters from the symbol table. */
12453 maybe_end_member_template_processing ();
12456 pop_scope (class_type);
12457 /* Now parse the body of the functions. */
12458 for (TREE_VALUE (parser->unparsed_functions_queues)
12459 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12460 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12461 TREE_VALUE (parser->unparsed_functions_queues)
12462 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12464 /* Figure out which function we need to process. */
12465 fn = TREE_VALUE (queue_entry);
12467 /* A hack to prevent garbage collection. */
12470 /* Parse the function. */
12471 cp_parser_late_parsing_for_member (parser, fn);
12476 /* Put back any saved access checks. */
12477 pop_deferring_access_checks ();
12479 /* Restore the count of active template-parameter-lists. */
12480 parser->num_template_parameter_lists
12481 = saved_num_template_parameter_lists;
12486 /* Parse a class-head.
12489 class-key identifier [opt] base-clause [opt]
12490 class-key nested-name-specifier identifier base-clause [opt]
12491 class-key nested-name-specifier [opt] template-id
12495 class-key attributes identifier [opt] base-clause [opt]
12496 class-key attributes nested-name-specifier identifier base-clause [opt]
12497 class-key attributes nested-name-specifier [opt] template-id
12500 Returns the TYPE of the indicated class. Sets
12501 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12502 involving a nested-name-specifier was used, and FALSE otherwise.
12504 Returns NULL_TREE if the class-head is syntactically valid, but
12505 semantically invalid in a way that means we should skip the entire
12506 body of the class. */
12509 cp_parser_class_head (cp_parser* parser,
12510 bool* nested_name_specifier_p,
12511 tree *attributes_p)
12513 tree nested_name_specifier;
12514 enum tag_types class_key;
12515 tree id = NULL_TREE;
12516 tree type = NULL_TREE;
12518 bool template_id_p = false;
12519 bool qualified_p = false;
12520 bool invalid_nested_name_p = false;
12521 bool invalid_explicit_specialization_p = false;
12522 bool pop_p = false;
12523 unsigned num_templates;
12526 /* Assume no nested-name-specifier will be present. */
12527 *nested_name_specifier_p = false;
12528 /* Assume no template parameter lists will be used in defining the
12532 /* Look for the class-key. */
12533 class_key = cp_parser_class_key (parser);
12534 if (class_key == none_type)
12535 return error_mark_node;
12537 /* Parse the attributes. */
12538 attributes = cp_parser_attributes_opt (parser);
12540 /* If the next token is `::', that is invalid -- but sometimes
12541 people do try to write:
12545 Handle this gracefully by accepting the extra qualifier, and then
12546 issuing an error about it later if this really is a
12547 class-head. If it turns out just to be an elaborated type
12548 specifier, remain silent. */
12549 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12550 qualified_p = true;
12552 push_deferring_access_checks (dk_no_check);
12554 /* Determine the name of the class. Begin by looking for an
12555 optional nested-name-specifier. */
12556 nested_name_specifier
12557 = cp_parser_nested_name_specifier_opt (parser,
12558 /*typename_keyword_p=*/false,
12559 /*check_dependency_p=*/false,
12561 /*is_declaration=*/false);
12562 /* If there was a nested-name-specifier, then there *must* be an
12564 if (nested_name_specifier)
12566 /* Although the grammar says `identifier', it really means
12567 `class-name' or `template-name'. You are only allowed to
12568 define a class that has already been declared with this
12571 The proposed resolution for Core Issue 180 says that whever
12572 you see `class T::X' you should treat `X' as a type-name.
12574 It is OK to define an inaccessible class; for example:
12576 class A { class B; };
12579 We do not know if we will see a class-name, or a
12580 template-name. We look for a class-name first, in case the
12581 class-name is a template-id; if we looked for the
12582 template-name first we would stop after the template-name. */
12583 cp_parser_parse_tentatively (parser);
12584 type = cp_parser_class_name (parser,
12585 /*typename_keyword_p=*/false,
12586 /*template_keyword_p=*/false,
12588 /*check_dependency_p=*/false,
12589 /*class_head_p=*/true,
12590 /*is_declaration=*/false);
12591 /* If that didn't work, ignore the nested-name-specifier. */
12592 if (!cp_parser_parse_definitely (parser))
12594 invalid_nested_name_p = true;
12595 id = cp_parser_identifier (parser);
12596 if (id == error_mark_node)
12599 /* If we could not find a corresponding TYPE, treat this
12600 declaration like an unqualified declaration. */
12601 if (type == error_mark_node)
12602 nested_name_specifier = NULL_TREE;
12603 /* Otherwise, count the number of templates used in TYPE and its
12604 containing scopes. */
12609 for (scope = TREE_TYPE (type);
12610 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12611 scope = (TYPE_P (scope)
12612 ? TYPE_CONTEXT (scope)
12613 : DECL_CONTEXT (scope)))
12615 && CLASS_TYPE_P (scope)
12616 && CLASSTYPE_TEMPLATE_INFO (scope)
12617 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12618 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12622 /* Otherwise, the identifier is optional. */
12625 /* We don't know whether what comes next is a template-id,
12626 an identifier, or nothing at all. */
12627 cp_parser_parse_tentatively (parser);
12628 /* Check for a template-id. */
12629 id = cp_parser_template_id (parser,
12630 /*template_keyword_p=*/false,
12631 /*check_dependency_p=*/true,
12632 /*is_declaration=*/true);
12633 /* If that didn't work, it could still be an identifier. */
12634 if (!cp_parser_parse_definitely (parser))
12636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12637 id = cp_parser_identifier (parser);
12643 template_id_p = true;
12648 pop_deferring_access_checks ();
12651 cp_parser_check_for_invalid_template_id (parser, id);
12653 /* If it's not a `:' or a `{' then we can't really be looking at a
12654 class-head, since a class-head only appears as part of a
12655 class-specifier. We have to detect this situation before calling
12656 xref_tag, since that has irreversible side-effects. */
12657 if (!cp_parser_next_token_starts_class_definition_p (parser))
12659 cp_parser_error (parser, "expected `{' or `:'");
12660 return error_mark_node;
12663 /* At this point, we're going ahead with the class-specifier, even
12664 if some other problem occurs. */
12665 cp_parser_commit_to_tentative_parse (parser);
12666 /* Issue the error about the overly-qualified name now. */
12668 cp_parser_error (parser,
12669 "global qualification of class name is invalid");
12670 else if (invalid_nested_name_p)
12671 cp_parser_error (parser,
12672 "qualified name does not name a class");
12673 else if (nested_name_specifier)
12676 /* Figure out in what scope the declaration is being placed. */
12677 scope = current_scope ();
12679 scope = current_namespace;
12680 /* If that scope does not contain the scope in which the
12681 class was originally declared, the program is invalid. */
12682 if (scope && !is_ancestor (scope, nested_name_specifier))
12684 error ("declaration of `%D' in `%D' which does not "
12685 "enclose `%D'", type, scope, nested_name_specifier);
12691 A declarator-id shall not be qualified exception of the
12692 definition of a ... nested class outside of its class
12693 ... [or] a the definition or explicit instantiation of a
12694 class member of a namespace outside of its namespace. */
12695 if (scope == nested_name_specifier)
12697 pedwarn ("extra qualification ignored");
12698 nested_name_specifier = NULL_TREE;
12702 /* An explicit-specialization must be preceded by "template <>". If
12703 it is not, try to recover gracefully. */
12704 if (at_namespace_scope_p ()
12705 && parser->num_template_parameter_lists == 0
12708 error ("an explicit specialization must be preceded by 'template <>'");
12709 invalid_explicit_specialization_p = true;
12710 /* Take the same action that would have been taken by
12711 cp_parser_explicit_specialization. */
12712 ++parser->num_template_parameter_lists;
12713 begin_specialization ();
12715 /* There must be no "return" statements between this point and the
12716 end of this function; set "type "to the correct return value and
12717 use "goto done;" to return. */
12718 /* Make sure that the right number of template parameters were
12720 if (!cp_parser_check_template_parameters (parser, num_templates))
12722 /* If something went wrong, there is no point in even trying to
12723 process the class-definition. */
12728 /* Look up the type. */
12731 type = TREE_TYPE (id);
12732 maybe_process_partial_specialization (type);
12734 else if (!nested_name_specifier)
12736 /* If the class was unnamed, create a dummy name. */
12738 id = make_anon_name ();
12739 type = xref_tag (class_key, id, /*globalize=*/false,
12740 parser->num_template_parameter_lists);
12745 bool pop_p = false;
12749 template <typename T> struct S { struct T };
12750 template <typename T> struct S<T>::T { };
12752 we will get a TYPENAME_TYPE when processing the definition of
12753 `S::T'. We need to resolve it to the actual type before we
12754 try to define it. */
12755 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12757 class_type = resolve_typename_type (TREE_TYPE (type),
12758 /*only_current_p=*/false);
12759 if (class_type != error_mark_node)
12760 type = TYPE_NAME (class_type);
12763 cp_parser_error (parser, "could not resolve typename type");
12764 type = error_mark_node;
12768 maybe_process_partial_specialization (TREE_TYPE (type));
12769 class_type = current_class_type;
12770 /* Enter the scope indicated by the nested-name-specifier. */
12771 if (nested_name_specifier)
12772 pop_p = push_scope (nested_name_specifier);
12773 /* Get the canonical version of this type. */
12774 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12775 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12776 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12777 type = push_template_decl (type);
12778 type = TREE_TYPE (type);
12779 if (nested_name_specifier)
12781 *nested_name_specifier_p = true;
12783 pop_scope (nested_name_specifier);
12786 /* Indicate whether this class was declared as a `class' or as a
12788 if (TREE_CODE (type) == RECORD_TYPE)
12789 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12790 cp_parser_check_class_key (class_key, type);
12792 /* Enter the scope containing the class; the names of base classes
12793 should be looked up in that context. For example, given:
12795 struct A { struct B {}; struct C; };
12796 struct A::C : B {};
12799 if (nested_name_specifier)
12800 pop_p = push_scope (nested_name_specifier);
12804 /* Get the list of base-classes, if there is one. */
12805 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12806 bases = cp_parser_base_clause (parser);
12808 /* Process the base classes. */
12809 xref_basetypes (type, bases);
12811 /* Leave the scope given by the nested-name-specifier. We will
12812 enter the class scope itself while processing the members. */
12814 pop_scope (nested_name_specifier);
12817 if (invalid_explicit_specialization_p)
12819 end_specialization ();
12820 --parser->num_template_parameter_lists;
12822 *attributes_p = attributes;
12826 /* Parse a class-key.
12833 Returns the kind of class-key specified, or none_type to indicate
12836 static enum tag_types
12837 cp_parser_class_key (cp_parser* parser)
12840 enum tag_types tag_type;
12842 /* Look for the class-key. */
12843 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12847 /* Check to see if the TOKEN is a class-key. */
12848 tag_type = cp_parser_token_is_class_key (token);
12850 cp_parser_error (parser, "expected class-key");
12854 /* Parse an (optional) member-specification.
12856 member-specification:
12857 member-declaration member-specification [opt]
12858 access-specifier : member-specification [opt] */
12861 cp_parser_member_specification_opt (cp_parser* parser)
12868 /* Peek at the next token. */
12869 token = cp_lexer_peek_token (parser->lexer);
12870 /* If it's a `}', or EOF then we've seen all the members. */
12871 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12874 /* See if this token is a keyword. */
12875 keyword = token->keyword;
12879 case RID_PROTECTED:
12881 /* Consume the access-specifier. */
12882 cp_lexer_consume_token (parser->lexer);
12883 /* Remember which access-specifier is active. */
12884 current_access_specifier = token->value;
12885 /* Look for the `:'. */
12886 cp_parser_require (parser, CPP_COLON, "`:'");
12890 /* Otherwise, the next construction must be a
12891 member-declaration. */
12892 cp_parser_member_declaration (parser);
12897 /* Parse a member-declaration.
12899 member-declaration:
12900 decl-specifier-seq [opt] member-declarator-list [opt] ;
12901 function-definition ; [opt]
12902 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12904 template-declaration
12906 member-declarator-list:
12908 member-declarator-list , member-declarator
12911 declarator pure-specifier [opt]
12912 declarator constant-initializer [opt]
12913 identifier [opt] : constant-expression
12917 member-declaration:
12918 __extension__ member-declaration
12921 declarator attributes [opt] pure-specifier [opt]
12922 declarator attributes [opt] constant-initializer [opt]
12923 identifier [opt] attributes [opt] : constant-expression */
12926 cp_parser_member_declaration (cp_parser* parser)
12928 cp_decl_specifier_seq decl_specifiers;
12929 tree prefix_attributes;
12931 int declares_class_or_enum;
12934 int saved_pedantic;
12936 /* Check for the `__extension__' keyword. */
12937 if (cp_parser_extension_opt (parser, &saved_pedantic))
12940 cp_parser_member_declaration (parser);
12941 /* Restore the old value of the PEDANTIC flag. */
12942 pedantic = saved_pedantic;
12947 /* Check for a template-declaration. */
12948 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12950 /* Parse the template-declaration. */
12951 cp_parser_template_declaration (parser, /*member_p=*/true);
12956 /* Check for a using-declaration. */
12957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12959 /* Parse the using-declaration. */
12960 cp_parser_using_declaration (parser);
12965 /* Parse the decl-specifier-seq. */
12966 cp_parser_decl_specifier_seq (parser,
12967 CP_PARSER_FLAGS_OPTIONAL,
12969 &declares_class_or_enum);
12970 prefix_attributes = decl_specifiers.attributes;
12971 decl_specifiers.attributes = NULL_TREE;
12972 /* Check for an invalid type-name. */
12973 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12975 /* If there is no declarator, then the decl-specifier-seq should
12977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12979 /* If there was no decl-specifier-seq, and the next token is a
12980 `;', then we have something like:
12986 Each member-declaration shall declare at least one member
12987 name of the class. */
12988 if (!decl_specifiers.any_specifiers_p)
12991 pedwarn ("extra semicolon");
12997 /* See if this declaration is a friend. */
12998 friend_p = cp_parser_friend_p (&decl_specifiers);
12999 /* If there were decl-specifiers, check to see if there was
13000 a class-declaration. */
13001 type = check_tag_decl (&decl_specifiers);
13002 /* Nested classes have already been added to the class, but
13003 a `friend' needs to be explicitly registered. */
13006 /* If the `friend' keyword was present, the friend must
13007 be introduced with a class-key. */
13008 if (!declares_class_or_enum)
13009 error ("a class-key must be used when declaring a friend");
13012 template <typename T> struct A {
13013 friend struct A<T>::B;
13016 A<T>::B will be represented by a TYPENAME_TYPE, and
13017 therefore not recognized by check_tag_decl. */
13019 && decl_specifiers.type
13020 && TYPE_P (decl_specifiers.type))
13021 type = decl_specifiers.type;
13022 if (!type || !TYPE_P (type))
13023 error ("friend declaration does not name a class or "
13026 make_friend_class (current_class_type, type,
13027 /*complain=*/true);
13029 /* If there is no TYPE, an error message will already have
13031 else if (!type || type == error_mark_node)
13033 /* An anonymous aggregate has to be handled specially; such
13034 a declaration really declares a data member (with a
13035 particular type), as opposed to a nested class. */
13036 else if (ANON_AGGR_TYPE_P (type))
13038 /* Remove constructors and such from TYPE, now that we
13039 know it is an anonymous aggregate. */
13040 fixup_anonymous_aggr (type);
13041 /* And make the corresponding data member. */
13042 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13043 /* Add it to the class. */
13044 finish_member_declaration (decl);
13047 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13052 /* See if these declarations will be friends. */
13053 friend_p = cp_parser_friend_p (&decl_specifiers);
13055 /* Keep going until we hit the `;' at the end of the
13057 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13059 tree attributes = NULL_TREE;
13060 tree first_attribute;
13062 /* Peek at the next token. */
13063 token = cp_lexer_peek_token (parser->lexer);
13065 /* Check for a bitfield declaration. */
13066 if (token->type == CPP_COLON
13067 || (token->type == CPP_NAME
13068 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13074 /* Get the name of the bitfield. Note that we cannot just
13075 check TOKEN here because it may have been invalidated by
13076 the call to cp_lexer_peek_nth_token above. */
13077 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13078 identifier = cp_parser_identifier (parser);
13080 identifier = NULL_TREE;
13082 /* Consume the `:' token. */
13083 cp_lexer_consume_token (parser->lexer);
13084 /* Get the width of the bitfield. */
13086 = cp_parser_constant_expression (parser,
13087 /*allow_non_constant=*/false,
13090 /* Look for attributes that apply to the bitfield. */
13091 attributes = cp_parser_attributes_opt (parser);
13092 /* Remember which attributes are prefix attributes and
13094 first_attribute = attributes;
13095 /* Combine the attributes. */
13096 attributes = chainon (prefix_attributes, attributes);
13098 /* Create the bitfield declaration. */
13099 decl = grokbitfield (identifier
13100 ? make_id_declarator (identifier)
13104 /* Apply the attributes. */
13105 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13109 cp_declarator *declarator;
13111 tree asm_specification;
13112 int ctor_dtor_or_conv_p;
13114 /* Parse the declarator. */
13116 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13117 &ctor_dtor_or_conv_p,
13118 /*parenthesized_p=*/NULL);
13120 /* If something went wrong parsing the declarator, make sure
13121 that we at least consume some tokens. */
13122 if (declarator == cp_error_declarator)
13124 /* Skip to the end of the statement. */
13125 cp_parser_skip_to_end_of_statement (parser);
13126 /* If the next token is not a semicolon, that is
13127 probably because we just skipped over the body of
13128 a function. So, we consume a semicolon if
13129 present, but do not issue an error message if it
13131 if (cp_lexer_next_token_is (parser->lexer,
13133 cp_lexer_consume_token (parser->lexer);
13137 cp_parser_check_for_definition_in_return_type
13138 (declarator, declares_class_or_enum);
13140 /* Look for an asm-specification. */
13141 asm_specification = cp_parser_asm_specification_opt (parser);
13142 /* Look for attributes that apply to the declaration. */
13143 attributes = cp_parser_attributes_opt (parser);
13144 /* Remember which attributes are prefix attributes and
13146 first_attribute = attributes;
13147 /* Combine the attributes. */
13148 attributes = chainon (prefix_attributes, attributes);
13150 /* If it's an `=', then we have a constant-initializer or a
13151 pure-specifier. It is not correct to parse the
13152 initializer before registering the member declaration
13153 since the member declaration should be in scope while
13154 its initializer is processed. However, the rest of the
13155 front end does not yet provide an interface that allows
13156 us to handle this correctly. */
13157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13161 A pure-specifier shall be used only in the declaration of
13162 a virtual function.
13164 A member-declarator can contain a constant-initializer
13165 only if it declares a static member of integral or
13168 Therefore, if the DECLARATOR is for a function, we look
13169 for a pure-specifier; otherwise, we look for a
13170 constant-initializer. When we call `grokfield', it will
13171 perform more stringent semantics checks. */
13172 if (declarator->kind == cdk_function)
13173 initializer = cp_parser_pure_specifier (parser);
13175 /* Parse the initializer. */
13176 initializer = cp_parser_constant_initializer (parser);
13178 /* Otherwise, there is no initializer. */
13180 initializer = NULL_TREE;
13182 /* See if we are probably looking at a function
13183 definition. We are certainly not looking at at a
13184 member-declarator. Calling `grokfield' has
13185 side-effects, so we must not do it unless we are sure
13186 that we are looking at a member-declarator. */
13187 if (cp_parser_token_starts_function_definition_p
13188 (cp_lexer_peek_token (parser->lexer)))
13190 /* The grammar does not allow a pure-specifier to be
13191 used when a member function is defined. (It is
13192 possible that this fact is an oversight in the
13193 standard, since a pure function may be defined
13194 outside of the class-specifier. */
13196 error ("pure-specifier on function-definition");
13197 decl = cp_parser_save_member_function_body (parser,
13201 /* If the member was not a friend, declare it here. */
13203 finish_member_declaration (decl);
13204 /* Peek at the next token. */
13205 token = cp_lexer_peek_token (parser->lexer);
13206 /* If the next token is a semicolon, consume it. */
13207 if (token->type == CPP_SEMICOLON)
13208 cp_lexer_consume_token (parser->lexer);
13213 /* Create the declaration. */
13214 decl = grokfield (declarator, &decl_specifiers,
13215 initializer, asm_specification,
13217 /* Any initialization must have been from a
13218 constant-expression. */
13219 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13220 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13224 /* Reset PREFIX_ATTRIBUTES. */
13225 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13226 attributes = TREE_CHAIN (attributes);
13228 TREE_CHAIN (attributes) = NULL_TREE;
13230 /* If there is any qualification still in effect, clear it
13231 now; we will be starting fresh with the next declarator. */
13232 parser->scope = NULL_TREE;
13233 parser->qualifying_scope = NULL_TREE;
13234 parser->object_scope = NULL_TREE;
13235 /* If it's a `,', then there are more declarators. */
13236 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13237 cp_lexer_consume_token (parser->lexer);
13238 /* If the next token isn't a `;', then we have a parse error. */
13239 else if (cp_lexer_next_token_is_not (parser->lexer,
13242 cp_parser_error (parser, "expected `;'");
13243 /* Skip tokens until we find a `;'. */
13244 cp_parser_skip_to_end_of_statement (parser);
13251 /* Add DECL to the list of members. */
13253 finish_member_declaration (decl);
13255 if (TREE_CODE (decl) == FUNCTION_DECL)
13256 cp_parser_save_default_args (parser, decl);
13261 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13264 /* Parse a pure-specifier.
13269 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13270 Otherwise, ERROR_MARK_NODE is returned. */
13273 cp_parser_pure_specifier (cp_parser* parser)
13277 /* Look for the `=' token. */
13278 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13279 return error_mark_node;
13280 /* Look for the `0' token. */
13281 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13282 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13283 to get information from the lexer about how the number was
13284 spelled in order to fix this problem. */
13285 if (!token || !integer_zerop (token->value))
13286 return error_mark_node;
13288 return integer_zero_node;
13291 /* Parse a constant-initializer.
13293 constant-initializer:
13294 = constant-expression
13296 Returns a representation of the constant-expression. */
13299 cp_parser_constant_initializer (cp_parser* parser)
13301 /* Look for the `=' token. */
13302 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13303 return error_mark_node;
13305 /* It is invalid to write:
13307 struct S { static const int i = { 7 }; };
13310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13312 cp_parser_error (parser,
13313 "a brace-enclosed initializer is not allowed here");
13314 /* Consume the opening brace. */
13315 cp_lexer_consume_token (parser->lexer);
13316 /* Skip the initializer. */
13317 cp_parser_skip_to_closing_brace (parser);
13318 /* Look for the trailing `}'. */
13319 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13321 return error_mark_node;
13324 return cp_parser_constant_expression (parser,
13325 /*allow_non_constant=*/false,
13329 /* Derived classes [gram.class.derived] */
13331 /* Parse a base-clause.
13334 : base-specifier-list
13336 base-specifier-list:
13338 base-specifier-list , base-specifier
13340 Returns a TREE_LIST representing the base-classes, in the order in
13341 which they were declared. The representation of each node is as
13342 described by cp_parser_base_specifier.
13344 In the case that no bases are specified, this function will return
13345 NULL_TREE, not ERROR_MARK_NODE. */
13348 cp_parser_base_clause (cp_parser* parser)
13350 tree bases = NULL_TREE;
13352 /* Look for the `:' that begins the list. */
13353 cp_parser_require (parser, CPP_COLON, "`:'");
13355 /* Scan the base-specifier-list. */
13361 /* Look for the base-specifier. */
13362 base = cp_parser_base_specifier (parser);
13363 /* Add BASE to the front of the list. */
13364 if (base != error_mark_node)
13366 TREE_CHAIN (base) = bases;
13369 /* Peek at the next token. */
13370 token = cp_lexer_peek_token (parser->lexer);
13371 /* If it's not a comma, then the list is complete. */
13372 if (token->type != CPP_COMMA)
13374 /* Consume the `,'. */
13375 cp_lexer_consume_token (parser->lexer);
13378 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13379 base class had a qualified name. However, the next name that
13380 appears is certainly not qualified. */
13381 parser->scope = NULL_TREE;
13382 parser->qualifying_scope = NULL_TREE;
13383 parser->object_scope = NULL_TREE;
13385 return nreverse (bases);
13388 /* Parse a base-specifier.
13391 :: [opt] nested-name-specifier [opt] class-name
13392 virtual access-specifier [opt] :: [opt] nested-name-specifier
13394 access-specifier virtual [opt] :: [opt] nested-name-specifier
13397 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13398 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13399 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13400 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13403 cp_parser_base_specifier (cp_parser* parser)
13407 bool virtual_p = false;
13408 bool duplicate_virtual_error_issued_p = false;
13409 bool duplicate_access_error_issued_p = false;
13410 bool class_scope_p, template_p;
13411 tree access = access_default_node;
13414 /* Process the optional `virtual' and `access-specifier'. */
13417 /* Peek at the next token. */
13418 token = cp_lexer_peek_token (parser->lexer);
13419 /* Process `virtual'. */
13420 switch (token->keyword)
13423 /* If `virtual' appears more than once, issue an error. */
13424 if (virtual_p && !duplicate_virtual_error_issued_p)
13426 cp_parser_error (parser,
13427 "`virtual' specified more than once in base-specified");
13428 duplicate_virtual_error_issued_p = true;
13433 /* Consume the `virtual' token. */
13434 cp_lexer_consume_token (parser->lexer);
13439 case RID_PROTECTED:
13441 /* If more than one access specifier appears, issue an
13443 if (access != access_default_node
13444 && !duplicate_access_error_issued_p)
13446 cp_parser_error (parser,
13447 "more than one access specifier in base-specified");
13448 duplicate_access_error_issued_p = true;
13451 access = ridpointers[(int) token->keyword];
13453 /* Consume the access-specifier. */
13454 cp_lexer_consume_token (parser->lexer);
13463 /* It is not uncommon to see programs mechanically, erroneously, use
13464 the 'typename' keyword to denote (dependent) qualified types
13465 as base classes. */
13466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13468 if (!processing_template_decl)
13469 error ("keyword `typename' not allowed outside of templates");
13471 error ("keyword `typename' not allowed in this context "
13472 "(the base class is implicitly a type)");
13473 cp_lexer_consume_token (parser->lexer);
13476 /* Look for the optional `::' operator. */
13477 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13478 /* Look for the nested-name-specifier. The simplest way to
13483 The keyword `typename' is not permitted in a base-specifier or
13484 mem-initializer; in these contexts a qualified name that
13485 depends on a template-parameter is implicitly assumed to be a
13488 is to pretend that we have seen the `typename' keyword at this
13490 cp_parser_nested_name_specifier_opt (parser,
13491 /*typename_keyword_p=*/true,
13492 /*check_dependency_p=*/true,
13494 /*is_declaration=*/true);
13495 /* If the base class is given by a qualified name, assume that names
13496 we see are type names or templates, as appropriate. */
13497 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13498 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13500 /* Finally, look for the class-name. */
13501 type = cp_parser_class_name (parser,
13505 /*check_dependency_p=*/true,
13506 /*class_head_p=*/false,
13507 /*is_declaration=*/true);
13509 if (type == error_mark_node)
13510 return error_mark_node;
13512 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13515 /* Exception handling [gram.exception] */
13517 /* Parse an (optional) exception-specification.
13519 exception-specification:
13520 throw ( type-id-list [opt] )
13522 Returns a TREE_LIST representing the exception-specification. The
13523 TREE_VALUE of each node is a type. */
13526 cp_parser_exception_specification_opt (cp_parser* parser)
13531 /* Peek at the next token. */
13532 token = cp_lexer_peek_token (parser->lexer);
13533 /* If it's not `throw', then there's no exception-specification. */
13534 if (!cp_parser_is_keyword (token, RID_THROW))
13537 /* Consume the `throw'. */
13538 cp_lexer_consume_token (parser->lexer);
13540 /* Look for the `('. */
13541 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13543 /* Peek at the next token. */
13544 token = cp_lexer_peek_token (parser->lexer);
13545 /* If it's not a `)', then there is a type-id-list. */
13546 if (token->type != CPP_CLOSE_PAREN)
13548 const char *saved_message;
13550 /* Types may not be defined in an exception-specification. */
13551 saved_message = parser->type_definition_forbidden_message;
13552 parser->type_definition_forbidden_message
13553 = "types may not be defined in an exception-specification";
13554 /* Parse the type-id-list. */
13555 type_id_list = cp_parser_type_id_list (parser);
13556 /* Restore the saved message. */
13557 parser->type_definition_forbidden_message = saved_message;
13560 type_id_list = empty_except_spec;
13562 /* Look for the `)'. */
13563 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13565 return type_id_list;
13568 /* Parse an (optional) type-id-list.
13572 type-id-list , type-id
13574 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13575 in the order that the types were presented. */
13578 cp_parser_type_id_list (cp_parser* parser)
13580 tree types = NULL_TREE;
13587 /* Get the next type-id. */
13588 type = cp_parser_type_id (parser);
13589 /* Add it to the list. */
13590 types = add_exception_specifier (types, type, /*complain=*/1);
13591 /* Peek at the next token. */
13592 token = cp_lexer_peek_token (parser->lexer);
13593 /* If it is not a `,', we are done. */
13594 if (token->type != CPP_COMMA)
13596 /* Consume the `,'. */
13597 cp_lexer_consume_token (parser->lexer);
13600 return nreverse (types);
13603 /* Parse a try-block.
13606 try compound-statement handler-seq */
13609 cp_parser_try_block (cp_parser* parser)
13613 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13614 try_block = begin_try_block ();
13615 cp_parser_compound_statement (parser, NULL, true);
13616 finish_try_block (try_block);
13617 cp_parser_handler_seq (parser);
13618 finish_handler_sequence (try_block);
13623 /* Parse a function-try-block.
13625 function-try-block:
13626 try ctor-initializer [opt] function-body handler-seq */
13629 cp_parser_function_try_block (cp_parser* parser)
13632 bool ctor_initializer_p;
13634 /* Look for the `try' keyword. */
13635 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13637 /* Let the rest of the front-end know where we are. */
13638 try_block = begin_function_try_block ();
13639 /* Parse the function-body. */
13641 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13642 /* We're done with the `try' part. */
13643 finish_function_try_block (try_block);
13644 /* Parse the handlers. */
13645 cp_parser_handler_seq (parser);
13646 /* We're done with the handlers. */
13647 finish_function_handler_sequence (try_block);
13649 return ctor_initializer_p;
13652 /* Parse a handler-seq.
13655 handler handler-seq [opt] */
13658 cp_parser_handler_seq (cp_parser* parser)
13664 /* Parse the handler. */
13665 cp_parser_handler (parser);
13666 /* Peek at the next token. */
13667 token = cp_lexer_peek_token (parser->lexer);
13668 /* If it's not `catch' then there are no more handlers. */
13669 if (!cp_parser_is_keyword (token, RID_CATCH))
13674 /* Parse a handler.
13677 catch ( exception-declaration ) compound-statement */
13680 cp_parser_handler (cp_parser* parser)
13685 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13686 handler = begin_handler ();
13687 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13688 declaration = cp_parser_exception_declaration (parser);
13689 finish_handler_parms (declaration, handler);
13690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13691 cp_parser_compound_statement (parser, NULL, false);
13692 finish_handler (handler);
13695 /* Parse an exception-declaration.
13697 exception-declaration:
13698 type-specifier-seq declarator
13699 type-specifier-seq abstract-declarator
13703 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13704 ellipsis variant is used. */
13707 cp_parser_exception_declaration (cp_parser* parser)
13710 cp_decl_specifier_seq type_specifiers;
13711 cp_declarator *declarator;
13712 const char *saved_message;
13714 /* If it's an ellipsis, it's easy to handle. */
13715 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13717 /* Consume the `...' token. */
13718 cp_lexer_consume_token (parser->lexer);
13722 /* Types may not be defined in exception-declarations. */
13723 saved_message = parser->type_definition_forbidden_message;
13724 parser->type_definition_forbidden_message
13725 = "types may not be defined in exception-declarations";
13727 /* Parse the type-specifier-seq. */
13728 cp_parser_type_specifier_seq (parser, &type_specifiers);
13729 /* If it's a `)', then there is no declarator. */
13730 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13733 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13734 /*ctor_dtor_or_conv_p=*/NULL,
13735 /*parenthesized_p=*/NULL);
13737 /* Restore the saved message. */
13738 parser->type_definition_forbidden_message = saved_message;
13740 if (type_specifiers.any_specifiers_p)
13742 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13743 if (decl == NULL_TREE)
13744 error ("invalid catch parameter");
13752 /* Parse a throw-expression.
13755 throw assignment-expression [opt]
13757 Returns a THROW_EXPR representing the throw-expression. */
13760 cp_parser_throw_expression (cp_parser* parser)
13765 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13766 token = cp_lexer_peek_token (parser->lexer);
13767 /* Figure out whether or not there is an assignment-expression
13768 following the "throw" keyword. */
13769 if (token->type == CPP_COMMA
13770 || token->type == CPP_SEMICOLON
13771 || token->type == CPP_CLOSE_PAREN
13772 || token->type == CPP_CLOSE_SQUARE
13773 || token->type == CPP_CLOSE_BRACE
13774 || token->type == CPP_COLON)
13775 expression = NULL_TREE;
13777 expression = cp_parser_assignment_expression (parser);
13779 return build_throw (expression);
13782 /* GNU Extensions */
13784 /* Parse an (optional) asm-specification.
13787 asm ( string-literal )
13789 If the asm-specification is present, returns a STRING_CST
13790 corresponding to the string-literal. Otherwise, returns
13794 cp_parser_asm_specification_opt (cp_parser* parser)
13797 tree asm_specification;
13799 /* Peek at the next token. */
13800 token = cp_lexer_peek_token (parser->lexer);
13801 /* If the next token isn't the `asm' keyword, then there's no
13802 asm-specification. */
13803 if (!cp_parser_is_keyword (token, RID_ASM))
13806 /* Consume the `asm' token. */
13807 cp_lexer_consume_token (parser->lexer);
13808 /* Look for the `('. */
13809 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13811 /* Look for the string-literal. */
13812 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13814 asm_specification = token->value;
13816 asm_specification = NULL_TREE;
13818 /* Look for the `)'. */
13819 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13821 return asm_specification;
13824 /* Parse an asm-operand-list.
13828 asm-operand-list , asm-operand
13831 string-literal ( expression )
13832 [ string-literal ] string-literal ( expression )
13834 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13835 each node is the expression. The TREE_PURPOSE is itself a
13836 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13837 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13838 is a STRING_CST for the string literal before the parenthesis. */
13841 cp_parser_asm_operand_list (cp_parser* parser)
13843 tree asm_operands = NULL_TREE;
13847 tree string_literal;
13852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13854 /* Consume the `[' token. */
13855 cp_lexer_consume_token (parser->lexer);
13856 /* Read the operand name. */
13857 name = cp_parser_identifier (parser);
13858 if (name != error_mark_node)
13859 name = build_string (IDENTIFIER_LENGTH (name),
13860 IDENTIFIER_POINTER (name));
13861 /* Look for the closing `]'. */
13862 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13866 /* Look for the string-literal. */
13867 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13868 string_literal = token ? token->value : error_mark_node;
13869 c_lex_string_translate = 1;
13870 /* Look for the `('. */
13871 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13872 /* Parse the expression. */
13873 expression = cp_parser_expression (parser);
13874 /* Look for the `)'. */
13875 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13876 c_lex_string_translate = 0;
13877 /* Add this operand to the list. */
13878 asm_operands = tree_cons (build_tree_list (name, string_literal),
13881 /* If the next token is not a `,', there are no more
13883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13885 /* Consume the `,'. */
13886 cp_lexer_consume_token (parser->lexer);
13889 return nreverse (asm_operands);
13892 /* Parse an asm-clobber-list.
13896 asm-clobber-list , string-literal
13898 Returns a TREE_LIST, indicating the clobbers in the order that they
13899 appeared. The TREE_VALUE of each node is a STRING_CST. */
13902 cp_parser_asm_clobber_list (cp_parser* parser)
13904 tree clobbers = NULL_TREE;
13909 tree string_literal;
13911 /* Look for the string literal. */
13912 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13913 string_literal = token ? token->value : error_mark_node;
13914 /* Add it to the list. */
13915 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13916 /* If the next token is not a `,', then the list is
13918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13920 /* Consume the `,' token. */
13921 cp_lexer_consume_token (parser->lexer);
13927 /* Parse an (optional) series of attributes.
13930 attributes attribute
13933 __attribute__ (( attribute-list [opt] ))
13935 The return value is as for cp_parser_attribute_list. */
13938 cp_parser_attributes_opt (cp_parser* parser)
13940 tree attributes = NULL_TREE;
13945 tree attribute_list;
13947 /* Peek at the next token. */
13948 token = cp_lexer_peek_token (parser->lexer);
13949 /* If it's not `__attribute__', then we're done. */
13950 if (token->keyword != RID_ATTRIBUTE)
13953 /* Consume the `__attribute__' keyword. */
13954 cp_lexer_consume_token (parser->lexer);
13955 /* Look for the two `(' tokens. */
13956 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13957 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13959 /* Peek at the next token. */
13960 token = cp_lexer_peek_token (parser->lexer);
13961 if (token->type != CPP_CLOSE_PAREN)
13962 /* Parse the attribute-list. */
13963 attribute_list = cp_parser_attribute_list (parser);
13965 /* If the next token is a `)', then there is no attribute
13967 attribute_list = NULL;
13969 /* Look for the two `)' tokens. */
13970 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13971 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13973 /* Add these new attributes to the list. */
13974 attributes = chainon (attributes, attribute_list);
13980 /* Parse an attribute-list.
13984 attribute-list , attribute
13988 identifier ( identifier )
13989 identifier ( identifier , expression-list )
13990 identifier ( expression-list )
13992 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13993 TREE_PURPOSE of each node is the identifier indicating which
13994 attribute is in use. The TREE_VALUE represents the arguments, if
13998 cp_parser_attribute_list (cp_parser* parser)
14000 tree attribute_list = NULL_TREE;
14002 c_lex_string_translate = 0;
14009 /* Look for the identifier. We also allow keywords here; for
14010 example `__attribute__ ((const))' is legal. */
14011 token = cp_lexer_peek_token (parser->lexer);
14012 if (token->type != CPP_NAME
14013 && token->type != CPP_KEYWORD)
14014 return error_mark_node;
14015 /* Consume the token. */
14016 token = cp_lexer_consume_token (parser->lexer);
14018 /* Save away the identifier that indicates which attribute this is. */
14019 identifier = token->value;
14020 attribute = build_tree_list (identifier, NULL_TREE);
14022 /* Peek at the next token. */
14023 token = cp_lexer_peek_token (parser->lexer);
14024 /* If it's an `(', then parse the attribute arguments. */
14025 if (token->type == CPP_OPEN_PAREN)
14029 arguments = (cp_parser_parenthesized_expression_list
14030 (parser, true, /*non_constant_p=*/NULL));
14031 /* Save the identifier and arguments away. */
14032 TREE_VALUE (attribute) = arguments;
14035 /* Add this attribute to the list. */
14036 TREE_CHAIN (attribute) = attribute_list;
14037 attribute_list = attribute;
14039 /* Now, look for more attributes. */
14040 token = cp_lexer_peek_token (parser->lexer);
14041 /* If the next token isn't a `,', we're done. */
14042 if (token->type != CPP_COMMA)
14045 /* Consume the comma and keep going. */
14046 cp_lexer_consume_token (parser->lexer);
14048 c_lex_string_translate = 1;
14050 /* We built up the list in reverse order. */
14051 return nreverse (attribute_list);
14054 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14055 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14056 current value of the PEDANTIC flag, regardless of whether or not
14057 the `__extension__' keyword is present. The caller is responsible
14058 for restoring the value of the PEDANTIC flag. */
14061 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14063 /* Save the old value of the PEDANTIC flag. */
14064 *saved_pedantic = pedantic;
14066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14068 /* Consume the `__extension__' token. */
14069 cp_lexer_consume_token (parser->lexer);
14070 /* We're not being pedantic while the `__extension__' keyword is
14080 /* Parse a label declaration.
14083 __label__ label-declarator-seq ;
14085 label-declarator-seq:
14086 identifier , label-declarator-seq
14090 cp_parser_label_declaration (cp_parser* parser)
14092 /* Look for the `__label__' keyword. */
14093 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14099 /* Look for an identifier. */
14100 identifier = cp_parser_identifier (parser);
14101 /* Declare it as a lobel. */
14102 finish_label_decl (identifier);
14103 /* If the next token is a `;', stop. */
14104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14106 /* Look for the `,' separating the label declarations. */
14107 cp_parser_require (parser, CPP_COMMA, "`,'");
14110 /* Look for the final `;'. */
14111 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14114 /* Support Functions */
14116 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14117 NAME should have one of the representations used for an
14118 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14119 is returned. If PARSER->SCOPE is a dependent type, then a
14120 SCOPE_REF is returned.
14122 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14123 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14124 was formed. Abstractly, such entities should not be passed to this
14125 function, because they do not need to be looked up, but it is
14126 simpler to check for this special case here, rather than at the
14129 In cases not explicitly covered above, this function returns a
14130 DECL, OVERLOAD, or baselink representing the result of the lookup.
14131 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14134 If IS_TYPE is TRUE, bindings that do not refer to types are
14137 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14140 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14143 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14146 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14147 results in an ambiguity, and false otherwise. */
14150 cp_parser_lookup_name (cp_parser *parser, tree name,
14151 bool is_type, bool is_template, bool is_namespace,
14152 bool check_dependency,
14156 tree object_type = parser->context->object_type;
14158 /* Assume that the lookup will be unambiguous. */
14160 *ambiguous_p = false;
14162 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14163 no longer valid. Note that if we are parsing tentatively, and
14164 the parse fails, OBJECT_TYPE will be automatically restored. */
14165 parser->context->object_type = NULL_TREE;
14167 if (name == error_mark_node)
14168 return error_mark_node;
14170 /* A template-id has already been resolved; there is no lookup to
14172 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14174 if (BASELINK_P (name))
14176 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14177 == TEMPLATE_ID_EXPR);
14181 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14182 it should already have been checked to make sure that the name
14183 used matches the type being destroyed. */
14184 if (TREE_CODE (name) == BIT_NOT_EXPR)
14188 /* Figure out to which type this destructor applies. */
14190 type = parser->scope;
14191 else if (object_type)
14192 type = object_type;
14194 type = current_class_type;
14195 /* If that's not a class type, there is no destructor. */
14196 if (!type || !CLASS_TYPE_P (type))
14197 return error_mark_node;
14198 if (!CLASSTYPE_DESTRUCTORS (type))
14199 return error_mark_node;
14200 /* If it was a class type, return the destructor. */
14201 return CLASSTYPE_DESTRUCTORS (type);
14204 /* By this point, the NAME should be an ordinary identifier. If
14205 the id-expression was a qualified name, the qualifying scope is
14206 stored in PARSER->SCOPE at this point. */
14207 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14209 /* Perform the lookup. */
14214 if (parser->scope == error_mark_node)
14215 return error_mark_node;
14217 /* If the SCOPE is dependent, the lookup must be deferred until
14218 the template is instantiated -- unless we are explicitly
14219 looking up names in uninstantiated templates. Even then, we
14220 cannot look up the name if the scope is not a class type; it
14221 might, for example, be a template type parameter. */
14222 dependent_p = (TYPE_P (parser->scope)
14223 && !(parser->in_declarator_p
14224 && currently_open_class (parser->scope))
14225 && dependent_type_p (parser->scope));
14226 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14230 /* The resolution to Core Issue 180 says that `struct A::B'
14231 should be considered a type-name, even if `A' is
14233 decl = TYPE_NAME (make_typename_type (parser->scope,
14236 else if (is_template)
14237 decl = make_unbound_class_template (parser->scope,
14241 decl = build_nt (SCOPE_REF, parser->scope, name);
14245 bool pop_p = false;
14247 /* If PARSER->SCOPE is a dependent type, then it must be a
14248 class type, and we must not be checking dependencies;
14249 otherwise, we would have processed this lookup above. So
14250 that PARSER->SCOPE is not considered a dependent base by
14251 lookup_member, we must enter the scope here. */
14253 pop_p = push_scope (parser->scope);
14254 /* If the PARSER->SCOPE is a a template specialization, it
14255 may be instantiated during name lookup. In that case,
14256 errors may be issued. Even if we rollback the current
14257 tentative parse, those errors are valid. */
14258 decl = lookup_qualified_name (parser->scope, name, is_type,
14259 /*complain=*/true);
14261 pop_scope (parser->scope);
14263 parser->qualifying_scope = parser->scope;
14264 parser->object_scope = NULL_TREE;
14266 else if (object_type)
14268 tree object_decl = NULL_TREE;
14269 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14270 OBJECT_TYPE is not a class. */
14271 if (CLASS_TYPE_P (object_type))
14272 /* If the OBJECT_TYPE is a template specialization, it may
14273 be instantiated during name lookup. In that case, errors
14274 may be issued. Even if we rollback the current tentative
14275 parse, those errors are valid. */
14276 object_decl = lookup_member (object_type,
14278 /*protect=*/0, is_type);
14279 /* Look it up in the enclosing context, too. */
14280 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14281 /*block_p=*/true, is_namespace,
14283 parser->object_scope = object_type;
14284 parser->qualifying_scope = NULL_TREE;
14286 decl = object_decl;
14290 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14291 /*block_p=*/true, is_namespace,
14293 parser->qualifying_scope = NULL_TREE;
14294 parser->object_scope = NULL_TREE;
14297 /* If the lookup failed, let our caller know. */
14299 || decl == error_mark_node
14300 || (TREE_CODE (decl) == FUNCTION_DECL
14301 && DECL_ANTICIPATED (decl)))
14302 return error_mark_node;
14304 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14305 if (TREE_CODE (decl) == TREE_LIST)
14308 *ambiguous_p = true;
14309 /* The error message we have to print is too complicated for
14310 cp_parser_error, so we incorporate its actions directly. */
14311 if (!cp_parser_simulate_error (parser))
14313 error ("reference to `%D' is ambiguous", name);
14314 print_candidates (decl);
14316 return error_mark_node;
14319 gcc_assert (DECL_P (decl)
14320 || TREE_CODE (decl) == OVERLOAD
14321 || TREE_CODE (decl) == SCOPE_REF
14322 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14323 || BASELINK_P (decl));
14325 /* If we have resolved the name of a member declaration, check to
14326 see if the declaration is accessible. When the name resolves to
14327 set of overloaded functions, accessibility is checked when
14328 overload resolution is done.
14330 During an explicit instantiation, access is not checked at all,
14331 as per [temp.explicit]. */
14333 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14338 /* Like cp_parser_lookup_name, but for use in the typical case where
14339 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14340 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14343 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14345 return cp_parser_lookup_name (parser, name,
14347 /*is_template=*/false,
14348 /*is_namespace=*/false,
14349 /*check_dependency=*/true,
14350 /*ambiguous_p=*/NULL);
14353 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14354 the current context, return the TYPE_DECL. If TAG_NAME_P is
14355 true, the DECL indicates the class being defined in a class-head,
14356 or declared in an elaborated-type-specifier.
14358 Otherwise, return DECL. */
14361 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14363 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14364 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14367 template <typename T> struct B;
14370 template <typename T> struct A::B {};
14372 Similarly, in a elaborated-type-specifier:
14374 namespace N { struct X{}; }
14377 template <typename T> friend struct N::X;
14380 However, if the DECL refers to a class type, and we are in
14381 the scope of the class, then the name lookup automatically
14382 finds the TYPE_DECL created by build_self_reference rather
14383 than a TEMPLATE_DECL. For example, in:
14385 template <class T> struct S {
14389 there is no need to handle such case. */
14391 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14392 return DECL_TEMPLATE_RESULT (decl);
14397 /* If too many, or too few, template-parameter lists apply to the
14398 declarator, issue an error message. Returns TRUE if all went well,
14399 and FALSE otherwise. */
14402 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14403 cp_declarator *declarator)
14405 unsigned num_templates;
14407 /* We haven't seen any classes that involve template parameters yet. */
14410 switch (declarator->kind)
14413 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14418 scope = TREE_OPERAND (declarator->u.id.name, 0);
14419 member = TREE_OPERAND (declarator->u.id.name, 1);
14421 while (scope && CLASS_TYPE_P (scope))
14423 /* You're supposed to have one `template <...>'
14424 for every template class, but you don't need one
14425 for a full specialization. For example:
14427 template <class T> struct S{};
14428 template <> struct S<int> { void f(); };
14429 void S<int>::f () {}
14431 is correct; there shouldn't be a `template <>' for
14432 the definition of `S<int>::f'. */
14433 if (CLASSTYPE_TEMPLATE_INFO (scope)
14434 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14435 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14436 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14439 scope = TYPE_CONTEXT (scope);
14443 /* If the DECLARATOR has the form `X<y>' then it uses one
14444 additional level of template parameters. */
14445 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14448 return cp_parser_check_template_parameters (parser,
14454 case cdk_reference:
14456 return (cp_parser_check_declarator_template_parameters
14457 (parser, declarator->declarator));
14463 gcc_unreachable ();
14468 /* NUM_TEMPLATES were used in the current declaration. If that is
14469 invalid, return FALSE and issue an error messages. Otherwise,
14473 cp_parser_check_template_parameters (cp_parser* parser,
14474 unsigned num_templates)
14476 /* If there are more template classes than parameter lists, we have
14479 template <class T> void S<T>::R<T>::f (); */
14480 if (parser->num_template_parameter_lists < num_templates)
14482 error ("too few template-parameter-lists");
14485 /* If there are the same number of template classes and parameter
14486 lists, that's OK. */
14487 if (parser->num_template_parameter_lists == num_templates)
14489 /* If there are more, but only one more, then we are referring to a
14490 member template. That's OK too. */
14491 if (parser->num_template_parameter_lists == num_templates + 1)
14493 /* Otherwise, there are too many template parameter lists. We have
14496 template <class T> template <class U> void S::f(); */
14497 error ("too many template-parameter-lists");
14501 /* Parse a binary-expression of the general form:
14505 binary-expression <token> <expr>
14507 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14508 to parser the <expr>s. If the first production is used, then the
14509 value returned by FN is returned directly. Otherwise, a node with
14510 the indicated EXPR_TYPE is returned, with operands corresponding to
14511 the two sub-expressions. */
14514 cp_parser_binary_expression (cp_parser* parser,
14515 const cp_parser_token_tree_map token_tree_map,
14516 cp_parser_expression_fn fn)
14520 /* Parse the first expression. */
14521 lhs = (*fn) (parser);
14522 /* Now, look for more expressions. */
14526 const cp_parser_token_tree_map_node *map_node;
14529 /* Peek at the next token. */
14530 token = cp_lexer_peek_token (parser->lexer);
14531 /* If the token is `>', and that's not an operator at the
14532 moment, then we're done. */
14533 if (token->type == CPP_GREATER
14534 && !parser->greater_than_is_operator_p)
14536 /* If we find one of the tokens we want, build the corresponding
14537 tree representation. */
14538 for (map_node = token_tree_map;
14539 map_node->token_type != CPP_EOF;
14541 if (map_node->token_type == token->type)
14543 /* Assume that an overloaded operator will not be used. */
14544 bool overloaded_p = false;
14546 /* Consume the operator token. */
14547 cp_lexer_consume_token (parser->lexer);
14548 /* Parse the right-hand side of the expression. */
14549 rhs = (*fn) (parser);
14550 /* Build the binary tree node. */
14551 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14553 /* If the binary operator required the use of an
14554 overloaded operator, then this expression cannot be an
14555 integral constant-expression. An overloaded operator
14556 can be used even if both operands are otherwise
14557 permissible in an integral constant-expression if at
14558 least one of the operands is of enumeration type. */
14560 && (cp_parser_non_integral_constant_expression
14561 (parser, "calls to overloaded operators")))
14562 lhs = error_mark_node;
14566 /* If the token wasn't one of the ones we want, we're done. */
14567 if (map_node->token_type == CPP_EOF)
14574 /* Parse an optional `::' token indicating that the following name is
14575 from the global namespace. If so, PARSER->SCOPE is set to the
14576 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14577 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14578 Returns the new value of PARSER->SCOPE, if the `::' token is
14579 present, and NULL_TREE otherwise. */
14582 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14586 /* Peek at the next token. */
14587 token = cp_lexer_peek_token (parser->lexer);
14588 /* If we're looking at a `::' token then we're starting from the
14589 global namespace, not our current location. */
14590 if (token->type == CPP_SCOPE)
14592 /* Consume the `::' token. */
14593 cp_lexer_consume_token (parser->lexer);
14594 /* Set the SCOPE so that we know where to start the lookup. */
14595 parser->scope = global_namespace;
14596 parser->qualifying_scope = global_namespace;
14597 parser->object_scope = NULL_TREE;
14599 return parser->scope;
14601 else if (!current_scope_valid_p)
14603 parser->scope = NULL_TREE;
14604 parser->qualifying_scope = NULL_TREE;
14605 parser->object_scope = NULL_TREE;
14611 /* Returns TRUE if the upcoming token sequence is the start of a
14612 constructor declarator. If FRIEND_P is true, the declarator is
14613 preceded by the `friend' specifier. */
14616 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14618 bool constructor_p;
14619 tree type_decl = NULL_TREE;
14620 bool nested_name_p;
14621 cp_token *next_token;
14623 /* The common case is that this is not a constructor declarator, so
14624 try to avoid doing lots of work if at all possible. It's not
14625 valid declare a constructor at function scope. */
14626 if (at_function_scope_p ())
14628 /* And only certain tokens can begin a constructor declarator. */
14629 next_token = cp_lexer_peek_token (parser->lexer);
14630 if (next_token->type != CPP_NAME
14631 && next_token->type != CPP_SCOPE
14632 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14633 && next_token->type != CPP_TEMPLATE_ID)
14636 /* Parse tentatively; we are going to roll back all of the tokens
14638 cp_parser_parse_tentatively (parser);
14639 /* Assume that we are looking at a constructor declarator. */
14640 constructor_p = true;
14642 /* Look for the optional `::' operator. */
14643 cp_parser_global_scope_opt (parser,
14644 /*current_scope_valid_p=*/false);
14645 /* Look for the nested-name-specifier. */
14647 = (cp_parser_nested_name_specifier_opt (parser,
14648 /*typename_keyword_p=*/false,
14649 /*check_dependency_p=*/false,
14651 /*is_declaration=*/false)
14653 /* Outside of a class-specifier, there must be a
14654 nested-name-specifier. */
14655 if (!nested_name_p &&
14656 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14658 constructor_p = false;
14659 /* If we still think that this might be a constructor-declarator,
14660 look for a class-name. */
14665 template <typename T> struct S { S(); };
14666 template <typename T> S<T>::S ();
14668 we must recognize that the nested `S' names a class.
14671 template <typename T> S<T>::S<T> ();
14673 we must recognize that the nested `S' names a template. */
14674 type_decl = cp_parser_class_name (parser,
14675 /*typename_keyword_p=*/false,
14676 /*template_keyword_p=*/false,
14678 /*check_dependency_p=*/false,
14679 /*class_head_p=*/false,
14680 /*is_declaration=*/false);
14681 /* If there was no class-name, then this is not a constructor. */
14682 constructor_p = !cp_parser_error_occurred (parser);
14685 /* If we're still considering a constructor, we have to see a `(',
14686 to begin the parameter-declaration-clause, followed by either a
14687 `)', an `...', or a decl-specifier. We need to check for a
14688 type-specifier to avoid being fooled into thinking that:
14692 is a constructor. (It is actually a function named `f' that
14693 takes one parameter (of type `int') and returns a value of type
14696 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14699 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14700 /* A parameter declaration begins with a decl-specifier,
14701 which is either the "attribute" keyword, a storage class
14702 specifier, or (usually) a type-specifier. */
14703 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14704 && !cp_parser_storage_class_specifier_opt (parser))
14707 bool pop_p = false;
14708 unsigned saved_num_template_parameter_lists;
14710 /* Names appearing in the type-specifier should be looked up
14711 in the scope of the class. */
14712 if (current_class_type)
14716 type = TREE_TYPE (type_decl);
14717 if (TREE_CODE (type) == TYPENAME_TYPE)
14719 type = resolve_typename_type (type,
14720 /*only_current_p=*/false);
14721 if (type == error_mark_node)
14723 cp_parser_abort_tentative_parse (parser);
14727 pop_p = push_scope (type);
14730 /* Inside the constructor parameter list, surrounding
14731 template-parameter-lists do not apply. */
14732 saved_num_template_parameter_lists
14733 = parser->num_template_parameter_lists;
14734 parser->num_template_parameter_lists = 0;
14736 /* Look for the type-specifier. */
14737 cp_parser_type_specifier (parser,
14738 CP_PARSER_FLAGS_NONE,
14739 /*decl_specs=*/NULL,
14740 /*is_declarator=*/true,
14741 /*declares_class_or_enum=*/NULL,
14742 /*is_cv_qualifier=*/NULL);
14744 parser->num_template_parameter_lists
14745 = saved_num_template_parameter_lists;
14747 /* Leave the scope of the class. */
14751 constructor_p = !cp_parser_error_occurred (parser);
14755 constructor_p = false;
14756 /* We did not really want to consume any tokens. */
14757 cp_parser_abort_tentative_parse (parser);
14759 return constructor_p;
14762 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14763 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14764 they must be performed once we are in the scope of the function.
14766 Returns the function defined. */
14769 cp_parser_function_definition_from_specifiers_and_declarator
14770 (cp_parser* parser,
14771 cp_decl_specifier_seq *decl_specifiers,
14773 const cp_declarator *declarator)
14778 /* Begin the function-definition. */
14779 success_p = start_function (decl_specifiers, declarator, attributes);
14781 /* The things we're about to see are not directly qualified by any
14782 template headers we've seen thus far. */
14783 reset_specialization ();
14785 /* If there were names looked up in the decl-specifier-seq that we
14786 did not check, check them now. We must wait until we are in the
14787 scope of the function to perform the checks, since the function
14788 might be a friend. */
14789 perform_deferred_access_checks ();
14793 /* Skip the entire function. */
14794 error ("invalid function declaration");
14795 cp_parser_skip_to_end_of_block_or_statement (parser);
14796 fn = error_mark_node;
14799 fn = cp_parser_function_definition_after_declarator (parser,
14800 /*inline_p=*/false);
14805 /* Parse the part of a function-definition that follows the
14806 declarator. INLINE_P is TRUE iff this function is an inline
14807 function defined with a class-specifier.
14809 Returns the function defined. */
14812 cp_parser_function_definition_after_declarator (cp_parser* parser,
14816 bool ctor_initializer_p = false;
14817 bool saved_in_unbraced_linkage_specification_p;
14818 unsigned saved_num_template_parameter_lists;
14820 /* If the next token is `return', then the code may be trying to
14821 make use of the "named return value" extension that G++ used to
14823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14825 /* Consume the `return' keyword. */
14826 cp_lexer_consume_token (parser->lexer);
14827 /* Look for the identifier that indicates what value is to be
14829 cp_parser_identifier (parser);
14830 /* Issue an error message. */
14831 error ("named return values are no longer supported");
14832 /* Skip tokens until we reach the start of the function body. */
14833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14834 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14835 cp_lexer_consume_token (parser->lexer);
14837 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14838 anything declared inside `f'. */
14839 saved_in_unbraced_linkage_specification_p
14840 = parser->in_unbraced_linkage_specification_p;
14841 parser->in_unbraced_linkage_specification_p = false;
14842 /* Inside the function, surrounding template-parameter-lists do not
14844 saved_num_template_parameter_lists
14845 = parser->num_template_parameter_lists;
14846 parser->num_template_parameter_lists = 0;
14847 /* If the next token is `try', then we are looking at a
14848 function-try-block. */
14849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14850 ctor_initializer_p = cp_parser_function_try_block (parser);
14851 /* A function-try-block includes the function-body, so we only do
14852 this next part if we're not processing a function-try-block. */
14855 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14857 /* Finish the function. */
14858 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14859 (inline_p ? 2 : 0));
14860 /* Generate code for it, if necessary. */
14861 expand_or_defer_fn (fn);
14862 /* Restore the saved values. */
14863 parser->in_unbraced_linkage_specification_p
14864 = saved_in_unbraced_linkage_specification_p;
14865 parser->num_template_parameter_lists
14866 = saved_num_template_parameter_lists;
14871 /* Parse a template-declaration, assuming that the `export' (and
14872 `extern') keywords, if present, has already been scanned. MEMBER_P
14873 is as for cp_parser_template_declaration. */
14876 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14878 tree decl = NULL_TREE;
14879 tree parameter_list;
14880 bool friend_p = false;
14882 /* Look for the `template' keyword. */
14883 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14887 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14890 /* If the next token is `>', then we have an invalid
14891 specialization. Rather than complain about an invalid template
14892 parameter, issue an error message here. */
14893 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14895 cp_parser_error (parser, "invalid explicit specialization");
14896 begin_specialization ();
14897 parameter_list = NULL_TREE;
14901 /* Parse the template parameters. */
14902 begin_template_parm_list ();
14903 parameter_list = cp_parser_template_parameter_list (parser);
14904 parameter_list = end_template_parm_list (parameter_list);
14907 /* Look for the `>'. */
14908 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14909 /* We just processed one more parameter list. */
14910 ++parser->num_template_parameter_lists;
14911 /* If the next token is `template', there are more template
14913 if (cp_lexer_next_token_is_keyword (parser->lexer,
14915 cp_parser_template_declaration_after_export (parser, member_p);
14918 /* There are no access checks when parsing a template, as we do not
14919 know if a specialization will be a friend. */
14920 push_deferring_access_checks (dk_no_check);
14922 decl = cp_parser_single_declaration (parser,
14926 pop_deferring_access_checks ();
14928 /* If this is a member template declaration, let the front
14930 if (member_p && !friend_p && decl)
14932 if (TREE_CODE (decl) == TYPE_DECL)
14933 cp_parser_check_access_in_redeclaration (decl);
14935 decl = finish_member_template_decl (decl);
14937 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14938 make_friend_class (current_class_type, TREE_TYPE (decl),
14939 /*complain=*/true);
14941 /* We are done with the current parameter list. */
14942 --parser->num_template_parameter_lists;
14945 finish_template_decl (parameter_list);
14947 /* Register member declarations. */
14948 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14949 finish_member_declaration (decl);
14951 /* If DECL is a function template, we must return to parse it later.
14952 (Even though there is no definition, there might be default
14953 arguments that need handling.) */
14954 if (member_p && decl
14955 && (TREE_CODE (decl) == FUNCTION_DECL
14956 || DECL_FUNCTION_TEMPLATE_P (decl)))
14957 TREE_VALUE (parser->unparsed_functions_queues)
14958 = tree_cons (NULL_TREE, decl,
14959 TREE_VALUE (parser->unparsed_functions_queues));
14962 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14963 `function-definition' sequence. MEMBER_P is true, this declaration
14964 appears in a class scope.
14966 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14967 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14970 cp_parser_single_declaration (cp_parser* parser,
14974 int declares_class_or_enum;
14975 tree decl = NULL_TREE;
14976 cp_decl_specifier_seq decl_specifiers;
14977 bool function_definition_p = false;
14979 /* Defer access checks until we know what is being declared. */
14980 push_deferring_access_checks (dk_deferred);
14982 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14984 cp_parser_decl_specifier_seq (parser,
14985 CP_PARSER_FLAGS_OPTIONAL,
14987 &declares_class_or_enum);
14989 *friend_p = cp_parser_friend_p (&decl_specifiers);
14990 /* Gather up the access checks that occurred the
14991 decl-specifier-seq. */
14992 stop_deferring_access_checks ();
14994 /* Check for the declaration of a template class. */
14995 if (declares_class_or_enum)
14997 if (cp_parser_declares_only_class_p (parser))
14999 decl = shadow_tag (&decl_specifiers);
15000 if (decl && decl != error_mark_node)
15001 decl = TYPE_NAME (decl);
15003 decl = error_mark_node;
15008 /* If it's not a template class, try for a template function. If
15009 the next token is a `;', then this declaration does not declare
15010 anything. But, if there were errors in the decl-specifiers, then
15011 the error might well have come from an attempted class-specifier.
15012 In that case, there's no need to warn about a missing declarator. */
15014 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15015 || decl_specifiers.type != error_mark_node))
15016 decl = cp_parser_init_declarator (parser,
15018 /*function_definition_allowed_p=*/true,
15020 declares_class_or_enum,
15021 &function_definition_p);
15023 pop_deferring_access_checks ();
15025 /* Clear any current qualification; whatever comes next is the start
15026 of something new. */
15027 parser->scope = NULL_TREE;
15028 parser->qualifying_scope = NULL_TREE;
15029 parser->object_scope = NULL_TREE;
15030 /* Look for a trailing `;' after the declaration. */
15031 if (!function_definition_p
15032 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
15033 cp_parser_skip_to_end_of_block_or_statement (parser);
15038 /* Parse a cast-expression that is not the operand of a unary "&". */
15041 cp_parser_simple_cast_expression (cp_parser *parser)
15043 return cp_parser_cast_expression (parser, /*address_p=*/false);
15046 /* Parse a functional cast to TYPE. Returns an expression
15047 representing the cast. */
15050 cp_parser_functional_cast (cp_parser* parser, tree type)
15052 tree expression_list;
15056 = cp_parser_parenthesized_expression_list (parser, false,
15057 /*non_constant_p=*/NULL);
15059 cast = build_functional_cast (type, expression_list);
15060 /* [expr.const]/1: In an integral constant expression "only type
15061 conversions to integral or enumeration type can be used". */
15062 if (cast != error_mark_node && !type_dependent_expression_p (type)
15063 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15065 if (cp_parser_non_integral_constant_expression
15066 (parser, "a call to a constructor"))
15067 return error_mark_node;
15072 /* Save the tokens that make up the body of a member function defined
15073 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15074 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15075 specifiers applied to the declaration. Returns the FUNCTION_DECL
15076 for the member function. */
15079 cp_parser_save_member_function_body (cp_parser* parser,
15080 cp_decl_specifier_seq *decl_specifiers,
15081 cp_declarator *declarator,
15084 cp_token_cache *cache;
15087 /* Create the function-declaration. */
15088 fn = start_method (decl_specifiers, declarator, attributes);
15089 /* If something went badly wrong, bail out now. */
15090 if (fn == error_mark_node)
15092 /* If there's a function-body, skip it. */
15093 if (cp_parser_token_starts_function_definition_p
15094 (cp_lexer_peek_token (parser->lexer)))
15095 cp_parser_skip_to_end_of_block_or_statement (parser);
15096 return error_mark_node;
15099 /* Remember it, if there default args to post process. */
15100 cp_parser_save_default_args (parser, fn);
15102 /* Create a token cache. */
15103 cache = cp_token_cache_new ();
15104 /* Save away the tokens that make up the body of the
15106 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15107 /* Handle function try blocks. */
15108 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15109 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15111 /* Save away the inline definition; we will process it when the
15112 class is complete. */
15113 DECL_PENDING_INLINE_INFO (fn) = cache;
15114 DECL_PENDING_INLINE_P (fn) = 1;
15116 /* We need to know that this was defined in the class, so that
15117 friend templates are handled correctly. */
15118 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15120 /* We're done with the inline definition. */
15121 finish_method (fn);
15123 /* Add FN to the queue of functions to be parsed later. */
15124 TREE_VALUE (parser->unparsed_functions_queues)
15125 = tree_cons (NULL_TREE, fn,
15126 TREE_VALUE (parser->unparsed_functions_queues));
15131 /* Parse a template-argument-list, as well as the trailing ">" (but
15132 not the opening ">"). See cp_parser_template_argument_list for the
15136 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15140 tree saved_qualifying_scope;
15141 tree saved_object_scope;
15142 bool saved_greater_than_is_operator_p;
15146 When parsing a template-id, the first non-nested `>' is taken as
15147 the end of the template-argument-list rather than a greater-than
15149 saved_greater_than_is_operator_p
15150 = parser->greater_than_is_operator_p;
15151 parser->greater_than_is_operator_p = false;
15152 /* Parsing the argument list may modify SCOPE, so we save it
15154 saved_scope = parser->scope;
15155 saved_qualifying_scope = parser->qualifying_scope;
15156 saved_object_scope = parser->object_scope;
15157 /* Parse the template-argument-list itself. */
15158 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15159 arguments = NULL_TREE;
15161 arguments = cp_parser_template_argument_list (parser);
15162 /* Look for the `>' that ends the template-argument-list. If we find
15163 a '>>' instead, it's probably just a typo. */
15164 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15166 if (!saved_greater_than_is_operator_p)
15168 /* If we're in a nested template argument list, the '>>' has to be
15169 a typo for '> >'. We emit the error message, but we continue
15170 parsing and we push a '>' as next token, so that the argument
15171 list will be parsed correctly.. */
15173 error ("`>>' should be `> >' within a nested template argument list");
15174 token = cp_lexer_peek_token (parser->lexer);
15175 token->type = CPP_GREATER;
15179 /* If this is not a nested template argument list, the '>>' is
15180 a typo for '>'. Emit an error message and continue. */
15181 error ("spurious `>>', use `>' to terminate a template argument list");
15182 cp_lexer_consume_token (parser->lexer);
15185 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15186 error ("missing `>' to terminate the template argument list");
15187 /* The `>' token might be a greater-than operator again now. */
15188 parser->greater_than_is_operator_p
15189 = saved_greater_than_is_operator_p;
15190 /* Restore the SAVED_SCOPE. */
15191 parser->scope = saved_scope;
15192 parser->qualifying_scope = saved_qualifying_scope;
15193 parser->object_scope = saved_object_scope;
15198 /* MEMBER_FUNCTION is a member function, or a friend. If default
15199 arguments, or the body of the function have not yet been parsed,
15203 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15205 cp_lexer *saved_lexer;
15207 /* If this member is a template, get the underlying
15209 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15210 member_function = DECL_TEMPLATE_RESULT (member_function);
15212 /* There should not be any class definitions in progress at this
15213 point; the bodies of members are only parsed outside of all class
15215 gcc_assert (parser->num_classes_being_defined == 0);
15216 /* While we're parsing the member functions we might encounter more
15217 classes. We want to handle them right away, but we don't want
15218 them getting mixed up with functions that are currently in the
15220 parser->unparsed_functions_queues
15221 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15223 /* Make sure that any template parameters are in scope. */
15224 maybe_begin_member_template_processing (member_function);
15226 /* If the body of the function has not yet been parsed, parse it
15228 if (DECL_PENDING_INLINE_P (member_function))
15230 tree function_scope;
15231 cp_token_cache *tokens;
15233 /* The function is no longer pending; we are processing it. */
15234 tokens = DECL_PENDING_INLINE_INFO (member_function);
15235 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15236 DECL_PENDING_INLINE_P (member_function) = 0;
15237 /* If this was an inline function in a local class, enter the scope
15238 of the containing function. */
15239 function_scope = decl_function_context (member_function);
15240 if (function_scope)
15241 push_function_context_to (function_scope);
15243 /* Save away the current lexer. */
15244 saved_lexer = parser->lexer;
15245 /* Make a new lexer to feed us the tokens saved for this function. */
15246 parser->lexer = cp_lexer_new_from_tokens (tokens);
15247 parser->lexer->next = saved_lexer;
15249 /* Set the current source position to be the location of the first
15250 token in the saved inline body. */
15251 cp_lexer_peek_token (parser->lexer);
15253 /* Let the front end know that we going to be defining this
15255 start_preparsed_function (member_function, NULL_TREE,
15256 SF_PRE_PARSED | SF_INCLASS_INLINE);
15258 /* Now, parse the body of the function. */
15259 cp_parser_function_definition_after_declarator (parser,
15260 /*inline_p=*/true);
15262 /* Leave the scope of the containing function. */
15263 if (function_scope)
15264 pop_function_context_from (function_scope);
15265 /* Restore the lexer. */
15266 parser->lexer = saved_lexer;
15269 /* Remove any template parameters from the symbol table. */
15270 maybe_end_member_template_processing ();
15272 /* Restore the queue. */
15273 parser->unparsed_functions_queues
15274 = TREE_CHAIN (parser->unparsed_functions_queues);
15277 /* If DECL contains any default args, remember it on the unparsed
15278 functions queue. */
15281 cp_parser_save_default_args (cp_parser* parser, tree decl)
15285 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15287 probe = TREE_CHAIN (probe))
15288 if (TREE_PURPOSE (probe))
15290 TREE_PURPOSE (parser->unparsed_functions_queues)
15291 = tree_cons (current_class_type, decl,
15292 TREE_PURPOSE (parser->unparsed_functions_queues));
15298 /* FN is a FUNCTION_DECL which may contains a parameter with an
15299 unparsed DEFAULT_ARG. Parse the default args now. This function
15300 assumes that the current scope is the scope in which the default
15301 argument should be processed. */
15304 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15306 cp_lexer *saved_lexer;
15307 cp_token_cache *tokens;
15308 bool saved_local_variables_forbidden_p;
15311 /* While we're parsing the default args, we might (due to the
15312 statement expression extension) encounter more classes. We want
15313 to handle them right away, but we don't want them getting mixed
15314 up with default args that are currently in the queue. */
15315 parser->unparsed_functions_queues
15316 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15318 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15320 parameters = TREE_CHAIN (parameters))
15322 if (!TREE_PURPOSE (parameters)
15323 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15326 /* Save away the current lexer. */
15327 saved_lexer = parser->lexer;
15328 /* Create a new one, using the tokens we have saved. */
15329 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
15330 parser->lexer = cp_lexer_new_from_tokens (tokens);
15332 /* Set the current source position to be the location of the
15333 first token in the default argument. */
15334 cp_lexer_peek_token (parser->lexer);
15336 /* Local variable names (and the `this' keyword) may not appear
15337 in a default argument. */
15338 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15339 parser->local_variables_forbidden_p = true;
15340 /* Parse the assignment-expression. */
15341 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15343 /* If the token stream has not been completely used up, then
15344 there was extra junk after the end of the default
15346 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15347 cp_parser_error (parser, "expected `,'");
15349 /* Restore saved state. */
15350 parser->lexer = saved_lexer;
15351 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15354 /* Restore the queue. */
15355 parser->unparsed_functions_queues
15356 = TREE_CHAIN (parser->unparsed_functions_queues);
15359 /* Parse the operand of `sizeof' (or a similar operator). Returns
15360 either a TYPE or an expression, depending on the form of the
15361 input. The KEYWORD indicates which kind of expression we have
15365 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15367 static const char *format;
15368 tree expr = NULL_TREE;
15369 const char *saved_message;
15370 bool saved_integral_constant_expression_p;
15372 /* Initialize FORMAT the first time we get here. */
15374 format = "types may not be defined in `%s' expressions";
15376 /* Types cannot be defined in a `sizeof' expression. Save away the
15378 saved_message = parser->type_definition_forbidden_message;
15379 /* And create the new one. */
15380 parser->type_definition_forbidden_message
15381 = xmalloc (strlen (format)
15382 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15384 sprintf ((char *) parser->type_definition_forbidden_message,
15385 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15387 /* The restrictions on constant-expressions do not apply inside
15388 sizeof expressions. */
15389 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15390 parser->integral_constant_expression_p = false;
15392 /* Do not actually evaluate the expression. */
15394 /* If it's a `(', then we might be looking at the type-id
15396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15399 bool saved_in_type_id_in_expr_p;
15401 /* We can't be sure yet whether we're looking at a type-id or an
15403 cp_parser_parse_tentatively (parser);
15404 /* Consume the `('. */
15405 cp_lexer_consume_token (parser->lexer);
15406 /* Parse the type-id. */
15407 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15408 parser->in_type_id_in_expr_p = true;
15409 type = cp_parser_type_id (parser);
15410 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15411 /* Now, look for the trailing `)'. */
15412 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15413 /* If all went well, then we're done. */
15414 if (cp_parser_parse_definitely (parser))
15416 cp_decl_specifier_seq decl_specs;
15418 /* Build a trivial decl-specifier-seq. */
15419 clear_decl_specs (&decl_specs);
15420 decl_specs.type = type;
15422 /* Call grokdeclarator to figure out what type this is. */
15423 expr = grokdeclarator (NULL,
15427 /*attrlist=*/NULL);
15431 /* If the type-id production did not work out, then we must be
15432 looking at the unary-expression production. */
15434 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15435 /* Go back to evaluating expressions. */
15438 /* Free the message we created. */
15439 free ((char *) parser->type_definition_forbidden_message);
15440 /* And restore the old one. */
15441 parser->type_definition_forbidden_message = saved_message;
15442 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15447 /* If the current declaration has no declarator, return true. */
15450 cp_parser_declares_only_class_p (cp_parser *parser)
15452 /* If the next token is a `;' or a `,' then there is no
15454 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15455 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15458 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15461 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15462 cp_storage_class storage_class)
15464 if (decl_specs->storage_class != sc_none)
15465 decl_specs->multiple_storage_classes_p = true;
15467 decl_specs->storage_class = storage_class;
15470 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15471 is true, the type is a user-defined type; otherwise it is a
15472 built-in type specified by a keyword. */
15475 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15477 bool user_defined_p)
15479 decl_specs->any_specifiers_p = true;
15481 /* If the user tries to redeclare a built-in type (with, for example,
15482 in "typedef int wchar_t;") we remember that this is what
15483 happened. In system headers, we ignore these declarations so
15484 that G++ can work with system headers that are not C++-safe. */
15485 if (decl_specs->specs[(int) ds_typedef]
15487 && (decl_specs->type
15488 || decl_specs->specs[(int) ds_long]
15489 || decl_specs->specs[(int) ds_short]
15490 || decl_specs->specs[(int) ds_unsigned]
15491 || decl_specs->specs[(int) ds_signed]))
15493 decl_specs->redefined_builtin_type = type_spec;
15494 if (!decl_specs->type)
15496 decl_specs->type = type_spec;
15497 decl_specs->user_defined_type_p = false;
15500 else if (decl_specs->type)
15501 decl_specs->multiple_types_p = true;
15504 decl_specs->type = type_spec;
15505 decl_specs->user_defined_type_p = user_defined_p;
15506 decl_specs->redefined_builtin_type = NULL_TREE;
15510 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15511 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15514 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15516 return decl_specifiers->specs[(int) ds_friend] != 0;
15519 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15520 issue an error message indicating that TOKEN_DESC was expected.
15522 Returns the token consumed, if the token had the appropriate type.
15523 Otherwise, returns NULL. */
15526 cp_parser_require (cp_parser* parser,
15527 enum cpp_ttype type,
15528 const char* token_desc)
15530 if (cp_lexer_next_token_is (parser->lexer, type))
15531 return cp_lexer_consume_token (parser->lexer);
15534 /* Output the MESSAGE -- unless we're parsing tentatively. */
15535 if (!cp_parser_simulate_error (parser))
15537 char *message = concat ("expected ", token_desc, NULL);
15538 cp_parser_error (parser, message);
15545 /* Like cp_parser_require, except that tokens will be skipped until
15546 the desired token is found. An error message is still produced if
15547 the next token is not as expected. */
15550 cp_parser_skip_until_found (cp_parser* parser,
15551 enum cpp_ttype type,
15552 const char* token_desc)
15555 unsigned nesting_depth = 0;
15557 if (cp_parser_require (parser, type, token_desc))
15560 /* Skip tokens until the desired token is found. */
15563 /* Peek at the next token. */
15564 token = cp_lexer_peek_token (parser->lexer);
15565 /* If we've reached the token we want, consume it and
15567 if (token->type == type && !nesting_depth)
15569 cp_lexer_consume_token (parser->lexer);
15572 /* If we've run out of tokens, stop. */
15573 if (token->type == CPP_EOF)
15575 if (token->type == CPP_OPEN_BRACE
15576 || token->type == CPP_OPEN_PAREN
15577 || token->type == CPP_OPEN_SQUARE)
15579 else if (token->type == CPP_CLOSE_BRACE
15580 || token->type == CPP_CLOSE_PAREN
15581 || token->type == CPP_CLOSE_SQUARE)
15583 if (nesting_depth-- == 0)
15586 /* Consume this token. */
15587 cp_lexer_consume_token (parser->lexer);
15591 /* If the next token is the indicated keyword, consume it. Otherwise,
15592 issue an error message indicating that TOKEN_DESC was expected.
15594 Returns the token consumed, if the token had the appropriate type.
15595 Otherwise, returns NULL. */
15598 cp_parser_require_keyword (cp_parser* parser,
15600 const char* token_desc)
15602 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15604 if (token && token->keyword != keyword)
15606 dyn_string_t error_msg;
15608 /* Format the error message. */
15609 error_msg = dyn_string_new (0);
15610 dyn_string_append_cstr (error_msg, "expected ");
15611 dyn_string_append_cstr (error_msg, token_desc);
15612 cp_parser_error (parser, error_msg->s);
15613 dyn_string_delete (error_msg);
15620 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15621 function-definition. */
15624 cp_parser_token_starts_function_definition_p (cp_token* token)
15626 return (/* An ordinary function-body begins with an `{'. */
15627 token->type == CPP_OPEN_BRACE
15628 /* A ctor-initializer begins with a `:'. */
15629 || token->type == CPP_COLON
15630 /* A function-try-block begins with `try'. */
15631 || token->keyword == RID_TRY
15632 /* The named return value extension begins with `return'. */
15633 || token->keyword == RID_RETURN);
15636 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15640 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15644 token = cp_lexer_peek_token (parser->lexer);
15645 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15648 /* Returns TRUE iff the next token is the "," or ">" ending a
15649 template-argument. ">>" is also accepted (after the full
15650 argument was parsed) because it's probably a typo for "> >",
15651 and there is a specific diagnostic for this. */
15654 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15658 token = cp_lexer_peek_token (parser->lexer);
15659 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15660 || token->type == CPP_RSHIFT);
15663 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15664 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15667 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15672 token = cp_lexer_peek_nth_token (parser->lexer, n);
15673 if (token->type == CPP_LESS)
15675 /* Check for the sequence `<::' in the original code. It would be lexed as
15676 `[:', where `[' is a digraph, and there is no whitespace before
15678 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15681 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15682 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15688 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15689 or none_type otherwise. */
15691 static enum tag_types
15692 cp_parser_token_is_class_key (cp_token* token)
15694 switch (token->keyword)
15699 return record_type;
15708 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15711 cp_parser_check_class_key (enum tag_types class_key, tree type)
15713 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15714 pedwarn ("`%s' tag used in naming `%#T'",
15715 class_key == union_type ? "union"
15716 : class_key == record_type ? "struct" : "class",
15720 /* Issue an error message if DECL is redeclared with different
15721 access than its original declaration [class.access.spec/3].
15722 This applies to nested classes and nested class templates.
15725 static void cp_parser_check_access_in_redeclaration (tree decl)
15727 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15730 if ((TREE_PRIVATE (decl)
15731 != (current_access_specifier == access_private_node))
15732 || (TREE_PROTECTED (decl)
15733 != (current_access_specifier == access_protected_node)))
15734 error ("%D redeclared with different access", decl);
15737 /* Look for the `template' keyword, as a syntactic disambiguator.
15738 Return TRUE iff it is present, in which case it will be
15742 cp_parser_optional_template_keyword (cp_parser *parser)
15744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15746 /* The `template' keyword can only be used within templates;
15747 outside templates the parser can always figure out what is a
15748 template and what is not. */
15749 if (!processing_template_decl)
15751 error ("`template' (as a disambiguator) is only allowed "
15752 "within templates");
15753 /* If this part of the token stream is rescanned, the same
15754 error message would be generated. So, we purge the token
15755 from the stream. */
15756 cp_lexer_purge_token (parser->lexer);
15761 /* Consume the `template' keyword. */
15762 cp_lexer_consume_token (parser->lexer);
15770 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15771 set PARSER->SCOPE, and perform other related actions. */
15774 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15779 /* Get the stored value. */
15780 value = cp_lexer_consume_token (parser->lexer)->value;
15781 /* Perform any access checks that were deferred. */
15782 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15783 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15784 /* Set the scope from the stored value. */
15785 parser->scope = TREE_VALUE (value);
15786 parser->qualifying_scope = TREE_TYPE (value);
15787 parser->object_scope = NULL_TREE;
15790 /* Add tokens to CACHE until a non-nested END token appears. */
15793 cp_parser_cache_group_1 (cp_parser *parser,
15794 cp_token_cache *cache,
15795 enum cpp_ttype end,
15802 /* Abort a parenthesized expression if we encounter a brace. */
15803 if ((end == CPP_CLOSE_PAREN || depth == 0)
15804 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15806 /* If we've reached the end of the file, stop. */
15807 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15809 /* Consume the next token. */
15810 token = cp_lexer_consume_token (parser->lexer);
15811 /* Add this token to the tokens we are saving. */
15812 cp_token_cache_push_token (cache, token);
15813 /* See if it starts a new group. */
15814 if (token->type == CPP_OPEN_BRACE)
15816 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15820 else if (token->type == CPP_OPEN_PAREN)
15821 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15822 else if (token->type == end)
15827 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15828 preserve string tokens in both translated and untranslated
15832 cp_parser_cache_group (cp_parser *parser,
15833 cp_token_cache *cache,
15834 enum cpp_ttype end,
15837 int saved_c_lex_string_translate;
15839 saved_c_lex_string_translate = c_lex_string_translate;
15840 c_lex_string_translate = -1;
15842 cp_parser_cache_group_1 (parser, cache, end, depth);
15844 c_lex_string_translate = saved_c_lex_string_translate;
15848 /* Begin parsing tentatively. We always save tokens while parsing
15849 tentatively so that if the tentative parsing fails we can restore the
15853 cp_parser_parse_tentatively (cp_parser* parser)
15855 /* Enter a new parsing context. */
15856 parser->context = cp_parser_context_new (parser->context);
15857 /* Begin saving tokens. */
15858 cp_lexer_save_tokens (parser->lexer);
15859 /* In order to avoid repetitive access control error messages,
15860 access checks are queued up until we are no longer parsing
15862 push_deferring_access_checks (dk_deferred);
15865 /* Commit to the currently active tentative parse. */
15868 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15870 cp_parser_context *context;
15873 /* Mark all of the levels as committed. */
15874 lexer = parser->lexer;
15875 for (context = parser->context; context->next; context = context->next)
15877 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15879 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15880 while (!cp_lexer_saving_tokens (lexer))
15881 lexer = lexer->next;
15882 cp_lexer_commit_tokens (lexer);
15886 /* Abort the currently active tentative parse. All consumed tokens
15887 will be rolled back, and no diagnostics will be issued. */
15890 cp_parser_abort_tentative_parse (cp_parser* parser)
15892 cp_parser_simulate_error (parser);
15893 /* Now, pretend that we want to see if the construct was
15894 successfully parsed. */
15895 cp_parser_parse_definitely (parser);
15898 /* Stop parsing tentatively. If a parse error has occurred, restore the
15899 token stream. Otherwise, commit to the tokens we have consumed.
15900 Returns true if no error occurred; false otherwise. */
15903 cp_parser_parse_definitely (cp_parser* parser)
15905 bool error_occurred;
15906 cp_parser_context *context;
15908 /* Remember whether or not an error occurred, since we are about to
15909 destroy that information. */
15910 error_occurred = cp_parser_error_occurred (parser);
15911 /* Remove the topmost context from the stack. */
15912 context = parser->context;
15913 parser->context = context->next;
15914 /* If no parse errors occurred, commit to the tentative parse. */
15915 if (!error_occurred)
15917 /* Commit to the tokens read tentatively, unless that was
15919 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15920 cp_lexer_commit_tokens (parser->lexer);
15922 pop_to_parent_deferring_access_checks ();
15924 /* Otherwise, if errors occurred, roll back our state so that things
15925 are just as they were before we began the tentative parse. */
15928 cp_lexer_rollback_tokens (parser->lexer);
15929 pop_deferring_access_checks ();
15931 /* Add the context to the front of the free list. */
15932 context->next = cp_parser_context_free_list;
15933 cp_parser_context_free_list = context;
15935 return !error_occurred;
15938 /* Returns true if we are parsing tentatively -- but have decided that
15939 we will stick with this tentative parse, even if errors occur. */
15942 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15944 return (cp_parser_parsing_tentatively (parser)
15945 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15948 /* Returns nonzero iff an error has occurred during the most recent
15949 tentative parse. */
15952 cp_parser_error_occurred (cp_parser* parser)
15954 return (cp_parser_parsing_tentatively (parser)
15955 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15958 /* Returns nonzero if GNU extensions are allowed. */
15961 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15963 return parser->allow_gnu_extensions_p;
15969 static GTY (()) cp_parser *the_parser;
15971 /* External interface. */
15973 /* Parse one entire translation unit. */
15976 c_parse_file (void)
15978 bool error_occurred;
15979 static bool already_called = false;
15981 if (already_called)
15983 sorry ("inter-module optimizations not implemented for C++");
15986 already_called = true;
15988 the_parser = cp_parser_new ();
15989 push_deferring_access_checks (flag_access_control
15990 ? dk_no_deferred : dk_no_check);
15991 error_occurred = cp_parser_translation_unit (the_parser);
15995 /* This variable must be provided by every front end. */
15999 #include "gt-cp-parser.h"