2 Copyright (C) 2000, 2001, 2002, 2003 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"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
75 /* The value associated with this token, if any. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block GTY (())
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
109 typedef struct cp_token_cache GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
116 cp_token_block *last;
121 static cp_token_cache *cp_token_cache_new
123 static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache *
129 cp_token_cache_new (void)
131 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 /* Add *TOKEN to *CACHE. */
137 cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token_block *b = cache->last;
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 b = ggc_alloc_cleared (sizeof (cp_token_block));
146 b->prev = cache->last;
149 cache->last->next = b;
153 cache->first = cache->last = b;
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
163 typedef struct cp_lexer GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
197 /* The STRING_CST tokens encountered while processing the current
199 varray_type string_tokens;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
205 /* True if we should output debugging information. */
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
214 static cp_lexer *cp_lexer_new_main
216 static cp_lexer *cp_lexer_new_from_tokens
217 (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
220 static cp_token *cp_lexer_next_token
221 (cp_lexer *, cp_token *);
222 static cp_token *cp_lexer_prev_token
223 (cp_lexer *, cp_token *);
224 static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer *, cp_token *, cp_token *);
226 static cp_token *cp_lexer_read_token
228 static void cp_lexer_maybe_grow_buffer
230 static void cp_lexer_get_preprocessor_token
231 (cp_lexer *, cp_token *);
232 static cp_token *cp_lexer_peek_token
234 static cp_token *cp_lexer_peek_nth_token
235 (cp_lexer *, size_t);
236 static inline bool cp_lexer_next_token_is
237 (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_not
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_keyword
241 (cp_lexer *, enum rid);
242 static cp_token *cp_lexer_consume_token
244 static void cp_lexer_purge_token
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
250 static void cp_lexer_commit_tokens
252 static void cp_lexer_rollback_tokens
254 static inline void cp_lexer_set_source_position_from_token
255 (cp_lexer *, const cp_token *);
256 static void cp_lexer_print_token
257 (FILE *, cp_token *);
258 static inline bool cp_lexer_debugging_p
260 static void cp_lexer_start_debugging
261 (cp_lexer *) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
265 /* Manifest constants. */
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream;
295 /* Create a new main C++ lexer, the lexer that gets tokens from the
299 cp_lexer_new_main (void)
302 cp_token first_token;
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL, &first_token);
308 c_common_no_more_pch ();
310 /* Allocate the memory. */
311 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
313 /* Create the circular buffer. */
314 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
323 /* This lexer obtains more tokens by calling c_lex. */
324 lexer->main_lexer_p = true;
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
349 /* Allocate the memory. */
350 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
352 /* Create a new buffer, appropriately sized. */
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
356 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
357 lexer->buffer_end = lexer->buffer + num_tokens;
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
389 /* Returns nonzero if debugging information should be output. */
392 cp_lexer_debugging_p (cp_lexer *lexer)
394 return lexer->debugging_p;
397 /* Set the current source position from the information stored in
401 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
409 input_location = token->location;
412 /* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
415 static inline cp_token *
416 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419 if (token == lexer->buffer_end)
420 token = lexer->buffer;
424 /* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
427 static inline cp_token *
428 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
430 if (token == lexer->buffer)
431 token = lexer->buffer_end;
435 /* nonzero if we are presently saving tokens. */
438 cp_lexer_saving_tokens (const cp_lexer* lexer)
440 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450 if (token >= lexer->buffer_end)
451 token = lexer->buffer + (token - lexer->buffer_end);
455 /* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
459 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462 return finish - start;
464 return ((lexer->buffer_end - lexer->buffer)
468 /* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
472 cp_lexer_read_token (cp_lexer* lexer)
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500 && flag_const_strings)
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type = TREE_TYPE (token->value);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509 /* Reset the type of the token. */
510 TREE_TYPE (token->value) = type;
516 /* If the circular buffer is full, make it bigger. */
519 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ggc_realloc (lexer->buffer,
538 2 * buffer_length * sizeof (cp_token));
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
545 num_tokens_to_copy = (lexer->first_token - old_buffer);
546 memcpy (new_buffer + buffer_length,
548 num_tokens_to_copy * sizeof (cp_token));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
552 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
554 /* Now recompute all of the buffer pointers. */
556 = new_buffer + (lexer->first_token - old_buffer);
557 if (lexer->next_token != NULL)
559 ptrdiff_t next_token_delta;
561 if (lexer->next_token > lexer->first_token)
562 next_token_delta = lexer->next_token - lexer->first_token;
565 buffer_length - (lexer->first_token - lexer->next_token);
566 lexer->next_token = new_first_token + next_token_delta;
568 lexer->last_token = new_first_token + buffer_length;
569 lexer->buffer = new_buffer;
570 lexer->buffer_end = new_buffer + buffer_length * 2;
571 lexer->first_token = new_first_token;
575 /* Store the next token from the preprocessor in *TOKEN. */
578 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
584 if (lexer != NULL && !lexer->main_lexer_p)
586 token->type = CPP_EOF;
587 token->location.line = 0;
588 token->location.file = NULL;
589 token->value = NULL_TREE;
590 token->keyword = RID_MAX;
596 /* Keep going until we get a token we like. */
599 /* Get a new token from the preprocessor. */
600 token->type = c_lex (&token->value);
601 /* Issue messages about tokens we cannot process. */
607 error ("invalid token");
611 /* This is a good token, so we exit the loop. */
616 /* Now we've got our token. */
617 token->location = input_location;
619 /* Check to see if this token is a keyword. */
620 if (token->type == CPP_NAME
621 && C_IS_RESERVED_WORD (token->value))
623 /* Mark this token as a keyword. */
624 token->type = CPP_KEYWORD;
625 /* Record which keyword. */
626 token->keyword = C_RID_CODE (token->value);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token->value = ridpointers[token->keyword];
634 token->keyword = RID_MAX;
637 /* Return a pointer to the next token in the token stream, but do not
641 cp_lexer_peek_token (cp_lexer* lexer)
645 /* If there are no tokens, read one now. */
646 if (!lexer->next_token)
647 cp_lexer_read_token (lexer);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654 fprintf (cp_lexer_debug_stream, "\n");
657 token = lexer->next_token;
658 cp_lexer_set_source_position_from_token (lexer, token);
662 /* Return true if the next token has the indicated TYPE. */
665 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
669 /* Peek at the next token. */
670 token = cp_lexer_peek_token (lexer);
671 /* Check to see if it has the indicated TYPE. */
672 return token->type == type;
675 /* Return true if the next token does not have the indicated TYPE. */
678 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
680 return !cp_lexer_next_token_is (lexer, type);
683 /* Return true if the next token is the indicated KEYWORD. */
686 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
690 /* Peek at the next token. */
691 token = cp_lexer_peek_token (lexer);
692 /* Check to see if it is the indicated keyword. */
693 return token->keyword == keyword;
696 /* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
700 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n > 0, 20000224);
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token = lexer->next_token;
709 /* If there are no tokens in the buffer, get one now. */
712 cp_lexer_read_token (lexer);
713 token = lexer->next_token;
716 /* Now, read tokens until we have enough. */
719 /* Advance to the next token. */
720 token = cp_lexer_next_token (lexer, token);
721 /* If that's all the tokens we have, read a new one. */
722 if (token == lexer->last_token)
723 token = cp_lexer_read_token (lexer);
729 /* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
735 cp_lexer_consume_token (cp_lexer* lexer)
739 /* If there are no tokens, read one now. */
740 if (!lexer->next_token)
741 cp_lexer_read_token (lexer);
743 /* Remember the token we'll be returning. */
744 token = lexer->next_token;
746 /* Increment NEXT_TOKEN. */
747 lexer->next_token = cp_lexer_next_token (lexer,
749 /* Check to see if we're all out of tokens. */
750 if (lexer->next_token == lexer->last_token)
751 lexer->next_token = NULL;
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer))
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer->next_token)
758 lexer->first_token = NULL;
760 lexer->first_token = lexer->next_token;
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer))
766 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream, token);
768 fprintf (cp_lexer_debug_stream, "\n");
774 /* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
779 cp_lexer_purge_token (cp_lexer *lexer)
782 cp_token *next_token;
784 token = lexer->next_token;
787 next_token = cp_lexer_next_token (lexer, token);
788 if (next_token == lexer->last_token)
790 *token = *next_token;
794 lexer->last_token = token;
795 /* The token purged may have been the only token remaining; if so,
797 if (lexer->next_token == token)
798 lexer->next_token = NULL;
801 /* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
806 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
812 if (lexer->next_token)
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1 = cp_lexer_next_token (lexer, token);
817 t2 = peek = cp_lexer_peek_token (lexer);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2 != lexer->last_token)
822 t1 = cp_lexer_next_token (lexer, t1);
823 t2 = cp_lexer_next_token (lexer, t2);
825 /* Now, the next available token is right after TOKEN. */
826 lexer->next_token = cp_lexer_next_token (lexer, token);
827 /* And the last token is wherever we ended up. */
828 lexer->last_token = t1;
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer->last_token = cp_lexer_next_token (lexer, token);
838 /* Begin saving tokens. All tokens consumed after this point will be
842 cp_lexer_save_tokens (cp_lexer* lexer)
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer))
846 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer->next_token)
851 cp_lexer_read_token (lexer);
853 VARRAY_PUSH_INT (lexer->saved_tokens,
854 cp_lexer_token_difference (lexer,
859 /* Commit to the portion of the token stream most recently saved. */
862 cp_lexer_commit_tokens (cp_lexer* lexer)
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer))
866 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
868 VARRAY_POP (lexer->saved_tokens);
871 /* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
875 cp_lexer_rollback_tokens (cp_lexer* lexer)
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer))
881 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
883 /* Find the token that was the NEXT_TOKEN when we started saving
885 delta = VARRAY_TOP_INT(lexer->saved_tokens);
886 /* Make it the next token again now. */
887 lexer->next_token = cp_lexer_advance_token (lexer,
890 /* It might be the case that there were no tokens when we started
891 saving tokens, but that there are some tokens now. */
892 if (!lexer->next_token && lexer->first_token)
893 lexer->next_token = lexer->first_token;
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer->saved_tokens);
899 /* Print a representation of the TOKEN on the STREAM. */
902 cp_lexer_print_token (FILE * stream, cp_token* token)
904 const char *token_type = NULL;
906 /* Figure out what kind of token this is. */
914 token_type = "COMMA";
918 token_type = "OPEN_PAREN";
921 case CPP_CLOSE_PAREN:
922 token_type = "CLOSE_PAREN";
926 token_type = "OPEN_BRACE";
929 case CPP_CLOSE_BRACE:
930 token_type = "CLOSE_BRACE";
934 token_type = "SEMICOLON";
946 token_type = "keyword";
949 /* This is not a token that we know how to handle yet. */
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
957 fprintf (stream, "%s", token_type);
959 fprintf (stream, "%d", token->type);
960 /* And, for an identifier, print the identifier name. */
961 if (token->type == CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token->type == CPP_KEYWORD
965 && TREE_CODE (token->value) == IDENTIFIER_NODE))
966 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 /* Start emitting debugging information. */
972 cp_lexer_start_debugging (cp_lexer* lexer)
974 ++lexer->debugging_p;
977 /* Stop emitting debugging information. */
980 cp_lexer_stop_debugging (cp_lexer* lexer)
982 --lexer->debugging_p;
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1048 /* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1051 typedef enum cp_parser_flags
1054 CP_PARSER_FLAGS_NONE = 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 /* The different kinds of declarators we want to parse. */
1064 typedef enum cp_parser_declarator_kind
1066 /* We want an abstract declartor. */
1067 CP_PARSER_DECLARATOR_ABSTRACT,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED,
1070 /* We don't mind, but the name must be an unqualified-id. */
1071 CP_PARSER_DECLARATOR_EITHER
1072 } cp_parser_declarator_kind;
1074 /* A mapping from a token type to a corresponding tree node type. */
1076 typedef struct cp_parser_token_tree_map_node
1078 /* The token type. */
1079 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1080 /* The corresponding tree code. */
1081 ENUM_BITFIELD (tree_code) tree_type : 8;
1082 } cp_parser_token_tree_map_node;
1084 /* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1087 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1089 /* The status of a tentative parse. */
1091 typedef enum cp_parser_status_kind
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1102 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116 } cp_parser_context;
1120 /* Constructors and destructors. */
1122 static cp_parser_context *cp_parser_context_new
1123 (cp_parser_context *);
1125 /* Class variables. */
1127 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1129 /* Constructors and destructors. */
1131 /* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1134 static cp_parser_context *
1135 cp_parser_context_new (cp_parser_context* next)
1137 cp_parser_context *context;
1139 /* Allocate the storage. */
1140 if (cp_parser_context_free_list != NULL)
1142 /* Pull the first entry from the free list. */
1143 context = cp_parser_context_free_list;
1144 cp_parser_context_free_list = context->next;
1145 memset (context, 0, sizeof (*context));
1148 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1149 /* No errors have occurred yet in this context. */
1150 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context->object_type = next->object_type;
1158 /* Thread the stack. */
1159 context->next = next;
1165 /* The cp_parser structure represents the C++ parser. */
1167 typedef struct cp_parser GTY(())
1169 /* The lexer from which we are obtaining tokens. */
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1192 tree qualifying_scope;
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context *context;
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p;
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p;
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
1216 them permissible. */
1217 bool default_arg_ok_p;
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
1221 bool constant_expression_p;
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
1227 bool allow_non_constant_expression_p;
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
1231 bool non_constant_expression_p;
1233 /* TRUE if local variable names and `this' are forbidden in the
1235 bool local_variables_forbidden_p;
1237 /* TRUE if the declaration we are parsing is part of a
1238 linkage-specification of the form `extern string-literal
1240 bool in_unbraced_linkage_specification_p;
1242 /* TRUE if we are presently parsing a declarator, after the
1243 direct-declarator. */
1244 bool in_declarator_p;
1246 /* TRUE if we are presently parsing the body of an
1247 iteration-statement. */
1248 bool in_iteration_statement_p;
1250 /* TRUE if we are presently parsing the body of a switch
1252 bool in_switch_statement_p;
1254 /* If non-NULL, then we are parsing a construct where new type
1255 definitions are not permitted. The string stored here will be
1256 issued as an error message if a type is defined. */
1257 const char *type_definition_forbidden_message;
1259 /* A list of lists. The outer list is a stack, used for member
1260 functions of local classes. At each level there are two sub-list,
1261 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1262 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1263 TREE_VALUE's. The functions are chained in reverse declaration
1266 The TREE_PURPOSE sublist contains those functions with default
1267 arguments that need post processing, and the TREE_VALUE sublist
1268 contains those functions with definitions that need post
1271 These lists can only be processed once the outermost class being
1272 defined is complete. */
1273 tree unparsed_functions_queues;
1275 /* The number of classes whose definitions are currently in
1277 unsigned num_classes_being_defined;
1279 /* The number of template parameter lists that apply directly to the
1280 current declaration. */
1281 unsigned num_template_parameter_lists;
1284 /* The type of a function that parses some kind of expression. */
1285 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1289 /* Constructors and destructors. */
1291 static cp_parser *cp_parser_new
1294 /* Routines to parse various constructs.
1296 Those that return `tree' will return the error_mark_node (rather
1297 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1298 Sometimes, they will return an ordinary node if error-recovery was
1299 attempted, even though a parse error occurred. So, to check
1300 whether or not a parse error occurred, you should always use
1301 cp_parser_error_occurred. If the construct is optional (indicated
1302 either by an `_opt' in the name of the function that does the
1303 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1304 the construct is not present. */
1306 /* Lexical conventions [gram.lex] */
1308 static tree cp_parser_identifier
1311 /* Basic concepts [gram.basic] */
1313 static bool cp_parser_translation_unit
1316 /* Expressions [gram.expr] */
1318 static tree cp_parser_primary_expression
1319 (cp_parser *, cp_id_kind *, tree *);
1320 static tree cp_parser_id_expression
1321 (cp_parser *, bool, bool, bool *, bool);
1322 static tree cp_parser_unqualified_id
1323 (cp_parser *, bool, bool, bool);
1324 static tree cp_parser_nested_name_specifier_opt
1325 (cp_parser *, bool, bool, bool, bool);
1326 static tree cp_parser_nested_name_specifier
1327 (cp_parser *, bool, bool, bool, bool);
1328 static tree cp_parser_class_or_namespace_name
1329 (cp_parser *, bool, bool, bool, bool, bool);
1330 static tree cp_parser_postfix_expression
1331 (cp_parser *, bool);
1332 static tree cp_parser_parenthesized_expression_list
1333 (cp_parser *, bool, bool *);
1334 static void cp_parser_pseudo_destructor_name
1335 (cp_parser *, tree *, tree *);
1336 static tree cp_parser_unary_expression
1337 (cp_parser *, bool);
1338 static enum tree_code cp_parser_unary_operator
1340 static tree cp_parser_new_expression
1342 static tree cp_parser_new_placement
1344 static tree cp_parser_new_type_id
1346 static tree cp_parser_new_declarator_opt
1348 static tree cp_parser_direct_new_declarator
1350 static tree cp_parser_new_initializer
1352 static tree cp_parser_delete_expression
1354 static tree cp_parser_cast_expression
1355 (cp_parser *, bool);
1356 static tree cp_parser_pm_expression
1358 static tree cp_parser_multiplicative_expression
1360 static tree cp_parser_additive_expression
1362 static tree cp_parser_shift_expression
1364 static tree cp_parser_relational_expression
1366 static tree cp_parser_equality_expression
1368 static tree cp_parser_and_expression
1370 static tree cp_parser_exclusive_or_expression
1372 static tree cp_parser_inclusive_or_expression
1374 static tree cp_parser_logical_and_expression
1376 static tree cp_parser_logical_or_expression
1378 static tree cp_parser_question_colon_clause
1379 (cp_parser *, tree);
1380 static tree cp_parser_assignment_expression
1382 static enum tree_code cp_parser_assignment_operator_opt
1384 static tree cp_parser_expression
1386 static tree cp_parser_constant_expression
1387 (cp_parser *, bool, bool *);
1389 /* Statements [gram.stmt.stmt] */
1391 static void cp_parser_statement
1392 (cp_parser *, bool);
1393 static tree cp_parser_labeled_statement
1394 (cp_parser *, bool);
1395 static tree cp_parser_expression_statement
1396 (cp_parser *, bool);
1397 static tree cp_parser_compound_statement
1398 (cp_parser *, bool);
1399 static void cp_parser_statement_seq_opt
1400 (cp_parser *, bool);
1401 static tree cp_parser_selection_statement
1403 static tree cp_parser_condition
1405 static tree cp_parser_iteration_statement
1407 static void cp_parser_for_init_statement
1409 static tree cp_parser_jump_statement
1411 static void cp_parser_declaration_statement
1414 static tree cp_parser_implicitly_scoped_statement
1416 static void cp_parser_already_scoped_statement
1419 /* Declarations [gram.dcl.dcl] */
1421 static void cp_parser_declaration_seq_opt
1423 static void cp_parser_declaration
1425 static void cp_parser_block_declaration
1426 (cp_parser *, bool);
1427 static void cp_parser_simple_declaration
1428 (cp_parser *, bool);
1429 static tree cp_parser_decl_specifier_seq
1430 (cp_parser *, cp_parser_flags, tree *, int *);
1431 static tree cp_parser_storage_class_specifier_opt
1433 static tree cp_parser_function_specifier_opt
1435 static tree cp_parser_type_specifier
1436 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1437 static tree cp_parser_simple_type_specifier
1438 (cp_parser *, cp_parser_flags, bool);
1439 static tree cp_parser_type_name
1441 static tree cp_parser_elaborated_type_specifier
1442 (cp_parser *, bool, bool);
1443 static tree cp_parser_enum_specifier
1445 static void cp_parser_enumerator_list
1446 (cp_parser *, tree);
1447 static void cp_parser_enumerator_definition
1448 (cp_parser *, tree);
1449 static tree cp_parser_namespace_name
1451 static void cp_parser_namespace_definition
1453 static void cp_parser_namespace_body
1455 static tree cp_parser_qualified_namespace_specifier
1457 static void cp_parser_namespace_alias_definition
1459 static void cp_parser_using_declaration
1461 static void cp_parser_using_directive
1463 static void cp_parser_asm_definition
1465 static void cp_parser_linkage_specification
1468 /* Declarators [gram.dcl.decl] */
1470 static tree cp_parser_init_declarator
1471 (cp_parser *, tree, tree, bool, bool, int, bool *);
1472 static tree cp_parser_declarator
1473 (cp_parser *, cp_parser_declarator_kind, int *);
1474 static tree cp_parser_direct_declarator
1475 (cp_parser *, cp_parser_declarator_kind, int *);
1476 static enum tree_code cp_parser_ptr_operator
1477 (cp_parser *, tree *, tree *);
1478 static tree cp_parser_cv_qualifier_seq_opt
1480 static tree cp_parser_cv_qualifier_opt
1482 static tree cp_parser_declarator_id
1484 static tree cp_parser_type_id
1486 static tree cp_parser_type_specifier_seq
1488 static tree cp_parser_parameter_declaration_clause
1490 static tree cp_parser_parameter_declaration_list
1492 static tree cp_parser_parameter_declaration
1493 (cp_parser *, bool);
1494 static tree cp_parser_function_definition
1495 (cp_parser *, bool *);
1496 static void cp_parser_function_body
1498 static tree cp_parser_initializer
1499 (cp_parser *, bool *, bool *);
1500 static tree cp_parser_initializer_clause
1501 (cp_parser *, bool *);
1502 static tree cp_parser_initializer_list
1503 (cp_parser *, bool *);
1505 static bool cp_parser_ctor_initializer_opt_and_function_body
1508 /* Classes [gram.class] */
1510 static tree cp_parser_class_name
1511 (cp_parser *, bool, bool, bool, bool, bool, bool);
1512 static tree cp_parser_class_specifier
1514 static tree cp_parser_class_head
1515 (cp_parser *, bool *);
1516 static enum tag_types cp_parser_class_key
1518 static void cp_parser_member_specification_opt
1520 static void cp_parser_member_declaration
1522 static tree cp_parser_pure_specifier
1524 static tree cp_parser_constant_initializer
1527 /* Derived classes [gram.class.derived] */
1529 static tree cp_parser_base_clause
1531 static tree cp_parser_base_specifier
1534 /* Special member functions [gram.special] */
1536 static tree cp_parser_conversion_function_id
1538 static tree cp_parser_conversion_type_id
1540 static tree cp_parser_conversion_declarator_opt
1542 static bool cp_parser_ctor_initializer_opt
1544 static void cp_parser_mem_initializer_list
1546 static tree cp_parser_mem_initializer
1548 static tree cp_parser_mem_initializer_id
1551 /* Overloading [gram.over] */
1553 static tree cp_parser_operator_function_id
1555 static tree cp_parser_operator
1558 /* Templates [gram.temp] */
1560 static void cp_parser_template_declaration
1561 (cp_parser *, bool);
1562 static tree cp_parser_template_parameter_list
1564 static tree cp_parser_template_parameter
1566 static tree cp_parser_type_parameter
1568 static tree cp_parser_template_id
1569 (cp_parser *, bool, bool, bool);
1570 static tree cp_parser_template_name
1571 (cp_parser *, bool, bool, bool, bool *);
1572 static tree cp_parser_template_argument_list
1574 static tree cp_parser_template_argument
1576 static void cp_parser_explicit_instantiation
1578 static void cp_parser_explicit_specialization
1581 /* Exception handling [gram.exception] */
1583 static tree cp_parser_try_block
1585 static bool cp_parser_function_try_block
1587 static void cp_parser_handler_seq
1589 static void cp_parser_handler
1591 static tree cp_parser_exception_declaration
1593 static tree cp_parser_throw_expression
1595 static tree cp_parser_exception_specification_opt
1597 static tree cp_parser_type_id_list
1600 /* GNU Extensions */
1602 static tree cp_parser_asm_specification_opt
1604 static tree cp_parser_asm_operand_list
1606 static tree cp_parser_asm_clobber_list
1608 static tree cp_parser_attributes_opt
1610 static tree cp_parser_attribute_list
1612 static bool cp_parser_extension_opt
1613 (cp_parser *, int *);
1614 static void cp_parser_label_declaration
1617 /* Utility Routines */
1619 static tree cp_parser_lookup_name
1620 (cp_parser *, tree, bool, bool, bool);
1621 static tree cp_parser_lookup_name_simple
1622 (cp_parser *, tree);
1623 static tree cp_parser_maybe_treat_template_as_class
1625 static bool cp_parser_check_declarator_template_parameters
1626 (cp_parser *, tree);
1627 static bool cp_parser_check_template_parameters
1628 (cp_parser *, unsigned);
1629 static tree cp_parser_simple_cast_expression
1631 static tree cp_parser_binary_expression
1632 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1633 static tree cp_parser_global_scope_opt
1634 (cp_parser *, bool);
1635 static bool cp_parser_constructor_declarator_p
1636 (cp_parser *, bool);
1637 static tree cp_parser_function_definition_from_specifiers_and_declarator
1638 (cp_parser *, tree, tree, tree);
1639 static tree cp_parser_function_definition_after_declarator
1640 (cp_parser *, bool);
1641 static void cp_parser_template_declaration_after_export
1642 (cp_parser *, bool);
1643 static tree cp_parser_single_declaration
1644 (cp_parser *, bool, bool *);
1645 static tree cp_parser_functional_cast
1646 (cp_parser *, tree);
1647 static tree cp_parser_enclosed_template_argument_list
1649 static void cp_parser_save_default_args
1650 (cp_parser *, tree);
1651 static void cp_parser_late_parsing_for_member
1652 (cp_parser *, tree);
1653 static void cp_parser_late_parsing_default_args
1654 (cp_parser *, tree);
1655 static tree cp_parser_sizeof_operand
1656 (cp_parser *, enum rid);
1657 static bool cp_parser_declares_only_class_p
1659 static tree cp_parser_fold_non_dependent_expr
1661 static bool cp_parser_friend_p
1663 static cp_token *cp_parser_require
1664 (cp_parser *, enum cpp_ttype, const char *);
1665 static cp_token *cp_parser_require_keyword
1666 (cp_parser *, enum rid, const char *);
1667 static bool cp_parser_token_starts_function_definition_p
1669 static bool cp_parser_next_token_starts_class_definition_p
1671 static bool cp_parser_next_token_ends_template_argument_p
1673 static enum tag_types cp_parser_token_is_class_key
1675 static void cp_parser_check_class_key
1676 (enum tag_types, tree type);
1677 static void cp_parser_check_access_in_redeclaration
1679 static bool cp_parser_optional_template_keyword
1681 static void cp_parser_pre_parsed_nested_name_specifier
1683 static void cp_parser_cache_group
1684 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1685 static void cp_parser_parse_tentatively
1687 static void cp_parser_commit_to_tentative_parse
1689 static void cp_parser_abort_tentative_parse
1691 static bool cp_parser_parse_definitely
1693 static inline bool cp_parser_parsing_tentatively
1695 static bool cp_parser_committed_to_tentative_parse
1697 static void cp_parser_error
1698 (cp_parser *, const char *);
1699 static bool cp_parser_simulate_error
1701 static void cp_parser_check_type_definition
1703 static void cp_parser_check_for_definition_in_return_type
1705 static void cp_parser_check_for_invalid_template_id
1706 (cp_parser *, tree);
1707 static tree cp_parser_non_constant_expression
1709 static bool cp_parser_diagnose_invalid_type_name
1711 static int cp_parser_skip_to_closing_parenthesis
1712 (cp_parser *, bool, bool, bool);
1713 static void cp_parser_skip_to_end_of_statement
1715 static void cp_parser_consume_semicolon_at_end_of_statement
1717 static void cp_parser_skip_to_end_of_block_or_statement
1719 static void cp_parser_skip_to_closing_brace
1721 static void cp_parser_skip_until_found
1722 (cp_parser *, enum cpp_ttype, const char *);
1723 static bool cp_parser_error_occurred
1725 static bool cp_parser_allow_gnu_extensions_p
1727 static bool cp_parser_is_string_literal
1729 static bool cp_parser_is_keyword
1730 (cp_token *, enum rid);
1732 /* Returns nonzero if we are parsing tentatively. */
1735 cp_parser_parsing_tentatively (cp_parser* parser)
1737 return parser->context->next != NULL;
1740 /* Returns nonzero if TOKEN is a string literal. */
1743 cp_parser_is_string_literal (cp_token* token)
1745 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1748 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1751 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1753 return token->keyword == keyword;
1756 /* Issue the indicated error MESSAGE. */
1759 cp_parser_error (cp_parser* parser, const char* message)
1761 /* Output the MESSAGE -- unless we're parsing tentatively. */
1762 if (!cp_parser_simulate_error (parser))
1766 /* If we are parsing tentatively, remember that an error has occurred
1767 during this tentative parse. Returns true if the error was
1768 simulated; false if a messgae should be issued by the caller. */
1771 cp_parser_simulate_error (cp_parser* parser)
1773 if (cp_parser_parsing_tentatively (parser)
1774 && !cp_parser_committed_to_tentative_parse (parser))
1776 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1782 /* This function is called when a type is defined. If type
1783 definitions are forbidden at this point, an error message is
1787 cp_parser_check_type_definition (cp_parser* parser)
1789 /* If types are forbidden here, issue a message. */
1790 if (parser->type_definition_forbidden_message)
1791 /* Use `%s' to print the string in case there are any escape
1792 characters in the message. */
1793 error ("%s", parser->type_definition_forbidden_message);
1796 /* This function is called when a declaration is parsed. If
1797 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1798 indicates that a type was defined in the decl-specifiers for DECL,
1799 then an error is issued. */
1802 cp_parser_check_for_definition_in_return_type (tree declarator,
1803 int declares_class_or_enum)
1805 /* [dcl.fct] forbids type definitions in return types.
1806 Unfortunately, it's not easy to know whether or not we are
1807 processing a return type until after the fact. */
1809 && (TREE_CODE (declarator) == INDIRECT_REF
1810 || TREE_CODE (declarator) == ADDR_EXPR))
1811 declarator = TREE_OPERAND (declarator, 0);
1813 && TREE_CODE (declarator) == CALL_EXPR
1814 && declares_class_or_enum & 2)
1815 error ("new types may not be defined in a return type");
1818 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1819 "<" in any valid C++ program. If the next token is indeed "<",
1820 issue a message warning the user about what appears to be an
1821 invalid attempt to form a template-id. */
1824 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1830 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1833 error ("`%T' is not a template", type);
1834 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1835 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1837 error ("invalid template-id");
1838 /* Remember the location of the invalid "<". */
1839 if (cp_parser_parsing_tentatively (parser)
1840 && !cp_parser_committed_to_tentative_parse (parser))
1842 token = cp_lexer_peek_token (parser->lexer);
1843 token = cp_lexer_prev_token (parser->lexer, token);
1844 start = cp_lexer_token_difference (parser->lexer,
1845 parser->lexer->first_token,
1850 /* Consume the "<". */
1851 cp_lexer_consume_token (parser->lexer);
1852 /* Parse the template arguments. */
1853 cp_parser_enclosed_template_argument_list (parser);
1854 /* Permanently remove the invalid template arugments so that
1855 this error message is not issued again. */
1858 token = cp_lexer_advance_token (parser->lexer,
1859 parser->lexer->first_token,
1861 cp_lexer_purge_tokens_after (parser->lexer, token);
1866 /* Issue an error message about the fact that THING appeared in a
1867 constant-expression. Returns ERROR_MARK_NODE. */
1870 cp_parser_non_constant_expression (const char *thing)
1872 error ("%s cannot appear in a constant-expression", thing);
1873 return error_mark_node;
1876 /* Check for a common situation where a type-name should be present,
1877 but is not, and issue a sensible error message. Returns true if an
1878 invalid type-name was detected. */
1881 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1883 /* If the next two tokens are both identifiers, the code is
1884 erroneous. The usual cause of this situation is code like:
1888 where "T" should name a type -- but does not. */
1889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1890 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1894 /* If parsing tentatively, we should commit; we really are
1895 looking at a declaration. */
1896 /* Consume the first identifier. */
1897 name = cp_lexer_consume_token (parser->lexer)->value;
1898 /* Issue an error message. */
1899 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1900 /* If we're in a template class, it's possible that the user was
1901 referring to a type from a base class. For example:
1903 template <typename T> struct A { typedef T X; };
1904 template <typename T> struct B : public A<T> { X x; };
1906 The user should have said "typename A<T>::X". */
1907 if (processing_template_decl && current_class_type)
1911 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1915 tree base_type = BINFO_TYPE (b);
1916 if (CLASS_TYPE_P (base_type)
1917 && dependent_type_p (base_type))
1920 /* Go from a particular instantiation of the
1921 template (which will have an empty TYPE_FIELDs),
1922 to the main version. */
1923 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1924 for (field = TYPE_FIELDS (base_type);
1926 field = TREE_CHAIN (field))
1927 if (TREE_CODE (field) == TYPE_DECL
1928 && DECL_NAME (field) == name)
1930 error ("(perhaps `typename %T::%s' was intended)",
1931 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1939 /* Skip to the end of the declaration; there's no point in
1940 trying to process it. */
1941 cp_parser_skip_to_end_of_statement (parser);
1949 /* Consume tokens up to, and including, the next non-nested closing `)'.
1950 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1951 are doing error recovery. Returns -1 if OR_COMMA is true and we
1952 found an unnested comma. */
1955 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1960 unsigned paren_depth = 0;
1961 unsigned brace_depth = 0;
1963 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1964 && !cp_parser_committed_to_tentative_parse (parser))
1971 /* If we've run out of tokens, then there is no closing `)'. */
1972 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1975 token = cp_lexer_peek_token (parser->lexer);
1977 /* This matches the processing in skip_to_end_of_statement */
1978 if (token->type == CPP_SEMICOLON && !brace_depth)
1980 if (token->type == CPP_OPEN_BRACE)
1982 if (token->type == CPP_CLOSE_BRACE)
1987 if (recovering && or_comma && token->type == CPP_COMMA
1988 && !brace_depth && !paren_depth)
1993 /* If it is an `(', we have entered another level of nesting. */
1994 if (token->type == CPP_OPEN_PAREN)
1996 /* If it is a `)', then we might be done. */
1997 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2000 cp_lexer_consume_token (parser->lexer);
2005 /* Consume the token. */
2006 cp_lexer_consume_token (parser->lexer);
2010 /* Consume tokens until we reach the end of the current statement.
2011 Normally, that will be just before consuming a `;'. However, if a
2012 non-nested `}' comes first, then we stop before consuming that. */
2015 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2017 unsigned nesting_depth = 0;
2023 /* Peek at the next token. */
2024 token = cp_lexer_peek_token (parser->lexer);
2025 /* If we've run out of tokens, stop. */
2026 if (token->type == CPP_EOF)
2028 /* If the next token is a `;', we have reached the end of the
2030 if (token->type == CPP_SEMICOLON && !nesting_depth)
2032 /* If the next token is a non-nested `}', then we have reached
2033 the end of the current block. */
2034 if (token->type == CPP_CLOSE_BRACE)
2036 /* If this is a non-nested `}', stop before consuming it.
2037 That way, when confronted with something like:
2041 we stop before consuming the closing `}', even though we
2042 have not yet reached a `;'. */
2043 if (nesting_depth == 0)
2045 /* If it is the closing `}' for a block that we have
2046 scanned, stop -- but only after consuming the token.
2052 we will stop after the body of the erroneously declared
2053 function, but before consuming the following `typedef'
2055 if (--nesting_depth == 0)
2057 cp_lexer_consume_token (parser->lexer);
2061 /* If it the next token is a `{', then we are entering a new
2062 block. Consume the entire block. */
2063 else if (token->type == CPP_OPEN_BRACE)
2065 /* Consume the token. */
2066 cp_lexer_consume_token (parser->lexer);
2070 /* This function is called at the end of a statement or declaration.
2071 If the next token is a semicolon, it is consumed; otherwise, error
2072 recovery is attempted. */
2075 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2077 /* Look for the trailing `;'. */
2078 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2080 /* If there is additional (erroneous) input, skip to the end of
2082 cp_parser_skip_to_end_of_statement (parser);
2083 /* If the next token is now a `;', consume it. */
2084 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2085 cp_lexer_consume_token (parser->lexer);
2089 /* Skip tokens until we have consumed an entire block, or until we
2090 have consumed a non-nested `;'. */
2093 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2095 unsigned nesting_depth = 0;
2101 /* Peek at the next token. */
2102 token = cp_lexer_peek_token (parser->lexer);
2103 /* If we've run out of tokens, stop. */
2104 if (token->type == CPP_EOF)
2106 /* If the next token is a `;', we have reached the end of the
2108 if (token->type == CPP_SEMICOLON && !nesting_depth)
2110 /* Consume the `;'. */
2111 cp_lexer_consume_token (parser->lexer);
2114 /* Consume the token. */
2115 token = cp_lexer_consume_token (parser->lexer);
2116 /* If the next token is a non-nested `}', then we have reached
2117 the end of the current block. */
2118 if (token->type == CPP_CLOSE_BRACE
2119 && (nesting_depth == 0 || --nesting_depth == 0))
2121 /* If it the next token is a `{', then we are entering a new
2122 block. Consume the entire block. */
2123 if (token->type == CPP_OPEN_BRACE)
2128 /* Skip tokens until a non-nested closing curly brace is the next
2132 cp_parser_skip_to_closing_brace (cp_parser *parser)
2134 unsigned nesting_depth = 0;
2140 /* Peek at the next token. */
2141 token = cp_lexer_peek_token (parser->lexer);
2142 /* If we've run out of tokens, stop. */
2143 if (token->type == CPP_EOF)
2145 /* If the next token is a non-nested `}', then we have reached
2146 the end of the current block. */
2147 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2149 /* If it the next token is a `{', then we are entering a new
2150 block. Consume the entire block. */
2151 else if (token->type == CPP_OPEN_BRACE)
2153 /* Consume the token. */
2154 cp_lexer_consume_token (parser->lexer);
2158 /* Create a new C++ parser. */
2161 cp_parser_new (void)
2166 /* cp_lexer_new_main is called before calling ggc_alloc because
2167 cp_lexer_new_main might load a PCH file. */
2168 lexer = cp_lexer_new_main ();
2170 parser = ggc_alloc_cleared (sizeof (cp_parser));
2171 parser->lexer = lexer;
2172 parser->context = cp_parser_context_new (NULL);
2174 /* For now, we always accept GNU extensions. */
2175 parser->allow_gnu_extensions_p = 1;
2177 /* The `>' token is a greater-than operator, not the end of a
2179 parser->greater_than_is_operator_p = true;
2181 parser->default_arg_ok_p = true;
2183 /* We are not parsing a constant-expression. */
2184 parser->constant_expression_p = false;
2185 parser->allow_non_constant_expression_p = false;
2186 parser->non_constant_expression_p = false;
2188 /* Local variable names are not forbidden. */
2189 parser->local_variables_forbidden_p = false;
2191 /* We are not processing an `extern "C"' declaration. */
2192 parser->in_unbraced_linkage_specification_p = false;
2194 /* We are not processing a declarator. */
2195 parser->in_declarator_p = false;
2197 /* We are not in an iteration statement. */
2198 parser->in_iteration_statement_p = false;
2200 /* We are not in a switch statement. */
2201 parser->in_switch_statement_p = false;
2203 /* The unparsed function queue is empty. */
2204 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2206 /* There are no classes being defined. */
2207 parser->num_classes_being_defined = 0;
2209 /* No template parameters apply. */
2210 parser->num_template_parameter_lists = 0;
2215 /* Lexical conventions [gram.lex] */
2217 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2221 cp_parser_identifier (cp_parser* parser)
2225 /* Look for the identifier. */
2226 token = cp_parser_require (parser, CPP_NAME, "identifier");
2227 /* Return the value. */
2228 return token ? token->value : error_mark_node;
2231 /* Basic concepts [gram.basic] */
2233 /* Parse a translation-unit.
2236 declaration-seq [opt]
2238 Returns TRUE if all went well. */
2241 cp_parser_translation_unit (cp_parser* parser)
2245 cp_parser_declaration_seq_opt (parser);
2247 /* If there are no tokens left then all went well. */
2248 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2251 /* Otherwise, issue an error message. */
2252 cp_parser_error (parser, "expected declaration");
2256 /* Consume the EOF token. */
2257 cp_parser_require (parser, CPP_EOF, "end-of-file");
2260 finish_translation_unit ();
2262 /* All went well. */
2266 /* Expressions [gram.expr] */
2268 /* Parse a primary-expression.
2279 ( compound-statement )
2280 __builtin_va_arg ( assignment-expression , type-id )
2285 Returns a representation of the expression.
2287 *IDK indicates what kind of id-expression (if any) was present.
2289 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2290 used as the operand of a pointer-to-member. In that case,
2291 *QUALIFYING_CLASS gives the class that is used as the qualifying
2292 class in the pointer-to-member. */
2295 cp_parser_primary_expression (cp_parser *parser,
2297 tree *qualifying_class)
2301 /* Assume the primary expression is not an id-expression. */
2302 *idk = CP_ID_KIND_NONE;
2303 /* And that it cannot be used as pointer-to-member. */
2304 *qualifying_class = NULL_TREE;
2306 /* Peek at the next token. */
2307 token = cp_lexer_peek_token (parser->lexer);
2308 switch (token->type)
2321 token = cp_lexer_consume_token (parser->lexer);
2322 return token->value;
2324 case CPP_OPEN_PAREN:
2327 bool saved_greater_than_is_operator_p;
2329 /* Consume the `('. */
2330 cp_lexer_consume_token (parser->lexer);
2331 /* Within a parenthesized expression, a `>' token is always
2332 the greater-than operator. */
2333 saved_greater_than_is_operator_p
2334 = parser->greater_than_is_operator_p;
2335 parser->greater_than_is_operator_p = true;
2336 /* If we see `( { ' then we are looking at the beginning of
2337 a GNU statement-expression. */
2338 if (cp_parser_allow_gnu_extensions_p (parser)
2339 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2341 /* Statement-expressions are not allowed by the standard. */
2343 pedwarn ("ISO C++ forbids braced-groups within expressions");
2345 /* And they're not allowed outside of a function-body; you
2346 cannot, for example, write:
2348 int i = ({ int j = 3; j + 1; });
2350 at class or namespace scope. */
2351 if (!at_function_scope_p ())
2352 error ("statement-expressions are allowed only inside functions");
2353 /* Start the statement-expression. */
2354 expr = begin_stmt_expr ();
2355 /* Parse the compound-statement. */
2356 cp_parser_compound_statement (parser, true);
2358 expr = finish_stmt_expr (expr, false);
2362 /* Parse the parenthesized expression. */
2363 expr = cp_parser_expression (parser);
2364 /* Let the front end know that this expression was
2365 enclosed in parentheses. This matters in case, for
2366 example, the expression is of the form `A::B', since
2367 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2369 finish_parenthesized_expr (expr);
2371 /* The `>' token might be the end of a template-id or
2372 template-parameter-list now. */
2373 parser->greater_than_is_operator_p
2374 = saved_greater_than_is_operator_p;
2375 /* Consume the `)'. */
2376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2377 cp_parser_skip_to_end_of_statement (parser);
2383 switch (token->keyword)
2385 /* These two are the boolean literals. */
2387 cp_lexer_consume_token (parser->lexer);
2388 return boolean_true_node;
2390 cp_lexer_consume_token (parser->lexer);
2391 return boolean_false_node;
2393 /* The `__null' literal. */
2395 cp_lexer_consume_token (parser->lexer);
2398 /* Recognize the `this' keyword. */
2400 cp_lexer_consume_token (parser->lexer);
2401 if (parser->local_variables_forbidden_p)
2403 error ("`this' may not be used in this context");
2404 return error_mark_node;
2406 /* Pointers cannot appear in constant-expressions. */
2407 if (parser->constant_expression_p)
2409 if (!parser->allow_non_constant_expression_p)
2410 return cp_parser_non_constant_expression ("`this'");
2411 parser->non_constant_expression_p = true;
2413 return finish_this_expr ();
2415 /* The `operator' keyword can be the beginning of an
2420 case RID_FUNCTION_NAME:
2421 case RID_PRETTY_FUNCTION_NAME:
2422 case RID_C99_FUNCTION_NAME:
2423 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2424 __func__ are the names of variables -- but they are
2425 treated specially. Therefore, they are handled here,
2426 rather than relying on the generic id-expression logic
2427 below. Grammatically, these names are id-expressions.
2429 Consume the token. */
2430 token = cp_lexer_consume_token (parser->lexer);
2431 /* Look up the name. */
2432 return finish_fname (token->value);
2439 /* The `__builtin_va_arg' construct is used to handle
2440 `va_arg'. Consume the `__builtin_va_arg' token. */
2441 cp_lexer_consume_token (parser->lexer);
2442 /* Look for the opening `('. */
2443 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2444 /* Now, parse the assignment-expression. */
2445 expression = cp_parser_assignment_expression (parser);
2446 /* Look for the `,'. */
2447 cp_parser_require (parser, CPP_COMMA, "`,'");
2448 /* Parse the type-id. */
2449 type = cp_parser_type_id (parser);
2450 /* Look for the closing `)'. */
2451 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2452 /* Using `va_arg' in a constant-expression is not
2454 if (parser->constant_expression_p)
2456 if (!parser->allow_non_constant_expression_p)
2457 return cp_parser_non_constant_expression ("`va_arg'");
2458 parser->non_constant_expression_p = true;
2460 return build_x_va_arg (expression, type);
2464 cp_parser_error (parser, "expected primary-expression");
2465 return error_mark_node;
2468 /* An id-expression can start with either an identifier, a
2469 `::' as the beginning of a qualified-id, or the "operator"
2473 case CPP_TEMPLATE_ID:
2474 case CPP_NESTED_NAME_SPECIFIER:
2478 const char *error_msg;
2481 /* Parse the id-expression. */
2483 = cp_parser_id_expression (parser,
2484 /*template_keyword_p=*/false,
2485 /*check_dependency_p=*/true,
2486 /*template_p=*/NULL,
2487 /*declarator_p=*/false);
2488 if (id_expression == error_mark_node)
2489 return error_mark_node;
2490 /* If we have a template-id, then no further lookup is
2491 required. If the template-id was for a template-class, we
2492 will sometimes have a TYPE_DECL at this point. */
2493 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2494 || TREE_CODE (id_expression) == TYPE_DECL)
2495 decl = id_expression;
2496 /* Look up the name. */
2499 decl = cp_parser_lookup_name_simple (parser, id_expression);
2500 /* If name lookup gives us a SCOPE_REF, then the
2501 qualifying scope was dependent. Just propagate the
2503 if (TREE_CODE (decl) == SCOPE_REF)
2505 if (TYPE_P (TREE_OPERAND (decl, 0)))
2506 *qualifying_class = TREE_OPERAND (decl, 0);
2509 /* Check to see if DECL is a local variable in a context
2510 where that is forbidden. */
2511 if (parser->local_variables_forbidden_p
2512 && local_variable_p (decl))
2514 /* It might be that we only found DECL because we are
2515 trying to be generous with pre-ISO scoping rules.
2516 For example, consider:
2520 for (int i = 0; i < 10; ++i) {}
2521 extern void f(int j = i);
2524 Here, name look up will originally find the out
2525 of scope `i'. We need to issue a warning message,
2526 but then use the global `i'. */
2527 decl = check_for_out_of_scope_variable (decl);
2528 if (local_variable_p (decl))
2530 error ("local variable `%D' may not appear in this context",
2532 return error_mark_node;
2537 decl = finish_id_expression (id_expression, decl, parser->scope,
2538 idk, qualifying_class,
2539 parser->constant_expression_p,
2540 parser->allow_non_constant_expression_p,
2541 &parser->non_constant_expression_p,
2544 cp_parser_error (parser, error_msg);
2548 /* Anything else is an error. */
2550 cp_parser_error (parser, "expected primary-expression");
2551 return error_mark_node;
2555 /* Parse an id-expression.
2562 :: [opt] nested-name-specifier template [opt] unqualified-id
2564 :: operator-function-id
2567 Return a representation of the unqualified portion of the
2568 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2569 a `::' or nested-name-specifier.
2571 Often, if the id-expression was a qualified-id, the caller will
2572 want to make a SCOPE_REF to represent the qualified-id. This
2573 function does not do this in order to avoid wastefully creating
2574 SCOPE_REFs when they are not required.
2576 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2579 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2580 uninstantiated templates.
2582 If *TEMPLATE_P is non-NULL, it is set to true iff the
2583 `template' keyword is used to explicitly indicate that the entity
2584 named is a template.
2586 If DECLARATOR_P is true, the id-expression is appearing as part of
2587 a declarator, rather than as part of an expression. */
2590 cp_parser_id_expression (cp_parser *parser,
2591 bool template_keyword_p,
2592 bool check_dependency_p,
2596 bool global_scope_p;
2597 bool nested_name_specifier_p;
2599 /* Assume the `template' keyword was not used. */
2601 *template_p = false;
2603 /* Look for the optional `::' operator. */
2605 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2607 /* Look for the optional nested-name-specifier. */
2608 nested_name_specifier_p
2609 = (cp_parser_nested_name_specifier_opt (parser,
2610 /*typename_keyword_p=*/false,
2613 /*is_declarator=*/false)
2615 /* If there is a nested-name-specifier, then we are looking at
2616 the first qualified-id production. */
2617 if (nested_name_specifier_p)
2620 tree saved_object_scope;
2621 tree saved_qualifying_scope;
2622 tree unqualified_id;
2625 /* See if the next token is the `template' keyword. */
2627 template_p = &is_template;
2628 *template_p = cp_parser_optional_template_keyword (parser);
2629 /* Name lookup we do during the processing of the
2630 unqualified-id might obliterate SCOPE. */
2631 saved_scope = parser->scope;
2632 saved_object_scope = parser->object_scope;
2633 saved_qualifying_scope = parser->qualifying_scope;
2634 /* Process the final unqualified-id. */
2635 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2638 /* Restore the SAVED_SCOPE for our caller. */
2639 parser->scope = saved_scope;
2640 parser->object_scope = saved_object_scope;
2641 parser->qualifying_scope = saved_qualifying_scope;
2643 return unqualified_id;
2645 /* Otherwise, if we are in global scope, then we are looking at one
2646 of the other qualified-id productions. */
2647 else if (global_scope_p)
2652 /* Peek at the next token. */
2653 token = cp_lexer_peek_token (parser->lexer);
2655 /* If it's an identifier, and the next token is not a "<", then
2656 we can avoid the template-id case. This is an optimization
2657 for this common case. */
2658 if (token->type == CPP_NAME
2659 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2660 return cp_parser_identifier (parser);
2662 cp_parser_parse_tentatively (parser);
2663 /* Try a template-id. */
2664 id = cp_parser_template_id (parser,
2665 /*template_keyword_p=*/false,
2666 /*check_dependency_p=*/true,
2668 /* If that worked, we're done. */
2669 if (cp_parser_parse_definitely (parser))
2672 /* Peek at the next token. (Changes in the token buffer may
2673 have invalidated the pointer obtained above.) */
2674 token = cp_lexer_peek_token (parser->lexer);
2676 switch (token->type)
2679 return cp_parser_identifier (parser);
2682 if (token->keyword == RID_OPERATOR)
2683 return cp_parser_operator_function_id (parser);
2687 cp_parser_error (parser, "expected id-expression");
2688 return error_mark_node;
2692 return cp_parser_unqualified_id (parser, template_keyword_p,
2693 /*check_dependency_p=*/true,
2697 /* Parse an unqualified-id.
2701 operator-function-id
2702 conversion-function-id
2706 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2707 keyword, in a construct like `A::template ...'.
2709 Returns a representation of unqualified-id. For the `identifier'
2710 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2711 production a BIT_NOT_EXPR is returned; the operand of the
2712 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2713 other productions, see the documentation accompanying the
2714 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2715 names are looked up in uninstantiated templates. If DECLARATOR_P
2716 is true, the unqualified-id is appearing as part of a declarator,
2717 rather than as part of an expression. */
2720 cp_parser_unqualified_id (cp_parser* parser,
2721 bool template_keyword_p,
2722 bool check_dependency_p,
2727 /* Peek at the next token. */
2728 token = cp_lexer_peek_token (parser->lexer);
2730 switch (token->type)
2736 /* We don't know yet whether or not this will be a
2738 cp_parser_parse_tentatively (parser);
2739 /* Try a template-id. */
2740 id = cp_parser_template_id (parser, template_keyword_p,
2743 /* If it worked, we're done. */
2744 if (cp_parser_parse_definitely (parser))
2746 /* Otherwise, it's an ordinary identifier. */
2747 return cp_parser_identifier (parser);
2750 case CPP_TEMPLATE_ID:
2751 return cp_parser_template_id (parser, template_keyword_p,
2758 tree qualifying_scope;
2762 /* Consume the `~' token. */
2763 cp_lexer_consume_token (parser->lexer);
2764 /* Parse the class-name. The standard, as written, seems to
2767 template <typename T> struct S { ~S (); };
2768 template <typename T> S<T>::~S() {}
2770 is invalid, since `~' must be followed by a class-name, but
2771 `S<T>' is dependent, and so not known to be a class.
2772 That's not right; we need to look in uninstantiated
2773 templates. A further complication arises from:
2775 template <typename T> void f(T t) {
2779 Here, it is not possible to look up `T' in the scope of `T'
2780 itself. We must look in both the current scope, and the
2781 scope of the containing complete expression.
2783 Yet another issue is:
2792 The standard does not seem to say that the `S' in `~S'
2793 should refer to the type `S' and not the data member
2796 /* DR 244 says that we look up the name after the "~" in the
2797 same scope as we looked up the qualifying name. That idea
2798 isn't fully worked out; it's more complicated than that. */
2799 scope = parser->scope;
2800 object_scope = parser->object_scope;
2801 qualifying_scope = parser->qualifying_scope;
2803 /* If the name is of the form "X::~X" it's OK. */
2804 if (scope && TYPE_P (scope)
2805 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2806 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2808 && (cp_lexer_peek_token (parser->lexer)->value
2809 == TYPE_IDENTIFIER (scope)))
2811 cp_lexer_consume_token (parser->lexer);
2812 return build_nt (BIT_NOT_EXPR, scope);
2815 /* If there was an explicit qualification (S::~T), first look
2816 in the scope given by the qualification (i.e., S). */
2819 cp_parser_parse_tentatively (parser);
2820 type_decl = cp_parser_class_name (parser,
2821 /*typename_keyword_p=*/false,
2822 /*template_keyword_p=*/false,
2824 /*check_dependency=*/false,
2825 /*class_head_p=*/false,
2827 if (cp_parser_parse_definitely (parser))
2828 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2830 /* In "N::S::~S", look in "N" as well. */
2831 if (scope && qualifying_scope)
2833 cp_parser_parse_tentatively (parser);
2834 parser->scope = qualifying_scope;
2835 parser->object_scope = NULL_TREE;
2836 parser->qualifying_scope = NULL_TREE;
2838 = cp_parser_class_name (parser,
2839 /*typename_keyword_p=*/false,
2840 /*template_keyword_p=*/false,
2842 /*check_dependency=*/false,
2843 /*class_head_p=*/false,
2845 if (cp_parser_parse_definitely (parser))
2846 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2848 /* In "p->S::~T", look in the scope given by "*p" as well. */
2849 else if (object_scope)
2851 cp_parser_parse_tentatively (parser);
2852 parser->scope = object_scope;
2853 parser->object_scope = NULL_TREE;
2854 parser->qualifying_scope = NULL_TREE;
2856 = cp_parser_class_name (parser,
2857 /*typename_keyword_p=*/false,
2858 /*template_keyword_p=*/false,
2860 /*check_dependency=*/false,
2861 /*class_head_p=*/false,
2863 if (cp_parser_parse_definitely (parser))
2864 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2866 /* Look in the surrounding context. */
2867 parser->scope = NULL_TREE;
2868 parser->object_scope = NULL_TREE;
2869 parser->qualifying_scope = NULL_TREE;
2871 = cp_parser_class_name (parser,
2872 /*typename_keyword_p=*/false,
2873 /*template_keyword_p=*/false,
2875 /*check_dependency=*/false,
2876 /*class_head_p=*/false,
2878 /* If an error occurred, assume that the name of the
2879 destructor is the same as the name of the qualifying
2880 class. That allows us to keep parsing after running
2881 into ill-formed destructor names. */
2882 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2883 return build_nt (BIT_NOT_EXPR, scope);
2884 else if (type_decl == error_mark_node)
2885 return error_mark_node;
2889 A typedef-name that names a class shall not be used as the
2890 identifier in the declarator for a destructor declaration. */
2892 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2893 && !DECL_SELF_REFERENCE_P (type_decl))
2894 error ("typedef-name `%D' used as destructor declarator",
2897 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2901 if (token->keyword == RID_OPERATOR)
2905 /* This could be a template-id, so we try that first. */
2906 cp_parser_parse_tentatively (parser);
2907 /* Try a template-id. */
2908 id = cp_parser_template_id (parser, template_keyword_p,
2909 /*check_dependency_p=*/true,
2911 /* If that worked, we're done. */
2912 if (cp_parser_parse_definitely (parser))
2914 /* We still don't know whether we're looking at an
2915 operator-function-id or a conversion-function-id. */
2916 cp_parser_parse_tentatively (parser);
2917 /* Try an operator-function-id. */
2918 id = cp_parser_operator_function_id (parser);
2919 /* If that didn't work, try a conversion-function-id. */
2920 if (!cp_parser_parse_definitely (parser))
2921 id = cp_parser_conversion_function_id (parser);
2928 cp_parser_error (parser, "expected unqualified-id");
2929 return error_mark_node;
2933 /* Parse an (optional) nested-name-specifier.
2935 nested-name-specifier:
2936 class-or-namespace-name :: nested-name-specifier [opt]
2937 class-or-namespace-name :: template nested-name-specifier [opt]
2939 PARSER->SCOPE should be set appropriately before this function is
2940 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2941 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2944 Sets PARSER->SCOPE to the class (TYPE) or namespace
2945 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2946 it unchanged if there is no nested-name-specifier. Returns the new
2947 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
2949 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
2950 part of a declaration and/or decl-specifier. */
2953 cp_parser_nested_name_specifier_opt (cp_parser *parser,
2954 bool typename_keyword_p,
2955 bool check_dependency_p,
2957 bool is_declaration)
2959 bool success = false;
2960 tree access_check = NULL_TREE;
2964 /* If the next token corresponds to a nested name specifier, there
2965 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2966 false, it may have been true before, in which case something
2967 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2968 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2969 CHECK_DEPENDENCY_P is false, we have to fall through into the
2971 if (check_dependency_p
2972 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2974 cp_parser_pre_parsed_nested_name_specifier (parser);
2975 return parser->scope;
2978 /* Remember where the nested-name-specifier starts. */
2979 if (cp_parser_parsing_tentatively (parser)
2980 && !cp_parser_committed_to_tentative_parse (parser))
2982 token = cp_lexer_peek_token (parser->lexer);
2983 start = cp_lexer_token_difference (parser->lexer,
2984 parser->lexer->first_token,
2990 push_deferring_access_checks (dk_deferred);
2996 tree saved_qualifying_scope;
2997 bool template_keyword_p;
2999 /* Spot cases that cannot be the beginning of a
3000 nested-name-specifier. */
3001 token = cp_lexer_peek_token (parser->lexer);
3003 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3004 the already parsed nested-name-specifier. */
3005 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3007 /* Grab the nested-name-specifier and continue the loop. */
3008 cp_parser_pre_parsed_nested_name_specifier (parser);
3013 /* Spot cases that cannot be the beginning of a
3014 nested-name-specifier. On the second and subsequent times
3015 through the loop, we look for the `template' keyword. */
3016 if (success && token->keyword == RID_TEMPLATE)
3018 /* A template-id can start a nested-name-specifier. */
3019 else if (token->type == CPP_TEMPLATE_ID)
3023 /* If the next token is not an identifier, then it is
3024 definitely not a class-or-namespace-name. */
3025 if (token->type != CPP_NAME)
3027 /* If the following token is neither a `<' (to begin a
3028 template-id), nor a `::', then we are not looking at a
3029 nested-name-specifier. */
3030 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3031 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3035 /* The nested-name-specifier is optional, so we parse
3037 cp_parser_parse_tentatively (parser);
3039 /* Look for the optional `template' keyword, if this isn't the
3040 first time through the loop. */
3042 template_keyword_p = cp_parser_optional_template_keyword (parser);
3044 template_keyword_p = false;
3046 /* Save the old scope since the name lookup we are about to do
3047 might destroy it. */
3048 old_scope = parser->scope;
3049 saved_qualifying_scope = parser->qualifying_scope;
3050 /* Parse the qualifying entity. */
3052 = cp_parser_class_or_namespace_name (parser,
3058 /* Look for the `::' token. */
3059 cp_parser_require (parser, CPP_SCOPE, "`::'");
3061 /* If we found what we wanted, we keep going; otherwise, we're
3063 if (!cp_parser_parse_definitely (parser))
3065 bool error_p = false;
3067 /* Restore the OLD_SCOPE since it was valid before the
3068 failed attempt at finding the last
3069 class-or-namespace-name. */
3070 parser->scope = old_scope;
3071 parser->qualifying_scope = saved_qualifying_scope;
3072 /* If the next token is an identifier, and the one after
3073 that is a `::', then any valid interpretation would have
3074 found a class-or-namespace-name. */
3075 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3076 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3078 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3081 token = cp_lexer_consume_token (parser->lexer);
3086 decl = cp_parser_lookup_name_simple (parser, token->value);
3087 if (TREE_CODE (decl) == TEMPLATE_DECL)
3088 error ("`%D' used without template parameters",
3090 else if (parser->scope)
3092 if (TYPE_P (parser->scope))
3093 error ("`%T::%D' is not a class-name or "
3095 parser->scope, token->value);
3096 else if (parser->scope == global_namespace)
3097 error ("`::%D' is not a class-name or "
3101 error ("`%D::%D' is not a class-name or "
3103 parser->scope, token->value);
3106 error ("`%D' is not a class-name or namespace-name",
3108 parser->scope = NULL_TREE;
3110 /* Treat this as a successful nested-name-specifier
3115 If the name found is not a class-name (clause
3116 _class_) or namespace-name (_namespace.def_), the
3117 program is ill-formed. */
3120 cp_lexer_consume_token (parser->lexer);
3125 /* We've found one valid nested-name-specifier. */
3127 /* Make sure we look in the right scope the next time through
3129 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3130 ? TREE_TYPE (new_scope)
3132 /* If it is a class scope, try to complete it; we are about to
3133 be looking up names inside the class. */
3134 if (TYPE_P (parser->scope)
3135 /* Since checking types for dependency can be expensive,
3136 avoid doing it if the type is already complete. */
3137 && !COMPLETE_TYPE_P (parser->scope)
3138 /* Do not try to complete dependent types. */
3139 && !dependent_type_p (parser->scope))
3140 complete_type (parser->scope);
3143 /* Retrieve any deferred checks. Do not pop this access checks yet
3144 so the memory will not be reclaimed during token replacing below. */
3145 access_check = get_deferred_access_checks ();
3147 /* If parsing tentatively, replace the sequence of tokens that makes
3148 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3149 token. That way, should we re-parse the token stream, we will
3150 not have to repeat the effort required to do the parse, nor will
3151 we issue duplicate error messages. */
3152 if (success && start >= 0)
3154 /* Find the token that corresponds to the start of the
3156 token = cp_lexer_advance_token (parser->lexer,
3157 parser->lexer->first_token,
3160 /* Reset the contents of the START token. */
3161 token->type = CPP_NESTED_NAME_SPECIFIER;
3162 token->value = build_tree_list (access_check, parser->scope);
3163 TREE_TYPE (token->value) = parser->qualifying_scope;
3164 token->keyword = RID_MAX;
3165 /* Purge all subsequent tokens. */
3166 cp_lexer_purge_tokens_after (parser->lexer, token);
3169 pop_deferring_access_checks ();
3170 return success ? parser->scope : NULL_TREE;
3173 /* Parse a nested-name-specifier. See
3174 cp_parser_nested_name_specifier_opt for details. This function
3175 behaves identically, except that it will an issue an error if no
3176 nested-name-specifier is present, and it will return
3177 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3181 cp_parser_nested_name_specifier (cp_parser *parser,
3182 bool typename_keyword_p,
3183 bool check_dependency_p,
3185 bool is_declaration)
3189 /* Look for the nested-name-specifier. */
3190 scope = cp_parser_nested_name_specifier_opt (parser,
3195 /* If it was not present, issue an error message. */
3198 cp_parser_error (parser, "expected nested-name-specifier");
3199 parser->scope = NULL_TREE;
3200 return error_mark_node;
3206 /* Parse a class-or-namespace-name.
3208 class-or-namespace-name:
3212 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3213 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3214 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3215 TYPE_P is TRUE iff the next name should be taken as a class-name,
3216 even the same name is declared to be another entity in the same
3219 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3220 specified by the class-or-namespace-name. If neither is found the
3221 ERROR_MARK_NODE is returned. */
3224 cp_parser_class_or_namespace_name (cp_parser *parser,
3225 bool typename_keyword_p,
3226 bool template_keyword_p,
3227 bool check_dependency_p,
3229 bool is_declaration)
3232 tree saved_qualifying_scope;
3233 tree saved_object_scope;
3237 /* Before we try to parse the class-name, we must save away the
3238 current PARSER->SCOPE since cp_parser_class_name will destroy
3240 saved_scope = parser->scope;
3241 saved_qualifying_scope = parser->qualifying_scope;
3242 saved_object_scope = parser->object_scope;
3243 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3244 there is no need to look for a namespace-name. */
3245 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3247 cp_parser_parse_tentatively (parser);
3248 scope = cp_parser_class_name (parser,
3253 /*class_head_p=*/false,
3255 /* If that didn't work, try for a namespace-name. */
3256 if (!only_class_p && !cp_parser_parse_definitely (parser))
3258 /* Restore the saved scope. */
3259 parser->scope = saved_scope;
3260 parser->qualifying_scope = saved_qualifying_scope;
3261 parser->object_scope = saved_object_scope;
3262 /* If we are not looking at an identifier followed by the scope
3263 resolution operator, then this is not part of a
3264 nested-name-specifier. (Note that this function is only used
3265 to parse the components of a nested-name-specifier.) */
3266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3267 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3268 return error_mark_node;
3269 scope = cp_parser_namespace_name (parser);
3275 /* Parse a postfix-expression.
3279 postfix-expression [ expression ]
3280 postfix-expression ( expression-list [opt] )
3281 simple-type-specifier ( expression-list [opt] )
3282 typename :: [opt] nested-name-specifier identifier
3283 ( expression-list [opt] )
3284 typename :: [opt] nested-name-specifier template [opt] template-id
3285 ( expression-list [opt] )
3286 postfix-expression . template [opt] id-expression
3287 postfix-expression -> template [opt] id-expression
3288 postfix-expression . pseudo-destructor-name
3289 postfix-expression -> pseudo-destructor-name
3290 postfix-expression ++
3291 postfix-expression --
3292 dynamic_cast < type-id > ( expression )
3293 static_cast < type-id > ( expression )
3294 reinterpret_cast < type-id > ( expression )
3295 const_cast < type-id > ( expression )
3296 typeid ( expression )
3302 ( type-id ) { initializer-list , [opt] }
3304 This extension is a GNU version of the C99 compound-literal
3305 construct. (The C99 grammar uses `type-name' instead of `type-id',
3306 but they are essentially the same concept.)
3308 If ADDRESS_P is true, the postfix expression is the operand of the
3311 Returns a representation of the expression. */
3314 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3318 cp_id_kind idk = CP_ID_KIND_NONE;
3319 tree postfix_expression = NULL_TREE;
3320 /* Non-NULL only if the current postfix-expression can be used to
3321 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3322 class used to qualify the member. */
3323 tree qualifying_class = NULL_TREE;
3325 /* Peek at the next token. */
3326 token = cp_lexer_peek_token (parser->lexer);
3327 /* Some of the productions are determined by keywords. */
3328 keyword = token->keyword;
3338 const char *saved_message;
3340 /* All of these can be handled in the same way from the point
3341 of view of parsing. Begin by consuming the token
3342 identifying the cast. */
3343 cp_lexer_consume_token (parser->lexer);
3345 /* New types cannot be defined in the cast. */
3346 saved_message = parser->type_definition_forbidden_message;
3347 parser->type_definition_forbidden_message
3348 = "types may not be defined in casts";
3350 /* Look for the opening `<'. */
3351 cp_parser_require (parser, CPP_LESS, "`<'");
3352 /* Parse the type to which we are casting. */
3353 type = cp_parser_type_id (parser);
3354 /* Look for the closing `>'. */
3355 cp_parser_require (parser, CPP_GREATER, "`>'");
3356 /* Restore the old message. */
3357 parser->type_definition_forbidden_message = saved_message;
3359 /* And the expression which is being cast. */
3360 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3361 expression = cp_parser_expression (parser);
3362 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3364 /* Only type conversions to integral or enumeration types
3365 can be used in constant-expressions. */
3366 if (parser->constant_expression_p
3367 && !dependent_type_p (type)
3368 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3370 if (!parser->allow_non_constant_expression_p)
3371 return (cp_parser_non_constant_expression
3372 ("a cast to a type other than an integral or "
3373 "enumeration type"));
3374 parser->non_constant_expression_p = true;
3381 = build_dynamic_cast (type, expression);
3385 = build_static_cast (type, expression);
3389 = build_reinterpret_cast (type, expression);
3393 = build_const_cast (type, expression);
3404 const char *saved_message;
3406 /* Consume the `typeid' token. */
3407 cp_lexer_consume_token (parser->lexer);
3408 /* Look for the `(' token. */
3409 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3410 /* Types cannot be defined in a `typeid' expression. */
3411 saved_message = parser->type_definition_forbidden_message;
3412 parser->type_definition_forbidden_message
3413 = "types may not be defined in a `typeid\' expression";
3414 /* We can't be sure yet whether we're looking at a type-id or an
3416 cp_parser_parse_tentatively (parser);
3417 /* Try a type-id first. */
3418 type = cp_parser_type_id (parser);
3419 /* Look for the `)' token. Otherwise, we can't be sure that
3420 we're not looking at an expression: consider `typeid (int
3421 (3))', for example. */
3422 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3423 /* If all went well, simply lookup the type-id. */
3424 if (cp_parser_parse_definitely (parser))
3425 postfix_expression = get_typeid (type);
3426 /* Otherwise, fall back to the expression variant. */
3431 /* Look for an expression. */
3432 expression = cp_parser_expression (parser);
3433 /* Compute its typeid. */
3434 postfix_expression = build_typeid (expression);
3435 /* Look for the `)' token. */
3436 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3439 /* Restore the saved message. */
3440 parser->type_definition_forbidden_message = saved_message;
3446 bool template_p = false;
3450 /* Consume the `typename' token. */
3451 cp_lexer_consume_token (parser->lexer);
3452 /* Look for the optional `::' operator. */
3453 cp_parser_global_scope_opt (parser,
3454 /*current_scope_valid_p=*/false);
3455 /* Look for the nested-name-specifier. */
3456 cp_parser_nested_name_specifier (parser,
3457 /*typename_keyword_p=*/true,
3458 /*check_dependency_p=*/true,
3460 /*is_declaration=*/true);
3461 /* Look for the optional `template' keyword. */
3462 template_p = cp_parser_optional_template_keyword (parser);
3463 /* We don't know whether we're looking at a template-id or an
3465 cp_parser_parse_tentatively (parser);
3466 /* Try a template-id. */
3467 id = cp_parser_template_id (parser, template_p,
3468 /*check_dependency_p=*/true,
3469 /*is_declaration=*/true);
3470 /* If that didn't work, try an identifier. */
3471 if (!cp_parser_parse_definitely (parser))
3472 id = cp_parser_identifier (parser);
3473 /* Create a TYPENAME_TYPE to represent the type to which the
3474 functional cast is being performed. */
3475 type = make_typename_type (parser->scope, id,
3478 postfix_expression = cp_parser_functional_cast (parser, type);
3486 /* If the next thing is a simple-type-specifier, we may be
3487 looking at a functional cast. We could also be looking at
3488 an id-expression. So, we try the functional cast, and if
3489 that doesn't work we fall back to the primary-expression. */
3490 cp_parser_parse_tentatively (parser);
3491 /* Look for the simple-type-specifier. */
3492 type = cp_parser_simple_type_specifier (parser,
3493 CP_PARSER_FLAGS_NONE,
3494 /*identifier_p=*/false);
3495 /* Parse the cast itself. */
3496 if (!cp_parser_error_occurred (parser))
3498 = cp_parser_functional_cast (parser, type);
3499 /* If that worked, we're done. */
3500 if (cp_parser_parse_definitely (parser))
3503 /* If the functional-cast didn't work out, try a
3504 compound-literal. */
3505 if (cp_parser_allow_gnu_extensions_p (parser)
3506 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3508 tree initializer_list = NULL_TREE;
3510 cp_parser_parse_tentatively (parser);
3511 /* Consume the `('. */
3512 cp_lexer_consume_token (parser->lexer);
3513 /* Parse the type. */
3514 type = cp_parser_type_id (parser);
3515 /* Look for the `)'. */
3516 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3517 /* Look for the `{'. */
3518 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3519 /* If things aren't going well, there's no need to
3521 if (!cp_parser_error_occurred (parser))
3523 bool non_constant_p;
3524 /* Parse the initializer-list. */
3526 = cp_parser_initializer_list (parser, &non_constant_p);
3527 /* Allow a trailing `,'. */
3528 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3529 cp_lexer_consume_token (parser->lexer);
3530 /* Look for the final `}'. */
3531 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3533 /* If that worked, we're definitely looking at a
3534 compound-literal expression. */
3535 if (cp_parser_parse_definitely (parser))
3537 /* Warn the user that a compound literal is not
3538 allowed in standard C++. */
3540 pedwarn ("ISO C++ forbids compound-literals");
3541 /* Form the representation of the compound-literal. */
3543 = finish_compound_literal (type, initializer_list);
3548 /* It must be a primary-expression. */
3549 postfix_expression = cp_parser_primary_expression (parser,
3556 /* If we were avoiding committing to the processing of a
3557 qualified-id until we knew whether or not we had a
3558 pointer-to-member, we now know. */
3559 if (qualifying_class)
3563 /* Peek at the next token. */
3564 token = cp_lexer_peek_token (parser->lexer);
3565 done = (token->type != CPP_OPEN_SQUARE
3566 && token->type != CPP_OPEN_PAREN
3567 && token->type != CPP_DOT
3568 && token->type != CPP_DEREF
3569 && token->type != CPP_PLUS_PLUS
3570 && token->type != CPP_MINUS_MINUS);
3572 postfix_expression = finish_qualified_id_expr (qualifying_class,
3577 return postfix_expression;
3580 /* Keep looping until the postfix-expression is complete. */
3583 if (idk == CP_ID_KIND_UNQUALIFIED
3584 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3585 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3586 /* It is not a Koenig lookup function call. */
3588 = unqualified_name_lookup_error (postfix_expression);
3590 /* Peek at the next token. */
3591 token = cp_lexer_peek_token (parser->lexer);
3593 switch (token->type)
3595 case CPP_OPEN_SQUARE:
3596 /* postfix-expression [ expression ] */
3600 /* Consume the `[' token. */
3601 cp_lexer_consume_token (parser->lexer);
3602 /* Parse the index expression. */
3603 index = cp_parser_expression (parser);
3604 /* Look for the closing `]'. */
3605 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3607 /* Build the ARRAY_REF. */
3609 = grok_array_decl (postfix_expression, index);
3610 idk = CP_ID_KIND_NONE;
3614 case CPP_OPEN_PAREN:
3615 /* postfix-expression ( expression-list [opt] ) */
3618 tree args = (cp_parser_parenthesized_expression_list
3619 (parser, false, /*non_constant_p=*/NULL));
3621 if (args == error_mark_node)
3623 postfix_expression = error_mark_node;
3627 /* Function calls are not permitted in
3628 constant-expressions. */
3629 if (parser->constant_expression_p)
3631 if (!parser->allow_non_constant_expression_p)
3632 return cp_parser_non_constant_expression ("a function call");
3633 parser->non_constant_expression_p = true;
3637 if (idk == CP_ID_KIND_UNQUALIFIED)
3640 && (is_overloaded_fn (postfix_expression)
3641 || DECL_P (postfix_expression)
3642 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3646 = perform_koenig_lookup (postfix_expression, args);
3648 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3650 = unqualified_fn_lookup_error (postfix_expression);
3653 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3655 tree instance = TREE_OPERAND (postfix_expression, 0);
3656 tree fn = TREE_OPERAND (postfix_expression, 1);
3658 if (processing_template_decl
3659 && (type_dependent_expression_p (instance)
3660 || (!BASELINK_P (fn)
3661 && TREE_CODE (fn) != FIELD_DECL)
3662 || type_dependent_expression_p (fn)
3663 || any_type_dependent_arguments_p (args)))
3666 = build_min_nt (CALL_EXPR, postfix_expression, args);
3671 = (build_new_method_call
3672 (instance, fn, args, NULL_TREE,
3673 (idk == CP_ID_KIND_QUALIFIED
3674 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3676 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3677 || TREE_CODE (postfix_expression) == MEMBER_REF
3678 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3679 postfix_expression = (build_offset_ref_call_from_tree
3680 (postfix_expression, args));
3681 else if (idk == CP_ID_KIND_QUALIFIED)
3682 /* A call to a static class member, or a namespace-scope
3685 = finish_call_expr (postfix_expression, args,
3686 /*disallow_virtual=*/true,
3689 /* All other function calls. */
3691 = finish_call_expr (postfix_expression, args,
3692 /*disallow_virtual=*/false,
3695 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3696 idk = CP_ID_KIND_NONE;
3702 /* postfix-expression . template [opt] id-expression
3703 postfix-expression . pseudo-destructor-name
3704 postfix-expression -> template [opt] id-expression
3705 postfix-expression -> pseudo-destructor-name */
3710 tree scope = NULL_TREE;
3712 /* If this is a `->' operator, dereference the pointer. */
3713 if (token->type == CPP_DEREF)
3714 postfix_expression = build_x_arrow (postfix_expression);
3715 /* Check to see whether or not the expression is
3717 dependent_p = type_dependent_expression_p (postfix_expression);
3718 /* The identifier following the `->' or `.' is not
3720 parser->scope = NULL_TREE;
3721 parser->qualifying_scope = NULL_TREE;
3722 parser->object_scope = NULL_TREE;
3723 idk = CP_ID_KIND_NONE;
3724 /* Enter the scope corresponding to the type of the object
3725 given by the POSTFIX_EXPRESSION. */
3727 && TREE_TYPE (postfix_expression) != NULL_TREE)
3729 scope = TREE_TYPE (postfix_expression);
3730 /* According to the standard, no expression should
3731 ever have reference type. Unfortunately, we do not
3732 currently match the standard in this respect in
3733 that our internal representation of an expression
3734 may have reference type even when the standard says
3735 it does not. Therefore, we have to manually obtain
3736 the underlying type here. */
3737 scope = non_reference (scope);
3738 /* The type of the POSTFIX_EXPRESSION must be
3740 scope = complete_type_or_else (scope, NULL_TREE);
3741 /* Let the name lookup machinery know that we are
3742 processing a class member access expression. */
3743 parser->context->object_type = scope;
3744 /* If something went wrong, we want to be able to
3745 discern that case, as opposed to the case where
3746 there was no SCOPE due to the type of expression
3749 scope = error_mark_node;
3752 /* Consume the `.' or `->' operator. */
3753 cp_lexer_consume_token (parser->lexer);
3754 /* If the SCOPE is not a scalar type, we are looking at an
3755 ordinary class member access expression, rather than a
3756 pseudo-destructor-name. */
3757 if (!scope || !SCALAR_TYPE_P (scope))
3759 template_p = cp_parser_optional_template_keyword (parser);
3760 /* Parse the id-expression. */
3761 name = cp_parser_id_expression (parser,
3763 /*check_dependency_p=*/true,
3764 /*template_p=*/NULL,
3765 /*declarator_p=*/false);
3766 /* In general, build a SCOPE_REF if the member name is
3767 qualified. However, if the name was not dependent
3768 and has already been resolved; there is no need to
3769 build the SCOPE_REF. For example;
3771 struct X { void f(); };
3772 template <typename T> void f(T* t) { t->X::f(); }
3774 Even though "t" is dependent, "X::f" is not and has
3775 been resolved to a BASELINK; there is no need to
3776 include scope information. */
3778 /* But we do need to remember that there was an explicit
3779 scope for virtual function calls. */
3781 idk = CP_ID_KIND_QUALIFIED;
3783 if (name != error_mark_node
3784 && !BASELINK_P (name)
3787 name = build_nt (SCOPE_REF, parser->scope, name);
3788 parser->scope = NULL_TREE;
3789 parser->qualifying_scope = NULL_TREE;
3790 parser->object_scope = NULL_TREE;
3793 = finish_class_member_access_expr (postfix_expression, name);
3795 /* Otherwise, try the pseudo-destructor-name production. */
3801 /* Parse the pseudo-destructor-name. */
3802 cp_parser_pseudo_destructor_name (parser, &s, &type);
3803 /* Form the call. */
3805 = finish_pseudo_destructor_expr (postfix_expression,
3806 s, TREE_TYPE (type));
3809 /* We no longer need to look up names in the scope of the
3810 object on the left-hand side of the `.' or `->'
3812 parser->context->object_type = NULL_TREE;
3817 /* postfix-expression ++ */
3818 /* Consume the `++' token. */
3819 cp_lexer_consume_token (parser->lexer);
3820 /* Increments may not appear in constant-expressions. */
3821 if (parser->constant_expression_p)
3823 if (!parser->allow_non_constant_expression_p)
3824 return cp_parser_non_constant_expression ("an increment");
3825 parser->non_constant_expression_p = true;
3827 /* Generate a representation for the complete expression. */
3829 = finish_increment_expr (postfix_expression,
3830 POSTINCREMENT_EXPR);
3831 idk = CP_ID_KIND_NONE;
3834 case CPP_MINUS_MINUS:
3835 /* postfix-expression -- */
3836 /* Consume the `--' token. */
3837 cp_lexer_consume_token (parser->lexer);
3838 /* Decrements may not appear in constant-expressions. */
3839 if (parser->constant_expression_p)
3841 if (!parser->allow_non_constant_expression_p)
3842 return cp_parser_non_constant_expression ("a decrement");
3843 parser->non_constant_expression_p = true;
3845 /* Generate a representation for the complete expression. */
3847 = finish_increment_expr (postfix_expression,
3848 POSTDECREMENT_EXPR);
3849 idk = CP_ID_KIND_NONE;
3853 return postfix_expression;
3857 /* We should never get here. */
3859 return error_mark_node;
3862 /* Parse a parenthesized expression-list.
3865 assignment-expression
3866 expression-list, assignment-expression
3871 identifier, expression-list
3873 Returns a TREE_LIST. The TREE_VALUE of each node is a
3874 representation of an assignment-expression. Note that a TREE_LIST
3875 is returned even if there is only a single expression in the list.
3876 error_mark_node is returned if the ( and or ) are
3877 missing. NULL_TREE is returned on no expressions. The parentheses
3878 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3879 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3880 indicates whether or not all of the expressions in the list were
3884 cp_parser_parenthesized_expression_list (cp_parser* parser,
3885 bool is_attribute_list,
3886 bool *non_constant_p)
3888 tree expression_list = NULL_TREE;
3889 tree identifier = NULL_TREE;
3891 /* Assume all the expressions will be constant. */
3893 *non_constant_p = false;
3895 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3896 return error_mark_node;
3898 /* Consume expressions until there are no more. */
3899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3904 /* At the beginning of attribute lists, check to see if the
3905 next token is an identifier. */
3906 if (is_attribute_list
3907 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3911 /* Consume the identifier. */
3912 token = cp_lexer_consume_token (parser->lexer);
3913 /* Save the identifier. */
3914 identifier = token->value;
3918 /* Parse the next assignment-expression. */
3921 bool expr_non_constant_p;
3922 expr = (cp_parser_constant_expression
3923 (parser, /*allow_non_constant_p=*/true,
3924 &expr_non_constant_p));
3925 if (expr_non_constant_p)
3926 *non_constant_p = true;
3929 expr = cp_parser_assignment_expression (parser);
3931 /* Add it to the list. We add error_mark_node
3932 expressions to the list, so that we can still tell if
3933 the correct form for a parenthesized expression-list
3934 is found. That gives better errors. */
3935 expression_list = tree_cons (NULL_TREE, expr, expression_list);
3937 if (expr == error_mark_node)
3941 /* After the first item, attribute lists look the same as
3942 expression lists. */
3943 is_attribute_list = false;
3946 /* If the next token isn't a `,', then we are done. */
3947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3950 /* Otherwise, consume the `,' and keep going. */
3951 cp_lexer_consume_token (parser->lexer);
3954 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3959 /* We try and resync to an unnested comma, as that will give the
3960 user better diagnostics. */
3961 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true,
3962 /*consume_paren=*/true);
3966 return error_mark_node;
3969 /* We built up the list in reverse order so we must reverse it now. */
3970 expression_list = nreverse (expression_list);
3972 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3974 return expression_list;
3977 /* Parse a pseudo-destructor-name.
3979 pseudo-destructor-name:
3980 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3981 :: [opt] nested-name-specifier template template-id :: ~ type-name
3982 :: [opt] nested-name-specifier [opt] ~ type-name
3984 If either of the first two productions is used, sets *SCOPE to the
3985 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3986 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3987 or ERROR_MARK_NODE if no type-name is present. */
3990 cp_parser_pseudo_destructor_name (cp_parser* parser,
3994 bool nested_name_specifier_p;
3996 /* Look for the optional `::' operator. */
3997 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3998 /* Look for the optional nested-name-specifier. */
3999 nested_name_specifier_p
4000 = (cp_parser_nested_name_specifier_opt (parser,
4001 /*typename_keyword_p=*/false,
4002 /*check_dependency_p=*/true,
4004 /*is_declaration=*/true)
4006 /* Now, if we saw a nested-name-specifier, we might be doing the
4007 second production. */
4008 if (nested_name_specifier_p
4009 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4011 /* Consume the `template' keyword. */
4012 cp_lexer_consume_token (parser->lexer);
4013 /* Parse the template-id. */
4014 cp_parser_template_id (parser,
4015 /*template_keyword_p=*/true,
4016 /*check_dependency_p=*/false,
4017 /*is_declaration=*/true);
4018 /* Look for the `::' token. */
4019 cp_parser_require (parser, CPP_SCOPE, "`::'");
4021 /* If the next token is not a `~', then there might be some
4022 additional qualification. */
4023 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4025 /* Look for the type-name. */
4026 *scope = TREE_TYPE (cp_parser_type_name (parser));
4027 /* Look for the `::' token. */
4028 cp_parser_require (parser, CPP_SCOPE, "`::'");
4033 /* Look for the `~'. */
4034 cp_parser_require (parser, CPP_COMPL, "`~'");
4035 /* Look for the type-name again. We are not responsible for
4036 checking that it matches the first type-name. */
4037 *type = cp_parser_type_name (parser);
4040 /* Parse a unary-expression.
4046 unary-operator cast-expression
4047 sizeof unary-expression
4055 __extension__ cast-expression
4056 __alignof__ unary-expression
4057 __alignof__ ( type-id )
4058 __real__ cast-expression
4059 __imag__ cast-expression
4062 ADDRESS_P is true iff the unary-expression is appearing as the
4063 operand of the `&' operator.
4065 Returns a representation of the expression. */
4068 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4071 enum tree_code unary_operator;
4073 /* Peek at the next token. */
4074 token = cp_lexer_peek_token (parser->lexer);
4075 /* Some keywords give away the kind of expression. */
4076 if (token->type == CPP_KEYWORD)
4078 enum rid keyword = token->keyword;
4088 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4089 /* Consume the token. */
4090 cp_lexer_consume_token (parser->lexer);
4091 /* Parse the operand. */
4092 operand = cp_parser_sizeof_operand (parser, keyword);
4094 if (TYPE_P (operand))
4095 return cxx_sizeof_or_alignof_type (operand, op, true);
4097 return cxx_sizeof_or_alignof_expr (operand, op);
4101 return cp_parser_new_expression (parser);
4104 return cp_parser_delete_expression (parser);
4108 /* The saved value of the PEDANTIC flag. */
4112 /* Save away the PEDANTIC flag. */
4113 cp_parser_extension_opt (parser, &saved_pedantic);
4114 /* Parse the cast-expression. */
4115 expr = cp_parser_simple_cast_expression (parser);
4116 /* Restore the PEDANTIC flag. */
4117 pedantic = saved_pedantic;
4127 /* Consume the `__real__' or `__imag__' token. */
4128 cp_lexer_consume_token (parser->lexer);
4129 /* Parse the cast-expression. */
4130 expression = cp_parser_simple_cast_expression (parser);
4131 /* Create the complete representation. */
4132 return build_x_unary_op ((keyword == RID_REALPART
4133 ? REALPART_EXPR : IMAGPART_EXPR),
4143 /* Look for the `:: new' and `:: delete', which also signal the
4144 beginning of a new-expression, or delete-expression,
4145 respectively. If the next token is `::', then it might be one of
4147 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4151 /* See if the token after the `::' is one of the keywords in
4152 which we're interested. */
4153 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4154 /* If it's `new', we have a new-expression. */
4155 if (keyword == RID_NEW)
4156 return cp_parser_new_expression (parser);
4157 /* Similarly, for `delete'. */
4158 else if (keyword == RID_DELETE)
4159 return cp_parser_delete_expression (parser);
4162 /* Look for a unary operator. */
4163 unary_operator = cp_parser_unary_operator (token);
4164 /* The `++' and `--' operators can be handled similarly, even though
4165 they are not technically unary-operators in the grammar. */
4166 if (unary_operator == ERROR_MARK)
4168 if (token->type == CPP_PLUS_PLUS)
4169 unary_operator = PREINCREMENT_EXPR;
4170 else if (token->type == CPP_MINUS_MINUS)
4171 unary_operator = PREDECREMENT_EXPR;
4172 /* Handle the GNU address-of-label extension. */
4173 else if (cp_parser_allow_gnu_extensions_p (parser)
4174 && token->type == CPP_AND_AND)
4178 /* Consume the '&&' token. */
4179 cp_lexer_consume_token (parser->lexer);
4180 /* Look for the identifier. */
4181 identifier = cp_parser_identifier (parser);
4182 /* Create an expression representing the address. */
4183 return finish_label_address_expr (identifier);
4186 if (unary_operator != ERROR_MARK)
4188 tree cast_expression;
4190 /* Consume the operator token. */
4191 token = cp_lexer_consume_token (parser->lexer);
4192 /* Parse the cast-expression. */
4194 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4195 /* Now, build an appropriate representation. */
4196 switch (unary_operator)
4199 return build_x_indirect_ref (cast_expression, "unary *");
4203 return build_x_unary_op (unary_operator, cast_expression);
4205 case PREINCREMENT_EXPR:
4206 case PREDECREMENT_EXPR:
4207 if (parser->constant_expression_p)
4209 if (!parser->allow_non_constant_expression_p)
4210 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4213 parser->non_constant_expression_p = true;
4218 case TRUTH_NOT_EXPR:
4219 return finish_unary_op_expr (unary_operator, cast_expression);
4223 return error_mark_node;
4227 return cp_parser_postfix_expression (parser, address_p);
4230 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4231 unary-operator, the corresponding tree code is returned. */
4233 static enum tree_code
4234 cp_parser_unary_operator (cp_token* token)
4236 switch (token->type)
4239 return INDIRECT_REF;
4245 return CONVERT_EXPR;
4251 return TRUTH_NOT_EXPR;
4254 return BIT_NOT_EXPR;
4261 /* Parse a new-expression.
4264 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4265 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4267 Returns a representation of the expression. */
4270 cp_parser_new_expression (cp_parser* parser)
4272 bool global_scope_p;
4277 /* Look for the optional `::' operator. */
4279 = (cp_parser_global_scope_opt (parser,
4280 /*current_scope_valid_p=*/false)
4282 /* Look for the `new' operator. */
4283 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4284 /* There's no easy way to tell a new-placement from the
4285 `( type-id )' construct. */
4286 cp_parser_parse_tentatively (parser);
4287 /* Look for a new-placement. */
4288 placement = cp_parser_new_placement (parser);
4289 /* If that didn't work out, there's no new-placement. */
4290 if (!cp_parser_parse_definitely (parser))
4291 placement = NULL_TREE;
4293 /* If the next token is a `(', then we have a parenthesized
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4297 /* Consume the `('. */
4298 cp_lexer_consume_token (parser->lexer);
4299 /* Parse the type-id. */
4300 type = cp_parser_type_id (parser);
4301 /* Look for the closing `)'. */
4302 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4304 /* Otherwise, there must be a new-type-id. */
4306 type = cp_parser_new_type_id (parser);
4308 /* If the next token is a `(', then we have a new-initializer. */
4309 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4310 initializer = cp_parser_new_initializer (parser);
4312 initializer = NULL_TREE;
4314 /* Create a representation of the new-expression. */
4315 return build_new (placement, type, initializer, global_scope_p);
4318 /* Parse a new-placement.
4323 Returns the same representation as for an expression-list. */
4326 cp_parser_new_placement (cp_parser* parser)
4328 tree expression_list;
4330 /* Parse the expression-list. */
4331 expression_list = (cp_parser_parenthesized_expression_list
4332 (parser, false, /*non_constant_p=*/NULL));
4334 return expression_list;
4337 /* Parse a new-type-id.
4340 type-specifier-seq new-declarator [opt]
4342 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4343 and whose TREE_VALUE is the new-declarator. */
4346 cp_parser_new_type_id (cp_parser* parser)
4348 tree type_specifier_seq;
4350 const char *saved_message;
4352 /* The type-specifier sequence must not contain type definitions.
4353 (It cannot contain declarations of new types either, but if they
4354 are not definitions we will catch that because they are not
4356 saved_message = parser->type_definition_forbidden_message;
4357 parser->type_definition_forbidden_message
4358 = "types may not be defined in a new-type-id";
4359 /* Parse the type-specifier-seq. */
4360 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4361 /* Restore the old message. */
4362 parser->type_definition_forbidden_message = saved_message;
4363 /* Parse the new-declarator. */
4364 declarator = cp_parser_new_declarator_opt (parser);
4366 return build_tree_list (type_specifier_seq, declarator);
4369 /* Parse an (optional) new-declarator.
4372 ptr-operator new-declarator [opt]
4373 direct-new-declarator
4375 Returns a representation of the declarator. See
4376 cp_parser_declarator for the representations used. */
4379 cp_parser_new_declarator_opt (cp_parser* parser)
4381 enum tree_code code;
4383 tree cv_qualifier_seq;
4385 /* We don't know if there's a ptr-operator next, or not. */
4386 cp_parser_parse_tentatively (parser);
4387 /* Look for a ptr-operator. */
4388 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4389 /* If that worked, look for more new-declarators. */
4390 if (cp_parser_parse_definitely (parser))
4394 /* Parse another optional declarator. */
4395 declarator = cp_parser_new_declarator_opt (parser);
4397 /* Create the representation of the declarator. */
4398 if (code == INDIRECT_REF)
4399 declarator = make_pointer_declarator (cv_qualifier_seq,
4402 declarator = make_reference_declarator (cv_qualifier_seq,
4405 /* Handle the pointer-to-member case. */
4407 declarator = build_nt (SCOPE_REF, type, declarator);
4412 /* If the next token is a `[', there is a direct-new-declarator. */
4413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4414 return cp_parser_direct_new_declarator (parser);
4419 /* Parse a direct-new-declarator.
4421 direct-new-declarator:
4423 direct-new-declarator [constant-expression]
4425 Returns an ARRAY_REF, following the same conventions as are
4426 documented for cp_parser_direct_declarator. */
4429 cp_parser_direct_new_declarator (cp_parser* parser)
4431 tree declarator = NULL_TREE;
4437 /* Look for the opening `['. */
4438 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4439 /* The first expression is not required to be constant. */
4442 expression = cp_parser_expression (parser);
4443 /* The standard requires that the expression have integral
4444 type. DR 74 adds enumeration types. We believe that the
4445 real intent is that these expressions be handled like the
4446 expression in a `switch' condition, which also allows
4447 classes with a single conversion to integral or
4448 enumeration type. */
4449 if (!processing_template_decl)
4452 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4457 error ("expression in new-declarator must have integral or enumeration type");
4458 expression = error_mark_node;
4462 /* But all the other expressions must be. */
4465 = cp_parser_constant_expression (parser,
4466 /*allow_non_constant=*/false,
4468 /* Look for the closing `]'. */
4469 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4471 /* Add this bound to the declarator. */
4472 declarator = build_nt (ARRAY_REF, declarator, expression);
4474 /* If the next token is not a `[', then there are no more
4476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4483 /* Parse a new-initializer.
4486 ( expression-list [opt] )
4488 Returns a representation of the expression-list. If there is no
4489 expression-list, VOID_ZERO_NODE is returned. */
4492 cp_parser_new_initializer (cp_parser* parser)
4494 tree expression_list;
4496 expression_list = (cp_parser_parenthesized_expression_list
4497 (parser, false, /*non_constant_p=*/NULL));
4498 if (!expression_list)
4499 expression_list = void_zero_node;
4501 return expression_list;
4504 /* Parse a delete-expression.
4507 :: [opt] delete cast-expression
4508 :: [opt] delete [ ] cast-expression
4510 Returns a representation of the expression. */
4513 cp_parser_delete_expression (cp_parser* parser)
4515 bool global_scope_p;
4519 /* Look for the optional `::' operator. */
4521 = (cp_parser_global_scope_opt (parser,
4522 /*current_scope_valid_p=*/false)
4524 /* Look for the `delete' keyword. */
4525 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4526 /* See if the array syntax is in use. */
4527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4529 /* Consume the `[' token. */
4530 cp_lexer_consume_token (parser->lexer);
4531 /* Look for the `]' token. */
4532 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4533 /* Remember that this is the `[]' construct. */
4539 /* Parse the cast-expression. */
4540 expression = cp_parser_simple_cast_expression (parser);
4542 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4545 /* Parse a cast-expression.
4549 ( type-id ) cast-expression
4551 Returns a representation of the expression. */
4554 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4556 /* If it's a `(', then we might be looking at a cast. */
4557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4559 tree type = NULL_TREE;
4560 tree expr = NULL_TREE;
4561 bool compound_literal_p;
4562 const char *saved_message;
4564 /* There's no way to know yet whether or not this is a cast.
4565 For example, `(int (3))' is a unary-expression, while `(int)
4566 3' is a cast. So, we resort to parsing tentatively. */
4567 cp_parser_parse_tentatively (parser);
4568 /* Types may not be defined in a cast. */
4569 saved_message = parser->type_definition_forbidden_message;
4570 parser->type_definition_forbidden_message
4571 = "types may not be defined in casts";
4572 /* Consume the `('. */
4573 cp_lexer_consume_token (parser->lexer);
4574 /* A very tricky bit is that `(struct S) { 3 }' is a
4575 compound-literal (which we permit in C++ as an extension).
4576 But, that construct is not a cast-expression -- it is a
4577 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4578 is legal; if the compound-literal were a cast-expression,
4579 you'd need an extra set of parentheses.) But, if we parse
4580 the type-id, and it happens to be a class-specifier, then we
4581 will commit to the parse at that point, because we cannot
4582 undo the action that is done when creating a new class. So,
4583 then we cannot back up and do a postfix-expression.
4585 Therefore, we scan ahead to the closing `)', and check to see
4586 if the token after the `)' is a `{'. If so, we are not
4587 looking at a cast-expression.
4589 Save tokens so that we can put them back. */
4590 cp_lexer_save_tokens (parser->lexer);
4591 /* Skip tokens until the next token is a closing parenthesis.
4592 If we find the closing `)', and the next token is a `{', then
4593 we are looking at a compound-literal. */
4595 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4596 /*consume_paren=*/true)
4597 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4598 /* Roll back the tokens we skipped. */
4599 cp_lexer_rollback_tokens (parser->lexer);
4600 /* If we were looking at a compound-literal, simulate an error
4601 so that the call to cp_parser_parse_definitely below will
4603 if (compound_literal_p)
4604 cp_parser_simulate_error (parser);
4607 /* Look for the type-id. */
4608 type = cp_parser_type_id (parser);
4609 /* Look for the closing `)'. */
4610 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4613 /* Restore the saved message. */
4614 parser->type_definition_forbidden_message = saved_message;
4616 /* If ok so far, parse the dependent expression. We cannot be
4617 sure it is a cast. Consider `(T ())'. It is a parenthesized
4618 ctor of T, but looks like a cast to function returning T
4619 without a dependent expression. */
4620 if (!cp_parser_error_occurred (parser))
4621 expr = cp_parser_simple_cast_expression (parser);
4623 if (cp_parser_parse_definitely (parser))
4625 /* Warn about old-style casts, if so requested. */
4626 if (warn_old_style_cast
4627 && !in_system_header
4628 && !VOID_TYPE_P (type)
4629 && current_lang_name != lang_name_c)
4630 warning ("use of old-style cast");
4632 /* Only type conversions to integral or enumeration types
4633 can be used in constant-expressions. */
4634 if (parser->constant_expression_p
4635 && !dependent_type_p (type)
4636 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4638 if (!parser->allow_non_constant_expression_p)
4639 return (cp_parser_non_constant_expression
4640 ("a casts to a type other than an integral or "
4641 "enumeration type"));
4642 parser->non_constant_expression_p = true;
4644 /* Perform the cast. */
4645 expr = build_c_cast (type, expr);
4650 /* If we get here, then it's not a cast, so it must be a
4651 unary-expression. */
4652 return cp_parser_unary_expression (parser, address_p);
4655 /* Parse a pm-expression.
4659 pm-expression .* cast-expression
4660 pm-expression ->* cast-expression
4662 Returns a representation of the expression. */
4665 cp_parser_pm_expression (cp_parser* parser)
4667 static const cp_parser_token_tree_map map = {
4668 { CPP_DEREF_STAR, MEMBER_REF },
4669 { CPP_DOT_STAR, DOTSTAR_EXPR },
4670 { CPP_EOF, ERROR_MARK }
4673 return cp_parser_binary_expression (parser, map,
4674 cp_parser_simple_cast_expression);
4677 /* Parse a multiplicative-expression.
4679 mulitplicative-expression:
4681 multiplicative-expression * pm-expression
4682 multiplicative-expression / pm-expression
4683 multiplicative-expression % pm-expression
4685 Returns a representation of the expression. */
4688 cp_parser_multiplicative_expression (cp_parser* parser)
4690 static const cp_parser_token_tree_map map = {
4691 { CPP_MULT, MULT_EXPR },
4692 { CPP_DIV, TRUNC_DIV_EXPR },
4693 { CPP_MOD, TRUNC_MOD_EXPR },
4694 { CPP_EOF, ERROR_MARK }
4697 return cp_parser_binary_expression (parser,
4699 cp_parser_pm_expression);
4702 /* Parse an additive-expression.
4704 additive-expression:
4705 multiplicative-expression
4706 additive-expression + multiplicative-expression
4707 additive-expression - multiplicative-expression
4709 Returns a representation of the expression. */
4712 cp_parser_additive_expression (cp_parser* parser)
4714 static const cp_parser_token_tree_map map = {
4715 { CPP_PLUS, PLUS_EXPR },
4716 { CPP_MINUS, MINUS_EXPR },
4717 { CPP_EOF, ERROR_MARK }
4720 return cp_parser_binary_expression (parser,
4722 cp_parser_multiplicative_expression);
4725 /* Parse a shift-expression.
4729 shift-expression << additive-expression
4730 shift-expression >> additive-expression
4732 Returns a representation of the expression. */
4735 cp_parser_shift_expression (cp_parser* parser)
4737 static const cp_parser_token_tree_map map = {
4738 { CPP_LSHIFT, LSHIFT_EXPR },
4739 { CPP_RSHIFT, RSHIFT_EXPR },
4740 { CPP_EOF, ERROR_MARK }
4743 return cp_parser_binary_expression (parser,
4745 cp_parser_additive_expression);
4748 /* Parse a relational-expression.
4750 relational-expression:
4752 relational-expression < shift-expression
4753 relational-expression > shift-expression
4754 relational-expression <= shift-expression
4755 relational-expression >= shift-expression
4759 relational-expression:
4760 relational-expression <? shift-expression
4761 relational-expression >? shift-expression
4763 Returns a representation of the expression. */
4766 cp_parser_relational_expression (cp_parser* parser)
4768 static const cp_parser_token_tree_map map = {
4769 { CPP_LESS, LT_EXPR },
4770 { CPP_GREATER, GT_EXPR },
4771 { CPP_LESS_EQ, LE_EXPR },
4772 { CPP_GREATER_EQ, GE_EXPR },
4773 { CPP_MIN, MIN_EXPR },
4774 { CPP_MAX, MAX_EXPR },
4775 { CPP_EOF, ERROR_MARK }
4778 return cp_parser_binary_expression (parser,
4780 cp_parser_shift_expression);
4783 /* Parse an equality-expression.
4785 equality-expression:
4786 relational-expression
4787 equality-expression == relational-expression
4788 equality-expression != relational-expression
4790 Returns a representation of the expression. */
4793 cp_parser_equality_expression (cp_parser* parser)
4795 static const cp_parser_token_tree_map map = {
4796 { CPP_EQ_EQ, EQ_EXPR },
4797 { CPP_NOT_EQ, NE_EXPR },
4798 { CPP_EOF, ERROR_MARK }
4801 return cp_parser_binary_expression (parser,
4803 cp_parser_relational_expression);
4806 /* Parse an and-expression.
4810 and-expression & equality-expression
4812 Returns a representation of the expression. */
4815 cp_parser_and_expression (cp_parser* parser)
4817 static const cp_parser_token_tree_map map = {
4818 { CPP_AND, BIT_AND_EXPR },
4819 { CPP_EOF, ERROR_MARK }
4822 return cp_parser_binary_expression (parser,
4824 cp_parser_equality_expression);
4827 /* Parse an exclusive-or-expression.
4829 exclusive-or-expression:
4831 exclusive-or-expression ^ and-expression
4833 Returns a representation of the expression. */
4836 cp_parser_exclusive_or_expression (cp_parser* parser)
4838 static const cp_parser_token_tree_map map = {
4839 { CPP_XOR, BIT_XOR_EXPR },
4840 { CPP_EOF, ERROR_MARK }
4843 return cp_parser_binary_expression (parser,
4845 cp_parser_and_expression);
4849 /* Parse an inclusive-or-expression.
4851 inclusive-or-expression:
4852 exclusive-or-expression
4853 inclusive-or-expression | exclusive-or-expression
4855 Returns a representation of the expression. */
4858 cp_parser_inclusive_or_expression (cp_parser* parser)
4860 static const cp_parser_token_tree_map map = {
4861 { CPP_OR, BIT_IOR_EXPR },
4862 { CPP_EOF, ERROR_MARK }
4865 return cp_parser_binary_expression (parser,
4867 cp_parser_exclusive_or_expression);
4870 /* Parse a logical-and-expression.
4872 logical-and-expression:
4873 inclusive-or-expression
4874 logical-and-expression && inclusive-or-expression
4876 Returns a representation of the expression. */
4879 cp_parser_logical_and_expression (cp_parser* parser)
4881 static const cp_parser_token_tree_map map = {
4882 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4883 { CPP_EOF, ERROR_MARK }
4886 return cp_parser_binary_expression (parser,
4888 cp_parser_inclusive_or_expression);
4891 /* Parse a logical-or-expression.
4893 logical-or-expression:
4894 logical-and-expression
4895 logical-or-expression || logical-and-expression
4897 Returns a representation of the expression. */
4900 cp_parser_logical_or_expression (cp_parser* parser)
4902 static const cp_parser_token_tree_map map = {
4903 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4904 { CPP_EOF, ERROR_MARK }
4907 return cp_parser_binary_expression (parser,
4909 cp_parser_logical_and_expression);
4912 /* Parse the `? expression : assignment-expression' part of a
4913 conditional-expression. The LOGICAL_OR_EXPR is the
4914 logical-or-expression that started the conditional-expression.
4915 Returns a representation of the entire conditional-expression.
4917 This routine is used by cp_parser_assignment_expression.
4919 ? expression : assignment-expression
4923 ? : assignment-expression */
4926 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4929 tree assignment_expr;
4931 /* Consume the `?' token. */
4932 cp_lexer_consume_token (parser->lexer);
4933 if (cp_parser_allow_gnu_extensions_p (parser)
4934 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4935 /* Implicit true clause. */
4938 /* Parse the expression. */
4939 expr = cp_parser_expression (parser);
4941 /* The next token should be a `:'. */
4942 cp_parser_require (parser, CPP_COLON, "`:'");
4943 /* Parse the assignment-expression. */
4944 assignment_expr = cp_parser_assignment_expression (parser);
4946 /* Build the conditional-expression. */
4947 return build_x_conditional_expr (logical_or_expr,
4952 /* Parse an assignment-expression.
4954 assignment-expression:
4955 conditional-expression
4956 logical-or-expression assignment-operator assignment_expression
4959 Returns a representation for the expression. */
4962 cp_parser_assignment_expression (cp_parser* parser)
4966 /* If the next token is the `throw' keyword, then we're looking at
4967 a throw-expression. */
4968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4969 expr = cp_parser_throw_expression (parser);
4970 /* Otherwise, it must be that we are looking at a
4971 logical-or-expression. */
4974 /* Parse the logical-or-expression. */
4975 expr = cp_parser_logical_or_expression (parser);
4976 /* If the next token is a `?' then we're actually looking at a
4977 conditional-expression. */
4978 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4979 return cp_parser_question_colon_clause (parser, expr);
4982 enum tree_code assignment_operator;
4984 /* If it's an assignment-operator, we're using the second
4987 = cp_parser_assignment_operator_opt (parser);
4988 if (assignment_operator != ERROR_MARK)
4992 /* Parse the right-hand side of the assignment. */
4993 rhs = cp_parser_assignment_expression (parser);
4994 /* An assignment may not appear in a
4995 constant-expression. */
4996 if (parser->constant_expression_p)
4998 if (!parser->allow_non_constant_expression_p)
4999 return cp_parser_non_constant_expression ("an assignment");
5000 parser->non_constant_expression_p = true;
5002 /* Build the assignment expression. */
5003 expr = build_x_modify_expr (expr,
5004 assignment_operator,
5013 /* Parse an (optional) assignment-operator.
5015 assignment-operator: one of
5016 = *= /= %= += -= >>= <<= &= ^= |=
5020 assignment-operator: one of
5023 If the next token is an assignment operator, the corresponding tree
5024 code is returned, and the token is consumed. For example, for
5025 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5026 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5027 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5028 operator, ERROR_MARK is returned. */
5030 static enum tree_code
5031 cp_parser_assignment_operator_opt (cp_parser* parser)
5036 /* Peek at the next toen. */
5037 token = cp_lexer_peek_token (parser->lexer);
5039 switch (token->type)
5050 op = TRUNC_DIV_EXPR;
5054 op = TRUNC_MOD_EXPR;
5094 /* Nothing else is an assignment operator. */
5098 /* If it was an assignment operator, consume it. */
5099 if (op != ERROR_MARK)
5100 cp_lexer_consume_token (parser->lexer);
5105 /* Parse an expression.
5108 assignment-expression
5109 expression , assignment-expression
5111 Returns a representation of the expression. */
5114 cp_parser_expression (cp_parser* parser)
5116 tree expression = NULL_TREE;
5120 tree assignment_expression;
5122 /* Parse the next assignment-expression. */
5123 assignment_expression
5124 = cp_parser_assignment_expression (parser);
5125 /* If this is the first assignment-expression, we can just
5128 expression = assignment_expression;
5130 expression = build_x_compound_expr (expression,
5131 assignment_expression);
5132 /* If the next token is not a comma, then we are done with the
5134 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5136 /* Consume the `,'. */
5137 cp_lexer_consume_token (parser->lexer);
5138 /* A comma operator cannot appear in a constant-expression. */
5139 if (parser->constant_expression_p)
5141 if (!parser->allow_non_constant_expression_p)
5143 = cp_parser_non_constant_expression ("a comma operator");
5144 parser->non_constant_expression_p = true;
5151 /* Parse a constant-expression.
5153 constant-expression:
5154 conditional-expression
5156 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5157 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5158 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5159 is false, NON_CONSTANT_P should be NULL. */
5162 cp_parser_constant_expression (cp_parser* parser,
5163 bool allow_non_constant_p,
5164 bool *non_constant_p)
5166 bool saved_constant_expression_p;
5167 bool saved_allow_non_constant_expression_p;
5168 bool saved_non_constant_expression_p;
5171 /* It might seem that we could simply parse the
5172 conditional-expression, and then check to see if it were
5173 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5174 one that the compiler can figure out is constant, possibly after
5175 doing some simplifications or optimizations. The standard has a
5176 precise definition of constant-expression, and we must honor
5177 that, even though it is somewhat more restrictive.
5183 is not a legal declaration, because `(2, 3)' is not a
5184 constant-expression. The `,' operator is forbidden in a
5185 constant-expression. However, GCC's constant-folding machinery
5186 will fold this operation to an INTEGER_CST for `3'. */
5188 /* Save the old settings. */
5189 saved_constant_expression_p = parser->constant_expression_p;
5190 saved_allow_non_constant_expression_p
5191 = parser->allow_non_constant_expression_p;
5192 saved_non_constant_expression_p = parser->non_constant_expression_p;
5193 /* We are now parsing a constant-expression. */
5194 parser->constant_expression_p = true;
5195 parser->allow_non_constant_expression_p = allow_non_constant_p;
5196 parser->non_constant_expression_p = false;
5197 /* Although the grammar says "conditional-expression", we parse an
5198 "assignment-expression", which also permits "throw-expression"
5199 and the use of assignment operators. In the case that
5200 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5201 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5202 actually essential that we look for an assignment-expression.
5203 For example, cp_parser_initializer_clauses uses this function to
5204 determine whether a particular assignment-expression is in fact
5206 expression = cp_parser_assignment_expression (parser);
5207 /* Restore the old settings. */
5208 parser->constant_expression_p = saved_constant_expression_p;
5209 parser->allow_non_constant_expression_p
5210 = saved_allow_non_constant_expression_p;
5211 if (allow_non_constant_p)
5212 *non_constant_p = parser->non_constant_expression_p;
5213 parser->non_constant_expression_p = saved_non_constant_expression_p;
5218 /* Statements [gram.stmt.stmt] */
5220 /* Parse a statement.
5224 expression-statement
5229 declaration-statement
5233 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5237 int statement_line_number;
5239 /* There is no statement yet. */
5240 statement = NULL_TREE;
5241 /* Peek at the next token. */
5242 token = cp_lexer_peek_token (parser->lexer);
5243 /* Remember the line number of the first token in the statement. */
5244 statement_line_number = token->location.line;
5245 /* If this is a keyword, then that will often determine what kind of
5246 statement we have. */
5247 if (token->type == CPP_KEYWORD)
5249 enum rid keyword = token->keyword;
5255 statement = cp_parser_labeled_statement (parser,
5256 in_statement_expr_p);
5261 statement = cp_parser_selection_statement (parser);
5267 statement = cp_parser_iteration_statement (parser);
5274 statement = cp_parser_jump_statement (parser);
5278 statement = cp_parser_try_block (parser);
5282 /* It might be a keyword like `int' that can start a
5283 declaration-statement. */
5287 else if (token->type == CPP_NAME)
5289 /* If the next token is a `:', then we are looking at a
5290 labeled-statement. */
5291 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5292 if (token->type == CPP_COLON)
5293 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5295 /* Anything that starts with a `{' must be a compound-statement. */
5296 else if (token->type == CPP_OPEN_BRACE)
5297 statement = cp_parser_compound_statement (parser, false);
5299 /* Everything else must be a declaration-statement or an
5300 expression-statement. Try for the declaration-statement
5301 first, unless we are looking at a `;', in which case we know that
5302 we have an expression-statement. */
5305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5307 cp_parser_parse_tentatively (parser);
5308 /* Try to parse the declaration-statement. */
5309 cp_parser_declaration_statement (parser);
5310 /* If that worked, we're done. */
5311 if (cp_parser_parse_definitely (parser))
5314 /* Look for an expression-statement instead. */
5315 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5318 /* Set the line number for the statement. */
5319 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5320 STMT_LINENO (statement) = statement_line_number;
5323 /* Parse a labeled-statement.
5326 identifier : statement
5327 case constant-expression : statement
5330 Returns the new CASE_LABEL, for a `case' or `default' label. For
5331 an ordinary label, returns a LABEL_STMT. */
5334 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5337 tree statement = error_mark_node;
5339 /* The next token should be an identifier. */
5340 token = cp_lexer_peek_token (parser->lexer);
5341 if (token->type != CPP_NAME
5342 && token->type != CPP_KEYWORD)
5344 cp_parser_error (parser, "expected labeled-statement");
5345 return error_mark_node;
5348 switch (token->keyword)
5354 /* Consume the `case' token. */
5355 cp_lexer_consume_token (parser->lexer);
5356 /* Parse the constant-expression. */
5357 expr = cp_parser_constant_expression (parser,
5358 /*allow_non_constant_p=*/false,
5360 if (!parser->in_switch_statement_p)
5361 error ("case label `%E' not within a switch statement", expr);
5363 statement = finish_case_label (expr, NULL_TREE);
5368 /* Consume the `default' token. */
5369 cp_lexer_consume_token (parser->lexer);
5370 if (!parser->in_switch_statement_p)
5371 error ("case label not within a switch statement");
5373 statement = finish_case_label (NULL_TREE, NULL_TREE);
5377 /* Anything else must be an ordinary label. */
5378 statement = finish_label_stmt (cp_parser_identifier (parser));
5382 /* Require the `:' token. */
5383 cp_parser_require (parser, CPP_COLON, "`:'");
5384 /* Parse the labeled statement. */
5385 cp_parser_statement (parser, in_statement_expr_p);
5387 /* Return the label, in the case of a `case' or `default' label. */
5391 /* Parse an expression-statement.
5393 expression-statement:
5396 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5397 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5398 indicates whether this expression-statement is part of an
5399 expression statement. */
5402 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5404 tree statement = NULL_TREE;
5406 /* If the next token is a ';', then there is no expression
5408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5409 statement = cp_parser_expression (parser);
5411 /* Consume the final `;'. */
5412 cp_parser_consume_semicolon_at_end_of_statement (parser);
5414 if (in_statement_expr_p
5415 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5417 /* This is the final expression statement of a statement
5419 statement = finish_stmt_expr_expr (statement);
5422 statement = finish_expr_stmt (statement);
5429 /* Parse a compound-statement.
5432 { statement-seq [opt] }
5434 Returns a COMPOUND_STMT representing the statement. */
5437 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5441 /* Consume the `{'. */
5442 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5443 return error_mark_node;
5444 /* Begin the compound-statement. */
5445 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5446 /* Parse an (optional) statement-seq. */
5447 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5448 /* Finish the compound-statement. */
5449 finish_compound_stmt (compound_stmt);
5450 /* Consume the `}'. */
5451 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5453 return compound_stmt;
5456 /* Parse an (optional) statement-seq.
5460 statement-seq [opt] statement */
5463 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5465 /* Scan statements until there aren't any more. */
5468 /* If we're looking at a `}', then we've run out of statements. */
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5470 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5473 /* Parse the statement. */
5474 cp_parser_statement (parser, in_statement_expr_p);
5478 /* Parse a selection-statement.
5480 selection-statement:
5481 if ( condition ) statement
5482 if ( condition ) statement else statement
5483 switch ( condition ) statement
5485 Returns the new IF_STMT or SWITCH_STMT. */
5488 cp_parser_selection_statement (cp_parser* parser)
5493 /* Peek at the next token. */
5494 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5496 /* See what kind of keyword it is. */
5497 keyword = token->keyword;
5506 /* Look for the `('. */
5507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5509 cp_parser_skip_to_end_of_statement (parser);
5510 return error_mark_node;
5513 /* Begin the selection-statement. */
5514 if (keyword == RID_IF)
5515 statement = begin_if_stmt ();
5517 statement = begin_switch_stmt ();
5519 /* Parse the condition. */
5520 condition = cp_parser_condition (parser);
5521 /* Look for the `)'. */
5522 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5523 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5524 /*consume_paren=*/true);
5526 if (keyword == RID_IF)
5530 /* Add the condition. */
5531 finish_if_stmt_cond (condition, statement);
5533 /* Parse the then-clause. */
5534 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5535 finish_then_clause (statement);
5537 /* If the next token is `else', parse the else-clause. */
5538 if (cp_lexer_next_token_is_keyword (parser->lexer,
5543 /* Consume the `else' keyword. */
5544 cp_lexer_consume_token (parser->lexer);
5545 /* Parse the else-clause. */
5547 = cp_parser_implicitly_scoped_statement (parser);
5548 finish_else_clause (statement);
5551 /* Now we're all done with the if-statement. */
5557 bool in_switch_statement_p;
5559 /* Add the condition. */
5560 finish_switch_cond (condition, statement);
5562 /* Parse the body of the switch-statement. */
5563 in_switch_statement_p = parser->in_switch_statement_p;
5564 parser->in_switch_statement_p = true;
5565 body = cp_parser_implicitly_scoped_statement (parser);
5566 parser->in_switch_statement_p = in_switch_statement_p;
5568 /* Now we're all done with the switch-statement. */
5569 finish_switch_stmt (statement);
5577 cp_parser_error (parser, "expected selection-statement");
5578 return error_mark_node;
5582 /* Parse a condition.
5586 type-specifier-seq declarator = assignment-expression
5591 type-specifier-seq declarator asm-specification [opt]
5592 attributes [opt] = assignment-expression
5594 Returns the expression that should be tested. */
5597 cp_parser_condition (cp_parser* parser)
5599 tree type_specifiers;
5600 const char *saved_message;
5602 /* Try the declaration first. */
5603 cp_parser_parse_tentatively (parser);
5604 /* New types are not allowed in the type-specifier-seq for a
5606 saved_message = parser->type_definition_forbidden_message;
5607 parser->type_definition_forbidden_message
5608 = "types may not be defined in conditions";
5609 /* Parse the type-specifier-seq. */
5610 type_specifiers = cp_parser_type_specifier_seq (parser);
5611 /* Restore the saved message. */
5612 parser->type_definition_forbidden_message = saved_message;
5613 /* If all is well, we might be looking at a declaration. */
5614 if (!cp_parser_error_occurred (parser))
5617 tree asm_specification;
5620 tree initializer = NULL_TREE;
5622 /* Parse the declarator. */
5623 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5624 /*ctor_dtor_or_conv_p=*/NULL);
5625 /* Parse the attributes. */
5626 attributes = cp_parser_attributes_opt (parser);
5627 /* Parse the asm-specification. */
5628 asm_specification = cp_parser_asm_specification_opt (parser);
5629 /* If the next token is not an `=', then we might still be
5630 looking at an expression. For example:
5634 looks like a decl-specifier-seq and a declarator -- but then
5635 there is no `=', so this is an expression. */
5636 cp_parser_require (parser, CPP_EQ, "`='");
5637 /* If we did see an `=', then we are looking at a declaration
5639 if (cp_parser_parse_definitely (parser))
5641 /* Create the declaration. */
5642 decl = start_decl (declarator, type_specifiers,
5643 /*initialized_p=*/true,
5644 attributes, /*prefix_attributes=*/NULL_TREE);
5645 /* Parse the assignment-expression. */
5646 initializer = cp_parser_assignment_expression (parser);
5648 /* Process the initializer. */
5649 cp_finish_decl (decl,
5652 LOOKUP_ONLYCONVERTING);
5654 return convert_from_reference (decl);
5657 /* If we didn't even get past the declarator successfully, we are
5658 definitely not looking at a declaration. */
5660 cp_parser_abort_tentative_parse (parser);
5662 /* Otherwise, we are looking at an expression. */
5663 return cp_parser_expression (parser);
5666 /* Parse an iteration-statement.
5668 iteration-statement:
5669 while ( condition ) statement
5670 do statement while ( expression ) ;
5671 for ( for-init-statement condition [opt] ; expression [opt] )
5674 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5677 cp_parser_iteration_statement (cp_parser* parser)
5682 bool in_iteration_statement_p;
5685 /* Peek at the next token. */
5686 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5688 return error_mark_node;
5690 /* Remember whether or not we are already within an iteration
5692 in_iteration_statement_p = parser->in_iteration_statement_p;
5694 /* See what kind of keyword it is. */
5695 keyword = token->keyword;
5702 /* Begin the while-statement. */
5703 statement = begin_while_stmt ();
5704 /* Look for the `('. */
5705 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5706 /* Parse the condition. */
5707 condition = cp_parser_condition (parser);
5708 finish_while_stmt_cond (condition, statement);
5709 /* Look for the `)'. */
5710 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5711 /* Parse the dependent statement. */
5712 parser->in_iteration_statement_p = true;
5713 cp_parser_already_scoped_statement (parser);
5714 parser->in_iteration_statement_p = in_iteration_statement_p;
5715 /* We're done with the while-statement. */
5716 finish_while_stmt (statement);
5724 /* Begin the do-statement. */
5725 statement = begin_do_stmt ();
5726 /* Parse the body of the do-statement. */
5727 parser->in_iteration_statement_p = true;
5728 cp_parser_implicitly_scoped_statement (parser);
5729 parser->in_iteration_statement_p = in_iteration_statement_p;
5730 finish_do_body (statement);
5731 /* Look for the `while' keyword. */
5732 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5733 /* Look for the `('. */
5734 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5735 /* Parse the expression. */
5736 expression = cp_parser_expression (parser);
5737 /* We're done with the do-statement. */
5738 finish_do_stmt (expression, statement);
5739 /* Look for the `)'. */
5740 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5741 /* Look for the `;'. */
5742 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5748 tree condition = NULL_TREE;
5749 tree expression = NULL_TREE;
5751 /* Begin the for-statement. */
5752 statement = begin_for_stmt ();
5753 /* Look for the `('. */
5754 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5755 /* Parse the initialization. */
5756 cp_parser_for_init_statement (parser);
5757 finish_for_init_stmt (statement);
5759 /* If there's a condition, process it. */
5760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5761 condition = cp_parser_condition (parser);
5762 finish_for_cond (condition, statement);
5763 /* Look for the `;'. */
5764 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5766 /* If there's an expression, process it. */
5767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5768 expression = cp_parser_expression (parser);
5769 finish_for_expr (expression, statement);
5770 /* Look for the `)'. */
5771 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5773 /* Parse the body of the for-statement. */
5774 parser->in_iteration_statement_p = true;
5775 cp_parser_already_scoped_statement (parser);
5776 parser->in_iteration_statement_p = in_iteration_statement_p;
5778 /* We're done with the for-statement. */
5779 finish_for_stmt (statement);
5784 cp_parser_error (parser, "expected iteration-statement");
5785 statement = error_mark_node;
5792 /* Parse a for-init-statement.
5795 expression-statement
5796 simple-declaration */
5799 cp_parser_for_init_statement (cp_parser* parser)
5801 /* If the next token is a `;', then we have an empty
5802 expression-statement. Grammatically, this is also a
5803 simple-declaration, but an invalid one, because it does not
5804 declare anything. Therefore, if we did not handle this case
5805 specially, we would issue an error message about an invalid
5807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5809 /* We're going to speculatively look for a declaration, falling back
5810 to an expression, if necessary. */
5811 cp_parser_parse_tentatively (parser);
5812 /* Parse the declaration. */
5813 cp_parser_simple_declaration (parser,
5814 /*function_definition_allowed_p=*/false);
5815 /* If the tentative parse failed, then we shall need to look for an
5816 expression-statement. */
5817 if (cp_parser_parse_definitely (parser))
5821 cp_parser_expression_statement (parser, false);
5824 /* Parse a jump-statement.
5829 return expression [opt] ;
5837 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5841 cp_parser_jump_statement (cp_parser* parser)
5843 tree statement = error_mark_node;
5847 /* Peek at the next token. */
5848 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5850 return error_mark_node;
5852 /* See what kind of keyword it is. */
5853 keyword = token->keyword;
5857 if (!parser->in_switch_statement_p
5858 && !parser->in_iteration_statement_p)
5860 error ("break statement not within loop or switch");
5861 statement = error_mark_node;
5864 statement = finish_break_stmt ();
5865 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5869 if (!parser->in_iteration_statement_p)
5871 error ("continue statement not within a loop");
5872 statement = error_mark_node;
5875 statement = finish_continue_stmt ();
5876 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5883 /* If the next token is a `;', then there is no
5885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5886 expr = cp_parser_expression (parser);
5889 /* Build the return-statement. */
5890 statement = finish_return_stmt (expr);
5891 /* Look for the final `;'. */
5892 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5897 /* Create the goto-statement. */
5898 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5900 /* Issue a warning about this use of a GNU extension. */
5902 pedwarn ("ISO C++ forbids computed gotos");
5903 /* Consume the '*' token. */
5904 cp_lexer_consume_token (parser->lexer);
5905 /* Parse the dependent expression. */
5906 finish_goto_stmt (cp_parser_expression (parser));
5909 finish_goto_stmt (cp_parser_identifier (parser));
5910 /* Look for the final `;'. */
5911 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5915 cp_parser_error (parser, "expected jump-statement");
5922 /* Parse a declaration-statement.
5924 declaration-statement:
5925 block-declaration */
5928 cp_parser_declaration_statement (cp_parser* parser)
5930 /* Parse the block-declaration. */
5931 cp_parser_block_declaration (parser, /*statement_p=*/true);
5933 /* Finish off the statement. */
5937 /* Some dependent statements (like `if (cond) statement'), are
5938 implicitly in their own scope. In other words, if the statement is
5939 a single statement (as opposed to a compound-statement), it is
5940 none-the-less treated as if it were enclosed in braces. Any
5941 declarations appearing in the dependent statement are out of scope
5942 after control passes that point. This function parses a statement,
5943 but ensures that is in its own scope, even if it is not a
5946 Returns the new statement. */
5949 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5953 /* If the token is not a `{', then we must take special action. */
5954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5956 /* Create a compound-statement. */
5957 statement = begin_compound_stmt (/*has_no_scope=*/false);
5958 /* Parse the dependent-statement. */
5959 cp_parser_statement (parser, false);
5960 /* Finish the dummy compound-statement. */
5961 finish_compound_stmt (statement);
5963 /* Otherwise, we simply parse the statement directly. */
5965 statement = cp_parser_compound_statement (parser, false);
5967 /* Return the statement. */
5971 /* For some dependent statements (like `while (cond) statement'), we
5972 have already created a scope. Therefore, even if the dependent
5973 statement is a compound-statement, we do not want to create another
5977 cp_parser_already_scoped_statement (cp_parser* parser)
5979 /* If the token is not a `{', then we must take special action. */
5980 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5984 /* Create a compound-statement. */
5985 statement = begin_compound_stmt (/*has_no_scope=*/true);
5986 /* Parse the dependent-statement. */
5987 cp_parser_statement (parser, false);
5988 /* Finish the dummy compound-statement. */
5989 finish_compound_stmt (statement);
5991 /* Otherwise, we simply parse the statement directly. */
5993 cp_parser_statement (parser, false);
5996 /* Declarations [gram.dcl.dcl] */
5998 /* Parse an optional declaration-sequence.
6002 declaration-seq declaration */
6005 cp_parser_declaration_seq_opt (cp_parser* parser)
6011 token = cp_lexer_peek_token (parser->lexer);
6013 if (token->type == CPP_CLOSE_BRACE
6014 || token->type == CPP_EOF)
6017 if (token->type == CPP_SEMICOLON)
6019 /* A declaration consisting of a single semicolon is
6020 invalid. Allow it unless we're being pedantic. */
6022 pedwarn ("extra `;'");
6023 cp_lexer_consume_token (parser->lexer);
6027 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6028 parser to enter or exit implicit `extern "C"' blocks. */
6029 while (pending_lang_change > 0)
6031 push_lang_context (lang_name_c);
6032 --pending_lang_change;
6034 while (pending_lang_change < 0)
6036 pop_lang_context ();
6037 ++pending_lang_change;
6040 /* Parse the declaration itself. */
6041 cp_parser_declaration (parser);
6045 /* Parse a declaration.
6050 template-declaration
6051 explicit-instantiation
6052 explicit-specialization
6053 linkage-specification
6054 namespace-definition
6059 __extension__ declaration */
6062 cp_parser_declaration (cp_parser* parser)
6068 /* Check for the `__extension__' keyword. */
6069 if (cp_parser_extension_opt (parser, &saved_pedantic))
6071 /* Parse the qualified declaration. */
6072 cp_parser_declaration (parser);
6073 /* Restore the PEDANTIC flag. */
6074 pedantic = saved_pedantic;
6079 /* Try to figure out what kind of declaration is present. */
6080 token1 = *cp_lexer_peek_token (parser->lexer);
6081 if (token1.type != CPP_EOF)
6082 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6084 /* If the next token is `extern' and the following token is a string
6085 literal, then we have a linkage specification. */
6086 if (token1.keyword == RID_EXTERN
6087 && cp_parser_is_string_literal (&token2))
6088 cp_parser_linkage_specification (parser);
6089 /* If the next token is `template', then we have either a template
6090 declaration, an explicit instantiation, or an explicit
6092 else if (token1.keyword == RID_TEMPLATE)
6094 /* `template <>' indicates a template specialization. */
6095 if (token2.type == CPP_LESS
6096 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6097 cp_parser_explicit_specialization (parser);
6098 /* `template <' indicates a template declaration. */
6099 else if (token2.type == CPP_LESS)
6100 cp_parser_template_declaration (parser, /*member_p=*/false);
6101 /* Anything else must be an explicit instantiation. */
6103 cp_parser_explicit_instantiation (parser);
6105 /* If the next token is `export', then we have a template
6107 else if (token1.keyword == RID_EXPORT)
6108 cp_parser_template_declaration (parser, /*member_p=*/false);
6109 /* If the next token is `extern', 'static' or 'inline' and the one
6110 after that is `template', we have a GNU extended explicit
6111 instantiation directive. */
6112 else if (cp_parser_allow_gnu_extensions_p (parser)
6113 && (token1.keyword == RID_EXTERN
6114 || token1.keyword == RID_STATIC
6115 || token1.keyword == RID_INLINE)
6116 && token2.keyword == RID_TEMPLATE)
6117 cp_parser_explicit_instantiation (parser);
6118 /* If the next token is `namespace', check for a named or unnamed
6119 namespace definition. */
6120 else if (token1.keyword == RID_NAMESPACE
6121 && (/* A named namespace definition. */
6122 (token2.type == CPP_NAME
6123 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6125 /* An unnamed namespace definition. */
6126 || token2.type == CPP_OPEN_BRACE))
6127 cp_parser_namespace_definition (parser);
6128 /* We must have either a block declaration or a function
6131 /* Try to parse a block-declaration, or a function-definition. */
6132 cp_parser_block_declaration (parser, /*statement_p=*/false);
6135 /* Parse a block-declaration.
6140 namespace-alias-definition
6147 __extension__ block-declaration
6150 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6151 part of a declaration-statement. */
6154 cp_parser_block_declaration (cp_parser *parser,
6160 /* Check for the `__extension__' keyword. */
6161 if (cp_parser_extension_opt (parser, &saved_pedantic))
6163 /* Parse the qualified declaration. */
6164 cp_parser_block_declaration (parser, statement_p);
6165 /* Restore the PEDANTIC flag. */
6166 pedantic = saved_pedantic;
6171 /* Peek at the next token to figure out which kind of declaration is
6173 token1 = cp_lexer_peek_token (parser->lexer);
6175 /* If the next keyword is `asm', we have an asm-definition. */
6176 if (token1->keyword == RID_ASM)
6179 cp_parser_commit_to_tentative_parse (parser);
6180 cp_parser_asm_definition (parser);
6182 /* If the next keyword is `namespace', we have a
6183 namespace-alias-definition. */
6184 else if (token1->keyword == RID_NAMESPACE)
6185 cp_parser_namespace_alias_definition (parser);
6186 /* If the next keyword is `using', we have either a
6187 using-declaration or a using-directive. */
6188 else if (token1->keyword == RID_USING)
6193 cp_parser_commit_to_tentative_parse (parser);
6194 /* If the token after `using' is `namespace', then we have a
6196 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6197 if (token2->keyword == RID_NAMESPACE)
6198 cp_parser_using_directive (parser);
6199 /* Otherwise, it's a using-declaration. */
6201 cp_parser_using_declaration (parser);
6203 /* If the next keyword is `__label__' we have a label declaration. */
6204 else if (token1->keyword == RID_LABEL)
6207 cp_parser_commit_to_tentative_parse (parser);
6208 cp_parser_label_declaration (parser);
6210 /* Anything else must be a simple-declaration. */
6212 cp_parser_simple_declaration (parser, !statement_p);
6215 /* Parse a simple-declaration.
6218 decl-specifier-seq [opt] init-declarator-list [opt] ;
6220 init-declarator-list:
6222 init-declarator-list , init-declarator
6224 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6225 function-definition as a simple-declaration. */
6228 cp_parser_simple_declaration (cp_parser* parser,
6229 bool function_definition_allowed_p)
6231 tree decl_specifiers;
6233 int declares_class_or_enum;
6234 bool saw_declarator;
6236 /* Defer access checks until we know what is being declared; the
6237 checks for names appearing in the decl-specifier-seq should be
6238 done as if we were in the scope of the thing being declared. */
6239 push_deferring_access_checks (dk_deferred);
6241 /* Parse the decl-specifier-seq. We have to keep track of whether
6242 or not the decl-specifier-seq declares a named class or
6243 enumeration type, since that is the only case in which the
6244 init-declarator-list is allowed to be empty.
6248 In a simple-declaration, the optional init-declarator-list can be
6249 omitted only when declaring a class or enumeration, that is when
6250 the decl-specifier-seq contains either a class-specifier, an
6251 elaborated-type-specifier, or an enum-specifier. */
6253 = cp_parser_decl_specifier_seq (parser,
6254 CP_PARSER_FLAGS_OPTIONAL,
6256 &declares_class_or_enum);
6257 /* We no longer need to defer access checks. */
6258 stop_deferring_access_checks ();
6260 /* In a block scope, a valid declaration must always have a
6261 decl-specifier-seq. By not trying to parse declarators, we can
6262 resolve the declaration/expression ambiguity more quickly. */
6263 if (!function_definition_allowed_p && !decl_specifiers)
6265 cp_parser_error (parser, "expected declaration");
6269 /* If the next two tokens are both identifiers, the code is
6270 erroneous. The usual cause of this situation is code like:
6274 where "T" should name a type -- but does not. */
6275 if (cp_parser_diagnose_invalid_type_name (parser))
6277 /* If parsing tentatively, we should commit; we really are
6278 looking at a declaration. */
6279 cp_parser_commit_to_tentative_parse (parser);
6284 /* Keep going until we hit the `;' at the end of the simple
6286 saw_declarator = false;
6287 while (cp_lexer_next_token_is_not (parser->lexer,
6291 bool function_definition_p;
6294 saw_declarator = true;
6295 /* Parse the init-declarator. */
6296 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6297 function_definition_allowed_p,
6299 declares_class_or_enum,
6300 &function_definition_p);
6301 /* If an error occurred while parsing tentatively, exit quickly.
6302 (That usually happens when in the body of a function; each
6303 statement is treated as a declaration-statement until proven
6305 if (cp_parser_error_occurred (parser))
6307 /* Handle function definitions specially. */
6308 if (function_definition_p)
6310 /* If the next token is a `,', then we are probably
6311 processing something like:
6315 which is erroneous. */
6316 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6317 error ("mixing declarations and function-definitions is forbidden");
6318 /* Otherwise, we're done with the list of declarators. */
6321 pop_deferring_access_checks ();
6325 /* The next token should be either a `,' or a `;'. */
6326 token = cp_lexer_peek_token (parser->lexer);
6327 /* If it's a `,', there are more declarators to come. */
6328 if (token->type == CPP_COMMA)
6329 cp_lexer_consume_token (parser->lexer);
6330 /* If it's a `;', we are done. */
6331 else if (token->type == CPP_SEMICOLON)
6333 /* Anything else is an error. */
6336 cp_parser_error (parser, "expected `,' or `;'");
6337 /* Skip tokens until we reach the end of the statement. */
6338 cp_parser_skip_to_end_of_statement (parser);
6341 /* After the first time around, a function-definition is not
6342 allowed -- even if it was OK at first. For example:
6347 function_definition_allowed_p = false;
6350 /* Issue an error message if no declarators are present, and the
6351 decl-specifier-seq does not itself declare a class or
6353 if (!saw_declarator)
6355 if (cp_parser_declares_only_class_p (parser))
6356 shadow_tag (decl_specifiers);
6357 /* Perform any deferred access checks. */
6358 perform_deferred_access_checks ();
6361 /* Consume the `;'. */
6362 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6365 pop_deferring_access_checks ();
6368 /* Parse a decl-specifier-seq.
6371 decl-specifier-seq [opt] decl-specifier
6374 storage-class-specifier
6383 decl-specifier-seq [opt] attributes
6385 Returns a TREE_LIST, giving the decl-specifiers in the order they
6386 appear in the source code. The TREE_VALUE of each node is the
6387 decl-specifier. For a keyword (such as `auto' or `friend'), the
6388 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6389 representation of a type-specifier, see cp_parser_type_specifier.
6391 If there are attributes, they will be stored in *ATTRIBUTES,
6392 represented as described above cp_parser_attributes.
6394 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6395 appears, and the entity that will be a friend is not going to be a
6396 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6397 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6398 friendship is granted might not be a class.
6400 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6403 1: one of the decl-specifiers is an elaborated-type-specifier
6404 2: one of the decl-specifiers is an enum-specifier or a
6410 cp_parser_decl_specifier_seq (cp_parser* parser,
6411 cp_parser_flags flags,
6413 int* declares_class_or_enum)
6415 tree decl_specs = NULL_TREE;
6416 bool friend_p = false;
6417 bool constructor_possible_p = !parser->in_declarator_p;
6419 /* Assume no class or enumeration type is declared. */
6420 *declares_class_or_enum = 0;
6422 /* Assume there are no attributes. */
6423 *attributes = NULL_TREE;
6425 /* Keep reading specifiers until there are no more to read. */
6428 tree decl_spec = NULL_TREE;
6432 /* Peek at the next token. */
6433 token = cp_lexer_peek_token (parser->lexer);
6434 /* Handle attributes. */
6435 if (token->keyword == RID_ATTRIBUTE)
6437 /* Parse the attributes. */
6438 decl_spec = cp_parser_attributes_opt (parser);
6439 /* Add them to the list. */
6440 *attributes = chainon (*attributes, decl_spec);
6443 /* If the next token is an appropriate keyword, we can simply
6444 add it to the list. */
6445 switch (token->keyword)
6451 error ("duplicate `friend'");
6454 /* The representation of the specifier is simply the
6455 appropriate TREE_IDENTIFIER node. */
6456 decl_spec = token->value;
6457 /* Consume the token. */
6458 cp_lexer_consume_token (parser->lexer);
6461 /* function-specifier:
6468 decl_spec = cp_parser_function_specifier_opt (parser);
6474 /* The representation of the specifier is simply the
6475 appropriate TREE_IDENTIFIER node. */
6476 decl_spec = token->value;
6477 /* Consume the token. */
6478 cp_lexer_consume_token (parser->lexer);
6479 /* A constructor declarator cannot appear in a typedef. */
6480 constructor_possible_p = false;
6481 /* The "typedef" keyword can only occur in a declaration; we
6482 may as well commit at this point. */
6483 cp_parser_commit_to_tentative_parse (parser);
6486 /* storage-class-specifier:
6501 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6508 /* Constructors are a special case. The `S' in `S()' is not a
6509 decl-specifier; it is the beginning of the declarator. */
6510 constructor_p = (!decl_spec
6511 && constructor_possible_p
6512 && cp_parser_constructor_declarator_p (parser,
6515 /* If we don't have a DECL_SPEC yet, then we must be looking at
6516 a type-specifier. */
6517 if (!decl_spec && !constructor_p)
6519 int decl_spec_declares_class_or_enum;
6520 bool is_cv_qualifier;
6523 = cp_parser_type_specifier (parser, flags,
6525 /*is_declaration=*/true,
6526 &decl_spec_declares_class_or_enum,
6529 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6531 /* If this type-specifier referenced a user-defined type
6532 (a typedef, class-name, etc.), then we can't allow any
6533 more such type-specifiers henceforth.
6537 The longest sequence of decl-specifiers that could
6538 possibly be a type name is taken as the
6539 decl-specifier-seq of a declaration. The sequence shall
6540 be self-consistent as described below.
6544 As a general rule, at most one type-specifier is allowed
6545 in the complete decl-specifier-seq of a declaration. The
6546 only exceptions are the following:
6548 -- const or volatile can be combined with any other
6551 -- signed or unsigned can be combined with char, long,
6559 void g (const int Pc);
6561 Here, Pc is *not* part of the decl-specifier seq; it's
6562 the declarator. Therefore, once we see a type-specifier
6563 (other than a cv-qualifier), we forbid any additional
6564 user-defined types. We *do* still allow things like `int
6565 int' to be considered a decl-specifier-seq, and issue the
6566 error message later. */
6567 if (decl_spec && !is_cv_qualifier)
6568 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6569 /* A constructor declarator cannot follow a type-specifier. */
6571 constructor_possible_p = false;
6574 /* If we still do not have a DECL_SPEC, then there are no more
6578 /* Issue an error message, unless the entire construct was
6580 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6582 cp_parser_error (parser, "expected decl specifier");
6583 return error_mark_node;
6589 /* Add the DECL_SPEC to the list of specifiers. */
6590 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6592 /* After we see one decl-specifier, further decl-specifiers are
6594 flags |= CP_PARSER_FLAGS_OPTIONAL;
6597 /* We have built up the DECL_SPECS in reverse order. Return them in
6598 the correct order. */
6599 return nreverse (decl_specs);
6602 /* Parse an (optional) storage-class-specifier.
6604 storage-class-specifier:
6613 storage-class-specifier:
6616 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6619 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6621 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6629 /* Consume the token. */
6630 return cp_lexer_consume_token (parser->lexer)->value;
6637 /* Parse an (optional) function-specifier.
6644 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6647 cp_parser_function_specifier_opt (cp_parser* parser)
6649 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6654 /* Consume the token. */
6655 return cp_lexer_consume_token (parser->lexer)->value;
6662 /* Parse a linkage-specification.
6664 linkage-specification:
6665 extern string-literal { declaration-seq [opt] }
6666 extern string-literal declaration */
6669 cp_parser_linkage_specification (cp_parser* parser)
6674 /* Look for the `extern' keyword. */
6675 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6677 /* Peek at the next token. */
6678 token = cp_lexer_peek_token (parser->lexer);
6679 /* If it's not a string-literal, then there's a problem. */
6680 if (!cp_parser_is_string_literal (token))
6682 cp_parser_error (parser, "expected language-name");
6685 /* Consume the token. */
6686 cp_lexer_consume_token (parser->lexer);
6688 /* Transform the literal into an identifier. If the literal is a
6689 wide-character string, or contains embedded NULs, then we can't
6690 handle it as the user wants. */
6691 if (token->type == CPP_WSTRING
6692 || (strlen (TREE_STRING_POINTER (token->value))
6693 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6695 cp_parser_error (parser, "invalid linkage-specification");
6696 /* Assume C++ linkage. */
6697 linkage = get_identifier ("c++");
6699 /* If it's a simple string constant, things are easier. */
6701 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6703 /* We're now using the new linkage. */
6704 push_lang_context (linkage);
6706 /* If the next token is a `{', then we're using the first
6708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6710 /* Consume the `{' token. */
6711 cp_lexer_consume_token (parser->lexer);
6712 /* Parse the declarations. */
6713 cp_parser_declaration_seq_opt (parser);
6714 /* Look for the closing `}'. */
6715 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6717 /* Otherwise, there's just one declaration. */
6720 bool saved_in_unbraced_linkage_specification_p;
6722 saved_in_unbraced_linkage_specification_p
6723 = parser->in_unbraced_linkage_specification_p;
6724 parser->in_unbraced_linkage_specification_p = true;
6725 have_extern_spec = true;
6726 cp_parser_declaration (parser);
6727 have_extern_spec = false;
6728 parser->in_unbraced_linkage_specification_p
6729 = saved_in_unbraced_linkage_specification_p;
6732 /* We're done with the linkage-specification. */
6733 pop_lang_context ();
6736 /* Special member functions [gram.special] */
6738 /* Parse a conversion-function-id.
6740 conversion-function-id:
6741 operator conversion-type-id
6743 Returns an IDENTIFIER_NODE representing the operator. */
6746 cp_parser_conversion_function_id (cp_parser* parser)
6750 tree saved_qualifying_scope;
6751 tree saved_object_scope;
6753 /* Look for the `operator' token. */
6754 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6755 return error_mark_node;
6756 /* When we parse the conversion-type-id, the current scope will be
6757 reset. However, we need that information in able to look up the
6758 conversion function later, so we save it here. */
6759 saved_scope = parser->scope;
6760 saved_qualifying_scope = parser->qualifying_scope;
6761 saved_object_scope = parser->object_scope;
6762 /* We must enter the scope of the class so that the names of
6763 entities declared within the class are available in the
6764 conversion-type-id. For example, consider:
6771 S::operator I() { ... }
6773 In order to see that `I' is a type-name in the definition, we
6774 must be in the scope of `S'. */
6776 push_scope (saved_scope);
6777 /* Parse the conversion-type-id. */
6778 type = cp_parser_conversion_type_id (parser);
6779 /* Leave the scope of the class, if any. */
6781 pop_scope (saved_scope);
6782 /* Restore the saved scope. */
6783 parser->scope = saved_scope;
6784 parser->qualifying_scope = saved_qualifying_scope;
6785 parser->object_scope = saved_object_scope;
6786 /* If the TYPE is invalid, indicate failure. */
6787 if (type == error_mark_node)
6788 return error_mark_node;
6789 return mangle_conv_op_name_for_type (type);
6792 /* Parse a conversion-type-id:
6795 type-specifier-seq conversion-declarator [opt]
6797 Returns the TYPE specified. */
6800 cp_parser_conversion_type_id (cp_parser* parser)
6803 tree type_specifiers;
6806 /* Parse the attributes. */
6807 attributes = cp_parser_attributes_opt (parser);
6808 /* Parse the type-specifiers. */
6809 type_specifiers = cp_parser_type_specifier_seq (parser);
6810 /* If that didn't work, stop. */
6811 if (type_specifiers == error_mark_node)
6812 return error_mark_node;
6813 /* Parse the conversion-declarator. */
6814 declarator = cp_parser_conversion_declarator_opt (parser);
6816 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6817 /*initialized=*/0, &attributes);
6820 /* Parse an (optional) conversion-declarator.
6822 conversion-declarator:
6823 ptr-operator conversion-declarator [opt]
6825 Returns a representation of the declarator. See
6826 cp_parser_declarator for details. */
6829 cp_parser_conversion_declarator_opt (cp_parser* parser)
6831 enum tree_code code;
6833 tree cv_qualifier_seq;
6835 /* We don't know if there's a ptr-operator next, or not. */
6836 cp_parser_parse_tentatively (parser);
6837 /* Try the ptr-operator. */
6838 code = cp_parser_ptr_operator (parser, &class_type,
6840 /* If it worked, look for more conversion-declarators. */
6841 if (cp_parser_parse_definitely (parser))
6845 /* Parse another optional declarator. */
6846 declarator = cp_parser_conversion_declarator_opt (parser);
6848 /* Create the representation of the declarator. */
6849 if (code == INDIRECT_REF)
6850 declarator = make_pointer_declarator (cv_qualifier_seq,
6853 declarator = make_reference_declarator (cv_qualifier_seq,
6856 /* Handle the pointer-to-member case. */
6858 declarator = build_nt (SCOPE_REF, class_type, declarator);
6866 /* Parse an (optional) ctor-initializer.
6869 : mem-initializer-list
6871 Returns TRUE iff the ctor-initializer was actually present. */
6874 cp_parser_ctor_initializer_opt (cp_parser* parser)
6876 /* If the next token is not a `:', then there is no
6877 ctor-initializer. */
6878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6880 /* Do default initialization of any bases and members. */
6881 if (DECL_CONSTRUCTOR_P (current_function_decl))
6882 finish_mem_initializers (NULL_TREE);
6887 /* Consume the `:' token. */
6888 cp_lexer_consume_token (parser->lexer);
6889 /* And the mem-initializer-list. */
6890 cp_parser_mem_initializer_list (parser);
6895 /* Parse a mem-initializer-list.
6897 mem-initializer-list:
6899 mem-initializer , mem-initializer-list */
6902 cp_parser_mem_initializer_list (cp_parser* parser)
6904 tree mem_initializer_list = NULL_TREE;
6906 /* Let the semantic analysis code know that we are starting the
6907 mem-initializer-list. */
6908 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6909 error ("only constructors take base initializers");
6911 /* Loop through the list. */
6914 tree mem_initializer;
6916 /* Parse the mem-initializer. */
6917 mem_initializer = cp_parser_mem_initializer (parser);
6918 /* Add it to the list, unless it was erroneous. */
6919 if (mem_initializer)
6921 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6922 mem_initializer_list = mem_initializer;
6924 /* If the next token is not a `,', we're done. */
6925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6927 /* Consume the `,' token. */
6928 cp_lexer_consume_token (parser->lexer);
6931 /* Perform semantic analysis. */
6932 if (DECL_CONSTRUCTOR_P (current_function_decl))
6933 finish_mem_initializers (mem_initializer_list);
6936 /* Parse a mem-initializer.
6939 mem-initializer-id ( expression-list [opt] )
6944 ( expression-list [opt] )
6946 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6947 class) or FIELD_DECL (for a non-static data member) to initialize;
6948 the TREE_VALUE is the expression-list. */
6951 cp_parser_mem_initializer (cp_parser* parser)
6953 tree mem_initializer_id;
6954 tree expression_list;
6957 /* Find out what is being initialized. */
6958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6960 pedwarn ("anachronistic old-style base class initializer");
6961 mem_initializer_id = NULL_TREE;
6964 mem_initializer_id = cp_parser_mem_initializer_id (parser);
6965 member = expand_member_init (mem_initializer_id);
6966 if (member && !DECL_P (member))
6967 in_base_initializer = 1;
6970 = cp_parser_parenthesized_expression_list (parser, false,
6971 /*non_constant_p=*/NULL);
6972 if (!expression_list)
6973 expression_list = void_type_node;
6975 in_base_initializer = 0;
6977 return member ? build_tree_list (member, expression_list) : NULL_TREE;
6980 /* Parse a mem-initializer-id.
6983 :: [opt] nested-name-specifier [opt] class-name
6986 Returns a TYPE indicating the class to be initializer for the first
6987 production. Returns an IDENTIFIER_NODE indicating the data member
6988 to be initialized for the second production. */
6991 cp_parser_mem_initializer_id (cp_parser* parser)
6993 bool global_scope_p;
6994 bool nested_name_specifier_p;
6997 /* Look for the optional `::' operator. */
6999 = (cp_parser_global_scope_opt (parser,
7000 /*current_scope_valid_p=*/false)
7002 /* Look for the optional nested-name-specifier. The simplest way to
7007 The keyword `typename' is not permitted in a base-specifier or
7008 mem-initializer; in these contexts a qualified name that
7009 depends on a template-parameter is implicitly assumed to be a
7012 is to assume that we have seen the `typename' keyword at this
7014 nested_name_specifier_p
7015 = (cp_parser_nested_name_specifier_opt (parser,
7016 /*typename_keyword_p=*/true,
7017 /*check_dependency_p=*/true,
7019 /*is_declaration=*/true)
7021 /* If there is a `::' operator or a nested-name-specifier, then we
7022 are definitely looking for a class-name. */
7023 if (global_scope_p || nested_name_specifier_p)
7024 return cp_parser_class_name (parser,
7025 /*typename_keyword_p=*/true,
7026 /*template_keyword_p=*/false,
7028 /*check_dependency_p=*/true,
7029 /*class_head_p=*/false,
7030 /*is_declaration=*/true);
7031 /* Otherwise, we could also be looking for an ordinary identifier. */
7032 cp_parser_parse_tentatively (parser);
7033 /* Try a class-name. */
7034 id = cp_parser_class_name (parser,
7035 /*typename_keyword_p=*/true,
7036 /*template_keyword_p=*/false,
7038 /*check_dependency_p=*/true,
7039 /*class_head_p=*/false,
7040 /*is_declaration=*/true);
7041 /* If we found one, we're done. */
7042 if (cp_parser_parse_definitely (parser))
7044 /* Otherwise, look for an ordinary identifier. */
7045 return cp_parser_identifier (parser);
7048 /* Overloading [gram.over] */
7050 /* Parse an operator-function-id.
7052 operator-function-id:
7055 Returns an IDENTIFIER_NODE for the operator which is a
7056 human-readable spelling of the identifier, e.g., `operator +'. */
7059 cp_parser_operator_function_id (cp_parser* parser)
7061 /* Look for the `operator' keyword. */
7062 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7063 return error_mark_node;
7064 /* And then the name of the operator itself. */
7065 return cp_parser_operator (parser);
7068 /* Parse an operator.
7071 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7072 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7073 || ++ -- , ->* -> () []
7080 Returns an IDENTIFIER_NODE for the operator which is a
7081 human-readable spelling of the identifier, e.g., `operator +'. */
7084 cp_parser_operator (cp_parser* parser)
7086 tree id = NULL_TREE;
7089 /* Peek at the next token. */
7090 token = cp_lexer_peek_token (parser->lexer);
7091 /* Figure out which operator we have. */
7092 switch (token->type)
7098 /* The keyword should be either `new' or `delete'. */
7099 if (token->keyword == RID_NEW)
7101 else if (token->keyword == RID_DELETE)
7106 /* Consume the `new' or `delete' token. */
7107 cp_lexer_consume_token (parser->lexer);
7109 /* Peek at the next token. */
7110 token = cp_lexer_peek_token (parser->lexer);
7111 /* If it's a `[' token then this is the array variant of the
7113 if (token->type == CPP_OPEN_SQUARE)
7115 /* Consume the `[' token. */
7116 cp_lexer_consume_token (parser->lexer);
7117 /* Look for the `]' token. */
7118 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7119 id = ansi_opname (op == NEW_EXPR
7120 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7122 /* Otherwise, we have the non-array variant. */
7124 id = ansi_opname (op);
7130 id = ansi_opname (PLUS_EXPR);
7134 id = ansi_opname (MINUS_EXPR);
7138 id = ansi_opname (MULT_EXPR);
7142 id = ansi_opname (TRUNC_DIV_EXPR);
7146 id = ansi_opname (TRUNC_MOD_EXPR);
7150 id = ansi_opname (BIT_XOR_EXPR);
7154 id = ansi_opname (BIT_AND_EXPR);
7158 id = ansi_opname (BIT_IOR_EXPR);
7162 id = ansi_opname (BIT_NOT_EXPR);
7166 id = ansi_opname (TRUTH_NOT_EXPR);
7170 id = ansi_assopname (NOP_EXPR);
7174 id = ansi_opname (LT_EXPR);
7178 id = ansi_opname (GT_EXPR);
7182 id = ansi_assopname (PLUS_EXPR);
7186 id = ansi_assopname (MINUS_EXPR);
7190 id = ansi_assopname (MULT_EXPR);
7194 id = ansi_assopname (TRUNC_DIV_EXPR);
7198 id = ansi_assopname (TRUNC_MOD_EXPR);
7202 id = ansi_assopname (BIT_XOR_EXPR);
7206 id = ansi_assopname (BIT_AND_EXPR);
7210 id = ansi_assopname (BIT_IOR_EXPR);
7214 id = ansi_opname (LSHIFT_EXPR);
7218 id = ansi_opname (RSHIFT_EXPR);
7222 id = ansi_assopname (LSHIFT_EXPR);
7226 id = ansi_assopname (RSHIFT_EXPR);
7230 id = ansi_opname (EQ_EXPR);
7234 id = ansi_opname (NE_EXPR);
7238 id = ansi_opname (LE_EXPR);
7241 case CPP_GREATER_EQ:
7242 id = ansi_opname (GE_EXPR);
7246 id = ansi_opname (TRUTH_ANDIF_EXPR);
7250 id = ansi_opname (TRUTH_ORIF_EXPR);
7254 id = ansi_opname (POSTINCREMENT_EXPR);
7257 case CPP_MINUS_MINUS:
7258 id = ansi_opname (PREDECREMENT_EXPR);
7262 id = ansi_opname (COMPOUND_EXPR);
7265 case CPP_DEREF_STAR:
7266 id = ansi_opname (MEMBER_REF);
7270 id = ansi_opname (COMPONENT_REF);
7273 case CPP_OPEN_PAREN:
7274 /* Consume the `('. */
7275 cp_lexer_consume_token (parser->lexer);
7276 /* Look for the matching `)'. */
7277 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7278 return ansi_opname (CALL_EXPR);
7280 case CPP_OPEN_SQUARE:
7281 /* Consume the `['. */
7282 cp_lexer_consume_token (parser->lexer);
7283 /* Look for the matching `]'. */
7284 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7285 return ansi_opname (ARRAY_REF);
7289 id = ansi_opname (MIN_EXPR);
7293 id = ansi_opname (MAX_EXPR);
7297 id = ansi_assopname (MIN_EXPR);
7301 id = ansi_assopname (MAX_EXPR);
7305 /* Anything else is an error. */
7309 /* If we have selected an identifier, we need to consume the
7312 cp_lexer_consume_token (parser->lexer);
7313 /* Otherwise, no valid operator name was present. */
7316 cp_parser_error (parser, "expected operator");
7317 id = error_mark_node;
7323 /* Parse a template-declaration.
7325 template-declaration:
7326 export [opt] template < template-parameter-list > declaration
7328 If MEMBER_P is TRUE, this template-declaration occurs within a
7331 The grammar rule given by the standard isn't correct. What
7334 template-declaration:
7335 export [opt] template-parameter-list-seq
7336 decl-specifier-seq [opt] init-declarator [opt] ;
7337 export [opt] template-parameter-list-seq
7340 template-parameter-list-seq:
7341 template-parameter-list-seq [opt]
7342 template < template-parameter-list > */
7345 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7347 /* Check for `export'. */
7348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7350 /* Consume the `export' token. */
7351 cp_lexer_consume_token (parser->lexer);
7352 /* Warn that we do not support `export'. */
7353 warning ("keyword `export' not implemented, and will be ignored");
7356 cp_parser_template_declaration_after_export (parser, member_p);
7359 /* Parse a template-parameter-list.
7361 template-parameter-list:
7363 template-parameter-list , template-parameter
7365 Returns a TREE_LIST. Each node represents a template parameter.
7366 The nodes are connected via their TREE_CHAINs. */
7369 cp_parser_template_parameter_list (cp_parser* parser)
7371 tree parameter_list = NULL_TREE;
7378 /* Parse the template-parameter. */
7379 parameter = cp_parser_template_parameter (parser);
7380 /* Add it to the list. */
7381 parameter_list = process_template_parm (parameter_list,
7384 /* Peek at the next token. */
7385 token = cp_lexer_peek_token (parser->lexer);
7386 /* If it's not a `,', we're done. */
7387 if (token->type != CPP_COMMA)
7389 /* Otherwise, consume the `,' token. */
7390 cp_lexer_consume_token (parser->lexer);
7393 return parameter_list;
7396 /* Parse a template-parameter.
7400 parameter-declaration
7402 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7403 TREE_PURPOSE is the default value, if any. */
7406 cp_parser_template_parameter (cp_parser* parser)
7410 /* Peek at the next token. */
7411 token = cp_lexer_peek_token (parser->lexer);
7412 /* If it is `class' or `template', we have a type-parameter. */
7413 if (token->keyword == RID_TEMPLATE)
7414 return cp_parser_type_parameter (parser);
7415 /* If it is `class' or `typename' we do not know yet whether it is a
7416 type parameter or a non-type parameter. Consider:
7418 template <typename T, typename T::X X> ...
7422 template <class C, class D*> ...
7424 Here, the first parameter is a type parameter, and the second is
7425 a non-type parameter. We can tell by looking at the token after
7426 the identifier -- if it is a `,', `=', or `>' then we have a type
7428 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7430 /* Peek at the token after `class' or `typename'. */
7431 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7432 /* If it's an identifier, skip it. */
7433 if (token->type == CPP_NAME)
7434 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7435 /* Now, see if the token looks like the end of a template
7437 if (token->type == CPP_COMMA
7438 || token->type == CPP_EQ
7439 || token->type == CPP_GREATER)
7440 return cp_parser_type_parameter (parser);
7443 /* Otherwise, it is a non-type parameter.
7447 When parsing a default template-argument for a non-type
7448 template-parameter, the first non-nested `>' is taken as the end
7449 of the template parameter-list rather than a greater-than
7452 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7455 /* Parse a type-parameter.
7458 class identifier [opt]
7459 class identifier [opt] = type-id
7460 typename identifier [opt]
7461 typename identifier [opt] = type-id
7462 template < template-parameter-list > class identifier [opt]
7463 template < template-parameter-list > class identifier [opt]
7466 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7467 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7468 the declaration of the parameter. */
7471 cp_parser_type_parameter (cp_parser* parser)
7476 /* Look for a keyword to tell us what kind of parameter this is. */
7477 token = cp_parser_require (parser, CPP_KEYWORD,
7478 "`class', `typename', or `template'");
7480 return error_mark_node;
7482 switch (token->keyword)
7488 tree default_argument;
7490 /* If the next token is an identifier, then it names the
7492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7493 identifier = cp_parser_identifier (parser);
7495 identifier = NULL_TREE;
7497 /* Create the parameter. */
7498 parameter = finish_template_type_parm (class_type_node, identifier);
7500 /* If the next token is an `=', we have a default argument. */
7501 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7503 /* Consume the `=' token. */
7504 cp_lexer_consume_token (parser->lexer);
7505 /* Parse the default-argument. */
7506 default_argument = cp_parser_type_id (parser);
7509 default_argument = NULL_TREE;
7511 /* Create the combined representation of the parameter and the
7512 default argument. */
7513 parameter = build_tree_list (default_argument, parameter);
7519 tree parameter_list;
7521 tree default_argument;
7523 /* Look for the `<'. */
7524 cp_parser_require (parser, CPP_LESS, "`<'");
7525 /* Parse the template-parameter-list. */
7526 begin_template_parm_list ();
7528 = cp_parser_template_parameter_list (parser);
7529 parameter_list = end_template_parm_list (parameter_list);
7530 /* Look for the `>'. */
7531 cp_parser_require (parser, CPP_GREATER, "`>'");
7532 /* Look for the `class' keyword. */
7533 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7534 /* If the next token is an `=', then there is a
7535 default-argument. If the next token is a `>', we are at
7536 the end of the parameter-list. If the next token is a `,',
7537 then we are at the end of this parameter. */
7538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7539 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7540 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7541 identifier = cp_parser_identifier (parser);
7543 identifier = NULL_TREE;
7544 /* Create the template parameter. */
7545 parameter = finish_template_template_parm (class_type_node,
7548 /* If the next token is an `=', then there is a
7549 default-argument. */
7550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7552 /* Consume the `='. */
7553 cp_lexer_consume_token (parser->lexer);
7554 /* Parse the id-expression. */
7556 = cp_parser_id_expression (parser,
7557 /*template_keyword_p=*/false,
7558 /*check_dependency_p=*/true,
7559 /*template_p=*/NULL,
7560 /*declarator_p=*/false);
7561 /* Look up the name. */
7563 = cp_parser_lookup_name_simple (parser, default_argument);
7564 /* See if the default argument is valid. */
7566 = check_template_template_default_arg (default_argument);
7569 default_argument = NULL_TREE;
7571 /* Create the combined representation of the parameter and the
7572 default argument. */
7573 parameter = build_tree_list (default_argument, parameter);
7578 /* Anything else is an error. */
7579 cp_parser_error (parser,
7580 "expected `class', `typename', or `template'");
7581 parameter = error_mark_node;
7587 /* Parse a template-id.
7590 template-name < template-argument-list [opt] >
7592 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7593 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7594 returned. Otherwise, if the template-name names a function, or set
7595 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7596 names a class, returns a TYPE_DECL for the specialization.
7598 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7599 uninstantiated templates. */
7602 cp_parser_template_id (cp_parser *parser,
7603 bool template_keyword_p,
7604 bool check_dependency_p,
7605 bool is_declaration)
7610 ptrdiff_t start_of_id;
7611 tree access_check = NULL_TREE;
7612 cp_token *next_token;
7615 /* If the next token corresponds to a template-id, there is no need
7617 next_token = cp_lexer_peek_token (parser->lexer);
7618 if (next_token->type == CPP_TEMPLATE_ID)
7623 /* Get the stored value. */
7624 value = cp_lexer_consume_token (parser->lexer)->value;
7625 /* Perform any access checks that were deferred. */
7626 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7627 perform_or_defer_access_check (TREE_PURPOSE (check),
7628 TREE_VALUE (check));
7629 /* Return the stored value. */
7630 return TREE_VALUE (value);
7633 /* Avoid performing name lookup if there is no possibility of
7634 finding a template-id. */
7635 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7636 || (next_token->type == CPP_NAME
7637 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7639 cp_parser_error (parser, "expected template-id");
7640 return error_mark_node;
7643 /* Remember where the template-id starts. */
7644 if (cp_parser_parsing_tentatively (parser)
7645 && !cp_parser_committed_to_tentative_parse (parser))
7647 next_token = cp_lexer_peek_token (parser->lexer);
7648 start_of_id = cp_lexer_token_difference (parser->lexer,
7649 parser->lexer->first_token,
7655 push_deferring_access_checks (dk_deferred);
7657 /* Parse the template-name. */
7658 is_identifier = false;
7659 template = cp_parser_template_name (parser, template_keyword_p,
7663 if (template == error_mark_node || is_identifier)
7665 pop_deferring_access_checks ();
7669 /* Look for the `<' that starts the template-argument-list. */
7670 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7672 pop_deferring_access_checks ();
7673 return error_mark_node;
7676 /* Parse the arguments. */
7677 arguments = cp_parser_enclosed_template_argument_list (parser);
7679 /* Build a representation of the specialization. */
7680 if (TREE_CODE (template) == IDENTIFIER_NODE)
7681 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7682 else if (DECL_CLASS_TEMPLATE_P (template)
7683 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7685 = finish_template_type (template, arguments,
7686 cp_lexer_next_token_is (parser->lexer,
7690 /* If it's not a class-template or a template-template, it should be
7691 a function-template. */
7692 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7693 || TREE_CODE (template) == OVERLOAD
7694 || BASELINK_P (template)),
7697 template_id = lookup_template_function (template, arguments);
7700 /* Retrieve any deferred checks. Do not pop this access checks yet
7701 so the memory will not be reclaimed during token replacing below. */
7702 access_check = get_deferred_access_checks ();
7704 /* If parsing tentatively, replace the sequence of tokens that makes
7705 up the template-id with a CPP_TEMPLATE_ID token. That way,
7706 should we re-parse the token stream, we will not have to repeat
7707 the effort required to do the parse, nor will we issue duplicate
7708 error messages about problems during instantiation of the
7710 if (start_of_id >= 0)
7714 /* Find the token that corresponds to the start of the
7716 token = cp_lexer_advance_token (parser->lexer,
7717 parser->lexer->first_token,
7720 /* Reset the contents of the START_OF_ID token. */
7721 token->type = CPP_TEMPLATE_ID;
7722 token->value = build_tree_list (access_check, template_id);
7723 token->keyword = RID_MAX;
7724 /* Purge all subsequent tokens. */
7725 cp_lexer_purge_tokens_after (parser->lexer, token);
7728 pop_deferring_access_checks ();
7732 /* Parse a template-name.
7737 The standard should actually say:
7741 operator-function-id
7742 conversion-function-id
7744 A defect report has been filed about this issue.
7746 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7747 `template' keyword, in a construction like:
7751 In that case `f' is taken to be a template-name, even though there
7752 is no way of knowing for sure.
7754 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7755 name refers to a set of overloaded functions, at least one of which
7756 is a template, or an IDENTIFIER_NODE with the name of the template,
7757 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7758 names are looked up inside uninstantiated templates. */
7761 cp_parser_template_name (cp_parser* parser,
7762 bool template_keyword_p,
7763 bool check_dependency_p,
7764 bool is_declaration,
7765 bool *is_identifier)
7771 /* If the next token is `operator', then we have either an
7772 operator-function-id or a conversion-function-id. */
7773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7775 /* We don't know whether we're looking at an
7776 operator-function-id or a conversion-function-id. */
7777 cp_parser_parse_tentatively (parser);
7778 /* Try an operator-function-id. */
7779 identifier = cp_parser_operator_function_id (parser);
7780 /* If that didn't work, try a conversion-function-id. */
7781 if (!cp_parser_parse_definitely (parser))
7782 identifier = cp_parser_conversion_function_id (parser);
7784 /* Look for the identifier. */
7786 identifier = cp_parser_identifier (parser);
7788 /* If we didn't find an identifier, we don't have a template-id. */
7789 if (identifier == error_mark_node)
7790 return error_mark_node;
7792 /* If the name immediately followed the `template' keyword, then it
7793 is a template-name. However, if the next token is not `<', then
7794 we do not treat it as a template-name, since it is not being used
7795 as part of a template-id. This enables us to handle constructs
7798 template <typename T> struct S { S(); };
7799 template <typename T> S<T>::S();
7801 correctly. We would treat `S' as a template -- if it were `S<T>'
7802 -- but we do not if there is no `<'. */
7804 if (processing_template_decl
7805 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7807 /* In a declaration, in a dependent context, we pretend that the
7808 "template" keyword was present in order to improve error
7809 recovery. For example, given:
7811 template <typename T> void f(T::X<int>);
7813 we want to treat "X<int>" as a template-id. */
7815 && !template_keyword_p
7816 && parser->scope && TYPE_P (parser->scope)
7817 && dependent_type_p (parser->scope))
7821 /* Explain what went wrong. */
7822 error ("non-template `%D' used as template", identifier);
7823 error ("(use `%T::template %D' to indicate that it is a template)",
7824 parser->scope, identifier);
7825 /* If parsing tentatively, find the location of the "<"
7827 if (cp_parser_parsing_tentatively (parser)
7828 && !cp_parser_committed_to_tentative_parse (parser))
7830 cp_parser_simulate_error (parser);
7831 token = cp_lexer_peek_token (parser->lexer);
7832 token = cp_lexer_prev_token (parser->lexer, token);
7833 start = cp_lexer_token_difference (parser->lexer,
7834 parser->lexer->first_token,
7839 /* Parse the template arguments so that we can issue error
7840 messages about them. */
7841 cp_lexer_consume_token (parser->lexer);
7842 cp_parser_enclosed_template_argument_list (parser);
7843 /* Skip tokens until we find a good place from which to
7844 continue parsing. */
7845 cp_parser_skip_to_closing_parenthesis (parser,
7846 /*recovering=*/true,
7848 /*consume_paren=*/false);
7849 /* If parsing tentatively, permanently remove the
7850 template argument list. That will prevent duplicate
7851 error messages from being issued about the missing
7852 "template" keyword. */
7855 token = cp_lexer_advance_token (parser->lexer,
7856 parser->lexer->first_token,
7858 cp_lexer_purge_tokens_after (parser->lexer, token);
7861 *is_identifier = true;
7864 if (template_keyword_p)
7868 /* Look up the name. */
7869 decl = cp_parser_lookup_name (parser, identifier,
7871 /*is_namespace=*/false,
7872 check_dependency_p);
7873 decl = maybe_get_template_decl_from_type_decl (decl);
7875 /* If DECL is a template, then the name was a template-name. */
7876 if (TREE_CODE (decl) == TEMPLATE_DECL)
7880 /* The standard does not explicitly indicate whether a name that
7881 names a set of overloaded declarations, some of which are
7882 templates, is a template-name. However, such a name should
7883 be a template-name; otherwise, there is no way to form a
7884 template-id for the overloaded templates. */
7885 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7886 if (TREE_CODE (fns) == OVERLOAD)
7890 for (fn = fns; fn; fn = OVL_NEXT (fn))
7891 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7896 /* Otherwise, the name does not name a template. */
7897 cp_parser_error (parser, "expected template-name");
7898 return error_mark_node;
7902 /* If DECL is dependent, and refers to a function, then just return
7903 its name; we will look it up again during template instantiation. */
7904 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7906 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7907 if (TYPE_P (scope) && dependent_type_p (scope))
7914 /* Parse a template-argument-list.
7916 template-argument-list:
7918 template-argument-list , template-argument
7920 Returns a TREE_VEC containing the arguments. */
7923 cp_parser_template_argument_list (cp_parser* parser)
7925 tree fixed_args[10];
7926 unsigned n_args = 0;
7927 unsigned alloced = 10;
7928 tree *arg_ary = fixed_args;
7936 /* Consume the comma. */
7937 cp_lexer_consume_token (parser->lexer);
7939 /* Parse the template-argument. */
7940 argument = cp_parser_template_argument (parser);
7941 if (n_args == alloced)
7945 if (arg_ary == fixed_args)
7947 arg_ary = xmalloc (sizeof (tree) * alloced);
7948 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7951 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7953 arg_ary[n_args++] = argument;
7955 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7957 vec = make_tree_vec (n_args);
7960 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7962 if (arg_ary != fixed_args)
7967 /* Parse a template-argument.
7970 assignment-expression
7974 The representation is that of an assignment-expression, type-id, or
7975 id-expression -- except that the qualified id-expression is
7976 evaluated, so that the value returned is either a DECL or an
7979 Although the standard says "assignment-expression", it forbids
7980 throw-expressions or assignments in the template argument.
7981 Therefore, we use "conditional-expression" instead. */
7984 cp_parser_template_argument (cp_parser* parser)
7991 tree qualifying_class;
7993 /* There's really no way to know what we're looking at, so we just
7994 try each alternative in order.
7998 In a template-argument, an ambiguity between a type-id and an
7999 expression is resolved to a type-id, regardless of the form of
8000 the corresponding template-parameter.
8002 Therefore, we try a type-id first. */
8003 cp_parser_parse_tentatively (parser);
8004 argument = cp_parser_type_id (parser);
8005 /* If the next token isn't a `,' or a `>', then this argument wasn't
8007 if (!cp_parser_next_token_ends_template_argument_p (parser))
8008 cp_parser_error (parser, "expected template-argument");
8009 /* If that worked, we're done. */
8010 if (cp_parser_parse_definitely (parser))
8012 /* We're still not sure what the argument will be. */
8013 cp_parser_parse_tentatively (parser);
8014 /* Try a template. */
8015 argument = cp_parser_id_expression (parser,
8016 /*template_keyword_p=*/false,
8017 /*check_dependency_p=*/true,
8019 /*declarator_p=*/false);
8020 /* If the next token isn't a `,' or a `>', then this argument wasn't
8022 if (!cp_parser_next_token_ends_template_argument_p (parser))
8023 cp_parser_error (parser, "expected template-argument");
8024 if (!cp_parser_error_occurred (parser))
8026 /* Figure out what is being referred to. */
8027 argument = cp_parser_lookup_name_simple (parser, argument);
8029 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8030 TREE_OPERAND (argument, 1),
8032 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8033 cp_parser_error (parser, "expected template-name");
8035 if (cp_parser_parse_definitely (parser))
8037 /* It must be a non-type argument. There permitted cases are given
8038 in [temp.arg.nontype]:
8040 -- an integral constant-expression of integral or enumeration
8043 -- the name of a non-type template-parameter; or
8045 -- the name of an object or function with external linkage...
8047 -- the address of an object or function with external linkage...
8049 -- a pointer to member... */
8050 /* Look for a non-type template parameter. */
8051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8053 cp_parser_parse_tentatively (parser);
8054 argument = cp_parser_primary_expression (parser,
8057 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8058 || !cp_parser_next_token_ends_template_argument_p (parser))
8059 cp_parser_simulate_error (parser);
8060 if (cp_parser_parse_definitely (parser))
8063 /* If the next token is "&", the argument must be the address of an
8064 object or function with external linkage. */
8065 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8067 cp_lexer_consume_token (parser->lexer);
8068 /* See if we might have an id-expression. */
8069 token = cp_lexer_peek_token (parser->lexer);
8070 if (token->type == CPP_NAME
8071 || token->keyword == RID_OPERATOR
8072 || token->type == CPP_SCOPE
8073 || token->type == CPP_TEMPLATE_ID
8074 || token->type == CPP_NESTED_NAME_SPECIFIER)
8076 cp_parser_parse_tentatively (parser);
8077 argument = cp_parser_primary_expression (parser,
8080 if (cp_parser_error_occurred (parser)
8081 || !cp_parser_next_token_ends_template_argument_p (parser))
8082 cp_parser_abort_tentative_parse (parser);
8085 if (qualifying_class)
8086 argument = finish_qualified_id_expr (qualifying_class,
8090 if (TREE_CODE (argument) == VAR_DECL)
8092 /* A variable without external linkage might still be a
8093 valid constant-expression, so no error is issued here
8094 if the external-linkage check fails. */
8095 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8096 cp_parser_simulate_error (parser);
8098 else if (is_overloaded_fn (argument))
8099 /* All overloaded functions are allowed; if the external
8100 linkage test does not pass, an error will be issued
8104 && (TREE_CODE (argument) == OFFSET_REF
8105 || TREE_CODE (argument) == SCOPE_REF))
8106 /* A pointer-to-member. */
8109 cp_parser_simulate_error (parser);
8111 if (cp_parser_parse_definitely (parser))
8114 argument = build_x_unary_op (ADDR_EXPR, argument);
8119 /* If the argument started with "&", there are no other valid
8120 alternatives at this point. */
8123 cp_parser_error (parser, "invalid non-type template argument");
8124 return error_mark_node;
8126 /* The argument must be a constant-expression. */
8127 argument = cp_parser_constant_expression (parser,
8128 /*allow_non_constant_p=*/false,
8129 /*non_constant_p=*/NULL);
8130 /* If it's non-dependent, simplify it. */
8131 return cp_parser_fold_non_dependent_expr (argument);
8134 /* Parse an explicit-instantiation.
8136 explicit-instantiation:
8137 template declaration
8139 Although the standard says `declaration', what it really means is:
8141 explicit-instantiation:
8142 template decl-specifier-seq [opt] declarator [opt] ;
8144 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8145 supposed to be allowed. A defect report has been filed about this
8150 explicit-instantiation:
8151 storage-class-specifier template
8152 decl-specifier-seq [opt] declarator [opt] ;
8153 function-specifier template
8154 decl-specifier-seq [opt] declarator [opt] ; */
8157 cp_parser_explicit_instantiation (cp_parser* parser)
8159 int declares_class_or_enum;
8160 tree decl_specifiers;
8162 tree extension_specifier = NULL_TREE;
8164 /* Look for an (optional) storage-class-specifier or
8165 function-specifier. */
8166 if (cp_parser_allow_gnu_extensions_p (parser))
8169 = cp_parser_storage_class_specifier_opt (parser);
8170 if (!extension_specifier)
8171 extension_specifier = cp_parser_function_specifier_opt (parser);
8174 /* Look for the `template' keyword. */
8175 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8176 /* Let the front end know that we are processing an explicit
8178 begin_explicit_instantiation ();
8179 /* [temp.explicit] says that we are supposed to ignore access
8180 control while processing explicit instantiation directives. */
8181 push_deferring_access_checks (dk_no_check);
8182 /* Parse a decl-specifier-seq. */
8184 = cp_parser_decl_specifier_seq (parser,
8185 CP_PARSER_FLAGS_OPTIONAL,
8187 &declares_class_or_enum);
8188 /* If there was exactly one decl-specifier, and it declared a class,
8189 and there's no declarator, then we have an explicit type
8191 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8195 type = check_tag_decl (decl_specifiers);
8196 /* Turn access control back on for names used during
8197 template instantiation. */
8198 pop_deferring_access_checks ();
8200 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8207 /* Parse the declarator. */
8209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8210 /*ctor_dtor_or_conv_p=*/NULL);
8211 cp_parser_check_for_definition_in_return_type (declarator,
8212 declares_class_or_enum);
8213 decl = grokdeclarator (declarator, decl_specifiers,
8215 /* Turn access control back on for names used during
8216 template instantiation. */
8217 pop_deferring_access_checks ();
8218 /* Do the explicit instantiation. */
8219 do_decl_instantiation (decl, extension_specifier);
8221 /* We're done with the instantiation. */
8222 end_explicit_instantiation ();
8224 cp_parser_consume_semicolon_at_end_of_statement (parser);
8227 /* Parse an explicit-specialization.
8229 explicit-specialization:
8230 template < > declaration
8232 Although the standard says `declaration', what it really means is:
8234 explicit-specialization:
8235 template <> decl-specifier [opt] init-declarator [opt] ;
8236 template <> function-definition
8237 template <> explicit-specialization
8238 template <> template-declaration */
8241 cp_parser_explicit_specialization (cp_parser* parser)
8243 /* Look for the `template' keyword. */
8244 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8245 /* Look for the `<'. */
8246 cp_parser_require (parser, CPP_LESS, "`<'");
8247 /* Look for the `>'. */
8248 cp_parser_require (parser, CPP_GREATER, "`>'");
8249 /* We have processed another parameter list. */
8250 ++parser->num_template_parameter_lists;
8251 /* Let the front end know that we are beginning a specialization. */
8252 begin_specialization ();
8254 /* If the next keyword is `template', we need to figure out whether
8255 or not we're looking a template-declaration. */
8256 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8258 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8259 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8260 cp_parser_template_declaration_after_export (parser,
8261 /*member_p=*/false);
8263 cp_parser_explicit_specialization (parser);
8266 /* Parse the dependent declaration. */
8267 cp_parser_single_declaration (parser,
8271 /* We're done with the specialization. */
8272 end_specialization ();
8273 /* We're done with this parameter list. */
8274 --parser->num_template_parameter_lists;
8277 /* Parse a type-specifier.
8280 simple-type-specifier
8283 elaborated-type-specifier
8291 Returns a representation of the type-specifier. If the
8292 type-specifier is a keyword (like `int' or `const', or
8293 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8294 For a class-specifier, enum-specifier, or elaborated-type-specifier
8295 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8297 If IS_FRIEND is TRUE then this type-specifier is being declared a
8298 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8299 appearing in a decl-specifier-seq.
8301 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8302 class-specifier, enum-specifier, or elaborated-type-specifier, then
8303 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8304 if a type is declared; 2 if it is defined. Otherwise, it is set to
8307 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8308 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8312 cp_parser_type_specifier (cp_parser* parser,
8313 cp_parser_flags flags,
8315 bool is_declaration,
8316 int* declares_class_or_enum,
8317 bool* is_cv_qualifier)
8319 tree type_spec = NULL_TREE;
8323 /* Assume this type-specifier does not declare a new type. */
8324 if (declares_class_or_enum)
8325 *declares_class_or_enum = false;
8326 /* And that it does not specify a cv-qualifier. */
8327 if (is_cv_qualifier)
8328 *is_cv_qualifier = false;
8329 /* Peek at the next token. */
8330 token = cp_lexer_peek_token (parser->lexer);
8332 /* If we're looking at a keyword, we can use that to guide the
8333 production we choose. */
8334 keyword = token->keyword;
8337 /* Any of these indicate either a class-specifier, or an
8338 elaborated-type-specifier. */
8343 /* Parse tentatively so that we can back up if we don't find a
8344 class-specifier or enum-specifier. */
8345 cp_parser_parse_tentatively (parser);
8346 /* Look for the class-specifier or enum-specifier. */
8347 if (keyword == RID_ENUM)
8348 type_spec = cp_parser_enum_specifier (parser);
8350 type_spec = cp_parser_class_specifier (parser);
8352 /* If that worked, we're done. */
8353 if (cp_parser_parse_definitely (parser))
8355 if (declares_class_or_enum)
8356 *declares_class_or_enum = 2;
8363 /* Look for an elaborated-type-specifier. */
8364 type_spec = cp_parser_elaborated_type_specifier (parser,
8367 /* We're declaring a class or enum -- unless we're using
8369 if (declares_class_or_enum && keyword != RID_TYPENAME)
8370 *declares_class_or_enum = 1;
8376 type_spec = cp_parser_cv_qualifier_opt (parser);
8377 /* Even though we call a routine that looks for an optional
8378 qualifier, we know that there should be one. */
8379 my_friendly_assert (type_spec != NULL, 20000328);
8380 /* This type-specifier was a cv-qualified. */
8381 if (is_cv_qualifier)
8382 *is_cv_qualifier = true;
8387 /* The `__complex__' keyword is a GNU extension. */
8388 return cp_lexer_consume_token (parser->lexer)->value;
8394 /* If we do not already have a type-specifier, assume we are looking
8395 at a simple-type-specifier. */
8396 type_spec = cp_parser_simple_type_specifier (parser, flags,
8397 /*identifier_p=*/true);
8399 /* If we didn't find a type-specifier, and a type-specifier was not
8400 optional in this context, issue an error message. */
8401 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8403 cp_parser_error (parser, "expected type specifier");
8404 return error_mark_node;
8410 /* Parse a simple-type-specifier.
8412 simple-type-specifier:
8413 :: [opt] nested-name-specifier [opt] type-name
8414 :: [opt] nested-name-specifier template template-id
8429 simple-type-specifier:
8430 __typeof__ unary-expression
8431 __typeof__ ( type-id )
8433 For the various keywords, the value returned is simply the
8434 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8435 For the first two productions, and if IDENTIFIER_P is false, the
8436 value returned is the indicated TYPE_DECL. */
8439 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8442 tree type = NULL_TREE;
8445 /* Peek at the next token. */
8446 token = cp_lexer_peek_token (parser->lexer);
8448 /* If we're looking at a keyword, things are easy. */
8449 switch (token->keyword)
8452 type = char_type_node;
8455 type = wchar_type_node;
8458 type = boolean_type_node;
8461 type = short_integer_type_node;
8464 type = integer_type_node;
8467 type = long_integer_type_node;
8470 type = integer_type_node;
8473 type = unsigned_type_node;
8476 type = float_type_node;
8479 type = double_type_node;
8482 type = void_type_node;
8489 /* Consume the `typeof' token. */
8490 cp_lexer_consume_token (parser->lexer);
8491 /* Parse the operand to `typeof'. */
8492 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8493 /* If it is not already a TYPE, take its type. */
8494 if (!TYPE_P (operand))
8495 operand = finish_typeof (operand);
8504 /* If the type-specifier was for a built-in type, we're done. */
8509 /* Consume the token. */
8510 id = cp_lexer_consume_token (parser->lexer)->value;
8511 return identifier_p ? id : TYPE_NAME (type);
8514 /* The type-specifier must be a user-defined type. */
8515 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8517 /* Don't gobble tokens or issue error messages if this is an
8518 optional type-specifier. */
8519 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8520 cp_parser_parse_tentatively (parser);
8522 /* Look for the optional `::' operator. */
8523 cp_parser_global_scope_opt (parser,
8524 /*current_scope_valid_p=*/false);
8525 /* Look for the nested-name specifier. */
8526 cp_parser_nested_name_specifier_opt (parser,
8527 /*typename_keyword_p=*/false,
8528 /*check_dependency_p=*/true,
8530 /*is_declaration=*/false);
8531 /* If we have seen a nested-name-specifier, and the next token
8532 is `template', then we are using the template-id production. */
8534 && cp_parser_optional_template_keyword (parser))
8536 /* Look for the template-id. */
8537 type = cp_parser_template_id (parser,
8538 /*template_keyword_p=*/true,
8539 /*check_dependency_p=*/true,
8540 /*is_declaration=*/false);
8541 /* If the template-id did not name a type, we are out of
8543 if (TREE_CODE (type) != TYPE_DECL)
8545 cp_parser_error (parser, "expected template-id for type");
8549 /* Otherwise, look for a type-name. */
8552 type = cp_parser_type_name (parser);
8553 if (type == error_mark_node)
8557 /* If it didn't work out, we don't have a TYPE. */
8558 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8559 && !cp_parser_parse_definitely (parser))
8563 /* If we didn't get a type-name, issue an error message. */
8564 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8566 cp_parser_error (parser, "expected type-name");
8567 return error_mark_node;
8570 /* There is no valid C++ program where a non-template type is
8571 followed by a "<". That usually indicates that the user thought
8572 that the type was a template. */
8574 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8579 /* Parse a type-name.
8592 Returns a TYPE_DECL for the the type. */
8595 cp_parser_type_name (cp_parser* parser)
8600 /* We can't know yet whether it is a class-name or not. */
8601 cp_parser_parse_tentatively (parser);
8602 /* Try a class-name. */
8603 type_decl = cp_parser_class_name (parser,
8604 /*typename_keyword_p=*/false,
8605 /*template_keyword_p=*/false,
8607 /*check_dependency_p=*/true,
8608 /*class_head_p=*/false,
8609 /*is_declaration=*/false);
8610 /* If it's not a class-name, keep looking. */
8611 if (!cp_parser_parse_definitely (parser))
8613 /* It must be a typedef-name or an enum-name. */
8614 identifier = cp_parser_identifier (parser);
8615 if (identifier == error_mark_node)
8616 return error_mark_node;
8618 /* Look up the type-name. */
8619 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8620 /* Issue an error if we did not find a type-name. */
8621 if (TREE_CODE (type_decl) != TYPE_DECL)
8623 cp_parser_error (parser, "expected type-name");
8624 type_decl = error_mark_node;
8626 /* Remember that the name was used in the definition of the
8627 current class so that we can check later to see if the
8628 meaning would have been different after the class was
8629 entirely defined. */
8630 else if (type_decl != error_mark_node
8632 maybe_note_name_used_in_class (identifier, type_decl);
8639 /* Parse an elaborated-type-specifier. Note that the grammar given
8640 here incorporates the resolution to DR68.
8642 elaborated-type-specifier:
8643 class-key :: [opt] nested-name-specifier [opt] identifier
8644 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8645 enum :: [opt] nested-name-specifier [opt] identifier
8646 typename :: [opt] nested-name-specifier identifier
8647 typename :: [opt] nested-name-specifier template [opt]
8650 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8651 declared `friend'. If IS_DECLARATION is TRUE, then this
8652 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8653 something is being declared.
8655 Returns the TYPE specified. */
8658 cp_parser_elaborated_type_specifier (cp_parser* parser,
8660 bool is_declaration)
8662 enum tag_types tag_type;
8664 tree type = NULL_TREE;
8666 /* See if we're looking at the `enum' keyword. */
8667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8669 /* Consume the `enum' token. */
8670 cp_lexer_consume_token (parser->lexer);
8671 /* Remember that it's an enumeration type. */
8672 tag_type = enum_type;
8674 /* Or, it might be `typename'. */
8675 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8678 /* Consume the `typename' token. */
8679 cp_lexer_consume_token (parser->lexer);
8680 /* Remember that it's a `typename' type. */
8681 tag_type = typename_type;
8682 /* The `typename' keyword is only allowed in templates. */
8683 if (!processing_template_decl)
8684 pedwarn ("using `typename' outside of template");
8686 /* Otherwise it must be a class-key. */
8689 tag_type = cp_parser_class_key (parser);
8690 if (tag_type == none_type)
8691 return error_mark_node;
8694 /* Look for the `::' operator. */
8695 cp_parser_global_scope_opt (parser,
8696 /*current_scope_valid_p=*/false);
8697 /* Look for the nested-name-specifier. */
8698 if (tag_type == typename_type)
8700 if (cp_parser_nested_name_specifier (parser,
8701 /*typename_keyword_p=*/true,
8702 /*check_dependency_p=*/true,
8706 return error_mark_node;
8709 /* Even though `typename' is not present, the proposed resolution
8710 to Core Issue 180 says that in `class A<T>::B', `B' should be
8711 considered a type-name, even if `A<T>' is dependent. */
8712 cp_parser_nested_name_specifier_opt (parser,
8713 /*typename_keyword_p=*/true,
8714 /*check_dependency_p=*/true,
8717 /* For everything but enumeration types, consider a template-id. */
8718 if (tag_type != enum_type)
8720 bool template_p = false;
8723 /* Allow the `template' keyword. */
8724 template_p = cp_parser_optional_template_keyword (parser);
8725 /* If we didn't see `template', we don't know if there's a
8726 template-id or not. */
8728 cp_parser_parse_tentatively (parser);
8729 /* Parse the template-id. */
8730 decl = cp_parser_template_id (parser, template_p,
8731 /*check_dependency_p=*/true,
8733 /* If we didn't find a template-id, look for an ordinary
8735 if (!template_p && !cp_parser_parse_definitely (parser))
8737 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8738 in effect, then we must assume that, upon instantiation, the
8739 template will correspond to a class. */
8740 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8741 && tag_type == typename_type)
8742 type = make_typename_type (parser->scope, decl,
8745 type = TREE_TYPE (decl);
8748 /* For an enumeration type, consider only a plain identifier. */
8751 identifier = cp_parser_identifier (parser);
8753 if (identifier == error_mark_node)
8755 parser->scope = NULL_TREE;
8756 return error_mark_node;
8759 /* For a `typename', we needn't call xref_tag. */
8760 if (tag_type == typename_type)
8761 return make_typename_type (parser->scope, identifier,
8763 /* Look up a qualified name in the usual way. */
8768 /* In an elaborated-type-specifier, names are assumed to name
8769 types, so we set IS_TYPE to TRUE when calling
8770 cp_parser_lookup_name. */
8771 decl = cp_parser_lookup_name (parser, identifier,
8773 /*is_namespace=*/false,
8774 /*check_dependency=*/true);
8776 /* If we are parsing friend declaration, DECL may be a
8777 TEMPLATE_DECL tree node here. However, we need to check
8778 whether this TEMPLATE_DECL results in valid code. Consider
8779 the following example:
8782 template <class T> class C {};
8785 template <class T> friend class N::C; // #1, valid code
8787 template <class T> class Y {
8788 friend class N::C; // #2, invalid code
8791 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8792 name lookup of `N::C'. We see that friend declaration must
8793 be template for the code to be valid. Note that
8794 processing_template_decl does not work here since it is
8795 always 1 for the above two cases. */
8797 decl = (cp_parser_maybe_treat_template_as_class
8798 (decl, /*tag_name_p=*/is_friend
8799 && parser->num_template_parameter_lists));
8801 if (TREE_CODE (decl) != TYPE_DECL)
8803 error ("expected type-name");
8804 return error_mark_node;
8807 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8808 check_elaborated_type_specifier
8810 (parser->num_template_parameter_lists
8811 || DECL_SELF_REFERENCE_P (decl)));
8813 type = TREE_TYPE (decl);
8817 /* An elaborated-type-specifier sometimes introduces a new type and
8818 sometimes names an existing type. Normally, the rule is that it
8819 introduces a new type only if there is not an existing type of
8820 the same name already in scope. For example, given:
8823 void f() { struct S s; }
8825 the `struct S' in the body of `f' is the same `struct S' as in
8826 the global scope; the existing definition is used. However, if
8827 there were no global declaration, this would introduce a new
8828 local class named `S'.
8830 An exception to this rule applies to the following code:
8832 namespace N { struct S; }
8834 Here, the elaborated-type-specifier names a new type
8835 unconditionally; even if there is already an `S' in the
8836 containing scope this declaration names a new type.
8837 This exception only applies if the elaborated-type-specifier
8838 forms the complete declaration:
8842 A declaration consisting solely of `class-key identifier ;' is
8843 either a redeclaration of the name in the current scope or a
8844 forward declaration of the identifier as a class name. It
8845 introduces the name into the current scope.
8847 We are in this situation precisely when the next token is a `;'.
8849 An exception to the exception is that a `friend' declaration does
8850 *not* name a new type; i.e., given:
8852 struct S { friend struct T; };
8854 `T' is not a new type in the scope of `S'.
8856 Also, `new struct S' or `sizeof (struct S)' never results in the
8857 definition of a new type; a new type can only be declared in a
8858 declaration context. */
8860 type = xref_tag (tag_type, identifier,
8861 /*attributes=*/NULL_TREE,
8864 || cp_lexer_next_token_is_not (parser->lexer,
8866 parser->num_template_parameter_lists);
8869 if (tag_type != enum_type)
8870 cp_parser_check_class_key (tag_type, type);
8872 /* A "<" cannot follow an elaborated type specifier. If that
8873 happens, the user was probably trying to form a template-id. */
8874 cp_parser_check_for_invalid_template_id (parser, type);
8879 /* Parse an enum-specifier.
8882 enum identifier [opt] { enumerator-list [opt] }
8884 Returns an ENUM_TYPE representing the enumeration. */
8887 cp_parser_enum_specifier (cp_parser* parser)
8890 tree identifier = NULL_TREE;
8893 /* Look for the `enum' keyword. */
8894 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8895 return error_mark_node;
8896 /* Peek at the next token. */
8897 token = cp_lexer_peek_token (parser->lexer);
8899 /* See if it is an identifier. */
8900 if (token->type == CPP_NAME)
8901 identifier = cp_parser_identifier (parser);
8903 /* Look for the `{'. */
8904 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8905 return error_mark_node;
8907 /* At this point, we're going ahead with the enum-specifier, even
8908 if some other problem occurs. */
8909 cp_parser_commit_to_tentative_parse (parser);
8911 /* Issue an error message if type-definitions are forbidden here. */
8912 cp_parser_check_type_definition (parser);
8914 /* Create the new type. */
8915 type = start_enum (identifier ? identifier : make_anon_name ());
8917 /* Peek at the next token. */
8918 token = cp_lexer_peek_token (parser->lexer);
8919 /* If it's not a `}', then there are some enumerators. */
8920 if (token->type != CPP_CLOSE_BRACE)
8921 cp_parser_enumerator_list (parser, type);
8922 /* Look for the `}'. */
8923 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8925 /* Finish up the enumeration. */
8931 /* Parse an enumerator-list. The enumerators all have the indicated
8935 enumerator-definition
8936 enumerator-list , enumerator-definition */
8939 cp_parser_enumerator_list (cp_parser* parser, tree type)
8945 /* Parse an enumerator-definition. */
8946 cp_parser_enumerator_definition (parser, type);
8947 /* Peek at the next token. */
8948 token = cp_lexer_peek_token (parser->lexer);
8949 /* If it's not a `,', then we've reached the end of the
8951 if (token->type != CPP_COMMA)
8953 /* Otherwise, consume the `,' and keep going. */
8954 cp_lexer_consume_token (parser->lexer);
8955 /* If the next token is a `}', there is a trailing comma. */
8956 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8958 if (pedantic && !in_system_header)
8959 pedwarn ("comma at end of enumerator list");
8965 /* Parse an enumerator-definition. The enumerator has the indicated
8968 enumerator-definition:
8970 enumerator = constant-expression
8976 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8982 /* Look for the identifier. */
8983 identifier = cp_parser_identifier (parser);
8984 if (identifier == error_mark_node)
8987 /* Peek at the next token. */
8988 token = cp_lexer_peek_token (parser->lexer);
8989 /* If it's an `=', then there's an explicit value. */
8990 if (token->type == CPP_EQ)
8992 /* Consume the `=' token. */
8993 cp_lexer_consume_token (parser->lexer);
8994 /* Parse the value. */
8995 value = cp_parser_constant_expression (parser,
8996 /*allow_non_constant_p=*/false,
9002 /* Create the enumerator. */
9003 build_enumerator (identifier, value, type);
9006 /* Parse a namespace-name.
9009 original-namespace-name
9012 Returns the NAMESPACE_DECL for the namespace. */
9015 cp_parser_namespace_name (cp_parser* parser)
9018 tree namespace_decl;
9020 /* Get the name of the namespace. */
9021 identifier = cp_parser_identifier (parser);
9022 if (identifier == error_mark_node)
9023 return error_mark_node;
9025 /* Look up the identifier in the currently active scope. Look only
9026 for namespaces, due to:
9030 When looking up a namespace-name in a using-directive or alias
9031 definition, only namespace names are considered.
9037 During the lookup of a name preceding the :: scope resolution
9038 operator, object, function, and enumerator names are ignored.
9040 (Note that cp_parser_class_or_namespace_name only calls this
9041 function if the token after the name is the scope resolution
9043 namespace_decl = cp_parser_lookup_name (parser, identifier,
9045 /*is_namespace=*/true,
9046 /*check_dependency=*/true);
9047 /* If it's not a namespace, issue an error. */
9048 if (namespace_decl == error_mark_node
9049 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9051 cp_parser_error (parser, "expected namespace-name");
9052 namespace_decl = error_mark_node;
9055 return namespace_decl;
9058 /* Parse a namespace-definition.
9060 namespace-definition:
9061 named-namespace-definition
9062 unnamed-namespace-definition
9064 named-namespace-definition:
9065 original-namespace-definition
9066 extension-namespace-definition
9068 original-namespace-definition:
9069 namespace identifier { namespace-body }
9071 extension-namespace-definition:
9072 namespace original-namespace-name { namespace-body }
9074 unnamed-namespace-definition:
9075 namespace { namespace-body } */
9078 cp_parser_namespace_definition (cp_parser* parser)
9082 /* Look for the `namespace' keyword. */
9083 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9085 /* Get the name of the namespace. We do not attempt to distinguish
9086 between an original-namespace-definition and an
9087 extension-namespace-definition at this point. The semantic
9088 analysis routines are responsible for that. */
9089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9090 identifier = cp_parser_identifier (parser);
9092 identifier = NULL_TREE;
9094 /* Look for the `{' to start the namespace. */
9095 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9096 /* Start the namespace. */
9097 push_namespace (identifier);
9098 /* Parse the body of the namespace. */
9099 cp_parser_namespace_body (parser);
9100 /* Finish the namespace. */
9102 /* Look for the final `}'. */
9103 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9106 /* Parse a namespace-body.
9109 declaration-seq [opt] */
9112 cp_parser_namespace_body (cp_parser* parser)
9114 cp_parser_declaration_seq_opt (parser);
9117 /* Parse a namespace-alias-definition.
9119 namespace-alias-definition:
9120 namespace identifier = qualified-namespace-specifier ; */
9123 cp_parser_namespace_alias_definition (cp_parser* parser)
9126 tree namespace_specifier;
9128 /* Look for the `namespace' keyword. */
9129 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9130 /* Look for the identifier. */
9131 identifier = cp_parser_identifier (parser);
9132 if (identifier == error_mark_node)
9134 /* Look for the `=' token. */
9135 cp_parser_require (parser, CPP_EQ, "`='");
9136 /* Look for the qualified-namespace-specifier. */
9138 = cp_parser_qualified_namespace_specifier (parser);
9139 /* Look for the `;' token. */
9140 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9142 /* Register the alias in the symbol table. */
9143 do_namespace_alias (identifier, namespace_specifier);
9146 /* Parse a qualified-namespace-specifier.
9148 qualified-namespace-specifier:
9149 :: [opt] nested-name-specifier [opt] namespace-name
9151 Returns a NAMESPACE_DECL corresponding to the specified
9155 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9157 /* Look for the optional `::'. */
9158 cp_parser_global_scope_opt (parser,
9159 /*current_scope_valid_p=*/false);
9161 /* Look for the optional nested-name-specifier. */
9162 cp_parser_nested_name_specifier_opt (parser,
9163 /*typename_keyword_p=*/false,
9164 /*check_dependency_p=*/true,
9166 /*is_declaration=*/true);
9168 return cp_parser_namespace_name (parser);
9171 /* Parse a using-declaration.
9174 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9175 using :: unqualified-id ; */
9178 cp_parser_using_declaration (cp_parser* parser)
9181 bool typename_p = false;
9182 bool global_scope_p;
9187 /* Look for the `using' keyword. */
9188 cp_parser_require_keyword (parser, RID_USING, "`using'");
9190 /* Peek at the next token. */
9191 token = cp_lexer_peek_token (parser->lexer);
9192 /* See if it's `typename'. */
9193 if (token->keyword == RID_TYPENAME)
9195 /* Remember that we've seen it. */
9197 /* Consume the `typename' token. */
9198 cp_lexer_consume_token (parser->lexer);
9201 /* Look for the optional global scope qualification. */
9203 = (cp_parser_global_scope_opt (parser,
9204 /*current_scope_valid_p=*/false)
9207 /* If we saw `typename', or didn't see `::', then there must be a
9208 nested-name-specifier present. */
9209 if (typename_p || !global_scope_p)
9210 cp_parser_nested_name_specifier (parser, typename_p,
9211 /*check_dependency_p=*/true,
9213 /*is_declaration=*/true);
9214 /* Otherwise, we could be in either of the two productions. In that
9215 case, treat the nested-name-specifier as optional. */
9217 cp_parser_nested_name_specifier_opt (parser,
9218 /*typename_keyword_p=*/false,
9219 /*check_dependency_p=*/true,
9221 /*is_declaration=*/true);
9223 /* Parse the unqualified-id. */
9224 identifier = cp_parser_unqualified_id (parser,
9225 /*template_keyword_p=*/false,
9226 /*check_dependency_p=*/true,
9227 /*declarator_p=*/true);
9229 /* The function we call to handle a using-declaration is different
9230 depending on what scope we are in. */
9231 if (identifier == error_mark_node)
9233 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9234 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9235 /* [namespace.udecl]
9237 A using declaration shall not name a template-id. */
9238 error ("a template-id may not appear in a using-declaration");
9241 scope = current_scope ();
9242 if (scope && TYPE_P (scope))
9244 /* Create the USING_DECL. */
9245 decl = do_class_using_decl (build_nt (SCOPE_REF,
9248 /* Add it to the list of members in this class. */
9249 finish_member_declaration (decl);
9253 decl = cp_parser_lookup_name_simple (parser, identifier);
9254 if (decl == error_mark_node)
9256 if (parser->scope && parser->scope != global_namespace)
9257 error ("`%D::%D' has not been declared",
9258 parser->scope, identifier);
9260 error ("`::%D' has not been declared", identifier);
9263 do_local_using_decl (decl);
9265 do_toplevel_using_decl (decl);
9269 /* Look for the final `;'. */
9270 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9273 /* Parse a using-directive.
9276 using namespace :: [opt] nested-name-specifier [opt]
9280 cp_parser_using_directive (cp_parser* parser)
9282 tree namespace_decl;
9285 /* Look for the `using' keyword. */
9286 cp_parser_require_keyword (parser, RID_USING, "`using'");
9287 /* And the `namespace' keyword. */
9288 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9289 /* Look for the optional `::' operator. */
9290 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9291 /* And the optional nested-name-specifier. */
9292 cp_parser_nested_name_specifier_opt (parser,
9293 /*typename_keyword_p=*/false,
9294 /*check_dependency_p=*/true,
9296 /*is_declaration=*/true);
9297 /* Get the namespace being used. */
9298 namespace_decl = cp_parser_namespace_name (parser);
9299 /* And any specified attributes. */
9300 attribs = cp_parser_attributes_opt (parser);
9301 /* Update the symbol table. */
9302 parse_using_directive (namespace_decl, attribs);
9303 /* Look for the final `;'. */
9304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9307 /* Parse an asm-definition.
9310 asm ( string-literal ) ;
9315 asm volatile [opt] ( string-literal ) ;
9316 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9317 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9318 : asm-operand-list [opt] ) ;
9319 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9320 : asm-operand-list [opt]
9321 : asm-operand-list [opt] ) ; */
9324 cp_parser_asm_definition (cp_parser* parser)
9328 tree outputs = NULL_TREE;
9329 tree inputs = NULL_TREE;
9330 tree clobbers = NULL_TREE;
9332 bool volatile_p = false;
9333 bool extended_p = false;
9335 /* Look for the `asm' keyword. */
9336 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9337 /* See if the next token is `volatile'. */
9338 if (cp_parser_allow_gnu_extensions_p (parser)
9339 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9341 /* Remember that we saw the `volatile' keyword. */
9343 /* Consume the token. */
9344 cp_lexer_consume_token (parser->lexer);
9346 /* Look for the opening `('. */
9347 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9348 /* Look for the string. */
9349 token = cp_parser_require (parser, CPP_STRING, "asm body");
9352 string = token->value;
9353 /* If we're allowing GNU extensions, check for the extended assembly
9354 syntax. Unfortunately, the `:' tokens need not be separated by
9355 a space in C, and so, for compatibility, we tolerate that here
9356 too. Doing that means that we have to treat the `::' operator as
9358 if (cp_parser_allow_gnu_extensions_p (parser)
9359 && at_function_scope_p ()
9360 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9361 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9363 bool inputs_p = false;
9364 bool clobbers_p = false;
9366 /* The extended syntax was used. */
9369 /* Look for outputs. */
9370 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9372 /* Consume the `:'. */
9373 cp_lexer_consume_token (parser->lexer);
9374 /* Parse the output-operands. */
9375 if (cp_lexer_next_token_is_not (parser->lexer,
9377 && cp_lexer_next_token_is_not (parser->lexer,
9379 && cp_lexer_next_token_is_not (parser->lexer,
9381 outputs = cp_parser_asm_operand_list (parser);
9383 /* If the next token is `::', there are no outputs, and the
9384 next token is the beginning of the inputs. */
9385 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9387 /* Consume the `::' token. */
9388 cp_lexer_consume_token (parser->lexer);
9389 /* The inputs are coming next. */
9393 /* Look for inputs. */
9395 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9398 /* Consume the `:'. */
9399 cp_lexer_consume_token (parser->lexer);
9400 /* Parse the output-operands. */
9401 if (cp_lexer_next_token_is_not (parser->lexer,
9403 && cp_lexer_next_token_is_not (parser->lexer,
9405 && cp_lexer_next_token_is_not (parser->lexer,
9407 inputs = cp_parser_asm_operand_list (parser);
9409 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9410 /* The clobbers are coming next. */
9413 /* Look for clobbers. */
9415 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9418 /* Consume the `:'. */
9419 cp_lexer_consume_token (parser->lexer);
9420 /* Parse the clobbers. */
9421 if (cp_lexer_next_token_is_not (parser->lexer,
9423 clobbers = cp_parser_asm_clobber_list (parser);
9426 /* Look for the closing `)'. */
9427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9428 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9429 /*consume_paren=*/true);
9430 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9432 /* Create the ASM_STMT. */
9433 if (at_function_scope_p ())
9436 finish_asm_stmt (volatile_p
9437 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9438 string, outputs, inputs, clobbers);
9439 /* If the extended syntax was not used, mark the ASM_STMT. */
9441 ASM_INPUT_P (asm_stmt) = 1;
9444 assemble_asm (string);
9447 /* Declarators [gram.dcl.decl] */
9449 /* Parse an init-declarator.
9452 declarator initializer [opt]
9457 declarator asm-specification [opt] attributes [opt] initializer [opt]
9459 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9460 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9461 then this declarator appears in a class scope. The new DECL created
9462 by this declarator is returned.
9464 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9465 for a function-definition here as well. If the declarator is a
9466 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9467 be TRUE upon return. By that point, the function-definition will
9468 have been completely parsed.
9470 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9474 cp_parser_init_declarator (cp_parser* parser,
9475 tree decl_specifiers,
9476 tree prefix_attributes,
9477 bool function_definition_allowed_p,
9479 int declares_class_or_enum,
9480 bool* function_definition_p)
9485 tree asm_specification;
9487 tree decl = NULL_TREE;
9489 bool is_initialized;
9490 bool is_parenthesized_init;
9491 bool is_non_constant_init;
9492 int ctor_dtor_or_conv_p;
9495 /* Assume that this is not the declarator for a function
9497 if (function_definition_p)
9498 *function_definition_p = false;
9500 /* Defer access checks while parsing the declarator; we cannot know
9501 what names are accessible until we know what is being
9503 resume_deferring_access_checks ();
9505 /* Parse the declarator. */
9507 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9508 &ctor_dtor_or_conv_p);
9509 /* Gather up the deferred checks. */
9510 stop_deferring_access_checks ();
9512 /* If the DECLARATOR was erroneous, there's no need to go
9514 if (declarator == error_mark_node)
9515 return error_mark_node;
9517 cp_parser_check_for_definition_in_return_type (declarator,
9518 declares_class_or_enum);
9520 /* Figure out what scope the entity declared by the DECLARATOR is
9521 located in. `grokdeclarator' sometimes changes the scope, so
9522 we compute it now. */
9523 scope = get_scope_of_declarator (declarator);
9525 /* If we're allowing GNU extensions, look for an asm-specification
9527 if (cp_parser_allow_gnu_extensions_p (parser))
9529 /* Look for an asm-specification. */
9530 asm_specification = cp_parser_asm_specification_opt (parser);
9531 /* And attributes. */
9532 attributes = cp_parser_attributes_opt (parser);
9536 asm_specification = NULL_TREE;
9537 attributes = NULL_TREE;
9540 /* Peek at the next token. */
9541 token = cp_lexer_peek_token (parser->lexer);
9542 /* Check to see if the token indicates the start of a
9543 function-definition. */
9544 if (cp_parser_token_starts_function_definition_p (token))
9546 if (!function_definition_allowed_p)
9548 /* If a function-definition should not appear here, issue an
9550 cp_parser_error (parser,
9551 "a function-definition is not allowed here");
9552 return error_mark_node;
9556 /* Neither attributes nor an asm-specification are allowed
9557 on a function-definition. */
9558 if (asm_specification)
9559 error ("an asm-specification is not allowed on a function-definition");
9561 error ("attributes are not allowed on a function-definition");
9562 /* This is a function-definition. */
9563 *function_definition_p = true;
9565 /* Parse the function definition. */
9566 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9567 (parser, decl_specifiers, prefix_attributes, declarator));
9575 Only in function declarations for constructors, destructors, and
9576 type conversions can the decl-specifier-seq be omitted.
9578 We explicitly postpone this check past the point where we handle
9579 function-definitions because we tolerate function-definitions
9580 that are missing their return types in some modes. */
9581 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9583 cp_parser_error (parser,
9584 "expected constructor, destructor, or type conversion");
9585 return error_mark_node;
9588 /* An `=' or an `(' indicates an initializer. */
9589 is_initialized = (token->type == CPP_EQ
9590 || token->type == CPP_OPEN_PAREN);
9591 /* If the init-declarator isn't initialized and isn't followed by a
9592 `,' or `;', it's not a valid init-declarator. */
9594 && token->type != CPP_COMMA
9595 && token->type != CPP_SEMICOLON)
9597 cp_parser_error (parser, "expected init-declarator");
9598 return error_mark_node;
9601 /* Because start_decl has side-effects, we should only call it if we
9602 know we're going ahead. By this point, we know that we cannot
9603 possibly be looking at any other construct. */
9604 cp_parser_commit_to_tentative_parse (parser);
9606 /* Check to see whether or not this declaration is a friend. */
9607 friend_p = cp_parser_friend_p (decl_specifiers);
9609 /* Check that the number of template-parameter-lists is OK. */
9610 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9611 return error_mark_node;
9613 /* Enter the newly declared entry in the symbol table. If we're
9614 processing a declaration in a class-specifier, we wait until
9615 after processing the initializer. */
9618 if (parser->in_unbraced_linkage_specification_p)
9620 decl_specifiers = tree_cons (error_mark_node,
9621 get_identifier ("extern"),
9623 have_extern_spec = false;
9625 decl = start_decl (declarator, decl_specifiers,
9626 is_initialized, attributes, prefix_attributes);
9629 /* Enter the SCOPE. That way unqualified names appearing in the
9630 initializer will be looked up in SCOPE. */
9634 /* Perform deferred access control checks, now that we know in which
9635 SCOPE the declared entity resides. */
9636 if (!member_p && decl)
9638 tree saved_current_function_decl = NULL_TREE;
9640 /* If the entity being declared is a function, pretend that we
9641 are in its scope. If it is a `friend', it may have access to
9642 things that would not otherwise be accessible. */
9643 if (TREE_CODE (decl) == FUNCTION_DECL)
9645 saved_current_function_decl = current_function_decl;
9646 current_function_decl = decl;
9649 /* Perform the access control checks for the declarator and the
9650 the decl-specifiers. */
9651 perform_deferred_access_checks ();
9653 /* Restore the saved value. */
9654 if (TREE_CODE (decl) == FUNCTION_DECL)
9655 current_function_decl = saved_current_function_decl;
9658 /* Parse the initializer. */
9660 initializer = cp_parser_initializer (parser,
9661 &is_parenthesized_init,
9662 &is_non_constant_init);
9665 initializer = NULL_TREE;
9666 is_parenthesized_init = false;
9667 is_non_constant_init = true;
9670 /* The old parser allows attributes to appear after a parenthesized
9671 initializer. Mark Mitchell proposed removing this functionality
9672 on the GCC mailing lists on 2002-08-13. This parser accepts the
9673 attributes -- but ignores them. */
9674 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9675 if (cp_parser_attributes_opt (parser))
9676 warning ("attributes after parenthesized initializer ignored");
9678 /* Leave the SCOPE, now that we have processed the initializer. It
9679 is important to do this before calling cp_finish_decl because it
9680 makes decisions about whether to create DECL_STMTs or not based
9681 on the current scope. */
9685 /* For an in-class declaration, use `grokfield' to create the
9689 decl = grokfield (declarator, decl_specifiers,
9690 initializer, /*asmspec=*/NULL_TREE,
9691 /*attributes=*/NULL_TREE);
9692 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9693 cp_parser_save_default_args (parser, decl);
9696 /* Finish processing the declaration. But, skip friend
9698 if (!friend_p && decl)
9699 cp_finish_decl (decl,
9702 /* If the initializer is in parentheses, then this is
9703 a direct-initialization, which means that an
9704 `explicit' constructor is OK. Otherwise, an
9705 `explicit' constructor cannot be used. */
9706 ((is_parenthesized_init || !is_initialized)
9707 ? 0 : LOOKUP_ONLYCONVERTING));
9709 /* Remember whether or not variables were initialized by
9710 constant-expressions. */
9711 if (decl && TREE_CODE (decl) == VAR_DECL
9712 && is_initialized && !is_non_constant_init)
9713 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9718 /* Parse a declarator.
9722 ptr-operator declarator
9724 abstract-declarator:
9725 ptr-operator abstract-declarator [opt]
9726 direct-abstract-declarator
9731 attributes [opt] direct-declarator
9732 attributes [opt] ptr-operator declarator
9734 abstract-declarator:
9735 attributes [opt] ptr-operator abstract-declarator [opt]
9736 attributes [opt] direct-abstract-declarator
9738 Returns a representation of the declarator. If the declarator has
9739 the form `* declarator', then an INDIRECT_REF is returned, whose
9740 only operand is the sub-declarator. Analogously, `& declarator' is
9741 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9742 used. The first operand is the TYPE for `X'. The second operand
9743 is an INDIRECT_REF whose operand is the sub-declarator.
9745 Otherwise, the representation is as for a direct-declarator.
9747 (It would be better to define a structure type to represent
9748 declarators, rather than abusing `tree' nodes to represent
9749 declarators. That would be much clearer and save some memory.
9750 There is no reason for declarators to be garbage-collected, for
9751 example; they are created during parser and no longer needed after
9752 `grokdeclarator' has been called.)
9754 For a ptr-operator that has the optional cv-qualifier-seq,
9755 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9758 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9759 detect constructor, destructor or conversion operators. It is set
9760 to -1 if the declarator is a name, and +1 if it is a
9761 function. Otherwise it is set to zero. Usually you just want to
9762 test for >0, but internally the negative value is used.
9764 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9765 a decl-specifier-seq unless it declares a constructor, destructor,
9766 or conversion. It might seem that we could check this condition in
9767 semantic analysis, rather than parsing, but that makes it difficult
9768 to handle something like `f()'. We want to notice that there are
9769 no decl-specifiers, and therefore realize that this is an
9770 expression, not a declaration.) */
9773 cp_parser_declarator (cp_parser* parser,
9774 cp_parser_declarator_kind dcl_kind,
9775 int* ctor_dtor_or_conv_p)
9779 enum tree_code code;
9780 tree cv_qualifier_seq;
9782 tree attributes = NULL_TREE;
9784 /* Assume this is not a constructor, destructor, or type-conversion
9786 if (ctor_dtor_or_conv_p)
9787 *ctor_dtor_or_conv_p = 0;
9789 if (cp_parser_allow_gnu_extensions_p (parser))
9790 attributes = cp_parser_attributes_opt (parser);
9792 /* Peek at the next token. */
9793 token = cp_lexer_peek_token (parser->lexer);
9795 /* Check for the ptr-operator production. */
9796 cp_parser_parse_tentatively (parser);
9797 /* Parse the ptr-operator. */
9798 code = cp_parser_ptr_operator (parser,
9801 /* If that worked, then we have a ptr-operator. */
9802 if (cp_parser_parse_definitely (parser))
9804 /* The dependent declarator is optional if we are parsing an
9805 abstract-declarator. */
9806 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9807 cp_parser_parse_tentatively (parser);
9809 /* Parse the dependent declarator. */
9810 declarator = cp_parser_declarator (parser, dcl_kind,
9811 /*ctor_dtor_or_conv_p=*/NULL);
9813 /* If we are parsing an abstract-declarator, we must handle the
9814 case where the dependent declarator is absent. */
9815 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9816 && !cp_parser_parse_definitely (parser))
9817 declarator = NULL_TREE;
9819 /* Build the representation of the ptr-operator. */
9820 if (code == INDIRECT_REF)
9821 declarator = make_pointer_declarator (cv_qualifier_seq,
9824 declarator = make_reference_declarator (cv_qualifier_seq,
9826 /* Handle the pointer-to-member case. */
9828 declarator = build_nt (SCOPE_REF, class_type, declarator);
9830 /* Everything else is a direct-declarator. */
9832 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9833 ctor_dtor_or_conv_p);
9835 if (attributes && declarator != error_mark_node)
9836 declarator = tree_cons (attributes, declarator, NULL_TREE);
9841 /* Parse a direct-declarator or direct-abstract-declarator.
9845 direct-declarator ( parameter-declaration-clause )
9846 cv-qualifier-seq [opt]
9847 exception-specification [opt]
9848 direct-declarator [ constant-expression [opt] ]
9851 direct-abstract-declarator:
9852 direct-abstract-declarator [opt]
9853 ( parameter-declaration-clause )
9854 cv-qualifier-seq [opt]
9855 exception-specification [opt]
9856 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9857 ( abstract-declarator )
9859 Returns a representation of the declarator. DCL_KIND is
9860 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9861 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9862 we are parsing a direct-declarator. It is
9863 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9864 of ambiguity we prefer an abstract declarator, as per
9865 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9866 cp_parser_declarator.
9868 For the declarator-id production, the representation is as for an
9869 id-expression, except that a qualified name is represented as a
9870 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9871 see the documentation of the FUNCTION_DECLARATOR_* macros for
9872 information about how to find the various declarator components.
9873 An array-declarator is represented as an ARRAY_REF. The
9874 direct-declarator is the first operand; the constant-expression
9875 indicating the size of the array is the second operand. */
9878 cp_parser_direct_declarator (cp_parser* parser,
9879 cp_parser_declarator_kind dcl_kind,
9880 int* ctor_dtor_or_conv_p)
9883 tree declarator = NULL_TREE;
9884 tree scope = NULL_TREE;
9885 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9886 bool saved_in_declarator_p = parser->in_declarator_p;
9891 /* Peek at the next token. */
9892 token = cp_lexer_peek_token (parser->lexer);
9893 if (token->type == CPP_OPEN_PAREN)
9895 /* This is either a parameter-declaration-clause, or a
9896 parenthesized declarator. When we know we are parsing a
9897 named declarator, it must be a parenthesized declarator
9898 if FIRST is true. For instance, `(int)' is a
9899 parameter-declaration-clause, with an omitted
9900 direct-abstract-declarator. But `((*))', is a
9901 parenthesized abstract declarator. Finally, when T is a
9902 template parameter `(T)' is a
9903 parameter-declaration-clause, and not a parenthesized
9906 We first try and parse a parameter-declaration-clause,
9907 and then try a nested declarator (if FIRST is true).
9909 It is not an error for it not to be a
9910 parameter-declaration-clause, even when FIRST is
9916 The first is the declaration of a function while the
9917 second is a the definition of a variable, including its
9920 Having seen only the parenthesis, we cannot know which of
9921 these two alternatives should be selected. Even more
9922 complex are examples like:
9927 The former is a function-declaration; the latter is a
9928 variable initialization.
9930 Thus again, we try a parameter-declaration-clause, and if
9931 that fails, we back out and return. */
9933 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9936 unsigned saved_num_template_parameter_lists;
9938 cp_parser_parse_tentatively (parser);
9940 /* Consume the `('. */
9941 cp_lexer_consume_token (parser->lexer);
9944 /* If this is going to be an abstract declarator, we're
9945 in a declarator and we can't have default args. */
9946 parser->default_arg_ok_p = false;
9947 parser->in_declarator_p = true;
9950 /* Inside the function parameter list, surrounding
9951 template-parameter-lists do not apply. */
9952 saved_num_template_parameter_lists
9953 = parser->num_template_parameter_lists;
9954 parser->num_template_parameter_lists = 0;
9956 /* Parse the parameter-declaration-clause. */
9957 params = cp_parser_parameter_declaration_clause (parser);
9959 parser->num_template_parameter_lists
9960 = saved_num_template_parameter_lists;
9962 /* If all went well, parse the cv-qualifier-seq and the
9963 exception-specification. */
9964 if (cp_parser_parse_definitely (parser))
9967 tree exception_specification;
9969 if (ctor_dtor_or_conv_p)
9970 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9972 /* Consume the `)'. */
9973 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9975 /* Parse the cv-qualifier-seq. */
9976 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9977 /* And the exception-specification. */
9978 exception_specification
9979 = cp_parser_exception_specification_opt (parser);
9981 /* Create the function-declarator. */
9982 declarator = make_call_declarator (declarator,
9985 exception_specification);
9986 /* Any subsequent parameter lists are to do with
9987 return type, so are not those of the declared
9989 parser->default_arg_ok_p = false;
9991 /* Repeat the main loop. */
9996 /* If this is the first, we can try a parenthesized
10000 parser->default_arg_ok_p = saved_default_arg_ok_p;
10001 parser->in_declarator_p = saved_in_declarator_p;
10003 /* Consume the `('. */
10004 cp_lexer_consume_token (parser->lexer);
10005 /* Parse the nested declarator. */
10007 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10009 /* Expect a `)'. */
10010 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10011 declarator = error_mark_node;
10012 if (declarator == error_mark_node)
10015 goto handle_declarator;
10017 /* Otherwise, we must be done. */
10021 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10022 && token->type == CPP_OPEN_SQUARE)
10024 /* Parse an array-declarator. */
10027 if (ctor_dtor_or_conv_p)
10028 *ctor_dtor_or_conv_p = 0;
10031 parser->default_arg_ok_p = false;
10032 parser->in_declarator_p = true;
10033 /* Consume the `['. */
10034 cp_lexer_consume_token (parser->lexer);
10035 /* Peek at the next token. */
10036 token = cp_lexer_peek_token (parser->lexer);
10037 /* If the next token is `]', then there is no
10038 constant-expression. */
10039 if (token->type != CPP_CLOSE_SQUARE)
10041 bool non_constant_p;
10044 = cp_parser_constant_expression (parser,
10045 /*allow_non_constant=*/true,
10047 if (!non_constant_p)
10048 bounds = cp_parser_fold_non_dependent_expr (bounds);
10051 bounds = NULL_TREE;
10052 /* Look for the closing `]'. */
10053 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10055 declarator = error_mark_node;
10059 declarator = build_nt (ARRAY_REF, declarator, bounds);
10061 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10063 /* Parse a declarator-id */
10064 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10065 cp_parser_parse_tentatively (parser);
10066 declarator = cp_parser_declarator_id (parser);
10067 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10069 if (!cp_parser_parse_definitely (parser))
10070 declarator = error_mark_node;
10071 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10073 cp_parser_error (parser, "expected unqualified-id");
10074 declarator = error_mark_node;
10078 if (declarator == error_mark_node)
10081 if (TREE_CODE (declarator) == SCOPE_REF
10082 && !current_scope ())
10084 tree scope = TREE_OPERAND (declarator, 0);
10086 /* In the declaration of a member of a template class
10087 outside of the class itself, the SCOPE will sometimes
10088 be a TYPENAME_TYPE. For example, given:
10090 template <typename T>
10091 int S<T>::R::i = 3;
10093 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10094 this context, we must resolve S<T>::R to an ordinary
10095 type, rather than a typename type.
10097 The reason we normally avoid resolving TYPENAME_TYPEs
10098 is that a specialization of `S' might render
10099 `S<T>::R' not a type. However, if `S' is
10100 specialized, then this `i' will not be used, so there
10101 is no harm in resolving the types here. */
10102 if (TREE_CODE (scope) == TYPENAME_TYPE)
10106 /* Resolve the TYPENAME_TYPE. */
10107 type = resolve_typename_type (scope,
10108 /*only_current_p=*/false);
10109 /* If that failed, the declarator is invalid. */
10110 if (type != error_mark_node)
10112 /* Build a new DECLARATOR. */
10113 declarator = build_nt (SCOPE_REF,
10115 TREE_OPERAND (declarator, 1));
10119 /* Check to see whether the declarator-id names a constructor,
10120 destructor, or conversion. */
10121 if (declarator && ctor_dtor_or_conv_p
10122 && ((TREE_CODE (declarator) == SCOPE_REF
10123 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10124 || (TREE_CODE (declarator) != SCOPE_REF
10125 && at_class_scope_p ())))
10127 tree unqualified_name;
10130 /* Get the unqualified part of the name. */
10131 if (TREE_CODE (declarator) == SCOPE_REF)
10133 class_type = TREE_OPERAND (declarator, 0);
10134 unqualified_name = TREE_OPERAND (declarator, 1);
10138 class_type = current_class_type;
10139 unqualified_name = declarator;
10142 /* See if it names ctor, dtor or conv. */
10143 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10144 || IDENTIFIER_TYPENAME_P (unqualified_name)
10145 || constructor_name_p (unqualified_name, class_type))
10146 *ctor_dtor_or_conv_p = -1;
10149 handle_declarator:;
10150 scope = get_scope_of_declarator (declarator);
10152 /* Any names that appear after the declarator-id for a member
10153 are looked up in the containing scope. */
10154 push_scope (scope);
10155 parser->in_declarator_p = true;
10156 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10158 && (TREE_CODE (declarator) == SCOPE_REF
10159 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10160 /* Default args are only allowed on function
10162 parser->default_arg_ok_p = saved_default_arg_ok_p;
10164 parser->default_arg_ok_p = false;
10173 /* For an abstract declarator, we might wind up with nothing at this
10174 point. That's an error; the declarator is not optional. */
10176 cp_parser_error (parser, "expected declarator");
10178 /* If we entered a scope, we must exit it now. */
10182 parser->default_arg_ok_p = saved_default_arg_ok_p;
10183 parser->in_declarator_p = saved_in_declarator_p;
10188 /* Parse a ptr-operator.
10191 * cv-qualifier-seq [opt]
10193 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10198 & cv-qualifier-seq [opt]
10200 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10201 used. Returns ADDR_EXPR if a reference was used. In the
10202 case of a pointer-to-member, *TYPE is filled in with the
10203 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10204 with the cv-qualifier-seq, or NULL_TREE, if there are no
10205 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10207 static enum tree_code
10208 cp_parser_ptr_operator (cp_parser* parser,
10210 tree* cv_qualifier_seq)
10212 enum tree_code code = ERROR_MARK;
10215 /* Assume that it's not a pointer-to-member. */
10217 /* And that there are no cv-qualifiers. */
10218 *cv_qualifier_seq = NULL_TREE;
10220 /* Peek at the next token. */
10221 token = cp_lexer_peek_token (parser->lexer);
10222 /* If it's a `*' or `&' we have a pointer or reference. */
10223 if (token->type == CPP_MULT || token->type == CPP_AND)
10225 /* Remember which ptr-operator we were processing. */
10226 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10228 /* Consume the `*' or `&'. */
10229 cp_lexer_consume_token (parser->lexer);
10231 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10232 `&', if we are allowing GNU extensions. (The only qualifier
10233 that can legally appear after `&' is `restrict', but that is
10234 enforced during semantic analysis. */
10235 if (code == INDIRECT_REF
10236 || cp_parser_allow_gnu_extensions_p (parser))
10237 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10241 /* Try the pointer-to-member case. */
10242 cp_parser_parse_tentatively (parser);
10243 /* Look for the optional `::' operator. */
10244 cp_parser_global_scope_opt (parser,
10245 /*current_scope_valid_p=*/false);
10246 /* Look for the nested-name specifier. */
10247 cp_parser_nested_name_specifier (parser,
10248 /*typename_keyword_p=*/false,
10249 /*check_dependency_p=*/true,
10251 /*is_declaration=*/false);
10252 /* If we found it, and the next token is a `*', then we are
10253 indeed looking at a pointer-to-member operator. */
10254 if (!cp_parser_error_occurred (parser)
10255 && cp_parser_require (parser, CPP_MULT, "`*'"))
10257 /* The type of which the member is a member is given by the
10259 *type = parser->scope;
10260 /* The next name will not be qualified. */
10261 parser->scope = NULL_TREE;
10262 parser->qualifying_scope = NULL_TREE;
10263 parser->object_scope = NULL_TREE;
10264 /* Indicate that the `*' operator was used. */
10265 code = INDIRECT_REF;
10266 /* Look for the optional cv-qualifier-seq. */
10267 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10269 /* If that didn't work we don't have a ptr-operator. */
10270 if (!cp_parser_parse_definitely (parser))
10271 cp_parser_error (parser, "expected ptr-operator");
10277 /* Parse an (optional) cv-qualifier-seq.
10280 cv-qualifier cv-qualifier-seq [opt]
10282 Returns a TREE_LIST. The TREE_VALUE of each node is the
10283 representation of a cv-qualifier. */
10286 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10288 tree cv_qualifiers = NULL_TREE;
10294 /* Look for the next cv-qualifier. */
10295 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10296 /* If we didn't find one, we're done. */
10300 /* Add this cv-qualifier to the list. */
10302 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10305 /* We built up the list in reverse order. */
10306 return nreverse (cv_qualifiers);
10309 /* Parse an (optional) cv-qualifier.
10321 cp_parser_cv_qualifier_opt (cp_parser* parser)
10324 tree cv_qualifier = NULL_TREE;
10326 /* Peek at the next token. */
10327 token = cp_lexer_peek_token (parser->lexer);
10328 /* See if it's a cv-qualifier. */
10329 switch (token->keyword)
10334 /* Save the value of the token. */
10335 cv_qualifier = token->value;
10336 /* Consume the token. */
10337 cp_lexer_consume_token (parser->lexer);
10344 return cv_qualifier;
10347 /* Parse a declarator-id.
10351 :: [opt] nested-name-specifier [opt] type-name
10353 In the `id-expression' case, the value returned is as for
10354 cp_parser_id_expression if the id-expression was an unqualified-id.
10355 If the id-expression was a qualified-id, then a SCOPE_REF is
10356 returned. The first operand is the scope (either a NAMESPACE_DECL
10357 or TREE_TYPE), but the second is still just a representation of an
10361 cp_parser_declarator_id (cp_parser* parser)
10363 tree id_expression;
10365 /* The expression must be an id-expression. Assume that qualified
10366 names are the names of types so that:
10369 int S<T>::R::i = 3;
10371 will work; we must treat `S<T>::R' as the name of a type.
10372 Similarly, assume that qualified names are templates, where
10376 int S<T>::R<T>::i = 3;
10379 id_expression = cp_parser_id_expression (parser,
10380 /*template_keyword_p=*/false,
10381 /*check_dependency_p=*/false,
10382 /*template_p=*/NULL,
10383 /*declarator_p=*/true);
10384 /* If the name was qualified, create a SCOPE_REF to represent
10388 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10389 parser->scope = NULL_TREE;
10392 return id_expression;
10395 /* Parse a type-id.
10398 type-specifier-seq abstract-declarator [opt]
10400 Returns the TYPE specified. */
10403 cp_parser_type_id (cp_parser* parser)
10405 tree type_specifier_seq;
10406 tree abstract_declarator;
10408 /* Parse the type-specifier-seq. */
10410 = cp_parser_type_specifier_seq (parser);
10411 if (type_specifier_seq == error_mark_node)
10412 return error_mark_node;
10414 /* There might or might not be an abstract declarator. */
10415 cp_parser_parse_tentatively (parser);
10416 /* Look for the declarator. */
10417 abstract_declarator
10418 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10419 /* Check to see if there really was a declarator. */
10420 if (!cp_parser_parse_definitely (parser))
10421 abstract_declarator = NULL_TREE;
10423 return groktypename (build_tree_list (type_specifier_seq,
10424 abstract_declarator));
10427 /* Parse a type-specifier-seq.
10429 type-specifier-seq:
10430 type-specifier type-specifier-seq [opt]
10434 type-specifier-seq:
10435 attributes type-specifier-seq [opt]
10437 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10438 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10441 cp_parser_type_specifier_seq (cp_parser* parser)
10443 bool seen_type_specifier = false;
10444 tree type_specifier_seq = NULL_TREE;
10446 /* Parse the type-specifiers and attributes. */
10449 tree type_specifier;
10451 /* Check for attributes first. */
10452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10454 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10456 type_specifier_seq);
10460 /* After the first type-specifier, others are optional. */
10461 if (seen_type_specifier)
10462 cp_parser_parse_tentatively (parser);
10463 /* Look for the type-specifier. */
10464 type_specifier = cp_parser_type_specifier (parser,
10465 CP_PARSER_FLAGS_NONE,
10466 /*is_friend=*/false,
10467 /*is_declaration=*/false,
10470 /* If the first type-specifier could not be found, this is not a
10471 type-specifier-seq at all. */
10472 if (!seen_type_specifier && type_specifier == error_mark_node)
10473 return error_mark_node;
10474 /* If subsequent type-specifiers could not be found, the
10475 type-specifier-seq is complete. */
10476 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10479 /* Add the new type-specifier to the list. */
10481 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10482 seen_type_specifier = true;
10485 /* We built up the list in reverse order. */
10486 return nreverse (type_specifier_seq);
10489 /* Parse a parameter-declaration-clause.
10491 parameter-declaration-clause:
10492 parameter-declaration-list [opt] ... [opt]
10493 parameter-declaration-list , ...
10495 Returns a representation for the parameter declarations. Each node
10496 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10497 representation.) If the parameter-declaration-clause ends with an
10498 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10499 list. A return value of NULL_TREE indicates a
10500 parameter-declaration-clause consisting only of an ellipsis. */
10503 cp_parser_parameter_declaration_clause (cp_parser* parser)
10509 /* Peek at the next token. */
10510 token = cp_lexer_peek_token (parser->lexer);
10511 /* Check for trivial parameter-declaration-clauses. */
10512 if (token->type == CPP_ELLIPSIS)
10514 /* Consume the `...' token. */
10515 cp_lexer_consume_token (parser->lexer);
10518 else if (token->type == CPP_CLOSE_PAREN)
10519 /* There are no parameters. */
10521 #ifndef NO_IMPLICIT_EXTERN_C
10522 if (in_system_header && current_class_type == NULL
10523 && current_lang_name == lang_name_c)
10527 return void_list_node;
10529 /* Check for `(void)', too, which is a special case. */
10530 else if (token->keyword == RID_VOID
10531 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10532 == CPP_CLOSE_PAREN))
10534 /* Consume the `void' token. */
10535 cp_lexer_consume_token (parser->lexer);
10536 /* There are no parameters. */
10537 return void_list_node;
10540 /* Parse the parameter-declaration-list. */
10541 parameters = cp_parser_parameter_declaration_list (parser);
10542 /* If a parse error occurred while parsing the
10543 parameter-declaration-list, then the entire
10544 parameter-declaration-clause is erroneous. */
10545 if (parameters == error_mark_node)
10546 return error_mark_node;
10548 /* Peek at the next token. */
10549 token = cp_lexer_peek_token (parser->lexer);
10550 /* If it's a `,', the clause should terminate with an ellipsis. */
10551 if (token->type == CPP_COMMA)
10553 /* Consume the `,'. */
10554 cp_lexer_consume_token (parser->lexer);
10555 /* Expect an ellipsis. */
10557 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10559 /* It might also be `...' if the optional trailing `,' was
10561 else if (token->type == CPP_ELLIPSIS)
10563 /* Consume the `...' token. */
10564 cp_lexer_consume_token (parser->lexer);
10565 /* And remember that we saw it. */
10569 ellipsis_p = false;
10571 /* Finish the parameter list. */
10572 return finish_parmlist (parameters, ellipsis_p);
10575 /* Parse a parameter-declaration-list.
10577 parameter-declaration-list:
10578 parameter-declaration
10579 parameter-declaration-list , parameter-declaration
10581 Returns a representation of the parameter-declaration-list, as for
10582 cp_parser_parameter_declaration_clause. However, the
10583 `void_list_node' is never appended to the list. */
10586 cp_parser_parameter_declaration_list (cp_parser* parser)
10588 tree parameters = NULL_TREE;
10590 /* Look for more parameters. */
10594 /* Parse the parameter. */
10596 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10598 /* If a parse error occurred parsing the parameter declaration,
10599 then the entire parameter-declaration-list is erroneous. */
10600 if (parameter == error_mark_node)
10602 parameters = error_mark_node;
10605 /* Add the new parameter to the list. */
10606 TREE_CHAIN (parameter) = parameters;
10607 parameters = parameter;
10609 /* Peek at the next token. */
10610 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10611 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10612 /* The parameter-declaration-list is complete. */
10614 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10618 /* Peek at the next token. */
10619 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10620 /* If it's an ellipsis, then the list is complete. */
10621 if (token->type == CPP_ELLIPSIS)
10623 /* Otherwise, there must be more parameters. Consume the
10625 cp_lexer_consume_token (parser->lexer);
10629 cp_parser_error (parser, "expected `,' or `...'");
10634 /* We built up the list in reverse order; straighten it out now. */
10635 return nreverse (parameters);
10638 /* Parse a parameter declaration.
10640 parameter-declaration:
10641 decl-specifier-seq declarator
10642 decl-specifier-seq declarator = assignment-expression
10643 decl-specifier-seq abstract-declarator [opt]
10644 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10646 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10647 declares a template parameter. (In that case, a non-nested `>'
10648 token encountered during the parsing of the assignment-expression
10649 is not interpreted as a greater-than operator.)
10651 Returns a TREE_LIST representing the parameter-declaration. The
10652 TREE_VALUE is a representation of the decl-specifier-seq and
10653 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10654 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10655 TREE_VALUE represents the declarator. */
10658 cp_parser_parameter_declaration (cp_parser *parser,
10659 bool template_parm_p)
10661 int declares_class_or_enum;
10662 bool greater_than_is_operator_p;
10663 tree decl_specifiers;
10666 tree default_argument;
10669 const char *saved_message;
10671 /* In a template parameter, `>' is not an operator.
10675 When parsing a default template-argument for a non-type
10676 template-parameter, the first non-nested `>' is taken as the end
10677 of the template parameter-list rather than a greater-than
10679 greater_than_is_operator_p = !template_parm_p;
10681 /* Type definitions may not appear in parameter types. */
10682 saved_message = parser->type_definition_forbidden_message;
10683 parser->type_definition_forbidden_message
10684 = "types may not be defined in parameter types";
10686 /* Parse the declaration-specifiers. */
10688 = cp_parser_decl_specifier_seq (parser,
10689 CP_PARSER_FLAGS_NONE,
10691 &declares_class_or_enum);
10692 /* If an error occurred, there's no reason to attempt to parse the
10693 rest of the declaration. */
10694 if (cp_parser_error_occurred (parser))
10696 parser->type_definition_forbidden_message = saved_message;
10697 return error_mark_node;
10700 /* Peek at the next token. */
10701 token = cp_lexer_peek_token (parser->lexer);
10702 /* If the next token is a `)', `,', `=', `>', or `...', then there
10703 is no declarator. */
10704 if (token->type == CPP_CLOSE_PAREN
10705 || token->type == CPP_COMMA
10706 || token->type == CPP_EQ
10707 || token->type == CPP_ELLIPSIS
10708 || token->type == CPP_GREATER)
10709 declarator = NULL_TREE;
10710 /* Otherwise, there should be a declarator. */
10713 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10714 parser->default_arg_ok_p = false;
10716 declarator = cp_parser_declarator (parser,
10717 CP_PARSER_DECLARATOR_EITHER,
10718 /*ctor_dtor_or_conv_p=*/NULL);
10719 parser->default_arg_ok_p = saved_default_arg_ok_p;
10720 /* After the declarator, allow more attributes. */
10721 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10724 /* The restriction on defining new types applies only to the type
10725 of the parameter, not to the default argument. */
10726 parser->type_definition_forbidden_message = saved_message;
10728 /* If the next token is `=', then process a default argument. */
10729 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10731 bool saved_greater_than_is_operator_p;
10732 /* Consume the `='. */
10733 cp_lexer_consume_token (parser->lexer);
10735 /* If we are defining a class, then the tokens that make up the
10736 default argument must be saved and processed later. */
10737 if (!template_parm_p && at_class_scope_p ()
10738 && TYPE_BEING_DEFINED (current_class_type))
10740 unsigned depth = 0;
10742 /* Create a DEFAULT_ARG to represented the unparsed default
10744 default_argument = make_node (DEFAULT_ARG);
10745 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10747 /* Add tokens until we have processed the entire default
10754 /* Peek at the next token. */
10755 token = cp_lexer_peek_token (parser->lexer);
10756 /* What we do depends on what token we have. */
10757 switch (token->type)
10759 /* In valid code, a default argument must be
10760 immediately followed by a `,' `)', or `...'. */
10762 case CPP_CLOSE_PAREN:
10764 /* If we run into a non-nested `;', `}', or `]',
10765 then the code is invalid -- but the default
10766 argument is certainly over. */
10767 case CPP_SEMICOLON:
10768 case CPP_CLOSE_BRACE:
10769 case CPP_CLOSE_SQUARE:
10772 /* Update DEPTH, if necessary. */
10773 else if (token->type == CPP_CLOSE_PAREN
10774 || token->type == CPP_CLOSE_BRACE
10775 || token->type == CPP_CLOSE_SQUARE)
10779 case CPP_OPEN_PAREN:
10780 case CPP_OPEN_SQUARE:
10781 case CPP_OPEN_BRACE:
10786 /* If we see a non-nested `>', and `>' is not an
10787 operator, then it marks the end of the default
10789 if (!depth && !greater_than_is_operator_p)
10793 /* If we run out of tokens, issue an error message. */
10795 error ("file ends in default argument");
10801 /* In these cases, we should look for template-ids.
10802 For example, if the default argument is
10803 `X<int, double>()', we need to do name lookup to
10804 figure out whether or not `X' is a template; if
10805 so, the `,' does not end the default argument.
10807 That is not yet done. */
10814 /* If we've reached the end, stop. */
10818 /* Add the token to the token block. */
10819 token = cp_lexer_consume_token (parser->lexer);
10820 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10824 /* Outside of a class definition, we can just parse the
10825 assignment-expression. */
10828 bool saved_local_variables_forbidden_p;
10830 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10832 saved_greater_than_is_operator_p
10833 = parser->greater_than_is_operator_p;
10834 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10835 /* Local variable names (and the `this' keyword) may not
10836 appear in a default argument. */
10837 saved_local_variables_forbidden_p
10838 = parser->local_variables_forbidden_p;
10839 parser->local_variables_forbidden_p = true;
10840 /* Parse the assignment-expression. */
10841 default_argument = cp_parser_assignment_expression (parser);
10842 /* Restore saved state. */
10843 parser->greater_than_is_operator_p
10844 = saved_greater_than_is_operator_p;
10845 parser->local_variables_forbidden_p
10846 = saved_local_variables_forbidden_p;
10848 if (!parser->default_arg_ok_p)
10850 if (!flag_pedantic_errors)
10851 warning ("deprecated use of default argument for parameter of non-function");
10854 error ("default arguments are only permitted for function parameters");
10855 default_argument = NULL_TREE;
10860 default_argument = NULL_TREE;
10862 /* Create the representation of the parameter. */
10864 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10865 parameter = build_tree_list (default_argument,
10866 build_tree_list (decl_specifiers,
10872 /* Parse a function-definition.
10874 function-definition:
10875 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10877 decl-specifier-seq [opt] declarator function-try-block
10881 function-definition:
10882 __extension__ function-definition
10884 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10885 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10889 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10891 tree decl_specifiers;
10896 int declares_class_or_enum;
10898 /* The saved value of the PEDANTIC flag. */
10899 int saved_pedantic;
10901 /* Any pending qualification must be cleared by our caller. It is
10902 more robust to force the callers to clear PARSER->SCOPE than to
10903 do it here since if the qualification is in effect here, it might
10904 also end up in effect elsewhere that it is not intended. */
10905 my_friendly_assert (!parser->scope, 20010821);
10907 /* Handle `__extension__'. */
10908 if (cp_parser_extension_opt (parser, &saved_pedantic))
10910 /* Parse the function-definition. */
10911 fn = cp_parser_function_definition (parser, friend_p);
10912 /* Restore the PEDANTIC flag. */
10913 pedantic = saved_pedantic;
10918 /* Check to see if this definition appears in a class-specifier. */
10919 member_p = (at_class_scope_p ()
10920 && TYPE_BEING_DEFINED (current_class_type));
10921 /* Defer access checks in the decl-specifier-seq until we know what
10922 function is being defined. There is no need to do this for the
10923 definition of member functions; we cannot be defining a member
10924 from another class. */
10925 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10927 /* Parse the decl-specifier-seq. */
10929 = cp_parser_decl_specifier_seq (parser,
10930 CP_PARSER_FLAGS_OPTIONAL,
10932 &declares_class_or_enum);
10933 /* Figure out whether this declaration is a `friend'. */
10935 *friend_p = cp_parser_friend_p (decl_specifiers);
10937 /* Parse the declarator. */
10938 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10939 /*ctor_dtor_or_conv_p=*/NULL);
10941 /* Gather up any access checks that occurred. */
10942 stop_deferring_access_checks ();
10944 /* If something has already gone wrong, we may as well stop now. */
10945 if (declarator == error_mark_node)
10947 /* Skip to the end of the function, or if this wasn't anything
10948 like a function-definition, to a `;' in the hopes of finding
10949 a sensible place from which to continue parsing. */
10950 cp_parser_skip_to_end_of_block_or_statement (parser);
10951 pop_deferring_access_checks ();
10952 return error_mark_node;
10955 /* The next character should be a `{' (for a simple function
10956 definition), a `:' (for a ctor-initializer), or `try' (for a
10957 function-try block). */
10958 token = cp_lexer_peek_token (parser->lexer);
10959 if (!cp_parser_token_starts_function_definition_p (token))
10961 /* Issue the error-message. */
10962 cp_parser_error (parser, "expected function-definition");
10963 /* Skip to the next `;'. */
10964 cp_parser_skip_to_end_of_block_or_statement (parser);
10966 pop_deferring_access_checks ();
10967 return error_mark_node;
10970 cp_parser_check_for_definition_in_return_type (declarator,
10971 declares_class_or_enum);
10973 /* If we are in a class scope, then we must handle
10974 function-definitions specially. In particular, we save away the
10975 tokens that make up the function body, and parse them again
10976 later, in order to handle code like:
10979 int f () { return i; }
10983 Here, we cannot parse the body of `f' until after we have seen
10984 the declaration of `i'. */
10987 cp_token_cache *cache;
10989 /* Create the function-declaration. */
10990 fn = start_method (decl_specifiers, declarator, attributes);
10991 /* If something went badly wrong, bail out now. */
10992 if (fn == error_mark_node)
10994 /* If there's a function-body, skip it. */
10995 if (cp_parser_token_starts_function_definition_p
10996 (cp_lexer_peek_token (parser->lexer)))
10997 cp_parser_skip_to_end_of_block_or_statement (parser);
10998 pop_deferring_access_checks ();
10999 return error_mark_node;
11002 /* Remember it, if there default args to post process. */
11003 cp_parser_save_default_args (parser, fn);
11005 /* Create a token cache. */
11006 cache = cp_token_cache_new ();
11007 /* Save away the tokens that make up the body of the
11009 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11010 /* Handle function try blocks. */
11011 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11012 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11014 /* Save away the inline definition; we will process it when the
11015 class is complete. */
11016 DECL_PENDING_INLINE_INFO (fn) = cache;
11017 DECL_PENDING_INLINE_P (fn) = 1;
11019 /* We need to know that this was defined in the class, so that
11020 friend templates are handled correctly. */
11021 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
11023 /* We're done with the inline definition. */
11024 finish_method (fn);
11026 /* Add FN to the queue of functions to be parsed later. */
11027 TREE_VALUE (parser->unparsed_functions_queues)
11028 = tree_cons (NULL_TREE, fn,
11029 TREE_VALUE (parser->unparsed_functions_queues));
11031 pop_deferring_access_checks ();
11035 /* Check that the number of template-parameter-lists is OK. */
11036 if (!cp_parser_check_declarator_template_parameters (parser,
11039 cp_parser_skip_to_end_of_block_or_statement (parser);
11040 pop_deferring_access_checks ();
11041 return error_mark_node;
11044 fn = cp_parser_function_definition_from_specifiers_and_declarator
11045 (parser, decl_specifiers, attributes, declarator);
11046 pop_deferring_access_checks ();
11050 /* Parse a function-body.
11053 compound_statement */
11056 cp_parser_function_body (cp_parser *parser)
11058 cp_parser_compound_statement (parser, false);
11061 /* Parse a ctor-initializer-opt followed by a function-body. Return
11062 true if a ctor-initializer was present. */
11065 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11068 bool ctor_initializer_p;
11070 /* Begin the function body. */
11071 body = begin_function_body ();
11072 /* Parse the optional ctor-initializer. */
11073 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11074 /* Parse the function-body. */
11075 cp_parser_function_body (parser);
11076 /* Finish the function body. */
11077 finish_function_body (body);
11079 return ctor_initializer_p;
11082 /* Parse an initializer.
11085 = initializer-clause
11086 ( expression-list )
11088 Returns a expression representing the initializer. If no
11089 initializer is present, NULL_TREE is returned.
11091 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11092 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11093 set to FALSE if there is no initializer present. If there is an
11094 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11095 is set to true; otherwise it is set to false. */
11098 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11099 bool* non_constant_p)
11104 /* Peek at the next token. */
11105 token = cp_lexer_peek_token (parser->lexer);
11107 /* Let our caller know whether or not this initializer was
11109 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11110 /* Assume that the initializer is constant. */
11111 *non_constant_p = false;
11113 if (token->type == CPP_EQ)
11115 /* Consume the `='. */
11116 cp_lexer_consume_token (parser->lexer);
11117 /* Parse the initializer-clause. */
11118 init = cp_parser_initializer_clause (parser, non_constant_p);
11120 else if (token->type == CPP_OPEN_PAREN)
11121 init = cp_parser_parenthesized_expression_list (parser, false,
11125 /* Anything else is an error. */
11126 cp_parser_error (parser, "expected initializer");
11127 init = error_mark_node;
11133 /* Parse an initializer-clause.
11135 initializer-clause:
11136 assignment-expression
11137 { initializer-list , [opt] }
11140 Returns an expression representing the initializer.
11142 If the `assignment-expression' production is used the value
11143 returned is simply a representation for the expression.
11145 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11146 the elements of the initializer-list (or NULL_TREE, if the last
11147 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11148 NULL_TREE. There is no way to detect whether or not the optional
11149 trailing `,' was provided. NON_CONSTANT_P is as for
11150 cp_parser_initializer. */
11153 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11157 /* If it is not a `{', then we are looking at an
11158 assignment-expression. */
11159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11161 = cp_parser_constant_expression (parser,
11162 /*allow_non_constant_p=*/true,
11166 /* Consume the `{' token. */
11167 cp_lexer_consume_token (parser->lexer);
11168 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11169 initializer = make_node (CONSTRUCTOR);
11170 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11171 necessary, but check_initializer depends upon it, for
11173 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11174 /* If it's not a `}', then there is a non-trivial initializer. */
11175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11177 /* Parse the initializer list. */
11178 CONSTRUCTOR_ELTS (initializer)
11179 = cp_parser_initializer_list (parser, non_constant_p);
11180 /* A trailing `,' token is allowed. */
11181 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11182 cp_lexer_consume_token (parser->lexer);
11184 /* Now, there should be a trailing `}'. */
11185 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11188 return initializer;
11191 /* Parse an initializer-list.
11195 initializer-list , initializer-clause
11200 identifier : initializer-clause
11201 initializer-list, identifier : initializer-clause
11203 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11204 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11205 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11206 as for cp_parser_initializer. */
11209 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11211 tree initializers = NULL_TREE;
11213 /* Assume all of the expressions are constant. */
11214 *non_constant_p = false;
11216 /* Parse the rest of the list. */
11222 bool clause_non_constant_p;
11224 /* If the next token is an identifier and the following one is a
11225 colon, we are looking at the GNU designated-initializer
11227 if (cp_parser_allow_gnu_extensions_p (parser)
11228 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11229 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11231 /* Consume the identifier. */
11232 identifier = cp_lexer_consume_token (parser->lexer)->value;
11233 /* Consume the `:'. */
11234 cp_lexer_consume_token (parser->lexer);
11237 identifier = NULL_TREE;
11239 /* Parse the initializer. */
11240 initializer = cp_parser_initializer_clause (parser,
11241 &clause_non_constant_p);
11242 /* If any clause is non-constant, so is the entire initializer. */
11243 if (clause_non_constant_p)
11244 *non_constant_p = true;
11245 /* Add it to the list. */
11246 initializers = tree_cons (identifier, initializer, initializers);
11248 /* If the next token is not a comma, we have reached the end of
11250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11253 /* Peek at the next token. */
11254 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11255 /* If the next token is a `}', then we're still done. An
11256 initializer-clause can have a trailing `,' after the
11257 initializer-list and before the closing `}'. */
11258 if (token->type == CPP_CLOSE_BRACE)
11261 /* Consume the `,' token. */
11262 cp_lexer_consume_token (parser->lexer);
11265 /* The initializers were built up in reverse order, so we need to
11266 reverse them now. */
11267 return nreverse (initializers);
11270 /* Classes [gram.class] */
11272 /* Parse a class-name.
11278 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11279 to indicate that names looked up in dependent types should be
11280 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11281 keyword has been used to indicate that the name that appears next
11282 is a template. TYPE_P is true iff the next name should be treated
11283 as class-name, even if it is declared to be some other kind of name
11284 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11285 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11286 being defined in a class-head.
11288 Returns the TYPE_DECL representing the class. */
11291 cp_parser_class_name (cp_parser *parser,
11292 bool typename_keyword_p,
11293 bool template_keyword_p,
11295 bool check_dependency_p,
11297 bool is_declaration)
11304 /* All class-names start with an identifier. */
11305 token = cp_lexer_peek_token (parser->lexer);
11306 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11308 cp_parser_error (parser, "expected class-name");
11309 return error_mark_node;
11312 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11313 to a template-id, so we save it here. */
11314 scope = parser->scope;
11315 if (scope == error_mark_node)
11316 return error_mark_node;
11318 /* Any name names a type if we're following the `typename' keyword
11319 in a qualified name where the enclosing scope is type-dependent. */
11320 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11321 && dependent_type_p (scope));
11322 /* Handle the common case (an identifier, but not a template-id)
11324 if (token->type == CPP_NAME
11325 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11329 /* Look for the identifier. */
11330 identifier = cp_parser_identifier (parser);
11331 /* If the next token isn't an identifier, we are certainly not
11332 looking at a class-name. */
11333 if (identifier == error_mark_node)
11334 decl = error_mark_node;
11335 /* If we know this is a type-name, there's no need to look it
11337 else if (typename_p)
11341 /* If the next token is a `::', then the name must be a type
11344 [basic.lookup.qual]
11346 During the lookup for a name preceding the :: scope
11347 resolution operator, object, function, and enumerator
11348 names are ignored. */
11349 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11351 /* Look up the name. */
11352 decl = cp_parser_lookup_name (parser, identifier,
11354 /*is_namespace=*/false,
11355 check_dependency_p);
11360 /* Try a template-id. */
11361 decl = cp_parser_template_id (parser, template_keyword_p,
11362 check_dependency_p,
11364 if (decl == error_mark_node)
11365 return error_mark_node;
11368 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11370 /* If this is a typename, create a TYPENAME_TYPE. */
11371 if (typename_p && decl != error_mark_node)
11372 decl = TYPE_NAME (make_typename_type (scope, decl,
11375 /* Check to see that it is really the name of a class. */
11376 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11377 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11378 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11379 /* Situations like this:
11381 template <typename T> struct A {
11382 typename T::template X<int>::I i;
11385 are problematic. Is `T::template X<int>' a class-name? The
11386 standard does not seem to be definitive, but there is no other
11387 valid interpretation of the following `::'. Therefore, those
11388 names are considered class-names. */
11389 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11390 else if (decl == error_mark_node
11391 || TREE_CODE (decl) != TYPE_DECL
11392 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11394 cp_parser_error (parser, "expected class-name");
11395 return error_mark_node;
11401 /* Parse a class-specifier.
11404 class-head { member-specification [opt] }
11406 Returns the TREE_TYPE representing the class. */
11409 cp_parser_class_specifier (cp_parser* parser)
11413 tree attributes = NULL_TREE;
11414 int has_trailing_semicolon;
11415 bool nested_name_specifier_p;
11416 unsigned saved_num_template_parameter_lists;
11418 push_deferring_access_checks (dk_no_deferred);
11420 /* Parse the class-head. */
11421 type = cp_parser_class_head (parser,
11422 &nested_name_specifier_p);
11423 /* If the class-head was a semantic disaster, skip the entire body
11427 cp_parser_skip_to_end_of_block_or_statement (parser);
11428 pop_deferring_access_checks ();
11429 return error_mark_node;
11432 /* Look for the `{'. */
11433 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11435 pop_deferring_access_checks ();
11436 return error_mark_node;
11439 /* Issue an error message if type-definitions are forbidden here. */
11440 cp_parser_check_type_definition (parser);
11441 /* Remember that we are defining one more class. */
11442 ++parser->num_classes_being_defined;
11443 /* Inside the class, surrounding template-parameter-lists do not
11445 saved_num_template_parameter_lists
11446 = parser->num_template_parameter_lists;
11447 parser->num_template_parameter_lists = 0;
11449 /* Start the class. */
11450 type = begin_class_definition (type);
11451 if (type == error_mark_node)
11452 /* If the type is erroneous, skip the entire body of the class. */
11453 cp_parser_skip_to_closing_brace (parser);
11455 /* Parse the member-specification. */
11456 cp_parser_member_specification_opt (parser);
11457 /* Look for the trailing `}'. */
11458 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11459 /* We get better error messages by noticing a common problem: a
11460 missing trailing `;'. */
11461 token = cp_lexer_peek_token (parser->lexer);
11462 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11463 /* Look for attributes to apply to this class. */
11464 if (cp_parser_allow_gnu_extensions_p (parser))
11465 attributes = cp_parser_attributes_opt (parser);
11466 /* If we got any attributes in class_head, xref_tag will stick them in
11467 TREE_TYPE of the type. Grab them now. */
11468 if (type != error_mark_node)
11470 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11471 TYPE_ATTRIBUTES (type) = NULL_TREE;
11472 type = finish_struct (type, attributes);
11474 if (nested_name_specifier_p)
11475 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11476 /* If this class is not itself within the scope of another class,
11477 then we need to parse the bodies of all of the queued function
11478 definitions. Note that the queued functions defined in a class
11479 are not always processed immediately following the
11480 class-specifier for that class. Consider:
11483 struct B { void f() { sizeof (A); } };
11486 If `f' were processed before the processing of `A' were
11487 completed, there would be no way to compute the size of `A'.
11488 Note that the nesting we are interested in here is lexical --
11489 not the semantic nesting given by TYPE_CONTEXT. In particular,
11492 struct A { struct B; };
11493 struct A::B { void f() { } };
11495 there is no need to delay the parsing of `A::B::f'. */
11496 if (--parser->num_classes_being_defined == 0)
11501 /* In a first pass, parse default arguments to the functions.
11502 Then, in a second pass, parse the bodies of the functions.
11503 This two-phased approach handles cases like:
11511 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11512 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11513 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11514 TREE_PURPOSE (parser->unparsed_functions_queues)
11515 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11517 fn = TREE_VALUE (queue_entry);
11518 /* Make sure that any template parameters are in scope. */
11519 maybe_begin_member_template_processing (fn);
11520 /* If there are default arguments that have not yet been processed,
11521 take care of them now. */
11522 cp_parser_late_parsing_default_args (parser, fn);
11523 /* Remove any template parameters from the symbol table. */
11524 maybe_end_member_template_processing ();
11526 /* Now parse the body of the functions. */
11527 for (TREE_VALUE (parser->unparsed_functions_queues)
11528 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11529 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11530 TREE_VALUE (parser->unparsed_functions_queues)
11531 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11533 /* Figure out which function we need to process. */
11534 fn = TREE_VALUE (queue_entry);
11536 /* Parse the function. */
11537 cp_parser_late_parsing_for_member (parser, fn);
11542 /* Put back any saved access checks. */
11543 pop_deferring_access_checks ();
11545 /* Restore the count of active template-parameter-lists. */
11546 parser->num_template_parameter_lists
11547 = saved_num_template_parameter_lists;
11552 /* Parse a class-head.
11555 class-key identifier [opt] base-clause [opt]
11556 class-key nested-name-specifier identifier base-clause [opt]
11557 class-key nested-name-specifier [opt] template-id
11561 class-key attributes identifier [opt] base-clause [opt]
11562 class-key attributes nested-name-specifier identifier base-clause [opt]
11563 class-key attributes nested-name-specifier [opt] template-id
11566 Returns the TYPE of the indicated class. Sets
11567 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11568 involving a nested-name-specifier was used, and FALSE otherwise.
11570 Returns NULL_TREE if the class-head is syntactically valid, but
11571 semantically invalid in a way that means we should skip the entire
11572 body of the class. */
11575 cp_parser_class_head (cp_parser* parser,
11576 bool* nested_name_specifier_p)
11579 tree nested_name_specifier;
11580 enum tag_types class_key;
11581 tree id = NULL_TREE;
11582 tree type = NULL_TREE;
11584 bool template_id_p = false;
11585 bool qualified_p = false;
11586 bool invalid_nested_name_p = false;
11587 unsigned num_templates;
11589 /* Assume no nested-name-specifier will be present. */
11590 *nested_name_specifier_p = false;
11591 /* Assume no template parameter lists will be used in defining the
11595 /* Look for the class-key. */
11596 class_key = cp_parser_class_key (parser);
11597 if (class_key == none_type)
11598 return error_mark_node;
11600 /* Parse the attributes. */
11601 attributes = cp_parser_attributes_opt (parser);
11603 /* If the next token is `::', that is invalid -- but sometimes
11604 people do try to write:
11608 Handle this gracefully by accepting the extra qualifier, and then
11609 issuing an error about it later if this really is a
11610 class-head. If it turns out just to be an elaborated type
11611 specifier, remain silent. */
11612 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11613 qualified_p = true;
11615 push_deferring_access_checks (dk_no_check);
11617 /* Determine the name of the class. Begin by looking for an
11618 optional nested-name-specifier. */
11619 nested_name_specifier
11620 = cp_parser_nested_name_specifier_opt (parser,
11621 /*typename_keyword_p=*/false,
11622 /*check_dependency_p=*/false,
11624 /*is_declaration=*/false);
11625 /* If there was a nested-name-specifier, then there *must* be an
11627 if (nested_name_specifier)
11629 /* Although the grammar says `identifier', it really means
11630 `class-name' or `template-name'. You are only allowed to
11631 define a class that has already been declared with this
11634 The proposed resolution for Core Issue 180 says that whever
11635 you see `class T::X' you should treat `X' as a type-name.
11637 It is OK to define an inaccessible class; for example:
11639 class A { class B; };
11642 We do not know if we will see a class-name, or a
11643 template-name. We look for a class-name first, in case the
11644 class-name is a template-id; if we looked for the
11645 template-name first we would stop after the template-name. */
11646 cp_parser_parse_tentatively (parser);
11647 type = cp_parser_class_name (parser,
11648 /*typename_keyword_p=*/false,
11649 /*template_keyword_p=*/false,
11651 /*check_dependency_p=*/false,
11652 /*class_head_p=*/true,
11653 /*is_declaration=*/false);
11654 /* If that didn't work, ignore the nested-name-specifier. */
11655 if (!cp_parser_parse_definitely (parser))
11657 invalid_nested_name_p = true;
11658 id = cp_parser_identifier (parser);
11659 if (id == error_mark_node)
11662 /* If we could not find a corresponding TYPE, treat this
11663 declaration like an unqualified declaration. */
11664 if (type == error_mark_node)
11665 nested_name_specifier = NULL_TREE;
11666 /* Otherwise, count the number of templates used in TYPE and its
11667 containing scopes. */
11672 for (scope = TREE_TYPE (type);
11673 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11674 scope = (TYPE_P (scope)
11675 ? TYPE_CONTEXT (scope)
11676 : DECL_CONTEXT (scope)))
11678 && CLASS_TYPE_P (scope)
11679 && CLASSTYPE_TEMPLATE_INFO (scope)
11680 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11681 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11685 /* Otherwise, the identifier is optional. */
11688 /* We don't know whether what comes next is a template-id,
11689 an identifier, or nothing at all. */
11690 cp_parser_parse_tentatively (parser);
11691 /* Check for a template-id. */
11692 id = cp_parser_template_id (parser,
11693 /*template_keyword_p=*/false,
11694 /*check_dependency_p=*/true,
11695 /*is_declaration=*/true);
11696 /* If that didn't work, it could still be an identifier. */
11697 if (!cp_parser_parse_definitely (parser))
11699 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11700 id = cp_parser_identifier (parser);
11706 template_id_p = true;
11711 pop_deferring_access_checks ();
11713 cp_parser_check_for_invalid_template_id (parser, id);
11715 /* If it's not a `:' or a `{' then we can't really be looking at a
11716 class-head, since a class-head only appears as part of a
11717 class-specifier. We have to detect this situation before calling
11718 xref_tag, since that has irreversible side-effects. */
11719 if (!cp_parser_next_token_starts_class_definition_p (parser))
11721 cp_parser_error (parser, "expected `{' or `:'");
11722 return error_mark_node;
11725 /* At this point, we're going ahead with the class-specifier, even
11726 if some other problem occurs. */
11727 cp_parser_commit_to_tentative_parse (parser);
11728 /* Issue the error about the overly-qualified name now. */
11730 cp_parser_error (parser,
11731 "global qualification of class name is invalid");
11732 else if (invalid_nested_name_p)
11733 cp_parser_error (parser,
11734 "qualified name does not name a class");
11735 /* Make sure that the right number of template parameters were
11737 if (!cp_parser_check_template_parameters (parser, num_templates))
11738 /* If something went wrong, there is no point in even trying to
11739 process the class-definition. */
11742 /* Look up the type. */
11745 type = TREE_TYPE (id);
11746 maybe_process_partial_specialization (type);
11748 else if (!nested_name_specifier)
11750 /* If the class was unnamed, create a dummy name. */
11752 id = make_anon_name ();
11753 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11754 parser->num_template_parameter_lists);
11763 template <typename T> struct S { struct T };
11764 template <typename T> struct S<T>::T { };
11766 we will get a TYPENAME_TYPE when processing the definition of
11767 `S::T'. We need to resolve it to the actual type before we
11768 try to define it. */
11769 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11771 class_type = resolve_typename_type (TREE_TYPE (type),
11772 /*only_current_p=*/false);
11773 if (class_type != error_mark_node)
11774 type = TYPE_NAME (class_type);
11777 cp_parser_error (parser, "could not resolve typename type");
11778 type = error_mark_node;
11782 /* Figure out in what scope the declaration is being placed. */
11783 scope = current_scope ();
11785 scope = current_namespace;
11786 /* If that scope does not contain the scope in which the
11787 class was originally declared, the program is invalid. */
11788 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11790 error ("declaration of `%D' in `%D' which does not "
11791 "enclose `%D'", type, scope, nested_name_specifier);
11796 A declarator-id shall not be qualified exception of the
11797 definition of a ... nested class outside of its class
11798 ... [or] a the definition or explicit instantiation of a
11799 class member of a namespace outside of its namespace. */
11800 if (scope == CP_DECL_CONTEXT (type))
11802 pedwarn ("extra qualification ignored");
11803 nested_name_specifier = NULL_TREE;
11806 maybe_process_partial_specialization (TREE_TYPE (type));
11807 class_type = current_class_type;
11808 /* Enter the scope indicated by the nested-name-specifier. */
11809 if (nested_name_specifier)
11810 push_scope (nested_name_specifier);
11811 /* Get the canonical version of this type. */
11812 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11813 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11814 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11815 type = push_template_decl (type);
11816 type = TREE_TYPE (type);
11817 if (nested_name_specifier)
11818 *nested_name_specifier_p = true;
11820 /* Indicate whether this class was declared as a `class' or as a
11822 if (TREE_CODE (type) == RECORD_TYPE)
11823 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11824 cp_parser_check_class_key (class_key, type);
11826 /* Enter the scope containing the class; the names of base classes
11827 should be looked up in that context. For example, given:
11829 struct A { struct B {}; struct C; };
11830 struct A::C : B {};
11833 if (nested_name_specifier)
11834 push_scope (nested_name_specifier);
11835 /* Now, look for the base-clause. */
11836 token = cp_lexer_peek_token (parser->lexer);
11837 if (token->type == CPP_COLON)
11841 /* Get the list of base-classes. */
11842 bases = cp_parser_base_clause (parser);
11843 /* Process them. */
11844 xref_basetypes (type, bases);
11846 /* Leave the scope given by the nested-name-specifier. We will
11847 enter the class scope itself while processing the members. */
11848 if (nested_name_specifier)
11849 pop_scope (nested_name_specifier);
11854 /* Parse a class-key.
11861 Returns the kind of class-key specified, or none_type to indicate
11864 static enum tag_types
11865 cp_parser_class_key (cp_parser* parser)
11868 enum tag_types tag_type;
11870 /* Look for the class-key. */
11871 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11875 /* Check to see if the TOKEN is a class-key. */
11876 tag_type = cp_parser_token_is_class_key (token);
11878 cp_parser_error (parser, "expected class-key");
11882 /* Parse an (optional) member-specification.
11884 member-specification:
11885 member-declaration member-specification [opt]
11886 access-specifier : member-specification [opt] */
11889 cp_parser_member_specification_opt (cp_parser* parser)
11896 /* Peek at the next token. */
11897 token = cp_lexer_peek_token (parser->lexer);
11898 /* If it's a `}', or EOF then we've seen all the members. */
11899 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11902 /* See if this token is a keyword. */
11903 keyword = token->keyword;
11907 case RID_PROTECTED:
11909 /* Consume the access-specifier. */
11910 cp_lexer_consume_token (parser->lexer);
11911 /* Remember which access-specifier is active. */
11912 current_access_specifier = token->value;
11913 /* Look for the `:'. */
11914 cp_parser_require (parser, CPP_COLON, "`:'");
11918 /* Otherwise, the next construction must be a
11919 member-declaration. */
11920 cp_parser_member_declaration (parser);
11925 /* Parse a member-declaration.
11927 member-declaration:
11928 decl-specifier-seq [opt] member-declarator-list [opt] ;
11929 function-definition ; [opt]
11930 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11932 template-declaration
11934 member-declarator-list:
11936 member-declarator-list , member-declarator
11939 declarator pure-specifier [opt]
11940 declarator constant-initializer [opt]
11941 identifier [opt] : constant-expression
11945 member-declaration:
11946 __extension__ member-declaration
11949 declarator attributes [opt] pure-specifier [opt]
11950 declarator attributes [opt] constant-initializer [opt]
11951 identifier [opt] attributes [opt] : constant-expression */
11954 cp_parser_member_declaration (cp_parser* parser)
11956 tree decl_specifiers;
11957 tree prefix_attributes;
11959 int declares_class_or_enum;
11962 int saved_pedantic;
11964 /* Check for the `__extension__' keyword. */
11965 if (cp_parser_extension_opt (parser, &saved_pedantic))
11968 cp_parser_member_declaration (parser);
11969 /* Restore the old value of the PEDANTIC flag. */
11970 pedantic = saved_pedantic;
11975 /* Check for a template-declaration. */
11976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11978 /* Parse the template-declaration. */
11979 cp_parser_template_declaration (parser, /*member_p=*/true);
11984 /* Check for a using-declaration. */
11985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11987 /* Parse the using-declaration. */
11988 cp_parser_using_declaration (parser);
11993 /* We can't tell whether we're looking at a declaration or a
11994 function-definition. */
11995 cp_parser_parse_tentatively (parser);
11997 /* Parse the decl-specifier-seq. */
11999 = cp_parser_decl_specifier_seq (parser,
12000 CP_PARSER_FLAGS_OPTIONAL,
12001 &prefix_attributes,
12002 &declares_class_or_enum);
12003 /* Check for an invalid type-name. */
12004 if (cp_parser_diagnose_invalid_type_name (parser))
12006 /* If there is no declarator, then the decl-specifier-seq should
12008 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12010 /* If there was no decl-specifier-seq, and the next token is a
12011 `;', then we have something like:
12017 Each member-declaration shall declare at least one member
12018 name of the class. */
12019 if (!decl_specifiers)
12022 pedwarn ("extra semicolon");
12028 /* See if this declaration is a friend. */
12029 friend_p = cp_parser_friend_p (decl_specifiers);
12030 /* If there were decl-specifiers, check to see if there was
12031 a class-declaration. */
12032 type = check_tag_decl (decl_specifiers);
12033 /* Nested classes have already been added to the class, but
12034 a `friend' needs to be explicitly registered. */
12037 /* If the `friend' keyword was present, the friend must
12038 be introduced with a class-key. */
12039 if (!declares_class_or_enum)
12040 error ("a class-key must be used when declaring a friend");
12043 template <typename T> struct A {
12044 friend struct A<T>::B;
12047 A<T>::B will be represented by a TYPENAME_TYPE, and
12048 therefore not recognized by check_tag_decl. */
12053 for (specifier = decl_specifiers;
12055 specifier = TREE_CHAIN (specifier))
12057 tree s = TREE_VALUE (specifier);
12059 if (TREE_CODE (s) == IDENTIFIER_NODE)
12060 get_global_value_if_present (s, &type);
12061 if (TREE_CODE (s) == TYPE_DECL)
12071 error ("friend declaration does not name a class or "
12074 make_friend_class (current_class_type, type,
12075 /*complain=*/true);
12077 /* If there is no TYPE, an error message will already have
12081 /* An anonymous aggregate has to be handled specially; such
12082 a declaration really declares a data member (with a
12083 particular type), as opposed to a nested class. */
12084 else if (ANON_AGGR_TYPE_P (type))
12086 /* Remove constructors and such from TYPE, now that we
12087 know it is an anonymous aggregate. */
12088 fixup_anonymous_aggr (type);
12089 /* And make the corresponding data member. */
12090 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12091 /* Add it to the class. */
12092 finish_member_declaration (decl);
12095 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12100 /* See if these declarations will be friends. */
12101 friend_p = cp_parser_friend_p (decl_specifiers);
12103 /* Keep going until we hit the `;' at the end of the
12105 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12107 tree attributes = NULL_TREE;
12108 tree first_attribute;
12110 /* Peek at the next token. */
12111 token = cp_lexer_peek_token (parser->lexer);
12113 /* Check for a bitfield declaration. */
12114 if (token->type == CPP_COLON
12115 || (token->type == CPP_NAME
12116 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12122 /* Get the name of the bitfield. Note that we cannot just
12123 check TOKEN here because it may have been invalidated by
12124 the call to cp_lexer_peek_nth_token above. */
12125 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12126 identifier = cp_parser_identifier (parser);
12128 identifier = NULL_TREE;
12130 /* Consume the `:' token. */
12131 cp_lexer_consume_token (parser->lexer);
12132 /* Get the width of the bitfield. */
12134 = cp_parser_constant_expression (parser,
12135 /*allow_non_constant=*/false,
12138 /* Look for attributes that apply to the bitfield. */
12139 attributes = cp_parser_attributes_opt (parser);
12140 /* Remember which attributes are prefix attributes and
12142 first_attribute = attributes;
12143 /* Combine the attributes. */
12144 attributes = chainon (prefix_attributes, attributes);
12146 /* Create the bitfield declaration. */
12147 decl = grokbitfield (identifier,
12150 /* Apply the attributes. */
12151 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12157 tree asm_specification;
12158 int ctor_dtor_or_conv_p;
12160 /* Parse the declarator. */
12162 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12163 &ctor_dtor_or_conv_p);
12165 /* If something went wrong parsing the declarator, make sure
12166 that we at least consume some tokens. */
12167 if (declarator == error_mark_node)
12169 /* Skip to the end of the statement. */
12170 cp_parser_skip_to_end_of_statement (parser);
12174 cp_parser_check_for_definition_in_return_type
12175 (declarator, declares_class_or_enum);
12177 /* Look for an asm-specification. */
12178 asm_specification = cp_parser_asm_specification_opt (parser);
12179 /* Look for attributes that apply to the declaration. */
12180 attributes = cp_parser_attributes_opt (parser);
12181 /* Remember which attributes are prefix attributes and
12183 first_attribute = attributes;
12184 /* Combine the attributes. */
12185 attributes = chainon (prefix_attributes, attributes);
12187 /* If it's an `=', then we have a constant-initializer or a
12188 pure-specifier. It is not correct to parse the
12189 initializer before registering the member declaration
12190 since the member declaration should be in scope while
12191 its initializer is processed. However, the rest of the
12192 front end does not yet provide an interface that allows
12193 us to handle this correctly. */
12194 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12198 A pure-specifier shall be used only in the declaration of
12199 a virtual function.
12201 A member-declarator can contain a constant-initializer
12202 only if it declares a static member of integral or
12205 Therefore, if the DECLARATOR is for a function, we look
12206 for a pure-specifier; otherwise, we look for a
12207 constant-initializer. When we call `grokfield', it will
12208 perform more stringent semantics checks. */
12209 if (TREE_CODE (declarator) == CALL_EXPR)
12210 initializer = cp_parser_pure_specifier (parser);
12213 /* This declaration cannot be a function
12215 cp_parser_commit_to_tentative_parse (parser);
12216 /* Parse the initializer. */
12217 initializer = cp_parser_constant_initializer (parser);
12220 /* Otherwise, there is no initializer. */
12222 initializer = NULL_TREE;
12224 /* See if we are probably looking at a function
12225 definition. We are certainly not looking at at a
12226 member-declarator. Calling `grokfield' has
12227 side-effects, so we must not do it unless we are sure
12228 that we are looking at a member-declarator. */
12229 if (cp_parser_token_starts_function_definition_p
12230 (cp_lexer_peek_token (parser->lexer)))
12231 decl = error_mark_node;
12234 /* Create the declaration. */
12235 decl = grokfield (declarator, decl_specifiers,
12236 initializer, asm_specification,
12238 /* Any initialization must have been from a
12239 constant-expression. */
12240 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12241 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12245 /* Reset PREFIX_ATTRIBUTES. */
12246 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12247 attributes = TREE_CHAIN (attributes);
12249 TREE_CHAIN (attributes) = NULL_TREE;
12251 /* If there is any qualification still in effect, clear it
12252 now; we will be starting fresh with the next declarator. */
12253 parser->scope = NULL_TREE;
12254 parser->qualifying_scope = NULL_TREE;
12255 parser->object_scope = NULL_TREE;
12256 /* If it's a `,', then there are more declarators. */
12257 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12258 cp_lexer_consume_token (parser->lexer);
12259 /* If the next token isn't a `;', then we have a parse error. */
12260 else if (cp_lexer_next_token_is_not (parser->lexer,
12263 cp_parser_error (parser, "expected `;'");
12264 /* Skip tokens until we find a `;'. */
12265 cp_parser_skip_to_end_of_statement (parser);
12272 /* Add DECL to the list of members. */
12274 finish_member_declaration (decl);
12276 if (TREE_CODE (decl) == FUNCTION_DECL)
12277 cp_parser_save_default_args (parser, decl);
12282 /* If everything went well, look for the `;'. */
12283 if (cp_parser_parse_definitely (parser))
12285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12289 /* Parse the function-definition. */
12290 decl = cp_parser_function_definition (parser, &friend_p);
12291 /* If the member was not a friend, declare it here. */
12293 finish_member_declaration (decl);
12294 /* Peek at the next token. */
12295 token = cp_lexer_peek_token (parser->lexer);
12296 /* If the next token is a semicolon, consume it. */
12297 if (token->type == CPP_SEMICOLON)
12298 cp_lexer_consume_token (parser->lexer);
12301 /* Parse a pure-specifier.
12306 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12307 Otherwise, ERROR_MARK_NODE is returned. */
12310 cp_parser_pure_specifier (cp_parser* parser)
12314 /* Look for the `=' token. */
12315 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12316 return error_mark_node;
12317 /* Look for the `0' token. */
12318 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12319 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12320 to get information from the lexer about how the number was
12321 spelled in order to fix this problem. */
12322 if (!token || !integer_zerop (token->value))
12323 return error_mark_node;
12325 return integer_zero_node;
12328 /* Parse a constant-initializer.
12330 constant-initializer:
12331 = constant-expression
12333 Returns a representation of the constant-expression. */
12336 cp_parser_constant_initializer (cp_parser* parser)
12338 /* Look for the `=' token. */
12339 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12340 return error_mark_node;
12342 /* It is invalid to write:
12344 struct S { static const int i = { 7 }; };
12347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12349 cp_parser_error (parser,
12350 "a brace-enclosed initializer is not allowed here");
12351 /* Consume the opening brace. */
12352 cp_lexer_consume_token (parser->lexer);
12353 /* Skip the initializer. */
12354 cp_parser_skip_to_closing_brace (parser);
12355 /* Look for the trailing `}'. */
12356 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12358 return error_mark_node;
12361 return cp_parser_constant_expression (parser,
12362 /*allow_non_constant=*/false,
12366 /* Derived classes [gram.class.derived] */
12368 /* Parse a base-clause.
12371 : base-specifier-list
12373 base-specifier-list:
12375 base-specifier-list , base-specifier
12377 Returns a TREE_LIST representing the base-classes, in the order in
12378 which they were declared. The representation of each node is as
12379 described by cp_parser_base_specifier.
12381 In the case that no bases are specified, this function will return
12382 NULL_TREE, not ERROR_MARK_NODE. */
12385 cp_parser_base_clause (cp_parser* parser)
12387 tree bases = NULL_TREE;
12389 /* Look for the `:' that begins the list. */
12390 cp_parser_require (parser, CPP_COLON, "`:'");
12392 /* Scan the base-specifier-list. */
12398 /* Look for the base-specifier. */
12399 base = cp_parser_base_specifier (parser);
12400 /* Add BASE to the front of the list. */
12401 if (base != error_mark_node)
12403 TREE_CHAIN (base) = bases;
12406 /* Peek at the next token. */
12407 token = cp_lexer_peek_token (parser->lexer);
12408 /* If it's not a comma, then the list is complete. */
12409 if (token->type != CPP_COMMA)
12411 /* Consume the `,'. */
12412 cp_lexer_consume_token (parser->lexer);
12415 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12416 base class had a qualified name. However, the next name that
12417 appears is certainly not qualified. */
12418 parser->scope = NULL_TREE;
12419 parser->qualifying_scope = NULL_TREE;
12420 parser->object_scope = NULL_TREE;
12422 return nreverse (bases);
12425 /* Parse a base-specifier.
12428 :: [opt] nested-name-specifier [opt] class-name
12429 virtual access-specifier [opt] :: [opt] nested-name-specifier
12431 access-specifier virtual [opt] :: [opt] nested-name-specifier
12434 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12435 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12436 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12437 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12440 cp_parser_base_specifier (cp_parser* parser)
12444 bool virtual_p = false;
12445 bool duplicate_virtual_error_issued_p = false;
12446 bool duplicate_access_error_issued_p = false;
12447 bool class_scope_p, template_p;
12448 tree access = access_default_node;
12451 /* Process the optional `virtual' and `access-specifier'. */
12454 /* Peek at the next token. */
12455 token = cp_lexer_peek_token (parser->lexer);
12456 /* Process `virtual'. */
12457 switch (token->keyword)
12460 /* If `virtual' appears more than once, issue an error. */
12461 if (virtual_p && !duplicate_virtual_error_issued_p)
12463 cp_parser_error (parser,
12464 "`virtual' specified more than once in base-specified");
12465 duplicate_virtual_error_issued_p = true;
12470 /* Consume the `virtual' token. */
12471 cp_lexer_consume_token (parser->lexer);
12476 case RID_PROTECTED:
12478 /* If more than one access specifier appears, issue an
12480 if (access != access_default_node
12481 && !duplicate_access_error_issued_p)
12483 cp_parser_error (parser,
12484 "more than one access specifier in base-specified");
12485 duplicate_access_error_issued_p = true;
12488 access = ridpointers[(int) token->keyword];
12490 /* Consume the access-specifier. */
12491 cp_lexer_consume_token (parser->lexer);
12501 /* Look for the optional `::' operator. */
12502 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12503 /* Look for the nested-name-specifier. The simplest way to
12508 The keyword `typename' is not permitted in a base-specifier or
12509 mem-initializer; in these contexts a qualified name that
12510 depends on a template-parameter is implicitly assumed to be a
12513 is to pretend that we have seen the `typename' keyword at this
12515 cp_parser_nested_name_specifier_opt (parser,
12516 /*typename_keyword_p=*/true,
12517 /*check_dependency_p=*/true,
12519 /*is_declaration=*/true);
12520 /* If the base class is given by a qualified name, assume that names
12521 we see are type names or templates, as appropriate. */
12522 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12523 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12525 /* Finally, look for the class-name. */
12526 type = cp_parser_class_name (parser,
12530 /*check_dependency_p=*/true,
12531 /*class_head_p=*/false,
12532 /*is_declaration=*/true);
12534 if (type == error_mark_node)
12535 return error_mark_node;
12537 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12540 /* Exception handling [gram.exception] */
12542 /* Parse an (optional) exception-specification.
12544 exception-specification:
12545 throw ( type-id-list [opt] )
12547 Returns a TREE_LIST representing the exception-specification. The
12548 TREE_VALUE of each node is a type. */
12551 cp_parser_exception_specification_opt (cp_parser* parser)
12556 /* Peek at the next token. */
12557 token = cp_lexer_peek_token (parser->lexer);
12558 /* If it's not `throw', then there's no exception-specification. */
12559 if (!cp_parser_is_keyword (token, RID_THROW))
12562 /* Consume the `throw'. */
12563 cp_lexer_consume_token (parser->lexer);
12565 /* Look for the `('. */
12566 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12568 /* Peek at the next token. */
12569 token = cp_lexer_peek_token (parser->lexer);
12570 /* If it's not a `)', then there is a type-id-list. */
12571 if (token->type != CPP_CLOSE_PAREN)
12573 const char *saved_message;
12575 /* Types may not be defined in an exception-specification. */
12576 saved_message = parser->type_definition_forbidden_message;
12577 parser->type_definition_forbidden_message
12578 = "types may not be defined in an exception-specification";
12579 /* Parse the type-id-list. */
12580 type_id_list = cp_parser_type_id_list (parser);
12581 /* Restore the saved message. */
12582 parser->type_definition_forbidden_message = saved_message;
12585 type_id_list = empty_except_spec;
12587 /* Look for the `)'. */
12588 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12590 return type_id_list;
12593 /* Parse an (optional) type-id-list.
12597 type-id-list , type-id
12599 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12600 in the order that the types were presented. */
12603 cp_parser_type_id_list (cp_parser* parser)
12605 tree types = NULL_TREE;
12612 /* Get the next type-id. */
12613 type = cp_parser_type_id (parser);
12614 /* Add it to the list. */
12615 types = add_exception_specifier (types, type, /*complain=*/1);
12616 /* Peek at the next token. */
12617 token = cp_lexer_peek_token (parser->lexer);
12618 /* If it is not a `,', we are done. */
12619 if (token->type != CPP_COMMA)
12621 /* Consume the `,'. */
12622 cp_lexer_consume_token (parser->lexer);
12625 return nreverse (types);
12628 /* Parse a try-block.
12631 try compound-statement handler-seq */
12634 cp_parser_try_block (cp_parser* parser)
12638 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12639 try_block = begin_try_block ();
12640 cp_parser_compound_statement (parser, false);
12641 finish_try_block (try_block);
12642 cp_parser_handler_seq (parser);
12643 finish_handler_sequence (try_block);
12648 /* Parse a function-try-block.
12650 function-try-block:
12651 try ctor-initializer [opt] function-body handler-seq */
12654 cp_parser_function_try_block (cp_parser* parser)
12657 bool ctor_initializer_p;
12659 /* Look for the `try' keyword. */
12660 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12662 /* Let the rest of the front-end know where we are. */
12663 try_block = begin_function_try_block ();
12664 /* Parse the function-body. */
12666 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12667 /* We're done with the `try' part. */
12668 finish_function_try_block (try_block);
12669 /* Parse the handlers. */
12670 cp_parser_handler_seq (parser);
12671 /* We're done with the handlers. */
12672 finish_function_handler_sequence (try_block);
12674 return ctor_initializer_p;
12677 /* Parse a handler-seq.
12680 handler handler-seq [opt] */
12683 cp_parser_handler_seq (cp_parser* parser)
12689 /* Parse the handler. */
12690 cp_parser_handler (parser);
12691 /* Peek at the next token. */
12692 token = cp_lexer_peek_token (parser->lexer);
12693 /* If it's not `catch' then there are no more handlers. */
12694 if (!cp_parser_is_keyword (token, RID_CATCH))
12699 /* Parse a handler.
12702 catch ( exception-declaration ) compound-statement */
12705 cp_parser_handler (cp_parser* parser)
12710 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12711 handler = begin_handler ();
12712 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12713 declaration = cp_parser_exception_declaration (parser);
12714 finish_handler_parms (declaration, handler);
12715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12716 cp_parser_compound_statement (parser, false);
12717 finish_handler (handler);
12720 /* Parse an exception-declaration.
12722 exception-declaration:
12723 type-specifier-seq declarator
12724 type-specifier-seq abstract-declarator
12728 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12729 ellipsis variant is used. */
12732 cp_parser_exception_declaration (cp_parser* parser)
12734 tree type_specifiers;
12736 const char *saved_message;
12738 /* If it's an ellipsis, it's easy to handle. */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12741 /* Consume the `...' token. */
12742 cp_lexer_consume_token (parser->lexer);
12746 /* Types may not be defined in exception-declarations. */
12747 saved_message = parser->type_definition_forbidden_message;
12748 parser->type_definition_forbidden_message
12749 = "types may not be defined in exception-declarations";
12751 /* Parse the type-specifier-seq. */
12752 type_specifiers = cp_parser_type_specifier_seq (parser);
12753 /* If it's a `)', then there is no declarator. */
12754 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12755 declarator = NULL_TREE;
12757 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12758 /*ctor_dtor_or_conv_p=*/NULL);
12760 /* Restore the saved message. */
12761 parser->type_definition_forbidden_message = saved_message;
12763 return start_handler_parms (type_specifiers, declarator);
12766 /* Parse a throw-expression.
12769 throw assignment-expression [opt]
12771 Returns a THROW_EXPR representing the throw-expression. */
12774 cp_parser_throw_expression (cp_parser* parser)
12778 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12779 /* We can't be sure if there is an assignment-expression or not. */
12780 cp_parser_parse_tentatively (parser);
12782 expression = cp_parser_assignment_expression (parser);
12783 /* If it didn't work, this is just a rethrow. */
12784 if (!cp_parser_parse_definitely (parser))
12785 expression = NULL_TREE;
12787 return build_throw (expression);
12790 /* GNU Extensions */
12792 /* Parse an (optional) asm-specification.
12795 asm ( string-literal )
12797 If the asm-specification is present, returns a STRING_CST
12798 corresponding to the string-literal. Otherwise, returns
12802 cp_parser_asm_specification_opt (cp_parser* parser)
12805 tree asm_specification;
12807 /* Peek at the next token. */
12808 token = cp_lexer_peek_token (parser->lexer);
12809 /* If the next token isn't the `asm' keyword, then there's no
12810 asm-specification. */
12811 if (!cp_parser_is_keyword (token, RID_ASM))
12814 /* Consume the `asm' token. */
12815 cp_lexer_consume_token (parser->lexer);
12816 /* Look for the `('. */
12817 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12819 /* Look for the string-literal. */
12820 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12822 asm_specification = token->value;
12824 asm_specification = NULL_TREE;
12826 /* Look for the `)'. */
12827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12829 return asm_specification;
12832 /* Parse an asm-operand-list.
12836 asm-operand-list , asm-operand
12839 string-literal ( expression )
12840 [ string-literal ] string-literal ( expression )
12842 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12843 each node is the expression. The TREE_PURPOSE is itself a
12844 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12845 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12846 is a STRING_CST for the string literal before the parenthesis. */
12849 cp_parser_asm_operand_list (cp_parser* parser)
12851 tree asm_operands = NULL_TREE;
12855 tree string_literal;
12860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12862 /* Consume the `[' token. */
12863 cp_lexer_consume_token (parser->lexer);
12864 /* Read the operand name. */
12865 name = cp_parser_identifier (parser);
12866 if (name != error_mark_node)
12867 name = build_string (IDENTIFIER_LENGTH (name),
12868 IDENTIFIER_POINTER (name));
12869 /* Look for the closing `]'. */
12870 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12874 /* Look for the string-literal. */
12875 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12876 string_literal = token ? token->value : error_mark_node;
12877 /* Look for the `('. */
12878 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12879 /* Parse the expression. */
12880 expression = cp_parser_expression (parser);
12881 /* Look for the `)'. */
12882 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12883 /* Add this operand to the list. */
12884 asm_operands = tree_cons (build_tree_list (name, string_literal),
12887 /* If the next token is not a `,', there are no more
12889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12891 /* Consume the `,'. */
12892 cp_lexer_consume_token (parser->lexer);
12895 return nreverse (asm_operands);
12898 /* Parse an asm-clobber-list.
12902 asm-clobber-list , string-literal
12904 Returns a TREE_LIST, indicating the clobbers in the order that they
12905 appeared. The TREE_VALUE of each node is a STRING_CST. */
12908 cp_parser_asm_clobber_list (cp_parser* parser)
12910 tree clobbers = NULL_TREE;
12915 tree string_literal;
12917 /* Look for the string literal. */
12918 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12919 string_literal = token ? token->value : error_mark_node;
12920 /* Add it to the list. */
12921 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12922 /* If the next token is not a `,', then the list is
12924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12926 /* Consume the `,' token. */
12927 cp_lexer_consume_token (parser->lexer);
12933 /* Parse an (optional) series of attributes.
12936 attributes attribute
12939 __attribute__ (( attribute-list [opt] ))
12941 The return value is as for cp_parser_attribute_list. */
12944 cp_parser_attributes_opt (cp_parser* parser)
12946 tree attributes = NULL_TREE;
12951 tree attribute_list;
12953 /* Peek at the next token. */
12954 token = cp_lexer_peek_token (parser->lexer);
12955 /* If it's not `__attribute__', then we're done. */
12956 if (token->keyword != RID_ATTRIBUTE)
12959 /* Consume the `__attribute__' keyword. */
12960 cp_lexer_consume_token (parser->lexer);
12961 /* Look for the two `(' tokens. */
12962 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12963 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12965 /* Peek at the next token. */
12966 token = cp_lexer_peek_token (parser->lexer);
12967 if (token->type != CPP_CLOSE_PAREN)
12968 /* Parse the attribute-list. */
12969 attribute_list = cp_parser_attribute_list (parser);
12971 /* If the next token is a `)', then there is no attribute
12973 attribute_list = NULL;
12975 /* Look for the two `)' tokens. */
12976 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12977 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12979 /* Add these new attributes to the list. */
12980 attributes = chainon (attributes, attribute_list);
12986 /* Parse an attribute-list.
12990 attribute-list , attribute
12994 identifier ( identifier )
12995 identifier ( identifier , expression-list )
12996 identifier ( expression-list )
12998 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12999 TREE_PURPOSE of each node is the identifier indicating which
13000 attribute is in use. The TREE_VALUE represents the arguments, if
13004 cp_parser_attribute_list (cp_parser* parser)
13006 tree attribute_list = NULL_TREE;
13014 /* Look for the identifier. We also allow keywords here; for
13015 example `__attribute__ ((const))' is legal. */
13016 token = cp_lexer_peek_token (parser->lexer);
13017 if (token->type != CPP_NAME
13018 && token->type != CPP_KEYWORD)
13019 return error_mark_node;
13020 /* Consume the token. */
13021 token = cp_lexer_consume_token (parser->lexer);
13023 /* Save away the identifier that indicates which attribute this is. */
13024 identifier = token->value;
13025 attribute = build_tree_list (identifier, NULL_TREE);
13027 /* Peek at the next token. */
13028 token = cp_lexer_peek_token (parser->lexer);
13029 /* If it's an `(', then parse the attribute arguments. */
13030 if (token->type == CPP_OPEN_PAREN)
13034 arguments = (cp_parser_parenthesized_expression_list
13035 (parser, true, /*non_constant_p=*/NULL));
13036 /* Save the identifier and arguments away. */
13037 TREE_VALUE (attribute) = arguments;
13040 /* Add this attribute to the list. */
13041 TREE_CHAIN (attribute) = attribute_list;
13042 attribute_list = attribute;
13044 /* Now, look for more attributes. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 /* If the next token isn't a `,', we're done. */
13047 if (token->type != CPP_COMMA)
13050 /* Consume the comma and keep going. */
13051 cp_lexer_consume_token (parser->lexer);
13054 /* We built up the list in reverse order. */
13055 return nreverse (attribute_list);
13058 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13059 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13060 current value of the PEDANTIC flag, regardless of whether or not
13061 the `__extension__' keyword is present. The caller is responsible
13062 for restoring the value of the PEDANTIC flag. */
13065 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13067 /* Save the old value of the PEDANTIC flag. */
13068 *saved_pedantic = pedantic;
13070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13072 /* Consume the `__extension__' token. */
13073 cp_lexer_consume_token (parser->lexer);
13074 /* We're not being pedantic while the `__extension__' keyword is
13084 /* Parse a label declaration.
13087 __label__ label-declarator-seq ;
13089 label-declarator-seq:
13090 identifier , label-declarator-seq
13094 cp_parser_label_declaration (cp_parser* parser)
13096 /* Look for the `__label__' keyword. */
13097 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13103 /* Look for an identifier. */
13104 identifier = cp_parser_identifier (parser);
13105 /* Declare it as a lobel. */
13106 finish_label_decl (identifier);
13107 /* If the next token is a `;', stop. */
13108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13110 /* Look for the `,' separating the label declarations. */
13111 cp_parser_require (parser, CPP_COMMA, "`,'");
13114 /* Look for the final `;'. */
13115 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13118 /* Support Functions */
13120 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13121 NAME should have one of the representations used for an
13122 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13123 is returned. If PARSER->SCOPE is a dependent type, then a
13124 SCOPE_REF is returned.
13126 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13127 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13128 was formed. Abstractly, such entities should not be passed to this
13129 function, because they do not need to be looked up, but it is
13130 simpler to check for this special case here, rather than at the
13133 In cases not explicitly covered above, this function returns a
13134 DECL, OVERLOAD, or baselink representing the result of the lookup.
13135 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13138 If IS_TYPE is TRUE, bindings that do not refer to types are
13141 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13144 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13148 cp_parser_lookup_name (cp_parser *parser, tree name,
13149 bool is_type, bool is_namespace, bool check_dependency)
13152 tree object_type = parser->context->object_type;
13154 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13155 no longer valid. Note that if we are parsing tentatively, and
13156 the parse fails, OBJECT_TYPE will be automatically restored. */
13157 parser->context->object_type = NULL_TREE;
13159 if (name == error_mark_node)
13160 return error_mark_node;
13162 /* A template-id has already been resolved; there is no lookup to
13164 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13166 if (BASELINK_P (name))
13168 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13169 == TEMPLATE_ID_EXPR),
13174 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13175 it should already have been checked to make sure that the name
13176 used matches the type being destroyed. */
13177 if (TREE_CODE (name) == BIT_NOT_EXPR)
13181 /* Figure out to which type this destructor applies. */
13183 type = parser->scope;
13184 else if (object_type)
13185 type = object_type;
13187 type = current_class_type;
13188 /* If that's not a class type, there is no destructor. */
13189 if (!type || !CLASS_TYPE_P (type))
13190 return error_mark_node;
13191 /* If it was a class type, return the destructor. */
13192 return CLASSTYPE_DESTRUCTORS (type);
13195 /* By this point, the NAME should be an ordinary identifier. If
13196 the id-expression was a qualified name, the qualifying scope is
13197 stored in PARSER->SCOPE at this point. */
13198 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13201 /* Perform the lookup. */
13206 if (parser->scope == error_mark_node)
13207 return error_mark_node;
13209 /* If the SCOPE is dependent, the lookup must be deferred until
13210 the template is instantiated -- unless we are explicitly
13211 looking up names in uninstantiated templates. Even then, we
13212 cannot look up the name if the scope is not a class type; it
13213 might, for example, be a template type parameter. */
13214 dependent_p = (TYPE_P (parser->scope)
13215 && !(parser->in_declarator_p
13216 && currently_open_class (parser->scope))
13217 && dependent_type_p (parser->scope));
13218 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13222 decl = build_nt (SCOPE_REF, parser->scope, name);
13224 /* The resolution to Core Issue 180 says that `struct A::B'
13225 should be considered a type-name, even if `A' is
13227 decl = TYPE_NAME (make_typename_type (parser->scope,
13233 /* If PARSER->SCOPE is a dependent type, then it must be a
13234 class type, and we must not be checking dependencies;
13235 otherwise, we would have processed this lookup above. So
13236 that PARSER->SCOPE is not considered a dependent base by
13237 lookup_member, we must enter the scope here. */
13239 push_scope (parser->scope);
13240 /* If the PARSER->SCOPE is a a template specialization, it
13241 may be instantiated during name lookup. In that case,
13242 errors may be issued. Even if we rollback the current
13243 tentative parse, those errors are valid. */
13244 decl = lookup_qualified_name (parser->scope, name, is_type,
13245 /*complain=*/true);
13247 pop_scope (parser->scope);
13249 parser->qualifying_scope = parser->scope;
13250 parser->object_scope = NULL_TREE;
13252 else if (object_type)
13254 tree object_decl = NULL_TREE;
13255 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13256 OBJECT_TYPE is not a class. */
13257 if (CLASS_TYPE_P (object_type))
13258 /* If the OBJECT_TYPE is a template specialization, it may
13259 be instantiated during name lookup. In that case, errors
13260 may be issued. Even if we rollback the current tentative
13261 parse, those errors are valid. */
13262 object_decl = lookup_member (object_type,
13264 /*protect=*/0, is_type);
13265 /* Look it up in the enclosing context, too. */
13266 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13269 parser->object_scope = object_type;
13270 parser->qualifying_scope = NULL_TREE;
13272 decl = object_decl;
13276 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13279 parser->qualifying_scope = NULL_TREE;
13280 parser->object_scope = NULL_TREE;
13283 /* If the lookup failed, let our caller know. */
13285 || decl == error_mark_node
13286 || (TREE_CODE (decl) == FUNCTION_DECL
13287 && DECL_ANTICIPATED (decl)))
13288 return error_mark_node;
13290 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13291 if (TREE_CODE (decl) == TREE_LIST)
13293 /* The error message we have to print is too complicated for
13294 cp_parser_error, so we incorporate its actions directly. */
13295 if (!cp_parser_simulate_error (parser))
13297 error ("reference to `%D' is ambiguous", name);
13298 print_candidates (decl);
13300 return error_mark_node;
13303 my_friendly_assert (DECL_P (decl)
13304 || TREE_CODE (decl) == OVERLOAD
13305 || TREE_CODE (decl) == SCOPE_REF
13306 || BASELINK_P (decl),
13309 /* If we have resolved the name of a member declaration, check to
13310 see if the declaration is accessible. When the name resolves to
13311 set of overloaded functions, accessibility is checked when
13312 overload resolution is done.
13314 During an explicit instantiation, access is not checked at all,
13315 as per [temp.explicit]. */
13317 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13322 /* Like cp_parser_lookup_name, but for use in the typical case where
13323 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13327 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13329 return cp_parser_lookup_name (parser, name,
13331 /*is_namespace=*/false,
13332 /*check_dependency=*/true);
13335 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13336 the current context, return the TYPE_DECL. If TAG_NAME_P is
13337 true, the DECL indicates the class being defined in a class-head,
13338 or declared in an elaborated-type-specifier.
13340 Otherwise, return DECL. */
13343 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13345 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13346 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13349 template <typename T> struct B;
13352 template <typename T> struct A::B {};
13354 Similarly, in a elaborated-type-specifier:
13356 namespace N { struct X{}; }
13359 template <typename T> friend struct N::X;
13362 However, if the DECL refers to a class type, and we are in
13363 the scope of the class, then the name lookup automatically
13364 finds the TYPE_DECL created by build_self_reference rather
13365 than a TEMPLATE_DECL. For example, in:
13367 template <class T> struct S {
13371 there is no need to handle such case. */
13373 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13374 return DECL_TEMPLATE_RESULT (decl);
13379 /* If too many, or too few, template-parameter lists apply to the
13380 declarator, issue an error message. Returns TRUE if all went well,
13381 and FALSE otherwise. */
13384 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13387 unsigned num_templates;
13389 /* We haven't seen any classes that involve template parameters yet. */
13392 switch (TREE_CODE (declarator))
13399 tree main_declarator = TREE_OPERAND (declarator, 0);
13401 cp_parser_check_declarator_template_parameters (parser,
13410 scope = TREE_OPERAND (declarator, 0);
13411 member = TREE_OPERAND (declarator, 1);
13413 /* If this is a pointer-to-member, then we are not interested
13414 in the SCOPE, because it does not qualify the thing that is
13416 if (TREE_CODE (member) == INDIRECT_REF)
13417 return (cp_parser_check_declarator_template_parameters
13420 while (scope && CLASS_TYPE_P (scope))
13422 /* You're supposed to have one `template <...>'
13423 for every template class, but you don't need one
13424 for a full specialization. For example:
13426 template <class T> struct S{};
13427 template <> struct S<int> { void f(); };
13428 void S<int>::f () {}
13430 is correct; there shouldn't be a `template <>' for
13431 the definition of `S<int>::f'. */
13432 if (CLASSTYPE_TEMPLATE_INFO (scope)
13433 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13434 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13435 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13438 scope = TYPE_CONTEXT (scope);
13442 /* Fall through. */
13445 /* If the DECLARATOR has the form `X<y>' then it uses one
13446 additional level of template parameters. */
13447 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13450 return cp_parser_check_template_parameters (parser,
13455 /* NUM_TEMPLATES were used in the current declaration. If that is
13456 invalid, return FALSE and issue an error messages. Otherwise,
13460 cp_parser_check_template_parameters (cp_parser* parser,
13461 unsigned num_templates)
13463 /* If there are more template classes than parameter lists, we have
13466 template <class T> void S<T>::R<T>::f (); */
13467 if (parser->num_template_parameter_lists < num_templates)
13469 error ("too few template-parameter-lists");
13472 /* If there are the same number of template classes and parameter
13473 lists, that's OK. */
13474 if (parser->num_template_parameter_lists == num_templates)
13476 /* If there are more, but only one more, then we are referring to a
13477 member template. That's OK too. */
13478 if (parser->num_template_parameter_lists == num_templates + 1)
13480 /* Otherwise, there are too many template parameter lists. We have
13483 template <class T> template <class U> void S::f(); */
13484 error ("too many template-parameter-lists");
13488 /* Parse a binary-expression of the general form:
13492 binary-expression <token> <expr>
13494 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13495 to parser the <expr>s. If the first production is used, then the
13496 value returned by FN is returned directly. Otherwise, a node with
13497 the indicated EXPR_TYPE is returned, with operands corresponding to
13498 the two sub-expressions. */
13501 cp_parser_binary_expression (cp_parser* parser,
13502 const cp_parser_token_tree_map token_tree_map,
13503 cp_parser_expression_fn fn)
13507 /* Parse the first expression. */
13508 lhs = (*fn) (parser);
13509 /* Now, look for more expressions. */
13513 const cp_parser_token_tree_map_node *map_node;
13516 /* Peek at the next token. */
13517 token = cp_lexer_peek_token (parser->lexer);
13518 /* If the token is `>', and that's not an operator at the
13519 moment, then we're done. */
13520 if (token->type == CPP_GREATER
13521 && !parser->greater_than_is_operator_p)
13523 /* If we find one of the tokens we want, build the corresponding
13524 tree representation. */
13525 for (map_node = token_tree_map;
13526 map_node->token_type != CPP_EOF;
13528 if (map_node->token_type == token->type)
13530 /* Consume the operator token. */
13531 cp_lexer_consume_token (parser->lexer);
13532 /* Parse the right-hand side of the expression. */
13533 rhs = (*fn) (parser);
13534 /* Build the binary tree node. */
13535 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13539 /* If the token wasn't one of the ones we want, we're done. */
13540 if (map_node->token_type == CPP_EOF)
13547 /* Parse an optional `::' token indicating that the following name is
13548 from the global namespace. If so, PARSER->SCOPE is set to the
13549 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13550 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13551 Returns the new value of PARSER->SCOPE, if the `::' token is
13552 present, and NULL_TREE otherwise. */
13555 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13559 /* Peek at the next token. */
13560 token = cp_lexer_peek_token (parser->lexer);
13561 /* If we're looking at a `::' token then we're starting from the
13562 global namespace, not our current location. */
13563 if (token->type == CPP_SCOPE)
13565 /* Consume the `::' token. */
13566 cp_lexer_consume_token (parser->lexer);
13567 /* Set the SCOPE so that we know where to start the lookup. */
13568 parser->scope = global_namespace;
13569 parser->qualifying_scope = global_namespace;
13570 parser->object_scope = NULL_TREE;
13572 return parser->scope;
13574 else if (!current_scope_valid_p)
13576 parser->scope = NULL_TREE;
13577 parser->qualifying_scope = NULL_TREE;
13578 parser->object_scope = NULL_TREE;
13584 /* Returns TRUE if the upcoming token sequence is the start of a
13585 constructor declarator. If FRIEND_P is true, the declarator is
13586 preceded by the `friend' specifier. */
13589 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13591 bool constructor_p;
13592 tree type_decl = NULL_TREE;
13593 bool nested_name_p;
13594 cp_token *next_token;
13596 /* The common case is that this is not a constructor declarator, so
13597 try to avoid doing lots of work if at all possible. It's not
13598 valid declare a constructor at function scope. */
13599 if (at_function_scope_p ())
13601 /* And only certain tokens can begin a constructor declarator. */
13602 next_token = cp_lexer_peek_token (parser->lexer);
13603 if (next_token->type != CPP_NAME
13604 && next_token->type != CPP_SCOPE
13605 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13606 && next_token->type != CPP_TEMPLATE_ID)
13609 /* Parse tentatively; we are going to roll back all of the tokens
13611 cp_parser_parse_tentatively (parser);
13612 /* Assume that we are looking at a constructor declarator. */
13613 constructor_p = true;
13615 /* Look for the optional `::' operator. */
13616 cp_parser_global_scope_opt (parser,
13617 /*current_scope_valid_p=*/false);
13618 /* Look for the nested-name-specifier. */
13620 = (cp_parser_nested_name_specifier_opt (parser,
13621 /*typename_keyword_p=*/false,
13622 /*check_dependency_p=*/false,
13624 /*is_declaration=*/false)
13626 /* Outside of a class-specifier, there must be a
13627 nested-name-specifier. */
13628 if (!nested_name_p &&
13629 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13631 constructor_p = false;
13632 /* If we still think that this might be a constructor-declarator,
13633 look for a class-name. */
13638 template <typename T> struct S { S(); };
13639 template <typename T> S<T>::S ();
13641 we must recognize that the nested `S' names a class.
13644 template <typename T> S<T>::S<T> ();
13646 we must recognize that the nested `S' names a template. */
13647 type_decl = cp_parser_class_name (parser,
13648 /*typename_keyword_p=*/false,
13649 /*template_keyword_p=*/false,
13651 /*check_dependency_p=*/false,
13652 /*class_head_p=*/false,
13653 /*is_declaration=*/false);
13654 /* If there was no class-name, then this is not a constructor. */
13655 constructor_p = !cp_parser_error_occurred (parser);
13658 /* If we're still considering a constructor, we have to see a `(',
13659 to begin the parameter-declaration-clause, followed by either a
13660 `)', an `...', or a decl-specifier. We need to check for a
13661 type-specifier to avoid being fooled into thinking that:
13665 is a constructor. (It is actually a function named `f' that
13666 takes one parameter (of type `int') and returns a value of type
13669 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13672 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13673 && !cp_parser_storage_class_specifier_opt (parser))
13676 unsigned saved_num_template_parameter_lists;
13678 /* Names appearing in the type-specifier should be looked up
13679 in the scope of the class. */
13680 if (current_class_type)
13684 type = TREE_TYPE (type_decl);
13685 if (TREE_CODE (type) == TYPENAME_TYPE)
13687 type = resolve_typename_type (type,
13688 /*only_current_p=*/false);
13689 if (type == error_mark_node)
13691 cp_parser_abort_tentative_parse (parser);
13698 /* Inside the constructor parameter list, surrounding
13699 template-parameter-lists do not apply. */
13700 saved_num_template_parameter_lists
13701 = parser->num_template_parameter_lists;
13702 parser->num_template_parameter_lists = 0;
13704 /* Look for the type-specifier. */
13705 cp_parser_type_specifier (parser,
13706 CP_PARSER_FLAGS_NONE,
13707 /*is_friend=*/false,
13708 /*is_declarator=*/true,
13709 /*declares_class_or_enum=*/NULL,
13710 /*is_cv_qualifier=*/NULL);
13712 parser->num_template_parameter_lists
13713 = saved_num_template_parameter_lists;
13715 /* Leave the scope of the class. */
13719 constructor_p = !cp_parser_error_occurred (parser);
13723 constructor_p = false;
13724 /* We did not really want to consume any tokens. */
13725 cp_parser_abort_tentative_parse (parser);
13727 return constructor_p;
13730 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13731 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13732 they must be performed once we are in the scope of the function.
13734 Returns the function defined. */
13737 cp_parser_function_definition_from_specifiers_and_declarator
13738 (cp_parser* parser,
13739 tree decl_specifiers,
13746 /* Begin the function-definition. */
13747 success_p = begin_function_definition (decl_specifiers,
13751 /* If there were names looked up in the decl-specifier-seq that we
13752 did not check, check them now. We must wait until we are in the
13753 scope of the function to perform the checks, since the function
13754 might be a friend. */
13755 perform_deferred_access_checks ();
13759 /* If begin_function_definition didn't like the definition, skip
13760 the entire function. */
13761 error ("invalid function declaration");
13762 cp_parser_skip_to_end_of_block_or_statement (parser);
13763 fn = error_mark_node;
13766 fn = cp_parser_function_definition_after_declarator (parser,
13767 /*inline_p=*/false);
13772 /* Parse the part of a function-definition that follows the
13773 declarator. INLINE_P is TRUE iff this function is an inline
13774 function defined with a class-specifier.
13776 Returns the function defined. */
13779 cp_parser_function_definition_after_declarator (cp_parser* parser,
13783 bool ctor_initializer_p = false;
13784 bool saved_in_unbraced_linkage_specification_p;
13785 unsigned saved_num_template_parameter_lists;
13787 /* If the next token is `return', then the code may be trying to
13788 make use of the "named return value" extension that G++ used to
13790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13792 /* Consume the `return' keyword. */
13793 cp_lexer_consume_token (parser->lexer);
13794 /* Look for the identifier that indicates what value is to be
13796 cp_parser_identifier (parser);
13797 /* Issue an error message. */
13798 error ("named return values are no longer supported");
13799 /* Skip tokens until we reach the start of the function body. */
13800 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13801 cp_lexer_consume_token (parser->lexer);
13803 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13804 anything declared inside `f'. */
13805 saved_in_unbraced_linkage_specification_p
13806 = parser->in_unbraced_linkage_specification_p;
13807 parser->in_unbraced_linkage_specification_p = false;
13808 /* Inside the function, surrounding template-parameter-lists do not
13810 saved_num_template_parameter_lists
13811 = parser->num_template_parameter_lists;
13812 parser->num_template_parameter_lists = 0;
13813 /* If the next token is `try', then we are looking at a
13814 function-try-block. */
13815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13816 ctor_initializer_p = cp_parser_function_try_block (parser);
13817 /* A function-try-block includes the function-body, so we only do
13818 this next part if we're not processing a function-try-block. */
13821 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13823 /* Finish the function. */
13824 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13825 (inline_p ? 2 : 0));
13826 /* Generate code for it, if necessary. */
13827 expand_or_defer_fn (fn);
13828 /* Restore the saved values. */
13829 parser->in_unbraced_linkage_specification_p
13830 = saved_in_unbraced_linkage_specification_p;
13831 parser->num_template_parameter_lists
13832 = saved_num_template_parameter_lists;
13837 /* Parse a template-declaration, assuming that the `export' (and
13838 `extern') keywords, if present, has already been scanned. MEMBER_P
13839 is as for cp_parser_template_declaration. */
13842 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13844 tree decl = NULL_TREE;
13845 tree parameter_list;
13846 bool friend_p = false;
13848 /* Look for the `template' keyword. */
13849 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13853 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13856 /* Parse the template parameters. */
13857 begin_template_parm_list ();
13858 /* If the next token is `>', then we have an invalid
13859 specialization. Rather than complain about an invalid template
13860 parameter, issue an error message here. */
13861 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13863 cp_parser_error (parser, "invalid explicit specialization");
13864 parameter_list = NULL_TREE;
13867 parameter_list = cp_parser_template_parameter_list (parser);
13868 parameter_list = end_template_parm_list (parameter_list);
13869 /* Look for the `>'. */
13870 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13871 /* We just processed one more parameter list. */
13872 ++parser->num_template_parameter_lists;
13873 /* If the next token is `template', there are more template
13875 if (cp_lexer_next_token_is_keyword (parser->lexer,
13877 cp_parser_template_declaration_after_export (parser, member_p);
13880 decl = cp_parser_single_declaration (parser,
13884 /* If this is a member template declaration, let the front
13886 if (member_p && !friend_p && decl)
13888 if (TREE_CODE (decl) == TYPE_DECL)
13889 cp_parser_check_access_in_redeclaration (decl);
13891 decl = finish_member_template_decl (decl);
13893 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13894 make_friend_class (current_class_type, TREE_TYPE (decl),
13895 /*complain=*/true);
13897 /* We are done with the current parameter list. */
13898 --parser->num_template_parameter_lists;
13901 finish_template_decl (parameter_list);
13903 /* Register member declarations. */
13904 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13905 finish_member_declaration (decl);
13907 /* If DECL is a function template, we must return to parse it later.
13908 (Even though there is no definition, there might be default
13909 arguments that need handling.) */
13910 if (member_p && decl
13911 && (TREE_CODE (decl) == FUNCTION_DECL
13912 || DECL_FUNCTION_TEMPLATE_P (decl)))
13913 TREE_VALUE (parser->unparsed_functions_queues)
13914 = tree_cons (NULL_TREE, decl,
13915 TREE_VALUE (parser->unparsed_functions_queues));
13918 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13919 `function-definition' sequence. MEMBER_P is true, this declaration
13920 appears in a class scope.
13922 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13923 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13926 cp_parser_single_declaration (cp_parser* parser,
13930 int declares_class_or_enum;
13931 tree decl = NULL_TREE;
13932 tree decl_specifiers;
13935 /* Parse the dependent declaration. We don't know yet
13936 whether it will be a function-definition. */
13937 cp_parser_parse_tentatively (parser);
13938 /* Defer access checks until we know what is being declared. */
13939 push_deferring_access_checks (dk_deferred);
13941 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13944 = cp_parser_decl_specifier_seq (parser,
13945 CP_PARSER_FLAGS_OPTIONAL,
13947 &declares_class_or_enum);
13948 /* Gather up the access checks that occurred the
13949 decl-specifier-seq. */
13950 stop_deferring_access_checks ();
13952 /* Check for the declaration of a template class. */
13953 if (declares_class_or_enum)
13955 if (cp_parser_declares_only_class_p (parser))
13957 decl = shadow_tag (decl_specifiers);
13959 decl = TYPE_NAME (decl);
13961 decl = error_mark_node;
13966 /* If it's not a template class, try for a template function. If
13967 the next token is a `;', then this declaration does not declare
13968 anything. But, if there were errors in the decl-specifiers, then
13969 the error might well have come from an attempted class-specifier.
13970 In that case, there's no need to warn about a missing declarator. */
13972 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13973 || !value_member (error_mark_node, decl_specifiers)))
13974 decl = cp_parser_init_declarator (parser,
13977 /*function_definition_allowed_p=*/false,
13979 declares_class_or_enum,
13980 /*function_definition_p=*/NULL);
13982 pop_deferring_access_checks ();
13984 /* Clear any current qualification; whatever comes next is the start
13985 of something new. */
13986 parser->scope = NULL_TREE;
13987 parser->qualifying_scope = NULL_TREE;
13988 parser->object_scope = NULL_TREE;
13989 /* Look for a trailing `;' after the declaration. */
13990 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13991 && cp_parser_committed_to_tentative_parse (parser))
13992 cp_parser_skip_to_end_of_block_or_statement (parser);
13993 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13994 if (cp_parser_parse_definitely (parser))
13997 *friend_p = cp_parser_friend_p (decl_specifiers);
13999 /* Otherwise, try a function-definition. */
14001 decl = cp_parser_function_definition (parser, friend_p);
14006 /* Parse a cast-expression that is not the operand of a unary "&". */
14009 cp_parser_simple_cast_expression (cp_parser *parser)
14011 return cp_parser_cast_expression (parser, /*address_p=*/false);
14014 /* Parse a functional cast to TYPE. Returns an expression
14015 representing the cast. */
14018 cp_parser_functional_cast (cp_parser* parser, tree type)
14020 tree expression_list;
14023 = cp_parser_parenthesized_expression_list (parser, false,
14024 /*non_constant_p=*/NULL);
14026 return build_functional_cast (type, expression_list);
14029 /* Parse a template-argument-list, as well as the trailing ">" (but
14030 not the opening ">"). See cp_parser_template_argument_list for the
14034 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14038 tree saved_qualifying_scope;
14039 tree saved_object_scope;
14040 bool saved_greater_than_is_operator_p;
14044 When parsing a template-id, the first non-nested `>' is taken as
14045 the end of the template-argument-list rather than a greater-than
14047 saved_greater_than_is_operator_p
14048 = parser->greater_than_is_operator_p;
14049 parser->greater_than_is_operator_p = false;
14050 /* Parsing the argument list may modify SCOPE, so we save it
14052 saved_scope = parser->scope;
14053 saved_qualifying_scope = parser->qualifying_scope;
14054 saved_object_scope = parser->object_scope;
14055 /* Parse the template-argument-list itself. */
14056 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14057 arguments = NULL_TREE;
14059 arguments = cp_parser_template_argument_list (parser);
14060 /* Look for the `>' that ends the template-argument-list. */
14061 cp_parser_require (parser, CPP_GREATER, "`>'");
14062 /* The `>' token might be a greater-than operator again now. */
14063 parser->greater_than_is_operator_p
14064 = saved_greater_than_is_operator_p;
14065 /* Restore the SAVED_SCOPE. */
14066 parser->scope = saved_scope;
14067 parser->qualifying_scope = saved_qualifying_scope;
14068 parser->object_scope = saved_object_scope;
14074 /* MEMBER_FUNCTION is a member function, or a friend. If default
14075 arguments, or the body of the function have not yet been parsed,
14079 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14081 cp_lexer *saved_lexer;
14083 /* If this member is a template, get the underlying
14085 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14086 member_function = DECL_TEMPLATE_RESULT (member_function);
14088 /* There should not be any class definitions in progress at this
14089 point; the bodies of members are only parsed outside of all class
14091 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14092 /* While we're parsing the member functions we might encounter more
14093 classes. We want to handle them right away, but we don't want
14094 them getting mixed up with functions that are currently in the
14096 parser->unparsed_functions_queues
14097 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14099 /* Make sure that any template parameters are in scope. */
14100 maybe_begin_member_template_processing (member_function);
14102 /* If the body of the function has not yet been parsed, parse it
14104 if (DECL_PENDING_INLINE_P (member_function))
14106 tree function_scope;
14107 cp_token_cache *tokens;
14109 /* The function is no longer pending; we are processing it. */
14110 tokens = DECL_PENDING_INLINE_INFO (member_function);
14111 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14112 DECL_PENDING_INLINE_P (member_function) = 0;
14113 /* If this was an inline function in a local class, enter the scope
14114 of the containing function. */
14115 function_scope = decl_function_context (member_function);
14116 if (function_scope)
14117 push_function_context_to (function_scope);
14119 /* Save away the current lexer. */
14120 saved_lexer = parser->lexer;
14121 /* Make a new lexer to feed us the tokens saved for this function. */
14122 parser->lexer = cp_lexer_new_from_tokens (tokens);
14123 parser->lexer->next = saved_lexer;
14125 /* Set the current source position to be the location of the first
14126 token in the saved inline body. */
14127 cp_lexer_peek_token (parser->lexer);
14129 /* Let the front end know that we going to be defining this
14131 start_function (NULL_TREE, member_function, NULL_TREE,
14132 SF_PRE_PARSED | SF_INCLASS_INLINE);
14134 /* Now, parse the body of the function. */
14135 cp_parser_function_definition_after_declarator (parser,
14136 /*inline_p=*/true);
14138 /* Leave the scope of the containing function. */
14139 if (function_scope)
14140 pop_function_context_from (function_scope);
14141 /* Restore the lexer. */
14142 parser->lexer = saved_lexer;
14145 /* Remove any template parameters from the symbol table. */
14146 maybe_end_member_template_processing ();
14148 /* Restore the queue. */
14149 parser->unparsed_functions_queues
14150 = TREE_CHAIN (parser->unparsed_functions_queues);
14153 /* If DECL contains any default args, remember it on the unparsed
14154 functions queue. */
14157 cp_parser_save_default_args (cp_parser* parser, tree decl)
14161 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14163 probe = TREE_CHAIN (probe))
14164 if (TREE_PURPOSE (probe))
14166 TREE_PURPOSE (parser->unparsed_functions_queues)
14167 = tree_cons (NULL_TREE, decl,
14168 TREE_PURPOSE (parser->unparsed_functions_queues));
14174 /* FN is a FUNCTION_DECL which may contains a parameter with an
14175 unparsed DEFAULT_ARG. Parse the default args now. */
14178 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14180 cp_lexer *saved_lexer;
14181 cp_token_cache *tokens;
14182 bool saved_local_variables_forbidden_p;
14185 /* While we're parsing the default args, we might (due to the
14186 statement expression extension) encounter more classes. We want
14187 to handle them right away, but we don't want them getting mixed
14188 up with default args that are currently in the queue. */
14189 parser->unparsed_functions_queues
14190 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14192 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14194 parameters = TREE_CHAIN (parameters))
14196 if (!TREE_PURPOSE (parameters)
14197 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14200 /* Save away the current lexer. */
14201 saved_lexer = parser->lexer;
14202 /* Create a new one, using the tokens we have saved. */
14203 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14204 parser->lexer = cp_lexer_new_from_tokens (tokens);
14206 /* Set the current source position to be the location of the
14207 first token in the default argument. */
14208 cp_lexer_peek_token (parser->lexer);
14210 /* Local variable names (and the `this' keyword) may not appear
14211 in a default argument. */
14212 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14213 parser->local_variables_forbidden_p = true;
14214 /* Parse the assignment-expression. */
14215 if (DECL_CONTEXT (fn))
14216 push_nested_class (DECL_CONTEXT (fn));
14217 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14218 if (DECL_CONTEXT (fn))
14219 pop_nested_class ();
14221 /* Restore saved state. */
14222 parser->lexer = saved_lexer;
14223 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14226 /* Restore the queue. */
14227 parser->unparsed_functions_queues
14228 = TREE_CHAIN (parser->unparsed_functions_queues);
14231 /* Parse the operand of `sizeof' (or a similar operator). Returns
14232 either a TYPE or an expression, depending on the form of the
14233 input. The KEYWORD indicates which kind of expression we have
14237 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14239 static const char *format;
14240 tree expr = NULL_TREE;
14241 const char *saved_message;
14242 bool saved_constant_expression_p;
14244 /* Initialize FORMAT the first time we get here. */
14246 format = "types may not be defined in `%s' expressions";
14248 /* Types cannot be defined in a `sizeof' expression. Save away the
14250 saved_message = parser->type_definition_forbidden_message;
14251 /* And create the new one. */
14252 parser->type_definition_forbidden_message
14253 = xmalloc (strlen (format)
14254 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14256 sprintf ((char *) parser->type_definition_forbidden_message,
14257 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14259 /* The restrictions on constant-expressions do not apply inside
14260 sizeof expressions. */
14261 saved_constant_expression_p = parser->constant_expression_p;
14262 parser->constant_expression_p = false;
14264 /* Do not actually evaluate the expression. */
14266 /* If it's a `(', then we might be looking at the type-id
14268 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14272 /* We can't be sure yet whether we're looking at a type-id or an
14274 cp_parser_parse_tentatively (parser);
14275 /* Consume the `('. */
14276 cp_lexer_consume_token (parser->lexer);
14277 /* Parse the type-id. */
14278 type = cp_parser_type_id (parser);
14279 /* Now, look for the trailing `)'. */
14280 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14281 /* If all went well, then we're done. */
14282 if (cp_parser_parse_definitely (parser))
14284 /* Build a list of decl-specifiers; right now, we have only
14285 a single type-specifier. */
14286 type = build_tree_list (NULL_TREE,
14289 /* Call grokdeclarator to figure out what type this is. */
14290 expr = grokdeclarator (NULL_TREE,
14294 /*attrlist=*/NULL);
14298 /* If the type-id production did not work out, then we must be
14299 looking at the unary-expression production. */
14301 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14302 /* Go back to evaluating expressions. */
14305 /* Free the message we created. */
14306 free ((char *) parser->type_definition_forbidden_message);
14307 /* And restore the old one. */
14308 parser->type_definition_forbidden_message = saved_message;
14309 parser->constant_expression_p = saved_constant_expression_p;
14314 /* If the current declaration has no declarator, return true. */
14317 cp_parser_declares_only_class_p (cp_parser *parser)
14319 /* If the next token is a `;' or a `,' then there is no
14321 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14322 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14325 /* Simplify EXPR if it is a non-dependent expression. Returns the
14326 (possibly simplified) expression. */
14329 cp_parser_fold_non_dependent_expr (tree expr)
14331 /* If we're in a template, but EXPR isn't value dependent, simplify
14332 it. We're supposed to treat:
14334 template <typename T> void f(T[1 + 1]);
14335 template <typename T> void f(T[2]);
14337 as two declarations of the same function, for example. */
14338 if (processing_template_decl
14339 && !type_dependent_expression_p (expr)
14340 && !value_dependent_expression_p (expr))
14342 HOST_WIDE_INT saved_processing_template_decl;
14344 saved_processing_template_decl = processing_template_decl;
14345 processing_template_decl = 0;
14346 expr = tsubst_copy_and_build (expr,
14347 /*args=*/NULL_TREE,
14349 /*in_decl=*/NULL_TREE,
14350 /*function_p=*/false);
14351 processing_template_decl = saved_processing_template_decl;
14356 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14357 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14360 cp_parser_friend_p (tree decl_specifiers)
14362 while (decl_specifiers)
14364 /* See if this decl-specifier is `friend'. */
14365 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14366 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14369 /* Go on to the next decl-specifier. */
14370 decl_specifiers = TREE_CHAIN (decl_specifiers);
14376 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14377 issue an error message indicating that TOKEN_DESC was expected.
14379 Returns the token consumed, if the token had the appropriate type.
14380 Otherwise, returns NULL. */
14383 cp_parser_require (cp_parser* parser,
14384 enum cpp_ttype type,
14385 const char* token_desc)
14387 if (cp_lexer_next_token_is (parser->lexer, type))
14388 return cp_lexer_consume_token (parser->lexer);
14391 /* Output the MESSAGE -- unless we're parsing tentatively. */
14392 if (!cp_parser_simulate_error (parser))
14393 error ("expected %s", token_desc);
14398 /* Like cp_parser_require, except that tokens will be skipped until
14399 the desired token is found. An error message is still produced if
14400 the next token is not as expected. */
14403 cp_parser_skip_until_found (cp_parser* parser,
14404 enum cpp_ttype type,
14405 const char* token_desc)
14408 unsigned nesting_depth = 0;
14410 if (cp_parser_require (parser, type, token_desc))
14413 /* Skip tokens until the desired token is found. */
14416 /* Peek at the next token. */
14417 token = cp_lexer_peek_token (parser->lexer);
14418 /* If we've reached the token we want, consume it and
14420 if (token->type == type && !nesting_depth)
14422 cp_lexer_consume_token (parser->lexer);
14425 /* If we've run out of tokens, stop. */
14426 if (token->type == CPP_EOF)
14428 if (token->type == CPP_OPEN_BRACE
14429 || token->type == CPP_OPEN_PAREN
14430 || token->type == CPP_OPEN_SQUARE)
14432 else if (token->type == CPP_CLOSE_BRACE
14433 || token->type == CPP_CLOSE_PAREN
14434 || token->type == CPP_CLOSE_SQUARE)
14436 if (nesting_depth-- == 0)
14439 /* Consume this token. */
14440 cp_lexer_consume_token (parser->lexer);
14444 /* If the next token is the indicated keyword, consume it. Otherwise,
14445 issue an error message indicating that TOKEN_DESC was expected.
14447 Returns the token consumed, if the token had the appropriate type.
14448 Otherwise, returns NULL. */
14451 cp_parser_require_keyword (cp_parser* parser,
14453 const char* token_desc)
14455 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14457 if (token && token->keyword != keyword)
14459 dyn_string_t error_msg;
14461 /* Format the error message. */
14462 error_msg = dyn_string_new (0);
14463 dyn_string_append_cstr (error_msg, "expected ");
14464 dyn_string_append_cstr (error_msg, token_desc);
14465 cp_parser_error (parser, error_msg->s);
14466 dyn_string_delete (error_msg);
14473 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14474 function-definition. */
14477 cp_parser_token_starts_function_definition_p (cp_token* token)
14479 return (/* An ordinary function-body begins with an `{'. */
14480 token->type == CPP_OPEN_BRACE
14481 /* A ctor-initializer begins with a `:'. */
14482 || token->type == CPP_COLON
14483 /* A function-try-block begins with `try'. */
14484 || token->keyword == RID_TRY
14485 /* The named return value extension begins with `return'. */
14486 || token->keyword == RID_RETURN);
14489 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14493 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14497 token = cp_lexer_peek_token (parser->lexer);
14498 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14501 /* Returns TRUE iff the next token is the "," or ">" ending a
14502 template-argument. */
14505 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14509 token = cp_lexer_peek_token (parser->lexer);
14510 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14513 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14514 or none_type otherwise. */
14516 static enum tag_types
14517 cp_parser_token_is_class_key (cp_token* token)
14519 switch (token->keyword)
14524 return record_type;
14533 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14536 cp_parser_check_class_key (enum tag_types class_key, tree type)
14538 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14539 pedwarn ("`%s' tag used in naming `%#T'",
14540 class_key == union_type ? "union"
14541 : class_key == record_type ? "struct" : "class",
14545 /* Issue an error message if DECL is redeclared with different
14546 access than its original declaration [class.access.spec/3].
14547 This applies to nested classes and nested class templates.
14550 static void cp_parser_check_access_in_redeclaration (tree decl)
14552 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14555 if ((TREE_PRIVATE (decl)
14556 != (current_access_specifier == access_private_node))
14557 || (TREE_PROTECTED (decl)
14558 != (current_access_specifier == access_protected_node)))
14559 error ("%D redeclared with different access", decl);
14562 /* Look for the `template' keyword, as a syntactic disambiguator.
14563 Return TRUE iff it is present, in which case it will be
14567 cp_parser_optional_template_keyword (cp_parser *parser)
14569 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14571 /* The `template' keyword can only be used within templates;
14572 outside templates the parser can always figure out what is a
14573 template and what is not. */
14574 if (!processing_template_decl)
14576 error ("`template' (as a disambiguator) is only allowed "
14577 "within templates");
14578 /* If this part of the token stream is rescanned, the same
14579 error message would be generated. So, we purge the token
14580 from the stream. */
14581 cp_lexer_purge_token (parser->lexer);
14586 /* Consume the `template' keyword. */
14587 cp_lexer_consume_token (parser->lexer);
14595 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14596 set PARSER->SCOPE, and perform other related actions. */
14599 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14604 /* Get the stored value. */
14605 value = cp_lexer_consume_token (parser->lexer)->value;
14606 /* Perform any access checks that were deferred. */
14607 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14608 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14609 /* Set the scope from the stored value. */
14610 parser->scope = TREE_VALUE (value);
14611 parser->qualifying_scope = TREE_TYPE (value);
14612 parser->object_scope = NULL_TREE;
14615 /* Add tokens to CACHE until an non-nested END token appears. */
14618 cp_parser_cache_group (cp_parser *parser,
14619 cp_token_cache *cache,
14620 enum cpp_ttype end,
14627 /* Abort a parenthesized expression if we encounter a brace. */
14628 if ((end == CPP_CLOSE_PAREN || depth == 0)
14629 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14631 /* Consume the next token. */
14632 token = cp_lexer_consume_token (parser->lexer);
14633 /* If we've reached the end of the file, stop. */
14634 if (token->type == CPP_EOF)
14636 /* Add this token to the tokens we are saving. */
14637 cp_token_cache_push_token (cache, token);
14638 /* See if it starts a new group. */
14639 if (token->type == CPP_OPEN_BRACE)
14641 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14645 else if (token->type == CPP_OPEN_PAREN)
14646 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14647 else if (token->type == end)
14652 /* Begin parsing tentatively. We always save tokens while parsing
14653 tentatively so that if the tentative parsing fails we can restore the
14657 cp_parser_parse_tentatively (cp_parser* parser)
14659 /* Enter a new parsing context. */
14660 parser->context = cp_parser_context_new (parser->context);
14661 /* Begin saving tokens. */
14662 cp_lexer_save_tokens (parser->lexer);
14663 /* In order to avoid repetitive access control error messages,
14664 access checks are queued up until we are no longer parsing
14666 push_deferring_access_checks (dk_deferred);
14669 /* Commit to the currently active tentative parse. */
14672 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14674 cp_parser_context *context;
14677 /* Mark all of the levels as committed. */
14678 lexer = parser->lexer;
14679 for (context = parser->context; context->next; context = context->next)
14681 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14683 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14684 while (!cp_lexer_saving_tokens (lexer))
14685 lexer = lexer->next;
14686 cp_lexer_commit_tokens (lexer);
14690 /* Abort the currently active tentative parse. All consumed tokens
14691 will be rolled back, and no diagnostics will be issued. */
14694 cp_parser_abort_tentative_parse (cp_parser* parser)
14696 cp_parser_simulate_error (parser);
14697 /* Now, pretend that we want to see if the construct was
14698 successfully parsed. */
14699 cp_parser_parse_definitely (parser);
14702 /* Stop parsing tentatively. If a parse error has occurred, restore the
14703 token stream. Otherwise, commit to the tokens we have consumed.
14704 Returns true if no error occurred; false otherwise. */
14707 cp_parser_parse_definitely (cp_parser* parser)
14709 bool error_occurred;
14710 cp_parser_context *context;
14712 /* Remember whether or not an error occurred, since we are about to
14713 destroy that information. */
14714 error_occurred = cp_parser_error_occurred (parser);
14715 /* Remove the topmost context from the stack. */
14716 context = parser->context;
14717 parser->context = context->next;
14718 /* If no parse errors occurred, commit to the tentative parse. */
14719 if (!error_occurred)
14721 /* Commit to the tokens read tentatively, unless that was
14723 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14724 cp_lexer_commit_tokens (parser->lexer);
14726 pop_to_parent_deferring_access_checks ();
14728 /* Otherwise, if errors occurred, roll back our state so that things
14729 are just as they were before we began the tentative parse. */
14732 cp_lexer_rollback_tokens (parser->lexer);
14733 pop_deferring_access_checks ();
14735 /* Add the context to the front of the free list. */
14736 context->next = cp_parser_context_free_list;
14737 cp_parser_context_free_list = context;
14739 return !error_occurred;
14742 /* Returns true if we are parsing tentatively -- but have decided that
14743 we will stick with this tentative parse, even if errors occur. */
14746 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14748 return (cp_parser_parsing_tentatively (parser)
14749 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14752 /* Returns nonzero iff an error has occurred during the most recent
14753 tentative parse. */
14756 cp_parser_error_occurred (cp_parser* parser)
14758 return (cp_parser_parsing_tentatively (parser)
14759 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14762 /* Returns nonzero if GNU extensions are allowed. */
14765 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14767 return parser->allow_gnu_extensions_p;
14774 static GTY (()) cp_parser *the_parser;
14776 /* External interface. */
14778 /* Parse one entire translation unit. */
14781 c_parse_file (void)
14783 bool error_occurred;
14785 the_parser = cp_parser_new ();
14786 push_deferring_access_checks (flag_access_control
14787 ? dk_no_deferred : dk_no_check);
14788 error_occurred = cp_parser_translation_unit (the_parser);
14792 /* Clean up after parsing the entire translation unit. */
14795 free_parser_stacks (void)
14797 /* Nothing to do. */
14800 /* This variable must be provided by every front end. */
14804 #include "gt-cp-parser.h"