2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype) type : 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid) keyword : 8;
78 /* The value associated with this token, if any. */
80 /* The location at which this token was found. */
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block GTY (())
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
119 cp_token_block *last;
124 static cp_token_cache *cp_token_cache_new
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return ggc_alloc_cleared (sizeof (cp_token_cache));
137 /* Add *TOKEN to *CACHE. */
140 cp_token_cache_push_token (cp_token_cache *cache,
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = ggc_alloc_cleared (sizeof (cp_token_block));
149 b->prev = cache->last;
152 cache->last->next = b;
156 cache->first = cache->last = b;
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
166 typedef struct cp_lexer GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token * GTY ((skip (""))) buffer_end;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token * GTY ((skip (""))) first_token;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token * GTY ((skip (""))) next_token;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token * GTY ((skip (""))) last_token;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
200 /* The STRING_CST tokens encountered while processing the current
202 varray_type string_tokens;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
208 /* True if we should output debugging information. */
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
217 static cp_lexer *cp_lexer_new_main
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
223 static cp_token *cp_lexer_next_token
224 (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
231 static void cp_lexer_maybe_grow_buffer
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
237 static cp_token *cp_lexer_peek_nth_token
238 (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
247 static void cp_lexer_purge_token
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
253 static void cp_lexer_commit_tokens
255 static void cp_lexer_rollback_tokens
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260 (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
263 static void cp_lexer_start_debugging
264 (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266 (cp_lexer *) ATTRIBUTE_UNUSED;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
302 cp_lexer_new_main (void)
305 cp_token first_token;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
316 /* Create the circular buffer. */
317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer->main_lexer_p = true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
352 /* Allocate the memory. */
353 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
355 /* Create a new buffer, appropriately sized. */
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
359 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360 lexer->buffer_end = lexer->buffer + num_tokens;
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
392 /* Returns nonzero if debugging information should be output. */
395 cp_lexer_debugging_p (cp_lexer *lexer)
397 return lexer->debugging_p;
400 /* Set the current source position from the information stored in
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
412 input_location = token->location;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
438 /* nonzero if we are presently saving tokens. */
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
465 return finish - start;
467 return ((lexer->buffer_end - lexer->buffer)
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
475 cp_lexer_read_token (cp_lexer* lexer)
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
507 /* Get the current type. It will be an ARRAY_TYPE. */
508 type = TREE_TYPE (token->value);
509 /* Use build_cplus_array_type to rebuild the array, thereby
510 getting the right type. */
511 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
512 /* Reset the type of the token. */
513 TREE_TYPE (token->value) = type;
519 /* If the circular buffer is full, make it bigger. */
522 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
524 /* If the buffer is full, enlarge it. */
525 if (lexer->last_token == lexer->first_token)
527 cp_token *new_buffer;
528 cp_token *old_buffer;
529 cp_token *new_first_token;
530 ptrdiff_t buffer_length;
531 size_t num_tokens_to_copy;
533 /* Remember the current buffer pointer. It will become invalid,
534 but we will need to do pointer arithmetic involving this
536 old_buffer = lexer->buffer;
537 /* Compute the current buffer size. */
538 buffer_length = lexer->buffer_end - lexer->buffer;
539 /* Allocate a buffer twice as big. */
540 new_buffer = ggc_realloc (lexer->buffer,
541 2 * buffer_length * sizeof (cp_token));
543 /* Because the buffer is circular, logically consecutive tokens
544 are not necessarily placed consecutively in memory.
545 Therefore, we must keep move the tokens that were before
546 FIRST_TOKEN to the second half of the newly allocated
548 num_tokens_to_copy = (lexer->first_token - old_buffer);
549 memcpy (new_buffer + buffer_length,
551 num_tokens_to_copy * sizeof (cp_token));
552 /* Clear the rest of the buffer. We never look at this storage,
553 but the garbage collector may. */
554 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
555 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
557 /* Now recompute all of the buffer pointers. */
559 = new_buffer + (lexer->first_token - old_buffer);
560 if (lexer->next_token != NULL)
562 ptrdiff_t next_token_delta;
564 if (lexer->next_token > lexer->first_token)
565 next_token_delta = lexer->next_token - lexer->first_token;
568 buffer_length - (lexer->first_token - lexer->next_token);
569 lexer->next_token = new_first_token + next_token_delta;
571 lexer->last_token = new_first_token + buffer_length;
572 lexer->buffer = new_buffer;
573 lexer->buffer_end = new_buffer + buffer_length * 2;
574 lexer->first_token = new_first_token;
578 /* Store the next token from the preprocessor in *TOKEN. */
581 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
586 /* If this not the main lexer, return a terminating CPP_EOF token. */
587 if (lexer != NULL && !lexer->main_lexer_p)
589 token->type = CPP_EOF;
590 token->location.line = 0;
591 token->location.file = NULL;
592 token->value = NULL_TREE;
593 token->keyword = RID_MAX;
599 /* Keep going until we get a token we like. */
602 /* Get a new token from the preprocessor. */
603 token->type = c_lex_with_flags (&token->value, &token->flags);
604 /* Issue messages about tokens we cannot process. */
610 error ("invalid token");
614 /* This is a good token, so we exit the loop. */
619 /* Now we've got our token. */
620 token->location = input_location;
622 /* Check to see if this token is a keyword. */
623 if (token->type == CPP_NAME
624 && C_IS_RESERVED_WORD (token->value))
626 /* Mark this token as a keyword. */
627 token->type = CPP_KEYWORD;
628 /* Record which keyword. */
629 token->keyword = C_RID_CODE (token->value);
630 /* Update the value. Some keywords are mapped to particular
631 entities, rather than simply having the value of the
632 corresponding IDENTIFIER_NODE. For example, `__const' is
633 mapped to `const'. */
634 token->value = ridpointers[token->keyword];
637 token->keyword = RID_MAX;
640 /* Return a pointer to the next token in the token stream, but do not
644 cp_lexer_peek_token (cp_lexer* lexer)
648 /* If there are no tokens, read one now. */
649 if (!lexer->next_token)
650 cp_lexer_read_token (lexer);
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
655 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
656 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
657 fprintf (cp_lexer_debug_stream, "\n");
660 token = lexer->next_token;
661 cp_lexer_set_source_position_from_token (lexer, token);
665 /* Return true if the next token has the indicated TYPE. */
668 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
672 /* Peek at the next token. */
673 token = cp_lexer_peek_token (lexer);
674 /* Check to see if it has the indicated TYPE. */
675 return token->type == type;
678 /* Return true if the next token does not have the indicated TYPE. */
681 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
683 return !cp_lexer_next_token_is (lexer, type);
686 /* Return true if the next token is the indicated KEYWORD. */
689 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
693 /* Peek at the next token. */
694 token = cp_lexer_peek_token (lexer);
695 /* Check to see if it is the indicated keyword. */
696 return token->keyword == keyword;
699 /* Return a pointer to the Nth token in the token stream. If N is 1,
700 then this is precisely equivalent to cp_lexer_peek_token. */
703 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
707 /* N is 1-based, not zero-based. */
708 my_friendly_assert (n > 0, 20000224);
710 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
711 token = lexer->next_token;
712 /* If there are no tokens in the buffer, get one now. */
715 cp_lexer_read_token (lexer);
716 token = lexer->next_token;
719 /* Now, read tokens until we have enough. */
722 /* Advance to the next token. */
723 token = cp_lexer_next_token (lexer, token);
724 /* If that's all the tokens we have, read a new one. */
725 if (token == lexer->last_token)
726 token = cp_lexer_read_token (lexer);
732 /* Consume the next token. The pointer returned is valid only until
733 another token is read. Callers should preserve copy the token
734 explicitly if they will need its value for a longer period of
738 cp_lexer_consume_token (cp_lexer* lexer)
742 /* If there are no tokens, read one now. */
743 if (!lexer->next_token)
744 cp_lexer_read_token (lexer);
746 /* Remember the token we'll be returning. */
747 token = lexer->next_token;
749 /* Increment NEXT_TOKEN. */
750 lexer->next_token = cp_lexer_next_token (lexer,
752 /* Check to see if we're all out of tokens. */
753 if (lexer->next_token == lexer->last_token)
754 lexer->next_token = NULL;
756 /* If we're not saving tokens, then move FIRST_TOKEN too. */
757 if (!cp_lexer_saving_tokens (lexer))
759 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
760 if (!lexer->next_token)
761 lexer->first_token = NULL;
763 lexer->first_token = lexer->next_token;
766 /* Provide debugging output. */
767 if (cp_lexer_debugging_p (lexer))
769 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
770 cp_lexer_print_token (cp_lexer_debug_stream, token);
771 fprintf (cp_lexer_debug_stream, "\n");
777 /* Permanently remove the next token from the token stream. There
778 must be a valid next token already; this token never reads
779 additional tokens from the preprocessor. */
782 cp_lexer_purge_token (cp_lexer *lexer)
785 cp_token *next_token;
787 token = lexer->next_token;
790 next_token = cp_lexer_next_token (lexer, token);
791 if (next_token == lexer->last_token)
793 *token = *next_token;
797 lexer->last_token = token;
798 /* The token purged may have been the only token remaining; if so,
800 if (lexer->next_token == token)
801 lexer->next_token = NULL;
804 /* Permanently remove all tokens after TOKEN, up to, but not
805 including, the token that will be returned next by
806 cp_lexer_peek_token. */
809 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
815 if (lexer->next_token)
817 /* Copy the tokens that have not yet been read to the location
818 immediately following TOKEN. */
819 t1 = cp_lexer_next_token (lexer, token);
820 t2 = peek = cp_lexer_peek_token (lexer);
821 /* Move tokens into the vacant area between TOKEN and PEEK. */
822 while (t2 != lexer->last_token)
825 t1 = cp_lexer_next_token (lexer, t1);
826 t2 = cp_lexer_next_token (lexer, t2);
828 /* Now, the next available token is right after TOKEN. */
829 lexer->next_token = cp_lexer_next_token (lexer, token);
830 /* And the last token is wherever we ended up. */
831 lexer->last_token = t1;
835 /* There are no tokens in the buffer, so there is nothing to
836 copy. The last token in the buffer is TOKEN itself. */
837 lexer->last_token = cp_lexer_next_token (lexer, token);
841 /* Begin saving tokens. All tokens consumed after this point will be
845 cp_lexer_save_tokens (cp_lexer* lexer)
847 /* Provide debugging output. */
848 if (cp_lexer_debugging_p (lexer))
849 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
851 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852 restore the tokens if required. */
853 if (!lexer->next_token)
854 cp_lexer_read_token (lexer);
856 VARRAY_PUSH_INT (lexer->saved_tokens,
857 cp_lexer_token_difference (lexer,
862 /* Commit to the portion of the token stream most recently saved. */
865 cp_lexer_commit_tokens (cp_lexer* lexer)
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer))
869 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
871 VARRAY_POP (lexer->saved_tokens);
874 /* Return all tokens saved since the last call to cp_lexer_save_tokens
875 to the token stream. Stop saving tokens. */
878 cp_lexer_rollback_tokens (cp_lexer* lexer)
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
886 /* Find the token that was the NEXT_TOKEN when we started saving
888 delta = VARRAY_TOP_INT(lexer->saved_tokens);
889 /* Make it the next token again now. */
890 lexer->next_token = cp_lexer_advance_token (lexer,
893 /* It might be the case that there were no tokens when we started
894 saving tokens, but that there are some tokens now. */
895 if (!lexer->next_token && lexer->first_token)
896 lexer->next_token = lexer->first_token;
898 /* Stop saving tokens. */
899 VARRAY_POP (lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
905 cp_lexer_print_token (FILE * stream, cp_token* token)
907 const char *token_type = NULL;
909 /* Figure out what kind of token this is. */
917 token_type = "COMMA";
921 token_type = "OPEN_PAREN";
924 case CPP_CLOSE_PAREN:
925 token_type = "CLOSE_PAREN";
929 token_type = "OPEN_BRACE";
932 case CPP_CLOSE_BRACE:
933 token_type = "CLOSE_BRACE";
937 token_type = "SEMICOLON";
949 token_type = "keyword";
952 /* This is not a token that we know how to handle yet. */
957 /* If we have a name for the token, print it out. Otherwise, we
958 simply give the numeric code. */
960 fprintf (stream, "%s", token_type);
962 fprintf (stream, "%d", token->type);
963 /* And, for an identifier, print the identifier name. */
964 if (token->type == CPP_NAME
965 /* Some keywords have a value that is not an IDENTIFIER_NODE.
966 For example, `struct' is mapped to an INTEGER_CST. */
967 || (token->type == CPP_KEYWORD
968 && TREE_CODE (token->value) == IDENTIFIER_NODE))
969 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
972 /* Start emitting debugging information. */
975 cp_lexer_start_debugging (cp_lexer* lexer)
977 ++lexer->debugging_p;
980 /* Stop emitting debugging information. */
983 cp_lexer_stop_debugging (cp_lexer* lexer)
985 --lexer->debugging_p;
994 A cp_parser parses the token stream as specified by the C++
995 grammar. Its job is purely parsing, not semantic analysis. For
996 example, the parser breaks the token stream into declarators,
997 expressions, statements, and other similar syntactic constructs.
998 It does not check that the types of the expressions on either side
999 of an assignment-statement are compatible, or that a function is
1000 not declared with a parameter of type `void'.
1002 The parser invokes routines elsewhere in the compiler to perform
1003 semantic analysis and to build up the abstract syntax tree for the
1006 The parser (and the template instantiation code, which is, in a
1007 way, a close relative of parsing) are the only parts of the
1008 compiler that should be calling push_scope and pop_scope, or
1009 related functions. The parser (and template instantiation code)
1010 keeps track of what scope is presently active; everything else
1011 should simply honor that. (The code that generates static
1012 initializers may also need to set the scope, in order to check
1013 access control correctly when emitting the initializers.)
1018 The parser is of the standard recursive-descent variety. Upcoming
1019 tokens in the token stream are examined in order to determine which
1020 production to use when parsing a non-terminal. Some C++ constructs
1021 require arbitrary look ahead to disambiguate. For example, it is
1022 impossible, in the general case, to tell whether a statement is an
1023 expression or declaration without scanning the entire statement.
1024 Therefore, the parser is capable of "parsing tentatively." When the
1025 parser is not sure what construct comes next, it enters this mode.
1026 Then, while we attempt to parse the construct, the parser queues up
1027 error messages, rather than issuing them immediately, and saves the
1028 tokens it consumes. If the construct is parsed successfully, the
1029 parser "commits", i.e., it issues any queued error messages and
1030 the tokens that were being preserved are permanently discarded.
1031 If, however, the construct is not parsed successfully, the parser
1032 rolls back its state completely so that it can resume parsing using
1033 a different alternative.
1038 The performance of the parser could probably be improved
1039 substantially. Some possible improvements include:
1041 - The expression parser recurses through the various levels of
1042 precedence as specified in the grammar, rather than using an
1043 operator-precedence technique. Therefore, parsing a simple
1044 identifier requires multiple recursive calls.
1046 - We could often eliminate the need to parse tentatively by
1047 looking ahead a little bit. In some places, this approach
1048 might not entirely eliminate the need to parse tentatively, but
1049 it might still speed up the average case. */
1051 /* Flags that are passed to some parsing functions. These values can
1052 be bitwise-ored together. */
1054 typedef enum cp_parser_flags
1057 CP_PARSER_FLAGS_NONE = 0x0,
1058 /* The construct is optional. If it is not present, then no error
1059 should be issued. */
1060 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1061 /* When parsing a type-specifier, do not allow user-defined types. */
1062 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1065 /* The different kinds of declarators we want to parse. */
1067 typedef enum cp_parser_declarator_kind
1069 /* We want an abstract declartor. */
1070 CP_PARSER_DECLARATOR_ABSTRACT,
1071 /* We want a named declarator. */
1072 CP_PARSER_DECLARATOR_NAMED,
1073 /* We don't mind, but the name must be an unqualified-id. */
1074 CP_PARSER_DECLARATOR_EITHER
1075 } cp_parser_declarator_kind;
1077 /* A mapping from a token type to a corresponding tree node type. */
1079 typedef struct cp_parser_token_tree_map_node
1081 /* The token type. */
1082 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1083 /* The corresponding tree code. */
1084 ENUM_BITFIELD (tree_code) tree_type : 8;
1085 } cp_parser_token_tree_map_node;
1087 /* A complete map consists of several ordinary entries, followed by a
1088 terminator. The terminating entry has a token_type of CPP_EOF. */
1090 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1092 /* The status of a tentative parse. */
1094 typedef enum cp_parser_status_kind
1096 /* No errors have occurred. */
1097 CP_PARSER_STATUS_KIND_NO_ERROR,
1098 /* An error has occurred. */
1099 CP_PARSER_STATUS_KIND_ERROR,
1100 /* We are committed to this tentative parse, whether or not an error
1102 CP_PARSER_STATUS_KIND_COMMITTED
1103 } cp_parser_status_kind;
1105 /* Context that is saved and restored when parsing tentatively. */
1107 typedef struct cp_parser_context GTY (())
1109 /* If this is a tentative parsing context, the status of the
1111 enum cp_parser_status_kind status;
1112 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1113 that are looked up in this context must be looked up both in the
1114 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115 the context of the containing expression. */
1117 /* The next parsing context in the stack. */
1118 struct cp_parser_context *next;
1119 } cp_parser_context;
1123 /* Constructors and destructors. */
1125 static cp_parser_context *cp_parser_context_new
1126 (cp_parser_context *);
1128 /* Class variables. */
1130 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1132 /* Constructors and destructors. */
1134 /* Construct a new context. The context below this one on the stack
1135 is given by NEXT. */
1137 static cp_parser_context *
1138 cp_parser_context_new (cp_parser_context* next)
1140 cp_parser_context *context;
1142 /* Allocate the storage. */
1143 if (cp_parser_context_free_list != NULL)
1145 /* Pull the first entry from the free list. */
1146 context = cp_parser_context_free_list;
1147 cp_parser_context_free_list = context->next;
1148 memset (context, 0, sizeof (*context));
1151 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1152 /* No errors have occurred yet in this context. */
1153 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context->object_type = next->object_type;
1161 /* Thread the stack. */
1162 context->next = next;
1168 /* The cp_parser structure represents the C++ parser. */
1170 typedef struct cp_parser GTY(())
1172 /* The lexer from which we are obtaining tokens. */
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1195 tree qualifying_scope;
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context *context;
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p;
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p;
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissible. */
1220 bool default_arg_ok_p;
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool integral_constant_expression_p;
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_integral_constant_expression_p;
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_integral_constant_expression_p;
1236 /* TRUE if we are parsing the argument to "__offsetof__". */
1239 /* TRUE if local variable names and `this' are forbidden in the
1241 bool local_variables_forbidden_p;
1243 /* TRUE if the declaration we are parsing is part of a
1244 linkage-specification of the form `extern string-literal
1246 bool in_unbraced_linkage_specification_p;
1248 /* TRUE if we are presently parsing a declarator, after the
1249 direct-declarator. */
1250 bool in_declarator_p;
1252 /* TRUE if we are presently parsing a template-argument-list. */
1253 bool in_template_argument_list_p;
1255 /* TRUE if we are presently parsing the body of an
1256 iteration-statement. */
1257 bool in_iteration_statement_p;
1259 /* TRUE if we are presently parsing the body of a switch
1261 bool in_switch_statement_p;
1263 /* TRUE if we are parsing a type-id in an expression context. In
1264 such a situation, both "type (expr)" and "type (type)" are valid
1266 bool in_type_id_in_expr_p;
1268 /* If non-NULL, then we are parsing a construct where new type
1269 definitions are not permitted. The string stored here will be
1270 issued as an error message if a type is defined. */
1271 const char *type_definition_forbidden_message;
1273 /* A list of lists. The outer list is a stack, used for member
1274 functions of local classes. At each level there are two sub-list,
1275 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277 TREE_VALUE's. The functions are chained in reverse declaration
1280 The TREE_PURPOSE sublist contains those functions with default
1281 arguments that need post processing, and the TREE_VALUE sublist
1282 contains those functions with definitions that need post
1285 These lists can only be processed once the outermost class being
1286 defined is complete. */
1287 tree unparsed_functions_queues;
1289 /* The number of classes whose definitions are currently in
1291 unsigned num_classes_being_defined;
1293 /* The number of template parameter lists that apply directly to the
1294 current declaration. */
1295 unsigned num_template_parameter_lists;
1298 /* The type of a function that parses some kind of expression. */
1299 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1303 /* Constructors and destructors. */
1305 static cp_parser *cp_parser_new
1308 /* Routines to parse various constructs.
1310 Those that return `tree' will return the error_mark_node (rather
1311 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312 Sometimes, they will return an ordinary node if error-recovery was
1313 attempted, even though a parse error occurred. So, to check
1314 whether or not a parse error occurred, you should always use
1315 cp_parser_error_occurred. If the construct is optional (indicated
1316 either by an `_opt' in the name of the function that does the
1317 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318 the construct is not present. */
1320 /* Lexical conventions [gram.lex] */
1322 static tree cp_parser_identifier
1325 /* Basic concepts [gram.basic] */
1327 static bool cp_parser_translation_unit
1330 /* Expressions [gram.expr] */
1332 static tree cp_parser_primary_expression
1333 (cp_parser *, cp_id_kind *, tree *);
1334 static tree cp_parser_id_expression
1335 (cp_parser *, bool, bool, bool *, bool);
1336 static tree cp_parser_unqualified_id
1337 (cp_parser *, bool, bool, bool);
1338 static tree cp_parser_nested_name_specifier_opt
1339 (cp_parser *, bool, bool, bool, bool);
1340 static tree cp_parser_nested_name_specifier
1341 (cp_parser *, bool, bool, bool, bool);
1342 static tree cp_parser_class_or_namespace_name
1343 (cp_parser *, bool, bool, bool, bool, bool);
1344 static tree cp_parser_postfix_expression
1345 (cp_parser *, bool);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser *, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser *, tree *, tree *);
1350 static tree cp_parser_unary_expression
1351 (cp_parser *, bool);
1352 static enum tree_code cp_parser_unary_operator
1354 static tree cp_parser_new_expression
1356 static tree cp_parser_new_placement
1358 static tree cp_parser_new_type_id
1360 static tree cp_parser_new_declarator_opt
1362 static tree cp_parser_direct_new_declarator
1364 static tree cp_parser_new_initializer
1366 static tree cp_parser_delete_expression
1368 static tree cp_parser_cast_expression
1369 (cp_parser *, bool);
1370 static tree cp_parser_pm_expression
1372 static tree cp_parser_multiplicative_expression
1374 static tree cp_parser_additive_expression
1376 static tree cp_parser_shift_expression
1378 static tree cp_parser_relational_expression
1380 static tree cp_parser_equality_expression
1382 static tree cp_parser_and_expression
1384 static tree cp_parser_exclusive_or_expression
1386 static tree cp_parser_inclusive_or_expression
1388 static tree cp_parser_logical_and_expression
1390 static tree cp_parser_logical_or_expression
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser *, tree);
1394 static tree cp_parser_assignment_expression
1396 static enum tree_code cp_parser_assignment_operator_opt
1398 static tree cp_parser_expression
1400 static tree cp_parser_constant_expression
1401 (cp_parser *, bool, bool *);
1403 /* Statements [gram.stmt.stmt] */
1405 static void cp_parser_statement
1406 (cp_parser *, bool);
1407 static tree cp_parser_labeled_statement
1408 (cp_parser *, bool);
1409 static tree cp_parser_expression_statement
1410 (cp_parser *, bool);
1411 static tree cp_parser_compound_statement
1412 (cp_parser *, bool);
1413 static void cp_parser_statement_seq_opt
1414 (cp_parser *, bool);
1415 static tree cp_parser_selection_statement
1417 static tree cp_parser_condition
1419 static tree cp_parser_iteration_statement
1421 static void cp_parser_for_init_statement
1423 static tree cp_parser_jump_statement
1425 static void cp_parser_declaration_statement
1428 static tree cp_parser_implicitly_scoped_statement
1430 static void cp_parser_already_scoped_statement
1433 /* Declarations [gram.dcl.dcl] */
1435 static void cp_parser_declaration_seq_opt
1437 static void cp_parser_declaration
1439 static void cp_parser_block_declaration
1440 (cp_parser *, bool);
1441 static void cp_parser_simple_declaration
1442 (cp_parser *, bool);
1443 static tree cp_parser_decl_specifier_seq
1444 (cp_parser *, cp_parser_flags, tree *, int *);
1445 static tree cp_parser_storage_class_specifier_opt
1447 static tree cp_parser_function_specifier_opt
1449 static tree cp_parser_type_specifier
1450 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1451 static tree cp_parser_simple_type_specifier
1452 (cp_parser *, cp_parser_flags, bool);
1453 static tree cp_parser_type_name
1455 static tree cp_parser_elaborated_type_specifier
1456 (cp_parser *, bool, bool);
1457 static tree cp_parser_enum_specifier
1459 static void cp_parser_enumerator_list
1460 (cp_parser *, tree);
1461 static void cp_parser_enumerator_definition
1462 (cp_parser *, tree);
1463 static tree cp_parser_namespace_name
1465 static void cp_parser_namespace_definition
1467 static void cp_parser_namespace_body
1469 static tree cp_parser_qualified_namespace_specifier
1471 static void cp_parser_namespace_alias_definition
1473 static void cp_parser_using_declaration
1475 static void cp_parser_using_directive
1477 static void cp_parser_asm_definition
1479 static void cp_parser_linkage_specification
1482 /* Declarators [gram.dcl.decl] */
1484 static tree cp_parser_init_declarator
1485 (cp_parser *, tree, tree, bool, bool, int, bool *);
1486 static tree cp_parser_declarator
1487 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1488 static tree cp_parser_direct_declarator
1489 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1490 static enum tree_code cp_parser_ptr_operator
1491 (cp_parser *, tree *, tree *);
1492 static tree cp_parser_cv_qualifier_seq_opt
1494 static tree cp_parser_cv_qualifier_opt
1496 static tree cp_parser_declarator_id
1498 static tree cp_parser_type_id
1500 static tree cp_parser_type_specifier_seq
1502 static tree cp_parser_parameter_declaration_clause
1504 static tree cp_parser_parameter_declaration_list
1506 static tree cp_parser_parameter_declaration
1507 (cp_parser *, bool, bool *);
1508 static void cp_parser_function_body
1510 static tree cp_parser_initializer
1511 (cp_parser *, bool *, bool *);
1512 static tree cp_parser_initializer_clause
1513 (cp_parser *, bool *);
1514 static tree cp_parser_initializer_list
1515 (cp_parser *, bool *);
1517 static bool cp_parser_ctor_initializer_opt_and_function_body
1520 /* Classes [gram.class] */
1522 static tree cp_parser_class_name
1523 (cp_parser *, bool, bool, bool, bool, bool, bool);
1524 static tree cp_parser_class_specifier
1526 static tree cp_parser_class_head
1527 (cp_parser *, bool *, tree *);
1528 static enum tag_types cp_parser_class_key
1530 static void cp_parser_member_specification_opt
1532 static void cp_parser_member_declaration
1534 static tree cp_parser_pure_specifier
1536 static tree cp_parser_constant_initializer
1539 /* Derived classes [gram.class.derived] */
1541 static tree cp_parser_base_clause
1543 static tree cp_parser_base_specifier
1546 /* Special member functions [gram.special] */
1548 static tree cp_parser_conversion_function_id
1550 static tree cp_parser_conversion_type_id
1552 static tree cp_parser_conversion_declarator_opt
1554 static bool cp_parser_ctor_initializer_opt
1556 static void cp_parser_mem_initializer_list
1558 static tree cp_parser_mem_initializer
1560 static tree cp_parser_mem_initializer_id
1563 /* Overloading [gram.over] */
1565 static tree cp_parser_operator_function_id
1567 static tree cp_parser_operator
1570 /* Templates [gram.temp] */
1572 static void cp_parser_template_declaration
1573 (cp_parser *, bool);
1574 static tree cp_parser_template_parameter_list
1576 static tree cp_parser_template_parameter
1578 static tree cp_parser_type_parameter
1580 static tree cp_parser_template_id
1581 (cp_parser *, bool, bool, bool);
1582 static tree cp_parser_template_name
1583 (cp_parser *, bool, bool, bool, bool *);
1584 static tree cp_parser_template_argument_list
1586 static tree cp_parser_template_argument
1588 static void cp_parser_explicit_instantiation
1590 static void cp_parser_explicit_specialization
1593 /* Exception handling [gram.exception] */
1595 static tree cp_parser_try_block
1597 static bool cp_parser_function_try_block
1599 static void cp_parser_handler_seq
1601 static void cp_parser_handler
1603 static tree cp_parser_exception_declaration
1605 static tree cp_parser_throw_expression
1607 static tree cp_parser_exception_specification_opt
1609 static tree cp_parser_type_id_list
1612 /* GNU Extensions */
1614 static tree cp_parser_asm_specification_opt
1616 static tree cp_parser_asm_operand_list
1618 static tree cp_parser_asm_clobber_list
1620 static tree cp_parser_attributes_opt
1622 static tree cp_parser_attribute_list
1624 static bool cp_parser_extension_opt
1625 (cp_parser *, int *);
1626 static void cp_parser_label_declaration
1629 /* Utility Routines */
1631 static tree cp_parser_lookup_name
1632 (cp_parser *, tree, bool, bool, bool, bool);
1633 static tree cp_parser_lookup_name_simple
1634 (cp_parser *, tree);
1635 static tree cp_parser_maybe_treat_template_as_class
1637 static bool cp_parser_check_declarator_template_parameters
1638 (cp_parser *, tree);
1639 static bool cp_parser_check_template_parameters
1640 (cp_parser *, unsigned);
1641 static tree cp_parser_simple_cast_expression
1643 static tree cp_parser_binary_expression
1644 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1645 static tree cp_parser_global_scope_opt
1646 (cp_parser *, bool);
1647 static bool cp_parser_constructor_declarator_p
1648 (cp_parser *, bool);
1649 static tree cp_parser_function_definition_from_specifiers_and_declarator
1650 (cp_parser *, tree, tree, tree);
1651 static tree cp_parser_function_definition_after_declarator
1652 (cp_parser *, bool);
1653 static void cp_parser_template_declaration_after_export
1654 (cp_parser *, bool);
1655 static tree cp_parser_single_declaration
1656 (cp_parser *, bool, bool *);
1657 static tree cp_parser_functional_cast
1658 (cp_parser *, tree);
1659 static tree cp_parser_save_member_function_body
1660 (cp_parser *, tree, tree, tree);
1661 static tree cp_parser_enclosed_template_argument_list
1663 static void cp_parser_save_default_args
1664 (cp_parser *, tree);
1665 static void cp_parser_late_parsing_for_member
1666 (cp_parser *, tree);
1667 static void cp_parser_late_parsing_default_args
1668 (cp_parser *, tree);
1669 static tree cp_parser_sizeof_operand
1670 (cp_parser *, enum rid);
1671 static bool cp_parser_declares_only_class_p
1673 static bool cp_parser_friend_p
1675 static bool cp_parser_typedef_p
1677 static cp_token *cp_parser_require
1678 (cp_parser *, enum cpp_ttype, const char *);
1679 static cp_token *cp_parser_require_keyword
1680 (cp_parser *, enum rid, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1683 static bool cp_parser_next_token_starts_class_definition_p
1685 static bool cp_parser_next_token_ends_template_argument_p
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser *, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1691 static void cp_parser_check_class_key
1692 (enum tag_types, tree type);
1693 static void cp_parser_check_access_in_redeclaration
1695 static bool cp_parser_optional_template_keyword
1697 static void cp_parser_pre_parsed_nested_name_specifier
1699 static void cp_parser_cache_group
1700 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1701 static void cp_parser_parse_tentatively
1703 static void cp_parser_commit_to_tentative_parse
1705 static void cp_parser_abort_tentative_parse
1707 static bool cp_parser_parse_definitely
1709 static inline bool cp_parser_parsing_tentatively
1711 static bool cp_parser_committed_to_tentative_parse
1713 static void cp_parser_error
1714 (cp_parser *, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser *, tree, tree, const char *);
1717 static bool cp_parser_simulate_error
1719 static void cp_parser_check_type_definition
1721 static void cp_parser_check_for_definition_in_return_type
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser *, tree);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser *, const char *);
1727 static bool cp_parser_diagnose_invalid_type_name
1729 static int cp_parser_skip_to_closing_parenthesis
1730 (cp_parser *, bool, bool, bool);
1731 static void cp_parser_skip_to_end_of_statement
1733 static void cp_parser_consume_semicolon_at_end_of_statement
1735 static void cp_parser_skip_to_end_of_block_or_statement
1737 static void cp_parser_skip_to_closing_brace
1739 static void cp_parser_skip_until_found
1740 (cp_parser *, enum cpp_ttype, const char *);
1741 static bool cp_parser_error_occurred
1743 static bool cp_parser_allow_gnu_extensions_p
1745 static bool cp_parser_is_string_literal
1747 static bool cp_parser_is_keyword
1748 (cp_token *, enum rid);
1750 /* Returns nonzero if we are parsing tentatively. */
1753 cp_parser_parsing_tentatively (cp_parser* parser)
1755 return parser->context->next != NULL;
1758 /* Returns nonzero if TOKEN is a string literal. */
1761 cp_parser_is_string_literal (cp_token* token)
1763 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1769 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1771 return token->keyword == keyword;
1774 /* Issue the indicated error MESSAGE. */
1777 cp_parser_error (cp_parser* parser, const char* message)
1779 /* Output the MESSAGE -- unless we're parsing tentatively. */
1780 if (!cp_parser_simulate_error (parser))
1783 token = cp_lexer_peek_token (parser->lexer);
1784 c_parse_error (message,
1785 /* Because c_parser_error does not understand
1786 CPP_KEYWORD, keywords are treated like
1788 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1793 /* Issue an error about name-lookup failing. NAME is the
1794 IDENTIFIER_NODE DECL is the result of
1795 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1796 the thing that we hoped to find. */
1799 cp_parser_name_lookup_error (cp_parser* parser,
1802 const char* desired)
1804 /* If name lookup completely failed, tell the user that NAME was not
1806 if (decl == error_mark_node)
1808 if (parser->scope && parser->scope != global_namespace)
1809 error ("`%D::%D' has not been declared",
1810 parser->scope, name);
1811 else if (parser->scope == global_namespace)
1812 error ("`::%D' has not been declared", name);
1814 error ("`%D' has not been declared", name);
1816 else if (parser->scope && parser->scope != global_namespace)
1817 error ("`%D::%D' %s", parser->scope, name, desired);
1818 else if (parser->scope == global_namespace)
1819 error ("`::%D' %s", name, desired);
1821 error ("`%D' %s", name, desired);
1824 /* If we are parsing tentatively, remember that an error has occurred
1825 during this tentative parse. Returns true if the error was
1826 simulated; false if a messgae should be issued by the caller. */
1829 cp_parser_simulate_error (cp_parser* parser)
1831 if (cp_parser_parsing_tentatively (parser)
1832 && !cp_parser_committed_to_tentative_parse (parser))
1834 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1840 /* This function is called when a type is defined. If type
1841 definitions are forbidden at this point, an error message is
1845 cp_parser_check_type_definition (cp_parser* parser)
1847 /* If types are forbidden here, issue a message. */
1848 if (parser->type_definition_forbidden_message)
1849 /* Use `%s' to print the string in case there are any escape
1850 characters in the message. */
1851 error ("%s", parser->type_definition_forbidden_message);
1854 /* This function is called when a declaration is parsed. If
1855 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1856 indicates that a type was defined in the decl-specifiers for DECL,
1857 then an error is issued. */
1860 cp_parser_check_for_definition_in_return_type (tree declarator,
1861 int declares_class_or_enum)
1863 /* [dcl.fct] forbids type definitions in return types.
1864 Unfortunately, it's not easy to know whether or not we are
1865 processing a return type until after the fact. */
1867 && (TREE_CODE (declarator) == INDIRECT_REF
1868 || TREE_CODE (declarator) == ADDR_EXPR))
1869 declarator = TREE_OPERAND (declarator, 0);
1871 && TREE_CODE (declarator) == CALL_EXPR
1872 && declares_class_or_enum & 2)
1873 error ("new types may not be defined in a return type");
1876 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1877 "<" in any valid C++ program. If the next token is indeed "<",
1878 issue a message warning the user about what appears to be an
1879 invalid attempt to form a template-id. */
1882 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1888 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1891 error ("`%T' is not a template", type);
1892 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1893 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1895 error ("invalid template-id");
1896 /* Remember the location of the invalid "<". */
1897 if (cp_parser_parsing_tentatively (parser)
1898 && !cp_parser_committed_to_tentative_parse (parser))
1900 token = cp_lexer_peek_token (parser->lexer);
1901 token = cp_lexer_prev_token (parser->lexer, token);
1902 start = cp_lexer_token_difference (parser->lexer,
1903 parser->lexer->first_token,
1908 /* Consume the "<". */
1909 cp_lexer_consume_token (parser->lexer);
1910 /* Parse the template arguments. */
1911 cp_parser_enclosed_template_argument_list (parser);
1912 /* Permanently remove the invalid template arguments so that
1913 this error message is not issued again. */
1916 token = cp_lexer_advance_token (parser->lexer,
1917 parser->lexer->first_token,
1919 cp_lexer_purge_tokens_after (parser->lexer, token);
1924 /* If parsing an integral constant-expression, issue an error message
1925 about the fact that THING appeared and return true. Otherwise,
1926 return false, marking the current expression as non-constant. */
1929 cp_parser_non_integral_constant_expression (cp_parser *parser,
1932 if (parser->integral_constant_expression_p)
1934 if (!parser->allow_non_integral_constant_expression_p)
1936 error ("%s cannot appear in a constant-expression", thing);
1939 parser->non_integral_constant_expression_p = true;
1944 /* Check for a common situation where a type-name should be present,
1945 but is not, and issue a sensible error message. Returns true if an
1946 invalid type-name was detected. */
1949 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1951 /* If the next two tokens are both identifiers, the code is
1952 erroneous. The usual cause of this situation is code like:
1956 where "T" should name a type -- but does not. */
1957 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1958 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1962 /* If parsing tentatively, we should commit; we really are
1963 looking at a declaration. */
1964 /* Consume the first identifier. */
1965 name = cp_lexer_consume_token (parser->lexer)->value;
1966 /* Issue an error message. */
1967 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1968 /* If we're in a template class, it's possible that the user was
1969 referring to a type from a base class. For example:
1971 template <typename T> struct A { typedef T X; };
1972 template <typename T> struct B : public A<T> { X x; };
1974 The user should have said "typename A<T>::X". */
1975 if (processing_template_decl && current_class_type)
1979 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1983 tree base_type = BINFO_TYPE (b);
1984 if (CLASS_TYPE_P (base_type)
1985 && dependent_type_p (base_type))
1988 /* Go from a particular instantiation of the
1989 template (which will have an empty TYPE_FIELDs),
1990 to the main version. */
1991 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1992 for (field = TYPE_FIELDS (base_type);
1994 field = TREE_CHAIN (field))
1995 if (TREE_CODE (field) == TYPE_DECL
1996 && DECL_NAME (field) == name)
1998 error ("(perhaps `typename %T::%s' was intended)",
1999 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2007 /* Skip to the end of the declaration; there's no point in
2008 trying to process it. */
2009 cp_parser_skip_to_end_of_statement (parser);
2017 /* Consume tokens up to, and including, the next non-nested closing `)'.
2018 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2019 are doing error recovery. Returns -1 if OR_COMMA is true and we
2020 found an unnested comma. */
2023 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2028 unsigned paren_depth = 0;
2029 unsigned brace_depth = 0;
2031 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2032 && !cp_parser_committed_to_tentative_parse (parser))
2039 /* If we've run out of tokens, then there is no closing `)'. */
2040 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2043 token = cp_lexer_peek_token (parser->lexer);
2045 /* This matches the processing in skip_to_end_of_statement. */
2046 if (token->type == CPP_SEMICOLON && !brace_depth)
2048 if (token->type == CPP_OPEN_BRACE)
2050 if (token->type == CPP_CLOSE_BRACE)
2055 if (recovering && or_comma && token->type == CPP_COMMA
2056 && !brace_depth && !paren_depth)
2061 /* If it is an `(', we have entered another level of nesting. */
2062 if (token->type == CPP_OPEN_PAREN)
2064 /* If it is a `)', then we might be done. */
2065 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2068 cp_lexer_consume_token (parser->lexer);
2073 /* Consume the token. */
2074 cp_lexer_consume_token (parser->lexer);
2078 /* Consume tokens until we reach the end of the current statement.
2079 Normally, that will be just before consuming a `;'. However, if a
2080 non-nested `}' comes first, then we stop before consuming that. */
2083 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2085 unsigned nesting_depth = 0;
2091 /* Peek at the next token. */
2092 token = cp_lexer_peek_token (parser->lexer);
2093 /* If we've run out of tokens, stop. */
2094 if (token->type == CPP_EOF)
2096 /* If the next token is a `;', we have reached the end of the
2098 if (token->type == CPP_SEMICOLON && !nesting_depth)
2100 /* If the next token is a non-nested `}', then we have reached
2101 the end of the current block. */
2102 if (token->type == CPP_CLOSE_BRACE)
2104 /* If this is a non-nested `}', stop before consuming it.
2105 That way, when confronted with something like:
2109 we stop before consuming the closing `}', even though we
2110 have not yet reached a `;'. */
2111 if (nesting_depth == 0)
2113 /* If it is the closing `}' for a block that we have
2114 scanned, stop -- but only after consuming the token.
2120 we will stop after the body of the erroneously declared
2121 function, but before consuming the following `typedef'
2123 if (--nesting_depth == 0)
2125 cp_lexer_consume_token (parser->lexer);
2129 /* If it the next token is a `{', then we are entering a new
2130 block. Consume the entire block. */
2131 else if (token->type == CPP_OPEN_BRACE)
2133 /* Consume the token. */
2134 cp_lexer_consume_token (parser->lexer);
2138 /* This function is called at the end of a statement or declaration.
2139 If the next token is a semicolon, it is consumed; otherwise, error
2140 recovery is attempted. */
2143 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2145 /* Look for the trailing `;'. */
2146 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2148 /* If there is additional (erroneous) input, skip to the end of
2150 cp_parser_skip_to_end_of_statement (parser);
2151 /* If the next token is now a `;', consume it. */
2152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2153 cp_lexer_consume_token (parser->lexer);
2157 /* Skip tokens until we have consumed an entire block, or until we
2158 have consumed a non-nested `;'. */
2161 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2163 unsigned nesting_depth = 0;
2169 /* Peek at the next token. */
2170 token = cp_lexer_peek_token (parser->lexer);
2171 /* If we've run out of tokens, stop. */
2172 if (token->type == CPP_EOF)
2174 /* If the next token is a `;', we have reached the end of the
2176 if (token->type == CPP_SEMICOLON && !nesting_depth)
2178 /* Consume the `;'. */
2179 cp_lexer_consume_token (parser->lexer);
2182 /* Consume the token. */
2183 token = cp_lexer_consume_token (parser->lexer);
2184 /* If the next token is a non-nested `}', then we have reached
2185 the end of the current block. */
2186 if (token->type == CPP_CLOSE_BRACE
2187 && (nesting_depth == 0 || --nesting_depth == 0))
2189 /* If it the next token is a `{', then we are entering a new
2190 block. Consume the entire block. */
2191 if (token->type == CPP_OPEN_BRACE)
2196 /* Skip tokens until a non-nested closing curly brace is the next
2200 cp_parser_skip_to_closing_brace (cp_parser *parser)
2202 unsigned nesting_depth = 0;
2208 /* Peek at the next token. */
2209 token = cp_lexer_peek_token (parser->lexer);
2210 /* If we've run out of tokens, stop. */
2211 if (token->type == CPP_EOF)
2213 /* If the next token is a non-nested `}', then we have reached
2214 the end of the current block. */
2215 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2217 /* If it the next token is a `{', then we are entering a new
2218 block. Consume the entire block. */
2219 else if (token->type == CPP_OPEN_BRACE)
2221 /* Consume the token. */
2222 cp_lexer_consume_token (parser->lexer);
2226 /* Create a new C++ parser. */
2229 cp_parser_new (void)
2234 /* cp_lexer_new_main is called before calling ggc_alloc because
2235 cp_lexer_new_main might load a PCH file. */
2236 lexer = cp_lexer_new_main ();
2238 parser = ggc_alloc_cleared (sizeof (cp_parser));
2239 parser->lexer = lexer;
2240 parser->context = cp_parser_context_new (NULL);
2242 /* For now, we always accept GNU extensions. */
2243 parser->allow_gnu_extensions_p = 1;
2245 /* The `>' token is a greater-than operator, not the end of a
2247 parser->greater_than_is_operator_p = true;
2249 parser->default_arg_ok_p = true;
2251 /* We are not parsing a constant-expression. */
2252 parser->integral_constant_expression_p = false;
2253 parser->allow_non_integral_constant_expression_p = false;
2254 parser->non_integral_constant_expression_p = false;
2256 /* We are not parsing offsetof. */
2257 parser->in_offsetof_p = false;
2259 /* Local variable names are not forbidden. */
2260 parser->local_variables_forbidden_p = false;
2262 /* We are not processing an `extern "C"' declaration. */
2263 parser->in_unbraced_linkage_specification_p = false;
2265 /* We are not processing a declarator. */
2266 parser->in_declarator_p = false;
2268 /* We are not processing a template-argument-list. */
2269 parser->in_template_argument_list_p = false;
2271 /* We are not in an iteration statement. */
2272 parser->in_iteration_statement_p = false;
2274 /* We are not in a switch statement. */
2275 parser->in_switch_statement_p = false;
2277 /* We are not parsing a type-id inside an expression. */
2278 parser->in_type_id_in_expr_p = false;
2280 /* The unparsed function queue is empty. */
2281 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2283 /* There are no classes being defined. */
2284 parser->num_classes_being_defined = 0;
2286 /* No template parameters apply. */
2287 parser->num_template_parameter_lists = 0;
2292 /* Lexical conventions [gram.lex] */
2294 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2298 cp_parser_identifier (cp_parser* parser)
2302 /* Look for the identifier. */
2303 token = cp_parser_require (parser, CPP_NAME, "identifier");
2304 /* Return the value. */
2305 return token ? token->value : error_mark_node;
2308 /* Basic concepts [gram.basic] */
2310 /* Parse a translation-unit.
2313 declaration-seq [opt]
2315 Returns TRUE if all went well. */
2318 cp_parser_translation_unit (cp_parser* parser)
2322 cp_parser_declaration_seq_opt (parser);
2324 /* If there are no tokens left then all went well. */
2325 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2328 /* Otherwise, issue an error message. */
2329 cp_parser_error (parser, "expected declaration");
2333 /* Consume the EOF token. */
2334 cp_parser_require (parser, CPP_EOF, "end-of-file");
2337 finish_translation_unit ();
2339 /* All went well. */
2343 /* Expressions [gram.expr] */
2345 /* Parse a primary-expression.
2356 ( compound-statement )
2357 __builtin_va_arg ( assignment-expression , type-id )
2362 Returns a representation of the expression.
2364 *IDK indicates what kind of id-expression (if any) was present.
2366 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2367 used as the operand of a pointer-to-member. In that case,
2368 *QUALIFYING_CLASS gives the class that is used as the qualifying
2369 class in the pointer-to-member. */
2372 cp_parser_primary_expression (cp_parser *parser,
2374 tree *qualifying_class)
2378 /* Assume the primary expression is not an id-expression. */
2379 *idk = CP_ID_KIND_NONE;
2380 /* And that it cannot be used as pointer-to-member. */
2381 *qualifying_class = NULL_TREE;
2383 /* Peek at the next token. */
2384 token = cp_lexer_peek_token (parser->lexer);
2385 switch (token->type)
2398 token = cp_lexer_consume_token (parser->lexer);
2399 return token->value;
2401 case CPP_OPEN_PAREN:
2404 bool saved_greater_than_is_operator_p;
2406 /* Consume the `('. */
2407 cp_lexer_consume_token (parser->lexer);
2408 /* Within a parenthesized expression, a `>' token is always
2409 the greater-than operator. */
2410 saved_greater_than_is_operator_p
2411 = parser->greater_than_is_operator_p;
2412 parser->greater_than_is_operator_p = true;
2413 /* If we see `( { ' then we are looking at the beginning of
2414 a GNU statement-expression. */
2415 if (cp_parser_allow_gnu_extensions_p (parser)
2416 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2418 /* Statement-expressions are not allowed by the standard. */
2420 pedwarn ("ISO C++ forbids braced-groups within expressions");
2422 /* And they're not allowed outside of a function-body; you
2423 cannot, for example, write:
2425 int i = ({ int j = 3; j + 1; });
2427 at class or namespace scope. */
2428 if (!at_function_scope_p ())
2429 error ("statement-expressions are allowed only inside functions");
2430 /* Start the statement-expression. */
2431 expr = begin_stmt_expr ();
2432 /* Parse the compound-statement. */
2433 cp_parser_compound_statement (parser, true);
2435 expr = finish_stmt_expr (expr, false);
2439 /* Parse the parenthesized expression. */
2440 expr = cp_parser_expression (parser);
2441 /* Let the front end know that this expression was
2442 enclosed in parentheses. This matters in case, for
2443 example, the expression is of the form `A::B', since
2444 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2446 finish_parenthesized_expr (expr);
2448 /* The `>' token might be the end of a template-id or
2449 template-parameter-list now. */
2450 parser->greater_than_is_operator_p
2451 = saved_greater_than_is_operator_p;
2452 /* Consume the `)'. */
2453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2454 cp_parser_skip_to_end_of_statement (parser);
2460 switch (token->keyword)
2462 /* These two are the boolean literals. */
2464 cp_lexer_consume_token (parser->lexer);
2465 return boolean_true_node;
2467 cp_lexer_consume_token (parser->lexer);
2468 return boolean_false_node;
2470 /* The `__null' literal. */
2472 cp_lexer_consume_token (parser->lexer);
2475 /* Recognize the `this' keyword. */
2477 cp_lexer_consume_token (parser->lexer);
2478 if (parser->local_variables_forbidden_p)
2480 error ("`this' may not be used in this context");
2481 return error_mark_node;
2483 /* Pointers cannot appear in constant-expressions. */
2484 if (cp_parser_non_integral_constant_expression (parser,
2486 return error_mark_node;
2487 return finish_this_expr ();
2489 /* The `operator' keyword can be the beginning of an
2494 case RID_FUNCTION_NAME:
2495 case RID_PRETTY_FUNCTION_NAME:
2496 case RID_C99_FUNCTION_NAME:
2497 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2498 __func__ are the names of variables -- but they are
2499 treated specially. Therefore, they are handled here,
2500 rather than relying on the generic id-expression logic
2501 below. Grammatically, these names are id-expressions.
2503 Consume the token. */
2504 token = cp_lexer_consume_token (parser->lexer);
2505 /* Look up the name. */
2506 return finish_fname (token->value);
2513 /* The `__builtin_va_arg' construct is used to handle
2514 `va_arg'. Consume the `__builtin_va_arg' token. */
2515 cp_lexer_consume_token (parser->lexer);
2516 /* Look for the opening `('. */
2517 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2518 /* Now, parse the assignment-expression. */
2519 expression = cp_parser_assignment_expression (parser);
2520 /* Look for the `,'. */
2521 cp_parser_require (parser, CPP_COMMA, "`,'");
2522 /* Parse the type-id. */
2523 type = cp_parser_type_id (parser);
2524 /* Look for the closing `)'. */
2525 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2526 /* Using `va_arg' in a constant-expression is not
2528 if (cp_parser_non_integral_constant_expression (parser,
2530 return error_mark_node;
2531 return build_x_va_arg (expression, type);
2537 bool saved_in_offsetof_p;
2539 /* Consume the "__offsetof__" token. */
2540 cp_lexer_consume_token (parser->lexer);
2541 /* Consume the opening `('. */
2542 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2543 /* Parse the parenthesized (almost) constant-expression. */
2544 saved_in_offsetof_p = parser->in_offsetof_p;
2545 parser->in_offsetof_p = true;
2547 = cp_parser_constant_expression (parser,
2548 /*allow_non_constant_p=*/false,
2549 /*non_constant_p=*/NULL);
2550 parser->in_offsetof_p = saved_in_offsetof_p;
2551 /* Consume the closing ')'. */
2552 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2558 cp_parser_error (parser, "expected primary-expression");
2559 return error_mark_node;
2562 /* An id-expression can start with either an identifier, a
2563 `::' as the beginning of a qualified-id, or the "operator"
2567 case CPP_TEMPLATE_ID:
2568 case CPP_NESTED_NAME_SPECIFIER:
2572 const char *error_msg;
2575 /* Parse the id-expression. */
2577 = cp_parser_id_expression (parser,
2578 /*template_keyword_p=*/false,
2579 /*check_dependency_p=*/true,
2580 /*template_p=*/NULL,
2581 /*declarator_p=*/false);
2582 if (id_expression == error_mark_node)
2583 return error_mark_node;
2584 /* If we have a template-id, then no further lookup is
2585 required. If the template-id was for a template-class, we
2586 will sometimes have a TYPE_DECL at this point. */
2587 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2588 || TREE_CODE (id_expression) == TYPE_DECL)
2589 decl = id_expression;
2590 /* Look up the name. */
2593 decl = cp_parser_lookup_name_simple (parser, id_expression);
2594 /* If name lookup gives us a SCOPE_REF, then the
2595 qualifying scope was dependent. Just propagate the
2597 if (TREE_CODE (decl) == SCOPE_REF)
2599 if (TYPE_P (TREE_OPERAND (decl, 0)))
2600 *qualifying_class = TREE_OPERAND (decl, 0);
2603 /* Check to see if DECL is a local variable in a context
2604 where that is forbidden. */
2605 if (parser->local_variables_forbidden_p
2606 && local_variable_p (decl))
2608 /* It might be that we only found DECL because we are
2609 trying to be generous with pre-ISO scoping rules.
2610 For example, consider:
2614 for (int i = 0; i < 10; ++i) {}
2615 extern void f(int j = i);
2618 Here, name look up will originally find the out
2619 of scope `i'. We need to issue a warning message,
2620 but then use the global `i'. */
2621 decl = check_for_out_of_scope_variable (decl);
2622 if (local_variable_p (decl))
2624 error ("local variable `%D' may not appear in this context",
2626 return error_mark_node;
2631 decl = finish_id_expression (id_expression, decl, parser->scope,
2632 idk, qualifying_class,
2633 parser->integral_constant_expression_p,
2634 parser->allow_non_integral_constant_expression_p,
2635 &parser->non_integral_constant_expression_p,
2638 cp_parser_error (parser, error_msg);
2642 /* Anything else is an error. */
2644 cp_parser_error (parser, "expected primary-expression");
2645 return error_mark_node;
2649 /* Parse an id-expression.
2656 :: [opt] nested-name-specifier template [opt] unqualified-id
2658 :: operator-function-id
2661 Return a representation of the unqualified portion of the
2662 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2663 a `::' or nested-name-specifier.
2665 Often, if the id-expression was a qualified-id, the caller will
2666 want to make a SCOPE_REF to represent the qualified-id. This
2667 function does not do this in order to avoid wastefully creating
2668 SCOPE_REFs when they are not required.
2670 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2673 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2674 uninstantiated templates.
2676 If *TEMPLATE_P is non-NULL, it is set to true iff the
2677 `template' keyword is used to explicitly indicate that the entity
2678 named is a template.
2680 If DECLARATOR_P is true, the id-expression is appearing as part of
2681 a declarator, rather than as part of an expression. */
2684 cp_parser_id_expression (cp_parser *parser,
2685 bool template_keyword_p,
2686 bool check_dependency_p,
2690 bool global_scope_p;
2691 bool nested_name_specifier_p;
2693 /* Assume the `template' keyword was not used. */
2695 *template_p = false;
2697 /* Look for the optional `::' operator. */
2699 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2701 /* Look for the optional nested-name-specifier. */
2702 nested_name_specifier_p
2703 = (cp_parser_nested_name_specifier_opt (parser,
2704 /*typename_keyword_p=*/false,
2709 /* If there is a nested-name-specifier, then we are looking at
2710 the first qualified-id production. */
2711 if (nested_name_specifier_p)
2714 tree saved_object_scope;
2715 tree saved_qualifying_scope;
2716 tree unqualified_id;
2719 /* See if the next token is the `template' keyword. */
2721 template_p = &is_template;
2722 *template_p = cp_parser_optional_template_keyword (parser);
2723 /* Name lookup we do during the processing of the
2724 unqualified-id might obliterate SCOPE. */
2725 saved_scope = parser->scope;
2726 saved_object_scope = parser->object_scope;
2727 saved_qualifying_scope = parser->qualifying_scope;
2728 /* Process the final unqualified-id. */
2729 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2732 /* Restore the SAVED_SCOPE for our caller. */
2733 parser->scope = saved_scope;
2734 parser->object_scope = saved_object_scope;
2735 parser->qualifying_scope = saved_qualifying_scope;
2737 return unqualified_id;
2739 /* Otherwise, if we are in global scope, then we are looking at one
2740 of the other qualified-id productions. */
2741 else if (global_scope_p)
2746 /* Peek at the next token. */
2747 token = cp_lexer_peek_token (parser->lexer);
2749 /* If it's an identifier, and the next token is not a "<", then
2750 we can avoid the template-id case. This is an optimization
2751 for this common case. */
2752 if (token->type == CPP_NAME
2753 && !cp_parser_nth_token_starts_template_argument_list_p
2755 return cp_parser_identifier (parser);
2757 cp_parser_parse_tentatively (parser);
2758 /* Try a template-id. */
2759 id = cp_parser_template_id (parser,
2760 /*template_keyword_p=*/false,
2761 /*check_dependency_p=*/true,
2763 /* If that worked, we're done. */
2764 if (cp_parser_parse_definitely (parser))
2767 /* Peek at the next token. (Changes in the token buffer may
2768 have invalidated the pointer obtained above.) */
2769 token = cp_lexer_peek_token (parser->lexer);
2771 switch (token->type)
2774 return cp_parser_identifier (parser);
2777 if (token->keyword == RID_OPERATOR)
2778 return cp_parser_operator_function_id (parser);
2782 cp_parser_error (parser, "expected id-expression");
2783 return error_mark_node;
2787 return cp_parser_unqualified_id (parser, template_keyword_p,
2788 /*check_dependency_p=*/true,
2792 /* Parse an unqualified-id.
2796 operator-function-id
2797 conversion-function-id
2801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2802 keyword, in a construct like `A::template ...'.
2804 Returns a representation of unqualified-id. For the `identifier'
2805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2806 production a BIT_NOT_EXPR is returned; the operand of the
2807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2808 other productions, see the documentation accompanying the
2809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2810 names are looked up in uninstantiated templates. If DECLARATOR_P
2811 is true, the unqualified-id is appearing as part of a declarator,
2812 rather than as part of an expression. */
2815 cp_parser_unqualified_id (cp_parser* parser,
2816 bool template_keyword_p,
2817 bool check_dependency_p,
2822 /* Peek at the next token. */
2823 token = cp_lexer_peek_token (parser->lexer);
2825 switch (token->type)
2831 /* We don't know yet whether or not this will be a
2833 cp_parser_parse_tentatively (parser);
2834 /* Try a template-id. */
2835 id = cp_parser_template_id (parser, template_keyword_p,
2838 /* If it worked, we're done. */
2839 if (cp_parser_parse_definitely (parser))
2841 /* Otherwise, it's an ordinary identifier. */
2842 return cp_parser_identifier (parser);
2845 case CPP_TEMPLATE_ID:
2846 return cp_parser_template_id (parser, template_keyword_p,
2853 tree qualifying_scope;
2858 /* Consume the `~' token. */
2859 cp_lexer_consume_token (parser->lexer);
2860 /* Parse the class-name. The standard, as written, seems to
2863 template <typename T> struct S { ~S (); };
2864 template <typename T> S<T>::~S() {}
2866 is invalid, since `~' must be followed by a class-name, but
2867 `S<T>' is dependent, and so not known to be a class.
2868 That's not right; we need to look in uninstantiated
2869 templates. A further complication arises from:
2871 template <typename T> void f(T t) {
2875 Here, it is not possible to look up `T' in the scope of `T'
2876 itself. We must look in both the current scope, and the
2877 scope of the containing complete expression.
2879 Yet another issue is:
2888 The standard does not seem to say that the `S' in `~S'
2889 should refer to the type `S' and not the data member
2892 /* DR 244 says that we look up the name after the "~" in the
2893 same scope as we looked up the qualifying name. That idea
2894 isn't fully worked out; it's more complicated than that. */
2895 scope = parser->scope;
2896 object_scope = parser->object_scope;
2897 qualifying_scope = parser->qualifying_scope;
2899 /* If the name is of the form "X::~X" it's OK. */
2900 if (scope && TYPE_P (scope)
2901 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2902 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2904 && (cp_lexer_peek_token (parser->lexer)->value
2905 == TYPE_IDENTIFIER (scope)))
2907 cp_lexer_consume_token (parser->lexer);
2908 return build_nt (BIT_NOT_EXPR, scope);
2911 /* If there was an explicit qualification (S::~T), first look
2912 in the scope given by the qualification (i.e., S). */
2914 type_decl = NULL_TREE;
2917 cp_parser_parse_tentatively (parser);
2918 type_decl = cp_parser_class_name (parser,
2919 /*typename_keyword_p=*/false,
2920 /*template_keyword_p=*/false,
2922 /*check_dependency=*/false,
2923 /*class_head_p=*/false,
2925 if (cp_parser_parse_definitely (parser))
2928 /* In "N::S::~S", look in "N" as well. */
2929 if (!done && scope && qualifying_scope)
2931 cp_parser_parse_tentatively (parser);
2932 parser->scope = qualifying_scope;
2933 parser->object_scope = NULL_TREE;
2934 parser->qualifying_scope = NULL_TREE;
2936 = cp_parser_class_name (parser,
2937 /*typename_keyword_p=*/false,
2938 /*template_keyword_p=*/false,
2940 /*check_dependency=*/false,
2941 /*class_head_p=*/false,
2943 if (cp_parser_parse_definitely (parser))
2946 /* In "p->S::~T", look in the scope given by "*p" as well. */
2947 else if (!done && object_scope)
2949 cp_parser_parse_tentatively (parser);
2950 parser->scope = object_scope;
2951 parser->object_scope = NULL_TREE;
2952 parser->qualifying_scope = NULL_TREE;
2954 = cp_parser_class_name (parser,
2955 /*typename_keyword_p=*/false,
2956 /*template_keyword_p=*/false,
2958 /*check_dependency=*/false,
2959 /*class_head_p=*/false,
2961 if (cp_parser_parse_definitely (parser))
2964 /* Look in the surrounding context. */
2967 parser->scope = NULL_TREE;
2968 parser->object_scope = NULL_TREE;
2969 parser->qualifying_scope = NULL_TREE;
2971 = cp_parser_class_name (parser,
2972 /*typename_keyword_p=*/false,
2973 /*template_keyword_p=*/false,
2975 /*check_dependency=*/false,
2976 /*class_head_p=*/false,
2979 /* If an error occurred, assume that the name of the
2980 destructor is the same as the name of the qualifying
2981 class. That allows us to keep parsing after running
2982 into ill-formed destructor names. */
2983 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2984 return build_nt (BIT_NOT_EXPR, scope);
2985 else if (type_decl == error_mark_node)
2986 return error_mark_node;
2990 A typedef-name that names a class shall not be used as the
2991 identifier in the declarator for a destructor declaration. */
2993 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2994 && !DECL_SELF_REFERENCE_P (type_decl))
2995 error ("typedef-name `%D' used as destructor declarator",
2998 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3002 if (token->keyword == RID_OPERATOR)
3006 /* This could be a template-id, so we try that first. */
3007 cp_parser_parse_tentatively (parser);
3008 /* Try a template-id. */
3009 id = cp_parser_template_id (parser, template_keyword_p,
3010 /*check_dependency_p=*/true,
3012 /* If that worked, we're done. */
3013 if (cp_parser_parse_definitely (parser))
3015 /* We still don't know whether we're looking at an
3016 operator-function-id or a conversion-function-id. */
3017 cp_parser_parse_tentatively (parser);
3018 /* Try an operator-function-id. */
3019 id = cp_parser_operator_function_id (parser);
3020 /* If that didn't work, try a conversion-function-id. */
3021 if (!cp_parser_parse_definitely (parser))
3022 id = cp_parser_conversion_function_id (parser);
3029 cp_parser_error (parser, "expected unqualified-id");
3030 return error_mark_node;
3034 /* Parse an (optional) nested-name-specifier.
3036 nested-name-specifier:
3037 class-or-namespace-name :: nested-name-specifier [opt]
3038 class-or-namespace-name :: template nested-name-specifier [opt]
3040 PARSER->SCOPE should be set appropriately before this function is
3041 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3042 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3045 Sets PARSER->SCOPE to the class (TYPE) or namespace
3046 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3047 it unchanged if there is no nested-name-specifier. Returns the new
3048 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3050 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3051 part of a declaration and/or decl-specifier. */
3054 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3055 bool typename_keyword_p,
3056 bool check_dependency_p,
3058 bool is_declaration)
3060 bool success = false;
3061 tree access_check = NULL_TREE;
3065 /* If the next token corresponds to a nested name specifier, there
3066 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3067 false, it may have been true before, in which case something
3068 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3069 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3070 CHECK_DEPENDENCY_P is false, we have to fall through into the
3072 if (check_dependency_p
3073 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3075 cp_parser_pre_parsed_nested_name_specifier (parser);
3076 return parser->scope;
3079 /* Remember where the nested-name-specifier starts. */
3080 if (cp_parser_parsing_tentatively (parser)
3081 && !cp_parser_committed_to_tentative_parse (parser))
3083 token = cp_lexer_peek_token (parser->lexer);
3084 start = cp_lexer_token_difference (parser->lexer,
3085 parser->lexer->first_token,
3091 push_deferring_access_checks (dk_deferred);
3097 tree saved_qualifying_scope;
3098 bool template_keyword_p;
3100 /* Spot cases that cannot be the beginning of a
3101 nested-name-specifier. */
3102 token = cp_lexer_peek_token (parser->lexer);
3104 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3105 the already parsed nested-name-specifier. */
3106 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3108 /* Grab the nested-name-specifier and continue the loop. */
3109 cp_parser_pre_parsed_nested_name_specifier (parser);
3114 /* Spot cases that cannot be the beginning of a
3115 nested-name-specifier. On the second and subsequent times
3116 through the loop, we look for the `template' keyword. */
3117 if (success && token->keyword == RID_TEMPLATE)
3119 /* A template-id can start a nested-name-specifier. */
3120 else if (token->type == CPP_TEMPLATE_ID)
3124 /* If the next token is not an identifier, then it is
3125 definitely not a class-or-namespace-name. */
3126 if (token->type != CPP_NAME)
3128 /* If the following token is neither a `<' (to begin a
3129 template-id), nor a `::', then we are not looking at a
3130 nested-name-specifier. */
3131 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3132 if (token->type != CPP_SCOPE
3133 && !cp_parser_nth_token_starts_template_argument_list_p
3138 /* The nested-name-specifier is optional, so we parse
3140 cp_parser_parse_tentatively (parser);
3142 /* Look for the optional `template' keyword, if this isn't the
3143 first time through the loop. */
3145 template_keyword_p = cp_parser_optional_template_keyword (parser);
3147 template_keyword_p = false;
3149 /* Save the old scope since the name lookup we are about to do
3150 might destroy it. */
3151 old_scope = parser->scope;
3152 saved_qualifying_scope = parser->qualifying_scope;
3153 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3154 look up names in "X<T>::I" in order to determine that "Y" is
3155 a template. So, if we have a typename at this point, we make
3156 an effort to look through it. */
3158 && !typename_keyword_p
3160 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3161 parser->scope = resolve_typename_type (parser->scope,
3162 /*only_current_p=*/false);
3163 /* Parse the qualifying entity. */
3165 = cp_parser_class_or_namespace_name (parser,
3171 /* Look for the `::' token. */
3172 cp_parser_require (parser, CPP_SCOPE, "`::'");
3174 /* If we found what we wanted, we keep going; otherwise, we're
3176 if (!cp_parser_parse_definitely (parser))
3178 bool error_p = false;
3180 /* Restore the OLD_SCOPE since it was valid before the
3181 failed attempt at finding the last
3182 class-or-namespace-name. */
3183 parser->scope = old_scope;
3184 parser->qualifying_scope = saved_qualifying_scope;
3185 /* If the next token is an identifier, and the one after
3186 that is a `::', then any valid interpretation would have
3187 found a class-or-namespace-name. */
3188 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3189 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3191 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3194 token = cp_lexer_consume_token (parser->lexer);
3199 decl = cp_parser_lookup_name_simple (parser, token->value);
3200 if (TREE_CODE (decl) == TEMPLATE_DECL)
3201 error ("`%D' used without template parameters",
3204 cp_parser_name_lookup_error
3205 (parser, token->value, decl,
3206 "is not a class or namespace");
3207 parser->scope = NULL_TREE;
3209 /* Treat this as a successful nested-name-specifier
3214 If the name found is not a class-name (clause
3215 _class_) or namespace-name (_namespace.def_), the
3216 program is ill-formed. */
3219 cp_lexer_consume_token (parser->lexer);
3224 /* We've found one valid nested-name-specifier. */
3226 /* Make sure we look in the right scope the next time through
3228 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3229 ? TREE_TYPE (new_scope)
3231 /* If it is a class scope, try to complete it; we are about to
3232 be looking up names inside the class. */
3233 if (TYPE_P (parser->scope)
3234 /* Since checking types for dependency can be expensive,
3235 avoid doing it if the type is already complete. */
3236 && !COMPLETE_TYPE_P (parser->scope)
3237 /* Do not try to complete dependent types. */
3238 && !dependent_type_p (parser->scope))
3239 complete_type (parser->scope);
3242 /* Retrieve any deferred checks. Do not pop this access checks yet
3243 so the memory will not be reclaimed during token replacing below. */
3244 access_check = get_deferred_access_checks ();
3246 /* If parsing tentatively, replace the sequence of tokens that makes
3247 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3248 token. That way, should we re-parse the token stream, we will
3249 not have to repeat the effort required to do the parse, nor will
3250 we issue duplicate error messages. */
3251 if (success && start >= 0)
3253 /* Find the token that corresponds to the start of the
3255 token = cp_lexer_advance_token (parser->lexer,
3256 parser->lexer->first_token,
3259 /* Reset the contents of the START token. */
3260 token->type = CPP_NESTED_NAME_SPECIFIER;
3261 token->value = build_tree_list (access_check, parser->scope);
3262 TREE_TYPE (token->value) = parser->qualifying_scope;
3263 token->keyword = RID_MAX;
3264 /* Purge all subsequent tokens. */
3265 cp_lexer_purge_tokens_after (parser->lexer, token);
3268 pop_deferring_access_checks ();
3269 return success ? parser->scope : NULL_TREE;
3272 /* Parse a nested-name-specifier. See
3273 cp_parser_nested_name_specifier_opt for details. This function
3274 behaves identically, except that it will an issue an error if no
3275 nested-name-specifier is present, and it will return
3276 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3280 cp_parser_nested_name_specifier (cp_parser *parser,
3281 bool typename_keyword_p,
3282 bool check_dependency_p,
3284 bool is_declaration)
3288 /* Look for the nested-name-specifier. */
3289 scope = cp_parser_nested_name_specifier_opt (parser,
3294 /* If it was not present, issue an error message. */
3297 cp_parser_error (parser, "expected nested-name-specifier");
3298 parser->scope = NULL_TREE;
3299 return error_mark_node;
3305 /* Parse a class-or-namespace-name.
3307 class-or-namespace-name:
3311 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3312 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3313 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3314 TYPE_P is TRUE iff the next name should be taken as a class-name,
3315 even the same name is declared to be another entity in the same
3318 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3319 specified by the class-or-namespace-name. If neither is found the
3320 ERROR_MARK_NODE is returned. */
3323 cp_parser_class_or_namespace_name (cp_parser *parser,
3324 bool typename_keyword_p,
3325 bool template_keyword_p,
3326 bool check_dependency_p,
3328 bool is_declaration)
3331 tree saved_qualifying_scope;
3332 tree saved_object_scope;
3336 /* Before we try to parse the class-name, we must save away the
3337 current PARSER->SCOPE since cp_parser_class_name will destroy
3339 saved_scope = parser->scope;
3340 saved_qualifying_scope = parser->qualifying_scope;
3341 saved_object_scope = parser->object_scope;
3342 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3343 there is no need to look for a namespace-name. */
3344 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3346 cp_parser_parse_tentatively (parser);
3347 scope = cp_parser_class_name (parser,
3352 /*class_head_p=*/false,
3354 /* If that didn't work, try for a namespace-name. */
3355 if (!only_class_p && !cp_parser_parse_definitely (parser))
3357 /* Restore the saved scope. */
3358 parser->scope = saved_scope;
3359 parser->qualifying_scope = saved_qualifying_scope;
3360 parser->object_scope = saved_object_scope;
3361 /* If we are not looking at an identifier followed by the scope
3362 resolution operator, then this is not part of a
3363 nested-name-specifier. (Note that this function is only used
3364 to parse the components of a nested-name-specifier.) */
3365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3366 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3367 return error_mark_node;
3368 scope = cp_parser_namespace_name (parser);
3374 /* Parse a postfix-expression.
3378 postfix-expression [ expression ]
3379 postfix-expression ( expression-list [opt] )
3380 simple-type-specifier ( expression-list [opt] )
3381 typename :: [opt] nested-name-specifier identifier
3382 ( expression-list [opt] )
3383 typename :: [opt] nested-name-specifier template [opt] template-id
3384 ( expression-list [opt] )
3385 postfix-expression . template [opt] id-expression
3386 postfix-expression -> template [opt] id-expression
3387 postfix-expression . pseudo-destructor-name
3388 postfix-expression -> pseudo-destructor-name
3389 postfix-expression ++
3390 postfix-expression --
3391 dynamic_cast < type-id > ( expression )
3392 static_cast < type-id > ( expression )
3393 reinterpret_cast < type-id > ( expression )
3394 const_cast < type-id > ( expression )
3395 typeid ( expression )
3401 ( type-id ) { initializer-list , [opt] }
3403 This extension is a GNU version of the C99 compound-literal
3404 construct. (The C99 grammar uses `type-name' instead of `type-id',
3405 but they are essentially the same concept.)
3407 If ADDRESS_P is true, the postfix expression is the operand of the
3410 Returns a representation of the expression. */
3413 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3417 cp_id_kind idk = CP_ID_KIND_NONE;
3418 tree postfix_expression = NULL_TREE;
3419 /* Non-NULL only if the current postfix-expression can be used to
3420 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3421 class used to qualify the member. */
3422 tree qualifying_class = NULL_TREE;
3424 /* Peek at the next token. */
3425 token = cp_lexer_peek_token (parser->lexer);
3426 /* Some of the productions are determined by keywords. */
3427 keyword = token->keyword;
3437 const char *saved_message;
3439 /* All of these can be handled in the same way from the point
3440 of view of parsing. Begin by consuming the token
3441 identifying the cast. */
3442 cp_lexer_consume_token (parser->lexer);
3444 /* New types cannot be defined in the cast. */
3445 saved_message = parser->type_definition_forbidden_message;
3446 parser->type_definition_forbidden_message
3447 = "types may not be defined in casts";
3449 /* Look for the opening `<'. */
3450 cp_parser_require (parser, CPP_LESS, "`<'");
3451 /* Parse the type to which we are casting. */
3452 type = cp_parser_type_id (parser);
3453 /* Look for the closing `>'. */
3454 cp_parser_require (parser, CPP_GREATER, "`>'");
3455 /* Restore the old message. */
3456 parser->type_definition_forbidden_message = saved_message;
3458 /* And the expression which is being cast. */
3459 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3460 expression = cp_parser_expression (parser);
3461 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3463 /* Only type conversions to integral or enumeration types
3464 can be used in constant-expressions. */
3465 if (parser->integral_constant_expression_p
3466 && !dependent_type_p (type)
3467 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3468 /* A cast to pointer or reference type is allowed in the
3469 implementation of "offsetof". */
3470 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3471 && (cp_parser_non_integral_constant_expression
3473 "a cast to a type other than an integral or "
3474 "enumeration type")))
3475 return error_mark_node;
3481 = build_dynamic_cast (type, expression);
3485 = build_static_cast (type, expression);
3489 = build_reinterpret_cast (type, expression);
3493 = build_const_cast (type, expression);
3504 const char *saved_message;
3505 bool saved_in_type_id_in_expr_p;
3507 /* Consume the `typeid' token. */
3508 cp_lexer_consume_token (parser->lexer);
3509 /* Look for the `(' token. */
3510 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3511 /* Types cannot be defined in a `typeid' expression. */
3512 saved_message = parser->type_definition_forbidden_message;
3513 parser->type_definition_forbidden_message
3514 = "types may not be defined in a `typeid\' expression";
3515 /* We can't be sure yet whether we're looking at a type-id or an
3517 cp_parser_parse_tentatively (parser);
3518 /* Try a type-id first. */
3519 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3520 parser->in_type_id_in_expr_p = true;
3521 type = cp_parser_type_id (parser);
3522 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3523 /* Look for the `)' token. Otherwise, we can't be sure that
3524 we're not looking at an expression: consider `typeid (int
3525 (3))', for example. */
3526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3527 /* If all went well, simply lookup the type-id. */
3528 if (cp_parser_parse_definitely (parser))
3529 postfix_expression = get_typeid (type);
3530 /* Otherwise, fall back to the expression variant. */
3535 /* Look for an expression. */
3536 expression = cp_parser_expression (parser);
3537 /* Compute its typeid. */
3538 postfix_expression = build_typeid (expression);
3539 /* Look for the `)' token. */
3540 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3542 /* `typeid' may not appear in an integral constant expression. */
3543 if (cp_parser_non_integral_constant_expression(parser,
3544 "`typeid' operator"))
3545 return error_mark_node;
3546 /* Restore the saved message. */
3547 parser->type_definition_forbidden_message = saved_message;
3553 bool template_p = false;
3558 /* Consume the `typename' token. */
3559 cp_lexer_consume_token (parser->lexer);
3560 /* Look for the optional `::' operator. */
3561 cp_parser_global_scope_opt (parser,
3562 /*current_scope_valid_p=*/false);
3563 /* Look for the nested-name-specifier. In case of error here,
3564 consume the trailing id to avoid subsequent error messages
3566 scope = cp_parser_nested_name_specifier (parser,
3567 /*typename_keyword_p=*/true,
3568 /*check_dependency_p=*/true,
3570 /*is_declaration=*/true);
3572 /* Look for the optional `template' keyword. */
3573 template_p = cp_parser_optional_template_keyword (parser);
3574 /* We don't know whether we're looking at a template-id or an
3576 cp_parser_parse_tentatively (parser);
3577 /* Try a template-id. */
3578 id = cp_parser_template_id (parser, template_p,
3579 /*check_dependency_p=*/true,
3580 /*is_declaration=*/true);
3581 /* If that didn't work, try an identifier. */
3582 if (!cp_parser_parse_definitely (parser))
3583 id = cp_parser_identifier (parser);
3585 /* Don't process id if nested name specifier is invalid. */
3586 if (scope == error_mark_node)
3587 return error_mark_node;
3588 /* If we look up a template-id in a non-dependent qualifying
3589 scope, there's no need to create a dependent type. */
3590 else if (TREE_CODE (id) == TYPE_DECL
3591 && !dependent_type_p (parser->scope))
3592 type = TREE_TYPE (id);
3593 /* Create a TYPENAME_TYPE to represent the type to which the
3594 functional cast is being performed. */
3596 type = make_typename_type (parser->scope, id,
3599 postfix_expression = cp_parser_functional_cast (parser, type);
3607 /* If the next thing is a simple-type-specifier, we may be
3608 looking at a functional cast. We could also be looking at
3609 an id-expression. So, we try the functional cast, and if
3610 that doesn't work we fall back to the primary-expression. */
3611 cp_parser_parse_tentatively (parser);
3612 /* Look for the simple-type-specifier. */
3613 type = cp_parser_simple_type_specifier (parser,
3614 CP_PARSER_FLAGS_NONE,
3615 /*identifier_p=*/false);
3616 /* Parse the cast itself. */
3617 if (!cp_parser_error_occurred (parser))
3619 = cp_parser_functional_cast (parser, type);
3620 /* If that worked, we're done. */
3621 if (cp_parser_parse_definitely (parser))
3624 /* If the functional-cast didn't work out, try a
3625 compound-literal. */
3626 if (cp_parser_allow_gnu_extensions_p (parser)
3627 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3629 tree initializer_list = NULL_TREE;
3630 bool saved_in_type_id_in_expr_p;
3632 cp_parser_parse_tentatively (parser);
3633 /* Consume the `('. */
3634 cp_lexer_consume_token (parser->lexer);
3635 /* Parse the type. */
3636 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3637 parser->in_type_id_in_expr_p = true;
3638 type = cp_parser_type_id (parser);
3639 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3640 /* Look for the `)'. */
3641 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3642 /* Look for the `{'. */
3643 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3644 /* If things aren't going well, there's no need to
3646 if (!cp_parser_error_occurred (parser))
3648 bool non_constant_p;
3649 /* Parse the initializer-list. */
3651 = cp_parser_initializer_list (parser, &non_constant_p);
3652 /* Allow a trailing `,'. */
3653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3654 cp_lexer_consume_token (parser->lexer);
3655 /* Look for the final `}'. */
3656 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3658 /* If that worked, we're definitely looking at a
3659 compound-literal expression. */
3660 if (cp_parser_parse_definitely (parser))
3662 /* Warn the user that a compound literal is not
3663 allowed in standard C++. */
3665 pedwarn ("ISO C++ forbids compound-literals");
3666 /* Form the representation of the compound-literal. */
3668 = finish_compound_literal (type, initializer_list);
3673 /* It must be a primary-expression. */
3674 postfix_expression = cp_parser_primary_expression (parser,
3681 /* If we were avoiding committing to the processing of a
3682 qualified-id until we knew whether or not we had a
3683 pointer-to-member, we now know. */
3684 if (qualifying_class)
3688 /* Peek at the next token. */
3689 token = cp_lexer_peek_token (parser->lexer);
3690 done = (token->type != CPP_OPEN_SQUARE
3691 && token->type != CPP_OPEN_PAREN
3692 && token->type != CPP_DOT
3693 && token->type != CPP_DEREF
3694 && token->type != CPP_PLUS_PLUS
3695 && token->type != CPP_MINUS_MINUS);
3697 postfix_expression = finish_qualified_id_expr (qualifying_class,
3702 return postfix_expression;
3705 /* Keep looping until the postfix-expression is complete. */
3708 if (idk == CP_ID_KIND_UNQUALIFIED
3709 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3710 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3711 /* It is not a Koenig lookup function call. */
3713 = unqualified_name_lookup_error (postfix_expression);
3715 /* Peek at the next token. */
3716 token = cp_lexer_peek_token (parser->lexer);
3718 switch (token->type)
3720 case CPP_OPEN_SQUARE:
3721 /* postfix-expression [ expression ] */
3725 /* Consume the `[' token. */
3726 cp_lexer_consume_token (parser->lexer);
3727 /* Parse the index expression. */
3728 index = cp_parser_expression (parser);
3729 /* Look for the closing `]'. */
3730 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3732 /* Build the ARRAY_REF. */
3734 = grok_array_decl (postfix_expression, index);
3735 idk = CP_ID_KIND_NONE;
3736 /* Array references are not permitted in
3737 constant-expressions (but they are allowed
3739 if (!parser->in_offsetof_p
3740 && cp_parser_non_integral_constant_expression
3741 (parser, "an array reference"))
3742 postfix_expression = error_mark_node;
3746 case CPP_OPEN_PAREN:
3747 /* postfix-expression ( expression-list [opt] ) */
3750 tree args = (cp_parser_parenthesized_expression_list
3751 (parser, false, /*non_constant_p=*/NULL));
3753 if (args == error_mark_node)
3755 postfix_expression = error_mark_node;
3759 /* Function calls are not permitted in
3760 constant-expressions. */
3761 if (cp_parser_non_integral_constant_expression (parser,
3764 postfix_expression = error_mark_node;
3769 if (idk == CP_ID_KIND_UNQUALIFIED)
3771 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3777 = perform_koenig_lookup (postfix_expression, args);
3781 = unqualified_fn_lookup_error (postfix_expression);
3783 /* We do not perform argument-dependent lookup if
3784 normal lookup finds a non-function, in accordance
3785 with the expected resolution of DR 218. */
3786 else if (args && is_overloaded_fn (postfix_expression))
3788 tree fn = get_first_fn (postfix_expression);
3789 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3790 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3791 /* Only do argument dependent lookup if regular
3792 lookup does not find a set of member functions.
3793 [basic.lookup.koenig]/2a */
3794 if (!DECL_FUNCTION_MEMBER_P (fn))
3798 = perform_koenig_lookup (postfix_expression, args);
3803 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3805 tree instance = TREE_OPERAND (postfix_expression, 0);
3806 tree fn = TREE_OPERAND (postfix_expression, 1);
3808 if (processing_template_decl
3809 && (type_dependent_expression_p (instance)
3810 || (!BASELINK_P (fn)
3811 && TREE_CODE (fn) != FIELD_DECL)
3812 || type_dependent_expression_p (fn)
3813 || any_type_dependent_arguments_p (args)))
3816 = build_min_nt (CALL_EXPR, postfix_expression, args);
3820 if (BASELINK_P (fn))
3822 = (build_new_method_call
3823 (instance, fn, args, NULL_TREE,
3824 (idk == CP_ID_KIND_QUALIFIED
3825 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3828 = finish_call_expr (postfix_expression, args,
3829 /*disallow_virtual=*/false,
3830 /*koenig_p=*/false);
3832 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3833 || TREE_CODE (postfix_expression) == MEMBER_REF
3834 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3835 postfix_expression = (build_offset_ref_call_from_tree
3836 (postfix_expression, args));
3837 else if (idk == CP_ID_KIND_QUALIFIED)
3838 /* A call to a static class member, or a namespace-scope
3841 = finish_call_expr (postfix_expression, args,
3842 /*disallow_virtual=*/true,
3845 /* All other function calls. */
3847 = finish_call_expr (postfix_expression, args,
3848 /*disallow_virtual=*/false,
3851 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3852 idk = CP_ID_KIND_NONE;
3858 /* postfix-expression . template [opt] id-expression
3859 postfix-expression . pseudo-destructor-name
3860 postfix-expression -> template [opt] id-expression
3861 postfix-expression -> pseudo-destructor-name */
3866 bool pseudo_destructor_p;
3867 tree scope = NULL_TREE;
3868 enum cpp_ttype token_type = token->type;
3870 /* If this is a `->' operator, dereference the pointer. */
3871 if (token->type == CPP_DEREF)
3872 postfix_expression = build_x_arrow (postfix_expression);
3873 /* Check to see whether or not the expression is
3875 dependent_p = type_dependent_expression_p (postfix_expression);
3876 /* The identifier following the `->' or `.' is not
3878 parser->scope = NULL_TREE;
3879 parser->qualifying_scope = NULL_TREE;
3880 parser->object_scope = NULL_TREE;
3881 idk = CP_ID_KIND_NONE;
3882 /* Enter the scope corresponding to the type of the object
3883 given by the POSTFIX_EXPRESSION. */
3885 && TREE_TYPE (postfix_expression) != NULL_TREE)
3887 scope = TREE_TYPE (postfix_expression);
3888 /* According to the standard, no expression should
3889 ever have reference type. Unfortunately, we do not
3890 currently match the standard in this respect in
3891 that our internal representation of an expression
3892 may have reference type even when the standard says
3893 it does not. Therefore, we have to manually obtain
3894 the underlying type here. */
3895 scope = non_reference (scope);
3896 /* The type of the POSTFIX_EXPRESSION must be
3898 scope = complete_type_or_else (scope, NULL_TREE);
3899 /* Let the name lookup machinery know that we are
3900 processing a class member access expression. */
3901 parser->context->object_type = scope;
3902 /* If something went wrong, we want to be able to
3903 discern that case, as opposed to the case where
3904 there was no SCOPE due to the type of expression
3907 scope = error_mark_node;
3908 /* If the SCOPE was erroneous, make the various
3909 semantic analysis functions exit quickly -- and
3910 without issuing additional error messages. */
3911 if (scope == error_mark_node)
3912 postfix_expression = error_mark_node;
3915 /* Consume the `.' or `->' operator. */
3916 cp_lexer_consume_token (parser->lexer);
3918 /* Assume this expression is not a pseudo-destructor access. */
3919 pseudo_destructor_p = false;
3921 /* If the SCOPE is a scalar type, then, if this is a valid program,
3922 we must be looking at a pseudo-destructor-name. */
3923 if (scope && SCALAR_TYPE_P (scope))
3928 cp_parser_parse_tentatively (parser);
3929 /* Parse the pseudo-destructor-name. */
3930 cp_parser_pseudo_destructor_name (parser, &s, &type);
3931 if (cp_parser_parse_definitely (parser))
3933 pseudo_destructor_p = true;
3935 = finish_pseudo_destructor_expr (postfix_expression,
3936 s, TREE_TYPE (type));
3940 if (!pseudo_destructor_p)
3942 /* If the SCOPE is not a scalar type, we are looking
3943 at an ordinary class member access expression,
3944 rather than a pseudo-destructor-name. */
3945 template_p = cp_parser_optional_template_keyword (parser);
3946 /* Parse the id-expression. */
3947 name = cp_parser_id_expression (parser,
3949 /*check_dependency_p=*/true,
3950 /*template_p=*/NULL,
3951 /*declarator_p=*/false);
3952 /* In general, build a SCOPE_REF if the member name is
3953 qualified. However, if the name was not dependent
3954 and has already been resolved; there is no need to
3955 build the SCOPE_REF. For example;
3957 struct X { void f(); };
3958 template <typename T> void f(T* t) { t->X::f(); }
3960 Even though "t" is dependent, "X::f" is not and has
3961 been resolved to a BASELINK; there is no need to
3962 include scope information. */
3964 /* But we do need to remember that there was an explicit
3965 scope for virtual function calls. */
3967 idk = CP_ID_KIND_QUALIFIED;
3969 if (name != error_mark_node
3970 && !BASELINK_P (name)
3973 name = build_nt (SCOPE_REF, parser->scope, name);
3974 parser->scope = NULL_TREE;
3975 parser->qualifying_scope = NULL_TREE;
3976 parser->object_scope = NULL_TREE;
3978 if (scope && name && BASELINK_P (name))
3979 adjust_result_of_qualified_name_lookup
3980 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3982 = finish_class_member_access_expr (postfix_expression, name);
3985 /* We no longer need to look up names in the scope of the
3986 object on the left-hand side of the `.' or `->'
3988 parser->context->object_type = NULL_TREE;
3989 /* These operators may not appear in constant-expressions. */
3990 if (/* The "->" operator is allowed in the implementation
3991 of "offsetof". The "." operator may appear in the
3992 name of the member. */
3993 !parser->in_offsetof_p
3994 && (cp_parser_non_integral_constant_expression
3996 token_type == CPP_DEREF ? "'->'" : "`.'")))
3997 postfix_expression = error_mark_node;
4002 /* postfix-expression ++ */
4003 /* Consume the `++' token. */
4004 cp_lexer_consume_token (parser->lexer);
4005 /* Generate a representation for the complete expression. */
4007 = finish_increment_expr (postfix_expression,
4008 POSTINCREMENT_EXPR);
4009 /* Increments may not appear in constant-expressions. */
4010 if (cp_parser_non_integral_constant_expression (parser,
4012 postfix_expression = error_mark_node;
4013 idk = CP_ID_KIND_NONE;
4016 case CPP_MINUS_MINUS:
4017 /* postfix-expression -- */
4018 /* Consume the `--' token. */
4019 cp_lexer_consume_token (parser->lexer);
4020 /* Generate a representation for the complete expression. */
4022 = finish_increment_expr (postfix_expression,
4023 POSTDECREMENT_EXPR);
4024 /* Decrements may not appear in constant-expressions. */
4025 if (cp_parser_non_integral_constant_expression (parser,
4027 postfix_expression = error_mark_node;
4028 idk = CP_ID_KIND_NONE;
4032 return postfix_expression;
4036 /* We should never get here. */
4038 return error_mark_node;
4041 /* Parse a parenthesized expression-list.
4044 assignment-expression
4045 expression-list, assignment-expression
4050 identifier, expression-list
4052 Returns a TREE_LIST. The TREE_VALUE of each node is a
4053 representation of an assignment-expression. Note that a TREE_LIST
4054 is returned even if there is only a single expression in the list.
4055 error_mark_node is returned if the ( and or ) are
4056 missing. NULL_TREE is returned on no expressions. The parentheses
4057 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4058 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4059 indicates whether or not all of the expressions in the list were
4063 cp_parser_parenthesized_expression_list (cp_parser* parser,
4064 bool is_attribute_list,
4065 bool *non_constant_p)
4067 tree expression_list = NULL_TREE;
4068 tree identifier = NULL_TREE;
4070 /* Assume all the expressions will be constant. */
4072 *non_constant_p = false;
4074 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4075 return error_mark_node;
4077 /* Consume expressions until there are no more. */
4078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4083 /* At the beginning of attribute lists, check to see if the
4084 next token is an identifier. */
4085 if (is_attribute_list
4086 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4090 /* Consume the identifier. */
4091 token = cp_lexer_consume_token (parser->lexer);
4092 /* Save the identifier. */
4093 identifier = token->value;
4097 /* Parse the next assignment-expression. */
4100 bool expr_non_constant_p;
4101 expr = (cp_parser_constant_expression
4102 (parser, /*allow_non_constant_p=*/true,
4103 &expr_non_constant_p));
4104 if (expr_non_constant_p)
4105 *non_constant_p = true;
4108 expr = cp_parser_assignment_expression (parser);
4110 /* Add it to the list. We add error_mark_node
4111 expressions to the list, so that we can still tell if
4112 the correct form for a parenthesized expression-list
4113 is found. That gives better errors. */
4114 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4116 if (expr == error_mark_node)
4120 /* After the first item, attribute lists look the same as
4121 expression lists. */
4122 is_attribute_list = false;
4125 /* If the next token isn't a `,', then we are done. */
4126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4129 /* Otherwise, consume the `,' and keep going. */
4130 cp_lexer_consume_token (parser->lexer);
4133 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4138 /* We try and resync to an unnested comma, as that will give the
4139 user better diagnostics. */
4140 ending = cp_parser_skip_to_closing_parenthesis (parser,
4141 /*recovering=*/true,
4143 /*consume_paren=*/true);
4147 return error_mark_node;
4150 /* We built up the list in reverse order so we must reverse it now. */
4151 expression_list = nreverse (expression_list);
4153 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4155 return expression_list;
4158 /* Parse a pseudo-destructor-name.
4160 pseudo-destructor-name:
4161 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4162 :: [opt] nested-name-specifier template template-id :: ~ type-name
4163 :: [opt] nested-name-specifier [opt] ~ type-name
4165 If either of the first two productions is used, sets *SCOPE to the
4166 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4167 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4168 or ERROR_MARK_NODE if the parse fails. */
4171 cp_parser_pseudo_destructor_name (cp_parser* parser,
4175 bool nested_name_specifier_p;
4177 /* Look for the optional `::' operator. */
4178 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4179 /* Look for the optional nested-name-specifier. */
4180 nested_name_specifier_p
4181 = (cp_parser_nested_name_specifier_opt (parser,
4182 /*typename_keyword_p=*/false,
4183 /*check_dependency_p=*/true,
4185 /*is_declaration=*/true)
4187 /* Now, if we saw a nested-name-specifier, we might be doing the
4188 second production. */
4189 if (nested_name_specifier_p
4190 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4192 /* Consume the `template' keyword. */
4193 cp_lexer_consume_token (parser->lexer);
4194 /* Parse the template-id. */
4195 cp_parser_template_id (parser,
4196 /*template_keyword_p=*/true,
4197 /*check_dependency_p=*/false,
4198 /*is_declaration=*/true);
4199 /* Look for the `::' token. */
4200 cp_parser_require (parser, CPP_SCOPE, "`::'");
4202 /* If the next token is not a `~', then there might be some
4203 additional qualification. */
4204 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4206 /* Look for the type-name. */
4207 *scope = TREE_TYPE (cp_parser_type_name (parser));
4209 /* If we didn't get an aggregate type, or we don't have ::~,
4210 then something has gone wrong. Since the only caller of this
4211 function is looking for something after `.' or `->' after a
4212 scalar type, most likely the program is trying to get a
4213 member of a non-aggregate type. */
4214 if (*scope == error_mark_node
4215 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4216 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4218 cp_parser_error (parser, "request for member of non-aggregate type");
4219 *type = error_mark_node;
4223 /* Look for the `::' token. */
4224 cp_parser_require (parser, CPP_SCOPE, "`::'");
4229 /* Look for the `~'. */
4230 cp_parser_require (parser, CPP_COMPL, "`~'");
4231 /* Look for the type-name again. We are not responsible for
4232 checking that it matches the first type-name. */
4233 *type = cp_parser_type_name (parser);
4236 /* Parse a unary-expression.
4242 unary-operator cast-expression
4243 sizeof unary-expression
4251 __extension__ cast-expression
4252 __alignof__ unary-expression
4253 __alignof__ ( type-id )
4254 __real__ cast-expression
4255 __imag__ cast-expression
4258 ADDRESS_P is true iff the unary-expression is appearing as the
4259 operand of the `&' operator.
4261 Returns a representation of the expression. */
4264 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4267 enum tree_code unary_operator;
4269 /* Peek at the next token. */
4270 token = cp_lexer_peek_token (parser->lexer);
4271 /* Some keywords give away the kind of expression. */
4272 if (token->type == CPP_KEYWORD)
4274 enum rid keyword = token->keyword;
4284 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4285 /* Consume the token. */
4286 cp_lexer_consume_token (parser->lexer);
4287 /* Parse the operand. */
4288 operand = cp_parser_sizeof_operand (parser, keyword);
4290 if (TYPE_P (operand))
4291 return cxx_sizeof_or_alignof_type (operand, op, true);
4293 return cxx_sizeof_or_alignof_expr (operand, op);
4297 return cp_parser_new_expression (parser);
4300 return cp_parser_delete_expression (parser);
4304 /* The saved value of the PEDANTIC flag. */
4308 /* Save away the PEDANTIC flag. */
4309 cp_parser_extension_opt (parser, &saved_pedantic);
4310 /* Parse the cast-expression. */
4311 expr = cp_parser_simple_cast_expression (parser);
4312 /* Restore the PEDANTIC flag. */
4313 pedantic = saved_pedantic;
4323 /* Consume the `__real__' or `__imag__' token. */
4324 cp_lexer_consume_token (parser->lexer);
4325 /* Parse the cast-expression. */
4326 expression = cp_parser_simple_cast_expression (parser);
4327 /* Create the complete representation. */
4328 return build_x_unary_op ((keyword == RID_REALPART
4329 ? REALPART_EXPR : IMAGPART_EXPR),
4339 /* Look for the `:: new' and `:: delete', which also signal the
4340 beginning of a new-expression, or delete-expression,
4341 respectively. If the next token is `::', then it might be one of
4343 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4347 /* See if the token after the `::' is one of the keywords in
4348 which we're interested. */
4349 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4350 /* If it's `new', we have a new-expression. */
4351 if (keyword == RID_NEW)
4352 return cp_parser_new_expression (parser);
4353 /* Similarly, for `delete'. */
4354 else if (keyword == RID_DELETE)
4355 return cp_parser_delete_expression (parser);
4358 /* Look for a unary operator. */
4359 unary_operator = cp_parser_unary_operator (token);
4360 /* The `++' and `--' operators can be handled similarly, even though
4361 they are not technically unary-operators in the grammar. */
4362 if (unary_operator == ERROR_MARK)
4364 if (token->type == CPP_PLUS_PLUS)
4365 unary_operator = PREINCREMENT_EXPR;
4366 else if (token->type == CPP_MINUS_MINUS)
4367 unary_operator = PREDECREMENT_EXPR;
4368 /* Handle the GNU address-of-label extension. */
4369 else if (cp_parser_allow_gnu_extensions_p (parser)
4370 && token->type == CPP_AND_AND)
4374 /* Consume the '&&' token. */
4375 cp_lexer_consume_token (parser->lexer);
4376 /* Look for the identifier. */
4377 identifier = cp_parser_identifier (parser);
4378 /* Create an expression representing the address. */
4379 return finish_label_address_expr (identifier);
4382 if (unary_operator != ERROR_MARK)
4384 tree cast_expression;
4385 tree expression = error_mark_node;
4386 const char *non_constant_p = NULL;
4388 /* Consume the operator token. */
4389 token = cp_lexer_consume_token (parser->lexer);
4390 /* Parse the cast-expression. */
4392 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4393 /* Now, build an appropriate representation. */
4394 switch (unary_operator)
4397 non_constant_p = "`*'";
4398 expression = build_x_indirect_ref (cast_expression, "unary *");
4402 /* The "&" operator is allowed in the implementation of
4404 if (!parser->in_offsetof_p)
4405 non_constant_p = "`&'";
4408 expression = build_x_unary_op (unary_operator, cast_expression);
4411 case PREINCREMENT_EXPR:
4412 case PREDECREMENT_EXPR:
4413 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4418 case TRUTH_NOT_EXPR:
4419 expression = finish_unary_op_expr (unary_operator, cast_expression);
4427 && cp_parser_non_integral_constant_expression (parser,
4429 expression = error_mark_node;
4434 return cp_parser_postfix_expression (parser, address_p);
4437 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4438 unary-operator, the corresponding tree code is returned. */
4440 static enum tree_code
4441 cp_parser_unary_operator (cp_token* token)
4443 switch (token->type)
4446 return INDIRECT_REF;
4452 return CONVERT_EXPR;
4458 return TRUTH_NOT_EXPR;
4461 return BIT_NOT_EXPR;
4468 /* Parse a new-expression.
4471 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4472 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4474 Returns a representation of the expression. */
4477 cp_parser_new_expression (cp_parser* parser)
4479 bool global_scope_p;
4484 /* Look for the optional `::' operator. */
4486 = (cp_parser_global_scope_opt (parser,
4487 /*current_scope_valid_p=*/false)
4489 /* Look for the `new' operator. */
4490 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4491 /* There's no easy way to tell a new-placement from the
4492 `( type-id )' construct. */
4493 cp_parser_parse_tentatively (parser);
4494 /* Look for a new-placement. */
4495 placement = cp_parser_new_placement (parser);
4496 /* If that didn't work out, there's no new-placement. */
4497 if (!cp_parser_parse_definitely (parser))
4498 placement = NULL_TREE;
4500 /* If the next token is a `(', then we have a parenthesized
4502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4504 /* Consume the `('. */
4505 cp_lexer_consume_token (parser->lexer);
4506 /* Parse the type-id. */
4507 type = cp_parser_type_id (parser);
4508 /* Look for the closing `)'. */
4509 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4510 /* There should not be a direct-new-declarator in this production,
4511 but GCC used to allowed this, so we check and emit a sensible error
4512 message for this case. */
4513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4515 error ("array bound forbidden after parenthesized type-id");
4516 inform ("try removing the parentheses around the type-id");
4517 cp_parser_direct_new_declarator (parser);
4520 /* Otherwise, there must be a new-type-id. */
4522 type = cp_parser_new_type_id (parser);
4524 /* If the next token is a `(', then we have a new-initializer. */
4525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4526 initializer = cp_parser_new_initializer (parser);
4528 initializer = NULL_TREE;
4530 /* A new-expression may not appear in an integral constant
4532 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4533 return error_mark_node;
4535 /* Create a representation of the new-expression. */
4536 return build_new (placement, type, initializer, global_scope_p);
4539 /* Parse a new-placement.
4544 Returns the same representation as for an expression-list. */
4547 cp_parser_new_placement (cp_parser* parser)
4549 tree expression_list;
4551 /* Parse the expression-list. */
4552 expression_list = (cp_parser_parenthesized_expression_list
4553 (parser, false, /*non_constant_p=*/NULL));
4555 return expression_list;
4558 /* Parse a new-type-id.
4561 type-specifier-seq new-declarator [opt]
4563 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4564 and whose TREE_VALUE is the new-declarator. */
4567 cp_parser_new_type_id (cp_parser* parser)
4569 tree type_specifier_seq;
4571 const char *saved_message;
4573 /* The type-specifier sequence must not contain type definitions.
4574 (It cannot contain declarations of new types either, but if they
4575 are not definitions we will catch that because they are not
4577 saved_message = parser->type_definition_forbidden_message;
4578 parser->type_definition_forbidden_message
4579 = "types may not be defined in a new-type-id";
4580 /* Parse the type-specifier-seq. */
4581 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4582 /* Restore the old message. */
4583 parser->type_definition_forbidden_message = saved_message;
4584 /* Parse the new-declarator. */
4585 declarator = cp_parser_new_declarator_opt (parser);
4587 return build_tree_list (type_specifier_seq, declarator);
4590 /* Parse an (optional) new-declarator.
4593 ptr-operator new-declarator [opt]
4594 direct-new-declarator
4596 Returns a representation of the declarator. See
4597 cp_parser_declarator for the representations used. */
4600 cp_parser_new_declarator_opt (cp_parser* parser)
4602 enum tree_code code;
4604 tree cv_qualifier_seq;
4606 /* We don't know if there's a ptr-operator next, or not. */
4607 cp_parser_parse_tentatively (parser);
4608 /* Look for a ptr-operator. */
4609 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4610 /* If that worked, look for more new-declarators. */
4611 if (cp_parser_parse_definitely (parser))
4615 /* Parse another optional declarator. */
4616 declarator = cp_parser_new_declarator_opt (parser);
4618 /* Create the representation of the declarator. */
4619 if (code == INDIRECT_REF)
4620 declarator = make_pointer_declarator (cv_qualifier_seq,
4623 declarator = make_reference_declarator (cv_qualifier_seq,
4626 /* Handle the pointer-to-member case. */
4628 declarator = build_nt (SCOPE_REF, type, declarator);
4633 /* If the next token is a `[', there is a direct-new-declarator. */
4634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4635 return cp_parser_direct_new_declarator (parser);
4640 /* Parse a direct-new-declarator.
4642 direct-new-declarator:
4644 direct-new-declarator [constant-expression]
4646 Returns an ARRAY_REF, following the same conventions as are
4647 documented for cp_parser_direct_declarator. */
4650 cp_parser_direct_new_declarator (cp_parser* parser)
4652 tree declarator = NULL_TREE;
4658 /* Look for the opening `['. */
4659 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4660 /* The first expression is not required to be constant. */
4663 expression = cp_parser_expression (parser);
4664 /* The standard requires that the expression have integral
4665 type. DR 74 adds enumeration types. We believe that the
4666 real intent is that these expressions be handled like the
4667 expression in a `switch' condition, which also allows
4668 classes with a single conversion to integral or
4669 enumeration type. */
4670 if (!processing_template_decl)
4673 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4678 error ("expression in new-declarator must have integral or enumeration type");
4679 expression = error_mark_node;
4683 /* But all the other expressions must be. */
4686 = cp_parser_constant_expression (parser,
4687 /*allow_non_constant=*/false,
4689 /* Look for the closing `]'. */
4690 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4692 /* Add this bound to the declarator. */
4693 declarator = build_nt (ARRAY_REF, declarator, expression);
4695 /* If the next token is not a `[', then there are no more
4697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4704 /* Parse a new-initializer.
4707 ( expression-list [opt] )
4709 Returns a representation of the expression-list. If there is no
4710 expression-list, VOID_ZERO_NODE is returned. */
4713 cp_parser_new_initializer (cp_parser* parser)
4715 tree expression_list;
4717 expression_list = (cp_parser_parenthesized_expression_list
4718 (parser, false, /*non_constant_p=*/NULL));
4719 if (!expression_list)
4720 expression_list = void_zero_node;
4722 return expression_list;
4725 /* Parse a delete-expression.
4728 :: [opt] delete cast-expression
4729 :: [opt] delete [ ] cast-expression
4731 Returns a representation of the expression. */
4734 cp_parser_delete_expression (cp_parser* parser)
4736 bool global_scope_p;
4740 /* Look for the optional `::' operator. */
4742 = (cp_parser_global_scope_opt (parser,
4743 /*current_scope_valid_p=*/false)
4745 /* Look for the `delete' keyword. */
4746 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4747 /* See if the array syntax is in use. */
4748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4750 /* Consume the `[' token. */
4751 cp_lexer_consume_token (parser->lexer);
4752 /* Look for the `]' token. */
4753 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4754 /* Remember that this is the `[]' construct. */
4760 /* Parse the cast-expression. */
4761 expression = cp_parser_simple_cast_expression (parser);
4763 /* A delete-expression may not appear in an integral constant
4765 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4766 return error_mark_node;
4768 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4771 /* Parse a cast-expression.
4775 ( type-id ) cast-expression
4777 Returns a representation of the expression. */
4780 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4782 /* If it's a `(', then we might be looking at a cast. */
4783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4785 tree type = NULL_TREE;
4786 tree expr = NULL_TREE;
4787 bool compound_literal_p;
4788 const char *saved_message;
4790 /* There's no way to know yet whether or not this is a cast.
4791 For example, `(int (3))' is a unary-expression, while `(int)
4792 3' is a cast. So, we resort to parsing tentatively. */
4793 cp_parser_parse_tentatively (parser);
4794 /* Types may not be defined in a cast. */
4795 saved_message = parser->type_definition_forbidden_message;
4796 parser->type_definition_forbidden_message
4797 = "types may not be defined in casts";
4798 /* Consume the `('. */
4799 cp_lexer_consume_token (parser->lexer);
4800 /* A very tricky bit is that `(struct S) { 3 }' is a
4801 compound-literal (which we permit in C++ as an extension).
4802 But, that construct is not a cast-expression -- it is a
4803 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4804 is legal; if the compound-literal were a cast-expression,
4805 you'd need an extra set of parentheses.) But, if we parse
4806 the type-id, and it happens to be a class-specifier, then we
4807 will commit to the parse at that point, because we cannot
4808 undo the action that is done when creating a new class. So,
4809 then we cannot back up and do a postfix-expression.
4811 Therefore, we scan ahead to the closing `)', and check to see
4812 if the token after the `)' is a `{'. If so, we are not
4813 looking at a cast-expression.
4815 Save tokens so that we can put them back. */
4816 cp_lexer_save_tokens (parser->lexer);
4817 /* Skip tokens until the next token is a closing parenthesis.
4818 If we find the closing `)', and the next token is a `{', then
4819 we are looking at a compound-literal. */
4821 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4822 /*consume_paren=*/true)
4823 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4824 /* Roll back the tokens we skipped. */
4825 cp_lexer_rollback_tokens (parser->lexer);
4826 /* If we were looking at a compound-literal, simulate an error
4827 so that the call to cp_parser_parse_definitely below will
4829 if (compound_literal_p)
4830 cp_parser_simulate_error (parser);
4833 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4834 parser->in_type_id_in_expr_p = true;
4835 /* Look for the type-id. */
4836 type = cp_parser_type_id (parser);
4837 /* Look for the closing `)'. */
4838 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4839 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4842 /* Restore the saved message. */
4843 parser->type_definition_forbidden_message = saved_message;
4845 /* If ok so far, parse the dependent expression. We cannot be
4846 sure it is a cast. Consider `(T ())'. It is a parenthesized
4847 ctor of T, but looks like a cast to function returning T
4848 without a dependent expression. */
4849 if (!cp_parser_error_occurred (parser))
4850 expr = cp_parser_simple_cast_expression (parser);
4852 if (cp_parser_parse_definitely (parser))
4854 /* Warn about old-style casts, if so requested. */
4855 if (warn_old_style_cast
4856 && !in_system_header
4857 && !VOID_TYPE_P (type)
4858 && current_lang_name != lang_name_c)
4859 warning ("use of old-style cast");
4861 /* Only type conversions to integral or enumeration types
4862 can be used in constant-expressions. */
4863 if (parser->integral_constant_expression_p
4864 && !dependent_type_p (type)
4865 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4866 && (cp_parser_non_integral_constant_expression
4868 "a casts to a type other than an integral or "
4869 "enumeration type")))
4870 return error_mark_node;
4872 /* Perform the cast. */
4873 expr = build_c_cast (type, expr);
4878 /* If we get here, then it's not a cast, so it must be a
4879 unary-expression. */
4880 return cp_parser_unary_expression (parser, address_p);
4883 /* Parse a pm-expression.
4887 pm-expression .* cast-expression
4888 pm-expression ->* cast-expression
4890 Returns a representation of the expression. */
4893 cp_parser_pm_expression (cp_parser* parser)
4895 static const cp_parser_token_tree_map map = {
4896 { CPP_DEREF_STAR, MEMBER_REF },
4897 { CPP_DOT_STAR, DOTSTAR_EXPR },
4898 { CPP_EOF, ERROR_MARK }
4901 return cp_parser_binary_expression (parser, map,
4902 cp_parser_simple_cast_expression);
4905 /* Parse a multiplicative-expression.
4907 mulitplicative-expression:
4909 multiplicative-expression * pm-expression
4910 multiplicative-expression / pm-expression
4911 multiplicative-expression % pm-expression
4913 Returns a representation of the expression. */
4916 cp_parser_multiplicative_expression (cp_parser* parser)
4918 static const cp_parser_token_tree_map map = {
4919 { CPP_MULT, MULT_EXPR },
4920 { CPP_DIV, TRUNC_DIV_EXPR },
4921 { CPP_MOD, TRUNC_MOD_EXPR },
4922 { CPP_EOF, ERROR_MARK }
4925 return cp_parser_binary_expression (parser,
4927 cp_parser_pm_expression);
4930 /* Parse an additive-expression.
4932 additive-expression:
4933 multiplicative-expression
4934 additive-expression + multiplicative-expression
4935 additive-expression - multiplicative-expression
4937 Returns a representation of the expression. */
4940 cp_parser_additive_expression (cp_parser* parser)
4942 static const cp_parser_token_tree_map map = {
4943 { CPP_PLUS, PLUS_EXPR },
4944 { CPP_MINUS, MINUS_EXPR },
4945 { CPP_EOF, ERROR_MARK }
4948 return cp_parser_binary_expression (parser,
4950 cp_parser_multiplicative_expression);
4953 /* Parse a shift-expression.
4957 shift-expression << additive-expression
4958 shift-expression >> additive-expression
4960 Returns a representation of the expression. */
4963 cp_parser_shift_expression (cp_parser* parser)
4965 static const cp_parser_token_tree_map map = {
4966 { CPP_LSHIFT, LSHIFT_EXPR },
4967 { CPP_RSHIFT, RSHIFT_EXPR },
4968 { CPP_EOF, ERROR_MARK }
4971 return cp_parser_binary_expression (parser,
4973 cp_parser_additive_expression);
4976 /* Parse a relational-expression.
4978 relational-expression:
4980 relational-expression < shift-expression
4981 relational-expression > shift-expression
4982 relational-expression <= shift-expression
4983 relational-expression >= shift-expression
4987 relational-expression:
4988 relational-expression <? shift-expression
4989 relational-expression >? shift-expression
4991 Returns a representation of the expression. */
4994 cp_parser_relational_expression (cp_parser* parser)
4996 static const cp_parser_token_tree_map map = {
4997 { CPP_LESS, LT_EXPR },
4998 { CPP_GREATER, GT_EXPR },
4999 { CPP_LESS_EQ, LE_EXPR },
5000 { CPP_GREATER_EQ, GE_EXPR },
5001 { CPP_MIN, MIN_EXPR },
5002 { CPP_MAX, MAX_EXPR },
5003 { CPP_EOF, ERROR_MARK }
5006 return cp_parser_binary_expression (parser,
5008 cp_parser_shift_expression);
5011 /* Parse an equality-expression.
5013 equality-expression:
5014 relational-expression
5015 equality-expression == relational-expression
5016 equality-expression != relational-expression
5018 Returns a representation of the expression. */
5021 cp_parser_equality_expression (cp_parser* parser)
5023 static const cp_parser_token_tree_map map = {
5024 { CPP_EQ_EQ, EQ_EXPR },
5025 { CPP_NOT_EQ, NE_EXPR },
5026 { CPP_EOF, ERROR_MARK }
5029 return cp_parser_binary_expression (parser,
5031 cp_parser_relational_expression);
5034 /* Parse an and-expression.
5038 and-expression & equality-expression
5040 Returns a representation of the expression. */
5043 cp_parser_and_expression (cp_parser* parser)
5045 static const cp_parser_token_tree_map map = {
5046 { CPP_AND, BIT_AND_EXPR },
5047 { CPP_EOF, ERROR_MARK }
5050 return cp_parser_binary_expression (parser,
5052 cp_parser_equality_expression);
5055 /* Parse an exclusive-or-expression.
5057 exclusive-or-expression:
5059 exclusive-or-expression ^ and-expression
5061 Returns a representation of the expression. */
5064 cp_parser_exclusive_or_expression (cp_parser* parser)
5066 static const cp_parser_token_tree_map map = {
5067 { CPP_XOR, BIT_XOR_EXPR },
5068 { CPP_EOF, ERROR_MARK }
5071 return cp_parser_binary_expression (parser,
5073 cp_parser_and_expression);
5077 /* Parse an inclusive-or-expression.
5079 inclusive-or-expression:
5080 exclusive-or-expression
5081 inclusive-or-expression | exclusive-or-expression
5083 Returns a representation of the expression. */
5086 cp_parser_inclusive_or_expression (cp_parser* parser)
5088 static const cp_parser_token_tree_map map = {
5089 { CPP_OR, BIT_IOR_EXPR },
5090 { CPP_EOF, ERROR_MARK }
5093 return cp_parser_binary_expression (parser,
5095 cp_parser_exclusive_or_expression);
5098 /* Parse a logical-and-expression.
5100 logical-and-expression:
5101 inclusive-or-expression
5102 logical-and-expression && inclusive-or-expression
5104 Returns a representation of the expression. */
5107 cp_parser_logical_and_expression (cp_parser* parser)
5109 static const cp_parser_token_tree_map map = {
5110 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5111 { CPP_EOF, ERROR_MARK }
5114 return cp_parser_binary_expression (parser,
5116 cp_parser_inclusive_or_expression);
5119 /* Parse a logical-or-expression.
5121 logical-or-expression:
5122 logical-and-expression
5123 logical-or-expression || logical-and-expression
5125 Returns a representation of the expression. */
5128 cp_parser_logical_or_expression (cp_parser* parser)
5130 static const cp_parser_token_tree_map map = {
5131 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5132 { CPP_EOF, ERROR_MARK }
5135 return cp_parser_binary_expression (parser,
5137 cp_parser_logical_and_expression);
5140 /* Parse the `? expression : assignment-expression' part of a
5141 conditional-expression. The LOGICAL_OR_EXPR is the
5142 logical-or-expression that started the conditional-expression.
5143 Returns a representation of the entire conditional-expression.
5145 This routine is used by cp_parser_assignment_expression.
5147 ? expression : assignment-expression
5151 ? : assignment-expression */
5154 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5157 tree assignment_expr;
5159 /* Consume the `?' token. */
5160 cp_lexer_consume_token (parser->lexer);
5161 if (cp_parser_allow_gnu_extensions_p (parser)
5162 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5163 /* Implicit true clause. */
5166 /* Parse the expression. */
5167 expr = cp_parser_expression (parser);
5169 /* The next token should be a `:'. */
5170 cp_parser_require (parser, CPP_COLON, "`:'");
5171 /* Parse the assignment-expression. */
5172 assignment_expr = cp_parser_assignment_expression (parser);
5174 /* Build the conditional-expression. */
5175 return build_x_conditional_expr (logical_or_expr,
5180 /* Parse an assignment-expression.
5182 assignment-expression:
5183 conditional-expression
5184 logical-or-expression assignment-operator assignment_expression
5187 Returns a representation for the expression. */
5190 cp_parser_assignment_expression (cp_parser* parser)
5194 /* If the next token is the `throw' keyword, then we're looking at
5195 a throw-expression. */
5196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5197 expr = cp_parser_throw_expression (parser);
5198 /* Otherwise, it must be that we are looking at a
5199 logical-or-expression. */
5202 /* Parse the logical-or-expression. */
5203 expr = cp_parser_logical_or_expression (parser);
5204 /* If the next token is a `?' then we're actually looking at a
5205 conditional-expression. */
5206 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5207 return cp_parser_question_colon_clause (parser, expr);
5210 enum tree_code assignment_operator;
5212 /* If it's an assignment-operator, we're using the second
5215 = cp_parser_assignment_operator_opt (parser);
5216 if (assignment_operator != ERROR_MARK)
5220 /* Parse the right-hand side of the assignment. */
5221 rhs = cp_parser_assignment_expression (parser);
5222 /* An assignment may not appear in a
5223 constant-expression. */
5224 if (cp_parser_non_integral_constant_expression (parser,
5226 return error_mark_node;
5227 /* Build the assignment expression. */
5228 expr = build_x_modify_expr (expr,
5229 assignment_operator,
5238 /* Parse an (optional) assignment-operator.
5240 assignment-operator: one of
5241 = *= /= %= += -= >>= <<= &= ^= |=
5245 assignment-operator: one of
5248 If the next token is an assignment operator, the corresponding tree
5249 code is returned, and the token is consumed. For example, for
5250 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5251 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5252 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5253 operator, ERROR_MARK is returned. */
5255 static enum tree_code
5256 cp_parser_assignment_operator_opt (cp_parser* parser)
5261 /* Peek at the next toen. */
5262 token = cp_lexer_peek_token (parser->lexer);
5264 switch (token->type)
5275 op = TRUNC_DIV_EXPR;
5279 op = TRUNC_MOD_EXPR;
5319 /* Nothing else is an assignment operator. */
5323 /* If it was an assignment operator, consume it. */
5324 if (op != ERROR_MARK)
5325 cp_lexer_consume_token (parser->lexer);
5330 /* Parse an expression.
5333 assignment-expression
5334 expression , assignment-expression
5336 Returns a representation of the expression. */
5339 cp_parser_expression (cp_parser* parser)
5341 tree expression = NULL_TREE;
5345 tree assignment_expression;
5347 /* Parse the next assignment-expression. */
5348 assignment_expression
5349 = cp_parser_assignment_expression (parser);
5350 /* If this is the first assignment-expression, we can just
5353 expression = assignment_expression;
5355 expression = build_x_compound_expr (expression,
5356 assignment_expression);
5357 /* If the next token is not a comma, then we are done with the
5359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5361 /* Consume the `,'. */
5362 cp_lexer_consume_token (parser->lexer);
5363 /* A comma operator cannot appear in a constant-expression. */
5364 if (cp_parser_non_integral_constant_expression (parser,
5365 "a comma operator"))
5366 expression = error_mark_node;
5372 /* Parse a constant-expression.
5374 constant-expression:
5375 conditional-expression
5377 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5378 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5379 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5380 is false, NON_CONSTANT_P should be NULL. */
5383 cp_parser_constant_expression (cp_parser* parser,
5384 bool allow_non_constant_p,
5385 bool *non_constant_p)
5387 bool saved_integral_constant_expression_p;
5388 bool saved_allow_non_integral_constant_expression_p;
5389 bool saved_non_integral_constant_expression_p;
5392 /* It might seem that we could simply parse the
5393 conditional-expression, and then check to see if it were
5394 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5395 one that the compiler can figure out is constant, possibly after
5396 doing some simplifications or optimizations. The standard has a
5397 precise definition of constant-expression, and we must honor
5398 that, even though it is somewhat more restrictive.
5404 is not a legal declaration, because `(2, 3)' is not a
5405 constant-expression. The `,' operator is forbidden in a
5406 constant-expression. However, GCC's constant-folding machinery
5407 will fold this operation to an INTEGER_CST for `3'. */
5409 /* Save the old settings. */
5410 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5411 saved_allow_non_integral_constant_expression_p
5412 = parser->allow_non_integral_constant_expression_p;
5413 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5414 /* We are now parsing a constant-expression. */
5415 parser->integral_constant_expression_p = true;
5416 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5417 parser->non_integral_constant_expression_p = false;
5418 /* Although the grammar says "conditional-expression", we parse an
5419 "assignment-expression", which also permits "throw-expression"
5420 and the use of assignment operators. In the case that
5421 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5422 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5423 actually essential that we look for an assignment-expression.
5424 For example, cp_parser_initializer_clauses uses this function to
5425 determine whether a particular assignment-expression is in fact
5427 expression = cp_parser_assignment_expression (parser);
5428 /* Restore the old settings. */
5429 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5430 parser->allow_non_integral_constant_expression_p
5431 = saved_allow_non_integral_constant_expression_p;
5432 if (allow_non_constant_p)
5433 *non_constant_p = parser->non_integral_constant_expression_p;
5434 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5439 /* Statements [gram.stmt.stmt] */
5441 /* Parse a statement.
5445 expression-statement
5450 declaration-statement
5454 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5458 int statement_line_number;
5460 /* There is no statement yet. */
5461 statement = NULL_TREE;
5462 /* Peek at the next token. */
5463 token = cp_lexer_peek_token (parser->lexer);
5464 /* Remember the line number of the first token in the statement. */
5465 statement_line_number = token->location.line;
5466 /* If this is a keyword, then that will often determine what kind of
5467 statement we have. */
5468 if (token->type == CPP_KEYWORD)
5470 enum rid keyword = token->keyword;
5476 statement = cp_parser_labeled_statement (parser,
5477 in_statement_expr_p);
5482 statement = cp_parser_selection_statement (parser);
5488 statement = cp_parser_iteration_statement (parser);
5495 statement = cp_parser_jump_statement (parser);
5499 statement = cp_parser_try_block (parser);
5503 /* It might be a keyword like `int' that can start a
5504 declaration-statement. */
5508 else if (token->type == CPP_NAME)
5510 /* If the next token is a `:', then we are looking at a
5511 labeled-statement. */
5512 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5513 if (token->type == CPP_COLON)
5514 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5516 /* Anything that starts with a `{' must be a compound-statement. */
5517 else if (token->type == CPP_OPEN_BRACE)
5518 statement = cp_parser_compound_statement (parser, false);
5520 /* Everything else must be a declaration-statement or an
5521 expression-statement. Try for the declaration-statement
5522 first, unless we are looking at a `;', in which case we know that
5523 we have an expression-statement. */
5526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5528 cp_parser_parse_tentatively (parser);
5529 /* Try to parse the declaration-statement. */
5530 cp_parser_declaration_statement (parser);
5531 /* If that worked, we're done. */
5532 if (cp_parser_parse_definitely (parser))
5535 /* Look for an expression-statement instead. */
5536 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5539 /* Set the line number for the statement. */
5540 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5541 STMT_LINENO (statement) = statement_line_number;
5544 /* Parse a labeled-statement.
5547 identifier : statement
5548 case constant-expression : statement
5554 case constant-expression ... constant-expression : statement
5556 Returns the new CASE_LABEL, for a `case' or `default' label. For
5557 an ordinary label, returns a LABEL_STMT. */
5560 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5563 tree statement = error_mark_node;
5565 /* The next token should be an identifier. */
5566 token = cp_lexer_peek_token (parser->lexer);
5567 if (token->type != CPP_NAME
5568 && token->type != CPP_KEYWORD)
5570 cp_parser_error (parser, "expected labeled-statement");
5571 return error_mark_node;
5574 switch (token->keyword)
5581 /* Consume the `case' token. */
5582 cp_lexer_consume_token (parser->lexer);
5583 /* Parse the constant-expression. */
5584 expr = cp_parser_constant_expression (parser,
5585 /*allow_non_constant_p=*/false,
5588 ellipsis = cp_lexer_peek_token (parser->lexer);
5589 if (ellipsis->type == CPP_ELLIPSIS)
5591 /* Consume the `...' token. */
5592 cp_lexer_consume_token (parser->lexer);
5594 cp_parser_constant_expression (parser,
5595 /*allow_non_constant_p=*/false,
5597 /* We don't need to emit warnings here, as the common code
5598 will do this for us. */
5601 expr_hi = NULL_TREE;
5603 if (!parser->in_switch_statement_p)
5604 error ("case label `%E' not within a switch statement", expr);
5606 statement = finish_case_label (expr, expr_hi);
5611 /* Consume the `default' token. */
5612 cp_lexer_consume_token (parser->lexer);
5613 if (!parser->in_switch_statement_p)
5614 error ("case label not within a switch statement");
5616 statement = finish_case_label (NULL_TREE, NULL_TREE);
5620 /* Anything else must be an ordinary label. */
5621 statement = finish_label_stmt (cp_parser_identifier (parser));
5625 /* Require the `:' token. */
5626 cp_parser_require (parser, CPP_COLON, "`:'");
5627 /* Parse the labeled statement. */
5628 cp_parser_statement (parser, in_statement_expr_p);
5630 /* Return the label, in the case of a `case' or `default' label. */
5634 /* Parse an expression-statement.
5636 expression-statement:
5639 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5640 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5641 indicates whether this expression-statement is part of an
5642 expression statement. */
5645 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5647 tree statement = NULL_TREE;
5649 /* If the next token is a ';', then there is no expression
5651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5652 statement = cp_parser_expression (parser);
5654 /* Consume the final `;'. */
5655 cp_parser_consume_semicolon_at_end_of_statement (parser);
5657 if (in_statement_expr_p
5658 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5660 /* This is the final expression statement of a statement
5662 statement = finish_stmt_expr_expr (statement);
5665 statement = finish_expr_stmt (statement);
5672 /* Parse a compound-statement.
5675 { statement-seq [opt] }
5677 Returns a COMPOUND_STMT representing the statement. */
5680 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5684 /* Consume the `{'. */
5685 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5686 return error_mark_node;
5687 /* Begin the compound-statement. */
5688 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5689 /* Parse an (optional) statement-seq. */
5690 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5691 /* Finish the compound-statement. */
5692 finish_compound_stmt (compound_stmt);
5693 /* Consume the `}'. */
5694 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5696 return compound_stmt;
5699 /* Parse an (optional) statement-seq.
5703 statement-seq [opt] statement */
5706 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5708 /* Scan statements until there aren't any more. */
5711 /* If we're looking at a `}', then we've run out of statements. */
5712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5713 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5716 /* Parse the statement. */
5717 cp_parser_statement (parser, in_statement_expr_p);
5721 /* Parse a selection-statement.
5723 selection-statement:
5724 if ( condition ) statement
5725 if ( condition ) statement else statement
5726 switch ( condition ) statement
5728 Returns the new IF_STMT or SWITCH_STMT. */
5731 cp_parser_selection_statement (cp_parser* parser)
5736 /* Peek at the next token. */
5737 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5739 /* See what kind of keyword it is. */
5740 keyword = token->keyword;
5749 /* Look for the `('. */
5750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5752 cp_parser_skip_to_end_of_statement (parser);
5753 return error_mark_node;
5756 /* Begin the selection-statement. */
5757 if (keyword == RID_IF)
5758 statement = begin_if_stmt ();
5760 statement = begin_switch_stmt ();
5762 /* Parse the condition. */
5763 condition = cp_parser_condition (parser);
5764 /* Look for the `)'. */
5765 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5766 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5767 /*consume_paren=*/true);
5769 if (keyword == RID_IF)
5773 /* Add the condition. */
5774 finish_if_stmt_cond (condition, statement);
5776 /* Parse the then-clause. */
5777 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5778 finish_then_clause (statement);
5780 /* If the next token is `else', parse the else-clause. */
5781 if (cp_lexer_next_token_is_keyword (parser->lexer,
5786 /* Consume the `else' keyword. */
5787 cp_lexer_consume_token (parser->lexer);
5788 /* Parse the else-clause. */
5790 = cp_parser_implicitly_scoped_statement (parser);
5791 finish_else_clause (statement);
5794 /* Now we're all done with the if-statement. */
5800 bool in_switch_statement_p;
5802 /* Add the condition. */
5803 finish_switch_cond (condition, statement);
5805 /* Parse the body of the switch-statement. */
5806 in_switch_statement_p = parser->in_switch_statement_p;
5807 parser->in_switch_statement_p = true;
5808 body = cp_parser_implicitly_scoped_statement (parser);
5809 parser->in_switch_statement_p = in_switch_statement_p;
5811 /* Now we're all done with the switch-statement. */
5812 finish_switch_stmt (statement);
5820 cp_parser_error (parser, "expected selection-statement");
5821 return error_mark_node;
5825 /* Parse a condition.
5829 type-specifier-seq declarator = assignment-expression
5834 type-specifier-seq declarator asm-specification [opt]
5835 attributes [opt] = assignment-expression
5837 Returns the expression that should be tested. */
5840 cp_parser_condition (cp_parser* parser)
5842 tree type_specifiers;
5843 const char *saved_message;
5845 /* Try the declaration first. */
5846 cp_parser_parse_tentatively (parser);
5847 /* New types are not allowed in the type-specifier-seq for a
5849 saved_message = parser->type_definition_forbidden_message;
5850 parser->type_definition_forbidden_message
5851 = "types may not be defined in conditions";
5852 /* Parse the type-specifier-seq. */
5853 type_specifiers = cp_parser_type_specifier_seq (parser);
5854 /* Restore the saved message. */
5855 parser->type_definition_forbidden_message = saved_message;
5856 /* If all is well, we might be looking at a declaration. */
5857 if (!cp_parser_error_occurred (parser))
5860 tree asm_specification;
5863 tree initializer = NULL_TREE;
5865 /* Parse the declarator. */
5866 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5867 /*ctor_dtor_or_conv_p=*/NULL,
5868 /*parenthesized_p=*/NULL,
5869 /*member_p=*/false);
5870 /* Parse the attributes. */
5871 attributes = cp_parser_attributes_opt (parser);
5872 /* Parse the asm-specification. */
5873 asm_specification = cp_parser_asm_specification_opt (parser);
5874 /* If the next token is not an `=', then we might still be
5875 looking at an expression. For example:
5879 looks like a decl-specifier-seq and a declarator -- but then
5880 there is no `=', so this is an expression. */
5881 cp_parser_require (parser, CPP_EQ, "`='");
5882 /* If we did see an `=', then we are looking at a declaration
5884 if (cp_parser_parse_definitely (parser))
5886 /* Create the declaration. */
5887 decl = start_decl (declarator, type_specifiers,
5888 /*initialized_p=*/true,
5889 attributes, /*prefix_attributes=*/NULL_TREE);
5890 /* Parse the assignment-expression. */
5891 initializer = cp_parser_assignment_expression (parser);
5893 /* Process the initializer. */
5894 cp_finish_decl (decl,
5897 LOOKUP_ONLYCONVERTING);
5899 return convert_from_reference (decl);
5902 /* If we didn't even get past the declarator successfully, we are
5903 definitely not looking at a declaration. */
5905 cp_parser_abort_tentative_parse (parser);
5907 /* Otherwise, we are looking at an expression. */
5908 return cp_parser_expression (parser);
5911 /* Parse an iteration-statement.
5913 iteration-statement:
5914 while ( condition ) statement
5915 do statement while ( expression ) ;
5916 for ( for-init-statement condition [opt] ; expression [opt] )
5919 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5922 cp_parser_iteration_statement (cp_parser* parser)
5927 bool in_iteration_statement_p;
5930 /* Peek at the next token. */
5931 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5933 return error_mark_node;
5935 /* Remember whether or not we are already within an iteration
5937 in_iteration_statement_p = parser->in_iteration_statement_p;
5939 /* See what kind of keyword it is. */
5940 keyword = token->keyword;
5947 /* Begin the while-statement. */
5948 statement = begin_while_stmt ();
5949 /* Look for the `('. */
5950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5951 /* Parse the condition. */
5952 condition = cp_parser_condition (parser);
5953 finish_while_stmt_cond (condition, statement);
5954 /* Look for the `)'. */
5955 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5956 /* Parse the dependent statement. */
5957 parser->in_iteration_statement_p = true;
5958 cp_parser_already_scoped_statement (parser);
5959 parser->in_iteration_statement_p = in_iteration_statement_p;
5960 /* We're done with the while-statement. */
5961 finish_while_stmt (statement);
5969 /* Begin the do-statement. */
5970 statement = begin_do_stmt ();
5971 /* Parse the body of the do-statement. */
5972 parser->in_iteration_statement_p = true;
5973 cp_parser_implicitly_scoped_statement (parser);
5974 parser->in_iteration_statement_p = in_iteration_statement_p;
5975 finish_do_body (statement);
5976 /* Look for the `while' keyword. */
5977 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5978 /* Look for the `('. */
5979 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5980 /* Parse the expression. */
5981 expression = cp_parser_expression (parser);
5982 /* We're done with the do-statement. */
5983 finish_do_stmt (expression, statement);
5984 /* Look for the `)'. */
5985 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5986 /* Look for the `;'. */
5987 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5993 tree condition = NULL_TREE;
5994 tree expression = NULL_TREE;
5996 /* Begin the for-statement. */
5997 statement = begin_for_stmt ();
5998 /* Look for the `('. */
5999 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6000 /* Parse the initialization. */
6001 cp_parser_for_init_statement (parser);
6002 finish_for_init_stmt (statement);
6004 /* If there's a condition, process it. */
6005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6006 condition = cp_parser_condition (parser);
6007 finish_for_cond (condition, statement);
6008 /* Look for the `;'. */
6009 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6011 /* If there's an expression, process it. */
6012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6013 expression = cp_parser_expression (parser);
6014 finish_for_expr (expression, statement);
6015 /* Look for the `)'. */
6016 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6018 /* Parse the body of the for-statement. */
6019 parser->in_iteration_statement_p = true;
6020 cp_parser_already_scoped_statement (parser);
6021 parser->in_iteration_statement_p = in_iteration_statement_p;
6023 /* We're done with the for-statement. */
6024 finish_for_stmt (statement);
6029 cp_parser_error (parser, "expected iteration-statement");
6030 statement = error_mark_node;
6037 /* Parse a for-init-statement.
6040 expression-statement
6041 simple-declaration */
6044 cp_parser_for_init_statement (cp_parser* parser)
6046 /* If the next token is a `;', then we have an empty
6047 expression-statement. Grammatically, this is also a
6048 simple-declaration, but an invalid one, because it does not
6049 declare anything. Therefore, if we did not handle this case
6050 specially, we would issue an error message about an invalid
6052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6054 /* We're going to speculatively look for a declaration, falling back
6055 to an expression, if necessary. */
6056 cp_parser_parse_tentatively (parser);
6057 /* Parse the declaration. */
6058 cp_parser_simple_declaration (parser,
6059 /*function_definition_allowed_p=*/false);
6060 /* If the tentative parse failed, then we shall need to look for an
6061 expression-statement. */
6062 if (cp_parser_parse_definitely (parser))
6066 cp_parser_expression_statement (parser, false);
6069 /* Parse a jump-statement.
6074 return expression [opt] ;
6082 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6086 cp_parser_jump_statement (cp_parser* parser)
6088 tree statement = error_mark_node;
6092 /* Peek at the next token. */
6093 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6095 return error_mark_node;
6097 /* See what kind of keyword it is. */
6098 keyword = token->keyword;
6102 if (!parser->in_switch_statement_p
6103 && !parser->in_iteration_statement_p)
6105 error ("break statement not within loop or switch");
6106 statement = error_mark_node;
6109 statement = finish_break_stmt ();
6110 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6114 if (!parser->in_iteration_statement_p)
6116 error ("continue statement not within a loop");
6117 statement = error_mark_node;
6120 statement = finish_continue_stmt ();
6121 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6128 /* If the next token is a `;', then there is no
6130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6131 expr = cp_parser_expression (parser);
6134 /* Build the return-statement. */
6135 statement = finish_return_stmt (expr);
6136 /* Look for the final `;'. */
6137 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6142 /* Create the goto-statement. */
6143 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6145 /* Issue a warning about this use of a GNU extension. */
6147 pedwarn ("ISO C++ forbids computed gotos");
6148 /* Consume the '*' token. */
6149 cp_lexer_consume_token (parser->lexer);
6150 /* Parse the dependent expression. */
6151 finish_goto_stmt (cp_parser_expression (parser));
6154 finish_goto_stmt (cp_parser_identifier (parser));
6155 /* Look for the final `;'. */
6156 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6160 cp_parser_error (parser, "expected jump-statement");
6167 /* Parse a declaration-statement.
6169 declaration-statement:
6170 block-declaration */
6173 cp_parser_declaration_statement (cp_parser* parser)
6175 /* Parse the block-declaration. */
6176 cp_parser_block_declaration (parser, /*statement_p=*/true);
6178 /* Finish off the statement. */
6182 /* Some dependent statements (like `if (cond) statement'), are
6183 implicitly in their own scope. In other words, if the statement is
6184 a single statement (as opposed to a compound-statement), it is
6185 none-the-less treated as if it were enclosed in braces. Any
6186 declarations appearing in the dependent statement are out of scope
6187 after control passes that point. This function parses a statement,
6188 but ensures that is in its own scope, even if it is not a
6191 Returns the new statement. */
6194 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6198 /* If the token is not a `{', then we must take special action. */
6199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6201 /* Create a compound-statement. */
6202 statement = begin_compound_stmt (/*has_no_scope=*/false);
6203 /* Parse the dependent-statement. */
6204 cp_parser_statement (parser, false);
6205 /* Finish the dummy compound-statement. */
6206 finish_compound_stmt (statement);
6208 /* Otherwise, we simply parse the statement directly. */
6210 statement = cp_parser_compound_statement (parser, false);
6212 /* Return the statement. */
6216 /* For some dependent statements (like `while (cond) statement'), we
6217 have already created a scope. Therefore, even if the dependent
6218 statement is a compound-statement, we do not want to create another
6222 cp_parser_already_scoped_statement (cp_parser* parser)
6224 /* If the token is not a `{', then we must take special action. */
6225 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6229 /* Create a compound-statement. */
6230 statement = begin_compound_stmt (/*has_no_scope=*/true);
6231 /* Parse the dependent-statement. */
6232 cp_parser_statement (parser, false);
6233 /* Finish the dummy compound-statement. */
6234 finish_compound_stmt (statement);
6236 /* Otherwise, we simply parse the statement directly. */
6238 cp_parser_statement (parser, false);
6241 /* Declarations [gram.dcl.dcl] */
6243 /* Parse an optional declaration-sequence.
6247 declaration-seq declaration */
6250 cp_parser_declaration_seq_opt (cp_parser* parser)
6256 token = cp_lexer_peek_token (parser->lexer);
6258 if (token->type == CPP_CLOSE_BRACE
6259 || token->type == CPP_EOF)
6262 if (token->type == CPP_SEMICOLON)
6264 /* A declaration consisting of a single semicolon is
6265 invalid. Allow it unless we're being pedantic. */
6266 if (pedantic && !in_system_header)
6267 pedwarn ("extra `;'");
6268 cp_lexer_consume_token (parser->lexer);
6272 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6273 parser to enter or exit implicit `extern "C"' blocks. */
6274 while (pending_lang_change > 0)
6276 push_lang_context (lang_name_c);
6277 --pending_lang_change;
6279 while (pending_lang_change < 0)
6281 pop_lang_context ();
6282 ++pending_lang_change;
6285 /* Parse the declaration itself. */
6286 cp_parser_declaration (parser);
6290 /* Parse a declaration.
6295 template-declaration
6296 explicit-instantiation
6297 explicit-specialization
6298 linkage-specification
6299 namespace-definition
6304 __extension__ declaration */
6307 cp_parser_declaration (cp_parser* parser)
6313 /* Check for the `__extension__' keyword. */
6314 if (cp_parser_extension_opt (parser, &saved_pedantic))
6316 /* Parse the qualified declaration. */
6317 cp_parser_declaration (parser);
6318 /* Restore the PEDANTIC flag. */
6319 pedantic = saved_pedantic;
6324 /* Try to figure out what kind of declaration is present. */
6325 token1 = *cp_lexer_peek_token (parser->lexer);
6326 if (token1.type != CPP_EOF)
6327 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6329 /* If the next token is `extern' and the following token is a string
6330 literal, then we have a linkage specification. */
6331 if (token1.keyword == RID_EXTERN
6332 && cp_parser_is_string_literal (&token2))
6333 cp_parser_linkage_specification (parser);
6334 /* If the next token is `template', then we have either a template
6335 declaration, an explicit instantiation, or an explicit
6337 else if (token1.keyword == RID_TEMPLATE)
6339 /* `template <>' indicates a template specialization. */
6340 if (token2.type == CPP_LESS
6341 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6342 cp_parser_explicit_specialization (parser);
6343 /* `template <' indicates a template declaration. */
6344 else if (token2.type == CPP_LESS)
6345 cp_parser_template_declaration (parser, /*member_p=*/false);
6346 /* Anything else must be an explicit instantiation. */
6348 cp_parser_explicit_instantiation (parser);
6350 /* If the next token is `export', then we have a template
6352 else if (token1.keyword == RID_EXPORT)
6353 cp_parser_template_declaration (parser, /*member_p=*/false);
6354 /* If the next token is `extern', 'static' or 'inline' and the one
6355 after that is `template', we have a GNU extended explicit
6356 instantiation directive. */
6357 else if (cp_parser_allow_gnu_extensions_p (parser)
6358 && (token1.keyword == RID_EXTERN
6359 || token1.keyword == RID_STATIC
6360 || token1.keyword == RID_INLINE)
6361 && token2.keyword == RID_TEMPLATE)
6362 cp_parser_explicit_instantiation (parser);
6363 /* If the next token is `namespace', check for a named or unnamed
6364 namespace definition. */
6365 else if (token1.keyword == RID_NAMESPACE
6366 && (/* A named namespace definition. */
6367 (token2.type == CPP_NAME
6368 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6370 /* An unnamed namespace definition. */
6371 || token2.type == CPP_OPEN_BRACE))
6372 cp_parser_namespace_definition (parser);
6373 /* We must have either a block declaration or a function
6376 /* Try to parse a block-declaration, or a function-definition. */
6377 cp_parser_block_declaration (parser, /*statement_p=*/false);
6380 /* Parse a block-declaration.
6385 namespace-alias-definition
6392 __extension__ block-declaration
6395 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6396 part of a declaration-statement. */
6399 cp_parser_block_declaration (cp_parser *parser,
6405 /* Check for the `__extension__' keyword. */
6406 if (cp_parser_extension_opt (parser, &saved_pedantic))
6408 /* Parse the qualified declaration. */
6409 cp_parser_block_declaration (parser, statement_p);
6410 /* Restore the PEDANTIC flag. */
6411 pedantic = saved_pedantic;
6416 /* Peek at the next token to figure out which kind of declaration is
6418 token1 = cp_lexer_peek_token (parser->lexer);
6420 /* If the next keyword is `asm', we have an asm-definition. */
6421 if (token1->keyword == RID_ASM)
6424 cp_parser_commit_to_tentative_parse (parser);
6425 cp_parser_asm_definition (parser);
6427 /* If the next keyword is `namespace', we have a
6428 namespace-alias-definition. */
6429 else if (token1->keyword == RID_NAMESPACE)
6430 cp_parser_namespace_alias_definition (parser);
6431 /* If the next keyword is `using', we have either a
6432 using-declaration or a using-directive. */
6433 else if (token1->keyword == RID_USING)
6438 cp_parser_commit_to_tentative_parse (parser);
6439 /* If the token after `using' is `namespace', then we have a
6441 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6442 if (token2->keyword == RID_NAMESPACE)
6443 cp_parser_using_directive (parser);
6444 /* Otherwise, it's a using-declaration. */
6446 cp_parser_using_declaration (parser);
6448 /* If the next keyword is `__label__' we have a label declaration. */
6449 else if (token1->keyword == RID_LABEL)
6452 cp_parser_commit_to_tentative_parse (parser);
6453 cp_parser_label_declaration (parser);
6455 /* Anything else must be a simple-declaration. */
6457 cp_parser_simple_declaration (parser, !statement_p);
6460 /* Parse a simple-declaration.
6463 decl-specifier-seq [opt] init-declarator-list [opt] ;
6465 init-declarator-list:
6467 init-declarator-list , init-declarator
6469 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6470 function-definition as a simple-declaration. */
6473 cp_parser_simple_declaration (cp_parser* parser,
6474 bool function_definition_allowed_p)
6476 tree decl_specifiers;
6478 int declares_class_or_enum;
6479 bool saw_declarator;
6481 /* Defer access checks until we know what is being declared; the
6482 checks for names appearing in the decl-specifier-seq should be
6483 done as if we were in the scope of the thing being declared. */
6484 push_deferring_access_checks (dk_deferred);
6486 /* Parse the decl-specifier-seq. We have to keep track of whether
6487 or not the decl-specifier-seq declares a named class or
6488 enumeration type, since that is the only case in which the
6489 init-declarator-list is allowed to be empty.
6493 In a simple-declaration, the optional init-declarator-list can be
6494 omitted only when declaring a class or enumeration, that is when
6495 the decl-specifier-seq contains either a class-specifier, an
6496 elaborated-type-specifier, or an enum-specifier. */
6498 = cp_parser_decl_specifier_seq (parser,
6499 CP_PARSER_FLAGS_OPTIONAL,
6501 &declares_class_or_enum);
6502 /* We no longer need to defer access checks. */
6503 stop_deferring_access_checks ();
6505 /* In a block scope, a valid declaration must always have a
6506 decl-specifier-seq. By not trying to parse declarators, we can
6507 resolve the declaration/expression ambiguity more quickly. */
6508 if (!function_definition_allowed_p && !decl_specifiers)
6510 cp_parser_error (parser, "expected declaration");
6514 /* If the next two tokens are both identifiers, the code is
6515 erroneous. The usual cause of this situation is code like:
6519 where "T" should name a type -- but does not. */
6520 if (cp_parser_diagnose_invalid_type_name (parser))
6522 /* If parsing tentatively, we should commit; we really are
6523 looking at a declaration. */
6524 cp_parser_commit_to_tentative_parse (parser);
6529 /* If we have seen at least one decl-specifier, and the next token
6530 is not a parenthesis, then we must be looking at a declaration.
6531 (After "int (" we might be looking at a functional cast.) */
6533 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6534 cp_parser_commit_to_tentative_parse (parser);
6536 /* Keep going until we hit the `;' at the end of the simple
6538 saw_declarator = false;
6539 while (cp_lexer_next_token_is_not (parser->lexer,
6543 bool function_definition_p;
6546 saw_declarator = true;
6547 /* Parse the init-declarator. */
6548 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6549 function_definition_allowed_p,
6551 declares_class_or_enum,
6552 &function_definition_p);
6553 /* If an error occurred while parsing tentatively, exit quickly.
6554 (That usually happens when in the body of a function; each
6555 statement is treated as a declaration-statement until proven
6557 if (cp_parser_error_occurred (parser))
6559 /* Handle function definitions specially. */
6560 if (function_definition_p)
6562 /* If the next token is a `,', then we are probably
6563 processing something like:
6567 which is erroneous. */
6568 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6569 error ("mixing declarations and function-definitions is forbidden");
6570 /* Otherwise, we're done with the list of declarators. */
6573 pop_deferring_access_checks ();
6577 /* The next token should be either a `,' or a `;'. */
6578 token = cp_lexer_peek_token (parser->lexer);
6579 /* If it's a `,', there are more declarators to come. */
6580 if (token->type == CPP_COMMA)
6581 cp_lexer_consume_token (parser->lexer);
6582 /* If it's a `;', we are done. */
6583 else if (token->type == CPP_SEMICOLON)
6585 /* Anything else is an error. */
6588 /* If we have already issued an error message we don't need
6589 to issue another one. */
6590 if (decl != error_mark_node
6591 || (cp_parser_parsing_tentatively (parser)
6592 && !cp_parser_committed_to_tentative_parse (parser)))
6593 cp_parser_error (parser, "expected `,' or `;'");
6594 /* Skip tokens until we reach the end of the statement. */
6595 cp_parser_skip_to_end_of_statement (parser);
6596 /* If the next token is now a `;', consume it. */
6597 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6598 cp_lexer_consume_token (parser->lexer);
6601 /* After the first time around, a function-definition is not
6602 allowed -- even if it was OK at first. For example:
6607 function_definition_allowed_p = false;
6610 /* Issue an error message if no declarators are present, and the
6611 decl-specifier-seq does not itself declare a class or
6613 if (!saw_declarator)
6615 if (cp_parser_declares_only_class_p (parser))
6616 shadow_tag (decl_specifiers);
6617 /* Perform any deferred access checks. */
6618 perform_deferred_access_checks ();
6621 /* Consume the `;'. */
6622 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6625 pop_deferring_access_checks ();
6628 /* Parse a decl-specifier-seq.
6631 decl-specifier-seq [opt] decl-specifier
6634 storage-class-specifier
6645 Returns a TREE_LIST, giving the decl-specifiers in the order they
6646 appear in the source code. The TREE_VALUE of each node is the
6647 decl-specifier. For a keyword (such as `auto' or `friend'), the
6648 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6649 representation of a type-specifier, see cp_parser_type_specifier.
6651 If there are attributes, they will be stored in *ATTRIBUTES,
6652 represented as described above cp_parser_attributes.
6654 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6655 appears, and the entity that will be a friend is not going to be a
6656 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6657 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6658 friendship is granted might not be a class.
6660 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6663 1: one of the decl-specifiers is an elaborated-type-specifier
6664 (i.e., a type declaration)
6665 2: one of the decl-specifiers is an enum-specifier or a
6666 class-specifier (i.e., a type definition)
6671 cp_parser_decl_specifier_seq (cp_parser* parser,
6672 cp_parser_flags flags,
6674 int* declares_class_or_enum)
6676 tree decl_specs = NULL_TREE;
6677 bool friend_p = false;
6678 bool constructor_possible_p = !parser->in_declarator_p;
6680 /* Assume no class or enumeration type is declared. */
6681 *declares_class_or_enum = 0;
6683 /* Assume there are no attributes. */
6684 *attributes = NULL_TREE;
6686 /* Keep reading specifiers until there are no more to read. */
6689 tree decl_spec = NULL_TREE;
6693 /* Peek at the next token. */
6694 token = cp_lexer_peek_token (parser->lexer);
6695 /* Handle attributes. */
6696 if (token->keyword == RID_ATTRIBUTE)
6698 /* Parse the attributes. */
6699 decl_spec = cp_parser_attributes_opt (parser);
6700 /* Add them to the list. */
6701 *attributes = chainon (*attributes, decl_spec);
6704 /* If the next token is an appropriate keyword, we can simply
6705 add it to the list. */
6706 switch (token->keyword)
6712 error ("duplicate `friend'");
6715 /* The representation of the specifier is simply the
6716 appropriate TREE_IDENTIFIER node. */
6717 decl_spec = token->value;
6718 /* Consume the token. */
6719 cp_lexer_consume_token (parser->lexer);
6722 /* function-specifier:
6729 decl_spec = cp_parser_function_specifier_opt (parser);
6735 /* The representation of the specifier is simply the
6736 appropriate TREE_IDENTIFIER node. */
6737 decl_spec = token->value;
6738 /* Consume the token. */
6739 cp_lexer_consume_token (parser->lexer);
6740 /* A constructor declarator cannot appear in a typedef. */
6741 constructor_possible_p = false;
6742 /* The "typedef" keyword can only occur in a declaration; we
6743 may as well commit at this point. */
6744 cp_parser_commit_to_tentative_parse (parser);
6747 /* storage-class-specifier:
6762 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6769 /* Constructors are a special case. The `S' in `S()' is not a
6770 decl-specifier; it is the beginning of the declarator. */
6771 constructor_p = (!decl_spec
6772 && constructor_possible_p
6773 && cp_parser_constructor_declarator_p (parser,
6776 /* If we don't have a DECL_SPEC yet, then we must be looking at
6777 a type-specifier. */
6778 if (!decl_spec && !constructor_p)
6780 int decl_spec_declares_class_or_enum;
6781 bool is_cv_qualifier;
6784 = cp_parser_type_specifier (parser, flags,
6786 /*is_declaration=*/true,
6787 &decl_spec_declares_class_or_enum,
6790 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6792 /* If this type-specifier referenced a user-defined type
6793 (a typedef, class-name, etc.), then we can't allow any
6794 more such type-specifiers henceforth.
6798 The longest sequence of decl-specifiers that could
6799 possibly be a type name is taken as the
6800 decl-specifier-seq of a declaration. The sequence shall
6801 be self-consistent as described below.
6805 As a general rule, at most one type-specifier is allowed
6806 in the complete decl-specifier-seq of a declaration. The
6807 only exceptions are the following:
6809 -- const or volatile can be combined with any other
6812 -- signed or unsigned can be combined with char, long,
6820 void g (const int Pc);
6822 Here, Pc is *not* part of the decl-specifier seq; it's
6823 the declarator. Therefore, once we see a type-specifier
6824 (other than a cv-qualifier), we forbid any additional
6825 user-defined types. We *do* still allow things like `int
6826 int' to be considered a decl-specifier-seq, and issue the
6827 error message later. */
6828 if (decl_spec && !is_cv_qualifier)
6829 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6830 /* A constructor declarator cannot follow a type-specifier. */
6832 constructor_possible_p = false;
6835 /* If we still do not have a DECL_SPEC, then there are no more
6839 /* Issue an error message, unless the entire construct was
6841 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6843 cp_parser_error (parser, "expected decl specifier");
6844 return error_mark_node;
6850 /* Add the DECL_SPEC to the list of specifiers. */
6851 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6852 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6854 /* After we see one decl-specifier, further decl-specifiers are
6856 flags |= CP_PARSER_FLAGS_OPTIONAL;
6859 /* Don't allow a friend specifier with a class definition. */
6860 if (friend_p && (*declares_class_or_enum & 2))
6861 error ("class definition may not be declared a friend");
6863 /* We have built up the DECL_SPECS in reverse order. Return them in
6864 the correct order. */
6865 return nreverse (decl_specs);
6868 /* Parse an (optional) storage-class-specifier.
6870 storage-class-specifier:
6879 storage-class-specifier:
6882 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6885 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6887 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6895 /* Consume the token. */
6896 return cp_lexer_consume_token (parser->lexer)->value;
6903 /* Parse an (optional) function-specifier.
6910 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6913 cp_parser_function_specifier_opt (cp_parser* parser)
6915 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6920 /* Consume the token. */
6921 return cp_lexer_consume_token (parser->lexer)->value;
6928 /* Parse a linkage-specification.
6930 linkage-specification:
6931 extern string-literal { declaration-seq [opt] }
6932 extern string-literal declaration */
6935 cp_parser_linkage_specification (cp_parser* parser)
6940 /* Look for the `extern' keyword. */
6941 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6943 /* Peek at the next token. */
6944 token = cp_lexer_peek_token (parser->lexer);
6945 /* If it's not a string-literal, then there's a problem. */
6946 if (!cp_parser_is_string_literal (token))
6948 cp_parser_error (parser, "expected language-name");
6951 /* Consume the token. */
6952 cp_lexer_consume_token (parser->lexer);
6954 /* Transform the literal into an identifier. If the literal is a
6955 wide-character string, or contains embedded NULs, then we can't
6956 handle it as the user wants. */
6957 if (token->type == CPP_WSTRING
6958 || (strlen (TREE_STRING_POINTER (token->value))
6959 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6961 cp_parser_error (parser, "invalid linkage-specification");
6962 /* Assume C++ linkage. */
6963 linkage = get_identifier ("c++");
6965 /* If it's a simple string constant, things are easier. */
6967 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6969 /* We're now using the new linkage. */
6970 push_lang_context (linkage);
6972 /* If the next token is a `{', then we're using the first
6974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6976 /* Consume the `{' token. */
6977 cp_lexer_consume_token (parser->lexer);
6978 /* Parse the declarations. */
6979 cp_parser_declaration_seq_opt (parser);
6980 /* Look for the closing `}'. */
6981 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6983 /* Otherwise, there's just one declaration. */
6986 bool saved_in_unbraced_linkage_specification_p;
6988 saved_in_unbraced_linkage_specification_p
6989 = parser->in_unbraced_linkage_specification_p;
6990 parser->in_unbraced_linkage_specification_p = true;
6991 have_extern_spec = true;
6992 cp_parser_declaration (parser);
6993 have_extern_spec = false;
6994 parser->in_unbraced_linkage_specification_p
6995 = saved_in_unbraced_linkage_specification_p;
6998 /* We're done with the linkage-specification. */
6999 pop_lang_context ();
7002 /* Special member functions [gram.special] */
7004 /* Parse a conversion-function-id.
7006 conversion-function-id:
7007 operator conversion-type-id
7009 Returns an IDENTIFIER_NODE representing the operator. */
7012 cp_parser_conversion_function_id (cp_parser* parser)
7016 tree saved_qualifying_scope;
7017 tree saved_object_scope;
7020 /* Look for the `operator' token. */
7021 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7022 return error_mark_node;
7023 /* When we parse the conversion-type-id, the current scope will be
7024 reset. However, we need that information in able to look up the
7025 conversion function later, so we save it here. */
7026 saved_scope = parser->scope;
7027 saved_qualifying_scope = parser->qualifying_scope;
7028 saved_object_scope = parser->object_scope;
7029 /* We must enter the scope of the class so that the names of
7030 entities declared within the class are available in the
7031 conversion-type-id. For example, consider:
7038 S::operator I() { ... }
7040 In order to see that `I' is a type-name in the definition, we
7041 must be in the scope of `S'. */
7043 pop_p = push_scope (saved_scope);
7044 /* Parse the conversion-type-id. */
7045 type = cp_parser_conversion_type_id (parser);
7046 /* Leave the scope of the class, if any. */
7048 pop_scope (saved_scope);
7049 /* Restore the saved scope. */
7050 parser->scope = saved_scope;
7051 parser->qualifying_scope = saved_qualifying_scope;
7052 parser->object_scope = saved_object_scope;
7053 /* If the TYPE is invalid, indicate failure. */
7054 if (type == error_mark_node)
7055 return error_mark_node;
7056 return mangle_conv_op_name_for_type (type);
7059 /* Parse a conversion-type-id:
7062 type-specifier-seq conversion-declarator [opt]
7064 Returns the TYPE specified. */
7067 cp_parser_conversion_type_id (cp_parser* parser)
7070 tree type_specifiers;
7073 /* Parse the attributes. */
7074 attributes = cp_parser_attributes_opt (parser);
7075 /* Parse the type-specifiers. */
7076 type_specifiers = cp_parser_type_specifier_seq (parser);
7077 /* If that didn't work, stop. */
7078 if (type_specifiers == error_mark_node)
7079 return error_mark_node;
7080 /* Parse the conversion-declarator. */
7081 declarator = cp_parser_conversion_declarator_opt (parser);
7083 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7084 /*initialized=*/0, &attributes);
7087 /* Parse an (optional) conversion-declarator.
7089 conversion-declarator:
7090 ptr-operator conversion-declarator [opt]
7092 Returns a representation of the declarator. See
7093 cp_parser_declarator for details. */
7096 cp_parser_conversion_declarator_opt (cp_parser* parser)
7098 enum tree_code code;
7100 tree cv_qualifier_seq;
7102 /* We don't know if there's a ptr-operator next, or not. */
7103 cp_parser_parse_tentatively (parser);
7104 /* Try the ptr-operator. */
7105 code = cp_parser_ptr_operator (parser, &class_type,
7107 /* If it worked, look for more conversion-declarators. */
7108 if (cp_parser_parse_definitely (parser))
7112 /* Parse another optional declarator. */
7113 declarator = cp_parser_conversion_declarator_opt (parser);
7115 /* Create the representation of the declarator. */
7116 if (code == INDIRECT_REF)
7117 declarator = make_pointer_declarator (cv_qualifier_seq,
7120 declarator = make_reference_declarator (cv_qualifier_seq,
7123 /* Handle the pointer-to-member case. */
7125 declarator = build_nt (SCOPE_REF, class_type, declarator);
7133 /* Parse an (optional) ctor-initializer.
7136 : mem-initializer-list
7138 Returns TRUE iff the ctor-initializer was actually present. */
7141 cp_parser_ctor_initializer_opt (cp_parser* parser)
7143 /* If the next token is not a `:', then there is no
7144 ctor-initializer. */
7145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7147 /* Do default initialization of any bases and members. */
7148 if (DECL_CONSTRUCTOR_P (current_function_decl))
7149 finish_mem_initializers (NULL_TREE);
7154 /* Consume the `:' token. */
7155 cp_lexer_consume_token (parser->lexer);
7156 /* And the mem-initializer-list. */
7157 cp_parser_mem_initializer_list (parser);
7162 /* Parse a mem-initializer-list.
7164 mem-initializer-list:
7166 mem-initializer , mem-initializer-list */
7169 cp_parser_mem_initializer_list (cp_parser* parser)
7171 tree mem_initializer_list = NULL_TREE;
7173 /* Let the semantic analysis code know that we are starting the
7174 mem-initializer-list. */
7175 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7176 error ("only constructors take base initializers");
7178 /* Loop through the list. */
7181 tree mem_initializer;
7183 /* Parse the mem-initializer. */
7184 mem_initializer = cp_parser_mem_initializer (parser);
7185 /* Add it to the list, unless it was erroneous. */
7186 if (mem_initializer)
7188 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7189 mem_initializer_list = mem_initializer;
7191 /* If the next token is not a `,', we're done. */
7192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7194 /* Consume the `,' token. */
7195 cp_lexer_consume_token (parser->lexer);
7198 /* Perform semantic analysis. */
7199 if (DECL_CONSTRUCTOR_P (current_function_decl))
7200 finish_mem_initializers (mem_initializer_list);
7203 /* Parse a mem-initializer.
7206 mem-initializer-id ( expression-list [opt] )
7211 ( expression-list [opt] )
7213 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7214 class) or FIELD_DECL (for a non-static data member) to initialize;
7215 the TREE_VALUE is the expression-list. */
7218 cp_parser_mem_initializer (cp_parser* parser)
7220 tree mem_initializer_id;
7221 tree expression_list;
7224 /* Find out what is being initialized. */
7225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7227 pedwarn ("anachronistic old-style base class initializer");
7228 mem_initializer_id = NULL_TREE;
7231 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7232 member = expand_member_init (mem_initializer_id);
7233 if (member && !DECL_P (member))
7234 in_base_initializer = 1;
7237 = cp_parser_parenthesized_expression_list (parser, false,
7238 /*non_constant_p=*/NULL);
7239 if (!expression_list)
7240 expression_list = void_type_node;
7242 in_base_initializer = 0;
7244 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7247 /* Parse a mem-initializer-id.
7250 :: [opt] nested-name-specifier [opt] class-name
7253 Returns a TYPE indicating the class to be initializer for the first
7254 production. Returns an IDENTIFIER_NODE indicating the data member
7255 to be initialized for the second production. */
7258 cp_parser_mem_initializer_id (cp_parser* parser)
7260 bool global_scope_p;
7261 bool nested_name_specifier_p;
7262 bool template_p = false;
7265 /* `typename' is not allowed in this context ([temp.res]). */
7266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7268 error ("keyword `typename' not allowed in this context (a qualified "
7269 "member initializer is implicitly a type)");
7270 cp_lexer_consume_token (parser->lexer);
7272 /* Look for the optional `::' operator. */
7274 = (cp_parser_global_scope_opt (parser,
7275 /*current_scope_valid_p=*/false)
7277 /* Look for the optional nested-name-specifier. The simplest way to
7282 The keyword `typename' is not permitted in a base-specifier or
7283 mem-initializer; in these contexts a qualified name that
7284 depends on a template-parameter is implicitly assumed to be a
7287 is to assume that we have seen the `typename' keyword at this
7289 nested_name_specifier_p
7290 = (cp_parser_nested_name_specifier_opt (parser,
7291 /*typename_keyword_p=*/true,
7292 /*check_dependency_p=*/true,
7294 /*is_declaration=*/true)
7296 if (nested_name_specifier_p)
7297 template_p = cp_parser_optional_template_keyword (parser);
7298 /* If there is a `::' operator or a nested-name-specifier, then we
7299 are definitely looking for a class-name. */
7300 if (global_scope_p || nested_name_specifier_p)
7301 return cp_parser_class_name (parser,
7302 /*typename_keyword_p=*/true,
7303 /*template_keyword_p=*/template_p,
7305 /*check_dependency_p=*/true,
7306 /*class_head_p=*/false,
7307 /*is_declaration=*/true);
7308 /* Otherwise, we could also be looking for an ordinary identifier. */
7309 cp_parser_parse_tentatively (parser);
7310 /* Try a class-name. */
7311 id = cp_parser_class_name (parser,
7312 /*typename_keyword_p=*/true,
7313 /*template_keyword_p=*/false,
7315 /*check_dependency_p=*/true,
7316 /*class_head_p=*/false,
7317 /*is_declaration=*/true);
7318 /* If we found one, we're done. */
7319 if (cp_parser_parse_definitely (parser))
7321 /* Otherwise, look for an ordinary identifier. */
7322 return cp_parser_identifier (parser);
7325 /* Overloading [gram.over] */
7327 /* Parse an operator-function-id.
7329 operator-function-id:
7332 Returns an IDENTIFIER_NODE for the operator which is a
7333 human-readable spelling of the identifier, e.g., `operator +'. */
7336 cp_parser_operator_function_id (cp_parser* parser)
7338 /* Look for the `operator' keyword. */
7339 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7340 return error_mark_node;
7341 /* And then the name of the operator itself. */
7342 return cp_parser_operator (parser);
7345 /* Parse an operator.
7348 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7349 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7350 || ++ -- , ->* -> () []
7357 Returns an IDENTIFIER_NODE for the operator which is a
7358 human-readable spelling of the identifier, e.g., `operator +'. */
7361 cp_parser_operator (cp_parser* parser)
7363 tree id = NULL_TREE;
7366 /* Peek at the next token. */
7367 token = cp_lexer_peek_token (parser->lexer);
7368 /* Figure out which operator we have. */
7369 switch (token->type)
7375 /* The keyword should be either `new' or `delete'. */
7376 if (token->keyword == RID_NEW)
7378 else if (token->keyword == RID_DELETE)
7383 /* Consume the `new' or `delete' token. */
7384 cp_lexer_consume_token (parser->lexer);
7386 /* Peek at the next token. */
7387 token = cp_lexer_peek_token (parser->lexer);
7388 /* If it's a `[' token then this is the array variant of the
7390 if (token->type == CPP_OPEN_SQUARE)
7392 /* Consume the `[' token. */
7393 cp_lexer_consume_token (parser->lexer);
7394 /* Look for the `]' token. */
7395 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7396 id = ansi_opname (op == NEW_EXPR
7397 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7399 /* Otherwise, we have the non-array variant. */
7401 id = ansi_opname (op);
7407 id = ansi_opname (PLUS_EXPR);
7411 id = ansi_opname (MINUS_EXPR);
7415 id = ansi_opname (MULT_EXPR);
7419 id = ansi_opname (TRUNC_DIV_EXPR);
7423 id = ansi_opname (TRUNC_MOD_EXPR);
7427 id = ansi_opname (BIT_XOR_EXPR);
7431 id = ansi_opname (BIT_AND_EXPR);
7435 id = ansi_opname (BIT_IOR_EXPR);
7439 id = ansi_opname (BIT_NOT_EXPR);
7443 id = ansi_opname (TRUTH_NOT_EXPR);
7447 id = ansi_assopname (NOP_EXPR);
7451 id = ansi_opname (LT_EXPR);
7455 id = ansi_opname (GT_EXPR);
7459 id = ansi_assopname (PLUS_EXPR);
7463 id = ansi_assopname (MINUS_EXPR);
7467 id = ansi_assopname (MULT_EXPR);
7471 id = ansi_assopname (TRUNC_DIV_EXPR);
7475 id = ansi_assopname (TRUNC_MOD_EXPR);
7479 id = ansi_assopname (BIT_XOR_EXPR);
7483 id = ansi_assopname (BIT_AND_EXPR);
7487 id = ansi_assopname (BIT_IOR_EXPR);
7491 id = ansi_opname (LSHIFT_EXPR);
7495 id = ansi_opname (RSHIFT_EXPR);
7499 id = ansi_assopname (LSHIFT_EXPR);
7503 id = ansi_assopname (RSHIFT_EXPR);
7507 id = ansi_opname (EQ_EXPR);
7511 id = ansi_opname (NE_EXPR);
7515 id = ansi_opname (LE_EXPR);
7518 case CPP_GREATER_EQ:
7519 id = ansi_opname (GE_EXPR);
7523 id = ansi_opname (TRUTH_ANDIF_EXPR);
7527 id = ansi_opname (TRUTH_ORIF_EXPR);
7531 id = ansi_opname (POSTINCREMENT_EXPR);
7534 case CPP_MINUS_MINUS:
7535 id = ansi_opname (PREDECREMENT_EXPR);
7539 id = ansi_opname (COMPOUND_EXPR);
7542 case CPP_DEREF_STAR:
7543 id = ansi_opname (MEMBER_REF);
7547 id = ansi_opname (COMPONENT_REF);
7550 case CPP_OPEN_PAREN:
7551 /* Consume the `('. */
7552 cp_lexer_consume_token (parser->lexer);
7553 /* Look for the matching `)'. */
7554 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7555 return ansi_opname (CALL_EXPR);
7557 case CPP_OPEN_SQUARE:
7558 /* Consume the `['. */
7559 cp_lexer_consume_token (parser->lexer);
7560 /* Look for the matching `]'. */
7561 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7562 return ansi_opname (ARRAY_REF);
7566 id = ansi_opname (MIN_EXPR);
7570 id = ansi_opname (MAX_EXPR);
7574 id = ansi_assopname (MIN_EXPR);
7578 id = ansi_assopname (MAX_EXPR);
7582 /* Anything else is an error. */
7586 /* If we have selected an identifier, we need to consume the
7589 cp_lexer_consume_token (parser->lexer);
7590 /* Otherwise, no valid operator name was present. */
7593 cp_parser_error (parser, "expected operator");
7594 id = error_mark_node;
7600 /* Parse a template-declaration.
7602 template-declaration:
7603 export [opt] template < template-parameter-list > declaration
7605 If MEMBER_P is TRUE, this template-declaration occurs within a
7608 The grammar rule given by the standard isn't correct. What
7611 template-declaration:
7612 export [opt] template-parameter-list-seq
7613 decl-specifier-seq [opt] init-declarator [opt] ;
7614 export [opt] template-parameter-list-seq
7617 template-parameter-list-seq:
7618 template-parameter-list-seq [opt]
7619 template < template-parameter-list > */
7622 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7624 /* Check for `export'. */
7625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7627 /* Consume the `export' token. */
7628 cp_lexer_consume_token (parser->lexer);
7629 /* Warn that we do not support `export'. */
7630 warning ("keyword `export' not implemented, and will be ignored");
7633 cp_parser_template_declaration_after_export (parser, member_p);
7636 /* Parse a template-parameter-list.
7638 template-parameter-list:
7640 template-parameter-list , template-parameter
7642 Returns a TREE_LIST. Each node represents a template parameter.
7643 The nodes are connected via their TREE_CHAINs. */
7646 cp_parser_template_parameter_list (cp_parser* parser)
7648 tree parameter_list = NULL_TREE;
7655 /* Parse the template-parameter. */
7656 parameter = cp_parser_template_parameter (parser);
7657 /* Add it to the list. */
7658 parameter_list = process_template_parm (parameter_list,
7661 /* Peek at the next token. */
7662 token = cp_lexer_peek_token (parser->lexer);
7663 /* If it's not a `,', we're done. */
7664 if (token->type != CPP_COMMA)
7666 /* Otherwise, consume the `,' token. */
7667 cp_lexer_consume_token (parser->lexer);
7670 return parameter_list;
7673 /* Parse a template-parameter.
7677 parameter-declaration
7679 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7680 TREE_PURPOSE is the default value, if any. */
7683 cp_parser_template_parameter (cp_parser* parser)
7687 /* Peek at the next token. */
7688 token = cp_lexer_peek_token (parser->lexer);
7689 /* If it is `class' or `template', we have a type-parameter. */
7690 if (token->keyword == RID_TEMPLATE)
7691 return cp_parser_type_parameter (parser);
7692 /* If it is `class' or `typename' we do not know yet whether it is a
7693 type parameter or a non-type parameter. Consider:
7695 template <typename T, typename T::X X> ...
7699 template <class C, class D*> ...
7701 Here, the first parameter is a type parameter, and the second is
7702 a non-type parameter. We can tell by looking at the token after
7703 the identifier -- if it is a `,', `=', or `>' then we have a type
7705 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7707 /* Peek at the token after `class' or `typename'. */
7708 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7709 /* If it's an identifier, skip it. */
7710 if (token->type == CPP_NAME)
7711 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7712 /* Now, see if the token looks like the end of a template
7714 if (token->type == CPP_COMMA
7715 || token->type == CPP_EQ
7716 || token->type == CPP_GREATER)
7717 return cp_parser_type_parameter (parser);
7720 /* Otherwise, it is a non-type parameter.
7724 When parsing a default template-argument for a non-type
7725 template-parameter, the first non-nested `>' is taken as the end
7726 of the template parameter-list rather than a greater-than
7729 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7730 /*parenthesized_p=*/NULL);
7733 /* Parse a type-parameter.
7736 class identifier [opt]
7737 class identifier [opt] = type-id
7738 typename identifier [opt]
7739 typename identifier [opt] = type-id
7740 template < template-parameter-list > class identifier [opt]
7741 template < template-parameter-list > class identifier [opt]
7744 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7745 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7746 the declaration of the parameter. */
7749 cp_parser_type_parameter (cp_parser* parser)
7754 /* Look for a keyword to tell us what kind of parameter this is. */
7755 token = cp_parser_require (parser, CPP_KEYWORD,
7756 "`class', `typename', or `template'");
7758 return error_mark_node;
7760 switch (token->keyword)
7766 tree default_argument;
7768 /* If the next token is an identifier, then it names the
7770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7771 identifier = cp_parser_identifier (parser);
7773 identifier = NULL_TREE;
7775 /* Create the parameter. */
7776 parameter = finish_template_type_parm (class_type_node, identifier);
7778 /* If the next token is an `=', we have a default argument. */
7779 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7781 /* Consume the `=' token. */
7782 cp_lexer_consume_token (parser->lexer);
7783 /* Parse the default-argument. */
7784 default_argument = cp_parser_type_id (parser);
7787 default_argument = NULL_TREE;
7789 /* Create the combined representation of the parameter and the
7790 default argument. */
7791 parameter = build_tree_list (default_argument, parameter);
7797 tree parameter_list;
7799 tree default_argument;
7801 /* Look for the `<'. */
7802 cp_parser_require (parser, CPP_LESS, "`<'");
7803 /* Parse the template-parameter-list. */
7804 begin_template_parm_list ();
7806 = cp_parser_template_parameter_list (parser);
7807 parameter_list = end_template_parm_list (parameter_list);
7808 /* Look for the `>'. */
7809 cp_parser_require (parser, CPP_GREATER, "`>'");
7810 /* Look for the `class' keyword. */
7811 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7812 /* If the next token is an `=', then there is a
7813 default-argument. If the next token is a `>', we are at
7814 the end of the parameter-list. If the next token is a `,',
7815 then we are at the end of this parameter. */
7816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7817 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7818 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7820 identifier = cp_parser_identifier (parser);
7821 /* Treat invalid names as if the parameter were nameless. */
7822 if (identifier == error_mark_node)
7823 identifier = NULL_TREE;
7826 identifier = NULL_TREE;
7828 /* Create the template parameter. */
7829 parameter = finish_template_template_parm (class_type_node,
7832 /* If the next token is an `=', then there is a
7833 default-argument. */
7834 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7838 /* Consume the `='. */
7839 cp_lexer_consume_token (parser->lexer);
7840 /* Parse the id-expression. */
7842 = cp_parser_id_expression (parser,
7843 /*template_keyword_p=*/false,
7844 /*check_dependency_p=*/true,
7845 /*template_p=*/&is_template,
7846 /*declarator_p=*/false);
7847 if (TREE_CODE (default_argument) == TYPE_DECL)
7848 /* If the id-expression was a template-id that refers to
7849 a template-class, we already have the declaration here,
7850 so no further lookup is needed. */
7853 /* Look up the name. */
7855 = cp_parser_lookup_name (parser, default_argument,
7857 /*is_template=*/is_template,
7858 /*is_namespace=*/false,
7859 /*check_dependency=*/true);
7860 /* See if the default argument is valid. */
7862 = check_template_template_default_arg (default_argument);
7865 default_argument = NULL_TREE;
7867 /* Create the combined representation of the parameter and the
7868 default argument. */
7869 parameter = build_tree_list (default_argument, parameter);
7881 /* Parse a template-id.
7884 template-name < template-argument-list [opt] >
7886 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7887 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7888 returned. Otherwise, if the template-name names a function, or set
7889 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7890 names a class, returns a TYPE_DECL for the specialization.
7892 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7893 uninstantiated templates. */
7896 cp_parser_template_id (cp_parser *parser,
7897 bool template_keyword_p,
7898 bool check_dependency_p,
7899 bool is_declaration)
7904 ptrdiff_t start_of_id;
7905 tree access_check = NULL_TREE;
7906 cp_token *next_token, *next_token_2;
7909 /* If the next token corresponds to a template-id, there is no need
7911 next_token = cp_lexer_peek_token (parser->lexer);
7912 if (next_token->type == CPP_TEMPLATE_ID)
7917 /* Get the stored value. */
7918 value = cp_lexer_consume_token (parser->lexer)->value;
7919 /* Perform any access checks that were deferred. */
7920 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7921 perform_or_defer_access_check (TREE_PURPOSE (check),
7922 TREE_VALUE (check));
7923 /* Return the stored value. */
7924 return TREE_VALUE (value);
7927 /* Avoid performing name lookup if there is no possibility of
7928 finding a template-id. */
7929 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7930 || (next_token->type == CPP_NAME
7931 && !cp_parser_nth_token_starts_template_argument_list_p
7934 cp_parser_error (parser, "expected template-id");
7935 return error_mark_node;
7938 /* Remember where the template-id starts. */
7939 if (cp_parser_parsing_tentatively (parser)
7940 && !cp_parser_committed_to_tentative_parse (parser))
7942 next_token = cp_lexer_peek_token (parser->lexer);
7943 start_of_id = cp_lexer_token_difference (parser->lexer,
7944 parser->lexer->first_token,
7950 push_deferring_access_checks (dk_deferred);
7952 /* Parse the template-name. */
7953 is_identifier = false;
7954 template = cp_parser_template_name (parser, template_keyword_p,
7958 if (template == error_mark_node || is_identifier)
7960 pop_deferring_access_checks ();
7964 /* If we find the sequence `[:' after a template-name, it's probably
7965 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7966 parse correctly the argument list. */
7967 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7968 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7969 if (next_token->type == CPP_OPEN_SQUARE
7970 && next_token->flags & DIGRAPH
7971 && next_token_2->type == CPP_COLON
7972 && !(next_token_2->flags & PREV_WHITE))
7974 cp_parser_parse_tentatively (parser);
7975 /* Change `:' into `::'. */
7976 next_token_2->type = CPP_SCOPE;
7977 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7979 cp_lexer_consume_token (parser->lexer);
7980 /* Parse the arguments. */
7981 arguments = cp_parser_enclosed_template_argument_list (parser);
7982 if (!cp_parser_parse_definitely (parser))
7984 /* If we couldn't parse an argument list, then we revert our changes
7985 and return simply an error. Maybe this is not a template-id
7987 next_token_2->type = CPP_COLON;
7988 cp_parser_error (parser, "expected `<'");
7989 pop_deferring_access_checks ();
7990 return error_mark_node;
7992 /* Otherwise, emit an error about the invalid digraph, but continue
7993 parsing because we got our argument list. */
7994 pedwarn ("`<::' cannot begin a template-argument list");
7995 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7996 "between `<' and `::'");
7997 if (!flag_permissive)
8002 inform ("(if you use `-fpermissive' G++ will accept your code)");
8009 /* Look for the `<' that starts the template-argument-list. */
8010 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8012 pop_deferring_access_checks ();
8013 return error_mark_node;
8015 /* Parse the arguments. */
8016 arguments = cp_parser_enclosed_template_argument_list (parser);
8019 /* Build a representation of the specialization. */
8020 if (TREE_CODE (template) == IDENTIFIER_NODE)
8021 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8022 else if (DECL_CLASS_TEMPLATE_P (template)
8023 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8025 = finish_template_type (template, arguments,
8026 cp_lexer_next_token_is (parser->lexer,
8030 /* If it's not a class-template or a template-template, it should be
8031 a function-template. */
8032 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8033 || TREE_CODE (template) == OVERLOAD
8034 || BASELINK_P (template)),
8037 template_id = lookup_template_function (template, arguments);
8040 /* Retrieve any deferred checks. Do not pop this access checks yet
8041 so the memory will not be reclaimed during token replacing below. */
8042 access_check = get_deferred_access_checks ();
8044 /* If parsing tentatively, replace the sequence of tokens that makes
8045 up the template-id with a CPP_TEMPLATE_ID token. That way,
8046 should we re-parse the token stream, we will not have to repeat
8047 the effort required to do the parse, nor will we issue duplicate
8048 error messages about problems during instantiation of the
8050 if (start_of_id >= 0)
8054 /* Find the token that corresponds to the start of the
8056 token = cp_lexer_advance_token (parser->lexer,
8057 parser->lexer->first_token,
8060 /* Reset the contents of the START_OF_ID token. */
8061 token->type = CPP_TEMPLATE_ID;
8062 token->value = build_tree_list (access_check, template_id);
8063 token->keyword = RID_MAX;
8064 /* Purge all subsequent tokens. */
8065 cp_lexer_purge_tokens_after (parser->lexer, token);
8067 /* ??? Can we actually assume that, if template_id ==
8068 error_mark_node, we will have issued a diagnostic to the
8069 user, as opposed to simply marking the tentative parse as
8071 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8072 error ("parse error in template argument list");
8075 pop_deferring_access_checks ();
8079 /* Parse a template-name.
8084 The standard should actually say:
8088 operator-function-id
8090 A defect report has been filed about this issue.
8092 A conversion-function-id cannot be a template name because they cannot
8093 be part of a template-id. In fact, looking at this code:
8097 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8098 It is impossible to call a templated conversion-function-id with an
8099 explicit argument list, since the only allowed template parameter is
8100 the type to which it is converting.
8102 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8103 `template' keyword, in a construction like:
8107 In that case `f' is taken to be a template-name, even though there
8108 is no way of knowing for sure.
8110 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8111 name refers to a set of overloaded functions, at least one of which
8112 is a template, or an IDENTIFIER_NODE with the name of the template,
8113 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8114 names are looked up inside uninstantiated templates. */
8117 cp_parser_template_name (cp_parser* parser,
8118 bool template_keyword_p,
8119 bool check_dependency_p,
8120 bool is_declaration,
8121 bool *is_identifier)
8127 /* If the next token is `operator', then we have either an
8128 operator-function-id or a conversion-function-id. */
8129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8131 /* We don't know whether we're looking at an
8132 operator-function-id or a conversion-function-id. */
8133 cp_parser_parse_tentatively (parser);
8134 /* Try an operator-function-id. */
8135 identifier = cp_parser_operator_function_id (parser);
8136 /* If that didn't work, try a conversion-function-id. */
8137 if (!cp_parser_parse_definitely (parser))
8139 cp_parser_error (parser, "expected template-name");
8140 return error_mark_node;
8143 /* Look for the identifier. */
8145 identifier = cp_parser_identifier (parser);
8147 /* If we didn't find an identifier, we don't have a template-id. */
8148 if (identifier == error_mark_node)
8149 return error_mark_node;
8151 /* If the name immediately followed the `template' keyword, then it
8152 is a template-name. However, if the next token is not `<', then
8153 we do not treat it as a template-name, since it is not being used
8154 as part of a template-id. This enables us to handle constructs
8157 template <typename T> struct S { S(); };
8158 template <typename T> S<T>::S();
8160 correctly. We would treat `S' as a template -- if it were `S<T>'
8161 -- but we do not if there is no `<'. */
8163 if (processing_template_decl
8164 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8166 /* In a declaration, in a dependent context, we pretend that the
8167 "template" keyword was present in order to improve error
8168 recovery. For example, given:
8170 template <typename T> void f(T::X<int>);
8172 we want to treat "X<int>" as a template-id. */
8174 && !template_keyword_p
8175 && parser->scope && TYPE_P (parser->scope)
8176 && check_dependency_p
8177 && dependent_type_p (parser->scope)
8178 /* Do not do this for dtors (or ctors), since they never
8179 need the template keyword before their name. */
8180 && !constructor_name_p (identifier, parser->scope))
8184 /* Explain what went wrong. */
8185 error ("non-template `%D' used as template", identifier);
8186 inform ("use `%T::template %D' to indicate that it is a template",
8187 parser->scope, identifier);
8188 /* If parsing tentatively, find the location of the "<"
8190 if (cp_parser_parsing_tentatively (parser)
8191 && !cp_parser_committed_to_tentative_parse (parser))
8193 cp_parser_simulate_error (parser);
8194 token = cp_lexer_peek_token (parser->lexer);
8195 token = cp_lexer_prev_token (parser->lexer, token);
8196 start = cp_lexer_token_difference (parser->lexer,
8197 parser->lexer->first_token,
8202 /* Parse the template arguments so that we can issue error
8203 messages about them. */
8204 cp_lexer_consume_token (parser->lexer);
8205 cp_parser_enclosed_template_argument_list (parser);
8206 /* Skip tokens until we find a good place from which to
8207 continue parsing. */
8208 cp_parser_skip_to_closing_parenthesis (parser,
8209 /*recovering=*/true,
8211 /*consume_paren=*/false);
8212 /* If parsing tentatively, permanently remove the
8213 template argument list. That will prevent duplicate
8214 error messages from being issued about the missing
8215 "template" keyword. */
8218 token = cp_lexer_advance_token (parser->lexer,
8219 parser->lexer->first_token,
8221 cp_lexer_purge_tokens_after (parser->lexer, token);
8224 *is_identifier = true;
8228 /* If the "template" keyword is present, then there is generally
8229 no point in doing name-lookup, so we just return IDENTIFIER.
8230 But, if the qualifying scope is non-dependent then we can
8231 (and must) do name-lookup normally. */
8232 if (template_keyword_p
8234 || (TYPE_P (parser->scope)
8235 && dependent_type_p (parser->scope))))
8239 /* Look up the name. */
8240 decl = cp_parser_lookup_name (parser, identifier,
8242 /*is_template=*/false,
8243 /*is_namespace=*/false,
8244 check_dependency_p);
8245 decl = maybe_get_template_decl_from_type_decl (decl);
8247 /* If DECL is a template, then the name was a template-name. */
8248 if (TREE_CODE (decl) == TEMPLATE_DECL)
8252 tree fn = NULL_TREE;
8254 /* The standard does not explicitly indicate whether a name that
8255 names a set of overloaded declarations, some of which are
8256 templates, is a template-name. However, such a name should
8257 be a template-name; otherwise, there is no way to form a
8258 template-id for the overloaded templates. */
8259 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8260 if (TREE_CODE (fns) == OVERLOAD)
8261 for (fn = fns; fn; fn = OVL_NEXT (fn))
8262 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8267 /* Otherwise, the name does not name a template. */
8268 cp_parser_error (parser, "expected template-name");
8269 return error_mark_node;
8273 /* If DECL is dependent, and refers to a function, then just return
8274 its name; we will look it up again during template instantiation. */
8275 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8277 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8278 if (TYPE_P (scope) && dependent_type_p (scope))
8285 /* Parse a template-argument-list.
8287 template-argument-list:
8289 template-argument-list , template-argument
8291 Returns a TREE_VEC containing the arguments. */
8294 cp_parser_template_argument_list (cp_parser* parser)
8296 tree fixed_args[10];
8297 unsigned n_args = 0;
8298 unsigned alloced = 10;
8299 tree *arg_ary = fixed_args;
8301 bool saved_in_template_argument_list_p;
8303 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8304 parser->in_template_argument_list_p = true;
8310 /* Consume the comma. */
8311 cp_lexer_consume_token (parser->lexer);
8313 /* Parse the template-argument. */
8314 argument = cp_parser_template_argument (parser);
8315 if (n_args == alloced)
8319 if (arg_ary == fixed_args)
8321 arg_ary = xmalloc (sizeof (tree) * alloced);
8322 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8325 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8327 arg_ary[n_args++] = argument;
8329 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8331 vec = make_tree_vec (n_args);
8334 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8336 if (arg_ary != fixed_args)
8338 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8342 /* Parse a template-argument.
8345 assignment-expression
8349 The representation is that of an assignment-expression, type-id, or
8350 id-expression -- except that the qualified id-expression is
8351 evaluated, so that the value returned is either a DECL or an
8354 Although the standard says "assignment-expression", it forbids
8355 throw-expressions or assignments in the template argument.
8356 Therefore, we use "conditional-expression" instead. */
8359 cp_parser_template_argument (cp_parser* parser)
8364 bool maybe_type_id = false;
8367 tree qualifying_class;
8369 /* There's really no way to know what we're looking at, so we just
8370 try each alternative in order.
8374 In a template-argument, an ambiguity between a type-id and an
8375 expression is resolved to a type-id, regardless of the form of
8376 the corresponding template-parameter.
8378 Therefore, we try a type-id first. */
8379 cp_parser_parse_tentatively (parser);
8380 argument = cp_parser_type_id (parser);
8381 /* If there was no error parsing the type-id but the next token is a '>>',
8382 we probably found a typo for '> >'. But there are type-id which are
8383 also valid expressions. For instance:
8385 struct X { int operator >> (int); };
8386 template <int V> struct Foo {};
8389 Here 'X()' is a valid type-id of a function type, but the user just
8390 wanted to write the expression "X() >> 5". Thus, we remember that we
8391 found a valid type-id, but we still try to parse the argument as an
8392 expression to see what happens. */
8393 if (!cp_parser_error_occurred (parser)
8394 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8396 maybe_type_id = true;
8397 cp_parser_abort_tentative_parse (parser);
8401 /* If the next token isn't a `,' or a `>', then this argument wasn't
8402 really finished. This means that the argument is not a valid
8404 if (!cp_parser_next_token_ends_template_argument_p (parser))
8405 cp_parser_error (parser, "expected template-argument");
8406 /* If that worked, we're done. */
8407 if (cp_parser_parse_definitely (parser))
8410 /* We're still not sure what the argument will be. */
8411 cp_parser_parse_tentatively (parser);
8412 /* Try a template. */
8413 argument = cp_parser_id_expression (parser,
8414 /*template_keyword_p=*/false,
8415 /*check_dependency_p=*/true,
8417 /*declarator_p=*/false);
8418 /* If the next token isn't a `,' or a `>', then this argument wasn't
8420 if (!cp_parser_next_token_ends_template_argument_p (parser))
8421 cp_parser_error (parser, "expected template-argument");
8422 if (!cp_parser_error_occurred (parser))
8424 /* Figure out what is being referred to. If the id-expression
8425 was for a class template specialization, then we will have a
8426 TYPE_DECL at this point. There is no need to do name lookup
8427 at this point in that case. */
8428 if (TREE_CODE (argument) != TYPE_DECL)
8429 argument = cp_parser_lookup_name (parser, argument,
8431 /*is_template=*/template_p,
8432 /*is_namespace=*/false,
8433 /*check_dependency=*/true);
8434 if (TREE_CODE (argument) != TEMPLATE_DECL
8435 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8436 cp_parser_error (parser, "expected template-name");
8438 if (cp_parser_parse_definitely (parser))
8440 /* It must be a non-type argument. There permitted cases are given
8441 in [temp.arg.nontype]:
8443 -- an integral constant-expression of integral or enumeration
8446 -- the name of a non-type template-parameter; or
8448 -- the name of an object or function with external linkage...
8450 -- the address of an object or function with external linkage...
8452 -- a pointer to member... */
8453 /* Look for a non-type template parameter. */
8454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8456 cp_parser_parse_tentatively (parser);
8457 argument = cp_parser_primary_expression (parser,
8460 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8461 || !cp_parser_next_token_ends_template_argument_p (parser))
8462 cp_parser_simulate_error (parser);
8463 if (cp_parser_parse_definitely (parser))
8466 /* If the next token is "&", the argument must be the address of an
8467 object or function with external linkage. */
8468 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8470 cp_lexer_consume_token (parser->lexer);
8471 /* See if we might have an id-expression. */
8472 token = cp_lexer_peek_token (parser->lexer);
8473 if (token->type == CPP_NAME
8474 || token->keyword == RID_OPERATOR
8475 || token->type == CPP_SCOPE
8476 || token->type == CPP_TEMPLATE_ID
8477 || token->type == CPP_NESTED_NAME_SPECIFIER)
8479 cp_parser_parse_tentatively (parser);
8480 argument = cp_parser_primary_expression (parser,
8483 if (cp_parser_error_occurred (parser)
8484 || !cp_parser_next_token_ends_template_argument_p (parser))
8485 cp_parser_abort_tentative_parse (parser);
8488 if (qualifying_class)
8489 argument = finish_qualified_id_expr (qualifying_class,
8493 if (TREE_CODE (argument) == VAR_DECL)
8495 /* A variable without external linkage might still be a
8496 valid constant-expression, so no error is issued here
8497 if the external-linkage check fails. */
8498 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8499 cp_parser_simulate_error (parser);
8501 else if (is_overloaded_fn (argument))
8502 /* All overloaded functions are allowed; if the external
8503 linkage test does not pass, an error will be issued
8507 && (TREE_CODE (argument) == OFFSET_REF
8508 || TREE_CODE (argument) == SCOPE_REF))
8509 /* A pointer-to-member. */
8512 cp_parser_simulate_error (parser);
8514 if (cp_parser_parse_definitely (parser))
8517 argument = build_x_unary_op (ADDR_EXPR, argument);
8522 /* If the argument started with "&", there are no other valid
8523 alternatives at this point. */
8526 cp_parser_error (parser, "invalid non-type template argument");
8527 return error_mark_node;
8529 /* If the argument wasn't successfully parsed as a type-id followed
8530 by '>>', the argument can only be a constant expression now.
8531 Otherwise, we try parsing the constant-expression tentatively,
8532 because the argument could really be a type-id. */
8534 cp_parser_parse_tentatively (parser);
8535 argument = cp_parser_constant_expression (parser,
8536 /*allow_non_constant_p=*/false,
8537 /*non_constant_p=*/NULL);
8538 argument = fold_non_dependent_expr (argument);
8541 if (!cp_parser_next_token_ends_template_argument_p (parser))
8542 cp_parser_error (parser, "expected template-argument");
8543 if (cp_parser_parse_definitely (parser))
8545 /* We did our best to parse the argument as a non type-id, but that
8546 was the only alternative that matched (albeit with a '>' after
8547 it). We can assume it's just a typo from the user, and a
8548 diagnostic will then be issued. */
8549 return cp_parser_type_id (parser);
8552 /* Parse an explicit-instantiation.
8554 explicit-instantiation:
8555 template declaration
8557 Although the standard says `declaration', what it really means is:
8559 explicit-instantiation:
8560 template decl-specifier-seq [opt] declarator [opt] ;
8562 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8563 supposed to be allowed. A defect report has been filed about this
8568 explicit-instantiation:
8569 storage-class-specifier template
8570 decl-specifier-seq [opt] declarator [opt] ;
8571 function-specifier template
8572 decl-specifier-seq [opt] declarator [opt] ; */
8575 cp_parser_explicit_instantiation (cp_parser* parser)
8577 int declares_class_or_enum;
8578 tree decl_specifiers;
8580 tree extension_specifier = NULL_TREE;
8582 /* Look for an (optional) storage-class-specifier or
8583 function-specifier. */
8584 if (cp_parser_allow_gnu_extensions_p (parser))
8587 = cp_parser_storage_class_specifier_opt (parser);
8588 if (!extension_specifier)
8589 extension_specifier = cp_parser_function_specifier_opt (parser);
8592 /* Look for the `template' keyword. */
8593 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8594 /* Let the front end know that we are processing an explicit
8596 begin_explicit_instantiation ();
8597 /* [temp.explicit] says that we are supposed to ignore access
8598 control while processing explicit instantiation directives. */
8599 push_deferring_access_checks (dk_no_check);
8600 /* Parse a decl-specifier-seq. */
8602 = cp_parser_decl_specifier_seq (parser,
8603 CP_PARSER_FLAGS_OPTIONAL,
8605 &declares_class_or_enum);
8606 /* If there was exactly one decl-specifier, and it declared a class,
8607 and there's no declarator, then we have an explicit type
8609 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8613 type = check_tag_decl (decl_specifiers);
8614 /* Turn access control back on for names used during
8615 template instantiation. */
8616 pop_deferring_access_checks ();
8618 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8625 /* Parse the declarator. */
8627 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8628 /*ctor_dtor_or_conv_p=*/NULL,
8629 /*parenthesized_p=*/NULL,
8630 /*member_p=*/false);
8631 cp_parser_check_for_definition_in_return_type (declarator,
8632 declares_class_or_enum);
8633 if (declarator != error_mark_node)
8635 decl = grokdeclarator (declarator, decl_specifiers,
8637 /* Turn access control back on for names used during
8638 template instantiation. */
8639 pop_deferring_access_checks ();
8640 /* Do the explicit instantiation. */
8641 do_decl_instantiation (decl, extension_specifier);
8645 pop_deferring_access_checks ();
8646 /* Skip the body of the explicit instantiation. */
8647 cp_parser_skip_to_end_of_statement (parser);
8650 /* We're done with the instantiation. */
8651 end_explicit_instantiation ();
8653 cp_parser_consume_semicolon_at_end_of_statement (parser);
8656 /* Parse an explicit-specialization.
8658 explicit-specialization:
8659 template < > declaration
8661 Although the standard says `declaration', what it really means is:
8663 explicit-specialization:
8664 template <> decl-specifier [opt] init-declarator [opt] ;
8665 template <> function-definition
8666 template <> explicit-specialization
8667 template <> template-declaration */
8670 cp_parser_explicit_specialization (cp_parser* parser)
8672 /* Look for the `template' keyword. */
8673 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8674 /* Look for the `<'. */
8675 cp_parser_require (parser, CPP_LESS, "`<'");
8676 /* Look for the `>'. */
8677 cp_parser_require (parser, CPP_GREATER, "`>'");
8678 /* We have processed another parameter list. */
8679 ++parser->num_template_parameter_lists;
8680 /* Let the front end know that we are beginning a specialization. */
8681 begin_specialization ();
8683 /* If the next keyword is `template', we need to figure out whether
8684 or not we're looking a template-declaration. */
8685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8687 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8688 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8689 cp_parser_template_declaration_after_export (parser,
8690 /*member_p=*/false);
8692 cp_parser_explicit_specialization (parser);
8695 /* Parse the dependent declaration. */
8696 cp_parser_single_declaration (parser,
8700 /* We're done with the specialization. */
8701 end_specialization ();
8702 /* We're done with this parameter list. */
8703 --parser->num_template_parameter_lists;
8706 /* Parse a type-specifier.
8709 simple-type-specifier
8712 elaborated-type-specifier
8720 Returns a representation of the type-specifier. If the
8721 type-specifier is a keyword (like `int' or `const', or
8722 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8723 For a class-specifier, enum-specifier, or elaborated-type-specifier
8724 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8726 If IS_FRIEND is TRUE then this type-specifier is being declared a
8727 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8728 appearing in a decl-specifier-seq.
8730 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8731 class-specifier, enum-specifier, or elaborated-type-specifier, then
8732 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8733 if a type is declared; 2 if it is defined. Otherwise, it is set to
8736 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8737 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8741 cp_parser_type_specifier (cp_parser* parser,
8742 cp_parser_flags flags,
8744 bool is_declaration,
8745 int* declares_class_or_enum,
8746 bool* is_cv_qualifier)
8748 tree type_spec = NULL_TREE;
8752 /* Assume this type-specifier does not declare a new type. */
8753 if (declares_class_or_enum)
8754 *declares_class_or_enum = 0;
8755 /* And that it does not specify a cv-qualifier. */
8756 if (is_cv_qualifier)
8757 *is_cv_qualifier = false;
8758 /* Peek at the next token. */
8759 token = cp_lexer_peek_token (parser->lexer);
8761 /* If we're looking at a keyword, we can use that to guide the
8762 production we choose. */
8763 keyword = token->keyword;
8767 /* 'enum' [identifier] '{' introduces an enum-specifier;
8768 'enum' <anything else> introduces an elaborated-type-specifier. */
8769 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8770 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8771 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8774 if (parser->num_template_parameter_lists)
8776 error ("template declaration of `enum'");
8777 cp_parser_skip_to_end_of_block_or_statement (parser);
8778 type_spec = error_mark_node;
8781 type_spec = cp_parser_enum_specifier (parser);
8783 if (declares_class_or_enum)
8784 *declares_class_or_enum = 2;
8788 goto elaborated_type_specifier;
8790 /* Any of these indicate either a class-specifier, or an
8791 elaborated-type-specifier. */
8795 /* Parse tentatively so that we can back up if we don't find a
8796 class-specifier or enum-specifier. */
8797 cp_parser_parse_tentatively (parser);
8798 /* Look for the class-specifier. */
8799 type_spec = cp_parser_class_specifier (parser);
8800 /* If that worked, we're done. */
8801 if (cp_parser_parse_definitely (parser))
8803 if (declares_class_or_enum)
8804 *declares_class_or_enum = 2;
8811 elaborated_type_specifier:
8812 /* Look for an elaborated-type-specifier. */
8813 type_spec = cp_parser_elaborated_type_specifier (parser,
8816 /* We're declaring a class or enum -- unless we're using
8818 if (declares_class_or_enum && keyword != RID_TYPENAME)
8819 *declares_class_or_enum = 1;
8825 type_spec = cp_parser_cv_qualifier_opt (parser);
8826 /* Even though we call a routine that looks for an optional
8827 qualifier, we know that there should be one. */
8828 my_friendly_assert (type_spec != NULL, 20000328);
8829 /* This type-specifier was a cv-qualified. */
8830 if (is_cv_qualifier)
8831 *is_cv_qualifier = true;
8836 /* The `__complex__' keyword is a GNU extension. */
8837 return cp_lexer_consume_token (parser->lexer)->value;
8843 /* If we do not already have a type-specifier, assume we are looking
8844 at a simple-type-specifier. */
8845 type_spec = cp_parser_simple_type_specifier (parser, flags,
8846 /*identifier_p=*/true);
8848 /* If we didn't find a type-specifier, and a type-specifier was not
8849 optional in this context, issue an error message. */
8850 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8852 cp_parser_error (parser, "expected type specifier");
8853 return error_mark_node;
8859 /* Parse a simple-type-specifier.
8861 simple-type-specifier:
8862 :: [opt] nested-name-specifier [opt] type-name
8863 :: [opt] nested-name-specifier template template-id
8878 simple-type-specifier:
8879 __typeof__ unary-expression
8880 __typeof__ ( type-id )
8882 For the various keywords, the value returned is simply the
8883 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8884 For the first two productions, and if IDENTIFIER_P is false, the
8885 value returned is the indicated TYPE_DECL. */
8888 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8891 tree type = NULL_TREE;
8894 /* Peek at the next token. */
8895 token = cp_lexer_peek_token (parser->lexer);
8897 /* If we're looking at a keyword, things are easy. */
8898 switch (token->keyword)
8901 type = char_type_node;
8904 type = wchar_type_node;
8907 type = boolean_type_node;
8910 type = short_integer_type_node;
8913 type = integer_type_node;
8916 type = long_integer_type_node;
8919 type = integer_type_node;
8922 type = unsigned_type_node;
8925 type = float_type_node;
8928 type = double_type_node;
8931 type = void_type_node;
8938 /* Consume the `typeof' token. */
8939 cp_lexer_consume_token (parser->lexer);
8940 /* Parse the operand to `typeof'. */
8941 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8942 /* If it is not already a TYPE, take its type. */
8943 if (!TYPE_P (operand))
8944 operand = finish_typeof (operand);
8953 /* If the type-specifier was for a built-in type, we're done. */
8958 /* Consume the token. */
8959 id = cp_lexer_consume_token (parser->lexer)->value;
8961 /* There is no valid C++ program where a non-template type is
8962 followed by a "<". That usually indicates that the user thought
8963 that the type was a template. */
8964 cp_parser_check_for_invalid_template_id (parser, type);
8966 return identifier_p ? id : TYPE_NAME (type);
8969 /* The type-specifier must be a user-defined type. */
8970 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8975 /* Don't gobble tokens or issue error messages if this is an
8976 optional type-specifier. */
8977 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8978 cp_parser_parse_tentatively (parser);
8980 /* Look for the optional `::' operator. */
8982 = (cp_parser_global_scope_opt (parser,
8983 /*current_scope_valid_p=*/false)
8985 /* Look for the nested-name specifier. */
8987 = (cp_parser_nested_name_specifier_opt (parser,
8988 /*typename_keyword_p=*/false,
8989 /*check_dependency_p=*/true,
8991 /*is_declaration=*/false)
8993 /* If we have seen a nested-name-specifier, and the next token
8994 is `template', then we are using the template-id production. */
8996 && cp_parser_optional_template_keyword (parser))
8998 /* Look for the template-id. */
8999 type = cp_parser_template_id (parser,
9000 /*template_keyword_p=*/true,
9001 /*check_dependency_p=*/true,
9002 /*is_declaration=*/false);
9003 /* If the template-id did not name a type, we are out of
9005 if (TREE_CODE (type) != TYPE_DECL)
9007 cp_parser_error (parser, "expected template-id for type");
9011 /* Otherwise, look for a type-name. */
9013 type = cp_parser_type_name (parser);
9014 /* Keep track of all name-lookups performed in class scopes. */
9018 && TREE_CODE (type) == TYPE_DECL
9019 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9020 maybe_note_name_used_in_class (DECL_NAME (type), type);
9021 /* If it didn't work out, we don't have a TYPE. */
9022 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9023 && !cp_parser_parse_definitely (parser))
9027 /* If we didn't get a type-name, issue an error message. */
9028 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9030 cp_parser_error (parser, "expected type-name");
9031 return error_mark_node;
9034 /* There is no valid C++ program where a non-template type is
9035 followed by a "<". That usually indicates that the user thought
9036 that the type was a template. */
9037 if (type && type != error_mark_node)
9038 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9043 /* Parse a type-name.
9056 Returns a TYPE_DECL for the the type. */
9059 cp_parser_type_name (cp_parser* parser)
9064 /* We can't know yet whether it is a class-name or not. */
9065 cp_parser_parse_tentatively (parser);
9066 /* Try a class-name. */
9067 type_decl = cp_parser_class_name (parser,
9068 /*typename_keyword_p=*/false,
9069 /*template_keyword_p=*/false,
9071 /*check_dependency_p=*/true,
9072 /*class_head_p=*/false,
9073 /*is_declaration=*/false);
9074 /* If it's not a class-name, keep looking. */
9075 if (!cp_parser_parse_definitely (parser))
9077 /* It must be a typedef-name or an enum-name. */
9078 identifier = cp_parser_identifier (parser);
9079 if (identifier == error_mark_node)
9080 return error_mark_node;
9082 /* Look up the type-name. */
9083 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9084 /* Issue an error if we did not find a type-name. */
9085 if (TREE_CODE (type_decl) != TYPE_DECL)
9087 if (!cp_parser_simulate_error (parser))
9088 cp_parser_name_lookup_error (parser, identifier, type_decl,
9090 type_decl = error_mark_node;
9092 /* Remember that the name was used in the definition of the
9093 current class so that we can check later to see if the
9094 meaning would have been different after the class was
9095 entirely defined. */
9096 else if (type_decl != error_mark_node
9098 maybe_note_name_used_in_class (identifier, type_decl);
9105 /* Parse an elaborated-type-specifier. Note that the grammar given
9106 here incorporates the resolution to DR68.
9108 elaborated-type-specifier:
9109 class-key :: [opt] nested-name-specifier [opt] identifier
9110 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9111 enum :: [opt] nested-name-specifier [opt] identifier
9112 typename :: [opt] nested-name-specifier identifier
9113 typename :: [opt] nested-name-specifier template [opt]
9118 elaborated-type-specifier:
9119 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9120 class-key attributes :: [opt] nested-name-specifier [opt]
9121 template [opt] template-id
9122 enum attributes :: [opt] nested-name-specifier [opt] identifier
9124 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9125 declared `friend'. If IS_DECLARATION is TRUE, then this
9126 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9127 something is being declared.
9129 Returns the TYPE specified. */
9132 cp_parser_elaborated_type_specifier (cp_parser* parser,
9134 bool is_declaration)
9136 enum tag_types tag_type;
9138 tree type = NULL_TREE;
9139 tree attributes = NULL_TREE;
9141 /* See if we're looking at the `enum' keyword. */
9142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9144 /* Consume the `enum' token. */
9145 cp_lexer_consume_token (parser->lexer);
9146 /* Remember that it's an enumeration type. */
9147 tag_type = enum_type;
9148 /* Parse the attributes. */
9149 attributes = cp_parser_attributes_opt (parser);
9151 /* Or, it might be `typename'. */
9152 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9155 /* Consume the `typename' token. */
9156 cp_lexer_consume_token (parser->lexer);
9157 /* Remember that it's a `typename' type. */
9158 tag_type = typename_type;
9159 /* The `typename' keyword is only allowed in templates. */
9160 if (!processing_template_decl)
9161 pedwarn ("using `typename' outside of template");
9163 /* Otherwise it must be a class-key. */
9166 tag_type = cp_parser_class_key (parser);
9167 if (tag_type == none_type)
9168 return error_mark_node;
9169 /* Parse the attributes. */
9170 attributes = cp_parser_attributes_opt (parser);
9173 /* Look for the `::' operator. */
9174 cp_parser_global_scope_opt (parser,
9175 /*current_scope_valid_p=*/false);
9176 /* Look for the nested-name-specifier. */
9177 if (tag_type == typename_type)
9179 if (cp_parser_nested_name_specifier (parser,
9180 /*typename_keyword_p=*/true,
9181 /*check_dependency_p=*/true,
9185 return error_mark_node;
9188 /* Even though `typename' is not present, the proposed resolution
9189 to Core Issue 180 says that in `class A<T>::B', `B' should be
9190 considered a type-name, even if `A<T>' is dependent. */
9191 cp_parser_nested_name_specifier_opt (parser,
9192 /*typename_keyword_p=*/true,
9193 /*check_dependency_p=*/true,
9196 /* For everything but enumeration types, consider a template-id. */
9197 if (tag_type != enum_type)
9199 bool template_p = false;
9202 /* Allow the `template' keyword. */
9203 template_p = cp_parser_optional_template_keyword (parser);
9204 /* If we didn't see `template', we don't know if there's a
9205 template-id or not. */
9207 cp_parser_parse_tentatively (parser);
9208 /* Parse the template-id. */
9209 decl = cp_parser_template_id (parser, template_p,
9210 /*check_dependency_p=*/true,
9212 /* If we didn't find a template-id, look for an ordinary
9214 if (!template_p && !cp_parser_parse_definitely (parser))
9216 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9217 in effect, then we must assume that, upon instantiation, the
9218 template will correspond to a class. */
9219 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9220 && tag_type == typename_type)
9221 type = make_typename_type (parser->scope, decl,
9224 type = TREE_TYPE (decl);
9227 /* For an enumeration type, consider only a plain identifier. */
9230 identifier = cp_parser_identifier (parser);
9232 if (identifier == error_mark_node)
9234 parser->scope = NULL_TREE;
9235 return error_mark_node;
9238 /* For a `typename', we needn't call xref_tag. */
9239 if (tag_type == typename_type)
9240 return make_typename_type (parser->scope, identifier,
9242 /* Look up a qualified name in the usual way. */
9247 /* In an elaborated-type-specifier, names are assumed to name
9248 types, so we set IS_TYPE to TRUE when calling
9249 cp_parser_lookup_name. */
9250 decl = cp_parser_lookup_name (parser, identifier,
9252 /*is_template=*/false,
9253 /*is_namespace=*/false,
9254 /*check_dependency=*/true);
9256 /* If we are parsing friend declaration, DECL may be a
9257 TEMPLATE_DECL tree node here. However, we need to check
9258 whether this TEMPLATE_DECL results in valid code. Consider
9259 the following example:
9262 template <class T> class C {};
9265 template <class T> friend class N::C; // #1, valid code
9267 template <class T> class Y {
9268 friend class N::C; // #2, invalid code
9271 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9272 name lookup of `N::C'. We see that friend declaration must
9273 be template for the code to be valid. Note that
9274 processing_template_decl does not work here since it is
9275 always 1 for the above two cases. */
9277 decl = (cp_parser_maybe_treat_template_as_class
9278 (decl, /*tag_name_p=*/is_friend
9279 && parser->num_template_parameter_lists));
9281 if (TREE_CODE (decl) != TYPE_DECL)
9283 error ("expected type-name");
9284 return error_mark_node;
9287 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9288 check_elaborated_type_specifier
9290 (parser->num_template_parameter_lists
9291 || DECL_SELF_REFERENCE_P (decl)));
9293 type = TREE_TYPE (decl);
9297 /* An elaborated-type-specifier sometimes introduces a new type and
9298 sometimes names an existing type. Normally, the rule is that it
9299 introduces a new type only if there is not an existing type of
9300 the same name already in scope. For example, given:
9303 void f() { struct S s; }
9305 the `struct S' in the body of `f' is the same `struct S' as in
9306 the global scope; the existing definition is used. However, if
9307 there were no global declaration, this would introduce a new
9308 local class named `S'.
9310 An exception to this rule applies to the following code:
9312 namespace N { struct S; }
9314 Here, the elaborated-type-specifier names a new type
9315 unconditionally; even if there is already an `S' in the
9316 containing scope this declaration names a new type.
9317 This exception only applies if the elaborated-type-specifier
9318 forms the complete declaration:
9322 A declaration consisting solely of `class-key identifier ;' is
9323 either a redeclaration of the name in the current scope or a
9324 forward declaration of the identifier as a class name. It
9325 introduces the name into the current scope.
9327 We are in this situation precisely when the next token is a `;'.
9329 An exception to the exception is that a `friend' declaration does
9330 *not* name a new type; i.e., given:
9332 struct S { friend struct T; };
9334 `T' is not a new type in the scope of `S'.
9336 Also, `new struct S' or `sizeof (struct S)' never results in the
9337 definition of a new type; a new type can only be declared in a
9338 declaration context. */
9340 /* Warn about attributes. They are ignored. */
9342 warning ("type attributes are honored only at type definition");
9344 type = xref_tag (tag_type, identifier,
9347 || cp_lexer_next_token_is_not (parser->lexer,
9349 parser->num_template_parameter_lists);
9352 if (tag_type != enum_type)
9353 cp_parser_check_class_key (tag_type, type);
9355 /* A "<" cannot follow an elaborated type specifier. If that
9356 happens, the user was probably trying to form a template-id. */
9357 cp_parser_check_for_invalid_template_id (parser, type);
9362 /* Parse an enum-specifier.
9365 enum identifier [opt] { enumerator-list [opt] }
9367 Returns an ENUM_TYPE representing the enumeration. */
9370 cp_parser_enum_specifier (cp_parser* parser)
9373 tree identifier = NULL_TREE;
9376 /* Look for the `enum' keyword. */
9377 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9378 return error_mark_node;
9379 /* Peek at the next token. */
9380 token = cp_lexer_peek_token (parser->lexer);
9382 /* See if it is an identifier. */
9383 if (token->type == CPP_NAME)
9384 identifier = cp_parser_identifier (parser);
9386 /* Look for the `{'. */
9387 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9388 return error_mark_node;
9390 /* At this point, we're going ahead with the enum-specifier, even
9391 if some other problem occurs. */
9392 cp_parser_commit_to_tentative_parse (parser);
9394 /* Issue an error message if type-definitions are forbidden here. */
9395 cp_parser_check_type_definition (parser);
9397 /* Create the new type. */
9398 type = start_enum (identifier ? identifier : make_anon_name ());
9400 /* Peek at the next token. */
9401 token = cp_lexer_peek_token (parser->lexer);
9402 /* If it's not a `}', then there are some enumerators. */
9403 if (token->type != CPP_CLOSE_BRACE)
9404 cp_parser_enumerator_list (parser, type);
9405 /* Look for the `}'. */
9406 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9408 /* Finish up the enumeration. */
9414 /* Parse an enumerator-list. The enumerators all have the indicated
9418 enumerator-definition
9419 enumerator-list , enumerator-definition */
9422 cp_parser_enumerator_list (cp_parser* parser, tree type)
9428 /* Parse an enumerator-definition. */
9429 cp_parser_enumerator_definition (parser, type);
9430 /* Peek at the next token. */
9431 token = cp_lexer_peek_token (parser->lexer);
9432 /* If it's not a `,', then we've reached the end of the
9434 if (token->type != CPP_COMMA)
9436 /* Otherwise, consume the `,' and keep going. */
9437 cp_lexer_consume_token (parser->lexer);
9438 /* If the next token is a `}', there is a trailing comma. */
9439 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9441 if (pedantic && !in_system_header)
9442 pedwarn ("comma at end of enumerator list");
9448 /* Parse an enumerator-definition. The enumerator has the indicated
9451 enumerator-definition:
9453 enumerator = constant-expression
9459 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9465 /* Look for the identifier. */
9466 identifier = cp_parser_identifier (parser);
9467 if (identifier == error_mark_node)
9470 /* Peek at the next token. */
9471 token = cp_lexer_peek_token (parser->lexer);
9472 /* If it's an `=', then there's an explicit value. */
9473 if (token->type == CPP_EQ)
9475 /* Consume the `=' token. */
9476 cp_lexer_consume_token (parser->lexer);
9477 /* Parse the value. */
9478 value = cp_parser_constant_expression (parser,
9479 /*allow_non_constant_p=*/false,
9485 /* Create the enumerator. */
9486 build_enumerator (identifier, value, type);
9489 /* Parse a namespace-name.
9492 original-namespace-name
9495 Returns the NAMESPACE_DECL for the namespace. */
9498 cp_parser_namespace_name (cp_parser* parser)
9501 tree namespace_decl;
9503 /* Get the name of the namespace. */
9504 identifier = cp_parser_identifier (parser);
9505 if (identifier == error_mark_node)
9506 return error_mark_node;
9508 /* Look up the identifier in the currently active scope. Look only
9509 for namespaces, due to:
9513 When looking up a namespace-name in a using-directive or alias
9514 definition, only namespace names are considered.
9520 During the lookup of a name preceding the :: scope resolution
9521 operator, object, function, and enumerator names are ignored.
9523 (Note that cp_parser_class_or_namespace_name only calls this
9524 function if the token after the name is the scope resolution
9526 namespace_decl = cp_parser_lookup_name (parser, identifier,
9528 /*is_template=*/false,
9529 /*is_namespace=*/true,
9530 /*check_dependency=*/true);
9531 /* If it's not a namespace, issue an error. */
9532 if (namespace_decl == error_mark_node
9533 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9535 cp_parser_error (parser, "expected namespace-name");
9536 namespace_decl = error_mark_node;
9539 return namespace_decl;
9542 /* Parse a namespace-definition.
9544 namespace-definition:
9545 named-namespace-definition
9546 unnamed-namespace-definition
9548 named-namespace-definition:
9549 original-namespace-definition
9550 extension-namespace-definition
9552 original-namespace-definition:
9553 namespace identifier { namespace-body }
9555 extension-namespace-definition:
9556 namespace original-namespace-name { namespace-body }
9558 unnamed-namespace-definition:
9559 namespace { namespace-body } */
9562 cp_parser_namespace_definition (cp_parser* parser)
9566 /* Look for the `namespace' keyword. */
9567 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9569 /* Get the name of the namespace. We do not attempt to distinguish
9570 between an original-namespace-definition and an
9571 extension-namespace-definition at this point. The semantic
9572 analysis routines are responsible for that. */
9573 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9574 identifier = cp_parser_identifier (parser);
9576 identifier = NULL_TREE;
9578 /* Look for the `{' to start the namespace. */
9579 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9580 /* Start the namespace. */
9581 push_namespace (identifier);
9582 /* Parse the body of the namespace. */
9583 cp_parser_namespace_body (parser);
9584 /* Finish the namespace. */
9586 /* Look for the final `}'. */
9587 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9590 /* Parse a namespace-body.
9593 declaration-seq [opt] */
9596 cp_parser_namespace_body (cp_parser* parser)
9598 cp_parser_declaration_seq_opt (parser);
9601 /* Parse a namespace-alias-definition.
9603 namespace-alias-definition:
9604 namespace identifier = qualified-namespace-specifier ; */
9607 cp_parser_namespace_alias_definition (cp_parser* parser)
9610 tree namespace_specifier;
9612 /* Look for the `namespace' keyword. */
9613 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9614 /* Look for the identifier. */
9615 identifier = cp_parser_identifier (parser);
9616 if (identifier == error_mark_node)
9618 /* Look for the `=' token. */
9619 cp_parser_require (parser, CPP_EQ, "`='");
9620 /* Look for the qualified-namespace-specifier. */
9622 = cp_parser_qualified_namespace_specifier (parser);
9623 /* Look for the `;' token. */
9624 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9626 /* Register the alias in the symbol table. */
9627 do_namespace_alias (identifier, namespace_specifier);
9630 /* Parse a qualified-namespace-specifier.
9632 qualified-namespace-specifier:
9633 :: [opt] nested-name-specifier [opt] namespace-name
9635 Returns a NAMESPACE_DECL corresponding to the specified
9639 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9641 /* Look for the optional `::'. */
9642 cp_parser_global_scope_opt (parser,
9643 /*current_scope_valid_p=*/false);
9645 /* Look for the optional nested-name-specifier. */
9646 cp_parser_nested_name_specifier_opt (parser,
9647 /*typename_keyword_p=*/false,
9648 /*check_dependency_p=*/true,
9650 /*is_declaration=*/true);
9652 return cp_parser_namespace_name (parser);
9655 /* Parse a using-declaration.
9658 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9659 using :: unqualified-id ; */
9662 cp_parser_using_declaration (cp_parser* parser)
9665 bool typename_p = false;
9666 bool global_scope_p;
9672 /* Look for the `using' keyword. */
9673 cp_parser_require_keyword (parser, RID_USING, "`using'");
9675 /* Peek at the next token. */
9676 token = cp_lexer_peek_token (parser->lexer);
9677 /* See if it's `typename'. */
9678 if (token->keyword == RID_TYPENAME)
9680 /* Remember that we've seen it. */
9682 /* Consume the `typename' token. */
9683 cp_lexer_consume_token (parser->lexer);
9686 /* Look for the optional global scope qualification. */
9688 = (cp_parser_global_scope_opt (parser,
9689 /*current_scope_valid_p=*/false)
9692 /* If we saw `typename', or didn't see `::', then there must be a
9693 nested-name-specifier present. */
9694 if (typename_p || !global_scope_p)
9695 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9696 /*check_dependency_p=*/true,
9698 /*is_declaration=*/true);
9699 /* Otherwise, we could be in either of the two productions. In that
9700 case, treat the nested-name-specifier as optional. */
9702 qscope = cp_parser_nested_name_specifier_opt (parser,
9703 /*typename_keyword_p=*/false,
9704 /*check_dependency_p=*/true,
9706 /*is_declaration=*/true);
9708 qscope = global_namespace;
9710 /* Parse the unqualified-id. */
9711 identifier = cp_parser_unqualified_id (parser,
9712 /*template_keyword_p=*/false,
9713 /*check_dependency_p=*/true,
9714 /*declarator_p=*/true);
9716 /* The function we call to handle a using-declaration is different
9717 depending on what scope we are in. */
9718 if (identifier == error_mark_node)
9720 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9721 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9722 /* [namespace.udecl]
9724 A using declaration shall not name a template-id. */
9725 error ("a template-id may not appear in a using-declaration");
9728 scope = current_scope ();
9729 if (scope && TYPE_P (scope))
9731 /* Create the USING_DECL. */
9732 decl = do_class_using_decl (build_nt (SCOPE_REF,
9735 /* Add it to the list of members in this class. */
9736 finish_member_declaration (decl);
9740 decl = cp_parser_lookup_name_simple (parser, identifier);
9741 if (decl == error_mark_node)
9742 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9744 do_local_using_decl (decl, qscope, identifier);
9746 do_toplevel_using_decl (decl, qscope, identifier);
9750 /* Look for the final `;'. */
9751 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9754 /* Parse a using-directive.
9757 using namespace :: [opt] nested-name-specifier [opt]
9761 cp_parser_using_directive (cp_parser* parser)
9763 tree namespace_decl;
9766 /* Look for the `using' keyword. */
9767 cp_parser_require_keyword (parser, RID_USING, "`using'");
9768 /* And the `namespace' keyword. */
9769 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9770 /* Look for the optional `::' operator. */
9771 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9772 /* And the optional nested-name-specifier. */
9773 cp_parser_nested_name_specifier_opt (parser,
9774 /*typename_keyword_p=*/false,
9775 /*check_dependency_p=*/true,
9777 /*is_declaration=*/true);
9778 /* Get the namespace being used. */
9779 namespace_decl = cp_parser_namespace_name (parser);
9780 /* And any specified attributes. */
9781 attribs = cp_parser_attributes_opt (parser);
9782 /* Update the symbol table. */
9783 parse_using_directive (namespace_decl, attribs);
9784 /* Look for the final `;'. */
9785 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9788 /* Parse an asm-definition.
9791 asm ( string-literal ) ;
9796 asm volatile [opt] ( string-literal ) ;
9797 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9798 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9799 : asm-operand-list [opt] ) ;
9800 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9801 : asm-operand-list [opt]
9802 : asm-operand-list [opt] ) ; */
9805 cp_parser_asm_definition (cp_parser* parser)
9809 tree outputs = NULL_TREE;
9810 tree inputs = NULL_TREE;
9811 tree clobbers = NULL_TREE;
9813 bool volatile_p = false;
9814 bool extended_p = false;
9816 /* Look for the `asm' keyword. */
9817 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9818 /* See if the next token is `volatile'. */
9819 if (cp_parser_allow_gnu_extensions_p (parser)
9820 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9822 /* Remember that we saw the `volatile' keyword. */
9824 /* Consume the token. */
9825 cp_lexer_consume_token (parser->lexer);
9827 /* Look for the opening `('. */
9828 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9829 /* Look for the string. */
9830 token = cp_parser_require (parser, CPP_STRING, "asm body");
9833 string = token->value;
9834 /* If we're allowing GNU extensions, check for the extended assembly
9835 syntax. Unfortunately, the `:' tokens need not be separated by
9836 a space in C, and so, for compatibility, we tolerate that here
9837 too. Doing that means that we have to treat the `::' operator as
9839 if (cp_parser_allow_gnu_extensions_p (parser)
9840 && at_function_scope_p ()
9841 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9842 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9844 bool inputs_p = false;
9845 bool clobbers_p = false;
9847 /* The extended syntax was used. */
9850 /* Look for outputs. */
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9853 /* Consume the `:'. */
9854 cp_lexer_consume_token (parser->lexer);
9855 /* Parse the output-operands. */
9856 if (cp_lexer_next_token_is_not (parser->lexer,
9858 && cp_lexer_next_token_is_not (parser->lexer,
9860 && cp_lexer_next_token_is_not (parser->lexer,
9862 outputs = cp_parser_asm_operand_list (parser);
9864 /* If the next token is `::', there are no outputs, and the
9865 next token is the beginning of the inputs. */
9866 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9867 /* The inputs are coming next. */
9870 /* Look for inputs. */
9872 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9874 /* Consume the `:' or `::'. */
9875 cp_lexer_consume_token (parser->lexer);
9876 /* Parse the output-operands. */
9877 if (cp_lexer_next_token_is_not (parser->lexer,
9879 && cp_lexer_next_token_is_not (parser->lexer,
9881 inputs = cp_parser_asm_operand_list (parser);
9883 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9884 /* The clobbers are coming next. */
9887 /* Look for clobbers. */
9889 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9891 /* Consume the `:' or `::'. */
9892 cp_lexer_consume_token (parser->lexer);
9893 /* Parse the clobbers. */
9894 if (cp_lexer_next_token_is_not (parser->lexer,
9896 clobbers = cp_parser_asm_clobber_list (parser);
9899 /* Look for the closing `)'. */
9900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9901 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9902 /*consume_paren=*/true);
9903 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9905 /* Create the ASM_STMT. */
9906 if (at_function_scope_p ())
9909 finish_asm_stmt (volatile_p
9910 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9911 string, outputs, inputs, clobbers);
9912 /* If the extended syntax was not used, mark the ASM_STMT. */
9914 ASM_INPUT_P (asm_stmt) = 1;
9917 assemble_asm (string);
9920 /* Declarators [gram.dcl.decl] */
9922 /* Parse an init-declarator.
9925 declarator initializer [opt]
9930 declarator asm-specification [opt] attributes [opt] initializer [opt]
9932 function-definition:
9933 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9935 decl-specifier-seq [opt] declarator function-try-block
9939 function-definition:
9940 __extension__ function-definition
9942 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9943 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9944 then this declarator appears in a class scope. The new DECL created
9945 by this declarator is returned.
9947 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9948 for a function-definition here as well. If the declarator is a
9949 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9950 be TRUE upon return. By that point, the function-definition will
9951 have been completely parsed.
9953 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9957 cp_parser_init_declarator (cp_parser* parser,
9958 tree decl_specifiers,
9959 tree prefix_attributes,
9960 bool function_definition_allowed_p,
9962 int declares_class_or_enum,
9963 bool* function_definition_p)
9968 tree asm_specification;
9970 tree decl = NULL_TREE;
9972 bool is_initialized;
9973 bool is_parenthesized_init;
9974 bool is_non_constant_init;
9975 int ctor_dtor_or_conv_p;
9979 /* Assume that this is not the declarator for a function
9981 if (function_definition_p)
9982 *function_definition_p = false;
9984 /* Defer access checks while parsing the declarator; we cannot know
9985 what names are accessible until we know what is being
9987 resume_deferring_access_checks ();
9989 /* Parse the declarator. */
9991 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9992 &ctor_dtor_or_conv_p,
9993 /*parenthesized_p=*/NULL,
9994 /*member_p=*/false);
9995 /* Gather up the deferred checks. */
9996 stop_deferring_access_checks ();
9998 /* If the DECLARATOR was erroneous, there's no need to go
10000 if (declarator == error_mark_node)
10001 return error_mark_node;
10003 cp_parser_check_for_definition_in_return_type (declarator,
10004 declares_class_or_enum);
10006 /* Figure out what scope the entity declared by the DECLARATOR is
10007 located in. `grokdeclarator' sometimes changes the scope, so
10008 we compute it now. */
10009 scope = get_scope_of_declarator (declarator);
10011 /* If we're allowing GNU extensions, look for an asm-specification
10013 if (cp_parser_allow_gnu_extensions_p (parser))
10015 /* Look for an asm-specification. */
10016 asm_specification = cp_parser_asm_specification_opt (parser);
10017 /* And attributes. */
10018 attributes = cp_parser_attributes_opt (parser);
10022 asm_specification = NULL_TREE;
10023 attributes = NULL_TREE;
10026 /* Peek at the next token. */
10027 token = cp_lexer_peek_token (parser->lexer);
10028 /* Check to see if the token indicates the start of a
10029 function-definition. */
10030 if (cp_parser_token_starts_function_definition_p (token))
10032 if (!function_definition_allowed_p)
10034 /* If a function-definition should not appear here, issue an
10036 cp_parser_error (parser,
10037 "a function-definition is not allowed here");
10038 return error_mark_node;
10042 /* Neither attributes nor an asm-specification are allowed
10043 on a function-definition. */
10044 if (asm_specification)
10045 error ("an asm-specification is not allowed on a function-definition");
10047 error ("attributes are not allowed on a function-definition");
10048 /* This is a function-definition. */
10049 *function_definition_p = true;
10051 /* Parse the function definition. */
10053 decl = cp_parser_save_member_function_body (parser,
10056 prefix_attributes);
10059 = (cp_parser_function_definition_from_specifiers_and_declarator
10060 (parser, decl_specifiers, prefix_attributes, declarator));
10068 Only in function declarations for constructors, destructors, and
10069 type conversions can the decl-specifier-seq be omitted.
10071 We explicitly postpone this check past the point where we handle
10072 function-definitions because we tolerate function-definitions
10073 that are missing their return types in some modes. */
10074 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10076 cp_parser_error (parser,
10077 "expected constructor, destructor, or type conversion");
10078 return error_mark_node;
10081 /* An `=' or an `(' indicates an initializer. */
10082 is_initialized = (token->type == CPP_EQ
10083 || token->type == CPP_OPEN_PAREN);
10084 /* If the init-declarator isn't initialized and isn't followed by a
10085 `,' or `;', it's not a valid init-declarator. */
10086 if (!is_initialized
10087 && token->type != CPP_COMMA
10088 && token->type != CPP_SEMICOLON)
10090 cp_parser_error (parser, "expected initializer");
10091 return error_mark_node;
10094 /* Because start_decl has side-effects, we should only call it if we
10095 know we're going ahead. By this point, we know that we cannot
10096 possibly be looking at any other construct. */
10097 cp_parser_commit_to_tentative_parse (parser);
10099 /* If the decl specifiers were bad, issue an error now that we're
10100 sure this was intended to be a declarator. Then continue
10101 declaring the variable(s), as int, to try to cut down on further
10103 if (decl_specifiers != NULL
10104 && TREE_VALUE (decl_specifiers) == error_mark_node)
10106 cp_parser_error (parser, "invalid type in declaration");
10107 TREE_VALUE (decl_specifiers) = integer_type_node;
10110 /* Check to see whether or not this declaration is a friend. */
10111 friend_p = cp_parser_friend_p (decl_specifiers);
10113 /* Check that the number of template-parameter-lists is OK. */
10114 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10115 return error_mark_node;
10117 /* Enter the newly declared entry in the symbol table. If we're
10118 processing a declaration in a class-specifier, we wait until
10119 after processing the initializer. */
10122 if (parser->in_unbraced_linkage_specification_p)
10124 decl_specifiers = tree_cons (error_mark_node,
10125 get_identifier ("extern"),
10127 have_extern_spec = false;
10129 decl = start_decl (declarator, decl_specifiers,
10130 is_initialized, attributes, prefix_attributes);
10133 /* Enter the SCOPE. That way unqualified names appearing in the
10134 initializer will be looked up in SCOPE. */
10136 pop_p = push_scope (scope);
10138 /* Perform deferred access control checks, now that we know in which
10139 SCOPE the declared entity resides. */
10140 if (!member_p && decl)
10142 tree saved_current_function_decl = NULL_TREE;
10144 /* If the entity being declared is a function, pretend that we
10145 are in its scope. If it is a `friend', it may have access to
10146 things that would not otherwise be accessible. */
10147 if (TREE_CODE (decl) == FUNCTION_DECL)
10149 saved_current_function_decl = current_function_decl;
10150 current_function_decl = decl;
10153 /* Perform the access control checks for the declarator and the
10154 the decl-specifiers. */
10155 perform_deferred_access_checks ();
10157 /* Restore the saved value. */
10158 if (TREE_CODE (decl) == FUNCTION_DECL)
10159 current_function_decl = saved_current_function_decl;
10162 /* Parse the initializer. */
10163 if (is_initialized)
10164 initializer = cp_parser_initializer (parser,
10165 &is_parenthesized_init,
10166 &is_non_constant_init);
10169 initializer = NULL_TREE;
10170 is_parenthesized_init = false;
10171 is_non_constant_init = true;
10174 /* The old parser allows attributes to appear after a parenthesized
10175 initializer. Mark Mitchell proposed removing this functionality
10176 on the GCC mailing lists on 2002-08-13. This parser accepts the
10177 attributes -- but ignores them. */
10178 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10179 if (cp_parser_attributes_opt (parser))
10180 warning ("attributes after parenthesized initializer ignored");
10182 /* Leave the SCOPE, now that we have processed the initializer. It
10183 is important to do this before calling cp_finish_decl because it
10184 makes decisions about whether to create DECL_STMTs or not based
10185 on the current scope. */
10189 /* For an in-class declaration, use `grokfield' to create the
10193 decl = grokfield (declarator, decl_specifiers,
10194 initializer, /*asmspec=*/NULL_TREE,
10195 /*attributes=*/NULL_TREE);
10196 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10197 cp_parser_save_default_args (parser, decl);
10200 /* Finish processing the declaration. But, skip friend
10202 if (!friend_p && decl)
10203 cp_finish_decl (decl,
10206 /* If the initializer is in parentheses, then this is
10207 a direct-initialization, which means that an
10208 `explicit' constructor is OK. Otherwise, an
10209 `explicit' constructor cannot be used. */
10210 ((is_parenthesized_init || !is_initialized)
10211 ? 0 : LOOKUP_ONLYCONVERTING));
10213 /* Remember whether or not variables were initialized by
10214 constant-expressions. */
10215 if (decl && TREE_CODE (decl) == VAR_DECL
10216 && is_initialized && !is_non_constant_init)
10217 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10222 /* Parse a declarator.
10226 ptr-operator declarator
10228 abstract-declarator:
10229 ptr-operator abstract-declarator [opt]
10230 direct-abstract-declarator
10235 attributes [opt] direct-declarator
10236 attributes [opt] ptr-operator declarator
10238 abstract-declarator:
10239 attributes [opt] ptr-operator abstract-declarator [opt]
10240 attributes [opt] direct-abstract-declarator
10242 Returns a representation of the declarator. If the declarator has
10243 the form `* declarator', then an INDIRECT_REF is returned, whose
10244 only operand is the sub-declarator. Analogously, `& declarator' is
10245 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10246 used. The first operand is the TYPE for `X'. The second operand
10247 is an INDIRECT_REF whose operand is the sub-declarator.
10249 Otherwise, the representation is as for a direct-declarator.
10251 (It would be better to define a structure type to represent
10252 declarators, rather than abusing `tree' nodes to represent
10253 declarators. That would be much clearer and save some memory.
10254 There is no reason for declarators to be garbage-collected, for
10255 example; they are created during parser and no longer needed after
10256 `grokdeclarator' has been called.)
10258 For a ptr-operator that has the optional cv-qualifier-seq,
10259 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10262 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10263 detect constructor, destructor or conversion operators. It is set
10264 to -1 if the declarator is a name, and +1 if it is a
10265 function. Otherwise it is set to zero. Usually you just want to
10266 test for >0, but internally the negative value is used.
10268 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10269 a decl-specifier-seq unless it declares a constructor, destructor,
10270 or conversion. It might seem that we could check this condition in
10271 semantic analysis, rather than parsing, but that makes it difficult
10272 to handle something like `f()'. We want to notice that there are
10273 no decl-specifiers, and therefore realize that this is an
10274 expression, not a declaration.)
10276 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10277 the declarator is a direct-declarator of the form "(...)".
10279 MEMBER_P is true iff this declarator is a member-declarator. */
10282 cp_parser_declarator (cp_parser* parser,
10283 cp_parser_declarator_kind dcl_kind,
10284 int* ctor_dtor_or_conv_p,
10285 bool* parenthesized_p,
10290 enum tree_code code;
10291 tree cv_qualifier_seq;
10293 tree attributes = NULL_TREE;
10295 /* Assume this is not a constructor, destructor, or type-conversion
10297 if (ctor_dtor_or_conv_p)
10298 *ctor_dtor_or_conv_p = 0;
10300 if (cp_parser_allow_gnu_extensions_p (parser))
10301 attributes = cp_parser_attributes_opt (parser);
10303 /* Peek at the next token. */
10304 token = cp_lexer_peek_token (parser->lexer);
10306 /* Check for the ptr-operator production. */
10307 cp_parser_parse_tentatively (parser);
10308 /* Parse the ptr-operator. */
10309 code = cp_parser_ptr_operator (parser,
10311 &cv_qualifier_seq);
10312 /* If that worked, then we have a ptr-operator. */
10313 if (cp_parser_parse_definitely (parser))
10315 /* If a ptr-operator was found, then this declarator was not
10317 if (parenthesized_p)
10318 *parenthesized_p = true;
10319 /* The dependent declarator is optional if we are parsing an
10320 abstract-declarator. */
10321 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10322 cp_parser_parse_tentatively (parser);
10324 /* Parse the dependent declarator. */
10325 declarator = cp_parser_declarator (parser, dcl_kind,
10326 /*ctor_dtor_or_conv_p=*/NULL,
10327 /*parenthesized_p=*/NULL,
10328 /*member_p=*/false);
10330 /* If we are parsing an abstract-declarator, we must handle the
10331 case where the dependent declarator is absent. */
10332 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10333 && !cp_parser_parse_definitely (parser))
10334 declarator = NULL_TREE;
10336 /* Build the representation of the ptr-operator. */
10337 if (code == INDIRECT_REF)
10338 declarator = make_pointer_declarator (cv_qualifier_seq,
10341 declarator = make_reference_declarator (cv_qualifier_seq,
10343 /* Handle the pointer-to-member case. */
10345 declarator = build_nt (SCOPE_REF, class_type, declarator);
10347 /* Everything else is a direct-declarator. */
10350 if (parenthesized_p)
10351 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10353 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10354 ctor_dtor_or_conv_p,
10358 if (attributes && declarator != error_mark_node)
10359 declarator = tree_cons (attributes, declarator, NULL_TREE);
10364 /* Parse a direct-declarator or direct-abstract-declarator.
10368 direct-declarator ( parameter-declaration-clause )
10369 cv-qualifier-seq [opt]
10370 exception-specification [opt]
10371 direct-declarator [ constant-expression [opt] ]
10374 direct-abstract-declarator:
10375 direct-abstract-declarator [opt]
10376 ( parameter-declaration-clause )
10377 cv-qualifier-seq [opt]
10378 exception-specification [opt]
10379 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10380 ( abstract-declarator )
10382 Returns a representation of the declarator. DCL_KIND is
10383 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10384 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10385 we are parsing a direct-declarator. It is
10386 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10387 of ambiguity we prefer an abstract declarator, as per
10388 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10389 cp_parser_declarator.
10391 For the declarator-id production, the representation is as for an
10392 id-expression, except that a qualified name is represented as a
10393 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10394 see the documentation of the FUNCTION_DECLARATOR_* macros for
10395 information about how to find the various declarator components.
10396 An array-declarator is represented as an ARRAY_REF. The
10397 direct-declarator is the first operand; the constant-expression
10398 indicating the size of the array is the second operand. */
10401 cp_parser_direct_declarator (cp_parser* parser,
10402 cp_parser_declarator_kind dcl_kind,
10403 int* ctor_dtor_or_conv_p,
10407 tree declarator = NULL_TREE;
10408 tree scope = NULL_TREE;
10409 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10410 bool saved_in_declarator_p = parser->in_declarator_p;
10412 bool pop_p = false;
10416 /* Peek at the next token. */
10417 token = cp_lexer_peek_token (parser->lexer);
10418 if (token->type == CPP_OPEN_PAREN)
10420 /* This is either a parameter-declaration-clause, or a
10421 parenthesized declarator. When we know we are parsing a
10422 named declarator, it must be a parenthesized declarator
10423 if FIRST is true. For instance, `(int)' is a
10424 parameter-declaration-clause, with an omitted
10425 direct-abstract-declarator. But `((*))', is a
10426 parenthesized abstract declarator. Finally, when T is a
10427 template parameter `(T)' is a
10428 parameter-declaration-clause, and not a parenthesized
10431 We first try and parse a parameter-declaration-clause,
10432 and then try a nested declarator (if FIRST is true).
10434 It is not an error for it not to be a
10435 parameter-declaration-clause, even when FIRST is
10441 The first is the declaration of a function while the
10442 second is a the definition of a variable, including its
10445 Having seen only the parenthesis, we cannot know which of
10446 these two alternatives should be selected. Even more
10447 complex are examples like:
10452 The former is a function-declaration; the latter is a
10453 variable initialization.
10455 Thus again, we try a parameter-declaration-clause, and if
10456 that fails, we back out and return. */
10458 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10461 unsigned saved_num_template_parameter_lists;
10463 /* In a member-declarator, the only valid interpretation
10464 of a parenthesis is the start of a
10465 parameter-declaration-clause. (It is invalid to
10466 initialize a static data member with a parenthesized
10467 initializer; only the "=" form of initialization is
10470 cp_parser_parse_tentatively (parser);
10472 /* Consume the `('. */
10473 cp_lexer_consume_token (parser->lexer);
10476 /* If this is going to be an abstract declarator, we're
10477 in a declarator and we can't have default args. */
10478 parser->default_arg_ok_p = false;
10479 parser->in_declarator_p = true;
10482 /* Inside the function parameter list, surrounding
10483 template-parameter-lists do not apply. */
10484 saved_num_template_parameter_lists
10485 = parser->num_template_parameter_lists;
10486 parser->num_template_parameter_lists = 0;
10488 /* Parse the parameter-declaration-clause. */
10489 params = cp_parser_parameter_declaration_clause (parser);
10491 parser->num_template_parameter_lists
10492 = saved_num_template_parameter_lists;
10494 /* If all went well, parse the cv-qualifier-seq and the
10495 exception-specification. */
10496 if (member_p || cp_parser_parse_definitely (parser))
10498 tree cv_qualifiers;
10499 tree exception_specification;
10501 if (ctor_dtor_or_conv_p)
10502 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10504 /* Consume the `)'. */
10505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10507 /* Parse the cv-qualifier-seq. */
10508 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10509 /* And the exception-specification. */
10510 exception_specification
10511 = cp_parser_exception_specification_opt (parser);
10513 /* Create the function-declarator. */
10514 declarator = make_call_declarator (declarator,
10517 exception_specification);
10518 /* Any subsequent parameter lists are to do with
10519 return type, so are not those of the declared
10521 parser->default_arg_ok_p = false;
10523 /* Repeat the main loop. */
10528 /* If this is the first, we can try a parenthesized
10532 bool saved_in_type_id_in_expr_p;
10534 parser->default_arg_ok_p = saved_default_arg_ok_p;
10535 parser->in_declarator_p = saved_in_declarator_p;
10537 /* Consume the `('. */
10538 cp_lexer_consume_token (parser->lexer);
10539 /* Parse the nested declarator. */
10540 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10541 parser->in_type_id_in_expr_p = true;
10543 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10544 /*parenthesized_p=*/NULL,
10546 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10548 /* Expect a `)'. */
10549 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10550 declarator = error_mark_node;
10551 if (declarator == error_mark_node)
10554 goto handle_declarator;
10556 /* Otherwise, we must be done. */
10560 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10561 && token->type == CPP_OPEN_SQUARE)
10563 /* Parse an array-declarator. */
10566 if (ctor_dtor_or_conv_p)
10567 *ctor_dtor_or_conv_p = 0;
10570 parser->default_arg_ok_p = false;
10571 parser->in_declarator_p = true;
10572 /* Consume the `['. */
10573 cp_lexer_consume_token (parser->lexer);
10574 /* Peek at the next token. */
10575 token = cp_lexer_peek_token (parser->lexer);
10576 /* If the next token is `]', then there is no
10577 constant-expression. */
10578 if (token->type != CPP_CLOSE_SQUARE)
10580 bool non_constant_p;
10583 = cp_parser_constant_expression (parser,
10584 /*allow_non_constant=*/true,
10586 if (!non_constant_p)
10587 bounds = fold_non_dependent_expr (bounds);
10590 bounds = NULL_TREE;
10591 /* Look for the closing `]'. */
10592 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10594 declarator = error_mark_node;
10598 declarator = build_nt (ARRAY_REF, declarator, bounds);
10600 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10602 /* Parse a declarator-id */
10603 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10604 cp_parser_parse_tentatively (parser);
10605 declarator = cp_parser_declarator_id (parser);
10606 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10608 if (!cp_parser_parse_definitely (parser))
10609 declarator = error_mark_node;
10610 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10612 cp_parser_error (parser, "expected unqualified-id");
10613 declarator = error_mark_node;
10617 if (declarator == error_mark_node)
10620 if (TREE_CODE (declarator) == SCOPE_REF
10621 && !current_scope ())
10623 tree scope = TREE_OPERAND (declarator, 0);
10625 /* In the declaration of a member of a template class
10626 outside of the class itself, the SCOPE will sometimes
10627 be a TYPENAME_TYPE. For example, given:
10629 template <typename T>
10630 int S<T>::R::i = 3;
10632 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10633 this context, we must resolve S<T>::R to an ordinary
10634 type, rather than a typename type.
10636 The reason we normally avoid resolving TYPENAME_TYPEs
10637 is that a specialization of `S' might render
10638 `S<T>::R' not a type. However, if `S' is
10639 specialized, then this `i' will not be used, so there
10640 is no harm in resolving the types here. */
10641 if (TREE_CODE (scope) == TYPENAME_TYPE)
10645 /* Resolve the TYPENAME_TYPE. */
10646 type = resolve_typename_type (scope,
10647 /*only_current_p=*/false);
10648 /* If that failed, the declarator is invalid. */
10649 if (type == error_mark_node)
10650 error ("`%T::%D' is not a type",
10651 TYPE_CONTEXT (scope),
10652 TYPE_IDENTIFIER (scope));
10653 /* Build a new DECLARATOR. */
10654 declarator = build_nt (SCOPE_REF,
10656 TREE_OPERAND (declarator, 1));
10660 /* Check to see whether the declarator-id names a constructor,
10661 destructor, or conversion. */
10662 if (declarator && ctor_dtor_or_conv_p
10663 && ((TREE_CODE (declarator) == SCOPE_REF
10664 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10665 || (TREE_CODE (declarator) != SCOPE_REF
10666 && at_class_scope_p ())))
10668 tree unqualified_name;
10671 /* Get the unqualified part of the name. */
10672 if (TREE_CODE (declarator) == SCOPE_REF)
10674 class_type = TREE_OPERAND (declarator, 0);
10675 unqualified_name = TREE_OPERAND (declarator, 1);
10679 class_type = current_class_type;
10680 unqualified_name = declarator;
10683 /* See if it names ctor, dtor or conv. */
10684 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10685 || IDENTIFIER_TYPENAME_P (unqualified_name)
10686 || constructor_name_p (unqualified_name, class_type)
10687 || (TREE_CODE (unqualified_name) == TYPE_DECL
10688 && same_type_p (TREE_TYPE (unqualified_name),
10690 *ctor_dtor_or_conv_p = -1;
10693 handle_declarator:;
10694 scope = get_scope_of_declarator (declarator);
10696 /* Any names that appear after the declarator-id for a
10697 member are looked up in the containing scope. */
10698 pop_p = push_scope (scope);
10699 parser->in_declarator_p = true;
10700 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10702 && (TREE_CODE (declarator) == SCOPE_REF
10703 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10704 /* Default args are only allowed on function
10706 parser->default_arg_ok_p = saved_default_arg_ok_p;
10708 parser->default_arg_ok_p = false;
10717 /* For an abstract declarator, we might wind up with nothing at this
10718 point. That's an error; the declarator is not optional. */
10720 cp_parser_error (parser, "expected declarator");
10722 /* If we entered a scope, we must exit it now. */
10726 parser->default_arg_ok_p = saved_default_arg_ok_p;
10727 parser->in_declarator_p = saved_in_declarator_p;
10732 /* Parse a ptr-operator.
10735 * cv-qualifier-seq [opt]
10737 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10742 & cv-qualifier-seq [opt]
10744 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10745 used. Returns ADDR_EXPR if a reference was used. In the
10746 case of a pointer-to-member, *TYPE is filled in with the
10747 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10748 with the cv-qualifier-seq, or NULL_TREE, if there are no
10749 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10751 static enum tree_code
10752 cp_parser_ptr_operator (cp_parser* parser,
10754 tree* cv_qualifier_seq)
10756 enum tree_code code = ERROR_MARK;
10759 /* Assume that it's not a pointer-to-member. */
10761 /* And that there are no cv-qualifiers. */
10762 *cv_qualifier_seq = NULL_TREE;
10764 /* Peek at the next token. */
10765 token = cp_lexer_peek_token (parser->lexer);
10766 /* If it's a `*' or `&' we have a pointer or reference. */
10767 if (token->type == CPP_MULT || token->type == CPP_AND)
10769 /* Remember which ptr-operator we were processing. */
10770 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10772 /* Consume the `*' or `&'. */
10773 cp_lexer_consume_token (parser->lexer);
10775 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10776 `&', if we are allowing GNU extensions. (The only qualifier
10777 that can legally appear after `&' is `restrict', but that is
10778 enforced during semantic analysis. */
10779 if (code == INDIRECT_REF
10780 || cp_parser_allow_gnu_extensions_p (parser))
10781 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10785 /* Try the pointer-to-member case. */
10786 cp_parser_parse_tentatively (parser);
10787 /* Look for the optional `::' operator. */
10788 cp_parser_global_scope_opt (parser,
10789 /*current_scope_valid_p=*/false);
10790 /* Look for the nested-name specifier. */
10791 cp_parser_nested_name_specifier (parser,
10792 /*typename_keyword_p=*/false,
10793 /*check_dependency_p=*/true,
10795 /*is_declaration=*/false);
10796 /* If we found it, and the next token is a `*', then we are
10797 indeed looking at a pointer-to-member operator. */
10798 if (!cp_parser_error_occurred (parser)
10799 && cp_parser_require (parser, CPP_MULT, "`*'"))
10801 /* The type of which the member is a member is given by the
10803 *type = parser->scope;
10804 /* The next name will not be qualified. */
10805 parser->scope = NULL_TREE;
10806 parser->qualifying_scope = NULL_TREE;
10807 parser->object_scope = NULL_TREE;
10808 /* Indicate that the `*' operator was used. */
10809 code = INDIRECT_REF;
10810 /* Look for the optional cv-qualifier-seq. */
10811 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10813 /* If that didn't work we don't have a ptr-operator. */
10814 if (!cp_parser_parse_definitely (parser))
10815 cp_parser_error (parser, "expected ptr-operator");
10821 /* Parse an (optional) cv-qualifier-seq.
10824 cv-qualifier cv-qualifier-seq [opt]
10826 Returns a TREE_LIST. The TREE_VALUE of each node is the
10827 representation of a cv-qualifier. */
10830 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10832 tree cv_qualifiers = NULL_TREE;
10838 /* Look for the next cv-qualifier. */
10839 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10840 /* If we didn't find one, we're done. */
10844 /* Add this cv-qualifier to the list. */
10846 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10849 /* We built up the list in reverse order. */
10850 return nreverse (cv_qualifiers);
10853 /* Parse an (optional) cv-qualifier.
10865 cp_parser_cv_qualifier_opt (cp_parser* parser)
10868 tree cv_qualifier = NULL_TREE;
10870 /* Peek at the next token. */
10871 token = cp_lexer_peek_token (parser->lexer);
10872 /* See if it's a cv-qualifier. */
10873 switch (token->keyword)
10878 /* Save the value of the token. */
10879 cv_qualifier = token->value;
10880 /* Consume the token. */
10881 cp_lexer_consume_token (parser->lexer);
10888 return cv_qualifier;
10891 /* Parse a declarator-id.
10895 :: [opt] nested-name-specifier [opt] type-name
10897 In the `id-expression' case, the value returned is as for
10898 cp_parser_id_expression if the id-expression was an unqualified-id.
10899 If the id-expression was a qualified-id, then a SCOPE_REF is
10900 returned. The first operand is the scope (either a NAMESPACE_DECL
10901 or TREE_TYPE), but the second is still just a representation of an
10905 cp_parser_declarator_id (cp_parser* parser)
10907 tree id_expression;
10909 /* The expression must be an id-expression. Assume that qualified
10910 names are the names of types so that:
10913 int S<T>::R::i = 3;
10915 will work; we must treat `S<T>::R' as the name of a type.
10916 Similarly, assume that qualified names are templates, where
10920 int S<T>::R<T>::i = 3;
10923 id_expression = cp_parser_id_expression (parser,
10924 /*template_keyword_p=*/false,
10925 /*check_dependency_p=*/false,
10926 /*template_p=*/NULL,
10927 /*declarator_p=*/true);
10928 /* If the name was qualified, create a SCOPE_REF to represent
10932 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10933 parser->scope = NULL_TREE;
10936 return id_expression;
10939 /* Parse a type-id.
10942 type-specifier-seq abstract-declarator [opt]
10944 Returns the TYPE specified. */
10947 cp_parser_type_id (cp_parser* parser)
10949 tree type_specifier_seq;
10950 tree abstract_declarator;
10952 /* Parse the type-specifier-seq. */
10954 = cp_parser_type_specifier_seq (parser);
10955 if (type_specifier_seq == error_mark_node)
10956 return error_mark_node;
10958 /* There might or might not be an abstract declarator. */
10959 cp_parser_parse_tentatively (parser);
10960 /* Look for the declarator. */
10961 abstract_declarator
10962 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10963 /*parenthesized_p=*/NULL,
10964 /*member_p=*/false);
10965 /* Check to see if there really was a declarator. */
10966 if (!cp_parser_parse_definitely (parser))
10967 abstract_declarator = NULL_TREE;
10969 return groktypename (build_tree_list (type_specifier_seq,
10970 abstract_declarator));
10973 /* Parse a type-specifier-seq.
10975 type-specifier-seq:
10976 type-specifier type-specifier-seq [opt]
10980 type-specifier-seq:
10981 attributes type-specifier-seq [opt]
10983 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10984 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10987 cp_parser_type_specifier_seq (cp_parser* parser)
10989 bool seen_type_specifier = false;
10990 tree type_specifier_seq = NULL_TREE;
10992 /* Parse the type-specifiers and attributes. */
10995 tree type_specifier;
10997 /* Check for attributes first. */
10998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11000 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
11002 type_specifier_seq);
11006 /* After the first type-specifier, others are optional. */
11007 if (seen_type_specifier)
11008 cp_parser_parse_tentatively (parser);
11009 /* Look for the type-specifier. */
11010 type_specifier = cp_parser_type_specifier (parser,
11011 CP_PARSER_FLAGS_NONE,
11012 /*is_friend=*/false,
11013 /*is_declaration=*/false,
11016 /* If the first type-specifier could not be found, this is not a
11017 type-specifier-seq at all. */
11018 if (!seen_type_specifier && type_specifier == error_mark_node)
11019 return error_mark_node;
11020 /* If subsequent type-specifiers could not be found, the
11021 type-specifier-seq is complete. */
11022 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11025 /* Add the new type-specifier to the list. */
11027 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11028 seen_type_specifier = true;
11031 /* We built up the list in reverse order. */
11032 return nreverse (type_specifier_seq);
11035 /* Parse a parameter-declaration-clause.
11037 parameter-declaration-clause:
11038 parameter-declaration-list [opt] ... [opt]
11039 parameter-declaration-list , ...
11041 Returns a representation for the parameter declarations. Each node
11042 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11043 representation.) If the parameter-declaration-clause ends with an
11044 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11045 list. A return value of NULL_TREE indicates a
11046 parameter-declaration-clause consisting only of an ellipsis. */
11049 cp_parser_parameter_declaration_clause (cp_parser* parser)
11055 /* Peek at the next token. */
11056 token = cp_lexer_peek_token (parser->lexer);
11057 /* Check for trivial parameter-declaration-clauses. */
11058 if (token->type == CPP_ELLIPSIS)
11060 /* Consume the `...' token. */
11061 cp_lexer_consume_token (parser->lexer);
11064 else if (token->type == CPP_CLOSE_PAREN)
11065 /* There are no parameters. */
11067 #ifndef NO_IMPLICIT_EXTERN_C
11068 if (in_system_header && current_class_type == NULL
11069 && current_lang_name == lang_name_c)
11073 return void_list_node;
11075 /* Check for `(void)', too, which is a special case. */
11076 else if (token->keyword == RID_VOID
11077 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11078 == CPP_CLOSE_PAREN))
11080 /* Consume the `void' token. */
11081 cp_lexer_consume_token (parser->lexer);
11082 /* There are no parameters. */
11083 return void_list_node;
11086 /* Parse the parameter-declaration-list. */
11087 parameters = cp_parser_parameter_declaration_list (parser);
11088 /* If a parse error occurred while parsing the
11089 parameter-declaration-list, then the entire
11090 parameter-declaration-clause is erroneous. */
11091 if (parameters == error_mark_node)
11092 return error_mark_node;
11094 /* Peek at the next token. */
11095 token = cp_lexer_peek_token (parser->lexer);
11096 /* If it's a `,', the clause should terminate with an ellipsis. */
11097 if (token->type == CPP_COMMA)
11099 /* Consume the `,'. */
11100 cp_lexer_consume_token (parser->lexer);
11101 /* Expect an ellipsis. */
11103 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11105 /* It might also be `...' if the optional trailing `,' was
11107 else if (token->type == CPP_ELLIPSIS)
11109 /* Consume the `...' token. */
11110 cp_lexer_consume_token (parser->lexer);
11111 /* And remember that we saw it. */
11115 ellipsis_p = false;
11117 /* Finish the parameter list. */
11118 return finish_parmlist (parameters, ellipsis_p);
11121 /* Parse a parameter-declaration-list.
11123 parameter-declaration-list:
11124 parameter-declaration
11125 parameter-declaration-list , parameter-declaration
11127 Returns a representation of the parameter-declaration-list, as for
11128 cp_parser_parameter_declaration_clause. However, the
11129 `void_list_node' is never appended to the list. */
11132 cp_parser_parameter_declaration_list (cp_parser* parser)
11134 tree parameters = NULL_TREE;
11136 /* Look for more parameters. */
11140 bool parenthesized_p;
11141 /* Parse the parameter. */
11143 = cp_parser_parameter_declaration (parser,
11144 /*template_parm_p=*/false,
11147 /* If a parse error occurred parsing the parameter declaration,
11148 then the entire parameter-declaration-list is erroneous. */
11149 if (parameter == error_mark_node)
11151 parameters = error_mark_node;
11154 /* Add the new parameter to the list. */
11155 TREE_CHAIN (parameter) = parameters;
11156 parameters = parameter;
11158 /* Peek at the next token. */
11159 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11160 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11161 /* The parameter-declaration-list is complete. */
11163 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11167 /* Peek at the next token. */
11168 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11169 /* If it's an ellipsis, then the list is complete. */
11170 if (token->type == CPP_ELLIPSIS)
11172 /* Otherwise, there must be more parameters. Consume the
11174 cp_lexer_consume_token (parser->lexer);
11175 /* When parsing something like:
11177 int i(float f, double d)
11179 we can tell after seeing the declaration for "f" that we
11180 are not looking at an initialization of a variable "i",
11181 but rather at the declaration of a function "i".
11183 Due to the fact that the parsing of template arguments
11184 (as specified to a template-id) requires backtracking we
11185 cannot use this technique when inside a template argument
11187 if (!parser->in_template_argument_list_p
11188 && !parser->in_type_id_in_expr_p
11189 && cp_parser_parsing_tentatively (parser)
11190 && !cp_parser_committed_to_tentative_parse (parser)
11191 /* However, a parameter-declaration of the form
11192 "foat(f)" (which is a valid declaration of a
11193 parameter "f") can also be interpreted as an
11194 expression (the conversion of "f" to "float"). */
11195 && !parenthesized_p)
11196 cp_parser_commit_to_tentative_parse (parser);
11200 cp_parser_error (parser, "expected `,' or `...'");
11201 if (!cp_parser_parsing_tentatively (parser)
11202 || cp_parser_committed_to_tentative_parse (parser))
11203 cp_parser_skip_to_closing_parenthesis (parser,
11204 /*recovering=*/true,
11205 /*or_comma=*/false,
11206 /*consume_paren=*/false);
11211 /* We built up the list in reverse order; straighten it out now. */
11212 return nreverse (parameters);
11215 /* Parse a parameter declaration.
11217 parameter-declaration:
11218 decl-specifier-seq declarator
11219 decl-specifier-seq declarator = assignment-expression
11220 decl-specifier-seq abstract-declarator [opt]
11221 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11223 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11224 declares a template parameter. (In that case, a non-nested `>'
11225 token encountered during the parsing of the assignment-expression
11226 is not interpreted as a greater-than operator.)
11228 Returns a TREE_LIST representing the parameter-declaration. The
11229 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11230 there is no default argument. The TREE_VALUE is a representation
11231 of the decl-specifier-seq and declarator. In particular, the
11232 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11233 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11234 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11235 the declarator is of the form "(p)". */
11238 cp_parser_parameter_declaration (cp_parser *parser,
11239 bool template_parm_p,
11240 bool *parenthesized_p)
11242 int declares_class_or_enum;
11243 bool greater_than_is_operator_p;
11244 tree decl_specifiers;
11247 tree default_argument;
11250 const char *saved_message;
11252 /* In a template parameter, `>' is not an operator.
11256 When parsing a default template-argument for a non-type
11257 template-parameter, the first non-nested `>' is taken as the end
11258 of the template parameter-list rather than a greater-than
11260 greater_than_is_operator_p = !template_parm_p;
11262 /* Type definitions may not appear in parameter types. */
11263 saved_message = parser->type_definition_forbidden_message;
11264 parser->type_definition_forbidden_message
11265 = "types may not be defined in parameter types";
11267 /* Parse the declaration-specifiers. */
11269 = cp_parser_decl_specifier_seq (parser,
11270 CP_PARSER_FLAGS_NONE,
11272 &declares_class_or_enum);
11273 /* If an error occurred, there's no reason to attempt to parse the
11274 rest of the declaration. */
11275 if (cp_parser_error_occurred (parser))
11277 parser->type_definition_forbidden_message = saved_message;
11278 return error_mark_node;
11281 /* Peek at the next token. */
11282 token = cp_lexer_peek_token (parser->lexer);
11283 /* If the next token is a `)', `,', `=', `>', or `...', then there
11284 is no declarator. */
11285 if (token->type == CPP_CLOSE_PAREN
11286 || token->type == CPP_COMMA
11287 || token->type == CPP_EQ
11288 || token->type == CPP_ELLIPSIS
11289 || token->type == CPP_GREATER)
11291 declarator = NULL_TREE;
11292 if (parenthesized_p)
11293 *parenthesized_p = false;
11295 /* Otherwise, there should be a declarator. */
11298 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11299 parser->default_arg_ok_p = false;
11301 /* After seeing a decl-specifier-seq, if the next token is not a
11302 "(", there is no possibility that the code is a valid
11303 expression. Therefore, if parsing tentatively, we commit at
11305 if (!parser->in_template_argument_list_p
11306 /* In an expression context, having seen:
11310 we cannot be sure whether we are looking at a
11311 function-type (taking a "char" as a parameter) or a cast
11312 of some object of type "char" to "int". */
11313 && !parser->in_type_id_in_expr_p
11314 && cp_parser_parsing_tentatively (parser)
11315 && !cp_parser_committed_to_tentative_parse (parser)
11316 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11317 cp_parser_commit_to_tentative_parse (parser);
11318 /* Parse the declarator. */
11319 declarator = cp_parser_declarator (parser,
11320 CP_PARSER_DECLARATOR_EITHER,
11321 /*ctor_dtor_or_conv_p=*/NULL,
11323 /*member_p=*/false);
11324 parser->default_arg_ok_p = saved_default_arg_ok_p;
11325 /* After the declarator, allow more attributes. */
11326 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11329 /* The restriction on defining new types applies only to the type
11330 of the parameter, not to the default argument. */
11331 parser->type_definition_forbidden_message = saved_message;
11333 /* If the next token is `=', then process a default argument. */
11334 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11336 bool saved_greater_than_is_operator_p;
11337 /* Consume the `='. */
11338 cp_lexer_consume_token (parser->lexer);
11340 /* If we are defining a class, then the tokens that make up the
11341 default argument must be saved and processed later. */
11342 if (!template_parm_p && at_class_scope_p ()
11343 && TYPE_BEING_DEFINED (current_class_type))
11345 unsigned depth = 0;
11347 /* Create a DEFAULT_ARG to represented the unparsed default
11349 default_argument = make_node (DEFAULT_ARG);
11350 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11352 /* Add tokens until we have processed the entire default
11359 /* Peek at the next token. */
11360 token = cp_lexer_peek_token (parser->lexer);
11361 /* What we do depends on what token we have. */
11362 switch (token->type)
11364 /* In valid code, a default argument must be
11365 immediately followed by a `,' `)', or `...'. */
11367 case CPP_CLOSE_PAREN:
11369 /* If we run into a non-nested `;', `}', or `]',
11370 then the code is invalid -- but the default
11371 argument is certainly over. */
11372 case CPP_SEMICOLON:
11373 case CPP_CLOSE_BRACE:
11374 case CPP_CLOSE_SQUARE:
11377 /* Update DEPTH, if necessary. */
11378 else if (token->type == CPP_CLOSE_PAREN
11379 || token->type == CPP_CLOSE_BRACE
11380 || token->type == CPP_CLOSE_SQUARE)
11384 case CPP_OPEN_PAREN:
11385 case CPP_OPEN_SQUARE:
11386 case CPP_OPEN_BRACE:
11391 /* If we see a non-nested `>', and `>' is not an
11392 operator, then it marks the end of the default
11394 if (!depth && !greater_than_is_operator_p)
11398 /* If we run out of tokens, issue an error message. */
11400 error ("file ends in default argument");
11406 /* In these cases, we should look for template-ids.
11407 For example, if the default argument is
11408 `X<int, double>()', we need to do name lookup to
11409 figure out whether or not `X' is a template; if
11410 so, the `,' does not end the default argument.
11412 That is not yet done. */
11419 /* If we've reached the end, stop. */
11423 /* Add the token to the token block. */
11424 token = cp_lexer_consume_token (parser->lexer);
11425 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11429 /* Outside of a class definition, we can just parse the
11430 assignment-expression. */
11433 bool saved_local_variables_forbidden_p;
11435 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11437 saved_greater_than_is_operator_p
11438 = parser->greater_than_is_operator_p;
11439 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11440 /* Local variable names (and the `this' keyword) may not
11441 appear in a default argument. */
11442 saved_local_variables_forbidden_p
11443 = parser->local_variables_forbidden_p;
11444 parser->local_variables_forbidden_p = true;
11445 /* Parse the assignment-expression. */
11446 default_argument = cp_parser_assignment_expression (parser);
11447 /* Restore saved state. */
11448 parser->greater_than_is_operator_p
11449 = saved_greater_than_is_operator_p;
11450 parser->local_variables_forbidden_p
11451 = saved_local_variables_forbidden_p;
11453 if (!parser->default_arg_ok_p)
11455 if (!flag_pedantic_errors)
11456 warning ("deprecated use of default argument for parameter of non-function");
11459 error ("default arguments are only permitted for function parameters");
11460 default_argument = NULL_TREE;
11465 default_argument = NULL_TREE;
11467 /* Create the representation of the parameter. */
11469 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11470 parameter = build_tree_list (default_argument,
11471 build_tree_list (decl_specifiers,
11477 /* Parse a function-body.
11480 compound_statement */
11483 cp_parser_function_body (cp_parser *parser)
11485 cp_parser_compound_statement (parser, false);
11488 /* Parse a ctor-initializer-opt followed by a function-body. Return
11489 true if a ctor-initializer was present. */
11492 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11495 bool ctor_initializer_p;
11497 /* Begin the function body. */
11498 body = begin_function_body ();
11499 /* Parse the optional ctor-initializer. */
11500 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11501 /* Parse the function-body. */
11502 cp_parser_function_body (parser);
11503 /* Finish the function body. */
11504 finish_function_body (body);
11506 return ctor_initializer_p;
11509 /* Parse an initializer.
11512 = initializer-clause
11513 ( expression-list )
11515 Returns a expression representing the initializer. If no
11516 initializer is present, NULL_TREE is returned.
11518 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11519 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11520 set to FALSE if there is no initializer present. If there is an
11521 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11522 is set to true; otherwise it is set to false. */
11525 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11526 bool* non_constant_p)
11531 /* Peek at the next token. */
11532 token = cp_lexer_peek_token (parser->lexer);
11534 /* Let our caller know whether or not this initializer was
11536 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11537 /* Assume that the initializer is constant. */
11538 *non_constant_p = false;
11540 if (token->type == CPP_EQ)
11542 /* Consume the `='. */
11543 cp_lexer_consume_token (parser->lexer);
11544 /* Parse the initializer-clause. */
11545 init = cp_parser_initializer_clause (parser, non_constant_p);
11547 else if (token->type == CPP_OPEN_PAREN)
11548 init = cp_parser_parenthesized_expression_list (parser, false,
11552 /* Anything else is an error. */
11553 cp_parser_error (parser, "expected initializer");
11554 init = error_mark_node;
11560 /* Parse an initializer-clause.
11562 initializer-clause:
11563 assignment-expression
11564 { initializer-list , [opt] }
11567 Returns an expression representing the initializer.
11569 If the `assignment-expression' production is used the value
11570 returned is simply a representation for the expression.
11572 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11573 the elements of the initializer-list (or NULL_TREE, if the last
11574 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11575 NULL_TREE. There is no way to detect whether or not the optional
11576 trailing `,' was provided. NON_CONSTANT_P is as for
11577 cp_parser_initializer. */
11580 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11582 tree initializer = NULL_TREE;
11584 /* Assume the expression is constant. */
11585 *non_constant_p = false;
11587 /* If it is not a `{', then we are looking at an
11588 assignment-expression. */
11589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11591 /* Speed up common initializers (simply a literal). */
11592 cp_token* token = cp_lexer_peek_token (parser->lexer);
11593 cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11595 if (token2->type == CPP_COMMA)
11596 switch (token->type)
11601 token = cp_lexer_consume_token (parser->lexer);
11602 initializer = token->value;
11607 token = cp_lexer_consume_token (parser->lexer);
11608 if (TREE_CHAIN (token->value))
11609 initializer = TREE_CHAIN (token->value);
11611 initializer = token->value;
11618 /* Otherwise, fall back to the generic assignment expression. */
11622 = cp_parser_constant_expression (parser,
11623 /*allow_non_constant_p=*/true,
11625 if (!*non_constant_p)
11626 initializer = fold_non_dependent_expr (initializer);
11631 /* Consume the `{' token. */
11632 cp_lexer_consume_token (parser->lexer);
11633 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11634 initializer = make_node (CONSTRUCTOR);
11635 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11636 necessary, but check_initializer depends upon it, for
11638 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11639 /* If it's not a `}', then there is a non-trivial initializer. */
11640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11642 /* Parse the initializer list. */
11643 CONSTRUCTOR_ELTS (initializer)
11644 = cp_parser_initializer_list (parser, non_constant_p);
11645 /* A trailing `,' token is allowed. */
11646 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11647 cp_lexer_consume_token (parser->lexer);
11649 /* Now, there should be a trailing `}'. */
11650 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11653 return initializer;
11656 /* Parse an initializer-list.
11660 initializer-list , initializer-clause
11665 identifier : initializer-clause
11666 initializer-list, identifier : initializer-clause
11668 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11669 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11670 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11671 as for cp_parser_initializer. */
11674 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11676 tree initializers = NULL_TREE;
11678 /* Assume all of the expressions are constant. */
11679 *non_constant_p = false;
11681 /* Parse the rest of the list. */
11687 bool clause_non_constant_p;
11689 /* If the next token is an identifier and the following one is a
11690 colon, we are looking at the GNU designated-initializer
11692 if (cp_parser_allow_gnu_extensions_p (parser)
11693 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11694 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11696 /* Consume the identifier. */
11697 identifier = cp_lexer_consume_token (parser->lexer)->value;
11698 /* Consume the `:'. */
11699 cp_lexer_consume_token (parser->lexer);
11702 identifier = NULL_TREE;
11704 /* Parse the initializer. */
11705 initializer = cp_parser_initializer_clause (parser,
11706 &clause_non_constant_p);
11707 /* If any clause is non-constant, so is the entire initializer. */
11708 if (clause_non_constant_p)
11709 *non_constant_p = true;
11710 /* Add it to the list. */
11711 initializers = tree_cons (identifier, initializer, initializers);
11713 /* If the next token is not a comma, we have reached the end of
11715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11718 /* Peek at the next token. */
11719 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11720 /* If the next token is a `}', then we're still done. An
11721 initializer-clause can have a trailing `,' after the
11722 initializer-list and before the closing `}'. */
11723 if (token->type == CPP_CLOSE_BRACE)
11726 /* Consume the `,' token. */
11727 cp_lexer_consume_token (parser->lexer);
11730 /* The initializers were built up in reverse order, so we need to
11731 reverse them now. */
11732 return nreverse (initializers);
11735 /* Classes [gram.class] */
11737 /* Parse a class-name.
11743 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11744 to indicate that names looked up in dependent types should be
11745 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11746 keyword has been used to indicate that the name that appears next
11747 is a template. TYPE_P is true iff the next name should be treated
11748 as class-name, even if it is declared to be some other kind of name
11749 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11750 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11751 being defined in a class-head.
11753 Returns the TYPE_DECL representing the class. */
11756 cp_parser_class_name (cp_parser *parser,
11757 bool typename_keyword_p,
11758 bool template_keyword_p,
11760 bool check_dependency_p,
11762 bool is_declaration)
11769 /* All class-names start with an identifier. */
11770 token = cp_lexer_peek_token (parser->lexer);
11771 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11773 cp_parser_error (parser, "expected class-name");
11774 return error_mark_node;
11777 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11778 to a template-id, so we save it here. */
11779 scope = parser->scope;
11780 if (scope == error_mark_node)
11781 return error_mark_node;
11783 /* Any name names a type if we're following the `typename' keyword
11784 in a qualified name where the enclosing scope is type-dependent. */
11785 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11786 && dependent_type_p (scope));
11787 /* Handle the common case (an identifier, but not a template-id)
11789 if (token->type == CPP_NAME
11790 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11794 /* Look for the identifier. */
11795 identifier = cp_parser_identifier (parser);
11796 /* If the next token isn't an identifier, we are certainly not
11797 looking at a class-name. */
11798 if (identifier == error_mark_node)
11799 decl = error_mark_node;
11800 /* If we know this is a type-name, there's no need to look it
11802 else if (typename_p)
11806 /* If the next token is a `::', then the name must be a type
11809 [basic.lookup.qual]
11811 During the lookup for a name preceding the :: scope
11812 resolution operator, object, function, and enumerator
11813 names are ignored. */
11814 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11816 /* Look up the name. */
11817 decl = cp_parser_lookup_name (parser, identifier,
11819 /*is_template=*/false,
11820 /*is_namespace=*/false,
11821 check_dependency_p);
11826 /* Try a template-id. */
11827 decl = cp_parser_template_id (parser, template_keyword_p,
11828 check_dependency_p,
11830 if (decl == error_mark_node)
11831 return error_mark_node;
11834 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11836 /* If this is a typename, create a TYPENAME_TYPE. */
11837 if (typename_p && decl != error_mark_node)
11839 decl = make_typename_type (scope, decl, /*complain=*/1);
11840 if (decl != error_mark_node)
11841 decl = TYPE_NAME (decl);
11844 /* Check to see that it is really the name of a class. */
11845 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11846 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11847 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11848 /* Situations like this:
11850 template <typename T> struct A {
11851 typename T::template X<int>::I i;
11854 are problematic. Is `T::template X<int>' a class-name? The
11855 standard does not seem to be definitive, but there is no other
11856 valid interpretation of the following `::'. Therefore, those
11857 names are considered class-names. */
11858 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11859 else if (decl == error_mark_node
11860 || TREE_CODE (decl) != TYPE_DECL
11861 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11863 cp_parser_error (parser, "expected class-name");
11864 return error_mark_node;
11870 /* Parse a class-specifier.
11873 class-head { member-specification [opt] }
11875 Returns the TREE_TYPE representing the class. */
11878 cp_parser_class_specifier (cp_parser* parser)
11883 int has_trailing_semicolon;
11884 bool nested_name_specifier_p;
11885 unsigned saved_num_template_parameter_lists;
11886 bool pop_p = false;
11887 tree scope = NULL_TREE;
11889 push_deferring_access_checks (dk_no_deferred);
11891 /* Parse the class-head. */
11892 type = cp_parser_class_head (parser,
11893 &nested_name_specifier_p,
11895 /* If the class-head was a semantic disaster, skip the entire body
11899 cp_parser_skip_to_end_of_block_or_statement (parser);
11900 pop_deferring_access_checks ();
11901 return error_mark_node;
11904 /* Look for the `{'. */
11905 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11907 pop_deferring_access_checks ();
11908 return error_mark_node;
11911 /* Issue an error message if type-definitions are forbidden here. */
11912 cp_parser_check_type_definition (parser);
11913 /* Remember that we are defining one more class. */
11914 ++parser->num_classes_being_defined;
11915 /* Inside the class, surrounding template-parameter-lists do not
11917 saved_num_template_parameter_lists
11918 = parser->num_template_parameter_lists;
11919 parser->num_template_parameter_lists = 0;
11921 /* Start the class. */
11922 if (nested_name_specifier_p)
11924 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11925 pop_p = push_scope (scope);
11927 type = begin_class_definition (type);
11928 if (type == error_mark_node)
11929 /* If the type is erroneous, skip the entire body of the class. */
11930 cp_parser_skip_to_closing_brace (parser);
11932 /* Parse the member-specification. */
11933 cp_parser_member_specification_opt (parser);
11934 /* Look for the trailing `}'. */
11935 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11936 /* We get better error messages by noticing a common problem: a
11937 missing trailing `;'. */
11938 token = cp_lexer_peek_token (parser->lexer);
11939 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11940 /* Look for trailing attributes to apply to this class. */
11941 if (cp_parser_allow_gnu_extensions_p (parser))
11943 tree sub_attr = cp_parser_attributes_opt (parser);
11944 attributes = chainon (attributes, sub_attr);
11946 if (type != error_mark_node)
11947 type = finish_struct (type, attributes);
11950 /* If this class is not itself within the scope of another class,
11951 then we need to parse the bodies of all of the queued function
11952 definitions. Note that the queued functions defined in a class
11953 are not always processed immediately following the
11954 class-specifier for that class. Consider:
11957 struct B { void f() { sizeof (A); } };
11960 If `f' were processed before the processing of `A' were
11961 completed, there would be no way to compute the size of `A'.
11962 Note that the nesting we are interested in here is lexical --
11963 not the semantic nesting given by TYPE_CONTEXT. In particular,
11966 struct A { struct B; };
11967 struct A::B { void f() { } };
11969 there is no need to delay the parsing of `A::B::f'. */
11970 if (--parser->num_classes_being_defined == 0)
11975 /* In a first pass, parse default arguments to the functions.
11976 Then, in a second pass, parse the bodies of the functions.
11977 This two-phased approach handles cases like:
11985 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11986 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11987 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11988 TREE_PURPOSE (parser->unparsed_functions_queues)
11989 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11991 fn = TREE_VALUE (queue_entry);
11992 /* Make sure that any template parameters are in scope. */
11993 maybe_begin_member_template_processing (fn);
11994 /* If there are default arguments that have not yet been processed,
11995 take care of them now. */
11996 cp_parser_late_parsing_default_args (parser, fn);
11997 /* Remove any template parameters from the symbol table. */
11998 maybe_end_member_template_processing ();
12000 /* Now parse the body of the functions. */
12001 for (TREE_VALUE (parser->unparsed_functions_queues)
12002 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12003 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12004 TREE_VALUE (parser->unparsed_functions_queues)
12005 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12007 /* Figure out which function we need to process. */
12008 fn = TREE_VALUE (queue_entry);
12010 /* A hack to prevent garbage collection. */
12013 /* Parse the function. */
12014 cp_parser_late_parsing_for_member (parser, fn);
12020 /* Put back any saved access checks. */
12021 pop_deferring_access_checks ();
12023 /* Restore the count of active template-parameter-lists. */
12024 parser->num_template_parameter_lists
12025 = saved_num_template_parameter_lists;
12030 /* Parse a class-head.
12033 class-key identifier [opt] base-clause [opt]
12034 class-key nested-name-specifier identifier base-clause [opt]
12035 class-key nested-name-specifier [opt] template-id
12039 class-key attributes identifier [opt] base-clause [opt]
12040 class-key attributes nested-name-specifier identifier base-clause [opt]
12041 class-key attributes nested-name-specifier [opt] template-id
12044 Returns the TYPE of the indicated class. Sets
12045 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12046 involving a nested-name-specifier was used, and FALSE otherwise.
12048 Returns NULL_TREE if the class-head is syntactically valid, but
12049 semantically invalid in a way that means we should skip the entire
12050 body of the class. */
12053 cp_parser_class_head (cp_parser* parser,
12054 bool* nested_name_specifier_p,
12055 tree *attributes_p)
12058 tree nested_name_specifier;
12059 enum tag_types class_key;
12060 tree id = NULL_TREE;
12061 tree type = NULL_TREE;
12063 bool template_id_p = false;
12064 bool qualified_p = false;
12065 bool invalid_nested_name_p = false;
12066 bool invalid_explicit_specialization_p = false;
12067 bool pop_p = false;
12068 unsigned num_templates;
12070 /* Assume no nested-name-specifier will be present. */
12071 *nested_name_specifier_p = false;
12072 /* Assume no template parameter lists will be used in defining the
12076 /* Look for the class-key. */
12077 class_key = cp_parser_class_key (parser);
12078 if (class_key == none_type)
12079 return error_mark_node;
12081 /* Parse the attributes. */
12082 attributes = cp_parser_attributes_opt (parser);
12084 /* If the next token is `::', that is invalid -- but sometimes
12085 people do try to write:
12089 Handle this gracefully by accepting the extra qualifier, and then
12090 issuing an error about it later if this really is a
12091 class-head. If it turns out just to be an elaborated type
12092 specifier, remain silent. */
12093 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12094 qualified_p = true;
12096 push_deferring_access_checks (dk_no_check);
12098 /* Determine the name of the class. Begin by looking for an
12099 optional nested-name-specifier. */
12100 nested_name_specifier
12101 = cp_parser_nested_name_specifier_opt (parser,
12102 /*typename_keyword_p=*/false,
12103 /*check_dependency_p=*/false,
12105 /*is_declaration=*/false);
12106 /* If there was a nested-name-specifier, then there *must* be an
12108 if (nested_name_specifier)
12110 /* Although the grammar says `identifier', it really means
12111 `class-name' or `template-name'. You are only allowed to
12112 define a class that has already been declared with this
12115 The proposed resolution for Core Issue 180 says that whever
12116 you see `class T::X' you should treat `X' as a type-name.
12118 It is OK to define an inaccessible class; for example:
12120 class A { class B; };
12123 We do not know if we will see a class-name, or a
12124 template-name. We look for a class-name first, in case the
12125 class-name is a template-id; if we looked for the
12126 template-name first we would stop after the template-name. */
12127 cp_parser_parse_tentatively (parser);
12128 type = cp_parser_class_name (parser,
12129 /*typename_keyword_p=*/false,
12130 /*template_keyword_p=*/false,
12132 /*check_dependency_p=*/false,
12133 /*class_head_p=*/true,
12134 /*is_declaration=*/false);
12135 /* If that didn't work, ignore the nested-name-specifier. */
12136 if (!cp_parser_parse_definitely (parser))
12138 invalid_nested_name_p = true;
12139 id = cp_parser_identifier (parser);
12140 if (id == error_mark_node)
12143 /* If we could not find a corresponding TYPE, treat this
12144 declaration like an unqualified declaration. */
12145 if (type == error_mark_node)
12146 nested_name_specifier = NULL_TREE;
12147 /* Otherwise, count the number of templates used in TYPE and its
12148 containing scopes. */
12153 for (scope = TREE_TYPE (type);
12154 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12155 scope = (TYPE_P (scope)
12156 ? TYPE_CONTEXT (scope)
12157 : DECL_CONTEXT (scope)))
12159 && CLASS_TYPE_P (scope)
12160 && CLASSTYPE_TEMPLATE_INFO (scope)
12161 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12162 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12166 /* Otherwise, the identifier is optional. */
12169 /* We don't know whether what comes next is a template-id,
12170 an identifier, or nothing at all. */
12171 cp_parser_parse_tentatively (parser);
12172 /* Check for a template-id. */
12173 id = cp_parser_template_id (parser,
12174 /*template_keyword_p=*/false,
12175 /*check_dependency_p=*/true,
12176 /*is_declaration=*/true);
12177 /* If that didn't work, it could still be an identifier. */
12178 if (!cp_parser_parse_definitely (parser))
12180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12181 id = cp_parser_identifier (parser);
12187 template_id_p = true;
12192 pop_deferring_access_checks ();
12195 cp_parser_check_for_invalid_template_id (parser, id);
12197 /* If it's not a `:' or a `{' then we can't really be looking at a
12198 class-head, since a class-head only appears as part of a
12199 class-specifier. We have to detect this situation before calling
12200 xref_tag, since that has irreversible side-effects. */
12201 if (!cp_parser_next_token_starts_class_definition_p (parser))
12203 cp_parser_error (parser, "expected `{' or `:'");
12204 return error_mark_node;
12207 /* At this point, we're going ahead with the class-specifier, even
12208 if some other problem occurs. */
12209 cp_parser_commit_to_tentative_parse (parser);
12210 /* Issue the error about the overly-qualified name now. */
12212 cp_parser_error (parser,
12213 "global qualification of class name is invalid");
12214 else if (invalid_nested_name_p)
12215 cp_parser_error (parser,
12216 "qualified name does not name a class");
12217 else if (nested_name_specifier)
12221 /* Reject typedef-names in class heads. */
12222 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12224 error ("invalid class name in declaration of `%D'", type);
12229 /* Figure out in what scope the declaration is being placed. */
12230 scope = current_scope ();
12232 scope = current_namespace;
12233 /* If that scope does not contain the scope in which the
12234 class was originally declared, the program is invalid. */
12235 if (scope && !is_ancestor (scope, nested_name_specifier))
12237 error ("declaration of `%D' in `%D' which does not "
12238 "enclose `%D'", type, scope, nested_name_specifier);
12244 A declarator-id shall not be qualified exception of the
12245 definition of a ... nested class outside of its class
12246 ... [or] a the definition or explicit instantiation of a
12247 class member of a namespace outside of its namespace. */
12248 if (scope == nested_name_specifier)
12250 pedwarn ("extra qualification ignored");
12251 nested_name_specifier = NULL_TREE;
12255 /* An explicit-specialization must be preceded by "template <>". If
12256 it is not, try to recover gracefully. */
12257 if (at_namespace_scope_p ()
12258 && parser->num_template_parameter_lists == 0
12261 error ("an explicit specialization must be preceded by 'template <>'");
12262 invalid_explicit_specialization_p = true;
12263 /* Take the same action that would have been taken by
12264 cp_parser_explicit_specialization. */
12265 ++parser->num_template_parameter_lists;
12266 begin_specialization ();
12268 /* There must be no "return" statements between this point and the
12269 end of this function; set "type "to the correct return value and
12270 use "goto done;" to return. */
12271 /* Make sure that the right number of template parameters were
12273 if (!cp_parser_check_template_parameters (parser, num_templates))
12275 /* If something went wrong, there is no point in even trying to
12276 process the class-definition. */
12281 /* Look up the type. */
12284 type = TREE_TYPE (id);
12285 maybe_process_partial_specialization (type);
12287 else if (!nested_name_specifier)
12289 /* If the class was unnamed, create a dummy name. */
12291 id = make_anon_name ();
12292 type = xref_tag (class_key, id, /*globalize=*/false,
12293 parser->num_template_parameter_lists);
12298 bool pop_p = false;
12302 template <typename T> struct S { struct T };
12303 template <typename T> struct S<T>::T { };
12305 we will get a TYPENAME_TYPE when processing the definition of
12306 `S::T'. We need to resolve it to the actual type before we
12307 try to define it. */
12308 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12310 class_type = resolve_typename_type (TREE_TYPE (type),
12311 /*only_current_p=*/false);
12312 if (class_type != error_mark_node)
12313 type = TYPE_NAME (class_type);
12316 cp_parser_error (parser, "could not resolve typename type");
12317 type = error_mark_node;
12321 maybe_process_partial_specialization (TREE_TYPE (type));
12322 class_type = current_class_type;
12323 /* Enter the scope indicated by the nested-name-specifier. */
12324 if (nested_name_specifier)
12325 pop_p = push_scope (nested_name_specifier);
12326 /* Get the canonical version of this type. */
12327 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12328 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12329 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12330 type = push_template_decl (type);
12331 type = TREE_TYPE (type);
12332 if (nested_name_specifier)
12334 *nested_name_specifier_p = true;
12336 pop_scope (nested_name_specifier);
12339 /* Indicate whether this class was declared as a `class' or as a
12341 if (TREE_CODE (type) == RECORD_TYPE)
12342 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12343 cp_parser_check_class_key (class_key, type);
12345 /* Enter the scope containing the class; the names of base classes
12346 should be looked up in that context. For example, given:
12348 struct A { struct B {}; struct C; };
12349 struct A::C : B {};
12352 if (nested_name_specifier)
12353 pop_p = push_scope (nested_name_specifier);
12354 /* Now, look for the base-clause. */
12355 token = cp_lexer_peek_token (parser->lexer);
12356 if (token->type == CPP_COLON)
12360 /* Get the list of base-classes. */
12361 bases = cp_parser_base_clause (parser);
12362 /* Process them. */
12363 xref_basetypes (type, bases);
12365 /* Leave the scope given by the nested-name-specifier. We will
12366 enter the class scope itself while processing the members. */
12368 pop_scope (nested_name_specifier);
12371 if (invalid_explicit_specialization_p)
12373 end_specialization ();
12374 --parser->num_template_parameter_lists;
12376 *attributes_p = attributes;
12380 /* Parse a class-key.
12387 Returns the kind of class-key specified, or none_type to indicate
12390 static enum tag_types
12391 cp_parser_class_key (cp_parser* parser)
12394 enum tag_types tag_type;
12396 /* Look for the class-key. */
12397 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12401 /* Check to see if the TOKEN is a class-key. */
12402 tag_type = cp_parser_token_is_class_key (token);
12404 cp_parser_error (parser, "expected class-key");
12408 /* Parse an (optional) member-specification.
12410 member-specification:
12411 member-declaration member-specification [opt]
12412 access-specifier : member-specification [opt] */
12415 cp_parser_member_specification_opt (cp_parser* parser)
12422 /* Peek at the next token. */
12423 token = cp_lexer_peek_token (parser->lexer);
12424 /* If it's a `}', or EOF then we've seen all the members. */
12425 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12428 /* See if this token is a keyword. */
12429 keyword = token->keyword;
12433 case RID_PROTECTED:
12435 /* Consume the access-specifier. */
12436 cp_lexer_consume_token (parser->lexer);
12437 /* Remember which access-specifier is active. */
12438 current_access_specifier = token->value;
12439 /* Look for the `:'. */
12440 cp_parser_require (parser, CPP_COLON, "`:'");
12444 /* Otherwise, the next construction must be a
12445 member-declaration. */
12446 cp_parser_member_declaration (parser);
12451 /* Parse a member-declaration.
12453 member-declaration:
12454 decl-specifier-seq [opt] member-declarator-list [opt] ;
12455 function-definition ; [opt]
12456 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12458 template-declaration
12460 member-declarator-list:
12462 member-declarator-list , member-declarator
12465 declarator pure-specifier [opt]
12466 declarator constant-initializer [opt]
12467 identifier [opt] : constant-expression
12471 member-declaration:
12472 __extension__ member-declaration
12475 declarator attributes [opt] pure-specifier [opt]
12476 declarator attributes [opt] constant-initializer [opt]
12477 identifier [opt] attributes [opt] : constant-expression */
12480 cp_parser_member_declaration (cp_parser* parser)
12482 tree decl_specifiers;
12483 tree prefix_attributes;
12485 int declares_class_or_enum;
12488 int saved_pedantic;
12490 /* Check for the `__extension__' keyword. */
12491 if (cp_parser_extension_opt (parser, &saved_pedantic))
12494 cp_parser_member_declaration (parser);
12495 /* Restore the old value of the PEDANTIC flag. */
12496 pedantic = saved_pedantic;
12501 /* Check for a template-declaration. */
12502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12504 /* Parse the template-declaration. */
12505 cp_parser_template_declaration (parser, /*member_p=*/true);
12510 /* Check for a using-declaration. */
12511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12513 /* Parse the using-declaration. */
12514 cp_parser_using_declaration (parser);
12519 /* Parse the decl-specifier-seq. */
12521 = cp_parser_decl_specifier_seq (parser,
12522 CP_PARSER_FLAGS_OPTIONAL,
12523 &prefix_attributes,
12524 &declares_class_or_enum);
12525 /* Check for an invalid type-name. */
12526 if (cp_parser_diagnose_invalid_type_name (parser))
12528 /* If there is no declarator, then the decl-specifier-seq should
12530 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12532 /* If there was no decl-specifier-seq, and the next token is a
12533 `;', then we have something like:
12539 Each member-declaration shall declare at least one member
12540 name of the class. */
12541 if (!decl_specifiers)
12544 pedwarn ("extra semicolon");
12550 /* See if this declaration is a friend. */
12551 friend_p = cp_parser_friend_p (decl_specifiers);
12552 /* If there were decl-specifiers, check to see if there was
12553 a class-declaration. */
12554 type = check_tag_decl (decl_specifiers);
12555 /* Nested classes have already been added to the class, but
12556 a `friend' needs to be explicitly registered. */
12559 /* If the `friend' keyword was present, the friend must
12560 be introduced with a class-key. */
12561 if (!declares_class_or_enum)
12562 error ("a class-key must be used when declaring a friend");
12565 template <typename T> struct A {
12566 friend struct A<T>::B;
12569 A<T>::B will be represented by a TYPENAME_TYPE, and
12570 therefore not recognized by check_tag_decl. */
12575 for (specifier = decl_specifiers;
12577 specifier = TREE_CHAIN (specifier))
12579 tree s = TREE_VALUE (specifier);
12581 if (TREE_CODE (s) == IDENTIFIER_NODE)
12582 get_global_value_if_present (s, &type);
12583 if (TREE_CODE (s) == TYPE_DECL)
12592 if (!type || !TYPE_P (type))
12593 error ("friend declaration does not name a class or "
12596 make_friend_class (current_class_type, type,
12597 /*complain=*/true);
12599 /* If there is no TYPE, an error message will already have
12603 /* An anonymous aggregate has to be handled specially; such
12604 a declaration really declares a data member (with a
12605 particular type), as opposed to a nested class. */
12606 else if (ANON_AGGR_TYPE_P (type))
12608 /* Remove constructors and such from TYPE, now that we
12609 know it is an anonymous aggregate. */
12610 fixup_anonymous_aggr (type);
12611 /* And make the corresponding data member. */
12612 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12613 /* Add it to the class. */
12614 finish_member_declaration (decl);
12617 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12622 /* See if these declarations will be friends. */
12623 friend_p = cp_parser_friend_p (decl_specifiers);
12625 /* Keep going until we hit the `;' at the end of the
12627 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12629 tree attributes = NULL_TREE;
12630 tree first_attribute;
12632 /* Peek at the next token. */
12633 token = cp_lexer_peek_token (parser->lexer);
12635 /* Check for a bitfield declaration. */
12636 if (token->type == CPP_COLON
12637 || (token->type == CPP_NAME
12638 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12644 /* Get the name of the bitfield. Note that we cannot just
12645 check TOKEN here because it may have been invalidated by
12646 the call to cp_lexer_peek_nth_token above. */
12647 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12648 identifier = cp_parser_identifier (parser);
12650 identifier = NULL_TREE;
12652 /* Consume the `:' token. */
12653 cp_lexer_consume_token (parser->lexer);
12654 /* Get the width of the bitfield. */
12656 = cp_parser_constant_expression (parser,
12657 /*allow_non_constant=*/false,
12660 /* Look for attributes that apply to the bitfield. */
12661 attributes = cp_parser_attributes_opt (parser);
12662 /* Remember which attributes are prefix attributes and
12664 first_attribute = attributes;
12665 /* Combine the attributes. */
12666 attributes = chainon (prefix_attributes, attributes);
12668 /* Create the bitfield declaration. */
12669 decl = grokbitfield (identifier,
12672 /* Apply the attributes. */
12673 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12679 tree asm_specification;
12680 int ctor_dtor_or_conv_p;
12682 /* Parse the declarator. */
12684 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12685 &ctor_dtor_or_conv_p,
12686 /*parenthesized_p=*/NULL,
12687 /*member_p=*/true);
12689 /* If something went wrong parsing the declarator, make sure
12690 that we at least consume some tokens. */
12691 if (declarator == error_mark_node)
12693 /* Skip to the end of the statement. */
12694 cp_parser_skip_to_end_of_statement (parser);
12695 /* If the next token is not a semicolon, that is
12696 probably because we just skipped over the body of
12697 a function. So, we consume a semicolon if
12698 present, but do not issue an error message if it
12700 if (cp_lexer_next_token_is (parser->lexer,
12702 cp_lexer_consume_token (parser->lexer);
12706 cp_parser_check_for_definition_in_return_type
12707 (declarator, declares_class_or_enum);
12709 /* Look for an asm-specification. */
12710 asm_specification = cp_parser_asm_specification_opt (parser);
12711 /* Look for attributes that apply to the declaration. */
12712 attributes = cp_parser_attributes_opt (parser);
12713 /* Remember which attributes are prefix attributes and
12715 first_attribute = attributes;
12716 /* Combine the attributes. */
12717 attributes = chainon (prefix_attributes, attributes);
12719 /* If it's an `=', then we have a constant-initializer or a
12720 pure-specifier. It is not correct to parse the
12721 initializer before registering the member declaration
12722 since the member declaration should be in scope while
12723 its initializer is processed. However, the rest of the
12724 front end does not yet provide an interface that allows
12725 us to handle this correctly. */
12726 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12730 A pure-specifier shall be used only in the declaration of
12731 a virtual function.
12733 A member-declarator can contain a constant-initializer
12734 only if it declares a static member of integral or
12737 Therefore, if the DECLARATOR is for a function, we look
12738 for a pure-specifier; otherwise, we look for a
12739 constant-initializer. When we call `grokfield', it will
12740 perform more stringent semantics checks. */
12741 if (TREE_CODE (declarator) == CALL_EXPR)
12742 initializer = cp_parser_pure_specifier (parser);
12744 /* Parse the initializer. */
12745 initializer = cp_parser_constant_initializer (parser);
12747 /* Otherwise, there is no initializer. */
12749 initializer = NULL_TREE;
12751 /* See if we are probably looking at a function
12752 definition. We are certainly not looking at at a
12753 member-declarator. Calling `grokfield' has
12754 side-effects, so we must not do it unless we are sure
12755 that we are looking at a member-declarator. */
12756 if (cp_parser_token_starts_function_definition_p
12757 (cp_lexer_peek_token (parser->lexer)))
12759 /* The grammar does not allow a pure-specifier to be
12760 used when a member function is defined. (It is
12761 possible that this fact is an oversight in the
12762 standard, since a pure function may be defined
12763 outside of the class-specifier. */
12765 error ("pure-specifier on function-definition");
12766 decl = cp_parser_save_member_function_body (parser,
12770 /* If the member was not a friend, declare it here. */
12772 finish_member_declaration (decl);
12773 /* Peek at the next token. */
12774 token = cp_lexer_peek_token (parser->lexer);
12775 /* If the next token is a semicolon, consume it. */
12776 if (token->type == CPP_SEMICOLON)
12777 cp_lexer_consume_token (parser->lexer);
12782 /* Create the declaration. */
12783 decl = grokfield (declarator, decl_specifiers,
12784 initializer, asm_specification,
12786 /* Any initialization must have been from a
12787 constant-expression. */
12788 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12789 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12793 /* Reset PREFIX_ATTRIBUTES. */
12794 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12795 attributes = TREE_CHAIN (attributes);
12797 TREE_CHAIN (attributes) = NULL_TREE;
12799 /* If there is any qualification still in effect, clear it
12800 now; we will be starting fresh with the next declarator. */
12801 parser->scope = NULL_TREE;
12802 parser->qualifying_scope = NULL_TREE;
12803 parser->object_scope = NULL_TREE;
12804 /* If it's a `,', then there are more declarators. */
12805 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12806 cp_lexer_consume_token (parser->lexer);
12807 /* If the next token isn't a `;', then we have a parse error. */
12808 else if (cp_lexer_next_token_is_not (parser->lexer,
12811 cp_parser_error (parser, "expected `;'");
12812 /* Skip tokens until we find a `;'. */
12813 cp_parser_skip_to_end_of_statement (parser);
12820 /* Add DECL to the list of members. */
12822 finish_member_declaration (decl);
12824 if (TREE_CODE (decl) == FUNCTION_DECL)
12825 cp_parser_save_default_args (parser, decl);
12830 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12833 /* Parse a pure-specifier.
12838 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12839 Otherwise, ERROR_MARK_NODE is returned. */
12842 cp_parser_pure_specifier (cp_parser* parser)
12846 /* Look for the `=' token. */
12847 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12848 return error_mark_node;
12849 /* Look for the `0' token. */
12850 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12851 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12852 to get information from the lexer about how the number was
12853 spelled in order to fix this problem. */
12854 if (!token || !integer_zerop (token->value))
12855 return error_mark_node;
12857 return integer_zero_node;
12860 /* Parse a constant-initializer.
12862 constant-initializer:
12863 = constant-expression
12865 Returns a representation of the constant-expression. */
12868 cp_parser_constant_initializer (cp_parser* parser)
12870 /* Look for the `=' token. */
12871 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12872 return error_mark_node;
12874 /* It is invalid to write:
12876 struct S { static const int i = { 7 }; };
12879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12881 cp_parser_error (parser,
12882 "a brace-enclosed initializer is not allowed here");
12883 /* Consume the opening brace. */
12884 cp_lexer_consume_token (parser->lexer);
12885 /* Skip the initializer. */
12886 cp_parser_skip_to_closing_brace (parser);
12887 /* Look for the trailing `}'. */
12888 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12890 return error_mark_node;
12893 return cp_parser_constant_expression (parser,
12894 /*allow_non_constant=*/false,
12898 /* Derived classes [gram.class.derived] */
12900 /* Parse a base-clause.
12903 : base-specifier-list
12905 base-specifier-list:
12907 base-specifier-list , base-specifier
12909 Returns a TREE_LIST representing the base-classes, in the order in
12910 which they were declared. The representation of each node is as
12911 described by cp_parser_base_specifier.
12913 In the case that no bases are specified, this function will return
12914 NULL_TREE, not ERROR_MARK_NODE. */
12917 cp_parser_base_clause (cp_parser* parser)
12919 tree bases = NULL_TREE;
12921 /* Look for the `:' that begins the list. */
12922 cp_parser_require (parser, CPP_COLON, "`:'");
12924 /* Scan the base-specifier-list. */
12930 /* Look for the base-specifier. */
12931 base = cp_parser_base_specifier (parser);
12932 /* Add BASE to the front of the list. */
12933 if (base != error_mark_node)
12935 TREE_CHAIN (base) = bases;
12938 /* Peek at the next token. */
12939 token = cp_lexer_peek_token (parser->lexer);
12940 /* If it's not a comma, then the list is complete. */
12941 if (token->type != CPP_COMMA)
12943 /* Consume the `,'. */
12944 cp_lexer_consume_token (parser->lexer);
12947 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12948 base class had a qualified name. However, the next name that
12949 appears is certainly not qualified. */
12950 parser->scope = NULL_TREE;
12951 parser->qualifying_scope = NULL_TREE;
12952 parser->object_scope = NULL_TREE;
12954 return nreverse (bases);
12957 /* Parse a base-specifier.
12960 :: [opt] nested-name-specifier [opt] class-name
12961 virtual access-specifier [opt] :: [opt] nested-name-specifier
12963 access-specifier virtual [opt] :: [opt] nested-name-specifier
12966 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12967 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12968 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12969 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12972 cp_parser_base_specifier (cp_parser* parser)
12976 bool virtual_p = false;
12977 bool duplicate_virtual_error_issued_p = false;
12978 bool duplicate_access_error_issued_p = false;
12979 bool class_scope_p, template_p;
12980 tree access = access_default_node;
12983 /* Process the optional `virtual' and `access-specifier'. */
12986 /* Peek at the next token. */
12987 token = cp_lexer_peek_token (parser->lexer);
12988 /* Process `virtual'. */
12989 switch (token->keyword)
12992 /* If `virtual' appears more than once, issue an error. */
12993 if (virtual_p && !duplicate_virtual_error_issued_p)
12995 cp_parser_error (parser,
12996 "`virtual' specified more than once in base-specified");
12997 duplicate_virtual_error_issued_p = true;
13002 /* Consume the `virtual' token. */
13003 cp_lexer_consume_token (parser->lexer);
13008 case RID_PROTECTED:
13010 /* If more than one access specifier appears, issue an
13012 if (access != access_default_node
13013 && !duplicate_access_error_issued_p)
13015 cp_parser_error (parser,
13016 "more than one access specifier in base-specified");
13017 duplicate_access_error_issued_p = true;
13020 access = ridpointers[(int) token->keyword];
13022 /* Consume the access-specifier. */
13023 cp_lexer_consume_token (parser->lexer);
13032 /* It is not uncommon to see programs mechanically, errouneously, use
13033 the 'typename' keyword to denote (dependent) qualified types
13034 as base classes. */
13035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13037 if (!processing_template_decl)
13038 error ("keyword `typename' not allowed outside of templates");
13040 error ("keyword `typename' not allowed in this context "
13041 "(the base class is implicitly a type)");
13042 cp_lexer_consume_token (parser->lexer);
13045 /* Look for the optional `::' operator. */
13046 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13047 /* Look for the nested-name-specifier. The simplest way to
13052 The keyword `typename' is not permitted in a base-specifier or
13053 mem-initializer; in these contexts a qualified name that
13054 depends on a template-parameter is implicitly assumed to be a
13057 is to pretend that we have seen the `typename' keyword at this
13059 cp_parser_nested_name_specifier_opt (parser,
13060 /*typename_keyword_p=*/true,
13061 /*check_dependency_p=*/true,
13063 /*is_declaration=*/true);
13064 /* If the base class is given by a qualified name, assume that names
13065 we see are type names or templates, as appropriate. */
13066 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13067 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13069 /* Finally, look for the class-name. */
13070 type = cp_parser_class_name (parser,
13074 /*check_dependency_p=*/true,
13075 /*class_head_p=*/false,
13076 /*is_declaration=*/true);
13078 if (type == error_mark_node)
13079 return error_mark_node;
13081 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13084 /* Exception handling [gram.exception] */
13086 /* Parse an (optional) exception-specification.
13088 exception-specification:
13089 throw ( type-id-list [opt] )
13091 Returns a TREE_LIST representing the exception-specification. The
13092 TREE_VALUE of each node is a type. */
13095 cp_parser_exception_specification_opt (cp_parser* parser)
13100 /* Peek at the next token. */
13101 token = cp_lexer_peek_token (parser->lexer);
13102 /* If it's not `throw', then there's no exception-specification. */
13103 if (!cp_parser_is_keyword (token, RID_THROW))
13106 /* Consume the `throw'. */
13107 cp_lexer_consume_token (parser->lexer);
13109 /* Look for the `('. */
13110 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13112 /* Peek at the next token. */
13113 token = cp_lexer_peek_token (parser->lexer);
13114 /* If it's not a `)', then there is a type-id-list. */
13115 if (token->type != CPP_CLOSE_PAREN)
13117 const char *saved_message;
13119 /* Types may not be defined in an exception-specification. */
13120 saved_message = parser->type_definition_forbidden_message;
13121 parser->type_definition_forbidden_message
13122 = "types may not be defined in an exception-specification";
13123 /* Parse the type-id-list. */
13124 type_id_list = cp_parser_type_id_list (parser);
13125 /* Restore the saved message. */
13126 parser->type_definition_forbidden_message = saved_message;
13129 type_id_list = empty_except_spec;
13131 /* Look for the `)'. */
13132 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13134 return type_id_list;
13137 /* Parse an (optional) type-id-list.
13141 type-id-list , type-id
13143 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13144 in the order that the types were presented. */
13147 cp_parser_type_id_list (cp_parser* parser)
13149 tree types = NULL_TREE;
13156 /* Get the next type-id. */
13157 type = cp_parser_type_id (parser);
13158 /* Add it to the list. */
13159 types = add_exception_specifier (types, type, /*complain=*/1);
13160 /* Peek at the next token. */
13161 token = cp_lexer_peek_token (parser->lexer);
13162 /* If it is not a `,', we are done. */
13163 if (token->type != CPP_COMMA)
13165 /* Consume the `,'. */
13166 cp_lexer_consume_token (parser->lexer);
13169 return nreverse (types);
13172 /* Parse a try-block.
13175 try compound-statement handler-seq */
13178 cp_parser_try_block (cp_parser* parser)
13182 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13183 try_block = begin_try_block ();
13184 cp_parser_compound_statement (parser, false);
13185 finish_try_block (try_block);
13186 cp_parser_handler_seq (parser);
13187 finish_handler_sequence (try_block);
13192 /* Parse a function-try-block.
13194 function-try-block:
13195 try ctor-initializer [opt] function-body handler-seq */
13198 cp_parser_function_try_block (cp_parser* parser)
13201 bool ctor_initializer_p;
13203 /* Look for the `try' keyword. */
13204 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13206 /* Let the rest of the front-end know where we are. */
13207 try_block = begin_function_try_block ();
13208 /* Parse the function-body. */
13210 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13211 /* We're done with the `try' part. */
13212 finish_function_try_block (try_block);
13213 /* Parse the handlers. */
13214 cp_parser_handler_seq (parser);
13215 /* We're done with the handlers. */
13216 finish_function_handler_sequence (try_block);
13218 return ctor_initializer_p;
13221 /* Parse a handler-seq.
13224 handler handler-seq [opt] */
13227 cp_parser_handler_seq (cp_parser* parser)
13233 /* Parse the handler. */
13234 cp_parser_handler (parser);
13235 /* Peek at the next token. */
13236 token = cp_lexer_peek_token (parser->lexer);
13237 /* If it's not `catch' then there are no more handlers. */
13238 if (!cp_parser_is_keyword (token, RID_CATCH))
13243 /* Parse a handler.
13246 catch ( exception-declaration ) compound-statement */
13249 cp_parser_handler (cp_parser* parser)
13254 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13255 handler = begin_handler ();
13256 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13257 declaration = cp_parser_exception_declaration (parser);
13258 finish_handler_parms (declaration, handler);
13259 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13260 cp_parser_compound_statement (parser, false);
13261 finish_handler (handler);
13264 /* Parse an exception-declaration.
13266 exception-declaration:
13267 type-specifier-seq declarator
13268 type-specifier-seq abstract-declarator
13272 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13273 ellipsis variant is used. */
13276 cp_parser_exception_declaration (cp_parser* parser)
13278 tree type_specifiers;
13280 const char *saved_message;
13282 /* If it's an ellipsis, it's easy to handle. */
13283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13285 /* Consume the `...' token. */
13286 cp_lexer_consume_token (parser->lexer);
13290 /* Types may not be defined in exception-declarations. */
13291 saved_message = parser->type_definition_forbidden_message;
13292 parser->type_definition_forbidden_message
13293 = "types may not be defined in exception-declarations";
13295 /* Parse the type-specifier-seq. */
13296 type_specifiers = cp_parser_type_specifier_seq (parser);
13297 /* If it's a `)', then there is no declarator. */
13298 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13299 declarator = NULL_TREE;
13301 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13302 /*ctor_dtor_or_conv_p=*/NULL,
13303 /*parenthesized_p=*/NULL,
13304 /*member_p=*/false);
13306 /* Restore the saved message. */
13307 parser->type_definition_forbidden_message = saved_message;
13309 return start_handler_parms (type_specifiers, declarator);
13312 /* Parse a throw-expression.
13315 throw assignment-expression [opt]
13317 Returns a THROW_EXPR representing the throw-expression. */
13320 cp_parser_throw_expression (cp_parser* parser)
13325 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13326 token = cp_lexer_peek_token (parser->lexer);
13327 /* Figure out whether or not there is an assignment-expression
13328 following the "throw" keyword. */
13329 if (token->type == CPP_COMMA
13330 || token->type == CPP_SEMICOLON
13331 || token->type == CPP_CLOSE_PAREN
13332 || token->type == CPP_CLOSE_SQUARE
13333 || token->type == CPP_CLOSE_BRACE
13334 || token->type == CPP_COLON)
13335 expression = NULL_TREE;
13337 expression = cp_parser_assignment_expression (parser);
13339 return build_throw (expression);
13342 /* GNU Extensions */
13344 /* Parse an (optional) asm-specification.
13347 asm ( string-literal )
13349 If the asm-specification is present, returns a STRING_CST
13350 corresponding to the string-literal. Otherwise, returns
13354 cp_parser_asm_specification_opt (cp_parser* parser)
13357 tree asm_specification;
13359 /* Peek at the next token. */
13360 token = cp_lexer_peek_token (parser->lexer);
13361 /* If the next token isn't the `asm' keyword, then there's no
13362 asm-specification. */
13363 if (!cp_parser_is_keyword (token, RID_ASM))
13366 /* Consume the `asm' token. */
13367 cp_lexer_consume_token (parser->lexer);
13368 /* Look for the `('. */
13369 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13371 /* Look for the string-literal. */
13372 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13374 asm_specification = token->value;
13376 asm_specification = NULL_TREE;
13378 /* Look for the `)'. */
13379 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13381 return asm_specification;
13384 /* Parse an asm-operand-list.
13388 asm-operand-list , asm-operand
13391 string-literal ( expression )
13392 [ string-literal ] string-literal ( expression )
13394 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13395 each node is the expression. The TREE_PURPOSE is itself a
13396 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13397 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13398 is a STRING_CST for the string literal before the parenthesis. */
13401 cp_parser_asm_operand_list (cp_parser* parser)
13403 tree asm_operands = NULL_TREE;
13407 tree string_literal;
13412 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13414 /* Consume the `[' token. */
13415 cp_lexer_consume_token (parser->lexer);
13416 /* Read the operand name. */
13417 name = cp_parser_identifier (parser);
13418 if (name != error_mark_node)
13419 name = build_string (IDENTIFIER_LENGTH (name),
13420 IDENTIFIER_POINTER (name));
13421 /* Look for the closing `]'. */
13422 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13426 /* Look for the string-literal. */
13427 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13428 string_literal = token ? token->value : error_mark_node;
13429 /* Look for the `('. */
13430 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13431 /* Parse the expression. */
13432 expression = cp_parser_expression (parser);
13433 /* Look for the `)'. */
13434 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13435 /* Add this operand to the list. */
13436 asm_operands = tree_cons (build_tree_list (name, string_literal),
13439 /* If the next token is not a `,', there are no more
13441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13443 /* Consume the `,'. */
13444 cp_lexer_consume_token (parser->lexer);
13447 return nreverse (asm_operands);
13450 /* Parse an asm-clobber-list.
13454 asm-clobber-list , string-literal
13456 Returns a TREE_LIST, indicating the clobbers in the order that they
13457 appeared. The TREE_VALUE of each node is a STRING_CST. */
13460 cp_parser_asm_clobber_list (cp_parser* parser)
13462 tree clobbers = NULL_TREE;
13467 tree string_literal;
13469 /* Look for the string literal. */
13470 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13471 string_literal = token ? token->value : error_mark_node;
13472 /* Add it to the list. */
13473 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13474 /* If the next token is not a `,', then the list is
13476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13478 /* Consume the `,' token. */
13479 cp_lexer_consume_token (parser->lexer);
13485 /* Parse an (optional) series of attributes.
13488 attributes attribute
13491 __attribute__ (( attribute-list [opt] ))
13493 The return value is as for cp_parser_attribute_list. */
13496 cp_parser_attributes_opt (cp_parser* parser)
13498 tree attributes = NULL_TREE;
13503 tree attribute_list;
13505 /* Peek at the next token. */
13506 token = cp_lexer_peek_token (parser->lexer);
13507 /* If it's not `__attribute__', then we're done. */
13508 if (token->keyword != RID_ATTRIBUTE)
13511 /* Consume the `__attribute__' keyword. */
13512 cp_lexer_consume_token (parser->lexer);
13513 /* Look for the two `(' tokens. */
13514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13515 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13517 /* Peek at the next token. */
13518 token = cp_lexer_peek_token (parser->lexer);
13519 if (token->type != CPP_CLOSE_PAREN)
13520 /* Parse the attribute-list. */
13521 attribute_list = cp_parser_attribute_list (parser);
13523 /* If the next token is a `)', then there is no attribute
13525 attribute_list = NULL;
13527 /* Look for the two `)' tokens. */
13528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13529 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13531 /* Add these new attributes to the list. */
13532 attributes = chainon (attributes, attribute_list);
13538 /* Parse an attribute-list.
13542 attribute-list , attribute
13546 identifier ( identifier )
13547 identifier ( identifier , expression-list )
13548 identifier ( expression-list )
13550 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13551 TREE_PURPOSE of each node is the identifier indicating which
13552 attribute is in use. The TREE_VALUE represents the arguments, if
13556 cp_parser_attribute_list (cp_parser* parser)
13558 tree attribute_list = NULL_TREE;
13566 /* Look for the identifier. We also allow keywords here; for
13567 example `__attribute__ ((const))' is legal. */
13568 token = cp_lexer_peek_token (parser->lexer);
13569 if (token->type == CPP_NAME
13570 || token->type == CPP_KEYWORD)
13572 /* Consume the token. */
13573 token = cp_lexer_consume_token (parser->lexer);
13575 /* Save away the identifier that indicates which attribute
13577 identifier = token->value;
13578 attribute = build_tree_list (identifier, NULL_TREE);
13580 /* Peek at the next token. */
13581 token = cp_lexer_peek_token (parser->lexer);
13582 /* If it's an `(', then parse the attribute arguments. */
13583 if (token->type == CPP_OPEN_PAREN)
13587 arguments = (cp_parser_parenthesized_expression_list
13588 (parser, true, /*non_constant_p=*/NULL));
13589 /* Save the identifier and arguments away. */
13590 TREE_VALUE (attribute) = arguments;
13593 /* Add this attribute to the list. */
13594 TREE_CHAIN (attribute) = attribute_list;
13595 attribute_list = attribute;
13597 /* Now, look for more attributes. */
13598 token = cp_lexer_peek_token (parser->lexer);
13600 /* Now, look for more attributes. If the next token isn't a
13601 `,', we're done. */
13602 if (token->type != CPP_COMMA)
13605 /* Consume the comma and keep going. */
13606 cp_lexer_consume_token (parser->lexer);
13609 /* We built up the list in reverse order. */
13610 return nreverse (attribute_list);
13613 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13614 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13615 current value of the PEDANTIC flag, regardless of whether or not
13616 the `__extension__' keyword is present. The caller is responsible
13617 for restoring the value of the PEDANTIC flag. */
13620 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13622 /* Save the old value of the PEDANTIC flag. */
13623 *saved_pedantic = pedantic;
13625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13627 /* Consume the `__extension__' token. */
13628 cp_lexer_consume_token (parser->lexer);
13629 /* We're not being pedantic while the `__extension__' keyword is
13639 /* Parse a label declaration.
13642 __label__ label-declarator-seq ;
13644 label-declarator-seq:
13645 identifier , label-declarator-seq
13649 cp_parser_label_declaration (cp_parser* parser)
13651 /* Look for the `__label__' keyword. */
13652 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13658 /* Look for an identifier. */
13659 identifier = cp_parser_identifier (parser);
13660 /* If we failed, stop. */
13661 if (identifier == error_mark_node)
13663 /* Declare it as a label. */
13664 finish_label_decl (identifier);
13665 /* If the next token is a `;', stop. */
13666 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13668 /* Look for the `,' separating the label declarations. */
13669 cp_parser_require (parser, CPP_COMMA, "`,'");
13672 /* Look for the final `;'. */
13673 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13676 /* Support Functions */
13678 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13679 NAME should have one of the representations used for an
13680 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13681 is returned. If PARSER->SCOPE is a dependent type, then a
13682 SCOPE_REF is returned.
13684 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13685 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13686 was formed. Abstractly, such entities should not be passed to this
13687 function, because they do not need to be looked up, but it is
13688 simpler to check for this special case here, rather than at the
13691 In cases not explicitly covered above, this function returns a
13692 DECL, OVERLOAD, or baselink representing the result of the lookup.
13693 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13696 If IS_TYPE is TRUE, bindings that do not refer to types are
13699 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13702 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13705 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13709 cp_parser_lookup_name (cp_parser *parser, tree name,
13710 bool is_type, bool is_template, bool is_namespace,
13711 bool check_dependency)
13714 tree object_type = parser->context->object_type;
13716 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13717 no longer valid. Note that if we are parsing tentatively, and
13718 the parse fails, OBJECT_TYPE will be automatically restored. */
13719 parser->context->object_type = NULL_TREE;
13721 if (name == error_mark_node)
13722 return error_mark_node;
13724 /* A template-id has already been resolved; there is no lookup to
13726 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13728 if (BASELINK_P (name))
13730 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13731 == TEMPLATE_ID_EXPR),
13736 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13737 it should already have been checked to make sure that the name
13738 used matches the type being destroyed. */
13739 if (TREE_CODE (name) == BIT_NOT_EXPR)
13743 /* Figure out to which type this destructor applies. */
13745 type = parser->scope;
13746 else if (object_type)
13747 type = object_type;
13749 type = current_class_type;
13750 /* If that's not a class type, there is no destructor. */
13751 if (!type || !CLASS_TYPE_P (type))
13752 return error_mark_node;
13753 if (!CLASSTYPE_DESTRUCTORS (type))
13754 return error_mark_node;
13755 /* If it was a class type, return the destructor. */
13756 return CLASSTYPE_DESTRUCTORS (type);
13759 /* By this point, the NAME should be an ordinary identifier. If
13760 the id-expression was a qualified name, the qualifying scope is
13761 stored in PARSER->SCOPE at this point. */
13762 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13765 /* Perform the lookup. */
13770 if (parser->scope == error_mark_node)
13771 return error_mark_node;
13773 /* If the SCOPE is dependent, the lookup must be deferred until
13774 the template is instantiated -- unless we are explicitly
13775 looking up names in uninstantiated templates. Even then, we
13776 cannot look up the name if the scope is not a class type; it
13777 might, for example, be a template type parameter. */
13778 dependent_p = (TYPE_P (parser->scope)
13779 && !(parser->in_declarator_p
13780 && currently_open_class (parser->scope))
13781 && dependent_type_p (parser->scope));
13782 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13786 /* The resolution to Core Issue 180 says that `struct A::B'
13787 should be considered a type-name, even if `A' is
13789 decl = TYPE_NAME (make_typename_type (parser->scope,
13792 else if (is_template)
13793 decl = make_unbound_class_template (parser->scope,
13797 decl = build_nt (SCOPE_REF, parser->scope, name);
13801 bool pop_p = false;
13803 /* If PARSER->SCOPE is a dependent type, then it must be a
13804 class type, and we must not be checking dependencies;
13805 otherwise, we would have processed this lookup above. So
13806 that PARSER->SCOPE is not considered a dependent base by
13807 lookup_member, we must enter the scope here. */
13809 pop_p = push_scope (parser->scope);
13810 /* If the PARSER->SCOPE is a a template specialization, it
13811 may be instantiated during name lookup. In that case,
13812 errors may be issued. Even if we rollback the current
13813 tentative parse, those errors are valid. */
13814 decl = lookup_qualified_name (parser->scope, name, is_type,
13815 /*complain=*/true);
13817 pop_scope (parser->scope);
13819 parser->qualifying_scope = parser->scope;
13820 parser->object_scope = NULL_TREE;
13822 else if (object_type)
13824 tree object_decl = NULL_TREE;
13825 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13826 OBJECT_TYPE is not a class. */
13827 if (CLASS_TYPE_P (object_type))
13828 /* If the OBJECT_TYPE is a template specialization, it may
13829 be instantiated during name lookup. In that case, errors
13830 may be issued. Even if we rollback the current tentative
13831 parse, those errors are valid. */
13832 object_decl = lookup_member (object_type,
13834 /*protect=*/0, is_type);
13835 /* Look it up in the enclosing context, too. */
13836 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13839 parser->object_scope = object_type;
13840 parser->qualifying_scope = NULL_TREE;
13842 decl = object_decl;
13846 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13849 parser->qualifying_scope = NULL_TREE;
13850 parser->object_scope = NULL_TREE;
13853 /* If the lookup failed, let our caller know. */
13855 || decl == error_mark_node
13856 || (TREE_CODE (decl) == FUNCTION_DECL
13857 && DECL_ANTICIPATED (decl)))
13858 return error_mark_node;
13860 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13861 if (TREE_CODE (decl) == TREE_LIST)
13863 /* The error message we have to print is too complicated for
13864 cp_parser_error, so we incorporate its actions directly. */
13865 if (!cp_parser_simulate_error (parser))
13867 error ("reference to `%D' is ambiguous", name);
13868 print_candidates (decl);
13870 return error_mark_node;
13873 my_friendly_assert (DECL_P (decl)
13874 || TREE_CODE (decl) == OVERLOAD
13875 || TREE_CODE (decl) == SCOPE_REF
13876 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13877 || BASELINK_P (decl),
13880 /* If we have resolved the name of a member declaration, check to
13881 see if the declaration is accessible. When the name resolves to
13882 set of overloaded functions, accessibility is checked when
13883 overload resolution is done.
13885 During an explicit instantiation, access is not checked at all,
13886 as per [temp.explicit]. */
13888 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13893 /* Like cp_parser_lookup_name, but for use in the typical case where
13894 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13895 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13898 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13900 return cp_parser_lookup_name (parser, name,
13902 /*is_template=*/false,
13903 /*is_namespace=*/false,
13904 /*check_dependency=*/true);
13907 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13908 the current context, return the TYPE_DECL. If TAG_NAME_P is
13909 true, the DECL indicates the class being defined in a class-head,
13910 or declared in an elaborated-type-specifier.
13912 Otherwise, return DECL. */
13915 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13917 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13918 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13921 template <typename T> struct B;
13924 template <typename T> struct A::B {};
13926 Similarly, in a elaborated-type-specifier:
13928 namespace N { struct X{}; }
13931 template <typename T> friend struct N::X;
13934 However, if the DECL refers to a class type, and we are in
13935 the scope of the class, then the name lookup automatically
13936 finds the TYPE_DECL created by build_self_reference rather
13937 than a TEMPLATE_DECL. For example, in:
13939 template <class T> struct S {
13943 there is no need to handle such case. */
13945 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13946 return DECL_TEMPLATE_RESULT (decl);
13951 /* If too many, or too few, template-parameter lists apply to the
13952 declarator, issue an error message. Returns TRUE if all went well,
13953 and FALSE otherwise. */
13956 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13959 unsigned num_templates;
13961 /* We haven't seen any classes that involve template parameters yet. */
13964 switch (TREE_CODE (declarator))
13971 tree main_declarator = TREE_OPERAND (declarator, 0);
13973 cp_parser_check_declarator_template_parameters (parser,
13982 scope = TREE_OPERAND (declarator, 0);
13983 member = TREE_OPERAND (declarator, 1);
13985 /* If this is a pointer-to-member, then we are not interested
13986 in the SCOPE, because it does not qualify the thing that is
13988 if (TREE_CODE (member) == INDIRECT_REF)
13989 return (cp_parser_check_declarator_template_parameters
13992 while (scope && CLASS_TYPE_P (scope))
13994 /* You're supposed to have one `template <...>'
13995 for every template class, but you don't need one
13996 for a full specialization. For example:
13998 template <class T> struct S{};
13999 template <> struct S<int> { void f(); };
14000 void S<int>::f () {}
14002 is correct; there shouldn't be a `template <>' for
14003 the definition of `S<int>::f'. */
14004 if (CLASSTYPE_TEMPLATE_INFO (scope)
14005 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14006 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14007 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14010 scope = TYPE_CONTEXT (scope);
14014 /* Fall through. */
14017 /* If the DECLARATOR has the form `X<y>' then it uses one
14018 additional level of template parameters. */
14019 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14022 return cp_parser_check_template_parameters (parser,
14027 /* NUM_TEMPLATES were used in the current declaration. If that is
14028 invalid, return FALSE and issue an error messages. Otherwise,
14032 cp_parser_check_template_parameters (cp_parser* parser,
14033 unsigned num_templates)
14035 /* If there are more template classes than parameter lists, we have
14038 template <class T> void S<T>::R<T>::f (); */
14039 if (parser->num_template_parameter_lists < num_templates)
14041 error ("too few template-parameter-lists");
14044 /* If there are the same number of template classes and parameter
14045 lists, that's OK. */
14046 if (parser->num_template_parameter_lists == num_templates)
14048 /* If there are more, but only one more, then we are referring to a
14049 member template. That's OK too. */
14050 if (parser->num_template_parameter_lists == num_templates + 1)
14052 /* Otherwise, there are too many template parameter lists. We have
14055 template <class T> template <class U> void S::f(); */
14056 error ("too many template-parameter-lists");
14060 /* Parse a binary-expression of the general form:
14064 binary-expression <token> <expr>
14066 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14067 to parser the <expr>s. If the first production is used, then the
14068 value returned by FN is returned directly. Otherwise, a node with
14069 the indicated EXPR_TYPE is returned, with operands corresponding to
14070 the two sub-expressions. */
14073 cp_parser_binary_expression (cp_parser* parser,
14074 const cp_parser_token_tree_map token_tree_map,
14075 cp_parser_expression_fn fn)
14079 /* Parse the first expression. */
14080 lhs = (*fn) (parser);
14081 /* Now, look for more expressions. */
14085 const cp_parser_token_tree_map_node *map_node;
14088 /* Peek at the next token. */
14089 token = cp_lexer_peek_token (parser->lexer);
14090 /* If the token is `>', and that's not an operator at the
14091 moment, then we're done. */
14092 if (token->type == CPP_GREATER
14093 && !parser->greater_than_is_operator_p)
14095 /* If we find one of the tokens we want, build the corresponding
14096 tree representation. */
14097 for (map_node = token_tree_map;
14098 map_node->token_type != CPP_EOF;
14100 if (map_node->token_type == token->type)
14102 /* Assume that an overloaded operator will not be used. */
14103 bool overloaded_p = false;
14105 /* Consume the operator token. */
14106 cp_lexer_consume_token (parser->lexer);
14107 /* Parse the right-hand side of the expression. */
14108 rhs = (*fn) (parser);
14109 /* Build the binary tree node. */
14110 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14112 /* If the binary operator required the use of an
14113 overloaded operator, then this expression cannot be an
14114 integral constant-expression. An overloaded operator
14115 can be used even if both operands are otherwise
14116 permissible in an integral constant-expression if at
14117 least one of the operands is of enumeration type. */
14119 && (cp_parser_non_integral_constant_expression
14120 (parser, "calls to overloaded operators")))
14121 lhs = error_mark_node;
14125 /* If the token wasn't one of the ones we want, we're done. */
14126 if (map_node->token_type == CPP_EOF)
14133 /* Parse an optional `::' token indicating that the following name is
14134 from the global namespace. If so, PARSER->SCOPE is set to the
14135 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14136 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14137 Returns the new value of PARSER->SCOPE, if the `::' token is
14138 present, and NULL_TREE otherwise. */
14141 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14145 /* Peek at the next token. */
14146 token = cp_lexer_peek_token (parser->lexer);
14147 /* If we're looking at a `::' token then we're starting from the
14148 global namespace, not our current location. */
14149 if (token->type == CPP_SCOPE)
14151 /* Consume the `::' token. */
14152 cp_lexer_consume_token (parser->lexer);
14153 /* Set the SCOPE so that we know where to start the lookup. */
14154 parser->scope = global_namespace;
14155 parser->qualifying_scope = global_namespace;
14156 parser->object_scope = NULL_TREE;
14158 return parser->scope;
14160 else if (!current_scope_valid_p)
14162 parser->scope = NULL_TREE;
14163 parser->qualifying_scope = NULL_TREE;
14164 parser->object_scope = NULL_TREE;
14170 /* Returns TRUE if the upcoming token sequence is the start of a
14171 constructor declarator. If FRIEND_P is true, the declarator is
14172 preceded by the `friend' specifier. */
14175 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14177 bool constructor_p;
14178 tree type_decl = NULL_TREE;
14179 bool nested_name_p;
14180 cp_token *next_token;
14182 /* The common case is that this is not a constructor declarator, so
14183 try to avoid doing lots of work if at all possible. It's not
14184 valid declare a constructor at function scope. */
14185 if (at_function_scope_p ())
14187 /* And only certain tokens can begin a constructor declarator. */
14188 next_token = cp_lexer_peek_token (parser->lexer);
14189 if (next_token->type != CPP_NAME
14190 && next_token->type != CPP_SCOPE
14191 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14192 && next_token->type != CPP_TEMPLATE_ID)
14195 /* Parse tentatively; we are going to roll back all of the tokens
14197 cp_parser_parse_tentatively (parser);
14198 /* Assume that we are looking at a constructor declarator. */
14199 constructor_p = true;
14201 /* Look for the optional `::' operator. */
14202 cp_parser_global_scope_opt (parser,
14203 /*current_scope_valid_p=*/false);
14204 /* Look for the nested-name-specifier. */
14206 = (cp_parser_nested_name_specifier_opt (parser,
14207 /*typename_keyword_p=*/false,
14208 /*check_dependency_p=*/false,
14210 /*is_declaration=*/false)
14212 /* Outside of a class-specifier, there must be a
14213 nested-name-specifier. */
14214 if (!nested_name_p &&
14215 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14217 constructor_p = false;
14218 /* If we still think that this might be a constructor-declarator,
14219 look for a class-name. */
14224 template <typename T> struct S { S(); };
14225 template <typename T> S<T>::S ();
14227 we must recognize that the nested `S' names a class.
14230 template <typename T> S<T>::S<T> ();
14232 we must recognize that the nested `S' names a template. */
14233 type_decl = cp_parser_class_name (parser,
14234 /*typename_keyword_p=*/false,
14235 /*template_keyword_p=*/false,
14237 /*check_dependency_p=*/false,
14238 /*class_head_p=*/false,
14239 /*is_declaration=*/false);
14240 /* If there was no class-name, then this is not a constructor. */
14241 constructor_p = !cp_parser_error_occurred (parser);
14244 /* If we're still considering a constructor, we have to see a `(',
14245 to begin the parameter-declaration-clause, followed by either a
14246 `)', an `...', or a decl-specifier. We need to check for a
14247 type-specifier to avoid being fooled into thinking that:
14251 is a constructor. (It is actually a function named `f' that
14252 takes one parameter (of type `int') and returns a value of type
14255 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14258 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14259 /* A parameter declaration begins with a decl-specifier,
14260 which is either the "attribute" keyword, a storage class
14261 specifier, or (usually) a type-specifier. */
14262 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14263 && !cp_parser_storage_class_specifier_opt (parser))
14266 bool pop_p = false;
14267 unsigned saved_num_template_parameter_lists;
14269 /* Names appearing in the type-specifier should be looked up
14270 in the scope of the class. */
14271 if (current_class_type)
14275 type = TREE_TYPE (type_decl);
14276 if (TREE_CODE (type) == TYPENAME_TYPE)
14278 type = resolve_typename_type (type,
14279 /*only_current_p=*/false);
14280 if (type == error_mark_node)
14282 cp_parser_abort_tentative_parse (parser);
14286 pop_p = push_scope (type);
14289 /* Inside the constructor parameter list, surrounding
14290 template-parameter-lists do not apply. */
14291 saved_num_template_parameter_lists
14292 = parser->num_template_parameter_lists;
14293 parser->num_template_parameter_lists = 0;
14295 /* Look for the type-specifier. */
14296 cp_parser_type_specifier (parser,
14297 CP_PARSER_FLAGS_NONE,
14298 /*is_friend=*/false,
14299 /*is_declarator=*/true,
14300 /*declares_class_or_enum=*/NULL,
14301 /*is_cv_qualifier=*/NULL);
14303 parser->num_template_parameter_lists
14304 = saved_num_template_parameter_lists;
14306 /* Leave the scope of the class. */
14310 constructor_p = !cp_parser_error_occurred (parser);
14314 constructor_p = false;
14315 /* We did not really want to consume any tokens. */
14316 cp_parser_abort_tentative_parse (parser);
14318 return constructor_p;
14321 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14322 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14323 they must be performed once we are in the scope of the function.
14325 Returns the function defined. */
14328 cp_parser_function_definition_from_specifiers_and_declarator
14329 (cp_parser* parser,
14330 tree decl_specifiers,
14337 /* Begin the function-definition. */
14338 success_p = begin_function_definition (decl_specifiers,
14342 /* If there were names looked up in the decl-specifier-seq that we
14343 did not check, check them now. We must wait until we are in the
14344 scope of the function to perform the checks, since the function
14345 might be a friend. */
14346 perform_deferred_access_checks ();
14350 /* If begin_function_definition didn't like the definition, skip
14351 the entire function. */
14352 error ("invalid function declaration");
14353 cp_parser_skip_to_end_of_block_or_statement (parser);
14354 fn = error_mark_node;
14357 fn = cp_parser_function_definition_after_declarator (parser,
14358 /*inline_p=*/false);
14363 /* Parse the part of a function-definition that follows the
14364 declarator. INLINE_P is TRUE iff this function is an inline
14365 function defined with a class-specifier.
14367 Returns the function defined. */
14370 cp_parser_function_definition_after_declarator (cp_parser* parser,
14374 bool ctor_initializer_p = false;
14375 bool saved_in_unbraced_linkage_specification_p;
14376 unsigned saved_num_template_parameter_lists;
14378 /* If the next token is `return', then the code may be trying to
14379 make use of the "named return value" extension that G++ used to
14381 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14383 /* Consume the `return' keyword. */
14384 cp_lexer_consume_token (parser->lexer);
14385 /* Look for the identifier that indicates what value is to be
14387 cp_parser_identifier (parser);
14388 /* Issue an error message. */
14389 error ("named return values are no longer supported");
14390 /* Skip tokens until we reach the start of the function body. */
14391 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14392 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14393 cp_lexer_consume_token (parser->lexer);
14395 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14396 anything declared inside `f'. */
14397 saved_in_unbraced_linkage_specification_p
14398 = parser->in_unbraced_linkage_specification_p;
14399 parser->in_unbraced_linkage_specification_p = false;
14400 /* Inside the function, surrounding template-parameter-lists do not
14402 saved_num_template_parameter_lists
14403 = parser->num_template_parameter_lists;
14404 parser->num_template_parameter_lists = 0;
14405 /* If the next token is `try', then we are looking at a
14406 function-try-block. */
14407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14408 ctor_initializer_p = cp_parser_function_try_block (parser);
14409 /* A function-try-block includes the function-body, so we only do
14410 this next part if we're not processing a function-try-block. */
14413 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14415 /* Finish the function. */
14416 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14417 (inline_p ? 2 : 0));
14418 /* Generate code for it, if necessary. */
14419 expand_or_defer_fn (fn);
14420 /* Restore the saved values. */
14421 parser->in_unbraced_linkage_specification_p
14422 = saved_in_unbraced_linkage_specification_p;
14423 parser->num_template_parameter_lists
14424 = saved_num_template_parameter_lists;
14429 /* Parse a template-declaration, assuming that the `export' (and
14430 `extern') keywords, if present, has already been scanned. MEMBER_P
14431 is as for cp_parser_template_declaration. */
14434 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14436 tree decl = NULL_TREE;
14437 tree parameter_list;
14438 bool friend_p = false;
14440 /* Look for the `template' keyword. */
14441 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14445 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14448 /* If the next token is `>', then we have an invalid
14449 specialization. Rather than complain about an invalid template
14450 parameter, issue an error message here. */
14451 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14453 cp_parser_error (parser, "invalid explicit specialization");
14454 begin_specialization ();
14455 parameter_list = NULL_TREE;
14459 /* Parse the template parameters. */
14460 begin_template_parm_list ();
14461 parameter_list = cp_parser_template_parameter_list (parser);
14462 parameter_list = end_template_parm_list (parameter_list);
14465 /* Look for the `>'. */
14466 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14467 /* We just processed one more parameter list. */
14468 ++parser->num_template_parameter_lists;
14469 /* If the next token is `template', there are more template
14471 if (cp_lexer_next_token_is_keyword (parser->lexer,
14473 cp_parser_template_declaration_after_export (parser, member_p);
14476 decl = cp_parser_single_declaration (parser,
14480 /* If this is a member template declaration, let the front
14482 if (member_p && !friend_p && decl)
14484 if (TREE_CODE (decl) == TYPE_DECL)
14485 cp_parser_check_access_in_redeclaration (decl);
14487 decl = finish_member_template_decl (decl);
14489 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14490 make_friend_class (current_class_type, TREE_TYPE (decl),
14491 /*complain=*/true);
14493 /* We are done with the current parameter list. */
14494 --parser->num_template_parameter_lists;
14497 finish_template_decl (parameter_list);
14499 /* Register member declarations. */
14500 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14501 finish_member_declaration (decl);
14503 /* If DECL is a function template, we must return to parse it later.
14504 (Even though there is no definition, there might be default
14505 arguments that need handling.) */
14506 if (member_p && decl
14507 && (TREE_CODE (decl) == FUNCTION_DECL
14508 || DECL_FUNCTION_TEMPLATE_P (decl)))
14509 TREE_VALUE (parser->unparsed_functions_queues)
14510 = tree_cons (NULL_TREE, decl,
14511 TREE_VALUE (parser->unparsed_functions_queues));
14514 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14515 `function-definition' sequence. MEMBER_P is true, this declaration
14516 appears in a class scope.
14518 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14519 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14522 cp_parser_single_declaration (cp_parser* parser,
14526 int declares_class_or_enum;
14527 tree decl = NULL_TREE;
14528 tree decl_specifiers;
14530 bool function_definition_p = false;
14532 /* This function is only used when processing a template
14534 if (innermost_scope_kind () != sk_template_parms
14535 && innermost_scope_kind () != sk_template_spec)
14538 /* Defer access checks until we know what is being declared. */
14539 push_deferring_access_checks (dk_deferred);
14541 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14544 = cp_parser_decl_specifier_seq (parser,
14545 CP_PARSER_FLAGS_OPTIONAL,
14547 &declares_class_or_enum);
14549 *friend_p = cp_parser_friend_p (decl_specifiers);
14551 /* There are no template typedefs. */
14552 if (cp_parser_typedef_p (decl_specifiers))
14554 error ("template declaration of `typedef'");
14555 decl = error_mark_node;
14558 /* Gather up the access checks that occurred the
14559 decl-specifier-seq. */
14560 stop_deferring_access_checks ();
14562 /* Check for the declaration of a template class. */
14563 if (declares_class_or_enum)
14565 if (cp_parser_declares_only_class_p (parser))
14567 decl = shadow_tag (decl_specifiers);
14569 decl = TYPE_NAME (decl);
14571 decl = error_mark_node;
14574 /* If it's not a template class, try for a template function. If
14575 the next token is a `;', then this declaration does not declare
14576 anything. But, if there were errors in the decl-specifiers, then
14577 the error might well have come from an attempted class-specifier.
14578 In that case, there's no need to warn about a missing declarator. */
14580 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14581 || !value_member (error_mark_node, decl_specifiers)))
14582 decl = cp_parser_init_declarator (parser,
14585 /*function_definition_allowed_p=*/true,
14587 declares_class_or_enum,
14588 &function_definition_p);
14590 pop_deferring_access_checks ();
14592 /* Clear any current qualification; whatever comes next is the start
14593 of something new. */
14594 parser->scope = NULL_TREE;
14595 parser->qualifying_scope = NULL_TREE;
14596 parser->object_scope = NULL_TREE;
14597 /* Look for a trailing `;' after the declaration. */
14598 if (!function_definition_p
14599 && (decl == error_mark_node
14600 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14601 cp_parser_skip_to_end_of_block_or_statement (parser);
14606 /* Parse a cast-expression that is not the operand of a unary "&". */
14609 cp_parser_simple_cast_expression (cp_parser *parser)
14611 return cp_parser_cast_expression (parser, /*address_p=*/false);
14614 /* Parse a functional cast to TYPE. Returns an expression
14615 representing the cast. */
14618 cp_parser_functional_cast (cp_parser* parser, tree type)
14620 tree expression_list;
14624 = cp_parser_parenthesized_expression_list (parser, false,
14625 /*non_constant_p=*/NULL);
14627 cast = build_functional_cast (type, expression_list);
14628 /* [expr.const]/1: In an integral constant expression "only type
14629 conversions to integral or enumeration type can be used". */
14630 if (cast != error_mark_node && !type_dependent_expression_p (type)
14631 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14633 if (cp_parser_non_integral_constant_expression
14634 (parser, "a call to a constructor"))
14635 return error_mark_node;
14640 /* Save the tokens that make up the body of a member function defined
14641 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14642 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14643 specifiers applied to the declaration. Returns the FUNCTION_DECL
14644 for the member function. */
14647 cp_parser_save_member_function_body (cp_parser* parser,
14648 tree decl_specifiers,
14652 cp_token_cache *cache;
14655 /* Create the function-declaration. */
14656 fn = start_method (decl_specifiers, declarator, attributes);
14657 /* If something went badly wrong, bail out now. */
14658 if (fn == error_mark_node)
14660 /* If there's a function-body, skip it. */
14661 if (cp_parser_token_starts_function_definition_p
14662 (cp_lexer_peek_token (parser->lexer)))
14663 cp_parser_skip_to_end_of_block_or_statement (parser);
14664 return error_mark_node;
14667 /* Remember it, if there default args to post process. */
14668 cp_parser_save_default_args (parser, fn);
14670 /* Create a token cache. */
14671 cache = cp_token_cache_new ();
14672 /* Save away the tokens that make up the body of the
14674 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14675 /* Handle function try blocks. */
14676 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14677 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14679 /* Save away the inline definition; we will process it when the
14680 class is complete. */
14681 DECL_PENDING_INLINE_INFO (fn) = cache;
14682 DECL_PENDING_INLINE_P (fn) = 1;
14684 /* We need to know that this was defined in the class, so that
14685 friend templates are handled correctly. */
14686 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14688 /* We're done with the inline definition. */
14689 finish_method (fn);
14691 /* Add FN to the queue of functions to be parsed later. */
14692 TREE_VALUE (parser->unparsed_functions_queues)
14693 = tree_cons (NULL_TREE, fn,
14694 TREE_VALUE (parser->unparsed_functions_queues));
14699 /* Parse a template-argument-list, as well as the trailing ">" (but
14700 not the opening ">"). See cp_parser_template_argument_list for the
14704 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14708 tree saved_qualifying_scope;
14709 tree saved_object_scope;
14710 bool saved_greater_than_is_operator_p;
14714 When parsing a template-id, the first non-nested `>' is taken as
14715 the end of the template-argument-list rather than a greater-than
14717 saved_greater_than_is_operator_p
14718 = parser->greater_than_is_operator_p;
14719 parser->greater_than_is_operator_p = false;
14720 /* Parsing the argument list may modify SCOPE, so we save it
14722 saved_scope = parser->scope;
14723 saved_qualifying_scope = parser->qualifying_scope;
14724 saved_object_scope = parser->object_scope;
14725 /* Parse the template-argument-list itself. */
14726 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14727 arguments = NULL_TREE;
14729 arguments = cp_parser_template_argument_list (parser);
14730 /* Look for the `>' that ends the template-argument-list. If we find
14731 a '>>' instead, it's probably just a typo. */
14732 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14734 if (!saved_greater_than_is_operator_p)
14736 /* If we're in a nested template argument list, the '>>' has to be
14737 a typo for '> >'. We emit the error message, but we continue
14738 parsing and we push a '>' as next token, so that the argument
14739 list will be parsed correctly.. */
14741 error ("`>>' should be `> >' within a nested template argument list");
14742 token = cp_lexer_peek_token (parser->lexer);
14743 token->type = CPP_GREATER;
14747 /* If this is not a nested template argument list, the '>>' is
14748 a typo for '>'. Emit an error message and continue. */
14749 error ("spurious `>>', use `>' to terminate a template argument list");
14750 cp_lexer_consume_token (parser->lexer);
14753 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14754 error ("missing `>' to terminate the template argument list");
14755 /* The `>' token might be a greater-than operator again now. */
14756 parser->greater_than_is_operator_p
14757 = saved_greater_than_is_operator_p;
14758 /* Restore the SAVED_SCOPE. */
14759 parser->scope = saved_scope;
14760 parser->qualifying_scope = saved_qualifying_scope;
14761 parser->object_scope = saved_object_scope;
14766 /* MEMBER_FUNCTION is a member function, or a friend. If default
14767 arguments, or the body of the function have not yet been parsed,
14771 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14773 cp_lexer *saved_lexer;
14775 /* If this member is a template, get the underlying
14777 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14778 member_function = DECL_TEMPLATE_RESULT (member_function);
14780 /* There should not be any class definitions in progress at this
14781 point; the bodies of members are only parsed outside of all class
14783 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14784 /* While we're parsing the member functions we might encounter more
14785 classes. We want to handle them right away, but we don't want
14786 them getting mixed up with functions that are currently in the
14788 parser->unparsed_functions_queues
14789 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14791 /* Make sure that any template parameters are in scope. */
14792 maybe_begin_member_template_processing (member_function);
14794 /* If the body of the function has not yet been parsed, parse it
14796 if (DECL_PENDING_INLINE_P (member_function))
14798 tree function_scope;
14799 cp_token_cache *tokens;
14801 /* The function is no longer pending; we are processing it. */
14802 tokens = DECL_PENDING_INLINE_INFO (member_function);
14803 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14804 DECL_PENDING_INLINE_P (member_function) = 0;
14806 /* If this is a local class, enter the scope of the containing
14808 function_scope = current_function_decl;
14809 if (function_scope)
14810 push_function_context_to (function_scope);
14812 /* Save away the current lexer. */
14813 saved_lexer = parser->lexer;
14814 /* Make a new lexer to feed us the tokens saved for this function. */
14815 parser->lexer = cp_lexer_new_from_tokens (tokens);
14816 parser->lexer->next = saved_lexer;
14818 /* Set the current source position to be the location of the first
14819 token in the saved inline body. */
14820 cp_lexer_peek_token (parser->lexer);
14822 /* Let the front end know that we going to be defining this
14824 start_function (NULL_TREE, member_function, NULL_TREE,
14825 SF_PRE_PARSED | SF_INCLASS_INLINE);
14827 /* Now, parse the body of the function. */
14828 cp_parser_function_definition_after_declarator (parser,
14829 /*inline_p=*/true);
14831 /* Leave the scope of the containing function. */
14832 if (function_scope)
14833 pop_function_context_from (function_scope);
14834 /* Restore the lexer. */
14835 parser->lexer = saved_lexer;
14838 /* Remove any template parameters from the symbol table. */
14839 maybe_end_member_template_processing ();
14841 /* Restore the queue. */
14842 parser->unparsed_functions_queues
14843 = TREE_CHAIN (parser->unparsed_functions_queues);
14846 /* If DECL contains any default args, remember it on the unparsed
14847 functions queue. */
14850 cp_parser_save_default_args (cp_parser* parser, tree decl)
14854 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14856 probe = TREE_CHAIN (probe))
14857 if (TREE_PURPOSE (probe))
14859 TREE_PURPOSE (parser->unparsed_functions_queues)
14860 = tree_cons (NULL_TREE, decl,
14861 TREE_PURPOSE (parser->unparsed_functions_queues));
14867 /* FN is a FUNCTION_DECL which may contains a parameter with an
14868 unparsed DEFAULT_ARG. Parse the default args now. */
14871 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14873 cp_lexer *saved_lexer;
14874 cp_token_cache *tokens;
14875 bool saved_local_variables_forbidden_p;
14878 /* While we're parsing the default args, we might (due to the
14879 statement expression extension) encounter more classes. We want
14880 to handle them right away, but we don't want them getting mixed
14881 up with default args that are currently in the queue. */
14882 parser->unparsed_functions_queues
14883 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14885 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14887 parameters = TREE_CHAIN (parameters))
14889 tree default_arg = TREE_PURPOSE (parameters);
14895 if (TREE_CODE (default_arg) != DEFAULT_ARG)
14896 /* This can happen for a friend declaration for a function
14897 already declared with default arguments. */
14900 /* Save away the current lexer. */
14901 saved_lexer = parser->lexer;
14902 /* Create a new one, using the tokens we have saved. */
14903 tokens = DEFARG_TOKENS (default_arg);
14904 parser->lexer = cp_lexer_new_from_tokens (tokens);
14906 /* Set the current source position to be the location of the
14907 first token in the default argument. */
14908 cp_lexer_peek_token (parser->lexer);
14910 /* Local variable names (and the `this' keyword) may not appear
14911 in a default argument. */
14912 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14913 parser->local_variables_forbidden_p = true;
14915 /* Parse the assignment-expression. */
14916 if (DECL_FRIEND_CONTEXT (fn))
14917 push_nested_class (DECL_FRIEND_CONTEXT (fn));
14918 else if (DECL_CLASS_SCOPE_P (fn))
14919 push_nested_class (DECL_CONTEXT (fn));
14920 parsed_arg = cp_parser_assignment_expression (parser);
14921 if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14922 pop_nested_class ();
14924 TREE_PURPOSE (parameters) = parsed_arg;
14926 /* Update any instantiations we've already created. */
14927 for (default_arg = TREE_CHAIN (default_arg);
14929 default_arg = TREE_CHAIN (default_arg))
14930 TREE_PURPOSE (TREE_PURPOSE (default_arg)) = parsed_arg;
14932 /* If the token stream has not been completely used up, then
14933 there was extra junk after the end of the default
14935 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14936 cp_parser_error (parser, "expected `,'");
14938 /* Restore saved state. */
14939 parser->lexer = saved_lexer;
14940 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14943 /* Restore the queue. */
14944 parser->unparsed_functions_queues
14945 = TREE_CHAIN (parser->unparsed_functions_queues);
14948 /* Parse the operand of `sizeof' (or a similar operator). Returns
14949 either a TYPE or an expression, depending on the form of the
14950 input. The KEYWORD indicates which kind of expression we have
14954 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14956 static const char *format;
14957 tree expr = NULL_TREE;
14958 const char *saved_message;
14959 bool saved_integral_constant_expression_p;
14961 /* Initialize FORMAT the first time we get here. */
14963 format = "types may not be defined in `%s' expressions";
14965 /* Types cannot be defined in a `sizeof' expression. Save away the
14967 saved_message = parser->type_definition_forbidden_message;
14968 /* And create the new one. */
14969 parser->type_definition_forbidden_message
14970 = xmalloc (strlen (format)
14971 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14973 sprintf ((char *) parser->type_definition_forbidden_message,
14974 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14976 /* The restrictions on constant-expressions do not apply inside
14977 sizeof expressions. */
14978 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14979 parser->integral_constant_expression_p = false;
14981 /* Do not actually evaluate the expression. */
14983 /* If it's a `(', then we might be looking at the type-id
14985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14988 bool saved_in_type_id_in_expr_p;
14990 /* We can't be sure yet whether we're looking at a type-id or an
14992 cp_parser_parse_tentatively (parser);
14993 /* Consume the `('. */
14994 cp_lexer_consume_token (parser->lexer);
14995 /* Parse the type-id. */
14996 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14997 parser->in_type_id_in_expr_p = true;
14998 type = cp_parser_type_id (parser);
14999 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15000 /* Now, look for the trailing `)'. */
15001 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15002 /* If all went well, then we're done. */
15003 if (cp_parser_parse_definitely (parser))
15005 /* Build a list of decl-specifiers; right now, we have only
15006 a single type-specifier. */
15007 type = build_tree_list (NULL_TREE,
15010 /* Call grokdeclarator to figure out what type this is. */
15011 expr = grokdeclarator (NULL_TREE,
15015 /*attrlist=*/NULL);
15019 /* If the type-id production did not work out, then we must be
15020 looking at the unary-expression production. */
15022 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15023 /* Go back to evaluating expressions. */
15026 /* Free the message we created. */
15027 free ((char *) parser->type_definition_forbidden_message);
15028 /* And restore the old one. */
15029 parser->type_definition_forbidden_message = saved_message;
15030 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15035 /* If the current declaration has no declarator, return true. */
15038 cp_parser_declares_only_class_p (cp_parser *parser)
15040 /* If the next token is a `;' or a `,' then there is no
15042 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15043 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15046 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15047 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15050 cp_parser_friend_p (tree decl_specifiers)
15052 while (decl_specifiers)
15054 /* See if this decl-specifier is `friend'. */
15055 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15056 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15059 /* Go on to the next decl-specifier. */
15060 decl_specifiers = TREE_CHAIN (decl_specifiers);
15066 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15067 Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS. */
15070 cp_parser_typedef_p (tree decl_specifiers)
15072 while (decl_specifiers)
15074 /* See if this decl-specifier is `typedef'. */
15075 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15076 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_TYPEDEF)
15079 /* Go on to the next decl-specifier. */
15080 decl_specifiers = TREE_CHAIN (decl_specifiers);
15087 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15088 issue an error message indicating that TOKEN_DESC was expected.
15090 Returns the token consumed, if the token had the appropriate type.
15091 Otherwise, returns NULL. */
15094 cp_parser_require (cp_parser* parser,
15095 enum cpp_ttype type,
15096 const char* token_desc)
15098 if (cp_lexer_next_token_is (parser->lexer, type))
15099 return cp_lexer_consume_token (parser->lexer);
15102 /* Output the MESSAGE -- unless we're parsing tentatively. */
15103 if (!cp_parser_simulate_error (parser))
15105 char *message = concat ("expected ", token_desc, NULL);
15106 cp_parser_error (parser, message);
15113 /* Like cp_parser_require, except that tokens will be skipped until
15114 the desired token is found. An error message is still produced if
15115 the next token is not as expected. */
15118 cp_parser_skip_until_found (cp_parser* parser,
15119 enum cpp_ttype type,
15120 const char* token_desc)
15123 unsigned nesting_depth = 0;
15125 if (cp_parser_require (parser, type, token_desc))
15128 /* Skip tokens until the desired token is found. */
15131 /* Peek at the next token. */
15132 token = cp_lexer_peek_token (parser->lexer);
15133 /* If we've reached the token we want, consume it and
15135 if (token->type == type && !nesting_depth)
15137 cp_lexer_consume_token (parser->lexer);
15140 /* If we've run out of tokens, stop. */
15141 if (token->type == CPP_EOF)
15143 if (token->type == CPP_OPEN_BRACE
15144 || token->type == CPP_OPEN_PAREN
15145 || token->type == CPP_OPEN_SQUARE)
15147 else if (token->type == CPP_CLOSE_BRACE
15148 || token->type == CPP_CLOSE_PAREN
15149 || token->type == CPP_CLOSE_SQUARE)
15151 if (nesting_depth-- == 0)
15154 /* Consume this token. */
15155 cp_lexer_consume_token (parser->lexer);
15159 /* If the next token is the indicated keyword, consume it. Otherwise,
15160 issue an error message indicating that TOKEN_DESC was expected.
15162 Returns the token consumed, if the token had the appropriate type.
15163 Otherwise, returns NULL. */
15166 cp_parser_require_keyword (cp_parser* parser,
15168 const char* token_desc)
15170 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15172 if (token && token->keyword != keyword)
15174 dyn_string_t error_msg;
15176 /* Format the error message. */
15177 error_msg = dyn_string_new (0);
15178 dyn_string_append_cstr (error_msg, "expected ");
15179 dyn_string_append_cstr (error_msg, token_desc);
15180 cp_parser_error (parser, error_msg->s);
15181 dyn_string_delete (error_msg);
15188 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15189 function-definition. */
15192 cp_parser_token_starts_function_definition_p (cp_token* token)
15194 return (/* An ordinary function-body begins with an `{'. */
15195 token->type == CPP_OPEN_BRACE
15196 /* A ctor-initializer begins with a `:'. */
15197 || token->type == CPP_COLON
15198 /* A function-try-block begins with `try'. */
15199 || token->keyword == RID_TRY
15200 /* The named return value extension begins with `return'. */
15201 || token->keyword == RID_RETURN);
15204 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15208 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15212 token = cp_lexer_peek_token (parser->lexer);
15213 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15216 /* Returns TRUE iff the next token is the "," or ">" ending a
15217 template-argument. */
15220 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15224 token = cp_lexer_peek_token (parser->lexer);
15225 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15228 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15229 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15232 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15237 token = cp_lexer_peek_nth_token (parser->lexer, n);
15238 if (token->type == CPP_LESS)
15240 /* Check for the sequence `<::' in the original code. It would be lexed as
15241 `[:', where `[' is a digraph, and there is no whitespace before
15243 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15246 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15247 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15253 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15254 or none_type otherwise. */
15256 static enum tag_types
15257 cp_parser_token_is_class_key (cp_token* token)
15259 switch (token->keyword)
15264 return record_type;
15273 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15276 cp_parser_check_class_key (enum tag_types class_key, tree type)
15278 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15279 pedwarn ("`%s' tag used in naming `%#T'",
15280 class_key == union_type ? "union"
15281 : class_key == record_type ? "struct" : "class",
15285 /* Issue an error message if DECL is redeclared with different
15286 access than its original declaration [class.access.spec/3].
15287 This applies to nested classes and nested class templates.
15290 static void cp_parser_check_access_in_redeclaration (tree decl)
15292 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15295 if ((TREE_PRIVATE (decl)
15296 != (current_access_specifier == access_private_node))
15297 || (TREE_PROTECTED (decl)
15298 != (current_access_specifier == access_protected_node)))
15299 error ("%D redeclared with different access", decl);
15302 /* Look for the `template' keyword, as a syntactic disambiguator.
15303 Return TRUE iff it is present, in which case it will be
15307 cp_parser_optional_template_keyword (cp_parser *parser)
15309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15311 /* The `template' keyword can only be used within templates;
15312 outside templates the parser can always figure out what is a
15313 template and what is not. */
15314 if (!processing_template_decl)
15316 error ("`template' (as a disambiguator) is only allowed "
15317 "within templates");
15318 /* If this part of the token stream is rescanned, the same
15319 error message would be generated. So, we purge the token
15320 from the stream. */
15321 cp_lexer_purge_token (parser->lexer);
15326 /* Consume the `template' keyword. */
15327 cp_lexer_consume_token (parser->lexer);
15335 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15336 set PARSER->SCOPE, and perform other related actions. */
15339 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15344 /* Get the stored value. */
15345 value = cp_lexer_consume_token (parser->lexer)->value;
15346 /* Perform any access checks that were deferred. */
15347 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15348 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15349 /* Set the scope from the stored value. */
15350 parser->scope = TREE_VALUE (value);
15351 parser->qualifying_scope = TREE_TYPE (value);
15352 parser->object_scope = NULL_TREE;
15355 /* Add tokens to CACHE until an non-nested END token appears. */
15358 cp_parser_cache_group (cp_parser *parser,
15359 cp_token_cache *cache,
15360 enum cpp_ttype end,
15367 /* Abort a parenthesized expression if we encounter a brace. */
15368 if ((end == CPP_CLOSE_PAREN || depth == 0)
15369 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15371 /* If we've reached the end of the file, stop. */
15372 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15374 /* Consume the next token. */
15375 token = cp_lexer_consume_token (parser->lexer);
15376 /* Add this token to the tokens we are saving. */
15377 cp_token_cache_push_token (cache, token);
15378 /* See if it starts a new group. */
15379 if (token->type == CPP_OPEN_BRACE)
15381 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15385 else if (token->type == CPP_OPEN_PAREN)
15386 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15387 else if (token->type == end)
15392 /* Begin parsing tentatively. We always save tokens while parsing
15393 tentatively so that if the tentative parsing fails we can restore the
15397 cp_parser_parse_tentatively (cp_parser* parser)
15399 /* Enter a new parsing context. */
15400 parser->context = cp_parser_context_new (parser->context);
15401 /* Begin saving tokens. */
15402 cp_lexer_save_tokens (parser->lexer);
15403 /* In order to avoid repetitive access control error messages,
15404 access checks are queued up until we are no longer parsing
15406 push_deferring_access_checks (dk_deferred);
15409 /* Commit to the currently active tentative parse. */
15412 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15414 cp_parser_context *context;
15417 /* Mark all of the levels as committed. */
15418 lexer = parser->lexer;
15419 for (context = parser->context; context->next; context = context->next)
15421 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15423 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15424 while (!cp_lexer_saving_tokens (lexer))
15425 lexer = lexer->next;
15426 cp_lexer_commit_tokens (lexer);
15430 /* Abort the currently active tentative parse. All consumed tokens
15431 will be rolled back, and no diagnostics will be issued. */
15434 cp_parser_abort_tentative_parse (cp_parser* parser)
15436 cp_parser_simulate_error (parser);
15437 /* Now, pretend that we want to see if the construct was
15438 successfully parsed. */
15439 cp_parser_parse_definitely (parser);
15442 /* Stop parsing tentatively. If a parse error has occurred, restore the
15443 token stream. Otherwise, commit to the tokens we have consumed.
15444 Returns true if no error occurred; false otherwise. */
15447 cp_parser_parse_definitely (cp_parser* parser)
15449 bool error_occurred;
15450 cp_parser_context *context;
15452 /* Remember whether or not an error occurred, since we are about to
15453 destroy that information. */
15454 error_occurred = cp_parser_error_occurred (parser);
15455 /* Remove the topmost context from the stack. */
15456 context = parser->context;
15457 parser->context = context->next;
15458 /* If no parse errors occurred, commit to the tentative parse. */
15459 if (!error_occurred)
15461 /* Commit to the tokens read tentatively, unless that was
15463 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15464 cp_lexer_commit_tokens (parser->lexer);
15466 pop_to_parent_deferring_access_checks ();
15468 /* Otherwise, if errors occurred, roll back our state so that things
15469 are just as they were before we began the tentative parse. */
15472 cp_lexer_rollback_tokens (parser->lexer);
15473 pop_deferring_access_checks ();
15475 /* Add the context to the front of the free list. */
15476 context->next = cp_parser_context_free_list;
15477 cp_parser_context_free_list = context;
15479 return !error_occurred;
15482 /* Returns true if we are parsing tentatively -- but have decided that
15483 we will stick with this tentative parse, even if errors occur. */
15486 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15488 return (cp_parser_parsing_tentatively (parser)
15489 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15492 /* Returns nonzero iff an error has occurred during the most recent
15493 tentative parse. */
15496 cp_parser_error_occurred (cp_parser* parser)
15498 return (cp_parser_parsing_tentatively (parser)
15499 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15502 /* Returns nonzero if GNU extensions are allowed. */
15505 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15507 return parser->allow_gnu_extensions_p;
15514 static GTY (()) cp_parser *the_parser;
15516 /* External interface. */
15518 /* Parse one entire translation unit. */
15521 c_parse_file (void)
15523 bool error_occurred;
15525 the_parser = cp_parser_new ();
15526 push_deferring_access_checks (flag_access_control
15527 ? dk_no_deferred : dk_no_check);
15528 error_occurred = cp_parser_translation_unit (the_parser);
15532 /* This variable must be provided by every front end. */
15536 #include "gt-cp-parser.h"